dsrc isn't necessary for this repo
[client-tools.git] / src / external / 3rd / library / libxml / xmlregexp.c
blob155a2f00c150d7d034942f7ae1dac5399a167865
1 /*
2 * regexp.c: generic and extensible Regular Expression engine
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
12 * See Copyright for the status of this software.
14 * Daniel Veillard <veillard@redhat.com>
17 #define IN_LIBXML
18 #include "libxml.h"
20 #ifdef LIBXML_REGEXP_ENABLED
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>
30 /* #define DEBUG_REGEXP_GRAPH */
31 /* #define DEBUG_REGEXP_EXEC */
32 /* #define DEBUG_PUSH */
33 /* #define DEBUG_COMPACTION */
35 #define ERROR(str) ctxt->error = 1; \
36 xmlGenericError(xmlGenericErrorContext, "Regexp: %s: %s\n", str, ctxt->cur)
37 #define NEXT ctxt->cur++
38 #define CUR (*(ctxt->cur))
39 #define NXT(index) (ctxt->cur[index])
41 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
42 #define NEXTL(l) ctxt->cur += l;
44 /**
45 * TODO:
47 * macro to flag unimplemented blocks
49 #define TODO \
50 xmlGenericError(xmlGenericErrorContext, \
51 "Unimplemented block at %s:%d\n", \
52 __FILE__, __LINE__);
55 /************************************************************************
56 * *
57 * Datatypes and structures *
58 * *
59 ************************************************************************/
61 typedef enum {
62 XML_REGEXP_EPSILON = 1,
63 XML_REGEXP_CHARVAL,
64 XML_REGEXP_RANGES,
65 XML_REGEXP_SUBREG,
66 XML_REGEXP_STRING,
67 XML_REGEXP_ANYCHAR, /* . */
68 XML_REGEXP_ANYSPACE, /* \s */
69 XML_REGEXP_NOTSPACE, /* \S */
70 XML_REGEXP_INITNAME, /* \l */
71 XML_REGEXP_NOTINITNAME, /* \l */
72 XML_REGEXP_NAMECHAR, /* \c */
73 XML_REGEXP_NOTNAMECHAR, /* \C */
74 XML_REGEXP_DECIMAL, /* \d */
75 XML_REGEXP_NOTDECIMAL, /* \d */
76 XML_REGEXP_REALCHAR, /* \w */
77 XML_REGEXP_NOTREALCHAR, /* \w */
78 XML_REGEXP_LETTER,
79 XML_REGEXP_LETTER_UPPERCASE,
80 XML_REGEXP_LETTER_LOWERCASE,
81 XML_REGEXP_LETTER_TITLECASE,
82 XML_REGEXP_LETTER_MODIFIER,
83 XML_REGEXP_LETTER_OTHERS,
84 XML_REGEXP_MARK,
85 XML_REGEXP_MARK_NONSPACING,
86 XML_REGEXP_MARK_SPACECOMBINING,
87 XML_REGEXP_MARK_ENCLOSING,
88 XML_REGEXP_NUMBER,
89 XML_REGEXP_NUMBER_DECIMAL,
90 XML_REGEXP_NUMBER_LETTER,
91 XML_REGEXP_NUMBER_OTHERS,
92 XML_REGEXP_PUNCT,
93 XML_REGEXP_PUNCT_CONNECTOR,
94 XML_REGEXP_PUNCT_DASH,
95 XML_REGEXP_PUNCT_OPEN,
96 XML_REGEXP_PUNCT_CLOSE,
97 XML_REGEXP_PUNCT_INITQUOTE,
98 XML_REGEXP_PUNCT_FINQUOTE,
99 XML_REGEXP_PUNCT_OTHERS,
100 XML_REGEXP_SEPAR,
101 XML_REGEXP_SEPAR_SPACE,
102 XML_REGEXP_SEPAR_LINE,
103 XML_REGEXP_SEPAR_PARA,
104 XML_REGEXP_SYMBOL,
105 XML_REGEXP_SYMBOL_MATH,
106 XML_REGEXP_SYMBOL_CURRENCY,
107 XML_REGEXP_SYMBOL_MODIFIER,
108 XML_REGEXP_SYMBOL_OTHERS,
109 XML_REGEXP_OTHER,
110 XML_REGEXP_OTHER_CONTROL,
111 XML_REGEXP_OTHER_FORMAT,
112 XML_REGEXP_OTHER_PRIVATE,
113 XML_REGEXP_OTHER_NA,
114 XML_REGEXP_BLOCK_NAME
115 } xmlRegAtomType;
117 typedef enum {
118 XML_REGEXP_QUANT_EPSILON = 1,
119 XML_REGEXP_QUANT_ONCE,
120 XML_REGEXP_QUANT_OPT,
121 XML_REGEXP_QUANT_MULT,
122 XML_REGEXP_QUANT_PLUS,
123 XML_REGEXP_QUANT_ONCEONLY,
124 XML_REGEXP_QUANT_ALL,
125 XML_REGEXP_QUANT_RANGE
126 } xmlRegQuantType;
128 typedef enum {
129 XML_REGEXP_START_STATE = 1,
130 XML_REGEXP_FINAL_STATE,
131 XML_REGEXP_TRANS_STATE
132 } xmlRegStateType;
134 typedef enum {
135 XML_REGEXP_MARK_NORMAL = 0,
136 XML_REGEXP_MARK_START,
137 XML_REGEXP_MARK_VISITED
138 } xmlRegMarkedType;
140 typedef struct _xmlRegRange xmlRegRange;
141 typedef xmlRegRange *xmlRegRangePtr;
143 struct _xmlRegRange {
144 int neg;
145 xmlRegAtomType type;
146 int start;
147 int end;
148 xmlChar *blockName;
151 typedef struct _xmlRegAtom xmlRegAtom;
152 typedef xmlRegAtom *xmlRegAtomPtr;
154 typedef struct _xmlAutomataState xmlRegState;
155 typedef xmlRegState *xmlRegStatePtr;
157 struct _xmlRegAtom {
158 int no;
159 xmlRegAtomType type;
160 xmlRegQuantType quant;
161 int min;
162 int max;
164 void *valuep;
165 void *valuep2;
166 int neg;
167 int codepoint;
168 xmlRegStatePtr start;
169 xmlRegStatePtr stop;
170 int maxRanges;
171 int nbRanges;
172 xmlRegRangePtr *ranges;
173 void *data;
176 typedef struct _xmlRegCounter xmlRegCounter;
177 typedef xmlRegCounter *xmlRegCounterPtr;
179 struct _xmlRegCounter {
180 int min;
181 int max;
184 typedef struct _xmlRegTrans xmlRegTrans;
185 typedef xmlRegTrans *xmlRegTransPtr;
187 struct _xmlRegTrans {
188 xmlRegAtomPtr atom;
189 int to;
190 int counter;
191 int count;
194 struct _xmlAutomataState {
195 xmlRegStateType type;
196 xmlRegMarkedType mark;
197 xmlRegMarkedType reached;
198 int no;
200 int maxTrans;
201 int nbTrans;
202 xmlRegTrans *trans;
205 typedef struct _xmlAutomata xmlRegParserCtxt;
206 typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
208 struct _xmlAutomata {
209 xmlChar *string;
210 xmlChar *cur;
212 int error;
213 int neg;
215 xmlRegStatePtr start;
216 xmlRegStatePtr end;
217 xmlRegStatePtr state;
219 xmlRegAtomPtr atom;
221 int maxAtoms;
222 int nbAtoms;
223 xmlRegAtomPtr *atoms;
225 int maxStates;
226 int nbStates;
227 xmlRegStatePtr *states;
229 int maxCounters;
230 int nbCounters;
231 xmlRegCounter *counters;
233 int determinist;
236 struct _xmlRegexp {
237 xmlChar *string;
238 int nbStates;
239 xmlRegStatePtr *states;
240 int nbAtoms;
241 xmlRegAtomPtr *atoms;
242 int nbCounters;
243 xmlRegCounter *counters;
244 int determinist;
246 * That's the compact form for determinists automatas
248 int nbstates;
249 int *compact;
250 void **transdata;
251 int nbstrings;
252 xmlChar **stringMap;
255 typedef struct _xmlRegExecRollback xmlRegExecRollback;
256 typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
258 struct _xmlRegExecRollback {
259 xmlRegStatePtr state;/* the current state */
260 int index; /* the index in the input stack */
261 int nextbranch; /* the next transition to explore in that state */
262 int *counts; /* save the automate state if it has some */
265 typedef struct _xmlRegInputToken xmlRegInputToken;
266 typedef xmlRegInputToken *xmlRegInputTokenPtr;
268 struct _xmlRegInputToken {
269 xmlChar *value;
270 void *data;
273 struct _xmlRegExecCtxt {
274 int status; /* execution status != 0 indicate an error */
275 int determinist; /* did we found an inderterministic behaviour */
276 xmlRegexpPtr comp; /* the compiled regexp */
277 xmlRegExecCallbacks callback;
278 void *data;
280 xmlRegStatePtr state;/* the current state */
281 int transno; /* the current transition on that state */
282 int transcount; /* the number of char in char counted transitions */
285 * A stack of rollback states
287 int maxRollbacks;
288 int nbRollbacks;
289 xmlRegExecRollback *rollbacks;
292 * The state of the automata if any
294 int *counts;
297 * The input stack
299 int inputStackMax;
300 int inputStackNr;
301 int index;
302 int *charStack;
303 const xmlChar *inputString; /* when operating on characters */
304 xmlRegInputTokenPtr inputStack;/* when operating on strings */
308 #define REGEXP_ALL_COUNTER 0x123456
309 #define REGEXP_ALL_LAX_COUNTER 0x123457
311 static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
312 static void xmlRegFreeState(xmlRegStatePtr state);
313 static void xmlRegFreeAtom(xmlRegAtomPtr atom);
315 /************************************************************************
317 * Allocation/Deallocation *
319 ************************************************************************/
321 static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
323 * xmlRegEpxFromParse:
324 * @ctxt: the parser context used to build it
326 * Allocate a new regexp and fill it with the reult from the parser
328 * Returns the new regexp or NULL in case of error
330 static xmlRegexpPtr
331 xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
332 xmlRegexpPtr ret;
334 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
335 if (ret == NULL)
336 return(NULL);
337 memset(ret, 0, sizeof(xmlRegexp));
338 ret->string = ctxt->string;
339 ctxt->string = NULL;
340 ret->nbStates = ctxt->nbStates;
341 ctxt->nbStates = 0;
342 ret->states = ctxt->states;
343 ctxt->states = NULL;
344 ret->nbAtoms = ctxt->nbAtoms;
345 ctxt->nbAtoms = 0;
346 ret->atoms = ctxt->atoms;
347 ctxt->atoms = NULL;
348 ret->nbCounters = ctxt->nbCounters;
349 ctxt->nbCounters = 0;
350 ret->counters = ctxt->counters;
351 ctxt->counters = NULL;
352 ret->determinist = ctxt->determinist;
354 if ((ret->determinist != 0) &&
355 (ret->nbCounters == 0) &&
356 (ret->atoms != NULL) &&
357 (ret->atoms[0] != NULL) &&
358 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
359 int i, j, nbstates = 0, nbatoms = 0;
360 int *stateRemap;
361 int *stringRemap;
362 int *transitions;
363 void **transdata;
364 xmlChar **stringMap;
365 xmlChar *value;
368 * Switch to a compact representation
369 * 1/ counting the effective number of states left
370 * 2/ conting the unique number of atoms, and check that
371 * they are all of the string type
372 * 3/ build a table state x atom for the transitions
375 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
376 for (i = 0;i < ret->nbStates;i++) {
377 if (ret->states[i] != NULL) {
378 stateRemap[i] = nbstates;
379 nbstates++;
380 } else {
381 stateRemap[i] = -1;
384 #ifdef DEBUG_COMPACTION
385 printf("Final: %d states\n", nbstates);
386 #endif
387 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
388 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
389 for (i = 0;i < ret->nbAtoms;i++) {
390 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
391 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
392 value = ret->atoms[i]->valuep;
393 for (j = 0;j < nbatoms;j++) {
394 if (xmlStrEqual(stringMap[j], value)) {
395 stringRemap[i] = j;
396 break;
399 if (j >= nbatoms) {
400 stringRemap[i] = nbatoms;
401 stringMap[nbatoms] = xmlStrdup(value);
402 nbatoms++;
404 } else {
405 xmlFree(stateRemap);
406 xmlFree(stringRemap);
407 for (i = 0;i < nbatoms;i++)
408 xmlFree(stringMap[i]);
409 xmlFree(stringMap);
410 goto fail_compact;
413 #ifdef DEBUG_COMPACTION
414 printf("Final: %d atoms\n", nbatoms);
415 #endif
418 * Allocate the transition table. The first entry for each
419 * state correspond to the state type.
421 transitions = (int *) xmlMalloc(nbstates * (nbatoms + 1) * sizeof(int));
422 transdata = NULL;
423 memset(transitions, 0, nbstates * (nbatoms + 1) * sizeof(int));
425 for (i = 0;i < ret->nbStates;i++) {
426 int stateno, atomno, targetno, prev;
427 xmlRegStatePtr state;
428 xmlRegTransPtr trans;
430 stateno = stateRemap[i];
431 if (stateno == -1)
432 continue;
433 state = ret->states[i];
435 transitions[stateno * (nbatoms + 1)] = state->type;
437 for (j = 0;j < state->nbTrans;j++) {
438 trans = &(state->trans[j]);
439 if ((trans->to == -1) || (trans->atom == NULL))
440 continue;
441 atomno = stringRemap[trans->atom->no];
442 if ((trans->atom->data != NULL) && (transdata == NULL)) {
443 transdata = (void **) xmlMalloc(nbstates * nbatoms *
444 sizeof(void *));
445 if (transdata != NULL)
446 memset(transdata, 0,
447 nbstates * nbatoms * sizeof(void *));
449 targetno = stateRemap[trans->to];
451 * if the same atome can generate transition to 2 different
452 * states then it means the automata is not determinist and
453 * the compact form can't be used !
455 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
456 if (prev != 0) {
457 if (prev != targetno + 1) {
458 printf("not determinist\n");
459 ret->determinist = 0;
460 #ifdef DEBUG_COMPACTION
461 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
462 i, j, trans->atom->no, trans->to, atomno, targetno);
463 printf(" previous to is %d\n", prev);
464 #endif
465 ret->determinist = 0;
466 if (transdata != NULL)
467 xmlFree(transdata);
468 xmlFree(transitions);
469 xmlFree(stateRemap);
470 xmlFree(stringRemap);
471 for (i = 0;i < nbatoms;i++)
472 xmlFree(stringMap[i]);
473 xmlFree(stringMap);
474 goto fail_compact;
476 } else {
477 #if 0
478 printf("State %d trans %d: atom %d to %d : %d to %d\n",
479 i, j, trans->atom->no, trans->to, atomno, targetno);
480 #endif
481 transitions[stateno * (nbatoms + 1) + atomno + 1] =
482 targetno + 1; /* to avoid 0 */
483 if (transdata != NULL)
484 transdata[stateno * nbatoms + atomno] =
485 trans->atom->data;
489 ret->determinist = 1;
490 #ifdef DEBUG_COMPACTION
492 * Debug
494 for (i = 0;i < nbstates;i++) {
495 for (j = 0;j < nbatoms + 1;j++) {
496 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
498 printf("\n");
500 printf("\n");
501 #endif
503 * Cleanup of the old data
505 if (ret->states != NULL) {
506 for (i = 0;i < ret->nbStates;i++)
507 xmlRegFreeState(ret->states[i]);
508 xmlFree(ret->states);
510 ret->states = NULL;
511 ret->nbStates = 0;
512 if (ret->atoms != NULL) {
513 for (i = 0;i < ret->nbAtoms;i++)
514 xmlRegFreeAtom(ret->atoms[i]);
515 xmlFree(ret->atoms);
517 ret->atoms = NULL;
518 ret->nbAtoms = 0;
520 ret->compact = transitions;
521 ret->transdata = transdata;
522 ret->stringMap = stringMap;
523 ret->nbstrings = nbatoms;
524 ret->nbstates = nbstates;
525 xmlFree(stateRemap);
526 xmlFree(stringRemap);
528 fail_compact:
529 return(ret);
533 * xmlRegNewParserCtxt:
534 * @string: the string to parse
536 * Allocate a new regexp parser context
538 * Returns the new context or NULL in case of error
540 static xmlRegParserCtxtPtr
541 xmlRegNewParserCtxt(const xmlChar *string) {
542 xmlRegParserCtxtPtr ret;
544 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
545 if (ret == NULL)
546 return(NULL);
547 memset(ret, 0, sizeof(xmlRegParserCtxt));
548 if (string != NULL)
549 ret->string = xmlStrdup(string);
550 ret->cur = ret->string;
551 ret->neg = 0;
552 ret->error = 0;
553 ret->determinist = -1;
554 return(ret);
558 * xmlRegNewRange:
559 * @ctxt: the regexp parser context
560 * @neg: is that negative
561 * @type: the type of range
562 * @start: the start codepoint
563 * @end: the end codepoint
565 * Allocate a new regexp range
567 * Returns the new range or NULL in case of error
569 static xmlRegRangePtr
570 xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
571 int neg, xmlRegAtomType type, int start, int end) {
572 xmlRegRangePtr ret;
574 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
575 if (ret == NULL) {
576 ERROR("failed to allocate regexp range");
577 return(NULL);
579 ret->neg = neg;
580 ret->type = type;
581 ret->start = start;
582 ret->end = end;
583 return(ret);
587 * xmlRegFreeRange:
588 * @range: the regexp range
590 * Free a regexp range
592 static void
593 xmlRegFreeRange(xmlRegRangePtr range) {
594 if (range == NULL)
595 return;
597 if (range->blockName != NULL)
598 xmlFree(range->blockName);
599 xmlFree(range);
603 * xmlRegNewAtom:
604 * @ctxt: the regexp parser context
605 * @type: the type of atom
607 * Allocate a new regexp range
609 * Returns the new atom or NULL in case of error
611 static xmlRegAtomPtr
612 xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
613 xmlRegAtomPtr ret;
615 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
616 if (ret == NULL) {
617 ERROR("failed to allocate regexp atom");
618 return(NULL);
620 memset(ret, 0, sizeof(xmlRegAtom));
621 ret->type = type;
622 ret->quant = XML_REGEXP_QUANT_ONCE;
623 ret->min = 0;
624 ret->max = 0;
625 return(ret);
629 * xmlRegFreeAtom:
630 * @atom: the regexp atom
632 * Free a regexp atom
634 static void
635 xmlRegFreeAtom(xmlRegAtomPtr atom) {
636 int i;
638 if (atom == NULL)
639 return;
641 for (i = 0;i < atom->nbRanges;i++)
642 xmlRegFreeRange(atom->ranges[i]);
643 if (atom->ranges != NULL)
644 xmlFree(atom->ranges);
645 if (atom->type == XML_REGEXP_STRING)
646 xmlFree(atom->valuep);
647 xmlFree(atom);
650 static xmlRegStatePtr
651 xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
652 xmlRegStatePtr ret;
654 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
655 if (ret == NULL) {
656 ERROR("failed to allocate regexp state");
657 return(NULL);
659 memset(ret, 0, sizeof(xmlRegState));
660 ret->type = XML_REGEXP_TRANS_STATE;
661 ret->mark = XML_REGEXP_MARK_NORMAL;
662 return(ret);
666 * xmlRegFreeState:
667 * @state: the regexp state
669 * Free a regexp state
671 static void
672 xmlRegFreeState(xmlRegStatePtr state) {
673 if (state == NULL)
674 return;
676 if (state->trans != NULL)
677 xmlFree(state->trans);
678 xmlFree(state);
682 * xmlRegFreeParserCtxt:
683 * @ctxt: the regexp parser context
685 * Free a regexp parser context
687 static void
688 xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
689 int i;
690 if (ctxt == NULL)
691 return;
693 if (ctxt->string != NULL)
694 xmlFree(ctxt->string);
695 if (ctxt->states != NULL) {
696 for (i = 0;i < ctxt->nbStates;i++)
697 xmlRegFreeState(ctxt->states[i]);
698 xmlFree(ctxt->states);
700 if (ctxt->atoms != NULL) {
701 for (i = 0;i < ctxt->nbAtoms;i++)
702 xmlRegFreeAtom(ctxt->atoms[i]);
703 xmlFree(ctxt->atoms);
705 if (ctxt->counters != NULL)
706 xmlFree(ctxt->counters);
707 xmlFree(ctxt);
710 /************************************************************************
712 * Display of Data structures *
714 ************************************************************************/
716 static void
717 xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
718 switch (type) {
719 case XML_REGEXP_EPSILON:
720 fprintf(output, "epsilon "); break;
721 case XML_REGEXP_CHARVAL:
722 fprintf(output, "charval "); break;
723 case XML_REGEXP_RANGES:
724 fprintf(output, "ranges "); break;
725 case XML_REGEXP_SUBREG:
726 fprintf(output, "subexpr "); break;
727 case XML_REGEXP_STRING:
728 fprintf(output, "string "); break;
729 case XML_REGEXP_ANYCHAR:
730 fprintf(output, "anychar "); break;
731 case XML_REGEXP_ANYSPACE:
732 fprintf(output, "anyspace "); break;
733 case XML_REGEXP_NOTSPACE:
734 fprintf(output, "notspace "); break;
735 case XML_REGEXP_INITNAME:
736 fprintf(output, "initname "); break;
737 case XML_REGEXP_NOTINITNAME:
738 fprintf(output, "notinitname "); break;
739 case XML_REGEXP_NAMECHAR:
740 fprintf(output, "namechar "); break;
741 case XML_REGEXP_NOTNAMECHAR:
742 fprintf(output, "notnamechar "); break;
743 case XML_REGEXP_DECIMAL:
744 fprintf(output, "decimal "); break;
745 case XML_REGEXP_NOTDECIMAL:
746 fprintf(output, "notdecimal "); break;
747 case XML_REGEXP_REALCHAR:
748 fprintf(output, "realchar "); break;
749 case XML_REGEXP_NOTREALCHAR:
750 fprintf(output, "notrealchar "); break;
751 case XML_REGEXP_LETTER:
752 fprintf(output, "LETTER "); break;
753 case XML_REGEXP_LETTER_UPPERCASE:
754 fprintf(output, "LETTER_UPPERCASE "); break;
755 case XML_REGEXP_LETTER_LOWERCASE:
756 fprintf(output, "LETTER_LOWERCASE "); break;
757 case XML_REGEXP_LETTER_TITLECASE:
758 fprintf(output, "LETTER_TITLECASE "); break;
759 case XML_REGEXP_LETTER_MODIFIER:
760 fprintf(output, "LETTER_MODIFIER "); break;
761 case XML_REGEXP_LETTER_OTHERS:
762 fprintf(output, "LETTER_OTHERS "); break;
763 case XML_REGEXP_MARK:
764 fprintf(output, "MARK "); break;
765 case XML_REGEXP_MARK_NONSPACING:
766 fprintf(output, "MARK_NONSPACING "); break;
767 case XML_REGEXP_MARK_SPACECOMBINING:
768 fprintf(output, "MARK_SPACECOMBINING "); break;
769 case XML_REGEXP_MARK_ENCLOSING:
770 fprintf(output, "MARK_ENCLOSING "); break;
771 case XML_REGEXP_NUMBER:
772 fprintf(output, "NUMBER "); break;
773 case XML_REGEXP_NUMBER_DECIMAL:
774 fprintf(output, "NUMBER_DECIMAL "); break;
775 case XML_REGEXP_NUMBER_LETTER:
776 fprintf(output, "NUMBER_LETTER "); break;
777 case XML_REGEXP_NUMBER_OTHERS:
778 fprintf(output, "NUMBER_OTHERS "); break;
779 case XML_REGEXP_PUNCT:
780 fprintf(output, "PUNCT "); break;
781 case XML_REGEXP_PUNCT_CONNECTOR:
782 fprintf(output, "PUNCT_CONNECTOR "); break;
783 case XML_REGEXP_PUNCT_DASH:
784 fprintf(output, "PUNCT_DASH "); break;
785 case XML_REGEXP_PUNCT_OPEN:
786 fprintf(output, "PUNCT_OPEN "); break;
787 case XML_REGEXP_PUNCT_CLOSE:
788 fprintf(output, "PUNCT_CLOSE "); break;
789 case XML_REGEXP_PUNCT_INITQUOTE:
790 fprintf(output, "PUNCT_INITQUOTE "); break;
791 case XML_REGEXP_PUNCT_FINQUOTE:
792 fprintf(output, "PUNCT_FINQUOTE "); break;
793 case XML_REGEXP_PUNCT_OTHERS:
794 fprintf(output, "PUNCT_OTHERS "); break;
795 case XML_REGEXP_SEPAR:
796 fprintf(output, "SEPAR "); break;
797 case XML_REGEXP_SEPAR_SPACE:
798 fprintf(output, "SEPAR_SPACE "); break;
799 case XML_REGEXP_SEPAR_LINE:
800 fprintf(output, "SEPAR_LINE "); break;
801 case XML_REGEXP_SEPAR_PARA:
802 fprintf(output, "SEPAR_PARA "); break;
803 case XML_REGEXP_SYMBOL:
804 fprintf(output, "SYMBOL "); break;
805 case XML_REGEXP_SYMBOL_MATH:
806 fprintf(output, "SYMBOL_MATH "); break;
807 case XML_REGEXP_SYMBOL_CURRENCY:
808 fprintf(output, "SYMBOL_CURRENCY "); break;
809 case XML_REGEXP_SYMBOL_MODIFIER:
810 fprintf(output, "SYMBOL_MODIFIER "); break;
811 case XML_REGEXP_SYMBOL_OTHERS:
812 fprintf(output, "SYMBOL_OTHERS "); break;
813 case XML_REGEXP_OTHER:
814 fprintf(output, "OTHER "); break;
815 case XML_REGEXP_OTHER_CONTROL:
816 fprintf(output, "OTHER_CONTROL "); break;
817 case XML_REGEXP_OTHER_FORMAT:
818 fprintf(output, "OTHER_FORMAT "); break;
819 case XML_REGEXP_OTHER_PRIVATE:
820 fprintf(output, "OTHER_PRIVATE "); break;
821 case XML_REGEXP_OTHER_NA:
822 fprintf(output, "OTHER_NA "); break;
823 case XML_REGEXP_BLOCK_NAME:
824 fprintf(output, "BLOCK "); break;
828 static void
829 xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
830 switch (type) {
831 case XML_REGEXP_QUANT_EPSILON:
832 fprintf(output, "epsilon "); break;
833 case XML_REGEXP_QUANT_ONCE:
834 fprintf(output, "once "); break;
835 case XML_REGEXP_QUANT_OPT:
836 fprintf(output, "? "); break;
837 case XML_REGEXP_QUANT_MULT:
838 fprintf(output, "* "); break;
839 case XML_REGEXP_QUANT_PLUS:
840 fprintf(output, "+ "); break;
841 case XML_REGEXP_QUANT_RANGE:
842 fprintf(output, "range "); break;
843 case XML_REGEXP_QUANT_ONCEONLY:
844 fprintf(output, "onceonly "); break;
845 case XML_REGEXP_QUANT_ALL:
846 fprintf(output, "all "); break;
849 static void
850 xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
851 fprintf(output, " range: ");
852 if (range->neg)
853 fprintf(output, "negative ");
854 xmlRegPrintAtomType(output, range->type);
855 fprintf(output, "%c - %c\n", range->start, range->end);
858 static void
859 xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
860 fprintf(output, " atom: ");
861 if (atom == NULL) {
862 fprintf(output, "NULL\n");
863 return;
865 xmlRegPrintAtomType(output, atom->type);
866 xmlRegPrintQuantType(output, atom->quant);
867 if (atom->quant == XML_REGEXP_QUANT_RANGE)
868 fprintf(output, "%d-%d ", atom->min, atom->max);
869 if (atom->type == XML_REGEXP_STRING)
870 fprintf(output, "'%s' ", (char *) atom->valuep);
871 if (atom->type == XML_REGEXP_CHARVAL)
872 fprintf(output, "char %c\n", atom->codepoint);
873 else if (atom->type == XML_REGEXP_RANGES) {
874 int i;
875 fprintf(output, "%d entries\n", atom->nbRanges);
876 for (i = 0; i < atom->nbRanges;i++)
877 xmlRegPrintRange(output, atom->ranges[i]);
878 } else if (atom->type == XML_REGEXP_SUBREG) {
879 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
880 } else {
881 fprintf(output, "\n");
885 static void
886 xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
887 fprintf(output, " trans: ");
888 if (trans == NULL) {
889 fprintf(output, "NULL\n");
890 return;
892 if (trans->to < 0) {
893 fprintf(output, "removed\n");
894 return;
896 if (trans->counter >= 0) {
897 fprintf(output, "counted %d, ", trans->counter);
899 if (trans->count == REGEXP_ALL_COUNTER) {
900 fprintf(output, "all transition, ");
901 } else if (trans->count >= 0) {
902 fprintf(output, "count based %d, ", trans->count);
904 if (trans->atom == NULL) {
905 fprintf(output, "epsilon to %d\n", trans->to);
906 return;
908 if (trans->atom->type == XML_REGEXP_CHARVAL)
909 fprintf(output, "char %c ", trans->atom->codepoint);
910 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
913 static void
914 xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
915 int i;
917 fprintf(output, " state: ");
918 if (state == NULL) {
919 fprintf(output, "NULL\n");
920 return;
922 if (state->type == XML_REGEXP_START_STATE)
923 fprintf(output, "START ");
924 if (state->type == XML_REGEXP_FINAL_STATE)
925 fprintf(output, "FINAL ");
927 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
928 for (i = 0;i < state->nbTrans; i++) {
929 xmlRegPrintTrans(output, &(state->trans[i]));
933 #ifdef DEBUG_REGEXP_GRAPH
934 static void
935 xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
936 int i;
938 fprintf(output, " ctxt: ");
939 if (ctxt == NULL) {
940 fprintf(output, "NULL\n");
941 return;
943 fprintf(output, "'%s' ", ctxt->string);
944 if (ctxt->error)
945 fprintf(output, "error ");
946 if (ctxt->neg)
947 fprintf(output, "neg ");
948 fprintf(output, "\n");
949 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
950 for (i = 0;i < ctxt->nbAtoms; i++) {
951 fprintf(output, " %02d ", i);
952 xmlRegPrintAtom(output, ctxt->atoms[i]);
954 if (ctxt->atom != NULL) {
955 fprintf(output, "current atom:\n");
956 xmlRegPrintAtom(output, ctxt->atom);
958 fprintf(output, "%d states:", ctxt->nbStates);
959 if (ctxt->start != NULL)
960 fprintf(output, " start: %d", ctxt->start->no);
961 if (ctxt->end != NULL)
962 fprintf(output, " end: %d", ctxt->end->no);
963 fprintf(output, "\n");
964 for (i = 0;i < ctxt->nbStates; i++) {
965 xmlRegPrintState(output, ctxt->states[i]);
967 fprintf(output, "%d counters:\n", ctxt->nbCounters);
968 for (i = 0;i < ctxt->nbCounters; i++) {
969 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
970 ctxt->counters[i].max);
973 #endif
975 /************************************************************************
977 * Finite Automata structures manipulations *
979 ************************************************************************/
981 static void
982 xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
983 int neg, xmlRegAtomType type, int start, int end,
984 xmlChar *blockName) {
985 xmlRegRangePtr range;
987 if (atom == NULL) {
988 ERROR("add range: atom is NULL");
989 return;
991 if (atom->type != XML_REGEXP_RANGES) {
992 ERROR("add range: atom is not ranges");
993 return;
995 if (atom->maxRanges == 0) {
996 atom->maxRanges = 4;
997 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
998 sizeof(xmlRegRangePtr));
999 if (atom->ranges == NULL) {
1000 ERROR("add range: allocation failed");
1001 atom->maxRanges = 0;
1002 return;
1004 } else if (atom->nbRanges >= atom->maxRanges) {
1005 xmlRegRangePtr *tmp;
1006 atom->maxRanges *= 2;
1007 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1008 sizeof(xmlRegRangePtr));
1009 if (tmp == NULL) {
1010 ERROR("add range: allocation failed");
1011 atom->maxRanges /= 2;
1012 return;
1014 atom->ranges = tmp;
1016 range = xmlRegNewRange(ctxt, neg, type, start, end);
1017 if (range == NULL)
1018 return;
1019 range->blockName = blockName;
1020 atom->ranges[atom->nbRanges++] = range;
1024 static int
1025 xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1026 if (ctxt->maxCounters == 0) {
1027 ctxt->maxCounters = 4;
1028 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1029 sizeof(xmlRegCounter));
1030 if (ctxt->counters == NULL) {
1031 ERROR("reg counter: allocation failed");
1032 ctxt->maxCounters = 0;
1033 return(-1);
1035 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1036 xmlRegCounter *tmp;
1037 ctxt->maxCounters *= 2;
1038 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1039 sizeof(xmlRegCounter));
1040 if (tmp == NULL) {
1041 ERROR("reg counter: allocation failed");
1042 ctxt->maxCounters /= 2;
1043 return(-1);
1045 ctxt->counters = tmp;
1047 ctxt->counters[ctxt->nbCounters].min = -1;
1048 ctxt->counters[ctxt->nbCounters].max = -1;
1049 return(ctxt->nbCounters++);
1052 static void
1053 xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1054 if (atom == NULL) {
1055 ERROR("atom push: atom is NULL");
1056 return;
1058 if (ctxt->maxAtoms == 0) {
1059 ctxt->maxAtoms = 4;
1060 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1061 sizeof(xmlRegAtomPtr));
1062 if (ctxt->atoms == NULL) {
1063 ERROR("atom push: allocation failed");
1064 ctxt->maxAtoms = 0;
1065 return;
1067 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1068 xmlRegAtomPtr *tmp;
1069 ctxt->maxAtoms *= 2;
1070 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1071 sizeof(xmlRegAtomPtr));
1072 if (tmp == NULL) {
1073 ERROR("atom push: allocation failed");
1074 ctxt->maxAtoms /= 2;
1075 return;
1077 ctxt->atoms = tmp;
1079 atom->no = ctxt->nbAtoms;
1080 ctxt->atoms[ctxt->nbAtoms++] = atom;
1083 static void
1084 xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1085 xmlRegAtomPtr atom, xmlRegStatePtr target,
1086 int counter, int count) {
1087 if (state == NULL) {
1088 ERROR("add state: state is NULL");
1089 return;
1091 if (target == NULL) {
1092 ERROR("add state: target is NULL");
1093 return;
1095 if (state->maxTrans == 0) {
1096 state->maxTrans = 4;
1097 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1098 sizeof(xmlRegTrans));
1099 if (state->trans == NULL) {
1100 ERROR("add range: allocation failed");
1101 state->maxTrans = 0;
1102 return;
1104 } else if (state->nbTrans >= state->maxTrans) {
1105 xmlRegTrans *tmp;
1106 state->maxTrans *= 2;
1107 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1108 sizeof(xmlRegTrans));
1109 if (tmp == NULL) {
1110 ERROR("add range: allocation failed");
1111 state->maxTrans /= 2;
1112 return;
1114 state->trans = tmp;
1116 #ifdef DEBUG_REGEXP_GRAPH
1117 printf("Add trans from %d to %d ", state->no, target->no);
1118 if (count == REGEXP_ALL_COUNTER)
1119 printf("all transition");
1120 else if (count >= 0)
1121 printf("count based %d", count);
1122 else if (counter >= 0)
1123 printf("counted %d", counter);
1124 else if (atom == NULL)
1125 printf("epsilon transition");
1126 printf("\n");
1127 #endif
1129 state->trans[state->nbTrans].atom = atom;
1130 state->trans[state->nbTrans].to = target->no;
1131 state->trans[state->nbTrans].counter = counter;
1132 state->trans[state->nbTrans].count = count;
1133 state->nbTrans++;
1136 static void
1137 xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
1138 if (ctxt->maxStates == 0) {
1139 ctxt->maxStates = 4;
1140 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1141 sizeof(xmlRegStatePtr));
1142 if (ctxt->states == NULL) {
1143 ERROR("add range: allocation failed");
1144 ctxt->maxStates = 0;
1145 return;
1147 } else if (ctxt->nbStates >= ctxt->maxStates) {
1148 xmlRegStatePtr *tmp;
1149 ctxt->maxStates *= 2;
1150 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1151 sizeof(xmlRegStatePtr));
1152 if (tmp == NULL) {
1153 ERROR("add range: allocation failed");
1154 ctxt->maxStates /= 2;
1155 return;
1157 ctxt->states = tmp;
1159 state->no = ctxt->nbStates;
1160 ctxt->states[ctxt->nbStates++] = state;
1164 * xmlFAGenerateAllTransition:
1165 * @ctxt: a regexp parser context
1166 * @from: the from state
1167 * @to: the target state or NULL for building a new one
1168 * @lax:
1171 static void
1172 xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
1173 xmlRegStatePtr from, xmlRegStatePtr to,
1174 int lax) {
1175 if (to == NULL) {
1176 to = xmlRegNewState(ctxt);
1177 xmlRegStatePush(ctxt, to);
1178 ctxt->state = to;
1180 if (lax)
1181 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1182 else
1183 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
1187 * xmlFAGenerateEpsilonTransition:
1188 * @ctxt: a regexp parser context
1189 * @from: the from state
1190 * @to: the target state or NULL for building a new one
1193 static void
1194 xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1195 xmlRegStatePtr from, xmlRegStatePtr to) {
1196 if (to == NULL) {
1197 to = xmlRegNewState(ctxt);
1198 xmlRegStatePush(ctxt, to);
1199 ctxt->state = to;
1201 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1205 * xmlFAGenerateCountedEpsilonTransition:
1206 * @ctxt: a regexp parser context
1207 * @from: the from state
1208 * @to: the target state or NULL for building a new one
1209 * counter: the counter for that transition
1212 static void
1213 xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1214 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1215 if (to == NULL) {
1216 to = xmlRegNewState(ctxt);
1217 xmlRegStatePush(ctxt, to);
1218 ctxt->state = to;
1220 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1224 * xmlFAGenerateCountedTransition:
1225 * @ctxt: a regexp parser context
1226 * @from: the from state
1227 * @to: the target state or NULL for building a new one
1228 * counter: the counter for that transition
1231 static void
1232 xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1233 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1234 if (to == NULL) {
1235 to = xmlRegNewState(ctxt);
1236 xmlRegStatePush(ctxt, to);
1237 ctxt->state = to;
1239 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1243 * xmlFAGenerateTransitions:
1244 * @ctxt: a regexp parser context
1245 * @from: the from state
1246 * @to: the target state or NULL for building a new one
1247 * @atom: the atom generating the transition
1250 static void
1251 xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1252 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1253 if (atom == NULL) {
1254 ERROR("genrate transition: atom == NULL");
1255 return;
1257 if (atom->type == XML_REGEXP_SUBREG) {
1259 * this is a subexpression handling one should not need to
1260 * create a new node excep for XML_REGEXP_QUANT_RANGE.
1262 xmlRegAtomPush(ctxt, atom);
1263 if ((to != NULL) && (atom->stop != to) &&
1264 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1266 * Generate an epsilon transition to link to the target
1268 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1270 switch (atom->quant) {
1271 case XML_REGEXP_QUANT_OPT:
1272 atom->quant = XML_REGEXP_QUANT_ONCE;
1273 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1274 break;
1275 case XML_REGEXP_QUANT_MULT:
1276 atom->quant = XML_REGEXP_QUANT_ONCE;
1277 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1278 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1279 break;
1280 case XML_REGEXP_QUANT_PLUS:
1281 atom->quant = XML_REGEXP_QUANT_ONCE;
1282 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1283 break;
1284 case XML_REGEXP_QUANT_RANGE: {
1285 int counter;
1286 xmlRegStatePtr newstate;
1289 * This one is nasty:
1290 * 1/ register a new counter
1291 * 2/ register an epsilon transition associated to
1292 * this counter going from atom->stop to atom->start
1293 * 3/ create a new state
1294 * 4/ generate a counted transition from atom->stop to
1295 * that state
1297 counter = xmlRegGetCounter(ctxt);
1298 ctxt->counters[counter].min = atom->min - 1;
1299 ctxt->counters[counter].max = atom->max - 1;
1300 atom->min = 0;
1301 atom->max = 0;
1302 atom->quant = XML_REGEXP_QUANT_ONCE;
1303 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1304 atom->start, counter);
1305 if (to != NULL) {
1306 newstate = to;
1307 } else {
1308 newstate = xmlRegNewState(ctxt);
1309 xmlRegStatePush(ctxt, newstate);
1310 ctxt->state = newstate;
1312 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1313 newstate, counter);
1315 default:
1316 break;
1318 return;
1319 } else {
1320 if (to == NULL) {
1321 to = xmlRegNewState(ctxt);
1322 xmlRegStatePush(ctxt, to);
1324 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1325 xmlRegAtomPush(ctxt, atom);
1326 ctxt->state = to;
1328 switch (atom->quant) {
1329 case XML_REGEXP_QUANT_OPT:
1330 atom->quant = XML_REGEXP_QUANT_ONCE;
1331 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1332 break;
1333 case XML_REGEXP_QUANT_MULT:
1334 atom->quant = XML_REGEXP_QUANT_ONCE;
1335 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1336 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1337 break;
1338 case XML_REGEXP_QUANT_PLUS:
1339 atom->quant = XML_REGEXP_QUANT_ONCE;
1340 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1341 break;
1342 default:
1343 break;
1348 * xmlFAReduceEpsilonTransitions:
1349 * @ctxt: a regexp parser context
1350 * @fromnr: the from state
1351 * @tonr: the to state
1352 * @cpunter: should that transition be associted to a counted
1355 static void
1356 xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1357 int tonr, int counter) {
1358 int transnr;
1359 xmlRegStatePtr from;
1360 xmlRegStatePtr to;
1362 #ifdef DEBUG_REGEXP_GRAPH
1363 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1364 #endif
1365 from = ctxt->states[fromnr];
1366 if (from == NULL)
1367 return;
1368 to = ctxt->states[tonr];
1369 if (to == NULL)
1370 return;
1371 if ((to->mark == XML_REGEXP_MARK_START) ||
1372 (to->mark == XML_REGEXP_MARK_VISITED))
1373 return;
1375 to->mark = XML_REGEXP_MARK_VISITED;
1376 if (to->type == XML_REGEXP_FINAL_STATE) {
1377 #ifdef DEBUG_REGEXP_GRAPH
1378 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1379 #endif
1380 from->type = XML_REGEXP_FINAL_STATE;
1382 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1383 if (to->trans[transnr].atom == NULL) {
1385 * Don't remove counted transitions
1386 * Don't loop either
1388 if (to->trans[transnr].to != fromnr) {
1389 if (to->trans[transnr].count >= 0) {
1390 int newto = to->trans[transnr].to;
1392 xmlRegStateAddTrans(ctxt, from, NULL,
1393 ctxt->states[newto],
1394 -1, to->trans[transnr].count);
1395 } else {
1396 #ifdef DEBUG_REGEXP_GRAPH
1397 printf("Found epsilon trans %d from %d to %d\n",
1398 transnr, tonr, to->trans[transnr].to);
1399 #endif
1400 if (to->trans[transnr].counter >= 0) {
1401 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1402 to->trans[transnr].to,
1403 to->trans[transnr].counter);
1404 } else {
1405 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1406 to->trans[transnr].to,
1407 counter);
1411 } else {
1412 int newto = to->trans[transnr].to;
1414 if (to->trans[transnr].counter >= 0) {
1415 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1416 ctxt->states[newto],
1417 to->trans[transnr].counter, -1);
1418 } else {
1419 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1420 ctxt->states[newto], counter, -1);
1424 to->mark = XML_REGEXP_MARK_NORMAL;
1428 * xmlFAEliminateEpsilonTransitions:
1429 * @ctxt: a regexp parser context
1432 static void
1433 xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1434 int statenr, transnr;
1435 xmlRegStatePtr state;
1438 * build the completed transitions bypassing the epsilons
1439 * Use a marking algorithm to avoid loops
1441 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1442 state = ctxt->states[statenr];
1443 if (state == NULL)
1444 continue;
1445 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1446 if ((state->trans[transnr].atom == NULL) &&
1447 (state->trans[transnr].to >= 0)) {
1448 if (state->trans[transnr].to == statenr) {
1449 state->trans[transnr].to = -1;
1450 #ifdef DEBUG_REGEXP_GRAPH
1451 printf("Removed loopback epsilon trans %d on %d\n",
1452 transnr, statenr);
1453 #endif
1454 } else if (state->trans[transnr].count < 0) {
1455 int newto = state->trans[transnr].to;
1457 #ifdef DEBUG_REGEXP_GRAPH
1458 printf("Found epsilon trans %d from %d to %d\n",
1459 transnr, statenr, newto);
1460 #endif
1461 state->mark = XML_REGEXP_MARK_START;
1462 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1463 newto, state->trans[transnr].counter);
1464 state->mark = XML_REGEXP_MARK_NORMAL;
1465 #ifdef DEBUG_REGEXP_GRAPH
1466 } else {
1467 printf("Found counted transition %d on %d\n",
1468 transnr, statenr);
1469 #endif
1475 * Eliminate the epsilon transitions
1477 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1478 state = ctxt->states[statenr];
1479 if (state == NULL)
1480 continue;
1481 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1482 if ((state->trans[transnr].atom == NULL) &&
1483 (state->trans[transnr].count < 0) &&
1484 (state->trans[transnr].to >= 0)) {
1485 state->trans[transnr].to = -1;
1491 * Use this pass to detect unreachable states too
1493 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1494 state = ctxt->states[statenr];
1495 if (state != NULL)
1496 state->reached = 0;
1498 state = ctxt->states[0];
1499 if (state != NULL)
1500 state->reached = 1;
1501 while (state != NULL) {
1502 xmlRegStatePtr target = NULL;
1503 state->reached = 2;
1505 * Mark all state reachable from the current reachable state
1507 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1508 if ((state->trans[transnr].to >= 0) &&
1509 ((state->trans[transnr].atom != NULL) ||
1510 (state->trans[transnr].count >= 0))) {
1511 int newto = state->trans[transnr].to;
1513 if (ctxt->states[newto] == NULL)
1514 continue;
1515 if (ctxt->states[newto]->reached == 0) {
1516 ctxt->states[newto]->reached = 1;
1517 target = ctxt->states[newto];
1522 * find the next accessible state not explored
1524 if (target == NULL) {
1525 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1526 state = ctxt->states[statenr];
1527 if ((state != NULL) && (state->reached == 1)) {
1528 target = state;
1529 break;
1533 state = target;
1535 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1536 state = ctxt->states[statenr];
1537 if ((state != NULL) && (state->reached == 0)) {
1538 #ifdef DEBUG_REGEXP_GRAPH
1539 printf("Removed unreachable state %d\n", statenr);
1540 #endif
1541 xmlRegFreeState(state);
1542 ctxt->states[statenr] = NULL;
1549 * xmlFACompareAtoms:
1550 * @atom1: an atom
1551 * @atom2: an atom
1553 * Compares two atoms to check whether they are equivatents
1555 * Returns 1 if yes and 0 otherwise
1557 static int
1558 xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1559 if (atom1 == atom2)
1560 return(1);
1561 if ((atom1 == NULL) || (atom2 == NULL))
1562 return(0);
1564 if (atom1->type != atom2->type)
1565 return(0);
1566 switch (atom1->type) {
1567 case XML_REGEXP_STRING:
1568 return(xmlStrEqual((xmlChar *)atom1->valuep,
1569 (xmlChar *)atom2->valuep));
1570 case XML_REGEXP_EPSILON:
1571 return(1);
1572 case XML_REGEXP_CHARVAL:
1573 return(atom1->codepoint == atom2->codepoint);
1574 case XML_REGEXP_RANGES:
1575 TODO;
1576 return(0);
1577 default:
1578 break;
1580 return(1);
1584 * xmlFARecurseDeterminism:
1585 * @ctxt: a regexp parser context
1587 * Check whether the associated regexp is determinist,
1588 * should be called after xmlFAEliminateEpsilonTransitions()
1591 static int
1592 xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1593 int to, xmlRegAtomPtr atom) {
1594 int ret = 1;
1595 int transnr;
1596 xmlRegTransPtr t1;
1598 if (state == NULL)
1599 return(ret);
1600 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1601 t1 = &(state->trans[transnr]);
1603 * check transitions conflicting with the one looked at
1605 if (t1->atom == NULL) {
1606 if (t1->to == -1)
1607 continue;
1608 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1609 to, atom);
1610 if (ret == 0)
1611 return(0);
1612 continue;
1614 if (t1->to != to)
1615 continue;
1616 if (xmlFACompareAtoms(t1->atom, atom))
1617 return(0);
1619 return(ret);
1623 * xmlFAComputesDeterminism:
1624 * @ctxt: a regexp parser context
1626 * Check whether the associated regexp is determinist,
1627 * should be called after xmlFAEliminateEpsilonTransitions()
1630 static int
1631 xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1632 int statenr, transnr;
1633 xmlRegStatePtr state;
1634 xmlRegTransPtr t1, t2;
1635 int i;
1636 int ret = 1;
1638 #ifdef DEBUG_REGEXP_GRAPH
1639 printf("xmlFAComputesDeterminism\n");
1640 xmlRegPrintCtxt(stdout, ctxt);
1641 #endif
1642 if (ctxt->determinist != -1)
1643 return(ctxt->determinist);
1646 * Check for all states that there isn't 2 transitions
1647 * with the same atom and a different target.
1649 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1650 state = ctxt->states[statenr];
1651 if (state == NULL)
1652 continue;
1653 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1654 t1 = &(state->trans[transnr]);
1656 * Determinism checks in case of counted or all transitions
1657 * will have to be handled separately
1659 if (t1->atom == NULL)
1660 continue;
1661 if (t1->to == -1) /* eliminated */
1662 continue;
1663 for (i = 0;i < transnr;i++) {
1664 t2 = &(state->trans[i]);
1665 if (t2->to == -1) /* eliminated */
1666 continue;
1667 if (t2->atom != NULL) {
1668 if (t1->to == t2->to) {
1669 if (xmlFACompareAtoms(t1->atom, t2->atom))
1670 t2->to = -1; /* eliminate */
1671 } else {
1672 /* not determinist ! */
1673 if (xmlFACompareAtoms(t1->atom, t2->atom))
1674 ret = 0;
1676 } else if (t1->to != -1) {
1678 * do the closure in case of remaining specific
1679 * epsilon transitions like choices or all
1681 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1682 t2->to, t2->atom);
1683 if (ret == 0)
1684 return(0);
1687 if (ret == 0)
1688 break;
1690 if (ret == 0)
1691 break;
1693 ctxt->determinist = ret;
1694 return(ret);
1697 /************************************************************************
1699 * Routines to check input against transition atoms *
1701 ************************************************************************/
1703 static int
1704 xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1705 int start, int end, const xmlChar *blockName) {
1706 int ret = 0;
1708 switch (type) {
1709 case XML_REGEXP_STRING:
1710 case XML_REGEXP_SUBREG:
1711 case XML_REGEXP_RANGES:
1712 case XML_REGEXP_EPSILON:
1713 return(-1);
1714 case XML_REGEXP_ANYCHAR:
1715 ret = ((codepoint != '\n') && (codepoint != '\r'));
1716 break;
1717 case XML_REGEXP_CHARVAL:
1718 ret = ((codepoint >= start) && (codepoint <= end));
1719 break;
1720 case XML_REGEXP_NOTSPACE:
1721 neg = !neg;
1722 case XML_REGEXP_ANYSPACE:
1723 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1724 (codepoint == '\t') || (codepoint == ' '));
1725 break;
1726 case XML_REGEXP_NOTINITNAME:
1727 neg = !neg;
1728 case XML_REGEXP_INITNAME:
1729 ret = (xmlIsLetter(codepoint) ||
1730 (codepoint == '_') || (codepoint == ':'));
1731 break;
1732 case XML_REGEXP_NOTNAMECHAR:
1733 neg = !neg;
1734 case XML_REGEXP_NAMECHAR:
1735 ret = (xmlIsLetter(codepoint) || xmlIsDigit(codepoint) ||
1736 (codepoint == '.') || (codepoint == '-') ||
1737 (codepoint == '_') || (codepoint == ':') ||
1738 xmlIsCombining(codepoint) || xmlIsExtender(codepoint));
1739 break;
1740 case XML_REGEXP_NOTDECIMAL:
1741 neg = !neg;
1742 case XML_REGEXP_DECIMAL:
1743 ret = xmlUCSIsCatNd(codepoint);
1744 break;
1745 case XML_REGEXP_REALCHAR:
1746 neg = !neg;
1747 case XML_REGEXP_NOTREALCHAR:
1748 ret = xmlUCSIsCatP(codepoint);
1749 if (ret == 0)
1750 ret = xmlUCSIsCatZ(codepoint);
1751 if (ret == 0)
1752 ret = xmlUCSIsCatC(codepoint);
1753 break;
1754 case XML_REGEXP_LETTER:
1755 ret = xmlUCSIsCatL(codepoint);
1756 break;
1757 case XML_REGEXP_LETTER_UPPERCASE:
1758 ret = xmlUCSIsCatLu(codepoint);
1759 break;
1760 case XML_REGEXP_LETTER_LOWERCASE:
1761 ret = xmlUCSIsCatLl(codepoint);
1762 break;
1763 case XML_REGEXP_LETTER_TITLECASE:
1764 ret = xmlUCSIsCatLt(codepoint);
1765 break;
1766 case XML_REGEXP_LETTER_MODIFIER:
1767 ret = xmlUCSIsCatLm(codepoint);
1768 break;
1769 case XML_REGEXP_LETTER_OTHERS:
1770 ret = xmlUCSIsCatLo(codepoint);
1771 break;
1772 case XML_REGEXP_MARK:
1773 ret = xmlUCSIsCatM(codepoint);
1774 break;
1775 case XML_REGEXP_MARK_NONSPACING:
1776 ret = xmlUCSIsCatMn(codepoint);
1777 break;
1778 case XML_REGEXP_MARK_SPACECOMBINING:
1779 ret = xmlUCSIsCatMc(codepoint);
1780 break;
1781 case XML_REGEXP_MARK_ENCLOSING:
1782 ret = xmlUCSIsCatMe(codepoint);
1783 break;
1784 case XML_REGEXP_NUMBER:
1785 ret = xmlUCSIsCatN(codepoint);
1786 break;
1787 case XML_REGEXP_NUMBER_DECIMAL:
1788 ret = xmlUCSIsCatNd(codepoint);
1789 break;
1790 case XML_REGEXP_NUMBER_LETTER:
1791 ret = xmlUCSIsCatNl(codepoint);
1792 break;
1793 case XML_REGEXP_NUMBER_OTHERS:
1794 ret = xmlUCSIsCatNo(codepoint);
1795 break;
1796 case XML_REGEXP_PUNCT:
1797 ret = xmlUCSIsCatP(codepoint);
1798 break;
1799 case XML_REGEXP_PUNCT_CONNECTOR:
1800 ret = xmlUCSIsCatPc(codepoint);
1801 break;
1802 case XML_REGEXP_PUNCT_DASH:
1803 ret = xmlUCSIsCatPd(codepoint);
1804 break;
1805 case XML_REGEXP_PUNCT_OPEN:
1806 ret = xmlUCSIsCatPs(codepoint);
1807 break;
1808 case XML_REGEXP_PUNCT_CLOSE:
1809 ret = xmlUCSIsCatPe(codepoint);
1810 break;
1811 case XML_REGEXP_PUNCT_INITQUOTE:
1812 ret = xmlUCSIsCatPi(codepoint);
1813 break;
1814 case XML_REGEXP_PUNCT_FINQUOTE:
1815 ret = xmlUCSIsCatPf(codepoint);
1816 break;
1817 case XML_REGEXP_PUNCT_OTHERS:
1818 ret = xmlUCSIsCatPo(codepoint);
1819 break;
1820 case XML_REGEXP_SEPAR:
1821 ret = xmlUCSIsCatZ(codepoint);
1822 break;
1823 case XML_REGEXP_SEPAR_SPACE:
1824 ret = xmlUCSIsCatZs(codepoint);
1825 break;
1826 case XML_REGEXP_SEPAR_LINE:
1827 ret = xmlUCSIsCatZl(codepoint);
1828 break;
1829 case XML_REGEXP_SEPAR_PARA:
1830 ret = xmlUCSIsCatZp(codepoint);
1831 break;
1832 case XML_REGEXP_SYMBOL:
1833 ret = xmlUCSIsCatS(codepoint);
1834 break;
1835 case XML_REGEXP_SYMBOL_MATH:
1836 ret = xmlUCSIsCatSm(codepoint);
1837 break;
1838 case XML_REGEXP_SYMBOL_CURRENCY:
1839 ret = xmlUCSIsCatSc(codepoint);
1840 break;
1841 case XML_REGEXP_SYMBOL_MODIFIER:
1842 ret = xmlUCSIsCatSk(codepoint);
1843 break;
1844 case XML_REGEXP_SYMBOL_OTHERS:
1845 ret = xmlUCSIsCatSo(codepoint);
1846 break;
1847 case XML_REGEXP_OTHER:
1848 ret = xmlUCSIsCatC(codepoint);
1849 break;
1850 case XML_REGEXP_OTHER_CONTROL:
1851 ret = xmlUCSIsCatCc(codepoint);
1852 break;
1853 case XML_REGEXP_OTHER_FORMAT:
1854 ret = xmlUCSIsCatCf(codepoint);
1855 break;
1856 case XML_REGEXP_OTHER_PRIVATE:
1857 ret = xmlUCSIsCatCo(codepoint);
1858 break;
1859 case XML_REGEXP_OTHER_NA:
1860 /* ret = xmlUCSIsCatCn(codepoint); */
1861 /* Seems it doesn't exist anymore in recent Unicode releases */
1862 ret = 0;
1863 break;
1864 case XML_REGEXP_BLOCK_NAME:
1865 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
1866 break;
1868 if (neg)
1869 return(!ret);
1870 return(ret);
1873 static int
1874 xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
1875 int i, ret = 0;
1876 xmlRegRangePtr range;
1878 if ((atom == NULL) || (!xmlIsChar(codepoint)))
1879 return(-1);
1881 switch (atom->type) {
1882 case XML_REGEXP_SUBREG:
1883 case XML_REGEXP_EPSILON:
1884 return(-1);
1885 case XML_REGEXP_CHARVAL:
1886 return(codepoint == atom->codepoint);
1887 case XML_REGEXP_RANGES: {
1888 int accept = 0;
1889 for (i = 0;i < atom->nbRanges;i++) {
1890 range = atom->ranges[i];
1891 if (range->neg) {
1892 ret = xmlRegCheckCharacterRange(range->type, codepoint,
1893 0, range->start, range->end,
1894 range->blockName);
1895 if (ret != 0)
1896 return(0); /* excluded char */
1897 } else {
1898 ret = xmlRegCheckCharacterRange(range->type, codepoint,
1899 0, range->start, range->end,
1900 range->blockName);
1901 if (ret != 0)
1902 accept = 1; /* might still be excluded */
1905 return(accept);
1907 case XML_REGEXP_STRING:
1908 printf("TODO: XML_REGEXP_STRING\n");
1909 return(-1);
1910 case XML_REGEXP_ANYCHAR:
1911 case XML_REGEXP_ANYSPACE:
1912 case XML_REGEXP_NOTSPACE:
1913 case XML_REGEXP_INITNAME:
1914 case XML_REGEXP_NOTINITNAME:
1915 case XML_REGEXP_NAMECHAR:
1916 case XML_REGEXP_NOTNAMECHAR:
1917 case XML_REGEXP_DECIMAL:
1918 case XML_REGEXP_NOTDECIMAL:
1919 case XML_REGEXP_REALCHAR:
1920 case XML_REGEXP_NOTREALCHAR:
1921 case XML_REGEXP_LETTER:
1922 case XML_REGEXP_LETTER_UPPERCASE:
1923 case XML_REGEXP_LETTER_LOWERCASE:
1924 case XML_REGEXP_LETTER_TITLECASE:
1925 case XML_REGEXP_LETTER_MODIFIER:
1926 case XML_REGEXP_LETTER_OTHERS:
1927 case XML_REGEXP_MARK:
1928 case XML_REGEXP_MARK_NONSPACING:
1929 case XML_REGEXP_MARK_SPACECOMBINING:
1930 case XML_REGEXP_MARK_ENCLOSING:
1931 case XML_REGEXP_NUMBER:
1932 case XML_REGEXP_NUMBER_DECIMAL:
1933 case XML_REGEXP_NUMBER_LETTER:
1934 case XML_REGEXP_NUMBER_OTHERS:
1935 case XML_REGEXP_PUNCT:
1936 case XML_REGEXP_PUNCT_CONNECTOR:
1937 case XML_REGEXP_PUNCT_DASH:
1938 case XML_REGEXP_PUNCT_OPEN:
1939 case XML_REGEXP_PUNCT_CLOSE:
1940 case XML_REGEXP_PUNCT_INITQUOTE:
1941 case XML_REGEXP_PUNCT_FINQUOTE:
1942 case XML_REGEXP_PUNCT_OTHERS:
1943 case XML_REGEXP_SEPAR:
1944 case XML_REGEXP_SEPAR_SPACE:
1945 case XML_REGEXP_SEPAR_LINE:
1946 case XML_REGEXP_SEPAR_PARA:
1947 case XML_REGEXP_SYMBOL:
1948 case XML_REGEXP_SYMBOL_MATH:
1949 case XML_REGEXP_SYMBOL_CURRENCY:
1950 case XML_REGEXP_SYMBOL_MODIFIER:
1951 case XML_REGEXP_SYMBOL_OTHERS:
1952 case XML_REGEXP_OTHER:
1953 case XML_REGEXP_OTHER_CONTROL:
1954 case XML_REGEXP_OTHER_FORMAT:
1955 case XML_REGEXP_OTHER_PRIVATE:
1956 case XML_REGEXP_OTHER_NA:
1957 case XML_REGEXP_BLOCK_NAME:
1958 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
1959 (const xmlChar *)atom->valuep);
1960 if (atom->neg)
1961 ret = !ret;
1962 break;
1964 return(ret);
1967 /************************************************************************
1969 * Saving an restoring state of an execution context *
1971 ************************************************************************/
1973 #ifdef DEBUG_REGEXP_EXEC
1974 static void
1975 xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
1976 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
1977 if (exec->inputStack != NULL) {
1978 int i;
1979 printf(": ");
1980 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
1981 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
1982 } else {
1983 printf(": %s", &(exec->inputString[exec->index]));
1985 printf("\n");
1987 #endif
1989 static void
1990 xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
1991 #ifdef DEBUG_REGEXP_EXEC
1992 printf("saving ");
1993 exec->transno++;
1994 xmlFARegDebugExec(exec);
1995 exec->transno--;
1996 #endif
1998 if (exec->maxRollbacks == 0) {
1999 exec->maxRollbacks = 4;
2000 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2001 sizeof(xmlRegExecRollback));
2002 if (exec->rollbacks == NULL) {
2003 fprintf(stderr, "exec save: allocation failed");
2004 exec->maxRollbacks = 0;
2005 return;
2007 memset(exec->rollbacks, 0,
2008 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2009 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2010 xmlRegExecRollback *tmp;
2011 int len = exec->maxRollbacks;
2013 exec->maxRollbacks *= 2;
2014 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2015 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2016 if (tmp == NULL) {
2017 fprintf(stderr, "exec save: allocation failed");
2018 exec->maxRollbacks /= 2;
2019 return;
2021 exec->rollbacks = tmp;
2022 tmp = &exec->rollbacks[len];
2023 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2025 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2026 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2027 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2028 if (exec->comp->nbCounters > 0) {
2029 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2030 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2031 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2032 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2033 fprintf(stderr, "exec save: allocation failed");
2034 exec->status = -5;
2035 return;
2038 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2039 exec->comp->nbCounters * sizeof(int));
2041 exec->nbRollbacks++;
2044 static void
2045 xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2046 if (exec->nbRollbacks <= 0) {
2047 exec->status = -1;
2048 #ifdef DEBUG_REGEXP_EXEC
2049 printf("rollback failed on empty stack\n");
2050 #endif
2051 return;
2053 exec->nbRollbacks--;
2054 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2055 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2056 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2057 if (exec->comp->nbCounters > 0) {
2058 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2059 fprintf(stderr, "exec save: allocation failed");
2060 exec->status = -6;
2061 return;
2063 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2064 exec->comp->nbCounters * sizeof(int));
2067 #ifdef DEBUG_REGEXP_EXEC
2068 printf("restored ");
2069 xmlFARegDebugExec(exec);
2070 #endif
2073 /************************************************************************
2075 * Verifyer, running an input against a compiled regexp *
2077 ************************************************************************/
2079 static int
2080 xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2081 xmlRegExecCtxt execval;
2082 xmlRegExecCtxtPtr exec = &execval;
2083 int ret, codepoint, len;
2085 exec->inputString = content;
2086 exec->index = 0;
2087 exec->determinist = 1;
2088 exec->maxRollbacks = 0;
2089 exec->nbRollbacks = 0;
2090 exec->rollbacks = NULL;
2091 exec->status = 0;
2092 exec->comp = comp;
2093 exec->state = comp->states[0];
2094 exec->transno = 0;
2095 exec->transcount = 0;
2096 if (comp->nbCounters > 0) {
2097 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2098 if (exec->counts == NULL)
2099 return(-1);
2100 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2101 } else
2102 exec->counts = NULL;
2103 while ((exec->status == 0) &&
2104 ((exec->inputString[exec->index] != 0) ||
2105 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2106 xmlRegTransPtr trans;
2107 xmlRegAtomPtr atom;
2110 * End of input on non-terminal state, rollback, however we may
2111 * still have epsilon like transition for counted transitions
2112 * on counters, in that case don't break too early.
2114 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2115 goto rollback;
2117 exec->transcount = 0;
2118 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2119 trans = &exec->state->trans[exec->transno];
2120 if (trans->to < 0)
2121 continue;
2122 atom = trans->atom;
2123 ret = 0;
2124 if (trans->count >= 0) {
2125 int count;
2126 xmlRegCounterPtr counter;
2129 * A counted transition.
2132 count = exec->counts[trans->count];
2133 counter = &exec->comp->counters[trans->count];
2134 #ifdef DEBUG_REGEXP_EXEC
2135 printf("testing count %d: val %d, min %d, max %d\n",
2136 trans->count, count, counter->min, counter->max);
2137 #endif
2138 ret = ((count >= counter->min) && (count <= counter->max));
2139 } else if (atom == NULL) {
2140 fprintf(stderr, "epsilon transition left at runtime\n");
2141 exec->status = -2;
2142 break;
2143 } else if (exec->inputString[exec->index] != 0) {
2144 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2145 ret = xmlRegCheckCharacter(atom, codepoint);
2146 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2147 xmlRegStatePtr to = comp->states[trans->to];
2150 * this is a multiple input sequence
2152 if (exec->state->nbTrans > exec->transno + 1) {
2153 xmlFARegExecSave(exec);
2155 exec->transcount = 1;
2156 do {
2158 * Try to progress as much as possible on the input
2160 if (exec->transcount == atom->max) {
2161 break;
2163 exec->index += len;
2165 * End of input: stop here
2167 if (exec->inputString[exec->index] == 0) {
2168 exec->index -= len;
2169 break;
2171 if (exec->transcount >= atom->min) {
2172 int transno = exec->transno;
2173 xmlRegStatePtr state = exec->state;
2176 * The transition is acceptable save it
2178 exec->transno = -1; /* trick */
2179 exec->state = to;
2180 xmlFARegExecSave(exec);
2181 exec->transno = transno;
2182 exec->state = state;
2184 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2185 len);
2186 ret = xmlRegCheckCharacter(atom, codepoint);
2187 exec->transcount++;
2188 } while (ret == 1);
2189 if (exec->transcount < atom->min)
2190 ret = 0;
2193 * If the last check failed but one transition was found
2194 * possible, rollback
2196 if (ret < 0)
2197 ret = 0;
2198 if (ret == 0) {
2199 goto rollback;
2203 if (ret == 1) {
2204 if (exec->state->nbTrans > exec->transno + 1) {
2205 xmlFARegExecSave(exec);
2207 if (trans->counter >= 0) {
2208 #ifdef DEBUG_REGEXP_EXEC
2209 printf("Increasing count %d\n", trans->counter);
2210 #endif
2211 exec->counts[trans->counter]++;
2213 #ifdef DEBUG_REGEXP_EXEC
2214 printf("entering state %d\n", trans->to);
2215 #endif
2216 exec->state = comp->states[trans->to];
2217 exec->transno = 0;
2218 if (trans->atom != NULL) {
2219 exec->index += len;
2221 goto progress;
2222 } else if (ret < 0) {
2223 exec->status = -4;
2224 break;
2227 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2228 rollback:
2230 * Failed to find a way out
2232 exec->determinist = 0;
2233 xmlFARegExecRollBack(exec);
2235 progress:
2236 continue;
2238 if (exec->rollbacks != NULL) {
2239 if (exec->counts != NULL) {
2240 int i;
2242 for (i = 0;i < exec->maxRollbacks;i++)
2243 if (exec->rollbacks[i].counts != NULL)
2244 xmlFree(exec->rollbacks[i].counts);
2246 xmlFree(exec->rollbacks);
2248 if (exec->counts != NULL)
2249 xmlFree(exec->counts);
2250 if (exec->status == 0)
2251 return(1);
2252 if (exec->status == -1)
2253 return(0);
2254 return(exec->status);
2257 /************************************************************************
2259 * Progressive interface to the verifyer one atom at a time *
2261 ************************************************************************/
2264 * xmlRegExecCtxtPtr:
2265 * @comp: a precompiled regular expression
2266 * @callback: a callback function used for handling progresses in the
2267 * automata matching phase
2268 * @data: the context data associated to the callback in this context
2270 * Build a context used for progressive evaluation of a regexp.
2272 xmlRegExecCtxtPtr
2273 xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2274 xmlRegExecCtxtPtr exec;
2276 if (comp == NULL)
2277 return(NULL);
2278 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2279 if (exec == NULL) {
2280 return(NULL);
2282 memset(exec, 0, sizeof(xmlRegExecCtxt));
2283 exec->inputString = NULL;
2284 exec->index = 0;
2285 exec->determinist = 1;
2286 exec->maxRollbacks = 0;
2287 exec->nbRollbacks = 0;
2288 exec->rollbacks = NULL;
2289 exec->status = 0;
2290 exec->comp = comp;
2291 if (comp->compact == NULL)
2292 exec->state = comp->states[0];
2293 exec->transno = 0;
2294 exec->transcount = 0;
2295 exec->callback = callback;
2296 exec->data = data;
2297 if (comp->nbCounters > 0) {
2298 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2299 if (exec->counts == NULL) {
2300 xmlFree(exec);
2301 return(NULL);
2303 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2304 } else
2305 exec->counts = NULL;
2306 exec->inputStackMax = 0;
2307 exec->inputStackNr = 0;
2308 exec->inputStack = NULL;
2309 return(exec);
2313 * xmlRegFreeExecCtxt:
2314 * @exec: a regular expression evaulation context
2316 * Free the structures associated to a regular expression evaulation context.
2318 void
2319 xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2320 if (exec == NULL)
2321 return;
2323 if (exec->rollbacks != NULL) {
2324 if (exec->counts != NULL) {
2325 int i;
2327 for (i = 0;i < exec->maxRollbacks;i++)
2328 if (exec->rollbacks[i].counts != NULL)
2329 xmlFree(exec->rollbacks[i].counts);
2331 xmlFree(exec->rollbacks);
2333 if (exec->counts != NULL)
2334 xmlFree(exec->counts);
2335 if (exec->inputStack != NULL) {
2336 int i;
2338 for (i = 0;i < exec->inputStackNr;i++) {
2339 if (exec->inputStack[i].value != NULL)
2340 xmlFree(exec->inputStack[i].value);
2342 xmlFree(exec->inputStack);
2344 xmlFree(exec);
2347 static void
2348 xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2349 void *data) {
2350 #ifdef DEBUG_PUSH
2351 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2352 #endif
2353 if (exec->inputStackMax == 0) {
2354 exec->inputStackMax = 4;
2355 exec->inputStack = (xmlRegInputTokenPtr)
2356 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2357 if (exec->inputStack == NULL) {
2358 fprintf(stderr, "push input: allocation failed");
2359 exec->inputStackMax = 0;
2360 return;
2362 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2363 xmlRegInputTokenPtr tmp;
2365 exec->inputStackMax *= 2;
2366 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2367 exec->inputStackMax * sizeof(xmlRegInputToken));
2368 if (tmp == NULL) {
2369 fprintf(stderr, "push input: allocation failed");
2370 exec->inputStackMax /= 2;
2371 return;
2373 exec->inputStack = tmp;
2375 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2376 exec->inputStack[exec->inputStackNr].data = data;
2377 exec->inputStackNr++;
2378 exec->inputStack[exec->inputStackNr].value = NULL;
2379 exec->inputStack[exec->inputStackNr].data = NULL;
2384 * xmlRegCompactPushString:
2385 * @exec: a regexp execution context
2386 * @comp: the precompiled exec with a compact table
2387 * @value: a string token input
2388 * @data: data associated to the token to reuse in callbacks
2390 * Push one input token in the execution context
2392 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2393 * a negative value in case of error.
2395 static int
2396 xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2397 xmlRegexpPtr comp,
2398 const xmlChar *value,
2399 void *data) {
2400 int state = exec->index;
2401 int i, target;
2403 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2404 return(-1);
2406 if (value == NULL) {
2408 * are we at a final state ?
2410 if (comp->compact[state * (comp->nbstrings + 1)] ==
2411 XML_REGEXP_FINAL_STATE)
2412 return(1);
2413 return(0);
2416 #ifdef DEBUG_PUSH
2417 printf("value pushed: %s\n", value);
2418 #endif
2421 * Examine all outside transition from current state
2423 for (i = 0;i < comp->nbstrings;i++) {
2424 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2425 if ((target > 0) && (target <= comp->nbstates)) {
2426 target--; /* to avoid 0 */
2427 if (xmlStrEqual(comp->stringMap[i], value)) {
2428 exec->index = target;
2429 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2430 exec->callback(exec->data, value,
2431 comp->transdata[state * comp->nbstrings + i], data);
2433 #ifdef DEBUG_PUSH
2434 printf("entering state %d\n", target);
2435 #endif
2436 if (comp->compact[target * (comp->nbstrings + 1)] ==
2437 XML_REGEXP_FINAL_STATE)
2438 return(1);
2439 return(0);
2444 * Failed to find an exit transition out from current state for the
2445 * current token
2447 #ifdef DEBUG_PUSH
2448 printf("failed to find a transition for %s on state %d\n", value, state);
2449 #endif
2450 exec->status = -1;
2451 return(-1);
2455 * xmlRegExecPushString:
2456 * @exec: a regexp execution context
2457 * @value: a string token input
2458 * @data: data associated to the token to reuse in callbacks
2460 * Push one input token in the execution context
2462 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2463 * a negative value in case of error.
2466 xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2467 void *data) {
2468 xmlRegTransPtr trans;
2469 xmlRegAtomPtr atom;
2470 int ret;
2471 int final = 0;
2473 if (exec == NULL)
2474 return(-1);
2475 if (exec->comp == NULL)
2476 return(-1);
2477 if (exec->status != 0)
2478 return(exec->status);
2480 if (exec->comp->compact != NULL)
2481 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2483 if (value == NULL) {
2484 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2485 return(1);
2486 final = 1;
2489 #ifdef DEBUG_PUSH
2490 printf("value pushed: %s\n", value);
2491 #endif
2493 * If we have an active rollback stack push the new value there
2494 * and get back to where we were left
2496 if ((value != NULL) && (exec->inputStackNr > 0)) {
2497 xmlFARegExecSaveInputString(exec, value, data);
2498 value = exec->inputStack[exec->index].value;
2499 data = exec->inputStack[exec->index].data;
2500 #ifdef DEBUG_PUSH
2501 printf("value loaded: %s\n", value);
2502 #endif
2505 while ((exec->status == 0) &&
2506 ((value != NULL) ||
2507 ((final == 1) &&
2508 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2511 * End of input on non-terminal state, rollback, however we may
2512 * still have epsilon like transition for counted transitions
2513 * on counters, in that case don't break too early.
2515 if ((value == NULL) && (exec->counts == NULL))
2516 goto rollback;
2518 exec->transcount = 0;
2519 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2520 trans = &exec->state->trans[exec->transno];
2521 if (trans->to < 0)
2522 continue;
2523 atom = trans->atom;
2524 ret = 0;
2525 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2526 int i;
2527 int count;
2528 xmlRegTransPtr t;
2529 xmlRegCounterPtr counter;
2531 ret = 0;
2533 #ifdef DEBUG_PUSH
2534 printf("testing all lax %d\n", trans->count);
2535 #endif
2537 * Check all counted transitions from the current state
2539 if ((value == NULL) && (final)) {
2540 ret = 1;
2541 } else if (value != NULL) {
2542 for (i = 0;i < exec->state->nbTrans;i++) {
2543 t = &exec->state->trans[i];
2544 if ((t->counter < 0) || (t == trans))
2545 continue;
2546 counter = &exec->comp->counters[t->counter];
2547 count = exec->counts[t->counter];
2548 if ((count < counter->max) &&
2549 (t->atom != NULL) &&
2550 (xmlStrEqual(value, t->atom->valuep))) {
2551 ret = 0;
2552 break;
2554 if ((count >= counter->min) &&
2555 (count < counter->max) &&
2556 (xmlStrEqual(value, t->atom->valuep))) {
2557 ret = 1;
2558 break;
2562 } else if (trans->count == REGEXP_ALL_COUNTER) {
2563 int i;
2564 int count;
2565 xmlRegTransPtr t;
2566 xmlRegCounterPtr counter;
2568 ret = 1;
2570 #ifdef DEBUG_PUSH
2571 printf("testing all %d\n", trans->count);
2572 #endif
2574 * Check all counted transitions from the current state
2576 for (i = 0;i < exec->state->nbTrans;i++) {
2577 t = &exec->state->trans[i];
2578 if ((t->counter < 0) || (t == trans))
2579 continue;
2580 counter = &exec->comp->counters[t->counter];
2581 count = exec->counts[t->counter];
2582 if ((count < counter->min) || (count > counter->max)) {
2583 ret = 0;
2584 break;
2587 } else if (trans->count >= 0) {
2588 int count;
2589 xmlRegCounterPtr counter;
2592 * A counted transition.
2595 count = exec->counts[trans->count];
2596 counter = &exec->comp->counters[trans->count];
2597 #ifdef DEBUG_PUSH
2598 printf("testing count %d: val %d, min %d, max %d\n",
2599 trans->count, count, counter->min, counter->max);
2600 #endif
2601 ret = ((count >= counter->min) && (count <= counter->max));
2602 } else if (atom == NULL) {
2603 fprintf(stderr, "epsilon transition left at runtime\n");
2604 exec->status = -2;
2605 break;
2606 } else if (value != NULL) {
2607 ret = xmlStrEqual(value, atom->valuep);
2608 if ((ret == 1) && (trans->counter >= 0)) {
2609 xmlRegCounterPtr counter;
2610 int count;
2612 count = exec->counts[trans->counter];
2613 counter = &exec->comp->counters[trans->counter];
2614 if (count >= counter->max)
2615 ret = 0;
2618 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2619 xmlRegStatePtr to = exec->comp->states[trans->to];
2622 * this is a multiple input sequence
2624 if (exec->state->nbTrans > exec->transno + 1) {
2625 if (exec->inputStackNr <= 0) {
2626 xmlFARegExecSaveInputString(exec, value, data);
2628 xmlFARegExecSave(exec);
2630 exec->transcount = 1;
2631 do {
2633 * Try to progress as much as possible on the input
2635 if (exec->transcount == atom->max) {
2636 break;
2638 exec->index++;
2639 value = exec->inputStack[exec->index].value;
2640 data = exec->inputStack[exec->index].data;
2641 #ifdef DEBUG_PUSH
2642 printf("value loaded: %s\n", value);
2643 #endif
2646 * End of input: stop here
2648 if (value == NULL) {
2649 exec->index --;
2650 break;
2652 if (exec->transcount >= atom->min) {
2653 int transno = exec->transno;
2654 xmlRegStatePtr state = exec->state;
2657 * The transition is acceptable save it
2659 exec->transno = -1; /* trick */
2660 exec->state = to;
2661 if (exec->inputStackNr <= 0) {
2662 xmlFARegExecSaveInputString(exec, value, data);
2664 xmlFARegExecSave(exec);
2665 exec->transno = transno;
2666 exec->state = state;
2668 ret = xmlStrEqual(value, atom->valuep);
2669 exec->transcount++;
2670 } while (ret == 1);
2671 if (exec->transcount < atom->min)
2672 ret = 0;
2675 * If the last check failed but one transition was found
2676 * possible, rollback
2678 if (ret < 0)
2679 ret = 0;
2680 if (ret == 0) {
2681 goto rollback;
2685 if (ret == 1) {
2686 if ((exec->callback != NULL) && (atom != NULL)) {
2687 exec->callback(exec->data, atom->valuep,
2688 atom->data, data);
2690 if (exec->state->nbTrans > exec->transno + 1) {
2691 if (exec->inputStackNr <= 0) {
2692 xmlFARegExecSaveInputString(exec, value, data);
2694 xmlFARegExecSave(exec);
2696 if (trans->counter >= 0) {
2697 #ifdef DEBUG_PUSH
2698 printf("Increasing count %d\n", trans->counter);
2699 #endif
2700 exec->counts[trans->counter]++;
2702 #ifdef DEBUG_PUSH
2703 printf("entering state %d\n", trans->to);
2704 #endif
2705 exec->state = exec->comp->states[trans->to];
2706 exec->transno = 0;
2707 if (trans->atom != NULL) {
2708 if (exec->inputStack != NULL) {
2709 exec->index++;
2710 if (exec->index < exec->inputStackNr) {
2711 value = exec->inputStack[exec->index].value;
2712 data = exec->inputStack[exec->index].data;
2713 #ifdef DEBUG_PUSH
2714 printf("value loaded: %s\n", value);
2715 #endif
2716 } else {
2717 value = NULL;
2718 data = NULL;
2719 #ifdef DEBUG_PUSH
2720 printf("end of input\n");
2721 #endif
2723 } else {
2724 value = NULL;
2725 data = NULL;
2726 #ifdef DEBUG_PUSH
2727 printf("end of input\n");
2728 #endif
2731 goto progress;
2732 } else if (ret < 0) {
2733 exec->status = -4;
2734 break;
2737 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2738 rollback:
2740 * Failed to find a way out
2742 exec->determinist = 0;
2743 xmlFARegExecRollBack(exec);
2744 if (exec->status == 0) {
2745 value = exec->inputStack[exec->index].value;
2746 data = exec->inputStack[exec->index].data;
2747 #ifdef DEBUG_PUSH
2748 printf("value loaded: %s\n", value);
2749 #endif
2752 progress:
2753 continue;
2755 if (exec->status == 0) {
2756 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2758 return(exec->status);
2761 #if 0
2762 static int
2763 xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2764 xmlRegTransPtr trans;
2765 xmlRegAtomPtr atom;
2766 int ret;
2767 int codepoint, len;
2769 if (exec == NULL)
2770 return(-1);
2771 if (exec->status != 0)
2772 return(exec->status);
2774 while ((exec->status == 0) &&
2775 ((exec->inputString[exec->index] != 0) ||
2776 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2779 * End of input on non-terminal state, rollback, however we may
2780 * still have epsilon like transition for counted transitions
2781 * on counters, in that case don't break too early.
2783 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2784 goto rollback;
2786 exec->transcount = 0;
2787 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2788 trans = &exec->state->trans[exec->transno];
2789 if (trans->to < 0)
2790 continue;
2791 atom = trans->atom;
2792 ret = 0;
2793 if (trans->count >= 0) {
2794 int count;
2795 xmlRegCounterPtr counter;
2798 * A counted transition.
2801 count = exec->counts[trans->count];
2802 counter = &exec->comp->counters[trans->count];
2803 #ifdef DEBUG_REGEXP_EXEC
2804 printf("testing count %d: val %d, min %d, max %d\n",
2805 trans->count, count, counter->min, counter->max);
2806 #endif
2807 ret = ((count >= counter->min) && (count <= counter->max));
2808 } else if (atom == NULL) {
2809 fprintf(stderr, "epsilon transition left at runtime\n");
2810 exec->status = -2;
2811 break;
2812 } else if (exec->inputString[exec->index] != 0) {
2813 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2814 ret = xmlRegCheckCharacter(atom, codepoint);
2815 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2816 xmlRegStatePtr to = exec->comp->states[trans->to];
2819 * this is a multiple input sequence
2821 if (exec->state->nbTrans > exec->transno + 1) {
2822 xmlFARegExecSave(exec);
2824 exec->transcount = 1;
2825 do {
2827 * Try to progress as much as possible on the input
2829 if (exec->transcount == atom->max) {
2830 break;
2832 exec->index += len;
2834 * End of input: stop here
2836 if (exec->inputString[exec->index] == 0) {
2837 exec->index -= len;
2838 break;
2840 if (exec->transcount >= atom->min) {
2841 int transno = exec->transno;
2842 xmlRegStatePtr state = exec->state;
2845 * The transition is acceptable save it
2847 exec->transno = -1; /* trick */
2848 exec->state = to;
2849 xmlFARegExecSave(exec);
2850 exec->transno = transno;
2851 exec->state = state;
2853 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2854 len);
2855 ret = xmlRegCheckCharacter(atom, codepoint);
2856 exec->transcount++;
2857 } while (ret == 1);
2858 if (exec->transcount < atom->min)
2859 ret = 0;
2862 * If the last check failed but one transition was found
2863 * possible, rollback
2865 if (ret < 0)
2866 ret = 0;
2867 if (ret == 0) {
2868 goto rollback;
2872 if (ret == 1) {
2873 if (exec->state->nbTrans > exec->transno + 1) {
2874 xmlFARegExecSave(exec);
2876 if (trans->counter >= 0) {
2877 #ifdef DEBUG_REGEXP_EXEC
2878 printf("Increasing count %d\n", trans->counter);
2879 #endif
2880 exec->counts[trans->counter]++;
2882 #ifdef DEBUG_REGEXP_EXEC
2883 printf("entering state %d\n", trans->to);
2884 #endif
2885 exec->state = exec->comp->states[trans->to];
2886 exec->transno = 0;
2887 if (trans->atom != NULL) {
2888 exec->index += len;
2890 goto progress;
2891 } else if (ret < 0) {
2892 exec->status = -4;
2893 break;
2896 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2897 rollback:
2899 * Failed to find a way out
2901 exec->determinist = 0;
2902 xmlFARegExecRollBack(exec);
2904 progress:
2905 continue;
2908 #endif
2909 /************************************************************************
2911 * Parser for the Shemas Datatype Regular Expressions *
2912 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
2914 ************************************************************************/
2917 * xmlFAIsChar:
2918 * @ctxt: a regexp parser context
2920 * [10] Char ::= [^.\?*+()|#x5B#x5D]
2922 static int
2923 xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
2924 int cur;
2925 int len;
2927 cur = CUR_SCHAR(ctxt->cur, len);
2928 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
2929 (cur == '*') || (cur == '+') || (cur == '(') ||
2930 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
2931 (cur == 0x5D) || (cur == 0))
2932 return(-1);
2933 return(cur);
2937 * xmlFAParseCharProp:
2938 * @ctxt: a regexp parser context
2940 * [27] charProp ::= IsCategory | IsBlock
2941 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
2942 * Separators | Symbols | Others
2943 * [29] Letters ::= 'L' [ultmo]?
2944 * [30] Marks ::= 'M' [nce]?
2945 * [31] Numbers ::= 'N' [dlo]?
2946 * [32] Punctuation ::= 'P' [cdseifo]?
2947 * [33] Separators ::= 'Z' [slp]?
2948 * [34] Symbols ::= 'S' [mcko]?
2949 * [35] Others ::= 'C' [cfon]?
2950 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
2952 static void
2953 xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
2954 int cur;
2955 xmlRegAtomType type = 0;
2956 xmlChar *blockName = NULL;
2958 cur = CUR;
2959 if (cur == 'L') {
2960 NEXT;
2961 cur = CUR;
2962 if (cur == 'u') {
2963 NEXT;
2964 type = XML_REGEXP_LETTER_UPPERCASE;
2965 } else if (cur == 'l') {
2966 NEXT;
2967 type = XML_REGEXP_LETTER_LOWERCASE;
2968 } else if (cur == 't') {
2969 NEXT;
2970 type = XML_REGEXP_LETTER_TITLECASE;
2971 } else if (cur == 'm') {
2972 NEXT;
2973 type = XML_REGEXP_LETTER_MODIFIER;
2974 } else if (cur == 'o') {
2975 NEXT;
2976 type = XML_REGEXP_LETTER_OTHERS;
2977 } else {
2978 type = XML_REGEXP_LETTER;
2980 } else if (cur == 'M') {
2981 NEXT;
2982 cur = CUR;
2983 if (cur == 'n') {
2984 NEXT;
2985 /* nonspacing */
2986 type = XML_REGEXP_MARK_NONSPACING;
2987 } else if (cur == 'c') {
2988 NEXT;
2989 /* spacing combining */
2990 type = XML_REGEXP_MARK_SPACECOMBINING;
2991 } else if (cur == 'e') {
2992 NEXT;
2993 /* enclosing */
2994 type = XML_REGEXP_MARK_ENCLOSING;
2995 } else {
2996 /* all marks */
2997 type = XML_REGEXP_MARK;
2999 } else if (cur == 'N') {
3000 NEXT;
3001 cur = CUR;
3002 if (cur == 'd') {
3003 NEXT;
3004 /* digital */
3005 type = XML_REGEXP_NUMBER_DECIMAL;
3006 } else if (cur == 'l') {
3007 NEXT;
3008 /* letter */
3009 type = XML_REGEXP_NUMBER_LETTER;
3010 } else if (cur == 'o') {
3011 NEXT;
3012 /* other */
3013 type = XML_REGEXP_NUMBER_OTHERS;
3014 } else {
3015 /* all numbers */
3016 type = XML_REGEXP_NUMBER;
3018 } else if (cur == 'P') {
3019 NEXT;
3020 cur = CUR;
3021 if (cur == 'c') {
3022 NEXT;
3023 /* connector */
3024 type = XML_REGEXP_PUNCT_CONNECTOR;
3025 } else if (cur == 'd') {
3026 NEXT;
3027 /* dash */
3028 type = XML_REGEXP_PUNCT_DASH;
3029 } else if (cur == 's') {
3030 NEXT;
3031 /* open */
3032 type = XML_REGEXP_PUNCT_OPEN;
3033 } else if (cur == 'e') {
3034 NEXT;
3035 /* close */
3036 type = XML_REGEXP_PUNCT_CLOSE;
3037 } else if (cur == 'i') {
3038 NEXT;
3039 /* initial quote */
3040 type = XML_REGEXP_PUNCT_INITQUOTE;
3041 } else if (cur == 'f') {
3042 NEXT;
3043 /* final quote */
3044 type = XML_REGEXP_PUNCT_FINQUOTE;
3045 } else if (cur == 'o') {
3046 NEXT;
3047 /* other */
3048 type = XML_REGEXP_PUNCT_OTHERS;
3049 } else {
3050 /* all punctuation */
3051 type = XML_REGEXP_PUNCT;
3053 } else if (cur == 'Z') {
3054 NEXT;
3055 cur = CUR;
3056 if (cur == 's') {
3057 NEXT;
3058 /* space */
3059 type = XML_REGEXP_SEPAR_SPACE;
3060 } else if (cur == 'l') {
3061 NEXT;
3062 /* line */
3063 type = XML_REGEXP_SEPAR_LINE;
3064 } else if (cur == 'p') {
3065 NEXT;
3066 /* paragraph */
3067 type = XML_REGEXP_SEPAR_PARA;
3068 } else {
3069 /* all separators */
3070 type = XML_REGEXP_SEPAR;
3072 } else if (cur == 'S') {
3073 NEXT;
3074 cur = CUR;
3075 if (cur == 'm') {
3076 NEXT;
3077 type = XML_REGEXP_SYMBOL_MATH;
3078 /* math */
3079 } else if (cur == 'c') {
3080 NEXT;
3081 type = XML_REGEXP_SYMBOL_CURRENCY;
3082 /* currency */
3083 } else if (cur == 'k') {
3084 NEXT;
3085 type = XML_REGEXP_SYMBOL_MODIFIER;
3086 /* modifiers */
3087 } else if (cur == 'o') {
3088 NEXT;
3089 type = XML_REGEXP_SYMBOL_OTHERS;
3090 /* other */
3091 } else {
3092 /* all symbols */
3093 type = XML_REGEXP_SYMBOL;
3095 } else if (cur == 'C') {
3096 NEXT;
3097 cur = CUR;
3098 if (cur == 'c') {
3099 NEXT;
3100 /* control */
3101 type = XML_REGEXP_OTHER_CONTROL;
3102 } else if (cur == 'f') {
3103 NEXT;
3104 /* format */
3105 type = XML_REGEXP_OTHER_FORMAT;
3106 } else if (cur == 'o') {
3107 NEXT;
3108 /* private use */
3109 type = XML_REGEXP_OTHER_PRIVATE;
3110 } else if (cur == 'n') {
3111 NEXT;
3112 /* not assigned */
3113 type = XML_REGEXP_OTHER_NA;
3114 } else {
3115 /* all others */
3116 type = XML_REGEXP_OTHER;
3118 } else if (cur == 'I') {
3119 const xmlChar *start;
3120 NEXT;
3121 cur = CUR;
3122 if (cur != 's') {
3123 ERROR("IsXXXX expected");
3124 return;
3126 NEXT;
3127 start = ctxt->cur;
3128 cur = CUR;
3129 if (((cur >= 'a') && (cur <= 'z')) ||
3130 ((cur >= 'A') && (cur <= 'Z')) ||
3131 ((cur >= '0') && (cur <= '9')) ||
3132 (cur == 0x2D)) {
3133 NEXT;
3134 cur = CUR;
3135 while (((cur >= 'a') && (cur <= 'z')) ||
3136 ((cur >= 'A') && (cur <= 'Z')) ||
3137 ((cur >= '0') && (cur <= '9')) ||
3138 (cur == 0x2D)) {
3139 NEXT;
3140 cur = CUR;
3143 type = XML_REGEXP_BLOCK_NAME;
3144 blockName = xmlStrndup(start, ctxt->cur - start);
3145 } else {
3146 ERROR("Unknown char property");
3147 return;
3149 if (ctxt->atom == NULL) {
3150 ctxt->atom = xmlRegNewAtom(ctxt, type);
3151 if (ctxt->atom != NULL)
3152 ctxt->atom->valuep = blockName;
3153 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3154 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3155 type, 0, 0, blockName);
3160 * xmlFAParseCharClassEsc:
3161 * @ctxt: a regexp parser context
3163 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3164 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3165 * [25] catEsc ::= '\p{' charProp '}'
3166 * [26] complEsc ::= '\P{' charProp '}'
3167 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3169 static void
3170 xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3171 int cur;
3173 if (CUR == '.') {
3174 if (ctxt->atom == NULL) {
3175 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3176 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3177 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3178 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3180 NEXT;
3181 return;
3183 if (CUR != '\\') {
3184 ERROR("Escaped sequence: expecting \\");
3185 return;
3187 NEXT;
3188 cur = CUR;
3189 if (cur == 'p') {
3190 NEXT;
3191 if (CUR != '{') {
3192 ERROR("Expecting '{'");
3193 return;
3195 NEXT;
3196 xmlFAParseCharProp(ctxt);
3197 if (CUR != '}') {
3198 ERROR("Expecting '}'");
3199 return;
3201 NEXT;
3202 } else if (cur == 'P') {
3203 NEXT;
3204 if (CUR != '{') {
3205 ERROR("Expecting '{'");
3206 return;
3208 NEXT;
3209 xmlFAParseCharProp(ctxt);
3210 ctxt->atom->neg = 1;
3211 if (CUR != '}') {
3212 ERROR("Expecting '}'");
3213 return;
3215 NEXT;
3216 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3217 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3218 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3219 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3220 (cur == 0x5E)) {
3221 if (ctxt->atom == NULL) {
3222 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3223 if (ctxt->atom != NULL)
3224 ctxt->atom->codepoint = cur;
3225 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3226 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3227 XML_REGEXP_CHARVAL, cur, cur, NULL);
3229 NEXT;
3230 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3231 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3232 (cur == 'w') || (cur == 'W')) {
3233 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
3235 switch (cur) {
3236 case 's':
3237 type = XML_REGEXP_ANYSPACE;
3238 break;
3239 case 'S':
3240 type = XML_REGEXP_NOTSPACE;
3241 break;
3242 case 'i':
3243 type = XML_REGEXP_INITNAME;
3244 break;
3245 case 'I':
3246 type = XML_REGEXP_NOTINITNAME;
3247 break;
3248 case 'c':
3249 type = XML_REGEXP_NAMECHAR;
3250 break;
3251 case 'C':
3252 type = XML_REGEXP_NOTNAMECHAR;
3253 break;
3254 case 'd':
3255 type = XML_REGEXP_DECIMAL;
3256 break;
3257 case 'D':
3258 type = XML_REGEXP_NOTDECIMAL;
3259 break;
3260 case 'w':
3261 type = XML_REGEXP_REALCHAR;
3262 break;
3263 case 'W':
3264 type = XML_REGEXP_NOTREALCHAR;
3265 break;
3267 NEXT;
3268 if (ctxt->atom == NULL) {
3269 ctxt->atom = xmlRegNewAtom(ctxt, type);
3270 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3271 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3272 type, 0, 0, NULL);
3278 * xmlFAParseCharRef:
3279 * @ctxt: a regexp parser context
3281 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3283 static int
3284 xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3285 int ret = 0, cur;
3287 if ((CUR != '&') || (NXT(1) != '#'))
3288 return(-1);
3289 NEXT;
3290 NEXT;
3291 cur = CUR;
3292 if (cur == 'x') {
3293 NEXT;
3294 cur = CUR;
3295 if (((cur >= '0') && (cur <= '9')) ||
3296 ((cur >= 'a') && (cur <= 'f')) ||
3297 ((cur >= 'A') && (cur <= 'F'))) {
3298 while (((cur >= '0') && (cur <= '9')) ||
3299 ((cur >= 'A') && (cur <= 'F'))) {
3300 if ((cur >= '0') && (cur <= '9'))
3301 ret = ret * 16 + cur - '0';
3302 else if ((cur >= 'a') && (cur <= 'f'))
3303 ret = ret * 16 + 10 + (cur - 'a');
3304 else
3305 ret = ret * 16 + 10 + (cur - 'A');
3306 NEXT;
3307 cur = CUR;
3309 } else {
3310 ERROR("Char ref: expecting [0-9A-F]");
3311 return(-1);
3313 } else {
3314 if ((cur >= '0') && (cur <= '9')) {
3315 while ((cur >= '0') && (cur <= '9')) {
3316 ret = ret * 10 + cur - '0';
3317 NEXT;
3318 cur = CUR;
3320 } else {
3321 ERROR("Char ref: expecting [0-9]");
3322 return(-1);
3325 if (cur != ';') {
3326 ERROR("Char ref: expecting ';'");
3327 return(-1);
3328 } else {
3329 NEXT;
3331 return(ret);
3335 * xmlFAParseCharRange:
3336 * @ctxt: a regexp parser context
3338 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3339 * [18] seRange ::= charOrEsc '-' charOrEsc
3340 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3341 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3342 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3344 static void
3345 xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
3346 int cur;
3347 int start = -1;
3348 int end = -1;
3350 if ((CUR == '&') && (NXT(1) == '#')) {
3351 end = start = xmlFAParseCharRef(ctxt);
3352 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3353 XML_REGEXP_CHARVAL, start, end, NULL);
3354 return;
3356 cur = CUR;
3357 if (cur == '\\') {
3358 NEXT;
3359 cur = CUR;
3360 switch (cur) {
3361 case 'n': start = 0xA; break;
3362 case 'r': start = 0xD; break;
3363 case 't': start = 0x9; break;
3364 case '\\': case '|': case '.': case '-': case '^': case '?':
3365 case '*': case '+': case '{': case '}': case '(': case ')':
3366 case '[': case ']':
3367 start = cur; break;
3368 default:
3369 ERROR("Invalid escape value");
3370 return;
3372 end = start;
3373 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3374 end = start = cur;
3375 } else {
3376 ERROR("Expecting a char range");
3377 return;
3379 NEXT;
3380 if (start == '-') {
3381 return;
3383 cur = CUR;
3384 if (cur != '-') {
3385 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3386 XML_REGEXP_CHARVAL, start, end, NULL);
3387 return;
3389 NEXT;
3390 cur = CUR;
3391 if (cur == '\\') {
3392 NEXT;
3393 cur = CUR;
3394 switch (cur) {
3395 case 'n': end = 0xA; break;
3396 case 'r': end = 0xD; break;
3397 case 't': end = 0x9; break;
3398 case '\\': case '|': case '.': case '-': case '^': case '?':
3399 case '*': case '+': case '{': case '}': case '(': case ')':
3400 case '[': case ']':
3401 end = cur; break;
3402 default:
3403 ERROR("Invalid escape value");
3404 return;
3406 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3407 end = cur;
3408 } else {
3409 ERROR("Expecting the end of a char range");
3410 return;
3412 NEXT;
3413 /* TODO check that the values are acceptable character ranges for XML */
3414 if (end < start) {
3415 ERROR("End of range is before start of range");
3416 } else {
3417 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3418 XML_REGEXP_CHARVAL, start, end, NULL);
3420 return;
3424 * xmlFAParsePosCharGroup:
3425 * @ctxt: a regexp parser context
3427 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3429 static void
3430 xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3431 do {
3432 if ((CUR == '\\') || (CUR == '.')) {
3433 xmlFAParseCharClassEsc(ctxt);
3434 } else {
3435 xmlFAParseCharRange(ctxt);
3437 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3438 (ctxt->error == 0));
3442 * xmlFAParseCharGroup:
3443 * @ctxt: a regexp parser context
3445 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3446 * [15] negCharGroup ::= '^' posCharGroup
3447 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3448 * [12] charClassExpr ::= '[' charGroup ']'
3450 static void
3451 xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3452 int n = ctxt->neg;
3453 while ((CUR != ']') && (ctxt->error == 0)) {
3454 if (CUR == '^') {
3455 int neg = ctxt->neg;
3457 NEXT;
3458 ctxt->neg = !ctxt->neg;
3459 xmlFAParsePosCharGroup(ctxt);
3460 ctxt->neg = neg;
3461 } else if (CUR == '-') {
3462 NEXT;
3463 ctxt->neg = !ctxt->neg;
3464 if (CUR != '[') {
3465 ERROR("charClassExpr: '[' expected");
3466 break;
3468 NEXT;
3469 xmlFAParseCharGroup(ctxt);
3470 if (CUR == ']') {
3471 NEXT;
3472 } else {
3473 ERROR("charClassExpr: ']' expected");
3474 break;
3476 break;
3477 } else if (CUR != ']') {
3478 xmlFAParsePosCharGroup(ctxt);
3481 ctxt->neg = n;
3485 * xmlFAParseCharClass:
3486 * @ctxt: a regexp parser context
3488 * [11] charClass ::= charClassEsc | charClassExpr
3489 * [12] charClassExpr ::= '[' charGroup ']'
3491 static void
3492 xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3493 if (CUR == '[') {
3494 NEXT;
3495 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3496 if (ctxt->atom == NULL)
3497 return;
3498 xmlFAParseCharGroup(ctxt);
3499 if (CUR == ']') {
3500 NEXT;
3501 } else {
3502 ERROR("xmlFAParseCharClass: ']' expected");
3504 } else {
3505 xmlFAParseCharClassEsc(ctxt);
3510 * xmlFAParseQuantExact:
3511 * @ctxt: a regexp parser context
3513 * [8] QuantExact ::= [0-9]+
3515 static int
3516 xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3517 int ret = 0;
3518 int ok = 0;
3520 while ((CUR >= '0') && (CUR <= '9')) {
3521 ret = ret * 10 + (CUR - '0');
3522 ok = 1;
3523 NEXT;
3525 if (ok != 1) {
3526 return(-1);
3528 return(ret);
3532 * xmlFAParseQuantifier:
3533 * @ctxt: a regexp parser context
3535 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3536 * [5] quantity ::= quantRange | quantMin | QuantExact
3537 * [6] quantRange ::= QuantExact ',' QuantExact
3538 * [7] quantMin ::= QuantExact ','
3539 * [8] QuantExact ::= [0-9]+
3541 static int
3542 xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3543 int cur;
3545 cur = CUR;
3546 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3547 if (ctxt->atom != NULL) {
3548 if (cur == '?')
3549 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3550 else if (cur == '*')
3551 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3552 else if (cur == '+')
3553 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3555 NEXT;
3556 return(1);
3558 if (cur == '{') {
3559 int min = 0, max = 0;
3561 NEXT;
3562 cur = xmlFAParseQuantExact(ctxt);
3563 if (cur >= 0)
3564 min = cur;
3565 if (CUR == ',') {
3566 NEXT;
3567 cur = xmlFAParseQuantExact(ctxt);
3568 if (cur >= 0)
3569 max = cur;
3571 if (CUR == '}') {
3572 NEXT;
3573 } else {
3574 ERROR("Unterminated quantifier");
3576 if (max == 0)
3577 max = min;
3578 if (ctxt->atom != NULL) {
3579 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3580 ctxt->atom->min = min;
3581 ctxt->atom->max = max;
3583 return(1);
3585 return(0);
3589 * xmlFAParseAtom:
3590 * @ctxt: a regexp parser context
3592 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3594 static int
3595 xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3596 int codepoint, len;
3598 codepoint = xmlFAIsChar(ctxt);
3599 if (codepoint > 0) {
3600 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3601 if (ctxt->atom == NULL)
3602 return(-1);
3603 codepoint = CUR_SCHAR(ctxt->cur, len);
3604 ctxt->atom->codepoint = codepoint;
3605 NEXTL(len);
3606 return(1);
3607 } else if (CUR == '|') {
3608 return(0);
3609 } else if (CUR == 0) {
3610 return(0);
3611 } else if (CUR == ')') {
3612 return(0);
3613 } else if (CUR == '(') {
3614 xmlRegStatePtr start, oldend;
3616 NEXT;
3617 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3618 start = ctxt->state;
3619 oldend = ctxt->end;
3620 ctxt->end = NULL;
3621 ctxt->atom = NULL;
3622 xmlFAParseRegExp(ctxt, 0);
3623 if (CUR == ')') {
3624 NEXT;
3625 } else {
3626 ERROR("xmlFAParseAtom: expecting ')'");
3628 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3629 if (ctxt->atom == NULL)
3630 return(-1);
3631 ctxt->atom->start = start;
3632 ctxt->atom->stop = ctxt->state;
3633 ctxt->end = oldend;
3634 return(1);
3635 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3636 xmlFAParseCharClass(ctxt);
3637 return(1);
3639 return(0);
3643 * xmlFAParsePiece:
3644 * @ctxt: a regexp parser context
3646 * [3] piece ::= atom quantifier?
3648 static int
3649 xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3650 int ret;
3652 ctxt->atom = NULL;
3653 ret = xmlFAParseAtom(ctxt);
3654 if (ret == 0)
3655 return(0);
3656 if (ctxt->atom == NULL) {
3657 ERROR("internal: no atom generated");
3659 xmlFAParseQuantifier(ctxt);
3660 return(1);
3664 * xmlFAParseBranch:
3665 * @ctxt: a regexp parser context
3666 * @first: is taht the first
3668 * [2] branch ::= piece*
3670 static void
3671 xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, int first) {
3672 xmlRegStatePtr previous;
3673 xmlRegAtomPtr prevatom = NULL;
3674 int ret;
3676 previous = ctxt->state;
3677 ret = xmlFAParsePiece(ctxt);
3678 if (ret != 0) {
3679 if (first) {
3680 xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom);
3681 previous = ctxt->state;
3682 } else {
3683 prevatom = ctxt->atom;
3685 ctxt->atom = NULL;
3687 while ((ret != 0) && (ctxt->error == 0)) {
3688 ret = xmlFAParsePiece(ctxt);
3689 if (ret != 0) {
3690 if (first) {
3691 xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom);
3692 } else {
3693 xmlFAGenerateTransitions(ctxt, previous, NULL, prevatom);
3694 prevatom = ctxt->atom;
3696 previous = ctxt->state;
3697 ctxt->atom = NULL;
3700 if (!first) {
3701 xmlFAGenerateTransitions(ctxt, previous, ctxt->end, prevatom);
3706 * xmlFAParseRegExp:
3707 * @ctxt: a regexp parser context
3708 * @top: is that the top-level expressions ?
3710 * [1] regExp ::= branch ( '|' branch )*
3712 static void
3713 xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
3714 xmlRegStatePtr start, end, oldend;
3716 oldend = ctxt->end;
3718 start = ctxt->state;
3719 xmlFAParseBranch(ctxt, (ctxt->end == NULL));
3720 if (CUR != '|') {
3721 ctxt->end = ctxt->state;
3722 return;
3724 end = ctxt->state;
3725 while ((CUR == '|') && (ctxt->error == 0)) {
3726 NEXT;
3727 ctxt->state = start;
3728 ctxt->end = end;
3729 xmlFAParseBranch(ctxt, 0);
3731 if (!top)
3732 ctxt->end = oldend;
3735 /************************************************************************
3737 * The basic API *
3739 ************************************************************************/
3742 * xmlRegexpPrint:
3743 * @output: the file for the output debug
3744 * @regexp: the compiled regexp
3746 * Print the content of the compiled regular expression
3748 void
3749 xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3750 int i;
3752 fprintf(output, " regexp: ");
3753 if (regexp == NULL) {
3754 fprintf(output, "NULL\n");
3755 return;
3757 fprintf(output, "'%s' ", regexp->string);
3758 fprintf(output, "\n");
3759 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3760 for (i = 0;i < regexp->nbAtoms; i++) {
3761 fprintf(output, " %02d ", i);
3762 xmlRegPrintAtom(output, regexp->atoms[i]);
3764 fprintf(output, "%d states:", regexp->nbStates);
3765 fprintf(output, "\n");
3766 for (i = 0;i < regexp->nbStates; i++) {
3767 xmlRegPrintState(output, regexp->states[i]);
3769 fprintf(output, "%d counters:\n", regexp->nbCounters);
3770 for (i = 0;i < regexp->nbCounters; i++) {
3771 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
3772 regexp->counters[i].max);
3777 * xmlRegexpCompile:
3778 * @regexp: a regular expression string
3780 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
3781 * Appendix F and build an automata suitable for testing strings against
3782 * that regular expression
3784 * Returns the compiled expression or NULL in case of error
3786 xmlRegexpPtr
3787 xmlRegexpCompile(const xmlChar *regexp) {
3788 xmlRegexpPtr ret;
3789 xmlRegParserCtxtPtr ctxt;
3791 ctxt = xmlRegNewParserCtxt(regexp);
3792 if (ctxt == NULL)
3793 return(NULL);
3795 /* initialize the parser */
3796 ctxt->end = NULL;
3797 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3798 xmlRegStatePush(ctxt, ctxt->start);
3800 /* parse the expression building an automata */
3801 xmlFAParseRegExp(ctxt, 1);
3802 if (CUR != 0) {
3803 ERROR("xmlFAParseRegExp: extra characters");
3805 ctxt->end = ctxt->state;
3806 ctxt->start->type = XML_REGEXP_START_STATE;
3807 ctxt->end->type = XML_REGEXP_FINAL_STATE;
3809 /* remove the Epsilon except for counted transitions */
3810 xmlFAEliminateEpsilonTransitions(ctxt);
3813 if (ctxt->error != 0) {
3814 xmlRegFreeParserCtxt(ctxt);
3815 return(NULL);
3817 ret = xmlRegEpxFromParse(ctxt);
3818 xmlRegFreeParserCtxt(ctxt);
3819 return(ret);
3823 * xmlRegexpExec:
3824 * @comp: the compiled regular expression
3825 * @content: the value to check against the regular expression
3827 * Check if the regular expression generate the value
3829 * Returns 1 if it matches, 0 if not and a negativa value in case of error
3832 xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
3833 if ((comp == NULL) || (content == NULL))
3834 return(-1);
3835 return(xmlFARegExec(comp, content));
3839 * xmlRegexpIsDeterminist:
3840 * @comp: the compiled regular expression
3842 * Check if the regular expression is determinist
3844 * Returns 1 if it yes, 0 if not and a negativa value in case of error
3847 xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
3848 xmlAutomataPtr am;
3849 int ret;
3851 if (comp == NULL)
3852 return(-1);
3853 if (comp->determinist != -1)
3854 return(comp->determinist);
3856 am = xmlNewAutomata();
3857 if (am->states != NULL) {
3858 int i;
3860 for (i = 0;i < am->nbStates;i++)
3861 xmlRegFreeState(am->states[i]);
3862 xmlFree(am->states);
3864 am->nbAtoms = comp->nbAtoms;
3865 am->atoms = comp->atoms;
3866 am->nbStates = comp->nbStates;
3867 am->states = comp->states;
3868 am->determinist = -1;
3869 ret = xmlFAComputesDeterminism(am);
3870 am->atoms = NULL;
3871 am->states = NULL;
3872 xmlFreeAutomata(am);
3873 return(ret);
3877 * xmlRegFreeRegexp:
3878 * @regexp: the regexp
3880 * Free a regexp
3882 void
3883 xmlRegFreeRegexp(xmlRegexpPtr regexp) {
3884 int i;
3885 if (regexp == NULL)
3886 return;
3888 if (regexp->string != NULL)
3889 xmlFree(regexp->string);
3890 if (regexp->states != NULL) {
3891 for (i = 0;i < regexp->nbStates;i++)
3892 xmlRegFreeState(regexp->states[i]);
3893 xmlFree(regexp->states);
3895 if (regexp->atoms != NULL) {
3896 for (i = 0;i < regexp->nbAtoms;i++)
3897 xmlRegFreeAtom(regexp->atoms[i]);
3898 xmlFree(regexp->atoms);
3900 if (regexp->counters != NULL)
3901 xmlFree(regexp->counters);
3902 if (regexp->compact != NULL)
3903 xmlFree(regexp->compact);
3904 if (regexp->transdata != NULL)
3905 xmlFree(regexp->transdata);
3906 if (regexp->stringMap != NULL) {
3907 for (i = 0; i < regexp->nbstrings;i++)
3908 xmlFree(regexp->stringMap[i]);
3909 xmlFree(regexp->stringMap);
3912 xmlFree(regexp);
3915 #ifdef LIBXML_AUTOMATA_ENABLED
3916 /************************************************************************
3918 * The Automata interface *
3920 ************************************************************************/
3923 * xmlNewAutomata:
3925 * Create a new automata
3927 * Returns the new object or NULL in case of failure
3929 xmlAutomataPtr
3930 xmlNewAutomata(void) {
3931 xmlAutomataPtr ctxt;
3933 ctxt = xmlRegNewParserCtxt(NULL);
3934 if (ctxt == NULL)
3935 return(NULL);
3937 /* initialize the parser */
3938 ctxt->end = NULL;
3939 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3940 xmlRegStatePush(ctxt, ctxt->start);
3942 return(ctxt);
3946 * xmlFreeAutomata:
3947 * @am: an automata
3949 * Free an automata
3951 void
3952 xmlFreeAutomata(xmlAutomataPtr am) {
3953 if (am == NULL)
3954 return;
3955 xmlRegFreeParserCtxt(am);
3959 * xmlAutomataGetInitState:
3960 * @am: an automata
3962 * Returns the initial state of the automata
3964 xmlAutomataStatePtr
3965 xmlAutomataGetInitState(xmlAutomataPtr am) {
3966 if (am == NULL)
3967 return(NULL);
3968 return(am->start);
3972 * xmlAutomataSetFinalState:
3973 * @am: an automata
3974 * @state: a state in this automata
3976 * Makes that state a final state
3978 * Returns 0 or -1 in case of error
3981 xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
3982 if ((am == NULL) || (state == NULL))
3983 return(-1);
3984 state->type = XML_REGEXP_FINAL_STATE;
3985 return(0);
3989 * xmlAutomataNewTransition:
3990 * @am: an automata
3991 * @from: the starting point of the transition
3992 * @to: the target point of the transition or NULL
3993 * @token: the input string associated to that transition
3994 * @data: data passed to the callback function if the transition is activated
3996 * If @to is NULL, this create first a new target state in the automata
3997 * and then adds a transition from the @from state to the target state
3998 * activated by the value of @token
4000 * Returns the target state or NULL in case of error
4002 xmlAutomataStatePtr
4003 xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4004 xmlAutomataStatePtr to, const xmlChar *token,
4005 void *data) {
4006 xmlRegAtomPtr atom;
4008 if ((am == NULL) || (from == NULL) || (token == NULL))
4009 return(NULL);
4010 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4011 atom->data = data;
4012 if (atom == NULL)
4013 return(NULL);
4014 atom->valuep = xmlStrdup(token);
4016 xmlFAGenerateTransitions(am, from, to, atom);
4017 if (to == NULL)
4018 return(am->state);
4019 return(to);
4023 * xmlAutomataNewCountTrans:
4024 * @am: an automata
4025 * @from: the starting point of the transition
4026 * @to: the target point of the transition or NULL
4027 * @token: the input string associated to that transition
4028 * @min: the minimum successive occurences of token
4029 * @min: the maximum successive occurences of token
4031 * If @to is NULL, this create first a new target state in the automata
4032 * and then adds a transition from the @from state to the target state
4033 * activated by a succession of input of value @token and whose number
4034 * is between @min and @max
4036 * Returns the target state or NULL in case of error
4038 xmlAutomataStatePtr
4039 xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4040 xmlAutomataStatePtr to, const xmlChar *token,
4041 int min, int max, void *data) {
4042 xmlRegAtomPtr atom;
4044 if ((am == NULL) || (from == NULL) || (token == NULL))
4045 return(NULL);
4046 if (min < 0)
4047 return(NULL);
4048 if ((max < min) || (max < 1))
4049 return(NULL);
4050 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4051 if (atom == NULL)
4052 return(NULL);
4053 atom->valuep = xmlStrdup(token);
4054 atom->data = data;
4055 if (min == 0)
4056 atom->min = 1;
4057 else
4058 atom->min = min;
4059 atom->max = max;
4061 xmlFAGenerateTransitions(am, from, to, atom);
4062 if (to == NULL)
4063 to = am->state;
4064 if (to == NULL)
4065 return(NULL);
4066 if (min == 0)
4067 xmlFAGenerateEpsilonTransition(am, from, to);
4068 return(to);
4072 * xmlAutomataNewOnceTrans:
4073 * @am: an automata
4074 * @from: the starting point of the transition
4075 * @to: the target point of the transition or NULL
4076 * @token: the input string associated to that transition
4077 * @min: the minimum successive occurences of token
4078 * @min: the maximum successive occurences of token
4080 * If @to is NULL, this create first a new target state in the automata
4081 * and then adds a transition from the @from state to the target state
4082 * activated by a succession of input of value @token and whose number
4083 * is between @min and @max, moreover that transistion can only be crossed
4084 * once.
4086 * Returns the target state or NULL in case of error
4088 xmlAutomataStatePtr
4089 xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4090 xmlAutomataStatePtr to, const xmlChar *token,
4091 int min, int max, void *data) {
4092 xmlRegAtomPtr atom;
4093 int counter;
4095 if ((am == NULL) || (from == NULL) || (token == NULL))
4096 return(NULL);
4097 if (min < 1)
4098 return(NULL);
4099 if ((max < min) || (max < 1))
4100 return(NULL);
4101 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4102 if (atom == NULL)
4103 return(NULL);
4104 atom->valuep = xmlStrdup(token);
4105 atom->data = data;
4106 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4107 if (min == 0)
4108 atom->min = 1;
4109 else
4110 atom->min = min;
4111 atom->max = max;
4113 * associate a counter to the transition.
4115 counter = xmlRegGetCounter(am);
4116 am->counters[counter].min = 1;
4117 am->counters[counter].max = 1;
4119 /* xmlFAGenerateTransitions(am, from, to, atom); */
4120 if (to == NULL) {
4121 to = xmlRegNewState(am);
4122 xmlRegStatePush(am, to);
4124 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4125 xmlRegAtomPush(am, atom);
4126 am->state = to;
4127 if (to == NULL)
4128 to = am->state;
4129 if (to == NULL)
4130 return(NULL);
4131 return(to);
4135 * xmlAutomataNewState:
4136 * @am: an automata
4138 * Create a new disconnected state in the automata
4140 * Returns the new state or NULL in case of error
4142 xmlAutomataStatePtr
4143 xmlAutomataNewState(xmlAutomataPtr am) {
4144 xmlAutomataStatePtr to;
4146 if (am == NULL)
4147 return(NULL);
4148 to = xmlRegNewState(am);
4149 xmlRegStatePush(am, to);
4150 return(to);
4154 * xmlAutomataNewTransition:
4155 * @am: an automata
4156 * @from: the starting point of the transition
4157 * @to: the target point of the transition or NULL
4159 * If @to is NULL, this create first a new target state in the automata
4160 * and then adds a an epsilon transition from the @from state to the
4161 * target state
4163 * Returns the target state or NULL in case of error
4165 xmlAutomataStatePtr
4166 xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4167 xmlAutomataStatePtr to) {
4168 if ((am == NULL) || (from == NULL))
4169 return(NULL);
4170 xmlFAGenerateEpsilonTransition(am, from, to);
4171 if (to == NULL)
4172 return(am->state);
4173 return(to);
4177 * xmlAutomataNewAllTrans:
4178 * @am: an automata
4179 * @from: the starting point of the transition
4180 * @to: the target point of the transition or NULL
4182 * If @to is NULL, this create first a new target state in the automata
4183 * and then adds a an ALL transition from the @from state to the
4184 * target state. That transition is an epsilon transition allowed only when
4185 * all transitions from the @from node have been activated.
4187 * Returns the target state or NULL in case of error
4189 xmlAutomataStatePtr
4190 xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4191 xmlAutomataStatePtr to, int lax) {
4192 if ((am == NULL) || (from == NULL))
4193 return(NULL);
4194 xmlFAGenerateAllTransition(am, from, to, lax);
4195 if (to == NULL)
4196 return(am->state);
4197 return(to);
4201 * xmlAutomataNewCounter:
4202 * @am: an automata
4203 * @min: the minimal value on the counter
4204 * @max: the maximal value on the counter
4206 * Create a new counter
4208 * Returns the counter number or -1 in case of error
4210 int
4211 xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4212 int ret;
4214 if (am == NULL)
4215 return(-1);
4217 ret = xmlRegGetCounter(am);
4218 if (ret < 0)
4219 return(-1);
4220 am->counters[ret].min = min;
4221 am->counters[ret].max = max;
4222 return(ret);
4226 * xmlAutomataNewCountedTrans:
4227 * @am: an automata
4228 * @from: the starting point of the transition
4229 * @to: the target point of the transition or NULL
4230 * @counter: the counter associated to that transition
4232 * If @to is NULL, this create first a new target state in the automata
4233 * and then adds an epsilon transition from the @from state to the target state
4234 * which will increment the counter provided
4236 * Returns the target state or NULL in case of error
4238 xmlAutomataStatePtr
4239 xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4240 xmlAutomataStatePtr to, int counter) {
4241 if ((am == NULL) || (from == NULL) || (counter < 0))
4242 return(NULL);
4243 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4244 if (to == NULL)
4245 return(am->state);
4246 return(to);
4250 * xmlAutomataNewCounterTrans:
4251 * @am: an automata
4252 * @from: the starting point of the transition
4253 * @to: the target point of the transition or NULL
4254 * @counter: the counter associated to that transition
4256 * If @to is NULL, this create first a new target state in the automata
4257 * and then adds an epsilon transition from the @from state to the target state
4258 * which will be allowed only if the counter is within the right range.
4260 * Returns the target state or NULL in case of error
4262 xmlAutomataStatePtr
4263 xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4264 xmlAutomataStatePtr to, int counter) {
4265 if ((am == NULL) || (from == NULL) || (counter < 0))
4266 return(NULL);
4267 xmlFAGenerateCountedTransition(am, from, to, counter);
4268 if (to == NULL)
4269 return(am->state);
4270 return(to);
4274 * xmlAutomataCompile:
4275 * @am: an automata
4277 * Compile the automata into a Reg Exp ready for being executed.
4278 * The automata should be free after this point.
4280 * Returns the compiled regexp or NULL in case of error
4282 xmlRegexpPtr
4283 xmlAutomataCompile(xmlAutomataPtr am) {
4284 xmlRegexpPtr ret;
4286 xmlFAEliminateEpsilonTransitions(am);
4287 /* xmlFAComputesDeterminism(am); */
4288 ret = xmlRegEpxFromParse(am);
4290 return(ret);
4294 * xmlAutomataIsDeterminist:
4295 * @am: an automata
4297 * Checks if an automata is determinist.
4299 * Returns 1 if true, 0 if not, and -1 in case of error
4301 int
4302 xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4303 int ret;
4305 if (am == NULL)
4306 return(-1);
4308 ret = xmlFAComputesDeterminism(am);
4309 return(ret);
4311 #endif /* LIBXML_AUTOMATA_ENABLED */
4312 #endif /* LIBXML_REGEXP_ENABLED */