aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/expect.lua (renamed from examples/expect.md)7
-rw-r--r--examples/listId1.lua45
-rw-r--r--examples/listId2.lua42
-rw-r--r--examples/listIdCatch.lua48
-rw-r--r--examples/listIdRe1.lua30
-rw-r--r--examples/listIdRe2.lua49
6 files changed, 139 insertions, 82 deletions
diff --git a/examples/expect.md b/examples/expect.lua
index d7d2d3e..2b7e904 100644
--- a/examples/expect.md
+++ b/examples/expect.lua
@@ -1,9 +1,3 @@
1Here's an example of an LPegLabel grammar that make its own function called
2'expect', which takes a pattern and a label as parameters and throws the label
3if the pattern fails to be matched. This function can be extended later on to
4record all errors encountered once error recovery is implemented.
5
6```lua
7local lpeg = require"lpeglabel" 1local lpeg = require"lpeglabel"
8 2
9local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T 3local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T
@@ -84,4 +78,3 @@ print(eval "1+(1-(1*2))/2x")
84 78
85print(eval "-1+(1-(1*2))/2") 79print(eval "-1+(1-(1*2))/2")
86--> syntax error: no expression found (at index 1) 80--> syntax error: no expression found (at index 1)
87```
diff --git a/examples/listId1.lua b/examples/listId1.lua
index 0bf26a1..12c0678 100644
--- a/examples/listId1.lua
+++ b/examples/listId1.lua
@@ -1,27 +1,38 @@
1local m = require'lpeglabel' 1local m = require'lpeglabel'
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
3local g = m.P{ 10local g = m.P{
4 "S", 11 "S",
5 S = m.V"Id" * m.V"List", 12 S = m.V"Id" * m.V"List",
6 List = -m.P(1) + ("," + m.T(2)) * m.V"Id" * m.V"List", 13 List = -m.P(1) + (m.V"Comma" + m.T(2)) * (m.V"Id" + m.T(1)) * m.V"List",
7 Id = m.R'az'^1 + m.T(1), 14 Id = m.V"Sp" * m.R'az'^1,
15 Comma = m.V"Sp" * ",",
16 Sp = m.S" \n\t"^0,
8} 17}
9 18
10function mymatch (g, s) 19function mymatch (g, s)
11 local r, e, sfail = g:match(s) 20 local r, e, sfail = g:match(s)
12 if not r then 21 if not r then
13 if e == 1 then 22 local line, col = calcline(s, #s - #sfail)
14 return r, "Error: expecting an identifier before '" .. sfail .. "'" 23 local msg = "Error at line " .. line .. " (col " .. col .. ")"
15 elseif e == 2 then 24 if e == 1 then
16 return r, "Error: expecting ',' before '" .. sfail .. "'" 25 return r, msg .. ": expecting an identifier before '" .. sfail .. "'"
17 else 26 elseif e == 2 then
18 return r, "Error" 27 return r, msg .. ": expecting ',' before '" .. sfail .. "'"
19 end 28 else
20 end 29 return r, msg
21 return r 30 end
31 end
32 return r
22end 33end
23 34
24print(mymatch(g, "a,b")) 35print(mymatch(g, "one,two"))
25print(mymatch(g, "a b")) 36print(mymatch(g, "one two"))
26print(mymatch(g, ", b")) 37print(mymatch(g, "one,\n two,\nthree,"))
27 38
diff --git a/examples/listId2.lua b/examples/listId2.lua
index 75060f9..48157f1 100644
--- a/examples/listId2.lua
+++ b/examples/listId2.lua
@@ -1,32 +1,42 @@
1local m = require'lpeglabel' 1local m = require'lpeglabel'
2 2
3local errUndef = 0 3local terror = {}
4local errId = 1
5local errComma = 2
6 4
7local terror = { 5local function newError(s)
8 [errUndef] = "Error", 6 table.insert(terror, s)
9 [errId] = "Error: expecting an identifier", 7 return #terror
10 [errComma] = "Error: expecting ','", 8end
11} 9
10local errUndef = newError("undefined")
11local errId = newError("expecting an identifier")
12local errComma = newError("expecting ','")
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
12 20
13local g = m.P{ 21local g = m.P{
14 "S", 22 "S",
15 S = m.V"Id" * m.V"List", 23 S = m.V"Id" * m.V"List",
16 List = -m.P(1) + ("," + m.T(errComma)) * 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",
17 Id = m.R'az'^1 + m.T(errId), 25 Id = m.V"Sp" * m.R'az'^1,
26 Comma = m.V"Sp" * ",",
27 Sp = m.S" \n\t"^0,
18} 28}
19 29
20function mymatch (g, s) 30function mymatch (g, s)
21 local r, e, sfail = g:match(s) 31 local r, e, sfail = g:match(s)
22 if not r then 32 if not r then
23 return r, terror[e] .. " before '" .. sfail .. "'" 33 local line, col = calcline(s, #s - #sfail)
34 local msg = "Error at line " .. line .. " (col " .. col .. "): "
35 return r, msg .. terror[e] .. " before '" .. sfail .. "'"
24 end 36 end
25 return r 37 return r
26end 38end
27 39
28print(mymatch(g, "a,b")) 40print(mymatch(g, "one,two"))
29print(mymatch(g, "a b")) 41print(mymatch(g, "one two"))
30print(mymatch(g, ", b")) 42print(mymatch(g, "one,\n two,\nthree,"))
31
32
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 @@
1local m = require'lpeglabel' 1local m = require'lpeglabel'
2 2
3local errUndef, errId, errComma = 0, 1, 2 3local terror = {}
4 4
5local terror = { 5local function newError(s)
6 [errUndef] = "Error", 6 table.insert(terror, s)
7 [errId] = "Error: expecting an identifier", 7 return #terror
8 [errComma] = "Error: expecting ','", 8end
9} 9
10local errUndef = newError("undefined")
11local errId = newError("expecting an identifier")
12local errComma = newError("expecting ','")
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
10 20
11g = m.P{ 21local 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
22print(g:match("a,b")) 33function mymatch (g, s)
23print(g:match("a b")) 34 local r, e, sfail = g:match(s)
24print(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
41end
25 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 fc213bc..361e53f 100644
--- a/examples/listIdRe1.lua
+++ b/examples/listIdRe1.lua
@@ -1,27 +1,37 @@
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
3local g = re.compile[[ 10local g = re.compile[[
4 S <- Id List 11 S <- Id List
5 List <- !. / (',' / %{2}) Id List 12 List <- !. / (',' / %{2}) (Id / %{1}) List
6 Id <- [a-z] / %{1} 13 Id <- Sp [a-z]+
14 Comma <- Sp ','
15 Sp <- %s*
7]] 16]]
8 17
9function mymatch (g, s) 18function mymatch (g, s)
10 local r, e, sfail = g:match(s) 19 local r, e, sfail = g:match(s)
11 if not r then 20 if not r then
21 local line, col = calcline(s, #s - #sfail)
22 local msg = "Error at line " .. line .. " (col " .. col .. ")"
12 if e == 1 then 23 if e == 1 then
13 return r, "Error: expecting an identifier before '" .. sfail .. "'" 24 return r, msg .. ": expecting an identifier before '" .. sfail .. "'"
14 elseif e == 2 then 25 elseif e == 2 then
15 return r, "Error: expecting ',' before '" .. sfail .. "'" 26 return r, msg .. ": expecting ',' before '" .. sfail .. "'"
16 else 27 else
17 return r, "Error" 28 return r, msg
18 end 29 end
19 end 30 end
20 return r 31 return r
21end 32end
22
23print(mymatch(g, "a,b"))
24print(mymatch(g, "a b"))
25print(mymatch(g, ", b"))
26 33
34print(mymatch(g, "one,two"))
35print(mymatch(g, "one two"))
36print(mymatch(g, "one,\n two,\nthree,"))
27 37
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua
index 4cd140b..ccb7ce5 100644
--- a/examples/listIdRe2.lua
+++ b/examples/listIdRe2.lua
@@ -1,35 +1,48 @@
1local re = require 'relabel' 1local re = require 'relabel'
2 2
3local errUndef, errId, errComma = 0, 1, 2 3local errinfo = {
4 4 {"errUndef", "undefined"},
5local terror = { 5 {"errId", "expecting an identifier"},
6 [errUndef] = "Error", 6 {"errComma", "expecting ','"},
7 [errId] = "Error: expecting an identifier",
8 [errComma] = "Error: expecting ','",
9} 7}
10 8
11local tlabels = { ["errUndef"] = errUndef, 9local errmsgs = {}
12 ["errId"] = errId, 10local labels = {}
13 ["errComma"] = errComma } 11
12for i, err in ipairs(errinfo) do
13 errmsgs[i] = err[2]
14 labels[err[1]] = i
15end
16
17re.setlabels(labels)
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
14 25
15re.setlabels(tlabels)
16 26
17local g = re.compile[[ 27local g = re.compile[[
18 S <- Id List 28 S <- Id List
19 List <- !. / (',' / %{errComma}) Id List 29 List <- !. / (',' / %{errComma}) (Id / %{errId}) List
20 Id <- [a-z] / %{errId} 30 Id <- Sp [a-z]+
31 Comma <- Sp ','
32 Sp <- %s*
21]] 33]]
22 34
23function mymatch (g, s) 35function mymatch (g, s)
24 local r, e, sfail = g:match(s) 36 local r, e, sfail = g:match(s)
25 if not r then 37 if not r then
26 return r, terror[e] .. " before '" .. sfail .. "'" 38 local line, col = calcline(s, #s - #sfail)
39 local msg = "Error at line " .. line .. " (col " .. col .. "): "
40 return r, msg .. errmsgs[e] .. " before '" .. sfail .. "'"
27 end 41 end
28 return r 42 return r
29end 43end
30
31print(mymatch(g, "a,b"))
32print(mymatch(g, "a b"))
33print(mymatch(g, ", b"))
34 44
45print(mymatch(g, "one,two"))
46print(mymatch(g, "one two"))
47print(mymatch(g, "one,\n two,\nthree,"))
35 48