当前位置: 首页>>代码示例>>Python>>正文


Python metamodel.metamodel_from_str函数代码示例

本文整理汇总了Python中textx.metamodel.metamodel_from_str函数的典型用法代码示例。如果您正苦于以下问题:Python metamodel_from_str函数的具体用法?Python metamodel_from_str怎么用?Python metamodel_from_str使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了metamodel_from_str函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_match_rule_suppress

def test_match_rule_suppress():
    """
    Test suppressing operator in match rules.
    """
    grammar = """
    FullyQualifiedID[noskipws]:
        /\s*/-
        QuotedID+['.']
        /\s*/-
    ;
    QuotedID:
        '"'?- ID '"'?-
    ;
    """

    meta = metamodel_from_str(grammar)
    model = meta.model_from_str('''
                                first."second".third."fourth"
                                ''')
    assert model == 'first.second.third.fourth'

    # Checking suppress rule reference
    grammar = """
        First: 'a' Second- Third;
        Second: 'b';
        Third: Second;
    """
    meta = metamodel_from_str(grammar)
    model = meta.model_from_str('a b b')
    # Second b should be suppressed
    assert model == 'ab'
开发者ID:c4pone,项目名称:textX,代码行数:31,代码来源:test_textx_language.py

示例2: test_repetition_single_assignment_error

def test_repetition_single_assignment_error():
    """
    Test that plain assignment in repetition end up being a list in the model.
    """

    grammar = r"""
    Rep: many_a*=A;
    A: 'A'
      ( ('b' '=' b=BOOL) |
        ('c' '=' c=C)
      )*;
    B: "b";
    C: "c";
    """
    mm = metamodel_from_str(grammar)
    model = mm.model_from_str(" A c=c b=true ")

    assert model.many_a[0].b == [True]
    assert model.many_a[0].c == ["c"]

    grammar = r"""
    Rep: many_a*=A;
    A: ( ('b' '=' b=BOOL) |
        ('c' '=' c=C)
      )*;
    B: "b";
    C: "c";
    """
    mm = metamodel_from_str(grammar)
    model = mm.model_from_str(" c=c b=true ")

    assert model.many_a[0].b == [True]
    assert model.many_a[0].c == ["c"]
开发者ID:igordejanovic,项目名称:textX,代码行数:33,代码来源:test_repetition_single_assignment_error.py

示例3: test_modifier_separator_optional

def test_modifier_separator_optional():
    model = """
    Rule:
        ("a"|"b")?[','];
    """
    with pytest.raises(TextXSyntaxError):
        # Modifiers are not possible for ? operator
        metamodel_from_str(model)
开发者ID:zeph,项目名称:textX,代码行数:8,代码来源:test_repeat_modifiers.py

示例4: test_assignment_modifier_separator_optional

def test_assignment_modifier_separator_optional():
    model = """
    Rule:
        a?=AorB[','];
    AorB:
        "a"|"b";
    """
    with pytest.raises(TextXSyntaxError):
        metamodel_from_str(model)
开发者ID:zeph,项目名称:textX,代码行数:9,代码来源:test_repeat_modifiers.py

示例5: test_assignment_modifier_separator_plain

def test_assignment_modifier_separator_plain():
    """
    Modifiers are not allowed for plain assignment.
    """
    model = """
    Rule:
        a=AorB[','];
    AorB:
        "a"|"b";
    """
    with pytest.raises(TextXSyntaxError):
        metamodel_from_str(model)
开发者ID:atveit,项目名称:textX,代码行数:12,代码来源:test_repeat_modifiers.py

示例6: test_ignore_case

def test_ignore_case():
    langdef = """
    Model: 'start' rules*='first' 'second';
    """
    meta = metamodel_from_str(langdef)

    # By default case is not ignored.
    with pytest.raises(TextXSyntaxError):
        meta.model_from_str('Start first First Second')

    meta = metamodel_from_str(langdef, ignore_case=True)
    meta.model_from_str('Start first First Second')
