Example #1
0
 def getVariables():
     # get all variable nodes from 'members' and (pot.) 'construct'
     variables = []
     if "members" in classMap:
         variables.extend([node for node in treeutil.nodeIterator(classMap["members"], ["variable"])])
     if "construct" in classMap:
         variables.extend([node for node in treeutil.nodeIterator(classMap["construct"], ["variable"])])
     return variables
Example #2
0
 def checkRequiredBlocks(self):
     for node in treeutil.nodeIterator(self.tree, "loop"):
         block = treeutil.selectNode(node, "statement/block")
         if not block:
             self.log(node, "The statement of loops and conditions should be enclosed by a block in braces '{}'")
     for node in treeutil.nodeIterator(self.tree, "elseStatement"):
         block = treeutil.selectNode(node, "block")
         if not block:
             block = treeutil.selectNode(node, "loop[@loopType='IF']")
         if not block:
             self.log(node, "The statement of loops and conditions should be enclosed by a block in braces '{}'")
Example #3
0
 def getVariables():
     # get all variable nodes from 'members' and (pot.) 'construct'
     variables = []
     if "members" in classMap:
         variables.extend([
             node for node in treeutil.nodeIterator(
                 classMap["members"], ["variable"])
         ])
     if "construct" in classMap:
         variables.extend([
             node for node in treeutil.nodeIterator(
                 classMap["construct"], ["variable"])
         ])
     return variables
Example #4
0
 def findVariables(rootNode):
     variables = []
     for node in treeutil.nodeIterator(rootNode, ["assignment", "call"]):
         if node.type == "assignment":
             variables.append(node.getChild("left"))
         elif node.type == "call":
             variables.append(node.getChild("operand"))
     return variables
Example #5
0
 def checkRequiredBlocks(self):
     for node in treeutil.nodeIterator(self.tree, "loop"):
         block = treeutil.selectNode(node, "statement/block")
         if not block:
             self.log(
                 node,
                 "The statement of loops and conditions should be enclosed by a block in braces '{}'"
             )
     for node in treeutil.nodeIterator(self.tree, "elseStatement"):
         block = treeutil.selectNode(node, "block")
         if not block:
             block = treeutil.selectNode(node, "loop[@loopType='IF']")
         if not block:
             self.log(
                 node,
                 "The statement of loops and conditions should be enclosed by a block in braces '{}'"
             )
 def findMethod(tree, methodName):
     for node in treeutil.nodeIterator(tree, ["function"]):  # check function nodes
         if node.hasParentContext("keyvalue/value"): # it's a key : function() member
             keyvalNode = node.parent.parent
             key = keyvalNode.get("key", False)
             if key and key == methodName:
                 return node
     return None
Example #7
0
def findVariantNodes(node):
    for callnode in list(
        treeutil.nodeIterator(node, ["call"])
    ):  # enforce eagerness so nodes that are moved are still handled
        if isEnvironmentCall(callnode):
            yield treeutil.selectNode(callnode, "operand").getFirstChild()
        else:
            continue
Example #8
0
def findCallNodes(node, key):
    for call_node in treeutil.nodeIterator(node, ["call"]):
        oper_node = call_node.getChild("operand").getChild(0)
        if not oper_node.isVar():
            continue
        oper_str = assembleVar(oper_node)
        if oper_str == key:
            yield call_node
Example #9
0
def findCallNodes(node, key):
    for call_node in treeutil.nodeIterator(node, ["call"]):
        oper_node = call_node.getChild("operand").getChild(0)
        if not oper_node.isVar():
            continue
        oper_str = assembleVar(oper_node)
        if oper_str == key:
            yield call_node
Example #10
0
def findVariantNodes(node):
    for callnode in list(treeutil.nodeIterator(
            node,
        ['call'
         ])):  # enforce eagerness so nodes that are moved are still handled
        if isEnvironmentCall(callnode):
            yield treeutil.selectNode(callnode, "operand").getFirstChild()
        else:
            continue
