aboutsummaryrefslogtreecommitdiff
path: root/testlabel.lua
diff options
context:
space:
mode:
authorSergio Medeiros <sqmedeiros@gmail.com>2014-10-29 18:13:38 -0300
committerSergio Medeiros <sqmedeiros@gmail.com>2014-10-29 18:13:38 -0300
commit8d30a0ff8a8584e225c03d878a9add439ea193a3 (patch)
treecdc03907837ebec1aed26be7290a5a40d00f3e2c /testlabel.lua
parent3af55803f0a261dd9b2ccf85a7532288c4a270ae (diff)
downloadlpeglabel-8d30a0ff8a8584e225c03d878a9add439ea193a3.tar.gz
lpeglabel-8d30a0ff8a8584e225c03d878a9add439ea193a3.tar.bz2
lpeglabel-8d30a0ff8a8584e225c03d878a9add439ea193a3.zip
Creating the git repository with the current implementation.
Diffstat (limited to 'testlabel.lua')
-rw-r--r--testlabel.lua422
1 files changed, 422 insertions, 0 deletions
diff --git a/testlabel.lua b/testlabel.lua
new file mode 100644
index 0000000..100c0d0
--- /dev/null
+++ b/testlabel.lua
@@ -0,0 +1,422 @@
1local m = require 'lpeglabel'
2
3local p = m.T(1, 2, 5)
4assert(p:match("abc") == nil)
5
6-- throws a label that is not caught by ordinary choice
7p = m.T(1) + m.P"a"
8local r = p:match("abc")
9assert(r == nil)
10
11-- again throws a label that is not caught by ordinary choice
12local g = m.P{
13 "S",
14 S = m.V"A" + m.V"B",
15 A = m.T(1),
16 B = m.P"a"
17}
18r = g:match("abc")
19assert(r == nil)
20
21-- throws a label that is not caught by labeled choice
22p = m.Lc(m.T(2), m.P"a", 1, 3)
23r = p:match("abc")
24assert(r == nil)
25
26-- modifies previous pattern
27-- adds another labeled choice to catch label "2"
28p = m.Lc(p, m.P"a", 2)
29r = p:match("abc")
30assert(r == 2)
31
32-- throws a label that is caught by labeled choice
33p = m.Lc(m.T(25), m.P"a", 25)
34r = p:match("abc")
35assert(r == 2)
36assert(p:match("bola") == nil)
37
38-- labeled choice did not catch "fail" by default
39p = m.Lc(m.P"b", m.P"a", 1)
40r = p:match("abc")
41assert(r == nil, r)
42assert(p:match("bola") == 2)
43
44-- "fail" is label "0"
45-- labeled choice can catch "fail"
46p = m.Lc(m.P"b", m.P"a", 0)
47r = p:match("abc")
48assert(r == 2, r)
49assert(p:match("bola") == 2)
50
51-- "fail" is label "0"
52-- labeled choice can catch "fail" or "3"
53p = m.Lc(m.P"a" * m.T(3), (m.P"a" + m.P"b"), 0, 3)
54assert(p:match("abc") == 2)
55assert(p:match("bac") == 2)
56assert(p:match("cab") == nil)
57
58--[[
59S -> A /{1} 'a'
60A -> B
61B -> %1
62]]
63g = m.P{
64 "S",
65 S = m.Lc(m.V"A", m.P"a", 1),
66 A = m.V"B",
67 B = m.T(1),
68}
69assert(g:match("ab") == 2)
70assert(g:match("bc") == nil)
71
72
73--[[
74S -> A
75A -> (B (';' / %{1}))*
76B -> 'a'
77]]
78g = m.P{
79 "S",
80 S = m.V"A",
81 A = m.P(m.V"B" * (";" + m.T(1)))^0,
82 B = m.P'a',
83}
84assert(g:match("a;a;") == 5)
85assert(g:match("a;a") == nil)
86
87
88
89-- %1 /{1,3} %2 /{2} 'a'
90p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2)
91r = p:match("abc")
92assert(r == 2)
93assert(p:match("") == nil)
94
95p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3)
96r = p:match("abc")
97assert(r == 2)
98assert(p:match("") == nil)
99
100print("+")
101
102--[[ grammar based on Figure 8 of paper submitted to SCP
103S -> S0 /{1} ID /{2} ID '=' Exp /{3} 'unsigned'* 'int' ID /{4} 'unsigned'* ID ID / %error
104S0 -> ID S1 / 'unsigned' S2 / 'int' %3
105S1 -> '=' %2 / !. %1 / ID %4
106S2 -> 'unsigned' S2 / ID %4 / 'int' %3
107]]
108
109local sp = m.S" \t\n"^0
110local eq = sp * m.P"="
111
112g = m.P{
113 "S",
114 S = m.Lc(
115 m.Lc(
116 m.Lc(
117 m.Lc(m.V"S0", m.V"ID" * (m.P(1) + ""), 1),
118 m.V"ID" * eq * m.V"Exp", 2
119 ),
120 m.V"U"^0 * m.V"I" * m.V"ID", 3
121 ),
122 m.V"U"^0 * m.V"ID" * m.V"ID", 4)
123 + m.T(5),
124 S0 = m.V"ID" * m.V"S1" + m.V"U" * m.V"S2" + m.V"I" * m.T(3),
125 S1 = eq * m.T(2) + sp * -m.P(1) * m.T(1) + m.V"ID" * m.T(4),
126 S2 = m.V"U" * m.V"S2" + m.V"ID" * m.T(4) + m.V"I" * m.T(3),
127 ID = sp * m.P"a",
128 U = sp * m.P"unsigned",
129 I = sp * m.P"int",
130 Exp = sp * m.P"E",
131}
132--g:pcode()
133
134local s = "a"
135assert(g:match(s) == #s + 1) --1
136s = "a = E"
137assert(g:match(s) == #s + 1) --2
138s = "int a"
139assert(g:match(s) == #s + 1) --3
140s = "unsigned int a"
141assert(g:match(s) == #s + 1) --3
142s = "unsigned a a"
143assert(g:match(s) == #s + 1) --4
144s = "b"
145assert(g:match(s) == nil)
146s = "unsigned"
147assert(g:match(s) == nil)
148s = "unsigned a"
149assert(g:match(s) == nil)
150s = "unsigned int"
151assert(g:match(s) == nil)
152
153
154print("+")
155
156local re = require 're'
157
158g = re.compile[['a' /{4,9} [a-z]
159]]
160assert(g:match("a") == 2)
161assert(g:match("b") == nil)
162
163g = re.compile[['a' /{4,9} [a-f] /{5, 7} [a-z]
164]]
165assert(g:match("a") == 2)
166assert(g:match("b") == nil)
167
168g = re.compile[[%{1} /{4,9} [a-z]
169]]
170assert(g:match("a") == nil)
171
172g = re.compile[[%{1} /{4,1} [a-f]
173]]
174assert(g:match("a") == 2)
175assert(g:match("h") == nil)
176
177g = re.compile[[[a-f]%{15, 9} /{4,9} [a-c]%{7} /{5, 7} [a-z] ]]
178assert(g:match("a") == 2)
179assert(g:match("c") == 2)
180assert(g:match("d") == nil)
181assert(g:match("g") == nil)
182
183--[[ grammar based on Figure 8 of paper submitted to SCP
184S -> S0 /{1} ID /{2} ID '=' Exp /{3} 'unsigned'* 'int' ID /{4} 'unsigned'* ID ID / %error
185S0 -> ID S1 / 'unsigned' S2 / 'int' %3
186S1 -> '=' %2 / !. %1 / ID %4
187S2 -> 'unsigned' S2 / ID %4 / 'int' %3
188]]
189
190
191g = re.compile([[
192 S <- S0 /{1} ID /{2} ID %s* '=' Exp /{3} U* Int ID /{4} U ID ID /{0} %{5}
193 S0 <- ID S1 / U S2 / Int %{3}
194 S1 <- %s* '=' %{2} / !. %{1} / ID %{4}
195 S2 <- U S2 / ID %{4} / Int %{3}
196 ID <- %s* 'a'
197 U <- %s* 'unsigned'
198 Int <- %s* 'int'
199 Exp <- %s* 'E'
200]])
201
202local s = "a"
203assert(g:match(s) == #s + 1) --1
204s = "a = E"
205assert(g:match(s) == #s + 1) --2
206s = "int a"
207assert(g:match(s) == #s + 1) --3
208s = "unsigned int a"
209assert(g:match(s) == #s + 1) --3
210s = "unsigned a a"
211assert(g:match(s) == #s + 1) --4
212s = "b"
213assert(g:match(s) == nil)
214s = "unsigned"
215assert(g:match(s) == nil)
216s = "unsigned a"
217assert(g:match(s) == nil)
218s = "unsigned int"
219assert(g:match(s) == nil)
220
221local terror = { ['cmdSeq'] = "Missing ';' in CmdSeq",
222 ['ifExp'] = "Error in expresion of 'if'",
223 ['ifThen'] = "Error matching 'then' keyword",
224 ['ifThenCmdSeq'] = "Error matching CmdSeq of 'then' branch",
225 ['ifElseCmdSeq'] = "Error matching CmdSeq of 'else' branch",
226 ['ifEnd'] = "Error matching 'end' keyword of 'if'",
227 ['repeatCmdSeq'] = "Error matching CmdSeq of 'repeat'",
228 ['repeatUntil'] = "Error matching 'until' keyword",
229 ['repeatExp'] = "Error matching expression of 'until'",
230 ['assignOp'] = "Error matching ':='",
231 ['assignExp'] = "Error matching expression of assignment",
232 ['readName'] = "Error matching 'NAME' after 'read'",
233 ['writeExp'] = "Error matching expression after 'write'",
234 ['simpleExp'] = "Error matching 'SimpleExp'",
235 ['term'] = "Error matching 'Term'",
236 ['factor'] = "Error matching 'Factor'",
237 ['openParExp'] = "Error matching expression after '('",
238 ['closePar'] = "Error matching ')'",
239 ['undefined'] = "Error undefined'"}
240
241g = re.compile([[
242 Tiny <- CmdSeq /{1} '' -> cmdSeq /{2} '' -> ifExp /{3} '' -> ifThen /{4} '' -> ifThenCmdSeq
243 /{5} '' -> ifElseCmdSeq /{6} '' -> ifEnd /{7} '' -> repeatCmdSeq
244 /{8} '' -> repeatUntil /{9} '' -> repeatExp /{10} '' -> assignOp
245 /{11} '' -> assignExp /{12} '' -> readName /{13} '' -> writeExp
246 /{14} '' -> simpleExp /{15} '' -> term /{16} '' -> factor
247 /{17} '' -> openParExp /{18} '' -> closePar /{0} '' -> undefined
248 CmdSeq <- (Cmd (SEMICOLON / %{1})) (Cmd (SEMICOLON / %{1}))*
249 Cmd <- IfCmd / RepeatCmd / ReadCmd / WriteCmd / AssignCmd
250 IfCmd <- IF (Exp / %{2}) (THEN / %{3}) (CmdSeq / %{4}) (ELSE (CmdSeq / %{5}) / '') (END / %{6})
251 RepeatCmd <- REPEAT (CmdSeq / %{7}) (UNTIL / %{8}) (Exp / %{9})
252 AssignCmd <- !RESERVED NAME (ASSIGNMENT / %{10}) (Exp / %{11})
253 ReadCmd <- READ (NAME / %{12})
254 WriteCmd <- WRITE (Exp / %{13})
255 Exp <- SimpleExp ((LESS / EQUAL) (SimpleExp / %{14}) / '')
256 SimpleExp <- Term ((ADD / SUB) (Term / %{15}))*
257 Term <- Factor ((MUL / DIV) (Factor / %{16}))*
258 Factor <- OPENPAR (Exp / %{17}) (CLOSEPAR / %{18}) / NUMBER / NAME
259 ADD <- Sp '+'
260 ASSIGNMENT <- Sp ':='
261 CLOSEPAR <- Sp ')'
262 DIV <- Sp '/'
263 IF <- Sp 'if'
264 ELSE <- Sp 'else'
265 END <- Sp 'end'
266 EQUAL <- Sp '='
267 LESS <- Sp '<'
268 MUL <- Sp '*'
269 NAME <- Sp [a-z]+
270 NUMBER <- Sp [0-9]+
271 OPENPAR <- Sp '('
272 READ <- Sp 'read'
273 REPEAT <- Sp 'repeat'
274 SEMICOLON <- Sp ';'
275 SUB <- Sp '-'
276 THEN <- Sp 'then'
277 UNTIL <- Sp 'until'
278 WRITE <- Sp 'write'
279 RESERVED <- IF / ELSE / END / READ / REPEAT / THEN / UNTIL / WRITE
280 Sp <- (%s / %nl)*
281]], terror)
282
283s = [[
284n := 5;]]
285assert(g:match(s) == #s + 1)
286
287s = [[
288n := 5;
289f := 1;
290repeat
291 f := f * n;
292 n := n - 1;
293until (n < 1);
294write f;]]
295assert(g:match(s) == #s + 1)
296
297-- a ';' is missing in 'read a'
298s = [[
299read a]]
300assert(g:match(s) == terror['cmdSeq'])
301
302
303-- a ';' is missing in 'n := n - 1'
304s = [[
305n := 5;
306f := 1;
307repeat
308 f := f * n;
309 n := n - 1
310until (n < 1);
311write f;]]
312assert(g:match(s) == terror['cmdSeq'])
313
314
315-- IF expression
316s = [[
317if a then a := a + 1; end;]]
318assert(g:match(s) == #s + 1)
319
320-- IF expression
321s = [[
322if a then a := a + 1; else write 2; end;]]
323assert(g:match(s) == #s + 1)
324
325-- Error in expression of 'if'. 'A' is not a valida name
326s = [[
327if A then a := a + 1; else write 2; end;]]
328assert(g:match(s) == terror['ifExp'])
329
330-- Error matching the 'then' keyword
331s = [[
332if a a := a + 1; else write 2; end;]]
333assert(g:match(s) == terror['ifThen'])
334
335-- Error matching the CmdSeq inside of 'then' branch
336s = [[
337if a then 3 := 2; else write 2; end;]]
338assert(g:match(s) == terror['ifThenCmdSeq'])
339
340-- Error matching the CmdSeq inside of 'else' branch
341s = [[
342if a then b := 2; else A := 2; end;]]
343assert(g:match(s) == terror['ifElseCmdSeq'])
344
345-- Error matching 'end' of 'if'
346s = [[
347if a then b := 2; else a := 2; 77;]]
348assert(g:match(s) == terror['ifEnd'])
349
350-- Error matching the CmdSeq of 'repeat'
351s = [[repeat
352 F := f * n;
353 n := n - 1;
354until (n < 1);]]
355assert(g:match(s) == terror['repeatCmdSeq'])
356
357-- Error matching 'until'
358s = [[repeat
359 f := f * n;
360 n := n - 1;
36188 (n < 1);]]
362assert(g:match(s) == terror['repeatUntil'])
363
364-- Error matching expression of 'until'
365s = [[repeat
366 f := f * n;
367 n := n - 1;
368until ; (n < 1);]]
369assert(g:match(s) == terror['repeatExp'])
370
371-- Error matching ':='
372s = [[
373f = f * n;]]
374assert(g:match(s) == terror['assignOp'])
375
376-- Error matching expression of assignment
377s = [[
378f := A * n;]]
379assert(g:match(s) == terror['assignExp'])
380
381-- Error matching 'name'
382s = [[
383read 2;]]
384assert(g:match(s) == terror['readName'])
385
386-- Error matching expression after 'write'
387s = [[
388write [a] := 2;]]
389assert(g:match(s) == terror['writeExp'])
390
391-- Error matching 'SimpleExp'
392s = [[
393a := a < A;]]
394assert(g:match(s) == terror['simpleExp'])
395
396-- Error matching 'Term'
397s = [[
398a := a + A;]]
399assert(g:match(s) == terror['term'])
400
401-- Error matching 'Factor'
402s = [[
403a := a * A;]]
404assert(g:match(s) == terror['factor'])
405
406-- Error matching expression after '('
407s = [[
408a := (A);]]
409assert(g:match(s) == terror['openParExp'])
410
411-- Error matching ')'
412s = [[
413a := (a];]]
414assert(g:match(s) == terror['closePar'])
415
416-- Error undefined
417s = [[
418A := a;]]
419assert(g:match(s) == terror['undefined'])
420
421
422print("OK")