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 {
15 this.sourceName = sourceName;
20 public static void main(String[] s) throws Exception {
21 Parser p = new Parser(new InputStreamReader(System.in), "stdin", 0);
23 ByteCodeBlock block = new ByteCodeBlock(0, "stdin");
24 p.parseStatement(false, block);
25 if (block == null) return;
26 System.out.println(block);
27 if (p.peekToken() == -1) return;
32 // Statics ////////////////////////////////////////////////////////////
34 static byte[] precedence = new byte[MAX_TOKEN + 1];
35 static boolean[] isRightAssociative = new boolean[MAX_TOKEN + 1];
37 isRightAssociative[ASSIGN] = true;
39 precedence[ASSIGN] = 1;
41 precedence[COMMA] = 3;
42 precedence[OR] = precedence[AND] = 4;
43 precedence[GT] = precedence[GE] = 5;
44 precedence[BITOR] = 6;
45 precedence[BITXOR] = 7;
46 precedence[BITAND] = 8;
47 precedence[EQ] = precedence[NE] = 9;
48 precedence[LT] = precedence[LE] = 10;
49 precedence[SHEQ] = precedence[SHNE] = 11;
50 precedence[LSH] = precedence[RSH] = precedence[URSH] = 12;
51 precedence[ADD] = precedence[SUB] = 13;
52 precedence[MUL] = precedence[DIV] = precedence[MOD] = 14;
53 precedence[BITNOT] = 15;
54 precedence[INC] = precedence[DEC] = 16;
61 // Parsing Logic /////////////////////////////////////////////////////////
63 private ByteCodeBlock newbb(int line) { return new ByteCodeBlock(line, sourceName); }
65 /** gets a token and throws an exception if it is not <tt>code</tt> */
66 public void consume(int code) throws IOException {
67 if (getToken() != code)
68 throw new ParserException("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
71 /** append the largest expression beginning with prefix containing no operators of precedence below <tt>minPrecedence</tt> */
72 public ByteCodeBlock startExpr() throws IOException { return startExpr(-1); }
73 public void startExpr(ByteCodeBlock block) throws IOException { startExpr(-1, block); }
74 public ByteCodeBlock startExpr(int minPrecedence) throws IOException {
75 ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
76 startExpr(minPrecedence, ret);
77 return ret.size() == 0 ? null : ret;
80 public void startExpr(int minPrecedence, ByteCodeBlock appendTo) throws IOException {
83 if (tok == -1) return;
84 if (minPrecedence > 0 && precedence[tok] != 0)
85 if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
86 { pushBackToken(); return; }
88 ByteCodeBlock b = appendTo;
91 case NUMBER: continueExpr(b.add(ByteCodeBlock.LITERAL, number), minPrecedence); return;
92 case STRING: continueExpr(b.add(ByteCodeBlock.LITERAL, string), minPrecedence); return;
93 case THIS: continueExpr(b.add(TOPSCOPE, null), minPrecedence); return;
94 case NULL: continueExpr(b.add(ByteCodeBlock.LITERAL, null), minPrecedence); return;
95 case TRUE: case FALSE: continueExpr(b.add(ByteCodeBlock.LITERAL, new Boolean(tok == TRUE)), minPrecedence); return;
97 b.add(ARRAY, new Integer(0));
102 if (size == b.size())
103 if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
104 b.add(LITERAL, new Integer(i++));
105 if (size == b.size()) b.add(LITERAL, null);
108 if (peekToken() == RB) { consume(RB); continueExpr(b, minPrecedence); return; }
114 continueExpr(b.add(ByteCodeBlock.LITERAL, new Double(number.doubleValue() * -1)), minPrecedence);
120 continueExpr(b, minPrecedence);
123 case INC: case DEC: {
125 startExpr(precedence[tok], b);
126 b.set(b.size() - 1, tok, new Boolean(true));
127 continueExpr(b, minPrecedence);
130 case BANG: case BITNOT: case TYPEOF: {
131 startExpr(precedence[tok], b);
133 continueExpr(b, minPrecedence);
138 if (peekToken() != RC) while(true) {
139 if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
141 b.add(LITERAL, string);
146 if (peekToken() == RC) break;
148 if (peekToken() == RC) break;
151 continueExpr(b, minPrecedence);
155 String name = string;
156 if (peekToken() == ASSIGN) {
159 b.add(ByteCodeBlock.LITERAL, name);
160 startExpr(minPrecedence, b);
161 b.add(ByteCodeBlock.PUT);
162 b.add(ByteCodeBlock.SWAP);
163 b.add(ByteCodeBlock.POP);
166 b.add(ByteCodeBlock.LITERAL, name);
167 b.add(ByteCodeBlock.GET);
169 continueExpr(b, minPrecedence);
175 ByteCodeBlock b2 = newbb(curLine);
178 b2.add(LITERAL, "arguments");
179 b2.add(LITERAL, "arguments");
185 if (peekToken() == RP) consume(RP);
187 if (peekToken() == COMMA) {
194 b2.add(LITERAL, string);
197 // retrieve it from the arguments array
198 b2.add(LITERAL, new Integer(numArgs));
199 b2.add(GET_PRESERVE);
203 // put it to the current scope
206 b2.add(LITERAL, string);
214 if (peekToken() == RP) { consume(RP); break; }
219 // pop off the arguments array
221 parseStatement(true, b2);
222 b2.add(LITERAL, null);
224 continueExpr(b.add(NEWFUNCTION, b2), minPrecedence);
227 default: pushBackToken(); return;
231 public void continueExpr(ByteCodeBlock prefix, int minPrecedence) throws IOException {
232 int tok = getToken();
234 if (tok == -1) return;
235 if (minPrecedence > 0 && precedence[tok] != 0)
236 if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
237 { pushBackToken(); return; }
239 if (prefix == null) throw new Error("got null prefix");
240 ByteCodeBlock b = prefix;
244 case ASSIGN_BITOR: case ASSIGN_BITXOR: case ASSIGN_BITAND: case ASSIGN_LSH: case ASSIGN_RSH: case ASSIGN_URSH:
245 case ASSIGN_ADD: case ASSIGN_SUB: case ASSIGN_MUL: case ASSIGN_DIV: case ASSIGN_MOD: {
246 prefix.set(prefix.size() - 1, b.GET_PRESERVE, new Boolean(true));
247 startExpr(precedence[tok - 1], b);
252 continueExpr(b, minPrecedence);
256 case INC: case DEC: {
258 b.set(b.size() - 1, tok, new Boolean(false));
259 continueExpr(b, minPrecedence);
266 while(peekToken() != RP) {
269 if (peekToken() == RP) break;
273 b.add(CALL, new Integer(i));
274 continueExpr(b, minPrecedence);
278 case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
279 case RSH: case URSH: case ADD: case MUL: case DIV: case MOD:
280 case GT: case GE: case EQ: case NE: case LT: case LE: case SUB: {
281 startExpr(precedence[tok], b);
283 continueExpr(b, minPrecedence);
288 b.add(tok == AND ? b.JF : b.JT, new Integer(0));
290 startExpr(precedence[tok], b);
291 b.set(size - 1, new Integer(b.size() - size + 2));
292 b.add(JMP, new Integer(2));
293 b.add(LITERAL, tok == AND ? new Boolean(false) : new Boolean(true));
294 continueExpr(b, minPrecedence);
300 String target = string;
301 if (peekToken() == ASSIGN) {
303 b.add(LITERAL, target);
309 b.add(LITERAL, target);
312 continueExpr(b, minPrecedence);
319 if (peekToken() == ASSIGN) {
328 continueExpr(b, minPrecedence);
333 b.add(JF, new Integer(0));
336 b.set(size - 1, new Integer(b.size() - size + 2));
337 b.add(JMP, new Integer(0));
341 b.set(size - 1, new Integer(b.size() - size + 1));
342 continueExpr(b, minPrecedence);
346 default: { pushBackToken(); return; }
350 /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
351 public ByteCodeBlock parseStatement() throws IOException {
352 ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
353 ret.add(ret.LITERAL, null);
354 parseStatement(false, ret);
355 if (ret.size() == 1) return null;
359 public void parseStatement(boolean requireBraces) throws IOException {
360 parseStatement(requireBraces, new ByteCodeBlock(line, sourceName));
362 public void parseStatement(boolean requireBraces, ByteCodeBlock b) throws IOException {
363 int tok = peekToken();
364 if (tok == -1) return;
365 boolean braced = tok == LC;
366 if (requireBraces && !braced) throw new ParserException("expected {, got " + codeToString[tok]);
367 if (braced) consume(LC);
370 switch(tok = peekToken()) {
372 case THROW: case RETURN: case ASSERT: {
374 if (tok == RETURN && peekToken() == SEMI) b.add(LITERAL, null);
381 case BREAK: case CONTINUE: {
383 if (peekToken() == NAME) consume(NAME);
396 b.add(TOPSCOPE); // push the current scope
399 String name = string;
400 b.add(LITERAL, name); // push the name to be declared
401 b.add(DECLARE); // declare it
402 if (peekToken() == ASSIGN) { // if there is an '=' after the variable name
403 b.add(LITERAL, name); // put the var name back on the stack
409 if (peekToken() != COMMA) break;
413 if (peekToken() == SEMI) consume(SEMI);
423 b.add(JF, new Integer(0));
425 parseStatement(false, b);
427 if (peekToken() == ELSE) {
429 b.set(size - 1, new Integer(2 + b.size() - size));
430 b.add(JMP, new Integer(0));
432 parseStatement(false, b);
434 b.set(size - 1, new Integer(1 + b.size() - size));
441 ByteCodeBlock loop = newbb(curLine);
446 loop.add(JT, new Integer(2));
449 parseStatement(false, loop);
451 // if we fall out of the end, definately continue
459 ByteCodeBlock loop = newbb(curLine);
465 if (peekToken() == CASE) {
471 loop.add(JF, new Integer(0));
472 int size = loop.size();
473 while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
474 int size2 = loop.size();
475 parseStatement(false, loop);
476 if (size2 == loop.size()) break;
478 loop.set(size - 1, new Integer(1 + loop.size() - size));
479 } else if (peekToken() == DEFAULT) {
482 while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
483 int size2 = loop.size();
484 parseStatement(false, loop);
485 if (size2 == loop.size()) break;
487 } else if (peekToken() == RC) {
492 throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
499 ByteCodeBlock loop = newbb(curLine);
502 parseStatement(false, loop);
506 loop.add(JT, new Integer(2));
515 // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
517 parseStatement(true, b);
519 if (peekToken() == CATCH) {
524 parseStatement(); // just discard the catchblock
527 if (peekToken() == FINALLY) {
529 parseStatement(false, b);
539 if (tok == VAR) tok = getToken();
540 String varName = string;
541 boolean forIn = peekToken() == IN;
542 pushBackToken(tok, varName);
549 b.add(LITERAL, "length");
552 ByteCodeBlock b2 = newbb(curLine);
554 b2.add(LITERAL, new Integer(1));
557 b2.add(LITERAL, new Integer(0));
559 b2.add(JT, new Integer(7));
560 b2.add(GET_PRESERVE);
561 b2.add(LITERAL, varName);
562 b2.add(LITERAL, varName);
565 parseStatement(false, b2);
569 ByteCodeBlock b2 = newbb(curLine);
573 int size = b2.size();
575 if (b2.size() - size > 0) b2.add(POP);
577 ByteCodeBlock e2 = startExpr();
579 if (e2 == null) e2 = newbb(curLine).add(b.LITERAL, null);
581 ByteCodeBlock b3 = newbb(curLine);
583 b2.add(LITERAL, null);
585 b3.add(JT, new Integer(0));
589 if (b3.size() - size > 0) b3.add(POP);
590 b3.set(size - 1, new Integer(b3.size() - size + 1));
593 b3.add(JT, new Integer(2));
595 parseStatement(false, b3);
603 String name = string;
604 if (peekToken() == COLON) {
606 b.add(ByteCodeBlock.LABEL, string);
609 pushBackToken(NAME, name);
610 // fall through to default case
615 if (tok == RC && braced) { consume(RC); return; }
620 if (size == b.size()) return;
622 if (peekToken() == SEMI) consume(SEMI);
631 class ParserException extends RuntimeException {
632 public ParserException(String s) { super(sourceName + ":" + line + " " + s); }