/** * Looks at the next token. */ public int lookAhead() { return lookAhead(0); }
private boolean nextIsClassCast() { int i = nextIsClassType(1); if (i < 0) return false; int t = lex.lookAhead(i); if (t != ')') return false; t = lex.lookAhead(i + 1); return t == '(' || t == NULL || t == StringL || t == Identifier || t == THIS || t == SUPER || t == NEW || t == TRUE || t == FALSE || t == LongConstant || t == IntConstant || t == CharConstant || t == DoubleConstant || t == FloatConstant; }
private ASTree parseInitializer(SymbolTable tbl) throws CompileError { if (lex.lookAhead() == '{') return parseArrayInitializer(tbl); else return parseExpression(tbl); }
private ASTList parseArraySize(SymbolTable tbl) throws CompileError { ASTList list = null; while (lex.lookAhead() == '[') list = ASTList.append(list, parseArrayIndex(tbl)); return list; }
private int parseArrayDimension() throws CompileError { int arrayDim = 0; while (lex.lookAhead() == '[') { ++arrayDim; lex.get(); if (lex.get() != ']') throw new CompileError("] is missing", lex); } return arrayDim; }
private ASTree parseBinaryExpr(SymbolTable tbl) throws CompileError { ASTree expr = parseUnaryExpr(tbl); for (;;) { int t = lex.lookAhead(); int p = getOpPrecedence(t); if (p == 0) return expr; else expr = binaryExpr2(tbl, expr, p); } }
private ASTList parseMemberMods() { int t; ASTList list = null; while (true) { t = lex.lookAhead(); if (t == ABSTRACT || t == FINAL || t == PUBLIC || t == PROTECTED || t == PRIVATE || t == SYNCHRONIZED || t == STATIC || t == VOLATILE || t == TRANSIENT || t == STRICT) list = new ASTList(new Keyword(lex.get()), list); else break; } return list; }
public ASTree parseExpression(SymbolTable tbl) throws CompileError { ASTree left = parseConditionalExpr(tbl); if (!isAssignOp(lex.lookAhead())) return left; int t = lex.get(); ASTree right = parseExpression(tbl); return AssignExpr.makeAssign(t, left, right); }
private Stmnt parseExprList(SymbolTable tbl) throws CompileError { Stmnt expr = null; for (;;) { Stmnt e = new Stmnt(EXPR, parseExpression(tbl)); expr = (Stmnt)ASTList.concat(expr, new Stmnt(BLOCK, e)); if (lex.lookAhead() == ',') lex.get(); else return expr; } }
private ASTree parseArrayIndex(SymbolTable tbl) throws CompileError { lex.get(); // '[' if (lex.lookAhead() == ']') { lex.get(); return null; } else { ASTree index = parseExpression(tbl); if (lex.get() != ']') throw new CompileError("] is missing", lex); return index; } }
public MethodDecl parseMethod2(SymbolTable tbl, MethodDecl md) throws CompileError { Stmnt body = null; if (lex.lookAhead() == ';') lex.get(); else { body = parseBlock(tbl); if (body == null) body = new Stmnt(BLOCK); } md.sublist(4).setHead(body); return md; }
private Stmnt parseReturn(SymbolTable tbl) throws CompileError { int t = lex.get(); // RETURN Stmnt s = new Stmnt(t); if (lex.lookAhead() != ';') s.setLeft(parseExpression(tbl)); if (lex.get() != ';') throw new CompileError("; is missing", lex); return s; }
private ASTList parseClassType(SymbolTable tbl) throws CompileError { ASTList list = null; for (;;) { if (lex.get() != Identifier) throw new SyntaxError(lex); list = ASTList.append(list, new Symbol(lex.getString())); if (lex.lookAhead() == '.') lex.get(); else break; } return list; }
private Declarator parseFormalType(SymbolTable tbl) throws CompileError { int t = lex.lookAhead(); if (isBuiltinType(t) || t == VOID) { lex.get(); // primitive type int dim = parseArrayDimension(); return new Declarator(t, dim); } else { ASTList name = parseClassType(tbl); int dim = parseArrayDimension(); return new Declarator(name, dim); } }
private ASTree parseConditionalExpr(SymbolTable tbl) throws CompileError { ASTree cond = parseBinaryExpr(tbl); if (lex.lookAhead() == '?') { lex.get(); ASTree thenExpr = parseExpression(tbl); if (lex.get() != ':') throw new CompileError(": is missing", lex); ASTree elseExpr = parseExpression(tbl); return new CondExpr(cond, thenExpr, elseExpr); } else return cond; }
private Stmnt parseStmntOrCase(SymbolTable tbl) throws CompileError { int t = lex.lookAhead(); if (t != CASE && t != DEFAULT) return parseStatement(tbl); lex.get(); Stmnt s; if (t == CASE) s = new Stmnt(t, parseExpression(tbl)); else s = new Stmnt(DEFAULT); if (lex.get() != ':') throw new CompileError(": is missing", lex); return s; }
private ArrayInit parseArrayInitializer(SymbolTable tbl) throws CompileError { lex.get(); // '{' ASTree expr = parseExpression(tbl); ArrayInit init = new ArrayInit(expr); while (lex.lookAhead() == ',') { lex.get(); expr = parseExpression(tbl); ASTList.append(init, expr); } if (lex.get() != '}') throw new SyntaxError(lex); return init; }
private Stmnt parseIf(SymbolTable tbl) throws CompileError { int t = lex.get(); // IF ASTree expr = parseParExpression(tbl); Stmnt thenp = parseStatement(tbl); Stmnt elsep; if (lex.lookAhead() == ELSE) { lex.get(); elsep = parseStatement(tbl); } else elsep = null; return new Stmnt(t, expr, new ASTList(thenp, new ASTList(elsep))); }