Skip to content

Commit

Permalink
fix: crash on undetected syntax error
Browse files Browse the repository at this point in the history
  • Loading branch information
boriel committed Nov 26, 2024
1 parent 3f63a11 commit 97dd410
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 26 deletions.
4 changes: 4 additions & 0 deletions src/arch/z80/peephole/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,10 @@ def parse_ifline(if_line: str, lineno: int) -> TreeType | None:
errmsg.warning(lineno, "missing element in list")
return None

if any(x != FN.OP_COMMA for i, x in enumerate(expr) if i % 2):
errmsg.warning(lineno, f"Invalid list {expr}")
return None

stack[-1].append(expr)
expr = stack.pop()
else:
Expand Down
68 changes: 42 additions & 26 deletions tests/arch/zx48k/peephole/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,14 +399,8 @@ def test_parse_if_must_start_in_a_new_line(self):
def test_parse_with_ending_binary_error(self):
result = parser.parse_str(
"""
;; Remove the boolean normalization if it's done after calling
;; certain routines that return the bool result already normalized.
;; The sequence
;; sub 1
;; sbc a, a
;; inc a
;; can be removed
;; Sample Comment
;;
OLEVEL: 1
OFLAG: 20
Expand All @@ -428,15 +422,6 @@ def test_parse_with_ending_binary_error(self):
def test_parse_with_comma_error(self):
result = parser.parse_str(
"""
;; Remove the boolean normalization if it's done after calling
;; certain routines that return the bool result already normalized.
;; The sequence
;; sub 1
;; sbc a, a
;; inc a
;; can be removed
OLEVEL: 1
OFLAG: 20
Expand All @@ -458,15 +443,6 @@ def test_parse_with_comma_error(self):
def test_parse_with_nested_comma_error(self):
result = parser.parse_str(
"""
;; Remove the boolean normalization if it's done after calling
;; certain routines that return the bool result already normalized.
;; The sequence
;; sub 1
;; sbc a, a
;; inc a
;; can be removed
OLEVEL: 1
OFLAG: 20
Expand All @@ -484,3 +460,43 @@ def test_parse_with_nested_comma_error(self):
"""
)
assert result is None

def test_parse_with_list_error(self):
result = parser.parse_str(
"""
OLEVEL: 1
OFLAG: 20
REPLACE {{
$1
}}
WITH {{
}}
IF {{
$1 IN ("x", "y" . "pera")
}}
"""
)
assert result is None

def test_parse_with_list_error2(self):
result = parser.parse_str(
"""
OLEVEL: 1
OFLAG: 20
REPLACE {{
$1
}}
WITH {{
}}
IF {{
$1 IN ("x", , , "pera")
}}
"""
)
assert result is None

0 comments on commit 97dd410

Please sign in to comment.