diff --git a/pyleri/grammar.py b/pyleri/grammar.py index 40fcc3b..9d9a1dc 100644 --- a/pyleri/grammar.py +++ b/pyleri/grammar.py @@ -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))) diff --git a/test/test_choice.py b/test/test_choice.py index d2ca71f..105fc7b 100644 --- a/test/test_choice.py +++ b/test/test_choice.py @@ -10,6 +10,7 @@ Sequence, Choice, Keyword, + Regex, ) # nopep8 @@ -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()