diff options
author | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-28 14:29:49 -0300 |
---|---|---|
committer | Sérgio Queiroz <sqmedeiros@gmail.com> | 2017-12-28 14:29:49 -0300 |
commit | 59da25ff241a83d8139e41199ef7a23f6e17fa65 (patch) | |
tree | a9ad13fa0f8ddb50855b716b349e4ded5d5fac8d /examples/listIdRe2.lua | |
parent | 772df00e061db3cd7d0af92c8ab65bc023d8121d (diff) | |
download | lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.tar.gz lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.tar.bz2 lpeglabel-59da25ff241a83d8139e41199ef7a23f6e17fa65.zip |
Updating examples to the new semantics
Diffstat (limited to 'examples/listIdRe2.lua')
-rw-r--r-- | examples/listIdRe2.lua | 53 |
1 files changed, 20 insertions, 33 deletions
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua index 58ddedd..0ea7352 100644 --- a/examples/listIdRe2.lua +++ b/examples/listIdRe2.lua | |||
@@ -1,46 +1,33 @@ | |||
1 | local re = require 'relabel' | 1 | local re = require 'relabel' |
2 | 2 | ||
3 | local errinfo = { | 3 | local errinfo = { |
4 | {"errUndef", "undefined"}, | 4 | ErrId = "expecting an identifier", |
5 | {"errId", "expecting an identifier"}, | 5 | ErrComma = "expecting ','", |
6 | {"errComma", "expecting ','"}, | 6 | fail = "undefined", |
7 | } | 7 | } |
8 | 8 | ||
9 | local errmsgs = {} | 9 | local function sync (p) |
10 | local labels = {} | 10 | return '( !(' .. p .. ') .)*' |
11 | |||
12 | for i, err in ipairs(errinfo) do | ||
13 | errmsgs[i] = err[2] | ||
14 | labels[err[1]] = i | ||
15 | end | 11 | end |
16 | 12 | ||
17 | re.setlabels(labels) | ||
18 | |||
19 | local g = re.compile[[ | ||
20 | S <- Id List | ||
21 | List <- !. / Comma Id List | ||
22 | Id <- Sp {[a-z]+} / %{errId} | ||
23 | Comma <- Sp ',' / %{errComma} | ||
24 | Sp <- %s* | ||
25 | ]] | ||
26 | |||
27 | local errors | 13 | local errors |
28 | 14 | ||
29 | function recorderror (subject, pos, label) | 15 | local function recorderror (subject, pos, label) |
30 | local line, col = re.calcline(subject, pos) | 16 | local line, col = re.calcline(subject, pos) |
31 | table.insert(errors, { line = line, col = col, msg = errmsgs[labels[label]] }) | 17 | table.insert(errors, { line = line, col = col, msg = errinfo[label] }) |
32 | return true | 18 | return true |
33 | end | 19 | end |
34 | 20 | ||
35 | function sync (p) | 21 | local g = re.compile([[ |
36 | return '( !(' .. p .. ') .)*' | 22 | S <- Id List |
37 | end | 23 | List <- !. / Comma Id List |
24 | Id <- (Sp {[a-z]+})^ErrId | ||
25 | Comma <- (Sp ',')^ErrComma | ||
26 | Sp <- %s*]] .. | ||
27 | "ErrId <- ('' -> 'ErrId' => recorderror) " .. sync('","') .. "-> default\n" .. | ||
28 | "ErrComma <- ('' -> 'ErrComma' => recorderror) " .. sync('[a-z]+'), | ||
29 | {default = "NONE", recorderror = recorderror}) | ||
38 | 30 | ||
39 | local grec = re.compile( | ||
40 | "S <- %g //{errComma} ErrComma //{errId} ErrId" .. "\n" .. | ||
41 | "ErrComma <- ('' -> 'errComma' => recorderror) " .. sync('[a-z]+') .. "\n" .. | ||
42 | "ErrId <- ('' -> 'errId' => recorderror) " .. sync('","') .. "-> default" | ||
43 | , {g = g, recorderror = recorderror, default = "NONE"}) | ||
44 | 31 | ||
45 | function mymatch (g, s) | 32 | function mymatch (g, s) |
46 | errors = {} | 33 | errors = {} |
@@ -65,7 +52,7 @@ function mymatch (g, s) | |||
65 | return r | 52 | return r |
66 | end | 53 | end |
67 | 54 | ||
68 | mymatch(grec, "one,two") | 55 | mymatch(g, "one,two") |
69 | mymatch(grec, "one two three") | 56 | mymatch(g, "one two three") |
70 | mymatch(grec, "1,\n two, \n3,") | 57 | mymatch(g, "1,\n two, \n3,") |
71 | mymatch(grec, "one\n two123, \nthree,") | 58 | mymatch(g, "one\n two123, \nthree,") |