当前位置: 首页>>代码示例>>Python>>正文


Python Node.add_child方法代码示例

本文整理汇总了Python中magpieparsers.parser_common.Node.add_child方法的典型用法代码示例。如果您正苦于以下问题:Python Node.add_child方法的具体用法?Python Node.add_child怎么用?Python Node.add_child使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在magpieparsers.parser_common.Node的用法示例。


在下文中一共展示了Node.add_child方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: make_scoped_name_ast

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
def make_scoped_name_ast(scope_ast, pt):
	assert pt.name == 'scoped_name'

	ast = Node(None, "expression", leaf = 'scoped_name')

	# Sanity check
	for child in pt.children:
		assert child.name in ('scope_operator', 'identifier')

	# Build the name.
	scoped_name_list = [child.leaf for child in pt.children]
	value = ''.join(scoped_name_list)

	# Check that it's valid
	target_asts = infogripper.getAllNodes(value, scope_ast, None)
	if len(target_asts) > 1:
		raise error.AmbiguousScopedNameError(value, scope_ast, target_asts)
	elif len(target_asts) == 0:
		raise error.NameNotFoundError(value, pt)

	target_wrapper = Node(scope_ast, 'target', [target_asts[0]])
	ast.add_child(target_wrapper)
	ast.add_attribute('value', value)

	return ast
开发者ID:BruceYi,项目名称:okl4,代码行数:27,代码来源:astexpr.py

示例2: type_from_declarator

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
	def type_from_declarator(self, parent_ast, declarator):
		"""
		Construct a partial type. Only used for typdefs currently.
		"""
		typenode = TypeNode(parent_ast, None, source = declarator)
		# Don't know the meta type here

		if declarator.my_children_are('simple_declarator'):
			typenode.leaf = declarator.the('simple_declarator').leaf
			#typenode.add_attribute('target_type', typeref)
		else: #complex_declarator
			newtype = TypeNode(parent_ast, source = declarator)
			newtype.add_attribute('meta_type','array')
			
			array_dcl = declarator.the('complex_declarator').the('array_declarator')
			array_dim = []
			shape = Node(newtype, 'shape', source = declarator)
			newtype.add_child(shape)

			for dimension in array_dcl.children:
				exp_node = dimension.the('positive_int_const').the('const_exp')
				expr = self.getExpression(shape, exp_node)
				if expr.attribute('value') < 0:
					raise DimensionOutOfRangeError(dimension)
				shape.add_child(expr)
			
			typenode.leaf = array_dcl.leaf
			typenode.add_child(newtype)
			
		assert typenode.leaf != None
		return typenode
开发者ID:BruceYi,项目名称:okl4,代码行数:33,代码来源:astgen.py

示例3: make_literal_ast

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
def make_literal_ast(scope_ast, pt):
	assert pt.name == 'literal'
	child_pt = pt.children[0]

	ast = Node(None, "expression")

	ast_type_name = TYPENAME_MAP.get(child_pt.name)
	if ast_type_name is None:
		raise Error("Unknown literal type %s" % child_pt.name)
	
	target_type = infogripper.getNode(ast_type_name, scope_ast, 'type')
	if target_type is None:
		raise Error("Couldn't find type %s in AST" % (ast_type_name))
	
	if target_type.has_attribute('smallest') and target_type.has_attribute('largest'):
		# FIXME: Shouldn't be doing this.
		ast_type_name = smallest_type_hack(get_python_value(child_pt.leaf))
		target_type = infogripper.getNode(ast_type_name, scope_ast, 'type')

	# FIXME: Use of eval is evil
	ast.add_attribute('value', get_python_value(child_pt.leaf))

	# Remember the original name for later.
	ast.add_attribute('display_name', child_pt.name)

	ast.add_child(target_type)

	return ast
开发者ID:BruceYi,项目名称:okl4,代码行数:30,代码来源:astexpr.py

示例4: get_call

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
def get_call(scope_ast, pt, ast_gen):
    """
	Translate function calls. PT looks like this:
	expression call
	 expression identifier
	  (value) = <function name>
	 expression comma (optional)
	  expression param1
	  expression param2
	  ...
	
	We turn them into this AST:
	expression call
	 expression scoped_name
	  (value) = <function name>
	 expression parameters (optional)
	  expression param1
	  expression param2
	  ...
	"""
    # Translate function calls.
    node = Node(scope_ast, "expression", leaf="call", source=pt)

    expression_identifier = pt.children[0]
    assert expression_identifier.leaf == "id_expression"

    node.add_child(get_scoped_name(node, expression_identifier, ast_gen))

    # Add parameters.
    if len(pt.children) == 2:
        node.add_child(_get_call_params(node, pt.children[1], ast_gen))

    return node
