Search in sources :

Example 1 with PyParser

use of com.jetbrains.python.parsing.PyParser in project intellij-community by JetBrains.

the class PyFileElementType method parseContents.

@Nullable
@Override
public ASTNode parseContents(ASTNode node) {
    final LanguageLevel languageLevel = getLanguageLevel(node.getPsi());
    if (PydevConsoleRunner.isPythonConsole(node)) {
        return parseConsoleCode(node, PydevConsoleRunner.getPythonConsoleData(node));
    } else {
        final PsiElement psi = node.getPsi();
        if (psi != null) {
            final Project project = psi.getProject();
            final PsiBuilderFactory factory = PsiBuilderFactory.getInstance();
            final Language language = getLanguage();
            final ParserDefinition parserDefinition = LanguageParserDefinitions.INSTANCE.forLanguage(language);
            if (parserDefinition == null) {
                return null;
            }
            final Lexer lexer = parserDefinition.createLexer(project);
            final PsiParser parser = parserDefinition.createParser(project);
            final PsiBuilder builder = factory.createBuilder(project, node, lexer, language, node.getChars());
            if (parser instanceof PyParser) {
                final PyParser pythonParser = (PyParser) parser;
                pythonParser.setLanguageLevel(languageLevel);
                if (languageLevel == LanguageLevel.PYTHON26 && psi.getContainingFile().getName().equals("__builtin__.py")) {
                    pythonParser.setFutureFlag(StatementParsing.FUTURE.PRINT_FUNCTION);
                }
            }
            return parser.parse(this, builder).getFirstChildNode();
        }
        return null;
    }
}
Also used : Project(com.intellij.openapi.project.Project) Lexer(com.intellij.lexer.Lexer) PythonConsoleLexer(com.jetbrains.python.console.parsing.PythonConsoleLexer) PythonLanguage(com.jetbrains.python.PythonLanguage) PyParser(com.jetbrains.python.parsing.PyParser) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PyParser

use of com.jetbrains.python.parsing.PyParser in project intellij-community by JetBrains.

the class PyFileElementType method parseConsoleCode.

@Nullable
private ASTNode parseConsoleCode(@NotNull ASTNode node, PythonConsoleData consoleData) {
    final Lexer lexer = createConsoleLexer(node, consoleData);
    final PsiElement psi = node.getPsi();
    if (psi != null) {
        final Project project = psi.getProject();
        final PsiBuilderFactory factory = PsiBuilderFactory.getInstance();
        final PsiBuilder builder = factory.createBuilder(project, node, lexer, getLanguage(), node.getChars());
        final PyParser parser = new PyConsoleParser(consoleData, getLanguageLevel(psi));
        return parser.parse(this, builder).getFirstChildNode();
    }
    return null;
}
Also used : Project(com.intellij.openapi.project.Project) Lexer(com.intellij.lexer.Lexer) PythonConsoleLexer(com.jetbrains.python.console.parsing.PythonConsoleLexer) PyConsoleParser(com.jetbrains.python.console.parsing.PyConsoleParser) PyParser(com.jetbrains.python.parsing.PyParser) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Lexer (com.intellij.lexer.Lexer)2 Project (com.intellij.openapi.project.Project)2 PsiElement (com.intellij.psi.PsiElement)2 PythonConsoleLexer (com.jetbrains.python.console.parsing.PythonConsoleLexer)2 PyParser (com.jetbrains.python.parsing.PyParser)2 Nullable (org.jetbrains.annotations.Nullable)2 PythonLanguage (com.jetbrains.python.PythonLanguage)1 PyConsoleParser (com.jetbrains.python.console.parsing.PyConsoleParser)1