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
48 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
49 xmlRegexpErrCompile(ctxt, str);
50 #define NEXT ctxt->cur++
51 #define CUR (*(ctxt->cur))
52 #define NXT(index) (ctxt->cur[index])
54 #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
55 #define NEXTL(l) ctxt->cur += l;
56 #define XML_REG_STRING_SEPARATOR '|'
58 * Need PREV to check on a '-' within a Character Group. May only be used
59 * when it's guaranteed that cur is not at the beginning of ctxt->string!
61 #define PREV (ctxt->cur[-1])
66 * macro to flag unimplemented blocks
69 xmlGenericError(xmlGenericErrorContext, \
70 "Unimplemented block at %s:%d\n", \
73 /************************************************************************
75 * Datatypes and structures *
77 ************************************************************************/
80 * Note: the order of the enums below is significant, do not shuffle
83 XML_REGEXP_EPSILON
= 1,
86 XML_REGEXP_SUBREG
, /* used for () sub regexps */
88 XML_REGEXP_ANYCHAR
, /* . */
89 XML_REGEXP_ANYSPACE
, /* \s */
90 XML_REGEXP_NOTSPACE
, /* \S */
91 XML_REGEXP_INITNAME
, /* \l */
92 XML_REGEXP_NOTINITNAME
, /* \L */
93 XML_REGEXP_NAMECHAR
, /* \c */
94 XML_REGEXP_NOTNAMECHAR
, /* \C */
95 XML_REGEXP_DECIMAL
, /* \d */
96 XML_REGEXP_NOTDECIMAL
, /* \D */
97 XML_REGEXP_REALCHAR
, /* \w */
98 XML_REGEXP_NOTREALCHAR
, /* \W */
99 XML_REGEXP_LETTER
= 100,
100 XML_REGEXP_LETTER_UPPERCASE
,
101 XML_REGEXP_LETTER_LOWERCASE
,
102 XML_REGEXP_LETTER_TITLECASE
,
103 XML_REGEXP_LETTER_MODIFIER
,
104 XML_REGEXP_LETTER_OTHERS
,
106 XML_REGEXP_MARK_NONSPACING
,
107 XML_REGEXP_MARK_SPACECOMBINING
,
108 XML_REGEXP_MARK_ENCLOSING
,
110 XML_REGEXP_NUMBER_DECIMAL
,
111 XML_REGEXP_NUMBER_LETTER
,
112 XML_REGEXP_NUMBER_OTHERS
,
114 XML_REGEXP_PUNCT_CONNECTOR
,
115 XML_REGEXP_PUNCT_DASH
,
116 XML_REGEXP_PUNCT_OPEN
,
117 XML_REGEXP_PUNCT_CLOSE
,
118 XML_REGEXP_PUNCT_INITQUOTE
,
119 XML_REGEXP_PUNCT_FINQUOTE
,
120 XML_REGEXP_PUNCT_OTHERS
,
122 XML_REGEXP_SEPAR_SPACE
,
123 XML_REGEXP_SEPAR_LINE
,
124 XML_REGEXP_SEPAR_PARA
,
126 XML_REGEXP_SYMBOL_MATH
,
127 XML_REGEXP_SYMBOL_CURRENCY
,
128 XML_REGEXP_SYMBOL_MODIFIER
,
129 XML_REGEXP_SYMBOL_OTHERS
,
131 XML_REGEXP_OTHER_CONTROL
,
132 XML_REGEXP_OTHER_FORMAT
,
133 XML_REGEXP_OTHER_PRIVATE
,
135 XML_REGEXP_BLOCK_NAME
139 XML_REGEXP_QUANT_EPSILON
= 1,
140 XML_REGEXP_QUANT_ONCE
,
141 XML_REGEXP_QUANT_OPT
,
142 XML_REGEXP_QUANT_MULT
,
143 XML_REGEXP_QUANT_PLUS
,
144 XML_REGEXP_QUANT_ONCEONLY
,
145 XML_REGEXP_QUANT_ALL
,
146 XML_REGEXP_QUANT_RANGE
150 XML_REGEXP_START_STATE
= 1,
151 XML_REGEXP_FINAL_STATE
,
152 XML_REGEXP_TRANS_STATE
,
153 XML_REGEXP_SINK_STATE
,
154 XML_REGEXP_UNREACH_STATE
158 XML_REGEXP_MARK_NORMAL
= 0,
159 XML_REGEXP_MARK_START
,
160 XML_REGEXP_MARK_VISITED
163 typedef struct _xmlRegRange xmlRegRange
;
164 typedef xmlRegRange
*xmlRegRangePtr
;
166 struct _xmlRegRange
{
167 int neg
; /* 0 normal, 1 not, 2 exclude */
174 typedef struct _xmlRegAtom xmlRegAtom
;
175 typedef xmlRegAtom
*xmlRegAtomPtr
;
177 typedef struct _xmlAutomataState xmlRegState
;
178 typedef xmlRegState
*xmlRegStatePtr
;
183 xmlRegQuantType quant
;
191 xmlRegStatePtr start
;
192 xmlRegStatePtr start0
;
196 xmlRegRangePtr
*ranges
;
200 typedef struct _xmlRegCounter xmlRegCounter
;
201 typedef xmlRegCounter
*xmlRegCounterPtr
;
203 struct _xmlRegCounter
{
208 typedef struct _xmlRegTrans xmlRegTrans
;
209 typedef xmlRegTrans
*xmlRegTransPtr
;
211 struct _xmlRegTrans
{
219 struct _xmlAutomataState
{
220 xmlRegStateType type
;
221 xmlRegMarkedType mark
;
222 xmlRegMarkedType reached
;
227 /* knowing states ponting to us can speed things up */
233 typedef struct _xmlAutomata xmlRegParserCtxt
;
234 typedef xmlRegParserCtxt
*xmlRegParserCtxtPtr
;
236 #define AM_AUTOMATA_RNG 1
238 struct _xmlAutomata
{
245 xmlRegStatePtr start
;
247 xmlRegStatePtr state
;
253 xmlRegAtomPtr
*atoms
;
257 xmlRegStatePtr
*states
;
261 xmlRegCounter
*counters
;
271 xmlRegStatePtr
*states
;
273 xmlRegAtomPtr
*atoms
;
275 xmlRegCounter
*counters
;
279 * That's the compact form for determinists automatas
288 typedef struct _xmlRegExecRollback xmlRegExecRollback
;
289 typedef xmlRegExecRollback
*xmlRegExecRollbackPtr
;
291 struct _xmlRegExecRollback
{
292 xmlRegStatePtr state
;/* the current state */
293 int index
; /* the index in the input stack */
294 int nextbranch
; /* the next transition to explore in that state */
295 int *counts
; /* save the automata state if it has some */
298 typedef struct _xmlRegInputToken xmlRegInputToken
;
299 typedef xmlRegInputToken
*xmlRegInputTokenPtr
;
301 struct _xmlRegInputToken
{
306 struct _xmlRegExecCtxt
{
307 int status
; /* execution status != 0 indicate an error */
308 int determinist
; /* did we find an indeterministic behaviour */
309 xmlRegexpPtr comp
; /* the compiled regexp */
310 xmlRegExecCallbacks callback
;
313 xmlRegStatePtr state
;/* the current state */
314 int transno
; /* the current transition on that state */
315 int transcount
; /* the number of chars in char counted transitions */
318 * A stack of rollback states
322 xmlRegExecRollback
*rollbacks
;
325 * The state of the automata if any
336 const xmlChar
*inputString
; /* when operating on characters */
337 xmlRegInputTokenPtr inputStack
;/* when operating on strings */
342 int errStateNo
; /* the error state number */
343 xmlRegStatePtr errState
; /* the error state */
344 xmlChar
*errString
; /* the string raising the error */
345 int *errCounts
; /* counters at the error state */
349 #define REGEXP_ALL_COUNTER 0x123456
350 #define REGEXP_ALL_LAX_COUNTER 0x123457
352 static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt
, int top
);
353 static void xmlRegFreeState(xmlRegStatePtr state
);
354 static void xmlRegFreeAtom(xmlRegAtomPtr atom
);
355 static int xmlRegStrEqualWildcard(const xmlChar
*expStr
, const xmlChar
*valStr
);
356 static int xmlRegCheckCharacter(xmlRegAtomPtr atom
, int codepoint
);
357 static int xmlRegCheckCharacterRange(xmlRegAtomType type
, int codepoint
,
358 int neg
, int start
, int end
, const xmlChar
*blockName
);
360 void xmlAutomataSetFlags(xmlAutomataPtr am
, int flags
);
362 /************************************************************************
364 * Regexp memory error handler *
366 ************************************************************************/
368 * xmlRegexpErrMemory:
369 * @extra: extra information
371 * Handle an out of memory condition
374 xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt
, const char *extra
)
376 const char *regexp
= NULL
;
378 regexp
= (const char *) ctxt
->string
;
379 ctxt
->error
= XML_ERR_NO_MEMORY
;
381 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_REGEXP
,
382 XML_ERR_NO_MEMORY
, XML_ERR_FATAL
, NULL
, 0, extra
,
384 "Memory allocation failed : %s\n", extra
);
388 * xmlRegexpErrCompile:
389 * @extra: extra information
391 * Handle a compilation failure
394 xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt
, const char *extra
)
396 const char *regexp
= NULL
;
400 regexp
= (const char *) ctxt
->string
;
401 idx
= ctxt
->cur
- ctxt
->string
;
402 ctxt
->error
= XML_REGEXP_COMPILE_ERROR
;
404 __xmlRaiseError(NULL
, NULL
, NULL
, NULL
, NULL
, XML_FROM_REGEXP
,
405 XML_REGEXP_COMPILE_ERROR
, XML_ERR_FATAL
, NULL
, 0, extra
,
406 regexp
, NULL
, idx
, 0,
407 "failed to compile: %s\n", extra
);
410 /************************************************************************
412 * Allocation/Deallocation *
414 ************************************************************************/
416 static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt
);
418 * xmlRegEpxFromParse:
419 * @ctxt: the parser context used to build it
421 * Allocate a new regexp and fill it with the result from the parser
423 * Returns the new regexp or NULL in case of error
426 xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt
) {
429 ret
= (xmlRegexpPtr
) xmlMalloc(sizeof(xmlRegexp
));
431 xmlRegexpErrMemory(ctxt
, "compiling regexp");
434 memset(ret
, 0, sizeof(xmlRegexp
));
435 ret
->string
= ctxt
->string
;
436 ret
->nbStates
= ctxt
->nbStates
;
437 ret
->states
= ctxt
->states
;
438 ret
->nbAtoms
= ctxt
->nbAtoms
;
439 ret
->atoms
= ctxt
->atoms
;
440 ret
->nbCounters
= ctxt
->nbCounters
;
441 ret
->counters
= ctxt
->counters
;
442 ret
->determinist
= ctxt
->determinist
;
443 ret
->flags
= ctxt
->flags
;
444 if (ret
->determinist
== -1) {
445 xmlRegexpIsDeterminist(ret
);
448 if ((ret
->determinist
!= 0) &&
449 (ret
->nbCounters
== 0) &&
451 (ret
->atoms
!= NULL
) &&
452 (ret
->atoms
[0] != NULL
) &&
453 (ret
->atoms
[0]->type
== XML_REGEXP_STRING
)) {
454 int i
, j
, nbstates
= 0, nbatoms
= 0;
463 * Switch to a compact representation
464 * 1/ counting the effective number of states left
465 * 2/ counting the unique number of atoms, and check that
466 * they are all of the string type
467 * 3/ build a table state x atom for the transitions
470 stateRemap
= xmlMalloc(ret
->nbStates
* sizeof(int));
471 if (stateRemap
== NULL
) {
472 xmlRegexpErrMemory(ctxt
, "compiling regexp");
476 for (i
= 0;i
< ret
->nbStates
;i
++) {
477 if (ret
->states
[i
] != NULL
) {
478 stateRemap
[i
] = nbstates
;
484 #ifdef DEBUG_COMPACTION
485 printf("Final: %d states\n", nbstates
);
487 stringMap
= xmlMalloc(ret
->nbAtoms
* sizeof(char *));
488 if (stringMap
== NULL
) {
489 xmlRegexpErrMemory(ctxt
, "compiling regexp");
494 stringRemap
= xmlMalloc(ret
->nbAtoms
* sizeof(int));
495 if (stringRemap
== NULL
) {
496 xmlRegexpErrMemory(ctxt
, "compiling regexp");
502 for (i
= 0;i
< ret
->nbAtoms
;i
++) {
503 if ((ret
->atoms
[i
]->type
== XML_REGEXP_STRING
) &&
504 (ret
->atoms
[i
]->quant
== XML_REGEXP_QUANT_ONCE
)) {
505 value
= ret
->atoms
[i
]->valuep
;
506 for (j
= 0;j
< nbatoms
;j
++) {
507 if (xmlStrEqual(stringMap
[j
], value
)) {
513 stringRemap
[i
] = nbatoms
;
514 stringMap
[nbatoms
] = xmlStrdup(value
);
515 if (stringMap
[nbatoms
] == NULL
) {
516 for (i
= 0;i
< nbatoms
;i
++)
517 xmlFree(stringMap
[i
]);
518 xmlFree(stringRemap
);
528 xmlFree(stringRemap
);
529 for (i
= 0;i
< nbatoms
;i
++)
530 xmlFree(stringMap
[i
]);
536 #ifdef DEBUG_COMPACTION
537 printf("Final: %d atoms\n", nbatoms
);
539 transitions
= (int *) xmlMalloc((nbstates
+ 1) *
540 (nbatoms
+ 1) * sizeof(int));
541 if (transitions
== NULL
) {
543 xmlFree(stringRemap
);
548 memset(transitions
, 0, (nbstates
+ 1) * (nbatoms
+ 1) * sizeof(int));
551 * Allocate the transition table. The first entry for each
552 * state corresponds to the state type.
556 for (i
= 0;i
< ret
->nbStates
;i
++) {
557 int stateno
, atomno
, targetno
, prev
;
558 xmlRegStatePtr state
;
559 xmlRegTransPtr trans
;
561 stateno
= stateRemap
[i
];
564 state
= ret
->states
[i
];
566 transitions
[stateno
* (nbatoms
+ 1)] = state
->type
;
568 for (j
= 0;j
< state
->nbTrans
;j
++) {
569 trans
= &(state
->trans
[j
]);
570 if ((trans
->to
== -1) || (trans
->atom
== NULL
))
572 atomno
= stringRemap
[trans
->atom
->no
];
573 if ((trans
->atom
->data
!= NULL
) && (transdata
== NULL
)) {
574 transdata
= (void **) xmlMalloc(nbstates
* nbatoms
*
576 if (transdata
!= NULL
)
578 nbstates
* nbatoms
* sizeof(void *));
580 xmlRegexpErrMemory(ctxt
, "compiling regexp");
584 targetno
= stateRemap
[trans
->to
];
586 * if the same atom can generate transitions to 2 different
587 * states then it means the automata is not determinist and
588 * the compact form can't be used !
590 prev
= transitions
[stateno
* (nbatoms
+ 1) + atomno
+ 1];
592 if (prev
!= targetno
+ 1) {
593 ret
->determinist
= 0;
594 #ifdef DEBUG_COMPACTION
595 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
596 i
, j
, trans
->atom
->no
, trans
->to
, atomno
, targetno
);
597 printf(" previous to is %d\n", prev
);
599 if (transdata
!= NULL
)
601 xmlFree(transitions
);
603 xmlFree(stringRemap
);
604 for (i
= 0;i
< nbatoms
;i
++)
605 xmlFree(stringMap
[i
]);
611 printf("State %d trans %d: atom %d to %d : %d to %d\n",
612 i
, j
, trans
->atom
->no
, trans
->to
, atomno
, targetno
);
614 transitions
[stateno
* (nbatoms
+ 1) + atomno
+ 1] =
615 targetno
+ 1; /* to avoid 0 */
616 if (transdata
!= NULL
)
617 transdata
[stateno
* nbatoms
+ atomno
] =
622 ret
->determinist
= 1;
623 #ifdef DEBUG_COMPACTION
627 for (i
= 0;i
< nbstates
;i
++) {
628 for (j
= 0;j
< nbatoms
+ 1;j
++) {
629 printf("%02d ", transitions
[i
* (nbatoms
+ 1) + j
]);
636 * Cleanup of the old data
638 if (ret
->states
!= NULL
) {
639 for (i
= 0;i
< ret
->nbStates
;i
++)
640 xmlRegFreeState(ret
->states
[i
]);
641 xmlFree(ret
->states
);
645 if (ret
->atoms
!= NULL
) {
646 for (i
= 0;i
< ret
->nbAtoms
;i
++)
647 xmlRegFreeAtom(ret
->atoms
[i
]);
653 ret
->compact
= transitions
;
654 ret
->transdata
= transdata
;
655 ret
->stringMap
= stringMap
;
656 ret
->nbstrings
= nbatoms
;
657 ret
->nbstates
= nbstates
;
659 xmlFree(stringRemap
);
667 ctxt
->nbCounters
= 0;
668 ctxt
->counters
= NULL
;
673 * xmlRegNewParserCtxt:
674 * @string: the string to parse
676 * Allocate a new regexp parser context
678 * Returns the new context or NULL in case of error
680 static xmlRegParserCtxtPtr
681 xmlRegNewParserCtxt(const xmlChar
*string
) {
682 xmlRegParserCtxtPtr ret
;
684 ret
= (xmlRegParserCtxtPtr
) xmlMalloc(sizeof(xmlRegParserCtxt
));
687 memset(ret
, 0, sizeof(xmlRegParserCtxt
));
689 ret
->string
= xmlStrdup(string
);
690 ret
->cur
= ret
->string
;
694 ret
->determinist
= -1;
700 * @ctxt: the regexp parser context
701 * @neg: is that negative
702 * @type: the type of range
703 * @start: the start codepoint
704 * @end: the end codepoint
706 * Allocate a new regexp range
708 * Returns the new range or NULL in case of error
710 static xmlRegRangePtr
711 xmlRegNewRange(xmlRegParserCtxtPtr ctxt
,
712 int neg
, xmlRegAtomType type
, int start
, int end
) {
715 ret
= (xmlRegRangePtr
) xmlMalloc(sizeof(xmlRegRange
));
717 xmlRegexpErrMemory(ctxt
, "allocating range");
729 * @range: the regexp range
731 * Free a regexp range
734 xmlRegFreeRange(xmlRegRangePtr range
) {
738 if (range
->blockName
!= NULL
)
739 xmlFree(range
->blockName
);
745 * @range: the regexp range
747 * Copy a regexp range
749 * Returns the new copy or NULL in case of error.
751 static xmlRegRangePtr
752 xmlRegCopyRange(xmlRegParserCtxtPtr ctxt
, xmlRegRangePtr range
) {
758 ret
= xmlRegNewRange(ctxt
, range
->neg
, range
->type
, range
->start
,
762 if (range
->blockName
!= NULL
) {
763 ret
->blockName
= xmlStrdup(range
->blockName
);
764 if (ret
->blockName
== NULL
) {
765 xmlRegexpErrMemory(ctxt
, "allocating range");
766 xmlRegFreeRange(ret
);
775 * @ctxt: the regexp parser context
776 * @type: the type of atom
778 * Allocate a new atom
780 * Returns the new atom or NULL in case of error
783 xmlRegNewAtom(xmlRegParserCtxtPtr ctxt
, xmlRegAtomType type
) {
786 ret
= (xmlRegAtomPtr
) xmlMalloc(sizeof(xmlRegAtom
));
788 xmlRegexpErrMemory(ctxt
, "allocating atom");
791 memset(ret
, 0, sizeof(xmlRegAtom
));
793 ret
->quant
= XML_REGEXP_QUANT_ONCE
;
801 * @atom: the regexp atom
806 xmlRegFreeAtom(xmlRegAtomPtr atom
) {
812 for (i
= 0;i
< atom
->nbRanges
;i
++)
813 xmlRegFreeRange(atom
->ranges
[i
]);
814 if (atom
->ranges
!= NULL
)
815 xmlFree(atom
->ranges
);
816 if ((atom
->type
== XML_REGEXP_STRING
) && (atom
->valuep
!= NULL
))
817 xmlFree(atom
->valuep
);
818 if ((atom
->type
== XML_REGEXP_STRING
) && (atom
->valuep2
!= NULL
))
819 xmlFree(atom
->valuep2
);
820 if ((atom
->type
== XML_REGEXP_BLOCK_NAME
) && (atom
->valuep
!= NULL
))
821 xmlFree(atom
->valuep
);
827 * @ctxt: the regexp parser context
828 * @atom: the oiginal atom
830 * Allocate a new regexp range
832 * Returns the new atom or NULL in case of error
835 xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt
, xmlRegAtomPtr atom
) {
838 ret
= (xmlRegAtomPtr
) xmlMalloc(sizeof(xmlRegAtom
));
840 xmlRegexpErrMemory(ctxt
, "copying atom");
843 memset(ret
, 0, sizeof(xmlRegAtom
));
844 ret
->type
= atom
->type
;
845 ret
->quant
= atom
->quant
;
846 ret
->min
= atom
->min
;
847 ret
->max
= atom
->max
;
848 if (atom
->nbRanges
> 0) {
851 ret
->ranges
= (xmlRegRangePtr
*) xmlMalloc(sizeof(xmlRegRangePtr
) *
853 if (ret
->ranges
== NULL
) {
854 xmlRegexpErrMemory(ctxt
, "copying atom");
857 for (i
= 0;i
< atom
->nbRanges
;i
++) {
858 ret
->ranges
[i
] = xmlRegCopyRange(ctxt
, atom
->ranges
[i
]);
859 if (ret
->ranges
[i
] == NULL
)
861 ret
->nbRanges
= i
+ 1;
871 static xmlRegStatePtr
872 xmlRegNewState(xmlRegParserCtxtPtr ctxt
) {
875 ret
= (xmlRegStatePtr
) xmlMalloc(sizeof(xmlRegState
));
877 xmlRegexpErrMemory(ctxt
, "allocating state");
880 memset(ret
, 0, sizeof(xmlRegState
));
881 ret
->type
= XML_REGEXP_TRANS_STATE
;
882 ret
->mark
= XML_REGEXP_MARK_NORMAL
;
888 * @state: the regexp state
890 * Free a regexp state
893 xmlRegFreeState(xmlRegStatePtr state
) {
897 if (state
->trans
!= NULL
)
898 xmlFree(state
->trans
);
899 if (state
->transTo
!= NULL
)
900 xmlFree(state
->transTo
);
905 * xmlRegFreeParserCtxt:
906 * @ctxt: the regexp parser context
908 * Free a regexp parser context
911 xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt
) {
916 if (ctxt
->string
!= NULL
)
917 xmlFree(ctxt
->string
);
918 if (ctxt
->states
!= NULL
) {
919 for (i
= 0;i
< ctxt
->nbStates
;i
++)
920 xmlRegFreeState(ctxt
->states
[i
]);
921 xmlFree(ctxt
->states
);
923 if (ctxt
->atoms
!= NULL
) {
924 for (i
= 0;i
< ctxt
->nbAtoms
;i
++)
925 xmlRegFreeAtom(ctxt
->atoms
[i
]);
926 xmlFree(ctxt
->atoms
);
928 if (ctxt
->counters
!= NULL
)
929 xmlFree(ctxt
->counters
);
933 /************************************************************************
935 * Display of Data structures *
937 ************************************************************************/
940 xmlRegPrintAtomType(FILE *output
, xmlRegAtomType type
) {
942 case XML_REGEXP_EPSILON
:
943 fprintf(output
, "epsilon "); break;
944 case XML_REGEXP_CHARVAL
:
945 fprintf(output
, "charval "); break;
946 case XML_REGEXP_RANGES
:
947 fprintf(output
, "ranges "); break;
948 case XML_REGEXP_SUBREG
:
949 fprintf(output
, "subexpr "); break;
950 case XML_REGEXP_STRING
:
951 fprintf(output
, "string "); break;
952 case XML_REGEXP_ANYCHAR
:
953 fprintf(output
, "anychar "); break;
954 case XML_REGEXP_ANYSPACE
:
955 fprintf(output
, "anyspace "); break;
956 case XML_REGEXP_NOTSPACE
:
957 fprintf(output
, "notspace "); break;
958 case XML_REGEXP_INITNAME
:
959 fprintf(output
, "initname "); break;
960 case XML_REGEXP_NOTINITNAME
:
961 fprintf(output
, "notinitname "); break;
962 case XML_REGEXP_NAMECHAR
:
963 fprintf(output
, "namechar "); break;
964 case XML_REGEXP_NOTNAMECHAR
:
965 fprintf(output
, "notnamechar "); break;
966 case XML_REGEXP_DECIMAL
:
967 fprintf(output
, "decimal "); break;
968 case XML_REGEXP_NOTDECIMAL
:
969 fprintf(output
, "notdecimal "); break;
970 case XML_REGEXP_REALCHAR
:
971 fprintf(output
, "realchar "); break;
972 case XML_REGEXP_NOTREALCHAR
:
973 fprintf(output
, "notrealchar "); break;
974 case XML_REGEXP_LETTER
:
975 fprintf(output
, "LETTER "); break;
976 case XML_REGEXP_LETTER_UPPERCASE
:
977 fprintf(output
, "LETTER_UPPERCASE "); break;
978 case XML_REGEXP_LETTER_LOWERCASE
:
979 fprintf(output
, "LETTER_LOWERCASE "); break;
980 case XML_REGEXP_LETTER_TITLECASE
:
981 fprintf(output
, "LETTER_TITLECASE "); break;
982 case XML_REGEXP_LETTER_MODIFIER
:
983 fprintf(output
, "LETTER_MODIFIER "); break;
984 case XML_REGEXP_LETTER_OTHERS
:
985 fprintf(output
, "LETTER_OTHERS "); break;
986 case XML_REGEXP_MARK
:
987 fprintf(output
, "MARK "); break;
988 case XML_REGEXP_MARK_NONSPACING
:
989 fprintf(output
, "MARK_NONSPACING "); break;
990 case XML_REGEXP_MARK_SPACECOMBINING
:
991 fprintf(output
, "MARK_SPACECOMBINING "); break;
992 case XML_REGEXP_MARK_ENCLOSING
:
993 fprintf(output
, "MARK_ENCLOSING "); break;
994 case XML_REGEXP_NUMBER
:
995 fprintf(output
, "NUMBER "); break;
996 case XML_REGEXP_NUMBER_DECIMAL
:
997 fprintf(output
, "NUMBER_DECIMAL "); break;
998 case XML_REGEXP_NUMBER_LETTER
:
999 fprintf(output
, "NUMBER_LETTER "); break;
1000 case XML_REGEXP_NUMBER_OTHERS
:
1001 fprintf(output
, "NUMBER_OTHERS "); break;
1002 case XML_REGEXP_PUNCT
:
1003 fprintf(output
, "PUNCT "); break;
1004 case XML_REGEXP_PUNCT_CONNECTOR
:
1005 fprintf(output
, "PUNCT_CONNECTOR "); break;
1006 case XML_REGEXP_PUNCT_DASH
:
1007 fprintf(output
, "PUNCT_DASH "); break;
1008 case XML_REGEXP_PUNCT_OPEN
:
1009 fprintf(output
, "PUNCT_OPEN "); break;
1010 case XML_REGEXP_PUNCT_CLOSE
:
1011 fprintf(output
, "PUNCT_CLOSE "); break;
1012 case XML_REGEXP_PUNCT_INITQUOTE
:
1013 fprintf(output
, "PUNCT_INITQUOTE "); break;
1014 case XML_REGEXP_PUNCT_FINQUOTE
:
1015 fprintf(output
, "PUNCT_FINQUOTE "); break;
1016 case XML_REGEXP_PUNCT_OTHERS
:
1017 fprintf(output
, "PUNCT_OTHERS "); break;
1018 case XML_REGEXP_SEPAR
:
1019 fprintf(output
, "SEPAR "); break;
1020 case XML_REGEXP_SEPAR_SPACE
:
1021 fprintf(output
, "SEPAR_SPACE "); break;
1022 case XML_REGEXP_SEPAR_LINE
:
1023 fprintf(output
, "SEPAR_LINE "); break;
1024 case XML_REGEXP_SEPAR_PARA
:
1025 fprintf(output
, "SEPAR_PARA "); break;
1026 case XML_REGEXP_SYMBOL
:
1027 fprintf(output
, "SYMBOL "); break;
1028 case XML_REGEXP_SYMBOL_MATH
:
1029 fprintf(output
, "SYMBOL_MATH "); break;
1030 case XML_REGEXP_SYMBOL_CURRENCY
:
1031 fprintf(output
, "SYMBOL_CURRENCY "); break;
1032 case XML_REGEXP_SYMBOL_MODIFIER
:
1033 fprintf(output
, "SYMBOL_MODIFIER "); break;
1034 case XML_REGEXP_SYMBOL_OTHERS
:
1035 fprintf(output
, "SYMBOL_OTHERS "); break;
1036 case XML_REGEXP_OTHER
:
1037 fprintf(output
, "OTHER "); break;
1038 case XML_REGEXP_OTHER_CONTROL
:
1039 fprintf(output
, "OTHER_CONTROL "); break;
1040 case XML_REGEXP_OTHER_FORMAT
:
1041 fprintf(output
, "OTHER_FORMAT "); break;
1042 case XML_REGEXP_OTHER_PRIVATE
:
1043 fprintf(output
, "OTHER_PRIVATE "); break;
1044 case XML_REGEXP_OTHER_NA
:
1045 fprintf(output
, "OTHER_NA "); break;
1046 case XML_REGEXP_BLOCK_NAME
:
1047 fprintf(output
, "BLOCK "); break;
1052 xmlRegPrintQuantType(FILE *output
, xmlRegQuantType type
) {
1054 case XML_REGEXP_QUANT_EPSILON
:
1055 fprintf(output
, "epsilon "); break;
1056 case XML_REGEXP_QUANT_ONCE
:
1057 fprintf(output
, "once "); break;
1058 case XML_REGEXP_QUANT_OPT
:
1059 fprintf(output
, "? "); break;
1060 case XML_REGEXP_QUANT_MULT
:
1061 fprintf(output
, "* "); break;
1062 case XML_REGEXP_QUANT_PLUS
:
1063 fprintf(output
, "+ "); break;
1064 case XML_REGEXP_QUANT_RANGE
:
1065 fprintf(output
, "range "); break;
1066 case XML_REGEXP_QUANT_ONCEONLY
:
1067 fprintf(output
, "onceonly "); break;
1068 case XML_REGEXP_QUANT_ALL
:
1069 fprintf(output
, "all "); break;
1073 xmlRegPrintRange(FILE *output
, xmlRegRangePtr range
) {
1074 fprintf(output
, " range: ");
1076 fprintf(output
, "negative ");
1077 xmlRegPrintAtomType(output
, range
->type
);
1078 fprintf(output
, "%c - %c\n", range
->start
, range
->end
);
1082 xmlRegPrintAtom(FILE *output
, xmlRegAtomPtr atom
) {
1083 fprintf(output
, " atom: ");
1085 fprintf(output
, "NULL\n");
1089 fprintf(output
, "not ");
1090 xmlRegPrintAtomType(output
, atom
->type
);
1091 xmlRegPrintQuantType(output
, atom
->quant
);
1092 if (atom
->quant
== XML_REGEXP_QUANT_RANGE
)
1093 fprintf(output
, "%d-%d ", atom
->min
, atom
->max
);
1094 if (atom
->type
== XML_REGEXP_STRING
)
1095 fprintf(output
, "'%s' ", (char *) atom
->valuep
);
1096 if (atom
->type
== XML_REGEXP_CHARVAL
)
1097 fprintf(output
, "char %c\n", atom
->codepoint
);
1098 else if (atom
->type
== XML_REGEXP_RANGES
) {
1100 fprintf(output
, "%d entries\n", atom
->nbRanges
);
1101 for (i
= 0; i
< atom
->nbRanges
;i
++)
1102 xmlRegPrintRange(output
, atom
->ranges
[i
]);
1103 } else if (atom
->type
== XML_REGEXP_SUBREG
) {
1104 fprintf(output
, "start %d end %d\n", atom
->start
->no
, atom
->stop
->no
);
1106 fprintf(output
, "\n");
1111 xmlRegPrintTrans(FILE *output
, xmlRegTransPtr trans
) {
1112 fprintf(output
, " trans: ");
1113 if (trans
== NULL
) {
1114 fprintf(output
, "NULL\n");
1117 if (trans
->to
< 0) {
1118 fprintf(output
, "removed\n");
1121 if (trans
->nd
!= 0) {
1123 fprintf(output
, "last not determinist, ");
1125 fprintf(output
, "not determinist, ");
1127 if (trans
->counter
>= 0) {
1128 fprintf(output
, "counted %d, ", trans
->counter
);
1130 if (trans
->count
== REGEXP_ALL_COUNTER
) {
1131 fprintf(output
, "all transition, ");
1132 } else if (trans
->count
>= 0) {
1133 fprintf(output
, "count based %d, ", trans
->count
);
1135 if (trans
->atom
== NULL
) {
1136 fprintf(output
, "epsilon to %d\n", trans
->to
);
1139 if (trans
->atom
->type
== XML_REGEXP_CHARVAL
)
1140 fprintf(output
, "char %c ", trans
->atom
->codepoint
);
1141 fprintf(output
, "atom %d, to %d\n", trans
->atom
->no
, trans
->to
);
1145 xmlRegPrintState(FILE *output
, xmlRegStatePtr state
) {
1148 fprintf(output
, " state: ");
1149 if (state
== NULL
) {
1150 fprintf(output
, "NULL\n");
1153 if (state
->type
== XML_REGEXP_START_STATE
)
1154 fprintf(output
, "START ");
1155 if (state
->type
== XML_REGEXP_FINAL_STATE
)
1156 fprintf(output
, "FINAL ");
1158 fprintf(output
, "%d, %d transitions:\n", state
->no
, state
->nbTrans
);
1159 for (i
= 0;i
< state
->nbTrans
; i
++) {
1160 xmlRegPrintTrans(output
, &(state
->trans
[i
]));
1164 #ifdef DEBUG_REGEXP_GRAPH
1166 xmlRegPrintCtxt(FILE *output
, xmlRegParserCtxtPtr ctxt
) {
1169 fprintf(output
, " ctxt: ");
1171 fprintf(output
, "NULL\n");
1174 fprintf(output
, "'%s' ", ctxt
->string
);
1176 fprintf(output
, "error ");
1178 fprintf(output
, "neg ");
1179 fprintf(output
, "\n");
1180 fprintf(output
, "%d atoms:\n", ctxt
->nbAtoms
);
1181 for (i
= 0;i
< ctxt
->nbAtoms
; i
++) {
1182 fprintf(output
, " %02d ", i
);
1183 xmlRegPrintAtom(output
, ctxt
->atoms
[i
]);
1185 if (ctxt
->atom
!= NULL
) {
1186 fprintf(output
, "current atom:\n");
1187 xmlRegPrintAtom(output
, ctxt
->atom
);
1189 fprintf(output
, "%d states:", ctxt
->nbStates
);
1190 if (ctxt
->start
!= NULL
)
1191 fprintf(output
, " start: %d", ctxt
->start
->no
);
1192 if (ctxt
->end
!= NULL
)
1193 fprintf(output
, " end: %d", ctxt
->end
->no
);
1194 fprintf(output
, "\n");
1195 for (i
= 0;i
< ctxt
->nbStates
; i
++) {
1196 xmlRegPrintState(output
, ctxt
->states
[i
]);
1198 fprintf(output
, "%d counters:\n", ctxt
->nbCounters
);
1199 for (i
= 0;i
< ctxt
->nbCounters
; i
++) {
1200 fprintf(output
, " %d: min %d max %d\n", i
, ctxt
->counters
[i
].min
,
1201 ctxt
->counters
[i
].max
);
1206 /************************************************************************
1208 * Finite Automata structures manipulations *
1210 ************************************************************************/
1213 xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt
, xmlRegAtomPtr atom
,
1214 int neg
, xmlRegAtomType type
, int start
, int end
,
1215 xmlChar
*blockName
) {
1216 xmlRegRangePtr range
;
1219 ERROR("add range: atom is NULL");
1222 if (atom
->type
!= XML_REGEXP_RANGES
) {
1223 ERROR("add range: atom is not ranges");
1226 if (atom
->maxRanges
== 0) {
1227 atom
->maxRanges
= 4;
1228 atom
->ranges
= (xmlRegRangePtr
*) xmlMalloc(atom
->maxRanges
*
1229 sizeof(xmlRegRangePtr
));
1230 if (atom
->ranges
== NULL
) {
1231 xmlRegexpErrMemory(ctxt
, "adding ranges");
1232 atom
->maxRanges
= 0;
1235 } else if (atom
->nbRanges
>= atom
->maxRanges
) {
1236 xmlRegRangePtr
*tmp
;
1237 atom
->maxRanges
*= 2;
1238 tmp
= (xmlRegRangePtr
*) xmlRealloc(atom
->ranges
, atom
->maxRanges
*
1239 sizeof(xmlRegRangePtr
));
1241 xmlRegexpErrMemory(ctxt
, "adding ranges");
1242 atom
->maxRanges
/= 2;
1247 range
= xmlRegNewRange(ctxt
, neg
, type
, start
, end
);
1250 range
->blockName
= blockName
;
1251 atom
->ranges
[atom
->nbRanges
++] = range
;
1256 xmlRegGetCounter(xmlRegParserCtxtPtr ctxt
) {
1257 if (ctxt
->maxCounters
== 0) {
1258 ctxt
->maxCounters
= 4;
1259 ctxt
->counters
= (xmlRegCounter
*) xmlMalloc(ctxt
->maxCounters
*
1260 sizeof(xmlRegCounter
));
1261 if (ctxt
->counters
== NULL
) {
1262 xmlRegexpErrMemory(ctxt
, "allocating counter");
1263 ctxt
->maxCounters
= 0;
1266 } else if (ctxt
->nbCounters
>= ctxt
->maxCounters
) {
1268 ctxt
->maxCounters
*= 2;
1269 tmp
= (xmlRegCounter
*) xmlRealloc(ctxt
->counters
, ctxt
->maxCounters
*
1270 sizeof(xmlRegCounter
));
1272 xmlRegexpErrMemory(ctxt
, "allocating counter");
1273 ctxt
->maxCounters
/= 2;
1276 ctxt
->counters
= tmp
;
1278 ctxt
->counters
[ctxt
->nbCounters
].min
= -1;
1279 ctxt
->counters
[ctxt
->nbCounters
].max
= -1;
1280 return(ctxt
->nbCounters
++);
1284 xmlRegAtomPush(xmlRegParserCtxtPtr ctxt
, xmlRegAtomPtr atom
) {
1286 ERROR("atom push: atom is NULL");
1289 if (ctxt
->maxAtoms
== 0) {
1291 ctxt
->atoms
= (xmlRegAtomPtr
*) xmlMalloc(ctxt
->maxAtoms
*
1292 sizeof(xmlRegAtomPtr
));
1293 if (ctxt
->atoms
== NULL
) {
1294 xmlRegexpErrMemory(ctxt
, "pushing atom");
1298 } else if (ctxt
->nbAtoms
>= ctxt
->maxAtoms
) {
1300 ctxt
->maxAtoms
*= 2;
1301 tmp
= (xmlRegAtomPtr
*) xmlRealloc(ctxt
->atoms
, ctxt
->maxAtoms
*
1302 sizeof(xmlRegAtomPtr
));
1304 xmlRegexpErrMemory(ctxt
, "allocating counter");
1305 ctxt
->maxAtoms
/= 2;
1310 atom
->no
= ctxt
->nbAtoms
;
1311 ctxt
->atoms
[ctxt
->nbAtoms
++] = atom
;
1316 xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt
, xmlRegStatePtr target
,
1318 if (target
->maxTransTo
== 0) {
1319 target
->maxTransTo
= 8;
1320 target
->transTo
= (int *) xmlMalloc(target
->maxTransTo
*
1322 if (target
->transTo
== NULL
) {
1323 xmlRegexpErrMemory(ctxt
, "adding transition");
1324 target
->maxTransTo
= 0;
1327 } else if (target
->nbTransTo
>= target
->maxTransTo
) {
1329 target
->maxTransTo
*= 2;
1330 tmp
= (int *) xmlRealloc(target
->transTo
, target
->maxTransTo
*
1333 xmlRegexpErrMemory(ctxt
, "adding transition");
1334 target
->maxTransTo
/= 2;
1337 target
->transTo
= tmp
;
1339 target
->transTo
[target
->nbTransTo
] = from
;
1340 target
->nbTransTo
++;
1344 xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt
, xmlRegStatePtr state
,
1345 xmlRegAtomPtr atom
, xmlRegStatePtr target
,
1346 int counter
, int count
) {
1350 if (state
== NULL
) {
1351 ERROR("add state: state is NULL");
1354 if (target
== NULL
) {
1355 ERROR("add state: target is NULL");
1359 * Other routines follow the philosophy 'When in doubt, add a transition'
1360 * so we check here whether such a transition is already present and, if
1361 * so, silently ignore this request.
1364 for (nrtrans
= state
->nbTrans
- 1; nrtrans
>= 0; nrtrans
--) {
1365 xmlRegTransPtr trans
= &(state
->trans
[nrtrans
]);
1366 if ((trans
->atom
== atom
) &&
1367 (trans
->to
== target
->no
) &&
1368 (trans
->counter
== counter
) &&
1369 (trans
->count
== count
)) {
1370 #ifdef DEBUG_REGEXP_GRAPH
1371 printf("Ignoring duplicate transition from %d to %d\n",
1372 state
->no
, target
->no
);
1378 if (state
->maxTrans
== 0) {
1379 state
->maxTrans
= 8;
1380 state
->trans
= (xmlRegTrans
*) xmlMalloc(state
->maxTrans
*
1381 sizeof(xmlRegTrans
));
1382 if (state
->trans
== NULL
) {
1383 xmlRegexpErrMemory(ctxt
, "adding transition");
1384 state
->maxTrans
= 0;
1387 } else if (state
->nbTrans
>= state
->maxTrans
) {
1389 state
->maxTrans
*= 2;
1390 tmp
= (xmlRegTrans
*) xmlRealloc(state
->trans
, state
->maxTrans
*
1391 sizeof(xmlRegTrans
));
1393 xmlRegexpErrMemory(ctxt
, "adding transition");
1394 state
->maxTrans
/= 2;
1399 #ifdef DEBUG_REGEXP_GRAPH
1400 printf("Add trans from %d to %d ", state
->no
, target
->no
);
1401 if (count
== REGEXP_ALL_COUNTER
)
1402 printf("all transition\n");
1403 else if (count
>= 0)
1404 printf("count based %d\n", count
);
1405 else if (counter
>= 0)
1406 printf("counted %d\n", counter
);
1407 else if (atom
== NULL
)
1408 printf("epsilon transition\n");
1409 else if (atom
!= NULL
)
1410 xmlRegPrintAtom(stdout
, atom
);
1413 state
->trans
[state
->nbTrans
].atom
= atom
;
1414 state
->trans
[state
->nbTrans
].to
= target
->no
;
1415 state
->trans
[state
->nbTrans
].counter
= counter
;
1416 state
->trans
[state
->nbTrans
].count
= count
;
1417 state
->trans
[state
->nbTrans
].nd
= 0;
1419 xmlRegStateAddTransTo(ctxt
, target
, state
->no
);
1423 xmlRegStatePush(xmlRegParserCtxtPtr ctxt
, xmlRegStatePtr state
) {
1424 if (state
== NULL
) return(-1);
1425 if (ctxt
->maxStates
== 0) {
1426 ctxt
->maxStates
= 4;
1427 ctxt
->states
= (xmlRegStatePtr
*) xmlMalloc(ctxt
->maxStates
*
1428 sizeof(xmlRegStatePtr
));
1429 if (ctxt
->states
== NULL
) {
1430 xmlRegexpErrMemory(ctxt
, "adding state");
1431 ctxt
->maxStates
= 0;
1434 } else if (ctxt
->nbStates
>= ctxt
->maxStates
) {
1435 xmlRegStatePtr
*tmp
;
1436 ctxt
->maxStates
*= 2;
1437 tmp
= (xmlRegStatePtr
*) xmlRealloc(ctxt
->states
, ctxt
->maxStates
*
1438 sizeof(xmlRegStatePtr
));
1440 xmlRegexpErrMemory(ctxt
, "adding state");
1441 ctxt
->maxStates
/= 2;
1446 state
->no
= ctxt
->nbStates
;
1447 ctxt
->states
[ctxt
->nbStates
++] = state
;
1452 * xmlFAGenerateAllTransition:
1453 * @ctxt: a regexp parser context
1454 * @from: the from state
1455 * @to: the target state or NULL for building a new one
1460 xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt
,
1461 xmlRegStatePtr from
, xmlRegStatePtr to
,
1464 to
= xmlRegNewState(ctxt
);
1465 xmlRegStatePush(ctxt
, to
);
1469 xmlRegStateAddTrans(ctxt
, from
, NULL
, to
, -1, REGEXP_ALL_LAX_COUNTER
);
1471 xmlRegStateAddTrans(ctxt
, from
, NULL
, to
, -1, REGEXP_ALL_COUNTER
);
1475 * xmlFAGenerateEpsilonTransition:
1476 * @ctxt: a regexp parser context
1477 * @from: the from state
1478 * @to: the target state or NULL for building a new one
1482 xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt
,
1483 xmlRegStatePtr from
, xmlRegStatePtr to
) {
1485 to
= xmlRegNewState(ctxt
);
1486 xmlRegStatePush(ctxt
, to
);
1489 xmlRegStateAddTrans(ctxt
, from
, NULL
, to
, -1, -1);
1493 * xmlFAGenerateCountedEpsilonTransition:
1494 * @ctxt: a regexp parser context
1495 * @from: the from state
1496 * @to: the target state or NULL for building a new one
1497 * counter: the counter for that transition
1501 xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt
,
1502 xmlRegStatePtr from
, xmlRegStatePtr to
, int counter
) {
1504 to
= xmlRegNewState(ctxt
);
1505 xmlRegStatePush(ctxt
, to
);
1508 xmlRegStateAddTrans(ctxt
, from
, NULL
, to
, counter
, -1);
1512 * xmlFAGenerateCountedTransition:
1513 * @ctxt: a regexp parser context
1514 * @from: the from state
1515 * @to: the target state or NULL for building a new one
1516 * counter: the counter for that transition
1520 xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt
,
1521 xmlRegStatePtr from
, xmlRegStatePtr to
, int counter
) {
1523 to
= xmlRegNewState(ctxt
);
1524 xmlRegStatePush(ctxt
, to
);
1527 xmlRegStateAddTrans(ctxt
, from
, NULL
, to
, -1, counter
);
1531 * xmlFAGenerateTransitions:
1532 * @ctxt: a regexp parser context
1533 * @from: the from state
1534 * @to: the target state or NULL for building a new one
1535 * @atom: the atom generating the transition
1537 * Returns 0 if success and -1 in case of error.
1540 xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt
, xmlRegStatePtr from
,
1541 xmlRegStatePtr to
, xmlRegAtomPtr atom
) {
1545 ERROR("genrate transition: atom == NULL");
1548 if (atom
->type
== XML_REGEXP_SUBREG
) {
1550 * this is a subexpression handling one should not need to
1551 * create a new node except for XML_REGEXP_QUANT_RANGE.
1553 if (xmlRegAtomPush(ctxt
, atom
) < 0) {
1556 if ((to
!= NULL
) && (atom
->stop
!= to
) &&
1557 (atom
->quant
!= XML_REGEXP_QUANT_RANGE
)) {
1559 * Generate an epsilon transition to link to the target
1561 xmlFAGenerateEpsilonTransition(ctxt
, atom
->stop
, to
);
1563 } else if ((to
== NULL
) && (atom
->quant
!= XML_REGEXP_QUANT_RANGE
) &&
1564 (atom
->quant
!= XML_REGEXP_QUANT_ONCE
)) {
1565 to
= xmlRegNewState(ctxt
);
1566 xmlRegStatePush(ctxt
, to
);
1568 xmlFAGenerateEpsilonTransition(ctxt
, atom
->stop
, to
);
1571 switch (atom
->quant
) {
1572 case XML_REGEXP_QUANT_OPT
:
1573 atom
->quant
= XML_REGEXP_QUANT_ONCE
;
1575 * transition done to the state after end of atom.
1576 * 1. set transition from atom start to new state
1577 * 2. set transition from atom end to this state.
1580 xmlFAGenerateEpsilonTransition(ctxt
, atom
->start
, 0);
1581 xmlFAGenerateEpsilonTransition(ctxt
, atom
->stop
,
1584 xmlFAGenerateEpsilonTransition(ctxt
, atom
->start
, to
);
1587 case XML_REGEXP_QUANT_MULT
:
1588 atom
->quant
= XML_REGEXP_QUANT_ONCE
;
1589 xmlFAGenerateEpsilonTransition(ctxt
, atom
->start
, atom
->stop
);
1590 xmlFAGenerateEpsilonTransition(ctxt
, atom
->stop
, atom
->start
);
1592 case XML_REGEXP_QUANT_PLUS
:
1593 atom
->quant
= XML_REGEXP_QUANT_ONCE
;
1594 xmlFAGenerateEpsilonTransition(ctxt
, atom
->stop
, atom
->start
);
1596 case XML_REGEXP_QUANT_RANGE
: {
1598 xmlRegStatePtr inter
, newstate
;
1601 * create the final state now if needed
1606 newstate
= xmlRegNewState(ctxt
);
1607 xmlRegStatePush(ctxt
, newstate
);
1611 * The principle here is to use counted transition
1612 * to avoid explosion in the number of states in the
1613 * graph. This is clearly more complex but should not
1614 * be exploitable at runtime.
1616 if ((atom
->min
== 0) && (atom
->start0
== NULL
)) {
1619 * duplicate a transition based on atom to count next
1620 * occurences after 1. We cannot loop to atom->start
1621 * directly because we need an epsilon transition to
1624 /* ???? For some reason it seems we never reach that
1625 case, I suppose this got optimized out before when
1626 building the automata */
1627 copy
= xmlRegCopyAtom(ctxt
, atom
);
1630 copy
->quant
= XML_REGEXP_QUANT_ONCE
;
1634 if (xmlFAGenerateTransitions(ctxt
, atom
->start
, NULL
, copy
)
1637 inter
= ctxt
->state
;
1638 counter
= xmlRegGetCounter(ctxt
);
1639 ctxt
->counters
[counter
].min
= atom
->min
- 1;
1640 ctxt
->counters
[counter
].max
= atom
->max
- 1;
1641 /* count the number of times we see it again */
1642 xmlFAGenerateCountedEpsilonTransition(ctxt
, inter
,
1643 atom
->stop
, counter
);
1644 /* allow a way out based on the count */
1645 xmlFAGenerateCountedTransition(ctxt
, inter
,
1647 /* and also allow a direct exit for 0 */
1648 xmlFAGenerateEpsilonTransition(ctxt
, atom
->start
,
1652 * either we need the atom at least once or there
1653 * is an atom->start0 allowing to easilly plug the
1654 * epsilon transition.
1656 counter
= xmlRegGetCounter(ctxt
);
1657 ctxt
->counters
[counter
].min
= atom
->min
- 1;
1658 ctxt
->counters
[counter
].max
= atom
->max
- 1;
1659 /* count the number of times we see it again */
1660 xmlFAGenerateCountedEpsilonTransition(ctxt
, atom
->stop
,
1661 atom
->start
, counter
);
1662 /* allow a way out based on the count */
1663 xmlFAGenerateCountedTransition(ctxt
, atom
->stop
,
1665 /* and if needed allow a direct exit for 0 */
1667 xmlFAGenerateEpsilonTransition(ctxt
, atom
->start0
,
1673 atom
->quant
= XML_REGEXP_QUANT_ONCE
;
1674 ctxt
->state
= newstate
;
1681 if ((atom
->min
== 0) && (atom
->max
== 0) &&
1682 (atom
->quant
== XML_REGEXP_QUANT_RANGE
)) {
1684 * we can discard the atom and generate an epsilon transition instead
1687 to
= xmlRegNewState(ctxt
);
1689 xmlRegStatePush(ctxt
, to
);
1694 xmlFAGenerateEpsilonTransition(ctxt
, from
, to
);
1696 xmlRegFreeAtom(atom
);
1700 to
= xmlRegNewState(ctxt
);
1702 xmlRegStatePush(ctxt
, to
);
1708 if ((atom
->quant
== XML_REGEXP_QUANT_MULT
) ||
1709 (atom
->quant
== XML_REGEXP_QUANT_PLUS
)) {
1711 * Do not pollute the target state by adding transitions from
1712 * it as it is likely to be the shared target of multiple branches.
1713 * So isolate with an epsilon transition.
1717 tmp
= xmlRegNewState(ctxt
);
1719 xmlRegStatePush(ctxt
, tmp
);
1723 xmlFAGenerateEpsilonTransition(ctxt
, tmp
, to
);
1726 if (xmlRegAtomPush(ctxt
, atom
) < 0) {
1729 xmlRegStateAddTrans(ctxt
, from
, atom
, to
, -1, -1);
1731 switch (atom
->quant
) {
1732 case XML_REGEXP_QUANT_OPT
:
1733 atom
->quant
= XML_REGEXP_QUANT_ONCE
;
1734 xmlFAGenerateEpsilonTransition(ctxt
, from
, to
);
1736 case XML_REGEXP_QUANT_MULT
:
1737 atom
->quant
= XML_REGEXP_QUANT_ONCE
;
1738 xmlFAGenerateEpsilonTransition(ctxt
, from
, to
);
1739 xmlRegStateAddTrans(ctxt
, to
, atom
, to
, -1, -1);
1741 case XML_REGEXP_QUANT_PLUS
:
1742 atom
->quant
= XML_REGEXP_QUANT_ONCE
;
1743 xmlRegStateAddTrans(ctxt
, to
, atom
, to
, -1, -1);
1745 case XML_REGEXP_QUANT_RANGE
:
1747 if (atom
->min
== 0) {
1748 xmlFAGenerateEpsilonTransition(ctxt
, from
, to
);
1759 * xmlFAReduceEpsilonTransitions:
1760 * @ctxt: a regexp parser context
1761 * @fromnr: the from state
1762 * @tonr: the to state
1763 * @counter: should that transition be associated to a counted
1767 xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt
, int fromnr
,
1768 int tonr
, int counter
) {
1770 xmlRegStatePtr from
;
1773 #ifdef DEBUG_REGEXP_GRAPH
1774 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr
, tonr
);
1776 from
= ctxt
->states
[fromnr
];
1779 to
= ctxt
->states
[tonr
];
1782 if ((to
->mark
== XML_REGEXP_MARK_START
) ||
1783 (to
->mark
== XML_REGEXP_MARK_VISITED
))
1786 to
->mark
= XML_REGEXP_MARK_VISITED
;
1787 if (to
->type
== XML_REGEXP_FINAL_STATE
) {
1788 #ifdef DEBUG_REGEXP_GRAPH
1789 printf("State %d is final, so %d becomes final\n", tonr
, fromnr
);
1791 from
->type
= XML_REGEXP_FINAL_STATE
;
1793 for (transnr
= 0;transnr
< to
->nbTrans
;transnr
++) {
1794 if (to
->trans
[transnr
].to
< 0)
1796 if (to
->trans
[transnr
].atom
== NULL
) {
1798 * Don't remove counted transitions
1801 if (to
->trans
[transnr
].to
!= fromnr
) {
1802 if (to
->trans
[transnr
].count
>= 0) {
1803 int newto
= to
->trans
[transnr
].to
;
1805 xmlRegStateAddTrans(ctxt
, from
, NULL
,
1806 ctxt
->states
[newto
],
1807 -1, to
->trans
[transnr
].count
);
1809 #ifdef DEBUG_REGEXP_GRAPH
1810 printf("Found epsilon trans %d from %d to %d\n",
1811 transnr
, tonr
, to
->trans
[transnr
].to
);
1813 if (to
->trans
[transnr
].counter
>= 0) {
1814 xmlFAReduceEpsilonTransitions(ctxt
, fromnr
,
1815 to
->trans
[transnr
].to
,
1816 to
->trans
[transnr
].counter
);
1818 xmlFAReduceEpsilonTransitions(ctxt
, fromnr
,
1819 to
->trans
[transnr
].to
,
1825 int newto
= to
->trans
[transnr
].to
;
1827 if (to
->trans
[transnr
].counter
>= 0) {
1828 xmlRegStateAddTrans(ctxt
, from
, to
->trans
[transnr
].atom
,
1829 ctxt
->states
[newto
],
1830 to
->trans
[transnr
].counter
, -1);
1832 xmlRegStateAddTrans(ctxt
, from
, to
->trans
[transnr
].atom
,
1833 ctxt
->states
[newto
], counter
, -1);
1837 to
->mark
= XML_REGEXP_MARK_NORMAL
;
1841 * xmlFAEliminateSimpleEpsilonTransitions:
1842 * @ctxt: a regexp parser context
1844 * Eliminating general epsilon transitions can get costly in the general
1845 * algorithm due to the large amount of generated new transitions and
1846 * associated comparisons. However for simple epsilon transition used just
1847 * to separate building blocks when generating the automata this can be
1848 * reduced to state elimination:
1849 * - if there exists an epsilon from X to Y
1850 * - if there is no other transition from X
1851 * then X and Y are semantically equivalent and X can be eliminated
1852 * If X is the start state then make Y the start state, else replace the
1853 * target of all transitions to X by transitions to Y.
1856 xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt
) {
1857 int statenr
, i
, j
, newto
;
1858 xmlRegStatePtr state
, tmp
;
1860 for (statenr
= 0;statenr
< ctxt
->nbStates
;statenr
++) {
1861 state
= ctxt
->states
[statenr
];
1864 if (state
->nbTrans
!= 1)
1866 if (state
->type
== XML_REGEXP_UNREACH_STATE
)
1868 /* is the only transition out a basic transition */
1869 if ((state
->trans
[0].atom
== NULL
) &&
1870 (state
->trans
[0].to
>= 0) &&
1871 (state
->trans
[0].to
!= statenr
) &&
1872 (state
->trans
[0].counter
< 0) &&
1873 (state
->trans
[0].count
< 0)) {
1874 newto
= state
->trans
[0].to
;
1876 if (state
->type
== XML_REGEXP_START_STATE
) {
1877 #ifdef DEBUG_REGEXP_GRAPH
1878 printf("Found simple epsilon trans from start %d to %d\n",
1882 #ifdef DEBUG_REGEXP_GRAPH
1883 printf("Found simple epsilon trans from %d to %d\n",
1886 for (i
= 0;i
< state
->nbTransTo
;i
++) {
1887 tmp
= ctxt
->states
[state
->transTo
[i
]];
1888 for (j
= 0;j
< tmp
->nbTrans
;j
++) {
1889 if (tmp
->trans
[j
].to
== statenr
) {
1890 #ifdef DEBUG_REGEXP_GRAPH
1891 printf("Changed transition %d on %d to go to %d\n",
1894 tmp
->trans
[j
].to
= -1;
1895 xmlRegStateAddTrans(ctxt
, tmp
, tmp
->trans
[j
].atom
,
1896 ctxt
->states
[newto
],
1897 tmp
->trans
[j
].counter
,
1898 tmp
->trans
[j
].count
);
1902 if (state
->type
== XML_REGEXP_FINAL_STATE
)
1903 ctxt
->states
[newto
]->type
= XML_REGEXP_FINAL_STATE
;
1904 /* eliminate the transition completely */
1907 state
->type
= XML_REGEXP_UNREACH_STATE
;
1915 * xmlFAEliminateEpsilonTransitions:
1916 * @ctxt: a regexp parser context
1920 xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt
) {
1921 int statenr
, transnr
;
1922 xmlRegStatePtr state
;
1925 if (ctxt
->states
== NULL
) return;
1928 * Eliminate simple epsilon transition and the associated unreachable
1931 xmlFAEliminateSimpleEpsilonTransitions(ctxt
);
1932 for (statenr
= 0;statenr
< ctxt
->nbStates
;statenr
++) {
1933 state
= ctxt
->states
[statenr
];
1934 if ((state
!= NULL
) && (state
->type
== XML_REGEXP_UNREACH_STATE
)) {
1935 #ifdef DEBUG_REGEXP_GRAPH
1936 printf("Removed unreachable state %d\n", statenr
);
1938 xmlRegFreeState(state
);
1939 ctxt
->states
[statenr
] = NULL
;
1946 * Build the completed transitions bypassing the epsilons
1947 * Use a marking algorithm to avoid loops
1948 * Mark sink states too.
1949 * Process from the latests states backward to the start when
1950 * there is long cascading epsilon chains this minimize the
1951 * recursions and transition compares when adding the new ones
1953 for (statenr
= ctxt
->nbStates
- 1;statenr
>= 0;statenr
--) {
1954 state
= ctxt
->states
[statenr
];
1957 if ((state
->nbTrans
== 0) &&
1958 (state
->type
!= XML_REGEXP_FINAL_STATE
)) {
1959 state
->type
= XML_REGEXP_SINK_STATE
;
1961 for (transnr
= 0;transnr
< state
->nbTrans
;transnr
++) {
1962 if ((state
->trans
[transnr
].atom
== NULL
) &&
1963 (state
->trans
[transnr
].to
>= 0)) {
1964 if (state
->trans
[transnr
].to
== statenr
) {
1965 state
->trans
[transnr
].to
= -1;
1966 #ifdef DEBUG_REGEXP_GRAPH
1967 printf("Removed loopback epsilon trans %d on %d\n",
1970 } else if (state
->trans
[transnr
].count
< 0) {
1971 int newto
= state
->trans
[transnr
].to
;
1973 #ifdef DEBUG_REGEXP_GRAPH
1974 printf("Found epsilon trans %d from %d to %d\n",
1975 transnr
, statenr
, newto
);
1978 state
->trans
[transnr
].to
= -2;
1979 state
->mark
= XML_REGEXP_MARK_START
;
1980 xmlFAReduceEpsilonTransitions(ctxt
, statenr
,
1981 newto
, state
->trans
[transnr
].counter
);
1982 state
->mark
= XML_REGEXP_MARK_NORMAL
;
1983 #ifdef DEBUG_REGEXP_GRAPH
1985 printf("Found counted transition %d on %d\n",
1993 * Eliminate the epsilon transitions
1996 for (statenr
= 0;statenr
< ctxt
->nbStates
;statenr
++) {
1997 state
= ctxt
->states
[statenr
];
2000 for (transnr
= 0;transnr
< state
->nbTrans
;transnr
++) {
2001 xmlRegTransPtr trans
= &(state
->trans
[transnr
]);
2002 if ((trans
->atom
== NULL
) &&
2003 (trans
->count
< 0) &&
2012 * Use this pass to detect unreachable states too
2014 for (statenr
= 0;statenr
< ctxt
->nbStates
;statenr
++) {
2015 state
= ctxt
->states
[statenr
];
2017 state
->reached
= XML_REGEXP_MARK_NORMAL
;
2019 state
= ctxt
->states
[0];
2021 state
->reached
= XML_REGEXP_MARK_START
;
2022 while (state
!= NULL
) {
2023 xmlRegStatePtr target
= NULL
;
2024 state
->reached
= XML_REGEXP_MARK_VISITED
;
2026 * Mark all states reachable from the current reachable state
2028 for (transnr
= 0;transnr
< state
->nbTrans
;transnr
++) {
2029 if ((state
->trans
[transnr
].to
>= 0) &&
2030 ((state
->trans
[transnr
].atom
!= NULL
) ||
2031 (state
->trans
[transnr
].count
>= 0))) {
2032 int newto
= state
->trans
[transnr
].to
;
2034 if (ctxt
->states
[newto
] == NULL
)
2036 if (ctxt
->states
[newto
]->reached
== XML_REGEXP_MARK_NORMAL
) {
2037 ctxt
->states
[newto
]->reached
= XML_REGEXP_MARK_START
;
2038 target
= ctxt
->states
[newto
];
2044 * find the next accessible state not explored
2046 if (target
== NULL
) {
2047 for (statenr
= 1;statenr
< ctxt
->nbStates
;statenr
++) {
2048 state
= ctxt
->states
[statenr
];
2049 if ((state
!= NULL
) && (state
->reached
==
2050 XML_REGEXP_MARK_START
)) {
2058 for (statenr
= 0;statenr
< ctxt
->nbStates
;statenr
++) {
2059 state
= ctxt
->states
[statenr
];
2060 if ((state
!= NULL
) && (state
->reached
== XML_REGEXP_MARK_NORMAL
)) {
2061 #ifdef DEBUG_REGEXP_GRAPH
2062 printf("Removed unreachable state %d\n", statenr
);
2064 xmlRegFreeState(state
);
2065 ctxt
->states
[statenr
] = NULL
;
2072 xmlFACompareRanges(xmlRegRangePtr range1
, xmlRegRangePtr range2
) {
2075 if ((range1
->type
== XML_REGEXP_RANGES
) ||
2076 (range2
->type
== XML_REGEXP_RANGES
) ||
2077 (range2
->type
== XML_REGEXP_SUBREG
) ||
2078 (range1
->type
== XML_REGEXP_SUBREG
) ||
2079 (range1
->type
== XML_REGEXP_STRING
) ||
2080 (range2
->type
== XML_REGEXP_STRING
))
2083 /* put them in order */
2084 if (range1
->type
> range2
->type
) {
2091 if ((range1
->type
== XML_REGEXP_ANYCHAR
) ||
2092 (range2
->type
== XML_REGEXP_ANYCHAR
)) {
2094 } else if ((range1
->type
== XML_REGEXP_EPSILON
) ||
2095 (range2
->type
== XML_REGEXP_EPSILON
)) {
2097 } else if (range1
->type
== range2
->type
) {
2098 if (range1
->type
!= XML_REGEXP_CHARVAL
)
2100 else if ((range1
->end
< range2
->start
) ||
2101 (range2
->end
< range1
->start
))
2105 } else if (range1
->type
== XML_REGEXP_CHARVAL
) {
2110 * just check all codepoints in the range for acceptance,
2111 * this is usually way cheaper since done only once at
2112 * compilation than testing over and over at runtime or
2113 * pushing too many states when evaluating.
2115 if (((range1
->neg
== 0) && (range2
->neg
!= 0)) ||
2116 ((range1
->neg
!= 0) && (range2
->neg
== 0)))
2119 for (codepoint
= range1
->start
;codepoint
<= range1
->end
;codepoint
++) {
2120 ret
= xmlRegCheckCharacterRange(range2
->type
, codepoint
,
2121 0, range2
->start
, range2
->end
,
2125 if (((neg
== 1) && (ret
== 0)) ||
2126 ((neg
== 0) && (ret
== 1)))
2130 } else if ((range1
->type
== XML_REGEXP_BLOCK_NAME
) ||
2131 (range2
->type
== XML_REGEXP_BLOCK_NAME
)) {
2132 if (range1
->type
== range2
->type
) {
2133 ret
= xmlStrEqual(range1
->blockName
, range2
->blockName
);
2136 * comparing a block range with anything else is way
2137 * too costly, and maintining the table is like too much
2138 * memory too, so let's force the automata to save state
2143 } else if ((range1
->type
< XML_REGEXP_LETTER
) ||
2144 (range2
->type
< XML_REGEXP_LETTER
)) {
2145 if ((range1
->type
== XML_REGEXP_ANYSPACE
) &&
2146 (range2
->type
== XML_REGEXP_NOTSPACE
))
2148 else if ((range1
->type
== XML_REGEXP_INITNAME
) &&
2149 (range2
->type
== XML_REGEXP_NOTINITNAME
))
2151 else if ((range1
->type
== XML_REGEXP_NAMECHAR
) &&
2152 (range2
->type
== XML_REGEXP_NOTNAMECHAR
))
2154 else if ((range1
->type
== XML_REGEXP_DECIMAL
) &&
2155 (range2
->type
== XML_REGEXP_NOTDECIMAL
))
2157 else if ((range1
->type
== XML_REGEXP_REALCHAR
) &&
2158 (range2
->type
== XML_REGEXP_NOTREALCHAR
))
2161 /* same thing to limit complexity */
2166 /* range1->type < range2->type here */
2167 switch (range1
->type
) {
2168 case XML_REGEXP_LETTER
:
2169 /* all disjoint except in the subgroups */
2170 if ((range2
->type
== XML_REGEXP_LETTER_UPPERCASE
) ||
2171 (range2
->type
== XML_REGEXP_LETTER_LOWERCASE
) ||
2172 (range2
->type
== XML_REGEXP_LETTER_TITLECASE
) ||
2173 (range2
->type
== XML_REGEXP_LETTER_MODIFIER
) ||
2174 (range2
->type
== XML_REGEXP_LETTER_OTHERS
))
2177 case XML_REGEXP_MARK
:
2178 if ((range2
->type
== XML_REGEXP_MARK_NONSPACING
) ||
2179 (range2
->type
== XML_REGEXP_MARK_SPACECOMBINING
) ||
2180 (range2
->type
== XML_REGEXP_MARK_ENCLOSING
))
2183 case XML_REGEXP_NUMBER
:
2184 if ((range2
->type
== XML_REGEXP_NUMBER_DECIMAL
) ||
2185 (range2
->type
== XML_REGEXP_NUMBER_LETTER
) ||
2186 (range2
->type
== XML_REGEXP_NUMBER_OTHERS
))
2189 case XML_REGEXP_PUNCT
:
2190 if ((range2
->type
== XML_REGEXP_PUNCT_CONNECTOR
) ||
2191 (range2
->type
== XML_REGEXP_PUNCT_DASH
) ||
2192 (range2
->type
== XML_REGEXP_PUNCT_OPEN
) ||
2193 (range2
->type
== XML_REGEXP_PUNCT_CLOSE
) ||
2194 (range2
->type
== XML_REGEXP_PUNCT_INITQUOTE
) ||
2195 (range2
->type
== XML_REGEXP_PUNCT_FINQUOTE
) ||
2196 (range2
->type
== XML_REGEXP_PUNCT_OTHERS
))
2199 case XML_REGEXP_SEPAR
:
2200 if ((range2
->type
== XML_REGEXP_SEPAR_SPACE
) ||
2201 (range2
->type
== XML_REGEXP_SEPAR_LINE
) ||
2202 (range2
->type
== XML_REGEXP_SEPAR_PARA
))
2205 case XML_REGEXP_SYMBOL
:
2206 if ((range2
->type
== XML_REGEXP_SYMBOL_MATH
) ||
2207 (range2
->type
== XML_REGEXP_SYMBOL_CURRENCY
) ||
2208 (range2
->type
== XML_REGEXP_SYMBOL_MODIFIER
) ||
2209 (range2
->type
== XML_REGEXP_SYMBOL_OTHERS
))
2212 case XML_REGEXP_OTHER
:
2213 if ((range2
->type
== XML_REGEXP_OTHER_CONTROL
) ||
2214 (range2
->type
== XML_REGEXP_OTHER_FORMAT
) ||
2215 (range2
->type
== XML_REGEXP_OTHER_PRIVATE
))
2219 if ((range2
->type
>= XML_REGEXP_LETTER
) &&
2220 (range2
->type
< XML_REGEXP_BLOCK_NAME
))
2228 if (((range1
->neg
== 0) && (range2
->neg
!= 0)) ||
2229 ((range1
->neg
!= 0) && (range2
->neg
== 0)))
2235 * xmlFACompareAtomTypes:
2236 * @type1: an atom type
2237 * @type2: an atom type
2239 * Compares two atoms type to check whether they intersect in some ways,
2240 * this is used by xmlFACompareAtoms only
2242 * Returns 1 if they may intersect and 0 otherwise
2245 xmlFACompareAtomTypes(xmlRegAtomType type1
, xmlRegAtomType type2
) {
2246 if ((type1
== XML_REGEXP_EPSILON
) ||
2247 (type1
== XML_REGEXP_CHARVAL
) ||
2248 (type1
== XML_REGEXP_RANGES
) ||
2249 (type1
== XML_REGEXP_SUBREG
) ||
2250 (type1
== XML_REGEXP_STRING
) ||
2251 (type1
== XML_REGEXP_ANYCHAR
))
2253 if ((type2
== XML_REGEXP_EPSILON
) ||
2254 (type2
== XML_REGEXP_CHARVAL
) ||
2255 (type2
== XML_REGEXP_RANGES
) ||
2256 (type2
== XML_REGEXP_SUBREG
) ||
2257 (type2
== XML_REGEXP_STRING
) ||
2258 (type2
== XML_REGEXP_ANYCHAR
))
2261 if (type1
== type2
) return(1);
2263 /* simplify subsequent compares by making sure type1 < type2 */
2264 if (type1
> type2
) {
2265 xmlRegAtomType tmp
= type1
;
2270 case XML_REGEXP_ANYSPACE
: /* \s */
2271 /* can't be a letter, number, mark, pontuation, symbol */
2272 if ((type2
== XML_REGEXP_NOTSPACE
) ||
2273 ((type2
>= XML_REGEXP_LETTER
) &&
2274 (type2
<= XML_REGEXP_LETTER_OTHERS
)) ||
2275 ((type2
>= XML_REGEXP_NUMBER
) &&
2276 (type2
<= XML_REGEXP_NUMBER_OTHERS
)) ||
2277 ((type2
>= XML_REGEXP_MARK
) &&
2278 (type2
<= XML_REGEXP_MARK_ENCLOSING
)) ||
2279 ((type2
>= XML_REGEXP_PUNCT
) &&
2280 (type2
<= XML_REGEXP_PUNCT_OTHERS
)) ||
2281 ((type2
>= XML_REGEXP_SYMBOL
) &&
2282 (type2
<= XML_REGEXP_SYMBOL_OTHERS
))
2285 case XML_REGEXP_NOTSPACE
: /* \S */
2287 case XML_REGEXP_INITNAME
: /* \l */
2288 /* can't be a number, mark, separator, pontuation, symbol or other */
2289 if ((type2
== XML_REGEXP_NOTINITNAME
) ||
2290 ((type2
>= XML_REGEXP_NUMBER
) &&
2291 (type2
<= XML_REGEXP_NUMBER_OTHERS
)) ||
2292 ((type2
>= XML_REGEXP_MARK
) &&
2293 (type2
<= XML_REGEXP_MARK_ENCLOSING
)) ||
2294 ((type2
>= XML_REGEXP_SEPAR
) &&
2295 (type2
<= XML_REGEXP_SEPAR_PARA
)) ||
2296 ((type2
>= XML_REGEXP_PUNCT
) &&
2297 (type2
<= XML_REGEXP_PUNCT_OTHERS
)) ||
2298 ((type2
>= XML_REGEXP_SYMBOL
) &&
2299 (type2
<= XML_REGEXP_SYMBOL_OTHERS
)) ||
2300 ((type2
>= XML_REGEXP_OTHER
) &&
2301 (type2
<= XML_REGEXP_OTHER_NA
))
2304 case XML_REGEXP_NOTINITNAME
: /* \L */
2306 case XML_REGEXP_NAMECHAR
: /* \c */
2307 /* can't be a mark, separator, pontuation, symbol or other */
2308 if ((type2
== XML_REGEXP_NOTNAMECHAR
) ||
2309 ((type2
>= XML_REGEXP_MARK
) &&
2310 (type2
<= XML_REGEXP_MARK_ENCLOSING
)) ||
2311 ((type2
>= XML_REGEXP_PUNCT
) &&
2312 (type2
<= XML_REGEXP_PUNCT_OTHERS
)) ||
2313 ((type2
>= XML_REGEXP_SEPAR
) &&
2314 (type2
<= XML_REGEXP_SEPAR_PARA
)) ||
2315 ((type2
>= XML_REGEXP_SYMBOL
) &&
2316 (type2
<= XML_REGEXP_SYMBOL_OTHERS
)) ||
2317 ((type2
>= XML_REGEXP_OTHER
) &&
2318 (type2
<= XML_REGEXP_OTHER_NA
))
2321 case XML_REGEXP_NOTNAMECHAR
: /* \C */
2323 case XML_REGEXP_DECIMAL
: /* \d */
2324 /* can't be a letter, mark, separator, pontuation, symbol or other */
2325 if ((type2
== XML_REGEXP_NOTDECIMAL
) ||
2326 (type2
== XML_REGEXP_REALCHAR
) ||
2327 ((type2
>= XML_REGEXP_LETTER
) &&
2328 (type2
<= XML_REGEXP_LETTER_OTHERS
)) ||
2329 ((type2
>= XML_REGEXP_MARK
) &&
2330 (type2
<= XML_REGEXP_MARK_ENCLOSING
)) ||
2331 ((type2
>= XML_REGEXP_PUNCT
) &&
2332 (type2
<= XML_REGEXP_PUNCT_OTHERS
)) ||
2333 ((type2
>= XML_REGEXP_SEPAR
) &&
2334 (type2
<= XML_REGEXP_SEPAR_PARA
)) ||
2335 ((type2
>= XML_REGEXP_SYMBOL
) &&
2336 (type2
<= XML_REGEXP_SYMBOL_OTHERS
)) ||
2337 ((type2
>= XML_REGEXP_OTHER
) &&
2338 (type2
<= XML_REGEXP_OTHER_NA
))
2341 case XML_REGEXP_NOTDECIMAL
: /* \D */
2343 case XML_REGEXP_REALCHAR
: /* \w */
2344 /* can't be a mark, separator, pontuation, symbol or other */
2345 if ((type2
== XML_REGEXP_NOTDECIMAL
) ||
2346 ((type2
>= XML_REGEXP_MARK
) &&
2347 (type2
<= XML_REGEXP_MARK_ENCLOSING
)) ||
2348 ((type2
>= XML_REGEXP_PUNCT
) &&
2349 (type2
<= XML_REGEXP_PUNCT_OTHERS
)) ||
2350 ((type2
>= XML_REGEXP_SEPAR
) &&
2351 (type2
<= XML_REGEXP_SEPAR_PARA
)) ||
2352 ((type2
>= XML_REGEXP_SYMBOL
) &&
2353 (type2
<= XML_REGEXP_SYMBOL_OTHERS
)) ||
2354 ((type2
>= XML_REGEXP_OTHER
) &&
2355 (type2
<= XML_REGEXP_OTHER_NA
))
2358 case XML_REGEXP_NOTREALCHAR
: /* \W */
2361 * at that point we know both type 1 and type2 are from
2362 * character categories are ordered and are different,
2363 * it becomes simple because this is a partition
2365 case XML_REGEXP_LETTER
:
2366 if (type2
<= XML_REGEXP_LETTER_OTHERS
)
2369 case XML_REGEXP_LETTER_UPPERCASE
:
2370 case XML_REGEXP_LETTER_LOWERCASE
:
2371 case XML_REGEXP_LETTER_TITLECASE
:
2372 case XML_REGEXP_LETTER_MODIFIER
:
2373 case XML_REGEXP_LETTER_OTHERS
:
2375 case XML_REGEXP_MARK
:
2376 if (type2
<= XML_REGEXP_MARK_ENCLOSING
)
2379 case XML_REGEXP_MARK_NONSPACING
:
2380 case XML_REGEXP_MARK_SPACECOMBINING
:
2381 case XML_REGEXP_MARK_ENCLOSING
:
2383 case XML_REGEXP_NUMBER
:
2384 if (type2
<= XML_REGEXP_NUMBER_OTHERS
)
2387 case XML_REGEXP_NUMBER_DECIMAL
:
2388 case XML_REGEXP_NUMBER_LETTER
:
2389 case XML_REGEXP_NUMBER_OTHERS
:
2391 case XML_REGEXP_PUNCT
:
2392 if (type2
<= XML_REGEXP_PUNCT_OTHERS
)
2395 case XML_REGEXP_PUNCT_CONNECTOR
:
2396 case XML_REGEXP_PUNCT_DASH
:
2397 case XML_REGEXP_PUNCT_OPEN
:
2398 case XML_REGEXP_PUNCT_CLOSE
:
2399 case XML_REGEXP_PUNCT_INITQUOTE
:
2400 case XML_REGEXP_PUNCT_FINQUOTE
:
2401 case XML_REGEXP_PUNCT_OTHERS
:
2403 case XML_REGEXP_SEPAR
:
2404 if (type2
<= XML_REGEXP_SEPAR_PARA
)
2407 case XML_REGEXP_SEPAR_SPACE
:
2408 case XML_REGEXP_SEPAR_LINE
:
2409 case XML_REGEXP_SEPAR_PARA
:
2411 case XML_REGEXP_SYMBOL
:
2412 if (type2
<= XML_REGEXP_SYMBOL_OTHERS
)
2415 case XML_REGEXP_SYMBOL_MATH
:
2416 case XML_REGEXP_SYMBOL_CURRENCY
:
2417 case XML_REGEXP_SYMBOL_MODIFIER
:
2418 case XML_REGEXP_SYMBOL_OTHERS
:
2420 case XML_REGEXP_OTHER
:
2421 if (type2
<= XML_REGEXP_OTHER_NA
)
2424 case XML_REGEXP_OTHER_CONTROL
:
2425 case XML_REGEXP_OTHER_FORMAT
:
2426 case XML_REGEXP_OTHER_PRIVATE
:
2427 case XML_REGEXP_OTHER_NA
:
2439 * @deep: if not set only compare string pointers
2441 * Compares two atoms to check whether they are the same exactly
2442 * this is used to remove equivalent transitions
2444 * Returns 1 if same and 0 otherwise
2447 xmlFAEqualAtoms(xmlRegAtomPtr atom1
, xmlRegAtomPtr atom2
, int deep
) {
2452 if ((atom1
== NULL
) || (atom2
== NULL
))
2455 if (atom1
->type
!= atom2
->type
)
2457 switch (atom1
->type
) {
2458 case XML_REGEXP_EPSILON
:
2461 case XML_REGEXP_STRING
:
2463 ret
= (atom1
->valuep
== atom2
->valuep
);
2465 ret
= xmlStrEqual((xmlChar
*)atom1
->valuep
,
2466 (xmlChar
*)atom2
->valuep
);
2468 case XML_REGEXP_CHARVAL
:
2469 ret
= (atom1
->codepoint
== atom2
->codepoint
);
2471 case XML_REGEXP_RANGES
:
2472 /* too hard to do in the general case */
2481 * xmlFACompareAtoms:
2484 * @deep: if not set only compare string pointers
2486 * Compares two atoms to check whether they intersect in some ways,
2487 * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2489 * Returns 1 if yes and 0 otherwise
2492 xmlFACompareAtoms(xmlRegAtomPtr atom1
, xmlRegAtomPtr atom2
, int deep
) {
2497 if ((atom1
== NULL
) || (atom2
== NULL
))
2500 if ((atom1
->type
== XML_REGEXP_ANYCHAR
) ||
2501 (atom2
->type
== XML_REGEXP_ANYCHAR
))
2504 if (atom1
->type
> atom2
->type
) {
2510 if (atom1
->type
!= atom2
->type
) {
2511 ret
= xmlFACompareAtomTypes(atom1
->type
, atom2
->type
);
2512 /* if they can't intersect at the type level break now */
2516 switch (atom1
->type
) {
2517 case XML_REGEXP_STRING
:
2519 ret
= (atom1
->valuep
!= atom2
->valuep
);
2521 ret
= xmlRegStrEqualWildcard((xmlChar
*)atom1
->valuep
,
2522 (xmlChar
*)atom2
->valuep
);
2524 case XML_REGEXP_EPSILON
:
2525 goto not_determinist
;
2526 case XML_REGEXP_CHARVAL
:
2527 if (atom2
->type
== XML_REGEXP_CHARVAL
) {
2528 ret
= (atom1
->codepoint
== atom2
->codepoint
);
2530 ret
= xmlRegCheckCharacter(atom2
, atom1
->codepoint
);
2535 case XML_REGEXP_RANGES
:
2536 if (atom2
->type
== XML_REGEXP_RANGES
) {
2538 xmlRegRangePtr r1
, r2
;
2541 * need to check that none of the ranges eventually matches
2543 for (i
= 0;i
< atom1
->nbRanges
;i
++) {
2544 for (j
= 0;j
< atom2
->nbRanges
;j
++) {
2545 r1
= atom1
->ranges
[i
];
2546 r2
= atom2
->ranges
[j
];
2547 res
= xmlFACompareRanges(r1
, r2
);
2558 goto not_determinist
;
2561 if (atom1
->neg
!= atom2
->neg
) {
2571 * xmlFARecurseDeterminism:
2572 * @ctxt: a regexp parser context
2574 * Check whether the associated regexp is determinist,
2575 * should be called after xmlFAEliminateEpsilonTransitions()
2579 xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt
, xmlRegStatePtr state
,
2580 int to
, xmlRegAtomPtr atom
) {
2583 int transnr
, nbTrans
;
2590 if (ctxt
->flags
& AM_AUTOMATA_RNG
)
2594 * don't recurse on transitions potentially added in the course of
2597 nbTrans
= state
->nbTrans
;
2598 for (transnr
= 0;transnr
< nbTrans
;transnr
++) {
2599 t1
= &(state
->trans
[transnr
]);
2601 * check transitions conflicting with the one looked at
2603 if (t1
->atom
== NULL
) {
2606 res
= xmlFARecurseDeterminism(ctxt
, ctxt
->states
[t1
->to
],
2616 if (xmlFACompareAtoms(t1
->atom
, atom
, deep
)) {
2618 /* mark the transition as non-deterministic */
2626 * xmlFAComputesDeterminism:
2627 * @ctxt: a regexp parser context
2629 * Check whether the associated regexp is determinist,
2630 * should be called after xmlFAEliminateEpsilonTransitions()
2634 xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt
) {
2635 int statenr
, transnr
;
2636 xmlRegStatePtr state
;
2637 xmlRegTransPtr t1
, t2
, last
;
2642 #ifdef DEBUG_REGEXP_GRAPH
2643 printf("xmlFAComputesDeterminism\n");
2644 xmlRegPrintCtxt(stdout
, ctxt
);
2646 if (ctxt
->determinist
!= -1)
2647 return(ctxt
->determinist
);
2649 if (ctxt
->flags
& AM_AUTOMATA_RNG
)
2653 * First cleanup the automata removing cancelled transitions
2655 for (statenr
= 0;statenr
< ctxt
->nbStates
;statenr
++) {
2656 state
= ctxt
->states
[statenr
];
2659 if (state
->nbTrans
< 2)
2661 for (transnr
= 0;transnr
< state
->nbTrans
;transnr
++) {
2662 t1
= &(state
->trans
[transnr
]);
2664 * Determinism checks in case of counted or all transitions
2665 * will have to be handled separately
2667 if (t1
->atom
== NULL
) {
2671 if (t1
->to
== -1) /* eliminated */
2673 for (i
= 0;i
< transnr
;i
++) {
2674 t2
= &(state
->trans
[i
]);
2675 if (t2
->to
== -1) /* eliminated */
2677 if (t2
->atom
!= NULL
) {
2678 if (t1
->to
== t2
->to
) {
2680 * Here we use deep because we want to keep the
2681 * transitions which indicate a conflict
2683 if (xmlFAEqualAtoms(t1
->atom
, t2
->atom
, deep
) &&
2684 (t1
->counter
== t2
->counter
) &&
2685 (t1
->count
== t2
->count
))
2686 t2
->to
= -1; /* eliminated */
2694 * Check for all states that there aren't 2 transitions
2695 * with the same atom and a different target.
2697 for (statenr
= 0;statenr
< ctxt
->nbStates
;statenr
++) {
2698 state
= ctxt
->states
[statenr
];
2701 if (state
->nbTrans
< 2)
2704 for (transnr
= 0;transnr
< state
->nbTrans
;transnr
++) {
2705 t1
= &(state
->trans
[transnr
]);
2707 * Determinism checks in case of counted or all transitions
2708 * will have to be handled separately
2710 if (t1
->atom
== NULL
) {
2713 if (t1
->to
== -1) /* eliminated */
2715 for (i
= 0;i
< transnr
;i
++) {
2716 t2
= &(state
->trans
[i
]);
2717 if (t2
->to
== -1) /* eliminated */
2719 if (t2
->atom
!= NULL
) {
2721 * But here we don't use deep because we want to
2722 * find transitions which indicate a conflict
2724 if (xmlFACompareAtoms(t1
->atom
, t2
->atom
, 1)) {
2726 /* mark the transitions as non-deterministic ones */
2731 } else if (t1
->to
!= -1) {
2733 * do the closure in case of remaining specific
2734 * epsilon transitions like choices or all
2736 ret
= xmlFARecurseDeterminism(ctxt
, ctxt
->states
[t1
->to
],
2738 /* don't shortcut the computation so all non deterministic
2739 transition get marked down
2750 /* don't shortcut the computation so all non deterministic
2751 transition get marked down
2757 * mark specifically the last non-deterministic transition
2758 * from a state since there is no need to set-up rollback
2765 /* don't shortcut the computation so all non deterministic
2766 transition get marked down
2771 ctxt
->determinist
= ret
;
2775 /************************************************************************
2777 * Routines to check input against transition atoms *
2779 ************************************************************************/
2782 xmlRegCheckCharacterRange(xmlRegAtomType type
, int codepoint
, int neg
,
2783 int start
, int end
, const xmlChar
*blockName
) {
2787 case XML_REGEXP_STRING
:
2788 case XML_REGEXP_SUBREG
:
2789 case XML_REGEXP_RANGES
:
2790 case XML_REGEXP_EPSILON
:
2792 case XML_REGEXP_ANYCHAR
:
2793 ret
= ((codepoint
!= '\n') && (codepoint
!= '\r'));
2795 case XML_REGEXP_CHARVAL
:
2796 ret
= ((codepoint
>= start
) && (codepoint
<= end
));
2798 case XML_REGEXP_NOTSPACE
:
2800 case XML_REGEXP_ANYSPACE
:
2801 ret
= ((codepoint
== '\n') || (codepoint
== '\r') ||
2802 (codepoint
== '\t') || (codepoint
== ' '));
2804 case XML_REGEXP_NOTINITNAME
:
2806 case XML_REGEXP_INITNAME
:
2807 ret
= (IS_LETTER(codepoint
) ||
2808 (codepoint
== '_') || (codepoint
== ':'));
2810 case XML_REGEXP_NOTNAMECHAR
:
2812 case XML_REGEXP_NAMECHAR
:
2813 ret
= (IS_LETTER(codepoint
) || IS_DIGIT(codepoint
) ||
2814 (codepoint
== '.') || (codepoint
== '-') ||
2815 (codepoint
== '_') || (codepoint
== ':') ||
2816 IS_COMBINING(codepoint
) || IS_EXTENDER(codepoint
));
2818 case XML_REGEXP_NOTDECIMAL
:
2820 case XML_REGEXP_DECIMAL
:
2821 ret
= xmlUCSIsCatNd(codepoint
);
2823 case XML_REGEXP_REALCHAR
:
2825 case XML_REGEXP_NOTREALCHAR
:
2826 ret
= xmlUCSIsCatP(codepoint
);
2828 ret
= xmlUCSIsCatZ(codepoint
);
2830 ret
= xmlUCSIsCatC(codepoint
);
2832 case XML_REGEXP_LETTER
:
2833 ret
= xmlUCSIsCatL(codepoint
);
2835 case XML_REGEXP_LETTER_UPPERCASE
:
2836 ret
= xmlUCSIsCatLu(codepoint
);
2838 case XML_REGEXP_LETTER_LOWERCASE
:
2839 ret
= xmlUCSIsCatLl(codepoint
);
2841 case XML_REGEXP_LETTER_TITLECASE
:
2842 ret
= xmlUCSIsCatLt(codepoint
);
2844 case XML_REGEXP_LETTER_MODIFIER
:
2845 ret
= xmlUCSIsCatLm(codepoint
);
2847 case XML_REGEXP_LETTER_OTHERS
:
2848 ret
= xmlUCSIsCatLo(codepoint
);
2850 case XML_REGEXP_MARK
:
2851 ret
= xmlUCSIsCatM(codepoint
);
2853 case XML_REGEXP_MARK_NONSPACING
:
2854 ret
= xmlUCSIsCatMn(codepoint
);
2856 case XML_REGEXP_MARK_SPACECOMBINING
:
2857 ret
= xmlUCSIsCatMc(codepoint
);
2859 case XML_REGEXP_MARK_ENCLOSING
:
2860 ret
= xmlUCSIsCatMe(codepoint
);
2862 case XML_REGEXP_NUMBER
:
2863 ret
= xmlUCSIsCatN(codepoint
);
2865 case XML_REGEXP_NUMBER_DECIMAL
:
2866 ret
= xmlUCSIsCatNd(codepoint
);
2868 case XML_REGEXP_NUMBER_LETTER
:
2869 ret
= xmlUCSIsCatNl(codepoint
);
2871 case XML_REGEXP_NUMBER_OTHERS
:
2872 ret
= xmlUCSIsCatNo(codepoint
);
2874 case XML_REGEXP_PUNCT
:
2875 ret
= xmlUCSIsCatP(codepoint
);
2877 case XML_REGEXP_PUNCT_CONNECTOR
:
2878 ret
= xmlUCSIsCatPc(codepoint
);
2880 case XML_REGEXP_PUNCT_DASH
:
2881 ret
= xmlUCSIsCatPd(codepoint
);
2883 case XML_REGEXP_PUNCT_OPEN
:
2884 ret
= xmlUCSIsCatPs(codepoint
);
2886 case XML_REGEXP_PUNCT_CLOSE
:
2887 ret
= xmlUCSIsCatPe(codepoint
);
2889 case XML_REGEXP_PUNCT_INITQUOTE
:
2890 ret
= xmlUCSIsCatPi(codepoint
);
2892 case XML_REGEXP_PUNCT_FINQUOTE
:
2893 ret
= xmlUCSIsCatPf(codepoint
);
2895 case XML_REGEXP_PUNCT_OTHERS
:
2896 ret
= xmlUCSIsCatPo(codepoint
);
2898 case XML_REGEXP_SEPAR
:
2899 ret
= xmlUCSIsCatZ(codepoint
);
2901 case XML_REGEXP_SEPAR_SPACE
:
2902 ret
= xmlUCSIsCatZs(codepoint
);
2904 case XML_REGEXP_SEPAR_LINE
:
2905 ret
= xmlUCSIsCatZl(codepoint
);
2907 case XML_REGEXP_SEPAR_PARA
:
2908 ret
= xmlUCSIsCatZp(codepoint
);
2910 case XML_REGEXP_SYMBOL
:
2911 ret
= xmlUCSIsCatS(codepoint
);
2913 case XML_REGEXP_SYMBOL_MATH
:
2914 ret
= xmlUCSIsCatSm(codepoint
);
2916 case XML_REGEXP_SYMBOL_CURRENCY
:
2917 ret
= xmlUCSIsCatSc(codepoint
);
2919 case XML_REGEXP_SYMBOL_MODIFIER
:
2920 ret
= xmlUCSIsCatSk(codepoint
);
2922 case XML_REGEXP_SYMBOL_OTHERS
:
2923 ret
= xmlUCSIsCatSo(codepoint
);
2925 case XML_REGEXP_OTHER
:
2926 ret
= xmlUCSIsCatC(codepoint
);
2928 case XML_REGEXP_OTHER_CONTROL
:
2929 ret
= xmlUCSIsCatCc(codepoint
);
2931 case XML_REGEXP_OTHER_FORMAT
:
2932 ret
= xmlUCSIsCatCf(codepoint
);
2934 case XML_REGEXP_OTHER_PRIVATE
:
2935 ret
= xmlUCSIsCatCo(codepoint
);
2937 case XML_REGEXP_OTHER_NA
:
2938 /* ret = xmlUCSIsCatCn(codepoint); */
2939 /* Seems it doesn't exist anymore in recent Unicode releases */
2942 case XML_REGEXP_BLOCK_NAME
:
2943 ret
= xmlUCSIsBlock(codepoint
, (const char *) blockName
);
2952 xmlRegCheckCharacter(xmlRegAtomPtr atom
, int codepoint
) {
2954 xmlRegRangePtr range
;
2956 if ((atom
== NULL
) || (!IS_CHAR(codepoint
)))
2959 switch (atom
->type
) {
2960 case XML_REGEXP_SUBREG
:
2961 case XML_REGEXP_EPSILON
:
2963 case XML_REGEXP_CHARVAL
:
2964 return(codepoint
== atom
->codepoint
);
2965 case XML_REGEXP_RANGES
: {
2968 for (i
= 0;i
< atom
->nbRanges
;i
++) {
2969 range
= atom
->ranges
[i
];
2970 if (range
->neg
== 2) {
2971 ret
= xmlRegCheckCharacterRange(range
->type
, codepoint
,
2972 0, range
->start
, range
->end
,
2975 return(0); /* excluded char */
2976 } else if (range
->neg
) {
2977 ret
= xmlRegCheckCharacterRange(range
->type
, codepoint
,
2978 0, range
->start
, range
->end
,
2985 ret
= xmlRegCheckCharacterRange(range
->type
, codepoint
,
2986 0, range
->start
, range
->end
,
2989 accept
= 1; /* might still be excluded */
2994 case XML_REGEXP_STRING
:
2995 printf("TODO: XML_REGEXP_STRING\n");
2997 case XML_REGEXP_ANYCHAR
:
2998 case XML_REGEXP_ANYSPACE
:
2999 case XML_REGEXP_NOTSPACE
:
3000 case XML_REGEXP_INITNAME
:
3001 case XML_REGEXP_NOTINITNAME
:
3002 case XML_REGEXP_NAMECHAR
:
3003 case XML_REGEXP_NOTNAMECHAR
:
3004 case XML_REGEXP_DECIMAL
:
3005 case XML_REGEXP_NOTDECIMAL
:
3006 case XML_REGEXP_REALCHAR
:
3007 case XML_REGEXP_NOTREALCHAR
:
3008 case XML_REGEXP_LETTER
:
3009 case XML_REGEXP_LETTER_UPPERCASE
:
3010 case XML_REGEXP_LETTER_LOWERCASE
:
3011 case XML_REGEXP_LETTER_TITLECASE
:
3012 case XML_REGEXP_LETTER_MODIFIER
:
3013 case XML_REGEXP_LETTER_OTHERS
:
3014 case XML_REGEXP_MARK
:
3015 case XML_REGEXP_MARK_NONSPACING
:
3016 case XML_REGEXP_MARK_SPACECOMBINING
:
3017 case XML_REGEXP_MARK_ENCLOSING
:
3018 case XML_REGEXP_NUMBER
:
3019 case XML_REGEXP_NUMBER_DECIMAL
:
3020 case XML_REGEXP_NUMBER_LETTER
:
3021 case XML_REGEXP_NUMBER_OTHERS
:
3022 case XML_REGEXP_PUNCT
:
3023 case XML_REGEXP_PUNCT_CONNECTOR
:
3024 case XML_REGEXP_PUNCT_DASH
:
3025 case XML_REGEXP_PUNCT_OPEN
:
3026 case XML_REGEXP_PUNCT_CLOSE
:
3027 case XML_REGEXP_PUNCT_INITQUOTE
:
3028 case XML_REGEXP_PUNCT_FINQUOTE
:
3029 case XML_REGEXP_PUNCT_OTHERS
:
3030 case XML_REGEXP_SEPAR
:
3031 case XML_REGEXP_SEPAR_SPACE
:
3032 case XML_REGEXP_SEPAR_LINE
:
3033 case XML_REGEXP_SEPAR_PARA
:
3034 case XML_REGEXP_SYMBOL
:
3035 case XML_REGEXP_SYMBOL_MATH
:
3036 case XML_REGEXP_SYMBOL_CURRENCY
:
3037 case XML_REGEXP_SYMBOL_MODIFIER
:
3038 case XML_REGEXP_SYMBOL_OTHERS
:
3039 case XML_REGEXP_OTHER
:
3040 case XML_REGEXP_OTHER_CONTROL
:
3041 case XML_REGEXP_OTHER_FORMAT
:
3042 case XML_REGEXP_OTHER_PRIVATE
:
3043 case XML_REGEXP_OTHER_NA
:
3044 case XML_REGEXP_BLOCK_NAME
:
3045 ret
= xmlRegCheckCharacterRange(atom
->type
, codepoint
, 0, 0, 0,
3046 (const xmlChar
*)atom
->valuep
);
3054 /************************************************************************
3056 * Saving and restoring state of an execution context *
3058 ************************************************************************/
3060 #ifdef DEBUG_REGEXP_EXEC
3062 xmlFARegDebugExec(xmlRegExecCtxtPtr exec
) {
3063 printf("state: %d:%d:idx %d", exec
->state
->no
, exec
->transno
, exec
->index
);
3064 if (exec
->inputStack
!= NULL
) {
3067 for (i
= 0;(i
< 3) && (i
< exec
->inputStackNr
);i
++)
3068 printf("%s ", (const char *)
3069 exec
->inputStack
[exec
->inputStackNr
- (i
+ 1)].value
);
3071 printf(": %s", &(exec
->inputString
[exec
->index
]));
3078 xmlFARegExecSave(xmlRegExecCtxtPtr exec
) {
3079 #ifdef DEBUG_REGEXP_EXEC
3082 xmlFARegDebugExec(exec
);
3086 if (exec
->nbPush
> MAX_PUSH
) {
3092 if (exec
->maxRollbacks
== 0) {
3093 exec
->maxRollbacks
= 4;
3094 exec
->rollbacks
= (xmlRegExecRollback
*) xmlMalloc(exec
->maxRollbacks
*
3095 sizeof(xmlRegExecRollback
));
3096 if (exec
->rollbacks
== NULL
) {
3097 xmlRegexpErrMemory(NULL
, "saving regexp");
3098 exec
->maxRollbacks
= 0;
3101 memset(exec
->rollbacks
, 0,
3102 exec
->maxRollbacks
* sizeof(xmlRegExecRollback
));
3103 } else if (exec
->nbRollbacks
>= exec
->maxRollbacks
) {
3104 xmlRegExecRollback
*tmp
;
3105 int len
= exec
->maxRollbacks
;
3107 exec
->maxRollbacks
*= 2;
3108 tmp
= (xmlRegExecRollback
*) xmlRealloc(exec
->rollbacks
,
3109 exec
->maxRollbacks
* sizeof(xmlRegExecRollback
));
3111 xmlRegexpErrMemory(NULL
, "saving regexp");
3112 exec
->maxRollbacks
/= 2;
3115 exec
->rollbacks
= tmp
;
3116 tmp
= &exec
->rollbacks
[len
];
3117 memset(tmp
, 0, (exec
->maxRollbacks
- len
) * sizeof(xmlRegExecRollback
));
3119 exec
->rollbacks
[exec
->nbRollbacks
].state
= exec
->state
;
3120 exec
->rollbacks
[exec
->nbRollbacks
].index
= exec
->index
;
3121 exec
->rollbacks
[exec
->nbRollbacks
].nextbranch
= exec
->transno
+ 1;
3122 if (exec
->comp
->nbCounters
> 0) {
3123 if (exec
->rollbacks
[exec
->nbRollbacks
].counts
== NULL
) {
3124 exec
->rollbacks
[exec
->nbRollbacks
].counts
= (int *)
3125 xmlMalloc(exec
->comp
->nbCounters
* sizeof(int));
3126 if (exec
->rollbacks
[exec
->nbRollbacks
].counts
== NULL
) {
3127 xmlRegexpErrMemory(NULL
, "saving regexp");
3132 memcpy(exec
->rollbacks
[exec
->nbRollbacks
].counts
, exec
->counts
,
3133 exec
->comp
->nbCounters
* sizeof(int));
3135 exec
->nbRollbacks
++;
3139 xmlFARegExecRollBack(xmlRegExecCtxtPtr exec
) {
3140 if (exec
->nbRollbacks
<= 0) {
3142 #ifdef DEBUG_REGEXP_EXEC
3143 printf("rollback failed on empty stack\n");
3147 exec
->nbRollbacks
--;
3148 exec
->state
= exec
->rollbacks
[exec
->nbRollbacks
].state
;
3149 exec
->index
= exec
->rollbacks
[exec
->nbRollbacks
].index
;
3150 exec
->transno
= exec
->rollbacks
[exec
->nbRollbacks
].nextbranch
;
3151 if (exec
->comp
->nbCounters
> 0) {
3152 if (exec
->rollbacks
[exec
->nbRollbacks
].counts
== NULL
) {
3153 fprintf(stderr
, "exec save: allocation failed");
3157 memcpy(exec
->counts
, exec
->rollbacks
[exec
->nbRollbacks
].counts
,
3158 exec
->comp
->nbCounters
* sizeof(int));
3161 #ifdef DEBUG_REGEXP_EXEC
3162 printf("restored ");
3163 xmlFARegDebugExec(exec
);
3167 /************************************************************************
3169 * Verifier, running an input against a compiled regexp *
3171 ************************************************************************/
3174 xmlFARegExec(xmlRegexpPtr comp
, const xmlChar
*content
) {
3175 xmlRegExecCtxt execval
;
3176 xmlRegExecCtxtPtr exec
= &execval
;
3177 int ret
, codepoint
= 0, len
, deter
;
3179 exec
->inputString
= content
;
3182 exec
->determinist
= 1;
3183 exec
->maxRollbacks
= 0;
3184 exec
->nbRollbacks
= 0;
3185 exec
->rollbacks
= NULL
;
3188 exec
->state
= comp
->states
[0];
3190 exec
->transcount
= 0;
3191 exec
->inputStack
= NULL
;
3192 exec
->inputStackMax
= 0;
3193 if (comp
->nbCounters
> 0) {
3194 exec
->counts
= (int *) xmlMalloc(comp
->nbCounters
* sizeof(int));
3195 if (exec
->counts
== NULL
) {
3196 xmlRegexpErrMemory(NULL
, "running regexp");
3199 memset(exec
->counts
, 0, comp
->nbCounters
* sizeof(int));
3201 exec
->counts
= NULL
;
3202 while ((exec
->status
== 0) &&
3203 ((exec
->inputString
[exec
->index
] != 0) ||
3204 ((exec
->state
!= NULL
) &&
3205 (exec
->state
->type
!= XML_REGEXP_FINAL_STATE
)))) {
3206 xmlRegTransPtr trans
;
3210 * If end of input on non-terminal state, rollback, however we may
3211 * still have epsilon like transition for counted transitions
3212 * on counters, in that case don't break too early. Additionally,
3213 * if we are working on a range like "AB{0,2}", where B is not present,
3214 * we don't want to break.
3217 if ((exec
->inputString
[exec
->index
] == 0) && (exec
->counts
== NULL
)) {
3219 * if there is a transition, we must check if
3220 * atom allows minOccurs of 0
3222 if (exec
->transno
< exec
->state
->nbTrans
) {
3223 trans
= &exec
->state
->trans
[exec
->transno
];
3224 if (trans
->to
>=0) {
3226 if (!((atom
->min
== 0) && (atom
->max
> 0)))
3233 exec
->transcount
= 0;
3234 for (;exec
->transno
< exec
->state
->nbTrans
;exec
->transno
++) {
3235 trans
= &exec
->state
->trans
[exec
->transno
];
3241 if (trans
->count
>= 0) {
3243 xmlRegCounterPtr counter
;
3245 if (exec
->counts
== NULL
) {
3250 * A counted transition.
3253 count
= exec
->counts
[trans
->count
];
3254 counter
= &exec
->comp
->counters
[trans
->count
];
3255 #ifdef DEBUG_REGEXP_EXEC
3256 printf("testing count %d: val %d, min %d, max %d\n",
3257 trans
->count
, count
, counter
->min
, counter
->max
);
3259 ret
= ((count
>= counter
->min
) && (count
<= counter
->max
));
3260 if ((ret
) && (counter
->min
!= counter
->max
))
3262 } else if (atom
== NULL
) {
3263 fprintf(stderr
, "epsilon transition left at runtime\n");
3266 } else if (exec
->inputString
[exec
->index
] != 0) {
3267 codepoint
= CUR_SCHAR(&(exec
->inputString
[exec
->index
]), len
);
3268 ret
= xmlRegCheckCharacter(atom
, codepoint
);
3269 if ((ret
== 1) && (atom
->min
>= 0) && (atom
->max
> 0)) {
3270 xmlRegStatePtr to
= comp
->states
[trans
->to
];
3273 * this is a multiple input sequence
3274 * If there is a counter associated increment it now.
3275 * before potentially saving and rollback
3276 * do not increment if the counter is already over the
3277 * maximum limit in which case get to next transition
3279 if (trans
->counter
>= 0) {
3280 xmlRegCounterPtr counter
;
3282 if ((exec
->counts
== NULL
) ||
3283 (exec
->comp
== NULL
) ||
3284 (exec
->comp
->counters
== NULL
)) {
3288 counter
= &exec
->comp
->counters
[trans
->counter
];
3289 if (exec
->counts
[trans
->counter
] >= counter
->max
)
3290 continue; /* for loop on transitions */
3292 #ifdef DEBUG_REGEXP_EXEC
3293 printf("Increasing count %d\n", trans
->counter
);
3295 exec
->counts
[trans
->counter
]++;
3297 if (exec
->state
->nbTrans
> exec
->transno
+ 1) {
3298 xmlFARegExecSave(exec
);
3300 exec
->transcount
= 1;
3303 * Try to progress as much as possible on the input
3305 if (exec
->transcount
== atom
->max
) {
3310 * End of input: stop here
3312 if (exec
->inputString
[exec
->index
] == 0) {
3316 if (exec
->transcount
>= atom
->min
) {
3317 int transno
= exec
->transno
;
3318 xmlRegStatePtr state
= exec
->state
;
3321 * The transition is acceptable save it
3323 exec
->transno
= -1; /* trick */
3325 xmlFARegExecSave(exec
);
3326 exec
->transno
= transno
;
3327 exec
->state
= state
;
3329 codepoint
= CUR_SCHAR(&(exec
->inputString
[exec
->index
]),
3331 ret
= xmlRegCheckCharacter(atom
, codepoint
);
3334 if (exec
->transcount
< atom
->min
)
3338 * If the last check failed but one transition was found
3339 * possible, rollback
3346 if (trans
->counter
>= 0) {
3347 if (exec
->counts
== NULL
) {
3351 #ifdef DEBUG_REGEXP_EXEC
3352 printf("Decreasing count %d\n", trans
->counter
);
3354 exec
->counts
[trans
->counter
]--;
3356 } else if ((ret
== 0) && (atom
->min
== 0) && (atom
->max
> 0)) {
3358 * we don't match on the codepoint, but minOccurs of 0
3359 * says that's ok. Setting len to 0 inhibits stepping
3360 * over the codepoint.
3362 exec
->transcount
= 1;
3366 } else if ((atom
->min
== 0) && (atom
->max
> 0)) {
3367 /* another spot to match when minOccurs is 0 */
3368 exec
->transcount
= 1;
3373 if ((trans
->nd
== 1) ||
3374 ((trans
->count
>= 0) && (deter
== 0) &&
3375 (exec
->state
->nbTrans
> exec
->transno
+ 1))) {
3376 #ifdef DEBUG_REGEXP_EXEC
3378 printf("Saving on nd transition atom %d for %c at %d\n",
3379 trans
->atom
->no
, codepoint
, exec
->index
);
3381 printf("Saving on counted transition count %d for %c at %d\n",
3382 trans
->count
, codepoint
, exec
->index
);
3384 xmlFARegExecSave(exec
);
3386 if (trans
->counter
>= 0) {
3387 xmlRegCounterPtr counter
;
3389 /* make sure we don't go over the counter maximum value */
3390 if ((exec
->counts
== NULL
) ||
3391 (exec
->comp
== NULL
) ||
3392 (exec
->comp
->counters
== NULL
)) {
3396 counter
= &exec
->comp
->counters
[trans
->counter
];
3397 if (exec
->counts
[trans
->counter
] >= counter
->max
)
3398 continue; /* for loop on transitions */
3399 #ifdef DEBUG_REGEXP_EXEC
3400 printf("Increasing count %d\n", trans
->counter
);
3402 exec
->counts
[trans
->counter
]++;
3404 if ((trans
->count
>= 0) &&
3405 (trans
->count
< REGEXP_ALL_COUNTER
)) {
3406 if (exec
->counts
== NULL
) {
3410 #ifdef DEBUG_REGEXP_EXEC
3411 printf("resetting count %d on transition\n",
3414 exec
->counts
[trans
->count
] = 0;
3416 #ifdef DEBUG_REGEXP_EXEC
3417 printf("entering state %d\n", trans
->to
);
3419 exec
->state
= comp
->states
[trans
->to
];
3421 if (trans
->atom
!= NULL
) {
3425 } else if (ret
< 0) {
3430 if ((exec
->transno
!= 0) || (exec
->state
->nbTrans
== 0)) {
3433 * Failed to find a way out
3435 exec
->determinist
= 0;
3436 #ifdef DEBUG_REGEXP_EXEC
3437 printf("rollback from state %d on %d:%c\n", exec
->state
->no
,
3438 codepoint
,codepoint
);
3440 xmlFARegExecRollBack(exec
);
3446 if (exec
->rollbacks
!= NULL
) {
3447 if (exec
->counts
!= NULL
) {
3450 for (i
= 0;i
< exec
->maxRollbacks
;i
++)
3451 if (exec
->rollbacks
[i
].counts
!= NULL
)
3452 xmlFree(exec
->rollbacks
[i
].counts
);
3454 xmlFree(exec
->rollbacks
);
3456 if (exec
->counts
!= NULL
)
3457 xmlFree(exec
->counts
);
3458 if (exec
->status
== 0)
3460 if (exec
->status
== -1) {
3461 if (exec
->nbPush
> MAX_PUSH
)
3465 return(exec
->status
);
3468 /************************************************************************
3470 * Progressive interface to the verifier one atom at a time *
3472 ************************************************************************/
3474 static void testerr(xmlRegExecCtxtPtr exec
);
3478 * xmlRegNewExecCtxt:
3479 * @comp: a precompiled regular expression
3480 * @callback: a callback function used for handling progresses in the
3481 * automata matching phase
3482 * @data: the context data associated to the callback in this context
3484 * Build a context used for progressive evaluation of a regexp.
3486 * Returns the new context
3489 xmlRegNewExecCtxt(xmlRegexpPtr comp
, xmlRegExecCallbacks callback
, void *data
) {
3490 xmlRegExecCtxtPtr exec
;
3494 if ((comp
->compact
== NULL
) && (comp
->states
== NULL
))
3496 exec
= (xmlRegExecCtxtPtr
) xmlMalloc(sizeof(xmlRegExecCtxt
));
3498 xmlRegexpErrMemory(NULL
, "creating execution context");
3501 memset(exec
, 0, sizeof(xmlRegExecCtxt
));
3502 exec
->inputString
= NULL
;
3504 exec
->determinist
= 1;
3505 exec
->maxRollbacks
= 0;
3506 exec
->nbRollbacks
= 0;
3507 exec
->rollbacks
= NULL
;
3510 if (comp
->compact
== NULL
)
3511 exec
->state
= comp
->states
[0];
3513 exec
->transcount
= 0;
3514 exec
->callback
= callback
;
3516 if (comp
->nbCounters
> 0) {
3518 * For error handling, exec->counts is allocated twice the size
3519 * the second half is used to store the data in case of rollback
3521 exec
->counts
= (int *) xmlMalloc(comp
->nbCounters
* sizeof(int)
3523 if (exec
->counts
== NULL
) {
3524 xmlRegexpErrMemory(NULL
, "creating execution context");
3528 memset(exec
->counts
, 0, comp
->nbCounters
* sizeof(int) * 2);
3529 exec
->errCounts
= &exec
->counts
[comp
->nbCounters
];
3531 exec
->counts
= NULL
;
3532 exec
->errCounts
= NULL
;
3534 exec
->inputStackMax
= 0;
3535 exec
->inputStackNr
= 0;
3536 exec
->inputStack
= NULL
;
3537 exec
->errStateNo
= -1;
3538 exec
->errString
= NULL
;
3544 * xmlRegFreeExecCtxt:
3545 * @exec: a regular expression evaulation context
3547 * Free the structures associated to a regular expression evaulation context.
3550 xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec
) {
3554 if (exec
->rollbacks
!= NULL
) {
3555 if (exec
->counts
!= NULL
) {
3558 for (i
= 0;i
< exec
->maxRollbacks
;i
++)
3559 if (exec
->rollbacks
[i
].counts
!= NULL
)
3560 xmlFree(exec
->rollbacks
[i
].counts
);
3562 xmlFree(exec
->rollbacks
);
3564 if (exec
->counts
!= NULL
)
3565 xmlFree(exec
->counts
);
3566 if (exec
->inputStack
!= NULL
) {
3569 for (i
= 0;i
< exec
->inputStackNr
;i
++) {
3570 if (exec
->inputStack
[i
].value
!= NULL
)
3571 xmlFree(exec
->inputStack
[i
].value
);
3573 xmlFree(exec
->inputStack
);
3575 if (exec
->errString
!= NULL
)
3576 xmlFree(exec
->errString
);
3581 xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec
, const xmlChar
*value
,
3584 printf("saving value: %d:%s\n", exec
->inputStackNr
, value
);
3586 if (exec
->inputStackMax
== 0) {
3587 exec
->inputStackMax
= 4;
3588 exec
->inputStack
= (xmlRegInputTokenPtr
)
3589 xmlMalloc(exec
->inputStackMax
* sizeof(xmlRegInputToken
));
3590 if (exec
->inputStack
== NULL
) {
3591 xmlRegexpErrMemory(NULL
, "pushing input string");
3592 exec
->inputStackMax
= 0;
3595 } else if (exec
->inputStackNr
+ 1 >= exec
->inputStackMax
) {
3596 xmlRegInputTokenPtr tmp
;
3598 exec
->inputStackMax
*= 2;
3599 tmp
= (xmlRegInputTokenPtr
) xmlRealloc(exec
->inputStack
,
3600 exec
->inputStackMax
* sizeof(xmlRegInputToken
));
3602 xmlRegexpErrMemory(NULL
, "pushing input string");
3603 exec
->inputStackMax
/= 2;
3606 exec
->inputStack
= tmp
;
3608 exec
->inputStack
[exec
->inputStackNr
].value
= xmlStrdup(value
);
3609 exec
->inputStack
[exec
->inputStackNr
].data
= data
;
3610 exec
->inputStackNr
++;
3611 exec
->inputStack
[exec
->inputStackNr
].value
= NULL
;
3612 exec
->inputStack
[exec
->inputStackNr
].data
= NULL
;
3616 * xmlRegStrEqualWildcard:
3617 * @expStr: the string to be evaluated
3618 * @valStr: the validation string
3620 * Checks if both strings are equal or have the same content. "*"
3621 * can be used as a wildcard in @valStr; "|" is used as a seperator of
3622 * substrings in both @expStr and @valStr.
3624 * Returns 1 if the comparison is satisfied and the number of substrings
3625 * is equal, 0 otherwise.
3629 xmlRegStrEqualWildcard(const xmlChar
*expStr
, const xmlChar
*valStr
) {
3630 if (expStr
== valStr
) return(1);
3631 if (expStr
== NULL
) return(0);
3632 if (valStr
== NULL
) return(0);
3635 * Eval if we have a wildcard for the current item.
3637 if (*expStr
!= *valStr
) {
3638 /* if one of them starts with a wildcard make valStr be it */
3639 if (*valStr
== '*') {
3646 if ((*valStr
!= 0) && (*expStr
!= 0) && (*expStr
++ == '*')) {
3648 if (*valStr
== XML_REG_STRING_SEPARATOR
)
3651 } while (*valStr
!= 0);
3658 } while (*valStr
!= 0);
3666 * xmlRegCompactPushString:
3667 * @exec: a regexp execution context
3668 * @comp: the precompiled exec with a compact table
3669 * @value: a string token input
3670 * @data: data associated to the token to reuse in callbacks
3672 * Push one input token in the execution context
3674 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3675 * a negative value in case of error.
3678 xmlRegCompactPushString(xmlRegExecCtxtPtr exec
,
3680 const xmlChar
*value
,
3682 int state
= exec
->index
;
3685 if ((comp
== NULL
) || (comp
->compact
== NULL
) || (comp
->stringMap
== NULL
))
3688 if (value
== NULL
) {
3690 * are we at a final state ?
3692 if (comp
->compact
[state
* (comp
->nbstrings
+ 1)] ==
3693 XML_REGEXP_FINAL_STATE
)
3699 printf("value pushed: %s\n", value
);
3703 * Examine all outside transitions from current state
3705 for (i
= 0;i
< comp
->nbstrings
;i
++) {
3706 target
= comp
->compact
[state
* (comp
->nbstrings
+ 1) + i
+ 1];
3707 if ((target
> 0) && (target
<= comp
->nbstates
)) {
3708 target
--; /* to avoid 0 */
3709 if (xmlRegStrEqualWildcard(comp
->stringMap
[i
], value
)) {
3710 exec
->index
= target
;
3711 if ((exec
->callback
!= NULL
) && (comp
->transdata
!= NULL
)) {
3712 exec
->callback(exec
->data
, value
,
3713 comp
->transdata
[state
* comp
->nbstrings
+ i
], data
);
3716 printf("entering state %d\n", target
);
3718 if (comp
->compact
[target
* (comp
->nbstrings
+ 1)] ==
3719 XML_REGEXP_SINK_STATE
)
3722 if (comp
->compact
[target
* (comp
->nbstrings
+ 1)] ==
3723 XML_REGEXP_FINAL_STATE
)
3730 * Failed to find an exit transition out from current state for the
3734 printf("failed to find a transition for %s on state %d\n", value
, state
);
3737 if (exec
->errString
!= NULL
)
3738 xmlFree(exec
->errString
);
3739 exec
->errString
= xmlStrdup(value
);
3740 exec
->errStateNo
= state
;
3749 * xmlRegExecPushStringInternal:
3750 * @exec: a regexp execution context or NULL to indicate the end
3751 * @value: a string token input
3752 * @data: data associated to the token to reuse in callbacks
3753 * @compound: value was assembled from 2 strings
3755 * Push one input token in the execution context
3757 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3758 * a negative value in case of error.
3761 xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec
, const xmlChar
*value
,
3762 void *data
, int compound
) {
3763 xmlRegTransPtr trans
;
3771 if (exec
->comp
== NULL
)
3773 if (exec
->status
!= 0)
3774 return(exec
->status
);
3776 if (exec
->comp
->compact
!= NULL
)
3777 return(xmlRegCompactPushString(exec
, exec
->comp
, value
, data
));
3779 if (value
== NULL
) {
3780 if (exec
->state
->type
== XML_REGEXP_FINAL_STATE
)
3786 printf("value pushed: %s\n", value
);
3789 * If we have an active rollback stack push the new value there
3790 * and get back to where we were left
3792 if ((value
!= NULL
) && (exec
->inputStackNr
> 0)) {
3793 xmlFARegExecSaveInputString(exec
, value
, data
);
3794 value
= exec
->inputStack
[exec
->index
].value
;
3795 data
= exec
->inputStack
[exec
->index
].data
;
3797 printf("value loaded: %s\n", value
);
3801 while ((exec
->status
== 0) &&
3804 (exec
->state
->type
!= XML_REGEXP_FINAL_STATE
)))) {
3807 * End of input on non-terminal state, rollback, however we may
3808 * still have epsilon like transition for counted transitions
3809 * on counters, in that case don't break too early.
3811 if ((value
== NULL
) && (exec
->counts
== NULL
))
3814 exec
->transcount
= 0;
3815 for (;exec
->transno
< exec
->state
->nbTrans
;exec
->transno
++) {
3816 trans
= &exec
->state
->trans
[exec
->transno
];
3821 if (trans
->count
== REGEXP_ALL_LAX_COUNTER
) {
3825 xmlRegCounterPtr counter
;
3830 printf("testing all lax %d\n", trans
->count
);
3833 * Check all counted transitions from the current state
3835 if ((value
== NULL
) && (final
)) {
3837 } else if (value
!= NULL
) {
3838 for (i
= 0;i
< exec
->state
->nbTrans
;i
++) {
3839 t
= &exec
->state
->trans
[i
];
3840 if ((t
->counter
< 0) || (t
== trans
))
3842 counter
= &exec
->comp
->counters
[t
->counter
];
3843 count
= exec
->counts
[t
->counter
];
3844 if ((count
< counter
->max
) &&
3845 (t
->atom
!= NULL
) &&
3846 (xmlStrEqual(value
, t
->atom
->valuep
))) {
3850 if ((count
>= counter
->min
) &&
3851 (count
< counter
->max
) &&
3852 (t
->atom
!= NULL
) &&
3853 (xmlStrEqual(value
, t
->atom
->valuep
))) {
3859 } else if (trans
->count
== REGEXP_ALL_COUNTER
) {
3863 xmlRegCounterPtr counter
;
3868 printf("testing all %d\n", trans
->count
);
3871 * Check all counted transitions from the current state
3873 for (i
= 0;i
< exec
->state
->nbTrans
;i
++) {
3874 t
= &exec
->state
->trans
[i
];
3875 if ((t
->counter
< 0) || (t
== trans
))
3877 counter
= &exec
->comp
->counters
[t
->counter
];
3878 count
= exec
->counts
[t
->counter
];
3879 if ((count
< counter
->min
) || (count
> counter
->max
)) {
3884 } else if (trans
->count
>= 0) {
3886 xmlRegCounterPtr counter
;
3889 * A counted transition.
3892 count
= exec
->counts
[trans
->count
];
3893 counter
= &exec
->comp
->counters
[trans
->count
];
3895 printf("testing count %d: val %d, min %d, max %d\n",
3896 trans
->count
, count
, counter
->min
, counter
->max
);
3898 ret
= ((count
>= counter
->min
) && (count
<= counter
->max
));
3899 } else if (atom
== NULL
) {
3900 fprintf(stderr
, "epsilon transition left at runtime\n");
3903 } else if (value
!= NULL
) {
3904 ret
= xmlRegStrEqualWildcard(atom
->valuep
, value
);
3910 if ((ret
== 1) && (trans
->counter
>= 0)) {
3911 xmlRegCounterPtr counter
;
3914 count
= exec
->counts
[trans
->counter
];
3915 counter
= &exec
->comp
->counters
[trans
->counter
];
3916 if (count
>= counter
->max
)
3920 if ((ret
== 1) && (atom
->min
> 0) && (atom
->max
> 0)) {
3921 xmlRegStatePtr to
= exec
->comp
->states
[trans
->to
];
3924 * this is a multiple input sequence
3926 if (exec
->state
->nbTrans
> exec
->transno
+ 1) {
3927 if (exec
->inputStackNr
<= 0) {
3928 xmlFARegExecSaveInputString(exec
, value
, data
);
3930 xmlFARegExecSave(exec
);
3932 exec
->transcount
= 1;
3935 * Try to progress as much as possible on the input
3937 if (exec
->transcount
== atom
->max
) {
3941 value
= exec
->inputStack
[exec
->index
].value
;
3942 data
= exec
->inputStack
[exec
->index
].data
;
3944 printf("value loaded: %s\n", value
);
3948 * End of input: stop here
3950 if (value
== NULL
) {
3954 if (exec
->transcount
>= atom
->min
) {
3955 int transno
= exec
->transno
;
3956 xmlRegStatePtr state
= exec
->state
;
3959 * The transition is acceptable save it
3961 exec
->transno
= -1; /* trick */
3963 if (exec
->inputStackNr
<= 0) {
3964 xmlFARegExecSaveInputString(exec
, value
, data
);
3966 xmlFARegExecSave(exec
);
3967 exec
->transno
= transno
;
3968 exec
->state
= state
;
3970 ret
= xmlStrEqual(value
, atom
->valuep
);
3973 if (exec
->transcount
< atom
->min
)
3977 * If the last check failed but one transition was found
3978 * possible, rollback
3988 if ((exec
->callback
!= NULL
) && (atom
!= NULL
) &&
3990 exec
->callback(exec
->data
, atom
->valuep
,
3993 if (exec
->state
->nbTrans
> exec
->transno
+ 1) {
3994 if (exec
->inputStackNr
<= 0) {
3995 xmlFARegExecSaveInputString(exec
, value
, data
);
3997 xmlFARegExecSave(exec
);
3999 if (trans
->counter
>= 0) {
4001 printf("Increasing count %d\n", trans
->counter
);
4003 exec
->counts
[trans
->counter
]++;
4005 if ((trans
->count
>= 0) &&
4006 (trans
->count
< REGEXP_ALL_COUNTER
)) {
4007 #ifdef DEBUG_REGEXP_EXEC
4008 printf("resetting count %d on transition\n",
4011 exec
->counts
[trans
->count
] = 0;
4014 printf("entering state %d\n", trans
->to
);
4016 if ((exec
->comp
->states
[trans
->to
] != NULL
) &&
4017 (exec
->comp
->states
[trans
->to
]->type
==
4018 XML_REGEXP_SINK_STATE
)) {
4020 * entering a sink state, save the current state as error
4023 if (exec
->errString
!= NULL
)
4024 xmlFree(exec
->errString
);
4025 exec
->errString
= xmlStrdup(value
);
4026 exec
->errState
= exec
->state
;
4027 memcpy(exec
->errCounts
, exec
->counts
,
4028 exec
->comp
->nbCounters
* sizeof(int));
4030 exec
->state
= exec
->comp
->states
[trans
->to
];
4032 if (trans
->atom
!= NULL
) {
4033 if (exec
->inputStack
!= NULL
) {
4035 if (exec
->index
< exec
->inputStackNr
) {
4036 value
= exec
->inputStack
[exec
->index
].value
;
4037 data
= exec
->inputStack
[exec
->index
].data
;
4039 printf("value loaded: %s\n", value
);
4045 printf("end of input\n");
4052 printf("end of input\n");
4057 } else if (ret
< 0) {
4062 if ((exec
->transno
!= 0) || (exec
->state
->nbTrans
== 0)) {
4065 * if we didn't yet rollback on the current input
4066 * store the current state as the error state.
4068 if ((progress
) && (exec
->state
!= NULL
) &&
4069 (exec
->state
->type
!= XML_REGEXP_SINK_STATE
)) {
4071 if (exec
->errString
!= NULL
)
4072 xmlFree(exec
->errString
);
4073 exec
->errString
= xmlStrdup(value
);
4074 exec
->errState
= exec
->state
;
4075 memcpy(exec
->errCounts
, exec
->counts
,
4076 exec
->comp
->nbCounters
* sizeof(int));
4080 * Failed to find a way out
4082 exec
->determinist
= 0;
4083 xmlFARegExecRollBack(exec
);
4084 if (exec
->status
== 0) {
4085 value
= exec
->inputStack
[exec
->index
].value
;
4086 data
= exec
->inputStack
[exec
->index
].data
;
4088 printf("value loaded: %s\n", value
);
4097 if (exec
->status
== 0) {
4098 return(exec
->state
->type
== XML_REGEXP_FINAL_STATE
);
4101 if (exec
->status
< 0) {
4105 return(exec
->status
);
4109 * xmlRegExecPushString:
4110 * @exec: a regexp execution context or NULL to indicate the end
4111 * @value: a string token input
4112 * @data: data associated to the token to reuse in callbacks
4114 * Push one input token in the execution context
4116 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4117 * a negative value in case of error.
4120 xmlRegExecPushString(xmlRegExecCtxtPtr exec
, const xmlChar
*value
,
4122 return(xmlRegExecPushStringInternal(exec
, value
, data
, 0));
4126 * xmlRegExecPushString2:
4127 * @exec: a regexp execution context or NULL to indicate the end
4128 * @value: the first string token input
4129 * @value2: the second string token input
4130 * @data: data associated to the token to reuse in callbacks
4132 * Push one input token in the execution context
4134 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
4135 * a negative value in case of error.
4138 xmlRegExecPushString2(xmlRegExecCtxtPtr exec
, const xmlChar
*value
,
4139 const xmlChar
*value2
, void *data
) {
4141 int lenn
, lenp
, ret
;
4146 if (exec
->comp
== NULL
)
4148 if (exec
->status
!= 0)
4149 return(exec
->status
);
4152 return(xmlRegExecPushString(exec
, value
, data
));
4154 lenn
= strlen((char *) value2
);
4155 lenp
= strlen((char *) value
);
4157 if (150 < lenn
+ lenp
+ 2) {
4158 str
= (xmlChar
*) xmlMallocAtomic(lenn
+ lenp
+ 2);
4166 memcpy(&str
[0], value
, lenp
);
4167 str
[lenp
] = XML_REG_STRING_SEPARATOR
;
4168 memcpy(&str
[lenp
+ 1], value2
, lenn
);
4169 str
[lenn
+ lenp
+ 1] = 0;
4171 if (exec
->comp
->compact
!= NULL
)
4172 ret
= xmlRegCompactPushString(exec
, exec
->comp
, str
, data
);
4174 ret
= xmlRegExecPushStringInternal(exec
, str
, data
, 1);
4182 * xmlRegExecGetValues:
4183 * @exec: a regexp execution context
4184 * @err: error extraction or normal one
4185 * @nbval: pointer to the number of accepted values IN/OUT
4186 * @nbneg: return number of negative transitions
4187 * @values: pointer to the array of acceptable values
4188 * @terminal: return value if this was a terminal state
4190 * Extract informations from the regexp execution, internal routine to
4191 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
4193 * Returns: 0 in case of success or -1 in case of error.
4196 xmlRegExecGetValues(xmlRegExecCtxtPtr exec
, int err
,
4197 int *nbval
, int *nbneg
,
4198 xmlChar
**values
, int *terminal
) {
4202 if ((exec
== NULL
) || (nbval
== NULL
) || (nbneg
== NULL
) ||
4203 (values
== NULL
) || (*nbval
<= 0))
4209 if ((exec
->comp
!= NULL
) && (exec
->comp
->compact
!= NULL
)) {
4211 int target
, i
, state
;
4216 if (exec
->errStateNo
== -1) return(-1);
4217 state
= exec
->errStateNo
;
4219 state
= exec
->index
;
4221 if (terminal
!= NULL
) {
4222 if (comp
->compact
[state
* (comp
->nbstrings
+ 1)] ==
4223 XML_REGEXP_FINAL_STATE
)
4228 for (i
= 0;(i
< comp
->nbstrings
) && (nb
< maxval
);i
++) {
4229 target
= comp
->compact
[state
* (comp
->nbstrings
+ 1) + i
+ 1];
4230 if ((target
> 0) && (target
<= comp
->nbstates
) &&
4231 (comp
->compact
[(target
- 1) * (comp
->nbstrings
+ 1)] !=
4232 XML_REGEXP_SINK_STATE
)) {
4233 values
[nb
++] = comp
->stringMap
[i
];
4237 for (i
= 0;(i
< comp
->nbstrings
) && (nb
< maxval
);i
++) {
4238 target
= comp
->compact
[state
* (comp
->nbstrings
+ 1) + i
+ 1];
4239 if ((target
> 0) && (target
<= comp
->nbstates
) &&
4240 (comp
->compact
[(target
- 1) * (comp
->nbstrings
+ 1)] ==
4241 XML_REGEXP_SINK_STATE
)) {
4242 values
[nb
++] = comp
->stringMap
[i
];
4248 xmlRegTransPtr trans
;
4250 xmlRegStatePtr state
;
4252 if (terminal
!= NULL
) {
4253 if (exec
->state
->type
== XML_REGEXP_FINAL_STATE
)
4260 if (exec
->errState
== NULL
) return(-1);
4261 state
= exec
->errState
;
4263 if (exec
->state
== NULL
) return(-1);
4264 state
= exec
->state
;
4267 (transno
< state
->nbTrans
) && (nb
< maxval
);
4269 trans
= &state
->trans
[transno
];
4273 if ((atom
== NULL
) || (atom
->valuep
== NULL
))
4275 if (trans
->count
== REGEXP_ALL_LAX_COUNTER
) {
4276 /* this should not be reached but ... */
4278 } else if (trans
->count
== REGEXP_ALL_COUNTER
) {
4279 /* this should not be reached but ... */
4281 } else if (trans
->counter
>= 0) {
4282 xmlRegCounterPtr counter
= NULL
;
4286 count
= exec
->errCounts
[trans
->counter
];
4288 count
= exec
->counts
[trans
->counter
];
4289 if (exec
->comp
!= NULL
)
4290 counter
= &exec
->comp
->counters
[trans
->counter
];
4291 if ((counter
== NULL
) || (count
< counter
->max
)) {
4293 values
[nb
++] = (xmlChar
*) atom
->valuep2
;
4295 values
[nb
++] = (xmlChar
*) atom
->valuep
;
4299 if ((exec
->comp
->states
[trans
->to
] != NULL
) &&
4300 (exec
->comp
->states
[trans
->to
]->type
!=
4301 XML_REGEXP_SINK_STATE
)) {
4303 values
[nb
++] = (xmlChar
*) atom
->valuep2
;
4305 values
[nb
++] = (xmlChar
*) atom
->valuep
;
4311 (transno
< state
->nbTrans
) && (nb
< maxval
);
4313 trans
= &state
->trans
[transno
];
4317 if ((atom
== NULL
) || (atom
->valuep
== NULL
))
4319 if (trans
->count
== REGEXP_ALL_LAX_COUNTER
) {
4321 } else if (trans
->count
== REGEXP_ALL_COUNTER
) {
4323 } else if (trans
->counter
>= 0) {
4326 if ((exec
->comp
->states
[trans
->to
] != NULL
) &&
4327 (exec
->comp
->states
[trans
->to
]->type
==
4328 XML_REGEXP_SINK_STATE
)) {
4330 values
[nb
++] = (xmlChar
*) atom
->valuep2
;
4332 values
[nb
++] = (xmlChar
*) atom
->valuep
;
4342 * xmlRegExecNextValues:
4343 * @exec: a regexp execution context
4344 * @nbval: pointer to the number of accepted values IN/OUT
4345 * @nbneg: return number of negative transitions
4346 * @values: pointer to the array of acceptable values
4347 * @terminal: return value if this was a terminal state
4349 * Extract informations from the regexp execution,
4350 * the parameter @values must point to an array of @nbval string pointers
4351 * on return nbval will contain the number of possible strings in that
4352 * state and the @values array will be updated with them. The string values
4353 * returned will be freed with the @exec context and don't need to be
4356 * Returns: 0 in case of success or -1 in case of error.
4359 xmlRegExecNextValues(xmlRegExecCtxtPtr exec
, int *nbval
, int *nbneg
,
4360 xmlChar
**values
, int *terminal
) {
4361 return(xmlRegExecGetValues(exec
, 0, nbval
, nbneg
, values
, terminal
));
4365 * xmlRegExecErrInfo:
4366 * @exec: a regexp execution context generating an error
4367 * @string: return value for the error string
4368 * @nbval: pointer to the number of accepted values IN/OUT
4369 * @nbneg: return number of negative transitions
4370 * @values: pointer to the array of acceptable values
4371 * @terminal: return value if this was a terminal state
4373 * Extract error informations from the regexp execution, the parameter
4374 * @string will be updated with the value pushed and not accepted,
4375 * the parameter @values must point to an array of @nbval string pointers
4376 * on return nbval will contain the number of possible strings in that
4377 * state and the @values array will be updated with them. The string values
4378 * returned will be freed with the @exec context and don't need to be
4381 * Returns: 0 in case of success or -1 in case of error.
4384 xmlRegExecErrInfo(xmlRegExecCtxtPtr exec
, const xmlChar
**string
,
4385 int *nbval
, int *nbneg
, xmlChar
**values
, int *terminal
) {
4388 if (string
!= NULL
) {
4389 if (exec
->status
!= 0)
4390 *string
= exec
->errString
;
4394 return(xmlRegExecGetValues(exec
, 1, nbval
, nbneg
, values
, terminal
));
4398 static void testerr(xmlRegExecCtxtPtr exec
) {
4399 const xmlChar
*string
;
4404 xmlRegExecErrInfo(exec
, &string
, &nb
, &nbneg
, &values
[0], &terminal
);
4410 xmlRegExecPushChar(xmlRegExecCtxtPtr exec
, int UCS
) {
4411 xmlRegTransPtr trans
;
4418 if (exec
->status
!= 0)
4419 return(exec
->status
);
4421 while ((exec
->status
== 0) &&
4422 ((exec
->inputString
[exec
->index
] != 0) ||
4423 (exec
->state
->type
!= XML_REGEXP_FINAL_STATE
))) {
4426 * End of input on non-terminal state, rollback, however we may
4427 * still have epsilon like transition for counted transitions
4428 * on counters, in that case don't break too early.
4430 if ((exec
->inputString
[exec
->index
] == 0) && (exec
->counts
== NULL
))
4433 exec
->transcount
= 0;
4434 for (;exec
->transno
< exec
->state
->nbTrans
;exec
->transno
++) {
4435 trans
= &exec
->state
->trans
[exec
->transno
];
4440 if (trans
->count
>= 0) {
4442 xmlRegCounterPtr counter
;
4445 * A counted transition.
4448 count
= exec
->counts
[trans
->count
];
4449 counter
= &exec
->comp
->counters
[trans
->count
];
4450 #ifdef DEBUG_REGEXP_EXEC
4451 printf("testing count %d: val %d, min %d, max %d\n",
4452 trans
->count
, count
, counter
->min
, counter
->max
);
4454 ret
= ((count
>= counter
->min
) && (count
<= counter
->max
));
4455 } else if (atom
== NULL
) {
4456 fprintf(stderr
, "epsilon transition left at runtime\n");
4459 } else if (exec
->inputString
[exec
->index
] != 0) {
4460 codepoint
= CUR_SCHAR(&(exec
->inputString
[exec
->index
]), len
);
4461 ret
= xmlRegCheckCharacter(atom
, codepoint
);
4462 if ((ret
== 1) && (atom
->min
> 0) && (atom
->max
> 0)) {
4463 xmlRegStatePtr to
= exec
->comp
->states
[trans
->to
];
4466 * this is a multiple input sequence
4468 if (exec
->state
->nbTrans
> exec
->transno
+ 1) {
4469 xmlFARegExecSave(exec
);
4471 exec
->transcount
= 1;
4474 * Try to progress as much as possible on the input
4476 if (exec
->transcount
== atom
->max
) {
4481 * End of input: stop here
4483 if (exec
->inputString
[exec
->index
] == 0) {
4487 if (exec
->transcount
>= atom
->min
) {
4488 int transno
= exec
->transno
;
4489 xmlRegStatePtr state
= exec
->state
;
4492 * The transition is acceptable save it
4494 exec
->transno
= -1; /* trick */
4496 xmlFARegExecSave(exec
);
4497 exec
->transno
= transno
;
4498 exec
->state
= state
;
4500 codepoint
= CUR_SCHAR(&(exec
->inputString
[exec
->index
]),
4502 ret
= xmlRegCheckCharacter(atom
, codepoint
);
4505 if (exec
->transcount
< atom
->min
)
4509 * If the last check failed but one transition was found
4510 * possible, rollback
4520 if (exec
->state
->nbTrans
> exec
->transno
+ 1) {
4521 xmlFARegExecSave(exec
);
4524 * restart count for expressions like this ((abc){2})*
4526 if (trans
->count
>= 0) {
4527 #ifdef DEBUG_REGEXP_EXEC
4528 printf("Reset count %d\n", trans
->count
);
4530 exec
->counts
[trans
->count
] = 0;
4532 if (trans
->counter
>= 0) {
4533 #ifdef DEBUG_REGEXP_EXEC
4534 printf("Increasing count %d\n", trans
->counter
);
4536 exec
->counts
[trans
->counter
]++;
4538 #ifdef DEBUG_REGEXP_EXEC
4539 printf("entering state %d\n", trans
->to
);
4541 exec
->state
= exec
->comp
->states
[trans
->to
];
4543 if (trans
->atom
!= NULL
) {
4547 } else if (ret
< 0) {
4552 if ((exec
->transno
!= 0) || (exec
->state
->nbTrans
== 0)) {
4555 * Failed to find a way out
4557 exec
->determinist
= 0;
4558 xmlFARegExecRollBack(exec
);
4565 /************************************************************************
4567 * Parser for the Schemas Datatype Regular Expressions *
4568 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4570 ************************************************************************/
4574 * @ctxt: a regexp parser context
4576 * [10] Char ::= [^.\?*+()|#x5B#x5D]
4579 xmlFAIsChar(xmlRegParserCtxtPtr ctxt
) {
4583 cur
= CUR_SCHAR(ctxt
->cur
, len
);
4584 if ((cur
== '.') || (cur
== '\\') || (cur
== '?') ||
4585 (cur
== '*') || (cur
== '+') || (cur
== '(') ||
4586 (cur
== ')') || (cur
== '|') || (cur
== 0x5B) ||
4587 (cur
== 0x5D) || (cur
== 0))
4593 * xmlFAParseCharProp:
4594 * @ctxt: a regexp parser context
4596 * [27] charProp ::= IsCategory | IsBlock
4597 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4598 * Separators | Symbols | Others
4599 * [29] Letters ::= 'L' [ultmo]?
4600 * [30] Marks ::= 'M' [nce]?
4601 * [31] Numbers ::= 'N' [dlo]?
4602 * [32] Punctuation ::= 'P' [cdseifo]?
4603 * [33] Separators ::= 'Z' [slp]?
4604 * [34] Symbols ::= 'S' [mcko]?
4605 * [35] Others ::= 'C' [cfon]?
4606 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4609 xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt
) {
4611 xmlRegAtomType type
= (xmlRegAtomType
) 0;
4612 xmlChar
*blockName
= NULL
;
4620 type
= XML_REGEXP_LETTER_UPPERCASE
;
4621 } else if (cur
== 'l') {
4623 type
= XML_REGEXP_LETTER_LOWERCASE
;
4624 } else if (cur
== 't') {
4626 type
= XML_REGEXP_LETTER_TITLECASE
;
4627 } else if (cur
== 'm') {
4629 type
= XML_REGEXP_LETTER_MODIFIER
;
4630 } else if (cur
== 'o') {
4632 type
= XML_REGEXP_LETTER_OTHERS
;
4634 type
= XML_REGEXP_LETTER
;
4636 } else if (cur
== 'M') {
4642 type
= XML_REGEXP_MARK_NONSPACING
;
4643 } else if (cur
== 'c') {
4645 /* spacing combining */
4646 type
= XML_REGEXP_MARK_SPACECOMBINING
;
4647 } else if (cur
== 'e') {
4650 type
= XML_REGEXP_MARK_ENCLOSING
;
4653 type
= XML_REGEXP_MARK
;
4655 } else if (cur
== 'N') {
4661 type
= XML_REGEXP_NUMBER_DECIMAL
;
4662 } else if (cur
== 'l') {
4665 type
= XML_REGEXP_NUMBER_LETTER
;
4666 } else if (cur
== 'o') {
4669 type
= XML_REGEXP_NUMBER_OTHERS
;
4672 type
= XML_REGEXP_NUMBER
;
4674 } else if (cur
== 'P') {
4680 type
= XML_REGEXP_PUNCT_CONNECTOR
;
4681 } else if (cur
== 'd') {
4684 type
= XML_REGEXP_PUNCT_DASH
;
4685 } else if (cur
== 's') {
4688 type
= XML_REGEXP_PUNCT_OPEN
;
4689 } else if (cur
== 'e') {
4692 type
= XML_REGEXP_PUNCT_CLOSE
;
4693 } else if (cur
== 'i') {
4696 type
= XML_REGEXP_PUNCT_INITQUOTE
;
4697 } else if (cur
== 'f') {
4700 type
= XML_REGEXP_PUNCT_FINQUOTE
;
4701 } else if (cur
== 'o') {
4704 type
= XML_REGEXP_PUNCT_OTHERS
;
4706 /* all punctuation */
4707 type
= XML_REGEXP_PUNCT
;
4709 } else if (cur
== 'Z') {
4715 type
= XML_REGEXP_SEPAR_SPACE
;
4716 } else if (cur
== 'l') {
4719 type
= XML_REGEXP_SEPAR_LINE
;
4720 } else if (cur
== 'p') {
4723 type
= XML_REGEXP_SEPAR_PARA
;
4725 /* all separators */
4726 type
= XML_REGEXP_SEPAR
;
4728 } else if (cur
== 'S') {
4733 type
= XML_REGEXP_SYMBOL_MATH
;
4735 } else if (cur
== 'c') {
4737 type
= XML_REGEXP_SYMBOL_CURRENCY
;
4739 } else if (cur
== 'k') {
4741 type
= XML_REGEXP_SYMBOL_MODIFIER
;
4743 } else if (cur
== 'o') {
4745 type
= XML_REGEXP_SYMBOL_OTHERS
;
4749 type
= XML_REGEXP_SYMBOL
;
4751 } else if (cur
== 'C') {
4757 type
= XML_REGEXP_OTHER_CONTROL
;
4758 } else if (cur
== 'f') {
4761 type
= XML_REGEXP_OTHER_FORMAT
;
4762 } else if (cur
== 'o') {
4765 type
= XML_REGEXP_OTHER_PRIVATE
;
4766 } else if (cur
== 'n') {
4769 type
= XML_REGEXP_OTHER_NA
;
4772 type
= XML_REGEXP_OTHER
;
4774 } else if (cur
== 'I') {
4775 const xmlChar
*start
;
4779 ERROR("IsXXXX expected");
4785 if (((cur
>= 'a') && (cur
<= 'z')) ||
4786 ((cur
>= 'A') && (cur
<= 'Z')) ||
4787 ((cur
>= '0') && (cur
<= '9')) ||
4791 while (((cur
>= 'a') && (cur
<= 'z')) ||
4792 ((cur
>= 'A') && (cur
<= 'Z')) ||
4793 ((cur
>= '0') && (cur
<= '9')) ||
4799 type
= XML_REGEXP_BLOCK_NAME
;
4800 blockName
= xmlStrndup(start
, ctxt
->cur
- start
);
4802 ERROR("Unknown char property");
4805 if (ctxt
->atom
== NULL
) {
4806 ctxt
->atom
= xmlRegNewAtom(ctxt
, type
);
4807 if (ctxt
->atom
!= NULL
)
4808 ctxt
->atom
->valuep
= blockName
;
4809 } else if (ctxt
->atom
->type
== XML_REGEXP_RANGES
) {
4810 xmlRegAtomAddRange(ctxt
, ctxt
->atom
, ctxt
->neg
,
4811 type
, 0, 0, blockName
);
4816 * xmlFAParseCharClassEsc:
4817 * @ctxt: a regexp parser context
4819 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4820 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4821 * [25] catEsc ::= '\p{' charProp '}'
4822 * [26] complEsc ::= '\P{' charProp '}'
4823 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4826 xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt
) {
4830 if (ctxt
->atom
== NULL
) {
4831 ctxt
->atom
= xmlRegNewAtom(ctxt
, XML_REGEXP_ANYCHAR
);
4832 } else if (ctxt
->atom
->type
== XML_REGEXP_RANGES
) {
4833 xmlRegAtomAddRange(ctxt
, ctxt
->atom
, ctxt
->neg
,
4834 XML_REGEXP_ANYCHAR
, 0, 0, NULL
);
4840 ERROR("Escaped sequence: expecting \\");
4848 ERROR("Expecting '{'");
4852 xmlFAParseCharProp(ctxt
);
4854 ERROR("Expecting '}'");
4858 } else if (cur
== 'P') {
4861 ERROR("Expecting '{'");
4865 xmlFAParseCharProp(ctxt
);
4866 ctxt
->atom
->neg
= 1;
4868 ERROR("Expecting '}'");
4872 } else if ((cur
== 'n') || (cur
== 'r') || (cur
== 't') || (cur
== '\\') ||
4873 (cur
== '|') || (cur
== '.') || (cur
== '?') || (cur
== '*') ||
4874 (cur
== '+') || (cur
== '(') || (cur
== ')') || (cur
== '{') ||
4875 (cur
== '}') || (cur
== 0x2D) || (cur
== 0x5B) || (cur
== 0x5D) ||
4877 if (ctxt
->atom
== NULL
) {
4878 ctxt
->atom
= xmlRegNewAtom(ctxt
, XML_REGEXP_CHARVAL
);
4879 if (ctxt
->atom
!= NULL
) {
4882 ctxt
->atom
->codepoint
= '\n';
4885 ctxt
->atom
->codepoint
= '\r';
4888 ctxt
->atom
->codepoint
= '\t';
4891 ctxt
->atom
->codepoint
= cur
;
4894 } else if (ctxt
->atom
->type
== XML_REGEXP_RANGES
) {
4906 xmlRegAtomAddRange(ctxt
, ctxt
->atom
, ctxt
->neg
,
4907 XML_REGEXP_CHARVAL
, cur
, cur
, NULL
);
4910 } else if ((cur
== 's') || (cur
== 'S') || (cur
== 'i') || (cur
== 'I') ||
4911 (cur
== 'c') || (cur
== 'C') || (cur
== 'd') || (cur
== 'D') ||
4912 (cur
== 'w') || (cur
== 'W')) {
4913 xmlRegAtomType type
= XML_REGEXP_ANYSPACE
;
4917 type
= XML_REGEXP_ANYSPACE
;
4920 type
= XML_REGEXP_NOTSPACE
;
4923 type
= XML_REGEXP_INITNAME
;
4926 type
= XML_REGEXP_NOTINITNAME
;
4929 type
= XML_REGEXP_NAMECHAR
;
4932 type
= XML_REGEXP_NOTNAMECHAR
;
4935 type
= XML_REGEXP_DECIMAL
;
4938 type
= XML_REGEXP_NOTDECIMAL
;
4941 type
= XML_REGEXP_REALCHAR
;
4944 type
= XML_REGEXP_NOTREALCHAR
;
4948 if (ctxt
->atom
== NULL
) {
4949 ctxt
->atom
= xmlRegNewAtom(ctxt
, type
);
4950 } else if (ctxt
->atom
->type
== XML_REGEXP_RANGES
) {
4951 xmlRegAtomAddRange(ctxt
, ctxt
->atom
, ctxt
->neg
,
4955 ERROR("Wrong escape sequence, misuse of character '\\'");
4960 * xmlFAParseCharRange:
4961 * @ctxt: a regexp parser context
4963 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4964 * [18] seRange ::= charOrEsc '-' charOrEsc
4965 * [20] charOrEsc ::= XmlChar | SingleCharEsc
4966 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
4967 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
4970 xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt
) {
4976 ERROR("Expecting ']'");
4985 case 'n': start
= 0xA; break;
4986 case 'r': start
= 0xD; break;
4987 case 't': start
= 0x9; break;
4988 case '\\': case '|': case '.': case '-': case '^': case '?':
4989 case '*': case '+': case '{': case '}': case '(': case ')':
4993 ERROR("Invalid escape value");
4998 } else if ((cur
!= 0x5B) && (cur
!= 0x5D)) {
4999 end
= start
= CUR_SCHAR(ctxt
->cur
, len
);
5001 ERROR("Expecting a char range");
5005 * Since we are "inside" a range, we can assume ctxt->cur is past
5006 * the start of ctxt->string, and PREV should be safe
5008 if ((start
== '-') && (NXT(1) != ']') && (PREV
!= '[') && (PREV
!= '^')) {
5014 if ((cur
!= '-') || (NXT(1) == ']')) {
5015 xmlRegAtomAddRange(ctxt
, ctxt
->atom
, ctxt
->neg
,
5016 XML_REGEXP_CHARVAL
, start
, end
, NULL
);
5025 case 'n': end
= 0xA; break;
5026 case 'r': end
= 0xD; break;
5027 case 't': end
= 0x9; break;
5028 case '\\': case '|': case '.': case '-': case '^': case '?':
5029 case '*': case '+': case '{': case '}': case '(': case ')':
5033 ERROR("Invalid escape value");
5037 } else if ((cur
!= 0x5B) && (cur
!= 0x5D)) {
5038 end
= CUR_SCHAR(ctxt
->cur
, len
);
5040 ERROR("Expecting the end of a char range");
5044 /* TODO check that the values are acceptable character ranges for XML */
5046 ERROR("End of range is before start of range");
5048 xmlRegAtomAddRange(ctxt
, ctxt
->atom
, ctxt
->neg
,
5049 XML_REGEXP_CHARVAL
, start
, end
, NULL
);
5055 * xmlFAParsePosCharGroup:
5056 * @ctxt: a regexp parser context
5058 * [14] posCharGroup ::= ( charRange | charClassEsc )+
5061 xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt
) {
5064 xmlFAParseCharClassEsc(ctxt
);
5066 xmlFAParseCharRange(ctxt
);
5068 } while ((CUR
!= ']') && (CUR
!= '^') && (CUR
!= '-') &&
5069 (CUR
!= 0) && (ctxt
->error
== 0));
5073 * xmlFAParseCharGroup:
5074 * @ctxt: a regexp parser context
5076 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5077 * [15] negCharGroup ::= '^' posCharGroup
5078 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5079 * [12] charClassExpr ::= '[' charGroup ']'
5082 xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt
) {
5084 while ((CUR
!= ']') && (ctxt
->error
== 0)) {
5086 int neg
= ctxt
->neg
;
5089 ctxt
->neg
= !ctxt
->neg
;
5090 xmlFAParsePosCharGroup(ctxt
);
5092 } else if ((CUR
== '-') && (NXT(1) == '[')) {
5093 int neg
= ctxt
->neg
;
5095 NEXT
; /* eat the '-' */
5096 NEXT
; /* eat the '[' */
5097 xmlFAParseCharGroup(ctxt
);
5101 ERROR("charClassExpr: ']' expected");
5106 } else if (CUR
!= ']') {
5107 xmlFAParsePosCharGroup(ctxt
);
5114 * xmlFAParseCharClass:
5115 * @ctxt: a regexp parser context
5117 * [11] charClass ::= charClassEsc | charClassExpr
5118 * [12] charClassExpr ::= '[' charGroup ']'
5121 xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt
) {
5124 ctxt
->atom
= xmlRegNewAtom(ctxt
, XML_REGEXP_RANGES
);
5125 if (ctxt
->atom
== NULL
)
5127 xmlFAParseCharGroup(ctxt
);
5131 ERROR("xmlFAParseCharClass: ']' expected");
5134 xmlFAParseCharClassEsc(ctxt
);
5139 * xmlFAParseQuantExact:
5140 * @ctxt: a regexp parser context
5142 * [8] QuantExact ::= [0-9]+
5144 * Returns 0 if success or -1 in case of error
5147 xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt
) {
5151 while ((CUR
>= '0') && (CUR
<= '9')) {
5152 ret
= ret
* 10 + (CUR
- '0');
5163 * xmlFAParseQuantifier:
5164 * @ctxt: a regexp parser context
5166 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5167 * [5] quantity ::= quantRange | quantMin | QuantExact
5168 * [6] quantRange ::= QuantExact ',' QuantExact
5169 * [7] quantMin ::= QuantExact ','
5170 * [8] QuantExact ::= [0-9]+
5173 xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt
) {
5177 if ((cur
== '?') || (cur
== '*') || (cur
== '+')) {
5178 if (ctxt
->atom
!= NULL
) {
5180 ctxt
->atom
->quant
= XML_REGEXP_QUANT_OPT
;
5181 else if (cur
== '*')
5182 ctxt
->atom
->quant
= XML_REGEXP_QUANT_MULT
;
5183 else if (cur
== '+')
5184 ctxt
->atom
->quant
= XML_REGEXP_QUANT_PLUS
;
5190 int min
= 0, max
= 0;
5193 cur
= xmlFAParseQuantExact(ctxt
);
5201 cur
= xmlFAParseQuantExact(ctxt
);
5205 ERROR("Improper quantifier");
5212 ERROR("Unterminated quantifier");
5216 if (ctxt
->atom
!= NULL
) {
5217 ctxt
->atom
->quant
= XML_REGEXP_QUANT_RANGE
;
5218 ctxt
->atom
->min
= min
;
5219 ctxt
->atom
->max
= max
;
5228 * @ctxt: a regexp parser context
5230 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
5233 xmlFAParseAtom(xmlRegParserCtxtPtr ctxt
) {
5236 codepoint
= xmlFAIsChar(ctxt
);
5237 if (codepoint
> 0) {
5238 ctxt
->atom
= xmlRegNewAtom(ctxt
, XML_REGEXP_CHARVAL
);
5239 if (ctxt
->atom
== NULL
)
5241 codepoint
= CUR_SCHAR(ctxt
->cur
, len
);
5242 ctxt
->atom
->codepoint
= codepoint
;
5245 } else if (CUR
== '|') {
5247 } else if (CUR
== 0) {
5249 } else if (CUR
== ')') {
5251 } else if (CUR
== '(') {
5252 xmlRegStatePtr start
, oldend
, start0
;
5256 * this extra Epsilon transition is needed if we count with 0 allowed
5257 * unfortunately this can't be known at that point
5259 xmlFAGenerateEpsilonTransition(ctxt
, ctxt
->state
, NULL
);
5260 start0
= ctxt
->state
;
5261 xmlFAGenerateEpsilonTransition(ctxt
, ctxt
->state
, NULL
);
5262 start
= ctxt
->state
;
5266 xmlFAParseRegExp(ctxt
, 0);
5270 ERROR("xmlFAParseAtom: expecting ')'");
5272 ctxt
->atom
= xmlRegNewAtom(ctxt
, XML_REGEXP_SUBREG
);
5273 if (ctxt
->atom
== NULL
)
5275 ctxt
->atom
->start
= start
;
5276 ctxt
->atom
->start0
= start0
;
5277 ctxt
->atom
->stop
= ctxt
->state
;
5280 } else if ((CUR
== '[') || (CUR
== '\\') || (CUR
== '.')) {
5281 xmlFAParseCharClass(ctxt
);
5289 * @ctxt: a regexp parser context
5291 * [3] piece ::= atom quantifier?
5294 xmlFAParsePiece(xmlRegParserCtxtPtr ctxt
) {
5298 ret
= xmlFAParseAtom(ctxt
);
5301 if (ctxt
->atom
== NULL
) {
5302 ERROR("internal: no atom generated");
5304 xmlFAParseQuantifier(ctxt
);
5310 * @ctxt: a regexp parser context
5311 * @to: optional target to the end of the branch
5313 * @to is used to optimize by removing duplicate path in automata
5314 * in expressions like (a|b)(c|d)
5316 * [2] branch ::= piece*
5319 xmlFAParseBranch(xmlRegParserCtxtPtr ctxt
, xmlRegStatePtr to
) {
5320 xmlRegStatePtr previous
;
5323 previous
= ctxt
->state
;
5324 ret
= xmlFAParsePiece(ctxt
);
5326 if (xmlFAGenerateTransitions(ctxt
, previous
,
5327 (CUR
=='|' || CUR
==')') ? to
: NULL
, ctxt
->atom
) < 0)
5329 previous
= ctxt
->state
;
5332 while ((ret
!= 0) && (ctxt
->error
== 0)) {
5333 ret
= xmlFAParsePiece(ctxt
);
5335 if (xmlFAGenerateTransitions(ctxt
, previous
,
5336 (CUR
=='|' || CUR
==')') ? to
: NULL
, ctxt
->atom
) < 0)
5338 previous
= ctxt
->state
;
5347 * @ctxt: a regexp parser context
5348 * @top: is this the top-level expression ?
5350 * [1] regExp ::= branch ( '|' branch )*
5353 xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt
, int top
) {
5354 xmlRegStatePtr start
, end
;
5356 /* if not top start should have been generated by an epsilon trans */
5357 start
= ctxt
->state
;
5359 xmlFAParseBranch(ctxt
, NULL
);
5361 #ifdef DEBUG_REGEXP_GRAPH
5362 printf("State %d is final\n", ctxt
->state
->no
);
5364 ctxt
->state
->type
= XML_REGEXP_FINAL_STATE
;
5367 ctxt
->end
= ctxt
->state
;
5371 while ((CUR
== '|') && (ctxt
->error
== 0)) {
5373 ctxt
->state
= start
;
5375 xmlFAParseBranch(ctxt
, end
);
5383 /************************************************************************
5387 ************************************************************************/
5391 * @output: the file for the output debug
5392 * @regexp: the compiled regexp
5394 * Print the content of the compiled regular expression
5397 xmlRegexpPrint(FILE *output
, xmlRegexpPtr regexp
) {
5402 fprintf(output
, " regexp: ");
5403 if (regexp
== NULL
) {
5404 fprintf(output
, "NULL\n");
5407 fprintf(output
, "'%s' ", regexp
->string
);
5408 fprintf(output
, "\n");
5409 fprintf(output
, "%d atoms:\n", regexp
->nbAtoms
);
5410 for (i
= 0;i
< regexp
->nbAtoms
; i
++) {
5411 fprintf(output
, " %02d ", i
);
5412 xmlRegPrintAtom(output
, regexp
->atoms
[i
]);
5414 fprintf(output
, "%d states:", regexp
->nbStates
);
5415 fprintf(output
, "\n");
5416 for (i
= 0;i
< regexp
->nbStates
; i
++) {
5417 xmlRegPrintState(output
, regexp
->states
[i
]);
5419 fprintf(output
, "%d counters:\n", regexp
->nbCounters
);
5420 for (i
= 0;i
< regexp
->nbCounters
; i
++) {
5421 fprintf(output
, " %d: min %d max %d\n", i
, regexp
->counters
[i
].min
,
5422 regexp
->counters
[i
].max
);
5428 * @regexp: a regular expression string
5430 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
5431 * Appendix F and builds an automata suitable for testing strings against
5432 * that regular expression
5434 * Returns the compiled expression or NULL in case of error
5437 xmlRegexpCompile(const xmlChar
*regexp
) {
5439 xmlRegParserCtxtPtr ctxt
;
5441 ctxt
= xmlRegNewParserCtxt(regexp
);
5445 /* initialize the parser */
5447 ctxt
->start
= ctxt
->state
= xmlRegNewState(ctxt
);
5448 xmlRegStatePush(ctxt
, ctxt
->start
);
5450 /* parse the expression building an automata */
5451 xmlFAParseRegExp(ctxt
, 1);
5453 ERROR("xmlFAParseRegExp: extra characters");
5455 if (ctxt
->error
!= 0) {
5456 xmlRegFreeParserCtxt(ctxt
);
5459 ctxt
->end
= ctxt
->state
;
5460 ctxt
->start
->type
= XML_REGEXP_START_STATE
;
5461 ctxt
->end
->type
= XML_REGEXP_FINAL_STATE
;
5463 /* remove the Epsilon except for counted transitions */
5464 xmlFAEliminateEpsilonTransitions(ctxt
);
5467 if (ctxt
->error
!= 0) {
5468 xmlRegFreeParserCtxt(ctxt
);
5471 ret
= xmlRegEpxFromParse(ctxt
);
5472 xmlRegFreeParserCtxt(ctxt
);
5478 * @comp: the compiled regular expression
5479 * @content: the value to check against the regular expression
5481 * Check if the regular expression generates the value
5483 * Returns 1 if it matches, 0 if not and a negative value in case of error
5486 xmlRegexpExec(xmlRegexpPtr comp
, const xmlChar
*content
) {
5487 if ((comp
== NULL
) || (content
== NULL
))
5489 return(xmlFARegExec(comp
, content
));
5493 * xmlRegexpIsDeterminist:
5494 * @comp: the compiled regular expression
5496 * Check if the regular expression is determinist
5498 * Returns 1 if it yes, 0 if not and a negative value in case of error
5501 xmlRegexpIsDeterminist(xmlRegexpPtr comp
) {
5507 if (comp
->determinist
!= -1)
5508 return(comp
->determinist
);
5510 am
= xmlNewAutomata();
5511 if (am
->states
!= NULL
) {
5514 for (i
= 0;i
< am
->nbStates
;i
++)
5515 xmlRegFreeState(am
->states
[i
]);
5516 xmlFree(am
->states
);
5518 am
->nbAtoms
= comp
->nbAtoms
;
5519 am
->atoms
= comp
->atoms
;
5520 am
->nbStates
= comp
->nbStates
;
5521 am
->states
= comp
->states
;
5522 am
->determinist
= -1;
5523 am
->flags
= comp
->flags
;
5524 ret
= xmlFAComputesDeterminism(am
);
5527 xmlFreeAutomata(am
);
5528 comp
->determinist
= ret
;
5534 * @regexp: the regexp
5539 xmlRegFreeRegexp(xmlRegexpPtr regexp
) {
5544 if (regexp
->string
!= NULL
)
5545 xmlFree(regexp
->string
);
5546 if (regexp
->states
!= NULL
) {
5547 for (i
= 0;i
< regexp
->nbStates
;i
++)
5548 xmlRegFreeState(regexp
->states
[i
]);
5549 xmlFree(regexp
->states
);
5551 if (regexp
->atoms
!= NULL
) {
5552 for (i
= 0;i
< regexp
->nbAtoms
;i
++)
5553 xmlRegFreeAtom(regexp
->atoms
[i
]);
5554 xmlFree(regexp
->atoms
);
5556 if (regexp
->counters
!= NULL
)
5557 xmlFree(regexp
->counters
);
5558 if (regexp
->compact
!= NULL
)
5559 xmlFree(regexp
->compact
);
5560 if (regexp
->transdata
!= NULL
)
5561 xmlFree(regexp
->transdata
);
5562 if (regexp
->stringMap
!= NULL
) {
5563 for (i
= 0; i
< regexp
->nbstrings
;i
++)
5564 xmlFree(regexp
->stringMap
[i
]);
5565 xmlFree(regexp
->stringMap
);
5571 #ifdef LIBXML_AUTOMATA_ENABLED
5572 /************************************************************************
5574 * The Automata interface *
5576 ************************************************************************/
5581 * Create a new automata
5583 * Returns the new object or NULL in case of failure
5586 xmlNewAutomata(void) {
5587 xmlAutomataPtr ctxt
;
5589 ctxt
= xmlRegNewParserCtxt(NULL
);
5593 /* initialize the parser */
5595 ctxt
->start
= ctxt
->state
= xmlRegNewState(ctxt
);
5596 if (ctxt
->start
== NULL
) {
5597 xmlFreeAutomata(ctxt
);
5600 ctxt
->start
->type
= XML_REGEXP_START_STATE
;
5601 if (xmlRegStatePush(ctxt
, ctxt
->start
) < 0) {
5602 xmlRegFreeState(ctxt
->start
);
5603 xmlFreeAutomata(ctxt
);
5618 xmlFreeAutomata(xmlAutomataPtr am
) {
5621 xmlRegFreeParserCtxt(am
);
5625 * xmlAutomataSetFlags:
5627 * @flags: a set of internal flags
5629 * Set some flags on the automata
5632 xmlAutomataSetFlags(xmlAutomataPtr am
, int flags
) {
5639 * xmlAutomataGetInitState:
5642 * Initial state lookup
5644 * Returns the initial state of the automata
5647 xmlAutomataGetInitState(xmlAutomataPtr am
) {
5654 * xmlAutomataSetFinalState:
5656 * @state: a state in this automata
5658 * Makes that state a final state
5660 * Returns 0 or -1 in case of error
5663 xmlAutomataSetFinalState(xmlAutomataPtr am
, xmlAutomataStatePtr state
) {
5664 if ((am
== NULL
) || (state
== NULL
))
5666 state
->type
= XML_REGEXP_FINAL_STATE
;
5671 * xmlAutomataNewTransition:
5673 * @from: the starting point of the transition
5674 * @to: the target point of the transition or NULL
5675 * @token: the input string associated to that transition
5676 * @data: data passed to the callback function if the transition is activated
5678 * If @to is NULL, this creates first a new target state in the automata
5679 * and then adds a transition from the @from state to the target state
5680 * activated by the value of @token
5682 * Returns the target state or NULL in case of error
5685 xmlAutomataNewTransition(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
5686 xmlAutomataStatePtr to
, const xmlChar
*token
,
5690 if ((am
== NULL
) || (from
== NULL
) || (token
== NULL
))
5692 atom
= xmlRegNewAtom(am
, XML_REGEXP_STRING
);
5698 atom
->valuep
= xmlStrdup(token
);
5700 if (xmlFAGenerateTransitions(am
, from
, to
, atom
) < 0) {
5701 xmlRegFreeAtom(atom
);
5710 * xmlAutomataNewTransition2:
5712 * @from: the starting point of the transition
5713 * @to: the target point of the transition or NULL
5714 * @token: the first input string associated to that transition
5715 * @token2: the second input string associated to that transition
5716 * @data: data passed to the callback function if the transition is activated
5718 * If @to is NULL, this creates first a new target state in the automata
5719 * and then adds a transition from the @from state to the target state
5720 * activated by the value of @token
5722 * Returns the target state or NULL in case of error
5725 xmlAutomataNewTransition2(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
5726 xmlAutomataStatePtr to
, const xmlChar
*token
,
5727 const xmlChar
*token2
, void *data
) {
5730 if ((am
== NULL
) || (from
== NULL
) || (token
== NULL
))
5732 atom
= xmlRegNewAtom(am
, XML_REGEXP_STRING
);
5736 if ((token2
== NULL
) || (*token2
== 0)) {
5737 atom
->valuep
= xmlStrdup(token
);
5742 lenn
= strlen((char *) token2
);
5743 lenp
= strlen((char *) token
);
5745 str
= (xmlChar
*) xmlMallocAtomic(lenn
+ lenp
+ 2);
5747 xmlRegFreeAtom(atom
);
5750 memcpy(&str
[0], token
, lenp
);
5752 memcpy(&str
[lenp
+ 1], token2
, lenn
);
5753 str
[lenn
+ lenp
+ 1] = 0;
5758 if (xmlFAGenerateTransitions(am
, from
, to
, atom
) < 0) {
5759 xmlRegFreeAtom(atom
);
5768 * xmlAutomataNewNegTrans:
5770 * @from: the starting point of the transition
5771 * @to: the target point of the transition or NULL
5772 * @token: the first input string associated to that transition
5773 * @token2: the second input string associated to that transition
5774 * @data: data passed to the callback function if the transition is activated
5776 * If @to is NULL, this creates first a new target state in the automata
5777 * and then adds a transition from the @from state to the target state
5778 * activated by any value except (@token,@token2)
5779 * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5780 # the semantic of XSD ##other
5782 * Returns the target state or NULL in case of error
5785 xmlAutomataNewNegTrans(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
5786 xmlAutomataStatePtr to
, const xmlChar
*token
,
5787 const xmlChar
*token2
, void *data
) {
5789 xmlChar err_msg
[200];
5791 if ((am
== NULL
) || (from
== NULL
) || (token
== NULL
))
5793 atom
= xmlRegNewAtom(am
, XML_REGEXP_STRING
);
5798 if ((token2
== NULL
) || (*token2
== 0)) {
5799 atom
->valuep
= xmlStrdup(token
);
5804 lenn
= strlen((char *) token2
);
5805 lenp
= strlen((char *) token
);
5807 str
= (xmlChar
*) xmlMallocAtomic(lenn
+ lenp
+ 2);
5809 xmlRegFreeAtom(atom
);
5812 memcpy(&str
[0], token
, lenp
);
5814 memcpy(&str
[lenp
+ 1], token2
, lenn
);
5815 str
[lenn
+ lenp
+ 1] = 0;
5819 snprintf((char *) err_msg
, 199, "not %s", (const char *) atom
->valuep
);
5821 atom
->valuep2
= xmlStrdup(err_msg
);
5823 if (xmlFAGenerateTransitions(am
, from
, to
, atom
) < 0) {
5824 xmlRegFreeAtom(atom
);
5834 * xmlAutomataNewCountTrans2:
5836 * @from: the starting point of the transition
5837 * @to: the target point of the transition or NULL
5838 * @token: the input string associated to that transition
5839 * @token2: the second input string associated to that transition
5840 * @min: the minimum successive occurences of token
5841 * @max: the maximum successive occurences of token
5842 * @data: data associated to the transition
5844 * If @to is NULL, this creates first a new target state in the automata
5845 * and then adds a transition from the @from state to the target state
5846 * activated by a succession of input of value @token and @token2 and
5847 * whose number is between @min and @max
5849 * Returns the target state or NULL in case of error
5852 xmlAutomataNewCountTrans2(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
5853 xmlAutomataStatePtr to
, const xmlChar
*token
,
5854 const xmlChar
*token2
,
5855 int min
, int max
, void *data
) {
5859 if ((am
== NULL
) || (from
== NULL
) || (token
== NULL
))
5863 if ((max
< min
) || (max
< 1))
5865 atom
= xmlRegNewAtom(am
, XML_REGEXP_STRING
);
5868 if ((token2
== NULL
) || (*token2
== 0)) {
5869 atom
->valuep
= xmlStrdup(token
);
5874 lenn
= strlen((char *) token2
);
5875 lenp
= strlen((char *) token
);
5877 str
= (xmlChar
*) xmlMallocAtomic(lenn
+ lenp
+ 2);
5879 xmlRegFreeAtom(atom
);
5882 memcpy(&str
[0], token
, lenp
);
5884 memcpy(&str
[lenp
+ 1], token2
, lenn
);
5885 str
[lenn
+ lenp
+ 1] = 0;
5897 * associate a counter to the transition.
5899 counter
= xmlRegGetCounter(am
);
5900 am
->counters
[counter
].min
= min
;
5901 am
->counters
[counter
].max
= max
;
5903 /* xmlFAGenerateTransitions(am, from, to, atom); */
5905 to
= xmlRegNewState(am
);
5906 xmlRegStatePush(am
, to
);
5908 xmlRegStateAddTrans(am
, from
, atom
, to
, counter
, -1);
5909 xmlRegAtomPush(am
, atom
);
5917 xmlFAGenerateEpsilonTransition(am
, from
, to
);
5922 * xmlAutomataNewCountTrans:
5924 * @from: the starting point of the transition
5925 * @to: the target point of the transition or NULL
5926 * @token: the input string associated to that transition
5927 * @min: the minimum successive occurences of token
5928 * @max: the maximum successive occurences of token
5929 * @data: data associated to the transition
5931 * If @to is NULL, this creates first a new target state in the automata
5932 * and then adds a transition from the @from state to the target state
5933 * activated by a succession of input of value @token and whose number
5934 * is between @min and @max
5936 * Returns the target state or NULL in case of error
5939 xmlAutomataNewCountTrans(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
5940 xmlAutomataStatePtr to
, const xmlChar
*token
,
5941 int min
, int max
, void *data
) {
5945 if ((am
== NULL
) || (from
== NULL
) || (token
== NULL
))
5949 if ((max
< min
) || (max
< 1))
5951 atom
= xmlRegNewAtom(am
, XML_REGEXP_STRING
);
5954 atom
->valuep
= xmlStrdup(token
);
5963 * associate a counter to the transition.
5965 counter
= xmlRegGetCounter(am
);
5966 am
->counters
[counter
].min
= min
;
5967 am
->counters
[counter
].max
= max
;
5969 /* xmlFAGenerateTransitions(am, from, to, atom); */
5971 to
= xmlRegNewState(am
);
5972 xmlRegStatePush(am
, to
);
5974 xmlRegStateAddTrans(am
, from
, atom
, to
, counter
, -1);
5975 xmlRegAtomPush(am
, atom
);
5983 xmlFAGenerateEpsilonTransition(am
, from
, to
);
5988 * xmlAutomataNewOnceTrans2:
5990 * @from: the starting point of the transition
5991 * @to: the target point of the transition or NULL
5992 * @token: the input string associated to that transition
5993 * @token2: the second input string associated to that transition
5994 * @min: the minimum successive occurences of token
5995 * @max: the maximum successive occurences of token
5996 * @data: data associated to the transition
5998 * If @to is NULL, this creates first a new target state in the automata
5999 * and then adds a transition from the @from state to the target state
6000 * activated by a succession of input of value @token and @token2 and whose
6001 * number is between @min and @max, moreover that transition can only be
6004 * Returns the target state or NULL in case of error
6007 xmlAutomataNewOnceTrans2(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
6008 xmlAutomataStatePtr to
, const xmlChar
*token
,
6009 const xmlChar
*token2
,
6010 int min
, int max
, void *data
) {
6014 if ((am
== NULL
) || (from
== NULL
) || (token
== NULL
))
6018 if ((max
< min
) || (max
< 1))
6020 atom
= xmlRegNewAtom(am
, XML_REGEXP_STRING
);
6023 if ((token2
== NULL
) || (*token2
== 0)) {
6024 atom
->valuep
= xmlStrdup(token
);
6029 lenn
= strlen((char *) token2
);
6030 lenp
= strlen((char *) token
);
6032 str
= (xmlChar
*) xmlMallocAtomic(lenn
+ lenp
+ 2);
6034 xmlRegFreeAtom(atom
);
6037 memcpy(&str
[0], token
, lenp
);
6039 memcpy(&str
[lenp
+ 1], token2
, lenn
);
6040 str
[lenn
+ lenp
+ 1] = 0;
6045 atom
->quant
= XML_REGEXP_QUANT_ONCEONLY
;
6049 * associate a counter to the transition.
6051 counter
= xmlRegGetCounter(am
);
6052 am
->counters
[counter
].min
= 1;
6053 am
->counters
[counter
].max
= 1;
6055 /* xmlFAGenerateTransitions(am, from, to, atom); */
6057 to
= xmlRegNewState(am
);
6058 xmlRegStatePush(am
, to
);
6060 xmlRegStateAddTrans(am
, from
, atom
, to
, counter
, -1);
6061 xmlRegAtomPush(am
, atom
);
6069 * xmlAutomataNewOnceTrans:
6071 * @from: the starting point of the transition
6072 * @to: the target point of the transition or NULL
6073 * @token: the input string associated to that transition
6074 * @min: the minimum successive occurences of token
6075 * @max: the maximum successive occurences of token
6076 * @data: data associated to the transition
6078 * If @to is NULL, this creates first a new target state in the automata
6079 * and then adds a transition from the @from state to the target state
6080 * activated by a succession of input of value @token and whose number
6081 * is between @min and @max, moreover that transition can only be crossed
6084 * Returns the target state or NULL in case of error
6087 xmlAutomataNewOnceTrans(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
6088 xmlAutomataStatePtr to
, const xmlChar
*token
,
6089 int min
, int max
, void *data
) {
6093 if ((am
== NULL
) || (from
== NULL
) || (token
== NULL
))
6097 if ((max
< min
) || (max
< 1))
6099 atom
= xmlRegNewAtom(am
, XML_REGEXP_STRING
);
6102 atom
->valuep
= xmlStrdup(token
);
6104 atom
->quant
= XML_REGEXP_QUANT_ONCEONLY
;
6108 * associate a counter to the transition.
6110 counter
= xmlRegGetCounter(am
);
6111 am
->counters
[counter
].min
= 1;
6112 am
->counters
[counter
].max
= 1;
6114 /* xmlFAGenerateTransitions(am, from, to, atom); */
6116 to
= xmlRegNewState(am
);
6117 xmlRegStatePush(am
, to
);
6119 xmlRegStateAddTrans(am
, from
, atom
, to
, counter
, -1);
6120 xmlRegAtomPush(am
, atom
);
6126 * xmlAutomataNewState:
6129 * Create a new disconnected state in the automata
6131 * Returns the new state or NULL in case of error
6134 xmlAutomataNewState(xmlAutomataPtr am
) {
6135 xmlAutomataStatePtr to
;
6139 to
= xmlRegNewState(am
);
6140 xmlRegStatePush(am
, to
);
6145 * xmlAutomataNewEpsilon:
6147 * @from: the starting point of the transition
6148 * @to: the target point of the transition or NULL
6150 * If @to is NULL, this creates first a new target state in the automata
6151 * and then adds an epsilon transition from the @from state to the
6154 * Returns the target state or NULL in case of error
6157 xmlAutomataNewEpsilon(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
6158 xmlAutomataStatePtr to
) {
6159 if ((am
== NULL
) || (from
== NULL
))
6161 xmlFAGenerateEpsilonTransition(am
, from
, to
);
6168 * xmlAutomataNewAllTrans:
6170 * @from: the starting point of the transition
6171 * @to: the target point of the transition or NULL
6172 * @lax: allow to transition if not all all transitions have been activated
6174 * If @to is NULL, this creates first a new target state in the automata
6175 * and then adds a an ALL transition from the @from state to the
6176 * target state. That transition is an epsilon transition allowed only when
6177 * all transitions from the @from node have been activated.
6179 * Returns the target state or NULL in case of error
6182 xmlAutomataNewAllTrans(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
6183 xmlAutomataStatePtr to
, int lax
) {
6184 if ((am
== NULL
) || (from
== NULL
))
6186 xmlFAGenerateAllTransition(am
, from
, to
, lax
);
6193 * xmlAutomataNewCounter:
6195 * @min: the minimal value on the counter
6196 * @max: the maximal value on the counter
6198 * Create a new counter
6200 * Returns the counter number or -1 in case of error
6203 xmlAutomataNewCounter(xmlAutomataPtr am
, int min
, int max
) {
6209 ret
= xmlRegGetCounter(am
);
6212 am
->counters
[ret
].min
= min
;
6213 am
->counters
[ret
].max
= max
;
6218 * xmlAutomataNewCountedTrans:
6220 * @from: the starting point of the transition
6221 * @to: the target point of the transition or NULL
6222 * @counter: the counter associated to that transition
6224 * If @to is NULL, this creates first a new target state in the automata
6225 * and then adds an epsilon transition from the @from state to the target state
6226 * which will increment the counter provided
6228 * Returns the target state or NULL in case of error
6231 xmlAutomataNewCountedTrans(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
6232 xmlAutomataStatePtr to
, int counter
) {
6233 if ((am
== NULL
) || (from
== NULL
) || (counter
< 0))
6235 xmlFAGenerateCountedEpsilonTransition(am
, from
, to
, counter
);
6242 * xmlAutomataNewCounterTrans:
6244 * @from: the starting point of the transition
6245 * @to: the target point of the transition or NULL
6246 * @counter: the counter associated to that transition
6248 * If @to is NULL, this creates first a new target state in the automata
6249 * and then adds an epsilon transition from the @from state to the target state
6250 * which will be allowed only if the counter is within the right range.
6252 * Returns the target state or NULL in case of error
6255 xmlAutomataNewCounterTrans(xmlAutomataPtr am
, xmlAutomataStatePtr from
,
6256 xmlAutomataStatePtr to
, int counter
) {
6257 if ((am
== NULL
) || (from
== NULL
) || (counter
< 0))
6259 xmlFAGenerateCountedTransition(am
, from
, to
, counter
);
6266 * xmlAutomataCompile:
6269 * Compile the automata into a Reg Exp ready for being executed.
6270 * The automata should be free after this point.
6272 * Returns the compiled regexp or NULL in case of error
6275 xmlAutomataCompile(xmlAutomataPtr am
) {
6278 if ((am
== NULL
) || (am
->error
!= 0)) return(NULL
);
6279 xmlFAEliminateEpsilonTransitions(am
);
6280 /* xmlFAComputesDeterminism(am); */
6281 ret
= xmlRegEpxFromParse(am
);
6287 * xmlAutomataIsDeterminist:
6290 * Checks if an automata is determinist.
6292 * Returns 1 if true, 0 if not, and -1 in case of error
6295 xmlAutomataIsDeterminist(xmlAutomataPtr am
) {
6301 ret
= xmlFAComputesDeterminism(am
);
6304 #endif /* LIBXML_AUTOMATA_ENABLED */
6306 #ifdef LIBXML_EXPR_ENABLED
6307 /************************************************************************
6309 * Formal Expression handling code *
6311 ************************************************************************/
6312 /************************************************************************
6314 * Expression handling context *
6316 ************************************************************************/
6318 struct _xmlExpCtxt
{
6320 xmlExpNodePtr
*table
;
6333 * @maxNodes: the maximum number of nodes
6334 * @dict: optional dictionnary to use internally
6336 * Creates a new context for manipulating expressions
6338 * Returns the context or NULL in case of error
6341 xmlExpNewCtxt(int maxNodes
, xmlDictPtr dict
) {
6345 if (maxNodes
<= 4096)
6348 ret
= (xmlExpCtxtPtr
) xmlMalloc(sizeof(xmlExpCtxt
));
6351 memset(ret
, 0, sizeof(xmlExpCtxt
));
6354 ret
->maxNodes
= maxNodes
;
6355 ret
->table
= xmlMalloc(size
* sizeof(xmlExpNodePtr
));
6356 if (ret
->table
== NULL
) {
6360 memset(ret
->table
, 0, size
* sizeof(xmlExpNodePtr
));
6362 ret
->dict
= xmlDictCreate();
6363 if (ret
->dict
== NULL
) {
6364 xmlFree(ret
->table
);
6370 xmlDictReference(ret
->dict
);
6377 * @ctxt: an expression context
6379 * Free an expression context
6382 xmlExpFreeCtxt(xmlExpCtxtPtr ctxt
) {
6385 xmlDictFree(ctxt
->dict
);
6386 if (ctxt
->table
!= NULL
)
6387 xmlFree(ctxt
->table
);
6391 /************************************************************************
6393 * Structure associated to an expression node *
6395 ************************************************************************/
6396 #define MAX_NODES 10000
6398 /* #define DEBUG_DERIV */
6403 * - public API for creation
6406 * - regression testing
6409 * - split into module and test tool
6414 XML_EXP_NILABLE
= (1 << 0)
6417 #define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6419 struct _xmlExpNode
{
6420 unsigned char type
;/* xmlExpNodeType */
6421 unsigned char info
;/* OR of xmlExpNodeInfo */
6422 unsigned short key
; /* the hash key */
6423 unsigned int ref
; /* The number of references */
6424 int c_max
; /* the maximum length it can consume */
6425 xmlExpNodePtr exp_left
;
6426 xmlExpNodePtr next
;/* the next node in the hash table or free list */
6433 xmlExpNodePtr f_right
;
6435 const xmlChar
*f_str
;
6439 #define exp_min field.count.f_min
6440 #define exp_max field.count.f_max
6441 /* #define exp_left field.children.f_left */
6442 #define exp_right field.children.f_right
6443 #define exp_str field.f_str
6445 static xmlExpNodePtr
xmlExpNewNode(xmlExpCtxtPtr ctxt
, xmlExpNodeType type
);
6446 static xmlExpNode forbiddenExpNode
= {
6447 XML_EXP_FORBID
, 0, 0, 0, 0, NULL
, NULL
, {{ 0, 0}}
6449 xmlExpNodePtr forbiddenExp
= &forbiddenExpNode
;
6450 static xmlExpNode emptyExpNode
= {
6451 XML_EXP_EMPTY
, 1, 0, 0, 0, NULL
, NULL
, {{ 0, 0}}
6453 xmlExpNodePtr emptyExp
= &emptyExpNode
;
6455 /************************************************************************
6457 * The custom hash table for unicity and canonicalization *
6458 * of sub-expressions pointers *
6460 ************************************************************************/
6462 * xmlExpHashNameComputeKey:
6463 * Calculate the hash key for a token
6465 static unsigned short
6466 xmlExpHashNameComputeKey(const xmlChar
*name
) {
6467 unsigned short value
= 0L;
6471 value
+= 30 * (*name
);
6472 while ((ch
= *name
++) != 0) {
6473 value
= value
^ ((value
<< 5) + (value
>> 3) + (unsigned short)ch
);
6480 * xmlExpHashComputeKey:
6481 * Calculate the hash key for a compound expression
6483 static unsigned short
6484 xmlExpHashComputeKey(xmlExpNodeType type
, xmlExpNodePtr left
,
6485 xmlExpNodePtr right
) {
6486 unsigned long value
;
6492 value
+= right
->key
;
6494 ret
= (unsigned short) value
;
6498 value
+= right
->key
;
6500 ret
= (unsigned short) value
;
6504 value
+= right
->key
;
6505 ret
= (unsigned short) value
;
6514 static xmlExpNodePtr
6515 xmlExpNewNode(xmlExpCtxtPtr ctxt
, xmlExpNodeType type
) {
6518 if (ctxt
->nb_nodes
>= MAX_NODES
)
6520 ret
= (xmlExpNodePtr
) xmlMalloc(sizeof(xmlExpNode
));
6523 memset(ret
, 0, sizeof(xmlExpNode
));
6532 * xmlExpHashGetEntry:
6533 * @table: the hash table
6535 * Get the unique entry from the hash table. The entry is created if
6536 * needed. @left and @right are consumed, i.e. their ref count will
6537 * be decremented by the operation.
6539 * Returns the pointer or NULL in case of error
6541 static xmlExpNodePtr
6542 xmlExpHashGetEntry(xmlExpCtxtPtr ctxt
, xmlExpNodeType type
,
6543 xmlExpNodePtr left
, xmlExpNodePtr right
,
6544 const xmlChar
*name
, int min
, int max
) {
6545 unsigned short kbase
, key
;
6546 xmlExpNodePtr entry
;
6547 xmlExpNodePtr insert
;
6553 * Check for duplicate and insertion location.
6555 if (type
== XML_EXP_ATOM
) {
6556 kbase
= xmlExpHashNameComputeKey(name
);
6557 } else if (type
== XML_EXP_COUNT
) {
6558 /* COUNT reduction rule 1 */
6565 xmlExpFree(ctxt
, left
);
6570 xmlExpFree(ctxt
, left
);
6571 return(forbiddenExp
);
6578 } else if (type
== XML_EXP_OR
) {
6579 /* Forbid reduction rules */
6580 if (left
->type
== XML_EXP_FORBID
) {
6581 xmlExpFree(ctxt
, left
);
6584 if (right
->type
== XML_EXP_FORBID
) {
6585 xmlExpFree(ctxt
, right
);
6589 /* OR reduction rule 1 */
6590 /* a | a reduced to a */
6591 if (left
== right
) {
6595 /* OR canonicalization rule 1 */
6596 /* linearize (a | b) | c into a | (b | c) */
6597 if ((left
->type
== XML_EXP_OR
) && (right
->type
!= XML_EXP_OR
)) {
6598 xmlExpNodePtr tmp
= left
;
6602 /* OR reduction rule 2 */
6603 /* a | (a | b) and b | (a | b) are reduced to a | b */
6604 if (right
->type
== XML_EXP_OR
) {
6605 if ((left
== right
->exp_left
) ||
6606 (left
== right
->exp_right
)) {
6607 xmlExpFree(ctxt
, left
);
6611 /* OR canonicalization rule 2 */
6612 /* linearize (a | b) | c into a | (b | c) */
6613 if (left
->type
== XML_EXP_OR
) {
6616 /* OR canonicalization rule 2 */
6617 if ((left
->exp_right
->type
!= XML_EXP_OR
) &&
6618 (left
->exp_right
->key
< left
->exp_left
->key
)) {
6619 tmp
= left
->exp_right
;
6620 left
->exp_right
= left
->exp_left
;
6621 left
->exp_left
= tmp
;
6623 left
->exp_right
->ref
++;
6624 tmp
= xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, left
->exp_right
, right
,
6626 left
->exp_left
->ref
++;
6627 tmp
= xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, left
->exp_left
, tmp
,
6630 xmlExpFree(ctxt
, left
);
6633 if (right
->type
== XML_EXP_OR
) {
6634 /* Ordering in the tree */
6635 /* C | (A | B) -> A | (B | C) */
6636 if (left
->key
> right
->exp_right
->key
) {
6638 right
->exp_right
->ref
++;
6639 tmp
= xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, right
->exp_right
,
6641 right
->exp_left
->ref
++;
6642 tmp
= xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, right
->exp_left
,
6644 xmlExpFree(ctxt
, right
);
6647 /* Ordering in the tree */
6648 /* B | (A | C) -> A | (B | C) */
6649 if (left
->key
> right
->exp_left
->key
) {
6651 right
->exp_right
->ref
++;
6652 tmp
= xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, left
,
6653 right
->exp_right
, NULL
, 0, 0);
6654 right
->exp_left
->ref
++;
6655 tmp
= xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, right
->exp_left
,
6657 xmlExpFree(ctxt
, right
);
6661 /* we know both types are != XML_EXP_OR here */
6662 else if (left
->key
> right
->key
) {
6663 xmlExpNodePtr tmp
= left
;
6667 kbase
= xmlExpHashComputeKey(type
, left
, right
);
6668 } else if (type
== XML_EXP_SEQ
) {
6669 /* Forbid reduction rules */
6670 if (left
->type
== XML_EXP_FORBID
) {
6671 xmlExpFree(ctxt
, right
);
6674 if (right
->type
== XML_EXP_FORBID
) {
6675 xmlExpFree(ctxt
, left
);
6678 /* Empty reduction rules */
6679 if (right
->type
== XML_EXP_EMPTY
) {
6682 if (left
->type
== XML_EXP_EMPTY
) {
6685 kbase
= xmlExpHashComputeKey(type
, left
, right
);
6689 key
= kbase
% ctxt
->size
;
6690 if (ctxt
->table
[key
] != NULL
) {
6691 for (insert
= ctxt
->table
[key
]; insert
!= NULL
;
6692 insert
= insert
->next
) {
6693 if ((insert
->key
== kbase
) &&
6694 (insert
->type
== type
)) {
6695 if (type
== XML_EXP_ATOM
) {
6696 if (name
== insert
->exp_str
) {
6700 } else if (type
== XML_EXP_COUNT
) {
6701 if ((insert
->exp_min
== min
) && (insert
->exp_max
== max
) &&
6702 (insert
->exp_left
== left
)) {
6707 } else if ((insert
->exp_left
== left
) &&
6708 (insert
->exp_right
== right
)) {
6718 entry
= xmlExpNewNode(ctxt
, type
);
6722 if (type
== XML_EXP_ATOM
) {
6723 entry
->exp_str
= name
;
6725 } else if (type
== XML_EXP_COUNT
) {
6726 entry
->exp_min
= min
;
6727 entry
->exp_max
= max
;
6728 entry
->exp_left
= left
;
6729 if ((min
== 0) || (IS_NILLABLE(left
)))
6730 entry
->info
|= XML_EXP_NILABLE
;
6734 entry
->c_max
= max
* entry
->exp_left
->c_max
;
6736 entry
->exp_left
= left
;
6737 entry
->exp_right
= right
;
6738 if (type
== XML_EXP_OR
) {
6739 if ((IS_NILLABLE(left
)) || (IS_NILLABLE(right
)))
6740 entry
->info
|= XML_EXP_NILABLE
;
6741 if ((entry
->exp_left
->c_max
== -1) ||
6742 (entry
->exp_right
->c_max
== -1))
6744 else if (entry
->exp_left
->c_max
> entry
->exp_right
->c_max
)
6745 entry
->c_max
= entry
->exp_left
->c_max
;
6747 entry
->c_max
= entry
->exp_right
->c_max
;
6749 if ((IS_NILLABLE(left
)) && (IS_NILLABLE(right
)))
6750 entry
->info
|= XML_EXP_NILABLE
;
6751 if ((entry
->exp_left
->c_max
== -1) ||
6752 (entry
->exp_right
->c_max
== -1))
6755 entry
->c_max
= entry
->exp_left
->c_max
+ entry
->exp_right
->c_max
;
6759 if (ctxt
->table
[key
] != NULL
)
6760 entry
->next
= ctxt
->table
[key
];
6762 ctxt
->table
[key
] = entry
;
6770 * @ctxt: the expression context
6771 * @exp: the expression
6773 * Dereference the expression
6776 xmlExpFree(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
) {
6777 if ((exp
== NULL
) || (exp
== forbiddenExp
) || (exp
== emptyExp
))
6780 if (exp
->ref
== 0) {
6783 /* Unlink it first from the hash table */
6784 key
= exp
->key
% ctxt
->size
;
6785 if (ctxt
->table
[key
] == exp
) {
6786 ctxt
->table
[key
] = exp
->next
;
6790 tmp
= ctxt
->table
[key
];
6791 while (tmp
!= NULL
) {
6792 if (tmp
->next
== exp
) {
6793 tmp
->next
= exp
->next
;
6800 if ((exp
->type
== XML_EXP_SEQ
) || (exp
->type
== XML_EXP_OR
)) {
6801 xmlExpFree(ctxt
, exp
->exp_left
);
6802 xmlExpFree(ctxt
, exp
->exp_right
);
6803 } else if (exp
->type
== XML_EXP_COUNT
) {
6804 xmlExpFree(ctxt
, exp
->exp_left
);
6813 * @exp: the expression
6815 * Increase the reference count of the expression
6818 xmlExpRef(xmlExpNodePtr exp
) {
6825 * @ctxt: the expression context
6826 * @name: the atom name
6827 * @len: the atom name lenght in byte (or -1);
6829 * Get the atom associated to this name from that context
6831 * Returns the node or NULL in case of error
6834 xmlExpNewAtom(xmlExpCtxtPtr ctxt
, const xmlChar
*name
, int len
) {
6835 if ((ctxt
== NULL
) || (name
== NULL
))
6837 name
= xmlDictLookup(ctxt
->dict
, name
, len
);
6840 return(xmlExpHashGetEntry(ctxt
, XML_EXP_ATOM
, NULL
, NULL
, name
, 0, 0));
6845 * @ctxt: the expression context
6846 * @left: left expression
6847 * @right: right expression
6849 * Get the atom associated to the choice @left | @right
6850 * Note that @left and @right are consumed in the operation, to keep
6851 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6852 * this is true even in case of failure (unless ctxt == NULL).
6854 * Returns the node or NULL in case of error
6857 xmlExpNewOr(xmlExpCtxtPtr ctxt
, xmlExpNodePtr left
, xmlExpNodePtr right
) {
6860 if ((left
== NULL
) || (right
== NULL
)) {
6861 xmlExpFree(ctxt
, left
);
6862 xmlExpFree(ctxt
, right
);
6865 return(xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, left
, right
, NULL
, 0, 0));
6870 * @ctxt: the expression context
6871 * @left: left expression
6872 * @right: right expression
6874 * Get the atom associated to the sequence @left , @right
6875 * Note that @left and @right are consumed in the operation, to keep
6876 * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6877 * this is true even in case of failure (unless ctxt == NULL).
6879 * Returns the node or NULL in case of error
6882 xmlExpNewSeq(xmlExpCtxtPtr ctxt
, xmlExpNodePtr left
, xmlExpNodePtr right
) {
6885 if ((left
== NULL
) || (right
== NULL
)) {
6886 xmlExpFree(ctxt
, left
);
6887 xmlExpFree(ctxt
, right
);
6890 return(xmlExpHashGetEntry(ctxt
, XML_EXP_SEQ
, left
, right
, NULL
, 0, 0));
6895 * @ctxt: the expression context
6896 * @subset: the expression to be repeated
6897 * @min: the lower bound for the repetition
6898 * @max: the upper bound for the repetition, -1 means infinite
6900 * Get the atom associated to the range (@subset){@min, @max}
6901 * Note that @subset is consumed in the operation, to keep
6902 * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6903 * this is true even in case of failure (unless ctxt == NULL).
6905 * Returns the node or NULL in case of error
6908 xmlExpNewRange(xmlExpCtxtPtr ctxt
, xmlExpNodePtr subset
, int min
, int max
) {
6911 if ((subset
== NULL
) || (min
< 0) || (max
< -1) ||
6912 ((max
>= 0) && (min
> max
))) {
6913 xmlExpFree(ctxt
, subset
);
6916 return(xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
, subset
,
6917 NULL
, NULL
, min
, max
));
6920 /************************************************************************
6922 * Public API for operations on expressions *
6924 ************************************************************************/
6927 xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
,
6928 const xmlChar
**list
, int len
, int nb
) {
6931 switch (exp
->type
) {
6935 for (tmp
= 0;tmp
< nb
;tmp
++)
6936 if (list
[tmp
] == exp
->exp_str
)
6940 list
[nb
] = exp
->exp_str
;
6943 exp
= exp
->exp_left
;
6947 tmp
= xmlExpGetLanguageInt(ctxt
, exp
->exp_left
, list
, len
, nb
);
6950 tmp2
= xmlExpGetLanguageInt(ctxt
, exp
->exp_right
, list
, len
,
6960 * xmlExpGetLanguage:
6961 * @ctxt: the expression context
6962 * @exp: the expression
6963 * @langList: where to store the tokens
6964 * @len: the allocated lenght of @list
6966 * Find all the strings used in @exp and store them in @list
6968 * Returns the number of unique strings found, -1 in case of errors and
6969 * -2 if there is more than @len strings
6972 xmlExpGetLanguage(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
,
6973 const xmlChar
**langList
, int len
) {
6974 if ((ctxt
== NULL
) || (exp
== NULL
) || (langList
== NULL
) || (len
<= 0))
6976 return(xmlExpGetLanguageInt(ctxt
, exp
, langList
, len
, 0));
6980 xmlExpGetStartInt(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
,
6981 const xmlChar
**list
, int len
, int nb
) {
6984 switch (exp
->type
) {
6985 case XML_EXP_FORBID
:
6990 for (tmp
= 0;tmp
< nb
;tmp
++)
6991 if (list
[tmp
] == exp
->exp_str
)
6995 list
[nb
] = exp
->exp_str
;
6998 exp
= exp
->exp_left
;
7001 tmp
= xmlExpGetStartInt(ctxt
, exp
->exp_left
, list
, len
, nb
);
7004 if (IS_NILLABLE(exp
->exp_left
)) {
7005 tmp2
= xmlExpGetStartInt(ctxt
, exp
->exp_right
, list
, len
,
7013 tmp
= xmlExpGetStartInt(ctxt
, exp
->exp_left
, list
, len
, nb
);
7016 tmp2
= xmlExpGetStartInt(ctxt
, exp
->exp_right
, list
, len
,
7027 * @ctxt: the expression context
7028 * @exp: the expression
7029 * @tokList: where to store the tokens
7030 * @len: the allocated lenght of @list
7032 * Find all the strings that appears at the start of the languages
7033 * accepted by @exp and store them in @list. E.g. for (a, b) | c
7034 * it will return the list [a, c]
7036 * Returns the number of unique strings found, -1 in case of errors and
7037 * -2 if there is more than @len strings
7040 xmlExpGetStart(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
,
7041 const xmlChar
**tokList
, int len
) {
7042 if ((ctxt
== NULL
) || (exp
== NULL
) || (tokList
== NULL
) || (len
<= 0))
7044 return(xmlExpGetStartInt(ctxt
, exp
, tokList
, len
, 0));
7049 * @exp: the expression
7051 * Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
7053 * Returns 1 if nillable, 0 if not and -1 in case of error
7056 xmlExpIsNillable(xmlExpNodePtr exp
) {
7059 return(IS_NILLABLE(exp
) != 0);
7062 static xmlExpNodePtr
7063 xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
, const xmlChar
*str
)
7067 switch (exp
->type
) {
7069 return(forbiddenExp
);
7070 case XML_EXP_FORBID
:
7071 return(forbiddenExp
);
7073 if (exp
->exp_str
== str
) {
7075 printf("deriv atom: equal => Empty\n");
7080 printf("deriv atom: mismatch => forbid\n");
7082 /* TODO wildcards here */
7090 printf("deriv or: => or(derivs)\n");
7092 tmp
= xmlExpStringDeriveInt(ctxt
, exp
->exp_left
, str
);
7096 ret
= xmlExpStringDeriveInt(ctxt
, exp
->exp_right
, str
);
7098 xmlExpFree(ctxt
, tmp
);
7101 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, tmp
, ret
,
7107 printf("deriv seq: starting with left\n");
7109 ret
= xmlExpStringDeriveInt(ctxt
, exp
->exp_left
, str
);
7112 } else if (ret
== forbiddenExp
) {
7113 if (IS_NILLABLE(exp
->exp_left
)) {
7115 printf("deriv seq: left failed but nillable\n");
7117 ret
= xmlExpStringDeriveInt(ctxt
, exp
->exp_right
, str
);
7121 printf("deriv seq: left match => sequence\n");
7123 exp
->exp_right
->ref
++;
7124 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_SEQ
, ret
, exp
->exp_right
,
7128 case XML_EXP_COUNT
: {
7132 if (exp
->exp_max
== 0)
7133 return(forbiddenExp
);
7134 ret
= xmlExpStringDeriveInt(ctxt
, exp
->exp_left
, str
);
7137 if (ret
== forbiddenExp
) {
7139 printf("deriv count: pattern mismatch => forbid\n");
7143 if (exp
->exp_max
== 1)
7145 if (exp
->exp_max
< 0) /* unbounded */
7148 max
= exp
->exp_max
- 1;
7149 if (exp
->exp_min
> 0)
7150 min
= exp
->exp_min
- 1;
7153 exp
->exp_left
->ref
++;
7154 tmp
= xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
, exp
->exp_left
, NULL
,
7156 if (ret
== emptyExp
) {
7158 printf("deriv count: match to empty => new count\n");
7163 printf("deriv count: match => sequence with new count\n");
7165 return(xmlExpHashGetEntry(ctxt
, XML_EXP_SEQ
, ret
, tmp
,
7173 * xmlExpStringDerive:
7174 * @ctxt: the expression context
7175 * @exp: the expression
7177 * @len: the string len in bytes if available
7179 * Do one step of Brzozowski derivation of the expression @exp with
7180 * respect to the input string
7182 * Returns the resulting expression or NULL in case of internal error
7185 xmlExpStringDerive(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
,
7186 const xmlChar
*str
, int len
) {
7187 const xmlChar
*input
;
7189 if ((exp
== NULL
) || (ctxt
== NULL
) || (str
== NULL
)) {
7193 * check the string is in the dictionnary, if yes use an interned
7194 * copy, otherwise we know it's not an acceptable input
7196 input
= xmlDictExists(ctxt
->dict
, str
, len
);
7197 if (input
== NULL
) {
7198 return(forbiddenExp
);
7200 return(xmlExpStringDeriveInt(ctxt
, exp
, input
));
7204 xmlExpCheckCard(xmlExpNodePtr exp
, xmlExpNodePtr sub
) {
7207 if (sub
->c_max
== -1) {
7208 if (exp
->c_max
!= -1)
7210 } else if ((exp
->c_max
>= 0) && (exp
->c_max
< sub
->c_max
)) {
7214 if ((IS_NILLABLE(sub
)) && (!IS_NILLABLE(exp
)))
7220 static xmlExpNodePtr
xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
,
7224 * @ctxt: the expressions context
7225 * @exp: the englobing expression
7226 * @sub: the subexpression
7227 * @mult: the multiple expression
7228 * @remain: the remain from the derivation of the multiple
7230 * Check if exp is a multiple of sub, i.e. if there is a finite number n
7231 * so that sub{n} subsume exp
7233 * Returns the multiple value if successful, 0 if it is not a multiple
7234 * and -1 in case of internel error.
7238 xmlExpDivide(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
, xmlExpNodePtr sub
,
7239 xmlExpNodePtr
*mult
, xmlExpNodePtr
*remain
) {
7241 xmlExpNodePtr tmp
, tmp2
;
7243 if (mult
!= NULL
) *mult
= NULL
;
7244 if (remain
!= NULL
) *remain
= NULL
;
7245 if (exp
->c_max
== -1) return(0);
7246 if (IS_NILLABLE(exp
) && (!IS_NILLABLE(sub
))) return(0);
7248 for (i
= 1;i
<= exp
->c_max
;i
++) {
7250 tmp
= xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
,
7251 sub
, NULL
, NULL
, i
, i
);
7255 if (!xmlExpCheckCard(tmp
, exp
)) {
7256 xmlExpFree(ctxt
, tmp
);
7259 tmp2
= xmlExpExpDeriveInt(ctxt
, tmp
, exp
);
7261 xmlExpFree(ctxt
, tmp
);
7264 if ((tmp2
!= forbiddenExp
) && (IS_NILLABLE(tmp2
))) {
7268 xmlExpFree(ctxt
, tmp2
);
7272 xmlExpFree(ctxt
, tmp
);
7274 printf("Divide succeeded %d\n", i
);
7278 xmlExpFree(ctxt
, tmp
);
7279 xmlExpFree(ctxt
, tmp2
);
7282 printf("Divide failed\n");
7288 * xmlExpExpDeriveInt:
7289 * @ctxt: the expressions context
7290 * @exp: the englobing expression
7291 * @sub: the subexpression
7293 * Try to do a step of Brzozowski derivation but at a higher level
7294 * the input being a subexpression.
7296 * Returns the resulting expression or NULL in case of internal error
7298 static xmlExpNodePtr
7299 xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
, xmlExpNodePtr sub
) {
7300 xmlExpNodePtr ret
, tmp
, tmp2
, tmp3
;
7301 const xmlChar
**tab
;
7305 * In case of equality and if the expression can only consume a finite
7306 * amount, then the derivation is empty
7308 if ((exp
== sub
) && (exp
->c_max
>= 0)) {
7310 printf("Equal(exp, sub) and finite -> Empty\n");
7315 * decompose sub sequence first
7317 if (sub
->type
== XML_EXP_EMPTY
) {
7319 printf("Empty(sub) -> Empty\n");
7324 if (sub
->type
== XML_EXP_SEQ
) {
7326 printf("Seq(sub) -> decompose\n");
7328 tmp
= xmlExpExpDeriveInt(ctxt
, exp
, sub
->exp_left
);
7331 if (tmp
== forbiddenExp
)
7333 ret
= xmlExpExpDeriveInt(ctxt
, tmp
, sub
->exp_right
);
7334 xmlExpFree(ctxt
, tmp
);
7337 if (sub
->type
== XML_EXP_OR
) {
7339 printf("Or(sub) -> decompose\n");
7341 tmp
= xmlExpExpDeriveInt(ctxt
, exp
, sub
->exp_left
);
7342 if (tmp
== forbiddenExp
)
7346 ret
= xmlExpExpDeriveInt(ctxt
, exp
, sub
->exp_right
);
7347 if ((ret
== NULL
) || (ret
== forbiddenExp
)) {
7348 xmlExpFree(ctxt
, tmp
);
7351 return(xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, tmp
, ret
, NULL
, 0, 0));
7353 if (!xmlExpCheckCard(exp
, sub
)) {
7355 printf("CheckCard(exp, sub) failed -> Forbid\n");
7357 return(forbiddenExp
);
7359 switch (exp
->type
) {
7361 if (sub
== emptyExp
)
7364 printf("Empty(exp) -> Forbid\n");
7366 return(forbiddenExp
);
7367 case XML_EXP_FORBID
:
7369 printf("Forbid(exp) -> Forbid\n");
7371 return(forbiddenExp
);
7373 if (sub
->type
== XML_EXP_ATOM
) {
7374 /* TODO: handle wildcards */
7375 if (exp
->exp_str
== sub
->exp_str
) {
7377 printf("Atom match -> Empty\n");
7382 printf("Atom mismatch -> Forbid\n");
7384 return(forbiddenExp
);
7386 if ((sub
->type
== XML_EXP_COUNT
) &&
7387 (sub
->exp_max
== 1) &&
7388 (sub
->exp_left
->type
== XML_EXP_ATOM
)) {
7389 /* TODO: handle wildcards */
7390 if (exp
->exp_str
== sub
->exp_left
->exp_str
) {
7392 printf("Atom match -> Empty\n");
7397 printf("Atom mismatch -> Forbid\n");
7399 return(forbiddenExp
);
7402 printf("Compex exp vs Atom -> Forbid\n");
7404 return(forbiddenExp
);
7406 /* try to get the sequence consumed only if possible */
7407 if (xmlExpCheckCard(exp
->exp_left
, sub
)) {
7408 /* See if the sequence can be consumed directly */
7410 printf("Seq trying left only\n");
7412 ret
= xmlExpExpDeriveInt(ctxt
, exp
->exp_left
, sub
);
7413 if ((ret
!= forbiddenExp
) && (ret
!= NULL
)) {
7415 printf("Seq trying left only worked\n");
7418 * TODO: assumption here that we are determinist
7419 * i.e. we won't get to a nillable exp left
7420 * subset which could be matched by the right
7422 * e.g.: (a | b)+,(a | c) and 'a+,a'
7424 exp
->exp_right
->ref
++;
7425 return(xmlExpHashGetEntry(ctxt
, XML_EXP_SEQ
, ret
,
7426 exp
->exp_right
, NULL
, 0, 0));
7430 printf("Seq: left too short\n");
7433 /* Try instead to decompose */
7434 if (sub
->type
== XML_EXP_COUNT
) {
7438 printf("Seq: sub is a count\n");
7440 ret
= xmlExpExpDeriveInt(ctxt
, exp
->exp_left
, sub
->exp_left
);
7443 if (ret
!= forbiddenExp
) {
7445 printf("Seq , Count match on left\n");
7447 if (sub
->exp_max
< 0)
7450 max
= sub
->exp_max
-1;
7451 if (sub
->exp_min
> 0)
7452 min
= sub
->exp_min
-1;
7455 exp
->exp_right
->ref
++;
7456 tmp
= xmlExpHashGetEntry(ctxt
, XML_EXP_SEQ
, ret
,
7457 exp
->exp_right
, NULL
, 0, 0);
7461 sub
->exp_left
->ref
++;
7462 tmp2
= xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
,
7463 sub
->exp_left
, NULL
, NULL
, min
, max
);
7465 xmlExpFree(ctxt
, tmp
);
7468 ret
= xmlExpExpDeriveInt(ctxt
, tmp
, tmp2
);
7469 xmlExpFree(ctxt
, tmp
);
7470 xmlExpFree(ctxt
, tmp2
);
7474 /* we made no progress on structured operations */
7478 printf("Or , trying both side\n");
7480 ret
= xmlExpExpDeriveInt(ctxt
, exp
->exp_left
, sub
);
7483 tmp
= xmlExpExpDeriveInt(ctxt
, exp
->exp_right
, sub
);
7485 xmlExpFree(ctxt
, ret
);
7488 return(xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, ret
, tmp
, NULL
, 0, 0));
7489 case XML_EXP_COUNT
: {
7492 if (sub
->type
== XML_EXP_COUNT
) {
7494 * Try to see if the loop is completely subsumed
7496 tmp
= xmlExpExpDeriveInt(ctxt
, exp
->exp_left
, sub
->exp_left
);
7499 if (tmp
== forbiddenExp
) {
7503 printf("Count, Count inner don't subsume\n");
7505 mult
= xmlExpDivide(ctxt
, sub
->exp_left
, exp
->exp_left
,
7509 printf("Count, Count not multiple => forbidden\n");
7511 return(forbiddenExp
);
7513 if (sub
->exp_max
== -1) {
7515 if (exp
->exp_max
== -1) {
7516 if (exp
->exp_min
<= sub
->exp_min
* mult
)
7519 min
= exp
->exp_min
- sub
->exp_min
* mult
;
7522 printf("Count, Count finite can't subsume infinite\n");
7524 xmlExpFree(ctxt
, tmp
);
7525 return(forbiddenExp
);
7528 if (exp
->exp_max
== -1) {
7530 printf("Infinite loop consume mult finite loop\n");
7532 if (exp
->exp_min
> sub
->exp_min
* mult
) {
7534 min
= exp
->exp_min
- sub
->exp_min
* mult
;
7540 if (exp
->exp_max
< sub
->exp_max
* mult
) {
7542 printf("loops max mult mismatch => forbidden\n");
7544 xmlExpFree(ctxt
, tmp
);
7545 return(forbiddenExp
);
7547 if (sub
->exp_max
* mult
> exp
->exp_min
)
7550 min
= exp
->exp_min
- sub
->exp_max
* mult
;
7551 max
= exp
->exp_max
- sub
->exp_max
* mult
;
7554 } else if (!IS_NILLABLE(tmp
)) {
7556 * TODO: loop here to try to grow if working on finite
7560 printf("Count, Count remain not nillable => forbidden\n");
7562 xmlExpFree(ctxt
, tmp
);
7563 return(forbiddenExp
);
7564 } else if (sub
->exp_max
== -1) {
7565 if (exp
->exp_max
== -1) {
7566 if (exp
->exp_min
<= sub
->exp_min
) {
7568 printf("Infinite loops Okay => COUNT(0,Inf)\n");
7574 printf("Infinite loops min => Count(X,Inf)\n");
7577 min
= exp
->exp_min
- sub
->exp_min
;
7579 } else if (exp
->exp_min
> sub
->exp_min
) {
7581 printf("loops min mismatch 1 => forbidden ???\n");
7583 xmlExpFree(ctxt
, tmp
);
7584 return(forbiddenExp
);
7590 if (exp
->exp_max
== -1) {
7592 printf("Infinite loop consume finite loop\n");
7594 if (exp
->exp_min
> sub
->exp_min
) {
7596 min
= exp
->exp_min
- sub
->exp_min
;
7602 if (exp
->exp_max
< sub
->exp_max
) {
7604 printf("loops max mismatch => forbidden\n");
7606 xmlExpFree(ctxt
, tmp
);
7607 return(forbiddenExp
);
7609 if (sub
->exp_max
> exp
->exp_min
)
7612 min
= exp
->exp_min
- sub
->exp_max
;
7613 max
= exp
->exp_max
- sub
->exp_max
;
7617 printf("loops match => SEQ(COUNT())\n");
7619 exp
->exp_left
->ref
++;
7620 tmp2
= xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
, exp
->exp_left
,
7621 NULL
, NULL
, min
, max
);
7625 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_SEQ
, tmp
, tmp2
,
7629 tmp
= xmlExpExpDeriveInt(ctxt
, exp
->exp_left
, sub
);
7632 if (tmp
== forbiddenExp
) {
7634 printf("loop mismatch => forbidden\n");
7636 return(forbiddenExp
);
7638 if (exp
->exp_min
> 0)
7639 min
= exp
->exp_min
- 1;
7642 if (exp
->exp_max
< 0)
7645 max
= exp
->exp_max
- 1;
7648 printf("loop match => SEQ(COUNT())\n");
7650 exp
->exp_left
->ref
++;
7651 tmp2
= xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
, exp
->exp_left
,
7652 NULL
, NULL
, min
, max
);
7655 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_SEQ
, tmp
, tmp2
,
7662 printf("Fallback to derivative\n");
7664 if (IS_NILLABLE(sub
)) {
7665 if (!(IS_NILLABLE(exp
)))
7666 return(forbiddenExp
);
7672 * here the structured derivation made no progress so
7673 * we use the default token based derivation to force one more step
7675 if (ctxt
->tabSize
== 0)
7678 tab
= (const xmlChar
**) xmlMalloc(ctxt
->tabSize
*
7679 sizeof(const xmlChar
*));
7685 * collect all the strings accepted by the subexpression on input
7687 len
= xmlExpGetStartInt(ctxt
, sub
, tab
, ctxt
->tabSize
, 0);
7689 const xmlChar
**temp
;
7690 temp
= (const xmlChar
**) xmlRealloc((xmlChar
**) tab
, ctxt
->tabSize
* 2 *
7691 sizeof(const xmlChar
*));
7693 xmlFree((xmlChar
**) tab
);
7698 len
= xmlExpGetStartInt(ctxt
, sub
, tab
, ctxt
->tabSize
, 0);
7700 for (i
= 0;i
< len
;i
++) {
7701 tmp
= xmlExpStringDeriveInt(ctxt
, exp
, tab
[i
]);
7702 if ((tmp
== NULL
) || (tmp
== forbiddenExp
)) {
7703 xmlExpFree(ctxt
, ret
);
7704 xmlFree((xmlChar
**) tab
);
7707 tmp2
= xmlExpStringDeriveInt(ctxt
, sub
, tab
[i
]);
7708 if ((tmp2
== NULL
) || (tmp2
== forbiddenExp
)) {
7709 xmlExpFree(ctxt
, tmp
);
7710 xmlExpFree(ctxt
, ret
);
7711 xmlFree((xmlChar
**) tab
);
7714 tmp3
= xmlExpExpDeriveInt(ctxt
, tmp
, tmp2
);
7715 xmlExpFree(ctxt
, tmp
);
7716 xmlExpFree(ctxt
, tmp2
);
7718 if ((tmp3
== NULL
) || (tmp3
== forbiddenExp
)) {
7719 xmlExpFree(ctxt
, ret
);
7720 xmlFree((xmlChar
**) tab
);
7727 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, ret
, tmp3
, NULL
, 0, 0);
7729 xmlFree((xmlChar
**) tab
);
7734 xmlFree((xmlChar
**) tab
);
7740 * @ctxt: the expressions context
7741 * @exp: the englobing expression
7742 * @sub: the subexpression
7744 * Evaluates the expression resulting from @exp consuming a sub expression @sub
7745 * Based on algebraic derivation and sometimes direct Brzozowski derivation
7746 * it usually tatkes less than linear time and can handle expressions generating
7747 * infinite languages.
7749 * Returns the resulting expression or NULL in case of internal error, the
7750 * result must be freed
7753 xmlExpExpDerive(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
, xmlExpNodePtr sub
) {
7754 if ((exp
== NULL
) || (ctxt
== NULL
) || (sub
== NULL
))
7760 if (IS_NILLABLE(sub
) && (!IS_NILLABLE(exp
))) {
7762 printf("Sub nillable and not exp : can't subsume\n");
7764 return(forbiddenExp
);
7766 if (xmlExpCheckCard(exp
, sub
) == 0) {
7768 printf("sub generate longuer sequances than exp : can't subsume\n");
7770 return(forbiddenExp
);
7772 return(xmlExpExpDeriveInt(ctxt
, exp
, sub
));
7777 * @ctxt: the expressions context
7778 * @exp: the englobing expression
7779 * @sub: the subexpression
7781 * Check whether @exp accepts all the languages accexpted by @sub
7782 * the input being a subexpression.
7784 * Returns 1 if true 0 if false and -1 in case of failure.
7787 xmlExpSubsume(xmlExpCtxtPtr ctxt
, xmlExpNodePtr exp
, xmlExpNodePtr sub
) {
7790 if ((exp
== NULL
) || (ctxt
== NULL
) || (sub
== NULL
))
7794 * TODO: speedup by checking the language of sub is a subset of the
7800 if (IS_NILLABLE(sub
) && (!IS_NILLABLE(exp
))) {
7802 printf("Sub nillable and not exp : can't subsume\n");
7806 if (xmlExpCheckCard(exp
, sub
) == 0) {
7808 printf("sub generate longuer sequances than exp : can't subsume\n");
7812 tmp
= xmlExpExpDeriveInt(ctxt
, exp
, sub
);
7814 printf("Result derivation :\n");
7819 if (tmp
== forbiddenExp
)
7821 if (tmp
== emptyExp
)
7823 if ((tmp
!= NULL
) && (IS_NILLABLE(tmp
))) {
7824 xmlExpFree(ctxt
, tmp
);
7827 xmlExpFree(ctxt
, tmp
);
7831 /************************************************************************
7833 * Parsing expression *
7835 ************************************************************************/
7837 static xmlExpNodePtr
xmlExpParseExpr(xmlExpCtxtPtr ctxt
);
7840 #define CUR (*ctxt->cur)
7842 #define NEXT ctxt->cur++;
7844 #define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7845 #define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7848 xmlExpParseNumber(xmlExpCtxtPtr ctxt
) {
7856 if ((CUR
< '0') || (CUR
> '9'))
7858 while ((CUR
>= '0') && (CUR
<= '9')) {
7859 ret
= ret
* 10 + (CUR
- '0');
7865 static xmlExpNodePtr
7866 xmlExpParseOr(xmlExpCtxtPtr ctxt
) {
7873 if (*ctxt
->cur
== '(') {
7875 ret
= xmlExpParseExpr(ctxt
);
7877 if (*ctxt
->cur
!= ')') {
7878 fprintf(stderr
, "unbalanced '(' : %s\n", base
);
7879 xmlExpFree(ctxt
, ret
);
7884 goto parse_quantifier
;
7886 while ((CUR
!= 0) && (!(IS_BLANK(CUR
))) && (CUR
!= '(') &&
7887 (CUR
!= ')') && (CUR
!= '|') && (CUR
!= ',') && (CUR
!= '{') &&
7888 (CUR
!= '*') && (CUR
!= '+') && (CUR
!= '?') && (CUR
!= '}'))
7890 val
= xmlDictLookup(ctxt
->dict
, BAD_CAST base
, ctxt
->cur
- base
);
7893 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_ATOM
, NULL
, NULL
, val
, 0, 0);
7902 min
= xmlExpParseNumber(ctxt
);
7904 xmlExpFree(ctxt
, ret
);
7910 max
= xmlExpParseNumber(ctxt
);
7915 xmlExpFree(ctxt
, ret
);
7919 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
, ret
, NULL
, NULL
,
7922 } else if (CUR
== '?') {
7924 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
, ret
, NULL
, NULL
,
7927 } else if (CUR
== '+') {
7929 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
, ret
, NULL
, NULL
,
7932 } else if (CUR
== '*') {
7934 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_COUNT
, ret
, NULL
, NULL
,
7942 static xmlExpNodePtr
7943 xmlExpParseSeq(xmlExpCtxtPtr ctxt
) {
7944 xmlExpNodePtr ret
, right
;
7946 ret
= xmlExpParseOr(ctxt
);
7948 while (CUR
== '|') {
7950 right
= xmlExpParseOr(ctxt
);
7951 if (right
== NULL
) {
7952 xmlExpFree(ctxt
, ret
);
7955 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_OR
, ret
, right
, NULL
, 0, 0);
7962 static xmlExpNodePtr
7963 xmlExpParseExpr(xmlExpCtxtPtr ctxt
) {
7964 xmlExpNodePtr ret
, right
;
7966 ret
= xmlExpParseSeq(ctxt
);
7968 while (CUR
== ',') {
7970 right
= xmlExpParseSeq(ctxt
);
7971 if (right
== NULL
) {
7972 xmlExpFree(ctxt
, ret
);
7975 ret
= xmlExpHashGetEntry(ctxt
, XML_EXP_SEQ
, ret
, right
, NULL
, 0, 0);
7984 * @ctxt: the expressions context
7985 * @expr: the 0 terminated string
7987 * Minimal parser for regexps, it understand the following constructs
7988 * - string terminals
7989 * - choice operator |
7990 * - sequence operator ,
7991 * - subexpressions (...)
7992 * - usual cardinality operators + * and ?
7993 * - finite sequences { min, max }
7994 * - infinite sequences { min, * }
7995 * There is minimal checkings made especially no checking on strings values
7997 * Returns a new expression or NULL in case of failure
8000 xmlExpParse(xmlExpCtxtPtr ctxt
, const char *expr
) {
8006 ret
= xmlExpParseExpr(ctxt
);
8008 if (*ctxt
->cur
!= 0) {
8009 xmlExpFree(ctxt
, ret
);
8016 xmlExpDumpInt(xmlBufferPtr buf
, xmlExpNodePtr expr
, int glob
) {
8019 if (expr
== NULL
) return;
8020 if (glob
) xmlBufferWriteChar(buf
, "(");
8021 switch (expr
->type
) {
8023 xmlBufferWriteChar(buf
, "empty");
8025 case XML_EXP_FORBID
:
8026 xmlBufferWriteChar(buf
, "forbidden");
8029 xmlBufferWriteCHAR(buf
, expr
->exp_str
);
8033 if ((c
->type
== XML_EXP_SEQ
) || (c
->type
== XML_EXP_OR
))
8034 xmlExpDumpInt(buf
, c
, 1);
8036 xmlExpDumpInt(buf
, c
, 0);
8037 xmlBufferWriteChar(buf
, " , ");
8038 c
= expr
->exp_right
;
8039 if ((c
->type
== XML_EXP_SEQ
) || (c
->type
== XML_EXP_OR
))
8040 xmlExpDumpInt(buf
, c
, 1);
8042 xmlExpDumpInt(buf
, c
, 0);
8046 if ((c
->type
== XML_EXP_SEQ
) || (c
->type
== XML_EXP_OR
))
8047 xmlExpDumpInt(buf
, c
, 1);
8049 xmlExpDumpInt(buf
, c
, 0);
8050 xmlBufferWriteChar(buf
, " | ");
8051 c
= expr
->exp_right
;
8052 if ((c
->type
== XML_EXP_SEQ
) || (c
->type
== XML_EXP_OR
))
8053 xmlExpDumpInt(buf
, c
, 1);
8055 xmlExpDumpInt(buf
, c
, 0);
8057 case XML_EXP_COUNT
: {
8061 if ((c
->type
== XML_EXP_SEQ
) || (c
->type
== XML_EXP_OR
))
8062 xmlExpDumpInt(buf
, c
, 1);
8064 xmlExpDumpInt(buf
, c
, 0);
8065 if ((expr
->exp_min
== 0) && (expr
->exp_max
== 1)) {
8068 } else if ((expr
->exp_min
== 0) && (expr
->exp_max
== -1)) {
8071 } else if ((expr
->exp_min
== 1) && (expr
->exp_max
== -1)) {
8074 } else if (expr
->exp_max
== expr
->exp_min
) {
8075 snprintf(rep
, 39, "{%d}", expr
->exp_min
);
8076 } else if (expr
->exp_max
< 0) {
8077 snprintf(rep
, 39, "{%d,inf}", expr
->exp_min
);
8079 snprintf(rep
, 39, "{%d,%d}", expr
->exp_min
, expr
->exp_max
);
8082 xmlBufferWriteChar(buf
, rep
);
8086 fprintf(stderr
, "Error in tree\n");
8089 xmlBufferWriteChar(buf
, ")");
8093 * @buf: a buffer to receive the output
8094 * @expr: the compiled expression
8096 * Serialize the expression as compiled to the buffer
8099 xmlExpDump(xmlBufferPtr buf
, xmlExpNodePtr expr
) {
8100 if ((buf
== NULL
) || (expr
== NULL
))
8102 xmlExpDumpInt(buf
, expr
, 0);
8107 * @expr: a compiled expression
8109 * Indicate the maximum number of input a expression can accept
8111 * Returns the maximum length or -1 in case of error
8114 xmlExpMaxToken(xmlExpNodePtr expr
) {
8117 return(expr
->c_max
);
8121 * xmlExpCtxtNbNodes:
8122 * @ctxt: an expression context
8124 * Debugging facility provides the number of allocated nodes at a that point
8126 * Returns the number of nodes in use or -1 in case of error
8129 xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt
) {
8132 return(ctxt
->nb_nodes
);
8137 * @ctxt: an expression context
8139 * Debugging facility provides the number of allocated nodes over lifetime
8141 * Returns the number of nodes ever allocated or -1 in case of error
8144 xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt
) {
8147 return(ctxt
->nb_cons
);
8150 #endif /* LIBXML_EXPR_ENABLED */
8151 #define bottom_xmlregexp
8152 #include "elfgcchack.h"
8153 #endif /* LIBXML_REGEXP_ENABLED */