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


Python textx.metamodel_from_str函数代码示例

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


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

示例1: test_obj_processor_sequence_match_rule

def test_obj_processor_sequence_match_rule():

    grammar = """
    First:
        i=MyFixedInt 'end'
    ;
    MyFixedInt:
    '0' '0' '04'
    ;
    """

    model = '0004 end'

    mm = metamodel_from_str(grammar)
    m = mm.model_from_str(model)
    assert type(m.i) is text

    processors = {
        'MyFixedInt': lambda x: int(x)
    }
    mm = metamodel_from_str(grammar)
    mm.register_obj_processors(processors)
    m = mm.model_from_str(model)

    assert type(m.i) is int
开发者ID:igordejanovic,项目名称:textX,代码行数:25,代码来源:test_processors.py

示例2: test_match_rule_suppress

def test_match_rule_suppress():
    """
    Test suppressing operator in match rules.
    """
    grammar = r"""
    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:igordejanovic,项目名称:textX,代码行数:31,代码来源:test_textx_language.py

示例3: 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:igordejanovic,项目名称:textX,代码行数:12,代码来源:test_metamodel_params.py

示例4: test_that_passing_a_non_unicode_raises_exception

def test_that_passing_a_non_unicode_raises_exception():

    # Test metamodel construction
    with pytest.raises(TextXError,
                       match=r'textX accepts only unicode strings.'):
        metamodel = metamodel_from_str(42)

    metamodel = metamodel_from_str('First: INT;')
    metamodel.model_from_str('42')

    # Test model constuction
    with pytest.raises(TextXError,
                       match=r'textX accepts only unicode strings.'):
        metamodel.model_from_str(42)
开发者ID:igordejanovic,项目名称:textX,代码行数:14,代码来源:test_strict_unicode.py

示例5: 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:igordejanovic,项目名称:textX,代码行数:14,代码来源:test_metamodel_params.py

示例6: 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:igordejanovic,项目名称:textX,代码行数:35,代码来源:test_processors.py

示例7: test_object_processor_replace_object

def test_object_processor_replace_object():
    """
    Test that what is returned from object processor is value used in the
    output model.
    """
    def second_obj_processor(second):
        return second.sec / 2

    def string_obj_processor(mystr):
        return "[{}]".format(mystr)

    obj_processors = {
        'Second': second_obj_processor,
        'STRING': string_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 len(first.seconds) == 3
    assert first.seconds[0] == 17

    assert first.c == '["dfdf"]'
开发者ID:igordejanovic,项目名称:textX,代码行数:26,代码来源:test_processors.py

示例8: 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
    assert type(model.a) is int

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

示例9: test_nested_match_rules

def test_nested_match_rules():
    """
    Test calling processors for nested match rules.
    """
    grammar = r"""
    Model: objects*=MyObject;
    MyObject: HowMany | MyNumber;
    HowMany: '+'+;  // We will register processor that returns a count of '+'
    MyNumber: MyFloat | INT;
    MyFloat:  /[+-]?(((\d+\.(\d*)?|\.\d+)([eE][+-]?\d+)?)|((\d+)([eE][+-]?\d+)))(?<=[\w\.])(?![\w\.])/;
    """  # noqa

    def howmany_processor(x):
        return len(x)

    mm = metamodel_from_str(grammar)
    mm.register_obj_processors({'HowMany': howmany_processor,
                                'MyFloat': lambda x: float(x)})
    model = mm.model_from_str('3.4 ++ + ++ 6')
    assert model.objects[0] == 3.4
    assert model.objects[1] == 5
    assert model.objects[2] == 6
    assert type(model.objects[2]) is int

    # Now we will add another processor for `MyObject` to test if we can change
    # the result returned from match processors lower in hierarchy.
    def myobject_processor(x):
        assert type(x) in [int, float]
        return '#{}'.format(text(x))
    mm.register_obj_processors({'HowMany': howmany_processor,
                                'MyFloat': lambda x: float(x),
                                'MyObject': myobject_processor})
    model = mm.model_from_str('3.4 ++ + ++ 6')
    assert model.objects[0] == '#3.4'
    assert model.objects[1] == '#5'
开发者ID:igordejanovic,项目名称:textX,代码行数:35,代码来源:test_processors.py

示例10: 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(ALL_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:igordejanovic,项目名称:textX,代码行数:27,代码来源:test_textx_language.py

示例11: test_assignment_zeroormore

def test_assignment_zeroormore():

    grammar = """
    Model: 'start' attr*=Rule;     // There should be zero or more Rule-s 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(ALL_TYPE_NAMES))

    model = meta.model_from_str('start 34 "foo"')
    assert model
    assert model.attr
    assert model.attr[0].a == 34
    assert model.attr[1].b == "foo"
    assert model.attr[0].__class__.__name__ == 'Rule1'
    assert model.attr[1].__class__.__name__ == 'Rule2'

    model = meta.model_from_str('start')
    assert model
