-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added basic parsing error reporting and fixed issue in grammar
- Loading branch information
1 parent
89357e2
commit 7747c1b
Showing
8 changed files
with
139 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,22 @@ | ||
class ASTMismatch(Exception): | ||
pass | ||
|
||
|
||
class ParsingError(SyntaxError): | ||
description: str = "" | ||
|
||
def __str__(self): | ||
context, line, column = self.args # pylint: disable=unpacking-non-sequence | ||
return f":{line}:{column}: {self.description}\n{context}" | ||
|
||
|
||
class GenericParsingError(ParsingError): | ||
description = "unspecified parsing error" | ||
|
||
|
||
class UnbalancedParentheses(ParsingError): | ||
description = "unbalanced parentheses" | ||
|
||
|
||
class UnbalancedBrackets(ParsingError): | ||
description = "unbalanced brackets" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,43 @@ | ||
import pytest | ||
import lark | ||
from gersemi.exceptions import ( | ||
ParsingError, | ||
UnbalancedParentheses, | ||
UnbalancedBrackets, | ||
) | ||
from .tests_generator import generate_input_only_tests | ||
|
||
|
||
def test_parser(parser, case): | ||
try: | ||
parser.parse(case.content) | ||
except lark.UnexpectedInput: | ||
except ParsingError: | ||
pytest.fail("invalid input to parse") | ||
raise | ||
|
||
|
||
@pytest.mark.parametrize( | ||
["invalid_code", "expected_exception"], | ||
[ | ||
("set(FOO BAR", UnbalancedParentheses), | ||
("message(FOO BAR (BAZ AND FOO)", UnbalancedParentheses), | ||
("set(FOO BAR # )", UnbalancedParentheses), | ||
("bar)", UnbalancedParentheses), | ||
("bar(FOO BAR (BAZ OR FOO)))", UnbalancedParentheses), | ||
("another_command #(", UnbalancedParentheses), | ||
("foo_command", UnbalancedParentheses), | ||
("foo([[foo]=])", UnbalancedBrackets), | ||
("foo([=[bar]])", UnbalancedBrackets), | ||
("foo(arg1 arg2 [==[arg3]===] arg4)", UnbalancedBrackets), | ||
], | ||
) | ||
def test_invalid_code_parsing_error(parser, invalid_code, expected_exception): | ||
try: | ||
parser.parse(invalid_code) | ||
raise AssertionError("Parser should throw an exception") | ||
except ParsingError as e: | ||
assert isinstance(e, expected_exception) | ||
|
||
|
||
pytest_generate_tests = generate_input_only_tests( | ||
where="parser", input_extension=".cmake", | ||
) |