Example #1
0
 def test_given_tree_then_correct_squash(self) -> None:
     from cmaj.testing.ast import tree, token
     node = tree(('X', [('X', [token('Y', 0),
                               ('X', [token('Y', 1),
                                      token('X', 2)])]),
                        ('Y', [('X', [token('X', 3)])])]))
     result = squash(node, 'X')
     expected = tree(('X', [token('Y', 0), token('Y', 1), token('X', 2), ('Y', [token('X', 3)])]))
     self.assertEqual(expected, result)
Example #2
0
def compile_grammar(grammar_node: Node) -> Grammar:
    from cmaj.ast.simplify import squash, prune, skip
    grammar_node = squash(grammar_node, 'GRAMMAR', 'OPTION', 'SEQUENCE')
    grammar_node = prune(grammar_node, 'comment', '=', '|', 'eol')
    grammar_node = skip(grammar_node, 'LINE', 'ANCHOR')

    rules: List[Rule] = []
    for definition_node in grammar_node.children:
        identifier, option = definition_node.children
        rules += compile_rules(identifier.token.value, option)
    return Grammar(*rules)
Example #3
0
 def test_given_root_is_leaf_then_no_squash(self) -> None:
     from cmaj.testing.ast import tree
     tree_def = ('node', [])
     result = squash(tree(tree_def), 'node')
     self.assertEqual(tree(tree_def), result)
Example #4
0
 def test_given_nodes_to_squash_separated_by_other_node_then_no_squash(self) -> None:
     from cmaj.testing.ast import tree, token
     tree_def = ('X', [('Y', [token('X', 0)])])
     result = squash(tree(tree_def), 'X')
     self.assertEqual(tree(tree_def), result)
Example #5
0
 def test_given_chain_then_squash_to_single_node(self) -> None:
     from cmaj.testing.ast import tree, token
     node = tree(('Y', [('X', [('X', [('X', [token('Y', 0)])])])]))
     result = squash(node, 'X')
     expected = tree(('Y', [('X', [token('Y', 0)])]))
     self.assertEqual(expected, result)
Example #6
0
 def test_given_inner_node_to_squash_then_grandchildren_become_children(self) -> None:
     from cmaj.testing.ast import tree, token
     node = tree(('Y', [('X', [('X', [token('Y', 0), token('Y', 1)])])]))
     result = squash(node, 'X')
     expected = tree(('Y', [('X', [token('Y', 0), token('Y', 1)])]))
     self.assertEqual(expected, result)
Example #7
0
 def test_given_many_children_to_squash_when_parent_is_root_then_no_squash(self) -> None:
     from cmaj.testing.ast import tree, token
     tree_def = ('X', [token('X', 0), token('X', 1)])
     result = squash(tree(tree_def), 'X')
     self.assertEqual(tree(tree_def), result)
Example #8
0
 def test_given_single_child_to_squash_when_parent_is_root_then_child_becomes_root(self) -> None:
     from cmaj.testing.ast import tree, token
     node = tree(('X', [token('X', 0)]))
     result = squash(node, 'X')
     expected = tree(token('X', 0))
     self.assertEqual(expected, result)