Example #11
0
 def findVariables(rootNode):
     variables = []
     for node in treeutil.nodeIterator(rootNode,
                                       ["assignment", "call"]):
         if node.type == "assignment":
             variables.append(node.getChild("left"))
         elif node.type == "call":
             variables.append(node.getChild("operand"))
     return variables
Example #12
0
    def _buildScopes(self):
        scopes = {}
        self.globalScope = Scope(self.root, self)
        scopes[self.root] = self.globalScope

        for node in treeutil.nodeIterator(self.root, ["function", "catch"]):
            scope = Scope(node, self)
            scopes[node] = scope

        return scopes
Example #13
0
    def _buildScopes(self):
        scopes = []
        self.globalScope = Scope(self.root, self)
        scopes.append((self.root, self.globalScope))

        for node in treeutil.nodeIterator(self.root, ["function", "catch"]):
            scope = Scope(node, self)
            scopes.append((node, scope))

        return scopes
Example #14
0
 def checkMaps(self):
     for node in treeutil.nodeIterator(self.tree, "map"):
         knownkeys = {}
         if node.hasChildren():
             for child in node.children:
                 if child.type == "keyvalue":
                     key = child.get("key")
                     if key in knownkeys:
                         self.log(child, "Map key '%s' redefined." % key)
                     else:
                         knownkeys[key] = child
Example #15
0
 def checkMaps(self):
     for node in treeutil.nodeIterator(self.tree, "map"):
         knownkeys = {}
         if node.hasChildren():
             for child in node.children:
                 if child.type == "keyvalue":
                     key = child.get("key")
                     if knownkeys.has_key(key):
                         self.log(child, "Map key '%s' redefined." % key)
                     else:
                         knownkeys[key] = child
Example #16
0
    def createEnvKeyProviderIndex(self, classesAll):
        envProviders = {}
        providingEnvClasses = [className for className in classesAll.keys() if className.startswith("qx.bom.client")]
        for className in providingEnvClasses:
            tree = classesAll[className].tree()
            for callnode in list(treeutil.nodeIterator(tree, ['call'])):
                if callnode.toJS(pp).startswith("qx.core.Environment.add"):
                    envKey = treeutil.selectNode(callnode, "arguments/constant/@value")
                    envProviders[envKey] = className

        return envProviders
Example #17
0
    def checkFields(self):
        define = treeutil.findQxDefine(self.tree)
        if not define:
            return

        classMapNode = treeutil.selectNode(define, "params/2")
        if classMapNode is None:
            return

        classMap = treeutil.mapNodeToMap(classMapNode)
        if not classMap.has_key("members"):
            return

        members = treeutil.mapNodeToMap(classMap["members"].children[0])
        restricted = [key for key in members if key.startswith("_")]

        assignNodes = [node for node in treeutil.nodeIterator(classMap["members"], "assignment")]
        if classMap.has_key("construct"):
            for node in treeutil.nodeIterator(classMap["construct"], "assignment"):
                assignNodes.append(node)

        for node in assignNodes:
            this = treeutil.selectNode(node, "left/variable/identifier[1]/@name")
            if this != "this":
                continue

            field = treeutil.selectNode(node, "left/variable/identifier[2]/@name")
            if field is None:
                continue

            if field[0] != "_":
                continue
            elif field[1] == "_":
                prot = "private"
            else:
                prot = "protected"

            if prot == "protected":
                self.log(node, "Protected data field '%s'. Protected fields are deprecated. Better use private fields in combination with getter and setter methods." % field)
            elif not field in restricted:
                self.log(node, "Implicit declaration of %s field '%s'. You should list this field in the members section." % (prot, field))
Example #18
0
    def createEnvKeyProviderIndex(self, classesAll):
        envProviders = {}
        providingEnvClasses = [
            className for className in classesAll.keys()
            if className.startswith("qx.bom.client")
        ]
        for className in providingEnvClasses:
            tree = classesAll[className].tree()
            for callnode in list(treeutil.nodeIterator(tree, ['call'])):
                if callnode.toJS(pp).startswith("qx.core.Environment.add"):
                    envKey = treeutil.selectNode(callnode,
                                                 "arguments/constant/@value")
                    envProviders[envKey] = className

        return envProviders
