]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/regexp.factor
Merge branch 'master' of git://factorcode.org/git/factor into clean-linux-x86-32
[factor.git] / basis / regexp / regexp.factor
1 ! Copyright (C) 2008, 2009 Doug Coleman, Daniel Ehrenberg.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators kernel math sequences strings sets
4 assocs prettyprint.backend prettyprint.custom make lexer
5 namespaces parser arrays fry locals regexp.parser splitting
6 sorting regexp.ast regexp.negation regexp.compiler words
7 call call.private math.ranges ;
8 IN: regexp
9
10 TUPLE: regexp
11     { raw read-only }
12     { parse-tree read-only }
13     { options read-only }
14     dfa next-match ;
15
16 TUPLE: reverse-regexp < regexp ;
17
18 <PRIVATE
19
20 : maybe-negated ( lookaround quot -- regexp-quot )
21     '[ term>> @ ] [ positive?>> [ ] [ not ] ? ] bi compose ; inline
22
23 M: lookahead question>quot ! Returns ( index string -- ? )
24     [ ast>dfa dfa>shortest-word '[ f _ execute ] ] maybe-negated ;
25
26 : <reversed-option> ( ast -- reversed )
27     "r" string>options <with-options> ;
28
29 M: lookbehind question>quot ! Returns ( index string -- ? )
30     [
31         <reversed-option>
32         ast>dfa dfa>reverse-shortest-word
33         '[ [ 1- ] dip f _ execute ]
34     ] maybe-negated ;
35
36 <PRIVATE
37
38 : check-string ( string -- string )
39     ! Make this configurable
40     dup string? [ "String required" throw ] unless ;
41
42 : match-index-from ( i string regexp -- index/f )
43     ! This word is unsafe. It assumes that i is a fixnum
44     ! and that string is a string.
45     dup dfa>> execute( index string regexp -- i/f ) ;
46
47 GENERIC: end/start ( string regexp -- end start )
48 M: regexp end/start drop length 0 ;
49 M: reverse-regexp end/start drop length 1- -1 swap ;
50
51 PRIVATE>
52
53 : matches? ( string regexp -- ? )
54     [ end/start ] 2keep
55     [ check-string ] dip
56     match-index-from
57     [ swap = ] [ drop f ] if* ;
58
59 <PRIVATE
60
61 : match-slice ( i string quot -- slice/f )
62     [ 2dup ] dip call
63     [ swap <slice> ] [ 2drop f ] if* ; inline
64
65 : match-from ( i string quot -- slice/f )
66     [ [ length [a,b) ] keep ] dip
67     '[ _ _ match-slice ] map-find drop ; inline
68
69 : next-match ( i string quot -- i match/f )
70     match-from [ dup [ to>> ] when ] keep ; inline
71
72 : do-next-match ( i string regexp -- i match/f )
73     dup next-match>> execute( i string regexp -- i match/f ) ;
74
75 PRIVATE>
76
77 : all-matches ( string regexp -- seq )
78     [ check-string ] dip
79     [ 0 [ dup ] ] 2dip '[ _ _ do-next-match ] produce
80     nip but-last ;
81
82 : count-matches ( string regexp -- n )
83     all-matches length ;
84
85 <PRIVATE
86
87 :: split-slices ( string slices -- new-slices )
88     slices [ to>> ] map 0 prefix
89     slices [ from>> ] map string length suffix
90     [ string <slice> ] 2map ;
91
92 PRIVATE>
93
94 : first-match ( string regexp -- slice/f )
95     [ 0 ] [ check-string ] [ ] tri*
96     do-next-match nip ;
97
98 : re-contains? ( string regexp -- ? )
99     first-match >boolean ;
100
101 : re-split1 ( string regexp -- before after/f )
102     dupd first-match [ 1array split-slices first2 ] [ f ] if* ;
103
104 : re-split ( string regexp -- seq )
105     dupd all-matches split-slices ;
106
107 : re-replace ( string regexp replacement -- result )
108     [ re-split ] dip join ;
109
110 <PRIVATE
111
112 : get-ast ( regexp -- ast )
113     [ parse-tree>> ] [ options>> ] bi <with-options> ;
114
115 GENERIC: compile-regexp ( regex -- regexp )
116
117 : regexp-initial-word ( i string regexp -- i/f )
118     compile-regexp match-index-from ;
119
120 : do-compile-regexp ( regexp -- regexp )
121     dup '[
122         dup \ regexp-initial-word =
123         [ drop _ get-ast ast>dfa dfa>word ] when
124     ] change-dfa ;
125
126 M: regexp compile-regexp ( regexp -- regexp )
127     do-compile-regexp ;
128
129 M: reverse-regexp compile-regexp ( regexp -- regexp )
130     t backwards? [ do-compile-regexp ] with-variable ;
131
132 GENERIC: compile-next-match ( regexp -- regexp )
133
134 : next-initial-word ( i string regexp -- i slice/f )
135     compile-next-match do-next-match ;
136
137 M: regexp compile-next-match ( regexp -- regexp )
138     dup '[
139         dup \ next-initial-word = [
140             drop _ compile-regexp dfa>>
141             '[ _ '[ _ _ execute ] next-match ]
142             (( i string -- i match/f )) simple-define-temp
143         ] when
144     ] change-next-match ;
145
146 ! Write M: reverse-regexp compile-next-match
147
148 PRIVATE>
149
150 : new-regexp ( string ast options class -- regexp )
151     [ \ regexp-initial-word \ next-initial-word ] dip boa ; inline
152
153 : make-regexp ( string ast -- regexp )
154     f f <options> regexp new-regexp ;
155
156 : <optioned-regexp> ( string options -- regexp )
157     [ dup parse-regexp ] [ string>options ] bi*
158     dup on>> reversed-regexp swap member?
159     [ reverse-regexp new-regexp ]
160     [ regexp new-regexp ] if ;
161
162 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
163
164 <PRIVATE
165
166 ! The following two should do some caching
167
168 : find-regexp-syntax ( string -- prefix suffix )
169     {
170         { "R/ "  "/"  }
171         { "R! "  "!"  }
172         { "R\" " "\"" }
173         { "R# "  "#"  }
174         { "R' "  "'"  }
175         { "R( "  ")"  }
176         { "R@ "  "@"  }
177         { "R[ "  "]"  }
178         { "R` "  "`"  }
179         { "R{ "  "}"  }
180         { "R| "  "|"  }
181     } swap [ subseq? not nip ] curry assoc-find drop ;
182
183 : take-until ( end lexer -- string )
184     dup skip-blank [
185         [ index-from ] 2keep
186         [ swapd subseq ]
187         [ 2drop 1+ ] 3bi
188     ] change-lexer-column ;
189
190 : parse-noblank-token ( lexer -- str/f )
191     dup still-parsing-line? [ (parse-token) ] [ drop f ] if ;
192
193 : parsing-regexp ( accum end -- accum )
194     lexer get [ take-until ] [ parse-noblank-token ] bi
195     <optioned-regexp> compile-next-match parsed ;
196
197 PRIVATE>
198
199 : R! CHAR: ! parsing-regexp ; parsing
200 : R" CHAR: " parsing-regexp ; parsing
201 : R# CHAR: # parsing-regexp ; parsing
202 : R' CHAR: ' parsing-regexp ; parsing
203 : R( CHAR: ) parsing-regexp ; parsing
204 : R/ CHAR: / parsing-regexp ; parsing
205 : R@ CHAR: @ parsing-regexp ; parsing
206 : R[ CHAR: ] parsing-regexp ; parsing
207 : R` CHAR: ` parsing-regexp ; parsing
208 : R{ CHAR: } parsing-regexp ; parsing
209 : R| CHAR: | parsing-regexp ; parsing
210
211 M: regexp pprint*
212     [
213         [
214             [ raw>> dup find-regexp-syntax swap % swap % % ]
215             [ options>> options>string % ] bi
216         ] "" make
217     ] keep present-text ;
218