aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorsergio <sqmedeiros@gmail.com>2016-12-09 10:50:34 -0300
committersergio <sqmedeiros@gmail.com>2016-12-09 10:50:34 -0300
commit2f1e173c3d7670f802a087cd2c80afc5b0ed23c3 (patch)
treeb5239cb07b8e92af6b48a11959688e3185f459cd /examples
parent8e3c0330defb4b5da81f88c9b45bc5fc9361eb34 (diff)
downloadlpeglabel-2f1e173c3d7670f802a087cd2c80afc5b0ed23c3.tar.gz
lpeglabel-2f1e173c3d7670f802a087cd2c80afc5b0ed23c3.tar.bz2
lpeglabel-2f1e173c3d7670f802a087cd2c80afc5b0ed23c3.zip
Updating documentantion and polishing examples
Diffstat (limited to 'examples')
-rw-r--r--examples/expRec.lua50
-rw-r--r--examples/expRecAut.lua52
-rw-r--r--examples/listId2Rec2.lua2
-rw-r--r--examples/listIdRe2.lua31
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 @@
1local m = require"lpeglabelrec" 1local m = require"lpeglabelrec"
2local re = require"relabelrec" 2local re = require"relabelrec"
3 3
4local R, S, P, V = m.R, m.S, m.P, m.V
5local C, Cc, Ct, Cmt = m.C, m.Cc, m.Ct, m.Cmt
6local T, Rec = m.T, m.Rec
7
8local labels = { 4local 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
23local errors, subject 19local errors, subject
24 20
25local function expect(patt, labname, recpatt) 21local 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)
28end 24end
29 25
30 26
31local num = R("09")^1 / tonumber 27local num = m.R("09")^1 / tonumber
32local op = S("+-") 28local op = m.S("+-")
33 29
34local function compute(tokens) 30local 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
46end 42end
47 43
48local g = P { 44local 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
57function recorderror(pos, lab) 53function recorderror(pos, lab)
@@ -71,22 +67,23 @@ function defaultValue (p)
71 return p or m.Cc(1000) 67 return p or m.Cc(1000)
72end 68end
73 69
74local recg = P { 70local 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
85local function eval(input) 80local 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
99end 97end
100 98
101print(eval "90-70*5") 99print(eval "90-70-(5)+3")
102--> 20 100--> 20
103 101
104print(eval "2+") 102print(eval "15+")
105--> 2 + 0 103--> 2 + 0
106 104
107print(eval "-2") 105print(eval "-2")
@@ -126,3 +124,5 @@ print(eval "1+(")
126 124
127print(eval "3)") 125print(eval "3)")
128 126
127print(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 @@
1local m = require"lpeglabelrec" 1local m = require"lpeglabelrec"
2local re = require"relabelrec" 2local re = require"relabelrec"
3 3
4local R, S, P, V = m.R, m.S, m.P, m.V 4local num = m.R("09")^1 / tonumber
5local C, Cc, Ct, Cmt = m.C, m.Cc, m.Ct, m.Cmt 5local op = m.S("+-")
6local T, Rec = m.T, m.Rec
7
8local num = R("09")^1 / tonumber
9local op = S("+-")
10 6
11local labels = {} 7local labels = {}
12local nlabels = 0 8local nlabels = 0
@@ -27,7 +23,7 @@ local errors, subject
27 23
28local function expect(patt, labname) 24local 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)
31end 27end
32 28
33local function compute(tokens) 29local function compute(tokens)
@@ -44,13 +40,13 @@ local function compute(tokens)
44 return result 40 return result
45end 41end
46 42
47local g = P { 43local 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
56function recorderror(pos, lab) 52function recorderror(pos, lab)
@@ -70,27 +66,18 @@ function defaultValue (p)
70 return p or m.Cc(1000) 66 return p or m.Cc(1000)
71end 67end
72 68
73local recg2 = g 69local grec = g * expect(m.P(-1), "Extra")
74for k, v in pairs(labels) do 70for 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)
76end 72end
77 73
78local 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
89local function eval(input) 74local 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
103end 91end
104 92
105print(eval "90-70*5") 93print(eval "90-70-(5)+3")
106--> 20 94--> 18
107 95
108print(eval "2+") 96print(eval "15+")
109--> 2 + 0 97--> 2 + 0
110 98
111print(eval "-2") 99print(eval "-2")
@@ -130,3 +118,5 @@ print(eval "1+(")
130 118
131print(eval "3)") 119print(eval "3)")
132 120
121print(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
62end 62end
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)
19local g = re.compile[[ 19local 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
39local grec = re.compile( 39local 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
45function mymatch (g, s) 45function 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
57end 66end
58 67
59print(mymatch(grec, "one,two")) 68mymatch(grec, "one,two")
60print(mymatch(grec, "one two three")) 69mymatch(grec, "one two three")
61print(mymatch(grec, "1,\n two, \n3,")) 70mymatch(grec, "1,\n two, \n3,")
62print(mymatch(grec, "one\n two123, \nthree,")) 71mymatch(grec, "one\n two123, \nthree,")