本文整理匯總了Python中jasy.js.parse.Node.Node.append方法的典型用法代碼示例。如果您正苦於以下問題:Python Node.append方法的具體用法?Python Node.append怎麽用?Python Node.append使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類jasy.js.parse.Node.Node
的用法示例。
在下文中一共展示了Node.append方法的13個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: compactIf
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def compactIf(node, thenPart, condition):
"""
Reduces the size of a if statement (without elsePart) using boolean operators
instead of the typical keywords e.g.
"if(something)make()" is translated to "something&&make()"
which is two characters shorter. This however only works when the
thenPart is only based on expressions and does not contain other
statements.
"""
thenExpression = getattr(thenPart, "expression", None)
if not thenExpression:
# Empty semicolon statement => translate if into semicolon statement
node.remove(condition)
node.remove(node.thenPart)
node.append(condition, "expression")
node.type = "semicolon"
else:
# Has expression => Translate IF using a AND or OR operator
if condition.type == "not":
replacement = Node(thenPart.tokenizer, "or")
condition = condition[0]
else:
replacement = Node(thenPart.tokenizer, "and")
replacement.append(condition)
replacement.append(thenExpression)
thenPart.append(replacement, "expression")
fixParens(thenExpression)
fixParens(condition)
node.parent.replace(node, thenPart)
示例2: __createSimpleAssignment
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def __createSimpleAssignment(identifier, valueNode):
assignNode = Node(None, "assign")
identNode = Node(None, "identifier")
identNode.value = identifier
assignNode.append(identNode)
assignNode.append(valueNode)
return assignNode
示例3: createHook
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def createHook(condition, thenPart, elsePart):
""" Creates a hook expression with the given then/else parts """
hook = Node(condition.tokenizer, "hook")
hook.append(condition, "condition")
hook.append(thenPart, "thenPart")
hook.append(elsePart, "elsePart")
return hook
示例4: combineExpressions
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def combineExpressions(condition, thenExpression, elseExpression):
""" Combines then and else expression using a hook statement. """
hook = createHook(condition, thenExpression, elseExpression)
semicolon = Node(condition.tokenizer, "semicolon")
semicolon.append(hook, "expression")
fixParens(condition)
fixParens(thenExpression)
fixParens(elseExpression)
return semicolon
示例5: combineToCommaExpression
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def combineToCommaExpression(node):
"""
This method tries to combine a block with multiple statements into
one semicolon statement with a comma expression containing all expressions
from the previous block. This only works when the block exclusively consists
of expressions as this do not work with other statements. Still this conversion
reduces the size impact of many blocks and leads to the removal of a lot of
curly braces in the result code.
Example: {x++;y+=3} => x++,x+=3
"""
if node == None or node.type != "block":
return node
counter = 0
for child in node:
if child is None:
pass
elif child.type != "semicolon":
return node
else:
counter = counter + 1
if counter == 1:
return node
comma = Node(node.tokenizer, "comma")
for child in list(node):
if child is None:
pass
# Ignore empty semicolons
if hasattr(child, "expression"):
comma.append(child.expression)
semicolon = Node(node.tokenizer, "semicolon")
semicolon.append(comma, "expression")
parent = node.parent
parent.replace(node, semicolon)
return semicolon
示例6: reworkElse
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def reworkElse(node, elsePart):
"""
If an if ends with a return/throw we are able to inline the content
of the else to the same parent as the if resides into. This method
deals with all the nasty details of this operation.
"""
target = node.parent
targetIndex = target.index(node)+1
# A workaround for compact if-else blocks
# We are a elsePart of the if where we want to move our
# content to. This cannot work. So we need to wrap ourself
# into a block and move the else statements to this newly
# established block
if not target.type in ("block","script"):
newBlock = Node(None, "block")
newBlock.wrapped = True
# Replace node with newly created block and put ourself into it
node.parent.replace(node, newBlock)
newBlock.append(node)
# Update the target and the index
target = newBlock
targetIndex = 1
if not target.type in ("block", "script"):
# print("No possible target found/created")
return elsePart
if elsePart.type == "block":
for child in reversed(elsePart):
target.insert(targetIndex, child)
# Remove else block from if statement
node.remove(elsePart)
else:
target.insert(targetIndex, elsePart)
return
示例7: __rebuildAsAssignment
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def __rebuildAsAssignment(node, firstVarStatement):
"""Rebuilds the items of a var statement into a assignment list and moves declarations to the given var statement"""
assignment = Node(node.tokenizer, "semicolon")
assignmentList = Node(node.tokenizer, "comma")
assignment.append(assignmentList, "expression")
# Casting to list() creates a copy during the process (keeps loop stable)
for child in list(node):
if hasattr(child, "name"):
# Cleanup initializer and move to assignment
if hasattr(child, "initializer"):
assign = __createSimpleAssignment(child.name, child.initializer)
assignmentList.append(assign)
firstVarStatement.append(child)
else:
# JS 1.7 Destructing Expression
for identifier in child.names:
firstVarStatement.append(__createDeclaration(identifier.value))
if hasattr(child, "initializer"):
assign = __createMultiAssignment(child.names, child.initializer)
assignmentList.append(assign)
node.remove(child)
# Patch parent node to contain assignment instead of declaration
if len(assignmentList) > 0:
node.parent.replace(node, assignment)
# Special process for "for-in" loops
# It is OK to be second because of assignments are not allowed at
# all in for-in loops and so the first if basically does nothing
# for these kind of statements.
elif getattr(node, "rel", None) == "iterator":
if hasattr(child, "name"):
node.parent.replace(node, __createIdentifier(child.name))
else:
# JS 1.7 Destructing Expressions
node.parent.replace(node, child.names)
# Edge case. Not yet found if this happen realistically
else:
if hasattr(node, "rel"):
logging.warn("Remove related node (%s) from parent: %s" % (node.rel, node))
node.parent.remove(node)
# Minor post-cleanup. Remove useless comma statement when only one expression is the result
if len(assignmentList) == 1:
assignment.replace(assignmentList, assignmentList[0])
示例8: __rebuildAsSplitted
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def __rebuildAsSplitted(self, value, mapper):
""" The real splitter engine. Creates plus Node instances and cascade them automatically """
result = []
splits = self.__replacer.split(value)
if len(splits) == 1:
return None
pair = Node(None, "plus")
for entry in splits:
if entry == "":
continue
if len(pair) == 2:
newPair = Node(None, "plus")
newPair.append(pair)
pair = newPair
if self.__replacer.match(entry):
pos = int(entry[1]) - 1
# Items might be added multiple times. Copy to protect original.
try:
repl = mapper[pos]
except KeyError:
raise TranslationError("Invalid positional value: %s in %s" % (entry, value))
copied = copy.deepcopy(mapper[pos])
if copied.type not in ("identifier", "call"):
copied.parenthesized = True
pair.append(copied)
else:
child = Node(None, "string")
child.value = entry
pair.append(child)
return pair
示例9: __createMultiAssignment
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def __createMultiAssignment(names, valueNode):
assignNode = Node(None, "assign")
assignNode.append(names)
assignNode.append(valueNode)
return assignNode
示例10: createReturn
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def createReturn(value):
""" Creates a return statement with the given value """
ret = Node(value.tokenizer, "return")
ret.append(value, "value")
return ret
示例11: __optimize
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
#.........這裏部分代碼省略.........
if result is not None and len(str(result)) < len(compressor.compress(node)):
logging.debug("Precompute numeric %s operation at line: %s" % (operator, node.line))
firstNumber.value = result
node.parent.replace(node, firstNumber)
# Pre-combine strings (even supports mixed string + number concats)
elif node.type == "plus" and node[0].type in ("number", "string") and node[1].type in ("number", "string"):
logging.debug("Joining strings at line: %s", node.line)
node[0].value = "%s%s" % (node[0].value, node[1].value)
node[0].type = "string"
node.parent.replace(node, node[0])
# Unwrap blocks
if node.type == "block":
if node.parent.type in ("try", "catch", "finally"):
pass
elif len(node) == 0:
logging.debug("Replace empty block with semicolon at line: %s", node.line)
repl = Node(node.tokenizer, "semicolon")
node.parent.replace(node, repl)
node = repl
elif len(node) == 1:
if node.parent.type == "if" and node.rel == "thenPart" and hasattr(node.parent, "elsePart") and containsIf(node):
# if with else where the thenBlock contains another if
pass
elif node.parent.type == "if" and node.rel == "thenPart" and containsIfElse(node):
# if without else where the thenBlock contains a if-else
pass
elif node.parent.type in ("case", "default"):
# virtual blocks inside case/default statements
pass
else:
# logging.debug("Removing block for single statement at line %s", node.line)
node.parent.replace(node, node[0])
node = node[0]
else:
node = combineToCommaExpression(node)
# Remove "empty" semicolons which are inside a block/script parent
if node.type == "semicolon":
if not hasattr(node, "expression"):
if node.parent.type in ("block", "script"):
logging.debug("Remove empty semicolon expression at line: %s", node.line)
node.parent.remove(node)
elif node.parent.type == "if":
rel = getattr(node, "rel", None)
if rel == "elsePart":
logging.debug("Remove empty else part at line: %s", node.line)
node.parent.remove(node)
# Process all if-statements
if node.type == "if":
condition = node.condition
thenPart = node.thenPart
elsePart = getattr(node, "elsePart", None)
# Optimize for empty thenPart if elsePart is available
if thenPart.type == "semicolon" and not hasattr(thenPart, "expression") and elsePart:
if condition.type == "not":
node.replace(condition, condition[0])
condition = condition[0]
else:
repl = Node(None, "not")
node.replace(condition, repl)
repl.append(condition)
fixParens(condition)
condition = repl
node.replace(thenPart, elsePart)
thenPart = elsePart
elsePart = None
# Optimize using hook operator
if elsePart and thenPart.type == "return" and elsePart.type == "return":
# Combine return statement
replacement = createReturn(createHook(condition, thenPart.value, elsePart.value))
node.parent.replace(node, replacement)
return
# Check whether if-part ends with a return statement. Then
# We do not need a else statement here and just can wrap the whole content
# of the else block inside the parent
if elsePart and endsWithReturnOrThrow(thenPart):
reworkElse(node, elsePart)
elsePart = None
# Optimize using "AND" or "OR" operators
# Combine multiple semicolon statements into one semicolon statement using an "comma" expression
thenPart = combineToCommaExpression(thenPart)
elsePart = combineToCommaExpression(elsePart)
# Optimize remaining if or if-else constructs
if elsePart:
mergeParts(node, thenPart, elsePart, condition, compressor)
elif thenPart.type == "semicolon":
compactIf(node, thenPart, condition)
示例12: __recurser
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def __recurser(self, node):
# Process children
for child in list(node):
if child != None:
self.__recurser(child)
if node.type == "call":
funcName = None
if node[0].type == "identifier":
funcName = node[0].value
elif node[0].type == "dot" and node[0][1].type == "identifier":
funcName = node[0][1].value
if funcName in ("tr", "trc", "trn", "marktr"):
params = node[1]
table = self.__table
# Remove marktr() calls
if funcName == "marktr":
node.parent.remove(node)
# Verify param types
elif params[0].type is not "string":
# maybe something marktr() relevant being used, in this case we need to keep the call and inline the data
pass
# Error handling
elif (funcName == "trn" or funcName == "trc") and params[1].type != "string":
warn("Expecting translation string to be type string: %s at line %s" % (params[1].type, params[1].line))
# Signature tr(msg, arg1, arg2, ...)
elif funcName == "tr":
key = params[0].value
if key in table:
params[0].value = table[key]
if len(params) == 1:
node.parent.replace(node, params[0])
else:
self.__splitTemplate(node, params[0], params[1:])
# Signature trc(hint, msg, arg1, arg2, ...)
elif funcName == "trc":
key = params[0].value
if key in table:
params[1].value = table[key]
if len(params) == 2:
node.parent.replace(node, params[1])
else:
self.__splitTemplate(node, params[1], params[2:])
# Signature trn(msg, msg2, [...], int, arg1, arg2, ...)
elif funcName == "trn":
keySingular = params[0].value
if keySingular in table:
params[0].value = table[keySingular]
keyPlural = params[1].value
if keyPlural in table:
params[1].value = table[keyPlural]
# TODO: Multi plural support
# Patch strings with dynamic values
if len(params) >= 3:
self.__splitTemplate(params[0], params[0], params[3:])
self.__splitTemplate(params[1], params[1], params[3:])
# Replace the whole call with: int < 2 ? singularMessage : pluralMessage
hook = Node(None, "hook")
hook.parenthesized = True
condition = Node(None, "le")
condition.append(params[2])
number = Node(None, "number")
number.value = 1
condition.append(number)
hook.append(condition, "condition")
hook.append(params[1], "elsePart")
hook.append(params[0], "thenPart")
node.parent.replace(node, hook)
示例13: __recurser
# 需要導入模塊: from jasy.js.parse.Node import Node [as 別名]
# 或者: from jasy.js.parse.Node.Node import append [as 別名]
def __recurser(node, unused):
"""
The cleanup part which always processes one scope and cleans up params and
variable definitions which are unused
"""
retval = False
# Process children
if node.type != "function":
for child in node:
# None children are allowed sometimes e.g. during array_init like [1,2,,,7,8]
if child != None:
if __recurser(child, unused):
retval = True
if node.type == "script" and hasattr(node, "parent"):
# Remove unused parameters
params = getattr(node.parent, "params", None)
if params:
# Start from back, as we can only remove params as long
# as there is not a required one after the current one
for identifier in reversed(params):
if identifier.value in unused:
logging.debug("Removing unused parameter '%s' in line %s", identifier.value, identifier.line)
params.remove(identifier)
retval = True
else:
break
# Remove function names which are unused
if node.parent.functionForm == "expressed_form":
funcName = getattr(node.parent, "name", None)
if funcName != None and funcName in unused:
logging.debug("Removing unused function name at line %s" % node.line)
del node.parent.name
retval = True
elif node.type == "function":
# Remove full unused functions (when not in top-level scope)
if node.functionForm == "declared_form" and getattr(node, "parent", None) and node.parent.type != "call":
funcName = getattr(node, "name", None)
if funcName != None and funcName in unused:
logging.debug("Removing unused function declaration %s at line %s" % (funcName, node.line))
node.parent.remove(node)
retval = True
elif node.type == "var":
for decl in reversed(node):
if getattr(decl, "name", None) in unused:
if hasattr(decl, "initializer"):
init = decl.initializer
if init.type in ("null", "this", "true", "false", "identifier", "number", "string", "regexp"):
logging.debug("Removing unused primitive variable %s at line %s" % (decl.name, decl.line))
node.remove(decl)
retval = True
elif init.type == "function" and (not hasattr(init, "name") or init.name in unused):
logging.debug("Removing unused function variable %s at line %s" % (decl.name, decl.line))
node.remove(decl)
retval = True
# If we have only one child, we replace the whole var statement with just the init block
elif len(node) == 1:
semicolon = Node(init.tokenizer, "semicolon")
semicolon.append(init, "expression")
# Protect non-expressions with parens
if init.type in ("array_init", "object_init"):
init.parenthesized = True
node.parent.replace(node, semicolon)
retval = True
# If we are the last declaration, move it out of node and append after var block
elif node[-1] == decl or node[0] == decl:
isFirst = node[0] == decl
node.remove(decl)
nodePos = node.parent.index(node)
semicolon = Node(init.tokenizer, "semicolon")
semicolon.append(init, "expression")
# Protect non-expressions with parens
if init.type in ("array_init", "object_init"):
init.parenthesized = True
if isFirst:
node.parent.insert(nodePos, semicolon)
else:
node.parent.insert(nodePos + 1, semicolon)
retval = True
else:
logging.debug("Could not automatically remove unused variable %s at line %s without possible side-effects" % (decl.name, decl.line))
#.........這裏部分代碼省略.........