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


Python Node.Node类代码示例

本文整理汇总了Python中jasy.js.parse.Node.Node的典型用法代码示例。如果您正苦于以下问题:Python Node类的具体用法?Python Node怎么用?Python Node使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


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

示例1: PRIMARY_build

 def PRIMARY_build(self, tokenizer, tokenType):
     # NB: tokenizer.token.type must be "null", "this", "true", "false", "identifier", "number", "string", or "regexp".
     node = Node(tokenizer, tokenType)
     if tokenType in ("identifier", "string", "regexp", "number"):
         node.value = tokenizer.token.value
         
     return node
开发者ID:zhangming0305,项目名称:jasy,代码行数:7,代码来源:VanillaBuilder.py

示例2: __createSimpleAssignment

def __createSimpleAssignment(identifier, valueNode):
    assignNode = Node(None, "assign")
    identNode = Node(None, "identifier")
    identNode.value = identifier
    assignNode.append(identNode)
    assignNode.append(valueNode)

    return assignNode
开发者ID:csakatoku,项目名称:jasy,代码行数:8,代码来源:CombineDeclarations.py

示例3: FUNCTION_build

 def FUNCTION_build(self, tokenizer):
     node = Node(tokenizer)
     if node.type != "function":
         if tokenizer.token.value == "get":
             node.type = "getter"
         else:
             node.type = "setter"
             
     return node
开发者ID:zhangming0305,项目名称:jasy,代码行数:9,代码来源:VanillaBuilder.py

示例4: createHook

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
    
开发者ID:csakatoku,项目名称:jasy,代码行数:8,代码来源:BlockReducer.py

示例5: combineExpressions

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
开发者ID:csakatoku,项目名称:jasy,代码行数:12,代码来源:BlockReducer.py

示例6: __rebuildAsAssignment

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])
开发者ID:csakatoku,项目名称:jasy,代码行数:52,代码来源:CombineDeclarations.py

示例7: reworkElse

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  
开发者ID:csakatoku,项目名称:jasy,代码行数:42,代码来源:BlockReducer.py

示例8: compactIf

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)
开发者ID:csakatoku,项目名称:jasy,代码行数:35,代码来源:BlockReducer.py

示例9: __rebuildAsSplitted

    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
开发者ID:Val9,项目名称:jasy,代码行数:39,代码来源:Translation.py

示例10: combineToCommaExpression

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
开发者ID:csakatoku,项目名称:jasy,代码行数:46,代码来源:BlockReducer.py

示例11: __createDeclaration

def __createDeclaration(name):
    declNode = Node(None, "declaration")
    declNode.name = name
    declNode.readOnly = False
    return declNode
开发者ID:csakatoku,项目名称:jasy,代码行数:5,代码来源:CombineDeclarations.py

示例12: __createMultiAssignment

def __createMultiAssignment(names, valueNode):
    assignNode = Node(None, "assign")
    assignNode.append(names)
    assignNode.append(valueNode)

    return assignNode    
开发者ID:csakatoku,项目名称:jasy,代码行数:6,代码来源:CombineDeclarations.py

示例13: DO_build

 def DO_build(self, tokenizer):
     node = Node(tokenizer, "do")
     node.isLoop = True
     return node
开发者ID:zhangming0305,项目名称:jasy,代码行数:4,代码来源:VanillaBuilder.py

示例14: CATCH_wrapException

 def CATCH_wrapException(self, tokenizer):
     node = Node(tokenizer, "exception")
     node.value = tokenizer.token.value
     return node
开发者ID:zhangming0305,项目名称:jasy,代码行数:4,代码来源:VanillaBuilder.py

示例15: createReturn

def createReturn(value):
    """ Creates a return statement with the given value """
    
    ret = Node(value.tokenizer, "return")
    ret.append(value, "value")
    return ret
开发者ID:csakatoku,项目名称:jasy,代码行数:6,代码来源:BlockReducer.py


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