Skip to content
This repository was archived by the owner on Nov 20, 2020. It is now read-only.

Commit 14b436c

Browse files
author
kapec
committed
Update to LPeg 0.10.2
1 parent 5019d49 commit 14b436c

File tree

7 files changed

+105
-19
lines changed

7 files changed

+105
-19
lines changed

HISTORY

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ HISTORY for LPeg 0.10
1010
+ works with Lua 5.2
1111
+ consumes less C stack
1212

13+
- "and" predicates do not keep captures
14+
1315
* Changes from version 0.8 to 0.9
1416
-------------------------------
1517
+ The accumulator capture was replaced by a fold capture;

dist.info

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
--- This file is part of LuaDist project
22

33
name = "lpeg"
4-
version = "0.10"
4+
version = "0.10.2"
55

66
desc = "Parsing Expression Grammars For Lua"
77
author = "Roberto Ierusalimschy"

lpeg.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
** $Id: lpeg.c,v 1.112 2010/11/03 17:07:50 roberto Exp $
2+
** $Id: lpeg.c,v 1.114 2011/02/16 15:02:20 roberto Exp $
33
** LPeg - PEG pattern matching for Lua
44
** Copyright 2007, Lua.org & PUC-Rio (see 'lpeg.html' for license)
55
** written by Roberto Ierusalimschy
@@ -209,6 +209,8 @@ typedef struct Capture {
209209
#define setchar(st,c) ((st)[(c) >> 3] |= (1 << ((c) & 7)))
210210

211211

212+
static int target (Instruction *p, int i);
213+
212214

213215
static int sizei (const Instruction *i) {
214216
switch((Opcode)i->i.code) {
@@ -245,7 +247,7 @@ static int getposition (lua_State *L, int t, int i) {
245247

246248
/*
247249
** {======================================================
248-
** Printing patterns
250+
** Printing patterns (for debugging)
249251
** =======================================================
250252
*/
251253

@@ -350,6 +352,7 @@ static void printpatt (Instruction *p) {
350352
}
351353

352354

355+
#if 0
353356
static void printcap (Capture *cap) {
354357
printcapkind(cap->kind);
355358
printf(" (idx: %d - size: %d) -> %p\n", cap->idx, cap->siz, cap->s);
@@ -359,6 +362,7 @@ static void printcap (Capture *cap) {
359362
static void printcaplist (Capture *cap) {
360363
for (; cap->s; cap++) printcap(cap);
361364
}
365+
#endif
362366

363367
/* }====================================================== */
364368

@@ -785,7 +789,8 @@ static void checkrule (lua_State *L, Instruction *op, int from, int to,
785789
for (i = from; i < to; i += sizei(op + i)) {
786790
if (op[i].i.code == IPartialCommit && op[i].i.offset < 0) { /* loop? */
787791
int start = dest(op, i);
788-
assert(op[start - 1].i.code == IChoice && dest(op, start - 1) == i + 1);
792+
assert(op[start - 1].i.code == IChoice &&
793+
dest(op, start - 1) == target(op, i + 1));
789794
if (start <= lastopen) { /* loop does contain an open call? */
790795
if (!verify(L, op, op + start, op + i, postable, rule)) /* check body */
791796
luaL_error(L, "possible infinite loop in %s", val2str(L, rule));
@@ -2345,7 +2350,7 @@ static int matchl (lua_State *L) {
23452350
}
23462351

23472352

2348-
static struct luaL_reg pattreg[] = {
2353+
static struct luaL_Reg pattreg[] = {
23492354
{"match", matchl},
23502355
{"print", printpat_l},
23512356
{"locale", locale_l},
@@ -2371,7 +2376,7 @@ static struct luaL_reg pattreg[] = {
23712376
};
23722377

23732378

2374-
static struct luaL_reg metapattreg[] = {
2379+
static struct luaL_Reg metapattreg[] = {
23752380
{"__add", union_l},
23762381
{"__pow", star_l},
23772382
{"__sub", diff_l},

lpeg.html

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</head>
1111
<body>
1212

13-
<!-- $Id: lpeg.html,v 1.60 2010/11/03 17:42:07 roberto Exp $ -->
13+
<!-- $Id: lpeg.html,v 1.64 2011/02/16 15:01:58 roberto Exp $ -->
1414

1515
<div id="container">
1616

@@ -705,7 +705,7 @@ <h3><a name="cap-b"></a><code>lpeg.Cb (name)</code></h3>
705705
</p>
706706

707707

708-
<h3><a name="cap-cc"></a><code>lpeg.Cc ({value})</code></h3>
708+
<h3><a name="cap-cc"></a><code>lpeg.Cc ([value, ...])</code></h3>
709709
<p>
710710
Creates a <em>constant capture</em>.
711711
This pattern matches the empty string and
@@ -1243,8 +1243,9 @@ <h3>Lua's long strings</h3>
12431243
</p>
12441244

12451245
<pre class="example">
1246-
open = "[" * lpeg.Cg(lpeg.P"="^0, "init") * "[" * lpeg.P"\n"^-1
1247-
close = "]" * lpeg.C(lpeg.P"="^0) * "]"
1246+
equals = lpeg.P"="^0
1247+
open = "[" * lpeg.Cg(equals, "init") * "[" * lpeg.P"\n"^-1
1248+
close = "]" * lpeg.C(equals) * "]"
12481249
closeeq = lpeg.Cmt(close * lpeg.Cb("init"), function (s, i, a, b) return a == b end)
12491250
string = open * lpeg.C((lpeg.P(1) - closeeq)^0) * close /
12501251
function (s, o) return s end
@@ -1368,7 +1369,7 @@ <h3>Arithmetic expressions</h3>
13681369
<h2><a name="download"></a>Download</h2>
13691370

13701371
<p>LPeg
1371-
<a href="http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-0.10.tar.gz">source code</a>.</p>
1372+
<a href="http://www.inf.puc-rio.br/~roberto/lpeg/lpeg-0.10.2.tar.gz">source code</a>.</p>
13721373

13731374

13741375
<h2><a name="license">License</a></h2>
@@ -1412,7 +1413,7 @@ <h2><a name="license">License</a></h2>
14121413

14131414
<div id="about">
14141415
<p><small>
1415-
$Id: lpeg.html,v 1.60 2010/11/03 17:42:07 roberto Exp $
1416+
$Id: lpeg.html,v 1.64 2011/02/16 15:01:58 roberto Exp $
14161417
</small></p>
14171418
</div> <!-- id="about" -->
14181419

re.html

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</head>
1111
<body>
1212

13-
<!-- $Id: re.html,v 1.14 2010/11/03 17:07:19 roberto Exp $ -->
13+
<!-- $Id: re.html,v 1.17 2011/01/10 15:08:06 roberto Exp $ -->
1414

1515
<div id="container">
1616

@@ -154,6 +154,13 @@ <h3><code>re.find (subject, pattern [, init])</code></h3>
154154
Otherwise, returns nil.
155155
</p>
156156

157+
<p>
158+
An optional numeric argument <code>init</code> makes the search
159+
starts at that position in the subject string.
160+
As usual in Lua libraries,
161+
a negative value counts from the end.
162+
</p>
163+
157164
<h3><code>re.match (subject, pattern)</code></h3>
158165
<p>
159166
Matches the given pattern against the given subject.
@@ -225,13 +232,81 @@ <h3>Lua's long strings</h3>
225232
</p>
226233
<pre class="example">
227234
c = re.compile([[
228-
longstring &lt;- ('[' {:eq: '='* :} '[' close) =&gt; void
235+
longstring &lt;- ('[' {:eq: '='* :} '[' close) -&gt; void
229236
close &lt;- ']' =eq ']' / . close
230-
]], {void = function () return true end})
237+
]], {void = function () end})
231238

232239
print(c:match'[==[]]===]]]]==]===[]') --&gt; 17
233240
</pre>
234241

242+
<h3>Abstract Syntax Trees</h3>
243+
<p>
244+
This example shows a simple way to build an
245+
abstract syntax tree (AST) for a given grammar.
246+
To keep our example simple,
247+
let us consider the following grammar
248+
for lists of names:
249+
</p>
250+
<pre class="example">
251+
p = re.compile[[
252+
listname &lt;- (name s)*
253+
name &lt;- [a-z][a-z]*
254+
s &lt;- %s*
255+
]]
256+
</pre>
257+
<p>
258+
Now, we will add captures to build a corresponding AST.
259+
As a first step, the pattern will build a table to
260+
represent each non terminal;
261+
terminals will be represented by their corresponding strings:
262+
</p>
263+
<pre class="example">
264+
c = re.compile[[
265+
listname &lt;- (name s)* -&gt; {}
266+
name &lt;- {[a-z][a-z]*} -&gt; {}
267+
s &lt;- %s*
268+
]]
269+
</pre>
270+
<p>
271+
Now, a match against <code>"hi hello bye"</code>
272+
results in the table
273+
<code>{{"hi"}, {"hello"}, {"bye"}}</code>.
274+
</p>
275+
<p>
276+
For such a simple grammar,
277+
this AST is more than enough;
278+
actually, the tables around each single name
279+
are already overkilling.
280+
More complex grammars,
281+
however, may need some more structure.
282+
Specifically,
283+
it would be useful if each table had
284+
a <code>tag</code> field telling what non terminal
285+
that table represents.
286+
We can add such a tag using
287+
<a href="lpeg.html/#cap-g">named group captures</a>:
288+
</p>
289+
<pre class="example">
290+
x = re.compile[[
291+
listname <- ({:tag: '' -> 'list':} (name s)*) -> {}
292+
name <- ({:tag: '' -> 'id':} {[a-z][a-z]*}) -> {}
293+
s <- ' '*
294+
]]
295+
</pre>
296+
<p>
297+
With these group captures,
298+
a match against <code>"hi hello bye"</code>
299+
results in the following table:
300+
</p>
301+
<pre class="example">
302+
{tag="list",
303+
{tag="id", "hi"},
304+
{tag="id", "hello"},
305+
{tag="id", "bye"}
306+
}
307+
</pre>
308+
309+
235310
<h3>Indented blocks</h3>
236311
<p>
237312
This example breaks indented blocks into tables,
@@ -401,7 +476,7 @@ <h2><a name="license">License</a></h2>
401476

402477
<div id="about">
403478
<p><small>
404-
$Id: re.html,v 1.14 2010/11/03 17:07:19 roberto Exp $
479+
$Id: re.html,v 1.17 2011/01/10 15:08:06 roberto Exp $
405480
</small></p>
406481
</div> <!-- id="about" -->
407482

re.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
-- $Id: re.lua,v 1.38 2010/11/03 17:21:07 roberto Exp $
1+
-- $Id: re.lua,v 1.39 2010/11/04 19:44:18 roberto Exp $
22

33
-- imported functions and modules
44
local tonumber, type, print, error = tonumber, type, print, error
@@ -102,7 +102,7 @@ local function equalcap (s, i, c)
102102
end
103103

104104

105-
local S = (Predef.space + "--" * (any - Predef.nl)^0)^0
105+
local S = (m.S(" \f\n\r\t\v") + "--" * (any - Predef.nl)^0)^0
106106

107107
local name = m.R("AZ", "az") * m.R("AZ", "az", "__", "09")^0
108108

test.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env lua5.1
22

3-
-- $Id: test.lua,v 1.81 2010/11/03 17:07:50 roberto Exp $
3+
-- $Id: test.lua,v 1.82 2010/12/03 14:49:54 roberto Exp $
44

55
require"strict" -- just to be pedantic
66

@@ -348,6 +348,9 @@ checkerr("rule <a table> is not defined", m.match, { m.V({}) }, "")
348348
print('+')
349349

350350

351+
-- bug in 0.10 (rechecking a grammar, after tail-call optimization)
352+
m.P{ m.P { (m.P(3) + "xuxu")^0 * m.V"xuxu", xuxu = m.P(1) } }
353+
351354
local V = m.V
352355

353356
local Space = m.S(" \n\t")^0

0 commit comments

Comments
 (0)