Skip to content

Commit 797754f

Browse files
steve-sDSouzaM
authored andcommitted
Fix some parser errors did not have location available
1 parent ee620d0 commit 797754f

File tree

2 files changed

+78
-26
lines changed

2 files changed

+78
-26
lines changed

graalpython/com.oracle.graal.python.test/src/tests/test_parser.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
1+
# Copyright (c) 2018, 2024, Oracle and/or its affiliates. All rights reserved.
22
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
33
#
44
# The Universal Permissive License (UPL), Version 1.0
@@ -268,6 +268,14 @@ def test_invalid_return_statement():
268268
assert_raise_syntax_error("return 10", "'return' outside function")
269269
assert_raise_syntax_error("class A: return 10\n", "'return' outside function")
270270

271+
def test_outside_of_loop_errors():
272+
assert_raise_syntax_error("break", "'break' outside loop")
273+
# TODO: parser gives invalid syntax for this one, but should be: "'break' outside loop"
274+
assert_raise_syntax_error("def bar(): break", "")
275+
assert_raise_syntax_error("continue", "'continue' not properly in loop")
276+
# TODO: parser gives invalid syntax for this one, but should be: "'continue' not properly in loop"
277+
assert_raise_syntax_error("def foo(): continue", "")
278+
271279

272280
def test_mangled_class_property():
273281
class P:

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java

+69-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,48 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* The Universal Permissive License (UPL), Version 1.0
6+
*
7+
* Subject to the condition set forth below, permission is hereby granted to any
8+
* person obtaining a copy of this software, associated documentation and/or
9+
* data (collectively the "Software"), free of charge and under any and all
10+
* copyright rights in the Software, and any and all patent rights owned or
11+
* freely licensable by each licensor hereunder covering either (i) the
12+
* unmodified Software as contributed to or provided by such licensor, or (ii)
13+
* the Larger Works (as defined below), to deal in both
14+
*
15+
* (a) the Software, and
16+
*
17+
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
18+
* one is included with the Software each a "Larger Work" to which the Software
19+
* is contributed by such licensors),
20+
*
21+
* without restriction, including without limitation the rights to copy, create
22+
* derivative works of, display, perform, and distribute the Software and make,
23+
* use, sell, offer for sale, import, export, have made, and have sold the
24+
* Software and the Larger Work(s), and to sublicense the foregoing rights on
25+
* either these or other terms.
26+
*
27+
* This license is subject to the following condition:
28+
*
29+
* The above copyright notice and either this complete permission notice or at a
30+
* minimum a reference to the UPL must be included in all copies or substantial
31+
* portions of the Software.
32+
*
33+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39+
* SOFTWARE.
40+
*/
141
package com.oracle.graal.python.compiler.bytecode_dsl;
242

343
import static com.oracle.graal.python.compiler.CompilationScope.Class;
4-
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
544
import static com.oracle.graal.python.nodes.SpecialAttributeNames.J___CLASS__;
45+
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
646

