-
Notifications
You must be signed in to change notification settings - Fork 161
/
parser.hpp
1909 lines (1797 loc) · 72 KB
/
parser.hpp
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
// Copyright Toru Niina 2017.
// Distributed under the MIT License.
#ifndef TOML11_PARSER_HPP
#define TOML11_PARSER_HPP
#include "result.hpp"
#include "region.hpp"
#include "combinator.hpp"
#include "lexer.hpp"
#include "types.hpp"
#include "value.hpp"
#include <iostream>
#include <cstring>
namespace toml
{
namespace detail
{
template<typename Container>
result<std::pair<boolean, region<Container>>, std::string>
parse_boolean(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_boolean::invoke(loc))
{
const auto reg = token.unwrap();
if (reg.str() == "true") {return ok(std::make_pair(true, reg));}
else if(reg.str() == "false") {return ok(std::make_pair(false, reg));}
else // internal error.
{
throw toml::internal_error(format_underline(
"[error] toml::parse_boolean: internal error",
{{std::addressof(reg), "invalid token"}}));
}
}
loc.reset(first); //rollback
return err(format_underline("[error] toml::parse_boolean: ",
{{std::addressof(loc), "the next token is not a boolean"}}));
}
template<typename Container>
result<std::pair<integer, region<Container>>, std::string>
parse_binary_integer(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_bin_int::invoke(loc))
{
auto str = token.unwrap().str();
assert(str.size() > 2); // minimum -> 0b1
integer retval(0), base(1);
for(auto i(str.rbegin()), e(str.rend() - 2); i!=e; ++i)
{
if (*i == '1'){retval += base; base *= 2;}
else if(*i == '0'){base *= 2;}
else if(*i == '_'){/* do nothing. */}
else // internal error.
{
throw toml::internal_error(format_underline(
"[error] toml::parse_integer: internal error",
{{std::addressof(token.unwrap()), "invalid token"}}));
}
}
return ok(std::make_pair(retval, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_binary_integer:",
{{std::addressof(loc), "the next token is not an integer"}}));
}
template<typename Container>
result<std::pair<integer, region<Container>>, std::string>
parse_octal_integer(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_oct_int::invoke(loc))
{
auto str = token.unwrap().str();
str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
str.erase(str.begin()); str.erase(str.begin()); // remove `0o` prefix
std::istringstream iss(str);
integer retval(0);
iss >> std::oct >> retval;
return ok(std::make_pair(retval, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_octal_integer:",
{{std::addressof(loc), "the next token is not an integer"}}));
}
template<typename Container>
result<std::pair<integer, region<Container>>, std::string>
parse_hexadecimal_integer(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_hex_int::invoke(loc))
{
auto str = token.unwrap().str();
str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
str.erase(str.begin()); str.erase(str.begin()); // remove `0x` prefix
std::istringstream iss(str);
integer retval(0);
iss >> std::hex >> retval;
return ok(std::make_pair(retval, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_hexadecimal_integer",
{{std::addressof(loc), "the next token is not an integer"}}));
}
template<typename Container>
result<std::pair<integer, region<Container>>, std::string>
parse_integer(location<Container>& loc)
{
const auto first = loc.iter();
if(first != loc.end() && *first == '0')
{
const auto second = std::next(first);
if(second == loc.end()) // the token is just zero.
{
return ok(std::make_pair(0, region<Container>(loc, first, second)));
}
if(*second == 'b') {return parse_binary_integer (loc);} // 0b1100
if(*second == 'o') {return parse_octal_integer (loc);} // 0o775
if(*second == 'x') {return parse_hexadecimal_integer(loc);} // 0xC0FFEE
if(std::isdigit(*second))
{
return err(format_underline("[error] toml::parse_integer: "
"leading zero in an Integer is not allowed.",
{{std::addressof(loc), "leading zero"}}));
}
else if(std::isalpha(*second))
{
return err(format_underline("[error] toml::parse_integer: "
"unknown integer prefix appeared.",
{{std::addressof(loc), "none of 0x, 0o, 0b"}}));
}
}
if(const auto token = lex_dec_int::invoke(loc))
{
auto str = token.unwrap().str();
str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
std::istringstream iss(str);
integer retval(0);
iss >> retval;
return ok(std::make_pair(retval, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_integer: ",
{{std::addressof(loc), "the next token is not an integer"}}));
}
template<typename Container>
result<std::pair<floating, region<Container>>, std::string>
parse_floating(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_float::invoke(loc))
{
auto str = token.unwrap().str();
if(str == "inf" || str == "+inf")
{
if(std::numeric_limits<floating>::has_infinity)
{
return ok(std::make_pair(
std::numeric_limits<floating>::infinity(), token.unwrap()));
}
else
{
throw std::domain_error("toml::parse_floating: inf value found"
" but the current environment does not support inf. Please"
" make sure that the floating-point implementation conforms"
" IEEE 754/ISO 60559 international standard.");
}
}
else if(str == "-inf")
{
if(std::numeric_limits<floating>::has_infinity)
{
return ok(std::make_pair(
-std::numeric_limits<floating>::infinity(), token.unwrap()));
}
else
{
throw std::domain_error("toml::parse_floating: inf value found"
" but the current environment does not support inf. Please"
" make sure that the floating-point implementation conforms"
" IEEE 754/ISO 60559 international standard.");
}
}
else if(str == "nan" || str == "+nan")
{
if(std::numeric_limits<floating>::has_quiet_NaN)
{
return ok(std::make_pair(
std::numeric_limits<floating>::quiet_NaN(), token.unwrap()));
}
else if(std::numeric_limits<floating>::has_signaling_NaN)
{
return ok(std::make_pair(
std::numeric_limits<floating>::signaling_NaN(), token.unwrap()));
}
else
{
throw std::domain_error("toml::parse_floating: NaN value found"
" but the current environment does not support NaN. Please"
" make sure that the floating-point implementation conforms"
" IEEE 754/ISO 60559 international standard.");
}
}
else if(str == "-nan")
{
if(std::numeric_limits<floating>::has_quiet_NaN)
{
return ok(std::make_pair(
-std::numeric_limits<floating>::quiet_NaN(), token.unwrap()));
}
else if(std::numeric_limits<floating>::has_signaling_NaN)
{
return ok(std::make_pair(
-std::numeric_limits<floating>::signaling_NaN(), token.unwrap()));
}
else
{
throw std::domain_error("toml::parse_floating: NaN value found"
" but the current environment does not support NaN. Please"
" make sure that the floating-point implementation conforms"
" IEEE 754/ISO 60559 international standard.");
}
}
str.erase(std::remove(str.begin(), str.end(), '_'), str.end());
std::istringstream iss(str);
floating v(0.0);
iss >> v;
return ok(std::make_pair(v, token.unwrap()));
}
loc.reset(first);
return err(format_underline("[error] toml::parse_floating: ",
{{std::addressof(loc), "the next token is not a float"}}));
}
template<typename Container, typename Container2>
std::string read_utf8_codepoint(const region<Container>& reg,
/* for err msg */ const location<Container2>& loc)
{
const auto str = reg.str().substr(1);
std::uint_least32_t codepoint;
std::istringstream iss(str);
iss >> std::hex >> codepoint;
std::string character;
if(codepoint < 0x80) // U+0000 ... U+0079 ; just an ASCII.
{
character += static_cast<char>(codepoint);
}
else if(codepoint < 0x800) //U+0080 ... U+07FF
{
// 110yyyyx 10xxxxxx; 0x3f == 0b0011'1111
character += static_cast<unsigned char>(0xC0| codepoint >> 6);
character += static_cast<unsigned char>(0x80|(codepoint & 0x3F));
}
else if(codepoint < 0x10000) // U+0800...U+FFFF
{
if(0xD800 <= codepoint && codepoint <= 0xDFFF)
{
throw syntax_error(format_underline("[error] "
"toml::read_utf8_codepoint: codepoints in the range "
"[0xD800, 0xDFFF] are not valid UTF-8.", {{
std::addressof(loc), "not a valid UTF-8 codepoint"
}}));
}
assert(codepoint < 0xD800 || 0xDFFF < codepoint);
// 1110yyyy 10yxxxxx 10xxxxxx
character += static_cast<unsigned char>(0xE0| codepoint >> 12);
character += static_cast<unsigned char>(0x80|(codepoint >> 6 & 0x3F));
character += static_cast<unsigned char>(0x80|(codepoint & 0x3F));
}
else if(codepoint < 0x110000) // U+010000 ... U+10FFFF
{
// 11110yyy 10yyxxxx 10xxxxxx 10xxxxxx
character += static_cast<unsigned char>(0xF0| codepoint >> 18);
character += static_cast<unsigned char>(0x80|(codepoint >> 12 & 0x3F));
character += static_cast<unsigned char>(0x80|(codepoint >> 6 & 0x3F));
character += static_cast<unsigned char>(0x80|(codepoint & 0x3F));
}
else // out of UTF-8 region
{
throw syntax_error(format_underline("[error] toml::read_utf8_codepoint:"
" input codepoint is too large.",
{{std::addressof(loc), "should be in [0x00..0x10FFFF]"}}));
}
return character;
}
template<typename Container>
result<std::string, std::string> parse_escape_sequence(location<Container>& loc)
{
const auto first = loc.iter();
if(first == loc.end() || *first != '\\')
{
return err(format_underline("[error]: toml::parse_escape_sequence: ", {{
std::addressof(loc), "the next token is not a backslash \"\\\""}}));
}
loc.advance();
switch(*loc.iter())
{
case '\\':{loc.advance(); return ok(std::string("\\"));}
case '"' :{loc.advance(); return ok(std::string("\""));}
case 'b' :{loc.advance(); return ok(std::string("\b"));}
case 't' :{loc.advance(); return ok(std::string("\t"));}
case 'n' :{loc.advance(); return ok(std::string("\n"));}
case 'f' :{loc.advance(); return ok(std::string("\f"));}
case 'r' :{loc.advance(); return ok(std::string("\r"));}
case 'u' :
{
if(const auto token = lex_escape_unicode_short::invoke(loc))
{
return ok(read_utf8_codepoint(token.unwrap(), loc));
}
else
{
return err(format_underline("[error] parse_escape_sequence: "
"invalid token found in UTF-8 codepoint uXXXX.",
{{std::addressof(loc), "here"}}));
}
}
case 'U':
{
if(const auto token = lex_escape_unicode_long::invoke(loc))
{
return ok(read_utf8_codepoint(token.unwrap(), loc));
}
else
{
return err(format_underline("[error] parse_escape_sequence: "
"invalid token found in UTF-8 codepoint Uxxxxxxxx",
{{std::addressof(loc), "here"}}));
}
}
}
const auto msg = format_underline("[error] parse_escape_sequence: "
"unknown escape sequence appeared.", {{std::addressof(loc),
"escape sequence is one of \\, \", b, t, n, f, r, uxxxx, Uxxxxxxxx"}},
/* Hints = */{"if you want to write backslash as just one backslash, "
"use literal string like: regex = '<\\i\\c*\\s*>'"});
loc.reset(first);
return err(msg);
}
template<typename Container>
result<std::pair<toml::string, region<Container>>, std::string>
parse_ml_basic_string(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_ml_basic_string::invoke(loc))
{
auto inner_loc = loc;
inner_loc.reset(first);
std::string retval;
retval.reserve(token.unwrap().size());
auto delim = lex_ml_basic_string_delim::invoke(inner_loc);
if(!delim)
{
throw internal_error(format_underline("[error] "
"parse_ml_basic_string: invalid token",
{{std::addressof(inner_loc), "should be \"\"\""}}));
}
// immediate newline is ignored (if exists)
/* discard return value */ lex_newline::invoke(inner_loc);
delim = none();
while(!delim)
{
using lex_unescaped_seq = repeat<
either<lex_ml_basic_unescaped, lex_newline>, unlimited>;
if(auto unescaped = lex_unescaped_seq::invoke(inner_loc))
{
retval += unescaped.unwrap().str();
}
if(auto escaped = parse_escape_sequence(inner_loc))
{
retval += escaped.unwrap();
}
if(auto esc_nl = lex_ml_basic_escaped_newline::invoke(inner_loc))
{
// ignore newline after escape until next non-ws char
}
if(inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error] "
"parse_ml_basic_string: unexpected end of region",
{{std::addressof(inner_loc), "not sufficient token"}}));
}
delim = lex_ml_basic_string_delim::invoke(inner_loc);
}
return ok(std::make_pair(toml::string(retval), token.unwrap()));
}
else
{
loc.reset(first);
return err(format_underline("[error] toml::parse_ml_basic_string: "
"the next token is not a multiline string",
{{std::addressof(loc), "here"}}));
}
}
template<typename Container>
result<std::pair<toml::string, region<Container>>, std::string>
parse_basic_string(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_basic_string::invoke(loc))
{
auto inner_loc = loc;
inner_loc.reset(first);
auto quot = lex_quotation_mark::invoke(inner_loc);
if(!quot)
{
throw internal_error(format_underline("[error] parse_basic_string: "
"invalid token", {{std::addressof(inner_loc), "should be \""}}));
}
std::string retval;
retval.reserve(token.unwrap().size());
quot = none();
while(!quot)
{
using lex_unescaped_seq = repeat<lex_basic_unescaped, unlimited>;
if(auto unescaped = lex_unescaped_seq::invoke(inner_loc))
{
retval += unescaped.unwrap().str();
}
if(auto escaped = parse_escape_sequence(inner_loc))
{
retval += escaped.unwrap();
}
if(inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error] "
"parse_ml_basic_string: unexpected end of region",
{{std::addressof(inner_loc), "not sufficient token"}}));
}
quot = lex_quotation_mark::invoke(inner_loc);
}
return ok(std::make_pair(toml::string(retval), token.unwrap()));
}
else
{
loc.reset(first); // rollback
return err(format_underline("[error] toml::parse_basic_string: "
"the next token is not a string",
{{std::addressof(loc), "here"}}));
}
}
template<typename Container>
result<std::pair<toml::string, region<Container>>, std::string>
parse_ml_literal_string(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_ml_literal_string::invoke(loc))
{
location<std::string> inner_loc(loc.name(), token.unwrap().str());
const auto open = lex_ml_literal_string_delim::invoke(inner_loc);
if(!open)
{
throw internal_error(format_underline("[error] "
"parse_ml_literal_string: invalid token",
{{std::addressof(inner_loc), "should be '''"}}));
}
// immediate newline is ignored (if exists)
/* discard return value */ lex_newline::invoke(inner_loc);
const auto body = lex_ml_literal_body::invoke(inner_loc);
const auto close = lex_ml_literal_string_delim::invoke(inner_loc);
if(!close)
{
throw internal_error(format_underline("[error] "
"parse_ml_literal_string: invalid token",
{{std::addressof(inner_loc), "should be '''"}}));
}
return ok(std::make_pair(
toml::string(body.unwrap().str(), toml::string_t::literal),
token.unwrap()));
}
else
{
loc.reset(first); // rollback
return err(format_underline("[error] toml::parse_ml_literal_string: "
"the next token is not a multiline literal string",
{{std::addressof(loc), "here"}}));
}
}
template<typename Container>
result<std::pair<toml::string, region<Container>>, std::string>
parse_literal_string(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_literal_string::invoke(loc))
{
location<std::string> inner_loc(loc.name(), token.unwrap().str());
const auto open = lex_apostrophe::invoke(inner_loc);
if(!open)
{
throw internal_error(format_underline("[error] "
"parse_literal_string: invalid token",
{{std::addressof(inner_loc), "should be '"}}));
}
const auto body = repeat<lex_literal_char, unlimited>::invoke(inner_loc);
const auto close = lex_apostrophe::invoke(inner_loc);
if(!close)
{
throw internal_error(format_underline("[error] "
"parse_literal_string: invalid token",
{{std::addressof(inner_loc), "should be '"}}));
}
return ok(std::make_pair(
toml::string(body.unwrap().str(), toml::string_t::literal),
token.unwrap()));
}
else
{
loc.reset(first); // rollback
return err(format_underline("[error] toml::parse_literal_string: "
"the next token is not a literal string",
{{std::addressof(loc), "here"}}));
}
}
template<typename Container>
result<std::pair<toml::string, region<Container>>, std::string>
parse_string(location<Container>& loc)
{
if(loc.iter() != loc.end() && *(loc.iter()) == '"')
{
if(loc.iter() + 1 != loc.end() && *(loc.iter() + 1) == '"' &&
loc.iter() + 2 != loc.end() && *(loc.iter() + 2) == '"')
{
return parse_ml_basic_string(loc);
}
else
{
return parse_basic_string(loc);
}
}
else if(loc.iter() != loc.end() && *(loc.iter()) == '\'')
{
if(loc.iter() + 1 != loc.end() && *(loc.iter() + 1) == '\'' &&
loc.iter() + 2 != loc.end() && *(loc.iter() + 2) == '\'')
{
return parse_ml_literal_string(loc);
}
else
{
return parse_literal_string(loc);
}
}
return err(format_underline("[error] toml::parse_string: ",
{{std::addressof(loc), "the next token is not a string"}}));
}
template<typename Container>
result<std::pair<local_date, region<Container>>, std::string>
parse_local_date(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_local_date::invoke(loc))
{
location<std::string> inner_loc(loc.name(), token.unwrap().str());
const auto y = lex_date_fullyear::invoke(inner_loc);
if(!y || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != '-')
{
throw internal_error(format_underline("[error]: "
"toml::parse_inner_local_date: invalid year format",
{{std::addressof(inner_loc), "should be `-`"}}));
}
inner_loc.advance();
const auto m = lex_date_month::invoke(inner_loc);
if(!m || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != '-')
{
throw internal_error(format_underline("[error]: "
"toml::parse_local_date: invalid month format",
{{std::addressof(inner_loc), "should be `-`"}}));
}
inner_loc.advance();
const auto d = lex_date_mday::invoke(inner_loc);
if(!d)
{
throw internal_error(format_underline("[error]: "
"toml::parse_local_date: invalid day format",
{{std::addressof(inner_loc), "here"}}));
}
return ok(std::make_pair(local_date(
static_cast<std::int16_t>(from_string<int>(y.unwrap().str(), 0)),
static_cast<month_t>(
static_cast<std::int8_t>(from_string<int>(m.unwrap().str(), 0)-1)),
static_cast<std::int8_t>(from_string<int>(d.unwrap().str(), 0))),
token.unwrap()));
}
else
{
loc.reset(first);
return err(format_underline("[error]: toml::parse_local_date: ",
{{std::addressof(loc), "the next token is not a local_date"}}));
}
}
template<typename Container>
result<std::pair<local_time, region<Container>>, std::string>
parse_local_time(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_local_time::invoke(loc))
{
location<std::string> inner_loc(loc.name(), token.unwrap().str());
const auto h = lex_time_hour::invoke(inner_loc);
if(!h || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != ':')
{
throw internal_error(format_underline("[error]: "
"toml::parse_local_time: invalid year format",
{{std::addressof(inner_loc), "should be `:`"}}));
}
inner_loc.advance();
const auto m = lex_time_minute::invoke(inner_loc);
if(!m || inner_loc.iter() == inner_loc.end() || *inner_loc.iter() != ':')
{
throw internal_error(format_underline("[error]: "
"toml::parse_local_time: invalid month format",
{{std::addressof(inner_loc), "should be `:`"}}));
}
inner_loc.advance();
const auto s = lex_time_second::invoke(inner_loc);
if(!s)
{
throw internal_error(format_underline("[error]: "
"toml::parse_local_time: invalid second format",
{{std::addressof(inner_loc), "here"}}));
}
local_time time(
static_cast<std::int8_t>(from_string<int>(h.unwrap().str(), 0)),
static_cast<std::int8_t>(from_string<int>(m.unwrap().str(), 0)),
static_cast<std::int8_t>(from_string<int>(s.unwrap().str(), 0)), 0, 0);
const auto before_secfrac = inner_loc.iter();
if(const auto secfrac = lex_time_secfrac::invoke(inner_loc))
{
auto sf = secfrac.unwrap().str();
sf.erase(sf.begin()); // sf.front() == '.'
switch(sf.size() % 3)
{
case 2: sf += '0'; break;
case 1: sf += "00"; break;
case 0: break;
default: break;
}
if(sf.size() >= 6)
{
time.millisecond = from_string<std::int16_t>(sf.substr(0, 3), 0);
time.microsecond = from_string<std::int16_t>(sf.substr(3, 3), 0);
}
else if(sf.size() >= 3)
{
time.millisecond = from_string<std::int16_t>(sf, 0);
time.microsecond = 0;
}
}
else
{
if(before_secfrac != inner_loc.iter())
{
throw internal_error(format_underline("[error]: "
"toml::parse_local_time: invalid subsecond format",
{{std::addressof(inner_loc), "here"}}));
}
}
return ok(std::make_pair(time, token.unwrap()));
}
else
{
loc.reset(first);
return err(format_underline("[error]: toml::parse_local_time: ",
{{std::addressof(loc), "the next token is not a local_time"}}));
}
}
template<typename Container>
result<std::pair<local_datetime, region<Container>>, std::string>
parse_local_datetime(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_local_date_time::invoke(loc))
{
location<std::string> inner_loc(loc.name(), token.unwrap().str());
const auto date = parse_local_date(inner_loc);
if(!date || inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error]: "
"toml::parse_local_datetime: invalid datetime format",
{{std::addressof(inner_loc), "date, not datetime"}}));
}
const char delim = *(inner_loc.iter());
if(delim != 'T' && delim != 't' && delim != ' ')
{
throw internal_error(format_underline("[error]: "
"toml::parse_local_datetime: invalid datetime format",
{{std::addressof(inner_loc), "should be `T` or ` ` (space)"}}));
}
inner_loc.advance();
const auto time = parse_local_time(inner_loc);
if(!time)
{
throw internal_error(format_underline("[error]: "
"toml::parse_local_datetime: invalid datetime format",
{{std::addressof(inner_loc), "invalid time fomrat"}}));
}
return ok(std::make_pair(
local_datetime(date.unwrap().first, time.unwrap().first),
token.unwrap()));
}
else
{
loc.reset(first);
return err(format_underline("[error]: toml::parse_local_datetime: ",
{{std::addressof(loc), "the next token is not a local_datetime"}}));
}
}
template<typename Container>
result<std::pair<offset_datetime, region<Container>>, std::string>
parse_offset_datetime(location<Container>& loc)
{
const auto first = loc.iter();
if(const auto token = lex_offset_date_time::invoke(loc))
{
location<std::string> inner_loc(loc.name(), token.unwrap().str());
const auto datetime = parse_local_datetime(inner_loc);
if(!datetime || inner_loc.iter() == inner_loc.end())
{
throw internal_error(format_underline("[error]: "
"toml::parse_offset_datetime: invalid datetime format",
{{std::addressof(inner_loc), "date, not datetime"}}));
}
time_offset offset(0, 0);
if(const auto ofs = lex_time_numoffset::invoke(inner_loc))
{
const auto str = ofs.unwrap().str();
if(str.front() == '+')
{
offset.hour = static_cast<std::int8_t>(from_string<int>(str.substr(1,2), 0));
offset.minute = static_cast<std::int8_t>(from_string<int>(str.substr(4,2), 0));
}
else
{
offset.hour = -static_cast<std::int8_t>(from_string<int>(str.substr(1,2), 0));
offset.minute = -static_cast<std::int8_t>(from_string<int>(str.substr(4,2), 0));
}
}
else if(*inner_loc.iter() != 'Z' && *inner_loc.iter() != 'z')
{
throw internal_error(format_underline("[error]: "
"toml::parse_offset_datetime: invalid datetime format",
{{std::addressof(inner_loc), "should be `Z` or `+HH:MM`"}}));
}
return ok(std::make_pair(offset_datetime(datetime.unwrap().first, offset),
token.unwrap()));
}
else
{
loc.reset(first);
return err(format_underline("[error]: toml::parse_offset_datetime: ",
{{std::addressof(loc), "the next token is not a offset_datetime"}}));
}
}
template<typename Container>
result<std::pair<key, region<Container>>, std::string>
parse_simple_key(location<Container>& loc)
{
if(const auto bstr = parse_basic_string(loc))
{
return ok(std::make_pair(bstr.unwrap().first.str, bstr.unwrap().second));
}
if(const auto lstr = parse_literal_string(loc))
{
return ok(std::make_pair(lstr.unwrap().first.str, lstr.unwrap().second));
}
if(const auto bare = lex_unquoted_key::invoke(loc))
{
const auto reg = bare.unwrap();
return ok(std::make_pair(reg.str(), reg));
}
return err(format_underline("[error] toml::parse_simple_key: ",
{{std::addressof(loc), "the next token is not a simple key"}}));
}
// dotted key become vector of keys
template<typename Container>
result<std::pair<std::vector<key>, region<Container>>, std::string>
parse_key(location<Container>& loc)
{
const auto first = loc.iter();
// dotted key -> foo.bar.baz whitespaces are allowed
if(const auto token = lex_dotted_key::invoke(loc))
{
const auto reg = token.unwrap();
location<std::string> inner_loc(loc.name(), reg.str());
std::vector<key> keys;
while(inner_loc.iter() != inner_loc.end())
{
lex_ws::invoke(inner_loc);
if(const auto k = parse_simple_key(inner_loc))
{
keys.push_back(k.unwrap().first);
}
else
{
throw internal_error(format_underline("[error] "
"toml::detail::parse_key: dotted key contains invalid key",
{{std::addressof(inner_loc), k.unwrap_err()}}));
}
lex_ws::invoke(inner_loc);
if(inner_loc.iter() == inner_loc.end())
{
break;
}
else if(*inner_loc.iter() == '.')
{
inner_loc.advance(); // to skip `.`
}
else
{
throw internal_error(format_underline("[error] toml::parse_key: "
"dotted key contains invalid key ",
{{std::addressof(inner_loc), "should be `.`"}}));
}
}
return ok(std::make_pair(keys, reg));
}
loc.reset(first);
// simple key -> foo
if(const auto smpl = parse_simple_key(loc))
{
return ok(std::make_pair(std::vector<key>(1, smpl.unwrap().first),
smpl.unwrap().second));
}
return err(format_underline("[error] toml::parse_key: ",
{{std::addressof(loc), "is not a valid key"}}));
}
// forward-decl to implement parse_array and parse_table
template<typename Container>
result<value, std::string> parse_value(location<Container>&);
template<typename Container>
result<std::pair<array, region<Container>>, std::string>
parse_array(location<Container>& loc)
{
const auto first = loc.iter();
if(loc.iter() == loc.end())
{
return err("[error] toml::parse_array: input is empty");
}
if(*loc.iter() != '[')
{
return err("[error] toml::parse_array: token is not an array");
}
loc.advance();
using lex_ws_comment_newline = repeat<
either<lex_wschar, lex_newline, lex_comment>, unlimited>;
array retval;
while(loc.iter() != loc.end())
{
lex_ws_comment_newline::invoke(loc); // skip
if(loc.iter() != loc.end() && *loc.iter() == ']')
{
loc.advance(); // skip ']'
return ok(std::make_pair(retval,
region<Container>(loc, first, loc.iter())));
}
if(auto val = parse_value(loc))
{
if(!retval.empty() && retval.front().type() != val.as_ok().type())
{
auto array_start_loc = loc;
array_start_loc.reset(first);
throw syntax_error(format_underline("[error] toml::parse_array: "
"type of elements should be the same each other.", {
{std::addressof(array_start_loc), "array starts here"},
{
std::addressof(get_region(retval.front())),
"value has type " + stringize(retval.front().type())
},
{
std::addressof(get_region(val.unwrap())),
"value has different type, " + stringize(val.unwrap().type())
}
}));
}
retval.push_back(std::move(val.unwrap()));
}
else
{
auto array_start_loc = loc;
array_start_loc.reset(first);
throw syntax_error(format_underline("[error] toml::parse_array: "
"value having invalid format appeared in an array", {
{std::addressof(array_start_loc), "array starts here"},
{std::addressof(loc), "it is not a valid value."}
}));
}
using lex_array_separator = sequence<maybe<lex_ws>, character<','>>;
const auto sp = lex_array_separator::invoke(loc);
if(!sp)
{
lex_ws_comment_newline::invoke(loc);
if(loc.iter() != loc.end() && *loc.iter() == ']')
{
loc.advance(); // skip ']'
return ok(std::make_pair(retval,
region<Container>(loc, first, loc.iter())));
}
else
{
auto array_start_loc = loc;
array_start_loc.reset(first);
throw syntax_error(format_underline("[error] toml::parse_array:"
" missing array separator `,` after a value", {
{std::addressof(array_start_loc), "array starts here"},
{std::addressof(loc), "should be `,`"}
}));
}
}
}
loc.reset(first);
throw syntax_error(format_underline("[error] toml::parse_array: "
"array did not closed by `]`",
{{std::addressof(loc), "should be closed"}}));
}
template<typename Container>
result<std::pair<std::pair<std::vector<key>, region<Container>>, value>, std::string>
parse_key_value_pair(location<Container>& loc)
{
const auto first = loc.iter();
auto key_reg = parse_key(loc);
if(!key_reg)
{
std::string msg = std::move(key_reg.unwrap_err());
// if the next token is keyvalue-separator, it means that there are no
// key. then we need to show error as "empty key is not allowed".
if(const auto keyval_sep = lex_keyval_sep::invoke(loc))
{
loc.reset(first);
msg = format_underline("[error] toml::parse_key_value_pair: "
"empty key is not allowed.",
{{std::addressof(loc), "key expected before '='"}});
}
return err(std::move(msg));
}
const auto kvsp = lex_keyval_sep::invoke(loc);
if(!kvsp)
{
std::string msg;
// if the line contains '=' after the invalid sequence, possibly the
// error is in the key (like, invalid character in bare key).
const auto line_end = std::find(loc.iter(), loc.end(), '\n');
if(std::find(loc.iter(), line_end, '=') != line_end)
{
msg = format_underline("[error] toml::parse_key_value_pair: "
"invalid format for key",