-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstyle.c
1339 lines (1156 loc) · 30.4 KB
/
style.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "style.h"
#include "config.h"
#include <sys/wait.h>
#include <assert.h>
#include <ctype.h>
#include <err.h>
#include <limits.h>
#include <regex.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "libks/arena-buffer.h"
#include "libks/arena.h"
#include "libks/arithmetic.h"
#include "libks/buffer.h"
#include "libks/compiler.h"
#include "libks/vector.h"
#include "fs.h"
#include "lexer.h"
#include "options.h"
#include "token.h"
#include "trace-types.h"
#include "trace.h"
#include "util.h"
#define style_trace(st, fmt, ...) \
trace_no_func(TRACE_STYLE, (st)->op, (fmt), __VA_ARGS__)
/*
* Return values for yaml parser routines. Only one of the following may be
* returned but disjoint values are favored allowing the caller to catch
* multiple return values.
*/
#define NONE 0x00000000
#define GOOD 0x00000001
#define SKIP 0x00000002
#define FAIL 0x00000004
#define FOR_YAML_TYPES(OP) \
OP(DocumentBegin, 1) \
OP(DocumentEnd, 2) \
OP(Colon, 3) \
OP(Sequence, 4) \
OP(String, 5) \
OP(Integer, 6) \
OP(Unknown, 7) \
/* Continuation of token types used to represent YAML primitives. */
enum yaml_type {
#define OP(type, idx) type = Last + (idx),
FOR_YAML_TYPES(OP)
#undef OP
};
struct style {
const struct options *op;
int scope;
int depth;
struct {
struct arena_scope *eternal_scope;
struct arena *scratch;
} arena;
struct {
int type;
unsigned int isset;
unsigned int val;
} options[Last];
VECTOR(struct include_category) include_categories;
};
struct include_category {
const char *pattern;
regex_t regex;
struct include_priority priority;
};
struct style_option {
int so_scope;
const char *so_key;
size_t so_len;
int so_type;
int (*so_parse)(struct style *, struct lexer *,
const struct style_option *);
int so_val[16];
};
struct yaml_token {
const struct style_option *so;
union {
int32_t i32;
uint32_t u32;
} integer;
};
static void style_free(void *);
static void style_defaults(struct style *);
static void style_set(struct style *, int, int, unsigned int);
static int style_parse_yaml(struct style *, const char *,
const struct buffer *);
static int style_parse_yaml_documents(struct style *, struct lexer *, int);
static void style_dump(const struct style *);
static void style_dump_IncludeCategories(const struct style *);
static struct token *yaml_read(struct lexer *, void *);
static struct token *yaml_read_integer(struct lexer *);
static struct token *yaml_token_alloc(struct arena_scope *,
const struct token *);
static const char *yaml_token_serialize(
const struct token *, struct arena_scope *);
static const char *yaml_token_type_serialize(int,
struct arena_scope *);
static struct token *yaml_keyword(struct lexer *,
const struct lexer_state *);
static const struct style_option *yaml_find_keyword(const char *,
size_t);
static int parse_bool(struct style *, struct lexer *,
const struct style_option *);
static int parse_enum(struct style *, struct lexer *,
const struct style_option *);
static int parse_integer(struct style *, struct lexer *,
const struct style_option *);
static int parse_integer_impl(struct style *, struct lexer *,
const struct style_option *, struct token **, struct token **);
static int parse_string(struct style *, struct lexer *,
const struct style_option *);
static int parse_nested(struct style *, struct lexer *,
const struct style_option *);
static int parse_AlignOperands(struct style *, struct lexer *,
const struct style_option *);
static int parse_BasedOnStyle(struct style *, struct lexer *,
const struct style_option *);
static int parse_ColumnLimit(struct style *, struct lexer *,
const struct style_option *);
static int parse_IncludeCategories(struct style *, struct lexer *,
const struct style_option *);
static int parse_IncludeGuards(struct style *, struct lexer *,
const struct style_option *);
static int parse_Priority(struct style *, struct lexer *,
const struct style_option *);
static int parse_Regex(struct style *, struct lexer *,
const struct style_option *);
static const char *yaml_type_str(enum yaml_type);
static struct style_option *keywords[256];
void
style_init(void)
{
static struct style_option kw[] = {
/* Top level style option. */
#define S(t) 0, #t, sizeof(#t) - 1, (t)
/* Nested style option. */
#define N(s, t) (s), #t, sizeof(#t) - 1, (t)
/* Enum style option value. */
#define E(t) 0, #t, sizeof(#t) - 1, (t), NULL, {0}
/* YAML primitive. */
#define P(t, s) 0, (s), sizeof(s) - 1, (t), NULL, {0}
{ S(AlignAfterOpenBracket), parse_enum,
{ Align, DontAlign, AlwaysBreak, BlockIndent } },
{ S(AlignEscapedNewlines), parse_enum,
{ DontAlign, Left, Right } },
{ S(AlignOperands), parse_AlignOperands,
{ Align, DontAlign, AlignAfterOperator, True, False } },
{ S(AlwaysBreakAfterReturnType), parse_enum,
{ None, All, TopLevel, AllDefinitions, TopLevelDefinitions } },
{ S(BitFieldColonSpacing), parse_enum,
{ Both, None, Before, After } },
{ S(BasedOnStyle), parse_BasedOnStyle,
{ LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU,
InheritParentConfig, OpenBSD } },
{ S(BraceWrapping), parse_nested, {0} },
{ N(BraceWrapping, AfterCaseLabel), parse_bool, {0} },
{ N(BraceWrapping, AfterClass), parse_bool, {0} },
{ N(BraceWrapping, AfterControlStatement), parse_enum,
{ Never, MultiLine, Always, True, False } },
{ N(BraceWrapping, AfterEnum), parse_bool, {0} },
{ N(BraceWrapping, AfterExternBlock), parse_bool, {0} },
{ N(BraceWrapping, AfterFunction), parse_bool, {0} },
{ N(BraceWrapping, AfterNamespace), parse_bool, {0} },
{ N(BraceWrapping, AfterObjCDeclaration), parse_bool, {0} },
{ N(BraceWrapping, AfterStruct), parse_bool, {0} },
{ N(BraceWrapping, AfterUnion), parse_bool, {0} },
{ N(BraceWrapping, BeforeCatch), parse_bool, {0} },
{ N(BraceWrapping, BeforeElse), parse_bool, {0} },
{ N(BraceWrapping, BeforeLambdaBody), parse_bool, {0} },
{ N(BraceWrapping, BeforeWhile), parse_bool, {0} },
{ N(BraceWrapping, IndentBraces), parse_bool, {0} },
{ N(BraceWrapping, SplitEmptyFunction), parse_bool, {0} },
{ N(BraceWrapping, SplitEmptyNamespace), parse_bool, {0} },
{ N(BraceWrapping, SplitEmptyRecord), parse_bool, {0} },
{ S(BreakBeforeBinaryOperators), parse_enum,
{ None, NonAssignment, All } },
{ S(BreakBeforeBraces), parse_enum,
{ Attach, Linux, Mozilla, Stroustrup, Allman, Whitesmiths,
GNU, WebKit, Custom } },
{ S(BreakBeforeTernaryOperators), parse_bool, {0} },
{ S(ColumnLimit), parse_ColumnLimit, {0} },
{ S(ContinuationIndentWidth), parse_integer, {0} },
{ S(IncludeBlocks), parse_enum,
{ Merge, Preserve, Regroup } },
{ S(IncludeCategories), parse_IncludeCategories, {0} },
{ N(IncludeCategories, CaseSensitive), parse_bool, {0} },
{ N(IncludeCategories, Priority), parse_Priority, {0} },
{ N(IncludeCategories, Regex), parse_Regex, {0} },
{ N(IncludeCategories, SortPriority), parse_Priority, {0} },
{ S(IncludeGuards), parse_IncludeGuards, {0} },
{ S(IndentWidth), parse_integer, {0} },
{ S(Language), parse_enum,
{ Cpp, CSharp, Java, JavaScript, Json, ObjC, Proto, TableGen,
TextProto, Verilog} },
{ S(SortIncludes), parse_enum,
{ Never, CaseSensitive, CaseInsensitive } },
{ S(UseTab), parse_enum,
{ Never, ForIndentation, ForContinuationAndIndentation,
AlignWithSpaces, Always } },
/* enum */
{ E(After) },
{ E(Align) },
{ E(AlignAfterOperator) },
{ E(AlignWithSpaces) },
{ E(All) },
{ E(AllDefinitions) },
{ E(Allman) },
{ E(Always) },
{ E(AlwaysBreak) },
{ E(Attach) },
{ E(Before) },
{ E(BlockIndent) },
{ E(Both) },
{ E(CSharp) },
{ E(Chromium) },
{ E(Cpp) },
{ E(Custom) },
{ E(DontAlign) },
{ E(ForContinuationAndIndentation) },
{ E(ForIndentation) },
{ E(GNU) },
{ E(Google) },
{ E(InheritParentConfig) },
{ E(Java) },
{ E(JavaScript) },
{ E(Json) },
{ E(LLVM) },
{ E(Left) },
{ E(Linux) },
{ E(Merge) },
{ E(Microsoft) },
{ E(Mozilla) },
{ E(MultiLine) },
{ E(Never) },
{ E(NonAssignment) },
{ E(None) },
{ E(ObjC) },
{ E(OpenBSD) },
{ E(Preserve) },
{ E(Proto) },
{ E(Regroup) },
{ E(Right) },
{ E(Stroustrup) },
{ E(TableGen) },
{ E(TextProto) },
{ E(TopLevel) },
{ E(TopLevelDefinitions) },
{ E(Verilog) },
{ E(WebKit) },
{ E(Whitesmiths) },
/* primitives */
{ P(Colon, ":") },
{ P(DocumentBegin, "---"), },
{ P(DocumentEnd, "..."), },
{ P(False, "false") },
{ P(Integer, "Integer") },
{ P(Sequence, "-") },
{ P(True, "true") },
#undef Y
#undef S
};
size_t nkeywords = sizeof(kw) / sizeof(kw[0]);
size_t i;
for (i = 0; i < nkeywords; i++) {
const struct style_option *src = &kw[i];
struct style_option *dst;
unsigned char slot;
slot = (unsigned char)src->so_key[0];
if (keywords[slot] == NULL) {
if (VECTOR_INIT(keywords[slot]))
err(1, NULL);
}
dst = VECTOR_ALLOC(keywords[slot]);
if (dst == NULL)
err(1, NULL);
*dst = *src;
}
}
void
style_shutdown(void)
{
size_t nslots = sizeof(keywords) / sizeof(keywords[0]);
size_t i;
for (i = 0; i < nslots; i++)
VECTOR_FREE(keywords[i]);
}
void
style_dump_keywords(struct buffer *bf)
{
size_t nslots = sizeof(keywords) / sizeof(keywords[0]);
size_t i;
for (i = 0; i < nslots; i++) {
VECTOR(struct style_option) options = keywords[i];
size_t j;
if (options == NULL)
continue;
for (j = 0; j < VECTOR_LENGTH(options); j++) {
const struct style_option *so = &options[j];
buffer_printf(bf, "\"%.*s\"\n",
(int)so->so_len, so->so_key);
}
}
}
struct style *
style_parse(const char *path, struct arena_scope *eternal_scope,
struct arena *scratch, const struct options *op)
{
struct buffer *bf = NULL;
struct style *st;
arena_scope(scratch, s);
if (path != NULL) {
bf = arena_buffer_read(&s, path);
if (bf == NULL) {
warn("%s", path);
return NULL;
}
} else {
int fd;
path = ".clang-format";
fd = searchpath(path, NULL);
if (fd != -1) {
bf = arena_buffer_read_fd(&s, fd);
close(fd);
}
}
st = style_parse_buffer(bf, path, eternal_scope, scratch, op);
if (st != NULL && options_trace_level(op, TRACE_STYLE) >= 2)
style_dump(st);
return st;
}
struct style *
style_parse_buffer(const struct buffer *bf, const char *path,
struct arena_scope *eternal_scope, struct arena *scratch,
const struct options *op)
{
struct style *st;
st = arena_calloc(eternal_scope, 1, sizeof(*st));
arena_cleanup(eternal_scope, style_free, st);
st->arena.eternal_scope = eternal_scope;
st->arena.scratch = scratch;
st->op = op;
if (VECTOR_INIT(st->include_categories))
err(1, NULL);
style_defaults(st);
if (bf == NULL) {
/*
* Only apply default style if no clang-format configuration
* file is present.
*/
style_set(st, BasedOnStyle, None, OpenBSD);
} else {
/* Errors are not considered fatal. */
(void)style_parse_yaml(st, path, bf);
}
return st;
}
static void
style_free(void *arg)
{
struct style *st = arg;
while (!VECTOR_EMPTY(st->include_categories)) {
struct include_category *ic;
ic = VECTOR_POP(st->include_categories);
regfree(&ic->regex);
}
VECTOR_FREE(st->include_categories);
}
unsigned int
style(const struct style *st, int option)
{
assert(option < Last);
return st->options[option].val;
}
int
style_brace_wrapping(const struct style *st, int option)
{
switch (st->options[BreakBeforeBraces].val) {
case Linux:
switch (option) {
case AfterFunction:
return 1;
}
break;
}
return st->options[option].val == True;
}
static int
priority_cmp(const int *aa, const int *bb)
{
int a = *aa;
int b = *bb;
if (a < b)
return -1;
if (a > b)
return 1;
return 0;
}
int *
style_include_priorities(const struct style *st)
{
VECTOR(int) priorities;
int *dst;
size_t i, n;
if (VECTOR_INIT(priorities))
err(1, NULL);
/* Add default min and max priorities. */
dst = VECTOR_ALLOC(priorities);
if (dst == NULL)
err(1, NULL);
*dst = 0;
dst = VECTOR_ALLOC(priorities);
if (dst == NULL)
err(1, NULL);
*dst = INT_MAX;
n = VECTOR_LENGTH(st->include_categories);
for (i = 0; i < n; i++) {
const struct include_category *ic = &st->include_categories[i];
dst = VECTOR_ALLOC(priorities);
if (dst == NULL)
err(1, NULL);
*dst = ic->priority.group;
}
VECTOR_SORT(priorities, priority_cmp);
return priorities;
}
struct include_priority
style_include_priority(const struct style *st, const char *include_path)
{
size_t i, n;
n = VECTOR_LENGTH(st->include_categories);
for (i = 0; i < n; i++) {
const struct include_category *ic = &st->include_categories[i];
int error;
error = regexec(&ic->regex, include_path, 0, NULL, 0);
if (error == 0)
return ic->priority;
}
return (struct include_priority){.group = INT_MAX, .sort = INT_MAX};
}
static void
style_defaults(struct style *st)
{
static const struct {
unsigned int key;
unsigned int val;
} defaults[] = {
{ AlignAfterOpenBracket, DontAlign },
{ AlignEscapedNewlines, Right },
{ AlignOperands, DontAlign },
{ AlwaysBreakAfterReturnType, AllDefinitions },
{ BitFieldColonSpacing, None },
{ BreakBeforeBinaryOperators, None },
{ BreakBeforeBraces, Linux },
{ BreakBeforeTernaryOperators, False },
{ ColumnLimit, 80 },
{ ContinuationIndentWidth, 4 },
{ IncludeBlocks, Preserve },
{ IndentWidth, 8 },
{ SortIncludes, Never },
{ UseTab, Always },
};
size_t ndefaults = sizeof(defaults) / sizeof(defaults[0]);
size_t i;
for (i = 0; i < ndefaults; i++)
st->options[defaults[i].key].val = defaults[i].val;
}
static void
style_set(struct style *st, int option, int type, unsigned int val)
{
st->options[option].type = type;
st->options[option].isset = 1;
st->options[option].val = val;
}
static int
style_parse_yaml(struct style *st, const char *path, const struct buffer *bf)
{
struct lexer *lx;
int error = 0;
lx = lexer_tokenize(&(const struct lexer_arg){
.path = path,
.bf = bf,
.op = st->op,
.error_flush = options_trace_level(st->op, TRACE_STYLE) > 0,
.arena = {
.eternal_scope = st->arena.eternal_scope,
.scratch = st->arena.scratch,
},
.callbacks = {
.read = yaml_read,
.alloc = yaml_token_alloc,
.serialize_token = yaml_token_serialize,
.serialize_token_type = yaml_token_type_serialize,
.arg = st,
},
});
if (lx == NULL) {
error = 1;
goto out;
}
error = style_parse_yaml_documents(st, lx, 0);
out:
return error;
}
static enum style_keyword
yaml_document_language(struct lexer *lx)
{
struct lexer_state s;
struct token *tk;
enum style_keyword language = None;
lexer_peek_enter(lx, &s);
do {
if (lexer_if(lx, LEXER_EOF, NULL))
break;
if (lexer_if(lx, DocumentBegin, NULL))
break;
if (lexer_if(lx, Language, NULL) &&
lexer_if(lx, Colon, NULL) &&
lexer_pop(lx, &tk)) {
language = (enum style_keyword)tk->tk_type;
break;
}
} while (lexer_pop(lx, &tk));
lexer_peek_leave(lx, &s);
return language;
}
static void
skip_document(struct lexer *lx)
{
struct token *tk;
do {
if (lexer_if(lx, LEXER_EOF, NULL) ||
lexer_if(lx, DocumentBegin, NULL))
break;
} while (lexer_pop(lx, &tk));
}
static int
style_parse_yaml_documents(struct style *st, struct lexer *lx, int nested)
{
int error;
for (;;) {
const struct style_option *so;
struct token *key;
error = 0;
if (lexer_if(lx, LEXER_EOF, NULL))
break;
if (!nested) {
enum style_keyword language;
/*
* Only honor documents applicable to all languages or
* C++ which is applicable to C as well according to
* clang-format.
*/
language = yaml_document_language(lx);
if (language != None && language != Cpp) {
skip_document(lx);
continue;
}
}
if (lexer_peek_if(lx, Sequence, NULL))
break;
if (lexer_if(lx, DocumentBegin, NULL))
continue;
if (lexer_if(lx, DocumentEnd, NULL))
continue;
if (!lexer_peek(lx, &key)) {
error = FAIL;
break;
}
so = token_priv(key, struct yaml_token)->so;
if (so != NULL && so->so_scope != st->scope)
break;
if (so != NULL)
error = so->so_parse(st, lx, so);
if (error & (GOOD | SKIP)) {
continue;
} else if (error & FAIL) {
break;
} else {
struct token *val;
/* Best effort, try to continue parsing. */
(void)lexer_pop(lx, &key);
(void)lexer_if(lx, Colon, NULL);
if (lexer_peek_if(lx, Sequence, NULL)) {
/* Ignore sequences. */
while (lexer_if(lx, Sequence, NULL) &&
lexer_pop(lx, &val))
continue;
} else {
(void)lexer_pop(lx, &val);
}
lexer_error(lx, key, __func__, __LINE__,
"unknown option %s", lexer_serialize(lx, key));
}
}
return error;
}
static void
style_dump(const struct style *st)
{
size_t noptions = sizeof(st->options) / sizeof(st->options[0]);
size_t i;
int scope = 0;
for (i = First; i < noptions; i++) {
const struct style_option *so;
const char *key;
if (!st->options[i].isset)
continue;
key = style_keyword_str(i);
so = yaml_find_keyword(key, strlen(key));
if (so->so_scope != scope && so->so_scope != 0) {
fprintf(stderr, "[s] %s:\n",
style_keyword_str(
(enum style_keyword)so->so_scope));
}
scope = so->so_scope;
fprintf(stderr, "[s] ");
if (scope != 0)
fprintf(stderr, " ");
fprintf(stderr, "%s: ", key);
if (so->so_type == IncludeCategories) {
style_dump_IncludeCategories(st);
} else {
switch (st->options[i].type) {
case Integer:
fprintf(stderr, "%u", st->options[i].val);
break;
default:
fprintf(stderr, "%s",
style_keyword_str(st->options[i].val));
break;
}
fprintf(stderr, "\n");
}
}
}
static void
style_dump_IncludeCategories(const struct style *st)
{
size_t i;
fprintf(stderr, "\n");
for (i = 0; i < VECTOR_LENGTH(st->include_categories); i++) {
const struct include_category *ic = &st->include_categories[i];
fprintf(stderr, "[s] - Regex: '%s'\n", ic->pattern);
fprintf(stderr, "[s] Priority: %d\n", ic->priority.group);
fprintf(stderr, "[s] SortPriority: %d\n", ic->priority.sort);
}
}
static struct token *
yaml_read(struct lexer *lx, void *UNUSED(arg))
{
struct lexer_state s;
struct token *tk;
unsigned char ch;
again:
lexer_eat_lines_and_spaces(lx, NULL);
tk = yaml_read_integer(lx);
if (tk != NULL)
return tk;
s = lexer_get_state(lx);
if (lexer_getc(lx, &ch))
goto eof;
if (ch == '#') {
for (;;) {
if (lexer_getc(lx, &ch))
goto eof;
if (ch == '\n')
break;
}
goto again;
}
if (isalpha(ch) || ch == '_') {
do {
if (lexer_getc(lx, &ch))
break;
} while (!isspace(ch) && ch != ':');
lexer_ungetc(lx);
return yaml_keyword(lx, &s);
}
if (ch == '-' || ch == '.' || ch == ':') {
unsigned char needle = ch;
do {
if (lexer_getc(lx, &ch))
goto eof;
} while (ch == needle);
lexer_ungetc(lx);
return yaml_keyword(lx, &s);
}
if (ch == '\'') {
s = lexer_get_state(lx); /* discard '\'' */
for (;;) {
if (lexer_getc(lx, &ch))
goto eof;
if (ch == '\'')
break;
}
lexer_ungetc(lx);
tk = yaml_keyword(lx, &s);
if (tk->tk_type == Unknown) {
token_rele(tk);
tk = NULL;
}
if (tk == NULL)
tk = lexer_emit(lx, &s, String);
lexer_getc(lx, &ch); /* discard '\'' */
return tk;
}
tk = lexer_emit(lx, &s, Unknown);
lexer_error(lx, tk, __func__, __LINE__,
"unknown token %s", lexer_serialize(lx, tk));
token_rele(tk);
return NULL;
eof:
return lexer_emit(lx, &s, LEXER_EOF);
}
static struct token *
yaml_read_integer(struct lexer *lx)
{
struct lexer_state s;
struct token *tk;
int32_t integer = 0;
int overflow = 0;
int peek = 0;
int sign = 1;
int string = 0;
unsigned char ch;
s = lexer_get_state(lx);
if (lexer_getc(lx, &ch))
return NULL;
if (ch == '\'') {
if (lexer_getc(lx, &ch))
return NULL;
string = 1;
}
if (isdigit(ch)) {
peek = 1;
} else if (ch == '-' && lexer_getc(lx, &ch) == 0) {
sign = -1;
peek = isdigit(ch);
if (!peek)
lexer_ungetc(lx);
}
if (!peek) {
lexer_ungetc(lx);
if (string)
lexer_ungetc(lx);
return NULL;
}
while (isdigit(ch)) {
int x = ch - '0';
if (KS_i32_mul_overflow(integer, 10, &integer) ||
KS_i32_add_overflow(integer, x, &integer))
overflow = 1;
if (lexer_getc(lx, &ch))
return NULL;
}
if (!string)
lexer_ungetc(lx);
if (KS_i32_mul_overflow(integer, sign, &integer))
overflow = 1;
tk = lexer_emit(lx, &s, Integer);
token_priv(tk, struct yaml_token)->integer.i32 = integer;
if (overflow) {
lexer_error(lx, tk, __func__, __LINE__,
"integer %s too large", lexer_serialize(lx, tk));
}
return tk;
}
static struct token *
yaml_token_alloc(struct arena_scope *s, const struct token *def)
{
return token_alloc(s, sizeof(struct yaml_token), def);
}
static const char *
yaml_token_serialize(const struct token *tk, struct arena_scope *s)
{
struct buffer *bf;
bf = arena_buffer_alloc(s, 128);
buffer_printf(bf, "%s", yaml_token_type_serialize(tk->tk_type, s));
if (tk->tk_str != NULL) {
buffer_printf(bf, "<%u:%u>(\"", tk->tk_lno, tk->tk_cno);
strnice_buffer(bf, tk->tk_str, tk->tk_len);
buffer_printf(bf, "\")");
}
return buffer_str(bf);
}
static const char *
yaml_token_type_serialize(int type, struct arena_scope *s)
{
struct buffer *bf;
bf = arena_buffer_alloc(s, 128);
if (type < Last)
buffer_printf(bf, "Keyword");
else
buffer_printf(bf, "%s", yaml_type_str((enum yaml_type)type));
return buffer_str(bf);
}
static struct token *
yaml_keyword(struct lexer *lx, const struct lexer_state *st)
{
const struct style_option *so;
struct token *tk;
const char *buf;
size_t buflen;
buf = lexer_buffer_slice(lx, st, &buflen);
if (buf == NULL)
goto unknown;
so = yaml_find_keyword(buf, buflen);
if (so == NULL)
goto unknown;
tk = lexer_emit(lx, st, so->so_type);
if (so->so_parse != NULL)
token_priv(tk, struct yaml_token)->so = so;
return tk;
unknown:
return lexer_emit(lx, st, Unknown);
}
static const struct style_option *
yaml_find_keyword(const char *str, size_t len)
{
size_t i;
unsigned char slot;
slot = (unsigned char)str[0];
if (keywords[slot] == NULL)
return NULL;
for (i = 0; i < VECTOR_LENGTH(keywords[slot]); i++) {
struct style_option *so = &keywords[slot][i];
if (len == so->so_len && strncmp(so->so_key, str, len) == 0)
return so;
}
return NULL;
}
static int
parse_bool(struct style *st, struct lexer *lx, const struct style_option *so)
{
struct token *key, *val;
if (!lexer_if(lx, so->so_type, &key))
return NONE;
if (!lexer_expect(lx, Colon, NULL))
return FAIL;
if (!lexer_if(lx, True, &val) && !lexer_if(lx, False, &val)) {
(void)lexer_pop(lx, &val);
lexer_error(lx, val, __func__, __LINE__,
"unknown value %s for option %s",
lexer_serialize(lx, val), lexer_serialize(lx, key));
return SKIP;
}
style_set(st, key->tk_type, None, (unsigned int)val->tk_type);
return GOOD;
}
static int
parse_enum(struct style *st, struct lexer *lx, const struct style_option *so)
{
struct token *key, *val;
const int *v;