⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parser.java

📁 java中比较著名的js引擎当属mozilla开源的rhino
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                tt = peekToken();                if (tt == Token.SEMI) {                    init = nf.createLeaf(Token.EMPTY);                } else {                    if (tt == Token.VAR) {                        // set init to a var list or initial                        consumeToken();    // consume the 'var' token                        init = variables(true);                    }                    else {                        init = expr(true);                    }                }                if (matchToken(Token.IN)) {                    decompiler.addToken(Token.IN);                    // 'cond' is the object over which we're iterating                    cond = expr(false);                } else {  // ordinary for loop                    mustMatchToken(Token.SEMI, "msg.no.semi.for");                    decompiler.addToken(Token.SEMI);                    if (peekToken() == Token.SEMI) {                        // no loop condition                        cond = nf.createLeaf(Token.EMPTY);                    } else {                        cond = expr(false);                    }                    mustMatchToken(Token.SEMI, "msg.no.semi.for.cond");                    decompiler.addToken(Token.SEMI);                    if (peekToken() == Token.RP) {                        incr = nf.createLeaf(Token.EMPTY);                    } else {                        incr = expr(false);                    }                }                mustMatchToken(Token.RP, "msg.no.paren.for.ctrl");                decompiler.addToken(Token.RP);                decompiler.addEOL(Token.LC);                body = statement();                decompiler.addEOL(Token.RC);                if (incr == null) {                    // cond could be null if 'in obj' got eaten                    // by the init node.                    pn = nf.createForIn(loop, init, cond, body, isForEach);                } else {                    pn = nf.createFor(loop, init, cond, incr, body);                }            } finally {                exitLoop();            }            return pn;          }          case Token.TRY: {            consumeToken();            int lineno = ts.getLineno();            Node tryblock;            Node catchblocks = null;            Node finallyblock = null;            decompiler.addToken(Token.TRY);            decompiler.addEOL(Token.LC);            tryblock = statement();            decompiler.addEOL(Token.RC);            catchblocks = nf.createLeaf(Token.BLOCK);            boolean sawDefaultCatch = false;            int peek = peekToken();            if (peek == Token.CATCH) {                while (matchToken(Token.CATCH)) {                    if (sawDefaultCatch) {                        reportError("msg.catch.unreachable");                    }                    decompiler.addToken(Token.CATCH);                    mustMatchToken(Token.LP, "msg.no.paren.catch");                    decompiler.addToken(Token.LP);                    mustMatchToken(Token.NAME, "msg.bad.catchcond");                    String varName = ts.getString();                    decompiler.addName(varName);                    Node catchCond = null;                    if (matchToken(Token.IF)) {                        decompiler.addToken(Token.IF);                        catchCond = expr(false);                    } else {                        sawDefaultCatch = true;                    }                    mustMatchToken(Token.RP, "msg.bad.catchcond");                    decompiler.addToken(Token.RP);                    mustMatchToken(Token.LC, "msg.no.brace.catchblock");                    decompiler.addEOL(Token.LC);                    nf.addChildToBack(catchblocks,                        nf.createCatch(varName, catchCond,                                       statements(),                                       ts.getLineno()));                    mustMatchToken(Token.RC, "msg.no.brace.after.body");                    decompiler.addEOL(Token.RC);                }            } else if (peek != Token.FINALLY) {                mustMatchToken(Token.FINALLY, "msg.try.no.catchfinally");            }            if (matchToken(Token.FINALLY)) {                decompiler.addToken(Token.FINALLY);                decompiler.addEOL(Token.LC);                finallyblock = statement();                decompiler.addEOL(Token.RC);            }            pn = nf.createTryCatchFinally(tryblock, catchblocks,                                          finallyblock, lineno);            return pn;          }          case Token.THROW: {            consumeToken();            if (peekTokenOrEOL() == Token.EOL) {                // ECMAScript does not allow new lines before throw expression,                // see bug 256617                reportError("msg.bad.throw.eol");            }            int lineno = ts.getLineno();            decompiler.addToken(Token.THROW);            pn = nf.createThrow(expr(false), lineno);            break;          }          case Token.BREAK: {            consumeToken();            int lineno = ts.getLineno();            decompiler.addToken(Token.BREAK);            // matchJumpLabelName only matches if there is one            Node breakStatement = matchJumpLabelName();            if (breakStatement == null) {                if (loopAndSwitchSet == null || loopAndSwitchSet.size() == 0) {                    reportError("msg.bad.break");                    return null;                }                breakStatement = (Node)loopAndSwitchSet.peek();            }            pn = nf.createBreak(breakStatement, lineno);            break;          }          case Token.CONTINUE: {            consumeToken();            int lineno = ts.getLineno();            decompiler.addToken(Token.CONTINUE);            Node loop;            // matchJumpLabelName only matches if there is one            Node label = matchJumpLabelName();            if (label == null) {                if (loopSet == null || loopSet.size() == 0) {                    reportError("msg.continue.outside");                    return null;                }                loop = (Node)loopSet.peek();            } else {                loop = nf.getLabelLoop(label);                if (loop == null) {                    reportError("msg.continue.nonloop");                    return null;                }            }            pn = nf.createContinue(loop, lineno);            break;          }          case Token.WITH: {            consumeToken();            decompiler.addToken(Token.WITH);            int lineno = ts.getLineno();            mustMatchToken(Token.LP, "msg.no.paren.with");            decompiler.addToken(Token.LP);            Node obj = expr(false);            mustMatchToken(Token.RP, "msg.no.paren.after.with");            decompiler.addToken(Token.RP);            decompiler.addEOL(Token.LC);            ++nestingOfWith;            Node body;            try {                body = statement();            } finally {                --nestingOfWith;            }            decompiler.addEOL(Token.RC);            pn = nf.createWith(obj, body, lineno);            return pn;          }          case Token.VAR: {            consumeToken();            pn = variables(false);            break;          }          case Token.RETURN: {            if (!insideFunction()) {                reportError("msg.bad.return");            }            consumeToken();            decompiler.addToken(Token.RETURN);            int lineno = ts.getLineno();            Node retExpr;            /* This is ugly, but we don't want to require a semicolon. */            tt = peekTokenOrEOL();            switch (tt) {              case Token.SEMI:              case Token.RC:              case Token.EOF:              case Token.EOL:              case Token.ERROR:                retExpr = null;                break;              default:                retExpr = expr(false);            }            pn = nf.createReturn(retExpr, lineno);            break;          }          case Token.LC:            consumeToken();            if (statementLabel != null) {                decompiler.addToken(Token.LC);            }            pn = statements();            mustMatchToken(Token.RC, "msg.no.brace.block");            if (statementLabel != null) {                decompiler.addEOL(Token.RC);            }            return pn;          case Token.ERROR:            // Fall thru, to have a node for error recovery to work on          case Token.SEMI:            consumeToken();            pn = nf.createLeaf(Token.EMPTY);            return pn;          case Token.FUNCTION: {            consumeToken();            pn = function(FunctionNode.FUNCTION_EXPRESSION_STATEMENT);            return pn;          }          case Token.DEFAULT :            consumeToken();            mustHaveXML();            decompiler.addToken(Token.DEFAULT);            int nsLine = ts.getLineno();            if (!(matchToken(Token.NAME)                  && ts.getString().equals("xml")))            {                reportError("msg.bad.namespace");            }            decompiler.addName(ts.getString());            if (!(matchToken(Token.NAME)                  && ts.getString().equals("namespace")))            {                reportError("msg.bad.namespace");            }            decompiler.addName(ts.getString());            if (!matchToken(Token.ASSIGN)) {                reportError("msg.bad.namespace");            }            decompiler.addToken(Token.ASSIGN);            Node expr = expr(false);            pn = nf.createDefaultNamespace(expr, nsLine);            break;          case Token.NAME: {            int lineno = ts.getLineno();            String name = ts.getString();            setCheckForLabel();            pn = expr(false);            if (pn.getType() != Token.LABEL) {                pn = nf.createExprStatement(pn, lineno);            } else {                // Parsed the label: push back token should be                // colon that primaryExpr left untouched.                if (peekToken() != Token.COLON) Kit.codeBug();                consumeToken();                // depend on decompiling lookahead to guess that that                // last name was a label.                decompiler.addName(name);                decompiler.addEOL(Token.COLON);                if (labelSet == null) {                    labelSet = new Hashtable();                } else if (labelSet.containsKey(name)) {                    reportError("msg.dup.label");                }                boolean firstLabel;                if (statementLabel == null) {                    firstLabel = true;                    statementLabel = pn;                } else {                    // Discard multiple label nodes and use only                    // the first: it allows to simplify IRFactory                    firstLabel = false;                }                labelSet.put(name, statementLabel);                try {                    pn = statementHelper(statementLabel);                } finally {                    labelSet.remove(name);                }                if (firstLabel) {                    pn = nf.createLabeledStatement(statementLabel, pn);                }                return pn;            }            break;          }          default: {            int lineno = ts.getLineno();            pn = expr(false);            pn = nf.createExprStatement(pn, lineno);            break;          }        }        int ttFlagged = peekFlaggedToken();        switch (ttFlagged & CLEAR_TI_MASK) {          case Token.SEMI:            // Consume ';' as a part of expression            consumeToken();            break;          case Token.ERROR:          case Token.EOF:          case Token.RC:            // Autoinsert ;            break;          default:            if ((ttFlagged & TI_AFTER_EOL) == 0) {                // Report error if no EOL or autoinsert ; otherwise                reportError("msg.no.semi.stmt");            }            break;        }        decompiler.addEOL(Token.SEMI);        return pn;    }    private Node variables(boolean inForInit)        throws IOException, ParserException    {        Node pn = nf.createVariables(ts.getLineno());        boolean first = true;        decompiler.addToken(Token.VAR);        for (;;) {            Node name;            Node init;            mustMatchToken(Token.NAME, "msg.bad.var");            String s = ts.getString();            if (!first)                decompiler.addToken(Token.COMMA);            first = false;            decompiler.addName(s);            currentScriptOrFn.addVar(s);            name = nf.createName(s);            // omitted check for argument hiding            if (matchToken(Token.ASSIGN)) {                decompiler.addToken(Token.ASSIGN);                init = assignExpr(inForInit);                nf.addChildToBack(name, init);            }            nf.addChildToBack(pn, name);            if (!matchToken(Token.COMMA))

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -