From 5ff36d597a47830ea0e266cce253cfda2cf43348 Mon Sep 17 00:00:00 2001 From: Sergio Queiroz Date: Fri, 1 Jul 2016 10:09:27 -0300 Subject: Updating code given that lpeglabel.T only throws one label --- lpcode.c | 24 +++--------------------- lptree.c | 24 ++++++++---------------- lptypes.h | 2 +- lpvm.c | 46 ++++++++++++++++------------------------------ lpvm.h | 2 +- makefile | 4 ++-- test.lua | 1 + testlabel.lua | 3 +-- 8 files changed, 33 insertions(+), 73 deletions(-) diff --git a/lpcode.c b/lpcode.c index 9fa8867..50e6764 100644 --- a/lpcode.c +++ b/lpcode.c @@ -11,7 +11,7 @@ #include "lptypes.h" #include "lpcode.h" -#include "lpprint.h" /* labeled failure */ + /* signals a "no-instruction */ #define NOINST -1 @@ -492,25 +492,10 @@ static int addinstruction (CompileState *compst, Opcode op, int aux) { static int addoffsetinst (CompileState *compst, Opcode op) { int i = addinstruction(compst, op, 0); /* instruction */ addinstruction(compst, (Opcode)0, 0); /* open space for offset */ - assert(op == ITestSet || sizei(&getinstr(compst, i)) == 2 || op == ILabChoice); /* labeled failure */ + assert(op == ITestSet || sizei(&getinstr(compst, i)) == 2); return i; } -/* labeled failure begin */ -static int addthrowinstruction (CompileState *compst, byte lab) { - return addinstruction(compst, IThrow, lab); -} - -/*static int addoffsetlabinst (CompileState *compst, const byte *cs) { - int j; - int i = addinstruction(compst, ILabChoice, 0); - addinstruction(compst, (Opcode)0, 0); - j = nextinstruction(compst); - getinstr(compst, j).labels = cs; - - return i; -}*/ -/* labeled failure end */ /* ** Set the offset of an instruction @@ -720,17 +705,14 @@ static void codelabchoice (CompileState *compst, TTree *p1, TTree *p2, int opt, int emptyp2 = (p2->tag == TTrue); int pcommit; int test = NOINST; - /* int pchoice = addoffsetlabinst(compst, cs);*/ int pchoice = addoffsetinst(compst, ILabChoice); addcharset(compst, cs); codegen(compst, p1, emptyp2, test, fullset); pcommit = addoffsetinst(compst, ICommit); jumptohere(compst, pchoice); jumptohere(compst, test); - /*printf("vou codificar codelabchoice %d\n", p2->tag);*/ codegen(compst, p2, opt, NOINST, fl); jumptohere(compst, pcommit); - /*printf("fim codelabchoice\n");*/ } /* labeled failure end */ @@ -962,7 +944,7 @@ static void codegen (CompileState *compst, TTree *tree, int opt, int tt, tree = sib2(tree); goto tailcall; } case TThrow: { /* labeled failure */ - addthrowinstruction(compst, (byte) tree->u.label); + addinstruction(compst, IThrow, (byte) tree->u.label); break; } case TLabChoice: { /* labeled failure */ diff --git a/lptree.c b/lptree.c index b974bbb..6d0f78c 100644 --- a/lptree.c +++ b/lptree.c @@ -34,6 +34,7 @@ const byte numsiblings[] = { static TTree *newgrammar (lua_State *L, int arg); + /* ** returns a reasonable name for value at index 'idx' on the stack */ @@ -563,7 +564,7 @@ static int lp_seq (lua_State *L) { else if (tree1->tag == TTrue) lua_pushvalue(L, 2); /* true . x = x */ else - newroot2sib(L, TSeq); + newroot2sib(L, TSeq); return 1; } @@ -718,11 +719,11 @@ static int lp_behind (lua_State *L) { /* labeled failure begin */ /* -** Throws a label +** Throws a label */ static int lp_throw (lua_State *L) { int label = luaL_checkinteger(L, -1); - luaL_argcheck(L, label >= 0 && label < MAXLABELS, -1, "max or min label index exceeded"); + luaL_argcheck(L, label >= 0 && label < MAXLABELS, -1, "the number of a label must be between 0 and 255"); newthrowleaf(L, label); return 1; } @@ -736,7 +737,7 @@ static int lp_labchoice (lua_State *L) { int i; for (i = 3; i <= n; i++) { int d = luaL_checkinteger(L, i); - luaL_argcheck(L, d >= 0 && d < MAXLABELS, i, "max or min label index exceeded"); + luaL_argcheck(L, d >= 0 && d < MAXLABELS, i, "the number of a label must be between 0 and 255"); setlabel(treelabelset(tree), (byte)d); } return 1; @@ -1209,26 +1210,17 @@ static int lp_match (lua_State *L) { const char *s = luaL_checklstring(L, SUBJIDX, &l); size_t i = initposition(L, l); int ptop = lua_gettop(L); - Labelset labelf; /* labeled failure */ + byte labelf; /* labeled failure */ const char *sfail = NULL; /* labeled failure */ lua_pushnil(L); /* initialize subscache */ lua_pushlightuserdata(L, capture); /* initialize caplistidx */ lua_getuservalue(L, 1); /* initialize penvidx */ r = match(L, s, s + i, s + l, code, capture, ptop, &labelf, &sfail); /* labeled failure */ if (r == NULL) { /* labeled failure begin */ - long long int j = 0; - int n = 1; lua_pushnil(L); - while (j < MAXLABELS) { - if (testlabel(labelf.cs, j)) { - lua_pushinteger(L, j); - n++; - break; /* Changing the semantics: only one label */ - } - j++; - } + lua_pushinteger(L, labelf); lua_pushstring(L, sfail); /* Pushing the subject where the failure occurred */ - return n + 1; + return 3; } /* labeled failure end */ return getcaptures(L, s, r, ptop); } diff --git a/lptypes.h b/lptypes.h index f61c0ff..dc4ada6 100644 --- a/lptypes.h +++ b/lptypes.h @@ -160,7 +160,7 @@ typedef Charset Labelset; #define IDXLFAIL 0 -#define testlabel testchar +#define LFAIL 0 /* labeled failure end */ #endif diff --git a/lpvm.c b/lpvm.c index a57c4b3..4d9797c 100644 --- a/lpvm.c +++ b/lpvm.c @@ -31,17 +31,6 @@ static void setlabelfail(Labelset *ls) { loopset(i, ls->cs[i] = 0); ls->cs[IDXLFAIL] = 1; } - -static void clearandsetlabel(Labelset *ls, byte b) { - loopset(i, ls->cs[i] = 0); - setlabel(ls->cs, b); -} - - -static int cs_disjoint (const Charset *cs1, const Charset *cs2) { - loopset(i, if ((cs1->cs[i] & cs2->cs[i]) != 0) return 0;) - return 1; -} /* labeled failure end */ /* @@ -54,8 +43,8 @@ static int cs_disjoint (const Charset *cs1, const Charset *cs2) { typedef struct Stack { const char *s; /* saved position (or NULL for calls) */ const Instruction *p; /* next instruction */ - int caplevel; const Labelset *ls; /* labeled failure */ + int caplevel; } Stack; @@ -165,7 +154,7 @@ static int removedyncap (lua_State *L, Capture *capture, ** Opcode interpreter */ const char *match (lua_State *L, const char *o, const char *s, const char *e, - Instruction *op, Capture *capture, int ptop, Labelset *labelf, const char **sfail) { /* labeled failure */ + Instruction *op, Capture *capture, int ptop, byte *labelf, const char **sfail) { /* labeled failure */ Stack stackbase[INITBACK]; Stack *stacklimit = stackbase + INITBACK; Stack *stack = stackbase; /* point to first empty slot in stack */ @@ -177,14 +166,13 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, setlabelfail(&lsfail); stack->p = &giveup; stack->s = s; stack->caplevel = 0; stack++; lua_pushlightuserdata(L, stackbase); - /*printf("match: %s\n", s);*/ - for (;;) { -/*#if defined(DEBUG)*/ - /* printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", + for (;;) { +#if defined(DEBUG) + printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", s, stack - getstackbase(L, ptop), ndyncap, captop); - printinst(op, p);*/ - /*printcaplist(capture, capture + captop);*/ -/*#endif*/ + printinst(op, p); + printcaplist(capture, capture + captop); +#endif assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); switch ((Opcode)p->i.code) { case IEnd: { @@ -205,7 +193,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, case IAny: { if (s < e) { p++; s++; } else { - setlabelfail(labelf); /* labeled failure */ + *labelf = LFAIL; /* labeled failure */ *sfail = s; goto fail; } @@ -219,7 +207,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, case IChar: { if ((byte)*s == p->i.aux && s < e) { p++; s++; } else { - setlabelfail(labelf); /* labeled failure */ + *labelf = LFAIL; /* labeled failure */ *sfail = s; goto fail; } @@ -235,7 +223,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, if (testchar((p+1)->buff, c) && s < e) { p += CHARSETINSTSIZE; s++; } else { - setlabelfail(labelf); /* labeled failure */ + *labelf = LFAIL; /* labeled failure */ *sfail = s; goto fail; } @@ -251,7 +239,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, case IBehind: { int n = p->i.aux; if (n > s - o) { - setlabelfail(labelf); /* labeled failure */ + *labelf = LFAIL; /* labeled failure */ *sfail = s; goto fail; } @@ -322,9 +310,8 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, continue; } case IThrow: { /* labeled failure */ - clearandsetlabel(labelf, p->i.aux); + *labelf = p->i.aux; *sfail = s; - /*printf("s = %s, sfail = %s\n", s, *sfail);*/ goto fail; } case IFailTwice: @@ -332,14 +319,13 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, stack--; /* go through */ case IFail: - setlabelfail(labelf); /* labeled failure */ + *labelf = LFAIL; /* labeled failure */ *sfail = s; fail: { /* pattern failed: try to backtrack */ do { /* remove pending calls */ assert(stack > getstackbase(L, ptop)); s = (--stack)->s; - /*printf("s = %s, disj = %d\n", s, cs_disjoint(stack->ls, labelf));*/ - } while (s == NULL || (stack->p != &giveup && cs_disjoint(stack->ls, labelf))); + } while (s == NULL || (stack->p != &giveup && !testlabel(stack->ls->cs, *labelf))); if (ndyncap > 0) /* is there matchtime captures? */ ndyncap -= removedyncap(L, capture, stack->caplevel, captop); captop = stack->caplevel; @@ -356,7 +342,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, fr -= rem; /* 'rem' items were popped from Lua stack */ res = resdyncaptures(L, fr, s - o, e - o); /* get result */ if (res == -1) { /* fail? */ - setlabelfail(labelf); /* labeled failure */ + *labelf = LFAIL; /* labeled failure */ *sfail = (const char *) s; /* TODO: ??? */ goto fail; } diff --git a/lpvm.h b/lpvm.h index 3e006d3..4b4dda5 100644 --- a/lpvm.h +++ b/lpvm.h @@ -53,7 +53,7 @@ typedef union Instruction { void printpatt (Instruction *p, int n); const char *match (lua_State *L, const char *o, const char *s, const char *e, - Instruction *op, Capture *capture, int ptop, Labelset *labelf, const char **sfail); /* labeled failure */ + Instruction *op, Capture *capture, int ptop, byte *labelf, const char **sfail); /* labeled failure */ #endif diff --git a/makefile b/makefile index ce53890..5728b38 100644 --- a/makefile +++ b/makefile @@ -1,8 +1,8 @@ LIBNAME = lpeglabel LUADIR = ../lua/ -#COPT = -O2 -COPT = -DLPEG_DEBUG -g +COPT = -O2 +# COPT = -DLPEG_DEBUG -g CWARNS = -Wall -Wextra -pedantic \ -Waggregate-return \ diff --git a/test.lua b/test.lua index 3f94edc..d5922ac 100755 --- a/test.lua +++ b/test.lua @@ -1115,6 +1115,7 @@ local re = require "relabel" local match, compile = re.match, re.compile + assert(match("a", ".") == 2) assert(match("a", "''") == 1) assert(match("", " ! . ") == 1) diff --git a/testlabel.lua b/testlabel.lua index 67dc36b..f7180a7 100644 --- a/testlabel.lua +++ b/testlabel.lua @@ -1,7 +1,5 @@ local m = require 'lpeglabel' -p = m.Lc(true, true, 1, 3) - local p, r, l, s, serror -- throws a label @@ -135,6 +133,7 @@ assert(p:match("B") == 2) p = m.Lc(m.P"A", m.P(false), 1) + m.P("B") assert(p:match("B") == 2) + --[[ S -> A /{1} 'a' A -> B -- cgit v1.2.3-55-g6feb