Class ExpressionParser


  • public final class ExpressionParser
    extends java.lang.Object
    This class is a parser allows to parse an expression and make a tree as the output
    • Field Detail

      • INSTANCE

        private static final ExpressionParser INSTANCE
        It contains the instance for the parser, because the parser is a singletone
    • Constructor Detail

      • ExpressionParser

        public ExpressionParser()
    • Method Detail

      • parse

        public ExpressionTree parse​(java.lang.String expressionStr,
                                    PreprocessorContext context)
                             throws java.io.IOException
        To parse an expression represented as a string and get a tree
        Parameters:
        expressionStr - the expression string to be parsed, must not be null
        context - a preprocessor context to be used to get variable values
        Returns:
        a tree containing parsed expression
        Throws:
        java.io.IOException - it will be thrown if there is a problem to read the expression string
      • readExpression

        public ExpressionItem readExpression​(java.io.PushbackReader reader,
                                             ExpressionTree tree,
                                             PreprocessorContext context,
                                             boolean insideBracket,
                                             boolean argument)
                                      throws java.io.IOException
        It reads an expression from a reader and fill a tree
        Parameters:
        reader - the reader to be used as the character source, must not be null
        tree - the result tree to be filled by read items, must not be null
        context - a preprocessor context to be used for variables
        insideBracket - the flag shows that the expression can be ended by a bracket
        argument - the flag shows that the expression can be ended by a comma
        Returns:
        the last read expression item (a comma or a bracket for instance), it can be null
        Throws:
        java.io.IOException - it will be thrown if there is a problem in reading from the reader
      • readFunction

        private ExpressionTree readFunction​(AbstractFunction function,
                                            java.io.PushbackReader reader,
                                            PreprocessorContext context,
                                            @MustNotContainNull
                                            FilePositionInfo[] includeStack,
                                            java.lang.String sources)
                                     throws java.io.IOException
        The auxiliary method allows to form a function and its arguments as a tree
        Parameters:
        function - the function which arguments will be read from the stream, must not be null
        reader - the reader to be used as the character source, must not be null
        context - a preprocessor context, it will be used for a user functions and variables
        includeStack - the current file include stack, can be null
        sources - the current source line, can be null
        Returns:
        an expression tree containing parsed function arguments
        Throws:
        java.io.IOException - it will be thrown if there is any problem to read chars
      • readFunctionArgument

        ExpressionItem readFunctionArgument​(java.io.PushbackReader reader,
                                            ExpressionTree tree,
                                            PreprocessorContext context,
                                            @MustNotContainNull
                                            FilePositionInfo[] callStack,
                                            java.lang.String source)
                                     throws java.io.IOException
        The auxiliary method allows to read a function argument
        Parameters:
        reader - a reader to be the character source, must not be null
        tree - the result tree to be filled by read items, must not be null
        context - a preprocessor context
        callStack - the current file call stack, can be null
        source - the current source line, can be null
        Returns:
        the last read expression item (a comma or a bracket)
        Throws:
        java.io.IOException - it will be thrown if there is any error during char reading from the reader
      • isDelimiterOrOperatorChar

        private static boolean isDelimiterOrOperatorChar​(char chr)
      • isDelimiter

        private static boolean isDelimiter​(char chr)
      • isOperatorChar

        private static boolean isOperatorChar​(char chr)
      • nextItem

        ExpressionItem nextItem​(java.io.PushbackReader reader,
                                PreprocessorContext context)
                         throws java.io.IOException
        Read the next item from the reader
        Parameters:
        reader - a reader to be used as the char source, must not be null
        context - a preprocessor context
        Returns:
        a read expression item, it can be null if the end is reached
        Throws:
        java.io.IOException - it will be thrown if there is any error during a char reading