2
/* Henry Spencer's implementation of Regular Expressions,
4
/* Refurbished by Stephen Gildea */
6
#include "tinyscheme/scheme-private.h"
9
#define EXPORT __declspec( dllexport )
14
/* Since not exported */
17
pointer foreign_re_match(scheme *sc, pointer args);
18
EXPORT void init_re(scheme *sc);
21
static void set_vector_elem(pointer vec, int ielem, pointer newel) {
24
vec[1+n]._object._cons._car=newel;
26
vec[1+n]._object._cons._cdr=newel;
30
pointer foreign_re_match(scheme *sc, pointer args) {
34
pointer first_arg, second_arg;
35
pointer third_arg=sc->NIL;
40
if(!((args != sc->NIL) && sc->vptr->is_string((first_arg = sc->vptr->pair_car(args)))
41
&& (args=sc->vptr->pair_cdr(args))
42
&& sc->vptr->is_pair(args) && sc->vptr->is_string((second_arg = sc->vptr->pair_car(args))))) {
45
pattern = sc->vptr->string_value(first_arg);
46
string = sc->vptr->string_value(second_arg);
48
args=sc->vptr->pair_cdr(args);
50
if(!(sc->vptr->is_pair(args) && sc->vptr->is_vector((third_arg = sc->vptr->pair_car(args))))) {
53
num=third_arg->_object._number.value.ivalue;
58
if(regcomp(&rt,pattern,REG_EXTENDED)!=0) {
63
retcode=regexec(&rt,string,0,0,0);
65
regmatch_t *pmatch=malloc((num+1)*sizeof(regmatch_t));
67
retcode=regexec(&rt,string,num+1,pmatch,0);
70
for(i=0; i<num; i++) {
72
set_vector_elem(third_arg, i,
73
sc->vptr->cons(sc, sc->vptr->mk_integer(sc, pmatch[i].rm_so),
74
sc->vptr->mk_integer(sc, pmatch[i].rm_eo)));
94
static char* utilities=";; return the substring of STRING matched in MATCH-VECTOR, \n"
95
";; the Nth subexpression match (default 0).\n"
96
"(define (re-match-nth string match-vector . n)\n"
97
" (let ((n (if (pair? n) (car n) 0)))\n"
98
" (substring string (car (vector-ref match-vector n))\n"
99
" (cdr (vector-ref match-vector n)))))\n"
100
"(define (re-before-nth string match-vector . n)\n"
101
" (let ((n (if (pair? n) (car n) 0)))\n"
102
" (substring string 0 (car (vector-ref match-vector n)))))\n"
103
"(define (re-after-nth string match-vector . n)\n"
104
" (let ((n (if (pair? n) (car n) 0)))\n"
105
" (substring string (cdr (vector-ref match-vector n))\n"
106
" (string-length string))))\n";
109
void init_re(scheme *sc) {
110
sc->vptr->scheme_define(sc,sc->global_env,sc->vptr->mk_symbol(sc,"re-match"),sc->vptr->mk_foreign_func(sc, foreign_re_match));
111
/* sc->vptr->load_string(sc,utilities);*/