Search in sources :

Example 1 with TokenMgrError

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

the class PyParser method reparseDocument.

/**
 * Parses the document, generates error annotations
 *
 * @param argsToReparse: will be passed to fireParserError / fireParserChanged so that the IParserObserver2
 * can check it. This is useful when the reparse was done with some specific thing in mind, so that its requestor
 * can pass some specific thing to the parser observers
 *
 * @return a tuple with the SimpleNode root(if parsed) and the error (if any).
 *         if we are able to recover from a reparse, we have both, the root and the error.
 */
@Override
public ParseOutput reparseDocument(Object... argsToReparse) {
    // get the document ast and error in object
    int version;
    AdditionalGrammarVersionsToCheck additionalGrammarsToCheck = null;
    try {
        version = grammarVersionProvider.getGrammarVersion();
        additionalGrammarsToCheck = grammarVersionProvider.getAdditionalGrammarVersions();
    } catch (MisconfigurationException e1) {
        // Ok, we cannot get it... let's put on the default
        version = IGrammarVersionProvider.LATEST_GRAMMAR_PY3_VERSION;
    }
    long documentTime = System.currentTimeMillis();
    ParseOutput obj = reparseDocument(new ParserInfo(document, version, true, additionalGrammarsToCheck));
    IFile original = null;
    IAdaptable adaptable = null;
    if (input == null) {
        return obj;
    }
    original = (input instanceof IAdaptable) ? ((IAdaptable) input).getAdapter(IFile.class) : null;
    if (original != null) {
        adaptable = original;
    } else {
        // probably an external file, may have some location provider mechanism
        // it may be org.eclipse.ui.internal.editors.text.JavaFileEditorInput
        adaptable = (IAdaptable) input;
    }
    // delete the markers
    if (original != null) {
        try {
            deleteErrorMarkers(original);
        } catch (ResourceException e) {
            // so, there is no need to log this failure
            if (original.exists()) {
                Log.log(e);
            }
        } catch (CoreException e) {
            Log.log(e);
        }
    } else if (adaptable == null) {
        // ok, we have nothing... maybe we are in tests...
        if (!PyParser.ACCEPT_NULL_INPUT_EDITOR) {
            throw new RuntimeException("Null input editor received in parser!");
        }
    }
    if (disposed) {
        // if it was disposed in this time, don't fire any notification nor return anything valid.
        return new ParseOutput();
    }
    ErrorParserInfoForObservers errorInfo = null;
    if (obj.error instanceof ParseException || obj.error instanceof TokenMgrError) {
        errorInfo = new ErrorParserInfoForObservers(obj.error, adaptable, document, argsToReparse);
    }
    if (obj.ast != null) {
        // Ok, reparse successful, lets erase the markers that are in the editor we just parsed
        // Note: we may get the ast even if errors happen (and we'll notify in that case too).
        ChangedParserInfoForObservers info = new ChangedParserInfoForObservers(obj.ast, obj.modificationStamp, adaptable, document, documentTime, errorInfo, argsToReparse);
        fireParserChanged(info);
    }
    if (errorInfo != null) {
        fireParserError(errorInfo);
    }
    if (postParserListeners.size() > 0) {
        ArrayList<IPostParserListener> tempList = new ArrayList<>(postParserListeners);
        for (IPostParserListener iParserObserver : tempList) {
            iParserObserver.participantsNotified(argsToReparse);
        }
    }
    return obj;
}
Also used : IAdaptable(org.eclipse.core.runtime.IAdaptable) ErrorParserInfoForObservers(org.python.pydev.shared_core.parsing.ErrorParserInfoForObservers) IFile(org.eclipse.core.resources.IFile) MisconfigurationException(org.python.pydev.core.MisconfigurationException) LowMemoryArrayList(org.python.pydev.shared_core.structure.LowMemoryArrayList) ArrayList(java.util.ArrayList) TokenMgrError(org.python.pydev.parser.jython.TokenMgrError) ChangedParserInfoForObservers(org.python.pydev.shared_core.parsing.ChangedParserInfoForObservers) CoreException(org.eclipse.core.runtime.CoreException) AdditionalGrammarVersionsToCheck(org.python.pydev.core.IGrammarVersionProvider.AdditionalGrammarVersionsToCheck) ResourceException(org.eclipse.core.internal.resources.ResourceException) ParseException(org.python.pydev.parser.jython.ParseException)

