Skip to content

Fixes for LSP #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ protected boolean isObjectOfLanguage(Object object) {
protected Object findMetaObject(PythonContext context, Object value) {
if (value != null) {
if (value instanceof PythonObject) {
return ((PythonObject) value).asPythonClass().getName();
return ((PythonObject) value).asPythonClass();
} else if (value instanceof PythonAbstractObject ||
value instanceof Number ||
value instanceof String ||
Expand All @@ -304,14 +304,16 @@ protected Iterable<Scope> findLocalScopes(PythonContext context, Node node, Fram
for (Scope s : super.findLocalScopes(context, node, frame)) {
scopes.add(s);
}
PythonObject globals = PArguments.getGlobals(frame);
if (globals != null) {
scopes.add(Scope.newBuilder("globals()", globals).build());
}
Frame generatorFrame = PArguments.getGeneratorFrame(frame);
if (generatorFrame != null) {
for (Scope s : super.findLocalScopes(context, node, generatorFrame)) {
scopes.add(s);
if (frame != null) {
PythonObject globals = PArguments.getGlobals(frame);
if (globals != null) {
scopes.add(Scope.newBuilder("globals()", globals).build());
}
Frame generatorFrame = PArguments.getGeneratorFrame(frame);
if (generatorFrame != null) {
for (Scope s : super.findLocalScopes(context, node, generatorFrame)) {
scopes.add(s);
}
}
}
return scopes;
Expand All @@ -321,6 +323,7 @@ protected Iterable<Scope> findLocalScopes(PythonContext context, Node node, Fram
protected Iterable<Scope> findTopScopes(PythonContext context) {
ArrayList<Scope> scopes = new ArrayList<>();
scopes.add(Scope.newBuilder("__main__", context.getMainModule()).build());
scopes.add(Scope.newBuilder("builtins", context.getBuiltins()).build());
return scopes;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public Object visitChildren(RuleNode node) {
}

protected void deriveSourceSection(RuleNode node, Object r) {
if (r instanceof PNode) {
if (r instanceof PNode && ((PNode) r).getSourceSection() == null) {
SourceSection derivedSection = deriveSourceSection(node);
if (derivedSection != null) {
((PNode) r).assignSourceSection(derivedSection);
Expand All @@ -185,7 +185,7 @@ private SourceSection deriveSourceSection(RuleNode node) {
} else if (node instanceof ParserRuleContext) {
int start = ((ParserRuleContext) node).getStart().getStartIndex();
int stop = ((ParserRuleContext) node).getStop().getStopIndex();
return createSourceSection(start, stop - start);
return createSourceSection(start, stop - start + 1);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ private static ParserRuleContext preParseWithAntlr(PythonCore core, Source sourc
Token token = ((RecognitionException) e).getOffendingToken();
line = token.getLine();
column = token.getCharPositionInLine();
} else if (e.getCause() instanceof NoViableAltException) {
Token token = ((NoViableAltException) e.getCause()).getOffendingToken();
} else if (e.getCause() instanceof RecognitionException) {
Token token = ((RecognitionException) e.getCause()).getOffendingToken();
line = token.getLine();
column = token.getCharPositionInLine();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,15 @@
*/
package com.oracle.graal.python.parser.antlr;

import com.oracle.graal.python.runtime.PythonParser.PIncompleteSourceException;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;

import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.misc.IntervalSet;

import com.oracle.graal.python.runtime.PythonParser.PIncompleteSourceException;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;

/**
* An error listener that immediately bails out of the parse (does not recover) and throws a runtime
* exception with a descriptive error message.
Expand Down Expand Up @@ -73,7 +74,9 @@ public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
throw handleRecognitionException;
}
}

if (offendingSymbol instanceof Token) {
throw new RuntimeException(entireMessage, new EmptyRecognitionException(entireMessage, recognizer, (Token) offendingSymbol));
}
throw new RuntimeException(entireMessage, e);
}

Expand All @@ -83,4 +86,19 @@ private static PIncompleteSourceException handleRecognitionException(IntervalSet
}
return null;
}

private static class EmptyRecognitionException extends RecognitionException {
private static final long serialVersionUID = 1L;
private Token offendingToken;

public EmptyRecognitionException(String message, Recognizer<?, ?> recognizer, Token offendingToken) {
super(message, recognizer, offendingToken.getInputStream(), null);
this.offendingToken = offendingToken;
}

@Override
public Token getOffendingToken() {
return offendingToken;
}
}
}