def parse(self, tokens): #вспомогательные конструкции первого уровня obr = skip(ntype("{")) cbr = skip(ntype("}")) osbr = skip(ntype("[")) csbr = skip(ntype("]")) check = skip(ntype("?")) ref = skip(ntype(">>")) alt = skip(ntype("|")) uns = skip(ntype("#")) star = skip(ntype("@")) twospot = skip(ntype(":")) #текст text = ntype("text") >> self.create_str value = (obr + text + cbr) >> self.create_var reference = (ref + text) >> self.create_ref cycle = (uns + reference + star + text + uns) >> self.create_cycle flag = (obr + text + twospot + (oneplus(text) >> zip) + cbr) >> self.create_flag alternative = future() tryme = future() ctr_ = oneplus(text | value | reference | alternative | flag | cycle | tryme) >> zip ctr = ctr_ + finish() alternative.define((osbr + ctr_.join(alt) + csbr) >> self.create_alter) tryme.define((check + osbr + ctr_.join(alt) + csbr) >> self.create_tryme) return ctr.parse(tokens)[0]
def test_node_combine(self): node = ntype("int") self.assertTrue(isinstance(ntype("int"), Node)) list( map( self.assertTrue, [ isinstance(nvalue("smth"), Node), isinstance(node + node, Node), isinstance(node | node, Node), isinstance(many(node), Node), isinstance(oneplus(node), Node), isinstance(future(), Node), isinstance(node.join(node), Node), isinstance(maby(node), Node), isinstance(exact("smth"), Node), isinstance(finish(), Node), isinstance(skip(node), Node), ], ) )
def parse(tokens): integer = ntype("int") >> (lambda t: int(t.value)) parser = oneplus(integer + integer) return parser.parse(tokens)[0]