Exemplo n.º 1
0
def __createSimpleAssignment(identifier, valueNode):
    assignNode = Node(None, "assign")
    identNode = Node(None, "identifier")
    identNode.value = identifier
    assignNode.append(identNode)
    assignNode.append(valueNode)

    return assignNode
Exemplo n.º 2
0
 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"):
         node.value = tokenizer.token.value
         
     if tokenType == "number":
         value = tokenizer.token.value
         if type(value) != str:
             node.value = value
         else:
             # Check whether Python and JavaScript number created is the same
             try:
                 conv = float(value)
                 if str(conv) == value:
                     node.value = conv
                 else:
                     node.value = value
             except ValueError:
                 # Required for preventing issues with 0xF0 like values
                 node.value = value
         
     return node
Exemplo n.º 3
0
    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"):
            node.value = tokenizer.token.value

        if tokenType == "number":
            value = tokenizer.token.value
            if type(value) != str:
                node.value = value
            else:
                # Check whether Python and JavaScript number created is the same
                try:
                    conv = float(value)
                    if str(conv) == value:
                        node.value = conv
                    else:
                        node.value = value
                except ValueError:
                    # Required for preventing issues with 0xF0 like values
                    node.value = value

        return node
Exemplo n.º 4
0
    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
Exemplo n.º 5
0
    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
Exemplo n.º 6
0
def __createIdentifier(value):
    identifier = Node(None, "identifier")
    identifier.value = value
    return identifier    
Exemplo n.º 7
0
 def MEMBER_build(self, tokenizer, tokenType=None):
     node = Node(tokenizer, tokenType)
     if node.type == "identifier":
         node.value = tokenizer.token.value
     return node
Exemplo n.º 8
0
 def FUNCTION_wrapParam(self, tokenizer):
     param = Node(tokenizer)
     param.value = tokenizer.token.value
     return param
Exemplo n.º 9
0
 def CATCH_wrapException(self, tokenizer):
     node = Node(tokenizer, "exception")
     node.value = tokenizer.token.value
     return node
Exemplo n.º 10
0
    def __recurser(self, node):
        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 methods:
                params = node[1]
                table = self.__table

                # Verify param types
                if params[0].type != "string":
                    logging.warn(
                        "Expecting translation string to be type string: %s at line %s"
                        % (params[0].type, params[0].line))

                if (funcName == "trn"
                        or funcName == "trc") and params[1].type != "string":
                    logging.warn(
                        "Expecting translation string to be type string: %s at line %s"
                        % (params[1].type, params[1].line))

                # Signature tr(msg, arg1, arg2, ...)
                if 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)

        # Process children
        for child in node:
            if child != None:
                self.__recurser(child)
Exemplo n.º 11
0
    def __recurser(self, node):
        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 methods:
                params = node[1]
                table = self.__table

                # Verify param types
                if params[0].type != "string":
                    logging.warn("Expecting translation string to be type string: %s at line %s" % (params[0].type, params[0].line))
                    
                if (funcName == "trn" or funcName == "trc") and params[1].type != "string":
                    logging.warn("Expecting translation string to be type string: %s at line %s" % (params[1].type, params[1].line))


                # Signature tr(msg, arg1, arg2, ...)
                if 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)



        # Process children
        for child in node:
            if child != None:
                self.__recurser(child)
                
Exemplo n.º 12
0
 def MEMBER_build(self, tokenizer, tokenType=None):
     node = Node(tokenizer, tokenType)
     if node.type == "identifier":
         node.value = tokenizer.token.value
     return node
Exemplo n.º 13
0
 def FUNCTION_wrapParam(self, tokenizer):
     param = Node(tokenizer)
     param.value = tokenizer.token.value
     return param
Exemplo n.º 14
0
 def CATCH_wrapException(self, tokenizer):
     node = Node(tokenizer, "exception")
     node.value = tokenizer.token.value
     return node