Class ConfigurableParser
- java.lang.Object
-
- com.singularsys.jep.configurableparser.ConfigurableParser
-
- All Implemented Interfaces:
JepComponent
,Parser
,java.io.Serializable
- Direct Known Subclasses:
StandardConfigurableParser
,StructuredParser
public class ConfigurableParser extends java.lang.Object implements Parser
A configurable parser.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected java.util.List<TokenFilter>
filters
protected java.util.List<GrammarMatcher>
g
protected GrammarParserFactory
gpf
protected Jep
jep
protected java.util.List<TokenMatcher>
m
protected OperatorTokenMatcher
otm
protected SymbolTokenMatcher
stm
protected TokenizerFactory
tf
protected Tokenizer
tk
-
Constructor Summary
Constructors Constructor Description ConfigurableParser()
Create a configurable parser with no matchers set.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description GrammarMatcher
addArrayAccessMatcher(java.lang.String open, java.lang.String close)
Matches array access: a[3]GrammarMatcher
addBracketMatcher(java.lang.String open, java.lang.String close)
Matches bracketed expressions: (2+3).TokenMatcher
addDoubleQuoteStrings()
Recognize double quote strings "...".TokenMatcher
addExponentNumbers()
Recognize numbers with optional exponents, 1.2e3, 1.2E-3, 1.2, 1.GrammarMatcher
addFunctionMatcher(java.lang.String open, java.lang.String close, java.lang.String sep)
Matches functions: atan2(y,x).GrammarMatcher
addGrammarMatcher(GrammarMatcher gm)
Adds a GrammarMatcherTokenMatcher
addHashComments()
Recognize comments of the form #....TokenMatcher
addIdentifiers()
Adds java-style function and variable names.GrammarMatcher
addListMatcher(java.lang.String open, java.lang.String close, java.lang.String sep)
Matches vectors/list: [1,2,3]GrammarMatcher
addListOrBracketMatcher(java.lang.String open, java.lang.String close, java.lang.String sep)
Matches bracketed expressions or lists depending on number of arguments.TokenMatcher
addOperatorTokenMatcher()
Recognize operators.TokenMatcher
addSemiColonTerminator()
Adds a matcher which will terminate the parsing process when a semi-colon is encountered.TokenMatcher
addSimpleNumbers()
Recognize numbers without exponents 1.2.TokenMatcher
addSingleQuoteStrings()
Recognize single quote strings '...'.void
addSlashComments()
Recognize comments of the form //... or /* ... */.TokenMatcher
addSymbols(java.lang.String... symbols)
Recognize given set of symbols.TokenFilter
addTokenFilter(TokenFilter tf1)
Adds a TokenFilter.TokenMatcher
addTokenMatcher(TokenMatcher tm)
Adds a TokenMatcher.TokenMatcher
addWhiteSpace()
Recognize java white space characters.TokenFilter
addWhiteSpaceCommentFilter()
Filter out whitespace and comments between the tokenizing and grammar matching stages.Node
continueParse()
Parse the next expressions from existing stream.java.util.Iterator<Token>
filter(java.util.List<Token> input)
Filters the tokens to remove whitespace etc.java.util.List<GrammarMatcher>
getGrammarMatchers()
Return the list of GrammarMatchers.GrammarParserFactory
getGrammarParserFactory()
Factory used to create GrammarParsersJep
getJep()
The Jep instance used by the parserJepComponent
getLightWeightInstance()
Returns a light weight instance, suitable for use in multiple evaluation threads.TokenMatcher
getOperatorTokenMatcher()
Return the OperatorTokenMatcher.SymbolToken
getSymbolToken(java.lang.String sym)
Return the SymbolToken for the given string.SymbolTokenMatcher
getSymbolTokenMatcher()
Return the SymbolTokenMatcher.java.util.List<TokenFilter>
getTokenFilters()
Return the list of filters.TokenizerFactory
getTokenizerFactory()
The factory used to create Tokenizersjava.util.List<TokenMatcher>
getTokenMatchers()
Return the list of TokenMatchersvoid
init(Jep jep1)
Initialize the component.Node
parse(java.io.Reader stream)
Parse a single equation.Node
parse(java.util.Iterator<Token> it)
Perform the syntax analysis stage.void
restart(java.io.Reader stream)
Load a new stream to read from.java.util.List<Token>
scan()
Scan next equations from an existing stream.java.util.List<Token>
scan(java.io.Reader stream)
Scan the input.void
setGrammarParserFactory(GrammarParserFactory gpf)
Sets the GrammarParserFactoryvoid
setImplicitMultiplicationSymbols(java.lang.String... symbols)
Sets those symbols which can appear on the right hand side of implicit multiplication.void
setTokenizerFactory(TokenizerFactory tf)
Sets the TokenizerFactory used to create Tokenizers.
-
-
-
Field Detail
-
m
protected java.util.List<TokenMatcher> m
-
filters
protected java.util.List<TokenFilter> filters
-
g
protected java.util.List<GrammarMatcher> g
-
tk
protected transient Tokenizer tk
-
jep
protected transient Jep jep
-
otm
protected OperatorTokenMatcher otm
-
stm
protected SymbolTokenMatcher stm
-
gpf
protected GrammarParserFactory gpf
-
tf
protected TokenizerFactory tf
-
-
Method Detail
-
init
public void init(Jep jep1)
Description copied from interface:JepComponent
Initialize the component. This methods is called whenever a component is added to Jep. Hence it allows components to keep track of the other components they may rely on.- Specified by:
init
in interfaceJepComponent
- Parameters:
jep1
- the current Jep instance
-
parse
public Node parse(java.io.Reader stream) throws ParseException
Parse a single equation.- Specified by:
parse
in interfaceParser
- Parameters:
stream
- the input stream to read from- Returns:
- top node of the expression
- Throws:
ParseException
- if there is a syntax error in the input or an empty expression
-
restart
public void restart(java.io.Reader stream)
Load a new stream to read from.- Specified by:
restart
in interfaceParser
- Parameters:
stream
- input stream- See Also:
continueParse()
-
continueParse
public Node continueParse() throws ParseException
Parse the next expressions from existing stream. Empty expressions such as;;
are silently ignored.- Specified by:
continueParse
in interfaceParser
- Returns:
- top node of the expression or null at end of file
- Throws:
ParseException
- if there is a syntax error in the input- See Also:
restart(java.io.Reader)
-
scan
public java.util.List<Token> scan(java.io.Reader stream) throws ParseException
Scan the input. Just perform the tokenizing stage. Not for normal use.- Parameters:
stream
- input- Returns:
- a list of tokens, including white space and comments, null at end of input. An empty list is returned for empty equations.
- Throws:
ParseException
- if there is a syntax error in the input
-
scan
public java.util.List<Token> scan() throws ParseException
Scan next equations from an existing stream. Not for normal use.- Returns:
- List of tokens, including white space and comments, null at end of input. An empty list is returned for empty equations.
- Throws:
ParseException
- if there is a syntax error in the input
-
filter
public java.util.Iterator<Token> filter(java.util.List<Token> input) throws ParseException
Filters the tokens to remove whitespace etc. Not for normal use.- Parameters:
input
- list of tokens- Returns:
- and iterator with the filtered tokens
- Throws:
ParseException
- if there is a syntax error in the input
-
parse
public Node parse(java.util.Iterator<Token> it) throws ParseException
Perform the syntax analysis stage. Not for normal used.- Parameters:
it
- an iterator with a list of tokens- Returns:
- top node of the parsed expression
- Throws:
ParseException
- if there is a gramatical error in the input
-
getLightWeightInstance
public JepComponent getLightWeightInstance()
Returns a light weight instance, suitable for use in multiple evaluation threads. Always returns null.- Specified by:
getLightWeightInstance
in interfaceJepComponent
- Returns:
- null
-
addTokenMatcher
public TokenMatcher addTokenMatcher(TokenMatcher tm)
Adds a TokenMatcher.- Parameters:
tm
- the TokenMatcher to add- Returns:
- the TokenMatcher
-
getTokenMatchers
public java.util.List<TokenMatcher> getTokenMatchers()
Return the list of TokenMatchers- Returns:
- the list
-
addGrammarMatcher
public GrammarMatcher addGrammarMatcher(GrammarMatcher gm)
Adds a GrammarMatcher- Parameters:
gm
- the GrammarMatcher to add- Returns:
- the GrammarMatcher
-
getGrammarMatchers
public java.util.List<GrammarMatcher> getGrammarMatchers()
Return the list of GrammarMatchers.- Returns:
- the list
-
addHashComments
public TokenMatcher addHashComments()
Recognize comments of the form #....- Returns:
- the corresponding matcher.
-
addSlashComments
public void addSlashComments()
Recognize comments of the form //... or /* ... */. Does not return a given matcher.
-
addDoubleQuoteStrings
public TokenMatcher addDoubleQuoteStrings()
Recognize double quote strings "...".- Returns:
- the corresponding matcher.
-
addSingleQuoteStrings
public TokenMatcher addSingleQuoteStrings()
Recognize single quote strings '...'.- Returns:
- the corresponding matcher.
-
addWhiteSpace
public TokenMatcher addWhiteSpace()
Recognize java white space characters.- Returns:
- the corresponding matcher.
-
addWhiteSpaceCommentFilter
public TokenFilter addWhiteSpaceCommentFilter()
Filter out whitespace and comments between the tokenizing and grammar matching stages.- Returns:
- the WhiteSpaceCommentFilter
-
addExponentNumbers
public TokenMatcher addExponentNumbers()
Recognize numbers with optional exponents, 1.2e3, 1.2E-3, 1.2, 1.- Returns:
- the corresponding matcher.
-
addSimpleNumbers
public TokenMatcher addSimpleNumbers()
Recognize numbers without exponents 1.2.- Returns:
- the corresponding matcher.
-
addOperatorTokenMatcher
public TokenMatcher addOperatorTokenMatcher()
Recognize operators.- Returns:
- the OperatorTokenMatcher.
-
getOperatorTokenMatcher
public TokenMatcher getOperatorTokenMatcher()
Return the OperatorTokenMatcher. Note this matcher is only added if addOperatorTokenMatcher() is called.- Returns:
- the OperatorTokenMatcher.
-
addSymbols
public TokenMatcher addSymbols(java.lang.String... symbols)
Recognize given set of symbols.- Parameters:
symbols
- one or more string for the symbols.- Returns:
- the SymbolTokenMatcher.
-
getSymbolTokenMatcher
public SymbolTokenMatcher getSymbolTokenMatcher()
Return the SymbolTokenMatcher. Note this matcher is only added if addSymbols() is called.- Returns:
- the SymbolTokenMatcher
-
getSymbolToken
public SymbolToken getSymbolToken(java.lang.String sym)
Return the SymbolToken for the given string.- Parameters:
sym
- string representing the symbol.- Returns:
- the corresponding SymbolToken.
-
setImplicitMultiplicationSymbols
public void setImplicitMultiplicationSymbols(java.lang.String... symbols)
Sets those symbols which can appear on the right hand side of implicit multiplication.- Parameters:
symbols
- one or more string for the symbols.
-
addIdentifiers
public TokenMatcher addIdentifiers()
Adds java-style function and variable names. These start with A-Z, a-z, or _ and are followed by zero or more alpha-numeric characters or _.- Returns:
- the IdentifierTokenMatcher.
-
addSemiColonTerminator
public TokenMatcher addSemiColonTerminator()
Adds a matcher which will terminate the parsing process when a semi-colon is encountered.- Returns:
- the TerminatorTokenMatcher
-
addBracketMatcher
public GrammarMatcher addBracketMatcher(java.lang.String open, java.lang.String close)
Matches bracketed expressions: (2+3).- Parameters:
open
- symbol for opening braceclose
- symbol for closing brace- Returns:
- the corresponding GrammarMatcher
-
addFunctionMatcher
public GrammarMatcher addFunctionMatcher(java.lang.String open, java.lang.String close, java.lang.String sep)
Matches functions: atan2(y,x).- Parameters:
open
- symbol for opening braceclose
- symbol for closing bracesep
- symbol for argument separator- Returns:
- the corresponding GrammarMatcher
-
addListMatcher
public GrammarMatcher addListMatcher(java.lang.String open, java.lang.String close, java.lang.String sep)
Matches vectors/list: [1,2,3]- Parameters:
open
- symbol for opening braceclose
- symbol for closing bracesep
- symbol for argument separator- Returns:
- the corresponding GrammarMatcher
-
addArrayAccessMatcher
public GrammarMatcher addArrayAccessMatcher(java.lang.String open, java.lang.String close)
Matches array access: a[3]- Parameters:
open
- symbol for opening braceclose
- symbol for closing brace- Returns:
- the corresponding GrammarMatcher
-
addListOrBracketMatcher
public GrammarMatcher addListOrBracketMatcher(java.lang.String open, java.lang.String close, java.lang.String sep)
Matches bracketed expressions or lists depending on number of arguments.- Parameters:
open
- symbol for opening braceclose
- symbol for closing bracesep
- symbol for argument separator- Returns:
- the corresponding GrammarMatcher
-
getJep
public Jep getJep()
The Jep instance used by the parser- Returns:
- the instance
- Since:
- 3.4
-
getTokenizerFactory
public TokenizerFactory getTokenizerFactory()
The factory used to create Tokenizers- Returns:
- the factory
- Since:
- 3.4
-
setTokenizerFactory
public void setTokenizerFactory(TokenizerFactory tf)
Sets the TokenizerFactory used to create Tokenizers.- Parameters:
tf
- factory to use- Since:
- 3.4
-
addTokenFilter
public TokenFilter addTokenFilter(TokenFilter tf1)
Adds a TokenFilter.- Parameters:
tf1
- the TokenFilter to add- Returns:
- the TokenFilter
-
getTokenFilters
public java.util.List<TokenFilter> getTokenFilters()
Return the list of filters.- Returns:
- the list
-
getGrammarParserFactory
public GrammarParserFactory getGrammarParserFactory()
Factory used to create GrammarParsers- Returns:
- the factory
- Since:
- 3.4
-
setGrammarParserFactory
public void setGrammarParserFactory(GrammarParserFactory gpf)
Sets the GrammarParserFactory- Parameters:
gpf
- the factory- Since:
- 3.4
-
-