Skip to content

Commit c49b8c0

Browse files
committed
separate parser from PythonCore
1 parent f75c7c5 commit c49b8c0

File tree

11 files changed

+116
-102
lines changed

11 files changed

+116
-102
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/Python3Core.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import static com.oracle.graal.python.nodes.BuiltinNames.__BUILTINS_PATCHES__;
2929
import static com.oracle.graal.python.nodes.SpecialAttributeNames.__PACKAGE__;
30+
import static com.oracle.graal.python.runtime.exception.PythonErrorType.SyntaxError;
3031

3132
import java.io.IOException;
3233
import java.math.BigInteger;
@@ -131,14 +132,15 @@
131132
import com.oracle.graal.python.runtime.PythonOptions;
132133
import com.oracle.graal.python.runtime.PythonParser;
133134
import com.oracle.graal.python.runtime.exception.PException;
134-
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
135135
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
136136
import com.oracle.truffle.api.CallTarget;
137137
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
138138
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
139139
import com.oracle.truffle.api.TruffleFile;
140140
import com.oracle.truffle.api.TruffleLanguage.Env;
141+
import com.oracle.truffle.api.nodes.Node;
141142
import com.oracle.truffle.api.source.Source;
143+
import com.oracle.truffle.api.source.SourceSection;
142144

