summaryrefslogtreecommitdiffstats
path: root/Modules/regexmodule.c
blob: c4b15ff0536f0af97c04688d9c6d07d49eee12d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
XXX support range parameter on search
XXX support mstop parameter on search
*/

/***********************************************************
Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Amsterdam, The Netherlands.

                        All Rights Reserved

Permission to use, copy, modify, and distribute this software and its 
documentation for any purpose and without fee is hereby granted, 
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in 
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.

STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

******************************************************************/

/* Regular expression objects */
/* This uses Tatu Ylonen's copyleft-free reimplementation of
   GNU regular expressions */

#include "allobjects.h"
#include "modsupport.h"

#include "regexpr.h"
#include "ctype.h"

static object *RegexError;	/* Exception */	

typedef struct {
	OB_HEAD
	struct re_pattern_buffer re_patbuf; /* The compiled expression */
	struct re_registers re_regs; /* The registers from the last match */
	char re_fastmap[256];	/* Storage for fastmap */
	object *re_translate;	/* String object for translate table */
	object *re_lastok;	/* String object last matched/searched */
	object *re_groupindex;	/* Group name to index dictionary */
	object *re_givenpat;	/* Pattern with symbolic groups */
	object *re_realpat;	/* Pattern without symbolic groups */
} regexobject;

/* Regex object methods */

static void
reg_dealloc(re)
	regexobject *re;
{
	XDECREF(re->re_translate);
	XDECREF(re->re_lastok);
	XDECREF(re->re_groupindex);
	XDECREF(re->re_givenpat);
	XDECREF(re->re_realpat);
	DEL(re);
}

static object *
makeresult(regs)
	struct re_registers *regs;
{
	object *v = newtupleobject(RE_NREGS);
	if (v != NULL) {
		int i;
		for (i = 0; i < RE_NREGS; i++) {
			object *w;
			w = mkvalue("(ii)", regs->start[i], regs->end[i]);
			if (w == NULL) {
				XDECREF(v);
				v = NULL;
				break;
			}
			settupleitem(v, i, w);
		}
	}
	return v;
}

static object *
reg_match(re, args)
	regexobject *re;
	object *args;
{
	object *argstring;
	char *buffer;
	int size;
	int offset;
	int result;
	if (getargs(args, "S", &argstring)) {
		offset = 0;
	}
	else {
		err_clear();
		if (!getargs(args, "(Si)", &argstring, &offset))
			return NULL;
	}
	buffer = getstringvalue(argstring);
	size = getstringsize(argstring);
	if (offset < 0 || offset > size) {
		err_setstr(RegexError, "match offset out of range");
		return NULL;
	}
	XDECREF(re->re_lastok);
	re->re_lastok = NULL;
	result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs);
	if (result < -1) {
		/* Failure like stack overflow */
		err_setstr(RegexError, "match failure");
		return NULL;
	}
	if (result >= 0) {
		INCREF(argstring);
		re->re_lastok = argstring;
	}
	return newintobject((long)result); /* Length of the match or -1 */
}

static object *
reg_search(re, args)
	regexobject *re;
	object *args;
{
	object *argstring;
	char *buffer;
	int size;
	int offset;
	int range;
	int result;
	
	if (getargs(args, "S", &argstring)) {
		offset = 0;
	}
	else {
		err_clear();
		if (!getargs(args, "(Si)", &argstring, &offset))
			return NULL;
	}
	buffer = getstringvalue(argstring);
	size = getstringsize(argstring);
	if (offset < 0 || offset > size) {
		err_setstr(RegexError, "search offset out of range");
		return NULL;
	}
	/* NB: In Emacs 18.57, the documentation for re_search[_2] and
	   the implementation don't match: the documentation states that
	   |range| positions are tried, while the code tries |range|+1
	   positions.  It seems more productive to believe the code! */
	range = size - offset;
	XDECREF(re->re_lastok);
	re->re_lastok = NULL;
	result = re_search(&re->re_patbuf, buffer, size, offset, range,
			   &re->re_regs);
	if (result < -1) {
		/* Failure like stack overflow */
		err_setstr(RegexError, "match failure");
		return NULL;
	}
	if (result >= 0) {
		INCREF(argstring);
		re->re_lastok = argstring;
	}
	return newintobject((long)result); /* Position of the match or -1 */
}

static object *
reg_group(re, args)
	regexobject *re;
	object *args;
{
	int i, a, b;
	if (args != NULL && is_tupleobject(args)) {
		int n = gettuplesize(args);
		object *res = newtupleobject(n);
		if (res == NULL)
			return NULL;
		for (i = 0; i < n; i++) {
			object *v = reg_group(re, gettupleitem(args, i));
			if (v == NULL) {
				DECREF(res);
				return NULL;
			}
			settupleitem(res, i, v);
		}
		return res;
	}
	if (!getargs(args, "i", &i)) {
		object *n;
		err_clear();
		if (!getargs(args, "S", &n))
			return NULL;
		else {
			object *index;
			if (re->re_groupindex == NULL)
				index = NULL;
			else
				index = mappinglookup(re->re_groupindex, n);
			if (index == NULL) {
				err_setstr(RegexError, "group() group name doesn't exist");
				return NULL;
			}
			i = getintvalue(index);
		}
	}
	if (i < 0 || i >= RE_NREGS) {
		err_setstr(RegexError, "group() index out of range");
		return NULL;
	}
	if (re->re_lastok == NULL) {
		err_setstr(RegexError,
		    "group() only valid after successful match/search");
		return NULL;
	}
	a = re->re_regs.start[i];
	b = re->re_regs.end[i];
	if (a < 0 || b < 0) {
		INCREF(None);
		return None;
	}
	return newsizedstringobject(getstringvalue(re->re_lastok)+a, b-a);
}

