! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license.
-USING: accessors combinators kernel kernel.private math sequences
-sequences.private strings sets assocs make lexer namespaces parser
-arrays fry locals regexp.parser splitting sorting regexp.ast
-regexp.negation regexp.compiler compiler.units words 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
-M: lookahead question>quot ! Returns ( index string -- ? )
- term>> ast>dfa dfa>shortest-word '[ f _ execute ] ;
+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 -- ? )
+M: lookbehind question>quot
+ ! Returns ( index string -- ? )
term>> <reversed-option>
ast>dfa dfa>reverse-shortest-word
- '[ [ 1 - ] dip f _ execute ] ;
-
-: check-string ( string -- string )
- ! Make this configurable
- dup string? [ "String required" throw ] unless ;
+ 1quotation [ [ 1 - ] dip f ] prepose ;
: match-index-from ( i string regexp -- index/f )
! This word is unsafe. It assumes that i is a fixnum
PRIVATE>
: matches? ( string regexp -- ? )
- [ check-string ] dip
+ [ string check-instance ] dip
[ end/start ] 2keep
match-index-from
[ = ] [ drop f ] if* ;
<PRIVATE
: search-range ( i string reverse? -- seq )
- [ drop -1 ] [ length ] if [a,b] ; inline
+ [ drop -1 ] [ length ] if [a..b] ; inline
:: (next-match) ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
i string regexp quot call dup
:: next-match ( i string regexp quot: ( i string regexp -- j ) reverse? -- start end ? )
f f f
i string reverse? search-range
- [ [ 3drop ] dip string regexp quot reverse? (next-match) dup ] find 2drop ; inline
+ [ 3nip string regexp quot reverse? (next-match) dup ] find 2drop ; inline
: do-next-match ( i string regexp -- start end ? )
dup next-match>>
M: reverse-regexp match-iterator-start drop length ;
: prepare-match-iterator ( string regexp -- i string regexp )
- [ check-string ] dip [ match-iterator-start ] 2keep ; inline
+ [ string check-instance ] dip [ match-iterator-start ] 2keep ; inline
PRIVATE>
] [ 2drop f ] if ;
: re-contains? ( string regexp -- ? )
- prepare-match-iterator do-next-match [ 2drop ] dip >boolean ;
+ prepare-match-iterator do-next-match 2nip >boolean ;
: re-split ( string regexp -- seq )
[ <slice-unsafe> ] (re-split) ;
: regexp-initial-word ( i string regexp -- i/f )
[ compile-regexp ] with-compilation-unit match-index-from ;
-M: regexp compile-regexp ( regexp -- regexp )
+M: regexp compile-regexp
dup '[
dup \ regexp-initial-word =
[ drop _ get-ast ast>dfa dfa>word ] when
] change-dfa ;
-M: reverse-regexp compile-regexp ( regexp -- regexp )
+M: reverse-regexp compile-regexp
t backwards? [ call-next-method ] with-variable ;
DEFER: compile-next-match
: take-until ( lexer -- string )
dup skip-blank [
dupd [
- [ CHAR: / -rot index-from ] keep
- over [ "Unterminated regexp" throw ] unless
- 2dup [ 1 - ] dip nth CHAR: \\ =
- [ [ [ 1 + ] dip ] when ] keep
+ [ [ "\\/" member? ] find-from ] keep swap [
+ CHAR: \ = [ [ 2 + ] dip t ] [ f ] if
+ ] [
+ "Unterminated regexp" throw
+ ] if*
] loop over [ subseq ] dip 1 +
] change-lexer-column ;
dup still-parsing-line? [ (parse-raw) ] [ drop f ] if ;
: parse-regexp ( accum -- accum )
- lexer get [ take-until ] [ parse-noblank-token ] bi
+ lexer get [ take-until "\\/" "/" replace ] [ parse-noblank-token ] bi
<optioned-regexp> compile-next-match suffix! ;
PRIVATE>
SYNTAX: R/ parse-regexp ;
-USE: vocabs.loader
{ "prettyprint" "regexp" } "regexp.prettyprint" require-when