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