diff options
author | Sergio Medeiros <sqmedeiros@gmail.com> | 2015-03-23 15:09:08 -0300 |
---|---|---|
committer | Sergio Medeiros <sqmedeiros@gmail.com> | 2015-03-23 15:09:08 -0300 |
commit | f124b2d4449a13ac21af9581a44b4455d89eaddb (patch) | |
tree | e0486555ac811c5f712927f110bb7bcda8569adf /examples/listIdRe2.lua | |
parent | 0e93d536ba2d312502737cce2ab0cc21393c4842 (diff) | |
download | lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.tar.gz lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.tar.bz2 lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.zip |
Renaming "re.lua" to "relabel.lua".
Adding examples, README and lpeglabel.html
Diffstat (limited to 'examples/listIdRe2.lua')
-rw-r--r-- | examples/listIdRe2.lua | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua new file mode 100644 index 0000000..67c42f1 --- /dev/null +++ b/examples/listIdRe2.lua | |||
@@ -0,0 +1,35 @@ | |||
1 | local re = require 're' | ||
2 | |||
3 | local errUndef, errId, errComma = 0, 1, 2 | ||
4 | |||
5 | local terror = { | ||
6 | [errUndef] = "Error", | ||
7 | [errId] = "Error: expecting an identifier", | ||
8 | [errComma] = "Error: expecting ','", | ||
9 | } | ||
10 | |||
11 | local tlabels = { ["errUndef"] = errUndef, | ||
12 | ["errId"] = errId, | ||
13 | ["errComma"] = errComma } | ||
14 | |||
15 | re.setlabels(tlabels) | ||
16 | |||
17 | local g = re.compile[[ | ||
18 | S <- Id List | ||
19 | List <- !. / (',' / %{errComma}) Id List | ||
20 | Id <- [a-z] / %{errId} | ||
21 | ]] | ||
22 | |||
23 | function mymatch (g, s) | ||
24 | local r, e = g:match(s) | ||
25 | if not r then | ||
26 | return r, terror[e] | ||
27 | end | ||
28 | return r | ||
29 | end | ||
30 | |||
31 | print(mymatch(g, "a,b")) | ||
32 | print(mymatch(g, "a b")) | ||
33 | print(mymatch(g, ", b")) | ||
34 | |||
35 | |||