开发者ID:berkus,项目名称:okl4,代码行数:35,代码来源:astexpr.py

示例5: enum_type

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
	def enum_type(self, ast, pt, defining_scope):
		type_node = TypeNode(ast, source = pt)
		type_node.add_attribute('meta_type', 'enum')

		enum_list = []
		for child in pt.children:
			if child.type == 'identifier':
				type_node.leaf = child.leaf
			elif child.type == 'enumerator_list':
				for enum in child['enumerator']:
					enum_list.append(enum.leaf)
				type_node.add_attribute('enumeration', enum_list)
	
		# All the enum internal names get added to the outer scope (Do they?!)
		if defining_scope:
			# Add the enum itself, too.
			defining_scope.add_child(type_node)

			for count, child_pt in enumerate(pt.the('enumerator_list')['enumerator']):
				enum_item_ast = TypeNode(defining_scope, leaf = child_pt.leaf, source = child)
				defining_scope.add_child(enum_item_ast)
				enum_item_ast.add_attribute('meta_type', 'enum_member')
				enum_item_ast.add_attribute('value', count)
				self._dupes_check(enum_item_ast)

				enum_item_target = Node(enum_item_ast, 'target')
				enum_item_target.add_child(type_node)
				enum_item_ast.add_child(enum_item_target)


		return type_node
开发者ID:BruceYi,项目名称:okl4,代码行数:33,代码来源:astgen.py

示例6: param_dcl

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
	def param_dcl(self, ast, pt):
		"""
		Param decls look like constant declarations: refer to "const_dcl".
		1.	parameter blah  (3)
			+- meta_type = ['param']
		    +- direction = ['in']
			+- indirection = ['*']
			+- target = [<Node object type:unsigned int>]
		"""
		param_ast = Node(ast, 'parameter', None, source = pt)

		# Add direction
		param_ast.add_attribute('direction', pt.leaf)

		for child in pt.children:
			if child.type == 'param_type_spec':
				#param_ast.add_attribute('target',self.param_type_spec(ast, child))
				target_type, is_new_type = self._get_target_type(param_ast, child,
						defining_scope = param_ast)
				param_ast.add_child(target_type)
			elif child.type == 'allow_indirection':
				indirection_list = [child.leaf]
				for indirection in child['allow_indirection']:
					indirection_list.append(indirection.leaf)
				param_ast.add_attribute('indirection', indirection_list)
			elif child.type == 'simple_declarator':
				param_ast.leaf = child.leaf
			else:
				param_ast.add_child(UnkownNode(None, child, source = pt))

		# Check we're not passing raw void parameters.
		if not self.is_valid_parameter_spec(param_ast):
			raise InvalidTypeUsageError(target_type.the('type').leaf, child)
		return param_ast
开发者ID:BruceYi,项目名称:okl4,代码行数:36,代码来源:astgen.py

示例7: create_instance_list

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
	def create_instance_list(self, ast, typeref, declarators):
		instance_list = []
		for child in declarators:
			instance = Node(ast, 'type_instance', None, source = child)
			
			if child.children[0].type == 'simple_declarator':
				instance.leaf = child.children[0].leaf
				instance.add_child(typeref)
			else: #complex_declarator
				newtype = TypeNode(ast, source = pt)
				newtype.add_attribute('meta_type','array')
				
				array_dcl = child.the('complex_declarator').the('array_declarator')
				array_dim = []
				for dimension in array_dcl.children:
					exp_node = dimension.the('positive_int_const').the('const_exp')
					expr = self.getExpression(ast, exp_node)
					array_dim.append(expr.leaf)
					'''
					if res != None:
						array_dcl.append(str(res))
					else:
						array_dcl.append(exp_str)
					'''
				newtype.add_attribute('shape',array_dim)
				newtype.add_child(typeref)
				
				instance.add_child(newtype)
				instance.leaf = array_dcl.leaf
			instance_list.append(instance)
		return instance_list
开发者ID:BruceYi,项目名称:okl4,代码行数:33,代码来源:astgen.py

示例8: decorator_element

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
	def decorator_element(self, ast, pt):
		dec_node = Node(None, 'annotation', source = pt)
		for child in pt.children:
			if child.type == 'identifier':
				dec_node.leaf = child.leaf
			elif child.type == 'expr_list':
				dec_node.add_children(self.expr_list(ast, child, True))
			else:
				dec_node.add_child(UnknownNode(child, source = pt))
		return dec_node
