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 these include:
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>
20 #ifdef LIBXML_REGEXP_ENABLED
22 /* #define DEBUG_ERR */
30 #include <libxml/tree.h>
31 #include <libxml/parserInternals.h>
32 #include <libxml/xmlregexp.h>
33 #include <libxml/xmlautomata.h>
34 #include <libxml/xmlunicode.h>
37 #define INT_MAX 123456789 /* easy to flag and big enough for our needs */
40 /* #define DEBUG_REGEXP_GRAPH */
41 /* #define DEBUG_REGEXP_EXEC */
42 /* #define DEBUG_PUSH */
43 /* #define DEBUG_COMPACTION */
45 #define MAX_PUSH 10000000
51 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
52 xmlRegexpErrCompile(ctxt, str);
53 #define NEXT ctxt->cur++
54 #define CUR (*(ctxt->cur))
55 #define NXT(index) (ctxt->cur[index])
57 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
58 #define NEXTL(l) ctxt->cur += l;
59 #define XML_REG_STRING_SEPARATOR '|'
61 * Need PREV to check on a '-' within a Character Group. May only be used
62 * when it's guaranteed that cur is not at the beginning of ctxt->string!
64 #define PREV (ctxt->cur[-1])
69 * macro to flag unimplemented blocks
72 xmlGenericError(xmlGenericErrorContext, \
73 "Unimplemented block at %s:%d\n", \
76 /************************************************************************
78 * Datatypes and structures *
80 ************************************************************************/
83 * Note: the order of the enums below is significant, do not shuffle
86 XML_REGEXP_EPSILON = 1,
89 XML_REGEXP_SUBREG, /* used for () sub regexps */
91 XML_REGEXP_ANYCHAR, /* . */
92 XML_REGEXP_ANYSPACE, /* \s */
93 XML_REGEXP_NOTSPACE, /* \S */
94 XML_REGEXP_INITNAME, /* \l */
95 XML_REGEXP_NOTINITNAME, /* \L */
96 XML_REGEXP_NAMECHAR, /* \c */
97 XML_REGEXP_NOTNAMECHAR, /* \C */
98 XML_REGEXP_DECIMAL, /* \d */
99 XML_REGEXP_NOTDECIMAL, /* \D */
100 XML_REGEXP_REALCHAR, /* \w */
101 XML_REGEXP_NOTREALCHAR, /* \W */
102 XML_REGEXP_LETTER = 100,
103 XML_REGEXP_LETTER_UPPERCASE,
104 XML_REGEXP_LETTER_LOWERCASE,
105 XML_REGEXP_LETTER_TITLECASE,
106 XML_REGEXP_LETTER_MODIFIER,
107 XML_REGEXP_LETTER_OTHERS,
109 XML_REGEXP_MARK_NONSPACING,
110 XML_REGEXP_MARK_SPACECOMBINING,
111 XML_REGEXP_MARK_ENCLOSING,
113 XML_REGEXP_NUMBER_DECIMAL,
114 XML_REGEXP_NUMBER_LETTER,
115 XML_REGEXP_NUMBER_OTHERS,
117 XML_REGEXP_PUNCT_CONNECTOR,
118 XML_REGEXP_PUNCT_DASH,
119 XML_REGEXP_PUNCT_OPEN,
120 XML_REGEXP_PUNCT_CLOSE,
121 XML_REGEXP_PUNCT_INITQUOTE,
122 XML_REGEXP_PUNCT_FINQUOTE,
123 XML_REGEXP_PUNCT_OTHERS,
125 XML_REGEXP_SEPAR_SPACE,
126 XML_REGEXP_SEPAR_LINE,
127 XML_REGEXP_SEPAR_PARA,
129 XML_REGEXP_SYMBOL_MATH,
130 XML_REGEXP_SYMBOL_CURRENCY,
131 XML_REGEXP_SYMBOL_MODIFIER,
132 XML_REGEXP_SYMBOL_OTHERS,
134 XML_REGEXP_OTHER_CONTROL,
135 XML_REGEXP_OTHER_FORMAT,
136 XML_REGEXP_OTHER_PRIVATE,
138 XML_REGEXP_BLOCK_NAME
142 XML_REGEXP_QUANT_EPSILON = 1,
143 XML_REGEXP_QUANT_ONCE,
144 XML_REGEXP_QUANT_OPT,
145 XML_REGEXP_QUANT_MULT,
146 XML_REGEXP_QUANT_PLUS,
147 XML_REGEXP_QUANT_ONCEONLY,
148 XML_REGEXP_QUANT_ALL,
149 XML_REGEXP_QUANT_RANGE
153 XML_REGEXP_START_STATE = 1,
154 XML_REGEXP_FINAL_STATE,
155 XML_REGEXP_TRANS_STATE,
156 XML_REGEXP_SINK_STATE,
157 XML_REGEXP_UNREACH_STATE
161 XML_REGEXP_MARK_NORMAL = 0,
162 XML_REGEXP_MARK_START,
163 XML_REGEXP_MARK_VISITED
166 typedef struct _xmlRegRange xmlRegRange;
167 typedef xmlRegRange *xmlRegRangePtr;
169 struct _xmlRegRange {
170 int neg; /* 0 normal, 1 not, 2 exclude */
177 typedef struct _xmlRegAtom xmlRegAtom;
178 typedef xmlRegAtom *xmlRegAtomPtr;
180 typedef struct _xmlAutomataState xmlRegState;
181 typedef xmlRegState *xmlRegStatePtr;
186 xmlRegQuantType quant;
194 xmlRegStatePtr start;
195 xmlRegStatePtr start0;
199 xmlRegRangePtr *ranges;
203 typedef struct _xmlRegCounter xmlRegCounter;
204 typedef xmlRegCounter *xmlRegCounterPtr;
206 struct _xmlRegCounter {
211 typedef struct _xmlRegTrans xmlRegTrans;
212 typedef xmlRegTrans *xmlRegTransPtr;
214 struct _xmlRegTrans {
222 struct _xmlAutomataState {
223 xmlRegStateType type;
224 xmlRegMarkedType mark;
225 xmlRegMarkedType markd;
226 xmlRegMarkedType reached;
231 /* knowing states ponting to us can speed things up */
237 typedef struct _xmlAutomata xmlRegParserCtxt;
238 typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
240 #define AM_AUTOMATA_RNG 1
242 struct _xmlAutomata {
249 xmlRegStatePtr start;
251 xmlRegStatePtr state;
257 xmlRegAtomPtr *atoms;
261 xmlRegStatePtr *states;
265 xmlRegCounter *counters;
275 xmlRegStatePtr *states;
277 xmlRegAtomPtr *atoms;
279 xmlRegCounter *counters;
283 * That's the compact form for determinists automatas
292 typedef struct _xmlRegExecRollback xmlRegExecRollback;
293 typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
295 struct _xmlRegExecRollback {
296 xmlRegStatePtr state;/* the current state */
297 int index; /* the index in the input stack */
298 int nextbranch; /* the next transition to explore in that state */
299 int *counts; /* save the automata state if it has some */
302 typedef struct _xmlRegInputToken xmlRegInputToken;
303 typedef xmlRegInputToken *xmlRegInputTokenPtr;
305 struct _xmlRegInputToken {
310 struct _xmlRegExecCtxt {
311 int status; /* execution status != 0 indicate an error */
312 int determinist; /* did we find an indeterministic behaviour */
313 xmlRegexpPtr comp; /* the compiled regexp */
314 xmlRegExecCallbacks callback;
317 xmlRegStatePtr state;/* the current state */
318 int transno; /* the current transition on that state */
319 int transcount; /* the number of chars in char counted transitions */
322 * A stack of rollback states
326 xmlRegExecRollback *rollbacks;
329 * The state of the automata if any
340 const xmlChar *inputString; /* when operating on characters */
341 xmlRegInputTokenPtr inputStack;/* when operating on strings */
346 int errStateNo; /* the error state number */
347 xmlRegStatePtr errState; /* the error state */
348 xmlChar *errString; /* the string raising the error */
349 int *errCounts; /* counters at the error state */
353 #define REGEXP_ALL_COUNTER 0x123456
354 #define REGEXP_ALL_LAX_COUNTER 0x123457
356 static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
357 static void xmlRegFreeState(xmlRegStatePtr state);
358 static void xmlRegFreeAtom(xmlRegAtomPtr atom);
359 static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
360 static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
361 static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
362 int neg, int start, int end, const xmlChar *blockName);
364 void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
366 /************************************************************************
368 * Regexp memory error handler *
370 ************************************************************************/
372 * xmlRegexpErrMemory:
373 * @extra: extra information
375 * Handle an out of memory condition
378 xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
380 const char *regexp = NULL;
382 regexp = (const char *) ctxt->string;
383 ctxt->error = XML_ERR_NO_MEMORY;
385 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
386 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
388 "Memory allocation failed : %s\n", extra);
392 * xmlRegexpErrCompile:
393 * @extra: extra information
395 * Handle a compilation failure
398 xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
400 const char *regexp = NULL;
404 regexp = (const char *) ctxt->string;
405 idx = ctxt->cur - ctxt->string;
406 ctxt->error = XML_REGEXP_COMPILE_ERROR;
408 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
409 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
410 regexp, NULL, idx, 0,
411 "failed to compile: %s\n", extra);
414 /************************************************************************
416 * Allocation/Deallocation *
418 ************************************************************************/
420 static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
422 * xmlRegEpxFromParse:
423 * @ctxt: the parser context used to build it
425 * Allocate a new regexp and fill it with the result from the parser
427 * Returns the new regexp or NULL in case of error
430 xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
433 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
435 xmlRegexpErrMemory(ctxt, "compiling regexp");
438 memset(ret, 0, sizeof(xmlRegexp));
439 ret->string = ctxt->string;
440 ret->nbStates = ctxt->nbStates;
441 ret->states = ctxt->states;
442 ret->nbAtoms = ctxt->nbAtoms;
443 ret->atoms = ctxt->atoms;
444 ret->nbCounters = ctxt->nbCounters;
445 ret->counters = ctxt->counters;
446 ret->determinist = ctxt->determinist;
447 ret->flags = ctxt->flags;
448 if (ret->determinist == -1) {
449 xmlRegexpIsDeterminist(ret);
452 if ((ret->determinist != 0) &&
453 (ret->nbCounters == 0) &&
455 (ret->atoms != NULL) &&
456 (ret->atoms[0] != NULL) &&
457 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
458 int i, j, nbstates = 0, nbatoms = 0;
467 * Switch to a compact representation
468 * 1/ counting the effective number of states left
469 * 2/ counting the unique number of atoms, and check that
470 * they are all of the string type
471 * 3/ build a table state x atom for the transitions
474 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
475 if (stateRemap == NULL) {
476 xmlRegexpErrMemory(ctxt, "compiling regexp");
480 for (i = 0;i < ret->nbStates;i++) {
481 if (ret->states[i] != NULL) {
482 stateRemap[i] = nbstates;
488 #ifdef DEBUG_COMPACTION
489 printf("Final: %d states\n", nbstates);
491 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
492 if (stringMap == NULL) {
493 xmlRegexpErrMemory(ctxt, "compiling regexp");
498 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
499 if (stringRemap == NULL) {
500 xmlRegexpErrMemory(ctxt, "compiling regexp");
506 for (i = 0;i < ret->nbAtoms;i++) {
507 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
508 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
509 value = ret->atoms[i]->valuep;
510 for (j = 0;j < nbatoms;j++) {
511 if (xmlStrEqual(stringMap[j], value)) {
517 stringRemap[i] = nbatoms;
518 stringMap[nbatoms] = xmlStrdup(value);
519 if (stringMap[nbatoms] == NULL) {
520 for (i = 0;i < nbatoms;i++)
521 xmlFree(stringMap[i]);
522 xmlFree(stringRemap);
532 xmlFree(stringRemap);
533 for (i = 0;i < nbatoms;i++)
534 xmlFree(stringMap[i]);
540 #ifdef DEBUG_COMPACTION
541 printf("Final: %d atoms\n", nbatoms);
543 transitions = (int *) xmlMalloc((nbstates + 1) *
544 (nbatoms + 1) * sizeof(int));
545 if (transitions == NULL) {
547 xmlFree(stringRemap);
552 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
555 * Allocate the transition table. The first entry for each
556 * state corresponds to the state type.
560 for (i = 0;i < ret->nbStates;i++) {
561 int stateno, atomno, targetno, prev;
562 xmlRegStatePtr state;
563 xmlRegTransPtr trans;
565 stateno = stateRemap[i];
568 state = ret->states[i];
570 transitions[stateno * (nbatoms + 1)] = state->type;
572 for (j = 0;j < state->nbTrans;j++) {
573 trans = &(state->trans[j]);
574 if ((trans->to == -1) || (trans->atom == NULL))
576 atomno = stringRemap[trans->atom->no];
577 if ((trans->atom->data != NULL) && (transdata == NULL)) {
578 transdata = (void **) xmlMalloc(nbstates * nbatoms *
580 if (transdata != NULL)
582 nbstates * nbatoms * sizeof(void *));
584 xmlRegexpErrMemory(ctxt, "compiling regexp");
588 targetno = stateRemap[trans->to];
590 * if the same atom can generate transitions to 2 different
591 * states then it means the automata is not determinist and
592 * the compact form can't be used !
594 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
596 if (prev != targetno + 1) {
597 ret->determinist = 0;
598 #ifdef DEBUG_COMPACTION
599 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
600 i, j, trans->atom->no, trans->to, atomno, targetno);
601 printf(" previous to is %d\n", prev);
603 if (transdata != NULL)
605 xmlFree(transitions);
607 xmlFree(stringRemap);
608 for (i = 0;i < nbatoms;i++)
609 xmlFree(stringMap[i]);
615 printf("State %d trans %d: atom %d to %d : %d to %d\n",
616 i, j, trans->atom->no, trans->to, atomno, targetno);
618 transitions[stateno * (nbatoms + 1) + atomno + 1] =
619 targetno + 1; /* to avoid 0 */
620 if (transdata != NULL)
621 transdata[stateno * nbatoms + atomno] =
626 ret->determinist = 1;
627 #ifdef DEBUG_COMPACTION
631 for (i = 0;i < nbstates;i++) {
632 for (j = 0;j < nbatoms + 1;j++) {
633 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
640 * Cleanup of the old data
642 if (ret->states != NULL) {
643 for (i = 0;i < ret->nbStates;i++)
644 xmlRegFreeState(ret->states[i]);
645 xmlFree(ret->states);
649 if (ret->atoms != NULL) {
650 for (i = 0;i < ret->nbAtoms;i++)
651 xmlRegFreeAtom(ret->atoms[i]);
657 ret->compact = transitions;
658 ret->transdata = transdata;
659 ret->stringMap = stringMap;
660 ret->nbstrings = nbatoms;
661 ret->nbstates = nbstates;
663 xmlFree(stringRemap);
671 ctxt->nbCounters = 0;
672 ctxt->counters = NULL;
677 * xmlRegNewParserCtxt:
678 * @string: the string to parse
680 * Allocate a new regexp parser context
682 * Returns the new context or NULL in case of error
684 static xmlRegParserCtxtPtr
685 xmlRegNewParserCtxt(const xmlChar *string) {
686 xmlRegParserCtxtPtr ret;
688 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
691 memset(ret, 0, sizeof(xmlRegParserCtxt));
693 ret->string = xmlStrdup(string);
694 ret->cur = ret->string;
698 ret->determinist = -1;
704 * @ctxt: the regexp parser context
705 * @neg: is that negative
706 * @type: the type of range
707 * @start: the start codepoint
708 * @end: the end codepoint
710 * Allocate a new regexp range
712 * Returns the new range or NULL in case of error
714 static xmlRegRangePtr
715 xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
716 int neg, xmlRegAtomType type, int start, int end) {
719 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
721 xmlRegexpErrMemory(ctxt, "allocating range");
733 * @range: the regexp range
735 * Free a regexp range
738 xmlRegFreeRange(xmlRegRangePtr range) {
742 if (range->blockName != NULL)
743 xmlFree(range->blockName);
749 * @range: the regexp range
751 * Copy a regexp range
753 * Returns the new copy or NULL in case of error.
755 static xmlRegRangePtr
756 xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
762 ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
766 if (range->blockName != NULL) {
767 ret->blockName = xmlStrdup(range->blockName);
768 if (ret->blockName == NULL) {
769 xmlRegexpErrMemory(ctxt, "allocating range");
770 xmlRegFreeRange(ret);
779 * @ctxt: the regexp parser context
780 * @type: the type of atom
782 * Allocate a new atom
784 * Returns the new atom or NULL in case of error
787 xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
790 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
792 xmlRegexpErrMemory(ctxt, "allocating atom");
795 memset(ret, 0, sizeof(xmlRegAtom));
797 ret->quant = XML_REGEXP_QUANT_ONCE;
805 * @atom: the regexp atom
810 xmlRegFreeAtom(xmlRegAtomPtr atom) {
816 for (i = 0;i < atom->nbRanges;i++)
817 xmlRegFreeRange(atom->ranges[i]);
818 if (atom->ranges != NULL)
819 xmlFree(atom->ranges);
820 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
821 xmlFree(atom->valuep);
822 if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
823 xmlFree(atom->valuep2);
824 if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
825 xmlFree(atom->valuep);
831 * @ctxt: the regexp parser context
832 * @atom: the oiginal atom
834 * Allocate a new regexp range
836 * Returns the new atom or NULL in case of error
839 xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
842 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
844 xmlRegexpErrMemory(ctxt, "copying atom");
847 memset(ret, 0, sizeof(xmlRegAtom));
848 ret->type = atom->type;
849 ret->quant = atom->quant;
850 ret->min = atom->min;
851 ret->max = atom->max;
852 if (atom->nbRanges > 0) {
855 ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
857 if (ret->ranges == NULL) {
858 xmlRegexpErrMemory(ctxt, "copying atom");
861 for (i = 0;i < atom->nbRanges;i++) {
862 ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
863 if (ret->ranges[i] == NULL)
865 ret->nbRanges = i + 1;
875 static xmlRegStatePtr
876 xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
879 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
881 xmlRegexpErrMemory(ctxt, "allocating state");
884 memset(ret, 0, sizeof(xmlRegState));
885 ret->type = XML_REGEXP_TRANS_STATE;
886 ret->mark = XML_REGEXP_MARK_NORMAL;
892 * @state: the regexp state
894 * Free a regexp state
897 xmlRegFreeState(xmlRegStatePtr state) {
901 if (state->trans != NULL)
902 xmlFree(state->trans);
903 if (state->transTo != NULL)
904 xmlFree(state->transTo);
909 * xmlRegFreeParserCtxt:
910 * @ctxt: the regexp parser context
912 * Free a regexp parser context
915 xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
920 if (ctxt->string != NULL)
921 xmlFree(ctxt->string);
922 if (ctxt->states != NULL) {
923 for (i = 0;i < ctxt->nbStates;i++)
924 xmlRegFreeState(ctxt->states[i]);
925 xmlFree(ctxt->states);
927 if (ctxt->atoms != NULL) {
928 for (i = 0;i < ctxt->nbAtoms;i++)
929 xmlRegFreeAtom(ctxt->atoms[i]);
930 xmlFree(ctxt->atoms);
932 if (ctxt->counters != NULL)
933 xmlFree(ctxt->counters);
937 /************************************************************************
939 * Display of Data structures *
941 ************************************************************************/
944 xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
946 case XML_REGEXP_EPSILON:
947 fprintf(output, "epsilon "); break;
948 case XML_REGEXP_CHARVAL:
949 fprintf(output, "charval "); break;
950 case XML_REGEXP_RANGES:
951 fprintf(output, "ranges "); break;
952 case XML_REGEXP_SUBREG:
953 fprintf(output, "subexpr "); break;
954 case XML_REGEXP_STRING:
955 fprintf(output, "string "); break;
956 case XML_REGEXP_ANYCHAR:
957 fprintf(output, "anychar "); break;
958 case XML_REGEXP_ANYSPACE:
959 fprintf(output, "anyspace "); break;
960 case XML_REGEXP_NOTSPACE:
961 fprintf(output, "notspace "); break;
962 case XML_REGEXP_INITNAME:
963 fprintf(output, "initname "); break;
964 case XML_REGEXP_NOTINITNAME:
965 fprintf(output, "notinitname "); break;
966 case XML_REGEXP_NAMECHAR:
967 fprintf(output, "namechar "); break;
968 case XML_REGEXP_NOTNAMECHAR:
969 fprintf(output, "notnamechar "); break;
970 case XML_REGEXP_DECIMAL:
971 fprintf(output, "decimal "); break;
972 case XML_REGEXP_NOTDECIMAL:
973 fprintf(output, "notdecimal "); break;
974 case XML_REGEXP_REALCHAR:
975 fprintf(output, "realchar "); break;
976 case XML_REGEXP_NOTREALCHAR:
977 fprintf(output, "notrealchar "); break;
978 case XML_REGEXP_LETTER:
979 fprintf(output, "LETTER "); break;
980 case XML_REGEXP_LETTER_UPPERCASE:
981 fprintf(output, "LETTER_UPPERCASE "); break;
982 case XML_REGEXP_LETTER_LOWERCASE:
983 fprintf(output, "LETTER_LOWERCASE "); break;
984 case XML_REGEXP_LETTER_TITLECASE:
985 fprintf(output, "LETTER_TITLECASE "); break;
986 case XML_REGEXP_LETTER_MODIFIER:
987 fprintf(output, "LETTER_MODIFIER "); break;
988 case XML_REGEXP_LETTER_OTHERS:
989 fprintf(output, "LETTER_OTHERS "); break;
990 case XML_REGEXP_MARK:
991 fprintf(output, "MARK "); break;
992 case XML_REGEXP_MARK_NONSPACING:
993 fprintf(output, "MARK_NONSPACING "); break;
994 case XML_REGEXP_MARK_SPACECOMBINING:
995 fprintf(output, "MARK_SPACECOMBINING "); break;
996 case XML_REGEXP_MARK_ENCLOSING:
997 fprintf(output, "MARK_ENCLOSING "); break;
998 case XML_REGEXP_NUMBER:
999 fprintf(output, "NUMBER "); break;
1000 case XML_REGEXP_NUMBER_DECIMAL:
1001 fprintf(output, "NUMBER_DECIMAL "); break;
1002 case XML_REGEXP_NUMBER_LETTER:
1003 fprintf(output, "NUMBER_LETTER "); break;
1004 case XML_REGEXP_NUMBER_OTHERS:
1005 fprintf(output, "NUMBER_OTHERS "); break;
1006 case XML_REGEXP_PUNCT:
1007 fprintf(output, "PUNCT "); break;
1008 case XML_REGEXP_PUNCT_CONNECTOR:
1009 fprintf(output, "PUNCT_CONNECTOR "); break;
1010 case XML_REGEXP_PUNCT_DASH:
1011 fprintf(output, "PUNCT_DASH "); break;
1012 case XML_REGEXP_PUNCT_OPEN:
1013 fprintf(output, "PUNCT_OPEN "); break;
1014 case XML_REGEXP_PUNCT_CLOSE:
1015 fprintf(output, "PUNCT_CLOSE "); break;
1016 case XML_REGEXP_PUNCT_INITQUOTE:
1017 fprintf(output, "PUNCT_INITQUOTE "); break;
1018 case XML_REGEXP_PUNCT_FINQUOTE:
1019 fprintf(output, "PUNCT_FINQUOTE "); break;
1020 case XML_REGEXP_PUNCT_OTHERS:
1021 fprintf(output, "PUNCT_OTHERS "); break;
1022 case XML_REGEXP_SEPAR:
1023 fprintf(output, "SEPAR "); break;
1024 case XML_REGEXP_SEPAR_SPACE:
1025 fprintf(output, "SEPAR_SPACE "); break;
1026 case XML_REGEXP_SEPAR_LINE:
1027 fprintf(output, "SEPAR_LINE "); break;
1028 case XML_REGEXP_SEPAR_PARA:
1029 fprintf(output, "SEPAR_PARA "); break;
1030 case XML_REGEXP_SYMBOL:
1031 fprintf(output, "SYMBOL "); break;
1032 case XML_REGEXP_SYMBOL_MATH:
1033 fprintf(output, "SYMBOL_MATH "); break;
1034 case XML_REGEXP_SYMBOL_CURRENCY:
1035 fprintf(output, "SYMBOL_CURRENCY "); break;
1036 case XML_REGEXP_SYMBOL_MODIFIER:
1037 fprintf(output, "SYMBOL_MODIFIER "); break;
1038 case XML_REGEXP_SYMBOL_OTHERS:
1039 fprintf(output, "SYMBOL_OTHERS "); break;
1040 case XML_REGEXP_OTHER:
1041 fprintf(output, "OTHER "); break;
1042 case XML_REGEXP_OTHER_CONTROL:
1043 fprintf(output, "OTHER_CONTROL "); break;
1044 case XML_REGEXP_OTHER_FORMAT:
1045 fprintf(output, "OTHER_FORMAT "); break;
1046 case XML_REGEXP_OTHER_PRIVATE:
1047 fprintf(output, "OTHER_PRIVATE "); break;
1048 case XML_REGEXP_OTHER_NA:
1049 fprintf(output, "OTHER_NA "); break;
1050 case XML_REGEXP_BLOCK_NAME:
1051 fprintf(output, "BLOCK "); break;
1056 xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
1058 case XML_REGEXP_QUANT_EPSILON:
1059 fprintf(output, "epsilon "); break;
1060 case XML_REGEXP_QUANT_ONCE:
1061 fprintf(output, "once "); break;
1062 case XML_REGEXP_QUANT_OPT:
1063 fprintf(output, "? "); break;
1064 case XML_REGEXP_QUANT_MULT:
1065 fprintf(output, "* "); break;
1066 case XML_REGEXP_QUANT_PLUS:
1067 fprintf(output, "+ "); break;
1068 case XML_REGEXP_QUANT_RANGE:
1069 fprintf(output, "range "); break;
1070 case XML_REGEXP_QUANT_ONCEONLY:
1071 fprintf(output, "onceonly "); break;
1072 case XML_REGEXP_QUANT_ALL:
1073 fprintf(output, "all "); break;
1077 xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
1078 fprintf(output, " range: ");
1080 fprintf(output, "negative ");
1081 xmlRegPrintAtomType(output, range->type);
1082 fprintf(output, "%c - %c\n", range->start, range->end);
1086 xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
1087 fprintf(output, " atom: ");
1089 fprintf(output, "NULL\n");
1093 fprintf(output, "not ");
1094 xmlRegPrintAtomType(output, atom->type);
1095 xmlRegPrintQuantType(output, atom->quant);
1096 if (atom->quant == XML_REGEXP_QUANT_RANGE)
1097 fprintf(output, "%d-%d ", atom->min, atom->max);
1098 if (atom->type == XML_REGEXP_STRING)
1099 fprintf(output, "'%s' ", (char *) atom->valuep);
1100 if (atom->type == XML_REGEXP_CHARVAL)
1101 fprintf(output, "char %c\n", atom->codepoint);
1102 else if (atom->type == XML_REGEXP_RANGES) {
1104 fprintf(output, "%d entries\n", atom->nbRanges);
1105 for (i = 0; i < atom->nbRanges;i++)
1106 xmlRegPrintRange(output, atom->ranges[i]);
1107 } else if (atom->type == XML_REGEXP_SUBREG) {
1108 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1110 fprintf(output, "\n");
1115 xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1116 fprintf(output, " trans: ");
1117 if (trans == NULL) {
1118 fprintf(output, "NULL\n");
1121 if (trans->to < 0) {
1122 fprintf(output, "removed\n");
1125 if (trans->nd != 0) {
1127 fprintf(output, "last not determinist, ");
1129 fprintf(output, "not determinist, ");
1131 if (trans->counter >= 0) {
1132 fprintf(output, "counted %d, ", trans->counter);
1134 if (trans->count == REGEXP_ALL_COUNTER) {
1135 fprintf(output, "all transition, ");
1136 } else if (trans->count >= 0) {
1137 fprintf(output, "count based %d, ", trans->count);
1139 if (trans->atom == NULL) {
1140 fprintf(output, "epsilon to %d\n", trans->to);
1143 if (trans->atom->type == XML_REGEXP_CHARVAL)
1144 fprintf(output, "char %c ", trans->atom->codepoint);
1145 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1149 xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1152 fprintf(output, " state: ");
1153 if (state == NULL) {
1154 fprintf(output, "NULL\n");
1157 if (state->type == XML_REGEXP_START_STATE)
1158 fprintf(output, "START ");
1159 if (state->type == XML_REGEXP_FINAL_STATE)
1160 fprintf(output, "FINAL ");
1162 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1163 for (i = 0;i < state->nbTrans; i++) {
1164 xmlRegPrintTrans(output, &(state->trans[i]));
1168 #ifdef DEBUG_REGEXP_GRAPH
1170 xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1173 fprintf(output, " ctxt: ");
1175 fprintf(output, "NULL\n");
1178 fprintf(output, "'%s' ", ctxt->string);
1180 fprintf(output, "error ");
1182 fprintf(output, "neg ");
1183 fprintf(output, "\n");
1184 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1185 for (i = 0;i < ctxt->nbAtoms; i++) {
1186 fprintf(output, " %02d ", i);
1187 xmlRegPrintAtom(output, ctxt->atoms[i]);
1189 if (ctxt->atom != NULL) {
1190 fprintf(output, "current atom:\n");
1191 xmlRegPrintAtom(output, ctxt->atom);
1193 fprintf(output, "%d states:", ctxt->nbStates);
1194 if (ctxt->start != NULL)
1195 fprintf(output, " start: %d", ctxt->start->no);
1196 if (ctxt->end != NULL)
1197 fprintf(output, " end: %d", ctxt->end->no);
1198 fprintf(output, "\n");
1199 for (i = 0;i < ctxt->nbStates; i++) {
1200 xmlRegPrintState(output, ctxt->states[i]);
1202 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1203 for (i = 0;i < ctxt->nbCounters; i++) {
1204 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1205 ctxt->counters[i].max);
1210 /************************************************************************
1212 * Finite Automata structures manipulations *
1214 ************************************************************************/
1217 xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1218 int neg, xmlRegAtomType type, int start, int end,
1219 xmlChar *blockName) {
1220 xmlRegRangePtr range;
1223 ERROR("add range: atom is NULL");
1226 if (atom->type != XML_REGEXP_RANGES) {
1227 ERROR("add range: atom is not ranges");
1230 if (atom->maxRanges == 0) {
1231 atom->maxRanges = 4;
1232 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1233 sizeof(xmlRegRangePtr));
1234 if (atom->ranges == NULL) {
1235 xmlRegexpErrMemory(ctxt, "adding ranges");
1236 atom->maxRanges = 0;
1239 } else if (atom->nbRanges >= atom->maxRanges) {
1240 xmlRegRangePtr *tmp;
1241 atom->maxRanges *= 2;
1242 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1243 sizeof(xmlRegRangePtr));
1245 xmlRegexpErrMemory(ctxt, "adding ranges");
1246 atom->maxRanges /= 2;
1251 range = xmlRegNewRange(ctxt, neg, type, start, end);
1254 range->blockName = blockName;
1255 atom->ranges[atom->nbRanges++] = range;
1260 xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1261 if (ctxt->maxCounters == 0) {
1262 ctxt->maxCounters = 4;
1263 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1264 sizeof(xmlRegCounter));
1265 if (ctxt->counters == NULL) {
1266 xmlRegexpErrMemory(ctxt, "allocating counter");
1267 ctxt->maxCounters = 0;
1270 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1272 ctxt->maxCounters *= 2;
1273 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1274 sizeof(xmlRegCounter));
1276 xmlRegexpErrMemory(ctxt, "allocating counter");
1277 ctxt->maxCounters /= 2;
1280 ctxt->counters = tmp;
1282 ctxt->counters[ctxt->nbCounters].min = -1;
1283 ctxt->counters[ctxt->nbCounters].max = -1;
1284 return(ctxt->nbCounters++);
1288 xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1290 ERROR("atom push: atom is NULL");
1293 if (ctxt->maxAtoms == 0) {
1295 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1296 sizeof(xmlRegAtomPtr));
1297 if (ctxt->atoms == NULL) {
1298 xmlRegexpErrMemory(ctxt, "pushing atom");
1302 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1304 ctxt->maxAtoms *= 2;
1305 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1306 sizeof(xmlRegAtomPtr));
1308 xmlRegexpErrMemory(ctxt, "allocating counter");
1309 ctxt->maxAtoms /= 2;
1314 atom->no = ctxt->nbAtoms;
1315 ctxt->atoms[ctxt->nbAtoms++] = atom;
1320 xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1322 if (target->maxTransTo == 0) {
1323 target->maxTransTo = 8;
1324 target->transTo = (int *) xmlMalloc(target->maxTransTo *
1326 if (target->transTo == NULL) {
1327 xmlRegexpErrMemory(ctxt, "adding transition");
1328 target->maxTransTo = 0;
1331 } else if (target->nbTransTo >= target->maxTransTo) {
1333 target->maxTransTo *= 2;
1334 tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1337 xmlRegexpErrMemory(ctxt, "adding transition");
1338 target->maxTransTo /= 2;
1341 target->transTo = tmp;
1343 target->transTo[target->nbTransTo] = from;
1344 target->nbTransTo++;
1348 xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1349 xmlRegAtomPtr atom, xmlRegStatePtr target,
1350 int counter, int count) {
1354 if (state == NULL) {
1355 ERROR("add state: state is NULL");
1358 if (target == NULL) {
1359 ERROR("add state: target is NULL");
1363 * Other routines follow the philosophy 'When in doubt, add a transition'
1364 * so we check here whether such a transition is already present and, if
1365 * so, silently ignore this request.
1368 for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1369 xmlRegTransPtr trans = &(state->trans[nrtrans]);
1370 if ((trans->atom == atom) &&
1371 (trans->to == target->no) &&
1372 (trans->counter == counter) &&
1373 (trans->count == count)) {
1374 #ifdef DEBUG_REGEXP_GRAPH
1375 printf("Ignoring duplicate transition from %d to %d\n",
1376 state->no, target->no);
1382 if (state->maxTrans == 0) {
1383 state->maxTrans = 8;
1384 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1385 sizeof(xmlRegTrans));
1386 if (state->trans == NULL) {
1387 xmlRegexpErrMemory(ctxt, "adding transition");
1388 state->maxTrans = 0;
1391 } else if (state->nbTrans >= state->maxTrans) {
1393 state->maxTrans *= 2;
1394 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1395 sizeof(xmlRegTrans));
1397 xmlRegexpErrMemory(ctxt, "adding transition");
1398 state->maxTrans /= 2;
1403 #ifdef DEBUG_REGEXP_GRAPH
1404 printf("Add trans from %d to %d ", state->no, target->no);
1405 if (count == REGEXP_ALL_COUNTER)
1406 printf("all transition\n");
1407 else if (count >= 0)
1408 printf("count based %d\n", count);
1409 else if (counter >= 0)
1410 printf("counted %d\n", counter);
1411 else if (atom == NULL)
1412 printf("epsilon transition\n");
1413 else if (atom != NULL)
1414 xmlRegPrintAtom(stdout, atom);
1417 state->trans[state->nbTrans].atom = atom;
1418 state->trans[state->nbTrans].to = target->no;
1419 state->trans[state->nbTrans].counter = counter;
1420 state->trans[state->nbTrans].count = count;
1421 state->trans[state->nbTrans].nd = 0;
1423 xmlRegStateAddTransTo(ctxt, target, state->no);
1427 xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
1428 if (state == NULL) return(-1);
1429 if (ctxt->maxStates == 0) {
1430 ctxt->maxStates = 4;
1431 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1432 sizeof(xmlRegStatePtr));
1433 if (ctxt->states == NULL) {
1434 xmlRegexpErrMemory(ctxt, "adding state");
1435 ctxt->maxStates = 0;
1438 } else if (ctxt->nbStates >= ctxt->maxStates) {
1439 xmlRegStatePtr *tmp;
1440 ctxt->maxStates *= 2;
1441 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1442 sizeof(xmlRegStatePtr));
1444 xmlRegexpErrMemory(ctxt, "adding state");
1445 ctxt->maxStates /= 2;
1450 state->no = ctxt->nbStates;
1451 ctxt->states[ctxt->nbStates++] = state;
1456 * xmlFAGenerateAllTransition:
1457 * @ctxt: a regexp parser context
1458 * @from: the from state
1459 * @to: the target state or NULL for building a new one
1464 xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
1465 xmlRegStatePtr from, xmlRegStatePtr to,
1468 to = xmlRegNewState(ctxt);
1469 xmlRegStatePush(ctxt, to);
1473 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1475 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
1479 * xmlFAGenerateEpsilonTransition:
1480 * @ctxt: a regexp parser context
1481 * @from: the from state
1482 * @to: the target state or NULL for building a new one
1486 xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1487 xmlRegStatePtr from, xmlRegStatePtr to) {
1489 to = xmlRegNewState(ctxt);
1490 xmlRegStatePush(ctxt, to);
1493 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1497 * xmlFAGenerateCountedEpsilonTransition:
1498 * @ctxt: a regexp parser context
1499 * @from: the from state
1500 * @to: the target state or NULL for building a new one
1501 * counter: the counter for that transition
1505 xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1506 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1508 to = xmlRegNewState(ctxt);
1509 xmlRegStatePush(ctxt, to);
1512 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1516 * xmlFAGenerateCountedTransition:
1517 * @ctxt: a regexp parser context
1518 * @from: the from state
1519 * @to: the target state or NULL for building a new one
1520 * counter: the counter for that transition
1524 xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1525 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1527 to = xmlRegNewState(ctxt);
1528 xmlRegStatePush(ctxt, to);
1531 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1535 * xmlFAGenerateTransitions:
1536 * @ctxt: a regexp parser context
1537 * @from: the from state
1538 * @to: the target state or NULL for building a new one
1539 * @atom: the atom generating the transition
1541 * Returns 0 if success and -1 in case of error.
1544 xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1545 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1550 ERROR("genrate transition: atom == NULL");
1553 if (atom->type == XML_REGEXP_SUBREG) {
1555 * this is a subexpression handling one should not need to
1556 * create a new node except for XML_REGEXP_QUANT_RANGE.
1558 if (xmlRegAtomPush(ctxt, atom) < 0) {
1561 if ((to != NULL) && (atom->stop != to) &&
1562 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1564 * Generate an epsilon transition to link to the target
1566 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1568 } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1569 (atom->quant != XML_REGEXP_QUANT_ONCE)) {
1570 to = xmlRegNewState(ctxt);
1571 xmlRegStatePush(ctxt, to);
1573 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1576 switch (atom->quant) {
1577 case XML_REGEXP_QUANT_OPT:
1578 atom->quant = XML_REGEXP_QUANT_ONCE;
1580 * transition done to the state after end of atom.
1581 * 1. set transition from atom start to new state
1582 * 2. set transition from atom end to this state.
1585 xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1586 xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
1589 xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
1592 case XML_REGEXP_QUANT_MULT:
1593 atom->quant = XML_REGEXP_QUANT_ONCE;
1594 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1595 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1597 case XML_REGEXP_QUANT_PLUS:
1598 atom->quant = XML_REGEXP_QUANT_ONCE;
1599 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1601 case XML_REGEXP_QUANT_RANGE: {
1603 xmlRegStatePtr inter, newstate;
1606 * create the final state now if needed
1611 newstate = xmlRegNewState(ctxt);
1612 xmlRegStatePush(ctxt, newstate);
1616 * The principle here is to use counted transition
1617 * to avoid explosion in the number of states in the
1618 * graph. This is clearly more complex but should not
1619 * be exploitable at runtime.
1621 if ((atom->min == 0) && (atom->start0 == NULL)) {
1624 * duplicate a transition based on atom to count next
1625 * occurences after 1. We cannot loop to atom->start
1626 * directly because we need an epsilon transition to
1629 /* ???? For some reason it seems we never reach that
1630 case, I suppose this got optimized out before when
1631 building the automata */
1632 copy = xmlRegCopyAtom(ctxt, atom);
1635 copy->quant = XML_REGEXP_QUANT_ONCE;
1639 if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1642 inter = ctxt->state;
1643 counter = xmlRegGetCounter(ctxt);
1644 ctxt->counters[counter].min = atom->min - 1;
1645 ctxt->counters[counter].max = atom->max - 1;
1646 /* count the number of times we see it again */
1647 xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1648 atom->stop, counter);
1649 /* allow a way out based on the count */
1650 xmlFAGenerateCountedTransition(ctxt, inter,
1652 /* and also allow a direct exit for 0 */
1653 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1657 * either we need the atom at least once or there
1658 * is an atom->start0 allowing to easilly plug the
1659 * epsilon transition.
1661 counter = xmlRegGetCounter(ctxt);
1662 ctxt->counters[counter].min = atom->min - 1;
1663 ctxt->counters[counter].max = atom->max - 1;
1664 /* count the number of times we see it again */
1665 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1666 atom->start, counter);
1667 /* allow a way out based on the count */
1668 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1670 /* and if needed allow a direct exit for 0 */
1672 xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1678 atom->quant = XML_REGEXP_QUANT_ONCE;
1679 ctxt->state = newstate;
1686 if ((atom->min == 0) && (atom->max == 0) &&
1687 (atom->quant == XML_REGEXP_QUANT_RANGE)) {
1689 * we can discard the atom and generate an epsilon transition instead
1692 to = xmlRegNewState(ctxt);
1694 xmlRegStatePush(ctxt, to);
1699 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1701 xmlRegFreeAtom(atom);
1705 to = xmlRegNewState(ctxt);
1707 xmlRegStatePush(ctxt, to);
1713 if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
1714 (atom->quant == XML_REGEXP_QUANT_PLUS)) {
1716 * Do not pollute the target state by adding transitions from
1717 * it as it is likely to be the shared target of multiple branches.
1718 * So isolate with an epsilon transition.
1722 tmp = xmlRegNewState(ctxt);
1724 xmlRegStatePush(ctxt, tmp);
1728 xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1731 if (xmlRegAtomPush(ctxt, atom) < 0) {
1734 if ((atom->quant == XML_REGEXP_QUANT_RANGE) &&
1735 (atom->min == 0) && (atom->max > 0)) {
1739 atom->quant = XML_REGEXP_QUANT_OPT;
1741 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1743 switch (atom->quant) {
1744 case XML_REGEXP_QUANT_OPT:
1745 atom->quant = XML_REGEXP_QUANT_ONCE;
1746 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1748 case XML_REGEXP_QUANT_MULT:
1749 atom->quant = XML_REGEXP_QUANT_ONCE;
1750 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1751 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1753 case XML_REGEXP_QUANT_PLUS:
1754 atom->quant = XML_REGEXP_QUANT_ONCE;
1755 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1757 case XML_REGEXP_QUANT_RANGE:
1759 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1768 * xmlFAReduceEpsilonTransitions:
1769 * @ctxt: a regexp parser context
1770 * @fromnr: the from state
1771 * @tonr: the to state
1772 * @counter: should that transition be associated to a counted
1776 xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1777 int tonr, int counter) {
1779 xmlRegStatePtr from;
1782 #ifdef DEBUG_REGEXP_GRAPH
1783 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1785 from = ctxt->states[fromnr];
1788 to = ctxt->states[tonr];
1791 if ((to->mark == XML_REGEXP_MARK_START) ||
1792 (to->mark == XML_REGEXP_MARK_VISITED))
1795 to->mark = XML_REGEXP_MARK_VISITED;
1796 if (to->type == XML_REGEXP_FINAL_STATE) {
1797 #ifdef DEBUG_REGEXP_GRAPH
1798 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1800 from->type = XML_REGEXP_FINAL_STATE;
1802 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1803 if (to->trans[transnr].to < 0)
1805 if (to->trans[transnr].atom == NULL) {
1807 * Don't remove counted transitions
1810 if (to->trans[transnr].to != fromnr) {
1811 if (to->trans[transnr].count >= 0) {
1812 int newto = to->trans[transnr].to;
1814 xmlRegStateAddTrans(ctxt, from, NULL,
1815 ctxt->states[newto],
1816 -1, to->trans[transnr].count);
1818 #ifdef DEBUG_REGEXP_GRAPH
1819 printf("Found epsilon trans %d from %d to %d\n",
1820 transnr, tonr, to->trans[transnr].to);
1822 if (to->trans[transnr].counter >= 0) {
1823 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1824 to->trans[transnr].to,
1825 to->trans[transnr].counter);
1827 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1828 to->trans[transnr].to,
1834 int newto = to->trans[transnr].to;
1836 if (to->trans[transnr].counter >= 0) {
1837 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1838 ctxt->states[newto],
1839 to->trans[transnr].counter, -1);
1841 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1842 ctxt->states[newto], counter, -1);
1846 to->mark = XML_REGEXP_MARK_NORMAL;
1850 * xmlFAEliminateSimpleEpsilonTransitions:
1851 * @ctxt: a regexp parser context
1853 * Eliminating general epsilon transitions can get costly in the general
1854 * algorithm due to the large amount of generated new transitions and
1855 * associated comparisons. However for simple epsilon transition used just
1856 * to separate building blocks when generating the automata this can be
1857 * reduced to state elimination:
1858 * - if there exists an epsilon from X to Y
1859 * - if there is no other transition from X
1860 * then X and Y are semantically equivalent and X can be eliminated
1861 * If X is the start state then make Y the start state, else replace the
1862 * target of all transitions to X by transitions to Y.
1865 xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1866 int statenr, i, j, newto;
1867 xmlRegStatePtr state, tmp;
1869 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1870 state = ctxt->states[statenr];
1873 if (state->nbTrans != 1)
1875 if (state->type == XML_REGEXP_UNREACH_STATE)
1877 /* is the only transition out a basic transition */
1878 if ((state->trans[0].atom == NULL) &&
1879 (state->trans[0].to >= 0) &&
1880 (state->trans[0].to != statenr) &&
1881 (state->trans[0].counter < 0) &&
1882 (state->trans[0].count < 0)) {
1883 newto = state->trans[0].to;
1885 if (state->type == XML_REGEXP_START_STATE) {
1886 #ifdef DEBUG_REGEXP_GRAPH
1887 printf("Found simple epsilon trans from start %d to %d\n",
1891 #ifdef DEBUG_REGEXP_GRAPH
1892 printf("Found simple epsilon trans from %d to %d\n",
1895 for (i = 0;i < state->nbTransTo;i++) {
1896 tmp = ctxt->states[state->transTo[i]];
1897 for (j = 0;j < tmp->nbTrans;j++) {
1898 if (tmp->trans[j].to == statenr) {
1899 #ifdef DEBUG_REGEXP_GRAPH
1900 printf("Changed transition %d on %d to go to %d\n",
1903 tmp->trans[j].to = -1;
1904 xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1905 ctxt->states[newto],
1906 tmp->trans[j].counter,
1907 tmp->trans[j].count);
1911 if (state->type == XML_REGEXP_FINAL_STATE)
1912 ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1913 /* eliminate the transition completely */
1916 state->type = XML_REGEXP_UNREACH_STATE;
1924 * xmlFAEliminateEpsilonTransitions:
1925 * @ctxt: a regexp parser context
1929 xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1930 int statenr, transnr;
1931 xmlRegStatePtr state;
1934 if (ctxt->states == NULL) return;
1937 * Eliminate simple epsilon transition and the associated unreachable
1940 xmlFAEliminateSimpleEpsilonTransitions(ctxt);
1941 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1942 state = ctxt->states[statenr];
1943 if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1944 #ifdef DEBUG_REGEXP_GRAPH
1945 printf("Removed unreachable state %d\n", statenr);
1947 xmlRegFreeState(state);
1948 ctxt->states[statenr] = NULL;
1955 * Build the completed transitions bypassing the epsilons
1956 * Use a marking algorithm to avoid loops
1957 * Mark sink states too.
1958 * Process from the latests states backward to the start when
1959 * there is long cascading epsilon chains this minimize the
1960 * recursions and transition compares when adding the new ones
1962 for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
1963 state = ctxt->states[statenr];
1966 if ((state->nbTrans == 0) &&
1967 (state->type != XML_REGEXP_FINAL_STATE)) {
1968 state->type = XML_REGEXP_SINK_STATE;
1970 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1971 if ((state->trans[transnr].atom == NULL) &&
1972 (state->trans[transnr].to >= 0)) {
1973 if (state->trans[transnr].to == statenr) {
1974 state->trans[transnr].to = -1;
1975 #ifdef DEBUG_REGEXP_GRAPH
1976 printf("Removed loopback epsilon trans %d on %d\n",
1979 } else if (state->trans[transnr].count < 0) {
1980 int newto = state->trans[transnr].to;
1982 #ifdef DEBUG_REGEXP_GRAPH
1983 printf("Found epsilon trans %d from %d to %d\n",
1984 transnr, statenr, newto);
1987 state->trans[transnr].to = -2;
1988 state->mark = XML_REGEXP_MARK_START;
1989 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1990 newto, state->trans[transnr].counter);
1991 state->mark = XML_REGEXP_MARK_NORMAL;
1992 #ifdef DEBUG_REGEXP_GRAPH
1994 printf("Found counted transition %d on %d\n",
2002 * Eliminate the epsilon transitions
2005 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2006 state = ctxt->states[statenr];
2009 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2010 xmlRegTransPtr trans = &(state->trans[transnr]);
2011 if ((trans->atom == NULL) &&
2012 (trans->count < 0) &&
2021 * Use this pass to detect unreachable states too
2023 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2024 state = ctxt->states[statenr];
2026 state->reached = XML_REGEXP_MARK_NORMAL;
2028 state = ctxt->states[0];
2030 state->reached = XML_REGEXP_MARK_START;
2031 while (state != NULL) {
2032 xmlRegStatePtr target = NULL;
2033 state->reached = XML_REGEXP_MARK_VISITED;
2035 * Mark all states reachable from the current reachable state
2037 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2038 if ((state->trans[transnr].to >= 0) &&
2039 ((state->trans[transnr].atom != NULL) ||
2040 (state->trans[transnr].count >= 0))) {
2041 int newto = state->trans[transnr].to;
2043 if (ctxt->states[newto] == NULL)
2045 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2046 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
2047 target = ctxt->states[newto];
2053 * find the next accessible state not explored
2055 if (target == NULL) {
2056 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2057 state = ctxt->states[statenr];
2058 if ((state != NULL) && (state->reached ==
2059 XML_REGEXP_MARK_START)) {
2067 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2068 state = ctxt->states[statenr];
2069 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
2070 #ifdef DEBUG_REGEXP_GRAPH
2071 printf("Removed unreachable state %d\n", statenr);
2073 xmlRegFreeState(state);
2074 ctxt->states[statenr] = NULL;
2081 xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2084 if ((range1->type == XML_REGEXP_RANGES) ||
2085 (range2->type == XML_REGEXP_RANGES) ||
2086 (range2->type == XML_REGEXP_SUBREG) ||
2087 (range1->type == XML_REGEXP_SUBREG) ||
2088 (range1->type == XML_REGEXP_STRING) ||
2089 (range2->type == XML_REGEXP_STRING))
2092 /* put them in order */
2093 if (range1->type > range2->type) {
2100 if ((range1->type == XML_REGEXP_ANYCHAR) ||
2101 (range2->type == XML_REGEXP_ANYCHAR)) {
2103 } else if ((range1->type == XML_REGEXP_EPSILON) ||
2104 (range2->type == XML_REGEXP_EPSILON)) {
2106 } else if (range1->type == range2->type) {
2107 if (range1->type != XML_REGEXP_CHARVAL)
2109 else if ((range1->end < range2->start) ||
2110 (range2->end < range1->start))
2114 } else if (range1->type == XML_REGEXP_CHARVAL) {
2119 * just check all codepoints in the range for acceptance,
2120 * this is usually way cheaper since done only once at
2121 * compilation than testing over and over at runtime or
2122 * pushing too many states when evaluating.
2124 if (((range1->neg == 0) && (range2->neg != 0)) ||
2125 ((range1->neg != 0) && (range2->neg == 0)))
2128 for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2129 ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2130 0, range2->start, range2->end,
2134 if (((neg == 1) && (ret == 0)) ||
2135 ((neg == 0) && (ret == 1)))
2139 } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2140 (range2->type == XML_REGEXP_BLOCK_NAME)) {
2141 if (range1->type == range2->type) {
2142 ret = xmlStrEqual(range1->blockName, range2->blockName);
2145 * comparing a block range with anything else is way
2146 * too costly, and maintining the table is like too much
2147 * memory too, so let's force the automata to save state
2152 } else if ((range1->type < XML_REGEXP_LETTER) ||
2153 (range2->type < XML_REGEXP_LETTER)) {
2154 if ((range1->type == XML_REGEXP_ANYSPACE) &&
2155 (range2->type == XML_REGEXP_NOTSPACE))
2157 else if ((range1->type == XML_REGEXP_INITNAME) &&
2158 (range2->type == XML_REGEXP_NOTINITNAME))
2160 else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2161 (range2->type == XML_REGEXP_NOTNAMECHAR))
2163 else if ((range1->type == XML_REGEXP_DECIMAL) &&
2164 (range2->type == XML_REGEXP_NOTDECIMAL))
2166 else if ((range1->type == XML_REGEXP_REALCHAR) &&
2167 (range2->type == XML_REGEXP_NOTREALCHAR))
2170 /* same thing to limit complexity */
2175 /* range1->type < range2->type here */
2176 switch (range1->type) {
2177 case XML_REGEXP_LETTER:
2178 /* all disjoint except in the subgroups */
2179 if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2180 (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2181 (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2182 (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2183 (range2->type == XML_REGEXP_LETTER_OTHERS))
2186 case XML_REGEXP_MARK:
2187 if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2188 (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2189 (range2->type == XML_REGEXP_MARK_ENCLOSING))
2192 case XML_REGEXP_NUMBER:
2193 if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2194 (range2->type == XML_REGEXP_NUMBER_LETTER) ||
2195 (range2->type == XML_REGEXP_NUMBER_OTHERS))
2198 case XML_REGEXP_PUNCT:
2199 if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2200 (range2->type == XML_REGEXP_PUNCT_DASH) ||
2201 (range2->type == XML_REGEXP_PUNCT_OPEN) ||
2202 (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2203 (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2204 (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2205 (range2->type == XML_REGEXP_PUNCT_OTHERS))
2208 case XML_REGEXP_SEPAR:
2209 if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2210 (range2->type == XML_REGEXP_SEPAR_LINE) ||
2211 (range2->type == XML_REGEXP_SEPAR_PARA))
2214 case XML_REGEXP_SYMBOL:
2215 if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2216 (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2217 (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2218 (range2->type == XML_REGEXP_SYMBOL_OTHERS))
2221 case XML_REGEXP_OTHER:
2222 if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2223 (range2->type == XML_REGEXP_OTHER_FORMAT) ||
2224 (range2->type == XML_REGEXP_OTHER_PRIVATE))
2228 if ((range2->type >= XML_REGEXP_LETTER) &&
2229 (range2->type < XML_REGEXP_BLOCK_NAME))
2237 if (((range1->neg == 0) && (range2->neg != 0)) ||
2238 ((range1->neg != 0) && (range2->neg == 0)))
2244 * xmlFACompareAtomTypes:
2245 * @type1: an atom type
2246 * @type2: an atom type
2248 * Compares two atoms type to check whether they intersect in some ways,
2249 * this is used by xmlFACompareAtoms only
2251 * Returns 1 if they may intersect and 0 otherwise
2254 xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2255 if ((type1 == XML_REGEXP_EPSILON) ||
2256 (type1 == XML_REGEXP_CHARVAL) ||
2257 (type1 == XML_REGEXP_RANGES) ||
2258 (type1 == XML_REGEXP_SUBREG) ||
2259 (type1 == XML_REGEXP_STRING) ||
2260 (type1 == XML_REGEXP_ANYCHAR))
2262 if ((type2 == XML_REGEXP_EPSILON) ||
2263 (type2 == XML_REGEXP_CHARVAL) ||
2264 (type2 == XML_REGEXP_RANGES) ||
2265 (type2 == XML_REGEXP_SUBREG) ||
2266 (type2 == XML_REGEXP_STRING) ||
2267 (type2 == XML_REGEXP_ANYCHAR))
2270 if (type1 == type2) return(1);
2272 /* simplify subsequent compares by making sure type1 < type2 */
2273 if (type1 > type2) {
2274 xmlRegAtomType tmp = type1;
2279 case XML_REGEXP_ANYSPACE: /* \s */
2280 /* can't be a letter, number, mark, pontuation, symbol */
2281 if ((type2 == XML_REGEXP_NOTSPACE) ||
2282 ((type2 >= XML_REGEXP_LETTER) &&
2283 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2284 ((type2 >= XML_REGEXP_NUMBER) &&
2285 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2286 ((type2 >= XML_REGEXP_MARK) &&
2287 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2288 ((type2 >= XML_REGEXP_PUNCT) &&
2289 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2290 ((type2 >= XML_REGEXP_SYMBOL) &&
2291 (type2 <= XML_REGEXP_SYMBOL_OTHERS))
2294 case XML_REGEXP_NOTSPACE: /* \S */
2296 case XML_REGEXP_INITNAME: /* \l */
2297 /* can't be a number, mark, separator, pontuation, symbol or other */
2298 if ((type2 == XML_REGEXP_NOTINITNAME) ||
2299 ((type2 >= XML_REGEXP_NUMBER) &&
2300 (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2301 ((type2 >= XML_REGEXP_MARK) &&
2302 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2303 ((type2 >= XML_REGEXP_SEPAR) &&
2304 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2305 ((type2 >= XML_REGEXP_PUNCT) &&
2306 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2307 ((type2 >= XML_REGEXP_SYMBOL) &&
2308 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2309 ((type2 >= XML_REGEXP_OTHER) &&
2310 (type2 <= XML_REGEXP_OTHER_NA))
2313 case XML_REGEXP_NOTINITNAME: /* \L */
2315 case XML_REGEXP_NAMECHAR: /* \c */
2316 /* can't be a mark, separator, pontuation, symbol or other */
2317 if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2318 ((type2 >= XML_REGEXP_MARK) &&
2319 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2320 ((type2 >= XML_REGEXP_PUNCT) &&
2321 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2322 ((type2 >= XML_REGEXP_SEPAR) &&
2323 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2324 ((type2 >= XML_REGEXP_SYMBOL) &&
2325 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2326 ((type2 >= XML_REGEXP_OTHER) &&
2327 (type2 <= XML_REGEXP_OTHER_NA))
2330 case XML_REGEXP_NOTNAMECHAR: /* \C */
2332 case XML_REGEXP_DECIMAL: /* \d */
2333 /* can't be a letter, mark, separator, pontuation, symbol or other */
2334 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2335 (type2 == XML_REGEXP_REALCHAR) ||
2336 ((type2 >= XML_REGEXP_LETTER) &&
2337 (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2338 ((type2 >= XML_REGEXP_MARK) &&
2339 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2340 ((type2 >= XML_REGEXP_PUNCT) &&
2341 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2342 ((type2 >= XML_REGEXP_SEPAR) &&
2343 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2344 ((type2 >= XML_REGEXP_SYMBOL) &&
2345 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2346 ((type2 >= XML_REGEXP_OTHER) &&
2347 (type2 <= XML_REGEXP_OTHER_NA))
2350 case XML_REGEXP_NOTDECIMAL: /* \D */
2352 case XML_REGEXP_REALCHAR: /* \w */
2353 /* can't be a mark, separator, pontuation, symbol or other */
2354 if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2355 ((type2 >= XML_REGEXP_MARK) &&
2356 (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2357 ((type2 >= XML_REGEXP_PUNCT) &&
2358 (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2359 ((type2 >= XML_REGEXP_SEPAR) &&
2360 (type2 <= XML_REGEXP_SEPAR_PARA)) ||
2361 ((type2 >= XML_REGEXP_SYMBOL) &&
2362 (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2363 ((type2 >= XML_REGEXP_OTHER) &&
2364 (type2 <= XML_REGEXP_OTHER_NA))
2367 case XML_REGEXP_NOTREALCHAR: /* \W */
2370 * at that point we know both type 1 and type2 are from
2371 * character categories are ordered and are different,
2372 * it becomes simple because this is a partition
2374 case XML_REGEXP_LETTER:
2375 if (type2 <= XML_REGEXP_LETTER_OTHERS)
2378 case XML_REGEXP_LETTER_UPPERCASE:
2379 case XML_REGEXP_LETTER_LOWERCASE:
2380 case XML_REGEXP_LETTER_TITLECASE:
2381 case XML_REGEXP_LETTER_MODIFIER:
2382 case XML_REGEXP_LETTER_OTHERS:
2384 case XML_REGEXP_MARK:
2385 if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2388 case XML_REGEXP_MARK_NONSPACING:
2389 case XML_REGEXP_MARK_SPACECOMBINING:
2390 case XML_REGEXP_MARK_ENCLOSING:
2392 case XML_REGEXP_NUMBER:
2393 if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2396 case XML_REGEXP_NUMBER_DECIMAL:
2397 case XML_REGEXP_NUMBER_LETTER:
2398 case XML_REGEXP_NUMBER_OTHERS:
2400 case XML_REGEXP_PUNCT:
2401 if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2404 case XML_REGEXP_PUNCT_CONNECTOR:
2405 case XML_REGEXP_PUNCT_DASH:
2406 case XML_REGEXP_PUNCT_OPEN:
2407 case XML_REGEXP_PUNCT_CLOSE:
2408 case XML_REGEXP_PUNCT_INITQUOTE:
2409 case XML_REGEXP_PUNCT_FINQUOTE:
2410 case XML_REGEXP_PUNCT_OTHERS:
2412 case XML_REGEXP_SEPAR:
2413 if (type2 <= XML_REGEXP_SEPAR_PARA)
2416 case XML_REGEXP_SEPAR_SPACE:
2417 case XML_REGEXP_SEPAR_LINE:
2418 case XML_REGEXP_SEPAR_PARA:
2420 case XML_REGEXP_SYMBOL:
2421 if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2424 case XML_REGEXP_SYMBOL_MATH:
2425 case XML_REGEXP_SYMBOL_CURRENCY:
2426 case XML_REGEXP_SYMBOL_MODIFIER:
2427 case XML_REGEXP_SYMBOL_OTHERS:
2429 case XML_REGEXP_OTHER:
2430 if (type2 <= XML_REGEXP_OTHER_NA)
2433 case XML_REGEXP_OTHER_CONTROL:
2434 case XML_REGEXP_OTHER_FORMAT:
2435 case XML_REGEXP_OTHER_PRIVATE:
2436 case XML_REGEXP_OTHER_NA:
2448 * @deep: if not set only compare string pointers
2450 * Compares two atoms to check whether they are the same exactly
2451 * this is used to remove equivalent transitions
2453 * Returns 1 if same and 0 otherwise
2456 xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
2461 if ((atom1 == NULL) || (atom2 == NULL))
2464 if (atom1->type != atom2->type)
2466 switch (atom1->type) {
2467 case XML_REGEXP_EPSILON:
2470 case XML_REGEXP_STRING:
2472 ret = (atom1->valuep == atom2->valuep);
2474 ret = xmlStrEqual((xmlChar *)atom1->valuep,
2475 (xmlChar *)atom2->valuep);
2477 case XML_REGEXP_CHARVAL:
2478 ret = (atom1->codepoint == atom2->codepoint);
2480 case XML_REGEXP_RANGES:
2481 /* too hard to do in the general case */
2490 * xmlFACompareAtoms:
2493 * @deep: if not set only compare string pointers
2495 * Compares two atoms to check whether they intersect in some ways,
2496 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2498 * Returns 1 if yes and 0 otherwise
2501 xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
2506 if ((atom1 == NULL) || (atom2 == NULL))
2509 if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2510 (atom2->type == XML_REGEXP_ANYCHAR))
2513 if (atom1->type > atom2->type) {
2519 if (atom1->type != atom2->type) {
2520 ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2521 /* if they can't intersect at the type level break now */
2525 switch (atom1->type) {
2526 case XML_REGEXP_STRING:
2528 ret = (atom1->valuep != atom2->valuep);
2530 ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2531 (xmlChar *)atom2->valuep);
2533 case XML_REGEXP_EPSILON:
2534 goto not_determinist;
2535 case XML_REGEXP_CHARVAL:
2536 if (atom2->type == XML_REGEXP_CHARVAL) {
2537 ret = (atom1->codepoint == atom2->codepoint);
2539 ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2544 case XML_REGEXP_RANGES:
2545 if (atom2->type == XML_REGEXP_RANGES) {
2547 xmlRegRangePtr r1, r2;
2550 * need to check that none of the ranges eventually matches
2552 for (i = 0;i < atom1->nbRanges;i++) {
2553 for (j = 0;j < atom2->nbRanges;j++) {
2554 r1 = atom1->ranges[i];
2555 r2 = atom2->ranges[j];
2556 res = xmlFACompareRanges(r1, r2);
2567 goto not_determinist;
2570 if (atom1->neg != atom2->neg) {
2580 * xmlFARecurseDeterminism:
2581 * @ctxt: a regexp parser context
2583 * Check whether the associated regexp is determinist,
2584 * should be called after xmlFAEliminateEpsilonTransitions()
2588 xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2589 int to, xmlRegAtomPtr atom) {
2592 int transnr, nbTrans;
2598 if (state->markd == XML_REGEXP_MARK_VISITED)
2601 if (ctxt->flags & AM_AUTOMATA_RNG)
2605 * don't recurse on transitions potentially added in the course of
2608 nbTrans = state->nbTrans;
2609 for (transnr = 0;transnr < nbTrans;transnr++) {
2610 t1 = &(state->trans[transnr]);
2612 * check transitions conflicting with the one looked at
2614 if (t1->atom == NULL) {
2617 state->markd = XML_REGEXP_MARK_VISITED;
2618 res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2629 if (xmlFACompareAtoms(t1->atom, atom, deep)) {
2631 /* mark the transition as non-deterministic */
2639 * xmlFAComputesDeterminism:
2640 * @ctxt: a regexp parser context
2642 * Check whether the associated regexp is determinist,
2643 * should be called after xmlFAEliminateEpsilonTransitions()
2647 xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2648 int statenr, transnr;
2649 xmlRegStatePtr state;
2650 xmlRegTransPtr t1, t2, last;
2655 #ifdef DEBUG_REGEXP_GRAPH
2656 printf("xmlFAComputesDeterminism\n");
2657 xmlRegPrintCtxt(stdout, ctxt);
2659 if (ctxt->determinist != -1)
2660 return(ctxt->determinist);
2662 if (ctxt->flags & AM_AUTOMATA_RNG)
2666 * First cleanup the automata removing cancelled transitions
2668 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2669 state = ctxt->states[statenr];
2672 if (state->nbTrans < 2)
2674 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2675 t1 = &(state->trans[transnr]);
2677 * Determinism checks in case of counted or all transitions
2678 * will have to be handled separately
2680 if (t1->atom == NULL) {
2684 if (t1->to == -1) /* eliminated */
2686 for (i = 0;i < transnr;i++) {
2687 t2 = &(state->trans[i]);
2688 if (t2->to == -1) /* eliminated */
2690 if (t2->atom != NULL) {
2691 if (t1->to == t2->to) {
2693 * Here we use deep because we want to keep the
2694 * transitions which indicate a conflict
2696 if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
2697 (t1->counter == t2->counter) &&
2698 (t1->count == t2->count))
2699 t2->to = -1; /* eliminated */
2707 * Check for all states that there aren't 2 transitions
2708 * with the same atom and a different target.
2710 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2711 state = ctxt->states[statenr];
2714 if (state->nbTrans < 2)
2717 for (transnr = 0;transnr < state->nbTrans;transnr++) {
2718 t1 = &(state->trans[transnr]);
2720 * Determinism checks in case of counted or all transitions
2721 * will have to be handled separately
2723 if (t1->atom == NULL) {
2726 if (t1->to == -1) /* eliminated */
2728 for (i = 0;i < transnr;i++) {
2729 t2 = &(state->trans[i]);
2730 if (t2->to == -1) /* eliminated */
2732 if (t2->atom != NULL) {
2734 * But here we don't use deep because we want to
2735 * find transitions which indicate a conflict
2737 if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
2739 /* mark the transitions as non-deterministic ones */
2744 } else if (t1->to != -1) {
2746 * do the closure in case of remaining specific
2747 * epsilon transitions like choices or all
2749 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2751 /* don't shortcut the computation so all non deterministic
2752 transition get marked down
2763 /* don't shortcut the computation so all non deterministic
2764 transition get marked down
2770 * mark specifically the last non-deterministic transition
2771 * from a state since there is no need to set-up rollback
2778 /* don't shortcut the computation so all non deterministic
2779 transition get marked down
2784 ctxt->determinist = ret;
2788 /************************************************************************
2790 * Routines to check input against transition atoms *
2792 ************************************************************************/
2795 xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2796 int start, int end, const xmlChar *blockName) {
2800 case XML_REGEXP_STRING:
2801 case XML_REGEXP_SUBREG:
2802 case XML_REGEXP_RANGES:
2803 case XML_REGEXP_EPSILON:
2805 case XML_REGEXP_ANYCHAR:
2806 ret = ((codepoint != '\n') && (codepoint != '\r'));
2808 case XML_REGEXP_CHARVAL:
2809 ret = ((codepoint >= start) && (codepoint <= end));
2811 case XML_REGEXP_NOTSPACE:
2813 /* Falls through. */
2814 case XML_REGEXP_ANYSPACE:
2815 ret = ((codepoint == '\n') || (codepoint == '\r') ||
2816 (codepoint == '\t') || (codepoint == ' '));
2818 case XML_REGEXP_NOTINITNAME:
2820 /* Falls through. */
2821 case XML_REGEXP_INITNAME:
2822 ret = (IS_LETTER(codepoint) ||
2823 (codepoint == '_') || (codepoint == ':'));
2825 case XML_REGEXP_NOTNAMECHAR:
2827 /* Falls through. */
2828 case XML_REGEXP_NAMECHAR:
2829 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
2830 (codepoint == '.') || (codepoint == '-') ||
2831 (codepoint == '_') || (codepoint == ':') ||
2832 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
2834 case XML_REGEXP_NOTDECIMAL:
2836 /* Falls through. */
2837 case XML_REGEXP_DECIMAL:
2838 ret = xmlUCSIsCatNd(codepoint);
2840 case XML_REGEXP_REALCHAR:
2842 /* Falls through. */
2843 case XML_REGEXP_NOTREALCHAR:
2844 ret = xmlUCSIsCatP(codepoint);
2846 ret = xmlUCSIsCatZ(codepoint);
2848 ret = xmlUCSIsCatC(codepoint);
2850 case XML_REGEXP_LETTER:
2851 ret = xmlUCSIsCatL(codepoint);
2853 case XML_REGEXP_LETTER_UPPERCASE:
2854 ret = xmlUCSIsCatLu(codepoint);
2856 case XML_REGEXP_LETTER_LOWERCASE:
2857 ret = xmlUCSIsCatLl(codepoint);
2859 case XML_REGEXP_LETTER_TITLECASE:
2860 ret = xmlUCSIsCatLt(codepoint);
2862 case XML_REGEXP_LETTER_MODIFIER:
2863 ret = xmlUCSIsCatLm(codepoint);
2865 case XML_REGEXP_LETTER_OTHERS:
2866 ret = xmlUCSIsCatLo(codepoint);
2868 case XML_REGEXP_MARK:
2869 ret = xmlUCSIsCatM(codepoint);
2871 case XML_REGEXP_MARK_NONSPACING:
2872 ret = xmlUCSIsCatMn(codepoint);
2874 case XML_REGEXP_MARK_SPACECOMBINING:
2875 ret = xmlUCSIsCatMc(codepoint);
2877 case XML_REGEXP_MARK_ENCLOSING:
2878 ret = xmlUCSIsCatMe(codepoint);
2880 case XML_REGEXP_NUMBER:
2881 ret = xmlUCSIsCatN(codepoint);
2883 case XML_REGEXP_NUMBER_DECIMAL:
2884 ret = xmlUCSIsCatNd(codepoint);
2886 case XML_REGEXP_NUMBER_LETTER:
2887 ret = xmlUCSIsCatNl(codepoint);
2889 case XML_REGEXP_NUMBER_OTHERS:
2890 ret = xmlUCSIsCatNo(codepoint);
2892 case XML_REGEXP_PUNCT:
2893 ret = xmlUCSIsCatP(codepoint);
2895 case XML_REGEXP_PUNCT_CONNECTOR:
2896 ret = xmlUCSIsCatPc(codepoint);
2898 case XML_REGEXP_PUNCT_DASH:
2899 ret = xmlUCSIsCatPd(codepoint);
2901 case XML_REGEXP_PUNCT_OPEN:
2902 ret = xmlUCSIsCatPs(codepoint);
2904 case XML_REGEXP_PUNCT_CLOSE:
2905 ret = xmlUCSIsCatPe(codepoint);
2907 case XML_REGEXP_PUNCT_INITQUOTE:
2908 ret = xmlUCSIsCatPi(codepoint);
2910 case XML_REGEXP_PUNCT_FINQUOTE:
2911 ret = xmlUCSIsCatPf(codepoint);
2913 case XML_REGEXP_PUNCT_OTHERS:
2914 ret = xmlUCSIsCatPo(codepoint);
2916 case XML_REGEXP_SEPAR:
2917 ret = xmlUCSIsCatZ(codepoint);
2919 case XML_REGEXP_SEPAR_SPACE:
2920 ret = xmlUCSIsCatZs(codepoint);
2922 case XML_REGEXP_SEPAR_LINE:
2923 ret = xmlUCSIsCatZl(codepoint);
2925 case XML_REGEXP_SEPAR_PARA:
2926 ret = xmlUCSIsCatZp(codepoint);
2928 case XML_REGEXP_SYMBOL:
2929 ret = xmlUCSIsCatS(codepoint);
2931 case XML_REGEXP_SYMBOL_MATH:
2932 ret = xmlUCSIsCatSm(codepoint);
2934 case XML_REGEXP_SYMBOL_CURRENCY:
2935 ret = xmlUCSIsCatSc(codepoint);
2937 case XML_REGEXP_SYMBOL_MODIFIER:
2938 ret = xmlUCSIsCatSk(codepoint);
2940 case XML_REGEXP_SYMBOL_OTHERS:
2941 ret = xmlUCSIsCatSo(codepoint);
2943 case XML_REGEXP_OTHER:
2944 ret = xmlUCSIsCatC(codepoint);
2946 case XML_REGEXP_OTHER_CONTROL:
2947 ret = xmlUCSIsCatCc(codepoint);
2949 case XML_REGEXP_OTHER_FORMAT:
2950 ret = xmlUCSIsCatCf(codepoint);
2952 case XML_REGEXP_OTHER_PRIVATE:
2953 ret = xmlUCSIsCatCo(codepoint);
2955 case XML_REGEXP_OTHER_NA:
2956 /* ret = xmlUCSIsCatCn(codepoint); */
2957 /* Seems it doesn't exist anymore in recent Unicode releases */
2960 case XML_REGEXP_BLOCK_NAME:
2961 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2970 xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2972 xmlRegRangePtr range;
2974 if ((atom == NULL) || (!IS_CHAR(codepoint)))
2977 switch (atom->type) {
2978 case XML_REGEXP_SUBREG:
2979 case XML_REGEXP_EPSILON:
2981 case XML_REGEXP_CHARVAL:
2982 return(codepoint == atom->codepoint);
2983 case XML_REGEXP_RANGES: {
2986 for (i = 0;i < atom->nbRanges;i++) {
2987 range = atom->ranges[i];
2988 if (range->neg == 2) {
2989 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2990 0, range->start, range->end,
2993 return(0); /* excluded char */
2994 } else if (range->neg) {
2995 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2996 0, range->start, range->end,
3003 ret = xmlRegCheckCharacterRange(range->type, codepoint,
3004 0, range->start, range->end,
3007 accept = 1; /* might still be excluded */
3012 case XML_REGEXP_STRING:
3013 printf("TODO: XML_REGEXP_STRING\n");
3015 case XML_REGEXP_ANYCHAR:
3016 case XML_REGEXP_ANYSPACE:
3017 case XML_REGEXP_NOTSPACE:
3018 case XML_REGEXP_INITNAME:
3019 case XML_REGEXP_NOTINITNAME:
3020 case XML_REGEXP_NAMECHAR:
3021 case XML_REGEXP_NOTNAMECHAR:
3022 case XML_REGEXP_DECIMAL:
3023 case XML_REGEXP_NOTDECIMAL:
3024 case XML_REGEXP_REALCHAR:
3025 case XML_REGEXP_NOTREALCHAR:
3026 case XML_REGEXP_LETTER:
3027 case XML_REGEXP_LETTER_UPPERCASE:
3028 case XML_REGEXP_LETTER_LOWERCASE:
3029 case XML_REGEXP_LETTER_TITLECASE:
3030 case XML_REGEXP_LETTER_MODIFIER:
3031 case XML_REGEXP_LETTER_OTHERS:
3032 case XML_REGEXP_MARK:
3033 case XML_REGEXP_MARK_NONSPACING:
3034 case XML_REGEXP_MARK_SPACECOMBINING:
3035 case XML_REGEXP_MARK_ENCLOSING:
3036 case XML_REGEXP_NUMBER:
3037 case XML_REGEXP_NUMBER_DECIMAL:
3038 case XML_REGEXP_NUMBER_LETTER:
3039 case XML_REGEXP_NUMBER_OTHERS:
3040 case XML_REGEXP_PUNCT:
3041 case XML_REGEXP_PUNCT_CONNECTOR:
3042 case XML_REGEXP_PUNCT_DASH:
3043 case XML_REGEXP_PUNCT_OPEN:
3044 case XML_REGEXP_PUNCT_CLOSE:
3045 case XML_REGEXP_PUNCT_INITQUOTE:
3046 case XML_REGEXP_PUNCT_FINQUOTE:
3047 case XML_REGEXP_PUNCT_OTHERS:
3048 case XML_REGEXP_SEPAR:
3049 case XML_REGEXP_SEPAR_SPACE:
3050 case XML_REGEXP_SEPAR_LINE:
3051 case XML_REGEXP_SEPAR_PARA:
3052 case XML_REGEXP_SYMBOL:
3053 case XML_REGEXP_SYMBOL_MATH:
3054 case XML_REGEXP_SYMBOL_CURRENCY:
3055 case XML_REGEXP_SYMBOL_MODIFIER:
3056 case XML_REGEXP_SYMBOL_OTHERS:
3057 case XML_REGEXP_OTHER:
3058 case XML_REGEXP_OTHER_CONTROL:
3059 case XML_REGEXP_OTHER_FORMAT:
3060 case XML_REGEXP_OTHER_PRIVATE:
3061 case XML_REGEXP_OTHER_NA:
3062 case XML_REGEXP_BLOCK_NAME:
3063 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3064 (const xmlChar *)atom->valuep);
3072 /************************************************************************
3074 * Saving and restoring state of an execution context *
3076 ************************************************************************/
3078 #ifdef DEBUG_REGEXP_EXEC
3080 xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
3081 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
3082 if (exec->inputStack != NULL) {
3085 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
3086 printf("%s ", (const char *)
3087 exec->inputStack[exec->inputStackNr - (i + 1)].value);
3089 printf(": %s", &(exec->inputString[exec->index]));
3096 xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3097 #ifdef DEBUG_REGEXP_EXEC
3100 xmlFARegDebugExec(exec);
3104 if (exec->nbPush > MAX_PUSH) {
3110 if (exec->maxRollbacks == 0) {
3111 exec->maxRollbacks = 4;
3112 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3113 sizeof(xmlRegExecRollback));
3114 if (exec->rollbacks == NULL) {
3115 xmlRegexpErrMemory(NULL, "saving regexp");
3116 exec->maxRollbacks = 0;
3119 memset(exec->rollbacks, 0,
3120 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3121 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
3122 xmlRegExecRollback *tmp;
3123 int len = exec->maxRollbacks;
3125 exec->maxRollbacks *= 2;
3126 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3127 exec->maxRollbacks * sizeof(xmlRegExecRollback));
3129 xmlRegexpErrMemory(NULL, "saving regexp");
3130 exec->maxRollbacks /= 2;
3133 exec->rollbacks = tmp;
3134 tmp = &exec->rollbacks[len];
3135 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3137 exec->rollbacks[exec->nbRollbacks].state = exec->state;
3138 exec->rollbacks[exec->nbRollbacks].index = exec->index;
3139 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3140 if (exec->comp->nbCounters > 0) {
3141 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3142 exec->rollbacks[exec->nbRollbacks].counts = (int *)
3143 xmlMalloc(exec->comp->nbCounters * sizeof(int));
3144 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3145 xmlRegexpErrMemory(NULL, "saving regexp");
3150 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3151 exec->comp->nbCounters * sizeof(int));
3153 exec->nbRollbacks++;
3157 xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3158 if (exec->nbRollbacks <= 0) {
3160 #ifdef DEBUG_REGEXP_EXEC
3161 printf("rollback failed on empty stack\n");
3165 exec->nbRollbacks--;
3166 exec->state = exec->rollbacks[exec->nbRollbacks].state;
3167 exec->index = exec->rollbacks[exec->nbRollbacks].index;
3168 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3169 if (exec->comp->nbCounters > 0) {
3170 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3171 fprintf(stderr, "exec save: allocation failed");
3176 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
3177 exec->comp->nbCounters * sizeof(int));
3181 #ifdef DEBUG_REGEXP_EXEC
3182 printf("restored ");
3183 xmlFARegDebugExec(exec);
3187 /************************************************************************
3189 * Verifier, running an input against a compiled regexp *
3191 ************************************************************************/
3194 xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3195 xmlRegExecCtxt execval;
3196 xmlRegExecCtxtPtr exec = &execval;
3197 int ret, codepoint = 0, len, deter;
3199 exec->inputString = content;
3202 exec->determinist = 1;
3203 exec->maxRollbacks = 0;
3204 exec->nbRollbacks = 0;
3205 exec->rollbacks = NULL;
3208 exec->state = comp->states[0];
3210 exec->transcount = 0;
3211 exec->inputStack = NULL;
3212 exec->inputStackMax = 0;
3213 if (comp->nbCounters > 0) {
3214 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
3215 if (exec->counts == NULL) {
3216 xmlRegexpErrMemory(NULL, "running regexp");
3219 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3221 exec->counts = NULL;
3222 while ((exec->status == 0) && (exec->state != NULL) &&
3223 ((exec->inputString[exec->index] != 0) ||
3224 ((exec->state != NULL) &&
3225 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3226 xmlRegTransPtr trans;
3230 * If end of input on non-terminal state, rollback, however we may
3231 * still have epsilon like transition for counted transitions
3232 * on counters, in that case don't break too early. Additionally,
3233 * if we are working on a range like "AB{0,2}", where B is not present,
3234 * we don't want to break.
3237 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
3239 * if there is a transition, we must check if
3240 * atom allows minOccurs of 0
3242 if (exec->transno < exec->state->nbTrans) {
3243 trans = &exec->state->trans[exec->transno];
3244 if (trans->to >=0) {
3246 if (!((atom->min == 0) && (atom->max > 0)))
3253 exec->transcount = 0;
3254 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3255 trans = &exec->state->trans[exec->transno];
3261 if (trans->count >= 0) {
3263 xmlRegCounterPtr counter;
3265 if (exec->counts == NULL) {
3270 * A counted transition.
3273 count = exec->counts[trans->count];
3274 counter = &exec->comp->counters[trans->count];
3275 #ifdef DEBUG_REGEXP_EXEC
3276 printf("testing count %d: val %d, min %d, max %d\n",
3277 trans->count, count, counter->min, counter->max);
3279 ret = ((count >= counter->min) && (count <= counter->max));
3280 if ((ret) && (counter->min != counter->max))
3282 } else if (atom == NULL) {
3283 fprintf(stderr, "epsilon transition left at runtime\n");
3286 } else if (exec->inputString[exec->index] != 0) {
3287 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3288 ret = xmlRegCheckCharacter(atom, codepoint);
3289 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
3290 xmlRegStatePtr to = comp->states[trans->to];
3293 * this is a multiple input sequence
3294 * If there is a counter associated increment it now.
3295 * before potentially saving and rollback
3296 * do not increment if the counter is already over the
3297 * maximum limit in which case get to next transition
3299 if (trans->counter >= 0) {
3300 xmlRegCounterPtr counter;
3302 if ((exec->counts == NULL) ||
3303 (exec->comp == NULL) ||
3304 (exec->comp->counters == NULL)) {
3308 counter = &exec->comp->counters[trans->counter];
3309 if (exec->counts[trans->counter] >= counter->max)
3310 continue; /* for loop on transitions */
3312 #ifdef DEBUG_REGEXP_EXEC
3313 printf("Increasing count %d\n", trans->counter);
3315 exec->counts[trans->counter]++;
3317 if (exec->state->nbTrans > exec->transno + 1) {
3318 xmlFARegExecSave(exec);
3320 exec->transcount = 1;
3323 * Try to progress as much as possible on the input
3325 if (exec->transcount == atom->max) {
3330 * End of input: stop here
3332 if (exec->inputString[exec->index] == 0) {
3336 if (exec->transcount >= atom->min) {
3337 int transno = exec->transno;
3338 xmlRegStatePtr state = exec->state;
3341 * The transition is acceptable save it
3343 exec->transno = -1; /* trick */
3345 xmlFARegExecSave(exec);
3346 exec->transno = transno;
3347 exec->state = state;
3349 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3351 ret = xmlRegCheckCharacter(atom, codepoint);
3354 if (exec->transcount < atom->min)
3358 * If the last check failed but one transition was found
3359 * possible, rollback
3366 if (trans->counter >= 0) {
3367 if (exec->counts == NULL) {
3371 #ifdef DEBUG_REGEXP_EXEC
3372 printf("Decreasing count %d\n", trans->counter);
3374 exec->counts[trans->counter]--;
3376 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3378 * we don't match on the codepoint, but minOccurs of 0
3379 * says that's ok. Setting len to 0 inhibits stepping
3380 * over the codepoint.
3382 exec->transcount = 1;
3386 } else if ((atom->min == 0) && (atom->max > 0)) {
3387 /* another spot to match when minOccurs is 0 */
3388 exec->transcount = 1;
3393 if ((trans->nd == 1) ||
3394 ((trans->count >= 0) && (deter == 0) &&
3395 (exec->state->nbTrans > exec->transno + 1))) {
3396 #ifdef DEBUG_REGEXP_EXEC
3398 printf("Saving on nd transition atom %d for %c at %d\n",
3399 trans->atom->no, codepoint, exec->index);
3401 printf("Saving on counted transition count %d for %c at %d\n",
3402 trans->count, codepoint, exec->index);
3404 xmlFARegExecSave(exec);
3406 if (trans->counter >= 0) {
3407 xmlRegCounterPtr counter;
3409 /* make sure we don't go over the counter maximum value */
3410 if ((exec->counts == NULL) ||
3411 (exec->comp == NULL) ||
3412 (exec->comp->counters == NULL)) {
3416 counter = &exec->comp->counters[trans->counter];
3417 if (exec->counts[trans->counter] >= counter->max)
3418 continue; /* for loop on transitions */
3419 #ifdef DEBUG_REGEXP_EXEC
3420 printf("Increasing count %d\n", trans->counter);
3422 exec->counts[trans->counter]++;
3424 if ((trans->count >= 0) &&
3425 (trans->count < REGEXP_ALL_COUNTER)) {
3426 if (exec->counts == NULL) {
3430 #ifdef DEBUG_REGEXP_EXEC
3431 printf("resetting count %d on transition\n",
3434 exec->counts[trans->count] = 0;
3436 #ifdef DEBUG_REGEXP_EXEC
3437 printf("entering state %d\n", trans->to);
3439 exec->state = comp->states[trans->to];
3441 if (trans->atom != NULL) {
3445 } else if (ret < 0) {
3450 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3453 * Failed to find a way out
3455 exec->determinist = 0;
3456 #ifdef DEBUG_REGEXP_EXEC
3457 printf("rollback from state %d on %d:%c\n", exec->state->no,
3458 codepoint,codepoint);
3460 xmlFARegExecRollBack(exec);
3466 if (exec->rollbacks != NULL) {
3467 if (exec->counts != NULL) {
3470 for (i = 0;i < exec->maxRollbacks;i++)
3471 if (exec->rollbacks[i].counts != NULL)
3472 xmlFree(exec->rollbacks[i].counts);
3474 xmlFree(exec->rollbacks);
3476 if (exec->state == NULL)
3478 if (exec->counts != NULL)
3479 xmlFree(exec->counts);
3480 if (exec->status == 0)
3482 if (exec->status == -1) {
3483 if (exec->nbPush > MAX_PUSH)
3487 return(exec->status);
3490 /************************************************************************
3492 * Progressive interface to the verifier one atom at a time *
3494 ************************************************************************/
3496 static void testerr(xmlRegExecCtxtPtr exec);
3500 * xmlRegNewExecCtxt:
3501 * @comp: a precompiled regular expression
3502 * @callback: a callback function used for handling progresses in the
3503 * automata matching phase
3504 * @data: the context data associated to the callback in this context
3506 * Build a context used for progressive evaluation of a regexp.
3508 * Returns the new context
3511 xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3512 xmlRegExecCtxtPtr exec;
3516 if ((comp->compact == NULL) && (comp->states == NULL))
3518 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3520 xmlRegexpErrMemory(NULL, "creating execution context");
3523 memset(exec, 0, sizeof(xmlRegExecCtxt));
3524 exec->inputString = NULL;
3526 exec->determinist = 1;
3527 exec->maxRollbacks = 0;
3528 exec->nbRollbacks = 0;
3529 exec->rollbacks = NULL;
3532 if (comp->compact == NULL)
3533 exec->state = comp->states[0];
3535 exec->transcount = 0;
3536 exec->callback = callback;
3538 if (comp->nbCounters > 0) {
3540 * For error handling, exec->counts is allocated twice the size
3541 * the second half is used to store the data in case of rollback
3543 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3545 if (exec->counts == NULL) {
3546 xmlRegexpErrMemory(NULL, "creating execution context");
3550 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3551 exec->errCounts = &exec->counts[comp->nbCounters];
3553 exec->counts = NULL;
3554 exec->errCounts = NULL;
3556 exec->inputStackMax = 0;
3557 exec->inputStackNr = 0;
3558 exec->inputStack = NULL;
3559 exec->errStateNo = -1;
3560 exec->errString = NULL;
3566 * xmlRegFreeExecCtxt:
3567 * @exec: a regular expression evaulation context
3569 * Free the structures associated to a regular expression evaulation context.
3572 xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3576 if (exec->rollbacks != NULL) {
3577 if (exec->counts != NULL) {
3580 for (i = 0;i < exec->maxRollbacks;i++)
3581 if (exec->rollbacks[i].counts != NULL)
3582 xmlFree(exec->rollbacks[i].counts);
3584 xmlFree(exec->rollbacks);
3586 if (exec->counts != NULL)
3587 xmlFree(exec->counts);
3588 if (exec->inputStack != NULL) {
3591 for (i = 0;i < exec->inputStackNr;i++) {
3592 if (exec->inputStack[i].value != NULL)
3593 xmlFree(exec->inputStack[i].value);
3595 xmlFree(exec->inputStack);
3597 if (exec->errString != NULL)
3598 xmlFree(exec->errString);
3603 xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3606 printf("saving value: %d:%s\n", exec->inputStackNr, value);
3608 if (exec->inputStackMax == 0) {
3609 exec->inputStackMax = 4;
3610 exec->inputStack = (xmlRegInputTokenPtr)
3611 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3612 if (exec->inputStack == NULL) {
3613 xmlRegexpErrMemory(NULL, "pushing input string");
3614 exec->inputStackMax = 0;
3617 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3618 xmlRegInputTokenPtr tmp;
3620 exec->inputStackMax *= 2;
3621 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3622 exec->inputStackMax * sizeof(xmlRegInputToken));
3624 xmlRegexpErrMemory(NULL, "pushing input string");
3625 exec->inputStackMax /= 2;
3628 exec->inputStack = tmp;
3630 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3631 exec->inputStack[exec->inputStackNr].data = data;
3632 exec->inputStackNr++;
3633 exec->inputStack[exec->inputStackNr].value = NULL;
3634 exec->inputStack[exec->inputStackNr].data = NULL;
3638 * xmlRegStrEqualWildcard:
3639 * @expStr: the string to be evaluated
3640 * @valStr: the validation string
3642 * Checks if both strings are equal or have the same content. "*"
3643 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3644 * substrings in both @expStr and @valStr.
3646 * Returns 1 if the comparison is satisfied and the number of substrings
3647 * is equal, 0 otherwise.
3651 xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3652 if (expStr == valStr) return(1);
3653 if (expStr == NULL) return(0);
3654 if (valStr == NULL) return(0);
3657 * Eval if we have a wildcard for the current item.
3659 if (*expStr != *valStr) {
3660 /* if one of them starts with a wildcard make valStr be it */
3661 if (*valStr == '*') {
3668 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3670 if (*valStr == XML_REG_STRING_SEPARATOR)
3673 } while (*valStr != 0);
3680 } while (*valStr != 0);
3688 * xmlRegCompactPushString:
3689 * @exec: a regexp execution context
3690 * @comp: the precompiled exec with a compact table
3691 * @value: a string token input
3692 * @data: data associated to the token to reuse in callbacks
3694 * Push one input token in the execution context
3696 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3697 * a negative value in case of error.
3700 xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3702 const xmlChar *value,
3704 int state = exec->index;
3707 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3710 if (value == NULL) {
3712 * are we at a final state ?
3714 if (comp->compact[state * (comp->nbstrings + 1)] ==
3715 XML_REGEXP_FINAL_STATE)
3721 printf("value pushed: %s\n", value);
3725 * Examine all outside transitions from current state
3727 for (i = 0;i < comp->nbstrings;i++) {
3728 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3729 if ((target > 0) && (target <= comp->nbstates)) {
3730 target--; /* to avoid 0 */
3731 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3732 exec->index = target;
3733 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3734 exec->callback(exec->data, value,
3735 comp->transdata[state * comp->nbstrings + i], data);
3738 printf("entering state %d\n", target);
3740 if (comp->compact[target * (comp->nbstrings + 1)] ==
3741 XML_REGEXP_SINK_STATE)
3744 if (comp->compact[target * (comp->nbstrings + 1)] ==
3745 XML_REGEXP_FINAL_STATE)
3752 * Failed to find an exit transition out from current state for the
3756 printf("failed to find a transition for %s on state %d\n", value, state);
3759 if (exec->errString != NULL)
3760 xmlFree(exec->errString);
3761 exec->errString = xmlStrdup(value);
3762 exec->errStateNo = state;
3771 * xmlRegExecPushStringInternal:
3772 * @exec: a regexp execution context or NULL to indicate the end
3773 * @value: a string token input
3774 * @data: data associated to the token to reuse in callbacks
3775 * @compound: value was assembled from 2 strings
3777 * Push one input token in the execution context
3779 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3780 * a negative value in case of error.
3783 xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3784 void *data, int compound) {
3785 xmlRegTransPtr trans;
3793 if (exec->comp == NULL)
3795 if (exec->status != 0)
3796 return(exec->status);
3798 if (exec->comp->compact != NULL)
3799 return(xmlRegCompactPushString(exec, exec->comp, value, data));
3801 if (value == NULL) {
3802 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3808 printf("value pushed: %s\n", value);
3811 * If we have an active rollback stack push the new value there
3812 * and get back to where we were left
3814 if ((value != NULL) && (exec->inputStackNr > 0)) {
3815 xmlFARegExecSaveInputString(exec, value, data);
3816 value = exec->inputStack[exec->index].value;
3817 data = exec->inputStack[exec->index].data;
3819 printf("value loaded: %s\n", value);
3823 while ((exec->status == 0) &&
3826 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3829 * End of input on non-terminal state, rollback, however we may
3830 * still have epsilon like transition for counted transitions
3831 * on counters, in that case don't break too early.
3833 if ((value == NULL) && (exec->counts == NULL))
3836 exec->transcount = 0;
3837 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3838 trans = &exec->state->trans[exec->transno];
3843 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3847 xmlRegCounterPtr counter;
3852 printf("testing all lax %d\n", trans->count);
3855 * Check all counted transitions from the current state
3857 if ((value == NULL) && (final)) {
3859 } else if (value != NULL) {
3860 for (i = 0;i < exec->state->nbTrans;i++) {
3861 t = &exec->state->trans[i];
3862 if ((t->counter < 0) || (t == trans))
3864 counter = &exec->comp->counters[t->counter];
3865 count = exec->counts[t->counter];
3866 if ((count < counter->max) &&
3867 (t->atom != NULL) &&
3868 (xmlStrEqual(value, t->atom->valuep))) {
3872 if ((count >= counter->min) &&
3873 (count < counter->max) &&
3874 (t->atom != NULL) &&
3875 (xmlStrEqual(value, t->atom->valuep))) {
3881 } else if (trans->count == REGEXP_ALL_COUNTER) {
3885 xmlRegCounterPtr counter;
3890 printf("testing all %d\n", trans->count);
3893 * Check all counted transitions from the current state
3895 for (i = 0;i < exec->state->nbTrans;i++) {
3896 t = &exec->state->trans[i];
3897 if ((t->counter < 0) || (t == trans))
3899 counter = &exec->comp->counters[t->counter];
3900 count = exec->counts[t->counter];
3901 if ((count < counter->min) || (count > counter->max)) {
3906 } else if (trans->count >= 0) {
3908 xmlRegCounterPtr counter;
3911 * A counted transition.
3914 count = exec->counts[trans->count];
3915 counter = &exec->comp->counters[trans->count];
3917 printf("testing count %d: val %d, min %d, max %d\n",
3918 trans->count, count, counter->min, counter->max);
3920 ret = ((count >= counter->min) && (count <= counter->max));
3921 } else if (atom == NULL) {
3922 fprintf(stderr, "epsilon transition left at runtime\n");
3925 } else if (value != NULL) {
3926 ret = xmlRegStrEqualWildcard(atom->valuep, value);
3932 if ((ret == 1) && (trans->counter >= 0)) {
3933 xmlRegCounterPtr counter;
3936 count = exec->counts[trans->counter];
3937 counter = &exec->comp->counters[trans->counter];
3938 if (count >= counter->max)
3942 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3943 xmlRegStatePtr to = exec->comp->states[trans->to];
3946 * this is a multiple input sequence
3948 if (exec->state->nbTrans > exec->transno + 1) {
3949 if (exec->inputStackNr <= 0) {
3950 xmlFARegExecSaveInputString(exec, value, data);
3952 xmlFARegExecSave(exec);
3954 exec->transcount = 1;
3957 * Try to progress as much as possible on the input
3959 if (exec->transcount == atom->max) {
3963 value = exec->inputStack[exec->index].value;
3964 data = exec->inputStack[exec->index].data;
3966 printf("value loaded: %s\n", value);
3970 * End of input: stop here
3972 if (value == NULL) {
3976 if (exec->transcount >= atom->min) {
3977 int transno = exec->transno;
3978 xmlRegStatePtr state = exec->state;
3981 * The transition is acceptable save it
3983 exec->transno = -1; /* trick */
3985 if (exec->inputStackNr <= 0) {
3986 xmlFARegExecSaveInputString(exec, value, data);
3988 xmlFARegExecSave(exec);
3989 exec->transno = transno;
3990 exec->state = state;
3992 ret = xmlStrEqual(value, atom->valuep);
3995 if (exec->transcount < atom->min)
3999 * If the last check failed but one transition was found
4000 * possible, rollback
4010 if ((exec->callback != NULL) && (atom != NULL) &&
4012 exec->callback(exec->data, atom->valuep,
4015 if (exec->state->nbTrans > exec->transno + 1) {
4016 if (exec->inputStackNr <= 0) {
4017 xmlFARegExecSaveInputString(exec, value, data);
4019 xmlFARegExecSave(exec);
4021 if (trans->counter >= 0) {
4023 printf("Increasing count %d\n", trans->counter);
4025 exec->counts[trans->counter]++;
4027 if ((trans->count >= 0) &&
4028 (trans->count < REGEXP_ALL_COUNTER)) {
4029 #ifdef DEBUG_REGEXP_EXEC
4030 printf("resetting count %d on transition\n",
4033 exec->counts[trans->count] = 0;
4036 printf("entering state %d\n", trans->to);
4038 if ((exec->comp->states[trans->to] != NULL) &&
4039 (exec->comp->states[trans->to]->type ==
4040 XML_REGEXP_SINK_STATE)) {
4042 * entering a sink state, save the current state as error
4045 if (exec->errString != NULL)
4046 xmlFree(exec->errString);
4047 exec->errString = xmlStrdup(value);
4048 exec->errState = exec->state;
4049 memcpy(exec->errCounts, exec->counts,
4050 exec->comp->nbCounters * sizeof(int));
4052 exec->state = exec->comp->states[trans->to];
4054 if (trans->atom != NULL) {
4055 if (exec->inputStack != NULL) {
4057 if (exec->index < exec->inputStackNr) {
4058 value = exec->inputStack[exec->index].value;
4059 data = exec->inputStack[exec->index].data;
4061 printf("value loaded: %s\n", value);
4067 printf("end of input\n");
4074 printf("end of input\n");
4079 } else if (ret < 0) {
4084 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4087 * if we didn't yet rollback on the current input
4088 * store the current state as the error state.
4090 if ((progress) && (exec->state != NULL) &&
4091 (exec->state->type != XML_REGEXP_SINK_STATE)) {
4093 if (exec->errString != NULL)
4094 xmlFree(exec->errString);
4095 exec->errString = xmlStrdup(value);
4096 exec->errState = exec->state;
4097 if (exec->comp->nbCounters)
4098 memcpy(exec->errCounts, exec->counts,
4099 exec->comp->nbCounters * sizeof(int));
4103 * Failed to find a way out
4105 exec->determinist = 0;
4106 xmlFARegExecRollBack(exec);
4107 if ((exec->inputStack != NULL ) && (exec->status == 0)) {
4108 value = exec->inputStack[exec->index].value;
4109 data = exec->inputStack[exec->index].data;
4111 printf("value loaded: %s\n", value);
4120 if (exec->status == 0) {
4121 return(exec->state->type == XML_REGEXP_FINAL_STATE);
4124 if (exec->status < 0) {
4128 return(exec->status);
4132 * xmlRegExecPushString:
4133 * @exec: a regexp execution context or NULL to indicate the end
4134 * @value: a string token input
4135 * @data: data associated to the token to reuse in callbacks
4137 * Push one input token in the execution context
4139 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4140 * a negative value in case of error.
4143 xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4145 return(xmlRegExecPushStringInternal(exec, value, data, 0));
4149 * xmlRegExecPushString2:
4150 * @exec: a regexp execution context or NULL to indicate the end
4151 * @value: the first string token input
4152 * @value2: the second string token input
4153 * @data: data associated to the token to reuse in callbacks
4155 * Push one input token in the execution context
4157 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4158 * a negative value in case of error.
4161 xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4162 const xmlChar *value2, void *data) {
4164 int lenn, lenp, ret;
4169 if (exec->comp == NULL)
4171 if (exec->status != 0)
4172 return(exec->status);
4175 return(xmlRegExecPushString(exec, value, data));
4177 lenn = strlen((char *) value2);
4178 lenp = strlen((char *) value);
4180 if (150 < lenn + lenp + 2) {
4181 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4189 memcpy(&str[0], value, lenp);
4190 str[lenp] = XML_REG_STRING_SEPARATOR;
4191 memcpy(&str[lenp + 1], value2, lenn);
4192 str[lenn + lenp + 1] = 0;
4194 if (exec->comp->compact != NULL)
4195 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4197 ret = xmlRegExecPushStringInternal(exec, str, data, 1);
4205 * xmlRegExecGetValues:
4206 * @exec: a regexp execution context
4207 * @err: error extraction or normal one
4208 * @nbval: pointer to the number of accepted values IN/OUT
4209 * @nbneg: return number of negative transitions
4210 * @values: pointer to the array of acceptable values
4211 * @terminal: return value if this was a terminal state
4213 * Extract informations from the regexp execution, internal routine to
4214 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
4216 * Returns: 0 in case of success or -1 in case of error.
4219 xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
4220 int *nbval, int *nbneg,
4221 xmlChar **values, int *terminal) {
4225 if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4226 (values == NULL) || (*nbval <= 0))
4232 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4234 int target, i, state;
4239 if (exec->errStateNo == -1) return(-1);
4240 state = exec->errStateNo;
4242 state = exec->index;
4244 if (terminal != NULL) {
4245 if (comp->compact[state * (comp->nbstrings + 1)] ==
4246 XML_REGEXP_FINAL_STATE)
4251 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4252 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4253 if ((target > 0) && (target <= comp->nbstates) &&
4254 (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4255 XML_REGEXP_SINK_STATE)) {
4256 values[nb++] = comp->stringMap[i];
4260 for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4261 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4262 if ((target > 0) && (target <= comp->nbstates) &&
4263 (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4264 XML_REGEXP_SINK_STATE)) {
4265 values[nb++] = comp->stringMap[i];
4271 xmlRegTransPtr trans;
4273 xmlRegStatePtr state;
4275 if (terminal != NULL) {
4276 if (exec->state->type == XML_REGEXP_FINAL_STATE)
4283 if (exec->errState == NULL) return(-1);
4284 state = exec->errState;
4286 if (exec->state == NULL) return(-1);
4287 state = exec->state;
4290 (transno < state->nbTrans) && (nb < maxval);
4292 trans = &state->trans[transno];
4296 if ((atom == NULL) || (atom->valuep == NULL))
4298 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4299 /* this should not be reached but ... */
4301 } else if (trans->count == REGEXP_ALL_COUNTER) {
4302 /* this should not be reached but ... */
4304 } else if (trans->counter >= 0) {
4305 xmlRegCounterPtr counter = NULL;
4309 count = exec->errCounts[trans->counter];
4311 count = exec->counts[trans->counter];
4312 if (exec->comp != NULL)
4313 counter = &exec->comp->counters[trans->counter];
4314 if ((counter == NULL) || (count < counter->max)) {
4316 values[nb++] = (xmlChar *) atom->valuep2;
4318 values[nb++] = (xmlChar *) atom->valuep;
4322 if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) &&
4323 (exec->comp->states[trans->to]->type !=
4324 XML_REGEXP_SINK_STATE)) {
4326 values[nb++] = (xmlChar *) atom->valuep2;
4328 values[nb++] = (xmlChar *) atom->valuep;
4334 (transno < state->nbTrans) && (nb < maxval);
4336 trans = &state->trans[transno];
4340 if ((atom == NULL) || (atom->valuep == NULL))
4342 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4344 } else if (trans->count == REGEXP_ALL_COUNTER) {
4346 } else if (trans->counter >= 0) {
4349 if ((exec->comp->states[trans->to] != NULL) &&
4350 (exec->comp->states[trans->to]->type ==
4351 XML_REGEXP_SINK_STATE)) {
4353 values[nb++] = (xmlChar *) atom->valuep2;
4355 values[nb++] = (xmlChar *) atom->valuep;
4365 * xmlRegExecNextValues:
4366 * @exec: a regexp execution context
4367 * @nbval: pointer to the number of accepted values IN/OUT
4368 * @nbneg: return number of negative transitions
4369 * @values: pointer to the array of acceptable values
4370 * @terminal: return value if this was a terminal state
4372 * Extract informations from the regexp execution,
4373 * the parameter @values must point to an array of @nbval string pointers
4374 * on return nbval will contain the number of possible strings in that
4375 * state and the @values array will be updated with them. The string values
4376 * returned will be freed with the @exec context and don't need to be
4379 * Returns: 0 in case of success or -1 in case of error.
4382 xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4383 xmlChar **values, int *terminal) {
4384 return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
4388 * xmlRegExecErrInfo:
4389 * @exec: a regexp execution context generating an error
4390 * @string: return value for the error string
4391 * @nbval: pointer to the number of accepted values IN/OUT
4392 * @nbneg: return number of negative transitions
4393 * @values: pointer to the array of acceptable values
4394 * @terminal: return value if this was a terminal state
4396 * Extract error informations from the regexp execution, the parameter
4397 * @string will be updated with the value pushed and not accepted,
4398 * the parameter @values must point to an array of @nbval string pointers
4399 * on return nbval will contain the number of possible strings in that
4400 * state and the @values array will be updated with them. The string values
4401 * returned will be freed with the @exec context and don't need to be
4404 * Returns: 0 in case of success or -1 in case of error.
4407 xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
4408 int *nbval, int *nbneg, xmlChar **values, int *terminal) {
4411 if (string != NULL) {
4412 if (exec->status != 0)
4413 *string = exec->errString;
4417 return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
4421 static void testerr(xmlRegExecCtxtPtr exec) {
4422 const xmlChar *string;
4427 xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
4433 xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4434 xmlRegTransPtr trans;
4441 if (exec->status != 0)
4442 return(exec->status);
4444 while ((exec->status == 0) &&
4445 ((exec->inputString[exec->index] != 0) ||
4446 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
4449 * End of input on non-terminal state, rollback, however we may
4450 * still have epsilon like transition for counted transitions
4451 * on counters, in that case don't break too early.
4453 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4456 exec->transcount = 0;
4457 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4458 trans = &exec->state->trans[exec->transno];
4463 if (trans->count >= 0) {
4465 xmlRegCounterPtr counter;
4468 * A counted transition.
4471 count = exec->counts[trans->count];
4472 counter = &exec->comp->counters[trans->count];
4473 #ifdef DEBUG_REGEXP_EXEC
4474 printf("testing count %d: val %d, min %d, max %d\n",
4475 trans->count, count, counter->min, counter->max);
4477 ret = ((count >= counter->min) && (count <= counter->max));
4478 } else if (atom == NULL) {
4479 fprintf(stderr, "epsilon transition left at runtime\n");
4482 } else if (exec->inputString[exec->index] != 0) {
4483 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4484 ret = xmlRegCheckCharacter(atom, codepoint);
4485 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4486 xmlRegStatePtr to = exec->comp->states[trans->to];
4489 * this is a multiple input sequence
4491 if (exec->state->nbTrans > exec->transno + 1) {
4492 xmlFARegExecSave(exec);
4494 exec->transcount = 1;
4497 * Try to progress as much as possible on the input
4499 if (exec->transcount == atom->max) {
4504 * End of input: stop here
4506 if (exec->inputString[exec->index] == 0) {
4510 if (exec->transcount >= atom->min) {
4511 int transno = exec->transno;
4512 xmlRegStatePtr state = exec->state;
4515 * The transition is acceptable save it
4517 exec->transno = -1; /* trick */
4519 xmlFARegExecSave(exec);
4520 exec->transno = transno;
4521 exec->state = state;
4523 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4525 ret = xmlRegCheckCharacter(atom, codepoint);
4528 if (exec->transcount < atom->min)
4532 * If the last check failed but one transition was found
4533 * possible, rollback
4543 if (exec->state->nbTrans > exec->transno + 1) {
4544 xmlFARegExecSave(exec);
4547 * restart count for expressions like this ((abc){2})*
4549 if (trans->count >= 0) {
4550 #ifdef DEBUG_REGEXP_EXEC
4551 printf("Reset count %d\n", trans->count);
4553 exec->counts[trans->count] = 0;
4555 if (trans->counter >= 0) {
4556 #ifdef DEBUG_REGEXP_EXEC
4557 printf("Increasing count %d\n", trans->counter);
4559 exec->counts[trans->counter]++;
4561 #ifdef DEBUG_REGEXP_EXEC
4562 printf("entering state %d\n", trans->to);
4564 exec->state = exec->comp->states[trans->to];
4566 if (trans->atom != NULL) {
4570 } else if (ret < 0) {
4575 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4578 * Failed to find a way out
4580 exec->determinist = 0;
4581 xmlFARegExecRollBack(exec);
4588 /************************************************************************
4590 * Parser for the Schemas Datatype Regular Expressions *
4591 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4593 ************************************************************************/
4597 * @ctxt: a regexp parser context
4599 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4602 xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4606 cur = CUR_SCHAR(ctxt->cur, len);
4607 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4608 (cur == '*') || (cur == '+') || (cur == '(') ||
4609 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
4610 (cur == 0x5D) || (cur == 0))
4616 * xmlFAParseCharProp:
4617 * @ctxt: a regexp parser context
4619 * [27] charProp ::= IsCategory | IsBlock
4620 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4621 * Separators | Symbols | Others
4622 * [29] Letters ::= 'L' [ultmo]?
4623 * [30] Marks ::= 'M' [nce]?
4624 * [31] Numbers ::= 'N' [dlo]?
4625 * [32] Punctuation ::= 'P' [cdseifo]?
4626 * [33] Separators ::= 'Z' [slp]?
4627 * [34] Symbols ::= 'S' [mcko]?
4628 * [35] Others ::= 'C' [cfon]?
4629 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4632 xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4634 xmlRegAtomType type = (xmlRegAtomType) 0;
4635 xmlChar *blockName = NULL;
4643 type = XML_REGEXP_LETTER_UPPERCASE;
4644 } else if (cur == 'l') {
4646 type = XML_REGEXP_LETTER_LOWERCASE;
4647 } else if (cur == 't') {
4649 type = XML_REGEXP_LETTER_TITLECASE;
4650 } else if (cur == 'm') {
4652 type = XML_REGEXP_LETTER_MODIFIER;
4653 } else if (cur == 'o') {
4655 type = XML_REGEXP_LETTER_OTHERS;
4657 type = XML_REGEXP_LETTER;
4659 } else if (cur == 'M') {
4665 type = XML_REGEXP_MARK_NONSPACING;
4666 } else if (cur == 'c') {
4668 /* spacing combining */
4669 type = XML_REGEXP_MARK_SPACECOMBINING;
4670 } else if (cur == 'e') {
4673 type = XML_REGEXP_MARK_ENCLOSING;
4676 type = XML_REGEXP_MARK;
4678 } else if (cur == 'N') {
4684 type = XML_REGEXP_NUMBER_DECIMAL;
4685 } else if (cur == 'l') {
4688 type = XML_REGEXP_NUMBER_LETTER;
4689 } else if (cur == 'o') {
4692 type = XML_REGEXP_NUMBER_OTHERS;
4695 type = XML_REGEXP_NUMBER;
4697 } else if (cur == 'P') {
4703 type = XML_REGEXP_PUNCT_CONNECTOR;
4704 } else if (cur == 'd') {
4707 type = XML_REGEXP_PUNCT_DASH;
4708 } else if (cur == 's') {
4711 type = XML_REGEXP_PUNCT_OPEN;
4712 } else if (cur == 'e') {
4715 type = XML_REGEXP_PUNCT_CLOSE;
4716 } else if (cur == 'i') {
4719 type = XML_REGEXP_PUNCT_INITQUOTE;
4720 } else if (cur == 'f') {
4723 type = XML_REGEXP_PUNCT_FINQUOTE;
4724 } else if (cur == 'o') {
4727 type = XML_REGEXP_PUNCT_OTHERS;
4729 /* all punctuation */
4730 type = XML_REGEXP_PUNCT;
4732 } else if (cur == 'Z') {
4738 type = XML_REGEXP_SEPAR_SPACE;
4739 } else if (cur == 'l') {
4742 type = XML_REGEXP_SEPAR_LINE;
4743 } else if (cur == 'p') {
4746 type = XML_REGEXP_SEPAR_PARA;
4748 /* all separators */
4749 type = XML_REGEXP_SEPAR;
4751 } else if (cur == 'S') {
4756 type = XML_REGEXP_SYMBOL_MATH;
4758 } else if (cur == 'c') {
4760 type = XML_REGEXP_SYMBOL_CURRENCY;
4762 } else if (cur == 'k') {
4764 type = XML_REGEXP_SYMBOL_MODIFIER;
4766 } else if (cur == 'o') {
4768 type = XML_REGEXP_SYMBOL_OTHERS;
4772 type = XML_REGEXP_SYMBOL;
4774 } else if (cur == 'C') {
4780 type = XML_REGEXP_OTHER_CONTROL;
4781 } else if (cur == 'f') {
4784 type = XML_REGEXP_OTHER_FORMAT;
4785 } else if (cur == 'o') {
4788 type = XML_REGEXP_OTHER_PRIVATE;
4789 } else if (cur == 'n') {
4792 type = XML_REGEXP_OTHER_NA;
4795 type = XML_REGEXP_OTHER;
4797 } else if (cur == 'I') {
4798 const xmlChar *start;
4802 ERROR("IsXXXX expected");
4808 if (((cur >= 'a') && (cur <= 'z')) ||
4809 ((cur >= 'A') && (cur <= 'Z')) ||
4810 ((cur >= '0') && (cur <= '9')) ||
4814 while (((cur >= 'a') && (cur <= 'z')) ||
4815 ((cur >= 'A') && (cur <= 'Z')) ||
4816 ((cur >= '0') && (cur <= '9')) ||
4822 type = XML_REGEXP_BLOCK_NAME;
4823 blockName = xmlStrndup(start, ctxt->cur - start);
4825 ERROR("Unknown char property");
4828 if (ctxt->atom == NULL) {
4829 ctxt->atom = xmlRegNewAtom(ctxt, type);
4830 if (ctxt->atom != NULL)
4831 ctxt->atom->valuep = blockName;
4832 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4833 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4834 type, 0, 0, blockName);
4839 * xmlFAParseCharClassEsc:
4840 * @ctxt: a regexp parser context
4842 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4843 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4844 * [25] catEsc ::= '\p{' charProp '}'
4845 * [26] complEsc ::= '\P{' charProp '}'
4846 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4849 xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4853 if (ctxt->atom == NULL) {
4854 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4855 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4856 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4857 XML_REGEXP_ANYCHAR, 0, 0, NULL);
4863 ERROR("Escaped sequence: expecting \\");
4871 ERROR("Expecting '{'");
4875 xmlFAParseCharProp(ctxt);
4877 ERROR("Expecting '}'");
4881 } else if (cur == 'P') {
4884 ERROR("Expecting '{'");
4888 xmlFAParseCharProp(ctxt);
4889 if (ctxt->atom != NULL)
4890 ctxt->atom->neg = 1;
4892 ERROR("Expecting '}'");
4896 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4897 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4898 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4899 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4901 if (ctxt->atom == NULL) {
4902 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4903 if (ctxt->atom != NULL) {
4906 ctxt->atom->codepoint = '\n';
4909 ctxt->atom->codepoint = '\r';
4912 ctxt->atom->codepoint = '\t';
4915 ctxt->atom->codepoint = cur;
4918 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4930 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4931 XML_REGEXP_CHARVAL, cur, cur, NULL);
4934 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4935 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4936 (cur == 'w') || (cur == 'W')) {
4937 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
4941 type = XML_REGEXP_ANYSPACE;
4944 type = XML_REGEXP_NOTSPACE;
4947 type = XML_REGEXP_INITNAME;
4950 type = XML_REGEXP_NOTINITNAME;
4953 type = XML_REGEXP_NAMECHAR;
4956 type = XML_REGEXP_NOTNAMECHAR;
4959 type = XML_REGEXP_DECIMAL;
4962 type = XML_REGEXP_NOTDECIMAL;
4965 type = XML_REGEXP_REALCHAR;
4968 type = XML_REGEXP_NOTREALCHAR;
4972 if (ctxt->atom == NULL) {
4973 ctxt->atom = xmlRegNewAtom(ctxt, type);
4974 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4975 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4979 ERROR("Wrong escape sequence, misuse of character '\\'");
4984 * xmlFAParseCharRange:
4985 * @ctxt: a regexp parser context
4987 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4988 * [18] seRange ::= charOrEsc '-' charOrEsc
4989 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4990 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4991 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4994 xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
5000 ERROR("Expecting ']'");
5009 case 'n': start = 0xA; break;
5010 case 'r': start = 0xD; break;
5011 case 't': start = 0x9; break;
5012 case '\\': case '|': case '.': case '-': case '^': case '?':
5013 case '*': case '+': case '{': case '}': case '(': case ')':
5017 ERROR("Invalid escape value");
5022 } else if ((cur != 0x5B) && (cur != 0x5D)) {
5023 end = start = CUR_SCHAR(ctxt->cur, len);
5025 ERROR("Expecting a char range");
5029 * Since we are "inside" a range, we can assume ctxt->cur is past
5030 * the start of ctxt->string, and PREV should be safe
5032 if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
5038 if ((cur != '-') || (NXT(1) == ']')) {
5039 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5040 XML_REGEXP_CHARVAL, start, end, NULL);
5049 case 'n': end = 0xA; break;
5050 case 'r': end = 0xD; break;
5051 case 't': end = 0x9; break;
5052 case '\\': case '|': case '.': case '-': case '^': case '?':
5053 case '*': case '+': case '{': case '}': case '(': case ')':
5057 ERROR("Invalid escape value");
5061 } else if ((cur != '\0') && (cur != 0x5B) && (cur != 0x5D)) {
5062 end = CUR_SCHAR(ctxt->cur, len);
5064 ERROR("Expecting the end of a char range");
5068 /* TODO check that the values are acceptable character ranges for XML */
5070 ERROR("End of range is before start of range");
5073 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5074 XML_REGEXP_CHARVAL, start, end, NULL);
5080 * xmlFAParsePosCharGroup:
5081 * @ctxt: a regexp parser context
5083 * [14] posCharGroup ::= ( charRange | charClassEsc )+
5086 xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5089 xmlFAParseCharClassEsc(ctxt);
5091 xmlFAParseCharRange(ctxt);
5093 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
5094 (CUR != 0) && (ctxt->error == 0));
5098 * xmlFAParseCharGroup:
5099 * @ctxt: a regexp parser context
5101 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5102 * [15] negCharGroup ::= '^' posCharGroup
5103 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5104 * [12] charClassExpr ::= '[' charGroup ']'
5107 xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5109 while ((CUR != ']') && (ctxt->error == 0)) {
5111 int neg = ctxt->neg;
5114 ctxt->neg = !ctxt->neg;
5115 xmlFAParsePosCharGroup(ctxt);
5117 } else if ((CUR == '-') && (NXT(1) == '[')) {
5118 int neg = ctxt->neg;
5120 NEXT; /* eat the '-' */
5121 NEXT; /* eat the '[' */
5122 xmlFAParseCharGroup(ctxt);
5126 ERROR("charClassExpr: ']' expected");
5131 } else if (CUR != ']') {
5132 xmlFAParsePosCharGroup(ctxt);
5139 * xmlFAParseCharClass:
5140 * @ctxt: a regexp parser context
5142 * [11] charClass ::= charClassEsc | charClassExpr
5143 * [12] charClassExpr ::= '[' charGroup ']'
5146 xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5149 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5150 if (ctxt->atom == NULL)
5152 xmlFAParseCharGroup(ctxt);
5156 ERROR("xmlFAParseCharClass: ']' expected");
5159 xmlFAParseCharClassEsc(ctxt);
5164 * xmlFAParseQuantExact:
5165 * @ctxt: a regexp parser context
5167 * [8] QuantExact ::= [0-9]+
5169 * Returns 0 if success or -1 in case of error
5172 xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5176 while ((CUR >= '0') && (CUR <= '9')) {
5177 ret = ret * 10 + (CUR - '0');
5188 * xmlFAParseQuantifier:
5189 * @ctxt: a regexp parser context
5191 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5192 * [5] quantity ::= quantRange | quantMin | QuantExact
5193 * [6] quantRange ::= QuantExact ',' QuantExact
5194 * [7] quantMin ::= QuantExact ','
5195 * [8] QuantExact ::= [0-9]+
5198 xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5202 if ((cur == '?') || (cur == '*') || (cur == '+')) {
5203 if (ctxt->atom != NULL) {
5205 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5206 else if (cur == '*')
5207 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5208 else if (cur == '+')
5209 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5215 int min = 0, max = 0;
5218 cur = xmlFAParseQuantExact(ctxt);
5226 cur = xmlFAParseQuantExact(ctxt);
5230 ERROR("Improper quantifier");
5237 ERROR("Unterminated quantifier");
5241 if (ctxt->atom != NULL) {
5242 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5243 ctxt->atom->min = min;
5244 ctxt->atom->max = max;
5253 * @ctxt: a regexp parser context
5255 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5258 xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5261 codepoint = xmlFAIsChar(ctxt);
5262 if (codepoint > 0) {
5263 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5264 if (ctxt->atom == NULL)
5266 codepoint = CUR_SCHAR(ctxt->cur, len);
5267 ctxt->atom->codepoint = codepoint;
5270 } else if (CUR == '|') {
5272 } else if (CUR == 0) {
5274 } else if (CUR == ')') {
5276 } else if (CUR == '(') {
5277 xmlRegStatePtr start, oldend, start0;
5281 * this extra Epsilon transition is needed if we count with 0 allowed
5282 * unfortunately this can't be known at that point
5284 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5285 start0 = ctxt->state;
5286 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5287 start = ctxt->state;
5291 xmlFAParseRegExp(ctxt, 0);
5295 ERROR("xmlFAParseAtom: expecting ')'");
5297 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5298 if (ctxt->atom == NULL)
5300 ctxt->atom->start = start;
5301 ctxt->atom->start0 = start0;
5302 ctxt->atom->stop = ctxt->state;
5305 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5306 xmlFAParseCharClass(ctxt);
5314 * @ctxt: a regexp parser context
5316 * [3] piece ::= atom quantifier?
5319 xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5323 ret = xmlFAParseAtom(ctxt);
5326 if (ctxt->atom == NULL) {
5327 ERROR("internal: no atom generated");
5329 xmlFAParseQuantifier(ctxt);
5335 * @ctxt: a regexp parser context
5336 * @to: optional target to the end of the branch
5338 * @to is used to optimize by removing duplicate path in automata
5339 * in expressions like (a|b)(c|d)
5341 * [2] branch ::= piece*
5344 xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
5345 xmlRegStatePtr previous;
5348 previous = ctxt->state;
5349 ret = xmlFAParsePiece(ctxt);
5351 if (xmlFAGenerateTransitions(ctxt, previous,
5352 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
5354 previous = ctxt->state;
5357 while ((ret != 0) && (ctxt->error == 0)) {
5358 ret = xmlFAParsePiece(ctxt);
5360 if (xmlFAGenerateTransitions(ctxt, previous,
5361 (CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
5363 previous = ctxt->state;
5372 * @ctxt: a regexp parser context
5373 * @top: is this the top-level expression ?
5375 * [1] regExp ::= branch ( '|' branch )*
5378 xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
5379 xmlRegStatePtr start, end;
5381 /* if not top start should have been generated by an epsilon trans */
5382 start = ctxt->state;
5384 xmlFAParseBranch(ctxt, NULL);
5386 #ifdef DEBUG_REGEXP_GRAPH
5387 printf("State %d is final\n", ctxt->state->no);
5389 ctxt->state->type = XML_REGEXP_FINAL_STATE;
5392 ctxt->end = ctxt->state;
5396 while ((CUR == '|') && (ctxt->error == 0)) {
5399 ERROR("expecting a branch after |")
5402 ctxt->state = start;
5404 xmlFAParseBranch(ctxt, end);
5412 /************************************************************************
5416 ************************************************************************/
5420 * @output: the file for the output debug
5421 * @regexp: the compiled regexp
5423 * Print the content of the compiled regular expression
5426 xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5431 fprintf(output, " regexp: ");
5432 if (regexp == NULL) {
5433 fprintf(output, "NULL\n");
5436 fprintf(output, "'%s' ", regexp->string);
5437 fprintf(output, "\n");
5438 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5439 for (i = 0;i < regexp->nbAtoms; i++) {
5440 fprintf(output, " %02d ", i);
5441 xmlRegPrintAtom(output, regexp->atoms[i]);
5443 fprintf(output, "%d states:", regexp->nbStates);
5444 fprintf(output, "\n");
5445 for (i = 0;i < regexp->nbStates; i++) {
5446 xmlRegPrintState(output, regexp->states[i]);
5448 fprintf(output, "%d counters:\n", regexp->nbCounters);
5449 for (i = 0;i < regexp->nbCounters; i++) {
5450 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5451 regexp->counters[i].max);
5457 * @regexp: a regular expression string
5459 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
5460 * Appendix F and builds an automata suitable for testing strings against
5461 * that regular expression
5463 * Returns the compiled expression or NULL in case of error
5466 xmlRegexpCompile(const xmlChar *regexp) {
5468 xmlRegParserCtxtPtr ctxt;
5470 ctxt = xmlRegNewParserCtxt(regexp);
5474 /* initialize the parser */
5476 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5477 xmlRegStatePush(ctxt, ctxt->start);
5479 /* parse the expression building an automata */
5480 xmlFAParseRegExp(ctxt, 1);
5482 ERROR("xmlFAParseRegExp: extra characters");
5484 if (ctxt->error != 0) {
5485 xmlRegFreeParserCtxt(ctxt);
5488 ctxt->end = ctxt->state;
5489 ctxt->start->type = XML_REGEXP_START_STATE;
5490 ctxt->end->type = XML_REGEXP_FINAL_STATE;
5492 /* remove the Epsilon except for counted transitions */
5493 xmlFAEliminateEpsilonTransitions(ctxt);
5496 if (ctxt->error != 0) {
5497 xmlRegFreeParserCtxt(ctxt);
5500 ret = xmlRegEpxFromParse(ctxt);
5501 xmlRegFreeParserCtxt(ctxt);
5507 * @comp: the compiled regular expression
5508 * @content: the value to check against the regular expression
5510 * Check if the regular expression generates the value
5512 * Returns 1 if it matches, 0 if not and a negative value in case of error
5515 xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5516 if ((comp == NULL) || (content == NULL))
5518 return(xmlFARegExec(comp, content));
5522 * xmlRegexpIsDeterminist:
5523 * @comp: the compiled regular expression
5525 * Check if the regular expression is determinist
5527 * Returns 1 if it yes, 0 if not and a negative value in case of error
5530 xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5536 if (comp->determinist != -1)
5537 return(comp->determinist);
5539 am = xmlNewAutomata();
5540 if (am->states != NULL) {
5543 for (i = 0;i < am->nbStates;i++)
5544 xmlRegFreeState(am->states[i]);
5545 xmlFree(am->states);
5547 am->nbAtoms = comp->nbAtoms;
5548 am->atoms = comp->atoms;
5549 am->nbStates = comp->nbStates;
5550 am->states = comp->states;
5551 am->determinist = -1;
5552 am->flags = comp->flags;
5553 ret = xmlFAComputesDeterminism(am);
5556 xmlFreeAutomata(am);
5557 comp->determinist = ret;
5563 * @regexp: the regexp
5568 xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5573 if (regexp->string != NULL)
5574 xmlFree(regexp->string);
5575 if (regexp->states != NULL) {
5576 for (i = 0;i < regexp->nbStates;i++)
5577 xmlRegFreeState(regexp->states[i]);
5578 xmlFree(regexp->states);
5580 if (regexp->atoms != NULL) {
5581 for (i = 0;i < regexp->nbAtoms;i++)
5582 xmlRegFreeAtom(regexp->atoms[i]);
5583 xmlFree(regexp->atoms);
5585 if (regexp->counters != NULL)
5586 xmlFree(regexp->counters);
5587 if (regexp->compact != NULL)
5588 xmlFree(regexp->compact);
5589 if (regexp->transdata != NULL)
5590 xmlFree(regexp->transdata);
5591 if (regexp->stringMap != NULL) {
5592 for (i = 0; i < regexp->nbstrings;i++)
5593 xmlFree(regexp->stringMap[i]);
5594 xmlFree(regexp->stringMap);
5600 #ifdef LIBXML_AUTOMATA_ENABLED
5601 /************************************************************************
5603 * The Automata interface *
5605 ************************************************************************/
5610 * Create a new automata
5612 * Returns the new object or NULL in case of failure
5615 xmlNewAutomata(void) {
5616 xmlAutomataPtr ctxt;
5618 ctxt = xmlRegNewParserCtxt(NULL);
5622 /* initialize the parser */
5624 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5625 if (ctxt->start == NULL) {
5626 xmlFreeAutomata(ctxt);
5629 ctxt->start->type = XML_REGEXP_START_STATE;
5630 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5631 xmlRegFreeState(ctxt->start);
5632 xmlFreeAutomata(ctxt);
5647 xmlFreeAutomata(xmlAutomataPtr am) {
5650 xmlRegFreeParserCtxt(am);
5654 * xmlAutomataSetFlags:
5656 * @flags: a set of internal flags
5658 * Set some flags on the automata
5661 xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
5668 * xmlAutomataGetInitState:
5671 * Initial state lookup
5673 * Returns the initial state of the automata
5676 xmlAutomataGetInitState(xmlAutomataPtr am) {
5683 * xmlAutomataSetFinalState:
5685 * @state: a state in this automata
5687 * Makes that state a final state
5689 * Returns 0 or -1 in case of error
5692 xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5693 if ((am == NULL) || (state == NULL))
5695 state->type = XML_REGEXP_FINAL_STATE;
5700 * xmlAutomataNewTransition:
5702 * @from: the starting point of the transition
5703 * @to: the target point of the transition or NULL
5704 * @token: the input string associated to that transition
5705 * @data: data passed to the callback function if the transition is activated
5707 * If @to is NULL, this creates first a new target state in the automata
5708 * and then adds a transition from the @from state to the target state
5709 * activated by the value of @token
5711 * Returns the target state or NULL in case of error
5714 xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5715 xmlAutomataStatePtr to, const xmlChar *token,
5719 if ((am == NULL) || (from == NULL) || (token == NULL))
5721 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5725 atom->valuep = xmlStrdup(token);
5727 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5728 xmlRegFreeAtom(atom);
5737 * xmlAutomataNewTransition2:
5739 * @from: the starting point of the transition
5740 * @to: the target point of the transition or NULL
5741 * @token: the first input string associated to that transition
5742 * @token2: the second input string associated to that transition
5743 * @data: data passed to the callback function if the transition is activated
5745 * If @to is NULL, this creates first a new target state in the automata
5746 * and then adds a transition from the @from state to the target state
5747 * activated by the value of @token
5749 * Returns the target state or NULL in case of error
5752 xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5753 xmlAutomataStatePtr to, const xmlChar *token,
5754 const xmlChar *token2, void *data) {
5757 if ((am == NULL) || (from == NULL) || (token == NULL))
5759 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5763 if ((token2 == NULL) || (*token2 == 0)) {
5764 atom->valuep = xmlStrdup(token);
5769 lenn = strlen((char *) token2);
5770 lenp = strlen((char *) token);
5772 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5774 xmlRegFreeAtom(atom);
5777 memcpy(&str[0], token, lenp);
5779 memcpy(&str[lenp + 1], token2, lenn);
5780 str[lenn + lenp + 1] = 0;
5785 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5786 xmlRegFreeAtom(atom);
5795 * xmlAutomataNewNegTrans:
5797 * @from: the starting point of the transition
5798 * @to: the target point of the transition or NULL
5799 * @token: the first input string associated to that transition
5800 * @token2: the second input string associated to that transition
5801 * @data: data passed to the callback function if the transition is activated
5803 * If @to is NULL, this creates first a new target state in the automata
5804 * and then adds a transition from the @from state to the target state
5805 * activated by any value except (@token,@token2)
5806 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5807 # the semantic of XSD ##other
5809 * Returns the target state or NULL in case of error
5812 xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5813 xmlAutomataStatePtr to, const xmlChar *token,
5814 const xmlChar *token2, void *data) {
5816 xmlChar err_msg[200];
5818 if ((am == NULL) || (from == NULL) || (token == NULL))
5820 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5825 if ((token2 == NULL) || (*token2 == 0)) {
5826 atom->valuep = xmlStrdup(token);
5831 lenn = strlen((char *) token2);
5832 lenp = strlen((char *) token);
5834 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5836 xmlRegFreeAtom(atom);
5839 memcpy(&str[0], token, lenp);
5841 memcpy(&str[lenp + 1], token2, lenn);
5842 str[lenn + lenp + 1] = 0;
5846 snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
5848 atom->valuep2 = xmlStrdup(err_msg);
5850 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5851 xmlRegFreeAtom(atom);
5861 * xmlAutomataNewCountTrans2:
5863 * @from: the starting point of the transition
5864 * @to: the target point of the transition or NULL
5865 * @token: the input string associated to that transition
5866 * @token2: the second input string associated to that transition
5867 * @min: the minimum successive occurences of token
5868 * @max: the maximum successive occurences of token
5869 * @data: data associated to the transition
5871 * If @to is NULL, this creates first a new target state in the automata
5872 * and then adds a transition from the @from state to the target state
5873 * activated by a succession of input of value @token and @token2 and
5874 * whose number is between @min and @max
5876 * Returns the target state or NULL in case of error
5879 xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5880 xmlAutomataStatePtr to, const xmlChar *token,
5881 const xmlChar *token2,
5882 int min, int max, void *data) {
5886 if ((am == NULL) || (from == NULL) || (token == NULL))
5890 if ((max < min) || (max < 1))
5892 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5895 if ((token2 == NULL) || (*token2 == 0)) {
5896 atom->valuep = xmlStrdup(token);
5901 lenn = strlen((char *) token2);
5902 lenp = strlen((char *) token);
5904 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5906 xmlRegFreeAtom(atom);
5909 memcpy(&str[0], token, lenp);
5911 memcpy(&str[lenp + 1], token2, lenn);
5912 str[lenn + lenp + 1] = 0;
5924 * associate a counter to the transition.
5926 counter = xmlRegGetCounter(am);
5927 am->counters[counter].min = min;
5928 am->counters[counter].max = max;
5930 /* xmlFAGenerateTransitions(am, from, to, atom); */
5932 to = xmlRegNewState(am);
5933 xmlRegStatePush(am, to);
5935 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5936 xmlRegAtomPush(am, atom);
5944 xmlFAGenerateEpsilonTransition(am, from, to);
5949 * xmlAutomataNewCountTrans:
5951 * @from: the starting point of the transition
5952 * @to: the target point of the transition or NULL
5953 * @token: the input string associated to that transition
5954 * @min: the minimum successive occurences of token
5955 * @max: the maximum successive occurences of token
5956 * @data: data associated to the transition
5958 * If @to is NULL, this creates first a new target state in the automata
5959 * and then adds a transition from the @from state to the target state
5960 * activated by a succession of input of value @token and whose number
5961 * is between @min and @max
5963 * Returns the target state or NULL in case of error
5966 xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5967 xmlAutomataStatePtr to, const xmlChar *token,
5968 int min, int max, void *data) {
5972 if ((am == NULL) || (from == NULL) || (token == NULL))
5976 if ((max < min) || (max < 1))
5978 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5981 atom->valuep = xmlStrdup(token);
5990 * associate a counter to the transition.
5992 counter = xmlRegGetCounter(am);
5993 am->counters[counter].min = min;
5994 am->counters[counter].max = max;
5996 /* xmlFAGenerateTransitions(am, from, to, atom); */
5998 to = xmlRegNewState(am);
5999 xmlRegStatePush(am, to);
6001 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6002 xmlRegAtomPush(am, atom);
6010 xmlFAGenerateEpsilonTransition(am, from, to);
6015 * xmlAutomataNewOnceTrans2:
6017 * @from: the starting point of the transition
6018 * @to: the target point of the transition or NULL
6019 * @token: the input string associated to that transition
6020 * @token2: the second input string associated to that transition
6021 * @min: the minimum successive occurences of token
6022 * @max: the maximum successive occurences of token
6023 * @data: data associated to the transition
6025 * If @to is NULL, this creates first a new target state in the automata
6026 * and then adds a transition from the @from state to the target state
6027 * activated by a succession of input of value @token and @token2 and whose
6028 * number is between @min and @max, moreover that transition can only be
6031 * Returns the target state or NULL in case of error
6034 xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
6035 xmlAutomataStatePtr to, const xmlChar *token,
6036 const xmlChar *token2,
6037 int min, int max, void *data) {
6041 if ((am == NULL) || (from == NULL) || (token == NULL))
6045 if ((max < min) || (max < 1))
6047 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6050 if ((token2 == NULL) || (*token2 == 0)) {
6051 atom->valuep = xmlStrdup(token);
6056 lenn = strlen((char *) token2);
6057 lenp = strlen((char *) token);
6059 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6061 xmlRegFreeAtom(atom);
6064 memcpy(&str[0], token, lenp);
6066 memcpy(&str[lenp + 1], token2, lenn);
6067 str[lenn + lenp + 1] = 0;
6072 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6076 * associate a counter to the transition.
6078 counter = xmlRegGetCounter(am);
6079 am->counters[counter].min = 1;
6080 am->counters[counter].max = 1;
6082 /* xmlFAGenerateTransitions(am, from, to, atom); */
6084 to = xmlRegNewState(am);
6085 xmlRegStatePush(am, to);
6087 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6088 xmlRegAtomPush(am, atom);
6096 * xmlAutomataNewOnceTrans:
6098 * @from: the starting point of the transition
6099 * @to: the target point of the transition or NULL
6100 * @token: the input string associated to that transition
6101 * @min: the minimum successive occurences of token
6102 * @max: the maximum successive occurences of token
6103 * @data: data associated to the transition
6105 * If @to is NULL, this creates first a new target state in the automata
6106 * and then adds a transition from the @from state to the target state
6107 * activated by a succession of input of value @token and whose number
6108 * is between @min and @max, moreover that transition can only be crossed
6111 * Returns the target state or NULL in case of error
6114 xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6115 xmlAutomataStatePtr to, const xmlChar *token,
6116 int min, int max, void *data) {
6120 if ((am == NULL) || (from == NULL) || (token == NULL))
6124 if ((max < min) || (max < 1))
6126 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6129 atom->valuep = xmlStrdup(token);
6131 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6135 * associate a counter to the transition.
6137 counter = xmlRegGetCounter(am);
6138 am->counters[counter].min = 1;
6139 am->counters[counter].max = 1;
6141 /* xmlFAGenerateTransitions(am, from, to, atom); */
6143 to = xmlRegNewState(am);
6144 xmlRegStatePush(am, to);
6146 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6147 xmlRegAtomPush(am, atom);
6153 * xmlAutomataNewState:
6156 * Create a new disconnected state in the automata
6158 * Returns the new state or NULL in case of error
6161 xmlAutomataNewState(xmlAutomataPtr am) {
6162 xmlAutomataStatePtr to;
6166 to = xmlRegNewState(am);
6167 xmlRegStatePush(am, to);
6172 * xmlAutomataNewEpsilon:
6174 * @from: the starting point of the transition
6175 * @to: the target point of the transition or NULL
6177 * If @to is NULL, this creates first a new target state in the automata
6178 * and then adds an epsilon transition from the @from state to the
6181 * Returns the target state or NULL in case of error
6184 xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6185 xmlAutomataStatePtr to) {
6186 if ((am == NULL) || (from == NULL))
6188 xmlFAGenerateEpsilonTransition(am, from, to);
6195 * xmlAutomataNewAllTrans:
6197 * @from: the starting point of the transition
6198 * @to: the target point of the transition or NULL
6199 * @lax: allow to transition if not all all transitions have been activated
6201 * If @to is NULL, this creates first a new target state in the automata
6202 * and then adds a an ALL transition from the @from state to the
6203 * target state. That transition is an epsilon transition allowed only when
6204 * all transitions from the @from node have been activated.
6206 * Returns the target state or NULL in case of error
6209 xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6210 xmlAutomataStatePtr to, int lax) {
6211 if ((am == NULL) || (from == NULL))
6213 xmlFAGenerateAllTransition(am, from, to, lax);
6220 * xmlAutomataNewCounter:
6222 * @min: the minimal value on the counter
6223 * @max: the maximal value on the counter
6225 * Create a new counter
6227 * Returns the counter number or -1 in case of error
6230 xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6236 ret = xmlRegGetCounter(am);
6239 am->counters[ret].min = min;
6240 am->counters[ret].max = max;
6245 * xmlAutomataNewCountedTrans:
6247 * @from: the starting point of the transition
6248 * @to: the target point of the transition or NULL
6249 * @counter: the counter associated to that transition
6251 * If @to is NULL, this creates first a new target state in the automata
6252 * and then adds an epsilon transition from the @from state to the target state
6253 * which will increment the counter provided
6255 * Returns the target state or NULL in case of error
6258 xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6259 xmlAutomataStatePtr to, int counter) {
6260 if ((am == NULL) || (from == NULL) || (counter < 0))
6262 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6269 * xmlAutomataNewCounterTrans:
6271 * @from: the starting point of the transition
6272 * @to: the target point of the transition or NULL
6273 * @counter: the counter associated to that transition
6275 * If @to is NULL, this creates first a new target state in the automata
6276 * and then adds an epsilon transition from the @from state to the target state
6277 * which will be allowed only if the counter is within the right range.
6279 * Returns the target state or NULL in case of error
6282 xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6283 xmlAutomataStatePtr to, int counter) {
6284 if ((am == NULL) || (from == NULL) || (counter < 0))
6286 xmlFAGenerateCountedTransition(am, from, to, counter);
6293 * xmlAutomataCompile:
6296 * Compile the automata into a Reg Exp ready for being executed.
6297 * The automata should be free after this point.
6299 * Returns the compiled regexp or NULL in case of error
6302 xmlAutomataCompile(xmlAutomataPtr am) {
6305 if ((am == NULL) || (am->error != 0)) return(NULL);
6306 xmlFAEliminateEpsilonTransitions(am);
6307 /* xmlFAComputesDeterminism(am); */
6308 ret = xmlRegEpxFromParse(am);
6314 * xmlAutomataIsDeterminist:
6317 * Checks if an automata is determinist.
6319 * Returns 1 if true, 0 if not, and -1 in case of error
6322 xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6328 ret = xmlFAComputesDeterminism(am);
6331 #endif /* LIBXML_AUTOMATA_ENABLED */
6333 #ifdef LIBXML_EXPR_ENABLED
6334 /************************************************************************
6336 * Formal Expression handling code *
6338 ************************************************************************/
6339 /************************************************************************
6341 * Expression handling context *
6343 ************************************************************************/
6345 struct _xmlExpCtxt {
6347 xmlExpNodePtr *table;
6360 * @maxNodes: the maximum number of nodes
6361 * @dict: optional dictionary to use internally
6363 * Creates a new context for manipulating expressions
6365 * Returns the context or NULL in case of error
6368 xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6372 if (maxNodes <= 4096)
6375 ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6378 memset(ret, 0, sizeof(xmlExpCtxt));
6381 ret->maxNodes = maxNodes;
6382 ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6383 if (ret->table == NULL) {
6387 memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6389 ret->dict = xmlDictCreate();
6390 if (ret->dict == NULL) {
6391 xmlFree(ret->table);
6397 xmlDictReference(ret->dict);
6404 * @ctxt: an expression context
6406 * Free an expression context
6409 xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6412 xmlDictFree(ctxt->dict);
6413 if (ctxt->table != NULL)
6414 xmlFree(ctxt->table);
6418 /************************************************************************
6420 * Structure associated to an expression node *
6422 ************************************************************************/
6423 #define MAX_NODES 10000
6425 /* #define DEBUG_DERIV */
6430 * - public API for creation
6433 * - regression testing
6436 * - split into module and test tool
6441 XML_EXP_NILABLE = (1 << 0)
6444 #define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6446 struct _xmlExpNode {
6447 unsigned char type;/* xmlExpNodeType */
6448 unsigned char info;/* OR of xmlExpNodeInfo */
6449 unsigned short key; /* the hash key */
6450 unsigned int ref; /* The number of references */
6451 int c_max; /* the maximum length it can consume */
6452 xmlExpNodePtr exp_left;
6453 xmlExpNodePtr next;/* the next node in the hash table or free list */
6460 xmlExpNodePtr f_right;
6462 const xmlChar *f_str;
6466 #define exp_min field.count.f_min
6467 #define exp_max field.count.f_max
6468 /* #define exp_left field.children.f_left */
6469 #define exp_right field.children.f_right
6470 #define exp_str field.f_str
6472 static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6473 static xmlExpNode forbiddenExpNode = {
6474 XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6476 xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6477 static xmlExpNode emptyExpNode = {
6478 XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6480 xmlExpNodePtr emptyExp = &emptyExpNode;
6482 /************************************************************************
6484 * The custom hash table for unicity and canonicalization *
6485 * of sub-expressions pointers *
6487 ************************************************************************/
6489 * xmlExpHashNameComputeKey:
6490 * Calculate the hash key for a token
6492 static unsigned short
6493 xmlExpHashNameComputeKey(const xmlChar *name) {
6494 unsigned short value = 0L;
6498 value += 30 * (*name);
6499 while ((ch = *name++) != 0) {
6500 value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6507 * xmlExpHashComputeKey:
6508 * Calculate the hash key for a compound expression
6510 static unsigned short
6511 xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6512 xmlExpNodePtr right) {
6513 unsigned long value;
6519 value += right->key;
6521 ret = (unsigned short) value;
6525 value += right->key;
6527 ret = (unsigned short) value;
6531 value += right->key;
6532 ret = (unsigned short) value;
6541 static xmlExpNodePtr
6542 xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6545 if (ctxt->nb_nodes >= MAX_NODES)
6547 ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6550 memset(ret, 0, sizeof(xmlExpNode));
6559 * xmlExpHashGetEntry:
6560 * @table: the hash table
6562 * Get the unique entry from the hash table. The entry is created if
6563 * needed. @left and @right are consumed, i.e. their ref count will
6564 * be decremented by the operation.
6566 * Returns the pointer or NULL in case of error
6568 static xmlExpNodePtr
6569 xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6570 xmlExpNodePtr left, xmlExpNodePtr right,
6571 const xmlChar *name, int min, int max) {
6572 unsigned short kbase, key;
6573 xmlExpNodePtr entry;
6574 xmlExpNodePtr insert;
6580 * Check for duplicate and insertion location.
6582 if (type == XML_EXP_ATOM) {
6583 kbase = xmlExpHashNameComputeKey(name);
6584 } else if (type == XML_EXP_COUNT) {
6585 /* COUNT reduction rule 1 */
6592 xmlExpFree(ctxt, left);
6597 xmlExpFree(ctxt, left);
6598 return(forbiddenExp);
6605 } else if (type == XML_EXP_OR) {
6606 /* Forbid reduction rules */
6607 if (left->type == XML_EXP_FORBID) {
6608 xmlExpFree(ctxt, left);
6611 if (right->type == XML_EXP_FORBID) {
6612 xmlExpFree(ctxt, right);
6616 /* OR reduction rule 1 */
6617 /* a | a reduced to a */
6618 if (left == right) {
6622 /* OR canonicalization rule 1 */
6623 /* linearize (a | b) | c into a | (b | c) */
6624 if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6625 xmlExpNodePtr tmp = left;
6629 /* OR reduction rule 2 */
6630 /* a | (a | b) and b | (a | b) are reduced to a | b */
6631 if (right->type == XML_EXP_OR) {
6632 if ((left == right->exp_left) ||
6633 (left == right->exp_right)) {
6634 xmlExpFree(ctxt, left);
6638 /* OR canonicalization rule 2 */
6639 /* linearize (a | b) | c into a | (b | c) */
6640 if (left->type == XML_EXP_OR) {
6643 /* OR canonicalization rule 2 */
6644 if ((left->exp_right->type != XML_EXP_OR) &&
6645 (left->exp_right->key < left->exp_left->key)) {
6646 tmp = left->exp_right;
6647 left->exp_right = left->exp_left;
6648 left->exp_left = tmp;
6650 left->exp_right->ref++;
6651 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6653 left->exp_left->ref++;
6654 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6657 xmlExpFree(ctxt, left);
6660 if (right->type == XML_EXP_OR) {
6661 /* Ordering in the tree */
6662 /* C | (A | B) -> A | (B | C) */
6663 if (left->key > right->exp_right->key) {
6665 right->exp_right->ref++;
6666 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6668 right->exp_left->ref++;
6669 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6671 xmlExpFree(ctxt, right);
6674 /* Ordering in the tree */
6675 /* B | (A | C) -> A | (B | C) */
6676 if (left->key > right->exp_left->key) {
6678 right->exp_right->ref++;
6679 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6680 right->exp_right, NULL, 0, 0);
6681 right->exp_left->ref++;
6682 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6684 xmlExpFree(ctxt, right);
6688 /* we know both types are != XML_EXP_OR here */
6689 else if (left->key > right->key) {
6690 xmlExpNodePtr tmp = left;
6694 kbase = xmlExpHashComputeKey(type, left, right);
6695 } else if (type == XML_EXP_SEQ) {
6696 /* Forbid reduction rules */
6697 if (left->type == XML_EXP_FORBID) {
6698 xmlExpFree(ctxt, right);
6701 if (right->type == XML_EXP_FORBID) {
6702 xmlExpFree(ctxt, left);
6705 /* Empty reduction rules */
6706 if (right->type == XML_EXP_EMPTY) {
6709 if (left->type == XML_EXP_EMPTY) {
6712 kbase = xmlExpHashComputeKey(type, left, right);
6716 key = kbase % ctxt->size;
6717 if (ctxt->table[key] != NULL) {
6718 for (insert = ctxt->table[key]; insert != NULL;
6719 insert = insert->next) {
6720 if ((insert->key == kbase) &&
6721 (insert->type == type)) {
6722 if (type == XML_EXP_ATOM) {
6723 if (name == insert->exp_str) {
6727 } else if (type == XML_EXP_COUNT) {
6728 if ((insert->exp_min == min) && (insert->exp_max == max) &&
6729 (insert->exp_left == left)) {
6734 } else if ((insert->exp_left == left) &&
6735 (insert->exp_right == right)) {
6745 entry = xmlExpNewNode(ctxt, type);
6749 if (type == XML_EXP_ATOM) {
6750 entry->exp_str = name;
6752 } else if (type == XML_EXP_COUNT) {
6753 entry->exp_min = min;
6754 entry->exp_max = max;
6755 entry->exp_left = left;
6756 if ((min == 0) || (IS_NILLABLE(left)))
6757 entry->info |= XML_EXP_NILABLE;
6761 entry->c_max = max * entry->exp_left->c_max;
6763 entry->exp_left = left;
6764 entry->exp_right = right;
6765 if (type == XML_EXP_OR) {
6766 if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6767 entry->info |= XML_EXP_NILABLE;
6768 if ((entry->exp_left->c_max == -1) ||
6769 (entry->exp_right->c_max == -1))
6771 else if (entry->exp_left->c_max > entry->exp_right->c_max)
6772 entry->c_max = entry->exp_left->c_max;
6774 entry->c_max = entry->exp_right->c_max;
6776 if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6777 entry->info |= XML_EXP_NILABLE;
6778 if ((entry->exp_left->c_max == -1) ||
6779 (entry->exp_right->c_max == -1))
6782 entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6786 if (ctxt->table[key] != NULL)
6787 entry->next = ctxt->table[key];
6789 ctxt->table[key] = entry;
6797 * @ctxt: the expression context
6798 * @exp: the expression
6800 * Dereference the expression
6803 xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6804 if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6807 if (exp->ref == 0) {
6810 /* Unlink it first from the hash table */
6811 key = exp->key % ctxt->size;
6812 if (ctxt->table[key] == exp) {
6813 ctxt->table[key] = exp->next;
6817 tmp = ctxt->table[key];
6818 while (tmp != NULL) {
6819 if (tmp->next == exp) {
6820 tmp->next = exp->next;
6827 if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6828 xmlExpFree(ctxt, exp->exp_left);
6829 xmlExpFree(ctxt, exp->exp_right);
6830 } else if (exp->type == XML_EXP_COUNT) {
6831 xmlExpFree(ctxt, exp->exp_left);
6840 * @exp: the expression
6842 * Increase the reference count of the expression
6845 xmlExpRef(xmlExpNodePtr exp) {
6852 * @ctxt: the expression context
6853 * @name: the atom name
6854 * @len: the atom name length in byte (or -1);
6856 * Get the atom associated to this name from that context
6858 * Returns the node or NULL in case of error
6861 xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6862 if ((ctxt == NULL) || (name == NULL))
6864 name = xmlDictLookup(ctxt->dict, name, len);
6867 return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6872 * @ctxt: the expression context
6873 * @left: left expression
6874 * @right: right expression
6876 * Get the atom associated to the choice @left | @right
6877 * Note that @left and @right are consumed in the operation, to keep
6878 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6879 * this is true even in case of failure (unless ctxt == NULL).
6881 * Returns the node or NULL in case of error
6884 xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6887 if ((left == NULL) || (right == NULL)) {
6888 xmlExpFree(ctxt, left);
6889 xmlExpFree(ctxt, right);
6892 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6897 * @ctxt: the expression context
6898 * @left: left expression
6899 * @right: right expression
6901 * Get the atom associated to the sequence @left , @right
6902 * Note that @left and @right are consumed in the operation, to keep
6903 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6904 * this is true even in case of failure (unless ctxt == NULL).
6906 * Returns the node or NULL in case of error
6909 xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6912 if ((left == NULL) || (right == NULL)) {
6913 xmlExpFree(ctxt, left);
6914 xmlExpFree(ctxt, right);
6917 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6922 * @ctxt: the expression context
6923 * @subset: the expression to be repeated
6924 * @min: the lower bound for the repetition
6925 * @max: the upper bound for the repetition, -1 means infinite
6927 * Get the atom associated to the range (@subset){@min, @max}
6928 * Note that @subset is consumed in the operation, to keep
6929 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6930 * this is true even in case of failure (unless ctxt == NULL).
6932 * Returns the node or NULL in case of error
6935 xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
6938 if ((subset == NULL) || (min < 0) || (max < -1) ||
6939 ((max >= 0) && (min > max))) {
6940 xmlExpFree(ctxt, subset);
6943 return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6944 NULL, NULL, min, max));
6947 /************************************************************************
6949 * Public API for operations on expressions *
6951 ************************************************************************/
6954 xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6955 const xmlChar**list, int len, int nb) {
6958 switch (exp->type) {
6962 for (tmp = 0;tmp < nb;tmp++)
6963 if (list[tmp] == exp->exp_str)
6967 list[nb] = exp->exp_str;
6970 exp = exp->exp_left;
6974 tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6977 tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6987 * xmlExpGetLanguage:
6988 * @ctxt: the expression context
6989 * @exp: the expression
6990 * @langList: where to store the tokens
6991 * @len: the allocated length of @list
6993 * Find all the strings used in @exp and store them in @list
6995 * Returns the number of unique strings found, -1 in case of errors and
6996 * -2 if there is more than @len strings
6999 xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7000 const xmlChar**langList, int len) {
7001 if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
7003 return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
7007 xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7008 const xmlChar**list, int len, int nb) {
7011 switch (exp->type) {
7012 case XML_EXP_FORBID:
7017 for (tmp = 0;tmp < nb;tmp++)
7018 if (list[tmp] == exp->exp_str)
7022 list[nb] = exp->exp_str;
7025 exp = exp->exp_left;
7028 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7031 if (IS_NILLABLE(exp->exp_left)) {
7032 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7040 tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7043 tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7054 * @ctxt: the expression context
7055 * @exp: the expression
7056 * @tokList: where to store the tokens
7057 * @len: the allocated length of @list
7059 * Find all the strings that appears at the start of the languages
7060 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7061 * it will return the list [a, c]
7063 * Returns the number of unique strings found, -1 in case of errors and
7064 * -2 if there is more than @len strings
7067 xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7068 const xmlChar**tokList, int len) {
7069 if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
7071 return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
7076 * @exp: the expression
7078 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
7080 * Returns 1 if nillable, 0 if not and -1 in case of error
7083 xmlExpIsNillable(xmlExpNodePtr exp) {
7086 return(IS_NILLABLE(exp) != 0);
7089 static xmlExpNodePtr
7090 xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7094 switch (exp->type) {
7096 return(forbiddenExp);
7097 case XML_EXP_FORBID:
7098 return(forbiddenExp);
7100 if (exp->exp_str == str) {
7102 printf("deriv atom: equal => Empty\n");
7107 printf("deriv atom: mismatch => forbid\n");
7109 /* TODO wildcards here */
7117 printf("deriv or: => or(derivs)\n");
7119 tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7123 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7125 xmlExpFree(ctxt, tmp);
7128 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7134 printf("deriv seq: starting with left\n");
7136 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7139 } else if (ret == forbiddenExp) {
7140 if (IS_NILLABLE(exp->exp_left)) {
7142 printf("deriv seq: left failed but nillable\n");
7144 ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7148 printf("deriv seq: left match => sequence\n");
7150 exp->exp_right->ref++;
7151 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7155 case XML_EXP_COUNT: {
7159 if (exp->exp_max == 0)
7160 return(forbiddenExp);
7161 ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7164 if (ret == forbiddenExp) {
7166 printf("deriv count: pattern mismatch => forbid\n");
7170 if (exp->exp_max == 1)
7172 if (exp->exp_max < 0) /* unbounded */
7175 max = exp->exp_max - 1;
7176 if (exp->exp_min > 0)
7177 min = exp->exp_min - 1;
7180 exp->exp_left->ref++;
7181 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7183 if (ret == emptyExp) {
7185 printf("deriv count: match to empty => new count\n");
7190 printf("deriv count: match => sequence with new count\n");
7192 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7200 * xmlExpStringDerive:
7201 * @ctxt: the expression context
7202 * @exp: the expression
7204 * @len: the string len in bytes if available
7206 * Do one step of Brzozowski derivation of the expression @exp with
7207 * respect to the input string
7209 * Returns the resulting expression or NULL in case of internal error
7212 xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7213 const xmlChar *str, int len) {
7214 const xmlChar *input;
7216 if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7220 * check the string is in the dictionary, if yes use an interned
7221 * copy, otherwise we know it's not an acceptable input
7223 input = xmlDictExists(ctxt->dict, str, len);
7224 if (input == NULL) {
7225 return(forbiddenExp);
7227 return(xmlExpStringDeriveInt(ctxt, exp, input));
7231 xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7234 if (sub->c_max == -1) {
7235 if (exp->c_max != -1)
7237 } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7241 if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7247 static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7251 * @ctxt: the expressions context
7252 * @exp: the englobing expression
7253 * @sub: the subexpression
7254 * @mult: the multiple expression
7255 * @remain: the remain from the derivation of the multiple
7257 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7258 * so that sub{n} subsume exp
7260 * Returns the multiple value if successful, 0 if it is not a multiple
7261 * and -1 in case of internel error.
7265 xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7266 xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7268 xmlExpNodePtr tmp, tmp2;
7270 if (mult != NULL) *mult = NULL;
7271 if (remain != NULL) *remain = NULL;
7272 if (exp->c_max == -1) return(0);
7273 if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7275 for (i = 1;i <= exp->c_max;i++) {
7277 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7278 sub, NULL, NULL, i, i);
7282 if (!xmlExpCheckCard(tmp, exp)) {
7283 xmlExpFree(ctxt, tmp);
7286 tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7288 xmlExpFree(ctxt, tmp);
7291 if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7295 xmlExpFree(ctxt, tmp2);
7299 xmlExpFree(ctxt, tmp);
7301 printf("Divide succeeded %d\n", i);
7305 xmlExpFree(ctxt, tmp);
7306 xmlExpFree(ctxt, tmp2);
7309 printf("Divide failed\n");
7315 * xmlExpExpDeriveInt:
7316 * @ctxt: the expressions context
7317 * @exp: the englobing expression
7318 * @sub: the subexpression
7320 * Try to do a step of Brzozowski derivation but at a higher level
7321 * the input being a subexpression.
7323 * Returns the resulting expression or NULL in case of internal error
7325 static xmlExpNodePtr
7326 xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7327 xmlExpNodePtr ret, tmp, tmp2, tmp3;
7328 const xmlChar **tab;
7332 * In case of equality and if the expression can only consume a finite
7333 * amount, then the derivation is empty
7335 if ((exp == sub) && (exp->c_max >= 0)) {
7337 printf("Equal(exp, sub) and finite -> Empty\n");
7342 * decompose sub sequence first
7344 if (sub->type == XML_EXP_EMPTY) {
7346 printf("Empty(sub) -> Empty\n");
7351 if (sub->type == XML_EXP_SEQ) {
7353 printf("Seq(sub) -> decompose\n");
7355 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7358 if (tmp == forbiddenExp)
7360 ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7361 xmlExpFree(ctxt, tmp);
7364 if (sub->type == XML_EXP_OR) {
7366 printf("Or(sub) -> decompose\n");
7368 tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7369 if (tmp == forbiddenExp)
7373 ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7374 if ((ret == NULL) || (ret == forbiddenExp)) {
7375 xmlExpFree(ctxt, tmp);
7378 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7380 if (!xmlExpCheckCard(exp, sub)) {
7382 printf("CheckCard(exp, sub) failed -> Forbid\n");
7384 return(forbiddenExp);
7386 switch (exp->type) {
7388 if (sub == emptyExp)
7391 printf("Empty(exp) -> Forbid\n");
7393 return(forbiddenExp);
7394 case XML_EXP_FORBID:
7396 printf("Forbid(exp) -> Forbid\n");
7398 return(forbiddenExp);
7400 if (sub->type == XML_EXP_ATOM) {
7401 /* TODO: handle wildcards */
7402 if (exp->exp_str == sub->exp_str) {
7404 printf("Atom match -> Empty\n");
7409 printf("Atom mismatch -> Forbid\n");
7411 return(forbiddenExp);
7413 if ((sub->type == XML_EXP_COUNT) &&
7414 (sub->exp_max == 1) &&
7415 (sub->exp_left->type == XML_EXP_ATOM)) {
7416 /* TODO: handle wildcards */
7417 if (exp->exp_str == sub->exp_left->exp_str) {
7419 printf("Atom match -> Empty\n");
7424 printf("Atom mismatch -> Forbid\n");
7426 return(forbiddenExp);
7429 printf("Compex exp vs Atom -> Forbid\n");
7431 return(forbiddenExp);
7433 /* try to get the sequence consumed only if possible */
7434 if (xmlExpCheckCard(exp->exp_left, sub)) {
7435 /* See if the sequence can be consumed directly */
7437 printf("Seq trying left only\n");
7439 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7440 if ((ret != forbiddenExp) && (ret != NULL)) {
7442 printf("Seq trying left only worked\n");
7445 * TODO: assumption here that we are determinist
7446 * i.e. we won't get to a nillable exp left
7447 * subset which could be matched by the right
7449 * e.g.: (a | b)+,(a | c) and 'a+,a'
7451 exp->exp_right->ref++;
7452 return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7453 exp->exp_right, NULL, 0, 0));
7457 printf("Seq: left too short\n");
7460 /* Try instead to decompose */
7461 if (sub->type == XML_EXP_COUNT) {
7465 printf("Seq: sub is a count\n");
7467 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7470 if (ret != forbiddenExp) {
7472 printf("Seq , Count match on left\n");
7474 if (sub->exp_max < 0)
7477 max = sub->exp_max -1;
7478 if (sub->exp_min > 0)
7479 min = sub->exp_min -1;
7482 exp->exp_right->ref++;
7483 tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7484 exp->exp_right, NULL, 0, 0);
7488 sub->exp_left->ref++;
7489 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7490 sub->exp_left, NULL, NULL, min, max);
7492 xmlExpFree(ctxt, tmp);
7495 ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7496 xmlExpFree(ctxt, tmp);
7497 xmlExpFree(ctxt, tmp2);
7501 /* we made no progress on structured operations */
7505 printf("Or , trying both side\n");
7507 ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7510 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7512 xmlExpFree(ctxt, ret);
7515 return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7516 case XML_EXP_COUNT: {
7519 if (sub->type == XML_EXP_COUNT) {
7521 * Try to see if the loop is completely subsumed
7523 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7526 if (tmp == forbiddenExp) {
7530 printf("Count, Count inner don't subsume\n");
7532 mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7536 printf("Count, Count not multiple => forbidden\n");
7538 return(forbiddenExp);
7540 if (sub->exp_max == -1) {
7542 if (exp->exp_max == -1) {
7543 if (exp->exp_min <= sub->exp_min * mult)
7546 min = exp->exp_min - sub->exp_min * mult;
7549 printf("Count, Count finite can't subsume infinite\n");
7551 xmlExpFree(ctxt, tmp);
7552 return(forbiddenExp);
7555 if (exp->exp_max == -1) {
7557 printf("Infinite loop consume mult finite loop\n");
7559 if (exp->exp_min > sub->exp_min * mult) {
7561 min = exp->exp_min - sub->exp_min * mult;
7567 if (exp->exp_max < sub->exp_max * mult) {
7569 printf("loops max mult mismatch => forbidden\n");
7571 xmlExpFree(ctxt, tmp);
7572 return(forbiddenExp);
7574 if (sub->exp_max * mult > exp->exp_min)
7577 min = exp->exp_min - sub->exp_max * mult;
7578 max = exp->exp_max - sub->exp_max * mult;
7581 } else if (!IS_NILLABLE(tmp)) {
7583 * TODO: loop here to try to grow if working on finite
7587 printf("Count, Count remain not nillable => forbidden\n");
7589 xmlExpFree(ctxt, tmp);
7590 return(forbiddenExp);
7591 } else if (sub->exp_max == -1) {
7592 if (exp->exp_max == -1) {
7593 if (exp->exp_min <= sub->exp_min) {
7595 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7601 printf("Infinite loops min => Count(X,Inf)\n");
7604 min = exp->exp_min - sub->exp_min;
7606 } else if (exp->exp_min > sub->exp_min) {
7608 printf("loops min mismatch 1 => forbidden ???\n");
7610 xmlExpFree(ctxt, tmp);
7611 return(forbiddenExp);
7617 if (exp->exp_max == -1) {
7619 printf("Infinite loop consume finite loop\n");
7621 if (exp->exp_min > sub->exp_min) {
7623 min = exp->exp_min - sub->exp_min;
7629 if (exp->exp_max < sub->exp_max) {
7631 printf("loops max mismatch => forbidden\n");
7633 xmlExpFree(ctxt, tmp);
7634 return(forbiddenExp);
7636 if (sub->exp_max > exp->exp_min)
7639 min = exp->exp_min - sub->exp_max;
7640 max = exp->exp_max - sub->exp_max;
7644 printf("loops match => SEQ(COUNT())\n");
7646 exp->exp_left->ref++;
7647 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7648 NULL, NULL, min, max);
7652 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7656 tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7659 if (tmp == forbiddenExp) {
7661 printf("loop mismatch => forbidden\n");
7663 return(forbiddenExp);
7665 if (exp->exp_min > 0)
7666 min = exp->exp_min - 1;
7669 if (exp->exp_max < 0)
7672 max = exp->exp_max - 1;
7675 printf("loop match => SEQ(COUNT())\n");
7677 exp->exp_left->ref++;
7678 tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7679 NULL, NULL, min, max);
7682 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7689 printf("Fallback to derivative\n");
7691 if (IS_NILLABLE(sub)) {
7692 if (!(IS_NILLABLE(exp)))
7693 return(forbiddenExp);
7699 * here the structured derivation made no progress so
7700 * we use the default token based derivation to force one more step
7702 if (ctxt->tabSize == 0)
7705 tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7706 sizeof(const xmlChar *));
7712 * collect all the strings accepted by the subexpression on input
7714 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7716 const xmlChar **temp;
7717 temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
7718 sizeof(const xmlChar *));
7720 xmlFree((xmlChar **) tab);
7725 len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7727 for (i = 0;i < len;i++) {
7728 tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7729 if ((tmp == NULL) || (tmp == forbiddenExp)) {
7730 xmlExpFree(ctxt, ret);
7731 xmlFree((xmlChar **) tab);
7734 tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7735 if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7736 xmlExpFree(ctxt, tmp);
7737 xmlExpFree(ctxt, ret);
7738 xmlFree((xmlChar **) tab);
7741 tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7742 xmlExpFree(ctxt, tmp);
7743 xmlExpFree(ctxt, tmp2);
7745 if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7746 xmlExpFree(ctxt, ret);
7747 xmlFree((xmlChar **) tab);
7754 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7756 xmlFree((xmlChar **) tab);
7761 xmlFree((xmlChar **) tab);
7767 * @ctxt: the expressions context
7768 * @exp: the englobing expression
7769 * @sub: the subexpression
7771 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7772 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7773 * it usually tatkes less than linear time and can handle expressions generating
7774 * infinite languages.
7776 * Returns the resulting expression or NULL in case of internal error, the
7777 * result must be freed
7780 xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7781 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7787 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7789 printf("Sub nillable and not exp : can't subsume\n");
7791 return(forbiddenExp);
7793 if (xmlExpCheckCard(exp, sub) == 0) {
7795 printf("sub generate longuer sequances than exp : can't subsume\n");
7797 return(forbiddenExp);
7799 return(xmlExpExpDeriveInt(ctxt, exp, sub));
7804 * @ctxt: the expressions context
7805 * @exp: the englobing expression
7806 * @sub: the subexpression
7808 * Check whether @exp accepts all the languages accexpted by @sub
7809 * the input being a subexpression.
7811 * Returns 1 if true 0 if false and -1 in case of failure.
7814 xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7817 if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7821 * TODO: speedup by checking the language of sub is a subset of the
7827 if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7829 printf("Sub nillable and not exp : can't subsume\n");
7833 if (xmlExpCheckCard(exp, sub) == 0) {
7835 printf("sub generate longuer sequances than exp : can't subsume\n");
7839 tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7841 printf("Result derivation :\n");
7846 if (tmp == forbiddenExp)
7848 if (tmp == emptyExp)
7850 if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7851 xmlExpFree(ctxt, tmp);
7854 xmlExpFree(ctxt, tmp);
7858 /************************************************************************
7860 * Parsing expression *
7862 ************************************************************************/
7864 static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7867 #define CUR (*ctxt->cur)
7869 #define NEXT ctxt->cur++;
7871 #define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7872 #define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7875 xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7883 if ((CUR < '0') || (CUR > '9'))
7885 while ((CUR >= '0') && (CUR <= '9')) {
7886 ret = ret * 10 + (CUR - '0');
7892 static xmlExpNodePtr
7893 xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7900 if (*ctxt->cur == '(') {
7902 ret = xmlExpParseExpr(ctxt);
7904 if (*ctxt->cur != ')') {
7905 fprintf(stderr, "unbalanced '(' : %s\n", base);
7906 xmlExpFree(ctxt, ret);
7911 goto parse_quantifier;
7913 while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7914 (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7915 (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7917 val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7920 ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7929 min = xmlExpParseNumber(ctxt);
7931 xmlExpFree(ctxt, ret);
7937 max = xmlExpParseNumber(ctxt);
7942 xmlExpFree(ctxt, ret);
7946 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7949 } else if (CUR == '?') {
7951 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7954 } else if (CUR == '+') {
7956 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7959 } else if (CUR == '*') {
7961 ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7969 static xmlExpNodePtr
7970 xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7971 xmlExpNodePtr ret, right;
7973 ret = xmlExpParseOr(ctxt);
7975 while (CUR == '|') {
7977 right = xmlExpParseOr(ctxt);
7978 if (right == NULL) {
7979 xmlExpFree(ctxt, ret);
7982 ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7989 static xmlExpNodePtr
7990 xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7991 xmlExpNodePtr ret, right;
7993 ret = xmlExpParseSeq(ctxt);
7995 while (CUR == ',') {
7997 right = xmlExpParseSeq(ctxt);
7998 if (right == NULL) {
7999 xmlExpFree(ctxt, ret);
8002 ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
8011 * @ctxt: the expressions context
8012 * @expr: the 0 terminated string
8014 * Minimal parser for regexps, it understand the following constructs
8015 * - string terminals
8016 * - choice operator |
8017 * - sequence operator ,
8018 * - subexpressions (...)
8019 * - usual cardinality operators + * and ?
8020 * - finite sequences { min, max }
8021 * - infinite sequences { min, * }
8022 * There is minimal checkings made especially no checking on strings values
8024 * Returns a new expression or NULL in case of failure
8027 xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
8033 ret = xmlExpParseExpr(ctxt);
8035 if (*ctxt->cur != 0) {
8036 xmlExpFree(ctxt, ret);
8043 xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
8046 if (expr == NULL) return;
8047 if (glob) xmlBufferWriteChar(buf, "(");
8048 switch (expr->type) {
8050 xmlBufferWriteChar(buf, "empty");
8052 case XML_EXP_FORBID:
8053 xmlBufferWriteChar(buf, "forbidden");
8056 xmlBufferWriteCHAR(buf, expr->exp_str);
8060 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8061 xmlExpDumpInt(buf, c, 1);
8063 xmlExpDumpInt(buf, c, 0);
8064 xmlBufferWriteChar(buf, " , ");
8065 c = expr->exp_right;
8066 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8067 xmlExpDumpInt(buf, c, 1);
8069 xmlExpDumpInt(buf, c, 0);
8073 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8074 xmlExpDumpInt(buf, c, 1);
8076 xmlExpDumpInt(buf, c, 0);
8077 xmlBufferWriteChar(buf, " | ");
8078 c = expr->exp_right;
8079 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8080 xmlExpDumpInt(buf, c, 1);
8082 xmlExpDumpInt(buf, c, 0);
8084 case XML_EXP_COUNT: {
8088 if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8089 xmlExpDumpInt(buf, c, 1);
8091 xmlExpDumpInt(buf, c, 0);
8092 if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
8095 } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
8098 } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
8101 } else if (expr->exp_max == expr->exp_min) {
8102 snprintf(rep, 39, "{%d}", expr->exp_min);
8103 } else if (expr->exp_max < 0) {
8104 snprintf(rep, 39, "{%d,inf}", expr->exp_min);
8106 snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
8109 xmlBufferWriteChar(buf, rep);
8113 fprintf(stderr, "Error in tree\n");
8116 xmlBufferWriteChar(buf, ")");
8120 * @buf: a buffer to receive the output
8121 * @expr: the compiled expression
8123 * Serialize the expression as compiled to the buffer
8126 xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
8127 if ((buf == NULL) || (expr == NULL))
8129 xmlExpDumpInt(buf, expr, 0);
8134 * @expr: a compiled expression
8136 * Indicate the maximum number of input a expression can accept
8138 * Returns the maximum length or -1 in case of error
8141 xmlExpMaxToken(xmlExpNodePtr expr) {
8144 return(expr->c_max);
8148 * xmlExpCtxtNbNodes:
8149 * @ctxt: an expression context
8151 * Debugging facility provides the number of allocated nodes at a that point
8153 * Returns the number of nodes in use or -1 in case of error
8156 xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
8159 return(ctxt->nb_nodes);
8164 * @ctxt: an expression context
8166 * Debugging facility provides the number of allocated nodes over lifetime
8168 * Returns the number of nodes ever allocated or -1 in case of error
8171 xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8174 return(ctxt->nb_cons);
8177 #endif /* LIBXML_EXPR_ENABLED */
8178 #define bottom_xmlregexp
8179 #include "elfgcchack.h"
8180 #endif /* LIBXML_REGEXP_ENABLED */