Example 2 with TokenMgrError

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

the class VisitorFactory method getRootNode.

public static Module getRootNode(IDocument doc, IGrammarVersionProvider versionProvider) throws ParseException, MisconfigurationException {
    ParseOutput objects = PyParser.reparseDocument(new PyParser.ParserInfo(doc, versionProvider));
    Throwable exception = objects.error;
    if (exception != null) {
        /* We try to get rid of the 'Throwable' exception, if possible */
        if (exception instanceof ParseException) {
            throw (ParseException) exception;
        } else if (exception instanceof TokenMgrError) {
            /* Error from Lexer */
            throw new ParseException(exception.toString());
        } else {
            throw new RuntimeException(exception);
        }
    }
    if (objects.error != null) {
        throw new RuntimeException(objects.error);
    }
    return (Module) objects.ast;
}
Also used : ParseOutput(org.python.pydev.shared_core.parsing.BaseParser.ParseOutput) PyParser(org.python.pydev.parser.PyParser) TokenMgrError(org.python.pydev.parser.jython.TokenMgrError) ParseException(org.python.pydev.parser.jython.ParseException) IModule(org.python.pydev.core.IModule) ISourceModule(org.python.pydev.core.ISourceModule) Module(org.python.pydev.parser.jython.ast.Module) SourceModule(org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)

Example 3 with TokenMgrError

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

the class RefactoringInfo method getParsedUserSelection.

public ModuleAdapter getParsedUserSelection() {
    ModuleAdapter parsedAdapter = null;
    String source = normalizeSourceSelection(this.userSelection);
    if (this.userSelection != null && source.length() > 0) {
        try {
            parsedAdapter = VisitorFactory.createModuleAdapter(moduleManager, null, new Document(source), nature, this.versionProvider);
        } catch (TokenMgrError e) {
            return null;
        } catch (ParseException e) {
            /* Parse Exception means the current selection is invalid, discard and return null */
            return null;
        } catch (Throwable e) {
            throw new RuntimeException(e);
        }
    }
    return parsedAdapter;
}
Also used : ModuleAdapter(org.python.pydev.refactoring.ast.adapters.ModuleAdapter) TokenMgrError(org.python.pydev.parser.jython.TokenMgrError) ParseException(org.python.pydev.parser.jython.ParseException) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument)

Example 4 with TokenMgrError

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

the class PyParser method createErrorDesc.

/**
 * Creates the error description for a given error in the parse.
 *
 * Must return an error!
 */
