Class Parser

    • Constructor Detail

      • Parser

        public Parser​(Grammar grammar,
                      TokenStream tokenStream)
        Instantiates a new Parser.
        Parameters:
        grammar - the grammar to use for parsing
        tokenStream - the TokenStream to parse from
    • Method Detail

      • consume

        public ParserToken consume()
        Description copied from interface: TokenStream
        Consumes a token from the stream.
        Specified by:
        consume in interface TokenStream
        Returns:
        the consumed token.
      • evaluate

        public <O> O evaluate​(Evaluator<? extends O> evaluator)
                       throws ParseException

        Parses the given resource and evaluates it with the given evaluator. Requires that the parse result in a single expression.

        Type Parameters:
        O - the return type of the evaluator
        Parameters:
        evaluator - the evaluator to use for transforming expressions
        Returns:
        the single return values from the evaluator
        Throws:
        ParseException - the parse exception
      • evaluateAll

        public <O> List<O> evaluateAll​(Evaluator<? extends O> evaluator)
                                throws ParseException

        Parses the given string and evaluates it with the given evaluator.

        Type Parameters:
        O - the return type of the evaluator
        Parameters:
        evaluator - the evaluator to use for transforming expressions
        Returns:
        A list of objects relating to the transformation of expressions by the given evaluator.
        Throws:
        ParseException - Something went wrong parsing
      • parseExpression

        public Expression parseExpression()
                                   throws ParseException
        Parses the token stream to get the next expression
        Returns:
        the next expression in the parse
        Throws:
        ParseException - Something went wrong parsing
      • parseExpression

        public Expression parseExpression​(ParserToken precedence)
                                   throws ParseException
        Parses the token stream to get the next expression
        Parameters:
        precedence - Uses the associated precedence of the handler associated with the given token or 0 if no precedence is defined.
        Returns:
        the next expression in the parse
        Throws:
        ParseException - Something went wrong parsing
      • parseExpression

        public Expression parseExpression​(ParserToken token,
                                          boolean isRightAssociative)
                                   throws ParseException
        Parses the token stream to get the next expression
        Parameters:
        token - Uses the associated precedence of the handler associated with the given token or 0 if no precedence is defined.
        isRightAssociative - True - if this is a right associative rule
        Returns:
        the next expression in the parse
        Throws:
        ParseException - Something went wrong parsing
      • parseExpression

        public Expression parseExpression​(int precedence)
                                   throws ParseException
        Parses the token stream to get the next expression
        Parameters:
        precedence - The precedence of the next prefix expression
        Returns:
        the next expression in the parse
        Throws:
        ParseException - Something went wrong parsing
      • parseExpression

        public Expression parseExpression​(int precedence,
                                          boolean isRightAssociative)
                                   throws ParseException
        Parses the token stream to get the next expression
        Parameters:
        precedence - The precedence of the next prefix expression
        isRightAssociative - True - if this is a right associative rule
        Returns:
        the next expression in the parse
        Throws:
        ParseException - Something went wrong parsing
      • parseExpressionList

        public <T extends ExpressionList<T> parseExpressionList​(Tag endOfList,
                                                                  Tag separator)
                                                           throws ParseException
        Parses a list of tokens ending with the endOfList tag and values separated using the separator tag.
        Parameters:
        endOfList - the Tag indicating the end of the list has been reached.
        separator - the Tag separating values of the list (null value means no separator).
        Returns:
        the list of parsed expressions
        Throws:
        ParseException - Something went wrong parsing the token stream
      • parseExpressionList

        public <T extends ExpressionList<T> parseExpressionList​(Tag startOfList,
                                                                  Tag endOfList,
                                                                  Tag separator)
                                                           throws ParseException
        Parses a list of tokens ending with the endOfList tag and values separated using the separator tag.
        Type Parameters:
        T - the type parameter
        Parameters:
        startOfList - the start of list
        endOfList - the Tag indicating the end of the list has been reached.
        separator - the Tag separating values of the list (null value means no separator).
        Returns:
        the list of parsed expressions
        Throws:
        ParseException - Something went wrong parsing the token stream
      • peek

        public ParserToken peek()
        Description copied from interface: TokenStream
        Peeks at the next token on the stream.
        Specified by:
        peek in interface TokenStream
        Returns:
        the next token on the stream (special EOF token if no more tokens exists)
      • token

        public ParserToken token()
        Description copied from interface: TokenStream
        Returns the last token extracted via the call to consume.
        Specified by:
        token in interface TokenStream
        Returns:
        the last extracted token via consume