Class ExpressionParser

java.lang.Object
com.igormaznitsa.jcp.expression.ExpressionParser

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

    • INSTANCE

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

    • ExpressionParser

      public ExpressionParser()
  • Method Details

    • getInstance

      public static ExpressionParser getInstance()
    • parse

      public ExpressionTree parse(String expressionStr, PreprocessorContext context) throws 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:
      IOException - it will be thrown if there is a problem to read the expression string
    • readExpression

      public ExpressionItem readExpression(PushbackReader reader, ExpressionTree tree, PreprocessorContext context, boolean insideBracket, boolean argument) throws 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:
      IOException - it will be thrown if there is a problem in reading from the reader
    • readFunction

      private ExpressionTree readFunction(AbstractFunction function, PushbackReader reader, PreprocessorContext context, @MustNotContainNull FilePositionInfo[] includeStack, String sources) throws 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:
      IOException - it will be thrown if there is any problem to read chars
    • readFunctionArgument

      ExpressionItem readFunctionArgument(PushbackReader reader, ExpressionTree tree, PreprocessorContext context, @MustNotContainNull FilePositionInfo[] callStack, String source) throws 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:
      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(PushbackReader reader, PreprocessorContext context) throws 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:
      IOException - it will be thrown if there is any error during a char reading