开发者ID:igordejanovic,项目名称:textX,代码行数:26,代码来源:test_textx_language.py

示例12: test_issue78_quickcheck_no_obj_processors_called_for_references

def test_issue78_quickcheck_no_obj_processors_called_for_references():
    """
    This test represents just a plausibility check.
    """
    grammarA = """
    Model: a+=A | b+=B;
    A:'A' name=ID;
    B:'B' name=ID '->' a=[A];
    """

    mm = textx.metamodel_from_str(grammarA)

    import textx.scoping.providers as scoping_providers
    global_repo_provider = scoping_providers.PlainNameGlobalRepo()
    mm.register_scope_providers({"*.*": global_repo_provider})

    test_list = []
    mm.register_obj_processors({
        'A': lambda o: test_list.append(o.name),
    })

    # no references to A: --> obj proc called
    m1 = mm.model_from_str('''
    A a1 A a2 A a3
    ''')
    assert ['a1', 'a2', 'a3'] == test_list

    # only references to A: --> obj proc not called
    global_repo_provider.add_model(m1)
    mm.model_from_str('''
    B b1 -> a1 B b2 -> a2 B b3 -> a3
    ''')
    assert ['a1', 'a2', 'a3'] == test_list  # unchanged...
开发者ID:igordejanovic,项目名称:textX,代码行数:33,代码来源:test_issue78.py

示例13: main

def main(debug=False):

    calc_mm = metamodel_from_str(grammar,
                                 classes=[Calc, Expression, Term, Factor,
                                          Operand],
                                 debug=debug)

    this_folder = dirname(__file__)
    if debug:
        metamodel_export(calc_mm, join(this_folder, 'calc_metamodel.dot'))

    input_expr = '''
        a = 10;
        b = 2 * a + 17;
        -(4-1)*a+(2+4.67)+b*5.89/(.2+7)
    '''

    model = calc_mm.model_from_str(input_expr)

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

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

    assert (model.value - 6.93805555) < 0.0001
    print("Result is", result)
开发者ID:igordejanovic,项目名称:textX,代码行数:27,代码来源:calc.py

示例14: test_sequence_ordered_choice

def test_sequence_ordered_choice():
    """
    Test ordered choice of sequences.
    """

    grammar = """
    Model:
        ('first' a=INT b?='a_is_here' |
        'second' c=INT d?='c_is_here' |
        e=RuleA)
        'END'
    ;
    RuleA: 'rule' name=ID;
    """
    meta = metamodel_from_str(grammar, debug=True)
    assert meta
    assert set([x.__name__ for x in meta]) == \
        set(['Model', 'RuleA'])\
        .union(set(ALL_TYPE_NAMES))

    model = meta.model_from_str('first 23 a_is_here END')
    assert model.a == 23
    assert model.c == 0
    assert model.b is True
    assert model.d is False
    model = meta.model_from_str('second 32 END')
    assert model.a == 0
    assert model.c == 32
    assert model.b is False
    assert model.d is False
    model = meta.model_from_str('rule A END')
    assert model.a == 0
    assert model.c == 0
    assert model.b is False
    assert model.d is False
开发者ID:igordejanovic,项目名称:textX,代码行数:35,代码来源:test_textx_language.py

示例15: 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:igordejanovic,项目名称:textX,代码行数:25,代码来源:test_textx_language.py


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