forked from cucumber/gherkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gherkin-objective-c-implementation.razor
282 lines (248 loc) · 8.87 KB
/
gherkin-objective-c-implementation.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
// ------------------------------------------------------------------------------
// <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>
// ------------------------------------------------------------------------------
@using Berp;
@helper CallProduction(ProductionRule production)
{
switch(production.Type)
{
case ProductionRuleType.Start:
@:[self startRuleWithContext: theContext ruleType: GHRuleType@(production.RuleName)];
break;
case ProductionRuleType.End:
@:[self endRuleWithContext: theContext ruleType: GHRuleType@(production.RuleName)];
break;
case ProductionRuleType.Process:
@:[self buildWithContext: theContext token: theToken];
break;
}
}
@helper HandleParserError(IEnumerable<string> expectedTokens, State state)
{<text>
NSString * stateComment = @@"State: @state.Id - @Raw(state.Comment)";
[theToken detach];
NSArray<NSString *> * expectedTokens = [[NSArray<NSString *> alloc] initWithObjects: @@"@Raw(string.Join("\", @\"", expectedTokens))", nil];
id error = [theToken isEOF] ? (GHParserException *)[[GHUnexpectedEOFException alloc] initWithToken: theToken expectedTokenTypes: expectedTokens stateComment: stateComment] : [[GHUnexpectedTokenException alloc] initWithToken: theToken expectedTokenTypes: expectedTokens stateComment: stateComment];
if (stopAtFirstError)
@@throw error;
[self addError: error withContext: theContext];
return @state.Id;
</text>}
@helper MatchToken(TokenType tokenType)
{<text>[self match@(tokenType)WithContext: theContext token: theToken]</text>}
#import "GHParser.h"
#import "GHAstBuilder.h"
#import "GHToken.h"
#import "GHParserException.h"
@@implementation GHParserContext
@@synthesize tokenScanner;
@@synthesize tokenMatcher;
@@synthesize tokenQueue;
@@synthesize errors;
@@end
@@implementation GH@(Model.ParserClassName)
{
id<GHAstBuilderProtocol> astBuilder;
}
@@synthesize stopAtFirstError;
- (id)init
{
return [self initWithAstBuilder: [[GHAstBuilder alloc] init]];
}
- (id)initWithAstBuilder:(id<GHAstBuilderProtocol>)theAstBuilder
{
if (self = [super init])
{
astBuilder = theAstBuilder;
}
return self;
}
- (id)parseWithTokenScanner:(id<GHTokenScannerProtocol>)theTokenScanner
{
return [self parseWithTokenScanner: theTokenScanner tokenMatcher: [[GHTokenMatcher alloc] init]];
}
- (id)parseWithTokenScanner:(id<GHTokenScannerProtocol>)theTokenScanner tokenMatcher:(id<GHTokenMatcherProtocol>)theTokenMatcher
{
[theTokenMatcher reset];
[astBuilder reset];
GHParserContext * context = [[GHParserContext alloc] init];
[context setTokenScanner: theTokenScanner];
[context setTokenMatcher: theTokenMatcher];
[context setTokenQueue: [[NSMutableArray<GHToken *> alloc] init]];
[context setErrors: [[NSMutableArray<GHParserException *> alloc] init]];
[self startRuleWithContext: context ruleType: GHRuleType@(Model.RuleSet.StartRule.Name)];
NSUInteger state = 0;
GHToken * token;
do
{
token = [self readTokenWithContext: context];
state = [self matchToken: token withState: state context: context];
} while(![token isEOF]);
[self endRuleWithContext: context ruleType: GHRuleType@(Model.RuleSet.StartRule.Name)];
if ([[context errors] count])
{
@@throw [[GHCompositeParserException alloc] initWithErrors: [context errors]];
}
return [self resultWithContext: context];
}
- (void)addError:(GHParserException *)theParserError withContext:(GHParserContext *)theContext
{
[[theContext errors] addObject: theParserError];
if ([[theContext errors] count] > 10)
@@throw [[GHCompositeParserException alloc] initWithErrors: [theContext errors]];
}
- (void)handleAstErrorWithContext:(GHParserContext *)theContext actionBlock:(void (^)(void))theActionBlock
{
[self handleExternalErrorWithContext: theContext actionBlock: ^() { theActionBlock(); return YES; } defaultValue: NO];
}
- (BOOL)handleExternalErrorWithContext:(GHParserContext *)theContext actionBlock:(BOOL (^)(void))theActionBlock defaultValue:(BOOL)theDefaultValue
{
if (stopAtFirstError)
{
return theActionBlock();
}
@@try
{
return theActionBlock();
}
@@catch (GHCompositeParserException * compositeParserException)
{
for (GHParserException * error in [compositeParserException errors])
{
[self addError: error withContext: theContext];
}
}
@@catch (GHParserException * error)
{
[self addError: error withContext: theContext];
}
return theDefaultValue;
}
- (void)buildWithContext:(GHParserContext *)theContext token:(GHToken *)theToken
{
[self handleAstErrorWithContext: theContext actionBlock: ^() { [self->astBuilder buildWithToken: theToken]; }];
}
- (void)startRuleWithContext:(GHParserContext *)theContext ruleType:(GHRuleType)theRuleType
{
[self handleAstErrorWithContext: theContext actionBlock: ^() { [self->astBuilder startRuleWithType: theRuleType]; }];
}
- (void)endRuleWithContext:(GHParserContext *)theContext ruleType:(GHRuleType)theRuleType
{
[self handleAstErrorWithContext: theContext actionBlock: ^() { [self->astBuilder endRuleWithType: theRuleType]; }];
}
- (id)resultWithContext:(GHParserContext *)theContext
{
return [astBuilder result];
}
- (GHToken *)readTokenWithContext:(GHParserContext *)theContext
{
if ([[theContext tokenQueue] count] > 0)
{
id firstObject = [[theContext tokenQueue] firstObject];
[[theContext tokenQueue] removeObjectAtIndex: 0];
return firstObject;
}
else
return [[theContext tokenScanner] read];
}
@foreach(var rule in Model.RuleSet.TokenRules)
{<text>
- (BOOL)match@(rule.Name.Replace("#", ""))WithContext:(GHParserContext *)theContext token:(GHToken *)theToken
{
@if (rule.Name != "#EOF")
{
@:if ([theToken isEOF]) return NO;
}
return [self handleExternalErrorWithContext: theContext actionBlock: ^() { return [[theContext tokenMatcher] match@(rule.Name.Replace("#", ""))WithToken: theToken]; } defaultValue: NO];
}</text>
}
- (NSUInteger)matchToken:(GHToken *)theToken withState:(NSUInteger)theState context:(GHParserContext *)theContext
{
NSUInteger newState;
switch (theState)
{
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
@:case @state.Id:
@:newState = [self matchTokenAt_@(state.Id): theToken context: theContext];
@:break;
}
default:
@@throw [NSException exceptionWithName: NSInvalidArgumentException reason: [@@"Unknown state: " stringByAppendingFormat: @@"%d", (int)theState] userInfo: nil];
}
return newState;
}
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
<text>
// @Raw(state.Comment)
- (int)matchTokenAt_@(state.Id):(GHToken *)theToken context:(GHParserContext *)theContext
{
@foreach(var transition in state.Transitions)
{
@:if (@MatchToken(transition.TokenType))
@:{
if (transition.LookAheadHint != null)
{
@:if ([self lookAheadWithContext_@(transition.LookAheadHint.Id): theContext token: theToken])
@:{
}
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)lookAheadWithContext_@(lookAheadHint.Id):(GHParserContext *)theContext token:(GHToken *)currentToken
{
[currentToken detach];
GHToken * theToken;
NSMutableArray<GHToken *> * tokenBuffer = [[NSMutableArray<GHToken *> alloc] init];
BOOL match = NO;
do
{
theToken = [self readTokenWithContext: theContext];
[theToken detach];
[tokenBuffer addObject: theToken];
if (NO
@foreach(var tokenType in lookAheadHint.ExpectedTokens)
{
@:|| @MatchToken(tokenType)
}
)
{
match = YES;
break;
}
} while (NO
@foreach(var tokenType in lookAheadHint.Skip)
{
@:|| @MatchToken(tokenType)
}
);
for (GHToken * token in tokenBuffer)
{
[[theContext tokenQueue] addObject: token];
}
return match;
}
</text>
}
@@end