-
Notifications
You must be signed in to change notification settings - Fork 0
/
rrel.t.cpp
526 lines (451 loc) · 18.2 KB
/
rrel.t.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
#include "catch.hpp"
#include <iostream>
#include <sstream>
#include "textx/lang.h"
#include "textx/arpeggio.h"
#include "textx/rrel.h"
#include "textx/metamodel.h"
TEST_CASE("adapted_from_python_textx_tests_test_rrel_basic_parser1", "[textx/rrel]")
{
textx::lang::TextxGrammar parser;
parser.add_rule("rrel_expression_standalone",
textx::arpeggio::sequence({
parser.ref("rrel_expression"),
textx::arpeggio::end_of_file()
}));
parser.set_main_rule("rrel_expression_standalone");
std::optional<textx::arpeggio::Match> m;
m = parser.parse_or_throw("^pkg*.cls");
m = parser.parse_or_throw("obj.ref.~extension *.methods");
m = parser.parse_or_throw("instance.(type.vals)*");
m = parser.parse_or_throw("+mp:a.b.c");
CHECK_THROWS(parser.parse_or_throw("+U:a.b.c")); // U not allowed
CHECK_THROWS(parser.parse_or_throw("a,b,c,")); // "," at the end not allowed
CHECK_THROWS(parser.parse_or_throw("")); // empty seq. not allowed
}
TEST_CASE("adapted_from_python_textx_tests_test_rrel_basic_parser2", "[textx/rrel]")
{
std::unique_ptr<textx::rrel::RRELBase> r;
r = textx::rrel::create_RREL_expression("^pkg*.cls");
CHECK(r->str() == "(..)*.(pkg)*.cls");
r = textx::rrel::create_RREL_expression("obj.ref.~extension *.methods");
CHECK(r->str() == "obj.ref.(~extension)*.methods");
r = textx::rrel::create_RREL_expression("type.vals");
CHECK(r->str() == "type.vals");
r = textx::rrel::create_RREL_expression("(type.vals)");
CHECK(r->str() == "(type.vals)");
r = textx::rrel::create_RREL_expression("(type.vals)*");
CHECK(r->str() == "(type.vals)*");
r = textx::rrel::create_RREL_expression("instance . ( type.vals ) *");
CHECK(r->str() == "instance.(type.vals)*");
r = textx::rrel::create_RREL_expression("a,b,c");
CHECK(r->str() == "a,b,c");
r = textx::rrel::create_RREL_expression("a.b.c");
CHECK(r->str() == "a.b.c");
r = textx::rrel::create_RREL_expression("parent(NAME)");
CHECK(r->str() == "parent(NAME)");
r = textx::rrel::create_RREL_expression("+p:a.b.c");
CHECK(r->str() == "+p:a.b.c");
r = textx::rrel::create_RREL_expression("+mp:a.b.c");
CHECK(r->str() == "+mp:a.b.c");
r = textx::rrel::create_RREL_expression("a.'b'~b.'c'~x");
CHECK(r->str() == "a.'b'~b.'c'~x");
}
TEST_CASE("simple_rrel1", "[textx/rrel]")
{
auto mm = textx::metamodel_from_str(R"#(
Model: packages+=Package;
Package: "package" name=ID "{" packages*=Package objects*=Object "}";
Object: "object" name=ID;
)#");
auto m = mm->model_from_str(R"(
package a {
package b {
object c
}
}
)");
auto res = textx::rrel::find_object_with_path(m->val().obj(), "a.b.c", "packages.packages.objects");
CHECK( std::get<0>(res).obj == m->fqn("a.b.c") );
res = textx::rrel::find_object_with_path(m->val().obj(), "a.b.c.d", "packages.packages.objects");
CHECK( std::get<0>(res).obj == nullptr );
res = textx::rrel::find_object_with_path(m->val().obj(), "a.b", "packages.packages.objects");
CHECK( std::get<0>(res).obj == nullptr );
// same with *
res = textx::rrel::find_object_with_path(m->val().obj(), "a.b.c", "packages*.objects");
CHECK( std::get<0>(res).obj == m->fqn("a.b.c") );
res = textx::rrel::find_object_with_path(m->val().obj(), "a.b.c.d", "packages*.objects");
CHECK( std::get<0>(res).obj == nullptr );
res = textx::rrel::find_object_with_path(m->val().obj(), "a.b", "packages*.objects");
CHECK( std::get<0>(res).obj == nullptr );
}
namespace {
const char* metamodel_str = R"#(
Model:
packages*=Package
;
Package:
'package' name=ID '{'
packages*=Package
classes*=Class
'}'
;
Class:
'class' name=ID '{'
attributes*=Attribute
'}'
;
Attribute:
'attr' name=ID ';'
;
Comment: /#.*?$/;
FQN: ID('.'ID)*;
)#";
const char* modeltext=modeltext = R"#(
package P1 {
class Part1 {
}
}
package P2 {
package Inner {
class Inner {
attr inner;
}
}
class Part2 {
attr rec;
}
class C2 {
attr p1;
attr p2a;
attr p2b;
}
class rec {
attr p1;
}
}
)#";
}
TEST_CASE("adapted_from_python_test_rrel_basic_lookup", "[textx/rrel]")
{
auto mm = textx::metamodel_from_str(metamodel_str);
auto m = mm->model_from_str(modeltext);
auto P2 = textx::rrel::find(m->val().obj(), "P2", "packages");
CHECK((*P2)["name"].str() == "P2");
auto Part2 = textx::rrel::find(m->val().obj(), "P2.Part2", "packages.classes");
CHECK((*Part2)["name"].str() == "Part2");
auto rec = textx::rrel::find(m->val().obj(), "P2.Part2.rec", "packages.classes.attributes");
CHECK((*rec)["name"].str() == "rec");
CHECK((*rec).parent() == Part2);
auto other_P2 = textx::rrel::find(m->val().obj(), "P2", "(packages)");
CHECK( other_P2 == P2 );
CHECK( m->val().obj()->tx_model() == m );
auto other2_P2 = textx::rrel::find(m->val().obj(), "P2", "packages*");
CHECK( other2_P2 == P2 );
auto other_Part2 = textx::rrel::find(m->val().obj(), "P2.Part2", "packages*.classes");
CHECK( other_Part2 == Part2 );
auto other_rec = textx::rrel::find(m->val().obj(), "P2.Part2.rec", "packages*.classes.attributes");
CHECK( other_rec == rec );
CHECK( other_rec->parent() == Part2 );
auto Part2_tst = textx::rrel::find(rec, "", "..");
CHECK( Part2_tst == Part2 );
auto P2_from_inner_node = textx::rrel::find(rec, "P2", "(packages)");
CHECK( P2_from_inner_node == P2 );
auto P2_tst = textx::rrel::find(rec, "", "parent(Package)");
CHECK( P2_tst == P2 );
P2_tst = textx::rrel::find(rec, "", "...");
CHECK( P2_tst == P2 );
P2_tst = textx::rrel::find(rec, "", ".(..).(..)");
CHECK( P2_tst == P2 );
P2_tst = textx::rrel::find(rec, "", "(..).(..)");
CHECK( P2_tst == P2 );
P2_tst = textx::rrel::find(rec, "", "...(.).(.)");
CHECK( P2_tst == P2 );
P2_tst = textx::rrel::find(rec, "", "..(.).(..)");
CHECK( P2_tst == P2 ); // p!
P2_tst = textx::rrel::find(rec, "", "..((.)*)*.(..)");
CHECK( P2_tst == P2 );
auto none = textx::rrel::find(m->val().obj(), "", "..");
CHECK( none == nullptr );
auto mobj = textx::rrel::find(m->val().obj(), "", "."); // '.' references the current element
CHECK(mobj == m->val().obj());
auto inner = textx::rrel::find(m->val().obj(), "inner", "~packages.~packages.~classes.attributes");
CHECK((*inner)["name"].str() == "inner");
auto package_Inner = textx::rrel::find(inner, "Inner", "parent(OBJECT)*.packages");
REQUIRE( package_Inner != nullptr );
CHECK( package_Inner->is_instance("Package") );
CHECK( !package_Inner->is_instance("Class") );
CHECK( nullptr == textx::rrel::find(inner, "P2", "parent(Class)*.packages") );
// expensive version of a "Plain Name" scope provider:
auto other_inner = textx::rrel::find(m->val().obj(), "inner", "~packages*.~classes.attributes");
CHECK( inner == other_inner );
auto rec2 = textx::rrel::find(m->val().obj(), "P2.Part2.rec", "other1,other2,packages*.classes.attributes");
CHECK( rec2 == rec );
rec2 = textx::rrel::find(m->val().obj(), "P2.Part2.rec", "other1,packages*.classes.attributes,other2");
CHECK( rec2 == rec );
rec2 = textx::rrel::find(m->val().obj(), "P2::Part2::rec", "other1,packages*.classes.attributes,other2","", "::");
CHECK( rec2 == rec );
rec2 = textx::rrel::find(m->val().obj(), "P2.Part2.rec", "other1,other2,other3");
CHECK( rec2 == nullptr );
rec2 = textx::rrel::find(m->val().obj(), "P2.Part2.rec", "(packages,classes,attributes)*");
CHECK( rec2 == rec );
rec2 = textx::rrel::find(m->val().obj(), "P2.Part2.rec", "(packages,(classes,attributes)*)*.attributes");
CHECK( rec2 == rec );
rec2 = textx::rrel::find(m->val().obj(), "rec", "(~packages,~classes,attributes,classes)*");
CHECK( rec2 == rec );
rec2 = textx::rrel::find(m->val().obj(), "rec",
"(~packages,~classes,attributes,classes)*", "OBJECT");
CHECK( rec2 == rec );
rec2 = textx::rrel::find(m->val().obj(), "rec",
"(~packages,~classes,attributes,classes)*", "Attribute");
CHECK( rec2 == rec );
rec2 = textx::rrel::find(m->val().obj(), "rec",
"(~packages,~classes,attributes,classes)*", "Package");
CHECK( rec2 == nullptr );
rec2 = textx::rrel::find(m->val().obj(), "rec",
"(~packages,classes,attributes,~classes)*", "Class");
CHECK((*rec2)["name"].str() == "rec");
CHECK( rec2 != rec );
rec2 = textx::rrel::find(m->val().obj(), "rec",
"(~packages,~classes,attributes,classes)*", "Class");
CHECK((*rec2)["name"].str() == "rec");
CHECK( rec2 != rec );
auto t = textx::rrel::find(m->val().obj(), "", ".");
CHECK( t == m->val().obj() );
t = textx::rrel::find(m->val().obj(), "", "(.)");
CHECK( t == m->val().obj() );
t = textx::rrel::find(m->val().obj(), "", "(.)*");
CHECK( t == m->val().obj() );
t = textx::rrel::find(m->val().obj(), "", "(.)*.no_existent"); // inifite recursion stopper
CHECK( t == nullptr );
rec2 = textx::rrel::find(m->val().obj(), "rec",
"(.)*.(~packages,~classes,attributes,classes)*", "Class");
CHECK((*rec2)["name"].str() == "rec");
CHECK( rec2 != rec ); // it is the class...
// Here, we test the start_from_root/start_locally logic:
auto P2t = textx::rrel::find(rec, "P2", "(.)*.packages");
CHECK( P2t == nullptr );
P2t = textx::rrel::find(rec, "P2", "(.,not_existent_but_root)*.packages");
CHECK( P2t == P2 );
auto rect = textx::rrel::find(rec, "rec", "(~packages)*.(..).attributes");
CHECK( rect == nullptr );
rect = textx::rrel::find(rec, "rec", "(.,~packages)*.(..).attributes");
CHECK( rect == rec );
}
// This is a basic extra test to demonstrate `()*`
// in RREL expressions.
TEST_CASE("adapted_from_python_test_rrel_repetitions", "[textx/rrel]")
{
auto my_metamodel = textx::metamodel_from_str(R"#(
Model: entries*=Entry;
Entry: name=ID (':' ref=[Entry])?;
Comment: /\/\/.*?$/;
)#");
auto my_model = my_metamodel->model_from_str(R"#(
a: b
c
b: a
)#");
auto a = textx::rrel::find(my_model->val().obj(), "a", "entries.ref*");
REQUIRE(a!=nullptr);
CHECK( (*a)["name"].str() == "a" );
auto b = textx::rrel::find(my_model->val().obj(), "b", "entries.ref*");
REQUIRE(b!=nullptr);
CHECK( (*b)["name"].str() == "b" );
auto c = textx::rrel::find(my_model->val().obj(), "c", "entries.ref*");
REQUIRE(c!=nullptr);
CHECK( (*c)["name"].str() == "c" );
auto a2 = textx::rrel::find(my_model->val().obj(), "a.b.a", "entries.ref*");
CHECK( a2 == a );
auto b2 = textx::rrel::find(my_model->val().obj(), "b.a.b", "entries.ref*");
CHECK( b2 == b );
{
auto [res, objpath] = std::get<0>(textx::rrel::find_object_with_path(my_model->val().obj(), "b.a.b", "entries.ref*"));
CHECK( res == b );
REQUIRE( objpath.size() == 3 );
CHECK( objpath[objpath.size()-1].lock() == res );
CHECK( textx::rrel::build_fqn(objpath) == "b.a.b" );
a2 = textx::rrel::find(my_model->val().obj(), "b.a.b.a", "entries.ref*");
CHECK( a2 == a );
}
{
auto [res, objpath] = std::get<0>(textx::rrel::find_object_with_path(my_model->val().obj(), "b.a.b.a", "entries.ref*"));
CHECK( res == a );
CHECK( objpath.size() == 4 );
CHECK( objpath[objpath.size()-1].lock() == res );
CHECK( textx::rrel::build_fqn(objpath,".") == "b.a.b.a" );
CHECK( textx::rrel::build_fqn(objpath,"::") == "b::a::b::a" );
a2 = textx::rrel::find(my_model->val().obj(), "b.a.b.a.b.a.b.a.b.a", "entries.ref*");
CHECK( a2 == a );
b2 = textx::rrel::find(my_model->val().obj(), "b.a.b.a.b.a.b.a.b.a.b", "entries.ref*");
CHECK( b2 == my_model->fqn("b") );
}
}
TEST_CASE("rrel_scope_provider", "[textx/rrel]")
{
auto mm = textx::metamodel_from_str(R"#(
Model: a+=A r+=R;
A: 'A' name=ID '{' a*=A '}';
R: 'R' a+=[A|FQN][','];
FQN: ID ('.' ID)*;
)#");
mm->set_resolver("R.a", std::make_unique<textx::rrel::RRELScopeProvider>("+p:a*"));
auto m = mm->model_from_str(R"#(
A a1 {
A aa1 {
A aaa1 {}
A aab1 {}
}
}
A a2 {
A aa2 {}
}
A R {
A r2 {}
}
R a1.aa1.aaa1, a1.aa1.aab1, R, R.r2
R R
R a2.aa2
)#");
CHECK( m->val()["r"].size() == 3 );
CHECK( m->val()["r"][0]["a"][0].obj() == m->fqn("a1.aa1.aaa1") );
CHECK( m->val()["r"][0]["a"][1].obj() == m->fqn("a1.aa1.aab1") );
CHECK( m->val()["r"][0]["a"][2].obj() == m->fqn("R") );
CHECK( m->val()["r"][0]["a"][3].obj() == m->fqn("R.r2") );
CHECK( m->val()["r"][1]["a"][0].obj() == m->fqn("R") );
CHECK( m->val()["r"][2]["a"][0].obj() == m->fqn("a2.aa2") );
}
TEST_CASE("from_python_tests_components1", "[textx/rrel]")
{
auto p_grammar = std::filesystem::path(__FILE__).parent_path().append("rrel/components/ComponentsRrel.tx");
auto mm = textx::metamodel_from_file(p_grammar);
for(auto fn : {
"example.components",
"example_A.components",
"example_B.components",
"example_inherit1.components",
"example_inherit2.components",
"example_inherit3.components"
}) {
auto m = mm->model_from_file(std::filesystem::path(__FILE__).parent_path().append("rrel/components").append(fn));
CHECK(m!=nullptr);
}
{
auto m = mm->model_from_file(std::filesystem::path(__FILE__).parent_path().append("rrel/components").append("example_inherit1.components"));
CHECK(m!=nullptr);
auto usage = m->fqn("usage");
auto connect3 = (*usage)["connections"][2].obj();
CHECK( (*connect3)["from_port"].obj() == m->fqn("base.Start.output1"));
}
for(auto fn : {
"example_err1.components",
"example_err2.components"
}) {
CHECK_THROWS( (void)mm->model_from_file(std::filesystem::path(__FILE__).parent_path().append("rrel/components").append(fn)) );
}
}
TEST_CASE("test_rrel_multifile", "[textx/rrel]")
{
auto p_grammar = std::filesystem::path(__FILE__).parent_path().append("rrel/example0/Grammar.tx");
auto mm = textx::metamodel_from_file(p_grammar);
auto m = mm->model_from_file(std::filesystem::path(__FILE__).parent_path().append("rrel/example0/main.model"));
CHECK(m!=nullptr);
}
TEST_CASE("test_rrel_multifile_nav_special_case", "[textx/rrel]")
{
//TODO check this in real/python textx version
auto p_grammar = std::filesystem::path(__FILE__).parent_path().append("rrel/example0/Grammar.tx");
auto mm = textx::metamodel_from_file(p_grammar);
auto m0 = mm->model_from_file(std::filesystem::path(__FILE__).parent_path().append("rrel/example0/navigation0.model"));
CHECK(m0!=nullptr);
auto m1 = mm->model_from_file(std::filesystem::path(__FILE__).parent_path().append("rrel/example0/navigation1.model"));
CHECK(m1!=nullptr);
REQUIRE_THROWS_WITH( (void)mm->model_from_file(std::filesystem::path(__FILE__).parent_path().append("rrel/example0/navigation1_err.model")),
Catch::Matchers::Contains( "'a2' not found" )
);
}
TEST_CASE("from_python_tests_test_split_str_multifile", "[textx/rrel]")
{
auto p_grammar = std::filesystem::path(__FILE__).parent_path().append("rrel/example1/Grammar.tx");
auto mm = textx::metamodel_from_file(p_grammar);
CHECK((*mm)["FQN"].tx_params().size()==1);
REQUIRE((*mm)["FQN"].tx_params().count("split")==1);
CHECK((*mm)["FQN"].tx_params("split") == "::");
auto m = mm->model_from_file(std::filesystem::path(__FILE__).parent_path().append("rrel/example1/main.model"));
CHECK(m!=nullptr);
}
TEST_CASE("rrel_regression1", "[textx/rrel]")
{
auto mm = textx::metamodel_from_str(R"#(
Model:
cls*=Cls
obj*=Obj
call*=Call
;
Cls: "class" name=ID (
( "extends" extends+=[Cls][','])?
"{"
methods*=Method
"}"
)?;
Method: "method" name=ID;
Obj: "obj" name=ID ":" ref=[Cls];
Call: "call" name=ID ":" obj=[Obj] "." method=[Method|ID|.~obj.~ref.~extends*.methods];
Comment: /\/\/.*?$/;
)#");
auto m = mm->model_from_str(R"#(
class A extends B,C {
}
class B extends D {
method b
}
class C extends D {
method c
}
class D {
method d
}
obj a:A
call callb: a.b
call callc: a.c
call calld: a.d
)#");
CHECK(m!=nullptr);
}
TEST_CASE("test_rrel_navigation_with_fixed_str", "[textx/rrel]")
{
auto mm = textx::metamodel_from_str(R"#(
Model: types_collection*=TypesCollection ('activeTypes' '=' active_types=[TypesCollection])? usings*=Using;
Using: 'using' name=ID "=" type=[Type|ID|+m:
~active_types.types, // "regular lookup"
'builtin'~types_collection.types // "default lookup" - name "builtin" hard coded in grammar
];
TypesCollection: 'types' name=ID "{" types*=Type "}";
Type: 'type' name=ID;
Comment: /#.*?$/;
)#");
auto builtin = mm->model_from_str(R"#(
types builtin {
type i32
type i64
type f32
type f64
}
)#");
mm->add_builtin_model(builtin);
auto m = mm->model_from_str(R"#(
types MyTypes {
type Int
type Double
}
types OtherTypes {
type Foo
type Bar
}
activeTypes=MyTypes
using myDouble = Double
using myInt = Int # found via "regular lookup"
using myi32 = i32 # found via "default lookup"
# using myFoo = Foo # --> not found
)#");
}