Search in sources :

Example 1 with Module

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

the class PyParser method createCythonAst.

public static ParseOutput createCythonAst(ParserInfo info) {
    ParseOutput parseOutput = null;
    if (USE_NEW_CYTHON_PARSER) {
        PyParserCython parserCython = new PyParserCython(info);
        try {
            parseOutput = parserCython.parse();
            parseOutput.isCython = true;
        } catch (Exception e) {
            // If cython is not available, an error is expected.
            Log.log(e);
        }
    }
    if (parseOutput == null || parseOutput.ast == null) {
        // If we couldn't parse with cython, try to give something even if not really complete.
        List<stmtType> classesAndFunctions = FastParser.parseCython(info.document);
        Module ast = new Module(classesAndFunctions.toArray(new stmtType[classesAndFunctions.size()]));
        parseOutput = new ParseOutput(ast, parseOutput != null ? parseOutput.error : null, ((IDocumentExtension4) info.document).getModificationStamp());
        parseOutput.isCython = true;
    }
    return parseOutput;
}
Also used : IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) PyParserCython(org.python.pydev.parser.grammar_cython.PyParserCython) Module(org.python.pydev.parser.jython.ast.Module) ResourceException(org.eclipse.core.internal.resources.ResourceException) CoreException(org.eclipse.core.runtime.CoreException) ParseException(org.python.pydev.parser.jython.ParseException) BadLocationException(org.eclipse.jface.text.BadLocationException) MisconfigurationException(org.python.pydev.core.MisconfigurationException)

Example 2 with Module

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

the class FastDefinitionsParser method parse.

public static SimpleNode parse(char[] cs, String moduleName, int len, File f) {
    FastDefinitionsParser parser = new FastDefinitionsParser(cs, len, moduleName, f);
    try {
        parser.extractBody();
    } catch (SyntaxErrorException e) {
        throw new RuntimeException(e);
    } catch (StackOverflowError e) {
        RuntimeException runtimeException = new RuntimeException(e);
        Log.log("Error parsing: " + moduleName + " - " + f + "\nContents:\n" + new String(cs, 0, len > 1000 ? 1000 : len), // report at most 1000 chars...
        runtimeException);
        throw runtimeException;
    }
    List<stmtType> body = parser.body;
    Module ret = new Module(body.toArray(new stmtType[body.size()]));
    ret.beginLine = 1;
    ret.beginColumn = 1;
    if (parseCallbacks.size() > 0) {
        Tuple<String, SimpleNode> arg = new Tuple<String, SimpleNode>(moduleName, ret);
        for (ICallback<Object, Tuple<String, SimpleNode>> c : parseCallbacks) {
            c.call(arg);
        }
    }
    return ret;
}
Also used : org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) SyntaxErrorException(org.python.pydev.core.docutils.SyntaxErrorException) SimpleNode(org.python.pydev.parser.jython.SimpleNode) Module(org.python.pydev.parser.jython.ast.Module) Tuple(org.python.pydev.shared_core.structure.Tuple)

Example 3 with Module

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

the class PyParser25Test method testSuiteLineNumber.

public void testSuiteLineNumber() throws Exception {
    setDefaultVersion(IPythonNature.GRAMMAR_PYTHON_VERSION_2_5);
    String str = "" + "class Process:\n" + "\n" + "    def Foo(self):\n" + "        if a == 1:\n" + "            pass\n" + "        elif a == 1:\n" + "            pass\n" + "\n" + "";
    SimpleNode ast = parseLegalDocStr(str);
    stmtType[] body = ((Module) ast).body;
    assertEquals(1, body.length);
    ClassDef classFound = (ClassDef) body[0];
    body = classFound.body;
    assertEquals(1, body.length);
    FunctionDef func = (FunctionDef) body[0];
    If ifFound = (If) func.body[0];
    assertEquals(6, ifFound.orelse.beginLine);
}
Also used : ClassDef(org.python.pydev.parser.jython.ast.ClassDef) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) FunctionDef(org.python.pydev.parser.jython.ast.FunctionDef) Module(org.python.pydev.parser.jython.ast.Module) If(org.python.pydev.parser.jython.ast.If) SimpleNode(org.python.pydev.parser.jython.SimpleNode)

Example 4 with Module

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

the class PyParser25Test method testNewWithStmt2.

public void testNewWithStmt2() {
    setDefaultVersion(IPythonNature.GRAMMAR_PYTHON_VERSION_2_5);
    String str = "" + "from __future__ import with_statement\n" + "with foo as x:\n" + "    print 'bla'\n" + "";
    // we'll actually treat this as a try..finally with a body with try..except..else
    Module mod = (Module) parseLegalDocStr(str);
    assertEquals(2, mod.body.length);
    assertTrue(mod.body[1] instanceof With);
    With w = (With) mod.body[1];
    assertTrue(((WithItem) w.with_item[0]).optional_vars != null);
}
Also used : WithItem(org.python.pydev.parser.jython.ast.WithItem) Module(org.python.pydev.parser.jython.ast.Module) With(org.python.pydev.parser.jython.ast.With)

Example 5 with Module

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

the class PyParser25Test method testNewRelativeImport3.

public void testNewRelativeImport3() {
    setDefaultVersion(IPythonNature.GRAMMAR_PYTHON_VERSION_2_5);
    String str = "from ... import foo\n";
    Module mod = (Module) parseLegalDocStr(str);
    ImportFrom f = (ImportFrom) mod.body[0];
    assertEquals(3, f.level);
    assertEquals("", ((NameTok) f.module).id);
}
Also used : ImportFrom(org.python.pydev.parser.jython.ast.ImportFrom) Module(org.python.pydev.parser.jython.ast.Module)

Aggregations

Module (org.python.pydev.parser.jython.ast.Module)135 ClassDef (org.python.pydev.parser.jython.ast.ClassDef)44 FunctionDef (org.python.pydev.parser.jython.ast.FunctionDef)44 Assign (org.python.pydev.parser.jython.ast.Assign)38 SimpleNode (org.python.pydev.parser.jython.SimpleNode)28 Name (org.python.pydev.parser.jython.ast.Name)13 org.python.pydev.parser.jython.ast.stmtType (org.python.pydev.parser.jython.ast.stmtType)12 BinOp (org.python.pydev.parser.jython.ast.BinOp)10 IGrammarVersionProvider (org.python.pydev.core.IGrammarVersionProvider)8 AbstractModule (org.python.pydev.ast.codecompletion.revisited.modules.AbstractModule)7 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)7 IModule (org.python.pydev.core.IModule)7 Document (org.eclipse.jface.text.Document)6 Attribute (org.python.pydev.parser.jython.ast.Attribute)6 Expr (org.python.pydev.parser.jython.ast.Expr)6 ImportFrom (org.python.pydev.parser.jython.ast.ImportFrom)6 NameTok (org.python.pydev.parser.jython.ast.NameTok)6 With (org.python.pydev.parser.jython.ast.With)6 File (java.io.File)5 Call (org.python.pydev.parser.jython.ast.Call)5