📄 parser.java
字号:
script = loadScript(className); } catch (Exception e) { throw new ESParseException(e); } script.setScriptPath(getScriptPath()); script.setClassDir(workPath); return script; } } /** * Tries to load an already compiled script. * * @param className mangled classname of the script. */ private Script loadScript(String className) throws Exception { ClassLoader loader = getClassLoader(); Class cl = CauchoSystem.loadClass(className, false, loader); Script script = (Script) cl.newInstance(); script.setScriptPath(getScriptPath()); script.setClassDir(workPath); return script; } /** * Parse a function */ private Function parseFunction() throws ESException { function.setNeedsScope(); ESId id = null; if (lexer.peek() == Lexer.IDENTIFIER) { lexer.next(); id = lexer.getId(); } if (lexer.next() != '(') throw expect("`('"); if (id != null) { function.addVariable(block, id, null); block.newVar(id).getVar().setType(Expr.TYPE_ES); } Block oldBlock = block; Function oldFun = function; function = parseClass.newFunction(oldFun, id, false); oldFun.addFunction(function); block = Block.create(this, function); boolean isFirst = true; while (lexer.peek() != ')') { if (! isFirst && lexer.next() != ',') throw expect("`,'"); isFirst = false; if (lexer.next() != Lexer.IDENTIFIER) throw expect(L.l("formal argument")); ESId argId = lexer.getId(); Expr type = null; if (lexer.peek() == ':') { lexer.next(); type = parseType(); } function.addFormal(block, argId, type); } lexer.next(); if (lexer.peek() == ':') { lexer.next(); Expr type = parseType(); function.setReturnType(type); } if (lexer.next() != '{') throw expect("`{'"); parseBlock(false); if (lexer.next() != '}') { throw expect("`}'"); } block.finish(); Function newFun = function; function = oldFun; block = oldBlock; return newFun; } /** * Parses a list of statements, returning a block representing the * statements. * * @param parent the containing block * @param isTop true if this is a top level block */ private void parseBlock(boolean isTop) throws ESException { loop: while (true) { switch (lexer.peek()) { case Lexer.UNARY_OP: case Lexer.BANDU_OP: case Lexer.NEW: case Lexer.DELETE: case Lexer.VOID: case Lexer.TYPEOF: case Lexer.POSTFIX: case Lexer.IDENTIFIER: case Lexer.THIS: case Lexer.EVAL: case Lexer.LITERAL: case Lexer.REGEXP: case '(': case '[': case Lexer.HASH_REF: case Lexer.HASH_DEF: case Lexer.FUNCTION: parseStatement(); break; case Lexer.IF: case Lexer.FOR: case Lexer.WHILE: case Lexer.DO: case Lexer.VAR: case Lexer.BREAK: case Lexer.WITH: case Lexer.SYNCHRONIZED: case Lexer.CONTINUE: case Lexer.RETURN: case Lexer.SWITCH: case Lexer.TRY: case Lexer.THROW: case ';': parseStatement(); break; case '{': block = block.startBlock(); parseStatement(); block = block.finishBlock(); break; case Lexer.CATCH: block.doTry(); parseCatch(); break; case Lexer.FINALLY: block.doTry(); parseFinally(); break; case Lexer.CLASS: parseClass(); break; case Lexer.IMPORT: parseImport(); break; case Lexer.STATIC: if (true) throw new ESException("nostatus"); //parseStatic(); break; default: break loop; } } } /** * Parses a statement. */ private void parseStatement() throws ESException { int lexeme = lexer.peek(); hashes.clear(); int line = lexer.getLine(); Expr expr = null; block.setLine(line); if (block.isDead) { switch (lexeme) { case ';': case Lexer.VAR: case Lexer.FUNCTION: case Lexer.CATCH: case Lexer.FINALLY: break; default: throw error(L.l("can't reach statement")); } } switch (lexeme) { case Lexer.IDENTIFIER: parseIdentifierStatement(); break; case Lexer.UNARY_OP: case Lexer.BANDU_OP: case Lexer.NEW: case Lexer.THIS: case Lexer.EVAL: case Lexer.LITERAL: case Lexer.REGEXP: case Lexer.POSTFIX: case Lexer.DELETE: case Lexer.VOID: case Lexer.TYPEOF: case '(': case '[': case Lexer.HASH_REF: case Lexer.HASH_DEF: block.addExpr(parseExpression(PREC_MAX, true)); parseStatementEnd(); break; case Lexer.FUNCTION: lexer.next(); Function newFun = parseFunction(); break; case Lexer.VAR: parseVar(false); parseStatementEnd(); break; case Lexer.BREAK: lexer.next(); if (lexer.peek() == Lexer.IDENTIFIER && ! lexer.seenLineFeed()) { block.doBreak(lexer.getId()); lexer.next(); } else block.doBreak(); parseStatementEnd(); break; case Lexer.CONTINUE: lexer.next(); if (lexer.peek() == Lexer.IDENTIFIER && ! lexer.seenLineFeed()) { block.doContinue(lexer.getId()); lexer.next(); } else block.doContinue(); parseStatementEnd(); break; case Lexer.RETURN: lexer.next(); if (lexer.peek() == ';' || lexer.peek() == '}' || lexer.seenLineFeed()) { block.doReturn(); } else { block.doReturn(parseExpression(PREC_MAX, true)); } parseStatementEnd(); break; case Lexer.IF: parseIf(); break; case Lexer.SWITCH: parseSwitch(); break; case Lexer.WHILE: parseWhile(null); break; case Lexer.DO: parseDo(null); break; case Lexer.FOR: parseFor(null); break; case Lexer.WITH: parseWith(); break; case Lexer.SYNCHRONIZED: parseSynchronized(); break; case Lexer.TRY: parseTry(); break; case Lexer.THROW: lexer.next(); block.doThrow(parseExpression(PREC_MAX)); break; case ';': lexer.next(); break; case '{': lexer.next(); parseBlock(false); if (lexer.next() != '}') throw expect("`}'"); break; default: throw expect(L.l("statement")); } } private void parseStatementEnd() throws ESException { if (lexer.peek() == ';') lexer.next(); else if (lexer.peek() == '}' || lexer.peek() == Lexer.EOF || lexer.seenLineFeed()) { } else { throw expect("`;'"); } } private void parseIdentifierStatement() throws ESException { ESId id = lexer.getId(); int line = lexer.getLine(); lexer.next(); if (lexer.peek() != ':') { Expr var = getVar(id); Expr expr = parseExprRec(parseTermTail(var, false, true), PREC_MAX, false, true); block.addExpr(expr); parseStatementEnd(); return; } lexer.next(); /* if (findLoop(null, id) != null) throw error("duplicate label `" + id + "'"); */ switch (lexer.peek()) { case Lexer.WHILE: parseWhile(id); break; case Lexer.DO: parseDo(id); break; case Lexer.FOR: parseFor(id); break; default: block = block.startBlock(id); parseStatement(); block = block.finishBlock(); break; } } private Expr parseType() throws ESException { if (lexer.next() != Lexer.IDENTIFIER) throw expect(L.l("identifier")); Expr type = block.newType(lexer.getId()); while (lexer.peek() == '.') { lexer.next(); if (lexer.next() != Lexer.IDENTIFIER) throw expect(L.l("identifier")); type = type.fieldReference(lexer.getId()); } return type; } /** * if ::= IF '(' expr ')' stmt */ private void parseIf() throws ESException { boolean isFirst = true; boolean isDead = true; block = block.create(); while (lexer.peek() == Lexer.IF) { lexer.next(); if (lexer.next() != '(') throw expect("`('"); block.startIf(parseBooleanExpression(PREC_MAX), ! isFirst); isFirst = false; if (lexer.next() != ')') throw expect("`)'"); parseStatement(); block.endBlock(); if (! block.isDead) isDead = false; block.isDead = false; if (lexer.peek() != Lexer.ELSE) { block = block.pop(); return; } lexer.next(); } block.startElse(); parseStatement(); block.endBlock(); if (! block.isDead) isDead = false; block = block.pop(); block.isDead = isDead; } /** * switch ::= SWITCH '(' expr ')' '{' ((CASE expr:|DEFAULT)+ stmt*)* '}' */ private void parseSwitch() throws ESException { lexer.next(); if (lexer.next() != '(') throw expect("`)'"); Expr test = parseExpression(PREC_MAX); if (lexer.next() != ')') throw expect("`)'"); if (lexer.next() != '{') throw expect("`{'"); ArrayList exprs = new ArrayList(); block = block.startSwitch(test); int ch; while ((ch = lexer.peek()) != -1 && ch != '}') { switch (ch) { case Lexer.CASE: lexer.next(); block.doCase(exprs.size()); exprs.add(parseExpression(PREC_MAX)); if (lexer.next() != ':') throw expect("`:'"); break; case Lexer.DEFAULT: lexer.next(); if (lexer.next() != ':') throw expect("`:'"); block.doDefault(); break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -