diff options
Diffstat (limited to 'examples/listId3.lua')
-rw-r--r-- | examples/listId3.lua | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/examples/listId3.lua b/examples/listId3.lua new file mode 100644 index 0000000..03da97d --- /dev/null +++ b/examples/listId3.lua | |||
@@ -0,0 +1,35 @@ | |||
1 | local m = require'lpeglabel' | ||
2 | local re = require'relabel' | ||
3 | |||
4 | local terror = { | ||
5 | ErrId = "expecting an identifier", | ||
6 | ErrComma = "expecting ','", | ||
7 | fail = "undefined" | ||
8 | } | ||
9 | |||
10 | local id = m.R'az'^1 | ||
11 | |||
12 | local g = m.P{ | ||
13 | 'S', | ||
14 | S = m.V'List', | ||
15 | List = m.V'Id' * (#m.P(1) * m.V'Comma' * (m.V'Id' + m.T'ErrId'))^0, | ||
16 | Id = m.V'Sp' * id, | ||
17 | Comma = m.V'Sp' * ',' + m.T'ErrComma', | ||
18 | Sp = m.S' \n\t'^0, | ||
19 | } | ||
20 | |||
21 | |||
22 | function mymatch (g, s) | ||
23 | local r, e, pos = g:match(s) | ||
24 | if not r then | ||
25 | local line, col = re.calcline(s, pos) | ||
26 | local msg = "Error at line " .. line .. " (col " .. col .. "): " | ||
27 | return r, msg .. terror[e] .. " before '" .. s:sub(pos) .. "'" | ||
28 | end | ||
29 | return r | ||
30 | end | ||
31 | |||
32 | print(mymatch(g, "one,two")) | ||
33 | print(mymatch(g, "one two")) | ||
34 | print(mymatch(g, "one,\n two,\nthree,4")) | ||
35 | print(mymatch(g, " 1,2")) | ||