/** * @param s the scope we're checking for * @return if the scope passed as a parameter starts with the same scope we have here. It should * not be called if the size of the scope we're checking is bigger than the size of 'this' * scope. */ @SuppressWarnings("unchecked") private boolean checkIfScopesMatch(ILocalScope s) { Iterator<SimpleNode> otIt = s.getScopeStack().iterator(); for (Iterator<SimpleNode> iter = this.scope.iterator(); iter.hasNext(); ) { SimpleNode element = iter.next(); SimpleNode otElement = otIt.next(); if (element.beginColumn != otElement.beginColumn) { return false; } if (element.beginLine != otElement.beginLine) { return false; } if (!element.getClass().equals(otElement.getClass())) { return false; } String rep1 = NodeUtils.getFullRepresentationString(element); String rep2 = NodeUtils.getFullRepresentationString(otElement); if (rep1 == null || rep2 == null) { if (rep1 != rep2) { return false; } } else if (!rep1.equals(rep2)) { return false; } } return true; }
protected final void addSpecialsBefore(SimpleNode from, SimpleNode to) { if (from.specialsBefore != null && from.specialsBefore.size() > 0) { to.getSpecialsBefore().addAll(from.specialsBefore); } if (from.specialsAfter != null && from.specialsAfter.size() > 0) { to.getSpecialsBefore().addAll(from.specialsAfter); } }
protected final SimpleNode makeDecorator(java.util.List<SimpleNode> nodes) { exprType starargs = null; exprType kwargs = null; exprType func = null; ArrayList<SimpleNode> keywordsl = new ArrayList<SimpleNode>(); ArrayList<SimpleNode> argsl = new ArrayList<SimpleNode>(); for (Iterator<SimpleNode> iter = nodes.iterator(); iter.hasNext(); ) { SimpleNode node = iter.next(); if (node.getId() == JJTEXTRAKEYWORDVALUELIST) { final ExtraArgValue extraArg = (ExtraArgValue) node; kwargs = (extraArg).value; this.addSpecialsAndClearOriginal(extraArg, kwargs); extraArg.specialsBefore = kwargs.getSpecialsBefore(); extraArg.specialsAfter = kwargs.getSpecialsAfter(); } else if (node.getId() == JJTEXTRAARGVALUELIST) { final ExtraArgValue extraArg = (ExtraArgValue) node; starargs = extraArg.value; this.addSpecialsAndClearOriginal(extraArg, starargs); extraArg.specialsBefore = starargs.getSpecialsBefore(); extraArg.specialsAfter = starargs.getSpecialsAfter(); } else if (node instanceof keywordType) { // keyword keywordsl.add(node); } else if (isArg(node)) { // default argsl.add(node); } else if (node instanceof Comprehension) { argsl.add( new ListComp( (exprType) iter.next(), new comprehensionType[] {(comprehensionType) node})); } else if (node instanceof ComprehensionCollection) { // list comp (2 nodes: comp type and the elt -- what does elt mean by the way?) argsl.add( new ListComp((exprType) iter.next(), ((ComprehensionCollection) node).getGenerators())); } else if (node instanceof decoratorsType) { func = (exprType) stack.popNode(); // the func is the last thing in the stack decoratorsType d = (decoratorsType) node; d.func = func; d.args = (exprType[]) argsl.toArray(new exprType[0]); d.keywords = (keywordType[]) keywordsl.toArray(new keywordType[0]); d.starargs = starargs; d.kwargs = kwargs; return d; } else { argsl.add(node); } } throw new RuntimeException("Something wrong happened while making the decorators..."); }
/** * @param request this is the request for the completion * @param theList OUT - returned completions are added here. (IToken instances) * @param getOnlySupers whether we should only get things from super classes (in this case, we * won't get things from the current class) * @param checkIfInCorrectScope if true, we'll first check if we're in a scope that actually has a * method with 'self' or 'cls' * @return true if we actually tried to get the completions for self or cls. * @throws MisconfigurationException */ @SuppressWarnings("unchecked") public static boolean getSelfOrClsCompletions( CompletionRequest request, List theList, ICompletionState state, boolean getOnlySupers, boolean checkIfInCorrectScope, String lookForRep) throws MisconfigurationException { SimpleNode s = PyParser.reparseDocument( new PyParser.ParserInfo(request.doc, true, request.nature, state.getLine())) .o1; if (s != null) { FindScopeVisitor visitor = new FindScopeVisitor(state.getLine(), 0); try { s.accept(visitor); if (checkIfInCorrectScope) { boolean scopeCorrect = false; FastStack<SimpleNode> scopeStack = visitor.scope.getScopeStack(); for (Iterator<SimpleNode> it = scopeStack.topDownIterator(); scopeCorrect == false && it.hasNext(); ) { SimpleNode node = it.next(); if (node instanceof FunctionDef) { FunctionDef funcDef = (FunctionDef) node; if (funcDef.args != null && funcDef.args.args != null && funcDef.args.args.length > 0) { // ok, we have some arg, let's check for self or cls String rep = NodeUtils.getRepresentationString(funcDef.args.args[0]); if (rep != null && (rep.equals("self") || rep.equals("cls"))) { scopeCorrect = true; } } } } if (!scopeCorrect) { return false; } } if (lookForRep.equals("self")) { state.setLookingFor(ICompletionState.LOOKING_FOR_INSTANCED_VARIABLE); } else { state.setLookingFor(ICompletionState.LOOKING_FOR_CLASSMETHOD_VARIABLE); } getSelfOrClsCompletions(visitor.scope, request, theList, state, getOnlySupers); } catch (Exception e1) { PydevPlugin.log(e1); } return true; } return false; }
/** @return an iterator that'll get the outline entries for the given ast. */ public static Tuple<DefinitionsASTIteratorVisitor, Iterator<ASTEntry>> getInnerEntriesForAST( SimpleNode node) throws Exception { DefinitionsASTIteratorVisitor visitor = new DefinitionsASTIteratorVisitor(); node.accept(visitor); Iterator<ASTEntry> entries = visitor.getOutline(); return new Tuple<DefinitionsASTIteratorVisitor, Iterator<ASTEntry>>(visitor, entries); }
@Override public void traverse(SimpleNode node) throws Exception { if (nodeHelper.isFunctionDef(node)) { traverse((FunctionDef) node); } else { node.traverse(this); } }
NameTok[] getVargAndKwarg(java.util.List<SimpleNode> args) throws Exception { NameTok varg = null; NameTok kwarg = null; for (Iterator<SimpleNode> iter = args.iterator(); iter.hasNext(); ) { SimpleNode node = iter.next(); if (node.getId() == JJTEXTRAKEYWORDLIST) { ExtraArg a = (ExtraArg) node; kwarg = a.tok; addSpecialsAndClearOriginal(a, kwarg); } else if (node.getId() == JJTEXTRAARGLIST) { ExtraArg a = (ExtraArg) node; varg = a.tok; addSpecialsAndClearOriginal(a, varg); } } return new NameTok[] {varg, kwarg}; }
public static <T extends VisitorIF> T createVisitor(Class<T> visitorClass, SimpleNode root) { T visitor = null; try { visitor = visitorClass.newInstance(); root.accept(visitor); } catch (Throwable e) { throw new RuntimeException(e); } return visitor; }
@Override public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException { AbstractScopeNode<?> scope = info.getScopeAdapter(); ITextSelection selection = info.getUserSelection(); SimpleNode node = scope.getASTNode(); LocalVariablesVisitor visitor = new LocalVariablesVisitor(); try { node.accept(visitor); } catch (Exception e) { throw new RuntimeException(e); } List<Name> variables = visitor.getVariables(); Name selectedVariable = findSelectedVariable(selection, variables); if (selectedVariable == null) { status.addFatalError(Messages.validationNoNameSelected); return status; } List<Name> relatedVariables = findAllRelatedVariables(variables, selectedVariable); Assign assignment = findAssignment(relatedVariables); if (assignment == null) { String id = selectedVariable.id; status.addFatalError(Messages.format(Messages.inlinelocalNoAssignment, id)); return status; } if (!isValid(relatedVariables)) { return status; } requestProcessor.setAssign(assignment); requestProcessor.setVariables(relatedVariables); return status; }
/** Unchecked (because if doing Class.cast, it does not work in java 1.4) */ @SuppressWarnings("unchecked") public static <T extends AbstractContextVisitor> T createContextVisitor( Class<T> visitorClass, SimpleNode root, ModuleAdapter module, AbstractNodeAdapter parent) { try { T visitor = (T) visitorClass.getConstructors()[0].newInstance(new Object[] {module, parent}); root.accept(visitor); return visitor; } catch (Exception e) { throw new RuntimeException(e); } }
public boolean equals(Object obj) { MemoVisitor other = (MemoVisitor) obj; Iterator<SimpleNode> iter1 = other.visited.iterator(); for (Iterator<SimpleNode> iter = visited.iterator(); iter.hasNext(); ) { SimpleNode n = (SimpleNode) iter.next(); SimpleNode n1 = null; try { n1 = (SimpleNode) iter1.next(); } catch (NoSuchElementException e) { throw new RuntimeException("Just received " + n, e); } if (n instanceof Expr && n1 instanceof Expr) { continue; } print(n.getClass()); if (n.getClass().equals(n1.getClass()) == false) { print("n.getClass() != n1.getClass() " + n.getClass() + " != " + n1.getClass()); return false; } // if(n.beginColumn != n1.beginColumn){ // print("n = "+n+" n1 = "+n1); // print("n = "+NodeUtils.getFullRepresentationString(n)+" n1 = // "+NodeUtils.getFullRepresentationString(n1)); // print("n.beginColumn != n1.beginColumn "+ n.beginColumn +" != "+ // n1.beginColumn); // return false; // } // if(n.beginLine != n1.beginLine){ // print("n.beginLine != n1.beginLine "+ n.beginLine +" != "+ n1.beginLine); // return false; // } String s1 = NodeUtils.getFullRepresentationString(n); String s2 = NodeUtils.getFullRepresentationString(n1); if ((s1 == null && s2 != null) || (s1 != null && s2 == null)) { print("(s1 == null && s2 != null) || (s1 != null && s2 == null)"); return false; } if (s1.equals(s2.replaceAll("\r", "")) == false) { print("s1 != s2 \n-->" + s1 + "<--\n!=\n-->" + s2 + "<--"); return false; } } return true; }
public void visit(SimpleNode node) throws Exception { if (node == null) { return; } node.accept(this); }
public void traverse(SimpleNode node) throws Exception { node.traverse(this); }
public final SimpleNode onCloseNode(SimpleNode n, int arity) throws Exception { exprType value; exprType[] exprs; Suite orelseSuite; stmtType[] body; Suite suite; int l; switch (n.getId()) { case JJTEXPR_STMT: value = (exprType) stack.popNode(); 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 = (exprType) stack.popNode(); return new Subscript(value, slice, Subscript.Load); case JJTPRINT_STMT: boolean nl = true; if (stack.nodeArity() == 0) { Print p = new Print(null, null, true); return p; } if (stack.peekNode().getId() == JJTCOMMA) { stack.popNode(); nl = false; } Print p = new Print(null, makeExprs(), nl); return p; case JJTPRINTEXT_STMT: nl = true; if (stack.peekNode().getId() == JJTCOMMA) { stack.popNode(); nl = false; } exprs = makeExprs(stack.nodeArity() - 1); p = new Print(((exprType) stack.popNode()), exprs, nl); return p; case JJTBEGIN_FOR_ELSE_STMT: return new Suite(null); case JJTBEGIN_ELSE_STMT: return new Suite(null); case JJTBEGIN_WHILE_STMT: return new While(null, null, null); case JJTWHILE_STMT: orelseSuite = null; if (stack.nodeArity() == 5) { orelseSuite = popSuiteAndSuiteType(); } body = popSuite(); exprType test = (exprType) stack.popNode(); While w = (While) stack.popNode(); w.test = test; w.body = body; w.orelse = orelseSuite; return w; case JJTDECORATED: if (stack.nodeArity() != 2) { throw new RuntimeException("Expected 2 nodes at this context, found: " + arity); } SimpleNode def = stack.popNode(); Decorators decorators = (Decorators) stack.popNode(); if (def instanceof ClassDef) { ClassDef classDef = (ClassDef) def; classDef.decs = decorators.exp; } else { FunctionDef fDef = (FunctionDef) def; fDef.decs = decorators.exp; } return def; case JJTCALL_OP: exprType starargs = null; exprType kwargs = null; java.util.List<exprType> args = new ArrayList<exprType>(); java.util.List<keywordType> keywords = new ArrayList<keywordType>(); for (int i = arity - 2; i >= 0; i--) { SimpleNode node = stack.popNode(); if (node instanceof keywordType) { keywords.add(0, (keywordType) node); } else if (node.getId() == JJTEXTRAARGVALUELIST) { ExtraArgValue nstarargs = (ExtraArgValue) node; starargs = nstarargs.value; this.addSpecialsAndClearOriginal(nstarargs, starargs); } else if (node.getId() == JJTEXTRAKEYWORDVALUELIST) { ExtraArgValue nkwargs = (ExtraArgValue) node; kwargs = nkwargs.value; this.addSpecialsAndClearOriginal(nkwargs, kwargs); } else if (node instanceof ComprehensionCollection) { // what can happen is something like print sum(x for x in y), where we have already // passed x in the args, and then get 'for x in y' args.add( 0, new ListComp( (exprType) stack.popNode(), ((ComprehensionCollection) node).getGenerators(), ListComp.EmptyCtx)); i--; // popped node } else { args.add(0, (exprType) node); } } exprType func = (exprType) stack.popNode(); Call c = new Call( func, args.toArray(new exprType[args.size()]), keywords.toArray(new keywordType[keywords.size()]), starargs, kwargs); addSpecialsAndClearOriginal(n, c); return c; case JJTFUNCDEF: suite = (Suite) stack.popNode(); body = suite.body; argumentsType arguments = makeArguments(stack.nodeArity() - 1); NameTok nameTok = makeName(NameTok.FunctionName); // decorator is always null at this point... it's decorated later on FunctionDef funcDef = new FunctionDef(nameTok, arguments, body, null, null); addSpecialsAndClearOriginal(suite, funcDef); setParentForFuncOrClass(body, funcDef); return funcDef; case JJTDEFAULTARG: value = (arity == 1) ? null : ((exprType) stack.popNode()); return new DefaultArg(((exprType) stack.popNode()), value, n.getId()); case JJTEXTRAARGLIST: return new ExtraArg(makeName(NameTok.VarArg), JJTEXTRAARGLIST); case JJTEXTRAKEYWORDLIST: return new ExtraArg(makeName(NameTok.KwArg), JJTEXTRAKEYWORDLIST); case JJTCLASSDEF: suite = (Suite) stack.popNode(); body = suite.body; exprType[] bases = makeExprs(stack.nodeArity() - 1); nameTok = makeName(NameTok.ClassName); ClassDef classDef = new ClassDef(nameTok, bases, body, null, null, null, null); addSpecialsAndClearOriginal(suite, classDef); setParentForFuncOrClass(body, classDef); return classDef; case JJTBEGIN_RETURN_STMT: return new Return(null); case JJTRETURN_STMT: value = arity == 2 ? ((exprType) stack.popNode()) : null; Return ret = (Return) stack.popNode(); ret.value = value; return ret; case JJTYIELD_STMT: return stack.popNode(); case JJTYIELD_EXPR: exprType yieldExpr = null; if (arity > 0) { // we may have an empty yield, so, we have to check it before yieldExpr = (exprType) stack.popNode(); } return new Yield(yieldExpr, false); case JJTRAISE_STMT: exprType tback = arity >= 3 ? ((exprType) stack.popNode()) : null; exprType inst = arity >= 2 ? ((exprType) stack.popNode()) : null; exprType type = arity >= 1 ? ((exprType) stack.popNode()) : null; return new Raise(type, inst, tback, null); case JJTGLOBAL_STMT: Global global = new Global(makeIdentifiers(NameTok.GlobalName), null); return global; case JJTASSERT_STMT: exprType msg = arity == 2 ? ((exprType) stack.popNode()) : null; test = (exprType) stack.popNode(); return new Assert(test, msg); case JJTBEGIN_TRY_STMT: // we do that just to get the specials return new TryExcept(null, null, null); case JJTTRYELSE_STMT: orelseSuite = popSuiteAndSuiteType(); return orelseSuite; case JJTTRYFINALLY_OUTER_STMT: orelseSuite = popSuiteAndSuiteType(); return new TryFinally( null, orelseSuite); // it does not have a body at this time... it will be filled with the // inner try..except case JJTTRY_STMT: TryFinally outer = null; if (stack.peekNode() instanceof TryFinally) { outer = (TryFinally) stack.popNode(); arity--; } orelseSuite = null; if (stack.peekNode() instanceof suiteType) { orelseSuite = (Suite) stack.popNode(); arity--; } l = arity; excepthandlerType[] handlers = new excepthandlerType[l]; for (int i = l - 1; i >= 0; i--) { handlers[i] = (excepthandlerType) stack.popNode(); } suite = (Suite) stack.popNode(); TryExcept tryExc = (TryExcept) stack.popNode(); if (outer != null) { outer.beginLine = tryExc.beginLine; } tryExc.body = suite.body; tryExc.handlers = handlers; tryExc.orelse = orelseSuite; addSpecials(suite, tryExc); if (outer == null) { return tryExc; } else { if (outer.body != null) { throw new RuntimeException( "Error. Expecting null body to be filled on try..except..finally"); } outer.body = new stmtType[] {tryExc}; return outer; } case JJTBEGIN_TRY_ELSE_STMT: // we do that just to get the specials return new Suite(null); case JJTBEGIN_EXCEPT_CLAUSE: return new excepthandlerType(null, null, null); case JJTEXCEPT_CLAUSE: suite = (Suite) stack.popNode(); body = suite.body; exprType excname = arity == 4 ? ((exprType) stack.popNode()) : null; if (excname != null) { ctx.setStore(excname); } type = arity >= 3 ? ((exprType) stack.popNode()) : null; excepthandlerType handler = (excepthandlerType) stack.popNode(); handler.type = type; handler.name = excname; handler.body = body; addSpecials(suite, handler); return handler; case JJTBEGIN_FINALLY_STMT: // we do that just to get the specials return new Suite(null); case JJTTRYFINALLY_STMT: suiteType finalBody = popSuiteAndSuiteType(); body = popSuite(); // We have a try..except in the stack, but we will change it for a try..finally // This is because we recognize a try..except in the 'try:' token, but actually end up with // a try..finally TryExcept tryExcept = (TryExcept) stack.popNode(); TryFinally tryFinally = new TryFinally(body, finalBody); tryFinally.beginLine = tryExcept.beginLine; tryFinally.beginColumn = tryExcept.beginColumn; addSpecialsAndClearOriginal(tryExcept, tryFinally); return tryFinally; case JJTWITH_STMT: return makeWithStmt(arity); case JJTWITH_ITEM: return makeWithItem(arity); case JJTEXTRAKEYWORDVALUELIST: return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAKEYWORDVALUELIST); case JJTEXTRAARGVALUELIST: return new ExtraArgValue(((exprType) stack.popNode()), JJTEXTRAARGVALUELIST); case JJTKEYWORD: value = (exprType) stack.popNode(); nameTok = makeName(NameTok.KeywordName); return new keywordType(nameTok, value, false); case JJTTUPLE: if (stack.nodeArity() > 0) { SimpleNode peeked = stack.peekNode(); if (peeked instanceof ComprehensionCollection) { ComprehensionCollection col = (ComprehensionCollection) stack.popNode(); return new ListComp( ((exprType) stack.popNode()), col.getGenerators(), ListComp.TupleCtx); } } return makeTuple(n); case JJTLIST: if (stack.nodeArity() > 0 && stack.peekNode() instanceof ComprehensionCollection) { ComprehensionCollection col = (ComprehensionCollection) stack.popNode(); return new ListComp(((exprType) stack.popNode()), col.getGenerators(), ListComp.ListCtx); } return new List(makeExprs(), List.Load); case JJTSET: return new Set(null); case JJTDICTIONARY: return makeDictionaryOrSet(arity); case JJTSTR_1OP: return new Repr(((exprType) stack.popNode())); case JJTTEST: if (arity == 2) { IfExp node = (IfExp) stack.popNode(); node.body = (exprType) stack.popNode(); return node; } else { return stack.popNode(); } case JJTIF_EXP: exprType ifExprOrelse = (exprType) stack.popNode(); exprType ifExprTest = (exprType) stack.popNode(); return new IfExp(ifExprTest, null, ifExprOrelse); case JJTOLD_LAMBDEF: case JJTLAMBDEF: test = (exprType) stack.popNode(); arguments = makeArguments(arity - 1); Lambda lambda = new Lambda(arguments, test); // if(arguments == null || arguments.args == null || arguments.args.length == 0){ // lambda.getSpecialsBefore().add("lambda"); // }else{ // lambda.getSpecialsBefore().add("lambda "); // } return lambda; case JJTELLIPSIS: return new Ellipsis(); case JJTSLICE: SimpleNode[] arr = new SimpleNode[arity]; for (int i = arity - 1; i >= 0; i--) { arr[i] = stack.popNode(); } exprType[] values = new exprType[3]; int k = 0; java.util.List<Object> specialsBefore = new ArrayList<Object>(); java.util.List<Object> specialsAfter = new ArrayList<Object>(); for (int j = 0; j < arity; j++) { if (arr[j].getId() == JJTCOLON) { if (arr[j].specialsBefore != null) { specialsBefore.addAll(arr[j].specialsBefore); arr[j].specialsBefore .clear(); // this nodes may be reused among parses, so, we have to erase the // specials } if (arr[j].specialsAfter != null) { specialsAfter.addAll(arr[j].specialsAfter); arr[j].specialsAfter.clear(); } k++; } else { values[k] = (exprType) arr[j]; if (specialsBefore.size() > 0) { values[k].getSpecialsBefore().addAll(specialsBefore); specialsBefore.clear(); } if (specialsAfter.size() > 0) { values[k].getSpecialsBefore().addAll(specialsAfter); specialsAfter.clear(); } } } SimpleNode sliceRet; if (k == 0) { sliceRet = new Index(values[0]); } else { sliceRet = new Slice(values[0], values[1], values[2]); } // this may happen if we have no values sliceRet.getSpecialsBefore().addAll(specialsBefore); sliceRet.getSpecialsAfter().addAll(specialsAfter); specialsBefore.clear(); specialsAfter.clear(); return sliceRet; case JJTCOMP_FOR: return makeCompFor(arity); case JJTIMPORTFROM: return makeImportFrom25Onwards(arity); default: Log.log(("Error at TreeBuilder: default not treated:" + n.getId())); return null; } }