factor/basis/regexp/combinators/combinators.factor

57 lines
1.5 KiB
Factor
Raw Normal View History

! Copyright (C) 2009 Daniel Ehrenberg
! See http://factorcode.org/license.txt for BSD license.
USING: regexp sequences kernel regexp.negation regexp.ast
accessors fry regexp.classes ;
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>
CONSTANT: <nothing> R/ (?~.*)/
: <literal> ( string -- regexp )
2009-02-25 17:22:01 -05:00
[ "\\Q" "\\E" surround ] [ <concatenation> ] bi make-regexp ; foldable
: <char-range> ( char1 char2 -- regexp )
[ [ "[" "-" surround ] [ "]" append ] bi* append ]
[ <range> ]
2bi make-regexp ;
: <or> ( regexps -- disjunction )
[ [ raw>> "(" ")" surround ] map "|" join ]
[ [ parse-tree>> ] map <alternation> ] bi
2009-02-25 17:22:01 -05:00
make-regexp ; foldable
: <any-of> ( strings -- regexp )
2009-02-25 17:22:01 -05:00
[ <literal> ] map <or> ; foldable
: <sequence> ( regexps -- regexp )
[ [ raw>> ] map concat ]
[ [ parse-tree>> ] map <concatenation> ] bi
2009-02-25 17:22:01 -05:00
make-regexp ; foldable
: <not> ( regexp -- not-regexp )
[ "(?~" ")" surround ]
2009-02-25 17:22:01 -05:00
[ <negation> ] modify-regexp ; foldable
: <and> ( regexps -- conjunction )
2009-02-25 17:22:01 -05:00
[ <not> ] map <or> <not> ; foldable
: <zero-or-more> ( regexp -- regexp* )
[ "(" ")*" surround ]
2009-02-25 17:22:01 -05:00
[ <star> ] modify-regexp ; foldable
: <one-or-more> ( regexp -- regexp+ )
[ "(" ")+" surround ]
2009-02-25 17:22:01 -05:00
[ <plus> ] modify-regexp ; foldable
: <option> ( regexp -- regexp? )
[ "(" ")?" surround ]
2009-02-25 17:22:01 -05:00
[ <maybe> ] modify-regexp ; foldable