diff options
| author | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-06-28 16:00:31 -0300 |
|---|---|---|
| committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-06-28 16:00:31 -0300 |
| commit | 6fd747b3061728245d4f7a0b5fc5baab18dac895 (patch) | |
| tree | 1764d7817edd3740be17c9a7a96a871ccd082f6e /examples | |
| parent | 2f18ef50aeb1acb15ec1bbed3ad03e758e2756c9 (diff) | |
| download | lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.tar.gz lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.tar.bz2 lpeglabel-6fd747b3061728245d4f7a0b5fc5baab18dac895.zip | |
Updating the documentation and the examples
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/expect.lua (renamed from examples/expect.md) | 7 | ||||
| -rw-r--r-- | examples/listId1.lua | 45 | ||||
| -rw-r--r-- | examples/listId2.lua | 42 | ||||
| -rw-r--r-- | examples/listIdCatch.lua | 48 | ||||
| -rw-r--r-- | examples/listIdRe1.lua | 30 | ||||
| -rw-r--r-- | examples/listIdRe2.lua | 49 |
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 @@ | |||
| 1 | Here'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 | ||
| 3 | if the pattern fails to be matched. This function can be extended later on to | ||
| 4 | record all errors encountered once error recovery is implemented. | ||
| 5 | |||
| 6 | ```lua | ||
| 7 | local lpeg = require"lpeglabel" | 1 | local lpeg = require"lpeglabel" |
| 8 | 2 | ||
| 9 | local R, S, P, V, C, Ct, T = lpeg.R, lpeg.S, lpeg.P, lpeg.V, lpeg.C, lpeg.Ct, lpeg.T | 3 | local 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 | ||
| 85 | print(eval "-1+(1-(1*2))/2") | 79 | print(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 @@ | |||
| 1 | local m = require'lpeglabel' | 1 | local m = require'lpeglabel' |
| 2 | 2 | ||
| 3 | local 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 | ||
| 8 | end | ||
| 9 | |||
| 3 | local g = m.P{ | 10 | local 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 | ||
| 10 | function mymatch (g, s) | 19 | function 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 | ||
| 22 | end | 33 | end |
| 23 | 34 | ||
| 24 | print(mymatch(g, "a,b")) | 35 | print(mymatch(g, "one,two")) |
| 25 | print(mymatch(g, "a b")) | 36 | print(mymatch(g, "one two")) |
| 26 | print(mymatch(g, ", b")) | 37 | print(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 @@ | |||
| 1 | local m = require'lpeglabel' | 1 | local m = require'lpeglabel' |
| 2 | 2 | ||
| 3 | local errUndef = 0 | 3 | local terror = {} |
| 4 | local errId = 1 | ||
| 5 | local errComma = 2 | ||
| 6 | 4 | ||
| 7 | local terror = { | 5 | local function newError(s) |
| 8 | [errUndef] = "Error", | 6 | table.insert(terror, s) |
| 9 | [errId] = "Error: expecting an identifier", | 7 | return #terror |
| 10 | [errComma] = "Error: expecting ','", | 8 | end |
| 11 | } | 9 | |
| 10 | local errUndef = newError("undefined") | ||
| 11 | local errId = newError("expecting an identifier") | ||
| 12 | local errComma = newError("expecting ','") | ||
| 13 | |||
| 14 | local 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 | ||
| 19 | end | ||
| 12 | 20 | ||
| 13 | local g = m.P{ | 21 | local 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 | ||
| 20 | function mymatch (g, s) | 30 | function 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 |
| 26 | end | 38 | end |
| 27 | 39 | ||
| 28 | print(mymatch(g, "a,b")) | 40 | print(mymatch(g, "one,two")) |
| 29 | print(mymatch(g, "a b")) | 41 | print(mymatch(g, "one two")) |
| 30 | print(mymatch(g, ", b")) | 42 | print(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 @@ | |||
| 1 | local m = require'lpeglabel' | 1 | local m = require'lpeglabel' |
| 2 | 2 | ||
| 3 | local errUndef, errId, errComma = 0, 1, 2 | 3 | local terror = {} |
| 4 | 4 | ||
| 5 | local terror = { | 5 | local function newError(s) |
| 6 | [errUndef] = "Error", | 6 | table.insert(terror, s) |
| 7 | [errId] = "Error: expecting an identifier", | 7 | return #terror |
| 8 | [errComma] = "Error: expecting ','", | 8 | end |
| 9 | } | 9 | |
| 10 | local errUndef = newError("undefined") | ||
| 11 | local errId = newError("expecting an identifier") | ||
| 12 | local errComma = newError("expecting ','") | ||
| 13 | |||
| 14 | local 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 | ||
| 19 | end | ||
| 10 | 20 | ||
| 11 | g = m.P{ | 21 | local 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 | ||
| 22 | print(g:match("a,b")) | 33 | function mymatch (g, s) |
| 23 | print(g:match("a b")) | 34 | local r, e, sfail = g:match(s) |
| 24 | print(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 | ||
| 41 | end | ||
| 25 | 42 | ||
| 43 | print(mymatch(g, "one,two")) | ||
| 44 | print(mymatch(g, "one two")) | ||
| 45 | print(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 @@ | |||
| 1 | local re = require 'relabel' | 1 | local re = require 'relabel' |
| 2 | 2 | ||
| 3 | local 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 | ||
| 8 | end | ||
| 9 | |||
| 3 | local g = re.compile[[ | 10 | local 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 | ||
| 9 | function mymatch (g, s) | 18 | function 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 |
| 21 | end | 32 | end |
| 22 | |||
| 23 | print(mymatch(g, "a,b")) | ||
| 24 | print(mymatch(g, "a b")) | ||
| 25 | print(mymatch(g, ", b")) | ||
| 26 | 33 | ||
| 34 | print(mymatch(g, "one,two")) | ||
| 35 | print(mymatch(g, "one two")) | ||
| 36 | print(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 @@ | |||
| 1 | local re = require 'relabel' | 1 | local re = require 'relabel' |
| 2 | 2 | ||
| 3 | local errUndef, errId, errComma = 0, 1, 2 | 3 | local errinfo = { |
| 4 | 4 | {"errUndef", "undefined"}, | |
| 5 | local 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 | ||
| 11 | local tlabels = { ["errUndef"] = errUndef, | 9 | local errmsgs = {} |
| 12 | ["errId"] = errId, | 10 | local labels = {} |
| 13 | ["errComma"] = errComma } | 11 | |
| 12 | for i, err in ipairs(errinfo) do | ||
| 13 | errmsgs[i] = err[2] | ||
| 14 | labels[err[1]] = i | ||
| 15 | end | ||
| 16 | |||
| 17 | re.setlabels(labels) | ||
| 18 | |||
| 19 | local 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 | ||
| 24 | end | ||
| 14 | 25 | ||
| 15 | re.setlabels(tlabels) | ||
| 16 | 26 | ||
| 17 | local g = re.compile[[ | 27 | local 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 | ||
| 23 | function mymatch (g, s) | 35 | function 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 |
| 29 | end | 43 | end |
| 30 | |||
| 31 | print(mymatch(g, "a,b")) | ||
| 32 | print(mymatch(g, "a b")) | ||
| 33 | print(mymatch(g, ", b")) | ||
| 34 | 44 | ||
| 45 | print(mymatch(g, "one,two")) | ||
| 46 | print(mymatch(g, "one two")) | ||
| 47 | print(mymatch(g, "one,\n two,\nthree,")) | ||
| 35 | 48 | ||
