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-02-19 01:11:45 -05:00
|
|
|
namespaces parser arrays fry locals regexp.minimize
|
2009-03-04 14:22:22 -05:00
|
|
|
regexp.parser regexp.nfa regexp.dfa
|
2009-02-19 17:48:46 -05:00
|
|
|
regexp.transition-tables splitting sorting regexp.ast
|
2009-02-26 19:06:57 -05:00
|
|
|
regexp.negation regexp.matchers regexp.compiler ;
|
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-04 14:22:22 -05:00
|
|
|
dfa reverse-dfa ;
|
2009-02-18 15:52:10 -05:00
|
|
|
|
2009-02-25 13:22:12 -05:00
|
|
|
: make-regexp ( string ast -- regexp )
|
2009-03-04 14:22:22 -05:00
|
|
|
f f <options> f f regexp boa ; foldable
|
2009-02-25 17:22:01 -05:00
|
|
|
! Foldable because, when the dfa slot is set,
|
|
|
|
! it'll be set to the same thing regardless of who sets it
|
2009-02-25 13:22:12 -05:00
|
|
|
|
2009-02-18 15:52:10 -05:00
|
|
|
: <optioned-regexp> ( string options -- regexp )
|
|
|
|
[ dup parse-regexp ] [ string>options ] bi*
|
2009-03-04 14:22:22 -05:00
|
|
|
f f regexp boa ;
|
2009-02-18 15:52:10 -05:00
|
|
|
|
|
|
|
: <regexp> ( string -- regexp ) "" <optioned-regexp> ;
|
|
|
|
|
2009-02-26 19:06:57 -05:00
|
|
|
TUPLE: reverse-matcher regexp ;
|
|
|
|
C: <reverse-matcher> reverse-matcher
|
|
|
|
|
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-02-25 17:22:01 -05:00
|
|
|
: compile-regexp ( regexp -- regexp )
|
2009-03-04 14:22:22 -05:00
|
|
|
dup '[ [ _ get-ast ast>dfa dfa>quotation ] unless* ] change-dfa ;
|
2009-02-26 19:06:57 -05:00
|
|
|
|
2009-02-26 15:19:02 -05:00
|
|
|
: <reversed-option> ( ast -- reversed )
|
|
|
|
"r" string>options <with-options> ;
|
|
|
|
|
|
|
|
: compile-reverse ( regexp -- regexp )
|
2009-03-04 14:22:22 -05:00
|
|
|
dup '[
|
|
|
|
[
|
|
|
|
_ get-ast <reversed-option>
|
|
|
|
ast>dfa dfa>quotation
|
|
|
|
] unless*
|
|
|
|
] change-reverse-dfa ;
|
2009-02-25 13:22:12 -05:00
|
|
|
|
2009-02-26 23:14:41 -05:00
|
|
|
M: regexp match-index-from ( string regexp -- index/f )
|
2009-03-04 16:54:56 -05:00
|
|
|
compile-regexp dfa>> <quot-matcher> match-index-from ;
|
2008-09-22 03:28:10 -04:00
|
|
|
|
2009-02-26 23:14:41 -05:00
|
|
|
M: reverse-matcher match-index-from ( string regexp -- index/f )
|
2009-02-26 19:06:57 -05:00
|
|
|
[ <reversed> ] [ regexp>> compile-reverse reverse-dfa>> ] bi*
|
2009-03-04 14:22:22 -05:00
|
|
|
<quot-matcher> match-index-from ;
|
2008-08-26 21:24:14 -04:00
|
|
|
|
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-04 16:54:56 -05:00
|
|
|
<optioned-regexp> compile-regexp 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 ;
|