public Expression Parse(PrattParser parser, Expression left, Token token)
        {
            var right = parser.ParseExpression(
                _precedence - (_isRight ? 1 : 0));

            return(new BinaryOpExpression(left, token, right));
        }
        public Expression Parse(PrattParser parser, Token token)
        {
            var expression = parser.ParseExpression();

            parser.ConsumeNext(TokenType.RPAREN);
            return(expression);
        }
Exemple #3
0
 /// <summary>
 /// Execute the left denotation function of this token.
 /// </summary>
 /// <param name="parser">The parser state.</param>
 /// <param name="left">The value to the left of this token.</param>
 /// <returns>The left denotation of this token.</returns>
 public T Led(PrattParser <T> parser, T left)
 {
     if (LeftDenotation == null)
     {
         throw new ParseException(Line, Column, "Unknown symbol: '" + Id + "'.");
     }
     return(LeftDenotation(parser, left));
 }
Exemple #4
0
 /// <summary>
 /// Execute the null denotation function of this token.
 /// </summary>
 /// <returns>The value for the null denotation of this token.</returns>
 public T Nud(PrattParser <T> parser)
 {
     if (NullDenotation == null)
     {
         throw new ParseException(Line, Column, "Syntax error: '" + Id + "'.");
     }
     return(NullDenotation(parser));
 }
Exemple #5
0
        public Expression Parse(PrattParser parser, Token token)
        {
            parser.ConsumeNext(TokenType.LPAREN);
            var operand = parser.ParseExpression(PrecedenceTable.FUNCTION);

            parser.ConsumeNext(TokenType.RPAREN);
            return(new AbsExpression(token.Location, operand));
        }
Exemple #6
0
    private bool Accept <TInput, TOutput>(PrattParser <TInput, TOutput> p, BnfStringifyVisitor state)
    {
        var children = p.GetChildren().ToArray();

        if (children.Length == 0)
        {
            state.Append("PRATT()");
            return(true);
        }

        state.Append("PRATT(", children[0]);
        for (int i = 1; i < children.Length; i++)
        {
            state.Append(", ", children[i]);
        }

        state.Append(')');
        return(true);
    }
Exemple #7
0
    /// <summary>
    /// Parse the given text and return the corresponding value, or throw a parse error.
    /// </summary>
    /// <param name="text">The text to parse.</param>
    /// <returns>The value parsed from the text.</returns>
    /// <exception cref="ParseException">Thrown when the text has an invalid structure.</exception>
    public T Parse(string text)
    {
        var p = new PrattParser <T>(text, symbols);

        try
        {
            var x = p.Parse(0);
            p.EnsureInputConsumed();
            return(x);
        }
        catch (ParseException)
        {
            throw;
        }
        catch (Exception e)
        {
            p.Fail(text, e.Message);
            return(default(T)); // unreachable
        }
    }
        public Expression Parse(PrattParser parser, Token token)
        {
            var operand = parser.ParseExpression(_precedence);

            return(new PrefixExpression(token.TokenType, token.Location, operand));
        }
Exemple #9
0
 public Expression Parse(PrattParser parser, Token token)
 {
     return(new RealExpression(token.Text, token.Location));
 }
 /// <summary>
 /// Creates a Pratt parser, which is especially useful for mathematical expression parsing.
 /// </summary>
 /// <typeparam name="TOutput"></typeparam>
 /// <param name="setup"></param>
 /// <returns></returns>
 public static IParser <TInput, TOutput> Pratt <TOutput>(Action <IConfiguration <TInput, TOutput> > setup)
 => PrattParser <TInput, TOutput> .Configure(setup);