+ /** Parse a single statement, consuming the RC or SEMI which terminates it. */
+ void parseStatement(JSFunction b, String label) throws IOException {
+ int saveParserLine = parserLine;
+ _parseStatement(b, label);
+ parserLine = saveParserLine;
+ }
+ void _parseStatement(JSFunction b, String label) throws IOException {
+ int tok = peekToken();
+ if (tok == -1) return;
+ switch(tok = getToken()) {
+
+ case THROW: case ASSERT: case RETURN: {
+ if (tok == RETURN && peekToken() == SEMI)
+ b.add(parserLine, LITERAL, null);
+ else
+ startExpr(b, -1);
+ b.add(parserLine, tok);
+ consume(SEMI);
+ break;
+ }
+ case BREAK: case CONTINUE: {
+ if (peekToken() == NAME) consume(NAME);
+ b.add(parserLine, tok, string);
+ consume(SEMI);
+ break;
+ }
+ case VAR: {
+ b.add(parserLine, TOPSCOPE); // push the current scope
+ while(true) {
+ consume(NAME);
+ b.add(parserLine, DECLARE, string); // declare it
+ if (peekToken() == ASSIGN) { // if there is an '=' after the variable name
+ consume(ASSIGN);
+ startExpr(b, NO_COMMA);
+ b.add(parserLine, PUT); // assign it
+ b.add(parserLine, POP); // clean the stack
+ } else {
+ b.add(parserLine, POP); // pop the string pushed by declare
+ }
+ if (peekToken() != COMMA) break;
+ consume(COMMA);
+ }
+ b.add(parserLine, POP); // pop off the topscope
+ if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI);
+ break;
+ }
+ case IF: {
+ consume(LP);
+ startExpr(b, -1);
+ consume(RP);
+
+ b.add(parserLine, JF, JS.ZERO); // if false, jump to the else-block
+ int size = b.size;
+ parseStatement(b, null);
+
+ if (peekToken() == ELSE) {
+ consume(ELSE);
+ b.add(parserLine, JMP, JS.ZERO); // if we took the true-block, jump over the else-block
+ b.set(size - 1, JS.N(b.size - size + 1));
+ size = b.size;
+ parseStatement(b, null);
+ }
+ b.set(size - 1, JS.N(b.size - size + 1)); // regardless of which branch we took, b[size] needs to point here
+ break;
+ }
+ case WHILE: {
+ consume(LP);
+ if (label != null) b.add(parserLine, LABEL, label);
+ b.add(parserLine, LOOP);
+ int size = b.size;
+ b.add(parserLine, POP); // discard the first-iteration indicator
+ startExpr(b, -1);
+ b.add(parserLine, JT, JS.N(2)); // if the while() clause is true, jump over the BREAK
+ b.add(parserLine, BREAK);
+ consume(RP);
+ parseStatement(b, null);
+ b.add(parserLine, CONTINUE); // if we fall out of the end, definately continue
+ b.set(size - 1, JS.N(b.size - size + 1)); // end of the loop
+ break;
+ }
+ case SWITCH: {
+ consume(LP);
+ if (label != null) b.add(parserLine, LABEL, label);
+ b.add(parserLine, LOOP);
+ int size0 = b.size;
+ startExpr(b, -1);
+ consume(RP);
+ consume(LC);
+ while(true)
+ if (peekToken() == CASE) { // we compile CASE statements like a bunch of if..else's
+ consume(CASE);
+ b.add(parserLine, DUP); // duplicate the switch() value; we'll consume one copy
+ startExpr(b, -1);
+ consume(COLON);
+ b.add(parserLine, EQ); // check if we should do this case-block
+ b.add(parserLine, JF, JS.ZERO); // if not, jump to the next one
+ int size = b.size;
+ while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) parseStatement(b, null);
+ b.set(size - 1, JS.N(1 + b.size - size));
+ } else if (peekToken() == DEFAULT) {
+ consume(DEFAULT);
+ consume(COLON);
+ while(peekToken() != CASE && peekToken() != DEFAULT && peekToken() != RC) parseStatement(b, null);
+ } else if (peekToken() == RC) {
+ consume(RC);
+ b.add(parserLine, BREAK); // break out of the loop if we 'fall through'
+ break;
+ } else {
+ throw pe("expected CASE, DEFAULT, or RC; got " + codeToString[peekToken()]);
+ }
+ b.set(size0 - 1, JS.N(b.size - size0 + 1)); // end of the loop
+ break;
+ }
+
+ case DO: {
+ if (label != null) b.add(parserLine, LABEL, label);
+ b.add(parserLine, LOOP);
+ int size = b.size;
+ parseStatement(b, null);
+ consume(WHILE);
+ consume(LP);
+ startExpr(b, -1);
+ b.add(parserLine, JT, JS.N(2)); // check the while() clause; jump over the BREAK if true
+ b.add(parserLine, BREAK);
+ b.add(parserLine, CONTINUE);
+ consume(RP);
+ consume(SEMI);
+ b.set(size - 1, JS.N(b.size - size + 1)); // end of the loop; write this location to the LOOP instruction
+ break;
+ }
+
+ case TRY: {
+ b.add(parserLine, TRY); // try bytecode causes a TryMarker to be pushed
+ int tryInsn = b.size - 1;
+ // parse the expression to be TRYed
+ parseStatement(b, null);
+ // pop the try marker. this is pushed when the TRY bytecode is executed
+ b.add(parserLine, POP);
+ // jump forward to the end of the catch block, start of the finally block
+ b.add(parserLine, JMP);
+ int successJMPInsn = b.size - 1;
+
+ if (peekToken() != CATCH && peekToken() != FINALLY)
+ throw pe("try without catch or finally");
+
+ int catchJMPDistance = -1;
+ if (peekToken() == CATCH) {
+ Vec catchEnds = new Vec();
+ boolean catchAll = false;
+
+ catchJMPDistance = b.size - tryInsn;
+
+ while(peekToken() == CATCH && !catchAll) {
+ String exceptionVar;
+ getToken();
+ consume(LP);
+ consume(NAME);
+ exceptionVar = string;
+ int[] writebacks = new int[] { -1, -1, -1 };
+ if (peekToken() != RP) {
+ // extended XWT catch block: catch(e faultCode "foo.bar.baz")
+ consume(NAME);
+ String propName = string;
+ b.add(parserLine, DUP);
+ b.add(parserLine, LITERAL, string);
+ b.add(parserLine, GET);
+ b.add(parserLine, DUP);
+ b.add(parserLine, LITERAL, null);
+ b.add(parserLine, EQ);
+ b.add(parserLine, JT);
+ writebacks[0] = b.size - 1;
+ if (peekToken() == STRING) {
+ consume(STRING);
+ b.add(parserLine, DUP);
+ b.add(parserLine, LITERAL, string);
+ b.add(parserLine, LT);
+ b.add(parserLine, JT);
+ writebacks[1] = b.size - 1;
+ b.add(parserLine, DUP);
+ b.add(parserLine, LITERAL, string + "/"); // (slash is ASCII after dot)
+ b.add(parserLine, GE);
+ b.add(parserLine, JT);
+ writebacks[2] = b.size - 1;
+ } else {
+ consume(NUMBER);
+ b.add(parserLine, DUP);
+ b.add(parserLine, LITERAL, number);
+ b.add(parserLine, EQ);
+ b.add(parserLine, JF);
+ writebacks[1] = b.size - 1;
+ }
+ b.add(parserLine, POP); // pop the element thats on the stack from the compare
+ } else {
+ catchAll = true;
+ }
+ consume(RP);
+ // the exception is on top of the stack; put it to the chosen name
+ b.add(parserLine, NEWSCOPE);
+ b.add(parserLine, TOPSCOPE);
+ b.add(parserLine, SWAP);
+ b.add(parserLine, LITERAL,exceptionVar);
+ b.add(parserLine, DECLARE);
+ b.add(parserLine, SWAP);
+ b.add(parserLine, PUT);
+ b.add(parserLine, POP);
+ b.add(parserLine, POP);
+ parseBlock(b, null);
+ b.add(parserLine, OLDSCOPE);
+
+ b.add(parserLine, JMP);
+ catchEnds.addElement(new Integer(b.size-1));
+
+ for(int i=0; i<3; i++) if (writebacks[i] != -1) b.set(writebacks[i], JS.N(b.size-writebacks[i]));
+ b.add(parserLine, POP); // pop the element thats on the stack from the compare
+ }
+
+ if(!catchAll)
+ b.add(parserLine, THROW);
+
+ for(int i=0;i<catchEnds.size();i++) {
+ int n = ((Integer)catchEnds.elementAt(i)).intValue();
+ b.set(n, JS.N(b.size-n));
+ }
+
+ // pop the try and catch markers
+ b.add(parserLine,POP);
+ b.add(parserLine,POP);
+ }
+
+ // jump here if no exception was thrown
+ b.set(successJMPInsn, JS.N(b.size - successJMPInsn));
+
+ int finallyJMPDistance = -1;
+ if (peekToken() == FINALLY) {
+ b.add(parserLine, LITERAL, null); // null FinallyData
+ finallyJMPDistance = b.size - tryInsn;
+ consume(FINALLY);
+ parseStatement(b, null);
+ b.add(parserLine,FINALLY_DONE);
+ }
+
+ // setup the TRY arguments
+ b.set(tryInsn, new int[] { catchJMPDistance, finallyJMPDistance });
+
+ break;
+ }
+
+ case FOR: {
+ consume(LP);
+
+ tok = getToken();
+ boolean hadVar = false; // if it's a for..in, we ignore the VAR
+ if (tok == VAR) { hadVar = true; tok = getToken(); }
+ String varName = string;
+ boolean forIn = peekToken() == IN; // determine if this is a for..in loop or not
+ pushBackToken(tok, varName);
+
+ if (forIn) {
+ b.add(parserLine, NEWSCOPE); // for-loops always create new scopes
+ b.add(parserLine, LITERAL, varName); // declare the new variable
+ b.add(parserLine, DECLARE);
+
+ b.add(parserLine, LOOP); // we actually only add this to ensure that BREAK works
+ b.add(parserLine, POP); // discard the first-iteration indicator
+ int size = b.size;
+ consume(NAME);
+ consume(IN);
+ startExpr(b, -1);
+ b.add(parserLine, PUSHKEYS); // push the keys as an array; check the length
+ b.add(parserLine, LITERAL, "length");
+ b.add(parserLine, GET);
+ consume(RP);
+
+ b.add(parserLine, LITERAL, JS.N(1)); // decrement the length
+ b.add(parserLine, SUB);
+ b.add(parserLine, DUP);
+ b.add(parserLine, LITERAL, JS.ZERO); // see if we've exhausted all the elements
+ b.add(parserLine, LT);
+ b.add(parserLine, JF, JS.N(2));
+ b.add(parserLine, BREAK); // if we have, then BREAK
+ b.add(parserLine, GET_PRESERVE); // get the key out of the keys array
+ b.add(parserLine, LITERAL, varName);
+ b.add(parserLine, PUT); // write it to this[varName]
+ parseStatement(b, null); // do some stuff
+ b.add(parserLine, CONTINUE); // continue if we fall out the bottom
+
+ b.set(size - 1, JS.N(b.size - size + 1)); // BREAK to here
+ b.add(parserLine, OLDSCOPE); // restore the scope
+
+ } else {
+ if (hadVar) pushBackToken(VAR, null); // yeah, this actually matters
+ b.add(parserLine, NEWSCOPE); // grab a fresh scope
+
+ parseStatement(b, null); // initializer
+ JSFunction e2 = // we need to put the incrementor before the test
+ new JSFunction(sourceName, parserLine, null); // so we save the test here
+ if (peekToken() != SEMI)
+ startExpr(e2, -1);
+ else
+ e2.add(parserLine, b.LITERAL, Boolean.TRUE); // handle the for(foo;;foo) case
+ consume(SEMI);
+ if (label != null) b.add(parserLine, LABEL, label);
+ b.add(parserLine, LOOP);
+ int size2 = b.size;
+
+ b.add(parserLine, JT, JS.ZERO); // if we're on the first iteration, jump over the incrementor
+ int size = b.size;
+ if (peekToken() != RP) { // do the increment thing
+ startExpr(b, -1);
+ b.add(parserLine, POP);
+ }
+ b.set(size - 1, JS.N(b.size - size + 1));
+ consume(RP);
+
+ b.paste(e2); // ok, *now* test if we're done yet
+ b.add(parserLine, JT, JS.N(2)); // break out if we don't meet the test
+ b.add(parserLine, BREAK);
+ parseStatement(b, null);
+ b.add(parserLine, CONTINUE); // if we fall out the bottom, CONTINUE
+ b.set(size2 - 1, JS.N(b.size - size2 + 1)); // end of the loop
+
+ b.add(parserLine, OLDSCOPE); // get our scope back
+ }
+ break;
+ }
+
+ case NAME: { // either a label or an identifier; this is the one place we're not LL(1)
+ String possiblyTheLabel = string;
+ if (peekToken() == COLON) { // label
+ consume(COLON);
+ parseStatement(b, possiblyTheLabel);
+ break;
+ } else { // expression
+ pushBackToken(NAME, possiblyTheLabel);
+ startExpr(b, -1);
+ b.add(parserLine, POP);
+ if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI);
+ break;
+ }
+ }
+
+ case SEMI: return; // yep, the null statement is valid
+
+ case LC: { // blocks are statements too
+ pushBackToken();
+ b.add(parserLine, NEWSCOPE);
+ parseBlock(b, label);
+ b.add(parserLine, OLDSCOPE);
+ break;
+ }
+
+ default: { // hope that it's an expression
+ pushBackToken();
+ startExpr(b, -1);
+ b.add(parserLine, POP);
+ if ((mostRecentlyReadToken != RC || peekToken() == SEMI) && peekToken() != -1 && mostRecentlyReadToken != SEMI) consume(SEMI);
+ break;
+ }
+ }