def init_ast(self, magic_parent=None): bos = BOS(Terminal(""), 0, []) eos = EOS(FinishSymbol(), 0, []) bos.magic_parent = magic_parent eos.magic_parent = magic_parent bos.next_term = eos eos.prev_term = bos root = TextNode(Nonterminal("Root"), 0, [bos, eos]) self.previous_version = AST(root)
def test_simple(self): bos = self.ast.parent.children[0] new = TextNode(Terminal("1+2")) bos.insert_after(new) self.lexer.relex(new) assert self.parser.inc_parse([]) == True assert self.ast.parent.symbol == Nonterminal("Root") assert isinstance(self.ast.parent.children[0], BOS) assert isinstance(self.ast.parent.children[-1], EOS) bos = self.ast.parent.children[0] root = TextNode(Nonterminal("Root")) bos = BOS(Terminal("")) eos = EOS(FinishSymbol()) Start = TextNode(Nonterminal("Startrule")) root.set_children([bos, Start, eos]) E1 = TextNode(Nonterminal("E")) Start.set_children([TextNode(N("WS")), E1]) E1.set_children(self.make_nodes([N("E"), T("+"), N("WS"), N("T")])) E2 = E1.children[0] E2.set_children(self.make_nodes([N("T")])) T1 = E2.children[0] T1.set_children(self.make_nodes([N("P")])) P1 = T1.children[0] P1.set_children(self.make_nodes([T("1"), N("WS")])) T2 = E1.children[3] T2.set_children(self.make_nodes([N("P")])) P2 = T2.children[0] P2.set_children(self.make_nodes([T("2"), N("WS")])) self.compare_trees(self.ast.parent, root)
def test_nodes(self): root = TextNode(Nonterminal("Root")) bos = BOS(Terminal("")) eos = EOS(FinishSymbol()) a = TextNode(Terminal("a")) b = TextNode(Terminal("b")) nB = TextNode(Nonterminal("B")) nB.set_children([b]) root.set_children([bos, a, nB, eos]) a.next_term = b b.next_term = eos l = Lexer([("name", "[a-z]+")]) assert l.treelex(a) == [("ab", "name", 0)]