开发者ID:atveit,项目名称:textX,代码行数:12,代码来源:test_metamodel_params.py

示例7: test_skipws

def test_skipws():
    langdef = """
    Model: 'start' rules*='first' 'second';
    """
    meta = metamodel_from_str(langdef)

    # By default ws are skipped.
    meta.model_from_str('start first first second')

    meta = metamodel_from_str(langdef, skipws=False)
    with pytest.raises(TextXSyntaxError):
        meta.model_from_str('start first first second')

    meta.model_from_str('startfirstfirstsecond')
开发者ID:atveit,项目名称:textX,代码行数:14,代码来源:test_metamodel_params.py

示例8: test_assignment_optional

def test_assignment_optional():
    grammar = """
    Model: 'start' (attr=Rule)?;    // There should be at most one Rule
                                 // after 'start'
    Rule: Rule1|Rule2|Rule3;
    Rule1: a=INT;
    Rule2: b=STRING;
    Rule3: c=ID;
    """
    meta = metamodel_from_str(grammar)
    assert meta
    assert set([x.__name__ for x in meta]) == \
        set(['Model', 'Rule', 'Rule1', 'Rule2', 'Rule3'])\
        .union(set(BASE_TYPE_NAMES))

    model = meta.model_from_str('start')
    assert model
    model = meta.model_from_str('start 34')
    assert model
    assert model.attr
    assert model.attr.a == 34
    assert model.attr.__class__.__name__ == 'Rule1'

    # There must be at most one Rule matched after 'start'
    with pytest.raises(TextXSyntaxError):
        model = meta.model_from_str('start 34 "foo"')
    assert model
开发者ID:zeph,项目名称:textX,代码行数:27,代码来源:test_textx_language.py

示例9: main

def main(debug=False):

    bool_mm = metamodel_from_str(grammar,
                                 classes=[Bool, Or, And, Not, Operand],
                                 ignore_case=True,
                                 debug=debug)

    this_folder = dirname(__file__)
    if debug:
        metamodel_export(bool_mm, join(this_folder, 'bool_metamodel.dot'))

    input_expr = '''
        a = true;
        b = not a and true;
        a and false or not b
    '''

    model = bool_mm.model_from_str(input_expr)

    if debug:
        model_export(model, join(this_folder, 'bool_model.dot'))

    # Getting value property from the Bool instance will start evaluation.
    result = model.value

    assert model.value is True
    print("Result is", result)
开发者ID:atveit,项目名称:textX,代码行数:27,代码来源:boolean.py

示例10: test_float_int_number

def test_float_int_number():
    """
    Test that numbers are recognized correctly.
    """
    grammar = """
        Rule:
            a=NUMBER
            b=INT
            c=FLOAT
        ;
    """
    meta = metamodel_from_str(grammar)

    model = meta.model_from_str('3.4 5 .3')
    assert model.a == 3.4
    assert type(model.a) is float
    assert model.b == 5
    assert model.c == 0.3

    model = meta.model_from_str('3 5 2.0')
    assert model.a == 3

    # NUMBER type always convert to python float
    assert type(model.a) is float

    assert model.b == 5
    assert model.c == 2
    assert type(model.c) is float
开发者ID:zeph,项目名称:textX,代码行数:28,代码来源:test_textx_language.py

示例11: test_basetype

def test_basetype():
    """
    Test that basetype will match each type properly.
    """
    grammar = """
        Rule:
            a1=BASETYPE
            a2=BASETYPE
            a3=BASETYPE
            a4=BASETYPE
            b=BASETYPE
            c=BASETYPE
            d=BASETYPE
            e=BASETYPE
        ;
    """

    meta = metamodel_from_str(grammar)
    assert meta
    assert meta['Rule']._tx_type is RULE_NORMAL

    model = meta.model_from_str('False false true True 0 4.5 "string" some_id')

    assert model.a1 is False
    assert model.a2 is False
    assert model.a3 is True
    assert model.a4 is True
    assert model.b == 0.0
    assert model.c == 4.5
    assert model.d == "string"
    assert model.e == "some_id"
