blob: d613d4ce744fb42bb41bf1c388dda44785d79daf [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * regexp.c: generic and extensible Regular Expression engine
3 *
4 * Basically designed with the purpose of compiling regexps for
5 * the variety of validation/shemas mechanisms now available in
6 * XML related specifications thise includes:
7 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
11 *
12 * See Copyright for the status of this software.
13 *
14 * Daniel Veillard <veillard@redhat.com>
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_REGEXP_ENABLED
21
22#include <stdio.h>
23#include <string.h>
24#include <libxml/tree.h>
25#include <libxml/parserInternals.h>
26#include <libxml/xmlregexp.h>
27#include <libxml/xmlautomata.h>
28#include <libxml/xmlunicode.h>
29
30/* #define DEBUG_REGEXP_GRAPH */
31/* #define DEBUG_REGEXP_EXEC */
32/* #define DEBUG_PUSH */
33
34#define ERROR(str) ctxt->error = 1; \
35 xmlGenericError(xmlGenericErrorContext, "Regexp: %s: %s\n", str, ctxt->cur)
36#define NEXT ctxt->cur++
37#define CUR (*(ctxt->cur))
38#define NXT(index) (ctxt->cur[index])
39
40#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
41#define NEXTL(l) ctxt->cur += l;
42
Daniel Veillarde19fc232002-04-22 16:01:24 +000043/**
44 * TODO:
45 *
46 * macro to flag unimplemented blocks
47 */
48#define TODO \
49 xmlGenericError(xmlGenericErrorContext, \
50 "Unimplemented block at %s:%d\n", \
51 __FILE__, __LINE__);
52
Daniel Veillard4255d502002-04-16 15:50:10 +000053
54/************************************************************************
55 * *
56 * Datatypes and structures *
57 * *
58 ************************************************************************/
59
60typedef enum {
61 XML_REGEXP_EPSILON = 1,
62 XML_REGEXP_CHARVAL,
63 XML_REGEXP_RANGES,
64 XML_REGEXP_SUBREG,
65 XML_REGEXP_STRING,
66 XML_REGEXP_ANYCHAR, /* . */
67 XML_REGEXP_ANYSPACE, /* \s */
68 XML_REGEXP_NOTSPACE, /* \S */
69 XML_REGEXP_INITNAME, /* \l */
70 XML_REGEXP_NOTINITNAME, /* \l */
71 XML_REGEXP_NAMECHAR, /* \c */
72 XML_REGEXP_NOTNAMECHAR, /* \C */
73 XML_REGEXP_DECIMAL, /* \d */
74 XML_REGEXP_NOTDECIMAL, /* \d */
75 XML_REGEXP_REALCHAR, /* \w */
76 XML_REGEXP_NOTREALCHAR, /* \w */
77 XML_REGEXP_LETTER,
78 XML_REGEXP_LETTER_UPPERCASE,
79 XML_REGEXP_LETTER_LOWERCASE,
80 XML_REGEXP_LETTER_TITLECASE,
81 XML_REGEXP_LETTER_MODIFIER,
82 XML_REGEXP_LETTER_OTHERS,
83 XML_REGEXP_MARK,
84 XML_REGEXP_MARK_NONSPACING,
85 XML_REGEXP_MARK_SPACECOMBINING,
86 XML_REGEXP_MARK_ENCLOSING,
87 XML_REGEXP_NUMBER,
88 XML_REGEXP_NUMBER_DECIMAL,
89 XML_REGEXP_NUMBER_LETTER,
90 XML_REGEXP_NUMBER_OTHERS,
91 XML_REGEXP_PUNCT,
92 XML_REGEXP_PUNCT_CONNECTOR,
93 XML_REGEXP_PUNCT_DASH,
94 XML_REGEXP_PUNCT_OPEN,
95 XML_REGEXP_PUNCT_CLOSE,
96 XML_REGEXP_PUNCT_INITQUOTE,
97 XML_REGEXP_PUNCT_FINQUOTE,
98 XML_REGEXP_PUNCT_OTHERS,
99 XML_REGEXP_SEPAR,
100 XML_REGEXP_SEPAR_SPACE,
101 XML_REGEXP_SEPAR_LINE,
102 XML_REGEXP_SEPAR_PARA,
103 XML_REGEXP_SYMBOL,
104 XML_REGEXP_SYMBOL_MATH,
105 XML_REGEXP_SYMBOL_CURRENCY,
106 XML_REGEXP_SYMBOL_MODIFIER,
107 XML_REGEXP_SYMBOL_OTHERS,
108 XML_REGEXP_OTHER,
109 XML_REGEXP_OTHER_CONTROL,
110 XML_REGEXP_OTHER_FORMAT,
111 XML_REGEXP_OTHER_PRIVATE,
112 XML_REGEXP_OTHER_NA,
113 XML_REGEXP_BLOCK_NAME
114} xmlRegAtomType;
115
116typedef enum {
117 XML_REGEXP_QUANT_EPSILON = 1,
118 XML_REGEXP_QUANT_ONCE,
119 XML_REGEXP_QUANT_OPT,
120 XML_REGEXP_QUANT_MULT,
121 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000122 XML_REGEXP_QUANT_ONCEONLY,
123 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000124 XML_REGEXP_QUANT_RANGE
125} xmlRegQuantType;
126
127typedef enum {
128 XML_REGEXP_START_STATE = 1,
129 XML_REGEXP_FINAL_STATE,
130 XML_REGEXP_TRANS_STATE
131} xmlRegStateType;
132
133typedef enum {
134 XML_REGEXP_MARK_NORMAL = 0,
135 XML_REGEXP_MARK_START,
136 XML_REGEXP_MARK_VISITED
137} xmlRegMarkedType;
138
139typedef struct _xmlRegRange xmlRegRange;
140typedef xmlRegRange *xmlRegRangePtr;
141
142struct _xmlRegRange {
143 int neg;
144 xmlRegAtomType type;
145 int start;
146 int end;
147 xmlChar *blockName;
148};
149
150typedef struct _xmlRegAtom xmlRegAtom;
151typedef xmlRegAtom *xmlRegAtomPtr;
152
153typedef struct _xmlAutomataState xmlRegState;
154typedef xmlRegState *xmlRegStatePtr;
155
156struct _xmlRegAtom {
157 int no;
158 xmlRegAtomType type;
159 xmlRegQuantType quant;
160 int min;
161 int max;
162
163 void *valuep;
164 int neg;
165 int codepoint;
166 xmlRegStatePtr start;
167 xmlRegStatePtr stop;
168 int maxRanges;
169 int nbRanges;
170 xmlRegRangePtr *ranges;
171 void *data;
172};
173
174typedef struct _xmlRegCounter xmlRegCounter;
175typedef xmlRegCounter *xmlRegCounterPtr;
176
177struct _xmlRegCounter {
178 int min;
179 int max;
180};
181
182typedef struct _xmlRegTrans xmlRegTrans;
183typedef xmlRegTrans *xmlRegTransPtr;
184
185struct _xmlRegTrans {
186 xmlRegAtomPtr atom;
187 int to;
188 int counter;
189 int count;
190};
191
192struct _xmlAutomataState {
193 xmlRegStateType type;
194 xmlRegMarkedType mark;
195 int no;
196
197 int maxTrans;
198 int nbTrans;
199 xmlRegTrans *trans;
200};
201
202typedef struct _xmlAutomata xmlRegParserCtxt;
203typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
204
205struct _xmlAutomata {
206 xmlChar *string;
207 xmlChar *cur;
208
209 int error;
210 int neg;
211
212 xmlRegStatePtr start;
213 xmlRegStatePtr end;
214 xmlRegStatePtr state;
215
216 xmlRegAtomPtr atom;
217
218 int maxAtoms;
219 int nbAtoms;
220 xmlRegAtomPtr *atoms;
221
222 int maxStates;
223 int nbStates;
224 xmlRegStatePtr *states;
225
226 int maxCounters;
227 int nbCounters;
228 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000229
230 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000231};
232
233struct _xmlRegexp {
234 xmlChar *string;
235 int nbStates;
236 xmlRegStatePtr *states;
237 int nbAtoms;
238 xmlRegAtomPtr *atoms;
239 int nbCounters;
240 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000241 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000242};
243
244typedef struct _xmlRegExecRollback xmlRegExecRollback;
245typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
246
247struct _xmlRegExecRollback {
248 xmlRegStatePtr state;/* the current state */
249 int index; /* the index in the input stack */
250 int nextbranch; /* the next transition to explore in that state */
251 int *counts; /* save the automate state if it has some */
252};
253
254typedef struct _xmlRegInputToken xmlRegInputToken;
255typedef xmlRegInputToken *xmlRegInputTokenPtr;
256
257struct _xmlRegInputToken {
258 xmlChar *value;
259 void *data;
260};
261
262struct _xmlRegExecCtxt {
263 int status; /* execution status != 0 indicate an error */
264 int determinist; /* did we found an inderterministic behaviour */
265 xmlRegexpPtr comp; /* the compiled regexp */
266 xmlRegExecCallbacks callback;
267 void *data;
268
269 xmlRegStatePtr state;/* the current state */
270 int transno; /* the current transition on that state */
271 int transcount; /* the number of char in char counted transitions */
272
273 /*
274 * A stack of rollback states
275 */
276 int maxRollbacks;
277 int nbRollbacks;
278 xmlRegExecRollback *rollbacks;
279
280 /*
281 * The state of the automata if any
282 */
283 int *counts;
284
285 /*
286 * The input stack
287 */
288 int inputStackMax;
289 int inputStackNr;
290 int index;
291 int *charStack;
292 const xmlChar *inputString; /* when operating on characters */
293 xmlRegInputTokenPtr inputStack;/* when operating on strings */
294
295};
296
Daniel Veillard441bc322002-04-20 17:38:48 +0000297#define REGEXP_ALL_COUNTER 0x123456
298#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000299
Daniel Veillard4255d502002-04-16 15:50:10 +0000300static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
301
302/************************************************************************
303 * *
304 * Allocation/Deallocation *
305 * *
306 ************************************************************************/
307
308/**
309 * xmlRegEpxFromParse:
310 * @ctxt: the parser context used to build it
311 *
312 * Allocate a new regexp and fill it with the reult from the parser
313 *
314 * Returns the new regexp or NULL in case of error
315 */
316static xmlRegexpPtr
317xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
318 xmlRegexpPtr ret;
319
320 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
321 if (ret == NULL)
322 return(NULL);
323 memset(ret, 0, sizeof(xmlRegexp));
324 ret->string = ctxt->string;
325 ctxt->string = NULL;
326 ret->nbStates = ctxt->nbStates;
327 ctxt->nbStates = 0;
328 ret->states = ctxt->states;
329 ctxt->states = NULL;
330 ret->nbAtoms = ctxt->nbAtoms;
331 ctxt->nbAtoms = 0;
332 ret->atoms = ctxt->atoms;
333 ctxt->atoms = NULL;
334 ret->nbCounters = ctxt->nbCounters;
335 ctxt->nbCounters = 0;
336 ret->counters = ctxt->counters;
337 ctxt->counters = NULL;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000338 ret->determinist = ctxt->determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000339 return(ret);
340}
341
342/**
343 * xmlRegNewParserCtxt:
344 * @string: the string to parse
345 *
346 * Allocate a new regexp parser context
347 *
348 * Returns the new context or NULL in case of error
349 */
350static xmlRegParserCtxtPtr
351xmlRegNewParserCtxt(const xmlChar *string) {
352 xmlRegParserCtxtPtr ret;
353
354 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
355 if (ret == NULL)
356 return(NULL);
357 memset(ret, 0, sizeof(xmlRegParserCtxt));
358 if (string != NULL)
359 ret->string = xmlStrdup(string);
360 ret->cur = ret->string;
361 ret->neg = 0;
362 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000363 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000364 return(ret);
365}
366
367/**
368 * xmlRegNewRange:
369 * @ctxt: the regexp parser context
370 * @neg: is that negative
371 * @type: the type of range
372 * @start: the start codepoint
373 * @end: the end codepoint
374 *
375 * Allocate a new regexp range
376 *
377 * Returns the new range or NULL in case of error
378 */
379static xmlRegRangePtr
380xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
381 int neg, xmlRegAtomType type, int start, int end) {
382 xmlRegRangePtr ret;
383
384 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
385 if (ret == NULL) {
386 ERROR("failed to allocate regexp range");
387 return(NULL);
388 }
389 ret->neg = neg;
390 ret->type = type;
391 ret->start = start;
392 ret->end = end;
393 return(ret);
394}
395
396/**
397 * xmlRegFreeRange:
398 * @range: the regexp range
399 *
400 * Free a regexp range
401 */
402static void
403xmlRegFreeRange(xmlRegRangePtr range) {
404 if (range == NULL)
405 return;
406
407 if (range->blockName != NULL)
408 xmlFree(range->blockName);
409 xmlFree(range);
410}
411
412/**
413 * xmlRegNewAtom:
414 * @ctxt: the regexp parser context
415 * @type: the type of atom
416 *
417 * Allocate a new regexp range
418 *
419 * Returns the new atom or NULL in case of error
420 */
421static xmlRegAtomPtr
422xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
423 xmlRegAtomPtr ret;
424
425 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
426 if (ret == NULL) {
427 ERROR("failed to allocate regexp atom");
428 return(NULL);
429 }
430 memset(ret, 0, sizeof(xmlRegAtom));
431 ret->type = type;
432 ret->quant = XML_REGEXP_QUANT_ONCE;
433 ret->min = 0;
434 ret->max = 0;
435 return(ret);
436}
437
438/**
439 * xmlRegFreeAtom:
440 * @atom: the regexp atom
441 *
442 * Free a regexp atom
443 */
444static void
445xmlRegFreeAtom(xmlRegAtomPtr atom) {
446 int i;
447
448 if (atom == NULL)
449 return;
450
451 for (i = 0;i < atom->nbRanges;i++)
452 xmlRegFreeRange(atom->ranges[i]);
453 if (atom->ranges != NULL)
454 xmlFree(atom->ranges);
455 if (atom->type == XML_REGEXP_STRING)
456 xmlFree(atom->valuep);
457 xmlFree(atom);
458}
459
460static xmlRegStatePtr
461xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
462 xmlRegStatePtr ret;
463
464 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
465 if (ret == NULL) {
466 ERROR("failed to allocate regexp state");
467 return(NULL);
468 }
469 memset(ret, 0, sizeof(xmlRegState));
470 ret->type = XML_REGEXP_TRANS_STATE;
471 ret->mark = XML_REGEXP_MARK_NORMAL;
472 return(ret);
473}
474
475/**
476 * xmlRegFreeState:
477 * @state: the regexp state
478 *
479 * Free a regexp state
480 */
481static void
482xmlRegFreeState(xmlRegStatePtr state) {
483 if (state == NULL)
484 return;
485
486 if (state->trans != NULL)
487 xmlFree(state->trans);
488 xmlFree(state);
489}
490
491/**
492 * xmlRegFreeParserCtxt:
493 * @ctxt: the regexp parser context
494 *
495 * Free a regexp parser context
496 */
497static void
498xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
499 int i;
500 if (ctxt == NULL)
501 return;
502
503 if (ctxt->string != NULL)
504 xmlFree(ctxt->string);
505 if (ctxt->states != NULL) {
506 for (i = 0;i < ctxt->nbStates;i++)
507 xmlRegFreeState(ctxt->states[i]);
508 xmlFree(ctxt->states);
509 }
510 if (ctxt->atoms != NULL) {
511 for (i = 0;i < ctxt->nbAtoms;i++)
512 xmlRegFreeAtom(ctxt->atoms[i]);
513 xmlFree(ctxt->atoms);
514 }
515 if (ctxt->counters != NULL)
516 xmlFree(ctxt->counters);
517 xmlFree(ctxt);
518}
519
520/************************************************************************
521 * *
522 * Display of Data structures *
523 * *
524 ************************************************************************/
525
526static void
527xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
528 switch (type) {
529 case XML_REGEXP_EPSILON:
530 fprintf(output, "epsilon "); break;
531 case XML_REGEXP_CHARVAL:
532 fprintf(output, "charval "); break;
533 case XML_REGEXP_RANGES:
534 fprintf(output, "ranges "); break;
535 case XML_REGEXP_SUBREG:
536 fprintf(output, "subexpr "); break;
537 case XML_REGEXP_STRING:
538 fprintf(output, "string "); break;
539 case XML_REGEXP_ANYCHAR:
540 fprintf(output, "anychar "); break;
541 case XML_REGEXP_ANYSPACE:
542 fprintf(output, "anyspace "); break;
543 case XML_REGEXP_NOTSPACE:
544 fprintf(output, "notspace "); break;
545 case XML_REGEXP_INITNAME:
546 fprintf(output, "initname "); break;
547 case XML_REGEXP_NOTINITNAME:
548 fprintf(output, "notinitname "); break;
549 case XML_REGEXP_NAMECHAR:
550 fprintf(output, "namechar "); break;
551 case XML_REGEXP_NOTNAMECHAR:
552 fprintf(output, "notnamechar "); break;
553 case XML_REGEXP_DECIMAL:
554 fprintf(output, "decimal "); break;
555 case XML_REGEXP_NOTDECIMAL:
556 fprintf(output, "notdecimal "); break;
557 case XML_REGEXP_REALCHAR:
558 fprintf(output, "realchar "); break;
559 case XML_REGEXP_NOTREALCHAR:
560 fprintf(output, "notrealchar "); break;
561 case XML_REGEXP_LETTER:
562 fprintf(output, "LETTER "); break;
563 case XML_REGEXP_LETTER_UPPERCASE:
564 fprintf(output, "LETTER_UPPERCASE "); break;
565 case XML_REGEXP_LETTER_LOWERCASE:
566 fprintf(output, "LETTER_LOWERCASE "); break;
567 case XML_REGEXP_LETTER_TITLECASE:
568 fprintf(output, "LETTER_TITLECASE "); break;
569 case XML_REGEXP_LETTER_MODIFIER:
570 fprintf(output, "LETTER_MODIFIER "); break;
571 case XML_REGEXP_LETTER_OTHERS:
572 fprintf(output, "LETTER_OTHERS "); break;
573 case XML_REGEXP_MARK:
574 fprintf(output, "MARK "); break;
575 case XML_REGEXP_MARK_NONSPACING:
576 fprintf(output, "MARK_NONSPACING "); break;
577 case XML_REGEXP_MARK_SPACECOMBINING:
578 fprintf(output, "MARK_SPACECOMBINING "); break;
579 case XML_REGEXP_MARK_ENCLOSING:
580 fprintf(output, "MARK_ENCLOSING "); break;
581 case XML_REGEXP_NUMBER:
582 fprintf(output, "NUMBER "); break;
583 case XML_REGEXP_NUMBER_DECIMAL:
584 fprintf(output, "NUMBER_DECIMAL "); break;
585 case XML_REGEXP_NUMBER_LETTER:
586 fprintf(output, "NUMBER_LETTER "); break;
587 case XML_REGEXP_NUMBER_OTHERS:
588 fprintf(output, "NUMBER_OTHERS "); break;
589 case XML_REGEXP_PUNCT:
590 fprintf(output, "PUNCT "); break;
591 case XML_REGEXP_PUNCT_CONNECTOR:
592 fprintf(output, "PUNCT_CONNECTOR "); break;
593 case XML_REGEXP_PUNCT_DASH:
594 fprintf(output, "PUNCT_DASH "); break;
595 case XML_REGEXP_PUNCT_OPEN:
596 fprintf(output, "PUNCT_OPEN "); break;
597 case XML_REGEXP_PUNCT_CLOSE:
598 fprintf(output, "PUNCT_CLOSE "); break;
599 case XML_REGEXP_PUNCT_INITQUOTE:
600 fprintf(output, "PUNCT_INITQUOTE "); break;
601 case XML_REGEXP_PUNCT_FINQUOTE:
602 fprintf(output, "PUNCT_FINQUOTE "); break;
603 case XML_REGEXP_PUNCT_OTHERS:
604 fprintf(output, "PUNCT_OTHERS "); break;
605 case XML_REGEXP_SEPAR:
606 fprintf(output, "SEPAR "); break;
607 case XML_REGEXP_SEPAR_SPACE:
608 fprintf(output, "SEPAR_SPACE "); break;
609 case XML_REGEXP_SEPAR_LINE:
610 fprintf(output, "SEPAR_LINE "); break;
611 case XML_REGEXP_SEPAR_PARA:
612 fprintf(output, "SEPAR_PARA "); break;
613 case XML_REGEXP_SYMBOL:
614 fprintf(output, "SYMBOL "); break;
615 case XML_REGEXP_SYMBOL_MATH:
616 fprintf(output, "SYMBOL_MATH "); break;
617 case XML_REGEXP_SYMBOL_CURRENCY:
618 fprintf(output, "SYMBOL_CURRENCY "); break;
619 case XML_REGEXP_SYMBOL_MODIFIER:
620 fprintf(output, "SYMBOL_MODIFIER "); break;
621 case XML_REGEXP_SYMBOL_OTHERS:
622 fprintf(output, "SYMBOL_OTHERS "); break;
623 case XML_REGEXP_OTHER:
624 fprintf(output, "OTHER "); break;
625 case XML_REGEXP_OTHER_CONTROL:
626 fprintf(output, "OTHER_CONTROL "); break;
627 case XML_REGEXP_OTHER_FORMAT:
628 fprintf(output, "OTHER_FORMAT "); break;
629 case XML_REGEXP_OTHER_PRIVATE:
630 fprintf(output, "OTHER_PRIVATE "); break;
631 case XML_REGEXP_OTHER_NA:
632 fprintf(output, "OTHER_NA "); break;
633 case XML_REGEXP_BLOCK_NAME:
634 fprintf(output, "BLOCK "); break;
635 }
636}
637
638static void
639xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
640 switch (type) {
641 case XML_REGEXP_QUANT_EPSILON:
642 fprintf(output, "epsilon "); break;
643 case XML_REGEXP_QUANT_ONCE:
644 fprintf(output, "once "); break;
645 case XML_REGEXP_QUANT_OPT:
646 fprintf(output, "? "); break;
647 case XML_REGEXP_QUANT_MULT:
648 fprintf(output, "* "); break;
649 case XML_REGEXP_QUANT_PLUS:
650 fprintf(output, "+ "); break;
651 case XML_REGEXP_QUANT_RANGE:
652 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000653 case XML_REGEXP_QUANT_ONCEONLY:
654 fprintf(output, "onceonly "); break;
655 case XML_REGEXP_QUANT_ALL:
656 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000657 }
658}
659static void
660xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
661 fprintf(output, " range: ");
662 if (range->neg)
663 fprintf(output, "negative ");
664 xmlRegPrintAtomType(output, range->type);
665 fprintf(output, "%c - %c\n", range->start, range->end);
666}
667
668static void
669xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
670 fprintf(output, " atom: ");
671 if (atom == NULL) {
672 fprintf(output, "NULL\n");
673 return;
674 }
675 xmlRegPrintAtomType(output, atom->type);
676 xmlRegPrintQuantType(output, atom->quant);
677 if (atom->quant == XML_REGEXP_QUANT_RANGE)
678 fprintf(output, "%d-%d ", atom->min, atom->max);
679 if (atom->type == XML_REGEXP_STRING)
680 fprintf(output, "'%s' ", (char *) atom->valuep);
681 if (atom->type == XML_REGEXP_CHARVAL)
682 fprintf(output, "char %c\n", atom->codepoint);
683 else if (atom->type == XML_REGEXP_RANGES) {
684 int i;
685 fprintf(output, "%d entries\n", atom->nbRanges);
686 for (i = 0; i < atom->nbRanges;i++)
687 xmlRegPrintRange(output, atom->ranges[i]);
688 } else if (atom->type == XML_REGEXP_SUBREG) {
689 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
690 } else {
691 fprintf(output, "\n");
692 }
693}
694
695static void
696xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
697 fprintf(output, " trans: ");
698 if (trans == NULL) {
699 fprintf(output, "NULL\n");
700 return;
701 }
702 if (trans->to < 0) {
703 fprintf(output, "removed\n");
704 return;
705 }
706 if (trans->counter >= 0) {
707 fprintf(output, "counted %d, ", trans->counter);
708 }
Daniel Veillard8a001f62002-04-20 07:24:11 +0000709 if (trans->count == REGEXP_ALL_COUNTER) {
710 fprintf(output, "all transition, ");
711 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000712 fprintf(output, "count based %d, ", trans->count);
713 }
714 if (trans->atom == NULL) {
715 fprintf(output, "epsilon to %d\n", trans->to);
716 return;
717 }
718 if (trans->atom->type == XML_REGEXP_CHARVAL)
719 fprintf(output, "char %c ", trans->atom->codepoint);
720 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
721}
722
723static void
724xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
725 int i;
726
727 fprintf(output, " state: ");
728 if (state == NULL) {
729 fprintf(output, "NULL\n");
730 return;
731 }
732 if (state->type == XML_REGEXP_START_STATE)
733 fprintf(output, "START ");
734 if (state->type == XML_REGEXP_FINAL_STATE)
735 fprintf(output, "FINAL ");
736
737 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
738 for (i = 0;i < state->nbTrans; i++) {
739 xmlRegPrintTrans(output, &(state->trans[i]));
740 }
741}
742
Daniel Veillard4255d502002-04-16 15:50:10 +0000743static void
744xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
745 int i;
746
747 fprintf(output, " ctxt: ");
748 if (ctxt == NULL) {
749 fprintf(output, "NULL\n");
750 return;
751 }
752 fprintf(output, "'%s' ", ctxt->string);
753 if (ctxt->error)
754 fprintf(output, "error ");
755 if (ctxt->neg)
756 fprintf(output, "neg ");
757 fprintf(output, "\n");
758 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
759 for (i = 0;i < ctxt->nbAtoms; i++) {
760 fprintf(output, " %02d ", i);
761 xmlRegPrintAtom(output, ctxt->atoms[i]);
762 }
763 if (ctxt->atom != NULL) {
764 fprintf(output, "current atom:\n");
765 xmlRegPrintAtom(output, ctxt->atom);
766 }
767 fprintf(output, "%d states:", ctxt->nbStates);
768 if (ctxt->start != NULL)
769 fprintf(output, " start: %d", ctxt->start->no);
770 if (ctxt->end != NULL)
771 fprintf(output, " end: %d", ctxt->end->no);
772 fprintf(output, "\n");
773 for (i = 0;i < ctxt->nbStates; i++) {
774 xmlRegPrintState(output, ctxt->states[i]);
775 }
776 fprintf(output, "%d counters:\n", ctxt->nbCounters);
777 for (i = 0;i < ctxt->nbCounters; i++) {
778 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
779 ctxt->counters[i].max);
780 }
781}
Daniel Veillard4255d502002-04-16 15:50:10 +0000782
783/************************************************************************
784 * *
785 * Finite Automata structures manipulations *
786 * *
787 ************************************************************************/
788
789static void
790xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
791 int neg, xmlRegAtomType type, int start, int end,
792 xmlChar *blockName) {
793 xmlRegRangePtr range;
794
795 if (atom == NULL) {
796 ERROR("add range: atom is NULL");
797 return;
798 }
799 if (atom->type != XML_REGEXP_RANGES) {
800 ERROR("add range: atom is not ranges");
801 return;
802 }
803 if (atom->maxRanges == 0) {
804 atom->maxRanges = 4;
805 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
806 sizeof(xmlRegRangePtr));
807 if (atom->ranges == NULL) {
808 ERROR("add range: allocation failed");
809 atom->maxRanges = 0;
810 return;
811 }
812 } else if (atom->nbRanges >= atom->maxRanges) {
813 xmlRegRangePtr *tmp;
814 atom->maxRanges *= 2;
815 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
816 sizeof(xmlRegRangePtr));
817 if (tmp == NULL) {
818 ERROR("add range: allocation failed");
819 atom->maxRanges /= 2;
820 return;
821 }
822 atom->ranges = tmp;
823 }
824 range = xmlRegNewRange(ctxt, neg, type, start, end);
825 if (range == NULL)
826 return;
827 range->blockName = blockName;
828 atom->ranges[atom->nbRanges++] = range;
829
830}
831
832static int
833xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
834 if (ctxt->maxCounters == 0) {
835 ctxt->maxCounters = 4;
836 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
837 sizeof(xmlRegCounter));
838 if (ctxt->counters == NULL) {
839 ERROR("reg counter: allocation failed");
840 ctxt->maxCounters = 0;
841 return(-1);
842 }
843 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
844 xmlRegCounter *tmp;
845 ctxt->maxCounters *= 2;
846 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
847 sizeof(xmlRegCounter));
848 if (tmp == NULL) {
849 ERROR("reg counter: allocation failed");
850 ctxt->maxCounters /= 2;
851 return(-1);
852 }
853 ctxt->counters = tmp;
854 }
855 ctxt->counters[ctxt->nbCounters].min = -1;
856 ctxt->counters[ctxt->nbCounters].max = -1;
857 return(ctxt->nbCounters++);
858}
859
860static void
861xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
862 if (atom == NULL) {
863 ERROR("atom push: atom is NULL");
864 return;
865 }
866 if (ctxt->maxAtoms == 0) {
867 ctxt->maxAtoms = 4;
868 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
869 sizeof(xmlRegAtomPtr));
870 if (ctxt->atoms == NULL) {
871 ERROR("atom push: allocation failed");
872 ctxt->maxAtoms = 0;
873 return;
874 }
875 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
876 xmlRegAtomPtr *tmp;
877 ctxt->maxAtoms *= 2;
878 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
879 sizeof(xmlRegAtomPtr));
880 if (tmp == NULL) {
881 ERROR("atom push: allocation failed");
882 ctxt->maxAtoms /= 2;
883 return;
884 }
885 ctxt->atoms = tmp;
886 }
887 atom->no = ctxt->nbAtoms;
888 ctxt->atoms[ctxt->nbAtoms++] = atom;
889}
890
891static void
892xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
893 xmlRegAtomPtr atom, xmlRegStatePtr target,
894 int counter, int count) {
895 if (state == NULL) {
896 ERROR("add state: state is NULL");
897 return;
898 }
899 if (target == NULL) {
900 ERROR("add state: target is NULL");
901 return;
902 }
903 if (state->maxTrans == 0) {
904 state->maxTrans = 4;
905 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
906 sizeof(xmlRegTrans));
907 if (state->trans == NULL) {
908 ERROR("add range: allocation failed");
909 state->maxTrans = 0;
910 return;
911 }
912 } else if (state->nbTrans >= state->maxTrans) {
913 xmlRegTrans *tmp;
914 state->maxTrans *= 2;
915 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
916 sizeof(xmlRegTrans));
917 if (tmp == NULL) {
918 ERROR("add range: allocation failed");
919 state->maxTrans /= 2;
920 return;
921 }
922 state->trans = tmp;
923 }
924#ifdef DEBUG_REGEXP_GRAPH
925 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +0000926 if (count == REGEXP_ALL_COUNTER)
927 printf("all transition");
Daniel Veillard4402ab42002-09-12 16:02:56 +0000928 else if (count >= 0)
Daniel Veillard4255d502002-04-16 15:50:10 +0000929 printf("count based %d", count);
930 else if (counter >= 0)
931 printf("counted %d", counter);
932 else if (atom == NULL)
933 printf("epsilon transition");
934 printf("\n");
935#endif
936
937 state->trans[state->nbTrans].atom = atom;
938 state->trans[state->nbTrans].to = target->no;
939 state->trans[state->nbTrans].counter = counter;
940 state->trans[state->nbTrans].count = count;
941 state->nbTrans++;
942}
943
944static void
945xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
946 if (ctxt->maxStates == 0) {
947 ctxt->maxStates = 4;
948 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
949 sizeof(xmlRegStatePtr));
950 if (ctxt->states == NULL) {
951 ERROR("add range: allocation failed");
952 ctxt->maxStates = 0;
953 return;
954 }
955 } else if (ctxt->nbStates >= ctxt->maxStates) {
956 xmlRegStatePtr *tmp;
957 ctxt->maxStates *= 2;
958 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
959 sizeof(xmlRegStatePtr));
960 if (tmp == NULL) {
961 ERROR("add range: allocation failed");
962 ctxt->maxStates /= 2;
963 return;
964 }
965 ctxt->states = tmp;
966 }
967 state->no = ctxt->nbStates;
968 ctxt->states[ctxt->nbStates++] = state;
969}
970
971/**
Daniel Veillard7646b182002-04-20 06:41:40 +0000972 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +0000973 * @ctxt: a regexp parser context
974 * @from: the from state
975 * @to: the target state or NULL for building a new one
976 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +0000977 *
978 */
979static void
980xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +0000981 xmlRegStatePtr from, xmlRegStatePtr to,
982 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +0000983 if (to == NULL) {
984 to = xmlRegNewState(ctxt);
985 xmlRegStatePush(ctxt, to);
986 ctxt->state = to;
987 }
Daniel Veillard441bc322002-04-20 17:38:48 +0000988 if (lax)
989 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
990 else
991 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +0000992}
993
994/**
Daniel Veillard4255d502002-04-16 15:50:10 +0000995 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +0000996 * @ctxt: a regexp parser context
997 * @from: the from state
998 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +0000999 *
1000 */
1001static void
1002xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1003 xmlRegStatePtr from, xmlRegStatePtr to) {
1004 if (to == NULL) {
1005 to = xmlRegNewState(ctxt);
1006 xmlRegStatePush(ctxt, to);
1007 ctxt->state = to;
1008 }
1009 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1010}
1011
1012/**
1013 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001014 * @ctxt: a regexp parser context
1015 * @from: the from state
1016 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001017 * counter: the counter for that transition
1018 *
1019 */
1020static void
1021xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1022 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1023 if (to == NULL) {
1024 to = xmlRegNewState(ctxt);
1025 xmlRegStatePush(ctxt, to);
1026 ctxt->state = to;
1027 }
1028 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1029}
1030
1031/**
1032 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001033 * @ctxt: a regexp parser context
1034 * @from: the from state
1035 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001036 * counter: the counter for that transition
1037 *
1038 */
1039static void
1040xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1041 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1042 if (to == NULL) {
1043 to = xmlRegNewState(ctxt);
1044 xmlRegStatePush(ctxt, to);
1045 ctxt->state = to;
1046 }
1047 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1048}
1049
1050/**
1051 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001052 * @ctxt: a regexp parser context
1053 * @from: the from state
1054 * @to: the target state or NULL for building a new one
1055 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001056 *
1057 */
1058static void
1059xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1060 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1061 if (atom == NULL) {
1062 ERROR("genrate transition: atom == NULL");
1063 return;
1064 }
1065 if (atom->type == XML_REGEXP_SUBREG) {
1066 /*
1067 * this is a subexpression handling one should not need to
1068 * create a new node excep for XML_REGEXP_QUANT_RANGE.
1069 */
1070 xmlRegAtomPush(ctxt, atom);
1071 if ((to != NULL) && (atom->stop != to) &&
1072 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1073 /*
1074 * Generate an epsilon transition to link to the target
1075 */
1076 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1077 }
1078 switch (atom->quant) {
1079 case XML_REGEXP_QUANT_OPT:
1080 atom->quant = XML_REGEXP_QUANT_ONCE;
1081 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1082 break;
1083 case XML_REGEXP_QUANT_MULT:
1084 atom->quant = XML_REGEXP_QUANT_ONCE;
1085 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1086 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1087 break;
1088 case XML_REGEXP_QUANT_PLUS:
1089 atom->quant = XML_REGEXP_QUANT_ONCE;
1090 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1091 break;
1092 case XML_REGEXP_QUANT_RANGE: {
1093 int counter;
1094 xmlRegStatePtr newstate;
1095
1096 /*
1097 * This one is nasty:
1098 * 1/ register a new counter
1099 * 2/ register an epsilon transition associated to
1100 * this counter going from atom->stop to atom->start
1101 * 3/ create a new state
1102 * 4/ generate a counted transition from atom->stop to
1103 * that state
1104 */
1105 counter = xmlRegGetCounter(ctxt);
1106 ctxt->counters[counter].min = atom->min - 1;
1107 ctxt->counters[counter].max = atom->max - 1;
1108 atom->min = 0;
1109 atom->max = 0;
1110 atom->quant = XML_REGEXP_QUANT_ONCE;
1111 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1112 atom->start, counter);
1113 if (to != NULL) {
1114 newstate = to;
1115 } else {
1116 newstate = xmlRegNewState(ctxt);
1117 xmlRegStatePush(ctxt, newstate);
1118 ctxt->state = newstate;
1119 }
1120 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1121 newstate, counter);
1122 }
1123 default:
1124 break;
1125 }
1126 return;
1127 } else {
1128 if (to == NULL) {
1129 to = xmlRegNewState(ctxt);
1130 xmlRegStatePush(ctxt, to);
1131 }
1132 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1133 xmlRegAtomPush(ctxt, atom);
1134 ctxt->state = to;
1135 }
1136 switch (atom->quant) {
1137 case XML_REGEXP_QUANT_OPT:
1138 atom->quant = XML_REGEXP_QUANT_ONCE;
1139 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1140 break;
1141 case XML_REGEXP_QUANT_MULT:
1142 atom->quant = XML_REGEXP_QUANT_ONCE;
1143 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1144 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1145 break;
1146 case XML_REGEXP_QUANT_PLUS:
1147 atom->quant = XML_REGEXP_QUANT_ONCE;
1148 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1149 break;
1150 default:
1151 break;
1152 }
1153}
1154
1155/**
1156 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001157 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001158 * @fromnr: the from state
1159 * @tonr: the to state
1160 * @cpunter: should that transition be associted to a counted
1161 *
1162 */
1163static void
1164xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1165 int tonr, int counter) {
1166 int transnr;
1167 xmlRegStatePtr from;
1168 xmlRegStatePtr to;
1169
1170#ifdef DEBUG_REGEXP_GRAPH
1171 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1172#endif
1173 from = ctxt->states[fromnr];
1174 if (from == NULL)
1175 return;
1176 to = ctxt->states[tonr];
1177 if (to == NULL)
1178 return;
1179 if ((to->mark == XML_REGEXP_MARK_START) ||
1180 (to->mark == XML_REGEXP_MARK_VISITED))
1181 return;
1182
1183 to->mark = XML_REGEXP_MARK_VISITED;
1184 if (to->type == XML_REGEXP_FINAL_STATE) {
1185#ifdef DEBUG_REGEXP_GRAPH
1186 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1187#endif
1188 from->type = XML_REGEXP_FINAL_STATE;
1189 }
1190 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1191 if (to->trans[transnr].atom == NULL) {
1192 /*
1193 * Don't remove counted transitions
1194 * Don't loop either
1195 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001196 if (to->trans[transnr].to != fromnr) {
1197 if (to->trans[transnr].count >= 0) {
1198 int newto = to->trans[transnr].to;
1199
1200 xmlRegStateAddTrans(ctxt, from, NULL,
1201 ctxt->states[newto],
1202 -1, to->trans[transnr].count);
1203 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001204#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001205 printf("Found epsilon trans %d from %d to %d\n",
1206 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001207#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001208 if (to->trans[transnr].counter >= 0) {
1209 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1210 to->trans[transnr].to,
1211 to->trans[transnr].counter);
1212 } else {
1213 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1214 to->trans[transnr].to,
1215 counter);
1216 }
1217 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001218 }
1219 } else {
1220 int newto = to->trans[transnr].to;
1221
Daniel Veillardb509f152002-04-17 16:28:10 +00001222 if (to->trans[transnr].counter >= 0) {
1223 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1224 ctxt->states[newto],
1225 to->trans[transnr].counter, -1);
1226 } else {
1227 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1228 ctxt->states[newto], counter, -1);
1229 }
1230
Daniel Veillard4255d502002-04-16 15:50:10 +00001231 }
1232 }
1233 to->mark = XML_REGEXP_MARK_NORMAL;
1234}
1235
1236/**
1237 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001238 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001239 *
1240 */
1241static void
1242xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1243 int statenr, transnr;
1244 xmlRegStatePtr state;
1245
1246 /*
1247 * build the completed transitions bypassing the epsilons
1248 * Use a marking algorithm to avoid loops
1249 */
1250 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1251 state = ctxt->states[statenr];
1252 if (state == NULL)
1253 continue;
1254 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1255 if ((state->trans[transnr].atom == NULL) &&
1256 (state->trans[transnr].to >= 0)) {
1257 if (state->trans[transnr].to == statenr) {
1258 state->trans[transnr].to = -1;
1259#ifdef DEBUG_REGEXP_GRAPH
1260 printf("Removed loopback epsilon trans %d on %d\n",
1261 transnr, statenr);
1262#endif
1263 } else if (state->trans[transnr].count < 0) {
1264 int newto = state->trans[transnr].to;
1265
1266#ifdef DEBUG_REGEXP_GRAPH
1267 printf("Found epsilon trans %d from %d to %d\n",
1268 transnr, statenr, newto);
1269#endif
1270 state->mark = XML_REGEXP_MARK_START;
1271 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1272 newto, state->trans[transnr].counter);
1273 state->mark = XML_REGEXP_MARK_NORMAL;
1274#ifdef DEBUG_REGEXP_GRAPH
1275 } else {
1276 printf("Found counted transition %d on %d\n",
1277 transnr, statenr);
1278#endif
1279 }
1280 }
1281 }
1282 }
1283 /*
1284 * Eliminate the epsilon transitions
1285 */
1286 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1287 state = ctxt->states[statenr];
1288 if (state == NULL)
1289 continue;
1290 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1291 if ((state->trans[transnr].atom == NULL) &&
1292 (state->trans[transnr].count < 0) &&
1293 (state->trans[transnr].to >= 0)) {
1294 state->trans[transnr].to = -1;
1295 }
1296 }
1297 }
1298}
1299
Daniel Veillarde19fc232002-04-22 16:01:24 +00001300/**
1301 * xmlFACompareAtoms:
1302 * @atom1: an atom
1303 * @atom2: an atom
1304 *
1305 * Compares two atoms to check whether they are equivatents
1306 *
1307 * Returns 1 if yes and 0 otherwise
1308 */
1309static int
1310xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1311 if (atom1 == atom2)
1312 return(1);
1313 if ((atom1 == NULL) || (atom2 == NULL))
1314 return(0);
1315
1316 if (atom1->type != atom2->type)
1317 return(0);
1318 switch (atom1->type) {
1319 case XML_REGEXP_STRING:
1320 return(xmlStrEqual((xmlChar *)atom1->valuep,
1321 (xmlChar *)atom2->valuep));
1322 case XML_REGEXP_EPSILON:
1323 return(1);
1324 case XML_REGEXP_CHARVAL:
1325 return(atom1->codepoint == atom2->codepoint);
1326 case XML_REGEXP_RANGES:
1327 TODO;
1328 return(0);
1329 default:
1330 break;
1331 }
1332 return(1);
1333}
1334
1335/**
1336 * xmlFARecurseDeterminism:
1337 * @ctxt: a regexp parser context
1338 *
1339 * Check whether the associated regexp is determinist,
1340 * should be called after xmlFAEliminateEpsilonTransitions()
1341 *
1342 */
1343static int
1344xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1345 int to, xmlRegAtomPtr atom) {
1346 int ret = 1;
1347 int transnr;
1348 xmlRegTransPtr t1;
1349
1350 if (state == NULL)
1351 return(ret);
1352 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1353 t1 = &(state->trans[transnr]);
1354 /*
1355 * check transitions conflicting with the one looked at
1356 */
1357 if (t1->atom == NULL) {
1358 if (t1->to == -1)
1359 continue;
1360 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1361 to, atom);
1362 if (ret == 0)
1363 return(0);
1364 continue;
1365 }
1366 if (t1->to != to)
1367 continue;
1368 if (xmlFACompareAtoms(t1->atom, atom))
1369 return(0);
1370 }
1371 return(ret);
1372}
1373
1374/**
1375 * xmlFAComputesDeterminism:
1376 * @ctxt: a regexp parser context
1377 *
1378 * Check whether the associated regexp is determinist,
1379 * should be called after xmlFAEliminateEpsilonTransitions()
1380 *
1381 */
1382static int
1383xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1384 int statenr, transnr;
1385 xmlRegStatePtr state;
1386 xmlRegTransPtr t1, t2;
1387 int i;
1388 int ret = 1;
1389
Daniel Veillard4402ab42002-09-12 16:02:56 +00001390#ifdef DEBUG_REGEXP_GRAPH
1391 printf("xmlFAComputesDeterminism\n");
1392 xmlRegPrintCtxt(stdout, ctxt);
1393#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001394 if (ctxt->determinist != -1)
1395 return(ctxt->determinist);
1396
1397 /*
1398 * Check for all states that there isn't 2 transitions
1399 * with the same atom and a different target.
1400 */
1401 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1402 state = ctxt->states[statenr];
1403 if (state == NULL)
1404 continue;
1405 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1406 t1 = &(state->trans[transnr]);
1407 /*
1408 * Determinism checks in case of counted or all transitions
1409 * will have to be handled separately
1410 */
1411 if (t1->atom == NULL)
1412 continue;
1413 if (t1->to == -1) /* eliminated */
1414 continue;
1415 for (i = 0;i < transnr;i++) {
1416 t2 = &(state->trans[i]);
1417 if (t2->to == -1) /* eliminated */
1418 continue;
1419 if (t2->atom != NULL) {
1420 if (t1->to == t2->to) {
1421 if (xmlFACompareAtoms(t1->atom, t2->atom))
1422 t2->to = -1; /* eliminate */
1423 } else {
1424 /* not determinist ! */
1425 if (xmlFACompareAtoms(t1->atom, t2->atom))
1426 ret = 0;
1427 }
1428 } else if (t1->to != -1) {
1429 /*
1430 * do the closure in case of remaining specific
1431 * epsilon transitions like choices or all
1432 */
1433 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1434 t2->to, t2->atom);
1435 if (ret == 0)
1436 return(0);
1437 }
1438 }
1439 if (ret == 0)
1440 break;
1441 }
1442 if (ret == 0)
1443 break;
1444 }
1445 ctxt->determinist = ret;
1446 return(ret);
1447}
1448
Daniel Veillard4255d502002-04-16 15:50:10 +00001449/************************************************************************
1450 * *
1451 * Routines to check input against transition atoms *
1452 * *
1453 ************************************************************************/
1454
1455static int
1456xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1457 int start, int end, const xmlChar *blockName) {
1458 int ret = 0;
1459
1460 switch (type) {
1461 case XML_REGEXP_STRING:
1462 case XML_REGEXP_SUBREG:
1463 case XML_REGEXP_RANGES:
1464 case XML_REGEXP_EPSILON:
1465 return(-1);
1466 case XML_REGEXP_ANYCHAR:
1467 ret = ((codepoint != '\n') && (codepoint != '\r'));
1468 break;
1469 case XML_REGEXP_CHARVAL:
1470 ret = ((codepoint >= start) && (codepoint <= end));
1471 break;
1472 case XML_REGEXP_NOTSPACE:
1473 neg = !neg;
1474 case XML_REGEXP_ANYSPACE:
1475 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1476 (codepoint == '\t') || (codepoint == ' '));
1477 break;
1478 case XML_REGEXP_NOTINITNAME:
1479 neg = !neg;
1480 case XML_REGEXP_INITNAME:
1481 ret = (xmlIsLetter(codepoint) ||
1482 (codepoint == '_') || (codepoint == ':'));
1483 break;
1484 case XML_REGEXP_NOTNAMECHAR:
1485 neg = !neg;
1486 case XML_REGEXP_NAMECHAR:
1487 ret = (xmlIsLetter(codepoint) || xmlIsDigit(codepoint) ||
1488 (codepoint == '.') || (codepoint == '-') ||
1489 (codepoint == '_') || (codepoint == ':') ||
1490 xmlIsCombining(codepoint) || xmlIsExtender(codepoint));
1491 break;
1492 case XML_REGEXP_NOTDECIMAL:
1493 neg = !neg;
1494 case XML_REGEXP_DECIMAL:
1495 ret = xmlUCSIsCatNd(codepoint);
1496 break;
1497 case XML_REGEXP_REALCHAR:
1498 neg = !neg;
1499 case XML_REGEXP_NOTREALCHAR:
1500 ret = xmlUCSIsCatP(codepoint);
1501 if (ret == 0)
1502 ret = xmlUCSIsCatZ(codepoint);
1503 if (ret == 0)
1504 ret = xmlUCSIsCatC(codepoint);
1505 break;
1506 case XML_REGEXP_LETTER:
1507 ret = xmlUCSIsCatL(codepoint);
1508 break;
1509 case XML_REGEXP_LETTER_UPPERCASE:
1510 ret = xmlUCSIsCatLu(codepoint);
1511 break;
1512 case XML_REGEXP_LETTER_LOWERCASE:
1513 ret = xmlUCSIsCatLl(codepoint);
1514 break;
1515 case XML_REGEXP_LETTER_TITLECASE:
1516 ret = xmlUCSIsCatLt(codepoint);
1517 break;
1518 case XML_REGEXP_LETTER_MODIFIER:
1519 ret = xmlUCSIsCatLm(codepoint);
1520 break;
1521 case XML_REGEXP_LETTER_OTHERS:
1522 ret = xmlUCSIsCatLo(codepoint);
1523 break;
1524 case XML_REGEXP_MARK:
1525 ret = xmlUCSIsCatM(codepoint);
1526 break;
1527 case XML_REGEXP_MARK_NONSPACING:
1528 ret = xmlUCSIsCatMn(codepoint);
1529 break;
1530 case XML_REGEXP_MARK_SPACECOMBINING:
1531 ret = xmlUCSIsCatMc(codepoint);
1532 break;
1533 case XML_REGEXP_MARK_ENCLOSING:
1534 ret = xmlUCSIsCatMe(codepoint);
1535 break;
1536 case XML_REGEXP_NUMBER:
1537 ret = xmlUCSIsCatN(codepoint);
1538 break;
1539 case XML_REGEXP_NUMBER_DECIMAL:
1540 ret = xmlUCSIsCatNd(codepoint);
1541 break;
1542 case XML_REGEXP_NUMBER_LETTER:
1543 ret = xmlUCSIsCatNl(codepoint);
1544 break;
1545 case XML_REGEXP_NUMBER_OTHERS:
1546 ret = xmlUCSIsCatNo(codepoint);
1547 break;
1548 case XML_REGEXP_PUNCT:
1549 ret = xmlUCSIsCatP(codepoint);
1550 break;
1551 case XML_REGEXP_PUNCT_CONNECTOR:
1552 ret = xmlUCSIsCatPc(codepoint);
1553 break;
1554 case XML_REGEXP_PUNCT_DASH:
1555 ret = xmlUCSIsCatPd(codepoint);
1556 break;
1557 case XML_REGEXP_PUNCT_OPEN:
1558 ret = xmlUCSIsCatPs(codepoint);
1559 break;
1560 case XML_REGEXP_PUNCT_CLOSE:
1561 ret = xmlUCSIsCatPe(codepoint);
1562 break;
1563 case XML_REGEXP_PUNCT_INITQUOTE:
1564 ret = xmlUCSIsCatPi(codepoint);
1565 break;
1566 case XML_REGEXP_PUNCT_FINQUOTE:
1567 ret = xmlUCSIsCatPf(codepoint);
1568 break;
1569 case XML_REGEXP_PUNCT_OTHERS:
1570 ret = xmlUCSIsCatPo(codepoint);
1571 break;
1572 case XML_REGEXP_SEPAR:
1573 ret = xmlUCSIsCatZ(codepoint);
1574 break;
1575 case XML_REGEXP_SEPAR_SPACE:
1576 ret = xmlUCSIsCatZs(codepoint);
1577 break;
1578 case XML_REGEXP_SEPAR_LINE:
1579 ret = xmlUCSIsCatZl(codepoint);
1580 break;
1581 case XML_REGEXP_SEPAR_PARA:
1582 ret = xmlUCSIsCatZp(codepoint);
1583 break;
1584 case XML_REGEXP_SYMBOL:
1585 ret = xmlUCSIsCatS(codepoint);
1586 break;
1587 case XML_REGEXP_SYMBOL_MATH:
1588 ret = xmlUCSIsCatSm(codepoint);
1589 break;
1590 case XML_REGEXP_SYMBOL_CURRENCY:
1591 ret = xmlUCSIsCatSc(codepoint);
1592 break;
1593 case XML_REGEXP_SYMBOL_MODIFIER:
1594 ret = xmlUCSIsCatSk(codepoint);
1595 break;
1596 case XML_REGEXP_SYMBOL_OTHERS:
1597 ret = xmlUCSIsCatSo(codepoint);
1598 break;
1599 case XML_REGEXP_OTHER:
1600 ret = xmlUCSIsCatC(codepoint);
1601 break;
1602 case XML_REGEXP_OTHER_CONTROL:
1603 ret = xmlUCSIsCatCc(codepoint);
1604 break;
1605 case XML_REGEXP_OTHER_FORMAT:
1606 ret = xmlUCSIsCatCf(codepoint);
1607 break;
1608 case XML_REGEXP_OTHER_PRIVATE:
1609 ret = xmlUCSIsCatCo(codepoint);
1610 break;
1611 case XML_REGEXP_OTHER_NA:
1612 /* ret = xmlUCSIsCatCn(codepoint); */
1613 /* Seems it doesn't exist anymore in recent Unicode releases */
1614 ret = 0;
1615 break;
1616 case XML_REGEXP_BLOCK_NAME:
1617 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
1618 break;
1619 }
1620 if (neg)
1621 return(!ret);
1622 return(ret);
1623}
1624
1625static int
1626xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
1627 int i, ret = 0;
1628 xmlRegRangePtr range;
1629
1630 if ((atom == NULL) || (!xmlIsChar(codepoint)))
1631 return(-1);
1632
1633 switch (atom->type) {
1634 case XML_REGEXP_SUBREG:
1635 case XML_REGEXP_EPSILON:
1636 return(-1);
1637 case XML_REGEXP_CHARVAL:
1638 return(codepoint == atom->codepoint);
1639 case XML_REGEXP_RANGES: {
1640 int accept = 0;
1641 for (i = 0;i < atom->nbRanges;i++) {
1642 range = atom->ranges[i];
1643 if (range->neg) {
1644 ret = xmlRegCheckCharacterRange(range->type, codepoint,
1645 0, range->start, range->end,
1646 range->blockName);
1647 if (ret != 0)
1648 return(0); /* excluded char */
1649 } else {
1650 ret = xmlRegCheckCharacterRange(range->type, codepoint,
1651 0, range->start, range->end,
1652 range->blockName);
1653 if (ret != 0)
1654 accept = 1; /* might still be excluded */
1655 }
1656 }
1657 return(accept);
1658 }
1659 case XML_REGEXP_STRING:
1660 printf("TODO: XML_REGEXP_STRING\n");
1661 return(-1);
1662 case XML_REGEXP_ANYCHAR:
1663 case XML_REGEXP_ANYSPACE:
1664 case XML_REGEXP_NOTSPACE:
1665 case XML_REGEXP_INITNAME:
1666 case XML_REGEXP_NOTINITNAME:
1667 case XML_REGEXP_NAMECHAR:
1668 case XML_REGEXP_NOTNAMECHAR:
1669 case XML_REGEXP_DECIMAL:
1670 case XML_REGEXP_NOTDECIMAL:
1671 case XML_REGEXP_REALCHAR:
1672 case XML_REGEXP_NOTREALCHAR:
1673 case XML_REGEXP_LETTER:
1674 case XML_REGEXP_LETTER_UPPERCASE:
1675 case XML_REGEXP_LETTER_LOWERCASE:
1676 case XML_REGEXP_LETTER_TITLECASE:
1677 case XML_REGEXP_LETTER_MODIFIER:
1678 case XML_REGEXP_LETTER_OTHERS:
1679 case XML_REGEXP_MARK:
1680 case XML_REGEXP_MARK_NONSPACING:
1681 case XML_REGEXP_MARK_SPACECOMBINING:
1682 case XML_REGEXP_MARK_ENCLOSING:
1683 case XML_REGEXP_NUMBER:
1684 case XML_REGEXP_NUMBER_DECIMAL:
1685 case XML_REGEXP_NUMBER_LETTER:
1686 case XML_REGEXP_NUMBER_OTHERS:
1687 case XML_REGEXP_PUNCT:
1688 case XML_REGEXP_PUNCT_CONNECTOR:
1689 case XML_REGEXP_PUNCT_DASH:
1690 case XML_REGEXP_PUNCT_OPEN:
1691 case XML_REGEXP_PUNCT_CLOSE:
1692 case XML_REGEXP_PUNCT_INITQUOTE:
1693 case XML_REGEXP_PUNCT_FINQUOTE:
1694 case XML_REGEXP_PUNCT_OTHERS:
1695 case XML_REGEXP_SEPAR:
1696 case XML_REGEXP_SEPAR_SPACE:
1697 case XML_REGEXP_SEPAR_LINE:
1698 case XML_REGEXP_SEPAR_PARA:
1699 case XML_REGEXP_SYMBOL:
1700 case XML_REGEXP_SYMBOL_MATH:
1701 case XML_REGEXP_SYMBOL_CURRENCY:
1702 case XML_REGEXP_SYMBOL_MODIFIER:
1703 case XML_REGEXP_SYMBOL_OTHERS:
1704 case XML_REGEXP_OTHER:
1705 case XML_REGEXP_OTHER_CONTROL:
1706 case XML_REGEXP_OTHER_FORMAT:
1707 case XML_REGEXP_OTHER_PRIVATE:
1708 case XML_REGEXP_OTHER_NA:
1709 case XML_REGEXP_BLOCK_NAME:
1710 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
1711 (const xmlChar *)atom->valuep);
1712 if (atom->neg)
1713 ret = !ret;
1714 break;
1715 }
1716 return(ret);
1717}
1718
1719/************************************************************************
1720 * *
1721 * Saving an restoring state of an execution context *
1722 * *
1723 ************************************************************************/
1724
1725#ifdef DEBUG_REGEXP_EXEC
1726static void
1727xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
1728 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
1729 if (exec->inputStack != NULL) {
1730 int i;
1731 printf(": ");
1732 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
1733 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
1734 } else {
1735 printf(": %s", &(exec->inputString[exec->index]));
1736 }
1737 printf("\n");
1738}
1739#endif
1740
1741static void
1742xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
1743#ifdef DEBUG_REGEXP_EXEC
1744 printf("saving ");
1745 exec->transno++;
1746 xmlFARegDebugExec(exec);
1747 exec->transno--;
1748#endif
1749
1750 if (exec->maxRollbacks == 0) {
1751 exec->maxRollbacks = 4;
1752 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
1753 sizeof(xmlRegExecRollback));
1754 if (exec->rollbacks == NULL) {
1755 fprintf(stderr, "exec save: allocation failed");
1756 exec->maxRollbacks = 0;
1757 return;
1758 }
1759 memset(exec->rollbacks, 0,
1760 exec->maxRollbacks * sizeof(xmlRegExecRollback));
1761 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
1762 xmlRegExecRollback *tmp;
1763 int len = exec->maxRollbacks;
1764
1765 exec->maxRollbacks *= 2;
1766 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
1767 exec->maxRollbacks * sizeof(xmlRegExecRollback));
1768 if (tmp == NULL) {
1769 fprintf(stderr, "exec save: allocation failed");
1770 exec->maxRollbacks /= 2;
1771 return;
1772 }
1773 exec->rollbacks = tmp;
1774 tmp = &exec->rollbacks[len];
1775 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
1776 }
1777 exec->rollbacks[exec->nbRollbacks].state = exec->state;
1778 exec->rollbacks[exec->nbRollbacks].index = exec->index;
1779 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
1780 if (exec->comp->nbCounters > 0) {
1781 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
1782 exec->rollbacks[exec->nbRollbacks].counts = (int *)
1783 xmlMalloc(exec->comp->nbCounters * sizeof(int));
1784 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
1785 fprintf(stderr, "exec save: allocation failed");
1786 exec->status = -5;
1787 return;
1788 }
1789 }
1790 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
1791 exec->comp->nbCounters * sizeof(int));
1792 }
1793 exec->nbRollbacks++;
1794}
1795
1796static void
1797xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
1798 if (exec->nbRollbacks <= 0) {
1799 exec->status = -1;
1800#ifdef DEBUG_REGEXP_EXEC
1801 printf("rollback failed on empty stack\n");
1802#endif
1803 return;
1804 }
1805 exec->nbRollbacks--;
1806 exec->state = exec->rollbacks[exec->nbRollbacks].state;
1807 exec->index = exec->rollbacks[exec->nbRollbacks].index;
1808 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
1809 if (exec->comp->nbCounters > 0) {
1810 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
1811 fprintf(stderr, "exec save: allocation failed");
1812 exec->status = -6;
1813 return;
1814 }
1815 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
1816 exec->comp->nbCounters * sizeof(int));
1817 }
1818
1819#ifdef DEBUG_REGEXP_EXEC
1820 printf("restored ");
1821 xmlFARegDebugExec(exec);
1822#endif
1823}
1824
1825/************************************************************************
1826 * *
1827 * Verifyer, running an input against a compiled regexp *
1828 * *
1829 ************************************************************************/
1830
1831static int
1832xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
1833 xmlRegExecCtxt execval;
1834 xmlRegExecCtxtPtr exec = &execval;
1835 int ret, codepoint, len;
1836
1837 exec->inputString = content;
1838 exec->index = 0;
1839 exec->determinist = 1;
1840 exec->maxRollbacks = 0;
1841 exec->nbRollbacks = 0;
1842 exec->rollbacks = NULL;
1843 exec->status = 0;
1844 exec->comp = comp;
1845 exec->state = comp->states[0];
1846 exec->transno = 0;
1847 exec->transcount = 0;
1848 if (comp->nbCounters > 0) {
1849 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
1850 if (exec->counts == NULL)
1851 return(-1);
1852 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
1853 } else
1854 exec->counts = NULL;
1855 while ((exec->status == 0) &&
1856 ((exec->inputString[exec->index] != 0) ||
1857 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
1858 xmlRegTransPtr trans;
1859 xmlRegAtomPtr atom;
1860
1861 /*
1862 * End of input on non-terminal state, rollback, however we may
1863 * still have epsilon like transition for counted transitions
1864 * on counters, in that case don't break too early.
1865 */
1866 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
1867 goto rollback;
1868
1869 exec->transcount = 0;
1870 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
1871 trans = &exec->state->trans[exec->transno];
1872 if (trans->to < 0)
1873 continue;
1874 atom = trans->atom;
1875 ret = 0;
1876 if (trans->count >= 0) {
1877 int count;
1878 xmlRegCounterPtr counter;
1879
1880 /*
1881 * A counted transition.
1882 */
1883
1884 count = exec->counts[trans->count];
1885 counter = &exec->comp->counters[trans->count];
1886#ifdef DEBUG_REGEXP_EXEC
1887 printf("testing count %d: val %d, min %d, max %d\n",
1888 trans->count, count, counter->min, counter->max);
1889#endif
1890 ret = ((count >= counter->min) && (count <= counter->max));
1891 } else if (atom == NULL) {
1892 fprintf(stderr, "epsilon transition left at runtime\n");
1893 exec->status = -2;
1894 break;
1895 } else if (exec->inputString[exec->index] != 0) {
1896 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
1897 ret = xmlRegCheckCharacter(atom, codepoint);
1898 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
1899 xmlRegStatePtr to = comp->states[trans->to];
1900
1901 /*
1902 * this is a multiple input sequence
1903 */
1904 if (exec->state->nbTrans > exec->transno + 1) {
1905 xmlFARegExecSave(exec);
1906 }
1907 exec->transcount = 1;
1908 do {
1909 /*
1910 * Try to progress as much as possible on the input
1911 */
1912 if (exec->transcount == atom->max) {
1913 break;
1914 }
1915 exec->index += len;
1916 /*
1917 * End of input: stop here
1918 */
1919 if (exec->inputString[exec->index] == 0) {
1920 exec->index -= len;
1921 break;
1922 }
1923 if (exec->transcount >= atom->min) {
1924 int transno = exec->transno;
1925 xmlRegStatePtr state = exec->state;
1926
1927 /*
1928 * The transition is acceptable save it
1929 */
1930 exec->transno = -1; /* trick */
1931 exec->state = to;
1932 xmlFARegExecSave(exec);
1933 exec->transno = transno;
1934 exec->state = state;
1935 }
1936 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
1937 len);
1938 ret = xmlRegCheckCharacter(atom, codepoint);
1939 exec->transcount++;
1940 } while (ret == 1);
1941 if (exec->transcount < atom->min)
1942 ret = 0;
1943
1944 /*
1945 * If the last check failed but one transition was found
1946 * possible, rollback
1947 */
1948 if (ret < 0)
1949 ret = 0;
1950 if (ret == 0) {
1951 goto rollback;
1952 }
1953 }
1954 }
1955 if (ret == 1) {
1956 if (exec->state->nbTrans > exec->transno + 1) {
1957 xmlFARegExecSave(exec);
1958 }
1959 if (trans->counter >= 0) {
1960#ifdef DEBUG_REGEXP_EXEC
1961 printf("Increasing count %d\n", trans->counter);
1962#endif
1963 exec->counts[trans->counter]++;
1964 }
1965#ifdef DEBUG_REGEXP_EXEC
1966 printf("entering state %d\n", trans->to);
1967#endif
1968 exec->state = comp->states[trans->to];
1969 exec->transno = 0;
1970 if (trans->atom != NULL) {
1971 exec->index += len;
1972 }
1973 goto progress;
1974 } else if (ret < 0) {
1975 exec->status = -4;
1976 break;
1977 }
1978 }
1979 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
1980rollback:
1981 /*
1982 * Failed to find a way out
1983 */
1984 exec->determinist = 0;
1985 xmlFARegExecRollBack(exec);
1986 }
1987progress:
1988 continue;
1989 }
1990 if (exec->rollbacks != NULL) {
1991 if (exec->counts != NULL) {
1992 int i;
1993
1994 for (i = 0;i < exec->maxRollbacks;i++)
1995 if (exec->rollbacks[i].counts != NULL)
1996 xmlFree(exec->rollbacks[i].counts);
1997 }
1998 xmlFree(exec->rollbacks);
1999 }
2000 if (exec->counts != NULL)
2001 xmlFree(exec->counts);
2002 if (exec->status == 0)
2003 return(1);
2004 if (exec->status == -1)
2005 return(0);
2006 return(exec->status);
2007}
2008
2009/************************************************************************
2010 * *
2011 * Progressive interface to the verifyer one atom at a time *
2012 * *
2013 ************************************************************************/
2014
2015/**
2016 * xmlRegExecCtxtPtr:
2017 * @comp: a precompiled regular expression
2018 * @callback: a callback function used for handling progresses in the
2019 * automata matching phase
2020 * @data: the context data associated to the callback in this context
2021 *
2022 * Build a context used for progressive evaluation of a regexp.
2023 */
2024xmlRegExecCtxtPtr
2025xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2026 xmlRegExecCtxtPtr exec;
2027
2028 if (comp == NULL)
2029 return(NULL);
2030 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2031 if (exec == NULL) {
2032 return(NULL);
2033 }
2034 memset(exec, 0, sizeof(xmlRegExecCtxt));
2035 exec->inputString = NULL;
2036 exec->index = 0;
2037 exec->determinist = 1;
2038 exec->maxRollbacks = 0;
2039 exec->nbRollbacks = 0;
2040 exec->rollbacks = NULL;
2041 exec->status = 0;
2042 exec->comp = comp;
2043 exec->state = comp->states[0];
2044 exec->transno = 0;
2045 exec->transcount = 0;
2046 exec->callback = callback;
2047 exec->data = data;
2048 if (comp->nbCounters > 0) {
2049 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2050 if (exec->counts == NULL) {
2051 xmlFree(exec);
2052 return(NULL);
2053 }
2054 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2055 } else
2056 exec->counts = NULL;
2057 exec->inputStackMax = 0;
2058 exec->inputStackNr = 0;
2059 exec->inputStack = NULL;
2060 return(exec);
2061}
2062
2063/**
2064 * xmlRegFreeExecCtxt:
2065 * @exec: a regular expression evaulation context
2066 *
2067 * Free the structures associated to a regular expression evaulation context.
2068 */
2069void
2070xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2071 if (exec == NULL)
2072 return;
2073
2074 if (exec->rollbacks != NULL) {
2075 if (exec->counts != NULL) {
2076 int i;
2077
2078 for (i = 0;i < exec->maxRollbacks;i++)
2079 if (exec->rollbacks[i].counts != NULL)
2080 xmlFree(exec->rollbacks[i].counts);
2081 }
2082 xmlFree(exec->rollbacks);
2083 }
2084 if (exec->counts != NULL)
2085 xmlFree(exec->counts);
2086 if (exec->inputStack != NULL) {
2087 int i;
2088
2089 for (i = 0;i < exec->inputStackNr;i++)
2090 xmlFree(exec->inputStack[i].value);
2091 xmlFree(exec->inputStack);
2092 }
2093 xmlFree(exec);
2094}
2095
2096static void
2097xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2098 void *data) {
2099#ifdef DEBUG_PUSH
2100 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2101#endif
2102 if (exec->inputStackMax == 0) {
2103 exec->inputStackMax = 4;
2104 exec->inputStack = (xmlRegInputTokenPtr)
2105 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2106 if (exec->inputStack == NULL) {
2107 fprintf(stderr, "push input: allocation failed");
2108 exec->inputStackMax = 0;
2109 return;
2110 }
2111 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2112 xmlRegInputTokenPtr tmp;
2113
2114 exec->inputStackMax *= 2;
2115 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2116 exec->inputStackMax * sizeof(xmlRegInputToken));
2117 if (tmp == NULL) {
2118 fprintf(stderr, "push input: allocation failed");
2119 exec->inputStackMax /= 2;
2120 return;
2121 }
2122 exec->inputStack = tmp;
2123 }
2124 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2125 exec->inputStack[exec->inputStackNr].data = data;
2126 exec->inputStackNr++;
2127 exec->inputStack[exec->inputStackNr].value = NULL;
2128 exec->inputStack[exec->inputStackNr].data = NULL;
2129}
2130
2131
2132/**
2133 * xmlRegExecPushString:
2134 * @exec: a regexp execution context
2135 * @value: a string token input
2136 * @data: data associated to the token to reuse in callbacks
2137 *
2138 * Push one input token in the execution context
2139 *
2140 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2141 * a negative value in case of error.
2142 */
2143int
2144xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2145 void *data) {
2146 xmlRegTransPtr trans;
2147 xmlRegAtomPtr atom;
2148 int ret;
2149 int final = 0;
2150
2151 if (exec == NULL)
2152 return(-1);
2153 if (exec->status != 0)
2154 return(exec->status);
2155
2156 if (value == NULL) {
2157 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2158 return(1);
2159 final = 1;
2160 }
2161
2162#ifdef DEBUG_PUSH
2163 printf("value pushed: %s\n", value);
2164#endif
2165 /*
2166 * If we have an active rollback stack push the new value there
2167 * and get back to where we were left
2168 */
2169 if ((value != NULL) && (exec->inputStackNr > 0)) {
2170 xmlFARegExecSaveInputString(exec, value, data);
2171 value = exec->inputStack[exec->index].value;
2172 data = exec->inputStack[exec->index].data;
2173#ifdef DEBUG_PUSH
2174 printf("value loaded: %s\n", value);
2175#endif
2176 }
2177
2178 while ((exec->status == 0) &&
2179 ((value != NULL) ||
2180 ((final == 1) &&
2181 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2182
2183 /*
2184 * End of input on non-terminal state, rollback, however we may
2185 * still have epsilon like transition for counted transitions
2186 * on counters, in that case don't break too early.
2187 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002188 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002189 goto rollback;
2190
2191 exec->transcount = 0;
2192 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2193 trans = &exec->state->trans[exec->transno];
2194 if (trans->to < 0)
2195 continue;
2196 atom = trans->atom;
2197 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002198 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2199 int i;
2200 int count;
2201 xmlRegTransPtr t;
2202 xmlRegCounterPtr counter;
2203
2204 ret = 0;
2205
2206#ifdef DEBUG_PUSH
2207 printf("testing all lax %d\n", trans->count);
2208#endif
2209 /*
2210 * Check all counted transitions from the current state
2211 */
2212 if ((value == NULL) && (final)) {
2213 ret = 1;
2214 } else if (value != NULL) {
2215 for (i = 0;i < exec->state->nbTrans;i++) {
2216 t = &exec->state->trans[i];
2217 if ((t->counter < 0) || (t == trans))
2218 continue;
2219 counter = &exec->comp->counters[t->counter];
2220 count = exec->counts[t->counter];
2221 if ((count < counter->max) &&
2222 (t->atom != NULL) &&
2223 (xmlStrEqual(value, t->atom->valuep))) {
2224 ret = 0;
2225 break;
2226 }
2227 if ((count >= counter->min) &&
2228 (count < counter->max) &&
2229 (xmlStrEqual(value, t->atom->valuep))) {
2230 ret = 1;
2231 break;
2232 }
2233 }
2234 }
2235 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002236 int i;
2237 int count;
2238 xmlRegTransPtr t;
2239 xmlRegCounterPtr counter;
2240
2241 ret = 1;
2242
2243#ifdef DEBUG_PUSH
2244 printf("testing all %d\n", trans->count);
2245#endif
2246 /*
2247 * Check all counted transitions from the current state
2248 */
2249 for (i = 0;i < exec->state->nbTrans;i++) {
2250 t = &exec->state->trans[i];
2251 if ((t->counter < 0) || (t == trans))
2252 continue;
2253 counter = &exec->comp->counters[t->counter];
2254 count = exec->counts[t->counter];
2255 if ((count < counter->min) || (count > counter->max)) {
2256 ret = 0;
2257 break;
2258 }
2259 }
2260 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002261 int count;
2262 xmlRegCounterPtr counter;
2263
2264 /*
2265 * A counted transition.
2266 */
2267
2268 count = exec->counts[trans->count];
2269 counter = &exec->comp->counters[trans->count];
2270#ifdef DEBUG_PUSH
2271 printf("testing count %d: val %d, min %d, max %d\n",
2272 trans->count, count, counter->min, counter->max);
2273#endif
2274 ret = ((count >= counter->min) && (count <= counter->max));
2275 } else if (atom == NULL) {
2276 fprintf(stderr, "epsilon transition left at runtime\n");
2277 exec->status = -2;
2278 break;
2279 } else if (value != NULL) {
2280 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002281 if ((ret == 1) && (trans->counter >= 0)) {
2282 xmlRegCounterPtr counter;
2283 int count;
2284
2285 count = exec->counts[trans->counter];
2286 counter = &exec->comp->counters[trans->counter];
2287 if (count >= counter->max)
2288 ret = 0;
2289 }
2290
Daniel Veillard4255d502002-04-16 15:50:10 +00002291 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2292 xmlRegStatePtr to = exec->comp->states[trans->to];
2293
2294 /*
2295 * this is a multiple input sequence
2296 */
2297 if (exec->state->nbTrans > exec->transno + 1) {
2298 if (exec->inputStackNr <= 0) {
2299 xmlFARegExecSaveInputString(exec, value, data);
2300 }
2301 xmlFARegExecSave(exec);
2302 }
2303 exec->transcount = 1;
2304 do {
2305 /*
2306 * Try to progress as much as possible on the input
2307 */
2308 if (exec->transcount == atom->max) {
2309 break;
2310 }
2311 exec->index++;
2312 value = exec->inputStack[exec->index].value;
2313 data = exec->inputStack[exec->index].data;
2314#ifdef DEBUG_PUSH
2315 printf("value loaded: %s\n", value);
2316#endif
2317
2318 /*
2319 * End of input: stop here
2320 */
2321 if (value == NULL) {
2322 exec->index --;
2323 break;
2324 }
2325 if (exec->transcount >= atom->min) {
2326 int transno = exec->transno;
2327 xmlRegStatePtr state = exec->state;
2328
2329 /*
2330 * The transition is acceptable save it
2331 */
2332 exec->transno = -1; /* trick */
2333 exec->state = to;
2334 if (exec->inputStackNr <= 0) {
2335 xmlFARegExecSaveInputString(exec, value, data);
2336 }
2337 xmlFARegExecSave(exec);
2338 exec->transno = transno;
2339 exec->state = state;
2340 }
2341 ret = xmlStrEqual(value, atom->valuep);
2342 exec->transcount++;
2343 } while (ret == 1);
2344 if (exec->transcount < atom->min)
2345 ret = 0;
2346
2347 /*
2348 * If the last check failed but one transition was found
2349 * possible, rollback
2350 */
2351 if (ret < 0)
2352 ret = 0;
2353 if (ret == 0) {
2354 goto rollback;
2355 }
2356 }
2357 }
2358 if (ret == 1) {
2359 if ((exec->callback != NULL) && (atom != NULL)) {
2360 exec->callback(exec->data, atom->valuep,
2361 atom->data, data);
2362 }
2363 if (exec->state->nbTrans > exec->transno + 1) {
2364 if (exec->inputStackNr <= 0) {
2365 xmlFARegExecSaveInputString(exec, value, data);
2366 }
2367 xmlFARegExecSave(exec);
2368 }
2369 if (trans->counter >= 0) {
2370#ifdef DEBUG_PUSH
2371 printf("Increasing count %d\n", trans->counter);
2372#endif
2373 exec->counts[trans->counter]++;
2374 }
2375#ifdef DEBUG_PUSH
2376 printf("entering state %d\n", trans->to);
2377#endif
2378 exec->state = exec->comp->states[trans->to];
2379 exec->transno = 0;
2380 if (trans->atom != NULL) {
2381 if (exec->inputStack != NULL) {
2382 exec->index++;
2383 if (exec->index < exec->inputStackNr) {
2384 value = exec->inputStack[exec->index].value;
2385 data = exec->inputStack[exec->index].data;
2386#ifdef DEBUG_PUSH
2387 printf("value loaded: %s\n", value);
2388#endif
2389 } else {
2390 value = NULL;
2391 data = NULL;
2392#ifdef DEBUG_PUSH
2393 printf("end of input\n");
2394#endif
2395 }
2396 } else {
2397 value = NULL;
2398 data = NULL;
2399#ifdef DEBUG_PUSH
2400 printf("end of input\n");
2401#endif
2402 }
2403 }
2404 goto progress;
2405 } else if (ret < 0) {
2406 exec->status = -4;
2407 break;
2408 }
2409 }
2410 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2411rollback:
2412 /*
2413 * Failed to find a way out
2414 */
2415 exec->determinist = 0;
2416 xmlFARegExecRollBack(exec);
2417 if (exec->status == 0) {
2418 value = exec->inputStack[exec->index].value;
2419 data = exec->inputStack[exec->index].data;
2420#ifdef DEBUG_PUSH
2421 printf("value loaded: %s\n", value);
2422#endif
2423 }
2424 }
2425progress:
2426 continue;
2427 }
2428 if (exec->status == 0) {
2429 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2430 }
2431 return(exec->status);
2432}
2433
2434#if 0
2435static int
2436xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2437 xmlRegTransPtr trans;
2438 xmlRegAtomPtr atom;
2439 int ret;
2440 int codepoint, len;
2441
2442 if (exec == NULL)
2443 return(-1);
2444 if (exec->status != 0)
2445 return(exec->status);
2446
2447 while ((exec->status == 0) &&
2448 ((exec->inputString[exec->index] != 0) ||
2449 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2450
2451 /*
2452 * End of input on non-terminal state, rollback, however we may
2453 * still have epsilon like transition for counted transitions
2454 * on counters, in that case don't break too early.
2455 */
2456 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2457 goto rollback;
2458
2459 exec->transcount = 0;
2460 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2461 trans = &exec->state->trans[exec->transno];
2462 if (trans->to < 0)
2463 continue;
2464 atom = trans->atom;
2465 ret = 0;
2466 if (trans->count >= 0) {
2467 int count;
2468 xmlRegCounterPtr counter;
2469
2470 /*
2471 * A counted transition.
2472 */
2473
2474 count = exec->counts[trans->count];
2475 counter = &exec->comp->counters[trans->count];
2476#ifdef DEBUG_REGEXP_EXEC
2477 printf("testing count %d: val %d, min %d, max %d\n",
2478 trans->count, count, counter->min, counter->max);
2479#endif
2480 ret = ((count >= counter->min) && (count <= counter->max));
2481 } else if (atom == NULL) {
2482 fprintf(stderr, "epsilon transition left at runtime\n");
2483 exec->status = -2;
2484 break;
2485 } else if (exec->inputString[exec->index] != 0) {
2486 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2487 ret = xmlRegCheckCharacter(atom, codepoint);
2488 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2489 xmlRegStatePtr to = exec->comp->states[trans->to];
2490
2491 /*
2492 * this is a multiple input sequence
2493 */
2494 if (exec->state->nbTrans > exec->transno + 1) {
2495 xmlFARegExecSave(exec);
2496 }
2497 exec->transcount = 1;
2498 do {
2499 /*
2500 * Try to progress as much as possible on the input
2501 */
2502 if (exec->transcount == atom->max) {
2503 break;
2504 }
2505 exec->index += len;
2506 /*
2507 * End of input: stop here
2508 */
2509 if (exec->inputString[exec->index] == 0) {
2510 exec->index -= len;
2511 break;
2512 }
2513 if (exec->transcount >= atom->min) {
2514 int transno = exec->transno;
2515 xmlRegStatePtr state = exec->state;
2516
2517 /*
2518 * The transition is acceptable save it
2519 */
2520 exec->transno = -1; /* trick */
2521 exec->state = to;
2522 xmlFARegExecSave(exec);
2523 exec->transno = transno;
2524 exec->state = state;
2525 }
2526 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2527 len);
2528 ret = xmlRegCheckCharacter(atom, codepoint);
2529 exec->transcount++;
2530 } while (ret == 1);
2531 if (exec->transcount < atom->min)
2532 ret = 0;
2533
2534 /*
2535 * If the last check failed but one transition was found
2536 * possible, rollback
2537 */
2538 if (ret < 0)
2539 ret = 0;
2540 if (ret == 0) {
2541 goto rollback;
2542 }
2543 }
2544 }
2545 if (ret == 1) {
2546 if (exec->state->nbTrans > exec->transno + 1) {
2547 xmlFARegExecSave(exec);
2548 }
2549 if (trans->counter >= 0) {
2550#ifdef DEBUG_REGEXP_EXEC
2551 printf("Increasing count %d\n", trans->counter);
2552#endif
2553 exec->counts[trans->counter]++;
2554 }
2555#ifdef DEBUG_REGEXP_EXEC
2556 printf("entering state %d\n", trans->to);
2557#endif
2558 exec->state = exec->comp->states[trans->to];
2559 exec->transno = 0;
2560 if (trans->atom != NULL) {
2561 exec->index += len;
2562 }
2563 goto progress;
2564 } else if (ret < 0) {
2565 exec->status = -4;
2566 break;
2567 }
2568 }
2569 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2570rollback:
2571 /*
2572 * Failed to find a way out
2573 */
2574 exec->determinist = 0;
2575 xmlFARegExecRollBack(exec);
2576 }
2577progress:
2578 continue;
2579 }
2580}
2581#endif
2582/************************************************************************
2583 * *
2584 * Parser for the Shemas Datatype Regular Expressions *
2585 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
2586 * *
2587 ************************************************************************/
2588
2589/**
2590 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00002591 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002592 *
2593 * [10] Char ::= [^.\?*+()|#x5B#x5D]
2594 */
2595static int
2596xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
2597 int cur;
2598 int len;
2599
2600 cur = CUR_SCHAR(ctxt->cur, len);
2601 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
2602 (cur == '*') || (cur == '+') || (cur == '(') ||
2603 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
2604 (cur == 0x5D) || (cur == 0))
2605 return(-1);
2606 return(cur);
2607}
2608
2609/**
2610 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00002611 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002612 *
2613 * [27] charProp ::= IsCategory | IsBlock
2614 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
2615 * Separators | Symbols | Others
2616 * [29] Letters ::= 'L' [ultmo]?
2617 * [30] Marks ::= 'M' [nce]?
2618 * [31] Numbers ::= 'N' [dlo]?
2619 * [32] Punctuation ::= 'P' [cdseifo]?
2620 * [33] Separators ::= 'Z' [slp]?
2621 * [34] Symbols ::= 'S' [mcko]?
2622 * [35] Others ::= 'C' [cfon]?
2623 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
2624 */
2625static void
2626xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
2627 int cur;
2628 xmlRegAtomType type = 0;
2629 xmlChar *blockName = NULL;
2630
2631 cur = CUR;
2632 if (cur == 'L') {
2633 NEXT;
2634 cur = CUR;
2635 if (cur == 'u') {
2636 NEXT;
2637 type = XML_REGEXP_LETTER_UPPERCASE;
2638 } else if (cur == 'l') {
2639 NEXT;
2640 type = XML_REGEXP_LETTER_LOWERCASE;
2641 } else if (cur == 't') {
2642 NEXT;
2643 type = XML_REGEXP_LETTER_TITLECASE;
2644 } else if (cur == 'm') {
2645 NEXT;
2646 type = XML_REGEXP_LETTER_MODIFIER;
2647 } else if (cur == 'o') {
2648 NEXT;
2649 type = XML_REGEXP_LETTER_OTHERS;
2650 } else {
2651 type = XML_REGEXP_LETTER;
2652 }
2653 } else if (cur == 'M') {
2654 NEXT;
2655 cur = CUR;
2656 if (cur == 'n') {
2657 NEXT;
2658 /* nonspacing */
2659 type = XML_REGEXP_MARK_NONSPACING;
2660 } else if (cur == 'c') {
2661 NEXT;
2662 /* spacing combining */
2663 type = XML_REGEXP_MARK_SPACECOMBINING;
2664 } else if (cur == 'e') {
2665 NEXT;
2666 /* enclosing */
2667 type = XML_REGEXP_MARK_ENCLOSING;
2668 } else {
2669 /* all marks */
2670 type = XML_REGEXP_MARK;
2671 }
2672 } else if (cur == 'N') {
2673 NEXT;
2674 cur = CUR;
2675 if (cur == 'd') {
2676 NEXT;
2677 /* digital */
2678 type = XML_REGEXP_NUMBER_DECIMAL;
2679 } else if (cur == 'l') {
2680 NEXT;
2681 /* letter */
2682 type = XML_REGEXP_NUMBER_LETTER;
2683 } else if (cur == 'o') {
2684 NEXT;
2685 /* other */
2686 type = XML_REGEXP_NUMBER_OTHERS;
2687 } else {
2688 /* all numbers */
2689 type = XML_REGEXP_NUMBER;
2690 }
2691 } else if (cur == 'P') {
2692 NEXT;
2693 cur = CUR;
2694 if (cur == 'c') {
2695 NEXT;
2696 /* connector */
2697 type = XML_REGEXP_PUNCT_CONNECTOR;
2698 } else if (cur == 'd') {
2699 NEXT;
2700 /* dash */
2701 type = XML_REGEXP_PUNCT_DASH;
2702 } else if (cur == 's') {
2703 NEXT;
2704 /* open */
2705 type = XML_REGEXP_PUNCT_OPEN;
2706 } else if (cur == 'e') {
2707 NEXT;
2708 /* close */
2709 type = XML_REGEXP_PUNCT_CLOSE;
2710 } else if (cur == 'i') {
2711 NEXT;
2712 /* initial quote */
2713 type = XML_REGEXP_PUNCT_INITQUOTE;
2714 } else if (cur == 'f') {
2715 NEXT;
2716 /* final quote */
2717 type = XML_REGEXP_PUNCT_FINQUOTE;
2718 } else if (cur == 'o') {
2719 NEXT;
2720 /* other */
2721 type = XML_REGEXP_PUNCT_OTHERS;
2722 } else {
2723 /* all punctuation */
2724 type = XML_REGEXP_PUNCT;
2725 }
2726 } else if (cur == 'Z') {
2727 NEXT;
2728 cur = CUR;
2729 if (cur == 's') {
2730 NEXT;
2731 /* space */
2732 type = XML_REGEXP_SEPAR_SPACE;
2733 } else if (cur == 'l') {
2734 NEXT;
2735 /* line */
2736 type = XML_REGEXP_SEPAR_LINE;
2737 } else if (cur == 'p') {
2738 NEXT;
2739 /* paragraph */
2740 type = XML_REGEXP_SEPAR_PARA;
2741 } else {
2742 /* all separators */
2743 type = XML_REGEXP_SEPAR;
2744 }
2745 } else if (cur == 'S') {
2746 NEXT;
2747 cur = CUR;
2748 if (cur == 'm') {
2749 NEXT;
2750 type = XML_REGEXP_SYMBOL_MATH;
2751 /* math */
2752 } else if (cur == 'c') {
2753 NEXT;
2754 type = XML_REGEXP_SYMBOL_CURRENCY;
2755 /* currency */
2756 } else if (cur == 'k') {
2757 NEXT;
2758 type = XML_REGEXP_SYMBOL_MODIFIER;
2759 /* modifiers */
2760 } else if (cur == 'o') {
2761 NEXT;
2762 type = XML_REGEXP_SYMBOL_OTHERS;
2763 /* other */
2764 } else {
2765 /* all symbols */
2766 type = XML_REGEXP_SYMBOL;
2767 }
2768 } else if (cur == 'C') {
2769 NEXT;
2770 cur = CUR;
2771 if (cur == 'c') {
2772 NEXT;
2773 /* control */
2774 type = XML_REGEXP_OTHER_CONTROL;
2775 } else if (cur == 'f') {
2776 NEXT;
2777 /* format */
2778 type = XML_REGEXP_OTHER_FORMAT;
2779 } else if (cur == 'o') {
2780 NEXT;
2781 /* private use */
2782 type = XML_REGEXP_OTHER_PRIVATE;
2783 } else if (cur == 'n') {
2784 NEXT;
2785 /* not assigned */
2786 type = XML_REGEXP_OTHER_NA;
2787 } else {
2788 /* all others */
2789 type = XML_REGEXP_OTHER;
2790 }
2791 } else if (cur == 'I') {
2792 const xmlChar *start;
2793 NEXT;
2794 cur = CUR;
2795 if (cur != 's') {
2796 ERROR("IsXXXX expected");
2797 return;
2798 }
2799 NEXT;
2800 start = ctxt->cur;
2801 cur = CUR;
2802 if (((cur >= 'a') && (cur <= 'z')) ||
2803 ((cur >= 'A') && (cur <= 'Z')) ||
2804 ((cur >= '0') && (cur <= '9')) ||
2805 (cur == 0x2D)) {
2806 NEXT;
2807 cur = CUR;
2808 while (((cur >= 'a') && (cur <= 'z')) ||
2809 ((cur >= 'A') && (cur <= 'Z')) ||
2810 ((cur >= '0') && (cur <= '9')) ||
2811 (cur == 0x2D)) {
2812 NEXT;
2813 cur = CUR;
2814 }
2815 }
2816 type = XML_REGEXP_BLOCK_NAME;
2817 blockName = xmlStrndup(start, ctxt->cur - start);
2818 } else {
2819 ERROR("Unknown char property");
2820 return;
2821 }
2822 if (ctxt->atom == NULL) {
2823 ctxt->atom = xmlRegNewAtom(ctxt, type);
2824 if (ctxt->atom != NULL)
2825 ctxt->atom->valuep = blockName;
2826 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
2827 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
2828 type, 0, 0, blockName);
2829 }
2830}
2831
2832/**
2833 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00002834 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002835 *
2836 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
2837 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
2838 * [25] catEsc ::= '\p{' charProp '}'
2839 * [26] complEsc ::= '\P{' charProp '}'
2840 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
2841 */
2842static void
2843xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
2844 int cur;
2845
2846 if (CUR == '.') {
2847 if (ctxt->atom == NULL) {
2848 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
2849 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
2850 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
2851 XML_REGEXP_ANYCHAR, 0, 0, NULL);
2852 }
2853 NEXT;
2854 return;
2855 }
2856 if (CUR != '\\') {
2857 ERROR("Escaped sequence: expecting \\");
2858 return;
2859 }
2860 NEXT;
2861 cur = CUR;
2862 if (cur == 'p') {
2863 NEXT;
2864 if (CUR != '{') {
2865 ERROR("Expecting '{'");
2866 return;
2867 }
2868 NEXT;
2869 xmlFAParseCharProp(ctxt);
2870 if (CUR != '}') {
2871 ERROR("Expecting '}'");
2872 return;
2873 }
2874 NEXT;
2875 } else if (cur == 'P') {
2876 NEXT;
2877 if (CUR != '{') {
2878 ERROR("Expecting '{'");
2879 return;
2880 }
2881 NEXT;
2882 xmlFAParseCharProp(ctxt);
2883 ctxt->atom->neg = 1;
2884 if (CUR != '}') {
2885 ERROR("Expecting '}'");
2886 return;
2887 }
2888 NEXT;
2889 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
2890 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
2891 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
2892 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
2893 (cur == 0x5E)) {
2894 if (ctxt->atom == NULL) {
2895 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
2896 if (ctxt->atom != NULL)
2897 ctxt->atom->codepoint = cur;
2898 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
2899 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
2900 XML_REGEXP_CHARVAL, cur, cur, NULL);
2901 }
2902 NEXT;
2903 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
2904 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
2905 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00002906 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00002907
2908 switch (cur) {
2909 case 's':
2910 type = XML_REGEXP_ANYSPACE;
2911 break;
2912 case 'S':
2913 type = XML_REGEXP_NOTSPACE;
2914 break;
2915 case 'i':
2916 type = XML_REGEXP_INITNAME;
2917 break;
2918 case 'I':
2919 type = XML_REGEXP_NOTINITNAME;
2920 break;
2921 case 'c':
2922 type = XML_REGEXP_NAMECHAR;
2923 break;
2924 case 'C':
2925 type = XML_REGEXP_NOTNAMECHAR;
2926 break;
2927 case 'd':
2928 type = XML_REGEXP_DECIMAL;
2929 break;
2930 case 'D':
2931 type = XML_REGEXP_NOTDECIMAL;
2932 break;
2933 case 'w':
2934 type = XML_REGEXP_REALCHAR;
2935 break;
2936 case 'W':
2937 type = XML_REGEXP_NOTREALCHAR;
2938 break;
2939 }
2940 NEXT;
2941 if (ctxt->atom == NULL) {
2942 ctxt->atom = xmlRegNewAtom(ctxt, type);
2943 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
2944 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
2945 type, 0, 0, NULL);
2946 }
2947 }
2948}
2949
2950/**
2951 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00002952 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002953 *
2954 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
2955 */
2956static int
2957xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
2958 int ret = 0, cur;
2959
2960 if ((CUR != '&') || (NXT(1) != '#'))
2961 return(-1);
2962 NEXT;
2963 NEXT;
2964 cur = CUR;
2965 if (cur == 'x') {
2966 NEXT;
2967 cur = CUR;
2968 if (((cur >= '0') && (cur <= '9')) ||
2969 ((cur >= 'a') && (cur <= 'f')) ||
2970 ((cur >= 'A') && (cur <= 'F'))) {
2971 while (((cur >= '0') && (cur <= '9')) ||
2972 ((cur >= 'A') && (cur <= 'F'))) {
2973 if ((cur >= '0') && (cur <= '9'))
2974 ret = ret * 16 + cur - '0';
2975 else if ((cur >= 'a') && (cur <= 'f'))
2976 ret = ret * 16 + 10 + (cur - 'a');
2977 else
2978 ret = ret * 16 + 10 + (cur - 'A');
2979 NEXT;
2980 cur = CUR;
2981 }
2982 } else {
2983 ERROR("Char ref: expecting [0-9A-F]");
2984 return(-1);
2985 }
2986 } else {
2987 if ((cur >= '0') && (cur <= '9')) {
2988 while ((cur >= '0') && (cur <= '9')) {
2989 ret = ret * 10 + cur - '0';
2990 NEXT;
2991 cur = CUR;
2992 }
2993 } else {
2994 ERROR("Char ref: expecting [0-9]");
2995 return(-1);
2996 }
2997 }
2998 if (cur != ';') {
2999 ERROR("Char ref: expecting ';'");
3000 return(-1);
3001 } else {
3002 NEXT;
3003 }
3004 return(ret);
3005}
3006
3007/**
3008 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003009 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003010 *
3011 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3012 * [18] seRange ::= charOrEsc '-' charOrEsc
3013 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3014 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3015 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3016 */
3017static void
3018xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
3019 int cur;
3020 int start = -1;
3021 int end = -1;
3022
3023 if ((CUR == '&') && (NXT(1) == '#')) {
3024 end = start = xmlFAParseCharRef(ctxt);
3025 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3026 XML_REGEXP_CHARVAL, start, end, NULL);
3027 return;
3028 }
3029 cur = CUR;
3030 if (cur == '\\') {
3031 NEXT;
3032 cur = CUR;
3033 switch (cur) {
3034 case 'n': start = 0xA; break;
3035 case 'r': start = 0xD; break;
3036 case 't': start = 0x9; break;
3037 case '\\': case '|': case '.': case '-': case '^': case '?':
3038 case '*': case '+': case '{': case '}': case '(': case ')':
3039 case '[': case ']':
3040 start = cur; break;
3041 default:
3042 ERROR("Invalid escape value");
3043 return;
3044 }
3045 end = start;
3046 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3047 end = start = cur;
3048 } else {
3049 ERROR("Expecting a char range");
3050 return;
3051 }
3052 NEXT;
3053 if (start == '-') {
3054 return;
3055 }
3056 cur = CUR;
3057 if (cur != '-') {
3058 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3059 XML_REGEXP_CHARVAL, start, end, NULL);
3060 return;
3061 }
3062 NEXT;
3063 cur = CUR;
3064 if (cur == '\\') {
3065 NEXT;
3066 cur = CUR;
3067 switch (cur) {
3068 case 'n': end = 0xA; break;
3069 case 'r': end = 0xD; break;
3070 case 't': end = 0x9; break;
3071 case '\\': case '|': case '.': case '-': case '^': case '?':
3072 case '*': case '+': case '{': case '}': case '(': case ')':
3073 case '[': case ']':
3074 end = cur; break;
3075 default:
3076 ERROR("Invalid escape value");
3077 return;
3078 }
3079 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3080 end = cur;
3081 } else {
3082 ERROR("Expecting the end of a char range");
3083 return;
3084 }
3085 NEXT;
3086 /* TODO check that the values are acceptable character ranges for XML */
3087 if (end < start) {
3088 ERROR("End of range is before start of range");
3089 } else {
3090 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3091 XML_REGEXP_CHARVAL, start, end, NULL);
3092 }
3093 return;
3094}
3095
3096/**
3097 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003098 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003099 *
3100 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3101 */
3102static void
3103xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3104 do {
3105 if ((CUR == '\\') || (CUR == '.')) {
3106 xmlFAParseCharClassEsc(ctxt);
3107 } else {
3108 xmlFAParseCharRange(ctxt);
3109 }
3110 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3111 (ctxt->error == 0));
3112}
3113
3114/**
3115 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003116 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003117 *
3118 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3119 * [15] negCharGroup ::= '^' posCharGroup
3120 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3121 * [12] charClassExpr ::= '[' charGroup ']'
3122 */
3123static void
3124xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3125 int n = ctxt->neg;
3126 while ((CUR != ']') && (ctxt->error == 0)) {
3127 if (CUR == '^') {
3128 int neg = ctxt->neg;
3129
3130 NEXT;
3131 ctxt->neg = !ctxt->neg;
3132 xmlFAParsePosCharGroup(ctxt);
3133 ctxt->neg = neg;
3134 } else if (CUR == '-') {
3135 NEXT;
3136 ctxt->neg = !ctxt->neg;
3137 if (CUR != '[') {
3138 ERROR("charClassExpr: '[' expected");
3139 break;
3140 }
3141 NEXT;
3142 xmlFAParseCharGroup(ctxt);
3143 if (CUR == ']') {
3144 NEXT;
3145 } else {
3146 ERROR("charClassExpr: ']' expected");
3147 break;
3148 }
3149 break;
3150 } else if (CUR != ']') {
3151 xmlFAParsePosCharGroup(ctxt);
3152 }
3153 }
3154 ctxt->neg = n;
3155}
3156
3157/**
3158 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003159 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003160 *
3161 * [11] charClass ::= charClassEsc | charClassExpr
3162 * [12] charClassExpr ::= '[' charGroup ']'
3163 */
3164static void
3165xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3166 if (CUR == '[') {
3167 NEXT;
3168 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3169 if (ctxt->atom == NULL)
3170 return;
3171 xmlFAParseCharGroup(ctxt);
3172 if (CUR == ']') {
3173 NEXT;
3174 } else {
3175 ERROR("xmlFAParseCharClass: ']' expected");
3176 }
3177 } else {
3178 xmlFAParseCharClassEsc(ctxt);
3179 }
3180}
3181
3182/**
3183 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003184 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003185 *
3186 * [8] QuantExact ::= [0-9]+
3187 */
3188static int
3189xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3190 int ret = 0;
3191 int ok = 0;
3192
3193 while ((CUR >= '0') && (CUR <= '9')) {
3194 ret = ret * 10 + (CUR - '0');
3195 ok = 1;
3196 NEXT;
3197 }
3198 if (ok != 1) {
3199 return(-1);
3200 }
3201 return(ret);
3202}
3203
3204/**
3205 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003206 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003207 *
3208 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3209 * [5] quantity ::= quantRange | quantMin | QuantExact
3210 * [6] quantRange ::= QuantExact ',' QuantExact
3211 * [7] quantMin ::= QuantExact ','
3212 * [8] QuantExact ::= [0-9]+
3213 */
3214static int
3215xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3216 int cur;
3217
3218 cur = CUR;
3219 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3220 if (ctxt->atom != NULL) {
3221 if (cur == '?')
3222 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3223 else if (cur == '*')
3224 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3225 else if (cur == '+')
3226 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3227 }
3228 NEXT;
3229 return(1);
3230 }
3231 if (cur == '{') {
3232 int min = 0, max = 0;
3233
3234 NEXT;
3235 cur = xmlFAParseQuantExact(ctxt);
3236 if (cur >= 0)
3237 min = cur;
3238 if (CUR == ',') {
3239 NEXT;
3240 cur = xmlFAParseQuantExact(ctxt);
3241 if (cur >= 0)
3242 max = cur;
3243 }
3244 if (CUR == '}') {
3245 NEXT;
3246 } else {
3247 ERROR("Unterminated quantifier");
3248 }
3249 if (max == 0)
3250 max = min;
3251 if (ctxt->atom != NULL) {
3252 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3253 ctxt->atom->min = min;
3254 ctxt->atom->max = max;
3255 }
3256 return(1);
3257 }
3258 return(0);
3259}
3260
3261/**
3262 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003263 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003264 *
3265 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3266 */
3267static int
3268xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3269 int codepoint, len;
3270
3271 codepoint = xmlFAIsChar(ctxt);
3272 if (codepoint > 0) {
3273 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3274 if (ctxt->atom == NULL)
3275 return(-1);
3276 codepoint = CUR_SCHAR(ctxt->cur, len);
3277 ctxt->atom->codepoint = codepoint;
3278 NEXTL(len);
3279 return(1);
3280 } else if (CUR == '|') {
3281 return(0);
3282 } else if (CUR == 0) {
3283 return(0);
3284 } else if (CUR == ')') {
3285 return(0);
3286 } else if (CUR == '(') {
3287 xmlRegStatePtr start, oldend;
3288
3289 NEXT;
3290 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3291 start = ctxt->state;
3292 oldend = ctxt->end;
3293 ctxt->end = NULL;
3294 ctxt->atom = NULL;
3295 xmlFAParseRegExp(ctxt, 0);
3296 if (CUR == ')') {
3297 NEXT;
3298 } else {
3299 ERROR("xmlFAParseAtom: expecting ')'");
3300 }
3301 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3302 if (ctxt->atom == NULL)
3303 return(-1);
3304 ctxt->atom->start = start;
3305 ctxt->atom->stop = ctxt->state;
3306 ctxt->end = oldend;
3307 return(1);
3308 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3309 xmlFAParseCharClass(ctxt);
3310 return(1);
3311 }
3312 return(0);
3313}
3314
3315/**
3316 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003317 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003318 *
3319 * [3] piece ::= atom quantifier?
3320 */
3321static int
3322xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3323 int ret;
3324
3325 ctxt->atom = NULL;
3326 ret = xmlFAParseAtom(ctxt);
3327 if (ret == 0)
3328 return(0);
3329 if (ctxt->atom == NULL) {
3330 ERROR("internal: no atom generated");
3331 }
3332 xmlFAParseQuantifier(ctxt);
3333 return(1);
3334}
3335
3336/**
3337 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003338 * @ctxt: a regexp parser context
3339 * @first: is taht the first
Daniel Veillard4255d502002-04-16 15:50:10 +00003340 *
3341 * [2] branch ::= piece*
3342 */
3343static void
3344xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, int first) {
3345 xmlRegStatePtr previous;
3346 xmlRegAtomPtr prevatom = NULL;
3347 int ret;
3348
3349 previous = ctxt->state;
3350 ret = xmlFAParsePiece(ctxt);
3351 if (ret != 0) {
3352 if (first) {
3353 xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom);
3354 previous = ctxt->state;
3355 } else {
3356 prevatom = ctxt->atom;
3357 }
3358 ctxt->atom = NULL;
3359 }
3360 while ((ret != 0) && (ctxt->error == 0)) {
3361 ret = xmlFAParsePiece(ctxt);
3362 if (ret != 0) {
3363 if (first) {
3364 xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom);
3365 } else {
3366 xmlFAGenerateTransitions(ctxt, previous, NULL, prevatom);
3367 prevatom = ctxt->atom;
3368 }
3369 previous = ctxt->state;
3370 ctxt->atom = NULL;
3371 }
3372 }
3373 if (!first) {
3374 xmlFAGenerateTransitions(ctxt, previous, ctxt->end, prevatom);
3375 }
3376}
3377
3378/**
3379 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003380 * @ctxt: a regexp parser context
3381 * @top: is that the top-level expressions ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003382 *
3383 * [1] regExp ::= branch ( '|' branch )*
3384 */
3385static void
3386xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
3387 xmlRegStatePtr start, end, oldend;
3388
3389 oldend = ctxt->end;
3390
3391 start = ctxt->state;
3392 xmlFAParseBranch(ctxt, (ctxt->end == NULL));
3393 if (CUR != '|') {
3394 ctxt->end = ctxt->state;
3395 return;
3396 }
3397 end = ctxt->state;
3398 while ((CUR == '|') && (ctxt->error == 0)) {
3399 NEXT;
3400 ctxt->state = start;
3401 ctxt->end = end;
3402 xmlFAParseBranch(ctxt, 0);
3403 }
3404 if (!top)
3405 ctxt->end = oldend;
3406}
3407
3408/************************************************************************
3409 * *
3410 * The basic API *
3411 * *
3412 ************************************************************************/
3413
3414/**
3415 * xmlRegexpPrint:
3416 * @output: the file for the output debug
3417 * @regexp: the compiled regexp
3418 *
3419 * Print the content of the compiled regular expression
3420 */
3421void
3422xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3423 int i;
3424
3425 fprintf(output, " regexp: ");
3426 if (regexp == NULL) {
3427 fprintf(output, "NULL\n");
3428 return;
3429 }
3430 fprintf(output, "'%s' ", regexp->string);
3431 fprintf(output, "\n");
3432 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3433 for (i = 0;i < regexp->nbAtoms; i++) {
3434 fprintf(output, " %02d ", i);
3435 xmlRegPrintAtom(output, regexp->atoms[i]);
3436 }
3437 fprintf(output, "%d states:", regexp->nbStates);
3438 fprintf(output, "\n");
3439 for (i = 0;i < regexp->nbStates; i++) {
3440 xmlRegPrintState(output, regexp->states[i]);
3441 }
3442 fprintf(output, "%d counters:\n", regexp->nbCounters);
3443 for (i = 0;i < regexp->nbCounters; i++) {
3444 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
3445 regexp->counters[i].max);
3446 }
3447}
3448
3449/**
3450 * xmlRegexpCompile:
3451 * @regexp: a regular expression string
3452 *
3453 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
3454 * Appendix F and build an automata suitable for testing strings against
3455 * that regular expression
3456 *
3457 * Returns the compiled expression or NULL in case of error
3458 */
3459xmlRegexpPtr
3460xmlRegexpCompile(const xmlChar *regexp) {
3461 xmlRegexpPtr ret;
3462 xmlRegParserCtxtPtr ctxt;
3463
3464 ctxt = xmlRegNewParserCtxt(regexp);
3465 if (ctxt == NULL)
3466 return(NULL);
3467
3468 /* initialize the parser */
3469 ctxt->end = NULL;
3470 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3471 xmlRegStatePush(ctxt, ctxt->start);
3472
3473 /* parse the expression building an automata */
3474 xmlFAParseRegExp(ctxt, 1);
3475 if (CUR != 0) {
3476 ERROR("xmlFAParseRegExp: extra characters");
3477 }
3478 ctxt->end = ctxt->state;
3479 ctxt->start->type = XML_REGEXP_START_STATE;
3480 ctxt->end->type = XML_REGEXP_FINAL_STATE;
3481
3482 /* remove the Epsilon except for counted transitions */
3483 xmlFAEliminateEpsilonTransitions(ctxt);
3484
3485
3486 if (ctxt->error != 0) {
3487 xmlRegFreeParserCtxt(ctxt);
3488 return(NULL);
3489 }
3490 ret = xmlRegEpxFromParse(ctxt);
3491 xmlRegFreeParserCtxt(ctxt);
3492 return(ret);
3493}
3494
3495/**
3496 * xmlRegexpExec:
3497 * @comp: the compiled regular expression
3498 * @content: the value to check against the regular expression
3499 *
3500 * Check if the regular expression generate the value
3501 *
3502 * Returns 1 if it matches, 0 if not and a negativa value in case of error
3503 */
3504int
3505xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
3506 if ((comp == NULL) || (content == NULL))
3507 return(-1);
3508 return(xmlFARegExec(comp, content));
3509}
3510
3511/**
3512 * xmlRegFreeRegexp:
3513 * @regexp: the regexp
3514 *
3515 * Free a regexp
3516 */
3517void
3518xmlRegFreeRegexp(xmlRegexpPtr regexp) {
3519 int i;
3520 if (regexp == NULL)
3521 return;
3522
3523 if (regexp->string != NULL)
3524 xmlFree(regexp->string);
3525 if (regexp->states != NULL) {
3526 for (i = 0;i < regexp->nbStates;i++)
3527 xmlRegFreeState(regexp->states[i]);
3528 xmlFree(regexp->states);
3529 }
3530 if (regexp->atoms != NULL) {
3531 for (i = 0;i < regexp->nbAtoms;i++)
3532 xmlRegFreeAtom(regexp->atoms[i]);
3533 xmlFree(regexp->atoms);
3534 }
3535 if (regexp->counters != NULL)
3536 xmlFree(regexp->counters);
3537 xmlFree(regexp);
3538}
3539
3540#ifdef LIBXML_AUTOMATA_ENABLED
3541/************************************************************************
3542 * *
3543 * The Automata interface *
3544 * *
3545 ************************************************************************/
3546
3547/**
3548 * xmlNewAutomata:
3549 *
3550 * Create a new automata
3551 *
3552 * Returns the new object or NULL in case of failure
3553 */
3554xmlAutomataPtr
3555xmlNewAutomata(void) {
3556 xmlAutomataPtr ctxt;
3557
3558 ctxt = xmlRegNewParserCtxt(NULL);
3559 if (ctxt == NULL)
3560 return(NULL);
3561
3562 /* initialize the parser */
3563 ctxt->end = NULL;
3564 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3565 xmlRegStatePush(ctxt, ctxt->start);
3566
3567 return(ctxt);
3568}
3569
3570/**
3571 * xmlFreeAutomata:
3572 * @am: an automata
3573 *
3574 * Free an automata
3575 */
3576void
3577xmlFreeAutomata(xmlAutomataPtr am) {
3578 if (am == NULL)
3579 return;
3580 xmlRegFreeParserCtxt(am);
3581}
3582
3583/**
3584 * xmlAutomataGetInitState:
3585 * @am: an automata
3586 *
3587 * Returns the initial state of the automata
3588 */
3589xmlAutomataStatePtr
3590xmlAutomataGetInitState(xmlAutomataPtr am) {
3591 if (am == NULL)
3592 return(NULL);
3593 return(am->start);
3594}
3595
3596/**
3597 * xmlAutomataSetFinalState:
3598 * @am: an automata
3599 * @state: a state in this automata
3600 *
3601 * Makes that state a final state
3602 *
3603 * Returns 0 or -1 in case of error
3604 */
3605int
3606xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
3607 if ((am == NULL) || (state == NULL))
3608 return(-1);
3609 state->type = XML_REGEXP_FINAL_STATE;
3610 return(0);
3611}
3612
3613/**
3614 * xmlAutomataNewTransition:
3615 * @am: an automata
3616 * @from: the starting point of the transition
3617 * @to: the target point of the transition or NULL
3618 * @token: the input string associated to that transition
3619 * @data: data passed to the callback function if the transition is activated
3620 *
3621 * If @to is NULL, this create first a new target state in the automata
3622 * and then adds a transition from the @from state to the target state
3623 * activated by the value of @token
3624 *
3625 * Returns the target state or NULL in case of error
3626 */
3627xmlAutomataStatePtr
3628xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
3629 xmlAutomataStatePtr to, const xmlChar *token,
3630 void *data) {
3631 xmlRegAtomPtr atom;
3632
3633 if ((am == NULL) || (from == NULL) || (token == NULL))
3634 return(NULL);
3635 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
3636 atom->data = data;
3637 if (atom == NULL)
3638 return(NULL);
3639 atom->valuep = xmlStrdup(token);
3640
3641 xmlFAGenerateTransitions(am, from, to, atom);
3642 if (to == NULL)
3643 return(am->state);
3644 return(to);
3645}
3646
3647/**
3648 * xmlAutomataNewCountTrans:
3649 * @am: an automata
3650 * @from: the starting point of the transition
3651 * @to: the target point of the transition or NULL
3652 * @token: the input string associated to that transition
3653 * @min: the minimum successive occurences of token
3654 * @min: the maximum successive occurences of token
3655 *
3656 * If @to is NULL, this create first a new target state in the automata
3657 * and then adds a transition from the @from state to the target state
3658 * activated by a succession of input of value @token and whose number
3659 * is between @min and @max
3660 *
3661 * Returns the target state or NULL in case of error
3662 */
3663xmlAutomataStatePtr
3664xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
3665 xmlAutomataStatePtr to, const xmlChar *token,
3666 int min, int max, void *data) {
3667 xmlRegAtomPtr atom;
3668
3669 if ((am == NULL) || (from == NULL) || (token == NULL))
3670 return(NULL);
3671 if (min < 0)
3672 return(NULL);
3673 if ((max < min) || (max < 1))
3674 return(NULL);
3675 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
3676 if (atom == NULL)
3677 return(NULL);
3678 atom->valuep = xmlStrdup(token);
3679 atom->data = data;
3680 if (min == 0)
3681 atom->min = 1;
3682 else
3683 atom->min = min;
3684 atom->max = max;
3685
3686 xmlFAGenerateTransitions(am, from, to, atom);
3687 if (to == NULL)
3688 to = am->state;
3689 if (to == NULL)
3690 return(NULL);
3691 if (min == 0)
3692 xmlFAGenerateEpsilonTransition(am, from, to);
3693 return(to);
3694}
3695
3696/**
Daniel Veillard7646b182002-04-20 06:41:40 +00003697 * xmlAutomataNewOnceTrans:
3698 * @am: an automata
3699 * @from: the starting point of the transition
3700 * @to: the target point of the transition or NULL
3701 * @token: the input string associated to that transition
3702 * @min: the minimum successive occurences of token
3703 * @min: the maximum successive occurences of token
3704 *
3705 * If @to is NULL, this create first a new target state in the automata
3706 * and then adds a transition from the @from state to the target state
3707 * activated by a succession of input of value @token and whose number
3708 * is between @min and @max, moreover that transistion can only be crossed
3709 * once.
3710 *
3711 * Returns the target state or NULL in case of error
3712 */
3713xmlAutomataStatePtr
3714xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
3715 xmlAutomataStatePtr to, const xmlChar *token,
3716 int min, int max, void *data) {
3717 xmlRegAtomPtr atom;
3718 int counter;
3719
3720 if ((am == NULL) || (from == NULL) || (token == NULL))
3721 return(NULL);
3722 if (min < 1)
3723 return(NULL);
3724 if ((max < min) || (max < 1))
3725 return(NULL);
3726 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
3727 if (atom == NULL)
3728 return(NULL);
3729 atom->valuep = xmlStrdup(token);
3730 atom->data = data;
3731 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
3732 if (min == 0)
3733 atom->min = 1;
3734 else
3735 atom->min = min;
3736 atom->max = max;
3737 /*
3738 * associate a counter to the transition.
3739 */
3740 counter = xmlRegGetCounter(am);
3741 am->counters[counter].min = 1;
3742 am->counters[counter].max = 1;
3743
3744 /* xmlFAGenerateTransitions(am, from, to, atom); */
3745 if (to == NULL) {
3746 to = xmlRegNewState(am);
3747 xmlRegStatePush(am, to);
3748 }
3749 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
3750 xmlRegAtomPush(am, atom);
3751 am->state = to;
3752 if (to == NULL)
3753 to = am->state;
3754 if (to == NULL)
3755 return(NULL);
3756 return(to);
3757}
3758
3759/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003760 * xmlAutomataNewState:
3761 * @am: an automata
3762 *
3763 * Create a new disconnected state in the automata
3764 *
3765 * Returns the new state or NULL in case of error
3766 */
3767xmlAutomataStatePtr
3768xmlAutomataNewState(xmlAutomataPtr am) {
3769 xmlAutomataStatePtr to;
3770
3771 if (am == NULL)
3772 return(NULL);
3773 to = xmlRegNewState(am);
3774 xmlRegStatePush(am, to);
3775 return(to);
3776}
3777
3778/**
3779 * xmlAutomataNewTransition:
3780 * @am: an automata
3781 * @from: the starting point of the transition
3782 * @to: the target point of the transition or NULL
3783 *
3784 * If @to is NULL, this create first a new target state in the automata
3785 * and then adds a an epsilon transition from the @from state to the
3786 * target state
3787 *
3788 * Returns the target state or NULL in case of error
3789 */
3790xmlAutomataStatePtr
3791xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
3792 xmlAutomataStatePtr to) {
3793 if ((am == NULL) || (from == NULL))
3794 return(NULL);
3795 xmlFAGenerateEpsilonTransition(am, from, to);
3796 if (to == NULL)
3797 return(am->state);
3798 return(to);
3799}
3800
Daniel Veillardb509f152002-04-17 16:28:10 +00003801/**
Daniel Veillard7646b182002-04-20 06:41:40 +00003802 * xmlAutomataNewAllTrans:
3803 * @am: an automata
3804 * @from: the starting point of the transition
3805 * @to: the target point of the transition or NULL
3806 *
3807 * If @to is NULL, this create first a new target state in the automata
3808 * and then adds a an ALL transition from the @from state to the
3809 * target state. That transition is an epsilon transition allowed only when
3810 * all transitions from the @from node have been activated.
3811 *
3812 * Returns the target state or NULL in case of error
3813 */
3814xmlAutomataStatePtr
3815xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00003816 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00003817 if ((am == NULL) || (from == NULL))
3818 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00003819 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00003820 if (to == NULL)
3821 return(am->state);
3822 return(to);
3823}
3824
3825/**
Daniel Veillardb509f152002-04-17 16:28:10 +00003826 * xmlAutomataNewCounter:
3827 * @am: an automata
3828 * @min: the minimal value on the counter
3829 * @max: the maximal value on the counter
3830 *
3831 * Create a new counter
3832 *
3833 * Returns the counter number or -1 in case of error
3834 */
3835int
3836xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
3837 int ret;
3838
3839 if (am == NULL)
3840 return(-1);
3841
3842 ret = xmlRegGetCounter(am);
3843 if (ret < 0)
3844 return(-1);
3845 am->counters[ret].min = min;
3846 am->counters[ret].max = max;
3847 return(ret);
3848}
3849
3850/**
3851 * xmlAutomataNewCountedTrans:
3852 * @am: an automata
3853 * @from: the starting point of the transition
3854 * @to: the target point of the transition or NULL
3855 * @counter: the counter associated to that transition
3856 *
3857 * If @to is NULL, this create first a new target state in the automata
3858 * and then adds an epsilon transition from the @from state to the target state
3859 * which will increment the counter provided
3860 *
3861 * Returns the target state or NULL in case of error
3862 */
3863xmlAutomataStatePtr
3864xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
3865 xmlAutomataStatePtr to, int counter) {
3866 if ((am == NULL) || (from == NULL) || (counter < 0))
3867 return(NULL);
3868 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
3869 if (to == NULL)
3870 return(am->state);
3871 return(to);
3872}
3873
3874/**
3875 * xmlAutomataNewCounterTrans:
3876 * @am: an automata
3877 * @from: the starting point of the transition
3878 * @to: the target point of the transition or NULL
3879 * @counter: the counter associated to that transition
3880 *
3881 * If @to is NULL, this create first a new target state in the automata
3882 * and then adds an epsilon transition from the @from state to the target state
3883 * which will be allowed only if the counter is within the right range.
3884 *
3885 * Returns the target state or NULL in case of error
3886 */
3887xmlAutomataStatePtr
3888xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
3889 xmlAutomataStatePtr to, int counter) {
3890 if ((am == NULL) || (from == NULL) || (counter < 0))
3891 return(NULL);
3892 xmlFAGenerateCountedTransition(am, from, to, counter);
3893 if (to == NULL)
3894 return(am->state);
3895 return(to);
3896}
Daniel Veillard4255d502002-04-16 15:50:10 +00003897
3898/**
3899 * xmlAutomataCompile:
3900 * @am: an automata
3901 *
3902 * Compile the automata into a Reg Exp ready for being executed.
3903 * The automata should be free after this point.
3904 *
3905 * Returns the compiled regexp or NULL in case of error
3906 */
3907xmlRegexpPtr
3908xmlAutomataCompile(xmlAutomataPtr am) {
3909 xmlRegexpPtr ret;
3910
3911 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillarde19fc232002-04-22 16:01:24 +00003912 xmlFAComputesDeterminism(am);
Daniel Veillard4255d502002-04-16 15:50:10 +00003913 ret = xmlRegEpxFromParse(am);
3914
3915 return(ret);
3916}
Daniel Veillarde19fc232002-04-22 16:01:24 +00003917
3918/**
3919 * xmlAutomataIsDeterminist:
3920 * @am: an automata
3921 *
3922 * Checks if an automata is determinist.
3923 *
3924 * Returns 1 if true, 0 if not, and -1 in case of error
3925 */
3926int
3927xmlAutomataIsDeterminist(xmlAutomataPtr am) {
3928 int ret;
3929
3930 if (am == NULL)
3931 return(-1);
3932
3933 ret = xmlFAComputesDeterminism(am);
3934 return(ret);
3935}
Daniel Veillard4255d502002-04-16 15:50:10 +00003936#endif /* LIBXML_AUTOMATA_ENABLED */
3937#endif /* LIBXML_REGEXP_ENABLED */