37#include <unordered_map>
49 int triangle_from(
bool upper,
bool diag,
int i) {
50 return upper ? (diag ?
i :
i+1) : 0;
54 int triangle_to(
bool upper,
bool diag,
int n,
int i) {
55 return upper ? n : (diag ?
i+1 :
i);
66 default:
return "a symbol";
83 : low(false), high(false), lo(0), hi(0) {}
85 bool contains(
long long int v)
const {
86 return (!low || (v >= lo)) && (!high || (
v <= hi));
89 bool bounded(
void)
const {
102 Domain(
const Type& t,
const Range& r)
103 : type(t), range(
r) {}
105 std::string describe(
void)
const {
106 std::string
d = Instance::describe(type.read);
108 d =
"an int in " + (range.low ? std::to_string(range.lo) :
"") +
".." +
109 (range.high ? std::to_string(range.hi) :
"");
110 return type.name.
empty() ? d : type.name +
" (" + d +
")";
118 Interpreter(
const SpecImpl& s,
const std::string& text,
const std::string& origin,
const Params& p,
bool c)
119 : Evaluator(s, text, origin), params(p), trailing(false), catalog(
c), instance_count(-1), select(false),
120 number(-1), current(0), instance_frame(0), selected(false), idle(0), token_start(0) {}
123 static void declared_params(
const Block& b, Seq<std::string>& out) {
124 for (
const std::unique_ptr<Stmt>& s : b) {
128 out.push(
"instance");
129 declared_params(s->body, out);
130 declared_params(s->orelse, out);
131 for (
const Arm& a : s->arms)
132 declared_params(
a.body, out);
137 void run(DataImpl& d) {
139 Seq<std::string> known;
140 declared_params(spec.body, known);
141 for (
int i=0;
i<params.
size();
i++) {
143 for (
const std::string& k : known)
144 found = found || (k == params.name(i));
146 throw Error(spec.origin +
": the spec has no parameter '" + params.name(i) +
"'");
151 if (!trailing && !selected && in.skip_to_data())
152 fail(
"unexpected input after the end of the spec: " +
quote(in.rest()));
157 n.name =
"instances";
158 n.value =
Value::integer(instance_count < 0 ? 1 : instance_count);
159 c.push(std::move(n));
160 if (names.leaves() > 0) {
164 c.push(std::move(e));
166 if (failures.leaves() > 0) {
170 c.push(std::move(e));
172 frames[0] = std::move(c);
174 for (Entry& e : frames[0])
177 d.entries.push(std::move(e));
183 const Params& params;
185 Seq<long long int> rows;
201 long long int number;
207 std::string deferred_error;
209 std::string deferred_message;
216 static const long long int max_idle = 1LL << 20;
218 std::unordered_map<const Stmt*, Range> type_ranges;
224 static const int max_spare = 8;
227 void work(
long long int k) {
230 fail(
"too many repetitions or empty rows without reading input");
238 static bool convert(
ReadType type,
const std::string& t, Value& v) {
257 [[noreturn]]
void expected(
const std::string& what) {
258 if (!in.skip_to_data())
259 fail(
"expected " + what +
", found end of input");
260 fail(
"expected " + what +
", found " +
quote(in.peek()));
270 if (!in.skip_to_data())
272 token_start = in.pos;
278 std::string t = in.token();
279 if (convert(type, t, v))
281 in.pos = token_start;
285 Value scalar(
ReadType type,
const std::string& what) {
292 Value scalar(
const Domain& d) {
294 if (!
scan(
d.type.read, v))
295 expected(
d.describe());
300 long long int int_scalar(
const Domain& d) {
302 if (!in.skip_to_data())
303 expected(
d.describe());
304 token_start = in.pos;
305 if (!in.int_token(x))
306 expected(
d.describe());
310 static bool fits(
const Domain& d,
const Value& v) {
315 [[noreturn]]
void outside(
const Domain& d,
size_t from,
size_t to) {
316 std::string t = in.text.substr(from, to-from);
318 fail(
"expected " +
d.describe() +
", found " +
quote(t));
321 Value value(
const Domain& d) {
324 outside(d, token_start, in.pos);
328 void push(
const Domain& d, Value& v) {
330 long long int x = int_scalar(d);
331 if (!
d.range.contains(x))
332 outside(d, token_start, in.pos);
340 Range
range(
const Type& t) {
341 if (t.named !=
nullptr) {
342 auto r = type_ranges.find(t.named);
343 if (r == type_ranges.end())
344 fail(
"the type '" + t.name +
"' is not defined yet");
355 if (t.exclusive && !
sub(
r.hi, 1,
r.hi))
356 fail(
"integer overflow in the upper bound of a range");
361 long long int bound(
const Expr& e) {
367 long long int x = bound(*e.args[0]);
369 fail(
"integer overflow in '-'");
373 const Value&
v = ref(e, tmp);
375 fail(
"the bounds of a range must be integers");
380 Value array(
const Domain& d,
const Seq<long long int>& dims) {
382 long long int size = 1;
383 for (
long long int n : dims) {
386 fail(
"array is too large");
390 long long int rows = 1;
391 for (
int k=0; (k < dims.size()) && (dims[k] > 0); k++) {
403 for (
long long int i=0;
i<
size;
i++)
406 }
catch (
const ShapeError& e) {
407 fail(e, context.back().text);
409 v.depth = dims.size();
411 long long int lists = 1;
412 for (
long long int n : dims) {
413 v.lengths.push(Seq<int>());
414 v.lengths.back().reserve(
static_cast<int>(lists));
415 for (
long long int i=0;
i<
lists;
i++)
416 v.lengths.back().push(
static_cast<int>(n));
429 Value layout(
const Domain& d,
long long int n,
long long int m,
const std::string& f,
long long int o) {
430 if (f ==
"FULL_MATRIX") {
431 Seq<long long int> dims;
432 dims.push(n); dims.push(m);
433 Value
v = array(d, dims);
438 fail(
"layout " + f +
" needs a square matrix");
441 if ((f ==
"UPPER_ROW") || (f ==
"LOWER_COL")) {
442 upper =
true; diag =
false;
443 }
else if ((f ==
"LOWER_ROW") || (f ==
"UPPER_COL")) {
444 upper =
false; diag =
false;
445 }
else if ((f ==
"UPPER_DIAG_ROW") || (f ==
"LOWER_DIAG_COL")) {
446 upper =
true; diag =
true;
447 }
else if ((f ==
"LOWER_DIAG_ROW") || (f ==
"UPPER_DIAG_COL")) {
448 upper =
false; diag =
true;
450 fail(
"unknown matrix layout " +
quote(f));
453 fail(
"matrix is too large");
454 int nn =
static_cast<int>(n);
459 for (
int i=0;
i<nn;
i++)
460 for (
int j=triangle_from(upper, diag, i); j<triangle_to(upper, diag, nn, i); j++)
467 v.lengths.push(Seq<int>());
468 v.lengths[0].push(nn);
469 v.lengths.push(Seq<int>());
470 for (
int i=0;
i<nn;
i++)
471 v.lengths[1].push(nn);
472 for (
int k=0; k<nn*nn; k++)
475 for (
int i=0;
i<nn;
i++)
476 for (
int j=triangle_from(upper, diag, i); j<triangle_to(upper, diag, nn, i); j++, k++)
478 v.ints[
i*nn+j] = t.ints[k];
v.ints[j*nn+
i] = t.ints[k];
480 v.reals[
i*nn+j] = t.reals[k];
v.reals[j*nn+
i] = t.reals[k];
486 void offset(Value& v,
long long int o) {
487 for (
long long int& x :
v.ints)
489 fail(
"integer overflow when applying offset");
490 for (
double& x :
v.reals)
491 x +=
static_cast<double>(o);
495 long long int offset(
const Stmt& s) {
498 Value o =
eval(*s.offset);
500 fail(
"offset must be an integer");
505 void store(
const Stmt& s, Value&& v) {
507 set(s.name, std::move(v));
514 const Pattern::Item* item;
524 bool match(
const Pattern& p) {
526 if (!matches(p, caps))
533 bool matches(
const Pattern& p, Seq<Capture>& caps) {
535 if (in.pos >= in.text.size())
537 std::string l = in.rest();
539 for (
int k=0; k<p.items.size(); k++)
540 if (!match_item(p, k, l, c, caps))
542 while ((c < l.size()) &&
is_blank(l[c]))
544 return c == l.size();
548 void take(
const Seq<Capture>& caps) {
550 for (
const Capture& x : caps)
551 if (!x.item->text.empty() && (x.item->text !=
"_"))
552 set(x.item->text, Value(x.value));
553 for (
const Capture& x : caps)
554 if (x.item->type.ranged()) {
555 Domain
d(x.item->type,
range(x.item->type));
556 if (!fits(d, x.value))
557 outside(d, in.pos + x.from, in.pos + x.to);
563 bool match_item(
const Pattern& p,
int k,
const std::string& l,
size_t& c, Seq<Capture>& caps) {
564 typedef Pattern::Item Item;
565 const Item& it = p.items[k];
567 case Item::Kind::SPACE: {
569 while ((c < l.size()) &&
is_blank(l[c]))
571 return !it.required || (
c > c0) || (c == l.size());
573 case Item::Kind::LIT:
574 if (l.compare(c, it.text.size(), it.text) != 0)
578 case Item::Kind::ANY:
581 case Item::Kind::VAR: {
582 std::string want = variable(it.text);
583 if (want.empty() || (l.compare(c, want.size(), want) != 0))
588 case Item::Kind::CAP: {
591 if ((k+1 < p.items.size()) && (p.items[k+1].kind == Item::Kind::LIT))
592 stop = p.items[k+1].text[0];
594 c = capture_end(it.type.read, l, c, stop);
595 std::string t = l.substr(c0, c-c0);
596 while (!t.empty() &&
is_blank(t[t.size()-1]))
599 if (!convert(it.type.read, t, x.value))
603 x.to = c0 + t.size();
604 caps.push(std::move(x));
612 std::string variable(
const std::string& n) {
613 const Value*
v = lookup(n);
615 fail(
"'" + n +
"' is not defined yet");
619 return std::to_string(
v->ints[0]);
620 fail(
"'" + n +
"' must be a string or an integer");
624 static size_t capture_end(
ReadType type,
const std::string& l,
size_t c,
char stop) {
629 while ((c < l.size()) && !
is_blank(l[c]) && (l[c] != stop))
638 void exec(
const Block& b) {
639 for (
const std::unique_ptr<Stmt>& s : b)
643 void exec(
const Stmt& s) {
647 for (
const std::string& w : s.words) in.comments.push(w);
650 for (
const std::string& w : s.words) in.stops.push(w);
669 exec(truth(
eval(*s.expr)) ? s.body : s.orelse);
682 void param(
const Stmt& s) {
689 if (!params.get(s.name, v)) {
691 fail(
"parameter '" + s.name +
"' must be given");
692 set(s.name, std::move(d));
699 }
else if (!s.expr) {
702 fail(
"parameter '" + s.name +
"' must be " + (
d.type ==
ValueType::INT ?
"an integer" :
"a number") +
703 ", not " +
quote(v));
708 void read(
const Stmt& s) {
709 context.push(Context(s.name));
710 long long int o = offset(s);
711 Domain
d(s.type,
range(s.type));
712 if (s.dims.empty()) {
715 store(s, std::move(v));
717 Seq<long long int> dims;
718 for (
const std::unique_ptr<Expr>& e : s.dims)
719 dims.push(
count(*e,
"array size"));
721 store(s, symbols(s.type, dims[0], dims[1]));
723 Value
f =
eval(*s.expr);
725 fail(
"matrix layout must be a string");
726 store(s, layout(d, dims[0], dims[1],
f.strs[0], o));
728 Value
v = array(d, dims);
730 store(s, std::move(v));
737 void list(
const Stmt& s) {
738 context.push(Context(s.name));
740 if (!spare.empty()) {
741 v = std::move(spare.back());
746 Domain
d(s.type,
range(s.type));
752 }
catch (
const ShapeError& e) {
755 v.lengths[0][0] =
v.leaves();
756 offset(v, offset(s));
758 store(s, std::move(v));
761 void list_line(
const Domain& d, Value& v) {
764 if (!in.skip_to_line_data() && !in.skip_to_data())
765 fail(
"expected a line of values, found end of input");
766 while (in.skip_to_line_data())
770 static bool same_number(
const Value& x,
const Value& y) {
772 return x.ints[0] == y.ints[0];
775 return x.as_double() == y.as_double();
778 void ints_until(
const Domain& d,
long long int stop, Value& v) {
781 long long int x = int_scalar(d);
784 if (!
d.range.contains(x))
785 outside(d, token_start, in.pos);
790 void list_until(
const Stmt& s,
const Domain& d, Value& v) {
792 ints_until(d, s.sentinel->i, v);
797 fail(
"list terminator must be a single value");
802 fail(
"a list of words needs a string or integer terminator");
804 ints_until(d,
stop.ints[0], v);
811 if (!in.skip_to_data())
812 fail(
"expected " +
quote(
stop.strs[0]) +
", found end of input");
813 if (in.peek() ==
stop.strs[0]) {
820 if (same_number(x, stop))
823 outside(d, token_start, in.pos);
830 void line(
const Stmt& s) {
831 if (match(s.pattern))
833 fail(
"expected a line matching \"" + s.pattern.source +
"\"" +
834 (in.pos >= in.text.size() ?
", found end of input" :
""));
837 void find(
const Stmt& s) {
838 while (!match(s.pattern)) {
839 if (!in.skip_to_data())
840 fail(
"no line matching \"" + s.pattern.source +
"\" found");
845 void skip_lines(
const Stmt& s) {
846 long long int n =
count(*s.expr,
"number of lines");
847 for (
long long int k=0; k<n; k++) {
848 if (!in.skip_to_data())
849 fail(
"cannot skip a line at end of input");
854 void expect(
const Stmt& s) {
855 for (
const std::string& w : s.words) {
856 if (!in.skip_to_data())
857 fail(
"expected " +
quote(w) +
", found end of input");
859 std::string t = in.token();
862 fail(
"expected " +
quote(w) +
", found " +
quote(t));
867 void index(
const Stmt& s) {
869 fail(
"'index' must be inside a repeat");
870 long long int want = rows.back() +
count(*s.expr,
"index base");
874 if (
v.ints[0] != want) {
876 fail(
"expected index " + std::to_string(want) +
", found " + std::to_string(
v.ints[0]));
881 bool until(
const Stmt& s) {
882 if (!s.has_until || !in.skip_to_data())
884 std::string t = in.peek();
885 for (
const std::string& w : s.words)
888 if (!s.store.empty())
894 std::string until_text(
const Stmt& s)
const {
896 for (
int k=0; k<s.words.size(); k++)
897 r += (k > 0 ?
" or " :
"") +
quote(s.words[k]);
908 bool may_be_skipped(
void)
const {
916 return current != number;
917 return instance_name(frames[instance_frame]) != want;
928 void reject(
const std::string& e,
const std::string& message) {
929 if (!may_be_skipped())
931 if (deferred_error.empty()) {
933 deferred_message = message;
938 void check(
const Stmt& s) {
941 reject(e, s.message.empty() ?
"check on line " + std::to_string(s.line) +
" of the spec" : s.message);
946 Seq<Entry> instance(
const Block& b,
int n) {
948 deferred_error.clear();
949 deferred_message.clear();
950 context.push(Context(
"instance", n));
951 frames.push(Seq<Entry>());
952 instance_frame = frames.size()-1;
955 }
catch (
const Error& e) {
957 if (deferred_error.empty())
959 throw Error(std::string(e.what()) +
"\n after an earlier error in the instance: " + deferred_message);
961 Seq<Entry>
f = std::move(frames.back());
969 static std::string instance_name(
const Seq<Entry>& f) {
970 for (
const Entry& e : f)
971 if ((e.name ==
"name") && (e.value.depth == 0)) {
973 if (e.value.type ==
ValueType::INT)
return std::to_string(e.value.ints[0]);
975 return std::string();
995 select = params.get(
"instance", want);
997 bool numbered = select &&
parse_int(want, k);
998 if (numbered && (k < 1))
999 fail(
"instance numbers start at 1");
1000 number = numbered ? k : -1;
1002 while (in.skip_to_data()) {
1004 size_t start = in.pos;
1005 Seq<Entry>
f = instance(s.body, n);
1007 if (in.pos == start)
1008 fail(
"an instance must read some input");
1009 std::string name = instance_name(f);
1013 }
else if (select ? ((number > 0) ? (number == n) : (name == want)) : (n == 1)) {
1016 if (!deferred_error.empty())
1017 throw Error(deferred_error);
1019 set(e.name, std::move(e.value));
1030 for (
const std::string& v : names.strs)
1031 all = all && !
v.empty();
1033 names.lengths[0][0] = n;
1036 bool failed =
false;
1037 for (
const std::string& m : failures.strs)
1038 failed = failed || !m.empty();
1040 failures.lengths[0][0] = n;
1046 fail(
"the file contains no instance");
1048 fail(
"there is no instance " +
quote(want) +
" (the file has " + std::to_string(n) +
" instance" +
1049 (n == 1 ?
"" :
"s") +
")");
1051 fail(
"the file has " + std::to_string(n) +
1052 " instances; select one with the parameter instance (a number from 1, or a name)");
1063 void repeat(
const Stmt& s) {
1066 long long int n = s.expr ?
count(*s.expr,
"repeat count") : -1;
1067 Seq<Column> columns;
1068 for (
const Decl& d : names)
1069 columns.push(Column(
d.type,
d.depth));
1074 for (
long long int r=0; (n < 0) || (
r < n);
r++) {
1075 if (s.until_eof && !in.skip_to_data())
1079 if (s.has_until && !in.skip_to_data())
1080 fail(
"expected " + until_text(s) +
" before end of input");
1081 context.push(Context(
"repeat", r+1, n));
1082 frames.push(std::move(frame));
1084 starts.ints.push(
static_cast<long long int>(in.next_data()));
1085 size_t start = in.pos;
1087 if (in.pos == start) {
1089 fail(
"the body of a repeat until read no input");
1093 Seq<Entry>&
f = frames.back();
1094 for (
int i=0;
i<names.
size();
i++) {
1097 if (x.name == names[i].name)
1103 columns[
i].add(e->value);
1104 }
catch (
const ShapeError& err) {
1105 fail(
"'" + names[i].name +
"' " + err.msg);
1110 if ((e.value.depth > 0) && (spare.size() < max_spare))
1111 spare.push(std::move(e.value));
1112 frame = std::move(frames.back());
1118 for (
int i=0;
i<names.
size();
i++)
1120 set(names[i].name, columns[i].finish());
1121 }
catch (
const ShapeError& err) {
1122 fail(
"'" + names[i].name +
"' " + err.msg);
1124 starts.lengths[0][0] = starts.ints.size();
1138 for (
int k=0; k<s.arms.size(); k++)
1140 std::string end =
"the end of the input";
1144 if (!in.skip_to_data()) {
1148 Place at = in.here();
1150 end = until_text(s);
1156 for (
int k=0; (found < 0) && (k < s.arms.size()); k++) {
1158 if (matches(s.arms[k].pattern, caps))
1166 std::string expected;
1167 for (
int k=0; (k < s.arms.size()) && (k < 6); k++)
1168 expected += (k > 0 ?
", \"" :
"\"") + s.arms[k].pattern.source +
"\"";
1169 if (s.arms.size() > 6)
1170 expected +=
", ...";
1171 fail(
"unexpected line; expected one of " + expected);
1173 const Arm&
a = s.arms[found];
1174 if (seen[found] > 0) {
1175 std::string m =
"a second line matching \"" +
a.pattern.source +
"\"";
1176 reject(located(m +
" (the first is on line " + std::to_string(seen[found]) +
")", at), m);
1178 seen[found] = at.line;
1181 context.push(Context(
"section \"" +
a.pattern.source +
"\""));
1185 end =
"the line matching \"" +
a.pattern.source +
"\"";
1190 for (
int k=0; k<s.arms.size(); k++)
1191 if (!s.arms[k].optional && (seen[k] == 0)) {
1192 std::string p =
"\"" + s.arms[k].pattern.source +
"\"";
1193 std::string e = located(
"expected a line matching " + p +
" before " + end, end_at);
1195 if (!in.skip_to_data())
1197 reject(e,
"no line matching " + p);
1203 Value symbols(
const Type& t,
long long int r,
long long int c) {
1205 fail(
"array is too large");
1209 for (
long long int i=0;
i<
r;
i++)
1210 for (
long long int j=0; j<
c; j++) {
1214 if (in.pos >= in.text.size())
1215 fail(
"expected a symbol for row " + std::to_string(i+1) +
", column " + std::to_string(j+1) +
1216 ", found end of input");
1217 ch = in.text[in.pos];
1218 if (t.strip.find(ch) == std::string::npos)
1220 in.advance(in.pos+1);
1222 size_t k = t.alphabet.find(ch);
1223 if (t.blank.find(ch) != std::string::npos) {
1225 }
else if (k != std::string::npos) {
1227 if (!
add(
static_cast<long long int>(k), t.base, v))
1228 fail(
"integer overflow in the value of a symbol");
1231 fail(
"unexpected symbol " +
quote(std::string(1, ch)) +
", expected one of " +
quote(t.alphabet + t.blank));
1233 in.advance(in.pos+1);
1238 g.lengths.push(Seq<int>());
1239 g.lengths[0].push(
static_cast<int>(r));
1240 g.lengths.push(Seq<int>());
1241 for (
long long int i=0;
i<
r;
i++)
1242 g.lengths[1].push(
static_cast<int>(c));
1251 std::shared_ptr<DataImpl> d = std::make_shared<DataImpl>();
1252 Interpreter i(*
this, input, o, p, catalog);
int size(void) const
Return size of array (number of elements).
Named values read from an instance.
Evaluator of the expressions of a spec on an input.
Values for the param statements of a spec.
Data read(const std::string &input, const std::string &origin, const Params &p, bool catalog) const
Read input from origin.
static Value real(double v)
Float scalar.
static Value string(const std::string &v)
String scalar.
static Value integer(long long int v)
Integer scalar.
static const IntSet empty
Empty set.
void stop(Support::Timer &timer, std::ostream &os)
Get time since start of timer and print user friendly time information.
void check(const FloatVal &n, const char *l)
Check whether float n is a valid number, otherwise throw out of limits exception with information l.
Reading instance files described by reader specs.
bool add(long long int x, long long int y, long long int &r)
Store x + y in r unless it overflows.
@ FLOAT
Floating point numbers.
std::string record_starts(int id)
Return the name of the record starts of the repeat numbered id.
bool parse_float(const std::string &t, double &v)
Parse decimal float t into v, and return whether it is one.
bool sub(long long int x, long long int y, long long int &r)
Store x - y in r unless it overflows.
void declared(const Block &b, Seq< Decl > &out)
Add the names that block b may define to out.
ReadType
What a declaration or a capture reads.
@ NUM
An integer if possible, otherwise a float.
@ REST
The rest of the line as a string.
@ SYMBOL
A character of an alphabet, as an integer.
@ WORD
A token as a string.
bool parse_int(const std::string &t, long long int &v)
Parse decimal integer t into v, and return whether it is one.
size_t number_end(const std::string &s, size_t p, bool sign, bool real)
Return where the number starting at position p of s ends.
std::string quote(const std::string &s)
Return s in single quotes, with newlines and tabs escaped.
bool is_blank(char c)
Test whether c is white space within a line.
Seq< std::unique_ptr< Stmt > > Block
Sequence of statements.
bool is_internal(const std::string &n)
Test whether n is a name used internally (see record_starts).
RelTest holds(VX x, VX y)
Test whether x and y are equal.
ExecStatus scan(Space &home, IdxViewArray< VA > &iv, VB x0, VC x1, Propagator &p, RelTest rt)
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode toplevel namespace
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void range(Home home, const IntVarArgs &x, SetVar y, SetVar z)
Post constraint .
Gecode::FloatVal a(-8, 5)
Expr eval(const LinInstr *pc, Expr reg[])
Evaluate linear instructions.
Case sections("Read::Sections", [](void) { const char *spec="sections until \"EOF\" {\n" " \"N : {n:int}\"\n" " \"DATA\" { x[n]: int }\n" " \"NAME : {name:word}\" optional\n" "}\n";Data d=read(spec, "NAME : a\nN: 2\nDATA\n5 6\nEOF\n");CHECK(d.integer("n")==2);CHECK(equal(d.ints("x"), {5, 6}));CHECK(d.text("name")=="a");CHECK(read(spec, "N : 1\nDATA\n4").ints("x").size()==1);CHECK(fails(spec, "N : 1\nXYZZY_PLUGH\n", "expected one of"));CHECK(read("sections skip other { \"v: {v:int}\" }", "junk\nv: 3\nmore junk\n").integer("v")==3);CHECK(fails(spec, "N : 1\nEOF\n", "test.txt:2:1: expected a line matching \"DATA\" before 'EOF'"));CHECK(fails(spec, "NAME : a\n", "expected a line matching \"N : {n:int}\" before the end of the input"));CHECK(fails(spec, "N : 1\nDATA\n4\nN : 2\n", "test.txt:4:1: a second line matching \"N : {n:int}\" (the first is on line 1)"));CHECK(fails(spec, "NAME : a\nN : 1\nDATA\n4\nNAME : b\n", "a second line matching \"NAME : {name:word}\""));const char *last="sections { \"a {a:int}\" \"end\" last } y: int";CHECK(read(last, "a 1\nend\n5").integer("y")==5);CHECK(fails(last, "a 1\n5", "expected one of"));CHECK(fails(last, "a 1", "expected a line matching \"end\" before the end of the input"));CHECK(fails(last, "end\n5", "test.txt:1:1: expected a line matching \"a {a:int}\" before the line matching \"end\""));const char *optional_last="sections { \"a {a:int}\" \"end\" optional last }";CHECK(read(optional_last, "a 1").integer("a")==1);CHECK(read(optional_last, "a 1\nend").integer("a")==1);return true;})
Case lists("Read::Lists", [](void) { Data d=read("list a: int until 0 list b: int until eol list c: int until eol list w: word until \"END\"", "1 2\n 3 0 4 5\n6 7\nx y END");CHECK(equal(d.ints("a"), {1, 2, 3}));CHECK(equal(d.ints("b"), {4, 5}));CHECK(equal(d.ints("c"), {6, 7}));CHECK(d.size("w")==2);CHECK(d.text("w", {1})=="y");return true;})
Case instances("Read::Instances", [](void) { const char *spec="comment \"+++\"\n" "instances {\n" " sections skip other {\n" " \"instance {name:word}\" optional\n" " \"{n:int}\" last { x[n]: int }\n" " }\n" "}\n";const char *text="header text\ninstance a\n2\n1 2\n+++\n" "instance b\ndescription\n1\n7\n+++ end\n";Params by_name, by_number, bad;by_name.set("instance", "b");by_number.set("instance", "1");bad.set("instance", "3");CHECK(equal(read(spec, text, by_name).ints("x"), {7}));CHECK(equal(read(spec, text, by_number).ints("x"), {1, 2}));CHECK(read(spec, text, by_number).text("name")=="a");CHECK(fails([&](void) {(void) read(spec, text, bad);}, "there is no instance '3' (the file has 2 instances)"));CHECK(fails(spec, text, "the file has 2 instances; select one"));CHECK(equal(read(spec, "3\n4 5 6\n").ints("x"), {4, 5, 6}));Spec s=Spec::parse(spec, "s");Data c=s.catalog_text(text);CHECK(c.integer("instances")==2);CHECK(c.text("name", {1})=="b");Data c1=s.catalog_text("3\n4 5 6\n");CHECK((c1.integer("instances")==1) &&!c1.has("name"));CHECK(Spec::parse("a: int", "s").catalog_text("5").integer("instances")==1);CHECK(fails(spec, "1\n2\njunk\n", "expected a line matching \"{n:int}\""));CHECK(fails("instances { a: int }", "", "the file contains no instance"));CHECK(fails([](void) {(void) Spec::parse("instances { a: int } b: int", "s");}, "'instances' must be the last statement"));CHECK(fails([](void) {(void) Spec::parse("repeat 2 { instances { a: int } }", "s");}, "'instances' must be at the top level"));CHECK(fails("instances { if 0 > 1 { a: int } }", "5", "an instance must read some input"));return true;})
Data read(const char *spec, const char *text, const Params &p=Params())
Read text with the spec spec.
Gecode::IntArgs i({1, 2, 3, 4})