From 80ec9f932aa01d445e86c699523265359055e1bd Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 17 Jun 2024 16:25:25 -0300 Subject: Small improvements --- lpeg.html | 30 +++++++++++++++++++++--------- lptree.c | 2 +- lptypes.h | 1 + makefile | 1 + test.lua | 2 +- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/lpeg.html b/lpeg.html index b31d575..bd54294 100644 --- a/lpeg.html +++ b/lpeg.html @@ -53,19 +53,22 @@

Introduction

-LPeg is a new pattern-matching library for Lua, +LPeg is a pattern-matching library for Lua, based on Parsing Expression Grammars (PEGs). This text is a reference manual for the library. +For those starting with LPeg, + +Mastering LPeg presents a good tutorial. For a more formal treatment of LPeg, as well as some discussion about its implementation, see A Text Pattern-Matching Tool based on Parsing Expression Grammars. -(You may also be interested in my +You may also be interested in my talk about LPeg -given at the III Lua Workshop.) +given at the III Lua Workshop.

@@ -664,13 +667,22 @@ produces values for each match.

Usually, -LPeg does not specify when (and if) it evaluates its captures. -(As an example, -consider the pattern lpeg.P"a" / func / 0. -Because the "division" by 0 instructs LPeg to throw away the -results from the pattern, -it is not specified whether LPeg will call func.) +LPeg does not specify when, if, or how many times it evaluates its captures. Therefore, captures should avoid side effects. +As an example, +LPeg may or may not call func in the pattern +lpeg.P"a" / func / 0, +given that the "division" by 0 +instructs LPeg to throw away the +results from the pattern. +Similarly, a capture nested inside a named group +may be evaluated only when that group is referred in a +back capture; +if there are multiple back captures, +the group may be evaluated multiple times. +

+ +

Moreover, captures cannot affect the way a pattern matches a subject. The only exception to this rule is the diff --git a/lptree.c b/lptree.c index 475b0c3..a176ad9 100644 --- a/lptree.c +++ b/lptree.c @@ -1123,7 +1123,7 @@ static int verifyrule (lua_State *L, TTree *tree, unsigned short *passed, return verifyerror(L, passed, npassed); /* error */ else { passed[npassed++] = tree->key; /* add rule to path */ - /* return verifyrule(L, sib1(tree), passed, npassed); */ + /* return verifyrule(L, sib1(tree), passed, npassed, nb); */ tree = sib1(tree); goto tailcall; } case TGrammar: diff --git a/lptypes.h b/lptypes.h index 3f860b9..b5cea44 100644 --- a/lptypes.h +++ b/lptypes.h @@ -34,6 +34,7 @@ #define lua_rawlen lua_objlen +#undef luaL_newlib #define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f) #define luaL_newlib(L,f) luaL_register(L,"lpeg",f) diff --git a/makefile b/makefile index 41a2928..82a95bb 100644 --- a/makefile +++ b/makefile @@ -1,5 +1,6 @@ LIBNAME = lpeg LUADIR = ./lua/ +#LUADIR = /home/roberto/prj/lua/ COPT = -O2 -DNDEBUG # COPT = -O0 -DLPEG_DEBUG -g diff --git a/test.lua b/test.lua index d0b82da..3241408 100755 --- a/test.lua +++ b/test.lua @@ -47,7 +47,7 @@ end print"General tests for LPeg library" assert(type(m.version) == "string") -print(m.version) +print(m.version, _VERSION) assert(m.type("alo") ~= "pattern") assert(m.type(io.input) ~= "pattern") assert(m.type(m.P"alo") == "pattern") -- cgit v1.2.3-55-g6feb