-
Notifications
You must be signed in to change notification settings - Fork 2
/
ArgSpec.mag
448 lines (423 loc) · 12.2 KB
/
ArgSpec.mag
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
declare type PGGArgSpec;
declare attributes PGGArgSpec: is_valid;
declare type PGGAttr_ArgSpecSubs: PGGAttr;
declare attributes PGGAttr_ArgSpecSubs: subs;
intrinsic PGG_Parsable(A :: PGGArgSpec, x) -> BoolElt, .
{Tries to parse x according to A.}
return A`is_valid(x);
end intrinsic;
intrinsic PGG_Parsable(A :: PGGArgSpec, x :: PGGAttr_ArgSpecSubs) -> BoolElt, .
{"}
return PGG_Parsable(A, x`subs);
end intrinsic;
intrinsic PGG_Parse(A :: PGGArgSpec, x) -> .
{Parses x according to A}
ok, y := PGG_Parsable(A, x);
error if not ok, "could not parse", x;
return y;
end intrinsic;
intrinsic PGG_ArgSpec(is_valid :: UserProgram) -> PGGArgSpec
{The argspec with the given is_valid function.}
A := New(PGGArgSpec);
A`is_valid := is_valid;
return A;
end intrinsic;
intrinsic PGG_ArgSpec_Union(choices :: [PGGArgSpec]) -> PGGArgSpec
{The argspec for the union of the given choices.}
return PGG_ArgSpec(function (x)
for A in choices do
ok, y := PGG_Parsable(A, x);
if ok then
return true, y;
end if;
end for;
return false, _;
end function);
end intrinsic;
intrinsic PGG_ArgSpec_Transform(A :: PGGArgSpec, tr) -> PGGArgSpec
{The argspec A with its output modified by tr.}
return PGG_ArgSpec(function (x)
ok, y := PGG_Parsable(A, x);
if ok then
return true, tr(y);
else
return false, _;
end if;
end function);
end intrinsic;
intrinsic PGG_ArgSpec_ISA(C) -> PGGArgSpec
{The argspec testing if the input has the given category.}
return PGG_ArgSpec(function (x)
if ISA(ExtendedType(x), C) then
return true, x;
else
return false, _;
end if;
end function);
end intrinsic;
intrinsic PGG_ArgSpec_Literal(X) -> PGGArgSpec
{The argspec matching only X exactly.}
return PGG_ArgSpec(function (x)
if X cmpeq x then
return true, x;
else
return false, _;
end if;
end function);
end intrinsic;
intrinsic PGG_ArgSpec_Attr() -> PGGArgSpec
{The argspec matching a PGGAttr.}
return PGG_ArgSpec(function (x)
ok, a := PGG_Attribute_IsCoercible(x);
if ok then
return true, a;
end if;
end function);
end intrinsic;
intrinsic PGG_ArgSpec_Attr_NoArgs() -> PGGArgSpec
{The argspec matching a PGGAttr with no args. Outputs the name.}
return PGG_ArgSpec(function (x)
ok, a := PGG_Attribute_IsCoercible(x);
if not ok then
return false, _;
end if;
if #Arguments(a) ne 0 then
return false, _;
end if;
return true, Name(a);
end function);
end intrinsic;
intrinsic PGG_ArgSpec_Attr(name :: MonStgElt) -> PGGArgSpec
{The argspec matching the PGGAttr with the given name and no args.}
return PGG_ArgSpec_Attr(name, [], func<args | name>);
end intrinsic;
intrinsic PGG_ArgSpec_AttrList(name :: MonStgElt, arg, transform) -> PGGArgSpec
{The argspec matching the PGGAttr with the given name and multiple args matching arg.}
return PGG_ArgSpec(function (x)
// coerce to a PGGAttr
ok, a := PGG_Attribute_IsCoercible(x);
if not ok then
return false, _;
end if;
// check the name
if Trim(Name(a)) ne name then
return false, _;
end if;
// check the args
xs := [**];
for aa in Arguments(a) do
ok, x := PGG_Parsable(arg, aa);
if ok then
Append(~xs, x);
else
return false, _;
end if;
end for;
// apply the transform
return true, transform(xs);
end function);
end intrinsic;
intrinsic PGG_ArgSpec_AttrSelect(name :: MonStgElt, parg, rarg, transform) -> PGGArgSpec
{The argspec matching the PGGAttr with the given name, and arguments [parg,rarg,parg,rarg,...,parg,rarg] (the final parg is optional). The pargs are predicates, and the rargs are results, so this is like select-else-select-else.}
return PGG_ArgSpec(function (x)
// coerce to a PGGAttr
ok, a := PGG_Attribute_IsCoercible(x);
if not ok then
return false, _;
end if;
// check the name
if Trim(Name(a)) ne name then
return false, _;
end if;
// check the args
args := Arguments(a);
ps := [**];
rs := [**];
for i in [1..#args] do
if i eq #args or ((i mod 2) eq 0) then
ok, x := PGG_Parsable(rarg, args[i]);
if ok then
Append(~rs, x);
else
return false, _;
end if;
else
ok, x := PGG_Parsable(parg, args[i]);
if ok then
Append(~ps, x);
else
return false, _;
end if;
end if;
end for;
// apply the transform
return true, transform(ps, rs);
end function);
end intrinsic;
procedure unpack_vars(~ok, ~a, var, val)
varargs := Arguments(var);
valargs := Arguments(val);
if #varargs gt #valargs then
ok := false;
return;
end if;
vname := Trim(Name(var));
if #vname gt 0 then
a[vname] := val;
end if;
for i in [1..#varargs] do
unpack_vars(~ok, ~a, varargs[i], valargs[i]);
if not ok then
return;
end if;
end for;
ok := true;
return;
end procedure;
function substitute(A, x)
nm := Name(x);
ok, r := IsDefined(A, Trim(nm));
if ok then
if #Arguments(x) eq 0 then
return true, r;
else
return false, _;
end if;
end if;
args := [];
for a in Arguments(x) do
ok, arg := substitute(A, a);
if ok then
Append(~args, arg);
else
return false, _;
end if;
end for;
return true, PGG_Attribute(nm, args);
end function;
intrinsic PGG_ArgSpec_AttrForEach(name :: MonStgElt, arg, transform) -> PGGArgSpec
{The argspec matching a PGGAttr with the given name and three arguments [var,iter,arg] where var specifies a variable (or variables), iter is a list of items, each getting assigned to var and substituted into arg in turn. The output is the list of args.}
varg := PGG_ArgSpec_Attr();
iarg := PGG_ArgSpec_AttrList("", varg, func<x | x>);
return PGG_ArgSpec(function (x)
// coerce to a PGGAttr
ok, a := PGG_Attribute_IsCoercible(x);
if not ok then
return false, _;
end if;
// check the name
if Trim(Name(a)) ne name then
return false, _;
end if;
// check the arguments
args := Arguments(a);
if #args ne 3 then
return false, _;
end if;
ok, var := PGG_Parsable(varg, args[1]);
if not ok then
return false, _;
end if;
ok, iter := PGG_Parsable(iarg, args[2]);
if not ok then
return false, _;
end if;
// iterate
rs := [**];
for y in iter do
A := AssociativeArray();
unpack_vars(~ok, ~A, var, y);
if not ok then
error "unpacking error";
return false, _;
end if;
ok, sa := substitute(A, args[3]);
if not ok then
error "substitution error";
return false, _;
end if;
ok, r := PGG_Parsable(arg, sa);
if not ok then
return false, _;
end if;
Append(~rs, r);
end for;
return true, transform(rs);
end function);
end intrinsic;
intrinsic PGG_ArgSpec_AttrScope(name :: MonStgElt, dfltvar :: MonStgElt, mkval :: UserProgram, inner :: PGGArgSpec) -> PGGArgSpec
{Parses `name[var,inner]` or `name[inner]` (with `var=dfltfar`), substrituting `var` for `mkval(var,inner)` in `inner`.}
return PGG_ArgSpec(function (x)
// coerce to a PGGAttr
ok, a := PGG_Attribute_IsCoercible(x);
if not ok then
return false, _;
end if;
// check the name
if Trim(Name(a)) ne name then
return false, _;
end if;
// check the arguments, either [var, inner] or [inner]
args := Arguments(a);
case #args:
when 1:
var := dfltvar;
inna := args[1];
when 2:
if #Arguments(args[1]) ne 0 then
return false, _;
end if;
var := Name(args[1]);
inna := args[2];
else
return false, _;
end case;
// whevever var is encountered, replace it with val
val := mkval(var, inna);
procedure subs(~x)
args := Arguments(x);
if Trim(Name(x)) eq var then
if #args eq 0 then
y := New(PGGAttr_ArgSpecSubs);
y`name := x`name;
y`arguments := x`arguments;
y`subs := val;
x := y;
else
error "not expecting arguments";
end if;
else
for i in [1..#args] do
subs(~args[i]);
end for;
end if;
end procedure;
subs(~inna);
// parse inner
return PGG_Parsable(inner, inna);
end function);
end intrinsic;
intrinsic PGG_ArgSpec_AttrInt() -> PGGArgSpec
{The argspec matching the PGGAttr when it looks like an integer.}
return PGG_ArgSpec(function (x)
// coerce toa PGGAttr
ok, a := PGG_Attribute_IsCoercible(x);
if not ok then
return false, _;
end if;
// check it has no arguments
if #Arguments(a) ne 0 then
return false, _;
end if;
// check it looks like an integer
z := Trim(Name(a));
ok, _, groups := Regexp("^-?[0-9]+$", z);
if ok then
return true, StringToInteger(z);
else
return false, _;
end if;
end function);
end intrinsic;
intrinsic PGG_ArgSpec_Attr(name :: MonStgElt, args, transform) -> PGGArgSpec
{The argspec matching the PGGAttr with the given name and args.}
return PGG_ArgSpec(function (x)
// coerce to a PGGAttr
ok, a := PGG_Attribute_IsCoercible(x);
if not ok then
return false, _;
end if;
// check the name
if Trim(Name(a)) ne name then
return false, _;
end if;
// check the args
// pargs := [* arg[3] : arg in args *];
pargs := [* #arg ge 3 select <true, arg[3]> else <false> : arg in args *];
pos := 1;
for aa in Arguments(a) do
// try to parse keyword argument
for i in [1..#args] do
if #args[i] ge 2 then
if Trim(Name(aa)) eq args[i][2] and #Arguments(aa) eq 1 then
ok, parg := PGG_Parsable(args[i][1], Arguments(aa)[1]);
if ok then
pargs[i] := <true, parg>;
continue aa;
end if;
end if;
end if;
end for;
// try to parse a positional argument
for i in [pos..#args] do
ok, parg := PGG_Parsable(args[i][1], aa);
if ok then
pargs[i] := <true, parg>;
pos := i+1;
continue aa;
end if;
end for;
// failed parsing
return false, _;
end for;
// check all the arguments are defined
if exists{p : p in pargs | not p[1]} then
return false, _;
end if;
// apply the transform
return true, transform([* p[2] : p in pargs *]);
end function);
end intrinsic;
intrinsic PGG_ArgSpec_Recursive(mk :: UserProgram) -> PGGArgSpec
{Allows the creation of a PGGArgSpec recursively.}
A := New(PGGArgSpec);
B := mk(A);
A`is_valid := B`is_valid;
return A;
end intrinsic;
intrinsic PGG_ArgSpec_Recursive(n :: RngIntElt, mk :: UserProgram) -> []
{Allows the creation of n PGGArgSpecs recursively.}
As := [New(PGGArgSpec) : i in [1..n]];
Bs := mk(As);
require #Bs eq n: "mk must return n PGGArgSpecs";
for i in [1..n] do
As[i]`is_valid := Bs[i]`is_valid;
end for;
return As;
end intrinsic;
intrinsic PGG_ArgSpec_ManyRecursive(mk :: UserProgram) -> Map
{Allows the creation of many PGGArgSpecs.}
store := NewStore();
function set(name, A)
ok, B := StoreIsDefined(store, name);
if ok then
B`is_valid := A`is_valid;
return B;
else
StoreSet(store, name, A);
return A;
end if;
end function;
function get(name)
ok, A := StoreIsDefined(store, name);
if ok then
return A;
else
A := New(PGGArgSpec);
StoreSet(store, name, A);
return A;
end if;
end function;
mk(set, get);
return func<name | StoreGet(store, name)>;
end intrinsic;
intrinsic PGG_ArgSpec_Predicate(test :: UserProgram) -> PGGArgSpec
{The PGGArgSpec passing with the given test.}
return PGG_ArgSpec(function (x)
if test(x) then
return true, x;
else
return false, _;
end if;
end function);
end intrinsic;