예제 #1
0
  /**
   * Attempts to parse a break statement from token stream. If parsing fails the stream is reset to
   * its initial position.
   *
   * @param tokens source token stream
   * @return BreakStatement object or null if tokens don't form a valid break statement
   */
  public static BreakStatement parse(TokenStream tokens) {
    Position pos = tokens.getPosition();
    tokens.pushMark();
    BreakStatement breakStatement = null;

    if (tokens.read().toString().equals("break")) {
      if (tokens.read().toString().equals(";")) breakStatement = new BreakStatement(pos);
    }

    tokens.popMark(breakStatement == null);
    return breakStatement;
  }
예제 #2
0
  /**
   * Attempts to parse a syntactic assignment expression from token stream. If parsing fails the
   * stream is reset to its initial position.
   *
   * @param tokens source token stream
   * @return Expression object or null if tokens don't form a valid expression
   */
  public static Expression parse(TokenStream tokens) {
    Position pos = tokens.getPosition();
    tokens.pushMark();
    Expression expr = BinaryExpression.parse(tokens);

    if (expr != null) {
      tokens.pushMark();
      Expression right = null;
      String op = tokens.read().toString();
      if (assignmentOperators.containsKey(op)) right = AssignmentExpression.parse(tokens);

      tokens.popMark(right == null);
      if (right != null) expr = new AssignmentExpression(op, expr, right, pos);
    }

    tokens.popMark(expr == null);
    return expr;
  }