]> gitweb.factorcode.org Git - factor.git/blob - extra/pcre/pcre.factor
change ERROR: words from throw-foo back to foo.
[factor.git] / extra / pcre / pcre.factor
1 ! Copyright (C) 2013 Björn Lindqvist
2 ! See http://factorcode.org/license.txt for BSD license
3
4 USING: accessors alien alien.accessors alien.c-types alien.data
5 alien.enums alien.strings arrays assocs combinators fry
6 io.encodings.string io.encodings.utf8 kernel literals math
7 math.bitwise pcre.ffi sequences splitting strings ;
8 QUALIFIED: regexp
9 IN: pcre
10
11 ERROR: bad-option what ;
12
13 ERROR: malformed-regexp expr error ;
14
15 ERROR: pcre-error value ;
16
17 <PRIVATE
18
19 : replace-all ( seq subseqs new -- seq )
20     swapd '[ _ replace ] reduce ;
21
22 : split-subseqs ( seq subseqs -- seqs )
23     dup first [ replace-all ] keep split-subseq [ >string ] map harvest ;
24
25 : utf8-start-byte? ( byte -- ? )
26     0xc0 bitand 0x80 = not ;
27
28 : next-utf8-char ( byte-array pos -- pos' )
29     1 + 2dup swap ?nth [
30         utf8-start-byte? [ nip ] [ next-utf8-char ] if
31     ] [ 2drop f ] if* ;
32
33 : check-bad-option ( err value what -- value )
34     rot 0 = [ drop ] [ bad-option ] if ;
35
36 : pcre-config ( what -- value )
37     [
38         dup {
39             PCRE_CONFIG_MATCH_LIMIT
40             PCRE_CONFIG_MATCH_LIMIT_RECURSION
41         } member? [
42             { long } [ pcre_config ] with-out-parameters
43         ] [
44             { int } [ pcre_config ] with-out-parameters
45         ] if
46     ] keep check-bad-option ;
47
48 : pcre-fullinfo ( pcre extra what -- obj )
49     [ { int } [ pcre_fullinfo ] with-out-parameters ] keep
50     check-bad-option ;
51
52 : pcre-substring-list ( subject match-array count -- alien )
53     { void* } [ pcre_get_substring_list drop ] with-out-parameters ;
54
55 : name-count ( pcre extra -- n )
56     PCRE_INFO_NAMECOUNT pcre-fullinfo ;
57
58 : name-table ( pcre extra -- addr )
59     [ drop alien-address 32 on-bits unmask ]
60     ! On at least win64, the pointer is returned as an int and is
61     ! negative. Cast it to a uint and everything works.
62     [ PCRE_INFO_NAMETABLE pcre-fullinfo int <ref> uint deref ] 2bi + ;
63
64 : name-entry-size ( pcre extra -- size )
65     PCRE_INFO_NAMEENTRYSIZE pcre-fullinfo ;
66
67 : name-table-entry ( addr -- group-index group-name )
68     [ <alien> 1 alien-unsigned-1 ]
69     [ 2 + <alien> utf8 alien>string ] bi ;
70
71 : name-table-entries ( pcre extra -- addrs )
72     [ name-table ] [ name-entry-size ] [ name-count ] 2tri
73     iota [ * + name-table-entry 2array ] 2with map ;
74
75 : options ( pcre -- opts )
76     f PCRE_INFO_OPTIONS pcre-fullinfo ;
77
78 CONSTANT: default-opts flags{ PCRE_UTF8 PCRE_UCP }
79
80 : (pcre) ( expr -- pcre err-message err-offset )
81     default-opts { c-string int } [ f pcre_compile ] with-out-parameters ;
82
83 : <pcre> ( expr -- pcre )
84     dup (pcre) 2array swap [ 2nip ] [ malformed-regexp ] if* ;
85
86 : <pcre-extra> ( pcre -- pcre-extra )
87     0 { c-string } [ pcre_study ] with-out-parameters drop ;
88
89 : exec ( pcre extra subject ofs opts -- count match-data )
90     [ dup length ] 2dip 30 int <c-array> 30 [ pcre_exec ] 2keep drop ;
91
92 TUPLE: matcher pcre extra subject ofs exec-opts ;
93
94 : <matcher> ( subject compiled-pcre -- matcher )
95     [ utf8 encode ] dip [ pcre>> ] [ extra>> ] bi rot 0 0 matcher boa ;
96
97 CONSTANT: empty-match-opts flags{ PCRE_NOTEMPTY_ATSTART PCRE_ANCHORED }
98
99 : findnext ( matcher -- matcher match/f )
100     dup {
101         [ pcre>> ]
102         [ extra>> ]
103         [ subject>> ]
104         [ ofs>> ]
105         [ exec-opts>> ]
106     } cleave exec over dup -1 < [
107         PCRE_ERRORS number>enum pcre-error
108     ] [
109         -1 = [
110             2drop dup exec-opts>> 0 =
111             [ f ]
112             [
113                 dup [ subject>> ] [ ofs>> ] bi next-utf8-char
114                 [ >>ofs 0 >>exec-opts findnext ] [ f ] if*
115             ] if
116         ] [
117             [
118                 nip
119                 [ first2 = [ empty-match-opts ] [ 0 ] if >>exec-opts ]
120                 [ second >>ofs ] bi
121             ] [
122                 2array
123             ] 2bi
124         ] if
125     ] if ;
126
127 : parse-match ( subject nametable match-data -- match )
128     swapd first2 swap [ pcre-substring-list ] keep void* <c-direct-array>
129     [ utf8 alien>string ] { } map-as [ of swap 2array ] with map-index ;
130
131 PRIVATE>
132
133 TUPLE: compiled-pcre pcre extra nametable ;
134
135 : <compiled-pcre> ( expr -- compiled-pcre )
136     <pcre> dup <pcre-extra> 2dup name-table-entries compiled-pcre boa ;
137
138 : has-option? ( compiled-pcre option -- ? )
139     [ pcre>> options ] dip bitand 0 > ;
140
141 GENERIC: findall ( subject obj -- matches )
142
143 M: compiled-pcre findall
144     [ <matcher> [ findnext dup ] [ ] produce 2nip ]
145     [ nametable>> rot [ parse-match ] 2with { } map-as ] 2bi ;
146
147 M: string findall
148     <compiled-pcre> findall ;
149
150 M: regexp:regexp findall
151     raw>> findall ;
152
153 : matches? ( subject obj -- ? )
154     dupd findall [ nip length 1 = ] [ ?first ?first ?last = ] 2bi and ;
155
156 : split ( subject obj -- strings )
157     dupd findall [ first second ] map split-subseqs ;