-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser-decl.c
564 lines (483 loc) · 14.6 KB
/
parser-decl.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#include "parser-decl.h"
#include "config.h"
#include "libks/arena.h"
#include "doc.h"
#include "expr.h"
#include "lexer.h"
#include "parser-attributes.h"
#include "parser-braces.h"
#include "parser-cpp.h"
#include "parser-expr.h"
#include "parser-func.h"
#include "parser-priv.h"
#include "parser-stmt-asm.h"
#include "parser-type.h"
#include "ruler.h"
#include "simple-decl-forward.h"
#include "simple-decl.h"
#include "simple.h"
#include "style.h"
#include "token.h"
struct parser_decl_init_arg {
struct doc *dc;
struct ruler *rl;
const struct token *semi;
unsigned int indent;
unsigned int flags;
/* Insert space before equal operator. */
#define PARSER_DECL_INIT_SPACE_BEFORE_EQUAL 0x00000001u
};
static int parser_decl1(struct parser *, struct doc *, unsigned int);
static int parser_decl2(struct parser *, struct doc *, struct ruler *,
unsigned int);
static int parser_decl_init(struct parser *, struct doc **,
struct parser_decl_init_arg *);
static int parser_decl_init1(struct parser *, struct doc *, struct doc **);
static int parser_decl_init_assign(struct parser *, struct doc *,
struct doc **, struct parser_decl_init_arg *);
static int parser_decl_bitfield(struct parser *, struct doc *);
static int parser_decl_braces(struct parser *, struct doc *, int);
static int parser_simple_decl_enter(struct parser *, unsigned int,
struct simple_cookie *);
static int parser_simple_decl_forward_enter(struct parser *, unsigned int,
struct simple_cookie *);
int
parser_decl_peek(struct parser *pr)
{
struct lexer_state s;
struct lexer *lx = pr->pr_lx;
struct doc *dc;
int error, simple;
arena_scope(pr->pr_arena.doc, doc_scope);
parser_arena_scope(&pr->pr_arena.doc_scope, &doc_scope, cookie);
dc = doc_root(&doc_scope);
lexer_peek_enter(lx, &s);
simple = simple_disable(pr->pr_si);
error = parser_decl(pr, dc, 0);
simple_enable(pr->pr_si, simple);
lexer_peek_leave(lx, &s);
return error & GOOD;
}
int
parser_decl(struct parser *pr, struct doc *dc, unsigned int flags)
{
int error;
arena_scope(pr->pr_arena.scratch, scratch_scope);
parser_arena_scope(&pr->pr_arena.scratch_scope, &scratch_scope, cookie);
simple_cookie(simple_decl_cookie);
error = parser_simple_decl_enter(pr, flags, &simple_decl_cookie);
if (error & HALT)
return error;
simple_cookie(simple_decl_forward_cookie);
error = parser_simple_decl_forward_enter(pr, flags,
&simple_decl_forward_cookie);
if (error & HALT)
return error;
return parser_decl1(pr, dc, flags);
}
static int
parser_decl1(struct parser *pr, struct doc *dc, unsigned int flags)
{
struct ruler rl;
struct doc *line = NULL;
struct doc *decl;
struct lexer *lx = pr->pr_lx;
void *cookie;
int ndecl = 0;
int error;
decl = doc_alloc(DOC_CONCAT, dc);
ruler_init(&rl, 0, RULER_ALIGN_SENSE);
cookie = parser_cpp_decl_enter(pr);
for (;;) {
struct token *tk;
if (ndecl > 0)
line = doc_alloc(DOC_HARDLINE, decl);
error = parser_decl2(pr, decl, &rl, flags);
if (error & (FAIL | NONE)) {
if (line != NULL)
doc_remove(line, decl);
break;
}
ndecl++;
if (error & BRCH)
break;
if (flags & PARSER_DECL_BREAK) {
/*
* Honor empty line(s) which denotes the end of this
* block of declarations.
*/
if (lexer_back(lx, &tk) && token_has_line(tk, 2))
break;
/*
* Any cpp directive also denotes the end of this block
* of declarations.
*/
if (lexer_peek_if_prefix_flags(lx, TOKEN_FLAG_CPP,
NULL))
break;
}
}
parser_cpp_decl_leave(pr, cookie);
if (ndecl == 0)
doc_remove(decl, dc);
else if ((error & FAIL) == 0)
ruler_exec(&rl);
ruler_free(&rl);
if (ndecl == 0)
return parser_none(pr);
if (flags & PARSER_DECL_LINE)
doc_alloc(DOC_HARDLINE, decl);
return parser_good(pr);
}
static int
parser_decl2(struct parser *pr, struct doc *dc, struct ruler *rl,
unsigned int flags)
{
struct parser_type type = {0};
struct lexer *lx = pr->pr_lx;
struct doc *out = NULL;
struct doc *concat;
struct token *beg, *end, *semi;
int iscpp = 0;
int error;
if (parser_cpp_cdefs(pr, dc) & GOOD)
return parser_good(pr);
if ((flags & PARSER_DECL_ROOT) && (parser_cpp_decl_root(pr, dc) & GOOD))
return parser_good(pr);
if (!parser_type_peek(pr, &type, 0)) {
iscpp = parser_cpp_peek_decl(pr, &type,
(flags & PARSER_DECL_ROOT) ? PARSER_CPP_DECL_ROOT : 0);
if (!iscpp)
return parser_cpp_x(pr, dc);
}
/*
* Presence of a type does not necessarily imply that this a declaration
* since it could be a function declaration or implementation.
*/
switch (parser_func_peek(pr)) {
case PARSER_FUNC_PEEK_NONE:
break;
case PARSER_FUNC_PEEK_DECL:
return parser_func_decl(pr, dc, rl);
case PARSER_FUNC_PEEK_IMPL:
return parser_none(pr);
}
concat = doc_alloc(DOC_CONCAT, doc_alloc(DOC_GROUP, dc));
if (!lexer_peek(lx, &beg))
return parser_fail(pr);
end = type.end;
if (is_simple_enabled(pr->pr_si, SIMPLE_DECL))
simple_decl_type(pr->pr_simple.decl, beg, end);
if (parser_type(pr, concat, &type, rl) & (FAIL | NONE))
return parser_fail(pr);
/* Presence of semicolon implies that this declaration is done. */
if (lexer_peek_if(lx, TOKEN_SEMI, NULL))
goto out;
if (token_is_decl(end, TOKEN_STRUCT)) {
error = parser_decl_braces(pr, concat,
style_brace_wrapping(pr->pr_st, AfterStruct));
if (error & FAIL)
return parser_fail(pr);
} else if (token_is_decl(end, TOKEN_UNION)) {
error = parser_decl_braces(pr, concat,
style_brace_wrapping(pr->pr_st, AfterUnion));
if (error & FAIL)
return parser_fail(pr);
} else if (token_is_decl(end, TOKEN_ENUM)) {
struct token *lbrace, *rbrace;
unsigned int w;
if (!lexer_peek_if_pair(lx, TOKEN_LBRACE, TOKEN_RBRACE,
&lbrace, &rbrace))
return parser_fail(pr);
parser_token_trim_before(pr, rbrace);
if (style_brace_wrapping(pr->pr_st, AfterEnum))
doc_alloc(DOC_HARDLINE, concat);
w = style(pr->pr_st, IndentWidth);
error = parser_braces(pr, concat, concat, w,
PARSER_BRACES_ENUM | PARSER_BRACES_TRIM);
if (error & HALT)
return parser_fail(pr);
}
if (!lexer_peek_until(lx, TOKEN_SEMI, &semi))
return parser_fail(pr);
struct parser_decl_init_arg arg = {
.dc = concat,
.rl = rl,
.semi = semi,
.indent = style(pr->pr_st, IndentWidth),
.flags = iscpp && lexer_peek_if(lx, TOKEN_EQUAL, NULL) ? 0 :
PARSER_DECL_INIT_SPACE_BEFORE_EQUAL,
};
error = parser_decl_init(pr, &out, &arg);
if (error & (FAIL | NONE))
return parser_fail(pr);
if (out != NULL)
concat = out;
out:
if (lexer_expect(lx, TOKEN_SEMI, &semi)) {
if (flags & PARSER_DECL_TRIM_SEMI) {
struct token *nx;
while (lexer_if(lx, TOKEN_SEMI, &nx))
lexer_remove(lx, nx);
}
parser_doc_token(pr, semi, concat);
if (is_simple_enabled(pr->pr_si, SIMPLE_DECL))
simple_decl_semi(pr->pr_simple.decl, semi);
if (is_simple_enabled(pr->pr_si, SIMPLE_DECL_FORWARD)) {
simple_decl_forward(pr->pr_simple.decl_forward, beg,
semi);
}
}
return parser_good(pr);
}
/*
* Parse any initialization as part of a declaration.
*/
static int
parser_decl_init(struct parser *pr, struct doc **out,
struct parser_decl_init_arg *arg)
{
struct doc *concat, *dc, *indent;
struct lexer *lx = pr->pr_lx;
struct ruler_indent *cookie = NULL;
int ninit = 0;
indent = ruler_indent(arg->rl, arg->dc, &cookie);
dc = doc_alloc(DOC_CONCAT, indent);
concat = doc_alloc(DOC_CONCAT, doc_alloc(DOC_GROUP, dc));
for (;;) {
struct token *comma, *semi;
int error;
if (lexer_peek_if(lx, TOKEN_SEMI, &semi) && semi == arg->semi)
break;
if (lexer_if(lx, TOKEN_COMMA, &comma)) {
parser_doc_token(pr, comma, concat);
doc_alloc(
token_has_line(comma, 1) ? DOC_HARDLINE : DOC_LINE,
concat);
if (is_simple_enabled(pr->pr_si, SIMPLE_DECL))
simple_decl_comma(pr->pr_simple.decl, comma);
/* Break before the argument. */
concat = doc_alloc(DOC_CONCAT,
doc_alloc(DOC_GROUP, dc));
doc_alloc(DOC_SOFTLINE, concat);
concat = doc_alloc(DOC_CONCAT,
doc_alloc(DOC_GROUP, concat));
}
error = parser_decl_init1(pr, concat, out);
if (error & NONE)
error = parser_decl_init_assign(pr, concat, out, arg);
if (error & HALT)
break;
ninit++;
}
if (ninit == 0) {
ruler_indent_remove(arg->rl, cookie);
doc_remove(indent, arg->dc);
}
return parser_good(pr);
}
static int
parser_decl_peek_parens_or_squares(struct parser *pr, struct token **lhs)
{
struct lexer *lx = pr->pr_lx;
if (lexer_peek_if_pair(lx, TOKEN_LSQUARE, TOKEN_RSQUARE, lhs, NULL))
return 1;
if (lexer_peek_if_pair(lx, TOKEN_LPAREN, TOKEN_RPAREN, lhs, NULL))
return 1;
return 0;
}
static int
parser_decl_init1(struct parser *pr, struct doc *dc, struct doc **out)
{
struct lexer *lx = pr->pr_lx;
struct token *lhs, *tk;
if (lexer_if(lx, TOKEN_IDENT, &tk)) {
parser_doc_token(pr, tk, dc);
if (lexer_peek_if(lx, TOKEN_IDENT, NULL))
doc_literal(" ", dc);
*out = dc;
return parser_good(pr);
} else if (parser_decl_peek_parens_or_squares(pr, &lhs)) {
struct doc *expr = NULL;
int rhs = lhs->tk_type == TOKEN_LSQUARE ?
TOKEN_RSQUARE : TOKEN_RPAREN;
int error;
if (lexer_expect(lx, lhs->tk_type, &lhs))
parser_doc_token(pr, lhs, dc);
/* Let the remaning tokens hang of the expression. */
error = parser_expr(pr, &expr, &(struct parser_expr_arg){
.dc = dc,
});
if (error & HALT)
expr = dc;
if (lexer_expect(lx, rhs, &tk))
parser_doc_token(pr, tk, expr);
if (lexer_peek_if(lx, TOKEN_IDENT, NULL))
doc_literal(" ", dc);
return parser_good(pr);
} else if (parser_decl_bitfield(pr, dc) & GOOD) {
return parser_good(pr);
} else if (lexer_if_flags(lx,
TOKEN_FLAG_QUALIFIER | TOKEN_FLAG_STORAGE, &tk)) {
parser_doc_token(pr, tk, dc);
doc_literal(" ", dc);
return parser_good(pr);
} else if (lexer_if(lx, TOKEN_STAR, &tk)) {
parser_doc_token(pr, tk, dc);
return parser_good(pr);
} else if (parser_attributes(pr, dc, out,
PARSER_ATTRIBUTES_LINE) & GOOD) {
if (!lexer_peek_if(lx, TOKEN_SEMI, NULL) &&
!lexer_peek_if(lx, TOKEN_EQUAL, NULL))
doc_literal(" ", dc);
return parser_good(pr);
} else if (parser_asm_peek(pr)) {
doc_alloc(DOC_LINE, dc);
return parser_decl_asm(pr, dc);
}
return parser_none(pr);
}
static int
parser_decl_init_assign(struct parser *pr, struct doc *dc, struct doc **out,
struct parser_decl_init_arg *arg)
{
struct doc *dedent;
struct lexer *lx = pr->pr_lx;
struct token *equal;
int error;
if (!lexer_if(lx, TOKEN_EQUAL, &equal))
return parser_none(pr);
if (arg->flags & PARSER_DECL_INIT_SPACE_BEFORE_EQUAL)
doc_literal(" ", dc);
parser_doc_token(pr, equal, dc);
doc_literal(" ", dc);
dedent = doc_alloc(DOC_CONCAT, ruler_dedent(arg->rl, dc, NULL));
if (lexer_peek_if(lx, TOKEN_LBRACE, NULL)) {
error = parser_braces(pr, arg->dc, dedent, arg->indent, 0);
if (error & (FAIL | NONE))
return parser_fail(pr);
*out = dedent;
} else {
struct token *pv, *stop;
unsigned int expr_flags = 0;
/* Never break before the assignment operator. */
if ((pv = token_prev(equal)) != NULL &&
token_has_line(pv, 1))
token_move_suffixes(pv, equal);
/*
* Honor hard line after assignment operator, must be emitted
* inside the expression document to get indentation right.
*/
if (token_has_line(equal, 1))
expr_flags |= EXPR_EXEC_HARDLINE;
lexer_peek_until_comma(lx, arg->semi, &stop);
error = parser_expr(pr, out, &(struct parser_expr_arg){
.dc = dedent,
.stop = stop,
.indent = style(pr->pr_st, ContinuationIndentWidth),
.flags = expr_flags,
});
if (error & HALT)
return parser_fail(pr);
}
return parser_good(pr);
}
static int
parser_decl_bitfield(struct parser *pr, struct doc *dc)
{
struct lexer *lx = pr->pr_lx;
struct token *colon, *size;
unsigned int dospace = style(pr->pr_st, BitFieldColonSpacing);
if (!lexer_if(lx, TOKEN_COLON, &colon))
return parser_none(pr);
if (dospace == Both || dospace == Before)
doc_literal(" ", dc);
parser_doc_token(pr, colon, dc);
if (dospace == Both || dospace == After)
doc_literal(" ", dc);
if (lexer_expect(lx, TOKEN_LITERAL, &size))
parser_doc_token(pr, size, dc);
return parser_good(pr);
}
static int
parser_decl_braces(struct parser *pr, struct doc *dc, int break_before_braces)
{
struct lexer *lx = pr->pr_lx;
struct doc *indent;
struct token *lbrace, *rbrace;
if (!lexer_peek_if_pair(lx, TOKEN_LBRACE, TOKEN_RBRACE,
&lbrace, &rbrace))
return parser_fail(pr);
parser_token_trim_before(pr, rbrace);
if (lexer_expect(lx, TOKEN_LBRACE, NULL)) {
parser_token_trim_after(pr, lbrace);
if (break_before_braces)
doc_alloc(DOC_HARDLINE, dc);
parser_doc_token(pr, lbrace, dc);
}
indent = doc_indent(style(pr->pr_st, IndentWidth), dc);
doc_alloc(DOC_HARDLINE, indent);
if (parser_decl(pr, indent, 0) & FAIL)
return parser_fail(pr);
doc_alloc(DOC_HARDLINE, dc);
if (lexer_expect(lx, TOKEN_RBRACE, &rbrace))
parser_doc_token(pr, rbrace, dc);
if (!lexer_peek_if(lx, TOKEN_SEMI, NULL) &&
!lexer_peek_if(lx, TOKEN_ATTRIBUTE, NULL))
doc_literal(" ", dc);
return parser_good(pr);
}
static int
parser_simple_decl_enter(struct parser *pr, unsigned int flags,
struct simple_cookie *simple)
{
struct lexer_state s;
struct lexer *lx = pr->pr_lx;
struct doc *dc;
unsigned int simple_flags;
int error;
simple_flags = (flags & PARSER_DECL_SIMPLE) ? 0 : SIMPLE_IGNORE;
if (!simple_enter(pr->pr_si, SIMPLE_DECL, simple_flags, simple))
return parser_good(pr);
arena_scope(pr->pr_arena.doc, doc_scope);
parser_arena_scope(&pr->pr_arena.doc_scope, &doc_scope, cookie);
pr->pr_simple.decl = simple_decl_enter(lx, pr->pr_arena.scratch_scope,
pr->pr_op);
dc = doc_root(&doc_scope);
lexer_peek_enter(lx, &s);
error = parser_decl1(pr, dc, flags);
lexer_peek_leave(lx, &s);
if (error & GOOD)
simple_decl_leave(pr->pr_simple.decl);
simple_decl_free(pr->pr_simple.decl);
pr->pr_simple.decl = NULL;
simple_leave(simple);
return parser_good(pr);
}
static int
parser_simple_decl_forward_enter(struct parser *pr, unsigned int flags,
struct simple_cookie *simple)
{
struct lexer_state s;
struct lexer *lx = pr->pr_lx;
struct doc *dc;
unsigned int simple_flags;
int error;
simple_flags = (flags & PARSER_DECL_SIMPLE_FORWARD) ? 0 : SIMPLE_IGNORE;
if (!simple_enter(pr->pr_si, SIMPLE_DECL_FORWARD, simple_flags, simple))
return parser_good(pr);
arena_scope(pr->pr_arena.doc, doc_scope);
parser_arena_scope(&pr->pr_arena.doc_scope, &doc_scope, cookie);
pr->pr_simple.decl_forward = simple_decl_forward_enter(lx,
pr->pr_arena.scratch_scope, pr->pr_op);
dc = doc_root(&doc_scope);
lexer_peek_enter(lx, &s);
error = parser_decl1(pr, dc, flags);
lexer_peek_leave(lx, &s);
if (error & GOOD)
simple_decl_forward_leave(pr->pr_simple.decl_forward);
simple_decl_forward_free(pr->pr_simple.decl_forward);
pr->pr_simple.decl_forward = NULL;
simple_leave(simple);
return parser_good(pr);
}