aboutsummaryrefslogtreecommitdiff
path: root/lptree.c
diff options
context:
space:
mode:
authorSergio Queiroz <sqmedeiros@gmail.com>2017-07-06 16:01:44 -0300
committerSergio Queiroz <sqmedeiros@gmail.com>2017-07-06 16:01:44 -0300
commite2a8e1c789a7a769c83a66d6fd2f191b0e465cba (patch)
treee2aecc3d439e44db98439c26cf23625da1bedb3f /lptree.c
parenta36fe71ac0271d9013f76d9342f9a2fca40f64fc (diff)
downloadlpeglabel-e2a8e1c789a7a769c83a66d6fd2f191b0e465cba.tar.gz
lpeglabel-e2a8e1c789a7a769c83a66d6fd2f191b0e465cba.tar.bz2
lpeglabel-e2a8e1c789a7a769c83a66d6fd2f191b0e465cba.zip
Reintroducing the labeled ordered choice (tests without it passed)
Diffstat (limited to 'lptree.c')
-rw-r--r--lptree.c35
1 files changed, 24 insertions, 11 deletions
diff --git a/lptree.c b/lptree.c
index fcf5ff9..19d6b1a 100644
--- a/lptree.c
+++ b/lptree.c
@@ -28,7 +28,7 @@ const byte numsiblings[] = {
28 0, 0, 2, 1, /* call, opencall, rule, grammar */ 28 0, 0, 2, 1, /* call, opencall, rule, grammar */
29 1, /* behind */ 29 1, /* behind */
30 1, 1, /* capture, runtime capture */ 30 1, 1, /* capture, runtime capture */
31 0, 2 /* labeled failure throw, recovery */ 31 0, 2, 2 /* labeled failure throw, recovery, labeled choice */
32}; 32};
33 33
34 34
@@ -734,16 +734,28 @@ static int lp_throw (lua_State *L) {
734static int lp_recovery (lua_State *L) { 734static int lp_recovery (lua_State *L) {
735 int n = lua_gettop(L); 735 int n = lua_gettop(L);
736 TTree *tree = newrootlab2sib(L, TRecov); 736 TTree *tree = newrootlab2sib(L, TRecov);
737 int i;
737 luaL_argcheck(L, n >= 3, 3, "non-nil value expected"); 738 luaL_argcheck(L, n >= 3, 3, "non-nil value expected");
738 if (n == 2) { /* catches fail as default */ 739 for (i = 3; i <= n; i++) {
739 /*setlabel(treelabelset(tree), LFAIL); recovery does not catch regular fail */ 740 int d = luaL_checkinteger(L, i);
740 } else { 741 luaL_argcheck(L, d >= 1 && d < MAXLABELS, i, "the number of a label must be between 1 and 255");
741 int i; 742 setlabel(treelabelset(tree), (byte)d);
742 for (i = 3; i <= n; i++) { 743 }
743 int d = luaL_checkinteger(L, i); 744 return 1;
744 luaL_argcheck(L, d >= 1 && d < MAXLABELS, i, "the number of a label must be between 1 and 255"); 745}
745 setlabel(treelabelset(tree), (byte)d); 746
746 } 747
748/*
749** labeled choice function
750*/
751static int lp_labchoice (lua_State *L) {
752 int n = lua_gettop(L);
753 TTree *tree = newrootlab2sib(L, TLabChoice);
754 int i;
755 for (i = 3; i <= n; i++) {
756 int d = luaL_checkinteger(L, i);
757 luaL_argcheck(L, d >= 1 && d < MAXLABELS, i, "the number of a label must be between 1 and 255");
758 setlabel(treelabelset(tree), (byte)d);
747 } 759 }
748 return 1; 760 return 1;
749} 761}
@@ -1083,7 +1095,7 @@ static int verifyrule (lua_State *L, TTree *tree, int *passed, int npassed,
1083 return nb; 1095 return nb;
1084 /* else return verifyrule(L, sib2(tree), passed, npassed, nb); */ 1096 /* else return verifyrule(L, sib2(tree), passed, npassed, nb); */
1085 tree = sib2(tree); goto tailcall; 1097 tree = sib2(tree); goto tailcall;
1086 case TChoice: case TRecov: /* must check both children */ /* labeled failure */ 1098 case TChoice: case TRecov: case TLabChoice: /* must check both children */ /* labeled failure */
1087 nb = verifyrule(L, sib1(tree), passed, npassed, nb); 1099 nb = verifyrule(L, sib1(tree), passed, npassed, nb);
1088 /* return verifyrule(L, sib2(tree), passed, npassed, nb); */ 1100 /* return verifyrule(L, sib2(tree), passed, npassed, nb); */
1089 tree = sib2(tree); goto tailcall; 1101 tree = sib2(tree); goto tailcall;
@@ -1337,6 +1349,7 @@ static struct luaL_Reg pattreg[] = {
1337 {"type", lp_type}, 1349 {"type", lp_type},
1338 {"T", lp_throw}, /* labeled failure throw */ 1350 {"T", lp_throw}, /* labeled failure throw */
1339 {"Rec", lp_recovery}, /* labeled failure recovery */ 1351 {"Rec", lp_recovery}, /* labeled failure recovery */
1352 {"Lc", lp_labchoice}, /* labeled failure choice */
1340 {NULL, NULL} 1353 {NULL, NULL}
1341}; 1354};
1342 1355