forked from cucumber/gherkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgherkin-java.razor
312 lines (272 loc) · 9.28 KB
/
gherkin-java.razor
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
// This code was generated by Berp (http://https://github.com/gasparnagy/berp/).
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
@using Berp;
@helper CallProduction(ProductionRule production)
{
switch(production.Type) {
case ProductionRuleType.Start:
@:startRule(context, [email protected]);
break;
case ProductionRuleType.End:
@:endRule(context, [email protected]);
break;
case ProductionRuleType.Process:
@:build(context, token);
break;
}
}
@helper HandleParserError(IEnumerable<string> expectedTokens, State state)
{<text>
final String stateComment = "State: @state.Id - @Raw(state.Comment)";
token.detach();
List<String> expectedTokens = asList("@Raw(string.Join("\", \"", expectedTokens))");
ParserException error = token.isEOF()
? new ParserException.UnexpectedEOFException(token, expectedTokens, stateComment)
: new ParserException.UnexpectedTokenException(token, expectedTokens, stateComment);
addError(context, error);
return @state.Id;
</text>}
@helper matchToken(TokenType tokenType)
{<text>match_@(tokenType)(context, token)</text>}
package io.cucumber.gherkin;
import java.io.Reader;
import java.io.StringReader;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import static java.util.Arrays.asList;
class @Model.ParserClassName<T> {
enum TokenType {
None,
@foreach(var rule in Model.RuleSet.TokenRules)
{<text> @rule.Name.Replace("#", ""),
</text>} ;
}
enum RuleType {
None,
@foreach(var rule in Model.RuleSet.Where(r => !r.TempRule))
{<text> @rule.Name.Replace("#", "_"), // @rule.ToString(true)
</text>} ;
static RuleType cast(TokenType tokenType) {
return RuleType.values()[tokenType.ordinal()];
}
}
private final Builder<T> builder;
static class ParserContext {
final ITokenScanner tokenScanner;
final ITokenMatcher tokenMatcher;
final Queue<Token> tokenQueue;
final List<ParserException> errors;
ParserContext(ITokenScanner tokenScanner, ITokenMatcher tokenMatcher, Queue<Token> tokenQueue, List<ParserException> errors) {
this.tokenScanner = tokenScanner;
this.tokenMatcher = tokenMatcher;
this.tokenQueue = tokenQueue;
this.errors = errors;
}
}
Parser(Builder<T> builder) {
this.builder = builder;
}
T parse(String source, String uri) {
return parse(new StringReader(source), uri);
}
T parse(Reader source, String uri) {
return parse(new TokenScanner(source), uri);
}
T parse(ITokenScanner tokenScanner, String uri) {
return parse(tokenScanner, new TokenMatcher(), uri);
}
T parse(String source, ITokenMatcher tokenMatcher, String uri) {
return parse(new StringReader(source), tokenMatcher, uri);
}
T parse(Reader source, ITokenMatcher tokenMatcher, String uri) {
return parse(new TokenScanner(source), tokenMatcher, uri);
}
T parse(ITokenScanner tokenScanner, ITokenMatcher tokenMatcher, String uri) {
builder.reset(uri);
tokenMatcher.reset();
ParserContext context = new ParserContext(
tokenScanner,
tokenMatcher,
new LinkedList<>(),
new ArrayList<>()
);
startRule(context, [email protected]);
int state = 0;
Token token;
do {
token = readToken(context);
state = matchToken(state, token, context);
} while (!token.isEOF());
endRule(context, [email protected]);
if (context.errors.size() > 0) {
throw new ParserException.CompositeParserException(context.errors);
}
return builder.getResult();
}
private void addError(ParserContext context, ParserException error) {
String newErrorMessage = error.getMessage();
for (ParserException e : context.errors) {
if (e.getMessage().equals(newErrorMessage)) {
return;
}
}
context.errors.add(error);
if (context.errors.size() > 10)
throw new ParserException.CompositeParserException(context.errors);
}
private <V> V handleAstError(ParserContext context, final Func<V> action) {
return handleExternalError(context, action, null);
}
private <V> V handleExternalError(ParserContext context, Func<V> action, V defaultValue) {
try {
return action.call();
} catch (ParserException.CompositeParserException compositeParserException) {
for (ParserException error : compositeParserException.errors) {
addError(context, error);
}
} catch (ParserException error) {
addError(context, error);
}
return defaultValue;
}
private void build(final ParserContext context, final Token token) {
handleAstError(context, new Func<Void>() {
public Void call() {
builder.build(token);
return null;
}
});
}
private void startRule(final ParserContext context, final RuleType ruleType) {
handleAstError(context, new Func<Void>() {
public Void call() {
builder.startRule(ruleType);
return null;
}
});
}
private void endRule(final ParserContext context, final RuleType ruleType) {
handleAstError(context, new Func<Void>() {
public Void call() {
builder.endRule(ruleType);
return null;
}
});
}
private Token readToken(ParserContext context) {
return context.tokenQueue.size() > 0 ? context.tokenQueue.remove() : context.tokenScanner.read();
}
@foreach(var rule in Model.RuleSet.TokenRules)
{<text>
private boolean match_@(rule.Name.Replace("#", ""))(final ParserContext context, final Token token) {
@if (rule.Name != "#EOF")
{
@:if (token.isEOF()) return false;
}
return handleExternalError(context, new Func<Boolean>() {
public Boolean call() {
return context.tokenMatcher.match_@(rule.Name.Replace("#", ""))(token);
}
}, false);
}</text>
}
private int matchToken(int state, Token token, ParserContext context) {
int newState;
switch (state) {
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
@:case @state.Id:
@:newState = matchTokenAt_@(state.Id)(token, context);
@:break;
}
default:
throw new IllegalStateException("Unknown state: " + state);
}
return newState;
}
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
<text>
// @Raw(state.Comment)
private int matchTokenAt_@(state.Id)(Token token, ParserContext context) {
@foreach(var transition in state.Transitions)
{
@:if (@matchToken(transition.TokenType))
@:{
if (transition.LookAheadHint != null)
{
@:if (lookahead_@(transition.LookAheadHint.Id)(context, token))
@:{
}
foreach(var production in transition.Productions)
{
@CallProduction(production)
}
@:return @transition.TargetState;
if (transition.LookAheadHint != null)
{
@:}
}
@:}
}
@HandleParserError(state.Transitions.Select(t => "#" + t.TokenType.ToString()).Distinct(), state)
}
</text>
}
@foreach(var lookAheadHint in Model.RuleSet.LookAheadHints)
{
<text>
private boolean lookahead_@(lookAheadHint.Id)(ParserContext context, Token currentToken) {
currentToken.detach();
Token token;
Queue<Token> queue = new ArrayDeque<Token>();
boolean match = false;
do
{
token = readToken(context);
token.detach();
queue.add(token);
if (false
@foreach(var tokenType in lookAheadHint.ExpectedTokens)
{
@:|| @matchToken(tokenType)
}
)
{
match = true;
break;
}
} while (false
@foreach(var tokenType in lookAheadHint.Skip)
{
@:|| @matchToken(tokenType)
}
);
context.tokenQueue.addAll(queue);
return match;
}
</text>
}
interface Builder<T> {
void build(Token token);
void startRule(RuleType ruleType);
void endRule(RuleType ruleType);
T getResult();
void reset(String uri);
}
interface ITokenScanner {
Token read();
}
interface ITokenMatcher {
@foreach(var rule in Model.RuleSet.TokenRules)
{
@:boolean match_@(rule.Name.Replace("#", ""))(Token token);
}
void reset();
}
}