747
import java.util.ArrayList;
848
import java.util.Arrays;
@@ -24,17 +64,17 @@
2464
import com.oracle.graal.python.compiler.Compiler;
2565
import com.oracle.graal.python.compiler.Compiler.ConstantCollection;
2666
import com.oracle.graal.python.compiler.OpCodes.CollectionBits;
67+
import com.oracle.graal.python.compiler.Unparser;
2768
import com.oracle.graal.python.compiler.bytecode_dsl.BytecodeDSLCompiler.BytecodeDSLCompilerContext;
2869
import com.oracle.graal.python.compiler.bytecode_dsl.BytecodeDSLCompiler.BytecodeDSLCompilerResult;
29-
import com.oracle.graal.python.compiler.Unparser;
3070
import com.oracle.graal.python.nodes.StringLiterals;
3171
import com.oracle.graal.python.nodes.bytecode_dsl.BytecodeDSLCodeUnit;
3272
import com.oracle.graal.python.nodes.bytecode_dsl.PBytecodeDSLRootNode;
3373
import com.oracle.graal.python.nodes.bytecode_dsl.PBytecodeDSLRootNodeGen;
3474
import com.oracle.graal.python.nodes.bytecode_dsl.PBytecodeDSLRootNodeGen.Builder;
35-
import com.oracle.graal.python.pegparser.FutureFeature;
3675
import com.oracle.graal.python.pegparser.ErrorCallback.ErrorType;
3776
import com.oracle.graal.python.pegparser.ErrorCallback.WarningType;
77+
import com.oracle.graal.python.pegparser.FutureFeature;
3878
import com.oracle.graal.python.pegparser.scope.Scope;
3979
import com.oracle.graal.python.pegparser.scope.Scope.DefUse;
4080
import com.oracle.graal.python.pegparser.sst.AliasTy;
@@ -48,31 +88,31 @@
4888
import com.oracle.graal.python.pegparser.sst.ExceptHandlerTy;
4989
import com.oracle.graal.python.pegparser.sst.ExprContextTy;
5090
import com.oracle.graal.python.pegparser.sst.ExprTy;
91+
import com.oracle.graal.python.pegparser.sst.ExprTy.Constant;
92+
import com.oracle.graal.python.pegparser.sst.ExprTy.DictComp;
93+
import com.oracle.graal.python.pegparser.sst.ExprTy.GeneratorExp;
94+
import com.oracle.graal.python.pegparser.sst.ExprTy.Lambda;
95+
import com.oracle.graal.python.pegparser.sst.ExprTy.ListComp;
96+
import com.oracle.graal.python.pegparser.sst.ExprTy.SetComp;
5197
import com.oracle.graal.python.pegparser.sst.KeywordTy;
5298
import com.oracle.graal.python.pegparser.sst.MatchCaseTy;
5399
import com.oracle.graal.python.pegparser.sst.ModTy;
54100
import com.oracle.graal.python.pegparser.sst.OperatorTy;
55101
import com.oracle.graal.python.pegparser.sst.PatternTy;
56102
import com.oracle.graal.python.pegparser.sst.SSTNode;
57103
import com.oracle.graal.python.pegparser.sst.StmtTy;
104+
import com.oracle.graal.python.pegparser.sst.StmtTy.AsyncFunctionDef;
58105
import com.oracle.graal.python.pegparser.sst.UnaryOpTy;
59106
import com.oracle.graal.python.pegparser.sst.WithItemTy;
60-
import com.oracle.graal.python.pegparser.sst.ExprTy.Constant;
61-
import com.oracle.graal.python.pegparser.sst.ExprTy.DictComp;
62-
import com.oracle.graal.python.pegparser.sst.ExprTy.GeneratorExp;
63-
import com.oracle.graal.python.pegparser.sst.ExprTy.Lambda;
64-
import com.oracle.graal.python.pegparser.sst.ExprTy.ListComp;
65-
import com.oracle.graal.python.pegparser.sst.ExprTy.SetComp;
66-
import com.oracle.graal.python.pegparser.sst.StmtTy.AsyncFunctionDef;
67107
import com.oracle.graal.python.pegparser.tokenizer.SourceRange;
68108
import com.oracle.graal.python.util.PythonUtils;
69109
import com.oracle.truffle.api.CompilerDirectives;
70110
import com.oracle.truffle.api.bytecode.BytecodeConfig;
71111
import com.oracle.truffle.api.bytecode.BytecodeLabel;
72112
import com.oracle.truffle.api.bytecode.BytecodeLocal;
113+
import com.oracle.truffle.api.bytecode.BytecodeParser;
73114
import com.oracle.truffle.api.bytecode.BytecodeRootNodes;
74115
import com.oracle.truffle.api.instrumentation.StandardTags.StatementTag;
75-
import com.oracle.truffle.api.bytecode.BytecodeParser;
76116
import com.oracle.truffle.api.strings.TruffleString;
77117

