/// <summary> /// Specfify the most part of the grammar for the parser /// The remaining part of the grammar is defined the static <see cref="ExpressionParser"/> /// constructor and limited by the <see cref="ExpressionParser.OnNewExpression"/> routine /// </summary> private void RegisterParselets(PrattParser parser) { parser.AddPrefix(TokenType.REAL, new RealParselet()); parser.AddPrefix(TokenType.LPAREN, new GroupParselet()); parser.AddPrefix(TokenType.PLUS, new PrefixOperatorParselet(PrecedenceTable.PREFIX)); parser.AddPrefix(TokenType.MINUS, new PrefixOperatorParselet(PrecedenceTable.PREFIX)); parser.AddPrefix(TokenType.ABS, new AbsFunctionParselet()); parser.AddInfix(TokenType.PLUS, new BinaryOperatorParselet(false, PrecedenceTable.SUM)); parser.AddInfix(TokenType.MINUS, new BinaryOperatorParselet(false, PrecedenceTable.SUM)); parser.AddInfix(TokenType.MUL, new BinaryOperatorParselet(false, PrecedenceTable.PROD)); parser.AddInfix(TokenType.DIV, new BinaryOperatorParselet(false, PrecedenceTable.PROD)); }
/// <summary> /// Initializes a new instance of the parser with the expression /// that should be parsed. /// </summary> /// <param name="text"></param> /// <remarks> /// Notice that the parser is a one-time-use object! /// </remarks> public ExpressionParser(string text) { _parser = new PrattParser(new Lexer(text), _ => RegisterParselets(_)); }