public class ShuntingYard extends java.lang.Object implements GrammarParser
Modifier and Type | Class and Description |
---|---|
protected static class |
ShuntingYard.PrefixRes
Return result of following methods.
|
static class |
ShuntingYard.ShuntingYardGrammarParserFactory
Factory creating new ShuntingYard instances.
|
Modifier and Type | Field and Description |
---|---|
protected static boolean |
DUMP |
protected static Operator |
implicitMul |
protected Lookahead2Iterator<Token> |
it |
protected Jep |
jep |
protected java.util.List<GrammarMatcher> |
matchers |
protected NodeFactory |
nf |
protected java.util.Stack<Node> |
nodes |
protected java.util.Stack<Operator> |
ops |
protected static Operator |
sentinel |
Constructor and Description |
---|
ShuntingYard(Jep jep,
java.util.List<GrammarMatcher> gm)
Normal constructor, itterator set in
parse(Iterator) |
Modifier and Type | Method and Description |
---|---|
protected boolean |
compareOps(Operator op1,
Operator op2)
Compare operators based on their precedence and associativity.
|
protected void |
dumpState(java.lang.String msg) |
protected void |
expression()
Match prefixSuffix() optionally followed by a binary or ternary operator or an implicit multiplication.
|
Lookahead2Iterator<Token> |
getIterator() |
Node |
parse(java.util.Iterator<Token> input)
Main entry point, construct tree from sequence of tokens.
|
Node |
parsePrefixSuffix()
Callback function used by GrammerMatchers where the matcher expects
a simple number, variable, or function call with optional, prefix or suffix operator.
|
Node |
parseSubExpression()
Callback function used by GrammerMatchers
|
protected void |
popOp()
Pops an operator off the Operator stack, and creates a new node.
|
protected void |
prefix()
Matches identifies, numbers, prefix operators and plugged in grammar matchers.
|
protected void |
prefixSuffix()
A prefix() optionally followed by suffix operators.
|
protected ShuntingYard.PrefixRes |
prefixSuffixUnchecked()
A prefix() optionally followed by suffix operators.
|
protected ShuntingYard.PrefixRes |
prefixUnchecked()
Matches identifies, numbers, prefix operators and plugged in grammar matchers.
|
protected void |
pushOp(Operator op,
Token tok)
The pushOp function is worth some explanation
Say 1+2*3 is parsed.
|
void |
setIterator(Lookahead2Iterator<Token> it)
Set the iterator used by the
GrammarParser.parseSubExpression() |
protected static final boolean DUMP
protected java.util.Stack<Operator> ops
protected java.util.Stack<Node> nodes
protected Lookahead2Iterator<Token> it
protected java.util.List<GrammarMatcher> matchers
protected Jep jep
protected static final Operator sentinel
protected static final Operator implicitMul
protected NodeFactory nf
public ShuntingYard(Jep jep, java.util.List<GrammarMatcher> gm)
parse(Iterator)
jep
- Jep instancegm
- list of grammar rulespublic Node parse(java.util.Iterator<Token> input) throws ParseException
parse
in interface GrammarParser
input
- stream of tokensParseException
- if the input cannot be parsedpublic Node parseSubExpression() throws ParseException
parseSubExpression
in interface GrammarParser
ParseException
- if the input cannot be parsedprotected void expression() throws ParseException
ParseException
public Node parsePrefixSuffix() throws ParseException
parsePrefixSuffix
in interface GrammarParser
ParseException
- if the input cannot be parsedprotected void prefixSuffix() throws ParseException
ParseException
protected ShuntingYard.PrefixRes prefixSuffixUnchecked() throws ParseException
ParseException
- if node construction failsprotected ShuntingYard.PrefixRes prefixUnchecked() throws ParseException
ParseException
- if node construction failsprotected void prefix() throws ParseException
prefixUnchecked()
ParseException
- if input fails to match or error construction node treeprotected void pushOp(Operator op, Token tok) throws ParseException
op
- tok
- Token operator came fromParseException
protected boolean compareOps(Operator op1, Operator op2)
op1
- op2
- protected void popOp() throws ParseException
ParseException
protected void dumpState(java.lang.String msg)
public Lookahead2Iterator<Token> getIterator()
public void setIterator(Lookahead2Iterator<Token> it)
GrammarParser
GrammarParser.parseSubExpression()
setIterator
in interface GrammarParser
it
- the iteratorCopyright © 2018 Singular Systems http://www.singularsys.com/jep