diff options
-rw-r--r-- | lpeg.html | 30 | ||||
-rw-r--r-- | lptree.c | 2 | ||||
-rw-r--r-- | lptypes.h | 1 | ||||
-rw-r--r-- | makefile | 1 | ||||
-rwxr-xr-x | test.lua | 2 |
5 files changed, 25 insertions, 11 deletions
@@ -53,19 +53,22 @@ | |||
53 | <h2><a name="intro">Introduction</a></h2> | 53 | <h2><a name="intro">Introduction</a></h2> |
54 | 54 | ||
55 | <p> | 55 | <p> |
56 | <em>LPeg</em> is a new pattern-matching library for Lua, | 56 | <em>LPeg</em> is a pattern-matching library for Lua, |
57 | based on | 57 | based on |
58 | <a href="//bford.info/packrat/"> | 58 | <a href="//bford.info/packrat/"> |
59 | Parsing Expression Grammars</a> (PEGs). | 59 | Parsing Expression Grammars</a> (PEGs). |
60 | This text is a reference manual for the library. | 60 | This text is a reference manual for the library. |
61 | For those starting with LPeg, | ||
62 | <a href="https://www.inf.puc-rio.br/~roberto/docs/lpeg-primer.pdf"> | ||
63 | Mastering LPeg</a> presents a good tutorial. | ||
61 | For a more formal treatment of LPeg, | 64 | For a more formal treatment of LPeg, |
62 | as well as some discussion about its implementation, | 65 | as well as some discussion about its implementation, |
63 | see | 66 | see |
64 | <a href="//www.inf.puc-rio.br/~roberto/docs/peg.pdf"> | 67 | <a href="//www.inf.puc-rio.br/~roberto/docs/peg.pdf"> |
65 | A Text Pattern-Matching Tool based on Parsing Expression Grammars</a>. | 68 | A Text Pattern-Matching Tool based on Parsing Expression Grammars</a>. |
66 | (You may also be interested in my | 69 | You may also be interested in my |
67 | <a href="//vimeo.com/1485123">talk about LPeg</a> | 70 | <a href="//vimeo.com/1485123">talk about LPeg</a> |
68 | given at the III Lua Workshop.) | 71 | given at the III Lua Workshop. |
69 | </p> | 72 | </p> |
70 | 73 | ||
71 | <p> | 74 | <p> |
@@ -664,13 +667,22 @@ produces values for each match. | |||
664 | 667 | ||
665 | <p> | 668 | <p> |
666 | Usually, | 669 | Usually, |
667 | LPeg does not specify when (and if) it evaluates its captures. | 670 | LPeg does not specify when, if, or how many times it evaluates its captures. |
668 | (As an example, | ||
669 | consider the pattern <code>lpeg.P"a" / func / 0</code>. | ||
670 | Because the "division" by 0 instructs LPeg to throw away the | ||
671 | results from the pattern, | ||
672 | it is not specified whether LPeg will call <code>func</code>.) | ||
673 | Therefore, captures should avoid side effects. | 671 | Therefore, captures should avoid side effects. |
672 | As an example, | ||
673 | LPeg may or may not call <code>func</code> in the pattern | ||
674 | <code>lpeg.P"a" / func / 0</code>, | ||
675 | given that the <a href="#cap-num">"division" by 0</a> | ||
676 | instructs LPeg to throw away the | ||
677 | results from the pattern. | ||
678 | Similarly, a capture nested inside a <a href="#cap-g">named group</a> | ||
679 | may be evaluated only when that group is referred in a | ||
680 | <a href="#cap-b">back capture</a>; | ||
681 | if there are multiple back captures, | ||
682 | the group may be evaluated multiple times. | ||
683 | </p> | ||
684 | |||
685 | <p> | ||
674 | Moreover, | 686 | Moreover, |
675 | captures cannot affect the way a pattern matches a subject. | 687 | captures cannot affect the way a pattern matches a subject. |
676 | The only exception to this rule is the | 688 | The only exception to this rule is the |
@@ -1123,7 +1123,7 @@ static int verifyrule (lua_State *L, TTree *tree, unsigned short *passed, | |||
1123 | return verifyerror(L, passed, npassed); /* error */ | 1123 | return verifyerror(L, passed, npassed); /* error */ |
1124 | else { | 1124 | else { |
1125 | passed[npassed++] = tree->key; /* add rule to path */ | 1125 | passed[npassed++] = tree->key; /* add rule to path */ |
1126 | /* return verifyrule(L, sib1(tree), passed, npassed); */ | 1126 | /* return verifyrule(L, sib1(tree), passed, npassed, nb); */ |
1127 | tree = sib1(tree); goto tailcall; | 1127 | tree = sib1(tree); goto tailcall; |
1128 | } | 1128 | } |
1129 | case TGrammar: | 1129 | case TGrammar: |
@@ -34,6 +34,7 @@ | |||
34 | 34 | ||
35 | #define lua_rawlen lua_objlen | 35 | #define lua_rawlen lua_objlen |
36 | 36 | ||
37 | #undef luaL_newlib | ||
37 | #define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f) | 38 | #define luaL_setfuncs(L,f,n) luaL_register(L,NULL,f) |
38 | #define luaL_newlib(L,f) luaL_register(L,"lpeg",f) | 39 | #define luaL_newlib(L,f) luaL_register(L,"lpeg",f) |
39 | 40 | ||
@@ -1,5 +1,6 @@ | |||
1 | LIBNAME = lpeg | 1 | LIBNAME = lpeg |
2 | LUADIR = ./lua/ | 2 | LUADIR = ./lua/ |
3 | #LUADIR = /home/roberto/prj/lua/ | ||
3 | 4 | ||
4 | COPT = -O2 -DNDEBUG | 5 | COPT = -O2 -DNDEBUG |
5 | # COPT = -O0 -DLPEG_DEBUG -g | 6 | # COPT = -O0 -DLPEG_DEBUG -g |
@@ -47,7 +47,7 @@ end | |||
47 | print"General tests for LPeg library" | 47 | print"General tests for LPeg library" |
48 | 48 | ||
49 | assert(type(m.version) == "string") | 49 | assert(type(m.version) == "string") |
50 | print(m.version) | 50 | print(m.version, _VERSION) |
51 | assert(m.type("alo") ~= "pattern") | 51 | assert(m.type("alo") ~= "pattern") |
52 | assert(m.type(io.input) ~= "pattern") | 52 | assert(m.type(io.input) ~= "pattern") |
53 | assert(m.type(m.P"alo") == "pattern") | 53 | assert(m.type(m.P"alo") == "pattern") |