1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
7 /** Parses a stream of lexed tokens into a tree of ByteCodeBlock's */
8 class Parser extends Lexer implements ByteCodes {
11 // Constructors //////////////////////////////////////////////////////
13 public Parser(Reader r, String sourceName, int line) throws IOException { super(r, sourceName, line); }
16 public static void main(String[] s) throws Exception {
17 Parser p = new Parser(new InputStreamReader(System.in), "stdin", 0);
19 ByteCodeBlock block = new ByteCodeBlock(0, "stdin");
20 p.parseStatement(false, block);
21 if (block == null) return;
22 System.out.println(block);
23 if (p.peekToken() == -1) return;
28 // Statics ////////////////////////////////////////////////////////////
30 static byte[] precedence = new byte[MAX_TOKEN + 1];
31 static boolean[] isRightAssociative = new boolean[MAX_TOKEN + 1];
33 isRightAssociative[ASSIGN] = true;
35 precedence[ASSIGN] = 1;
37 precedence[COMMA] = 3;
38 precedence[OR] = precedence[AND] = 4;
39 precedence[GT] = precedence[GE] = 5;
40 precedence[BITOR] = 6;
41 precedence[BITXOR] = 7;
42 precedence[BITAND] = 8;
43 precedence[EQ] = precedence[NE] = 9;
44 precedence[LT] = precedence[LE] = 10;
45 precedence[SHEQ] = precedence[SHNE] = 11;
46 precedence[LSH] = precedence[RSH] = precedence[URSH] = 12;
47 precedence[ADD] = precedence[SUB] = 13;
48 precedence[MUL] = precedence[DIV] = precedence[MOD] = 14;
49 precedence[BITNOT] = 15;
50 precedence[INC] = precedence[DEC] = 16;
57 // Parsing Logic /////////////////////////////////////////////////////////
59 private ByteCodeBlock newbb(int line) { return new ByteCodeBlock(line, sourceName); }
61 /** gets a token and throws an exception if it is not <tt>code</tt> */
62 public void consume(int code) throws IOException {
63 if (getToken() != code)
64 throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
67 /** append the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
68 public void startExpr(ByteCodeBlock block) throws IOException { startExpr(-1, block); }
70 public ByteCodeBlock startExpr(int minPrecedence) throws IOException {
71 ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
72 startExpr(minPrecedence, ret);
73 return ret.size() == 0 ? null : ret;
77 public void startExpr(int minPrecedence, ByteCodeBlock appendTo) throws IOException {
80 if (tok == -1) return;
82 ByteCodeBlock b = appendTo;
85 case NUMBER: continueExpr(b.add(ByteCodeBlock.LITERAL, number), minPrecedence); return;
86 case STRING: continueExpr(b.add(ByteCodeBlock.LITERAL, string), minPrecedence); return;
87 case THIS: continueExpr(b.add(TOPSCOPE, null), minPrecedence); return;
88 case NULL: continueExpr(b.add(ByteCodeBlock.LITERAL, null), minPrecedence); return;
89 case TRUE: case FALSE: continueExpr(b.add(ByteCodeBlock.LITERAL, new Boolean(tok == TRUE)), minPrecedence); return;
91 b.add(ARRAY, new Integer(0));
97 if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
98 b.add(LITERAL, new Integer(i++));
99 if (size == b.size()) b.add(LITERAL, null);
102 if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
108 b.add(ByteCodeBlock.LITERAL, new Double(number.doubleValue() * -1));
109 continueExpr(b, minPrecedence);
115 continueExpr(b, minPrecedence);
118 case INC: case DEC: {
120 startExpr(precedence[tok], b);
121 b.set(b.size() - 1, tok, new Boolean(true));
122 continueExpr(b, minPrecedence);
125 case BANG: case BITNOT: case TYPEOF: {
126 startExpr(precedence[tok], b);
128 continueExpr(b, minPrecedence);
133 if (peekToken() != RC) while(true) {
134 if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
136 b.add(LITERAL, string);
141 if (peekToken() == RC) break;
143 if (peekToken() == RC) break;
146 continueExpr(b, minPrecedence);
150 String name = string;
151 if (peekToken() == ASSIGN) {
154 b.add(ByteCodeBlock.LITERAL, name);
155 startExpr(minPrecedence, b);
156 b.add(ByteCodeBlock.PUT);
157 b.add(ByteCodeBlock.SWAP);
158 b.add(ByteCodeBlock.POP);
161 b.add(ByteCodeBlock.LITERAL, name);
162 b.add(ByteCodeBlock.GET);
164 continueExpr(b, minPrecedence);
170 ByteCodeBlock b2 = newbb(curLine);
173 b2.add(LITERAL, "arguments");
174 b2.add(LITERAL, "arguments");
180 if (peekToken() == RP) consume(RP);
182 if (peekToken() == COMMA) {
189 b2.add(LITERAL, string);
192 // retrieve it from the arguments array
193 b2.add(LITERAL, new Integer(numArgs));
194 b2.add(GET_PRESERVE);
198 // put it to the current scope
201 b2.add(LITERAL, string);
209 if (peekToken() == RP) { consume(RP); break; }
214 // pop off the arguments array
216 parseStatement(true, b2);
217 b2.add(LITERAL, null);
219 continueExpr(b.add(NEWFUNCTION, b2), minPrecedence);
222 default: pushBackToken(); return;
226 public void continueExpr(ByteCodeBlock prefix, int minPrecedence) throws IOException {
227 int tok = getToken();
229 if (tok == -1) return;
230 if (minPrecedence > 0 && precedence[tok] != 0)
231 if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
232 { pushBackToken(); return; }
234 if (prefix == null) throw new Error("got null prefix");
235 ByteCodeBlock b = prefix;
239 case ASSIGN_BITOR: case ASSIGN_BITXOR: case ASSIGN_BITAND: case ASSIGN_LSH: case ASSIGN_RSH: case ASSIGN_URSH:
240 case ASSIGN_ADD: case ASSIGN_SUB: case ASSIGN_MUL: case ASSIGN_DIV: case ASSIGN_MOD: {
241 prefix.set(prefix.size() - 1, b.GET_PRESERVE, new Boolean(true));
242 startExpr(precedence[tok - 1], b);
247 continueExpr(b, minPrecedence);
251 case INC: case DEC: {
253 b.set(b.size() - 1, tok, new Boolean(false));
254 continueExpr(b, minPrecedence);
261 while(peekToken() != RP) {
264 if (peekToken() == RP) break;
268 b.add(CALL, new Integer(i));
269 continueExpr(b, minPrecedence);
273 case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
274 case RSH: case URSH: case ADD: case MUL: case DIV: case MOD:
275 case GT: case GE: case EQ: case NE: case LT: case LE: case SUB: {
276 startExpr(precedence[tok], b);
278 continueExpr(b, minPrecedence);
283 b.add(tok == AND ? b.JF : b.JT, new Integer(0));
285 startExpr(precedence[tok], b);
286 b.set(size - 1, new Integer(b.size() - size + 2));
287 b.add(JMP, new Integer(2));
288 b.add(LITERAL, tok == AND ? new Boolean(false) : new Boolean(true));
289 continueExpr(b, minPrecedence);
295 String target = string;
296 if (peekToken() == ASSIGN) {
298 b.add(LITERAL, target);
304 b.add(LITERAL, target);
307 continueExpr(b, minPrecedence);
314 if (peekToken() == ASSIGN) {
323 continueExpr(b, minPrecedence);
328 b.add(JF, new Integer(0));
331 b.set(size - 1, new Integer(b.size() - size + 2));
332 b.add(JMP, new Integer(0));
336 b.set(size - 1, new Integer(b.size() - size + 1));
337 continueExpr(b, minPrecedence);
341 default: { pushBackToken(); return; }
345 /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
347 public ByteCodeBlock parseStatement() throws IOException {
348 ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
349 ret.add(ret.LITERAL, null);
350 parseStatement(false, ret);
351 if (ret.size() == 1) return null;
355 public void parseStatement(boolean requireBraces, ByteCodeBlock b) throws IOException {
356 int tok = peekToken();
357 if (tok == -1) return;
358 boolean braced = tok == LC;
359 if (requireBraces && !braced) throw new ParserException("expected {, got " + codeToString[tok]);
360 if (braced) consume(LC);
363 switch(tok = peekToken()) {
365 case THROW: case RETURN: case ASSERT: {
367 if (tok == RETURN && peekToken() == SEMI) b.add(LITERAL, null);
374 case BREAK: case CONTINUE: {
376 if (peekToken() == NAME) consume(NAME);
389 b.add(TOPSCOPE); // push the current scope
392 String name = string;
393 b.add(LITERAL, name); // push the name to be declared
394 b.add(DECLARE); // declare it
395 if (peekToken() == ASSIGN) { // if there is an '=' after the variable name
396 b.add(LITERAL, name); // put the var name back on the stack
402 if (peekToken() != COMMA) break;
406 if (peekToken() == SEMI) consume(SEMI);
416 b.add(JF, new Integer(0));
418 parseStatement(false, b);
420 if (peekToken() == ELSE) {
422 b.set(size - 1, new Integer(2 + b.size() - size));
423 b.add(JMP, new Integer(0));
425 parseStatement(false, b);
427 b.set(size - 1, new Integer(1 + b.size() - size));
438 b.add(JT, new Integer(2));
441 parseStatement(false, b);
442 b.add(CONTINUE); // if we fall out of the end, definately continue
443 b.set(size - 1, new Integer(b.size() - size + 1)); // end of the loop
451 int size0 = b.size();
456 if (peekToken() == CASE) {
462 b.add(JF, new Integer(0));
464 while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
465 int size2 = b.size();
466 parseStatement(false, b);
467 if (size2 == b.size()) break;
469 b.set(size - 1, new Integer(1 + b.size() - size));
470 } else if (peekToken() == DEFAULT) {
473 while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
474 int size2 = b.size();
475 parseStatement(false, b);
476 if (size2 == b.size()) break;
478 } else if (peekToken() == RC) {
483 throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
486 b.set(size0 - 1, new Integer(b.size() - size0 + 1)); // end of the loop
494 parseStatement(false, b);
498 b.add(JT, new Integer(2));
503 b.set(size - 1, new Integer(b.size() - size + 1)); // end of the loop
508 // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
510 parseStatement(true, b);
512 if (peekToken() == CATCH) {
517 parseStatement(); // just discard the catchblock
520 if (peekToken() == FINALLY) {
522 parseStatement(false, b);
532 boolean hadVar = false;
533 if (tok == VAR) { hadVar = true; tok = getToken(); }
534 String varName = string;
535 boolean forIn = peekToken() == IN;
536 pushBackToken(tok, varName);
539 // FIXME: break needs to work in here
544 b.add(LITERAL, "length");
547 ByteCodeBlock b2 = newbb(curLine);
551 b.add(LITERAL, new Integer(1));
554 b.add(LITERAL, new Integer(0));
556 b.add(JT, new Integer(7));
558 b.add(LITERAL, varName);
559 b.add(LITERAL, varName);
562 parseStatement(false, b);
569 if (hadVar) pushBackToken(VAR, null);
572 parseStatement(false, b);
573 ByteCodeBlock e2 = new ByteCodeBlock(line, sourceName);
576 if (e2 == null) e2 = newbb(curLine).add(b.LITERAL, Boolean.TRUE);
579 int size2 = b.size();
581 b.add(JT, new Integer(0));
584 if (b.size() > size) b.add(POP);
585 b.set(size - 1, new Integer(b.size() - size + 1));
589 b.add(JT, new Integer(2));
591 parseStatement(false, b);
593 b.set(size2 - 1, new Integer(b.size() - size2 + 1)); // end of the loop
602 String name = string;
603 if (peekToken() == COLON) {
605 b.add(ByteCodeBlock.LABEL, string);
608 pushBackToken(NAME, name);
609 // fall through to default case
614 if (tok == RC && braced) { consume(RC); return; }
619 if (size == b.size()) return;
621 if (peekToken() == SEMI) consume(SEMI);
630 class ParserException extends RuntimeException {
631 public ParserException(String s) { super(sourceName + ":" + line + " " + s); }