diff options
Diffstat (limited to 'examples/listId1.lua')
-rw-r--r-- | examples/listId1.lua | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/examples/listId1.lua b/examples/listId1.lua new file mode 100644 index 0000000..e075bd1 --- /dev/null +++ b/examples/listId1.lua | |||
@@ -0,0 +1,27 @@ | |||
1 | local m = require'lpeglabel' | ||
2 | |||
3 | local g = m.P{ | ||
4 | "S", | ||
5 | S = m.V"Id" * m.V"List", | ||
6 | List = -m.P(1) + ("," + m.T(2)) * m.V"Id" * m.V"List", | ||
7 | Id = m.R'az'^1 + m.T(1), | ||
8 | } | ||
9 | |||
10 | function mymatch (g, s) | ||
11 | local r, e = g:match(s) | ||
12 | if not r then | ||
13 | if e == 1 then | ||
14 | return r, "Error: expecting an identifier" | ||
15 | elseif e == 2 then | ||
16 | return r, "Error: expecting ','" | ||
17 | else | ||
18 | return r, "Error" | ||
19 | end | ||
20 | end | ||
21 | return r | ||
22 | end | ||
23 | |||
24 | print(mymatch(g, "a,b")) | ||
25 | print(mymatch(g, "a b")) | ||
26 | print(mymatch(g, ", b")) | ||
27 | |||