Debian Codegraph @

rx/rgx.c

raw symbols SCIP
1 /* Copyright (C) 1995 Free Software Foundation, Inc.
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
6 * any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of SYSTAS.
19 *
20 * The exception is that, if you link the SYSTAS library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the SYSTAS library code into it.
25 *
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
28 *
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name SYSTAS. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * SYSTAS, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
36 *
37 * If you write modifications of your own for SYSTAS, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
40 */
41
42
43
44 #include <stdio.h>
45 #include <_scm.h>
46 #include "inst-rxposix.h"
47 #include "rxgnucomp.h"
48 #include "rxanal.h"
49 #include "rxunfa.h"
50 #include "rxbasic.h"
51
52
53 SCM_CONST_LONG (scm_REG_EXTENDED, "REG_EXTENDED", REG_EXTENDED);
54 SCM_CONST_LONG (scm_REG_ICASE, "REG_ICASE", REG_ICASE);
55 SCM_CONST_LONG (scm_REG_NEWLINE, "REG_NEWLINE", REG_NEWLINE);
56 SCM_CONST_LONG (scm_REG_NOSUB, "REG_NOSUB", REG_NOSUB);
57 SCM_CONST_LONG (scm_REG_NOTBOL, "REG_NOTBOL", REG_NOTBOL);
58 SCM_CONST_LONG (scm_REG_NOTEOL, "REG_NOTEOL", REG_NOTEOL);
59
60
61
62 long scm_tc16_regex_t;
63 #define RGX(X) ((regex_t *)SCM_CDR(X))
64 #define RGXP(X) (SCM_CAR(X) == (SCM)scm_tc16_regex_t)
65
66 #ifdef __STDC__
67 size_t
68 free_regex_t (SCM obj)
69 #else
70 size_t
71 free_regex_t (obj)
72 SCM obj;
73 #endif
74 {
75 regex_t *r;
76 r = RGX(obj);
77 free ((char *)(r->owner_data));
78 regfree (r);
79 return 0;
80 }
81
82 #ifdef __STDC__
83 int
84 print_regex_t (SCM obj, SCM port, int writing)
85 #else
86 int
87 print_regex_t (obj, port, writing)
88 SCM obj;
89 SCM port;
90 int writing;
91 #endif
92 {
93 regex_t *r;
94 r = RGX (obj);
95 scm_gen_puts (scm_regular_string, "#<rgx ", port);
96 scm_gen_puts (scm_regular_string, (char *)(r->owner_data), port);
97 scm_gen_puts (scm_regular_string, ">", port);
98 return 1;
99 }
100
101
102 static scm_smobfuns regex_t_smob =
103 { scm_mark0, free_regex_t, print_regex_t, 0 };
104
105
106 SCM_PROC (s_compiled_regexp_p, "compiled-regexp?", 1, 0, 0, scm_compiled_regexp_p);
107 #ifdef __STDC__
108 SCM
109 scm_compiled_regexp_p (SCM obj)
110 #else
111 SCM
112 scm_compiled_regexp_p (obj)
113 SCM obj;
114 #endif
115 {
116 return ((SCM_NIMP (obj) && RGXP (obj))
117 ? SCM_BOOL_T
118 : SCM_BOOL_F);
119 }
120
121 SCM_PROC(s_regcomp, "regcomp", 1, 1, 0, scm_regcomp);
122 #ifdef __STDC__
123 SCM
124 scm_regcomp (SCM pat, SCM cfl)
125 #else
126 SCM
127 scm_regcomp (pat, cfl)
128 SCM pat;
129 SCM cfl;
130 #endif
131 {
132 SCM answer;
133 SCM_ASSERT (SCM_NIMP (pat) && SCM_RO_STRINGP (pat), pat, SCM_ARG1, s_regcomp);
134 if (cfl == SCM_UNDEFINED)
135 cfl = SCM_INUM0;
136 SCM_ASSERT (SCM_INUMP (cfl), cfl, SCM_ARG2, s_regcomp);
137
138 SCM_NEWCELL (answer);
139 SCM_DEFER_INTS;
140 {
141 regex_t * it;
142 int status;
143 it = malloc (sizeof (*it));
144 if (!it)
145 {
146 allocation:
147 SCM_ALLOW_INTS;
148 SCM_ASSERT (0, pat, "allocation failure", s_regcomp);
149 }
150 status = regncomp (it, SCM_RO_CHARS (pat), SCM_RO_LENGTH (pat), SCM_INUM (cfl));
151 if (status)
152 {
153 free (it);
154 answer = SCM_MAKINUM (status);
155 }
156 else
157 {
158 it->owner_data = (void *)malloc (SCM_RO_LENGTH (pat) + 1);
159 if (!it->owner_data)
160 {
161 regfree (it);
162 free (it);
163 goto allocation;
164 }
165 memcpy (it->owner_data, SCM_RO_CHARS (pat), SCM_RO_LENGTH (pat));
166 ((char *)it->owner_data)[SCM_RO_LENGTH (pat)] = 0;
167 SCM_SETCAR (answer, scm_tc16_regex_t);
168 SCM_SETCDR (answer, (SCM)it);
169 }
170 }
171 SCM_ALLOW_INTS;
172 return answer;
173 }
174
175
176 SCM_PROC(s_regexec, "regexec", 2, 2, 0, scm_regexec);
177 #ifdef __STDC__
178 SCM
179 scm_regexec (SCM rgx, SCM str, SCM match_pick, SCM eflags)
180 #else
181 SCM
182 scm_regexec (rgx, str, match_pick, eflags)
183 SCM rgx;
184 SCM str;
185 SCM match_pick;
186 SCM eflags;
187 #endif
188 {
189 SCM answer;
190 SCM malloc_protect;
191 regmatch_t * pmatch;
192 int vector_result;
193
194 SCM_ASSERT (SCM_NIMP (rgx) && RGXP (rgx), rgx, SCM_ARG1, s_regexec);
195 SCM_ASSERT (SCM_NIMP (str) && SCM_RO_STRINGP (str), str, SCM_ARG2, s_regexec);
196 if (eflags == SCM_UNDEFINED)
197 eflags = SCM_INUM0;
198 SCM_ASSERT (SCM_INUMP (eflags), eflags, SCM_ARG4, s_regexec);
199
200 vector_result = (SCM_NIMP (match_pick) && SCM_VECTORP (match_pick));
201
202 if (RGX(rgx)->re_nsub)
203 {
204 SCM starts;
205 SCM ends;
206 starts = scm_make_vector (SCM_MAKINUM (RGX(rgx)->re_nsub), SCM_BOOL_F, SCM_BOOL_F);
207 ends = scm_make_vector (SCM_MAKINUM (RGX(rgx)->re_nsub), SCM_BOOL_F, SCM_BOOL_F);
208 answer = scm_cons (starts, ends);
209 }
210
211 malloc_protect = scm_malloc_obj (0);
212 SCM_DEFER_INTS;
213 {
214 int status;
215 pmatch = 0;
216 status = regnexec (RGX(rgx), SCM_RO_CHARS (str), SCM_RO_LENGTH (str),
217 0, &pmatch, SCM_INUM (eflags) | REG_ALLOC_REGS);
218 if (status)
219 {
220 SCM_ALLOW_INTS;
221 if (vector_result)
222 return SCM_MAKINUM (status);
223 else
224 {
225 if (status == REG_NOMATCH)
226 return SCM_BOOL_F;
227 else
228 scm_throw (SCM_CAR (scm_intern0 ("regexp-error")),
229 scm_listify (SCM_MAKINUM (status),
230 rgx,
231 str,
232 match_pick,
233 eflags,
234 SCM_UNDEFINED));
235 }
236 }
237 if (match_pick == SCM_BOOL_F)
238 free (pmatch);
239 else
240 SCM_SETMALLOCDATA (malloc_protect, pmatch);
241 }
242 SCM_ALLOW_INTS;
243
244 if (match_pick == SCM_BOOL_F)
245 return SCM_BOOL_T;
246 else if ((match_pick == SCM_BOOL_T) || (match_pick == SCM_UNDEFINED))
247 {
248 answer = (scm_listify
249 (scm_make_shared_substring (str, SCM_MAKINUM (0), SCM_MAKINUM (pmatch[0].rm_so)),
250 scm_make_shared_substring (str, SCM_MAKINUM (pmatch[0].rm_so), SCM_MAKINUM (pmatch[0].rm_eo)),
251 scm_make_shared_substring (str, SCM_MAKINUM (pmatch[0].rm_eo), SCM_UNDEFINED),
252 SCM_UNDEFINED));
253 return answer;
254 }
255 else if (vector_result)
256 {
257 int i;
258 size_t bound;
259 int vlen;
260
261 bound = RGX(rgx)->re_nsub;
262 vlen = SCM_LENGTH (match_pick);
263 if (vlen < bound)
264 bound = vlen;
265 for (i = 0; i < bound; ++i)
266 if (pmatch[0].rm_so >= 0)
267 SCM_VELTS (match_pick)[i] = scm_cons (SCM_MAKINUM (pmatch[i].rm_so),
268 SCM_MAKINUM (pmatch[i].rm_eo));
269 else
270 SCM_VELTS (match_pick)[i] = SCM_BOOL_F;
271 while (i < vlen)
272 {
273 SCM_VELTS (match_pick)[i] = SCM_BOOL_F;
274 ++i;
275 }
276 return match_pick;
277 }
278 else
279 {
280 SCM spec;
281 size_t bound;
282 SCM ans_pos;
283
284 answer = scm_cons (SCM_BOOL_F, SCM_BOOL_F);
285 ans_pos = answer;
286 bound = RGX(rgx)->re_nsub;
287
288 for (spec = match_pick; spec != SCM_EOL; spec = SCM_CDR (spec))
289 {
290 SCM item;
291 SCM frm;
292 SCM to;
293
294 SCM_ASSERT (SCM_NIMP (spec) && SCM_CONSP (spec), spec, SCM_ARG3, s_regexec);
295 item = SCM_CAR (spec);
296
297 if (SCM_ICHRP (item))
298 {
299 if (SCM_ICHR (item) == '<')
300 {
301 frm = SCM_INUM0;
302 to = SCM_MAKINUM (pmatch[0].rm_so);
303 SCM_SETCDR (ans_pos,
304 scm_cons (scm_make_shared_substring (str, frm, to), SCM_EOL));
305 }
306 else if (SCM_ICHR (item) == '>')
307 {
308 frm = SCM_MAKINUM (pmatch[0].rm_eo);
309 to = SCM_UNDEFINED;
310 SCM_SETCDR (ans_pos,
311 scm_cons (scm_make_shared_substring (str, frm, to), SCM_EOL));
312 }
313 else if (SCM_ICHR (item) == 'c')
314 {
315 SCM_SETCDR (ans_pos,
316 scm_cons (SCM_MAKINUM (pmatch[0].final_tag), SCM_EOL));
317 }
318 else
319 SCM_ASSERT (0, spec, SCM_ARG3, s_regexec);
320 ans_pos = SCM_CDR (ans_pos);
321 }
322 else if (SCM_NIMP (item) && SCM_CONSP (item))
323 {
324 SCM ipos;
325 int solved;
326
327 solved = 0;
328 for (ipos = item; SCM_NIMP (ipos) && SCM_CONSP (ipos); ipos = SCM_CDR (ipos))
329 {
330 SCM iitem;
331 int ival;
332 iitem = SCM_CAR (ipos);
333 SCM_ASSERT (SCM_INUMP (iitem), spec, SCM_ARG3, s_regexec);
334 ival = SCM_INUM (iitem);
335 SCM_ASSERT ((ival >= 0) && (ival < bound), spec, SCM_OUTOFRANGE, s_regexec);
336 if (pmatch[ival].rm_so >= 0)
337 {
338 SCM_SETCDR (ans_pos, scm_cons (SCM_MAKINUM (ival), SCM_EOL));
339 ans_pos = SCM_CDR (ans_pos);
340 solved = 1;
341 break;
342 }
343 }
344 if (!solved)
345 {
346 SCM_SETCDR (ans_pos, scm_cons (SCM_BOOL_F, SCM_EOL));
347 ans_pos = SCM_CDR (ans_pos);
348 }
349 }
350 else
351 {
352 int n;
353 SCM_ASSERT (SCM_INUMP (item), spec, SCM_ARG3, s_regexec);
354 n = SCM_INUM (item);
355 SCM_ASSERT ((n >= 0) && (n < bound), spec, SCM_OUTOFRANGE, s_regexec);
356 if (pmatch[n].rm_so < 0)
357 {
358 SCM_SETCDR (ans_pos, scm_cons (SCM_BOOL_F, SCM_EOL));
359 ans_pos = SCM_CDR (ans_pos);
360 continue;
361 }
362 frm = SCM_MAKINUM (pmatch[n].rm_so);
363 to = SCM_MAKINUM (pmatch[n].rm_eo);
364 SCM_SETCDR (ans_pos,
365 scm_cons (scm_make_shared_substring (str, frm, to), SCM_EOL));
366 ans_pos = SCM_CDR (ans_pos);
367 }
368 }
369 return SCM_CDR (answer);
370 }
371 }
372
373
374
375
376 struct rx_dfa_state
377 {
378 struct rx_classical_system frame;
379 struct rx_unfa * unfa;
380 };
381
382 long scm_tc16_dfa_t;
383 #define DFA(X) ((struct rx_dfa_state *)SCM_CDR(X))
384 #define DFAP(X) (SCM_CAR(X) == (SCM)scm_tc16_dfa_t)
385
386 #ifdef __STDC__
387 size_t
388 free_dfa_t (SCM obj)
389 #else
390 size_t
391 free_dfa_t (obj)
392 SCM obj;
393 #endif
394 {
395 struct rx_dfa_state *r;
396 r = DFA(obj);
397 rx_terminate_system (&r->frame);
398 rx_free_unfa (r->unfa);
399 scm_must_free ((char *)r);
400 return sizeof (struct rx_dfa_state);
401 }
402
403 #ifdef __STDC__
404 int
405 print_dfa_t (SCM obj, SCM port, int writing)
406 #else
407 int
408 print_dfa_t (obj, port, writing)
409 SCM obj;
410 SCM port;
411 int writing;
412 #endif
413 {
414 struct rx_dfa_state *r;
415 r = DFA (obj);
416 scm_gen_puts (scm_regular_string, "#<dfa ", port);
417 scm_intprint (r->frame.rx->rx_id, 10, port);
418 scm_gen_puts (scm_regular_string, ">", port);
419 return 1;
420 }
421
422 static scm_smobfuns dfa_t_smob =
423 { scm_mark0, free_dfa_t, print_dfa_t, 0 };
424
425
426 SCM_PROC (s_regexp_to_dfa, "regexp->dfa", 1, 1, 0, scm_regexp_to_dfa);
427 #ifdef __STDC__
428 SCM
429 scm_regexp_to_dfa (SCM regexp, SCM cfl)
430 #else
431 SCM
432 scm_regexp_to_dfa (regexp, cfl)
433 SCM regexp;
434 SCM cfl;
435 #endif
436 {
437 int ret;
438 unsigned int syntax;
439 struct rx_dfa_state *r;
440 struct rexp_node * parsed;
441 int cflags;
442 char * pattern;
443 int len;
444 SCM answer;
445
446 SCM_ASSERT (SCM_NIMP (regexp) && SCM_RO_STRINGP (regexp),
447 regexp, SCM_ARG1, s_regexp_to_dfa);
448 if (cfl == SCM_UNDEFINED)
449 cfl = SCM_INUM0;
450 SCM_ASSERT (SCM_INUMP (cfl), cfl, SCM_ARG2, s_regexp_to_dfa);
451
452 pattern = SCM_RO_CHARS (regexp);
453 len = SCM_RO_LENGTH (regexp);
454 cflags = SCM_INUM (cfl);
455
456
457 SCM_NEWCELL (answer);
458
459 SCM_DEFER_INTS;
460 syntax = ((cflags & REG_EXTENDED)
461 ? RE_SYNTAX_POSIX_EXTENDED
462 : RE_SYNTAX_POSIX_BASIC);
463
464 if (cflags & REG_NEWLINE)
465 {
466 syntax &= ~RE_DOT_NEWLINE;
467 syntax |= RE_HAT_LISTS_NOT_NEWLINE;
468 }
469
470
471 ret = rx_parse (&parsed, pattern, len, syntax, 256, 0);
472
473 if (ret)
474 {
475 SCM_ALLOW_INTS;
476 return SCM_MAKINUM (ret);
477 }
478
479
480 r = (struct rx_dfa_state *)scm_must_malloc (sizeof (struct rx_dfa_state), s_regexp_to_dfa);
481 r->unfa = rx_unfa (rx_basic_unfaniverse (), parsed, 256);
482 rx_free_rexp (parsed);
483 if (!r->unfa)
484 {
485 scm_mallocated -= sizeof (*r);
486 scm_must_free ((char *)r);
487 SCM_ALLOW_INTS;
488 SCM_ASSERT (0, regexp, "internal error constructing rx_unfa", s_regexp_to_dfa);
489 }
490
491 rx_init_system (&r->frame, r->unfa->nfa);
492 SCM_SETCAR (answer, scm_tc16_dfa_t);
493 SCM_SETCDR (answer, (SCM)r);
494 SCM_ALLOW_INTS;
495 return answer;
496 }
497
498
499 SCM_PROC (s_dfa_fork, "dfa-fork", 1, 0, 0, scm_dfa_fork);
500 #ifdef __STDC__
501 SCM
502 scm_dfa_fork (SCM dfa)
503 #else
504 SCM
505 scm_dfa_fork (dfa)
506 SCM dfa;
507 #endif
508 {
509 struct rx_dfa_state *r;
510 SCM answer;
511
512 SCM_ASSERT (SCM_NIMP (dfa) && DFAP (dfa),
513 dfa, SCM_ARG1, s_dfa_fork);
514
515 SCM_NEWCELL (answer);
516
517 SCM_DEFER_INTS;
518 r = (struct rx_dfa_state *)scm_must_malloc (sizeof (struct rx_dfa_state), s_dfa_fork);
519 rx_save_unfa (DFA (dfa)->unfa);
520 r->unfa = DFA (dfa)->unfa;
521 rx_init_system (&r->frame, r->unfa->nfa);
522 r->frame.state = DFA(dfa)->frame.state;
523 if (r->frame.state)
524 rx_lock_superstate (r->frame.rx, r->frame.state);
525 SCM_SETCAR (answer, scm_tc16_dfa_t);
526 SCM_SETCDR (answer, (SCM)r);
527 SCM_ALLOW_INTS;
528 return answer;
529 }
530
531
532 SCM_PROC (s_reset_dfa_x, "reset-dfa!", 1, 0, 0, scm_reset_dfa_x);
533 #ifdef __STDC__
534 SCM
535 scm_reset_dfa_x (SCM dfa)
536 #else
537 SCM
538 scm_reset_dfa_x (dfa)
539 SCM dfa;
540 #endif
541 {
542 SCM_ASSERT (SCM_NIMP (dfa) && DFAP (dfa),
543 dfa, SCM_ARG1, s_reset_dfa_x);
544
545 SCM_DEFER_INTS;
546 if (rx_yes != rx_start_superstate (&DFA(dfa)->frame))
547 {
548 SCM_ALLOW_INTS;
549 SCM_ASSERT (0, dfa, "internal error constructing rx starting superstate", s_reset_dfa_x);
550 }
551 SCM_ALLOW_INTS;
552 return dfa;
553 }
554
555
556
557 SCM_PROC (s_dfa_final_tag, "dfa-final-tag", 1, 0, 0, scm_dfa_final_tag);
558 #ifdef __STDC__
559 SCM
560 scm_dfa_final_tag (SCM dfa)
561 #else
562 SCM
563 scm_dfa_final_tag (dfa)
564 SCM dfa;
565 #endif
566 {
567 SCM_ASSERT (SCM_NIMP (dfa) && DFAP (dfa),
568 dfa, SCM_ARG1, s_dfa_final_tag);
569
570 if (DFA (dfa)->frame.state)
571 return scm_long2num ((long)DFA (dfa)->frame.state->contents->is_final);
572 else
573 return SCM_INUM0;
574 }
575
576
577
578 SCM_PROC (s_dfa_continuable_p, "dfa-continuable?", 1, 0, 0, scm_dfa_continuable_p);
579 #ifdef __STDC__
580 SCM
581 scm_dfa_continuable_p (SCM dfa)
582 #else
583 SCM
584 scm_dfa_continuable_p (dfa)
585 SCM dfa;
586 #endif
587 {
588 SCM_ASSERT (SCM_NIMP (dfa) && DFAP (dfa),
589 dfa, SCM_ARG1, s_dfa_continuable_p);
590
591 return ((DFA (dfa)->frame.state && DFA (dfa)->frame.state->contents->has_cset_edges)
592 ? SCM_BOOL_T
593 : SCM_BOOL_F);
594 }
595
596
597 SCM_PROC (s_advance_dfa_x, "advance-dfa!", 2, 0, 0, scm_advance_dfa_x);
598 #ifdef __STDC__
599 SCM
600 scm_advance_dfa_x (SCM dfa, SCM s)
601 #else
602 SCM
603 scm_advance_dfa_x (dfa, s)
604 SCM dfa;
605 SCM s;
606 #endif
607 {
608 struct rx_dfa_state * d;
609 char * str;
610 int len;
611 int matched;
612
613 SCM_ASSERT (SCM_NIMP (dfa) && DFAP (dfa),
614 dfa, SCM_ARG1, s_advance_dfa_x);
615 SCM_ASSERT (SCM_NIMP (s) && SCM_RO_STRINGP (s),
616 s, SCM_ARG2, s_advance_dfa_x);
617
618 str = SCM_RO_CHARS (s);
619 len = SCM_RO_LENGTH (s);
620 d = DFA (dfa);
621
622 SCM_DEFER_INTS;
623 matched = rx_advance_to_final (&d->frame, (unsigned char *)str, len);
624 SCM_ALLOW_INTS;
625
626 if (matched >= 0)
627 scm_return_first (SCM_MAKINUM (matched), dfa, s);
628 else
629 SCM_ASSERT (0, dfa, "internal error in rx_advance_to_final", s_advance_dfa_x);
630 }
631
632
633
634
635
636
637 #ifdef __STDC__
638 void
639 scm_init_rgx (void)
640 #else
641 void
642 scm_init_rgx ()
643 #endif
644 {
645 scm_tc16_regex_t = scm_newsmob (&regex_t_smob);
646 scm_tc16_dfa_t = scm_newsmob (&dfa_t_smob);
647 #include "rgx.x"
648 }
649