| 1 |
;; |
| 2 |
;; testing regexp |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(use scheme.list) |
| 7 |
(use srfi.14) |
| 8 |
|
| 9 |
(test-start "regexp") |
| 10 |
|
| 11 |
;; Some test examples are taken from the test suite of Henry Spencer's |
| 12 |
;; regexp package and PCRE. |
| 13 |
|
| 14 |
(define (test-re re str expect) |
| 15 |
(test* (write-to-string `(,re ,str)) expect |
| 16 |
(rxmatch-substrings (rxmatch re str)))) |
| 17 |
|
| 18 |
(define (test-before re str expect . opts) |
| 19 |
(test* (write-to-string `(rxmatch-before (,re ,str))) expect |
| 20 |
(apply rxmatch-before (re str) opts))) |
| 21 |
(define (test-after re str expect . opts) |
| 22 |
(test* (write-to-string `(rxmatch-after (,re ,str))) expect |
| 23 |
(apply rxmatch-after (re str) opts))) |
| 24 |
(define (test-substring re str expect . opts) |
| 25 |
(test* (write-to-string `(rxmatch-substring (,re ,str))) expect |
| 26 |
(apply rxmatch-substring (re str) opts))) |
| 27 |
|
| 28 |
(define-syntax test-parse |
| 29 |
(syntax-rules () |
| 30 |
((_ re ast) |
| 31 |
(test* #"regexp-parse \"~re\"" ast (regexp-parse re))))) |
| 32 |
|
| 33 |
;;------------------------------------------------------------------------- |
| 34 |
(test-section "regexp-parse") |
| 35 |
|
| 36 |
(test-parse "a" '(0 #f #\a)) |
| 37 |
(test-parse "ab" '(0 #f #\a #\b)) |
| 38 |
(test-parse "(?:ab)" '(0 #f (seq #\a #\b))) |
| 39 |
(test-parse "(a)" '(0 #f (1 #f #\a))) |
| 40 |
(test-parse "a?" '(0 #f (rep 0 1 #\a))) |
| 41 |
(test-parse "a*" '(0 #f (rep 0 #f #\a))) |
| 42 |
(test-parse "a+" '(0 #f (rep 1 #f #\a))) |
| 43 |
(test-parse "a{3,5}" '(0 #f (rep 3 5 #\a))) |
| 44 |
(test-parse "a{3}" '(0 #f (rep 3 3 #\a))) |
| 45 |
(test-parse "a|b" '(0 #f (alt #\a #\b))) |
| 46 |
(test-parse "[ab]" '(0 #f #[ab])) |
| 47 |
(test-parse "[^ab]" '(0 #f (comp . #[ab]))) |
| 48 |
(test-parse "\\x0d;\\x0a;" '(0 #f #\return #\newline)) |
| 49 |
(test-parse "\\x0dbc\\x0acd" ;legacy fallback |
| 50 |
'(0 #f (seq #\return #\b #\c) (seq #\newline #\c #\d))) |
| 51 |
(test-parse "\\u000dbc;" '(0 #f (seq #\return #\b #\c #\;))) |
| 52 |
(test-parse "\x0d;[\x0a;-\x0d]" '(0 #f #\return #[\u000a-\u000d])) |
| 53 |
(test-parse "." '(0 #f any)) |
| 54 |
(test-parse "^" '(0 #f bol)) |
| 55 |
(test-parse "$" '(0 #f eol)) |
| 56 |
(test-parse "\\b" '(0 #f wb)) |
| 57 |
(test-parse "\\B" '(0 #f nwb)) |
| 58 |
(test-parse "(?>a)" '(0 #f (once #\a))) |
| 59 |
(test-parse "a*+" '(0 #f (once (rep 0 #f #\a)))) |
| 60 |
(test-parse "a++" '(0 #f (once (rep 1 #f #\a)))) |
| 61 |
(test-parse "a?+" '(0 #f (once (rep 0 1 #\a)))) |
| 62 |
(test-parse "(?i:a)" '(0 #f (seq-uncase #\a))) |
| 63 |
(test-parse "(?-i:a)" '(0 #f (seq-case #\a))) |
| 64 |
(test-parse "(?=a)" '(0 #f (assert #\a))) |
| 65 |
(test-parse "(?!a)" '(0 #f (nassert #\a))) |
| 66 |
(test-parse "(?<=ab)" '(0 #f (assert (lookbehind #\a #\b)))) |
| 67 |
(test-parse "(?<!ab)" '(0 #f (nassert (lookbehind #\a #\b)))) |
| 68 |
(test-parse "(?<name>a)" '(0 #f (1 name #\a))) |
| 69 |
(test-parse "(?(?=)y)" '(0 #f (cpat (assert) (#\y) ()))) |
| 70 |
(test-parse "(?(?=)y|n)" '(0 #f (cpat (assert) (#\y) (#\n)))) |
| 71 |
(test-parse "(?(?<=)y)" '(0 #f (cpat (assert (lookbehind)) (#\y) ()))) |
| 72 |
(test-parse "(?(?<=)y|n)" '(0 #f (cpat (assert (lookbehind)) (#\y) (#\n)))) |
| 73 |
(test-parse "()(?(1)y)" '(0 #f (1 #f) (cpat 1 (#\y) ()))) |
| 74 |
(test-parse "()(?(1)y|n)"'(0 #f (1 #f) (cpat 1 (#\y) (#\n)))) |
| 75 |
(test-parse "()\\1" '(0 #f (1 #f) (backref . 1))) |
| 76 |
(test-parse "(?<name>)\\k<name>" '(0 #f (1 name) (backref . 1))) |
| 77 |
(test-parse "(?<name>)(?<name>)\\k<name>" |
| 78 |
'(0 #f (1 name) (2 name) (alt (backref . 2) (backref . 1)))) |
| 79 |
|
| 80 |
;;------------------------------------------------------------------------- |
| 81 |
(test-section "regexp-unparse") |
| 82 |
|
| 83 |
(define (test-regexp-unparse src) |
| 84 |
(let1 ast (if (string? src) (regexp-optimize (regexp-parse src)) src) |
| 85 |
(test* (format "regexp-unparse ~s" src) ast |
| 86 |
(regexp-optimize (regexp-parse (regexp-unparse ast))) |
| 87 |
equal?))) |
| 88 |
|
| 89 |
(for-each test-regexp-unparse |
| 90 |
'(;; simple ones |
| 91 |
"" "a" "ab" "a*" "a+" "a?" "ab*" "ab+" "ab?" "a*b" "a+b" "a?b" |
| 92 |
"a{3}" "a{2,4}" "a{3,}" "a|b" "a|b|c" "^ab" "^a|b" "^a|b$" |
| 93 |
"." "\\." "\\**" "\\(\\)\\{\\}\\[\\]" "\\bfoo\\B" |
| 94 |
;; charset |
| 95 |
"[a-z]" "[^a-z]" "[a-z]+" "[a^ef-]" "[a]" "[.]" "[\\[]" "[\\^]" |
| 96 |
;; grouping |
| 97 |
"(a)(b)" "(a|b)c(d)" "(a(b(c|d)|e))f" |
| 98 |
"(?:abc)*" "(ab(?:cd)?e{3,4})" "(?i:ab(?-i:cd)ef)" |
| 99 |
;; backref |
| 100 |
"(a)bc\\1" "ab(?<foo>c)(d)\\k<foo>" "((.)\\2)" |
| 101 |
;; once, lookahead, lookbehind |
| 102 |
"(?>abc)" "(?=a*b*c)" "(?!a*b*c)" "(?<=a[bc])" "(?<!a[bc])" |
| 103 |
"(a)(?(1)b)" "(a)(?(1)b|c)" "(a)(?(1)|c)" |
| 104 |
"(?(?=ab)cd|ef)" "(?(?!ab)cd|ef)" |
| 105 |
"(?(?<=ab)cd|ef)" "(?(?<!ab)cd|ef)" |
| 106 |
)) |
| 107 |
|
| 108 |
;;------------------------------------------------------------------------- |
| 109 |
(test-section "compile") |
| 110 |
|
| 111 |
(define-syntax test-regexp-compile |
| 112 |
(syntax-rules () |
| 113 |
[(_ pat) |
| 114 |
(test* #"regexp-compile \"~|pat|\"" |
| 115 |
(let1 orig (string->regexp pat) |
| 116 |
(list (regexp->string orig) |
| 117 |
(regexp-num-groups orig) |
| 118 |
(regexp-named-groups orig))) |
| 119 |
(let1 compiled (regexp-compile (regexp-parse pat)) |
| 120 |
(list (regexp->string compiled) |
| 121 |
(regexp-num-groups compiled) |
| 122 |
(regexp-named-groups compiled))))])) |
| 123 |
|
| 124 |
(test-regexp-compile "a") |
| 125 |
(test-regexp-compile "ab") |
| 126 |
(test-regexp-compile "(?:ab)") |
| 127 |
(test-regexp-compile "(a)") |
| 128 |
(test-regexp-compile "a?") |
| 129 |
(test-regexp-compile "a*") |
| 130 |
(test-regexp-compile "a+") |
| 131 |
(test-regexp-compile "a{3,5}") |
| 132 |
(test-regexp-compile "a{3}") |
| 133 |
(test-regexp-compile "a|b") |
| 134 |
(test-regexp-compile "[ab]") |
| 135 |
(test-regexp-compile "[^ab]") |
| 136 |
(test-regexp-compile ".") |
| 137 |
(test-regexp-compile "^") |
| 138 |
(test-regexp-compile "$") |
| 139 |
(test-regexp-compile "\\b") |
| 140 |
(test-regexp-compile "\\B") |
| 141 |
(test-regexp-compile "(?>a)") |
| 142 |
(test-regexp-compile "a*+") |
| 143 |
(test-regexp-compile "a++") |
| 144 |
(test-regexp-compile "a?+") |
| 145 |
(test-regexp-compile "(?i:a)") |
| 146 |
(test-regexp-compile "(?-i:a)") |
| 147 |
(test-regexp-compile "(?=a)") |
| 148 |
(test-regexp-compile "(?!a)") |
| 149 |
(test-regexp-compile "(?<=ab)") |
| 150 |
(test-regexp-compile "(?<!ab)") |
| 151 |
(test-regexp-compile "(?<name>a)") |
| 152 |
(test-regexp-compile "(?(?=)y)") |
| 153 |
(test-regexp-compile "(?(?=)y|n)") |
| 154 |
(test-regexp-compile "(?(?<=)y)") |
| 155 |
(test-regexp-compile "(?(?<=)y|n)") |
| 156 |
(test-regexp-compile "()(?(1)y)") |
| 157 |
(test-regexp-compile "()(?(1)y|n)") |
| 158 |
(test-regexp-compile "()\\1") |
| 159 |
(test-regexp-compile "(?<name>)\\k<name>") |
| 160 |
(test-regexp-compile "(?<name>)(?<name>)\\k<name>") |
| 161 |
|
| 162 |
;; Renumbering groups |
| 163 |
(test* "regexp-compile group renumbering" |
| 164 |
'(5 (0 #f (1 #f (2 #f #\a) (3 #f #\b)) (4 #f #\c))) |
| 165 |
(let1 c (regexp-compile |
| 166 |
'(0 #f (3 #f (100 #f #\a) (0 #f #\b)) (3 #f #\c))) |
| 167 |
(list (regexp-num-groups c) |
| 168 |
(regexp-ast c)))) |
| 169 |
|
| 170 |
(define %regexp-laset (with-module gauche.internal %regexp-laset)) |
| 171 |
(define-syntax test-regexp-laset |
| 172 |
(syntax-rules () |
| 173 |
[(_ pat exp) |
| 174 |
(test* #"regexp-laset \"~|pat|\"" exp |
| 175 |
(%regexp-laset (regexp-compile (regexp-parse pat))))])) |
| 176 |
|
| 177 |
(test-regexp-laset "abc" #[a]) |
| 178 |
(test-regexp-laset "(abc)" #[a]) |
| 179 |
(test-regexp-laset "a|b|c" #[a-c]) |
| 180 |
(test-regexp-laset "(a|b)|c" #[a-c]) |
| 181 |
(test-regexp-laset "a*b" #[ab]) |
| 182 |
(test-regexp-laset "a+b" #[a]) |
| 183 |
(test-regexp-laset "(abc)*(bcd)*ef" #[abe]) |
| 184 |
(test-regexp-laset "([^\"]|\"\")+" (char-set-complement #[])) |
| 185 |
|
| 186 |
;;------------------------------------------------------------------------- |
| 187 |
(test-section "boundary") |
| 188 |
|
| 189 |
(test* "submatch #f" #f (rxmatch-start #f)) |
| 190 |
(test* "submatch #f" #f (rxmatch-end #f)) |
| 191 |
(test* "submatch #f" #f (rxmatch-substring #f)) |
| 192 |
(test* "num-match #f" 0 (rxmatch-num-matches #f)) |
| 193 |
|
| 194 |
;;------------------------------------------------------------------------- |
| 195 |
(test-section "basics") |
| 196 |
|
| 197 |
(test-re #/a/ "a" '("a")) |
| 198 |
(test-re #/a/ "A" '()) |
| 199 |
(test-re #/a/ "ba" '("a")) |
| 200 |
(test-re #/a/ "bac" '("a")) |
| 201 |
(test-re #/a/ "" '()) ;; input null str |
| 202 |
(test-re #/a/ (string (integer->char 0) #\a #\b) '("a")) ;; NUL character |
| 203 |
(test-re #/abc/ "abc" '("abc")) |
| 204 |
(test-re #/abc/ "abbc" '()) |
| 205 |
(test-re #/abc/ "babcd" '("abc")) |
| 206 |
(test-re #/abc|de/ "dabce" '("abc")) |
| 207 |
(test-re #/abc|de/ "abdec" '("de")) |
| 208 |
(test-re #/abc|de/ "abe" '()) |
| 209 |
(test-re #/a|b|c/ "abc" '("a")) |
| 210 |
(test-re #/a|b|c/ "bac" '("b")) |
| 211 |
(test-re #/a|b|c/ "def" '()) |
| 212 |
(test-re #/|abc/ "abc" '("")) |
| 213 |
(test-re #/abc|/ "abc" '("abc")) |
| 214 |
(test-re #/abc|/ "abd" '("")) |
| 215 |
|
| 216 |
;;------------------------------------------------------------------------- |
| 217 |
(test-section "parens") |
| 218 |
|
| 219 |
(test-re #/a(b)c/ "abc" '("abc" "b")) |
| 220 |
(test-re #/a((b)(c))/ "abc" '("abc" "bc" "b" "c")) |
| 221 |
(test-re #/a((((b))))c/ "abc" '("abc" "b" "b" "b" "b")) |
| 222 |
(test-re #/a((((b))))c/ "a(b)c" '()) |
| 223 |
(test-re #/a\(/ "a(" '("a(")) |
| 224 |
(test-re #/a()b/ "ab" '("ab" "")) |
| 225 |
(test-re #/a()()b/ "ab" '("ab" "" "")) |
| 226 |
(test-re #/(we|wee|week|frob)(knights|night|day)/ "weeknights" |
| 227 |
'("weeknights" "wee" "knights")) |
| 228 |
(test-re #/aa|(bb)|cc/ "aabb" '("aa" #f)) |
| 229 |
(test-re #/aa|(bb)|cc/ "abbaa" '("bb" "bb")) |
| 230 |
(test-re #/aa|(bb)|cc/ "bccaa" '("cc" #f)) |
| 231 |
(test-re #/aa|a(b)|cc/ "abaab" '("ab" "b")) |
| 232 |
(test-re #/aa|a(b)/ "abaab" '("ab" "b")) |
| 233 |
(test-re #/aa|(a(b))|cc/ "abaabcc" '("ab" "ab" "b")) |
| 234 |
(test-re #/(ab)|ac/ "aaaabcc" '("ab" "ab")) |
| 235 |
(test-re #/(a(b))|ac/ "abaabcc" '("ab" "ab" "b")) |
| 236 |
(test-re #/ab|(ac)/ "aaaabcc" '("ab" #f)) |
| 237 |
(test-re #/ab|(ac)/ "aaaacbc" '("ac" "ac")) |
| 238 |
(test-re #/aa|(ab|(ac))|ad/ "cac" '("ac" "ac" "ac")) |
| 239 |
(test-re #/(aa|(a(b)|a(c))|ad)/ "cac" '("ac" "ac" "ac" #f "c")) |
| 240 |
(test-re #/(.)*/ "abc" '("abc" "c")) |
| 241 |
(test-re #/(a([^a])*)*/ "abcaBC" '("abcaBC" "aBC" "C")) |
| 242 |
(test-re #/b|()|a/ "cac" '("" "")) |
| 243 |
(test-re #/(a)*a/ "a" '("a" #f)) |
| 244 |
(test-re #/(a)*a/ "aa" '("aa" "a")) |
| 245 |
(test-re #/(a)*a/ "aaa" '("aaa" "a")) |
| 246 |
(test-re #/(a)*?a/ "a" '("a" #f)) |
| 247 |
(test-re #/(a)*?a/ "aa" '("a" #f)) |
| 248 |
|
| 249 |
;;------------------------------------------------------------------------- |
| 250 |
(test-section "simple meta") |
| 251 |
|
| 252 |
(test-re #/a.c/ "abc" '("abc")) |
| 253 |
(test-re #/a../ "abc" '("abc")) |
| 254 |
(test-re #/a../ "ab" '()) |
| 255 |
(test-re #/.../ "ab" '()) |
| 256 |
(test-re #/./ "abc" '("a")) |
| 257 |
(test-re #/./ "" '()) |
| 258 |
|
| 259 |
;;------------------------------------------------------------------------- |
| 260 |
(test-section "anchors") |
| 261 |
|
| 262 |
(test-re #/^abc/ "abcd" '("abc")) |
| 263 |
(test-re #/^abc/ "aabcd" '()) |
| 264 |
(test-re #/^^/ "^^abc" '("^")) |
| 265 |
(test-re #/^^/ "a^^c" '()) |
| 266 |
(test-re #/^abc|def/ "abc" '("abc")) |
| 267 |
(test-re #/^abc|def/ "zabc" '()) |
| 268 |
(test-re #/^abc|def/ "zabcdef" '("def")) |
| 269 |
(test-re #/abc|^def/ "defabc" '("def")) |
| 270 |
(test-re #/abc|^def/ "abcdef" '("abc")) |
| 271 |
(test-re #/abc|^def/ "defabbc" '("def")) |
| 272 |
(test-re #/abc|^def/ "adefbc" '()) |
| 273 |
(test-re #/^(abc|def)/ "abc" '("abc" "abc")) |
| 274 |
(test-re #/^(abc|def)/ "aabc" '()) |
| 275 |
(test-re #/(^abc|def)/ "abcdef" '("abc" "abc")) |
| 276 |
(test-re #/(^abc|def)/ "^abcdef" '("def" "def")) |
| 277 |
(test-re #/a(^bc|def)/ "a^bcdef" '("a^bc" "^bc")) |
| 278 |
(test-re #/a(^bc|def)/ "abcdef" '()) |
| 279 |
(test-re #/^/ "hoge" '("")) |
| 280 |
(test-re (string->regexp "^a" :multi-line #t) |
| 281 |
"def\nabc\nghi" |
| 282 |
'("a")) |
| 283 |
(test-re #/$/ "hoge" '("")) |
| 284 |
(test-re (string->regexp "c$" :multi-line #t) |
| 285 |
"def\nabc\nghi" |
| 286 |
'("c")) |
| 287 |
(test-re #/abc$/ "bcabc" '("abc")) |
| 288 |
(test-re #/abc$/ "abcab" '()) |
| 289 |
(test-re #/^abc$/ "abc" '("abc")) |
| 290 |
(test-re #/abc$$/ "abc" '()) |
| 291 |
(test-re #/abc$$/ "abc$" '("abc$")) |
| 292 |
(test-re #/$$/ "abc$" '("$")) |
| 293 |
(test-re #/^$/ "" '("")) |
| 294 |
(test-re #/^$/ "a" '()) |
| 295 |
(test-re #/^^$$/ "^$" '("^$")) |
| 296 |
(test-re #/abc$|def/ "abc" '("abc")) |
| 297 |
(test-re #/abc$|def/ "defabc" '("def")) |
| 298 |
(test-re #/^abc|def$/ "abcdef" '("abc")) |
| 299 |
(test-re #/^abc|def$/ "defabc" '()) |
| 300 |
(test-re #/^abc|def$/ "defabc" '()) |
| 301 |
(test-re #/(^abc|def$)/ "aaadef" '("def" "def")) |
| 302 |
(test-re #/(^abc|def$)$/ "aaadef" '()) |
| 303 |
(test-re #/(^abc|def$)$/ "aaadef$" '("def$" "def$")) |
| 304 |
(test-re #/(abc$|def)$/ "aaabc" '()) |
| 305 |
(test-re #/(abc$|def)$/ "aaabc$" '("abc$" "abc$")) |
| 306 |
(test-re #/a$b/ "aa$bb" '("a$b")) |
| 307 |
(test-re #/ab\$/ "ab$cd" '("ab$")) |
| 308 |
|
| 309 |
;;------------------------------------------------------------------------- |
| 310 |
(test-section "backslash escape") |
| 311 |
|
| 312 |
(test-re #/a\*c/ "a*c" '("a*c")) |
| 313 |
(test-re #/a\.c/ "a.c" '("a.c")) |
| 314 |
(test-re #/a\.c/ "abc" '()) |
| 315 |
(test-re #/a\\b/ "a\\b" '("a\\b")) |
| 316 |
(test-re #/a\\\*b/ "a\\*b" '("a\\*b")) |
| 317 |
(test-re #/a\jc/ "ajc" '("ajc")) |
| 318 |
(test-re #/a\\bc/ "a\\bc" '("a\\bc")) |
| 319 |
(test-re #/a\[b/ "a[b" '("a[b")) |
| 320 |
|
| 321 |
;;------------------------------------------------------------------------- |
| 322 |
(test-section "word boundary") |
| 323 |
|
| 324 |
(test-re #/.z\b/ "bzbazoz ize" '("oz")) |
| 325 |
(test-re #/\b.z/ "brzbazoz ize" '("iz")) |
| 326 |
(test-re #/.z\B/ "bz baz oz ize" '("iz")) |
| 327 |
(test-re #/\B.z/ "bz baz oz ize" '("az")) |
| 328 |
|
| 329 |
;;------------------------------------------------------------------------- |
| 330 |
(test-section "repetitions") |
| 331 |
|
| 332 |
(test-re #/ab*c/ "abc" '("abc")) |
| 333 |
(test-re #/ab*c/ "ac" '("ac")) |
| 334 |
(test-re #/ab*c/ "abbbc" '("abbbc")) |
| 335 |
(test-re #/ab*c/ "abbabaabbc" '("abbc")) |
| 336 |
(test-re #/ab+c/ "abc" '("abc")) |
| 337 |
(test-re #/ab+c/ "abbc" '("abbc")) |
| 338 |
(test-re #/ab+c/ "abbabaabbc" '("abbc")) |
| 339 |
(test-re #/ab?c/ "abc" '("abc")) |
| 340 |
(test-re #/ab?c/ "abbaac" '("ac")) |
| 341 |
(test-re #/a.*c/ "abc" '("abc")) |
| 342 |
(test-re #/a.*c/ "zaabcabcabcabcczab" '("aabcabcabcabcc")) |
| 343 |
(test-re #/a(b*|c)d/ "abbd" '("abbd" "bb")) |
| 344 |
(test-re #/a(b*|c)d/ "ad" '("ad" "")) |
| 345 |
(test-re #/a(b*|c)d/ "acd" '("acd" "c")) |
| 346 |
(test-re #/a(b*|c)d/ "abcd" '()) |
| 347 |
(test-re #/a.*c/ "bacbababbbbadbaba" '("ac")) |
| 348 |
(test-re #/a.*c/ "abaaaabababbadbabdba" '()) |
| 349 |
|
| 350 |
;; Tests for input skipping using laset |
| 351 |
(test-re #/a+b+/ "aaaacccccccbaaabbccc" '("aaabb")) |
| 352 |
(test-re #/a*b+/ "aaaacccccccbaaabbccc" '("b")) |
| 353 |
|
| 354 |
(test-re #/a+@a+\.a+/ "aaaaaaaaaaa@aaaaaaaa@@a.aaaaa@aaaaa" '()) |
| 355 |
(test-re #/a+@a+\.a+/ "aaaaaaaaaaa@aaaaaaaa@@[email protected]" '("[email protected]")) |
| 356 |
|
| 357 |
;; Repetition of empty string |
| 358 |
(test-re #/a(?:)*b/ "zabr" '("ab")) |
| 359 |
(test-re #/a(?:)?b/ "zabr" '("ab")) |
| 360 |
(test-re #/a(?:){2,5}b/ "zabr" '("ab")) |
| 361 |
|
| 362 |
;;------------------------------------------------------------------------- |
| 363 |
(test-section "repetitions (non-greedy)") |
| 364 |
|
| 365 |
(test-re #/ab*?./ "abc" '("ab")) |
| 366 |
(test-re #/ab*?./ "ac" '("ac")) |
| 367 |
(test-re #/a.*?c/ "abbbc" '("abbbc")) |
| 368 |
(test-re #/a.*?a/ "abbabaabbc" '("abba")) |
| 369 |
(test-re #/<.*?>/ "<tag1><tag2><tag3>" '("<tag1>")) |
| 370 |
|
| 371 |
(test-re #/ab+?./ "abc" '("abc")) |
| 372 |
(test-re #/ab+?./ "abbc" '("abb")) |
| 373 |
(test-re #/a.+?a/ "abbabaabbc" '("abba")) |
| 374 |
(test-re #/<.+?>/ " <><tag1><tag2>" '("<><tag1>")) |
| 375 |
|
| 376 |
(test-re #/ab??c/ "abc" '("abc")) |
| 377 |
(test-re #/ab??c/ "abbaac" '("ac")) |
| 378 |
(test-re #/ab??./ "abbaac" '("ab")) |
| 379 |
(test-re #/a(hoge)??hoge/ "ahogehoge" '("ahoge" #f)) |
| 380 |
(test-re #/(foo)??bar/ "foobar" '("foobar" "foo")) |
| 381 |
(test-re #/(foo)??bar/ "foofoobar" '("foobar" "foo")) |
| 382 |
(test-re #/(foo)*?bar/ "foofoobar" '("foofoobar" "foo")) |
| 383 |
|
| 384 |
;;------------------------------------------------------------------------- |
| 385 |
(test-section "character class") |
| 386 |
|
| 387 |
(test-re #/a[bc]d/ "abd" '("abd")) |
| 388 |
(test-re #/a[bc]d/ "acd" '("acd")) |
| 389 |
(test-re #/a[bc]d/ "aed" '()) |
| 390 |
(test-re #/a[a-z]d/ "aed" '("aed")) |
| 391 |
(test-re #/a[a-z]d/ "aEd" '()) |
| 392 |
(test-re #/a[]]d/ "a]d" '("a]d")) |
| 393 |
(test-re #/a[]-]d/ "a-d" '("a-d")) |
| 394 |
(test-re #/a[]-^]d/ "a-d" '()) |
| 395 |
(test-re #/a[]-^]d/ "a]d" '("a]d")) |
| 396 |
(test-re #/a[a-z-]d/ "a-d" '("a-d")) |
| 397 |
(test-re #/a[a-z-]d/ "afd" '("afd")) |
| 398 |
(test-re #/a[az-]d/ "a-d" '("a-d")) |
| 399 |
(test-re #/a[a-]d/ "a-d" '("a-d")) |
| 400 |
(test-re #/a[az-]d/ "afd" '()) |
| 401 |
(test-re #/a[az-]d/ "azd" '("azd")) |
| 402 |
(test-re #/a[^ab]c/ "abacc" '("acc")) |
| 403 |
(test-re #/a[^]]c/ "abc" '("abc")) |
| 404 |
(test-re #/a[^]]c/ "a]c" '()) |
| 405 |
(test-re #/a[^^]c/ "abc" '("abc")) |
| 406 |
(test-re #/a[^^]c/ "a^c" '()) |
| 407 |
(test-re #/a[Bc]*d/ "aBccBd" '("aBccBd")) |
| 408 |
(test-re #/[a]b[c]/ "abc" '("abc")) |
| 409 |
(test-re #/[abc]b[abc]/ "abc" '("abc")) |
| 410 |
(test-re #/a[bc]d/ "xyzaaabcaababdacd" '("abd")) |
| 411 |
(test-re #/a[ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab][ab](wee|week)(knights|night)/ |
| 412 |
"aaaaabaaaabaaaabaaaabweeknights" |
| 413 |
'("aaaaabaaaabaaaabaaaabweeknights" "wee" "knights")) |
| 414 |
(test-re #/[ab][cd][ef][gh][ij][kl][mn]/ "xacegikmoq" '("acegikm")) |
| 415 |
(test-re #/[ab][cd][ef][gh][ij][kl][mn][op]/ "xacegikmoq" '("acegikmo")) |
| 416 |
(test-re #/[ab][cd][ef][gh][ij][kl][mn][op][qr]/ "xacegikmoqy" '("acegikmoq")) |
| 417 |
(test-re #/[ab][cd][ef][gh][ij][kl][mn][op][q]/ "xacegikmoqy" '("acegikmoq")) |
| 418 |
|
| 419 |
;; this tests optimizer |
| 420 |
(test-re #/[^a]*./ "b" '("b")) |
| 421 |
(test-re #/\s/ " " '(" ")) |
| 422 |
(test-re #/\s\s/ " " '(" ")) |
| 423 |
(test-re #/\s/ "\t " '("\t")) |
| 424 |
(test-re #/\s\s/ "\\s" '()) |
| 425 |
(test-re #/\\s\s/ "\\s " '("\\s ")) |
| 426 |
(test-re #/(\s*)(\S+)(\s*)/ "\txyz abc" '("\txyz " "\t" "xyz" " ")) |
| 427 |
(test-re #/(\d+)(\D+)(\d+)(\D+)(\d+)/ " (1234) 5678-9012 " |
| 428 |
'("1234) 5678-9012" "1234" ") " "5678" "-" "9012")) |
| 429 |
(test-re #/(\w+)(\W+)(\w+)/ "three o'clock" '("three o" "three" " " "o")) |
| 430 |
|
| 431 |
(test-re #/[ab\sc]+/ "d ba e" '(" ba ")) |
| 432 |
(test-re #/[\sabc]+/ "d ba e" '(" ba ")) |
| 433 |
(test-re #/[\S\t]+/ "\tab cd" '("\tab")) |
| 434 |
(test-re #/[\s\d]+/ "a 1 2 3 b " '(" 1 2 3 ")) |
| 435 |
(test-re #/[\s\D]+/ "a 1 2 3 b " '("a ")) |
| 436 |
|
| 437 |
(test-re #/\u3000*x/ "\u3000\u3000\u3000x" '("\u3000\u3000\u3000x")) |
| 438 |
(test-re #/\u3000+/ "\u3001\u3000\u3000" '("\u3000\u3000")) |
| 439 |
(test-re #/\u3000+x/ "\u3000\u3000\u3001x" '()) |
| 440 |
(test-re #/\u3000+[xy]/ "\u3000\u3000\u3001x\u3001\u3000y" '("\u3000y")) |
| 441 |
|
| 442 |
;; this tests optimizer |
| 443 |
(test-re #/^\[?([^\]]*)\]?:(\d+)$/ "127.0.0.1:80" |
| 444 |
'("127.0.0.1:80" "127.0.0.1" "80")) |
| 445 |
(test-re #/^\[?([^\]]*)\]?:(\d+)$/ "[127.0.0.1:80" |
| 446 |
'("[127.0.0.1:80" "127.0.0.1" "80")) |
| 447 |
(test-re #/^\[?([^\]]*)\]?:(\d+)$/ "[127.0.0.1]:80" |
| 448 |
'("[127.0.0.1]:80" "127.0.0.1" "80")) |
| 449 |
|
| 450 |
;;------------------------------------------------------------------------- |
| 451 |
(test-section "{n,m}") |
| 452 |
|
| 453 |
(test-after #/a{2}/ "abaaaabaa" "aabaa") |
| 454 |
(test-after #/a{2,}/ "abaaaabaa" "baa") |
| 455 |
(test-after #/a{1,2}/ "abaaaabaa" "baaaabaa") |
| 456 |
(test-after #/a{2,3}/ "abaaaabaa" "abaa") |
| 457 |
(test-after #/a{b,c}/ "za{b,c}def" "def") |
| 458 |
;; The following test assumes Perl compatibility. (See the comment in |
| 459 |
;; rc1_lex_minmax in regexp.c. |
| 460 |
;(test-after #/a{,2}*/ "za{,2}}def" "def") |
| 461 |
;; The above should be written in Oniguruma compatible mode. |
| 462 |
(test-after #/a\{,2}*/ "za{,2}}def" "def") |
| 463 |
;; The following test assumes Oniguruma compability. |
| 464 |
(test-after #/a{,3}/ "aabaaaab" "baaaab") |
| 465 |
|
| 466 |
(test-after #/(ab){2}/ "babbabababba" "abba") |
| 467 |
(test-after #/(ab){2,2}/ "babbabababba" "abba") |
| 468 |
(test-after #/(ab){2,}/ "babbabababba" "ba") |
| 469 |
(test-after #/(ab){1,2}/ "babbabababba" "babababba") |
| 470 |
(test-after #/(ab){2,3}/ "babbabababba" "ba") |
| 471 |
(test-after #/(ab){b,c}/ "zab{b,c}def" "def") |
| 472 |
|
| 473 |
(test-re #/(\d{2})(\d{2})/ "a12345b" '("1234" "12" "34")) |
| 474 |
(test-re #/(\d{2,})(\d{2,})/ "a12345b" '("12345" "123" "45")) |
| 475 |
(test-re #/(\d{2})(\d{2,})/ "a12345b" '("12345" "12" "345")) |
| 476 |
(test-re #/(\d{1,3})(\d{2,})/ "a1234b" '("1234" "12" "34")) |
| 477 |
(test-re #/(\d{1,3})(\d{0,2})/ "a1234b" '("1234" "123" "4")) |
| 478 |
(test-re #/(\d{2}){2}/ "a12345b" '("1234" "34")) |
| 479 |
|
| 480 |
(test* "{2}" (test-error) (regexp? (string->regexp "{2}"))) |
| 481 |
(test* "{z}" #t (regexp? (string->regexp "{z}"))) |
| 482 |
(test* "{-1}" #t (regexp? (string->regexp "{-1}"))) |
| 483 |
(test* "{300}" (test-error) (regexp? (string->regexp "{300}"))) |
| 484 |
(test* "{3,1}" (test-error) (regexp? (string->regexp "{3,1}"))) |
| 485 |
|
| 486 |
;;------------------------------------------------------------------------- |
| 487 |
(test-section "{n,m} (non-greedy)") |
| 488 |
|
| 489 |
(test-after #/a{2}?/ "abaaaabaa" "aabaa") |
| 490 |
(test-after #/a{2,}?/ "abaaaabaa" "aabaa") |
| 491 |
(test-after #/a{1,2}?/ "abaaaabaa" "baaaabaa") |
| 492 |
(test-after #/a{2,3}?/ "abaaaabaa" "aabaa") |
| 493 |
|
| 494 |
(test-after #/(ab){2}?/ "babbabababba" "abba") |
| 495 |
(test-after #/(ab){2,2}?/ "babbabababba" "abba") |
| 496 |
(test-after #/(ab){2,}?/ "babbabababba" "abba") |
| 497 |
(test-after #/(ab){1,2}?/ "babbabababba" "babababba") |
| 498 |
(test-after #/(ab){2,3}?/ "babbabababba" "abba") |
| 499 |
|
| 500 |
(test-re #/(\d{2,}?)(\d{2,}?)/ "a12345b" '("1234" "12" "34")) |
| 501 |
(test-re #/(\d{2,})(\d{2,}?)/ "a12345b" '("12345" "123" "45")) |
| 502 |
(test-re #/(\d{2,}?)(\d{2,})/ "a12345b" '("12345" "12" "345")) |
| 503 |
(test-re #/(\d{1,3}?)(\d{2,}?)/ "a1234b" '("123" "1" "23")) |
| 504 |
(test-re #/(\d{1,3}?)(\d{0,2}?)/ "a1234b" '("1" "1" "")) |
| 505 |
|
| 506 |
;;------------------------------------------------------------------------- |
| 507 |
(test-section "uncapturing group") |
| 508 |
|
| 509 |
(test-substring #/a(?:b)*c(d)/ "abcdbcdefg" "d" 1) |
| 510 |
(test-substring #/a(?:bcd)*e(f)/ "abcdbcdefg" "f" 1) |
| 511 |
(test-substring #/a(?:bcd)*e(f)/ "aefg" "f" 1) |
| 512 |
(test-substring #/a(?:bcd)+e(f)/ "aefg" #f 1) |
| 513 |
(test-substring #/a(?:bc(de(?:fg)?hi)jk)?l/ "abcdefghijkl" "defghi" 1) |
| 514 |
(test-substring #/a(?:bc(de(?:fg)?hi)jk)?l/ "abcdehijkl" "dehi" 1) |
| 515 |
|
| 516 |
(test-substring #/a(?i:bc)d/ "!aBCd!" "aBCd") |
| 517 |
(test-substring #/a(?i:bc)d/ "!aBCD!" #f) |
| 518 |
(test-substring #/a(?i:[a-z]+)d/ "!aBcd!" "aBcd") |
| 519 |
(test-substring #/a(?i:[a-z]+)d/ "!ABcd!" #f) |
| 520 |
|
| 521 |
(test-substring #/A(?-i:Bc)D/ "!ABcD!" "ABcD") |
| 522 |
(test-substring #/A(?-i:Bc)D/ "!ABcd!" #f) |
| 523 |
(test-substring #/A(?-i:[A-Z]+)D/ "!ABCD!" "ABCD") |
| 524 |
(test-substring #/A(?-i:[A-Z]+)D/ "!abCD!" #f) |
| 525 |
|
| 526 |
(test-substring #/A(?-i:Bc)D/i "!aBcd!" "aBcd") |
| 527 |
(test-substring #/A(?-i:Bc)D/i "!abCd!" #f) |
| 528 |
(test-substring #/A(?-i:[A-Z]+)D/i "!aBCd!" "aBCd") |
| 529 |
(test-substring #/A(?-i:[A-Z]+)D/i "!abcd!" #f) |
| 530 |
|
| 531 |
;; these tests optimizer |
| 532 |
(test-substring #/^(?i:a*).$/ "b" "b") |
| 533 |
(test-substring #/^(?i:a*).$/ "a" "a") |
| 534 |
(test-substring #/^(?i:a*).$/ "A" "A") |
| 535 |
(test-substring #/^(?i:a*).$/ "Ab" "Ab") |
| 536 |
|
| 537 |
;;------------------------------------------------------------------------- |
| 538 |
(test-section "backreference") |
| 539 |
(test-re #/^(.)\1$/ "aa" '("aa" "a")) |
| 540 |
(test-re #/^(.)\1$/ "ab" '()) |
| 541 |
(test-re #/(.+)\1/ "a123123j" '("123123" "123")) |
| 542 |
(test-re #/(.+)\1/i "AbCaBC" '("AbCaBC" "AbC")) |
| 543 |
(test-re #/(.+)\1/ "AbCAb1" '()) |
| 544 |
(test-re #/((.)\2)/ "aa" '("aa" "aa" "a")) |
| 545 |
(test-re #/(a)*\1/ "aa" '("aa" "a")) |
| 546 |
(test-re #/(aa)*\1/ "aaa" '()) |
| 547 |
(test-re #/(aa)*\1/ "aaaaa" '("aaaa" "aa")) |
| 548 |
(test* "^\\1(.)$" (test-error) (string->regexp "^\\1(.)")) |
| 549 |
(test* "^(\\1)$" (test-error) (string->regexp "^(\\1)$")) |
| 550 |
(test* "(.)\\2" (test-error) (string->regexp "(.)\\2")) |
| 551 |
(test* "((.)\\1)" (test-error) (string->regexp "((.)\\1)")) |
| 552 |
|
| 553 |
;;------------------------------------------------------------------------- |
| 554 |
(test-section "independent subexpression") |
| 555 |
(test-re #/(?>.*\/)foo/ "/this/is/a/long/line/" '()) |
| 556 |
(test-re #/(?>.*\/)foo/ "/this/is/a/long/line/foo" '("/this/is/a/long/line/foo")) |
| 557 |
(test-re #/(?>(\.\d\d[1-9]?))\d+/ "1.230003938" '(".230003938" ".23")) |
| 558 |
(test-re #/(?>(\.\d\d[1-9]?))\d+/ "1.875000282" '(".875000282" ".875")) |
| 559 |
(test-re #/(?>(\.\d\d[1-9]?))\d+/ "1.235" '()) |
| 560 |
(test-re #/^((?>\w+)|(?>\s+))*$/ "foo bar" '("foo bar" "bar")) |
| 561 |
(test-re #/a*+a/ "aaa" '()) |
| 562 |
(test-re #/a*+b/ "aab" '("aab")) |
| 563 |
(test-re #/a++a/ "aaa" '()) |
| 564 |
(test-re #/a++b/ "aab" '("aab")) |
| 565 |
(test-re #/a?+a/ "a" '()) |
| 566 |
(test-re #/a?+b/ "ab" '("ab")) |
| 567 |
|
| 568 |
;;------------------------------------------------------------------------- |
| 569 |
(test-section "lookahead assertion") |
| 570 |
|
| 571 |
(test-re #/^(?=ab(de))(abd)(e)/ "abde" '("abde" "de" "abd" "e")) |
| 572 |
(test-re #/^(?!(ab)de|x)(abd)(f)/ "abdf" '("abdf" #f "abd" "f")) |
| 573 |
(test-re #/^(?=(ab(cd)))(ab)/ "abcd" '("ab" "abcd" "cd" "ab")) |
| 574 |
(test-re #/\w+(?=\t)/ "the quick brown\t fox" '("brown")) |
| 575 |
(test-re #/foo(?!bar)(.*)/ "foobar is foolish see?" '("foolish see?" "lish see?")) |
| 576 |
(test-re #/(?:(?!foo)...|^.{0,2})bar(.*)/ "foobar crowbar etc" '("rowbar etc" " etc")) |
| 577 |
(test-re #/(?:(?!foo)...|^.{0,2})bar(.*)/ "barrel" '("barrel" "rel")) |
| 578 |
(test-re #/(?:(?!foo)...|^.{0,2})bar(.*)/ "2barrel" '("2barrel" "rel")) |
| 579 |
(test-re #/(?:(?!foo)...|^.{0,2})bar(.*)/ "A barrel" '("A barrel" "rel")) |
| 580 |
(test-re #/^(\D*)(?=\d)(?!123)/ "abc456" '("abc" "abc")) |
| 581 |
(test-re #/^(\D*)(?=\d)(?!123)/ "abc123" '()) |
| 582 |
(test-re #/(?!^)abc/ "the abc" '("abc")) |
| 583 |
(test-re #/(?!^)abc/ "abc" '()) |
| 584 |
(test-re #/(?!^^).../ "^^abc" '("^ab")) |
| 585 |
(test-re #/(?!^^).../ "^abc" '("abc")) |
| 586 |
(test-re #/(?=^)abc/ "abc" '("abc")) |
| 587 |
(test-re #/(?=^)abc/ "the abc" '()) |
| 588 |
(test-re #/(?=^^).../ "^abc" '("^ab")) |
| 589 |
(test-re #/(?=^^).../ "^^abc" '("^^a")) |
| 590 |
(test-re #/^(?=$).*/ "" '("")) |
| 591 |
(test-re #/^(?!$).*/ "" '()) |
| 592 |
(test-re #/^(?=$).*/ "a" '()) |
| 593 |
(test-re #/^(?=$$).*/ "$" '("$")) |
| 594 |
(test-re #/^(?!$).*/ "a" '("a")) |
| 595 |
|
| 596 |
(test-re #/(\.\d\d((?=0)|\d(?=\d)))/ "1.230003938" '(".23" ".23" "")) |
| 597 |
(test-re #/(\.\d\d((?=0)|\d(?=\d)))/ "1.875000282" '(".875" ".875" "5")) |
| 598 |
(test-re #/(\.\d\d((?=0)|\d(?=\d)))/ "1.235" '()) |
| 599 |
(test-re #/^\D*(?!123)/ "ABC123" '("AB")) |
| 600 |
(test-re #/^(\D*)(?=\d)(?!123)/ "ABC445" '("ABC" "ABC")) |
| 601 |
(test-re #/^(\D*)(?=\d)(?!123)/ "ABC123" '()) |
| 602 |
(test-re #/a(?!b)./ "abad" '("ad")) |
| 603 |
(test-re #/a(?!^b)./ "a^bad" '("a^")) |
| 604 |
(test-re #/a(?!b)/ "abad" '("a")) |
| 605 |
(test-re #/a(?=d)./ "abad" '("ad")) |
| 606 |
(test-re #/a(?=^b)./ "a^bad" '()) |
| 607 |
(test-re #/a(?=c|d)./ "abad" '("ad")) |
| 608 |
|
| 609 |
;;------------------------------------------------------------------------- |
| 610 |
(test-section "lookbehind assertion") |
| 611 |
(test-re #/(?<=a)b/ "b" '()) |
| 612 |
(test-re #/(?<=a)b/ "ab" '("b")) |
| 613 |
(test-re #/(?<=a+)b/ "aab" '("b")) |
| 614 |
(test-re #/(?<=x[yz])b/ "xzb" '("b")) |
| 615 |
(test-re #/(?<=zyx)b/ "xyzb" '()) |
| 616 |
(test-re #/(?<=[ab]+)c/ "abc" '("c")) |
| 617 |
(test-re #/(?<!<[^>]*)foo/ "<foo>" '()) |
| 618 |
(test-re #/(?<!<[^>]*)foo/ "<bar>foo" '("foo")) |
| 619 |
(test-re #/(?<=^a)b/ "ab" '("b")) |
| 620 |
(test-re #/(?<=^)b/ "ab" '()) |
| 621 |
(test-re #/(?<=^)b/ "b" '("b")) |
| 622 |
(test-re #/.(?<=^)b/ "a^b" '()) |
| 623 |
(test-re #/(?<=^a$)/ "a" '("")) |
| 624 |
(test-re #/(?<=^a$)b/ "a$b" '()) |
| 625 |
(test-re #/(?<=(a))b/ "ab" '("b" "a")) |
| 626 |
(test-re #/(?<=(a)(b))c/ "abc" '("c" "a" "b")) |
| 627 |
(test-re #/(?<=(a)|(b))c/ "bc" '("c" #f "b")) |
| 628 |
(test-re #/(?<=(?<!foo)bar)baz/ "abarbaz" '("baz")) |
| 629 |
(test-re #/(?<=(?<!foo)bar)baz/ "foobarbaz" '()) |
| 630 |
(test-re #/(?<=\d{3})(?<!999)foo/ "865foo" '("foo")) |
| 631 |
(test-re #/(?<=\d{3})(?<!999)foo/ "999foo" '()) |
| 632 |
(test* "(?<=(?>a*))" (test-error) (string->regexp "(?<=(?>a*))")) |
| 633 |
(test-re #/(abc)...(?<=\1)/ "abcabc" '("abcabc" "abc")) |
| 634 |
(test-re #/(abC)...(?<=\1)/i "abCAbc" '("abCAbc" "abC")) |
| 635 |
|
| 636 |
;;------------------------------------------------------------------------- |
| 637 |
(test-section "named group") |
| 638 |
(test-re #/(?<foo>a)/ "a" '("a" "a")) |
| 639 |
(test* "(?<foo>a)" "a" |
| 640 |
(let1 m (#/(?<foo>a)/ "a") |
| 641 |
(m 'foo))) |
| 642 |
(test* "(?<foo>a)" #f |
| 643 |
(let1 m (#/(?<foo>a)?/ "b") |
| 644 |
(m 'foo))) |
| 645 |
(test* "(?<foo>a)(?<bar>.*)" '("a" "bcd") |
| 646 |
(let1 m (#/(?<foo>a)(?<bar>.*)/ "abcd") |
| 647 |
(list (m 'foo) (m 'bar)))) |
| 648 |
(test-re #/(?<foo>a)(?<bar>.*)/ "abcd" '("abcd" "a" "bcd")) |
| 649 |
(test* "(?<foo>a)" (test-error) |
| 650 |
(let1 m (#/(?<foo>a)/ "abcd") |
| 651 |
(m 'bar))) |
| 652 |
(test-re #/(?<foo>^a$)/ "a" '("a" "a")) |
| 653 |
(test-re #/(?<foo>^a$)/ "ab" '()) |
| 654 |
(test-re #/(?<name-with-hyphen>a)/ "a" '("a" "a")) |
| 655 |
(test* "(?<host>\d+.\d+.\d+.\d+)|(?<host>[\w.]+)" |
| 656 |
'("127.0.0.1" "127.0.0.1" #f "127.0.0.1") |
| 657 |
(let1 m (#/(?<host>\d+.\d+.\d+.\d+)|(?<host>[\w.]+)/ "127.0.0.1") |
| 658 |
(list (m 0) (m 1) (m 2) (m 'host)))) |
| 659 |
(test* "(?<host>\d+.\d+.\d+.\d+)|(?<host>[\w.]+)" |
| 660 |
'("foo.com" #f "foo.com" "foo.com") |
| 661 |
(let1 m (#/(?<host>\d+.\d+.\d+.\d+)|(?<host>[\w.]+)/ "foo.com") |
| 662 |
(list (m 0) (m 1) (m 2) (m 'host)))) |
| 663 |
(test-re #/(?<foo>.+)\k<foo>/ "abcabc" '("abcabc" "abc")) |
| 664 |
(test-re #/(?<foo>.+)\k<foo>/ "abcdef" '()) |
| 665 |
|
| 666 |
(test-parse "\\k<foo>" (test-error)) |
| 667 |
(test-before #/(?<foo>def)/ "abcdefghi" "abc" 'foo) |
| 668 |
(test-after #/(?<foo>def)/ "abcdefghi" "ghi" 'foo) |
| 669 |
|
| 670 |
(test* "rxmatch-start" 3 (rxmatch-start (#/(?<foo>def)/ "abcdefghi") 'foo)) |
| 671 |
(test* "rxmatch-end" 6 (rxmatch-end (#/(?<foo>def)/ "abcdefghi") 'foo)) |
| 672 |
|
| 673 |
;;------------------------------------------------------------------------- |
| 674 |
(test-section "conditional subexpression") |
| 675 |
(test-re #/(a)(?(1)b)/ "ab" '("ab" "a")) |
| 676 |
(test-re #/(a)(?(1)b)/ "aa" '()) |
| 677 |
(test-re #/(a)(?(1)b)/ "ac" '()) |
| 678 |
(test-re #/(a)?(?(1)b|c)/ "xb" '()) |
| 679 |
(test-re #/(a)?(?(1)b|c)/ "xc" '("c" #f)) |
| 680 |
(test-re #/(<)?[^<>]+(?(1)>)/ "<foo>" '("<foo>" "<")) |
| 681 |
(test-re #/(<)?[^<>]+(?(1)>)/ "foo" '("foo" #f)) |
| 682 |
(test-re #/(?(?<=a)b)/ "ab" '("")) |
| 683 |
(test-re #/(?(?<=a)b)/ "ac" '("")) |
| 684 |
(test-re #/(?(?<=a)b)/ "xb" '("")) |
| 685 |
(test-parse "(?(?a)b|c)" (test-error)) |
| 686 |
(test-re #/()(?(1))/ "" '("" "")) |
| 687 |
|
| 688 |
(test-parse "()(?(" (test-error)) |
| 689 |
(test-parse "()(?(1" (test-error)) |
| 690 |
(test-parse "()(?(1)b|c|d)" (test-error)) |
| 691 |
|
| 692 |
;;------------------------------------------------------------------------- |
| 693 |
(test-section "rxmatch with start/end") |
| 694 |
|
| 695 |
(define (rxmatch->full-match re str :optional start end) |
| 696 |
(let ([match (rxmatch re str start end)]) |
| 697 |
(and match |
| 698 |
(list (rxmatch-start match) |
| 699 |
(rxmatch-end match) |
| 700 |
(match 0))))) |
| 701 |
|
| 702 |
(test* "abc" '(0 3 "abc") (rxmatch->full-match "abc" "abc")) |
| 703 |
(test* "abc" '(0 3 "abc") (rxmatch->full-match "abc" "abc" 0 3)) |
| 704 |
(test* "abc" '(2 5 "abc") (rxmatch->full-match "abc" "zzabczz" 2)) |
| 705 |
(test* "abc" '(2 5 "abc") (rxmatch->full-match "^abc" "zzabczz" 2 6)) |
| 706 |
(test* "abc" '(3 6 "abc") (rxmatch->full-match "abc$" "zzzabczz" 2 6)) |
| 707 |
(test* "abc" '(5 8 "abc") (rxmatch->full-match "abc" "abczzabczz" 4)) |
| 708 |
|
| 709 |
;;------------------------------------------------------------------------- |
| 710 |
(test-section "regexp macros") |
| 711 |
|
| 712 |
(use gauche.regexp) |
| 713 |
|
| 714 |
(test* "rxmatch-let" '("23:59:58" "23" "59" "58") |
| 715 |
(rxmatch-let (rxmatch #/(\d+):(\d+):(\d+)/ |
| 716 |
"Jan 1 23:59:58, 2001") |
| 717 |
(time hh mm ss) |
| 718 |
(list time hh mm ss))) |
| 719 |
(test* "rxmatch-let" '("23" "59") |
| 720 |
(rxmatch-let (rxmatch #/(\d+):(\d+):(\d+)/ |
| 721 |
"Jan 1 23:59:58, 2001") |
| 722 |
(#f hh mm) |
| 723 |
(list hh mm))) |
| 724 |
|
| 725 |
(test* "rxmatch-if" "time is 11:22" |
| 726 |
(rxmatch-if (rxmatch #/(\d+:\d+)/ "Jan 1 11:22:33") |
| 727 |
(time) |
| 728 |
(format #f "time is ~a" time) |
| 729 |
"unknown time")) |
| 730 |
(test* "rxmatch-if" "unknown time" |
| 731 |
(rxmatch-if (rxmatch #/(\d+:\d+)/ "Jan 1 11-22-33") |
| 732 |
(time) |
| 733 |
(format #f "time is ~a" time) |
| 734 |
"unknown time")) |
| 735 |
|
| 736 |
(define (test-parse-date str) |
| 737 |
(rxmatch-cond |
| 738 |
(test (not (string? str)) #f) |
| 739 |
((rxmatch #/^(\d\d?)\/(\d\d?)\/(\d\d\d\d)$/ str) |
| 740 |
(#f mm dd yyyy) |
| 741 |
(map string->number (list yyyy mm dd))) |
| 742 |
((rxmatch #/^(\d\d\d\d)\/(\d\d?)\/(\d\d?)$/ str) |
| 743 |
(#f yyyy mm dd) |
| 744 |
(map string->number (list yyyy mm dd))) |
| 745 |
((rxmatch #/^\d+\/\d+\/\d+$/ str) |
| 746 |
(#f) |
| 747 |
(error "ambiguous:" str)) |
| 748 |
(else (error "bogus:" str)))) |
| 749 |
|
| 750 |
(test* "rxmatch-cond" '(2001 2 3) |
| 751 |
(test-parse-date "2001/2/3")) |
| 752 |
(test* "rxmatch-cond" '(1999 12 25) |
| 753 |
(test-parse-date "1999/12/25")) |
| 754 |
(test* "rxmatch-cond" #f |
| 755 |
(test-parse-date 'abc)) |
| 756 |
|
| 757 |
(define (test-parse-date2 str) |
| 758 |
(rxmatch-case str |
| 759 |
(test (^s (not (string? s))) #f) |
| 760 |
(#/^(\d\d?)\/(\d\d?)\/(\d\d\d\d)$/ (#f mm dd yyyy) |
| 761 |
(map string->number (list yyyy mm dd))) |
| 762 |
(#/^(\d\d\d\d)\/(\d\d?)\/(\d\d?)$/ (#f yyyy mm dd) |
| 763 |
(map string->number (list yyyy mm dd))) |
| 764 |
(#/^\d+\/\d+\/\d+$/ (#f) (error "ambiguous:" str)) |
| 765 |
(else (error "bogus:" str)))) |
| 766 |
(test* "rxmatch-case" '(2001 2 3) |
| 767 |
(test-parse-date2 "2001/2/3")) |
| 768 |
(test* "rxmatch-case" '(1999 12 25) |
| 769 |
(test-parse-date2 "1999/12/25")) |
| 770 |
(test* "rxmatch-case" #f |
| 771 |
(test-parse-date2 'abc)) |
| 772 |
|
| 773 |
(define (test-parse-date3 str) |
| 774 |
(rxmatch-case str |
| 775 |
(#/^(\d\d\d\d)\/(\d\d?)\/(\d\d?)$/ (#f yyyy mm dd) |
| 776 |
(map string->number (list yyyy mm dd))) |
| 777 |
(else => (cut format "bogus: ~a" <>)))) |
| 778 |
(test* "rxmatch-case (else)" "bogus: 100/2/3" |
| 779 |
(test-parse-date3 "100/2/3")) |
| 780 |
|
| 781 |
(test* "regexp-replace" "abc|def|ghi" |
| 782 |
(regexp-replace #/def|DEF/ "abcdefghi" "|\\0|")) |
| 783 |
|
| 784 |
(test* "regexp-replace" "abc|\\0|ghi" |
| 785 |
(regexp-replace #/def|DEF/ "abcdefghi" "|\\\\0|")) |
| 786 |
|
| 787 |
(test* "regexp-replace" "abc|def|ghi" |
| 788 |
(regexp-replace #/(?<match>def|DEF)/ "abcdefghi" "|\\k<match>|")) |
| 789 |
|
| 790 |
(test* "regexp-replace" (test-error) |
| 791 |
(regexp-replace #/(?<match>def|DEF)/ "abcdefghi" "|\\k<matchee>|")) |
| 792 |
|
| 793 |
(test* "regexp-replace" "abraabra**brabra**brabrabracadabrabrabra" |
| 794 |
(regexp-replace #/a((bra)+)cadabra/ |
| 795 |
"abraabraabrabracadabrabrabrabracadabrabrabra" |
| 796 |
"**\\1**")) |
| 797 |
|
| 798 |
(test* "regexp-replace-all" "abraabra**brabra**br**brabra**brabra" |
| 799 |
(regexp-replace-all #/a((bra)+)cadabra/ |
| 800 |
"abraabraabrabracadabrabrabrabracadabrabrabra" |
| 801 |
"**\\1**")) |
| 802 |
|
| 803 |
(test* "regexp-replace-all" "abraabra**brabra**br**brabra**brabra" |
| 804 |
(regexp-replace-all #/a(?<match>(bra)+)cadabra/ |
| 805 |
"abraabraabrabracadabrabrabrabracadabrabrabra" |
| 806 |
"**\\k<match>**")) |
| 807 |
|
| 808 |
(test* "regexp-replace" "abfedhi" |
| 809 |
(regexp-replace #/c(.*)g/ "abcdefghi" |
| 810 |
(lambda (m) |
| 811 |
(list->string |
| 812 |
(reverse |
| 813 |
(string->list (rxmatch-substring m 1))))))) |
| 814 |
|
| 815 |
(test* "regexp-replace-all" "abraabra(bra^2)br(bra^2)brabra" |
| 816 |
(regexp-replace-all #/a((bra)+)cadabra/ |
| 817 |
"abraabraabrabracadabrabrabrabracadabrabrabra" |
| 818 |
(lambda (m) |
| 819 |
(format #f "(bra^~a)" |
| 820 |
(/ (string-length (rxmatch-substring m 1)) |
| 821 |
3))))) |
| 822 |
|
| 823 |
(test* "regexp-replace-all" "XaXbXcXdXeXfX" |
| 824 |
(regexp-replace-all #/\d*/ "abcdef" "X")) |
| 825 |
(test* "regexp-replace-all" "XXaXbXcXXdXeXfX" |
| 826 |
(regexp-replace-all #/\d*/ "123abc45def" "X")) |
| 827 |
|
| 828 |
(test* "regexp-replace*" "cbazzbc" |
| 829 |
(regexp-replace* "abcbabc" |
| 830 |
#/abc/ "cba" |
| 831 |
#/aba/ "abc" |
| 832 |
#/bc/ "zz")) |
| 833 |
|
| 834 |
(test* "regexp-replace-all*" "cbazzccbazz" |
| 835 |
(regexp-replace-all* "abcbacabcbc" |
| 836 |
#/abc/ "cba" |
| 837 |
#/aba/ "abc" |
| 838 |
#/bc/ "zz")) |
| 839 |
|
| 840 |
;; Substitution procedure's current-output-port. |
| 841 |
;; https://github.com/shirok/Gauche/issues/967 |
| 842 |
(test* "regexp-replace and current-output-port" |
| 843 |
'("ac" "yo") |
| 844 |
(let* ((r #f) |
| 845 |
(out (with-output-to-string |
| 846 |
(^[] |
| 847 |
(set! r (regexp-replace #/b/ "ab" (^_ (display "yo") "c"))))))) |
| 848 |
(list r out))) |
| 849 |
|
| 850 |
;;------------------------------------------------------------------------- |
| 851 |
(test-section "regexp cimatch") |
| 852 |
|
| 853 |
(test* "regexp/ci" "BC" |
| 854 |
(cond ((rxmatch #/bc/i "ABCD") => rxmatch-substring) |
| 855 |
(else #f))) |
| 856 |
(test* "regexp/ci" "bC" |
| 857 |
(cond ((rxmatch #/Bc/i "AbCD") => rxmatch-substring) |
| 858 |
(else #f))) |
| 859 |
(test* "regexp/ci" "bc" |
| 860 |
(cond ((rxmatch #/BC/i "AbcD") => rxmatch-substring) |
| 861 |
(else #f))) |
| 862 |
(test* "regexp/ci" #f |
| 863 |
(cond ((rxmatch #/Bc/ "AbCD") => rxmatch-substring) |
| 864 |
(else #f))) |
| 865 |
(test* "regexp/ci" #f |
| 866 |
(cond ((rxmatch #/BC/ "ABcD") => rxmatch-substring) |
| 867 |
(else #f))) |
| 868 |
(test* "regexp/ci" "PAD" |
| 869 |
(cond ((rxmatch #/p[a-z]d/i "PAD") => rxmatch-substring) |
| 870 |
(else #f))) |
| 871 |
(test* "regexp/ci" "pad" |
| 872 |
(cond ((rxmatch #/P[A-Z]D/i "pad") => rxmatch-substring) |
| 873 |
(else #f))) |
| 874 |
(test* "regexp/ci" "bad" |
| 875 |
(cond ((rxmatch #/.[a-pQ-Z][A-Pq-z]/i "bad") => rxmatch-substring) |
| 876 |
(else #f))) |
| 877 |
(test* "regexp/ci" #f |
| 878 |
(cond ((rxmatch #/p[a-z]d/ "PAD") => rxmatch-substring) |
| 879 |
(else #f))) |
| 880 |
(test* "regexp/ci" #f |
| 881 |
(cond ((rxmatch #/P[A-Z]D/ "pad") => rxmatch-substring) |
| 882 |
(else #f))) |
| 883 |
(test* "regexp/ci" #f |
| 884 |
(cond ((rxmatch #/.[a-pQ-Z][A-Pq-z]/ "bad") => rxmatch-substring) |
| 885 |
(else #f))) |
| 886 |
(test* "regexp/ci" "pad" |
| 887 |
(cond ((rxmatch (string->regexp "p[A-Z]d" :case-fold #t) "pad") |
| 888 |
=> rxmatch-substring) |
| 889 |
(else #f))) |
| 890 |
(test* "regexp/ci" #f |
| 891 |
(cond ((rxmatch (string->regexp "p[A-Z]d") "pad") |
| 892 |
=> rxmatch-substring) |
| 893 |
(else #f))) |
| 894 |
|
| 895 |
;;------------------------------------------------------------------------- |
| 896 |
(test-section "applicable regexp") |
| 897 |
|
| 898 |
(define match #f) |
| 899 |
|
| 900 |
(test* "object-apply regexp" #t |
| 901 |
(begin |
| 902 |
(set! match (#/(\d+)\.(\d+)/ "pi=3.14...")) |
| 903 |
(regmatch? match))) |
| 904 |
|
| 905 |
(test* "object-apply regmatch (index)" '("3.14" "3" "14") |
| 906 |
(list (match) (match 1) (match 2))) |
| 907 |
|
| 908 |
(test* "object-apply regmatch (symbol)" '("..." ".14..." "pi=" "pi=3.") |
| 909 |
(list (match 'after) (match 'after 1) |
| 910 |
(match 'before) (match 'before 2))) |
| 911 |
|
| 912 |
(set! match (#/(?<int>\d+)\.(?<frac>\d+)/ "pi=3.14...")) |
| 913 |
|
| 914 |
(test* "object-apply regmatch (index)" '("3.14" "3" "14" "3" "14") |
| 915 |
(list (match) (match 1) (match 2) |
| 916 |
(match 'int) (match 'frac))) |
| 917 |
|
| 918 |
(test* "object-apply regmatch (symbol)" |
| 919 |
'("..." ".14..." ".14..." "pi=" "pi=3." "pi=3.") |
| 920 |
(list (match 'after) (match 'after 1) (match 'after 'int) |
| 921 |
(match 'before) (match 'before 2) (match 'before 'frac))) |
| 922 |
|
| 923 |
(test* "object-apply regmatch (named submatch)" |
| 924 |
'("..." ".14..." ".14..." "pi=" "pi=3." "pi=3.") |
| 925 |
(list (match 'after) (match 'after 1) (match 'after 'int) |
| 926 |
(match 'before) (match 'before 2) (match 'before 'frac))) |
| 927 |
|
| 928 |
;;------------------------------------------------------------------------- |
| 929 |
(test-section "regexp quote") |
| 930 |
|
| 931 |
(test* "regexp-quote" #t |
| 932 |
(let ((str "^(#$%#}{)\\-+?^$")) |
| 933 |
(regmatch? (rxmatch (string->regexp (regexp-quote str)) str)))) |
| 934 |
|
| 935 |
;;------------------------------------------------------------------------- |
| 936 |
(test-section "regexp comparison") |
| 937 |
|
| 938 |
(test* "equal #/abc/ #/abc/" #t |
| 939 |
(equal? #/abc/ #/abc/)) |
| 940 |
(test* "not equal #/abc/ #/a(bc)/" #f |
| 941 |
(equal? #/abc/ #/a(bc)/)) |
| 942 |
(test* "not equal #/abc/ #/abc/i" #f |
| 943 |
(equal? #/abc/ #/abc/i)) |
| 944 |
(test* "not equal #/abc/i #/abc/" #f |
| 945 |
(equal? #/abc/i #/abc/)) |
| 946 |
(test* "equal #/abc/i #/abc/i" #t |
| 947 |
(equal? #/abc/i #/abc/i)) |
| 948 |
|
| 949 |
;;------------------------------------------------------------------------- |
| 950 |
(test-section "regexp printer") |
| 951 |
|
| 952 |
(let () |
| 953 |
(define (test-regexp-writer exp pat) |
| 954 |
(test* exp exp (write-to-string (string->regexp pat)))) |
| 955 |
|
| 956 |
(test-regexp-writer "#/abc/" "abc") |
| 957 |
(test-regexp-writer "#/a[0-9]/" "a[\\d]") |
| 958 |
(test-regexp-writer "#/a[a-z]/" "a[a-z]") |
| 959 |
(test-regexp-writer "#/a\\/b/" "a/b") |
| 960 |
(test-regexp-writer "#/\\\\/" "\\\\") |
| 961 |
(test-regexp-writer "#/ /" "\x20") |
| 962 |
(test-regexp-writer "#/ /" "\\x20") |
| 963 |
(test-regexp-writer "#/\\//" "\\/") ; single '/' |
| 964 |
(test-regexp-writer "#/\\\\\\//" "\\\\/") ; backslash, then '/' |
| 965 |
(test-regexp-writer "#/\\\\\\//" "\\\\\\/") ; backslash, then '/' |
| 966 |
) |
| 967 |
|
| 968 |
(test* "#/(?i:abc)/" "#/(?i:abc)/" |
| 969 |
(write-to-string (string->regexp "abc" :case-fold #t))) |
| 970 |
|
| 971 |
;;------------------------------------------------------------------------- |
| 972 |
(test-section "regexp from AST") |
| 973 |
|
| 974 |
;; A trivial cases are already covered by the preceding tests. |
| 975 |
;; We test some edge cases. |
| 976 |
|
| 977 |
;; empty alt clause always fails. |
| 978 |
(test* "empty alt" #f |
| 979 |
(rxmatch (regexp-compile '(alt)) "a")) |
| 980 |
|
| 981 |
;; empty alt clause can't be represented by '|' operator. When unparsed, |
| 982 |
;; we use negative lookahead assertion to represent "always fail" op. |
| 983 |
(test* "empty alt unparse" "ab(?!)cd" |
| 984 |
(regexp->string (regexp-compile '(seq #\a #\b (alt) #\c #\d)))) |
| 985 |
|
| 986 |
;; a bit twisted way to test "always fail" pattern. The first branch |
| 987 |
;; matches to "abc" but fails at the empty alt, so the second branch is tried. |
| 988 |
(test* "empty alt" "ab" |
| 989 |
(rxmatch->string (regexp-compile '(alt (seq #\a #\b #\c (alt)) |
| 990 |
(seq #\a #\b))) |
| 991 |
"abc")) |
| 992 |
|
| 993 |
;;------------------------------------------------------------------------- |
| 994 |
(test-section "SRE to AST transformation") |
| 995 |
|
| 996 |
(use gauche.regexp.sre) |
| 997 |
|
| 998 |
(define-syntax test-ast |
| 999 |
(syntax-rules () |
| 1000 |
[(_ expected regexp) |
| 1001 |
(test* 'regexp |
| 1002 |
'(0 #f expected) |
| 1003 |
(regexp-parse-sre 'regexp))])) |
| 1004 |
|
| 1005 |
(define-syntax test-cset |
| 1006 |
(syntax-rules () |
| 1007 |
[(_ expected regexp) |
| 1008 |
(test* 'regexp |
| 1009 |
(list 0 #f expected) |
| 1010 |
(regexp-parse-sre 'regexp))])) |
| 1011 |
|
| 1012 |
(define-syntax test-ast-error |
| 1013 |
(syntax-rules () |
| 1014 |
[(_ regexp) |
| 1015 |
(test* 'regexp |
| 1016 |
(test-error) |
| 1017 |
(regexp-parse-sre 'regexp))])) |
| 1018 |
|
| 1019 |
;; <cset-sre> part |
| 1020 |
|
| 1021 |
(test-cset (string->char-set "a") (& ascii #\a)) |
| 1022 |
(test-cset (string->char-set "a") (& ascii "a")) |
| 1023 |
(test-ast-error (& ascii "ab")) |
| 1024 |
(test-cset (string->char-set "abc") ("abc")) |
| 1025 |
(test-ast-error ("abc" "def")) |
| 1026 |
(test-cset (string->char-set "abc") (char-set "abc")) |
| 1027 |
(test-ast-error (char-set "abc" "def")) |
| 1028 |
(test* "<char-set>" `(0 #f ,char-set:ascii) (regexp-parse-sre char-set:ascii)) |
| 1029 |
(test-cset (string->char-set "abc") ("abc")) |
| 1030 |
(test-ast-error ("abc" "def")) |
| 1031 |
(test-cset (string->char-set "ABC") (/ "AC")) |
| 1032 |
(test-cset (string->char-set "ABC") (char-range "AC")) |
| 1033 |
(test-cset (string->char-set "ABC") (/ "A" #\C)) |
| 1034 |
(test-cset (string->char-set "") (/)) |
| 1035 |
(test-cset (string->char-set "ab") (or #\a #\b)) |
| 1036 |
(test-cset (string->char-set "a") (and ascii #\a)) |
| 1037 |
(test-cset (string->char-set "b") (- (or #\a #\b) #\a)) |
| 1038 |
(test-cset `(comp . ,(string->char-set "a")) (complement #\a)) |
| 1039 |
(test-cset `(comp . ,(string->char-set "ab")) (~ #\a #\b)) |
| 1040 |
(test-cset (string->char-set "bc") (and ("abc") (~ #\a))) |
| 1041 |
|
| 1042 |
(test-cset (string->char-set "BC") (& ("ABC") (w/case ("aBC")))) |
| 1043 |
(test-ast-error (& ("ABC") (w/case ("aBC") "def"))) |
| 1044 |
(test-cset (string->char-set "ABC") (& ("ABC") (w/nocase ("aBC")))) |
| 1045 |
(test-ast-error (& ("ABC") (w/nocase ("aBC") "def"))) |
| 1046 |
(test-cset (string->char-set "BC") (& ("ABC") (w/unicode ("aBC")))) |
| 1047 |
(test-ast-error (& ("ABC") (w/unicode ("aBC") "def"))) |
| 1048 |
|
| 1049 |
(test-cset char-set:ascii ascii) |
| 1050 |
(test-ast any any) |
| 1051 |
(test-cset `(comp . ,(string->char-set "\n\r")) nonl) |
| 1052 |
(test-cset char-set:lower-case lower-case) |
| 1053 |
(test-cset char-set:lower-case lower) |
| 1054 |
(test-cset char-set:upper-case upper-case) |
| 1055 |
(test-cset char-set:upper-case upper) |
| 1056 |
(test-cset char-set:title-case title-case) |
| 1057 |
(test-cset char-set:title-case title) |
| 1058 |
(test-cset char-set:letter alphabetic) |
| 1059 |
(test-cset char-set:letter alpha) |
| 1060 |
(test-cset char-set:letter+digit alphanumeric) |
| 1061 |
(test-cset char-set:letter+digit alphanum) |
| 1062 |
(test-cset char-set:letter+digit alnum) |
| 1063 |
(test-cset char-set:digit numeric) |
| 1064 |
(test-cset char-set:digit num) |
| 1065 |
(test-cset char-set:punctuation punctuation) |
| 1066 |
(test-cset char-set:punctuation punct) |
| 1067 |
(test-cset char-set:symbol symbol) |
| 1068 |
(test-cset char-set:graphic graphic) |
| 1069 |
(test-cset char-set:graphic graph) |
| 1070 |
(test-cset char-set:whitespace whitespace) |
| 1071 |
(test-cset char-set:whitespace white) |
| 1072 |
(test-cset char-set:whitespace space) |
| 1073 |
(test-cset char-set:printing printing) |
| 1074 |
(test-cset char-set:printing print) |
| 1075 |
(test-cset (ucs-range->char-set 0 32) control) |
| 1076 |
(test-cset (ucs-range->char-set 0 32) cntrl) |
| 1077 |
(test-cset char-set:hex-digit hex-digit) |
| 1078 |
(test-cset char-set:hex-digit xdigit) |
| 1079 |
|
| 1080 |
;; <sre> part |
| 1081 |
|
| 1082 |
(test-ast (seq #\s #\t #\r) "str") |
| 1083 |
(test-ast (rep 0 #f #\a) (* #\a)) |
| 1084 |
(test-ast (rep 0 #f #\a) (zero-or-more #\a)) |
| 1085 |
|
| 1086 |
(test-ast (rep 1 #f #\a) (+ #\a)) |
| 1087 |
(test-ast (rep 1 #f #\a) (one-or-more #\a)) |
| 1088 |
|
| 1089 |
(test-ast (rep 0 1 #\a) (? #\a)) |
| 1090 |
(test-ast (rep 0 1 #\a) (optional #\a)) |
| 1091 |
|
| 1092 |
(test-ast (rep 3 3 #\a) (= 3 #\a)) |
| 1093 |
(test-ast (rep 3 3 #\a) (exactly 3 #\a)) |
| 1094 |
|
| 1095 |
(test-ast (rep 3 #f #\a) (>= 3 #\a)) |
| 1096 |
(test-ast (rep 3 #f #\a) (at-least 3 #\a)) |
| 1097 |
|
| 1098 |
(test-ast (rep 3 5 #\a) (** 3 5 #\a)) |
| 1099 |
(test-ast (rep 3 5 #\a) (repeated 3 5 #\a)) |
| 1100 |
|
| 1101 |
(test-ast (alt (seq #\a #\b) (seq #\c #\d)) |
| 1102 |
(|\|| "ab" "cd")) |
| 1103 |
(test-ast (alt (seq #\a #\b) (seq #\c #\d)) |
| 1104 |
(or "ab" "cd")) |
| 1105 |
|
| 1106 |
(test-ast (seq (seq #\a #\b) (seq #\c #\d)) |
| 1107 |
(: "ab" "cd")) |
| 1108 |
(test-ast (seq (seq #\a #\b) (seq #\c #\d)) |
| 1109 |
(seq "ab" "cd")) |
| 1110 |
|
| 1111 |
(test-ast (1 #f |
| 1112 |
(2 #f (seq #\a #\b #\c)) |
| 1113 |
(3 #f (seq #\d #\e #\f)) |
| 1114 |
(seq #\g #\h #\i)) |
| 1115 |
($ ($ "abc") ($ "def") "ghi")) |
| 1116 |
|
| 1117 |
(test-ast (1 result (seq #\a #\b #\c)) (-> result "abc")) |
| 1118 |
|
| 1119 |
(test-ast (seq (seq (seq (seq #\a #\b #\c)) |
| 1120 |
(seq (seq #\d #\e #\f)) |
| 1121 |
(seq #\g #\h #\i))) |
| 1122 |
(w/nocapture ($ ($ "abc") ($ "def") "ghi"))) |
| 1123 |
|
| 1124 |
(test-ast (seq (seq #\a #\b)) (w/case "ab")) |
| 1125 |
(test-cset `(seq (seq ,(string->char-set "Aa") |
| 1126 |
,(string->char-set "Bb"))) |
| 1127 |
(w/nocase "ab")) |
| 1128 |
(test-ast (seq (seq (seq #\a #\b))) (w/nocase (w/case "ab"))) |
| 1129 |
(test-ast (seq (seq #[Aa] #[Bb]) (seq (seq #\c #\d)) (seq #[Ee] #[Ff])) |
| 1130 |
(w/nocase "ab" (w/case "cd") "ef")) |
| 1131 |
|
| 1132 |
(test-ast bol bol) |
| 1133 |
(test-ast eol eol) |
| 1134 |
(test-ast bow bow) |
| 1135 |
(test-ast eow eow) |
| 1136 |
(test-ast nwb nwb) |
| 1137 |
|
| 1138 |
(test-ast (seq bow (seq #\a #\b #\c) eow) (word "abc")) |
| 1139 |
(test-ast (seq bow (rep 1 #f #[a-c]) eow) (word+ ("a**bc"))) |
| 1140 |
(test-cset `(seq bow |
| 1141 |
(rep 1 #f |
| 1142 |
,(char-set-union char-set:letter+digit |
| 1143 |
(string->char-set "_"))) |
| 1144 |
eow) |
| 1145 |
word) |
| 1146 |
|
| 1147 |
(test-ast (rep-min 0 1 #\a) (?? #\a)) |
| 1148 |
(test-ast (rep-min 0 1 #\a) (non-greedy-optional #\a)) |
| 1149 |
|
| 1150 |
(test-ast (rep-min 0 #f #\a) (*? #\a)) |
| 1151 |
(test-ast (rep-min 0 #f #\a) (non-greedy-zero-or-more #\a)) |
| 1152 |
|
| 1153 |
(test-ast (rep-min 3 5 #\a) (**? 3 5 #\a)) |
| 1154 |
(test-ast (rep-min 3 5 #\a) (non-greedy-repeated 3 5 #\a)) |
| 1155 |
|
| 1156 |
(test-ast (assert #\a #\b #\c) (look-ahead #\a #\b #\c)) |
| 1157 |
(test-ast (assert (lookbehind #\a #\b #\c)) (look-behind #\a #\b #\c)) |
| 1158 |
|
| 1159 |
(test-ast (nassert #\a #\b #\c) (neg-look-ahead #\a #\b #\c)) |
| 1160 |
(test-ast (nassert (lookbehind #\a #\b #\c)) (neg-look-behind #\a #\b #\c)) |
| 1161 |
|
| 1162 |
(test-ast #\a #\a) |
| 1163 |
(test-ast (seq #\a #\b #\c) "abc") |
| 1164 |
|
| 1165 |
(test* "atomic sre -> once ast" |
| 1166 |
'(0 #f (once (seq #\f #\o) (seq #\o))) |
| 1167 |
(regexp-parse-sre '(atomic "fo" "o"))) |
| 1168 |
|
| 1169 |
(test* "once ast -> atomic sre" |
| 1170 |
'(seq (atomic #\f #\o #\o)) |
| 1171 |
(regexp-unparse-sre (regexp-ast #/(?>foo)/))) |
| 1172 |
|
| 1173 |
|
| 1174 |
(test* "if-look-ahead sre -> cpat/assert ast" |
| 1175 |
'(0 #f (cpat (assert (seq #\t #\e #\s #\t)) |
| 1176 |
((seq #\t #\h #\e #\n)) |
| 1177 |
((seq #\e #\l #\s #\e)))) |
| 1178 |
(regexp-parse-sre '(if-look-ahead "test" "then" "else"))) |
| 1179 |
|
| 1180 |
(test* "cpat/assert ast -> if-look-ahead sre" |
| 1181 |
'(seq (if-look-ahead (seq #\t #\e #\s #\t) |
| 1182 |
(seq (seq #\t #\h #\e #\n)) |
| 1183 |
(seq (seq #\e #\l #\s #\e)))) |
| 1184 |
(regexp-unparse-sre (regexp-ast #/(?(?=test)then|else)/))) |
| 1185 |
|
| 1186 |
(test* "if-look-ahead sre -> cpat/assert ast, no else pattern" |
| 1187 |
'(0 #f (cpat (assert (seq #\t #\e #\s #\t)) |
| 1188 |
((seq #\t #\h #\e #\n)) |
| 1189 |
())) |
| 1190 |
(regexp-parse-sre '(if-look-ahead "test" "then"))) |
| 1191 |
|
| 1192 |
(test* "cpat/assert ast -> if-look-ahead sre, no else pattern" |
| 1193 |
'(seq (if-look-ahead (seq #\t #\e #\s #\t) |
| 1194 |
(seq #\t #\h #\e #\n) |
| 1195 |
(seq))) |
| 1196 |
(regexp-unparse-sre (regexp-ast #/(?(?=test)then)/))) |
| 1197 |
|
| 1198 |
(test* "if-neg-look-ahead sre -> cpat/nassert ast" |
| 1199 |
'(0 #f (cpat (nassert (seq #\t #\e #\s #\t)) |
| 1200 |
((seq #\t #\h #\e #\n)) |
| 1201 |
((seq #\e #\l #\s #\e)))) |
| 1202 |
(regexp-parse-sre '(if-neg-look-ahead "test" "then" "else"))) |
| 1203 |
|
| 1204 |
(test* "cpat/nassert ast -> if-neg-look-ahead sre" |
| 1205 |
'(seq (if-neg-look-ahead (seq #\t #\e #\s #\t) |
| 1206 |
(seq (seq #\t #\h #\e #\n)) |
| 1207 |
(seq (seq #\e #\l #\s #\e)))) |
| 1208 |
(regexp-unparse-sre (regexp-ast #/(?(?!test)then|else)/))) |
| 1209 |
|
| 1210 |
(test* "if-neg-look-ahead sre -> cpat/nassert ast, no else pattern" |
| 1211 |
'(0 #f (cpat (nassert (seq #\t #\e #\s #\t)) |
| 1212 |
((seq #\t #\h #\e #\n)) |
| 1213 |
())) |
| 1214 |
(regexp-parse-sre '(if-neg-look-ahead "test" "then"))) |
| 1215 |
|
| 1216 |
(test* "cpat/nassert ast -> if-neg-look-ahead sre, no else pattern" |
| 1217 |
'(seq (if-neg-look-ahead (seq #\t #\e #\s #\t) |
| 1218 |
(seq #\t #\h #\e #\n) |
| 1219 |
(seq))) |
| 1220 |
(regexp-unparse-sre (regexp-ast #/(?(?!test)then)/))) |
| 1221 |
|
| 1222 |
(test* "if-look-behind sre -> cpat/lookbehind ast" |
| 1223 |
'(0 #f (cpat (assert (lookbehind (seq #\t #\e #\s #\t))) |
| 1224 |
((seq #\t #\h #\e #\n)) |
| 1225 |
((seq #\e #\l #\s #\e)))) |
| 1226 |
(regexp-parse-sre '(if-look-behind "test" "then" "else"))) |
| 1227 |
|
| 1228 |
(test* "cpat/lookbehind ast -> if-look-behind sre" |
| 1229 |
'(seq (if-look-behind (seq #\t #\e #\s #\t) |
| 1230 |
(seq (seq #\t #\h #\e #\n)) |
| 1231 |
(seq (seq #\e #\l #\s #\e)))) |
| 1232 |
(regexp-unparse-sre (regexp-ast #/(?(?<=test)then|else)/))) |
| 1233 |
|
| 1234 |
(test* "if-neg-look-behind sre -> cpat/lookbehind ast" |
| 1235 |
'(0 #f (cpat (nassert (lookbehind (seq #\t #\e #\s #\t))) |
| 1236 |
((seq #\t #\h #\e #\n)) |
| 1237 |
((seq #\e #\l #\s #\e)))) |
| 1238 |
(regexp-parse-sre '(if-neg-look-behind "test" "then" "else"))) |
| 1239 |
|
| 1240 |
(test* "cpat/lookbehind ast -> if-neg-look-behind sre" |
| 1241 |
'(seq (if-neg-look-behind (seq #\t #\e #\s #\t) |
| 1242 |
(seq (seq #\t #\h #\e #\n)) |
| 1243 |
(seq (seq #\e #\l #\s #\e)))) |
| 1244 |
(regexp-unparse-sre (regexp-ast #/(?(?<!test)then|else)/))) |
| 1245 |
|
| 1246 |
(test* "if-backref -> cpat/num ast" |
| 1247 |
'(0 #f (cpat 1 |
| 1248 |
((seq #\t #\h #\e #\n)) |
| 1249 |
((seq #\e #\l #\s #\e)))) |
| 1250 |
(regexp-parse-sre '(if-backref 1 "then" "else"))) |
| 1251 |
|
| 1252 |
(test* "cpat/num ast -> if-backref sre" |
| 1253 |
'(seq (if-backref 1 |
| 1254 |
(seq (seq #\t #\h #\e #\n)) |
| 1255 |
(seq (seq #\e #\l #\s #\e)))) |
| 1256 |
(regexp-unparse-sre (regexp-ast #/(?(1)then|else)/))) |
| 1257 |
|
| 1258 |
|
| 1259 |
;;------------------------------------------------------------------------- |
| 1260 |
(test-section "regexp-unparse-sre") |
| 1261 |
|
| 1262 |
(define (test-regexp-unparse-sre src) |
| 1263 |
(let1 ast (regexp-optimize (regexp-parse-sre src)) |
| 1264 |
(test* (format "regexp-unparse-sre ~s" src) ast |
| 1265 |
(regexp-optimize (regexp-parse-sre (regexp-unparse-sre ast))) |
| 1266 |
equal?))) |
| 1267 |
|
| 1268 |
(for-each test-regexp-unparse-sre |
| 1269 |
'(;; simple ones |
| 1270 |
"" "a" "ab" (: #\a #\b) (* #\a) (+ "a") (? "a") (: "a" (* "b")) |
| 1271 |
(: "a" (+ "b")) (: "a" (? "b")) (: (* "a") "b") (: (+ "a") "b") |
| 1272 |
(: (? "a") "b") (= 3 "a") (** 2 4 "a") (>= 3 #\a) (or "a" "b") |
| 1273 |
(or "a" "b" "c") (: bol "ab") (: bol (or #\a #\b)) (or (: bol "a") "b") |
| 1274 |
(: bol (or "a" "b") eol) any #\. (: (* #\\) #\*) (seq bow "foo" eow) |
| 1275 |
;; charset |
| 1276 |
(/ "az") (~ (/ #\a #\z)) (+ (/ "az")) ("a^ef-") ("a") (".") |
| 1277 |
;; grouping |
| 1278 |
(: ($ "a") ($ "b")) (: ($ (or "a" "b")) "c" ($ "d")) |
| 1279 |
(: ($ "a" ($ "b" ($ (or "c" "d")) "e")) "f") |
| 1280 |
(* (w/nocapture (: "abc"))) |
| 1281 |
(w/nocase "ab" (w/case "cd") "ef") |
| 1282 |
;; backref |
| 1283 |
(: ($ "a") "bc" (backref 1)) |
| 1284 |
(: "ab" (-> foo "c") ($ "d") (backref foo)) |
| 1285 |
($ ($ ".") (backref 2)) |
| 1286 |
;; lookahead, lookbehind |
| 1287 |
(look-ahead (: (* "a") (* #\b) "c")) |
| 1288 |
(neg-look-ahead (* #\a) (* #\b) #\c) |
| 1289 |
(look-behind #\a ("bc")) |
| 1290 |
(neg-look-behind #\a ("bc")))) |
| 1291 |
|
| 1292 |
;;------------------------------------------------------------------------- |
| 1293 |
(test-section "SRE") ; based on chibi's regexp-test.sld |
| 1294 |
|
| 1295 |
(define (test-sre expected sre input) |
| 1296 |
(test* sre |
| 1297 |
expected |
| 1298 |
(let ([result (rxmatch (sre->regexp sre) input)]) |
| 1299 |
(and result |
| 1300 |
(map (cut result <>) |
| 1301 |
(iota (rxmatch-num-matches result))))))) |
| 1302 |
|
| 1303 |
(define (test-sre-named expected sre input) |
| 1304 |
(test* sre |
| 1305 |
expected |
| 1306 |
(let ([result (rxmatch (sre->regexp sre) input)]) |
| 1307 |
(and result (rxmatch-named-groups result))))) |
| 1308 |
|
| 1309 |
(test-sre '("ababc" "abab") |
| 1310 |
'(: ($ (* "ab")) "c") |
| 1311 |
"ababc") |
| 1312 |
|
| 1313 |
(test-sre '("y") '(: "y") "xy") |
| 1314 |
|
| 1315 |
(test-sre #f |
| 1316 |
'(: (* any) ($ "foo" (* any)) ($ "bar" (* any))) |
| 1317 |
"fooxbafba") |
| 1318 |
|
| 1319 |
(test-sre '("fooxbarfbar" "fooxbarf" "bar") |
| 1320 |
'(: (* any) ($ "foo" (* any)) ($ "bar" (* any))) |
| 1321 |
"fooxbarfbar") |
| 1322 |
|
| 1323 |
(test-sre '("abcd" "abcd") |
| 1324 |
'($ (* (or "ab" "cd"))) |
| 1325 |
"abcd") |
| 1326 |
|
| 1327 |
(test* '(or (-> foo "ab") (-> foo "cd")) |
| 1328 |
"ab" |
| 1329 |
((rxmatch |
| 1330 |
(sre->regexp |
| 1331 |
'(or (-> foo "ab") (-> foo "cd"))) |
| 1332 |
"ab") |
| 1333 |
'foo)) |
| 1334 |
|
| 1335 |
(test* '(or (-> foo "ab") (-> foo "cd")) |
| 1336 |
"cd" |
| 1337 |
((rxmatch |
| 1338 |
(sre->regexp |
| 1339 |
'(or (-> foo "ab") (-> foo "cd"))) |
| 1340 |
"cd") |
| 1341 |
'foo)) |
| 1342 |
|
| 1343 |
(test-sre '("ababc" "abab") |
| 1344 |
'(: bos ($ (* "ab")) "c") |
| 1345 |
"ababc") |
| 1346 |
(test-sre '("ababc" "abab") |
| 1347 |
'(: ($ (* "ab")) "c" eos) |
| 1348 |
"ababc") |
| 1349 |
(test-sre '("ababc" "abab") |
| 1350 |
'(: bos ($ (* "ab")) "c" eos) |
| 1351 |
"ababc") |
| 1352 |
(test-sre #f |
| 1353 |
'(: bos ($ (* "ab")) eos "c") |
| 1354 |
"ababc") |
| 1355 |
(test-sre #f |
| 1356 |
'(: ($ (* "ab")) bos "c" eos) |
| 1357 |
"ababc") |
| 1358 |
|
| 1359 |
(test-sre '("ababc" "abab") |
| 1360 |
'(: bol ($ (* "ab")) "c") |
| 1361 |
"ababc") |
| 1362 |
(test-sre '("ababc" "abab") |
| 1363 |
'(: ($ (* "ab")) "c" eol) |
| 1364 |
"ababc") |
| 1365 |
(test-sre '("ababc" "abab") |
| 1366 |
'(: bol ($ (* "ab")) "c" eol) |
| 1367 |
"ababc") |
| 1368 |
(test-sre #f |
| 1369 |
'(: bol ($ (* "ab")) eol "c") |
| 1370 |
"ababc") |
| 1371 |
(test-sre #f |
| 1372 |
'(: ($ (* "ab")) bol "c" eol) |
| 1373 |
"ababc") |
| 1374 |
(test-sre '(".\nabc\n" "abc") |
| 1375 |
'(: #\. (* #\newline) bol ($ (* alpha)) eol (* #\newline)) |
| 1376 |
".\nabc\n") |
| 1377 |
;; note that chibi scheme has similar tests, but due to the |
| 1378 |
;; non-tracking nature (?) of chibi regex engine, the results are both |
| 1379 |
;; false (without the literal dot) instead of matching here. |
| 1380 |
(test-sre #f |
| 1381 |
'(: #\. (* #\newline) bol ($ (* alpha)) eol (* #\newline)) |
| 1382 |
".\n'abc\n") |
| 1383 |
(test-sre '(".\n" "\n" "") |
| 1384 |
'(: #\. ($ (* #\newline)) bol ($ (* alpha)) eol (* #\newline)) |
| 1385 |
".\nabc.\n") |
| 1386 |
|
| 1387 |
(test-sre '("ababc" "abab") |
| 1388 |
'(: bow ($ (* "ab")) "c") |
| 1389 |
"ababc") |
| 1390 |
(test-sre '("ababc" "abab") |
| 1391 |
'(: ($ (* "ab")) "c" eow) |
| 1392 |
"ababc") |
| 1393 |
(test-sre '("ababc" "abab") |
| 1394 |
'(: bow ($ (* "ab")) "c" eow) |
| 1395 |
"ababc") |
| 1396 |
(test-sre #f |
| 1397 |
'(: bow ($ (* "ab")) eow "c") |
| 1398 |
"ababc") |
| 1399 |
(test-sre #f |
| 1400 |
'(: ($ (* "ab")) bow "c" eow) |
| 1401 |
"ababc") |
| 1402 |
(test-sre '(" abc " "abc") |
| 1403 |
'(: (* space) bow ($ (* alpha)) eow (* space)) |
| 1404 |
" abc ") |
| 1405 |
|
| 1406 |
(test-sre '("abc " "abc") |
| 1407 |
'(: ($ (* alpha)) (* any)) |
| 1408 |
"abc ") |
| 1409 |
(test-sre '("abc " "") |
| 1410 |
'(: ($ (*? alpha)) (* any)) |
| 1411 |
"abc ") |
| 1412 |
(test-sre '("<em>Hello World</em>" "em>Hello World</em") |
| 1413 |
'(: "<" ($ (* any)) ">" (* any)) |
| 1414 |
"<em>Hello World</em>") |
| 1415 |
(test-sre '("<em>Hello World</em>" "em") |
| 1416 |
'(: "<" ($ (*? any)) ">" (* any)) |
| 1417 |
"<em>Hello World</em>") |
| 1418 |
|
| 1419 |
(test-sre '("foo") '(: "foo") " foo ") |
| 1420 |
(test-sre #f '(: nwb "foo" nwb) " foo ") |
| 1421 |
(test-sre '("foo") '(: nwb "foo" nwb) "xfoox") |
| 1422 |
|
| 1423 |
(test-sre '("beef") |
| 1424 |
'(* (/"af")) |
| 1425 |
"beef") |
| 1426 |
|
| 1427 |
(test-sre '("12345beef" "beef") |
| 1428 |
'(: (* numeric) ($ (* (/"af")))) |
| 1429 |
"12345beef") |
| 1430 |
|
| 1431 |
(let ((number '($ (+ numeric)))) |
| 1432 |
(test-sre '("555-867-5309" "555" "867" "5309") |
| 1433 |
`(: ,number "-" ,number "-" ,number) |
| 1434 |
"555-867-5309") |
| 1435 |
|
| 1436 |
(test-sre '("555-867-5309" "555" "5309") |
| 1437 |
`(: ,number "-" (w/nocapture ,number) "-" ,number) |
| 1438 |
"555-867-5309")) |
| 1439 |
|
| 1440 |
(test-sre '("12345BeeF" "BeeF") |
| 1441 |
'(: (* numeric) (w/nocase ($ (* (/ "af"))))) |
| 1442 |
"12345BeeF") |
| 1443 |
|
| 1444 |
(include "include/regexp-sre-utf8") |
| 1445 |
|
| 1446 |
(test-end) |