blob: 0befd12481a884a99ec8ef37395ac57b09cba176 [file] [log] [blame]
Daniel Veillard4255d502002-04-16 15:50:10 +00001/*
2 * regexp.c: generic and extensible Regular Expression engine
3 *
4 * Basically designed with the purpose of compiling regexps for
5 * the variety of validation/shemas mechanisms now available in
William M. Brackddf71d62004-05-06 04:17:26 +00006 * XML related specifications these include:
Daniel Veillard4255d502002-04-16 15:50:10 +00007 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
11 *
12 * See Copyright for the status of this software.
13 *
14 * Daniel Veillard <veillard@redhat.com>
15 */
16
17#define IN_LIBXML
18#include "libxml.h"
19
20#ifdef LIBXML_REGEXP_ENABLED
21
22#include <stdio.h>
23#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000024#ifdef HAVE_LIMITS_H
25#include <limits.h>
26#endif
27
Daniel Veillard4255d502002-04-16 15:50:10 +000028#include <libxml/tree.h>
29#include <libxml/parserInternals.h>
30#include <libxml/xmlregexp.h>
31#include <libxml/xmlautomata.h>
32#include <libxml/xmlunicode.h>
33
Daniel Veillardebe48c62003-12-03 12:12:27 +000034#ifndef INT_MAX
35#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
36#endif
37
Daniel Veillard4255d502002-04-16 15:50:10 +000038/* #define DEBUG_REGEXP_GRAPH */
39/* #define DEBUG_REGEXP_EXEC */
40/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000041/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000042
Daniel Veillardff46a042003-10-08 08:53:17 +000043#define ERROR(str) \
44 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
45 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000046#define NEXT ctxt->cur++
47#define CUR (*(ctxt->cur))
48#define NXT(index) (ctxt->cur[index])
49
50#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
51#define NEXTL(l) ctxt->cur += l;
52
Daniel Veillarde19fc232002-04-22 16:01:24 +000053/**
54 * TODO:
55 *
56 * macro to flag unimplemented blocks
57 */
58#define TODO \
59 xmlGenericError(xmlGenericErrorContext, \
60 "Unimplemented block at %s:%d\n", \
61 __FILE__, __LINE__);
62
Daniel Veillard4255d502002-04-16 15:50:10 +000063/************************************************************************
64 * *
65 * Datatypes and structures *
66 * *
67 ************************************************************************/
68
69typedef enum {
70 XML_REGEXP_EPSILON = 1,
71 XML_REGEXP_CHARVAL,
72 XML_REGEXP_RANGES,
73 XML_REGEXP_SUBREG,
74 XML_REGEXP_STRING,
75 XML_REGEXP_ANYCHAR, /* . */
76 XML_REGEXP_ANYSPACE, /* \s */
77 XML_REGEXP_NOTSPACE, /* \S */
78 XML_REGEXP_INITNAME, /* \l */
79 XML_REGEXP_NOTINITNAME, /* \l */
80 XML_REGEXP_NAMECHAR, /* \c */
81 XML_REGEXP_NOTNAMECHAR, /* \C */
82 XML_REGEXP_DECIMAL, /* \d */
83 XML_REGEXP_NOTDECIMAL, /* \d */
84 XML_REGEXP_REALCHAR, /* \w */
85 XML_REGEXP_NOTREALCHAR, /* \w */
86 XML_REGEXP_LETTER,
87 XML_REGEXP_LETTER_UPPERCASE,
88 XML_REGEXP_LETTER_LOWERCASE,
89 XML_REGEXP_LETTER_TITLECASE,
90 XML_REGEXP_LETTER_MODIFIER,
91 XML_REGEXP_LETTER_OTHERS,
92 XML_REGEXP_MARK,
93 XML_REGEXP_MARK_NONSPACING,
94 XML_REGEXP_MARK_SPACECOMBINING,
95 XML_REGEXP_MARK_ENCLOSING,
96 XML_REGEXP_NUMBER,
97 XML_REGEXP_NUMBER_DECIMAL,
98 XML_REGEXP_NUMBER_LETTER,
99 XML_REGEXP_NUMBER_OTHERS,
100 XML_REGEXP_PUNCT,
101 XML_REGEXP_PUNCT_CONNECTOR,
102 XML_REGEXP_PUNCT_DASH,
103 XML_REGEXP_PUNCT_OPEN,
104 XML_REGEXP_PUNCT_CLOSE,
105 XML_REGEXP_PUNCT_INITQUOTE,
106 XML_REGEXP_PUNCT_FINQUOTE,
107 XML_REGEXP_PUNCT_OTHERS,
108 XML_REGEXP_SEPAR,
109 XML_REGEXP_SEPAR_SPACE,
110 XML_REGEXP_SEPAR_LINE,
111 XML_REGEXP_SEPAR_PARA,
112 XML_REGEXP_SYMBOL,
113 XML_REGEXP_SYMBOL_MATH,
114 XML_REGEXP_SYMBOL_CURRENCY,
115 XML_REGEXP_SYMBOL_MODIFIER,
116 XML_REGEXP_SYMBOL_OTHERS,
117 XML_REGEXP_OTHER,
118 XML_REGEXP_OTHER_CONTROL,
119 XML_REGEXP_OTHER_FORMAT,
120 XML_REGEXP_OTHER_PRIVATE,
121 XML_REGEXP_OTHER_NA,
122 XML_REGEXP_BLOCK_NAME
123} xmlRegAtomType;
124
125typedef enum {
126 XML_REGEXP_QUANT_EPSILON = 1,
127 XML_REGEXP_QUANT_ONCE,
128 XML_REGEXP_QUANT_OPT,
129 XML_REGEXP_QUANT_MULT,
130 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000131 XML_REGEXP_QUANT_ONCEONLY,
132 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000133 XML_REGEXP_QUANT_RANGE
134} xmlRegQuantType;
135
136typedef enum {
137 XML_REGEXP_START_STATE = 1,
138 XML_REGEXP_FINAL_STATE,
139 XML_REGEXP_TRANS_STATE
140} xmlRegStateType;
141
142typedef enum {
143 XML_REGEXP_MARK_NORMAL = 0,
144 XML_REGEXP_MARK_START,
145 XML_REGEXP_MARK_VISITED
146} xmlRegMarkedType;
147
148typedef struct _xmlRegRange xmlRegRange;
149typedef xmlRegRange *xmlRegRangePtr;
150
151struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000152 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000153 xmlRegAtomType type;
154 int start;
155 int end;
156 xmlChar *blockName;
157};
158
159typedef struct _xmlRegAtom xmlRegAtom;
160typedef xmlRegAtom *xmlRegAtomPtr;
161
162typedef struct _xmlAutomataState xmlRegState;
163typedef xmlRegState *xmlRegStatePtr;
164
165struct _xmlRegAtom {
166 int no;
167 xmlRegAtomType type;
168 xmlRegQuantType quant;
169 int min;
170 int max;
171
172 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000173 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000174 int neg;
175 int codepoint;
176 xmlRegStatePtr start;
177 xmlRegStatePtr stop;
178 int maxRanges;
179 int nbRanges;
180 xmlRegRangePtr *ranges;
181 void *data;
182};
183
184typedef struct _xmlRegCounter xmlRegCounter;
185typedef xmlRegCounter *xmlRegCounterPtr;
186
187struct _xmlRegCounter {
188 int min;
189 int max;
190};
191
192typedef struct _xmlRegTrans xmlRegTrans;
193typedef xmlRegTrans *xmlRegTransPtr;
194
195struct _xmlRegTrans {
196 xmlRegAtomPtr atom;
197 int to;
198 int counter;
199 int count;
200};
201
202struct _xmlAutomataState {
203 xmlRegStateType type;
204 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000205 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000206 int no;
207
208 int maxTrans;
209 int nbTrans;
210 xmlRegTrans *trans;
211};
212
213typedef struct _xmlAutomata xmlRegParserCtxt;
214typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
215
216struct _xmlAutomata {
217 xmlChar *string;
218 xmlChar *cur;
219
220 int error;
221 int neg;
222
223 xmlRegStatePtr start;
224 xmlRegStatePtr end;
225 xmlRegStatePtr state;
226
227 xmlRegAtomPtr atom;
228
229 int maxAtoms;
230 int nbAtoms;
231 xmlRegAtomPtr *atoms;
232
233 int maxStates;
234 int nbStates;
235 xmlRegStatePtr *states;
236
237 int maxCounters;
238 int nbCounters;
239 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000240
241 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000242};
243
244struct _xmlRegexp {
245 xmlChar *string;
246 int nbStates;
247 xmlRegStatePtr *states;
248 int nbAtoms;
249 xmlRegAtomPtr *atoms;
250 int nbCounters;
251 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000252 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000253 /*
254 * That's the compact form for determinists automatas
255 */
256 int nbstates;
257 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000258 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000259 int nbstrings;
260 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000261};
262
263typedef struct _xmlRegExecRollback xmlRegExecRollback;
264typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
265
266struct _xmlRegExecRollback {
267 xmlRegStatePtr state;/* the current state */
268 int index; /* the index in the input stack */
269 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000270 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000271};
272
273typedef struct _xmlRegInputToken xmlRegInputToken;
274typedef xmlRegInputToken *xmlRegInputTokenPtr;
275
276struct _xmlRegInputToken {
277 xmlChar *value;
278 void *data;
279};
280
281struct _xmlRegExecCtxt {
282 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000283 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000284 xmlRegexpPtr comp; /* the compiled regexp */
285 xmlRegExecCallbacks callback;
286 void *data;
287
288 xmlRegStatePtr state;/* the current state */
289 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000290 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000291
292 /*
293 * A stack of rollback states
294 */
295 int maxRollbacks;
296 int nbRollbacks;
297 xmlRegExecRollback *rollbacks;
298
299 /*
300 * The state of the automata if any
301 */
302 int *counts;
303
304 /*
305 * The input stack
306 */
307 int inputStackMax;
308 int inputStackNr;
309 int index;
310 int *charStack;
311 const xmlChar *inputString; /* when operating on characters */
312 xmlRegInputTokenPtr inputStack;/* when operating on strings */
313
314};
315
Daniel Veillard441bc322002-04-20 17:38:48 +0000316#define REGEXP_ALL_COUNTER 0x123456
317#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000318
Daniel Veillard4255d502002-04-16 15:50:10 +0000319static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000320static void xmlRegFreeState(xmlRegStatePtr state);
321static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard4255d502002-04-16 15:50:10 +0000322
323/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000324 * *
325 * Regexp memory error handler *
326 * *
327 ************************************************************************/
328/**
329 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000330 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000331 *
332 * Handle an out of memory condition
333 */
334static void
335xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
336{
337 const char *regexp = NULL;
338 if (ctxt != NULL) {
339 regexp = (const char *) ctxt->string;
340 ctxt->error = XML_ERR_NO_MEMORY;
341 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000342 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000343 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
344 regexp, NULL, 0, 0,
345 "Memory allocation failed : %s\n", extra);
346}
347
348/**
349 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000350 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000351 *
William M. Brackddf71d62004-05-06 04:17:26 +0000352 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000353 */
354static void
355xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
356{
357 const char *regexp = NULL;
358 int idx = 0;
359
360 if (ctxt != NULL) {
361 regexp = (const char *) ctxt->string;
362 idx = ctxt->cur - ctxt->string;
363 ctxt->error = XML_REGEXP_COMPILE_ERROR;
364 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000365 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000366 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
367 regexp, NULL, idx, 0,
368 "failed to compile: %s\n", extra);
369}
370
371/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000372 * *
373 * Allocation/Deallocation *
374 * *
375 ************************************************************************/
376
Daniel Veillard23e73572002-09-19 19:56:43 +0000377static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000378/**
379 * xmlRegEpxFromParse:
380 * @ctxt: the parser context used to build it
381 *
William M. Brackddf71d62004-05-06 04:17:26 +0000382 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000383 *
384 * Returns the new regexp or NULL in case of error
385 */
386static xmlRegexpPtr
387xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
388 xmlRegexpPtr ret;
389
390 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000391 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000392 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000393 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000394 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000395 memset(ret, 0, sizeof(xmlRegexp));
396 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000397 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000398 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000399 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000400 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000401 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000402 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000403 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000404
405 if ((ret->determinist != 0) &&
406 (ret->nbCounters == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000407 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000408 (ret->atoms[0] != NULL) &&
409 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
410 int i, j, nbstates = 0, nbatoms = 0;
411 int *stateRemap;
412 int *stringRemap;
413 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000414 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000415 xmlChar **stringMap;
416 xmlChar *value;
417
418 /*
419 * Switch to a compact representation
420 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000421 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000422 * they are all of the string type
423 * 3/ build a table state x atom for the transitions
424 */
425
426 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000427 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000428 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000429 xmlFree(ret);
430 return(NULL);
431 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000432 for (i = 0;i < ret->nbStates;i++) {
433 if (ret->states[i] != NULL) {
434 stateRemap[i] = nbstates;
435 nbstates++;
436 } else {
437 stateRemap[i] = -1;
438 }
439 }
440#ifdef DEBUG_COMPACTION
441 printf("Final: %d states\n", nbstates);
442#endif
443 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000444 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000445 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000446 xmlFree(stateRemap);
447 xmlFree(ret);
448 return(NULL);
449 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000450 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000451 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000452 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000453 xmlFree(stringMap);
454 xmlFree(stateRemap);
455 xmlFree(ret);
456 return(NULL);
457 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000458 for (i = 0;i < ret->nbAtoms;i++) {
459 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
460 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
461 value = ret->atoms[i]->valuep;
462 for (j = 0;j < nbatoms;j++) {
463 if (xmlStrEqual(stringMap[j], value)) {
464 stringRemap[i] = j;
465 break;
466 }
467 }
468 if (j >= nbatoms) {
469 stringRemap[i] = nbatoms;
470 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000471 if (stringMap[nbatoms] == NULL) {
472 for (i = 0;i < nbatoms;i++)
473 xmlFree(stringMap[i]);
474 xmlFree(stringRemap);
475 xmlFree(stringMap);
476 xmlFree(stateRemap);
477 xmlFree(ret);
478 return(NULL);
479 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000480 nbatoms++;
481 }
482 } else {
483 xmlFree(stateRemap);
484 xmlFree(stringRemap);
485 for (i = 0;i < nbatoms;i++)
486 xmlFree(stringMap[i]);
487 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000488 xmlFree(ret);
489 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000490 }
491 }
492#ifdef DEBUG_COMPACTION
493 printf("Final: %d atoms\n", nbatoms);
494#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000495 transitions = (int *) xmlMalloc((nbstates + 1) *
496 (nbatoms + 1) * sizeof(int));
497 if (transitions == NULL) {
498 xmlFree(stateRemap);
499 xmlFree(stringRemap);
500 xmlFree(stringMap);
501 xmlFree(ret);
502 return(NULL);
503 }
504 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000505
506 /*
507 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000508 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000509 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000510 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000511
512 for (i = 0;i < ret->nbStates;i++) {
513 int stateno, atomno, targetno, prev;
514 xmlRegStatePtr state;
515 xmlRegTransPtr trans;
516
517 stateno = stateRemap[i];
518 if (stateno == -1)
519 continue;
520 state = ret->states[i];
521
522 transitions[stateno * (nbatoms + 1)] = state->type;
523
524 for (j = 0;j < state->nbTrans;j++) {
525 trans = &(state->trans[j]);
526 if ((trans->to == -1) || (trans->atom == NULL))
527 continue;
528 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000529 if ((trans->atom->data != NULL) && (transdata == NULL)) {
530 transdata = (void **) xmlMalloc(nbstates * nbatoms *
531 sizeof(void *));
532 if (transdata != NULL)
533 memset(transdata, 0,
534 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000535 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000536 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000537 break;
538 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000539 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000540 targetno = stateRemap[trans->to];
541 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000542 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000543 * states then it means the automata is not determinist and
544 * the compact form can't be used !
545 */
546 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
547 if (prev != 0) {
548 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000549 ret->determinist = 0;
550#ifdef DEBUG_COMPACTION
551 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
552 i, j, trans->atom->no, trans->to, atomno, targetno);
553 printf(" previous to is %d\n", prev);
554#endif
555 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000556 if (transdata != NULL)
557 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000558 xmlFree(transitions);
559 xmlFree(stateRemap);
560 xmlFree(stringRemap);
561 for (i = 0;i < nbatoms;i++)
562 xmlFree(stringMap[i]);
563 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000564 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000565 }
566 } else {
567#if 0
568 printf("State %d trans %d: atom %d to %d : %d to %d\n",
569 i, j, trans->atom->no, trans->to, atomno, targetno);
570#endif
571 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000572 targetno + 1; /* to avoid 0 */
573 if (transdata != NULL)
574 transdata[stateno * nbatoms + atomno] =
575 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000576 }
577 }
578 }
579 ret->determinist = 1;
580#ifdef DEBUG_COMPACTION
581 /*
582 * Debug
583 */
584 for (i = 0;i < nbstates;i++) {
585 for (j = 0;j < nbatoms + 1;j++) {
586 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
587 }
588 printf("\n");
589 }
590 printf("\n");
591#endif
592 /*
593 * Cleanup of the old data
594 */
595 if (ret->states != NULL) {
596 for (i = 0;i < ret->nbStates;i++)
597 xmlRegFreeState(ret->states[i]);
598 xmlFree(ret->states);
599 }
600 ret->states = NULL;
601 ret->nbStates = 0;
602 if (ret->atoms != NULL) {
603 for (i = 0;i < ret->nbAtoms;i++)
604 xmlRegFreeAtom(ret->atoms[i]);
605 xmlFree(ret->atoms);
606 }
607 ret->atoms = NULL;
608 ret->nbAtoms = 0;
609
610 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000611 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000612 ret->stringMap = stringMap;
613 ret->nbstrings = nbatoms;
614 ret->nbstates = nbstates;
615 xmlFree(stateRemap);
616 xmlFree(stringRemap);
617 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000618not_determ:
619 ctxt->string = NULL;
620 ctxt->nbStates = 0;
621 ctxt->states = NULL;
622 ctxt->nbAtoms = 0;
623 ctxt->atoms = NULL;
624 ctxt->nbCounters = 0;
625 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000626 return(ret);
627}
628
629/**
630 * xmlRegNewParserCtxt:
631 * @string: the string to parse
632 *
633 * Allocate a new regexp parser context
634 *
635 * Returns the new context or NULL in case of error
636 */
637static xmlRegParserCtxtPtr
638xmlRegNewParserCtxt(const xmlChar *string) {
639 xmlRegParserCtxtPtr ret;
640
641 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
642 if (ret == NULL)
643 return(NULL);
644 memset(ret, 0, sizeof(xmlRegParserCtxt));
645 if (string != NULL)
646 ret->string = xmlStrdup(string);
647 ret->cur = ret->string;
648 ret->neg = 0;
649 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000650 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000651 return(ret);
652}
653
654/**
655 * xmlRegNewRange:
656 * @ctxt: the regexp parser context
657 * @neg: is that negative
658 * @type: the type of range
659 * @start: the start codepoint
660 * @end: the end codepoint
661 *
662 * Allocate a new regexp range
663 *
664 * Returns the new range or NULL in case of error
665 */
666static xmlRegRangePtr
667xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
668 int neg, xmlRegAtomType type, int start, int end) {
669 xmlRegRangePtr ret;
670
671 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
672 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000673 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000674 return(NULL);
675 }
676 ret->neg = neg;
677 ret->type = type;
678 ret->start = start;
679 ret->end = end;
680 return(ret);
681}
682
683/**
684 * xmlRegFreeRange:
685 * @range: the regexp range
686 *
687 * Free a regexp range
688 */
689static void
690xmlRegFreeRange(xmlRegRangePtr range) {
691 if (range == NULL)
692 return;
693
694 if (range->blockName != NULL)
695 xmlFree(range->blockName);
696 xmlFree(range);
697}
698
699/**
700 * xmlRegNewAtom:
701 * @ctxt: the regexp parser context
702 * @type: the type of atom
703 *
704 * Allocate a new regexp range
705 *
706 * Returns the new atom or NULL in case of error
707 */
708static xmlRegAtomPtr
709xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
710 xmlRegAtomPtr ret;
711
712 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
713 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000714 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000715 return(NULL);
716 }
717 memset(ret, 0, sizeof(xmlRegAtom));
718 ret->type = type;
719 ret->quant = XML_REGEXP_QUANT_ONCE;
720 ret->min = 0;
721 ret->max = 0;
722 return(ret);
723}
724
725/**
726 * xmlRegFreeAtom:
727 * @atom: the regexp atom
728 *
729 * Free a regexp atom
730 */
731static void
732xmlRegFreeAtom(xmlRegAtomPtr atom) {
733 int i;
734
735 if (atom == NULL)
736 return;
737
738 for (i = 0;i < atom->nbRanges;i++)
739 xmlRegFreeRange(atom->ranges[i]);
740 if (atom->ranges != NULL)
741 xmlFree(atom->ranges);
742 if (atom->type == XML_REGEXP_STRING)
743 xmlFree(atom->valuep);
744 xmlFree(atom);
745}
746
747static xmlRegStatePtr
748xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
749 xmlRegStatePtr ret;
750
751 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
752 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000753 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000754 return(NULL);
755 }
756 memset(ret, 0, sizeof(xmlRegState));
757 ret->type = XML_REGEXP_TRANS_STATE;
758 ret->mark = XML_REGEXP_MARK_NORMAL;
759 return(ret);
760}
761
762/**
763 * xmlRegFreeState:
764 * @state: the regexp state
765 *
766 * Free a regexp state
767 */
768static void
769xmlRegFreeState(xmlRegStatePtr state) {
770 if (state == NULL)
771 return;
772
773 if (state->trans != NULL)
774 xmlFree(state->trans);
775 xmlFree(state);
776}
777
778/**
779 * xmlRegFreeParserCtxt:
780 * @ctxt: the regexp parser context
781 *
782 * Free a regexp parser context
783 */
784static void
785xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
786 int i;
787 if (ctxt == NULL)
788 return;
789
790 if (ctxt->string != NULL)
791 xmlFree(ctxt->string);
792 if (ctxt->states != NULL) {
793 for (i = 0;i < ctxt->nbStates;i++)
794 xmlRegFreeState(ctxt->states[i]);
795 xmlFree(ctxt->states);
796 }
797 if (ctxt->atoms != NULL) {
798 for (i = 0;i < ctxt->nbAtoms;i++)
799 xmlRegFreeAtom(ctxt->atoms[i]);
800 xmlFree(ctxt->atoms);
801 }
802 if (ctxt->counters != NULL)
803 xmlFree(ctxt->counters);
804 xmlFree(ctxt);
805}
806
807/************************************************************************
808 * *
809 * Display of Data structures *
810 * *
811 ************************************************************************/
812
813static void
814xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
815 switch (type) {
816 case XML_REGEXP_EPSILON:
817 fprintf(output, "epsilon "); break;
818 case XML_REGEXP_CHARVAL:
819 fprintf(output, "charval "); break;
820 case XML_REGEXP_RANGES:
821 fprintf(output, "ranges "); break;
822 case XML_REGEXP_SUBREG:
823 fprintf(output, "subexpr "); break;
824 case XML_REGEXP_STRING:
825 fprintf(output, "string "); break;
826 case XML_REGEXP_ANYCHAR:
827 fprintf(output, "anychar "); break;
828 case XML_REGEXP_ANYSPACE:
829 fprintf(output, "anyspace "); break;
830 case XML_REGEXP_NOTSPACE:
831 fprintf(output, "notspace "); break;
832 case XML_REGEXP_INITNAME:
833 fprintf(output, "initname "); break;
834 case XML_REGEXP_NOTINITNAME:
835 fprintf(output, "notinitname "); break;
836 case XML_REGEXP_NAMECHAR:
837 fprintf(output, "namechar "); break;
838 case XML_REGEXP_NOTNAMECHAR:
839 fprintf(output, "notnamechar "); break;
840 case XML_REGEXP_DECIMAL:
841 fprintf(output, "decimal "); break;
842 case XML_REGEXP_NOTDECIMAL:
843 fprintf(output, "notdecimal "); break;
844 case XML_REGEXP_REALCHAR:
845 fprintf(output, "realchar "); break;
846 case XML_REGEXP_NOTREALCHAR:
847 fprintf(output, "notrealchar "); break;
848 case XML_REGEXP_LETTER:
849 fprintf(output, "LETTER "); break;
850 case XML_REGEXP_LETTER_UPPERCASE:
851 fprintf(output, "LETTER_UPPERCASE "); break;
852 case XML_REGEXP_LETTER_LOWERCASE:
853 fprintf(output, "LETTER_LOWERCASE "); break;
854 case XML_REGEXP_LETTER_TITLECASE:
855 fprintf(output, "LETTER_TITLECASE "); break;
856 case XML_REGEXP_LETTER_MODIFIER:
857 fprintf(output, "LETTER_MODIFIER "); break;
858 case XML_REGEXP_LETTER_OTHERS:
859 fprintf(output, "LETTER_OTHERS "); break;
860 case XML_REGEXP_MARK:
861 fprintf(output, "MARK "); break;
862 case XML_REGEXP_MARK_NONSPACING:
863 fprintf(output, "MARK_NONSPACING "); break;
864 case XML_REGEXP_MARK_SPACECOMBINING:
865 fprintf(output, "MARK_SPACECOMBINING "); break;
866 case XML_REGEXP_MARK_ENCLOSING:
867 fprintf(output, "MARK_ENCLOSING "); break;
868 case XML_REGEXP_NUMBER:
869 fprintf(output, "NUMBER "); break;
870 case XML_REGEXP_NUMBER_DECIMAL:
871 fprintf(output, "NUMBER_DECIMAL "); break;
872 case XML_REGEXP_NUMBER_LETTER:
873 fprintf(output, "NUMBER_LETTER "); break;
874 case XML_REGEXP_NUMBER_OTHERS:
875 fprintf(output, "NUMBER_OTHERS "); break;
876 case XML_REGEXP_PUNCT:
877 fprintf(output, "PUNCT "); break;
878 case XML_REGEXP_PUNCT_CONNECTOR:
879 fprintf(output, "PUNCT_CONNECTOR "); break;
880 case XML_REGEXP_PUNCT_DASH:
881 fprintf(output, "PUNCT_DASH "); break;
882 case XML_REGEXP_PUNCT_OPEN:
883 fprintf(output, "PUNCT_OPEN "); break;
884 case XML_REGEXP_PUNCT_CLOSE:
885 fprintf(output, "PUNCT_CLOSE "); break;
886 case XML_REGEXP_PUNCT_INITQUOTE:
887 fprintf(output, "PUNCT_INITQUOTE "); break;
888 case XML_REGEXP_PUNCT_FINQUOTE:
889 fprintf(output, "PUNCT_FINQUOTE "); break;
890 case XML_REGEXP_PUNCT_OTHERS:
891 fprintf(output, "PUNCT_OTHERS "); break;
892 case XML_REGEXP_SEPAR:
893 fprintf(output, "SEPAR "); break;
894 case XML_REGEXP_SEPAR_SPACE:
895 fprintf(output, "SEPAR_SPACE "); break;
896 case XML_REGEXP_SEPAR_LINE:
897 fprintf(output, "SEPAR_LINE "); break;
898 case XML_REGEXP_SEPAR_PARA:
899 fprintf(output, "SEPAR_PARA "); break;
900 case XML_REGEXP_SYMBOL:
901 fprintf(output, "SYMBOL "); break;
902 case XML_REGEXP_SYMBOL_MATH:
903 fprintf(output, "SYMBOL_MATH "); break;
904 case XML_REGEXP_SYMBOL_CURRENCY:
905 fprintf(output, "SYMBOL_CURRENCY "); break;
906 case XML_REGEXP_SYMBOL_MODIFIER:
907 fprintf(output, "SYMBOL_MODIFIER "); break;
908 case XML_REGEXP_SYMBOL_OTHERS:
909 fprintf(output, "SYMBOL_OTHERS "); break;
910 case XML_REGEXP_OTHER:
911 fprintf(output, "OTHER "); break;
912 case XML_REGEXP_OTHER_CONTROL:
913 fprintf(output, "OTHER_CONTROL "); break;
914 case XML_REGEXP_OTHER_FORMAT:
915 fprintf(output, "OTHER_FORMAT "); break;
916 case XML_REGEXP_OTHER_PRIVATE:
917 fprintf(output, "OTHER_PRIVATE "); break;
918 case XML_REGEXP_OTHER_NA:
919 fprintf(output, "OTHER_NA "); break;
920 case XML_REGEXP_BLOCK_NAME:
921 fprintf(output, "BLOCK "); break;
922 }
923}
924
925static void
926xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
927 switch (type) {
928 case XML_REGEXP_QUANT_EPSILON:
929 fprintf(output, "epsilon "); break;
930 case XML_REGEXP_QUANT_ONCE:
931 fprintf(output, "once "); break;
932 case XML_REGEXP_QUANT_OPT:
933 fprintf(output, "? "); break;
934 case XML_REGEXP_QUANT_MULT:
935 fprintf(output, "* "); break;
936 case XML_REGEXP_QUANT_PLUS:
937 fprintf(output, "+ "); break;
938 case XML_REGEXP_QUANT_RANGE:
939 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000940 case XML_REGEXP_QUANT_ONCEONLY:
941 fprintf(output, "onceonly "); break;
942 case XML_REGEXP_QUANT_ALL:
943 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000944 }
945}
946static void
947xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
948 fprintf(output, " range: ");
949 if (range->neg)
950 fprintf(output, "negative ");
951 xmlRegPrintAtomType(output, range->type);
952 fprintf(output, "%c - %c\n", range->start, range->end);
953}
954
955static void
956xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
957 fprintf(output, " atom: ");
958 if (atom == NULL) {
959 fprintf(output, "NULL\n");
960 return;
961 }
962 xmlRegPrintAtomType(output, atom->type);
963 xmlRegPrintQuantType(output, atom->quant);
964 if (atom->quant == XML_REGEXP_QUANT_RANGE)
965 fprintf(output, "%d-%d ", atom->min, atom->max);
966 if (atom->type == XML_REGEXP_STRING)
967 fprintf(output, "'%s' ", (char *) atom->valuep);
968 if (atom->type == XML_REGEXP_CHARVAL)
969 fprintf(output, "char %c\n", atom->codepoint);
970 else if (atom->type == XML_REGEXP_RANGES) {
971 int i;
972 fprintf(output, "%d entries\n", atom->nbRanges);
973 for (i = 0; i < atom->nbRanges;i++)
974 xmlRegPrintRange(output, atom->ranges[i]);
975 } else if (atom->type == XML_REGEXP_SUBREG) {
976 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
977 } else {
978 fprintf(output, "\n");
979 }
980}
981
982static void
983xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
984 fprintf(output, " trans: ");
985 if (trans == NULL) {
986 fprintf(output, "NULL\n");
987 return;
988 }
989 if (trans->to < 0) {
990 fprintf(output, "removed\n");
991 return;
992 }
993 if (trans->counter >= 0) {
994 fprintf(output, "counted %d, ", trans->counter);
995 }
Daniel Veillard8a001f62002-04-20 07:24:11 +0000996 if (trans->count == REGEXP_ALL_COUNTER) {
997 fprintf(output, "all transition, ");
998 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000999 fprintf(output, "count based %d, ", trans->count);
1000 }
1001 if (trans->atom == NULL) {
1002 fprintf(output, "epsilon to %d\n", trans->to);
1003 return;
1004 }
1005 if (trans->atom->type == XML_REGEXP_CHARVAL)
1006 fprintf(output, "char %c ", trans->atom->codepoint);
1007 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1008}
1009
1010static void
1011xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1012 int i;
1013
1014 fprintf(output, " state: ");
1015 if (state == NULL) {
1016 fprintf(output, "NULL\n");
1017 return;
1018 }
1019 if (state->type == XML_REGEXP_START_STATE)
1020 fprintf(output, "START ");
1021 if (state->type == XML_REGEXP_FINAL_STATE)
1022 fprintf(output, "FINAL ");
1023
1024 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1025 for (i = 0;i < state->nbTrans; i++) {
1026 xmlRegPrintTrans(output, &(state->trans[i]));
1027 }
1028}
1029
Daniel Veillard23e73572002-09-19 19:56:43 +00001030#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001031static void
1032xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1033 int i;
1034
1035 fprintf(output, " ctxt: ");
1036 if (ctxt == NULL) {
1037 fprintf(output, "NULL\n");
1038 return;
1039 }
1040 fprintf(output, "'%s' ", ctxt->string);
1041 if (ctxt->error)
1042 fprintf(output, "error ");
1043 if (ctxt->neg)
1044 fprintf(output, "neg ");
1045 fprintf(output, "\n");
1046 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1047 for (i = 0;i < ctxt->nbAtoms; i++) {
1048 fprintf(output, " %02d ", i);
1049 xmlRegPrintAtom(output, ctxt->atoms[i]);
1050 }
1051 if (ctxt->atom != NULL) {
1052 fprintf(output, "current atom:\n");
1053 xmlRegPrintAtom(output, ctxt->atom);
1054 }
1055 fprintf(output, "%d states:", ctxt->nbStates);
1056 if (ctxt->start != NULL)
1057 fprintf(output, " start: %d", ctxt->start->no);
1058 if (ctxt->end != NULL)
1059 fprintf(output, " end: %d", ctxt->end->no);
1060 fprintf(output, "\n");
1061 for (i = 0;i < ctxt->nbStates; i++) {
1062 xmlRegPrintState(output, ctxt->states[i]);
1063 }
1064 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1065 for (i = 0;i < ctxt->nbCounters; i++) {
1066 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1067 ctxt->counters[i].max);
1068 }
1069}
Daniel Veillard23e73572002-09-19 19:56:43 +00001070#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001071
1072/************************************************************************
1073 * *
1074 * Finite Automata structures manipulations *
1075 * *
1076 ************************************************************************/
1077
1078static void
1079xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1080 int neg, xmlRegAtomType type, int start, int end,
1081 xmlChar *blockName) {
1082 xmlRegRangePtr range;
1083
1084 if (atom == NULL) {
1085 ERROR("add range: atom is NULL");
1086 return;
1087 }
1088 if (atom->type != XML_REGEXP_RANGES) {
1089 ERROR("add range: atom is not ranges");
1090 return;
1091 }
1092 if (atom->maxRanges == 0) {
1093 atom->maxRanges = 4;
1094 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1095 sizeof(xmlRegRangePtr));
1096 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001097 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001098 atom->maxRanges = 0;
1099 return;
1100 }
1101 } else if (atom->nbRanges >= atom->maxRanges) {
1102 xmlRegRangePtr *tmp;
1103 atom->maxRanges *= 2;
1104 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1105 sizeof(xmlRegRangePtr));
1106 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001107 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001108 atom->maxRanges /= 2;
1109 return;
1110 }
1111 atom->ranges = tmp;
1112 }
1113 range = xmlRegNewRange(ctxt, neg, type, start, end);
1114 if (range == NULL)
1115 return;
1116 range->blockName = blockName;
1117 atom->ranges[atom->nbRanges++] = range;
1118
1119}
1120
1121static int
1122xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1123 if (ctxt->maxCounters == 0) {
1124 ctxt->maxCounters = 4;
1125 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1126 sizeof(xmlRegCounter));
1127 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001128 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001129 ctxt->maxCounters = 0;
1130 return(-1);
1131 }
1132 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1133 xmlRegCounter *tmp;
1134 ctxt->maxCounters *= 2;
1135 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1136 sizeof(xmlRegCounter));
1137 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001138 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001139 ctxt->maxCounters /= 2;
1140 return(-1);
1141 }
1142 ctxt->counters = tmp;
1143 }
1144 ctxt->counters[ctxt->nbCounters].min = -1;
1145 ctxt->counters[ctxt->nbCounters].max = -1;
1146 return(ctxt->nbCounters++);
1147}
1148
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001149static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001150xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1151 if (atom == NULL) {
1152 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001153 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001154 }
1155 if (ctxt->maxAtoms == 0) {
1156 ctxt->maxAtoms = 4;
1157 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1158 sizeof(xmlRegAtomPtr));
1159 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001160 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001161 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001162 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001163 }
1164 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1165 xmlRegAtomPtr *tmp;
1166 ctxt->maxAtoms *= 2;
1167 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1168 sizeof(xmlRegAtomPtr));
1169 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001170 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001171 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001172 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001173 }
1174 ctxt->atoms = tmp;
1175 }
1176 atom->no = ctxt->nbAtoms;
1177 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001178 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001179}
1180
1181static void
1182xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1183 xmlRegAtomPtr atom, xmlRegStatePtr target,
1184 int counter, int count) {
1185 if (state == NULL) {
1186 ERROR("add state: state is NULL");
1187 return;
1188 }
1189 if (target == NULL) {
1190 ERROR("add state: target is NULL");
1191 return;
1192 }
1193 if (state->maxTrans == 0) {
1194 state->maxTrans = 4;
1195 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1196 sizeof(xmlRegTrans));
1197 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001198 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001199 state->maxTrans = 0;
1200 return;
1201 }
1202 } else if (state->nbTrans >= state->maxTrans) {
1203 xmlRegTrans *tmp;
1204 state->maxTrans *= 2;
1205 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1206 sizeof(xmlRegTrans));
1207 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001208 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001209 state->maxTrans /= 2;
1210 return;
1211 }
1212 state->trans = tmp;
1213 }
1214#ifdef DEBUG_REGEXP_GRAPH
1215 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001216 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001217 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001218 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001219 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001220 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001221 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001222 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001223 printf("epsilon transition\n");
1224 else if (atom != NULL)
1225 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001226#endif
1227
1228 state->trans[state->nbTrans].atom = atom;
1229 state->trans[state->nbTrans].to = target->no;
1230 state->trans[state->nbTrans].counter = counter;
1231 state->trans[state->nbTrans].count = count;
1232 state->nbTrans++;
1233}
1234
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001235static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001236xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001237 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001238 if (ctxt->maxStates == 0) {
1239 ctxt->maxStates = 4;
1240 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1241 sizeof(xmlRegStatePtr));
1242 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001243 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001244 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001245 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001246 }
1247 } else if (ctxt->nbStates >= ctxt->maxStates) {
1248 xmlRegStatePtr *tmp;
1249 ctxt->maxStates *= 2;
1250 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1251 sizeof(xmlRegStatePtr));
1252 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001253 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001254 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001255 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001256 }
1257 ctxt->states = tmp;
1258 }
1259 state->no = ctxt->nbStates;
1260 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001261 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001262}
1263
1264/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001265 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001266 * @ctxt: a regexp parser context
1267 * @from: the from state
1268 * @to: the target state or NULL for building a new one
1269 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001270 *
1271 */
1272static void
1273xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001274 xmlRegStatePtr from, xmlRegStatePtr to,
1275 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001276 if (to == NULL) {
1277 to = xmlRegNewState(ctxt);
1278 xmlRegStatePush(ctxt, to);
1279 ctxt->state = to;
1280 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001281 if (lax)
1282 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1283 else
1284 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001285}
1286
1287/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001288 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001289 * @ctxt: a regexp parser context
1290 * @from: the from state
1291 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001292 *
1293 */
1294static void
1295xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1296 xmlRegStatePtr from, xmlRegStatePtr to) {
1297 if (to == NULL) {
1298 to = xmlRegNewState(ctxt);
1299 xmlRegStatePush(ctxt, to);
1300 ctxt->state = to;
1301 }
1302 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1303}
1304
1305/**
1306 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001307 * @ctxt: a regexp parser context
1308 * @from: the from state
1309 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001310 * counter: the counter for that transition
1311 *
1312 */
1313static void
1314xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1315 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1316 if (to == NULL) {
1317 to = xmlRegNewState(ctxt);
1318 xmlRegStatePush(ctxt, to);
1319 ctxt->state = to;
1320 }
1321 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1322}
1323
1324/**
1325 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001326 * @ctxt: a regexp parser context
1327 * @from: the from state
1328 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001329 * counter: the counter for that transition
1330 *
1331 */
1332static void
1333xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1334 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1335 if (to == NULL) {
1336 to = xmlRegNewState(ctxt);
1337 xmlRegStatePush(ctxt, to);
1338 ctxt->state = to;
1339 }
1340 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1341}
1342
1343/**
1344 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001345 * @ctxt: a regexp parser context
1346 * @from: the from state
1347 * @to: the target state or NULL for building a new one
1348 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001349 *
William M. Brackddf71d62004-05-06 04:17:26 +00001350 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001351 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001352static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001353xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1354 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1355 if (atom == NULL) {
1356 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001357 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001358 }
1359 if (atom->type == XML_REGEXP_SUBREG) {
1360 /*
1361 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001362 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001363 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001364 if (xmlRegAtomPush(ctxt, atom) < 0) {
1365 return(-1);
1366 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001367 if ((to != NULL) && (atom->stop != to) &&
1368 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1369 /*
1370 * Generate an epsilon transition to link to the target
1371 */
1372 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1373 }
1374 switch (atom->quant) {
1375 case XML_REGEXP_QUANT_OPT:
1376 atom->quant = XML_REGEXP_QUANT_ONCE;
1377 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1378 break;
1379 case XML_REGEXP_QUANT_MULT:
1380 atom->quant = XML_REGEXP_QUANT_ONCE;
1381 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1382 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1383 break;
1384 case XML_REGEXP_QUANT_PLUS:
1385 atom->quant = XML_REGEXP_QUANT_ONCE;
1386 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1387 break;
1388 case XML_REGEXP_QUANT_RANGE: {
1389 int counter;
1390 xmlRegStatePtr newstate;
1391
1392 /*
1393 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001394 * 1/ if range has minOccurs == 0, create a new state
1395 * and create epsilon transitions from atom->start
1396 * to atom->stop, as well as atom->start to the new
1397 * state
1398 * 2/ register a new counter
1399 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001400 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001401 * 4/ create a new state
1402 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001403 * that state
1404 */
William M. Brackddf71d62004-05-06 04:17:26 +00001405 if (atom->min == 0) {
1406 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1407 atom->stop);
1408 newstate = xmlRegNewState(ctxt);
1409 xmlRegStatePush(ctxt, newstate);
1410 ctxt->state = newstate;
1411 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1412 newstate);
1413 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001414 counter = xmlRegGetCounter(ctxt);
1415 ctxt->counters[counter].min = atom->min - 1;
1416 ctxt->counters[counter].max = atom->max - 1;
1417 atom->min = 0;
1418 atom->max = 0;
1419 atom->quant = XML_REGEXP_QUANT_ONCE;
1420 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1421 atom->start, counter);
1422 if (to != NULL) {
1423 newstate = to;
1424 } else {
1425 newstate = xmlRegNewState(ctxt);
1426 xmlRegStatePush(ctxt, newstate);
1427 ctxt->state = newstate;
1428 }
1429 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1430 newstate, counter);
1431 }
1432 default:
1433 break;
1434 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001435 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001436 } else {
1437 if (to == NULL) {
1438 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001439 if (to != NULL)
1440 xmlRegStatePush(ctxt, to);
1441 else {
1442 return(-1);
1443 }
1444 }
1445 if (xmlRegAtomPush(ctxt, atom) < 0) {
1446 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001447 }
1448 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001449 ctxt->state = to;
1450 }
1451 switch (atom->quant) {
1452 case XML_REGEXP_QUANT_OPT:
1453 atom->quant = XML_REGEXP_QUANT_ONCE;
1454 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1455 break;
1456 case XML_REGEXP_QUANT_MULT:
1457 atom->quant = XML_REGEXP_QUANT_ONCE;
1458 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1459 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1460 break;
1461 case XML_REGEXP_QUANT_PLUS:
1462 atom->quant = XML_REGEXP_QUANT_ONCE;
1463 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1464 break;
1465 default:
1466 break;
1467 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001468 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001469}
1470
1471/**
1472 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001473 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001474 * @fromnr: the from state
1475 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001476 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001477 *
1478 */
1479static void
1480xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1481 int tonr, int counter) {
1482 int transnr;
1483 xmlRegStatePtr from;
1484 xmlRegStatePtr to;
1485
1486#ifdef DEBUG_REGEXP_GRAPH
1487 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1488#endif
1489 from = ctxt->states[fromnr];
1490 if (from == NULL)
1491 return;
1492 to = ctxt->states[tonr];
1493 if (to == NULL)
1494 return;
1495 if ((to->mark == XML_REGEXP_MARK_START) ||
1496 (to->mark == XML_REGEXP_MARK_VISITED))
1497 return;
1498
1499 to->mark = XML_REGEXP_MARK_VISITED;
1500 if (to->type == XML_REGEXP_FINAL_STATE) {
1501#ifdef DEBUG_REGEXP_GRAPH
1502 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1503#endif
1504 from->type = XML_REGEXP_FINAL_STATE;
1505 }
1506 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1507 if (to->trans[transnr].atom == NULL) {
1508 /*
1509 * Don't remove counted transitions
1510 * Don't loop either
1511 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001512 if (to->trans[transnr].to != fromnr) {
1513 if (to->trans[transnr].count >= 0) {
1514 int newto = to->trans[transnr].to;
1515
1516 xmlRegStateAddTrans(ctxt, from, NULL,
1517 ctxt->states[newto],
1518 -1, to->trans[transnr].count);
1519 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001520#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001521 printf("Found epsilon trans %d from %d to %d\n",
1522 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001523#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001524 if (to->trans[transnr].counter >= 0) {
1525 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1526 to->trans[transnr].to,
1527 to->trans[transnr].counter);
1528 } else {
1529 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1530 to->trans[transnr].to,
1531 counter);
1532 }
1533 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001534 }
1535 } else {
1536 int newto = to->trans[transnr].to;
1537
Daniel Veillardb509f152002-04-17 16:28:10 +00001538 if (to->trans[transnr].counter >= 0) {
1539 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1540 ctxt->states[newto],
1541 to->trans[transnr].counter, -1);
1542 } else {
1543 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1544 ctxt->states[newto], counter, -1);
1545 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001546 }
1547 }
1548 to->mark = XML_REGEXP_MARK_NORMAL;
1549}
1550
1551/**
1552 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001553 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001554 *
1555 */
1556static void
1557xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1558 int statenr, transnr;
1559 xmlRegStatePtr state;
1560
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001561 if (ctxt->states == NULL) return;
1562
1563
Daniel Veillard4255d502002-04-16 15:50:10 +00001564 /*
1565 * build the completed transitions bypassing the epsilons
1566 * Use a marking algorithm to avoid loops
1567 */
1568 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1569 state = ctxt->states[statenr];
1570 if (state == NULL)
1571 continue;
1572 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1573 if ((state->trans[transnr].atom == NULL) &&
1574 (state->trans[transnr].to >= 0)) {
1575 if (state->trans[transnr].to == statenr) {
1576 state->trans[transnr].to = -1;
1577#ifdef DEBUG_REGEXP_GRAPH
1578 printf("Removed loopback epsilon trans %d on %d\n",
1579 transnr, statenr);
1580#endif
1581 } else if (state->trans[transnr].count < 0) {
1582 int newto = state->trans[transnr].to;
1583
1584#ifdef DEBUG_REGEXP_GRAPH
1585 printf("Found epsilon trans %d from %d to %d\n",
1586 transnr, statenr, newto);
1587#endif
1588 state->mark = XML_REGEXP_MARK_START;
1589 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1590 newto, state->trans[transnr].counter);
1591 state->mark = XML_REGEXP_MARK_NORMAL;
1592#ifdef DEBUG_REGEXP_GRAPH
1593 } else {
1594 printf("Found counted transition %d on %d\n",
1595 transnr, statenr);
1596#endif
1597 }
1598 }
1599 }
1600 }
1601 /*
1602 * Eliminate the epsilon transitions
1603 */
1604 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1605 state = ctxt->states[statenr];
1606 if (state == NULL)
1607 continue;
1608 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1609 if ((state->trans[transnr].atom == NULL) &&
1610 (state->trans[transnr].count < 0) &&
1611 (state->trans[transnr].to >= 0)) {
1612 state->trans[transnr].to = -1;
1613 }
1614 }
1615 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001616
1617 /*
1618 * Use this pass to detect unreachable states too
1619 */
1620 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1621 state = ctxt->states[statenr];
1622 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001623 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001624 }
1625 state = ctxt->states[0];
1626 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001627 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001628 while (state != NULL) {
1629 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001630 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001631 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001632 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001633 */
1634 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1635 if ((state->trans[transnr].to >= 0) &&
1636 ((state->trans[transnr].atom != NULL) ||
1637 (state->trans[transnr].count >= 0))) {
1638 int newto = state->trans[transnr].to;
1639
1640 if (ctxt->states[newto] == NULL)
1641 continue;
William M. Brack779af002003-08-01 15:55:39 +00001642 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1643 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001644 target = ctxt->states[newto];
1645 }
1646 }
1647 }
1648 /*
1649 * find the next accessible state not explored
1650 */
1651 if (target == NULL) {
1652 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1653 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001654 if ((state != NULL) && (state->reached ==
1655 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001656 target = state;
1657 break;
1658 }
1659 }
1660 }
1661 state = target;
1662 }
1663 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1664 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001665 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001666#ifdef DEBUG_REGEXP_GRAPH
1667 printf("Removed unreachable state %d\n", statenr);
1668#endif
1669 xmlRegFreeState(state);
1670 ctxt->states[statenr] = NULL;
1671 }
1672 }
1673
Daniel Veillard4255d502002-04-16 15:50:10 +00001674}
1675
Daniel Veillarde19fc232002-04-22 16:01:24 +00001676/**
1677 * xmlFACompareAtoms:
1678 * @atom1: an atom
1679 * @atom2: an atom
1680 *
William M. Brackddf71d62004-05-06 04:17:26 +00001681 * Compares two atoms to check whether they are equivalents
Daniel Veillarde19fc232002-04-22 16:01:24 +00001682 *
1683 * Returns 1 if yes and 0 otherwise
1684 */
1685static int
1686xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1687 if (atom1 == atom2)
1688 return(1);
1689 if ((atom1 == NULL) || (atom2 == NULL))
1690 return(0);
1691
1692 if (atom1->type != atom2->type)
1693 return(0);
1694 switch (atom1->type) {
1695 case XML_REGEXP_STRING:
1696 return(xmlStrEqual((xmlChar *)atom1->valuep,
1697 (xmlChar *)atom2->valuep));
1698 case XML_REGEXP_EPSILON:
1699 return(1);
1700 case XML_REGEXP_CHARVAL:
1701 return(atom1->codepoint == atom2->codepoint);
1702 case XML_REGEXP_RANGES:
1703 TODO;
1704 return(0);
1705 default:
1706 break;
1707 }
1708 return(1);
1709}
1710
1711/**
1712 * xmlFARecurseDeterminism:
1713 * @ctxt: a regexp parser context
1714 *
1715 * Check whether the associated regexp is determinist,
1716 * should be called after xmlFAEliminateEpsilonTransitions()
1717 *
1718 */
1719static int
1720xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1721 int to, xmlRegAtomPtr atom) {
1722 int ret = 1;
1723 int transnr;
1724 xmlRegTransPtr t1;
1725
1726 if (state == NULL)
1727 return(ret);
1728 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1729 t1 = &(state->trans[transnr]);
1730 /*
1731 * check transitions conflicting with the one looked at
1732 */
1733 if (t1->atom == NULL) {
1734 if (t1->to == -1)
1735 continue;
1736 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1737 to, atom);
1738 if (ret == 0)
1739 return(0);
1740 continue;
1741 }
1742 if (t1->to != to)
1743 continue;
1744 if (xmlFACompareAtoms(t1->atom, atom))
1745 return(0);
1746 }
1747 return(ret);
1748}
1749
1750/**
1751 * xmlFAComputesDeterminism:
1752 * @ctxt: a regexp parser context
1753 *
1754 * Check whether the associated regexp is determinist,
1755 * should be called after xmlFAEliminateEpsilonTransitions()
1756 *
1757 */
1758static int
1759xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1760 int statenr, transnr;
1761 xmlRegStatePtr state;
1762 xmlRegTransPtr t1, t2;
1763 int i;
1764 int ret = 1;
1765
Daniel Veillard4402ab42002-09-12 16:02:56 +00001766#ifdef DEBUG_REGEXP_GRAPH
1767 printf("xmlFAComputesDeterminism\n");
1768 xmlRegPrintCtxt(stdout, ctxt);
1769#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001770 if (ctxt->determinist != -1)
1771 return(ctxt->determinist);
1772
1773 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001774 * Check for all states that there aren't 2 transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00001775 * with the same atom and a different target.
1776 */
1777 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1778 state = ctxt->states[statenr];
1779 if (state == NULL)
1780 continue;
1781 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1782 t1 = &(state->trans[transnr]);
1783 /*
1784 * Determinism checks in case of counted or all transitions
1785 * will have to be handled separately
1786 */
1787 if (t1->atom == NULL)
1788 continue;
1789 if (t1->to == -1) /* eliminated */
1790 continue;
1791 for (i = 0;i < transnr;i++) {
1792 t2 = &(state->trans[i]);
1793 if (t2->to == -1) /* eliminated */
1794 continue;
1795 if (t2->atom != NULL) {
1796 if (t1->to == t2->to) {
1797 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00001798 t2->to = -1; /* eliminated */
Daniel Veillarde19fc232002-04-22 16:01:24 +00001799 } else {
1800 /* not determinist ! */
1801 if (xmlFACompareAtoms(t1->atom, t2->atom))
1802 ret = 0;
1803 }
1804 } else if (t1->to != -1) {
1805 /*
1806 * do the closure in case of remaining specific
1807 * epsilon transitions like choices or all
1808 */
1809 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1810 t2->to, t2->atom);
1811 if (ret == 0)
1812 return(0);
1813 }
1814 }
1815 if (ret == 0)
1816 break;
1817 }
1818 if (ret == 0)
1819 break;
1820 }
1821 ctxt->determinist = ret;
1822 return(ret);
1823}
1824
Daniel Veillard4255d502002-04-16 15:50:10 +00001825/************************************************************************
1826 * *
1827 * Routines to check input against transition atoms *
1828 * *
1829 ************************************************************************/
1830
1831static int
1832xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1833 int start, int end, const xmlChar *blockName) {
1834 int ret = 0;
1835
1836 switch (type) {
1837 case XML_REGEXP_STRING:
1838 case XML_REGEXP_SUBREG:
1839 case XML_REGEXP_RANGES:
1840 case XML_REGEXP_EPSILON:
1841 return(-1);
1842 case XML_REGEXP_ANYCHAR:
1843 ret = ((codepoint != '\n') && (codepoint != '\r'));
1844 break;
1845 case XML_REGEXP_CHARVAL:
1846 ret = ((codepoint >= start) && (codepoint <= end));
1847 break;
1848 case XML_REGEXP_NOTSPACE:
1849 neg = !neg;
1850 case XML_REGEXP_ANYSPACE:
1851 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1852 (codepoint == '\t') || (codepoint == ' '));
1853 break;
1854 case XML_REGEXP_NOTINITNAME:
1855 neg = !neg;
1856 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00001857 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001858 (codepoint == '_') || (codepoint == ':'));
1859 break;
1860 case XML_REGEXP_NOTNAMECHAR:
1861 neg = !neg;
1862 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00001863 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001864 (codepoint == '.') || (codepoint == '-') ||
1865 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00001866 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00001867 break;
1868 case XML_REGEXP_NOTDECIMAL:
1869 neg = !neg;
1870 case XML_REGEXP_DECIMAL:
1871 ret = xmlUCSIsCatNd(codepoint);
1872 break;
1873 case XML_REGEXP_REALCHAR:
1874 neg = !neg;
1875 case XML_REGEXP_NOTREALCHAR:
1876 ret = xmlUCSIsCatP(codepoint);
1877 if (ret == 0)
1878 ret = xmlUCSIsCatZ(codepoint);
1879 if (ret == 0)
1880 ret = xmlUCSIsCatC(codepoint);
1881 break;
1882 case XML_REGEXP_LETTER:
1883 ret = xmlUCSIsCatL(codepoint);
1884 break;
1885 case XML_REGEXP_LETTER_UPPERCASE:
1886 ret = xmlUCSIsCatLu(codepoint);
1887 break;
1888 case XML_REGEXP_LETTER_LOWERCASE:
1889 ret = xmlUCSIsCatLl(codepoint);
1890 break;
1891 case XML_REGEXP_LETTER_TITLECASE:
1892 ret = xmlUCSIsCatLt(codepoint);
1893 break;
1894 case XML_REGEXP_LETTER_MODIFIER:
1895 ret = xmlUCSIsCatLm(codepoint);
1896 break;
1897 case XML_REGEXP_LETTER_OTHERS:
1898 ret = xmlUCSIsCatLo(codepoint);
1899 break;
1900 case XML_REGEXP_MARK:
1901 ret = xmlUCSIsCatM(codepoint);
1902 break;
1903 case XML_REGEXP_MARK_NONSPACING:
1904 ret = xmlUCSIsCatMn(codepoint);
1905 break;
1906 case XML_REGEXP_MARK_SPACECOMBINING:
1907 ret = xmlUCSIsCatMc(codepoint);
1908 break;
1909 case XML_REGEXP_MARK_ENCLOSING:
1910 ret = xmlUCSIsCatMe(codepoint);
1911 break;
1912 case XML_REGEXP_NUMBER:
1913 ret = xmlUCSIsCatN(codepoint);
1914 break;
1915 case XML_REGEXP_NUMBER_DECIMAL:
1916 ret = xmlUCSIsCatNd(codepoint);
1917 break;
1918 case XML_REGEXP_NUMBER_LETTER:
1919 ret = xmlUCSIsCatNl(codepoint);
1920 break;
1921 case XML_REGEXP_NUMBER_OTHERS:
1922 ret = xmlUCSIsCatNo(codepoint);
1923 break;
1924 case XML_REGEXP_PUNCT:
1925 ret = xmlUCSIsCatP(codepoint);
1926 break;
1927 case XML_REGEXP_PUNCT_CONNECTOR:
1928 ret = xmlUCSIsCatPc(codepoint);
1929 break;
1930 case XML_REGEXP_PUNCT_DASH:
1931 ret = xmlUCSIsCatPd(codepoint);
1932 break;
1933 case XML_REGEXP_PUNCT_OPEN:
1934 ret = xmlUCSIsCatPs(codepoint);
1935 break;
1936 case XML_REGEXP_PUNCT_CLOSE:
1937 ret = xmlUCSIsCatPe(codepoint);
1938 break;
1939 case XML_REGEXP_PUNCT_INITQUOTE:
1940 ret = xmlUCSIsCatPi(codepoint);
1941 break;
1942 case XML_REGEXP_PUNCT_FINQUOTE:
1943 ret = xmlUCSIsCatPf(codepoint);
1944 break;
1945 case XML_REGEXP_PUNCT_OTHERS:
1946 ret = xmlUCSIsCatPo(codepoint);
1947 break;
1948 case XML_REGEXP_SEPAR:
1949 ret = xmlUCSIsCatZ(codepoint);
1950 break;
1951 case XML_REGEXP_SEPAR_SPACE:
1952 ret = xmlUCSIsCatZs(codepoint);
1953 break;
1954 case XML_REGEXP_SEPAR_LINE:
1955 ret = xmlUCSIsCatZl(codepoint);
1956 break;
1957 case XML_REGEXP_SEPAR_PARA:
1958 ret = xmlUCSIsCatZp(codepoint);
1959 break;
1960 case XML_REGEXP_SYMBOL:
1961 ret = xmlUCSIsCatS(codepoint);
1962 break;
1963 case XML_REGEXP_SYMBOL_MATH:
1964 ret = xmlUCSIsCatSm(codepoint);
1965 break;
1966 case XML_REGEXP_SYMBOL_CURRENCY:
1967 ret = xmlUCSIsCatSc(codepoint);
1968 break;
1969 case XML_REGEXP_SYMBOL_MODIFIER:
1970 ret = xmlUCSIsCatSk(codepoint);
1971 break;
1972 case XML_REGEXP_SYMBOL_OTHERS:
1973 ret = xmlUCSIsCatSo(codepoint);
1974 break;
1975 case XML_REGEXP_OTHER:
1976 ret = xmlUCSIsCatC(codepoint);
1977 break;
1978 case XML_REGEXP_OTHER_CONTROL:
1979 ret = xmlUCSIsCatCc(codepoint);
1980 break;
1981 case XML_REGEXP_OTHER_FORMAT:
1982 ret = xmlUCSIsCatCf(codepoint);
1983 break;
1984 case XML_REGEXP_OTHER_PRIVATE:
1985 ret = xmlUCSIsCatCo(codepoint);
1986 break;
1987 case XML_REGEXP_OTHER_NA:
1988 /* ret = xmlUCSIsCatCn(codepoint); */
1989 /* Seems it doesn't exist anymore in recent Unicode releases */
1990 ret = 0;
1991 break;
1992 case XML_REGEXP_BLOCK_NAME:
1993 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
1994 break;
1995 }
1996 if (neg)
1997 return(!ret);
1998 return(ret);
1999}
2000
2001static int
2002xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2003 int i, ret = 0;
2004 xmlRegRangePtr range;
2005
William M. Brack871611b2003-10-18 04:53:14 +00002006 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002007 return(-1);
2008
2009 switch (atom->type) {
2010 case XML_REGEXP_SUBREG:
2011 case XML_REGEXP_EPSILON:
2012 return(-1);
2013 case XML_REGEXP_CHARVAL:
2014 return(codepoint == atom->codepoint);
2015 case XML_REGEXP_RANGES: {
2016 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002017
Daniel Veillard4255d502002-04-16 15:50:10 +00002018 for (i = 0;i < atom->nbRanges;i++) {
2019 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002020 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002021 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2022 0, range->start, range->end,
2023 range->blockName);
2024 if (ret != 0)
2025 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002026 } else if (range->neg) {
2027 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2028 0, range->start, range->end,
2029 range->blockName);
2030 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002031 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002032 else
2033 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002034 } else {
2035 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2036 0, range->start, range->end,
2037 range->blockName);
2038 if (ret != 0)
2039 accept = 1; /* might still be excluded */
2040 }
2041 }
2042 return(accept);
2043 }
2044 case XML_REGEXP_STRING:
2045 printf("TODO: XML_REGEXP_STRING\n");
2046 return(-1);
2047 case XML_REGEXP_ANYCHAR:
2048 case XML_REGEXP_ANYSPACE:
2049 case XML_REGEXP_NOTSPACE:
2050 case XML_REGEXP_INITNAME:
2051 case XML_REGEXP_NOTINITNAME:
2052 case XML_REGEXP_NAMECHAR:
2053 case XML_REGEXP_NOTNAMECHAR:
2054 case XML_REGEXP_DECIMAL:
2055 case XML_REGEXP_NOTDECIMAL:
2056 case XML_REGEXP_REALCHAR:
2057 case XML_REGEXP_NOTREALCHAR:
2058 case XML_REGEXP_LETTER:
2059 case XML_REGEXP_LETTER_UPPERCASE:
2060 case XML_REGEXP_LETTER_LOWERCASE:
2061 case XML_REGEXP_LETTER_TITLECASE:
2062 case XML_REGEXP_LETTER_MODIFIER:
2063 case XML_REGEXP_LETTER_OTHERS:
2064 case XML_REGEXP_MARK:
2065 case XML_REGEXP_MARK_NONSPACING:
2066 case XML_REGEXP_MARK_SPACECOMBINING:
2067 case XML_REGEXP_MARK_ENCLOSING:
2068 case XML_REGEXP_NUMBER:
2069 case XML_REGEXP_NUMBER_DECIMAL:
2070 case XML_REGEXP_NUMBER_LETTER:
2071 case XML_REGEXP_NUMBER_OTHERS:
2072 case XML_REGEXP_PUNCT:
2073 case XML_REGEXP_PUNCT_CONNECTOR:
2074 case XML_REGEXP_PUNCT_DASH:
2075 case XML_REGEXP_PUNCT_OPEN:
2076 case XML_REGEXP_PUNCT_CLOSE:
2077 case XML_REGEXP_PUNCT_INITQUOTE:
2078 case XML_REGEXP_PUNCT_FINQUOTE:
2079 case XML_REGEXP_PUNCT_OTHERS:
2080 case XML_REGEXP_SEPAR:
2081 case XML_REGEXP_SEPAR_SPACE:
2082 case XML_REGEXP_SEPAR_LINE:
2083 case XML_REGEXP_SEPAR_PARA:
2084 case XML_REGEXP_SYMBOL:
2085 case XML_REGEXP_SYMBOL_MATH:
2086 case XML_REGEXP_SYMBOL_CURRENCY:
2087 case XML_REGEXP_SYMBOL_MODIFIER:
2088 case XML_REGEXP_SYMBOL_OTHERS:
2089 case XML_REGEXP_OTHER:
2090 case XML_REGEXP_OTHER_CONTROL:
2091 case XML_REGEXP_OTHER_FORMAT:
2092 case XML_REGEXP_OTHER_PRIVATE:
2093 case XML_REGEXP_OTHER_NA:
2094 case XML_REGEXP_BLOCK_NAME:
2095 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2096 (const xmlChar *)atom->valuep);
2097 if (atom->neg)
2098 ret = !ret;
2099 break;
2100 }
2101 return(ret);
2102}
2103
2104/************************************************************************
2105 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002106 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002107 * *
2108 ************************************************************************/
2109
2110#ifdef DEBUG_REGEXP_EXEC
2111static void
2112xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2113 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2114 if (exec->inputStack != NULL) {
2115 int i;
2116 printf(": ");
2117 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2118 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2119 } else {
2120 printf(": %s", &(exec->inputString[exec->index]));
2121 }
2122 printf("\n");
2123}
2124#endif
2125
2126static void
2127xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2128#ifdef DEBUG_REGEXP_EXEC
2129 printf("saving ");
2130 exec->transno++;
2131 xmlFARegDebugExec(exec);
2132 exec->transno--;
2133#endif
2134
2135 if (exec->maxRollbacks == 0) {
2136 exec->maxRollbacks = 4;
2137 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2138 sizeof(xmlRegExecRollback));
2139 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002140 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002141 exec->maxRollbacks = 0;
2142 return;
2143 }
2144 memset(exec->rollbacks, 0,
2145 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2146 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2147 xmlRegExecRollback *tmp;
2148 int len = exec->maxRollbacks;
2149
2150 exec->maxRollbacks *= 2;
2151 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2152 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2153 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002154 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002155 exec->maxRollbacks /= 2;
2156 return;
2157 }
2158 exec->rollbacks = tmp;
2159 tmp = &exec->rollbacks[len];
2160 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2161 }
2162 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2163 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2164 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2165 if (exec->comp->nbCounters > 0) {
2166 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2167 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2168 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2169 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002170 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002171 exec->status = -5;
2172 return;
2173 }
2174 }
2175 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2176 exec->comp->nbCounters * sizeof(int));
2177 }
2178 exec->nbRollbacks++;
2179}
2180
2181static void
2182xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2183 if (exec->nbRollbacks <= 0) {
2184 exec->status = -1;
2185#ifdef DEBUG_REGEXP_EXEC
2186 printf("rollback failed on empty stack\n");
2187#endif
2188 return;
2189 }
2190 exec->nbRollbacks--;
2191 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2192 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2193 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2194 if (exec->comp->nbCounters > 0) {
2195 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2196 fprintf(stderr, "exec save: allocation failed");
2197 exec->status = -6;
2198 return;
2199 }
2200 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2201 exec->comp->nbCounters * sizeof(int));
2202 }
2203
2204#ifdef DEBUG_REGEXP_EXEC
2205 printf("restored ");
2206 xmlFARegDebugExec(exec);
2207#endif
2208}
2209
2210/************************************************************************
2211 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002212 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002213 * *
2214 ************************************************************************/
2215
2216static int
2217xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2218 xmlRegExecCtxt execval;
2219 xmlRegExecCtxtPtr exec = &execval;
2220 int ret, codepoint, len;
2221
2222 exec->inputString = content;
2223 exec->index = 0;
2224 exec->determinist = 1;
2225 exec->maxRollbacks = 0;
2226 exec->nbRollbacks = 0;
2227 exec->rollbacks = NULL;
2228 exec->status = 0;
2229 exec->comp = comp;
2230 exec->state = comp->states[0];
2231 exec->transno = 0;
2232 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002233 exec->inputStack = NULL;
2234 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002235 if (comp->nbCounters > 0) {
2236 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002237 if (exec->counts == NULL) {
2238 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002239 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002240 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002241 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2242 } else
2243 exec->counts = NULL;
2244 while ((exec->status == 0) &&
2245 ((exec->inputString[exec->index] != 0) ||
2246 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2247 xmlRegTransPtr trans;
2248 xmlRegAtomPtr atom;
2249
2250 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002251 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002252 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002253 * on counters, in that case don't break too early. Additionally,
2254 * if we are working on a range like "AB{0,2}", where B is not present,
2255 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002256 */
William M. Brack0e00b282004-04-26 15:40:47 +00002257 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002258 /*
2259 * if there is a transition, we must check if
2260 * atom allows minOccurs of 0
2261 */
2262 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002263 trans = &exec->state->trans[exec->transno];
2264 if (trans->to >=0) {
2265 atom = trans->atom;
2266 if (!((atom->min == 0) && (atom->max > 0)))
2267 goto rollback;
2268 }
2269 } else
2270 goto rollback;
2271 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002272
2273 exec->transcount = 0;
2274 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2275 trans = &exec->state->trans[exec->transno];
2276 if (trans->to < 0)
2277 continue;
2278 atom = trans->atom;
2279 ret = 0;
2280 if (trans->count >= 0) {
2281 int count;
2282 xmlRegCounterPtr counter;
2283
2284 /*
2285 * A counted transition.
2286 */
2287
2288 count = exec->counts[trans->count];
2289 counter = &exec->comp->counters[trans->count];
2290#ifdef DEBUG_REGEXP_EXEC
2291 printf("testing count %d: val %d, min %d, max %d\n",
2292 trans->count, count, counter->min, counter->max);
2293#endif
2294 ret = ((count >= counter->min) && (count <= counter->max));
2295 } else if (atom == NULL) {
2296 fprintf(stderr, "epsilon transition left at runtime\n");
2297 exec->status = -2;
2298 break;
2299 } else if (exec->inputString[exec->index] != 0) {
2300 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2301 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002302 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002303 xmlRegStatePtr to = comp->states[trans->to];
2304
2305 /*
2306 * this is a multiple input sequence
2307 */
2308 if (exec->state->nbTrans > exec->transno + 1) {
2309 xmlFARegExecSave(exec);
2310 }
2311 exec->transcount = 1;
2312 do {
2313 /*
2314 * Try to progress as much as possible on the input
2315 */
2316 if (exec->transcount == atom->max) {
2317 break;
2318 }
2319 exec->index += len;
2320 /*
2321 * End of input: stop here
2322 */
2323 if (exec->inputString[exec->index] == 0) {
2324 exec->index -= len;
2325 break;
2326 }
2327 if (exec->transcount >= atom->min) {
2328 int transno = exec->transno;
2329 xmlRegStatePtr state = exec->state;
2330
2331 /*
2332 * The transition is acceptable save it
2333 */
2334 exec->transno = -1; /* trick */
2335 exec->state = to;
2336 xmlFARegExecSave(exec);
2337 exec->transno = transno;
2338 exec->state = state;
2339 }
2340 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2341 len);
2342 ret = xmlRegCheckCharacter(atom, codepoint);
2343 exec->transcount++;
2344 } while (ret == 1);
2345 if (exec->transcount < atom->min)
2346 ret = 0;
2347
2348 /*
2349 * If the last check failed but one transition was found
2350 * possible, rollback
2351 */
2352 if (ret < 0)
2353 ret = 0;
2354 if (ret == 0) {
2355 goto rollback;
2356 }
William M. Brack0e00b282004-04-26 15:40:47 +00002357 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2358 /*
2359 * we don't match on the codepoint, but minOccurs of 0
2360 * says that's ok. Setting len to 0 inhibits stepping
2361 * over the codepoint.
2362 */
2363 exec->transcount = 1;
2364 len = 0;
2365 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002366 }
William M. Brack0e00b282004-04-26 15:40:47 +00002367 } else if ((atom->min == 0) && (atom->max > 0)) {
2368 /* another spot to match when minOccurs is 0 */
2369 exec->transcount = 1;
2370 len = 0;
2371 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002372 }
2373 if (ret == 1) {
2374 if (exec->state->nbTrans > exec->transno + 1) {
2375 xmlFARegExecSave(exec);
2376 }
2377 if (trans->counter >= 0) {
2378#ifdef DEBUG_REGEXP_EXEC
2379 printf("Increasing count %d\n", trans->counter);
2380#endif
2381 exec->counts[trans->counter]++;
2382 }
2383#ifdef DEBUG_REGEXP_EXEC
2384 printf("entering state %d\n", trans->to);
2385#endif
2386 exec->state = comp->states[trans->to];
2387 exec->transno = 0;
2388 if (trans->atom != NULL) {
2389 exec->index += len;
2390 }
2391 goto progress;
2392 } else if (ret < 0) {
2393 exec->status = -4;
2394 break;
2395 }
2396 }
2397 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2398rollback:
2399 /*
2400 * Failed to find a way out
2401 */
2402 exec->determinist = 0;
2403 xmlFARegExecRollBack(exec);
2404 }
2405progress:
2406 continue;
2407 }
2408 if (exec->rollbacks != NULL) {
2409 if (exec->counts != NULL) {
2410 int i;
2411
2412 for (i = 0;i < exec->maxRollbacks;i++)
2413 if (exec->rollbacks[i].counts != NULL)
2414 xmlFree(exec->rollbacks[i].counts);
2415 }
2416 xmlFree(exec->rollbacks);
2417 }
2418 if (exec->counts != NULL)
2419 xmlFree(exec->counts);
2420 if (exec->status == 0)
2421 return(1);
2422 if (exec->status == -1)
2423 return(0);
2424 return(exec->status);
2425}
2426
2427/************************************************************************
2428 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002429 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002430 * *
2431 ************************************************************************/
2432
2433/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002434 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002435 * @comp: a precompiled regular expression
2436 * @callback: a callback function used for handling progresses in the
2437 * automata matching phase
2438 * @data: the context data associated to the callback in this context
2439 *
2440 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002441 *
2442 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002443 */
2444xmlRegExecCtxtPtr
2445xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2446 xmlRegExecCtxtPtr exec;
2447
2448 if (comp == NULL)
2449 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002450 if ((comp->compact == NULL) && (comp->states == NULL))
2451 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002452 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2453 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002454 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002455 return(NULL);
2456 }
2457 memset(exec, 0, sizeof(xmlRegExecCtxt));
2458 exec->inputString = NULL;
2459 exec->index = 0;
2460 exec->determinist = 1;
2461 exec->maxRollbacks = 0;
2462 exec->nbRollbacks = 0;
2463 exec->rollbacks = NULL;
2464 exec->status = 0;
2465 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002466 if (comp->compact == NULL)
2467 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002468 exec->transno = 0;
2469 exec->transcount = 0;
2470 exec->callback = callback;
2471 exec->data = data;
2472 if (comp->nbCounters > 0) {
2473 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2474 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002475 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002476 xmlFree(exec);
2477 return(NULL);
2478 }
2479 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2480 } else
2481 exec->counts = NULL;
2482 exec->inputStackMax = 0;
2483 exec->inputStackNr = 0;
2484 exec->inputStack = NULL;
2485 return(exec);
2486}
2487
2488/**
2489 * xmlRegFreeExecCtxt:
2490 * @exec: a regular expression evaulation context
2491 *
2492 * Free the structures associated to a regular expression evaulation context.
2493 */
2494void
2495xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2496 if (exec == NULL)
2497 return;
2498
2499 if (exec->rollbacks != NULL) {
2500 if (exec->counts != NULL) {
2501 int i;
2502
2503 for (i = 0;i < exec->maxRollbacks;i++)
2504 if (exec->rollbacks[i].counts != NULL)
2505 xmlFree(exec->rollbacks[i].counts);
2506 }
2507 xmlFree(exec->rollbacks);
2508 }
2509 if (exec->counts != NULL)
2510 xmlFree(exec->counts);
2511 if (exec->inputStack != NULL) {
2512 int i;
2513
Daniel Veillard32370232002-10-16 14:08:14 +00002514 for (i = 0;i < exec->inputStackNr;i++) {
2515 if (exec->inputStack[i].value != NULL)
2516 xmlFree(exec->inputStack[i].value);
2517 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002518 xmlFree(exec->inputStack);
2519 }
2520 xmlFree(exec);
2521}
2522
2523static void
2524xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2525 void *data) {
2526#ifdef DEBUG_PUSH
2527 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2528#endif
2529 if (exec->inputStackMax == 0) {
2530 exec->inputStackMax = 4;
2531 exec->inputStack = (xmlRegInputTokenPtr)
2532 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2533 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002534 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002535 exec->inputStackMax = 0;
2536 return;
2537 }
2538 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2539 xmlRegInputTokenPtr tmp;
2540
2541 exec->inputStackMax *= 2;
2542 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2543 exec->inputStackMax * sizeof(xmlRegInputToken));
2544 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002545 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002546 exec->inputStackMax /= 2;
2547 return;
2548 }
2549 exec->inputStack = tmp;
2550 }
2551 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2552 exec->inputStack[exec->inputStackNr].data = data;
2553 exec->inputStackNr++;
2554 exec->inputStack[exec->inputStackNr].value = NULL;
2555 exec->inputStack[exec->inputStackNr].data = NULL;
2556}
2557
2558
2559/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002560 * xmlRegCompactPushString:
2561 * @exec: a regexp execution context
2562 * @comp: the precompiled exec with a compact table
2563 * @value: a string token input
2564 * @data: data associated to the token to reuse in callbacks
2565 *
2566 * Push one input token in the execution context
2567 *
2568 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2569 * a negative value in case of error.
2570 */
2571static int
2572xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2573 xmlRegexpPtr comp,
2574 const xmlChar *value,
2575 void *data) {
2576 int state = exec->index;
2577 int i, target;
2578
2579 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2580 return(-1);
2581
2582 if (value == NULL) {
2583 /*
2584 * are we at a final state ?
2585 */
2586 if (comp->compact[state * (comp->nbstrings + 1)] ==
2587 XML_REGEXP_FINAL_STATE)
2588 return(1);
2589 return(0);
2590 }
2591
2592#ifdef DEBUG_PUSH
2593 printf("value pushed: %s\n", value);
2594#endif
2595
2596 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002597 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00002598 */
2599 for (i = 0;i < comp->nbstrings;i++) {
2600 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2601 if ((target > 0) && (target <= comp->nbstates)) {
2602 target--; /* to avoid 0 */
2603 if (xmlStrEqual(comp->stringMap[i], value)) {
2604 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002605 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2606 exec->callback(exec->data, value,
2607 comp->transdata[state * comp->nbstrings + i], data);
2608 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002609#ifdef DEBUG_PUSH
2610 printf("entering state %d\n", target);
2611#endif
2612 if (comp->compact[target * (comp->nbstrings + 1)] ==
2613 XML_REGEXP_FINAL_STATE)
2614 return(1);
2615 return(0);
2616 }
2617 }
2618 }
2619 /*
2620 * Failed to find an exit transition out from current state for the
2621 * current token
2622 */
2623#ifdef DEBUG_PUSH
2624 printf("failed to find a transition for %s on state %d\n", value, state);
2625#endif
2626 exec->status = -1;
2627 return(-1);
2628}
2629
2630/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002631 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002632 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002633 * @value: a string token input
2634 * @data: data associated to the token to reuse in callbacks
2635 *
2636 * Push one input token in the execution context
2637 *
2638 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2639 * a negative value in case of error.
2640 */
2641int
2642xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2643 void *data) {
2644 xmlRegTransPtr trans;
2645 xmlRegAtomPtr atom;
2646 int ret;
2647 int final = 0;
2648
2649 if (exec == NULL)
2650 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002651 if (exec->comp == NULL)
2652 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002653 if (exec->status != 0)
2654 return(exec->status);
2655
Daniel Veillard23e73572002-09-19 19:56:43 +00002656 if (exec->comp->compact != NULL)
2657 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2658
Daniel Veillard4255d502002-04-16 15:50:10 +00002659 if (value == NULL) {
2660 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2661 return(1);
2662 final = 1;
2663 }
2664
2665#ifdef DEBUG_PUSH
2666 printf("value pushed: %s\n", value);
2667#endif
2668 /*
2669 * If we have an active rollback stack push the new value there
2670 * and get back to where we were left
2671 */
2672 if ((value != NULL) && (exec->inputStackNr > 0)) {
2673 xmlFARegExecSaveInputString(exec, value, data);
2674 value = exec->inputStack[exec->index].value;
2675 data = exec->inputStack[exec->index].data;
2676#ifdef DEBUG_PUSH
2677 printf("value loaded: %s\n", value);
2678#endif
2679 }
2680
2681 while ((exec->status == 0) &&
2682 ((value != NULL) ||
2683 ((final == 1) &&
2684 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2685
2686 /*
2687 * End of input on non-terminal state, rollback, however we may
2688 * still have epsilon like transition for counted transitions
2689 * on counters, in that case don't break too early.
2690 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002691 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002692 goto rollback;
2693
2694 exec->transcount = 0;
2695 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2696 trans = &exec->state->trans[exec->transno];
2697 if (trans->to < 0)
2698 continue;
2699 atom = trans->atom;
2700 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002701 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2702 int i;
2703 int count;
2704 xmlRegTransPtr t;
2705 xmlRegCounterPtr counter;
2706
2707 ret = 0;
2708
2709#ifdef DEBUG_PUSH
2710 printf("testing all lax %d\n", trans->count);
2711#endif
2712 /*
2713 * Check all counted transitions from the current state
2714 */
2715 if ((value == NULL) && (final)) {
2716 ret = 1;
2717 } else if (value != NULL) {
2718 for (i = 0;i < exec->state->nbTrans;i++) {
2719 t = &exec->state->trans[i];
2720 if ((t->counter < 0) || (t == trans))
2721 continue;
2722 counter = &exec->comp->counters[t->counter];
2723 count = exec->counts[t->counter];
2724 if ((count < counter->max) &&
2725 (t->atom != NULL) &&
2726 (xmlStrEqual(value, t->atom->valuep))) {
2727 ret = 0;
2728 break;
2729 }
2730 if ((count >= counter->min) &&
2731 (count < counter->max) &&
2732 (xmlStrEqual(value, t->atom->valuep))) {
2733 ret = 1;
2734 break;
2735 }
2736 }
2737 }
2738 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002739 int i;
2740 int count;
2741 xmlRegTransPtr t;
2742 xmlRegCounterPtr counter;
2743
2744 ret = 1;
2745
2746#ifdef DEBUG_PUSH
2747 printf("testing all %d\n", trans->count);
2748#endif
2749 /*
2750 * Check all counted transitions from the current state
2751 */
2752 for (i = 0;i < exec->state->nbTrans;i++) {
2753 t = &exec->state->trans[i];
2754 if ((t->counter < 0) || (t == trans))
2755 continue;
2756 counter = &exec->comp->counters[t->counter];
2757 count = exec->counts[t->counter];
2758 if ((count < counter->min) || (count > counter->max)) {
2759 ret = 0;
2760 break;
2761 }
2762 }
2763 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002764 int count;
2765 xmlRegCounterPtr counter;
2766
2767 /*
2768 * A counted transition.
2769 */
2770
2771 count = exec->counts[trans->count];
2772 counter = &exec->comp->counters[trans->count];
2773#ifdef DEBUG_PUSH
2774 printf("testing count %d: val %d, min %d, max %d\n",
2775 trans->count, count, counter->min, counter->max);
2776#endif
2777 ret = ((count >= counter->min) && (count <= counter->max));
2778 } else if (atom == NULL) {
2779 fprintf(stderr, "epsilon transition left at runtime\n");
2780 exec->status = -2;
2781 break;
2782 } else if (value != NULL) {
2783 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002784 if ((ret == 1) && (trans->counter >= 0)) {
2785 xmlRegCounterPtr counter;
2786 int count;
2787
2788 count = exec->counts[trans->counter];
2789 counter = &exec->comp->counters[trans->counter];
2790 if (count >= counter->max)
2791 ret = 0;
2792 }
2793
Daniel Veillard4255d502002-04-16 15:50:10 +00002794 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2795 xmlRegStatePtr to = exec->comp->states[trans->to];
2796
2797 /*
2798 * this is a multiple input sequence
2799 */
2800 if (exec->state->nbTrans > exec->transno + 1) {
2801 if (exec->inputStackNr <= 0) {
2802 xmlFARegExecSaveInputString(exec, value, data);
2803 }
2804 xmlFARegExecSave(exec);
2805 }
2806 exec->transcount = 1;
2807 do {
2808 /*
2809 * Try to progress as much as possible on the input
2810 */
2811 if (exec->transcount == atom->max) {
2812 break;
2813 }
2814 exec->index++;
2815 value = exec->inputStack[exec->index].value;
2816 data = exec->inputStack[exec->index].data;
2817#ifdef DEBUG_PUSH
2818 printf("value loaded: %s\n", value);
2819#endif
2820
2821 /*
2822 * End of input: stop here
2823 */
2824 if (value == NULL) {
2825 exec->index --;
2826 break;
2827 }
2828 if (exec->transcount >= atom->min) {
2829 int transno = exec->transno;
2830 xmlRegStatePtr state = exec->state;
2831
2832 /*
2833 * The transition is acceptable save it
2834 */
2835 exec->transno = -1; /* trick */
2836 exec->state = to;
2837 if (exec->inputStackNr <= 0) {
2838 xmlFARegExecSaveInputString(exec, value, data);
2839 }
2840 xmlFARegExecSave(exec);
2841 exec->transno = transno;
2842 exec->state = state;
2843 }
2844 ret = xmlStrEqual(value, atom->valuep);
2845 exec->transcount++;
2846 } while (ret == 1);
2847 if (exec->transcount < atom->min)
2848 ret = 0;
2849
2850 /*
2851 * If the last check failed but one transition was found
2852 * possible, rollback
2853 */
2854 if (ret < 0)
2855 ret = 0;
2856 if (ret == 0) {
2857 goto rollback;
2858 }
2859 }
2860 }
2861 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00002862 if ((exec->callback != NULL) && (atom != NULL) &&
2863 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002864 exec->callback(exec->data, atom->valuep,
2865 atom->data, data);
2866 }
2867 if (exec->state->nbTrans > exec->transno + 1) {
2868 if (exec->inputStackNr <= 0) {
2869 xmlFARegExecSaveInputString(exec, value, data);
2870 }
2871 xmlFARegExecSave(exec);
2872 }
2873 if (trans->counter >= 0) {
2874#ifdef DEBUG_PUSH
2875 printf("Increasing count %d\n", trans->counter);
2876#endif
2877 exec->counts[trans->counter]++;
2878 }
2879#ifdef DEBUG_PUSH
2880 printf("entering state %d\n", trans->to);
2881#endif
2882 exec->state = exec->comp->states[trans->to];
2883 exec->transno = 0;
2884 if (trans->atom != NULL) {
2885 if (exec->inputStack != NULL) {
2886 exec->index++;
2887 if (exec->index < exec->inputStackNr) {
2888 value = exec->inputStack[exec->index].value;
2889 data = exec->inputStack[exec->index].data;
2890#ifdef DEBUG_PUSH
2891 printf("value loaded: %s\n", value);
2892#endif
2893 } else {
2894 value = NULL;
2895 data = NULL;
2896#ifdef DEBUG_PUSH
2897 printf("end of input\n");
2898#endif
2899 }
2900 } else {
2901 value = NULL;
2902 data = NULL;
2903#ifdef DEBUG_PUSH
2904 printf("end of input\n");
2905#endif
2906 }
2907 }
2908 goto progress;
2909 } else if (ret < 0) {
2910 exec->status = -4;
2911 break;
2912 }
2913 }
2914 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2915rollback:
2916 /*
2917 * Failed to find a way out
2918 */
2919 exec->determinist = 0;
2920 xmlFARegExecRollBack(exec);
2921 if (exec->status == 0) {
2922 value = exec->inputStack[exec->index].value;
2923 data = exec->inputStack[exec->index].data;
2924#ifdef DEBUG_PUSH
2925 printf("value loaded: %s\n", value);
2926#endif
2927 }
2928 }
2929progress:
2930 continue;
2931 }
2932 if (exec->status == 0) {
2933 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2934 }
2935 return(exec->status);
2936}
2937
Daniel Veillard52b48c72003-04-13 19:53:42 +00002938/**
2939 * xmlRegExecPushString2:
2940 * @exec: a regexp execution context or NULL to indicate the end
2941 * @value: the first string token input
2942 * @value2: the second string token input
2943 * @data: data associated to the token to reuse in callbacks
2944 *
2945 * Push one input token in the execution context
2946 *
2947 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2948 * a negative value in case of error.
2949 */
2950int
2951xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
2952 const xmlChar *value2, void *data) {
2953 xmlChar buf[150];
2954 int lenn, lenp, ret;
2955 xmlChar *str;
2956
2957 if (exec == NULL)
2958 return(-1);
2959 if (exec->comp == NULL)
2960 return(-1);
2961 if (exec->status != 0)
2962 return(exec->status);
2963
2964 if (value2 == NULL)
2965 return(xmlRegExecPushString(exec, value, data));
2966
2967 lenn = strlen((char *) value2);
2968 lenp = strlen((char *) value);
2969
2970 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002971 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002972 if (str == NULL) {
2973 exec->status = -1;
2974 return(-1);
2975 }
2976 } else {
2977 str = buf;
2978 }
2979 memcpy(&str[0], value, lenp);
2980 str[lenp] = '|';
2981 memcpy(&str[lenp + 1], value2, lenn);
2982 str[lenn + lenp + 1] = 0;
2983
2984 if (exec->comp->compact != NULL)
2985 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
2986 else
2987 ret = xmlRegExecPushString(exec, str, data);
2988
2989 if (str != buf)
2990 xmlFree(buf);
2991 return(ret);
2992}
2993
Daniel Veillard4255d502002-04-16 15:50:10 +00002994#if 0
2995static int
2996xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2997 xmlRegTransPtr trans;
2998 xmlRegAtomPtr atom;
2999 int ret;
3000 int codepoint, len;
3001
3002 if (exec == NULL)
3003 return(-1);
3004 if (exec->status != 0)
3005 return(exec->status);
3006
3007 while ((exec->status == 0) &&
3008 ((exec->inputString[exec->index] != 0) ||
3009 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3010
3011 /*
3012 * End of input on non-terminal state, rollback, however we may
3013 * still have epsilon like transition for counted transitions
3014 * on counters, in that case don't break too early.
3015 */
3016 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3017 goto rollback;
3018
3019 exec->transcount = 0;
3020 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3021 trans = &exec->state->trans[exec->transno];
3022 if (trans->to < 0)
3023 continue;
3024 atom = trans->atom;
3025 ret = 0;
3026 if (trans->count >= 0) {
3027 int count;
3028 xmlRegCounterPtr counter;
3029
3030 /*
3031 * A counted transition.
3032 */
3033
3034 count = exec->counts[trans->count];
3035 counter = &exec->comp->counters[trans->count];
3036#ifdef DEBUG_REGEXP_EXEC
3037 printf("testing count %d: val %d, min %d, max %d\n",
3038 trans->count, count, counter->min, counter->max);
3039#endif
3040 ret = ((count >= counter->min) && (count <= counter->max));
3041 } else if (atom == NULL) {
3042 fprintf(stderr, "epsilon transition left at runtime\n");
3043 exec->status = -2;
3044 break;
3045 } else if (exec->inputString[exec->index] != 0) {
3046 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3047 ret = xmlRegCheckCharacter(atom, codepoint);
3048 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3049 xmlRegStatePtr to = exec->comp->states[trans->to];
3050
3051 /*
3052 * this is a multiple input sequence
3053 */
3054 if (exec->state->nbTrans > exec->transno + 1) {
3055 xmlFARegExecSave(exec);
3056 }
3057 exec->transcount = 1;
3058 do {
3059 /*
3060 * Try to progress as much as possible on the input
3061 */
3062 if (exec->transcount == atom->max) {
3063 break;
3064 }
3065 exec->index += len;
3066 /*
3067 * End of input: stop here
3068 */
3069 if (exec->inputString[exec->index] == 0) {
3070 exec->index -= len;
3071 break;
3072 }
3073 if (exec->transcount >= atom->min) {
3074 int transno = exec->transno;
3075 xmlRegStatePtr state = exec->state;
3076
3077 /*
3078 * The transition is acceptable save it
3079 */
3080 exec->transno = -1; /* trick */
3081 exec->state = to;
3082 xmlFARegExecSave(exec);
3083 exec->transno = transno;
3084 exec->state = state;
3085 }
3086 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3087 len);
3088 ret = xmlRegCheckCharacter(atom, codepoint);
3089 exec->transcount++;
3090 } while (ret == 1);
3091 if (exec->transcount < atom->min)
3092 ret = 0;
3093
3094 /*
3095 * If the last check failed but one transition was found
3096 * possible, rollback
3097 */
3098 if (ret < 0)
3099 ret = 0;
3100 if (ret == 0) {
3101 goto rollback;
3102 }
3103 }
3104 }
3105 if (ret == 1) {
3106 if (exec->state->nbTrans > exec->transno + 1) {
3107 xmlFARegExecSave(exec);
3108 }
3109 if (trans->counter >= 0) {
3110#ifdef DEBUG_REGEXP_EXEC
3111 printf("Increasing count %d\n", trans->counter);
3112#endif
3113 exec->counts[trans->counter]++;
3114 }
3115#ifdef DEBUG_REGEXP_EXEC
3116 printf("entering state %d\n", trans->to);
3117#endif
3118 exec->state = exec->comp->states[trans->to];
3119 exec->transno = 0;
3120 if (trans->atom != NULL) {
3121 exec->index += len;
3122 }
3123 goto progress;
3124 } else if (ret < 0) {
3125 exec->status = -4;
3126 break;
3127 }
3128 }
3129 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3130rollback:
3131 /*
3132 * Failed to find a way out
3133 */
3134 exec->determinist = 0;
3135 xmlFARegExecRollBack(exec);
3136 }
3137progress:
3138 continue;
3139 }
3140}
3141#endif
3142/************************************************************************
3143 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003144 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00003145 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3146 * *
3147 ************************************************************************/
3148
3149/**
3150 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003151 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003152 *
3153 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3154 */
3155static int
3156xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3157 int cur;
3158 int len;
3159
3160 cur = CUR_SCHAR(ctxt->cur, len);
3161 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3162 (cur == '*') || (cur == '+') || (cur == '(') ||
3163 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3164 (cur == 0x5D) || (cur == 0))
3165 return(-1);
3166 return(cur);
3167}
3168
3169/**
3170 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003171 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003172 *
3173 * [27] charProp ::= IsCategory | IsBlock
3174 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3175 * Separators | Symbols | Others
3176 * [29] Letters ::= 'L' [ultmo]?
3177 * [30] Marks ::= 'M' [nce]?
3178 * [31] Numbers ::= 'N' [dlo]?
3179 * [32] Punctuation ::= 'P' [cdseifo]?
3180 * [33] Separators ::= 'Z' [slp]?
3181 * [34] Symbols ::= 'S' [mcko]?
3182 * [35] Others ::= 'C' [cfon]?
3183 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3184 */
3185static void
3186xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3187 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003188 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003189 xmlChar *blockName = NULL;
3190
3191 cur = CUR;
3192 if (cur == 'L') {
3193 NEXT;
3194 cur = CUR;
3195 if (cur == 'u') {
3196 NEXT;
3197 type = XML_REGEXP_LETTER_UPPERCASE;
3198 } else if (cur == 'l') {
3199 NEXT;
3200 type = XML_REGEXP_LETTER_LOWERCASE;
3201 } else if (cur == 't') {
3202 NEXT;
3203 type = XML_REGEXP_LETTER_TITLECASE;
3204 } else if (cur == 'm') {
3205 NEXT;
3206 type = XML_REGEXP_LETTER_MODIFIER;
3207 } else if (cur == 'o') {
3208 NEXT;
3209 type = XML_REGEXP_LETTER_OTHERS;
3210 } else {
3211 type = XML_REGEXP_LETTER;
3212 }
3213 } else if (cur == 'M') {
3214 NEXT;
3215 cur = CUR;
3216 if (cur == 'n') {
3217 NEXT;
3218 /* nonspacing */
3219 type = XML_REGEXP_MARK_NONSPACING;
3220 } else if (cur == 'c') {
3221 NEXT;
3222 /* spacing combining */
3223 type = XML_REGEXP_MARK_SPACECOMBINING;
3224 } else if (cur == 'e') {
3225 NEXT;
3226 /* enclosing */
3227 type = XML_REGEXP_MARK_ENCLOSING;
3228 } else {
3229 /* all marks */
3230 type = XML_REGEXP_MARK;
3231 }
3232 } else if (cur == 'N') {
3233 NEXT;
3234 cur = CUR;
3235 if (cur == 'd') {
3236 NEXT;
3237 /* digital */
3238 type = XML_REGEXP_NUMBER_DECIMAL;
3239 } else if (cur == 'l') {
3240 NEXT;
3241 /* letter */
3242 type = XML_REGEXP_NUMBER_LETTER;
3243 } else if (cur == 'o') {
3244 NEXT;
3245 /* other */
3246 type = XML_REGEXP_NUMBER_OTHERS;
3247 } else {
3248 /* all numbers */
3249 type = XML_REGEXP_NUMBER;
3250 }
3251 } else if (cur == 'P') {
3252 NEXT;
3253 cur = CUR;
3254 if (cur == 'c') {
3255 NEXT;
3256 /* connector */
3257 type = XML_REGEXP_PUNCT_CONNECTOR;
3258 } else if (cur == 'd') {
3259 NEXT;
3260 /* dash */
3261 type = XML_REGEXP_PUNCT_DASH;
3262 } else if (cur == 's') {
3263 NEXT;
3264 /* open */
3265 type = XML_REGEXP_PUNCT_OPEN;
3266 } else if (cur == 'e') {
3267 NEXT;
3268 /* close */
3269 type = XML_REGEXP_PUNCT_CLOSE;
3270 } else if (cur == 'i') {
3271 NEXT;
3272 /* initial quote */
3273 type = XML_REGEXP_PUNCT_INITQUOTE;
3274 } else if (cur == 'f') {
3275 NEXT;
3276 /* final quote */
3277 type = XML_REGEXP_PUNCT_FINQUOTE;
3278 } else if (cur == 'o') {
3279 NEXT;
3280 /* other */
3281 type = XML_REGEXP_PUNCT_OTHERS;
3282 } else {
3283 /* all punctuation */
3284 type = XML_REGEXP_PUNCT;
3285 }
3286 } else if (cur == 'Z') {
3287 NEXT;
3288 cur = CUR;
3289 if (cur == 's') {
3290 NEXT;
3291 /* space */
3292 type = XML_REGEXP_SEPAR_SPACE;
3293 } else if (cur == 'l') {
3294 NEXT;
3295 /* line */
3296 type = XML_REGEXP_SEPAR_LINE;
3297 } else if (cur == 'p') {
3298 NEXT;
3299 /* paragraph */
3300 type = XML_REGEXP_SEPAR_PARA;
3301 } else {
3302 /* all separators */
3303 type = XML_REGEXP_SEPAR;
3304 }
3305 } else if (cur == 'S') {
3306 NEXT;
3307 cur = CUR;
3308 if (cur == 'm') {
3309 NEXT;
3310 type = XML_REGEXP_SYMBOL_MATH;
3311 /* math */
3312 } else if (cur == 'c') {
3313 NEXT;
3314 type = XML_REGEXP_SYMBOL_CURRENCY;
3315 /* currency */
3316 } else if (cur == 'k') {
3317 NEXT;
3318 type = XML_REGEXP_SYMBOL_MODIFIER;
3319 /* modifiers */
3320 } else if (cur == 'o') {
3321 NEXT;
3322 type = XML_REGEXP_SYMBOL_OTHERS;
3323 /* other */
3324 } else {
3325 /* all symbols */
3326 type = XML_REGEXP_SYMBOL;
3327 }
3328 } else if (cur == 'C') {
3329 NEXT;
3330 cur = CUR;
3331 if (cur == 'c') {
3332 NEXT;
3333 /* control */
3334 type = XML_REGEXP_OTHER_CONTROL;
3335 } else if (cur == 'f') {
3336 NEXT;
3337 /* format */
3338 type = XML_REGEXP_OTHER_FORMAT;
3339 } else if (cur == 'o') {
3340 NEXT;
3341 /* private use */
3342 type = XML_REGEXP_OTHER_PRIVATE;
3343 } else if (cur == 'n') {
3344 NEXT;
3345 /* not assigned */
3346 type = XML_REGEXP_OTHER_NA;
3347 } else {
3348 /* all others */
3349 type = XML_REGEXP_OTHER;
3350 }
3351 } else if (cur == 'I') {
3352 const xmlChar *start;
3353 NEXT;
3354 cur = CUR;
3355 if (cur != 's') {
3356 ERROR("IsXXXX expected");
3357 return;
3358 }
3359 NEXT;
3360 start = ctxt->cur;
3361 cur = CUR;
3362 if (((cur >= 'a') && (cur <= 'z')) ||
3363 ((cur >= 'A') && (cur <= 'Z')) ||
3364 ((cur >= '0') && (cur <= '9')) ||
3365 (cur == 0x2D)) {
3366 NEXT;
3367 cur = CUR;
3368 while (((cur >= 'a') && (cur <= 'z')) ||
3369 ((cur >= 'A') && (cur <= 'Z')) ||
3370 ((cur >= '0') && (cur <= '9')) ||
3371 (cur == 0x2D)) {
3372 NEXT;
3373 cur = CUR;
3374 }
3375 }
3376 type = XML_REGEXP_BLOCK_NAME;
3377 blockName = xmlStrndup(start, ctxt->cur - start);
3378 } else {
3379 ERROR("Unknown char property");
3380 return;
3381 }
3382 if (ctxt->atom == NULL) {
3383 ctxt->atom = xmlRegNewAtom(ctxt, type);
3384 if (ctxt->atom != NULL)
3385 ctxt->atom->valuep = blockName;
3386 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3387 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3388 type, 0, 0, blockName);
3389 }
3390}
3391
3392/**
3393 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003394 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003395 *
3396 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3397 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3398 * [25] catEsc ::= '\p{' charProp '}'
3399 * [26] complEsc ::= '\P{' charProp '}'
3400 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3401 */
3402static void
3403xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3404 int cur;
3405
3406 if (CUR == '.') {
3407 if (ctxt->atom == NULL) {
3408 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3409 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3410 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3411 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3412 }
3413 NEXT;
3414 return;
3415 }
3416 if (CUR != '\\') {
3417 ERROR("Escaped sequence: expecting \\");
3418 return;
3419 }
3420 NEXT;
3421 cur = CUR;
3422 if (cur == 'p') {
3423 NEXT;
3424 if (CUR != '{') {
3425 ERROR("Expecting '{'");
3426 return;
3427 }
3428 NEXT;
3429 xmlFAParseCharProp(ctxt);
3430 if (CUR != '}') {
3431 ERROR("Expecting '}'");
3432 return;
3433 }
3434 NEXT;
3435 } else if (cur == 'P') {
3436 NEXT;
3437 if (CUR != '{') {
3438 ERROR("Expecting '{'");
3439 return;
3440 }
3441 NEXT;
3442 xmlFAParseCharProp(ctxt);
3443 ctxt->atom->neg = 1;
3444 if (CUR != '}') {
3445 ERROR("Expecting '}'");
3446 return;
3447 }
3448 NEXT;
3449 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3450 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3451 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3452 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3453 (cur == 0x5E)) {
3454 if (ctxt->atom == NULL) {
3455 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3456 if (ctxt->atom != NULL)
3457 ctxt->atom->codepoint = cur;
3458 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3459 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3460 XML_REGEXP_CHARVAL, cur, cur, NULL);
3461 }
3462 NEXT;
3463 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3464 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3465 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003466 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003467
3468 switch (cur) {
3469 case 's':
3470 type = XML_REGEXP_ANYSPACE;
3471 break;
3472 case 'S':
3473 type = XML_REGEXP_NOTSPACE;
3474 break;
3475 case 'i':
3476 type = XML_REGEXP_INITNAME;
3477 break;
3478 case 'I':
3479 type = XML_REGEXP_NOTINITNAME;
3480 break;
3481 case 'c':
3482 type = XML_REGEXP_NAMECHAR;
3483 break;
3484 case 'C':
3485 type = XML_REGEXP_NOTNAMECHAR;
3486 break;
3487 case 'd':
3488 type = XML_REGEXP_DECIMAL;
3489 break;
3490 case 'D':
3491 type = XML_REGEXP_NOTDECIMAL;
3492 break;
3493 case 'w':
3494 type = XML_REGEXP_REALCHAR;
3495 break;
3496 case 'W':
3497 type = XML_REGEXP_NOTREALCHAR;
3498 break;
3499 }
3500 NEXT;
3501 if (ctxt->atom == NULL) {
3502 ctxt->atom = xmlRegNewAtom(ctxt, type);
3503 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3504 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3505 type, 0, 0, NULL);
3506 }
3507 }
3508}
3509
3510/**
3511 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003512 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003513 *
3514 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3515 */
3516static int
3517xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3518 int ret = 0, cur;
3519
3520 if ((CUR != '&') || (NXT(1) != '#'))
3521 return(-1);
3522 NEXT;
3523 NEXT;
3524 cur = CUR;
3525 if (cur == 'x') {
3526 NEXT;
3527 cur = CUR;
3528 if (((cur >= '0') && (cur <= '9')) ||
3529 ((cur >= 'a') && (cur <= 'f')) ||
3530 ((cur >= 'A') && (cur <= 'F'))) {
3531 while (((cur >= '0') && (cur <= '9')) ||
3532 ((cur >= 'A') && (cur <= 'F'))) {
3533 if ((cur >= '0') && (cur <= '9'))
3534 ret = ret * 16 + cur - '0';
3535 else if ((cur >= 'a') && (cur <= 'f'))
3536 ret = ret * 16 + 10 + (cur - 'a');
3537 else
3538 ret = ret * 16 + 10 + (cur - 'A');
3539 NEXT;
3540 cur = CUR;
3541 }
3542 } else {
3543 ERROR("Char ref: expecting [0-9A-F]");
3544 return(-1);
3545 }
3546 } else {
3547 if ((cur >= '0') && (cur <= '9')) {
3548 while ((cur >= '0') && (cur <= '9')) {
3549 ret = ret * 10 + cur - '0';
3550 NEXT;
3551 cur = CUR;
3552 }
3553 } else {
3554 ERROR("Char ref: expecting [0-9]");
3555 return(-1);
3556 }
3557 }
3558 if (cur != ';') {
3559 ERROR("Char ref: expecting ';'");
3560 return(-1);
3561 } else {
3562 NEXT;
3563 }
3564 return(ret);
3565}
3566
3567/**
3568 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003569 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003570 *
3571 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3572 * [18] seRange ::= charOrEsc '-' charOrEsc
3573 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3574 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3575 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3576 */
3577static void
3578xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00003579 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00003580 int start = -1;
3581 int end = -1;
3582
3583 if ((CUR == '&') && (NXT(1) == '#')) {
3584 end = start = xmlFAParseCharRef(ctxt);
3585 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3586 XML_REGEXP_CHARVAL, start, end, NULL);
3587 return;
3588 }
3589 cur = CUR;
3590 if (cur == '\\') {
3591 NEXT;
3592 cur = CUR;
3593 switch (cur) {
3594 case 'n': start = 0xA; break;
3595 case 'r': start = 0xD; break;
3596 case 't': start = 0x9; break;
3597 case '\\': case '|': case '.': case '-': case '^': case '?':
3598 case '*': case '+': case '{': case '}': case '(': case ')':
3599 case '[': case ']':
3600 start = cur; break;
3601 default:
3602 ERROR("Invalid escape value");
3603 return;
3604 }
3605 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00003606 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003607 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003608 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003609 } else {
3610 ERROR("Expecting a char range");
3611 return;
3612 }
William M. Brackdc99df92003-12-27 01:54:25 +00003613 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003614 if (start == '-') {
3615 return;
3616 }
3617 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00003618 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003619 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3620 XML_REGEXP_CHARVAL, start, end, NULL);
3621 return;
3622 }
3623 NEXT;
3624 cur = CUR;
3625 if (cur == '\\') {
3626 NEXT;
3627 cur = CUR;
3628 switch (cur) {
3629 case 'n': end = 0xA; break;
3630 case 'r': end = 0xD; break;
3631 case 't': end = 0x9; break;
3632 case '\\': case '|': case '.': case '-': case '^': case '?':
3633 case '*': case '+': case '{': case '}': case '(': case ')':
3634 case '[': case ']':
3635 end = cur; break;
3636 default:
3637 ERROR("Invalid escape value");
3638 return;
3639 }
William M. Brackdc99df92003-12-27 01:54:25 +00003640 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003641 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003642 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003643 } else {
3644 ERROR("Expecting the end of a char range");
3645 return;
3646 }
William M. Brackdc99df92003-12-27 01:54:25 +00003647 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003648 /* TODO check that the values are acceptable character ranges for XML */
3649 if (end < start) {
3650 ERROR("End of range is before start of range");
3651 } else {
3652 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3653 XML_REGEXP_CHARVAL, start, end, NULL);
3654 }
3655 return;
3656}
3657
3658/**
3659 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003660 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003661 *
3662 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3663 */
3664static void
3665xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3666 do {
3667 if ((CUR == '\\') || (CUR == '.')) {
3668 xmlFAParseCharClassEsc(ctxt);
3669 } else {
3670 xmlFAParseCharRange(ctxt);
3671 }
3672 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3673 (ctxt->error == 0));
3674}
3675
3676/**
3677 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003678 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003679 *
3680 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3681 * [15] negCharGroup ::= '^' posCharGroup
3682 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3683 * [12] charClassExpr ::= '[' charGroup ']'
3684 */
3685static void
3686xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3687 int n = ctxt->neg;
3688 while ((CUR != ']') && (ctxt->error == 0)) {
3689 if (CUR == '^') {
3690 int neg = ctxt->neg;
3691
3692 NEXT;
3693 ctxt->neg = !ctxt->neg;
3694 xmlFAParsePosCharGroup(ctxt);
3695 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00003696 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003697 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003698 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00003699 NEXT; /* eat the '-' */
3700 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00003701 xmlFAParseCharGroup(ctxt);
3702 if (CUR == ']') {
3703 NEXT;
3704 } else {
3705 ERROR("charClassExpr: ']' expected");
3706 break;
3707 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003708 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00003709 break;
3710 } else if (CUR != ']') {
3711 xmlFAParsePosCharGroup(ctxt);
3712 }
3713 }
3714 ctxt->neg = n;
3715}
3716
3717/**
3718 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003719 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003720 *
3721 * [11] charClass ::= charClassEsc | charClassExpr
3722 * [12] charClassExpr ::= '[' charGroup ']'
3723 */
3724static void
3725xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3726 if (CUR == '[') {
3727 NEXT;
3728 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3729 if (ctxt->atom == NULL)
3730 return;
3731 xmlFAParseCharGroup(ctxt);
3732 if (CUR == ']') {
3733 NEXT;
3734 } else {
3735 ERROR("xmlFAParseCharClass: ']' expected");
3736 }
3737 } else {
3738 xmlFAParseCharClassEsc(ctxt);
3739 }
3740}
3741
3742/**
3743 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003744 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003745 *
3746 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003747 *
3748 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003749 */
3750static int
3751xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3752 int ret = 0;
3753 int ok = 0;
3754
3755 while ((CUR >= '0') && (CUR <= '9')) {
3756 ret = ret * 10 + (CUR - '0');
3757 ok = 1;
3758 NEXT;
3759 }
3760 if (ok != 1) {
3761 return(-1);
3762 }
3763 return(ret);
3764}
3765
3766/**
3767 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003768 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003769 *
3770 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3771 * [5] quantity ::= quantRange | quantMin | QuantExact
3772 * [6] quantRange ::= QuantExact ',' QuantExact
3773 * [7] quantMin ::= QuantExact ','
3774 * [8] QuantExact ::= [0-9]+
3775 */
3776static int
3777xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3778 int cur;
3779
3780 cur = CUR;
3781 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3782 if (ctxt->atom != NULL) {
3783 if (cur == '?')
3784 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3785 else if (cur == '*')
3786 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3787 else if (cur == '+')
3788 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3789 }
3790 NEXT;
3791 return(1);
3792 }
3793 if (cur == '{') {
3794 int min = 0, max = 0;
3795
3796 NEXT;
3797 cur = xmlFAParseQuantExact(ctxt);
3798 if (cur >= 0)
3799 min = cur;
3800 if (CUR == ',') {
3801 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00003802 if (CUR == '}')
3803 max = INT_MAX;
3804 else {
3805 cur = xmlFAParseQuantExact(ctxt);
3806 if (cur >= 0)
3807 max = cur;
3808 else {
3809 ERROR("Improper quantifier");
3810 }
3811 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003812 }
3813 if (CUR == '}') {
3814 NEXT;
3815 } else {
3816 ERROR("Unterminated quantifier");
3817 }
3818 if (max == 0)
3819 max = min;
3820 if (ctxt->atom != NULL) {
3821 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3822 ctxt->atom->min = min;
3823 ctxt->atom->max = max;
3824 }
3825 return(1);
3826 }
3827 return(0);
3828}
3829
3830/**
3831 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003832 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003833 *
3834 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3835 */
3836static int
3837xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3838 int codepoint, len;
3839
3840 codepoint = xmlFAIsChar(ctxt);
3841 if (codepoint > 0) {
3842 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3843 if (ctxt->atom == NULL)
3844 return(-1);
3845 codepoint = CUR_SCHAR(ctxt->cur, len);
3846 ctxt->atom->codepoint = codepoint;
3847 NEXTL(len);
3848 return(1);
3849 } else if (CUR == '|') {
3850 return(0);
3851 } else if (CUR == 0) {
3852 return(0);
3853 } else if (CUR == ')') {
3854 return(0);
3855 } else if (CUR == '(') {
3856 xmlRegStatePtr start, oldend;
3857
3858 NEXT;
3859 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3860 start = ctxt->state;
3861 oldend = ctxt->end;
3862 ctxt->end = NULL;
3863 ctxt->atom = NULL;
3864 xmlFAParseRegExp(ctxt, 0);
3865 if (CUR == ')') {
3866 NEXT;
3867 } else {
3868 ERROR("xmlFAParseAtom: expecting ')'");
3869 }
3870 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3871 if (ctxt->atom == NULL)
3872 return(-1);
3873 ctxt->atom->start = start;
3874 ctxt->atom->stop = ctxt->state;
3875 ctxt->end = oldend;
3876 return(1);
3877 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3878 xmlFAParseCharClass(ctxt);
3879 return(1);
3880 }
3881 return(0);
3882}
3883
3884/**
3885 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003886 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003887 *
3888 * [3] piece ::= atom quantifier?
3889 */
3890static int
3891xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3892 int ret;
3893
3894 ctxt->atom = NULL;
3895 ret = xmlFAParseAtom(ctxt);
3896 if (ret == 0)
3897 return(0);
3898 if (ctxt->atom == NULL) {
3899 ERROR("internal: no atom generated");
3900 }
3901 xmlFAParseQuantifier(ctxt);
3902 return(1);
3903}
3904
3905/**
3906 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003907 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003908 *
3909 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003910 8
Daniel Veillard4255d502002-04-16 15:50:10 +00003911 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003912static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003913xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003914 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00003915 int ret;
3916
3917 previous = ctxt->state;
3918 ret = xmlFAParsePiece(ctxt);
3919 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003920 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
3921 return(-1);
3922 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00003923 ctxt->atom = NULL;
3924 }
3925 while ((ret != 0) && (ctxt->error == 0)) {
3926 ret = xmlFAParsePiece(ctxt);
3927 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003928 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3929 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003930 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003931 previous = ctxt->state;
3932 ctxt->atom = NULL;
3933 }
3934 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003935 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00003936}
3937
3938/**
3939 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003940 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00003941 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003942 *
3943 * [1] regExp ::= branch ( '|' branch )*
3944 */
3945static void
3946xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003947 xmlRegStatePtr start, end, oldend, oldstart;
Daniel Veillard4255d502002-04-16 15:50:10 +00003948
3949 oldend = ctxt->end;
3950
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003951 oldstart = ctxt->state;
3952 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00003953 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003954 ctxt->end = NULL;
3955 xmlFAParseBranch(ctxt);
3956 if (top) {
3957#ifdef DEBUG_REGEXP_GRAPH
3958 printf("State %d is final\n", ctxt->state->no);
3959#endif
3960 ctxt->state->type = XML_REGEXP_FINAL_STATE;
3961 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003962 if (CUR != '|') {
3963 ctxt->end = ctxt->state;
3964 return;
3965 }
3966 end = ctxt->state;
3967 while ((CUR == '|') && (ctxt->error == 0)) {
3968 NEXT;
3969 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003970 ctxt->end = NULL;
3971 xmlFAParseBranch(ctxt);
3972 if (top) {
3973 ctxt->state->type = XML_REGEXP_FINAL_STATE;
3974#ifdef DEBUG_REGEXP_GRAPH
3975 printf("State %d is final\n", ctxt->state->no);
3976#endif
3977 } else {
3978 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
3979 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003980 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003981 if (!top) {
3982 ctxt->state = end;
3983 ctxt->end = end;
3984 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003985}
3986
3987/************************************************************************
3988 * *
3989 * The basic API *
3990 * *
3991 ************************************************************************/
3992
3993/**
3994 * xmlRegexpPrint:
3995 * @output: the file for the output debug
3996 * @regexp: the compiled regexp
3997 *
3998 * Print the content of the compiled regular expression
3999 */
4000void
4001xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4002 int i;
4003
4004 fprintf(output, " regexp: ");
4005 if (regexp == NULL) {
4006 fprintf(output, "NULL\n");
4007 return;
4008 }
4009 fprintf(output, "'%s' ", regexp->string);
4010 fprintf(output, "\n");
4011 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4012 for (i = 0;i < regexp->nbAtoms; i++) {
4013 fprintf(output, " %02d ", i);
4014 xmlRegPrintAtom(output, regexp->atoms[i]);
4015 }
4016 fprintf(output, "%d states:", regexp->nbStates);
4017 fprintf(output, "\n");
4018 for (i = 0;i < regexp->nbStates; i++) {
4019 xmlRegPrintState(output, regexp->states[i]);
4020 }
4021 fprintf(output, "%d counters:\n", regexp->nbCounters);
4022 for (i = 0;i < regexp->nbCounters; i++) {
4023 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4024 regexp->counters[i].max);
4025 }
4026}
4027
4028/**
4029 * xmlRegexpCompile:
4030 * @regexp: a regular expression string
4031 *
4032 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004033 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004034 * that regular expression
4035 *
4036 * Returns the compiled expression or NULL in case of error
4037 */
4038xmlRegexpPtr
4039xmlRegexpCompile(const xmlChar *regexp) {
4040 xmlRegexpPtr ret;
4041 xmlRegParserCtxtPtr ctxt;
4042
4043 ctxt = xmlRegNewParserCtxt(regexp);
4044 if (ctxt == NULL)
4045 return(NULL);
4046
4047 /* initialize the parser */
4048 ctxt->end = NULL;
4049 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4050 xmlRegStatePush(ctxt, ctxt->start);
4051
4052 /* parse the expression building an automata */
4053 xmlFAParseRegExp(ctxt, 1);
4054 if (CUR != 0) {
4055 ERROR("xmlFAParseRegExp: extra characters");
4056 }
4057 ctxt->end = ctxt->state;
4058 ctxt->start->type = XML_REGEXP_START_STATE;
4059 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4060
4061 /* remove the Epsilon except for counted transitions */
4062 xmlFAEliminateEpsilonTransitions(ctxt);
4063
4064
4065 if (ctxt->error != 0) {
4066 xmlRegFreeParserCtxt(ctxt);
4067 return(NULL);
4068 }
4069 ret = xmlRegEpxFromParse(ctxt);
4070 xmlRegFreeParserCtxt(ctxt);
4071 return(ret);
4072}
4073
4074/**
4075 * xmlRegexpExec:
4076 * @comp: the compiled regular expression
4077 * @content: the value to check against the regular expression
4078 *
William M. Brackddf71d62004-05-06 04:17:26 +00004079 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004080 *
William M. Brackddf71d62004-05-06 04:17:26 +00004081 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004082 */
4083int
4084xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4085 if ((comp == NULL) || (content == NULL))
4086 return(-1);
4087 return(xmlFARegExec(comp, content));
4088}
4089
4090/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004091 * xmlRegexpIsDeterminist:
4092 * @comp: the compiled regular expression
4093 *
4094 * Check if the regular expression is determinist
4095 *
William M. Brackddf71d62004-05-06 04:17:26 +00004096 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004097 */
4098int
4099xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4100 xmlAutomataPtr am;
4101 int ret;
4102
4103 if (comp == NULL)
4104 return(-1);
4105 if (comp->determinist != -1)
4106 return(comp->determinist);
4107
4108 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004109 if (am->states != NULL) {
4110 int i;
4111
4112 for (i = 0;i < am->nbStates;i++)
4113 xmlRegFreeState(am->states[i]);
4114 xmlFree(am->states);
4115 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004116 am->nbAtoms = comp->nbAtoms;
4117 am->atoms = comp->atoms;
4118 am->nbStates = comp->nbStates;
4119 am->states = comp->states;
4120 am->determinist = -1;
4121 ret = xmlFAComputesDeterminism(am);
4122 am->atoms = NULL;
4123 am->states = NULL;
4124 xmlFreeAutomata(am);
4125 return(ret);
4126}
4127
4128/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004129 * xmlRegFreeRegexp:
4130 * @regexp: the regexp
4131 *
4132 * Free a regexp
4133 */
4134void
4135xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4136 int i;
4137 if (regexp == NULL)
4138 return;
4139
4140 if (regexp->string != NULL)
4141 xmlFree(regexp->string);
4142 if (regexp->states != NULL) {
4143 for (i = 0;i < regexp->nbStates;i++)
4144 xmlRegFreeState(regexp->states[i]);
4145 xmlFree(regexp->states);
4146 }
4147 if (regexp->atoms != NULL) {
4148 for (i = 0;i < regexp->nbAtoms;i++)
4149 xmlRegFreeAtom(regexp->atoms[i]);
4150 xmlFree(regexp->atoms);
4151 }
4152 if (regexp->counters != NULL)
4153 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004154 if (regexp->compact != NULL)
4155 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004156 if (regexp->transdata != NULL)
4157 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004158 if (regexp->stringMap != NULL) {
4159 for (i = 0; i < regexp->nbstrings;i++)
4160 xmlFree(regexp->stringMap[i]);
4161 xmlFree(regexp->stringMap);
4162 }
4163
Daniel Veillard4255d502002-04-16 15:50:10 +00004164 xmlFree(regexp);
4165}
4166
4167#ifdef LIBXML_AUTOMATA_ENABLED
4168/************************************************************************
4169 * *
4170 * The Automata interface *
4171 * *
4172 ************************************************************************/
4173
4174/**
4175 * xmlNewAutomata:
4176 *
4177 * Create a new automata
4178 *
4179 * Returns the new object or NULL in case of failure
4180 */
4181xmlAutomataPtr
4182xmlNewAutomata(void) {
4183 xmlAutomataPtr ctxt;
4184
4185 ctxt = xmlRegNewParserCtxt(NULL);
4186 if (ctxt == NULL)
4187 return(NULL);
4188
4189 /* initialize the parser */
4190 ctxt->end = NULL;
4191 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004192 if (ctxt->start == NULL) {
4193 xmlFreeAutomata(ctxt);
4194 return(NULL);
4195 }
4196 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4197 xmlRegFreeState(ctxt->start);
4198 xmlFreeAutomata(ctxt);
4199 return(NULL);
4200 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004201
4202 return(ctxt);
4203}
4204
4205/**
4206 * xmlFreeAutomata:
4207 * @am: an automata
4208 *
4209 * Free an automata
4210 */
4211void
4212xmlFreeAutomata(xmlAutomataPtr am) {
4213 if (am == NULL)
4214 return;
4215 xmlRegFreeParserCtxt(am);
4216}
4217
4218/**
4219 * xmlAutomataGetInitState:
4220 * @am: an automata
4221 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004222 * Initial state lookup
4223 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004224 * Returns the initial state of the automata
4225 */
4226xmlAutomataStatePtr
4227xmlAutomataGetInitState(xmlAutomataPtr am) {
4228 if (am == NULL)
4229 return(NULL);
4230 return(am->start);
4231}
4232
4233/**
4234 * xmlAutomataSetFinalState:
4235 * @am: an automata
4236 * @state: a state in this automata
4237 *
4238 * Makes that state a final state
4239 *
4240 * Returns 0 or -1 in case of error
4241 */
4242int
4243xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4244 if ((am == NULL) || (state == NULL))
4245 return(-1);
4246 state->type = XML_REGEXP_FINAL_STATE;
4247 return(0);
4248}
4249
4250/**
4251 * xmlAutomataNewTransition:
4252 * @am: an automata
4253 * @from: the starting point of the transition
4254 * @to: the target point of the transition or NULL
4255 * @token: the input string associated to that transition
4256 * @data: data passed to the callback function if the transition is activated
4257 *
William M. Brackddf71d62004-05-06 04:17:26 +00004258 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004259 * and then adds a transition from the @from state to the target state
4260 * activated by the value of @token
4261 *
4262 * Returns the target state or NULL in case of error
4263 */
4264xmlAutomataStatePtr
4265xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4266 xmlAutomataStatePtr to, const xmlChar *token,
4267 void *data) {
4268 xmlRegAtomPtr atom;
4269
4270 if ((am == NULL) || (from == NULL) || (token == NULL))
4271 return(NULL);
4272 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004273 if (atom == NULL)
4274 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004275 atom->data = data;
4276 if (atom == NULL)
4277 return(NULL);
4278 atom->valuep = xmlStrdup(token);
4279
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004280 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4281 xmlRegFreeAtom(atom);
4282 return(NULL);
4283 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004284 if (to == NULL)
4285 return(am->state);
4286 return(to);
4287}
4288
4289/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004290 * xmlAutomataNewTransition2:
4291 * @am: an automata
4292 * @from: the starting point of the transition
4293 * @to: the target point of the transition or NULL
4294 * @token: the first input string associated to that transition
4295 * @token2: the second input string associated to that transition
4296 * @data: data passed to the callback function if the transition is activated
4297 *
William M. Brackddf71d62004-05-06 04:17:26 +00004298 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00004299 * and then adds a transition from the @from state to the target state
4300 * activated by the value of @token
4301 *
4302 * Returns the target state or NULL in case of error
4303 */
4304xmlAutomataStatePtr
4305xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4306 xmlAutomataStatePtr to, const xmlChar *token,
4307 const xmlChar *token2, void *data) {
4308 xmlRegAtomPtr atom;
4309
4310 if ((am == NULL) || (from == NULL) || (token == NULL))
4311 return(NULL);
4312 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4313 atom->data = data;
4314 if (atom == NULL)
4315 return(NULL);
4316 if ((token2 == NULL) || (*token2 == 0)) {
4317 atom->valuep = xmlStrdup(token);
4318 } else {
4319 int lenn, lenp;
4320 xmlChar *str;
4321
4322 lenn = strlen((char *) token2);
4323 lenp = strlen((char *) token);
4324
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004325 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004326 if (str == NULL) {
4327 xmlRegFreeAtom(atom);
4328 return(NULL);
4329 }
4330 memcpy(&str[0], token, lenp);
4331 str[lenp] = '|';
4332 memcpy(&str[lenp + 1], token2, lenn);
4333 str[lenn + lenp + 1] = 0;
4334
4335 atom->valuep = str;
4336 }
4337
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004338 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4339 xmlRegFreeAtom(atom);
4340 return(NULL);
4341 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004342 if (to == NULL)
4343 return(am->state);
4344 return(to);
4345}
4346
4347/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004348 * xmlAutomataNewCountTrans:
4349 * @am: an automata
4350 * @from: the starting point of the transition
4351 * @to: the target point of the transition or NULL
4352 * @token: the input string associated to that transition
4353 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004354 * @max: the maximum successive occurences of token
4355 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004356 *
William M. Brackddf71d62004-05-06 04:17:26 +00004357 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004358 * and then adds a transition from the @from state to the target state
4359 * activated by a succession of input of value @token and whose number
4360 * is between @min and @max
4361 *
4362 * Returns the target state or NULL in case of error
4363 */
4364xmlAutomataStatePtr
4365xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4366 xmlAutomataStatePtr to, const xmlChar *token,
4367 int min, int max, void *data) {
4368 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004369 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00004370
4371 if ((am == NULL) || (from == NULL) || (token == NULL))
4372 return(NULL);
4373 if (min < 0)
4374 return(NULL);
4375 if ((max < min) || (max < 1))
4376 return(NULL);
4377 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4378 if (atom == NULL)
4379 return(NULL);
4380 atom->valuep = xmlStrdup(token);
4381 atom->data = data;
4382 if (min == 0)
4383 atom->min = 1;
4384 else
4385 atom->min = min;
4386 atom->max = max;
4387
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004388 /*
4389 * associate a counter to the transition.
4390 */
4391 counter = xmlRegGetCounter(am);
4392 am->counters[counter].min = min;
4393 am->counters[counter].max = max;
4394
4395 /* xmlFAGenerateTransitions(am, from, to, atom); */
4396 if (to == NULL) {
4397 to = xmlRegNewState(am);
4398 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004399 }
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004400 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4401 xmlRegAtomPush(am, atom);
4402 am->state = to;
4403
Daniel Veillard4255d502002-04-16 15:50:10 +00004404 if (to == NULL)
4405 to = am->state;
4406 if (to == NULL)
4407 return(NULL);
4408 if (min == 0)
4409 xmlFAGenerateEpsilonTransition(am, from, to);
4410 return(to);
4411}
4412
4413/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004414 * xmlAutomataNewOnceTrans:
4415 * @am: an automata
4416 * @from: the starting point of the transition
4417 * @to: the target point of the transition or NULL
4418 * @token: the input string associated to that transition
4419 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004420 * @max: the maximum successive occurences of token
4421 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00004422 *
William M. Brackddf71d62004-05-06 04:17:26 +00004423 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00004424 * and then adds a transition from the @from state to the target state
4425 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00004426 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00004427 * once.
4428 *
4429 * Returns the target state or NULL in case of error
4430 */
4431xmlAutomataStatePtr
4432xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4433 xmlAutomataStatePtr to, const xmlChar *token,
4434 int min, int max, void *data) {
4435 xmlRegAtomPtr atom;
4436 int counter;
4437
4438 if ((am == NULL) || (from == NULL) || (token == NULL))
4439 return(NULL);
4440 if (min < 1)
4441 return(NULL);
4442 if ((max < min) || (max < 1))
4443 return(NULL);
4444 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4445 if (atom == NULL)
4446 return(NULL);
4447 atom->valuep = xmlStrdup(token);
4448 atom->data = data;
4449 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4450 if (min == 0)
4451 atom->min = 1;
4452 else
4453 atom->min = min;
4454 atom->max = max;
4455 /*
4456 * associate a counter to the transition.
4457 */
4458 counter = xmlRegGetCounter(am);
4459 am->counters[counter].min = 1;
4460 am->counters[counter].max = 1;
4461
4462 /* xmlFAGenerateTransitions(am, from, to, atom); */
4463 if (to == NULL) {
4464 to = xmlRegNewState(am);
4465 xmlRegStatePush(am, to);
4466 }
4467 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4468 xmlRegAtomPush(am, atom);
4469 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00004470 return(to);
4471}
4472
4473/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004474 * xmlAutomataNewState:
4475 * @am: an automata
4476 *
4477 * Create a new disconnected state in the automata
4478 *
4479 * Returns the new state or NULL in case of error
4480 */
4481xmlAutomataStatePtr
4482xmlAutomataNewState(xmlAutomataPtr am) {
4483 xmlAutomataStatePtr to;
4484
4485 if (am == NULL)
4486 return(NULL);
4487 to = xmlRegNewState(am);
4488 xmlRegStatePush(am, to);
4489 return(to);
4490}
4491
4492/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004493 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004494 * @am: an automata
4495 * @from: the starting point of the transition
4496 * @to: the target point of the transition or NULL
4497 *
William M. Brackddf71d62004-05-06 04:17:26 +00004498 * If @to is NULL, this creates first a new target state in the automata
4499 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00004500 * target state
4501 *
4502 * Returns the target state or NULL in case of error
4503 */
4504xmlAutomataStatePtr
4505xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4506 xmlAutomataStatePtr to) {
4507 if ((am == NULL) || (from == NULL))
4508 return(NULL);
4509 xmlFAGenerateEpsilonTransition(am, from, to);
4510 if (to == NULL)
4511 return(am->state);
4512 return(to);
4513}
4514
Daniel Veillardb509f152002-04-17 16:28:10 +00004515/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004516 * xmlAutomataNewAllTrans:
4517 * @am: an automata
4518 * @from: the starting point of the transition
4519 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004520 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004521 *
William M. Brackddf71d62004-05-06 04:17:26 +00004522 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00004523 * and then adds a an ALL transition from the @from state to the
4524 * target state. That transition is an epsilon transition allowed only when
4525 * all transitions from the @from node have been activated.
4526 *
4527 * Returns the target state or NULL in case of error
4528 */
4529xmlAutomataStatePtr
4530xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004531 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004532 if ((am == NULL) || (from == NULL))
4533 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004534 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004535 if (to == NULL)
4536 return(am->state);
4537 return(to);
4538}
4539
4540/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004541 * xmlAutomataNewCounter:
4542 * @am: an automata
4543 * @min: the minimal value on the counter
4544 * @max: the maximal value on the counter
4545 *
4546 * Create a new counter
4547 *
4548 * Returns the counter number or -1 in case of error
4549 */
4550int
4551xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4552 int ret;
4553
4554 if (am == NULL)
4555 return(-1);
4556
4557 ret = xmlRegGetCounter(am);
4558 if (ret < 0)
4559 return(-1);
4560 am->counters[ret].min = min;
4561 am->counters[ret].max = max;
4562 return(ret);
4563}
4564
4565/**
4566 * xmlAutomataNewCountedTrans:
4567 * @am: an automata
4568 * @from: the starting point of the transition
4569 * @to: the target point of the transition or NULL
4570 * @counter: the counter associated to that transition
4571 *
William M. Brackddf71d62004-05-06 04:17:26 +00004572 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00004573 * and then adds an epsilon transition from the @from state to the target state
4574 * which will increment the counter provided
4575 *
4576 * Returns the target state or NULL in case of error
4577 */
4578xmlAutomataStatePtr
4579xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4580 xmlAutomataStatePtr to, int counter) {
4581 if ((am == NULL) || (from == NULL) || (counter < 0))
4582 return(NULL);
4583 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4584 if (to == NULL)
4585 return(am->state);
4586 return(to);
4587}
4588
4589/**
4590 * xmlAutomataNewCounterTrans:
4591 * @am: an automata
4592 * @from: the starting point of the transition
4593 * @to: the target point of the transition or NULL
4594 * @counter: the counter associated to that transition
4595 *
William M. Brackddf71d62004-05-06 04:17:26 +00004596 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00004597 * and then adds an epsilon transition from the @from state to the target state
4598 * which will be allowed only if the counter is within the right range.
4599 *
4600 * Returns the target state or NULL in case of error
4601 */
4602xmlAutomataStatePtr
4603xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4604 xmlAutomataStatePtr to, int counter) {
4605 if ((am == NULL) || (from == NULL) || (counter < 0))
4606 return(NULL);
4607 xmlFAGenerateCountedTransition(am, from, to, counter);
4608 if (to == NULL)
4609 return(am->state);
4610 return(to);
4611}
Daniel Veillard4255d502002-04-16 15:50:10 +00004612
4613/**
4614 * xmlAutomataCompile:
4615 * @am: an automata
4616 *
4617 * Compile the automata into a Reg Exp ready for being executed.
4618 * The automata should be free after this point.
4619 *
4620 * Returns the compiled regexp or NULL in case of error
4621 */
4622xmlRegexpPtr
4623xmlAutomataCompile(xmlAutomataPtr am) {
4624 xmlRegexpPtr ret;
4625
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004626 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004627 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004628 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004629 ret = xmlRegEpxFromParse(am);
4630
4631 return(ret);
4632}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004633
4634/**
4635 * xmlAutomataIsDeterminist:
4636 * @am: an automata
4637 *
4638 * Checks if an automata is determinist.
4639 *
4640 * Returns 1 if true, 0 if not, and -1 in case of error
4641 */
4642int
4643xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4644 int ret;
4645
4646 if (am == NULL)
4647 return(-1);
4648
4649 ret = xmlFAComputesDeterminism(am);
4650 return(ret);
4651}
Daniel Veillard4255d502002-04-16 15:50:10 +00004652#endif /* LIBXML_AUTOMATA_ENABLED */
4653#endif /* LIBXML_REGEXP_ENABLED */