static Interpolate() { // Parser for a text block var text = from s in asString(many1(attempt(satisfy(c => c != '{')))) select ScriptExpr.ConstString(s); // Parser for the interpolation block var format = from o in ch('{') from s in asString(many1(attempt(satisfy(c => c != '}')))) from c in ch('}') from d in ScriptParser.Parse(s) .Match( Right: dsl => result(dsl), Left: e => failure <ScriptExpr>(e)) select d; // Parser for many blocks var blocks = many(attempt(choice(attempt(text), format))); // Final parser which does some post processing on the blocks Parser = from b in blocks from _ in eof select b.IsEmpty ? ScriptExpr.ConstString("") : b.Tail.IsEmpty && b.Head is StringConstExpr ? b.Head : ScriptExpr.InterpString(b); }
public FailExpr(ScriptExpr expr) => Expr = expr;
/// <summary> /// Helper function for taking the tree expression that you get with comma /// separated values and flattens them inta Seq of ScriptExprs. /// </summary> static Seq <ScriptExpr> FlattenTuple(ScriptExpr expr) => expr is TupleExpr tuple
public static ScriptExpr Log(ScriptExpr expr) => new LogExpr(expr);
public static ScriptExpr Fail(ScriptExpr expr) => new FailExpr(expr);
public static ScriptExpr Parens(ScriptExpr expr) => new ParensExpr(expr);
public static ScriptExpr Postfix(ScriptExpr expr, string @operator) => new UnaryExpr(false, expr, @operator);
public static ScriptExpr Prefix(ScriptExpr expr, string @operator) => new UnaryExpr(true, expr, @operator);
public static ScriptExpr BooleanBinary(ScriptExpr left, ScriptExpr right, string @operator) => new BooleanBinaryExpr(left, right, @operator);
public static ScriptExpr Args(ScriptExpr left, ScriptExpr right) => new TupleExpr(left, right);