// Constructors //////////////////////////////////////////////////////
- public Parser(Reader r, String sourceName, int line) throws IOException {
- super(r);
- this.sourceName = sourceName;
- this.line = line;
- }
+ public Parser(Reader r, String sourceName, int line) throws IOException { super(r, sourceName, line); }
/** for debugging */
public static void main(String[] s) throws Exception {
/** gets a token and throws an exception if it is not <tt>code</tt> */
public void consume(int code) throws IOException {
if (getToken() != code)
- throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOL" : codeToString[op]));
+ throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
}
/** append the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
int tok = getToken();
int curLine = line;
if (tok == -1) return;
- if (minPrecedence > 0 && precedence[tok] != 0)
- if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
- { pushBackToken(); return; }
ByteCodeBlock b = appendTo;
}
case SUB: {
consume(NUMBER);
- continueExpr(b.add(ByteCodeBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence);
+ b.add(ByteCodeBlock.LITERAL, new Double(number.doubleValue() * -1));
+ continueExpr(b, minPrecedence);
return;
}
case LP: {
}
case LC: {
b.add(OBJECT, null);
- if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
- while(true) {
+ if (peekToken() != RC) while(true) {
if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
getToken();
b.add(LITERAL, string);
startExpr(b);
b.add(PUT);
b.add(POP);
- if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
+ if (peekToken() == RC) break;
consume(COMMA);
- if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
+ if (peekToken() == RC) break;
}
+ consume(RC);
+ continueExpr(b, minPrecedence);
+ return;
}
case NAME: {
String name = string;
// invocation
int i = 0;
while(peekToken() != RP) {
- startExpr(b);
i++;
+ startExpr(b);
if (peekToken() == RP) break;
consume(COMMA);
}
consume(LP);
tok = getToken();
- if (tok == VAR) tok = getToken();
+ boolean hadVar = false;
+ if (tok == VAR) { hadVar = true; tok = getToken(); }
String varName = string;
boolean forIn = peekToken() == IN;
pushBackToken(tok, varName);
break;
} else {
+ if (hadVar) pushBackToken(VAR, null);
ByteCodeBlock b2 = newbb(curLine);
b.add(SCOPE, b2);
b.add(POP);
int size = b2.size();
- startExpr(b2);
- if (b2.size() - size > 0) b2.add(POP);
- consume(SEMI);
+ parseStatement(false, b2);
ByteCodeBlock e2 = startExpr();
consume(SEMI);
if (e2 == null) e2 = newbb(curLine).add(b.LITERAL, null);
b3.add(JT, new Integer(2));
b3.add(BREAK);
parseStatement(false, b3);
- b3.add(BREAK);
+ b3.add(CONTINUE);
break;
}
}