public static ErrorDescription createErrorDesc(Throwable error, IDocument doc) {
    try {
        int errorStart = -1;
        int errorEnd = -1;
        int errorLine = -1;
        String message = null;
        int tokenBeginLine = -1;
        if (error instanceof ParseException) {
            ParseException parseErr = (ParseException) error;
            message = parseErr.getMessage();
            // marker for it
            if (parseErr.currentToken == null) {
                try {
                    IRegion endLine = doc.getLineInformationOfOffset(doc.getLength());
                    errorStart = endLine.getOffset();
                    errorEnd = endLine.getOffset() + endLine.getLength();
                } catch (BadLocationException e) {
                // ignore (can have changed in the meanwhile)
                }
            } else {
                Token errorToken = parseErr.currentToken.next != null ? parseErr.currentToken.next : parseErr.currentToken;
                if (errorToken != null) {
                    tokenBeginLine = errorToken.beginLine - 1;
                    try {
                        IRegion startLine = doc.getLineInformation(getDocPosFromAstPos(errorToken.beginLine));
                        IRegion endLine;
                        if (errorToken.endLine == 0) {
                            endLine = startLine;
                        } else {
                            endLine = doc.getLineInformation(getDocPosFromAstPos(errorToken.endLine));
                        }
                        errorStart = startLine.getOffset() + getDocPosFromAstPos(errorToken.beginColumn);
                        errorEnd = endLine.getOffset() + errorToken.endColumn;
                    } catch (BadLocationException e) {
                    // ignore (can have changed in the meanwhile)
                    }
                }
            }
        } else if (error instanceof TokenMgrError) {
            TokenMgrError tokenErr = (TokenMgrError) error;
            message = tokenErr.getMessage();
            tokenBeginLine = tokenErr.errorLine - 1;
            try {
                IRegion startLine = doc.getLineInformation(tokenErr.errorLine - 1);
                errorStart = startLine.getOffset();
                errorEnd = startLine.getOffset() + tokenErr.errorColumn;
            } catch (BadLocationException e) {
            // ignore (can have changed in the meanwhile)
            }
        } else {
            Log.log("Error, expecting ParseException or TokenMgrError. Received: " + error, error);
            return new ErrorDescription("Internal PyDev Error", 0, 0, 0);
        }
        try {
            errorLine = doc.getLineOfOffset(errorStart) + 1;
        } catch (BadLocationException e) {
            errorLine = tokenBeginLine;
        }
        // in task manager
        if (message != null) {
            // prettyprint
            message = StringUtils.replaceNewLines(message, " ");
        }
        return new ErrorDescription(message, errorLine, errorStart, errorEnd);
    } catch (Exception e) {
        Log.log(e);
        return new ErrorDescription("Internal PyDev Error", 0, 0, 0);
    }
}
Also used : ErrorDescription(org.python.pydev.shared_core.model.ErrorDescription) Token(org.python.pydev.parser.jython.Token) TokenMgrError(org.python.pydev.parser.jython.TokenMgrError) ParseException(org.python.pydev.parser.jython.ParseException) IRegion(org.eclipse.jface.text.IRegion) BadLocationException(org.eclipse.jface.text.BadLocationException) 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 5 with TokenMgrError

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

the class PyParser method reparseDocument.

/**
 * @return a tuple with the SimpleNode root(if parsed) and the error (if any).
 *         if we are able to recover from a reparse, we have both, the root and the error.
 */
