本文整理汇总了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'
示例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"]
示例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)
示例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)
示例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)
示例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')
示例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')
示例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
示例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)
示例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
示例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"
示例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"
示例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]
示例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'
示例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"