2013-09-18 16:45:17 -04:00
|
|
|
USING:
|
2013-09-19 17:41:11 -04:00
|
|
|
accessors
|
|
|
|
alien.c-types alien.data
|
|
|
|
arrays
|
|
|
|
kernel
|
|
|
|
pcre.ffi
|
|
|
|
sequences ;
|
2013-09-18 16:45:17 -04:00
|
|
|
IN: pcre
|
|
|
|
|
2013-09-19 17:41:11 -04:00
|
|
|
ERROR: malformed-regexp expr error ;
|
2013-09-18 16:45:17 -04:00
|
|
|
|
2013-09-19 17:41:11 -04:00
|
|
|
TUPLE: compiled-pcre pcre extra ;
|
2013-09-18 16:45:17 -04:00
|
|
|
|
2013-09-19 17:41:11 -04:00
|
|
|
! Low-level
|
2013-09-18 16:45:17 -04:00
|
|
|
|
2013-09-19 17:41:11 -04:00
|
|
|
: exec ( pcre extra subject ofs -- count match-data )
|
|
|
|
[ dup length ] dip 0 30 int <c-array> [ 30 pcre_exec ] keep ;
|
2013-09-18 16:45:17 -04:00
|
|
|
|
2013-09-19 17:41:11 -04:00
|
|
|
: (pcre) ( expr -- pcre err-message err-offset )
|
|
|
|
0 { c-string int } [ f pcre_compile ] with-out-parameters ;
|
2013-09-18 16:45:17 -04:00
|
|
|
|
2013-09-19 17:41:11 -04:00
|
|
|
: <pcre> ( expr -- pcre )
|
|
|
|
dup (pcre) 2array swap [ 2nip ] [ malformed-regexp ] if* ;
|
|
|
|
|
|
|
|
: <pcre-extra> ( pcre -- pcre-extra )
|
|
|
|
0 { c-string } [ pcre_study ] with-out-parameters drop ;
|
|
|
|
|
|
|
|
! High-level
|
|
|
|
|
|
|
|
: <compiled-pcre> ( expr -- compiled-pcre )
|
|
|
|
<pcre> dup <pcre-extra> compiled-pcre boa ;
|
|
|
|
|
|
|
|
: findall ( subject compiled-pcre -- matches )
|
|
|
|
[ pcre>> ] [ extra>> ] bi rot 0 exec nip ;
|
2013-09-18 16:45:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
: info ( pcre -- x x x )
|
|
|
|
{ int int } [ pcre_info ] with-out-parameters ;
|
|
|
|
|
2013-09-19 17:41:11 -04:00
|
|
|
: fullinfo ( pcre pcre-extra what -- num x )
|
|
|
|
{ int } [ pcre_fullinfo ] with-out-parameters ;
|
|
|
|
|
|
|
|
: substring ( subject match-data count n -- str )
|
|
|
|
{ c-string } [ pcre_get_substring drop ] with-out-parameters ;
|