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

📄 treebuilder.java

📁 无线通信的主要编程软件,是无线通信工作人员的必备工具,关天相关教程我会在后续传上.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.python.parser;import org.python.parser.ast.*;import org.python.core.PyObject;public class TreeBuilder implements PythonGrammarTreeConstants {    private JJTPythonGrammarState stack;    CtxVisitor ctx;    public TreeBuilder(JJTPythonGrammarState stack) {        this.stack = stack;        ctx = new CtxVisitor();    }    private stmtType[] makeStmts(int l) {        stmtType[] stmts = new stmtType[l];        for (int i = l-1; i >= 0; i--) {            stmts[i] = (stmtType) stack.popNode();        }        return stmts;    }    private stmtType[] popSuite() {        return ((Suite) popNode()).body;    }    private exprType[] makeExprs() {        if (stack.nodeArity() > 0 && peekNode().getId() == JJTCOMMA)            popNode();        return makeExprs(stack.nodeArity());    }    private exprType[] makeExprs(int l) {        exprType[] exprs = new exprType[l];        for (int i = l-1; i >= 0; i--) {            exprs[i] = makeExpr();        }        return exprs;    }    private exprType makeExpr(SimpleNode node) {        return (exprType) node;    }    private exprType makeExpr() {        return makeExpr((SimpleNode) stack.popNode());    }    private String makeIdentifier() {        return ((Name) stack.popNode()).id;    }    private String[] makeIdentifiers() {        int l = stack.nodeArity();        String[] ids = new String[l];        for (int i = l - 1; i >= 0; i--) {            ids[i] = makeIdentifier();        }        return ids;    }    private aliasType[] makeAliases() {        return makeAliases(stack.nodeArity());    }    private aliasType[] makeAliases(int l) {        aliasType[] aliases = new aliasType[l];        for (int i = l-1; i >= 0; i--) {            aliases[i] = (aliasType) stack.popNode();        }        return aliases;    }    private static SimpleNode[] nodes =        new SimpleNode[PythonGrammarTreeConstants.jjtNodeName.length];    public SimpleNode openNode(int id) {        if (nodes[id] == null)            nodes[id] = new IdentityNode(id);        return nodes[id];    }        public SimpleNode closeNode(SimpleNode n, int arity) throws Exception {        exprType value;        exprType[] exprs;        switch (n.getId()) {        case -1:            System.out.println("Illegal node");        case JJTSINGLE_INPUT:            return new Interactive(makeStmts(arity));        case JJTFILE_INPUT:            return new Module(makeStmts(arity));        case JJTEVAL_INPUT:            return new Expression(makeExpr());        case JJTNAME:            return new Name(n.getImage().toString(), Name.Load);        case JJTNUM:            return new Num((PyObject) n.getImage());        case JJTSTRING:            return new Str(n.getImage().toString());        case JJTSUITE:            stmtType[] stmts = new stmtType[arity];            for (int i = arity-1; i >= 0; i--) {                stmts[i] = (stmtType) popNode();            }            return new Suite(stmts);        case JJTEXPR_STMT:            value = makeExpr();            if (arity > 1) {                exprs = makeExprs(arity-1);                ctx.setStore(exprs);                return new Assign(exprs, value);            } else {                return new Expr(value);            }        case JJTINDEX_OP:            sliceType slice = (sliceType) stack.popNode();            value = makeExpr();            return new Subscript(value, slice, Subscript.Load);        case JJTDOT_OP:            String attr = makeIdentifier();            value = makeExpr();            return new Attribute(value, attr, Attribute.Load);        case JJTDEL_STMT:            exprs = makeExprs(arity);            ctx.setDelete(exprs);            return new Delete(exprs);        case JJTPRINT_STMT:            boolean nl = true;            if (stack.nodeArity() == 0)                return new Print(null, null, true);            if (peekNode().getId() == JJTCOMMA) {                popNode();                nl = false;            }            return new Print(null, makeExprs(), nl);        case JJTPRINTEXT_STMT:            nl = true;            if (peekNode().getId() == JJTCOMMA) {                popNode();                nl = false;            }            exprs = makeExprs(stack.nodeArity()-1);            return new Print(makeExpr(), exprs, nl);        case JJTFOR_STMT:            stmtType[] orelse = null;            if (stack.nodeArity() == 4)                orelse = popSuite();            stmtType[] body = popSuite();            exprType iter = makeExpr();            exprType target = makeExpr();            ctx.setStore(target);            return new For(target, iter, body, orelse);        case JJTWHILE_STMT:            orelse = null;            if (stack.nodeArity() == 3)                orelse = popSuite();            body = popSuite();            exprType test = makeExpr();            return new While(test, body, orelse);        case JJTIF_STMT:            orelse = null;            if (arity % 2 == 1)                orelse = popSuite();            body = popSuite();            test = makeExpr();            If last = new If(test, body, orelse);            for (int i = 0; i < (arity / 2)-1; i++) {                body = popSuite();                test = makeExpr();                last = new If(test, body, new stmtType[] { last });            }            return last;        case JJTPASS_STMT:            return new Pass();        case JJTBREAK_STMT:            return new Break();        case JJTCONTINUE_STMT:            return new Continue();        case JJTFUNCDEF:            body = popSuite();            argumentsType arguments = makeArguments(arity - 2);            String name = makeIdentifier();            return new FunctionDef(name, arguments, body);        case JJTDEFAULTARG:            value = (arity == 1) ? null : makeExpr();            return new DefaultArg(makeExpr(), value);        case JJTEXTRAARGLIST:            return new ExtraArg(makeIdentifier(), JJTEXTRAARGLIST);        case JJTEXTRAKEYWORDLIST:            return new ExtraArg(makeIdentifier(), JJTEXTRAKEYWORDLIST);/*        case JJTFPLIST:            fpdefType[] list = new fpdefType[arity];            for (int i = arity-1; i >= 0; i--) {                list[i] = popFpdef();            }            return new FpList(list);*/        case JJTCLASSDEF:            body = popSuite();            exprType[] bases = makeExprs(stack.nodeArity() - 1);            name = makeIdentifier();            return new ClassDef(name, bases, body);        case JJTRETURN_STMT:            value = arity == 1 ? makeExpr() : null;            return new Return(value);        case JJTYIELD_STMT:            return new Yield(makeExpr());        case JJTRAISE_STMT:            exprType tback = arity >= 3 ? makeExpr() : null;            exprType inst = arity >= 2 ? makeExpr() : null;            exprType type = arity >= 1 ? makeExpr() : null;            return new Raise(type, inst, tback);        case JJTGLOBAL_STMT:            return new Global(makeIdentifiers());        case JJTEXEC_STMT:            exprType globals = arity >= 3 ? makeExpr() : null;            exprType locals = arity >= 2 ? makeExpr() : null;            value = makeExpr();            return new Exec(value, locals, globals);        case JJTASSERT_STMT:            exprType msg = arity == 2 ? makeExpr() : null;            test = makeExpr();            return new Assert(test, msg);        case JJTTRYFINALLY_STMT:            orelse = popSuite();            return new TryFinally(popSuite(), orelse);        case JJTTRY_STMT:            orelse = null;            if (peekNode() instanceof Suite) {                arity--;                orelse = popSuite();            }            int l = arity - 1;            excepthandlerType[] handlers = new excepthandlerType[l];            for (int i = l - 1; i >= 0; i--) {                handlers[i] = (excepthandlerType) popNode();            }            return new TryExcept(popSuite(), handlers, orelse);        case JJTEXCEPT_CLAUSE:            body = popSuite();            exprType excname = arity == 3 ? makeExpr() : null;            if (excname != null)                    ctx.setStore(excname);            type = arity >= 2 ? makeExpr() : null;            return new excepthandlerType(type, excname, body);        case JJTOR_BOOLEAN:            return new BoolOp(BoolOp.Or, makeExprs());        case JJTAND_BOOLEAN:            return new BoolOp(BoolOp.And, makeExprs());        case JJTCOMPARISION:            l = arity / 2;            exprType[] comparators = new exprType[l];            int[] ops = new int[l];            for (int i = l-1; i >= 0; i--) {                comparators[i] = makeExpr();                SimpleNode op = (SimpleNode) stack.popNode();                switch (op.getId()) {                case JJTLESS_CMP:          ops[i] = Compare.Lt; break;                case JJTGREATER_CMP:       ops[i] = Compare.Gt; break;                case JJTEQUAL_CMP:         ops[i] = Compare.Eq; break;                case JJTGREATER_EQUAL_CMP: ops[i] = Compare.GtE; break;                case JJTLESS_EQUAL_CMP:    ops[i] = Compare.LtE; break;                case JJTNOTEQUAL_CMP:      ops[i] = Compare.NotEq; break;                case JJTIN_CMP:            ops[i] = Compare.In; break;                case JJTNOT_IN_CMP:        ops[i] = Compare.NotIn; break;                case JJTIS_NOT_CMP:        ops[i] = Compare.IsNot; break;                case JJTIS_CMP:            ops[i] = Compare.Is; break;                default:                    throw new RuntimeException("Unknown cmp op:" + op.getId());                }            }            return new Compare(makeExpr(), ops, comparators);        case JJTLESS_CMP:        case JJTGREATER_CMP:        case JJTEQUAL_CMP:        case JJTGREATER_EQUAL_CMP:        case JJTLESS_EQUAL_CMP:        case JJTNOTEQUAL_CMP:        case JJTIN_CMP:        case JJTNOT_IN_CMP:        case JJTIS_NOT_CMP:        case JJTIS_CMP:            return n;        case JJTOR_2OP:            return makeBinOp(BinOp.BitOr);        case JJTXOR_2OP:            return makeBinOp(BinOp.BitXor);        case JJTAND_2OP:            return makeBinOp(BinOp.BitAnd);        case JJTLSHIFT_2OP:            return makeBinOp(BinOp.LShift);        case JJTRSHIFT_2OP:            return makeBinOp(BinOp.RShift);        case JJTADD_2OP:              return makeBinOp(BinOp.Add);        case JJTSUB_2OP:             return makeBinOp(BinOp.Sub);        case JJTMUL_2OP:            return makeBinOp(BinOp.Mult);        case JJTDIV_2OP:             return makeBinOp(BinOp.Div);        case JJTMOD_2OP:            return makeBinOp(BinOp.Mod);        case JJTPOW_2OP:            return makeBinOp(BinOp.Pow);        case JJTFLOORDIV_2OP:            return makeBinOp(BinOp.FloorDiv);        case JJTPOS_1OP:            return new UnaryOp(UnaryOp.UAdd, makeExpr());        case JJTNEG_1OP:            return new UnaryOp(UnaryOp.USub, makeExpr());        case JJTINVERT_1OP:            return new UnaryOp(UnaryOp.Invert, makeExpr());        case JJTNOT_1OP:            return new UnaryOp(UnaryOp.Not, makeExpr());        case JJTCALL_OP:            //if (arity == 1)            //    return new Call(makeExpr(), null, null, null, null);            exprType starargs = null;            exprType kwargs = null;            l = arity - 1;            if (l > 0 && peekNode().getId() == JJTEXTRAKEYWORDVALUELIST) {                kwargs = ((ExtraArgValue) popNode()).value;                l--;            }            if (l > 0 && peekNode().getId() == JJTEXTRAARGVALUELIST) {                starargs = ((ExtraArgValue) popNode()).value;                l--;            }                        int nargs = l;            SimpleNode[] tmparr = new SimpleNode[l];             for (int i = l - 1; i >= 0; i--) {                tmparr[i] = popNode();                if (tmparr[i] instanceof keywordType) {                    nargs = i;                }            }            

⌨️ 快捷键说明

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