コード例 #1
0
ファイル: plyInmantaParser.py プロジェクト: wdesmedt/inmanta
def create_string_format(format_string, variables, location):
    """
        Create a string interpolation statement
    """
    _vars = []
    for var_str in variables:
        var_parts = var_str[1].split(".")
        ref = Reference(var_parts[0])
        ref.namespace = namespace
        ref.location = location

        if len(var_parts) > 1:
            for attr in var_parts[1:]:
                ref = AttributeReference(ref, attr)
                ref.location = location
                ref.namespace = namespace
            _vars.append((ref, var_str[0]))
        else:
            _vars.append((ref, var_str[0]))

    return StringFormat(format_string, _vars)
コード例 #2
0
def create_string_format(
        format_string: LocatableString,
        variables: List[Tuple[str, LocatableString]]) -> StringFormat:
    """
    Create a string interpolation statement. This function assumes that the variables of a match are on the same line.

    :param format_string: the LocatableString as it was received by get_string_ast_node()
    :param variables: A list of tuples where each tuple is a combination of a string and LocatableString
                        The string is the match containing the {{}} (ex: {{a.b}}) and the LocatableString is composed of
                        just the variables and the range for those variables.
                        (ex. LocatableString("a.b", range(a.b), lexpos, namespace))
    """
    assert namespace
    _vars = []
    for match, var in variables:
        var_name: str = str(var)
        var_parts: List[str] = var_name.split(".")
        start_char = var.location.start_char
        end_char = start_char + len(var_parts[0])
        range: Range = Range(var.location.file, var.location.lnr, start_char,
                             var.location.lnr, end_char)
        ref_locatable_string = LocatableString(var_parts[0], range, var.lexpos,
                                               var.namespace)
        ref = Reference(ref_locatable_string)
        ref.namespace = namespace
        if len(var_parts) > 1:
            attribute_offsets: Iterator[int] = accumulate(
                var_parts[1:],
                lambda acc, part: acc + len(part) + 1,
                initial=end_char + 1)
            for attr, char_offset in zip(var_parts[1:], attribute_offsets):
                range_attr: Range = Range(var.location.file, var.location.lnr,
                                          char_offset, var.location.lnr,
                                          char_offset + len(attr))
                attr_locatable_string: LocatableString = LocatableString(
                    attr, range_attr, var.lexpos, var.namespace)
                ref = AttributeReference(ref, attr_locatable_string)
                ref.location = range_attr
                ref.namespace = namespace
            _vars.append((ref, match))
        else:
            _vars.append((ref, match))
    return StringFormat(str(format_string), _vars)