diff -r bc532fb2efc4 irregex.scm
|
a
|
b
|
|
| 159 | 159 | (if (pair? opt) |
| 160 | 160 | (if (number? (car opt)) |
| 161 | 161 | (car opt) |
| 162 | | (let lp ((ls (irregex-match-names m))) |
| 163 | | (cond ((null? ls) (error "unknown match name" (car opt))) |
| 164 | | ((and (eq? (car opt) (caar ls)) |
| 165 | | (%irregex-match-start-chunk m (cdar ls))) |
| 166 | | (cdar ls)) |
| 167 | | (else (lp (cdr ls)))))) |
| | 162 | (let lp ((ls (irregex-match-names m)) |
| | 163 | (exists #f)) |
| | 164 | (cond ((null? ls) |
| | 165 | (if exists #f (error "unknown match name" (car opt)))) |
| | 166 | ((eq? (car opt) (caar ls)) |
| | 167 | (if (%irregex-match-start-chunk m (cdar ls)) |
| | 168 | (cdar ls) |
| | 169 | (lp (cdr ls) #t))) |
| | 170 | (else (lp (cdr ls) exists))))) |
| 168 | 171 | 0)) |
| 169 | 172 | |
| 170 | 173 | (define (%irregex-match-valid-index? m n) |
| … |
… |
|
| 183 | 186 | (error "irregex-match-substring: not match data" m)) |
| 184 | 187 | (let* ((cnk (irregex-match-chunker m)) |
| 185 | 188 | (n (irregex-match-index m opt))) |
| 186 | | (and (%irregex-match-valid-index? m n) |
| | 189 | (and n |
| | 190 | (%irregex-match-valid-index? m n) |
| 187 | 191 | ((chunker-get-substring cnk) |
| 188 | 192 | (%irregex-match-start-chunk m n) |
| 189 | 193 | (%irregex-match-start-index m n) |
diff -r bc532fb2efc4 test-irregex.scm
|
a
|
b
|
|
| 283 | 283 | (lambda (src i s) (reverse s)))) |
| 284 | 284 | ) |
| 285 | 285 | |
| 286 | | |
| 287 | 286 | (define (extract name irx str) |
| 288 | 287 | (irregex-match-substring (irregex-match irx str) name)) |
| 289 | 288 | |
| 290 | 289 | (test-group "named submatches" |
| | 290 | (test "matching submatch is seen and extracted" |
| | 291 | "first" (extract 'first `(or (submatch-named first "first") |
| | 292 | (submatch-named second "second")) |
| | 293 | "first")) |
| | 294 | (test "nonmatching submatch is known but returns false" |
| | 295 | #f (extract 'second `(or (submatch-named first "first") |
| | 296 | (submatch-named second "second")) |
| | 297 | "first")) |
| | 298 | (test-error "nonexisting submatch is unknown and raises an error" |
| | 299 | (extract 'third `(or (submatch-named first "first") |
| | 300 | (submatch-named second "second")) |
| | 301 | "first")) |
| 291 | 302 | (test "matching alternative is used" |
| 292 | 303 | "first" (extract 'sub `(or (submatch-named sub "first") |
| 293 | 304 | (submatch-named sub "second")) |