Class 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
    • Constructor Detail

      • DefaultXMLGrammar

        public DefaultXMLGrammar()
        Constructs the grammar
    • Method Detail

      • isDelimiter

        public boolean isDelimiter​(ExpressionToken token)
        Checks whether the token is a delimiter or not.
        Specified by:
        isDelimiter in interface Grammar
        Parameters:
        token - the token
        Returns:
        true if the token is delimiter false otherwise.
      • isDelimiter

        public boolean isDelimiter​(String token)
        Checks whether the token is a delimiter or not.
        Specified by:
        isDelimiter in interface Grammar
        Parameters:
        token - the token
        Returns:
        true if the token is delimiter false 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 interface Grammar
        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 approachable false 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.
        Specified by:
        isAllowed in interface Grammar
        Parameters:
        token - the token which is to be checked for its validity
        Returns:
        true if the token is allowed false otherwise.
      • 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 interface Grammar
        Returns:
        true if parser wants to exclude the blanks false otherwise.
      • isOperator

        public boolean isOperator​(ExpressionToken token)
        Checks whether the given token is an operator or not.
        Specified by:
        isOperator in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token is an operator false otherwise
      • isOperator

        public boolean isOperator​(String token)
        Checks whether the given token is an operator or not.
        Specified by:
        isOperator in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token is an operator false otherwise
      • isBinaryOperator

        public boolean isBinaryOperator​(ExpressionToken token)
        Checks whether the given token is a binary operator or not.
        Specified by:
        isBinaryOperator in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token is a binary operator false otherwise
      • isBinaryOperator

        public boolean isBinaryOperator​(String token)
        Checks whether the given token is a binary operator or not.
        Specified by:
        isBinaryOperator in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token is a binary operator false 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 interface Grammar
        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 interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token is an function false otherwise
      • isFunction

        public boolean isFunction​(String token)
        Checks whether the token is an function or not.
        Specified by:
        isFunction in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token is an function false otherwise
      • isUnary

        public boolean isUnary​(ExpressionToken operator)
        Checks whether the given operator is a unary operator or not.
        Specified by:
        isUnary in interface Grammar
        Parameters:
        operator - the operator to check
        Returns:
        true if the operator is used as unary operator false otherwise.
      • isUnary

        public boolean isUnary​(String operator)
        Checks whether the given operator is a unary operator or not.
        Specified by:
        isUnary in interface Grammar
        Parameters:
        operator - the operator to check
        Returns:
        true if the operator is used as unary operator false otherwise.
      • isLeftBracket

        public boolean isLeftBracket​(ExpressionToken token)
        Checks whether the token is a left bracket or not.
        Specified by:
        isLeftBracket in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token can be used as left bracket false otherwise.
      • isLeftBracket

        public boolean isLeftBracket​(String token)
        Checks whether the token is a left bracket or not.
        Specified by:
        isLeftBracket in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token can be used as left bracket false otherwise.
      • isRightBracket

        public boolean isRightBracket​(ExpressionToken token)
        Checks whether the token is a right bracket or not.
        Specified by:
        isRightBracket in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token can be used as right bracket false otherwise.
      • isRightBracket

        public boolean isRightBracket​(String token)
        Checks whether the token is a right bracket or not.
        Specified by:
        isRightBracket in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token can be used as right bracket false otherwise.
      • isBracket

        public boolean isBracket​(ExpressionToken token)
        Check whether the given token is a bracket or not.
        Specified by:
        isBracket in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token is a bracket false otherwise
      • isBracket

        public boolean isBracket​(String token)
        Check whether the given token is a bracket or not.
        Specified by:
        isBracket in interface Grammar
        Parameters:
        token - the token to check
        Returns:
        true if the token is a bracket false otherwise
      • getOppositeBracket

        public String getOppositeBracket​(String bracket)
        Returns the opposite bracket for the given bracket.
        Specified by:
        getOppositeBracket in interface Grammar
        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 interface Grammar
        Parameters:
        operator - the operator to check for precedence
        isUnary - 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 interface Grammar
        Parameters:
        operator - the operator to check for precedence
        isUnary - 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.