blob: 12ecd83f794d45f97e007484e12077862f5f2e68 [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) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001185
1186 int nrtrans;
1187
Daniel Veillard4255d502002-04-16 15:50:10 +00001188 if (state == NULL) {
1189 ERROR("add state: state is NULL");
1190 return;
1191 }
1192 if (target == NULL) {
1193 ERROR("add state: target is NULL");
1194 return;
1195 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001196 /*
1197 * Other routines follow the philosophy 'When in doubt, add a transition'
1198 * so we check here whether such a transition is already present and, if
1199 * so, silently ignore this request.
1200 */
1201
1202 for (nrtrans=0; nrtrans<state->nbTrans; nrtrans++) {
1203 if ((state->trans[nrtrans].atom == atom) &&
1204 (state->trans[nrtrans].to == target->no) &&
1205 (state->trans[nrtrans].counter == counter) &&
1206 (state->trans[nrtrans].count == count)) {
1207#ifdef DEBUG_REGEXP_GRAPH
1208 printf("Ignoring duplicate transition from %d to %d\n",
1209 state->no, target->no);
1210#endif
1211 return;
1212 }
1213 }
1214
Daniel Veillard4255d502002-04-16 15:50:10 +00001215 if (state->maxTrans == 0) {
1216 state->maxTrans = 4;
1217 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1218 sizeof(xmlRegTrans));
1219 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001220 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001221 state->maxTrans = 0;
1222 return;
1223 }
1224 } else if (state->nbTrans >= state->maxTrans) {
1225 xmlRegTrans *tmp;
1226 state->maxTrans *= 2;
1227 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1228 sizeof(xmlRegTrans));
1229 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001230 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001231 state->maxTrans /= 2;
1232 return;
1233 }
1234 state->trans = tmp;
1235 }
1236#ifdef DEBUG_REGEXP_GRAPH
1237 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001238 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001239 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001240 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001241 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001242 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001243 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001244 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001245 printf("epsilon transition\n");
1246 else if (atom != NULL)
1247 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001248#endif
1249
1250 state->trans[state->nbTrans].atom = atom;
1251 state->trans[state->nbTrans].to = target->no;
1252 state->trans[state->nbTrans].counter = counter;
1253 state->trans[state->nbTrans].count = count;
1254 state->nbTrans++;
1255}
1256
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001257static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001258xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001259 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001260 if (ctxt->maxStates == 0) {
1261 ctxt->maxStates = 4;
1262 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1263 sizeof(xmlRegStatePtr));
1264 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001265 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001266 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001267 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001268 }
1269 } else if (ctxt->nbStates >= ctxt->maxStates) {
1270 xmlRegStatePtr *tmp;
1271 ctxt->maxStates *= 2;
1272 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1273 sizeof(xmlRegStatePtr));
1274 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001275 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001276 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001277 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001278 }
1279 ctxt->states = tmp;
1280 }
1281 state->no = ctxt->nbStates;
1282 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001283 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001284}
1285
1286/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001287 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001288 * @ctxt: a regexp parser context
1289 * @from: the from state
1290 * @to: the target state or NULL for building a new one
1291 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001292 *
1293 */
1294static void
1295xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001296 xmlRegStatePtr from, xmlRegStatePtr to,
1297 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001298 if (to == NULL) {
1299 to = xmlRegNewState(ctxt);
1300 xmlRegStatePush(ctxt, to);
1301 ctxt->state = to;
1302 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001303 if (lax)
1304 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1305 else
1306 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001307}
1308
1309/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001310 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001311 * @ctxt: a regexp parser context
1312 * @from: the from state
1313 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001314 *
1315 */
1316static void
1317xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1318 xmlRegStatePtr from, xmlRegStatePtr to) {
1319 if (to == NULL) {
1320 to = xmlRegNewState(ctxt);
1321 xmlRegStatePush(ctxt, to);
1322 ctxt->state = to;
1323 }
1324 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1325}
1326
1327/**
1328 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001329 * @ctxt: a regexp parser context
1330 * @from: the from state
1331 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001332 * counter: the counter for that transition
1333 *
1334 */
1335static void
1336xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1337 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1338 if (to == NULL) {
1339 to = xmlRegNewState(ctxt);
1340 xmlRegStatePush(ctxt, to);
1341 ctxt->state = to;
1342 }
1343 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1344}
1345
1346/**
1347 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001348 * @ctxt: a regexp parser context
1349 * @from: the from state
1350 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001351 * counter: the counter for that transition
1352 *
1353 */
1354static void
1355xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1356 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1357 if (to == NULL) {
1358 to = xmlRegNewState(ctxt);
1359 xmlRegStatePush(ctxt, to);
1360 ctxt->state = to;
1361 }
1362 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1363}
1364
1365/**
1366 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001367 * @ctxt: a regexp parser context
1368 * @from: the from state
1369 * @to: the target state or NULL for building a new one
1370 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001371 *
William M. Brackddf71d62004-05-06 04:17:26 +00001372 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001373 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001374static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001375xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1376 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1377 if (atom == NULL) {
1378 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001379 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001380 }
1381 if (atom->type == XML_REGEXP_SUBREG) {
1382 /*
1383 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001384 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001385 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001386 if (xmlRegAtomPush(ctxt, atom) < 0) {
1387 return(-1);
1388 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001389 if ((to != NULL) && (atom->stop != to) &&
1390 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1391 /*
1392 * Generate an epsilon transition to link to the target
1393 */
1394 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1395 }
1396 switch (atom->quant) {
1397 case XML_REGEXP_QUANT_OPT:
1398 atom->quant = XML_REGEXP_QUANT_ONCE;
1399 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1400 break;
1401 case XML_REGEXP_QUANT_MULT:
1402 atom->quant = XML_REGEXP_QUANT_ONCE;
1403 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1404 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1405 break;
1406 case XML_REGEXP_QUANT_PLUS:
1407 atom->quant = XML_REGEXP_QUANT_ONCE;
1408 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1409 break;
1410 case XML_REGEXP_QUANT_RANGE: {
1411 int counter;
1412 xmlRegStatePtr newstate;
1413
1414 /*
1415 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001416 * 1/ if range has minOccurs == 0, create a new state
1417 * and create epsilon transitions from atom->start
1418 * to atom->stop, as well as atom->start to the new
1419 * state
1420 * 2/ register a new counter
1421 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001422 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001423 * 4/ create a new state
1424 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001425 * that state
1426 */
William M. Brackddf71d62004-05-06 04:17:26 +00001427 if (atom->min == 0) {
1428 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1429 atom->stop);
1430 newstate = xmlRegNewState(ctxt);
1431 xmlRegStatePush(ctxt, newstate);
1432 ctxt->state = newstate;
1433 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1434 newstate);
1435 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001436 counter = xmlRegGetCounter(ctxt);
1437 ctxt->counters[counter].min = atom->min - 1;
1438 ctxt->counters[counter].max = atom->max - 1;
1439 atom->min = 0;
1440 atom->max = 0;
1441 atom->quant = XML_REGEXP_QUANT_ONCE;
1442 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1443 atom->start, counter);
1444 if (to != NULL) {
1445 newstate = to;
1446 } else {
1447 newstate = xmlRegNewState(ctxt);
1448 xmlRegStatePush(ctxt, newstate);
1449 ctxt->state = newstate;
1450 }
1451 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1452 newstate, counter);
1453 }
1454 default:
1455 break;
1456 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001457 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001458 } else {
1459 if (to == NULL) {
1460 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001461 if (to != NULL)
1462 xmlRegStatePush(ctxt, to);
1463 else {
1464 return(-1);
1465 }
1466 }
1467 if (xmlRegAtomPush(ctxt, atom) < 0) {
1468 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001469 }
1470 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001471 ctxt->state = to;
1472 }
1473 switch (atom->quant) {
1474 case XML_REGEXP_QUANT_OPT:
1475 atom->quant = XML_REGEXP_QUANT_ONCE;
1476 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1477 break;
1478 case XML_REGEXP_QUANT_MULT:
1479 atom->quant = XML_REGEXP_QUANT_ONCE;
1480 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1481 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1482 break;
1483 case XML_REGEXP_QUANT_PLUS:
1484 atom->quant = XML_REGEXP_QUANT_ONCE;
1485 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1486 break;
1487 default:
1488 break;
1489 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001490 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001491}
1492
1493/**
1494 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001495 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001496 * @fromnr: the from state
1497 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001498 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001499 *
1500 */
1501static void
1502xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1503 int tonr, int counter) {
1504 int transnr;
1505 xmlRegStatePtr from;
1506 xmlRegStatePtr to;
1507
1508#ifdef DEBUG_REGEXP_GRAPH
1509 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1510#endif
1511 from = ctxt->states[fromnr];
1512 if (from == NULL)
1513 return;
1514 to = ctxt->states[tonr];
1515 if (to == NULL)
1516 return;
1517 if ((to->mark == XML_REGEXP_MARK_START) ||
1518 (to->mark == XML_REGEXP_MARK_VISITED))
1519 return;
1520
1521 to->mark = XML_REGEXP_MARK_VISITED;
1522 if (to->type == XML_REGEXP_FINAL_STATE) {
1523#ifdef DEBUG_REGEXP_GRAPH
1524 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1525#endif
1526 from->type = XML_REGEXP_FINAL_STATE;
1527 }
1528 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1529 if (to->trans[transnr].atom == NULL) {
1530 /*
1531 * Don't remove counted transitions
1532 * Don't loop either
1533 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001534 if (to->trans[transnr].to != fromnr) {
1535 if (to->trans[transnr].count >= 0) {
1536 int newto = to->trans[transnr].to;
1537
1538 xmlRegStateAddTrans(ctxt, from, NULL,
1539 ctxt->states[newto],
1540 -1, to->trans[transnr].count);
1541 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001542#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001543 printf("Found epsilon trans %d from %d to %d\n",
1544 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001545#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001546 if (to->trans[transnr].counter >= 0) {
1547 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1548 to->trans[transnr].to,
1549 to->trans[transnr].counter);
1550 } else {
1551 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1552 to->trans[transnr].to,
1553 counter);
1554 }
1555 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001556 }
1557 } else {
1558 int newto = to->trans[transnr].to;
1559
Daniel Veillardb509f152002-04-17 16:28:10 +00001560 if (to->trans[transnr].counter >= 0) {
1561 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1562 ctxt->states[newto],
1563 to->trans[transnr].counter, -1);
1564 } else {
1565 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1566 ctxt->states[newto], counter, -1);
1567 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001568 }
1569 }
1570 to->mark = XML_REGEXP_MARK_NORMAL;
1571}
1572
1573/**
1574 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001575 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001576 *
1577 */
1578static void
1579xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1580 int statenr, transnr;
1581 xmlRegStatePtr state;
1582
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001583 if (ctxt->states == NULL) return;
1584
1585
Daniel Veillard4255d502002-04-16 15:50:10 +00001586 /*
1587 * build the completed transitions bypassing the epsilons
1588 * Use a marking algorithm to avoid loops
1589 */
1590 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1591 state = ctxt->states[statenr];
1592 if (state == NULL)
1593 continue;
1594 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1595 if ((state->trans[transnr].atom == NULL) &&
1596 (state->trans[transnr].to >= 0)) {
1597 if (state->trans[transnr].to == statenr) {
1598 state->trans[transnr].to = -1;
1599#ifdef DEBUG_REGEXP_GRAPH
1600 printf("Removed loopback epsilon trans %d on %d\n",
1601 transnr, statenr);
1602#endif
1603 } else if (state->trans[transnr].count < 0) {
1604 int newto = state->trans[transnr].to;
1605
1606#ifdef DEBUG_REGEXP_GRAPH
1607 printf("Found epsilon trans %d from %d to %d\n",
1608 transnr, statenr, newto);
1609#endif
1610 state->mark = XML_REGEXP_MARK_START;
1611 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1612 newto, state->trans[transnr].counter);
1613 state->mark = XML_REGEXP_MARK_NORMAL;
1614#ifdef DEBUG_REGEXP_GRAPH
1615 } else {
1616 printf("Found counted transition %d on %d\n",
1617 transnr, statenr);
1618#endif
1619 }
1620 }
1621 }
1622 }
1623 /*
1624 * Eliminate the epsilon transitions
1625 */
1626 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1627 state = ctxt->states[statenr];
1628 if (state == NULL)
1629 continue;
1630 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1631 if ((state->trans[transnr].atom == NULL) &&
1632 (state->trans[transnr].count < 0) &&
1633 (state->trans[transnr].to >= 0)) {
1634 state->trans[transnr].to = -1;
1635 }
1636 }
1637 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001638
1639 /*
1640 * Use this pass to detect unreachable states too
1641 */
1642 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1643 state = ctxt->states[statenr];
1644 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001645 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001646 }
1647 state = ctxt->states[0];
1648 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001649 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001650 while (state != NULL) {
1651 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001652 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001653 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001654 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001655 */
1656 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1657 if ((state->trans[transnr].to >= 0) &&
1658 ((state->trans[transnr].atom != NULL) ||
1659 (state->trans[transnr].count >= 0))) {
1660 int newto = state->trans[transnr].to;
1661
1662 if (ctxt->states[newto] == NULL)
1663 continue;
William M. Brack779af002003-08-01 15:55:39 +00001664 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1665 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001666 target = ctxt->states[newto];
1667 }
1668 }
1669 }
1670 /*
1671 * find the next accessible state not explored
1672 */
1673 if (target == NULL) {
1674 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1675 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001676 if ((state != NULL) && (state->reached ==
1677 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001678 target = state;
1679 break;
1680 }
1681 }
1682 }
1683 state = target;
1684 }
1685 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1686 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001687 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001688#ifdef DEBUG_REGEXP_GRAPH
1689 printf("Removed unreachable state %d\n", statenr);
1690#endif
1691 xmlRegFreeState(state);
1692 ctxt->states[statenr] = NULL;
1693 }
1694 }
1695
Daniel Veillard4255d502002-04-16 15:50:10 +00001696}
1697
Daniel Veillarde19fc232002-04-22 16:01:24 +00001698/**
1699 * xmlFACompareAtoms:
1700 * @atom1: an atom
1701 * @atom2: an atom
1702 *
William M. Brackddf71d62004-05-06 04:17:26 +00001703 * Compares two atoms to check whether they are equivalents
Daniel Veillarde19fc232002-04-22 16:01:24 +00001704 *
1705 * Returns 1 if yes and 0 otherwise
1706 */
1707static int
1708xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1709 if (atom1 == atom2)
1710 return(1);
1711 if ((atom1 == NULL) || (atom2 == NULL))
1712 return(0);
1713
1714 if (atom1->type != atom2->type)
1715 return(0);
1716 switch (atom1->type) {
1717 case XML_REGEXP_STRING:
1718 return(xmlStrEqual((xmlChar *)atom1->valuep,
1719 (xmlChar *)atom2->valuep));
1720 case XML_REGEXP_EPSILON:
1721 return(1);
1722 case XML_REGEXP_CHARVAL:
1723 return(atom1->codepoint == atom2->codepoint);
1724 case XML_REGEXP_RANGES:
1725 TODO;
1726 return(0);
1727 default:
1728 break;
1729 }
1730 return(1);
1731}
1732
1733/**
1734 * xmlFARecurseDeterminism:
1735 * @ctxt: a regexp parser context
1736 *
1737 * Check whether the associated regexp is determinist,
1738 * should be called after xmlFAEliminateEpsilonTransitions()
1739 *
1740 */
1741static int
1742xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1743 int to, xmlRegAtomPtr atom) {
1744 int ret = 1;
1745 int transnr;
1746 xmlRegTransPtr t1;
1747
1748 if (state == NULL)
1749 return(ret);
1750 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1751 t1 = &(state->trans[transnr]);
1752 /*
1753 * check transitions conflicting with the one looked at
1754 */
1755 if (t1->atom == NULL) {
1756 if (t1->to == -1)
1757 continue;
1758 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1759 to, atom);
1760 if (ret == 0)
1761 return(0);
1762 continue;
1763 }
1764 if (t1->to != to)
1765 continue;
1766 if (xmlFACompareAtoms(t1->atom, atom))
1767 return(0);
1768 }
1769 return(ret);
1770}
1771
1772/**
1773 * xmlFAComputesDeterminism:
1774 * @ctxt: a regexp parser context
1775 *
1776 * Check whether the associated regexp is determinist,
1777 * should be called after xmlFAEliminateEpsilonTransitions()
1778 *
1779 */
1780static int
1781xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1782 int statenr, transnr;
1783 xmlRegStatePtr state;
1784 xmlRegTransPtr t1, t2;
1785 int i;
1786 int ret = 1;
1787
Daniel Veillard4402ab42002-09-12 16:02:56 +00001788#ifdef DEBUG_REGEXP_GRAPH
1789 printf("xmlFAComputesDeterminism\n");
1790 xmlRegPrintCtxt(stdout, ctxt);
1791#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001792 if (ctxt->determinist != -1)
1793 return(ctxt->determinist);
1794
1795 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001796 * Check for all states that there aren't 2 transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00001797 * with the same atom and a different target.
1798 */
1799 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1800 state = ctxt->states[statenr];
1801 if (state == NULL)
1802 continue;
1803 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1804 t1 = &(state->trans[transnr]);
1805 /*
1806 * Determinism checks in case of counted or all transitions
1807 * will have to be handled separately
1808 */
1809 if (t1->atom == NULL)
1810 continue;
1811 if (t1->to == -1) /* eliminated */
1812 continue;
1813 for (i = 0;i < transnr;i++) {
1814 t2 = &(state->trans[i]);
1815 if (t2->to == -1) /* eliminated */
1816 continue;
1817 if (t2->atom != NULL) {
1818 if (t1->to == t2->to) {
1819 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00001820 t2->to = -1; /* eliminated */
Daniel Veillarde19fc232002-04-22 16:01:24 +00001821 } else {
1822 /* not determinist ! */
1823 if (xmlFACompareAtoms(t1->atom, t2->atom))
1824 ret = 0;
1825 }
1826 } else if (t1->to != -1) {
1827 /*
1828 * do the closure in case of remaining specific
1829 * epsilon transitions like choices or all
1830 */
1831 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1832 t2->to, t2->atom);
1833 if (ret == 0)
1834 return(0);
1835 }
1836 }
1837 if (ret == 0)
1838 break;
1839 }
1840 if (ret == 0)
1841 break;
1842 }
1843 ctxt->determinist = ret;
1844 return(ret);
1845}
1846
Daniel Veillard4255d502002-04-16 15:50:10 +00001847/************************************************************************
1848 * *
1849 * Routines to check input against transition atoms *
1850 * *
1851 ************************************************************************/
1852
1853static int
1854xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1855 int start, int end, const xmlChar *blockName) {
1856 int ret = 0;
1857
1858 switch (type) {
1859 case XML_REGEXP_STRING:
1860 case XML_REGEXP_SUBREG:
1861 case XML_REGEXP_RANGES:
1862 case XML_REGEXP_EPSILON:
1863 return(-1);
1864 case XML_REGEXP_ANYCHAR:
1865 ret = ((codepoint != '\n') && (codepoint != '\r'));
1866 break;
1867 case XML_REGEXP_CHARVAL:
1868 ret = ((codepoint >= start) && (codepoint <= end));
1869 break;
1870 case XML_REGEXP_NOTSPACE:
1871 neg = !neg;
1872 case XML_REGEXP_ANYSPACE:
1873 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1874 (codepoint == '\t') || (codepoint == ' '));
1875 break;
1876 case XML_REGEXP_NOTINITNAME:
1877 neg = !neg;
1878 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00001879 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001880 (codepoint == '_') || (codepoint == ':'));
1881 break;
1882 case XML_REGEXP_NOTNAMECHAR:
1883 neg = !neg;
1884 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00001885 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001886 (codepoint == '.') || (codepoint == '-') ||
1887 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00001888 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00001889 break;
1890 case XML_REGEXP_NOTDECIMAL:
1891 neg = !neg;
1892 case XML_REGEXP_DECIMAL:
1893 ret = xmlUCSIsCatNd(codepoint);
1894 break;
1895 case XML_REGEXP_REALCHAR:
1896 neg = !neg;
1897 case XML_REGEXP_NOTREALCHAR:
1898 ret = xmlUCSIsCatP(codepoint);
1899 if (ret == 0)
1900 ret = xmlUCSIsCatZ(codepoint);
1901 if (ret == 0)
1902 ret = xmlUCSIsCatC(codepoint);
1903 break;
1904 case XML_REGEXP_LETTER:
1905 ret = xmlUCSIsCatL(codepoint);
1906 break;
1907 case XML_REGEXP_LETTER_UPPERCASE:
1908 ret = xmlUCSIsCatLu(codepoint);
1909 break;
1910 case XML_REGEXP_LETTER_LOWERCASE:
1911 ret = xmlUCSIsCatLl(codepoint);
1912 break;
1913 case XML_REGEXP_LETTER_TITLECASE:
1914 ret = xmlUCSIsCatLt(codepoint);
1915 break;
1916 case XML_REGEXP_LETTER_MODIFIER:
1917 ret = xmlUCSIsCatLm(codepoint);
1918 break;
1919 case XML_REGEXP_LETTER_OTHERS:
1920 ret = xmlUCSIsCatLo(codepoint);
1921 break;
1922 case XML_REGEXP_MARK:
1923 ret = xmlUCSIsCatM(codepoint);
1924 break;
1925 case XML_REGEXP_MARK_NONSPACING:
1926 ret = xmlUCSIsCatMn(codepoint);
1927 break;
1928 case XML_REGEXP_MARK_SPACECOMBINING:
1929 ret = xmlUCSIsCatMc(codepoint);
1930 break;
1931 case XML_REGEXP_MARK_ENCLOSING:
1932 ret = xmlUCSIsCatMe(codepoint);
1933 break;
1934 case XML_REGEXP_NUMBER:
1935 ret = xmlUCSIsCatN(codepoint);
1936 break;
1937 case XML_REGEXP_NUMBER_DECIMAL:
1938 ret = xmlUCSIsCatNd(codepoint);
1939 break;
1940 case XML_REGEXP_NUMBER_LETTER:
1941 ret = xmlUCSIsCatNl(codepoint);
1942 break;
1943 case XML_REGEXP_NUMBER_OTHERS:
1944 ret = xmlUCSIsCatNo(codepoint);
1945 break;
1946 case XML_REGEXP_PUNCT:
1947 ret = xmlUCSIsCatP(codepoint);
1948 break;
1949 case XML_REGEXP_PUNCT_CONNECTOR:
1950 ret = xmlUCSIsCatPc(codepoint);
1951 break;
1952 case XML_REGEXP_PUNCT_DASH:
1953 ret = xmlUCSIsCatPd(codepoint);
1954 break;
1955 case XML_REGEXP_PUNCT_OPEN:
1956 ret = xmlUCSIsCatPs(codepoint);
1957 break;
1958 case XML_REGEXP_PUNCT_CLOSE:
1959 ret = xmlUCSIsCatPe(codepoint);
1960 break;
1961 case XML_REGEXP_PUNCT_INITQUOTE:
1962 ret = xmlUCSIsCatPi(codepoint);
1963 break;
1964 case XML_REGEXP_PUNCT_FINQUOTE:
1965 ret = xmlUCSIsCatPf(codepoint);
1966 break;
1967 case XML_REGEXP_PUNCT_OTHERS:
1968 ret = xmlUCSIsCatPo(codepoint);
1969 break;
1970 case XML_REGEXP_SEPAR:
1971 ret = xmlUCSIsCatZ(codepoint);
1972 break;
1973 case XML_REGEXP_SEPAR_SPACE:
1974 ret = xmlUCSIsCatZs(codepoint);
1975 break;
1976 case XML_REGEXP_SEPAR_LINE:
1977 ret = xmlUCSIsCatZl(codepoint);
1978 break;
1979 case XML_REGEXP_SEPAR_PARA:
1980 ret = xmlUCSIsCatZp(codepoint);
1981 break;
1982 case XML_REGEXP_SYMBOL:
1983 ret = xmlUCSIsCatS(codepoint);
1984 break;
1985 case XML_REGEXP_SYMBOL_MATH:
1986 ret = xmlUCSIsCatSm(codepoint);
1987 break;
1988 case XML_REGEXP_SYMBOL_CURRENCY:
1989 ret = xmlUCSIsCatSc(codepoint);
1990 break;
1991 case XML_REGEXP_SYMBOL_MODIFIER:
1992 ret = xmlUCSIsCatSk(codepoint);
1993 break;
1994 case XML_REGEXP_SYMBOL_OTHERS:
1995 ret = xmlUCSIsCatSo(codepoint);
1996 break;
1997 case XML_REGEXP_OTHER:
1998 ret = xmlUCSIsCatC(codepoint);
1999 break;
2000 case XML_REGEXP_OTHER_CONTROL:
2001 ret = xmlUCSIsCatCc(codepoint);
2002 break;
2003 case XML_REGEXP_OTHER_FORMAT:
2004 ret = xmlUCSIsCatCf(codepoint);
2005 break;
2006 case XML_REGEXP_OTHER_PRIVATE:
2007 ret = xmlUCSIsCatCo(codepoint);
2008 break;
2009 case XML_REGEXP_OTHER_NA:
2010 /* ret = xmlUCSIsCatCn(codepoint); */
2011 /* Seems it doesn't exist anymore in recent Unicode releases */
2012 ret = 0;
2013 break;
2014 case XML_REGEXP_BLOCK_NAME:
2015 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2016 break;
2017 }
2018 if (neg)
2019 return(!ret);
2020 return(ret);
2021}
2022
2023static int
2024xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2025 int i, ret = 0;
2026 xmlRegRangePtr range;
2027
William M. Brack871611b2003-10-18 04:53:14 +00002028 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002029 return(-1);
2030
2031 switch (atom->type) {
2032 case XML_REGEXP_SUBREG:
2033 case XML_REGEXP_EPSILON:
2034 return(-1);
2035 case XML_REGEXP_CHARVAL:
2036 return(codepoint == atom->codepoint);
2037 case XML_REGEXP_RANGES: {
2038 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002039
Daniel Veillard4255d502002-04-16 15:50:10 +00002040 for (i = 0;i < atom->nbRanges;i++) {
2041 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002042 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002043 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2044 0, range->start, range->end,
2045 range->blockName);
2046 if (ret != 0)
2047 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002048 } else if (range->neg) {
2049 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2050 0, range->start, range->end,
2051 range->blockName);
2052 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002053 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002054 else
2055 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002056 } else {
2057 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2058 0, range->start, range->end,
2059 range->blockName);
2060 if (ret != 0)
2061 accept = 1; /* might still be excluded */
2062 }
2063 }
2064 return(accept);
2065 }
2066 case XML_REGEXP_STRING:
2067 printf("TODO: XML_REGEXP_STRING\n");
2068 return(-1);
2069 case XML_REGEXP_ANYCHAR:
2070 case XML_REGEXP_ANYSPACE:
2071 case XML_REGEXP_NOTSPACE:
2072 case XML_REGEXP_INITNAME:
2073 case XML_REGEXP_NOTINITNAME:
2074 case XML_REGEXP_NAMECHAR:
2075 case XML_REGEXP_NOTNAMECHAR:
2076 case XML_REGEXP_DECIMAL:
2077 case XML_REGEXP_NOTDECIMAL:
2078 case XML_REGEXP_REALCHAR:
2079 case XML_REGEXP_NOTREALCHAR:
2080 case XML_REGEXP_LETTER:
2081 case XML_REGEXP_LETTER_UPPERCASE:
2082 case XML_REGEXP_LETTER_LOWERCASE:
2083 case XML_REGEXP_LETTER_TITLECASE:
2084 case XML_REGEXP_LETTER_MODIFIER:
2085 case XML_REGEXP_LETTER_OTHERS:
2086 case XML_REGEXP_MARK:
2087 case XML_REGEXP_MARK_NONSPACING:
2088 case XML_REGEXP_MARK_SPACECOMBINING:
2089 case XML_REGEXP_MARK_ENCLOSING:
2090 case XML_REGEXP_NUMBER:
2091 case XML_REGEXP_NUMBER_DECIMAL:
2092 case XML_REGEXP_NUMBER_LETTER:
2093 case XML_REGEXP_NUMBER_OTHERS:
2094 case XML_REGEXP_PUNCT:
2095 case XML_REGEXP_PUNCT_CONNECTOR:
2096 case XML_REGEXP_PUNCT_DASH:
2097 case XML_REGEXP_PUNCT_OPEN:
2098 case XML_REGEXP_PUNCT_CLOSE:
2099 case XML_REGEXP_PUNCT_INITQUOTE:
2100 case XML_REGEXP_PUNCT_FINQUOTE:
2101 case XML_REGEXP_PUNCT_OTHERS:
2102 case XML_REGEXP_SEPAR:
2103 case XML_REGEXP_SEPAR_SPACE:
2104 case XML_REGEXP_SEPAR_LINE:
2105 case XML_REGEXP_SEPAR_PARA:
2106 case XML_REGEXP_SYMBOL:
2107 case XML_REGEXP_SYMBOL_MATH:
2108 case XML_REGEXP_SYMBOL_CURRENCY:
2109 case XML_REGEXP_SYMBOL_MODIFIER:
2110 case XML_REGEXP_SYMBOL_OTHERS:
2111 case XML_REGEXP_OTHER:
2112 case XML_REGEXP_OTHER_CONTROL:
2113 case XML_REGEXP_OTHER_FORMAT:
2114 case XML_REGEXP_OTHER_PRIVATE:
2115 case XML_REGEXP_OTHER_NA:
2116 case XML_REGEXP_BLOCK_NAME:
2117 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2118 (const xmlChar *)atom->valuep);
2119 if (atom->neg)
2120 ret = !ret;
2121 break;
2122 }
2123 return(ret);
2124}
2125
2126/************************************************************************
2127 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002128 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002129 * *
2130 ************************************************************************/
2131
2132#ifdef DEBUG_REGEXP_EXEC
2133static void
2134xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2135 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2136 if (exec->inputStack != NULL) {
2137 int i;
2138 printf(": ");
2139 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2140 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2141 } else {
2142 printf(": %s", &(exec->inputString[exec->index]));
2143 }
2144 printf("\n");
2145}
2146#endif
2147
2148static void
2149xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2150#ifdef DEBUG_REGEXP_EXEC
2151 printf("saving ");
2152 exec->transno++;
2153 xmlFARegDebugExec(exec);
2154 exec->transno--;
2155#endif
2156
2157 if (exec->maxRollbacks == 0) {
2158 exec->maxRollbacks = 4;
2159 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2160 sizeof(xmlRegExecRollback));
2161 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002162 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002163 exec->maxRollbacks = 0;
2164 return;
2165 }
2166 memset(exec->rollbacks, 0,
2167 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2168 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2169 xmlRegExecRollback *tmp;
2170 int len = exec->maxRollbacks;
2171
2172 exec->maxRollbacks *= 2;
2173 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2174 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2175 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002176 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002177 exec->maxRollbacks /= 2;
2178 return;
2179 }
2180 exec->rollbacks = tmp;
2181 tmp = &exec->rollbacks[len];
2182 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2183 }
2184 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2185 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2186 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2187 if (exec->comp->nbCounters > 0) {
2188 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2189 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2190 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2191 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002192 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002193 exec->status = -5;
2194 return;
2195 }
2196 }
2197 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2198 exec->comp->nbCounters * sizeof(int));
2199 }
2200 exec->nbRollbacks++;
2201}
2202
2203static void
2204xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2205 if (exec->nbRollbacks <= 0) {
2206 exec->status = -1;
2207#ifdef DEBUG_REGEXP_EXEC
2208 printf("rollback failed on empty stack\n");
2209#endif
2210 return;
2211 }
2212 exec->nbRollbacks--;
2213 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2214 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2215 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2216 if (exec->comp->nbCounters > 0) {
2217 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2218 fprintf(stderr, "exec save: allocation failed");
2219 exec->status = -6;
2220 return;
2221 }
2222 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2223 exec->comp->nbCounters * sizeof(int));
2224 }
2225
2226#ifdef DEBUG_REGEXP_EXEC
2227 printf("restored ");
2228 xmlFARegDebugExec(exec);
2229#endif
2230}
2231
2232/************************************************************************
2233 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002234 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002235 * *
2236 ************************************************************************/
2237
2238static int
2239xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2240 xmlRegExecCtxt execval;
2241 xmlRegExecCtxtPtr exec = &execval;
2242 int ret, codepoint, len;
2243
2244 exec->inputString = content;
2245 exec->index = 0;
2246 exec->determinist = 1;
2247 exec->maxRollbacks = 0;
2248 exec->nbRollbacks = 0;
2249 exec->rollbacks = NULL;
2250 exec->status = 0;
2251 exec->comp = comp;
2252 exec->state = comp->states[0];
2253 exec->transno = 0;
2254 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002255 exec->inputStack = NULL;
2256 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002257 if (comp->nbCounters > 0) {
2258 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002259 if (exec->counts == NULL) {
2260 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002261 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002262 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002263 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2264 } else
2265 exec->counts = NULL;
2266 while ((exec->status == 0) &&
2267 ((exec->inputString[exec->index] != 0) ||
2268 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2269 xmlRegTransPtr trans;
2270 xmlRegAtomPtr atom;
2271
2272 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002273 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002274 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002275 * on counters, in that case don't break too early. Additionally,
2276 * if we are working on a range like "AB{0,2}", where B is not present,
2277 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002278 */
William M. Brack0e00b282004-04-26 15:40:47 +00002279 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002280 /*
2281 * if there is a transition, we must check if
2282 * atom allows minOccurs of 0
2283 */
2284 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002285 trans = &exec->state->trans[exec->transno];
2286 if (trans->to >=0) {
2287 atom = trans->atom;
2288 if (!((atom->min == 0) && (atom->max > 0)))
2289 goto rollback;
2290 }
2291 } else
2292 goto rollback;
2293 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002294
2295 exec->transcount = 0;
2296 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2297 trans = &exec->state->trans[exec->transno];
2298 if (trans->to < 0)
2299 continue;
2300 atom = trans->atom;
2301 ret = 0;
2302 if (trans->count >= 0) {
2303 int count;
2304 xmlRegCounterPtr counter;
2305
2306 /*
2307 * A counted transition.
2308 */
2309
2310 count = exec->counts[trans->count];
2311 counter = &exec->comp->counters[trans->count];
2312#ifdef DEBUG_REGEXP_EXEC
2313 printf("testing count %d: val %d, min %d, max %d\n",
2314 trans->count, count, counter->min, counter->max);
2315#endif
2316 ret = ((count >= counter->min) && (count <= counter->max));
2317 } else if (atom == NULL) {
2318 fprintf(stderr, "epsilon transition left at runtime\n");
2319 exec->status = -2;
2320 break;
2321 } else if (exec->inputString[exec->index] != 0) {
2322 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2323 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002324 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002325 xmlRegStatePtr to = comp->states[trans->to];
2326
2327 /*
2328 * this is a multiple input sequence
2329 */
2330 if (exec->state->nbTrans > exec->transno + 1) {
2331 xmlFARegExecSave(exec);
2332 }
2333 exec->transcount = 1;
2334 do {
2335 /*
2336 * Try to progress as much as possible on the input
2337 */
2338 if (exec->transcount == atom->max) {
2339 break;
2340 }
2341 exec->index += len;
2342 /*
2343 * End of input: stop here
2344 */
2345 if (exec->inputString[exec->index] == 0) {
2346 exec->index -= len;
2347 break;
2348 }
2349 if (exec->transcount >= atom->min) {
2350 int transno = exec->transno;
2351 xmlRegStatePtr state = exec->state;
2352
2353 /*
2354 * The transition is acceptable save it
2355 */
2356 exec->transno = -1; /* trick */
2357 exec->state = to;
2358 xmlFARegExecSave(exec);
2359 exec->transno = transno;
2360 exec->state = state;
2361 }
2362 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2363 len);
2364 ret = xmlRegCheckCharacter(atom, codepoint);
2365 exec->transcount++;
2366 } while (ret == 1);
2367 if (exec->transcount < atom->min)
2368 ret = 0;
2369
2370 /*
2371 * If the last check failed but one transition was found
2372 * possible, rollback
2373 */
2374 if (ret < 0)
2375 ret = 0;
2376 if (ret == 0) {
2377 goto rollback;
2378 }
William M. Brack0e00b282004-04-26 15:40:47 +00002379 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2380 /*
2381 * we don't match on the codepoint, but minOccurs of 0
2382 * says that's ok. Setting len to 0 inhibits stepping
2383 * over the codepoint.
2384 */
2385 exec->transcount = 1;
2386 len = 0;
2387 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002388 }
William M. Brack0e00b282004-04-26 15:40:47 +00002389 } else if ((atom->min == 0) && (atom->max > 0)) {
2390 /* another spot to match when minOccurs is 0 */
2391 exec->transcount = 1;
2392 len = 0;
2393 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002394 }
2395 if (ret == 1) {
2396 if (exec->state->nbTrans > exec->transno + 1) {
2397 xmlFARegExecSave(exec);
2398 }
2399 if (trans->counter >= 0) {
2400#ifdef DEBUG_REGEXP_EXEC
2401 printf("Increasing count %d\n", trans->counter);
2402#endif
2403 exec->counts[trans->counter]++;
2404 }
2405#ifdef DEBUG_REGEXP_EXEC
2406 printf("entering state %d\n", trans->to);
2407#endif
2408 exec->state = comp->states[trans->to];
2409 exec->transno = 0;
2410 if (trans->atom != NULL) {
2411 exec->index += len;
2412 }
2413 goto progress;
2414 } else if (ret < 0) {
2415 exec->status = -4;
2416 break;
2417 }
2418 }
2419 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2420rollback:
2421 /*
2422 * Failed to find a way out
2423 */
2424 exec->determinist = 0;
2425 xmlFARegExecRollBack(exec);
2426 }
2427progress:
2428 continue;
2429 }
2430 if (exec->rollbacks != NULL) {
2431 if (exec->counts != NULL) {
2432 int i;
2433
2434 for (i = 0;i < exec->maxRollbacks;i++)
2435 if (exec->rollbacks[i].counts != NULL)
2436 xmlFree(exec->rollbacks[i].counts);
2437 }
2438 xmlFree(exec->rollbacks);
2439 }
2440 if (exec->counts != NULL)
2441 xmlFree(exec->counts);
2442 if (exec->status == 0)
2443 return(1);
2444 if (exec->status == -1)
2445 return(0);
2446 return(exec->status);
2447}
2448
2449/************************************************************************
2450 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002451 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002452 * *
2453 ************************************************************************/
2454
2455/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002456 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002457 * @comp: a precompiled regular expression
2458 * @callback: a callback function used for handling progresses in the
2459 * automata matching phase
2460 * @data: the context data associated to the callback in this context
2461 *
2462 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002463 *
2464 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002465 */
2466xmlRegExecCtxtPtr
2467xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2468 xmlRegExecCtxtPtr exec;
2469
2470 if (comp == NULL)
2471 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002472 if ((comp->compact == NULL) && (comp->states == NULL))
2473 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002474 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2475 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002476 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002477 return(NULL);
2478 }
2479 memset(exec, 0, sizeof(xmlRegExecCtxt));
2480 exec->inputString = NULL;
2481 exec->index = 0;
2482 exec->determinist = 1;
2483 exec->maxRollbacks = 0;
2484 exec->nbRollbacks = 0;
2485 exec->rollbacks = NULL;
2486 exec->status = 0;
2487 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002488 if (comp->compact == NULL)
2489 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002490 exec->transno = 0;
2491 exec->transcount = 0;
2492 exec->callback = callback;
2493 exec->data = data;
2494 if (comp->nbCounters > 0) {
2495 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2496 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002497 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002498 xmlFree(exec);
2499 return(NULL);
2500 }
2501 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2502 } else
2503 exec->counts = NULL;
2504 exec->inputStackMax = 0;
2505 exec->inputStackNr = 0;
2506 exec->inputStack = NULL;
2507 return(exec);
2508}
2509
2510/**
2511 * xmlRegFreeExecCtxt:
2512 * @exec: a regular expression evaulation context
2513 *
2514 * Free the structures associated to a regular expression evaulation context.
2515 */
2516void
2517xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2518 if (exec == NULL)
2519 return;
2520
2521 if (exec->rollbacks != NULL) {
2522 if (exec->counts != NULL) {
2523 int i;
2524
2525 for (i = 0;i < exec->maxRollbacks;i++)
2526 if (exec->rollbacks[i].counts != NULL)
2527 xmlFree(exec->rollbacks[i].counts);
2528 }
2529 xmlFree(exec->rollbacks);
2530 }
2531 if (exec->counts != NULL)
2532 xmlFree(exec->counts);
2533 if (exec->inputStack != NULL) {
2534 int i;
2535
Daniel Veillard32370232002-10-16 14:08:14 +00002536 for (i = 0;i < exec->inputStackNr;i++) {
2537 if (exec->inputStack[i].value != NULL)
2538 xmlFree(exec->inputStack[i].value);
2539 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002540 xmlFree(exec->inputStack);
2541 }
2542 xmlFree(exec);
2543}
2544
2545static void
2546xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2547 void *data) {
2548#ifdef DEBUG_PUSH
2549 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2550#endif
2551 if (exec->inputStackMax == 0) {
2552 exec->inputStackMax = 4;
2553 exec->inputStack = (xmlRegInputTokenPtr)
2554 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2555 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002556 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002557 exec->inputStackMax = 0;
2558 return;
2559 }
2560 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2561 xmlRegInputTokenPtr tmp;
2562
2563 exec->inputStackMax *= 2;
2564 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2565 exec->inputStackMax * sizeof(xmlRegInputToken));
2566 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002567 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002568 exec->inputStackMax /= 2;
2569 return;
2570 }
2571 exec->inputStack = tmp;
2572 }
2573 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2574 exec->inputStack[exec->inputStackNr].data = data;
2575 exec->inputStackNr++;
2576 exec->inputStack[exec->inputStackNr].value = NULL;
2577 exec->inputStack[exec->inputStackNr].data = NULL;
2578}
2579
2580
2581/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002582 * xmlRegCompactPushString:
2583 * @exec: a regexp execution context
2584 * @comp: the precompiled exec with a compact table
2585 * @value: a string token input
2586 * @data: data associated to the token to reuse in callbacks
2587 *
2588 * Push one input token in the execution context
2589 *
2590 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2591 * a negative value in case of error.
2592 */
2593static int
2594xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2595 xmlRegexpPtr comp,
2596 const xmlChar *value,
2597 void *data) {
2598 int state = exec->index;
2599 int i, target;
2600
2601 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2602 return(-1);
2603
2604 if (value == NULL) {
2605 /*
2606 * are we at a final state ?
2607 */
2608 if (comp->compact[state * (comp->nbstrings + 1)] ==
2609 XML_REGEXP_FINAL_STATE)
2610 return(1);
2611 return(0);
2612 }
2613
2614#ifdef DEBUG_PUSH
2615 printf("value pushed: %s\n", value);
2616#endif
2617
2618 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002619 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00002620 */
2621 for (i = 0;i < comp->nbstrings;i++) {
2622 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2623 if ((target > 0) && (target <= comp->nbstates)) {
2624 target--; /* to avoid 0 */
2625 if (xmlStrEqual(comp->stringMap[i], value)) {
2626 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002627 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2628 exec->callback(exec->data, value,
2629 comp->transdata[state * comp->nbstrings + i], data);
2630 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002631#ifdef DEBUG_PUSH
2632 printf("entering state %d\n", target);
2633#endif
2634 if (comp->compact[target * (comp->nbstrings + 1)] ==
2635 XML_REGEXP_FINAL_STATE)
2636 return(1);
2637 return(0);
2638 }
2639 }
2640 }
2641 /*
2642 * Failed to find an exit transition out from current state for the
2643 * current token
2644 */
2645#ifdef DEBUG_PUSH
2646 printf("failed to find a transition for %s on state %d\n", value, state);
2647#endif
2648 exec->status = -1;
2649 return(-1);
2650}
2651
2652/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002653 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002654 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002655 * @value: a string token input
2656 * @data: data associated to the token to reuse in callbacks
2657 *
2658 * Push one input token in the execution context
2659 *
2660 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2661 * a negative value in case of error.
2662 */
2663int
2664xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2665 void *data) {
2666 xmlRegTransPtr trans;
2667 xmlRegAtomPtr atom;
2668 int ret;
2669 int final = 0;
2670
2671 if (exec == NULL)
2672 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002673 if (exec->comp == NULL)
2674 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002675 if (exec->status != 0)
2676 return(exec->status);
2677
Daniel Veillard23e73572002-09-19 19:56:43 +00002678 if (exec->comp->compact != NULL)
2679 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2680
Daniel Veillard4255d502002-04-16 15:50:10 +00002681 if (value == NULL) {
2682 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2683 return(1);
2684 final = 1;
2685 }
2686
2687#ifdef DEBUG_PUSH
2688 printf("value pushed: %s\n", value);
2689#endif
2690 /*
2691 * If we have an active rollback stack push the new value there
2692 * and get back to where we were left
2693 */
2694 if ((value != NULL) && (exec->inputStackNr > 0)) {
2695 xmlFARegExecSaveInputString(exec, value, data);
2696 value = exec->inputStack[exec->index].value;
2697 data = exec->inputStack[exec->index].data;
2698#ifdef DEBUG_PUSH
2699 printf("value loaded: %s\n", value);
2700#endif
2701 }
2702
2703 while ((exec->status == 0) &&
2704 ((value != NULL) ||
2705 ((final == 1) &&
2706 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2707
2708 /*
2709 * End of input on non-terminal state, rollback, however we may
2710 * still have epsilon like transition for counted transitions
2711 * on counters, in that case don't break too early.
2712 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002713 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002714 goto rollback;
2715
2716 exec->transcount = 0;
2717 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2718 trans = &exec->state->trans[exec->transno];
2719 if (trans->to < 0)
2720 continue;
2721 atom = trans->atom;
2722 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002723 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2724 int i;
2725 int count;
2726 xmlRegTransPtr t;
2727 xmlRegCounterPtr counter;
2728
2729 ret = 0;
2730
2731#ifdef DEBUG_PUSH
2732 printf("testing all lax %d\n", trans->count);
2733#endif
2734 /*
2735 * Check all counted transitions from the current state
2736 */
2737 if ((value == NULL) && (final)) {
2738 ret = 1;
2739 } else if (value != NULL) {
2740 for (i = 0;i < exec->state->nbTrans;i++) {
2741 t = &exec->state->trans[i];
2742 if ((t->counter < 0) || (t == trans))
2743 continue;
2744 counter = &exec->comp->counters[t->counter];
2745 count = exec->counts[t->counter];
2746 if ((count < counter->max) &&
2747 (t->atom != NULL) &&
2748 (xmlStrEqual(value, t->atom->valuep))) {
2749 ret = 0;
2750 break;
2751 }
2752 if ((count >= counter->min) &&
2753 (count < counter->max) &&
2754 (xmlStrEqual(value, t->atom->valuep))) {
2755 ret = 1;
2756 break;
2757 }
2758 }
2759 }
2760 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002761 int i;
2762 int count;
2763 xmlRegTransPtr t;
2764 xmlRegCounterPtr counter;
2765
2766 ret = 1;
2767
2768#ifdef DEBUG_PUSH
2769 printf("testing all %d\n", trans->count);
2770#endif
2771 /*
2772 * Check all counted transitions from the current state
2773 */
2774 for (i = 0;i < exec->state->nbTrans;i++) {
2775 t = &exec->state->trans[i];
2776 if ((t->counter < 0) || (t == trans))
2777 continue;
2778 counter = &exec->comp->counters[t->counter];
2779 count = exec->counts[t->counter];
2780 if ((count < counter->min) || (count > counter->max)) {
2781 ret = 0;
2782 break;
2783 }
2784 }
2785 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002786 int count;
2787 xmlRegCounterPtr counter;
2788
2789 /*
2790 * A counted transition.
2791 */
2792
2793 count = exec->counts[trans->count];
2794 counter = &exec->comp->counters[trans->count];
2795#ifdef DEBUG_PUSH
2796 printf("testing count %d: val %d, min %d, max %d\n",
2797 trans->count, count, counter->min, counter->max);
2798#endif
2799 ret = ((count >= counter->min) && (count <= counter->max));
2800 } else if (atom == NULL) {
2801 fprintf(stderr, "epsilon transition left at runtime\n");
2802 exec->status = -2;
2803 break;
2804 } else if (value != NULL) {
2805 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002806 if ((ret == 1) && (trans->counter >= 0)) {
2807 xmlRegCounterPtr counter;
2808 int count;
2809
2810 count = exec->counts[trans->counter];
2811 counter = &exec->comp->counters[trans->counter];
2812 if (count >= counter->max)
2813 ret = 0;
2814 }
2815
Daniel Veillard4255d502002-04-16 15:50:10 +00002816 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2817 xmlRegStatePtr to = exec->comp->states[trans->to];
2818
2819 /*
2820 * this is a multiple input sequence
2821 */
2822 if (exec->state->nbTrans > exec->transno + 1) {
2823 if (exec->inputStackNr <= 0) {
2824 xmlFARegExecSaveInputString(exec, value, data);
2825 }
2826 xmlFARegExecSave(exec);
2827 }
2828 exec->transcount = 1;
2829 do {
2830 /*
2831 * Try to progress as much as possible on the input
2832 */
2833 if (exec->transcount == atom->max) {
2834 break;
2835 }
2836 exec->index++;
2837 value = exec->inputStack[exec->index].value;
2838 data = exec->inputStack[exec->index].data;
2839#ifdef DEBUG_PUSH
2840 printf("value loaded: %s\n", value);
2841#endif
2842
2843 /*
2844 * End of input: stop here
2845 */
2846 if (value == NULL) {
2847 exec->index --;
2848 break;
2849 }
2850 if (exec->transcount >= atom->min) {
2851 int transno = exec->transno;
2852 xmlRegStatePtr state = exec->state;
2853
2854 /*
2855 * The transition is acceptable save it
2856 */
2857 exec->transno = -1; /* trick */
2858 exec->state = to;
2859 if (exec->inputStackNr <= 0) {
2860 xmlFARegExecSaveInputString(exec, value, data);
2861 }
2862 xmlFARegExecSave(exec);
2863 exec->transno = transno;
2864 exec->state = state;
2865 }
2866 ret = xmlStrEqual(value, atom->valuep);
2867 exec->transcount++;
2868 } while (ret == 1);
2869 if (exec->transcount < atom->min)
2870 ret = 0;
2871
2872 /*
2873 * If the last check failed but one transition was found
2874 * possible, rollback
2875 */
2876 if (ret < 0)
2877 ret = 0;
2878 if (ret == 0) {
2879 goto rollback;
2880 }
2881 }
2882 }
2883 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00002884 if ((exec->callback != NULL) && (atom != NULL) &&
2885 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002886 exec->callback(exec->data, atom->valuep,
2887 atom->data, data);
2888 }
2889 if (exec->state->nbTrans > exec->transno + 1) {
2890 if (exec->inputStackNr <= 0) {
2891 xmlFARegExecSaveInputString(exec, value, data);
2892 }
2893 xmlFARegExecSave(exec);
2894 }
2895 if (trans->counter >= 0) {
2896#ifdef DEBUG_PUSH
2897 printf("Increasing count %d\n", trans->counter);
2898#endif
2899 exec->counts[trans->counter]++;
2900 }
2901#ifdef DEBUG_PUSH
2902 printf("entering state %d\n", trans->to);
2903#endif
2904 exec->state = exec->comp->states[trans->to];
2905 exec->transno = 0;
2906 if (trans->atom != NULL) {
2907 if (exec->inputStack != NULL) {
2908 exec->index++;
2909 if (exec->index < exec->inputStackNr) {
2910 value = exec->inputStack[exec->index].value;
2911 data = exec->inputStack[exec->index].data;
2912#ifdef DEBUG_PUSH
2913 printf("value loaded: %s\n", value);
2914#endif
2915 } else {
2916 value = NULL;
2917 data = NULL;
2918#ifdef DEBUG_PUSH
2919 printf("end of input\n");
2920#endif
2921 }
2922 } else {
2923 value = NULL;
2924 data = NULL;
2925#ifdef DEBUG_PUSH
2926 printf("end of input\n");
2927#endif
2928 }
2929 }
2930 goto progress;
2931 } else if (ret < 0) {
2932 exec->status = -4;
2933 break;
2934 }
2935 }
2936 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2937rollback:
2938 /*
2939 * Failed to find a way out
2940 */
2941 exec->determinist = 0;
2942 xmlFARegExecRollBack(exec);
2943 if (exec->status == 0) {
2944 value = exec->inputStack[exec->index].value;
2945 data = exec->inputStack[exec->index].data;
2946#ifdef DEBUG_PUSH
2947 printf("value loaded: %s\n", value);
2948#endif
2949 }
2950 }
2951progress:
2952 continue;
2953 }
2954 if (exec->status == 0) {
2955 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2956 }
2957 return(exec->status);
2958}
2959
Daniel Veillard52b48c72003-04-13 19:53:42 +00002960/**
2961 * xmlRegExecPushString2:
2962 * @exec: a regexp execution context or NULL to indicate the end
2963 * @value: the first string token input
2964 * @value2: the second string token input
2965 * @data: data associated to the token to reuse in callbacks
2966 *
2967 * Push one input token in the execution context
2968 *
2969 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2970 * a negative value in case of error.
2971 */
2972int
2973xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
2974 const xmlChar *value2, void *data) {
2975 xmlChar buf[150];
2976 int lenn, lenp, ret;
2977 xmlChar *str;
2978
2979 if (exec == NULL)
2980 return(-1);
2981 if (exec->comp == NULL)
2982 return(-1);
2983 if (exec->status != 0)
2984 return(exec->status);
2985
2986 if (value2 == NULL)
2987 return(xmlRegExecPushString(exec, value, data));
2988
2989 lenn = strlen((char *) value2);
2990 lenp = strlen((char *) value);
2991
2992 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002993 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002994 if (str == NULL) {
2995 exec->status = -1;
2996 return(-1);
2997 }
2998 } else {
2999 str = buf;
3000 }
3001 memcpy(&str[0], value, lenp);
3002 str[lenp] = '|';
3003 memcpy(&str[lenp + 1], value2, lenn);
3004 str[lenn + lenp + 1] = 0;
3005
3006 if (exec->comp->compact != NULL)
3007 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3008 else
3009 ret = xmlRegExecPushString(exec, str, data);
3010
3011 if (str != buf)
3012 xmlFree(buf);
3013 return(ret);
3014}
3015
Daniel Veillard4255d502002-04-16 15:50:10 +00003016#if 0
3017static int
3018xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3019 xmlRegTransPtr trans;
3020 xmlRegAtomPtr atom;
3021 int ret;
3022 int codepoint, len;
3023
3024 if (exec == NULL)
3025 return(-1);
3026 if (exec->status != 0)
3027 return(exec->status);
3028
3029 while ((exec->status == 0) &&
3030 ((exec->inputString[exec->index] != 0) ||
3031 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3032
3033 /*
3034 * End of input on non-terminal state, rollback, however we may
3035 * still have epsilon like transition for counted transitions
3036 * on counters, in that case don't break too early.
3037 */
3038 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3039 goto rollback;
3040
3041 exec->transcount = 0;
3042 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3043 trans = &exec->state->trans[exec->transno];
3044 if (trans->to < 0)
3045 continue;
3046 atom = trans->atom;
3047 ret = 0;
3048 if (trans->count >= 0) {
3049 int count;
3050 xmlRegCounterPtr counter;
3051
3052 /*
3053 * A counted transition.
3054 */
3055
3056 count = exec->counts[trans->count];
3057 counter = &exec->comp->counters[trans->count];
3058#ifdef DEBUG_REGEXP_EXEC
3059 printf("testing count %d: val %d, min %d, max %d\n",
3060 trans->count, count, counter->min, counter->max);
3061#endif
3062 ret = ((count >= counter->min) && (count <= counter->max));
3063 } else if (atom == NULL) {
3064 fprintf(stderr, "epsilon transition left at runtime\n");
3065 exec->status = -2;
3066 break;
3067 } else if (exec->inputString[exec->index] != 0) {
3068 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3069 ret = xmlRegCheckCharacter(atom, codepoint);
3070 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3071 xmlRegStatePtr to = exec->comp->states[trans->to];
3072
3073 /*
3074 * this is a multiple input sequence
3075 */
3076 if (exec->state->nbTrans > exec->transno + 1) {
3077 xmlFARegExecSave(exec);
3078 }
3079 exec->transcount = 1;
3080 do {
3081 /*
3082 * Try to progress as much as possible on the input
3083 */
3084 if (exec->transcount == atom->max) {
3085 break;
3086 }
3087 exec->index += len;
3088 /*
3089 * End of input: stop here
3090 */
3091 if (exec->inputString[exec->index] == 0) {
3092 exec->index -= len;
3093 break;
3094 }
3095 if (exec->transcount >= atom->min) {
3096 int transno = exec->transno;
3097 xmlRegStatePtr state = exec->state;
3098
3099 /*
3100 * The transition is acceptable save it
3101 */
3102 exec->transno = -1; /* trick */
3103 exec->state = to;
3104 xmlFARegExecSave(exec);
3105 exec->transno = transno;
3106 exec->state = state;
3107 }
3108 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3109 len);
3110 ret = xmlRegCheckCharacter(atom, codepoint);
3111 exec->transcount++;
3112 } while (ret == 1);
3113 if (exec->transcount < atom->min)
3114 ret = 0;
3115
3116 /*
3117 * If the last check failed but one transition was found
3118 * possible, rollback
3119 */
3120 if (ret < 0)
3121 ret = 0;
3122 if (ret == 0) {
3123 goto rollback;
3124 }
3125 }
3126 }
3127 if (ret == 1) {
3128 if (exec->state->nbTrans > exec->transno + 1) {
3129 xmlFARegExecSave(exec);
3130 }
3131 if (trans->counter >= 0) {
3132#ifdef DEBUG_REGEXP_EXEC
3133 printf("Increasing count %d\n", trans->counter);
3134#endif
3135 exec->counts[trans->counter]++;
3136 }
3137#ifdef DEBUG_REGEXP_EXEC
3138 printf("entering state %d\n", trans->to);
3139#endif
3140 exec->state = exec->comp->states[trans->to];
3141 exec->transno = 0;
3142 if (trans->atom != NULL) {
3143 exec->index += len;
3144 }
3145 goto progress;
3146 } else if (ret < 0) {
3147 exec->status = -4;
3148 break;
3149 }
3150 }
3151 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3152rollback:
3153 /*
3154 * Failed to find a way out
3155 */
3156 exec->determinist = 0;
3157 xmlFARegExecRollBack(exec);
3158 }
3159progress:
3160 continue;
3161 }
3162}
3163#endif
3164/************************************************************************
3165 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003166 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00003167 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3168 * *
3169 ************************************************************************/
3170
3171/**
3172 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003173 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003174 *
3175 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3176 */
3177static int
3178xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3179 int cur;
3180 int len;
3181
3182 cur = CUR_SCHAR(ctxt->cur, len);
3183 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3184 (cur == '*') || (cur == '+') || (cur == '(') ||
3185 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3186 (cur == 0x5D) || (cur == 0))
3187 return(-1);
3188 return(cur);
3189}
3190
3191/**
3192 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003193 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003194 *
3195 * [27] charProp ::= IsCategory | IsBlock
3196 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3197 * Separators | Symbols | Others
3198 * [29] Letters ::= 'L' [ultmo]?
3199 * [30] Marks ::= 'M' [nce]?
3200 * [31] Numbers ::= 'N' [dlo]?
3201 * [32] Punctuation ::= 'P' [cdseifo]?
3202 * [33] Separators ::= 'Z' [slp]?
3203 * [34] Symbols ::= 'S' [mcko]?
3204 * [35] Others ::= 'C' [cfon]?
3205 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3206 */
3207static void
3208xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3209 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003210 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003211 xmlChar *blockName = NULL;
3212
3213 cur = CUR;
3214 if (cur == 'L') {
3215 NEXT;
3216 cur = CUR;
3217 if (cur == 'u') {
3218 NEXT;
3219 type = XML_REGEXP_LETTER_UPPERCASE;
3220 } else if (cur == 'l') {
3221 NEXT;
3222 type = XML_REGEXP_LETTER_LOWERCASE;
3223 } else if (cur == 't') {
3224 NEXT;
3225 type = XML_REGEXP_LETTER_TITLECASE;
3226 } else if (cur == 'm') {
3227 NEXT;
3228 type = XML_REGEXP_LETTER_MODIFIER;
3229 } else if (cur == 'o') {
3230 NEXT;
3231 type = XML_REGEXP_LETTER_OTHERS;
3232 } else {
3233 type = XML_REGEXP_LETTER;
3234 }
3235 } else if (cur == 'M') {
3236 NEXT;
3237 cur = CUR;
3238 if (cur == 'n') {
3239 NEXT;
3240 /* nonspacing */
3241 type = XML_REGEXP_MARK_NONSPACING;
3242 } else if (cur == 'c') {
3243 NEXT;
3244 /* spacing combining */
3245 type = XML_REGEXP_MARK_SPACECOMBINING;
3246 } else if (cur == 'e') {
3247 NEXT;
3248 /* enclosing */
3249 type = XML_REGEXP_MARK_ENCLOSING;
3250 } else {
3251 /* all marks */
3252 type = XML_REGEXP_MARK;
3253 }
3254 } else if (cur == 'N') {
3255 NEXT;
3256 cur = CUR;
3257 if (cur == 'd') {
3258 NEXT;
3259 /* digital */
3260 type = XML_REGEXP_NUMBER_DECIMAL;
3261 } else if (cur == 'l') {
3262 NEXT;
3263 /* letter */
3264 type = XML_REGEXP_NUMBER_LETTER;
3265 } else if (cur == 'o') {
3266 NEXT;
3267 /* other */
3268 type = XML_REGEXP_NUMBER_OTHERS;
3269 } else {
3270 /* all numbers */
3271 type = XML_REGEXP_NUMBER;
3272 }
3273 } else if (cur == 'P') {
3274 NEXT;
3275 cur = CUR;
3276 if (cur == 'c') {
3277 NEXT;
3278 /* connector */
3279 type = XML_REGEXP_PUNCT_CONNECTOR;
3280 } else if (cur == 'd') {
3281 NEXT;
3282 /* dash */
3283 type = XML_REGEXP_PUNCT_DASH;
3284 } else if (cur == 's') {
3285 NEXT;
3286 /* open */
3287 type = XML_REGEXP_PUNCT_OPEN;
3288 } else if (cur == 'e') {
3289 NEXT;
3290 /* close */
3291 type = XML_REGEXP_PUNCT_CLOSE;
3292 } else if (cur == 'i') {
3293 NEXT;
3294 /* initial quote */
3295 type = XML_REGEXP_PUNCT_INITQUOTE;
3296 } else if (cur == 'f') {
3297 NEXT;
3298 /* final quote */
3299 type = XML_REGEXP_PUNCT_FINQUOTE;
3300 } else if (cur == 'o') {
3301 NEXT;
3302 /* other */
3303 type = XML_REGEXP_PUNCT_OTHERS;
3304 } else {
3305 /* all punctuation */
3306 type = XML_REGEXP_PUNCT;
3307 }
3308 } else if (cur == 'Z') {
3309 NEXT;
3310 cur = CUR;
3311 if (cur == 's') {
3312 NEXT;
3313 /* space */
3314 type = XML_REGEXP_SEPAR_SPACE;
3315 } else if (cur == 'l') {
3316 NEXT;
3317 /* line */
3318 type = XML_REGEXP_SEPAR_LINE;
3319 } else if (cur == 'p') {
3320 NEXT;
3321 /* paragraph */
3322 type = XML_REGEXP_SEPAR_PARA;
3323 } else {
3324 /* all separators */
3325 type = XML_REGEXP_SEPAR;
3326 }
3327 } else if (cur == 'S') {
3328 NEXT;
3329 cur = CUR;
3330 if (cur == 'm') {
3331 NEXT;
3332 type = XML_REGEXP_SYMBOL_MATH;
3333 /* math */
3334 } else if (cur == 'c') {
3335 NEXT;
3336 type = XML_REGEXP_SYMBOL_CURRENCY;
3337 /* currency */
3338 } else if (cur == 'k') {
3339 NEXT;
3340 type = XML_REGEXP_SYMBOL_MODIFIER;
3341 /* modifiers */
3342 } else if (cur == 'o') {
3343 NEXT;
3344 type = XML_REGEXP_SYMBOL_OTHERS;
3345 /* other */
3346 } else {
3347 /* all symbols */
3348 type = XML_REGEXP_SYMBOL;
3349 }
3350 } else if (cur == 'C') {
3351 NEXT;
3352 cur = CUR;
3353 if (cur == 'c') {
3354 NEXT;
3355 /* control */
3356 type = XML_REGEXP_OTHER_CONTROL;
3357 } else if (cur == 'f') {
3358 NEXT;
3359 /* format */
3360 type = XML_REGEXP_OTHER_FORMAT;
3361 } else if (cur == 'o') {
3362 NEXT;
3363 /* private use */
3364 type = XML_REGEXP_OTHER_PRIVATE;
3365 } else if (cur == 'n') {
3366 NEXT;
3367 /* not assigned */
3368 type = XML_REGEXP_OTHER_NA;
3369 } else {
3370 /* all others */
3371 type = XML_REGEXP_OTHER;
3372 }
3373 } else if (cur == 'I') {
3374 const xmlChar *start;
3375 NEXT;
3376 cur = CUR;
3377 if (cur != 's') {
3378 ERROR("IsXXXX expected");
3379 return;
3380 }
3381 NEXT;
3382 start = ctxt->cur;
3383 cur = CUR;
3384 if (((cur >= 'a') && (cur <= 'z')) ||
3385 ((cur >= 'A') && (cur <= 'Z')) ||
3386 ((cur >= '0') && (cur <= '9')) ||
3387 (cur == 0x2D)) {
3388 NEXT;
3389 cur = CUR;
3390 while (((cur >= 'a') && (cur <= 'z')) ||
3391 ((cur >= 'A') && (cur <= 'Z')) ||
3392 ((cur >= '0') && (cur <= '9')) ||
3393 (cur == 0x2D)) {
3394 NEXT;
3395 cur = CUR;
3396 }
3397 }
3398 type = XML_REGEXP_BLOCK_NAME;
3399 blockName = xmlStrndup(start, ctxt->cur - start);
3400 } else {
3401 ERROR("Unknown char property");
3402 return;
3403 }
3404 if (ctxt->atom == NULL) {
3405 ctxt->atom = xmlRegNewAtom(ctxt, type);
3406 if (ctxt->atom != NULL)
3407 ctxt->atom->valuep = blockName;
3408 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3409 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3410 type, 0, 0, blockName);
3411 }
3412}
3413
3414/**
3415 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003416 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003417 *
3418 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3419 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3420 * [25] catEsc ::= '\p{' charProp '}'
3421 * [26] complEsc ::= '\P{' charProp '}'
3422 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3423 */
3424static void
3425xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3426 int cur;
3427
3428 if (CUR == '.') {
3429 if (ctxt->atom == NULL) {
3430 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3431 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3432 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3433 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3434 }
3435 NEXT;
3436 return;
3437 }
3438 if (CUR != '\\') {
3439 ERROR("Escaped sequence: expecting \\");
3440 return;
3441 }
3442 NEXT;
3443 cur = CUR;
3444 if (cur == 'p') {
3445 NEXT;
3446 if (CUR != '{') {
3447 ERROR("Expecting '{'");
3448 return;
3449 }
3450 NEXT;
3451 xmlFAParseCharProp(ctxt);
3452 if (CUR != '}') {
3453 ERROR("Expecting '}'");
3454 return;
3455 }
3456 NEXT;
3457 } else if (cur == 'P') {
3458 NEXT;
3459 if (CUR != '{') {
3460 ERROR("Expecting '{'");
3461 return;
3462 }
3463 NEXT;
3464 xmlFAParseCharProp(ctxt);
3465 ctxt->atom->neg = 1;
3466 if (CUR != '}') {
3467 ERROR("Expecting '}'");
3468 return;
3469 }
3470 NEXT;
3471 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3472 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3473 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3474 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3475 (cur == 0x5E)) {
3476 if (ctxt->atom == NULL) {
3477 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3478 if (ctxt->atom != NULL)
3479 ctxt->atom->codepoint = cur;
3480 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3481 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3482 XML_REGEXP_CHARVAL, cur, cur, NULL);
3483 }
3484 NEXT;
3485 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3486 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3487 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003488 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003489
3490 switch (cur) {
3491 case 's':
3492 type = XML_REGEXP_ANYSPACE;
3493 break;
3494 case 'S':
3495 type = XML_REGEXP_NOTSPACE;
3496 break;
3497 case 'i':
3498 type = XML_REGEXP_INITNAME;
3499 break;
3500 case 'I':
3501 type = XML_REGEXP_NOTINITNAME;
3502 break;
3503 case 'c':
3504 type = XML_REGEXP_NAMECHAR;
3505 break;
3506 case 'C':
3507 type = XML_REGEXP_NOTNAMECHAR;
3508 break;
3509 case 'd':
3510 type = XML_REGEXP_DECIMAL;
3511 break;
3512 case 'D':
3513 type = XML_REGEXP_NOTDECIMAL;
3514 break;
3515 case 'w':
3516 type = XML_REGEXP_REALCHAR;
3517 break;
3518 case 'W':
3519 type = XML_REGEXP_NOTREALCHAR;
3520 break;
3521 }
3522 NEXT;
3523 if (ctxt->atom == NULL) {
3524 ctxt->atom = xmlRegNewAtom(ctxt, type);
3525 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3526 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3527 type, 0, 0, NULL);
3528 }
3529 }
3530}
3531
3532/**
3533 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003534 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003535 *
3536 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3537 */
3538static int
3539xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3540 int ret = 0, cur;
3541
3542 if ((CUR != '&') || (NXT(1) != '#'))
3543 return(-1);
3544 NEXT;
3545 NEXT;
3546 cur = CUR;
3547 if (cur == 'x') {
3548 NEXT;
3549 cur = CUR;
3550 if (((cur >= '0') && (cur <= '9')) ||
3551 ((cur >= 'a') && (cur <= 'f')) ||
3552 ((cur >= 'A') && (cur <= 'F'))) {
3553 while (((cur >= '0') && (cur <= '9')) ||
3554 ((cur >= 'A') && (cur <= 'F'))) {
3555 if ((cur >= '0') && (cur <= '9'))
3556 ret = ret * 16 + cur - '0';
3557 else if ((cur >= 'a') && (cur <= 'f'))
3558 ret = ret * 16 + 10 + (cur - 'a');
3559 else
3560 ret = ret * 16 + 10 + (cur - 'A');
3561 NEXT;
3562 cur = CUR;
3563 }
3564 } else {
3565 ERROR("Char ref: expecting [0-9A-F]");
3566 return(-1);
3567 }
3568 } else {
3569 if ((cur >= '0') && (cur <= '9')) {
3570 while ((cur >= '0') && (cur <= '9')) {
3571 ret = ret * 10 + cur - '0';
3572 NEXT;
3573 cur = CUR;
3574 }
3575 } else {
3576 ERROR("Char ref: expecting [0-9]");
3577 return(-1);
3578 }
3579 }
3580 if (cur != ';') {
3581 ERROR("Char ref: expecting ';'");
3582 return(-1);
3583 } else {
3584 NEXT;
3585 }
3586 return(ret);
3587}
3588
3589/**
3590 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003591 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003592 *
3593 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3594 * [18] seRange ::= charOrEsc '-' charOrEsc
3595 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3596 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3597 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3598 */
3599static void
3600xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00003601 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00003602 int start = -1;
3603 int end = -1;
3604
3605 if ((CUR == '&') && (NXT(1) == '#')) {
3606 end = start = xmlFAParseCharRef(ctxt);
3607 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3608 XML_REGEXP_CHARVAL, start, end, NULL);
3609 return;
3610 }
3611 cur = CUR;
3612 if (cur == '\\') {
3613 NEXT;
3614 cur = CUR;
3615 switch (cur) {
3616 case 'n': start = 0xA; break;
3617 case 'r': start = 0xD; break;
3618 case 't': start = 0x9; break;
3619 case '\\': case '|': case '.': case '-': case '^': case '?':
3620 case '*': case '+': case '{': case '}': case '(': case ')':
3621 case '[': case ']':
3622 start = cur; break;
3623 default:
3624 ERROR("Invalid escape value");
3625 return;
3626 }
3627 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00003628 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003629 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003630 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003631 } else {
3632 ERROR("Expecting a char range");
3633 return;
3634 }
William M. Brackdc99df92003-12-27 01:54:25 +00003635 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003636 if (start == '-') {
3637 return;
3638 }
3639 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00003640 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003641 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3642 XML_REGEXP_CHARVAL, start, end, NULL);
3643 return;
3644 }
3645 NEXT;
3646 cur = CUR;
3647 if (cur == '\\') {
3648 NEXT;
3649 cur = CUR;
3650 switch (cur) {
3651 case 'n': end = 0xA; break;
3652 case 'r': end = 0xD; break;
3653 case 't': end = 0x9; break;
3654 case '\\': case '|': case '.': case '-': case '^': case '?':
3655 case '*': case '+': case '{': case '}': case '(': case ')':
3656 case '[': case ']':
3657 end = cur; break;
3658 default:
3659 ERROR("Invalid escape value");
3660 return;
3661 }
William M. Brackdc99df92003-12-27 01:54:25 +00003662 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003663 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003664 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003665 } else {
3666 ERROR("Expecting the end of a char range");
3667 return;
3668 }
William M. Brackdc99df92003-12-27 01:54:25 +00003669 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003670 /* TODO check that the values are acceptable character ranges for XML */
3671 if (end < start) {
3672 ERROR("End of range is before start of range");
3673 } else {
3674 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3675 XML_REGEXP_CHARVAL, start, end, NULL);
3676 }
3677 return;
3678}
3679
3680/**
3681 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003682 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003683 *
3684 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3685 */
3686static void
3687xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3688 do {
3689 if ((CUR == '\\') || (CUR == '.')) {
3690 xmlFAParseCharClassEsc(ctxt);
3691 } else {
3692 xmlFAParseCharRange(ctxt);
3693 }
3694 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3695 (ctxt->error == 0));
3696}
3697
3698/**
3699 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003700 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003701 *
3702 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3703 * [15] negCharGroup ::= '^' posCharGroup
3704 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3705 * [12] charClassExpr ::= '[' charGroup ']'
3706 */
3707static void
3708xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3709 int n = ctxt->neg;
3710 while ((CUR != ']') && (ctxt->error == 0)) {
3711 if (CUR == '^') {
3712 int neg = ctxt->neg;
3713
3714 NEXT;
3715 ctxt->neg = !ctxt->neg;
3716 xmlFAParsePosCharGroup(ctxt);
3717 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00003718 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003719 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003720 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00003721 NEXT; /* eat the '-' */
3722 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00003723 xmlFAParseCharGroup(ctxt);
3724 if (CUR == ']') {
3725 NEXT;
3726 } else {
3727 ERROR("charClassExpr: ']' expected");
3728 break;
3729 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003730 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00003731 break;
3732 } else if (CUR != ']') {
3733 xmlFAParsePosCharGroup(ctxt);
3734 }
3735 }
3736 ctxt->neg = n;
3737}
3738
3739/**
3740 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003741 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003742 *
3743 * [11] charClass ::= charClassEsc | charClassExpr
3744 * [12] charClassExpr ::= '[' charGroup ']'
3745 */
3746static void
3747xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3748 if (CUR == '[') {
3749 NEXT;
3750 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3751 if (ctxt->atom == NULL)
3752 return;
3753 xmlFAParseCharGroup(ctxt);
3754 if (CUR == ']') {
3755 NEXT;
3756 } else {
3757 ERROR("xmlFAParseCharClass: ']' expected");
3758 }
3759 } else {
3760 xmlFAParseCharClassEsc(ctxt);
3761 }
3762}
3763
3764/**
3765 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003766 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003767 *
3768 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003769 *
3770 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003771 */
3772static int
3773xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3774 int ret = 0;
3775 int ok = 0;
3776
3777 while ((CUR >= '0') && (CUR <= '9')) {
3778 ret = ret * 10 + (CUR - '0');
3779 ok = 1;
3780 NEXT;
3781 }
3782 if (ok != 1) {
3783 return(-1);
3784 }
3785 return(ret);
3786}
3787
3788/**
3789 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003790 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003791 *
3792 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3793 * [5] quantity ::= quantRange | quantMin | QuantExact
3794 * [6] quantRange ::= QuantExact ',' QuantExact
3795 * [7] quantMin ::= QuantExact ','
3796 * [8] QuantExact ::= [0-9]+
3797 */
3798static int
3799xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3800 int cur;
3801
3802 cur = CUR;
3803 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3804 if (ctxt->atom != NULL) {
3805 if (cur == '?')
3806 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3807 else if (cur == '*')
3808 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3809 else if (cur == '+')
3810 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3811 }
3812 NEXT;
3813 return(1);
3814 }
3815 if (cur == '{') {
3816 int min = 0, max = 0;
3817
3818 NEXT;
3819 cur = xmlFAParseQuantExact(ctxt);
3820 if (cur >= 0)
3821 min = cur;
3822 if (CUR == ',') {
3823 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00003824 if (CUR == '}')
3825 max = INT_MAX;
3826 else {
3827 cur = xmlFAParseQuantExact(ctxt);
3828 if (cur >= 0)
3829 max = cur;
3830 else {
3831 ERROR("Improper quantifier");
3832 }
3833 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003834 }
3835 if (CUR == '}') {
3836 NEXT;
3837 } else {
3838 ERROR("Unterminated quantifier");
3839 }
3840 if (max == 0)
3841 max = min;
3842 if (ctxt->atom != NULL) {
3843 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3844 ctxt->atom->min = min;
3845 ctxt->atom->max = max;
3846 }
3847 return(1);
3848 }
3849 return(0);
3850}
3851
3852/**
3853 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003854 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003855 *
3856 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3857 */
3858static int
3859xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3860 int codepoint, len;
3861
3862 codepoint = xmlFAIsChar(ctxt);
3863 if (codepoint > 0) {
3864 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3865 if (ctxt->atom == NULL)
3866 return(-1);
3867 codepoint = CUR_SCHAR(ctxt->cur, len);
3868 ctxt->atom->codepoint = codepoint;
3869 NEXTL(len);
3870 return(1);
3871 } else if (CUR == '|') {
3872 return(0);
3873 } else if (CUR == 0) {
3874 return(0);
3875 } else if (CUR == ')') {
3876 return(0);
3877 } else if (CUR == '(') {
3878 xmlRegStatePtr start, oldend;
3879
3880 NEXT;
3881 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3882 start = ctxt->state;
3883 oldend = ctxt->end;
3884 ctxt->end = NULL;
3885 ctxt->atom = NULL;
3886 xmlFAParseRegExp(ctxt, 0);
3887 if (CUR == ')') {
3888 NEXT;
3889 } else {
3890 ERROR("xmlFAParseAtom: expecting ')'");
3891 }
3892 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3893 if (ctxt->atom == NULL)
3894 return(-1);
3895 ctxt->atom->start = start;
3896 ctxt->atom->stop = ctxt->state;
3897 ctxt->end = oldend;
3898 return(1);
3899 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3900 xmlFAParseCharClass(ctxt);
3901 return(1);
3902 }
3903 return(0);
3904}
3905
3906/**
3907 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003908 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003909 *
3910 * [3] piece ::= atom quantifier?
3911 */
3912static int
3913xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3914 int ret;
3915
3916 ctxt->atom = NULL;
3917 ret = xmlFAParseAtom(ctxt);
3918 if (ret == 0)
3919 return(0);
3920 if (ctxt->atom == NULL) {
3921 ERROR("internal: no atom generated");
3922 }
3923 xmlFAParseQuantifier(ctxt);
3924 return(1);
3925}
3926
3927/**
3928 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003929 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003930 *
3931 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003932 8
Daniel Veillard4255d502002-04-16 15:50:10 +00003933 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003934static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003935xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003936 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00003937 int ret;
3938
3939 previous = ctxt->state;
3940 ret = xmlFAParsePiece(ctxt);
3941 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003942 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
3943 return(-1);
3944 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00003945 ctxt->atom = NULL;
3946 }
3947 while ((ret != 0) && (ctxt->error == 0)) {
3948 ret = xmlFAParsePiece(ctxt);
3949 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003950 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3951 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003952 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003953 previous = ctxt->state;
3954 ctxt->atom = NULL;
3955 }
3956 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003957 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00003958}
3959
3960/**
3961 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003962 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00003963 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003964 *
3965 * [1] regExp ::= branch ( '|' branch )*
3966 */
3967static void
3968xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003969 xmlRegStatePtr start, end, oldend, oldstart;
Daniel Veillard4255d502002-04-16 15:50:10 +00003970
3971 oldend = ctxt->end;
3972
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003973 oldstart = ctxt->state;
3974 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00003975 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003976 ctxt->end = NULL;
3977 xmlFAParseBranch(ctxt);
3978 if (top) {
3979#ifdef DEBUG_REGEXP_GRAPH
3980 printf("State %d is final\n", ctxt->state->no);
3981#endif
3982 ctxt->state->type = XML_REGEXP_FINAL_STATE;
3983 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003984 if (CUR != '|') {
3985 ctxt->end = ctxt->state;
3986 return;
3987 }
3988 end = ctxt->state;
3989 while ((CUR == '|') && (ctxt->error == 0)) {
3990 NEXT;
3991 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003992 ctxt->end = NULL;
3993 xmlFAParseBranch(ctxt);
3994 if (top) {
3995 ctxt->state->type = XML_REGEXP_FINAL_STATE;
3996#ifdef DEBUG_REGEXP_GRAPH
3997 printf("State %d is final\n", ctxt->state->no);
3998#endif
3999 } else {
4000 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4001 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004002 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004003 if (!top) {
4004 ctxt->state = end;
4005 ctxt->end = end;
4006 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004007}
4008
4009/************************************************************************
4010 * *
4011 * The basic API *
4012 * *
4013 ************************************************************************/
4014
4015/**
4016 * xmlRegexpPrint:
4017 * @output: the file for the output debug
4018 * @regexp: the compiled regexp
4019 *
4020 * Print the content of the compiled regular expression
4021 */
4022void
4023xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4024 int i;
4025
4026 fprintf(output, " regexp: ");
4027 if (regexp == NULL) {
4028 fprintf(output, "NULL\n");
4029 return;
4030 }
4031 fprintf(output, "'%s' ", regexp->string);
4032 fprintf(output, "\n");
4033 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4034 for (i = 0;i < regexp->nbAtoms; i++) {
4035 fprintf(output, " %02d ", i);
4036 xmlRegPrintAtom(output, regexp->atoms[i]);
4037 }
4038 fprintf(output, "%d states:", regexp->nbStates);
4039 fprintf(output, "\n");
4040 for (i = 0;i < regexp->nbStates; i++) {
4041 xmlRegPrintState(output, regexp->states[i]);
4042 }
4043 fprintf(output, "%d counters:\n", regexp->nbCounters);
4044 for (i = 0;i < regexp->nbCounters; i++) {
4045 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4046 regexp->counters[i].max);
4047 }
4048}
4049
4050/**
4051 * xmlRegexpCompile:
4052 * @regexp: a regular expression string
4053 *
4054 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004055 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004056 * that regular expression
4057 *
4058 * Returns the compiled expression or NULL in case of error
4059 */
4060xmlRegexpPtr
4061xmlRegexpCompile(const xmlChar *regexp) {
4062 xmlRegexpPtr ret;
4063 xmlRegParserCtxtPtr ctxt;
4064
4065 ctxt = xmlRegNewParserCtxt(regexp);
4066 if (ctxt == NULL)
4067 return(NULL);
4068
4069 /* initialize the parser */
4070 ctxt->end = NULL;
4071 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4072 xmlRegStatePush(ctxt, ctxt->start);
4073
4074 /* parse the expression building an automata */
4075 xmlFAParseRegExp(ctxt, 1);
4076 if (CUR != 0) {
4077 ERROR("xmlFAParseRegExp: extra characters");
4078 }
4079 ctxt->end = ctxt->state;
4080 ctxt->start->type = XML_REGEXP_START_STATE;
4081 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4082
4083 /* remove the Epsilon except for counted transitions */
4084 xmlFAEliminateEpsilonTransitions(ctxt);
4085
4086
4087 if (ctxt->error != 0) {
4088 xmlRegFreeParserCtxt(ctxt);
4089 return(NULL);
4090 }
4091 ret = xmlRegEpxFromParse(ctxt);
4092 xmlRegFreeParserCtxt(ctxt);
4093 return(ret);
4094}
4095
4096/**
4097 * xmlRegexpExec:
4098 * @comp: the compiled regular expression
4099 * @content: the value to check against the regular expression
4100 *
William M. Brackddf71d62004-05-06 04:17:26 +00004101 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004102 *
William M. Brackddf71d62004-05-06 04:17:26 +00004103 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004104 */
4105int
4106xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4107 if ((comp == NULL) || (content == NULL))
4108 return(-1);
4109 return(xmlFARegExec(comp, content));
4110}
4111
4112/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004113 * xmlRegexpIsDeterminist:
4114 * @comp: the compiled regular expression
4115 *
4116 * Check if the regular expression is determinist
4117 *
William M. Brackddf71d62004-05-06 04:17:26 +00004118 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004119 */
4120int
4121xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4122 xmlAutomataPtr am;
4123 int ret;
4124
4125 if (comp == NULL)
4126 return(-1);
4127 if (comp->determinist != -1)
4128 return(comp->determinist);
4129
4130 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004131 if (am->states != NULL) {
4132 int i;
4133
4134 for (i = 0;i < am->nbStates;i++)
4135 xmlRegFreeState(am->states[i]);
4136 xmlFree(am->states);
4137 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004138 am->nbAtoms = comp->nbAtoms;
4139 am->atoms = comp->atoms;
4140 am->nbStates = comp->nbStates;
4141 am->states = comp->states;
4142 am->determinist = -1;
4143 ret = xmlFAComputesDeterminism(am);
4144 am->atoms = NULL;
4145 am->states = NULL;
4146 xmlFreeAutomata(am);
4147 return(ret);
4148}
4149
4150/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004151 * xmlRegFreeRegexp:
4152 * @regexp: the regexp
4153 *
4154 * Free a regexp
4155 */
4156void
4157xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4158 int i;
4159 if (regexp == NULL)
4160 return;
4161
4162 if (regexp->string != NULL)
4163 xmlFree(regexp->string);
4164 if (regexp->states != NULL) {
4165 for (i = 0;i < regexp->nbStates;i++)
4166 xmlRegFreeState(regexp->states[i]);
4167 xmlFree(regexp->states);
4168 }
4169 if (regexp->atoms != NULL) {
4170 for (i = 0;i < regexp->nbAtoms;i++)
4171 xmlRegFreeAtom(regexp->atoms[i]);
4172 xmlFree(regexp->atoms);
4173 }
4174 if (regexp->counters != NULL)
4175 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004176 if (regexp->compact != NULL)
4177 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004178 if (regexp->transdata != NULL)
4179 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004180 if (regexp->stringMap != NULL) {
4181 for (i = 0; i < regexp->nbstrings;i++)
4182 xmlFree(regexp->stringMap[i]);
4183 xmlFree(regexp->stringMap);
4184 }
4185
Daniel Veillard4255d502002-04-16 15:50:10 +00004186 xmlFree(regexp);
4187}
4188
4189#ifdef LIBXML_AUTOMATA_ENABLED
4190/************************************************************************
4191 * *
4192 * The Automata interface *
4193 * *
4194 ************************************************************************/
4195
4196/**
4197 * xmlNewAutomata:
4198 *
4199 * Create a new automata
4200 *
4201 * Returns the new object or NULL in case of failure
4202 */
4203xmlAutomataPtr
4204xmlNewAutomata(void) {
4205 xmlAutomataPtr ctxt;
4206
4207 ctxt = xmlRegNewParserCtxt(NULL);
4208 if (ctxt == NULL)
4209 return(NULL);
4210
4211 /* initialize the parser */
4212 ctxt->end = NULL;
4213 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004214 if (ctxt->start == NULL) {
4215 xmlFreeAutomata(ctxt);
4216 return(NULL);
4217 }
4218 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4219 xmlRegFreeState(ctxt->start);
4220 xmlFreeAutomata(ctxt);
4221 return(NULL);
4222 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004223
4224 return(ctxt);
4225}
4226
4227/**
4228 * xmlFreeAutomata:
4229 * @am: an automata
4230 *
4231 * Free an automata
4232 */
4233void
4234xmlFreeAutomata(xmlAutomataPtr am) {
4235 if (am == NULL)
4236 return;
4237 xmlRegFreeParserCtxt(am);
4238}
4239
4240/**
4241 * xmlAutomataGetInitState:
4242 * @am: an automata
4243 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004244 * Initial state lookup
4245 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004246 * Returns the initial state of the automata
4247 */
4248xmlAutomataStatePtr
4249xmlAutomataGetInitState(xmlAutomataPtr am) {
4250 if (am == NULL)
4251 return(NULL);
4252 return(am->start);
4253}
4254
4255/**
4256 * xmlAutomataSetFinalState:
4257 * @am: an automata
4258 * @state: a state in this automata
4259 *
4260 * Makes that state a final state
4261 *
4262 * Returns 0 or -1 in case of error
4263 */
4264int
4265xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4266 if ((am == NULL) || (state == NULL))
4267 return(-1);
4268 state->type = XML_REGEXP_FINAL_STATE;
4269 return(0);
4270}
4271
4272/**
4273 * xmlAutomataNewTransition:
4274 * @am: an automata
4275 * @from: the starting point of the transition
4276 * @to: the target point of the transition or NULL
4277 * @token: the input string associated to that transition
4278 * @data: data passed to the callback function if the transition is activated
4279 *
William M. Brackddf71d62004-05-06 04:17:26 +00004280 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004281 * and then adds a transition from the @from state to the target state
4282 * activated by the value of @token
4283 *
4284 * Returns the target state or NULL in case of error
4285 */
4286xmlAutomataStatePtr
4287xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4288 xmlAutomataStatePtr to, const xmlChar *token,
4289 void *data) {
4290 xmlRegAtomPtr atom;
4291
4292 if ((am == NULL) || (from == NULL) || (token == NULL))
4293 return(NULL);
4294 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004295 if (atom == NULL)
4296 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004297 atom->data = data;
4298 if (atom == NULL)
4299 return(NULL);
4300 atom->valuep = xmlStrdup(token);
4301
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004302 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4303 xmlRegFreeAtom(atom);
4304 return(NULL);
4305 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004306 if (to == NULL)
4307 return(am->state);
4308 return(to);
4309}
4310
4311/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004312 * xmlAutomataNewTransition2:
4313 * @am: an automata
4314 * @from: the starting point of the transition
4315 * @to: the target point of the transition or NULL
4316 * @token: the first input string associated to that transition
4317 * @token2: the second input string associated to that transition
4318 * @data: data passed to the callback function if the transition is activated
4319 *
William M. Brackddf71d62004-05-06 04:17:26 +00004320 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00004321 * and then adds a transition from the @from state to the target state
4322 * activated by the value of @token
4323 *
4324 * Returns the target state or NULL in case of error
4325 */
4326xmlAutomataStatePtr
4327xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4328 xmlAutomataStatePtr to, const xmlChar *token,
4329 const xmlChar *token2, void *data) {
4330 xmlRegAtomPtr atom;
4331
4332 if ((am == NULL) || (from == NULL) || (token == NULL))
4333 return(NULL);
4334 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4335 atom->data = data;
4336 if (atom == NULL)
4337 return(NULL);
4338 if ((token2 == NULL) || (*token2 == 0)) {
4339 atom->valuep = xmlStrdup(token);
4340 } else {
4341 int lenn, lenp;
4342 xmlChar *str;
4343
4344 lenn = strlen((char *) token2);
4345 lenp = strlen((char *) token);
4346
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004347 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004348 if (str == NULL) {
4349 xmlRegFreeAtom(atom);
4350 return(NULL);
4351 }
4352 memcpy(&str[0], token, lenp);
4353 str[lenp] = '|';
4354 memcpy(&str[lenp + 1], token2, lenn);
4355 str[lenn + lenp + 1] = 0;
4356
4357 atom->valuep = str;
4358 }
4359
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004360 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4361 xmlRegFreeAtom(atom);
4362 return(NULL);
4363 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004364 if (to == NULL)
4365 return(am->state);
4366 return(to);
4367}
4368
4369/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004370 * xmlAutomataNewCountTrans:
4371 * @am: an automata
4372 * @from: the starting point of the transition
4373 * @to: the target point of the transition or NULL
4374 * @token: the input string associated to that transition
4375 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004376 * @max: the maximum successive occurences of token
4377 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004378 *
William M. Brackddf71d62004-05-06 04:17:26 +00004379 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004380 * and then adds a transition from the @from state to the target state
4381 * activated by a succession of input of value @token and whose number
4382 * is between @min and @max
4383 *
4384 * Returns the target state or NULL in case of error
4385 */
4386xmlAutomataStatePtr
4387xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4388 xmlAutomataStatePtr to, const xmlChar *token,
4389 int min, int max, void *data) {
4390 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004391 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00004392
4393 if ((am == NULL) || (from == NULL) || (token == NULL))
4394 return(NULL);
4395 if (min < 0)
4396 return(NULL);
4397 if ((max < min) || (max < 1))
4398 return(NULL);
4399 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4400 if (atom == NULL)
4401 return(NULL);
4402 atom->valuep = xmlStrdup(token);
4403 atom->data = data;
4404 if (min == 0)
4405 atom->min = 1;
4406 else
4407 atom->min = min;
4408 atom->max = max;
4409
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004410 /*
4411 * associate a counter to the transition.
4412 */
4413 counter = xmlRegGetCounter(am);
4414 am->counters[counter].min = min;
4415 am->counters[counter].max = max;
4416
4417 /* xmlFAGenerateTransitions(am, from, to, atom); */
4418 if (to == NULL) {
4419 to = xmlRegNewState(am);
4420 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004421 }
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004422 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4423 xmlRegAtomPush(am, atom);
4424 am->state = to;
4425
Daniel Veillard4255d502002-04-16 15:50:10 +00004426 if (to == NULL)
4427 to = am->state;
4428 if (to == NULL)
4429 return(NULL);
4430 if (min == 0)
4431 xmlFAGenerateEpsilonTransition(am, from, to);
4432 return(to);
4433}
4434
4435/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004436 * xmlAutomataNewOnceTrans:
4437 * @am: an automata
4438 * @from: the starting point of the transition
4439 * @to: the target point of the transition or NULL
4440 * @token: the input string associated to that transition
4441 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004442 * @max: the maximum successive occurences of token
4443 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00004444 *
William M. Brackddf71d62004-05-06 04:17:26 +00004445 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00004446 * and then adds a transition from the @from state to the target state
4447 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00004448 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00004449 * once.
4450 *
4451 * Returns the target state or NULL in case of error
4452 */
4453xmlAutomataStatePtr
4454xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4455 xmlAutomataStatePtr to, const xmlChar *token,
4456 int min, int max, void *data) {
4457 xmlRegAtomPtr atom;
4458 int counter;
4459
4460 if ((am == NULL) || (from == NULL) || (token == NULL))
4461 return(NULL);
4462 if (min < 1)
4463 return(NULL);
4464 if ((max < min) || (max < 1))
4465 return(NULL);
4466 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4467 if (atom == NULL)
4468 return(NULL);
4469 atom->valuep = xmlStrdup(token);
4470 atom->data = data;
4471 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4472 if (min == 0)
4473 atom->min = 1;
4474 else
4475 atom->min = min;
4476 atom->max = max;
4477 /*
4478 * associate a counter to the transition.
4479 */
4480 counter = xmlRegGetCounter(am);
4481 am->counters[counter].min = 1;
4482 am->counters[counter].max = 1;
4483
4484 /* xmlFAGenerateTransitions(am, from, to, atom); */
4485 if (to == NULL) {
4486 to = xmlRegNewState(am);
4487 xmlRegStatePush(am, to);
4488 }
4489 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4490 xmlRegAtomPush(am, atom);
4491 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00004492 return(to);
4493}
4494
4495/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004496 * xmlAutomataNewState:
4497 * @am: an automata
4498 *
4499 * Create a new disconnected state in the automata
4500 *
4501 * Returns the new state or NULL in case of error
4502 */
4503xmlAutomataStatePtr
4504xmlAutomataNewState(xmlAutomataPtr am) {
4505 xmlAutomataStatePtr to;
4506
4507 if (am == NULL)
4508 return(NULL);
4509 to = xmlRegNewState(am);
4510 xmlRegStatePush(am, to);
4511 return(to);
4512}
4513
4514/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004515 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004516 * @am: an automata
4517 * @from: the starting point of the transition
4518 * @to: the target point of the transition or NULL
4519 *
William M. Brackddf71d62004-05-06 04:17:26 +00004520 * If @to is NULL, this creates first a new target state in the automata
4521 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00004522 * target state
4523 *
4524 * Returns the target state or NULL in case of error
4525 */
4526xmlAutomataStatePtr
4527xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4528 xmlAutomataStatePtr to) {
4529 if ((am == NULL) || (from == NULL))
4530 return(NULL);
4531 xmlFAGenerateEpsilonTransition(am, from, to);
4532 if (to == NULL)
4533 return(am->state);
4534 return(to);
4535}
4536
Daniel Veillardb509f152002-04-17 16:28:10 +00004537/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004538 * xmlAutomataNewAllTrans:
4539 * @am: an automata
4540 * @from: the starting point of the transition
4541 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004542 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004543 *
William M. Brackddf71d62004-05-06 04:17:26 +00004544 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00004545 * and then adds a an ALL transition from the @from state to the
4546 * target state. That transition is an epsilon transition allowed only when
4547 * all transitions from the @from node have been activated.
4548 *
4549 * Returns the target state or NULL in case of error
4550 */
4551xmlAutomataStatePtr
4552xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004553 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004554 if ((am == NULL) || (from == NULL))
4555 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004556 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004557 if (to == NULL)
4558 return(am->state);
4559 return(to);
4560}
4561
4562/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004563 * xmlAutomataNewCounter:
4564 * @am: an automata
4565 * @min: the minimal value on the counter
4566 * @max: the maximal value on the counter
4567 *
4568 * Create a new counter
4569 *
4570 * Returns the counter number or -1 in case of error
4571 */
4572int
4573xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4574 int ret;
4575
4576 if (am == NULL)
4577 return(-1);
4578
4579 ret = xmlRegGetCounter(am);
4580 if (ret < 0)
4581 return(-1);
4582 am->counters[ret].min = min;
4583 am->counters[ret].max = max;
4584 return(ret);
4585}
4586
4587/**
4588 * xmlAutomataNewCountedTrans:
4589 * @am: an automata
4590 * @from: the starting point of the transition
4591 * @to: the target point of the transition or NULL
4592 * @counter: the counter associated to that transition
4593 *
William M. Brackddf71d62004-05-06 04:17:26 +00004594 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00004595 * and then adds an epsilon transition from the @from state to the target state
4596 * which will increment the counter provided
4597 *
4598 * Returns the target state or NULL in case of error
4599 */
4600xmlAutomataStatePtr
4601xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4602 xmlAutomataStatePtr to, int counter) {
4603 if ((am == NULL) || (from == NULL) || (counter < 0))
4604 return(NULL);
4605 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4606 if (to == NULL)
4607 return(am->state);
4608 return(to);
4609}
4610
4611/**
4612 * xmlAutomataNewCounterTrans:
4613 * @am: an automata
4614 * @from: the starting point of the transition
4615 * @to: the target point of the transition or NULL
4616 * @counter: the counter associated to that transition
4617 *
William M. Brackddf71d62004-05-06 04:17:26 +00004618 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00004619 * and then adds an epsilon transition from the @from state to the target state
4620 * which will be allowed only if the counter is within the right range.
4621 *
4622 * Returns the target state or NULL in case of error
4623 */
4624xmlAutomataStatePtr
4625xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4626 xmlAutomataStatePtr to, int counter) {
4627 if ((am == NULL) || (from == NULL) || (counter < 0))
4628 return(NULL);
4629 xmlFAGenerateCountedTransition(am, from, to, counter);
4630 if (to == NULL)
4631 return(am->state);
4632 return(to);
4633}
Daniel Veillard4255d502002-04-16 15:50:10 +00004634
4635/**
4636 * xmlAutomataCompile:
4637 * @am: an automata
4638 *
4639 * Compile the automata into a Reg Exp ready for being executed.
4640 * The automata should be free after this point.
4641 *
4642 * Returns the compiled regexp or NULL in case of error
4643 */
4644xmlRegexpPtr
4645xmlAutomataCompile(xmlAutomataPtr am) {
4646 xmlRegexpPtr ret;
4647
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004648 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004649 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004650 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004651 ret = xmlRegEpxFromParse(am);
4652
4653 return(ret);
4654}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004655
4656/**
4657 * xmlAutomataIsDeterminist:
4658 * @am: an automata
4659 *
4660 * Checks if an automata is determinist.
4661 *
4662 * Returns 1 if true, 0 if not, and -1 in case of error
4663 */
4664int
4665xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4666 int ret;
4667
4668 if (am == NULL)
4669 return(-1);
4670
4671 ret = xmlFAComputesDeterminism(am);
4672 return(ret);
4673}
Daniel Veillard4255d502002-04-16 15:50:10 +00004674#endif /* LIBXML_AUTOMATA_ENABLED */
4675#endif /* LIBXML_REGEXP_ENABLED */