#
# Test the perl_regex_parser and perl_regex_engine modules
#
# makelib::make "../regex-lib.lib"; This makefile has been merged into lib7.lib
with
package re
=
regular_expression_matcher_g (
package p = perl_regex_parser;
package e = perl_regex_engine;
);
package m = regex_match_result;
fun search regexp
=
number_string::scan_string
(re::find
(re::compile_string regexp));
fun get_args text children
=
{ fun walk (m::Match (THE { pos, len }, children))
=
{ s = string::substring
(text, pos, len);
s . list::cat
(map walk children);
}
| walk (m::Match (NULL, children))
=
"" . list::cat
(map walk children);
list::cat
(map walk children);
}
fun pr x = "[" + pr' x + "]"
also pr' [] = ""
| pr' [x] = "\"" + x + "\""
| pr' (x . xs) = "\"" + x + "\", " + pr' xs
fun show s
=
{ print s;
();
}
do
fun grep regexp text
=
case search regexp text
of NULL => NULL
| THE (m::Match(_, children))
=>
THE (get_args text children)
fun matches regexp text expected
=
case grep regexp text
of THE result
=>
if result == expected
then
show("OK /" + regexp + "/ matches \"" + text + "\" result=" + pr result + "\n")
else
show("BUG /" + regexp + "/ matches \"" + text + "\" result=" + pr result +
" expected=" + pr expected + "\n")
| NULL
=>
show("BUG /" + regexp + "/ fails to match \"" + text +
"\" expected=" + pr expected + "\n")
fun fails regexp text
=
case grep regexp text
of THE result
=>
show ("BUG /" + regexp + "/ should not match \"" + text +
"\" result=" + pr result + "\n")
| NULL
=>
show ("OK /" + regexp + "/ fails to match \"" + text + "\" as expected\n")
fun demo ()
=
{ matches "a(.*)b" "baabcdefbsf" ["abcdef"];
matches "a(.+)b" "cabbab" ["bba"];
matches "(\\w+)\\s*=\\s*(\\d+)" "number = 123" ["number", "123"];
matches "(\\w+)\\s*=\\s*(-?\\d+(\\.\\d*)?)" "real = -123.23"
["real", "-123.23", ".23"];
matches "(\\w+)\\s*=\\s*(-?\\d+(\\.\\d*)?)" "real=-123"
["real", "-123", ""];
matches "a(.*)b(.*)c" "ccdcdcabcdfdsae" ["", ""];
fails "^abc" "acdcdabc";
fails "abc$" "acbcabcdc";
matches "a*(aba)*" "aaabaabaaba" [""];
matches "a*(ab\\w)*$" "aaabaabbabc" ["abc"];
matches "^[a-z]$" "a" [];
matches "^[A-Z]$" "A" [];
matches "\\Q.$\\][)( }{\\E" ".$\\][)( }{" []; # Testing \Q and \E
matches "\\bAllen\\b" "Allen Leung" [];
fails "\\bAllen\\b" "AllenLeung";
matches "(a { 5, 7 } )" "aaaaaaaaaa" ["aaaaaaa"];
matches "(a { 5, 5 } )" "baaaaaaa" ["aaaaa"];
matches "(a { 5, 3 } )" "aaaaaaa" [""];
matches "((ab) { 3, 3 } )" "fffffababababababab" ["ababab", "ab"];
matches "(\\d { 3,} )" "01xxx02xx0234xx" ["0234"];
matches "^\\ca$" "\x01" [];
matches "^\\c[$" "\x1b" [];
matches "^(x { 3 } ) { 3 }$" "xxxxxxxxx" ["xxx"];
matches "^(()*)*" "xxxxxxxxx" ["", ""];
matches "\\[(([^\\]]
|\\.)*)\\]" "[a-zA-Z0-9]" ["a-zA-Z0-9", "9"];
matches "[^0-9]" "a" [];
matches "#x([0-9a-hA-H]+)" "#xa" ["a"];
fails "^(x { 3 } ) { 3 }$" "xxxxxxxx";
fails "^(x { 3 } ) { 3 }$" "xxxxxxxxxx";
apply
(\\ x =>
matches "^(while
|for|do|package|generic|end|case|fun|my)$"
x [x])
["fun", "while", "case", "do"];
apply
(fails "^(while
|for|do|package|generic|end|case|fun|my)$")
["typedef", "goto", "switch", "return"];
();
}
end