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(id) (ctxt->cur[id])
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
struct _xmlAutomata {
243
xmlRegStatePtr start;
245
xmlRegStatePtr state;
251
xmlRegAtomPtr *atoms;
255
xmlRegStatePtr *states;
259
xmlRegCounter *counters;
268
xmlRegStatePtr *states;
270
xmlRegAtomPtr *atoms;
272
xmlRegCounter *counters;
275
* That's the compact form for determinists automatas
284
typedef struct _xmlRegExecRollback xmlRegExecRollback;
285
typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
287
struct _xmlRegExecRollback {
288
xmlRegStatePtr state;/* the current state */
289
int index; /* the index in the input stack */
290
int nextbranch; /* the next transition to explore in that state */
291
int *counts; /* save the automata state if it has some */
294
typedef struct _xmlRegInputToken xmlRegInputToken;
295
typedef xmlRegInputToken *xmlRegInputTokenPtr;
297
struct _xmlRegInputToken {
302
struct _xmlRegExecCtxt {
303
int status; /* execution status != 0 indicate an error */
304
int determinist; /* did we find an indeterministic behaviour */
305
xmlRegexpPtr comp; /* the compiled regexp */
306
xmlRegExecCallbacks callback;
309
xmlRegStatePtr state;/* the current state */
310
int transno; /* the current transition on that state */
311
int transcount; /* the number of chars in char counted transitions */
314
* A stack of rollback states
318
xmlRegExecRollback *rollbacks;
321
* The state of the automata if any
332
const xmlChar *inputString; /* when operating on characters */
333
xmlRegInputTokenPtr inputStack;/* when operating on strings */
338
int errStateNo; /* the error state number */
339
xmlRegStatePtr errState; /* the error state */
340
xmlChar *errString; /* the string raising the error */
341
int *errCounts; /* counters at the error state */
345
#define REGEXP_ALL_COUNTER 0x123456
346
#define REGEXP_ALL_LAX_COUNTER 0x123457
348
static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
349
static void xmlRegFreeState(xmlRegStatePtr state);
350
static void xmlRegFreeAtom(xmlRegAtomPtr atom);
351
static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
352
static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
353
static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
354
int neg, int start, int end, const xmlChar *blockName);
356
/************************************************************************
358
* Regexp memory error handler *
360
************************************************************************/
362
* xmlRegexpErrMemory:
363
* @extra: extra information
365
* Handle an out of memory condition
368
xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
370
const char *regexp = NULL;
372
regexp = (const char *) ctxt->string;
373
ctxt->error = XML_ERR_NO_MEMORY;
375
__xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
376
XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
378
"Memory allocation failed : %s\n", extra);
382
* xmlRegexpErrCompile:
383
* @extra: extra information
385
* Handle a compilation failure
388
xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
390
const char *regexp = NULL;
394
regexp = (const char *) ctxt->string;
395
idx = ctxt->cur - ctxt->string;
396
ctxt->error = XML_REGEXP_COMPILE_ERROR;
398
__xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
399
XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
400
regexp, NULL, idx, 0,
401
"failed to compile: %s\n", extra);
404
/************************************************************************
406
* Allocation/Deallocation *
408
************************************************************************/
410
static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
412
* xmlRegEpxFromParse:
413
* @ctxt: the parser context used to build it
415
* Allocate a new regexp and fill it with the result from the parser
417
* Returns the new regexp or NULL in case of error
420
xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
423
ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
425
xmlRegexpErrMemory(ctxt, "compiling regexp");
428
memset(ret, 0, sizeof(xmlRegexp));
429
ret->string = ctxt->string;
430
ret->nbStates = ctxt->nbStates;
431
ret->states = ctxt->states;
432
ret->nbAtoms = ctxt->nbAtoms;
433
ret->atoms = ctxt->atoms;
434
ret->nbCounters = ctxt->nbCounters;
435
ret->counters = ctxt->counters;
436
ret->determinist = ctxt->determinist;
437
if (ret->determinist == -1) {
438
xmlRegexpIsDeterminist(ret);
441
if ((ret->determinist != 0) &&
442
(ret->nbCounters == 0) &&
444
(ret->atoms != NULL) &&
445
(ret->atoms[0] != NULL) &&
446
(ret->atoms[0]->type == XML_REGEXP_STRING)) {
447
int i, j, nbstates = 0, nbatoms = 0;
456
* Switch to a compact representation
457
* 1/ counting the effective number of states left
458
* 2/ counting the unique number of atoms, and check that
459
* they are all of the string type
460
* 3/ build a table state x atom for the transitions
463
stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
464
if (stateRemap == NULL) {
465
xmlRegexpErrMemory(ctxt, "compiling regexp");
469
for (i = 0;i < ret->nbStates;i++) {
470
if (ret->states[i] != NULL) {
471
stateRemap[i] = nbstates;
477
#ifdef DEBUG_COMPACTION
478
printf("Final: %d states\n", nbstates);
480
stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
481
if (stringMap == NULL) {
482
xmlRegexpErrMemory(ctxt, "compiling regexp");
487
stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
488
if (stringRemap == NULL) {
489
xmlRegexpErrMemory(ctxt, "compiling regexp");
495
for (i = 0;i < ret->nbAtoms;i++) {
496
if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
497
(ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
498
value = ret->atoms[i]->valuep;
499
for (j = 0;j < nbatoms;j++) {
500
if (xmlStrEqual(stringMap[j], value)) {
506
stringRemap[i] = nbatoms;
507
stringMap[nbatoms] = xmlStrdup(value);
508
if (stringMap[nbatoms] == NULL) {
509
for (i = 0;i < nbatoms;i++)
510
xmlFree(stringMap[i]);
511
xmlFree(stringRemap);
521
xmlFree(stringRemap);
522
for (i = 0;i < nbatoms;i++)
523
xmlFree(stringMap[i]);
529
#ifdef DEBUG_COMPACTION
530
printf("Final: %d atoms\n", nbatoms);
532
transitions = (int *) xmlMalloc((nbstates + 1) *
533
(nbatoms + 1) * sizeof(int));
534
if (transitions == NULL) {
536
xmlFree(stringRemap);
541
memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
544
* Allocate the transition table. The first entry for each
545
* state corresponds to the state type.
549
for (i = 0;i < ret->nbStates;i++) {
550
int stateno, atomno, targetno, prev;
551
xmlRegStatePtr state;
552
xmlRegTransPtr trans;
554
stateno = stateRemap[i];
557
state = ret->states[i];
559
transitions[stateno * (nbatoms + 1)] = state->type;
561
for (j = 0;j < state->nbTrans;j++) {
562
trans = &(state->trans[j]);
563
if ((trans->to == -1) || (trans->atom == NULL))
565
atomno = stringRemap[trans->atom->no];
566
if ((trans->atom->data != NULL) && (transdata == NULL)) {
567
transdata = (void **) xmlMalloc(nbstates * nbatoms *
569
if (transdata != NULL)
571
nbstates * nbatoms * sizeof(void *));
573
xmlRegexpErrMemory(ctxt, "compiling regexp");
577
targetno = stateRemap[trans->to];
579
* if the same atom can generate transitions to 2 different
580
* states then it means the automata is not determinist and
581
* the compact form can't be used !
583
prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
585
if (prev != targetno + 1) {
586
ret->determinist = 0;
587
#ifdef DEBUG_COMPACTION
588
printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
589
i, j, trans->atom->no, trans->to, atomno, targetno);
590
printf(" previous to is %d\n", prev);
592
if (transdata != NULL)
594
xmlFree(transitions);
596
xmlFree(stringRemap);
597
for (i = 0;i < nbatoms;i++)
598
xmlFree(stringMap[i]);
604
printf("State %d trans %d: atom %d to %d : %d to %d\n",
605
i, j, trans->atom->no, trans->to, atomno, targetno);
607
transitions[stateno * (nbatoms + 1) + atomno + 1] =
608
targetno + 1; /* to avoid 0 */
609
if (transdata != NULL)
610
transdata[stateno * nbatoms + atomno] =
615
ret->determinist = 1;
616
#ifdef DEBUG_COMPACTION
620
for (i = 0;i < nbstates;i++) {
621
for (j = 0;j < nbatoms + 1;j++) {
622
printf("%02d ", transitions[i * (nbatoms + 1) + j]);
629
* Cleanup of the old data
631
if (ret->states != NULL) {
632
for (i = 0;i < ret->nbStates;i++)
633
xmlRegFreeState(ret->states[i]);
634
xmlFree(ret->states);
638
if (ret->atoms != NULL) {
639
for (i = 0;i < ret->nbAtoms;i++)
640
xmlRegFreeAtom(ret->atoms[i]);
646
ret->compact = transitions;
647
ret->transdata = transdata;
648
ret->stringMap = stringMap;
649
ret->nbstrings = nbatoms;
650
ret->nbstates = nbstates;
652
xmlFree(stringRemap);
660
ctxt->nbCounters = 0;
661
ctxt->counters = NULL;
666
* xmlRegNewParserCtxt:
667
* @string: the string to parse
669
* Allocate a new regexp parser context
671
* Returns the new context or NULL in case of error
673
static xmlRegParserCtxtPtr
674
xmlRegNewParserCtxt(const xmlChar *string) {
675
xmlRegParserCtxtPtr ret;
677
ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
680
memset(ret, 0, sizeof(xmlRegParserCtxt));
682
ret->string = xmlStrdup(string);
683
ret->cur = ret->string;
687
ret->determinist = -1;
693
* @ctxt: the regexp parser context
694
* @neg: is that negative
695
* @type: the type of range
696
* @start: the start codepoint
697
* @end: the end codepoint
699
* Allocate a new regexp range
701
* Returns the new range or NULL in case of error
703
static xmlRegRangePtr
704
xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
705
int neg, xmlRegAtomType type, int start, int end) {
708
ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
710
xmlRegexpErrMemory(ctxt, "allocating range");
722
* @range: the regexp range
724
* Free a regexp range
727
xmlRegFreeRange(xmlRegRangePtr range) {
731
if (range->blockName != NULL)
732
xmlFree(range->blockName);
738
* @range: the regexp range
740
* Copy a regexp range
742
* Returns the new copy or NULL in case of error.
744
static xmlRegRangePtr
745
xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
751
ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
755
if (range->blockName != NULL) {
756
ret->blockName = xmlStrdup(range->blockName);
757
if (ret->blockName == NULL) {
758
xmlRegexpErrMemory(ctxt, "allocating range");
759
xmlRegFreeRange(ret);
768
* @ctxt: the regexp parser context
769
* @type: the type of atom
771
* Allocate a new atom
773
* Returns the new atom or NULL in case of error
776
xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
779
ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
781
xmlRegexpErrMemory(ctxt, "allocating atom");
784
memset(ret, 0, sizeof(xmlRegAtom));
786
ret->quant = XML_REGEXP_QUANT_ONCE;
794
* @atom: the regexp atom
799
xmlRegFreeAtom(xmlRegAtomPtr atom) {
805
for (i = 0;i < atom->nbRanges;i++)
806
xmlRegFreeRange(atom->ranges[i]);
807
if (atom->ranges != NULL)
808
xmlFree(atom->ranges);
809
if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
810
xmlFree(atom->valuep);
811
if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
812
xmlFree(atom->valuep2);
813
if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
814
xmlFree(atom->valuep);
820
* @ctxt: the regexp parser context
821
* @atom: the oiginal atom
823
* Allocate a new regexp range
825
* Returns the new atom or NULL in case of error
828
xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
831
ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
833
xmlRegexpErrMemory(ctxt, "copying atom");
836
memset(ret, 0, sizeof(xmlRegAtom));
837
ret->type = atom->type;
838
ret->quant = atom->quant;
839
ret->min = atom->min;
840
ret->max = atom->max;
841
if (atom->nbRanges > 0) {
844
ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
846
if (ret->ranges == NULL) {
847
xmlRegexpErrMemory(ctxt, "copying atom");
850
for (i = 0;i < atom->nbRanges;i++) {
851
ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
852
if (ret->ranges[i] == NULL)
854
ret->nbRanges = i + 1;
864
static xmlRegStatePtr
865
xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
868
ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
870
xmlRegexpErrMemory(ctxt, "allocating state");
873
memset(ret, 0, sizeof(xmlRegState));
874
ret->type = XML_REGEXP_TRANS_STATE;
875
ret->mark = XML_REGEXP_MARK_NORMAL;
881
* @state: the regexp state
883
* Free a regexp state
886
xmlRegFreeState(xmlRegStatePtr state) {
890
if (state->trans != NULL)
891
xmlFree(state->trans);
892
if (state->transTo != NULL)
893
xmlFree(state->transTo);
898
* xmlRegFreeParserCtxt:
899
* @ctxt: the regexp parser context
901
* Free a regexp parser context
904
xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
909
if (ctxt->string != NULL)
910
xmlFree(ctxt->string);
911
if (ctxt->states != NULL) {
912
for (i = 0;i < ctxt->nbStates;i++)
913
xmlRegFreeState(ctxt->states[i]);
914
xmlFree(ctxt->states);
916
if (ctxt->atoms != NULL) {
917
for (i = 0;i < ctxt->nbAtoms;i++)
918
xmlRegFreeAtom(ctxt->atoms[i]);
919
xmlFree(ctxt->atoms);
921
if (ctxt->counters != NULL)
922
xmlFree(ctxt->counters);
926
/************************************************************************
928
* Display of Data structures *
930
************************************************************************/
933
xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
935
case XML_REGEXP_EPSILON:
936
fprintf(output, "epsilon "); break;
937
case XML_REGEXP_CHARVAL:
938
fprintf(output, "charval "); break;
939
case XML_REGEXP_RANGES:
940
fprintf(output, "ranges "); break;
941
case XML_REGEXP_SUBREG:
942
fprintf(output, "subexpr "); break;
943
case XML_REGEXP_STRING:
944
fprintf(output, "string "); break;
945
case XML_REGEXP_ANYCHAR:
946
fprintf(output, "anychar "); break;
947
case XML_REGEXP_ANYSPACE:
948
fprintf(output, "anyspace "); break;
949
case XML_REGEXP_NOTSPACE:
950
fprintf(output, "notspace "); break;
951
case XML_REGEXP_INITNAME:
952
fprintf(output, "initname "); break;
953
case XML_REGEXP_NOTINITNAME:
954
fprintf(output, "notinitname "); break;
955
case XML_REGEXP_NAMECHAR:
956
fprintf(output, "namechar "); break;
957
case XML_REGEXP_NOTNAMECHAR:
958
fprintf(output, "notnamechar "); break;
959
case XML_REGEXP_DECIMAL:
960
fprintf(output, "decimal "); break;
961
case XML_REGEXP_NOTDECIMAL:
962
fprintf(output, "notdecimal "); break;
963
case XML_REGEXP_REALCHAR:
964
fprintf(output, "realchar "); break;
965
case XML_REGEXP_NOTREALCHAR:
966
fprintf(output, "notrealchar "); break;
967
case XML_REGEXP_LETTER:
968
fprintf(output, "LETTER "); break;
969
case XML_REGEXP_LETTER_UPPERCASE:
970
fprintf(output, "LETTER_UPPERCASE "); break;
971
case XML_REGEXP_LETTER_LOWERCASE:
972
fprintf(output, "LETTER_LOWERCASE "); break;
973
case XML_REGEXP_LETTER_TITLECASE:
974
fprintf(output, "LETTER_TITLECASE "); break;
975
case XML_REGEXP_LETTER_MODIFIER:
976
fprintf(output, "LETTER_MODIFIER "); break;
977
case XML_REGEXP_LETTER_OTHERS:
978
fprintf(output, "LETTER_OTHERS "); break;
979
case XML_REGEXP_MARK:
980
fprintf(output, "MARK "); break;
981
case XML_REGEXP_MARK_NONSPACING:
982
fprintf(output, "MARK_NONSPACING "); break;
983
case XML_REGEXP_MARK_SPACECOMBINING:
984
fprintf(output, "MARK_SPACECOMBINING "); break;
985
case XML_REGEXP_MARK_ENCLOSING:
986
fprintf(output, "MARK_ENCLOSING "); break;
987
case XML_REGEXP_NUMBER:
988
fprintf(output, "NUMBER "); break;
989
case XML_REGEXP_NUMBER_DECIMAL:
990
fprintf(output, "NUMBER_DECIMAL "); break;
991
case XML_REGEXP_NUMBER_LETTER:
992
fprintf(output, "NUMBER_LETTER "); break;
993
case XML_REGEXP_NUMBER_OTHERS:
994
fprintf(output, "NUMBER_OTHERS "); break;
995
case XML_REGEXP_PUNCT:
996
fprintf(output, "PUNCT "); break;
997
case XML_REGEXP_PUNCT_CONNECTOR:
998
fprintf(output, "PUNCT_CONNECTOR "); break;
999
case XML_REGEXP_PUNCT_DASH:
1000
fprintf(output, "PUNCT_DASH "); break;
1001
case XML_REGEXP_PUNCT_OPEN:
1002
fprintf(output, "PUNCT_OPEN "); break;
1003
case XML_REGEXP_PUNCT_CLOSE:
1004
fprintf(output, "PUNCT_CLOSE "); break;
1005
case XML_REGEXP_PUNCT_INITQUOTE:
1006
fprintf(output, "PUNCT_INITQUOTE "); break;
1007
case XML_REGEXP_PUNCT_FINQUOTE:
1008
fprintf(output, "PUNCT_FINQUOTE "); break;
1009
case XML_REGEXP_PUNCT_OTHERS:
1010
fprintf(output, "PUNCT_OTHERS "); break;
1011
case XML_REGEXP_SEPAR:
1012
fprintf(output, "SEPAR "); break;
1013
case XML_REGEXP_SEPAR_SPACE:
1014
fprintf(output, "SEPAR_SPACE "); break;
1015
case XML_REGEXP_SEPAR_LINE:
1016
fprintf(output, "SEPAR_LINE "); break;
1017
case XML_REGEXP_SEPAR_PARA:
1018
fprintf(output, "SEPAR_PARA "); break;
1019
case XML_REGEXP_SYMBOL:
1020
fprintf(output, "SYMBOL "); break;
1021
case XML_REGEXP_SYMBOL_MATH:
1022
fprintf(output, "SYMBOL_MATH "); break;
1023
case XML_REGEXP_SYMBOL_CURRENCY:
1024
fprintf(output, "SYMBOL_CURRENCY "); break;
1025
case XML_REGEXP_SYMBOL_MODIFIER:
1026
fprintf(output, "SYMBOL_MODIFIER "); break;
1027
case XML_REGEXP_SYMBOL_OTHERS:
1028
fprintf(output, "SYMBOL_OTHERS "); break;
1029
case XML_REGEXP_OTHER:
1030
fprintf(output, "OTHER "); break;
1031
case XML_REGEXP_OTHER_CONTROL:
1032
fprintf(output, "OTHER_CONTROL "); break;
1033
case XML_REGEXP_OTHER_FORMAT:
1034
fprintf(output, "OTHER_FORMAT "); break;
1035
case XML_REGEXP_OTHER_PRIVATE:
1036
fprintf(output, "OTHER_PRIVATE "); break;
1037
case XML_REGEXP_OTHER_NA:
1038
fprintf(output, "OTHER_NA "); break;
1039
case XML_REGEXP_BLOCK_NAME:
1040
fprintf(output, "BLOCK "); break;
1045
xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
1047
case XML_REGEXP_QUANT_EPSILON:
1048
fprintf(output, "epsilon "); break;
1049
case XML_REGEXP_QUANT_ONCE:
1050
fprintf(output, "once "); break;
1051
case XML_REGEXP_QUANT_OPT:
1052
fprintf(output, "? "); break;
1053
case XML_REGEXP_QUANT_MULT:
1054
fprintf(output, "* "); break;
1055
case XML_REGEXP_QUANT_PLUS:
1056
fprintf(output, "+ "); break;
1057
case XML_REGEXP_QUANT_RANGE:
1058
fprintf(output, "range "); break;
1059
case XML_REGEXP_QUANT_ONCEONLY:
1060
fprintf(output, "onceonly "); break;
1061
case XML_REGEXP_QUANT_ALL:
1062
fprintf(output, "all "); break;
1066
xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
1067
fprintf(output, " range: ");
1069
fprintf(output, "negative ");
1070
xmlRegPrintAtomType(output, range->type);
1071
fprintf(output, "%c - %c\n", range->start, range->end);
1075
xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
1076
fprintf(output, " atom: ");
1078
fprintf(output, "NULL\n");
1082
fprintf(output, "not ");
1083
xmlRegPrintAtomType(output, atom->type);
1084
xmlRegPrintQuantType(output, atom->quant);
1085
if (atom->quant == XML_REGEXP_QUANT_RANGE)
1086
fprintf(output, "%d-%d ", atom->min, atom->max);
1087
if (atom->type == XML_REGEXP_STRING)
1088
fprintf(output, "'%s' ", (char *) atom->valuep);
1089
if (atom->type == XML_REGEXP_CHARVAL)
1090
fprintf(output, "char %c\n", atom->codepoint);
1091
else if (atom->type == XML_REGEXP_RANGES) {
1093
fprintf(output, "%d entries\n", atom->nbRanges);
1094
for (i = 0; i < atom->nbRanges;i++)
1095
xmlRegPrintRange(output, atom->ranges[i]);
1096
} else if (atom->type == XML_REGEXP_SUBREG) {
1097
fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
1099
fprintf(output, "\n");
1104
xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
1105
fprintf(output, " trans: ");
1106
if (trans == NULL) {
1107
fprintf(output, "NULL\n");
1110
if (trans->to < 0) {
1111
fprintf(output, "removed\n");
1114
if (trans->nd != 0) {
1116
fprintf(output, "last not determinist, ");
1118
fprintf(output, "not determinist, ");
1120
if (trans->counter >= 0) {
1121
fprintf(output, "counted %d, ", trans->counter);
1123
if (trans->count == REGEXP_ALL_COUNTER) {
1124
fprintf(output, "all transition, ");
1125
} else if (trans->count >= 0) {
1126
fprintf(output, "count based %d, ", trans->count);
1128
if (trans->atom == NULL) {
1129
fprintf(output, "epsilon to %d\n", trans->to);
1132
if (trans->atom->type == XML_REGEXP_CHARVAL)
1133
fprintf(output, "char %c ", trans->atom->codepoint);
1134
fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1138
xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1141
fprintf(output, " state: ");
1142
if (state == NULL) {
1143
fprintf(output, "NULL\n");
1146
if (state->type == XML_REGEXP_START_STATE)
1147
fprintf(output, "START ");
1148
if (state->type == XML_REGEXP_FINAL_STATE)
1149
fprintf(output, "FINAL ");
1151
fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1152
for (i = 0;i < state->nbTrans; i++) {
1153
xmlRegPrintTrans(output, &(state->trans[i]));
1157
#ifdef DEBUG_REGEXP_GRAPH
1159
xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1162
fprintf(output, " ctxt: ");
1164
fprintf(output, "NULL\n");
1167
fprintf(output, "'%s' ", ctxt->string);
1169
fprintf(output, "error ");
1171
fprintf(output, "neg ");
1172
fprintf(output, "\n");
1173
fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1174
for (i = 0;i < ctxt->nbAtoms; i++) {
1175
fprintf(output, " %02d ", i);
1176
xmlRegPrintAtom(output, ctxt->atoms[i]);
1178
if (ctxt->atom != NULL) {
1179
fprintf(output, "current atom:\n");
1180
xmlRegPrintAtom(output, ctxt->atom);
1182
fprintf(output, "%d states:", ctxt->nbStates);
1183
if (ctxt->start != NULL)
1184
fprintf(output, " start: %d", ctxt->start->no);
1185
if (ctxt->end != NULL)
1186
fprintf(output, " end: %d", ctxt->end->no);
1187
fprintf(output, "\n");
1188
for (i = 0;i < ctxt->nbStates; i++) {
1189
xmlRegPrintState(output, ctxt->states[i]);
1191
fprintf(output, "%d counters:\n", ctxt->nbCounters);
1192
for (i = 0;i < ctxt->nbCounters; i++) {
1193
fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1194
ctxt->counters[i].max);
1199
/************************************************************************
1201
* Finite Automata structures manipulations *
1203
************************************************************************/
1206
xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1207
int neg, xmlRegAtomType type, int start, int end,
1208
xmlChar *blockName) {
1209
xmlRegRangePtr range;
1212
ERROR("add range: atom is NULL");
1215
if (atom->type != XML_REGEXP_RANGES) {
1216
ERROR("add range: atom is not ranges");
1219
if (atom->maxRanges == 0) {
1220
atom->maxRanges = 4;
1221
atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1222
sizeof(xmlRegRangePtr));
1223
if (atom->ranges == NULL) {
1224
xmlRegexpErrMemory(ctxt, "adding ranges");
1225
atom->maxRanges = 0;
1228
} else if (atom->nbRanges >= atom->maxRanges) {
1229
xmlRegRangePtr *tmp;
1230
atom->maxRanges *= 2;
1231
tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1232
sizeof(xmlRegRangePtr));
1234
xmlRegexpErrMemory(ctxt, "adding ranges");
1235
atom->maxRanges /= 2;
1240
range = xmlRegNewRange(ctxt, neg, type, start, end);
1243
range->blockName = blockName;
1244
atom->ranges[atom->nbRanges++] = range;
1249
xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1250
if (ctxt->maxCounters == 0) {
1251
ctxt->maxCounters = 4;
1252
ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1253
sizeof(xmlRegCounter));
1254
if (ctxt->counters == NULL) {
1255
xmlRegexpErrMemory(ctxt, "allocating counter");
1256
ctxt->maxCounters = 0;
1259
} else if (ctxt->nbCounters >= ctxt->maxCounters) {
1261
ctxt->maxCounters *= 2;
1262
tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1263
sizeof(xmlRegCounter));
1265
xmlRegexpErrMemory(ctxt, "allocating counter");
1266
ctxt->maxCounters /= 2;
1269
ctxt->counters = tmp;
1271
ctxt->counters[ctxt->nbCounters].min = -1;
1272
ctxt->counters[ctxt->nbCounters].max = -1;
1273
return(ctxt->nbCounters++);
1277
xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1279
ERROR("atom push: atom is NULL");
1282
if (ctxt->maxAtoms == 0) {
1284
ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1285
sizeof(xmlRegAtomPtr));
1286
if (ctxt->atoms == NULL) {
1287
xmlRegexpErrMemory(ctxt, "pushing atom");
1291
} else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1293
ctxt->maxAtoms *= 2;
1294
tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1295
sizeof(xmlRegAtomPtr));
1297
xmlRegexpErrMemory(ctxt, "allocating counter");
1298
ctxt->maxAtoms /= 2;
1303
atom->no = ctxt->nbAtoms;
1304
ctxt->atoms[ctxt->nbAtoms++] = atom;
1309
xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
1311
if (target->maxTransTo == 0) {
1312
target->maxTransTo = 8;
1313
target->transTo = (int *) xmlMalloc(target->maxTransTo *
1315
if (target->transTo == NULL) {
1316
xmlRegexpErrMemory(ctxt, "adding transition");
1317
target->maxTransTo = 0;
1320
} else if (target->nbTransTo >= target->maxTransTo) {
1322
target->maxTransTo *= 2;
1323
tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
1326
xmlRegexpErrMemory(ctxt, "adding transition");
1327
target->maxTransTo /= 2;
1330
target->transTo = tmp;
1332
target->transTo[target->nbTransTo] = from;
1333
target->nbTransTo++;
1337
xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1338
xmlRegAtomPtr atom, xmlRegStatePtr target,
1339
int counter, int count) {
1343
if (state == NULL) {
1344
ERROR("add state: state is NULL");
1347
if (target == NULL) {
1348
ERROR("add state: target is NULL");
1352
* Other routines follow the philosophy 'When in doubt, add a transition'
1353
* so we check here whether such a transition is already present and, if
1354
* so, silently ignore this request.
1357
for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
1358
xmlRegTransPtr trans = &(state->trans[nrtrans]);
1359
if ((trans->atom == atom) &&
1360
(trans->to == target->no) &&
1361
(trans->counter == counter) &&
1362
(trans->count == count)) {
1363
#ifdef DEBUG_REGEXP_GRAPH
1364
printf("Ignoring duplicate transition from %d to %d\n",
1365
state->no, target->no);
1371
if (state->maxTrans == 0) {
1372
state->maxTrans = 8;
1373
state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1374
sizeof(xmlRegTrans));
1375
if (state->trans == NULL) {
1376
xmlRegexpErrMemory(ctxt, "adding transition");
1377
state->maxTrans = 0;
1380
} else if (state->nbTrans >= state->maxTrans) {
1382
state->maxTrans *= 2;
1383
tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1384
sizeof(xmlRegTrans));
1386
xmlRegexpErrMemory(ctxt, "adding transition");
1387
state->maxTrans /= 2;
1392
#ifdef DEBUG_REGEXP_GRAPH
1393
printf("Add trans from %d to %d ", state->no, target->no);
1394
if (count == REGEXP_ALL_COUNTER)
1395
printf("all transition\n");
1396
else if (count >= 0)
1397
printf("count based %d\n", count);
1398
else if (counter >= 0)
1399
printf("counted %d\n", counter);
1400
else if (atom == NULL)
1401
printf("epsilon transition\n");
1402
else if (atom != NULL)
1403
xmlRegPrintAtom(stdout, atom);
1406
state->trans[state->nbTrans].atom = atom;
1407
state->trans[state->nbTrans].to = target->no;
1408
state->trans[state->nbTrans].counter = counter;
1409
state->trans[state->nbTrans].count = count;
1410
state->trans[state->nbTrans].nd = 0;
1412
xmlRegStateAddTransTo(ctxt, target, state->no);
1416
xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
1417
if (state == NULL) return(-1);
1418
if (ctxt->maxStates == 0) {
1419
ctxt->maxStates = 4;
1420
ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1421
sizeof(xmlRegStatePtr));
1422
if (ctxt->states == NULL) {
1423
xmlRegexpErrMemory(ctxt, "adding state");
1424
ctxt->maxStates = 0;
1427
} else if (ctxt->nbStates >= ctxt->maxStates) {
1428
xmlRegStatePtr *tmp;
1429
ctxt->maxStates *= 2;
1430
tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1431
sizeof(xmlRegStatePtr));
1433
xmlRegexpErrMemory(ctxt, "adding state");
1434
ctxt->maxStates /= 2;
1439
state->no = ctxt->nbStates;
1440
ctxt->states[ctxt->nbStates++] = state;
1445
* xmlFAGenerateAllTransition:
1446
* @ctxt: a regexp parser context
1447
* @from: the from state
1448
* @to: the target state or NULL for building a new one
1453
xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
1454
xmlRegStatePtr from, xmlRegStatePtr to,
1457
to = xmlRegNewState(ctxt);
1458
xmlRegStatePush(ctxt, to);
1462
xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1464
xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
1468
* xmlFAGenerateEpsilonTransition:
1469
* @ctxt: a regexp parser context
1470
* @from: the from state
1471
* @to: the target state or NULL for building a new one
1475
xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1476
xmlRegStatePtr from, xmlRegStatePtr to) {
1478
to = xmlRegNewState(ctxt);
1479
xmlRegStatePush(ctxt, to);
1482
xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1486
* xmlFAGenerateCountedEpsilonTransition:
1487
* @ctxt: a regexp parser context
1488
* @from: the from state
1489
* @to: the target state or NULL for building a new one
1490
* counter: the counter for that transition
1494
xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1495
xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1497
to = xmlRegNewState(ctxt);
1498
xmlRegStatePush(ctxt, to);
1501
xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1505
* xmlFAGenerateCountedTransition:
1506
* @ctxt: a regexp parser context
1507
* @from: the from state
1508
* @to: the target state or NULL for building a new one
1509
* counter: the counter for that transition
1513
xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1514
xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1516
to = xmlRegNewState(ctxt);
1517
xmlRegStatePush(ctxt, to);
1520
xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1524
* xmlFAGenerateTransitions:
1525
* @ctxt: a regexp parser context
1526
* @from: the from state
1527
* @to: the target state or NULL for building a new one
1528
* @atom: the atom generating the transition
1530
* Returns 0 if success and -1 in case of error.
1533
xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1534
xmlRegStatePtr to, xmlRegAtomPtr atom) {
1538
ERROR("genrate transition: atom == NULL");
1541
if (atom->type == XML_REGEXP_SUBREG) {
1543
* this is a subexpression handling one should not need to
1544
* create a new node except for XML_REGEXP_QUANT_RANGE.
1546
if (xmlRegAtomPush(ctxt, atom) < 0) {
1549
if ((to != NULL) && (atom->stop != to) &&
1550
(atom->quant != XML_REGEXP_QUANT_RANGE)) {
1552
* Generate an epsilon transition to link to the target
1554
xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1556
} else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
1557
(atom->quant != XML_REGEXP_QUANT_ONCE)) {
1558
to = xmlRegNewState(ctxt);
1559
xmlRegStatePush(ctxt, to);
1561
xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1564
switch (atom->quant) {
1565
case XML_REGEXP_QUANT_OPT:
1566
atom->quant = XML_REGEXP_QUANT_ONCE;
1568
* transition done to the state after end of atom.
1569
* 1. set transition from atom start to new state
1570
* 2. set transition from atom end to this state.
1572
xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
1573
xmlFAGenerateEpsilonTransition(ctxt, atom->stop, ctxt->state);
1575
case XML_REGEXP_QUANT_MULT:
1576
atom->quant = XML_REGEXP_QUANT_ONCE;
1577
xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1578
xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1580
case XML_REGEXP_QUANT_PLUS:
1581
atom->quant = XML_REGEXP_QUANT_ONCE;
1582
xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1584
case XML_REGEXP_QUANT_RANGE: {
1586
xmlRegStatePtr inter, newstate;
1589
* create the final state now if needed
1594
newstate = xmlRegNewState(ctxt);
1595
xmlRegStatePush(ctxt, newstate);
1599
* The principle here is to use counted transition
1600
* to avoid explosion in the number of states in the
1601
* graph. This is clearly more complex but should not
1602
* be exploitable at runtime.
1604
if ((atom->min == 0) && (atom->start0 == NULL)) {
1607
* duplicate a transition based on atom to count next
1608
* occurences after 1. We cannot loop to atom->start
1609
* directly because we need an epsilon transition to
1612
/* ???? For some reason it seems we never reach that
1613
case, I suppose this got optimized out before when
1614
building the automata */
1615
copy = xmlRegCopyAtom(ctxt, atom);
1618
copy->quant = XML_REGEXP_QUANT_ONCE;
1622
if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
1625
inter = ctxt->state;
1626
counter = xmlRegGetCounter(ctxt);
1627
ctxt->counters[counter].min = atom->min - 1;
1628
ctxt->counters[counter].max = atom->max - 1;
1629
/* count the number of times we see it again */
1630
xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
1631
atom->stop, counter);
1632
/* allow a way out based on the count */
1633
xmlFAGenerateCountedTransition(ctxt, inter,
1635
/* and also allow a direct exit for 0 */
1636
xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1640
* either we need the atom at least once or there
1641
* is an atom->start0 allowing to easilly plug the
1642
* epsilon transition.
1644
counter = xmlRegGetCounter(ctxt);
1645
ctxt->counters[counter].min = atom->min - 1;
1646
ctxt->counters[counter].max = atom->max - 1;
1647
/* count the number of times we see it again */
1648
xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1649
atom->start, counter);
1650
/* allow a way out based on the count */
1651
xmlFAGenerateCountedTransition(ctxt, atom->stop,
1653
/* and if needed allow a direct exit for 0 */
1655
xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
1661
atom->quant = XML_REGEXP_QUANT_ONCE;
1662
ctxt->state = newstate;
1669
if ((atom->min == 0) && (atom->max == 0) &&
1670
(atom->quant == XML_REGEXP_QUANT_RANGE)) {
1672
* we can discard the atom and generate an epsilon transition instead
1675
to = xmlRegNewState(ctxt);
1677
xmlRegStatePush(ctxt, to);
1682
xmlFAGenerateEpsilonTransition(ctxt, from, to);
1684
xmlRegFreeAtom(atom);
1688
to = xmlRegNewState(ctxt);
1690
xmlRegStatePush(ctxt, to);
1696
if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
1697
(atom->quant == XML_REGEXP_QUANT_PLUS)) {
1699
* Do not pollute the target state by adding transitions from
1700
* it as it is likely to be the shared target of multiple branches.
1701
* So isolate with an epsilon transition.
1705
tmp = xmlRegNewState(ctxt);
1707
xmlRegStatePush(ctxt, tmp);
1711
xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
1714
if (xmlRegAtomPush(ctxt, atom) < 0) {
1717
xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1719
switch (atom->quant) {
1720
case XML_REGEXP_QUANT_OPT:
1721
atom->quant = XML_REGEXP_QUANT_ONCE;
1722
xmlFAGenerateEpsilonTransition(ctxt, from, to);
1724
case XML_REGEXP_QUANT_MULT:
1725
atom->quant = XML_REGEXP_QUANT_ONCE;
1726
xmlFAGenerateEpsilonTransition(ctxt, from, to);
1727
xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1729
case XML_REGEXP_QUANT_PLUS:
1730
atom->quant = XML_REGEXP_QUANT_ONCE;
1731
xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1733
case XML_REGEXP_QUANT_RANGE:
1735
if (atom->min == 0) {
1736
xmlFAGenerateEpsilonTransition(ctxt, from, to);
1747
* xmlFAReduceEpsilonTransitions:
1748
* @ctxt: a regexp parser context
1749
* @fromnr: the from state
1750
* @tonr: the to state
1751
* @counter: should that transition be associated to a counted
1755
xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1756
int tonr, int counter) {
1758
xmlRegStatePtr from;
1761
#ifdef DEBUG_REGEXP_GRAPH
1762
printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1764
from = ctxt->states[fromnr];
1767
to = ctxt->states[tonr];
1770
if ((to->mark == XML_REGEXP_MARK_START) ||
1771
(to->mark == XML_REGEXP_MARK_VISITED))
1774
to->mark = XML_REGEXP_MARK_VISITED;
1775
if (to->type == XML_REGEXP_FINAL_STATE) {
1776
#ifdef DEBUG_REGEXP_GRAPH
1777
printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1779
from->type = XML_REGEXP_FINAL_STATE;
1781
for (transnr = 0;transnr < to->nbTrans;transnr++) {
1782
if (to->trans[transnr].to < 0)
1784
if (to->trans[transnr].atom == NULL) {
1786
* Don't remove counted transitions
1789
if (to->trans[transnr].to != fromnr) {
1790
if (to->trans[transnr].count >= 0) {
1791
int newto = to->trans[transnr].to;
1793
xmlRegStateAddTrans(ctxt, from, NULL,
1794
ctxt->states[newto],
1795
-1, to->trans[transnr].count);
1797
#ifdef DEBUG_REGEXP_GRAPH
1798
printf("Found epsilon trans %d from %d to %d\n",
1799
transnr, tonr, to->trans[transnr].to);
1801
if (to->trans[transnr].counter >= 0) {
1802
xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1803
to->trans[transnr].to,
1804
to->trans[transnr].counter);
1806
xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1807
to->trans[transnr].to,
1813
int newto = to->trans[transnr].to;
1815
if (to->trans[transnr].counter >= 0) {
1816
xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1817
ctxt->states[newto],
1818
to->trans[transnr].counter, -1);
1820
xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1821
ctxt->states[newto], counter, -1);
1825
to->mark = XML_REGEXP_MARK_NORMAL;
1829
* xmlFAEliminateSimpleEpsilonTransitions:
1830
* @ctxt: a regexp parser context
1832
* Eliminating general epsilon transitions can get costly in the general
1833
* algorithm due to the large amount of generated new transitions and
1834
* associated comparisons. However for simple epsilon transition used just
1835
* to separate building blocks when generating the automata this can be
1836
* reduced to state elimination:
1837
* - if there exists an epsilon from X to Y
1838
* - if there is no other transition from X
1839
* then X and Y are semantically equivalent and X can be eliminated
1840
* If X is the start state then make Y the start state, else replace the
1841
* target of all transitions to X by transitions to Y.
1844
xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1845
int statenr, i, j, newto;
1846
xmlRegStatePtr state, tmp;
1848
for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1849
state = ctxt->states[statenr];
1852
if (state->nbTrans != 1)
1854
if (state->type == XML_REGEXP_UNREACH_STATE)
1856
/* is the only transition out a basic transition */
1857
if ((state->trans[0].atom == NULL) &&
1858
(state->trans[0].to >= 0) &&
1859
(state->trans[0].to != statenr) &&
1860
(state->trans[0].counter < 0) &&
1861
(state->trans[0].count < 0)) {
1862
newto = state->trans[0].to;
1864
if (state->type == XML_REGEXP_START_STATE) {
1865
#ifdef DEBUG_REGEXP_GRAPH
1866
printf("Found simple epsilon trans from start %d to %d\n",
1870
#ifdef DEBUG_REGEXP_GRAPH
1871
printf("Found simple epsilon trans from %d to %d\n",
1874
for (i = 0;i < state->nbTransTo;i++) {
1875
tmp = ctxt->states[state->transTo[i]];
1876
for (j = 0;j < tmp->nbTrans;j++) {
1877
if (tmp->trans[j].to == statenr) {
1878
#ifdef DEBUG_REGEXP_GRAPH
1879
printf("Changed transition %d on %d to go to %d\n",
1882
tmp->trans[j].to = -1;
1883
xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
1884
ctxt->states[newto],
1885
tmp->trans[j].counter,
1886
tmp->trans[j].count);
1890
if (state->type == XML_REGEXP_FINAL_STATE)
1891
ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
1892
/* eliminate the transition completely */
1895
state->type = XML_REGEXP_UNREACH_STATE;
1903
* xmlFAEliminateEpsilonTransitions:
1904
* @ctxt: a regexp parser context
1908
xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1909
int statenr, transnr;
1910
xmlRegStatePtr state;
1913
if (ctxt->states == NULL) return;
1916
* Eliminate simple epsilon transition and the associated unreachable
1919
xmlFAEliminateSimpleEpsilonTransitions(ctxt);
1920
for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1921
state = ctxt->states[statenr];
1922
if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
1923
#ifdef DEBUG_REGEXP_GRAPH
1924
printf("Removed unreachable state %d\n", statenr);
1926
xmlRegFreeState(state);
1927
ctxt->states[statenr] = NULL;
1934
* Build the completed transitions bypassing the epsilons
1935
* Use a marking algorithm to avoid loops
1936
* Mark sink states too.
1937
* Process from the latests states backward to the start when
1938
* there is long cascading epsilon chains this minimize the
1939
* recursions and transition compares when adding the new ones
1941
for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
1942
state = ctxt->states[statenr];
1945
if ((state->nbTrans == 0) &&
1946
(state->type != XML_REGEXP_FINAL_STATE)) {
1947
state->type = XML_REGEXP_SINK_STATE;
1949
for (transnr = 0;transnr < state->nbTrans;transnr++) {
1950
if ((state->trans[transnr].atom == NULL) &&
1951
(state->trans[transnr].to >= 0)) {
1952
if (state->trans[transnr].to == statenr) {
1953
state->trans[transnr].to = -1;
1954
#ifdef DEBUG_REGEXP_GRAPH
1955
printf("Removed loopback epsilon trans %d on %d\n",
1958
} else if (state->trans[transnr].count < 0) {
1959
int newto = state->trans[transnr].to;
1961
#ifdef DEBUG_REGEXP_GRAPH
1962
printf("Found epsilon trans %d from %d to %d\n",
1963
transnr, statenr, newto);
1966
state->trans[transnr].to = -2;
1967
state->mark = XML_REGEXP_MARK_START;
1968
xmlFAReduceEpsilonTransitions(ctxt, statenr,
1969
newto, state->trans[transnr].counter);
1970
state->mark = XML_REGEXP_MARK_NORMAL;
1971
#ifdef DEBUG_REGEXP_GRAPH
1973
printf("Found counted transition %d on %d\n",
1981
* Eliminate the epsilon transitions
1984
for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1985
state = ctxt->states[statenr];
1988
for (transnr = 0;transnr < state->nbTrans;transnr++) {
1989
xmlRegTransPtr trans = &(state->trans[transnr]);
1990
if ((trans->atom == NULL) &&
1991
(trans->count < 0) &&
2000
* Use this pass to detect unreachable states too
2002
for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2003
state = ctxt->states[statenr];
2005
state->reached = XML_REGEXP_MARK_NORMAL;
2007
state = ctxt->states[0];
2009
state->reached = XML_REGEXP_MARK_START;
2010
while (state != NULL) {
2011
xmlRegStatePtr target = NULL;
2012
state->reached = XML_REGEXP_MARK_VISITED;
2014
* Mark all states reachable from the current reachable state
2016
for (transnr = 0;transnr < state->nbTrans;transnr++) {
2017
if ((state->trans[transnr].to >= 0) &&
2018
((state->trans[transnr].atom != NULL) ||
2019
(state->trans[transnr].count >= 0))) {
2020
int newto = state->trans[transnr].to;
2022
if (ctxt->states[newto] == NULL)
2024
if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
2025
ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
2026
target = ctxt->states[newto];
2032
* find the next accessible state not explored
2034
if (target == NULL) {
2035
for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
2036
state = ctxt->states[statenr];
2037
if ((state != NULL) && (state->reached ==
2038
XML_REGEXP_MARK_START)) {
2046
for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2047
state = ctxt->states[statenr];
2048
if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
2049
#ifdef DEBUG_REGEXP_GRAPH
2050
printf("Removed unreachable state %d\n", statenr);
2052
xmlRegFreeState(state);
2053
ctxt->states[statenr] = NULL;
2060
xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
2063
if ((range1->type == XML_REGEXP_RANGES) ||
2064
(range2->type == XML_REGEXP_RANGES) ||
2065
(range2->type == XML_REGEXP_SUBREG) ||
2066
(range1->type == XML_REGEXP_SUBREG) ||
2067
(range1->type == XML_REGEXP_STRING) ||
2068
(range2->type == XML_REGEXP_STRING))
2071
/* put them in order */
2072
if (range1->type > range2->type) {
2079
if ((range1->type == XML_REGEXP_ANYCHAR) ||
2080
(range2->type == XML_REGEXP_ANYCHAR)) {
2082
} else if ((range1->type == XML_REGEXP_EPSILON) ||
2083
(range2->type == XML_REGEXP_EPSILON)) {
2085
} else if (range1->type == range2->type) {
2086
if ((range1->type != XML_REGEXP_CHARVAL) ||
2087
(range1->end < range2->start) ||
2088
(range2->end < range1->start))
2092
} else if (range1->type == XML_REGEXP_CHARVAL) {
2097
* just check all codepoints in the range for acceptance,
2098
* this is usually way cheaper since done only once at
2099
* compilation than testing over and over at runtime or
2100
* pushing too many states when evaluating.
2102
if (((range1->neg == 0) && (range2->neg != 0)) ||
2103
((range1->neg != 0) && (range2->neg == 0)))
2106
for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
2107
ret = xmlRegCheckCharacterRange(range2->type, codepoint,
2108
0, range2->start, range2->end,
2112
if (((neg == 1) && (ret == 0)) ||
2113
((neg == 0) && (ret == 1)))
2117
} else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
2118
(range2->type == XML_REGEXP_BLOCK_NAME)) {
2119
if (range1->type == range2->type) {
2120
ret = xmlStrEqual(range1->blockName, range2->blockName);
2123
* comparing a block range with anything else is way
2124
* too costly, and maintining the table is like too much
2125
* memory too, so let's force the automata to save state
2130
} else if ((range1->type < XML_REGEXP_LETTER) ||
2131
(range2->type < XML_REGEXP_LETTER)) {
2132
if ((range1->type == XML_REGEXP_ANYSPACE) &&
2133
(range2->type == XML_REGEXP_NOTSPACE))
2135
else if ((range1->type == XML_REGEXP_INITNAME) &&
2136
(range2->type == XML_REGEXP_NOTINITNAME))
2138
else if ((range1->type == XML_REGEXP_NAMECHAR) &&
2139
(range2->type == XML_REGEXP_NOTNAMECHAR))
2141
else if ((range1->type == XML_REGEXP_DECIMAL) &&
2142
(range2->type == XML_REGEXP_NOTDECIMAL))
2144
else if ((range1->type == XML_REGEXP_REALCHAR) &&
2145
(range2->type == XML_REGEXP_NOTREALCHAR))
2148
/* same thing to limit complexity */
2153
/* range1->type < range2->type here */
2154
switch (range1->type) {
2155
case XML_REGEXP_LETTER:
2156
/* all disjoint except in the subgroups */
2157
if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
2158
(range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
2159
(range2->type == XML_REGEXP_LETTER_TITLECASE) ||
2160
(range2->type == XML_REGEXP_LETTER_MODIFIER) ||
2161
(range2->type == XML_REGEXP_LETTER_OTHERS))
2164
case XML_REGEXP_MARK:
2165
if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
2166
(range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
2167
(range2->type == XML_REGEXP_MARK_ENCLOSING))
2170
case XML_REGEXP_NUMBER:
2171
if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
2172
(range2->type == XML_REGEXP_NUMBER_LETTER) ||
2173
(range2->type == XML_REGEXP_NUMBER_OTHERS))
2176
case XML_REGEXP_PUNCT:
2177
if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
2178
(range2->type == XML_REGEXP_PUNCT_DASH) ||
2179
(range2->type == XML_REGEXP_PUNCT_OPEN) ||
2180
(range2->type == XML_REGEXP_PUNCT_CLOSE) ||
2181
(range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
2182
(range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
2183
(range2->type == XML_REGEXP_PUNCT_OTHERS))
2186
case XML_REGEXP_SEPAR:
2187
if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
2188
(range2->type == XML_REGEXP_SEPAR_LINE) ||
2189
(range2->type == XML_REGEXP_SEPAR_PARA))
2192
case XML_REGEXP_SYMBOL:
2193
if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
2194
(range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
2195
(range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
2196
(range2->type == XML_REGEXP_SYMBOL_OTHERS))
2199
case XML_REGEXP_OTHER:
2200
if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
2201
(range2->type == XML_REGEXP_OTHER_FORMAT) ||
2202
(range2->type == XML_REGEXP_OTHER_PRIVATE))
2206
if ((range2->type >= XML_REGEXP_LETTER) &&
2207
(range2->type < XML_REGEXP_BLOCK_NAME))
2215
if (((range1->neg == 0) && (range2->neg != 0)) ||
2216
((range1->neg != 0) && (range2->neg == 0)))
2222
* xmlFACompareAtomTypes:
2223
* @type1: an atom type
2224
* @type2: an atom type
2226
* Compares two atoms type to check whether they intersect in some ways,
2227
* this is used by xmlFACompareAtoms only
2229
* Returns 1 if they may intersect and 0 otherwise
2232
xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
2233
if ((type1 == XML_REGEXP_EPSILON) ||
2234
(type1 == XML_REGEXP_CHARVAL) ||
2235
(type1 == XML_REGEXP_RANGES) ||
2236
(type1 == XML_REGEXP_SUBREG) ||
2237
(type1 == XML_REGEXP_STRING) ||
2238
(type1 == XML_REGEXP_ANYCHAR))
2240
if ((type2 == XML_REGEXP_EPSILON) ||
2241
(type2 == XML_REGEXP_CHARVAL) ||
2242
(type2 == XML_REGEXP_RANGES) ||
2243
(type2 == XML_REGEXP_SUBREG) ||
2244
(type2 == XML_REGEXP_STRING) ||
2245
(type2 == XML_REGEXP_ANYCHAR))
2248
if (type1 == type2) return(1);
2250
/* simplify subsequent compares by making sure type1 < type2 */
2251
if (type1 > type2) {
2252
xmlRegAtomType tmp = type1;
2257
case XML_REGEXP_ANYSPACE: /* \s */
2258
/* can't be a letter, number, mark, pontuation, symbol */
2259
if ((type2 == XML_REGEXP_NOTSPACE) ||
2260
((type2 >= XML_REGEXP_LETTER) &&
2261
(type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2262
((type2 >= XML_REGEXP_NUMBER) &&
2263
(type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2264
((type2 >= XML_REGEXP_MARK) &&
2265
(type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2266
((type2 >= XML_REGEXP_PUNCT) &&
2267
(type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2268
((type2 >= XML_REGEXP_SYMBOL) &&
2269
(type2 <= XML_REGEXP_SYMBOL_OTHERS))
2272
case XML_REGEXP_NOTSPACE: /* \S */
2274
case XML_REGEXP_INITNAME: /* \l */
2275
/* can't be a number, mark, separator, pontuation, symbol or other */
2276
if ((type2 == XML_REGEXP_NOTINITNAME) ||
2277
((type2 >= XML_REGEXP_NUMBER) &&
2278
(type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
2279
((type2 >= XML_REGEXP_MARK) &&
2280
(type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2281
((type2 >= XML_REGEXP_SEPAR) &&
2282
(type2 <= XML_REGEXP_SEPAR_PARA)) ||
2283
((type2 >= XML_REGEXP_PUNCT) &&
2284
(type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2285
((type2 >= XML_REGEXP_SYMBOL) &&
2286
(type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2287
((type2 >= XML_REGEXP_OTHER) &&
2288
(type2 <= XML_REGEXP_OTHER_NA))
2291
case XML_REGEXP_NOTINITNAME: /* \L */
2293
case XML_REGEXP_NAMECHAR: /* \c */
2294
/* can't be a mark, separator, pontuation, symbol or other */
2295
if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
2296
((type2 >= XML_REGEXP_MARK) &&
2297
(type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2298
((type2 >= XML_REGEXP_PUNCT) &&
2299
(type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2300
((type2 >= XML_REGEXP_SEPAR) &&
2301
(type2 <= XML_REGEXP_SEPAR_PARA)) ||
2302
((type2 >= XML_REGEXP_SYMBOL) &&
2303
(type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2304
((type2 >= XML_REGEXP_OTHER) &&
2305
(type2 <= XML_REGEXP_OTHER_NA))
2308
case XML_REGEXP_NOTNAMECHAR: /* \C */
2310
case XML_REGEXP_DECIMAL: /* \d */
2311
/* can't be a letter, mark, separator, pontuation, symbol or other */
2312
if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2313
(type2 == XML_REGEXP_REALCHAR) ||
2314
((type2 >= XML_REGEXP_LETTER) &&
2315
(type2 <= XML_REGEXP_LETTER_OTHERS)) ||
2316
((type2 >= XML_REGEXP_MARK) &&
2317
(type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2318
((type2 >= XML_REGEXP_PUNCT) &&
2319
(type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2320
((type2 >= XML_REGEXP_SEPAR) &&
2321
(type2 <= XML_REGEXP_SEPAR_PARA)) ||
2322
((type2 >= XML_REGEXP_SYMBOL) &&
2323
(type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2324
((type2 >= XML_REGEXP_OTHER) &&
2325
(type2 <= XML_REGEXP_OTHER_NA))
2328
case XML_REGEXP_NOTDECIMAL: /* \D */
2330
case XML_REGEXP_REALCHAR: /* \w */
2331
/* can't be a mark, separator, pontuation, symbol or other */
2332
if ((type2 == XML_REGEXP_NOTDECIMAL) ||
2333
((type2 >= XML_REGEXP_MARK) &&
2334
(type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
2335
((type2 >= XML_REGEXP_PUNCT) &&
2336
(type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
2337
((type2 >= XML_REGEXP_SEPAR) &&
2338
(type2 <= XML_REGEXP_SEPAR_PARA)) ||
2339
((type2 >= XML_REGEXP_SYMBOL) &&
2340
(type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
2341
((type2 >= XML_REGEXP_OTHER) &&
2342
(type2 <= XML_REGEXP_OTHER_NA))
2345
case XML_REGEXP_NOTREALCHAR: /* \W */
2348
* at that point we know both type 1 and type2 are from
2349
* character categories are ordered and are different,
2350
* it becomes simple because this is a partition
2352
case XML_REGEXP_LETTER:
2353
if (type2 <= XML_REGEXP_LETTER_OTHERS)
2356
case XML_REGEXP_LETTER_UPPERCASE:
2357
case XML_REGEXP_LETTER_LOWERCASE:
2358
case XML_REGEXP_LETTER_TITLECASE:
2359
case XML_REGEXP_LETTER_MODIFIER:
2360
case XML_REGEXP_LETTER_OTHERS:
2362
case XML_REGEXP_MARK:
2363
if (type2 <= XML_REGEXP_MARK_ENCLOSING)
2366
case XML_REGEXP_MARK_NONSPACING:
2367
case XML_REGEXP_MARK_SPACECOMBINING:
2368
case XML_REGEXP_MARK_ENCLOSING:
2370
case XML_REGEXP_NUMBER:
2371
if (type2 <= XML_REGEXP_NUMBER_OTHERS)
2374
case XML_REGEXP_NUMBER_DECIMAL:
2375
case XML_REGEXP_NUMBER_LETTER:
2376
case XML_REGEXP_NUMBER_OTHERS:
2378
case XML_REGEXP_PUNCT:
2379
if (type2 <= XML_REGEXP_PUNCT_OTHERS)
2382
case XML_REGEXP_PUNCT_CONNECTOR:
2383
case XML_REGEXP_PUNCT_DASH:
2384
case XML_REGEXP_PUNCT_OPEN:
2385
case XML_REGEXP_PUNCT_CLOSE:
2386
case XML_REGEXP_PUNCT_INITQUOTE:
2387
case XML_REGEXP_PUNCT_FINQUOTE:
2388
case XML_REGEXP_PUNCT_OTHERS:
2390
case XML_REGEXP_SEPAR:
2391
if (type2 <= XML_REGEXP_SEPAR_PARA)
2394
case XML_REGEXP_SEPAR_SPACE:
2395
case XML_REGEXP_SEPAR_LINE:
2396
case XML_REGEXP_SEPAR_PARA:
2398
case XML_REGEXP_SYMBOL:
2399
if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
2402
case XML_REGEXP_SYMBOL_MATH:
2403
case XML_REGEXP_SYMBOL_CURRENCY:
2404
case XML_REGEXP_SYMBOL_MODIFIER:
2405
case XML_REGEXP_SYMBOL_OTHERS:
2407
case XML_REGEXP_OTHER:
2408
if (type2 <= XML_REGEXP_OTHER_NA)
2411
case XML_REGEXP_OTHER_CONTROL:
2412
case XML_REGEXP_OTHER_FORMAT:
2413
case XML_REGEXP_OTHER_PRIVATE:
2414
case XML_REGEXP_OTHER_NA:
2427
* Compares two atoms to check whether they are the same exactly
2428
* this is used to remove equivalent transitions
2430
* Returns 1 if same and 0 otherwise
2433
xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2438
if ((atom1 == NULL) || (atom2 == NULL))
2441
if (atom1->type != atom2->type)
2443
switch (atom1->type) {
2444
case XML_REGEXP_EPSILON:
2447
case XML_REGEXP_STRING:
2448
ret = xmlStrEqual((xmlChar *)atom1->valuep,
2449
(xmlChar *)atom2->valuep);
2451
case XML_REGEXP_CHARVAL:
2452
ret = (atom1->codepoint == atom2->codepoint);
2454
case XML_REGEXP_RANGES:
2455
/* too hard to do in the general case */
2464
* xmlFACompareAtoms:
2468
* Compares two atoms to check whether they intersect in some ways,
2469
* this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
2471
* Returns 1 if yes and 0 otherwise
2474
xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
2479
if ((atom1 == NULL) || (atom2 == NULL))
2482
if ((atom1->type == XML_REGEXP_ANYCHAR) ||
2483
(atom2->type == XML_REGEXP_ANYCHAR))
2486
if (atom1->type > atom2->type) {
2492
if (atom1->type != atom2->type) {
2493
ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
2494
/* if they can't intersect at the type level break now */
2498
switch (atom1->type) {
2499
case XML_REGEXP_STRING:
2500
ret = xmlRegStrEqualWildcard((xmlChar *)atom1->valuep,
2501
(xmlChar *)atom2->valuep);
2503
case XML_REGEXP_EPSILON:
2504
goto not_determinist;
2505
case XML_REGEXP_CHARVAL:
2506
if (atom2->type == XML_REGEXP_CHARVAL) {
2507
ret = (atom1->codepoint == atom2->codepoint);
2509
ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
2514
case XML_REGEXP_RANGES:
2515
if (atom2->type == XML_REGEXP_RANGES) {
2517
xmlRegRangePtr r1, r2;
2520
* need to check that none of the ranges eventually matches
2522
for (i = 0;i < atom1->nbRanges;i++) {
2523
for (j = 0;j < atom2->nbRanges;j++) {
2524
r1 = atom1->ranges[i];
2525
r2 = atom2->ranges[j];
2526
res = xmlFACompareRanges(r1, r2);
2537
goto not_determinist;
2540
if (atom1->neg != atom2->neg) {
2550
* xmlFARecurseDeterminism:
2551
* @ctxt: a regexp parser context
2553
* Check whether the associated regexp is determinist,
2554
* should be called after xmlFAEliminateEpsilonTransitions()
2558
xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
2559
int to, xmlRegAtomPtr atom) {
2562
int transnr, nbTrans;
2568
* don't recurse on transitions potentially added in the course of
2571
nbTrans = state->nbTrans;
2572
for (transnr = 0;transnr < nbTrans;transnr++) {
2573
t1 = &(state->trans[transnr]);
2575
* check transitions conflicting with the one looked at
2577
if (t1->atom == NULL) {
2580
res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2590
if (xmlFACompareAtoms(t1->atom, atom)) {
2592
/* mark the transition as non-deterministic */
2600
* xmlFAComputesDeterminism:
2601
* @ctxt: a regexp parser context
2603
* Check whether the associated regexp is determinist,
2604
* should be called after xmlFAEliminateEpsilonTransitions()
2608
xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
2609
int statenr, transnr;
2610
xmlRegStatePtr state;
2611
xmlRegTransPtr t1, t2, last;
2615
#ifdef DEBUG_REGEXP_GRAPH
2616
printf("xmlFAComputesDeterminism\n");
2617
xmlRegPrintCtxt(stdout, ctxt);
2619
if (ctxt->determinist != -1)
2620
return(ctxt->determinist);
2623
* First cleanup the automata removing cancelled transitions
2625
for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2626
state = ctxt->states[statenr];
2629
if (state->nbTrans < 2)
2631
for (transnr = 0;transnr < state->nbTrans;transnr++) {
2632
t1 = &(state->trans[transnr]);
2634
* Determinism checks in case of counted or all transitions
2635
* will have to be handled separately
2637
if (t1->atom == NULL) {
2641
if (t1->to == -1) /* eliminated */
2643
for (i = 0;i < transnr;i++) {
2644
t2 = &(state->trans[i]);
2645
if (t2->to == -1) /* eliminated */
2647
if (t2->atom != NULL) {
2648
if (t1->to == t2->to) {
2649
if (xmlFAEqualAtoms(t1->atom, t2->atom))
2650
t2->to = -1; /* eliminated */
2658
* Check for all states that there aren't 2 transitions
2659
* with the same atom and a different target.
2661
for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
2662
state = ctxt->states[statenr];
2665
if (state->nbTrans < 2)
2668
for (transnr = 0;transnr < state->nbTrans;transnr++) {
2669
t1 = &(state->trans[transnr]);
2671
* Determinism checks in case of counted or all transitions
2672
* will have to be handled separately
2674
if (t1->atom == NULL) {
2677
if (t1->to == -1) /* eliminated */
2679
for (i = 0;i < transnr;i++) {
2680
t2 = &(state->trans[i]);
2681
if (t2->to == -1) /* eliminated */
2683
if (t2->atom != NULL) {
2684
/* not determinist ! */
2685
if (xmlFACompareAtoms(t1->atom, t2->atom)) {
2687
/* mark the transitions as non-deterministic ones */
2692
} else if (t1->to != -1) {
2694
* do the closure in case of remaining specific
2695
* epsilon transitions like choices or all
2697
ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
2699
/* don't shortcut the computation so all non deterministic
2700
transition get marked down
2711
/* don't shortcut the computation so all non deterministic
2712
transition get marked down
2718
* mark specifically the last non-deterministic transition
2719
* from a state since there is no need to set-up rollback
2726
/* don't shortcut the computation so all non deterministic
2727
transition get marked down
2732
ctxt->determinist = ret;
2736
/************************************************************************
2738
* Routines to check input against transition atoms *
2740
************************************************************************/
2743
xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
2744
int start, int end, const xmlChar *blockName) {
2748
case XML_REGEXP_STRING:
2749
case XML_REGEXP_SUBREG:
2750
case XML_REGEXP_RANGES:
2751
case XML_REGEXP_EPSILON:
2753
case XML_REGEXP_ANYCHAR:
2754
ret = ((codepoint != '\n') && (codepoint != '\r'));
2756
case XML_REGEXP_CHARVAL:
2757
ret = ((codepoint >= start) && (codepoint <= end));
2759
case XML_REGEXP_NOTSPACE:
2761
case XML_REGEXP_ANYSPACE:
2762
ret = ((codepoint == '\n') || (codepoint == '\r') ||
2763
(codepoint == '\t') || (codepoint == ' '));
2765
case XML_REGEXP_NOTINITNAME:
2767
case XML_REGEXP_INITNAME:
2768
ret = (IS_LETTER(codepoint) ||
2769
(codepoint == '_') || (codepoint == ':'));
2771
case XML_REGEXP_NOTNAMECHAR:
2773
case XML_REGEXP_NAMECHAR:
2774
ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
2775
(codepoint == '.') || (codepoint == '-') ||
2776
(codepoint == '_') || (codepoint == ':') ||
2777
IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
2779
case XML_REGEXP_NOTDECIMAL:
2781
case XML_REGEXP_DECIMAL:
2782
ret = xmlUCSIsCatNd(codepoint);
2784
case XML_REGEXP_REALCHAR:
2786
case XML_REGEXP_NOTREALCHAR:
2787
ret = xmlUCSIsCatP(codepoint);
2789
ret = xmlUCSIsCatZ(codepoint);
2791
ret = xmlUCSIsCatC(codepoint);
2793
case XML_REGEXP_LETTER:
2794
ret = xmlUCSIsCatL(codepoint);
2796
case XML_REGEXP_LETTER_UPPERCASE:
2797
ret = xmlUCSIsCatLu(codepoint);
2799
case XML_REGEXP_LETTER_LOWERCASE:
2800
ret = xmlUCSIsCatLl(codepoint);
2802
case XML_REGEXP_LETTER_TITLECASE:
2803
ret = xmlUCSIsCatLt(codepoint);
2805
case XML_REGEXP_LETTER_MODIFIER:
2806
ret = xmlUCSIsCatLm(codepoint);
2808
case XML_REGEXP_LETTER_OTHERS:
2809
ret = xmlUCSIsCatLo(codepoint);
2811
case XML_REGEXP_MARK:
2812
ret = xmlUCSIsCatM(codepoint);
2814
case XML_REGEXP_MARK_NONSPACING:
2815
ret = xmlUCSIsCatMn(codepoint);
2817
case XML_REGEXP_MARK_SPACECOMBINING:
2818
ret = xmlUCSIsCatMc(codepoint);
2820
case XML_REGEXP_MARK_ENCLOSING:
2821
ret = xmlUCSIsCatMe(codepoint);
2823
case XML_REGEXP_NUMBER:
2824
ret = xmlUCSIsCatN(codepoint);
2826
case XML_REGEXP_NUMBER_DECIMAL:
2827
ret = xmlUCSIsCatNd(codepoint);
2829
case XML_REGEXP_NUMBER_LETTER:
2830
ret = xmlUCSIsCatNl(codepoint);
2832
case XML_REGEXP_NUMBER_OTHERS:
2833
ret = xmlUCSIsCatNo(codepoint);
2835
case XML_REGEXP_PUNCT:
2836
ret = xmlUCSIsCatP(codepoint);
2838
case XML_REGEXP_PUNCT_CONNECTOR:
2839
ret = xmlUCSIsCatPc(codepoint);
2841
case XML_REGEXP_PUNCT_DASH:
2842
ret = xmlUCSIsCatPd(codepoint);
2844
case XML_REGEXP_PUNCT_OPEN:
2845
ret = xmlUCSIsCatPs(codepoint);
2847
case XML_REGEXP_PUNCT_CLOSE:
2848
ret = xmlUCSIsCatPe(codepoint);
2850
case XML_REGEXP_PUNCT_INITQUOTE:
2851
ret = xmlUCSIsCatPi(codepoint);
2853
case XML_REGEXP_PUNCT_FINQUOTE:
2854
ret = xmlUCSIsCatPf(codepoint);
2856
case XML_REGEXP_PUNCT_OTHERS:
2857
ret = xmlUCSIsCatPo(codepoint);
2859
case XML_REGEXP_SEPAR:
2860
ret = xmlUCSIsCatZ(codepoint);
2862
case XML_REGEXP_SEPAR_SPACE:
2863
ret = xmlUCSIsCatZs(codepoint);
2865
case XML_REGEXP_SEPAR_LINE:
2866
ret = xmlUCSIsCatZl(codepoint);
2868
case XML_REGEXP_SEPAR_PARA:
2869
ret = xmlUCSIsCatZp(codepoint);
2871
case XML_REGEXP_SYMBOL:
2872
ret = xmlUCSIsCatS(codepoint);
2874
case XML_REGEXP_SYMBOL_MATH:
2875
ret = xmlUCSIsCatSm(codepoint);
2877
case XML_REGEXP_SYMBOL_CURRENCY:
2878
ret = xmlUCSIsCatSc(codepoint);
2880
case XML_REGEXP_SYMBOL_MODIFIER:
2881
ret = xmlUCSIsCatSk(codepoint);
2883
case XML_REGEXP_SYMBOL_OTHERS:
2884
ret = xmlUCSIsCatSo(codepoint);
2886
case XML_REGEXP_OTHER:
2887
ret = xmlUCSIsCatC(codepoint);
2889
case XML_REGEXP_OTHER_CONTROL:
2890
ret = xmlUCSIsCatCc(codepoint);
2892
case XML_REGEXP_OTHER_FORMAT:
2893
ret = xmlUCSIsCatCf(codepoint);
2895
case XML_REGEXP_OTHER_PRIVATE:
2896
ret = xmlUCSIsCatCo(codepoint);
2898
case XML_REGEXP_OTHER_NA:
2899
/* ret = xmlUCSIsCatCn(codepoint); */
2900
/* Seems it doesn't exist anymore in recent Unicode releases */
2903
case XML_REGEXP_BLOCK_NAME:
2904
ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2913
xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2915
xmlRegRangePtr range;
2917
if ((atom == NULL) || (!IS_CHAR(codepoint)))
2920
switch (atom->type) {
2921
case XML_REGEXP_SUBREG:
2922
case XML_REGEXP_EPSILON:
2924
case XML_REGEXP_CHARVAL:
2925
return(codepoint == atom->codepoint);
2926
case XML_REGEXP_RANGES: {
2929
for (i = 0;i < atom->nbRanges;i++) {
2930
range = atom->ranges[i];
2931
if (range->neg == 2) {
2932
ret = xmlRegCheckCharacterRange(range->type, codepoint,
2933
0, range->start, range->end,
2936
return(0); /* excluded char */
2937
} else if (range->neg) {
2938
ret = xmlRegCheckCharacterRange(range->type, codepoint,
2939
0, range->start, range->end,
2946
ret = xmlRegCheckCharacterRange(range->type, codepoint,
2947
0, range->start, range->end,
2950
accept = 1; /* might still be excluded */
2955
case XML_REGEXP_STRING:
2956
printf("TODO: XML_REGEXP_STRING\n");
2958
case XML_REGEXP_ANYCHAR:
2959
case XML_REGEXP_ANYSPACE:
2960
case XML_REGEXP_NOTSPACE:
2961
case XML_REGEXP_INITNAME:
2962
case XML_REGEXP_NOTINITNAME:
2963
case XML_REGEXP_NAMECHAR:
2964
case XML_REGEXP_NOTNAMECHAR:
2965
case XML_REGEXP_DECIMAL:
2966
case XML_REGEXP_NOTDECIMAL:
2967
case XML_REGEXP_REALCHAR:
2968
case XML_REGEXP_NOTREALCHAR:
2969
case XML_REGEXP_LETTER:
2970
case XML_REGEXP_LETTER_UPPERCASE:
2971
case XML_REGEXP_LETTER_LOWERCASE:
2972
case XML_REGEXP_LETTER_TITLECASE:
2973
case XML_REGEXP_LETTER_MODIFIER:
2974
case XML_REGEXP_LETTER_OTHERS:
2975
case XML_REGEXP_MARK:
2976
case XML_REGEXP_MARK_NONSPACING:
2977
case XML_REGEXP_MARK_SPACECOMBINING:
2978
case XML_REGEXP_MARK_ENCLOSING:
2979
case XML_REGEXP_NUMBER:
2980
case XML_REGEXP_NUMBER_DECIMAL:
2981
case XML_REGEXP_NUMBER_LETTER:
2982
case XML_REGEXP_NUMBER_OTHERS:
2983
case XML_REGEXP_PUNCT:
2984
case XML_REGEXP_PUNCT_CONNECTOR:
2985
case XML_REGEXP_PUNCT_DASH:
2986
case XML_REGEXP_PUNCT_OPEN:
2987
case XML_REGEXP_PUNCT_CLOSE:
2988
case XML_REGEXP_PUNCT_INITQUOTE:
2989
case XML_REGEXP_PUNCT_FINQUOTE:
2990
case XML_REGEXP_PUNCT_OTHERS:
2991
case XML_REGEXP_SEPAR:
2992
case XML_REGEXP_SEPAR_SPACE:
2993
case XML_REGEXP_SEPAR_LINE:
2994
case XML_REGEXP_SEPAR_PARA:
2995
case XML_REGEXP_SYMBOL:
2996
case XML_REGEXP_SYMBOL_MATH:
2997
case XML_REGEXP_SYMBOL_CURRENCY:
2998
case XML_REGEXP_SYMBOL_MODIFIER:
2999
case XML_REGEXP_SYMBOL_OTHERS:
3000
case XML_REGEXP_OTHER:
3001
case XML_REGEXP_OTHER_CONTROL:
3002
case XML_REGEXP_OTHER_FORMAT:
3003
case XML_REGEXP_OTHER_PRIVATE:
3004
case XML_REGEXP_OTHER_NA:
3005
case XML_REGEXP_BLOCK_NAME:
3006
ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
3007
(const xmlChar *)atom->valuep);
3015
/************************************************************************
3017
* Saving and restoring state of an execution context *
3019
************************************************************************/
3021
#ifdef DEBUG_REGEXP_EXEC
3023
xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
3024
printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
3025
if (exec->inputStack != NULL) {
3028
for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
3029
printf("%s ", (const char *)
3030
exec->inputStack[exec->inputStackNr - (i + 1)].value);
3032
printf(": %s", &(exec->inputString[exec->index]));
3039
xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
3040
#ifdef DEBUG_REGEXP_EXEC
3043
xmlFARegDebugExec(exec);
3047
if (exec->nbPush > MAX_PUSH) {
3053
if (exec->maxRollbacks == 0) {
3054
exec->maxRollbacks = 4;
3055
exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
3056
sizeof(xmlRegExecRollback));
3057
if (exec->rollbacks == NULL) {
3058
xmlRegexpErrMemory(NULL, "saving regexp");
3059
exec->maxRollbacks = 0;
3062
memset(exec->rollbacks, 0,
3063
exec->maxRollbacks * sizeof(xmlRegExecRollback));
3064
} else if (exec->nbRollbacks >= exec->maxRollbacks) {
3065
xmlRegExecRollback *tmp;
3066
int len = exec->maxRollbacks;
3068
exec->maxRollbacks *= 2;
3069
tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
3070
exec->maxRollbacks * sizeof(xmlRegExecRollback));
3072
xmlRegexpErrMemory(NULL, "saving regexp");
3073
exec->maxRollbacks /= 2;
3076
exec->rollbacks = tmp;
3077
tmp = &exec->rollbacks[len];
3078
memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
3080
exec->rollbacks[exec->nbRollbacks].state = exec->state;
3081
exec->rollbacks[exec->nbRollbacks].index = exec->index;
3082
exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
3083
if (exec->comp->nbCounters > 0) {
3084
if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3085
exec->rollbacks[exec->nbRollbacks].counts = (int *)
3086
xmlMalloc(exec->comp->nbCounters * sizeof(int));
3087
if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3088
xmlRegexpErrMemory(NULL, "saving regexp");
3093
memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
3094
exec->comp->nbCounters * sizeof(int));
3096
exec->nbRollbacks++;
3100
xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
3101
if (exec->nbRollbacks <= 0) {
3103
#ifdef DEBUG_REGEXP_EXEC
3104
printf("rollback failed on empty stack\n");
3108
exec->nbRollbacks--;
3109
exec->state = exec->rollbacks[exec->nbRollbacks].state;
3110
exec->index = exec->rollbacks[exec->nbRollbacks].index;
3111
exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
3112
if (exec->comp->nbCounters > 0) {
3113
if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
3114
fprintf(stderr, "exec save: allocation failed");
3118
memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
3119
exec->comp->nbCounters * sizeof(int));
3122
#ifdef DEBUG_REGEXP_EXEC
3123
printf("restored ");
3124
xmlFARegDebugExec(exec);
3128
/************************************************************************
3130
* Verifier, running an input against a compiled regexp *
3132
************************************************************************/
3135
xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
3136
xmlRegExecCtxt execval;
3137
xmlRegExecCtxtPtr exec = &execval;
3138
int ret, codepoint = 0, len, deter;
3140
exec->inputString = content;
3143
exec->determinist = 1;
3144
exec->maxRollbacks = 0;
3145
exec->nbRollbacks = 0;
3146
exec->rollbacks = NULL;
3149
exec->state = comp->states[0];
3151
exec->transcount = 0;
3152
exec->inputStack = NULL;
3153
exec->inputStackMax = 0;
3154
if (comp->nbCounters > 0) {
3155
exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
3156
if (exec->counts == NULL) {
3157
xmlRegexpErrMemory(NULL, "running regexp");
3160
memset(exec->counts, 0, comp->nbCounters * sizeof(int));
3162
exec->counts = NULL;
3163
while ((exec->status == 0) &&
3164
((exec->inputString[exec->index] != 0) ||
3165
(exec->state->type != XML_REGEXP_FINAL_STATE))) {
3166
xmlRegTransPtr trans;
3170
* If end of input on non-terminal state, rollback, however we may
3171
* still have epsilon like transition for counted transitions
3172
* on counters, in that case don't break too early. Additionally,
3173
* if we are working on a range like "AB{0,2}", where B is not present,
3174
* we don't want to break.
3177
if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
3179
* if there is a transition, we must check if
3180
* atom allows minOccurs of 0
3182
if (exec->transno < exec->state->nbTrans) {
3183
trans = &exec->state->trans[exec->transno];
3184
if (trans->to >=0) {
3186
if (!((atom->min == 0) && (atom->max > 0)))
3193
exec->transcount = 0;
3194
for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3195
trans = &exec->state->trans[exec->transno];
3201
if (trans->count >= 0) {
3203
xmlRegCounterPtr counter;
3205
if (exec->counts == NULL) {
3210
* A counted transition.
3213
count = exec->counts[trans->count];
3214
counter = &exec->comp->counters[trans->count];
3215
#ifdef DEBUG_REGEXP_EXEC
3216
printf("testing count %d: val %d, min %d, max %d\n",
3217
trans->count, count, counter->min, counter->max);
3219
ret = ((count >= counter->min) && (count <= counter->max));
3220
if ((ret) && (counter->min != counter->max))
3222
} else if (atom == NULL) {
3223
fprintf(stderr, "epsilon transition left at runtime\n");
3226
} else if (exec->inputString[exec->index] != 0) {
3227
codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3228
ret = xmlRegCheckCharacter(atom, codepoint);
3229
if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
3230
xmlRegStatePtr to = comp->states[trans->to];
3233
* this is a multiple input sequence
3234
* If there is a counter associated increment it now.
3235
* before potentially saving and rollback
3236
* do not increment if the counter is already over the
3237
* maximum limit in which case get to next transition
3239
if (trans->counter >= 0) {
3240
xmlRegCounterPtr counter;
3242
if ((exec->counts == NULL) ||
3243
(exec->comp == NULL) ||
3244
(exec->comp->counters == NULL)) {
3248
counter = &exec->comp->counters[trans->counter];
3249
if (exec->counts[trans->counter] >= counter->max)
3250
continue; /* for loop on transitions */
3252
#ifdef DEBUG_REGEXP_EXEC
3253
printf("Increasing count %d\n", trans->counter);
3255
exec->counts[trans->counter]++;
3257
if (exec->state->nbTrans > exec->transno + 1) {
3258
xmlFARegExecSave(exec);
3260
exec->transcount = 1;
3263
* Try to progress as much as possible on the input
3265
if (exec->transcount == atom->max) {
3270
* End of input: stop here
3272
if (exec->inputString[exec->index] == 0) {
3276
if (exec->transcount >= atom->min) {
3277
int transno = exec->transno;
3278
xmlRegStatePtr state = exec->state;
3281
* The transition is acceptable save it
3283
exec->transno = -1; /* trick */
3285
xmlFARegExecSave(exec);
3286
exec->transno = transno;
3287
exec->state = state;
3289
codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3291
ret = xmlRegCheckCharacter(atom, codepoint);
3294
if (exec->transcount < atom->min)
3298
* If the last check failed but one transition was found
3299
* possible, rollback
3306
if (trans->counter >= 0) {
3307
if (exec->counts == NULL) {
3311
#ifdef DEBUG_REGEXP_EXEC
3312
printf("Decreasing count %d\n", trans->counter);
3314
exec->counts[trans->counter]--;
3316
} else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
3318
* we don't match on the codepoint, but minOccurs of 0
3319
* says that's ok. Setting len to 0 inhibits stepping
3320
* over the codepoint.
3322
exec->transcount = 1;
3326
} else if ((atom->min == 0) && (atom->max > 0)) {
3327
/* another spot to match when minOccurs is 0 */
3328
exec->transcount = 1;
3333
if ((trans->nd == 1) ||
3334
((trans->count >= 0) && (deter == 0) &&
3335
(exec->state->nbTrans > exec->transno + 1))) {
3336
#ifdef DEBUG_REGEXP_EXEC
3338
printf("Saving on nd transition atom %d for %c at %d\n",
3339
trans->atom->no, codepoint, exec->index);
3341
printf("Saving on counted transition count %d for %c at %d\n",
3342
trans->count, codepoint, exec->index);
3344
xmlFARegExecSave(exec);
3346
if (trans->counter >= 0) {
3347
xmlRegCounterPtr counter;
3349
/* make sure we don't go over the counter maximum value */
3350
if ((exec->counts == NULL) ||
3351
(exec->comp == NULL) ||
3352
(exec->comp->counters == NULL)) {
3356
counter = &exec->comp->counters[trans->counter];
3357
if (exec->counts[trans->counter] >= counter->max)
3358
continue; /* for loop on transitions */
3359
#ifdef DEBUG_REGEXP_EXEC
3360
printf("Increasing count %d\n", trans->counter);
3362
exec->counts[trans->counter]++;
3364
if ((trans->count >= 0) &&
3365
(trans->count < REGEXP_ALL_COUNTER)) {
3366
if (exec->counts == NULL) {
3370
#ifdef DEBUG_REGEXP_EXEC
3371
printf("resetting count %d on transition\n",
3374
exec->counts[trans->count] = 0;
3376
#ifdef DEBUG_REGEXP_EXEC
3377
printf("entering state %d\n", trans->to);
3379
exec->state = comp->states[trans->to];
3381
if (trans->atom != NULL) {
3385
} else if (ret < 0) {
3390
if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3393
* Failed to find a way out
3395
exec->determinist = 0;
3396
#ifdef DEBUG_REGEXP_EXEC
3397
printf("rollback from state %d on %d:%c\n", exec->state->no,
3398
codepoint,codepoint);
3400
xmlFARegExecRollBack(exec);
3406
if (exec->rollbacks != NULL) {
3407
if (exec->counts != NULL) {
3410
for (i = 0;i < exec->maxRollbacks;i++)
3411
if (exec->rollbacks[i].counts != NULL)
3412
xmlFree(exec->rollbacks[i].counts);
3414
xmlFree(exec->rollbacks);
3416
if (exec->counts != NULL)
3417
xmlFree(exec->counts);
3418
if (exec->status == 0)
3420
if (exec->status == -1) {
3421
if (exec->nbPush > MAX_PUSH)
3425
return(exec->status);
3428
/************************************************************************
3430
* Progressive interface to the verifier one atom at a time *
3432
************************************************************************/
3434
static void testerr(xmlRegExecCtxtPtr exec);
3438
* xmlRegNewExecCtxt:
3439
* @comp: a precompiled regular expression
3440
* @callback: a callback function used for handling progresses in the
3441
* automata matching phase
3442
* @data: the context data associated to the callback in this context
3444
* Build a context used for progressive evaluation of a regexp.
3446
* Returns the new context
3449
xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
3450
xmlRegExecCtxtPtr exec;
3454
if ((comp->compact == NULL) && (comp->states == NULL))
3456
exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
3458
xmlRegexpErrMemory(NULL, "creating execution context");
3461
memset(exec, 0, sizeof(xmlRegExecCtxt));
3462
exec->inputString = NULL;
3464
exec->determinist = 1;
3465
exec->maxRollbacks = 0;
3466
exec->nbRollbacks = 0;
3467
exec->rollbacks = NULL;
3470
if (comp->compact == NULL)
3471
exec->state = comp->states[0];
3473
exec->transcount = 0;
3474
exec->callback = callback;
3476
if (comp->nbCounters > 0) {
3478
* For error handling, exec->counts is allocated twice the size
3479
* the second half is used to store the data in case of rollback
3481
exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
3483
if (exec->counts == NULL) {
3484
xmlRegexpErrMemory(NULL, "creating execution context");
3488
memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
3489
exec->errCounts = &exec->counts[comp->nbCounters];
3491
exec->counts = NULL;
3492
exec->errCounts = NULL;
3494
exec->inputStackMax = 0;
3495
exec->inputStackNr = 0;
3496
exec->inputStack = NULL;
3497
exec->errStateNo = -1;
3498
exec->errString = NULL;
3504
* xmlRegFreeExecCtxt:
3505
* @exec: a regular expression evaulation context
3507
* Free the structures associated to a regular expression evaulation context.
3510
xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
3514
if (exec->rollbacks != NULL) {
3515
if (exec->counts != NULL) {
3518
for (i = 0;i < exec->maxRollbacks;i++)
3519
if (exec->rollbacks[i].counts != NULL)
3520
xmlFree(exec->rollbacks[i].counts);
3522
xmlFree(exec->rollbacks);
3524
if (exec->counts != NULL)
3525
xmlFree(exec->counts);
3526
if (exec->inputStack != NULL) {
3529
for (i = 0;i < exec->inputStackNr;i++) {
3530
if (exec->inputStack[i].value != NULL)
3531
xmlFree(exec->inputStack[i].value);
3533
xmlFree(exec->inputStack);
3535
if (exec->errString != NULL)
3536
xmlFree(exec->errString);
3541
xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
3544
printf("saving value: %d:%s\n", exec->inputStackNr, value);
3546
if (exec->inputStackMax == 0) {
3547
exec->inputStackMax = 4;
3548
exec->inputStack = (xmlRegInputTokenPtr)
3549
xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
3550
if (exec->inputStack == NULL) {
3551
xmlRegexpErrMemory(NULL, "pushing input string");
3552
exec->inputStackMax = 0;
3555
} else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
3556
xmlRegInputTokenPtr tmp;
3558
exec->inputStackMax *= 2;
3559
tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
3560
exec->inputStackMax * sizeof(xmlRegInputToken));
3562
xmlRegexpErrMemory(NULL, "pushing input string");
3563
exec->inputStackMax /= 2;
3566
exec->inputStack = tmp;
3568
exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
3569
exec->inputStack[exec->inputStackNr].data = data;
3570
exec->inputStackNr++;
3571
exec->inputStack[exec->inputStackNr].value = NULL;
3572
exec->inputStack[exec->inputStackNr].data = NULL;
3576
* xmlRegStrEqualWildcard:
3577
* @expStr: the string to be evaluated
3578
* @valStr: the validation string
3580
* Checks if both strings are equal or have the same content. "*"
3581
* can be used as a wildcard in @valStr; "|" is used as a seperator of
3582
* substrings in both @expStr and @valStr.
3584
* Returns 1 if the comparison is satisfied and the number of substrings
3585
* is equal, 0 otherwise.
3589
xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
3590
if (expStr == valStr) return(1);
3591
if (expStr == NULL) return(0);
3592
if (valStr == NULL) return(0);
3595
* Eval if we have a wildcard for the current item.
3597
if (*expStr != *valStr) {
3598
/* if one of them starts with a wildcard make valStr be it */
3599
if (*valStr == '*') {
3606
if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
3608
if (*valStr == XML_REG_STRING_SEPARATOR)
3611
} while (*valStr != 0);
3618
} while (*valStr != 0);
3626
* xmlRegCompactPushString:
3627
* @exec: a regexp execution context
3628
* @comp: the precompiled exec with a compact table
3629
* @value: a string token input
3630
* @data: data associated to the token to reuse in callbacks
3632
* Push one input token in the execution context
3634
* Returns: 1 if the regexp reached a final state, 0 if non-final, and
3635
* a negative value in case of error.
3638
xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
3640
const xmlChar *value,
3642
int state = exec->index;
3645
if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
3648
if (value == NULL) {
3650
* are we at a final state ?
3652
if (comp->compact[state * (comp->nbstrings + 1)] ==
3653
XML_REGEXP_FINAL_STATE)
3659
printf("value pushed: %s\n", value);
3663
* Examine all outside transitions from current state
3665
for (i = 0;i < comp->nbstrings;i++) {
3666
target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3667
if ((target > 0) && (target <= comp->nbstates)) {
3668
target--; /* to avoid 0 */
3669
if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
3670
exec->index = target;
3671
if ((exec->callback != NULL) && (comp->transdata != NULL)) {
3672
exec->callback(exec->data, value,
3673
comp->transdata[state * comp->nbstrings + i], data);
3676
printf("entering state %d\n", target);
3678
if (comp->compact[target * (comp->nbstrings + 1)] ==
3679
XML_REGEXP_SINK_STATE)
3682
if (comp->compact[target * (comp->nbstrings + 1)] ==
3683
XML_REGEXP_FINAL_STATE)
3690
* Failed to find an exit transition out from current state for the
3694
printf("failed to find a transition for %s on state %d\n", value, state);
3697
if (exec->errString != NULL)
3698
xmlFree(exec->errString);
3699
exec->errString = xmlStrdup(value);
3700
exec->errStateNo = state;
3709
* xmlRegExecPushStringInternal:
3710
* @exec: a regexp execution context or NULL to indicate the end
3711
* @value: a string token input
3712
* @data: data associated to the token to reuse in callbacks
3713
* @compound: value was assembled from 2 strings
3715
* Push one input token in the execution context
3717
* Returns: 1 if the regexp reached a final state, 0 if non-final, and
3718
* a negative value in case of error.
3721
xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
3722
void *data, int compound) {
3723
xmlRegTransPtr trans;
3731
if (exec->comp == NULL)
3733
if (exec->status != 0)
3734
return(exec->status);
3736
if (exec->comp->compact != NULL)
3737
return(xmlRegCompactPushString(exec, exec->comp, value, data));
3739
if (value == NULL) {
3740
if (exec->state->type == XML_REGEXP_FINAL_STATE)
3746
printf("value pushed: %s\n", value);
3749
* If we have an active rollback stack push the new value there
3750
* and get back to where we were left
3752
if ((value != NULL) && (exec->inputStackNr > 0)) {
3753
xmlFARegExecSaveInputString(exec, value, data);
3754
value = exec->inputStack[exec->index].value;
3755
data = exec->inputStack[exec->index].data;
3757
printf("value loaded: %s\n", value);
3761
while ((exec->status == 0) &&
3764
(exec->state->type != XML_REGEXP_FINAL_STATE)))) {
3767
* End of input on non-terminal state, rollback, however we may
3768
* still have epsilon like transition for counted transitions
3769
* on counters, in that case don't break too early.
3771
if ((value == NULL) && (exec->counts == NULL))
3774
exec->transcount = 0;
3775
for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3776
trans = &exec->state->trans[exec->transno];
3781
if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3785
xmlRegCounterPtr counter;
3790
printf("testing all lax %d\n", trans->count);
3793
* Check all counted transitions from the current state
3795
if ((value == NULL) && (final)) {
3797
} else if (value != NULL) {
3798
for (i = 0;i < exec->state->nbTrans;i++) {
3799
t = &exec->state->trans[i];
3800
if ((t->counter < 0) || (t == trans))
3802
counter = &exec->comp->counters[t->counter];
3803
count = exec->counts[t->counter];
3804
if ((count < counter->max) &&
3805
(t->atom != NULL) &&
3806
(xmlStrEqual(value, t->atom->valuep))) {
3810
if ((count >= counter->min) &&
3811
(count < counter->max) &&
3812
(t->atom != NULL) &&
3813
(xmlStrEqual(value, t->atom->valuep))) {
3819
} else if (trans->count == REGEXP_ALL_COUNTER) {
3823
xmlRegCounterPtr counter;
3828
printf("testing all %d\n", trans->count);
3831
* Check all counted transitions from the current state
3833
for (i = 0;i < exec->state->nbTrans;i++) {
3834
t = &exec->state->trans[i];
3835
if ((t->counter < 0) || (t == trans))
3837
counter = &exec->comp->counters[t->counter];
3838
count = exec->counts[t->counter];
3839
if ((count < counter->min) || (count > counter->max)) {
3844
} else if (trans->count >= 0) {
3846
xmlRegCounterPtr counter;
3849
* A counted transition.
3852
count = exec->counts[trans->count];
3853
counter = &exec->comp->counters[trans->count];
3855
printf("testing count %d: val %d, min %d, max %d\n",
3856
trans->count, count, counter->min, counter->max);
3858
ret = ((count >= counter->min) && (count <= counter->max));
3859
} else if (atom == NULL) {
3860
fprintf(stderr, "epsilon transition left at runtime\n");
3863
} else if (value != NULL) {
3864
ret = xmlRegStrEqualWildcard(atom->valuep, value);
3870
if ((ret == 1) && (trans->counter >= 0)) {
3871
xmlRegCounterPtr counter;
3874
count = exec->counts[trans->counter];
3875
counter = &exec->comp->counters[trans->counter];
3876
if (count >= counter->max)
3880
if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3881
xmlRegStatePtr to = exec->comp->states[trans->to];
3884
* this is a multiple input sequence
3886
if (exec->state->nbTrans > exec->transno + 1) {
3887
if (exec->inputStackNr <= 0) {
3888
xmlFARegExecSaveInputString(exec, value, data);
3890
xmlFARegExecSave(exec);
3892
exec->transcount = 1;
3895
* Try to progress as much as possible on the input
3897
if (exec->transcount == atom->max) {
3901
value = exec->inputStack[exec->index].value;
3902
data = exec->inputStack[exec->index].data;
3904
printf("value loaded: %s\n", value);
3908
* End of input: stop here
3910
if (value == NULL) {
3914
if (exec->transcount >= atom->min) {
3915
int transno = exec->transno;
3916
xmlRegStatePtr state = exec->state;
3919
* The transition is acceptable save it
3921
exec->transno = -1; /* trick */
3923
if (exec->inputStackNr <= 0) {
3924
xmlFARegExecSaveInputString(exec, value, data);
3926
xmlFARegExecSave(exec);
3927
exec->transno = transno;
3928
exec->state = state;
3930
ret = xmlStrEqual(value, atom->valuep);
3933
if (exec->transcount < atom->min)
3937
* If the last check failed but one transition was found
3938
* possible, rollback
3948
if ((exec->callback != NULL) && (atom != NULL) &&
3950
exec->callback(exec->data, atom->valuep,
3953
if (exec->state->nbTrans > exec->transno + 1) {
3954
if (exec->inputStackNr <= 0) {
3955
xmlFARegExecSaveInputString(exec, value, data);
3957
xmlFARegExecSave(exec);
3959
if (trans->counter >= 0) {
3961
printf("Increasing count %d\n", trans->counter);
3963
exec->counts[trans->counter]++;
3965
if ((trans->count >= 0) &&
3966
(trans->count < REGEXP_ALL_COUNTER)) {
3967
#ifdef DEBUG_REGEXP_EXEC
3968
printf("resetting count %d on transition\n",
3971
exec->counts[trans->count] = 0;
3974
printf("entering state %d\n", trans->to);
3976
if ((exec->comp->states[trans->to] != NULL) &&
3977
(exec->comp->states[trans->to]->type ==
3978
XML_REGEXP_SINK_STATE)) {
3980
* entering a sink state, save the current state as error
3983
if (exec->errString != NULL)
3984
xmlFree(exec->errString);
3985
exec->errString = xmlStrdup(value);
3986
exec->errState = exec->state;
3987
memcpy(exec->errCounts, exec->counts,
3988
exec->comp->nbCounters * sizeof(int));
3990
exec->state = exec->comp->states[trans->to];
3992
if (trans->atom != NULL) {
3993
if (exec->inputStack != NULL) {
3995
if (exec->index < exec->inputStackNr) {
3996
value = exec->inputStack[exec->index].value;
3997
data = exec->inputStack[exec->index].data;
3999
printf("value loaded: %s\n", value);
4005
printf("end of input\n");
4012
printf("end of input\n");
4017
} else if (ret < 0) {
4022
if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4025
* if we didn't yet rollback on the current input
4026
* store the current state as the error state.
4028
if ((progress) && (exec->state != NULL) &&
4029
(exec->state->type != XML_REGEXP_SINK_STATE)) {
4031
if (exec->errString != NULL)
4032
xmlFree(exec->errString);
4033
exec->errString = xmlStrdup(value);
4034
exec->errState = exec->state;
4035
memcpy(exec->errCounts, exec->counts,
4036
exec->comp->nbCounters * sizeof(int));
4040
* Failed to find a way out
4042
exec->determinist = 0;
4043
xmlFARegExecRollBack(exec);
4044
if (exec->status == 0) {
4045
value = exec->inputStack[exec->index].value;
4046
data = exec->inputStack[exec->index].data;
4048
printf("value loaded: %s\n", value);
4057
if (exec->status == 0) {
4058
return(exec->state->type == XML_REGEXP_FINAL_STATE);
4061
if (exec->status < 0) {
4065
return(exec->status);
4069
* xmlRegExecPushString:
4070
* @exec: a regexp execution context or NULL to indicate the end
4071
* @value: a string token input
4072
* @data: data associated to the token to reuse in callbacks
4074
* Push one input token in the execution context
4076
* Returns: 1 if the regexp reached a final state, 0 if non-final, and
4077
* a negative value in case of error.
4080
xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
4082
return(xmlRegExecPushStringInternal(exec, value, data, 0));
4086
* xmlRegExecPushString2:
4087
* @exec: a regexp execution context or NULL to indicate the end
4088
* @value: the first string token input
4089
* @value2: the second string token input
4090
* @data: data associated to the token to reuse in callbacks
4092
* Push one input token in the execution context
4094
* Returns: 1 if the regexp reached a final state, 0 if non-final, and
4095
* a negative value in case of error.
4098
xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
4099
const xmlChar *value2, void *data) {
4101
int lenn, lenp, ret;
4106
if (exec->comp == NULL)
4108
if (exec->status != 0)
4109
return(exec->status);
4112
return(xmlRegExecPushString(exec, value, data));
4114
lenn = strlen((char *) value2);
4115
lenp = strlen((char *) value);
4117
if (150 < lenn + lenp + 2) {
4118
str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4126
memcpy(&str[0], value, lenp);
4127
str[lenp] = XML_REG_STRING_SEPARATOR;
4128
memcpy(&str[lenp + 1], value2, lenn);
4129
str[lenn + lenp + 1] = 0;
4131
if (exec->comp->compact != NULL)
4132
ret = xmlRegCompactPushString(exec, exec->comp, str, data);
4134
ret = xmlRegExecPushStringInternal(exec, str, data, 1);
4142
* xmlRegExecGetValues:
4143
* @exec: a regexp execution context
4144
* @err: error extraction or normal one
4145
* @nbval: pointer to the number of accepted values IN/OUT
4146
* @nbneg: return number of negative transitions
4147
* @values: pointer to the array of acceptable values
4148
* @terminal: return value if this was a terminal state
4150
* Extract informations from the regexp execution, internal routine to
4151
* implement xmlRegExecNextValues() and xmlRegExecErrInfo()
4153
* Returns: 0 in case of success or -1 in case of error.
4156
xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
4157
int *nbval, int *nbneg,
4158
xmlChar **values, int *terminal) {
4162
if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
4163
(values == NULL) || (*nbval <= 0))
4169
if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
4171
int target, i, state;
4176
if (exec->errStateNo == -1) return(-1);
4177
state = exec->errStateNo;
4179
state = exec->index;
4181
if (terminal != NULL) {
4182
if (comp->compact[state * (comp->nbstrings + 1)] ==
4183
XML_REGEXP_FINAL_STATE)
4188
for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4189
target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4190
if ((target > 0) && (target <= comp->nbstates) &&
4191
(comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
4192
XML_REGEXP_SINK_STATE)) {
4193
values[nb++] = comp->stringMap[i];
4197
for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
4198
target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
4199
if ((target > 0) && (target <= comp->nbstates) &&
4200
(comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
4201
XML_REGEXP_SINK_STATE)) {
4202
values[nb++] = comp->stringMap[i];
4208
xmlRegTransPtr trans;
4210
xmlRegStatePtr state;
4212
if (terminal != NULL) {
4213
if (exec->state->type == XML_REGEXP_FINAL_STATE)
4220
if (exec->errState == NULL) return(-1);
4221
state = exec->errState;
4223
if (exec->state == NULL) return(-1);
4224
state = exec->state;
4227
(transno < state->nbTrans) && (nb < maxval);
4229
trans = &state->trans[transno];
4233
if ((atom == NULL) || (atom->valuep == NULL))
4235
if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4236
/* this should not be reached but ... */
4238
} else if (trans->count == REGEXP_ALL_COUNTER) {
4239
/* this should not be reached but ... */
4241
} else if (trans->counter >= 0) {
4242
xmlRegCounterPtr counter = NULL;
4246
count = exec->errCounts[trans->counter];
4248
count = exec->counts[trans->counter];
4249
if (exec->comp != NULL)
4250
counter = &exec->comp->counters[trans->counter];
4251
if ((counter == NULL) || (count < counter->max)) {
4253
values[nb++] = (xmlChar *) atom->valuep2;
4255
values[nb++] = (xmlChar *) atom->valuep;
4259
if ((exec->comp->states[trans->to] != NULL) &&
4260
(exec->comp->states[trans->to]->type !=
4261
XML_REGEXP_SINK_STATE)) {
4263
values[nb++] = (xmlChar *) atom->valuep2;
4265
values[nb++] = (xmlChar *) atom->valuep;
4271
(transno < state->nbTrans) && (nb < maxval);
4273
trans = &state->trans[transno];
4277
if ((atom == NULL) || (atom->valuep == NULL))
4279
if (trans->count == REGEXP_ALL_LAX_COUNTER) {
4281
} else if (trans->count == REGEXP_ALL_COUNTER) {
4283
} else if (trans->counter >= 0) {
4286
if ((exec->comp->states[trans->to] != NULL) &&
4287
(exec->comp->states[trans->to]->type ==
4288
XML_REGEXP_SINK_STATE)) {
4290
values[nb++] = (xmlChar *) atom->valuep2;
4292
values[nb++] = (xmlChar *) atom->valuep;
4302
* xmlRegExecNextValues:
4303
* @exec: a regexp execution context
4304
* @nbval: pointer to the number of accepted values IN/OUT
4305
* @nbneg: return number of negative transitions
4306
* @values: pointer to the array of acceptable values
4307
* @terminal: return value if this was a terminal state
4309
* Extract informations from the regexp execution,
4310
* the parameter @values must point to an array of @nbval string pointers
4311
* on return nbval will contain the number of possible strings in that
4312
* state and the @values array will be updated with them. The string values
4313
* returned will be freed with the @exec context and don't need to be
4316
* Returns: 0 in case of success or -1 in case of error.
4319
xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
4320
xmlChar **values, int *terminal) {
4321
return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
4325
* xmlRegExecErrInfo:
4326
* @exec: a regexp execution context generating an error
4327
* @string: return value for the error string
4328
* @nbval: pointer to the number of accepted values IN/OUT
4329
* @nbneg: return number of negative transitions
4330
* @values: pointer to the array of acceptable values
4331
* @terminal: return value if this was a terminal state
4333
* Extract error informations from the regexp execution, the parameter
4334
* @string will be updated with the value pushed and not accepted,
4335
* the parameter @values must point to an array of @nbval string pointers
4336
* on return nbval will contain the number of possible strings in that
4337
* state and the @values array will be updated with them. The string values
4338
* returned will be freed with the @exec context and don't need to be
4341
* Returns: 0 in case of success or -1 in case of error.
4344
xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
4345
int *nbval, int *nbneg, xmlChar **values, int *terminal) {
4348
if (string != NULL) {
4349
if (exec->status != 0)
4350
*string = exec->errString;
4354
return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
4358
static void testerr(xmlRegExecCtxtPtr exec) {
4359
const xmlChar *string;
4364
xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
4370
xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
4371
xmlRegTransPtr trans;
4378
if (exec->status != 0)
4379
return(exec->status);
4381
while ((exec->status == 0) &&
4382
((exec->inputString[exec->index] != 0) ||
4383
(exec->state->type != XML_REGEXP_FINAL_STATE))) {
4386
* End of input on non-terminal state, rollback, however we may
4387
* still have epsilon like transition for counted transitions
4388
* on counters, in that case don't break too early.
4390
if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
4393
exec->transcount = 0;
4394
for (;exec->transno < exec->state->nbTrans;exec->transno++) {
4395
trans = &exec->state->trans[exec->transno];
4400
if (trans->count >= 0) {
4402
xmlRegCounterPtr counter;
4405
* A counted transition.
4408
count = exec->counts[trans->count];
4409
counter = &exec->comp->counters[trans->count];
4410
#ifdef DEBUG_REGEXP_EXEC
4411
printf("testing count %d: val %d, min %d, max %d\n",
4412
trans->count, count, counter->min, counter->max);
4414
ret = ((count >= counter->min) && (count <= counter->max));
4415
} else if (atom == NULL) {
4416
fprintf(stderr, "epsilon transition left at runtime\n");
4419
} else if (exec->inputString[exec->index] != 0) {
4420
codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
4421
ret = xmlRegCheckCharacter(atom, codepoint);
4422
if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
4423
xmlRegStatePtr to = exec->comp->states[trans->to];
4426
* this is a multiple input sequence
4428
if (exec->state->nbTrans > exec->transno + 1) {
4429
xmlFARegExecSave(exec);
4431
exec->transcount = 1;
4434
* Try to progress as much as possible on the input
4436
if (exec->transcount == atom->max) {
4441
* End of input: stop here
4443
if (exec->inputString[exec->index] == 0) {
4447
if (exec->transcount >= atom->min) {
4448
int transno = exec->transno;
4449
xmlRegStatePtr state = exec->state;
4452
* The transition is acceptable save it
4454
exec->transno = -1; /* trick */
4456
xmlFARegExecSave(exec);
4457
exec->transno = transno;
4458
exec->state = state;
4460
codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
4462
ret = xmlRegCheckCharacter(atom, codepoint);
4465
if (exec->transcount < atom->min)
4469
* If the last check failed but one transition was found
4470
* possible, rollback
4480
if (exec->state->nbTrans > exec->transno + 1) {
4481
xmlFARegExecSave(exec);
4484
* restart count for expressions like this ((abc){2})*
4486
if (trans->count >= 0) {
4487
#ifdef DEBUG_REGEXP_EXEC
4488
printf("Reset count %d\n", trans->count);
4490
exec->counts[trans->count] = 0;
4492
if (trans->counter >= 0) {
4493
#ifdef DEBUG_REGEXP_EXEC
4494
printf("Increasing count %d\n", trans->counter);
4496
exec->counts[trans->counter]++;
4498
#ifdef DEBUG_REGEXP_EXEC
4499
printf("entering state %d\n", trans->to);
4501
exec->state = exec->comp->states[trans->to];
4503
if (trans->atom != NULL) {
4507
} else if (ret < 0) {
4512
if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
4515
* Failed to find a way out
4517
exec->determinist = 0;
4518
xmlFARegExecRollBack(exec);
4525
/************************************************************************
4527
* Parser for the Schemas Datatype Regular Expressions *
4528
* http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
4530
************************************************************************/
4534
* @ctxt: a regexp parser context
4536
* [10] Char ::= [^.\?*+()|#x5B#x5D]
4539
xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
4543
cur = CUR_SCHAR(ctxt->cur, len);
4544
if ((cur == '.') || (cur == '\\') || (cur == '?') ||
4545
(cur == '*') || (cur == '+') || (cur == '(') ||
4546
(cur == ')') || (cur == '|') || (cur == 0x5B) ||
4547
(cur == 0x5D) || (cur == 0))
4553
* xmlFAParseCharProp:
4554
* @ctxt: a regexp parser context
4556
* [27] charProp ::= IsCategory | IsBlock
4557
* [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
4558
* Separators | Symbols | Others
4559
* [29] Letters ::= 'L' [ultmo]?
4560
* [30] Marks ::= 'M' [nce]?
4561
* [31] Numbers ::= 'N' [dlo]?
4562
* [32] Punctuation ::= 'P' [cdseifo]?
4563
* [33] Separators ::= 'Z' [slp]?
4564
* [34] Symbols ::= 'S' [mcko]?
4565
* [35] Others ::= 'C' [cfon]?
4566
* [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
4569
xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
4571
xmlRegAtomType type = (xmlRegAtomType) 0;
4572
xmlChar *blockName = NULL;
4580
type = XML_REGEXP_LETTER_UPPERCASE;
4581
} else if (cur == 'l') {
4583
type = XML_REGEXP_LETTER_LOWERCASE;
4584
} else if (cur == 't') {
4586
type = XML_REGEXP_LETTER_TITLECASE;
4587
} else if (cur == 'm') {
4589
type = XML_REGEXP_LETTER_MODIFIER;
4590
} else if (cur == 'o') {
4592
type = XML_REGEXP_LETTER_OTHERS;
4594
type = XML_REGEXP_LETTER;
4596
} else if (cur == 'M') {
4602
type = XML_REGEXP_MARK_NONSPACING;
4603
} else if (cur == 'c') {
4605
/* spacing combining */
4606
type = XML_REGEXP_MARK_SPACECOMBINING;
4607
} else if (cur == 'e') {
4610
type = XML_REGEXP_MARK_ENCLOSING;
4613
type = XML_REGEXP_MARK;
4615
} else if (cur == 'N') {
4621
type = XML_REGEXP_NUMBER_DECIMAL;
4622
} else if (cur == 'l') {
4625
type = XML_REGEXP_NUMBER_LETTER;
4626
} else if (cur == 'o') {
4629
type = XML_REGEXP_NUMBER_OTHERS;
4632
type = XML_REGEXP_NUMBER;
4634
} else if (cur == 'P') {
4640
type = XML_REGEXP_PUNCT_CONNECTOR;
4641
} else if (cur == 'd') {
4644
type = XML_REGEXP_PUNCT_DASH;
4645
} else if (cur == 's') {
4648
type = XML_REGEXP_PUNCT_OPEN;
4649
} else if (cur == 'e') {
4652
type = XML_REGEXP_PUNCT_CLOSE;
4653
} else if (cur == 'i') {
4656
type = XML_REGEXP_PUNCT_INITQUOTE;
4657
} else if (cur == 'f') {
4660
type = XML_REGEXP_PUNCT_FINQUOTE;
4661
} else if (cur == 'o') {
4664
type = XML_REGEXP_PUNCT_OTHERS;
4666
/* all punctuation */
4667
type = XML_REGEXP_PUNCT;
4669
} else if (cur == 'Z') {
4675
type = XML_REGEXP_SEPAR_SPACE;
4676
} else if (cur == 'l') {
4679
type = XML_REGEXP_SEPAR_LINE;
4680
} else if (cur == 'p') {
4683
type = XML_REGEXP_SEPAR_PARA;
4685
/* all separators */
4686
type = XML_REGEXP_SEPAR;
4688
} else if (cur == 'S') {
4693
type = XML_REGEXP_SYMBOL_MATH;
4695
} else if (cur == 'c') {
4697
type = XML_REGEXP_SYMBOL_CURRENCY;
4699
} else if (cur == 'k') {
4701
type = XML_REGEXP_SYMBOL_MODIFIER;
4703
} else if (cur == 'o') {
4705
type = XML_REGEXP_SYMBOL_OTHERS;
4709
type = XML_REGEXP_SYMBOL;
4711
} else if (cur == 'C') {
4717
type = XML_REGEXP_OTHER_CONTROL;
4718
} else if (cur == 'f') {
4721
type = XML_REGEXP_OTHER_FORMAT;
4722
} else if (cur == 'o') {
4725
type = XML_REGEXP_OTHER_PRIVATE;
4726
} else if (cur == 'n') {
4729
type = XML_REGEXP_OTHER_NA;
4732
type = XML_REGEXP_OTHER;
4734
} else if (cur == 'I') {
4735
const xmlChar *start;
4739
ERROR("IsXXXX expected");
4745
if (((cur >= 'a') && (cur <= 'z')) ||
4746
((cur >= 'A') && (cur <= 'Z')) ||
4747
((cur >= '0') && (cur <= '9')) ||
4751
while (((cur >= 'a') && (cur <= 'z')) ||
4752
((cur >= 'A') && (cur <= 'Z')) ||
4753
((cur >= '0') && (cur <= '9')) ||
4759
type = XML_REGEXP_BLOCK_NAME;
4760
blockName = xmlStrndup(start, ctxt->cur - start);
4762
ERROR("Unknown char property");
4765
if (ctxt->atom == NULL) {
4766
ctxt->atom = xmlRegNewAtom(ctxt, type);
4767
if (ctxt->atom != NULL)
4768
ctxt->atom->valuep = blockName;
4769
} else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4770
xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4771
type, 0, 0, blockName);
4776
* xmlFAParseCharClassEsc:
4777
* @ctxt: a regexp parser context
4779
* [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
4780
* [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
4781
* [25] catEsc ::= '\p{' charProp '}'
4782
* [26] complEsc ::= '\P{' charProp '}'
4783
* [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
4786
xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
4790
if (ctxt->atom == NULL) {
4791
ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
4792
} else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4793
xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4794
XML_REGEXP_ANYCHAR, 0, 0, NULL);
4800
ERROR("Escaped sequence: expecting \\");
4808
ERROR("Expecting '{'");
4812
xmlFAParseCharProp(ctxt);
4814
ERROR("Expecting '}'");
4818
} else if (cur == 'P') {
4821
ERROR("Expecting '{'");
4825
xmlFAParseCharProp(ctxt);
4826
ctxt->atom->neg = 1;
4828
ERROR("Expecting '}'");
4832
} else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
4833
(cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
4834
(cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
4835
(cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
4837
if (ctxt->atom == NULL) {
4838
ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4839
if (ctxt->atom != NULL) {
4842
ctxt->atom->codepoint = '\n';
4845
ctxt->atom->codepoint = '\r';
4848
ctxt->atom->codepoint = '\t';
4851
ctxt->atom->codepoint = cur;
4854
} else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4855
xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4856
XML_REGEXP_CHARVAL, cur, cur, NULL);
4859
} else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
4860
(cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
4861
(cur == 'w') || (cur == 'W')) {
4862
xmlRegAtomType type = XML_REGEXP_ANYSPACE;
4866
type = XML_REGEXP_ANYSPACE;
4869
type = XML_REGEXP_NOTSPACE;
4872
type = XML_REGEXP_INITNAME;
4875
type = XML_REGEXP_NOTINITNAME;
4878
type = XML_REGEXP_NAMECHAR;
4881
type = XML_REGEXP_NOTNAMECHAR;
4884
type = XML_REGEXP_DECIMAL;
4887
type = XML_REGEXP_NOTDECIMAL;
4890
type = XML_REGEXP_REALCHAR;
4893
type = XML_REGEXP_NOTREALCHAR;
4897
if (ctxt->atom == NULL) {
4898
ctxt->atom = xmlRegNewAtom(ctxt, type);
4899
} else if (ctxt->atom->type == XML_REGEXP_RANGES) {
4900
xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4904
ERROR("Wrong escape sequence, misuse of character '\\'");
4909
* xmlFAParseCharRef:
4910
* @ctxt: a regexp parser context
4912
* [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
4915
xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
4918
if ((CUR != '&') || (NXT(1) != '#'))
4926
if (((cur >= '0') && (cur <= '9')) ||
4927
((cur >= 'a') && (cur <= 'f')) ||
4928
((cur >= 'A') && (cur <= 'F'))) {
4929
while (((cur >= '0') && (cur <= '9')) ||
4930
((cur >= 'a') && (cur <= 'f')) ||
4931
((cur >= 'A') && (cur <= 'F'))) {
4932
if ((cur >= '0') && (cur <= '9'))
4933
ret = ret * 16 + cur - '0';
4934
else if ((cur >= 'a') && (cur <= 'f'))
4935
ret = ret * 16 + 10 + (cur - 'a');
4937
ret = ret * 16 + 10 + (cur - 'A');
4942
ERROR("Char ref: expecting [0-9A-F]");
4946
if ((cur >= '0') && (cur <= '9')) {
4947
while ((cur >= '0') && (cur <= '9')) {
4948
ret = ret * 10 + cur - '0';
4953
ERROR("Char ref: expecting [0-9]");
4958
ERROR("Char ref: expecting ';'");
4967
* xmlFAParseCharRange:
4968
* @ctxt: a regexp parser context
4970
* [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
4971
* [18] seRange ::= charOrEsc '-' charOrEsc
4972
* [20] charOrEsc ::= XmlChar | SingleCharEsc
4973
* [21] XmlChar ::= [^\#x2D#x5B#x5D]
4974
* [22] XmlCharIncDash ::= [^\#x5B#x5D]
4977
xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
4983
ERROR("Expecting ']'");
4987
if ((CUR == '&') && (NXT(1) == '#')) {
4988
end = start = xmlFAParseCharRef(ctxt);
4989
xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
4990
XML_REGEXP_CHARVAL, start, end, NULL);
4998
case 'n': start = 0xA; break;
4999
case 'r': start = 0xD; break;
5000
case 't': start = 0x9; break;
5001
case '\\': case '|': case '.': case '-': case '^': case '?':
5002
case '*': case '+': case '{': case '}': case '(': case ')':
5006
ERROR("Invalid escape value");
5011
} else if ((cur != 0x5B) && (cur != 0x5D)) {
5012
end = start = CUR_SCHAR(ctxt->cur, len);
5014
ERROR("Expecting a char range");
5018
* Since we are "inside" a range, we can assume ctxt->cur is past
5019
* the start of ctxt->string, and PREV should be safe
5021
if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
5027
if ((cur != '-') || (NXT(1) == ']')) {
5028
xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5029
XML_REGEXP_CHARVAL, start, end, NULL);
5038
case 'n': end = 0xA; break;
5039
case 'r': end = 0xD; break;
5040
case 't': end = 0x9; break;
5041
case '\\': case '|': case '.': case '-': case '^': case '?':
5042
case '*': case '+': case '{': case '}': case '(': case ')':
5046
ERROR("Invalid escape value");
5050
} else if ((cur != 0x5B) && (cur != 0x5D)) {
5051
end = CUR_SCHAR(ctxt->cur, len);
5053
ERROR("Expecting the end of a char range");
5057
/* TODO check that the values are acceptable character ranges for XML */
5059
ERROR("End of range is before start of range");
5061
xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
5062
XML_REGEXP_CHARVAL, start, end, NULL);
5068
* xmlFAParsePosCharGroup:
5069
* @ctxt: a regexp parser context
5071
* [14] posCharGroup ::= ( charRange | charClassEsc )+
5074
xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
5077
xmlFAParseCharClassEsc(ctxt);
5079
xmlFAParseCharRange(ctxt);
5081
} while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
5082
(CUR != 0) && (ctxt->error == 0));
5086
* xmlFAParseCharGroup:
5087
* @ctxt: a regexp parser context
5089
* [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
5090
* [15] negCharGroup ::= '^' posCharGroup
5091
* [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
5092
* [12] charClassExpr ::= '[' charGroup ']'
5095
xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
5097
while ((CUR != ']') && (ctxt->error == 0)) {
5099
int neg = ctxt->neg;
5102
ctxt->neg = !ctxt->neg;
5103
xmlFAParsePosCharGroup(ctxt);
5105
} else if ((CUR == '-') && (NXT(1) == '[')) {
5106
int neg = ctxt->neg;
5108
NEXT; /* eat the '-' */
5109
NEXT; /* eat the '[' */
5110
xmlFAParseCharGroup(ctxt);
5114
ERROR("charClassExpr: ']' expected");
5119
} else if (CUR != ']') {
5120
xmlFAParsePosCharGroup(ctxt);
5127
* xmlFAParseCharClass:
5128
* @ctxt: a regexp parser context
5130
* [11] charClass ::= charClassEsc | charClassExpr
5131
* [12] charClassExpr ::= '[' charGroup ']'
5134
xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
5137
ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
5138
if (ctxt->atom == NULL)
5140
xmlFAParseCharGroup(ctxt);
5144
ERROR("xmlFAParseCharClass: ']' expected");
5147
xmlFAParseCharClassEsc(ctxt);
5152
* xmlFAParseQuantExact:
5153
* @ctxt: a regexp parser context
5155
* [8] QuantExact ::= [0-9]+
5157
* Returns 0 if success or -1 in case of error
5160
xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
5164
while ((CUR >= '0') && (CUR <= '9')) {
5165
ret = ret * 10 + (CUR - '0');
5176
* xmlFAParseQuantifier:
5177
* @ctxt: a regexp parser context
5179
* [4] quantifier ::= [?*+] | ( '{' quantity '}' )
5180
* [5] quantity ::= quantRange | quantMin | QuantExact
5181
* [6] quantRange ::= QuantExact ',' QuantExact
5182
* [7] quantMin ::= QuantExact ','
5183
* [8] QuantExact ::= [0-9]+
5186
xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
5190
if ((cur == '?') || (cur == '*') || (cur == '+')) {
5191
if (ctxt->atom != NULL) {
5193
ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
5194
else if (cur == '*')
5195
ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
5196
else if (cur == '+')
5197
ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
5203
int min = 0, max = 0;
5206
cur = xmlFAParseQuantExact(ctxt);
5214
cur = xmlFAParseQuantExact(ctxt);
5218
ERROR("Improper quantifier");
5225
ERROR("Unterminated quantifier");
5229
if (ctxt->atom != NULL) {
5230
ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
5231
ctxt->atom->min = min;
5232
ctxt->atom->max = max;
5241
* @ctxt: a regexp parser context
5243
* [9] atom ::= Char | charClass | ( '(' regExp ')' )
5246
xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
5249
codepoint = xmlFAIsChar(ctxt);
5250
if (codepoint > 0) {
5251
ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
5252
if (ctxt->atom == NULL)
5254
codepoint = CUR_SCHAR(ctxt->cur, len);
5255
ctxt->atom->codepoint = codepoint;
5258
} else if (CUR == '|') {
5260
} else if (CUR == 0) {
5262
} else if (CUR == ')') {
5264
} else if (CUR == '(') {
5265
xmlRegStatePtr start, oldend, start0;
5269
* this extra Epsilon transition is needed if we count with 0 allowed
5270
* unfortunately this can't be known at that point
5272
xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5273
start0 = ctxt->state;
5274
xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
5275
start = ctxt->state;
5279
xmlFAParseRegExp(ctxt, 0);
5283
ERROR("xmlFAParseAtom: expecting ')'");
5285
ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
5286
if (ctxt->atom == NULL)
5288
ctxt->atom->start = start;
5289
ctxt->atom->start0 = start0;
5290
ctxt->atom->stop = ctxt->state;
5293
} else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
5294
xmlFAParseCharClass(ctxt);
5302
* @ctxt: a regexp parser context
5304
* [3] piece ::= atom quantifier?
5307
xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
5311
ret = xmlFAParseAtom(ctxt);
5314
if (ctxt->atom == NULL) {
5315
ERROR("internal: no atom generated");
5317
xmlFAParseQuantifier(ctxt);
5323
* @ctxt: a regexp parser context
5324
* @to: optional target to the end of the branch
5326
* @to is used to optimize by removing duplicate path in automata
5327
* in expressions like (a|b)(c|d)
5329
* [2] branch ::= piece*
5332
xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
5333
xmlRegStatePtr previous;
5336
previous = ctxt->state;
5337
ret = xmlFAParsePiece(ctxt);
5339
if (xmlFAGenerateTransitions(ctxt, previous,
5340
(CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
5342
previous = ctxt->state;
5345
while ((ret != 0) && (ctxt->error == 0)) {
5346
ret = xmlFAParsePiece(ctxt);
5348
if (xmlFAGenerateTransitions(ctxt, previous,
5349
(CUR=='|' || CUR==')') ? to : NULL, ctxt->atom) < 0)
5351
previous = ctxt->state;
5360
* @ctxt: a regexp parser context
5361
* @top: is this the top-level expression ?
5363
* [1] regExp ::= branch ( '|' branch )*
5366
xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
5367
xmlRegStatePtr start, end;
5369
/* if not top start should have been generated by an epsilon trans */
5370
start = ctxt->state;
5372
xmlFAParseBranch(ctxt, NULL);
5374
#ifdef DEBUG_REGEXP_GRAPH
5375
printf("State %d is final\n", ctxt->state->no);
5377
ctxt->state->type = XML_REGEXP_FINAL_STATE;
5380
ctxt->end = ctxt->state;
5384
while ((CUR == '|') && (ctxt->error == 0)) {
5386
ctxt->state = start;
5388
xmlFAParseBranch(ctxt, end);
5396
/************************************************************************
5400
************************************************************************/
5404
* @output: the file for the output debug
5405
* @regexp: the compiled regexp
5407
* Print the content of the compiled regular expression
5410
xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
5415
fprintf(output, " regexp: ");
5416
if (regexp == NULL) {
5417
fprintf(output, "NULL\n");
5420
fprintf(output, "'%s' ", regexp->string);
5421
fprintf(output, "\n");
5422
fprintf(output, "%d atoms:\n", regexp->nbAtoms);
5423
for (i = 0;i < regexp->nbAtoms; i++) {
5424
fprintf(output, " %02d ", i);
5425
xmlRegPrintAtom(output, regexp->atoms[i]);
5427
fprintf(output, "%d states:", regexp->nbStates);
5428
fprintf(output, "\n");
5429
for (i = 0;i < regexp->nbStates; i++) {
5430
xmlRegPrintState(output, regexp->states[i]);
5432
fprintf(output, "%d counters:\n", regexp->nbCounters);
5433
for (i = 0;i < regexp->nbCounters; i++) {
5434
fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
5435
regexp->counters[i].max);
5441
* @regexp: a regular expression string
5443
* Parses a regular expression conforming to XML Schemas Part 2 Datatype
5444
* Appendix F and builds an automata suitable for testing strings against
5445
* that regular expression
5447
* Returns the compiled expression or NULL in case of error
5450
xmlRegexpCompile(const xmlChar *regexp) {
5452
xmlRegParserCtxtPtr ctxt;
5454
ctxt = xmlRegNewParserCtxt(regexp);
5458
/* initialize the parser */
5460
ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5461
xmlRegStatePush(ctxt, ctxt->start);
5463
/* parse the expression building an automata */
5464
xmlFAParseRegExp(ctxt, 1);
5466
ERROR("xmlFAParseRegExp: extra characters");
5468
if (ctxt->error != 0) {
5469
xmlRegFreeParserCtxt(ctxt);
5472
ctxt->end = ctxt->state;
5473
ctxt->start->type = XML_REGEXP_START_STATE;
5474
ctxt->end->type = XML_REGEXP_FINAL_STATE;
5476
/* remove the Epsilon except for counted transitions */
5477
xmlFAEliminateEpsilonTransitions(ctxt);
5480
if (ctxt->error != 0) {
5481
xmlRegFreeParserCtxt(ctxt);
5484
ret = xmlRegEpxFromParse(ctxt);
5485
xmlRegFreeParserCtxt(ctxt);
5491
* @comp: the compiled regular expression
5492
* @content: the value to check against the regular expression
5494
* Check if the regular expression generates the value
5496
* Returns 1 if it matches, 0 if not and a negative value in case of error
5499
xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
5500
if ((comp == NULL) || (content == NULL))
5502
return(xmlFARegExec(comp, content));
5506
* xmlRegexpIsDeterminist:
5507
* @comp: the compiled regular expression
5509
* Check if the regular expression is determinist
5511
* Returns 1 if it yes, 0 if not and a negative value in case of error
5514
xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
5520
if (comp->determinist != -1)
5521
return(comp->determinist);
5523
am = xmlNewAutomata();
5524
if (am->states != NULL) {
5527
for (i = 0;i < am->nbStates;i++)
5528
xmlRegFreeState(am->states[i]);
5529
xmlFree(am->states);
5531
am->nbAtoms = comp->nbAtoms;
5532
am->atoms = comp->atoms;
5533
am->nbStates = comp->nbStates;
5534
am->states = comp->states;
5535
am->determinist = -1;
5536
ret = xmlFAComputesDeterminism(am);
5539
xmlFreeAutomata(am);
5545
* @regexp: the regexp
5550
xmlRegFreeRegexp(xmlRegexpPtr regexp) {
5555
if (regexp->string != NULL)
5556
xmlFree(regexp->string);
5557
if (regexp->states != NULL) {
5558
for (i = 0;i < regexp->nbStates;i++)
5559
xmlRegFreeState(regexp->states[i]);
5560
xmlFree(regexp->states);
5562
if (regexp->atoms != NULL) {
5563
for (i = 0;i < regexp->nbAtoms;i++)
5564
xmlRegFreeAtom(regexp->atoms[i]);
5565
xmlFree(regexp->atoms);
5567
if (regexp->counters != NULL)
5568
xmlFree(regexp->counters);
5569
if (regexp->compact != NULL)
5570
xmlFree(regexp->compact);
5571
if (regexp->transdata != NULL)
5572
xmlFree(regexp->transdata);
5573
if (regexp->stringMap != NULL) {
5574
for (i = 0; i < regexp->nbstrings;i++)
5575
xmlFree(regexp->stringMap[i]);
5576
xmlFree(regexp->stringMap);
5582
#ifdef LIBXML_AUTOMATA_ENABLED
5583
/************************************************************************
5585
* The Automata interface *
5587
************************************************************************/
5592
* Create a new automata
5594
* Returns the new object or NULL in case of failure
5597
xmlNewAutomata(void) {
5598
xmlAutomataPtr ctxt;
5600
ctxt = xmlRegNewParserCtxt(NULL);
5604
/* initialize the parser */
5606
ctxt->start = ctxt->state = xmlRegNewState(ctxt);
5607
if (ctxt->start == NULL) {
5608
xmlFreeAutomata(ctxt);
5611
ctxt->start->type = XML_REGEXP_START_STATE;
5612
if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
5613
xmlRegFreeState(ctxt->start);
5614
xmlFreeAutomata(ctxt);
5628
xmlFreeAutomata(xmlAutomataPtr am) {
5631
xmlRegFreeParserCtxt(am);
5635
* xmlAutomataGetInitState:
5638
* Initial state lookup
5640
* Returns the initial state of the automata
5643
xmlAutomataGetInitState(xmlAutomataPtr am) {
5650
* xmlAutomataSetFinalState:
5652
* @state: a state in this automata
5654
* Makes that state a final state
5656
* Returns 0 or -1 in case of error
5659
xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
5660
if ((am == NULL) || (state == NULL))
5662
state->type = XML_REGEXP_FINAL_STATE;
5667
* xmlAutomataNewTransition:
5669
* @from: the starting point of the transition
5670
* @to: the target point of the transition or NULL
5671
* @token: the input string associated to that transition
5672
* @data: data passed to the callback function if the transition is activated
5674
* If @to is NULL, this creates first a new target state in the automata
5675
* and then adds a transition from the @from state to the target state
5676
* activated by the value of @token
5678
* Returns the target state or NULL in case of error
5681
xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
5682
xmlAutomataStatePtr to, const xmlChar *token,
5686
if ((am == NULL) || (from == NULL) || (token == NULL))
5688
atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5694
atom->valuep = xmlStrdup(token);
5696
if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5697
xmlRegFreeAtom(atom);
5706
* xmlAutomataNewTransition2:
5708
* @from: the starting point of the transition
5709
* @to: the target point of the transition or NULL
5710
* @token: the first input string associated to that transition
5711
* @token2: the second input string associated to that transition
5712
* @data: data passed to the callback function if the transition is activated
5714
* If @to is NULL, this creates first a new target state in the automata
5715
* and then adds a transition from the @from state to the target state
5716
* activated by the value of @token
5718
* Returns the target state or NULL in case of error
5721
xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5722
xmlAutomataStatePtr to, const xmlChar *token,
5723
const xmlChar *token2, void *data) {
5726
if ((am == NULL) || (from == NULL) || (token == NULL))
5728
atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5732
if ((token2 == NULL) || (*token2 == 0)) {
5733
atom->valuep = xmlStrdup(token);
5738
lenn = strlen((char *) token2);
5739
lenp = strlen((char *) token);
5741
str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5743
xmlRegFreeAtom(atom);
5746
memcpy(&str[0], token, lenp);
5748
memcpy(&str[lenp + 1], token2, lenn);
5749
str[lenn + lenp + 1] = 0;
5754
if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5755
xmlRegFreeAtom(atom);
5764
* xmlAutomataNewNegTrans:
5766
* @from: the starting point of the transition
5767
* @to: the target point of the transition or NULL
5768
* @token: the first input string associated to that transition
5769
* @token2: the second input string associated to that transition
5770
* @data: data passed to the callback function if the transition is activated
5772
* If @to is NULL, this creates first a new target state in the automata
5773
* and then adds a transition from the @from state to the target state
5774
* activated by any value except (@token,@token2)
5775
* Note that if @token2 is not NULL, then (X, NULL) won't match to follow
5776
# the semantic of XSD ##other
5778
* Returns the target state or NULL in case of error
5781
xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5782
xmlAutomataStatePtr to, const xmlChar *token,
5783
const xmlChar *token2, void *data) {
5785
xmlChar err_msg[200];
5787
if ((am == NULL) || (from == NULL) || (token == NULL))
5789
atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5794
if ((token2 == NULL) || (*token2 == 0)) {
5795
atom->valuep = xmlStrdup(token);
5800
lenn = strlen((char *) token2);
5801
lenp = strlen((char *) token);
5803
str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5805
xmlRegFreeAtom(atom);
5808
memcpy(&str[0], token, lenp);
5810
memcpy(&str[lenp + 1], token2, lenn);
5811
str[lenn + lenp + 1] = 0;
5815
snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
5817
atom->valuep2 = xmlStrdup(err_msg);
5819
if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
5820
xmlRegFreeAtom(atom);
5830
* xmlAutomataNewCountTrans2:
5832
* @from: the starting point of the transition
5833
* @to: the target point of the transition or NULL
5834
* @token: the input string associated to that transition
5835
* @token2: the second input string associated to that transition
5836
* @min: the minimum successive occurences of token
5837
* @max: the maximum successive occurences of token
5838
* @data: data associated to the transition
5840
* If @to is NULL, this creates first a new target state in the automata
5841
* and then adds a transition from the @from state to the target state
5842
* activated by a succession of input of value @token and @token2 and
5843
* whose number is between @min and @max
5845
* Returns the target state or NULL in case of error
5848
xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
5849
xmlAutomataStatePtr to, const xmlChar *token,
5850
const xmlChar *token2,
5851
int min, int max, void *data) {
5855
if ((am == NULL) || (from == NULL) || (token == NULL))
5859
if ((max < min) || (max < 1))
5861
atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5864
if ((token2 == NULL) || (*token2 == 0)) {
5865
atom->valuep = xmlStrdup(token);
5870
lenn = strlen((char *) token2);
5871
lenp = strlen((char *) token);
5873
str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
5875
xmlRegFreeAtom(atom);
5878
memcpy(&str[0], token, lenp);
5880
memcpy(&str[lenp + 1], token2, lenn);
5881
str[lenn + lenp + 1] = 0;
5893
* associate a counter to the transition.
5895
counter = xmlRegGetCounter(am);
5896
am->counters[counter].min = min;
5897
am->counters[counter].max = max;
5899
/* xmlFAGenerateTransitions(am, from, to, atom); */
5901
to = xmlRegNewState(am);
5902
xmlRegStatePush(am, to);
5904
xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5905
xmlRegAtomPush(am, atom);
5913
xmlFAGenerateEpsilonTransition(am, from, to);
5918
* xmlAutomataNewCountTrans:
5920
* @from: the starting point of the transition
5921
* @to: the target point of the transition or NULL
5922
* @token: the input string associated to that transition
5923
* @min: the minimum successive occurences of token
5924
* @max: the maximum successive occurences of token
5925
* @data: data associated to the transition
5927
* If @to is NULL, this creates first a new target state in the automata
5928
* and then adds a transition from the @from state to the target state
5929
* activated by a succession of input of value @token and whose number
5930
* is between @min and @max
5932
* Returns the target state or NULL in case of error
5935
xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5936
xmlAutomataStatePtr to, const xmlChar *token,
5937
int min, int max, void *data) {
5941
if ((am == NULL) || (from == NULL) || (token == NULL))
5945
if ((max < min) || (max < 1))
5947
atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
5950
atom->valuep = xmlStrdup(token);
5959
* associate a counter to the transition.
5961
counter = xmlRegGetCounter(am);
5962
am->counters[counter].min = min;
5963
am->counters[counter].max = max;
5965
/* xmlFAGenerateTransitions(am, from, to, atom); */
5967
to = xmlRegNewState(am);
5968
xmlRegStatePush(am, to);
5970
xmlRegStateAddTrans(am, from, atom, to, counter, -1);
5971
xmlRegAtomPush(am, atom);
5979
xmlFAGenerateEpsilonTransition(am, from, to);
5984
* xmlAutomataNewOnceTrans2:
5986
* @from: the starting point of the transition
5987
* @to: the target point of the transition or NULL
5988
* @token: the input string associated to that transition
5989
* @token2: the second input string associated to that transition
5990
* @min: the minimum successive occurences of token
5991
* @max: the maximum successive occurences of token
5992
* @data: data associated to the transition
5994
* If @to is NULL, this creates first a new target state in the automata
5995
* and then adds a transition from the @from state to the target state
5996
* activated by a succession of input of value @token and @token2 and whose
5997
* number is between @min and @max, moreover that transition can only be
6000
* Returns the target state or NULL in case of error
6003
xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
6004
xmlAutomataStatePtr to, const xmlChar *token,
6005
const xmlChar *token2,
6006
int min, int max, void *data) {
6010
if ((am == NULL) || (from == NULL) || (token == NULL))
6014
if ((max < min) || (max < 1))
6016
atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6019
if ((token2 == NULL) || (*token2 == 0)) {
6020
atom->valuep = xmlStrdup(token);
6025
lenn = strlen((char *) token2);
6026
lenp = strlen((char *) token);
6028
str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
6030
xmlRegFreeAtom(atom);
6033
memcpy(&str[0], token, lenp);
6035
memcpy(&str[lenp + 1], token2, lenn);
6036
str[lenn + lenp + 1] = 0;
6041
atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6045
* associate a counter to the transition.
6047
counter = xmlRegGetCounter(am);
6048
am->counters[counter].min = 1;
6049
am->counters[counter].max = 1;
6051
/* xmlFAGenerateTransitions(am, from, to, atom); */
6053
to = xmlRegNewState(am);
6054
xmlRegStatePush(am, to);
6056
xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6057
xmlRegAtomPush(am, atom);
6065
* xmlAutomataNewOnceTrans:
6067
* @from: the starting point of the transition
6068
* @to: the target point of the transition or NULL
6069
* @token: the input string associated to that transition
6070
* @min: the minimum successive occurences of token
6071
* @max: the maximum successive occurences of token
6072
* @data: data associated to the transition
6074
* If @to is NULL, this creates first a new target state in the automata
6075
* and then adds a transition from the @from state to the target state
6076
* activated by a succession of input of value @token and whose number
6077
* is between @min and @max, moreover that transition can only be crossed
6080
* Returns the target state or NULL in case of error
6083
xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6084
xmlAutomataStatePtr to, const xmlChar *token,
6085
int min, int max, void *data) {
6089
if ((am == NULL) || (from == NULL) || (token == NULL))
6093
if ((max < min) || (max < 1))
6095
atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
6098
atom->valuep = xmlStrdup(token);
6100
atom->quant = XML_REGEXP_QUANT_ONCEONLY;
6104
* associate a counter to the transition.
6106
counter = xmlRegGetCounter(am);
6107
am->counters[counter].min = 1;
6108
am->counters[counter].max = 1;
6110
/* xmlFAGenerateTransitions(am, from, to, atom); */
6112
to = xmlRegNewState(am);
6113
xmlRegStatePush(am, to);
6115
xmlRegStateAddTrans(am, from, atom, to, counter, -1);
6116
xmlRegAtomPush(am, atom);
6122
* xmlAutomataNewState:
6125
* Create a new disconnected state in the automata
6127
* Returns the new state or NULL in case of error
6130
xmlAutomataNewState(xmlAutomataPtr am) {
6131
xmlAutomataStatePtr to;
6135
to = xmlRegNewState(am);
6136
xmlRegStatePush(am, to);
6141
* xmlAutomataNewEpsilon:
6143
* @from: the starting point of the transition
6144
* @to: the target point of the transition or NULL
6146
* If @to is NULL, this creates first a new target state in the automata
6147
* and then adds an epsilon transition from the @from state to the
6150
* Returns the target state or NULL in case of error
6153
xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
6154
xmlAutomataStatePtr to) {
6155
if ((am == NULL) || (from == NULL))
6157
xmlFAGenerateEpsilonTransition(am, from, to);
6164
* xmlAutomataNewAllTrans:
6166
* @from: the starting point of the transition
6167
* @to: the target point of the transition or NULL
6168
* @lax: allow to transition if not all all transitions have been activated
6170
* If @to is NULL, this creates first a new target state in the automata
6171
* and then adds a an ALL transition from the @from state to the
6172
* target state. That transition is an epsilon transition allowed only when
6173
* all transitions from the @from node have been activated.
6175
* Returns the target state or NULL in case of error
6178
xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6179
xmlAutomataStatePtr to, int lax) {
6180
if ((am == NULL) || (from == NULL))
6182
xmlFAGenerateAllTransition(am, from, to, lax);
6189
* xmlAutomataNewCounter:
6191
* @min: the minimal value on the counter
6192
* @max: the maximal value on the counter
6194
* Create a new counter
6196
* Returns the counter number or -1 in case of error
6199
xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
6205
ret = xmlRegGetCounter(am);
6208
am->counters[ret].min = min;
6209
am->counters[ret].max = max;
6214
* xmlAutomataNewCountedTrans:
6216
* @from: the starting point of the transition
6217
* @to: the target point of the transition or NULL
6218
* @counter: the counter associated to that transition
6220
* If @to is NULL, this creates first a new target state in the automata
6221
* and then adds an epsilon transition from the @from state to the target state
6222
* which will increment the counter provided
6224
* Returns the target state or NULL in case of error
6227
xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6228
xmlAutomataStatePtr to, int counter) {
6229
if ((am == NULL) || (from == NULL) || (counter < 0))
6231
xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
6238
* xmlAutomataNewCounterTrans:
6240
* @from: the starting point of the transition
6241
* @to: the target point of the transition or NULL
6242
* @counter: the counter associated to that transition
6244
* If @to is NULL, this creates first a new target state in the automata
6245
* and then adds an epsilon transition from the @from state to the target state
6246
* which will be allowed only if the counter is within the right range.
6248
* Returns the target state or NULL in case of error
6251
xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
6252
xmlAutomataStatePtr to, int counter) {
6253
if ((am == NULL) || (from == NULL) || (counter < 0))
6255
xmlFAGenerateCountedTransition(am, from, to, counter);
6262
* xmlAutomataCompile:
6265
* Compile the automata into a Reg Exp ready for being executed.
6266
* The automata should be free after this point.
6268
* Returns the compiled regexp or NULL in case of error
6271
xmlAutomataCompile(xmlAutomataPtr am) {
6274
if ((am == NULL) || (am->error != 0)) return(NULL);
6275
xmlFAEliminateEpsilonTransitions(am);
6276
/* xmlFAComputesDeterminism(am); */
6277
ret = xmlRegEpxFromParse(am);
6283
* xmlAutomataIsDeterminist:
6286
* Checks if an automata is determinist.
6288
* Returns 1 if true, 0 if not, and -1 in case of error
6291
xmlAutomataIsDeterminist(xmlAutomataPtr am) {
6297
ret = xmlFAComputesDeterminism(am);
6300
#endif /* LIBXML_AUTOMATA_ENABLED */
6302
#ifdef LIBXML_EXPR_ENABLED
6303
/************************************************************************
6305
* Formal Expression handling code *
6307
************************************************************************/
6308
/************************************************************************
6310
* Expression handling context *
6312
************************************************************************/
6314
struct _xmlExpCtxt {
6316
xmlExpNodePtr *table;
6328
* @maxNodes: the maximum number of nodes
6329
* @dict: optional dictionnary to use internally
6331
* Creates a new context for manipulating expressions
6333
* Returns the context or NULL in case of error
6336
xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
6340
if (maxNodes <= 4096)
6343
ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
6346
memset(ret, 0, sizeof(xmlExpCtxt));
6349
ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
6350
if (ret->table == NULL) {
6354
memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
6356
ret->dict = xmlDictCreate();
6357
if (ret->dict == NULL) {
6358
xmlFree(ret->table);
6364
xmlDictReference(ret->dict);
6371
* @ctxt: an expression context
6373
* Free an expression context
6376
xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
6379
xmlDictFree(ctxt->dict);
6380
if (ctxt->table != NULL)
6381
xmlFree(ctxt->table);
6385
/************************************************************************
6387
* Structure associated to an expression node *
6389
************************************************************************/
6390
#define MAX_NODES 10000
6392
/* #define DEBUG_DERIV */
6397
* - public API for creation
6400
* - regression testing
6403
* - split into module and test tool
6408
XML_EXP_NILABLE = (1 << 0)
6411
#define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
6413
struct _xmlExpNode {
6414
unsigned char type;/* xmlExpNodeType */
6415
unsigned char info;/* OR of xmlExpNodeInfo */
6416
unsigned short key; /* the hash key */
6417
unsigned int ref; /* The number of references */
6418
int c_max; /* the maximum length it can consume */
6419
xmlExpNodePtr exp_left;
6420
xmlExpNodePtr next;/* the next node in the hash table or free list */
6427
xmlExpNodePtr f_right;
6429
const xmlChar *f_str;
6433
#define exp_min field.count.f_min
6434
#define exp_max field.count.f_max
6435
/* #define exp_left field.children.f_left */
6436
#define exp_right field.children.f_right
6437
#define exp_str field.f_str
6439
static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
6440
static xmlExpNode forbiddenExpNode = {
6441
XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6443
xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
6444
static xmlExpNode emptyExpNode = {
6445
XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
6447
xmlExpNodePtr emptyExp = &emptyExpNode;
6449
/************************************************************************
6451
* The custom hash table for unicity and canonicalization *
6452
* of sub-expressions pointers *
6454
************************************************************************/
6456
* xmlExpHashNameComputeKey:
6457
* Calculate the hash key for a token
6459
static unsigned short
6460
xmlExpHashNameComputeKey(const xmlChar *name) {
6461
unsigned short value = 0L;
6465
value += 30 * (*name);
6466
while ((ch = *name++) != 0) {
6467
value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
6474
* xmlExpHashComputeKey:
6475
* Calculate the hash key for a compound expression
6477
static unsigned short
6478
xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
6479
xmlExpNodePtr right) {
6480
unsigned long value;
6486
value += right->key;
6488
ret = (unsigned short) value;
6492
value += right->key;
6494
ret = (unsigned short) value;
6498
value += right->key;
6499
ret = (unsigned short) value;
6508
static xmlExpNodePtr
6509
xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
6512
if (ctxt->nb_nodes >= MAX_NODES)
6514
ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
6517
memset(ret, 0, sizeof(xmlExpNode));
6526
* xmlExpHashGetEntry:
6527
* @table: the hash table
6529
* Get the unique entry from the hash table. The entry is created if
6530
* needed. @left and @right are consumed, i.e. their ref count will
6531
* be decremented by the operation.
6533
* Returns the pointer or NULL in case of error
6535
static xmlExpNodePtr
6536
xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
6537
xmlExpNodePtr left, xmlExpNodePtr right,
6538
const xmlChar *name, int min, int max) {
6539
unsigned short kbase, key;
6540
xmlExpNodePtr entry;
6541
xmlExpNodePtr insert;
6547
* Check for duplicate and insertion location.
6549
if (type == XML_EXP_ATOM) {
6550
kbase = xmlExpHashNameComputeKey(name);
6551
} else if (type == XML_EXP_COUNT) {
6552
/* COUNT reduction rule 1 */
6559
xmlExpFree(ctxt, left);
6564
xmlExpFree(ctxt, left);
6565
return(forbiddenExp);
6572
} else if (type == XML_EXP_OR) {
6573
/* Forbid reduction rules */
6574
if (left->type == XML_EXP_FORBID) {
6575
xmlExpFree(ctxt, left);
6578
if (right->type == XML_EXP_FORBID) {
6579
xmlExpFree(ctxt, right);
6583
/* OR reduction rule 1 */
6584
/* a | a reduced to a */
6585
if (left == right) {
6589
/* OR canonicalization rule 1 */
6590
/* linearize (a | b) | c into a | (b | c) */
6591
if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
6592
xmlExpNodePtr tmp = left;
6596
/* OR reduction rule 2 */
6597
/* a | (a | b) and b | (a | b) are reduced to a | b */
6598
if (right->type == XML_EXP_OR) {
6599
if ((left == right->exp_left) ||
6600
(left == right->exp_right)) {
6601
xmlExpFree(ctxt, left);
6605
/* OR canonicalization rule 2 */
6606
/* linearize (a | b) | c into a | (b | c) */
6607
if (left->type == XML_EXP_OR) {
6610
/* OR canonicalization rule 2 */
6611
if ((left->exp_right->type != XML_EXP_OR) &&
6612
(left->exp_right->key < left->exp_left->key)) {
6613
tmp = left->exp_right;
6614
left->exp_right = left->exp_left;
6615
left->exp_left = tmp;
6617
left->exp_right->ref++;
6618
tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
6620
left->exp_left->ref++;
6621
tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
6624
xmlExpFree(ctxt, left);
6627
if (right->type == XML_EXP_OR) {
6628
/* Ordering in the tree */
6629
/* C | (A | B) -> A | (B | C) */
6630
if (left->key > right->exp_right->key) {
6632
right->exp_right->ref++;
6633
tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
6635
right->exp_left->ref++;
6636
tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6638
xmlExpFree(ctxt, right);
6641
/* Ordering in the tree */
6642
/* B | (A | C) -> A | (B | C) */
6643
if (left->key > right->exp_left->key) {
6645
right->exp_right->ref++;
6646
tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
6647
right->exp_right, NULL, 0, 0);
6648
right->exp_left->ref++;
6649
tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
6651
xmlExpFree(ctxt, right);
6655
/* we know both types are != XML_EXP_OR here */
6656
else if (left->key > right->key) {
6657
xmlExpNodePtr tmp = left;
6661
kbase = xmlExpHashComputeKey(type, left, right);
6662
} else if (type == XML_EXP_SEQ) {
6663
/* Forbid reduction rules */
6664
if (left->type == XML_EXP_FORBID) {
6665
xmlExpFree(ctxt, right);
6668
if (right->type == XML_EXP_FORBID) {
6669
xmlExpFree(ctxt, left);
6672
/* Empty reduction rules */
6673
if (right->type == XML_EXP_EMPTY) {
6676
if (left->type == XML_EXP_EMPTY) {
6679
kbase = xmlExpHashComputeKey(type, left, right);
6683
key = kbase % ctxt->size;
6684
if (ctxt->table[key] != NULL) {
6685
for (insert = ctxt->table[key]; insert != NULL;
6686
insert = insert->next) {
6687
if ((insert->key == kbase) &&
6688
(insert->type == type)) {
6689
if (type == XML_EXP_ATOM) {
6690
if (name == insert->exp_str) {
6694
} else if (type == XML_EXP_COUNT) {
6695
if ((insert->exp_min == min) && (insert->exp_max == max) &&
6696
(insert->exp_left == left)) {
6701
} else if ((insert->exp_left == left) &&
6702
(insert->exp_right == right)) {
6712
entry = xmlExpNewNode(ctxt, type);
6716
if (type == XML_EXP_ATOM) {
6717
entry->exp_str = name;
6719
} else if (type == XML_EXP_COUNT) {
6720
entry->exp_min = min;
6721
entry->exp_max = max;
6722
entry->exp_left = left;
6723
if ((min == 0) || (IS_NILLABLE(left)))
6724
entry->info |= XML_EXP_NILABLE;
6728
entry->c_max = max * entry->exp_left->c_max;
6730
entry->exp_left = left;
6731
entry->exp_right = right;
6732
if (type == XML_EXP_OR) {
6733
if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
6734
entry->info |= XML_EXP_NILABLE;
6735
if ((entry->exp_left->c_max == -1) ||
6736
(entry->exp_right->c_max == -1))
6738
else if (entry->exp_left->c_max > entry->exp_right->c_max)
6739
entry->c_max = entry->exp_left->c_max;
6741
entry->c_max = entry->exp_right->c_max;
6743
if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
6744
entry->info |= XML_EXP_NILABLE;
6745
if ((entry->exp_left->c_max == -1) ||
6746
(entry->exp_right->c_max == -1))
6749
entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
6753
if (ctxt->table[key] != NULL)
6754
entry->next = ctxt->table[key];
6756
ctxt->table[key] = entry;
6764
* @ctxt: the expression context
6765
* @exp: the expression
6767
* Dereference the expression
6770
xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
6771
if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
6774
if (exp->ref == 0) {
6777
/* Unlink it first from the hash table */
6778
key = exp->key % ctxt->size;
6779
if (ctxt->table[key] == exp) {
6780
ctxt->table[key] = exp->next;
6784
tmp = ctxt->table[key];
6785
while (tmp != NULL) {
6786
if (tmp->next == exp) {
6787
tmp->next = exp->next;
6794
if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
6795
xmlExpFree(ctxt, exp->exp_left);
6796
xmlExpFree(ctxt, exp->exp_right);
6797
} else if (exp->type == XML_EXP_COUNT) {
6798
xmlExpFree(ctxt, exp->exp_left);
6807
* @exp: the expression
6809
* Increase the reference count of the expression
6812
xmlExpRef(xmlExpNodePtr exp) {
6819
* @ctxt: the expression context
6820
* @name: the atom name
6821
* @len: the atom name lenght in byte (or -1);
6823
* Get the atom associated to this name from that context
6825
* Returns the node or NULL in case of error
6828
xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
6829
if ((ctxt == NULL) || (name == NULL))
6831
name = xmlDictLookup(ctxt->dict, name, len);
6834
return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
6839
* @ctxt: the expression context
6840
* @left: left expression
6841
* @right: right expression
6843
* Get the atom associated to the choice @left | @right
6844
* Note that @left and @right are consumed in the operation, to keep
6845
* an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6846
* this is true even in case of failure (unless ctxt == NULL).
6848
* Returns the node or NULL in case of error
6851
xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6854
if ((left == NULL) || (right == NULL)) {
6855
xmlExpFree(ctxt, left);
6856
xmlExpFree(ctxt, right);
6859
return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
6864
* @ctxt: the expression context
6865
* @left: left expression
6866
* @right: right expression
6868
* Get the atom associated to the sequence @left , @right
6869
* Note that @left and @right are consumed in the operation, to keep
6870
* an handle on them use xmlExpRef() and use xmlExpFree() to release them,
6871
* this is true even in case of failure (unless ctxt == NULL).
6873
* Returns the node or NULL in case of error
6876
xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
6879
if ((left == NULL) || (right == NULL)) {
6880
xmlExpFree(ctxt, left);
6881
xmlExpFree(ctxt, right);
6884
return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
6889
* @ctxt: the expression context
6890
* @subset: the expression to be repeated
6891
* @min: the lower bound for the repetition
6892
* @max: the upper bound for the repetition, -1 means infinite
6894
* Get the atom associated to the range (@subset){@min, @max}
6895
* Note that @subset is consumed in the operation, to keep
6896
* an handle on it use xmlExpRef() and use xmlExpFree() to release it,
6897
* this is true even in case of failure (unless ctxt == NULL).
6899
* Returns the node or NULL in case of error
6902
xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
6905
if ((subset == NULL) || (min < 0) || (max < -1) ||
6906
((max >= 0) && (min > max))) {
6907
xmlExpFree(ctxt, subset);
6910
return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
6911
NULL, NULL, min, max));
6914
/************************************************************************
6916
* Public API for operations on expressions *
6918
************************************************************************/
6921
xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6922
const xmlChar**list, int len, int nb) {
6925
switch (exp->type) {
6929
for (tmp = 0;tmp < nb;tmp++)
6930
if (list[tmp] == exp->exp_str)
6934
list[nb++] = exp->exp_str;
6937
exp = exp->exp_left;
6941
tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
6944
tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
6954
* xmlExpGetLanguage:
6955
* @ctxt: the expression context
6956
* @exp: the expression
6957
* @langList: where to store the tokens
6958
* @len: the allocated lenght of @list
6960
* Find all the strings used in @exp and store them in @list
6962
* Returns the number of unique strings found, -1 in case of errors and
6963
* -2 if there is more than @len strings
6966
xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6967
const xmlChar**langList, int len) {
6968
if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
6970
return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
6974
xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
6975
const xmlChar**list, int len, int nb) {
6978
switch (exp->type) {
6979
case XML_EXP_FORBID:
6984
for (tmp = 0;tmp < nb;tmp++)
6985
if (list[tmp] == exp->exp_str)
6989
list[nb++] = exp->exp_str;
6992
exp = exp->exp_left;
6995
tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
6998
if (IS_NILLABLE(exp->exp_left)) {
6999
tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7007
tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
7010
tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
7021
* @ctxt: the expression context
7022
* @exp: the expression
7023
* @tokList: where to store the tokens
7024
* @len: the allocated lenght of @list
7026
* Find all the strings that appears at the start of the languages
7027
* accepted by @exp and store them in @list. E.g. for (a, b) | c
7028
* it will return the list [a, c]
7030
* Returns the number of unique strings found, -1 in case of errors and
7031
* -2 if there is more than @len strings
7034
xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7035
const xmlChar**tokList, int len) {
7036
if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
7038
return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
7043
* @exp: the expression
7045
* Finds if the expression is nillable, i.e. if it accepts the empty sequqnce
7047
* Returns 1 if nillable, 0 if not and -1 in case of error
7050
xmlExpIsNillable(xmlExpNodePtr exp) {
7053
return(IS_NILLABLE(exp) != 0);
7056
static xmlExpNodePtr
7057
xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
7061
switch (exp->type) {
7063
return(forbiddenExp);
7064
case XML_EXP_FORBID:
7065
return(forbiddenExp);
7067
if (exp->exp_str == str) {
7069
printf("deriv atom: equal => Empty\n");
7074
printf("deriv atom: mismatch => forbid\n");
7076
/* TODO wildcards here */
7084
printf("deriv or: => or(derivs)\n");
7086
tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7090
ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7092
xmlExpFree(ctxt, tmp);
7095
ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
7101
printf("deriv seq: starting with left\n");
7103
ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7106
} else if (ret == forbiddenExp) {
7107
if (IS_NILLABLE(exp->exp_left)) {
7109
printf("deriv seq: left failed but nillable\n");
7111
ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
7115
printf("deriv seq: left match => sequence\n");
7117
exp->exp_right->ref++;
7118
ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
7122
case XML_EXP_COUNT: {
7126
if (exp->exp_max == 0)
7127
return(forbiddenExp);
7128
ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
7131
if (ret == forbiddenExp) {
7133
printf("deriv count: pattern mismatch => forbid\n");
7137
if (exp->exp_max == 1)
7139
if (exp->exp_max < 0) /* unbounded */
7142
max = exp->exp_max - 1;
7143
if (exp->exp_min > 0)
7144
min = exp->exp_min - 1;
7147
exp->exp_left->ref++;
7148
tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
7150
if (ret == emptyExp) {
7152
printf("deriv count: match to empty => new count\n");
7157
printf("deriv count: match => sequence with new count\n");
7159
return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
7167
* xmlExpStringDerive:
7168
* @ctxt: the expression context
7169
* @exp: the expression
7171
* @len: the string len in bytes if available
7173
* Do one step of Brzozowski derivation of the expression @exp with
7174
* respect to the input string
7176
* Returns the resulting expression or NULL in case of internal error
7179
xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7180
const xmlChar *str, int len) {
7181
const xmlChar *input;
7183
if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
7187
* check the string is in the dictionnary, if yes use an interned
7188
* copy, otherwise we know it's not an acceptable input
7190
input = xmlDictExists(ctxt->dict, str, len);
7191
if (input == NULL) {
7192
return(forbiddenExp);
7194
return(xmlExpStringDeriveInt(ctxt, exp, input));
7198
xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
7201
if (sub->c_max == -1) {
7202
if (exp->c_max != -1)
7204
} else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
7208
if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
7214
static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
7218
* @ctxt: the expressions context
7219
* @exp: the englobing expression
7220
* @sub: the subexpression
7221
* @mult: the multiple expression
7222
* @remain: the remain from the derivation of the multiple
7224
* Check if exp is a multiple of sub, i.e. if there is a finite number n
7225
* so that sub{n} subsume exp
7227
* Returns the multiple value if successful, 0 if it is not a multiple
7228
* and -1 in case of internel error.
7232
xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
7233
xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
7235
xmlExpNodePtr tmp, tmp2;
7237
if (mult != NULL) *mult = NULL;
7238
if (remain != NULL) *remain = NULL;
7239
if (exp->c_max == -1) return(0);
7240
if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
7242
for (i = 1;i <= exp->c_max;i++) {
7244
tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7245
sub, NULL, NULL, i, i);
7249
if (!xmlExpCheckCard(tmp, exp)) {
7250
xmlExpFree(ctxt, tmp);
7253
tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
7255
xmlExpFree(ctxt, tmp);
7258
if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
7262
xmlExpFree(ctxt, tmp2);
7266
xmlExpFree(ctxt, tmp);
7268
printf("Divide succeeded %d\n", i);
7272
xmlExpFree(ctxt, tmp);
7273
xmlExpFree(ctxt, tmp2);
7276
printf("Divide failed\n");
7282
* xmlExpExpDeriveInt:
7283
* @ctxt: the expressions context
7284
* @exp: the englobing expression
7285
* @sub: the subexpression
7287
* Try to do a step of Brzozowski derivation but at a higher level
7288
* the input being a subexpression.
7290
* Returns the resulting expression or NULL in case of internal error
7292
static xmlExpNodePtr
7293
xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7294
xmlExpNodePtr ret, tmp, tmp2, tmp3;
7295
const xmlChar **tab;
7299
* In case of equality and if the expression can only consume a finite
7300
* amount, then the derivation is empty
7302
if ((exp == sub) && (exp->c_max >= 0)) {
7304
printf("Equal(exp, sub) and finite -> Empty\n");
7309
* decompose sub sequence first
7311
if (sub->type == XML_EXP_EMPTY) {
7313
printf("Empty(sub) -> Empty\n");
7318
if (sub->type == XML_EXP_SEQ) {
7320
printf("Seq(sub) -> decompose\n");
7322
tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7325
if (tmp == forbiddenExp)
7327
ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
7328
xmlExpFree(ctxt, tmp);
7331
if (sub->type == XML_EXP_OR) {
7333
printf("Or(sub) -> decompose\n");
7335
tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
7336
if (tmp == forbiddenExp)
7340
ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
7341
if ((ret == NULL) || (ret == forbiddenExp)) {
7342
xmlExpFree(ctxt, tmp);
7345
return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
7347
if (!xmlExpCheckCard(exp, sub)) {
7349
printf("CheckCard(exp, sub) failed -> Forbid\n");
7351
return(forbiddenExp);
7353
switch (exp->type) {
7355
if (sub == emptyExp)
7358
printf("Empty(exp) -> Forbid\n");
7360
return(forbiddenExp);
7361
case XML_EXP_FORBID:
7363
printf("Forbid(exp) -> Forbid\n");
7365
return(forbiddenExp);
7367
if (sub->type == XML_EXP_ATOM) {
7368
/* TODO: handle wildcards */
7369
if (exp->exp_str == sub->exp_str) {
7371
printf("Atom match -> Empty\n");
7376
printf("Atom mismatch -> Forbid\n");
7378
return(forbiddenExp);
7380
if ((sub->type == XML_EXP_COUNT) &&
7381
(sub->exp_max == 1) &&
7382
(sub->exp_left->type == XML_EXP_ATOM)) {
7383
/* TODO: handle wildcards */
7384
if (exp->exp_str == sub->exp_left->exp_str) {
7386
printf("Atom match -> Empty\n");
7391
printf("Atom mismatch -> Forbid\n");
7393
return(forbiddenExp);
7396
printf("Compex exp vs Atom -> Forbid\n");
7398
return(forbiddenExp);
7400
/* try to get the sequence consumed only if possible */
7401
if (xmlExpCheckCard(exp->exp_left, sub)) {
7402
/* See if the sequence can be consumed directly */
7404
printf("Seq trying left only\n");
7406
ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7407
if ((ret != forbiddenExp) && (ret != NULL)) {
7409
printf("Seq trying left only worked\n");
7412
* TODO: assumption here that we are determinist
7413
* i.e. we won't get to a nillable exp left
7414
* subset which could be matched by the right
7416
* e.g.: (a | b)+,(a | c) and 'a+,a'
7418
exp->exp_right->ref++;
7419
return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7420
exp->exp_right, NULL, 0, 0));
7424
printf("Seq: left too short\n");
7427
/* Try instead to decompose */
7428
if (sub->type == XML_EXP_COUNT) {
7432
printf("Seq: sub is a count\n");
7434
ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7437
if (ret != forbiddenExp) {
7439
printf("Seq , Count match on left\n");
7441
if (sub->exp_max < 0)
7444
max = sub->exp_max -1;
7445
if (sub->exp_min > 0)
7446
min = sub->exp_min -1;
7449
exp->exp_right->ref++;
7450
tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
7451
exp->exp_right, NULL, 0, 0);
7455
sub->exp_left->ref++;
7456
tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
7457
sub->exp_left, NULL, NULL, min, max);
7459
xmlExpFree(ctxt, tmp);
7462
ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7463
xmlExpFree(ctxt, tmp);
7464
xmlExpFree(ctxt, tmp2);
7468
/* we made no progress on structured operations */
7472
printf("Or , trying both side\n");
7474
ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7477
tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
7479
xmlExpFree(ctxt, ret);
7482
return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
7483
case XML_EXP_COUNT: {
7486
if (sub->type == XML_EXP_COUNT) {
7488
* Try to see if the loop is completely subsumed
7490
tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
7493
if (tmp == forbiddenExp) {
7497
printf("Count, Count inner don't subsume\n");
7499
mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
7503
printf("Count, Count not multiple => forbidden\n");
7505
return(forbiddenExp);
7507
if (sub->exp_max == -1) {
7509
if (exp->exp_max == -1) {
7510
if (exp->exp_min <= sub->exp_min * mult)
7513
min = exp->exp_min - sub->exp_min * mult;
7516
printf("Count, Count finite can't subsume infinite\n");
7518
xmlExpFree(ctxt, tmp);
7519
return(forbiddenExp);
7522
if (exp->exp_max == -1) {
7524
printf("Infinite loop consume mult finite loop\n");
7526
if (exp->exp_min > sub->exp_min * mult) {
7528
min = exp->exp_min - sub->exp_min * mult;
7534
if (exp->exp_max < sub->exp_max * mult) {
7536
printf("loops max mult mismatch => forbidden\n");
7538
xmlExpFree(ctxt, tmp);
7539
return(forbiddenExp);
7541
if (sub->exp_max * mult > exp->exp_min)
7544
min = exp->exp_min - sub->exp_max * mult;
7545
max = exp->exp_max - sub->exp_max * mult;
7548
} else if (!IS_NILLABLE(tmp)) {
7550
* TODO: loop here to try to grow if working on finite
7554
printf("Count, Count remain not nillable => forbidden\n");
7556
xmlExpFree(ctxt, tmp);
7557
return(forbiddenExp);
7558
} else if (sub->exp_max == -1) {
7559
if (exp->exp_max == -1) {
7560
if (exp->exp_min <= sub->exp_min) {
7562
printf("Infinite loops Okay => COUNT(0,Inf)\n");
7568
printf("Infinite loops min => Count(X,Inf)\n");
7571
min = exp->exp_min - sub->exp_min;
7573
} else if (exp->exp_min > sub->exp_min) {
7575
printf("loops min mismatch 1 => forbidden ???\n");
7577
xmlExpFree(ctxt, tmp);
7578
return(forbiddenExp);
7584
if (exp->exp_max == -1) {
7586
printf("Infinite loop consume finite loop\n");
7588
if (exp->exp_min > sub->exp_min) {
7590
min = exp->exp_min - sub->exp_min;
7596
if (exp->exp_max < sub->exp_max) {
7598
printf("loops max mismatch => forbidden\n");
7600
xmlExpFree(ctxt, tmp);
7601
return(forbiddenExp);
7603
if (sub->exp_max > exp->exp_min)
7606
min = exp->exp_min - sub->exp_max;
7607
max = exp->exp_max - sub->exp_max;
7611
printf("loops match => SEQ(COUNT())\n");
7613
exp->exp_left->ref++;
7614
tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7615
NULL, NULL, min, max);
7619
ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7623
tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
7626
if (tmp == forbiddenExp) {
7628
printf("loop mismatch => forbidden\n");
7630
return(forbiddenExp);
7632
if (exp->exp_min > 0)
7633
min = exp->exp_min - 1;
7636
if (exp->exp_max < 0)
7639
max = exp->exp_max - 1;
7642
printf("loop match => SEQ(COUNT())\n");
7644
exp->exp_left->ref++;
7645
tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
7646
NULL, NULL, min, max);
7649
ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
7656
printf("Fallback to derivative\n");
7658
if (IS_NILLABLE(sub)) {
7659
if (!(IS_NILLABLE(exp)))
7660
return(forbiddenExp);
7666
* here the structured derivation made no progress so
7667
* we use the default token based derivation to force one more step
7669
if (ctxt->tabSize == 0)
7672
tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
7673
sizeof(const xmlChar *));
7679
* collect all the strings accepted by the subexpression on input
7681
len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7683
const xmlChar **temp;
7684
temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
7685
sizeof(const xmlChar *));
7687
xmlFree((xmlChar **) tab);
7692
len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
7694
for (i = 0;i < len;i++) {
7695
tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
7696
if ((tmp == NULL) || (tmp == forbiddenExp)) {
7697
xmlExpFree(ctxt, ret);
7698
xmlFree((xmlChar **) tab);
7701
tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
7702
if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
7703
xmlExpFree(ctxt, tmp);
7704
xmlExpFree(ctxt, ret);
7705
xmlFree((xmlChar **) tab);
7708
tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
7709
xmlExpFree(ctxt, tmp);
7710
xmlExpFree(ctxt, tmp2);
7712
if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
7713
xmlExpFree(ctxt, ret);
7714
xmlFree((xmlChar **) tab);
7721
ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
7723
xmlFree((xmlChar **) tab);
7728
xmlFree((xmlChar **) tab);
7734
* @ctxt: the expressions context
7735
* @exp: the englobing expression
7736
* @sub: the subexpression
7738
* Evaluates the expression resulting from @exp consuming a sub expression @sub
7739
* Based on algebraic derivation and sometimes direct Brzozowski derivation
7740
* it usually tatkes less than linear time and can handle expressions generating
7741
* infinite languages.
7743
* Returns the resulting expression or NULL in case of internal error, the
7744
* result must be freed
7747
xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7748
if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7754
if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7756
printf("Sub nillable and not exp : can't subsume\n");
7758
return(forbiddenExp);
7760
if (xmlExpCheckCard(exp, sub) == 0) {
7762
printf("sub generate longuer sequances than exp : can't subsume\n");
7764
return(forbiddenExp);
7766
return(xmlExpExpDeriveInt(ctxt, exp, sub));
7771
* @ctxt: the expressions context
7772
* @exp: the englobing expression
7773
* @sub: the subexpression
7775
* Check whether @exp accepts all the languages accexpted by @sub
7776
* the input being a subexpression.
7778
* Returns 1 if true 0 if false and -1 in case of failure.
7781
xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
7784
if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
7788
* TODO: speedup by checking the language of sub is a subset of the
7794
if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
7796
printf("Sub nillable and not exp : can't subsume\n");
7800
if (xmlExpCheckCard(exp, sub) == 0) {
7802
printf("sub generate longuer sequances than exp : can't subsume\n");
7806
tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
7808
printf("Result derivation :\n");
7813
if (tmp == forbiddenExp)
7815
if (tmp == emptyExp)
7817
if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
7818
xmlExpFree(ctxt, tmp);
7821
xmlExpFree(ctxt, tmp);
7825
/************************************************************************
7827
* Parsing expression *
7829
************************************************************************/
7831
static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
7834
#define CUR (*ctxt->cur)
7836
#define NEXT ctxt->cur++;
7838
#define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
7839
#define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
7842
xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
7850
if ((CUR < '0') || (CUR > '9'))
7852
while ((CUR >= '0') && (CUR <= '9')) {
7853
ret = ret * 10 + (CUR - '0');
7859
static xmlExpNodePtr
7860
xmlExpParseOr(xmlExpCtxtPtr ctxt) {
7867
if (*ctxt->cur == '(') {
7869
ret = xmlExpParseExpr(ctxt);
7871
if (*ctxt->cur != ')') {
7872
fprintf(stderr, "unbalanced '(' : %s\n", base);
7873
xmlExpFree(ctxt, ret);
7878
goto parse_quantifier;
7880
while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
7881
(CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
7882
(CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
7884
val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
7887
ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
7896
min = xmlExpParseNumber(ctxt);
7898
xmlExpFree(ctxt, ret);
7904
max = xmlExpParseNumber(ctxt);
7909
xmlExpFree(ctxt, ret);
7913
ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7916
} else if (CUR == '?') {
7918
ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7921
} else if (CUR == '+') {
7923
ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7926
} else if (CUR == '*') {
7928
ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
7936
static xmlExpNodePtr
7937
xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
7938
xmlExpNodePtr ret, right;
7940
ret = xmlExpParseOr(ctxt);
7942
while (CUR == '|') {
7944
right = xmlExpParseOr(ctxt);
7945
if (right == NULL) {
7946
xmlExpFree(ctxt, ret);
7949
ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
7956
static xmlExpNodePtr
7957
xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
7958
xmlExpNodePtr ret, right;
7960
ret = xmlExpParseSeq(ctxt);
7962
while (CUR == ',') {
7964
right = xmlExpParseSeq(ctxt);
7965
if (right == NULL) {
7966
xmlExpFree(ctxt, ret);
7969
ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
7978
* @ctxt: the expressions context
7979
* @expr: the 0 terminated string
7981
* Minimal parser for regexps, it understand the following constructs
7982
* - string terminals
7983
* - choice operator |
7984
* - sequence operator ,
7985
* - subexpressions (...)
7986
* - usual cardinality operators + * and ?
7987
* - finite sequences { min, max }
7988
* - infinite sequences { min, * }
7989
* There is minimal checkings made especially no checking on strings values
7991
* Returns a new expression or NULL in case of failure
7994
xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
8000
ret = xmlExpParseExpr(ctxt);
8002
if (*ctxt->cur != 0) {
8003
xmlExpFree(ctxt, ret);
8010
xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
8013
if (expr == NULL) return;
8014
if (glob) xmlBufferWriteChar(buf, "(");
8015
switch (expr->type) {
8017
xmlBufferWriteChar(buf, "empty");
8019
case XML_EXP_FORBID:
8020
xmlBufferWriteChar(buf, "forbidden");
8023
xmlBufferWriteCHAR(buf, expr->exp_str);
8027
if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8028
xmlExpDumpInt(buf, c, 1);
8030
xmlExpDumpInt(buf, c, 0);
8031
xmlBufferWriteChar(buf, " , ");
8032
c = expr->exp_right;
8033
if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8034
xmlExpDumpInt(buf, c, 1);
8036
xmlExpDumpInt(buf, c, 0);
8040
if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8041
xmlExpDumpInt(buf, c, 1);
8043
xmlExpDumpInt(buf, c, 0);
8044
xmlBufferWriteChar(buf, " | ");
8045
c = expr->exp_right;
8046
if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8047
xmlExpDumpInt(buf, c, 1);
8049
xmlExpDumpInt(buf, c, 0);
8051
case XML_EXP_COUNT: {
8055
if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
8056
xmlExpDumpInt(buf, c, 1);
8058
xmlExpDumpInt(buf, c, 0);
8059
if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
8062
} else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
8065
} else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
8068
} else if (expr->exp_max == expr->exp_min) {
8069
snprintf(rep, 39, "{%d}", expr->exp_min);
8070
} else if (expr->exp_max < 0) {
8071
snprintf(rep, 39, "{%d,inf}", expr->exp_min);
8073
snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
8076
xmlBufferWriteChar(buf, rep);
8080
fprintf(stderr, "Error in tree\n");
8083
xmlBufferWriteChar(buf, ")");
8087
* @buf: a buffer to receive the output
8088
* @expr: the compiled expression
8090
* Serialize the expression as compiled to the buffer
8093
xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
8094
if ((buf == NULL) || (expr == NULL))
8096
xmlExpDumpInt(buf, expr, 0);
8101
* @expr: a compiled expression
8103
* Indicate the maximum number of input a expression can accept
8105
* Returns the maximum length or -1 in case of error
8108
xmlExpMaxToken(xmlExpNodePtr expr) {
8111
return(expr->c_max);
8115
* xmlExpCtxtNbNodes:
8116
* @ctxt: an expression context
8118
* Debugging facility provides the number of allocated nodes at a that point
8120
* Returns the number of nodes in use or -1 in case of error
8123
xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
8126
return(ctxt->nb_nodes);
8131
* @ctxt: an expression context
8133
* Debugging facility provides the number of allocated nodes over lifetime
8135
* Returns the number of nodes ever allocated or -1 in case of error
8138
xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
8141
return(ctxt->nb_cons);
8144
#endif /* LIBXML_EXPR_ENABLED */
8145
#define bottom_xmlregexp
8146
#include "elfgcchack.h"
8147
#endif /* LIBXML_REGEXP_ENABLED */