1 ! Copyright (C) 2008 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors combinators kernel math sequences strings
4 sets assocs prettyprint.backend make lexer namespaces parser
5 arrays fry regexp.backend regexp.utils regexp.parser regexp.nfa
6 regexp.dfa regexp.traversal regexp.transition-tables splitting
10 : default-regexp ( string -- regexp )
13 <transition-table> >>nfa-table
14 <transition-table> >>dfa-table
15 <transition-table> >>minimized-table
16 H{ } clone >>nfa-traversal-flags
17 H{ } clone >>dfa-traversal-flags
21 : construct-regexp ( regexp -- regexp' )
29 : (match) ( string regexp -- dfa-traverser )
30 <dfa-traverser> do-match ; inline
32 : match ( string regexp -- slice/f )
33 (match) return-match ;
35 : match* ( string regexp -- slice/f captured-groups )
36 (match) [ return-match ] [ captured-groups>> ] bi ;
38 : matches? ( string regexp -- ? )
40 [ [ length ] bi@ = ] [ drop f ] if* ;
42 : match-head ( string regexp -- end/f ) match [ length ] [ f ] if* ;
44 : match-at ( string m regexp -- n/f finished? )
46 2dup swap length > [ 2drop f f ] [ tail-slice t ] if
47 ] dip swap [ match-head f ] [ 2drop f t ] if ;
49 : match-range ( string m regexp -- a/f b/f )
51 drop nip rot drop dupd +
53 [ 3drop drop f f ] [ drop [ 1+ ] dip match-range ] if
56 : first-match ( string regexp -- slice/f )
57 dupd 0 swap match-range rot over [ <slice> ] [ 3drop f ] if ;
59 : re-cut ( string regexp -- end/f start )
61 [ split1-slice swap ] [ "" like f swap ] if* ;
63 : re-split ( string regexp -- seq )
64 [ dup length 0 > ] swap '[ _ re-cut ] [ ] produce nip ;
66 : re-replace ( string regexp replacement -- result )
67 [ re-split ] dip join ;
69 : next-match ( string regexp -- end/f match/f )
71 [ [ split1-slice nip ] keep ] [ 2drop f f ] if ;
73 : all-matches ( string regexp -- seq )
74 [ dup ] swap '[ _ next-match ] [ ] produce nip harvest ;
76 : count-matches ( string regexp -- n )
81 : find-regexp-syntax ( string -- prefix suffix )
94 } swap [ subseq? not nip ] curry assoc-find drop ;
96 ERROR: unknown-regexp-option option ;
98 : option>ch ( option -- string )
100 { case-insensitive [ CHAR: i ] }
101 { multiline [ CHAR: m ] }
102 { reversed-regexp [ CHAR: r ] }
103 { dotall [ CHAR: s ] }
104 [ unknown-regexp-option ]
107 : ch>option ( ch -- option )
109 { CHAR: i [ case-insensitive ] }
110 { CHAR: m [ multiline ] }
111 { CHAR: r [ reversed-regexp ] }
112 { CHAR: s [ dotall ] }
113 [ unknown-regexp-option ]
116 : string>options ( string -- options )
117 [ ch>option dup ] H{ } map>assoc ;
119 : options>string ( options -- string )
120 keys [ option>ch ] map natural-sort >string ;
124 : <optioned-regexp> ( string option-string -- regexp )
125 [ default-regexp ] [ string>options ] bi* >>options
128 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
132 : parsing-regexp ( accum end -- accum )
133 lexer get dup skip-blank
134 [ [ index-from dup 1+ swap ] 2keep swapd subseq swap ] change-lexer-column
135 lexer get dup still-parsing-line?
136 [ (parse-token) ] [ drop f ] if
137 <optioned-regexp> parsed ;
141 : R! CHAR: ! parsing-regexp ; parsing
142 : R" CHAR: " parsing-regexp ; parsing
143 : R# CHAR: # parsing-regexp ; parsing
144 : R' CHAR: ' parsing-regexp ; parsing
145 : R( CHAR: ) parsing-regexp ; parsing
146 : R/ CHAR: / parsing-regexp ; parsing
147 : R@ CHAR: @ parsing-regexp ; parsing
148 : R[ CHAR: ] parsing-regexp ; parsing
149 : R` CHAR: ` parsing-regexp ; parsing
150 : R{ CHAR: } parsing-regexp ; parsing
151 : R| CHAR: | parsing-regexp ; parsing
156 [ raw>> dup find-regexp-syntax swap % swap % % ]
157 [ options>> options>string % ] bi
159 ] keep present-text ;