Example #19
0
 def extractChecksMap(self):
     tree = self.tree()
     checksMap = None
     for node in treeutil.nodeIterator(tree, "keyvalue"):
         if node.get("key", "") == "_checksMap":
             checksMap = node
             break
     assert checksMap
     assert checksMap.hasParentContext("keyvalue/value/map")  # 'statics/_checksMap'
     checksMap = treeutil.selectNode(checksMap, "value/map")
     checksMap = treeutil.mapNodeToMap(checksMap)
     # stringify map values
     for key in checksMap:
         checksMap[key] = checksMap[key].children[0].get("value")
     return checksMap
Example #20
0
 def extractChecksMap(self):
     tree = self.tree()
     checksMap = None
     for node in treeutil.nodeIterator(tree, "keyvalue"):
         if node.get("key", "") == "_checksMap":
             checksMap = node
             break
     assert checksMap
     assert checksMap.hasParentContext("keyvalue/value/map") # 'statics/_checksMap'
     checksMap = treeutil.selectNode(checksMap, "value/map")
     checksMap = treeutil.mapNodeToMap(checksMap)
     # stringify map values
     for key in checksMap:
         try:
             checksMap[key] = checksMap[key].children[0].get("value")
         except NodeAccessException:
             raise ValueError(("Error extracting checks map from %s: " +
                               "expected string value for key '%s' (found %s)") % 
                               (self.id, key, checksMap[key].children[0].type))
     return checksMap
Example #21
0
 def extractChecksMap(self):
     tree = self.tree()
     checksMap = None
     for node in treeutil.nodeIterator(tree, "keyvalue"):
         if node.get("key", "") == "_checksMap":
             checksMap = node
             break
     assert checksMap
     assert checksMap.hasParentContext("keyvalue/value/map") # 'statics/_checksMap'
     checksMap = treeutil.selectNode(checksMap, "value/map")
     checksMap = treeutil.mapNodeToMap(checksMap)
     # stringify map values
     for key in checksMap:
         try:
             checksMap[key] = checksMap[key].children[0].get("value")
         except NodeAccessException:
             raise ValueError(("Error extracting checks map from %s: " +
                               "expected string value for key '%s' (found %s)") % 
                               (self.id, key, checksMap[key].children[0].type))
     return checksMap
def patch(node):
    global cnt
    #for _, commentNode in treeutil.nodeIteratorNonRec(node,["comment", "commentsBefore", "commentsAfter"]):
    for commentNode in treeutil.nodeIterator(node,["comment", "commentsBefore", "commentsAfter"]):
        commentNode.parent.removeChild(commentNode)
