Class BracketedSequenceGrammarMatcher
- java.lang.Object
-
- com.singularsys.jep.configurableparser.matchers.BracketedSequenceGrammarMatcher
-
- All Implemented Interfaces:
GrammarMatcher
,java.io.Serializable
public class BracketedSequenceGrammarMatcher extends java.lang.Object implements GrammarMatcher
A GrammarMatcher which matches lists of items with the syntax or a simple list[1,2,3,4]
. This differs from theListGrammarMatcher
in that the comma operator is treated like a normal operator and parsed using the ShuntingYard. So1,2,3,4
is parsed as a single subexpression rather than as four subexpressions.To set up first a new sequence operator must be created. This should be a binary operator with right associativity. It should have a precedence level lower than all other operators. This operator should be passed to the constructor of the FunctionSequenceGrammarMatcher and possibly also the BracketedSequenceGrammarMatcher.
OperatorTable2 ot = new StandardOperatorTable2(); Operator seqOp = new Operator(",", null, Operator.BINARY+Operator.RIGHT); ot.appendOperator(new OperatorKey() {}, seqOp, optab.getAssign()); // lower precedence than assignment ConfigurableParser cp = new ConfigurableParser(); cp.addHashComments(); cp.addSlashComments(); cp.addSingleQuoteStrings(); cp.addDoubleQuoteStrings(); cp.addWhiteSpace(); cp.addExponentNumbers(); cp.addSymbols("(",")","[","]"); // "," not include in symbols cp.setImplicitMultiplicationSymbols("(","["); cp.addOperatorTokenMatcher(); cp.addIdentifiers(); cp.addSemiColonTerminator(); cp.addWhiteSpaceCommentFilter(); cp.addBracketMatcher("(",")"); // Add the function matcher cp.addGrammarMatcher(new FunctionSequenceGrammarMatcher( cp.getSymbolToken("("), cp.getSymbolToken(")"), seqOp)); // Add the bracket matcher cp.addGrammarMatcher(new BracketedSequenceGrammarMatcher( cp.getSymbolToken("["), cp.getSymbolToken("]"), seqOp )); cp.addArrayAccessMatcher("[","]"); Jep jep = new Jep(ot,cp);
- See Also:
FunctionSequenceGrammarMatcher
,CPSeqOpTest
, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description BracketedSequenceGrammarMatcher(Token open, Token close, Operator seqOp)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
init(Jep jep)
Delayed initialisation, this methods is called whenever components of the jep instance are changed.Node
match(Lookahead2Iterator<Token> it, GrammarParser parser)
Test whether the input matches this pattern.
-
-
-
Constructor Detail
-
BracketedSequenceGrammarMatcher
public BracketedSequenceGrammarMatcher(Token open, Token close, Operator seqOp)
Constructor. The operator for the list is found in the init() method- Parameters:
open
- token representing an opening bracketclose
- token representing a closing bracketseqOp
- operator representing the sequence
-
-
Method Detail
-
init
public void init(Jep jep)
Description copied from interface:GrammarMatcher
Delayed initialisation, this methods is called whenever components of the jep instance are changed.- Specified by:
init
in interfaceGrammarMatcher
- Parameters:
jep
- the current jep instance.
-
match
public Node match(Lookahead2Iterator<Token> it, GrammarParser parser) throws ParseException
Description copied from interface:GrammarMatcher
Test whether the input matches this pattern.- Specified by:
match
in interfaceGrammarMatcher
- Parameters:
it
- An iterator inspecting the inputparser
- the parser to use when evaluating sub expressions- Returns:
- if matched returns a node representing the content, return null is does not match
- Throws:
ParseException
- if there is a syntactical error in the input.
-
-