! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors combinators kernel math sequences strings sets
-assocs prettyprint.backend prettyprint.custom make lexer
-namespaces parser arrays fry locals regexp.parser splitting
-sorting regexp.ast regexp.negation regexp.compiler words
-call call.private math.ranges ;
+USING: accessors arrays classes compiler.units kernel
+kernel.private lexer make math ranges namespaces quotations
+regexp.ast regexp.compiler regexp.negation regexp.parser
+sequences sequences.private splitting strings vocabs.loader
+words ;
IN: regexp
TUPLE: regexp
<PRIVATE
-: maybe-negated ( lookaround quot -- regexp-quot )
- '[ term>> @ ] [ positive?>> [ ] [ not ] ? ] bi compose ; inline
-
-M: lookahead question>quot ! Returns ( index string -- ? )
- [ ast>dfa dfa>shortest-word '[ f _ execute ] ] maybe-negated ;
+M: lookahead question>quot
+ ! Returns ( index string -- ? )
+ term>> ast>dfa dfa>shortest-word 1quotation [ f ] prepose ;
: <reversed-option> ( ast -- reversed )
"r" string>options <with-options> ;
-M: lookbehind question>quot ! Returns ( index string -- ? )
- [
- <reversed-option>
- ast>dfa dfa>reverse-shortest-word
- '[ [ 1- ] dip f _ execute ]
- ] maybe-negated ;
-
-: check-string ( string -- string )
- ! Make this configurable
- dup string? [ "String required" throw ] unless ;
+M: lookbehind question>quot
+ ! Returns ( index string -- ? )
+ term>> <reversed-option>
+ ast>dfa dfa>reverse-shortest-word
+ 1quotation [ [ 1 - ] dip f ] prepose ;
: match-index-from ( i string regexp -- index/f )
! This word is unsafe. It assumes that i is a fixnum
! and that string is a string.
- dup dfa>> execute( index string regexp -- i/f ) ;
+ dup dfa>> execute( index string regexp -- i/f ) ; inline
GENERIC: end/start ( string regexp -- end start )
M: regexp end/start drop length 0 ;
-M: reverse-regexp end/start drop length 1- -1 swap ;
+M: reverse-regexp end/start drop length 1 - -1 swap ;
PRIVATE>
: matches? ( string regexp -- ? )
+ [ string check-instance ] dip
[ end/start ] 2keep
- [ check-string ] dip
match-index-from
- [ swap = ] [ drop f ] if* ;
+ [ = ] [ drop f ] if* ;
<PRIVATE
-TUPLE: match { i read-only } { j read-only } { seq read-only } ;
-
-: match-slice ( i string quot -- match/f )
- [ 2dup ] dip call
- [ swap match boa ] [ 2drop f ] if* ; inline
-
: search-range ( i string reverse? -- seq )
- [ drop 0 [a,b] ] [ length [a,b) ] if ; inline
+ [ drop -1 ] [ length ] if [a..b] ; inline
-: match>result ( match reverse? -- i start end string )
- over [
- [ [ i>> ] [ j>> tuck ] [ seq>> ] tri ] dip
- [ [ swap [ 1+ ] bi@ ] dip ] when
- ] [ 2drop f f f f ] if ; inline
+:: (next-match) ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
+ i string regexp quot call dup
+ [| j | reverse? [ j i ] [ i j ] if string ] [ drop f f f ] if ; inline
-:: next-match ( i string quot reverse? -- i start end string )
+:: next-match ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
+ f f f
i string reverse? search-range
- [ string quot match-slice ] map-find drop
- reverse? match>result ; inline
+ [ 3nip string regexp quot reverse? (next-match) dup ] find 2drop ; inline
-: do-next-match ( i string regexp -- i start end string )
+: do-next-match ( i string regexp -- start end ? )
dup next-match>>
- execute( i string regexp -- i start end string ) ;
-
-: next-slice ( i string regexp -- i/f slice/f )
- do-next-match
- [ slice boa ] [ drop ] if* ; inline
+ execute( i string regexp -- start end ? ) ; inline
+
+:: (each-match-forward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
+ i string length <= [
+ i string regexp do-next-match [| start end |
+ start end string quot call
+ start end eq? [ end 1 + ] [ end ] if
+ string regexp quot (each-match-forward)
+ ] [ 2drop ] if
+ ] when ; inline recursive
+
+:: (each-match-backward) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
+ i -1 >= [
+ i string regexp do-next-match [| start end |
+ start 1 + end 1 + string quot call
+ start end eq? [ start 1 - ] [ start ] if
+ string regexp quot (each-match-backward)
+ ] [ 2drop ] if
+ ] when ; inline recursive
+
+: (each-match) ( ... i string regexp quot: ( ... start end string -- ... ) -- ... )
+ over reverse-regexp? [ (each-match-backward) ] [ (each-match-forward) ] if ; inline
+
+GENERIC: match-iterator-start ( string regexp -- start )
+M: regexp match-iterator-start 2drop 0 ;
+M: reverse-regexp match-iterator-start drop length ;
+
+: prepare-match-iterator ( string regexp -- i string regexp )
+ [ string check-instance ] dip [ match-iterator-start ] 2keep ; inline
PRIVATE>
-TUPLE: match-iterator
- { string read-only }
- { regexp read-only }
- { i read-only }
- { value read-only } ;
-
-: iterate ( iterator -- iterator'/f )
- dup
- [ i>> ] [ string>> ] [ regexp>> ] tri next-slice
- [ [ [ string>> ] [ regexp>> ] bi ] 2dip match-iterator boa ]
- [ 2drop f ] if* ;
+: each-match ( ... string regexp quot: ( ... start end string -- ... ) -- ... )
+ [ prepare-match-iterator ] dip (each-match) ; inline
-: value ( iterator/f -- value/f )
- dup [ value>> ] when ;
+: map-matches ( ... string regexp quot: ( ... start end string -- ... obj ) -- ... seq )
+ collector [ each-match ] dip >array ; inline
-: <match-iterator> ( string regexp -- match-iterator )
- [ check-string ] dip
- 2dup end/start nip f
- match-iterator boa
- iterate ; inline
+: all-matching-slices ( string regexp -- seq )
+ [ <slice-unsafe> ] map-matches ;
-: all-matches ( string regexp -- seq )
- <match-iterator> [ iterate ] follow [ value ] map ;
+: all-matching-subseqs ( string regexp -- seq )
+ [ subseq ] map-matches ;
: count-matches ( string regexp -- n )
- all-matches length ;
+ [ 0 ] 2dip [ 3drop 1 + ] each-match ;
<PRIVATE
-:: split-slices ( string slices -- new-slices )
- slices [ to>> ] map 0 prefix
- slices [ from>> ] map string length suffix
- [ string <slice> ] 2map ;
+:: (re-split) ( string regexp quot: ( from to seq -- slice ) -- new-slices )
+ 0 string regexp [| end start end' string |
+ end' ! leave it on the stack for the next iteration
+ end start string quot call
+ ] map-matches
+ ! Final chunk
+ swap string length string quot call suffix ; inline
PRIVATE>
-: first-match ( string regexp -- slice/f )
- <match-iterator> value ;
+:: first-match ( string regexp -- slice/f )
+ string regexp prepare-match-iterator do-next-match [
+ regexp reverse-regexp? [ [ 1 + ] bi@ ] when
+ string <slice-unsafe>
+ ] [ 2drop f ] if ;
: re-contains? ( string regexp -- ? )
- first-match >boolean ;
-
-: re-split1 ( string regexp -- before after/f )
- dupd first-match [ 1array split-slices first2 ] [ f ] if* ;
+ prepare-match-iterator do-next-match 2nip >boolean ;
: re-split ( string regexp -- seq )
- dupd all-matches split-slices ;
+ [ <slice-unsafe> ] (re-split) ;
: re-replace ( string regexp replacement -- result )
- [ re-split ] dip join ;
+ [ [ subseq ] (re-split) ] dip join ;
+
+:: re-replace-with ( string regexp quot: ( slice -- replacement ) -- result )
+ [
+ 0 string regexp [
+ drop [ [ string <slice-unsafe> , ] keep ] dip
+ [ string <slice-unsafe> quot call( x -- x ) , ] keep
+ ] each-match string [ length ] [ <slice-unsafe> ] bi ,
+ ] { } make concat ;
<PRIVATE
GENERIC: compile-regexp ( regex -- regexp )
: regexp-initial-word ( i string regexp -- i/f )
- compile-regexp match-index-from ;
+ [ compile-regexp ] with-compilation-unit match-index-from ;
-: do-compile-regexp ( regexp -- regexp )
+M: regexp compile-regexp
dup '[
dup \ regexp-initial-word =
[ drop _ get-ast ast>dfa dfa>word ] when
] change-dfa ;
-M: regexp compile-regexp ( regexp -- regexp )
- do-compile-regexp ;
-
-M: reverse-regexp compile-regexp ( regexp -- regexp )
- t backwards? [ do-compile-regexp ] with-variable ;
+M: reverse-regexp compile-regexp
+ t backwards? [ call-next-method ] with-variable ;
DEFER: compile-next-match
-: next-initial-word ( i string regexp -- i start end string )
- compile-next-match do-next-match ;
+: next-initial-word ( i string regexp -- start end string )
+ [ compile-next-match ] with-compilation-unit do-next-match ;
: compile-next-match ( regexp -- regexp )
dup '[
dup \ next-initial-word = [
- drop _ [ compile-regexp dfa>> ] [ reverse-regexp? ] bi
- '[ _ '[ _ _ execute ] _ next-match ]
- (( i string regexp -- i start end string )) simple-define-temp
+ drop _ [ compile-regexp dfa>> def>> ] [ reverse-regexp? ] bi
+ '[ { array-capacity string regexp } declare _ _ next-match ]
+ ( i string regexp -- start end string ) define-temp
] when
] change-next-match ;
<PRIVATE
-! The following two should do some caching
-
-: find-regexp-syntax ( string -- prefix suffix )
- {
- { "R/ " "/" }
- { "R! " "!" }
- { "R\" " "\"" }
- { "R# " "#" }
- { "R' " "'" }
- { "R( " ")" }
- { "R@ " "@" }
- { "R[ " "]" }
- { "R` " "`" }
- { "R{ " "}" }
- { "R| " "|" }
- } swap [ subseq? not nip ] curry assoc-find drop ;
-
-: take-until ( end lexer -- string )
+: take-until ( lexer -- string )
dup skip-blank [
- [ index-from ] 2keep
- [ swapd subseq ]
- [ 2drop 1+ ] 3bi
+ dupd [
+ [ [ "\\/" member? ] find-from ] keep swap [
+ CHAR: \ = [ [ 2 + ] dip t ] [ f ] if
+ ] [
+ "Unterminated regexp" throw
+ ] if*
+ ] loop over [ subseq ] dip 1 +
] change-lexer-column ;
: parse-noblank-token ( lexer -- str/f )
- dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
+ dup still-parsing-line? [ (parse-raw) ] [ drop f ] if ;
-: parsing-regexp ( accum end -- accum )
- lexer get [ take-until ] [ parse-noblank-token ] bi
- <optioned-regexp> compile-next-match parsed ;
+: parse-regexp ( accum -- accum )
+ lexer get [ take-until "\\/" "/" replace ] [ parse-noblank-token ] bi
+ <optioned-regexp> compile-next-match suffix! ;
PRIVATE>
-: R! CHAR: ! parsing-regexp ; parsing
-: R" CHAR: " parsing-regexp ; parsing
-: R# CHAR: # parsing-regexp ; parsing
-: R' CHAR: ' parsing-regexp ; parsing
-: R( CHAR: ) parsing-regexp ; parsing
-: R/ CHAR: / parsing-regexp ; parsing
-: R@ CHAR: @ parsing-regexp ; parsing
-: R[ CHAR: ] parsing-regexp ; parsing
-: R` CHAR: ` parsing-regexp ; parsing
-: R{ CHAR: } parsing-regexp ; parsing
-: R| CHAR: | parsing-regexp ; parsing
-
-M: regexp pprint*
- [
- [
- [ raw>> dup find-regexp-syntax swap % swap % % ]
- [ options>> options>string % ] bi
- ] "" make
- ] keep present-text ;
+SYNTAX: R/ parse-regexp ;
+
+{ "prettyprint" "regexp" } "regexp.prettyprint" require-when