blob: c8d1ad131aab35c9a89a0c85527c0c883a44d310 [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 {
144 int neg;
145 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];
1998 if (range->neg) {
1999 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2000 0, range->start, range->end,
2001 range->blockName);
2002 if (ret != 0)
2003 return(0); /* excluded char */
Daniel Veillardf2a12832003-11-24 13:04:35 +00002004 else
2005 accept = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002006 } else {
2007 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2008 0, range->start, range->end,
2009 range->blockName);
2010 if (ret != 0)
2011 accept = 1; /* might still be excluded */
2012 }
2013 }
2014 return(accept);
2015 }
2016 case XML_REGEXP_STRING:
2017 printf("TODO: XML_REGEXP_STRING\n");
2018 return(-1);
2019 case XML_REGEXP_ANYCHAR:
2020 case XML_REGEXP_ANYSPACE:
2021 case XML_REGEXP_NOTSPACE:
2022 case XML_REGEXP_INITNAME:
2023 case XML_REGEXP_NOTINITNAME:
2024 case XML_REGEXP_NAMECHAR:
2025 case XML_REGEXP_NOTNAMECHAR:
2026 case XML_REGEXP_DECIMAL:
2027 case XML_REGEXP_NOTDECIMAL:
2028 case XML_REGEXP_REALCHAR:
2029 case XML_REGEXP_NOTREALCHAR:
2030 case XML_REGEXP_LETTER:
2031 case XML_REGEXP_LETTER_UPPERCASE:
2032 case XML_REGEXP_LETTER_LOWERCASE:
2033 case XML_REGEXP_LETTER_TITLECASE:
2034 case XML_REGEXP_LETTER_MODIFIER:
2035 case XML_REGEXP_LETTER_OTHERS:
2036 case XML_REGEXP_MARK:
2037 case XML_REGEXP_MARK_NONSPACING:
2038 case XML_REGEXP_MARK_SPACECOMBINING:
2039 case XML_REGEXP_MARK_ENCLOSING:
2040 case XML_REGEXP_NUMBER:
2041 case XML_REGEXP_NUMBER_DECIMAL:
2042 case XML_REGEXP_NUMBER_LETTER:
2043 case XML_REGEXP_NUMBER_OTHERS:
2044 case XML_REGEXP_PUNCT:
2045 case XML_REGEXP_PUNCT_CONNECTOR:
2046 case XML_REGEXP_PUNCT_DASH:
2047 case XML_REGEXP_PUNCT_OPEN:
2048 case XML_REGEXP_PUNCT_CLOSE:
2049 case XML_REGEXP_PUNCT_INITQUOTE:
2050 case XML_REGEXP_PUNCT_FINQUOTE:
2051 case XML_REGEXP_PUNCT_OTHERS:
2052 case XML_REGEXP_SEPAR:
2053 case XML_REGEXP_SEPAR_SPACE:
2054 case XML_REGEXP_SEPAR_LINE:
2055 case XML_REGEXP_SEPAR_PARA:
2056 case XML_REGEXP_SYMBOL:
2057 case XML_REGEXP_SYMBOL_MATH:
2058 case XML_REGEXP_SYMBOL_CURRENCY:
2059 case XML_REGEXP_SYMBOL_MODIFIER:
2060 case XML_REGEXP_SYMBOL_OTHERS:
2061 case XML_REGEXP_OTHER:
2062 case XML_REGEXP_OTHER_CONTROL:
2063 case XML_REGEXP_OTHER_FORMAT:
2064 case XML_REGEXP_OTHER_PRIVATE:
2065 case XML_REGEXP_OTHER_NA:
2066 case XML_REGEXP_BLOCK_NAME:
2067 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2068 (const xmlChar *)atom->valuep);
2069 if (atom->neg)
2070 ret = !ret;
2071 break;
2072 }
2073 return(ret);
2074}
2075
2076/************************************************************************
2077 * *
2078 * Saving an restoring state of an execution context *
2079 * *
2080 ************************************************************************/
2081
2082#ifdef DEBUG_REGEXP_EXEC
2083static void
2084xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2085 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2086 if (exec->inputStack != NULL) {
2087 int i;
2088 printf(": ");
2089 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2090 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2091 } else {
2092 printf(": %s", &(exec->inputString[exec->index]));
2093 }
2094 printf("\n");
2095}
2096#endif
2097
2098static void
2099xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2100#ifdef DEBUG_REGEXP_EXEC
2101 printf("saving ");
2102 exec->transno++;
2103 xmlFARegDebugExec(exec);
2104 exec->transno--;
2105#endif
2106
2107 if (exec->maxRollbacks == 0) {
2108 exec->maxRollbacks = 4;
2109 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2110 sizeof(xmlRegExecRollback));
2111 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002112 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002113 exec->maxRollbacks = 0;
2114 return;
2115 }
2116 memset(exec->rollbacks, 0,
2117 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2118 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2119 xmlRegExecRollback *tmp;
2120 int len = exec->maxRollbacks;
2121
2122 exec->maxRollbacks *= 2;
2123 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2124 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2125 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002126 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002127 exec->maxRollbacks /= 2;
2128 return;
2129 }
2130 exec->rollbacks = tmp;
2131 tmp = &exec->rollbacks[len];
2132 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2133 }
2134 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2135 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2136 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2137 if (exec->comp->nbCounters > 0) {
2138 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2139 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2140 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2141 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002142 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002143 exec->status = -5;
2144 return;
2145 }
2146 }
2147 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2148 exec->comp->nbCounters * sizeof(int));
2149 }
2150 exec->nbRollbacks++;
2151}
2152
2153static void
2154xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2155 if (exec->nbRollbacks <= 0) {
2156 exec->status = -1;
2157#ifdef DEBUG_REGEXP_EXEC
2158 printf("rollback failed on empty stack\n");
2159#endif
2160 return;
2161 }
2162 exec->nbRollbacks--;
2163 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2164 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2165 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2166 if (exec->comp->nbCounters > 0) {
2167 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2168 fprintf(stderr, "exec save: allocation failed");
2169 exec->status = -6;
2170 return;
2171 }
2172 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2173 exec->comp->nbCounters * sizeof(int));
2174 }
2175
2176#ifdef DEBUG_REGEXP_EXEC
2177 printf("restored ");
2178 xmlFARegDebugExec(exec);
2179#endif
2180}
2181
2182/************************************************************************
2183 * *
2184 * Verifyer, running an input against a compiled regexp *
2185 * *
2186 ************************************************************************/
2187
2188static int
2189xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2190 xmlRegExecCtxt execval;
2191 xmlRegExecCtxtPtr exec = &execval;
2192 int ret, codepoint, len;
2193
2194 exec->inputString = content;
2195 exec->index = 0;
2196 exec->determinist = 1;
2197 exec->maxRollbacks = 0;
2198 exec->nbRollbacks = 0;
2199 exec->rollbacks = NULL;
2200 exec->status = 0;
2201 exec->comp = comp;
2202 exec->state = comp->states[0];
2203 exec->transno = 0;
2204 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002205 exec->inputStack = NULL;
2206 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002207 if (comp->nbCounters > 0) {
2208 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002209 if (exec->counts == NULL) {
2210 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002211 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002212 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002213 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2214 } else
2215 exec->counts = NULL;
2216 while ((exec->status == 0) &&
2217 ((exec->inputString[exec->index] != 0) ||
2218 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2219 xmlRegTransPtr trans;
2220 xmlRegAtomPtr atom;
2221
2222 /*
2223 * End of input on non-terminal state, rollback, however we may
2224 * still have epsilon like transition for counted transitions
2225 * on counters, in that case don't break too early.
2226 */
2227 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2228 goto rollback;
2229
2230 exec->transcount = 0;
2231 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2232 trans = &exec->state->trans[exec->transno];
2233 if (trans->to < 0)
2234 continue;
2235 atom = trans->atom;
2236 ret = 0;
2237 if (trans->count >= 0) {
2238 int count;
2239 xmlRegCounterPtr counter;
2240
2241 /*
2242 * A counted transition.
2243 */
2244
2245 count = exec->counts[trans->count];
2246 counter = &exec->comp->counters[trans->count];
2247#ifdef DEBUG_REGEXP_EXEC
2248 printf("testing count %d: val %d, min %d, max %d\n",
2249 trans->count, count, counter->min, counter->max);
2250#endif
2251 ret = ((count >= counter->min) && (count <= counter->max));
2252 } else if (atom == NULL) {
2253 fprintf(stderr, "epsilon transition left at runtime\n");
2254 exec->status = -2;
2255 break;
2256 } else if (exec->inputString[exec->index] != 0) {
2257 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2258 ret = xmlRegCheckCharacter(atom, codepoint);
2259 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2260 xmlRegStatePtr to = comp->states[trans->to];
2261
2262 /*
2263 * this is a multiple input sequence
2264 */
2265 if (exec->state->nbTrans > exec->transno + 1) {
2266 xmlFARegExecSave(exec);
2267 }
2268 exec->transcount = 1;
2269 do {
2270 /*
2271 * Try to progress as much as possible on the input
2272 */
2273 if (exec->transcount == atom->max) {
2274 break;
2275 }
2276 exec->index += len;
2277 /*
2278 * End of input: stop here
2279 */
2280 if (exec->inputString[exec->index] == 0) {
2281 exec->index -= len;
2282 break;
2283 }
2284 if (exec->transcount >= atom->min) {
2285 int transno = exec->transno;
2286 xmlRegStatePtr state = exec->state;
2287
2288 /*
2289 * The transition is acceptable save it
2290 */
2291 exec->transno = -1; /* trick */
2292 exec->state = to;
2293 xmlFARegExecSave(exec);
2294 exec->transno = transno;
2295 exec->state = state;
2296 }
2297 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2298 len);
2299 ret = xmlRegCheckCharacter(atom, codepoint);
2300 exec->transcount++;
2301 } while (ret == 1);
2302 if (exec->transcount < atom->min)
2303 ret = 0;
2304
2305 /*
2306 * If the last check failed but one transition was found
2307 * possible, rollback
2308 */
2309 if (ret < 0)
2310 ret = 0;
2311 if (ret == 0) {
2312 goto rollback;
2313 }
2314 }
2315 }
2316 if (ret == 1) {
2317 if (exec->state->nbTrans > exec->transno + 1) {
2318 xmlFARegExecSave(exec);
2319 }
2320 if (trans->counter >= 0) {
2321#ifdef DEBUG_REGEXP_EXEC
2322 printf("Increasing count %d\n", trans->counter);
2323#endif
2324 exec->counts[trans->counter]++;
2325 }
2326#ifdef DEBUG_REGEXP_EXEC
2327 printf("entering state %d\n", trans->to);
2328#endif
2329 exec->state = comp->states[trans->to];
2330 exec->transno = 0;
2331 if (trans->atom != NULL) {
2332 exec->index += len;
2333 }
2334 goto progress;
2335 } else if (ret < 0) {
2336 exec->status = -4;
2337 break;
2338 }
2339 }
2340 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2341rollback:
2342 /*
2343 * Failed to find a way out
2344 */
2345 exec->determinist = 0;
2346 xmlFARegExecRollBack(exec);
2347 }
2348progress:
2349 continue;
2350 }
2351 if (exec->rollbacks != NULL) {
2352 if (exec->counts != NULL) {
2353 int i;
2354
2355 for (i = 0;i < exec->maxRollbacks;i++)
2356 if (exec->rollbacks[i].counts != NULL)
2357 xmlFree(exec->rollbacks[i].counts);
2358 }
2359 xmlFree(exec->rollbacks);
2360 }
2361 if (exec->counts != NULL)
2362 xmlFree(exec->counts);
2363 if (exec->status == 0)
2364 return(1);
2365 if (exec->status == -1)
2366 return(0);
2367 return(exec->status);
2368}
2369
2370/************************************************************************
2371 * *
2372 * Progressive interface to the verifyer one atom at a time *
2373 * *
2374 ************************************************************************/
2375
2376/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002377 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002378 * @comp: a precompiled regular expression
2379 * @callback: a callback function used for handling progresses in the
2380 * automata matching phase
2381 * @data: the context data associated to the callback in this context
2382 *
2383 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002384 *
2385 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002386 */
2387xmlRegExecCtxtPtr
2388xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2389 xmlRegExecCtxtPtr exec;
2390
2391 if (comp == NULL)
2392 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002393 if ((comp->compact == NULL) && (comp->states == NULL))
2394 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002395 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2396 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002397 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002398 return(NULL);
2399 }
2400 memset(exec, 0, sizeof(xmlRegExecCtxt));
2401 exec->inputString = NULL;
2402 exec->index = 0;
2403 exec->determinist = 1;
2404 exec->maxRollbacks = 0;
2405 exec->nbRollbacks = 0;
2406 exec->rollbacks = NULL;
2407 exec->status = 0;
2408 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002409 if (comp->compact == NULL)
2410 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002411 exec->transno = 0;
2412 exec->transcount = 0;
2413 exec->callback = callback;
2414 exec->data = data;
2415 if (comp->nbCounters > 0) {
2416 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2417 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002418 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002419 xmlFree(exec);
2420 return(NULL);
2421 }
2422 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2423 } else
2424 exec->counts = NULL;
2425 exec->inputStackMax = 0;
2426 exec->inputStackNr = 0;
2427 exec->inputStack = NULL;
2428 return(exec);
2429}
2430
2431/**
2432 * xmlRegFreeExecCtxt:
2433 * @exec: a regular expression evaulation context
2434 *
2435 * Free the structures associated to a regular expression evaulation context.
2436 */
2437void
2438xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2439 if (exec == NULL)
2440 return;
2441
2442 if (exec->rollbacks != NULL) {
2443 if (exec->counts != NULL) {
2444 int i;
2445
2446 for (i = 0;i < exec->maxRollbacks;i++)
2447 if (exec->rollbacks[i].counts != NULL)
2448 xmlFree(exec->rollbacks[i].counts);
2449 }
2450 xmlFree(exec->rollbacks);
2451 }
2452 if (exec->counts != NULL)
2453 xmlFree(exec->counts);
2454 if (exec->inputStack != NULL) {
2455 int i;
2456
Daniel Veillard32370232002-10-16 14:08:14 +00002457 for (i = 0;i < exec->inputStackNr;i++) {
2458 if (exec->inputStack[i].value != NULL)
2459 xmlFree(exec->inputStack[i].value);
2460 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002461 xmlFree(exec->inputStack);
2462 }
2463 xmlFree(exec);
2464}
2465
2466static void
2467xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2468 void *data) {
2469#ifdef DEBUG_PUSH
2470 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2471#endif
2472 if (exec->inputStackMax == 0) {
2473 exec->inputStackMax = 4;
2474 exec->inputStack = (xmlRegInputTokenPtr)
2475 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2476 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002477 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002478 exec->inputStackMax = 0;
2479 return;
2480 }
2481 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2482 xmlRegInputTokenPtr tmp;
2483
2484 exec->inputStackMax *= 2;
2485 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2486 exec->inputStackMax * sizeof(xmlRegInputToken));
2487 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002488 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002489 exec->inputStackMax /= 2;
2490 return;
2491 }
2492 exec->inputStack = tmp;
2493 }
2494 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2495 exec->inputStack[exec->inputStackNr].data = data;
2496 exec->inputStackNr++;
2497 exec->inputStack[exec->inputStackNr].value = NULL;
2498 exec->inputStack[exec->inputStackNr].data = NULL;
2499}
2500
2501
2502/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002503 * xmlRegCompactPushString:
2504 * @exec: a regexp execution context
2505 * @comp: the precompiled exec with a compact table
2506 * @value: a string token input
2507 * @data: data associated to the token to reuse in callbacks
2508 *
2509 * Push one input token in the execution context
2510 *
2511 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2512 * a negative value in case of error.
2513 */
2514static int
2515xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2516 xmlRegexpPtr comp,
2517 const xmlChar *value,
2518 void *data) {
2519 int state = exec->index;
2520 int i, target;
2521
2522 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2523 return(-1);
2524
2525 if (value == NULL) {
2526 /*
2527 * are we at a final state ?
2528 */
2529 if (comp->compact[state * (comp->nbstrings + 1)] ==
2530 XML_REGEXP_FINAL_STATE)
2531 return(1);
2532 return(0);
2533 }
2534
2535#ifdef DEBUG_PUSH
2536 printf("value pushed: %s\n", value);
2537#endif
2538
2539 /*
2540 * Examine all outside transition from current state
2541 */
2542 for (i = 0;i < comp->nbstrings;i++) {
2543 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2544 if ((target > 0) && (target <= comp->nbstates)) {
2545 target--; /* to avoid 0 */
2546 if (xmlStrEqual(comp->stringMap[i], value)) {
2547 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002548 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2549 exec->callback(exec->data, value,
2550 comp->transdata[state * comp->nbstrings + i], data);
2551 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002552#ifdef DEBUG_PUSH
2553 printf("entering state %d\n", target);
2554#endif
2555 if (comp->compact[target * (comp->nbstrings + 1)] ==
2556 XML_REGEXP_FINAL_STATE)
2557 return(1);
2558 return(0);
2559 }
2560 }
2561 }
2562 /*
2563 * Failed to find an exit transition out from current state for the
2564 * current token
2565 */
2566#ifdef DEBUG_PUSH
2567 printf("failed to find a transition for %s on state %d\n", value, state);
2568#endif
2569 exec->status = -1;
2570 return(-1);
2571}
2572
2573/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002574 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002575 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002576 * @value: a string token input
2577 * @data: data associated to the token to reuse in callbacks
2578 *
2579 * Push one input token in the execution context
2580 *
2581 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2582 * a negative value in case of error.
2583 */
2584int
2585xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2586 void *data) {
2587 xmlRegTransPtr trans;
2588 xmlRegAtomPtr atom;
2589 int ret;
2590 int final = 0;
2591
2592 if (exec == NULL)
2593 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002594 if (exec->comp == NULL)
2595 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002596 if (exec->status != 0)
2597 return(exec->status);
2598
Daniel Veillard23e73572002-09-19 19:56:43 +00002599 if (exec->comp->compact != NULL)
2600 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2601
Daniel Veillard4255d502002-04-16 15:50:10 +00002602 if (value == NULL) {
2603 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2604 return(1);
2605 final = 1;
2606 }
2607
2608#ifdef DEBUG_PUSH
2609 printf("value pushed: %s\n", value);
2610#endif
2611 /*
2612 * If we have an active rollback stack push the new value there
2613 * and get back to where we were left
2614 */
2615 if ((value != NULL) && (exec->inputStackNr > 0)) {
2616 xmlFARegExecSaveInputString(exec, value, data);
2617 value = exec->inputStack[exec->index].value;
2618 data = exec->inputStack[exec->index].data;
2619#ifdef DEBUG_PUSH
2620 printf("value loaded: %s\n", value);
2621#endif
2622 }
2623
2624 while ((exec->status == 0) &&
2625 ((value != NULL) ||
2626 ((final == 1) &&
2627 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2628
2629 /*
2630 * End of input on non-terminal state, rollback, however we may
2631 * still have epsilon like transition for counted transitions
2632 * on counters, in that case don't break too early.
2633 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002634 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002635 goto rollback;
2636
2637 exec->transcount = 0;
2638 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2639 trans = &exec->state->trans[exec->transno];
2640 if (trans->to < 0)
2641 continue;
2642 atom = trans->atom;
2643 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002644 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2645 int i;
2646 int count;
2647 xmlRegTransPtr t;
2648 xmlRegCounterPtr counter;
2649
2650 ret = 0;
2651
2652#ifdef DEBUG_PUSH
2653 printf("testing all lax %d\n", trans->count);
2654#endif
2655 /*
2656 * Check all counted transitions from the current state
2657 */
2658 if ((value == NULL) && (final)) {
2659 ret = 1;
2660 } else if (value != NULL) {
2661 for (i = 0;i < exec->state->nbTrans;i++) {
2662 t = &exec->state->trans[i];
2663 if ((t->counter < 0) || (t == trans))
2664 continue;
2665 counter = &exec->comp->counters[t->counter];
2666 count = exec->counts[t->counter];
2667 if ((count < counter->max) &&
2668 (t->atom != NULL) &&
2669 (xmlStrEqual(value, t->atom->valuep))) {
2670 ret = 0;
2671 break;
2672 }
2673 if ((count >= counter->min) &&
2674 (count < counter->max) &&
2675 (xmlStrEqual(value, t->atom->valuep))) {
2676 ret = 1;
2677 break;
2678 }
2679 }
2680 }
2681 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002682 int i;
2683 int count;
2684 xmlRegTransPtr t;
2685 xmlRegCounterPtr counter;
2686
2687 ret = 1;
2688
2689#ifdef DEBUG_PUSH
2690 printf("testing all %d\n", trans->count);
2691#endif
2692 /*
2693 * Check all counted transitions from the current state
2694 */
2695 for (i = 0;i < exec->state->nbTrans;i++) {
2696 t = &exec->state->trans[i];
2697 if ((t->counter < 0) || (t == trans))
2698 continue;
2699 counter = &exec->comp->counters[t->counter];
2700 count = exec->counts[t->counter];
2701 if ((count < counter->min) || (count > counter->max)) {
2702 ret = 0;
2703 break;
2704 }
2705 }
2706 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002707 int count;
2708 xmlRegCounterPtr counter;
2709
2710 /*
2711 * A counted transition.
2712 */
2713
2714 count = exec->counts[trans->count];
2715 counter = &exec->comp->counters[trans->count];
2716#ifdef DEBUG_PUSH
2717 printf("testing count %d: val %d, min %d, max %d\n",
2718 trans->count, count, counter->min, counter->max);
2719#endif
2720 ret = ((count >= counter->min) && (count <= counter->max));
2721 } else if (atom == NULL) {
2722 fprintf(stderr, "epsilon transition left at runtime\n");
2723 exec->status = -2;
2724 break;
2725 } else if (value != NULL) {
2726 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002727 if ((ret == 1) && (trans->counter >= 0)) {
2728 xmlRegCounterPtr counter;
2729 int count;
2730
2731 count = exec->counts[trans->counter];
2732 counter = &exec->comp->counters[trans->counter];
2733 if (count >= counter->max)
2734 ret = 0;
2735 }
2736
Daniel Veillard4255d502002-04-16 15:50:10 +00002737 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2738 xmlRegStatePtr to = exec->comp->states[trans->to];
2739
2740 /*
2741 * this is a multiple input sequence
2742 */
2743 if (exec->state->nbTrans > exec->transno + 1) {
2744 if (exec->inputStackNr <= 0) {
2745 xmlFARegExecSaveInputString(exec, value, data);
2746 }
2747 xmlFARegExecSave(exec);
2748 }
2749 exec->transcount = 1;
2750 do {
2751 /*
2752 * Try to progress as much as possible on the input
2753 */
2754 if (exec->transcount == atom->max) {
2755 break;
2756 }
2757 exec->index++;
2758 value = exec->inputStack[exec->index].value;
2759 data = exec->inputStack[exec->index].data;
2760#ifdef DEBUG_PUSH
2761 printf("value loaded: %s\n", value);
2762#endif
2763
2764 /*
2765 * End of input: stop here
2766 */
2767 if (value == NULL) {
2768 exec->index --;
2769 break;
2770 }
2771 if (exec->transcount >= atom->min) {
2772 int transno = exec->transno;
2773 xmlRegStatePtr state = exec->state;
2774
2775 /*
2776 * The transition is acceptable save it
2777 */
2778 exec->transno = -1; /* trick */
2779 exec->state = to;
2780 if (exec->inputStackNr <= 0) {
2781 xmlFARegExecSaveInputString(exec, value, data);
2782 }
2783 xmlFARegExecSave(exec);
2784 exec->transno = transno;
2785 exec->state = state;
2786 }
2787 ret = xmlStrEqual(value, atom->valuep);
2788 exec->transcount++;
2789 } while (ret == 1);
2790 if (exec->transcount < atom->min)
2791 ret = 0;
2792
2793 /*
2794 * If the last check failed but one transition was found
2795 * possible, rollback
2796 */
2797 if (ret < 0)
2798 ret = 0;
2799 if (ret == 0) {
2800 goto rollback;
2801 }
2802 }
2803 }
2804 if (ret == 1) {
2805 if ((exec->callback != NULL) && (atom != NULL)) {
2806 exec->callback(exec->data, atom->valuep,
2807 atom->data, data);
2808 }
2809 if (exec->state->nbTrans > exec->transno + 1) {
2810 if (exec->inputStackNr <= 0) {
2811 xmlFARegExecSaveInputString(exec, value, data);
2812 }
2813 xmlFARegExecSave(exec);
2814 }
2815 if (trans->counter >= 0) {
2816#ifdef DEBUG_PUSH
2817 printf("Increasing count %d\n", trans->counter);
2818#endif
2819 exec->counts[trans->counter]++;
2820 }
2821#ifdef DEBUG_PUSH
2822 printf("entering state %d\n", trans->to);
2823#endif
2824 exec->state = exec->comp->states[trans->to];
2825 exec->transno = 0;
2826 if (trans->atom != NULL) {
2827 if (exec->inputStack != NULL) {
2828 exec->index++;
2829 if (exec->index < exec->inputStackNr) {
2830 value = exec->inputStack[exec->index].value;
2831 data = exec->inputStack[exec->index].data;
2832#ifdef DEBUG_PUSH
2833 printf("value loaded: %s\n", value);
2834#endif
2835 } else {
2836 value = NULL;
2837 data = NULL;
2838#ifdef DEBUG_PUSH
2839 printf("end of input\n");
2840#endif
2841 }
2842 } else {
2843 value = NULL;
2844 data = NULL;
2845#ifdef DEBUG_PUSH
2846 printf("end of input\n");
2847#endif
2848 }
2849 }
2850 goto progress;
2851 } else if (ret < 0) {
2852 exec->status = -4;
2853 break;
2854 }
2855 }
2856 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2857rollback:
2858 /*
2859 * Failed to find a way out
2860 */
2861 exec->determinist = 0;
2862 xmlFARegExecRollBack(exec);
2863 if (exec->status == 0) {
2864 value = exec->inputStack[exec->index].value;
2865 data = exec->inputStack[exec->index].data;
2866#ifdef DEBUG_PUSH
2867 printf("value loaded: %s\n", value);
2868#endif
2869 }
2870 }
2871progress:
2872 continue;
2873 }
2874 if (exec->status == 0) {
2875 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2876 }
2877 return(exec->status);
2878}
2879
Daniel Veillard52b48c72003-04-13 19:53:42 +00002880/**
2881 * xmlRegExecPushString2:
2882 * @exec: a regexp execution context or NULL to indicate the end
2883 * @value: the first string token input
2884 * @value2: the second string token input
2885 * @data: data associated to the token to reuse in callbacks
2886 *
2887 * Push one input token in the execution context
2888 *
2889 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2890 * a negative value in case of error.
2891 */
2892int
2893xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
2894 const xmlChar *value2, void *data) {
2895 xmlChar buf[150];
2896 int lenn, lenp, ret;
2897 xmlChar *str;
2898
2899 if (exec == NULL)
2900 return(-1);
2901 if (exec->comp == NULL)
2902 return(-1);
2903 if (exec->status != 0)
2904 return(exec->status);
2905
2906 if (value2 == NULL)
2907 return(xmlRegExecPushString(exec, value, data));
2908
2909 lenn = strlen((char *) value2);
2910 lenp = strlen((char *) value);
2911
2912 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002913 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002914 if (str == NULL) {
2915 exec->status = -1;
2916 return(-1);
2917 }
2918 } else {
2919 str = buf;
2920 }
2921 memcpy(&str[0], value, lenp);
2922 str[lenp] = '|';
2923 memcpy(&str[lenp + 1], value2, lenn);
2924 str[lenn + lenp + 1] = 0;
2925
2926 if (exec->comp->compact != NULL)
2927 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
2928 else
2929 ret = xmlRegExecPushString(exec, str, data);
2930
2931 if (str != buf)
2932 xmlFree(buf);
2933 return(ret);
2934}
2935
Daniel Veillard4255d502002-04-16 15:50:10 +00002936#if 0
2937static int
2938xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2939 xmlRegTransPtr trans;
2940 xmlRegAtomPtr atom;
2941 int ret;
2942 int codepoint, len;
2943
2944 if (exec == NULL)
2945 return(-1);
2946 if (exec->status != 0)
2947 return(exec->status);
2948
2949 while ((exec->status == 0) &&
2950 ((exec->inputString[exec->index] != 0) ||
2951 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2952
2953 /*
2954 * End of input on non-terminal state, rollback, however we may
2955 * still have epsilon like transition for counted transitions
2956 * on counters, in that case don't break too early.
2957 */
2958 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2959 goto rollback;
2960
2961 exec->transcount = 0;
2962 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2963 trans = &exec->state->trans[exec->transno];
2964 if (trans->to < 0)
2965 continue;
2966 atom = trans->atom;
2967 ret = 0;
2968 if (trans->count >= 0) {
2969 int count;
2970 xmlRegCounterPtr counter;
2971
2972 /*
2973 * A counted transition.
2974 */
2975
2976 count = exec->counts[trans->count];
2977 counter = &exec->comp->counters[trans->count];
2978#ifdef DEBUG_REGEXP_EXEC
2979 printf("testing count %d: val %d, min %d, max %d\n",
2980 trans->count, count, counter->min, counter->max);
2981#endif
2982 ret = ((count >= counter->min) && (count <= counter->max));
2983 } else if (atom == NULL) {
2984 fprintf(stderr, "epsilon transition left at runtime\n");
2985 exec->status = -2;
2986 break;
2987 } else if (exec->inputString[exec->index] != 0) {
2988 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2989 ret = xmlRegCheckCharacter(atom, codepoint);
2990 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2991 xmlRegStatePtr to = exec->comp->states[trans->to];
2992
2993 /*
2994 * this is a multiple input sequence
2995 */
2996 if (exec->state->nbTrans > exec->transno + 1) {
2997 xmlFARegExecSave(exec);
2998 }
2999 exec->transcount = 1;
3000 do {
3001 /*
3002 * Try to progress as much as possible on the input
3003 */
3004 if (exec->transcount == atom->max) {
3005 break;
3006 }
3007 exec->index += len;
3008 /*
3009 * End of input: stop here
3010 */
3011 if (exec->inputString[exec->index] == 0) {
3012 exec->index -= len;
3013 break;
3014 }
3015 if (exec->transcount >= atom->min) {
3016 int transno = exec->transno;
3017 xmlRegStatePtr state = exec->state;
3018
3019 /*
3020 * The transition is acceptable save it
3021 */
3022 exec->transno = -1; /* trick */
3023 exec->state = to;
3024 xmlFARegExecSave(exec);
3025 exec->transno = transno;
3026 exec->state = state;
3027 }
3028 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3029 len);
3030 ret = xmlRegCheckCharacter(atom, codepoint);
3031 exec->transcount++;
3032 } while (ret == 1);
3033 if (exec->transcount < atom->min)
3034 ret = 0;
3035
3036 /*
3037 * If the last check failed but one transition was found
3038 * possible, rollback
3039 */
3040 if (ret < 0)
3041 ret = 0;
3042 if (ret == 0) {
3043 goto rollback;
3044 }
3045 }
3046 }
3047 if (ret == 1) {
3048 if (exec->state->nbTrans > exec->transno + 1) {
3049 xmlFARegExecSave(exec);
3050 }
3051 if (trans->counter >= 0) {
3052#ifdef DEBUG_REGEXP_EXEC
3053 printf("Increasing count %d\n", trans->counter);
3054#endif
3055 exec->counts[trans->counter]++;
3056 }
3057#ifdef DEBUG_REGEXP_EXEC
3058 printf("entering state %d\n", trans->to);
3059#endif
3060 exec->state = exec->comp->states[trans->to];
3061 exec->transno = 0;
3062 if (trans->atom != NULL) {
3063 exec->index += len;
3064 }
3065 goto progress;
3066 } else if (ret < 0) {
3067 exec->status = -4;
3068 break;
3069 }
3070 }
3071 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3072rollback:
3073 /*
3074 * Failed to find a way out
3075 */
3076 exec->determinist = 0;
3077 xmlFARegExecRollBack(exec);
3078 }
3079progress:
3080 continue;
3081 }
3082}
3083#endif
3084/************************************************************************
3085 * *
3086 * Parser for the Shemas Datatype Regular Expressions *
3087 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3088 * *
3089 ************************************************************************/
3090
3091/**
3092 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003093 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003094 *
3095 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3096 */
3097static int
3098xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3099 int cur;
3100 int len;
3101
3102 cur = CUR_SCHAR(ctxt->cur, len);
3103 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3104 (cur == '*') || (cur == '+') || (cur == '(') ||
3105 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3106 (cur == 0x5D) || (cur == 0))
3107 return(-1);
3108 return(cur);
3109}
3110
3111/**
3112 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003113 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003114 *
3115 * [27] charProp ::= IsCategory | IsBlock
3116 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3117 * Separators | Symbols | Others
3118 * [29] Letters ::= 'L' [ultmo]?
3119 * [30] Marks ::= 'M' [nce]?
3120 * [31] Numbers ::= 'N' [dlo]?
3121 * [32] Punctuation ::= 'P' [cdseifo]?
3122 * [33] Separators ::= 'Z' [slp]?
3123 * [34] Symbols ::= 'S' [mcko]?
3124 * [35] Others ::= 'C' [cfon]?
3125 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3126 */
3127static void
3128xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3129 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003130 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003131 xmlChar *blockName = NULL;
3132
3133 cur = CUR;
3134 if (cur == 'L') {
3135 NEXT;
3136 cur = CUR;
3137 if (cur == 'u') {
3138 NEXT;
3139 type = XML_REGEXP_LETTER_UPPERCASE;
3140 } else if (cur == 'l') {
3141 NEXT;
3142 type = XML_REGEXP_LETTER_LOWERCASE;
3143 } else if (cur == 't') {
3144 NEXT;
3145 type = XML_REGEXP_LETTER_TITLECASE;
3146 } else if (cur == 'm') {
3147 NEXT;
3148 type = XML_REGEXP_LETTER_MODIFIER;
3149 } else if (cur == 'o') {
3150 NEXT;
3151 type = XML_REGEXP_LETTER_OTHERS;
3152 } else {
3153 type = XML_REGEXP_LETTER;
3154 }
3155 } else if (cur == 'M') {
3156 NEXT;
3157 cur = CUR;
3158 if (cur == 'n') {
3159 NEXT;
3160 /* nonspacing */
3161 type = XML_REGEXP_MARK_NONSPACING;
3162 } else if (cur == 'c') {
3163 NEXT;
3164 /* spacing combining */
3165 type = XML_REGEXP_MARK_SPACECOMBINING;
3166 } else if (cur == 'e') {
3167 NEXT;
3168 /* enclosing */
3169 type = XML_REGEXP_MARK_ENCLOSING;
3170 } else {
3171 /* all marks */
3172 type = XML_REGEXP_MARK;
3173 }
3174 } else if (cur == 'N') {
3175 NEXT;
3176 cur = CUR;
3177 if (cur == 'd') {
3178 NEXT;
3179 /* digital */
3180 type = XML_REGEXP_NUMBER_DECIMAL;
3181 } else if (cur == 'l') {
3182 NEXT;
3183 /* letter */
3184 type = XML_REGEXP_NUMBER_LETTER;
3185 } else if (cur == 'o') {
3186 NEXT;
3187 /* other */
3188 type = XML_REGEXP_NUMBER_OTHERS;
3189 } else {
3190 /* all numbers */
3191 type = XML_REGEXP_NUMBER;
3192 }
3193 } else if (cur == 'P') {
3194 NEXT;
3195 cur = CUR;
3196 if (cur == 'c') {
3197 NEXT;
3198 /* connector */
3199 type = XML_REGEXP_PUNCT_CONNECTOR;
3200 } else if (cur == 'd') {
3201 NEXT;
3202 /* dash */
3203 type = XML_REGEXP_PUNCT_DASH;
3204 } else if (cur == 's') {
3205 NEXT;
3206 /* open */
3207 type = XML_REGEXP_PUNCT_OPEN;
3208 } else if (cur == 'e') {
3209 NEXT;
3210 /* close */
3211 type = XML_REGEXP_PUNCT_CLOSE;
3212 } else if (cur == 'i') {
3213 NEXT;
3214 /* initial quote */
3215 type = XML_REGEXP_PUNCT_INITQUOTE;
3216 } else if (cur == 'f') {
3217 NEXT;
3218 /* final quote */
3219 type = XML_REGEXP_PUNCT_FINQUOTE;
3220 } else if (cur == 'o') {
3221 NEXT;
3222 /* other */
3223 type = XML_REGEXP_PUNCT_OTHERS;
3224 } else {
3225 /* all punctuation */
3226 type = XML_REGEXP_PUNCT;
3227 }
3228 } else if (cur == 'Z') {
3229 NEXT;
3230 cur = CUR;
3231 if (cur == 's') {
3232 NEXT;
3233 /* space */
3234 type = XML_REGEXP_SEPAR_SPACE;
3235 } else if (cur == 'l') {
3236 NEXT;
3237 /* line */
3238 type = XML_REGEXP_SEPAR_LINE;
3239 } else if (cur == 'p') {
3240 NEXT;
3241 /* paragraph */
3242 type = XML_REGEXP_SEPAR_PARA;
3243 } else {
3244 /* all separators */
3245 type = XML_REGEXP_SEPAR;
3246 }
3247 } else if (cur == 'S') {
3248 NEXT;
3249 cur = CUR;
3250 if (cur == 'm') {
3251 NEXT;
3252 type = XML_REGEXP_SYMBOL_MATH;
3253 /* math */
3254 } else if (cur == 'c') {
3255 NEXT;
3256 type = XML_REGEXP_SYMBOL_CURRENCY;
3257 /* currency */
3258 } else if (cur == 'k') {
3259 NEXT;
3260 type = XML_REGEXP_SYMBOL_MODIFIER;
3261 /* modifiers */
3262 } else if (cur == 'o') {
3263 NEXT;
3264 type = XML_REGEXP_SYMBOL_OTHERS;
3265 /* other */
3266 } else {
3267 /* all symbols */
3268 type = XML_REGEXP_SYMBOL;
3269 }
3270 } else if (cur == 'C') {
3271 NEXT;
3272 cur = CUR;
3273 if (cur == 'c') {
3274 NEXT;
3275 /* control */
3276 type = XML_REGEXP_OTHER_CONTROL;
3277 } else if (cur == 'f') {
3278 NEXT;
3279 /* format */
3280 type = XML_REGEXP_OTHER_FORMAT;
3281 } else if (cur == 'o') {
3282 NEXT;
3283 /* private use */
3284 type = XML_REGEXP_OTHER_PRIVATE;
3285 } else if (cur == 'n') {
3286 NEXT;
3287 /* not assigned */
3288 type = XML_REGEXP_OTHER_NA;
3289 } else {
3290 /* all others */
3291 type = XML_REGEXP_OTHER;
3292 }
3293 } else if (cur == 'I') {
3294 const xmlChar *start;
3295 NEXT;
3296 cur = CUR;
3297 if (cur != 's') {
3298 ERROR("IsXXXX expected");
3299 return;
3300 }
3301 NEXT;
3302 start = ctxt->cur;
3303 cur = CUR;
3304 if (((cur >= 'a') && (cur <= 'z')) ||
3305 ((cur >= 'A') && (cur <= 'Z')) ||
3306 ((cur >= '0') && (cur <= '9')) ||
3307 (cur == 0x2D)) {
3308 NEXT;
3309 cur = CUR;
3310 while (((cur >= 'a') && (cur <= 'z')) ||
3311 ((cur >= 'A') && (cur <= 'Z')) ||
3312 ((cur >= '0') && (cur <= '9')) ||
3313 (cur == 0x2D)) {
3314 NEXT;
3315 cur = CUR;
3316 }
3317 }
3318 type = XML_REGEXP_BLOCK_NAME;
3319 blockName = xmlStrndup(start, ctxt->cur - start);
3320 } else {
3321 ERROR("Unknown char property");
3322 return;
3323 }
3324 if (ctxt->atom == NULL) {
3325 ctxt->atom = xmlRegNewAtom(ctxt, type);
3326 if (ctxt->atom != NULL)
3327 ctxt->atom->valuep = blockName;
3328 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3329 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3330 type, 0, 0, blockName);
3331 }
3332}
3333
3334/**
3335 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003336 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003337 *
3338 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3339 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3340 * [25] catEsc ::= '\p{' charProp '}'
3341 * [26] complEsc ::= '\P{' charProp '}'
3342 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3343 */
3344static void
3345xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3346 int cur;
3347
3348 if (CUR == '.') {
3349 if (ctxt->atom == NULL) {
3350 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3351 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3352 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3353 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3354 }
3355 NEXT;
3356 return;
3357 }
3358 if (CUR != '\\') {
3359 ERROR("Escaped sequence: expecting \\");
3360 return;
3361 }
3362 NEXT;
3363 cur = CUR;
3364 if (cur == 'p') {
3365 NEXT;
3366 if (CUR != '{') {
3367 ERROR("Expecting '{'");
3368 return;
3369 }
3370 NEXT;
3371 xmlFAParseCharProp(ctxt);
3372 if (CUR != '}') {
3373 ERROR("Expecting '}'");
3374 return;
3375 }
3376 NEXT;
3377 } else if (cur == 'P') {
3378 NEXT;
3379 if (CUR != '{') {
3380 ERROR("Expecting '{'");
3381 return;
3382 }
3383 NEXT;
3384 xmlFAParseCharProp(ctxt);
3385 ctxt->atom->neg = 1;
3386 if (CUR != '}') {
3387 ERROR("Expecting '}'");
3388 return;
3389 }
3390 NEXT;
3391 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3392 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3393 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3394 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3395 (cur == 0x5E)) {
3396 if (ctxt->atom == NULL) {
3397 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3398 if (ctxt->atom != NULL)
3399 ctxt->atom->codepoint = cur;
3400 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3401 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3402 XML_REGEXP_CHARVAL, cur, cur, NULL);
3403 }
3404 NEXT;
3405 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3406 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3407 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003408 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003409
3410 switch (cur) {
3411 case 's':
3412 type = XML_REGEXP_ANYSPACE;
3413 break;
3414 case 'S':
3415 type = XML_REGEXP_NOTSPACE;
3416 break;
3417 case 'i':
3418 type = XML_REGEXP_INITNAME;
3419 break;
3420 case 'I':
3421 type = XML_REGEXP_NOTINITNAME;
3422 break;
3423 case 'c':
3424 type = XML_REGEXP_NAMECHAR;
3425 break;
3426 case 'C':
3427 type = XML_REGEXP_NOTNAMECHAR;
3428 break;
3429 case 'd':
3430 type = XML_REGEXP_DECIMAL;
3431 break;
3432 case 'D':
3433 type = XML_REGEXP_NOTDECIMAL;
3434 break;
3435 case 'w':
3436 type = XML_REGEXP_REALCHAR;
3437 break;
3438 case 'W':
3439 type = XML_REGEXP_NOTREALCHAR;
3440 break;
3441 }
3442 NEXT;
3443 if (ctxt->atom == NULL) {
3444 ctxt->atom = xmlRegNewAtom(ctxt, type);
3445 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3446 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3447 type, 0, 0, NULL);
3448 }
3449 }
3450}
3451
3452/**
3453 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003454 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003455 *
3456 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3457 */
3458static int
3459xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3460 int ret = 0, cur;
3461
3462 if ((CUR != '&') || (NXT(1) != '#'))
3463 return(-1);
3464 NEXT;
3465 NEXT;
3466 cur = CUR;
3467 if (cur == 'x') {
3468 NEXT;
3469 cur = CUR;
3470 if (((cur >= '0') && (cur <= '9')) ||
3471 ((cur >= 'a') && (cur <= 'f')) ||
3472 ((cur >= 'A') && (cur <= 'F'))) {
3473 while (((cur >= '0') && (cur <= '9')) ||
3474 ((cur >= 'A') && (cur <= 'F'))) {
3475 if ((cur >= '0') && (cur <= '9'))
3476 ret = ret * 16 + cur - '0';
3477 else if ((cur >= 'a') && (cur <= 'f'))
3478 ret = ret * 16 + 10 + (cur - 'a');
3479 else
3480 ret = ret * 16 + 10 + (cur - 'A');
3481 NEXT;
3482 cur = CUR;
3483 }
3484 } else {
3485 ERROR("Char ref: expecting [0-9A-F]");
3486 return(-1);
3487 }
3488 } else {
3489 if ((cur >= '0') && (cur <= '9')) {
3490 while ((cur >= '0') && (cur <= '9')) {
3491 ret = ret * 10 + cur - '0';
3492 NEXT;
3493 cur = CUR;
3494 }
3495 } else {
3496 ERROR("Char ref: expecting [0-9]");
3497 return(-1);
3498 }
3499 }
3500 if (cur != ';') {
3501 ERROR("Char ref: expecting ';'");
3502 return(-1);
3503 } else {
3504 NEXT;
3505 }
3506 return(ret);
3507}
3508
3509/**
3510 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003511 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003512 *
3513 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3514 * [18] seRange ::= charOrEsc '-' charOrEsc
3515 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3516 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3517 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3518 */
3519static void
3520xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
3521 int cur;
3522 int start = -1;
3523 int end = -1;
3524
3525 if ((CUR == '&') && (NXT(1) == '#')) {
3526 end = start = xmlFAParseCharRef(ctxt);
3527 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3528 XML_REGEXP_CHARVAL, start, end, NULL);
3529 return;
3530 }
3531 cur = CUR;
3532 if (cur == '\\') {
3533 NEXT;
3534 cur = CUR;
3535 switch (cur) {
3536 case 'n': start = 0xA; break;
3537 case 'r': start = 0xD; break;
3538 case 't': start = 0x9; break;
3539 case '\\': case '|': case '.': case '-': case '^': case '?':
3540 case '*': case '+': case '{': case '}': case '(': case ')':
3541 case '[': case ']':
3542 start = cur; break;
3543 default:
3544 ERROR("Invalid escape value");
3545 return;
3546 }
3547 end = start;
3548 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3549 end = start = cur;
3550 } else {
3551 ERROR("Expecting a char range");
3552 return;
3553 }
3554 NEXT;
3555 if (start == '-') {
3556 return;
3557 }
3558 cur = CUR;
3559 if (cur != '-') {
3560 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3561 XML_REGEXP_CHARVAL, start, end, NULL);
3562 return;
3563 }
3564 NEXT;
3565 cur = CUR;
3566 if (cur == '\\') {
3567 NEXT;
3568 cur = CUR;
3569 switch (cur) {
3570 case 'n': end = 0xA; break;
3571 case 'r': end = 0xD; break;
3572 case 't': end = 0x9; break;
3573 case '\\': case '|': case '.': case '-': case '^': case '?':
3574 case '*': case '+': case '{': case '}': case '(': case ')':
3575 case '[': case ']':
3576 end = cur; break;
3577 default:
3578 ERROR("Invalid escape value");
3579 return;
3580 }
3581 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3582 end = cur;
3583 } else {
3584 ERROR("Expecting the end of a char range");
3585 return;
3586 }
3587 NEXT;
3588 /* TODO check that the values are acceptable character ranges for XML */
3589 if (end < start) {
3590 ERROR("End of range is before start of range");
3591 } else {
3592 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3593 XML_REGEXP_CHARVAL, start, end, NULL);
3594 }
3595 return;
3596}
3597
3598/**
3599 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003600 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003601 *
3602 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3603 */
3604static void
3605xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3606 do {
3607 if ((CUR == '\\') || (CUR == '.')) {
3608 xmlFAParseCharClassEsc(ctxt);
3609 } else {
3610 xmlFAParseCharRange(ctxt);
3611 }
3612 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3613 (ctxt->error == 0));
3614}
3615
3616/**
3617 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003618 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003619 *
3620 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3621 * [15] negCharGroup ::= '^' posCharGroup
3622 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3623 * [12] charClassExpr ::= '[' charGroup ']'
3624 */
3625static void
3626xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3627 int n = ctxt->neg;
3628 while ((CUR != ']') && (ctxt->error == 0)) {
3629 if (CUR == '^') {
3630 int neg = ctxt->neg;
3631
3632 NEXT;
3633 ctxt->neg = !ctxt->neg;
3634 xmlFAParsePosCharGroup(ctxt);
3635 ctxt->neg = neg;
3636 } else if (CUR == '-') {
3637 NEXT;
3638 ctxt->neg = !ctxt->neg;
3639 if (CUR != '[') {
3640 ERROR("charClassExpr: '[' expected");
3641 break;
3642 }
3643 NEXT;
3644 xmlFAParseCharGroup(ctxt);
3645 if (CUR == ']') {
3646 NEXT;
3647 } else {
3648 ERROR("charClassExpr: ']' expected");
3649 break;
3650 }
3651 break;
3652 } else if (CUR != ']') {
3653 xmlFAParsePosCharGroup(ctxt);
3654 }
3655 }
3656 ctxt->neg = n;
3657}
3658
3659/**
3660 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003661 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003662 *
3663 * [11] charClass ::= charClassEsc | charClassExpr
3664 * [12] charClassExpr ::= '[' charGroup ']'
3665 */
3666static void
3667xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3668 if (CUR == '[') {
3669 NEXT;
3670 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3671 if (ctxt->atom == NULL)
3672 return;
3673 xmlFAParseCharGroup(ctxt);
3674 if (CUR == ']') {
3675 NEXT;
3676 } else {
3677 ERROR("xmlFAParseCharClass: ']' expected");
3678 }
3679 } else {
3680 xmlFAParseCharClassEsc(ctxt);
3681 }
3682}
3683
3684/**
3685 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003686 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003687 *
3688 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003689 *
3690 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003691 */
3692static int
3693xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3694 int ret = 0;
3695 int ok = 0;
3696
3697 while ((CUR >= '0') && (CUR <= '9')) {
3698 ret = ret * 10 + (CUR - '0');
3699 ok = 1;
3700 NEXT;
3701 }
3702 if (ok != 1) {
3703 return(-1);
3704 }
3705 return(ret);
3706}
3707
3708/**
3709 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003710 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003711 *
3712 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3713 * [5] quantity ::= quantRange | quantMin | QuantExact
3714 * [6] quantRange ::= QuantExact ',' QuantExact
3715 * [7] quantMin ::= QuantExact ','
3716 * [8] QuantExact ::= [0-9]+
3717 */
3718static int
3719xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3720 int cur;
3721
3722 cur = CUR;
3723 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3724 if (ctxt->atom != NULL) {
3725 if (cur == '?')
3726 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3727 else if (cur == '*')
3728 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3729 else if (cur == '+')
3730 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3731 }
3732 NEXT;
3733 return(1);
3734 }
3735 if (cur == '{') {
3736 int min = 0, max = 0;
3737
3738 NEXT;
3739 cur = xmlFAParseQuantExact(ctxt);
3740 if (cur >= 0)
3741 min = cur;
3742 if (CUR == ',') {
3743 NEXT;
3744 cur = xmlFAParseQuantExact(ctxt);
3745 if (cur >= 0)
3746 max = cur;
3747 }
3748 if (CUR == '}') {
3749 NEXT;
3750 } else {
3751 ERROR("Unterminated quantifier");
3752 }
3753 if (max == 0)
3754 max = min;
3755 if (ctxt->atom != NULL) {
3756 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3757 ctxt->atom->min = min;
3758 ctxt->atom->max = max;
3759 }
3760 return(1);
3761 }
3762 return(0);
3763}
3764
3765/**
3766 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003767 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003768 *
3769 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3770 */
3771static int
3772xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3773 int codepoint, len;
3774
3775 codepoint = xmlFAIsChar(ctxt);
3776 if (codepoint > 0) {
3777 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3778 if (ctxt->atom == NULL)
3779 return(-1);
3780 codepoint = CUR_SCHAR(ctxt->cur, len);
3781 ctxt->atom->codepoint = codepoint;
3782 NEXTL(len);
3783 return(1);
3784 } else if (CUR == '|') {
3785 return(0);
3786 } else if (CUR == 0) {
3787 return(0);
3788 } else if (CUR == ')') {
3789 return(0);
3790 } else if (CUR == '(') {
3791 xmlRegStatePtr start, oldend;
3792
3793 NEXT;
3794 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3795 start = ctxt->state;
3796 oldend = ctxt->end;
3797 ctxt->end = NULL;
3798 ctxt->atom = NULL;
3799 xmlFAParseRegExp(ctxt, 0);
3800 if (CUR == ')') {
3801 NEXT;
3802 } else {
3803 ERROR("xmlFAParseAtom: expecting ')'");
3804 }
3805 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3806 if (ctxt->atom == NULL)
3807 return(-1);
3808 ctxt->atom->start = start;
3809 ctxt->atom->stop = ctxt->state;
3810 ctxt->end = oldend;
3811 return(1);
3812 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3813 xmlFAParseCharClass(ctxt);
3814 return(1);
3815 }
3816 return(0);
3817}
3818
3819/**
3820 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003821 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003822 *
3823 * [3] piece ::= atom quantifier?
3824 */
3825static int
3826xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3827 int ret;
3828
3829 ctxt->atom = NULL;
3830 ret = xmlFAParseAtom(ctxt);
3831 if (ret == 0)
3832 return(0);
3833 if (ctxt->atom == NULL) {
3834 ERROR("internal: no atom generated");
3835 }
3836 xmlFAParseQuantifier(ctxt);
3837 return(1);
3838}
3839
3840/**
3841 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003842 * @ctxt: a regexp parser context
3843 * @first: is taht the first
Daniel Veillard4255d502002-04-16 15:50:10 +00003844 *
3845 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003846 8
Daniel Veillard4255d502002-04-16 15:50:10 +00003847 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003848static int
Daniel Veillard4255d502002-04-16 15:50:10 +00003849xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, int first) {
3850 xmlRegStatePtr previous;
3851 xmlRegAtomPtr prevatom = NULL;
3852 int ret;
3853
3854 previous = ctxt->state;
3855 ret = xmlFAParsePiece(ctxt);
3856 if (ret != 0) {
3857 if (first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003858 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
3859 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003860 previous = ctxt->state;
3861 } else {
3862 prevatom = ctxt->atom;
3863 }
3864 ctxt->atom = NULL;
3865 }
3866 while ((ret != 0) && (ctxt->error == 0)) {
3867 ret = xmlFAParsePiece(ctxt);
3868 if (ret != 0) {
3869 if (first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003870 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3871 ctxt->atom) < 0)
3872 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003873 } else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003874 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3875 prevatom) < 0)
3876 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003877 prevatom = ctxt->atom;
3878 }
3879 previous = ctxt->state;
3880 ctxt->atom = NULL;
3881 }
3882 }
3883 if (!first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003884 if (xmlFAGenerateTransitions(ctxt, previous, ctxt->end, prevatom) < 0)
3885 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003886 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003887 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00003888}
3889
3890/**
3891 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003892 * @ctxt: a regexp parser context
3893 * @top: is that the top-level expressions ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003894 *
3895 * [1] regExp ::= branch ( '|' branch )*
3896 */
3897static void
3898xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
3899 xmlRegStatePtr start, end, oldend;
3900
3901 oldend = ctxt->end;
3902
3903 start = ctxt->state;
3904 xmlFAParseBranch(ctxt, (ctxt->end == NULL));
3905 if (CUR != '|') {
3906 ctxt->end = ctxt->state;
3907 return;
3908 }
3909 end = ctxt->state;
3910 while ((CUR == '|') && (ctxt->error == 0)) {
3911 NEXT;
3912 ctxt->state = start;
3913 ctxt->end = end;
3914 xmlFAParseBranch(ctxt, 0);
3915 }
3916 if (!top)
3917 ctxt->end = oldend;
3918}
3919
3920/************************************************************************
3921 * *
3922 * The basic API *
3923 * *
3924 ************************************************************************/
3925
3926/**
3927 * xmlRegexpPrint:
3928 * @output: the file for the output debug
3929 * @regexp: the compiled regexp
3930 *
3931 * Print the content of the compiled regular expression
3932 */
3933void
3934xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3935 int i;
3936
3937 fprintf(output, " regexp: ");
3938 if (regexp == NULL) {
3939 fprintf(output, "NULL\n");
3940 return;
3941 }
3942 fprintf(output, "'%s' ", regexp->string);
3943 fprintf(output, "\n");
3944 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3945 for (i = 0;i < regexp->nbAtoms; i++) {
3946 fprintf(output, " %02d ", i);
3947 xmlRegPrintAtom(output, regexp->atoms[i]);
3948 }
3949 fprintf(output, "%d states:", regexp->nbStates);
3950 fprintf(output, "\n");
3951 for (i = 0;i < regexp->nbStates; i++) {
3952 xmlRegPrintState(output, regexp->states[i]);
3953 }
3954 fprintf(output, "%d counters:\n", regexp->nbCounters);
3955 for (i = 0;i < regexp->nbCounters; i++) {
3956 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
3957 regexp->counters[i].max);
3958 }
3959}
3960
3961/**
3962 * xmlRegexpCompile:
3963 * @regexp: a regular expression string
3964 *
3965 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
3966 * Appendix F and build an automata suitable for testing strings against
3967 * that regular expression
3968 *
3969 * Returns the compiled expression or NULL in case of error
3970 */
3971xmlRegexpPtr
3972xmlRegexpCompile(const xmlChar *regexp) {
3973 xmlRegexpPtr ret;
3974 xmlRegParserCtxtPtr ctxt;
3975
3976 ctxt = xmlRegNewParserCtxt(regexp);
3977 if (ctxt == NULL)
3978 return(NULL);
3979
3980 /* initialize the parser */
3981 ctxt->end = NULL;
3982 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3983 xmlRegStatePush(ctxt, ctxt->start);
3984
3985 /* parse the expression building an automata */
3986 xmlFAParseRegExp(ctxt, 1);
3987 if (CUR != 0) {
3988 ERROR("xmlFAParseRegExp: extra characters");
3989 }
3990 ctxt->end = ctxt->state;
3991 ctxt->start->type = XML_REGEXP_START_STATE;
3992 ctxt->end->type = XML_REGEXP_FINAL_STATE;
3993
3994 /* remove the Epsilon except for counted transitions */
3995 xmlFAEliminateEpsilonTransitions(ctxt);
3996
3997
3998 if (ctxt->error != 0) {
3999 xmlRegFreeParserCtxt(ctxt);
4000 return(NULL);
4001 }
4002 ret = xmlRegEpxFromParse(ctxt);
4003 xmlRegFreeParserCtxt(ctxt);
4004 return(ret);
4005}
4006
4007/**
4008 * xmlRegexpExec:
4009 * @comp: the compiled regular expression
4010 * @content: the value to check against the regular expression
4011 *
4012 * Check if the regular expression generate the value
4013 *
4014 * Returns 1 if it matches, 0 if not and a negativa value in case of error
4015 */
4016int
4017xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4018 if ((comp == NULL) || (content == NULL))
4019 return(-1);
4020 return(xmlFARegExec(comp, content));
4021}
4022
4023/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004024 * xmlRegexpIsDeterminist:
4025 * @comp: the compiled regular expression
4026 *
4027 * Check if the regular expression is determinist
4028 *
4029 * Returns 1 if it yes, 0 if not and a negativa value in case of error
4030 */
4031int
4032xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4033 xmlAutomataPtr am;
4034 int ret;
4035
4036 if (comp == NULL)
4037 return(-1);
4038 if (comp->determinist != -1)
4039 return(comp->determinist);
4040
4041 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004042 if (am->states != NULL) {
4043 int i;
4044
4045 for (i = 0;i < am->nbStates;i++)
4046 xmlRegFreeState(am->states[i]);
4047 xmlFree(am->states);
4048 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004049 am->nbAtoms = comp->nbAtoms;
4050 am->atoms = comp->atoms;
4051 am->nbStates = comp->nbStates;
4052 am->states = comp->states;
4053 am->determinist = -1;
4054 ret = xmlFAComputesDeterminism(am);
4055 am->atoms = NULL;
4056 am->states = NULL;
4057 xmlFreeAutomata(am);
4058 return(ret);
4059}
4060
4061/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004062 * xmlRegFreeRegexp:
4063 * @regexp: the regexp
4064 *
4065 * Free a regexp
4066 */
4067void
4068xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4069 int i;
4070 if (regexp == NULL)
4071 return;
4072
4073 if (regexp->string != NULL)
4074 xmlFree(regexp->string);
4075 if (regexp->states != NULL) {
4076 for (i = 0;i < regexp->nbStates;i++)
4077 xmlRegFreeState(regexp->states[i]);
4078 xmlFree(regexp->states);
4079 }
4080 if (regexp->atoms != NULL) {
4081 for (i = 0;i < regexp->nbAtoms;i++)
4082 xmlRegFreeAtom(regexp->atoms[i]);
4083 xmlFree(regexp->atoms);
4084 }
4085 if (regexp->counters != NULL)
4086 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004087 if (regexp->compact != NULL)
4088 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004089 if (regexp->transdata != NULL)
4090 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004091 if (regexp->stringMap != NULL) {
4092 for (i = 0; i < regexp->nbstrings;i++)
4093 xmlFree(regexp->stringMap[i]);
4094 xmlFree(regexp->stringMap);
4095 }
4096
Daniel Veillard4255d502002-04-16 15:50:10 +00004097 xmlFree(regexp);
4098}
4099
4100#ifdef LIBXML_AUTOMATA_ENABLED
4101/************************************************************************
4102 * *
4103 * The Automata interface *
4104 * *
4105 ************************************************************************/
4106
4107/**
4108 * xmlNewAutomata:
4109 *
4110 * Create a new automata
4111 *
4112 * Returns the new object or NULL in case of failure
4113 */
4114xmlAutomataPtr
4115xmlNewAutomata(void) {
4116 xmlAutomataPtr ctxt;
4117
4118 ctxt = xmlRegNewParserCtxt(NULL);
4119 if (ctxt == NULL)
4120 return(NULL);
4121
4122 /* initialize the parser */
4123 ctxt->end = NULL;
4124 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004125 if (ctxt->start == NULL) {
4126 xmlFreeAutomata(ctxt);
4127 return(NULL);
4128 }
4129 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4130 xmlRegFreeState(ctxt->start);
4131 xmlFreeAutomata(ctxt);
4132 return(NULL);
4133 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004134
4135 return(ctxt);
4136}
4137
4138/**
4139 * xmlFreeAutomata:
4140 * @am: an automata
4141 *
4142 * Free an automata
4143 */
4144void
4145xmlFreeAutomata(xmlAutomataPtr am) {
4146 if (am == NULL)
4147 return;
4148 xmlRegFreeParserCtxt(am);
4149}
4150
4151/**
4152 * xmlAutomataGetInitState:
4153 * @am: an automata
4154 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004155 * Initial state lookup
4156 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004157 * Returns the initial state of the automata
4158 */
4159xmlAutomataStatePtr
4160xmlAutomataGetInitState(xmlAutomataPtr am) {
4161 if (am == NULL)
4162 return(NULL);
4163 return(am->start);
4164}
4165
4166/**
4167 * xmlAutomataSetFinalState:
4168 * @am: an automata
4169 * @state: a state in this automata
4170 *
4171 * Makes that state a final state
4172 *
4173 * Returns 0 or -1 in case of error
4174 */
4175int
4176xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4177 if ((am == NULL) || (state == NULL))
4178 return(-1);
4179 state->type = XML_REGEXP_FINAL_STATE;
4180 return(0);
4181}
4182
4183/**
4184 * xmlAutomataNewTransition:
4185 * @am: an automata
4186 * @from: the starting point of the transition
4187 * @to: the target point of the transition or NULL
4188 * @token: the input string associated to that transition
4189 * @data: data passed to the callback function if the transition is activated
4190 *
4191 * If @to is NULL, this create first a new target state in the automata
4192 * and then adds a transition from the @from state to the target state
4193 * activated by the value of @token
4194 *
4195 * Returns the target state or NULL in case of error
4196 */
4197xmlAutomataStatePtr
4198xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4199 xmlAutomataStatePtr to, const xmlChar *token,
4200 void *data) {
4201 xmlRegAtomPtr atom;
4202
4203 if ((am == NULL) || (from == NULL) || (token == NULL))
4204 return(NULL);
4205 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004206 if (atom == NULL)
4207 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004208 atom->data = data;
4209 if (atom == NULL)
4210 return(NULL);
4211 atom->valuep = xmlStrdup(token);
4212
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004213 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4214 xmlRegFreeAtom(atom);
4215 return(NULL);
4216 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004217 if (to == NULL)
4218 return(am->state);
4219 return(to);
4220}
4221
4222/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004223 * xmlAutomataNewTransition2:
4224 * @am: an automata
4225 * @from: the starting point of the transition
4226 * @to: the target point of the transition or NULL
4227 * @token: the first input string associated to that transition
4228 * @token2: the second input string associated to that transition
4229 * @data: data passed to the callback function if the transition is activated
4230 *
4231 * If @to is NULL, this create first a new target state in the automata
4232 * and then adds a transition from the @from state to the target state
4233 * activated by the value of @token
4234 *
4235 * Returns the target state or NULL in case of error
4236 */
4237xmlAutomataStatePtr
4238xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4239 xmlAutomataStatePtr to, const xmlChar *token,
4240 const xmlChar *token2, void *data) {
4241 xmlRegAtomPtr atom;
4242
4243 if ((am == NULL) || (from == NULL) || (token == NULL))
4244 return(NULL);
4245 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4246 atom->data = data;
4247 if (atom == NULL)
4248 return(NULL);
4249 if ((token2 == NULL) || (*token2 == 0)) {
4250 atom->valuep = xmlStrdup(token);
4251 } else {
4252 int lenn, lenp;
4253 xmlChar *str;
4254
4255 lenn = strlen((char *) token2);
4256 lenp = strlen((char *) token);
4257
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004258 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004259 if (str == NULL) {
4260 xmlRegFreeAtom(atom);
4261 return(NULL);
4262 }
4263 memcpy(&str[0], token, lenp);
4264 str[lenp] = '|';
4265 memcpy(&str[lenp + 1], token2, lenn);
4266 str[lenn + lenp + 1] = 0;
4267
4268 atom->valuep = str;
4269 }
4270
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004271 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4272 xmlRegFreeAtom(atom);
4273 return(NULL);
4274 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004275 if (to == NULL)
4276 return(am->state);
4277 return(to);
4278}
4279
4280/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004281 * xmlAutomataNewCountTrans:
4282 * @am: an automata
4283 * @from: the starting point of the transition
4284 * @to: the target point of the transition or NULL
4285 * @token: the input string associated to that transition
4286 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004287 * @max: the maximum successive occurences of token
4288 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004289 *
4290 * If @to is NULL, this create first a new target state in the automata
4291 * and then adds a transition from the @from state to the target state
4292 * activated by a succession of input of value @token and whose number
4293 * is between @min and @max
4294 *
4295 * Returns the target state or NULL in case of error
4296 */
4297xmlAutomataStatePtr
4298xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4299 xmlAutomataStatePtr to, const xmlChar *token,
4300 int min, int max, void *data) {
4301 xmlRegAtomPtr atom;
4302
4303 if ((am == NULL) || (from == NULL) || (token == NULL))
4304 return(NULL);
4305 if (min < 0)
4306 return(NULL);
4307 if ((max < min) || (max < 1))
4308 return(NULL);
4309 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4310 if (atom == NULL)
4311 return(NULL);
4312 atom->valuep = xmlStrdup(token);
4313 atom->data = data;
4314 if (min == 0)
4315 atom->min = 1;
4316 else
4317 atom->min = min;
4318 atom->max = max;
4319
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004320 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4321 xmlRegFreeAtom(atom);
4322 return(NULL);
4323 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004324 if (to == NULL)
4325 to = am->state;
4326 if (to == NULL)
4327 return(NULL);
4328 if (min == 0)
4329 xmlFAGenerateEpsilonTransition(am, from, to);
4330 return(to);
4331}
4332
4333/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004334 * xmlAutomataNewOnceTrans:
4335 * @am: an automata
4336 * @from: the starting point of the transition
4337 * @to: the target point of the transition or NULL
4338 * @token: the input string associated to that transition
4339 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004340 * @max: the maximum successive occurences of token
4341 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00004342 *
4343 * If @to is NULL, this create first a new target state in the automata
4344 * and then adds a transition from the @from state to the target state
4345 * activated by a succession of input of value @token and whose number
4346 * is between @min and @max, moreover that transistion can only be crossed
4347 * once.
4348 *
4349 * Returns the target state or NULL in case of error
4350 */
4351xmlAutomataStatePtr
4352xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4353 xmlAutomataStatePtr to, const xmlChar *token,
4354 int min, int max, void *data) {
4355 xmlRegAtomPtr atom;
4356 int counter;
4357
4358 if ((am == NULL) || (from == NULL) || (token == NULL))
4359 return(NULL);
4360 if (min < 1)
4361 return(NULL);
4362 if ((max < min) || (max < 1))
4363 return(NULL);
4364 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4365 if (atom == NULL)
4366 return(NULL);
4367 atom->valuep = xmlStrdup(token);
4368 atom->data = data;
4369 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4370 if (min == 0)
4371 atom->min = 1;
4372 else
4373 atom->min = min;
4374 atom->max = max;
4375 /*
4376 * associate a counter to the transition.
4377 */
4378 counter = xmlRegGetCounter(am);
4379 am->counters[counter].min = 1;
4380 am->counters[counter].max = 1;
4381
4382 /* xmlFAGenerateTransitions(am, from, to, atom); */
4383 if (to == NULL) {
4384 to = xmlRegNewState(am);
4385 xmlRegStatePush(am, to);
4386 }
4387 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4388 xmlRegAtomPush(am, atom);
4389 am->state = to;
4390 if (to == NULL)
4391 to = am->state;
4392 if (to == NULL)
4393 return(NULL);
4394 return(to);
4395}
4396
4397/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004398 * xmlAutomataNewState:
4399 * @am: an automata
4400 *
4401 * Create a new disconnected state in the automata
4402 *
4403 * Returns the new state or NULL in case of error
4404 */
4405xmlAutomataStatePtr
4406xmlAutomataNewState(xmlAutomataPtr am) {
4407 xmlAutomataStatePtr to;
4408
4409 if (am == NULL)
4410 return(NULL);
4411 to = xmlRegNewState(am);
4412 xmlRegStatePush(am, to);
4413 return(to);
4414}
4415
4416/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004417 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004418 * @am: an automata
4419 * @from: the starting point of the transition
4420 * @to: the target point of the transition or NULL
4421 *
4422 * If @to is NULL, this create first a new target state in the automata
4423 * and then adds a an epsilon transition from the @from state to the
4424 * target state
4425 *
4426 * Returns the target state or NULL in case of error
4427 */
4428xmlAutomataStatePtr
4429xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4430 xmlAutomataStatePtr to) {
4431 if ((am == NULL) || (from == NULL))
4432 return(NULL);
4433 xmlFAGenerateEpsilonTransition(am, from, to);
4434 if (to == NULL)
4435 return(am->state);
4436 return(to);
4437}
4438
Daniel Veillardb509f152002-04-17 16:28:10 +00004439/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004440 * xmlAutomataNewAllTrans:
4441 * @am: an automata
4442 * @from: the starting point of the transition
4443 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004444 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004445 *
4446 * If @to is NULL, this create first a new target state in the automata
4447 * and then adds a an ALL transition from the @from state to the
4448 * target state. That transition is an epsilon transition allowed only when
4449 * all transitions from the @from node have been activated.
4450 *
4451 * Returns the target state or NULL in case of error
4452 */
4453xmlAutomataStatePtr
4454xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004455 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004456 if ((am == NULL) || (from == NULL))
4457 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004458 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004459 if (to == NULL)
4460 return(am->state);
4461 return(to);
4462}
4463
4464/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004465 * xmlAutomataNewCounter:
4466 * @am: an automata
4467 * @min: the minimal value on the counter
4468 * @max: the maximal value on the counter
4469 *
4470 * Create a new counter
4471 *
4472 * Returns the counter number or -1 in case of error
4473 */
4474int
4475xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4476 int ret;
4477
4478 if (am == NULL)
4479 return(-1);
4480
4481 ret = xmlRegGetCounter(am);
4482 if (ret < 0)
4483 return(-1);
4484 am->counters[ret].min = min;
4485 am->counters[ret].max = max;
4486 return(ret);
4487}
4488
4489/**
4490 * xmlAutomataNewCountedTrans:
4491 * @am: an automata
4492 * @from: the starting point of the transition
4493 * @to: the target point of the transition or NULL
4494 * @counter: the counter associated to that transition
4495 *
4496 * If @to is NULL, this create first a new target state in the automata
4497 * and then adds an epsilon transition from the @from state to the target state
4498 * which will increment the counter provided
4499 *
4500 * Returns the target state or NULL in case of error
4501 */
4502xmlAutomataStatePtr
4503xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4504 xmlAutomataStatePtr to, int counter) {
4505 if ((am == NULL) || (from == NULL) || (counter < 0))
4506 return(NULL);
4507 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4508 if (to == NULL)
4509 return(am->state);
4510 return(to);
4511}
4512
4513/**
4514 * xmlAutomataNewCounterTrans:
4515 * @am: an automata
4516 * @from: the starting point of the transition
4517 * @to: the target point of the transition or NULL
4518 * @counter: the counter associated to that transition
4519 *
4520 * If @to is NULL, this create first a new target state in the automata
4521 * and then adds an epsilon transition from the @from state to the target state
4522 * which will be allowed only if the counter is within the right range.
4523 *
4524 * Returns the target state or NULL in case of error
4525 */
4526xmlAutomataStatePtr
4527xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4528 xmlAutomataStatePtr to, int counter) {
4529 if ((am == NULL) || (from == NULL) || (counter < 0))
4530 return(NULL);
4531 xmlFAGenerateCountedTransition(am, from, to, counter);
4532 if (to == NULL)
4533 return(am->state);
4534 return(to);
4535}
Daniel Veillard4255d502002-04-16 15:50:10 +00004536
4537/**
4538 * xmlAutomataCompile:
4539 * @am: an automata
4540 *
4541 * Compile the automata into a Reg Exp ready for being executed.
4542 * The automata should be free after this point.
4543 *
4544 * Returns the compiled regexp or NULL in case of error
4545 */
4546xmlRegexpPtr
4547xmlAutomataCompile(xmlAutomataPtr am) {
4548 xmlRegexpPtr ret;
4549
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004550 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004551 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004552 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004553 ret = xmlRegEpxFromParse(am);
4554
4555 return(ret);
4556}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004557
4558/**
4559 * xmlAutomataIsDeterminist:
4560 * @am: an automata
4561 *
4562 * Checks if an automata is determinist.
4563 *
4564 * Returns 1 if true, 0 if not, and -1 in case of error
4565 */
4566int
4567xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4568 int ret;
4569
4570 if (am == NULL)
4571 return(-1);
4572
4573 ret = xmlFAComputesDeterminism(am);
4574 return(ret);
4575}
Daniel Veillard4255d502002-04-16 15:50:10 +00004576#endif /* LIBXML_AUTOMATA_ENABLED */
4577#endif /* LIBXML_REGEXP_ENABLED */