def _trans_ruleref(self, ast): children = ast.get_children() if len(children) == 1: return Ast('ruleref', children[0].value) else: ret = Ast('ruleref', children[2].value) ret.set_attr('data-id', children[0].value) return ret
def _trans_branches(self, ast): ret = Ast('oneof') ret.add_children_by_id(ast, 'branch') children = ret.get_children() if len(children) != 1: return ret else: return children[0]
def _list_item(ast): children = ast.find_children_by_id('single') if children: ret = children[0] ret.id = '' return ret else: ret = Ast('list') ret.add_children_by_id(ast, 'li') 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 enter_node(self, node): if node.has_attr('root'): self._ast = node.copy() self._node_stack.append(self._ast) self._func_stack = [{}] elif node.name == 'fundef': arity = Ast("arity") func_name = node.get_attr('name') funcs = self._func_stack[-1] if func_name not in funcs: func_node = node.copy() funcs[func_name] = func_node self._add_to_parent(func_node) else: func_node = funcs[func_name] func_node.add_child(arity) self._node_stack.append(arity) self._func_stack.append({}) else: self._node_stack.append(node.copy())
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 _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 _list(ast): ret = Ast('list') ret.add_children_by_id(ast, 'li') return ret
def _boolean(ast): child = ast.get_children()[0] if child.value == '#t' or child.value == '#true': return Ast('TRUE') else: return Ast('FALSE')
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
def _cond_expr(ast): ret = Ast('cond') ret.add_children_by_id(ast, 'branch') 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 _start(ast): ret = Ast('hackeme') for child in ast.get_children(): child.id = '' ret.add_child(child) return ret
def _vardef(ast): ret = Ast('vardef') name_node = ast.find_children_by_id('name')[0] ret.set_attr('name', name_node.value) ret.add_children_by_id(ast, 'value') 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 _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 _operator(ast): ret = Ast('operator') op = ast.get_children()[0].value ret.set_attr('value', op) 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 _trans_case_sensitive(self, ast): state = ast.get_children()[1].value return Ast('case_sensitive', state)