Skip to content

Commit 7a2bad6

Browse files
authored
Merge pull request #15 from jg-rp/improve-unbalanced-brackets
Improve error messages related to unbalanced brackets
2 parents eb0a88d + 4c632a7 commit 7a2bad6

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

jsonpath_rfc9535/lex.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,12 @@ def tokenize(query: str) -> List[Token]:
509509
lexer, tokens = lex(query)
510510
lexer.run()
511511

512-
# Check for remaining opening brackets that have not been closes.
512+
if tokens and tokens[-1].type_ == TokenType.ERROR:
513+
raise JSONPathSyntaxError(tokens[-1].message, token=tokens[-1])
514+
515+
# Check for remaining opening brackets that have not been closed.
513516
if lexer.bracket_stack:
514-
ch, index = lexer.bracket_stack[0]
517+
ch, index = lexer.bracket_stack[-1]
515518
msg = f"unbalanced {'brackets' if ch == '[' else 'parentheses'}"
516519
raise JSONPathSyntaxError(
517520
msg,
@@ -524,7 +527,4 @@ def tokenize(query: str) -> List[Token]:
524527
),
525528
)
526529

527-
if tokens and tokens[-1].type_ == TokenType.ERROR:
528-
raise JSONPathSyntaxError(tokens[-1].message, token=tokens[-1])
529-
530530
return tokens

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ exclude = [
121121
"dist",
122122
"node_modules",
123123
"venv",
124+
"tests/nts",
124125
]
125126

126127
# Same as Black.

tests/test_errors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ def env() -> JSONPathEnvironment:
1818

1919
def test_unclosed_selection_list(env: JSONPathEnvironment) -> None:
2020
with pytest.raises(
21-
JSONPathSyntaxError, match=r"unbalanced brackets, line 1, column 1"
21+
JSONPathSyntaxError, match=r"unbalanced brackets, line 1, column 5"
2222
):
2323
env.compile("$[1,2")
2424

2525

2626
def test_unclosed_selection_list_inside_filter(env: JSONPathEnvironment) -> None:
2727
with pytest.raises(
28-
JSONPathSyntaxError, match=r"unbalanced brackets, line 1, column 1"
28+
JSONPathSyntaxError, match=r"unclosed bracketed selection, line 1, column 10"
2929
):
3030
env.compile("$[[email protected] < 1")
3131

0 commit comments

Comments
 (0)