aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/listId1.lua11
-rw-r--r--examples/listId2.lua32
-rw-r--r--examples/listIdCatch.lua35
-rw-r--r--examples/listIdRe1.lua35
-rw-r--r--examples/listIdRe2.lua30
5 files changed, 49 insertions, 94 deletions
diff --git a/examples/listId1.lua b/examples/listId1.lua
index 12c0678..8976f5f 100644
--- a/examples/listId1.lua
+++ b/examples/listId1.lua
@@ -1,11 +1,5 @@
1local m = require'lpeglabel' 1local m = require'lpeglabel'
2 2local re = require'relabel'
3local function calcline (s, i)
4 if i == 1 then return 1, 1 end
5 local rest, line = s:sub(1,i):gsub("[^\n]*\n", "")
6 local col = #rest
7 return 1 + line, col ~= 0 and col or 1
8end
9 3
10local g = m.P{ 4local g = m.P{
11 "S", 5 "S",
@@ -19,7 +13,7 @@ local g = m.P{
19function mymatch (g, s) 13function mymatch (g, s)
20 local r, e, sfail = g:match(s) 14 local r, e, sfail = g:match(s)
21 if not r then 15 if not r then
22 local line, col = calcline(s, #s - #sfail) 16 local line, col = re.calcline(s, #s - #sfail)
23 local msg = "Error at line " .. line .. " (col " .. col .. ")" 17 local msg = "Error at line " .. line .. " (col " .. col .. ")"
24 if e == 1 then 18 if e == 1 then
25 return r, msg .. ": expecting an identifier before '" .. sfail .. "'" 19 return r, msg .. ": expecting an identifier before '" .. sfail .. "'"
@@ -35,4 +29,3 @@ end
35print(mymatch(g, "one,two")) 29print(mymatch(g, "one,two"))
36print(mymatch(g, "one two")) 30print(mymatch(g, "one two"))
37print(mymatch(g, "one,\n two,\nthree,")) 31print(mymatch(g, "one,\n two,\nthree,"))
38
diff --git a/examples/listId2.lua b/examples/listId2.lua
index 48157f1..509fda4 100644
--- a/examples/listId2.lua
+++ b/examples/listId2.lua
@@ -1,42 +1,36 @@
1local m = require'lpeglabel' 1local m = require'lpeglabel'
2local re = require'relabel'
2 3
3local terror = {} 4local terror = {}
4 5
5local function newError(s) 6local function newError(s)
6 table.insert(terror, s) 7 table.insert(terror, s)
7 return #terror 8 return #terror
8end 9end
9 10
10local errUndef = newError("undefined") 11local errUndef = newError("undefined")
11local errId = newError("expecting an identifier") 12local errId = newError("expecting an identifier")
12local errComma = newError("expecting ','") 13local errComma = newError("expecting ','")
13 14
14local 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
19end
20
21local g = m.P{ 15local g = m.P{
22 "S", 16 "S",
23 S = m.V"Id" * m.V"List", 17 S = m.V"Id" * m.V"List",
24 List = -m.P(1) + (m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)) * m.V"List", 18 List = -m.P(1) + (m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)) * m.V"List",
25 Id = m.V"Sp" * m.R'az'^1, 19 Id = m.V"Sp" * m.R'az'^1,
26 Comma = m.V"Sp" * ",", 20 Comma = m.V"Sp" * ",",
27 Sp = m.S" \n\t"^0, 21 Sp = m.S" \n\t"^0,
28} 22}
29 23
30function mymatch (g, s) 24function mymatch (g, s)
31 local r, e, sfail = g:match(s) 25 local r, e, sfail = g:match(s)
32 if not r then 26 if not r then
33 local line, col = calcline(s, #s - #sfail) 27 local line, col = re.calcline(s, #s - #sfail)
34 local msg = "Error at line " .. line .. " (col " .. col .. "): " 28 local msg = "Error at line " .. line .. " (col " .. col .. "): "
35 return r, msg .. terror[e] .. " before '" .. sfail .. "'" 29 return r, msg .. terror[e] .. " before '" .. sfail .. "'"
36 end 30 end
37 return r 31 return r
38end 32end
39 33
40print(mymatch(g, "one,two")) 34print(mymatch(g, "one,two"))
41print(mymatch(g, "one two")) 35print(mymatch(g, "one two"))
42print(mymatch(g, "one,\n two,\nthree,")) 36print(mymatch(g, "one,\n two,\nthree,"))
diff --git a/examples/listIdCatch.lua b/examples/listIdCatch.lua
index 3cbc834..5ad6f2d 100644
--- a/examples/listIdCatch.lua
+++ b/examples/listIdCatch.lua
@@ -3,43 +3,26 @@ local m = require'lpeglabel'
3local terror = {} 3local terror = {}
4 4
5local function newError(s) 5local function newError(s)
6 table.insert(terror, s) 6 table.insert(terror, s)
7 return #terror 7 return #terror
8end 8end
9 9
10local errUndef = newError("undefined") 10local errUndef = newError("undefined")
11local errId = newError("expecting an identifier") 11local errId = newError("expecting an identifier")
12local errComma = newError("expecting ','") 12local errComma = newError("expecting ','")
13 13
14local 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
19end
20
21local g = m.P{ 14local g = m.P{
22 "S", 15 "S",
23 S = m.Lc(m.Lc(m.V"Id" * m.V"List", m.V"ErrId", errId), 16 S = m.Lc(m.Lc(m.V"Id" * m.V"List", m.V"ErrId", errId),
24 m.V"ErrComma", errComma), 17 m.V"ErrComma", errComma),
25 List = -m.P(1) + (m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)) * m.V"List", 18 List = -m.P(1) + (m.V"Comma" + m.T(errComma)) * (m.V"Id" + m.T(errId)) * m.V"List",
26 Id = m.V"Sp" * m.R'az'^1, 19 Id = m.V"Sp" * m.R'az'^1,
27 Comma = m.V"Sp" * ",", 20 Comma = m.V"Sp" * ",",
28 Sp = m.S" \n\t"^0, 21 Sp = m.S" \n\t"^0,
29 ErrId = m.Cc(errId) / terror, 22 ErrId = m.Cc(errId) / terror,
30 ErrComma = m.Cc(errComma) / terror 23 ErrComma = m.Cc(errComma) / terror
31} 24}
32 25
33function mymatch (g, s) 26print(m.match(g, "one,two"))
34 local r, e, sfail = g:match(s) 27print(m.match(g, "one two"))
35 if not r then 28print(m.match(g, "one,\n two,\nthree,"))
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
41end
42
43print(mymatch(g, "one,two"))
44print(mymatch(g, "one two"))
45print(mymatch(g, "one,\n two,\nthree,"))
diff --git a/examples/listIdRe1.lua b/examples/listIdRe1.lua
index 361e53f..d092566 100644
--- a/examples/listIdRe1.lua
+++ b/examples/listIdRe1.lua
@@ -1,34 +1,27 @@
1local re = require 'relabel' 1local re = require 'relabel'
2 2
3local function calcline (s, i)
4 if i == 1 then return 1, 1 end
5 local rest, line = s:sub(1,i):gsub("[^\n]*\n", "")
6 local col = #rest
7 return 1 + line, col ~= 0 and col or 1
8end
9
10local g = re.compile[[ 3local g = re.compile[[
11 S <- Id List 4 S <- Id List
12 List <- !. / (',' / %{2}) (Id / %{1}) List 5 List <- !. / (',' / %{2}) (Id / %{1}) List
13 Id <- Sp [a-z]+ 6 Id <- Sp [a-z]+
14 Comma <- Sp ',' 7 Comma <- Sp ','
15 Sp <- %s* 8 Sp <- %s*
16]] 9]]
17 10
18function mymatch (g, s) 11function mymatch (g, s)
19 local r, e, sfail = g:match(s) 12 local r, e, sfail = g:match(s)
20 if not r then 13 if not r then
21 local line, col = calcline(s, #s - #sfail) 14 local line, col = re.calcline(s, #s - #sfail)
22 local msg = "Error at line " .. line .. " (col " .. col .. ")" 15 local msg = "Error at line " .. line .. " (col " .. col .. ")"
23 if e == 1 then 16 if e == 1 then
24 return r, msg .. ": expecting an identifier before '" .. sfail .. "'" 17 return r, msg .. ": expecting an identifier before '" .. sfail .. "'"
25 elseif e == 2 then 18 elseif e == 2 then
26 return r, msg .. ": expecting ',' before '" .. sfail .. "'" 19 return r, msg .. ": expecting ',' before '" .. sfail .. "'"
27 else 20 else
28 return r, msg 21 return r, msg
29 end 22 end
30 end 23 end
31 return r 24 return r
32end 25end
33 26
34print(mymatch(g, "one,two")) 27print(mymatch(g, "one,two"))
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua
index ccb7ce5..fe30535 100644
--- a/examples/listIdRe2.lua
+++ b/examples/listIdRe2.lua
@@ -1,9 +1,9 @@
1local re = require 'relabel' 1local re = require 'relabel'
2 2
3local errinfo = { 3local errinfo = {
4 {"errUndef", "undefined"}, 4 {"errUndef", "undefined"},
5 {"errId", "expecting an identifier"}, 5 {"errId", "expecting an identifier"},
6 {"errComma", "expecting ','"}, 6 {"errComma", "expecting ','"},
7} 7}
8 8
9local errmsgs = {} 9local errmsgs = {}
@@ -16,30 +16,22 @@ end
16 16
17re.setlabels(labels) 17re.setlabels(labels)
18 18
19local function calcline (s, i)
20 if i == 1 then return 1, 1 end
21 local rest, line = s:sub(1,i):gsub("[^\n]*\n", "")
22 local col = #rest
23 return 1 + line, col ~= 0 and col or 1
24end
25
26
27local g = re.compile[[ 19local g = re.compile[[
28 S <- Id List 20 S <- Id List
29 List <- !. / (',' / %{errComma}) (Id / %{errId}) List 21 List <- !. / (',' / %{errComma}) (Id / %{errId}) List
30 Id <- Sp [a-z]+ 22 Id <- Sp [a-z]+
31 Comma <- Sp ',' 23 Comma <- Sp ','
32 Sp <- %s* 24 Sp <- %s*
33]] 25]]
34 26
35function mymatch (g, s) 27function mymatch (g, s)
36 local r, e, sfail = g:match(s) 28 local r, e, sfail = g:match(s)
37 if not r then 29 if not r then
38 local line, col = calcline(s, #s - #sfail) 30 local line, col = re.calcline(s, #s - #sfail)
39 local msg = "Error at line " .. line .. " (col " .. col .. "): " 31 local msg = "Error at line " .. line .. " (col " .. col .. "): "
40 return r, msg .. errmsgs[e] .. " before '" .. sfail .. "'" 32 return r, msg .. errmsgs[e] .. " before '" .. sfail .. "'"
41 end 33 end
42 return r 34 return r
43end 35end
44 36
45print(mymatch(g, "one,two")) 37print(mymatch(g, "one,two"))