-
-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathgherkin-csharp.razor
332 lines (303 loc) · 10.1 KB
/
gherkin-csharp.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
// ------------------------------------------------------------------------------
// <auto-generated>
// 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.
// </auto-generated>
// ------------------------------------------------------------------------------
#region Designer generated code
#pragma warning disable
@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>
const string stateComment = "State: @state.Id - @Raw(state.Comment)";
token.Detach();
var expectedTokens = new string[] {"@Raw(string.Join("\", \"", expectedTokens))"};
var error = token.IsEOF ? (ParserException)new UnexpectedEOFException(token, expectedTokens, stateComment)
: new UnexpectedTokenException(token, expectedTokens, stateComment);
if (StopAtFirstError)
throw error;
AddError(context, error);
return @state.Id;
</text>}
@helper MatchToken(TokenType tokenType)
{<text>Match_@(tokenType)(context, token)</text>}
using System;
using System.Linq;
using System.Collections.Generic;
namespace @Model.Namespace
{
public enum TokenType
{
None,
@foreach(var rule in Model.RuleSet.TokenRules)
{<text> @rule.Name.Replace("#", ""),
</text>}
}
public enum RuleType
{
None,
@foreach(var rule in Model.RuleSet.Where(r => !r.TempRule))
{<text> @rule.Name.Replace("#", "_"), // @rule.ToString(true)
</text>}
}
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public partial class @Model.ParserClassName<T>
{
private readonly IAstBuilder<T> astBuilder;
public Parser()
: this(new AstBuilder<T>())
{
}
public Parser(IAstBuilder<T> astBuilder)
{
this.astBuilder = astBuilder;
}
public bool StopAtFirstError { get; set;}
[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
protected class ParserContext
{
public ITokenScanner TokenScanner { get; set; }
public ITokenMatcher TokenMatcher { get; set; }
public Queue<Token> TokenQueue { get; set; }
public List<ParserException> Errors { get; set; }
}
public T Parse(ITokenScanner tokenScanner)
{
return Parse(tokenScanner, new TokenMatcher());
}
public T Parse(ITokenScanner tokenScanner, ITokenMatcher tokenMatcher)
{
tokenMatcher.Reset();
astBuilder.Reset();
var context = new ParserContext
{
TokenScanner = tokenScanner,
TokenMatcher = tokenMatcher,
TokenQueue = new Queue<Token>(),
Errors = new List<ParserException>()
};
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.Count > 0)
{
throw new CompositeParserException(context.Errors.ToArray());
}
return GetResult(context);
}
protected virtual void AddError(ParserContext context, ParserException error)
{
if (context.Errors.Any(e => e.Message == error.Message))
return;
context.Errors.Add(error);
if (context.Errors.Count > 10)
throw new CompositeParserException(context.Errors.ToArray());
}
private bool TryHandleExternalError(ParserContext context, Exception exception)
{
if (StopAtFirstError)
return false;
if (exception is CompositeParserException compositeParserException)
{
foreach (var error in compositeParserException.Errors)
AddError(context, error);
}
else if (exception is ParserException error)
{
AddError(context, error);
}
return true;
}
void Build(ParserContext context, Token token)
{
try
{
this.astBuilder.Build(token);
}
catch (Exception ex)
{
if (!TryHandleExternalError(context, ex))
throw;
}
}
void StartRule(ParserContext context, RuleType ruleType)
{
try
{
this.astBuilder.StartRule(ruleType);
}
catch (Exception ex)
{
if (!TryHandleExternalError(context, ex))
throw;
}
}
void EndRule(ParserContext context, RuleType ruleType)
{
try
{
this.astBuilder.EndRule(ruleType);
}
catch (Exception ex)
{
if (!TryHandleExternalError(context, ex))
throw;
}
}
T GetResult(ParserContext context)
{
return this.astBuilder.GetResult();
}
Token ReadToken(ParserContext context)
{
return context.TokenQueue.Count > 0 ? context.TokenQueue.Dequeue() : context.TokenScanner.Read();
}
@foreach(var rule in Model.RuleSet.TokenRules)
{<text>
bool Match_@(rule.Name.Replace("#", ""))(ParserContext context, Token token)
{
@if (rule.Name != "#EOF")
{
@:if (token.IsEOF) return false;
}
try
{
return context.TokenMatcher.Match_@(rule.Name.Replace("#", ""))(token);
}
catch (Exception exception)
{
if (TryHandleExternalError(context, exception))
return false;
throw;
}
}</text>
}
protected virtual 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 InvalidOperationException("Unknown state: " + state);
}
return newState;
}
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
<text>
// @Raw(state.Comment)
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>
bool LookAhead_@(lookAheadHint.Id)(ParserContext context, Token currentToken)
{
currentToken.Detach();
Token token;
var queue = new Queue<Token>();
bool match = false;
do
{
token = ReadToken(context);
token.Detach();
queue.Enqueue(token);
if (false
@foreach(var tokenType in lookAheadHint.ExpectedTokens)
{
@:|| @MatchToken(tokenType)
}
)
{
match = true;
break;
}
} while (false
@foreach(var tokenType in lookAheadHint.Skip)
{
@:|| @MatchToken(tokenType)
}
);
foreach(var t in queue)
context.TokenQueue.Enqueue(t);
return match;
}
</text>
}
}
public partial interface IAstBuilder<T>
{
void Build(Token token);
void StartRule(RuleType ruleType);
void EndRule(RuleType ruleType);
T GetResult();
void Reset();
}
public partial interface ITokenScanner
{
Token Read();
}
public partial interface ITokenMatcher
{
@foreach(var rule in Model.RuleSet.TokenRules)
{
@:bool Match_@(rule.Name.Replace("#", ""))(Token token);
}
void Reset();
}
}
#pragma warning restore
#endregion