-
Notifications
You must be signed in to change notification settings - Fork 3
/
termpose.cpp
2158 lines (2054 loc) · 69 KB
/
termpose.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
//
#include <iostream>
#include <sstream>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <functional>
#include <vector>
#include <unordered_map>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <sstream>
#include <type_traits>
#include <memory>
#include "termpose.c"
namespace termpose{
namespace detail{
template <typename T>
std::vector<T> operator+(const std::vector<T> &A, const std::vector<T> &B)
{
std::vector<T> AB;
AB.reserve( A.size() + B.size() );
AB.insert( AB.end(), A.begin(), A.end() );
AB.insert( AB.end(), B.begin(), B.end() );
return AB;
}
//could probably be optimized for moves:
template <typename T>
void append(std::vector<T> &A, std::vector<T> const&B){
A.reserve( A.size() + B.size() );
A.insert( A.end(), B.begin(), B.end() );
}
std::string toString(int b){
std::stringstream ss;
ss << b;
return ss.str();
}
// hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh
template<typename T, typename B> std::vector<B> map(std::vector<T>& v, std::function<B (T)> f){
unsigned ei = v.size();
std::vector<B> ret(ei);
for(int i=0; i<ei; ++i){
ret[i] = f(v[i]);
}
return ret;
}
const std::string EMPTY_STRING = "";
}
struct Error{
unsigned line;
unsigned column;
std::string msg;
Error(unsigned line, unsigned column, std::string msg): line(line), column(column), msg(msg) {}
};
struct TermposeError:public std::runtime_error{
TermposeError(std::string msg):std::runtime_error(msg){}
static void putErr(std::stringstream& ss, Error const& e){
ss<<" error line:";
ss<<e.line;
ss<<" column:";
ss<<e.column;
ss<<' ';
ss<<" msg:";
ss<<e.msg;
ss<<'\n';
}
};
struct ParserError:public TermposeError{
public:
Error e;
std::string errString;
ParserError(unsigned line, unsigned column, std::string msg):TermposeError("ParserError"), e{line, column, msg} {
std::stringstream ss;
ss<<"ParserError ";
putErr(ss, e);
errString = ss.str();
}
char const* what() const noexcept override{
return errString.c_str();
}
};
union Term;
bool escapeIsNeeded(std::string const& str){
if(str == ""){ return true; } //empty strings need to be quoted, so, escape
for(int i=0; i<str.size(); ++i){
switch(str[i]){
case ' ': return true; break;
case '(': return true; break;
case ':': return true; break;
case '\t': return true; break;
case '\n': return true; break;
case '\r': return true; break;
case ')': return true; break;
}
}
return false;
}
void escapeSymbol(std::stringstream& ss, std::string const& str){
for(int i=0; i<str.size(); ++i){
char c = str[i];
switch(c){
case '\\':
ss << '\\';
ss << '\\';
break;
case '"':
ss << '\\';
ss << '"';
break;
case '\t':
ss << '\\';
ss << 't';
break;
case '\n':
ss << '\\';
ss << 'n';
break;
case '\r':
ss << '\\';
ss << 'r';
break;
default:
ss << c;
}
}
}
/// just used during pretty printing
union Term;
struct PrettyPrintingState {
std::unordered_map<Term const*, unsigned> totalLengthsWithChildren;
std::stringstream out;
std::string indent;
std::string lineEndings;
unsigned depth;
unsigned lineWidth;
unsigned shortWordLimit;
};
struct List;
struct Stri{
uint8_t tag;
uint32_t line;
uint32_t column;
std::string s;
static Stri create(std::string str);
static Stri create(std::string str, uint32_t line, uint32_t column);
private:
void baseLineStringify(std::stringstream& ss) const;
void stringify(std::stringstream& ss) const;
void prettyPrinting(PrettyPrintingState& s, unsigned depth) const;
// void prettyPrinting(std::stringstream& ss, unsigned depth, std::string indent, std::string lineEndings, unsigned lineWidth) const;
friend Term;
friend List;
};
struct List{
uint8_t tag;
uint32_t line;
uint32_t column;
std::vector<Term> l;
static List create(std::vector<Term> ov);
static List create(std::vector<Term> ov, uint32_t line, uint32_t column);
private:
void stringify(std::stringstream& ss) const;
void baseLineStringify(std::stringstream& ss) const;
void prettyPrinting(PrettyPrintingState& s, unsigned depth) const;
friend Term;
friend Stri;
};
struct Checker;
union Term{
public:
Stri s; List l;
static void mimicInterTerm(Term* r, InterTerm* v);
bool isStr() const;
bool isList() const;
bool isEmpty() const; //iff it's an empty list
std::string& strContents();
std::vector<Term>& listContents();
Term& findTerm(std::string const& key); //seeks term by initialString, throws if it doesn't find
Term* seekTerm(std::string const& key); //seeks term by initialString, returns null if it doesn't find
Term& findSubTerm(std::string const& key); //seeks term by initialString, then returns the second element of it
Term* seekSubTerm(std::string const& key);
Term const& findTermConst(std::string const& key) const;
Term const* seekTermConst(std::string const& key) const;
Term const& findSubTermConst(std::string const& key) const;
Term const* seekSubTermConst(std::string const& key) const;
Term& subTerm();
Term const& subTermConst() const;
Term& head();
Term const& headConst() const;
std::vector<Term> const& listContentsConst() const;
std::string const& strContentsConst() const;
std::string toString() const;
std::string toStringMultiline() const;
std::vector<Term> const& listContentsConst(uint expectedLength) const;
std::vector<Term>& listContents(uint expectedLength);
std::vector<Term> const& listContentsOfLengthConst(uint expectedLength) const ;
std::vector<Term>& listContentsOfLength(uint expectedLength);
std::vector<Term> const& listContentsOfMinimumLengthConst(uint minimumLength) const ;
std::vector<Term>& listContentsOfMinimumLength(uint minimumLength);
bool startsWith(std::string const& str) const;
std::string escapedString() const ;
std::string prettyPrint(unsigned lineLimit = 80) const;
std::string prettyPrintMultiline(unsigned lineLimit = 80) const;
std::string const& initialString() const; //Stri("a") | List("a", ...) | List(List("a" ...) ...) -> "a", List() | List(List() ...) -> ""
static inline Term parseMultiline(std::string const& s);
static inline Term parse(std::string const& s);
Term();
Term(Term&& other) noexcept;
Term(Term const& other);
Term(List other);
Term(Stri other);
Term(std::vector<Term>&& ts, unsigned line=0, unsigned column=0);
Term(std::vector<Term> const& ts, unsigned line=0, unsigned column=0);
Term(char const* other, unsigned line=0, unsigned column=0);
Term(std::string&& other, unsigned line=0, unsigned column=0);
Term(std::string const& other, unsigned line=0, unsigned column=0);
uint32_t line() const;
uint32_t column() const;
Term& operator=(Term const& other);
Term& operator=(Term&& other) noexcept;
size_t hash() const;
~Term();
private:
uint computeTotalLengths(PrettyPrintingState& pp) const;
void baseLineStringify(std::stringstream& ss) const;
void stringify(std::stringstream& ss) const;
void prettyPrinting(PrettyPrintingState& s, unsigned depth) const;
// void prettyPrinting(std::stringstream& sb, unsigned depth, std::string indent, std::string lineEndings, unsigned lineWidth) const;
static void parseLengthedToSeqs(Term* termOut, std::string& unicodeString, char** errorOut);
friend Stri;
friend List;
};
struct TyperError:public TermposeError{
std::vector<termpose::Error> errors;
std::string waht; //can't generate on demand, because what() is a const method. Needs to be created on construction. It needs to be stored in the body so that it's still available after what() returns.
TyperError(Term const& t, std::string msg): TyperError(t.l.line,t.l.column,msg){}
TyperError(unsigned line, unsigned column, std::string msg):TyperError({Error{line, column, msg}}) {}
TyperError(std::vector<Error> errors): TermposeError("TyperError"), errors(move(errors)) {
std::stringstream ss;
ss<<"TyperError ";
for(Error const& e : this->errors){ putErr(ss, e); } //`this->`, because if we refer to the parameter, it's empty, it was moved see
waht = ss.str();
}
virtual char const* what() const noexcept {
return waht.c_str();
}
TyperError():TermposeError("TyperError"){}
};
Error error(Term& t, std::string msg){ return Error{t.l.line, t.l.column, msg}; }
void escapedStringification(std::stringstream& ss, std::string const& s){
ss<<'"';
escapeSymbol(ss, s);
ss<<'"';
}
void Stri::stringify(std::stringstream& ss) const{
if(termpose::escapeIsNeeded(s)){
escapedStringification(ss, s);
}else{
ss<<s;
}
}
void Stri::baseLineStringify(std::stringstream& ss) const{ stringify(ss); }
void Stri::prettyPrinting(PrettyPrintingState& pp, unsigned depth) const{
for(int i=0; i<depth; ++i){ pp.out<<pp.indent; }
stringify(pp.out);
pp.out << pp.lineEndings;
}
void Term::mimicInterTerm(Term* r, InterTerm* v){
if(isInterStri(v)){
auto is = (InterStri*)v;
auto rs = &r->s;
rs->tag = 1;
rs->line = is->line;
rs->column = is->column;
new (& rs->s) std::string(is->v);
}else{
auto is = (InterSeqs*)v;
auto rl = &r->l;
rl->tag = 0;
rl->line = is->line;
rl->column = is->column;
new (& rl->l) std::vector<Term>();
std::vector<Term>& rv = rl->l;
size_t vl = is->s.len;
rv.reserve(vl);
auto mimiced = [is](InterTerm* v){
Term ntb;
mimicInterTerm(&ntb, v);
return ntb;
};
for(int i=0; i<vl; ++i){
rv.emplace_back(mimiced((InterTerm*)(is->s.v[i])));
}
}
}
bool Term::isStr() const{ return (*(uint8_t*)this) != 0; }
bool Term::isList() const{ return !isStr(); }
bool Term::isEmpty() const{ return !isStr() && l.l.size() == 0; }
Term& Term::findTerm(std::string const& key){
if(!isList()) throw TyperError(*this, "searching for a term on a non-list term");
for(Term& t : l.l){
if(t.initialString() == key){
return t;
}
}
std::stringstream ss;
ss<<"no term found having key ";
ss<<key;
throw TyperError(*this, ss.str());
}
Term* Term::seekTerm(std::string const& key){
if(!isList()){
if(key == s.s){
return this;
}else{
return nullptr;
}
}else{
for(Term& t : l.l){
if(t.initialString() == key){
return &t;
}
}
return nullptr;
}
}
Term& Term::findSubTerm(std::string const& key){
if(!isList()) throw TyperError(*this, "searching for a term on a non-list term");
for(Term& t : l.l){
if(t.isList() && t.l.l.size() == 2){
Term& ft = t.l.l[0];
if(ft.isStr() && ft.s.s == key){
return t.l.l[1];
}
}
}
std::stringstream ss;
ss<<"no pair found having key ";
ss<<key;
throw TyperError(*this, ss.str());
}
Term* Term::seekSubTerm(std::string const& key){
if(!isList()){ return nullptr; }
for(Term& t : l.l){
if(t.isList() && t.l.l.size() == 2){
Term& ft = t.l.l[0];
if(ft.isStr() && ft.s.s == key){
return &t.l.l[1];
}
}
}
return nullptr;
}
Term const& Term::findTermConst(std::string const& key) const {
if(!isList()) throw TyperError(*this, "searching for a term on a non-list term");
for(Term const& t : l.l){
if(t.initialString() == key){
return t;
}
}
std::stringstream ss;
ss<<"no term found having key ";
ss<<key;
throw TyperError(*this, ss.str());
}
Term const* Term::seekTermConst(std::string const& key) const {
if(!isList()){
if(key == s.s){
return this;
}else{
return nullptr;
}
}else{
for(Term const& t : l.l){
if(t.initialString() == key){
return &t;
}
}
return nullptr;
}
}
Term const& Term::findSubTermConst(std::string const& key) const {
if(!isList()) throw TyperError(*this, "searching for a term on a non-list term");
for(Term const& t : l.l){
if(t.isList() && t.l.l.size() >= 1){
Term const& ft = t.l.l[0];
if(ft.isStr() && ft.s.s == key){
if(t.l.l.size() == 2){
return t.l.l[1];
}else{
throw TyperError(t, "seekSubTerm matched this term's key, but the term is not a pair. It should only have two elements.");
}
}
}
}
std::stringstream ss;
ss<<"no pair found having key ";
ss<<key;
throw TyperError(*this, ss.str());
}
Term const* Term::seekSubTermConst(std::string const& key) const {
if(!isList()){ return nullptr; }
for(Term const& t : l.l){
if(t.initialString() == key){
if(t.isList() && t.l.l.size() >= 2){
return &t.l.l[1];
}else{
return nullptr;
}
}
}
return nullptr;
}
Term& Term::subTerm() {
if(!isList()){ throw TyperError(*this, "wanted to find a subterm, but it is not a list term"); }
if(l.l.size() < 2){ throw TyperError(*this, "wanted to find a subterm, but the list is too short"); }
return l.l[1];
}
Term const& Term::subTermConst() const {
if(!isList()){ throw TyperError(*this, "wanted to find a subterm, but it is not a list term"); }
if(l.l.size() < 2){ throw TyperError(*this, "wanted to find a subterm, but the list is too short"); }
return l.l[1];
}
Term& Term::head() {
if(!isList()){ throw TyperError(*this, "wanted to find the head term, but it is not a list term"); }
if(l.l.size() < 1){ throw TyperError(*this, "wanted to find the head term, but the list is empty"); }
return l.l[0];
}
Term const& Term::headConst() const {
if(!isList()){ throw TyperError(*this, "wanted to find the head term, but it is not a list term"); }
if(l.l.size() < 1){ throw TyperError(*this, "wanted to find the head term, but the list is empty"); }
return l.l[0];
}
std::vector<Term>& Term::listContents(){
if(isList()){
return l.l;
}else{
throw TyperError(*this, "expected a list term here");
}
}
std::string& Term::strContents(){
if(isStr()){
return s.s;
}else{
throw TyperError(*this, "expected a str term here");
}
}
std::vector<Term> const& Term::listContentsConst() const{
if(isList()){
return l.l;
}else{
throw TyperError(*this, "expected a list term here");
}
}
std::string const& Term::strContentsConst() const{
if(isStr()){
return s.s;
}else{
throw TyperError(*this, "expected a str term here");
}
}
std::vector<Term> const& Term::listContentsOfLengthConst(uint expectedLength) const {
std::vector<Term> const& l = listContentsConst();
if(l.size() != expectedLength){ throw TyperError(*this, std::string("expected a list of length ")+detail::toString(expectedLength)); }
return l;
}
std::vector<Term>& Term::listContentsOfLength(uint expectedLength){
std::vector<Term>& l = listContents();
if(l.size() != expectedLength){ throw TyperError(*this, std::string("expected a list of length ")+detail::toString(expectedLength)); }
return l;
}
std::vector<Term> const& Term::listContentsOfMinimumLengthConst(uint minimumLength) const {
std::vector<Term> const& l = listContentsConst();
if(l.size() < minimumLength){ throw TyperError(*this, std::string("expected a list of length ")+detail::toString(minimumLength)); }
return l;
}
std::vector<Term>& Term::listContentsOfMinimumLength(uint minimumLength){
std::vector<Term>& l = listContents();
if(l.size() < minimumLength){ throw TyperError(*this, std::string("expected a list of length ")+detail::toString(minimumLength)); }
return l;
}
Term::Term(){
List* os = (List*)this;
os->tag = 0;
os->line = 0;
os->column = 0;
new (&os->l) std::vector<Term>();
}
Term::Term(Term&& other) noexcept {
if(other.isStr()){
Stri* fs = &other.s;
Stri* os = (Stri*)this;
os->tag = 1;
os->line = fs->line;
os->column = fs->column;
new (&os->s) std::string(std::move(fs->s));
}else{
List* fl = &other.l;
List* ol = (List*)this;
ol->tag = 0;
ol->line = fl->line;
ol->column = fl->column;
new (&ol->l) std::vector<Term>(std::move(fl->l));
}
}
Term::Term(Term const& other){
if(other.isStr()){
Stri const* fs = &other.s;
Stri* os = (Stri*)this;
os->tag = 1;
os->line = fs->line;
os->column = fs->column;
new (&os->s) std::string(fs->s);
}else{
List const* fl = &other.l;
List* ol = (List*)this;
ol->tag = 0;
ol->line = fl->line;
ol->column = fl->column;
new (&ol->l) std::vector<Term>(fl->l);
}
}
Term::Term(char const* other, unsigned line, unsigned column){
new (this) Term(std::string(other), line, column);
}
Term::Term(std::vector<Term> const& ts, unsigned line, unsigned column):Term(std::vector<Term>(ts), line, column){}
Term::Term(std::vector<Term>&& ts, unsigned line, unsigned column){
List* os = (List*)this;
os->tag = 0;
os->line = line;
os->column = column;
new (&os->l) std::vector<Term>(std::move(ts));
}
Term::Term(std::string const& ts, unsigned line, unsigned column):Term(std::string(ts), line, column){}
Term::Term(std::string&& other, unsigned line, unsigned column){
Stri* os = (Stri*)this;
os->tag = 1;
os->line = 0;
os->column = 0;
new (&os->s) std::string(std::move(other));
}
Term::Term(List other){
List* ol = (List*)this;
ol->tag = other.tag;
ol->line = other.line;
ol->column = other.column;
new (&ol->l) std::vector<Term>(std::move(other.l));
}
Term::Term(Stri other){
Stri* os = (Stri*)this;
os->tag = other.tag;
os->line = other.line;
os->column = other.column;
new (&os->s) std::string(std::move(other.s));
}
uint32_t Term::line() const {
return ((List*)(this))->line; //doesn't matter if not List, layout is same in both
}
uint32_t Term::column() const {
return ((List*)(this))->column; //doesn't matter if not List, layout is same in both
}
std::string Term::escapedString() const {
std::stringstream ss;
escapedStringification(ss, initialString());
return ss.str();
}
Term& Term::operator=(Term const& other){
this->~Term();
new (this) Term(other);
return *this;
}
Term& Term::operator=(Term&& other) noexcept {
this->~Term();
new (this) Term(std::move(other));
return *this;
}
Term::~Term(){
if(isStr()){
((Stri*)this)->~Stri();
}else{
((List*)this)->~List();
}
}
void Term::baseLineStringify(std::stringstream& ss) const{
if(isStr()){
s.baseLineStringify(ss);
}else{
l.baseLineStringify(ss);
}
}
void Term::prettyPrinting(PrettyPrintingState& pp, unsigned depth) const{
if(isStr()){
s.prettyPrinting(pp, depth);
}else{
l.prettyPrinting(pp, depth);
}
}
void Term::stringify(std::stringstream& ss) const{
if(isStr()){
s.stringify(ss);
}else{
l.stringify(ss);
}
}
std::string const& Term::initialString() const{
if(isStr()){
return s.s;
}else{
if(l.l.size()){
return l.l[0].initialString();
}else{
return detail::EMPTY_STRING;
}
}
}
std::string Term::toString() const{
std::stringstream ss;
stringify(ss);
return ss.str();
}
uint Term::computeTotalLengths(PrettyPrintingState& pp) const {
unsigned thisl = 0;
if(isStr()){
thisl = s.s.length();
}else{
thisl = 2;
if(l.l.size()){
thisl += l.l.size() - 1;
for(Term const& c : l.l){
thisl += c.computeTotalLengths(pp);
}
}
}
pp.totalLengthsWithChildren[this] = thisl;
return thisl;
}
std::string Term::prettyPrint(unsigned lineLimit) const {
PrettyPrintingState pp;
pp.indent = " ";
pp.lineEndings = "\n";
pp.shortWordLimit = 21;
pp.lineWidth = lineLimit;
computeTotalLengths(pp);
if(isStr()){
s.stringify(pp.out);
}else{
//special case for root line of multiline files
if(l.l.size() > 1 && pp.totalLengthsWithChildren[this] > lineLimit){
for(Term const& t : l.l){
t.prettyPrinting(pp, 0);
}
}else{
prettyPrinting(pp, 0);
}
}
return pp.out.str();
}
std::string Term::prettyPrintMultiline(unsigned lineLimit) const{
PrettyPrintingState pp;
pp.indent = " ";
pp.lineEndings = "\n";
pp.shortWordLimit = 21;
pp.lineWidth = lineLimit;
computeTotalLengths(pp);
if(isStr()){
//this should maybe throw exception..
s.stringify(pp.out);
}else{
for(Term const& t : l.l){
t.prettyPrinting(pp, 0);
}
}
return pp.out.str();
}
std::string Term::toStringMultiline() const {
std::stringstream ss;
if(isStr()){
//this should maybe throw exception..
s.stringify(ss);
}else{
for(Term const& t : l.l){
t.stringify(ss);
ss << "\n";
}
}
return ss.str();
}
bool Term::startsWith(std::string const& str) const{
return !isStr() && l.l.size() && l.l[0].isStr() && l.l[0].s.s == str;
}
inline Term Term::parseMultiline(std::string const& unicodeString){ //note, throws ParserError if there was a syntax error
//redefining stuff so that we can avoid the middle part of the translation from interterm to ctermpose::term to termpose::term. Musing: If I were REALLY serious I'd cut out the interterms alltogether and do a proper C++ port. Hm. I suppose there might be a way of abstracting over the differences between C and C++ and using the same parsing code for both.
TermposeParsingError errorOut;
Parser p;
initParser(&p);
parseLengthedToSeqsTakingParserRef(&p, unicodeString.c_str(), unicodeString.size(), &errorOut);
if(!errorOut.msg){
Term ret;
List* lv = (List*)&ret;
lv->tag = 0;
lv->line = 0;
lv->column = 0;
new(&lv->l) std::vector<Term>();
std::vector<Term>& outVec = lv->l;
unsigned rarl = p.rootArBuf.len;
InterTerm** rarv = (InterTerm**)p.rootArBuf.v;
for(int i=0; i<rarl; ++i){
Term t;
mimicInterTerm(&t, rarv[i]);
outVec.push_back(t);
}
return ret;
}else{
std::string msg(errorOut.msg);
throw ParserError(errorOut.line,errorOut.column,msg);
}
}
inline Term Term::parse(std::string const& unicodeString){ //if there is exactly one line, it will be given sole, otherwise the lines(or lack thereof) will be wrapped in a List. parseMultiline has more consistent behavior, always wrapping the input in a List no matter how many root lines there were. throws ParserError if there was a syntax error
Term ret = Term::parseMultiline(unicodeString);
if(ret.isStr()){
return ret;
}else{
List& rl = ret.l;
if(rl.l.size() == 1){
Term rr = rl.l[0];
rl.l.pop_back();
return rr;
}else{
return ret;
}
}
}
void List::stringify(std::stringstream& ss) const {
ss<<'(';
size_t ls = l.size();
if(ls > 0){
l[0].stringify(ss);
for(int i=1; i<ls; ++i){
ss<<' ';
l[i].stringify(ss);
}
}
ss<<')';
}
void List::baseLineStringify(std::stringstream& ss) const {
size_t ls = l.size();
if(ls > 0){
if(ls > 1){
l[0].stringify(ss);
for(int i=1; i<ls; ++i){
ss<<' ';
l[i].stringify(ss);
}
}else{
ss << '(';
l[0].stringify(ss);
ss << ')';
}
}else{
ss << '(';
ss << ')';
}
}
void List::prettyPrinting(PrettyPrintingState& pp, uint depth) const {
for(int i=0; i<depth; ++i){ pp.out << pp.indent; }
if(l.size() == 0){
pp.out << '(';
pp.out << ')';
pp.out << pp.lineEndings;
return;
}else{
if(pp.totalLengthsWithChildren[(Term const*)this] > pp.lineWidth){
//consider printing in line anyway if none of the inner terms are long enough for that to be hard to read
for(Term const& t : l){
if(pp.totalLengthsWithChildren[&t] > pp.shortWordLimit){
//print in indent style after all
if(pp.totalLengthsWithChildren[&l[0]] > pp.lineWidth){
//indent sequence style
pp.out << ':';
pp.out << pp.lineEndings;
for(Term const& t : l){
t.prettyPrinting(pp, depth + 1);
}
}else{
l[0].stringify(pp.out);
pp.out << pp.lineEndings;
size_t ln = l.size();
for(int i=1; i<ln; ++i){
l[i].prettyPrinting(pp, depth+1);
}
}
return;
}
}
//nothing was too long, okay then, print inline
}
//otherwise, either it fits in one line, or none of the interior terms are long enough for printing inline to be difficult to read.
baseLineStringify(pp.out);
pp.out << pp.lineEndings;
}
}
Stri Stri::create(std::string str){ return Stri::create(str, 0,0); }
Stri Stri::create(std::string str, uint32_t line, uint32_t column){
return Stri{1,line,column,str};
}
List List::create(std::vector<Term> iv){ return List::create(iv, 0,0); }
List List::create(std::vector<Term> ov, uint32_t line, uint32_t column){
return List{0,line,column,ov};
}
bool operator==(Stri const& a, Stri const& b){ return a.s == b.s; }
bool operator!=(Term const& a, Term const& b);
bool operator==(List const& a, List const& b){
unsigned len = a.l.size();
if(len != b.l.size()) return false;
for(int i=0; i<len; ++i){
if(a.l[i] != b.l[i]) return false;
}
return true;
}
bool operator==(Term const& a, Term const& b){
if(a.isStr()){
if(b.isStr()){
return (Stri const&)a == (Stri const&)b;
}else{
return false;
}
}else{
if(b.isStr()){
return false;
}else{
return (List const&)a == (List const&)b;
}
}
}
size_t Term::hash() const{
size_t soFar = std::hash<bool>()(isList());
if(isList()){
for(Term const& nt : this->l.l){ soFar ^= nt.hash(); }
}else{
soFar ^= std::hash<std::string>()(this->s.s);
}
return soFar;
}
bool operator!=(Stri const& a, Stri const& b){ return a.s != b.s; }
bool operator!=(List const& a, List const& b){ return ! (a == b); }
bool operator!=(Term const& a, Term const& b){ return ! (a == b); }
template<class... Rest>
inline static void recu_terms(std::vector<Term>& o, Term t, Rest... rest){
o.emplace_back(std::move(t));
recu_terms(o,rest...);
}
inline static void recu_terms(std::vector<Term>& o){}
template<class... Rest>
static Term terms(Rest... rest){
std::vector<Term> o;
recu_terms(o, rest...);
return List::create(o);
}
namespace parsingDSL{
template<typename T> struct Checker{
virtual T check(Term const& v) = 0; //throws TyperError
};
template<typename T> struct Termer{
virtual Term termify(T const& v) = 0;
};
template<typename T> struct Translator : Checker<T>, Termer<T>{
};
template<typename T> struct BuiltTranslator : Translator<T> {
std::shared_ptr<Checker<T>> ck;
std::shared_ptr<Termer<T>> tk;
BuiltTranslator(std::shared_ptr<Checker<T>> ck, std::shared_ptr<Termer<T>> tk): ck(move(ck)), tk(move(tk)) {}
T check(Term const& v) override { return ck->check(v); }
Term termify(T const& v) override { return tk->termify(v); }
};
template<typename T> struct LambdaChecker : Checker<T> {
std::function<T(Term const&)> f;
LambdaChecker(std::function<T(Term const&)> f):f(move(f)) {}
T check(Term const& v) override { return f(v); }
};
template<typename T> struct LambdaTermer : Termer<T> {
std::function<Term(T const&)> f;
LambdaTermer(std::function<Term(T const&)> f):f(move(f)) {}
Term termify(T const& v) override { return f(v); }
};
Term termifyBool(bool const& v){
return Stri::create(v?"true":"false");
}
bool checkBool(Term const& v){
if(v.isStr()){
std::string const& st = v.s.s;
if(st == "true" || st == "⊤" || st == "yes" || st == "on") return true;
else if(st == "false" || st == "⊥" || st == "no" || st == "off") return false;
else throw TyperError(v, "expected a bool here");
}else{
throw TyperError(v, "expected a bool here");
}
}
struct BoolTermer : Termer<bool>{ virtual Term termify(bool const& b){
return termifyBool(b);
}};
struct BoolChecker : Checker<bool>{ virtual bool check(Term const& v){
return checkBool(v);
}};
struct BoolTranslator : Translator<bool> {
virtual Term termify(bool const& b){
return termifyBool(b);
}
virtual bool check(Term const& v){
return checkBool(v);
}
};
Term termifyString(std::string const& b){
std::stringstream ss;
ss << b;
return Stri::create(ss.str());
}
std::string checkString(Term const& v){
if(v.isStr()){
return v.s.s;
}else{
throw TyperError(v, "expected a string here");
}
}
struct StringTermer : Termer<std::string>{ virtual Term termify(std::string const& b){
return termifyString(b);
}};
struct StringChecker : Checker<std::string>{ virtual std::string check(Term const& v){
return checkString(v);
}};
struct StringTranslator : Translator<std::string> {
std::string check(Term const& v){ return checkString(v); }
Term termify(std::string const& v){ return termifyString(v); }
};
struct BlandTermer : Termer<Term>{ virtual Term termify(Term const& b){ return b; }};
struct BlandChecker : Checker<Term>{ virtual Term check(Term const& v){ return v; }};
struct BlandTranslator : Translator<Term> {
Term termify(Term const& b){ return b; }
Term check(Term const& v){ return v; }
};
Term termifyInt(int const& b){
return Stri::create(detail::toString(b));
}
int checkInt(Term const& v){
if(v.isStr()){
std::string const& st = v.s.s;
try{
return stoi(st);
}catch(std::invalid_argument e){
throw TyperError(v, "expected a int here (can't be parsed to an int)");
}catch(std::out_of_range e){
throw TyperError(v, "expected a int here (is not in the allowable range)");
}
}else{
throw TyperError(v, "expected a int here (is a list)");
}
}
struct IntTermer : Termer<int>{ virtual Term termify(int const& b){ return termifyInt(b); } };
struct IntChecker : Checker<int>{ int check(Term const& v){ return checkInt(v); } };