Example #1
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;
  }
Example #2
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;
  }
Example #3
0
  /**
   * Attempts to parse a parameter list from token stream. If parsing fails the stream is reset to
   * its initial position.
   *
   * @param tokens source token stream
   * @return ParameterList object or null if tokens don't form a valid parameter list
   */
  public static ParameterList parse(TokenStream tokens) {
    int line = tokens.getLine(), column = tokens.getColumn();
    tokens.pushMark();
    ParameterList paramList = null;

    if (tokens.read().toString().equals("(")) {
      List<Parameter> params = new LinkedList<Parameter>();
      Parameter param = Parameter.parse(tokens);
      while (param != null) {
        tokens.pushMark();
        params.add(param);
        param = null;
        if (tokens.read().toString().equals(",")) param = Parameter.parse(tokens);
        tokens.popMark(param == null);
      }

      if (tokens.read().toString().equals(")")) paramList = new ParameterList(params, line, column);
    }

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