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 StringALLOWED_PATTERNThis is the constant name for allowed pattern tag in XMLstatic StringAPPROACHABLE_PATTERNThis is the constant name for approachable pattern tag in XMLstatic StringBRACKETThis is the constant name for bracket tag in XMLstatic StringBRACKETSThis is the constant name for brackets tag in XMLstatic StringDELIMITERThis is the constant name for delimiter tag in XMLstatic StringDELIMITERSThis is the constant name for delimiters tag in XMLStringFILE_PATHFile path for grammar configuration.static StringFUNCTIONSThis is the constant name for functions tag in XMLstatic StringIGNORE_BLANKThis is the constant name for ignore blank tag in XMLstatic StringLEFTThis is the constant name for left tag in XMLstatic StringNAMEThis is the constant name for name tag in XMLstatic StringOPERATORThis is the constant name for operator tag in XMLstatic StringPRECEDENCEThis is the constant name for precedence tag in XMLstatic StringPRODUCTION_RULEThis is the constant name for production rule tag in XMLstatic StringPRODUCTION_RULESThis is the constant name for production rules tag in XMLstatic StringRIGHTThis is the constant name for right tag in XMLstatic StringTRUEThis is the constant name for true tag in XMLstatic StringUNARY_OPERATORSThis 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 voidaddFunction(String functionName)Adds a function name to the grammar.StringgetOppositeBracket(String bracket)Returns the opposite bracket for the given bracket.intgetPrecedenceOrder(String operator, boolean isUnary)Gets the precedence order of the given operatorintgetPrecedenceOrder(ExpressionToken operator, boolean isUnary)Gets the precedence order of the given operatorbooleanisAllowed(String token)Checks whether the token is allowed or not.booleanisApproachable(String token)Checks whether the given token is approachable using any of the pattern or not.booleanisBinaryOperator(String token)Checks whether the given token is a binary operator or not.booleanisBinaryOperator(ExpressionToken token)Checks whether the given token is a binary operator or not.booleanisBracket(String token)Check whether the given token is a bracket or not.booleanisBracket(ExpressionToken token)Check whether the given token is a bracket or not.booleanisDelimiter(String token)Checks whether the token is a delimiter or not.booleanisDelimiter(ExpressionToken token)Checks whether the token is a delimiter or not.booleanisFunction(String token)Checks whether the token is an function or not.booleanisFunction(ExpressionToken token)Checks whether the token is an function or not.booleanisIgnoreBlank()Checks whether to ignore the blanks in expression or not.booleanisLeftBracket(String token)Checks whether the token is a left bracket or not.booleanisLeftBracket(ExpressionToken token)Checks whether the token is a left bracket or not.booleanisOperator(String token)Checks whether the given token is an operator or not.booleanisOperator(ExpressionToken token)Checks whether the given token is an operator or not.booleanisRightBracket(String token)Checks whether the token is a right bracket or not.booleanisRightBracket(ExpressionToken token)Checks whether the token is a right bracket or not.booleanisUnary(String operator)Checks whether the given operator is a unary operator or not.booleanisUnary(ExpressionToken operator)Checks whether the given operator is a unary operator or not.static voidmain(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:
isDelimiterin interfaceGrammar- Parameters:
token- the token- Returns:
trueif the token is delimiterfalseotherwise.
-
isDelimiter
public boolean isDelimiter(String token)
Checks whether the token is a delimiter or not.- Specified by:
isDelimiterin interfaceGrammar- Parameters:
token- the token- Returns:
trueif the token is delimiterfalseotherwise.
-
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:
isApproachablein interfaceGrammar- Parameters:
token- the token, partially or full constructed, to check whether it can approach to any expression token pattern or not.- Returns:
trueif the token pattern is approachablefalseotherwise.
-
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:
isIgnoreBlankin interfaceGrammar- Returns:
trueif parser wants to exclude the blanksfalseotherwise.
-
isOperator
public boolean isOperator(ExpressionToken token)
Checks whether the given token is an operator or not.- Specified by:
isOperatorin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token is an operatorfalseotherwise
-
isOperator
public boolean isOperator(String token)
Checks whether the given token is an operator or not.- Specified by:
isOperatorin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token is an operatorfalseotherwise
-
isBinaryOperator
public boolean isBinaryOperator(ExpressionToken token)
Checks whether the given token is a binary operator or not.- Specified by:
isBinaryOperatorin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token is a binary operatorfalseotherwise
-
isBinaryOperator
public boolean isBinaryOperator(String token)
Checks whether the given token is a binary operator or not.- Specified by:
isBinaryOperatorin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token is a binary operatorfalseotherwise
-
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:
addFunctionin 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:
isFunctionin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token is an functionfalseotherwise
-
isFunction
public boolean isFunction(String token)
Checks whether the token is an function or not.- Specified by:
isFunctionin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token is an functionfalseotherwise
-
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:
isLeftBracketin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token can be used as left bracketfalseotherwise.
-
isLeftBracket
public boolean isLeftBracket(String token)
Checks whether the token is a left bracket or not.- Specified by:
isLeftBracketin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token can be used as left bracketfalseotherwise.
-
isRightBracket
public boolean isRightBracket(ExpressionToken token)
Checks whether the token is a right bracket or not.- Specified by:
isRightBracketin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token can be used as right bracketfalseotherwise.
-
isRightBracket
public boolean isRightBracket(String token)
Checks whether the token is a right bracket or not.- Specified by:
isRightBracketin interfaceGrammar- Parameters:
token- the token to check- Returns:
trueif the token can be used as right bracketfalseotherwise.
-
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:
getOppositeBracketin 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:
getPrecedenceOrderin 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:
getPrecedenceOrderin 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.
-
-