]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/regexp.factor
Merge branch 'master' into experimental
[factor.git] / basis / regexp / regexp.factor
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 ;
8 IN: regexp
9
10 : default-regexp ( string -- regexp )
11     regexp new
12         swap >>raw
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
18         H{ } clone >>options
19         H{ } clone >>matchers
20         reset-regexp ;
21
22 : construct-regexp ( regexp -- regexp' )
23     {
24         [ parse-regexp ]
25         [ construct-nfa ]
26         [ construct-dfa ]
27         [ ]
28     } cleave ;
29
30 : (match) ( string regexp -- dfa-traverser )
31     <dfa-traverser> do-match ; inline
32
33 : match ( string regexp -- slice/f )
34     (match) return-match ;
35
36 : match* ( string regexp -- slice/f captured-groups )
37     (match) [ return-match ] [ captured-groups>> ] bi ;
38
39 : matches? ( string regexp -- ? )
40     dupd match
41     [ [ length ] bi@ = ] [ drop f ] if* ;
42
43 : match-head ( string regexp -- end/f ) match [ length ] [ f ] if* ;
44
45 : match-at ( string m regexp -- n/f finished? )
46     [
47         2dup swap length > [ 2drop f f ] [ tail-slice t ] if
48     ] dip swap [ match-head f ] [ 2drop f t ] if ;
49
50 : match-range ( string m regexp -- a/f b/f )
51     3dup match-at over [
52         drop nip rot drop dupd +
53     ] [
54         [ 3drop drop f f ] [ drop [ 1+ ] dip match-range ] if
55     ] if ;
56
57 : first-match ( string regexp -- slice/f )
58     dupd 0 swap match-range rot over [ <slice> ] [ 3drop f ] if ;
59
60 : re-cut ( string regexp -- end/f start )
61     dupd first-match
62     [ split1-slice swap ] [ "" like f swap ] if* ;
63
64 : (re-split) ( string regexp -- )
65     over [ [ re-cut , ] keep (re-split) ] [ 2drop ] if ;
66
67 : re-split ( string regexp -- seq )
68     [ (re-split) ] { } make ;
69
70 : re-replace ( string regexp replacement -- result )
71     [ re-split ] dip join ;
72
73 : next-match ( string regexp -- end/f match/f )
74     dupd first-match dup
75     [ [ split1-slice nip ] keep ] [ 2drop f f ] if ;
76
77 : all-matches ( string regexp -- seq )
78     [ dup ] swap '[ _ next-match ] [ ] produce nip harvest ;
79
80 : count-matches ( string regexp -- n )
81     all-matches length ;
82
83 <PRIVATE
84
85 : find-regexp-syntax ( string -- prefix suffix )
86     {
87         { "R/ "  "/"  }
88         { "R! "  "!"  }
89         { "R\" " "\"" }
90         { "R# "  "#"  }
91         { "R' "  "'"  }
92         { "R( "  ")"  }
93         { "R@ "  "@"  }
94         { "R[ "  "]"  }
95         { "R` "  "`"  }
96         { "R{ "  "}"  }
97         { "R| "  "|"  }
98     } swap [ subseq? not nip ] curry assoc-find drop ;
99
100 : string>options ( string -- options )
101     [ ch>option dup ] H{ } map>assoc ;
102
103 : options>string ( options -- string )
104     keys [ option>ch ] map natural-sort >string ;
105
106 PRIVATE>
107
108 : <optioned-regexp> ( string option-string -- regexp )
109     [ default-regexp ] [ string>options ] bi* >>options
110     construct-regexp ;
111
112 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
113
114 <PRIVATE
115
116 : parsing-regexp ( accum end -- accum )
117     lexer get dup skip-blank
118     [ [ index-from dup 1+ swap ] 2keep swapd subseq swap ] change-lexer-column
119     lexer get dup still-parsing-line?
120     [ (parse-token) ] [ drop f ] if
121     <optioned-regexp> parsed ;
122
123 PRIVATE>
124
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
133 : R` CHAR: ` parsing-regexp ; parsing
134 : R{ CHAR: } parsing-regexp ; parsing
135 : R| CHAR: | parsing-regexp ; parsing
136
137 M: regexp pprint*
138     [
139         [
140             [ raw>> dup find-regexp-syntax swap % swap % % ]
141             [ options>> options>string % ] bi
142         ] "" make
143     ] keep present-text ;