def _define_grammer(self): """Define the grammar to be used, and add the various actions""" self._define_actions() node_name = Word(alphas, alphanums) lb = Literal("(").suppress() clb = Literal("(").suppress() rb = Literal(")").suppress() crb = Literal(")").suppress() node = Forward() children = clb + Group(ZeroOrMore(node)) + crb node << lb + node_name + Optional(children) + rb empty_tree = lb + rb tree = node | empty_tree self._grammar = tree node_name.addParseAction(self._name_action) clb.addParseAction(self._clb_action) crb.addParseAction(self._crb_action)
def _define_grammer(self): '''Define the grammar to be used, and add the various actions''' self._define_actions() node_name = Word(alphas, alphanums) lb = Literal('(').suppress() clb = Literal('(').suppress() rb = Literal(')').suppress() crb = Literal(')').suppress() node = Forward() children = clb + Group(ZeroOrMore(node)) + crb node << lb + node_name + Optional(children) + rb empty_tree = lb + rb tree = node | empty_tree self._grammar = tree node_name.addParseAction(self._name_action) clb.addParseAction(self._clb_action) crb.addParseAction(self._crb_action)
def push_current(): global current_node, node_stack node_stack.append(current_node) current_node = None def pop_current(): global current_node, node_stack current_node = node_stack.pop() node_name = Word(alphas, alphanums) node_name.addParseAction(create_node) lb = Literal('(').suppress() clb = Literal('(').suppress() clb.addParseAction(push_current) rb = Literal(')').suppress() crb = Literal(')').suppress() crb.addParseAction(pop_current) node = Forward() children = clb + Group(ZeroOrMore(node)) + crb node << lb + node_name + Optional(children) + rb tree_str = '(c1 ((c2) (c3 ((c4) (c5))) (c6)))' results = node.parseString(tree_str) print(results) writer = IndentedStringWriter() print(writer.write(current_node))
def field_cb(tok): if tok[0] == "array": return ArrayField(tok[0], tok[1]) return Field(tok[0], tok[1]) def all_cb(tok): return AllSub() def contents_cb(tok): return Contents(tok[0], tok[1]) field_element = field_with_type.addParseAction( field_cb) ^ contents.addParseAction(contents_cb) ^ all_sub.addParseAction( all_cb) identifier = package_or_class field_list = Suppress("[") + delimitedList(field_element) + Suppress("]") ag_grammar = Optional(identifier)("base_id") + Optional(field_list)("fields") site_identifier = Word(nums).addParseAction(lambda tok: int(tok[0])) sync_node = (Literal("{s").addParseAction(lambda tok: "SYNC") + site_identifier + Suppress("}")) transition_node = (Suppress("{") + Literal("t").addParseAction(lambda tok: "TR") + site_identifier + Suppress(",") + site_identifier + Suppress("}")) call_node = (Literal("{").addParseAction(lambda tok: "C") + site_identifier +
def push_current(): global current_node, node_stack node_stack.append(current_node) current_node = None def pop_current(): global current_node, node_stack current_node = node_stack.pop() node_name = Word(alphas, alphanums) node_name.addParseAction(create_node) lb = Literal('(').suppress() clb = Literal('(').suppress() clb.addParseAction(push_current) rb = Literal(')').suppress() crb = Literal(')').suppress() crb.addParseAction(pop_current) node = Forward() children = clb + Group(ZeroOrMore(node)) + crb node << lb + node_name + Optional(children) + rb tree_str = '(c1 ((c2) (c3 ((c4) (c5))) (c6)))' results = node.parseString(tree_str) print(results) writer = IndentedStringWriter() print(writer.write(current_node))