Search in sources :

Example 1 with SimpleNode

use of org.python.pydev.parser.jython.SimpleNode in project Pydev by fabioz.

the class IOUtils method addAstInfo.

/**
 * Adds ast info information for a module.
 *
 * @param m the module we want to add to the info
 */
public List<IInfo> addAstInfo(SimpleNode node, ModulesKey key, boolean generateDelta) {
    List<IInfo> createdInfos = new ArrayList<IInfo>();
    if (node == null || key.name == null) {
        return createdInfos;
    }
    try {
        Tuple<DefinitionsASTIteratorVisitor, Iterator<ASTEntry>> tup = getInnerEntriesForAST(node);
        if (DebugSettings.DEBUG_ANALYSIS_REQUESTS) {
            org.python.pydev.shared_core.log.ToLogFile.toLogFile(this, "Adding ast info to: " + key.name);
        }
        try {
            Iterator<ASTEntry> entries = tup.o2;
            FastStack<SimpleNode> tempStack = new FastStack<SimpleNode>(10);
            synchronized (this.lock) {
                synchronized (ObjectsInternPool.lock) {
                    final String file = key.file != null ? ObjectsInternPool.internUnsynched(key.file.toString()) : null;
                    key.name = ObjectsInternPool.internUnsynched(key.name);
                    while (entries.hasNext()) {
                        ASTEntry entry = entries.next();
                        IInfo infoCreated = null;
                        if (entry.parent == null) {
                            // we only want those that are in the global scope
                            if (entry.node instanceof ClassDef) {
                                // no intern construct (locked in this loop)
                                NameTok name = (NameTok) ((ClassDef) entry.node).name;
                                ClassInfo info = new ClassInfo(ObjectsInternPool.internUnsynched(name.id), key.name, null, false, getNature(), file, name.beginLine, name.beginColumn);
                                add(info, TOP_LEVEL);
                                infoCreated = info;
                            } else if (entry.node instanceof FunctionDef) {
                                // no intern construct (locked in this loop)
                                NameTok name = (NameTok) ((FunctionDef) entry.node).name;
                                FuncInfo info2 = new FuncInfo(ObjectsInternPool.internUnsynched(name.id), key.name, null, false, getNature(), file, name.beginLine, name.beginColumn);
                                add(info2, TOP_LEVEL);
                                infoCreated = info2;
                            } else {
                                // it is an assign
                                infoCreated = this.addAssignTargets(entry, key.name, TOP_LEVEL, null, false, file);
                            }
                        } else {
                            if (entry.node instanceof ClassDef || entry.node instanceof FunctionDef) {
                                // ok, it has a parent, so, let's check to see if the path we got only has class definitions
                                // as the parent (and get that path)
                                Tuple<String, Boolean> pathToRoot = this.getPathToRoot(entry, false, false, tempStack);
                                if (pathToRoot != null && pathToRoot.o1 != null && pathToRoot.o1.length() > 0) {
                                    if (entry.node instanceof ClassDef) {
                                        NameTok name = ((NameTok) ((ClassDef) entry.node).name);
                                        ClassInfo info = new ClassInfo(ObjectsInternPool.internUnsynched(name.id), key.name, ObjectsInternPool.internUnsynched(pathToRoot.o1), false, getNature(), file, name.beginLine, name.beginColumn);
                                        add(info, INNER);
                                        infoCreated = info;
                                    } else {
                                        // FunctionDef
                                        NameTok name = ((NameTok) ((FunctionDef) entry.node).name);
                                        FuncInfo info2 = new FuncInfo(ObjectsInternPool.internUnsynched(name.id), key.name, ObjectsInternPool.internUnsynched(pathToRoot.o1), false, getNature(), file, name.beginLine, name.beginColumn);
                                        add(info2, INNER);
                                        infoCreated = info2;
                                    }
                                }
                            } else {
                                // it is an assign
                                Tuple<String, Boolean> pathToRoot = this.getPathToRoot(entry, true, false, tempStack);
                                if (pathToRoot != null && pathToRoot.o1 != null && pathToRoot.o1.length() > 0) {
                                    infoCreated = this.addAssignTargets(entry, key.name, INNER, pathToRoot.o1, pathToRoot.o2, file);
                                }
                            }
                        }
                        if (infoCreated != null) {
                            createdInfos.add(infoCreated);
                        }
                    }
                // end while
                }
            // end lock ObjectsPool.lock
            }
        // end this.lock
        } catch (Exception e) {
            Log.log(e);
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return createdInfos;
}
Also used : FastStack(org.python.pydev.shared_core.structure.FastStack) ArrayList(java.util.ArrayList) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) IOException(java.io.IOException) MisconfigurationException(org.python.pydev.core.MisconfigurationException) SimpleNode(org.python.pydev.parser.jython.SimpleNode) IInfo(org.python.pydev.core.IInfo) ClassDef(org.python.pydev.parser.jython.ast.ClassDef) Iterator(java.util.Iterator) ASTEntry(org.python.pydev.parser.visitors.scope.ASTEntry) DefinitionsASTIteratorVisitor(org.python.pydev.parser.visitors.scope.DefinitionsASTIteratorVisitor) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Example 2 with SimpleNode

use of org.python.pydev.parser.jython.SimpleNode in project Pydev by fabioz.

the class IOUtils method addAstInfo.

public List<IInfo> addAstInfo(ModulesKey key, boolean generateDelta) throws Exception {
    if (key instanceof ModulesKeyForFolder) {
        addModulesKeyForFolderToIndex(key, generateDelta);
        return new ArrayList<IInfo>(0);
    }
    boolean isZipModule = key instanceof ModulesKeyForZip;
    ModulesKeyForZip modulesKeyForZip = null;
    if (isZipModule) {
        modulesKeyForZip = (ModulesKeyForZip) key;
    }
    Object doc;
    if (isZipModule) {
        doc = FileUtilsFileBuffer.getCustomReturnFromZip(modulesKeyForZip.file, modulesKeyForZip.zipModulePath, null);
    } else {
        doc = FileUtilsFileBuffer.getCustomReturnFromFile(key.file, true, null);
    }
    char[] charArray;
    int len;
    if (doc instanceof IDocument) {
        IDocument document = (IDocument) doc;
        charArray = document.get().toCharArray();
        len = charArray.length;
    } else if (doc instanceof FastStringBuffer) {
        FastStringBuffer fastStringBuffer = (FastStringBuffer) doc;
        // In this case, we can actually get the internal array without doing any copies (and just specifying the len).
        charArray = fastStringBuffer.getInternalCharsArray();
        len = fastStringBuffer.length();
    } else if (doc instanceof String) {
        String str = (String) doc;
        charArray = str.toCharArray();
        len = charArray.length;
    } else if (doc instanceof char[]) {
        charArray = (char[]) doc;
        len = charArray.length;
    } else {
        throw new RuntimeException("Don't know how to handle: " + doc + " -- " + doc.getClass());
    }
    SimpleNode node = FastDefinitionsParser.parse(charArray, key.file.getName(), len, key.file);
    if (node == null) {
        return null;
    }
    return addAstInfo(node, key, generateDelta);
}
Also used : FastStringBuffer(org.python.pydev.shared_core.string.FastStringBuffer) ArrayList(java.util.ArrayList) ModulesKeyForZip(org.python.pydev.core.ModulesKeyForZip) IDocument(org.eclipse.jface.text.IDocument) ModulesKeyForFolder(org.python.pydev.core.ModulesKeyForFolder) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 3 with SimpleNode

use of org.python.pydev.parser.jython.SimpleNode in project Pydev by fabioz.

the class ArgumentsChecker method checkNameFound.

/*default*/
void checkNameFound(Call callNode, SourceToken sourceToken) throws Exception {
    FunctionDef functionDefinitionReferenced;
    boolean callingBoundMethod = false;
    SimpleNode ast = sourceToken.getAst();
    if (ast instanceof FunctionDef) {
        functionDefinitionReferenced = (FunctionDef) ast;
        analyzeCallAndFunctionMatch(callNode, functionDefinitionReferenced, sourceToken, callingBoundMethod);
    } else if (ast instanceof ClassDef) {
        ClassDef classDef = (ClassDef) ast;
        SimpleNode initNode = defToConsideredInit.get(classDef);
        callingBoundMethod = true;
        if (initNode == null) {
            String className = ((NameTok) classDef.name).id;
            Definition foundDef = sourceToken.getDefinition();
            IModule mod = this.current;
            if (foundDef != null) {
                mod = foundDef.module;
            }
            SimpleNode n = NodeUtils.getNodeFromPath(classDef, "__init__");
            if (n instanceof FunctionDef) {
                initNode = n;
            } else {
                IDefinition[] definition = mod.findDefinition(CompletionStateFactory.getEmptyCompletionState(className + ".__init__", this.nature, this.completionCache), -1, -1, this.nature);
                for (IDefinition iDefinition : definition) {
                    Definition d = (Definition) iDefinition;
                    if (d.ast instanceof FunctionDef) {
                        initNode = d.ast;
                        defToConsideredInit.put(classDef, initNode);
                        break;
                    }
                }
            }
        }
        if (initNode instanceof FunctionDef) {
            functionDefinitionReferenced = (FunctionDef) initNode;
            analyzeCallAndFunctionMatch(callNode, functionDefinitionReferenced, sourceToken, callingBoundMethod);
        }
    }
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) IModule(org.python.pydev.core.IModule) IDefinition(org.python.pydev.core.IDefinition) Definition(org.python.pydev.ast.codecompletion.revisited.visitors.Definition) PyRefactoringFindDefinition(org.python.pydev.ast.refactoring.PyRefactoringFindDefinition) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) IDefinition(org.python.pydev.core.IDefinition) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 4 with SimpleNode

use of org.python.pydev.parser.jython.SimpleNode in project Pydev by fabioz.

the class DuplicationChecker method endScope.

/**
 * we are ending a scope
 * @param node
 */
private void endScope(String name, SimpleNode node) {
    stack.pop();
    Map<String, SimpleNode> currScope = stack.peek();
    SimpleNode currNode = currScope.get(name);
    if (currNode == null || canReplaceScope(currNode, node)) {
        currScope.put(name, node);
    }
}
Also used : SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 5 with SimpleNode

use of org.python.pydev.parser.jython.SimpleNode in project Pydev by fabioz.

the class MessagesManager method addUnusedMessage.

/**
 * adds a message for something that was not used
 *
 * @param node the node representing the scope being closed when adding the
 *             unused message
 */
public void addUnusedMessage(SimpleNode node, Found f) {
    List<GenAndTok> all = f.getAll();
    int len = all.size();
    for (int i = 0; i < len; i++) {
        GenAndTok g = all.get(i);
        if (g.generator instanceof SourceToken) {
            SimpleNode ast = ((SourceToken) g.generator).getAst();
            // it can be an unused import
            boolean isFromImport = ast instanceof ImportFrom;
            if (isFromImport || ast instanceof Import) {
                if (isFromImport && AbstractVisitor.isWildImport((ImportFrom) ast)) {
                    addMessage(IAnalysisPreferences.TYPE_UNUSED_WILD_IMPORT, g.generator, g.tok);
                } else if (!(g.generator instanceof ImportPartSourceToken)) {
                    addMessage(IAnalysisPreferences.TYPE_UNUSED_IMPORT, g.generator, g.tok);
                }
                // finish it...
                continue;
            }
        }
        // we have to check if this is a name we should ignore
        if (startsWithNamesToIgnore(g)) {
            int type = IAnalysisPreferences.TYPE_UNUSED_VARIABLE;
            if (g.tok instanceof SourceToken) {
                SourceToken t = (SourceToken) g.tok;
                SimpleNode ast = t.getAst();
                if (ast instanceof NameTok) {
                    NameTok n = (NameTok) ast;
                    if (n.ctx == NameTok.KwArg || n.ctx == NameTok.VarArg || n.ctx == NameTok.KeywordName) {
                        type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
                    }
                } else if (ast instanceof Name) {
                    Name n = (Name) ast;
                    if (n.ctx == Name.Param || n.ctx == Name.KwOnlyParam) {
                        type = IAnalysisPreferences.TYPE_UNUSED_PARAMETER;
                    }
                }
            }
            boolean addMessage = true;
            if (type == IAnalysisPreferences.TYPE_UNUSED_PARAMETER) {
                if (node instanceof FunctionDef) {
                    addMessage = false;
                    FunctionDef def = (FunctionDef) node;
                    for (stmtType b : def.body) {
                        if (b instanceof Pass) {
                            continue;
                        }
                        if (b instanceof Expr) {
                            Expr expr = (Expr) b;
                            if (expr.value instanceof Str) {
                                continue;
                            }
                        }
                        addMessage = true;
                        break;
                    }
                }
            }
            if (addMessage) {
                addMessage(type, g.generator, g.tok);
            }
        }
    }
}
Also used : Import(org.python.pydev.parser.jython.ast.Import) ImportPartSourceToken(org.python.pydev.ast.codecompletion.revisited.visitors.AbstractVisitor.ImportPartSourceToken) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Name(org.python.pydev.parser.jython.ast.Name) Str(org.python.pydev.parser.jython.ast.Str) Pass(org.python.pydev.parser.jython.ast.Pass) Expr(org.python.pydev.parser.jython.ast.Expr) ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) ImportPartSourceToken(org.python.pydev.ast.codecompletion.revisited.visitors.AbstractVisitor.ImportPartSourceToken) SourceToken(org.python.pydev.ast.codecompletion.revisited.modules.SourceToken) NameTok(org.python.pydev.parser.jython.ast.NameTok)

Aggregations

SimpleNode (org.python.pydev.parser.jython.SimpleNode)833 ParseException (org.python.pydev.parser.jython.ParseException)516 TokenMgrError (org.python.pydev.parser.jython.TokenMgrError)85 Token (org.python.pydev.parser.jython.Token)71 ArrayList (java.util.ArrayList)47 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)39 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)37 NameTok (org.python.pydev.parser.jython.ast.NameTok)35 org.python.pydev.parser.jython.ast.exprType (org.python.pydev.parser.jython.ast.exprType)30 ASTEntry (org.python.pydev.parser.visitors.scope.ASTEntry)30 Module (org.python.pydev.parser.jython.ast.Module)28 Name (org.python.pydev.parser.jython.ast.Name)28 Document (org.eclipse.jface.text.Document)27 ISimpleNode (org.python.pydev.shared_core.model.ISimpleNode)27 ParseOutput (org.python.pydev.shared_core.parsing.BaseParser.ParseOutput)27 SourceToken (org.python.pydev.ast.codecompletion.revisited.modules.SourceToken)26 PyParser (org.python.pydev.parser.PyParser)25 Assign (org.python.pydev.parser.jython.ast.Assign)24 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)24 TokensList (org.python.pydev.core.TokensList)21