Repository navigation
Expand file tree
/
Copy pathparser.cpp
More file actions
1163 lines (1124 loc) · 36.9 KB
/
Copy pathparser.cpp
File metadata and controls
1163 lines (1124 loc) · 36.9 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
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
*
* Copyright:
* Mikael Zayenz Lagerkvist, 2026
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.dev
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
// Ported from gecode/instance/spec.cpp at Gecode commit
// 599f977b7855dba2a1a5f67db1892722372067d3.
#include "parser.hpp"
#include "fail.hpp"
#include "text.hpp"
#include <algorithm>
#include <array>
#include <optional>
#include <string_view>
#include <utility>
namespace iris::detail {
namespace {
/// Return the base type named `t`, or nothing if there is none
std::optional<ReadType> read_type(std::string_view t) {
if (t == "int")
return ReadType::Int;
if (t == "float")
return ReadType::Float;
if (t == "num")
return ReadType::Num;
if (t == "word")
return ReadType::Word;
if (t == "rest")
return ReadType::Rest;
return std::nullopt;
}
/// Test whether `s` is a name: a letter or underscore, then letters, digits,
/// and underscores
bool is_name(std::string_view s) {
return !s.empty() && !is_digit(s[0]) && std::ranges::all_of(s, is_alnum);
}
/// Add the literal text `lit`, if any, as an item to `pat` and clear it
void flush(Pattern &pat, String &lit) {
if (lit.empty())
return;
Pattern::Item it(Pattern::Item::Kind::Lit);
it.text = std::move(lit);
pat.items.push_back(std::move(it));
lit.clear();
}
/// Test whether pattern item `it` ends with a word character
bool ends_word(const Pattern::Item &it) {
using Kind = Pattern::Item::Kind;
return (it.kind == Kind::Cap) || (it.kind == Kind::Var) ||
((it.kind == Kind::Lit) && is_alnum(it.text.back()));
}
/// Test whether pattern item `it` starts with a word character
bool starts_word(const Pattern::Item &it) {
using Kind = Pattern::Item::Kind;
return (it.kind == Kind::Cap) || (it.kind == Kind::Var) ||
((it.kind == Kind::Lit) && is_alnum(it.text.front()));
}
/// A line pattern with the text of the type of each capture, which is still
/// to be parsed
struct CompiledPattern {
Pattern pattern;
Seq<String> capture_types;
};
/// Compile line pattern `src` on line `line` of the reader spec from
/// `origin`, in the statement or arm on line `owner`
CompiledPattern compile(const String &src, const String &origin,
std::size_t line, std::size_t owner) {
using Item = Pattern::Item;
CompiledPattern compiled;
Pattern &pat = compiled.pattern;
pat.source = src;
std::size_t p = 0;
String lit;
while (p < src.size()) {
const char c = src[p];
if (is_blank(c)) {
flush(pat, lit);
while ((p < src.size()) && is_blank(src[p]))
p++;
pat.items.emplace_back(Item::Kind::Space);
} else if (c == '{') {
flush(pat, lit);
const std::size_t q = src.find('}', p);
if (q == String::npos)
fail_in(ErrorCode::SpecPattern, origin, line, owner,
"unterminated capture in pattern \"" + src + "\"");
const String cap = src.substr(p + 1, q - p - 1);
p = q + 1;
if (cap.starts_with('$')) {
Item it(Item::Kind::Var);
it.text = cap.substr(1);
if (!is_name(it.text))
fail_in(ErrorCode::SpecPattern, origin, line, owner,
"missing or bad name in pattern \"" + src + "\"");
pat.items.push_back(std::move(it));
continue;
}
Item it(Item::Kind::Cap);
const std::size_t colon = cap.find(':');
it.text = cap.substr(0, colon);
if (!is_name(it.text) && !it.text.empty() && (it.text != "_"))
fail_in(ErrorCode::SpecPattern, origin, line, owner,
"capture name '" + it.text + "' in pattern \"" + src +
"\" is not a name");
compiled.capture_types.push_back(
(colon == String::npos) ? "word" : cap.substr(colon + 1));
pat.items.push_back(std::move(it));
} else if (std::string_view(src).substr(p).starts_with("...")) {
flush(pat, lit);
if (src.find_first_not_of(" \t", p + 3) != String::npos)
fail_in(ErrorCode::SpecPattern, origin, line, owner,
"'...' must end the pattern \"" + src + "\"");
pat.items.emplace_back(Item::Kind::Any);
p += 3;
} else {
lit += c;
p++;
}
}
flush(pat, lit);
// A space is required between two items that would otherwise run
// together, such as "p cnf" or "{a:int} {b:int}".
for (std::size_t k = 1; k + 1 < pat.items.size(); k++)
if (pat.items[k].kind == Item::Kind::Space)
pat.items[k].required =
ends_word(pat.items[k - 1]) && starts_word(pat.items[k + 1]);
return compiled;
}
/// A name bound by a comprehension, at a line of the reader spec
struct BoundName {
String name;
std::size_t line;
};
/// A named type, defined by a type statement
struct TypeName {
String name;
const Stmt *def;
};
/// A binary operator and its precedence
struct BinaryOp {
Expr::Op op;
int precedence;
};
/// Parser for reader specs
class Parser {
public:
/// Initialize for `tokens` of a reader spec from `origin`
Parser(Seq<Token> tokens, const String &origin)
: origin_(origin), toks_(std::move(tokens)) {}
/// Parse the whole reader spec
ParsedSpec parse() {
ParsedSpec s;
while (peek().kind != Token::Kind::End) {
owner_line_ = peek().line;
if (keyword("format")) {
next();
if ((peek().kind != Token::Kind::Ident) &&
(peek().kind != Token::Kind::Str))
fail(ErrorCode::SpecSyntax,
"expected a format name, found " + describe(peek()));
s.name = next().text;
// Names such as dimacs-cnf: words joined by '-' or '.'
while (at_name_joiner()) {
s.name += next().text;
s.name += next().text;
}
} else {
if (!s.body.empty() && (s.body.back()->kind == Stmt::Kind::Instances))
fail(ErrorCode::SpecPlacement,
"'instances' must be the last statement of the spec");
s.body.push_back(statement());
}
}
check_conflicts(s.body);
return s;
}
private:
/// Where the reader spec comes from
const String &origin_;
/// Tokens
Seq<Token> toks_;
/// Current position
std::size_t p_ = 0;
/// Nesting depth of blocks
std::size_t depth_ = 0;
/// Nesting depth of expressions
std::size_t nesting_ = 0;
/// Number of repeats parsed
std::size_t repeats_ = 0;
/// Whether the next statement is the first of the body of a repeat
bool repeat_start_ = false;
/// Whether the statement being parsed may be each
bool each_ok_ = false;
/// Line of the innermost statement or sections arm being parsed, which
/// the errors in it belong to wherever they are found
std::size_t owner_line_ = 0;
/// Names bound by the comprehensions being parsed
Seq<String> bound_;
/// All names bound by comprehensions
Seq<BoundName> bound_all_;
/// The named types visible to the statement being parsed
Seq<TypeName> types_;
/// All named types
Seq<TypeName> types_all_;
/// Maximal nesting of blocks and expressions
static constexpr std::size_t max_nesting = 256;
/// Check that no comprehension binds and no named type is a name of the
/// spec, as it would hide that name
void check_conflicts(const Block &body) const {
Seq<Decl> names;
declared(body, names);
const auto is_declared = [&names](const String &n) {
return std::ranges::any_of(names,
[&n](const Decl &d) { return d.name == n; });
};
for (const BoundName &b : bound_all_)
if (is_declared(b.name))
fail_at(ErrorCode::SpecNameConflict, origin_, b.line,
"'" + b.name +
"' is bound by a comprehension but is also a name in the "
"spec");
for (const TypeName &t : types_all_)
if (is_declared(t.name))
fail_at(ErrorCode::SpecNameConflict, origin_, t.def->line,
"'" + t.name +
"' names a type, so it cannot also name a value");
}
[[nodiscard]] const Token &peek() const { return toks_[p_]; }
const Token &next() {
const Token &t = toks_[p_];
if (p_ + 1 < toks_.size())
p_++;
return t;
}
[[noreturn]] void fail(ErrorCode code, std::string_view m) const {
fail_on(code, peek().line, m);
}
/// Fail with `code` and message `m` on line `line`, in the statement or
/// arm being parsed
[[noreturn]] void fail_on(ErrorCode code, std::size_t line,
std::string_view m) const {
fail_in(code, origin_, line, owner_line_, m);
}
static String describe(const Token &t) {
if (t.kind == Token::Kind::End)
return "end of spec";
if (t.kind == Token::Kind::Str)
return "\"" + t.text + "\"";
return "'" + t.text + "'";
}
[[nodiscard]] bool keyword(std::string_view k) const {
return (peek().kind == Token::Kind::Ident) && (peek().text == k);
}
[[nodiscard]] bool punct(std::string_view k) const {
return (peek().kind == Token::Kind::Punct) && (peek().text == k);
}
/// Test whether a '-' or '.' joining the words of a format name such as
/// dimacs-cnf comes next
[[nodiscard]] bool at_name_joiner() const {
if (!(punct("-") || punct(".")) || !peek().glued ||
(p_ + 1 >= toks_.size()))
return false;
const Token &t = toks_[p_ + 1];
return t.glued &&
((t.kind == Token::Kind::Ident) || (t.kind == Token::Kind::Int));
}
void expect_punct(std::string_view k) {
if (!punct(k))
fail(ErrorCode::SpecSyntax,
"expected '" + String(k) + "', found " + describe(peek()));
next();
}
void expect_keyword(std::string_view k) {
if (!keyword(k))
fail(ErrorCode::SpecSyntax,
"expected '" + String(k) + "', found " + describe(peek()));
next();
}
String ident(std::string_view what) {
if (peek().kind != Token::Kind::Ident)
fail(ErrorCode::SpecSyntax,
"expected " + String(what) + ", found " + describe(peek()));
return next().text;
}
/// A name that the spec defines, described by `what`
String new_name(std::string_view what) {
String n = ident(what);
if (is_gecode_name(n))
fail(ErrorCode::SpecName, "'" + n +
"' cannot be defined: names starting "
"with '" +
String(gecode_prefix) +
"' are Gecode constants");
return n;
}
String string(std::string_view what) {
if (peek().kind != Token::Kind::Str)
fail(ErrorCode::SpecSyntax, "expected " + String(what) +
" (a string), found " + describe(peek()));
return next().text;
}
Pattern pattern() {
const std::size_t line = peek().line;
const String s = string("a line pattern");
CompiledPattern compiled = compile(s, origin_, line, owner_line_);
std::size_t k = 0;
for (Pattern::Item &it : compiled.pattern.items)
if (it.kind == Pattern::Item::Kind::Cap) {
it.type = capture_type(compiled.capture_types[k++], line);
if (it.type.read == ReadType::Symbol)
fail_on(ErrorCode::SpecPattern, line,
"a capture cannot be of an alphabet type");
}
return std::move(compiled.pattern);
}
/// Return the type `text` of a capture in a pattern on line `line`
Type capture_type(const String &text, std::size_t line) {
// Parse the text in place of the tokens of the spec
Seq<Token> sub = lex(text, origin_, line);
if (sub.size() == 1)
fail_on(ErrorCode::SpecPattern, line, "a capture needs a type after ':'");
std::swap(toks_, sub);
const std::size_t at = p_;
p_ = 0;
Type t = type();
if (peek().kind != Token::Kind::End)
fail(ErrorCode::SpecPattern, "unexpected " + describe(peek()) +
" in the capture type '" + text + "'");
std::swap(toks_, sub);
p_ = at;
return t;
}
/// Increment nesting counter `n` for `what`, failing beyond the limit
void enter(std::size_t &n, std::string_view what) {
if (++n > max_nesting)
fail(ErrorCode::SpecNesting, String(what) + " are nested too deeply");
}
Block block() {
expect_punct("{");
enter(depth_, "blocks");
// The types defined in the block are only visible inside it
const std::size_t scope = types_.size();
Block b;
while (!punct("}")) {
if (peek().kind == Token::Kind::End)
fail(ErrorCode::SpecSyntax, "missing '}'");
b.push_back(statement());
}
next();
while (types_.size() > scope)
types_.pop_back();
depth_--;
return b;
}
static Node<Expr> integer(std::int64_t v) {
auto e = make_node<Expr>(Expr::Kind::Int);
e->int_value = v;
return e;
}
/// Consume keyword `k` if it comes next, and return whether it did
bool accept(std::string_view k) {
if (!keyword(k))
return false;
next();
return true;
}
/*
* Statements: one function per keyword, called after the keyword with a
* new statement of the keyword's kind
*/
/// A function completing statement `s`
using Parse = void (Parser::*)(Stmt &s);
/// A statement that starts with a keyword
struct Keyword {
/// The keyword
std::string_view keyword;
/// Kind of statement
Stmt::Kind kind;
/// Function parsing the rest of the statement
Parse parse;
/// Whether the statement is a setting, which changes how the whole input
/// is read, so it is at the top level, where it runs once
bool setting;
};
/// Return the statement that starts with keyword `k`, or null
static const Keyword *statement_keyword(std::string_view k) {
using K = Stmt::Kind;
static constexpr std::array<Keyword, 20> statements = {{
{"comment", K::Comment, &Parser::words, true},
{"stop", K::Stop, &Parser::words, true},
{"expect", K::Expect, &Parser::words, false},
{"tokens", K::Tokens, &Parser::chars, true},
{"separators", K::Separators, &Parser::chars, true},
{"ignore", K::Trailing, &Parser::trailing, true},
{"param", K::Param, &Parser::param, false},
{"list", K::List, &Parser::list, false},
{"line", K::Line, &Parser::line_pattern, false},
{"find", K::Find, &Parser::line_pattern, false},
{"skip", K::Skip, &Parser::skip, false},
{"index", K::Index, &Parser::index, false},
{"repeat", K::Repeat, &Parser::repeat, false},
{"each", K::Each, &Parser::each, false},
{"instances", K::Instances, &Parser::instances, false},
{"sections", K::Sections, &Parser::sections, false},
{"if", K::If, &Parser::conditional, false},
{"let", K::Let, &Parser::let, false},
{"check", K::Check, &Parser::check, false},
{"type", K::Type, &Parser::type_def, false},
}};
const auto e = std::ranges::find(statements, k, &Keyword::keyword);
return (e == statements.end()) ? nullptr : &*e;
}
/**
* Test whether a declaration starts at token `k`
*
* A declaration is a name, any dimensions, and a colon, as in
* `d[n][n]: int`. Its name can be a keyword.
*/
[[nodiscard]] bool at_declaration(std::size_t k) const {
if (toks_[k].kind != Token::Kind::Ident)
return false;
k++;
while ((toks_[k].kind == Token::Kind::Punct) && (toks_[k].text == "[")) {
int open = 0;
do {
if (toks_[k].kind == Token::Kind::End)
return false;
if (toks_[k].kind == Token::Kind::Punct) {
if (toks_[k].text == "[")
open++;
else if (toks_[k].text == "]")
open--;
}
k++;
} while (open > 0);
}
return (toks_[k].kind == Token::Kind::Punct) && (toks_[k].text == ":");
}
/// Parse a statement, which owns the errors found while parsing it
Node<Stmt> statement() {
const std::size_t outer = std::exchange(owner_line_, peek().line);
Node<Stmt> s = statement_contents();
owner_line_ = outer;
return s;
}
Node<Stmt> statement_contents() {
const Token &t = peek();
each_ok_ = repeat_start_;
repeat_start_ = false;
if (t.kind != Token::Kind::Ident)
fail(ErrorCode::SpecSyntax, "expected a statement, found " + describe(t));
if (at_declaration(p_)) {
auto s = make_node<Stmt>(Stmt::Kind::Read, t.line);
declaration(*s);
return s;
}
const Keyword *e = statement_keyword(t.text);
if (e == nullptr)
fail(ErrorCode::SpecSyntax, "unknown statement '" + t.text + "'");
if (e->setting && (depth_ > 0))
fail(ErrorCode::SpecPlacement,
"the setting '" + t.text + "' must be at the top level of the spec");
auto s = make_node<Stmt>(e->kind, t.line);
next();
(this->*e->parse)(*s);
return s;
}
/// comment, stop, and expect: one or more strings
void words(Stmt &s) {
s.words.push_back(string(s.kind == Stmt::Kind::Expect ? "a literal token"
: "a line prefix"));
while (peek().kind == Token::Kind::Str)
s.words.push_back(next().text);
}
/// tokens and separators: a string of characters
void chars(Stmt &s) {
s.chars = string(s.kind == Stmt::Kind::Tokens ? "single-character tokens"
: "separator characters");
}
/// ignore trailing
void trailing(Stmt & /*s*/) { expect_keyword("trailing"); }
/// A name for a value that is kept, so not _
String defined(std::string_view what) {
String n = new_name(what);
if (n == "_")
fail(ErrorCode::SpecName, "'_' cannot be defined here");
return n;
}
/// param x [= default]
void param(Stmt &s) {
s.name = defined("a parameter name");
if (punct("=")) {
next();
s.expr = expr();
}
}
/**
* Consume option `k` of a statement if it comes next, and return whether
* it did
*
* A declaration of a value with the same name ends the statement.
*/
bool option(std::string_view k) { return !at_declaration(p_) && accept(k); }
/// Test whether keyword `k` can end a range without an upper bound
static bool ends_range(std::string_view k) {
return (statement_keyword(k) != nullptr) || (k == "layout") ||
(k == "offset") || (k == "until");
}
/// Test whether the upper bound of a range comes next
[[nodiscard]] bool at_upper_bound() const {
const Token &t = peek();
switch (t.kind) {
case Token::Kind::Ident:
return !ends_range(t.text) && !at_declaration(p_);
case Token::Kind::Punct:
return punct("(") || punct("-") || punct("!");
case Token::Kind::End:
return false;
case Token::Kind::Int:
case Token::Kind::Float:
case Token::Kind::Str:
return true;
}
std::unreachable();
}
/// Return the type statement of the named type `n`, or null
[[nodiscard]] const Stmt *named_type(std::string_view n) const {
const auto t =
std::ranges::find(types_.rbegin(), types_.rend(), n, &TypeName::name);
return (t == types_.rend()) ? nullptr : t->def;
}
/// Test whether `n` is the name of a base type
static bool is_base_type(std::string_view n) {
return read_type(n).has_value() || (n == "alphabet");
}
/**
* A type
*
* A base type, a named type, an alphabet type, or a range lo..hi or
* lo..<hi, where one of the bounds can be left out (but not the upper
* bound of lo..<hi).
*/
Type type() {
Type t;
if (peek().kind == Token::Kind::Ident) {
const String &n = peek().text;
if (const std::optional<ReadType> r = read_type(n)) {
next();
t.read = *r;
return t;
}
if (n == "alphabet") {
next();
alphabet(t);
return t;
}
if (const Stmt *d = named_type(n)) {
next();
Type u = d->type.use(d);
u.name = d->name;
return u;
}
}
if (!punct("..") && !punct("..<")) {
Node<Expr> lo = expr();
if (!punct("..") && !punct("..<")) {
if (lo->kind == Expr::Kind::Name)
fail(ErrorCode::SpecUnknownType, "unknown type '" + lo->s + "'");
fail(ErrorCode::SpecSyntax,
"expected '..' or '..<' after the lower bound of a range, found " +
describe(peek()));
}
t.lo = std::move(lo);
}
t.exclusive = punct("..<");
next();
if (at_upper_bound())
t.hi = expr();
if (t.exclusive && !t.hi)
fail(ErrorCode::SpecSyntax, "'..<' needs an upper bound");
if (!t.lo && !t.hi)
fail(ErrorCode::SpecSyntax, "a range needs a lower or an upper bound");
return t;
}
/// alphabet "..." [blank "..."] [strip "..."] [base n], after alphabet
void alphabet(Type &t) {
t.read = ReadType::Symbol;
t.alphabet = string("an alphabet");
for (;;) {
if (option("blank")) {
t.blank = string("blank symbols");
} else if (option("strip")) {
t.strip = string("symbols to strip");
} else if (option("base")) {
const bool negative = punct("-");
if (negative)
next();
if (peek().kind != Token::Kind::Int)
fail(ErrorCode::SpecSyntax,
"expected an integer, found " + describe(peek()));
const std::int64_t base = next().int_value;
t.base = negative ? -base : base;
} else {
break;
}
}
}
/// Test whether type `t` reads numbers
static bool numeric(const Type &t) {
return (t.read == ReadType::Int) || (t.read == ReadType::Float) ||
(t.read == ReadType::Num);
}
/// Optional offset of a declaration or list statement
void offset(Stmt &s) {
if (!option("offset"))
return;
if (!numeric(s.type))
fail(ErrorCode::SpecType, "'offset' needs numbers");
s.offset = expr();
}
/// x[d1][d2]...: type [layout e] [offset e]
void declaration(Stmt &s) {
s.name = new_name("a name");
while (punct("[")) {
next();
s.dims.push_back(expr());
expect_punct("]");
}
expect_punct(":");
s.type = type();
if ((s.type.read == ReadType::Rest) && !s.dims.empty())
fail(ErrorCode::SpecType, "'rest' values cannot be arrays");
if ((s.type.read == ReadType::Symbol) && (s.dims.size() != 2))
fail(ErrorCode::SpecType,
"an alphabet type needs a two-dimensional array");
if (option("layout")) {
if ((s.dims.size() != 2) || !numeric(s.type))
fail(ErrorCode::SpecType,
"'layout' needs a two-dimensional array of numbers");
s.expr = expr();
}
offset(s);
}
/// list x: type until (e | eol) [offset e]
void list(Stmt &s) {
s.name = new_name("a name");
expect_punct(":");
s.type = type();
if ((s.type.read == ReadType::Rest) || (s.type.read == ReadType::Symbol))
fail(ErrorCode::SpecType,
"a list cannot be of 'rest' or of an alphabet type");
expect_keyword("until");
if (accept("eol"))
s.until_eol = true;
else
s.sentinel = expr();
offset(s);
}
/// type t = type
void type_def(Stmt &s) {
s.name = defined("a type name");
if (is_base_type(s.name))
fail(ErrorCode::SpecNameConflict, "'" + s.name + "' is a base type");
if (named_type(s.name) != nullptr)
fail(ErrorCode::SpecNameConflict,
"the type '" + s.name + "' is already defined");
expect_punct("=");
s.type = type();
const TypeName t{.name = s.name, .def = &s};
types_.push_back(t);
types_all_.push_back(t);
}
/// line and find: a pattern
void line_pattern(Stmt &s) { s.pattern = pattern(); }
/// skip line, or skip e lines
void skip(Stmt &s) {
if (accept("line")) {
s.expr = integer(1);
} else {
s.expr = expr();
expect_keyword("lines");
}
}
/// index e
void index(Stmt &s) { s.expr = expr(); }
/// Terminating tokens: "a" "b" ... [store x]
void until(Stmt &s) {
s.has_until = true;
s.words.push_back(string("a terminating token"));
while (peek().kind == Token::Kind::Str)
s.words.push_back(next().text);
if (accept("store"))
s.store = new_name("a name");
}
/// repeat (e | until eof | until tokens) [as name] { ... }
void repeat(Stmt &s) {
s.id = repeats_++;
if (accept("until")) {
if (accept("eof"))
s.until_eof = true;
else
until(s);
} else {
s.expr = expr();
}
if (accept("as"))
s.list = new_name("a name for the records");
repeat_start_ = true;
s.body = block();
repeat_start_ = false;
if (!s.body.empty() && (s.body[0]->kind == Stmt::Kind::Each))
s.each = s.body[0]->name;
}
/// each x, first in the body of a repeat
void each(Stmt &s) {
if (!each_ok_)
fail(ErrorCode::SpecPlacement,
"'each' must be the first statement of a repeat, and comes at most "
"once");
s.name = new_name("a name for each record");
}
/// instances { ... }, at the top level only
void instances(Stmt &s) {
if (depth_ > 0)
fail(ErrorCode::SpecPlacement,
"'instances' must be at the top level of the spec");
s.body = block();
}
/// sections [until tokens] [skip other] { "pattern" [optional] [last]
/// [{...}] ... }
void sections(Stmt &s) {
for (;;) {
if (accept("until")) {
until(s);
} else if (accept("skip")) {
expect_keyword("other");
s.skip_other = true;
} else {
break;
}
}
expect_punct("{");
while (!punct("}")) {
if (peek().kind == Token::Kind::End)
fail(ErrorCode::SpecSyntax, "missing '}'");
// The arm owns the errors found while parsing it
Arm a;
a.line = peek().line;
const std::size_t outer = std::exchange(owner_line_, a.line);
a.pattern = pattern();
a.optional = accept("optional");
a.last = accept("last");
if (punct("{"))
a.body = block();
owner_line_ = outer;
s.arms.push_back(std::move(a));
}
next();
}
/// if e { ... } [else (if ... | { ... })]
void conditional(Stmt &s) {
s.expr = expr();
s.body = block();
if (!accept("else"))
return;
if (keyword("if")) {
// A chain of else if nests like blocks
enter(depth_, "blocks");
s.orelse.push_back(statement());
depth_--;
} else {
s.orelse = block();
}
}
/// let x = e
void let(Stmt &s) {
s.name = defined("a name");
expect_punct("=");
s.expr = expr();
}
/// check e ["message"]
void check(Stmt &s) {
s.expr = expr();
if (peek().kind == Token::Kind::Str)
s.message = next().text;
}
/*
* Expressions, by precedence climbing
*/
Node<Expr> expr() { return binary(0); }
/// Return the binary operator `t` with its precedence, or nothing if `t`
/// is none
static std::optional<BinaryOp> binary_op(const Token &t) {
struct Entry {
std::string_view text;
BinaryOp op;
};
using O = Expr::Op;
static constexpr std::array<Entry, 13> ops = {{
{"||", {O::Or, 1}},
{"&&", {O::And, 2}},
{"==", {O::Eq, 3}},
{"!=", {O::Ne, 3}},
{"<", {O::Lt, 4}},
{">", {O::Gt, 4}},
{"<=", {O::Le, 4}},
{">=", {O::Ge, 4}},
{"+", {O::Add, 5}},
{"-", {O::Sub, 5}},
{"*", {O::Mul, 6}},
{"/", {O::Div, 6}},
{"%", {O::Mod, 6}},
}};
if (t.kind != Token::Kind::Punct)
return std::nullopt;
const auto o = std::ranges::find(ops, t.text, &Entry::text);
if (o == ops.end())
return std::nullopt;
return o->op;
}
/// Binary operators of at least precedence `min`, left-associative
Node<Expr> binary(int min) {
Node<Expr> lhs = unary();
// Each operator adds a level above lhs
std::size_t levels = 0;
for (;;) {
const std::optional<BinaryOp> op = binary_op(peek());
if (!op || (op->precedence < min))
break;
enter(nesting_, "expressions");
levels++;
auto e = make_node<Expr>(Expr::Kind::Binary);
e->op = op->op;
e->s = next().text;
e->args.push_back(std::move(lhs));
e->args.push_back(binary(op->precedence + 1));
lhs = std::move(e);
}
nesting_ -= levels;
return lhs;
}
Node<Expr> unary() {
enter(nesting_, "expressions");
Node<Expr> e;
if (punct("-") || punct("!")) {
e = make_node<Expr>(Expr::Kind::Unary);
e->op = punct("-") ? Expr::Op::Neg : Expr::Op::Not;
e->s = next().text;
e->args.push_back(unary());
} else {
e = power();
}
nesting_--;
return e;
}
/// a ^ b, which binds tighter than unary operators and groups to the
/// right; b is a unary expression, so 2^-1 is 2^(-1)
Node<Expr> power() {
Node<Expr> e = postfix();
if (!punct("^"))
return e;
auto x = make_node<Expr>(Expr::Kind::Binary);
x->op = Expr::Op::Pow;
x->s = next().text;
x->args.push_back(std::move(e));
x->args.push_back(unary());
return x;
}
/// Indices and fields a[i][j].f...
Node<Expr> postfix() {
Node<Expr> e = primary();
// Each index or field adds a level above e
std::size_t levels = 0;
while (punct("[") || punct(".")) {
const bool field = punct(".");
next();
enter(nesting_, "expressions");
levels++;
auto x = make_node<Expr>(field ? Expr::Kind::Field : Expr::Kind::Index);
x->args.push_back(std::move(e));
if (field) {
x->s = ident("a field name");
} else {
x->args.push_back(expr());
expect_punct("]");
}
e = std::move(x);
}
nesting_ -= levels;
return e;
}
/// Check that call `e` on reader-spec line `line` names a function with
/// suitable arguments
void check_call(const Expr &e, std::size_t line) const {
const String &f = e.s;
const std::size_t n = e.args.size();
bool ok = false;
if (f == "defined")
ok = (n == 1) && (e.args[0]->kind == Expr::Kind::Name);
else if ((f == "count") || (f == "size"))
ok = (n == 1);
else if ((f == "min") || (f == "max") || (f == "sum") || (f == "distinct"))
ok = (n >= 1);
else if ((f == "all") || (f == "any"))
fail_on(ErrorCode::SpecFunction, line,
"'" + f + "' needs the form " + f + "(x in a: e)");
else
fail_on(ErrorCode::SpecFunction, line, "unknown function '" + f + "'");
if (!ok)
fail_on(ErrorCode::SpecFunction, line,
"wrong arguments for function '" + f + "'");
}
/// Return the value of the Gecode constant `n` on reader-spec line `line`
[[nodiscard]] std::int64_t gecode_constant(const String &n,
std::size_t line) const {
struct Constant {
std::string_view name;
std::int64_t value;
};
// Gecode's Int::Limits::max and min, fixed in Iris 0.1
static constexpr std::array<Constant, 2> constants = {{
{"gecode.int_max", 2147483646},
{"gecode.int_min", -2147483646},
}};
String known;
for (const Constant &c : constants) {
if (n == c.name)
return c.value;
known += (known.empty() ? "" : ", ") + String(c.name);
}
fail_on(ErrorCode::SpecConstant, line,
"unknown Gecode constant '" + n + "' (known: " + known + ")");
}
/// Test whether a comprehension for function `f` comes next, after "f("
[[nodiscard]] bool at_comprehension(std::string_view f) const {
if ((f != "all") && (f != "any") && (f != "sum") && (f != "count"))
return false;
if ((peek().kind != Token::Kind::Ident) || (p_ + 1 >= toks_.size()))
return false;
const Token &t = toks_[p_ + 1];
return (t.kind == Token::Kind::Ident) && (t.text == "in");