Esempio n. 1
0
 /**
  * Parses an expression. Returns a object of type Node, does not catch errors. Does not set the
  * topNode variable of the JEP instance. This method should generally be used with the {@link
  * #evaluate evaluate} method rather than getValueAsObject.
  *
  * @param expression represented as a string.
  * @return The top node of a tree representing the parsed expression.
  * @throws ParseException
  * @since 2.3.0 alpha
  * @since 2.3.0 beta - will raise exception if errorList non empty
  */
 public Node parse(String expression) throws ParseException {
   java.io.StringReader sr = new java.io.StringReader(expression);
   errorList.removeAllElements();
   Node node = parser.parseStream(sr, this);
   if (this.hasError()) throw new ParseException(getErrorInfo());
   return node;
 }
Esempio n. 2
0
  /**
   * Parses the expression. If there are errors in the expression, they are added to the <code>
   * errorList</code> member. Errors can be obtained through <code>getErrorInfo()</code>.
   *
   * @param expression_in The input expression string
   * @return the top node of the expression tree if the parse was successful, <code>null</code>
   *     otherwise
   */
  public Node parseExpression(String expression_in) {
    Reader reader = new StringReader(expression_in);

    try {
      // try parsing
      errorList.removeAllElements();
      topNode = parser.parseStream(reader, this);

      // if there is an error in the list, the parse failed
      // so set topNode to null
      if (hasError()) topNode = null;
    } catch (Throwable e) {
      // an exception was thrown, so there is no parse tree
      topNode = null;

      // check the type of error
      if (e instanceof ParseException) {
        // the ParseException object contains additional error
        // information
        errorList.addElement(((ParseException) e).getMessage());
        // getErrorInfo());
      } else {
        // if the exception was not a ParseException, it was most
        // likely a syntax error
        if (debug) {
          System.out.println(e.getMessage());
          e.printStackTrace();
        }
        errorList.addElement("Syntax error");
      }
    }

    // If traversing is enabled, print a dump of the tree to
    // standard output
    if (traverse && !hasError()) {
      ParserVisitor v = new ParserDumpVisitor();
      try {
        topNode.jjtAccept(v, null);
      } catch (ParseException e) {
        errorList.addElement(e.getMessage());
      }
    }

    return topNode;
  }