From 3bbc292e65e5874b1a7931a8c5ce9aaf697dc027 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Tue, 24 Sep 2024 13:14:23 +0200 Subject: [PATCH] Try to use enums as token --- .../java/org/mozilla/javascript/Icode.java | 6 +- .../java/org/mozilla/javascript/Node.java | 198 +++---- .../java/org/mozilla/javascript/Parser.java | 329 ++++++------ .../org/mozilla/javascript/ScriptRuntime.java | 24 +- .../java/org/mozilla/javascript/Token.java | 482 +++++++++--------- .../org/mozilla/javascript/TokenStream.java | 4 +- .../org/mozilla/javascript/ast/AstNode.java | 146 +++--- .../org/mozilla/javascript/ast/AstRoot.java | 2 +- .../java/org/mozilla/javascript/ast/Jump.java | 8 +- .../org/mozilla/javascript/ast/Symbol.java | 8 +- 10 files changed, 604 insertions(+), 603 deletions(-) diff --git a/rhino/src/main/java/org/mozilla/javascript/Icode.java b/rhino/src/main/java/org/mozilla/javascript/Icode.java index 046cb02367..71ecac3785 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Icode.java +++ b/rhino/src/main/java/org/mozilla/javascript/Icode.java @@ -146,7 +146,7 @@ abstract class Icode { // Last icode MIN_ICODE = -76; - static String bytecodeName(int bytecode) { + static String bytecodeName(Token bytecode) { if (!validBytecode(bytecode)) { throw new IllegalArgumentException(String.valueOf(bytecode)); } @@ -316,7 +316,7 @@ static String bytecodeName(int bytecode) { throw new IllegalStateException(String.valueOf(bytecode)); } - static boolean validIcode(int icode) { + static boolean validIcode(Token icode) { return MIN_ICODE <= icode && icode <= 0; } @@ -324,7 +324,7 @@ static boolean validTokenCode(int token) { return Token.FIRST_BYTECODE_TOKEN <= token && token <= Token.LAST_BYTECODE_TOKEN; } - static boolean validBytecode(int bytecode) { + static boolean validBytecode(Token bytecode) { return validIcode(bytecode) || validTokenCode(bytecode); } } diff --git a/rhino/src/main/java/org/mozilla/javascript/Node.java b/rhino/src/main/java/org/mozilla/javascript/Node.java index 47cee265af..de235cbfdc 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Node.java +++ b/rhino/src/main/java/org/mozilla/javascript/Node.java @@ -90,17 +90,17 @@ private static class PropListItem { Object objectValue; } - public Node(int nodeType) { + public Node(Token nodeType) { type = nodeType; } - public Node(int nodeType, Node child) { + public Node(Token nodeType, Node child) { type = nodeType; first = last = child; child.next = null; } - public Node(int nodeType, Node left, Node right) { + public Node(Token nodeType, Node left, Node right) { type = nodeType; first = left; last = right; @@ -108,7 +108,7 @@ public Node(int nodeType, Node left, Node right) { right.next = null; } - public Node(int nodeType, Node left, Node mid, Node right) { + public Node(Token nodeType, Node left, Node mid, Node right) { type = nodeType; first = left; last = right; @@ -117,22 +117,22 @@ public Node(int nodeType, Node left, Node mid, Node right) { right.next = null; } - public Node(int nodeType, int line) { + public Node(Token nodeType, int line) { type = nodeType; lineno = line; } - public Node(int nodeType, Node child, int line) { + public Node(Token nodeType, Node child, int line) { this(nodeType, child); lineno = line; } - public Node(int nodeType, Node left, Node right, int line) { + public Node(Token nodeType, Node left, Node right, int line) { this(nodeType, left, right); lineno = line; } - public Node(int nodeType, Node left, Node mid, Node right, int line) { + public Node(Token nodeType, Node left, Node mid, Node right, int line) { this(nodeType, left, mid, right); lineno = line; } @@ -147,19 +147,19 @@ public static Node newString(String str) { return newString(Token.STRING, str); } - public static Node newString(int type, String str) { + public static Node newString(Token type, String str) { Name name = new Name(); name.setIdentifier(str); name.setType(type); return name; } - public int getType() { + public Token getType() { return type; } /** Sets the node type and returns this node. */ - public Node setType(int type) { + public Node setType(Token type) { this.type = type; return this; } @@ -855,48 +855,48 @@ private int endCheckBreak() { */ private int endCheck() { switch (type) { - case Token.BREAK: + case BREAK: return endCheckBreak(); - case Token.EXPR_VOID: + case EXPR_VOID: if (this.first != null) return first.endCheck(); return END_DROPS_OFF; - case Token.YIELD: - case Token.YIELD_STAR: + case YIELD: + case YIELD_STAR: return END_YIELDS; - case Token.CONTINUE: - case Token.THROW: + case CONTINUE: + case THROW: return END_UNREACHED; - case Token.RETURN: + case RETURN: if (this.first != null) return END_RETURNS_VALUE; return END_RETURNS; - case Token.TARGET: + case TARGET: if (next != null) return next.endCheck(); return END_DROPS_OFF; - case Token.LOOP: + case LOOP: return endCheckLoop(); - case Token.LOCAL_BLOCK: - case Token.BLOCK: + case LOCAL_BLOCK: + case BLOCK: // there are several special kinds of blocks if (first == null) return END_DROPS_OFF; switch (first.type) { - case Token.LABEL: + case LABEL: return first.endCheckLabel(); - case Token.IFNE: + case IFNE: return first.endCheckIf(); - case Token.SWITCH: + case SWITCH: return first.endCheckSwitch(); - case Token.TRY: + case TRY: return first.endCheckTry(); default: @@ -910,87 +910,87 @@ private int endCheck() { public boolean hasSideEffects() { switch (type) { - case Token.EXPR_VOID: - case Token.COMMA: + case EXPR_VOID: + case COMMA: if (last != null) return last.hasSideEffects(); return true; - case Token.HOOK: + case HOOK: if (first == null || first.next == null || first.next.next == null) Kit.codeBug(); return first.next.hasSideEffects() && first.next.next.hasSideEffects(); - case Token.AND: - case Token.OR: + case AND: + case OR: if (first == null || last == null) Kit.codeBug(); return first.hasSideEffects() || last.hasSideEffects(); - case Token.ERROR: // Avoid cascaded error messages - case Token.EXPR_RESULT: - case Token.ASSIGN: - case Token.ASSIGN_ADD: - case Token.ASSIGN_SUB: - case Token.ASSIGN_MUL: - case Token.ASSIGN_DIV: - case Token.ASSIGN_MOD: - case Token.ASSIGN_BITOR: - case Token.ASSIGN_LOGICAL_OR: - case Token.ASSIGN_BITXOR: - case Token.ASSIGN_BITAND: - case Token.ASSIGN_LOGICAL_AND: - case Token.ASSIGN_LSH: - case Token.ASSIGN_RSH: - case Token.ASSIGN_URSH: - case Token.ENTERWITH: - case Token.LEAVEWITH: - case Token.RETURN: - case Token.GOTO: - case Token.IFEQ: - case Token.IFNE: - case Token.NEW: - case Token.DELPROP: - case Token.SETNAME: - case Token.SETPROP: - case Token.SETELEM: - case Token.CALL: - case Token.THROW: - case Token.RETHROW: - case Token.SETVAR: - case Token.CATCH_SCOPE: - case Token.RETURN_RESULT: - case Token.SET_REF: - case Token.DEL_REF: - case Token.REF_CALL: - case Token.TRY: - case Token.SEMI: - case Token.INC: - case Token.DEC: - case Token.IF: - case Token.ELSE: - case Token.SWITCH: - case Token.WHILE: - case Token.DO: - case Token.FOR: - case Token.BREAK: - case Token.CONTINUE: - case Token.VAR: - case Token.CONST: - case Token.LET: - case Token.LETEXPR: - case Token.WITH: - case Token.WITHEXPR: - case Token.CATCH: - case Token.FINALLY: - case Token.BLOCK: - case Token.LABEL: - case Token.TARGET: - case Token.LOOP: - case Token.JSR: - case Token.SETPROP_OP: - case Token.SETELEM_OP: - case Token.LOCAL_BLOCK: - case Token.SET_REF_OP: - case Token.YIELD: - case Token.YIELD_STAR: + case ERROR: // Avoid cascaded error messages + case EXPR_RESULT: + case ASSIGN: + case ASSIGN_ADD: + case ASSIGN_SUB: + case ASSIGN_MUL: + case ASSIGN_DIV: + case ASSIGN_MOD: + case ASSIGN_BITOR: + case ASSIGN_LOGICAL_OR: + case ASSIGN_BITXOR: + case ASSIGN_BITAND: + case ASSIGN_LOGICAL_AND: + case ASSIGN_LSH: + case ASSIGN_RSH: + case ASSIGN_URSH: + case ENTERWITH: + case LEAVEWITH: + case RETURN: + case GOTO: + case IFEQ: + case IFNE: + case NEW: + case DELPROP: + case SETNAME: + case SETPROP: + case SETELEM: + case CALL: + case THROW: + case RETHROW: + case SETVAR: + case CATCH_SCOPE: + case RETURN_RESULT: + case SET_REF: + case DEL_REF: + case REF_CALL: + case TRY: + case SEMI: + case INC: + case DEC: + case IF: + case ELSE: + case SWITCH: + case WHILE: + case DO: + case FOR: + case BREAK: + case CONTINUE: + case VAR: + case CONST: + case LET: + case LETEXPR: + case WITH: + case WITHEXPR: + case CATCH: + case FINALLY: + case BLOCK: + case LABEL: + case TARGET: + case LOOP: + case JSR: + case SETPROP_OP: + case SETELEM_OP: + case LOCAL_BLOCK: + case SET_REF_OP: + case YIELD: + case YIELD_STAR: return true; default: @@ -1247,7 +1247,7 @@ private static void appendPrintId(Node n, Map printIds, StringBui } } - protected int type = Token.ERROR; // type of the node, e.g. Token.NAME + protected Token type = Token.ERROR; // type of the node, e.g. Token.NAME protected Node next; // next sibling protected Node first; // first element of a linked list of children protected Node last; // last element of a linked list of children diff --git a/rhino/src/main/java/org/mozilla/javascript/Parser.java b/rhino/src/main/java/org/mozilla/javascript/Parser.java index aa87884228..0acd404248 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Parser.java +++ b/rhino/src/main/java/org/mozilla/javascript/Parser.java @@ -125,8 +125,8 @@ public class Parser { private TokenStream ts; CurrentPositionReporter currentPos; - private int currentFlaggedToken = Token.EOF; - private int currentToken; + private Token currentFlaggedToken = Token.EOF; + private Token currentToken; private int syntaxErrorCount; private List scannedComments; @@ -369,7 +369,7 @@ private Comment getAndResetJsDoc() { // // Note that this function always returned the un-flagged token! // The flags, if any, are saved in currentFlaggedToken. - private int peekToken() throws IOException { + private Token peekToken() throws IOException { // By far the most common case: last token hasn't been consumed, // so return already-peeked token. if (currentFlaggedToken != Token.EOF) { @@ -377,7 +377,7 @@ private int peekToken() throws IOException { } int lineno = ts.getLineno(); - int tt = ts.getToken(); + Token tt = ts.getToken(); boolean sawEOL = false; // process comments and whitespace @@ -401,7 +401,7 @@ private int peekToken() throws IOException { return currentToken; // return unflagged token } - private int peekFlaggedToken() throws IOException { + private Token peekFlaggedToken() throws IOException { peekToken(); return currentFlaggedToken; } @@ -410,14 +410,14 @@ private void consumeToken() { currentFlaggedToken = Token.EOF; } - private int nextToken() throws IOException { - int tt = peekToken(); + private Token nextToken() throws IOException { + Token tt = peekToken(); consumeToken(); return tt; } - private boolean matchToken(int toMatch, boolean ignoreComment) throws IOException { - int tt = peekToken(); + private boolean matchToken(Token toMatch, boolean ignoreComment) throws IOException { + Token tt = peekToken(); while (tt == Token.COMMENT && ignoreComment) { consumeToken(); tt = peekToken(); @@ -434,8 +434,8 @@ private boolean matchToken(int toMatch, boolean ignoreComment) throws IOExceptio // token types valid if they are preceded by a newline. One example is the // postfix ++ or -- operator, which has to be on the same line as its // operand. - private int peekTokenOrEOL() throws IOException { - int tt = peekToken(); + private Token peekTokenOrEOL() throws IOException { + Token tt = peekToken(); // Check for last peeked token flags if ((currentFlaggedToken & TI_AFTER_EOL) != 0) { tt = Token.EOL; @@ -443,14 +443,15 @@ private int peekTokenOrEOL() throws IOException { return tt; } - private boolean mustMatchToken(int toMatch, String messageId, boolean ignoreComment) + private boolean mustMatchToken(Token toMatch, String messageId, boolean ignoreComment) throws IOException { return mustMatchToken( toMatch, messageId, ts.tokenBeg, ts.tokenEnd - ts.tokenBeg, ignoreComment); } private boolean mustMatchToken( - int toMatch, String msgId, int pos, int len, boolean ignoreComment) throws IOException { + Token toMatch, String msgId, int pos, int len, boolean ignoreComment) + throws IOException { if (matchToken(toMatch, ignoreComment)) { return true; } @@ -592,8 +593,8 @@ private AstRoot parse() throws IOException { try { for (; ; ) { - int tt = peekToken(); - if (tt <= Token.EOF) { + Token tt = peekToken(); + if (tt.ordinal() <= Token.EOF.ordinal()) { break; } @@ -698,17 +699,17 @@ private AstNode parseFunctionBody(int type, FunctionNode fnNode) throws IOExcept bodyLoop: for (; ; ) { AstNode n; - int tt = peekToken(); + Token tt = peekToken(); switch (tt) { - case Token.ERROR: - case Token.EOF: - case Token.RC: + case ERROR: + case EOF: + case RC: break bodyLoop; - case Token.COMMENT: + case COMMENT: consumeToken(); n = scannedComments.get(scannedComments.size() - 1); break; - case Token.FUNCTION: + case FUNCTION: consumeToken(); n = function(FunctionNode.FUNCTION_STATEMENT); break; @@ -770,7 +771,7 @@ private void parseFunctionParams(FunctionNode fnNode) throws IOException { Set paramNames = new HashSet<>(); do { - int tt = peekToken(); + Token tt = peekToken(); if (tt == Token.RP) { if (fnNode.hasRestParameter()) { // Error: parameter after rest parameter @@ -1243,10 +1244,10 @@ private AstNode statement() throws IOException { int tt = peekTokenOrEOL(); consumeToken(); switch (tt) { - case Token.ERROR: - case Token.EOF: - case Token.EOL: - case Token.SEMI: + case ERROR: + case EOF: + case EOL: + case SEMI: break guessingStatementEnd; } } @@ -1264,93 +1265,93 @@ private AstNode statementHelper() throws IOException { int tt = peekToken(), pos = ts.tokenBeg; switch (tt) { - case Token.IF: + case IF: return ifStatement(); - case Token.SWITCH: + case SWITCH: return switchStatement(); - case Token.WHILE: + case WHILE: return whileLoop(); - case Token.DO: + case DO: return doLoop(); - case Token.FOR: + case FOR: return forLoop(); - case Token.TRY: + case TRY: return tryStatement(); - case Token.THROW: + case THROW: pn = throwStatement(); break; - case Token.BREAK: + case BREAK: pn = breakStatement(); break; - case Token.CONTINUE: + case CONTINUE: pn = continueStatement(); break; - case Token.WITH: + case WITH: if (this.inUseStrictDirective) { reportError("msg.no.with.strict"); } return withStatement(); - case Token.CONST: - case Token.VAR: + case CONST: + case VAR: consumeToken(); int lineno = ts.lineno; pn = variables(currentToken, ts.tokenBeg, true); pn.setLineno(lineno); break; - case Token.LET: + case LET: pn = letStatement(); if (pn instanceof VariableDeclaration && peekToken() == Token.SEMI) break; return pn; - case Token.RETURN: - case Token.YIELD: + case RETURN: + case YIELD: pn = returnOrYield(tt, false); break; - case Token.DEBUGGER: + case DEBUGGER: consumeToken(); pn = new KeywordLiteral(ts.tokenBeg, ts.tokenEnd - ts.tokenBeg, tt); pn.setLineno(ts.lineno); break; - case Token.LC: + case LC: return block(); - case Token.ERROR: + case ERROR: consumeToken(); return makeErrorNode(); - case Token.SEMI: + case SEMI: consumeToken(); pos = ts.tokenBeg; pn = new EmptyStatement(pos, ts.tokenEnd - pos); pn.setLineno(ts.lineno); return pn; - case Token.FUNCTION: + case FUNCTION: consumeToken(); return function(FunctionNode.FUNCTION_EXPRESSION_STATEMENT); - case Token.DEFAULT: + case DEFAULT: pn = defaultXmlNamespace(); break; - case Token.NAME: + case NAME: pn = nameOrLabel(); if (pn instanceof ExpressionStatement) break; return pn; // LabeledStatement - case Token.COMMENT: + case COMMENT: // Do not consume token here pn = scannedComments.get(scannedComments.size() - 1); return pn; @@ -1369,15 +1370,15 @@ private void autoInsertSemicolon(AstNode pn) throws IOException { int ttFlagged = peekFlaggedToken(); int pos = pn.getPosition(); switch (ttFlagged & CLEAR_TI_MASK) { - case Token.SEMI: + case SEMI: // Consume ';' as a part of expression consumeToken(); // extend the node bounds to include the semicolon. pn.setLength(ts.tokenEnd - pos); break; - case Token.ERROR: - case Token.EOF: - case Token.RC: + case ERROR: + case EOF: + case RC: // Autoinsert ; // Token.EOF can have negative length and negative nodeEnd(pn). // So, make the end position at least pos+1. @@ -1449,23 +1450,23 @@ private SwitchStatement switchStatement() throws IOException { int caseLineno = ts.lineno; AstNode caseExpression = null; switch (tt) { - case Token.RC: + case RC: pn.setLength(ts.tokenEnd - pos); break switchLoop; - case Token.CASE: + case CASE: caseExpression = expr(false); mustMatchToken(Token.COLON, "msg.no.colon.case", true); break; - case Token.DEFAULT: + case DEFAULT: if (hasDefault) { reportError("msg.double.switch.default"); } hasDefault = true; mustMatchToken(Token.COLON, "msg.no.colon.case", true); break; - case Token.COMMENT: + case COMMENT: AstNode n = scannedComments.get(scannedComments.size() - 1); pn.addChild(n); continue switchLoop; @@ -1757,7 +1758,7 @@ private TryStatement tryStatement() throws IOException { AstNode catchCond = null; switch (peekToken()) { - case Token.LP: + case LP: { matchToken(Token.LP, true); lp = ts.tokenBeg; @@ -1789,7 +1790,7 @@ private TryStatement tryStatement() throws IOException { mustMatchToken(Token.LC, "msg.no.brace.catchblock", true); } break; - case Token.LC: + case LC: if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6) { matchToken(Token.LC, true); } else { @@ -2023,15 +2024,15 @@ private AstNode returnOrYield(int tt, boolean exprContext) throws IOException { AstNode e = null; // This is ugly, but we don't want to require a semicolon. switch (peekTokenOrEOL()) { - case Token.SEMI: - case Token.RC: - case Token.RB: - case Token.RP: - case Token.EOF: - case Token.EOL: - case Token.ERROR: + case SEMI: + case RC: + case RB: + case RP: + case EOF: + case EOL: + case ERROR: break; - case Token.YIELD: + case YIELD: if (compilerEnv.getLanguageVersion() < Context.VERSION_ES6) { // Take extra care to preserve language compatibility break; @@ -2334,11 +2335,11 @@ private AstNode let(boolean isStatement, int pos) throws IOException { return pn; } - void defineSymbol(int declType, String name) { + void defineSymbol(Token declType, String name) { defineSymbol(declType, name, false); } - void defineSymbol(int declType, String name, boolean ignoreNotInBlock) { + void defineSymbol(Token declType, String name, boolean ignoreNotInBlock) { if (name == null) { if (compilerEnv.isIdeMode()) { // be robust in IDE-mode return; @@ -2347,7 +2348,7 @@ void defineSymbol(int declType, String name, boolean ignoreNotInBlock) { } Scope definingScope = currentScope.getDefiningScope(name); Symbol symbol = definingScope != null ? definingScope.getSymbol(name) : null; - int symDeclType = symbol != null ? symbol.getDeclType() : -1; + Token symDeclType = symbol != null ? symbol.getDeclType() : -1; if (symbol != null && (symDeclType == Token.CONST || declType == Token.CONST @@ -2366,7 +2367,7 @@ void defineSymbol(int declType, String name, boolean ignoreNotInBlock) { return; } switch (declType) { - case Token.LET: + case LET: if (!ignoreNotInBlock && ((currentScope.getType() == Token.IF) || currentScope instanceof Loop)) { addError("msg.let.decl.not.in.block"); @@ -2375,9 +2376,9 @@ void defineSymbol(int declType, String name, boolean ignoreNotInBlock) { currentScope.putSymbol(new Symbol(declType, name)); return; - case Token.VAR: - case Token.CONST: - case Token.FUNCTION: + case VAR: + case CONST: + case FUNCTION: if (symbol != null) { if (symDeclType == Token.VAR) addStrictWarning("msg.var.redecl", name); else if (symDeclType == Token.LP) { @@ -2388,7 +2389,7 @@ else if (symDeclType == Token.LP) { } return; - case Token.LP: + case LP: if (symbol != null) { // must be duplicate parameter. Second parameter hides the // first, so go ahead and add the second parameter @@ -2563,10 +2564,10 @@ private AstNode eqExpr() throws IOException { for (; ; ) { int tt = peekToken(), opPos = ts.tokenBeg; switch (tt) { - case Token.EQ: - case Token.NE: - case Token.SHEQ: - case Token.SHNE: + case EQ: + case NE: + case SHEQ: + case SHNE: consumeToken(); int parseToken = tt; if (compilerEnv.getLanguageVersion() == Context.VERSION_1_2) { @@ -2587,14 +2588,14 @@ private AstNode relExpr() throws IOException { for (; ; ) { int tt = peekToken(), opPos = ts.tokenBeg; switch (tt) { - case Token.IN: + case IN: if (inForInit) break; // fall through - case Token.INSTANCEOF: - case Token.LE: - case Token.LT: - case Token.GE: - case Token.GT: + case INSTANCEOF: + case LE: + case LT: + case GE: + case GT: consumeToken(); pn = new InfixExpression(tt, pn, shiftExpr(), opPos); continue; @@ -2609,9 +2610,9 @@ private AstNode shiftExpr() throws IOException { for (; ; ) { int tt = peekToken(), opPos = ts.tokenBeg; switch (tt) { - case Token.LSH: - case Token.URSH: - case Token.RSH: + case LSH: + case URSH: + case RSH: consumeToken(); pn = new InfixExpression(tt, pn, addExpr(), opPos); continue; @@ -2640,9 +2641,9 @@ private AstNode mulExpr() throws IOException { for (; ; ) { int tt = peekToken(), opPos = ts.tokenBeg; switch (tt) { - case Token.MUL: - case Token.DIV: - case Token.MOD: + case MUL: + case DIV: + case MOD: consumeToken(); pn = new InfixExpression(tt, pn, expExpr(), opPos); continue; @@ -2657,7 +2658,7 @@ private AstNode expExpr() throws IOException { for (; ; ) { int tt = peekToken(), opPos = ts.tokenBeg; switch (tt) { - case Token.EXP: + case EXP: if (pn instanceof UnaryExpression) { reportError( "msg.no.unary.expr.on.left.exp", @@ -2683,47 +2684,47 @@ private AstNode unaryExpr() throws IOException { int line = ts.lineno; switch (tt) { - case Token.VOID: - case Token.NOT: - case Token.BITNOT: - case Token.TYPEOF: + case VOID: + case NOT: + case BITNOT: + case TYPEOF: consumeToken(); node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr()); node.setLineno(line); return node; - case Token.ADD: + case ADD: consumeToken(); // Convert to special POS token in parse tree node = new UnaryExpression(Token.POS, ts.tokenBeg, unaryExpr()); node.setLineno(line); return node; - case Token.SUB: + case SUB: consumeToken(); // Convert to special NEG token in parse tree node = new UnaryExpression(Token.NEG, ts.tokenBeg, unaryExpr()); node.setLineno(line); return node; - case Token.INC: - case Token.DEC: + case INC: + case DEC: consumeToken(); UpdateExpression expr = new UpdateExpression(tt, ts.tokenBeg, memberExpr(true)); expr.setLineno(line); checkBadIncDec(expr); return expr; - case Token.DELPROP: + case DELPROP: consumeToken(); node = new UnaryExpression(tt, ts.tokenBeg, unaryExpr()); node.setLineno(line); return node; - case Token.ERROR: + case ERROR: consumeToken(); return makeErrorNode(); - case Token.LT: + case LT: // XML stream encountered in expression. if (compilerEnv.isXmlAvailable()) { consumeToken(); @@ -2760,7 +2761,7 @@ private AstNode xmlInitializer() throws IOException { for (; ; tt = ts.getNextXMLToken()) { switch (tt) { - case Token.XML: + case XML: pn.addFragment(new XmlString(ts.tokenBeg, ts.getString())); mustMatchToken(Token.LC, "msg.syntax", true); int beg = ts.tokenBeg; @@ -2775,7 +2776,7 @@ private AstNode xmlInitializer() throws IOException { pn.addFragment(xexpr); break; - case Token.XMLEND: + case XMLEND: pn.addFragment(new XmlString(ts.tokenBeg, ts.getString())); return pn; @@ -2886,14 +2887,14 @@ private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) throws IOExc for (; ; ) { int tt = peekToken(); switch (tt) { - case Token.DOT: - case Token.DOTDOT: + case DOT: + case DOTDOT: lineno = ts.lineno; pn = propertyAccess(tt, pn); pn.setLineno(lineno); break; - case Token.DOTQUERY: + case DOTQUERY: consumeToken(); int opPos = ts.tokenBeg, rp = -1; lineno = ts.lineno; @@ -2914,7 +2915,7 @@ private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) throws IOExc pn = q; break; - case Token.LB: + case LB: consumeToken(); int lb = ts.tokenBeg, rb = -1; lineno = ts.lineno; @@ -2932,7 +2933,7 @@ private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) throws IOExc pn = g; break; - case Token.LP: + case LP: if (!allowCallSyntax) { break tailLoop; } @@ -2953,7 +2954,7 @@ private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) throws IOExc f.setLength(ts.tokenEnd - pos); pn = f; break; - case Token.COMMENT: + case COMMENT: // Ignoring all the comments, because previous statement may not be terminated // properly. int currentFlagTOken = currentFlaggedToken; @@ -2963,7 +2964,7 @@ private AstNode memberExprTail(boolean allowCallSyntax, AstNode pn) throws IOExc ? currentFlaggedToken : currentFlagTOken; break; - case Token.TEMPLATE_LITERAL: + case TEMPLATE_LITERAL: consumeToken(); pn = taggedTemplateLiteral(pn); break; @@ -3019,30 +3020,30 @@ private AstNode propertyAccess(int tt, AstNode pn) throws IOException { int token = nextToken(); switch (token) { - case Token.THROW: + case THROW: // needed for generator.throw(); saveNameTokenData(ts.tokenBeg, "throw", ts.lineno); ref = propertyName(-1, memberTypeFlags); break; - case Token.NAME: + case NAME: // handles: name, ns::name, ns::*, ns::[expr] ref = propertyName(-1, memberTypeFlags); break; - case Token.MUL: + case MUL: // handles: *, *::name, *::*, *::[expr] saveNameTokenData(ts.tokenBeg, "*", ts.lineno); ref = propertyName(-1, memberTypeFlags); break; - case Token.XMLATTR: + case XMLATTR: // handles: '@attr', '@ns::attr', '@ns::*', '@ns::*', // '@::attr', '@::*', '@*', '@*::attr', '@*::*' ref = attributeAccess(); break; - case Token.RESERVED: + case RESERVED: { String name = ts.getString(); saveNameTokenData(ts.tokenBeg, name, ts.lineno); @@ -3090,16 +3091,16 @@ private AstNode attributeAccess() throws IOException { switch (tt) { // handles: @name, @ns::name, @ns::*, @ns::[expr] - case Token.NAME: + case NAME: return propertyName(atPos, 0); // handles: @*, @*::name, @*::*, @*::[expr] - case Token.MUL: + case MUL: saveNameTokenData(ts.tokenBeg, "*", ts.lineno); return propertyName(atPos, 0); // handles @[expr] - case Token.LB: + case LB: return xmlElemRef(atPos, null, -1); default: @@ -3129,18 +3130,18 @@ private AstNode propertyName(int atPos, int memberTypeFlags) throws IOException switch (nextToken()) { // handles name::name - case Token.NAME: + case NAME: name = createNameNode(); break; // handles name::* - case Token.MUL: + case MUL: saveNameTokenData(ts.tokenBeg, "*", ts.lineno); name = createNameNode(false, -1); break; // handles name::[expr] or *::[expr] - case Token.LB: + case LB: return xmlElemRef(atPos, ns, colonPos); default: @@ -3206,48 +3207,48 @@ private AstNode primaryExpr() throws IOException { int tt = ttFlagged & CLEAR_TI_MASK; switch (tt) { - case Token.FUNCTION: + case FUNCTION: consumeToken(); return function(FunctionNode.FUNCTION_EXPRESSION); - case Token.LB: + case LB: consumeToken(); return arrayLiteral(); - case Token.LC: + case LC: consumeToken(); return objectLiteral(); - case Token.LET: + case LET: consumeToken(); return let(false, ts.tokenBeg); - case Token.LP: + case LP: consumeToken(); return parenExpr(); - case Token.XMLATTR: + case XMLATTR: consumeToken(); mustHaveXML(); return attributeAccess(); - case Token.NAME: + case NAME: consumeToken(); return name(ttFlagged, tt); - case Token.NUMBER: - case Token.BIGINT: + case NUMBER: + case BIGINT: { consumeToken(); return createNumericLiteral(tt, false); } - case Token.STRING: + case STRING: consumeToken(); return createStringLiteral(); - case Token.DIV: - case Token.ASSIGN_DIV: + case DIV: + case ASSIGN_DIV: consumeToken(); // Got / or /= which in this context means a regexp ts.readRegExp(tt); @@ -3257,30 +3258,30 @@ private AstNode primaryExpr() throws IOException { re.setFlags(ts.readAndClearRegExpFlags()); return re; - case Token.NULL: - case Token.THIS: - case Token.FALSE: - case Token.TRUE: + case NULL: + case THIS: + case FALSE: + case TRUE: consumeToken(); pos = ts.tokenBeg; end = ts.tokenEnd; return new KeywordLiteral(pos, end - pos, tt); - case Token.TEMPLATE_LITERAL: + case TEMPLATE_LITERAL: consumeToken(); return templateLiteral(false); - case Token.RESERVED: + case RESERVED: consumeToken(); reportError("msg.reserved.id", ts.getString()); break; - case Token.ERROR: + case ERROR: consumeToken(); // the scanner or one of its subroutines reported the error. break; - case Token.EOF: + case EOF: consumeToken(); reportError("msg.unexpected.eof"); break; @@ -3467,13 +3468,13 @@ private ArrayComprehensionLoop arrayComprehensionLoop() throws IOException { AstNode iter = null; switch (peekToken()) { - case Token.LB: - case Token.LC: + case LB: + case LC: // handle destructuring assignment iter = destructuringPrimaryExpr(); markDestructuring(iter); break; - case Token.NAME: + case NAME: consumeToken(); iter = createNameNode(); break; @@ -3488,10 +3489,10 @@ private ArrayComprehensionLoop arrayComprehensionLoop() throws IOException { } switch (nextToken()) { - case Token.IN: + case IN: inPos = ts.tokenBeg - pos; break; - case Token.NAME: + case NAME: if ("of".equals(ts.getString())) { if (eachPos != -1) { reportError("msg.invalid.for.each"); @@ -3568,13 +3569,13 @@ private GeneratorExpressionLoop generatorExpressionLoop() throws IOException { AstNode iter = null; switch (peekToken()) { - case Token.LB: - case Token.LC: + case LB: + case LC: // handle destructuring assignment iter = destructuringPrimaryExpr(); markDestructuring(iter); break; - case Token.NAME: + case NAME: consumeToken(); iter = createNameNode(); break; @@ -3750,20 +3751,20 @@ private AstNode objliteralProperty() throws IOException { AstNode pname; int tt = peekToken(); switch (tt) { - case Token.NAME: + case NAME: pname = createNameNode(); break; - case Token.STRING: + case STRING: pname = createStringLiteral(); break; - case Token.NUMBER: - case Token.BIGINT: + case NUMBER: + case BIGINT: pname = createNumericLiteral(tt, true); break; - case Token.LB: + case LB: if (compilerEnv.getLanguageVersion() >= Context.VERSION_ES6) { int pos = ts.tokenBeg; int lineno = ts.lineno; @@ -3914,7 +3915,7 @@ private AstNode templateLiteral(boolean isTaggedLiteral) throws IOException { TemplateLiteral pn = new TemplateLiteral(pos); int posChars = ts.tokenBeg + 1; - int tt = ts.readTemplateLiteral(isTaggedLiteral); + Token tt = ts.readTemplateLiteral(isTaggedLiteral); while (tt == Token.TEMPLATE_LITERAL_SUBST) { elements.add(createTemplateLiteralCharacters(posChars)); elements.add(expr(false)); @@ -4221,9 +4222,9 @@ Node destructuringAssignmentHelper( transformer); } else if (left.getType() == Token.GETPROP || left.getType() == Token.GETELEM) { switch (variableType) { - case Token.CONST: - case Token.LET: - case Token.VAR: + case CONST: + case LET: + case VAR: reportError("msg.bad.assign.left"); } comma.addChildToBack(simpleAssignment(left, createName(tempName), transformer)); @@ -4562,7 +4563,7 @@ protected Node simpleAssignment(Node left, Node right) { protected Node simpleAssignment(Node left, Node right, Transformer transformer) { int nodeType = left.getType(); switch (nodeType) { - case Token.NAME: + case NAME: String name = ((Name) left).getIdentifier(); if (inUseStrictDirective && ("eval".equals(name) || "arguments".equals(name))) { reportError("msg.bad.id.strict", name); @@ -4570,8 +4571,8 @@ protected Node simpleAssignment(Node left, Node right, Transformer transformer) left.setType(Token.BINDNAME); return new Node(Token.SETNAME, left, right); - case Token.GETPROP: - case Token.GETELEM: + case GETPROP: + case GETELEM: { Node obj, id; // If it's a PropertyGet or ElementGet, we're in the parse pass. @@ -4608,7 +4609,7 @@ protected Node simpleAssignment(Node left, Node right, Transformer transformer) } return new Node(type, obj, id, right); } - case Token.GET_REF: + case GET_REF: { Node ref = left.getFirstChild(); checkMutableReference(ref); diff --git a/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java b/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java index d7baba50ff..b40fdabc1d 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java +++ b/rhino/src/main/java/org/mozilla/javascript/ScriptRuntime.java @@ -3902,7 +3902,7 @@ public static boolean in(Object a, Object b, Context cx) { return hasObjectElem((Scriptable) b, a, cx); } - public static boolean compare(Object val1, Object val2, int op) { + public static boolean compare(Object val1, Object val2, Token op) { assert op == Token.GE || op == Token.LE || op == Token.GT || op == Token.LT; if (val1 instanceof Number && val2 instanceof Number) { @@ -3924,7 +3924,7 @@ public static boolean compare(Object val1, Object val2, int op) { } } - public static boolean compare(Number val1, Number val2, int op) { + public static boolean compare(Number val1, Number val2, Token op) { assert op == Token.GE || op == Token.LE || op == Token.GT || op == Token.LT; if (val1 instanceof BigInteger && val2 instanceof BigInteger) { @@ -3965,30 +3965,30 @@ public static boolean compare(Number val1, Number val2, int op) { return compareTo(val1.doubleValue(), val2.doubleValue(), op); } - private static boolean compareTo(Comparable val1, T val2, int op) { + private static boolean compareTo(Comparable val1, T val2, Token op) { switch (op) { - case Token.GE: + case GE: return val1.compareTo(val2) >= 0; - case Token.LE: + case LE: return val1.compareTo(val2) <= 0; - case Token.GT: + case GT: return val1.compareTo(val2) > 0; - case Token.LT: + case LT: return val1.compareTo(val2) < 0; default: throw Kit.codeBug(); } } - private static boolean compareTo(double d1, double d2, int op) { + private static boolean compareTo(double d1, double d2, Token op) { switch (op) { - case Token.GE: + case GE: return d1 >= d2; - case Token.LE: + case LE: return d1 <= d2; - case Token.GT: + case GT: return d1 > d2; - case Token.LT: + case LT: return d1 < d2; default: throw Kit.codeBug(); diff --git a/rhino/src/main/java/org/mozilla/javascript/Token.java b/rhino/src/main/java/org/mozilla/javascript/Token.java index b8ed54fb0f..d85b6f6096 100644 --- a/rhino/src/main/java/org/mozilla/javascript/Token.java +++ b/rhino/src/main/java/org/mozilla/javascript/Token.java @@ -15,7 +15,206 @@ * @author Mike McCabe * @author Brendan Eich */ -public class Token { +public enum Token { + ERROR, // well-known as the only code < EOF + EOF, // end of file token - (not EOF_CHAR) + EOL, // end of line + // Interpreter reuses the following as bytecodes + + ENTERWITH, + LEAVEWITH, + RETURN, + GOTO, + IFEQ, + IFNE, + SETNAME, + BITOR, + BITXOR, + BITAND, + EQ, + NE, + LT, + LE, + GT, + GE, + LSH, + RSH, + URSH, + ADD, + SUB, + MUL, + DIV, + MOD, + NOT, + BITNOT, + POS, + NEG, + NEW, + DELPROP, + TYPEOF, + GETPROP, + GETPROPNOWARN, + SETPROP, + GETELEM, + SETELEM, + CALL, + NAME, + NUMBER, + STRING, + NULL, + THIS, + FALSE, + TRUE, + SHEQ, // shallow equality (===) + SHNE, // shallow inequality (!==) + REGEXP, + BINDNAME, + THROW, + RETHROW, // rethrow caught exception: catch (e if ) use it + IN, + INSTANCEOF, + LOCAL_LOAD, + GETVAR, + SETVAR, + CATCH_SCOPE, + ENUM_INIT_KEYS, + ENUM_INIT_VALUES, + ENUM_INIT_ARRAY, + ENUM_INIT_VALUES_IN_ORDER, + ENUM_NEXT, + ENUM_ID, + THISFN, + RETURN_RESULT, // to return previously stored return result + ARRAYLIT, // array literal + OBJECTLIT, // object literal + GET_REF, // *reference + SET_REF, // *reference = something + DEL_REF, // delete reference + REF_CALL, // f(args) = something or f(args)++ + REF_SPECIAL, // reference for special properties like __proto + YIELD, // JS 1.7 yield pseudo keyword + STRICT_SETNAME, + EXP, // Exponentiation Operator + + // For XML support: + DEFAULTNAMESPACE, // default xml namespace = + ESCXMLATTR, + ESCXMLTEXT, + REF_MEMBER, // Reference for x.@y, x..y etc. + REF_NS_MEMBER, // Reference for x.ns::y, x..ns::y etc. + REF_NAME, // Reference for @y, @[y] etc. + REF_NS_NAME, // Reference for ns::y, @ns::y@[y] etc. + BIGINT, // ES2020 BigInt + + // End of interpreter bytecodes + TRY, + SEMI, // semicolon + LB, // left and right brackets + RB, + LC, // left and right curlies (braces) + RC, + LP, // left and right parentheses + RP, + COMMA, // comma operator + + ASSIGN, // simple assignment (=) + ASSIGN_BITOR, // |= + ASSIGN_LOGICAL_OR, // ||= + ASSIGN_BITXOR, // ^= + ASSIGN_BITAND, // |= + ASSIGN_LOGICAL_AND, // &&= + ASSIGN_LSH, // <<= + ASSIGN_RSH, // >>= + ASSIGN_URSH, // >>>= + ASSIGN_ADD, // += + ASSIGN_SUB, // -= + ASSIGN_MUL, // *= + ASSIGN_DIV, // /= + ASSIGN_MOD, // %= + ASSIGN_EXP, // **= + + HOOK, // conditional (?:) + COLON, + OR, // logical or (||) + AND, // logical and (&&) + INC, // increment/decrement (++ --) + DEC, + DOT, // member operator (.) + FUNCTION, // function keyword + EXPORT, // export keyword + IMPORT, // import keyword + IF, // if keyword + ELSE, // else keyword + SWITCH, // switch keyword + CASE, // case keyword + DEFAULT, // default keyword + WHILE, // while keyword + DO, // do keyword + FOR, // for keyword + BREAK, // break keyword + CONTINUE, // continue keyword + VAR, // var keyword + WITH, // with keyword + CATCH, // catch keyword + FINALLY, // finally keyword + VOID, // void keyword + RESERVED, // reserved keywords + EMPTY, + COMPUTED_PROPERTY, // computed property in object initializer [x] + + /* types used for the parse tree - these never get returned + * by the scanner. + */ + + BLOCK, // statement block + LABEL, // label + TARGET, + LOOP, + EXPR_VOID, // expression statement in functions + EXPR_RESULT, // expression statement in scripts + JSR, + SCRIPT, // top-level node for entire script + TYPEOFNAME, // for typeof(simple-name) + USE_STACK, + SETPROP_OP, // x.y op= something + SETELEM_OP, // x[y] op= something + LOCAL_BLOCK, + SET_REF_OP, // *reference op= something + + // For XML support: + DOTDOT, // member operator (..) + COLONCOLON, // namespace::name + XML, // XML type + DOTQUERY, // .() -- e.g., x.emps.emp.(name == "terry") + XMLATTR, // @ + XMLEND, + + // Optimizer-only-tokens + TO_OBJECT, + TO_DOUBLE, + GET, // JS 1.5 get pseudo keyword + SET, // JS 1.5 set pseudo keyword + LET, // JS 1.7 let pseudo keyword + CONST, + SETCONST, + SETCONSTVAR, + ARRAYCOMP, // array comprehension + LETEXPR, + WITHEXPR, + DEBUGGER, + COMMENT, + GENEXPR, + METHOD, // ES6 MethodDefinition + ARROW, // ES6 ArrowFunction + YIELD_STAR, // ES6 "yield *", a specialization of yield + TEMPLATE_LITERAL, // template literal + TEMPLATE_CHARS, // template literal - literal section + TEMPLATE_LITERAL_SUBST, // template literal - substitution + TAGGED_TEMPLATE_LITERAL, // template literal - tagged/handler + DOTDOTDOT, // spread/rest ... + NULLISH_COALESCING, + ; + public static enum CommentType { LINE, BLOCK_COMMENT, @@ -28,216 +227,17 @@ public static enum CommentType { static final boolean printICode = false; static final boolean printNames = printTrees || printICode; - /** Token types. These values correspond to JSTokenType values in jsscan.c. */ - public static final int - // start enum - ERROR = -1, // well-known as the only code < EOF - EOF = 0, // end of file token - (not EOF_CHAR) - EOL = 1, // end of line - - // Interpreter reuses the following as bytecodes - FIRST_BYTECODE_TOKEN = 2, - ENTERWITH = 2, - LEAVEWITH = 3, - RETURN = 4, - GOTO = 5, - IFEQ = 6, - IFNE = 7, - SETNAME = 8, - BITOR = 9, - BITXOR = 10, - BITAND = 11, - EQ = 12, - NE = 13, - LT = 14, - LE = 15, - GT = 16, - GE = 17, - LSH = 18, - RSH = 19, - URSH = 20, - ADD = 21, - SUB = 22, - MUL = 23, - DIV = 24, - MOD = 25, - NOT = 26, - BITNOT = 27, - POS = 28, - NEG = 29, - NEW = 30, - DELPROP = 31, - TYPEOF = 32, - GETPROP = 33, - GETPROPNOWARN = 34, - SETPROP = 35, - GETELEM = 36, - SETELEM = 37, - CALL = 38, - NAME = 39, - NUMBER = 40, - STRING = 41, - NULL = 42, - THIS = 43, - FALSE = 44, - TRUE = 45, - SHEQ = 46, // shallow equality (===) - SHNE = 47, // shallow inequality (!==) - REGEXP = 48, - BINDNAME = 49, - THROW = 50, - RETHROW = 51, // rethrow caught exception: catch (e if ) use it - IN = 52, - INSTANCEOF = 53, - LOCAL_LOAD = 54, - GETVAR = 55, - SETVAR = 56, - CATCH_SCOPE = 57, - ENUM_INIT_KEYS = 58, - ENUM_INIT_VALUES = 59, - ENUM_INIT_ARRAY = 60, - ENUM_INIT_VALUES_IN_ORDER = 61, - ENUM_NEXT = 62, - ENUM_ID = 63, - THISFN = 64, - RETURN_RESULT = 65, // to return previously stored return result - ARRAYLIT = 66, // array literal - OBJECTLIT = 67, // object literal - GET_REF = 68, // *reference - SET_REF = 69, // *reference = something - DEL_REF = 70, // delete reference - REF_CALL = 71, // f(args) = something or f(args)++ - REF_SPECIAL = 72, // reference for special properties like __proto - YIELD = 73, // JS 1.7 yield pseudo keyword - STRICT_SETNAME = 74, - EXP = 75, // Exponentiation Operator - - // For XML support: - DEFAULTNAMESPACE = 76, // default xml namespace = - ESCXMLATTR = 77, - ESCXMLTEXT = 78, - REF_MEMBER = 79, // Reference for x.@y, x..y etc. - REF_NS_MEMBER = 80, // Reference for x.ns::y, x..ns::y etc. - REF_NAME = 81, // Reference for @y, @[y] etc. - REF_NS_NAME = 82, // Reference for ns::y, @ns::y@[y] etc. - BIGINT = 83; // ES2020 BigInt - - // End of interpreter bytecodes - public static final int LAST_BYTECODE_TOKEN = BIGINT, - TRY = 84, - SEMI = 85, // semicolon - LB = 86, // left and right brackets - RB = 87, - LC = 88, // left and right curlies (braces) - RC = 89, - LP = 90, // left and right parentheses - RP = 91, - COMMA = 92, // comma operator - ASSIGN = 93, // simple assignment (=) - ASSIGN_BITOR = 94, // |= - ASSIGN_LOGICAL_OR = 95, // ||= - ASSIGN_BITXOR = 96, // ^= - ASSIGN_BITAND = 97, // |= - ASSIGN_LOGICAL_AND = 98, // &&= - ASSIGN_LSH = 99, // <<= - ASSIGN_RSH = 100, // >>= - ASSIGN_URSH = 101, // >>>= - ASSIGN_ADD = 102, // += - ASSIGN_SUB = 103, // -= - ASSIGN_MUL = 104, // *= - ASSIGN_DIV = 105, // /= - ASSIGN_MOD = 106, // %= - ASSIGN_EXP = 107; // **= - public static final int FIRST_ASSIGN = ASSIGN, - LAST_ASSIGN = ASSIGN_EXP, - HOOK = 108, // conditional (?:) - COLON = 109, - OR = 110, // logical or (||) - AND = 111, // logical and (&&) - INC = 112, // increment/decrement (++ --) - DEC = 113, - DOT = 114, // member operator (.) - FUNCTION = 115, // function keyword - EXPORT = 116, // export keyword - IMPORT = 117, // import keyword - IF = 118, // if keyword - ELSE = 119, // else keyword - SWITCH = 120, // switch keyword - CASE = 121, // case keyword - DEFAULT = 122, // default keyword - WHILE = 123, // while keyword - DO = 124, // do keyword - FOR = 125, // for keyword - BREAK = 126, // break keyword - CONTINUE = 127, // continue keyword - VAR = 128, // var keyword - WITH = 129, // with keyword - CATCH = 130, // catch keyword - FINALLY = 131, // finally keyword - VOID = 132, // void keyword - RESERVED = 133, // reserved keywords - EMPTY = 134, - COMPUTED_PROPERTY = 135, // computed property in object initializer [x] - - /* types used for the parse tree - these never get returned - * by the scanner. - */ - - BLOCK = 136, // statement block - LABEL = 137, // label - TARGET = 138, - LOOP = 139, - EXPR_VOID = 140, // expression statement in functions - EXPR_RESULT = 141, // expression statement in scripts - JSR = 142, - SCRIPT = 143, // top-level node for entire script - TYPEOFNAME = 144, // for typeof(simple-name) - USE_STACK = 145, - SETPROP_OP = 146, // x.y op= something - SETELEM_OP = 147, // x[y] op= something - LOCAL_BLOCK = 148, - SET_REF_OP = 149, // *reference op= something - - // For XML support: - DOTDOT = 150, // member operator (..) - COLONCOLON = 151, // namespace::name - XML = 152, // XML type - DOTQUERY = 153, // .() -- e.g., x.emps.emp.(name == "terry") - XMLATTR = 154, // @ - XMLEND = 155, - - // Optimizer-only-tokens - TO_OBJECT = 156, - TO_DOUBLE = 157, - GET = 158, // JS 1.5 get pseudo keyword - SET = 159, // JS 1.5 set pseudo keyword - LET = 160, // JS 1.7 let pseudo keyword - CONST = 161, - SETCONST = 162, - SETCONSTVAR = 163, - ARRAYCOMP = 164, // array comprehension - LETEXPR = 165, - WITHEXPR = 166, - DEBUGGER = 167, - COMMENT = 168, - GENEXPR = 169, - METHOD = 170, // ES6 MethodDefinition - ARROW = 171, // ES6 ArrowFunction - YIELD_STAR = 172, // ES6 "yield *", a specialization of yield - TEMPLATE_LITERAL = 173, // template literal - TEMPLATE_CHARS = 174, // template literal - literal section - TEMPLATE_LITERAL_SUBST = 175, // template literal - substitution - TAGGED_TEMPLATE_LITERAL = 176, // template literal - tagged/handler - DOTDOTDOT = 177, // spread/rest ... - NULLISH_COALESCING = 178, // nullish coalescing (??) - LAST_TOKEN = 178; + static Token FIRST_BYTECODE_TOKEN = ENTERWITH; + static Token LAST_BYTECODE_TOKEN = BIGINT; + public static final Token FIRST_ASSIGN = ASSIGN; + public static final Token LAST_ASSIGN = ASSIGN_EXP; /** * Returns a name for the token. If Rhino is compiled with certain hardcoded debugging flags in * this file, it calls {@code #typeToName}; otherwise it returns a string whose value is the * token number. */ - public static String name(int token) { + public static String name(Token token) { if (!printNames) { return String.valueOf(token); } @@ -251,7 +251,7 @@ public static String name(int token) { * @param token the token code * @return the actual name for the token code */ - public static String typeToName(int token) { + public static String typeToName(Token token) { switch (token) { case ERROR: return "ERROR"; @@ -622,71 +622,71 @@ public static String typeToName(int token) { * @param token A token * @return the corresponding name string */ - public static String keywordToName(int token) { + public static String keywordToName(Token token) { switch (token) { - case Token.BREAK: + case BREAK: return "break"; - case Token.CASE: + case CASE: return "case"; - case Token.CONTINUE: + case CONTINUE: return "continue"; - case Token.DEFAULT: + case DEFAULT: return "default"; - case Token.DELPROP: + case DELPROP: return "delete"; - case Token.DO: + case DO: return "do"; - case Token.ELSE: + case ELSE: return "else"; - case Token.FALSE: + case FALSE: return "false"; - case Token.FOR: + case FOR: return "for"; - case Token.FUNCTION: + case FUNCTION: return "function"; - case Token.IF: + case IF: return "if"; - case Token.IN: + case IN: return "in"; - case Token.LET: + case LET: return "let"; - case Token.NEW: + case NEW: return "new"; - case Token.NULL: + case NULL: return "null"; - case Token.RETURN: + case RETURN: return "return"; - case Token.SWITCH: + case SWITCH: return "switch"; - case Token.THIS: + case THIS: return "this"; - case Token.TRUE: + case TRUE: return "true"; - case Token.TYPEOF: + case TYPEOF: return "typeof"; - case Token.VAR: + case VAR: return "var"; - case Token.VOID: + case VOID: return "void"; - case Token.WHILE: + case WHILE: return "while"; - case Token.WITH: + case WITH: return "with"; - case Token.YIELD: + case YIELD: return "yield"; - case Token.CATCH: + case CATCH: return "catch"; - case Token.CONST: + case CONST: return "const"; - case Token.DEBUGGER: + case DEBUGGER: return "debugger"; - case Token.FINALLY: + case FINALLY: return "finally"; - case Token.INSTANCEOF: + case INSTANCEOF: return "instanceof"; - case Token.THROW: + case THROW: return "throw"; - case Token.TRY: + case TRY: return "try"; default: return null; @@ -699,7 +699,7 @@ public static String keywordToName(int token) { * @param code a potential token code * @return true if it's a known token */ - public static boolean isValidToken(int code) { - return code >= ERROR && code <= LAST_TOKEN; + public static boolean isValidToken(Token code) { + return code.ordinal() >= ERROR.ordinal(); } } diff --git a/rhino/src/main/java/org/mozilla/javascript/TokenStream.java b/rhino/src/main/java/org/mozilla/javascript/TokenStream.java index 9ea5aedeab..2ffa5e370a 100644 --- a/rhino/src/main/java/org/mozilla/javascript/TokenStream.java +++ b/rhino/src/main/java/org/mozilla/javascript/TokenStream.java @@ -642,7 +642,7 @@ final boolean eof() { return hitEOF; } - final int getToken() throws IOException { + final Token getToken() throws IOException { int c; for (; ; ) { @@ -1609,7 +1609,7 @@ private int peekTemplateLiteralChar() throws IOException { return c; } - int readTemplateLiteral(boolean isTaggedLiteral) throws IOException { + Token readTemplateLiteral(boolean isTaggedLiteral) throws IOException { rawString.setLength(0); stringBufferTop = 0; boolean hasInvalidEscapeSequences = false; diff --git a/rhino/src/main/java/org/mozilla/javascript/ast/AstNode.java b/rhino/src/main/java/org/mozilla/javascript/ast/AstNode.java index f5465dbaef..9c7ee749c7 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ast/AstNode.java +++ b/rhino/src/main/java/org/mozilla/javascript/ast/AstNode.java @@ -8,7 +8,7 @@ import java.io.Serializable; import java.util.Comparator; -import java.util.HashMap; +import java.util.EnumMap; import java.util.List; import java.util.Map; import org.mozilla.javascript.Kit; @@ -69,7 +69,7 @@ public abstract class AstNode extends Node implements Comparable { * and so on */ protected AstNode inlineComment; - private static Map operatorNames = new HashMap<>(); + private static Map operatorNames = new EnumMap<>(Token.class); private static final int MAX_INDENT = 42; private static final String[] INDENTATIONS = new String[MAX_INDENT + 1]; @@ -359,76 +359,76 @@ public static String operatorToString(int op) { @Override public boolean hasSideEffects() { switch (getType()) { - case Token.ASSIGN: - case Token.ASSIGN_ADD: - case Token.ASSIGN_BITAND: - case Token.ASSIGN_LOGICAL_AND: - case Token.ASSIGN_BITOR: - case Token.ASSIGN_LOGICAL_OR: - case Token.ASSIGN_BITXOR: - case Token.ASSIGN_DIV: - case Token.ASSIGN_LSH: - case Token.ASSIGN_MOD: - case Token.ASSIGN_MUL: - case Token.ASSIGN_RSH: - case Token.ASSIGN_SUB: - case Token.ASSIGN_URSH: - case Token.BLOCK: - case Token.BREAK: - case Token.CALL: - case Token.CATCH: - case Token.CATCH_SCOPE: - case Token.CONST: - case Token.CONTINUE: - case Token.DEC: - case Token.DELPROP: - case Token.DEL_REF: - case Token.DO: - case Token.ELSE: - case Token.ENTERWITH: - case Token.ERROR: // Avoid cascaded error messages - case Token.EXPORT: - case Token.EXPR_RESULT: - case Token.FINALLY: - case Token.FUNCTION: - case Token.FOR: - case Token.GOTO: - case Token.IF: - case Token.IFEQ: - case Token.IFNE: - case Token.IMPORT: - case Token.INC: - case Token.JSR: - case Token.LABEL: - case Token.LEAVEWITH: - case Token.LET: - case Token.LETEXPR: - case Token.LOCAL_BLOCK: - case Token.LOOP: - case Token.NEW: - case Token.REF_CALL: - case Token.RETHROW: - case Token.RETURN: - case Token.RETURN_RESULT: - case Token.SEMI: - case Token.SETELEM: - case Token.SETELEM_OP: - case Token.SETNAME: - case Token.SETPROP: - case Token.SETPROP_OP: - case Token.SETVAR: - case Token.SET_REF: - case Token.SET_REF_OP: - case Token.SWITCH: - case Token.TARGET: - case Token.THROW: - case Token.TRY: - case Token.VAR: - case Token.WHILE: - case Token.WITH: - case Token.WITHEXPR: - case Token.YIELD: - case Token.YIELD_STAR: + case ASSIGN: + case ASSIGN_ADD: + case ASSIGN_BITAND: + case ASSIGN_LOGICAL_AND: + case ASSIGN_BITOR: + case ASSIGN_LOGICAL_OR: + case ASSIGN_BITXOR: + case ASSIGN_DIV: + case ASSIGN_LSH: + case ASSIGN_MOD: + case ASSIGN_MUL: + case ASSIGN_RSH: + case ASSIGN_SUB: + case ASSIGN_URSH: + case BLOCK: + case BREAK: + case CALL: + case CATCH: + case CATCH_SCOPE: + case CONST: + case CONTINUE: + case DEC: + case DELPROP: + case DEL_REF: + case DO: + case ELSE: + case ENTERWITH: + case ERROR: // Avoid cascaded error messages + case EXPORT: + case EXPR_RESULT: + case FINALLY: + case FUNCTION: + case FOR: + case GOTO: + case IF: + case IFEQ: + case IFNE: + case IMPORT: + case INC: + case JSR: + case LABEL: + case LEAVEWITH: + case LET: + case LETEXPR: + case LOCAL_BLOCK: + case LOOP: + case NEW: + case REF_CALL: + case RETHROW: + case RETURN: + case RETURN_RESULT: + case SEMI: + case SETELEM: + case SETELEM_OP: + case SETNAME: + case SETPROP: + case SETPROP_OP: + case SETVAR: + case SET_REF: + case SET_REF_OP: + case SWITCH: + case TARGET: + case THROW: + case TRY: + case VAR: + case WHILE: + case WITH: + case WITHEXPR: + case YIELD: + case YIELD_STAR: return true; default: @@ -569,7 +569,7 @@ private static String makeIndent(int depth) { @Override public boolean visit(AstNode node) { - int tt = node.getType(); + Token tt = node.getType(); String name = Token.typeToName(tt); buffer.append(node.getAbsolutePosition()).append("\t"); buffer.append(makeIndent(node.depth())); diff --git a/rhino/src/main/java/org/mozilla/javascript/ast/AstRoot.java b/rhino/src/main/java/org/mozilla/javascript/ast/AstRoot.java index 379d5aee4b..8bde0ff82e 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ast/AstRoot.java +++ b/rhino/src/main/java/org/mozilla/javascript/ast/AstRoot.java @@ -131,7 +131,7 @@ public void checkParentLinks() { new NodeVisitor() { @Override public boolean visit(AstNode node) { - int type = node.getType(); + Token type = node.getType(); if (type == Token.SCRIPT) return true; if (node.getParent() == null) throw new IllegalStateException( diff --git a/rhino/src/main/java/org/mozilla/javascript/ast/Jump.java b/rhino/src/main/java/org/mozilla/javascript/ast/Jump.java index 4fd4939e2e..cf48536127 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ast/Jump.java +++ b/rhino/src/main/java/org/mozilla/javascript/ast/Jump.java @@ -25,21 +25,21 @@ public Jump() { type = Token.ERROR; } - public Jump(int nodeType) { + public Jump(Token nodeType) { type = nodeType; } - public Jump(int type, int lineno) { + public Jump(Token type, int lineno) { this(type); setLineno(lineno); } - public Jump(int type, Node child) { + public Jump(Token type, Node child) { this(type); addChildToBack(child); } - public Jump(int type, Node child, int lineno) { + public Jump(Token type, Node child, int lineno) { this(type, child); setLineno(lineno); } diff --git a/rhino/src/main/java/org/mozilla/javascript/ast/Symbol.java b/rhino/src/main/java/org/mozilla/javascript/ast/Symbol.java index 2b5f5598ac..02081b1204 100644 --- a/rhino/src/main/java/org/mozilla/javascript/ast/Symbol.java +++ b/rhino/src/main/java/org/mozilla/javascript/ast/Symbol.java @@ -14,7 +14,7 @@ public class Symbol { // One of Token.FUNCTION, Token.LP (for parameters), Token.VAR, // Token.LET, or Token.CONST - private int declType; + private Token declType; private int index = -1; private String name; private Node node; @@ -28,18 +28,18 @@ public Symbol() {} * @param declType {@link Token#FUNCTION}, {@link Token#LP} (for params), {@link Token#VAR}, * {@link Token#LET} or {@link Token#CONST} */ - public Symbol(int declType, String name) { + public Symbol(Token declType, String name) { setName(name); setDeclType(declType); } /** Returns symbol declaration type */ - public int getDeclType() { + public Token getDeclType() { return declType; } /** Sets symbol declaration type */ - public void setDeclType(int declType) { + public void setDeclType(Token declType) { if (!(declType == Token.FUNCTION || declType == Token.LP || declType == Token.VAR