-
Notifications
You must be signed in to change notification settings - Fork 0
/
scoping.t.cpp
256 lines (240 loc) · 8.19 KB
/
scoping.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
#include "catch.hpp"
#include <iostream>
#include <sstream>
#include "textx/metamodel.h"
#include "textx/scoping.h"
TEST_CASE("model_ref1", "[textx/scoping]")
{
{
auto grammar1 = R"#(
Model: items+=Item 'ref' ref=[Item];
Item: 'item' name=ID;
)#";
{
auto mm = textx::metamodel_from_str(grammar1);
auto m = mm->model_from_str("item A item B item C ref B");
CHECK( m->val()["items"].size() == 3 );
CHECK( m->val()["items"][0]["name"].str() == "A" );
CHECK( m->val()["items"][1]["name"].str() == "B" );
CHECK( m->val()["items"][2]["name"].str() == "C" );
CHECK( m->val()["ref"].is_ref() );
CHECK( m->val()["ref"].ref().name == "B" ); // the reference string (the identifier to find the obj)
CHECK( m->val()["ref"]["name"].str() == "B" ); // the name of the (found) referenced element
CHECK( m->val()["ref"].obj() == m->val()["items"][1].obj() );
}
}
}
TEST_CASE("model_fqn_scope_provider", "[textx/scoping]")
{
{
auto v = textx::scoping::separate_name("a.b.c");
CHECK(v.size()==3);
CHECK(v[0]=="a");
CHECK(v[1]=="b");
CHECK(v[2]=="c");
}
{
auto v = textx::scoping::separate_name("a");
CHECK(v.size()==1);
CHECK(v[0]=="a");
}
{
CHECK_THROWS(textx::scoping::separate_name("a..b"));
CHECK_THROWS(textx::scoping::separate_name("a.b."));
CHECK_THROWS(textx::scoping::separate_name("."));
}
{
auto grammar1 = R"#(
Model: packages+=Package;
Package: 'package' name=ID
'begin'
( packages=Package | items=Item )*
'end';
Item: 'item' name=ID;
)#";
auto modeltext = R"(
package p1 begin
item A
item C
package p2 begin
item A
item B
end
end
package b1 begin
package b2 begin
item X
end
end
)";
auto mm = textx::metamodel_from_str(grammar1);
auto m = mm->model_from_str(modeltext);
auto p1_C = textx::scoping::dot_separated_name_search(m->val().obj(),"p1.C");
CHECK( p1_C == m->val()["packages"][0]["items"][1].obj() );
auto p1_A = textx::scoping::dot_separated_name_search(p1_C->parent(),"A");
CHECK( p1_A == m->val()["packages"][0]["items"][0].obj() );
auto p1_p2_A = textx::scoping::dot_separated_name_search(p1_C->parent(),"p2.A");
CHECK( p1_p2_A == m->val()["packages"][0]["packages"][0]["items"][0].obj() );
auto none_C = textx::scoping::dot_separated_name_search(m->val().obj(),"C");
CHECK( none_C == nullptr );
}
}
TEST_CASE("model_ref_fqn", "[textx/scoping]")
{
{
auto grammar1 = R"#(
Model: packages+=Package;
Package: 'package' name=ID
'begin'
( packages=Package | items=Item | usings=Using )*
'end';
Item: 'item' name=ID;
Using: 'using' name=ID '=' ref=[ItemOrPackage|FQN];
FQN: ID ('.' ID)*;
Comment: /\/\/.*?$/;
ItemOrPackage: Item|Package;
)#";
{
auto modeltext = R"(
package p1 begin
item A
item C
package p2 begin
item A
item B
using p2_p2A=A
using p2_p1C=C
using p2_x=b1.b2.X
using b2r=b1.b2
//does not work with FQN, wait for REEL: using pass_through_ref=b2r.X
end
using p1_p1A=A
using p1_p1C=C
using p1_p2A=p2.A
end
package b1 begin
package b2 begin
item X
end
end
)";
{
auto mm = textx::metamodel_from_str(grammar1);
CHECK_THROWS_WITH((void)mm->model_from_str(modeltext),Catch::Matchers::Contains("p2.A"));
CHECK_THROWS_WITH((void)mm->model_from_str(modeltext),Catch::Matchers::Contains("b1.b2.X"));
}
{
auto mm = textx::metamodel_from_str(grammar1);
mm->set_resolver("*.*", std::make_unique<textx::scoping::FQNRefResolver>());
auto m = mm->model_from_str(modeltext);
CHECK( (*m->fqn("p1.p2.p2_x"))["ref"].obj() == m->fqn("b1.b2.X") );
}
}
}
}
TEST_CASE("model_ref_fqn_with_builtin", "[textx/scoping]")
{
auto grammar1 = R"#(
Model: packages+=Package;
Package: 'package' name=ID
'begin'
( packages=Package | items=Item | usings=Using )*
'end';
Item: 'item' name=ID;
Using: 'using' name=ID '=' ref=[ItemOrPackage|FQN];
FQN: ID ('.' ID)*;
Comment: /\/\/.*?$/;
ItemOrPackage: Item|Package;
)#";
auto modeltext = R"(
package p1 begin
item A
item C
using X = p1.builtin.X
end
)";
auto modeltext_builtin = R"(
package p1 begin
package builtin begin
item X
end
end
)";
auto mm = textx::metamodel_from_str(grammar1);
mm->set_resolver("*.*", std::make_unique<textx::scoping::FQNRefResolver>());
mm->add_builtin_model(mm->model_from_str(modeltext_builtin));
CHECK_NOTHROW(mm->model_from_str(modeltext));
}
TEST_CASE("model_ref_fqn_bad_target_type", "[textx/scoping]")
{
{
auto grammar1 = R"#(
Model: packages+=Package;
Package: 'package' name=ID
'begin'
( packages=Package | items=Item | usings=Using )*
'end';
Item: 'item' name=ID;
Using: 'using' name=ID '=' ref=[Item|FQN];
FQN: ID ('.' ID)*;
Comment: /\/\/.*?$/;
)#";
{
auto modeltext = R"(
package p1 begin
item A
item C
package p2 begin
item A
item B
using p2_p2A=A
using p2_p1C=C
using p2_x=b1.b2.X
using b2r=b1.b2 // bad type: exception <-------- expected exception
//does not work with FQN, wait for REEL: using pass_through_ref=b2r.X
end
using p1_p1A=A
using p1_p1C=C
using p1_p2A=p2.A
end
package b1 begin
package b2 begin
item X
end
end
)";
auto mm = textx::metamodel_from_str(grammar1);
mm->set_resolver("*.*", std::make_unique<textx::scoping::FQNRefResolver>());
CHECK_THROWS_WITH( (void)mm->model_from_str(modeltext), Catch::Matchers::Contains("'b2' has not expected type 'Item'"));
}
}
}
TEST_CASE("model_ref1_type_check", "[textx/scoping]")
{
{
auto grammar1 = R"#(
Model: as+=A bs+=B 'ref' ref=[Item];
Item: A|B;
A: 'a' name=ID;
B: 'b' name=ID;
)#";
{
auto mm = textx::metamodel_from_str(grammar1);
auto m1 = mm->model_from_str("a A1 a A2 b B1 ref B1");
auto m2 = mm->model_from_str("a A1 a A2 b B1 ref A2");
}
}
{
auto grammar1 = R"#(
Model: as+=A bs+=B 'ref' ref=[A];
Item: A|B;
A: 'a' name=ID;
B: 'b' name=ID;
)#";
{
auto mm = textx::metamodel_from_str(grammar1);
CHECK_THROWS_WITH( mm->model_from_str("a A1 a A2 b B1 ref B1"), Catch::Matchers::Contains("'B1' has not expected type 'A'") );
auto m2 = mm->model_from_str("a A1 a A2 b B1 ref A2");
}
}
}