pcre: split the module into a ffi part and a main part for easier maintanability

db4
Björn Lindqvist 2013-09-19 23:41:11 +02:00 committed by John Benediktsson
parent ad1e1d8455
commit 152ccf4b27
2 changed files with 99 additions and 30 deletions

66
basis/pcre/ffi/ffi.factor Normal file
View File

@ -0,0 +1,66 @@
USING:
alien alien.c-types alien.data alien.libraries alien.syntax
classes.struct
combinators
system ;
IN: pcre.ffi
<< {
{ [ os unix? ] [ "libpcre" "libpcre.so" cdecl add-library ] }
} cond >>
CONSTANT: PCRE_INFO_OPTIONS 0
CONSTANT: PCRE_INFO_SIZE 1
CONSTANT: PCRE_INFO_CAPTURECOUNT 2
CONSTANT: PCRE_INFO_BACKREFMAX 3
CONSTANT: PCRE_INFO_FIRSTBYTE 4
CONSTANT: PCRE_INFO_FIRSTCHAR 4
CONSTANT: PCRE_INFO_FIRSTTABLE 5
CONSTANT: PCRE_INFO_LASTLITERAL 6
CONSTANT: PCRE_INFO_NAMEENTRYSIZE 7
CONSTANT: PCRE_INFO_NAMECOUNT 8
CONSTANT: PCRE_INFO_NAMETABLE 9
CONSTANT: PCRE_INFO_STUDYSIZE 10
CONSTANT: PCRE_INFO_DEFAULT_TABLES 11
CONSTANT: PCRE_INFO_OKPARTIAL 12
CONSTANT: PCRE_INFO_JCHANGED 13
CONSTANT: PCRE_INFO_HASCRORLF 14
CONSTANT: PCRE_INFO_MINLENGTH 15
STRUCT: pcre_extra
{ flags int }
{ study_data void* }
{ match_limit long }
{ callout_data void* }
{ tables uchar* }
{ match_limit_recursion int }
{ mark uchar** } ;
FUNCTION: void* pcre_compile ( c-string pattern,
int options,
char** errptr,
int* erroffset,
char* tableptr ) ;
FUNCTION: int pcre_info ( void* pcre, int* optptr, int* first_byte ) ;
FUNCTION: int pcre_fullinfo ( void* pcre, pcre_extra* extra, int what, void *where ) ;
FUNCTION: pcre_extra* pcre_study ( void* pcre, int options, char** errptr ) ;
FUNCTION: int pcre_exec ( void* pcre,
pcre_extra* extra,
c-string subject,
int length,
int startoffset,
int options,
int* ovector,
int ovecsize ) ;
FUNCTION: int pcre_get_substring ( c-string subject,
int* ovector,
int stringcount,
int stringnumber,
void *stringptr ) ;
FUNCTION: c-string pcre_version ( ) ;
FUNCTION: uchar* pcre_maketables ( ) ;

View File

@ -1,42 +1,45 @@
USING:
alien alien.c-types alien.data alien.libraries alien.syntax
classes.struct
combinators
system ;
accessors
alien.c-types alien.data
arrays
kernel
pcre.ffi
sequences ;
IN: pcre
<< {
{ [ os unix? ] [ "libpcre" "libpcre.so" cdecl add-library ] }
} cond >>
ERROR: malformed-regexp expr error ;
STRUCT: pcre_extra
{ flags int }
{ study_data void* }
{ match_limit long }
{ callout_data void* }
{ tables uchar* }
{ match_limit_recursion int }
{ mark uchar** } ;
TUPLE: compiled-pcre pcre extra ;
FUNCTION: void* pcre_compile ( c-string pattern,
int options,
char** errptr,
int* erroffset,
char* tableptr ) ;
! Low-level
FUNCTION: int pcre_info ( void* pcre, int* optptr, int* first_byte ) ;
: exec ( pcre extra subject ofs -- count match-data )
[ dup length ] dip 0 30 int <c-array> [ 30 pcre_exec ] keep ;
FUNCTION: pcre_extra* pcre_study ( void* pcre, int options, char** errptr ) ;
FUNCTION: c-string pcre_version ( ) ;
FUNCTION: uchar* pcre_maketables ( ) ;
: <pcre> ( expr -- pcre err-message err-offset )
: (pcre) ( expr -- pcre err-message err-offset )
0 { c-string int } [ f pcre_compile ] with-out-parameters ;
: <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 ;
: info ( pcre -- x x x )
{ int int } [ pcre_info ] with-out-parameters ;
: study ( pcre -- pcre-extra err-message )
0 { c-string } [ pcre_study ] with-out-parameters ;
: 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 ;