blob: a26b8bb175d45570ed48730c3903bc27f3345b23 [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
6 * XML related specifications thise includes:
7 * - XML-1.0 DTD validation
8 * - XML Schemas structure part 1
9 * - XML Schemas Datatypes part 2 especially Appendix F
10 * - RELAX-NG/TREX i.e. the counter proposal
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>
24#include <libxml/tree.h>
25#include <libxml/parserInternals.h>
26#include <libxml/xmlregexp.h>
27#include <libxml/xmlautomata.h>
28#include <libxml/xmlunicode.h>
29
30/* #define DEBUG_REGEXP_GRAPH */
31/* #define DEBUG_REGEXP_EXEC */
32/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000033/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000034
Daniel Veillardff46a042003-10-08 08:53:17 +000035#define ERROR(str) \
36 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
37 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000038#define NEXT ctxt->cur++
39#define CUR (*(ctxt->cur))
40#define NXT(index) (ctxt->cur[index])
41
42#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
43#define NEXTL(l) ctxt->cur += l;
44
Daniel Veillarde19fc232002-04-22 16:01:24 +000045/**
46 * TODO:
47 *
48 * macro to flag unimplemented blocks
49 */
50#define TODO \
51 xmlGenericError(xmlGenericErrorContext, \
52 "Unimplemented block at %s:%d\n", \
53 __FILE__, __LINE__);
54
Daniel Veillard4255d502002-04-16 15:50:10 +000055/************************************************************************
56 * *
57 * Datatypes and structures *
58 * *
59 ************************************************************************/
60
61typedef enum {
62 XML_REGEXP_EPSILON = 1,
63 XML_REGEXP_CHARVAL,
64 XML_REGEXP_RANGES,
65 XML_REGEXP_SUBREG,
66 XML_REGEXP_STRING,
67 XML_REGEXP_ANYCHAR, /* . */
68 XML_REGEXP_ANYSPACE, /* \s */
69 XML_REGEXP_NOTSPACE, /* \S */
70 XML_REGEXP_INITNAME, /* \l */
71 XML_REGEXP_NOTINITNAME, /* \l */
72 XML_REGEXP_NAMECHAR, /* \c */
73 XML_REGEXP_NOTNAMECHAR, /* \C */
74 XML_REGEXP_DECIMAL, /* \d */
75 XML_REGEXP_NOTDECIMAL, /* \d */
76 XML_REGEXP_REALCHAR, /* \w */
77 XML_REGEXP_NOTREALCHAR, /* \w */
78 XML_REGEXP_LETTER,
79 XML_REGEXP_LETTER_UPPERCASE,
80 XML_REGEXP_LETTER_LOWERCASE,
81 XML_REGEXP_LETTER_TITLECASE,
82 XML_REGEXP_LETTER_MODIFIER,
83 XML_REGEXP_LETTER_OTHERS,
84 XML_REGEXP_MARK,
85 XML_REGEXP_MARK_NONSPACING,
86 XML_REGEXP_MARK_SPACECOMBINING,
87 XML_REGEXP_MARK_ENCLOSING,
88 XML_REGEXP_NUMBER,
89 XML_REGEXP_NUMBER_DECIMAL,
90 XML_REGEXP_NUMBER_LETTER,
91 XML_REGEXP_NUMBER_OTHERS,
92 XML_REGEXP_PUNCT,
93 XML_REGEXP_PUNCT_CONNECTOR,
94 XML_REGEXP_PUNCT_DASH,
95 XML_REGEXP_PUNCT_OPEN,
96 XML_REGEXP_PUNCT_CLOSE,
97 XML_REGEXP_PUNCT_INITQUOTE,
98 XML_REGEXP_PUNCT_FINQUOTE,
99 XML_REGEXP_PUNCT_OTHERS,
100 XML_REGEXP_SEPAR,
101 XML_REGEXP_SEPAR_SPACE,
102 XML_REGEXP_SEPAR_LINE,
103 XML_REGEXP_SEPAR_PARA,
104 XML_REGEXP_SYMBOL,
105 XML_REGEXP_SYMBOL_MATH,
106 XML_REGEXP_SYMBOL_CURRENCY,
107 XML_REGEXP_SYMBOL_MODIFIER,
108 XML_REGEXP_SYMBOL_OTHERS,
109 XML_REGEXP_OTHER,
110 XML_REGEXP_OTHER_CONTROL,
111 XML_REGEXP_OTHER_FORMAT,
112 XML_REGEXP_OTHER_PRIVATE,
113 XML_REGEXP_OTHER_NA,
114 XML_REGEXP_BLOCK_NAME
115} xmlRegAtomType;
116
117typedef enum {
118 XML_REGEXP_QUANT_EPSILON = 1,
119 XML_REGEXP_QUANT_ONCE,
120 XML_REGEXP_QUANT_OPT,
121 XML_REGEXP_QUANT_MULT,
122 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000123 XML_REGEXP_QUANT_ONCEONLY,
124 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000125 XML_REGEXP_QUANT_RANGE
126} xmlRegQuantType;
127
128typedef enum {
129 XML_REGEXP_START_STATE = 1,
130 XML_REGEXP_FINAL_STATE,
131 XML_REGEXP_TRANS_STATE
132} xmlRegStateType;
133
134typedef enum {
135 XML_REGEXP_MARK_NORMAL = 0,
136 XML_REGEXP_MARK_START,
137 XML_REGEXP_MARK_VISITED
138} xmlRegMarkedType;
139
140typedef struct _xmlRegRange xmlRegRange;
141typedef xmlRegRange *xmlRegRangePtr;
142
143struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000144 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000145 xmlRegAtomType type;
146 int start;
147 int end;
148 xmlChar *blockName;
149};
150
151typedef struct _xmlRegAtom xmlRegAtom;
152typedef xmlRegAtom *xmlRegAtomPtr;
153
154typedef struct _xmlAutomataState xmlRegState;
155typedef xmlRegState *xmlRegStatePtr;
156
157struct _xmlRegAtom {
158 int no;
159 xmlRegAtomType type;
160 xmlRegQuantType quant;
161 int min;
162 int max;
163
164 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000165 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000166 int neg;
167 int codepoint;
168 xmlRegStatePtr start;
169 xmlRegStatePtr stop;
170 int maxRanges;
171 int nbRanges;
172 xmlRegRangePtr *ranges;
173 void *data;
174};
175
176typedef struct _xmlRegCounter xmlRegCounter;
177typedef xmlRegCounter *xmlRegCounterPtr;
178
179struct _xmlRegCounter {
180 int min;
181 int max;
182};
183
184typedef struct _xmlRegTrans xmlRegTrans;
185typedef xmlRegTrans *xmlRegTransPtr;
186
187struct _xmlRegTrans {
188 xmlRegAtomPtr atom;
189 int to;
190 int counter;
191 int count;
192};
193
194struct _xmlAutomataState {
195 xmlRegStateType type;
196 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000197 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000198 int no;
199
200 int maxTrans;
201 int nbTrans;
202 xmlRegTrans *trans;
203};
204
205typedef struct _xmlAutomata xmlRegParserCtxt;
206typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
207
208struct _xmlAutomata {
209 xmlChar *string;
210 xmlChar *cur;
211
212 int error;
213 int neg;
214
215 xmlRegStatePtr start;
216 xmlRegStatePtr end;
217 xmlRegStatePtr state;
218
219 xmlRegAtomPtr atom;
220
221 int maxAtoms;
222 int nbAtoms;
223 xmlRegAtomPtr *atoms;
224
225 int maxStates;
226 int nbStates;
227 xmlRegStatePtr *states;
228
229 int maxCounters;
230 int nbCounters;
231 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000232
233 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000234};
235
236struct _xmlRegexp {
237 xmlChar *string;
238 int nbStates;
239 xmlRegStatePtr *states;
240 int nbAtoms;
241 xmlRegAtomPtr *atoms;
242 int nbCounters;
243 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000244 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000245 /*
246 * That's the compact form for determinists automatas
247 */
248 int nbstates;
249 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000250 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000251 int nbstrings;
252 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000253};
254
255typedef struct _xmlRegExecRollback xmlRegExecRollback;
256typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
257
258struct _xmlRegExecRollback {
259 xmlRegStatePtr state;/* the current state */
260 int index; /* the index in the input stack */
261 int nextbranch; /* the next transition to explore in that state */
262 int *counts; /* save the automate state if it has some */
263};
264
265typedef struct _xmlRegInputToken xmlRegInputToken;
266typedef xmlRegInputToken *xmlRegInputTokenPtr;
267
268struct _xmlRegInputToken {
269 xmlChar *value;
270 void *data;
271};
272
273struct _xmlRegExecCtxt {
274 int status; /* execution status != 0 indicate an error */
275 int determinist; /* did we found an inderterministic behaviour */
276 xmlRegexpPtr comp; /* the compiled regexp */
277 xmlRegExecCallbacks callback;
278 void *data;
279
280 xmlRegStatePtr state;/* the current state */
281 int transno; /* the current transition on that state */
282 int transcount; /* the number of char in char counted transitions */
283
284 /*
285 * A stack of rollback states
286 */
287 int maxRollbacks;
288 int nbRollbacks;
289 xmlRegExecRollback *rollbacks;
290
291 /*
292 * The state of the automata if any
293 */
294 int *counts;
295
296 /*
297 * The input stack
298 */
299 int inputStackMax;
300 int inputStackNr;
301 int index;
302 int *charStack;
303 const xmlChar *inputString; /* when operating on characters */
304 xmlRegInputTokenPtr inputStack;/* when operating on strings */
305
306};
307
Daniel Veillard441bc322002-04-20 17:38:48 +0000308#define REGEXP_ALL_COUNTER 0x123456
309#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000310
Daniel Veillard4255d502002-04-16 15:50:10 +0000311static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000312static void xmlRegFreeState(xmlRegStatePtr state);
313static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard4255d502002-04-16 15:50:10 +0000314
315/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000316 * *
317 * Regexp memory error handler *
318 * *
319 ************************************************************************/
320/**
321 * xmlRegexpErrMemory:
322 * @extra: extra informations
323 *
324 * Handle an out of memory condition
325 */
326static void
327xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
328{
329 const char *regexp = NULL;
330 if (ctxt != NULL) {
331 regexp = (const char *) ctxt->string;
332 ctxt->error = XML_ERR_NO_MEMORY;
333 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000334 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000335 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
336 regexp, NULL, 0, 0,
337 "Memory allocation failed : %s\n", extra);
338}
339
340/**
341 * xmlRegexpErrCompile:
342 * @extra: extra informations
343 *
344 * Handle an compilation failure
345 */
346static void
347xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
348{
349 const char *regexp = NULL;
350 int idx = 0;
351
352 if (ctxt != NULL) {
353 regexp = (const char *) ctxt->string;
354 idx = ctxt->cur - ctxt->string;
355 ctxt->error = XML_REGEXP_COMPILE_ERROR;
356 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000357 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000358 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
359 regexp, NULL, idx, 0,
360 "failed to compile: %s\n", extra);
361}
362
363/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000364 * *
365 * Allocation/Deallocation *
366 * *
367 ************************************************************************/
368
Daniel Veillard23e73572002-09-19 19:56:43 +0000369static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000370/**
371 * xmlRegEpxFromParse:
372 * @ctxt: the parser context used to build it
373 *
374 * Allocate a new regexp and fill it with the reult from the parser
375 *
376 * Returns the new regexp or NULL in case of error
377 */
378static xmlRegexpPtr
379xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
380 xmlRegexpPtr ret;
381
382 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000383 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000384 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000385 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000386 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000387 memset(ret, 0, sizeof(xmlRegexp));
388 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000389 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000390 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000391 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000392 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000393 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000394 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000395 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000396
397 if ((ret->determinist != 0) &&
398 (ret->nbCounters == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000399 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000400 (ret->atoms[0] != NULL) &&
401 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
402 int i, j, nbstates = 0, nbatoms = 0;
403 int *stateRemap;
404 int *stringRemap;
405 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000406 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000407 xmlChar **stringMap;
408 xmlChar *value;
409
410 /*
411 * Switch to a compact representation
412 * 1/ counting the effective number of states left
413 * 2/ conting the unique number of atoms, and check that
414 * they are all of the string type
415 * 3/ build a table state x atom for the transitions
416 */
417
418 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000419 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000420 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000421 xmlFree(ret);
422 return(NULL);
423 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000424 for (i = 0;i < ret->nbStates;i++) {
425 if (ret->states[i] != NULL) {
426 stateRemap[i] = nbstates;
427 nbstates++;
428 } else {
429 stateRemap[i] = -1;
430 }
431 }
432#ifdef DEBUG_COMPACTION
433 printf("Final: %d states\n", nbstates);
434#endif
435 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000436 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000437 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000438 xmlFree(stateRemap);
439 xmlFree(ret);
440 return(NULL);
441 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000442 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000443 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000444 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000445 xmlFree(stringMap);
446 xmlFree(stateRemap);
447 xmlFree(ret);
448 return(NULL);
449 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000450 for (i = 0;i < ret->nbAtoms;i++) {
451 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
452 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
453 value = ret->atoms[i]->valuep;
454 for (j = 0;j < nbatoms;j++) {
455 if (xmlStrEqual(stringMap[j], value)) {
456 stringRemap[i] = j;
457 break;
458 }
459 }
460 if (j >= nbatoms) {
461 stringRemap[i] = nbatoms;
462 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000463 if (stringMap[nbatoms] == NULL) {
464 for (i = 0;i < nbatoms;i++)
465 xmlFree(stringMap[i]);
466 xmlFree(stringRemap);
467 xmlFree(stringMap);
468 xmlFree(stateRemap);
469 xmlFree(ret);
470 return(NULL);
471 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000472 nbatoms++;
473 }
474 } else {
475 xmlFree(stateRemap);
476 xmlFree(stringRemap);
477 for (i = 0;i < nbatoms;i++)
478 xmlFree(stringMap[i]);
479 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000480 xmlFree(ret);
481 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000482 }
483 }
484#ifdef DEBUG_COMPACTION
485 printf("Final: %d atoms\n", nbatoms);
486#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000487 transitions = (int *) xmlMalloc((nbstates + 1) *
488 (nbatoms + 1) * sizeof(int));
489 if (transitions == NULL) {
490 xmlFree(stateRemap);
491 xmlFree(stringRemap);
492 xmlFree(stringMap);
493 xmlFree(ret);
494 return(NULL);
495 }
496 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000497
498 /*
499 * Allocate the transition table. The first entry for each
500 * state correspond to the state type.
501 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000502 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000503
504 for (i = 0;i < ret->nbStates;i++) {
505 int stateno, atomno, targetno, prev;
506 xmlRegStatePtr state;
507 xmlRegTransPtr trans;
508
509 stateno = stateRemap[i];
510 if (stateno == -1)
511 continue;
512 state = ret->states[i];
513
514 transitions[stateno * (nbatoms + 1)] = state->type;
515
516 for (j = 0;j < state->nbTrans;j++) {
517 trans = &(state->trans[j]);
518 if ((trans->to == -1) || (trans->atom == NULL))
519 continue;
520 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000521 if ((trans->atom->data != NULL) && (transdata == NULL)) {
522 transdata = (void **) xmlMalloc(nbstates * nbatoms *
523 sizeof(void *));
524 if (transdata != NULL)
525 memset(transdata, 0,
526 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000527 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000528 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000529 break;
530 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000531 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000532 targetno = stateRemap[trans->to];
533 /*
534 * if the same atome can generate transition to 2 different
535 * states then it means the automata is not determinist and
536 * the compact form can't be used !
537 */
538 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
539 if (prev != 0) {
540 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000541 ret->determinist = 0;
542#ifdef DEBUG_COMPACTION
543 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
544 i, j, trans->atom->no, trans->to, atomno, targetno);
545 printf(" previous to is %d\n", prev);
546#endif
547 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000548 if (transdata != NULL)
549 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000550 xmlFree(transitions);
551 xmlFree(stateRemap);
552 xmlFree(stringRemap);
553 for (i = 0;i < nbatoms;i++)
554 xmlFree(stringMap[i]);
555 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000556 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000557 }
558 } else {
559#if 0
560 printf("State %d trans %d: atom %d to %d : %d to %d\n",
561 i, j, trans->atom->no, trans->to, atomno, targetno);
562#endif
563 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000564 targetno + 1; /* to avoid 0 */
565 if (transdata != NULL)
566 transdata[stateno * nbatoms + atomno] =
567 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000568 }
569 }
570 }
571 ret->determinist = 1;
572#ifdef DEBUG_COMPACTION
573 /*
574 * Debug
575 */
576 for (i = 0;i < nbstates;i++) {
577 for (j = 0;j < nbatoms + 1;j++) {
578 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
579 }
580 printf("\n");
581 }
582 printf("\n");
583#endif
584 /*
585 * Cleanup of the old data
586 */
587 if (ret->states != NULL) {
588 for (i = 0;i < ret->nbStates;i++)
589 xmlRegFreeState(ret->states[i]);
590 xmlFree(ret->states);
591 }
592 ret->states = NULL;
593 ret->nbStates = 0;
594 if (ret->atoms != NULL) {
595 for (i = 0;i < ret->nbAtoms;i++)
596 xmlRegFreeAtom(ret->atoms[i]);
597 xmlFree(ret->atoms);
598 }
599 ret->atoms = NULL;
600 ret->nbAtoms = 0;
601
602 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000603 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000604 ret->stringMap = stringMap;
605 ret->nbstrings = nbatoms;
606 ret->nbstates = nbstates;
607 xmlFree(stateRemap);
608 xmlFree(stringRemap);
609 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000610not_determ:
611 ctxt->string = NULL;
612 ctxt->nbStates = 0;
613 ctxt->states = NULL;
614 ctxt->nbAtoms = 0;
615 ctxt->atoms = NULL;
616 ctxt->nbCounters = 0;
617 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000618 return(ret);
619}
620
621/**
622 * xmlRegNewParserCtxt:
623 * @string: the string to parse
624 *
625 * Allocate a new regexp parser context
626 *
627 * Returns the new context or NULL in case of error
628 */
629static xmlRegParserCtxtPtr
630xmlRegNewParserCtxt(const xmlChar *string) {
631 xmlRegParserCtxtPtr ret;
632
633 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
634 if (ret == NULL)
635 return(NULL);
636 memset(ret, 0, sizeof(xmlRegParserCtxt));
637 if (string != NULL)
638 ret->string = xmlStrdup(string);
639 ret->cur = ret->string;
640 ret->neg = 0;
641 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000642 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000643 return(ret);
644}
645
646/**
647 * xmlRegNewRange:
648 * @ctxt: the regexp parser context
649 * @neg: is that negative
650 * @type: the type of range
651 * @start: the start codepoint
652 * @end: the end codepoint
653 *
654 * Allocate a new regexp range
655 *
656 * Returns the new range or NULL in case of error
657 */
658static xmlRegRangePtr
659xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
660 int neg, xmlRegAtomType type, int start, int end) {
661 xmlRegRangePtr ret;
662
663 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
664 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000665 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000666 return(NULL);
667 }
668 ret->neg = neg;
669 ret->type = type;
670 ret->start = start;
671 ret->end = end;
672 return(ret);
673}
674
675/**
676 * xmlRegFreeRange:
677 * @range: the regexp range
678 *
679 * Free a regexp range
680 */
681static void
682xmlRegFreeRange(xmlRegRangePtr range) {
683 if (range == NULL)
684 return;
685
686 if (range->blockName != NULL)
687 xmlFree(range->blockName);
688 xmlFree(range);
689}
690
691/**
692 * xmlRegNewAtom:
693 * @ctxt: the regexp parser context
694 * @type: the type of atom
695 *
696 * Allocate a new regexp range
697 *
698 * Returns the new atom or NULL in case of error
699 */
700static xmlRegAtomPtr
701xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
702 xmlRegAtomPtr ret;
703
704 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
705 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000706 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000707 return(NULL);
708 }
709 memset(ret, 0, sizeof(xmlRegAtom));
710 ret->type = type;
711 ret->quant = XML_REGEXP_QUANT_ONCE;
712 ret->min = 0;
713 ret->max = 0;
714 return(ret);
715}
716
717/**
718 * xmlRegFreeAtom:
719 * @atom: the regexp atom
720 *
721 * Free a regexp atom
722 */
723static void
724xmlRegFreeAtom(xmlRegAtomPtr atom) {
725 int i;
726
727 if (atom == NULL)
728 return;
729
730 for (i = 0;i < atom->nbRanges;i++)
731 xmlRegFreeRange(atom->ranges[i]);
732 if (atom->ranges != NULL)
733 xmlFree(atom->ranges);
734 if (atom->type == XML_REGEXP_STRING)
735 xmlFree(atom->valuep);
736 xmlFree(atom);
737}
738
739static xmlRegStatePtr
740xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
741 xmlRegStatePtr ret;
742
743 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
744 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000745 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000746 return(NULL);
747 }
748 memset(ret, 0, sizeof(xmlRegState));
749 ret->type = XML_REGEXP_TRANS_STATE;
750 ret->mark = XML_REGEXP_MARK_NORMAL;
751 return(ret);
752}
753
754/**
755 * xmlRegFreeState:
756 * @state: the regexp state
757 *
758 * Free a regexp state
759 */
760static void
761xmlRegFreeState(xmlRegStatePtr state) {
762 if (state == NULL)
763 return;
764
765 if (state->trans != NULL)
766 xmlFree(state->trans);
767 xmlFree(state);
768}
769
770/**
771 * xmlRegFreeParserCtxt:
772 * @ctxt: the regexp parser context
773 *
774 * Free a regexp parser context
775 */
776static void
777xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
778 int i;
779 if (ctxt == NULL)
780 return;
781
782 if (ctxt->string != NULL)
783 xmlFree(ctxt->string);
784 if (ctxt->states != NULL) {
785 for (i = 0;i < ctxt->nbStates;i++)
786 xmlRegFreeState(ctxt->states[i]);
787 xmlFree(ctxt->states);
788 }
789 if (ctxt->atoms != NULL) {
790 for (i = 0;i < ctxt->nbAtoms;i++)
791 xmlRegFreeAtom(ctxt->atoms[i]);
792 xmlFree(ctxt->atoms);
793 }
794 if (ctxt->counters != NULL)
795 xmlFree(ctxt->counters);
796 xmlFree(ctxt);
797}
798
799/************************************************************************
800 * *
801 * Display of Data structures *
802 * *
803 ************************************************************************/
804
805static void
806xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
807 switch (type) {
808 case XML_REGEXP_EPSILON:
809 fprintf(output, "epsilon "); break;
810 case XML_REGEXP_CHARVAL:
811 fprintf(output, "charval "); break;
812 case XML_REGEXP_RANGES:
813 fprintf(output, "ranges "); break;
814 case XML_REGEXP_SUBREG:
815 fprintf(output, "subexpr "); break;
816 case XML_REGEXP_STRING:
817 fprintf(output, "string "); break;
818 case XML_REGEXP_ANYCHAR:
819 fprintf(output, "anychar "); break;
820 case XML_REGEXP_ANYSPACE:
821 fprintf(output, "anyspace "); break;
822 case XML_REGEXP_NOTSPACE:
823 fprintf(output, "notspace "); break;
824 case XML_REGEXP_INITNAME:
825 fprintf(output, "initname "); break;
826 case XML_REGEXP_NOTINITNAME:
827 fprintf(output, "notinitname "); break;
828 case XML_REGEXP_NAMECHAR:
829 fprintf(output, "namechar "); break;
830 case XML_REGEXP_NOTNAMECHAR:
831 fprintf(output, "notnamechar "); break;
832 case XML_REGEXP_DECIMAL:
833 fprintf(output, "decimal "); break;
834 case XML_REGEXP_NOTDECIMAL:
835 fprintf(output, "notdecimal "); break;
836 case XML_REGEXP_REALCHAR:
837 fprintf(output, "realchar "); break;
838 case XML_REGEXP_NOTREALCHAR:
839 fprintf(output, "notrealchar "); break;
840 case XML_REGEXP_LETTER:
841 fprintf(output, "LETTER "); break;
842 case XML_REGEXP_LETTER_UPPERCASE:
843 fprintf(output, "LETTER_UPPERCASE "); break;
844 case XML_REGEXP_LETTER_LOWERCASE:
845 fprintf(output, "LETTER_LOWERCASE "); break;
846 case XML_REGEXP_LETTER_TITLECASE:
847 fprintf(output, "LETTER_TITLECASE "); break;
848 case XML_REGEXP_LETTER_MODIFIER:
849 fprintf(output, "LETTER_MODIFIER "); break;
850 case XML_REGEXP_LETTER_OTHERS:
851 fprintf(output, "LETTER_OTHERS "); break;
852 case XML_REGEXP_MARK:
853 fprintf(output, "MARK "); break;
854 case XML_REGEXP_MARK_NONSPACING:
855 fprintf(output, "MARK_NONSPACING "); break;
856 case XML_REGEXP_MARK_SPACECOMBINING:
857 fprintf(output, "MARK_SPACECOMBINING "); break;
858 case XML_REGEXP_MARK_ENCLOSING:
859 fprintf(output, "MARK_ENCLOSING "); break;
860 case XML_REGEXP_NUMBER:
861 fprintf(output, "NUMBER "); break;
862 case XML_REGEXP_NUMBER_DECIMAL:
863 fprintf(output, "NUMBER_DECIMAL "); break;
864 case XML_REGEXP_NUMBER_LETTER:
865 fprintf(output, "NUMBER_LETTER "); break;
866 case XML_REGEXP_NUMBER_OTHERS:
867 fprintf(output, "NUMBER_OTHERS "); break;
868 case XML_REGEXP_PUNCT:
869 fprintf(output, "PUNCT "); break;
870 case XML_REGEXP_PUNCT_CONNECTOR:
871 fprintf(output, "PUNCT_CONNECTOR "); break;
872 case XML_REGEXP_PUNCT_DASH:
873 fprintf(output, "PUNCT_DASH "); break;
874 case XML_REGEXP_PUNCT_OPEN:
875 fprintf(output, "PUNCT_OPEN "); break;
876 case XML_REGEXP_PUNCT_CLOSE:
877 fprintf(output, "PUNCT_CLOSE "); break;
878 case XML_REGEXP_PUNCT_INITQUOTE:
879 fprintf(output, "PUNCT_INITQUOTE "); break;
880 case XML_REGEXP_PUNCT_FINQUOTE:
881 fprintf(output, "PUNCT_FINQUOTE "); break;
882 case XML_REGEXP_PUNCT_OTHERS:
883 fprintf(output, "PUNCT_OTHERS "); break;
884 case XML_REGEXP_SEPAR:
885 fprintf(output, "SEPAR "); break;
886 case XML_REGEXP_SEPAR_SPACE:
887 fprintf(output, "SEPAR_SPACE "); break;
888 case XML_REGEXP_SEPAR_LINE:
889 fprintf(output, "SEPAR_LINE "); break;
890 case XML_REGEXP_SEPAR_PARA:
891 fprintf(output, "SEPAR_PARA "); break;
892 case XML_REGEXP_SYMBOL:
893 fprintf(output, "SYMBOL "); break;
894 case XML_REGEXP_SYMBOL_MATH:
895 fprintf(output, "SYMBOL_MATH "); break;
896 case XML_REGEXP_SYMBOL_CURRENCY:
897 fprintf(output, "SYMBOL_CURRENCY "); break;
898 case XML_REGEXP_SYMBOL_MODIFIER:
899 fprintf(output, "SYMBOL_MODIFIER "); break;
900 case XML_REGEXP_SYMBOL_OTHERS:
901 fprintf(output, "SYMBOL_OTHERS "); break;
902 case XML_REGEXP_OTHER:
903 fprintf(output, "OTHER "); break;
904 case XML_REGEXP_OTHER_CONTROL:
905 fprintf(output, "OTHER_CONTROL "); break;
906 case XML_REGEXP_OTHER_FORMAT:
907 fprintf(output, "OTHER_FORMAT "); break;
908 case XML_REGEXP_OTHER_PRIVATE:
909 fprintf(output, "OTHER_PRIVATE "); break;
910 case XML_REGEXP_OTHER_NA:
911 fprintf(output, "OTHER_NA "); break;
912 case XML_REGEXP_BLOCK_NAME:
913 fprintf(output, "BLOCK "); break;
914 }
915}
916
917static void
918xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
919 switch (type) {
920 case XML_REGEXP_QUANT_EPSILON:
921 fprintf(output, "epsilon "); break;
922 case XML_REGEXP_QUANT_ONCE:
923 fprintf(output, "once "); break;
924 case XML_REGEXP_QUANT_OPT:
925 fprintf(output, "? "); break;
926 case XML_REGEXP_QUANT_MULT:
927 fprintf(output, "* "); break;
928 case XML_REGEXP_QUANT_PLUS:
929 fprintf(output, "+ "); break;
930 case XML_REGEXP_QUANT_RANGE:
931 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000932 case XML_REGEXP_QUANT_ONCEONLY:
933 fprintf(output, "onceonly "); break;
934 case XML_REGEXP_QUANT_ALL:
935 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000936 }
937}
938static void
939xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
940 fprintf(output, " range: ");
941 if (range->neg)
942 fprintf(output, "negative ");
943 xmlRegPrintAtomType(output, range->type);
944 fprintf(output, "%c - %c\n", range->start, range->end);
945}
946
947static void
948xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
949 fprintf(output, " atom: ");
950 if (atom == NULL) {
951 fprintf(output, "NULL\n");
952 return;
953 }
954 xmlRegPrintAtomType(output, atom->type);
955 xmlRegPrintQuantType(output, atom->quant);
956 if (atom->quant == XML_REGEXP_QUANT_RANGE)
957 fprintf(output, "%d-%d ", atom->min, atom->max);
958 if (atom->type == XML_REGEXP_STRING)
959 fprintf(output, "'%s' ", (char *) atom->valuep);
960 if (atom->type == XML_REGEXP_CHARVAL)
961 fprintf(output, "char %c\n", atom->codepoint);
962 else if (atom->type == XML_REGEXP_RANGES) {
963 int i;
964 fprintf(output, "%d entries\n", atom->nbRanges);
965 for (i = 0; i < atom->nbRanges;i++)
966 xmlRegPrintRange(output, atom->ranges[i]);
967 } else if (atom->type == XML_REGEXP_SUBREG) {
968 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
969 } else {
970 fprintf(output, "\n");
971 }
972}
973
974static void
975xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
976 fprintf(output, " trans: ");
977 if (trans == NULL) {
978 fprintf(output, "NULL\n");
979 return;
980 }
981 if (trans->to < 0) {
982 fprintf(output, "removed\n");
983 return;
984 }
985 if (trans->counter >= 0) {
986 fprintf(output, "counted %d, ", trans->counter);
987 }
Daniel Veillard8a001f62002-04-20 07:24:11 +0000988 if (trans->count == REGEXP_ALL_COUNTER) {
989 fprintf(output, "all transition, ");
990 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000991 fprintf(output, "count based %d, ", trans->count);
992 }
993 if (trans->atom == NULL) {
994 fprintf(output, "epsilon to %d\n", trans->to);
995 return;
996 }
997 if (trans->atom->type == XML_REGEXP_CHARVAL)
998 fprintf(output, "char %c ", trans->atom->codepoint);
999 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1000}
1001
1002static void
1003xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1004 int i;
1005
1006 fprintf(output, " state: ");
1007 if (state == NULL) {
1008 fprintf(output, "NULL\n");
1009 return;
1010 }
1011 if (state->type == XML_REGEXP_START_STATE)
1012 fprintf(output, "START ");
1013 if (state->type == XML_REGEXP_FINAL_STATE)
1014 fprintf(output, "FINAL ");
1015
1016 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1017 for (i = 0;i < state->nbTrans; i++) {
1018 xmlRegPrintTrans(output, &(state->trans[i]));
1019 }
1020}
1021
Daniel Veillard23e73572002-09-19 19:56:43 +00001022#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001023static void
1024xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1025 int i;
1026
1027 fprintf(output, " ctxt: ");
1028 if (ctxt == NULL) {
1029 fprintf(output, "NULL\n");
1030 return;
1031 }
1032 fprintf(output, "'%s' ", ctxt->string);
1033 if (ctxt->error)
1034 fprintf(output, "error ");
1035 if (ctxt->neg)
1036 fprintf(output, "neg ");
1037 fprintf(output, "\n");
1038 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1039 for (i = 0;i < ctxt->nbAtoms; i++) {
1040 fprintf(output, " %02d ", i);
1041 xmlRegPrintAtom(output, ctxt->atoms[i]);
1042 }
1043 if (ctxt->atom != NULL) {
1044 fprintf(output, "current atom:\n");
1045 xmlRegPrintAtom(output, ctxt->atom);
1046 }
1047 fprintf(output, "%d states:", ctxt->nbStates);
1048 if (ctxt->start != NULL)
1049 fprintf(output, " start: %d", ctxt->start->no);
1050 if (ctxt->end != NULL)
1051 fprintf(output, " end: %d", ctxt->end->no);
1052 fprintf(output, "\n");
1053 for (i = 0;i < ctxt->nbStates; i++) {
1054 xmlRegPrintState(output, ctxt->states[i]);
1055 }
1056 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1057 for (i = 0;i < ctxt->nbCounters; i++) {
1058 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1059 ctxt->counters[i].max);
1060 }
1061}
Daniel Veillard23e73572002-09-19 19:56:43 +00001062#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001063
1064/************************************************************************
1065 * *
1066 * Finite Automata structures manipulations *
1067 * *
1068 ************************************************************************/
1069
1070static void
1071xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1072 int neg, xmlRegAtomType type, int start, int end,
1073 xmlChar *blockName) {
1074 xmlRegRangePtr range;
1075
1076 if (atom == NULL) {
1077 ERROR("add range: atom is NULL");
1078 return;
1079 }
1080 if (atom->type != XML_REGEXP_RANGES) {
1081 ERROR("add range: atom is not ranges");
1082 return;
1083 }
1084 if (atom->maxRanges == 0) {
1085 atom->maxRanges = 4;
1086 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1087 sizeof(xmlRegRangePtr));
1088 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001089 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001090 atom->maxRanges = 0;
1091 return;
1092 }
1093 } else if (atom->nbRanges >= atom->maxRanges) {
1094 xmlRegRangePtr *tmp;
1095 atom->maxRanges *= 2;
1096 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1097 sizeof(xmlRegRangePtr));
1098 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001099 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001100 atom->maxRanges /= 2;
1101 return;
1102 }
1103 atom->ranges = tmp;
1104 }
1105 range = xmlRegNewRange(ctxt, neg, type, start, end);
1106 if (range == NULL)
1107 return;
1108 range->blockName = blockName;
1109 atom->ranges[atom->nbRanges++] = range;
1110
1111}
1112
1113static int
1114xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1115 if (ctxt->maxCounters == 0) {
1116 ctxt->maxCounters = 4;
1117 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1118 sizeof(xmlRegCounter));
1119 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001120 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001121 ctxt->maxCounters = 0;
1122 return(-1);
1123 }
1124 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1125 xmlRegCounter *tmp;
1126 ctxt->maxCounters *= 2;
1127 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1128 sizeof(xmlRegCounter));
1129 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001130 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001131 ctxt->maxCounters /= 2;
1132 return(-1);
1133 }
1134 ctxt->counters = tmp;
1135 }
1136 ctxt->counters[ctxt->nbCounters].min = -1;
1137 ctxt->counters[ctxt->nbCounters].max = -1;
1138 return(ctxt->nbCounters++);
1139}
1140
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001141static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001142xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1143 if (atom == NULL) {
1144 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001145 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001146 }
1147 if (ctxt->maxAtoms == 0) {
1148 ctxt->maxAtoms = 4;
1149 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1150 sizeof(xmlRegAtomPtr));
1151 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001152 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001153 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001154 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001155 }
1156 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1157 xmlRegAtomPtr *tmp;
1158 ctxt->maxAtoms *= 2;
1159 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1160 sizeof(xmlRegAtomPtr));
1161 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001162 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001163 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001164 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001165 }
1166 ctxt->atoms = tmp;
1167 }
1168 atom->no = ctxt->nbAtoms;
1169 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001170 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001171}
1172
1173static void
1174xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1175 xmlRegAtomPtr atom, xmlRegStatePtr target,
1176 int counter, int count) {
1177 if (state == NULL) {
1178 ERROR("add state: state is NULL");
1179 return;
1180 }
1181 if (target == NULL) {
1182 ERROR("add state: target is NULL");
1183 return;
1184 }
1185 if (state->maxTrans == 0) {
1186 state->maxTrans = 4;
1187 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1188 sizeof(xmlRegTrans));
1189 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001190 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001191 state->maxTrans = 0;
1192 return;
1193 }
1194 } else if (state->nbTrans >= state->maxTrans) {
1195 xmlRegTrans *tmp;
1196 state->maxTrans *= 2;
1197 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1198 sizeof(xmlRegTrans));
1199 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001200 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001201 state->maxTrans /= 2;
1202 return;
1203 }
1204 state->trans = tmp;
1205 }
1206#ifdef DEBUG_REGEXP_GRAPH
1207 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001208 if (count == REGEXP_ALL_COUNTER)
1209 printf("all transition");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001210 else if (count >= 0)
Daniel Veillard4255d502002-04-16 15:50:10 +00001211 printf("count based %d", count);
1212 else if (counter >= 0)
1213 printf("counted %d", counter);
1214 else if (atom == NULL)
1215 printf("epsilon transition");
1216 printf("\n");
1217#endif
1218
1219 state->trans[state->nbTrans].atom = atom;
1220 state->trans[state->nbTrans].to = target->no;
1221 state->trans[state->nbTrans].counter = counter;
1222 state->trans[state->nbTrans].count = count;
1223 state->nbTrans++;
1224}
1225
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001226static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001227xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001228 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001229 if (ctxt->maxStates == 0) {
1230 ctxt->maxStates = 4;
1231 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1232 sizeof(xmlRegStatePtr));
1233 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001234 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001235 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001236 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001237 }
1238 } else if (ctxt->nbStates >= ctxt->maxStates) {
1239 xmlRegStatePtr *tmp;
1240 ctxt->maxStates *= 2;
1241 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1242 sizeof(xmlRegStatePtr));
1243 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001244 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001245 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001246 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001247 }
1248 ctxt->states = tmp;
1249 }
1250 state->no = ctxt->nbStates;
1251 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001252 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001253}
1254
1255/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001256 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001257 * @ctxt: a regexp parser context
1258 * @from: the from state
1259 * @to: the target state or NULL for building a new one
1260 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001261 *
1262 */
1263static void
1264xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001265 xmlRegStatePtr from, xmlRegStatePtr to,
1266 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001267 if (to == NULL) {
1268 to = xmlRegNewState(ctxt);
1269 xmlRegStatePush(ctxt, to);
1270 ctxt->state = to;
1271 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001272 if (lax)
1273 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1274 else
1275 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001276}
1277
1278/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001279 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001280 * @ctxt: a regexp parser context
1281 * @from: the from state
1282 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001283 *
1284 */
1285static void
1286xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1287 xmlRegStatePtr from, xmlRegStatePtr to) {
1288 if (to == NULL) {
1289 to = xmlRegNewState(ctxt);
1290 xmlRegStatePush(ctxt, to);
1291 ctxt->state = to;
1292 }
1293 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1294}
1295
1296/**
1297 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001298 * @ctxt: a regexp parser context
1299 * @from: the from state
1300 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001301 * counter: the counter for that transition
1302 *
1303 */
1304static void
1305xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1306 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1307 if (to == NULL) {
1308 to = xmlRegNewState(ctxt);
1309 xmlRegStatePush(ctxt, to);
1310 ctxt->state = to;
1311 }
1312 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1313}
1314
1315/**
1316 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001317 * @ctxt: a regexp parser context
1318 * @from: the from state
1319 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001320 * counter: the counter for that transition
1321 *
1322 */
1323static void
1324xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1325 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1326 if (to == NULL) {
1327 to = xmlRegNewState(ctxt);
1328 xmlRegStatePush(ctxt, to);
1329 ctxt->state = to;
1330 }
1331 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1332}
1333
1334/**
1335 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001336 * @ctxt: a regexp parser context
1337 * @from: the from state
1338 * @to: the target state or NULL for building a new one
1339 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001340 *
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001341 * Returns 0 if succes and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001342 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001343static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001344xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1345 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1346 if (atom == NULL) {
1347 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001348 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001349 }
1350 if (atom->type == XML_REGEXP_SUBREG) {
1351 /*
1352 * this is a subexpression handling one should not need to
1353 * create a new node excep for XML_REGEXP_QUANT_RANGE.
1354 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001355 if (xmlRegAtomPush(ctxt, atom) < 0) {
1356 return(-1);
1357 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001358 if ((to != NULL) && (atom->stop != to) &&
1359 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1360 /*
1361 * Generate an epsilon transition to link to the target
1362 */
1363 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1364 }
1365 switch (atom->quant) {
1366 case XML_REGEXP_QUANT_OPT:
1367 atom->quant = XML_REGEXP_QUANT_ONCE;
1368 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1369 break;
1370 case XML_REGEXP_QUANT_MULT:
1371 atom->quant = XML_REGEXP_QUANT_ONCE;
1372 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1373 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1374 break;
1375 case XML_REGEXP_QUANT_PLUS:
1376 atom->quant = XML_REGEXP_QUANT_ONCE;
1377 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1378 break;
1379 case XML_REGEXP_QUANT_RANGE: {
1380 int counter;
1381 xmlRegStatePtr newstate;
1382
1383 /*
1384 * This one is nasty:
1385 * 1/ register a new counter
1386 * 2/ register an epsilon transition associated to
1387 * this counter going from atom->stop to atom->start
1388 * 3/ create a new state
1389 * 4/ generate a counted transition from atom->stop to
1390 * that state
1391 */
1392 counter = xmlRegGetCounter(ctxt);
1393 ctxt->counters[counter].min = atom->min - 1;
1394 ctxt->counters[counter].max = atom->max - 1;
1395 atom->min = 0;
1396 atom->max = 0;
1397 atom->quant = XML_REGEXP_QUANT_ONCE;
1398 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1399 atom->start, counter);
1400 if (to != NULL) {
1401 newstate = to;
1402 } else {
1403 newstate = xmlRegNewState(ctxt);
1404 xmlRegStatePush(ctxt, newstate);
1405 ctxt->state = newstate;
1406 }
1407 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1408 newstate, counter);
1409 }
1410 default:
1411 break;
1412 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001413 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001414 } else {
1415 if (to == NULL) {
1416 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001417 if (to != NULL)
1418 xmlRegStatePush(ctxt, to);
1419 else {
1420 return(-1);
1421 }
1422 }
1423 if (xmlRegAtomPush(ctxt, atom) < 0) {
1424 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001425 }
1426 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001427 ctxt->state = to;
1428 }
1429 switch (atom->quant) {
1430 case XML_REGEXP_QUANT_OPT:
1431 atom->quant = XML_REGEXP_QUANT_ONCE;
1432 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1433 break;
1434 case XML_REGEXP_QUANT_MULT:
1435 atom->quant = XML_REGEXP_QUANT_ONCE;
1436 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1437 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1438 break;
1439 case XML_REGEXP_QUANT_PLUS:
1440 atom->quant = XML_REGEXP_QUANT_ONCE;
1441 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1442 break;
1443 default:
1444 break;
1445 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001446 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001447}
1448
1449/**
1450 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001451 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001452 * @fromnr: the from state
1453 * @tonr: the to state
1454 * @cpunter: should that transition be associted to a counted
1455 *
1456 */
1457static void
1458xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1459 int tonr, int counter) {
1460 int transnr;
1461 xmlRegStatePtr from;
1462 xmlRegStatePtr to;
1463
1464#ifdef DEBUG_REGEXP_GRAPH
1465 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1466#endif
1467 from = ctxt->states[fromnr];
1468 if (from == NULL)
1469 return;
1470 to = ctxt->states[tonr];
1471 if (to == NULL)
1472 return;
1473 if ((to->mark == XML_REGEXP_MARK_START) ||
1474 (to->mark == XML_REGEXP_MARK_VISITED))
1475 return;
1476
1477 to->mark = XML_REGEXP_MARK_VISITED;
1478 if (to->type == XML_REGEXP_FINAL_STATE) {
1479#ifdef DEBUG_REGEXP_GRAPH
1480 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1481#endif
1482 from->type = XML_REGEXP_FINAL_STATE;
1483 }
1484 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1485 if (to->trans[transnr].atom == NULL) {
1486 /*
1487 * Don't remove counted transitions
1488 * Don't loop either
1489 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001490 if (to->trans[transnr].to != fromnr) {
1491 if (to->trans[transnr].count >= 0) {
1492 int newto = to->trans[transnr].to;
1493
1494 xmlRegStateAddTrans(ctxt, from, NULL,
1495 ctxt->states[newto],
1496 -1, to->trans[transnr].count);
1497 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001498#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001499 printf("Found epsilon trans %d from %d to %d\n",
1500 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001501#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001502 if (to->trans[transnr].counter >= 0) {
1503 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1504 to->trans[transnr].to,
1505 to->trans[transnr].counter);
1506 } else {
1507 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1508 to->trans[transnr].to,
1509 counter);
1510 }
1511 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001512 }
1513 } else {
1514 int newto = to->trans[transnr].to;
1515
Daniel Veillardb509f152002-04-17 16:28:10 +00001516 if (to->trans[transnr].counter >= 0) {
1517 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1518 ctxt->states[newto],
1519 to->trans[transnr].counter, -1);
1520 } else {
1521 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1522 ctxt->states[newto], counter, -1);
1523 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001524 }
1525 }
1526 to->mark = XML_REGEXP_MARK_NORMAL;
1527}
1528
1529/**
1530 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001531 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001532 *
1533 */
1534static void
1535xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1536 int statenr, transnr;
1537 xmlRegStatePtr state;
1538
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001539 if (ctxt->states == NULL) return;
1540
1541
Daniel Veillard4255d502002-04-16 15:50:10 +00001542 /*
1543 * build the completed transitions bypassing the epsilons
1544 * Use a marking algorithm to avoid loops
1545 */
1546 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1547 state = ctxt->states[statenr];
1548 if (state == NULL)
1549 continue;
1550 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1551 if ((state->trans[transnr].atom == NULL) &&
1552 (state->trans[transnr].to >= 0)) {
1553 if (state->trans[transnr].to == statenr) {
1554 state->trans[transnr].to = -1;
1555#ifdef DEBUG_REGEXP_GRAPH
1556 printf("Removed loopback epsilon trans %d on %d\n",
1557 transnr, statenr);
1558#endif
1559 } else if (state->trans[transnr].count < 0) {
1560 int newto = state->trans[transnr].to;
1561
1562#ifdef DEBUG_REGEXP_GRAPH
1563 printf("Found epsilon trans %d from %d to %d\n",
1564 transnr, statenr, newto);
1565#endif
1566 state->mark = XML_REGEXP_MARK_START;
1567 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1568 newto, state->trans[transnr].counter);
1569 state->mark = XML_REGEXP_MARK_NORMAL;
1570#ifdef DEBUG_REGEXP_GRAPH
1571 } else {
1572 printf("Found counted transition %d on %d\n",
1573 transnr, statenr);
1574#endif
1575 }
1576 }
1577 }
1578 }
1579 /*
1580 * Eliminate the epsilon transitions
1581 */
1582 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1583 state = ctxt->states[statenr];
1584 if (state == NULL)
1585 continue;
1586 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1587 if ((state->trans[transnr].atom == NULL) &&
1588 (state->trans[transnr].count < 0) &&
1589 (state->trans[transnr].to >= 0)) {
1590 state->trans[transnr].to = -1;
1591 }
1592 }
1593 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001594
1595 /*
1596 * Use this pass to detect unreachable states too
1597 */
1598 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1599 state = ctxt->states[statenr];
1600 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001601 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001602 }
1603 state = ctxt->states[0];
1604 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001605 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001606 while (state != NULL) {
1607 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001608 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001609 /*
1610 * Mark all state reachable from the current reachable state
1611 */
1612 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1613 if ((state->trans[transnr].to >= 0) &&
1614 ((state->trans[transnr].atom != NULL) ||
1615 (state->trans[transnr].count >= 0))) {
1616 int newto = state->trans[transnr].to;
1617
1618 if (ctxt->states[newto] == NULL)
1619 continue;
William M. Brack779af002003-08-01 15:55:39 +00001620 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1621 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001622 target = ctxt->states[newto];
1623 }
1624 }
1625 }
1626 /*
1627 * find the next accessible state not explored
1628 */
1629 if (target == NULL) {
1630 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1631 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001632 if ((state != NULL) && (state->reached ==
1633 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001634 target = state;
1635 break;
1636 }
1637 }
1638 }
1639 state = target;
1640 }
1641 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1642 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001643 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001644#ifdef DEBUG_REGEXP_GRAPH
1645 printf("Removed unreachable state %d\n", statenr);
1646#endif
1647 xmlRegFreeState(state);
1648 ctxt->states[statenr] = NULL;
1649 }
1650 }
1651
Daniel Veillard4255d502002-04-16 15:50:10 +00001652}
1653
Daniel Veillarde19fc232002-04-22 16:01:24 +00001654/**
1655 * xmlFACompareAtoms:
1656 * @atom1: an atom
1657 * @atom2: an atom
1658 *
1659 * Compares two atoms to check whether they are equivatents
1660 *
1661 * Returns 1 if yes and 0 otherwise
1662 */
1663static int
1664xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1665 if (atom1 == atom2)
1666 return(1);
1667 if ((atom1 == NULL) || (atom2 == NULL))
1668 return(0);
1669
1670 if (atom1->type != atom2->type)
1671 return(0);
1672 switch (atom1->type) {
1673 case XML_REGEXP_STRING:
1674 return(xmlStrEqual((xmlChar *)atom1->valuep,
1675 (xmlChar *)atom2->valuep));
1676 case XML_REGEXP_EPSILON:
1677 return(1);
1678 case XML_REGEXP_CHARVAL:
1679 return(atom1->codepoint == atom2->codepoint);
1680 case XML_REGEXP_RANGES:
1681 TODO;
1682 return(0);
1683 default:
1684 break;
1685 }
1686 return(1);
1687}
1688
1689/**
1690 * xmlFARecurseDeterminism:
1691 * @ctxt: a regexp parser context
1692 *
1693 * Check whether the associated regexp is determinist,
1694 * should be called after xmlFAEliminateEpsilonTransitions()
1695 *
1696 */
1697static int
1698xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1699 int to, xmlRegAtomPtr atom) {
1700 int ret = 1;
1701 int transnr;
1702 xmlRegTransPtr t1;
1703
1704 if (state == NULL)
1705 return(ret);
1706 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1707 t1 = &(state->trans[transnr]);
1708 /*
1709 * check transitions conflicting with the one looked at
1710 */
1711 if (t1->atom == NULL) {
1712 if (t1->to == -1)
1713 continue;
1714 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1715 to, atom);
1716 if (ret == 0)
1717 return(0);
1718 continue;
1719 }
1720 if (t1->to != to)
1721 continue;
1722 if (xmlFACompareAtoms(t1->atom, atom))
1723 return(0);
1724 }
1725 return(ret);
1726}
1727
1728/**
1729 * xmlFAComputesDeterminism:
1730 * @ctxt: a regexp parser context
1731 *
1732 * Check whether the associated regexp is determinist,
1733 * should be called after xmlFAEliminateEpsilonTransitions()
1734 *
1735 */
1736static int
1737xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1738 int statenr, transnr;
1739 xmlRegStatePtr state;
1740 xmlRegTransPtr t1, t2;
1741 int i;
1742 int ret = 1;
1743
Daniel Veillard4402ab42002-09-12 16:02:56 +00001744#ifdef DEBUG_REGEXP_GRAPH
1745 printf("xmlFAComputesDeterminism\n");
1746 xmlRegPrintCtxt(stdout, ctxt);
1747#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001748 if (ctxt->determinist != -1)
1749 return(ctxt->determinist);
1750
1751 /*
1752 * Check for all states that there isn't 2 transitions
1753 * with the same atom and a different target.
1754 */
1755 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1756 state = ctxt->states[statenr];
1757 if (state == NULL)
1758 continue;
1759 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1760 t1 = &(state->trans[transnr]);
1761 /*
1762 * Determinism checks in case of counted or all transitions
1763 * will have to be handled separately
1764 */
1765 if (t1->atom == NULL)
1766 continue;
1767 if (t1->to == -1) /* eliminated */
1768 continue;
1769 for (i = 0;i < transnr;i++) {
1770 t2 = &(state->trans[i]);
1771 if (t2->to == -1) /* eliminated */
1772 continue;
1773 if (t2->atom != NULL) {
1774 if (t1->to == t2->to) {
1775 if (xmlFACompareAtoms(t1->atom, t2->atom))
1776 t2->to = -1; /* eliminate */
1777 } else {
1778 /* not determinist ! */
1779 if (xmlFACompareAtoms(t1->atom, t2->atom))
1780 ret = 0;
1781 }
1782 } else if (t1->to != -1) {
1783 /*
1784 * do the closure in case of remaining specific
1785 * epsilon transitions like choices or all
1786 */
1787 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1788 t2->to, t2->atom);
1789 if (ret == 0)
1790 return(0);
1791 }
1792 }
1793 if (ret == 0)
1794 break;
1795 }
1796 if (ret == 0)
1797 break;
1798 }
1799 ctxt->determinist = ret;
1800 return(ret);
1801}
1802
Daniel Veillard4255d502002-04-16 15:50:10 +00001803/************************************************************************
1804 * *
1805 * Routines to check input against transition atoms *
1806 * *
1807 ************************************************************************/
1808
1809static int
1810xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1811 int start, int end, const xmlChar *blockName) {
1812 int ret = 0;
1813
1814 switch (type) {
1815 case XML_REGEXP_STRING:
1816 case XML_REGEXP_SUBREG:
1817 case XML_REGEXP_RANGES:
1818 case XML_REGEXP_EPSILON:
1819 return(-1);
1820 case XML_REGEXP_ANYCHAR:
1821 ret = ((codepoint != '\n') && (codepoint != '\r'));
1822 break;
1823 case XML_REGEXP_CHARVAL:
1824 ret = ((codepoint >= start) && (codepoint <= end));
1825 break;
1826 case XML_REGEXP_NOTSPACE:
1827 neg = !neg;
1828 case XML_REGEXP_ANYSPACE:
1829 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1830 (codepoint == '\t') || (codepoint == ' '));
1831 break;
1832 case XML_REGEXP_NOTINITNAME:
1833 neg = !neg;
1834 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00001835 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001836 (codepoint == '_') || (codepoint == ':'));
1837 break;
1838 case XML_REGEXP_NOTNAMECHAR:
1839 neg = !neg;
1840 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00001841 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001842 (codepoint == '.') || (codepoint == '-') ||
1843 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00001844 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00001845 break;
1846 case XML_REGEXP_NOTDECIMAL:
1847 neg = !neg;
1848 case XML_REGEXP_DECIMAL:
1849 ret = xmlUCSIsCatNd(codepoint);
1850 break;
1851 case XML_REGEXP_REALCHAR:
1852 neg = !neg;
1853 case XML_REGEXP_NOTREALCHAR:
1854 ret = xmlUCSIsCatP(codepoint);
1855 if (ret == 0)
1856 ret = xmlUCSIsCatZ(codepoint);
1857 if (ret == 0)
1858 ret = xmlUCSIsCatC(codepoint);
1859 break;
1860 case XML_REGEXP_LETTER:
1861 ret = xmlUCSIsCatL(codepoint);
1862 break;
1863 case XML_REGEXP_LETTER_UPPERCASE:
1864 ret = xmlUCSIsCatLu(codepoint);
1865 break;
1866 case XML_REGEXP_LETTER_LOWERCASE:
1867 ret = xmlUCSIsCatLl(codepoint);
1868 break;
1869 case XML_REGEXP_LETTER_TITLECASE:
1870 ret = xmlUCSIsCatLt(codepoint);
1871 break;
1872 case XML_REGEXP_LETTER_MODIFIER:
1873 ret = xmlUCSIsCatLm(codepoint);
1874 break;
1875 case XML_REGEXP_LETTER_OTHERS:
1876 ret = xmlUCSIsCatLo(codepoint);
1877 break;
1878 case XML_REGEXP_MARK:
1879 ret = xmlUCSIsCatM(codepoint);
1880 break;
1881 case XML_REGEXP_MARK_NONSPACING:
1882 ret = xmlUCSIsCatMn(codepoint);
1883 break;
1884 case XML_REGEXP_MARK_SPACECOMBINING:
1885 ret = xmlUCSIsCatMc(codepoint);
1886 break;
1887 case XML_REGEXP_MARK_ENCLOSING:
1888 ret = xmlUCSIsCatMe(codepoint);
1889 break;
1890 case XML_REGEXP_NUMBER:
1891 ret = xmlUCSIsCatN(codepoint);
1892 break;
1893 case XML_REGEXP_NUMBER_DECIMAL:
1894 ret = xmlUCSIsCatNd(codepoint);
1895 break;
1896 case XML_REGEXP_NUMBER_LETTER:
1897 ret = xmlUCSIsCatNl(codepoint);
1898 break;
1899 case XML_REGEXP_NUMBER_OTHERS:
1900 ret = xmlUCSIsCatNo(codepoint);
1901 break;
1902 case XML_REGEXP_PUNCT:
1903 ret = xmlUCSIsCatP(codepoint);
1904 break;
1905 case XML_REGEXP_PUNCT_CONNECTOR:
1906 ret = xmlUCSIsCatPc(codepoint);
1907 break;
1908 case XML_REGEXP_PUNCT_DASH:
1909 ret = xmlUCSIsCatPd(codepoint);
1910 break;
1911 case XML_REGEXP_PUNCT_OPEN:
1912 ret = xmlUCSIsCatPs(codepoint);
1913 break;
1914 case XML_REGEXP_PUNCT_CLOSE:
1915 ret = xmlUCSIsCatPe(codepoint);
1916 break;
1917 case XML_REGEXP_PUNCT_INITQUOTE:
1918 ret = xmlUCSIsCatPi(codepoint);
1919 break;
1920 case XML_REGEXP_PUNCT_FINQUOTE:
1921 ret = xmlUCSIsCatPf(codepoint);
1922 break;
1923 case XML_REGEXP_PUNCT_OTHERS:
1924 ret = xmlUCSIsCatPo(codepoint);
1925 break;
1926 case XML_REGEXP_SEPAR:
1927 ret = xmlUCSIsCatZ(codepoint);
1928 break;
1929 case XML_REGEXP_SEPAR_SPACE:
1930 ret = xmlUCSIsCatZs(codepoint);
1931 break;
1932 case XML_REGEXP_SEPAR_LINE:
1933 ret = xmlUCSIsCatZl(codepoint);
1934 break;
1935 case XML_REGEXP_SEPAR_PARA:
1936 ret = xmlUCSIsCatZp(codepoint);
1937 break;
1938 case XML_REGEXP_SYMBOL:
1939 ret = xmlUCSIsCatS(codepoint);
1940 break;
1941 case XML_REGEXP_SYMBOL_MATH:
1942 ret = xmlUCSIsCatSm(codepoint);
1943 break;
1944 case XML_REGEXP_SYMBOL_CURRENCY:
1945 ret = xmlUCSIsCatSc(codepoint);
1946 break;
1947 case XML_REGEXP_SYMBOL_MODIFIER:
1948 ret = xmlUCSIsCatSk(codepoint);
1949 break;
1950 case XML_REGEXP_SYMBOL_OTHERS:
1951 ret = xmlUCSIsCatSo(codepoint);
1952 break;
1953 case XML_REGEXP_OTHER:
1954 ret = xmlUCSIsCatC(codepoint);
1955 break;
1956 case XML_REGEXP_OTHER_CONTROL:
1957 ret = xmlUCSIsCatCc(codepoint);
1958 break;
1959 case XML_REGEXP_OTHER_FORMAT:
1960 ret = xmlUCSIsCatCf(codepoint);
1961 break;
1962 case XML_REGEXP_OTHER_PRIVATE:
1963 ret = xmlUCSIsCatCo(codepoint);
1964 break;
1965 case XML_REGEXP_OTHER_NA:
1966 /* ret = xmlUCSIsCatCn(codepoint); */
1967 /* Seems it doesn't exist anymore in recent Unicode releases */
1968 ret = 0;
1969 break;
1970 case XML_REGEXP_BLOCK_NAME:
1971 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
1972 break;
1973 }
1974 if (neg)
1975 return(!ret);
1976 return(ret);
1977}
1978
1979static int
1980xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
1981 int i, ret = 0;
1982 xmlRegRangePtr range;
1983
William M. Brack871611b2003-10-18 04:53:14 +00001984 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00001985 return(-1);
1986
1987 switch (atom->type) {
1988 case XML_REGEXP_SUBREG:
1989 case XML_REGEXP_EPSILON:
1990 return(-1);
1991 case XML_REGEXP_CHARVAL:
1992 return(codepoint == atom->codepoint);
1993 case XML_REGEXP_RANGES: {
1994 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00001995
Daniel Veillard4255d502002-04-16 15:50:10 +00001996 for (i = 0;i < atom->nbRanges;i++) {
1997 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00001998 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001999 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2000 0, range->start, range->end,
2001 range->blockName);
2002 if (ret != 0)
2003 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002004 } else if (range->neg) {
2005 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2006 0, range->start, range->end,
2007 range->blockName);
2008 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002009 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002010 else
2011 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002012 } else {
2013 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2014 0, range->start, range->end,
2015 range->blockName);
2016 if (ret != 0)
2017 accept = 1; /* might still be excluded */
2018 }
2019 }
2020 return(accept);
2021 }
2022 case XML_REGEXP_STRING:
2023 printf("TODO: XML_REGEXP_STRING\n");
2024 return(-1);
2025 case XML_REGEXP_ANYCHAR:
2026 case XML_REGEXP_ANYSPACE:
2027 case XML_REGEXP_NOTSPACE:
2028 case XML_REGEXP_INITNAME:
2029 case XML_REGEXP_NOTINITNAME:
2030 case XML_REGEXP_NAMECHAR:
2031 case XML_REGEXP_NOTNAMECHAR:
2032 case XML_REGEXP_DECIMAL:
2033 case XML_REGEXP_NOTDECIMAL:
2034 case XML_REGEXP_REALCHAR:
2035 case XML_REGEXP_NOTREALCHAR:
2036 case XML_REGEXP_LETTER:
2037 case XML_REGEXP_LETTER_UPPERCASE:
2038 case XML_REGEXP_LETTER_LOWERCASE:
2039 case XML_REGEXP_LETTER_TITLECASE:
2040 case XML_REGEXP_LETTER_MODIFIER:
2041 case XML_REGEXP_LETTER_OTHERS:
2042 case XML_REGEXP_MARK:
2043 case XML_REGEXP_MARK_NONSPACING:
2044 case XML_REGEXP_MARK_SPACECOMBINING:
2045 case XML_REGEXP_MARK_ENCLOSING:
2046 case XML_REGEXP_NUMBER:
2047 case XML_REGEXP_NUMBER_DECIMAL:
2048 case XML_REGEXP_NUMBER_LETTER:
2049 case XML_REGEXP_NUMBER_OTHERS:
2050 case XML_REGEXP_PUNCT:
2051 case XML_REGEXP_PUNCT_CONNECTOR:
2052 case XML_REGEXP_PUNCT_DASH:
2053 case XML_REGEXP_PUNCT_OPEN:
2054 case XML_REGEXP_PUNCT_CLOSE:
2055 case XML_REGEXP_PUNCT_INITQUOTE:
2056 case XML_REGEXP_PUNCT_FINQUOTE:
2057 case XML_REGEXP_PUNCT_OTHERS:
2058 case XML_REGEXP_SEPAR:
2059 case XML_REGEXP_SEPAR_SPACE:
2060 case XML_REGEXP_SEPAR_LINE:
2061 case XML_REGEXP_SEPAR_PARA:
2062 case XML_REGEXP_SYMBOL:
2063 case XML_REGEXP_SYMBOL_MATH:
2064 case XML_REGEXP_SYMBOL_CURRENCY:
2065 case XML_REGEXP_SYMBOL_MODIFIER:
2066 case XML_REGEXP_SYMBOL_OTHERS:
2067 case XML_REGEXP_OTHER:
2068 case XML_REGEXP_OTHER_CONTROL:
2069 case XML_REGEXP_OTHER_FORMAT:
2070 case XML_REGEXP_OTHER_PRIVATE:
2071 case XML_REGEXP_OTHER_NA:
2072 case XML_REGEXP_BLOCK_NAME:
2073 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2074 (const xmlChar *)atom->valuep);
2075 if (atom->neg)
2076 ret = !ret;
2077 break;
2078 }
2079 return(ret);
2080}
2081
2082/************************************************************************
2083 * *
2084 * Saving an restoring state of an execution context *
2085 * *
2086 ************************************************************************/
2087
2088#ifdef DEBUG_REGEXP_EXEC
2089static void
2090xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2091 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2092 if (exec->inputStack != NULL) {
2093 int i;
2094 printf(": ");
2095 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2096 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2097 } else {
2098 printf(": %s", &(exec->inputString[exec->index]));
2099 }
2100 printf("\n");
2101}
2102#endif
2103
2104static void
2105xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2106#ifdef DEBUG_REGEXP_EXEC
2107 printf("saving ");
2108 exec->transno++;
2109 xmlFARegDebugExec(exec);
2110 exec->transno--;
2111#endif
2112
2113 if (exec->maxRollbacks == 0) {
2114 exec->maxRollbacks = 4;
2115 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2116 sizeof(xmlRegExecRollback));
2117 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002118 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002119 exec->maxRollbacks = 0;
2120 return;
2121 }
2122 memset(exec->rollbacks, 0,
2123 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2124 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2125 xmlRegExecRollback *tmp;
2126 int len = exec->maxRollbacks;
2127
2128 exec->maxRollbacks *= 2;
2129 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2130 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2131 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002132 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002133 exec->maxRollbacks /= 2;
2134 return;
2135 }
2136 exec->rollbacks = tmp;
2137 tmp = &exec->rollbacks[len];
2138 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2139 }
2140 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2141 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2142 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2143 if (exec->comp->nbCounters > 0) {
2144 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2145 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2146 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2147 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002148 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002149 exec->status = -5;
2150 return;
2151 }
2152 }
2153 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2154 exec->comp->nbCounters * sizeof(int));
2155 }
2156 exec->nbRollbacks++;
2157}
2158
2159static void
2160xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2161 if (exec->nbRollbacks <= 0) {
2162 exec->status = -1;
2163#ifdef DEBUG_REGEXP_EXEC
2164 printf("rollback failed on empty stack\n");
2165#endif
2166 return;
2167 }
2168 exec->nbRollbacks--;
2169 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2170 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2171 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2172 if (exec->comp->nbCounters > 0) {
2173 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2174 fprintf(stderr, "exec save: allocation failed");
2175 exec->status = -6;
2176 return;
2177 }
2178 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2179 exec->comp->nbCounters * sizeof(int));
2180 }
2181
2182#ifdef DEBUG_REGEXP_EXEC
2183 printf("restored ");
2184 xmlFARegDebugExec(exec);
2185#endif
2186}
2187
2188/************************************************************************
2189 * *
2190 * Verifyer, running an input against a compiled regexp *
2191 * *
2192 ************************************************************************/
2193
2194static int
2195xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2196 xmlRegExecCtxt execval;
2197 xmlRegExecCtxtPtr exec = &execval;
2198 int ret, codepoint, len;
2199
2200 exec->inputString = content;
2201 exec->index = 0;
2202 exec->determinist = 1;
2203 exec->maxRollbacks = 0;
2204 exec->nbRollbacks = 0;
2205 exec->rollbacks = NULL;
2206 exec->status = 0;
2207 exec->comp = comp;
2208 exec->state = comp->states[0];
2209 exec->transno = 0;
2210 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002211 exec->inputStack = NULL;
2212 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002213 if (comp->nbCounters > 0) {
2214 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002215 if (exec->counts == NULL) {
2216 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002217 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002218 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002219 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2220 } else
2221 exec->counts = NULL;
2222 while ((exec->status == 0) &&
2223 ((exec->inputString[exec->index] != 0) ||
2224 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2225 xmlRegTransPtr trans;
2226 xmlRegAtomPtr atom;
2227
2228 /*
2229 * End of input on non-terminal state, rollback, however we may
2230 * still have epsilon like transition for counted transitions
2231 * on counters, in that case don't break too early.
2232 */
2233 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2234 goto rollback;
2235
2236 exec->transcount = 0;
2237 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2238 trans = &exec->state->trans[exec->transno];
2239 if (trans->to < 0)
2240 continue;
2241 atom = trans->atom;
2242 ret = 0;
2243 if (trans->count >= 0) {
2244 int count;
2245 xmlRegCounterPtr counter;
2246
2247 /*
2248 * A counted transition.
2249 */
2250
2251 count = exec->counts[trans->count];
2252 counter = &exec->comp->counters[trans->count];
2253#ifdef DEBUG_REGEXP_EXEC
2254 printf("testing count %d: val %d, min %d, max %d\n",
2255 trans->count, count, counter->min, counter->max);
2256#endif
2257 ret = ((count >= counter->min) && (count <= counter->max));
2258 } else if (atom == NULL) {
2259 fprintf(stderr, "epsilon transition left at runtime\n");
2260 exec->status = -2;
2261 break;
2262 } else if (exec->inputString[exec->index] != 0) {
2263 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2264 ret = xmlRegCheckCharacter(atom, codepoint);
2265 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2266 xmlRegStatePtr to = comp->states[trans->to];
2267
2268 /*
2269 * this is a multiple input sequence
2270 */
2271 if (exec->state->nbTrans > exec->transno + 1) {
2272 xmlFARegExecSave(exec);
2273 }
2274 exec->transcount = 1;
2275 do {
2276 /*
2277 * Try to progress as much as possible on the input
2278 */
2279 if (exec->transcount == atom->max) {
2280 break;
2281 }
2282 exec->index += len;
2283 /*
2284 * End of input: stop here
2285 */
2286 if (exec->inputString[exec->index] == 0) {
2287 exec->index -= len;
2288 break;
2289 }
2290 if (exec->transcount >= atom->min) {
2291 int transno = exec->transno;
2292 xmlRegStatePtr state = exec->state;
2293
2294 /*
2295 * The transition is acceptable save it
2296 */
2297 exec->transno = -1; /* trick */
2298 exec->state = to;
2299 xmlFARegExecSave(exec);
2300 exec->transno = transno;
2301 exec->state = state;
2302 }
2303 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2304 len);
2305 ret = xmlRegCheckCharacter(atom, codepoint);
2306 exec->transcount++;
2307 } while (ret == 1);
2308 if (exec->transcount < atom->min)
2309 ret = 0;
2310
2311 /*
2312 * If the last check failed but one transition was found
2313 * possible, rollback
2314 */
2315 if (ret < 0)
2316 ret = 0;
2317 if (ret == 0) {
2318 goto rollback;
2319 }
2320 }
2321 }
2322 if (ret == 1) {
2323 if (exec->state->nbTrans > exec->transno + 1) {
2324 xmlFARegExecSave(exec);
2325 }
2326 if (trans->counter >= 0) {
2327#ifdef DEBUG_REGEXP_EXEC
2328 printf("Increasing count %d\n", trans->counter);
2329#endif
2330 exec->counts[trans->counter]++;
2331 }
2332#ifdef DEBUG_REGEXP_EXEC
2333 printf("entering state %d\n", trans->to);
2334#endif
2335 exec->state = comp->states[trans->to];
2336 exec->transno = 0;
2337 if (trans->atom != NULL) {
2338 exec->index += len;
2339 }
2340 goto progress;
2341 } else if (ret < 0) {
2342 exec->status = -4;
2343 break;
2344 }
2345 }
2346 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2347rollback:
2348 /*
2349 * Failed to find a way out
2350 */
2351 exec->determinist = 0;
2352 xmlFARegExecRollBack(exec);
2353 }
2354progress:
2355 continue;
2356 }
2357 if (exec->rollbacks != NULL) {
2358 if (exec->counts != NULL) {
2359 int i;
2360
2361 for (i = 0;i < exec->maxRollbacks;i++)
2362 if (exec->rollbacks[i].counts != NULL)
2363 xmlFree(exec->rollbacks[i].counts);
2364 }
2365 xmlFree(exec->rollbacks);
2366 }
2367 if (exec->counts != NULL)
2368 xmlFree(exec->counts);
2369 if (exec->status == 0)
2370 return(1);
2371 if (exec->status == -1)
2372 return(0);
2373 return(exec->status);
2374}
2375
2376/************************************************************************
2377 * *
2378 * Progressive interface to the verifyer one atom at a time *
2379 * *
2380 ************************************************************************/
2381
2382/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002383 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002384 * @comp: a precompiled regular expression
2385 * @callback: a callback function used for handling progresses in the
2386 * automata matching phase
2387 * @data: the context data associated to the callback in this context
2388 *
2389 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002390 *
2391 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002392 */
2393xmlRegExecCtxtPtr
2394xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2395 xmlRegExecCtxtPtr exec;
2396
2397 if (comp == NULL)
2398 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002399 if ((comp->compact == NULL) && (comp->states == NULL))
2400 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002401 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2402 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002403 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002404 return(NULL);
2405 }
2406 memset(exec, 0, sizeof(xmlRegExecCtxt));
2407 exec->inputString = NULL;
2408 exec->index = 0;
2409 exec->determinist = 1;
2410 exec->maxRollbacks = 0;
2411 exec->nbRollbacks = 0;
2412 exec->rollbacks = NULL;
2413 exec->status = 0;
2414 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002415 if (comp->compact == NULL)
2416 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002417 exec->transno = 0;
2418 exec->transcount = 0;
2419 exec->callback = callback;
2420 exec->data = data;
2421 if (comp->nbCounters > 0) {
2422 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2423 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002424 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002425 xmlFree(exec);
2426 return(NULL);
2427 }
2428 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2429 } else
2430 exec->counts = NULL;
2431 exec->inputStackMax = 0;
2432 exec->inputStackNr = 0;
2433 exec->inputStack = NULL;
2434 return(exec);
2435}
2436
2437/**
2438 * xmlRegFreeExecCtxt:
2439 * @exec: a regular expression evaulation context
2440 *
2441 * Free the structures associated to a regular expression evaulation context.
2442 */
2443void
2444xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2445 if (exec == NULL)
2446 return;
2447
2448 if (exec->rollbacks != NULL) {
2449 if (exec->counts != NULL) {
2450 int i;
2451
2452 for (i = 0;i < exec->maxRollbacks;i++)
2453 if (exec->rollbacks[i].counts != NULL)
2454 xmlFree(exec->rollbacks[i].counts);
2455 }
2456 xmlFree(exec->rollbacks);
2457 }
2458 if (exec->counts != NULL)
2459 xmlFree(exec->counts);
2460 if (exec->inputStack != NULL) {
2461 int i;
2462
Daniel Veillard32370232002-10-16 14:08:14 +00002463 for (i = 0;i < exec->inputStackNr;i++) {
2464 if (exec->inputStack[i].value != NULL)
2465 xmlFree(exec->inputStack[i].value);
2466 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002467 xmlFree(exec->inputStack);
2468 }
2469 xmlFree(exec);
2470}
2471
2472static void
2473xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2474 void *data) {
2475#ifdef DEBUG_PUSH
2476 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2477#endif
2478 if (exec->inputStackMax == 0) {
2479 exec->inputStackMax = 4;
2480 exec->inputStack = (xmlRegInputTokenPtr)
2481 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2482 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002483 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002484 exec->inputStackMax = 0;
2485 return;
2486 }
2487 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2488 xmlRegInputTokenPtr tmp;
2489
2490 exec->inputStackMax *= 2;
2491 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2492 exec->inputStackMax * sizeof(xmlRegInputToken));
2493 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002494 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002495 exec->inputStackMax /= 2;
2496 return;
2497 }
2498 exec->inputStack = tmp;
2499 }
2500 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2501 exec->inputStack[exec->inputStackNr].data = data;
2502 exec->inputStackNr++;
2503 exec->inputStack[exec->inputStackNr].value = NULL;
2504 exec->inputStack[exec->inputStackNr].data = NULL;
2505}
2506
2507
2508/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002509 * xmlRegCompactPushString:
2510 * @exec: a regexp execution context
2511 * @comp: the precompiled exec with a compact table
2512 * @value: a string token input
2513 * @data: data associated to the token to reuse in callbacks
2514 *
2515 * Push one input token in the execution context
2516 *
2517 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2518 * a negative value in case of error.
2519 */
2520static int
2521xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2522 xmlRegexpPtr comp,
2523 const xmlChar *value,
2524 void *data) {
2525 int state = exec->index;
2526 int i, target;
2527
2528 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2529 return(-1);
2530
2531 if (value == NULL) {
2532 /*
2533 * are we at a final state ?
2534 */
2535 if (comp->compact[state * (comp->nbstrings + 1)] ==
2536 XML_REGEXP_FINAL_STATE)
2537 return(1);
2538 return(0);
2539 }
2540
2541#ifdef DEBUG_PUSH
2542 printf("value pushed: %s\n", value);
2543#endif
2544
2545 /*
2546 * Examine all outside transition from current state
2547 */
2548 for (i = 0;i < comp->nbstrings;i++) {
2549 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2550 if ((target > 0) && (target <= comp->nbstates)) {
2551 target--; /* to avoid 0 */
2552 if (xmlStrEqual(comp->stringMap[i], value)) {
2553 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002554 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2555 exec->callback(exec->data, value,
2556 comp->transdata[state * comp->nbstrings + i], data);
2557 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002558#ifdef DEBUG_PUSH
2559 printf("entering state %d\n", target);
2560#endif
2561 if (comp->compact[target * (comp->nbstrings + 1)] ==
2562 XML_REGEXP_FINAL_STATE)
2563 return(1);
2564 return(0);
2565 }
2566 }
2567 }
2568 /*
2569 * Failed to find an exit transition out from current state for the
2570 * current token
2571 */
2572#ifdef DEBUG_PUSH
2573 printf("failed to find a transition for %s on state %d\n", value, state);
2574#endif
2575 exec->status = -1;
2576 return(-1);
2577}
2578
2579/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002580 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002581 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002582 * @value: a string token input
2583 * @data: data associated to the token to reuse in callbacks
2584 *
2585 * Push one input token in the execution context
2586 *
2587 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2588 * a negative value in case of error.
2589 */
2590int
2591xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2592 void *data) {
2593 xmlRegTransPtr trans;
2594 xmlRegAtomPtr atom;
2595 int ret;
2596 int final = 0;
2597
2598 if (exec == NULL)
2599 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002600 if (exec->comp == NULL)
2601 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002602 if (exec->status != 0)
2603 return(exec->status);
2604
Daniel Veillard23e73572002-09-19 19:56:43 +00002605 if (exec->comp->compact != NULL)
2606 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2607
Daniel Veillard4255d502002-04-16 15:50:10 +00002608 if (value == NULL) {
2609 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2610 return(1);
2611 final = 1;
2612 }
2613
2614#ifdef DEBUG_PUSH
2615 printf("value pushed: %s\n", value);
2616#endif
2617 /*
2618 * If we have an active rollback stack push the new value there
2619 * and get back to where we were left
2620 */
2621 if ((value != NULL) && (exec->inputStackNr > 0)) {
2622 xmlFARegExecSaveInputString(exec, value, data);
2623 value = exec->inputStack[exec->index].value;
2624 data = exec->inputStack[exec->index].data;
2625#ifdef DEBUG_PUSH
2626 printf("value loaded: %s\n", value);
2627#endif
2628 }
2629
2630 while ((exec->status == 0) &&
2631 ((value != NULL) ||
2632 ((final == 1) &&
2633 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2634
2635 /*
2636 * End of input on non-terminal state, rollback, however we may
2637 * still have epsilon like transition for counted transitions
2638 * on counters, in that case don't break too early.
2639 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002640 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002641 goto rollback;
2642
2643 exec->transcount = 0;
2644 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2645 trans = &exec->state->trans[exec->transno];
2646 if (trans->to < 0)
2647 continue;
2648 atom = trans->atom;
2649 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002650 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2651 int i;
2652 int count;
2653 xmlRegTransPtr t;
2654 xmlRegCounterPtr counter;
2655
2656 ret = 0;
2657
2658#ifdef DEBUG_PUSH
2659 printf("testing all lax %d\n", trans->count);
2660#endif
2661 /*
2662 * Check all counted transitions from the current state
2663 */
2664 if ((value == NULL) && (final)) {
2665 ret = 1;
2666 } else if (value != NULL) {
2667 for (i = 0;i < exec->state->nbTrans;i++) {
2668 t = &exec->state->trans[i];
2669 if ((t->counter < 0) || (t == trans))
2670 continue;
2671 counter = &exec->comp->counters[t->counter];
2672 count = exec->counts[t->counter];
2673 if ((count < counter->max) &&
2674 (t->atom != NULL) &&
2675 (xmlStrEqual(value, t->atom->valuep))) {
2676 ret = 0;
2677 break;
2678 }
2679 if ((count >= counter->min) &&
2680 (count < counter->max) &&
2681 (xmlStrEqual(value, t->atom->valuep))) {
2682 ret = 1;
2683 break;
2684 }
2685 }
2686 }
2687 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002688 int i;
2689 int count;
2690 xmlRegTransPtr t;
2691 xmlRegCounterPtr counter;
2692
2693 ret = 1;
2694
2695#ifdef DEBUG_PUSH
2696 printf("testing all %d\n", trans->count);
2697#endif
2698 /*
2699 * Check all counted transitions from the current state
2700 */
2701 for (i = 0;i < exec->state->nbTrans;i++) {
2702 t = &exec->state->trans[i];
2703 if ((t->counter < 0) || (t == trans))
2704 continue;
2705 counter = &exec->comp->counters[t->counter];
2706 count = exec->counts[t->counter];
2707 if ((count < counter->min) || (count > counter->max)) {
2708 ret = 0;
2709 break;
2710 }
2711 }
2712 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002713 int count;
2714 xmlRegCounterPtr counter;
2715
2716 /*
2717 * A counted transition.
2718 */
2719
2720 count = exec->counts[trans->count];
2721 counter = &exec->comp->counters[trans->count];
2722#ifdef DEBUG_PUSH
2723 printf("testing count %d: val %d, min %d, max %d\n",
2724 trans->count, count, counter->min, counter->max);
2725#endif
2726 ret = ((count >= counter->min) && (count <= counter->max));
2727 } else if (atom == NULL) {
2728 fprintf(stderr, "epsilon transition left at runtime\n");
2729 exec->status = -2;
2730 break;
2731 } else if (value != NULL) {
2732 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002733 if ((ret == 1) && (trans->counter >= 0)) {
2734 xmlRegCounterPtr counter;
2735 int count;
2736
2737 count = exec->counts[trans->counter];
2738 counter = &exec->comp->counters[trans->counter];
2739 if (count >= counter->max)
2740 ret = 0;
2741 }
2742
Daniel Veillard4255d502002-04-16 15:50:10 +00002743 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2744 xmlRegStatePtr to = exec->comp->states[trans->to];
2745
2746 /*
2747 * this is a multiple input sequence
2748 */
2749 if (exec->state->nbTrans > exec->transno + 1) {
2750 if (exec->inputStackNr <= 0) {
2751 xmlFARegExecSaveInputString(exec, value, data);
2752 }
2753 xmlFARegExecSave(exec);
2754 }
2755 exec->transcount = 1;
2756 do {
2757 /*
2758 * Try to progress as much as possible on the input
2759 */
2760 if (exec->transcount == atom->max) {
2761 break;
2762 }
2763 exec->index++;
2764 value = exec->inputStack[exec->index].value;
2765 data = exec->inputStack[exec->index].data;
2766#ifdef DEBUG_PUSH
2767 printf("value loaded: %s\n", value);
2768#endif
2769
2770 /*
2771 * End of input: stop here
2772 */
2773 if (value == NULL) {
2774 exec->index --;
2775 break;
2776 }
2777 if (exec->transcount >= atom->min) {
2778 int transno = exec->transno;
2779 xmlRegStatePtr state = exec->state;
2780
2781 /*
2782 * The transition is acceptable save it
2783 */
2784 exec->transno = -1; /* trick */
2785 exec->state = to;
2786 if (exec->inputStackNr <= 0) {
2787 xmlFARegExecSaveInputString(exec, value, data);
2788 }
2789 xmlFARegExecSave(exec);
2790 exec->transno = transno;
2791 exec->state = state;
2792 }
2793 ret = xmlStrEqual(value, atom->valuep);
2794 exec->transcount++;
2795 } while (ret == 1);
2796 if (exec->transcount < atom->min)
2797 ret = 0;
2798
2799 /*
2800 * If the last check failed but one transition was found
2801 * possible, rollback
2802 */
2803 if (ret < 0)
2804 ret = 0;
2805 if (ret == 0) {
2806 goto rollback;
2807 }
2808 }
2809 }
2810 if (ret == 1) {
2811 if ((exec->callback != NULL) && (atom != NULL)) {
2812 exec->callback(exec->data, atom->valuep,
2813 atom->data, data);
2814 }
2815 if (exec->state->nbTrans > exec->transno + 1) {
2816 if (exec->inputStackNr <= 0) {
2817 xmlFARegExecSaveInputString(exec, value, data);
2818 }
2819 xmlFARegExecSave(exec);
2820 }
2821 if (trans->counter >= 0) {
2822#ifdef DEBUG_PUSH
2823 printf("Increasing count %d\n", trans->counter);
2824#endif
2825 exec->counts[trans->counter]++;
2826 }
2827#ifdef DEBUG_PUSH
2828 printf("entering state %d\n", trans->to);
2829#endif
2830 exec->state = exec->comp->states[trans->to];
2831 exec->transno = 0;
2832 if (trans->atom != NULL) {
2833 if (exec->inputStack != NULL) {
2834 exec->index++;
2835 if (exec->index < exec->inputStackNr) {
2836 value = exec->inputStack[exec->index].value;
2837 data = exec->inputStack[exec->index].data;
2838#ifdef DEBUG_PUSH
2839 printf("value loaded: %s\n", value);
2840#endif
2841 } else {
2842 value = NULL;
2843 data = NULL;
2844#ifdef DEBUG_PUSH
2845 printf("end of input\n");
2846#endif
2847 }
2848 } else {
2849 value = NULL;
2850 data = NULL;
2851#ifdef DEBUG_PUSH
2852 printf("end of input\n");
2853#endif
2854 }
2855 }
2856 goto progress;
2857 } else if (ret < 0) {
2858 exec->status = -4;
2859 break;
2860 }
2861 }
2862 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2863rollback:
2864 /*
2865 * Failed to find a way out
2866 */
2867 exec->determinist = 0;
2868 xmlFARegExecRollBack(exec);
2869 if (exec->status == 0) {
2870 value = exec->inputStack[exec->index].value;
2871 data = exec->inputStack[exec->index].data;
2872#ifdef DEBUG_PUSH
2873 printf("value loaded: %s\n", value);
2874#endif
2875 }
2876 }
2877progress:
2878 continue;
2879 }
2880 if (exec->status == 0) {
2881 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2882 }
2883 return(exec->status);
2884}
2885
Daniel Veillard52b48c72003-04-13 19:53:42 +00002886/**
2887 * xmlRegExecPushString2:
2888 * @exec: a regexp execution context or NULL to indicate the end
2889 * @value: the first string token input
2890 * @value2: the second string token input
2891 * @data: data associated to the token to reuse in callbacks
2892 *
2893 * Push one input token in the execution context
2894 *
2895 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2896 * a negative value in case of error.
2897 */
2898int
2899xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
2900 const xmlChar *value2, void *data) {
2901 xmlChar buf[150];
2902 int lenn, lenp, ret;
2903 xmlChar *str;
2904
2905 if (exec == NULL)
2906 return(-1);
2907 if (exec->comp == NULL)
2908 return(-1);
2909 if (exec->status != 0)
2910 return(exec->status);
2911
2912 if (value2 == NULL)
2913 return(xmlRegExecPushString(exec, value, data));
2914
2915 lenn = strlen((char *) value2);
2916 lenp = strlen((char *) value);
2917
2918 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002919 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002920 if (str == NULL) {
2921 exec->status = -1;
2922 return(-1);
2923 }
2924 } else {
2925 str = buf;
2926 }
2927 memcpy(&str[0], value, lenp);
2928 str[lenp] = '|';
2929 memcpy(&str[lenp + 1], value2, lenn);
2930 str[lenn + lenp + 1] = 0;
2931
2932 if (exec->comp->compact != NULL)
2933 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
2934 else
2935 ret = xmlRegExecPushString(exec, str, data);
2936
2937 if (str != buf)
2938 xmlFree(buf);
2939 return(ret);
2940}
2941
Daniel Veillard4255d502002-04-16 15:50:10 +00002942#if 0
2943static int
2944xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2945 xmlRegTransPtr trans;
2946 xmlRegAtomPtr atom;
2947 int ret;
2948 int codepoint, len;
2949
2950 if (exec == NULL)
2951 return(-1);
2952 if (exec->status != 0)
2953 return(exec->status);
2954
2955 while ((exec->status == 0) &&
2956 ((exec->inputString[exec->index] != 0) ||
2957 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2958
2959 /*
2960 * End of input on non-terminal state, rollback, however we may
2961 * still have epsilon like transition for counted transitions
2962 * on counters, in that case don't break too early.
2963 */
2964 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2965 goto rollback;
2966
2967 exec->transcount = 0;
2968 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2969 trans = &exec->state->trans[exec->transno];
2970 if (trans->to < 0)
2971 continue;
2972 atom = trans->atom;
2973 ret = 0;
2974 if (trans->count >= 0) {
2975 int count;
2976 xmlRegCounterPtr counter;
2977
2978 /*
2979 * A counted transition.
2980 */
2981
2982 count = exec->counts[trans->count];
2983 counter = &exec->comp->counters[trans->count];
2984#ifdef DEBUG_REGEXP_EXEC
2985 printf("testing count %d: val %d, min %d, max %d\n",
2986 trans->count, count, counter->min, counter->max);
2987#endif
2988 ret = ((count >= counter->min) && (count <= counter->max));
2989 } else if (atom == NULL) {
2990 fprintf(stderr, "epsilon transition left at runtime\n");
2991 exec->status = -2;
2992 break;
2993 } else if (exec->inputString[exec->index] != 0) {
2994 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2995 ret = xmlRegCheckCharacter(atom, codepoint);
2996 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2997 xmlRegStatePtr to = exec->comp->states[trans->to];
2998
2999 /*
3000 * this is a multiple input sequence
3001 */
3002 if (exec->state->nbTrans > exec->transno + 1) {
3003 xmlFARegExecSave(exec);
3004 }
3005 exec->transcount = 1;
3006 do {
3007 /*
3008 * Try to progress as much as possible on the input
3009 */
3010 if (exec->transcount == atom->max) {
3011 break;
3012 }
3013 exec->index += len;
3014 /*
3015 * End of input: stop here
3016 */
3017 if (exec->inputString[exec->index] == 0) {
3018 exec->index -= len;
3019 break;
3020 }
3021 if (exec->transcount >= atom->min) {
3022 int transno = exec->transno;
3023 xmlRegStatePtr state = exec->state;
3024
3025 /*
3026 * The transition is acceptable save it
3027 */
3028 exec->transno = -1; /* trick */
3029 exec->state = to;
3030 xmlFARegExecSave(exec);
3031 exec->transno = transno;
3032 exec->state = state;
3033 }
3034 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3035 len);
3036 ret = xmlRegCheckCharacter(atom, codepoint);
3037 exec->transcount++;
3038 } while (ret == 1);
3039 if (exec->transcount < atom->min)
3040 ret = 0;
3041
3042 /*
3043 * If the last check failed but one transition was found
3044 * possible, rollback
3045 */
3046 if (ret < 0)
3047 ret = 0;
3048 if (ret == 0) {
3049 goto rollback;
3050 }
3051 }
3052 }
3053 if (ret == 1) {
3054 if (exec->state->nbTrans > exec->transno + 1) {
3055 xmlFARegExecSave(exec);
3056 }
3057 if (trans->counter >= 0) {
3058#ifdef DEBUG_REGEXP_EXEC
3059 printf("Increasing count %d\n", trans->counter);
3060#endif
3061 exec->counts[trans->counter]++;
3062 }
3063#ifdef DEBUG_REGEXP_EXEC
3064 printf("entering state %d\n", trans->to);
3065#endif
3066 exec->state = exec->comp->states[trans->to];
3067 exec->transno = 0;
3068 if (trans->atom != NULL) {
3069 exec->index += len;
3070 }
3071 goto progress;
3072 } else if (ret < 0) {
3073 exec->status = -4;
3074 break;
3075 }
3076 }
3077 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3078rollback:
3079 /*
3080 * Failed to find a way out
3081 */
3082 exec->determinist = 0;
3083 xmlFARegExecRollBack(exec);
3084 }
3085progress:
3086 continue;
3087 }
3088}
3089#endif
3090/************************************************************************
3091 * *
3092 * Parser for the Shemas Datatype Regular Expressions *
3093 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3094 * *
3095 ************************************************************************/
3096
3097/**
3098 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003099 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003100 *
3101 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3102 */
3103static int
3104xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3105 int cur;
3106 int len;
3107
3108 cur = CUR_SCHAR(ctxt->cur, len);
3109 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3110 (cur == '*') || (cur == '+') || (cur == '(') ||
3111 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3112 (cur == 0x5D) || (cur == 0))
3113 return(-1);
3114 return(cur);
3115}
3116
3117/**
3118 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003119 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003120 *
3121 * [27] charProp ::= IsCategory | IsBlock
3122 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3123 * Separators | Symbols | Others
3124 * [29] Letters ::= 'L' [ultmo]?
3125 * [30] Marks ::= 'M' [nce]?
3126 * [31] Numbers ::= 'N' [dlo]?
3127 * [32] Punctuation ::= 'P' [cdseifo]?
3128 * [33] Separators ::= 'Z' [slp]?
3129 * [34] Symbols ::= 'S' [mcko]?
3130 * [35] Others ::= 'C' [cfon]?
3131 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3132 */
3133static void
3134xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3135 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003136 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003137 xmlChar *blockName = NULL;
3138
3139 cur = CUR;
3140 if (cur == 'L') {
3141 NEXT;
3142 cur = CUR;
3143 if (cur == 'u') {
3144 NEXT;
3145 type = XML_REGEXP_LETTER_UPPERCASE;
3146 } else if (cur == 'l') {
3147 NEXT;
3148 type = XML_REGEXP_LETTER_LOWERCASE;
3149 } else if (cur == 't') {
3150 NEXT;
3151 type = XML_REGEXP_LETTER_TITLECASE;
3152 } else if (cur == 'm') {
3153 NEXT;
3154 type = XML_REGEXP_LETTER_MODIFIER;
3155 } else if (cur == 'o') {
3156 NEXT;
3157 type = XML_REGEXP_LETTER_OTHERS;
3158 } else {
3159 type = XML_REGEXP_LETTER;
3160 }
3161 } else if (cur == 'M') {
3162 NEXT;
3163 cur = CUR;
3164 if (cur == 'n') {
3165 NEXT;
3166 /* nonspacing */
3167 type = XML_REGEXP_MARK_NONSPACING;
3168 } else if (cur == 'c') {
3169 NEXT;
3170 /* spacing combining */
3171 type = XML_REGEXP_MARK_SPACECOMBINING;
3172 } else if (cur == 'e') {
3173 NEXT;
3174 /* enclosing */
3175 type = XML_REGEXP_MARK_ENCLOSING;
3176 } else {
3177 /* all marks */
3178 type = XML_REGEXP_MARK;
3179 }
3180 } else if (cur == 'N') {
3181 NEXT;
3182 cur = CUR;
3183 if (cur == 'd') {
3184 NEXT;
3185 /* digital */
3186 type = XML_REGEXP_NUMBER_DECIMAL;
3187 } else if (cur == 'l') {
3188 NEXT;
3189 /* letter */
3190 type = XML_REGEXP_NUMBER_LETTER;
3191 } else if (cur == 'o') {
3192 NEXT;
3193 /* other */
3194 type = XML_REGEXP_NUMBER_OTHERS;
3195 } else {
3196 /* all numbers */
3197 type = XML_REGEXP_NUMBER;
3198 }
3199 } else if (cur == 'P') {
3200 NEXT;
3201 cur = CUR;
3202 if (cur == 'c') {
3203 NEXT;
3204 /* connector */
3205 type = XML_REGEXP_PUNCT_CONNECTOR;
3206 } else if (cur == 'd') {
3207 NEXT;
3208 /* dash */
3209 type = XML_REGEXP_PUNCT_DASH;
3210 } else if (cur == 's') {
3211 NEXT;
3212 /* open */
3213 type = XML_REGEXP_PUNCT_OPEN;
3214 } else if (cur == 'e') {
3215 NEXT;
3216 /* close */
3217 type = XML_REGEXP_PUNCT_CLOSE;
3218 } else if (cur == 'i') {
3219 NEXT;
3220 /* initial quote */
3221 type = XML_REGEXP_PUNCT_INITQUOTE;
3222 } else if (cur == 'f') {
3223 NEXT;
3224 /* final quote */
3225 type = XML_REGEXP_PUNCT_FINQUOTE;
3226 } else if (cur == 'o') {
3227 NEXT;
3228 /* other */
3229 type = XML_REGEXP_PUNCT_OTHERS;
3230 } else {
3231 /* all punctuation */
3232 type = XML_REGEXP_PUNCT;
3233 }
3234 } else if (cur == 'Z') {
3235 NEXT;
3236 cur = CUR;
3237 if (cur == 's') {
3238 NEXT;
3239 /* space */
3240 type = XML_REGEXP_SEPAR_SPACE;
3241 } else if (cur == 'l') {
3242 NEXT;
3243 /* line */
3244 type = XML_REGEXP_SEPAR_LINE;
3245 } else if (cur == 'p') {
3246 NEXT;
3247 /* paragraph */
3248 type = XML_REGEXP_SEPAR_PARA;
3249 } else {
3250 /* all separators */
3251 type = XML_REGEXP_SEPAR;
3252 }
3253 } else if (cur == 'S') {
3254 NEXT;
3255 cur = CUR;
3256 if (cur == 'm') {
3257 NEXT;
3258 type = XML_REGEXP_SYMBOL_MATH;
3259 /* math */
3260 } else if (cur == 'c') {
3261 NEXT;
3262 type = XML_REGEXP_SYMBOL_CURRENCY;
3263 /* currency */
3264 } else if (cur == 'k') {
3265 NEXT;
3266 type = XML_REGEXP_SYMBOL_MODIFIER;
3267 /* modifiers */
3268 } else if (cur == 'o') {
3269 NEXT;
3270 type = XML_REGEXP_SYMBOL_OTHERS;
3271 /* other */
3272 } else {
3273 /* all symbols */
3274 type = XML_REGEXP_SYMBOL;
3275 }
3276 } else if (cur == 'C') {
3277 NEXT;
3278 cur = CUR;
3279 if (cur == 'c') {
3280 NEXT;
3281 /* control */
3282 type = XML_REGEXP_OTHER_CONTROL;
3283 } else if (cur == 'f') {
3284 NEXT;
3285 /* format */
3286 type = XML_REGEXP_OTHER_FORMAT;
3287 } else if (cur == 'o') {
3288 NEXT;
3289 /* private use */
3290 type = XML_REGEXP_OTHER_PRIVATE;
3291 } else if (cur == 'n') {
3292 NEXT;
3293 /* not assigned */
3294 type = XML_REGEXP_OTHER_NA;
3295 } else {
3296 /* all others */
3297 type = XML_REGEXP_OTHER;
3298 }
3299 } else if (cur == 'I') {
3300 const xmlChar *start;
3301 NEXT;
3302 cur = CUR;
3303 if (cur != 's') {
3304 ERROR("IsXXXX expected");
3305 return;
3306 }
3307 NEXT;
3308 start = ctxt->cur;
3309 cur = CUR;
3310 if (((cur >= 'a') && (cur <= 'z')) ||
3311 ((cur >= 'A') && (cur <= 'Z')) ||
3312 ((cur >= '0') && (cur <= '9')) ||
3313 (cur == 0x2D)) {
3314 NEXT;
3315 cur = CUR;
3316 while (((cur >= 'a') && (cur <= 'z')) ||
3317 ((cur >= 'A') && (cur <= 'Z')) ||
3318 ((cur >= '0') && (cur <= '9')) ||
3319 (cur == 0x2D)) {
3320 NEXT;
3321 cur = CUR;
3322 }
3323 }
3324 type = XML_REGEXP_BLOCK_NAME;
3325 blockName = xmlStrndup(start, ctxt->cur - start);
3326 } else {
3327 ERROR("Unknown char property");
3328 return;
3329 }
3330 if (ctxt->atom == NULL) {
3331 ctxt->atom = xmlRegNewAtom(ctxt, type);
3332 if (ctxt->atom != NULL)
3333 ctxt->atom->valuep = blockName;
3334 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3335 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3336 type, 0, 0, blockName);
3337 }
3338}
3339
3340/**
3341 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003342 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003343 *
3344 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3345 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3346 * [25] catEsc ::= '\p{' charProp '}'
3347 * [26] complEsc ::= '\P{' charProp '}'
3348 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3349 */
3350static void
3351xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3352 int cur;
3353
3354 if (CUR == '.') {
3355 if (ctxt->atom == NULL) {
3356 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3357 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3358 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3359 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3360 }
3361 NEXT;
3362 return;
3363 }
3364 if (CUR != '\\') {
3365 ERROR("Escaped sequence: expecting \\");
3366 return;
3367 }
3368 NEXT;
3369 cur = CUR;
3370 if (cur == 'p') {
3371 NEXT;
3372 if (CUR != '{') {
3373 ERROR("Expecting '{'");
3374 return;
3375 }
3376 NEXT;
3377 xmlFAParseCharProp(ctxt);
3378 if (CUR != '}') {
3379 ERROR("Expecting '}'");
3380 return;
3381 }
3382 NEXT;
3383 } else if (cur == 'P') {
3384 NEXT;
3385 if (CUR != '{') {
3386 ERROR("Expecting '{'");
3387 return;
3388 }
3389 NEXT;
3390 xmlFAParseCharProp(ctxt);
3391 ctxt->atom->neg = 1;
3392 if (CUR != '}') {
3393 ERROR("Expecting '}'");
3394 return;
3395 }
3396 NEXT;
3397 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3398 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3399 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3400 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3401 (cur == 0x5E)) {
3402 if (ctxt->atom == NULL) {
3403 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3404 if (ctxt->atom != NULL)
3405 ctxt->atom->codepoint = cur;
3406 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3407 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3408 XML_REGEXP_CHARVAL, cur, cur, NULL);
3409 }
3410 NEXT;
3411 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3412 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3413 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003414 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003415
3416 switch (cur) {
3417 case 's':
3418 type = XML_REGEXP_ANYSPACE;
3419 break;
3420 case 'S':
3421 type = XML_REGEXP_NOTSPACE;
3422 break;
3423 case 'i':
3424 type = XML_REGEXP_INITNAME;
3425 break;
3426 case 'I':
3427 type = XML_REGEXP_NOTINITNAME;
3428 break;
3429 case 'c':
3430 type = XML_REGEXP_NAMECHAR;
3431 break;
3432 case 'C':
3433 type = XML_REGEXP_NOTNAMECHAR;
3434 break;
3435 case 'd':
3436 type = XML_REGEXP_DECIMAL;
3437 break;
3438 case 'D':
3439 type = XML_REGEXP_NOTDECIMAL;
3440 break;
3441 case 'w':
3442 type = XML_REGEXP_REALCHAR;
3443 break;
3444 case 'W':
3445 type = XML_REGEXP_NOTREALCHAR;
3446 break;
3447 }
3448 NEXT;
3449 if (ctxt->atom == NULL) {
3450 ctxt->atom = xmlRegNewAtom(ctxt, type);
3451 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3452 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3453 type, 0, 0, NULL);
3454 }
3455 }
3456}
3457
3458/**
3459 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003460 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003461 *
3462 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3463 */
3464static int
3465xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3466 int ret = 0, cur;
3467
3468 if ((CUR != '&') || (NXT(1) != '#'))
3469 return(-1);
3470 NEXT;
3471 NEXT;
3472 cur = CUR;
3473 if (cur == 'x') {
3474 NEXT;
3475 cur = CUR;
3476 if (((cur >= '0') && (cur <= '9')) ||
3477 ((cur >= 'a') && (cur <= 'f')) ||
3478 ((cur >= 'A') && (cur <= 'F'))) {
3479 while (((cur >= '0') && (cur <= '9')) ||
3480 ((cur >= 'A') && (cur <= 'F'))) {
3481 if ((cur >= '0') && (cur <= '9'))
3482 ret = ret * 16 + cur - '0';
3483 else if ((cur >= 'a') && (cur <= 'f'))
3484 ret = ret * 16 + 10 + (cur - 'a');
3485 else
3486 ret = ret * 16 + 10 + (cur - 'A');
3487 NEXT;
3488 cur = CUR;
3489 }
3490 } else {
3491 ERROR("Char ref: expecting [0-9A-F]");
3492 return(-1);
3493 }
3494 } else {
3495 if ((cur >= '0') && (cur <= '9')) {
3496 while ((cur >= '0') && (cur <= '9')) {
3497 ret = ret * 10 + cur - '0';
3498 NEXT;
3499 cur = CUR;
3500 }
3501 } else {
3502 ERROR("Char ref: expecting [0-9]");
3503 return(-1);
3504 }
3505 }
3506 if (cur != ';') {
3507 ERROR("Char ref: expecting ';'");
3508 return(-1);
3509 } else {
3510 NEXT;
3511 }
3512 return(ret);
3513}
3514
3515/**
3516 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003517 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003518 *
3519 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3520 * [18] seRange ::= charOrEsc '-' charOrEsc
3521 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3522 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3523 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3524 */
3525static void
3526xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
3527 int cur;
3528 int start = -1;
3529 int end = -1;
3530
3531 if ((CUR == '&') && (NXT(1) == '#')) {
3532 end = start = xmlFAParseCharRef(ctxt);
3533 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3534 XML_REGEXP_CHARVAL, start, end, NULL);
3535 return;
3536 }
3537 cur = CUR;
3538 if (cur == '\\') {
3539 NEXT;
3540 cur = CUR;
3541 switch (cur) {
3542 case 'n': start = 0xA; break;
3543 case 'r': start = 0xD; break;
3544 case 't': start = 0x9; break;
3545 case '\\': case '|': case '.': case '-': case '^': case '?':
3546 case '*': case '+': case '{': case '}': case '(': case ')':
3547 case '[': case ']':
3548 start = cur; break;
3549 default:
3550 ERROR("Invalid escape value");
3551 return;
3552 }
3553 end = start;
3554 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3555 end = start = cur;
3556 } else {
3557 ERROR("Expecting a char range");
3558 return;
3559 }
3560 NEXT;
3561 if (start == '-') {
3562 return;
3563 }
3564 cur = CUR;
3565 if (cur != '-') {
3566 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3567 XML_REGEXP_CHARVAL, start, end, NULL);
3568 return;
3569 }
3570 NEXT;
3571 cur = CUR;
3572 if (cur == '\\') {
3573 NEXT;
3574 cur = CUR;
3575 switch (cur) {
3576 case 'n': end = 0xA; break;
3577 case 'r': end = 0xD; break;
3578 case 't': end = 0x9; break;
3579 case '\\': case '|': case '.': case '-': case '^': case '?':
3580 case '*': case '+': case '{': case '}': case '(': case ')':
3581 case '[': case ']':
3582 end = cur; break;
3583 default:
3584 ERROR("Invalid escape value");
3585 return;
3586 }
3587 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3588 end = cur;
3589 } else {
3590 ERROR("Expecting the end of a char range");
3591 return;
3592 }
3593 NEXT;
3594 /* TODO check that the values are acceptable character ranges for XML */
3595 if (end < start) {
3596 ERROR("End of range is before start of range");
3597 } else {
3598 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3599 XML_REGEXP_CHARVAL, start, end, NULL);
3600 }
3601 return;
3602}
3603
3604/**
3605 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003606 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003607 *
3608 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3609 */
3610static void
3611xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3612 do {
3613 if ((CUR == '\\') || (CUR == '.')) {
3614 xmlFAParseCharClassEsc(ctxt);
3615 } else {
3616 xmlFAParseCharRange(ctxt);
3617 }
3618 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3619 (ctxt->error == 0));
3620}
3621
3622/**
3623 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003624 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003625 *
3626 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3627 * [15] negCharGroup ::= '^' posCharGroup
3628 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3629 * [12] charClassExpr ::= '[' charGroup ']'
3630 */
3631static void
3632xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3633 int n = ctxt->neg;
3634 while ((CUR != ']') && (ctxt->error == 0)) {
3635 if (CUR == '^') {
3636 int neg = ctxt->neg;
3637
3638 NEXT;
3639 ctxt->neg = !ctxt->neg;
3640 xmlFAParsePosCharGroup(ctxt);
3641 ctxt->neg = neg;
3642 } else if (CUR == '-') {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003643 int neg = ctxt->neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00003644 NEXT;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003645 ctxt->neg = 2;
Daniel Veillard4255d502002-04-16 15:50:10 +00003646 if (CUR != '[') {
3647 ERROR("charClassExpr: '[' expected");
3648 break;
3649 }
3650 NEXT;
3651 xmlFAParseCharGroup(ctxt);
3652 if (CUR == ']') {
3653 NEXT;
3654 } else {
3655 ERROR("charClassExpr: ']' expected");
3656 break;
3657 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003658 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00003659 break;
3660 } else if (CUR != ']') {
3661 xmlFAParsePosCharGroup(ctxt);
3662 }
3663 }
3664 ctxt->neg = n;
3665}
3666
3667/**
3668 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003669 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003670 *
3671 * [11] charClass ::= charClassEsc | charClassExpr
3672 * [12] charClassExpr ::= '[' charGroup ']'
3673 */
3674static void
3675xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3676 if (CUR == '[') {
3677 NEXT;
3678 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3679 if (ctxt->atom == NULL)
3680 return;
3681 xmlFAParseCharGroup(ctxt);
3682 if (CUR == ']') {
3683 NEXT;
3684 } else {
3685 ERROR("xmlFAParseCharClass: ']' expected");
3686 }
3687 } else {
3688 xmlFAParseCharClassEsc(ctxt);
3689 }
3690}
3691
3692/**
3693 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003694 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003695 *
3696 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003697 *
3698 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003699 */
3700static int
3701xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3702 int ret = 0;
3703 int ok = 0;
3704
3705 while ((CUR >= '0') && (CUR <= '9')) {
3706 ret = ret * 10 + (CUR - '0');
3707 ok = 1;
3708 NEXT;
3709 }
3710 if (ok != 1) {
3711 return(-1);
3712 }
3713 return(ret);
3714}
3715
3716/**
3717 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003718 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003719 *
3720 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3721 * [5] quantity ::= quantRange | quantMin | QuantExact
3722 * [6] quantRange ::= QuantExact ',' QuantExact
3723 * [7] quantMin ::= QuantExact ','
3724 * [8] QuantExact ::= [0-9]+
3725 */
3726static int
3727xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3728 int cur;
3729
3730 cur = CUR;
3731 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3732 if (ctxt->atom != NULL) {
3733 if (cur == '?')
3734 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3735 else if (cur == '*')
3736 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3737 else if (cur == '+')
3738 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3739 }
3740 NEXT;
3741 return(1);
3742 }
3743 if (cur == '{') {
3744 int min = 0, max = 0;
3745
3746 NEXT;
3747 cur = xmlFAParseQuantExact(ctxt);
3748 if (cur >= 0)
3749 min = cur;
3750 if (CUR == ',') {
3751 NEXT;
3752 cur = xmlFAParseQuantExact(ctxt);
3753 if (cur >= 0)
3754 max = cur;
3755 }
3756 if (CUR == '}') {
3757 NEXT;
3758 } else {
3759 ERROR("Unterminated quantifier");
3760 }
3761 if (max == 0)
3762 max = min;
3763 if (ctxt->atom != NULL) {
3764 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3765 ctxt->atom->min = min;
3766 ctxt->atom->max = max;
3767 }
3768 return(1);
3769 }
3770 return(0);
3771}
3772
3773/**
3774 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003775 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003776 *
3777 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3778 */
3779static int
3780xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3781 int codepoint, len;
3782
3783 codepoint = xmlFAIsChar(ctxt);
3784 if (codepoint > 0) {
3785 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3786 if (ctxt->atom == NULL)
3787 return(-1);
3788 codepoint = CUR_SCHAR(ctxt->cur, len);
3789 ctxt->atom->codepoint = codepoint;
3790 NEXTL(len);
3791 return(1);
3792 } else if (CUR == '|') {
3793 return(0);
3794 } else if (CUR == 0) {
3795 return(0);
3796 } else if (CUR == ')') {
3797 return(0);
3798 } else if (CUR == '(') {
3799 xmlRegStatePtr start, oldend;
3800
3801 NEXT;
3802 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3803 start = ctxt->state;
3804 oldend = ctxt->end;
3805 ctxt->end = NULL;
3806 ctxt->atom = NULL;
3807 xmlFAParseRegExp(ctxt, 0);
3808 if (CUR == ')') {
3809 NEXT;
3810 } else {
3811 ERROR("xmlFAParseAtom: expecting ')'");
3812 }
3813 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3814 if (ctxt->atom == NULL)
3815 return(-1);
3816 ctxt->atom->start = start;
3817 ctxt->atom->stop = ctxt->state;
3818 ctxt->end = oldend;
3819 return(1);
3820 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3821 xmlFAParseCharClass(ctxt);
3822 return(1);
3823 }
3824 return(0);
3825}
3826
3827/**
3828 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003829 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003830 *
3831 * [3] piece ::= atom quantifier?
3832 */
3833static int
3834xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3835 int ret;
3836
3837 ctxt->atom = NULL;
3838 ret = xmlFAParseAtom(ctxt);
3839 if (ret == 0)
3840 return(0);
3841 if (ctxt->atom == NULL) {
3842 ERROR("internal: no atom generated");
3843 }
3844 xmlFAParseQuantifier(ctxt);
3845 return(1);
3846}
3847
3848/**
3849 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003850 * @ctxt: a regexp parser context
3851 * @first: is taht the first
Daniel Veillard4255d502002-04-16 15:50:10 +00003852 *
3853 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003854 8
Daniel Veillard4255d502002-04-16 15:50:10 +00003855 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003856static int
Daniel Veillard4255d502002-04-16 15:50:10 +00003857xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, int first) {
3858 xmlRegStatePtr previous;
3859 xmlRegAtomPtr prevatom = NULL;
3860 int ret;
3861
3862 previous = ctxt->state;
3863 ret = xmlFAParsePiece(ctxt);
3864 if (ret != 0) {
3865 if (first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003866 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
3867 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003868 previous = ctxt->state;
3869 } else {
3870 prevatom = ctxt->atom;
3871 }
3872 ctxt->atom = NULL;
3873 }
3874 while ((ret != 0) && (ctxt->error == 0)) {
3875 ret = xmlFAParsePiece(ctxt);
3876 if (ret != 0) {
3877 if (first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003878 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3879 ctxt->atom) < 0)
3880 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003881 } else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003882 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3883 prevatom) < 0)
3884 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003885 prevatom = ctxt->atom;
3886 }
3887 previous = ctxt->state;
3888 ctxt->atom = NULL;
3889 }
3890 }
3891 if (!first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003892 if (xmlFAGenerateTransitions(ctxt, previous, ctxt->end, prevatom) < 0)
3893 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003894 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003895 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00003896}
3897
3898/**
3899 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003900 * @ctxt: a regexp parser context
3901 * @top: is that the top-level expressions ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003902 *
3903 * [1] regExp ::= branch ( '|' branch )*
3904 */
3905static void
3906xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
3907 xmlRegStatePtr start, end, oldend;
3908
3909 oldend = ctxt->end;
3910
3911 start = ctxt->state;
3912 xmlFAParseBranch(ctxt, (ctxt->end == NULL));
3913 if (CUR != '|') {
3914 ctxt->end = ctxt->state;
3915 return;
3916 }
3917 end = ctxt->state;
3918 while ((CUR == '|') && (ctxt->error == 0)) {
3919 NEXT;
3920 ctxt->state = start;
3921 ctxt->end = end;
3922 xmlFAParseBranch(ctxt, 0);
3923 }
3924 if (!top)
3925 ctxt->end = oldend;
3926}
3927
3928/************************************************************************
3929 * *
3930 * The basic API *
3931 * *
3932 ************************************************************************/
3933
3934/**
3935 * xmlRegexpPrint:
3936 * @output: the file for the output debug
3937 * @regexp: the compiled regexp
3938 *
3939 * Print the content of the compiled regular expression
3940 */
3941void
3942xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3943 int i;
3944
3945 fprintf(output, " regexp: ");
3946 if (regexp == NULL) {
3947 fprintf(output, "NULL\n");
3948 return;
3949 }
3950 fprintf(output, "'%s' ", regexp->string);
3951 fprintf(output, "\n");
3952 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3953 for (i = 0;i < regexp->nbAtoms; i++) {
3954 fprintf(output, " %02d ", i);
3955 xmlRegPrintAtom(output, regexp->atoms[i]);
3956 }
3957 fprintf(output, "%d states:", regexp->nbStates);
3958 fprintf(output, "\n");
3959 for (i = 0;i < regexp->nbStates; i++) {
3960 xmlRegPrintState(output, regexp->states[i]);
3961 }
3962 fprintf(output, "%d counters:\n", regexp->nbCounters);
3963 for (i = 0;i < regexp->nbCounters; i++) {
3964 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
3965 regexp->counters[i].max);
3966 }
3967}
3968
3969/**
3970 * xmlRegexpCompile:
3971 * @regexp: a regular expression string
3972 *
3973 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
3974 * Appendix F and build an automata suitable for testing strings against
3975 * that regular expression
3976 *
3977 * Returns the compiled expression or NULL in case of error
3978 */
3979xmlRegexpPtr
3980xmlRegexpCompile(const xmlChar *regexp) {
3981 xmlRegexpPtr ret;
3982 xmlRegParserCtxtPtr ctxt;
3983
3984 ctxt = xmlRegNewParserCtxt(regexp);
3985 if (ctxt == NULL)
3986 return(NULL);
3987
3988 /* initialize the parser */
3989 ctxt->end = NULL;
3990 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3991 xmlRegStatePush(ctxt, ctxt->start);
3992
3993 /* parse the expression building an automata */
3994 xmlFAParseRegExp(ctxt, 1);
3995 if (CUR != 0) {
3996 ERROR("xmlFAParseRegExp: extra characters");
3997 }
3998 ctxt->end = ctxt->state;
3999 ctxt->start->type = XML_REGEXP_START_STATE;
4000 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4001
4002 /* remove the Epsilon except for counted transitions */
4003 xmlFAEliminateEpsilonTransitions(ctxt);
4004
4005
4006 if (ctxt->error != 0) {
4007 xmlRegFreeParserCtxt(ctxt);
4008 return(NULL);
4009 }
4010 ret = xmlRegEpxFromParse(ctxt);
4011 xmlRegFreeParserCtxt(ctxt);
4012 return(ret);
4013}
4014
4015/**
4016 * xmlRegexpExec:
4017 * @comp: the compiled regular expression
4018 * @content: the value to check against the regular expression
4019 *
4020 * Check if the regular expression generate the value
4021 *
4022 * Returns 1 if it matches, 0 if not and a negativa value in case of error
4023 */
4024int
4025xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4026 if ((comp == NULL) || (content == NULL))
4027 return(-1);
4028 return(xmlFARegExec(comp, content));
4029}
4030
4031/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004032 * xmlRegexpIsDeterminist:
4033 * @comp: the compiled regular expression
4034 *
4035 * Check if the regular expression is determinist
4036 *
4037 * Returns 1 if it yes, 0 if not and a negativa value in case of error
4038 */
4039int
4040xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4041 xmlAutomataPtr am;
4042 int ret;
4043
4044 if (comp == NULL)
4045 return(-1);
4046 if (comp->determinist != -1)
4047 return(comp->determinist);
4048
4049 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004050 if (am->states != NULL) {
4051 int i;
4052
4053 for (i = 0;i < am->nbStates;i++)
4054 xmlRegFreeState(am->states[i]);
4055 xmlFree(am->states);
4056 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004057 am->nbAtoms = comp->nbAtoms;
4058 am->atoms = comp->atoms;
4059 am->nbStates = comp->nbStates;
4060 am->states = comp->states;
4061 am->determinist = -1;
4062 ret = xmlFAComputesDeterminism(am);
4063 am->atoms = NULL;
4064 am->states = NULL;
4065 xmlFreeAutomata(am);
4066 return(ret);
4067}
4068
4069/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004070 * xmlRegFreeRegexp:
4071 * @regexp: the regexp
4072 *
4073 * Free a regexp
4074 */
4075void
4076xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4077 int i;
4078 if (regexp == NULL)
4079 return;
4080
4081 if (regexp->string != NULL)
4082 xmlFree(regexp->string);
4083 if (regexp->states != NULL) {
4084 for (i = 0;i < regexp->nbStates;i++)
4085 xmlRegFreeState(regexp->states[i]);
4086 xmlFree(regexp->states);
4087 }
4088 if (regexp->atoms != NULL) {
4089 for (i = 0;i < regexp->nbAtoms;i++)
4090 xmlRegFreeAtom(regexp->atoms[i]);
4091 xmlFree(regexp->atoms);
4092 }
4093 if (regexp->counters != NULL)
4094 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004095 if (regexp->compact != NULL)
4096 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004097 if (regexp->transdata != NULL)
4098 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004099 if (regexp->stringMap != NULL) {
4100 for (i = 0; i < regexp->nbstrings;i++)
4101 xmlFree(regexp->stringMap[i]);
4102 xmlFree(regexp->stringMap);
4103 }
4104
Daniel Veillard4255d502002-04-16 15:50:10 +00004105 xmlFree(regexp);
4106}
4107
4108#ifdef LIBXML_AUTOMATA_ENABLED
4109/************************************************************************
4110 * *
4111 * The Automata interface *
4112 * *
4113 ************************************************************************/
4114
4115/**
4116 * xmlNewAutomata:
4117 *
4118 * Create a new automata
4119 *
4120 * Returns the new object or NULL in case of failure
4121 */
4122xmlAutomataPtr
4123xmlNewAutomata(void) {
4124 xmlAutomataPtr ctxt;
4125
4126 ctxt = xmlRegNewParserCtxt(NULL);
4127 if (ctxt == NULL)
4128 return(NULL);
4129
4130 /* initialize the parser */
4131 ctxt->end = NULL;
4132 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004133 if (ctxt->start == NULL) {
4134 xmlFreeAutomata(ctxt);
4135 return(NULL);
4136 }
4137 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4138 xmlRegFreeState(ctxt->start);
4139 xmlFreeAutomata(ctxt);
4140 return(NULL);
4141 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004142
4143 return(ctxt);
4144}
4145
4146/**
4147 * xmlFreeAutomata:
4148 * @am: an automata
4149 *
4150 * Free an automata
4151 */
4152void
4153xmlFreeAutomata(xmlAutomataPtr am) {
4154 if (am == NULL)
4155 return;
4156 xmlRegFreeParserCtxt(am);
4157}
4158
4159/**
4160 * xmlAutomataGetInitState:
4161 * @am: an automata
4162 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004163 * Initial state lookup
4164 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004165 * Returns the initial state of the automata
4166 */
4167xmlAutomataStatePtr
4168xmlAutomataGetInitState(xmlAutomataPtr am) {
4169 if (am == NULL)
4170 return(NULL);
4171 return(am->start);
4172}
4173
4174/**
4175 * xmlAutomataSetFinalState:
4176 * @am: an automata
4177 * @state: a state in this automata
4178 *
4179 * Makes that state a final state
4180 *
4181 * Returns 0 or -1 in case of error
4182 */
4183int
4184xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4185 if ((am == NULL) || (state == NULL))
4186 return(-1);
4187 state->type = XML_REGEXP_FINAL_STATE;
4188 return(0);
4189}
4190
4191/**
4192 * xmlAutomataNewTransition:
4193 * @am: an automata
4194 * @from: the starting point of the transition
4195 * @to: the target point of the transition or NULL
4196 * @token: the input string associated to that transition
4197 * @data: data passed to the callback function if the transition is activated
4198 *
4199 * If @to is NULL, this create first a new target state in the automata
4200 * and then adds a transition from the @from state to the target state
4201 * activated by the value of @token
4202 *
4203 * Returns the target state or NULL in case of error
4204 */
4205xmlAutomataStatePtr
4206xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4207 xmlAutomataStatePtr to, const xmlChar *token,
4208 void *data) {
4209 xmlRegAtomPtr atom;
4210
4211 if ((am == NULL) || (from == NULL) || (token == NULL))
4212 return(NULL);
4213 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004214 if (atom == NULL)
4215 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004216 atom->data = data;
4217 if (atom == NULL)
4218 return(NULL);
4219 atom->valuep = xmlStrdup(token);
4220
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004221 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4222 xmlRegFreeAtom(atom);
4223 return(NULL);
4224 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004225 if (to == NULL)
4226 return(am->state);
4227 return(to);
4228}
4229
4230/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004231 * xmlAutomataNewTransition2:
4232 * @am: an automata
4233 * @from: the starting point of the transition
4234 * @to: the target point of the transition or NULL
4235 * @token: the first input string associated to that transition
4236 * @token2: the second input string associated to that transition
4237 * @data: data passed to the callback function if the transition is activated
4238 *
4239 * If @to is NULL, this create first a new target state in the automata
4240 * and then adds a transition from the @from state to the target state
4241 * activated by the value of @token
4242 *
4243 * Returns the target state or NULL in case of error
4244 */
4245xmlAutomataStatePtr
4246xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4247 xmlAutomataStatePtr to, const xmlChar *token,
4248 const xmlChar *token2, void *data) {
4249 xmlRegAtomPtr atom;
4250
4251 if ((am == NULL) || (from == NULL) || (token == NULL))
4252 return(NULL);
4253 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4254 atom->data = data;
4255 if (atom == NULL)
4256 return(NULL);
4257 if ((token2 == NULL) || (*token2 == 0)) {
4258 atom->valuep = xmlStrdup(token);
4259 } else {
4260 int lenn, lenp;
4261 xmlChar *str;
4262
4263 lenn = strlen((char *) token2);
4264 lenp = strlen((char *) token);
4265
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004266 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004267 if (str == NULL) {
4268 xmlRegFreeAtom(atom);
4269 return(NULL);
4270 }
4271 memcpy(&str[0], token, lenp);
4272 str[lenp] = '|';
4273 memcpy(&str[lenp + 1], token2, lenn);
4274 str[lenn + lenp + 1] = 0;
4275
4276 atom->valuep = str;
4277 }
4278
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004279 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4280 xmlRegFreeAtom(atom);
4281 return(NULL);
4282 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004283 if (to == NULL)
4284 return(am->state);
4285 return(to);
4286}
4287
4288/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004289 * xmlAutomataNewCountTrans:
4290 * @am: an automata
4291 * @from: the starting point of the transition
4292 * @to: the target point of the transition or NULL
4293 * @token: the input string associated to that transition
4294 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004295 * @max: the maximum successive occurences of token
4296 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004297 *
4298 * If @to is NULL, this create first a new target state in the automata
4299 * and then adds a transition from the @from state to the target state
4300 * activated by a succession of input of value @token and whose number
4301 * is between @min and @max
4302 *
4303 * Returns the target state or NULL in case of error
4304 */
4305xmlAutomataStatePtr
4306xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4307 xmlAutomataStatePtr to, const xmlChar *token,
4308 int min, int max, void *data) {
4309 xmlRegAtomPtr atom;
4310
4311 if ((am == NULL) || (from == NULL) || (token == NULL))
4312 return(NULL);
4313 if (min < 0)
4314 return(NULL);
4315 if ((max < min) || (max < 1))
4316 return(NULL);
4317 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4318 if (atom == NULL)
4319 return(NULL);
4320 atom->valuep = xmlStrdup(token);
4321 atom->data = data;
4322 if (min == 0)
4323 atom->min = 1;
4324 else
4325 atom->min = min;
4326 atom->max = max;
4327
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004328 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4329 xmlRegFreeAtom(atom);
4330 return(NULL);
4331 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004332 if (to == NULL)
4333 to = am->state;
4334 if (to == NULL)
4335 return(NULL);
4336 if (min == 0)
4337 xmlFAGenerateEpsilonTransition(am, from, to);
4338 return(to);
4339}
4340
4341/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004342 * xmlAutomataNewOnceTrans:
4343 * @am: an automata
4344 * @from: the starting point of the transition
4345 * @to: the target point of the transition or NULL
4346 * @token: the input string associated to that transition
4347 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004348 * @max: the maximum successive occurences of token
4349 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00004350 *
4351 * If @to is NULL, this create first a new target state in the automata
4352 * and then adds a transition from the @from state to the target state
4353 * activated by a succession of input of value @token and whose number
4354 * is between @min and @max, moreover that transistion can only be crossed
4355 * once.
4356 *
4357 * Returns the target state or NULL in case of error
4358 */
4359xmlAutomataStatePtr
4360xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4361 xmlAutomataStatePtr to, const xmlChar *token,
4362 int min, int max, void *data) {
4363 xmlRegAtomPtr atom;
4364 int counter;
4365
4366 if ((am == NULL) || (from == NULL) || (token == NULL))
4367 return(NULL);
4368 if (min < 1)
4369 return(NULL);
4370 if ((max < min) || (max < 1))
4371 return(NULL);
4372 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4373 if (atom == NULL)
4374 return(NULL);
4375 atom->valuep = xmlStrdup(token);
4376 atom->data = data;
4377 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4378 if (min == 0)
4379 atom->min = 1;
4380 else
4381 atom->min = min;
4382 atom->max = max;
4383 /*
4384 * associate a counter to the transition.
4385 */
4386 counter = xmlRegGetCounter(am);
4387 am->counters[counter].min = 1;
4388 am->counters[counter].max = 1;
4389
4390 /* xmlFAGenerateTransitions(am, from, to, atom); */
4391 if (to == NULL) {
4392 to = xmlRegNewState(am);
4393 xmlRegStatePush(am, to);
4394 }
4395 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4396 xmlRegAtomPush(am, atom);
4397 am->state = to;
4398 if (to == NULL)
4399 to = am->state;
4400 if (to == NULL)
4401 return(NULL);
4402 return(to);
4403}
4404
4405/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004406 * xmlAutomataNewState:
4407 * @am: an automata
4408 *
4409 * Create a new disconnected state in the automata
4410 *
4411 * Returns the new state or NULL in case of error
4412 */
4413xmlAutomataStatePtr
4414xmlAutomataNewState(xmlAutomataPtr am) {
4415 xmlAutomataStatePtr to;
4416
4417 if (am == NULL)
4418 return(NULL);
4419 to = xmlRegNewState(am);
4420 xmlRegStatePush(am, to);
4421 return(to);
4422}
4423
4424/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004425 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004426 * @am: an automata
4427 * @from: the starting point of the transition
4428 * @to: the target point of the transition or NULL
4429 *
4430 * If @to is NULL, this create first a new target state in the automata
4431 * and then adds a an epsilon transition from the @from state to the
4432 * target state
4433 *
4434 * Returns the target state or NULL in case of error
4435 */
4436xmlAutomataStatePtr
4437xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4438 xmlAutomataStatePtr to) {
4439 if ((am == NULL) || (from == NULL))
4440 return(NULL);
4441 xmlFAGenerateEpsilonTransition(am, from, to);
4442 if (to == NULL)
4443 return(am->state);
4444 return(to);
4445}
4446
Daniel Veillardb509f152002-04-17 16:28:10 +00004447/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004448 * xmlAutomataNewAllTrans:
4449 * @am: an automata
4450 * @from: the starting point of the transition
4451 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004452 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004453 *
4454 * If @to is NULL, this create first a new target state in the automata
4455 * and then adds a an ALL transition from the @from state to the
4456 * target state. That transition is an epsilon transition allowed only when
4457 * all transitions from the @from node have been activated.
4458 *
4459 * Returns the target state or NULL in case of error
4460 */
4461xmlAutomataStatePtr
4462xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004463 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004464 if ((am == NULL) || (from == NULL))
4465 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004466 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004467 if (to == NULL)
4468 return(am->state);
4469 return(to);
4470}
4471
4472/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004473 * xmlAutomataNewCounter:
4474 * @am: an automata
4475 * @min: the minimal value on the counter
4476 * @max: the maximal value on the counter
4477 *
4478 * Create a new counter
4479 *
4480 * Returns the counter number or -1 in case of error
4481 */
4482int
4483xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4484 int ret;
4485
4486 if (am == NULL)
4487 return(-1);
4488
4489 ret = xmlRegGetCounter(am);
4490 if (ret < 0)
4491 return(-1);
4492 am->counters[ret].min = min;
4493 am->counters[ret].max = max;
4494 return(ret);
4495}
4496
4497/**
4498 * xmlAutomataNewCountedTrans:
4499 * @am: an automata
4500 * @from: the starting point of the transition
4501 * @to: the target point of the transition or NULL
4502 * @counter: the counter associated to that transition
4503 *
4504 * If @to is NULL, this create first a new target state in the automata
4505 * and then adds an epsilon transition from the @from state to the target state
4506 * which will increment the counter provided
4507 *
4508 * Returns the target state or NULL in case of error
4509 */
4510xmlAutomataStatePtr
4511xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4512 xmlAutomataStatePtr to, int counter) {
4513 if ((am == NULL) || (from == NULL) || (counter < 0))
4514 return(NULL);
4515 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4516 if (to == NULL)
4517 return(am->state);
4518 return(to);
4519}
4520
4521/**
4522 * xmlAutomataNewCounterTrans:
4523 * @am: an automata
4524 * @from: the starting point of the transition
4525 * @to: the target point of the transition or NULL
4526 * @counter: the counter associated to that transition
4527 *
4528 * If @to is NULL, this create first a new target state in the automata
4529 * and then adds an epsilon transition from the @from state to the target state
4530 * which will be allowed only if the counter is within the right range.
4531 *
4532 * Returns the target state or NULL in case of error
4533 */
4534xmlAutomataStatePtr
4535xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4536 xmlAutomataStatePtr to, int counter) {
4537 if ((am == NULL) || (from == NULL) || (counter < 0))
4538 return(NULL);
4539 xmlFAGenerateCountedTransition(am, from, to, counter);
4540 if (to == NULL)
4541 return(am->state);
4542 return(to);
4543}
Daniel Veillard4255d502002-04-16 15:50:10 +00004544
4545/**
4546 * xmlAutomataCompile:
4547 * @am: an automata
4548 *
4549 * Compile the automata into a Reg Exp ready for being executed.
4550 * The automata should be free after this point.
4551 *
4552 * Returns the compiled regexp or NULL in case of error
4553 */
4554xmlRegexpPtr
4555xmlAutomataCompile(xmlAutomataPtr am) {
4556 xmlRegexpPtr ret;
4557
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004558 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004559 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004560 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004561 ret = xmlRegEpxFromParse(am);
4562
4563 return(ret);
4564}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004565
4566/**
4567 * xmlAutomataIsDeterminist:
4568 * @am: an automata
4569 *
4570 * Checks if an automata is determinist.
4571 *
4572 * Returns 1 if true, 0 if not, and -1 in case of error
4573 */
4574int
4575xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4576 int ret;
4577
4578 if (am == NULL)
4579 return(-1);
4580
4581 ret = xmlFAComputesDeterminism(am);
4582 return(ret);
4583}
Daniel Veillard4255d502002-04-16 15:50:10 +00004584#endif /* LIBXML_AUTOMATA_ENABLED */
4585#endif /* LIBXML_REGEXP_ENABLED */