-
Notifications
You must be signed in to change notification settings - Fork 317
/
ConcreteSyntaxConstraintProviderTest.java
254 lines (219 loc) · 9.43 KB
/
ConcreteSyntaxConstraintProviderTest.java
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
/*******************************************************************************
* Copyright (c) 2010 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.xtext.validation;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.xtext.AbstractRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.XtextStandaloneSetup;
import org.eclipse.xtext.testing.AbstractXtextTests;
import org.eclipse.xtext.validation.IConcreteSyntaxConstraintProvider.ISyntaxConstraint;
import org.eclipse.xtext.validation.impl.ConcreteSyntaxConstraintProvider;
import org.junit.Test;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
/**
* @author Moritz Eysholdt - Initial contribution and API
*/
public class ConcreteSyntaxConstraintProviderTest extends AbstractXtextTests {
private static class CSCPTest extends ConcreteSyntaxConstraintProvider {
public CSCPTest(Grammar grammar) {
super();
this.grammar = grammar;
}
}
final static String HEADER = "grammar org.eclipse.xtext.validation.ConcreteSyntaxConstraintProviderTestLanguage"
+ " with org.eclipse.xtext.common.Terminals "
+ "generate treeConstTest \"http://www.eclipse.org/2010/tmf/xtext/CSCT\" ";
private String parseRule(String body) throws Exception {
Grammar grammar = (Grammar) getModel(HEADER + body);
Matcher m = Pattern.compile("^[a-zA-Z]+").matcher(body);
m.find();
String firstRuleName = m.group();
AbstractRule firstRule = GrammarUtil.findRuleForName(grammar, firstRuleName);
IConcreteSyntaxConstraintProvider cscp = new CSCPTest(grammar);
return toString(cscp.getConstraint((ParserRule) firstRule));
}
@Override
public void setUp() throws Exception {
super.setUp();
with(XtextStandaloneSetup.class);
}
private String toString(ISyntaxConstraint c) {
if (c == null)
return null;
String t = "";
if (c.getSemanticTypesToCheck() != null) {
List<String> names = Lists.newArrayList(Iterables.transform(c.getSemanticTypesToCheck(),
new Function<EClass, String>() {
@Override
public String apply(EClass from) {
return from.getName();
}
}));
Collections.sort(names);
t = "<" + Joiner.on(", ").join(names) + ">";
}
Iterable<String> contents = Iterables.transform(c.getContents(), new Function<ISyntaxConstraint, String>() {
@Override
public String apply(ISyntaxConstraint from) {
return ConcreteSyntaxConstraintProviderTest.this.toString(from);
}
});
switch (c.getType()) {
case ASSIGNMENT:
return t + c.getAssignmentName() + c.getCardinality();
case GROUP:
return t + "(" + Joiner.on(" ").join(contents) + ")" + c.getCardinality();
case ALTERNATIVE:
return t + "(" + Joiner.on("|").join(contents) + ")" + c.getCardinality();
case ACTION:
return t + "{}";
}
return "";
}
@Test public void testSimple() throws Exception {
String constraint = parseRule("Model: name=ID;");
assertEquals("<Model>name", constraint);
}
@Test public void testGroup1() throws Exception {
String constraint = parseRule("Model: name=ID (val=INT 'somekw')?;");
assertEquals("<Model>(name val?)", constraint);
}
@Test public void testAlternatives1() throws Exception {
String constraint = parseRule("Model: name=ID | val=INT | 'somekw';");
assertEquals("<Model>(name|val)?", constraint);
}
@Test public void testAction1() throws Exception {
String constraint = parseRule("Model: {Model} name=ID;");
assertEquals("<Model>({} name)", constraint);
}
@Test public void testAction2() throws Exception {
String constraint = parseRule("Model: {Type1} name=ID;");
assertEquals("<Type1>({} name)", constraint);
}
@Test public void testAction3() throws Exception {
String constraint = parseRule("Model: 'something' ({Type1} name=ID)? val=INT;");
assertEquals("<Model, Type1>(<Type1>({} name)? val)", constraint);
}
@Test public void testAction4() throws Exception {
String constraint = parseRule("Model: 't1' {Type1} | 't2' {Type2} | 't3' {Type3};");
assertEquals("<Type1, Type2, Type3>(<Type1>{}|<Type2>{}|<Type3>{})", constraint);
}
@Test public void testAction5() throws Exception {
String constraint = parseRule("Model: 't1' {Type1} | 't2' {Type2} | 't3' {Type3} | 't4';");
assertEquals("<Model, Type1, Type2, Type3>(<Type1>{}|<Type2>{}|<Type3>{})?", constraint);
}
@Test public void testAction6() throws Exception {
String constraint = parseRule("Model: 't1' {Type1} val=ID | 't2' {Type2} val=ID | 't3' {Type3} | 't4';");
assertEquals("<Model, Type1, Type2, Type3>(<Type1>({} val)|<Type2>({} val)|<Type3>{})?", constraint);
}
@Test public void testAction7() throws Exception {
String constraint = parseRule("Model: ('t1' {Type1} val=ID | 't2' {Type2} val=ID | 't3' {Type3})?;");
assertEquals("<Model, Type1, Type2, Type3>(<Type1>({} val)|<Type2>({} val)|<Type3>{})?", constraint);
}
@Test public void testAction8() throws Exception {
String constraint = parseRule("Model: 't1' {Type1} val=ID | ('something' ({Type2} 't2' | {Type3} 't3'));");
assertEquals("<Type1, Type2, Type3>(<Type1>({} val)|<Type2, Type3>(<Type2>{}|<Type3>{}))", constraint);
}
@Test public void testAction9() throws Exception {
String constraint = parseRule("Model: ('t1' {Type1} val1=ID | val2=ID) val3=ID;");
assertEquals("<Model, Type1>((<Type1>({} val1)|<Model>val2) val3)", constraint);
}
@Test public void testSummarizeAssignments1() throws Exception {
String constraint = parseRule("Model: val+=ID (',' val+=ID)*;");
assertEquals("<Model>val+", constraint);
}
@Test public void testSummarizeAssignments2() throws Exception {
String constraint = parseRule("Model: val+=ID (',' val+=ID)+;");
assertEquals("<Model>(val val+)", constraint);
}
@Test public void testSummarizeAssignments3() throws Exception {
String constraint = parseRule("Model: (val+=ID (',' val+=ID)*)?;");
assertEquals("<Model>val*", constraint);
}
@Test public void testMulti1() throws Exception {
String constraint = parseRule("model : ('kw1' a+=ID b+=ID)* ('kw2' a+=ID c+=ID)* ('kw3' b+=ID c+=ID)*;");
assertEquals("<model>((a b)* (a c)* (b c)*)", constraint);
}
@Test public void testRuleCall1() throws Exception {
StringBuilder b = new StringBuilder();
b.append("Model: Sub; ");
b.append("Sub: val=ID; ");
String constraint = parseRule(b.toString());
assertEquals("<Sub>val", constraint);
}
@Test public void testRuleCall2() throws Exception {
StringBuilder b = new StringBuilder();
b.append("Model: (Sub1 | Sub2) name=ID 'somekeyword'; ");
b.append("Sub1: 'sub1' val1=ID; ");
b.append("Sub2: 'sub2' val2=ID; ");
String constraint = parseRule(b.toString());
assertEquals("<Sub1, Sub2>((<Sub1>val1|<Sub2>val2) name)", constraint);
}
@Test public void testRuleCall3() throws Exception {
StringBuilder b = new StringBuilder();
b.append("Model: (Sub1 | Sub2)? name=ID 'somekeyword'; ");
b.append("Sub1: 'sub1' val1=ID; ");
b.append("Sub2: 'sub2' val2=ID; ");
String constraint = parseRule(b.toString());
assertEquals("<Model, Sub1, Sub2>(<Sub1, Sub2>(<Sub1>val1|<Sub2>val2)? name)", constraint);
}
@Test public void testRecursion() throws Exception {
StringBuilder b = new StringBuilder();
b.append("Model returns Model: Sub1 | name=ID 'somekeyword'; ");
b.append("Sub1 returns Model: '(' Model ')'; ");
String constraint = parseRule(b.toString());
assertNull(constraint);
}
@Test public void testAssignedActions() throws Exception {
StringBuilder b = new StringBuilder();
b.append("Model: 'y' val=ID ({Evil.left=current} ',' right=ID)?; ");
String constraint = parseRule(b.toString());
assertNull(constraint);
}
@Test public void testNestedAssignedActions() throws Exception {
StringBuilder b = new StringBuilder();
b.append("Model: Foo | Bar; ");
b.append("Foo: 'x' val=ID; ");
b.append("Bar: 'y' val=ID ({Evil.left=current} ',' right=ID)?; ");
String constraint = parseRule(b.toString());
assertNull(constraint);
}
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=312220
@Test public void testUnassignedRuleCall() throws Exception {
String constraint = parseRule("WithoutHidden hidden(): '[' 'kw' WS x=INT '.' y=INT ']';");
assertEquals("<WithoutHidden>(x y)", constraint);
}
@Test public void testNestedAlternative() throws Exception {
StringBuilder b = new StringBuilder();
b.append("Model: (SE | Decl) ';'; ");
b.append("Decl: type+=ID name+=ID; ");
b.append("SE: '`' (ids+=(WS|ID|ANY_OTHER)|ints+=INT)+ '`'; ");
String constraint = parseRule(b.toString());
assertEquals("<Decl, SE>(<SE>(ids|ints)+|<Decl>(type name))", constraint);
}
@Test public void testEmptyAlternative() throws Exception {
String constraint = parseRule("Import: ('import' | 'imp') foo=ID?;");
assertEquals("<Import>foo?", constraint);
}
@Test public void testOptionalGroupWithUnassignedElements() throws Exception {
StringBuilder b = new StringBuilder();
b.append("Model: ('(' INT ')')? a=STRING b=STRING;");
String constraint = parseRule(b.toString());
assertEquals("<Model>(a b)", constraint);
}
}