예제 #1
0
  @Override
  public Expr parse(MagpieParser parser, Token token) {
    switch (token.getType()) {
      case BOOL:
        return Expr.bool(token.getPosition(), token.getBool());
      case INT:
        return Expr.int_(token.getPosition(), token.getInt());
      case NOTHING:
        return Expr.nothing(token.getPosition());
      case STRING:
        return Expr.string(token.getPosition(), token.getString());
    }

    throw new ParseException("Unexpected token type for literal.");
  }
예제 #2
0
  @Override
  public Expr parse(MagpieParser parser, Token token) {
    PositionSpan span = parser.span();
    String name = parser.consume(TokenType.NAME).getString();

    // Parse the parents, if any.
    List<String> parents = new ArrayList<String>();
    if (parser.match("is")) {
      do {
        parents.add(parser.consume(TokenType.NAME).getString());
      } while (parser.match(TokenType.COMMA));
    }

    parser.consume(TokenType.LINE);

    // Parse the doc comment if given.
    String doc = "";
    if (parser.match(TokenType.DOC_COMMENT, TokenType.LINE)) {
      doc = parser.last(2).getString();
    }

    Map<String, Field> fields = new HashMap<String, Field>();

    // Parse the body.
    while (!parser.match("end")) {
      if (parser.match("var")) parseField(parser, true, fields);
      else if (parser.match("val")) parseField(parser, false, fields);

      parser.consume(TokenType.LINE);
    }

    return Expr.class_(span.end(), doc, name, parents, fields);
  }