-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhx.cpp
5301 lines (3768 loc) · 82.6 KB
/
hx.cpp
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
#line 38 "index.md"
#line 163 "index.md"
#line 36 "read.md"
#include <string>
#line 176 "read.md"
#include <fstream>
#line 94 "blocks.md"
#include <vector>
#line 9 "log.md"
#include <iostream>
#include <exception>
#line 231 "frag.md"
#include <vector>
#line 680 "frag.md"
#include <sstream>
#line 5 "input.md"
#include <iostream>
#include <vector>
#include <filesystem>
#line 524 "index.md"
#include <algorithm>
#line 947 "index.md"
#include <functional>
#include <sstream>
#line 81 "html.md"
#include <string>
#line 707 "html.md"
#include <cctype>
#line 748 "html.md"
#include <set>
#line 108 "line.md"
#include <limits>
#line 5 "ncurses.md"
#if defined HAVE_CONFIG_H
#include "config.h"
#endif
#line 14 "ncurses.md"
#if defined HAVE_NCURSESW_CURSES_H
#include <ncursesw/curses.h>
#elif defined HAVE_NCURSESW_H
#include <ncursesw.h>
#elif defined HAVE_NCURSES_CURSES_H
#include <ncurses/curses.h>
#elif defined HAVE_NCURSES_H
#include <ncurses.h>
#elif defined HAVE_CURSES_H
#include <curses.h>
#endif
#line 31 "ncurses.md"
#include <locale.h>
#line 164 "index.md"
#line 175 "index.md"
class Frag;
class Frag_Ref;
Frag *find_frag(const std::string &path, const std::string &key,
bool local, std::string *got_path = nullptr
);
Frag *find_frag(const Frag_Ref &ref, std::string *got_path = nullptr);
Frag &get_frag(const std::string &path,
const std::string &key, bool local
);
Frag &get_frag(const Frag_Ref &ref);
#include <map>
using Frag_Map = std::map<std::string, Frag>;
Frag_Map &frag_map(const std::string &in);
Frag_Map &frag_map();
void split_frag(const std::string &name, Frag *meta,
std::map<std::string, std::string> &&values
);
void clear_frags();
void eval_metas();
#line 5 "read.md"
#line 20 "read.md"
#line 62 "read.md"
struct No_More_Lines {};
#line 147 "read.md"
#line 161 "read.md"
#line 7 "blocks.md"
#line 14 "blocks.md"
enum class Read_State {
new_element
#line 67 "blocks.md"
,
header
#line 149 "blocks.md"
,
code,
after_code
#line 199 "blocks.md"
,
notes
#line 257 "blocks.md"
,
para
#line 311 "blocks.md"
,
img
#line 17 "blocks.md"
};
#line 26 "blocks.md"
using RS = Read_State;
#line 101 "blocks.md"
struct Block {
Read_State state;
std::vector<std::string> value;
std::vector<std::string> notes;
int level;
};
#line 8 "blocks.md"
#line 6 "frag.md"
class Frag;
struct Write_State {
std::string source_name = {};
bool in_macro = false;
bool c_style;
Write_State(const std::string &f);
};
#line 25 "frag.md"
#line 19 "log.md"
#define ASSERT(COND) \
if (! (COND)) { \
\
std::cerr << \
__FILE__ << ':' << __LINE__ << \
' ' << #COND << " FAILED"; \
\
\
std::cerr << '\n'; \
\
\
throw std::exception(); \
\
}
#line 36 "log.md"
#define ASSERT_MSG(COND, MSG) \
if (! (COND)) { \
\
std::cerr << \
__FILE__ << ':' << __LINE__ << \
' ' << #COND << " FAILED"; \
\
std::cerr << ": " << MSG; \
\
std::cerr << '\n'; \
\
\
throw std::exception(); \
\
}
#line 51 "log.md"
#define WARN_MSG(MSG) \
std::cerr << __FILE__ << ':' << \
__LINE__ << ' '; \
std::cerr << MSG; \
\
std::cerr << '\n'; \
class Frag_Ref {
public:
const std::string path;
const std::string name;
const bool local;
Frag_Ref(const std::string &p, const std::string &n, bool l):
path { p }, name { n }, local { l }
{ }
};
class Frag_Entry {
std::string str_;
std::string file_;
int first_line_ { -1 };
int last_line_;
Frag_Ref sub_ = { std::string {}, std::string {}, true };
public:
#line 67 "frag.md"
Frag_Entry() { }
Frag_Entry(Frag_Ref sub):
sub_ { sub }
{ }
#line 79 "frag.md"
void update_state(Write_State &state) const {
#line 90 "frag.md"
auto c { str_.end() };
auto b { str_.begin() };
bool some_nl { false };
while (b != c) {
--c;
#line 108 "frag.md"
if (*c == '\n' || *c == '\r') {
some_nl = true;
continue;
}
if (*c <= ' ') { continue; }
if (*c == '\\') {
if (some_nl) {
state.in_macro = true;
return;
}
}
#line 96 "frag.md"
break;
}
if (b != c && *c > ' ') {
state.in_macro = false;
}
#line 81 "frag.md"
}
#line 127 "frag.md"
std::string str(Write_State &state) const {
#line 144 "frag.md"
bool old { state.in_macro };
update_state(state);
if (old) { return str_; }
if (! state.c_style) { return str_; }
if (first_line_ < 1) { return str_; }
if (str_.empty()) { return str_; };
#line 129 "frag.md"
std::ostringstream oss;
oss << "\n#line " << first_line_ << " \"" << file_ << "\"\n" << str_;
return oss.str();
}
#line 160 "frag.md"
void add(char ch, const std::string &file, int line) {
#line 171 "frag.md"
if (file_.empty() || first_line_ <= 0) {
file_ = file;
first_line_ = line;
}
last_line_ = line;
#line 162 "frag.md"
str_ += ch;
}
#line 183 "frag.md"
void add(const std::string &value, const std::string &file, int line) {
#line 171 "frag.md"
if (file_.empty() || first_line_ <= 0) {
file_ = file;
first_line_ = line;
}
last_line_ = line;
#line 185 "frag.md"
str_ += value;
}
#line 193 "frag.md"
bool canAdd(const std::string &file, int line) {
#line 204 "frag.md"
if (! file_.empty() && file != file_) {
return false;
}
#line 214 "frag.md"
if (last_line_ > 0 && last_line_ != line && last_line_ + 1 != line) {
return false;
}
#line 224 "frag.md"
return true;
#line 195 "frag.md"
return false;
}
#line 50 "frag.md"
const Frag_Ref &sub() const {
return sub_;
}
};
#line 238 "frag.md"
class Frag {
std::vector<Frag_Entry> entries_;
int expands_;
int multiples_;
Frag *prefix_;
const bool is_meta_;
public:
const std::string name;
#line 258 "frag.md"
static bool isFile(const std::string &name) {
static const std::string prefix { "file: " };
std::string p { name.substr(0, prefix.size()) };
return p == prefix;
}
#line 269 "frag.md"
static std::string cmd(const std::string &name) {
static const std::string prefix { "| " };
std::string p { name.substr(0, prefix.size()) };
return p == prefix ? name.substr(prefix.size()) : std::string {};
}
#line 280 "frag.md"
Frag(const std::string &name, Frag *prefix):
entries_ {}, expands_ { 0 }, multiples_ { 0 }, prefix_ { prefix },
is_meta_ { name.find('@') != std::string::npos }, name { name }
{
if (isFile(name)) { ++expands_; }
if (cmd(name).size()) { ++expands_; }
}
#line 294 "frag.md"
const Frag *prefix() const {
return prefix_;
}
Frag *prefix() {
return prefix_;
}
#line 306 "frag.md"
bool is_meta() {
return is_meta_;
}
#line 314 "frag.md"
void clear() {
if (prefix_) {
prefix_->clear();
}
entries_.clear();
}
#line 327 "frag.md"
bool empty() const {
if (prefix_ && ! prefix_->empty()) {
return false;
}
return entries_.empty();
}
#line 411 "frag.md"
void add(const std::string &value, const std::string &file, int line) {
if (value.empty()) { return; }
#line 425 "frag.md"
if (entries_.empty()) {
entries_.emplace_back();
} else if (! entries_.back().canAdd(file, line)) {
entries_.emplace_back();
}
#line 414 "frag.md"
entries_.back().add(value, file, line);
}
#line 437 "frag.md"
void add(char ch, const std::string &file, int line) {
#line 425 "frag.md"
if (entries_.empty()) {
entries_.emplace_back();
} else if (! entries_.back().canAdd(file, line)) {
entries_.emplace_back();
}
#line 439 "frag.md"
entries_.back().add(ch, file, line);
}
#line 447 "frag.md"
Frag &add(const Frag_Ref &sub);
#line 474 "frag.md"
auto begin() const {
return entries_.cbegin();
}
#line 483 "frag.md"
auto end() const {
return entries_.cend();
}
#line 492 "frag.md"
int expands() const {
return expands_ + (prefix_ ? prefix_->expands() : 0);
}
#line 501 "frag.md"
void addExpand() {
++expands_;
}
#line 510 "frag.md"
int multiples() const {
return multiples_ + (prefix_ ? prefix_->multiples() : 0);
}
#line 519 "frag.md"
void addMultiple() {
++multiples_;
}
#line 528 "frag.md"
static bool is_c_style(const std::string &name) {
#line 538 "frag.md"
static const std::string exts[] = { ".c", ".h", ".cpp" };
const std::string *end = exts + sizeof(exts)/sizeof(*exts);
#line 546 "frag.md"
for (auto i = exts; i != end; ++i) {
if (name.length() > i->length()) {
if (name.substr(name.length() - i->length()) == *i) {
return true;
}
}
}
#line 530 "frag.md"
return false;
}
#line 247 "frag.md"
};
#line 339 "frag.md"
Write_State::Write_State(const std::string &f):
c_style { Frag::is_c_style(f) }
{ }
#line 355 "frag.md"
void test_frag_name(const std::string &name) {
Frag f(name, nullptr);
ASSERT(f.name == name);
}
#line 382 "frag.md"
bool isPopulatedFrag(const Frag *f) {
return f && ! f->empty();
}
#line 455 "frag.md"
#line 748 "frag.md"
bool isFragInFrag(const std::string &path,
const Frag *needle, const Frag *haystack
) {
ASSERT(needle);
ASSERT(haystack);
#line 772 "frag.md"
if (needle == haystack) {
return true;
}
#line 754 "frag.md"
#line 781 "frag.md"
if (haystack->prefix() &&
isFragInFrag(path, needle, haystack->prefix())
) {
return true;
}
for (const auto &i : *haystack) {
if (i.sub().name.empty()) { continue; }
std::string new_path { path };
Frag *f { find_frag(i.sub(), &new_path) };
if (! f) { continue; }
if (isFragInFrag(new_path, needle, f)) {
return true;
}
}
#line 755 "frag.md"
return false;
}
#line 456 "frag.md"
Frag &Frag::add(const Frag_Ref &sub) {
#line 764 "frag.md"
Frag &f { get_frag(sub) };
ASSERT(! isFragInFrag(sub.path, this, &f));
#line 458 "frag.md"
#line 467 "frag.md"
entries_.push_back(Frag_Entry { sub });
#line 459 "frag.md"
return *this;
}
#line 563 "frag.md"
void serializeFrag(const Frag &frag, std::ostream &out,
Write_State &state, const std::string &path
) {
#line 586 "frag.md"
if (frag.prefix()) {
serializeFrag(*frag.prefix(), out, state, path);
}
for (const auto &entry : frag) {
if (! entry.sub().name.empty()) {
std::string new_path = path;
const Frag *f { find_frag(entry.sub(), &new_path) };
if (f) {
serializeFrag(*f, out, state, new_path);
} else {
std::cerr << "no frag [" << entry.sub().name << "], " <<
(entry.sub().local ? "local" : "global" ) << ", [" <<
path << "]\n";
}
}
out << entry.str(state);
}
#line 567 "frag.md"
}
#line 574 "frag.md"
void serializeFrag(const std::string &name, const Frag &f,
std::ostream &out, const std::string &path
) {
Write_State state { name };
return serializeFrag(f, out, state, path);
}
#line 610 "frag.md"
bool check_frag(const Frag &f, std::istream &in,
Write_State &state, const std::string &path
) {
#line 634 "frag.md"
if (f.prefix()) {
if (! check_frag(*f.prefix(), in, state, path)) {
return false;
}
}
for (const auto &entry : f) {
if (! entry.sub().name.empty()) {
std::string new_path = path;
const Frag *f { find_frag(entry.sub(), &new_path) };
if (f) {
if (! check_frag(*f, in, state, new_path)) {
return false;
}
}
}
#line 658 "frag.md"
for (const auto &i : entry.str(state)) {
if (in.get() != i) {
return false;
}
}
#line 650 "frag.md"
}
#line 614 "frag.md"
return true;
}
#line 622 "frag.md"
bool check_frag(const std::string &name, const Frag &f,
std::istream &in, const std::string &path
) {
Write_State state { name };
return check_frag(f, in, state, path);
}
#line 669 "frag.md"
void testFrag(const std::string &name,
const Frag &frag, const std::string &expected
) {
#line 687 "frag.md"
std::ostringstream buffer;
serializeFrag(name, frag, buffer, "");
ASSERT(buffer.str() == expected);
#line 673 "frag.md"
}
#line 697 "frag.md"
void addStringToFrag(Frag *frag, const std::string &str) {
frag->add(str, std::string {}, 0);
}
#line 27 "frag.md"
#line 162 "read.md"
class Input {
public:
#line 228 "read.md"
Input(const std::string &prev = {}): prev { prev } { }
#line 236 "read.md"
Input(const Input &) = delete;
Input(Input &&) = default;
#line 245 "read.md"
Input &operator=(const Input &) = delete;
#line 113 "blocks.md"
std::vector<Block> blocks;
#line 165 "read.md"
const std::string prev;
};
#line 148 "read.md"
class Open_Input {
public:
#line 192 "read.md"
Open_Input(const std::string &path):
path_ { path },
file_ { path.c_str() }
{}
#line 202 "read.md"
const std::string &path() const {
return path_;
}
#line 210 "read.md"
Open_Input(const Open_Input &) = delete;
Open_Input(Open_Input &&) = default;
#line 219 "read.md"
Open_Input &operator=(const Open_Input &) = delete;
Open_Input &operator=(Open_Input &&) = default;
#line 253 "read.md"
void read_line(std::string &line) {
if (file_.is_open()) {
#line 266 "read.md"
if (std::getline(file_, line)) {
#line 137 "input.md"
++line_;
#line 268 "read.md"
return;
}
file_.close();
#line 256 "read.md"
}
throw No_More_Lines {};
}
#line 33 "blocks.md"
Read_State state = RS::new_element;
#line 128 "input.md"
int line() const {
return line_;
}
#line 151 "read.md"
private:
#line 183 "read.md"
std::string path_;
std::ifstream file_;
#line 121 "input.md"
int line_ = 0;
#line 153 "read.md"
};
#line 318 "index.md"
using SI = std::string::const_iterator;
#line 21 "read.md"
class Inputs {
public:
#line 43 "read.md"
void read_line(std::string &line);
#line 107 "read.md"
void clear() {
#line 144 "input.md"
used_.clear();
open_.clear();
if (roots_.empty()) {
#line 157 "input.md"
if (std::filesystem::exists("index.md")) {
roots_.push_back("index.md");
} else if (std::filesystem::exists("index.x")) {
roots_.push_back("index.x");
} else {
std::cerr << "no input paths\n";
roots_.push_back("index.md");
}
#line 148 "input.md"
;
}
current_path_ = roots_.begin();
#line 109 "read.md"
}
#line 14 "input.md"
auto &cur() {
ASSERT (! open_.empty());
return open_.back();
}
auto &cur_input() {
return used_.find(cur().path())->second;
}
#line 27 "input.md"
auto begin() {
return used_.begin();
}
#line 36 "input.md"
auto end() {
return used_.end();
}
#line 45 "input.md"
auto size() const {
return used_.size();
}
#line 54 "input.md"
void push(const std::string &path) {
std::string prev;
if (open_.size()) {
auto got { used_.find(open_.back().path()) };
if (got != used_.end()) {
prev = open_.back().path();
}
}
used_.insert(std::move(
std::map<std::string, Input>::value_type(path, Input(prev))
));
open_.emplace_back(path);
}
const std::string open_head() const {
ASSERT(! open_.empty());
return open_.back().path();
}
Input &operator[](const std::string &name) {
return used_[name];
}
#line 81 "input.md"
void add(const std::string &path) {
roots_.push_back(path);
push(path);
}
#line 96 "input.md"
bool has(const std::string &name) const {
#line 106 "input.md"
if (used_.find(name) != used_.end()) {
return true;
}
#line 98 "input.md"
;
return false;
}
#line 24 "read.md"
private:
#line 279 "read.md"
std::vector<std::string> roots_;
std::vector<std::string>::const_iterator current_path_;
#line 288 "read.md"
std::vector<Open_Input> open_;
std::map<std::string, Input> used_;
#line 26 "read.md"
};