diff options
author | Sergio Queiroz <sqmedeiros@gmail.com> | 2017-07-07 10:21:53 -0300 |
---|---|---|
committer | Sergio Queiroz <sqmedeiros@gmail.com> | 2017-07-07 10:21:53 -0300 |
commit | cf75331b7163d7a1fa72b3032327bda01489b944 (patch) | |
tree | 8a29aeb124e2f182bef61e33e1bd46f168cf15d9 | |
parent | 5ed66d31f0245abf1209fb2cc6161910aca211ad (diff) | |
download | lpeglabel-cf75331b7163d7a1fa72b3032327bda01489b944.tar.gz lpeglabel-cf75331b7163d7a1fa72b3032327bda01489b944.tar.bz2 lpeglabel-cf75331b7163d7a1fa72b3032327bda01489b944.zip |
Adding tests related to labeled choice
-rw-r--r-- | testlabel.lua | 252 |
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) | |||
246 | r, l, serror = p:match("abc") | 246 | r, l, serror = p:match("abc") |
247 | assert(r == nil and l == 15 and serror == "bc") | 247 | assert(r == nil and l == 15 and serror == "bc") |
248 | 248 | ||
249 | p = m.Lc(#m.T(22), m.P"a", 22) | ||
250 | assert(p:match("abc") == 2) | ||
251 | |||
252 | p = #m.Lc(m.T(22), m.P"a", 22) | ||
253 | assert(p:match("abc") == 1) | ||
254 | |||
255 | p = m.Lc(m.T(22), #m.P"a", 22) | ||
256 | assert(p:match("abc") == 1) | ||
257 | |||
258 | p = m.Lc(#m.T(22), m.P"a", 22) | ||
259 | r, l, serror = p:match("bbc") | ||
260 | assert(r == nil and l == 0 and serror == "bbc") | ||
261 | |||
262 | p = m.Lc(#m.P("a") * m.T(22), m.T(15), 22) | ||
263 | r, l, serror = p:match("abc") | ||
264 | assert(r == nil and l == 15 and serror == "abc") | ||
265 | |||
266 | p = m.Lc(#(m.P("a") * m.T(22)), m.T(15), 22) | ||
267 | r, l, serror = p:match("abc") | ||
268 | assert(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 | ||
264 | p = m.Rec(m.P"A", m.P(true), 1) + m.P("B") | 287 | p = m.Rec(m.P"A", m.P(true), 1) + m.P("B") |
265 | assert(p:match("B") == 2) | 288 | assert(p:match("B") == 2) |
266 | 289 | ||
@@ -268,6 +291,14 @@ p = m.Rec(m.P"A", m.P(false), 1) + m.P("B") | |||
268 | assert(p:match("B") == 2) | 291 | assert(p:match("B") == 2) |
269 | 292 | ||
270 | 293 | ||
294 | -- labeled choices | ||
295 | p = m.Lc(m.P"A", m.P(true), 1) + m.P("B") | ||
296 | assert(p:match("B") == 2) | ||
297 | |||
298 | p = m.Lc(m.P"A", m.P(false), 1) + m.P("B") | ||
299 | assert(p:match("B") == 2) | ||
300 | |||
301 | |||
271 | --[[ | 302 | --[[ |
272 | S -> A //{1} 'a' | 303 | S -> A //{1} 'a' |
273 | A -> B | 304 | A -> B |
@@ -314,6 +345,53 @@ assert(p:match("abc") == 2) | |||
314 | r, l, serror = p:match("") | 345 | r, l, serror = p:match("") |
315 | assert(r == nil and l == 0 and serror == "") | 346 | assert(r == nil and l == 0 and serror == "") |
316 | 347 | ||
348 | -- labeled choice | ||
349 | --[[ | ||
350 | S -> A /{1} 'a' | ||
351 | A -> B | ||
352 | B -> %1 | ||
353 | ]] | ||
354 | g = 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 | } | ||
360 | assert(g:match("ab") == 2) | ||
361 | r, l, serror = g:match("bc") | ||
362 | assert(r == nil and l == 0 and serror == "bc") | ||
363 | |||
364 | |||
365 | --[[ | ||
366 | S -> A | ||
367 | A -> (B (';' / %{1}))* | ||
368 | B -> 'a' | ||
369 | ]] | ||
370 | g = 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 | } | ||
376 | assert(g:match("a;a;") == 5) | ||
377 | |||
378 | r, l, serror = g:match("a;a") | ||
379 | assert(r == nil and l == 1 and serror == "") | ||
380 | |||
381 | |||
382 | -- %1 /{1,3} %2 /{2} 'a' | ||
383 | p = m.Lc(m.Lc(m.T(1), m.T(2), 1, 3), m.P"a", 2) | ||
384 | assert(p:match("abc") == 2) | ||
385 | |||
386 | r, l, serror = p:match("") | ||
387 | assert(r == nil and l == 0 and serror == "") | ||
388 | |||
389 | p = m.Lc(m.T(1), m.Lc(m.T(2), m.P"a", 2), 1, 3) | ||
390 | assert(p:match("abc") == 2) | ||
391 | |||
392 | r, l, serror = p:match("") | ||
393 | assert(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) | |||
345 | local r = pcall(m.Rec, m.P"b", m.P"a", -1) | 423 | local r = pcall(m.Rec, m.P"b", m.P"a", -1) |
346 | assert(r == false) | 424 | assert(r == false) |
347 | 425 | ||
426 | local r = pcall(m.Lc, m.P"b", m.P"a", 0) | ||
427 | assert(r == false) | ||
428 | |||
429 | local r = pcall(m.Lc, m.P"b", m.P"a", 256) | ||
430 | assert(r == false) | ||
431 | |||
432 | local r = pcall(m.Lc, m.P"b", m.P"a", -1) | ||
433 | assert(r == false) | ||
434 | |||
348 | local r = pcall(m.T, 0) | 435 | local r = pcall(m.T, 0) |
349 | assert(r == false) | 436 | assert(r == false) |
350 | 437 | ||
@@ -468,7 +555,7 @@ r, l, serror = g:match("g") | |||
468 | assert(r == nil and l == 0 and serror == "g") | 555 | assert(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) |
472 | S -> S0 //{1} ID //{2} ID '=' Exp //{3} 'unsigned'* 'int' ID //{4} 'unsigned'* ID ID / %error | 559 | S -> S0 //{1} ID //{2} ID '=' Exp //{3} 'unsigned'* 'int' ID //{4} 'unsigned'* ID ID / %error |
473 | S0 -> S1 / S2 / &'int' %3 | 560 | S0 -> S1 / S2 / &'int' %3 |
474 | S1 -> &(ID '=') %2 / &(ID !.) %1 / &ID %4 | 561 | S1 -> &(ID '=') %2 / &(ID !.) %1 / &ID %4 |
@@ -510,6 +597,135 @@ r, l, serror = g:match(s) | |||
510 | assert(r == nil and l == 5 and serror == s) | 597 | assert(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 | ||
605 | s = "abc" | ||
606 | p = m.Lc(m.T(2), m.P"a", 1, 3) | ||
607 | r, l, serror = p:match(s) | ||
608 | assert(r == nil and l == 2 and serror == "abc") | ||
609 | |||
610 | -- modifies previous pattern | ||
611 | -- adds another labeled choice to catch label "2" | ||
612 | p = m.Lc(p, m.P"a", 2) | ||
613 | assert(p:match(s) == 2) | ||
614 | |||
615 | -- throws a label that is caught by labeled choice | ||
616 | p = m.Lc(m.T(25), m.P"a", 25) | ||
617 | assert(p:match(s) == 2) | ||
618 | |||
619 | -- "fail" is label "0" | ||
620 | -- throws the "fail" label that is not caught by the labeled choice | ||
621 | s = "bola" | ||
622 | r, l, serror = p:match("bola") | ||
623 | assert(r == nil and l == 0 and serror == "bola") | ||
624 | |||
625 | -- labeled choice does not catch "fail" | ||
626 | p = m.Lc(m.P"b", m.P"a", 1) | ||
627 | |||
628 | r, l, serror = p:match("abc") | ||
629 | assert(r == nil and l == 0 and serror == "abc") | ||
630 | assert(p:match("bola") == 2) | ||
631 | |||
632 | -- labeled choice catches "1" or "3" | ||
633 | p = m.Lc(-m.P"a" * m.T(1) + m.P"a" * m.T(3), m.P"a" + m.P"b", 1, 3) | ||
634 | assert(p:match("abc") == 2) | ||
635 | assert(p:match("bac") == 2) | ||
636 | |||
637 | -- associativity | ||
638 | -- (p1 / %1) /{1} (p2 / %2) /{2} p3 | ||
639 | -- left-associativity | ||
640 | -- ("a" /{1} "b") /{2} "c" | ||
641 | p = m.Lc(m.Lc(m.P"a" + m.T(1), m.P"b" + m.T(2), 1), m.P"c", 2) | ||
642 | assert(p:match("abc") == 2) | ||
643 | assert(p:match("bac") == 2) | ||
644 | assert(p:match("cab") == 2) | ||
645 | r, l, serror = p:match("dab") | ||
646 | assert(r == nil and l == 0 and serror == "dab") | ||
647 | |||
648 | |||
649 | -- righ-associativity | ||
650 | -- "a" /{1} ("b" /{2} "c") | ||
651 | p = m.Lc(m.P"a" + m.T(1), m.Lc(m.P"b" + m.T(2), m.P"c", 2), 1) | ||
652 | assert(p:match("abc") == 2) | ||
653 | assert(p:match("bac") == 2) | ||
654 | assert(p:match("cab") == 2) | ||
655 | r, l, serror = p:match("dab") | ||
656 | assert(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" | ||
664 | p = m.Lc(m.Lc(m.P"a" + m.T(2), m.P"b" + m.T(2), 1), m.P"c", 2) | ||
665 | assert(p:match("abc") == 2) | ||
666 | r, l, serror = p:match("bac") | ||
667 | assert(r == nil and l == 0 and serror == "bac") | ||
668 | assert(p:match("cab") == 2) | ||
669 | r, l, serror = p:match("dab") | ||
670 | assert(r == nil and l == 0 and serror == "dab") | ||
671 | |||
672 | |||
673 | -- righ-associativity | ||
674 | -- "a" /{1} ("b" /{2} "c") | ||
675 | p = m.Lc(m.P"a" + m.T(2), m.Lc(m.P"b" + m.T(2), m.P"c", 2), 1) | ||
676 | assert(p:match("abc") == 2) | ||
677 | r, l, serror = p:match("bac") | ||
678 | assert(r == nil and l == 2 and serror == "bac") | ||
679 | r, l, serror = p:match("cab") | ||
680 | assert(r == nil and l == 2 and serror == "cab") | ||
681 | r, l, serror = p:match("dab") | ||
682 | assert(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) | ||
687 | S -> S0 /{1} ID /{2} ID '=' Exp /{3} 'unsigned'* 'int' ID /{4} 'unsigned'* ID ID / %error | ||
688 | S0 -> ID S1 / 'unsigned' S2 / 'int' %3 | ||
689 | S1 -> '=' %2 / !. %1 / ID %4 | ||
690 | S2 -> 'unsigned' S2 / ID %4 / 'int' %3 | ||
691 | ]] | ||
692 | |||
693 | |||
694 | g = 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 | |||
705 | local s = "a" | ||
706 | assert(g:match(s) == #s + 1) --1 | ||
707 | s = "a = E" | ||
708 | assert(g:match(s) == #s + 1) --2 | ||
709 | s = "int a" | ||
710 | assert(g:match(s) == #s + 1) --3 | ||
711 | s = "unsigned int a" | ||
712 | assert(g:match(s) == #s + 1) --3 | ||
713 | s = "unsigned a a" | ||
714 | assert(g:match(s) == #s + 1) --4 | ||
715 | s = "b" | ||
716 | r, l, serror = g:match(s) | ||
717 | assert(r == nil and l == 5 and serror == s) | ||
718 | s = "unsigned" | ||
719 | r, l, serror = g:match(s) | ||
720 | assert(r == nil and l == 5 and serror == s) | ||
721 | s = "unsigned a" | ||
722 | r, l, serror = g:match(s) | ||
723 | assert(r == nil and l == 5 and serror == s) | ||
724 | s = "unsigned int" | ||
725 | r, l, serror = g:match(s) | ||
726 | assert(r == nil and l == 5 and serror == s) | ||
727 | |||
728 | |||
513 | 729 | ||
514 | local terror = { ['cmdSeq'] = "Missing ';' in CmdSeq", | 730 | local 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 |
862 | end | 1078 | end |
863 | 1079 | ||
864 | print(eval "98-76*(54/32)") | 1080 | assert(eval "98-76*(54/32)" == 37.125) |
865 | --> 37.125 | 1081 | --> 37.125 |
866 | 1082 | ||
867 | print(eval "(1+1-1*2/2") | 1083 | local e, msg = eval "(1+1-1*2/2" |
868 | --> syntax error: missing a closing ')' after the expression (at index 11) | 1084 | assert(e == nil and msg == "syntax error: missing a closing ')' after the expression (at index 11)") |
869 | 1085 | ||
870 | print(eval "(1+)-1*(2/2)") | 1086 | e, msg = eval "(1+)-1*(2/2)" |
871 | --> syntax error: expected a term after the operator (at index 4) | 1087 | assert(e == nil and msg == "syntax error: expected a term after the operator (at index 4)") |
872 | 1088 | ||
873 | print(eval "(1+1)-1*(/2)") | 1089 | e, msg = eval "(1+1)-1*(/2)" |
874 | --> syntax error: expected an expression after the parenthesis (at index 10) | 1090 | assert(e == nil and msg == "syntax error: expected an expression after the parenthesis (at index 10)") |
875 | 1091 | ||
876 | print(eval "1+(1-(1*2))/2x") | 1092 | e, msg = eval "1+(1-(1*2))/2x" |
877 | --> syntax error: extra chracters found after the expression (at index 14) | 1093 | assert(e == nil and msg == "syntax error: extra characters found after the expression (at index 14)") |
878 | 1094 | ||
879 | print(eval "-1+(1-(1*2))/2") | 1095 | e, msg = eval "-1+(1-(1*2))/2" |
880 | --> syntax error: no expression found (at index 1) | 1096 | assert(e == nil and msg == "syntax error: no expression found (at index 1)") |
881 | 1097 | ||
882 | print(eval "(1+1-1*(2/2+)-():") | 1098 | e, msg = eval "(1+1-1*(2/2+)-():" |
883 | --> syntax error: expected a term after the operator (at index 13) | 1099 | assert(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 | ||
889 | print("+") | 1105 | print("+") |