From 3897cf320b3335a2863631699b10f9147c98636a Mon Sep 17 00:00:00 2001 From: Daniel Stolpe Date: Tue, 8 May 2018 15:32:05 +0200 Subject: [PATCH 1/4] Fix handling of RecognitionExceptions to get line and column of syntax errors --- .../src/com/oracle/graal/python/parser/PythonParserImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java index b6acdf05ac..8ea1d0b42b 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonParserImpl.java @@ -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 { From e3281c9099172e8179fe9f480df2eedbef67a304 Mon Sep 17 00:00:00 2001 From: Daniel Stolpe Date: Tue, 8 May 2018 15:32:58 +0200 Subject: [PATCH 2/4] fix findLocalsScopes() for null frames, add builtins to top scopes --- .../oracle/graal/python/PythonLanguage.java | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java index 2549f906df..5589a38d62 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java @@ -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 || @@ -304,14 +304,16 @@ protected Iterable 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; @@ -321,6 +323,7 @@ protected Iterable findLocalScopes(PythonContext context, Node node, Fram protected Iterable findTopScopes(PythonContext context) { ArrayList scopes = new ArrayList<>(); scopes.add(Scope.newBuilder("__main__", context.getMainModule()).build()); + scopes.add(Scope.newBuilder("builtins", context.getBuiltins()).build()); return scopes; } From 0fa5f8d1faf0e1cd9bb55722da59ea66c73f852b Mon Sep 17 00:00:00 2001 From: Daniel Stolpe Date: Tue, 8 May 2018 15:34:31 +0200 Subject: [PATCH 3/4] Fix source section off-by-one error, fix source sections for leaf nodes --- .../oracle/graal/python/parser/PythonBaseTreeTranslator.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonBaseTreeTranslator.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonBaseTreeTranslator.java index 105c2c96c1..f4ad2dcd70 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonBaseTreeTranslator.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/PythonBaseTreeTranslator.java @@ -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); @@ -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; } From 64aa2844d163a57d59267ef640b00cd6d0fe201d Mon Sep 17 00:00:00 2001 From: Daniel Stolpe Date: Tue, 8 May 2018 15:37:41 +0200 Subject: [PATCH 4/4] fix empty RecogitionException to get syntax error position --- .../antlr/DescriptiveBailErrorListener.java | 26 ++++++++++++++++--- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/DescriptiveBailErrorListener.java b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/DescriptiveBailErrorListener.java index af4177fc87..497bec1495 100644 --- a/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/DescriptiveBailErrorListener.java +++ b/graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/antlr/DescriptiveBailErrorListener.java @@ -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. @@ -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); } @@ -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; + } + } }