Skip to content

Commit 0671e87

Browse files
committed
Updating LPegLabel to the codebase of LPeg 1.0.2
1 parent 0f48dad commit 0671e87

13 files changed

+152
-99
lines changed

lpcap.c

+41-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lpcap.c,v 1.6 2015/06/15 16:09:57 roberto Exp $
2+
** $Id: lpcap.c $
33
** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license)
44
*/
55

@@ -271,15 +271,15 @@ int finddyncap (Capture *cap, Capture *last) {
271271

272272

273273
/*
274-
** Calls a runtime capture. Returns number of captures removed by
275-
** the call, including the initial Cgroup. (Captures to be added are
276-
** on the Lua stack.)
274+
** Calls a runtime capture. Returns number of captures "removed" by the
275+
** call, that is, those inside the group capture. Captures to be added
276+
** are on the Lua stack.
277277
*/
278278
int runtimecap (CapState *cs, Capture *close, const char *s, int *rem) {
279279
int n, id;
280280
lua_State *L = cs->L;
281281
int otop = lua_gettop(L);
282-
Capture *open = findopen(close);
282+
Capture *open = findopen(close); /* get open group capture */
283283
assert(captype(open) == Cgroup);
284284
id = finddyncap(open, close); /* get first dynamic capture argument */
285285
close->kind = Cclose; /* closes the group */
@@ -299,7 +299,7 @@ int runtimecap (CapState *cs, Capture *close, const char *s, int *rem) {
299299
}
300300
else
301301
*rem = 0; /* no dynamic captures removed */
302-
return close - open; /* number of captures of all kinds removed */
302+
return close - open - 1; /* number of captures to be removed */
303303
}
304304

305305

@@ -441,70 +441,88 @@ static int addonestring (luaL_Buffer *b, CapState *cs, const char *what) {
441441
}
442442

443443

444+
#if !defined(MAXRECLEVEL)
445+
#define MAXRECLEVEL 200
446+
#endif
447+
448+
444449
/*
445450
** Push all values of the current capture into the stack; returns
446451
** number of values pushed
447452
*/
448453
static int pushcapture (CapState *cs) {
449454
lua_State *L = cs->L;
455+
int res;
450456
luaL_checkstack(L, 4, "too many captures");
457+
if (cs->reclevel++ > MAXRECLEVEL)
458+
return luaL_error(L, "subcapture nesting too deep");
451459
switch (captype(cs->cap)) {
452460
case Cposition: {
453461
lua_pushinteger(L, cs->cap->s - cs->s + 1);
454462
cs->cap++;
455-
return 1;
463+
res = 1;
464+
break;
456465
}
457466
case Cconst: {
458467
pushluaval(cs);
459468
cs->cap++;
460-
return 1;
469+
res = 1;
470+
break;
461471
}
462472
case Carg: {
463473
int arg = (cs->cap++)->idx;
464474
if (arg + FIXEDARGS > cs->ptop)
465475
return luaL_error(L, "reference to absent extra argument #%d", arg);
466476
lua_pushvalue(L, arg + FIXEDARGS);
467-
return 1;
477+
res = 1;
478+
break;
468479
}
469480
case Csimple: {
470481
int k = pushnestedvalues(cs, 1);
471482
lua_insert(L, -k); /* make whole match be first result */
472-
return k;
483+
res = k;
484+
break;
473485
}
474486
case Cruntime: {
475487
lua_pushvalue(L, (cs->cap++)->idx); /* value is in the stack */
476-
return 1;
488+
res = 1;
489+
break;
477490
}
478491
case Cstring: {
479492
luaL_Buffer b;
480493
luaL_buffinit(L, &b);
481494
stringcap(&b, cs);
482495
luaL_pushresult(&b);
483-
return 1;
496+
res = 1;
497+
break;
484498
}
485499
case Csubst: {
486500
luaL_Buffer b;
487501
luaL_buffinit(L, &b);
488502
substcap(&b, cs);
489503
luaL_pushresult(&b);
490-
return 1;
504+
res = 1;
505+
break;
491506
}
492507
case Cgroup: {
493508
if (cs->cap->idx == 0) /* anonymous group? */
494-
return pushnestedvalues(cs, 0); /* add all nested values */
509+
res = pushnestedvalues(cs, 0); /* add all nested values */
495510
else { /* named group: add no values */
496511
nextcap(cs); /* skip capture */
497-
return 0;
512+
res = 0;
498513
}
514+
break;
499515
}
500-
case Cbackref: return backrefcap(cs);
501-
case Ctable: return tablecap(cs);
502-
case Cfunction: return functioncap(cs);
503-
case Cnum: return numcap(cs);
504-
case Cquery: return querycap(cs);
505-
case Cfold: return foldcap(cs);
506-
default: assert(0); return 0;
516+
case Cbackref: res = backrefcap(cs); break;
517+
case Ctable: res = tablecap(cs); break;
518+
case Cfunction: res = functioncap(cs); break;
519+
case Cnum: res = numcap(cs); break;
520+
case Cquery: res = querycap(cs); break;
521+
case Cfold: res = foldcap(cs); break;
522+
default: assert(0); res = 0;
507523
}
524+
cs->reclevel--;
525+
return res;
508526
}
509527

510528

@@ -521,7 +539,7 @@ int getcaptures (lua_State *L, const char *s, const char *r, int ptop) {
521539
int n = 0;
522540
if (!isclosecap(capture)) { /* is there any capture? */
523541
CapState cs;
524-
cs.ocap = cs.cap = capture; cs.L = L;
542+
cs.ocap = cs.cap = capture; cs.L = L; cs.reclevel = 0;
525543
cs.s = s; cs.valuecached = 0; cs.ptop = ptop;
526544
do { /* collect their values */
527545
n += pushcapture(&cs);

lpcap.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lpcap.h,v 1.3 2016/09/13 17:45:58 roberto Exp $
2+
** $Id: lpcap.h $
33
*/
44

55
#if !defined(lpcap_h)
@@ -44,6 +44,7 @@ typedef struct CapState {
4444
int ptop; /* index of last argument to 'match' */
4545
const char *s; /* original string */
4646
int valuecached; /* value stored in cache slot */
47+
int reclevel; /* recursion level */
4748
} CapState;
4849

4950

lpcode.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lpcode.c,v 1.24 2016/09/15 17:46:13 roberto Exp $
2+
** $Id: lpcode.c $
33
** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license)
44
*/
55

lpcode.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lpcode.h,v 1.8 2016/09/15 17:46:13 roberto Exp $
2+
** $Id: lpcode.h $
33
*/
44

55
#if !defined(lpcode_h)

lpprint.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lpprint.c,v 1.10 2016/09/13 16:06:03 roberto Exp $
2+
** $Id: lpprint.c $
33
** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license)
44
*/
55

lpprint.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lpprint.h,v 1.2 2015/06/12 18:18:08 roberto Exp $
2+
** $Id: lpprint.h $
33
*/
44

55

lptree.c

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lptree.c,v 1.22 2016/09/13 18:10:22 roberto Exp $
2+
** $Id: lptree.c $
33
** Copyright 2013, Lua.org & PUC-Rio (see 'lpeg.html' for license)
44
*/
55

@@ -750,6 +750,7 @@ static int capture_aux (lua_State *L, int cap, int labelidx) {
750750

751751
/*
752752
** Fill a tree with an empty capture, using an empty (TTrue) sibling.
753+
** (The 'key' field must be filled by the caller to finish the tree.)
753754
*/
754755
static TTree *auxemptycap (TTree *tree, int cap) {
755756
tree->tag = TCapture;
@@ -760,15 +761,17 @@ static TTree *auxemptycap (TTree *tree, int cap) {
760761

761762

762763
/*
763-
** Create a tree for an empty capture
764+
** Create a tree for an empty capture.
764765
*/
765-
static TTree *newemptycap (lua_State *L, int cap) {
766-
return auxemptycap(newtree(L, 2), cap);
766+
static TTree *newemptycap (lua_State *L, int cap, int key) {
767+
TTree *tree = auxemptycap(newtree(L, 2), cap);
768+
tree->key = key;
769+
return tree;
767770
}
768771

769772

770773
/*
771-
** Create a tree for an empty capture with an associated Lua value
774+
** Create a tree for an empty capture with an associated Lua value.
772775
*/
773776
static TTree *newemptycapkey (lua_State *L, int cap, int idx) {
774777
TTree *tree = auxemptycap(newtree(L, 2), cap);
@@ -829,16 +832,15 @@ static int lp_simplecapture (lua_State *L) {
829832

830833

831834
static int lp_poscapture (lua_State *L) {
832-
newemptycap(L, Cposition);
835+
newemptycap(L, Cposition, 0);
833836
return 1;
834837
}
835838

836839

837840
static int lp_argcapture (lua_State *L) {
838841
int n = (int)luaL_checkinteger(L, 1);
839-
TTree *tree = newemptycap(L, Carg);
840-
tree->key = n;
841842
luaL_argcheck(L, 0 < n && n <= SHRT_MAX, 1, "invalid argument index");
843+
newemptycap(L, Carg, n);
842844
return 1;
843845
}
844846

lptree.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lptree.h,v 1.3 2016/09/13 18:07:51 roberto Exp $
2+
** $Id: lptree.h $
33
*/
44

55
#if !defined(lptree_h)

lptypes.h

+3-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,21 @@
11
/*
2-
** $Id: lptypes.h,v 1.16 2017/01/13 13:33:17 roberto Exp $
2+
** $Id: lptypes.h $
33
** LPeg - PEG pattern matching for Lua
4-
** Copyright 2007-2017, Lua.org & PUC-Rio (see 'lpeg.html' for license)
4+
** Copyright 2007-2019, Lua.org & PUC-Rio (see 'lpeg.html' for license)
55
** written by Roberto Ierusalimschy
66
*/
77

88
#if !defined(lptypes_h)
99
#define lptypes_h
1010

1111

12-
#if !defined(LPEG_DEBUG)
13-
#define NDEBUG
14-
#endif
15-
1612
#include <assert.h>
1713
#include <limits.h>
1814

1915
#include "lua.h"
2016

2117

22-
#define VERSION "1.5.1"
18+
#define VERSION "1.5.2"
2319

2420

2521
#define PATTERN_T "lpeg-pattern"

0 commit comments

Comments
 (0)