diff options
author | sergio <sqmedeiros@gmail.com> | 2016-12-09 10:50:34 -0300 |
---|---|---|
committer | sergio <sqmedeiros@gmail.com> | 2016-12-09 10:50:34 -0300 |
commit | 2f1e173c3d7670f802a087cd2c80afc5b0ed23c3 (patch) | |
tree | b5239cb07b8e92af6b48a11959688e3185f459cd /examples | |
parent | 8e3c0330defb4b5da81f88c9b45bc5fc9361eb34 (diff) | |
download | lpeglabel-2f1e173c3d7670f802a087cd2c80afc5b0ed23c3.tar.gz lpeglabel-2f1e173c3d7670f802a087cd2c80afc5b0ed23c3.tar.bz2 lpeglabel-2f1e173c3d7670f802a087cd2c80afc5b0ed23c3.zip |
Updating documentantion and polishing examples
Diffstat (limited to 'examples')
-rw-r--r-- | examples/expRec.lua | 50 | ||||
-rw-r--r-- | examples/expRecAut.lua | 52 | ||||
-rw-r--r-- | examples/listId2Rec2.lua | 2 | ||||
-rw-r--r-- | examples/listIdRe2.lua | 31 |
4 files changed, 67 insertions, 68 deletions
diff --git a/examples/expRec.lua b/examples/expRec.lua index c5cbcca..5c5fd7d 100644 --- a/examples/expRec.lua +++ b/examples/expRec.lua | |||
@@ -1,10 +1,6 @@ | |||
1 | local m = require"lpeglabelrec" | 1 | local m = require"lpeglabelrec" |
2 | local re = require"relabelrec" | 2 | local re = require"relabelrec" |
3 | 3 | ||
4 | local R, S, P, V = m.R, m.S, m.P, m.V | ||
5 | local C, Cc, Ct, Cmt = m.C, m.Cc, m.Ct, m.Cmt | ||
6 | local T, Rec = m.T, m.Rec | ||
7 | |||
8 | local labels = { | 4 | local labels = { |
9 | {"ExpTermFirst", "expected an expression"}, | 5 | {"ExpTermFirst", "expected an expression"}, |
10 | {"ExpTermOp", "expected a term after the operator"}, | 6 | {"ExpTermOp", "expected a term after the operator"}, |
@@ -22,14 +18,14 @@ end | |||
22 | 18 | ||
23 | local errors, subject | 19 | local errors, subject |
24 | 20 | ||
25 | local function expect(patt, labname, recpatt) | 21 | local function expect(patt, labname) |
26 | local i = labelindex(labname) | 22 | local i = labelindex(labname) |
27 | return patt + T(i) | 23 | return patt + m.T(i) |
28 | end | 24 | end |
29 | 25 | ||
30 | 26 | ||
31 | local num = R("09")^1 / tonumber | 27 | local num = m.R("09")^1 / tonumber |
32 | local op = S("+-") | 28 | local op = m.S("+-") |
33 | 29 | ||
34 | local function compute(tokens) | 30 | local function compute(tokens) |
35 | local result = tokens[1] | 31 | local result = tokens[1] |
@@ -45,13 +41,13 @@ local function compute(tokens) | |||
45 | return result | 41 | return result |
46 | end | 42 | end |
47 | 43 | ||
48 | local g = P { | 44 | local g = m.P { |
49 | "Exp", | 45 | "Exp", |
50 | Exp = Ct(V"OperandFirst" * (C(op) * V"Operand")^0) / compute, | 46 | Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, |
51 | OperandFirst = expect(V"Term", "ExpTermFirst"), | 47 | OperandFirst = expect(m.V"Term", "ExpTermFirst"), |
52 | Operand = expect(V"Term", "ExpTermOp"), | 48 | Operand = expect(m.V"Term", "ExpTermOp"), |
53 | Term = num + V"Group", | 49 | Term = num + m.V"Group", |
54 | Group = "(" * V"Exp" * expect(")", "MisClose"), | 50 | Group = "(" * m.V"Exp" * expect(")", "MisClose"), |
55 | } | 51 | } |
56 | 52 | ||
57 | function recorderror(pos, lab) | 53 | function recorderror(pos, lab) |
@@ -71,22 +67,23 @@ function defaultValue (p) | |||
71 | return p or m.Cc(1000) | 67 | return p or m.Cc(1000) |
72 | end | 68 | end |
73 | 69 | ||
74 | local recg = P { | 70 | local grec = m.P { |
75 | "S", | 71 | "S", |
76 | S = Rec(V"A", V"ErrExpTermFirst", labelindex("ExpTermFirst")), -- default value is 0 | 72 | S = m.Rec(m.V"A", m.V"ErrExpTermFirst", labelindex("ExpTermFirst")), -- default value is 0 |
77 | A = Rec(V"Sg", V"ErrExpTermOp", labelindex("ExpTermOp")), | 73 | A = m.Rec(m.V"Sg", m.V"ErrExpTermOp", labelindex("ExpTermOp")), |
78 | Sg = Rec(g, V"ErrMisClose", labelindex("MisClose")), | 74 | Sg = m.Rec(g, m.V"ErrMisClose", labelindex("MisClose")), |
79 | ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), | 75 | ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), |
80 | ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), | 76 | ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), |
81 | ErrMisClose = record("MisClose") * sync(P")") * defaultValue(m.P""), | 77 | ErrMisClose = record("MisClose") * sync(m.P")") * defaultValue(m.P""), |
82 | } | 78 | } |
83 | 79 | ||
84 | |||
85 | local function eval(input) | 80 | local function eval(input) |
86 | errors = {} | 81 | errors = {} |
82 | io.write("Input: ", input, "\n") | ||
87 | subject = input | 83 | subject = input |
88 | local result, label, suffix = recg:match(input) | 84 | local result, label, suffix = grec:match(input) |
89 | if #errors > 0 then | 85 | io.write("Syntactic errors found: " .. #errors, "\n") |
86 | if #errors > 0 then | ||
90 | local out = {} | 87 | local out = {} |
91 | for i, err in ipairs(errors) do | 88 | for i, err in ipairs(errors) do |
92 | local pos = err.col | 89 | local pos = err.col |
@@ -95,13 +92,14 @@ local function eval(input) | |||
95 | end | 92 | end |
96 | print(table.concat(out, "\n")) | 93 | print(table.concat(out, "\n")) |
97 | end | 94 | end |
95 | io.write("Result = ") | ||
98 | return result | 96 | return result |
99 | end | 97 | end |
100 | 98 | ||
101 | print(eval "90-70*5") | 99 | print(eval "90-70-(5)+3") |
102 | --> 20 | 100 | --> 20 |
103 | 101 | ||
104 | print(eval "2+") | 102 | print(eval "15+") |
105 | --> 2 + 0 | 103 | --> 2 + 0 |
106 | 104 | ||
107 | print(eval "-2") | 105 | print(eval "-2") |
@@ -126,3 +124,5 @@ print(eval "1+(") | |||
126 | 124 | ||
127 | print(eval "3)") | 125 | print(eval "3)") |
128 | 126 | ||
127 | print(eval "11+())3") | ||
128 | |||
diff --git a/examples/expRecAut.lua b/examples/expRecAut.lua index e098078..f870d73 100644 --- a/examples/expRecAut.lua +++ b/examples/expRecAut.lua | |||
@@ -1,12 +1,8 @@ | |||
1 | local m = require"lpeglabelrec" | 1 | local m = require"lpeglabelrec" |
2 | local re = require"relabelrec" | 2 | local re = require"relabelrec" |
3 | 3 | ||
4 | local R, S, P, V = m.R, m.S, m.P, m.V | 4 | local num = m.R("09")^1 / tonumber |
5 | local C, Cc, Ct, Cmt = m.C, m.Cc, m.Ct, m.Cmt | 5 | local op = m.S("+-") |
6 | local T, Rec = m.T, m.Rec | ||
7 | |||
8 | local num = R("09")^1 / tonumber | ||
9 | local op = S("+-") | ||
10 | 6 | ||
11 | local labels = {} | 7 | local labels = {} |
12 | local nlabels = 0 | 8 | local nlabels = 0 |
@@ -27,7 +23,7 @@ local errors, subject | |||
27 | 23 | ||
28 | local function expect(patt, labname) | 24 | local function expect(patt, labname) |
29 | local i = labels[labname].id | 25 | local i = labels[labname].id |
30 | return patt + T(i) | 26 | return patt + m.T(i) |
31 | end | 27 | end |
32 | 28 | ||
33 | local function compute(tokens) | 29 | local function compute(tokens) |
@@ -44,13 +40,13 @@ local function compute(tokens) | |||
44 | return result | 40 | return result |
45 | end | 41 | end |
46 | 42 | ||
47 | local g = P { | 43 | local g = m.P { |
48 | "Exp", | 44 | "Exp", |
49 | Exp = Ct(V"OperandFirst" * (C(op) * V"Operand")^0) / compute, | 45 | Exp = m.Ct(m.V"OperandFirst" * (m.C(op) * m.V"Operand")^0) / compute, |
50 | OperandFirst = expect(V"Term", "ExpTermFirst"), | 46 | OperandFirst = expect(m.V"Term", "ExpTermFirst"), |
51 | Operand = expect(V"Term", "ExpTermOp"), | 47 | Operand = expect(m.V"Term", "ExpTermOp"), |
52 | Term = num + V"Group", | 48 | Term = num + m.V"Group", |
53 | Group = "(" * V"Exp" * expect(")", "MisClose"), | 49 | Group = "(" * m.V"Exp" * expect(")", "MisClose"), |
54 | } | 50 | } |
55 | 51 | ||
56 | function recorderror(pos, lab) | 52 | function recorderror(pos, lab) |
@@ -70,27 +66,18 @@ function defaultValue (p) | |||
70 | return p or m.Cc(1000) | 66 | return p or m.Cc(1000) |
71 | end | 67 | end |
72 | 68 | ||
73 | local recg2 = g | 69 | local grec = g * expect(m.P(-1), "Extra") |
74 | for k, v in pairs(labels) do | 70 | for k, v in pairs(labels) do |
75 | recg2 = Rec(recg2, record(k) * sync(v.psync) * v.pcap, v.id) | 71 | grec = m.Rec(grec, record(k) * sync(v.psync) * v.pcap, v.id) |
76 | end | 72 | end |
77 | 73 | ||
78 | local recg = P { | ||
79 | "S", | ||
80 | S = Rec(V"A", V"ErrExpTermFirst", labels["ExpTermFirst"].id), -- default value is 0 | ||
81 | A = Rec(V"Sg", V"ErrExpTermOp", labels["ExpTermOp"].id), | ||
82 | Sg = Rec(g, V"ErrMisClose", labels["MisClose"].id), | ||
83 | ErrExpTermFirst = record("ExpTermFirst") * sync(op + ")") * defaultValue(), | ||
84 | ErrExpTermOp = record("ExpTermOp") * sync(op + ")") * defaultValue(), | ||
85 | ErrMisClose = record("MisClose") * sync(P")") * defaultValue(m.P""), | ||
86 | } | ||
87 | |||
88 | |||
89 | local function eval(input) | 74 | local function eval(input) |
90 | errors = {} | 75 | errors = {} |
76 | io.write("Input: ", input, "\n") | ||
91 | subject = input | 77 | subject = input |
92 | local result, label, suffix = recg2:match(input) | 78 | local result, label, suffix = grec:match(input) |
93 | if #errors > 0 then | 79 | io.write("Syntactic errors found: " .. #errors, "\n") |
80 | if #errors > 0 then | ||
94 | local out = {} | 81 | local out = {} |
95 | for i, err in ipairs(errors) do | 82 | for i, err in ipairs(errors) do |
96 | local pos = err.col | 83 | local pos = err.col |
@@ -99,13 +86,14 @@ local function eval(input) | |||
99 | end | 86 | end |
100 | print(table.concat(out, "\n")) | 87 | print(table.concat(out, "\n")) |
101 | end | 88 | end |
89 | io.write("Result = ") | ||
102 | return result | 90 | return result |
103 | end | 91 | end |
104 | 92 | ||
105 | print(eval "90-70*5") | 93 | print(eval "90-70-(5)+3") |
106 | --> 20 | 94 | --> 18 |
107 | 95 | ||
108 | print(eval "2+") | 96 | print(eval "15+") |
109 | --> 2 + 0 | 97 | --> 2 + 0 |
110 | 98 | ||
111 | print(eval "-2") | 99 | print(eval "-2") |
@@ -130,3 +118,5 @@ print(eval "1+(") | |||
130 | 118 | ||
131 | print(eval "3)") | 119 | print(eval "3)") |
132 | 120 | ||
121 | print(eval "11+()3") | ||
122 | --> 1 + ([0]) [+] 3 + [0] | ||
diff --git a/examples/listId2Rec2.lua b/examples/listId2Rec2.lua index ab8f1dd..c6705dd 100644 --- a/examples/listId2Rec2.lua +++ b/examples/listId2Rec2.lua | |||
@@ -56,7 +56,7 @@ function mymatch (g, s) | |||
56 | local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg | 56 | local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg |
57 | table.insert(out, msg) | 57 | table.insert(out, msg) |
58 | end | 58 | end |
59 | return nil, table.concat(out, "\n") | 59 | return nil, table.concat(out, "\n") .. "\n" |
60 | end | 60 | end |
61 | return r | 61 | return r |
62 | end | 62 | end |
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua index 070bcdb..6bab6ba 100644 --- a/examples/listIdRe2.lua +++ b/examples/listIdRe2.lua | |||
@@ -19,7 +19,7 @@ re.setlabels(labels) | |||
19 | local g = re.compile[[ | 19 | local g = re.compile[[ |
20 | S <- Id List | 20 | S <- Id List |
21 | List <- !. / Comma Id List | 21 | List <- !. / Comma Id List |
22 | Id <- Sp [a-z]+ / %{errId} | 22 | Id <- Sp {[a-z]+} / %{errId} |
23 | Comma <- Sp ',' / %{errComma} | 23 | Comma <- Sp ',' / %{errComma} |
24 | Sp <- %s* | 24 | Sp <- %s* |
25 | ]] | 25 | ]] |
@@ -38,25 +38,34 @@ end | |||
38 | 38 | ||
39 | local grec = re.compile( | 39 | local grec = re.compile( |
40 | "S <- %g //{errComma} ErrComma //{errId} ErrId" .. "\n" .. | 40 | "S <- %g //{errComma} ErrComma //{errId} ErrId" .. "\n" .. |
41 | "ErrComma <- ('' -> 'errComma' => recorderror) " .. sync('!. / [a-z]+') .. "\n" .. | 41 | "ErrComma <- ('' -> 'errComma' => recorderror) " .. sync('[a-z]+') .. "\n" .. |
42 | "ErrId <- ('' -> 'errId' => recorderror) (!(!. / ',') .)*" | 42 | "ErrId <- ('' -> 'errId' => recorderror) " .. sync('","') .. "-> default" |
43 | , {g = g, recorderror = recorderror}) | 43 | , {g = g, recorderror = recorderror, default = "NONE"}) |
44 | 44 | ||
45 | function mymatch (g, s) | 45 | function mymatch (g, s) |
46 | errors = {} | 46 | errors = {} |
47 | local r, e, sfail = g:match(s) | 47 | subject = s |
48 | io.write("Input: ", s, "\n") | ||
49 | local r = { g:match(s) } | ||
50 | io.write("Captures (separated by ';'): ") | ||
51 | for k, v in pairs(r) do | ||
52 | io.write(v .. "; ") | ||
53 | end | ||
54 | io.write("\nSyntactic errors found: " .. #errors) | ||
48 | if #errors > 0 then | 55 | if #errors > 0 then |
56 | io.write("\n") | ||
49 | local out = {} | 57 | local out = {} |
50 | for i, err in ipairs(errors) do | 58 | for i, err in ipairs(errors) do |
51 | local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg | 59 | local msg = "Error at line " .. err.line .. " (col " .. err.col .. "): " .. err.msg |
52 | table.insert(out, msg) | 60 | table.insert(out, msg) |
53 | end | 61 | end |
54 | return nil, table.concat(out, "\n") | 62 | io.write(table.concat(out, "\n")) |
55 | end | 63 | end |
56 | return r | 64 | print("\n") |
65 | return r | ||
57 | end | 66 | end |
58 | 67 | ||
59 | print(mymatch(grec, "one,two")) | 68 | mymatch(grec, "one,two") |
60 | print(mymatch(grec, "one two three")) | 69 | mymatch(grec, "one two three") |
61 | print(mymatch(grec, "1,\n two, \n3,")) | 70 | mymatch(grec, "1,\n two, \n3,") |
62 | print(mymatch(grec, "one\n two123, \nthree,")) | 71 | mymatch(grec, "one\n two123, \nthree,") |