Skip to content

Commit

Permalink
Merge pull request #24 from cdamours/unused-element
Browse files Browse the repository at this point in the history
Don't raise UnusedElementError when used has more elems than Grammar
  • Loading branch information
joente authored Mar 26, 2024
2 parents 4694185 + 71dd44b commit cb44e4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pyleri/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def __new__(mcs, name, bases, attrs, **kwargs):
elems = {
elem for elem in attrs['_order']
if isinstance(attrs[elem], Element)}
if used != elems:
if elems - used:
raise UnusedElementError(
'Unused element(s) found: {}'.format(
', '.join(elems - used)))
Expand Down
19 changes: 19 additions & 0 deletions test/test_choice.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
Sequence,
Choice,
Keyword,
Regex,
) # nopep8


Expand Down Expand Up @@ -39,6 +40,24 @@ def test_choice_first_match(self):
self.assertFalse(grammar.parse(' hi iris ').is_valid)
self.assertFalse(grammar.parse(' hi sasha ').is_valid)

def test_choice_with_named_elements(self):
int_value = Regex(r"\d+")
int_value.name = "INT_VALUE"

float_value = Regex(r"\d+(\.\d+)?")
float_value.name = "FLOAT_VALUE"

choice = Choice(float_value, int_value)
grammar = create_grammar(choice)

result = grammar.parse("invalid")

expecting = {str(element) for element in result.expecting}

self.assertIn('"INT_VALUE"', expecting)
self.assertIn('"FLOAT_VALUE"', expecting)
self.assertNotIn("Regex", expecting)


if __name__ == '__main__':
unittest.main()

0 comments on commit cb44e4e

Please sign in to comment.