Skip to content

Commit 64f3bc3

Browse files
committed
feat: improve parser error msgs
1 parent 05140c1 commit 64f3bc3

6 files changed

Lines changed: 44 additions & 0 deletions

File tree

src/api/errmsg.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,4 +333,18 @@ def syntax_error_mandatory_param_after_optional(lineno: int, param1: str, param2
333333
error(lineno, f"Can't declare mandatory param '{param2}' after optional param '{param1}'")
334334

335335

336+
# ----------------------------------------
337+
# FOR without NEXT
338+
# ----------------------------------------
339+
def syntax_error_for_without_next(lineno: int):
340+
error(lineno, "FOR without NEXT")
341+
342+
343+
# ----------------------------------------
344+
# FOR without NEXT
345+
# ----------------------------------------
346+
def syntax_error_loop_not_closed(lineno: int, loop_type: str):
347+
error(lineno, f"{loop_type} loop not closed")
348+
349+
336350
# endregion

src/zxbc/zxbparser.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3401,6 +3401,13 @@ def p_error(p):
34013401
error(p.lineno, msg)
34023402
return
34033403

3404+
# Try to give some hints
3405+
if gl.LOOPS: # some loop(s) are not closed
3406+
loop_info = gl.LOOPS[-1]
3407+
if loop_info.type == LoopType.FOR:
3408+
src.api.errmsg.syntax_error_for_without_next(loop_info.lineno)
3409+
else:
3410+
src.api.errmsg.syntax_error_loop_not_closed(loop_info.lineno, loop_info.type)
34043411
# If there were previous errors, stop here
34053412
# since this end of file is due to previous errors
34063413
if gl.has_errors:

tests/functional/test_errmsg.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,14 @@ dim_str_error0.bas:3: error: Cannot initialize array of type string
206206
>>> process_file('zx48k/dim_str_error1.bas')
207207
dim_str_error1.bas:3: error: Cannot initialize array of type string
208208

209+
# Test parsing error improvements
210+
>>> process_file('zx48k/for_err.bas')
211+
for_err.bas:3: error: FOR without NEXT
212+
>>> process_file('zx48k/while_err.bas')
213+
while_err.bas:3: error: WHILE loop not closed
214+
>>> process_file('zx48k/do_err.bas')
215+
do_err.bas:3: error: DO loop not closed
216+
209217
# Unreachable code detection
210218
# Should not emit warning for these case:
211219
>>> process_file('zx48k/warn_unreach0.bas')

tests/functional/zx48k/do_err.bas

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DIM i, c as Ubyte
2+
3+
DO
4+
LET c = c * 2
5+

tests/functional/zx48k/for_err.bas

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DIM i, c as Ubyte
2+
3+
FOR i = 1 TO 5
4+
LET c = c * 2
5+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DIM i, c as Ubyte
2+
3+
WHILE i = 1
4+
LET c = c * 2
5+

0 commit comments

Comments
 (0)