diff --git a/basis/pcre/ffi/ffi.factor b/basis/pcre/ffi/ffi.factor new file mode 100644 index 0000000000..e7ace4b1b7 --- /dev/null +++ b/basis/pcre/ffi/ffi.factor @@ -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 ( ) ; diff --git a/basis/pcre/pcre.factor b/basis/pcre/pcre.factor index 4e3d1974cf..811925d3d7 100644 --- a/basis/pcre/pcre.factor +++ b/basis/pcre/pcre.factor @@ -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 [ 30 pcre_exec ] keep ; -FUNCTION: pcre_extra* pcre_study ( void* pcre, int options, char** errptr ) ; - -FUNCTION: c-string pcre_version ( ) ; - -FUNCTION: uchar* pcre_maketables ( ) ; - -: ( expr -- pcre err-message err-offset ) +: (pcre) ( expr -- pcre err-message err-offset ) 0 { c-string int } [ f pcre_compile ] with-out-parameters ; +: ( expr -- pcre ) + dup (pcre) 2array swap [ 2nip ] [ malformed-regexp ] if* ; + +: ( pcre -- pcre-extra ) + 0 { c-string } [ pcre_study ] with-out-parameters drop ; + +! High-level + +: ( expr -- compiled-pcre ) + dup 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 ;