-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #57 Support for user meta-data + tests.
- Loading branch information
1 parent
fc236c6
commit 3aca5dd
Showing
2 changed files
with
125 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
""" | ||
Allow arbitrary user meta-data. | ||
See issue: https://github.com/igordejanovic/parglare/issues/57 | ||
""" | ||
import pytest | ||
from parglare import Grammar, ParseError | ||
from parglare.grammar import ASSOC_LEFT | ||
|
||
|
||
def test_production_meta_data(): | ||
|
||
grammar_str = r''' | ||
MyRule: 'a' {left, 1, dynamic, nops, label:'My Label'}; | ||
''' | ||
|
||
grammar = Grammar.from_string(grammar_str) | ||
my_rule = grammar.get_nonterminal('MyRule') | ||
|
||
prod = my_rule.productions[0] | ||
assert prod.assoc == ASSOC_LEFT | ||
assert prod.prior == 1 | ||
assert prod.dynamic | ||
assert prod.label == 'My Label' | ||
|
||
with pytest.raises(AttributeError): | ||
prod.non_existing | ||
|
||
|
||
def test_production_meta_data_must_be_key_value(): | ||
|
||
grammar_str = r''' | ||
MyRule: 'a' {left, 1, dynamic, nops, label:'My Label', not_allowed}; | ||
''' | ||
|
||
with pytest.raises(ParseError, match=r'ot_allowed\*}'): | ||
Grammar.from_string(grammar_str) | ||
|
||
|
||
def test_terminal_meta_data(): | ||
|
||
grammar_str = r''' | ||
MyRule: a; | ||
terminals | ||
a: 'a' {dynamic, 1, label: 'My Label'}; | ||
''' | ||
|
||
grammar = Grammar.from_string(grammar_str) | ||
term_a = grammar.get_terminal('a') | ||
|
||
assert term_a.prior == 1 | ||
assert term_a.dynamic | ||
assert term_a.label == 'My Label' | ||
|
||
with pytest.raises(AttributeError): | ||
term_a.non_existing | ||
|
||
|
||
def test_terminal_meta_data_must_be_key_value(): | ||
|
||
grammar_str = r''' | ||
MyRule: a; | ||
terminals | ||
a: 'a' {dynamic, 1, label: 'My Label', not_allowed}; | ||
''' | ||
|
||
with pytest.raises(ParseError, match=r'ot_allowed\*}'): | ||
Grammar.from_string(grammar_str) |