Example #23
0
def reduceOperation(literalNode):

    resultNode = None
    treeModified = False

    # can only reduce with constants
    if literalNode.type != "constant":
        return literalNode, False
    else:
        literalValue = constNodeToPyValue(literalNode)

    # check if we're in an operation
    ngParent = nextNongroupParent(literalNode)  # could be "first", "second" etc. in ops
    if not ngParent or not ngParent.parent or ngParent.parent.type != "operation":
        return literalNode, False
    else:
        operationNode = ngParent.parent
    # get operator
    operator = operationNode.get("operator")

    # normalize expression
    noperationNode = normalizeExpression(operationNode)
    # re-gain knownn literal node
    for node in treeutil.nodeIterator(noperationNode, [literalNode.type]):
        if literalNode.attributes == node.attributes:
            nliteralNode = node
            break

    # equal, unequal
    if operator in ["EQ", "SHEQ", "NE", "SHNE"]:
        otherOperand, _ = getOtherOperand(noperationNode, nliteralNode)
        if otherOperand.type != "constant":
            return literalNode, False
        if operator in ["EQ", "SHEQ"]:
            cmpFcn = operators.eq
        elif operator in ["NE", "SHNE"]:
            cmpFcn = operators.ne

        operands = [literalValue]
        otherVal = constNodeToPyValue(otherOperand)
        operands.append(otherVal)

        result = cmpFcn(operands[0], operands[1])
        resultNode = tree.Node("constant")
        resultNode.set("constantType", "boolean")
        resultNode.set("value", str(result).lower())
        resultNode.set("line", noperationNode.get("line"))

    # order compares <, =<, ...
    elif operator in ["LT", "LE", "GT", "GE"]:
        otherOperand, otherPosition = getOtherOperand(noperationNode, nliteralNode)
        if otherOperand.type != "constant":
            return literalNode, False
        if operator == "LT":
            cmpFcn = operators.lt
        elif operator == "LE":
            cmpFcn = operators.le
        elif operator == "GT":
            cmpFcn = operators.gt
        elif operator == "GE":
            cmpFcn = operators.ge

        operands = {}
        operands[1 - otherPosition] = literalValue
        otherVal = constNodeToPyValue(otherOperand)
        operands[otherPosition] = otherVal

        result = cmpFcn(operands[0], operands[1])
        resultNode = tree.Node("constant")
        resultNode.set("constantType", "boolean")
        resultNode.set("value", str(result).lower())
        resultNode.set("line", noperationNode.get("line"))

    # logical ! (not)
    elif operator in ["NOT"]:
        result = not literalValue
        resultNode = tree.Node("constant")
        resultNode.set("constantType", "boolean")
        resultNode.set("value", str(result).lower())
        resultNode.set("line", noperationNode.get("line"))

    # logical operators &&, || -- Currently disabled, s. bug#4856
    elif False and operator in ["AND", "OR"]:
        result = None
        otherOperand, otherPosition = getOtherOperand(noperationNode, nliteralNode)
        if operator == "AND":
            # if otherPosition==1 and not literalValue:  # short circuit
            #    result = False
            # else:
            cmpFcn = lambda x, y: x and y
        elif operator == "OR":
            # if otherPosition==1 and literalValue:  # short circuit
            #    result = True
            # else:
            cmpFcn = lambda x, y: x or y

        if result == None:
            if otherOperand.type != "constant":
                return literalNode, False
            operands = {}
            operands[1 - otherPosition] = literalValue
            otherVal = constNodeToPyValue(otherOperand)
            operands[otherPosition] = otherVal
            result = cmpFcn(operands[0], operands[1])
            resultNode = {literalValue: literalNode, otherVal: otherOperand}[result]

    # hook ?: operator
    elif operator in ["HOOK"]:
        if ngParent.type == "first":  # optimize a literal condition
            if bool(literalValue):
                resultNode = treeutil.selectNode(noperationNode, "second/1", True)
            else:
                resultNode = treeutil.selectNode(noperationNode, "third/1", True)

    # unsupported operation
    else:
        pass

    if resultNode != None:
        # print "optimizing: operation"
        operationNode.parent.replaceChild(operationNode, resultNode)
        treeModified = True
    else:
        resultNode = literalNode
        treeModified = False

    return resultNode, treeModified
