1 ! Copyright (C) 2008 Doug Coleman.
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 regexp.backend regexp.utils
6 regexp.parser regexp.nfa regexp.dfa regexp.traversal
7 regexp.transition-tables splitting sorting ;
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
22 : construct-regexp ( regexp -- regexp' )
30 : (match) ( string regexp -- dfa-traverser )
31 <dfa-traverser> do-match ; inline
33 : match ( string regexp -- slice/f )
34 (match) return-match ;
36 : match* ( string regexp -- slice/f captured-groups )
37 (match) [ return-match ] [ captured-groups>> ] bi ;
39 : matches? ( string regexp -- ? )
41 [ [ length ] bi@ = ] [ drop f ] if* ;
43 : match-head ( string regexp -- end/f ) match [ length ] [ f ] if* ;
45 : match-at ( string m regexp -- n/f finished? )
47 2dup swap length > [ 2drop f f ] [ tail-slice t ] if
48 ] dip swap [ match-head f ] [ 2drop f t ] if ;
50 : match-range ( string m regexp -- a/f b/f )
52 drop nip rot drop dupd +
54 [ 3drop drop f f ] [ drop [ 1+ ] dip match-range ] if
57 : first-match ( string regexp -- slice/f )
58 dupd 0 swap match-range rot over [ <slice> ] [ 3drop f ] if ;
60 : re-cut ( string regexp -- end/f start )
62 [ split1-slice swap ] [ "" like f swap ] if* ;
64 : re-split ( string regexp -- seq )
65 [ dup length 0 > ] swap '[ _ re-cut ] [ ] produce nip ;
67 : re-replace ( string regexp replacement -- result )
68 [ re-split ] dip join ;
70 : next-match ( string regexp -- end/f match/f )
72 [ [ split1-slice nip ] keep ] [ 2drop f f ] if ;
74 : all-matches ( string regexp -- seq )
75 [ dup ] swap '[ _ next-match ] [ ] produce nip harvest ;
77 : count-matches ( string regexp -- n )
82 : find-regexp-syntax ( string -- prefix suffix )
95 } swap [ subseq? not nip ] curry assoc-find drop ;
97 : string>options ( string -- options )
98 [ ch>option dup ] H{ } map>assoc ;
100 : options>string ( options -- string )
101 keys [ option>ch ] map natural-sort >string ;
105 : <optioned-regexp> ( string option-string -- regexp )
106 [ default-regexp ] [ string>options ] bi* >>options
109 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
113 : parsing-regexp ( accum end -- accum )
114 lexer get dup skip-blank
115 [ [ index-from dup 1+ swap ] 2keep swapd subseq swap ] change-lexer-column
116 lexer get dup still-parsing-line?
117 [ (parse-token) ] [ drop f ] if
118 <optioned-regexp> parsed ;
122 : R! CHAR: ! parsing-regexp ; parsing
123 : R" CHAR: " parsing-regexp ; parsing
124 : R# CHAR: # parsing-regexp ; parsing
125 : R' CHAR: ' parsing-regexp ; parsing
126 : R( CHAR: ) parsing-regexp ; parsing
127 : R/ CHAR: / parsing-regexp ; parsing
128 : R@ CHAR: @ parsing-regexp ; parsing
129 : R[ CHAR: ] parsing-regexp ; parsing
130 : R` CHAR: ` parsing-regexp ; parsing
131 : R{ CHAR: } parsing-regexp ; parsing
132 : R| CHAR: | parsing-regexp ; parsing
137 [ raw>> dup find-regexp-syntax swap % swap % % ]
138 [ options>> options>string % ] bi
140 ] keep present-text ;