def transform_call_expr(self, node: TSNode, source: bytes): func_name_node = node.child_by_field_name('func_name') args_node = node.child_by_field_name('args') assert func_name_node assert args_node func_name = self.transform_ident_expr(func_name_node, source) args = self._transform_argument_list(args_node, source) return CallExpr(node.start_point, node.end_point, func_name, *args)
def transform_while_stmt(self, node: TSNode, source: bytes): condition = node.child_by_field_name('condition') body = node.child_by_field_name('body') assert condition assert body cb = self._transform_codeblock(body, source) return WhileLoop(node.start_point, node.end_point, self.transform_expr(condition, source), cb)
def transform_declare_stmt(self, node: TSNode, source: bytes): typed_ident = node.child_by_field_name('typed_ident') initial_node = node.child_by_field_name('initial') assert typed_ident assert initial_node ident = self._transform_typed_ident(typed_ident, source) initial_value = self.transform_expr(initial_node, source) return DeclareStatement(node.start_point, node.end_point, ident, initial_value)
def transform_binary_expr(self, node: TSNode, source: bytes): operator = node.child_by_field_name('operator') left = node.child_by_field_name('left') right = node.child_by_field_name('right') assert operator assert left assert right return BinaryExpr(node.start_point, node.end_point, BinaryOp(operator.type), self.transform_expr(left, source), self.transform_expr(right, source))
def __init__(self, node: tree_sitter.Node, code: List, father=None): super(FuncDefinition, self).__init__(node=node, code=code, father=father) declarator = node.child_by_field_name("declarator") if declarator is None: raise ValueError( f"Error: declarator of {node.start_point, node.end_point} is None" ) else: declarator: tree_sitter.Node self.declarator = None for child in self.children: if child.node == declarator: self.declarator = child break self.declarator: Node if self.declarator.node.type != "function_declarator": raise ValueError( f"Declarator of {node.start_point, node.end_point} is {self.declarator.node.type}" ) else: func_declarator = self.declarator.node.child_by_field_name( "declarator") parameter_list = self.declarator.node.child_by_field_name( "parameters") if func_declarator is None: raise ValueError( f"No declarator of {node.start_point, node.end_point}") else: for child in self.declarator.children: if child.node == func_declarator: self.func_declarator = child if parameter_list is None: raise ValueError( f"No parameter list of {node.start_point, node.end_point}") else: for child in self.declarator.children: if child.node == parameter_list: self.parameter_list = child body = node.child_by_field_name("body") self.body = None if body is not None: for child in self.children: if child.node == body: self.body = child break
def transform_assignment_stmt(self, node: TSNode, source: bytes): left_node = node.child_by_field_name('left') op_node = node.child_by_field_name('operator') right_node = node.child_by_field_name('right') assert left_node assert op_node assert right_node op = AsssignmentOp(self.get_text(op_node, source)) left = self.transform_ident_expr(left_node, source) right = self.transform_expr(right_node, source) return AssignmentExpr(node.start_point, node.end_point, left, op, right)
def transform_parenthesized_expr(self, node: TSNode, source: bytes): expr = node.child_by_field_name('expr') assert expr return ParenthesizedExpr(node.start_point, node.end_point, self.transform_expr(expr, source))
def transform_condition_expr(self, node: TSNode, source: bytes): condition = node.child_by_field_name('condition') consequence = node.child_by_field_name('consequence') alternative = node.child_by_field_name('alternative') assert condition assert consequence assert alternative return ConditionExpr( node.start_point, node.end_point, self.transform_expr(condition, source), self.transform_expr(consequence, source), self.transform_expr(alternative, source), )
def transform_return_stmt(self, node: TSNode, source: bytes): returns = node.child_by_field_name('returns') assert returns return ReturnStatement(node.start_point, node.end_point, self.transform_expr(returns, source))
def _transform_typed_ident(self, node: TSNode, source: bytes) -> TypedIdent: ident_node = node.child_by_field_name('ident') typing_node = node.child_by_field_name('typing') assert ident_node assert typing_node ident = self.get_text(ident_node, source) if typing_node.type == 'ident_expr': typing = self.transform_ident_expr(typing_node, source) elif typing_node.type == 'pointer': underlying_node = typing_node.child_by_field_name('underlying') assert underlying_node typing = self.transform_ident_expr(underlying_node, source) else: raise Exception(f'unexpected typing node type {typing_node.type}') return TypedIdent(ident, typing, typing_node.type == 'pointer')
def transform_unary_expr(self, node: TSNode, source: bytes): operator = node.child_by_field_name('operator') argument = node.child_by_field_name('argument') assert operator assert argument return UnaryExpr(node.start_point, node.end_point, UnaryOp(operator.type), self.transform_expr(argument, source))
def transform_if_stmt(self, node: TSNode, source: bytes): condition = node.child_by_field_name('condition') consequence_node = node.child_by_field_name('consequence') alternative_node = node.child_by_field_name('alternative') assert condition assert consequence_node if alternative_node: consequence = self._transform_codeblock(consequence_node, source) alternative = self._transform_codeblock(alternative_node, source) return If(node.start_point, node.end_point, self.transform_expr(condition, source), consequence, alternative) else: consequence = self._transform_codeblock(consequence_node, source) return If(node.start_point, node.end_point, self.transform_expr(condition, source), consequence, None)
def transform_func_stmt(self, node: TSNode, source: bytes): func_name_node = node.child_by_field_name('func_name') params_node = node.child_by_field_name('params') returns_node = node.child_by_field_name('returns') body_node = node.child_by_field_name('body') assert func_name_node assert body_node assert params_node assert returns_node func_name = self.get_text(func_name_node, source) # 解析函数签名 params = self._transform_params(params_node, source) returns = self.transform_ident_expr(returns_node, source) # 解析函数体内容 body = self._transform_codeblock(body_node, source) return Func(node.start_point, node.end_point, func_name, params, returns, body)
def on_assignment(self, node: Node, value: str, depth: int, breadth: int): # TODO: We're not handling the asssignment properly, ie. # class A: # STATIC = 1 # SOMEVAR[1] = 10 # A,B = (10, 20) name_node = node.child_by_field_name("left") name = self.extract(name_node) if name_node else None self.scope = self.scope.derive(type=type, range=(node.start_byte, node.end_byte), name=name) def on_exit(_, self=self): self.scope = self.scope.parent return on_exit
def on_definition(self, node: Node, value: str, depth: int, breadth: int, type: str = "block"): name_node = node.child_by_field_name("name") name = self.extract(name_node) if name_node else None self.scope = self.scope.derive(type=type, range=(node.start_byte, node.end_byte), name=name) self.mode = "def" def on_exit(_, self=self): self.scope = self.scope.parent return on_exit
def transform_mod(self, node: TSNode, source: bytes): ident_node = node.child_by_field_name('name') assert ident_node name = self.get_text(ident_node, source) return ModDeclare(node.start_point, node.end_point, name)