Example #24
0
def reduceOperation(literalNode):

    resultNode = None
    treeModified = False

    # can only reduce with constants
    if literalNode.type != "constant":
        return literalNode, False
    else:
        literalValue = constNodeToPyValue(literalNode)

    # check if we're in an operation
    ngParent = nextNongroupParent(
        literalNode)  # could be "first", "second" etc. in ops
    if not ngParent or not ngParent.parent or ngParent.parent.type != "operation":
        return literalNode, False
    else:
        operationNode = ngParent.parent
    # get operator
    operator = operationNode.get("operator")

    # normalize expression
    noperationNode = normalizeExpression(operationNode)
    # re-gain knownn literal node
    for node in treeutil.nodeIterator(noperationNode, [literalNode.type]):
        if literalNode.attributes == node.attributes:
            nliteralNode = node
            break

    # equal, unequal
    if operator in ["EQ", "SHEQ", "NE", "SHNE"]:
        otherOperand, _ = getOtherOperand(noperationNode, nliteralNode)
        if otherOperand.type != "constant":
            return literalNode, False
        if operator in ["EQ", "SHEQ"]:
            cmpFcn = operators.eq
        elif operator in ["NE", "SHNE"]:
            cmpFcn = operators.ne

        operands = [literalValue]
        otherVal = constNodeToPyValue(otherOperand)
        operands.append(otherVal)

        result = cmpFcn(operands[0], operands[1])
        resultNode = tree.Node("constant")
        resultNode.set("constantType", "boolean")
        resultNode.set("value", str(result).lower())
        resultNode.set("line", noperationNode.get("line"))

    # order compares <, =<, ...
    elif operator in ["LT", "LE", "GT", "GE"]:
        otherOperand, otherPosition = getOtherOperand(noperationNode,
                                                      nliteralNode)
        if otherOperand.type != "constant":
            return literalNode, False
        if operator == "LT":
            cmpFcn = operators.lt
        elif operator == "LE":
            cmpFcn = operators.le
        elif operator == "GT":
            cmpFcn = operators.gt
        elif operator == "GE":
            cmpFcn = operators.ge

        operands = {}
        operands[1 - otherPosition] = literalValue
        otherVal = constNodeToPyValue(otherOperand)
        operands[otherPosition] = otherVal

        result = cmpFcn(operands[0], operands[1])
        resultNode = tree.Node("constant")
        resultNode.set("constantType", "boolean")
        resultNode.set("value", str(result).lower())
        resultNode.set("line", noperationNode.get("line"))

    # logical ! (not)
    elif operator in ["NOT"]:
        result = not literalValue
        resultNode = tree.Node("constant")
        resultNode.set("constantType", "boolean")
        resultNode.set("value", str(result).lower())
        resultNode.set("line", noperationNode.get("line"))

    # logical operators &&, || -- Currently disabled, s. bug#4856
    elif False and operator in ["AND", "OR"]:
        result = None
        otherOperand, otherPosition = getOtherOperand(noperationNode,
                                                      nliteralNode)
        if operator == "AND":
            #if otherPosition==1 and not literalValue:  # short circuit
            #    result = False
            #else:
            cmpFcn = (lambda x, y: x and y)
        elif operator == "OR":
            #if otherPosition==1 and literalValue:  # short circuit
            #    result = True
            #else:
            cmpFcn = (lambda x, y: x or y)

        if result == None:
            if otherOperand.type != "constant":
                return literalNode, False
            operands = {}
            operands[1 - otherPosition] = literalValue
            otherVal = constNodeToPyValue(otherOperand)
            operands[otherPosition] = otherVal
            result = cmpFcn(operands[0], operands[1])
            resultNode = {
                literalValue: literalNode,
                otherVal: otherOperand
            }[result]

    # hook ?: operator
    elif operator in ["HOOK"]:
        if ngParent.type == "first":  # optimize a literal condition
            if bool(literalValue):
                resultNode = treeutil.selectNode(noperationNode, "second/1",
                                                 True)
            else:
                resultNode = treeutil.selectNode(noperationNode, "third/1",
                                                 True)

    # unsupported operation
    else:
        pass

    if resultNode != None:
        #print "optimizing: operation"
        operationNode.parent.replaceChild(operationNode, resultNode)
        treeModified = True
    else:
        resultNode = literalNode
        treeModified = False

    return resultNode, treeModified
Example #25
0
 def checkRequiredBlocks(self):
     for node in treeutil.nodeIterator(self.tree, "loop"):
         block = treeutil.selectNode(node, "statement/block")
         if not block:
             self.log(node, "The statement of loops and conditions must be enclosed by a block in braces '{}'")
Example #26
0
def findVariantNodes(node):
    for callnode in treeutil.nodeIterator(node, ['call']):
        if isEnvironmentCall(callnode):
            yield treeutil.selectNode(callnode, "operand").getFirstChild()
        else:
            continue
Example #27
0
def findVariantNodes(node):
    for callnode in treeutil.nodeIterator(node, ['call']):
        if isEnvironmentCall(callnode):
            yield treeutil.selectNode(callnode, "operand").getFirstChild()
        else:
            continue
Example #28
0
def patch(node):
    global cnt
    #for _, commentNode in treeutil.nodeIteratorNonRec(node,["comment", "commentsBefore", "commentsAfter"]):
    for commentNode in treeutil.nodeIterator(
            node, ["comment", "commentsBefore", "commentsAfter"]):
        commentNode.parent.removeChild(commentNode)