aboutsummaryrefslogtreecommitdiff
path: root/examples/listIdRe2.lua
diff options
context:
space:
mode:
authorSérgio Queiroz <sqmedeiros@gmail.com>2017-12-28 14:29:49 -0300
committerSérgio Queiroz <sqmedeiros@gmail.com>2017-12-28 14:29:49 -0300
commit59da25ff241a83d8139e41199ef7a23f6e17fa65 (patch)
treea9ad13fa0f8ddb50855b716b349e4ded5d5fac8d /examples/listIdRe2.lua
parent772df00e061db3cd7d0af92c8ab65bc023d8121d (diff)
downloadlpeglabel-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.lua53
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 @@
1local re = require 'relabel' 1local re = require 'relabel'
2 2
3local errinfo = { 3local 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
9local errmsgs = {} 9local function sync (p)
10local labels = {} 10 return '( !(' .. p .. ') .)*'
11
12for i, err in ipairs(errinfo) do
13 errmsgs[i] = err[2]
14 labels[err[1]] = i
15end 11end
16 12
17re.setlabels(labels)
18
19local 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
27local errors 13local errors
28 14
29function recorderror (subject, pos, label) 15local 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
33end 19end
34 20
35function sync (p) 21local g = re.compile([[
36 return '( !(' .. p .. ') .)*' 22 S <- Id List
37end 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
39local 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
45function mymatch (g, s) 32function mymatch (g, s)
46 errors = {} 33 errors = {}
@@ -65,7 +52,7 @@ function mymatch (g, s)
65 return r 52 return r
66end 53end
67 54
68mymatch(grec, "one,two") 55mymatch(g, "one,two")
69mymatch(grec, "one two three") 56mymatch(g, "one two three")
70mymatch(grec, "1,\n two, \n3,") 57mymatch(g, "1,\n two, \n3,")
71mymatch(grec, "one\n two123, \nthree,") 58mymatch(g, "one\n two123, \nthree,")