-
Notifications
You must be signed in to change notification settings - Fork 200
/
lens.c
2455 lines (2161 loc) · 68.5 KB
/
lens.c
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
/*
* lens.c:
*
* Copyright (C) 2007-2016 David Lutterkort
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: David Lutterkort <[email protected]>
*/
#include <config.h>
#include <stddef.h>
#include "lens.h"
#include "memory.h"
#include "errcode.h"
#include "internal.h"
/* This enum must be kept in sync with type_offs and ntypes */
enum lens_type {
CTYPE, ATYPE, KTYPE, VTYPE
};
static const int type_offs[] = {
offsetof(struct lens, ctype),
offsetof(struct lens, atype),
offsetof(struct lens, ktype),
offsetof(struct lens, vtype)
};
static const int ntypes = sizeof(type_offs)/sizeof(type_offs[0]);
static const char *lens_type_names[] =
{ "ctype", "atype", "ktype", "vtype" };
#define ltype(lns, t) *((struct regexp **) ((char *) lns + type_offs[t]))
static struct value * typecheck_union(struct info *,
struct lens *l1, struct lens *l2);
static struct value *typecheck_concat(struct info *,
struct lens *l1, struct lens *l2);
static struct value *typecheck_square(struct info *,
struct lens *l1, struct lens *l2);
static struct value *typecheck_iter(struct info *info, struct lens *l);
static struct value *typecheck_maybe(struct info *info, struct lens *l);
/* Lens names for pretty printing */
/* keep order in sync with enum type */
static const char *const tags[] = {
"del", "store", "value", "key", "label", "seq", "counter",
"concat", "union",
"subtree", "star", "maybe", "rec", "square"
};
#define ltag(lens) (tags[lens->tag - L_DEL])
static const struct string digits_string = {
.ref = REF_MAX, .str = (char *) "[0123456789]+"
};
static const struct string *const digits_pat = &digits_string;
char *format_lens(struct lens *l) {
if (l == NULL) {
return strdup("(no lens)");
}
char *inf = format_info(l->info);
char *result;
xasprintf(&result, "%s[%s]%s", tags[l->tag - L_DEL], inf,
l->recursive ? "R" : "r");
free(inf);
return result;
}
#define BUG_LENS_TAG(lns) bug_lens_tag(lns, __FILE__, __LINE__)
static void bug_lens_tag(struct lens *lens, const char *file, int lineno) {
if (lens != NULL && lens->info != NULL && lens->info->error != NULL) {
char *s = format_lens(lens);
bug_on(lens->info->error, file, lineno, "Unexpected lens tag %s", s);
free(s);
} else {
/* We are really screwed */
assert(0);
}
return;
}
/* Construct a finite automaton from REGEXP and return it in *FA.
*
* Return NULL if REGEXP is valid, if the regexp REGEXP has syntax errors,
* return an exception.
*/
static struct value *str_to_fa(struct info *info, const char *pattern,
struct fa **fa, int nocase) {
int error;
struct value *exn = NULL;
size_t re_err_len;
char *re_str = NULL, *re_err = NULL;
*fa = NULL;
error = fa_compile(pattern, strlen(pattern), fa);
if (error == REG_NOERROR) {
if (nocase) {
error = fa_nocase(*fa);
ERR_NOMEM(error < 0, info);
}
return NULL;
}
re_str = escape(pattern, -1, RX_ESCAPES);
ERR_NOMEM(re_str == NULL, info);
exn = make_exn_value(info, "Invalid regular expression /%s/", re_str);
re_err_len = regerror(error, NULL, NULL, 0);
error = ALLOC_N(re_err, re_err_len);
ERR_NOMEM(error < 0, info);
regerror(error, NULL, re_err, re_err_len);
exn_printf_line(exn, "%s", re_err);
done:
free(re_str);
free(re_err);
return exn;
error:
fa_free(*fa);
*fa = NULL;
exn = info->error->exn;
goto done;
}
static struct value *regexp_to_fa(struct regexp *regexp, struct fa **fa) {
return str_to_fa(regexp->info, regexp->pattern->str, fa, regexp->nocase);
}
static struct lens *make_lens(enum lens_tag tag, struct info *info) {
struct lens *lens;
make_ref(lens);
lens->tag = tag;
lens->info = info;
return lens;
}
static struct lens *make_lens_unop(enum lens_tag tag, struct info *info,
struct lens *child) {
struct lens *lens = make_lens(tag, info);
lens->child = child;
lens->value = child->value;
lens->key = child->key;
return lens;
}
typedef struct regexp *regexp_combinator(struct info *, int, struct regexp **);
static struct lens *make_lens_binop(enum lens_tag tag, struct info *info,
struct lens *l1, struct lens *l2,
regexp_combinator *combinator) {
struct lens *lens = make_lens(tag, info);
int n1 = (l1->tag == tag) ? l1->nchildren : 1;
struct regexp **types = NULL;
if (lens == NULL)
goto error;
lens->nchildren = n1;
lens->nchildren += (l2->tag == tag) ? l2->nchildren : 1;
lens->recursive = l1->recursive || l2->recursive;
lens->rec_internal = l1->rec_internal || l2->rec_internal;
if (ALLOC_N(lens->children, lens->nchildren) < 0) {
lens->nchildren = 0;
goto error;
}
if (l1->tag == tag) {
for (int i=0; i < l1->nchildren; i++)
lens->children[i] = ref(l1->children[i]);
unref(l1, lens);
} else {
lens->children[0] = l1;
}
if (l2->tag == tag) {
for (int i=0; i < l2->nchildren; i++)
lens->children[n1 + i] = ref(l2->children[i]);
unref(l2, lens);
} else {
lens->children[n1] = l2;
}
for (int i=0; i < lens->nchildren; i++) {
lens->value = lens->value || lens->children[i]->value;
lens->key = lens->key || lens->children[i]->key;
}
if (ALLOC_N(types, lens->nchildren) < 0)
goto error;
if (! lens->rec_internal) {
/* Inside a recursive lens, we assign types with lns_check_rec
* once we know the entire lens */
for (int t=0; t < ntypes; t++) {
if (lens->recursive && t == CTYPE)
continue;
for (int i=0; i < lens->nchildren; i++)
types[i] = ltype(lens->children[i], t);
ltype(lens, t) = (*combinator)(info, lens->nchildren, types);
}
}
FREE(types);
for (int i=0; i < lens->nchildren; i++)
ensure(tag != lens->children[i]->tag, lens->info);
return lens;
error:
unref(lens, lens);
FREE(types);
return NULL;
}
static struct value *make_lens_value(struct lens *lens) {
struct value *v;
v = make_value(V_LENS, ref(lens->info));
v->lens = lens;
return v;
}
struct value *lns_make_union(struct info *info,
struct lens *l1, struct lens *l2, int check) {
struct lens *lens = NULL;
int consumes_value = l1->consumes_value && l2->consumes_value;
int recursive = l1->recursive || l2->recursive;
int ctype_nullable = l1->ctype_nullable || l2->ctype_nullable;
if (check) {
struct value *exn = typecheck_union(info, l1, l2);
if (exn != NULL)
return exn;
}
lens = make_lens_binop(L_UNION, info, l1, l2, regexp_union_n);
lens->consumes_value = consumes_value;
if (! recursive)
lens->ctype_nullable = ctype_nullable;
return make_lens_value(lens);
}
struct value *lns_make_concat(struct info *info,
struct lens *l1, struct lens *l2, int check) {
struct lens *lens = NULL;
int consumes_value = l1->consumes_value || l2->consumes_value;
int recursive = l1->recursive || l2->recursive;
int ctype_nullable = l1->ctype_nullable && l2->ctype_nullable;
if (check) {
struct value *exn = typecheck_concat(info, l1, l2);
if (exn != NULL)
return exn;
}
if (l1->value && l2->value) {
return make_exn_value(info, "Multiple stores in concat");
}
if (l1->key && l2->key) {
return make_exn_value(info, "Multiple keys/labels in concat");
}
lens = make_lens_binop(L_CONCAT, info, l1, l2, regexp_concat_n);
lens->consumes_value = consumes_value;
if (! recursive)
lens->ctype_nullable = ctype_nullable;
return make_lens_value(lens);
}
static struct regexp *subtree_atype(struct info *info,
struct regexp *ktype,
struct regexp *vtype) {
const char *kpat = (ktype == NULL) ? ENC_NULL : ktype->pattern->str;
const char *vpat = (vtype == NULL) ? ENC_NULL : vtype->pattern->str;
char *pat;
struct regexp *result = NULL;
char *ks = NULL, *vs = NULL;
int nocase;
if (ktype != NULL && vtype != NULL && ktype->nocase != vtype->nocase) {
ks = regexp_expand_nocase(ktype);
vs = regexp_expand_nocase(vtype);
ERR_NOMEM(ks == NULL || vs == NULL, info);
if (asprintf(&pat, "(%s)%s(%s)%s", ks, ENC_EQ, vs, ENC_SLASH) < 0)
ERR_NOMEM(true, info);
nocase = 0;
} else {
if (asprintf(&pat, "(%s)%s(%s)%s", kpat, ENC_EQ, vpat, ENC_SLASH) < 0)
ERR_NOMEM(pat == NULL, info);
nocase = 0;
if (ktype != NULL)
nocase = ktype->nocase;
else if (vtype != NULL)
nocase = vtype->nocase;
}
result = make_regexp(info, pat, nocase);
error:
free(ks);
free(vs);
return result;
}
/*
* A subtree lens l1 = [ l ]
*
* Types are assigned as follows:
*
* l1->ctype = l->ctype
* l1->atype = encode(l->ktype, l->vtype)
* l1->ktype = NULL
* l1->vtype = NULL
*/
struct value *lns_make_subtree(struct info *info, struct lens *l) {
struct lens *lens;
lens = make_lens_unop(L_SUBTREE, info, l);
lens->ctype = ref(l->ctype);
if (! l->recursive)
lens->atype = subtree_atype(info, l->ktype, l->vtype);
lens->value = lens->key = 0;
lens->recursive = l->recursive;
lens->rec_internal = l->rec_internal;
if (! l->recursive)
lens->ctype_nullable = l->ctype_nullable;
return make_lens_value(lens);
}
struct value *lns_make_star(struct info *info, struct lens *l, int check) {
struct lens *lens;
if (check) {
struct value *exn = typecheck_iter(info, l);
if (exn != NULL)
return exn;
}
if (l->value) {
return make_exn_value(info, "Multiple stores in iteration");
}
if (l->key) {
return make_exn_value(info, "Multiple keys/labels in iteration");
}
lens = make_lens_unop(L_STAR, info, l);
for (int t = 0; t < ntypes; t++) {
ltype(lens, t) = regexp_iter(info, ltype(l, t), 0, -1);
}
lens->recursive = l->recursive;
lens->rec_internal = l->rec_internal;
lens->ctype_nullable = 1;
return make_lens_value(lens);
}
struct value *lns_make_plus(struct info *info, struct lens *l, int check) {
struct value *star, *conc;
star = lns_make_star(info, l, check);
if (EXN(star))
return star;
conc = lns_make_concat(ref(info), ref(l), ref(star->lens), check);
unref(star, value);
return conc;
}
struct value *lns_make_maybe(struct info *info, struct lens *l, int check) {
struct lens *lens;
if (check) {
struct value *exn = typecheck_maybe(info, l);
if (exn != NULL)
return exn;
}
lens = make_lens_unop(L_MAYBE, info, l);
for (int t=0; t < ntypes; t++)
ltype(lens, t) = regexp_maybe(info, ltype(l, t));
lens->value = l->value;
lens->key = l->key;
lens->recursive = l->recursive;
lens->rec_internal = l->rec_internal;
lens->ctype_nullable = 1;
return make_lens_value(lens);
}
/* The ctype of SQR is a regular approximation of the true ctype of SQR
* at this point. In some situations, for example in processing quoted
* strings this leads to false typecheck errors; to lower the chances
* of these, we try to construct the precise ctype of SQR if the
* language of L1 is finite (and has a small number of words)
*/
static void square_precise_type(struct info *info,
struct regexp **sqr,
struct regexp *left,
struct regexp *body) {
char **words = NULL;
int nwords = 0, r;
struct fa *fa = NULL;
struct value *exn = NULL;
struct regexp **u = NULL, *c[3], *w = NULL;
exn = str_to_fa(info, left->pattern->str, &fa, left->nocase);
if (exn != NULL)
goto error;
nwords = fa_enumerate(fa, 10, &words); /* The limit of 10 is arbitrary */
if (nwords < 0)
goto error;
r = ALLOC_N(u, nwords);
ERR_NOMEM(r < 0, info);
c[1] = body;
for (int i=0; i < nwords; i++) {
w = make_regexp_literal(left->info, words[i]);
ERR_NOMEM(w == NULL, info);
w->nocase = left->nocase;
c[0] = c[2] = w;
u[i] = regexp_concat_n(info, 3, c);
unref(w, regexp);
ERR_NOMEM(u[i] == NULL, info);
}
w = regexp_union_n(info, nwords, u);
if (w != NULL) {
unref(*sqr, regexp);
*sqr = w;
w = NULL;
}
error:
unref(w, regexp);
for (int i=0; i < nwords; i++) {
free(words[i]);
if (u != NULL)
unref(u[i], regexp);
}
free(words);
free(u);
fa_free(fa);
unref(exn, value);
}
/* Build a square lens as
* left . body . right
* where left and right accepts the same language and
* captured strings must match. The inability to express this with other
* lenses makes the square primitive necessary.
*/
struct value * lns_make_square(struct info *info, struct lens *l1,
struct lens *l2, struct lens *l3, int check) {
struct value *cnt1 = NULL, *cnt2 = NULL, *res = NULL;
struct lens *sqr = NULL;
/* supported types: L_KEY . body . L_DEL or L_DEL . body . L_DEL */
if (l3->tag != L_DEL || (l1->tag != L_DEL && l1->tag != L_KEY))
return make_exn_value(info, "Supported types: (key lns del) or (del lns del)");
res = typecheck_square(info, l1, l3);
if (res != NULL)
goto error;
res = lns_make_concat(ref(info), ref(l1), ref(l2), check);
if (EXN(res))
goto error;
cnt1 = res;
res = lns_make_concat(ref(info), ref(cnt1->lens), ref(l3), check);
if (EXN(res))
goto error;
cnt2 = res;
sqr = make_lens_unop(L_SQUARE, ref(info), ref(cnt2->lens));
ERR_NOMEM(sqr == NULL, info);
for (int t=0; t < ntypes; t++)
ltype(sqr, t) = ref(ltype(cnt2->lens, t));
square_precise_type(info, &(sqr->ctype), l1->ctype, l2->ctype);
sqr->recursive = cnt2->lens->recursive;
sqr->rec_internal = cnt2->lens->rec_internal;
sqr->consumes_value = cnt2->lens->consumes_value;
res = make_lens_value(sqr);
ERR_NOMEM(res == NULL, info);
sqr = NULL;
error:
unref(info, info);
unref(l1, lens);
unref(l2, lens);
unref(l3, lens);
unref(cnt1, value);
unref(cnt2, value);
unref(sqr, lens);
return res;
}
/*
* Lens primitives
*/
static struct regexp *make_regexp_from_string(struct info *info,
struct string *string) {
struct regexp *r;
make_ref(r);
if (r != NULL) {
r->info = ref(info);
r->pattern = ref(string);
r->nocase = 0;
}
return r;
}
static struct regexp *restrict_regexp(struct regexp *r) {
char *nre = NULL;
struct regexp *result = NULL;
size_t nre_len;
int ret;
ret = fa_restrict_alphabet(r->pattern->str, strlen(r->pattern->str),
&nre, &nre_len,
RESERVED_FROM_CH, RESERVED_TO_CH);
ERR_NOMEM(ret == REG_ESPACE || ret < 0, r->info);
BUG_ON(ret != 0, r->info, NULL);
ensure(nre_len == strlen(nre), r->info);
ret = regexp_c_locale(&nre, &nre_len);
ERR_NOMEM(ret < 0, r->info);
result = make_regexp(r->info, nre, r->nocase);
nre = NULL;
BUG_ON(regexp_compile(result) != 0, r->info,
"Could not compile restricted regexp");
done:
free(nre);
return result;
error:
unref(result, regexp);
goto done;
}
static struct value *
typecheck_prim(enum lens_tag tag, struct info *info,
struct regexp *regexp, struct string *string) {
struct fa *fa_slash = NULL;
struct fa *fa_key = NULL;
struct fa *fa_isect = NULL;
struct value *exn = NULL;
/* Typecheck */
if (tag == L_DEL && string != NULL) {
int cnt;
const char *dflt = string->str;
cnt = regexp_match(regexp, dflt, strlen(dflt), 0, NULL);
if (cnt != strlen(dflt)) {
char *s = escape(dflt, -1, RX_ESCAPES);
char *r = regexp_escape(regexp);
exn = make_exn_value(info,
"del: the default value '%s' does not match /%s/", s, r);
FREE(s);
FREE(r);
goto error;
}
}
error:
fa_free(fa_isect);
fa_free(fa_key);
fa_free(fa_slash);
return exn;
}
struct value *lns_make_prim(enum lens_tag tag, struct info *info,
struct regexp *regexp, struct string *string) {
struct lens *lens = NULL;
struct value *exn = NULL;
if (typecheck_p(info)) {
exn = typecheck_prim(tag, info, regexp, string);
if (exn != NULL)
goto error;
}
/* Build the actual lens */
lens = make_lens(tag, info);
lens->regexp = regexp;
lens->string = string;
lens->key = (tag == L_KEY || tag == L_LABEL || tag == L_SEQ);
lens->value = (tag == L_STORE || tag == L_VALUE);
lens->consumes_value = (tag == L_STORE || tag == L_VALUE);
lens->atype = regexp_make_empty(info);
/* Set the ctype */
if (tag == L_DEL || tag == L_STORE || tag == L_KEY) {
lens->ctype = ref(regexp);
lens->ctype_nullable = regexp_matches_empty(lens->ctype);
} else if (tag == L_LABEL || tag == L_VALUE
|| tag == L_SEQ || tag == L_COUNTER) {
lens->ctype = regexp_make_empty(info);
lens->ctype_nullable = 1;
} else {
BUG_LENS_TAG(lens);
goto error;
}
/* Set the ktype */
if (tag == L_SEQ) {
lens->ktype =
make_regexp_from_string(info, (struct string *) digits_pat);
if (lens->ktype == NULL)
goto error;
} else if (tag == L_KEY) {
lens->ktype = restrict_regexp(lens->regexp);
} else if (tag == L_LABEL) {
lens->ktype = make_regexp_literal(info, lens->string->str);
if (lens->ktype == NULL)
goto error;
}
/* Set the vtype */
if (tag == L_STORE) {
lens->vtype = restrict_regexp(lens->regexp);
} else if (tag == L_VALUE) {
lens->vtype = make_regexp_literal(info, lens->string->str);
if (lens->vtype == NULL)
goto error;
}
return make_lens_value(lens);
error:
return exn;
}
/*
* Typechecking of lenses
*/
static struct value *disjoint_check(struct info *info, bool is_get,
struct regexp *r1, struct regexp *r2) {
struct fa *fa1 = NULL;
struct fa *fa2 = NULL;
struct fa *fa = NULL;
struct value *exn = NULL;
const char *const msg = is_get ? "union.get" : "tree union.put";
if (r1 == NULL || r2 == NULL)
return NULL;
exn = regexp_to_fa(r1, &fa1);
if (exn != NULL)
goto done;
exn = regexp_to_fa(r2, &fa2);
if (exn != NULL)
goto done;
fa = fa_intersect(fa1, fa2);
if (! fa_is_basic(fa, FA_EMPTY)) {
size_t xmpl_len;
char *xmpl;
fa_example(fa, &xmpl, &xmpl_len);
if (! is_get) {
char *fmt = enc_format(xmpl, xmpl_len);
if (fmt != NULL) {
FREE(xmpl);
xmpl = fmt;
}
}
exn = make_exn_value(ref(info),
"overlapping lenses in %s", msg);
if (is_get)
exn_printf_line(exn, "Example matched by both: '%s'", xmpl);
else
exn_printf_line(exn, "Example matched by both: %s", xmpl);
free(xmpl);
}
done:
fa_free(fa);
fa_free(fa1);
fa_free(fa2);
return exn;
}
static struct value *typecheck_union(struct info *info,
struct lens *l1, struct lens *l2) {
struct value *exn = NULL;
exn = disjoint_check(info, true, l1->ctype, l2->ctype);
if (exn == NULL) {
exn = disjoint_check(info, false, l1->atype, l2->atype);
}
if (exn != NULL) {
char *fi = format_info(l1->info);
exn_printf_line(exn, "First lens: %s", fi);
free(fi);
fi = format_info(l2->info);
exn_printf_line(exn, "Second lens: %s", fi);
free(fi);
}
return exn;
}
static struct value *
ambig_check(struct info *info, struct fa *fa1, struct fa *fa2,
enum lens_type typ, struct lens *l1, struct lens *l2,
const char *msg, bool iterated) {
char *upv, *pv, *v;
size_t upv_len;
struct value *exn = NULL;
int r;
r = fa_ambig_example(fa1, fa2, &upv, &upv_len, &pv, &v);
if (r < 0) {
exn = make_exn_value(ref(info), "not enough memory");
if (exn != NULL) {
return exn;
} else {
ERR_REPORT(info, AUG_ENOMEM, NULL);
return info->error->exn;
}
}
if (upv != NULL) {
char *e_u, *e_up, *e_upv, *e_pv, *e_v;
char *s1, *s2;
if (typ == ATYPE) {
e_u = enc_format(upv, pv - upv);
e_up = enc_format(upv, v - upv);
e_upv = enc_format(upv, upv_len);
e_pv = enc_format(pv, strlen(pv));
e_v = enc_format(v, strlen(v));
lns_format_atype(l1, &s1);
lns_format_atype(l2, &s2);
} else {
e_u = escape(upv, pv - upv, RX_ESCAPES);
e_up = escape(upv, v - upv, RX_ESCAPES);
e_upv = escape(upv, -1, RX_ESCAPES);
e_pv = escape(pv, -1, RX_ESCAPES);
e_v = escape(v, -1, RX_ESCAPES);
s1 = regexp_escape(ltype(l1, typ));
s2 = regexp_escape(ltype(l2, typ));
}
exn = make_exn_value(ref(info), "%s", msg);
if (iterated) {
exn_printf_line(exn, " Iterated regexp: /%s/", s1);
} else {
exn_printf_line(exn, " First regexp: /%s/", s1);
exn_printf_line(exn, " Second regexp: /%s/", s2);
}
exn_printf_line(exn, " '%s' can be split into", e_upv);
exn_printf_line(exn, " '%s|=|%s'\n", e_u, e_pv);
exn_printf_line(exn, " and");
exn_printf_line(exn, " '%s|=|%s'\n", e_up, e_v);
free(e_u);
free(e_up);
free(e_upv);
free(e_pv);
free(e_v);
free(s1);
free(s2);
}
free(upv);
return exn;
}
static struct value *
ambig_concat_check(struct info *info, const char *msg,
enum lens_type typ, struct lens *l1, struct lens *l2) {
struct fa *fa1 = NULL;
struct fa *fa2 = NULL;
struct value *result = NULL;
struct regexp *r1 = ltype(l1, typ);
struct regexp *r2 = ltype(l2, typ);
if (r1 == NULL || r2 == NULL)
return NULL;
result = regexp_to_fa(r1, &fa1);
if (result != NULL)
goto done;
result = regexp_to_fa(r2, &fa2);
if (result != NULL)
goto done;
result = ambig_check(info, fa1, fa2, typ, l1, l2, msg, false);
done:
fa_free(fa1);
fa_free(fa2);
return result;
}
static struct value *typecheck_concat(struct info *info,
struct lens *l1, struct lens *l2) {
struct value *result = NULL;
result = ambig_concat_check(info, "ambiguous concatenation",
CTYPE, l1, l2);
if (result == NULL) {
result = ambig_concat_check(info, "ambiguous tree concatenation",
ATYPE, l1, l2);
}
if (result != NULL) {
char *fi = format_info(l1->info);
exn_printf_line(result, "First lens: %s", fi);
free(fi);
fi = format_info(l2->info);
exn_printf_line(result, "Second lens: %s", fi);
free(fi);
}
return result;
}
static struct value *make_exn_square(struct info *info, struct lens *l1,
struct lens *l2, const char *msg) {
char *fi;
struct value *exn = make_exn_value(ref(info), "%s",
"Inconsistency in lens square");
exn_printf_line(exn, "%s", msg);
fi = format_info(l1->info);
exn_printf_line(exn, "Left lens: %s", fi);
free(fi);
fi = format_info(l2->info);
exn_printf_line(exn, "Right lens: %s", fi);
free(fi);
return exn;
}
static struct value *typecheck_square(struct info *info, struct lens *l1,
struct lens *l2) {
int r;
struct value *exn = NULL;
struct fa *fa1 = NULL, *fa2 = NULL;
struct regexp *r1 = ltype(l1, CTYPE);
struct regexp *r2 = ltype(l2, CTYPE);
if (r1 == NULL || r2 == NULL)
return NULL;
exn = regexp_to_fa(r1, &fa1);
if (exn != NULL)
goto done;
exn = regexp_to_fa(r2, &fa2);
if (exn != NULL)
goto done;
r = fa_equals(fa1, fa2);
if (r < 0) {
exn = make_exn_value(ref(info), "not enough memory");
if (exn != NULL) {
return exn;
} else {
ERR_REPORT(info, AUG_ENOMEM, NULL);
return info->error->exn;;
}
}
if (r == 0) {
exn = make_exn_square(info, l1, l2,
"Left and right lenses must accept the same language");
goto done;
}
/* check del create consistency */
if (l1->tag == L_DEL && l2->tag == L_DEL) {
if (!STREQ(l1->string->str, l2->string->str)) {
exn = make_exn_square(info, l1, l2,
"Left and right lenses must have the same default value");
goto done;
}
}
done:
fa_free(fa1);
fa_free(fa2);
return exn;
}
static struct value *
ambig_iter_check(struct info *info, const char *msg,
enum lens_type typ, struct lens *l) {
struct fa *fas = NULL, *fa = NULL;
struct value *result = NULL;
struct regexp *r = ltype(l, typ);
if (r == NULL)
return NULL;
result = regexp_to_fa(r, &fa);
if (result != NULL)
goto done;
fas = fa_iter(fa, 0, -1);
result = ambig_check(info, fa, fas, typ, l, l, msg, true);
done:
fa_free(fa);
fa_free(fas);
return result;
}
static struct value *typecheck_iter(struct info *info, struct lens *l) {
struct value *result = NULL;
result = ambig_iter_check(info, "ambiguous iteration", CTYPE, l);
if (result == NULL) {
result = ambig_iter_check(info, "ambiguous tree iteration", ATYPE, l);
}
if (result != NULL) {
char *fi = format_info(l->info);
exn_printf_line(result, "Iterated lens: %s", fi);
free(fi);
}
return result;
}
static struct value *typecheck_maybe(struct info *info, struct lens *l) {
/* Check (r)? as (<e>|r) where <e> is the empty language */
struct value *exn = NULL;
if (l->ctype != NULL && regexp_matches_empty(l->ctype)) {
exn = make_exn_value(ref(info),
"illegal optional expression: /%s/ matches the empty word",
l->ctype->pattern->str);
}
/* Typecheck the put direction; the check passes if
(1) the atype does not match the empty string, because we can tell
from looking at tree nodes whether L should be applied or not
(2) L handles a value; with that, we know whether to apply L or not
depending on whether the current node has a non NULL value or not
*/
if (exn == NULL && ! l->consumes_value) {
if (l->atype != NULL && regexp_matches_empty(l->atype)) {
exn = make_exn_value(ref(info),
"optional expression matches the empty tree but does not consume a value");
}
}
return exn;
}
void free_lens(struct lens *lens) {
if (lens == NULL)
return;
ensure(lens->ref == 0, lens->info);
if (debugging("lenses"))
dump_lens_tree(lens);
switch (lens->tag) {
case L_DEL:
unref(lens->regexp, regexp);
unref(lens->string, string);
break;
case L_STORE:
case L_KEY:
unref(lens->regexp, regexp);
break;
case L_LABEL:
case L_SEQ:
case L_COUNTER:
case L_VALUE:
unref(lens->string, string);
break;
case L_SUBTREE:
case L_STAR:
case L_MAYBE:
case L_SQUARE:
unref(lens->child, lens);
break;