]> gitweb.factorcode.org Git - factor.git/blob - basis/regexp/regexp.factor
Merge branch 'master' into experimental (untested!)
[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 -- seq )
65     [ dup length 0 > ] swap '[ _ re-cut ] [ ] produce nip ;
66
67 : re-replace ( string regexp replacement -- result )
68     [ re-split ] dip join ;
69
70 : next-match ( string regexp -- end/f match/f )
71     dupd first-match dup
72     [ [ split1-slice nip ] keep ] [ 2drop f f ] if ;
73
74 : all-matches ( string regexp -- seq )
75     [ dup ] swap '[ _ next-match ] [ ] produce nip harvest ;
76
77 : count-matches ( string regexp -- n )
78     all-matches length ;
79
80 <PRIVATE
81
82 : find-regexp-syntax ( string -- prefix suffix )
83     {
84         { "R/ "  "/"  }
85         { "R! "  "!"  }
86         { "R\" " "\"" }
87         { "R# "  "#"  }
88         { "R' "  "'"  }
89         { "R( "  ")"  }
90         { "R@ "  "@"  }
91         { "R[ "  "]"  }
92         { "R` "  "`"  }
93         { "R{ "  "}"  }
94         { "R| "  "|"  }
95     } swap [ subseq? not nip ] curry assoc-find drop ;
96
97 : string>options ( string -- options )
98     [ ch>option dup ] H{ } map>assoc ;
99
100 : options>string ( options -- string )
101     keys [ option>ch ] map natural-sort >string ;
102
103 PRIVATE>
104
105 : <optioned-regexp> ( string option-string -- regexp )
106     [ default-regexp ] [ string>options ] bi* >>options
107     construct-regexp ;
108
109 : <regexp> ( string -- regexp ) "" <optioned-regexp> ;
110
111 <PRIVATE
112
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 ;
119
120 PRIVATE>
121
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
133
134 M: regexp pprint*
135     [
136         [
137             [ raw>> dup find-regexp-syntax swap % swap % % ]
138             [ options>> options>string % ] bi
139         ] "" make
140     ] keep present-text ;