diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-11-29 18:06:10 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2016-11-29 18:06:10 -0300 |
commit | e7e17699870f0bd6ba43b4e946297fb581d28b48 (patch) | |
tree | 36d938b8d93c916ee47548d78d0acd19fe296658 | |
parent | 3a5f5683bf6d676ea4118c3a8d0fc25de317d91f (diff) | |
download | lpeglabel-e7e17699870f0bd6ba43b4e946297fb581d28b48.tar.gz lpeglabel-e7e17699870f0bd6ba43b4e946297fb581d28b48.tar.bz2 lpeglabel-e7e17699870f0bd6ba43b4e946297fb581d28b48.zip |
Updating examples
-rw-r--r-- | examples/expressionRecovery.lua (renamed from examples/recoveryRecCap.lua) | 10 | ||||
-rw-r--r-- | examples/recoveryOpFail.lua | 108 | ||||
-rw-r--r-- | examples/recoveryOpLab.lua | 107 | ||||
-rwxr-xr-x | examples/typedlua/test.lua | 9 | ||||
-rw-r--r-- | examples/typedlua/tllexer.lua | 11 | ||||
-rw-r--r-- | examples/typedlua/tlparser.lua | 2 | ||||
-rw-r--r-- | testlabel.lua | 9 |
7 files changed, 29 insertions, 227 deletions
diff --git a/examples/recoveryRecCap.lua b/examples/expressionRecovery.lua index c1b3a53..a4a3288 100644 --- a/examples/recoveryRecCap.lua +++ b/examples/expressionRecovery.lua | |||
@@ -68,7 +68,7 @@ function sync (p) | |||
68 | end | 68 | end |
69 | 69 | ||
70 | function defaultValue (p) | 70 | function defaultValue (p) |
71 | return p or m.Cc(0) | 71 | return p or m.Cc(1000) |
72 | end | 72 | end |
73 | 73 | ||
74 | local recg = P { | 74 | local recg = P { |
@@ -91,6 +91,7 @@ local function eval(input) | |||
91 | for i, err in ipairs(errors) do | 91 | for i, err in ipairs(errors) do |
92 | local pos = err.col | 92 | local pos = err.col |
93 | local msg = err.msg | 93 | local msg = err.msg |
94 | print("sub", subject) | ||
94 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") | 95 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") |
95 | end | 96 | end |
96 | print(table.concat(out, "\n")) | 97 | print(table.concat(out, "\n")) |
@@ -111,7 +112,7 @@ print(eval "1+3+-9") | |||
111 | --> 1 + 3 + [0] - 9 | 112 | --> 1 + 3 + [0] - 9 |
112 | 113 | ||
113 | print(eval "1+()3+") | 114 | print(eval "1+()3+") |
114 | --> 1 + ([0]) [+] 3 + [0] | 115 | --> 1 + ([0]) [3 +] [0] |
115 | 116 | ||
116 | print(eval "8-(2+)-5") | 117 | print(eval "8-(2+)-5") |
117 | --> 8 - (2 + [0]) - 5 | 118 | --> 8 - (2 + [0]) - 5 |
@@ -119,3 +120,8 @@ print(eval "8-(2+)-5") | |||
119 | print(eval "()") | 120 | print(eval "()") |
120 | 121 | ||
121 | print(eval "") | 122 | print(eval "") |
123 | |||
124 | print(eval "1+()+") | ||
125 | |||
126 | print(eval "3)") | ||
127 | |||
diff --git a/examples/recoveryOpFail.lua b/examples/recoveryOpFail.lua deleted file mode 100644 index bd1beb7..0000000 --- a/examples/recoveryOpFail.lua +++ /dev/null | |||
@@ -1,108 +0,0 @@ | |||
1 | local lpeg = require"lpeglabelrec" | ||
2 | |||
3 | local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V | ||
4 | local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt | ||
5 | local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec | ||
6 | |||
7 | local labels = { | ||
8 | {"NoExp", "no expression found"}, | ||
9 | {"Extra", "extra characters found after the expression"}, | ||
10 | {"ExpTerm", "expected a term after the operator"}, | ||
11 | {"ExpExp", "expected an expression after the parenthesis"}, | ||
12 | {"MisClose", "missing a closing ')' after the expression"}, | ||
13 | } | ||
14 | |||
15 | local function labelindex(labname) | ||
16 | for i, elem in ipairs(labels) do | ||
17 | if elem[1] == labname then | ||
18 | return i | ||
19 | end | ||
20 | end | ||
21 | error("could not find label: " .. labname) | ||
22 | end | ||
23 | |||
24 | local errors = {} | ||
25 | |||
26 | local function expect(patt, labname, recpatt) | ||
27 | local i = labelindex(labname) | ||
28 | function recorderror(input, pos) | ||
29 | table.insert(errors, {i, pos}) | ||
30 | return true | ||
31 | end | ||
32 | if not recpatt then recpatt = P"" end | ||
33 | return Rec(patt, Cmt("", recorderror) * recpatt) | ||
34 | end | ||
35 | |||
36 | local num = R("09")^1 / tonumber | ||
37 | local op = S("+-*/") | ||
38 | |||
39 | local function compute(tokens) | ||
40 | local result = tokens[1] | ||
41 | for i = 2, #tokens, 2 do | ||
42 | if tokens[i] == '+' then | ||
43 | result = result + tokens[i+1] | ||
44 | elseif tokens[i] == '-' then | ||
45 | result = result - tokens[i+1] | ||
46 | elseif tokens[i] == '*' then | ||
47 | result = result * tokens[i+1] | ||
48 | elseif tokens[i] == '/' then | ||
49 | result = result / tokens[i+1] | ||
50 | else | ||
51 | error('unknown operation: ' .. tokens[i]) | ||
52 | end | ||
53 | end | ||
54 | return result | ||
55 | end | ||
56 | |||
57 | |||
58 | local g = P { | ||
59 | "Exp", | ||
60 | Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; | ||
61 | OpRecov = V"Operand"; | ||
62 | Operand = expect(V"Term", "ExpTerm", Cc(0)); | ||
63 | Term = num + V"Group"; | ||
64 | Group = "(" * V"InnerExp" * expect(")", "MisClose", ""); | ||
65 | InnerExp = expect(V"Exp", "ExpExp", (P(1) - ")")^0 * Cc(0)); | ||
66 | } | ||
67 | |||
68 | g = expect(g, "NoExp", P(1)^0) * expect(-P(1), "Extra") | ||
69 | |||
70 | local function eval(input) | ||
71 | local result, label, suffix = g:match(input) | ||
72 | if #errors == 0 then | ||
73 | return result | ||
74 | else | ||
75 | local out = {} | ||
76 | for i, err in ipairs(errors) do | ||
77 | local pos = err[2] | ||
78 | local msg = labels[err[1]][2] | ||
79 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") | ||
80 | end | ||
81 | errors = {} | ||
82 | return nil, table.concat(out, "\n") | ||
83 | end | ||
84 | end | ||
85 | |||
86 | print(eval "98-76*(54/32)") | ||
87 | --> 37.125 | ||
88 | |||
89 | print(eval "(1+1-1*2/2") | ||
90 | --> syntax error: missing a closing ')' after the expression (at index 11) | ||
91 | |||
92 | print(eval "(1+)-1*(2/2)") | ||
93 | --> syntax error: expected a term after the operator (at index 4) | ||
94 | |||
95 | print(eval "(1+1)-1*(/2)") | ||
96 | --> syntax error: expected an expression after the parenthesis (at index 10) | ||
97 | |||
98 | print(eval "1+(1-(1*2))/2x") | ||
99 | --> syntax error: extra chracters found after the expression (at index 14) | ||
100 | |||
101 | print(eval "-1+(1-(1*2))/2") | ||
102 | --> syntax error: no expression found (at index 1) | ||
103 | |||
104 | print(eval "(1+1-1*(2/2+)-():") | ||
105 | --> syntax error: expected a term after the operator (at index 13) | ||
106 | --> syntax error: expected an expression after the parenthesis (at index 16) | ||
107 | --> syntax error: missing a closing ')' after the expression (at index 17) | ||
108 | --> syntax error: extra characters found after the expression (at index 17) | ||
diff --git a/examples/recoveryOpLab.lua b/examples/recoveryOpLab.lua deleted file mode 100644 index 6697f8b..0000000 --- a/examples/recoveryOpLab.lua +++ /dev/null | |||
@@ -1,107 +0,0 @@ | |||
1 | local lpeg = require"lpeglabel" | ||
2 | |||
3 | local R, S, P, V = lpeg.R, lpeg.S, lpeg.P, lpeg.V | ||
4 | local C, Cc, Ct, Cmt = lpeg.C, lpeg.Cc, lpeg.Ct, lpeg.Cmt | ||
5 | local T, Lc, Rec = lpeg.T, lpeg.Lc, lpeg.Rec | ||
6 | |||
7 | local labels = { | ||
8 | {"NoExp", "no expression found"}, | ||
9 | {"Extra", "extra characters found after the expression"}, | ||
10 | {"ExpTerm", "expected a term after the operator"}, | ||
11 | {"ExpExp", "expected an expression after the parenthesis"}, | ||
12 | {"MisClose", "missing a closing ')' after the expression"}, | ||
13 | } | ||
14 | |||
15 | local function labelindex(labname) | ||
16 | for i, elem in ipairs(labels) do | ||
17 | if elem[1] == labname then | ||
18 | return i | ||
19 | end | ||
20 | end | ||
21 | error("could not find label: " .. labname) | ||
22 | end | ||
23 | |||
24 | local errors = {} | ||
25 | |||
26 | local function expect(patt, labname) | ||
27 | local i = labelindex(labname) | ||
28 | function recorderror(input, pos) | ||
29 | table.insert(errors, {i, pos}) | ||
30 | return true | ||
31 | end | ||
32 | return patt + Cmt("", recorderror) * T(i) | ||
33 | end | ||
34 | |||
35 | local num = R("09")^1 / tonumber | ||
36 | local op = S("+-*/") | ||
37 | |||
38 | local function compute(tokens) | ||
39 | local result = tokens[1] | ||
40 | for i = 2, #tokens, 2 do | ||
41 | if tokens[i] == '+' then | ||
42 | result = result + tokens[i+1] | ||
43 | elseif tokens[i] == '-' then | ||
44 | result = result - tokens[i+1] | ||
45 | elseif tokens[i] == '*' then | ||
46 | result = result * tokens[i+1] | ||
47 | elseif tokens[i] == '/' then | ||
48 | result = result / tokens[i+1] | ||
49 | else | ||
50 | error('unknown operation: ' .. tokens[i]) | ||
51 | end | ||
52 | end | ||
53 | return result | ||
54 | end | ||
55 | |||
56 | |||
57 | local g = P { | ||
58 | "Exp", | ||
59 | Exp = Ct(V"Term" * (C(op) * V"OpRecov")^0) / compute; | ||
60 | OpRecov = Rec(V"Operand", Cc(0), labelindex("ExpTerm")); | ||
61 | Operand = expect(V"Term", "ExpTerm"); | ||
62 | Term = num + Rec(V"Group", P"", labelindex("MisClose")); | ||
63 | Group = "(" * Rec(V"InnerExp", (P(1) - ")")^0 * Cc(0), labelindex("ExpExp")) * expect(")", "MisClose"); | ||
64 | InnerExp = expect(V"Exp", "ExpExp"); | ||
65 | } | ||
66 | |||
67 | g = expect(g, "NoExp") * expect(-P(1), "Extra") | ||
68 | |||
69 | local function eval(input) | ||
70 | local result, label, suffix = g:match(input) | ||
71 | if #errors == 0 then | ||
72 | return result | ||
73 | else | ||
74 | local out = {} | ||
75 | for i, err in ipairs(errors) do | ||
76 | local pos = err[2] | ||
77 | local msg = labels[err[1]][2] | ||
78 | table.insert(out, "syntax error: " .. msg .. " (at index " .. pos .. ")") | ||
79 | end | ||
80 | errors = {} | ||
81 | return nil, table.concat(out, "\n") | ||
82 | end | ||
83 | end | ||
84 | |||
85 | print(eval "98-76*(54/32)") | ||
86 | --> 37.125 | ||
87 | |||
88 | print(eval "(1+1-1*2/2") | ||
89 | --> syntax error: missing a closing ')' after the expression (at index 11) | ||
90 | |||
91 | print(eval "(1+)-1*(2/2)") | ||
92 | --> syntax error: expected a term after the operator (at index 4) | ||
93 | |||
94 | print(eval "(1+1)-1*(/2)") | ||
95 | --> syntax error: expected an expression after the parenthesis (at index 10) | ||
96 | |||
97 | print(eval "1+(1-(1*2))/2x") | ||
98 | --> syntax error: extra chracters found after the expression (at index 14) | ||
99 | |||
100 | print(eval "-1+(1-(1*2))/2") | ||
101 | --> syntax error: no expression found (at index 1) | ||
102 | |||
103 | print(eval "(1+1-1*(2/2+)-():") | ||
104 | --> syntax error: expected a term after the operator (at index 13) | ||
105 | --> syntax error: expected an expression after the parenthesis (at index 16) | ||
106 | --> syntax error: missing a closing ')' after the expression (at index 17) | ||
107 | --> syntax error: extra characters found after the expression (at index 17) | ||
diff --git a/examples/typedlua/test.lua b/examples/typedlua/test.lua index ed4e7a1..95474ba 100755 --- a/examples/typedlua/test.lua +++ b/examples/typedlua/test.lua | |||
@@ -401,15 +401,18 @@ assert(m == e) | |||
401 | -- unfinished comments | 401 | -- unfinished comments |
402 | 402 | ||
403 | s = [=[ | 403 | s = [=[ |
404 | --[[ testing | 404 | --[[ |
405 | |||
406 | testing | ||
405 | unfinished | 407 | unfinished |
408 | |||
406 | comment | 409 | comment |
407 | ]=] | 410 | ]=] |
408 | --[=[ | 411 | --[=[ |
409 | test.lua:3:1: syntax error, unexpected 'comment', expecting '=', ',', 'String', '{', '(', ':', '[', '.' | 412 | test.lua:3:1: syntax error, unexpected 'comment', expecting '=', ',', 'String', '{', '(', ':', '[', '.' |
410 | ]=] | 413 | ]=] |
411 | e = [=[ | 414 | e = [=[ |
412 | test.lua:1:1: unfinished long comment | 415 | test.lua:1:2: unfinished long comment |
413 | ]=] | 416 | ]=] |
414 | 417 | ||
415 | r, m = parse(s) | 418 | r, m = parse(s) |
diff --git a/examples/typedlua/tllexer.lua b/examples/typedlua/tllexer.lua index 6517ba5..d6033ec 100644 --- a/examples/typedlua/tllexer.lua +++ b/examples/typedlua/tllexer.lua | |||
@@ -1,6 +1,6 @@ | |||
1 | local tllexer = {} | 1 | local tllexer = {} |
2 | 2 | ||
3 | local lpeg = require "lpeglabel" | 3 | local lpeg = require "lpeglabelrec" |
4 | lpeg.locale(lpeg) | 4 | lpeg.locale(lpeg) |
5 | 5 | ||
6 | local tlerror = require "tlerror" | 6 | local tlerror = require "tlerror" |
@@ -9,10 +9,6 @@ function tllexer.try (pat, label) | |||
9 | return pat + lpeg.T(tlerror.labels[label]) | 9 | return pat + lpeg.T(tlerror.labels[label]) |
10 | end | 10 | end |
11 | 11 | ||
12 | function tllexer.catch (pat, label) | ||
13 | return lpeg.Lc(pat, lpeg.P(false), tlerror.labels[label]) | ||
14 | end | ||
15 | |||
16 | local function setffp (s, i, t, n) | 12 | local function setffp (s, i, t, n) |
17 | if not t.ffp or i > t.ffp then | 13 | if not t.ffp or i > t.ffp then |
18 | t.ffp = i | 14 | t.ffp = i |
@@ -45,7 +41,10 @@ local CloseEQ = lpeg.Cmt(Close * lpeg.Cb("init"), | |||
45 | local LongString = Open * (lpeg.P(1) - CloseEQ)^0 * tllexer.try(Close, "LongString") / | 41 | local LongString = Open * (lpeg.P(1) - CloseEQ)^0 * tllexer.try(Close, "LongString") / |
46 | function (s, o) return s end | 42 | function (s, o) return s end |
47 | 43 | ||
48 | local Comment = lpeg.Lc(lpeg.P("--") * LongString / function () return end, | 44 | local LongStringCm1 = Open * (lpeg.P(1) - CloseEQ)^0 * Close / |
45 | function (s, o) return s end | ||
46 | |||
47 | local Comment = lpeg.Rec(lpeg.P"--" * #Open * (LongStringCm1 / function() return end + lpeg.T(tlerror.labels["LongString"])), | ||
49 | lpeg.T(tlerror.labels["LongComment"]), tlerror.labels["LongString"]) + | 48 | lpeg.T(tlerror.labels["LongComment"]), tlerror.labels["LongString"]) + |
50 | lpeg.P("--") * (lpeg.P(1) - lpeg.P("\n"))^0 | 49 | lpeg.P("--") * (lpeg.P(1) - lpeg.P("\n"))^0 |
51 | 50 | ||
diff --git a/examples/typedlua/tlparser.lua b/examples/typedlua/tlparser.lua index a301fa6..fe4fd5e 100644 --- a/examples/typedlua/tlparser.lua +++ b/examples/typedlua/tlparser.lua | |||
@@ -1,6 +1,6 @@ | |||
1 | local tlparser = {} | 1 | local tlparser = {} |
2 | 2 | ||
3 | local lpeg = require "lpeglabel" | 3 | local lpeg = require "lpeglabelrec" |
4 | lpeg.locale(lpeg) | 4 | lpeg.locale(lpeg) |
5 | 5 | ||
6 | local tllexer = require "tllexer" | 6 | local tllexer = require "tllexer" |
diff --git a/testlabel.lua b/testlabel.lua index cb92657..d9bac64 100644 --- a/testlabel.lua +++ b/testlabel.lua | |||
@@ -173,6 +173,15 @@ p = m.Rec(#m.T(22), m.P"a", 22) | |||
173 | r, l, serror = p:match("bbc") | 173 | r, l, serror = p:match("bbc") |
174 | assert(r == nil and l == 0 and serror == "bbc") | 174 | assert(r == nil and l == 0 and serror == "bbc") |
175 | 175 | ||
176 | p = m.Rec(#m.P("a") * m.T(22), m.T(15), 22) | ||
177 | r, l, serror = p:match("abc") | ||
178 | assert(r == nil and l == 15 and serror == "abc") | ||
179 | |||
180 | p = m.Rec(#(m.P("a") * m.T(22)), m.T(15), 22) | ||
181 | r, l, serror = p:match("abc") | ||
182 | assert(r == nil and l == 15 and serror == "bc") | ||
183 | |||
184 | |||
176 | 185 | ||
177 | -- tests related to repetition | 186 | -- tests related to repetition |
178 | p = m.T(1)^0 | 187 | p = m.T(1)^0 |