diff options
Diffstat (limited to 'examples/listIdCatch.lua')
-rw-r--r-- | examples/listIdCatch.lua | 48 |
1 files changed, 34 insertions, 14 deletions
diff --git a/examples/listIdCatch.lua b/examples/listIdCatch.lua index 38ad2e5..3cbc834 100644 --- a/examples/listIdCatch.lua +++ b/examples/listIdCatch.lua | |||
@@ -1,25 +1,45 @@ | |||
1 | local m = require'lpeglabel' | 1 | local m = require'lpeglabel' |
2 | 2 | ||
3 | local errUndef, errId, errComma = 0, 1, 2 | 3 | local terror = {} |
4 | 4 | ||
5 | local terror = { | 5 | local function newError(s) |
6 | [errUndef] = "Error", | 6 | table.insert(terror, s) |
7 | [errId] = "Error: expecting an identifier", | 7 | return #terror |
8 | [errComma] = "Error: expecting ','", | 8 | end |
9 | } | 9 | |
10 | local errUndef = newError("undefined") | ||
11 | local errId = newError("expecting an identifier") | ||
12 | local errComma = newError("expecting ','") | ||
13 | |||
14 | local function calcline (s, i) | ||
15 | if i == 1 then return 1, 1 end | ||
16 | local rest, line = s:sub(1,i):gsub("[^\n]*\n", "") | ||
17 | local col = #rest | ||
18 | return 1 + line, col ~= 0 and col or 1 | ||
19 | end | ||
10 | 20 | ||
11 | g = m.P{ | 21 | local g = m.P{ |
12 | "S", | 22 | "S", |
13 | S = m.Lc(m.Lc(m.V"Id" * m.V"List", m.V"ErrId", errId), | 23 | S = m.Lc(m.Lc(m.V"Id" * m.V"List", m.V"ErrId", errId), |
14 | m.V"ErrComma", errComma), | 24 | m.V"ErrComma", errComma), |
15 | List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", | 25 | List = -m.P(1) + (m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)) * m.V"List", |
16 | Id = m.R'az'^1 + m.T(errId), | 26 | Id = m.V"Sp" * m.R'az'^1, |
17 | Comma = "," + m.T(errComma), | 27 | Comma = m.V"Sp" * ",", |
28 | Sp = m.S" \n\t"^0, | ||
18 | ErrId = m.Cc(errId) / terror, | 29 | ErrId = m.Cc(errId) / terror, |
19 | ErrComma = m.Cc(errComma) / terror | 30 | ErrComma = m.Cc(errComma) / terror |
20 | } | 31 | } |
21 | 32 | ||
22 | print(g:match("a,b")) | 33 | function mymatch (g, s) |
23 | print(g:match("a b")) | 34 | local r, e, sfail = g:match(s) |
24 | print(g:match(",b")) | 35 | if not r then |
36 | local line, col = calcline(s, #s - #sfail) | ||
37 | local msg = "Error at line " .. line .. " (col " .. col .. "): " | ||
38 | return r, msg .. terror[e] .. " before '" .. sfail .. "'" | ||
39 | end | ||
40 | return r | ||
41 | end | ||
25 | 42 | ||
43 | print(mymatch(g, "one,two")) | ||
44 | print(mymatch(g, "one two")) | ||
45 | print(mymatch(g, "one,\n two,\nthree,")) | ||