Class ExpressionParser

java.lang.Object
org.scijava.parsington.ExpressionParser

public class ExpressionParser extends Object
A parser for mathematical expressions, using Dijkstra's famous shunting-yard algorithm.

It is important to note that this parser does not attempt to evaluate the expression in any way; rather, it only provides the parsed tree according to the desired operators.

Author:
Curtis Rueden
  • Constructor Details

    • ExpressionParser

      public ExpressionParser()
      Creates an expression parser with the standard set of operators and default separator symbols (, for group elements, ; for statements).
      See Also:
    • ExpressionParser

      public ExpressionParser(Collection<? extends Operator> operators)
      Creates an expression parser with custom operators and default separator symbols (, for group elements, ; for statements).
      Parameters:
      operators - The collection of operators available to expressions.
    • ExpressionParser

      public ExpressionParser(String elementSeparator, String statementSeparator)
      Creates an expression parser with custom separator symbols.
      Parameters:
      elementSeparator - The symbol to use for separating group elements.
      statementSeparator - The symbol to use for separating statements.
    • ExpressionParser

      public ExpressionParser(BiFunction<ExpressionParser,String,ParseOperation> parseOperationFactory)
      Creates an expression parser with custom ParseOperation behavior. Customizing this behavior allows you to control lower level parsing characteristics, including what character sequences constitute whitespace, literals, variables, operators, group terminators, element separators, and statement separators.
      Parameters:
      parseOperationFactory - A function producing ParseOperation objects with behavior customized to your requirements. The typical use case is to subclass ParseOperation to override one or more of its parseSomething methods, and then pass MyCustomParseOperation::new for this argument.
    • ExpressionParser

      public ExpressionParser(Collection<? extends Operator> operators, String elementSeparator, String statementSeparator)
      Creates an expression parser with custom operators and separator symbols.
      Parameters:
      operators - The collection of operators available to expressions.
      elementSeparator - The symbol to use for separating group elements.
      statementSeparator - The symbol to use for separating statements.
    • ExpressionParser

      public ExpressionParser(Collection<? extends Operator> operators, String elementSeparator, String statementSeparator, BiFunction<ExpressionParser,String,ParseOperation> parseOperationFactory)
      Creates an expression parser maximally customized to your requirements!
      Parameters:
      operators - The collection of operators available to expressions.
      elementSeparator - The symbol to use for separating group elements.
      statementSeparator - The symbol to use for separating statements.
      parseOperationFactory - A function producing ParseOperation objects with behavior customized to your requirements. The typical use case is to subclass ParseOperation to override one or more of its parseSomething methods, and then pass MyCustomParseOperation::new for this argument.
  • Method Details

    • parseTree

      public SyntaxTree parseTree(String expression)
      Parses the given mathematical expression into a syntax tree.
      Parameters:
      expression - The mathematical expression to parse.
      Returns:
      Parsed hierarchy of tokens.
      Throws:
      IllegalArgumentException - if the syntax of the expression is incorrect.
    • parsePostfix

      public LinkedList<Object> parsePostfix(String expression)
      Parses the given mathematical expression into a queue in Reverse Polish notation (i.e., postfix notation).
      Parameters:
      expression - The mathematical expression to parse.
      Returns:
      Parsed queue of tokens in postfix notation.
      Throws:
      IllegalArgumentException - if the syntax of the expression is incorrect.
    • operators

      public List<Operator> operators()
      Gets the list of operators available to expressions.
      Returns:
      A read-only view of the operators list.
    • elementSeparator

      public String elementSeparator()
      Separator symbol between group elements. The default symbol is comma (,). Example: f(1, 2)
      See Also:
    • statementSeparator

      public String statementSeparator()
      Separator symbol between statements. The default symbol is semicolon (;). Example: x = 1; y = 2; z = x + y
      See Also: