Search in sources :

Example 1 with ParseOutput

use of org.python.pydev.shared_core.parsing.BaseParser.ParseOutput in project Pydev by fabioz.

the class MatchImportsVisitorTest method testMatchImports2.

public void testMatchImports2() throws Exception {
    Document doc = new Document("" + // rename a.b.c
    "import a.b.c.d\n" + // rename a.b.c
    "import a.b.c\n" + "import a.b\n" + "");
    IPythonNature nature = new PythonNatureStub();
    ParseOutput obj = PyParser.reparseDocument(new PyParser.ParserInfo(doc, nature));
    SourceModule module = (SourceModule) AbstractModule.createModule((SimpleNode) obj.ast, null, "z", null);
    MatchImportsVisitor visitor = new MatchImportsVisitor(nature, "a.b.c", module, null);
    module.getAst().accept(visitor);
    assertEquals(visitor.importsMatchingOnAliasPart.size(), 2);
    assertEquals(visitor.occurrences.size(), 2);
}
Also used : SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule) ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) IPythonNature(org.python.pydev.core.IPythonNature) PyParser(org.python.pydev.parser.PyParser) MatchImportsVisitor(com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor) Document(org.eclipse.jface.text.Document) PythonNatureStub(org.python.pydev.parser.PythonNatureStub) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 2 with ParseOutput

use of org.python.pydev.shared_core.parsing.BaseParser.ParseOutput in project Pydev by fabioz.

the class GenCythonAstImpl method jsonToParseOutput.

private ParseOutput jsonToParseOutput(ParserInfo p, String cythonJson, long modifiedTime) {
    JsonValue json = JsonValue.readFrom(cythonJson);
    JsonObject asObject = json.asObject();
    JsonValue errors = asObject.get("errors");
    ParseException exc = null;
    for (JsonValue v : errors.asArray()) {
        JsonObject objError = v.asObject();
        JsonValue lineValue = objError.get("line");
        JsonValue colValue = objError.get("col");
        JsonValue messageValue = objError.get("message_only");
        exc = new ParseException(messageValue.asString(), lineValue.asInt(), colValue.asInt());
    }
    JsonValue ast = asObject.get("ast");
    if (ast == null || !ast.isObject()) {
        ParseOutput parseOutput = new ParseOutput(null, exc, modifiedTime);
        parseOutput.isCython = true;
        return parseOutput;
    } else {
        JsonValue body = ast.asObject().get("stats");
        if (body != null && body.isArray()) {
            // System.out.println(body.toPrettyString());
            JsonToNodesBuilder builder = new JsonToNodesBuilder(p);
            JsonArray asArray = body.asArray();
            Iterator<JsonValue> iterator = asArray.iterator();
            while (iterator.hasNext()) {
                JsonValue node = iterator.next();
                try {
                    builder.addStatement(node);
                } catch (Exception e) {
                    log("Error converting cython json to ast: " + node, e);
                }
            }
            ISimpleNode mod = builder.createModule();
            ParseOutput parseOutput = new ParseOutput(mod, null, modifiedTime);
            parseOutput.isCython = true;
            return parseOutput;
        }
    }
    return null;
}
Also used : JsonArray(org.python.pydev.json.eclipsesource.JsonArray) ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) JsonValue(org.python.pydev.json.eclipsesource.JsonValue) JsonObject(org.python.pydev.json.eclipsesource.JsonObject) ParseException(org.python.pydev.parser.jython.ParseException) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) ParseException(org.python.pydev.parser.jython.ParseException)

Example 3 with ParseOutput

use of org.python.pydev.shared_core.parsing.BaseParser.ParseOutput in project Pydev by fabioz.

the class GraphView method loadGraph.

private void loadGraph(String fileName) throws FileNotFoundException, IOException, Throwable {
    ASTGraph ast = new ASTGraph();
    ParseOutput objects = ast.parseFile(fileName);
    graph.setGraphLayoutCache(new GraphLayoutCache());
    DefaultGraphCell[] cells = ast.generateTree((SimpleNode) objects.ast);
    graph.getGraphLayoutCache().insert(cells);
    graph.clearSelection();
}
Also used : ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) DefaultGraphCell(org.jgraph.graph.DefaultGraphCell) GraphLayoutCache(org.jgraph.graph.GraphLayoutCache)

Example 4 with ParseOutput

use of org.python.pydev.shared_core.parsing.BaseParser.ParseOutput in project Pydev by fabioz.

the class CodeFoldingVisitorTest method testWith.

public void testWith() throws Exception {
    CodeFoldingVisitor visitor = new CodeFoldingVisitor();
    String str = "" + "from __future__ import with_statement\n" + "with a:\n" + "    print a\n" + "";
    ParseOutput objects = PyParser.reparseDocument(new PyParser.ParserInfo(new Document(str), IPythonNature.GRAMMAR_PYTHON_VERSION_2_5, null));
    SimpleNode root = (SimpleNode) objects.ast;
    root.accept(visitor);
    Iterator<ASTEntry> iterator = visitor.getIterator();
    check((ASTEntryWithChildren) iterator.next(), "from __future__ import with_statement", 6, 1, 1, 0);
    check((ASTEntryWithChildren) iterator.next(), "With", 1, 2, 3, 0);
    assertTrue(iterator.hasNext() == false);
}
Also used : ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) PyParser(org.python.pydev.parser.PyParser) Document(org.eclipse.jface.text.Document) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 5 with ParseOutput

use of org.python.pydev.shared_core.parsing.BaseParser.ParseOutput in project Pydev by fabioz.

the class CodeFoldingVisitorTest method testTryFinally.

public void testTryFinally() throws Exception {
    CodeFoldingVisitor visitor = new CodeFoldingVisitor();
    String str = "" + "try:\n" + "    print 4\n" + "finally:\n" + "    print 5\n" + "\n" + "";
    ParseOutput objects = PyParser.reparseDocument(new PyParser.ParserInfo(new Document(str), IPythonNature.GRAMMAR_PYTHON_VERSION_2_5, null));
    SimpleNode root = (SimpleNode) objects.ast;
    root.accept(visitor);
    Iterator<ASTEntry> iterator = visitor.getIterator();
    check((ASTEntryWithChildren) iterator.next(), "TryFinally", 1, 1, 4, 0);
    assertTrue(iterator.hasNext() == false);
}
Also used : ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) PyParser(org.python.pydev.parser.PyParser) Document(org.eclipse.jface.text.Document) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Aggregations

ParseOutput (org.python.pydev.shared_core.parsing.BaseParser.ParseOutput)42 Document (org.eclipse.jface.text.Document)31 PyParser (org.python.pydev.parser.PyParser)28 SimpleNode (org.python.pydev.parser.jython.SimpleNode)27 IModule (org.python.pydev.core.IModule)7 ParserInfo (org.python.pydev.parser.PyParser.ParserInfo)7 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)6 ParseException (org.python.pydev.parser.jython.ParseException)6 TokensList (org.python.pydev.core.TokensList)5 File (java.io.File)4 GenCythonAstImpl (org.python.pydev.ast.cython.GenCythonAstImpl)4 MisconfigurationException (org.python.pydev.core.MisconfigurationException)4 MatchImportsVisitor (com.python.pydev.analysis.refactoring.wizards.rename.MatchImportsVisitor)3 IDocument (org.eclipse.jface.text.IDocument)3 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)3 IPythonNature (org.python.pydev.core.IPythonNature)3 PythonNatureStub (org.python.pydev.parser.PythonNatureStub)3 Module (org.python.pydev.parser.jython.ast.Module)3 IOException (java.io.IOException)2 AbstractModule (org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule)2