143145
/**
144146
* The core is intended to the immutable part of the interpreter, including most modules and most
@@ -627,4 +629,22 @@ public PInt getTrue() {
627629
public PInt getFalse() {
628630
return pyFalse;
629631
}
632+
633+
public RuntimeException raiseInvalidSyntax(Source source, SourceSection section) {
634+
Node location = new Node() {
635+
@Override
636+
public SourceSection getSourceSection() {
637+
return section;
638+
}
639+
};
640+
PBaseException instance;
641+
instance = factory().createBaseException(SyntaxError, "invalid syntax", new Object[0]);
642+
String path = source.getPath();
643+
instance.setAttribute("filename", path != null ? path : source.getName() != null ? source.getName() : "<string>");
644+
instance.setAttribute("text", section.isAvailable() ? source.getCharacters(section.getStartLine()) : "");
645+
instance.setAttribute("lineno", section.getStartLine());
646+
instance.setAttribute("offset", section.getStartColumn());
647+
instance.setAttribute("msg", section.getCharIndex() == source.getLength() ? "unexpected EOF while parsing" : "invalid syntax");
648+
throw PException.fromObject(instance, location);
649+
}
630650
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesUtils.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.io.UnsupportedEncodingException;
3333

3434
import com.oracle.graal.python.runtime.PythonCore;
35+
import com.oracle.graal.python.runtime.PythonParser.ParserErrorCallback;
3536
import com.oracle.truffle.api.CompilerAsserts;
3637
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
3738

@@ -47,8 +48,8 @@ public static byte[] fromSize(PythonCore core, int size) {
4748
return new byte[size];
4849
}
4950

50-
public static byte[] fromString(PythonCore core, String source) {
51-
return decodeEscapeToBytes(core, source);
51+
public static byte[] fromString(ParserErrorCallback errors, String source) {
52+
return decodeEscapeToBytes(errors, source);
5253
}
5354

5455
@TruffleBoundary(transferToInterpreterOnException = false)
@@ -96,7 +97,7 @@ public static String bytesRepr(byte[] bytes, int length) {
9697
}
9798

9899
@TruffleBoundary(transferToInterpreterOnException = false)
99-
public static StringBuilder decodeEscapes(PythonCore core, String string, boolean regexMode) {
100+
public static StringBuilder decodeEscapes(ParserErrorCallback errors, String string, boolean regexMode) {
100101
// see _PyBytes_DecodeEscape from
101102
// https://github.com/python/cpython/blob/master/Objects/bytesobject.c
102103
// TODO: for the moment we assume ASCII
@@ -111,7 +112,7 @@ public static StringBuilder decodeEscapes(PythonCore core, String string, boolea
111112

112113
i++;
113114
if (i >= length) {
114-
throw core.raise(ValueError, "Trailing \\ in string");
115+
throw errors.raise(ValueError, "Trailing \\ in string");
115116
}
116117

117118
chr = string.charAt(i);
@@ -196,23 +197,23 @@ public static StringBuilder decodeEscapes(PythonCore core, String string, boolea
196197
// fall through
197198
}
198199
}
199-
throw core.raise(ValueError, "invalid \\x escape at position %d", i);
200+
throw errors.raise(ValueError, "invalid \\x escape at position %d", i);
200201
default:
201202
if (regexMode) {
202203
charList.append('\\');
203204
charList.append(chr);
204205
} else {
205-
throw core.raise(ValueError, "invalid escape sequence '\\%s' at position %d", chr, i);
206+
throw errors.raise(ValueError, "invalid escape sequence '\\%s' at position %d", chr, i);
206207
}
207208
}
208209
}
209210

210211
return charList;
211212
}
212213

213-
@TruffleBoundary
214-
public static byte[] decodeEscapeToBytes(PythonCore core, String string) {
215-
StringBuilder sb = decodeEscapes(core, string, false);
214+
private static byte[] decodeEscapeToBytes(ParserErrorCallback errors, String string) {
215+
CompilerAsserts.neverPartOfCompilation();
216+
StringBuilder sb = decodeEscapes(errors, string, false);
216217
byte[] bytes = new byte[sb.length()];
217218
for (int i = 0; i < sb.length(); i++) {
218219
bytes[i] = (byte) sb.charAt(i);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/FunctionDefinitionNode.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import com.oracle.graal.python.nodes.statement.StatementNode;
3636
import com.oracle.graal.python.parser.DefinitionCellSlots;
3737
import com.oracle.graal.python.parser.ExecutionCellSlots;
38-
import com.oracle.graal.python.runtime.PythonCore;
3938
import com.oracle.truffle.api.RootCallTarget;
4039
import com.oracle.truffle.api.frame.FrameDescriptor;
4140
import com.oracle.truffle.api.frame.VirtualFrame;
@@ -45,7 +44,6 @@ public class FunctionDefinitionNode extends ExpressionDefinitionNode {
4544

4645
protected final String functionName;
4746
protected final String enclosingClassName;
48-
protected final PythonCore core;
4947
protected final RootCallTarget callTarget;
5048
protected final FrameDescriptor frameDescriptor;
5149
protected final Arity arity;
@@ -54,14 +52,12 @@ public class FunctionDefinitionNode extends ExpressionDefinitionNode {
5452
@Child private ExpressionNode doc;
5553
@Child private WriteAttributeToObjectNode writeDocNode = WriteAttributeToObjectNode.create();
5654

57-
public FunctionDefinitionNode(String functionName, String enclosingClassName, ExpressionNode doc, PythonCore core, Arity arity, StatementNode defaults, RootCallTarget callTarget,
58-
FrameDescriptor frameDescriptor,
59-
DefinitionCellSlots definitionCellSlots, ExecutionCellSlots executionCellSlots) {
55+
public FunctionDefinitionNode(String functionName, String enclosingClassName, ExpressionNode doc, Arity arity, StatementNode defaults, RootCallTarget callTarget,
56+
FrameDescriptor frameDescriptor, DefinitionCellSlots definitionCellSlots, ExecutionCellSlots executionCellSlots) {
6057
super(definitionCellSlots, executionCellSlots);
6158
this.functionName = functionName;
6259
this.enclosingClassName = enclosingClassName;
6360
this.doc = doc;
64-
this.core = core;
6561
this.callTarget = callTarget;
6662
this.frameDescriptor = frameDescriptor;
6763
this.arity = arity;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/GeneratorFunctionDefinitionNode.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
import com.oracle.graal.python.nodes.statement.StatementNode;
3737
import com.oracle.graal.python.parser.DefinitionCellSlots;
3838
import com.oracle.graal.python.parser.ExecutionCellSlots;
39-
import com.oracle.graal.python.runtime.PythonCore;
4039
import com.oracle.truffle.api.CompilerDirectives;
4140
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
4241
import com.oracle.truffle.api.RootCallTarget;
@@ -50,25 +49,25 @@ public class GeneratorFunctionDefinitionNode extends FunctionDefinitionNode {
5049
protected final int numOfGeneratorForNode;
5150
@CompilationFinal private RootCallTarget generatorCallTarget;
5251

53-
public GeneratorFunctionDefinitionNode(String name, String enclosingClassName, ExpressionNode doc, PythonCore core, Arity arity, StatementNode defaults, RootCallTarget callTarget,
52+
public GeneratorFunctionDefinitionNode(String name, String enclosingClassName, ExpressionNode doc, Arity arity, StatementNode defaults, RootCallTarget callTarget,
5453
FrameDescriptor frameDescriptor, DefinitionCellSlots definitionCellSlots, ExecutionCellSlots executionCellSlots,
5554
int numOfActiveFlags, int numOfGeneratorBlockNode, int numOfGeneratorForNode) {
56-
super(name, enclosingClassName, doc, core, arity, defaults, callTarget, frameDescriptor, definitionCellSlots, executionCellSlots);
55+
super(name, enclosingClassName, doc, arity, defaults, callTarget, frameDescriptor, definitionCellSlots, executionCellSlots);
5756
this.numOfActiveFlags = numOfActiveFlags;
5857
this.numOfGeneratorBlockNode = numOfGeneratorBlockNode;
5958
this.numOfGeneratorForNode = numOfGeneratorForNode;
6059
}
6160

62-
public static GeneratorFunctionDefinitionNode create(String name, String enclosingClassName, ExpressionNode doc, PythonCore core, Arity arity, StatementNode defaults, RootCallTarget callTarget,
61+
public static GeneratorFunctionDefinitionNode create(String name, String enclosingClassName, ExpressionNode doc, Arity arity, StatementNode defaults, RootCallTarget callTarget,
6362
FrameDescriptor frameDescriptor, DefinitionCellSlots definitionCellSlots, ExecutionCellSlots executionCellSlots,
6463
int numOfActiveFlags, int numOfGeneratorBlockNode, int numOfGeneratorForNode) {
6564
if (!EmptyNode.isEmpty(defaults)) {
66-
return new GeneratorFunctionDefinitionNode(name, enclosingClassName, doc, core, arity, defaults, callTarget,
65+
return new GeneratorFunctionDefinitionNode(name, enclosingClassName, doc, arity, defaults, callTarget,
6766
frameDescriptor, definitionCellSlots, executionCellSlots,
6867
numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode);
6968
}
7069

71-
return new StatelessGeneratorFunctionDefinitionNode(name, enclosingClassName, doc, core, arity, callTarget,
70+
return new StatelessGeneratorFunctionDefinitionNode(name, enclosingClassName, doc, arity, callTarget,
7271
frameDescriptor, definitionCellSlots, executionCellSlots,
7372
numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode);
7473
}
@@ -99,10 +98,10 @@ protected RootCallTarget getGeneratorCallTarget(PCell[] closure) {
9998
public static final class StatelessGeneratorFunctionDefinitionNode extends GeneratorFunctionDefinitionNode {
10099
@CompilationFinal private PGeneratorFunction cached;
101100

102-
public StatelessGeneratorFunctionDefinitionNode(String name, String enclosingClassName, ExpressionNode doc, PythonCore core, Arity arity, RootCallTarget callTarget,
101+
public StatelessGeneratorFunctionDefinitionNode(String name, String enclosingClassName, ExpressionNode doc, Arity arity, RootCallTarget callTarget,
103102
FrameDescriptor frameDescriptor, DefinitionCellSlots definitionCellSlots, ExecutionCellSlots executionCellSlots,
104103
int numOfActiveFlags, int numOfGeneratorBlockNode, int numOfGeneratorForNode) {
105-
super(name, enclosingClassName, doc, core, arity, BlockNode.create(), callTarget,
104+
super(name, enclosingClassName, doc, arity, BlockNode.create(), callTarget,
106105
frameDescriptor, definitionCellSlots, executionCellSlots,
107106
numOfActiveFlags, numOfGeneratorBlockNode, numOfGeneratorForNode);
108107
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/ArgListCompiler.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,17 @@
5454
import com.oracle.graal.python.parser.antlr.Python3Parser.VdefparameterContext;
5555
import com.oracle.graal.python.parser.antlr.Python3Parser.VkwargsparameterContext;
5656
import com.oracle.graal.python.parser.antlr.Python3Parser.VsplatparameterContext;
57-
import com.oracle.graal.python.runtime.PythonCore;
57+
import com.oracle.graal.python.runtime.PythonParser.ParserErrorCallback;
5858

5959
public class ArgListCompiler<T> extends Python3BaseVisitor<T> {
6060
public boolean arglist, keywordlist;
6161
public final List<String> names;
6262
public final List<String> fpnames;
6363
public final List<ParserRuleContext> init_code;
64-
private final PythonCore core;
64+
private final ParserErrorCallback errors;
6565

66-
public ArgListCompiler(PythonCore core) {
67-
this.core = core;
66+
public ArgListCompiler(ParserErrorCallback errors) {
67+
this.errors = errors;
6868
arglist = keywordlist = false;
6969
// defaults = null;
7070
names = new ArrayList<>();
@@ -80,7 +80,7 @@ public void reset() {
8080

8181
private void addName(String name) {
8282
if (names.contains(name)) {
83-
throw core.raise(SyntaxError, "duplicate argument name found");
83+
throw errors.raise(SyntaxError, "duplicate argument name found");
8484
}
8585
names.add(name);
8686
}
@@ -92,7 +92,7 @@ public T visitVdefparameter(VdefparameterContext ctx) {
9292
if (ctx.test() != null) {
9393
init_code.add(ctx);
9494
} else if (!init_code.isEmpty() && !arglist) {
95-
throw core.raise(SyntaxError, "non-default argument follows default argument. Line %d column %d", ctx.start.getLine(), ctx.start.getCharPositionInLine());
95+
throw errors.raise(SyntaxError, "non-default argument follows default argument. Line %d column %d", ctx.start.getLine(), ctx.start.getCharPositionInLine());
9696
}
9797
return null;
9898
}
@@ -129,7 +129,7 @@ public T visitDefparameter(DefparameterContext ctx) {
129129
if (ctx.test() != null) {
130130
init_code.add(ctx);
131131
} else if (!init_code.isEmpty() && !arglist) {
132-
throw core.raise(SyntaxError, "non-default argument follows default argument. Line %d column %d", ctx.start.getLine(), ctx.start.getCharPositionInLine());
132+
throw errors.raise(SyntaxError, "non-default argument follows default argument. Line %d column %d", ctx.start.getLine(), ctx.start.getCharPositionInLine());
133133
}
134134
return null;
135135
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/parser/AssignmentTranslator.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@
5555
import com.oracle.graal.python.parser.antlr.Python3Parser.NormassignContext;
5656
import com.oracle.graal.python.parser.antlr.Python3Parser.Star_exprContext;
5757
import com.oracle.graal.python.parser.antlr.Python3Parser.TestContext;
58-
import com.oracle.graal.python.runtime.PythonCore;
58+
import com.oracle.graal.python.runtime.PythonParser.ParserErrorCallback;
5959

6060
public class AssignmentTranslator extends Python3BaseVisitor<PNode> {
6161

6262
private final NodeFactory factory;
6363
private final TranslationEnvironment environment;
6464
private final PythonTreeTranslator translator;
65-
private final PythonCore core;
65+
private final ParserErrorCallback errors;
6666

67-
public AssignmentTranslator(PythonCore core, TranslationEnvironment environment, PythonTreeTranslator translator) {
68-
this.core = core;
69-
this.factory = core.getLanguage().getNodeFactory();
67+
public AssignmentTranslator(ParserErrorCallback errors, TranslationEnvironment environment, PythonTreeTranslator translator) {
68+
this.errors = errors;
69+
this.factory = errors.getLanguage().getNodeFactory();
7070
this.environment = environment;
7171
this.translator = translator;
7272
}
@@ -113,7 +113,7 @@ private StatementNode createDestructuringAssignment(ExpressionNode[] leftHandSid
113113
temps[i] = tempRead;
114114
if (leftHandSides[i] instanceof StarredExpressionNode) {
115115
if (starredIndex != -1) {
116-
throw core.raise(SyntaxError, "two starred expressions in assignment");
116+
throw errors.raise(SyntaxError, "two starred expressions in assignment");
117117
}
118118
starredIndex = i;
119119
statements[i] = createAssignment(((StarredExpressionNode) leftHandSides[i]).getValue(), (ExpressionNode) tempRead);
@@ -133,7 +133,7 @@ private PNode createMultiAssignment(List<NormassignContext> normassign, Expressi
133133
for (int i = 0; i < normassign.size() - 1; i++) {
134134
NormassignContext normassignContext = normassign.get(i);
135135
if (normassignContext.yield_expr() != null) {
136-
throw core.raise(SyntaxError, "assignment to yield expression not possible");
136+
throw errors.raise(SyntaxError, "assignment to yield expression not possible");
137137
}
138138
assignments[i + 2] = createAssignment((ExpressionNode) normassignContext.accept(this), (ExpressionNode) tmp);
139139
}
@@ -164,7 +164,7 @@ private PNode makeAugmentedAssignment(Expr_stmtContext ctx) {
164164

165165
private PNode makeAugmentedAssignment(ExpressionNode lhs, String text, ExpressionNode rhs) {
166166
if (!(lhs instanceof ReadNode)) {
167-
throw core.raise(SyntaxError, "illegal expression for augmented assignment");
167+
throw errors.raise(SyntaxError, "illegal expression for augmented assignment");
168168
}
169169
ExpressionNode binOp = factory.createInplaceOperation(text, lhs, rhs);
170170
PNode duplicate = factory.duplicate(lhs, PNode.class);
@@ -175,7 +175,7 @@ private PNode makeAugmentedAssignment(ExpressionNode lhs, String text, Expressio
175175
private PNode visitTargetlist(ParserRuleContext ctx, int starSize) {
176176
if (starSize > 0) {
177177
if (starSize > 1) {
178-
throw core.raise(SyntaxError, "%d starred expressions in assigment", starSize);
178+
throw errors.raise(SyntaxError, "%d starred expressions in assigment", starSize);
179179
}
180180
}
181181
List<ExpressionNode> targets = new ArrayList<>();
@@ -198,13 +198,13 @@ private PNode visitTargetlist(ParserRuleContext ctx, int starSize) {
198198
} else if (pNode instanceof TupleLiteralNode || pNode instanceof ListLiteralNode) {
199199
return pNode;
200200
} else if (pNode instanceof StarredExpressionNode) {
201-
throw core.raise(SyntaxError, "starred assignment target must be in a list or tuple");
201+
throw errors.raise(SyntaxError, "starred assignment target must be in a list or tuple");
202202
} else {
203203
String text = ctx.getText();
204204
if (environment.isNonlocal(text)) {
205-
throw core.raise(SyntaxError, "no binding for nonlocal variable \"%s\" found", text);
205+
throw errors.raise(SyntaxError, "no binding for nonlocal variable \"%s\" found", text);
206206
}
207-
throw core.raise(SyntaxError, "Cannot assign to %s", pNode);
207+
throw errors.raise(SyntaxError, "Cannot assign to %s", pNode);
208208
}
209209
} else {
210210
return factory.createObjectLiteral(targets.toArray(new ExpressionNode[0]));

0 commit comments

Comments
 (0)