]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/combinators/combinators.factor
Switch to https urls
[factor.git] / basis / regexp / combinators / combinators.factor
1 ! Copyright (C) 2009 Daniel Ehrenberg
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel regexp regexp.ast regexp.classes
4 sequences strings ;
5 IN: regexp.combinators
6
7 <PRIVATE
8
9 : modify-regexp ( regexp raw-quot tree-quot -- new-regexp )
10     [ '[ raw>> @ ] ]
11     [ '[ parse-tree>> @ ] ] bi* bi
12     make-regexp ; inline
13
14 PRIVATE>
15
16 CONSTANT: <nothing> R/ (?~.*)/s
17
18 : <literal> ( string -- regexp )
19     [ "\\Q" "\\E" surround ] [ <concatenation> ] bi make-regexp ; foldable
20
21 : <char-range> ( char1 char2 -- regexp )
22     [ [ 1string ] bi@ [ "[" "-" surround ] [ "]" append ] bi* append ]
23     [ <range-class> ] 2bi make-regexp ;
24
25 : <or> ( regexps -- disjunction )
26     [ [ raw>> "(" ")" surround ] map "|" join ]
27     [ [ parse-tree>> ] map <alternation> ] bi
28     make-regexp ; foldable
29
30 : <any-of> ( strings -- regexp )
31     [ <literal> ] map <or> ; foldable
32
33 : <sequence> ( regexps -- regexp )
34     [ [ raw>> ] map concat ]
35     [ [ parse-tree>> ] map <concatenation> ] bi
36     make-regexp ; foldable
37
38 : <not> ( regexp -- not-regexp )
39     [ "(?~" ")" surround ]
40     [ <negation> ] modify-regexp ; foldable
41
42 : <and> ( regexps -- conjunction )
43     [ <not> ] map <or> <not> ; foldable
44
45 : <zero-or-more> ( regexp -- regexp* )
46     [ "(" ")*" surround ]
47     [ <star> ] modify-regexp ; foldable
48
49 : <one-or-more> ( regexp -- regexp+ )
50     [ "(" ")+" surround ]
51     [ <plus> ] modify-regexp ; foldable
52
53 : <option> ( regexp -- regexp? )
54     [ "(" ")?" surround ]
55     [ <maybe> ] modify-regexp ; foldable