-// This file was derived from org.mozilla.javascript.TokenStream; it\r
-// is covered by the NPL 1.1.\r
-\r
-/**\r
- * The contents of this file are subject to the Netscape Public\r
- * License Version 1.1 (the "License"); you may not use this file\r
- * except in compliance with the License. You may obtain a copy of\r
- * the License at http://www.mozilla.org/NPL/\r
- *\r
- * Software distributed under the License is distributed on an "AS\r
- * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or\r
- * implied. See the License for the specific language governing\r
- * rights and limitations under the License.\r
- *\r
- * The Initial Developer of the Original Code is Netscape\r
- * Communications Corporation.\r
- *\r
- * Contributor(s): Roger Lawrence, Mike McCabe\r
- */\r
-\r
-package org.xwt.js;\r
-import java.io.*;\r
-\r
-class Lexer {\r
-\r
- SmartReader in;\r
- int op;\r
- Number number;\r
- String string;\r
- public Lexer(Reader r) { in = new SmartReader(r); }\r
-\r
- // Token Constants //////////////////////////////////////////////////////////\r
-\r
- public final static int\r
- EOL = 1, // end of line\r
- RETURN = 5,\r
- GOTO = 6,\r
- BITOR = 11,\r
- BITXOR = 12,\r
- BITAND = 13,\r
- EQ = 14,\r
- NE = 15,\r
- LT = 16,\r
- LE = 17,\r
- GT = 18,\r
- GE = 19,\r
- LSH = 20,\r
- RSH = 21,\r
- URSH = 22,\r
- ADD = 23,\r
- SUB = 24,\r
- MUL = 25,\r
- DIV = 26,\r
- MOD = 27,\r
- BITNOT = 28,\r
- DELPROP = 31,\r
- TYPEOF = 32,\r
- NAME = 44,\r
- NUMBER = 45,\r
- STRING = 46,\r
- NULL = 49,\r
- THIS = 50,\r
- FALSE = 51,\r
- TRUE = 52,\r
- SHEQ = 53, // shallow equality (===)\r
- SHNE = 54, // shallow inequality (!==)\r
- THROW = 62,\r
- IN = 63,\r
- INSTANCEOF = 64,\r
- TRY = 75,\r
- SEMI = 89, // semicolon\r
- LB = 90, // left bracket\r
- RB = 91, // right bracket\r
- LC = 92, // left curly brace\r
- RC = 93, // right curly brace\r
- LP = 94, // left paren\r
- RP = 95, // right paren\r
- COMMA = 96, // comma operator\r
- ASSIGN = 97, // assignment ops (= += -= etc.)\r
- HOOK = 98, // conditional (?:)\r
- COLON = 99, // colon\r
- OR = 100, // logical or (||)\r
- AND = 101, // logical and (&&)\r
- EQOP = 102, // equality ops (== !=)\r
- RELOP = 103, // relational ops (< <= > >=)\r
- SHOP = 104, // shift ops (<< >> >>>)\r
- UNARYOP = 105, // unary prefix operator\r
- INC = 106, // increment (++)\r
- DEC = 107, // decrement (--)\r
- DOT = 108, // member operator (.)\r
- PRIMARY = 109, // true, false, null, this\r
- FUNCTION = 110, // function keyword\r
- \r
- IF = 113, // if keyword\r
- ELSE = 114, // else keyword\r
- SWITCH = 115, // switch keyword\r
- CASE = 116, // case keyword\r
- DEFAULT = 117, // default keyword\r
- WHILE = 118, // while keyword\r
- DO = 119, // do keyword\r
- FOR = 120, // for keyword\r
- BREAK = 121, // break keyword\r
- CONTINUE = 122, // continue keyword\r
- VAR = 123, // var keyword\r
- WITH = 124, // with keyword\r
- CATCH = 125, // catch keyword\r
- FINALLY = 126, // finally keyword\r
- RESERVED = 127, // reserved keywords\r
- NOP = 128, // NOP\r
- VOID = 132, // void keyword\r
- ASSERT = 150; // assert keyword\r
-\r
-\r
- // Predicates ///////////////////////////////////////////////////////////////////////\r
-\r
- protected static boolean isJSIdentifier(String s) {\r
- int length = s.length();\r
- if (length == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) return false;\r
- for (int i=1; i<length; i++) {\r
- char c = s.charAt(i);\r
- if (!Character.isJavaIdentifierPart(c) && c == '\\' && !((i + 5) < length) &&\r
- (s.charAt(i + 1) == 'u') && 0 <= xDigitToInt(s.charAt(i + 2)) && 0 <= xDigitToInt(s.charAt(i + 3)) && \r
- 0 <= xDigitToInt(s.charAt(i + 4)) && 0 <= xDigitToInt(s.charAt(i + 5)))\r
- return false;\r
- }\r
- return true;\r
- }\r
-\r
- private static boolean isAlpha(int c) { return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); }\r
- private static boolean isDigit(int c) { return (c >= '0' && c <= '9'); }\r
- private static boolean isLineBreak(int c) { return (c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029); }\r
- private static int xDigitToInt(int c) {\r
- if ('0' <= c && c <= '9') return c - '0';\r
- if ('a' <= c && c <= 'f') return c - ('a' - 10);\r
- if ('A' <= c && c <= 'F') return c - ('A' - 10);\r
- return -1;\r
- }\r
- public static boolean isWhiteSpace(int c) {\r
- if (c == '\u0020' || c == '\u0009' || c == '\u000C' || c == '\u000B' || c == '\u00A0') return true;\r
- return Character.getType((char)c) == Character.SPACE_SEPARATOR;\r
- }\r
-\r
- \r
- // Token Subtype Handlers /////////////////////////////////////////////////////////\r
-\r
- private int getKeyword(String name) throws IOException {\r
- final int \r
- Id_break = BREAK,\r
- Id_case = CASE,\r
- Id_continue = CONTINUE,\r
- Id_default = DEFAULT,\r
- Id_delete = DELPROP,\r
- Id_do = DO,\r
- Id_else = ELSE,\r
- Id_false = PRIMARY | (FALSE << 8),\r
- Id_for = FOR,\r
- Id_function = FUNCTION,\r
- Id_if = IF,\r
- Id_in = RELOP | (IN << 8),\r
- Id_null = PRIMARY | (NULL << 8),\r
- Id_return = RETURN,\r
- Id_switch = SWITCH,\r
- Id_this = PRIMARY | (THIS << 8),\r
- Id_true = PRIMARY | (TRUE << 8),\r
- Id_typeof = UNARYOP | (TYPEOF << 8),\r
- Id_var = VAR,\r
- Id_void = UNARYOP | (VOID << 8),\r
- Id_while = WHILE,\r
- Id_with = WITH,\r
-\r
- // the following are #ifdef RESERVE_JAVA_KEYWORDS in jsscan.c\r
- Id_abstract = RESERVED,\r
- Id_boolean = RESERVED,\r
- Id_byte = RESERVED,\r
- Id_catch = CATCH,\r
- Id_char = RESERVED,\r
- Id_class = RESERVED,\r
- Id_const = RESERVED,\r
- Id_debugger = RESERVED,\r
- Id_double = RESERVED,\r
- Id_enum = RESERVED,\r
- Id_extends = RESERVED,\r
- Id_final = RESERVED,\r
- Id_finally = FINALLY,\r
- Id_float = RESERVED,\r
- Id_goto = RESERVED,\r
- Id_implements = RESERVED,\r
- Id_instanceof = RELOP | (INSTANCEOF << 8),\r
- Id_int = RESERVED,\r
- Id_interface = RESERVED,\r
- Id_long = RESERVED,\r
- Id_native = RESERVED,\r
- Id_package = RESERVED,\r
- Id_private = RESERVED,\r
- Id_protected = RESERVED,\r
- Id_public = RESERVED,\r
- Id_assert = ASSERT,\r
- Id_short = RESERVED,\r
- Id_static = RESERVED,\r
- Id_super = RESERVED,\r
- Id_synchronized = RESERVED,\r
- Id_throw = THROW,\r
- Id_throws = RESERVED,\r
- Id_transient = RESERVED,\r
- Id_try = TRY,\r
- Id_volatile = RESERVED;\r
- \r
- int id;\r
- String s = name;\r
- L0: { id = -1; String X = null; int c;\r
- L: switch (s.length()) {\r
- case 2: c=s.charAt(1);\r
- if (c=='f') { if (s.charAt(0)=='i') {id=Id_if; break L0;} }\r
- else if (c=='n') { if (s.charAt(0)=='i') {id=Id_in; break L0;} }\r
- else if (c=='o') { if (s.charAt(0)=='d') {id=Id_do; break L0;} }\r
- break L;\r
- case 3: switch (s.charAt(0)) {\r
- case 'f': if (s.charAt(2)=='r' && s.charAt(1)=='o') {id=Id_for; break L0;} break L;\r
- case 'i': if (s.charAt(2)=='t' && s.charAt(1)=='n') {id=Id_int; break L0;} break L;\r
- case 'n': if (s.charAt(2)=='w' && s.charAt(1)=='e')\r
- throw new IOException("the new keyword is not permitted in XWT scripts");\r
- break L;\r
- case 't': if (s.charAt(2)=='y' && s.charAt(1)=='r') {id=Id_try; break L0;} break L;\r
- case 'v': if (s.charAt(2)=='r' && s.charAt(1)=='a') {id=Id_var; break L0;} break L;\r
- } break L;\r
- case 4: switch (s.charAt(0)) {\r
- case 'b': X="byte";id=Id_byte; break L;\r
- case 'c': c=s.charAt(3);\r
- if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='a') {id=Id_case; break L0;} }\r
- else if (c=='r') { if (s.charAt(2)=='a' && s.charAt(1)=='h') {id=Id_char; break L0;} }\r
- break L;\r
- case 'e': c=s.charAt(3);\r
- if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='l') {id=Id_else; break L0;} }\r
- else if (c=='m') { if (s.charAt(2)=='u' && s.charAt(1)=='n') {id=Id_enum; break L0;} }\r
- break L;\r
- case 'g': X="goto";id=Id_goto; break L;\r
- case 'l': X="long";id=Id_long; break L;\r
- case 'n': X="null";id=Id_null; break L;\r
- case 't': c=s.charAt(3);\r
- if (c=='e') { if (s.charAt(2)=='u' && s.charAt(1)=='r') {id=Id_true; break L0;} }\r
- else if (c=='s') { if (s.charAt(2)=='i' && s.charAt(1)=='h') {id=Id_this; break L0;} }\r
- break L;\r
- case 'v': X="void";id=Id_void; break L;\r
- case 'w': X="with";id=Id_with; break L;\r
- } break L;\r
- case 5: switch (s.charAt(2)) {\r
- case 'a': X="class";id=Id_class; break L;\r
- case 'e': X="break";id=Id_break; break L;\r
- case 'i': X="while";id=Id_while; break L;\r
- case 'l': X="false";id=Id_false; break L;\r
- case 'n': c=s.charAt(0);\r
- if (c=='c') { X="const"; throw new IOException("the const keyword is not permitted in XWT"); }\r
- else if (c=='f') { X="final";id=Id_final; }\r
- break L;\r
- case 'o': c=s.charAt(0);\r
- if (c=='f') { X="float";id=Id_float; }\r
- else if (c=='s') { X="short";id=Id_short; }\r
- break L;\r
- case 'p': X="super";id=Id_super; break L;\r
- case 'r': X="throw";id=Id_throw; break L;\r
- case 't': X="catch";id=Id_catch; break L;\r
- } break L;\r
- case 6: switch (s.charAt(1)) {\r
- case 'a': X="native";id=Id_native; break L;\r
- case 'e': c=s.charAt(0);\r
- if (c=='d') { X="delete"; throw new IOException("the delete keyword is not permitted in XWT scripts"); }\r
- else if (c=='r') { X="return";id=Id_return; }\r
- break L;\r
- case 'h': X="throws";id=Id_throws; break L;\r
- case 'o': X="double";id=Id_double; break L;\r
- case 's': X="assert";id=Id_assert; break L;\r
- case 'u': X="public";id=Id_public; break L;\r
- case 'w': X="switch";id=Id_switch; break L;\r
- case 'y': X="typeof";id=Id_typeof; break L;\r
- } break L;\r
- case 7: switch (s.charAt(1)) {\r
- case 'a': X="package";id=Id_package; break L;\r
- case 'e': X="default";id=Id_default; break L;\r
- case 'i': X="finally";id=Id_finally; break L;\r
- case 'o': X="boolean";id=Id_boolean; break L;\r
- case 'r': X="private";id=Id_private; break L;\r
- case 'x': X="extends";id=Id_extends; break L;\r
- } break L;\r
- case 8: switch (s.charAt(0)) {\r
- case 'a': X="abstract";id=Id_abstract; break L;\r
- case 'c': X="continue";id=Id_continue; break L;\r
- case 'd': X="debugger";id=Id_debugger; break L;\r
- case 'f': X="function";id=Id_function; break L;\r
- case 'v': X="volatile";id=Id_volatile; break L;\r
- } break L;\r
- case 9: c=s.charAt(0);\r
- if (c=='i') { X="interface";id=Id_interface; }\r
- else if (c=='p') { X="protected";id=Id_protected; }\r
- else if (c=='t') { X="transient";id=Id_transient; }\r
- break L;\r
- case 10: c=s.charAt(1);\r
- if (c=='m') { X="implements";id=Id_implements; }\r
- else if (c=='n') { X="instanceof"; throw new IOException("the instanceof keyword is not permitted in XWT scripts"); }\r
- break L;\r
- case 12: X="synchronized";id=Id_synchronized; break L;\r
- }\r
- if (X!=null && X!=s && !X.equals(s)) id = -1;\r
- }\r
- if (id == -1) { return -1; }\r
- this.op = id >> 8;\r
- return id & 0xff;\r
- }\r
-\r
- private int getIdentifier(int c) throws IOException {\r
- in.startString();\r
- while (Character.isJavaIdentifierPart((char)(c = in.read())));\r
- in.unread();\r
- String str = in.getString();\r
- int result = getKeyword(str);\r
- if (result != -1) return result;\r
- this.string = str;\r
- return NAME;\r
- }\r
- \r
- private int getNumber(int c) throws IOException {\r
- int base = 10;\r
- in.startString();\r
- double dval = Double.NaN;\r
- long longval = 0;\r
- boolean isInteger = true;\r
- \r
- // figure out what base we're using\r
- if (c == '0') {\r
- if (Character.toLowerCase((char)(c = in.read())) == 'x') { base = 16; in.startString(); }\r
- else if (isDigit(c)) base = 8;\r
- }\r
- \r
- while (0 <= xDigitToInt(c) && !(base < 16 && isAlpha(c))) c = in.read();\r
- if (base == 10 && (c == '.' || c == 'e' || c == 'E')) {\r
- isInteger = false;\r
- if (c == '.') do { c = in.read(); } while (isDigit(c));\r
- if (c == 'e' || c == 'E') {\r
- c = in.read();\r
- if (c == '+' || c == '-') c = in.read();\r
- if (!isDigit(c)) throw new IOException("msg.missing.exponent");\r
- do { c = in.read(); } while (isDigit(c));\r
- }\r
- }\r
- in.unread();\r
-\r
- String numString = in.getString();\r
- if (base == 10 && !isInteger) {\r
- try { dval = (Double.valueOf(numString)).doubleValue(); }\r
- catch (NumberFormatException ex) { throw new IOException("msg.caught.nfe"); }\r
- } else {\r
- if (isInteger) {\r
- longval = Long.parseLong(numString, base);\r
- dval = (double)longval;\r
- else {\r
- // FIXME: we're not handling hex/octal fractions... does that matter?\r
- dval = Double.parseDouble(numString);\r
- longval = (long) dval;\r
- if (longval == dval) isInteger = true;\r
- }\r
- }\r
- \r
- if (!isInteger) this.number = new Double(dval);\r
- else if (Byte.MIN_VALUE <= longval && longval <= Byte.MAX_VALUE) this.number = new Byte((byte)longval);\r
- else if (Short.MIN_VALUE <= longval && longval <= Short.MAX_VALUE) this.number = new Short((short)longval);\r
- else if (Integer.MIN_VALUE <= longval && longval <= Integer.MAX_VALUE) this.number = new Integer((int)longval);\r
- else this.number = new Double(longval);\r
- return NUMBER;\r
- }\r
- \r
- private int getString(int c) throws IOException {\r
- StringBuffer stringBuf = null;\r
- int quoteChar = c;\r
- int val = 0;\r
- c = in.read();\r
- in.startString(); // start after the first "\r
- while(c != quoteChar) {\r
- if (c == '\n' || c == -1) throw new IOException("msg.unterminated.string.lit");\r
- if (c == '\\') {\r
- if (stringBuf == null) {\r
- in.unread(); // Don't include the backslash\r
- stringBuf = new StringBuffer(in.getString());\r
- in.read();\r
- }\r
- switch (c = in.read()) {\r
- case 'b': c = '\b'; break;\r
- case 'f': c = '\f'; break;\r
- case 'n': c = '\n'; break;\r
- case 'r': c = '\r'; break;\r
- case 't': c = '\t'; break;\r
- case 'v': c = '\u000B'; break;\r
- default: throw new IOException("\\u and \\0 escapes not currently supported -- use XML entities");\r
- }\r
- }\r
- if (stringBuf != null) stringBuf.append((char) c);\r
- c = in.read();\r
- }\r
- if (stringBuf != null) this.string = stringBuf.toString();\r
- else {\r
- in.unread(); // miss the trailing "\r
- this.string = in.getString();\r
- in.read();\r
- }\r
- return STRING;\r
- }\r
-\r
- // hack because you can't do "foo ? 1 : (bar(); 2)" in Java\r
- final int op(int set, int ret) { this.op = set; return ret; }\r
-\r
- public int getToken() throws IOException {\r
- int c;\r
- do { if ((c = in.read()) == '\n') break; } while (isWhiteSpace(c) || c == '\n');\r
- if (c == -1) return -1;\r
- if (c == '\\' && in.peek() == 'u') throw new IOException("\\u and \\0 escapes not currently supported -- use XML entities");\r
- if (Character.isJavaIdentifierStart((char)c)) return getIdentifier(c);\r
- if (isDigit(c) || (c == '.' && isDigit(in.peek()))) return getNumber(c);\r
- if (c == '"' || c == '\'') return getString(c);\r
- switch (c) {\r
- case '\n': return EOL;\r
- case ';': return SEMI;\r
- case '[': return LB;\r
- case ']': return RB;\r
- case '{': return LC;\r
- case '}': return RC;\r
- case '(': return LP;\r
- case ')': return RP;\r
- case ',': return COMMA;\r
- case '?': return HOOK;\r
- case ':': return COLON;\r
- case '.': return DOT;\r
- case '|': return in.match('|') ? OR : (in.match('=') ? op(BITOR, ASSIGN) : BITOR);\r
- case '^': return in.match('=') ? op(BITXOR, ASSIGN) : BITXOR;\r
- case '&': return in.match('&') ? AND : in.match('=') ? op(BITAND, ASSIGN) : BITAND;\r
- case '=': return !in.match('=') ? op(NOP, ASSIGN) : op(in.match('=') ? SHEQ : EQ, EQOP);\r
- case '!': return !in.match('=') ? op(NOP, UNARYOP) : op(in.match('=') ? SHNE : NE, EQOP);\r
- case '%': return op(MOD, in.match('=') ? ASSIGN : MOD);\r
- case '~': return op(BITNOT, UNARYOP);\r
- case '+': return in.match('=') ? op(ADD, ASSIGN) : in.match('+') ? INC : ADD;\r
- case '-': return in.match('=') ? op(SUB, ASSIGN) : in.match('-') ? DEC : SUB;\r
- case '*': return in.match('=') ? op(MUL, ASSIGN) : MUL;\r
- case '<': return !in.match('<') ?\r
- op(in.match('=') ? LE : LT, RELOP) :\r
- in.match('=') ? op(LSH, ASSIGN) : op(LSH, SHOP);\r
- case '>': return !in.match('>') ?\r
- op(in.match('=') ? GE : GT, RELOP) :\r
- in.match('>') ?\r
- op(URSH, in.match('=') ? ASSIGN : SHOP) :\r
- op(RSH, in.match('=') ? ASSIGN : SHOP);\r
- case '/':\r
- if (in.match('=')) { op(DIV, ASSIGN); }\r
- if (in.match('/')) { while ((c = in.read()) != -1 && c != '\n'); in.unread(); return getToken(); }\r
- if (!in.match('*')) return DIV;\r
- while ((c = in.read()) != -1 && !(c == '*' && in.match('/'))) {\r
- if (c == '\n' || c != '/' || !in.match('*')) continue;\r
- if (in.match('/')) return getToken();\r
- throw new IOException("msg.nested.comment");\r
- }\r
- if (c == -1) throw new IOException("msg.unterminated.comment");\r
- return getToken(); // `goto retry'\r
- default: throw new IOException("illegal character: " + c);\r
- }\r
- }\r
-\r
- private static class SmartReader {\r
- PushbackReader reader = null;\r
- int lastread = -1;\r
-\r
- public SmartReader(Reader r) { reader = new PushbackReader(r); }\r
- public void unread() throws IOException { reader.unread(lastread); }\r
- public boolean match(char c) throws IOException { if (peek() == c) { reader.read(); return true; } else return false; }\r
- public int peek() throws IOException {\r
- int peeked = reader.read();\r
- if (peeked != -1) reader.unread((char)peeked);\r
- return peeked;\r
- }\r
- public int read() throws IOException {\r
- lastread = reader.read();\r
- if (accumulator != null) accumulator.append(lastread);\r
- return lastread;\r
- }\r
-\r
- // FIXME: could be much more efficient\r
- StringBuffer accumulator = null;\r
- public void startString() { accumulator = new StringBuffer(); }\r
- public String getString() throws IOException {\r
- String ret = accumulator.toString();\r
- accumulator = null;\r
- return ret;\r
- }\r
- }\r
-\r
-}\r
+// This file was derived from org.mozilla.javascript.TokenStream; it
+// is covered by the NPL 1.1.
+
+/**
+ * The contents of this file are subject to the Netscape Public
+ * License Version 1.1 (the "License"); you may not use this file
+ * except in compliance with the License. You may obtain a copy of
+ * the License at http://www.mozilla.org/NPL/
+ *
+ * Software distributed under the License is distributed on an "AS
+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
+ * implied. See the License for the specific language governing
+ * rights and limitations under the License.
+ *
+ * The Initial Developer of the Original Code is Netscape
+ * Communications Corporation.
+ *
+ * Contributor(s): Roger Lawrence, Mike McCabe
+ */
+
+package org.xwt.js;
+import java.io.*;
+
+class Lexer {
+
+ private SmartReader in;
+ private boolean pushedBack = false;
+
+ private int op;
+ public Number number;
+ public String string;
+
+ public Lexer(Reader r) throws IOException { in = new SmartReader(r); }
+ public int peekToken() throws IOException { int ret = peekToken(); pushBackToken(); return ret; }
+ public void pushBackToken() { if (pushedBack) throw new Error("can't push back twice"); pushedBack = true; }
+
+ // Token Constants //////////////////////////////////////////////////////////
+
+ public final static int
+ EOL = 1, // end of line
+ RETURN = 5, // return
+ GOTO = 6, // goto
+ BITOR = 11, // |
+ ASSIGN_BITOR = 211, // |=
+ BITXOR = 12, // ^
+ ASSIGN_BITXOR= 212, // ^=
+ BITAND = 13, // &
+ ASSIGN_BITAND= 213, // &=
+ EQ = 14, // ==
+ NE = 15, // !=
+ LT = 16, // <
+ LE = 17, // <=
+ GT = 18, // >
+ GE = 19, // >=
+ LSH = 20, // <<
+ ASSIGN_LSH = 220, // <<=
+ RSH = 21, // >>
+ ASSIGN_RSH = 221, // >>=
+ URSH = 22, // >>>
+ ASSIGN_URSH = 222, // >>>=
+ ADD = 23, // +
+ ASSIGN_ADD = 223, // +=
+ SUB = 24, // -
+ ASSIGN_SUB = 224, // -=
+ MUL = 25, // *
+ ASSIGN_MUL = 225, // *=
+ DIV = 26, // /
+ ASSIGN_DIV = 226, // /=
+ MOD = 27, // %
+ ASSIGN_MOD = 227, // %=
+ BITNOT = 28, // ~
+ ASSIGN_BITNOT= 228, // ~=
+ DELPROP = 31, // delete
+ TYPEOF = 32, // typeof
+ NAME = 44, // *** identifiers ***
+ NUMBER = 45, // *** numeric literals ***
+ STRING = 46, // *** string literals ***
+ NULL = 49, // null
+ THIS = 50, // this
+ FALSE = 51, // false
+ TRUE = 52, // true
+ SHEQ = 53, // ===
+ SHNE = 54, // !==
+ THROW = 62, // throw
+ IN = 63, // in
+ INSTANCEOF = 64, // instanceof
+ TRY = 75, // try
+ SEMI = 89, // ;
+ LB = 90, // [
+ RB = 91, // ]
+ LC = 92, // {
+ RC = 93, // }
+ LP = 94, // (
+ RP = 95, // )
+ COMMA = 96, // ,
+ ASSIGN = 97, // =
+ HOOK = 98, // ?
+ COLON = 99, // :
+ OR = 100, // ||
+ AND = 101, // &&
+ INC = 106, // ++
+ DEC = 107, // --
+ DOT = 108, // .
+ FUNCTION = 110, // function
+ IF = 113, // if keyword
+ ELSE = 114, // else keyword
+ SWITCH = 115, // switch keyword
+ CASE = 116, // case keyword
+ DEFAULT = 117, // default keyword
+ WHILE = 118, // while keyword
+ DO = 119, // do keyword
+ FOR = 120, // for keyword
+ BREAK = 121, // break keyword
+ CONTINUE = 122, // continue keyword
+ VAR = 123, // var keyword
+ WITH = 124, // with keyword
+ CATCH = 125, // catch keyword
+ FINALLY = 126, // finally keyword
+ RESERVED = 127, // reserved keywords
+ NOP = 128, // NOP
+ VOID = 132, // void keyword
+ MOD_ASSIGN = 133, // %=
+ BANG = 134, // %=
+ ASSERT = 150; // assert keyword
+
+
+ // Predicates ///////////////////////////////////////////////////////////////////////
+
+ protected static boolean isJSIdentifier(String s) {
+ int length = s.length();
+ if (length == 0 || !Character.isJavaIdentifierStart(s.charAt(0))) return false;
+ for (int i=1; i<length; i++) {
+ char c = s.charAt(i);
+ if (!Character.isJavaIdentifierPart(c) && c == '\\' && !((i + 5) < length) &&
+ (s.charAt(i + 1) == 'u') && 0 <= xDigitToInt(s.charAt(i + 2)) && 0 <= xDigitToInt(s.charAt(i + 3)) &&
+ 0 <= xDigitToInt(s.charAt(i + 4)) && 0 <= xDigitToInt(s.charAt(i + 5)))
+ return false;
+ }
+ return true;
+ }
+
+ private static boolean isAlpha(int c) { return ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')); }
+ private static boolean isDigit(int c) { return (c >= '0' && c <= '9'); }
+ private static boolean isLineBreak(int c) { return (c == '\n' || c == '\r' || c == 0x2028 || c == 0x2029); }
+ private static int xDigitToInt(int c) {
+ if ('0' <= c && c <= '9') return c - '0';
+ if ('a' <= c && c <= 'f') return c - ('a' - 10);
+ if ('A' <= c && c <= 'F') return c - ('A' - 10);
+ return -1;
+ }
+ public static boolean isWhiteSpace(int c) {
+ if (c == '\u0020' || c == '\u0009' || c == '\u000C' || c == '\u000B' || c == '\u00A0') return true;
+ return Character.getType((char)c) == Character.SPACE_SEPARATOR;
+ }
+
+
+ // Token Subtype Handlers /////////////////////////////////////////////////////////
+
+ private int getKeyword(String name) throws IOException {
+ final int
+ Id_break = BREAK,
+ Id_case = CASE,
+ Id_continue = CONTINUE,
+ Id_default = DEFAULT,
+ Id_delete = DELPROP,
+ Id_do = DO,
+ Id_else = ELSE,
+ Id_false = FALSE,
+ Id_for = FOR,
+ Id_function = FUNCTION,
+ Id_if = IF,
+ Id_in = IN,
+ Id_null = NULL,
+ Id_return = RETURN,
+ Id_switch = SWITCH,
+ Id_this = THIS,
+ Id_true = TRUE,
+ Id_typeof = TYPEOF,
+ Id_var = VAR,
+ Id_void = VOID,
+ Id_while = WHILE,
+ Id_with = WITH,
+
+ // the following are #ifdef RESERVE_JAVA_KEYWORDS in jsscan.c
+ Id_abstract = RESERVED,
+ Id_boolean = RESERVED,
+ Id_byte = RESERVED,
+ Id_catch = CATCH,
+ Id_char = RESERVED,
+ Id_class = RESERVED,
+ Id_const = RESERVED,
+ Id_debugger = RESERVED,
+ Id_double = RESERVED,
+ Id_enum = RESERVED,
+ Id_extends = RESERVED,
+ Id_final = RESERVED,
+ Id_finally = FINALLY,
+ Id_float = RESERVED,
+ Id_goto = RESERVED,
+ Id_implements = RESERVED,
+ Id_instanceof = INSTANCEOF,
+ Id_int = RESERVED,
+ Id_interface = RESERVED,
+ Id_long = RESERVED,
+ Id_native = RESERVED,
+ Id_package = RESERVED,
+ Id_private = RESERVED,
+ Id_protected = RESERVED,
+ Id_public = RESERVED,
+ Id_assert = ASSERT,
+ Id_short = RESERVED,
+ Id_static = RESERVED,
+ Id_super = RESERVED,
+ Id_synchronized = RESERVED,
+ Id_throw = THROW,
+ Id_throws = RESERVED,
+ Id_transient = RESERVED,
+ Id_try = TRY,
+ Id_volatile = RESERVED;
+
+ int id;
+ String s = name;
+ L0: { id = -1; String X = null; int c;
+ L: switch (s.length()) {
+ case 2: c=s.charAt(1);
+ if (c=='f') { if (s.charAt(0)=='i') {id=Id_if; break L0;} }
+ else if (c=='n') { if (s.charAt(0)=='i') {id=Id_in; break L0;} }
+ else if (c=='o') { if (s.charAt(0)=='d') {id=Id_do; break L0;} }
+ break L;
+ case 3: switch (s.charAt(0)) {
+ case 'f': if (s.charAt(2)=='r' && s.charAt(1)=='o') {id=Id_for; break L0;} break L;
+ case 'i': if (s.charAt(2)=='t' && s.charAt(1)=='n') {id=Id_int; break L0;} break L;
+ case 'n': if (s.charAt(2)=='w' && s.charAt(1)=='e')
+ throw new IOException("the new keyword is not permitted in XWT scripts");
+ break L;
+ case 't': if (s.charAt(2)=='y' && s.charAt(1)=='r') {id=Id_try; break L0;} break L;
+ case 'v': if (s.charAt(2)=='r' && s.charAt(1)=='a') {id=Id_var; break L0;} break L;
+ } break L;
+ case 4: switch (s.charAt(0)) {
+ case 'b': X="byte";id=Id_byte; break L;
+ case 'c': c=s.charAt(3);
+ if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='a') {id=Id_case; break L0;} }
+ else if (c=='r') { if (s.charAt(2)=='a' && s.charAt(1)=='h') {id=Id_char; break L0;} }
+ break L;
+ case 'e': c=s.charAt(3);
+ if (c=='e') { if (s.charAt(2)=='s' && s.charAt(1)=='l') {id=Id_else; break L0;} }
+ else if (c=='m') { if (s.charAt(2)=='u' && s.charAt(1)=='n') {id=Id_enum; break L0;} }
+ break L;
+ case 'g': X="goto";id=Id_goto; break L;
+ case 'l': X="long";id=Id_long; break L;
+ case 'n': X="null";id=Id_null; break L;
+ case 't': c=s.charAt(3);
+ if (c=='e') { if (s.charAt(2)=='u' && s.charAt(1)=='r') {id=Id_true; break L0;} }
+ else if (c=='s') { if (s.charAt(2)=='i' && s.charAt(1)=='h') {id=Id_this; break L0;} }
+ break L;
+ case 'v': X="void";id=Id_void; break L;
+ case 'w': X="with";id=Id_with; break L;
+ } break L;
+ case 5: switch (s.charAt(2)) {
+ case 'a': X="class";id=Id_class; break L;
+ case 'e': X="break";id=Id_break; break L;
+ case 'i': X="while";id=Id_while; break L;
+ case 'l': X="false";id=Id_false; break L;
+ case 'n': c=s.charAt(0);
+ if (c=='c') { X="const"; throw new IOException("the const keyword is not permitted in XWT"); }
+ else if (c=='f') { X="final";id=Id_final; }
+ break L;
+ case 'o': c=s.charAt(0);
+ if (c=='f') { X="float";id=Id_float; }
+ else if (c=='s') { X="short";id=Id_short; }
+ break L;
+ case 'p': X="super";id=Id_super; break L;
+ case 'r': X="throw";id=Id_throw; break L;
+ case 't': X="catch";id=Id_catch; break L;
+ } break L;
+ case 6: switch (s.charAt(1)) {
+ case 'a': X="native";id=Id_native; break L;
+ case 'e': c=s.charAt(0);
+ if (c=='d') { X="delete"; throw new IOException("the delete keyword is not permitted in XWT scripts"); }
+ else if (c=='r') { X="return";id=Id_return; }
+ break L;
+ case 'h': X="throws";id=Id_throws; break L;
+ case 'o': X="double";id=Id_double; break L;
+ case 's': X="assert";id=Id_assert; break L;
+ case 'u': X="public";id=Id_public; break L;
+ case 'w': X="switch";id=Id_switch; break L;
+ case 'y': X="typeof";id=Id_typeof; break L;
+ } break L;
+ case 7: switch (s.charAt(1)) {
+ case 'a': X="package";id=Id_package; break L;
+ case 'e': X="default";id=Id_default; break L;
+ case 'i': X="finally";id=Id_finally; break L;
+ case 'o': X="boolean";id=Id_boolean; break L;
+ case 'r': X="private";id=Id_private; break L;
+ case 'x': X="extends";id=Id_extends; break L;
+ } break L;
+ case 8: switch (s.charAt(0)) {
+ case 'a': X="abstract";id=Id_abstract; break L;
+ case 'c': X="continue";id=Id_continue; break L;
+ case 'd': X="debugger";id=Id_debugger; break L;
+ case 'f': X="function";id=Id_function; break L;
+ case 'v': X="volatile";id=Id_volatile; break L;
+ } break L;
+ case 9: c=s.charAt(0);
+ if (c=='i') { X="interface";id=Id_interface; }
+ else if (c=='p') { X="protected";id=Id_protected; }
+ else if (c=='t') { X="transient";id=Id_transient; }
+ break L;
+ case 10: c=s.charAt(1);
+ if (c=='m') { X="implements";id=Id_implements; }
+ else if (c=='n') { X="instanceof"; throw new IOException("the instanceof keyword is not permitted in XWT scripts"); }
+ break L;
+ case 12: X="synchronized";id=Id_synchronized; break L;
+ }
+ if (X!=null && X!=s && !X.equals(s)) id = -1;
+ }
+ if (id == -1) { return -1; }
+ this.op = id >> 8;
+ return id & 0xff;
+ }
+
+ private int getIdentifier(int c) throws IOException {
+ in.startString();
+ while (Character.isJavaIdentifierPart((char)(c = in.read())));
+ in.unread();
+ String str = in.getString();
+ int result = getKeyword(str);
+ if (result != -1) return result;
+ this.string = str;
+ return NAME;
+ }
+
+ private int getNumber(int c) throws IOException {
+ int base = 10;
+ in.startString();
+ double dval = Double.NaN;
+ long longval = 0;
+ boolean isInteger = true;
+
+ // figure out what base we're using
+ if (c == '0') {
+ if (Character.toLowerCase((char)(c = in.read())) == 'x') { base = 16; in.startString(); }
+ else if (isDigit(c)) base = 8;
+ }
+
+ while (0 <= xDigitToInt(c) && !(base < 16 && isAlpha(c))) c = in.read();
+ if (base == 10 && (c == '.' || c == 'e' || c == 'E')) {
+ isInteger = false;
+ if (c == '.') do { c = in.read(); } while (isDigit(c));
+ if (c == 'e' || c == 'E') {
+ c = in.read();
+ if (c == '+' || c == '-') c = in.read();
+ if (!isDigit(c)) throw new IOException("msg.missing.exponent");
+ do { c = in.read(); } while (isDigit(c));
+ }
+ }
+ in.unread();
+
+ String numString = in.getString();
+ if (base == 10 && !isInteger) {
+ try { dval = (Double.valueOf(numString)).doubleValue(); }
+ catch (NumberFormatException ex) { throw new IOException("msg.caught.nfe"); }
+ } else {
+ if (isInteger) {
+ longval = Long.parseLong(numString, base);
+ dval = (double)longval;
+ } else {
+ // FIXME: we're not handling hex/octal fractions... does that matter?
+ dval = Double.parseDouble(numString);
+ longval = (long) dval;
+ if (longval == dval) isInteger = true;
+ }
+ }
+
+ if (!isInteger) this.number = new Double(dval);
+ else if (Byte.MIN_VALUE <= longval && longval <= Byte.MAX_VALUE) this.number = new Byte((byte)longval);
+ else if (Short.MIN_VALUE <= longval && longval <= Short.MAX_VALUE) this.number = new Short((short)longval);
+ else if (Integer.MIN_VALUE <= longval && longval <= Integer.MAX_VALUE) this.number = new Integer((int)longval);
+ else this.number = new Double(longval);
+ return NUMBER;
+ }
+
+ private int getString(int c) throws IOException {
+ StringBuffer stringBuf = null;
+ int quoteChar = c;
+ int val = 0;
+ c = in.read();
+ in.startString(); // start after the first "
+ while(c != quoteChar) {
+ if (c == '\n' || c == -1) throw new IOException("msg.unterminated.string.lit");
+ if (c == '\\') {
+ if (stringBuf == null) {
+ in.unread(); // Don't include the backslash
+ stringBuf = new StringBuffer(in.getString());
+ in.read();
+ }
+ switch (c = in.read()) {
+ case 'b': c = '\b'; break;
+ case 'f': c = '\f'; break;
+ case 'n': c = '\n'; break;
+ case 'r': c = '\r'; break;
+ case 't': c = '\t'; break;
+ case 'v': c = '\u000B'; break;
+ default: throw new IOException("\\u and \\0 escapes not currently supported -- use XML entities");
+ }
+ }
+ if (stringBuf != null) stringBuf.append((char) c);
+ c = in.read();
+ }
+ if (stringBuf != null) this.string = stringBuf.toString();
+ else {
+ in.unread(); // miss the trailing "
+ this.string = in.getString();
+ in.read();
+ }
+ return STRING;
+ }
+
+ public int getToken() throws IOException {
+ if (pushedBack) { pushedBack = false; return op; }
+ return (op = getToken());
+ }
+
+ public int _getToken() throws IOException {
+ int c;
+ do { if ((c = in.read()) == '\n') break; } while (isWhiteSpace(c) || c == '\n');
+ if (c == -1) return -1;
+ if (c == '\\' && in.peek() == 'u') throw new IOException("\\u and \\0 escapes not currently supported -- use XML entities");
+ if (Character.isJavaIdentifierStart((char)c)) return getIdentifier(c);
+ if (isDigit(c) || (c == '.' && isDigit(in.peek()))) return getNumber(c);
+ if (c == '"' || c == '\'') return getString(c);
+ switch (c) {
+ case '\n': return EOL;
+ case ';': return SEMI;
+ case '[': return LB;
+ case ']': return RB;
+ case '{': return LC;
+ case '}': return RC;
+ case '(': return LP;
+ case ')': return RP;
+ case ',': return COMMA;
+ case '?': return HOOK;
+ case ':': return COLON;
+ case '.': return DOT;
+ case '|': return in.match('|') ? OR : (in.match('=') ? ASSIGN_BITOR : BITOR);
+ case '^': return in.match('=') ? ASSIGN_BITXOR : BITXOR;
+ case '&': return in.match('&') ? AND : in.match('=') ? ASSIGN_BITAND : BITAND;
+ case '=': return !in.match('=') ? ASSIGN : in.match('=') ? SHEQ : EQ;
+ case '!': return !in.match('=') ? BANG : in.match('=') ? SHNE : NE;
+ case '%': return in.match('=') ? MOD_ASSIGN : MOD;
+ case '~': return BITNOT;
+ case '+': return in.match('=') ? ASSIGN_ADD : in.match('+') ? INC : ADD;
+ case '-': return in.match('=') ? ASSIGN_SUB: in.match('-') ? DEC : SUB;
+ case '*': return in.match('=') ? ASSIGN_MUL : MUL;
+ case '<': return !in.match('<') ?
+ (in.match('=') ? LE : LT) :
+ in.match('=') ? ASSIGN_LSH : LSH;
+ case '>': return !in.match('>') ?
+ (in.match('=') ? GE : GT) :
+ in.match('>') ?
+ (in.match('=') ? ASSIGN_URSH : URSH) :
+ (in.match('=') ? ASSIGN_RSH : RSH);
+ case '/':
+ if (in.match('=')) return ASSIGN_DIV;
+ if (in.match('/')) { while ((c = in.read()) != -1 && c != '\n'); in.unread(); return getToken(); }
+ if (!in.match('*')) return DIV;
+ while ((c = in.read()) != -1 && !(c == '*' && in.match('/'))) {
+ if (c == '\n' || c != '/' || !in.match('*')) continue;
+ if (in.match('/')) return getToken();
+ throw new IOException("msg.nested.comment");
+ }
+ if (c == -1) throw new IOException("msg.unterminated.comment");
+ return getToken(); // `goto retry'
+ default: throw new IOException("illegal character: " + c);
+ }
+ }
+
+ private static class SmartReader {
+ PushbackReader reader = null;
+ int lastread = -1;
+
+ public SmartReader(Reader r) { reader = new PushbackReader(r); }
+ public void unread() throws IOException { reader.unread(lastread); }
+ public boolean match(char c) throws IOException { if (peek() == c) { reader.read(); return true; } else return false; }
+ public int peek() throws IOException {
+ int peeked = reader.read();
+ if (peeked != -1) reader.unread((char)peeked);
+ return peeked;
+ }
+ public int read() throws IOException {
+ lastread = reader.read();
+ if (accumulator != null) accumulator.append(lastread);
+ return lastread;
+ }
+
+ // FIXME: could be much more efficient
+ StringBuffer accumulator = null;
+ public void startString() { accumulator = new StringBuffer(); }
+ public String getString() throws IOException {
+ String ret = accumulator.toString();
+ accumulator = null;
+ return ret;
+ }
+ }
+
+}
--- /dev/null
+package org.xwt.js;
+import org.xwt.util.*;
+import java.io.*;
+
+// FIXME: for..in
+// FIXME: delete keyword
+public class Parser extends Lexer {
+
+ public Parser(Reader r) throws IOException { super(r); }
+ private Parser skipToken() throws IOException { getToken(); return this; }
+
+ /** sorta like gcc trees */
+ public static class Expr {
+ int code = -1;
+
+ Expr left = null;
+ Expr right = null;
+ Expr extra = null;
+
+ Expr next = null; // if this expr is part of a list
+
+ String string = null;
+
+ public Expr(String s) { this.string = s; } // an identifier or label
+ public Expr(int code) { this(code, null, null, null); }
+ public Expr(int code, Expr left) { this(code, left, null, null); }
+ public Expr(int code, Expr left, Expr right) { this(code, left, right, null); }
+ public Expr(int code, Expr left, Expr right, Expr extra) { this.left = left; this.right = right; this.extra = extra; this.code = code; }
+ }
+
+ /** parses a single statement */
+ public Expr parseStatement() throws IOException {
+ int tok;
+ Expr ret;
+ switch(tok = peekToken()) {
+
+ case LC:
+ ret = parseBlock(true);
+
+ case THROW: case RETURN: case ASSERT:
+ ret = new Expr(ASSERT, skipToken().parseExpr());
+
+ case GOTO: case BREAK: case CONTINUE:
+ skipToken();
+ if (getToken() == NAME)
+ ret = new Expr(tok, new Expr(string));
+ else if (tok == GOTO)
+ throw new Error("goto must be followed by a label");
+ else
+ ret = new Expr(tok);
+
+ default:
+ ret = parseExpr();
+ }
+
+ if (getToken() != SEMI) throw new Error("expected ;");
+ return ret;
+ }
+
+ /** a block is either a single statement or a list of statements surrounded by curly braces; all expressions are also statements */
+ public Expr parseBlock(boolean requireBraces) throws IOException {
+ int tok = peekToken();
+ if (requireBraces && tok != LC) throw new Error("expected {");
+ if (tok != LC) return parseStatement();
+ skipToken();
+ Expr head = null;
+ Expr tail = null;
+ while(peekToken() != RC)
+ if (head == null) head = tail = parseStatement(); else tail = tail.next = parseStatement();
+ skipToken();
+ return new Expr(LC, head);
+ }
+
+ /** Subexpressions come in two flavors: starters and continuers.
+ * Starters can appear at the start of an expression or after a
+ * continuer, and continuers, which can appear after a starter.
+ */
+ public Expr parseExpr() throws IOException {
+ Expr e = parseStarter();
+ while(true) {
+ Expr e2 = parseContinuer(e);
+ if (e2 == null) return e;
+ e = e2;
+ }
+ }
+
+ public Expr parseStarter() throws IOException {
+ Expr e1 = null;
+ Expr e2 = null;
+ Expr e3 = null;
+ Expr head = null;
+ Expr tail = null;
+ int tok = getToken();
+ switch(tok) {
+
+ case SWITCH: {
+ if (getToken() != LP) throw new Error("expected left paren");
+ Expr switchExpr = parseExpr();
+ if (getToken() != RP) throw new Error("expected left paren");
+ if (getToken() != LC) throw new Error("expected left brace");
+ Expr firstExpr = null;
+ Expr lastExpr = null;
+ while(true) {
+ if (getToken() != CASE) throw new Error("expected CASE");
+ Expr caseExpr = parseExpr();
+ if (getToken() != COLON) throw new Error("expected COLON");
+ Expr e = new Expr(CASE, caseExpr, parseBlock(false));
+ if (lastExpr == null) firstExpr = e;
+ else lastExpr.next = e;
+ lastExpr = e;
+ if (getToken() == RC) return new Expr(SWITCH, switchExpr, firstExpr);
+ }
+ }
+
+ case FUNCTION: {
+ if (getToken() != LP) throw new Error("function keyword must be followed by a left paren");
+ Expr formalArgs = null, cur = null;
+ tok = getToken();
+ while(tok != RP) {
+ if (tok != NAME) throw new Error("expected a variable name");
+ if (cur == null) { formalArgs = cur = new Expr(string); }
+ else { cur.next = new Expr(string); cur = cur.next; }
+ tok = getToken();
+ if (tok == RP) break;
+ if (tok != COMMA) throw new Error("function argument list must consist of alternating NAMEs and COMMAs");
+ tok = getToken();
+ }
+ return new Expr(tok, formalArgs, parseBlock(true));
+ }
+
+ case VAR:
+ while(true) {
+ if (getToken() != NAME) throw new Error("variable declarations must start with a variable name");
+ Expr name = new Expr(string);
+ Expr initVal = null;
+ tok = peekToken();
+ if (tok == ASSIGN) {
+ skipToken();
+ initVal = parseExpr();
+ tok = peekToken();
+ }
+ Expr e = new Expr(VAR, name, initVal);
+ if (head == null) head = tail = e; else tail = tail.next = e;
+ if (tok != COMMA) break;
+ skipToken();
+ }
+ return new Expr(VAR, head);
+
+ case LC:
+ tok = getToken();
+ while(true) {
+ if (tok == RP) return new Expr(LC, head);
+ if (tok != NAME) throw new Error("expecting name");
+ Expr name = parseExpr();
+ if (tok != COLON) throw new Error("expecting colon");
+ e1 = new Expr(COLON, name, parseExpr());
+ if (head == null) head = tail = e1; else tail = tail.next = e1;
+ tok = getToken();
+ if (tok != COMMA && tok != RP) throw new Error("expected right curly or comma");
+ }
+
+ case LB:
+ tok = getToken();
+ while(true) {
+ if (tok == RB) return new Expr(LB, head);
+ if (head == null) head = tail = parseExpr(); else tail = tail.next = parseExpr();
+ tok = getToken();
+ if (tok != COMMA && tok != RP) throw new Error("expected right bracket or comma");
+ }
+
+ case NAME:
+ return new Expr(string);
+
+ case INC: case DEC: case TYPEOF:
+ return new Expr(tok, parseExpr());
+
+ case TRUE: case FALSE: case NOP:
+ return new Expr(tok);
+
+ case TRY: {
+ // FIXME: we deliberately allow you to omit braces in catch{}/finally{} if they are single statements...
+ Expr tryBlock = parseBlock(true);
+ while ((tok = peekToken()) == CATCH)
+ if (head == null) head = tail = parseBlock(false); else tail = tail.next = parseBlock(false);
+ if (head == null) throw new Error("try without catch");
+ return new Expr(TRY, tryBlock, head, tok == FINALLY ? skipToken().parseBlock(false) : null);
+ }
+
+ case IF: case WHILE: {
+ if (getToken() != LP) throw new Error("expected left paren");
+ Expr parenExpr = parseExpr();
+ if (getToken() != RP) throw new Error("expected right paren");
+ Expr firstBlock = parseBlock(false);
+ if (tok == IF && peekToken() == ELSE) return new Expr(tok, parenExpr, firstBlock, skipToken().parseBlock(false));
+ return new Expr(tok, parenExpr, firstBlock);
+ }
+
+ case FOR:
+ // FIXME: for..in
+ if (getToken() != LP) throw new Error("expected left paren");
+ e1 = parseStatement();
+ e2 = parseStatement();
+ e3 = parseStatement(); // FIXME: this guy has to be okay with ending via a )
+ if (getToken() != RP) throw new Error("expected right paren");
+ throw new Error("not yet implemented");
+ //return new Expr(FOR, e1, e2, e3, parseBlock(false));
+
+ case DO: {
+ Expr firstBlock = parseBlock(false);
+ if (getToken() != WHILE) throw new Error("expecting WHILE");
+ if (getToken() != LP) throw new Error("expected left paren");
+ Expr whileExpr = parseExpr();
+ if (getToken() != RP) throw new Error("expected right paren");
+ if (getToken() != SEMI) throw new Error("semicolon");
+ return new Expr(DO, firstBlock, whileExpr);
+ }
+
+ case VOID: case RESERVED:
+ throw new Error("reserved word that you shouldn't be using");
+
+ case WITH:
+ throw new Error("WITH not yet implemented"); // FIXME
+
+ default: throw new Error("I wasn't expecting a " + tok);
+ }
+ }
+
+ // called after each parseExpr(); returns null if we can't make the expression any bigger
+ public Expr parseContinuer(Expr prefix) throws IOException {
+ Expr head = null;
+ Expr tail = null;
+ Expr e1, e2, e3;
+ Expr ret = null;
+ int tok;
+
+ // FIXME: postfix and infix operators -- need to handle precedence
+ switch (tok = getToken()) {
+
+ case BITOR: case BITXOR: case BITAND: case EQ: case NE: case LT: case LE:
+ case GT: case GE: case LSH: case RSH: case URSH: case ADD: case SUB: case MUL:
+ case DIV: case MOD: case BITNOT: case SHEQ: case SHNE: case INSTANCEOF:
+ case OR: case AND: case COMMA: case INC: case DEC:
+ throw new Error("haven't figured out how to handle postfix/infix operators yet");
+ //return new Expr(tok, prefix, (tok == INC || tok == DEC) ? null : parseExpr());
+
+ case ASSIGN:
+ throw new Error("haven't figured out how to handle postfix/infix operators yet");
+
+ case LP:
+ while(peekToken() != RP) {
+ if (head == null) head = tail = parseExpr(); else tail = tail.next = parseExpr();
+ tok = getToken();
+ if (tok == RP) break;
+ if (tok != COMMA) throw new Error("expected comma or right paren");
+ }
+ return new Expr(LP, prefix, head);
+
+ case LB:
+ e1 = parseExpr();
+ if (getToken() != RB) throw new Error("expected a right brace");
+ return new Expr(LB, prefix, e1);
+
+ case HOOK:
+ e2 = parseExpr();
+ if (getToken() != COLON) throw new Error("expected colon to close ?: expression");
+ e3 = parseExpr();
+ return new Expr(HOOK, prefix, e2, e3);
+
+ default:
+ pushBackToken();
+ return null;
+ }
+ }
+
+}
+