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 ? "EOL" : 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) { consume(RC); continueExpr(b, minPrecedence); return; }
140 if (peekToken() != NAME && peekToken() != STRING) throw new Error("expected NAME or STRING");
142 b.add(LITERAL, string);
147 if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
149 if (peekToken() == RC) { consume(RC); continueExpr(b, minPrecedence); return; }
153 String name = string;
154 if (peekToken() == ASSIGN) {
157 b.add(ByteCodeBlock.LITERAL, name);
158 startExpr(minPrecedence, b);
159 b.add(ByteCodeBlock.PUT);
160 b.add(ByteCodeBlock.SWAP);
161 b.add(ByteCodeBlock.POP);
164 b.add(ByteCodeBlock.LITERAL, name);
165 b.add(ByteCodeBlock.GET);
167 continueExpr(b, minPrecedence);
173 ByteCodeBlock b2 = newbb(curLine);
176 b2.add(LITERAL, "arguments");
177 b2.add(LITERAL, "arguments");
183 if (peekToken() == RP) consume(RP);
185 if (peekToken() == COMMA) {
192 b2.add(LITERAL, string);
195 // retrieve it from the arguments array
196 b2.add(LITERAL, new Integer(numArgs));
197 b2.add(GET_PRESERVE);
201 // put it to the current scope
204 b2.add(LITERAL, string);
212 if (peekToken() == RP) { consume(RP); break; }
217 // pop off the arguments array
219 parseStatement(true, b2);
220 b2.add(LITERAL, null);
222 continueExpr(b.add(NEWFUNCTION, b2), minPrecedence);
225 default: pushBackToken(); return;
229 public void continueExpr(ByteCodeBlock prefix, int minPrecedence) throws IOException {
230 int tok = getToken();
232 if (tok == -1) return;
233 if (minPrecedence > 0 && precedence[tok] != 0)
234 if (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))
235 { pushBackToken(); return; }
237 if (prefix == null) throw new Error("got null prefix");
238 ByteCodeBlock b = prefix;
242 case ASSIGN_BITOR: case ASSIGN_BITXOR: case ASSIGN_BITAND: case ASSIGN_LSH: case ASSIGN_RSH: case ASSIGN_URSH:
243 case ASSIGN_ADD: case ASSIGN_SUB: case ASSIGN_MUL: case ASSIGN_DIV: case ASSIGN_MOD: {
244 prefix.set(prefix.size() - 1, b.GET_PRESERVE, new Boolean(true));
245 startExpr(precedence[tok - 1], b);
250 continueExpr(b, minPrecedence);
254 case INC: case DEC: {
256 b.set(b.size() - 1, tok, new Boolean(false));
257 continueExpr(b, minPrecedence);
264 while(peekToken() != RP) {
267 if (peekToken() == RP) break;
271 b.add(CALL, new Integer(i));
272 continueExpr(b, minPrecedence);
276 case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
277 case RSH: case URSH: case ADD: case MUL: case DIV: case MOD:
278 case GT: case GE: case EQ: case NE: case LT: case LE: case SUB: {
279 startExpr(precedence[tok], b);
281 continueExpr(b, minPrecedence);
286 b.add(tok == AND ? b.JF : b.JT, new Integer(0));
288 startExpr(precedence[tok], b);
289 b.set(size - 1, new Integer(b.size() - size + 2));
290 b.add(JMP, new Integer(2));
291 b.add(LITERAL, tok == AND ? new Boolean(false) : new Boolean(true));
292 continueExpr(b, minPrecedence);
298 String target = string;
299 if (peekToken() == ASSIGN) {
301 b.add(LITERAL, target);
307 b.add(LITERAL, target);
310 continueExpr(b, minPrecedence);
317 if (peekToken() == ASSIGN) {
326 continueExpr(b, minPrecedence);
331 b.add(JF, new Integer(0));
334 b.set(size - 1, new Integer(b.size() - size + 2));
335 b.add(JMP, new Integer(0));
339 b.set(size - 1, new Integer(b.size() - size + 1));
340 continueExpr(b, minPrecedence);
344 default: { pushBackToken(); return; }
348 /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
349 public ByteCodeBlock parseStatement() throws IOException {
350 ByteCodeBlock ret = new ByteCodeBlock(line, sourceName);
351 ret.add(ret.LITERAL, null);
352 parseStatement(false, ret);
353 if (ret.size() == 1) return null;
357 public void parseStatement(boolean requireBraces) throws IOException {
358 parseStatement(requireBraces, new ByteCodeBlock(line, sourceName));
360 public void parseStatement(boolean requireBraces, ByteCodeBlock b) throws IOException {
361 int tok = peekToken();
362 if (tok == -1) return;
363 boolean braced = tok == LC;
364 if (requireBraces && !braced) throw new ParserException("expected {, got " + codeToString[tok]);
365 if (braced) consume(LC);
368 switch(tok = peekToken()) {
370 case THROW: case RETURN: case ASSERT: {
372 if (tok == RETURN && peekToken() == SEMI) b.add(LITERAL, null);
379 case BREAK: case CONTINUE: {
381 if (peekToken() == NAME) consume(NAME);
394 b.add(TOPSCOPE); // push the current scope
397 String name = string;
398 b.add(LITERAL, name); // push the name to be declared
399 b.add(DECLARE); // declare it
400 if (peekToken() == ASSIGN) { // if there is an '=' after the variable name
401 b.add(LITERAL, name); // put the var name back on the stack
407 if (peekToken() != COMMA) break;
411 if (peekToken() == SEMI) consume(SEMI);
421 b.add(JF, new Integer(0));
423 parseStatement(false, b);
425 if (peekToken() == ELSE) {
427 b.set(size - 1, new Integer(2 + b.size() - size));
428 b.add(JMP, new Integer(0));
430 parseStatement(false, b);
432 b.set(size - 1, new Integer(1 + b.size() - size));
439 ByteCodeBlock loop = newbb(curLine);
444 loop.add(JT, new Integer(2));
447 parseStatement(false, loop);
449 // if we fall out of the end, definately continue
457 ByteCodeBlock loop = newbb(curLine);
463 if (peekToken() == CASE) {
469 loop.add(JF, new Integer(0));
470 int size = loop.size();
471 while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
472 int size2 = loop.size();
473 parseStatement(false, loop);
474 if (size2 == loop.size()) break;
476 loop.set(size - 1, new Integer(1 + loop.size() - size));
477 } else if (peekToken() == DEFAULT) {
480 while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) {
481 int size2 = loop.size();
482 parseStatement(false, loop);
483 if (size2 == loop.size()) break;
485 } else if (peekToken() == RC) {
490 throw new ParserException("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
497 ByteCodeBlock loop = newbb(curLine);
500 parseStatement(false, loop);
504 loop.add(JT, new Integer(2));
513 // We deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
515 parseStatement(true, b);
517 if (peekToken() == CATCH) {
522 parseStatement(); // just discard the catchblock
525 if (peekToken() == FINALLY) {
527 parseStatement(false, b);
537 if (tok == VAR) tok = getToken();
538 String varName = string;
539 boolean forIn = peekToken() == IN;
540 pushBackToken(tok, varName);
547 b.add(LITERAL, "length");
550 ByteCodeBlock b2 = newbb(curLine);
552 b2.add(LITERAL, new Integer(1));
555 b2.add(LITERAL, new Integer(0));
557 b2.add(JT, new Integer(7));
558 b2.add(GET_PRESERVE);
559 b2.add(LITERAL, varName);
560 b2.add(LITERAL, varName);
563 parseStatement(false, b2);
567 ByteCodeBlock b2 = newbb(curLine);
571 int size = b2.size();
573 if (b2.size() - size > 0) b2.add(POP);
575 ByteCodeBlock e2 = startExpr();
577 if (e2 == null) e2 = newbb(curLine).add(b.LITERAL, null);
579 ByteCodeBlock b3 = newbb(curLine);
581 b2.add(LITERAL, null);
583 b3.add(JT, new Integer(0));
587 if (b3.size() - size > 0) b3.add(POP);
588 b3.set(size - 1, new Integer(b3.size() - size + 1));
591 b3.add(JT, new Integer(2));
593 parseStatement(false, b3);
601 String name = string;
602 if (peekToken() == COLON) {
604 b.add(ByteCodeBlock.LABEL, string);
607 pushBackToken(NAME, name);
608 // fall through to default case
613 if (tok == RC && braced) { consume(RC); return; }
618 if (size == b.size()) return;
620 if (peekToken() == SEMI) consume(SEMI);
629 class ParserException extends RuntimeException {
630 public ParserException(String s) { super(sourceName + ":" + line + " " + s); }