aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSergio Queiroz <sqmedeiros@gmail.com>2017-07-07 10:21:53 -0300
committerSergio Queiroz <sqmedeiros@gmail.com>2017-07-07 10:21:53 -0300
commitcf75331b7163d7a1fa72b3032327bda01489b944 (patch)
tree8a29aeb124e2f182bef61e33e1bd46f168cf15d9
parent5ed66d31f0245abf1209fb2cc6161910aca211ad (diff)
downloadlpeglabel-cf75331b7163d7a1fa72b3032327bda01489b944.tar.gz
lpeglabel-cf75331b7163d7a1fa72b3032327bda01489b944.tar.bz2
lpeglabel-cf75331b7163d7a1fa72b3032327bda01489b944.zip
Adding tests related to labeled choice
-rw-r--r--testlabel.lua252
1 files changed, 234 insertions, 18 deletions
diff --git a/testlabel.lua b/testlabel.lua
index e2833ea..a8439e4 100644
--- a/testlabel.lua
+++ b/testlabel.lua
@@ -246,6 +246,27 @@ p = m.Rec(#(m.P("a") * m.T(22)), m.T(15), 22)
246r, l, serror = p:match("abc") 246r, l, serror = p:match("abc")
247assert(r == nil and l == 15 and serror == "bc") 247assert(r == nil and l == 15 and serror == "bc")
248 248
249p = m.Lc(#m.T(22), m.P"a", 22)
250assert(p:match("abc") == 2)
251
252p = #m.Lc(m.T(22), m.P"a", 22)
253assert(p:match("abc") == 1)
254
255p = m.Lc(m.T(22), #m.P"a", 22)
256assert(p:match("abc") == 1)
257
258p = m.Lc(#m.T(22), m.P"a", 22)
259r, l, serror = p:match("bbc")
260assert(r == nil and l == 0 and serror == "bbc")
261
262p = m.Lc(#m.P("a") * m.T(22), m.T(15), 22)
263r, l, serror = p:match("abc")
264assert(r == nil and l == 15 and serror == "abc")
265
266p = m.Lc(#(m.P("a") * m.T(22)), m.T(15), 22)
267r, l, serror = p:match("abc")
268assert(r == nil and l == 15 and serror == "abc")
269
249 270
250 271
251-- tests related to repetition 272-- tests related to repetition
@@ -260,7 +281,9 @@ assert(r == nil and l == 1 and serror == "")
260 281
261-- Bug reported by Matthew Allen 282-- Bug reported by Matthew Allen
262-- some optmizations performed by LPeg should not be 283-- some optmizations performed by LPeg should not be
263-- applied in case of labeled choices 284-- applied in case of labels
285
286-- recovery operator
264p = m.Rec(m.P"A", m.P(true), 1) + m.P("B") 287p = m.Rec(m.P"A", m.P(true), 1) + m.P("B")
265assert(p:match("B") == 2) 288assert(p:match("B") == 2)
266 289
@@ -268,6 +291,14 @@ p = m.Rec(m.P"A", m.P(false), 1) + m.P("B")
268assert(p:match("B") == 2) 291assert(p:match("B") == 2)
269 292
270 293
294-- labeled choices
295p = m.Lc(m.P"A", m.P(true), 1) + m.P("B")
296assert(p:match("B") == 2)
297
298p = m.Lc(m.P"A", m.P(false), 1) + m.P("B")
299assert(p:match("B") == 2)
300
301
271--[[ 302--[[
272S -> A //{1} 'a' 303S -> A //{1} 'a'
273A -> B 304A -> B
@@ -314,6 +345,53 @@ assert(p:match("abc") == 2)
314r, l, serror = p:match("") 345r, l, serror = p:match("")
315assert(r == nil and l == 0 and serror == "") 346assert(r == nil and l == 0 and serror == "")
316 347
348-- labeled choice
349--[[
350S -> A /{1} 'a'
351A -> B
352B -> %1
353]]
354g = m.P{
355 "S",
356 S = m.Lc(m.V"A", m.P"a", 1),
357 A = m.V"B",
358 B = m.T(1),
359}
360assert(g:match("ab") == 2)
361r, l, serror = g:match("bc")
362assert(r == nil and l == 0 and serror == "bc")
363
364
365--[[
366S -> A
367A -> (B (';' / %{1}))*
368B -> 'a'
369]]
370g = m.P{
371 "S",
372 S = m.V"A",
373 A = m.P(m.V"B" * (";" + m.T(1)))^0,
374 B = m.P'a',
375}
376assert(g:match("a;a;") == 5)
377
378r, l, serror = g:match("a;a")
379assert(r == nil and l == 1 and serror == "")
380
381
382-- %1 /{1,3} %2 /{2} 'a'
383p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2)
384assert(p:match("abc") == 2)
385
386r, l, serror = p:match("")
387assert(r == nil and l == 0 and serror == "")
388
389p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3)
390assert(p:match("abc") == 2)
391
392r, l, serror = p:match("")
393assert(r == nil and l == 0 and serror == "")
394
317 395
318-- Infinte Loop TODO: check the semantics 396-- Infinte Loop TODO: check the semantics
319-- %1 //{1} %1 397-- %1 //{1} %1
@@ -345,6 +423,15 @@ assert(r == false)
345local r = pcall(m.Rec, m.P"b", m.P"a", -1) 423local r = pcall(m.Rec, m.P"b", m.P"a", -1)
346assert(r == false) 424assert(r == false)
347 425
426local r = pcall(m.Lc, m.P"b", m.P"a", 0)
427assert(r == false)
428
429local r = pcall(m.Lc, m.P"b", m.P"a", 256)
430assert(r == false)
431
432local r = pcall(m.Lc, m.P"b", m.P"a", -1)
433assert(r == false)
434
348local r = pcall(m.T, 0) 435local r = pcall(m.T, 0)
349assert(r == false) 436assert(r == false)
350 437
@@ -468,7 +555,7 @@ r, l, serror = g:match("g")
468assert(r == nil and l == 0 and serror == "g") 555assert(r == nil and l == 0 and serror == "g")
469 556
470 557
471--[[ grammar based on Figure 8 of paper submitted to SCP 558--[[ grammar based on Figure 8 of paper submitted to SCP (using the recovery operator)
472S -> S0 //{1} ID //{2} ID '=' Exp //{3} 'unsigned'* 'int' ID //{4} 'unsigned'* ID ID / %error 559S -> S0 //{1} ID //{2} ID '=' Exp //{3} 'unsigned'* 'int' ID //{4} 'unsigned'* ID ID / %error
473S0 -> S1 / S2 / &'int' %3 560S0 -> S1 / S2 / &'int' %3
474S1 -> &(ID '=') %2 / &(ID !.) %1 / &ID %4 561S1 -> &(ID '=') %2 / &(ID !.) %1 / &ID %4
@@ -510,6 +597,135 @@ r, l, serror = g:match(s)
510assert(r == nil and l == 5 and serror == s) 597assert(r == nil and l == 5 and serror == s)
511 598
512 599
600------------------------------------------
601-- Tests related to labeled ordered choice
602------------------------------------------
603
604-- throws a label that is not caught by labeled choice
605s = "abc"
606p = m.Lc(m.T(2), m.P"a", 1, 3)
607r, l, serror = p:match(s)
608assert(r == nil and l == 2 and serror == "abc")
609
610-- modifies previous pattern
611-- adds another labeled choice to catch label "2"
612p = m.Lc(p, m.P"a", 2)
613assert(p:match(s) == 2)
614
615-- throws a label that is caught by labeled choice
616p = m.Lc(m.T(25), m.P"a", 25)
617assert(p:match(s) == 2)
618
619-- "fail" is label "0"
620-- throws the "fail" label that is not caught by the labeled choice
621s = "bola"
622r, l, serror = p:match("bola")
623assert(r == nil and l == 0 and serror == "bola")
624
625-- labeled choice does not catch "fail"
626p = m.Lc(m.P"b", m.P"a", 1)
627
628r, l, serror = p:match("abc")
629assert(r == nil and l == 0 and serror == "abc")
630assert(p:match("bola") == 2)
631
632-- labeled choice catches "1" or "3"
633p = m.Lc(-m.P"a" * m.T(1) + m.P"a" * m.T(3), m.P"a" + m.P"b", 1, 3)
634assert(p:match("abc") == 2)
635assert(p:match("bac") == 2)
636
637-- associativity
638-- (p1 / %1) /{1} (p2 / %2) /{2} p3
639-- left-associativity
640-- ("a" /{1} "b") /{2} "c"
641p = m.Lc(m.Lc(m.P"a" + m.T(1), m.P"b" + m.T(2), 1), m.P"c", 2)
642assert(p:match("abc") == 2)
643assert(p:match("bac") == 2)
644assert(p:match("cab") == 2)
645r, l, serror = p:match("dab")
646assert(r == nil and l == 0 and serror == "dab")
647
648
649-- righ-associativity
650-- "a" /{1} ("b" /{2} "c")
651p = m.Lc(m.P"a" + m.T(1), m.Lc(m.P"b" + m.T(2), m.P"c", 2), 1)
652assert(p:match("abc") == 2)
653assert(p:match("bac") == 2)
654assert(p:match("cab") == 2)
655r, l, serror = p:match("dab")
656assert(r == nil and l == 0 and serror == "dab")
657
658
659-- associativity -> in this case the error thrown by p1 is only
660-- recovered when we have a left-associative operator
661-- (p1 / %2) /{1} (p2 / %2) /{2} p3
662-- left-associativity
663-- ("a" /{1} "b") /{2} "c"
664p = m.Lc(m.Lc(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2)
665assert(p:match("abc") == 2)
666r, l, serror = p:match("bac")
667assert(r == nil and l == 0 and serror == "bac")
668assert(p:match("cab") == 2)
669r, l, serror = p:match("dab")
670assert(r == nil and l == 0 and serror == "dab")
671
672
673-- righ-associativity
674-- "a" /{1} ("b" /{2} "c")
675p = m.Lc(m.P"a" + m.T(2), m.Lc(m.P"b" + m.T(2), m.P"c", 2), 1)
676assert(p:match("abc") == 2)
677r, l, serror = p:match("bac")
678assert(r == nil and l == 2 and serror == "bac")
679r, l, serror = p:match("cab")
680assert(r == nil and l == 2 and serror == "cab")
681r, l, serror = p:match("dab")
682assert(r == nil and l == 2 and serror == "dab")
683
684
685
686--[[ grammar based on Figure 8 of paper submitted to SCP (using labeled choice)
687S -> S0 /{1} ID /{2} ID '=' Exp /{3} 'unsigned'* 'int' ID /{4} 'unsigned'* ID ID / %error
688S0 -> ID S1 / 'unsigned' S2 / 'int' %3
689S1 -> '=' %2 / !. %1 / ID %4
690S2 -> 'unsigned' S2 / ID %4 / 'int' %3
691]]
692
693
694g = re.compile([[
695 S <- S0 /{1} ID /{2} ID %s* '=' Exp /{3} U* Int ID /{4} U ID ID / %{5}
696 S0 <- ID S1 / U S2 / Int %{3}
697 S1 <- %s* '=' %{2} / !. %{1} / ID %{4}
698 S2 <- U S2 / ID %{4} / Int %{3}
699 ID <- %s* 'a'
700 U <- %s* 'unsigned'
701 Int <- %s* 'int'
702 Exp <- %s* 'E'
703]])
704
705local s = "a"
706assert(g:match(s) == #s + 1) --1
707s = "a = E"
708assert(g:match(s) == #s + 1) --2
709s = "int a"
710assert(g:match(s) == #s + 1) --3
711s = "unsigned int a"
712assert(g:match(s) == #s + 1) --3
713s = "unsigned a a"
714assert(g:match(s) == #s + 1) --4
715s = "b"
716r, l, serror = g:match(s)
717assert(r == nil and l == 5 and serror == s)
718s = "unsigned"
719r, l, serror = g:match(s)
720assert(r == nil and l == 5 and serror == s)
721s = "unsigned a"
722r, l, serror = g:match(s)
723assert(r == nil and l == 5 and serror == s)
724s = "unsigned int"
725r, l, serror = g:match(s)
726assert(r == nil and l == 5 and serror == s)
727
728
513 729
514local terror = { ['cmdSeq'] = "Missing ';' in CmdSeq", 730local terror = { ['cmdSeq'] = "Missing ';' in CmdSeq",
515 ['ifExp'] = "Error in expresion of 'if'", 731 ['ifExp'] = "Error in expresion of 'if'",
@@ -861,29 +1077,29 @@ local function eval(input)
861 end 1077 end
862end 1078end
863 1079
864print(eval "98-76*(54/32)") 1080assert(eval "98-76*(54/32)" == 37.125)
865--> 37.125 1081--> 37.125
866 1082
867print(eval "(1+1-1*2/2") 1083local e, msg = eval "(1+1-1*2/2"
868--> syntax error: missing a closing ')' after the expression (at index 11) 1084assert(e == nil and msg == "syntax error: missing a closing ')' after the expression (at index 11)")
869 1085
870print(eval "(1+)-1*(2/2)") 1086e, msg = eval "(1+)-1*(2/2)"
871--> syntax error: expected a term after the operator (at index 4) 1087assert(e == nil and msg == "syntax error: expected a term after the operator (at index 4)")
872 1088
873print(eval "(1+1)-1*(/2)") 1089e, msg = eval "(1+1)-1*(/2)"
874--> syntax error: expected an expression after the parenthesis (at index 10) 1090assert(e == nil and msg == "syntax error: expected an expression after the parenthesis (at index 10)")
875 1091
876print(eval "1+(1-(1*2))/2x") 1092e, msg = eval "1+(1-(1*2))/2x"
877--> syntax error: extra chracters found after the expression (at index 14) 1093assert(e == nil and msg == "syntax error: extra characters found after the expression (at index 14)")
878 1094
879print(eval "-1+(1-(1*2))/2") 1095e, msg = eval "-1+(1-(1*2))/2"
880--> syntax error: no expression found (at index 1) 1096assert(e == nil and msg == "syntax error: no expression found (at index 1)")
881 1097
882print(eval "(1+1-1*(2/2+)-():") 1098e, msg = eval "(1+1-1*(2/2+)-():"
883--> syntax error: expected a term after the operator (at index 13) 1099assert(e == nil and msg == "syntax error: expected a term after the operator (at index 13)\n" ..
884--> syntax error: expected an expression after the parenthesis (at index 16) 1100 "syntax error: expected an expression after the parenthesis (at index 16)\n" ..
885--> syntax error: missing a closing ')' after the expression (at index 17) 1101 "syntax error: missing a closing ')' after the expression (at index 17)\n" ..
886--> syntax error: extra characters found after the expression (at index 1102 "syntax error: extra characters found after the expression (at index 17)")
887 1103
888 1104
889print("+") 1105print("+")