diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/listId1.lua | 12 | ||||
| -rw-r--r-- | examples/listId2.lua | 13 | ||||
| -rw-r--r-- | examples/listId2Rec2.lua | 67 |
3 files changed, 82 insertions, 10 deletions
diff --git a/examples/listId1.lua b/examples/listId1.lua index 8976f5f..dee46e9 100644 --- a/examples/listId1.lua +++ b/examples/listId1.lua | |||
| @@ -1,12 +1,14 @@ | |||
| 1 | local m = require'lpeglabel' | 1 | local m = require'lpeglabelrec' |
| 2 | local re = require'relabel' | 2 | local re = require'relabelrec' |
| 3 | |||
| 4 | local id = m.R'az'^1 | ||
| 3 | 5 | ||
| 4 | local g = m.P{ | 6 | local g = m.P{ |
| 5 | "S", | 7 | "S", |
| 6 | S = m.V"Id" * m.V"List", | 8 | S = m.V"Id" * m.V"List", |
| 7 | List = -m.P(1) + (m.V"Comma" + m.T(2)) * (m.V"Id" + m.T(1)) * m.V"List", | 9 | List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", |
| 8 | Id = m.V"Sp" * m.R'az'^1, | 10 | Id = m.V"Sp" * id + m.T(1), |
| 9 | Comma = m.V"Sp" * ",", | 11 | Comma = m.V"Sp" * "," + m.T(2), |
| 10 | Sp = m.S" \n\t"^0, | 12 | Sp = m.S" \n\t"^0, |
| 11 | } | 13 | } |
| 12 | 14 | ||
diff --git a/examples/listId2.lua b/examples/listId2.lua index 509fda4..46f0063 100644 --- a/examples/listId2.lua +++ b/examples/listId2.lua | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | local m = require'lpeglabel' | 1 | local m = require'lpeglabelrec' |
| 2 | local re = require'relabel' | 2 | local re = require'relabelrec' |
| 3 | 3 | ||
| 4 | local terror = {} | 4 | local terror = {} |
| 5 | 5 | ||
| @@ -12,15 +12,18 @@ local errUndef = newError("undefined") | |||
| 12 | local errId = newError("expecting an identifier") | 12 | local errId = newError("expecting an identifier") |
| 13 | local errComma = newError("expecting ','") | 13 | local errComma = newError("expecting ','") |
| 14 | 14 | ||
| 15 | local id = m.R'az'^1 | ||
| 16 | |||
| 15 | local g = m.P{ | 17 | local g = m.P{ |
| 16 | "S", | 18 | "S", |
| 17 | S = m.V"Id" * m.V"List", | 19 | S = m.V"Id" * m.V"List", |
| 18 | List = -m.P(1) + (m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)) * m.V"List", | 20 | List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", |
| 19 | Id = m.V"Sp" * m.R'az'^1, | 21 | Id = m.V"Sp" * id + m.T(errId), |
| 20 | Comma = m.V"Sp" * ",", | 22 | Comma = m.V"Sp" * "," + m.T(errComma), |
| 21 | Sp = m.S" \n\t"^0, | 23 | Sp = m.S" \n\t"^0, |
| 22 | } | 24 | } |
| 23 | 25 | ||
| 26 | |||
| 24 | function mymatch (g, s) | 27 | function mymatch (g, s) |
| 25 | local r, e, sfail = g:match(s) | 28 | local r, e, sfail = g:match(s) |
| 26 | if not r then | 29 | if not r then |
diff --git a/examples/listId2Rec2.lua b/examples/listId2Rec2.lua new file mode 100644 index 0000000..a347a5b --- /dev/null +++ b/examples/listId2Rec2.lua | |||
| @@ -0,0 +1,67 @@ | |||
| 1 | local m = require'lpeglabelrec' | ||
| 2 | local re = require'relabelrec' | ||
| 3 | |||
| 4 | local terror = {} | ||
| 5 | |||
| 6 | local function newError(s) | ||
| 7 | table.insert(terror, s) | ||
| 8 | return #terror | ||
| 9 | end | ||
| 10 | |||
| 11 | local errUndef = newError("undefined") | ||
| 12 | local errId = newError("expecting an identifier") | ||
| 13 | local errComma = newError("expecting ','") | ||
| 14 | |||
| 15 | local id = m.R'az'^1 | ||
| 16 | |||
| 17 | local g = m.P{ | ||
| 18 | "S", | ||
| 19 | S = m.V"Id" * m.V"List", | ||
| 20 | List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List", | ||
| 21 | Id = m.V"Sp" * id + m.T(errId), | ||
| 22 | Comma = m.V"Sp" * "," + m.T(errComma), | ||
| 23 | Sp = m.S" \n\t"^0, | ||
| 24 | } | ||
| 25 | |||
| 26 | local subject, errors | ||
| 27 | |||
| 28 | function recorderror(pos, lab) | ||
| 29 | local line, col = re.calcline(subject, pos) | ||
| 30 | table.insert(errors, { line = line, col = col, msg = terror[lab] }) | ||
| 31 | end | ||
| 32 | |||
| 33 | function record (lab) | ||
| 34 | return (m.Cp() * m.Cc(lab)) / recorderror | ||
| 35 | end | ||
| 36 | |||
| 37 | function sync (p) | ||
| 38 | return (-p * m.P(1))^0 | ||
| 39 | end | ||
| 40 | |||
| 41 | local grec = m.P{ | ||
| 42 | "S", | ||
| 43 | S = m.Rec(m.Rec(g, m.V"ErrComma", errComma), m.V"ErrId", errId), | ||
| 44 | ErrComma = record(errComma) * sync(-m.P(1) + id), | ||
| 45 | ErrId = record(errId) * sync(-m.P(1) + ",") | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | function mymatch (g, s) | ||
| 50 | errors = {} | ||
| 51 | subject = s | ||
| 52 | local r, e, sfail = g:match(s) | ||
| 53 | if #errors > 0 then | ||
| 54 | local out = {} | ||
| 55 | for i, err in ipairs(errors) do | ||
| 56 | local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg | ||
| 57 | table.insert(out, msg) | ||
| 58 | end | ||
| 59 | return nil, table.concat(out, "\n") | ||
| 60 | end | ||
| 61 | return r | ||
| 62 | end | ||
| 63 | |||
| 64 | print(mymatch(grec, "one,two")) | ||
| 65 | print(mymatch(grec, "one two three")) | ||
| 66 | print(mymatch(grec, "1,\n two, \n3,")) | ||
| 67 | print(mymatch(grec, "one\n two123, \nthree,")) | ||
