diff options
| author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-19 15:03:25 -0300 |
|---|---|---|
| committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-19 15:03:25 -0300 |
| commit | 4cd22aa26a5256009d258e81aa10419199decb39 (patch) | |
| tree | 746e63cabde72c2e8fd50becb5f023f46e38ddd1 | |
| parent | 44d16477a2c412ce9d010e2f793558902ffb737f (diff) | |
| download | lpeglabel-4cd22aa26a5256009d258e81aa10419199decb39.tar.gz lpeglabel-4cd22aa26a5256009d258e81aa10419199decb39.tar.bz2 lpeglabel-4cd22aa26a5256009d258e81aa10419199decb39.zip | |
Updating lpprint and removing label related fields from structs
| -rw-r--r-- | lpcode.c | 2 | ||||
| -rw-r--r-- | lpprint.c | 13 | ||||
| -rw-r--r-- | lptree.c | 30 | ||||
| -rw-r--r-- | lptree.h | 8 |
4 files changed, 26 insertions, 27 deletions
| @@ -526,7 +526,7 @@ static int addoffsetinst (CompileState *compst, Opcode op) { | |||
| 526 | /* labeled failure */ | 526 | /* labeled failure */ |
| 527 | static void codethrow (CompileState *compst, TTree *throw) { | 527 | static void codethrow (CompileState *compst, TTree *throw) { |
| 528 | int recov, aux; | 528 | int recov, aux; |
| 529 | if (throw->u.s.ps != 0) { | 529 | if (throw->u.ps != 0) { |
| 530 | recov = addoffsetinst(compst, IThrowRec); | 530 | recov = addoffsetinst(compst, IThrowRec); |
| 531 | assert(sib2(throw)->tag == TRule); | 531 | assert(sib2(throw)->tag == TRule); |
| 532 | } else { | 532 | } else { |
| @@ -61,7 +61,7 @@ void printinst (const Instruction *op, const Instruction *p) { | |||
| 61 | "choice", "jmp", "call", "open_call", | 61 | "choice", "jmp", "call", "open_call", |
| 62 | "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup", | 62 | "commit", "partial_commit", "back_commit", "failtwice", "fail", "giveup", |
| 63 | "fullcapture", "opencapture", "closecapture", "closeruntime", | 63 | "fullcapture", "opencapture", "closecapture", "closeruntime", |
| 64 | "throw", "recovery", "labeled_choice" /* labeled failure */ | 64 | "throw", "throw_rec", /* labeled failure */ |
| 65 | }; | 65 | }; |
| 66 | printf("%02ld: %s ", (long)(p - op), names[p->i.code]); | 66 | printf("%02ld: %s ", (long)(p - op), names[p->i.code]); |
| 67 | switch ((Opcode)p->i.code) { | 67 | switch ((Opcode)p->i.code) { |
| @@ -108,7 +108,11 @@ void printinst (const Instruction *op, const Instruction *p) { | |||
| 108 | break; | 108 | break; |
| 109 | } | 109 | } |
| 110 | case IThrow: { /* labeled failure */ | 110 | case IThrow: { /* labeled failure */ |
| 111 | printf("%d", p->i.aux); | 111 | printf("(idx = %d)", (p + 1)->i.key); |
| 112 | break; | ||
| 113 | } | ||
| 114 | case IThrowRec: { /* labeled failure */ | ||
| 115 | printjmp(op, p); printf(" (idx = %d)", (p + 2)->i.key); | ||
| 112 | break; | 116 | break; |
| 113 | } | 117 | } |
| 114 | default: break; | 118 | default: break; |
| @@ -159,7 +163,7 @@ static const char *tagnames[] = { | |||
| 159 | "call", "opencall", "rule", "grammar", | 163 | "call", "opencall", "rule", "grammar", |
| 160 | "behind", | 164 | "behind", |
| 161 | "capture", "run-time", | 165 | "capture", "run-time", |
| 162 | "throw", "recov", "labeled-choice" /* labeled failure */ | 166 | "throw" /* labeled failure */ |
| 163 | }; | 167 | }; |
| 164 | 168 | ||
| 165 | 169 | ||
| @@ -212,10 +216,9 @@ void printtree (TTree *tree, int ident) { | |||
| 212 | break; | 216 | break; |
| 213 | } | 217 | } |
| 214 | case TThrow: { /* labeled failure */ | 218 | case TThrow: { /* labeled failure */ |
| 215 | if (tree->u.s.ps != 0) | 219 | if (tree->u.ps != 0) |
| 216 | assert(sib2(tree)->tag == TRule); | 220 | assert(sib2(tree)->tag == TRule); |
| 217 | printf(" key: %d (rule: %d)\n", tree->key, sib2(tree)->cap); | 221 | printf(" key: %d (rule: %d)\n", tree->key, sib2(tree)->cap); |
| 218 | printf(" labels: %d\n", tree->u.label); | ||
| 219 | break; | 222 | break; |
| 220 | } | 223 | } |
| 221 | default: { | 224 | default: { |
| @@ -64,11 +64,11 @@ static void fixonecall (lua_State *L, int postable, TTree *g, TTree *t, byte tag | |||
| 64 | luaL_error(L, "rule '%s' undefined in given grammar", val2str(L, -1)); | 64 | luaL_error(L, "rule '%s' undefined in given grammar", val2str(L, -1)); |
| 65 | } | 65 | } |
| 66 | t->tag = TCall; | 66 | t->tag = TCall; |
| 67 | t->u.s.ps = n - (t - g); /* position relative to node */ | 67 | t->u.ps = n - (t - g); /* position relative to node */ |
| 68 | assert(sib2(t)->tag == TRule); | 68 | assert(sib2(t)->tag == TRule); |
| 69 | sib2(t)->key = t->key; /* fix rule's key */ | 69 | sib2(t)->key = t->key; /* fix rule's key */ |
| 70 | } else if (n != 0) { /* labeled failure */ | 70 | } else if (n != 0) { /* labeled failure */ |
| 71 | t->u.s.ps = n - (t - g); /* position relative to node */ | 71 | t->u.ps = n - (t - g); /* position relative to node */ |
| 72 | } | 72 | } |
| 73 | } | 73 | } |
| 74 | 74 | ||
| @@ -84,13 +84,13 @@ static void correctassociativity (TTree *tree) { | |||
| 84 | TTree *t1 = sib1(tree); | 84 | TTree *t1 = sib1(tree); |
| 85 | assert(tree->tag == TChoice || tree->tag == TSeq); | 85 | assert(tree->tag == TChoice || tree->tag == TSeq); |
| 86 | while (t1->tag == tree->tag) { | 86 | while (t1->tag == tree->tag) { |
| 87 | int n1size = tree->u.s.ps - 1; /* t1 == Op t11 t12 */ | 87 | int n1size = tree->u.ps - 1; /* t1 == Op t11 t12 */ |
| 88 | int n11size = t1->u.s.ps - 1; | 88 | int n11size = t1->u.ps - 1; |
| 89 | int n12size = n1size - n11size - 1; | 89 | int n12size = n1size - n11size - 1; |
| 90 | memmove(sib1(tree), sib1(t1), n11size * sizeof(TTree)); /* move t11 */ | 90 | memmove(sib1(tree), sib1(t1), n11size * sizeof(TTree)); /* move t11 */ |
| 91 | tree->u.s.ps = n11size + 1; | 91 | tree->u.ps = n11size + 1; |
| 92 | sib2(tree)->tag = tree->tag; | 92 | sib2(tree)->tag = tree->tag; |
| 93 | sib2(tree)->u.s.ps = n12size + 1; | 93 | sib2(tree)->u.ps = n12size + 1; |
| 94 | } | 94 | } |
| 95 | } | 95 | } |
| 96 | 96 | ||
| @@ -397,7 +397,7 @@ static TTree *newcharset (lua_State *L) { | |||
| 397 | ** 'sibsize'); returns position for second sibling | 397 | ** 'sibsize'); returns position for second sibling |
| 398 | */ | 398 | */ |
| 399 | static TTree *seqaux (TTree *tree, TTree *sib, int sibsize) { | 399 | static TTree *seqaux (TTree *tree, TTree *sib, int sibsize) { |
| 400 | tree->tag = TSeq; tree->u.s.ps = sibsize + 1; | 400 | tree->tag = TSeq; tree->u.ps = sibsize + 1; |
| 401 | memcpy(sib1(tree), sib, sibsize * sizeof(TTree)); | 401 | memcpy(sib1(tree), sib, sibsize * sizeof(TTree)); |
| 402 | return sib2(tree); | 402 | return sib2(tree); |
| 403 | } | 403 | } |
| @@ -411,7 +411,7 @@ static TTree *seqaux (TTree *tree, TTree *sib, int sibsize) { | |||
| 411 | static void fillseq (TTree *tree, int tag, int n, const char *s) { | 411 | static void fillseq (TTree *tree, int tag, int n, const char *s) { |
| 412 | int i; | 412 | int i; |
| 413 | for (i = 0; i < n - 1; i++) { /* initial n-1 copies of Seq tag; Seq ... */ | 413 | for (i = 0; i < n - 1; i++) { /* initial n-1 copies of Seq tag; Seq ... */ |
| 414 | tree->tag = TSeq; tree->u.s.ps = 2; | 414 | tree->tag = TSeq; tree->u.ps = 2; |
| 415 | sib1(tree)->tag = tag; | 415 | sib1(tree)->tag = tag; |
| 416 | sib1(tree)->u.n = s ? (byte)s[i] : 0; | 416 | sib1(tree)->u.n = s ? (byte)s[i] : 0; |
| 417 | tree = sib2(tree); | 417 | tree = sib2(tree); |
| @@ -518,7 +518,7 @@ static TTree *newroot2sib (lua_State *L, int tag) { | |||
| 518 | TTree *tree2 = getpatt(L, 2, &s2); | 518 | TTree *tree2 = getpatt(L, 2, &s2); |
| 519 | TTree *tree = newtree(L, 1 + s1 + s2); /* create new tree */ | 519 | TTree *tree = newtree(L, 1 + s1 + s2); /* create new tree */ |
| 520 | tree->tag = tag; | 520 | tree->tag = tag; |
| 521 | tree->u.s.ps = 1 + s1; | 521 | tree->u.ps = 1 + s1; |
| 522 | memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); | 522 | memcpy(sib1(tree), tree1, s1 * sizeof(TTree)); |
| 523 | memcpy(sib2(tree), tree2, s2 * sizeof(TTree)); | 523 | memcpy(sib2(tree), tree2, s2 * sizeof(TTree)); |
| 524 | joinktables(L, 1, sib2(tree), 2); | 524 | joinktables(L, 1, sib2(tree), 2); |
| @@ -530,7 +530,7 @@ static TTree *newroot2sib (lua_State *L, int tag) { | |||
| 530 | static TTree *newthrowleaf (lua_State *L) { | 530 | static TTree *newthrowleaf (lua_State *L) { |
| 531 | TTree *tree = newtree(L, 1); | 531 | TTree *tree = newtree(L, 1); |
| 532 | tree->tag = TThrow; | 532 | tree->tag = TThrow; |
| 533 | tree->u.s.ps = 0; /* there is no recovery rule associated */ | 533 | tree->u.ps = 0; /* there is no recovery rule associated */ |
| 534 | return tree; | 534 | return tree; |
| 535 | } | 535 | } |
| 536 | /* labeled failure end */ | 536 | /* labeled failure end */ |
| @@ -608,12 +608,12 @@ static int lp_star (lua_State *L) { | |||
| 608 | /* size = (choice + seq + tree1 + true) * n, but the last has no seq */ | 608 | /* size = (choice + seq + tree1 + true) * n, but the last has no seq */ |
| 609 | tree = newtree(L, n * (size1 + 3) - 1); | 609 | tree = newtree(L, n * (size1 + 3) - 1); |
| 610 | for (; n > 1; n--) { /* repeat (n - 1) times */ | 610 | for (; n > 1; n--) { /* repeat (n - 1) times */ |
| 611 | tree->tag = TChoice; tree->u.s.ps = n * (size1 + 3) - 2; | 611 | tree->tag = TChoice; tree->u.ps = n * (size1 + 3) - 2; |
| 612 | sib2(tree)->tag = TTrue; | 612 | sib2(tree)->tag = TTrue; |
| 613 | tree = sib1(tree); | 613 | tree = sib1(tree); |
| 614 | tree = seqaux(tree, tree1, size1); | 614 | tree = seqaux(tree, tree1, size1); |
| 615 | } | 615 | } |
| 616 | tree->tag = TChoice; tree->u.s.ps = size1 + 1; | 616 | tree->tag = TChoice; tree->u.ps = size1 + 1; |
| 617 | sib2(tree)->tag = TTrue; | 617 | sib2(tree)->tag = TTrue; |
| 618 | memcpy(sib1(tree), tree1, size1 * sizeof(TTree)); | 618 | memcpy(sib1(tree), tree1, size1 * sizeof(TTree)); |
| 619 | } | 619 | } |
| @@ -656,7 +656,7 @@ static int lp_sub (lua_State *L) { | |||
| 656 | else { | 656 | else { |
| 657 | TTree *tree = newtree(L, 2 + s1 + s2); | 657 | TTree *tree = newtree(L, 2 + s1 + s2); |
| 658 | tree->tag = TSeq; /* sequence of... */ | 658 | tree->tag = TSeq; /* sequence of... */ |
| 659 | tree->u.s.ps = 2 + s2; | 659 | tree->u.ps = 2 + s2; |
| 660 | sib1(tree)->tag = TNot; /* ...not... */ | 660 | sib1(tree)->tag = TNot; /* ...not... */ |
| 661 | memcpy(sib1(sib1(tree)), t2, s2 * sizeof(TTree)); /* ...t2 */ | 661 | memcpy(sib1(sib1(tree)), t2, s2 * sizeof(TTree)); /* ...t2 */ |
| 662 | memcpy(sib2(tree), t1, s1 * sizeof(TTree)); /* ... and t1 */ | 662 | memcpy(sib2(tree), t1, s1 * sizeof(TTree)); /* ... and t1 */ |
| @@ -869,7 +869,7 @@ static int lp_constcapture (lua_State *L) { | |||
| 869 | tree = sib1(tree); | 869 | tree = sib1(tree); |
| 870 | for (i = 1; i <= n - 1; i++) { | 870 | for (i = 1; i <= n - 1; i++) { |
| 871 | tree->tag = TSeq; | 871 | tree->tag = TSeq; |
| 872 | tree->u.s.ps = 3; /* skip TCapture and its sibling */ | 872 | tree->u.ps = 3; /* skip TCapture and its sibling */ |
| 873 | auxemptycap(sib1(tree), Cconst); | 873 | auxemptycap(sib1(tree), Cconst); |
| 874 | sib1(tree)->key = addtoktable(L, i); | 874 | sib1(tree)->key = addtoktable(L, i); |
| 875 | tree = sib2(tree); | 875 | tree = sib2(tree); |
| @@ -971,7 +971,7 @@ static void buildgrammar (lua_State *L, TTree *grammar, int frule, int n) { | |||
| 971 | nd->tag = TRule; | 971 | nd->tag = TRule; |
| 972 | nd->key = 0; /* will be fixed when rule is used */ | 972 | nd->key = 0; /* will be fixed when rule is used */ |
| 973 | nd->cap = i; /* rule number */ | 973 | nd->cap = i; /* rule number */ |
| 974 | nd->u.s.ps = rulesize + 1; /* point to next rule */ | 974 | nd->u.ps = rulesize + 1; /* point to next rule */ |
| 975 | memcpy(sib1(nd), rn, rulesize * sizeof(TTree)); /* copy rule */ | 975 | memcpy(sib1(nd), rn, rulesize * sizeof(TTree)); /* copy rule */ |
| 976 | mergektable(L, ridx, sib1(nd)); /* merge its ktable into new one */ | 976 | mergektable(L, ridx, sib1(nd)); /* merge its ktable into new one */ |
| 977 | nd = sib2(nd); /* move to next rule */ | 977 | nd = sib2(nd); /* move to next rule */ |
| @@ -51,11 +51,7 @@ typedef struct TTree { | |||
| 51 | unsigned short key; /* key in ktable for Lua data (0 if no key) */ | 51 | unsigned short key; /* key in ktable for Lua data (0 if no key) */ |
| 52 | union { | 52 | union { |
| 53 | int n; /* occasional counter */ | 53 | int n; /* occasional counter */ |
| 54 | int label; /* labeled failure */ | 54 | int ps; /* occasional second child */ |
| 55 | struct { /* labeled failure */ | ||
| 56 | int ps; /* occasional second child */ | ||
| 57 | int plab; /* occasional label set */ | ||
| 58 | } s; /* labeled failure */ | ||
| 59 | } u; | 55 | } u; |
| 60 | } TTree; | 56 | } TTree; |
| 61 | 57 | ||
| @@ -76,7 +72,7 @@ extern const byte numsiblings[]; | |||
| 76 | 72 | ||
| 77 | /* access to children */ | 73 | /* access to children */ |
| 78 | #define sib1(t) ((t) + 1) | 74 | #define sib1(t) ((t) + 1) |
| 79 | #define sib2(t) ((t) + (t)->u.s.ps) | 75 | #define sib2(t) ((t) + (t)->u.ps) |
| 80 | 76 | ||
| 81 | 77 | ||
| 82 | 78 | ||
