forked from cucumber/gherkin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gherkin-c-parser.razor
276 lines (243 loc) · 8.53 KB
/
gherkin-c-parser.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
@using Berp;
@helper CallProduction(ProductionRule production)
{
switch(production.Type)
{
case ProductionRuleType.Start:
@: start_rule(context, [email protected]);
break;
case ProductionRuleType.End:
@: end_rule(context, [email protected]);
break;
case ProductionRuleType.Process:
@: build(context, token);
break;
}
}
@helper HandleParserError(IEnumerable<string> expectedTokens, State state)
{<text>
/* "State: @state.Id - @Raw(state.Comment)" */
const wchar_t* const expected_tokens = L"@Raw(string.Join(", ", expectedTokens))";
Token_is_eof(token) ? ErrorList_add_unexpected_eof_error(context->errors, token, expected_tokens) : ErrorList_add_unexpected_token_error(context->errors, token, expected_tokens);
Token_delete(token);
if (context->stop_at_first_error) {
ErrorList_jump_to_global_rescue_env(context->errors);
}
return @state.Id;</text>}
@helper MatchToken(TokenType tokenType)
{<text>match_@(tokenType)(context, token)</text>}
/* This file is generated. Do not edit! Edit gherkin-c-parser.razor instead. */
#include "parser.h"
#include "rule_type.h"
#include "token_scanner.h"
#include "token_matcher.h"
#include "token_queue.h"
#include "error_list.h"
#include <stdlib.h>
#include <setjmp.h>
typedef struct ParserContext {
bool stop_at_first_error;
TokenScanner* token_scanner;
TokenMatcher* token_matcher;
Builder* builder;
TokenQueue* token_queue;
ErrorList* errors;
} ParserContext;
struct @Model.ParserClassName {
ParserContext* parser_context;
Builder* builder;
ErrorList* errors;
};
static Token* read_token(ParserContext* context);
static void start_rule(ParserContext* context, RuleType rule_type);
static void end_rule(ParserContext* context, RuleType rule_type);
static int match_token(int state, Token* token, ParserContext* context);
ParserContext* ParserContext_new(TokenScanner* token_scanner, TokenMatcher* token_matcher, Builder* builder, TokenQueue* token_queue, ErrorList* errors) {
ParserContext* parser_context = (ParserContext*)malloc(sizeof(ParserContext));
parser_context->stop_at_first_error = false;
parser_context->token_scanner = token_scanner;
parser_context->token_matcher = token_matcher;
parser_context->builder = builder;
parser_context->token_queue = token_queue;
parser_context->errors = errors;
return parser_context;
}
void ParserContext_delete(ParserContext* parser_context) {
free((void*)parser_context);
}
@Model.ParserClassName* @(Model.ParserClassName)_new(Builder* builder) {
@Model.ParserClassName* parser = (Parser*)malloc(sizeof(@Model.ParserClassName));
parser->parser_context = 0;
parser->builder = builder;
parser->errors = ErrorList_new();
return parser;
}
void @(Model.ParserClassName)_delete(@Model.ParserClassName* parser) {
if (parser->errors) {
ErrorList_delete(parser->errors);
}
if (parser->parser_context) {
ParserContext_delete(parser->parser_context);
}
free((void*)parser);
}
int @(Model.ParserClassName)_parse(@Model.ParserClassName* parser, TokenMatcher* token_matcher, TokenScanner* token_scanner) {
parser->builder->reset(parser->builder);
parser->builder->set_error_context(parser->builder, parser->errors);
token_matcher->reset(token_matcher);
token_matcher->errors = parser->errors;
TokenQueue* token_queue = TokenQueue_new();
ParserContext* context = ParserContext_new(token_scanner, token_matcher, parser->builder, token_queue, parser->errors);
int val = 0;
jmp_buf env;
val = setjmp(env);
ErrorList_set_global_rescue_env(parser->errors, &env);
if (val == 0) {
start_rule(context, [email protected]);
int state = 0;
bool token_is_eof;
Token* token = 0;
do {
token = read_token(context);
token_is_eof = Token_is_eof(token);
state = match_token(state, token, context);
} while (!token_is_eof);
end_rule(context, [email protected]);
}
int result_code = ErrorList_is_empty(context->errors) ? 0 : 1;
ParserContext_delete(context);
TokenQueue_delete(token_queue);
return result_code;
}
bool Parser_has_more_errors(Parser* parser) {
return ErrorList_has_more_errors(parser->errors);
}
Error* Parser_next_error(Parser* parser) {
return ErrorList_next_error(parser->errors);
}
static Token* read_token(ParserContext* context) {
if (!TokenQueue_is_empty(context->token_queue))
return TokenQueue_remove(context->token_queue);
else
return context->token_scanner->read(context->token_scanner);
}
static void build(ParserContext* context, Token* token) {
context->builder->build(context->builder, token);
}
static void handle_ast_error(ParserContext* context, RuleType rule_type, rule_function action) {
if (context->stop_at_first_error) {
action(context->builder, rule_type);
return;
}
jmp_buf env;
int val = setjmp(env);
ErrorList_set_local_rescue_env(context->errors, &env);
if (val == 0) {
action(context->builder, rule_type);
}
}
static void start_rule(ParserContext* context, RuleType rule_type) {
handle_ast_error(context, rule_type, context->builder->start_rule);
}
static void end_rule(ParserContext* context, RuleType rule_type) {
handle_ast_error(context, rule_type, context->builder->end_rule);
}
static bool handle_external_error(ParserContext* context, Token* token, match_function action) {
if (context->stop_at_first_error) {
return action(context->token_matcher, token);
}
jmp_buf env;
int val = setjmp(env);
ErrorList_set_local_rescue_env(context->errors, &env);
if (val == 0) {
return action(context->token_matcher, token);
}
return false;
}
@foreach(var rule in Model.RuleSet.TokenRules)
{<text>
static bool match_@(rule.Name.Replace("#", ""))(ParserContext* context, Token* token) {
@if (rule.Name != "#EOF")
{
@:if (token->matched_type == Token_EOF) {
@: return false;
@:};
}
@if (rule.Name != "#TagLine")
{
@:return handle_external_error(context, token, context->token_matcher->match_@(rule.Name.Replace("#", "")));
}
@if (rule.Name == "#TagLine")
{
@:bool match_result = handle_external_error(context, token, context->token_matcher->match_TagLine);
@:if (match_result) {
@: bool tag_error = ErrorList_check_token_tags_for_whitespace(context->errors, token);
@: if (tag_error && context->stop_at_first_error) {
@: ErrorList_jump_to_global_rescue_env(context->errors);
@: }
@:}
@:return match_result;
}
}</text>
}
@foreach(var lookAheadHint in Model.RuleSet.LookAheadHints)
{
<text>
static bool lookahead_@(lookAheadHint.Id)(ParserContext* context) {
Token* token = 0;
TokenQueue* queue = TokenQueue_new();
bool match = false;
while (true) {
token = read_token(context);
TokenQueue_add(queue, token);
if (@foreach(var tokenType in lookAheadHint.ExpectedTokens) {<text>@MatchToken(tokenType) || </text>}false) {
match = true;
break;
}
if (!(@foreach(var tokenType in lookAheadHint.Skip) {<text>@MatchToken(tokenType) || </text>}false)) {
break;
}
}
TokenQueue_extend(context->token_queue, queue);
return match;
}
</text>
}
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{<text>
/* @Raw(state.Comment) */
static int match_token_at_@(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)) {
}
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>
}
static int match_token(int state, Token* token, ParserContext* context) {
switch (state) {
@foreach(var state in Model.States.Values.Where(s => !s.IsEndState))
{
@:case @state.Id:
@:return match_token_at_@(state.Id)(token, context);
}
default:
ErrorList_add_invalid_operation_error(context->errors, state);
ErrorList_jump_to_global_rescue_env(context->errors);
return -1;
}
}