开发者ID:BruceYi,项目名称:okl4,代码行数:12,代码来源:astgen.py

示例9: make_identifier_ast

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
def make_identifier_ast(pt):
	assert pt.name == 'identifier'

	ast = Node(None, "expression")
	child_ast = Node(ast, "identifier")
	ast.add_child(child_ast)

	child_ast.add_attribute('value', pt.leaf)

	return ast
开发者ID:BruceYi,项目名称:okl4,代码行数:12,代码来源:astexpr.py

示例10: attr_declarator

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
	def attr_declarator(self, ast, pt):
		decl = Node(ast, 'declarator', None, source = pt)
		decl.leaf = []
		for child in pt.children:
			if child.type == 'simple_declarator':
				decl.leaf.append(child.leaf)
			elif child.type == 'attr_raises_expr':
				decl.add_child(self.attr_raises_expr(decl, child))
			else:
				decl.add_child(UnkownNode(None, child, source = pt))
		return decl
开发者ID:BruceYi,项目名称:okl4,代码行数:13,代码来源:astgen.py

示例11: value_dcl

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
	def value_dcl(self, ast, pt):
		node = Node(ast, 'valuetype', None, source = pt)
		for child in pt.children:
			if child.type == 'identifier':
				node.leaf = child.leaf
			elif child.type == 'value_inheritance_spec':
				val_inh_node = self.value_inheritance_spec(node, self, child)
				node.add_child(val_inh_node)
			else:
				node.add_child(UnknownNode(child, source = pt))
		return node
开发者ID:BruceYi,项目名称:okl4,代码行数:13,代码来源:astgen.py

示例12: get_conditional_expression

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
def get_conditional_expression(scope_ast, pt, ast_gen):
    """
	Conditional expressions (ternary expression)
	"""
    node = Node(scope_ast, "expression", leaf="ternary_if", source=pt)
    cond_pt, true_pt, false_pt = pt.children

    node.add_child(get_expression(node, cond_pt, ast_gen))
    node.add_child(get_expression(node, true_pt, ast_gen))
    node.add_child(get_expression(node, false_pt, ast_gen))

    return node
开发者ID:berkus,项目名称:okl4,代码行数:14,代码来源:astexpr.py

示例13: string_type

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
	def string_type(self, ast, pt, wstring = False):
		typenode = Node(ast, 'type', source = pt)
		if wstring:
			target = Node(typenode, 'customised', [infogripper.getTypenode('wstring', ast)])
		else:
			target = Node(typenode, 'customised', [infogripper.getTypenode('string', ast)])
		typenode.add_child(target)
		pos_const_node = pt.the('positive_int_const')
		if pos_const_node != None:
			expr = self.getExpression(ast, pos_const_node.the('const_exp'))
			typenode.add_attribute('max_length', expr.leaf)
		# This is a new, anonymous type.
		return typenode
开发者ID:BruceYi,项目名称:okl4,代码行数:15,代码来源:astgen.py

示例14: get_expression

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
def get_expression(scope_ast, pt, ast_gen):
    if pt.name == "expression" and pt.leaf == "" and len(pt.children) == 1:
        pt = pt.children[0]

    if pt.name == "expression" and pt.leaf in OK_ALREADY:
        node = Node(scope_ast, "expression", leaf=pt.leaf, source=pt)
        for child_pt in pt.children:
            node.add_child(get_expression(node, child_pt, ast_gen))
        return node
    elif pt.name == "expression" and pt.leaf in HANDLERS:
        return HANDLERS[pt.leaf](scope_ast, pt, ast_gen)
    else:
        pt.print_tree()
        raise Error("Couldn't translate PT")
开发者ID:berkus,项目名称:okl4,代码行数:16,代码来源:astexpr.py

示例15: get_scoped_name

# 需要导入模块: from magpieparsers.parser_common import Node [as 别名]
# 或者: from magpieparsers.parser_common.Node import add_child [as 别名]
def get_scoped_name(scope_ast, pt, ast_gen):
    the_name = pt.get_single_attribute("value")

    scoped_name = Node(scope_ast, "expression", leaf="scoped_name", source=pt)
    scoped_name.add_attribute("value", the_name)

    # Try to find a target.
    type_ast = getNode(the_name, scope_ast)
    # It's not the end of the world if we don't find a target.
    if type_ast:
        target_ast = Node(scoped_name, "target", [type_ast])
        scoped_name.add_child(target_ast)

    return scoped_name
开发者ID:berkus,项目名称:okl4,代码行数:16,代码来源:astexpr.py


注:本文中的magpieparsers.parser_common.Node.add_child方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。