public BooleanExpressionParser() { Parser <Expression> boolean = BasicParser.IdentifierToken .Select(id => Expression.Constant(conditionalConstants.Contains(id))) .Named("boolean"); Parser <Expression> factor = (from lparen in Parse.Char('(') from expr in Parse.Ref(() => expression) from rparen in Parse.Char(')') select expr).Named("expression") .XOr(boolean); Parser <Expression> operand = ((from sign in Parse.Char('!').Token() from f in factor select Expression.Not(f)) .XOr(factor)).Token(); // The higher the expr number, the higher the precendence. In this case, AND takes precedence over OR Parser <Expression> expression2 = Parse.ChainOperator(and, operand, Expression.MakeBinary); expression = Parse.ChainOperator(or, expression2, Expression.MakeBinary); lambda = expression.End().Select(body => Expression.Lambda <Func <bool> >(body)); }
public static Parser <Expression> BinaryExpression(Parser <string> oper, Parser <Expression> operands) { return(Parse.ChainOperator(oper, operands, (op, left, right) => new BinaryExpression(op, left, right))); //return // from left in operands // from right in (from op in oper // from right in operands // select Tuple.Create(op, right)).Optional() // select right.IsEmpty ? left : new BinaryExpression(right.Get().Item1, left, right.Get().Item2); }
// Parse all comparison operations static Parser <ESExpression> Comparisons(Parser <ESExpression> input) => Parse.ChainOperator( Equal .Or(NotEqual) .Or(GreaterThanOrEqual) .Or(LessThanOrEqual) .Or(GreaterThan) .Or(LessThan), input, ESExpression.CombineExpression );
private Parser <Expression> BuildParser() { Parser <Expression> anyParserPromise = null !; // ReSharper disable once AccessToModifiedClosure Parser <Expression> anyParser = Parse.Ref(() => anyParserPromise); var trueParser = Parse.String("true").Token().Return(Expression.True); var falseParser = Parse.String("false").Token().Return(Expression.False); var identParser = Parse.Regex(IdentRegex).Token().Select(Expression.Ident); var constantParser = trueParser.Or(falseParser); var valueParser = constantParser.Or(identParser); var parenParser = ( from lp in Parse.Char('(').Token() from e in anyParser from rp in Parse.Char(')').Token() select e ).Or(valueParser); var notParser = ( from n in Parse.Char('~').Token() from e in parenParser select ~e ).Or(parenParser); // var andParser = ( // from a in notParser // from op in Parse.Char('&').Token() // from b in anyParser // select a & b // ).Or(notParser); // var orParser = ( // from a in andParser // from op in Parse.Char('|').Token() // from b in anyParser // select a | b // ).Or(andParser); var andParser = ( Parse.ChainOperator(Parse.Char('&').Token(), notParser, (_, a, b) => a & b) ).Or(notParser); var orParser = ( Parse.ChainOperator(Parse.Char('|').Token(), andParser, (_, a, b) => a | b) ).Or(andParser); anyParserPromise = orParser; return(anyParser); }
private static ParserWithContainer <Expression, KeyPropertyContainer> ConditionExpr(KeyPropertyContainer container) { return(new ParserWithContainer <Expression, KeyPropertyContainer>( Parse.ChainOperator(Or, AndConditionExpr(container).Parser, Expression.MakeBinary), container)); }
// Parse assing operations // This operation is right associative static Parser <ESExpression> AssignMove(Parser <ESExpression> input) => Parse.ChainOperator(MoveOp.Or(Assign), input, ESExpression.CombineExpression);
// Parse the simple math operations static Parser <ESExpression> AddSubtract(Parser <ESExpression> input) => Parse.ChainOperator(Add.Or(Subtract), input, ESExpression.CombineExpression);
// Parse things like multiply and divide static Parser <ESExpression> MultiplyDivideModulo(Parser <ESExpression> input) => Parse.ChainOperator(Multiply.Or(Divide).Or(Modulo), input, ESExpression.CombineExpression);
// Parse things like power and root static Parser <ESExpression> PowerRoot(Parser <ESExpression> input) => Parse.ChainOperator(Power, input, ESExpression.CombineExpression);
// Parse all conditional operations static Parser <ESExpression> Conditionals(Parser <ESExpression> input) => Parse.ChainOperator(And.Or(Or), input, ESExpression.CombineExpression);
// Parse convert operations static Parser <ESExpression> Casts(Parser <ESExpression> input) => Parse.ChainOperator( CastOp, input, ESExpression.CombineExpression );