-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathdoc.c
More file actions
2086 lines (1794 loc) · 48.7 KB
/
doc.c
File metadata and controls
2086 lines (1794 loc) · 48.7 KB
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 "doc.h"
#include "config.h"
#include <assert.h>
#include <err.h>
#include <float.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "libks/arena-buffer.h"
#include "libks/arena-vector.h"
#include "libks/arena.h"
#include "libks/arithmetic.h"
#include "libks/buffer.h"
#include "libks/compiler.h"
#include "libks/consistency.h"
#include "libks/list.h"
#include "libks/string.h"
#include "libks/vector.h"
#include "diff.h"
#include "lexer.h"
#include "style.h"
#include "token.h"
#include "util.h"
#define IS_DOC_INDENT_PARENS(indent) \
((indent) > 0 && ((indent) & DOC_INDENT_PARENS))
#define IS_DOC_INDENT_FORCE(indent) \
((indent) > 0 && ((indent) & DOC_INDENT_FORCE))
#define IS_DOC_INDENT_NEWLINE(indent) \
((indent) > 0 && ((indent) & DOC_INDENT_NEWLINE))
#define IS_DOC_INDENT_WIDTH(indent) \
((indent) > 0 && ((indent) & DOC_INDENT_WIDTH))
LIST(doc_list, doc);
enum doc_diff_group {
DOC_DIFF_GROUP_IGNORE,
DOC_DIFF_GROUP_NOT_COVERED,
DOC_DIFF_GROUP_COVERED,
};
struct doc_state_indent {
unsigned int cur; /* current indent */
unsigned int pre; /* last emitted indent */
};
struct doc_walk_state {
struct doc_state_indent indent;
};
struct doc {
enum doc_type dc_type;
/* allocation trace */
int dc_lno;
const char *dc_fun;
const char *dc_suffix;
/* children */
union {
struct doc_list dc_list;
struct doc *dc_doc;
struct token *dc_tk;
};
/* value */
union {
VECTOR(struct doc_minimize) dc_minimizers;
struct {
const char *dc_str;
size_t dc_len;
};
struct doc_align dc_align;
int dc_int;
};
struct arena_scope *dc_scope;
struct doc_walk_state dc_walk;
LIST_ENTRY(doc_list, doc);
};
struct doc_state {
const struct style *st_st;
struct buffer *st_bf;
struct lexer *st_lx;
struct arena *st_scratch;
const struct diffchunk *st_diff_chunks;
enum doc_mode {
BREAK,
MUNGE,
} st_mode;
struct {
const struct token *verbatim;
const struct doc *group;
int group_has_many_chunks;
int mute;
unsigned int beg;
unsigned int end;
unsigned int muteline;
} st_diff;
struct doc_state_indent st_indent;
struct {
int idx; /* index of best minimizer */
int force; /* index of minimizer with force flag */
} st_minimize;
struct {
unsigned int nfits; /* # doc_fits() invocations */
unsigned int nlines; /* # emitted lines */
unsigned int nexceeds; /* # characters exceeding column limit */
} st_stats;
unsigned int st_col;
unsigned int st_depth;
unsigned int st_refit;
unsigned int st_parens;
/* # allowed consecutive new line(s). */
unsigned int st_maxlines;
/* # consecutive emitted new line(s). */
unsigned int st_nlines;
/* Pending new line emitted on next doc_print() invocation. */
unsigned int st_newline;
/* Missed new line while muted. */
unsigned int st_muteline;
/* Optional new line(s) honored. */
int st_optline;
/* Muted, doc_print() does not emit anything. */
int st_mute;
/* Flags given to doc_exec(). */
unsigned int st_flags;
};
struct doc_state_snapshot {
struct doc_state sn_st;
struct {
char *ptr;
size_t len;
} sn_bf;
};
/*
* Tokens and line numbers extracted from a group document that covers a diff
* chunk.
*/
struct doc_diff {
/* Last verbatim token not covered by chunk. */
const struct token *dd_verbatim;
/* First token covered by chunk. */
const struct token *dd_tk;
/* Last seen token. */
unsigned int dd_threshold;
/* First token in group. */
unsigned int dd_first;
/* Return value for doc_diff_covers(). */
enum doc_diff_group dd_covers;
/* Below threshold doc_walk() return value. */
unsigned int dd_below_threshold;
};
struct doc_fits {
int fits;
unsigned int optline;
};
struct doc_walk_queue {
const struct doc *dc;
int restore;
};
enum {
DOC_WALK_BREAK = 0x00000001u,
DOC_WALK_CONTINUE = 0x00000002u,
DOC_WALK_RESTORE = 0x40000000u,
};
/*
* Description of per document type specific semantics.
*/
static const struct doc_description {
const char *name;
/* Indicates which field in child union being used. */
struct {
uint8_t many:1; /* dc_list */
uint8_t one:1; /* dc_doc */
uint8_t token:1; /* dc_tk */
} children;
/* Indicates which field in value union being used. */
struct {
uint8_t minimizers:1; /* dc_minimizers */
uint8_t integer:1; /* dc_int */
uint8_t string:1; /* dc_str/dc_len */
} value;
} doc_descriptions[] = {
[DOC_CONCAT] = {
.name = "CONCAT",
.children = { .many = 1 },
},
[DOC_GROUP] = {
.name = "GROUP",
.children = { .one = 1 },
},
[DOC_INDENT] = {
.name = "INDENT",
.children = { .one = 1 },
},
[DOC_NOINDENT] = {
.name = "NOINDENT",
.children = { .one = 1 },
},
[DOC_ALIGN] = {
.name = "ALIGN",
.value = { .integer = 1 },
},
[DOC_LITERAL] = {
.name = "LITERAL",
.children = { .token = 1 },
.value = { .string = 1 },
},
[DOC_VERBATIM] = {
.name = "VERBATIM",
.children = { .token = 1 },
.value = { .string = 1 },
},
[DOC_LINE] = {
.name = "LINE",
},
[DOC_SOFTLINE] = {
.name = "SOFTLINE",
},
[DOC_HARDLINE] = {
.name = "HARDLINE",
},
[DOC_OPTLINE] = {
.name = "OPTLINE",
},
[DOC_MUTE] = {
.name = "MUTE",
.value = { .integer = 1 },
},
[DOC_UNMUTE] = {
.name = "UNMUTE",
.value = { .integer = 1 },
},
[DOC_OPTIONAL] = {
.name = "OPTIONAL",
.children = { .one = 1 },
},
[DOC_MINIMIZE] = {
.name = "MINIMIZE",
.children = { .one = 1 },
.value = { .minimizers = 1 },
},
[DOC_SCOPE] = {
.name = "SCOPE",
.children = { .one = 1 },
},
[DOC_MAXLINES] = {
.name = "MAXLINES",
.children = { .one = 1 },
.value = { .integer = 1 },
},
};
static void doc_exec1(const struct doc *, struct doc_state *);
static void doc_exec_indent(const struct doc *, struct doc_state *,
int);
static void doc_exec_align(const struct doc *, struct doc_state *);
static void doc_exec_verbatim(const struct doc *,
struct doc_state *);
static void doc_exec_minimize(const struct doc *,
struct doc_state *);
static void doc_exec_minimize_indent(const struct doc *,
struct doc_state *);
static void doc_exec_minimize_indent1(struct doc *,
struct doc_state *, int);
static void doc_exec_mute(const struct doc *, struct doc_state *);
static void doc_exec_scope(const struct doc *, struct doc_state *);
static void doc_exec_maxlines(const struct doc *,
struct doc_state *);
static void doc_walk(const struct doc *, struct doc_state *,
unsigned int (*)(const struct doc *, struct doc_state *, void *), void *);
static int doc_fits(const struct doc *, struct doc_state *);
static unsigned int doc_fits1(const struct doc *, struct doc_state *,
void *);
static unsigned int doc_print_indent(const struct doc *,
struct doc_state *, unsigned int);
static unsigned int doc_print_indent1(const struct doc *,
struct doc_state *, unsigned int, int);
static void doc_trim_spaces(const struct doc *, struct doc_state *);
static void doc_trim_lines(const struct doc *, struct doc_state *);
static int doc_is_mute(const struct doc_state *);
static int doc_parens_align(const struct doc_state *);
static int doc_has_list(const struct doc *);
static unsigned int doc_column(struct doc_state *, const char *, size_t);
static unsigned int doc_max1(const struct doc *, struct doc_state *,
void *);
static void doc_state_init(struct doc_state *, struct doc_exec_arg *,
enum doc_mode);
static void doc_state_reset_lines(struct doc_state *);
static void doc_state_snapshot(struct doc_state_snapshot *,
const struct doc_state *, struct arena_scope *);
static void doc_state_snapshot_restore(const struct doc_state_snapshot *,
struct doc_state *);
#define DOC_DIFF(st) (((st)->st_flags & DOC_EXEC_DIFF))
static int doc_diff_group_enter(const struct doc *,
struct doc_state *, int);
static void doc_diff_group_leave(const struct doc *,
struct doc_state *, int);
static int doc_diff_group_has_many_chunks(const struct doc *,
struct doc_state *, unsigned int);
static void doc_diff_mute_enter(const struct doc *,
struct doc_state *);
static void doc_diff_mute_leave(const struct doc *,
struct doc_state *);
static void doc_diff_literal(const struct doc *,
struct doc_state *);
static unsigned int doc_diff_verbatim(const struct doc *,
struct doc_state *);
static void doc_diff_exit(const struct doc *, struct doc_state *);
static void doc_diff_emit(const struct doc *, struct doc_state *,
unsigned int, unsigned int);
static unsigned int doc_diff_covers(const struct doc *, struct doc_state *,
void *);
static int doc_diff_is_mute(const struct doc_state *);
#define doc_diff_leave(a, b, c) \
doc_diff_leave_impl((a), (b), (c), __func__)
static void doc_diff_leave_impl(const struct doc *, struct doc_state *,
unsigned int, const char *);
/* Emit indentation after new line. */
#define DOC_PRINT_INDENT 0x00000001u
/* Internal to doc_print() used to signal that a pending new line is emitted. */
#define DOC_PRINT_NEWLINE 0x00000002u
/* Emit even if muted. */
#define DOC_PRINT_FORCE 0x00000004u
static int doc_print(const struct doc *, struct doc_state *, const char *,
size_t, unsigned int);
static void doc_walk_state_snapshot(struct doc_walk_state *,
const struct doc_state *);
static void doc_walk_state_restore(const struct doc_walk_state *,
struct doc_state *);
#define doc_trace(dc, st, fmt, ...) do { \
if ((st)->st_flags & DOC_EXEC_TRACE) \
doc_trace_impl((dc), (st), (fmt), __VA_ARGS__); \
} while (0)
static void doc_trace_impl(const struct doc *, const struct doc_state *,
const char *, ...)
__attribute__((format(printf, 3, 4)));
#define doc_trace_enter(dc, st) do { \
if ((st)->st_flags & DOC_EXEC_TRACE) \
doc_trace_enter_impl((dc), (st)); \
} while (0)
static void doc_trace_enter_impl(const struct doc *, struct doc_state *);
#define doc_trace_leave(dc, st) do { \
if ((st)->st_flags & DOC_EXEC_TRACE) \
doc_trace_leave_impl((dc), (st)); \
} while (0)
static void doc_trace_leave_impl(const struct doc *, struct doc_state *);
static const char *docstr(const struct doc *, struct arena_scope *);
static const char *indentstr(const struct doc *,
const struct doc_state *, struct arena_scope *);
static const char *statestr(const struct doc_state *, unsigned int,
struct arena_scope *);
static unsigned int count_verbatim_lines(const char *, size_t,
unsigned int);
void
doc_exec(struct doc_exec_arg *arg)
{
const struct doc *dc = arg->dc;
struct doc_state st;
doc_state_init(&st, arg, BREAK);
doc_exec1(dc, &st);
if (arg->flags & DOC_EXEC_TRIM)
doc_trim_lines(dc, &st);
doc_diff_exit(dc, &st);
doc_trace(dc, &st, "%s: nfits %u", __func__, st.st_stats.nfits);
}
unsigned int
doc_width(struct doc_exec_arg *arg)
{
struct doc_state st;
doc_state_init(&st, arg, MUNGE);
doc_exec1(arg->dc, &st);
return st.st_col;
}
void
doc_remove(struct doc *dc, struct doc *parent)
{
assert(doc_has_list(parent));
LIST_REMOVE(&parent->dc_list, dc);
}
int
doc_remove_tail(struct doc *parent)
{
struct doc *dc;
assert(doc_has_list(parent));
dc = LIST_LAST(&parent->dc_list);
if (dc == NULL)
return 0;
LIST_REMOVE(&parent->dc_list, dc);
return 1;
}
void
doc_set_indent(struct doc *dc, unsigned int indent)
{
dc->dc_int = (int)indent;
}
void
doc_set_dedent(struct doc *dc, unsigned int indent)
{
dc->dc_int = -(int)indent;
}
void
doc_set_align(struct doc *dc, const struct doc_align *align)
{
dc->dc_align = *align;
}
void
doc_append(struct doc *dc, struct doc *parent)
{
if (doc_has_list(parent)) {
LIST_INSERT_TAIL(&parent->dc_list, dc);
} else {
assert(parent->dc_doc == NULL);
parent->dc_doc = dc;
}
}
void
doc_move_before(struct doc *dc, struct doc *before, struct doc *parent)
{
assert(doc_has_list(parent));
LIST_REMOVE(&parent->dc_list, dc);
LIST_INSERT_BEFORE(before, dc);
}
static void
doc_init(struct doc *dc)
{
if (doc_has_list(dc))
LIST_INIT(&dc->dc_list);
}
struct doc *
doc_root_impl(struct arena_scope *s, const char *fun, int lno)
{
struct doc *dc;
dc = arena_calloc(s, 1, sizeof(*dc));
dc->dc_type = DOC_CONCAT;
dc->dc_fun = fun;
dc->dc_lno = lno;
dc->dc_scope = s;
doc_init(dc);
return dc;
}
struct doc *
doc_alloc_impl(enum doc_type type, struct doc *parent, int val,
const char *fun, int lno)
{
struct doc *dc;
dc = arena_calloc(parent->dc_scope, 1, sizeof(*dc));
dc->dc_type = type;
dc->dc_fun = fun;
dc->dc_lno = lno;
dc->dc_int = val;
dc->dc_scope = parent->dc_scope;
doc_init(dc);
doc_append(dc, parent);
return dc;
}
struct doc *
doc_indent_impl(unsigned int val, struct doc *dc, const char *fun, int lno)
{
struct doc *indent;
indent = doc_alloc_impl(DOC_INDENT, dc, 0, fun, lno);
doc_set_indent(indent, val);
return doc_alloc_impl(DOC_CONCAT, indent, 0, fun, lno);
}
struct doc *
doc_dedent_impl(unsigned int val, struct doc *dc, const char *fun, int lno)
{
struct doc *indent;
indent = doc_alloc_impl(DOC_INDENT, dc, 0, fun, lno);
doc_set_dedent(indent, val);
return doc_alloc_impl(DOC_CONCAT, indent, 0, fun, lno);
}
static void
doc_minimize_free(void *arg)
{
struct doc *dc = arg;
VECTOR_FREE(dc->dc_minimizers);
}
struct doc *
doc_minimize_impl(const struct doc_minimize *minimizers, size_t nminimizers,
struct doc *parent, const char *fun, int lno)
{
struct doc *dc;
size_t i;
dc = doc_alloc_impl(DOC_MINIMIZE, parent, 0, fun, lno);
arena_cleanup(dc->dc_scope, doc_minimize_free, dc);
if (VECTOR_INIT(dc->dc_minimizers))
err(1, NULL);
if (VECTOR_RESERVE(dc->dc_minimizers, nminimizers))
err(1, NULL);
for (i = 0; i < nminimizers; i++) {
struct doc_minimize *dst;
dst = VECTOR_ALLOC(dc->dc_minimizers);
if (dst == NULL)
err(1, NULL);
*dst = minimizers[i];
}
return doc_alloc_impl(DOC_CONCAT, dc, 0, fun, lno);
}
struct doc *
doc_literal_impl(const char *str, struct doc *dc, const char *fun,
int lno)
{
struct doc *literal;
literal = doc_alloc_impl(DOC_LITERAL, dc, 0, fun, lno);
literal->dc_str = str;
literal->dc_len = strlen(str);
return literal;
}
static void
doc_token_free(void *arg)
{
struct doc *dc = arg;
token_rele(dc->dc_tk);
}
struct doc *
doc_token(struct token *tk, struct doc *dc, enum doc_type type,
const char *fun, int lno)
{
struct doc *token;
assert(doc_descriptions[type].children.token);
token = doc_alloc_impl(type, dc, 0, fun, lno);
token->dc_tk = tk;
token_ref(token->dc_tk);
arena_cleanup(token->dc_scope, doc_token_free, token);
token->dc_str = tk->tk_str;
token->dc_len = tk->tk_len;
return token;
}
/*
* Returns the maximum value associated with the given document.
*/
int
doc_max(const struct doc *dc, struct arena *scratch)
{
struct doc_state st;
struct doc_exec_arg arg = {
.scratch = scratch,
};
int max = 0;
doc_state_init(&st, &arg, BREAK);
doc_walk(dc, &st, doc_max1, &max);
return max;
}
void
doc_annotate(struct doc *dc, const char *suffix)
{
dc->dc_suffix = suffix;
}
static void
doc_exec1(const struct doc *dc, struct doc_state *st)
{
doc_trace_enter(dc, st);
switch (dc->dc_type) {
case DOC_CONCAT: {
struct doc *concat;
LIST_FOREACH(concat, &dc->dc_list)
doc_exec1(concat, st);
break;
}
case DOC_GROUP: {
unsigned int oldmode;
int diff;
diff = doc_diff_group_enter(dc, st, 0);
switch (st->st_mode) {
case MUNGE:
if (st->st_refit == 0) {
doc_exec1(dc->dc_doc, st);
break;
}
FALLTHROUGH;
case BREAK:
st->st_refit = 0;
oldmode = st->st_mode;
st->st_mode = doc_fits(dc, st) ? MUNGE : BREAK;
doc_exec1(dc->dc_doc, st);
st->st_mode = oldmode;
break;
}
doc_diff_group_leave(dc, st, diff);
break;
}
case DOC_INDENT:
doc_exec_indent(dc, st, dc->dc_int);
break;
case DOC_NOINDENT: {
struct doc_state_indent oldindent;
doc_trim_spaces(dc, st);
oldindent = st->st_indent;
memset(&st->st_indent, 0, sizeof(st->st_indent));
doc_exec1(dc->dc_doc, st);
st->st_indent = oldindent;
break;
}
case DOC_ALIGN:
doc_exec_align(dc, st);
break;
case DOC_LITERAL:
doc_diff_literal(dc, st);
doc_print(dc, st, dc->dc_str, dc->dc_len, DOC_PRINT_INDENT);
break;
case DOC_VERBATIM:
doc_exec_verbatim(dc, st);
break;
case DOC_LINE:
switch (st->st_mode) {
case BREAK:
doc_print(dc, st, "\n", 1, DOC_PRINT_INDENT);
break;
case MUNGE:
if (doc_print(dc, st, " ", 1, DOC_PRINT_INDENT)) {
doc_trace(dc, st, "%s: refit %u -> %d",
__func__, st->st_refit, 1);
st->st_refit = 1;
}
break;
}
break;
case DOC_SOFTLINE:
switch (st->st_mode) {
case BREAK:
doc_print(dc, st, "\n", 1, DOC_PRINT_INDENT);
break;
case MUNGE:
break;
}
break;
case DOC_HARDLINE:
doc_print(dc, st, "\n", 1, DOC_PRINT_INDENT);
break;
case DOC_OPTLINE:
/*
* Instruct the next doc_print() invocation to emit a new line.
* Necessary in order to get indentation right.
*/
if (st->st_optline)
st->st_newline = 1;
break;
case DOC_MUTE:
case DOC_UNMUTE:
doc_exec_mute(dc, st);
break;
case DOC_OPTIONAL: {
int oldoptline = st->st_optline;
st->st_optline = 1;
doc_exec1(dc->dc_doc, st);
/* Note, could already be cleared by doc_print(). */
if (st->st_optline)
st->st_optline = oldoptline;
break;
}
case DOC_MINIMIZE:
doc_exec_minimize(dc, st);
break;
case DOC_SCOPE:
doc_exec_scope(dc, st);
break;
case DOC_MAXLINES:
doc_exec_maxlines(dc, st);
break;
}
doc_trace_leave(dc, st);
}
static void
doc_exec_indent(const struct doc *dc, struct doc_state *st, int indent)
{
unsigned int oldparens = 0;
unsigned int oldindent = st->st_indent.cur;
if (IS_DOC_INDENT_PARENS(indent)) {
oldparens = st->st_parens;
if (doc_parens_align(st))
st->st_parens++;
} else if (IS_DOC_INDENT_FORCE(indent)) {
doc_print_indent(dc, st, st->st_indent.cur);
} else if (IS_DOC_INDENT_NEWLINE(indent)) {
if (st->st_stats.nlines > 0) {
st->st_indent.cur +=
(unsigned int)(indent & ~DOC_INDENT_NEWLINE);
}
} else if (IS_DOC_INDENT_WIDTH(indent)) {
if (!st->st_newline)
st->st_indent.cur = st->st_col;
} else {
if (indent > 0)
st->st_indent.cur += (unsigned int)indent;
else if (KS_u32_sub_overflow(st->st_indent.cur,
(unsigned int)-indent, &st->st_indent.cur))
st->st_indent.cur = 0;
}
doc_exec1(dc->dc_doc, st);
if (IS_DOC_INDENT_PARENS(indent)) {
st->st_parens = oldparens;
} else if (IS_DOC_INDENT_FORCE(indent)) {
/* nothing */
} else {
st->st_indent.cur = oldindent;
}
}
static void
doc_exec_align(const struct doc *dc, struct doc_state *st)
{
if (dc->dc_align.tabalign) {
unsigned int indent = dc->dc_align.indent;
while (indent > 0) {
unsigned int n;
n = doc_print_indent1(dc, st, 8, 1);
indent = n < indent ? indent - n : 0;
}
doc_print_indent1(dc, st, dc->dc_align.spaces, 0);
} else {
doc_print_indent1(dc, st,
dc->dc_align.indent + dc->dc_align.spaces, 0);
}
}
static void
doc_exec_verbatim(const struct doc *dc, struct doc_state *st)
{
struct token *tk = dc->dc_tk;
unsigned int diff, oldcol;
int isblock, isnewline;
if (doc_is_mute(st)) {
if (!(DOC_DIFF(st) && st->st_diff.verbatim == tk))
return;
}
diff = doc_diff_verbatim(dc, st);
isblock = dc->dc_len > 1 && dc->dc_str[dc->dc_len - 1] == '\n';
isnewline = dc->dc_len == 1 && dc->dc_str[0] == '\n';
/* Verbatims must never be indented. */
doc_trim_spaces(dc, st);
oldcol = st->st_col;
/* Verbatim blocks must always start on a new line. */
if (isblock && st->st_col > 0)
st->st_newline = 1;
doc_print(dc, st, dc->dc_str, dc->dc_len, 0);
/* Unmute in diff mode. */
if (DOC_DIFF(st) && st->st_diff.verbatim == tk)
st->st_diff.verbatim = NULL;
/* Restore indentation after emitting a verbatim block or new line. */
if (isblock || isnewline) {
struct doc_state_indent *it = &st->st_indent;
unsigned int indent;
if (oldcol > 0) {
/*
* The line is not empty after trimming. Assume this a
* continuation in which the current indentation level
* must be used.
*/
indent = it->cur;
} else {
/*
* The line is empty after trimming. Assume this is not
* a continuation in which the previously emitted
* indentation must be used.
*/
indent = it->pre;
}
doc_print_indent(dc, st, indent);
}
doc_diff_leave(dc, st, diff);
}
static void
doc_exec_minimize(const struct doc *dc, struct doc_state *st)
{
/* All minimizers are expected to be of the same type. */
switch (dc->dc_minimizers[0].type) {
case DOC_MINIMIZE_INDENT:
doc_exec_minimize_indent(dc, st);
break;
}
}
static void
doc_exec_minimize_indent(const struct doc *cdc, struct doc_state *st)
{
/* Ugly, must be mutable for value mutation. */
struct doc *dc = UNSAFE_CAST(struct doc *, cdc);
VECTOR(struct doc_minimize) minimizers;
struct doc_state_snapshot sn;
unsigned int nlines = 0;
unsigned int nexceeds = 0;
int best = -1;
int i, nminimizers;
double minpenality = DBL_MAX;
if (st->st_minimize.idx != -1) {
doc_exec_minimize_indent1(dc, st, st->st_minimize.idx);
return;
}
arena_scope(st->st_scratch, s);
doc_state_snapshot(&sn, st, &s);
minimizers = dc->dc_minimizers;
nminimizers = (int)VECTOR_LENGTH(minimizers);
for (i = 0; i < nminimizers; i++) {
memset(&st->st_stats, 0, sizeof(st->st_stats));
st->st_minimize.force = -1;
st->st_flags &= ~DOC_EXEC_TRACE;
st->st_minimize.idx = i;
doc_exec_minimize_indent1(dc, st, i);
st->st_minimize.idx = -1;
if (st->st_minimize.force != -1)
minimizers[i].flags |= DOC_MINIMIZE_FORCE;
if (st->st_stats.nlines > nlines)
nlines = st->st_stats.nlines;
minimizers[i].penality.nlines = st->st_stats.nlines;
if (st->st_stats.nexceeds > nexceeds)
nexceeds = st->st_stats.nexceeds;
minimizers[i].penality.nexceeds = st->st_stats.nexceeds;
doc_state_snapshot_restore(&sn, st);
}
dc->dc_minimizers = minimizers;
for (i = 0; i < nminimizers; i++) {
struct doc_minimize *mi = &dc->dc_minimizers[i];
double p = 0;
if (nlines > 0)
p += mi->penality.nlines / (double)nlines;
if (nexceeds > 0)
p += mi->penality.nexceeds / (double)nexceeds;
p /= 2;
mi->penality.sum = p;
if (mi->flags & DOC_MINIMIZE_FORCE) {
best = i;
break;
}
if (p < minpenality) {
minpenality = p;
best = i;
}
}
if (st->st_flags & DOC_EXEC_TRACE) {
for (i = 0; i < nminimizers; i++) {
const struct doc_minimize *mi = &dc->dc_minimizers[i];
const char *suffix = "";
if (mi->flags & DOC_MINIMIZE_FORCE)
suffix = ", force";
else if (i == best)
suffix = ", best";
doc_trace(dc, st, "%s: type indent, penality %.2f, "
"indent %#x, nlines %u, nexceeds %u%s",
__func__, mi->penality.sum, mi->indent,
mi->penality.nlines, mi->penality.nexceeds,
suffix);
}
}
assert(best != -1);
assert(st->st_minimize.idx == -1);
st->st_minimize.idx = best;
doc_exec_minimize_indent1(dc, st, best);
st->st_minimize.idx = -1;
}
static void
doc_exec_minimize_indent1(struct doc *dc, struct doc_state *st, int idx)
{
VECTOR(struct doc_minimize) minimizers = dc->dc_minimizers;
if (minimizers[idx].flags & DOC_MINIMIZE_FORCE)
st->st_minimize.force = idx;
doc_exec_indent(dc, st, (int)minimizers[idx].indent);
}
static void
doc_exec_mute(const struct doc *dc, struct doc_state *st)
{
/*
* While going mute, check if any new line is missed while being inside
* a diff chunk.
*/
if (st->st_mute == 0 && dc->dc_int > 0)
doc_diff_mute_enter(dc, st);
if (dc->dc_int > 0 || st->st_mute >= -dc->dc_int)
st->st_mute += dc->dc_int;
/*
* While going unmute, instruct the next doc_print() invocation to emit
* any missed new line.
*/
if (st->st_mute == 0) {