2003/11/03 06:32:56
[org.ibex.core.git] / src / org / xwt / js / Parser.java
1 // Copyright 2003 Adam Megacz, see the COPYING file for licensing [GPL]
2 package org.xwt.js;
3
4 import org.xwt.util.*;
5 import java.io.*;
6
7 /**
8  *  Parses a stream of lexed tokens into a tree of Function's.
9  *
10  *  There are three kinds of things we parse: blocks, statements, and
11  *  expressions.
12  *
13  *  - Expressions are a special type of statement that evaluates to a
14  *    value (for example, "break" is not an expression, * but "3+2"
15  *    is).  Some tokens sequences start expressions (for * example,
16  *    literal numbers) and others continue an expression which * has
17  *    already been begun (for example, '+').  Finally, some *
18  *    expressions are valid targets for an assignment operation; after
19  *    * each of these expressions, continueExprAfterAssignable() is
20  *    called * to check for an assignment operation.
21  *
22  *  - A statement ends with a semicolon and does not return a value.
23  *
24  *  - A block is a single statement or a sequence of statements
25  *    surrounded by curly braces.
26  *
27  *  Each parsing method saves the parserLine before doing its actual
28  *  work and restores it afterwards.  This ensures that parsing a
29  *  subexpression does not modify the line number until a token
30  *  *after* the subexpression has been consumed by the parent
31  *  expression.
32  *
33  *  Technically it would be a better design for this class to build an
34  *  intermediate parse tree and use that to emit bytecode.  Here's the
35  *  tradeoff:
36  *
37  *  Advantages of building a parse tree:
38  *  - easier to apply optimizations
39  *  - would let us handle more sophisticated languages than JavaScript
40  *
41  *  Advantages of leaving out the parse tree
42  *  - faster compilation
43  *  - less load on the garbage collector
44  *  - much simpler code, easier to understand
45  *  - less error-prone
46  *
47  *  Fortunately JS is such a simple language that we can get away with
48  *  the half-assed approach and still produce a working, complete
49  *  compiler.
50  *
51  *  The bytecode language emitted doesn't really cause any appreciable
52  *  semantic loss, and is itself a parseable language very similar to
53  *  Forth or a postfix variant of LISP.  This means that the bytecode
54  *  can be transformed into a parse tree, which can be manipulated.
55  *  So if we ever want to add an optimizer, it could easily be done by
56  *  producing a parse tree from the bytecode, optimizing that tree,
57  *  and then re-emitting the bytecode.  The parse tree node class
58  *  would also be much simpler since the bytecode language has so few
59  *  operators.
60  *
61  *  Actually, the above paragraph is slightly inaccurate -- there are
62  *  places where we push a value and then perform an arbitrary number
63  *  of operations using it before popping it; this doesn't parse well.
64  *  But these cases are clearly marked and easy to change if we do
65  *  need to move to a parse tree format.
66  */
67 class Parser extends Lexer implements ByteCodes {
68
69
70     // Constructors //////////////////////////////////////////////////////
71
72     public Parser(Reader r, String sourceName, int line) throws IOException { super(r, sourceName, line); }
73
74     /** for debugging */
75     public static void main(String[] s) throws Exception {
76         Function block = new Function("stdin", 0, new InputStreamReader(System.in), null);
77         if (block == null) return;
78         System.out.println(block);
79     }
80
81
82     // Statics ////////////////////////////////////////////////////////////
83
84     static byte[] precedence = new byte[MAX_TOKEN + 1];
85     static boolean[] isRightAssociative = new boolean[MAX_TOKEN + 1];
86     // Use this as the precedence when we want anything up to the comma
87     private final static int NO_COMMA = 2;
88     static {
89         isRightAssociative[ASSIGN] =
90             isRightAssociative[ASSIGN_BITOR] =
91             isRightAssociative[ASSIGN_BITXOR] =
92             isRightAssociative[ASSIGN_BITAND] =
93             isRightAssociative[ASSIGN_LSH] =
94             isRightAssociative[ASSIGN_RSH] =
95             isRightAssociative[ASSIGN_URSH] =
96             isRightAssociative[ASSIGN_ADD] =
97             isRightAssociative[ASSIGN_SUB] =
98             isRightAssociative[ASSIGN_MUL] =
99             isRightAssociative[ASSIGN_DIV] =
100             isRightAssociative[ASSIGN_MOD] = true;
101
102         precedence[COMMA] = 1;
103         // 2 is intentionally left unassigned. we use minPrecedence==2 for comma separated lists
104         precedence[ASSIGN] =
105             precedence[ASSIGN_BITOR] =
106             precedence[ASSIGN_BITXOR] =
107             precedence[ASSIGN_BITAND] =
108             precedence[ASSIGN_LSH] =
109             precedence[ASSIGN_RSH] =
110             precedence[ASSIGN_URSH] =
111             precedence[ASSIGN_ADD] =
112             precedence[ASSIGN_SUB] =
113             precedence[ASSIGN_MUL] =
114             precedence[ASSIGN_DIV] =
115             precedence[ASSIGN_MOD] = 3;
116         precedence[HOOK] = 4;
117         precedence[OR] = 5;
118         precedence[AND] = 6;
119         precedence[BITOR] = 7;
120         precedence[BITXOR] = 8;
121         precedence[BITAND] = 9;
122         precedence[EQ] = precedence[NE] = precedence[SHEQ] = precedence[SHNE] = 10;
123         precedence[LT] = precedence[LE] = precedence[GT] = precedence[GE] = 11;
124         precedence[LSH] = precedence[RSH] = precedence[URSH] = 12;
125         precedence[ADD] = precedence[SUB] = 12;
126         precedence[MUL] = precedence[DIV] = precedence[MOD] = 13;
127         precedence[BITNOT] =  precedence[BANG] = precedence[TYPEOF] = 14;
128         precedence[DOT] = precedence[LB] = precedence[LP] =  precedence[INC] = precedence[DEC] = 15;
129     }
130
131
132     // Parsing Logic /////////////////////////////////////////////////////////
133
134     /** gets a token and throws an exception if it is not <tt>code</tt> */
135     private void consume(int code) throws IOException {
136         if (getToken() != code) throw pe("expected " + codeToString[code] + ", got " + (op == -1 ? "EOF" : codeToString[op]));
137     }
138
139     /**
140      *  Parse the largest possible expression containing no operators
141      *  of precedence below <tt>minPrecedence</tt> and append the
142      *  bytecodes for that expression to <tt>appendTo</tt>; the
143      *  appended bytecodes MUST grow the stack by exactly one element.
144      */ 
145     private void startExpr(Function appendTo, int minPrecedence) throws IOException {
146         int saveParserLine = parserLine;
147         _startExpr(appendTo, minPrecedence);
148         parserLine = saveParserLine;
149     }
150     private void _startExpr(Function appendTo, int minPrecedence) throws IOException {
151         int tok = getToken();
152         Function b = appendTo;
153
154         switch (tok) {
155         case -1: throw pe("expected expression");
156
157         // all of these simply push values onto the stack
158         case NUMBER: b.add(parserLine, LITERAL, number); break;
159         case STRING: b.add(parserLine, LITERAL, string); break;
160         case THIS: b.add(parserLine, TOPSCOPE, null); break;
161         case NULL: b.add(parserLine, LITERAL, null); break;
162         case TRUE: case FALSE: b.add(parserLine, LITERAL, new Boolean(tok == TRUE)); break;
163
164         case LB: {
165             b.add(parserLine, ARRAY, new Integer(0));                       // push an array onto the stack
166             int size0 = b.size;
167             int i = 0;
168             if (peekToken() != RB)
169                 while(true) {                                               // iterate over the initialization values
170                     int size = b.size;
171                     b.add(parserLine, LITERAL, new Integer(i++));           // push the index in the array to place it into
172                     if (peekToken() == COMMA || peekToken() == RB)
173                         b.add(parserLine, LITERAL, null);                   // for stuff like [1,,2,]
174                     else
175                         startExpr(b, NO_COMMA);                             // push the value onto the stack
176                     b.add(parserLine, PUT);                                 // put it into the array
177                     b.add(parserLine, POP);                                 // discard the value remaining on the stack
178                     if (peekToken() == RB) break;
179                     consume(COMMA);
180                 }
181             b.set(size0 - 1, new Integer(i));                               // back at the ARRAY instruction, write the size of the array
182             consume(RB);
183             break;
184         }
185         case SUB: {  // negative literal (like "3 * -1")
186             consume(NUMBER);
187             b.add(parserLine, LITERAL, new Double(number.doubleValue() * -1));
188             break;
189         }
190         case LP: {  // grouping (not calling)
191             startExpr(b, -1);
192             consume(RP);
193             break;
194         }
195         case INC: case DEC: {  // prefix (not postfix)
196             startExpr(b, precedence[tok]);
197             int prev = b.size - 1;
198             if (b.get(prev) == GET && b.getArg(prev) != null)
199                 b.set(prev, LITERAL, b.getArg(prev));
200             else if(b.get(prev) == GET)
201                 b.pop();
202             else
203                 throw pe("prefixed increment/decrement can only be performed on a valid assignment target");
204             b.add(parserLine, tok, Boolean.TRUE);
205             break;
206         }
207         case BANG: case BITNOT: case TYPEOF: {
208             startExpr(b, precedence[tok]);
209             b.add(parserLine, tok);
210             break;
211         }
212         case LC: { // object constructor
213             b.add(parserLine, OBJECT, null);                                     // put an object on the stack
214             if (peekToken() != RC)
215                 while(true) {
216                     if (peekToken() != NAME && peekToken() != STRING)
217                         throw pe("expected NAME or STRING");
218                     getToken();
219                     b.add(parserLine, LITERAL, string);                          // grab the key
220                     consume(COLON);
221                     startExpr(b, NO_COMMA);                                      // grab the value
222                     b.add(parserLine, PUT);                                      // put the value into the object
223                     b.add(parserLine, POP);                                      // discard the remaining value
224                     if (peekToken() == RC) break;
225                     consume(COMMA);
226                     if (peekToken() == RC) break;                                // we permit {,,} -- I'm not sure if ECMA does
227                 }
228             consume(RC);
229             break;
230         }
231         case NAME: {
232             b.add(parserLine, TOPSCOPE);
233             b.add(parserLine, LITERAL, string);
234             continueExprAfterAssignable(b,minPrecedence);
235             break;
236         }
237         case FUNCTION: {
238             consume(LP);
239             int numArgs = 0;
240             Function b2 = new Function(sourceName, parserLine, null, null);
241             b.add(parserLine, NEWFUNCTION, b2);
242
243             // function prelude; arguments array is already on the stack
244             b2.add(parserLine, TOPSCOPE);
245             b2.add(parserLine, SWAP);
246             b2.add(parserLine, DECLARE, "arguments");                     // declare arguments (equivalent to 'var arguments;')
247             b2.add(parserLine, SWAP);                                     // set this.arguments and leave the value on the stack
248             b2.add(parserLine, PUT);
249
250             while(peekToken() != RP) {                                    // run through the list of argument names
251                 numArgs++;
252                 if (peekToken() == NAME) {
253                     consume(NAME);                                        // a named argument
254                     String varName = string;
255                     
256                     b2.add(parserLine, DUP);                              // dup the args array 
257                     b2.add(parserLine, GET, new Integer(numArgs - 1));   // retrieve it from the arguments array
258                     b2.add(parserLine, TOPSCOPE);
259                     b2.add(parserLine, SWAP);
260                     b2.add(parserLine, DECLARE, varName);                  // declare the name
261                     b2.add(parserLine, SWAP);
262                     b2.add(parserLine, PUT); 
263                     b2.add(parserLine, POP);                               // pop the value
264                     b2.add(parserLine, POP);                               // pop the scope                  
265                 }
266                 if (peekToken() == RP) break;
267                 consume(COMMA);
268             }
269             consume(RP);
270
271             b2.numFormalArgs = numArgs;
272             b2.add(parserLine, POP);                                      // pop off the arguments array
273             b2.add(parserLine, POP);                                      // pop off TOPSCOPE
274             
275            if(peekToken() != LC)
276                 throw pe("Functions must have a block surrounded by curly brackets");
277                 
278             parseBlock(b2, null);                                   // the function body
279
280             b2.add(parserLine, LITERAL, null);                        // in case we "fall out the bottom", return NULL
281             b2.add(parserLine, RETURN);
282
283             break;
284         }
285         default: throw pe("expected expression, found " + codeToString[tok] + ", which cannot start an expression");
286         }
287
288         // attempt to continue the expression
289         continueExpr(b, minPrecedence);
290     }
291
292
293     /**
294      *  Assuming that a complete assignable (lvalue) has just been
295      *  parsed and the object and key are on the stack,
296      *  <tt>continueExprAfterAssignable</tt> will attempt to parse an
297      *  expression that modifies the assignable.  This method always
298      *  decreases the stack depth by exactly one element.
299      */
300     private void continueExprAfterAssignable(Function b,int minPrecedence) throws IOException {
301         int saveParserLine = parserLine;
302         _continueExprAfterAssignable(b,minPrecedence);
303         parserLine = saveParserLine;
304     }
305     private void _continueExprAfterAssignable(Function b,int minPrecedence) throws IOException {
306         if (b == null) throw new Error("got null b; this should never happen");
307         int tok = getToken();
308         if (minPrecedence != -1 && (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok])))
309             // force the default case
310             tok = -1;
311         switch(tok) {
312         case ASSIGN_BITOR: case ASSIGN_BITXOR: case ASSIGN_BITAND: case ASSIGN_LSH: case ASSIGN_RSH: case ASSIGN_URSH:
313         case ASSIGN_MUL: case ASSIGN_DIV: case ASSIGN_MOD: case ASSIGN_ADD: case ASSIGN_SUB: {
314             b.add(parserLine, GET_PRESERVE);
315             startExpr(b,  precedence[tok]);
316             int size = b.size;
317             if (tok == ASSIGN_ADD || tok == ASSIGN_SUB) {
318                 b.add(parserLine, tok);
319             }
320             // tok-1 is always s/^ASSIGN_// (0 is BITOR, 1 is ASSIGN_BITOR, etc) 
321             b.add(parserLine, tok - 1, tok-1==ADD ? new Integer(2) : null);
322             b.add(parserLine, PUT);
323             b.add(parserLine, SWAP);
324             b.add(parserLine, POP);
325             if (tok == ASSIGN_ADD || tok == ASSIGN_SUB) b.set(size, tok, new Integer(b.size - size));
326             break;
327         }
328         case INC: case DEC: { // postfix
329             b.add(parserLine, tok, Boolean.FALSE);
330             break;
331         }
332         case ASSIGN: {
333             startExpr(b, precedence[tok]);
334             b.add(parserLine, PUT);
335             b.add(parserLine, SWAP);
336             b.add(parserLine, POP);
337             break;
338         }
339         case LP: {
340             int n = parseArgs(b);
341
342             // if the object supports GETCALL, we use this, and jump over the following two instructions
343             b.add(parserLine,CALLMETHOD,new Integer(n));
344             b.add(parserLine,GET);
345             b.add(parserLine,CALL_REVERSED,new Integer(n));
346             break;
347         }
348         default: {
349             pushBackToken();
350             if(b.get(b.size-1) == LITERAL && b.getArg(b.size-1) != null)
351                 b.set(b.size-1,GET,b.getArg(b.size-1));
352             else
353                 b.add(parserLine, GET);
354             return;
355         }
356         }
357     }
358
359
360     /**
361      *  Assuming that a complete expression has just been parsed,
362      *  <tt>continueExpr</tt> will attempt to extend this expression by
363      *  parsing additional tokens and appending additional bytecodes.
364      *
365      *  No operators with precedence less than <tt>minPrecedence</tt>
366      *  will be parsed.
367      *
368      *  If any bytecodes are appended, they will not alter the stack
369      *  depth.
370      */
371     private void continueExpr(Function b, int minPrecedence) throws IOException {
372         int saveParserLine = parserLine;
373         _continueExpr(b, minPrecedence);
374         parserLine = saveParserLine;
375     }
376     private void _continueExpr(Function b, int minPrecedence) throws IOException {
377         if (b == null) throw new Error("got null b; this should never happen");
378         int tok = getToken();
379         if (tok == -1) return;
380         if (minPrecedence != -1 && (precedence[tok] < minPrecedence || (precedence[tok] == minPrecedence && !isRightAssociative[tok]))) {
381             pushBackToken();
382             return;
383         }
384
385         switch (tok) {
386         case LP: {  // invocation (not grouping)
387             int n = parseArgs(b);
388             b.add(parserLine, CALL, new Integer(n));
389             break;
390         }
391         case BITOR: case BITXOR: case BITAND: case SHEQ: case SHNE: case LSH:
392         case RSH: case URSH: case MUL: case DIV: case MOD:
393         case GT: case GE: case EQ: case NE: case LT: case LE: case SUB: {
394             startExpr(b, precedence[tok]);
395             b.add(parserLine, tok);
396             break;
397         }
398         case ADD: {
399             int count=1;
400             int nextTok;
401             do {
402                 startExpr(b,precedence[tok]);
403                 count++;
404                 nextTok = getToken();
405             } while(nextTok == tok);
406             pushBackToken();
407             b.add(parserLine, tok, new Integer(count));
408             break;
409         }
410         case OR: case AND: {
411             b.add(parserLine, tok == AND ? b.JF : b.JT, new Integer(0));       // test to see if we can short-circuit
412             int size = b.size;
413             startExpr(b, precedence[tok]);                                     // otherwise check the second value
414             b.add(parserLine, JMP, new Integer(2));                            // leave the second value on the stack and jump to the end
415             b.add(parserLine, LITERAL, tok == AND ?
416                   new Boolean(false) : new Boolean(true));                     // target of the short-circuit jump is here
417             b.set(size - 1, new Integer(b.size - size));                     // write the target of the short-circuit jump
418             break;
419         }
420         case DOT: {
421             // support foo..bar syntax for foo[""].bar
422             if (peekToken() == DOT) {
423                 string = "";
424             } else {
425                 consume(NAME);
426             }
427             b.add(parserLine, LITERAL, string);
428             continueExprAfterAssignable(b,minPrecedence);
429             break;
430         }
431         case LB: { // subscripting (not array constructor)
432             startExpr(b, -1);
433             consume(RB);
434             continueExprAfterAssignable(b,minPrecedence);
435             break;
436         }
437         case HOOK: {
438             b.add(parserLine, JF, new Integer(0));                // jump to the if-false expression
439             int size = b.size;
440             startExpr(b, minPrecedence);                          // write the if-true expression
441             b.add(parserLine, JMP, new Integer(0));               // if true, jump *over* the if-false expression     
442             b.set(size - 1, new Integer(b.size - size + 1));    // now we know where the target of the jump is
443             consume(COLON);
444             size = b.size;
445             startExpr(b, minPrecedence);                          // write the if-false expression
446             b.set(size - 1, new Integer(b.size - size + 1));    // this is the end; jump to here
447             break;
448         }
449         case COMMA: {
450             // pop the result of the previous expression, it is ignored
451             b.add(parserLine,POP);
452             startExpr(b,-1);
453             break;
454         }
455         default: {
456             pushBackToken();
457             return;
458         }
459         }
460
461         continueExpr(b, minPrecedence);                           // try to continue the expression
462     }
463     
464     // parse a set of comma separated function arguments, assume LP has already been consumed
465     private int parseArgs(Function b) throws IOException {
466         int i = 0;
467         while(peekToken() != RP) {
468             i++;
469             if (peekToken() != COMMA) {
470                 startExpr(b, NO_COMMA);
471                 if (peekToken() == RP) break;
472             }
473             consume(COMMA);
474         }
475         consume(RP);
476         return i;
477     }
478     
479     /** Parse a block of statements which must be surrounded by LC..RC. */
480     void parseBlock(Function b) throws IOException { parseBlock(b, null); }
481     void parseBlock(Function b, String label) throws IOException {
482         int saveParserLine = parserLine;
483         _parseBlock(b, label);
484         parserLine = saveParserLine;
485     }
486     void _parseBlock(Function b, String label) throws IOException {
487         if (peekToken() == -1) return;
488         else if (peekToken() != LC) parseStatement(b, null);
489         else {
490             consume(LC);
491             while(peekToken() != RC && peekToken() != -1) parseStatement(b, null);
492             consume(RC);
493         }
494     }
495
496     /** Parse a single statement, consuming the RC or SEMI which terminates it. */
497     void parseStatement(Function b, String label) throws IOException {
498         int saveParserLine = parserLine;
499         _parseStatement(b, label);
500         parserLine = saveParserLine;
501     }
502     void _parseStatement(Function b, String label) throws IOException {
503         int tok = peekToken();
504         if (tok == -1) return;
505         switch(tok = getToken()) {
506             
507         case THROW: case ASSERT: case RETURN: {
508             if (tok == RETURN && peekToken() == SEMI)
509                 b.add(parserLine, LITERAL, null);
510             else
511                 startExpr(b, -1);
512             b.add(parserLine, tok);
513             consume(SEMI);
514             break;
515         }
516         case BREAK: case CONTINUE: {
517             if (peekToken() == NAME) consume(NAME);
518             b.add(parserLine, tok, string);
519             consume(SEMI);
520             break;
521         }
522         case VAR: {
523             b.add(parserLine, TOPSCOPE);                         // push the current scope
524             while(true) {
525                 consume(NAME);
526                 b.add(parserLine, DECLARE, string);               // declare it
527                 if (peekToken() == ASSIGN) {                     // if there is an '=' after the variable name
528                     consume(ASSIGN);
529                     startExpr(b, NO_COMMA);
530                     b.add(parserLine, PUT);                      // assign it
531                     b.add(parserLine, POP);                      // clean the stack
532                 } else {
533                     b.add(parserLine, POP);                      // pop the string pushed by declare
534                 }   
535                 if (peekToken() != COMMA) break;
536                 consume(COMMA);
537             }
538             b.add(parserLine, POP);                              // pop off the topscope
539             if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI);
540             break;
541         }
542         case IF: {
543             consume(LP);
544             startExpr(b, -1);
545             consume(RP);
546             
547             b.add(parserLine, JF, new Integer(0));                    // if false, jump to the else-block
548             int size = b.size;
549             parseStatement(b, null);
550             
551             if (peekToken() == ELSE) {
552                 consume(ELSE);
553                 b.add(parserLine, JMP, new Integer(0));               // if we took the true-block, jump over the else-block
554                 b.set(size - 1, new Integer(b.size - size + 1));
555                 size = b.size;
556                 parseStatement(b, null);
557             }
558             b.set(size - 1, new Integer(b.size - size + 1));        // regardless of which branch we took, b[size] needs to point here
559             break;
560         }
561         case WHILE: {
562             consume(LP);
563             if (label != null) b.add(parserLine, LABEL, label);
564             b.add(parserLine, LOOP);
565             int size = b.size;
566             b.add(parserLine, POP);                                   // discard the first-iteration indicator
567             startExpr(b, -1);
568             b.add(parserLine, JT, new Integer(2));                    // if the while() clause is true, jump over the BREAK
569             b.add(parserLine, BREAK);
570             consume(RP);
571             parseStatement(b, null);
572             b.add(parserLine, CONTINUE);                              // if we fall out of the end, definately continue
573             b.set(size - 1, new Integer(b.size - size + 1));        // end of the loop
574             break;
575         }
576         case SWITCH: {
577             consume(LP);
578             if (label != null) b.add(parserLine, LABEL, label);
579             b.add(parserLine, LOOP);
580             int size0 = b.size;
581             startExpr(b, -1);
582             consume(RP);
583             consume(LC);
584             while(true)
585                 if (peekToken() == CASE) {                         // we compile CASE statements like a bunch of if..else's
586                     consume(CASE);
587                     b.add(parserLine, DUP);                        // duplicate the switch() value; we'll consume one copy
588                     startExpr(b, -1);
589                     consume(COLON);
590                     b.add(parserLine, EQ);                         // check if we should do this case-block
591                     b.add(parserLine, JF, new Integer(0));         // if not, jump to the next one
592                     int size = b.size;
593                     while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) parseStatement(b, null);
594                     b.set(size - 1, new Integer(1 + b.size - size));
595                 } else if (peekToken() == DEFAULT) {
596                     consume(DEFAULT);
597                     consume(COLON);
598                     while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) parseStatement(b, null);
599                 } else if (peekToken() == RC) {
600                     consume(RC);
601                     b.add(parserLine, BREAK);                      // break out of the loop if we 'fall through'
602                     break;
603                 } else {
604                     throw pe("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
605                 }
606             b.set(size0 - 1, new Integer(b.size - size0 + 1));      // end of the loop
607             break;
608         }
609             
610         case DO: {
611             if (label != null) b.add(parserLine, LABEL, label);
612             b.add(parserLine, LOOP);
613             int size = b.size;
614             parseStatement(b, null);
615             consume(WHILE);
616             consume(LP);
617             startExpr(b, -1);
618             b.add(parserLine, JT, new Integer(2));                  // check the while() clause; jump over the BREAK if true
619             b.add(parserLine, BREAK);
620             b.add(parserLine, CONTINUE);
621             consume(RP);
622             consume(SEMI);
623             b.set(size - 1, new Integer(b.size - size + 1));      // end of the loop; write this location to the LOOP instruction
624             break;
625         }
626             
627         case TRY: {
628             b.add(parserLine, TRY); // try bytecode causes a TryMarker to be pushed
629             int tryInsn = b.size - 1;
630             // parse the expression to be TRYed
631             parseStatement(b, null); 
632             // pop the try  marker. this is pushed when the TRY bytecode is executed                              
633             b.add(parserLine, POP);
634             // jump forward to the end of the catch block, start of the finally block                             
635             b.add(parserLine, JMP);                                  
636             int successJMPInsn = b.size - 1;
637             
638             if (peekToken() != CATCH && peekToken() != FINALLY)
639                 throw pe("try without catch or finally");
640             
641             int catchJMPDistance = -1;
642             if (peekToken() == CATCH) {
643                 catchJMPDistance = b.size - tryInsn;
644                 String exceptionVar;
645                 getToken();
646                 consume(LP);
647                 consume(NAME);
648                 exceptionVar = string;
649                 consume(RP);
650                 b.add(parserLine, TOPSCOPE);                        // the exception is on top of the stack; put it to the chosen name
651                 b.add(parserLine, SWAP);
652                 b.add(parserLine, LITERAL,exceptionVar);
653                 b.add(parserLine, SWAP);
654                 b.add(parserLine, PUT);
655                 b.add(parserLine, POP);
656                 b.add(parserLine, POP);
657                 parseStatement(b, null);
658                 // pop the try and catch markers
659                 b.add(parserLine,POP);
660                 b.add(parserLine,POP);
661             }
662             
663             // jump here if no exception was thrown
664             b.set(successJMPInsn, new Integer(b.size - successJMPInsn)); 
665                         
666             int finallyJMPDistance = -1;
667             if (peekToken() == FINALLY) {
668                 b.add(parserLine, LITERAL, null); // null FinallyData
669                 finallyJMPDistance = b.size - tryInsn;
670                 consume(FINALLY);
671                 parseStatement(b, null);
672                 b.add(parserLine,FINALLY_DONE); 
673             }
674             
675             // setup the TRY arguments
676             b.set(tryInsn, new int[] { catchJMPDistance, finallyJMPDistance });
677             
678             break;
679         }
680             
681         case FOR: {
682             consume(LP);
683             
684             tok = getToken();
685             boolean hadVar = false;                                      // if it's a for..in, we ignore the VAR
686             if (tok == VAR) { hadVar = true; tok = getToken(); }
687             String varName = string;
688             boolean forIn = peekToken() == IN;                           // determine if this is a for..in loop or not
689             pushBackToken(tok, varName);
690             
691             if (forIn) {
692                 b.add(parserLine, NEWSCOPE);                             // for-loops always create new scopes
693                 b.add(parserLine, LITERAL, varName);                     // declare the new variable
694                 b.add(parserLine, DECLARE);
695                     
696                 b.add(parserLine, LOOP);                                 // we actually only add this to ensure that BREAK works
697                 b.add(parserLine, POP);                                  // discard the first-iteration indicator
698                 int size = b.size;
699                 consume(NAME);
700                 consume(IN);
701                 startExpr(b, -1);
702                 b.add(parserLine, PUSHKEYS);                             // push the keys as an array; check the length
703                 b.add(parserLine, LITERAL, "length");
704                 b.add(parserLine, GET);
705                 consume(RP);
706                     
707                 b.add(parserLine, LITERAL, new Integer(1));              // decrement the length
708                 b.add(parserLine, SUB);
709                 b.add(parserLine, DUP);
710                 b.add(parserLine, LITERAL, new Integer(0));              // see if we've exhausted all the elements
711                 b.add(parserLine, LT);
712                 b.add(parserLine, JF, new Integer(2));
713                 b.add(parserLine, BREAK);                                // if we have, then BREAK
714                 b.add(parserLine, GET_PRESERVE);                         // get the key out of the keys array
715                 b.add(parserLine, LITERAL, varName);
716                 b.add(parserLine, PUT);                                  // write it to this[varName]                          
717                 parseStatement(b, null);                                 // do some stuff
718                 b.add(parserLine, CONTINUE);                             // continue if we fall out the bottom
719
720                 b.set(size - 1, new Integer(b.size - size + 1));       // BREAK to here
721                 b.add(parserLine, OLDSCOPE);                             // restore the scope
722                     
723             } else {
724                 if (hadVar) pushBackToken(VAR, null);                    // yeah, this actually matters
725                 b.add(parserLine, NEWSCOPE);                             // grab a fresh scope
726                     
727                 parseStatement(b, null);                                 // initializer
728                 Function e2 =                                    // we need to put the incrementor before the test
729                     new Function(sourceName, parserLine, null, null);  // so we save the test here
730                 if (peekToken() != SEMI)
731                     startExpr(e2, -1);
732                 else
733                     e2.add(parserLine, b.LITERAL, Boolean.TRUE);         // handle the for(foo;;foo) case
734                 consume(SEMI);
735                 if (label != null) b.add(parserLine, LABEL, label);
736                 b.add(parserLine, LOOP);
737                 int size2 = b.size;
738                     
739                 b.add(parserLine, JT, new Integer(0));                   // if we're on the first iteration, jump over the incrementor
740                 int size = b.size;
741                 if (peekToken() != RP) {                                 // do the increment thing
742                     startExpr(b, -1);
743                     b.add(parserLine, POP);
744                 }
745                 b.set(size - 1, new Integer(b.size - size + 1));
746                 consume(RP);
747                     
748                 b.paste(e2);                                             // ok, *now* test if we're done yet
749                 b.add(parserLine, JT, new Integer(2));                   // break out if we don't meet the test
750                 b.add(parserLine, BREAK);
751                 parseStatement(b, null);
752                 b.add(parserLine, CONTINUE);                             // if we fall out the bottom, CONTINUE
753                 b.set(size2 - 1, new Integer(b.size - size2 + 1));     // end of the loop
754                     
755                 b.add(parserLine, OLDSCOPE);                             // get our scope back
756             }
757             break;
758         }
759                 
760         case NAME: {  // either a label or an identifier; this is the one place we're not LL(1)
761             String possiblyTheLabel = string;
762             if (peekToken() == COLON) {      // label
763                 consume(COLON);
764                 parseStatement(b, possiblyTheLabel);
765                 break;
766             } else {                         // expression
767                 pushBackToken(NAME, possiblyTheLabel);  
768                 startExpr(b, -1);
769                 b.add(parserLine, POP);
770                 if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI);
771                 break;
772             }
773         }
774
775         case SEMI: return;                                               // yep, the null statement is valid
776
777         case LC: {  // blocks are statements too
778             pushBackToken();
779             b.add(parserLine, NEWSCOPE);
780             parseBlock(b, label);
781             b.add(parserLine, OLDSCOPE);
782             break;
783         }
784
785         default: {  // hope that it's an expression
786             pushBackToken();
787             startExpr(b, -1);
788             b.add(parserLine, POP);
789             if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI);
790             break;
791         }
792         }
793     }
794
795
796     // ParserException //////////////////////////////////////////////////////////////////////
797     private IOException pe(String s) { return new IOException(sourceName + ":" + parserLine + " " + s); }
798     
799 }
800