78118
/**
@@ -82,7 +122,7 @@
82122
* This visitor is a small wrapper that calls into another visitor, {@link StatementCompiler}, to
83123
* produce bytecode for the various statements/expressions within the AST.
84124
*/
85-
public class RootNodeCompiler implements BaseBytecodeDSLVisitor<BytecodeDSLCompilerResult> {
125+
public final class RootNodeCompiler implements BaseBytecodeDSLVisitor<BytecodeDSLCompilerResult> {
86126
/**
87127
* Because a {@link RootNodeCompiler} instance gets reused on reparse, it should be idempotent.
88128
* Consequently, most of its fields are final and immutable/not mutated after construction. For
@@ -303,41 +343,45 @@ private boolean nonEmpty() {
303343
}
304344
}
305345

306-
protected final void checkForbiddenName(String id, NameOperation context) {
346+
private void checkForbiddenName(String id, NameOperation context) {
347+
checkForbiddenName(id, context, currentLocation);
348+
}
349+
350+
private void checkForbiddenName(String id, NameOperation context, SourceRange location) {
307351
if (context == NameOperation.BeginWrite) {
308352
if (id.equals("__debug__")) {
309-
ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "cannot assign to __debug__");
353+
ctx.errorCallback.onError(ErrorType.Syntax, location, "cannot assign to __debug__");
310354
}
311355
}
312356
if (context == NameOperation.Delete) {
313357
if (id.equals("__debug__")) {
314-
ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "cannot delete __debug__");
358+
ctx.errorCallback.onError(ErrorType.Syntax, location, "cannot delete __debug__");
315359
}
316360
}
317361
}
318362

319-
private void checkForbiddenArgs(ArgumentsTy args) {
363+
private void checkForbiddenArgs(ArgumentsTy args, SourceRange location) {
320364
if (args != null) {
321365
if (args.posOnlyArgs != null) {
322366
for (ArgTy arg : args.posOnlyArgs) {
323-
checkForbiddenName(arg.arg, NameOperation.BeginWrite);
367+
checkForbiddenName(arg.arg, NameOperation.BeginWrite, location);
324368
}
325369
}
326370
if (args.args != null) {
327371
for (ArgTy arg : args.args) {
328-
checkForbiddenName(arg.arg, NameOperation.BeginWrite);
372+
checkForbiddenName(arg.arg, NameOperation.BeginWrite, location);
329373
}
330374
}
331375
if (args.kwOnlyArgs != null) {
332376
for (ArgTy arg : args.kwOnlyArgs) {
333-
checkForbiddenName(arg.arg, NameOperation.BeginWrite);
377+
checkForbiddenName(arg.arg, NameOperation.BeginWrite, location);
334378
}
335379
}
336380
if (args.varArg != null) {
337-
checkForbiddenName(args.varArg.arg, NameOperation.BeginWrite);
381+
checkForbiddenName(args.varArg.arg, NameOperation.BeginWrite, location);
338382
}
339383
if (args.kwArg != null) {
340-
checkForbiddenName(args.kwArg.arg, NameOperation.BeginWrite);
384+
checkForbiddenName(args.kwArg.arg, NameOperation.BeginWrite, location);
341385
}
342386
}
343387
}
@@ -436,7 +480,7 @@ void beginRootNode(SSTNode node, ArgumentsTy args, Builder b) {
436480

437481
b.beginRoot();
438482

439-
checkForbiddenArgs(args);
483+
checkForbiddenArgs(args, node.getSourceRange());
440484
setUpFrame(args, b);
441485

442486
b.emitTraceOrProfileCall();
@@ -4041,10 +4085,10 @@ public Void visit(StmtTy.Raise node) {
40414085

40424086
@Override
40434087
public Void visit(StmtTy.Return node) {
4088+
boolean newStatement = beginSourceSection(node, b);
40444089
if (!scope.isFunction()) {
40454090
ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "'return' outside function");
40464091
}
4047-
boolean newStatement = beginSourceSection(node, b);
40484092
beginReturn(b);
40494093
if (node.value != null) {
40504094
node.value.accept(this);
@@ -4538,21 +4582,21 @@ public Void visit(WithItemTy node) {
45384582

45394583
@Override
45404584
public Void visit(StmtTy.Break aThis) {
4585+
boolean newStatement = beginSourceSection(aThis, b);
45414586
if (breakLabel == null) {
45424587
ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "'break' outside loop");
45434588
}
4544-
boolean newStatement = beginSourceSection(aThis, b);
45454589
b.emitBranch(breakLabel);
45464590
endSourceSection(b, newStatement);
45474591
return null;
45484592
}
45494593

45504594
@Override
45514595
public Void visit(StmtTy.Continue aThis) {
4596+
boolean newStatement = beginSourceSection(aThis, b);
45524597
if (continueLabel == null) {
45534598
ctx.errorCallback.onError(ErrorType.Syntax, currentLocation, "'continue' not properly in loop");
45544599
}
4555-
boolean newStatement = beginSourceSection(aThis, b);
45564600
b.emitBranch(continueLabel);
45574601
endSourceSection(b, newStatement);
45584602
return null;

0 commit comments

Comments
 (0)