2009-02-25 13:22:12 -05:00
|
|
|
! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
|
2008-08-26 21:24:14 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-12-08 15:58:00 -05:00
|
|
|
USING: accessors combinators kernel math sequences strings sets
|
|
|
|
assocs prettyprint.backend prettyprint.custom make lexer
|
2009-03-10 19:27:04 -04:00
|
|
|
namespaces parser arrays fry locals regexp.parser splitting
|
|
|
|
sorting regexp.ast regexp.negation regexp.compiler words
|
|
|
|
call call.private math.ranges ;
|
2008-09-18 15:42:16 -04:00
|
|
|
IN: regexp
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-25 17:22:01 -05:00
|
|
|
TUPLE: regexp
|
|
|
|
{ raw read-only }
|
|
|
|
{ parse-tree read-only }
|
|
|
|
{ options read-only }
|
2009-03-10 19:27:04 -04:00
|
|
|
dfa next-match ;
|
2009-02-18 15:52:10 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
TUPLE: reverse-regexp < regexp ;
|
2009-02-25 13:22:12 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
<PRIVATE
|
2009-02-18 15:52:10 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
: 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 ;
|
2009-02-18 15:52:10 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
: <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 ;
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: check-string ( string -- string )
|
|
|
|
! Make this configurable
|
|
|
|
dup string? [ "String required" throw ] unless ;
|
|
|
|
|
|
|
|
: 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 ) ;
|
|
|
|
|
2009-03-10 20:17:25 -04:00
|
|
|
GENERIC: end/start ( string regexp -- end start )
|
|
|
|
M: regexp end/start drop length 0 ;
|
|
|
|
M: reverse-regexp end/start drop length 1- -1 swap ;
|
2009-03-10 19:27:04 -04:00
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: matches? ( string regexp -- ? )
|
2009-03-10 20:17:25 -04:00
|
|
|
[ end/start ] 2keep
|
|
|
|
[ check-string ] dip
|
|
|
|
match-index-from
|
|
|
|
[ swap = ] [ drop f ] if* ;
|
2009-03-10 19:27:04 -04:00
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: match-slice ( i string quot -- slice/f )
|
|
|
|
[ 2dup ] dip call
|
|
|
|
[ swap <slice> ] [ 2drop f ] if* ; inline
|
|
|
|
|
|
|
|
: match-from ( i string quot -- slice/f )
|
|
|
|
[ [ length [a,b) ] keep ] dip
|
|
|
|
'[ _ _ match-slice ] map-find drop ; inline
|
|
|
|
|
|
|
|
: next-match ( i string quot -- i match/f )
|
|
|
|
match-from [ dup [ to>> ] when ] keep ; inline
|
|
|
|
|
|
|
|
: do-next-match ( i string regexp -- i match/f )
|
|
|
|
dup next-match>> execute( i string regexp -- i match/f ) ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: all-matches ( string regexp -- seq )
|
|
|
|
[ check-string ] dip
|
|
|
|
[ 0 [ dup ] ] 2dip '[ _ _ do-next-match ] produce
|
|
|
|
nip but-last ;
|
|
|
|
|
|
|
|
: count-matches ( string regexp -- n )
|
|
|
|
all-matches length ;
|
|
|
|
|
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
:: split-slices ( string slices -- new-slices )
|
|
|
|
slices [ to>> ] map 0 prefix
|
|
|
|
slices [ from>> ] map string length suffix
|
|
|
|
[ string <slice> ] 2map ;
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2009-03-10 20:34:49 -04:00
|
|
|
: first-match ( string regexp -- slice/f )
|
|
|
|
[ 0 ] [ check-string ] [ ] tri*
|
|
|
|
do-next-match nip ;
|
|
|
|
|
|
|
|
: re-contains? ( string regexp -- ? )
|
|
|
|
first-match >boolean ;
|
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
: re-split1 ( string regexp -- before after/f )
|
2009-03-10 20:34:49 -04:00
|
|
|
dupd first-match [ 1array split-slices first2 ] [ f ] if* ;
|
2009-03-10 19:27:04 -04:00
|
|
|
|
|
|
|
: re-split ( string regexp -- seq )
|
|
|
|
dupd all-matches split-slices ;
|
|
|
|
|
|
|
|
: re-replace ( string regexp replacement -- result )
|
|
|
|
[ re-split ] dip join ;
|
2009-02-26 19:06:57 -05:00
|
|
|
|
2009-02-18 15:52:10 -05:00
|
|
|
<PRIVATE
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-26 15:19:02 -05:00
|
|
|
: get-ast ( regexp -- ast )
|
|
|
|
[ parse-tree>> ] [ options>> ] bi <with-options> ;
|
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
GENERIC: compile-regexp ( regex -- regexp )
|
2009-02-26 19:06:57 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
: regexp-initial-word ( i string regexp -- i/f )
|
|
|
|
compile-regexp match-index-from ;
|
2009-02-26 15:19:02 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
: do-compile-regexp ( regexp -- regexp )
|
|
|
|
dup '[
|
|
|
|
dup \ regexp-initial-word =
|
|
|
|
[ drop _ get-ast ast>dfa dfa>word ] when
|
|
|
|
] change-dfa ;
|
2009-03-07 17:31:46 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
M: regexp compile-regexp ( regexp -- regexp )
|
|
|
|
do-compile-regexp ;
|
2009-03-05 17:34:04 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
M: reverse-regexp compile-regexp ( regexp -- regexp )
|
|
|
|
t backwards? [ do-compile-regexp ] with-variable ;
|
|
|
|
|
|
|
|
GENERIC: compile-next-match ( regexp -- regexp )
|
2009-03-05 17:34:04 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
: next-initial-word ( i string regexp -- i slice/f )
|
|
|
|
compile-next-match do-next-match ;
|
|
|
|
|
|
|
|
M: regexp compile-next-match ( regexp -- regexp )
|
2009-03-04 14:22:22 -05:00
|
|
|
dup '[
|
2009-03-10 19:27:04 -04:00
|
|
|
dup \ next-initial-word = [
|
|
|
|
drop _ compile-regexp dfa>>
|
|
|
|
'[ _ '[ _ _ execute ] next-match ]
|
|
|
|
(( i string -- i match/f )) simple-define-temp
|
|
|
|
] when
|
|
|
|
] change-next-match ;
|
2009-02-25 13:22:12 -05:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
! Write M: reverse-regexp compile-next-match
|
2008-09-22 03:28:10 -04:00
|
|
|
|
2009-03-10 19:27:04 -04:00
|
|
|
PRIVATE>
|
|
|
|
|
|
|
|
: new-regexp ( string ast options class -- regexp )
|
|
|
|
[ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
|
|
|
|
|
|
|
|
: make-regexp ( string ast -- regexp )
|
|
|
|
f f <options> regexp new-regexp ;
|
|
|
|
|
|
|
|
: <optioned-regexp> ( string options -- regexp )
|
|
|
|
[ dup parse-regexp ] [ string>options ] bi*
|
|
|
|
dup on>> reversed-regexp swap member?
|
|
|
|
[ reverse-regexp new-regexp ]
|
|
|
|
[ regexp new-regexp ] if ;
|
|
|
|
|
|
|
|
: <regexp> ( string -- regexp ) "" <optioned-regexp> ;
|
|
|
|
|
|
|
|
<PRIVATE
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-03-05 17:34:04 -05:00
|
|
|
! The following two should do some caching
|
|
|
|
|
2008-11-24 01:18:27 -05:00
|
|
|
: 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 ;
|
2008-08-26 21:24:14 -04:00
|
|
|
|
2009-02-26 15:19:02 -05:00
|
|
|
: take-until ( end lexer -- string )
|
|
|
|
dup skip-blank [
|
|
|
|
[ index-from ] 2keep
|
|
|
|
[ swapd subseq ]
|
|
|
|
[ 2drop 1+ ] 3bi
|
|
|
|
] change-lexer-column ;
|
|
|
|
|
|
|
|
: parse-noblank-token ( lexer -- str/f )
|
|
|
|
dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
|
|
|
|
|
2008-09-18 15:42:16 -04:00
|
|
|
: parsing-regexp ( accum end -- accum )
|
2009-02-26 15:19:02 -05:00
|
|
|
lexer get [ take-until ] [ parse-noblank-token ] bi
|
2009-03-10 19:27:04 -04:00
|
|
|
<optioned-regexp> compile-next-match parsed ;
|
2008-11-24 01:18:27 -05:00
|
|
|
|
|
|
|
PRIVATE>
|
2008-09-18 15:42:16 -04:00
|
|
|
|
|
|
|
: 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
|
|
|
|
|
2008-09-12 22:56:25 -04:00
|
|
|
M: regexp pprint*
|
|
|
|
[
|
|
|
|
[
|
2008-11-24 01:18:27 -05:00
|
|
|
[ raw>> dup find-regexp-syntax swap % swap % % ]
|
|
|
|
[ options>> options>string % ] bi
|
2008-09-12 22:56:25 -04:00
|
|
|
] "" make
|
|
|
|
] keep present-text ;
|
2009-03-10 19:27:04 -04:00
|
|
|
|