public static ParseOutput reparseDocument(ParserInfo info) {
    if (info.grammarVersion == IPythonNature.GRAMMAR_PYTHON_VERSION_CYTHON) {
        return createCythonAst(info);
    }
    // create a stream with document's data
    // Note: safer could be locking, but if for some reason we get the modification stamp and the document changes
    // right after that, at least any cache will check against the old stamp to be reconstructed (which is the main
    // reason for this stamp).
    long modifiedTime = ((IDocumentExtension4) info.document).getModificationStamp();
    String startDoc = info.document.get();
    if (startDoc.trim().length() == 0) {
        // If empty, don't bother to parse!
        return new ParseOutput(new Module(new stmtType[0]), null, modifiedTime);
    }
    Set<Integer> parsedVersions = new HashSet<>();
    char[] charArray;
    try {
        charArray = createCharArrayToParse(startDoc);
    } catch (OutOfMemoryError e1) {
        OnExpectedOutOfMemory.clearCacheOnOutOfMemory.call(null);
        // retry now with caches cleared...
        charArray = createCharArrayToParse(startDoc);
    }
    // it can be garbage-collected now.
    startDoc = null;
    Tuple<ISimpleNode, Throwable> returnVar = new Tuple<ISimpleNode, Throwable>(null, null);
    IGrammar grammar = null;
    try {
        parsedVersions.add(info.grammarVersion);
        grammar = createGrammar(info.generateTree, info.grammarVersion, charArray);
        SimpleNode newRoot;
        try {
            newRoot = grammar.file_input();
        } catch (OutOfMemoryError e) {
            OnExpectedOutOfMemory.clearCacheOnOutOfMemory.call(null);
            // retry now with caches cleared...
            newRoot = grammar.file_input();
        }
        returnVar.o1 = newRoot;
        // only notify successful parses
        if (successfulParseListeners.size() > 0) {
            Tuple3<ISimpleNode, Throwable, ParserInfo> param = new Tuple3<ISimpleNode, Throwable, ParserInfo>(returnVar.o1, returnVar.o2, info);
            for (ICallback<Object, Tuple3<ISimpleNode, Throwable, ParserInfo>> callback : successfulParseListeners) {
                callback.call(param);
            }
        }
        returnVar.o2 = grammar.getErrorOnParsing();
        if (returnVar.o2 == null) {
            AdditionalGrammarVersionsToCheck additionalGrammarVersionsToCheck = info.additionalGrammarVersionsToCheck;
            if (additionalGrammarVersionsToCheck != null) {
                for (int grammarVersion : additionalGrammarVersionsToCheck.getGrammarVersions()) {
                    if (parsedVersions.contains(grammarVersion)) {
                        continue;
                    }
                    parsedVersions.add(grammarVersion);
                    grammar = createGrammar(false, grammarVersion, charArray);
                    try {
                        grammar.file_input();
                    } catch (OutOfMemoryError e) {
                        OnExpectedOutOfMemory.clearCacheOnOutOfMemory.call(null);
                        // retry now with caches cleared...
                        grammar.file_input();
                    }
                    returnVar.o2 = grammar.getErrorOnParsing();
                    if (returnVar.o2 != null) {
                        break;
                    }
                }
            }
        }
    } catch (Throwable e) {
        // another parse.
        if (DEBUG_SHOW_PARSE_ERRORS) {
            e.printStackTrace();
        }
        // If the grammar was not created, the problem wasn't in the parsing... so, let's just rethrow the error
        if (grammar == null) {
            throw new RuntimeException(e);
        }
        // We have to change it for the 1st error we got (the one in catch is the last one).
        Throwable errorOnParsing = grammar.getErrorOnParsing();
        if (errorOnParsing != null) {
            e = errorOnParsing;
        } else if (DEBUG_SHOW_PARSE_ERRORS) {
            System.out.println("Unhandled error");
            e.printStackTrace();
        }
        grammar = null;
        if (e instanceof ParseException || e instanceof TokenMgrError) {
            returnVar = new Tuple<ISimpleNode, Throwable>(null, e);
        } else if (e.getClass().getName().indexOf("LookaheadSuccess") != -1) {
        // don't log this kind of error...
        } else {
            Log.log(e);
        }
    }
    if (DEBUG_SHOW_PARSE_ERRORS) {
        if (returnVar.o1 == null) {
            System.out.println("Unable to parse " + info);
        }
    }
    // System.out.println("Output grammar: "+returnVar);
    return new ParseOutput(returnVar, modifiedTime);
}
Also used : IDocumentExtension4(org.eclipse.jface.text.IDocumentExtension4) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) SimpleNode(org.python.pydev.parser.jython.SimpleNode) ISimpleNode(org.python.pydev.shared_core.model.ISimpleNode) HashSet(java.util.HashSet) org.python.pydev.parser.jython.ast.stmtType(org.python.pydev.parser.jython.ast.stmtType) TokenMgrError(org.python.pydev.parser.jython.TokenMgrError) Tuple3(org.python.pydev.shared_core.structure.Tuple3) AdditionalGrammarVersionsToCheck(org.python.pydev.core.IGrammarVersionProvider.AdditionalGrammarVersionsToCheck) ParseException(org.python.pydev.parser.jython.ParseException) Module(org.python.pydev.parser.jython.ast.Module) Tuple(org.python.pydev.shared_core.structure.Tuple)

Aggregations

ParseException (org.python.pydev.parser.jython.ParseException)8 TokenMgrError (org.python.pydev.parser.jython.TokenMgrError)8 MisconfigurationException (org.python.pydev.core.MisconfigurationException)3 ParseOutput (org.python.pydev.shared_core.parsing.BaseParser.ParseOutput)3 ResourceException (org.eclipse.core.internal.resources.ResourceException)2 CoreException (org.eclipse.core.runtime.CoreException)2 Document (org.eclipse.jface.text.Document)2 AdditionalGrammarVersionsToCheck (org.python.pydev.core.IGrammarVersionProvider.AdditionalGrammarVersionsToCheck)2 Module (org.python.pydev.parser.jython.ast.Module)2 ModuleAdapter (org.python.pydev.refactoring.ast.adapters.ModuleAdapter)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IFile (org.eclipse.core.resources.IFile)1 IAdaptable (org.eclipse.core.runtime.IAdaptable)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)1 IRegion (org.eclipse.jface.text.IRegion)1 SourceModule (org.python.pydev.ast.codecompletion.revisited.modules.SourceModule)1 IModule (org.python.pydev.core.IModule)1