开发者ID:zeph,项目名称:textX,代码行数:31,代码来源:test_textx_language.py

示例12: test_all_basetypes

def test_all_basetypes():
    """
    Test that base types are matched properly.
    """
    grammar = """
        Rule:
            a=FLOAT
            b=INT
            c1=BOOL
            c2=BOOL
            d1=STRING
            d2=STRING
            e=ID
        ;
    """
    meta = metamodel_from_str(grammar)
    assert meta
    assert meta['Rule']._tx_type is RULE_NORMAL

    model = meta.model_from_str('3.4 5 true 0 "some string" '
                                '\'some other string\' some_id')
    assert model.a == 3.4
    assert model.b == 5
    assert model.c1 is True
    assert model.c2 is False
    assert model.d1 == "some string"
    assert model.d2 == "some other string"
    assert model.e == "some_id"
开发者ID:zeph,项目名称:textX,代码行数:28,代码来源:test_textx_language.py

示例13: test_object_processors

def test_object_processors():
    """
    Test that object processors are called.
    They should be called after each model object construction.
    """

    call_order = []

    def first_obj_processor(first):
        first._first_called = True
        call_order.append(1)

    def second_obj_processor(second):
        second._second_called = True
        call_order.append(2)

        # test that parent is fully initialised.
        # b should be True
        assert second.parent.b

    obj_processors = {
        'First': first_obj_processor,
        'Second': second_obj_processor,
        }

    metamodel = metamodel_from_str(grammar)
    metamodel.register_obj_processors(obj_processors)

    model_str = 'first 34 45 7 A 45 65 B true C "dfdf"'
    first = metamodel.model_from_str(model_str)

    assert hasattr(first, '_first_called')
    for s in first.seconds:
        assert hasattr(s, '_second_called')
    assert call_order == [2, 2, 2, 1]
开发者ID:atveit,项目名称:textX,代码行数:35,代码来源:test_processors.py

示例14: test_syntactic_predicate_not

def test_syntactic_predicate_not():
    """
    Test negative lookahead using `not` syntactic predicate.
    """
    grammar = """
    Expression: Let | MyID | NUMBER;
    Let:
        'let'
            expr+=Expression
        'end'
    ;

    Keyword: 'let' | 'end';
    MyID: !Keyword ID;
    """
    meta = metamodel_from_str(grammar)

    model = meta.model_from_str("""
                                let let let 34 end let foo end end end
                                """)

    assert model
    assert len(model.expr) == 1
    assert model.expr[0].expr[0].expr[0] == 34
    assert model.expr[0].expr[1].expr[0] == 'foo'
开发者ID:c4pone,项目名称:textX,代码行数:25,代码来源:test_textx_language.py

示例15: test_abstract_rule_and_object_reference

def test_abstract_rule_and_object_reference():
    grammar = """
    Model: 'start' rules*=RuleA 'ref' ref=[RuleA];
    RuleA: Rule1|Rule2;
    Rule1: RuleI|RuleE;
    Rule2: 'r2' name=ID;
    RuleI: 'rI' name=ID;
    RuleE: 'rE' name=ID;
    """
    meta = metamodel_from_str(grammar)
    assert meta
    assert set([x.__name__ for x in meta]) == \
        set(['Model', 'RuleA', 'Rule1', 'Rule2', 'RuleI', 'RuleE'])\
        .union(set(BASE_TYPE_NAMES))

    model = meta.model_from_str('start r2 rule1 rE rule2 ref rule2')
    assert model
    assert hasattr(model, 'rules')
    assert hasattr(model, 'ref')
    assert model.rules
    assert model.ref

    # Reference to first rule
    assert model.ref is model.rules[1]
    assert model.ref.__class__.__name__ == "RuleE"
开发者ID:zeph,项目名称:textX,代码行数:25,代码来源:test_textx_language.py


注:本文中的textx.metamodel.metamodel_from_str函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。