2009-02-25 13:22:12 -05:00
|
|
|
! Copyright (C) 2009 Daniel Ehrenberg
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
|
|
|
USING: regexp sequences kernel regexp.negation regexp.ast
|
2009-03-02 16:31:28 -05:00
|
|
|
accessors fry regexp.classes ;
|
2009-02-25 13:22:12 -05:00
|
|
|
IN: regexp.combinators
|
|
|
|
|
2009-02-25 17:22:01 -05:00
|
|
|
<PRIVATE
|
|
|
|
|
|
|
|
: modify-regexp ( regexp raw-quot tree-quot -- new-regexp )
|
|
|
|
[ '[ raw>> @ ] ]
|
|
|
|
[ '[ parse-tree>> @ ] ] bi* bi
|
|
|
|
make-regexp ; inline
|
|
|
|
|
|
|
|
PRIVATE>
|
|
|
|
|
2009-03-18 17:09:45 -04:00
|
|
|
CONSTANT: <nothing> R/ (?~.*)/s
|
2009-02-25 13:22:12 -05:00
|
|
|
|
|
|
|
: <literal> ( string -- regexp )
|
2009-02-25 17:22:01 -05:00
|
|
|
[ "\\Q" "\\E" surround ] [ <concatenation> ] bi make-regexp ; foldable
|
2009-02-25 13:22:12 -05:00
|
|
|
|
2009-03-02 16:31:28 -05:00
|
|
|
: <char-range> ( char1 char2 -- regexp )
|
|
|
|
[ [ "[" "-" surround ] [ "]" append ] bi* append ]
|
2009-03-18 17:09:45 -04:00
|
|
|
[ <range-class> ]
|
2009-03-02 16:31:28 -05:00
|
|
|
2bi make-regexp ;
|
|
|
|
|
2009-02-25 13:22:12 -05:00
|
|
|
: <or> ( regexps -- disjunction )
|
|
|
|
[ [ raw>> "(" ")" surround ] map "|" join ]
|
|
|
|
[ [ parse-tree>> ] map <alternation> ] bi
|
2009-02-25 17:22:01 -05:00
|
|
|
make-regexp ; foldable
|
2009-02-25 13:22:12 -05:00
|
|
|
|
|
|
|
: <any-of> ( strings -- regexp )
|
2009-02-25 17:22:01 -05:00
|
|
|
[ <literal> ] map <or> ; foldable
|
2009-02-25 13:22:12 -05:00
|
|
|
|
|
|
|
: <sequence> ( regexps -- regexp )
|
|
|
|
[ [ raw>> ] map concat ]
|
|
|
|
[ [ parse-tree>> ] map <concatenation> ] bi
|
2009-02-25 17:22:01 -05:00
|
|
|
make-regexp ; foldable
|
2009-02-25 13:22:12 -05:00
|
|
|
|
|
|
|
: <not> ( regexp -- not-regexp )
|
|
|
|
[ "(?~" ")" surround ]
|
2009-02-25 17:22:01 -05:00
|
|
|
[ <negation> ] modify-regexp ; foldable
|
2009-02-25 13:22:12 -05:00
|
|
|
|
|
|
|
: <and> ( regexps -- conjunction )
|
2009-02-25 17:22:01 -05:00
|
|
|
[ <not> ] map <or> <not> ; foldable
|
2009-02-25 13:22:12 -05:00
|
|
|
|
|
|
|
: <zero-or-more> ( regexp -- regexp* )
|
|
|
|
[ "(" ")*" surround ]
|
2009-02-25 17:22:01 -05:00
|
|
|
[ <star> ] modify-regexp ; foldable
|
2009-02-25 13:22:12 -05:00
|
|
|
|
|
|
|
: <one-or-more> ( regexp -- regexp+ )
|
|
|
|
[ "(" ")+" surround ]
|
2009-02-25 17:22:01 -05:00
|
|
|
[ <plus> ] modify-regexp ; foldable
|
2009-02-25 13:22:12 -05:00
|
|
|
|
|
|
|
: <option> ( regexp -- regexp? )
|
|
|
|
[ "(" ")?" surround ]
|
2009-02-25 17:22:01 -05:00
|
|
|
[ <maybe> ] modify-regexp ; foldable
|