Esempio n. 1
0
def get_property_constexpr(a, pname):
    t = get_property_type(a, pname)
    v = get_property(a, pname)
    if t is str:
        return '"{}"'.format(v)
    else:
        return "{}".format(v)
Esempio n. 2
0
def check_Property(p):
    mm = get_metamodel(p)
    if textx_isinstance(p.parent, mm["VariantMapping"]):
        textx_assert(
            is_applicable(p),
            p,
            f"{p.parent.parent.name}.{p.definition.name} not "
            f"applicable for variant mapping {p.parent.type.name}",
        )
    else:
        textx_assert(is_applicable(p), p,
                     f"{p.parent.name}.{p.definition.name} not applicable")

    prop_value = get_property(p.parent, p.definition.name)  # throws on error

    if p.definition.name == "defaultStringValue":
        textx_assert(
            not has_property(p.parent, "defaultValue"),
            p.parent,
            "only one default is allowed",
        )
        if textx_isinstance(p.parent, mm["ScalarAttribute"]):
            textx_assert(
                len(prop_value) == 1, p,
                "only exactly one char is allowed as default")

    # if p.definition.name == "fixedSizeInBytes":
    #    struct_type = p.parent.type
    #    assert textx_isinstance(struct_type, mm['Struct'])
    #    value = get_property(p.parent, "fixedSizeInBytes")
    #    textx_assert(
    #        value>=struct_type."getmaxsize MISSING TODO",
    #        p.parent.parent,
    #        f"max size of {struct_type.name} is smaler than {value}",
    #    )

    if p.definition.name in [
            "fixpointLsbValue",
            "fixpointMsbValue",
            "fixpointOffsetValue",
    ]:
        a = p.parent  # must be an attribute (restriction of fixpoint pros)
        textx_assert(
            not (has_property(a, "fixpointLsbValue")
                 and has_property(a, "fixpointMsbValue")),
            a,
            "specify either MSB or LSB (and not both at the same time)",
        )
        textx_assert(
            has_property(a, "fixpointLsbValue")
            or has_property(a, "fixpointMsbValue"),
            p,
            "specify either MSB or LSB (you need at least one of them for fixpoint values)",
        )
        textx_assert(
            a.type.internaltype in ["UINT", "INT"],
            a,
            "fixpoint meta information only possible with integral values",
        )
Esempio n. 3
0
def get_property_constexpr(a, pname):
    t = get_property_type(a, pname)
    v = get_property(a, pname)
    if t is str:
        return '"{}"'.format(v)
    else:
        if isinstance(v, bool):
            v = int(v)
        return f"{get_cpp_return_type(t)}{{ {v} }}"  # TODO, better: return enum-value directly (same for python code)
Esempio n. 4
0
 def get_all_properties_of_struct(s):
     lst = []
     for a in s.attributes:
         lst = lst + a.properties
         if textx_isinstance(a, mm["ScalarAttribute"]) and textx_isinstance(
                 a.type, mm["Struct"]):
             do_break = get_property(a, "is_payload")  # None or bool
             if do_break is None or not do_break:
                 lst = lst + get_all_properties_of_struct(a.type)
     return lst
Esempio n. 5
0
def test_props_load_and_import():
    mm = metamodel_for_language("item")
    assert mm is not None
    model = mm.model_from_file(
        os.path.join(os.path.abspath(os.path.dirname(__file__)), "model",
                     "props_example.item"))
    assert model is not None

    items = get_children_of_type("Struct", model)
    assert len(items) == 1
    i = items[0]
    assert len(i.attributes[0].properties) == 2

    assert has_property(i.attributes[0], "myprop1")
    assert has_property(i.attributes[0], "myprop2")
    assert get_property(i.attributes[0], "myprop1") is True
    assert get_property(i.attributes[0], "myprop2") == "Hello"
    assert not has_property(i.attributes[0], "minValue")
    assert not has_property(i.attributes[0], "maxValue")
    with pytest.raises(TextXSemanticError, match=r".*max.*"):
        has_property(i.attributes[0], "max")
Esempio n. 6
0
def test_property1():
    text = r"""
    package example

    struct Point {
      scalar x : built_in.float (.minValue=0.1, .defaultValue=1, .maxValue=1e5)
      scalar y : built_in.float (.defaultValue=0x0aB, .description="Hello")
    }
    struct Polygon {
      scalar n : built_in.uint32 (.defaultValue=16, .maxValue=1000)
      array points : Point[n]
    }
    """
    mm = metamodel_for_language("item")
    assert mm is not None
    model = mm.model_from_str(text)
    assert model is not None
    items = get_children_of_type("Struct", model)
    assert len(items) == 2

    Point = items[0]
    assert Point.name == "Point"
    assert len(Point.attributes[0].properties) == 3
    assert len(Point.attributes[1].properties) == 2
    assert Point.attributes[1].properties[0].definition.name == "defaultValue"
    assert Point.attributes[1].properties[0].numberValue.x.compute_formula(
    ) == 0xAB
    assert Point.attributes[1].properties[1].textValue.x == "Hello"

    assert get_property(Point.attributes[1], "defaultValue") == 0xAB
    assert get_property(Point.attributes[1], "description") == "Hello"

    assert has_property(Point.attributes[1], "defaultValue")
    assert has_property(Point.attributes[1], "description")
    assert not has_property(Point.attributes[1], "minValue")
    assert not has_property(Point.attributes[1], "maxValue")

    pdefs = get_all_possible_properties(
        Point, filter_applicable_to_model_object=False)
    assert len(pdefs) >= 6
Esempio n. 7
0
    def compute_formula(self, **kwargs):
        use_max_for_attributes = kwargs.get("use_max_for_attributes", False)
        if self.ref:
            from textx import textx_isinstance, get_metamodel

            if textx_isinstance(self.ref.ref, get_metamodel(self)["Attribute"]):
                if use_max_for_attributes:
                    from item_lang.properties import has_property, get_property

                    assert self.ref.ref.is_scalar()
                    assert has_property(self.ref.ref, "maxValue")
                    return get_property(self.ref.ref, "maxValue")
                else:
                    raise Exception("no constexpr")
            else:
                return self.ref.ref.value.compute_formula(**kwargs)
        elif self.sum:
            return self.sum.compute_formula(**kwargs)
        else:
            return self.value