diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2019-01-10 10:06:33 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2019-01-10 10:06:33 -0300 |
commit | 4fb816d55f47c48c34cc4478e584b1567a75863b (patch) | |
tree | 54ec6965c9d3983b193cf000a521a06cb3d73b8a /lpvm.c | |
parent | 9be59fb8f4b176a16643e707c74051b243202296 (diff) | |
download | lpeglabel-4fb816d55f47c48c34cc4478e584b1567a75863b.tar.gz lpeglabel-4fb816d55f47c48c34cc4478e584b1567a75863b.tar.bz2 lpeglabel-4fb816d55f47c48c34cc4478e584b1567a75863b.zip |
Adapting lpeglabel-1.5 to the codebase of lpeg-1.0.0lpeg-1.0.0
Diffstat (limited to 'lpvm.c')
-rw-r--r-- | lpvm.c | 30 |
1 files changed, 11 insertions, 19 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lpvm.c,v 1.9 2016/06/03 20:11:18 roberto Exp $ | 2 | ** $Id: lpvm.c,v 1.6 2015/09/28 17:01:25 roberto Exp $ |
3 | ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) | 3 | ** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license) |
4 | */ | 4 | */ |
5 | 5 | ||
@@ -47,16 +47,14 @@ typedef struct Stack { | |||
47 | 47 | ||
48 | 48 | ||
49 | /* | 49 | /* |
50 | ** Make the size of the array of captures 'cap' twice as large as needed | 50 | ** Double the size of the array of captures |
51 | ** (which is 'captop'). ('n' is the number of new elements.) | ||
52 | */ | 51 | */ |
53 | static Capture *doublecap (lua_State *L, Capture *cap, int captop, | 52 | static Capture *doublecap (lua_State *L, Capture *cap, int captop, int ptop) { |
54 | int n, int ptop) { | ||
55 | Capture *newc; | 53 | Capture *newc; |
56 | if (captop >= INT_MAX/((int)sizeof(Capture) * 2)) | 54 | if (captop >= INT_MAX/((int)sizeof(Capture) * 2)) |
57 | luaL_error(L, "too many captures"); | 55 | luaL_error(L, "too many captures"); |
58 | newc = (Capture *)lua_newuserdata(L, captop * 2 * sizeof(Capture)); | 56 | newc = (Capture *)lua_newuserdata(L, captop * 2 * sizeof(Capture)); |
59 | memcpy(newc, cap, (captop - n) * sizeof(Capture)); | 57 | memcpy(newc, cap, captop * sizeof(Capture)); |
60 | lua_replace(L, caplistidx(ptop)); | 58 | lua_replace(L, caplistidx(ptop)); |
61 | return newc; | 59 | return newc; |
62 | } | 60 | } |
@@ -117,8 +115,8 @@ static int resdyncaptures (lua_State *L, int fr, int curr, int limit) { | |||
117 | */ | 115 | */ |
118 | static void adddyncaptures (const char *s, Capture *base, int n, int fd) { | 116 | static void adddyncaptures (const char *s, Capture *base, int n, int fd) { |
119 | int i; | 117 | int i; |
120 | base[0].kind = Cgroup; /* create group capture */ | 118 | /* Cgroup capture is already there */ |
121 | base[0].siz = 0; | 119 | assert(base[0].kind == Cgroup && base[0].siz == 0); |
122 | base[0].idx = 0; /* make it an anonymous group */ | 120 | base[0].idx = 0; /* make it an anonymous group */ |
123 | for (i = 1; i <= n; i++) { /* add runtime captures */ | 121 | for (i = 1; i <= n; i++) { /* add runtime captures */ |
124 | base[i].kind = Cruntime; | 122 | base[i].kind = Cruntime; |
@@ -163,11 +161,10 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, | |||
163 | lua_pushlightuserdata(L, stackbase); | 161 | lua_pushlightuserdata(L, stackbase); |
164 | for (;;) { | 162 | for (;;) { |
165 | #if defined(DEBUG) | 163 | #if defined(DEBUG) |
166 | printf("-------------------------------------\n"); | ||
167 | printcaplist(capture, capture + captop); | ||
168 | printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", | 164 | printf("s: |%s| stck:%d, dyncaps:%d, caps:%d ", |
169 | s, (int)(stack - getstackbase(L, ptop)), ndyncap, captop); | 165 | s, stack - getstackbase(L, ptop), ndyncap, captop); |
170 | printinst(op, p); | 166 | printinst(op, p); |
167 | printcaplist(capture, capture + captop); | ||
171 | #endif | 168 | #endif |
172 | assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); | 169 | assert(stackidx(ptop) + ndyncap == lua_gettop(L) && ndyncap <= captop); |
173 | assert(insidepred == INPRED || insidepred == OUTPRED); | 170 | assert(insidepred == INPRED || insidepred == OUTPRED); |
@@ -365,9 +362,6 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, | |||
365 | assert((insidepred == INPRED && stack->labenv == OUTPRED) || insidepred == stack->labenv); | 362 | assert((insidepred == INPRED && stack->labenv == OUTPRED) || insidepred == stack->labenv); |
366 | insidepred = stack->labenv; /* labeled failure */ | 363 | insidepred = stack->labenv; /* labeled failure */ |
367 | p = stack->p; | 364 | p = stack->p; |
368 | #if defined(DEBUG) | ||
369 | printf("**FAIL**\n"); | ||
370 | #endif | ||
371 | continue; | 365 | continue; |
372 | } | 366 | } |
373 | case ICloseRunTime: { | 367 | case ICloseRunTime: { |
@@ -377,7 +371,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, | |||
377 | cs.s = o; cs.L = L; cs.ocap = capture; cs.ptop = ptop; | 371 | cs.s = o; cs.L = L; cs.ocap = capture; cs.ptop = ptop; |
378 | n = runtimecap(&cs, capture + captop, s, &rem); /* call function */ | 372 | n = runtimecap(&cs, capture + captop, s, &rem); /* call function */ |
379 | captop -= n; /* remove nested captures */ | 373 | captop -= n; /* remove nested captures */ |
380 | ndyncap -= rem; /* update number of dynamic captures */ | 374 | ndyncap += n - rem; /* update number of dynamic captures */ |
381 | fr -= rem; /* 'rem' items were popped from Lua stack */ | 375 | fr -= rem; /* 'rem' items were popped from Lua stack */ |
382 | res = resdyncaptures(L, fr, s - o, e - o); /* get result */ | 376 | res = resdyncaptures(L, fr, s - o, e - o); /* get result */ |
383 | if (res == -1) { /* fail? */ | 377 | if (res == -1) { /* fail? */ |
@@ -389,10 +383,8 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, | |||
389 | n = lua_gettop(L) - fr + 1; /* number of new captures */ | 383 | n = lua_gettop(L) - fr + 1; /* number of new captures */ |
390 | ndyncap += n; /* update number of dynamic captures */ | 384 | ndyncap += n; /* update number of dynamic captures */ |
391 | if (n > 0) { /* any new capture? */ | 385 | if (n > 0) { /* any new capture? */ |
392 | if (fr + n >= SHRT_MAX) | ||
393 | luaL_error(L, "too many results in match-time capture"); | ||
394 | if ((captop += n + 2) >= capsize) { | 386 | if ((captop += n + 2) >= capsize) { |
395 | capture = doublecap(L, capture, captop, n + 2, ptop); | 387 | capture = doublecap(L, capture, captop, ptop); |
396 | capsize = 2 * captop; | 388 | capsize = 2 * captop; |
397 | } | 389 | } |
398 | /* add new captures to 'capture' list */ | 390 | /* add new captures to 'capture' list */ |
@@ -429,7 +421,7 @@ const char *match (lua_State *L, const char *o, const char *s, const char *e, | |||
429 | capture[captop].idx = p->i.key; | 421 | capture[captop].idx = p->i.key; |
430 | capture[captop].kind = getkind(p); | 422 | capture[captop].kind = getkind(p); |
431 | if (++captop >= capsize) { | 423 | if (++captop >= capsize) { |
432 | capture = doublecap(L, capture, captop, 0, ptop); | 424 | capture = doublecap(L, capture, captop, ptop); |
433 | capsize = 2 * captop; | 425 | capsize = 2 * captop; |
434 | } | 426 | } |
435 | p++; | 427 | p++; |