51
52
char *value; /* GET_STORE leaves a value here */
53
char *square; /* last L_DEL from L_SQUARE */
52
54
struct lns_error *error;
53
55
/* We use the registers from a regular expression match to keep track
54
56
* of the substring we are currently looking at. REGS are the registers
156
160
void free_skel(struct skel *skel) {
157
161
if (skel == NULL)
159
if (skel->tag == L_CONCAT || skel->tag == L_STAR || skel->tag == L_MAYBE) {
163
if (skel->tag == L_CONCAT || skel->tag == L_STAR || skel->tag == L_MAYBE ||
164
skel->tag == L_SQUARE) {
160
165
while (skel->skels != NULL) {
161
166
struct skel *del = skel->skels;
162
167
skel->skels = del->next;
383
388
get_error(state, lens, "no match for del /%s/", pat);
391
if (lens->string == NULL) {
392
state->square = token(state);
394
update_span(state->span, REG_START(state), REG_END(state));
408
417
get_error(state, lens, "More than one store in a subtree");
409
418
else if (! REG_MATCHED(state))
410
419
no_match_error(state, lens);
412
421
state->value = token(state);
423
state->span->value_start = REG_START(state);
424
state->span->value_end = REG_END(state);
425
update_span(state->span, REG_START(state), REG_END(state));
435
450
ensure0(lens->tag == L_KEY, state->info);
436
451
if (! REG_MATCHED(state))
437
452
no_match_error(state, lens);
439
454
state->key = token(state);
456
state->span->label_start = REG_START(state);
457
state->span->label_end = REG_END(state);
458
update_span(state->span, REG_START(state), REG_END(state));
656
677
static struct tree *get_subtree(struct lens *lens, struct state *state) {
657
678
char *key = state->key;
658
679
char *value = state->value;
680
struct span *span = state->span;
659
682
struct tree *tree = NULL, *children;
661
684
state->key = NULL;
662
685
state->value = NULL;
686
if (state->info->flags & AUG_ENABLE_SPAN) {
687
state->span = make_span(state->info);
688
ERR_NOMEM(state->span == NULL, state->info);
663
691
children = get_lens(lens->child, state);
665
693
tree = make_tree(state->key, state->value, NULL, children);
694
tree->span = state->span;
696
if (state->span != NULL) {
697
update_span(span, state->span->span_start, state->span->span_end);
667
700
state->key = key;
668
701
state->value = value;
672
708
static struct skel *parse_subtree(struct lens *lens, struct state *state,
682
718
return make_skel(lens);
721
static struct tree *get_square(struct lens *lens, struct state *state) {
722
ensure0(lens->tag == L_SQUARE, state->info);
724
struct tree *tree = NULL;
725
char *key = NULL, *square = NULL;
727
// get the child lens
728
tree = get_concat(lens->child, state);
731
square = state->square;
732
ensure0(key != NULL, state->info);
733
ensure0(square != NULL, state->info);
735
if (strcmp(key, square) != 0) {
736
get_error(state, lens, "%s \"%s\" %s \"%s\"",
737
"Parse error: mismatched key in square lens, expecting", key,
745
static struct skel *parse_square(struct lens *lens, struct state *state,
746
struct dict **dict) {
747
ensure0(lens->tag == L_SQUARE, state->info);
748
struct skel *skel, *sk;
750
skel = parse_concat(lens->child, state, dict);
751
sk = make_skel(lens);
686
758
* Helpers for recursive lenses
691
763
for (int j = state->fused - 1; j >=0; j--) {
692
764
struct frame *f = state->frames + j;
693
765
for (int i=0; i < state->lvl; i++) fputc(' ', stderr);
694
fprintf(stderr, "%2d %s %s", j, f->key, f->value);
766
fprintf(stderr, "%2d %s %s %s", j, f->key, f->value, f->square);
695
767
if (f->tree == NULL) {
696
768
fprintf(stderr, " - ");
760
832
top->tree = get_lens(lens, state);
761
833
top->key = state->key;
762
834
top->value = state->value;
835
top->square = state->square;
763
836
state->key = NULL;
764
837
state->value = NULL;
838
state->square = NULL;
767
841
static void parse_terminal(struct frame *top, struct lens *lens,
813
887
struct frame *f = push_frame(rec_state, lens);
814
888
f->key = state->key;
815
889
f->value = state->value;
890
f->span = state->span;
816
891
state->key = NULL;
817
892
state->value = NULL;
893
} else if (lens->tag == L_MAYBE) {
894
push_frame(rec_state, lens);
895
if (state->info->flags & AUG_ENABLE_SPAN) {
896
state->span = make_span(state->info);
897
ERR_NOMEM(state->span == NULL, state->info);
821
904
static void get_combine(struct rec_state *rec_state,
822
905
struct lens *lens, uint n) {
823
906
struct tree *tree = NULL, *tail = NULL;
824
char *key = NULL, *value = NULL;
907
char *key = NULL, *value = NULL, *square = NULL;
825
908
struct frame *top = NULL;
841
924
ensure(value == NULL, rec_state->state->info);
842
925
value = top->value;
927
if (top->square != NULL) {
928
ensure(square == NULL, rec_state->state->info);
929
square = top->square;
845
932
top = push_frame(rec_state, lens);
846
933
top->tree = tree;
848
935
top->value = value;
936
top->square = square;
899
987
struct frame *top = top_frame(rec_state);
900
988
if (rec_state->mode == M_GET) {
901
989
struct tree *tree;
990
// FIXME: tree may leak if pop_frame ensure0 fail
902
991
tree = make_tree(top->key, top->value, NULL, top->tree);
992
tree->span = state->span;
903
993
ERR_NOMEM(tree == NULL, lens->info);
904
994
top = pop_frame(rec_state);
905
995
ensure(lens == top->lens, state->info);
906
996
state->key = top->key;
907
997
state->value = top->value;
998
state->span = top->span;
908
999
pop_frame(rec_state);
909
1000
top = push_frame(rec_state, lens);
910
1001
top->tree = tree;
949
1040
parse_combine(rec_state, lens, n);
950
1041
} else if (lens->tag == L_MAYBE) {
952
1043
if (rec_state->fused > 0
953
1044
&& top_frame(rec_state)->lens == lens->child) {
956
1047
if (rec_state->mode == M_GET)
957
1048
get_combine(rec_state, lens, n);
959
1050
parse_combine(rec_state, lens, n);
1051
} else if (lens->tag == L_SQUARE) {
1052
if (rec_state->mode == M_GET) {
1055
key = top_frame(rec_state)->key;
1056
square = top_frame(rec_state)->square;
1058
ensure(key != NULL, state->info);
1059
ensure(square != NULL, state->info);
1061
// raise syntax error if they are not equals
1062
if (strcmp(key, square) != 0){
1063
get_error(state, lens, "%s \"%s\" %s \"%s\"",
1064
"Parse error: mismatched key in square lens, expecting",
1065
key, "but got", square);
1066
state->error->pos = end - strlen(square);
1071
get_combine(rec_state, lens, 1);
1073
parse_combine(rec_state, lens, 1);
961
1076
top_frame(rec_state)->lens = lens;
1032
1149
jmt_free_parse(visitor.parse);
1033
1150
return rec_state.frames;
1153
for(i = 0; i < rec_state.fused; i++) {
1154
f = nth_frame(&rec_state, i);
1157
if (mode == M_GET) {
1160
} else if (mode == M_PARSE) {
1035
1165
FREE(rec_state.frames);
1106
1236
tree = get_quant_maybe(lens, state);
1239
tree = get_square(lens, state);
1109
1242
BUG_ON(true, state->info, "illegal lens tag %d", lens->tag);
1180
1313
get_error(&state, lens, "get left unused value %s", state.value);
1181
1314
free(state.value);
1316
if (state.square != NULL) {
1317
get_error(&state, lens, "get left unused square %s", state.square);
1183
1320
if (partial && state.error == NULL) {
1184
1321
get_error(&state, lens, "Get did not match entire input");
1242
1379
skel = parse_quant_maybe(lens, state, dict);
1382
skel = parse_square(lens, state, dict);
1245
1385
BUG_ON(true, state->info, "illegal lens tag %d", lens->tag);