Package com.gengoai.parsing
Class Parser
- java.lang.Object
-
- com.gengoai.parsing.Parser
-
- All Implemented Interfaces:
TokenStream
,Serializable
public class Parser extends Object implements TokenStream, Serializable
An implementation of a Pratt Parser inspired by Pratt Parsers: Expression Parsing Made Easy. An instance of a Parser is tied to a specific
Grammar
andTokenStream
. This implementation allows for operator precedence to be defined for eachParserHandler
and allows the precedence to be specified when retrieving the next expression.- Author:
- David B. Bracewell
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from interface com.gengoai.parsing.TokenStream
EOF, EOF_TOKEN
-
-
Constructor Summary
Constructors Constructor Description Parser(Grammar grammar, TokenStream tokenStream)
Instantiates a new Parser.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description ParserToken
consume()
Consumes a token from the stream.<O> O
evaluate(Evaluator<? extends O> evaluator)
Parses the given resource and evaluates it with the given evaluator.<O> List<O>
evaluateAll(Evaluator<? extends O> evaluator)
Parses the given string and evaluates it with the given evaluator.List<Expression>
parseAllExpressions()
Parse all expressions list.Expression
parseExpression()
Parses the token stream to get the next expressionExpression
parseExpression(int precedence)
Parses the token stream to get the next expressionExpression
parseExpression(int precedence, boolean isRightAssociative)
Parses the token stream to get the next expression<T extends Expression>
TparseExpression(int precedence, Class<T> tClass)
Expression
parseExpression(ParserToken precedence)
Parses the token stream to get the next expressionExpression
parseExpression(ParserToken token, boolean isRightAssociative)
Parses the token stream to get the next expression<T extends Expression>
TparseExpression(ParserToken precedence, Class<T> tClass)
<T extends Expression>
TparseExpression(Class<T> tClass)
<T extends Expression>
List<T>parseExpressionList(Tag endOfList, Tag separator)
Parses a list of tokens ending with theendOfList
tag and values separated using theseparator
tag.<T extends Expression>
List<T>parseExpressionList(Tag startOfList, Tag endOfList, Tag separator)
Parses a list of tokens ending with theendOfList
tag and values separated using theseparator
tag.ParserToken
peek()
Peeks at the next token on the stream.ParserToken
token()
Returns the last token extracted via the call to consume.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.gengoai.parsing.TokenStream
consume, hasNext
-
-
-
-
Constructor Detail
-
Parser
public Parser(Grammar grammar, TokenStream tokenStream)
Instantiates a new Parser.- Parameters:
grammar
- the grammar to use for parsingtokenStream
- theTokenStream
to parse from
-
-
Method Detail
-
consume
public ParserToken consume()
Description copied from interface:TokenStream
Consumes a token from the stream.- Specified by:
consume
in interfaceTokenStream
- 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
-
parseAllExpressions
public List<Expression> parseAllExpressions() throws ParseException
Parse all expressions list.- Returns:
- the list
- Throws:
ParseException
- the parse exception
-
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 <T extends Expression> T parseExpression(Class<T> tClass) throws ParseException
- Throws:
ParseException
-
parseExpression
public <T extends Expression> T parseExpression(ParserToken precedence, Class<T> tClass) throws ParseException
- Throws:
ParseException
-
parseExpression
public <T extends Expression> T parseExpression(int precedence, Class<T> tClass) throws ParseException
- Throws:
ParseException
-
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 expressionisRightAssociative
- 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 Expression> List<T> parseExpressionList(Tag endOfList, Tag separator) throws ParseException
Parses a list of tokens ending with theendOfList
tag and values separated using theseparator
tag.- Parameters:
endOfList
- theTag
indicating the end of the list has been reached.separator
- theTag
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 Expression> List<T> parseExpressionList(Tag startOfList, Tag endOfList, Tag separator) throws ParseException
Parses a list of tokens ending with theendOfList
tag and values separated using theseparator
tag.- Type Parameters:
T
- the type parameter- Parameters:
startOfList
- the start of listendOfList
- theTag
indicating the end of the list has been reached.separator
- theTag
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 interfaceTokenStream
- 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 interfaceTokenStream
- Returns:
- the last extracted token via consume
-
-