Ticket #281: irregex-chicken-and-named-submatch-stacking.patch

File irregex-chicken-and-named-submatch-stacking.patch, 5.4 KB (added by sjamaan, 14 years ago)

Adds chicken 4 module declaration, documentation of previously missing functions, allows named submatches to "stack" (see issue 4 of irregex)

  • irregex-chicken.scm

    diff -r afe5d137e816 -r 3becabcea570 irregex-chicken.scm
    a b  
    11
    2 (cond-expand
    3  (compiling
    4   (declare
    5    (export
    6     irregex string->irregex sre->irregex
    7     string->sre maybe-string->sre
    8     irregex? irregex-match-data?
    9     irregex-new-matches irregex-reset-matches!
    10     irregex-search irregex-search/matches irregex-match
    11     irregex-search/chunked irregex-match/chunked make-irregex-chunker
    12     irregex-match-substring irregex-match-subchunk
    13     irregex-match-start-source irregex-match-start-index
    14     irregex-match-end-source irregex-match-end-index
    15     irregex-match-num-submatches
    16     irregex-fold irregex-replace irregex-replace/all
    17     irregex-dfa irregex-dfa/search irregex-dfa/extract
    18     irregex-nfa irregex-flags irregex-lengths irregex-names
    19     irregex-num-submatches
    20     ))))
    21 
     2(module irregex
     3 (irregex string->irregex sre->irregex
     4  string->sre maybe-string->sre
     5  irregex? irregex-match-data?
     6  irregex-new-matches irregex-reset-matches!
     7  irregex-search irregex-search/matches irregex-match
     8  irregex-search/chunked irregex-match/chunked make-irregex-chunker
     9  irregex-match-substring irregex-match-subchunk
     10  irregex-match-start-chunk irregex-match-start-index
     11  irregex-match-end-chunk irregex-match-end-index
     12  irregex-match-num-submatches
     13  irregex-fold irregex-replace irregex-replace/all
     14  irregex-dfa irregex-dfa/search irregex-dfa/extract
     15  irregex-nfa irregex-flags irregex-lengths irregex-names
     16  irregex-num-submatches
     17  )
     18  (import scheme)
     19  (import chicken)
     20  (include "irregex.scm")
     21)
  • irregex.doc

    diff -r afe5d137e816 -r 3becabcea570 irregex.doc
    a b  
    170170Returns \q{#t} iff the object is a successful match result from
    171171\q{irregex-search} or \q{irregex-match}.
    172172
     173\subsubsection*{(irregex-num-submatches <irx>)}
     174\subsubsection*{(irregex-match-num-submatches <match>)}
     175
     176Returns the number of numbered submatches that are defined in the
     177irregex or match object.
     178
     179\subsubsection*{(irregex-names <irx>)}
     180\subsubsection*{(irregex-match-names <match>)}
     181
     182Returns an association list of named submatches that are defined in
     183the irregex or match object.  The \q{car} of each item in this list is
     184the name of a submatch, the \q{cdr} of each item is the numerical
     185submatch corresponding to this name.  If a named submatch occurs
     186multiple times in the irregex, it will also occur multiple times in
     187this list.
     188
    173189\subsubsection*{(irregex-match-substring <match> [<index-or-name>])}
    174190\subsubsection*{(irregex-match-start-index <match> <index-or-name>)}
    175191\subsubsection*{(irregex-match-end-index <match> <index-or-name>)}
  • irregex.scm

    diff -r afe5d137e816 -r 3becabcea570 irregex.scm
    a b  
    155155
    156156(define (irregex-match-index m opt)
    157157  (if (pair? opt)
    158       (cond ((number? (car opt)) (car opt))
    159             ((assq (car opt) (irregex-match-names m)) => cdr)
    160             (else (error "unknown match name" (car opt))))
     158      (if (number? (car opt))
     159          (car opt)
     160          (let lp ((ls (irregex-match-names m)))
     161            (cond ((null? ls) (error "unknown match name" (car opt)))
     162                  ((and (eq? (car opt) (caar ls))
     163                        (%irregex-match-start-chunk m (cdar ls)))
     164                   (cdar ls))
     165                  (else (lp (cdr ls))))))
    161166      0))
    162167
    163168(define (%irregex-match-valid-index? m n)
  • irregex.setup

    diff -r afe5d137e816 -r 3becabcea570 irregex.setup
    a b  
    1 (define has-exports? (string>=? (chicken-version) "2.310"))
    21
    3 (compile irregex.scm -s -O2 -f -inline -lambda-lift -disable-interrupts ;-d0
    4          -prologue irregex-chicken.scm
    5          ,@(if has-exports?
    6                '(-check-imports -emit-exports irregex.exports)
    7                '())
    8          -o irregex.so)
     2(compile irregex-chicken.scm -s -O3 -f -inline -lambda-lift -disable-interrupts ;-d0
     3         -j irregex -o irregex.so)
     4(compile irregex.import.scm -s -Os -d0)
    95(install-extension
    106 'irregex
    117 '("irregex.so")
    12  `((version 0.7.0)
    13    (documentation "irregex.html")
    14    ,@(if has-exports? `((exports "irregex.exports")) '())))
     8 `((version 0.8.1)
     9   (documentation "irregex.html")))
  • test-irregex.scm

    diff -r afe5d137e816 -r 3becabcea570 test-irregex.scm
    a b  
    283283       (lambda (src i s) (reverse s))))
    284284  )
    285285
     286
     287(define (extract name irx str)
     288  (irregex-match-substring (irregex-match irx str) name))
     289
     290(test-group "named submatches"
     291  (test "matching alternative is used"
     292        "first" (extract 'sub `(or (submatch-named sub "first")
     293                                   (submatch-named sub "second"))
     294                         "first"))
     295  (test "matching alternative is used (second match)"
     296        "second" (extract 'sub `(or (submatch-named sub "first")
     297                                    (submatch-named sub "second"))
     298                         "second"))
     299  (test "last match is used with multiple matches for a name"
     300        "second" (extract 'sub `(seq (submatch-named sub "first")
     301                                     space
     302                                     (submatch-named sub "second"))
     303                         "first second")))
     304
    286305(test-end)
    287306