-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
309 lines (270 loc) · 10.8 KB
/
parse.c
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
/**
* JCUnit - a very simple unit testing framework for C
*
* Copyright (C) 2021-2022 Denis Korchagin <denis.korchagin.1995@gmail.com>
*
* This file is part of JCUnit
*
* For the full license information, please view the LICENSE
* file that was distributed with this source code.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation of version 2
* of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <string.h>
#include "headers/parse.h"
#include "headers/ast.h"
#include "headers/object-allocator.h"
#include "headers/errors.h"
struct directive_parse_context
{
struct string ** directive;
struct slist ** arguments_end;
bool has_unnamed_argument;
};
static struct ast_test * parse_test(struct tokenizer_context * context);
static void parse_test_prolog(struct tokenizer_context * context, struct ast_test ** ast_test);
static void parse_test_epilog(struct tokenizer_context * context);
static void parse_requirement_list(struct tokenizer_context * context, struct ast_test ** ast_test);
static void parse_requirement(struct tokenizer_context * context, struct ast_requirement * requirement);
static void parse_directive(struct tokenizer_context * context, struct directive_parse_context * directive_parse_context);
static void parse_directive_argument(struct tokenizer_context * context, struct directive_parse_context * directive_parse_context);
static void parse_named_directive_argument(struct tokenizer_context * context, struct directive_parse_context * directive_parse_context);
static void skip_whitespaces(struct tokenizer_context * context);
struct slist * parse_test_suite(struct tokenizer_context * context)
{
struct ast_test * ast_test;
struct slist * tests = make_slist();
struct slist ** tests_end = &tests->next;
struct token * token = peek_one_token(context);
while (!is_token_eof(token)) {
if (is_token_newline(token)) { /* skip some newline between a tests */
for(;;) {
token = get_one_token(context);
if (!is_token_newline(token)) {
unget_one_token(context, token);
break;
}
release_token(token);
}
continue;
}
ast_test = parse_test(context);
slist_append(tests_end, &ast_test->list_entry);
token = peek_one_token(context);
}
return tests;
}
struct ast_test * parse_test(struct tokenizer_context * context)
{
struct ast_test * ast_test = make_ast_test();
parse_test_prolog(context, &ast_test);
context->mode = TOKENIZER_MODE_DIRECTIVE_AND_TEXT;
parse_requirement_list(context, &ast_test);
context->mode = TOKENIZER_MODE_NONE;
parse_test_epilog(context);
return ast_test;
}
void parse_requirement_list(struct tokenizer_context * context, struct ast_test ** ast_test)
{
struct token * token = get_one_token(context);
if (is_token_directive_equals(token, "endtest")) {
jcunit_fatal_error("There is no any requirement provided for test!");
}
unget_one_token(context, token);
unsigned int loop_control = 0;
struct ast_requirement * requirement = NULL;
struct slist ** requirements_end = slist_get_end(&(*ast_test)->requirements);
for(;;) {
token = peek_one_token(context);
if (is_token_directive_equals(token, "endtest")) {
break;
}
if (loop_control >= MAX_REQUIREMENT_COUNT) {
jcunit_fatal_error("Exceed max requirement for test!");
}
++loop_control;
requirement = make_ast_requirement();
parse_requirement(context, requirement);
slist_append(requirements_end, &requirement->list_entry);
}
}
void parse_requirement(struct tokenizer_context * context, struct ast_requirement * requirement)
{
static char buffer[MAX_REQUIREMENT_CONTENT_SIZE] = {0};
struct directive_parse_context directive_parse_context;
directive_parse_context.directive = &requirement->name;
directive_parse_context.arguments_end = &requirement->arguments.next;
directive_parse_context.has_unnamed_argument = false;
parse_directive(context, &directive_parse_context);
struct token * token = peek_one_token(context);
if (is_token_directive(token)) {
return; /* no content for requirement */
}
unsigned int oldmode = context->mode;
context->mode = TOKENIZER_MODE_DIRECTIVE_AND_TEXT;
unsigned int len = 0;
char * w = buffer;
int last_ch = token->content.ch;
while (len < MAX_REQUIREMENT_CONTENT_SIZE) {
token = get_one_token(context);
if (is_token_eof(token)) {
jcunit_fatal_error("An unterminated test!");
}
if (is_token_directive(token)) {
unget_one_token(context, token);
break;
}
last_ch = token->content.ch;
*w++ = (char)last_ch;
++len;
release_token(token);
}
if (!is_token_directive(token)) { /* TODO: test it, make configurable 'max_requirement_content_size' */
jcunit_fatal_error("The requirement's content of the test too long!");
}
if (last_ch == '\n') {
--len; /* we don't need to take a very last char of the requirement's content */
}
context->mode = oldmode;
requirement->content = make_string(buffer, len);
}
void parse_test_prolog(struct tokenizer_context * context, struct ast_test ** ast_test)
{
struct string * directive = NULL;
struct directive_parse_context directive_parse_context;
directive_parse_context.directive = &directive;
directive_parse_context.arguments_end = &(*ast_test)->arguments.next;
directive_parse_context.has_unnamed_argument = false;
parse_directive(context, &directive_parse_context);
if (!string_equals_with_cstring(directive, "test")) {
jcunit_fatal_error("Expected test directive, but given '%.*s'!", directive->len, directive->value);
}
release_string(directive);
directive = NULL;
}
void parse_test_epilog(struct tokenizer_context * context)
{
struct string * directive = NULL;
struct slist arguments;
slist_init(&arguments);
struct directive_parse_context directive_parse_context;
directive_parse_context.directive = &directive;
directive_parse_context.arguments_end = &arguments.next;
directive_parse_context.has_unnamed_argument = false;
parse_directive(context, &directive_parse_context);
if (!string_equals_with_cstring(directive, "endtest")) { /* unreachable error for now */
jcunit_fatal_error("Expected endtest directive, but given '%.*s'!", directive->len, directive->value);
}
if (!list_is_empty(&arguments)) {
jcunit_fatal_error("Unexpected arguments for the 'endtest' directive!");
}
release_string(directive);
directive = NULL;
}
void parse_directive_arguments(struct tokenizer_context * context, struct directive_parse_context * directive_parse_context)
{
struct token * token;
for (;;) {
skip_whitespaces(context);
token = peek_one_token(context);
if (is_token_punctuator(token, ')')) {
break;
}
parse_directive_argument(context, directive_parse_context);
token = get_one_token(context);
if (!is_token_punctuator(token, ',')) {
unget_one_token(context, token);
break;
}
release_token(token);
}
token = get_one_token(context);
if (!is_token_punctuator(token, ')')) {
jcunit_fatal_error("Expected ')' at end of directive argument!");
}
release_token(token);
}
void parse_directive_argument(struct tokenizer_context * context, struct directive_parse_context * directive_parse_context)
{
struct token * token = get_one_token(context);
if (is_token_name(token)) {
unget_one_token(context, token);
return parse_named_directive_argument(context, directive_parse_context);
}
if (!is_token_string(token)) {
jcunit_fatal_error("An unnamed argument of a directive should be a string!");
}
if (directive_parse_context->has_unnamed_argument) {
jcunit_fatal_error("A directive cannot have two unnamed arguments!");
}
struct ast_requirement_argument * argument = alloc_ast_requirement_argument();
argument->name = NULL;
argument->value = token->content.string;
slist_append(directive_parse_context->arguments_end, &argument->list_entry);
directive_parse_context->has_unnamed_argument = true;
release_token(token);
}
void parse_named_directive_argument(struct tokenizer_context * context, struct directive_parse_context * directive_parse_context)
{
struct token * token = get_one_token(context);
struct string * name = NULL;
name = token->content.string;
release_token(token);
token = get_one_token(context);
if (!is_token_punctuator(token, '=')) {
jcunit_fatal_error("Expected '=' after name of argument!");
}
release_token(token);
token = get_one_token(context);
if (!is_token_string(token)) {
jcunit_fatal_error("Expected value of the named argument as a string!");
}
struct ast_requirement_argument * argument = alloc_ast_requirement_argument();
argument->name = name;
argument->value = token->content.string;
slist_append(directive_parse_context->arguments_end, &argument->list_entry);
release_token(token);
}
void parse_directive(struct tokenizer_context * context, struct directive_parse_context * directive_parse_context)
{
struct token * token = get_one_token(context);
if (!is_token_directive(token)) {
jcunit_fatal_error("Expected a directive!");
}
*directive_parse_context->directive = token->content.string;
release_token(token);
unsigned int oldmode = context->mode;
context->mode = TOKENIZER_MODE_NONE;
int ch = peek_one_char(context);
if (ch == '(') {
token = get_one_token(context);
release_token(token);
parse_directive_arguments(context, directive_parse_context);
}
token = get_one_token(context);
if (!is_token_newline(token)) {
jcunit_fatal_error("Expected newline character after directive!");
}
release_token(token);
context->mode = oldmode;
}
void skip_whitespaces(struct tokenizer_context * context)
{
int ch = get_one_char(context);
while (is_whitespace_char(ch)) {
ch = get_one_char(context);
}
unget_one_char(context, ch);
}