static struct methodlist reg_methods[] = {
	{"match",	(method)reg_match},
	{"search",	(method)reg_search},
	{"group",	(method)reg_group},
	{NULL,		NULL}		/* sentinel */
};

static object *
reg_getattr(re, name)
	regexobject *re;
	char *name;
{
	if (strcmp(name, "regs") == 0) {
		if (re->re_lastok == NULL) {
			INCREF(None);
			return None;
		}
		return makeresult(&re->re_regs);
	}
	if (strcmp(name, "last") == 0) {
		if (re->re_lastok == NULL) {
			INCREF(None);
			return None;
		}
		INCREF(re->re_lastok);
		return re->re_lastok;
	}
	if (strcmp(name, "translate") == 0) {
		if (re->re_translate == NULL) {
			INCREF(None);
			return None;
		}
		INCREF(re->re_translate);
		return re->re_translate;
	}
	if (strcmp(name, "groupindex") == 0) {
		if (re->re_groupindex == NULL) {
			INCREF(None);
			return None;
		}
		INCREF(re->re_groupindex);
		return re->re_groupindex;
	}
	if (strcmp(name, "realpat") == 0) {
		if (re->re_realpat == NULL) {
			INCREF(None);
			return None;
		}
		INCREF(re->re_realpat);
		return re->re_realpat;
	}
	if (strcmp(name, "givenpat") == 0) {
		if (re->re_givenpat == NULL) {
			INCREF(None);
			return None;
		}
		INCREF(re->re_givenpat);
		return re->re_givenpat;
	}
	if (strcmp(name, "__members__") == 0) {
		object *list = newlistobject(6);
		if (list) {
			setlistitem(list, 0, newstringobject("last"));
			setlistitem(list, 1, newstringobject("regs"));
			setlistitem(list, 2, newstringobject("translate"));
			setlistitem(list, 3, newstringobject("groupindex"));
			setlistitem(list, 4, newstringobject("realpat"));
			setlistitem(list, 5, newstringobject("givenpat"));
			if (err_occurred()) {
				DECREF(list);
				list = NULL;
			}
		}
		return list;
	}
	return findmethod(reg_methods, (object *)re, name);
}

static typeobject Regextype = {
	OB_HEAD_INIT(&Typetype)
	0,			/*ob_size*/
	"regex",		/*tp_name*/
	sizeof(regexobject),	/*tp_size*/
	0,			/*tp_itemsize*/
	/* methods */
	(destructor)reg_dealloc, /*tp_dealloc*/
	0,			/*tp_print*/
	(getattrfunc)reg_getattr, /*tp_getattr*/
	0,			/*tp_setattr*/
	0,			/*tp_compare*/
	0,			/*tp_repr*/
};

static object *
newregexobject(pattern, translate, givenpat, groupindex)
	object *pattern;
	object *translate;
	object *givenpat;
	object *groupindex;
{
	regexobject *re;
	char *pat = getstringvalue(pattern);
	int size = getstringsize(pattern);

	if (translate != NULL && getstringsize(translate) != 256) {
		err_setstr(RegexError,
			   "translation table must be 256 bytes");
		return NULL;
	}
	re = NEWOBJ(regexobject, &Regextype);
	if (re != NULL) {
		char *error;
		re->re_patbuf.buffer = NULL;
		re->re_patbuf.allocated = 0;
		re->re_patbuf.fastmap = re->re_fastmap;
		if (translate)
			re->re_patbuf.translate = getstringvalue(translate);
		else
			re->re_patbuf.translate = NULL;
		XINCREF(translate);
		re->re_translate = translate;
		re->re_lastok = NULL;
		re->re_groupindex = groupindex;
		INCREF(pattern);
		re->re_realpat = pattern;
		INCREF(givenpat);
		re->re_givenpat = givenpat;
		error = re_compile_pattern(pat, size, &re->re_patbuf);
		if (error != NULL) {
			err_setstr(RegexError, error);
			DECREF(re);
			re = NULL;
		}
	}
	return (object *)re;
}

static object *
regex_compile(self, args)
	object *self;
	object *args;
{
	object *pat = NULL;
	object *tran = NULL;
	if (!getargs(args, "S", &pat)) {
		err_clear();
		if (!getargs(args, "(SS)", &pat, &tran))
			return NULL;
	}
	return newregexobject(pat, tran, pat, NULL);
}

