def _trans_tokendef(self, ast): ret = Ast('tokendef') token_id = ast.find_children_by_id('token_id')[0] regex = ast.find_children_by_id('regex')[0] ret.add_child(Ast('id', token_id.value)) ret.add_child(Ast('regex', regex.value)) return ret
def _cond_branch(ast): ret = Ast('branch') test = Ast('test') ret.add_child(test) test.add_children_by_id(ast, 'test') consequent = Ast('consequent') ret.add_child(consequent) consequent.add_children_by_id(ast, 'consequent') return ret
def _call(ast): ret = Ast('call') callee = Ast('callee') ret.add_child(callee) callee.add_children_by_id(ast, 'callee') args = Ast('arguments') ret.add_child(args) args.add_children_by_id(ast, 'arg') return ret
def _trans_productionrule(self, ast): ret = Ast('ruledef') annots = ast.find_children_by_id('annot') if annots: ret.set_attr('start', "true") id_ = ast.find_children_by_id('rule_id')[0] ret.add_child(Ast('id', id_.value)) rhs = ast.find_children_by_id('rhs')[0] rhs.id = "" ret.add_child(rhs) return ret
def _fundef(ast): ret = Ast('fundef') name_node = ast.find_children_by_id('name')[0] ret.set_attr('name', name_node.value) params = Ast('parameters') ret.add_child(params) param_nodes = ast.find_children_by_id('param') for param_node in param_nodes: params.add_child(Ast('parameter', param_node.value)) vararg = ast.find_children_by_id('vararg') if vararg: vararg = vararg[0] params.add_child(Ast('var', vararg.value[:-1])) localdefs = Ast('localdefs') ret.add_child(localdefs) localdefs.add_children_by_id(ast, 'localdef') body = Ast('body') ret.add_child(body) body.add_children_by_id(ast, 'body') return ret
def _trans_stringdef(self, ast): ret = Ast('stringdef') start = ast.find_children_by_id('start')[0] end = ast.find_children_by_id('end')[0] escs = ast.find_children_by_id('escape') esc = escs and escs[0].value or None ids = ast.find_children_by_id('token_id') token_id = ids and ids[0].value or 'STRING' ret.add_child(Ast('id', token_id)) ret.add_child(Ast('start', start.value)) ret.add_child(Ast('end', end.value)) if esc is not None: ret.add_child(Ast('escape', esc)) return ret
def _trans_commentdef(self, ast): ret = Ast('commentdef') start = ast.find_children_by_id('start')[0] end = ast.find_children_by_id('end')[0] ret.add_child(Ast('start', start.value)) ret.add_child(Ast('end', end.value)) if ast.find_children_by_id('nestable'): ret.add_child(Ast('nestable')) return ret
def _trans_branch(self, ast): ret = Ast('sequence') children = ast.get_children() prev = None for child in children: if child.id == 'card': card_child = child.get_children()[0] cardinality = { '?': 'optional', '+': 'one-or-more', '*': 'many' }[card_child.value] prev.set_attr('cardinality', cardinality) else: if child.id == "keyword": child = Ast('keyword', child.value) ret.add_child(child) prev = child if len(ret.get_children()) != 1: return ret else: return prev
def _if_expr(ast): ret = Ast('if_expr') test = Ast('test') ret.add_child(test) test.add_children_by_id(ast, 'test') consequent = Ast('consequent') ret.add_child(consequent) consequent.add_children_by_id(ast, 'consequent') alternate = Ast('alternate') ret.add_child(alternate) alternate.add_children_by_id(ast, 'alternate') return ret
def _start(ast): ret = Ast('hackeme') for child in ast.get_children(): child.id = '' ret.add_child(child) return ret
def _trans_whitespace(self, ast): wschars = ast.find_children_by_id('wschars') ret = Ast('whitespace') for wschar in wschars: ret.add_child(Ast('wschar', wschar.value)) return ret