def test_no_unnamed_nodes(self): """Certain nodes, when translated, are missing names.""" grammar = r''' Grammar: ArgumentsGrammar start: <arguments-section> <arguments-section> ::= <ahead> <line> # Causes an missing production name. | <ahead> # Simplified the remaning grammar. <line> ::= "TokenType\.LINE" <ahead> ::= "TokenType\.AHEAD" ''' tree = Parser().parse(grammar) node = Translator().translate(tree) python = node.to_python() unacceptable_pattern = re.compile(r'P\([^"]') self.assertEqual( unacceptable_pattern.findall(python), [], 'Found production which doesn\'t begin with a name:\n{}'.format( python), )
def test_translate_annotation_follows_pattern(self): """Annotations should be the first item in the tuple.""" grammar = r''' start: <d> <d> ::= <s> <s> ::= @Wrong <l> | @Wrong <l> <nl> <l> ::= "l" <nl> ::= "n" | ε ''' tree = Parser().parse(grammar) node = Translator().translate(tree) python = node.to_python() self.assertEqual( python.count(', [Wrong]'), 0, python, )