Class DefaultXMLGrammar
- java.lang.Object
-
- org.vedantatree.expressionoasis.grammar.DefaultXMLGrammar
-
- All Implemented Interfaces:
Grammar
public class DefaultXMLGrammar extends Object implements Grammar
This is the default XML based grammar implementation. It read the production rules from a XML file, and populate its rule sets. Use of XML makes it highly customizable without making any change to code. TODO Can we remove operators array and use precedence map keys? DefaultXMLGrammar can be loosely coupled with Compiler, i.e. can be picked from configuration.- Since:
- 3.1
-
-
Field Summary
Fields Modifier and Type Field Description static String
ALLOWED_PATTERN
This is the constant name for allowed pattern tag in XMLstatic String
APPROACHABLE_PATTERN
This is the constant name for approachable pattern tag in XMLstatic String
BRACKET
This is the constant name for bracket tag in XMLstatic String
BRACKETS
This is the constant name for brackets tag in XMLstatic String
DELIMITER
This is the constant name for delimiter tag in XMLstatic String
DELIMITERS
This is the constant name for delimiters tag in XMLString
FILE_PATH
File path for grammar configuration.static String
FUNCTIONS
This is the constant name for functions tag in XMLstatic String
IGNORE_BLANK
This is the constant name for ignore blank tag in XMLstatic String
LEFT
This is the constant name for left tag in XMLstatic String
NAME
This is the constant name for name tag in XMLstatic String
OPERATOR
This is the constant name for operator tag in XMLstatic String
PRECEDENCE
This is the constant name for precedence tag in XMLstatic String
PRODUCTION_RULE
This is the constant name for production rule tag in XMLstatic String
PRODUCTION_RULES
This is the constant name for production rules tag in XMLstatic String
RIGHT
This is the constant name for right tag in XMLstatic String
TRUE
This is the constant name for true tag in XMLstatic String
UNARY_OPERATORS
This is the constant name for unary operators tag in XML
-
Constructor Summary
Constructors Constructor Description DefaultXMLGrammar()
Constructs the grammar
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addFunction(String functionName)
Adds a function name to the grammar.String
getOppositeBracket(String bracket)
Returns the opposite bracket for the given bracket.int
getPrecedenceOrder(String operator, boolean isUnary)
Gets the precedence order of the given operatorint
getPrecedenceOrder(ExpressionToken operator, boolean isUnary)
Gets the precedence order of the given operatorboolean
isAllowed(String token)
Checks whether the token is allowed or not.boolean
isApproachable(String token)
Checks whether the given token is approachable using any of the pattern or not.boolean
isBinaryOperator(String token)
Checks whether the given token is a binary operator or not.boolean
isBinaryOperator(ExpressionToken token)
Checks whether the given token is a binary operator or not.boolean
isBracket(String token)
Check whether the given token is a bracket or not.boolean
isBracket(ExpressionToken token)
Check whether the given token is a bracket or not.boolean
isDelimiter(String token)
Checks whether the token is a delimiter or not.boolean
isDelimiter(ExpressionToken token)
Checks whether the token is a delimiter or not.boolean
isFunction(String token)
Checks whether the token is an function or not.boolean
isFunction(ExpressionToken token)
Checks whether the token is an function or not.boolean
isIgnoreBlank()
Checks whether to ignore the blanks in expression or not.boolean
isLeftBracket(String token)
Checks whether the token is a left bracket or not.boolean
isLeftBracket(ExpressionToken token)
Checks whether the token is a left bracket or not.boolean
isOperator(String token)
Checks whether the given token is an operator or not.boolean
isOperator(ExpressionToken token)
Checks whether the given token is an operator or not.boolean
isRightBracket(String token)
Checks whether the token is a right bracket or not.boolean
isRightBracket(ExpressionToken token)
Checks whether the token is a right bracket or not.boolean
isUnary(String operator)
Checks whether the given operator is a unary operator or not.boolean
isUnary(ExpressionToken operator)
Checks whether the given operator is a unary operator or not.static void
main(String[] args)
Runs the test for grammar.
-
-
-
Field Detail
-
FILE_PATH
public final String FILE_PATH
File path for grammar configuration.
-
PRODUCTION_RULES
public static final String PRODUCTION_RULES
This is the constant name for production rules tag in XML- See Also:
- Constant Field Values
-
UNARY_OPERATORS
public static final String UNARY_OPERATORS
This is the constant name for unary operators tag in XML- See Also:
- Constant Field Values
-
FUNCTIONS
public static final String FUNCTIONS
This is the constant name for functions tag in XML- See Also:
- Constant Field Values
-
DELIMITERS
public static final String DELIMITERS
This is the constant name for delimiters tag in XML- See Also:
- Constant Field Values
-
BRACKETS
public static final String BRACKETS
This is the constant name for brackets tag in XML- See Also:
- Constant Field Values
-
IGNORE_BLANK
public static final String IGNORE_BLANK
This is the constant name for ignore blank tag in XML- See Also:
- Constant Field Values
-
TRUE
public static final String TRUE
This is the constant name for true tag in XML- See Also:
- Constant Field Values
-
NAME
public static final String NAME
This is the constant name for name tag in XML- See Also:
- Constant Field Values
-
BRACKET
public static final String BRACKET
This is the constant name for bracket tag in XML- See Also:
- Constant Field Values
-
LEFT
public static final String LEFT
This is the constant name for left tag in XML- See Also:
- Constant Field Values
-
RIGHT
public static final String RIGHT
This is the constant name for right tag in XML- See Also:
- Constant Field Values
-
OPERATOR
public static final String OPERATOR
This is the constant name for operator tag in XML- See Also:
- Constant Field Values
-
PRECEDENCE
public static final String PRECEDENCE
This is the constant name for precedence tag in XML- See Also:
- Constant Field Values
-
DELIMITER
public static final String DELIMITER
This is the constant name for delimiter tag in XML- See Also:
- Constant Field Values
-
PRODUCTION_RULE
public static final String PRODUCTION_RULE
This is the constant name for production rule tag in XML- See Also:
- Constant Field Values
-
APPROACHABLE_PATTERN
public static final String APPROACHABLE_PATTERN
This is the constant name for approachable pattern tag in XML- See Also:
- Constant Field Values
-
ALLOWED_PATTERN
public static final String ALLOWED_PATTERN
This is the constant name for allowed pattern tag in XML- See Also:
- Constant Field Values
-
-
Method Detail
-
isDelimiter
public boolean isDelimiter(ExpressionToken token)
Checks whether the token is a delimiter or not.- Specified by:
isDelimiter
in interfaceGrammar
- Parameters:
token
- the token- Returns:
true
if the token is delimiterfalse
otherwise.
-
isDelimiter
public boolean isDelimiter(String token)
Checks whether the token is a delimiter or not.- Specified by:
isDelimiter
in interfaceGrammar
- Parameters:
token
- the token- Returns:
true
if the token is delimiterfalse
otherwise.
-
isApproachable
public boolean isApproachable(String token)
Checks whether the given token is approachable using any of the pattern or not. Given token can be partially or fully constructed token during parsing process. Parser generally calls this method to check whether the current token can be combined with next character of expression to form some meaningful token or not. If not, then it utilize the existing collected characters as one token, otherwise it keep collecting characters.- Specified by:
isApproachable
in interfaceGrammar
- Parameters:
token
- the token, partially or full constructed, to check whether it can approach to any expression token pattern or not.- Returns:
true
if the token pattern is approachablefalse
otherwise.
-
isAllowed
public boolean isAllowed(String token)
Checks whether the token is allowed or not. A token is fully constructed token. Parser generally calls this method to check whether the current token is a valid token as per the production rules or not.
-
isIgnoreBlank
public boolean isIgnoreBlank()
Checks whether to ignore the blanks in expression or not. It tells the parser whether to exclude the extra blanks while parsing or not.- Specified by:
isIgnoreBlank
in interfaceGrammar
- Returns:
true
if parser wants to exclude the blanksfalse
otherwise.
-
isOperator
public boolean isOperator(ExpressionToken token)
Checks whether the given token is an operator or not.- Specified by:
isOperator
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token is an operatorfalse
otherwise
-
isOperator
public boolean isOperator(String token)
Checks whether the given token is an operator or not.- Specified by:
isOperator
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token is an operatorfalse
otherwise
-
isBinaryOperator
public boolean isBinaryOperator(ExpressionToken token)
Checks whether the given token is a binary operator or not.- Specified by:
isBinaryOperator
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token is a binary operatorfalse
otherwise
-
isBinaryOperator
public boolean isBinaryOperator(String token)
Checks whether the given token is a binary operator or not.- Specified by:
isBinaryOperator
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token is a binary operatorfalse
otherwise
-
addFunction
public void addFunction(String functionName)
Adds a function name to the grammar. TODO: should probably look up function precedence from grammar config file rather than hard coding it to 13.- Specified by:
addFunction
in interfaceGrammar
- Parameters:
name
- name of the function to add
-
isFunction
public boolean isFunction(ExpressionToken token)
Checks whether the token is an function or not.- Specified by:
isFunction
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token is an functionfalse
otherwise
-
isFunction
public boolean isFunction(String token)
Checks whether the token is an function or not.- Specified by:
isFunction
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token is an functionfalse
otherwise
-
isUnary
public boolean isUnary(ExpressionToken operator)
Checks whether the given operator is a unary operator or not.
-
isUnary
public boolean isUnary(String operator)
Checks whether the given operator is a unary operator or not.
-
isLeftBracket
public boolean isLeftBracket(ExpressionToken token)
Checks whether the token is a left bracket or not.- Specified by:
isLeftBracket
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token can be used as left bracketfalse
otherwise.
-
isLeftBracket
public boolean isLeftBracket(String token)
Checks whether the token is a left bracket or not.- Specified by:
isLeftBracket
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token can be used as left bracketfalse
otherwise.
-
isRightBracket
public boolean isRightBracket(ExpressionToken token)
Checks whether the token is a right bracket or not.- Specified by:
isRightBracket
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token can be used as right bracketfalse
otherwise.
-
isRightBracket
public boolean isRightBracket(String token)
Checks whether the token is a right bracket or not.- Specified by:
isRightBracket
in interfaceGrammar
- Parameters:
token
- the token to check- Returns:
true
if the token can be used as right bracketfalse
otherwise.
-
isBracket
public boolean isBracket(ExpressionToken token)
Check whether the given token is a bracket or not.
-
isBracket
public boolean isBracket(String token)
Check whether the given token is a bracket or not.
-
getOppositeBracket
public String getOppositeBracket(String bracket)
Returns the opposite bracket for the given bracket.- Specified by:
getOppositeBracket
in interfaceGrammar
- Parameters:
bracket
- the bracket for which we need to find the opposite bracket- Returns:
- the opposite part of bracket w.r.t given bracket
-
getPrecedenceOrder
public int getPrecedenceOrder(ExpressionToken operator, boolean isUnary)
Gets the precedence order of the given operator- Specified by:
getPrecedenceOrder
in interfaceGrammar
- Parameters:
operator
- the operator to check for precedenceisUnary
- true if the operator is unary, as an operator can behave either as unary or as binary- Returns:
- the precedence order of the operator
-
getPrecedenceOrder
public int getPrecedenceOrder(String operator, boolean isUnary)
Gets the precedence order of the given operator- Specified by:
getPrecedenceOrder
in interfaceGrammar
- Parameters:
operator
- the operator to check for precedenceisUnary
- true if the operator is unary, as an operator can behave either as unary or as binary- Returns:
- the precedence order of the operator
-
main
public static void main(String[] args)
Runs the test for grammar.
-
-