aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--testlabel.lua123
1 files changed, 86 insertions, 37 deletions
diff --git a/testlabel.lua b/testlabel.lua
index 1962fbf..f7180a7 100644
--- a/testlabel.lua
+++ b/testlabel.lua
@@ -1,11 +1,17 @@
1local m = require 'lpeglabel' 1local m = require 'lpeglabel'
2 2
3local p = m.T(1, 2, 5) 3local p, r, l, s, serror
4assert(p:match("abc") == nil) 4
5-- throws a label
6p = m.T(1)
7s = "abc"
8r, l, serror = p:match(s)
9assert(r == nil and l == 1 and serror == "abc")
5 10
6-- throws a label that is not caught by ordinary choice 11-- throws a label that is not caught by ordinary choice
7p = m.T(1) + m.P"a" 12p = m.T(1) + m.P"a"
8assert(p:match("abc") == nil) 13r, l, serror = p:match(s)
14assert(r == nil and l == 1 and serror == "abc")
9 15
10-- again throws a label that is not caught by ordinary choice 16-- again throws a label that is not caught by ordinary choice
11local g = m.P{ 17local g = m.P{
@@ -14,28 +20,37 @@ local g = m.P{
14 A = m.T(1), 20 A = m.T(1),
15 B = m.P"a" 21 B = m.P"a"
16} 22}
17assert(g:match("abc") == nil) 23r, l, serror = g:match(s)
24assert(r == nil and l == 1 and serror == "abc")
18 25
19-- throws a label that is not caught by labeled choice 26-- throws a label that is not caught by labeled choice
20p = m.Lc(m.T(2), m.P"a", 1, 3) 27p = m.Lc(m.T(2), m.P"a", 1, 3)
21assert(p:match("abc") == nil) 28r, l, serror = p:match(s)
29assert(r == nil and l == 2 and serror == "abc")
22 30
23-- modifies previous pattern 31-- modifies previous pattern
24-- adds another labeled choice to catch label "2" 32-- adds another labeled choice to catch label "2"
25p = m.Lc(p, m.P"a", 2) 33p = m.Lc(p, m.P"a", 2)
26assert(p:match("abc") == 2) 34assert(p:match(s) == 2)
27 35
28-- throws a label that is caught by labeled choice 36-- throws a label that is caught by labeled choice
29p = m.Lc(m.T(25), m.P"a", 25) 37p = m.Lc(m.T(25), m.P"a", 25)
30assert(p:match("abc") == 2) 38assert(p:match(s) == 2)
31assert(p:match("bola") == nil) 39
40-- "fail" is label "0"
41-- throws the "fail" label that is not caught by the labeled choice
42s = "bola"
43r, l, serror = p:match("bola")
44assert(r == nil and l == 0 and serror == "bola")
32 45
33-- labeled choice does not catch "fail" by default 46-- labeled choice does not catch "fail" by default
34p = m.Lc(m.P"b", m.P"a", 1) 47p = m.Lc(m.P"b", m.P"a", 1)
35assert(p:match("abc") == nil) 48
49r, l, serror = p:match("abc")
50assert(r == nil and l == 0 and serror == "abc")
51
36assert(p:match("bola") == 2) 52assert(p:match("bola") == 2)
37 53
38-- "fail" is label "0"
39-- labeled choice can catch "fail" 54-- labeled choice can catch "fail"
40p = m.Lc(m.P"b", m.P"a", 0) 55p = m.Lc(m.P"b", m.P"a", 0)
41assert(p:match("abc") == 2) 56assert(p:match("abc") == 2)
@@ -46,14 +61,18 @@ assert(p:match("bola") == 2)
46p = m.Lc(m.P"a" * m.T(3), (m.P"a" + m.P"b"), 0, 3) 61p = m.Lc(m.P"a" * m.T(3), (m.P"a" + m.P"b"), 0, 3)
47assert(p:match("abc") == 2) 62assert(p:match("abc") == 2)
48assert(p:match("bac") == 2) 63assert(p:match("bac") == 2)
49assert(p:match("cab") == nil) 64
65r, l, serror = p:match("cab")
66assert(r == nil and l == 0 and serror == "cab")
50 67
51-- tests related to predicates 68-- tests related to predicates
52p = #m.T(1) + m.P"a" 69p = #m.T(1) + m.P"a"
53assert(p:match("abc") == nil) 70r, l, serror = p:match("abc")
71assert(r == nil and l == 1 and serror == "abc")
54 72
55p = ##m.T(1) + m.P"a" 73p = ##m.T(1) + m.P"a"
56assert(p:match("abc") == nil) 74r, l, serror = p:match("abc")
75assert(r == nil and l == 1 and serror == "abc")
57 76
58p = #m.T(0) * m.P"a" 77p = #m.T(0) * m.P"a"
59assert(p:match("abc") == fail) 78assert(p:match("abc") == fail)
@@ -62,10 +81,12 @@ p = #m.T(0) + m.P"a"
62assert(p:match("abc") == 2) 81assert(p:match("abc") == 2)
63 82
64p = -m.T(1) * m.P"a" 83p = -m.T(1) * m.P"a"
65assert(p:match("abc") == nil) 84r, l, serror = p:match("abc")
85assert(r == nil and l == 1 and serror == "abc")
66 86
67p = -(-m.T(1)) * m.P"a" 87p = -(-m.T(1)) * m.P"a"
68assert(p:match("abc") == nil) 88r, l, serror = p:match("abc")
89assert(r == nil and l == 1 and serror == "abc")
69 90
70p = -m.T(0) * m.P"a" 91p = -m.T(0) * m.P"a"
71assert(p:match("abc") == 2) 92assert(p:match("abc") == 2)
@@ -90,13 +111,15 @@ assert(p:match("abc") == 2)
90 111
91-- tests related to repetition 112-- tests related to repetition
92p = m.T(1)^0 113p = m.T(1)^0
93assert(p:match("ab") == nil) 114r, l, serror = p:match("ab")
115assert(r == nil and l == 1 and serror == "ab")
94 116
95p = m.T(0)^0 117p = m.T(0)^0
96assert(p:match("ab") == 1) 118assert(p:match("ab") == 1)
97 119
98p = (m.P"a" + m.T(1))^0 120p = (m.P"a" + m.T(1))^0
99assert(p:match("aa") == nil) 121r, l, serror = p:match("aa")
122assert(r == nil and l == 1 and serror == "")
100 123
101p = (m.P"a" + m.T(0))^0 124p = (m.P"a" + m.T(0))^0
102assert(p:match("aa") == 3) 125assert(p:match("aa") == 3)
@@ -123,7 +146,8 @@ g = m.P{
123 B = m.T(1), 146 B = m.T(1),
124} 147}
125assert(g:match("ab") == 2) 148assert(g:match("ab") == 2)
126assert(g:match("bc") == nil) 149r, l, serror = g:match("bc")
150assert(r == nil and l == 0 and serror == "bc")
127 151
128 152
129--[[ 153--[[
@@ -138,17 +162,23 @@ g = m.P{
138 B = m.P'a', 162 B = m.P'a',
139} 163}
140assert(g:match("a;a;") == 5) 164assert(g:match("a;a;") == 5)
141assert(g:match("a;a") == nil) 165
166r, l, serror = g:match("a;a")
167assert(r == nil and l == 1 and serror == "")
142 168
143 169
144-- %1 /{1,3} %2 /{2} 'a' 170-- %1 /{1,3} %2 /{2} 'a'
145p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2) 171p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2)
146assert(p:match("abc") == 2) 172assert(p:match("abc") == 2)
147assert(p:match("") == nil) 173
174r, l, serror = p:match("")
175assert(r == nil and l == 0 and serror == "")
148 176
149p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3) 177p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3)
150assert(p:match("abc") == 2) 178assert(p:match("abc") == 2)
151assert(p:match("") == nil) 179
180r, l, serror = p:match("")
181assert(r == nil and l == 0 and serror == "")
152 182
153print("+") 183print("+")
154 184
@@ -173,7 +203,7 @@ g = m.P{
173 m.V"U"^0 * m.V"I" * m.V"ID", 3 203 m.V"U"^0 * m.V"I" * m.V"ID", 3
174 ), 204 ),
175 m.V"U"^0 * m.V"ID" * m.V"ID", 4) 205 m.V"U"^0 * m.V"ID" * m.V"ID", 4)
176 + m.T(5), 206 + m.T(5), -- error
177 S0 = m.V"ID" * m.V"S1" + m.V"U" * m.V"S2" + m.V"I" * m.T(3), 207 S0 = m.V"ID" * m.V"S1" + m.V"U" * m.V"S2" + m.V"I" * m.T(3),
178 S1 = eq * m.T(2) + sp * -m.P(1) * m.T(1) + m.V"ID" * m.T(4), 208 S1 = eq * m.T(2) + sp * -m.P(1) * m.T(1) + m.V"ID" * m.T(4),
179 S2 = m.V"U" * m.V"S2" + m.V"ID" * m.T(4) + m.V"I" * m.T(3), 209 S2 = m.V"U" * m.V"S2" + m.V"ID" * m.T(4) + m.V"I" * m.T(3),
@@ -193,14 +223,22 @@ s = "unsigned int a"
193assert(g:match(s) == #s + 1) --3 223assert(g:match(s) == #s + 1) --3
194s = "unsigned a a" 224s = "unsigned a a"
195assert(g:match(s) == #s + 1) --4 225assert(g:match(s) == #s + 1) --4
226
196s = "b" 227s = "b"
197assert(g:match(s) == nil) 228r, l, serror = g:match(s)
229assert(r == nil and l == 5 and serror == "b")
230
198s = "unsigned" 231s = "unsigned"
199assert(g:match(s) == nil) 232r, l, serror = g:match(s)
233assert(r == nil and l == 5 and serror == s)
234
200s = "unsigned a" 235s = "unsigned a"
201assert(g:match(s) == nil) 236r, l, serror = g:match(s)
237assert(r == nil and l == 5 and serror == s)
238
202s = "unsigned int" 239s = "unsigned int"
203assert(g:match(s) == nil) 240r, l, serror = g:match(s)
241assert(r == nil and l == 5 and serror == s)
204 242
205 243
206print("+") 244print("+")
@@ -210,27 +248,34 @@ local re = require 'relabel'
210g = re.compile[['a' /{4,9} [a-z] 248g = re.compile[['a' /{4,9} [a-z]
211]] 249]]
212assert(g:match("a") == 2) 250assert(g:match("a") == 2)
213assert(g:match("b") == nil) 251r, l, serror = g:match("b")
252assert(r == nil and l == 0 and serror == "b")
214 253
215g = re.compile[['a' /{4,9} [a-f] /{5, 7} [a-z] 254g = re.compile[['a' /{4,9} [a-f] /{5, 7} [a-z]
216]] 255]]
217assert(g:match("a") == 2) 256assert(g:match("a") == 2)
218assert(g:match("b") == nil) 257r, l, serror = g:match("b")
258assert(r == nil and l == 0 and serror == "b")
219 259
220g = re.compile[[%{1} /{4,9} [a-z] 260g = re.compile[[%{1} /{4,9} [a-z]
221]] 261]]
222assert(g:match("a") == nil) 262r, l, serror = g:match("a")
263assert(r == nil and l == 1 and serror == "a")
264
223 265
224g = re.compile[[%{1} /{4,1} [a-f] 266g = re.compile[[%{1} /{4,1} [a-f]
225]] 267]]
226assert(g:match("a") == 2) 268assert(g:match("a") == 2)
227assert(g:match("h") == nil) 269r, l, serror = g:match("h")
270assert(r == nil and l == 0 and serror == "h")
228 271
229g = re.compile[[[a-f]%{15, 9} /{4,9} [a-c]%{7} /{5, 7} [a-z] ]] 272g = re.compile[[[a-f]%{9} /{4,9} [a-c]%{7} /{5, 7} [a-z] ]]
230assert(g:match("a") == 2) 273assert(g:match("a") == 2)
231assert(g:match("c") == 2) 274assert(g:match("c") == 2)
232assert(g:match("d") == nil) 275r, l, serror = g:match("d")
233assert(g:match("g") == nil) 276assert(r == nil and l == 0 and serror == "d")
277r, l, serror = g:match("g")
278assert(r == nil and l == 0 and serror == "g")
234 279
235--[[ grammar based on Figure 8 of paper submitted to SCP 280--[[ grammar based on Figure 8 of paper submitted to SCP
236S -> S0 /{1} ID /{2} ID '=' Exp /{3} 'unsigned'* 'int' ID /{4} 'unsigned'* ID ID / %error 281S -> S0 /{1} ID /{2} ID '=' Exp /{3} 'unsigned'* 'int' ID /{4} 'unsigned'* ID ID / %error
@@ -262,13 +307,17 @@ assert(g:match(s) == #s + 1) --3
262s = "unsigned a a" 307s = "unsigned a a"
263assert(g:match(s) == #s + 1) --4 308assert(g:match(s) == #s + 1) --4
264s = "b" 309s = "b"
265assert(g:match(s) == nil) 310r, l, serror = g:match(s)
311assert(r == nil and l == 5 and serror == s)
266s = "unsigned" 312s = "unsigned"
267assert(g:match(s) == nil) 313r, l, serror = g:match(s)
314assert(r == nil and l == 5 and serror == s)
268s = "unsigned a" 315s = "unsigned a"
269assert(g:match(s) == nil) 316r, l, serror = g:match(s)
317assert(r == nil and l == 5 and serror == s)
270s = "unsigned int" 318s = "unsigned int"
271assert(g:match(s) == nil) 319r, l, serror = g:match(s)
320assert(r == nil and l == 5 and serror == s)
272 321
273local terror = { ['cmdSeq'] = "Missing ';' in CmdSeq", 322local terror = { ['cmdSeq'] = "Missing ';' in CmdSeq",
274 ['ifExp'] = "Error in expresion of 'if'", 323 ['ifExp'] = "Error in expresion of 'if'",