Esempio n. 1
0
def test_slots_rt():
    ns = Namespace("root", None)
    rs = Resolver(ns)
    e = Entity("xx", ns)
    qs = QueueScheduler(None, [], [], {}, set())
    r = RelationAttribute(e, None, "xx", Location("", 1))
    i = Instance(e, rs, qs)
    sa = SetAttribute(Reference("a"), "a", Literal("a"))

    assert_slotted(ResultVariable())
    assert_slotted(AttributeVariable(None, None))
    assert_slotted(Promise(None, None))
    assert_slotted(ListVariable(r, i, qs))
    assert_slotted(OptionVariable(r, i, qs))

    assert_slotted(qs)
    assert_slotted(DelegateQueueScheduler(qs, None))

    assert_slotted(Waiter(qs))

    assert_slotted(
        ExecutionUnit(qs, r, ResultVariable(), {}, Literal(""), None))
    assert_slotted(HangUnit(qs, r, {}, None, Resumer()))
    assert_slotted(RawUnit(qs, r, {}, Resumer()))

    assert_slotted(FunctionUnit(qs, rs, ResultVariable(), {}, None))

    assert_slotted(i)
    assert_slotted(GradualSetAttributeHelper(sa, i, "A", ResultVariable()))
    assert_slotted(
        SetAttributeHelper(qs, rs, ResultVariable(), {}, Literal("A"), sa, i,
                           "A"))
Esempio n. 2
0
def t_REGEX(t: lex.LexToken) -> lex.LexToken:  # noqa: N802
    r"/([^/\\]|\\.)+/"
    value = Reference("self")  # anonymous value
    try:
        expr = Regex(value, t.value[1:-1])
        t.value = expr
        return t
    except RegexError as error:
        end = t.lexer.lexpos - t.lexer.linestart + 1
        (s, e) = t.lexer.lexmatch.span()
        start = end - (e - s)

        r: Range = Range(t.lexer.inmfile, t.lexer.lineno, start,
                         t.lexer.lineno, end)
        raise ParserException(r, t.value,
                              "Regex error in %s: '%s'" % (t.value, error))
Esempio n. 3
0
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)
Esempio n. 4
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)
Esempio n. 5
0
def p_var_ref_2(p: YaccProduction) -> None:
    "var_ref : ns_ref %prec NS_REF"
    p[0] = Reference(p[1])
    attach_from_string(p, 1)
Esempio n. 6
0
 def __init__(self, attr: Reference, name: str) -> None:
     super(IsDefined, self).__init__([attr])
     self.attr = attr.root_in_self()
     self.name = name
Esempio n. 7
0
def t_REGEX(t):  # noqa: N802
    r'/[^/]*/'
    value = Reference("self")  # anonymous value
    expr = Regex(value, t.value[1:-1])
    t.value = expr
    return t
Esempio n. 8
0
def p_var_ref_2(p):
    "var_ref : ns_ref"
    p[0] = Reference(str(p[1]))
    attach_from_string(p, 1)
Esempio n. 9
0
def p_condition_is_defined_short(p):
    """condition : ID IS DEFINED"""
    p[0] = IsDefined(Reference('self'), p[1])
    attach_lnr(p)
Esempio n. 10
0
def p_local_var(p: YaccProduction) -> None:
    "local_var : ns_ref"
    p[0] = Reference(p[1])
    attach_from_string(p, 1)