static object *
symcomp(pattern, gdict)
	object *pattern;
	object *gdict;
{
	char *opat = getstringvalue(pattern);
	char *oend = opat + getstringsize(pattern);
	int group_count = 0;
	int escaped = 0;
	char *o = opat;
	char *n;
	char name_buf[128];
	char *g;
	object *npattern;
	int require_escape = re_syntax & RE_NO_BK_PARENS ? 0 : 1;

	npattern = newsizedstringobject((char*)NULL, getstringsize(pattern));
	if (npattern == NULL)
		return NULL;
	n = getstringvalue(npattern);

	while (o < oend) {
		if (*o == '(' && escaped == require_escape) {
			char *backtrack;
			escaped = 0;
			++group_count;
			*n++ = *o;
			if (++o >= oend || *o != '<')
				continue;
			/* *o == '<' */
			if (o+1 < oend && *(o+1) == '>')
				continue;
			backtrack = o;
			g = name_buf;
			for (++o; o < oend;) {
				if (*o == '>') {
					object *group_name = NULL;
					object *group_index = NULL;
					*g++ = '\0';
					group_name = newstringobject(name_buf);
					group_index = newintobject(group_count);
					if (group_name == NULL || group_index == NULL
					    || mappinginsert(gdict, group_name, group_index) != 0) {
						XDECREF(group_name);
						XDECREF(group_index);
						XDECREF(npattern);
						return NULL;
					}
					++o; /* eat the '>' */
					break;
				}
				if (!isalnum(*o) && *o != '_') {
					o = backtrack;
					break;
				}
				*g++ = *o++;
			}
		}
		if (*o == '[' && !escaped) {
			*n++ = *o;
			++o;	/* eat the char following '[' */
			*n++ = *o;
			while (o < oend && *o != ']') {
				++o;
				*n++ = *o;
			}
			if (o < oend)
				++o;
		}
		else if (*o == '\\') {
			escaped = 1;
			*n++ = *o;
			++o;
		}
		else {
			escaped = 0;
			*n++ = *o;
			++o;
		}
	}

	if (resizestring(&npattern, n - getstringvalue(npattern)) == 0)
		return npattern;
	else {
		DECREF(npattern);
		return NULL;
	}

}

static object *
regex_symcomp(self, args)
	object *self;
	object *args;
{
	object *pattern;
	object *tran = NULL;
	object *gdict = NULL;
	object *npattern;
	if (!getargs(args, "S", &pattern)) {
		err_clear();
		if (!getargs(args, "(SS)", &pattern, &tran))
			return NULL;
	}
	gdict = newmappingobject();
	if (gdict == NULL
	    || (npattern = symcomp(pattern, gdict)) == NULL) {
		DECREF(gdict);
		DECREF(pattern);
		return NULL;
	}
	return newregexobject(npattern, tran, pattern, gdict);
}


static object *cache_pat;
static object *cache_prog;

static int
update_cache(pat)
	object *pat;
{
	if (pat != cache_pat) {
		XDECREF(cache_pat);
		cache_pat = NULL;
		XDECREF(cache_prog);
		cache_prog = regex_compile((object *)NULL, pat);
		if (cache_prog == NULL)
			return -1;
		cache_pat = pat;
		INCREF(cache_pat);
	}
	return 0;
}

static object *
regex_match(self, args)
	object *self;
	object *args;
{
	object *pat, *string;
	if (!getargs(args, "(SS)", &pat, &string))
		return NULL;
	if (update_cache(pat) < 0)
		return NULL;
	return reg_match((regexobject *)cache_prog, string);
}

static object *
regex_search(self, args)
	object *self;
	object *args;
{
	object *pat, *string;
	if (!getargs(args, "(SS)", &pat, &string))
		return NULL;
	if (update_cache(pat) < 0)
		return NULL;
	return reg_search((regexobject *)cache_prog, string);
}

static object *
regex_set_syntax(self, args)
	object *self, *args;
{
	int syntax;
	if (!getintarg(args, &syntax))
		return NULL;
	syntax = re_set_syntax(syntax);
	return newintobject((long)syntax);
}

static struct methodlist regex_global_methods[] = {
	{"compile",	regex_compile},
	{"symcomp",	regex_symcomp},
	{"match",	regex_match},
	{"search",	regex_search},
	{"set_syntax",	regex_set_syntax},
	{NULL,		NULL}		/* sentinel */
};

initregex()
{
	object *m, *d, *v;
	
	m = initmodule("regex", regex_global_methods);
	d = getmoduledict(m);
	
	/* Initialize regex.error exception */
	RegexError = newstringobject("regex.error");
	if (RegexError == NULL || dictinsert(d, "error", RegexError) != 0)
		fatal("can't define regex.error");

	/* Initialize regex.casefold constant */
	v = newsizedstringobject((char *)NULL, 256);
	if (v != NULL) {
		int i;
		char *s = getstringvalue(v);
		for (i = 0; i < 256; i++) {
			if (isupper(i))
				s[i] = tolower(i);
			else
				s[i] = i;
		}
		dictinsert(d, "casefold", v);
		DECREF(v);
	}
}