aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorSergio Medeiros <sqmedeiros@gmail.com>2015-03-23 15:09:08 -0300
committerSergio Medeiros <sqmedeiros@gmail.com>2015-03-23 15:09:08 -0300
commitf124b2d4449a13ac21af9581a44b4455d89eaddb (patch)
treee0486555ac811c5f712927f110bb7bcda8569adf /examples
parent0e93d536ba2d312502737cce2ab0cc21393c4842 (diff)
downloadlpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.tar.gz
lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.tar.bz2
lpeglabel-f124b2d4449a13ac21af9581a44b4455d89eaddb.zip
Renaming "re.lua" to "relabel.lua".
Adding examples, README and lpeglabel.html
Diffstat (limited to 'examples')
-rw-r--r--examples/listId1.lua27
-rw-r--r--examples/listId2.lua32
-rw-r--r--examples/listIdCatch.lua25
-rw-r--r--examples/listIdCatchRe.lua34
-rw-r--r--examples/listIdRe1.lua27
-rw-r--r--examples/listIdRe2.lua35
-rw-r--r--examples/tiny.lua163
7 files changed, 343 insertions, 0 deletions
diff --git a/examples/listId1.lua b/examples/listId1.lua
new file mode 100644
index 0000000..e075bd1
--- /dev/null
+++ b/examples/listId1.lua
@@ -0,0 +1,27 @@
1local m = require'lpeglabel'
2
3local g = m.P{
4 "S",
5 S = m.V"Id" * m.V"List",
6 List = -m.P(1) + ("," + m.T(2)) * m.V"Id" * m.V"List",
7 Id = m.R'az'^1 + m.T(1),
8}
9
10function mymatch (g, s)
11 local r, e = g:match(s)
12 if not r then
13 if e == 1 then
14 return r, "Error: expecting an identifier"
15 elseif e == 2 then
16 return r, "Error: expecting ','"
17 else
18 return r, "Error"
19 end
20 end
21 return r
22end
23
24print(mymatch(g, "a,b"))
25print(mymatch(g, "a b"))
26print(mymatch(g, ", b"))
27
diff --git a/examples/listId2.lua b/examples/listId2.lua
new file mode 100644
index 0000000..4943368
--- /dev/null
+++ b/examples/listId2.lua
@@ -0,0 +1,32 @@
1local m = require'lpeglabel'
2
3local errUndef = 0
4local errId = 1
5local errComma = 2
6
7local terror = {
8 [errUndef] = "Error",
9 [errId] = "Error: expecting an identifier",
10 [errComma] = "Error: expecting ','",
11}
12
13local g = m.P{
14 "S",
15 S = m.V"Id" * m.V"List",
16 List = -m.P(1) + ("," + m.T(errComma)) * m.V"Id" * m.V"List",
17 Id = m.R'az'^1 + m.T(errId),
18}
19
20function mymatch (g, s)
21 local r, e = g:match(s)
22 if not r then
23 return r, terror[e]
24 end
25 return r
26end
27
28print(mymatch(g, "a,b"))
29print(mymatch(g, "a b"))
30print(mymatch(g, ", b"))
31
32
diff --git a/examples/listIdCatch.lua b/examples/listIdCatch.lua
new file mode 100644
index 0000000..38ad2e5
--- /dev/null
+++ b/examples/listIdCatch.lua
@@ -0,0 +1,25 @@
1local m = require'lpeglabel'
2
3local errUndef, errId, errComma = 0, 1, 2
4
5local terror = {
6 [errUndef] = "Error",
7 [errId] = "Error: expecting an identifier",
8 [errComma] = "Error: expecting ','",
9}
10
11g = m.P{
12 "S",
13 S = m.Lc(m.Lc(m.V"Id" * m.V"List", m.V"ErrId", errId),
14 m.V"ErrComma", errComma),
15 List = -m.P(1) + m.V"Comma" * m.V"Id" * m.V"List",
16 Id = m.R'az'^1 + m.T(errId),
17 Comma = "," + m.T(errComma),
18 ErrId = m.Cc(errId) / terror,
19 ErrComma = m.Cc(errComma) / terror
20}
21
22print(g:match("a,b"))
23print(g:match("a b"))
24print(g:match(",b"))
25
diff --git a/examples/listIdCatchRe.lua b/examples/listIdCatchRe.lua
new file mode 100644
index 0000000..5d38fec
--- /dev/null
+++ b/examples/listIdCatchRe.lua
@@ -0,0 +1,34 @@
1local re = require're'
2
3local terror = {}
4
5local function newError(l, msg)
6 table.insert(terror, { l = l, msg = msg } )
7end
8
9newError("errId", "Error: expecting an identifier")
10newError("errComma", "Error: expecting ','")
11
12local labelCode = {}
13local labelMsg = {}
14for k, v in ipairs(terror) do
15 labelCode[v.l] = k
16 labelMsg[v.l] = v.msg
17end
18
19re.setlabels(labelCode)
20
21local p = re.compile([[
22 S <- Id List /{errId} ErrId /{errComma} ErrComma
23 List <- !. / Comma Id List
24 Id <- [a-z]+ / %{errId}
25 Comma <- ',' / %{errComma}
26 ErrId <- '' -> errId
27 ErrComma <- '' -> errComma
28]], labelMsg)
29
30print(p:match("a,b"))
31print(p:match("a b"))
32print(p:match(",b"))
33
34
diff --git a/examples/listIdRe1.lua b/examples/listIdRe1.lua
new file mode 100644
index 0000000..c75bb1d
--- /dev/null
+++ b/examples/listIdRe1.lua
@@ -0,0 +1,27 @@
1local re = require 're'
2
3local g = re.compile[[
4 S <- Id List
5 List <- !. / (',' / %{2}) Id List
6 Id <- [a-z] / %{1}
7]]
8
9function mymatch (g, s)
10 local r, e = g:match(s)
11 if not r then
12 if e == 1 then
13 return r, "Error: expecting an identifier"
14 elseif e == 2 then
15 return r, "Error: expecting ','"
16 else
17 return r, "Error"
18 end
19 end
20 return r
21end
22
23print(mymatch(g, "a,b"))
24print(mymatch(g, "a b"))
25print(mymatch(g, ", b"))
26
27
diff --git a/examples/listIdRe2.lua b/examples/listIdRe2.lua
new file mode 100644
index 0000000..67c42f1
--- /dev/null
+++ b/examples/listIdRe2.lua
@@ -0,0 +1,35 @@
1local re = require 're'
2
3local errUndef, errId, errComma = 0, 1, 2
4
5local terror = {
6 [errUndef] = "Error",
7 [errId] = "Error: expecting an identifier",
8 [errComma] = "Error: expecting ','",
9}
10
11local tlabels = { ["errUndef"] = errUndef,
12 ["errId"] = errId,
13 ["errComma"] = errComma }
14
15re.setlabels(tlabels)
16
17local g = re.compile[[
18 S <- Id List
19 List <- !. / (',' / %{errComma}) Id List
20 Id <- [a-z] / %{errId}
21]]
22
23function mymatch (g, s)
24 local r, e = g:match(s)
25 if not r then
26 return r, terror[e]
27 end
28 return r
29end
30
31print(mymatch(g, "a,b"))
32print(mymatch(g, "a b"))
33print(mymatch(g, ", b"))
34
35
diff --git a/examples/tiny.lua b/examples/tiny.lua
new file mode 100644
index 0000000..aa9dbea
--- /dev/null
+++ b/examples/tiny.lua
@@ -0,0 +1,163 @@
1local re = require 're'
2
3local terror = {}
4
5local function newError(l, msg)
6 table.insert(terror, { l = l, msg = msg} )
7end
8
9newError("errSemi", "Error: missing ';'")
10newError("errExpIf", "Error: expected expression after 'if'")
11newError("errThen", "Error: expected 'then' keyword")
12newError("errCmdSeq1", "Error: expected at least a command after 'then'")
13newError("errCmdSeq2", "Error: expected at least a command after 'else'")
14newError("errEnd", "Error: expected 'end' keyword")
15newError("errCmdSeqRep", "Error: expected at least a command after 'repeat'")
16newError("errUntil", "Error: expected 'until' keyword")
17newError("errExpRep", "Error: expected expression after 'until'")
18newError("errAssignOp", "Error: expected ':=' in assigment")
19newError("errExpAssign", "Error: expected expression after ':='")
20newError("errReadName", "Error: expected an identifier after 'read'")
21newError("errWriteExp", "Error: expected expression after 'write'")
22newError("errSimpExp", "Error: expected '(', ID, or number after '<' or '='")
23newError("errTerm", "Error: expected '(', ID, or number after '+' or '-'")
24newError("errFactor", "Error: expected '(', ID, or number after '*' or '/'")
25newError("errExpFac", "Error: expected expression after '('")
26newError("errClosePar", "Error: expected ')' after expression")
27
28local line
29
30local function incLine()
31 line = line + 1
32 return true
33end
34
35local function countLine(s, i)
36 line = 1
37 local p = re.compile([[
38 S <- (%nl -> incLine / .)*
39 ]], { incLine = incLine})
40 p:match(s:sub(1, i))
41 return true
42end
43
44local labelCode = {}
45for k, v in ipairs(terror) do
46 labelCode[v.l] = k
47end
48
49re.setlabels(labelCode)
50
51local g = re.compile([[
52 Tiny <- CmdSeq
53 CmdSeq <- (Cmd (SEMICOLON / ErrSemi)) (Cmd (SEMICOLON / ErrSemi))*
54 Cmd <- IfCmd / RepeatCmd / ReadCmd / WriteCmd / AssignCmd
55 IfCmd <- IF (Exp / ErrExpIf) (THEN / ErrThen) (CmdSeq / ErrCmdSeq1) (ELSE (CmdSeq / ErrCmdSeq2) / '') (END / ErrEnd)
56 RepeatCmd <- REPEAT (CmdSeq / ErrCmdSeqRep) (UNTIL / ErrUntil) (Exp / ErrExpRep)
57 AssignCmd <- NAME (ASSIGNMENT / ErrAssignOp) (Exp / ErrExpAssign)
58 ReadCmd <- READ (NAME / ErrReadName)
59 WriteCmd <- WRITE (Exp / ErrWriteExp)
60 Exp <- SimpleExp ((LESS / EQUAL) (SimpleExp / ErrSimpExp) / '')
61 SimpleExp <- Term ((ADD / SUB) (Term / ErrTerm))*
62 Term <- Factor ((MUL / DIV) (Factor / ErrFactor))*
63 Factor <- OPENPAR (Exp / ErrExpFac) (CLOSEPAR / ErrClosePar) / NUMBER / NAME
64 ErrSemi <- ErrCount %{errSemi}
65 ErrExpIf <- ErrCount %{errExpIf}
66 ErrThen <- ErrCount %{errThen}
67 ErrCmdSeq1 <- ErrCount %{errCmdSeq1}
68 ErrCmdSeq2 <- ErrCount %{errCmdSeq2}
69 ErrEnd <- ErrCount %{errEnd}
70 ErrCmdSeqRep <- ErrCount %{errCmdSeqRep}
71 ErrUntil <- ErrCount %{errUntil}
72 ErrExpRep <- ErrCount %{errExpRep}
73 ErrAssignOp <- ErrCount %{errAssignOp}
74 ErrExpAssign <- ErrCount %{errExpAssign}
75 ErrReadName <- ErrCount %{errReadName}
76 ErrWriteExp <- ErrCount %{errWriteExp}
77 ErrSimpExp <- ErrCount %{errSimpExp}
78 ErrTerm <- ErrCount %{errTerm}
79 ErrFactor <- ErrCount %{errFactor}
80 ErrExpFac <- ErrCount %{errExpFac}
81 ErrClosePar <- ErrCount %{errClosePar}
82 ErrCount <- '' => countLine
83 ADD <- Sp '+'
84 ASSIGNMENT <- Sp ':='
85 CLOSEPAR <- Sp ')'
86 DIV <- Sp '/'
87 IF <- Sp 'if'
88 ELSE <- Sp 'else'
89 END <- Sp 'end'
90 EQUAL <- Sp '='
91 LESS <- Sp '<'
92 MUL <- Sp '*'
93 NAME <- Sp !RESERVED [a-z]+
94 NUMBER <- Sp [0-9]+
95 OPENPAR <- Sp '('
96 READ <- Sp 'read'
97 REPEAT <- Sp 'repeat'
98 SEMICOLON <- Sp ';'
99 SUB <- Sp '-'
100 THEN <- Sp 'then'
101 UNTIL <- Sp 'until'
102 WRITE <- Sp 'write'
103 RESERVED <- (IF / ELSE / END / READ / REPEAT / THEN / UNTIL / WRITE) ![a-z]+
104 Sp <- %s*
105]], { countLine = countLine })
106
107
108local function printError(n, e)
109 assert(n == nil)
110 print("Line " .. line .. ": " .. terror[e].msg)
111end
112
113local s = [[
114n := 5;
115f := 1;
116repeat
117 f := f + n;
118 n := n - 1
119until (n < 1);
120write f;]]
121printError(g:match(s))
122
123s = [[
124n := 5;
125f := 1;
126repeat
127 f := f + n;
128 n := n - 1;
129until (n < 1);
130read ;]]
131printError(g:match(s))
132
133s = [[
134if a < 1 then
135 b := 2;
136else
137 b := 3;]]
138printError(g:match(s))
139
140s = [[
141n := 5;
142f := 1;
143repeat
144 f := f + n;
145 n := n - 1;
146untill (n < 1);
147]]
148printError(g:match(s))
149
150s = [[
151n := 5;
152f := 1;
153repeat
154 f := f + n;
155 n := n - 1;
1563 (n < 1);
157]]
158printError(g:match(s))
159
160printError(g:match("a : 2"))
161printError(g:match("a := (2"))
162
163