blob: 6ea1bfe41352b4d104450b0351462fca900475e8 [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
35#define ERROR(str) ctxt->error = 1; \
36 xmlGenericError(xmlGenericErrorContext, "Regexp: %s: %s\n", str, ctxt->cur)
37#define NEXT ctxt->cur++
38#define CUR (*(ctxt->cur))
39#define NXT(index) (ctxt->cur[index])
40
41#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
42#define NEXTL(l) ctxt->cur += l;
43
Daniel Veillarde19fc232002-04-22 16:01:24 +000044/**
45 * TODO:
46 *
47 * macro to flag unimplemented blocks
48 */
49#define TODO \
50 xmlGenericError(xmlGenericErrorContext, \
51 "Unimplemented block at %s:%d\n", \
52 __FILE__, __LINE__);
53
Daniel Veillard4255d502002-04-16 15:50:10 +000054
55/************************************************************************
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/************************************************************************
316 * *
317 * Allocation/Deallocation *
318 * *
319 ************************************************************************/
320
Daniel Veillard23e73572002-09-19 19:56:43 +0000321static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000322/**
323 * xmlRegEpxFromParse:
324 * @ctxt: the parser context used to build it
325 *
326 * Allocate a new regexp and fill it with the reult from the parser
327 *
328 * Returns the new regexp or NULL in case of error
329 */
330static xmlRegexpPtr
331xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
332 xmlRegexpPtr ret;
333
334 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000335 if (ret == NULL) {
336 xmlGenericError(xmlGenericErrorContext,
337 "out of memory compiling regexp\n");
Daniel Veillard4255d502002-04-16 15:50:10 +0000338 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000339 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000340 memset(ret, 0, sizeof(xmlRegexp));
341 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000342 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000343 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000344 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000345 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000346 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000347 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000348 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000349
350 if ((ret->determinist != 0) &&
351 (ret->nbCounters == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000352 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000353 (ret->atoms[0] != NULL) &&
354 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
355 int i, j, nbstates = 0, nbatoms = 0;
356 int *stateRemap;
357 int *stringRemap;
358 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000359 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000360 xmlChar **stringMap;
361 xmlChar *value;
362
363 /*
364 * Switch to a compact representation
365 * 1/ counting the effective number of states left
366 * 2/ conting the unique number of atoms, and check that
367 * they are all of the string type
368 * 3/ build a table state x atom for the transitions
369 */
370
371 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000372 if (stateRemap == NULL) {
373 xmlGenericError(xmlGenericErrorContext,
374 "out of memory compiling regexp\n");
375 xmlFree(ret);
376 return(NULL);
377 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000378 for (i = 0;i < ret->nbStates;i++) {
379 if (ret->states[i] != NULL) {
380 stateRemap[i] = nbstates;
381 nbstates++;
382 } else {
383 stateRemap[i] = -1;
384 }
385 }
386#ifdef DEBUG_COMPACTION
387 printf("Final: %d states\n", nbstates);
388#endif
389 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000390 if (stringMap == NULL) {
391 xmlGenericError(xmlGenericErrorContext,
392 "out of memory compiling regexp\n");
393 xmlFree(stateRemap);
394 xmlFree(ret);
395 return(NULL);
396 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000397 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000398 if (stringRemap == NULL) {
399 xmlGenericError(xmlGenericErrorContext,
400 "out of memory compiling regexp\n");
401 xmlFree(stringMap);
402 xmlFree(stateRemap);
403 xmlFree(ret);
404 return(NULL);
405 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000406 for (i = 0;i < ret->nbAtoms;i++) {
407 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
408 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
409 value = ret->atoms[i]->valuep;
410 for (j = 0;j < nbatoms;j++) {
411 if (xmlStrEqual(stringMap[j], value)) {
412 stringRemap[i] = j;
413 break;
414 }
415 }
416 if (j >= nbatoms) {
417 stringRemap[i] = nbatoms;
418 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000419 if (stringMap[nbatoms] == NULL) {
420 for (i = 0;i < nbatoms;i++)
421 xmlFree(stringMap[i]);
422 xmlFree(stringRemap);
423 xmlFree(stringMap);
424 xmlFree(stateRemap);
425 xmlFree(ret);
426 return(NULL);
427 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000428 nbatoms++;
429 }
430 } else {
431 xmlFree(stateRemap);
432 xmlFree(stringRemap);
433 for (i = 0;i < nbatoms;i++)
434 xmlFree(stringMap[i]);
435 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000436 xmlFree(ret);
437 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000438 }
439 }
440#ifdef DEBUG_COMPACTION
441 printf("Final: %d atoms\n", nbatoms);
442#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000443 transitions = (int *) xmlMalloc((nbstates + 1) *
444 (nbatoms + 1) * sizeof(int));
445 if (transitions == NULL) {
446 xmlFree(stateRemap);
447 xmlFree(stringRemap);
448 xmlFree(stringMap);
449 xmlFree(ret);
450 return(NULL);
451 }
452 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000453
454 /*
455 * Allocate the transition table. The first entry for each
456 * state correspond to the state type.
457 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000458 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000459
460 for (i = 0;i < ret->nbStates;i++) {
461 int stateno, atomno, targetno, prev;
462 xmlRegStatePtr state;
463 xmlRegTransPtr trans;
464
465 stateno = stateRemap[i];
466 if (stateno == -1)
467 continue;
468 state = ret->states[i];
469
470 transitions[stateno * (nbatoms + 1)] = state->type;
471
472 for (j = 0;j < state->nbTrans;j++) {
473 trans = &(state->trans[j]);
474 if ((trans->to == -1) || (trans->atom == NULL))
475 continue;
476 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000477 if ((trans->atom->data != NULL) && (transdata == NULL)) {
478 transdata = (void **) xmlMalloc(nbstates * nbatoms *
479 sizeof(void *));
480 if (transdata != NULL)
481 memset(transdata, 0,
482 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000483 else {
484 xmlGenericError(xmlGenericErrorContext,
485 "out of memory compiling regexp\n");
486 break;
487 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000488 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000489 targetno = stateRemap[trans->to];
490 /*
491 * if the same atome can generate transition to 2 different
492 * states then it means the automata is not determinist and
493 * the compact form can't be used !
494 */
495 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
496 if (prev != 0) {
497 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000498 ret->determinist = 0;
499#ifdef DEBUG_COMPACTION
500 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
501 i, j, trans->atom->no, trans->to, atomno, targetno);
502 printf(" previous to is %d\n", prev);
503#endif
504 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000505 if (transdata != NULL)
506 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000507 xmlFree(transitions);
508 xmlFree(stateRemap);
509 xmlFree(stringRemap);
510 for (i = 0;i < nbatoms;i++)
511 xmlFree(stringMap[i]);
512 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000513 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000514 }
515 } else {
516#if 0
517 printf("State %d trans %d: atom %d to %d : %d to %d\n",
518 i, j, trans->atom->no, trans->to, atomno, targetno);
519#endif
520 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000521 targetno + 1; /* to avoid 0 */
522 if (transdata != NULL)
523 transdata[stateno * nbatoms + atomno] =
524 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000525 }
526 }
527 }
528 ret->determinist = 1;
529#ifdef DEBUG_COMPACTION
530 /*
531 * Debug
532 */
533 for (i = 0;i < nbstates;i++) {
534 for (j = 0;j < nbatoms + 1;j++) {
535 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
536 }
537 printf("\n");
538 }
539 printf("\n");
540#endif
541 /*
542 * Cleanup of the old data
543 */
544 if (ret->states != NULL) {
545 for (i = 0;i < ret->nbStates;i++)
546 xmlRegFreeState(ret->states[i]);
547 xmlFree(ret->states);
548 }
549 ret->states = NULL;
550 ret->nbStates = 0;
551 if (ret->atoms != NULL) {
552 for (i = 0;i < ret->nbAtoms;i++)
553 xmlRegFreeAtom(ret->atoms[i]);
554 xmlFree(ret->atoms);
555 }
556 ret->atoms = NULL;
557 ret->nbAtoms = 0;
558
559 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000560 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000561 ret->stringMap = stringMap;
562 ret->nbstrings = nbatoms;
563 ret->nbstates = nbstates;
564 xmlFree(stateRemap);
565 xmlFree(stringRemap);
566 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000567not_determ:
568 ctxt->string = NULL;
569 ctxt->nbStates = 0;
570 ctxt->states = NULL;
571 ctxt->nbAtoms = 0;
572 ctxt->atoms = NULL;
573 ctxt->nbCounters = 0;
574 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000575 return(ret);
576}
577
578/**
579 * xmlRegNewParserCtxt:
580 * @string: the string to parse
581 *
582 * Allocate a new regexp parser context
583 *
584 * Returns the new context or NULL in case of error
585 */
586static xmlRegParserCtxtPtr
587xmlRegNewParserCtxt(const xmlChar *string) {
588 xmlRegParserCtxtPtr ret;
589
590 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
591 if (ret == NULL)
592 return(NULL);
593 memset(ret, 0, sizeof(xmlRegParserCtxt));
594 if (string != NULL)
595 ret->string = xmlStrdup(string);
596 ret->cur = ret->string;
597 ret->neg = 0;
598 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000599 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000600 return(ret);
601}
602
603/**
604 * xmlRegNewRange:
605 * @ctxt: the regexp parser context
606 * @neg: is that negative
607 * @type: the type of range
608 * @start: the start codepoint
609 * @end: the end codepoint
610 *
611 * Allocate a new regexp range
612 *
613 * Returns the new range or NULL in case of error
614 */
615static xmlRegRangePtr
616xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
617 int neg, xmlRegAtomType type, int start, int end) {
618 xmlRegRangePtr ret;
619
620 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
621 if (ret == NULL) {
622 ERROR("failed to allocate regexp range");
623 return(NULL);
624 }
625 ret->neg = neg;
626 ret->type = type;
627 ret->start = start;
628 ret->end = end;
629 return(ret);
630}
631
632/**
633 * xmlRegFreeRange:
634 * @range: the regexp range
635 *
636 * Free a regexp range
637 */
638static void
639xmlRegFreeRange(xmlRegRangePtr range) {
640 if (range == NULL)
641 return;
642
643 if (range->blockName != NULL)
644 xmlFree(range->blockName);
645 xmlFree(range);
646}
647
648/**
649 * xmlRegNewAtom:
650 * @ctxt: the regexp parser context
651 * @type: the type of atom
652 *
653 * Allocate a new regexp range
654 *
655 * Returns the new atom or NULL in case of error
656 */
657static xmlRegAtomPtr
658xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
659 xmlRegAtomPtr ret;
660
661 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
662 if (ret == NULL) {
663 ERROR("failed to allocate regexp atom");
664 return(NULL);
665 }
666 memset(ret, 0, sizeof(xmlRegAtom));
667 ret->type = type;
668 ret->quant = XML_REGEXP_QUANT_ONCE;
669 ret->min = 0;
670 ret->max = 0;
671 return(ret);
672}
673
674/**
675 * xmlRegFreeAtom:
676 * @atom: the regexp atom
677 *
678 * Free a regexp atom
679 */
680static void
681xmlRegFreeAtom(xmlRegAtomPtr atom) {
682 int i;
683
684 if (atom == NULL)
685 return;
686
687 for (i = 0;i < atom->nbRanges;i++)
688 xmlRegFreeRange(atom->ranges[i]);
689 if (atom->ranges != NULL)
690 xmlFree(atom->ranges);
691 if (atom->type == XML_REGEXP_STRING)
692 xmlFree(atom->valuep);
693 xmlFree(atom);
694}
695
696static xmlRegStatePtr
697xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
698 xmlRegStatePtr ret;
699
700 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
701 if (ret == NULL) {
702 ERROR("failed to allocate regexp state");
703 return(NULL);
704 }
705 memset(ret, 0, sizeof(xmlRegState));
706 ret->type = XML_REGEXP_TRANS_STATE;
707 ret->mark = XML_REGEXP_MARK_NORMAL;
708 return(ret);
709}
710
711/**
712 * xmlRegFreeState:
713 * @state: the regexp state
714 *
715 * Free a regexp state
716 */
717static void
718xmlRegFreeState(xmlRegStatePtr state) {
719 if (state == NULL)
720 return;
721
722 if (state->trans != NULL)
723 xmlFree(state->trans);
724 xmlFree(state);
725}
726
727/**
728 * xmlRegFreeParserCtxt:
729 * @ctxt: the regexp parser context
730 *
731 * Free a regexp parser context
732 */
733static void
734xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
735 int i;
736 if (ctxt == NULL)
737 return;
738
739 if (ctxt->string != NULL)
740 xmlFree(ctxt->string);
741 if (ctxt->states != NULL) {
742 for (i = 0;i < ctxt->nbStates;i++)
743 xmlRegFreeState(ctxt->states[i]);
744 xmlFree(ctxt->states);
745 }
746 if (ctxt->atoms != NULL) {
747 for (i = 0;i < ctxt->nbAtoms;i++)
748 xmlRegFreeAtom(ctxt->atoms[i]);
749 xmlFree(ctxt->atoms);
750 }
751 if (ctxt->counters != NULL)
752 xmlFree(ctxt->counters);
753 xmlFree(ctxt);
754}
755
756/************************************************************************
757 * *
758 * Display of Data structures *
759 * *
760 ************************************************************************/
761
762static void
763xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
764 switch (type) {
765 case XML_REGEXP_EPSILON:
766 fprintf(output, "epsilon "); break;
767 case XML_REGEXP_CHARVAL:
768 fprintf(output, "charval "); break;
769 case XML_REGEXP_RANGES:
770 fprintf(output, "ranges "); break;
771 case XML_REGEXP_SUBREG:
772 fprintf(output, "subexpr "); break;
773 case XML_REGEXP_STRING:
774 fprintf(output, "string "); break;
775 case XML_REGEXP_ANYCHAR:
776 fprintf(output, "anychar "); break;
777 case XML_REGEXP_ANYSPACE:
778 fprintf(output, "anyspace "); break;
779 case XML_REGEXP_NOTSPACE:
780 fprintf(output, "notspace "); break;
781 case XML_REGEXP_INITNAME:
782 fprintf(output, "initname "); break;
783 case XML_REGEXP_NOTINITNAME:
784 fprintf(output, "notinitname "); break;
785 case XML_REGEXP_NAMECHAR:
786 fprintf(output, "namechar "); break;
787 case XML_REGEXP_NOTNAMECHAR:
788 fprintf(output, "notnamechar "); break;
789 case XML_REGEXP_DECIMAL:
790 fprintf(output, "decimal "); break;
791 case XML_REGEXP_NOTDECIMAL:
792 fprintf(output, "notdecimal "); break;
793 case XML_REGEXP_REALCHAR:
794 fprintf(output, "realchar "); break;
795 case XML_REGEXP_NOTREALCHAR:
796 fprintf(output, "notrealchar "); break;
797 case XML_REGEXP_LETTER:
798 fprintf(output, "LETTER "); break;
799 case XML_REGEXP_LETTER_UPPERCASE:
800 fprintf(output, "LETTER_UPPERCASE "); break;
801 case XML_REGEXP_LETTER_LOWERCASE:
802 fprintf(output, "LETTER_LOWERCASE "); break;
803 case XML_REGEXP_LETTER_TITLECASE:
804 fprintf(output, "LETTER_TITLECASE "); break;
805 case XML_REGEXP_LETTER_MODIFIER:
806 fprintf(output, "LETTER_MODIFIER "); break;
807 case XML_REGEXP_LETTER_OTHERS:
808 fprintf(output, "LETTER_OTHERS "); break;
809 case XML_REGEXP_MARK:
810 fprintf(output, "MARK "); break;
811 case XML_REGEXP_MARK_NONSPACING:
812 fprintf(output, "MARK_NONSPACING "); break;
813 case XML_REGEXP_MARK_SPACECOMBINING:
814 fprintf(output, "MARK_SPACECOMBINING "); break;
815 case XML_REGEXP_MARK_ENCLOSING:
816 fprintf(output, "MARK_ENCLOSING "); break;
817 case XML_REGEXP_NUMBER:
818 fprintf(output, "NUMBER "); break;
819 case XML_REGEXP_NUMBER_DECIMAL:
820 fprintf(output, "NUMBER_DECIMAL "); break;
821 case XML_REGEXP_NUMBER_LETTER:
822 fprintf(output, "NUMBER_LETTER "); break;
823 case XML_REGEXP_NUMBER_OTHERS:
824 fprintf(output, "NUMBER_OTHERS "); break;
825 case XML_REGEXP_PUNCT:
826 fprintf(output, "PUNCT "); break;
827 case XML_REGEXP_PUNCT_CONNECTOR:
828 fprintf(output, "PUNCT_CONNECTOR "); break;
829 case XML_REGEXP_PUNCT_DASH:
830 fprintf(output, "PUNCT_DASH "); break;
831 case XML_REGEXP_PUNCT_OPEN:
832 fprintf(output, "PUNCT_OPEN "); break;
833 case XML_REGEXP_PUNCT_CLOSE:
834 fprintf(output, "PUNCT_CLOSE "); break;
835 case XML_REGEXP_PUNCT_INITQUOTE:
836 fprintf(output, "PUNCT_INITQUOTE "); break;
837 case XML_REGEXP_PUNCT_FINQUOTE:
838 fprintf(output, "PUNCT_FINQUOTE "); break;
839 case XML_REGEXP_PUNCT_OTHERS:
840 fprintf(output, "PUNCT_OTHERS "); break;
841 case XML_REGEXP_SEPAR:
842 fprintf(output, "SEPAR "); break;
843 case XML_REGEXP_SEPAR_SPACE:
844 fprintf(output, "SEPAR_SPACE "); break;
845 case XML_REGEXP_SEPAR_LINE:
846 fprintf(output, "SEPAR_LINE "); break;
847 case XML_REGEXP_SEPAR_PARA:
848 fprintf(output, "SEPAR_PARA "); break;
849 case XML_REGEXP_SYMBOL:
850 fprintf(output, "SYMBOL "); break;
851 case XML_REGEXP_SYMBOL_MATH:
852 fprintf(output, "SYMBOL_MATH "); break;
853 case XML_REGEXP_SYMBOL_CURRENCY:
854 fprintf(output, "SYMBOL_CURRENCY "); break;
855 case XML_REGEXP_SYMBOL_MODIFIER:
856 fprintf(output, "SYMBOL_MODIFIER "); break;
857 case XML_REGEXP_SYMBOL_OTHERS:
858 fprintf(output, "SYMBOL_OTHERS "); break;
859 case XML_REGEXP_OTHER:
860 fprintf(output, "OTHER "); break;
861 case XML_REGEXP_OTHER_CONTROL:
862 fprintf(output, "OTHER_CONTROL "); break;
863 case XML_REGEXP_OTHER_FORMAT:
864 fprintf(output, "OTHER_FORMAT "); break;
865 case XML_REGEXP_OTHER_PRIVATE:
866 fprintf(output, "OTHER_PRIVATE "); break;
867 case XML_REGEXP_OTHER_NA:
868 fprintf(output, "OTHER_NA "); break;
869 case XML_REGEXP_BLOCK_NAME:
870 fprintf(output, "BLOCK "); break;
871 }
872}
873
874static void
875xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
876 switch (type) {
877 case XML_REGEXP_QUANT_EPSILON:
878 fprintf(output, "epsilon "); break;
879 case XML_REGEXP_QUANT_ONCE:
880 fprintf(output, "once "); break;
881 case XML_REGEXP_QUANT_OPT:
882 fprintf(output, "? "); break;
883 case XML_REGEXP_QUANT_MULT:
884 fprintf(output, "* "); break;
885 case XML_REGEXP_QUANT_PLUS:
886 fprintf(output, "+ "); break;
887 case XML_REGEXP_QUANT_RANGE:
888 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000889 case XML_REGEXP_QUANT_ONCEONLY:
890 fprintf(output, "onceonly "); break;
891 case XML_REGEXP_QUANT_ALL:
892 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000893 }
894}
895static void
896xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
897 fprintf(output, " range: ");
898 if (range->neg)
899 fprintf(output, "negative ");
900 xmlRegPrintAtomType(output, range->type);
901 fprintf(output, "%c - %c\n", range->start, range->end);
902}
903
904static void
905xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
906 fprintf(output, " atom: ");
907 if (atom == NULL) {
908 fprintf(output, "NULL\n");
909 return;
910 }
911 xmlRegPrintAtomType(output, atom->type);
912 xmlRegPrintQuantType(output, atom->quant);
913 if (atom->quant == XML_REGEXP_QUANT_RANGE)
914 fprintf(output, "%d-%d ", atom->min, atom->max);
915 if (atom->type == XML_REGEXP_STRING)
916 fprintf(output, "'%s' ", (char *) atom->valuep);
917 if (atom->type == XML_REGEXP_CHARVAL)
918 fprintf(output, "char %c\n", atom->codepoint);
919 else if (atom->type == XML_REGEXP_RANGES) {
920 int i;
921 fprintf(output, "%d entries\n", atom->nbRanges);
922 for (i = 0; i < atom->nbRanges;i++)
923 xmlRegPrintRange(output, atom->ranges[i]);
924 } else if (atom->type == XML_REGEXP_SUBREG) {
925 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
926 } else {
927 fprintf(output, "\n");
928 }
929}
930
931static void
932xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
933 fprintf(output, " trans: ");
934 if (trans == NULL) {
935 fprintf(output, "NULL\n");
936 return;
937 }
938 if (trans->to < 0) {
939 fprintf(output, "removed\n");
940 return;
941 }
942 if (trans->counter >= 0) {
943 fprintf(output, "counted %d, ", trans->counter);
944 }
Daniel Veillard8a001f62002-04-20 07:24:11 +0000945 if (trans->count == REGEXP_ALL_COUNTER) {
946 fprintf(output, "all transition, ");
947 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000948 fprintf(output, "count based %d, ", trans->count);
949 }
950 if (trans->atom == NULL) {
951 fprintf(output, "epsilon to %d\n", trans->to);
952 return;
953 }
954 if (trans->atom->type == XML_REGEXP_CHARVAL)
955 fprintf(output, "char %c ", trans->atom->codepoint);
956 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
957}
958
959static void
960xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
961 int i;
962
963 fprintf(output, " state: ");
964 if (state == NULL) {
965 fprintf(output, "NULL\n");
966 return;
967 }
968 if (state->type == XML_REGEXP_START_STATE)
969 fprintf(output, "START ");
970 if (state->type == XML_REGEXP_FINAL_STATE)
971 fprintf(output, "FINAL ");
972
973 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
974 for (i = 0;i < state->nbTrans; i++) {
975 xmlRegPrintTrans(output, &(state->trans[i]));
976 }
977}
978
Daniel Veillard23e73572002-09-19 19:56:43 +0000979#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +0000980static void
981xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
982 int i;
983
984 fprintf(output, " ctxt: ");
985 if (ctxt == NULL) {
986 fprintf(output, "NULL\n");
987 return;
988 }
989 fprintf(output, "'%s' ", ctxt->string);
990 if (ctxt->error)
991 fprintf(output, "error ");
992 if (ctxt->neg)
993 fprintf(output, "neg ");
994 fprintf(output, "\n");
995 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
996 for (i = 0;i < ctxt->nbAtoms; i++) {
997 fprintf(output, " %02d ", i);
998 xmlRegPrintAtom(output, ctxt->atoms[i]);
999 }
1000 if (ctxt->atom != NULL) {
1001 fprintf(output, "current atom:\n");
1002 xmlRegPrintAtom(output, ctxt->atom);
1003 }
1004 fprintf(output, "%d states:", ctxt->nbStates);
1005 if (ctxt->start != NULL)
1006 fprintf(output, " start: %d", ctxt->start->no);
1007 if (ctxt->end != NULL)
1008 fprintf(output, " end: %d", ctxt->end->no);
1009 fprintf(output, "\n");
1010 for (i = 0;i < ctxt->nbStates; i++) {
1011 xmlRegPrintState(output, ctxt->states[i]);
1012 }
1013 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1014 for (i = 0;i < ctxt->nbCounters; i++) {
1015 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1016 ctxt->counters[i].max);
1017 }
1018}
Daniel Veillard23e73572002-09-19 19:56:43 +00001019#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001020
1021/************************************************************************
1022 * *
1023 * Finite Automata structures manipulations *
1024 * *
1025 ************************************************************************/
1026
1027static void
1028xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1029 int neg, xmlRegAtomType type, int start, int end,
1030 xmlChar *blockName) {
1031 xmlRegRangePtr range;
1032
1033 if (atom == NULL) {
1034 ERROR("add range: atom is NULL");
1035 return;
1036 }
1037 if (atom->type != XML_REGEXP_RANGES) {
1038 ERROR("add range: atom is not ranges");
1039 return;
1040 }
1041 if (atom->maxRanges == 0) {
1042 atom->maxRanges = 4;
1043 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1044 sizeof(xmlRegRangePtr));
1045 if (atom->ranges == NULL) {
1046 ERROR("add range: allocation failed");
1047 atom->maxRanges = 0;
1048 return;
1049 }
1050 } else if (atom->nbRanges >= atom->maxRanges) {
1051 xmlRegRangePtr *tmp;
1052 atom->maxRanges *= 2;
1053 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1054 sizeof(xmlRegRangePtr));
1055 if (tmp == NULL) {
1056 ERROR("add range: allocation failed");
1057 atom->maxRanges /= 2;
1058 return;
1059 }
1060 atom->ranges = tmp;
1061 }
1062 range = xmlRegNewRange(ctxt, neg, type, start, end);
1063 if (range == NULL)
1064 return;
1065 range->blockName = blockName;
1066 atom->ranges[atom->nbRanges++] = range;
1067
1068}
1069
1070static int
1071xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1072 if (ctxt->maxCounters == 0) {
1073 ctxt->maxCounters = 4;
1074 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1075 sizeof(xmlRegCounter));
1076 if (ctxt->counters == NULL) {
1077 ERROR("reg counter: allocation failed");
1078 ctxt->maxCounters = 0;
1079 return(-1);
1080 }
1081 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1082 xmlRegCounter *tmp;
1083 ctxt->maxCounters *= 2;
1084 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1085 sizeof(xmlRegCounter));
1086 if (tmp == NULL) {
1087 ERROR("reg counter: allocation failed");
1088 ctxt->maxCounters /= 2;
1089 return(-1);
1090 }
1091 ctxt->counters = tmp;
1092 }
1093 ctxt->counters[ctxt->nbCounters].min = -1;
1094 ctxt->counters[ctxt->nbCounters].max = -1;
1095 return(ctxt->nbCounters++);
1096}
1097
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001098static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001099xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1100 if (atom == NULL) {
1101 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001102 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001103 }
1104 if (ctxt->maxAtoms == 0) {
1105 ctxt->maxAtoms = 4;
1106 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1107 sizeof(xmlRegAtomPtr));
1108 if (ctxt->atoms == NULL) {
1109 ERROR("atom push: allocation failed");
1110 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001111 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001112 }
1113 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1114 xmlRegAtomPtr *tmp;
1115 ctxt->maxAtoms *= 2;
1116 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1117 sizeof(xmlRegAtomPtr));
1118 if (tmp == NULL) {
1119 ERROR("atom push: allocation failed");
1120 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001121 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001122 }
1123 ctxt->atoms = tmp;
1124 }
1125 atom->no = ctxt->nbAtoms;
1126 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001127 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001128}
1129
1130static void
1131xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1132 xmlRegAtomPtr atom, xmlRegStatePtr target,
1133 int counter, int count) {
1134 if (state == NULL) {
1135 ERROR("add state: state is NULL");
1136 return;
1137 }
1138 if (target == NULL) {
1139 ERROR("add state: target is NULL");
1140 return;
1141 }
1142 if (state->maxTrans == 0) {
1143 state->maxTrans = 4;
1144 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1145 sizeof(xmlRegTrans));
1146 if (state->trans == NULL) {
1147 ERROR("add range: allocation failed");
1148 state->maxTrans = 0;
1149 return;
1150 }
1151 } else if (state->nbTrans >= state->maxTrans) {
1152 xmlRegTrans *tmp;
1153 state->maxTrans *= 2;
1154 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1155 sizeof(xmlRegTrans));
1156 if (tmp == NULL) {
1157 ERROR("add range: allocation failed");
1158 state->maxTrans /= 2;
1159 return;
1160 }
1161 state->trans = tmp;
1162 }
1163#ifdef DEBUG_REGEXP_GRAPH
1164 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001165 if (count == REGEXP_ALL_COUNTER)
1166 printf("all transition");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001167 else if (count >= 0)
Daniel Veillard4255d502002-04-16 15:50:10 +00001168 printf("count based %d", count);
1169 else if (counter >= 0)
1170 printf("counted %d", counter);
1171 else if (atom == NULL)
1172 printf("epsilon transition");
1173 printf("\n");
1174#endif
1175
1176 state->trans[state->nbTrans].atom = atom;
1177 state->trans[state->nbTrans].to = target->no;
1178 state->trans[state->nbTrans].counter = counter;
1179 state->trans[state->nbTrans].count = count;
1180 state->nbTrans++;
1181}
1182
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001183static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001184xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001185 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001186 if (ctxt->maxStates == 0) {
1187 ctxt->maxStates = 4;
1188 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1189 sizeof(xmlRegStatePtr));
1190 if (ctxt->states == NULL) {
1191 ERROR("add range: allocation failed");
1192 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001193 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001194 }
1195 } else if (ctxt->nbStates >= ctxt->maxStates) {
1196 xmlRegStatePtr *tmp;
1197 ctxt->maxStates *= 2;
1198 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1199 sizeof(xmlRegStatePtr));
1200 if (tmp == NULL) {
1201 ERROR("add range: allocation failed");
1202 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001203 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001204 }
1205 ctxt->states = tmp;
1206 }
1207 state->no = ctxt->nbStates;
1208 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001209 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001210}
1211
1212/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001213 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001214 * @ctxt: a regexp parser context
1215 * @from: the from state
1216 * @to: the target state or NULL for building a new one
1217 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001218 *
1219 */
1220static void
1221xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001222 xmlRegStatePtr from, xmlRegStatePtr to,
1223 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001224 if (to == NULL) {
1225 to = xmlRegNewState(ctxt);
1226 xmlRegStatePush(ctxt, to);
1227 ctxt->state = to;
1228 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001229 if (lax)
1230 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1231 else
1232 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001233}
1234
1235/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001236 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001237 * @ctxt: a regexp parser context
1238 * @from: the from state
1239 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001240 *
1241 */
1242static void
1243xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1244 xmlRegStatePtr from, xmlRegStatePtr to) {
1245 if (to == NULL) {
1246 to = xmlRegNewState(ctxt);
1247 xmlRegStatePush(ctxt, to);
1248 ctxt->state = to;
1249 }
1250 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1251}
1252
1253/**
1254 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001255 * @ctxt: a regexp parser context
1256 * @from: the from state
1257 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001258 * counter: the counter for that transition
1259 *
1260 */
1261static void
1262xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1263 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1264 if (to == NULL) {
1265 to = xmlRegNewState(ctxt);
1266 xmlRegStatePush(ctxt, to);
1267 ctxt->state = to;
1268 }
1269 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1270}
1271
1272/**
1273 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001274 * @ctxt: a regexp parser context
1275 * @from: the from state
1276 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001277 * counter: the counter for that transition
1278 *
1279 */
1280static void
1281xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1282 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1283 if (to == NULL) {
1284 to = xmlRegNewState(ctxt);
1285 xmlRegStatePush(ctxt, to);
1286 ctxt->state = to;
1287 }
1288 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1289}
1290
1291/**
1292 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001293 * @ctxt: a regexp parser context
1294 * @from: the from state
1295 * @to: the target state or NULL for building a new one
1296 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001297 *
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001298 * Returns 0 if succes and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001299 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001300static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001301xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1302 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1303 if (atom == NULL) {
1304 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001305 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001306 }
1307 if (atom->type == XML_REGEXP_SUBREG) {
1308 /*
1309 * this is a subexpression handling one should not need to
1310 * create a new node excep for XML_REGEXP_QUANT_RANGE.
1311 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001312 if (xmlRegAtomPush(ctxt, atom) < 0) {
1313 return(-1);
1314 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001315 if ((to != NULL) && (atom->stop != to) &&
1316 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1317 /*
1318 * Generate an epsilon transition to link to the target
1319 */
1320 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1321 }
1322 switch (atom->quant) {
1323 case XML_REGEXP_QUANT_OPT:
1324 atom->quant = XML_REGEXP_QUANT_ONCE;
1325 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1326 break;
1327 case XML_REGEXP_QUANT_MULT:
1328 atom->quant = XML_REGEXP_QUANT_ONCE;
1329 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1330 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1331 break;
1332 case XML_REGEXP_QUANT_PLUS:
1333 atom->quant = XML_REGEXP_QUANT_ONCE;
1334 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1335 break;
1336 case XML_REGEXP_QUANT_RANGE: {
1337 int counter;
1338 xmlRegStatePtr newstate;
1339
1340 /*
1341 * This one is nasty:
1342 * 1/ register a new counter
1343 * 2/ register an epsilon transition associated to
1344 * this counter going from atom->stop to atom->start
1345 * 3/ create a new state
1346 * 4/ generate a counted transition from atom->stop to
1347 * that state
1348 */
1349 counter = xmlRegGetCounter(ctxt);
1350 ctxt->counters[counter].min = atom->min - 1;
1351 ctxt->counters[counter].max = atom->max - 1;
1352 atom->min = 0;
1353 atom->max = 0;
1354 atom->quant = XML_REGEXP_QUANT_ONCE;
1355 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1356 atom->start, counter);
1357 if (to != NULL) {
1358 newstate = to;
1359 } else {
1360 newstate = xmlRegNewState(ctxt);
1361 xmlRegStatePush(ctxt, newstate);
1362 ctxt->state = newstate;
1363 }
1364 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1365 newstate, counter);
1366 }
1367 default:
1368 break;
1369 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001370 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001371 } else {
1372 if (to == NULL) {
1373 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001374 if (to != NULL)
1375 xmlRegStatePush(ctxt, to);
1376 else {
1377 return(-1);
1378 }
1379 }
1380 if (xmlRegAtomPush(ctxt, atom) < 0) {
1381 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001382 }
1383 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001384 ctxt->state = to;
1385 }
1386 switch (atom->quant) {
1387 case XML_REGEXP_QUANT_OPT:
1388 atom->quant = XML_REGEXP_QUANT_ONCE;
1389 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1390 break;
1391 case XML_REGEXP_QUANT_MULT:
1392 atom->quant = XML_REGEXP_QUANT_ONCE;
1393 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1394 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1395 break;
1396 case XML_REGEXP_QUANT_PLUS:
1397 atom->quant = XML_REGEXP_QUANT_ONCE;
1398 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1399 break;
1400 default:
1401 break;
1402 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001403 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001404}
1405
1406/**
1407 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001408 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001409 * @fromnr: the from state
1410 * @tonr: the to state
1411 * @cpunter: should that transition be associted to a counted
1412 *
1413 */
1414static void
1415xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1416 int tonr, int counter) {
1417 int transnr;
1418 xmlRegStatePtr from;
1419 xmlRegStatePtr to;
1420
1421#ifdef DEBUG_REGEXP_GRAPH
1422 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1423#endif
1424 from = ctxt->states[fromnr];
1425 if (from == NULL)
1426 return;
1427 to = ctxt->states[tonr];
1428 if (to == NULL)
1429 return;
1430 if ((to->mark == XML_REGEXP_MARK_START) ||
1431 (to->mark == XML_REGEXP_MARK_VISITED))
1432 return;
1433
1434 to->mark = XML_REGEXP_MARK_VISITED;
1435 if (to->type == XML_REGEXP_FINAL_STATE) {
1436#ifdef DEBUG_REGEXP_GRAPH
1437 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1438#endif
1439 from->type = XML_REGEXP_FINAL_STATE;
1440 }
1441 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1442 if (to->trans[transnr].atom == NULL) {
1443 /*
1444 * Don't remove counted transitions
1445 * Don't loop either
1446 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001447 if (to->trans[transnr].to != fromnr) {
1448 if (to->trans[transnr].count >= 0) {
1449 int newto = to->trans[transnr].to;
1450
1451 xmlRegStateAddTrans(ctxt, from, NULL,
1452 ctxt->states[newto],
1453 -1, to->trans[transnr].count);
1454 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001455#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001456 printf("Found epsilon trans %d from %d to %d\n",
1457 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001458#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001459 if (to->trans[transnr].counter >= 0) {
1460 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1461 to->trans[transnr].to,
1462 to->trans[transnr].counter);
1463 } else {
1464 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1465 to->trans[transnr].to,
1466 counter);
1467 }
1468 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001469 }
1470 } else {
1471 int newto = to->trans[transnr].to;
1472
Daniel Veillardb509f152002-04-17 16:28:10 +00001473 if (to->trans[transnr].counter >= 0) {
1474 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1475 ctxt->states[newto],
1476 to->trans[transnr].counter, -1);
1477 } else {
1478 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1479 ctxt->states[newto], counter, -1);
1480 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001481 }
1482 }
1483 to->mark = XML_REGEXP_MARK_NORMAL;
1484}
1485
1486/**
1487 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001488 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001489 *
1490 */
1491static void
1492xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1493 int statenr, transnr;
1494 xmlRegStatePtr state;
1495
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001496 if (ctxt->states == NULL) return;
1497
1498
Daniel Veillard4255d502002-04-16 15:50:10 +00001499 /*
1500 * build the completed transitions bypassing the epsilons
1501 * Use a marking algorithm to avoid loops
1502 */
1503 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1504 state = ctxt->states[statenr];
1505 if (state == NULL)
1506 continue;
1507 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1508 if ((state->trans[transnr].atom == NULL) &&
1509 (state->trans[transnr].to >= 0)) {
1510 if (state->trans[transnr].to == statenr) {
1511 state->trans[transnr].to = -1;
1512#ifdef DEBUG_REGEXP_GRAPH
1513 printf("Removed loopback epsilon trans %d on %d\n",
1514 transnr, statenr);
1515#endif
1516 } else if (state->trans[transnr].count < 0) {
1517 int newto = state->trans[transnr].to;
1518
1519#ifdef DEBUG_REGEXP_GRAPH
1520 printf("Found epsilon trans %d from %d to %d\n",
1521 transnr, statenr, newto);
1522#endif
1523 state->mark = XML_REGEXP_MARK_START;
1524 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1525 newto, state->trans[transnr].counter);
1526 state->mark = XML_REGEXP_MARK_NORMAL;
1527#ifdef DEBUG_REGEXP_GRAPH
1528 } else {
1529 printf("Found counted transition %d on %d\n",
1530 transnr, statenr);
1531#endif
1532 }
1533 }
1534 }
1535 }
1536 /*
1537 * Eliminate the epsilon transitions
1538 */
1539 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1540 state = ctxt->states[statenr];
1541 if (state == NULL)
1542 continue;
1543 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1544 if ((state->trans[transnr].atom == NULL) &&
1545 (state->trans[transnr].count < 0) &&
1546 (state->trans[transnr].to >= 0)) {
1547 state->trans[transnr].to = -1;
1548 }
1549 }
1550 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001551
1552 /*
1553 * Use this pass to detect unreachable states too
1554 */
1555 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1556 state = ctxt->states[statenr];
1557 if (state != NULL)
1558 state->reached = 0;
1559 }
1560 state = ctxt->states[0];
1561 if (state != NULL)
1562 state->reached = 1;
1563 while (state != NULL) {
1564 xmlRegStatePtr target = NULL;
1565 state->reached = 2;
1566 /*
1567 * Mark all state reachable from the current reachable state
1568 */
1569 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1570 if ((state->trans[transnr].to >= 0) &&
1571 ((state->trans[transnr].atom != NULL) ||
1572 (state->trans[transnr].count >= 0))) {
1573 int newto = state->trans[transnr].to;
1574
1575 if (ctxt->states[newto] == NULL)
1576 continue;
1577 if (ctxt->states[newto]->reached == 0) {
1578 ctxt->states[newto]->reached = 1;
1579 target = ctxt->states[newto];
1580 }
1581 }
1582 }
1583 /*
1584 * find the next accessible state not explored
1585 */
1586 if (target == NULL) {
1587 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1588 state = ctxt->states[statenr];
1589 if ((state != NULL) && (state->reached == 1)) {
1590 target = state;
1591 break;
1592 }
1593 }
1594 }
1595 state = target;
1596 }
1597 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1598 state = ctxt->states[statenr];
1599 if ((state != NULL) && (state->reached == 0)) {
1600#ifdef DEBUG_REGEXP_GRAPH
1601 printf("Removed unreachable state %d\n", statenr);
1602#endif
1603 xmlRegFreeState(state);
1604 ctxt->states[statenr] = NULL;
1605 }
1606 }
1607
Daniel Veillard4255d502002-04-16 15:50:10 +00001608}
1609
Daniel Veillarde19fc232002-04-22 16:01:24 +00001610/**
1611 * xmlFACompareAtoms:
1612 * @atom1: an atom
1613 * @atom2: an atom
1614 *
1615 * Compares two atoms to check whether they are equivatents
1616 *
1617 * Returns 1 if yes and 0 otherwise
1618 */
1619static int
1620xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1621 if (atom1 == atom2)
1622 return(1);
1623 if ((atom1 == NULL) || (atom2 == NULL))
1624 return(0);
1625
1626 if (atom1->type != atom2->type)
1627 return(0);
1628 switch (atom1->type) {
1629 case XML_REGEXP_STRING:
1630 return(xmlStrEqual((xmlChar *)atom1->valuep,
1631 (xmlChar *)atom2->valuep));
1632 case XML_REGEXP_EPSILON:
1633 return(1);
1634 case XML_REGEXP_CHARVAL:
1635 return(atom1->codepoint == atom2->codepoint);
1636 case XML_REGEXP_RANGES:
1637 TODO;
1638 return(0);
1639 default:
1640 break;
1641 }
1642 return(1);
1643}
1644
1645/**
1646 * xmlFARecurseDeterminism:
1647 * @ctxt: a regexp parser context
1648 *
1649 * Check whether the associated regexp is determinist,
1650 * should be called after xmlFAEliminateEpsilonTransitions()
1651 *
1652 */
1653static int
1654xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1655 int to, xmlRegAtomPtr atom) {
1656 int ret = 1;
1657 int transnr;
1658 xmlRegTransPtr t1;
1659
1660 if (state == NULL)
1661 return(ret);
1662 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1663 t1 = &(state->trans[transnr]);
1664 /*
1665 * check transitions conflicting with the one looked at
1666 */
1667 if (t1->atom == NULL) {
1668 if (t1->to == -1)
1669 continue;
1670 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1671 to, atom);
1672 if (ret == 0)
1673 return(0);
1674 continue;
1675 }
1676 if (t1->to != to)
1677 continue;
1678 if (xmlFACompareAtoms(t1->atom, atom))
1679 return(0);
1680 }
1681 return(ret);
1682}
1683
1684/**
1685 * xmlFAComputesDeterminism:
1686 * @ctxt: a regexp parser context
1687 *
1688 * Check whether the associated regexp is determinist,
1689 * should be called after xmlFAEliminateEpsilonTransitions()
1690 *
1691 */
1692static int
1693xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1694 int statenr, transnr;
1695 xmlRegStatePtr state;
1696 xmlRegTransPtr t1, t2;
1697 int i;
1698 int ret = 1;
1699
Daniel Veillard4402ab42002-09-12 16:02:56 +00001700#ifdef DEBUG_REGEXP_GRAPH
1701 printf("xmlFAComputesDeterminism\n");
1702 xmlRegPrintCtxt(stdout, ctxt);
1703#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001704 if (ctxt->determinist != -1)
1705 return(ctxt->determinist);
1706
1707 /*
1708 * Check for all states that there isn't 2 transitions
1709 * with the same atom and a different target.
1710 */
1711 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1712 state = ctxt->states[statenr];
1713 if (state == NULL)
1714 continue;
1715 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1716 t1 = &(state->trans[transnr]);
1717 /*
1718 * Determinism checks in case of counted or all transitions
1719 * will have to be handled separately
1720 */
1721 if (t1->atom == NULL)
1722 continue;
1723 if (t1->to == -1) /* eliminated */
1724 continue;
1725 for (i = 0;i < transnr;i++) {
1726 t2 = &(state->trans[i]);
1727 if (t2->to == -1) /* eliminated */
1728 continue;
1729 if (t2->atom != NULL) {
1730 if (t1->to == t2->to) {
1731 if (xmlFACompareAtoms(t1->atom, t2->atom))
1732 t2->to = -1; /* eliminate */
1733 } else {
1734 /* not determinist ! */
1735 if (xmlFACompareAtoms(t1->atom, t2->atom))
1736 ret = 0;
1737 }
1738 } else if (t1->to != -1) {
1739 /*
1740 * do the closure in case of remaining specific
1741 * epsilon transitions like choices or all
1742 */
1743 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1744 t2->to, t2->atom);
1745 if (ret == 0)
1746 return(0);
1747 }
1748 }
1749 if (ret == 0)
1750 break;
1751 }
1752 if (ret == 0)
1753 break;
1754 }
1755 ctxt->determinist = ret;
1756 return(ret);
1757}
1758
Daniel Veillard4255d502002-04-16 15:50:10 +00001759/************************************************************************
1760 * *
1761 * Routines to check input against transition atoms *
1762 * *
1763 ************************************************************************/
1764
1765static int
1766xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1767 int start, int end, const xmlChar *blockName) {
1768 int ret = 0;
1769
1770 switch (type) {
1771 case XML_REGEXP_STRING:
1772 case XML_REGEXP_SUBREG:
1773 case XML_REGEXP_RANGES:
1774 case XML_REGEXP_EPSILON:
1775 return(-1);
1776 case XML_REGEXP_ANYCHAR:
1777 ret = ((codepoint != '\n') && (codepoint != '\r'));
1778 break;
1779 case XML_REGEXP_CHARVAL:
1780 ret = ((codepoint >= start) && (codepoint <= end));
1781 break;
1782 case XML_REGEXP_NOTSPACE:
1783 neg = !neg;
1784 case XML_REGEXP_ANYSPACE:
1785 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1786 (codepoint == '\t') || (codepoint == ' '));
1787 break;
1788 case XML_REGEXP_NOTINITNAME:
1789 neg = !neg;
1790 case XML_REGEXP_INITNAME:
1791 ret = (xmlIsLetter(codepoint) ||
1792 (codepoint == '_') || (codepoint == ':'));
1793 break;
1794 case XML_REGEXP_NOTNAMECHAR:
1795 neg = !neg;
1796 case XML_REGEXP_NAMECHAR:
1797 ret = (xmlIsLetter(codepoint) || xmlIsDigit(codepoint) ||
1798 (codepoint == '.') || (codepoint == '-') ||
1799 (codepoint == '_') || (codepoint == ':') ||
1800 xmlIsCombining(codepoint) || xmlIsExtender(codepoint));
1801 break;
1802 case XML_REGEXP_NOTDECIMAL:
1803 neg = !neg;
1804 case XML_REGEXP_DECIMAL:
1805 ret = xmlUCSIsCatNd(codepoint);
1806 break;
1807 case XML_REGEXP_REALCHAR:
1808 neg = !neg;
1809 case XML_REGEXP_NOTREALCHAR:
1810 ret = xmlUCSIsCatP(codepoint);
1811 if (ret == 0)
1812 ret = xmlUCSIsCatZ(codepoint);
1813 if (ret == 0)
1814 ret = xmlUCSIsCatC(codepoint);
1815 break;
1816 case XML_REGEXP_LETTER:
1817 ret = xmlUCSIsCatL(codepoint);
1818 break;
1819 case XML_REGEXP_LETTER_UPPERCASE:
1820 ret = xmlUCSIsCatLu(codepoint);
1821 break;
1822 case XML_REGEXP_LETTER_LOWERCASE:
1823 ret = xmlUCSIsCatLl(codepoint);
1824 break;
1825 case XML_REGEXP_LETTER_TITLECASE:
1826 ret = xmlUCSIsCatLt(codepoint);
1827 break;
1828 case XML_REGEXP_LETTER_MODIFIER:
1829 ret = xmlUCSIsCatLm(codepoint);
1830 break;
1831 case XML_REGEXP_LETTER_OTHERS:
1832 ret = xmlUCSIsCatLo(codepoint);
1833 break;
1834 case XML_REGEXP_MARK:
1835 ret = xmlUCSIsCatM(codepoint);
1836 break;
1837 case XML_REGEXP_MARK_NONSPACING:
1838 ret = xmlUCSIsCatMn(codepoint);
1839 break;
1840 case XML_REGEXP_MARK_SPACECOMBINING:
1841 ret = xmlUCSIsCatMc(codepoint);
1842 break;
1843 case XML_REGEXP_MARK_ENCLOSING:
1844 ret = xmlUCSIsCatMe(codepoint);
1845 break;
1846 case XML_REGEXP_NUMBER:
1847 ret = xmlUCSIsCatN(codepoint);
1848 break;
1849 case XML_REGEXP_NUMBER_DECIMAL:
1850 ret = xmlUCSIsCatNd(codepoint);
1851 break;
1852 case XML_REGEXP_NUMBER_LETTER:
1853 ret = xmlUCSIsCatNl(codepoint);
1854 break;
1855 case XML_REGEXP_NUMBER_OTHERS:
1856 ret = xmlUCSIsCatNo(codepoint);
1857 break;
1858 case XML_REGEXP_PUNCT:
1859 ret = xmlUCSIsCatP(codepoint);
1860 break;
1861 case XML_REGEXP_PUNCT_CONNECTOR:
1862 ret = xmlUCSIsCatPc(codepoint);
1863 break;
1864 case XML_REGEXP_PUNCT_DASH:
1865 ret = xmlUCSIsCatPd(codepoint);
1866 break;
1867 case XML_REGEXP_PUNCT_OPEN:
1868 ret = xmlUCSIsCatPs(codepoint);
1869 break;
1870 case XML_REGEXP_PUNCT_CLOSE:
1871 ret = xmlUCSIsCatPe(codepoint);
1872 break;
1873 case XML_REGEXP_PUNCT_INITQUOTE:
1874 ret = xmlUCSIsCatPi(codepoint);
1875 break;
1876 case XML_REGEXP_PUNCT_FINQUOTE:
1877 ret = xmlUCSIsCatPf(codepoint);
1878 break;
1879 case XML_REGEXP_PUNCT_OTHERS:
1880 ret = xmlUCSIsCatPo(codepoint);
1881 break;
1882 case XML_REGEXP_SEPAR:
1883 ret = xmlUCSIsCatZ(codepoint);
1884 break;
1885 case XML_REGEXP_SEPAR_SPACE:
1886 ret = xmlUCSIsCatZs(codepoint);
1887 break;
1888 case XML_REGEXP_SEPAR_LINE:
1889 ret = xmlUCSIsCatZl(codepoint);
1890 break;
1891 case XML_REGEXP_SEPAR_PARA:
1892 ret = xmlUCSIsCatZp(codepoint);
1893 break;
1894 case XML_REGEXP_SYMBOL:
1895 ret = xmlUCSIsCatS(codepoint);
1896 break;
1897 case XML_REGEXP_SYMBOL_MATH:
1898 ret = xmlUCSIsCatSm(codepoint);
1899 break;
1900 case XML_REGEXP_SYMBOL_CURRENCY:
1901 ret = xmlUCSIsCatSc(codepoint);
1902 break;
1903 case XML_REGEXP_SYMBOL_MODIFIER:
1904 ret = xmlUCSIsCatSk(codepoint);
1905 break;
1906 case XML_REGEXP_SYMBOL_OTHERS:
1907 ret = xmlUCSIsCatSo(codepoint);
1908 break;
1909 case XML_REGEXP_OTHER:
1910 ret = xmlUCSIsCatC(codepoint);
1911 break;
1912 case XML_REGEXP_OTHER_CONTROL:
1913 ret = xmlUCSIsCatCc(codepoint);
1914 break;
1915 case XML_REGEXP_OTHER_FORMAT:
1916 ret = xmlUCSIsCatCf(codepoint);
1917 break;
1918 case XML_REGEXP_OTHER_PRIVATE:
1919 ret = xmlUCSIsCatCo(codepoint);
1920 break;
1921 case XML_REGEXP_OTHER_NA:
1922 /* ret = xmlUCSIsCatCn(codepoint); */
1923 /* Seems it doesn't exist anymore in recent Unicode releases */
1924 ret = 0;
1925 break;
1926 case XML_REGEXP_BLOCK_NAME:
1927 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
1928 break;
1929 }
1930 if (neg)
1931 return(!ret);
1932 return(ret);
1933}
1934
1935static int
1936xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
1937 int i, ret = 0;
1938 xmlRegRangePtr range;
1939
1940 if ((atom == NULL) || (!xmlIsChar(codepoint)))
1941 return(-1);
1942
1943 switch (atom->type) {
1944 case XML_REGEXP_SUBREG:
1945 case XML_REGEXP_EPSILON:
1946 return(-1);
1947 case XML_REGEXP_CHARVAL:
1948 return(codepoint == atom->codepoint);
1949 case XML_REGEXP_RANGES: {
1950 int accept = 0;
1951 for (i = 0;i < atom->nbRanges;i++) {
1952 range = atom->ranges[i];
1953 if (range->neg) {
1954 ret = xmlRegCheckCharacterRange(range->type, codepoint,
1955 0, range->start, range->end,
1956 range->blockName);
1957 if (ret != 0)
1958 return(0); /* excluded char */
1959 } else {
1960 ret = xmlRegCheckCharacterRange(range->type, codepoint,
1961 0, range->start, range->end,
1962 range->blockName);
1963 if (ret != 0)
1964 accept = 1; /* might still be excluded */
1965 }
1966 }
1967 return(accept);
1968 }
1969 case XML_REGEXP_STRING:
1970 printf("TODO: XML_REGEXP_STRING\n");
1971 return(-1);
1972 case XML_REGEXP_ANYCHAR:
1973 case XML_REGEXP_ANYSPACE:
1974 case XML_REGEXP_NOTSPACE:
1975 case XML_REGEXP_INITNAME:
1976 case XML_REGEXP_NOTINITNAME:
1977 case XML_REGEXP_NAMECHAR:
1978 case XML_REGEXP_NOTNAMECHAR:
1979 case XML_REGEXP_DECIMAL:
1980 case XML_REGEXP_NOTDECIMAL:
1981 case XML_REGEXP_REALCHAR:
1982 case XML_REGEXP_NOTREALCHAR:
1983 case XML_REGEXP_LETTER:
1984 case XML_REGEXP_LETTER_UPPERCASE:
1985 case XML_REGEXP_LETTER_LOWERCASE:
1986 case XML_REGEXP_LETTER_TITLECASE:
1987 case XML_REGEXP_LETTER_MODIFIER:
1988 case XML_REGEXP_LETTER_OTHERS:
1989 case XML_REGEXP_MARK:
1990 case XML_REGEXP_MARK_NONSPACING:
1991 case XML_REGEXP_MARK_SPACECOMBINING:
1992 case XML_REGEXP_MARK_ENCLOSING:
1993 case XML_REGEXP_NUMBER:
1994 case XML_REGEXP_NUMBER_DECIMAL:
1995 case XML_REGEXP_NUMBER_LETTER:
1996 case XML_REGEXP_NUMBER_OTHERS:
1997 case XML_REGEXP_PUNCT:
1998 case XML_REGEXP_PUNCT_CONNECTOR:
1999 case XML_REGEXP_PUNCT_DASH:
2000 case XML_REGEXP_PUNCT_OPEN:
2001 case XML_REGEXP_PUNCT_CLOSE:
2002 case XML_REGEXP_PUNCT_INITQUOTE:
2003 case XML_REGEXP_PUNCT_FINQUOTE:
2004 case XML_REGEXP_PUNCT_OTHERS:
2005 case XML_REGEXP_SEPAR:
2006 case XML_REGEXP_SEPAR_SPACE:
2007 case XML_REGEXP_SEPAR_LINE:
2008 case XML_REGEXP_SEPAR_PARA:
2009 case XML_REGEXP_SYMBOL:
2010 case XML_REGEXP_SYMBOL_MATH:
2011 case XML_REGEXP_SYMBOL_CURRENCY:
2012 case XML_REGEXP_SYMBOL_MODIFIER:
2013 case XML_REGEXP_SYMBOL_OTHERS:
2014 case XML_REGEXP_OTHER:
2015 case XML_REGEXP_OTHER_CONTROL:
2016 case XML_REGEXP_OTHER_FORMAT:
2017 case XML_REGEXP_OTHER_PRIVATE:
2018 case XML_REGEXP_OTHER_NA:
2019 case XML_REGEXP_BLOCK_NAME:
2020 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2021 (const xmlChar *)atom->valuep);
2022 if (atom->neg)
2023 ret = !ret;
2024 break;
2025 }
2026 return(ret);
2027}
2028
2029/************************************************************************
2030 * *
2031 * Saving an restoring state of an execution context *
2032 * *
2033 ************************************************************************/
2034
2035#ifdef DEBUG_REGEXP_EXEC
2036static void
2037xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2038 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2039 if (exec->inputStack != NULL) {
2040 int i;
2041 printf(": ");
2042 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2043 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2044 } else {
2045 printf(": %s", &(exec->inputString[exec->index]));
2046 }
2047 printf("\n");
2048}
2049#endif
2050
2051static void
2052xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2053#ifdef DEBUG_REGEXP_EXEC
2054 printf("saving ");
2055 exec->transno++;
2056 xmlFARegDebugExec(exec);
2057 exec->transno--;
2058#endif
2059
2060 if (exec->maxRollbacks == 0) {
2061 exec->maxRollbacks = 4;
2062 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2063 sizeof(xmlRegExecRollback));
2064 if (exec->rollbacks == NULL) {
2065 fprintf(stderr, "exec save: allocation failed");
2066 exec->maxRollbacks = 0;
2067 return;
2068 }
2069 memset(exec->rollbacks, 0,
2070 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2071 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2072 xmlRegExecRollback *tmp;
2073 int len = exec->maxRollbacks;
2074
2075 exec->maxRollbacks *= 2;
2076 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2077 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2078 if (tmp == NULL) {
2079 fprintf(stderr, "exec save: allocation failed");
2080 exec->maxRollbacks /= 2;
2081 return;
2082 }
2083 exec->rollbacks = tmp;
2084 tmp = &exec->rollbacks[len];
2085 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2086 }
2087 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2088 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2089 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2090 if (exec->comp->nbCounters > 0) {
2091 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2092 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2093 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2094 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2095 fprintf(stderr, "exec save: allocation failed");
2096 exec->status = -5;
2097 return;
2098 }
2099 }
2100 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2101 exec->comp->nbCounters * sizeof(int));
2102 }
2103 exec->nbRollbacks++;
2104}
2105
2106static void
2107xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2108 if (exec->nbRollbacks <= 0) {
2109 exec->status = -1;
2110#ifdef DEBUG_REGEXP_EXEC
2111 printf("rollback failed on empty stack\n");
2112#endif
2113 return;
2114 }
2115 exec->nbRollbacks--;
2116 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2117 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2118 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2119 if (exec->comp->nbCounters > 0) {
2120 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2121 fprintf(stderr, "exec save: allocation failed");
2122 exec->status = -6;
2123 return;
2124 }
2125 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2126 exec->comp->nbCounters * sizeof(int));
2127 }
2128
2129#ifdef DEBUG_REGEXP_EXEC
2130 printf("restored ");
2131 xmlFARegDebugExec(exec);
2132#endif
2133}
2134
2135/************************************************************************
2136 * *
2137 * Verifyer, running an input against a compiled regexp *
2138 * *
2139 ************************************************************************/
2140
2141static int
2142xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2143 xmlRegExecCtxt execval;
2144 xmlRegExecCtxtPtr exec = &execval;
2145 int ret, codepoint, len;
2146
2147 exec->inputString = content;
2148 exec->index = 0;
2149 exec->determinist = 1;
2150 exec->maxRollbacks = 0;
2151 exec->nbRollbacks = 0;
2152 exec->rollbacks = NULL;
2153 exec->status = 0;
2154 exec->comp = comp;
2155 exec->state = comp->states[0];
2156 exec->transno = 0;
2157 exec->transcount = 0;
2158 if (comp->nbCounters > 0) {
2159 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2160 if (exec->counts == NULL)
2161 return(-1);
2162 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2163 } else
2164 exec->counts = NULL;
2165 while ((exec->status == 0) &&
2166 ((exec->inputString[exec->index] != 0) ||
2167 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2168 xmlRegTransPtr trans;
2169 xmlRegAtomPtr atom;
2170
2171 /*
2172 * End of input on non-terminal state, rollback, however we may
2173 * still have epsilon like transition for counted transitions
2174 * on counters, in that case don't break too early.
2175 */
2176 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2177 goto rollback;
2178
2179 exec->transcount = 0;
2180 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2181 trans = &exec->state->trans[exec->transno];
2182 if (trans->to < 0)
2183 continue;
2184 atom = trans->atom;
2185 ret = 0;
2186 if (trans->count >= 0) {
2187 int count;
2188 xmlRegCounterPtr counter;
2189
2190 /*
2191 * A counted transition.
2192 */
2193
2194 count = exec->counts[trans->count];
2195 counter = &exec->comp->counters[trans->count];
2196#ifdef DEBUG_REGEXP_EXEC
2197 printf("testing count %d: val %d, min %d, max %d\n",
2198 trans->count, count, counter->min, counter->max);
2199#endif
2200 ret = ((count >= counter->min) && (count <= counter->max));
2201 } else if (atom == NULL) {
2202 fprintf(stderr, "epsilon transition left at runtime\n");
2203 exec->status = -2;
2204 break;
2205 } else if (exec->inputString[exec->index] != 0) {
2206 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2207 ret = xmlRegCheckCharacter(atom, codepoint);
2208 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2209 xmlRegStatePtr to = comp->states[trans->to];
2210
2211 /*
2212 * this is a multiple input sequence
2213 */
2214 if (exec->state->nbTrans > exec->transno + 1) {
2215 xmlFARegExecSave(exec);
2216 }
2217 exec->transcount = 1;
2218 do {
2219 /*
2220 * Try to progress as much as possible on the input
2221 */
2222 if (exec->transcount == atom->max) {
2223 break;
2224 }
2225 exec->index += len;
2226 /*
2227 * End of input: stop here
2228 */
2229 if (exec->inputString[exec->index] == 0) {
2230 exec->index -= len;
2231 break;
2232 }
2233 if (exec->transcount >= atom->min) {
2234 int transno = exec->transno;
2235 xmlRegStatePtr state = exec->state;
2236
2237 /*
2238 * The transition is acceptable save it
2239 */
2240 exec->transno = -1; /* trick */
2241 exec->state = to;
2242 xmlFARegExecSave(exec);
2243 exec->transno = transno;
2244 exec->state = state;
2245 }
2246 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2247 len);
2248 ret = xmlRegCheckCharacter(atom, codepoint);
2249 exec->transcount++;
2250 } while (ret == 1);
2251 if (exec->transcount < atom->min)
2252 ret = 0;
2253
2254 /*
2255 * If the last check failed but one transition was found
2256 * possible, rollback
2257 */
2258 if (ret < 0)
2259 ret = 0;
2260 if (ret == 0) {
2261 goto rollback;
2262 }
2263 }
2264 }
2265 if (ret == 1) {
2266 if (exec->state->nbTrans > exec->transno + 1) {
2267 xmlFARegExecSave(exec);
2268 }
2269 if (trans->counter >= 0) {
2270#ifdef DEBUG_REGEXP_EXEC
2271 printf("Increasing count %d\n", trans->counter);
2272#endif
2273 exec->counts[trans->counter]++;
2274 }
2275#ifdef DEBUG_REGEXP_EXEC
2276 printf("entering state %d\n", trans->to);
2277#endif
2278 exec->state = comp->states[trans->to];
2279 exec->transno = 0;
2280 if (trans->atom != NULL) {
2281 exec->index += len;
2282 }
2283 goto progress;
2284 } else if (ret < 0) {
2285 exec->status = -4;
2286 break;
2287 }
2288 }
2289 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2290rollback:
2291 /*
2292 * Failed to find a way out
2293 */
2294 exec->determinist = 0;
2295 xmlFARegExecRollBack(exec);
2296 }
2297progress:
2298 continue;
2299 }
2300 if (exec->rollbacks != NULL) {
2301 if (exec->counts != NULL) {
2302 int i;
2303
2304 for (i = 0;i < exec->maxRollbacks;i++)
2305 if (exec->rollbacks[i].counts != NULL)
2306 xmlFree(exec->rollbacks[i].counts);
2307 }
2308 xmlFree(exec->rollbacks);
2309 }
2310 if (exec->counts != NULL)
2311 xmlFree(exec->counts);
2312 if (exec->status == 0)
2313 return(1);
2314 if (exec->status == -1)
2315 return(0);
2316 return(exec->status);
2317}
2318
2319/************************************************************************
2320 * *
2321 * Progressive interface to the verifyer one atom at a time *
2322 * *
2323 ************************************************************************/
2324
2325/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002326 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002327 * @comp: a precompiled regular expression
2328 * @callback: a callback function used for handling progresses in the
2329 * automata matching phase
2330 * @data: the context data associated to the callback in this context
2331 *
2332 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002333 *
2334 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002335 */
2336xmlRegExecCtxtPtr
2337xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2338 xmlRegExecCtxtPtr exec;
2339
2340 if (comp == NULL)
2341 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002342 if ((comp->compact == NULL) && (comp->states == NULL))
2343 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002344 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2345 if (exec == NULL) {
2346 return(NULL);
2347 }
2348 memset(exec, 0, sizeof(xmlRegExecCtxt));
2349 exec->inputString = NULL;
2350 exec->index = 0;
2351 exec->determinist = 1;
2352 exec->maxRollbacks = 0;
2353 exec->nbRollbacks = 0;
2354 exec->rollbacks = NULL;
2355 exec->status = 0;
2356 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002357 if (comp->compact == NULL)
2358 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002359 exec->transno = 0;
2360 exec->transcount = 0;
2361 exec->callback = callback;
2362 exec->data = data;
2363 if (comp->nbCounters > 0) {
2364 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2365 if (exec->counts == NULL) {
2366 xmlFree(exec);
2367 return(NULL);
2368 }
2369 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2370 } else
2371 exec->counts = NULL;
2372 exec->inputStackMax = 0;
2373 exec->inputStackNr = 0;
2374 exec->inputStack = NULL;
2375 return(exec);
2376}
2377
2378/**
2379 * xmlRegFreeExecCtxt:
2380 * @exec: a regular expression evaulation context
2381 *
2382 * Free the structures associated to a regular expression evaulation context.
2383 */
2384void
2385xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2386 if (exec == NULL)
2387 return;
2388
2389 if (exec->rollbacks != NULL) {
2390 if (exec->counts != NULL) {
2391 int i;
2392
2393 for (i = 0;i < exec->maxRollbacks;i++)
2394 if (exec->rollbacks[i].counts != NULL)
2395 xmlFree(exec->rollbacks[i].counts);
2396 }
2397 xmlFree(exec->rollbacks);
2398 }
2399 if (exec->counts != NULL)
2400 xmlFree(exec->counts);
2401 if (exec->inputStack != NULL) {
2402 int i;
2403
Daniel Veillard32370232002-10-16 14:08:14 +00002404 for (i = 0;i < exec->inputStackNr;i++) {
2405 if (exec->inputStack[i].value != NULL)
2406 xmlFree(exec->inputStack[i].value);
2407 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002408 xmlFree(exec->inputStack);
2409 }
2410 xmlFree(exec);
2411}
2412
2413static void
2414xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2415 void *data) {
2416#ifdef DEBUG_PUSH
2417 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2418#endif
2419 if (exec->inputStackMax == 0) {
2420 exec->inputStackMax = 4;
2421 exec->inputStack = (xmlRegInputTokenPtr)
2422 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2423 if (exec->inputStack == NULL) {
2424 fprintf(stderr, "push input: allocation failed");
2425 exec->inputStackMax = 0;
2426 return;
2427 }
2428 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2429 xmlRegInputTokenPtr tmp;
2430
2431 exec->inputStackMax *= 2;
2432 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2433 exec->inputStackMax * sizeof(xmlRegInputToken));
2434 if (tmp == NULL) {
2435 fprintf(stderr, "push input: allocation failed");
2436 exec->inputStackMax /= 2;
2437 return;
2438 }
2439 exec->inputStack = tmp;
2440 }
2441 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2442 exec->inputStack[exec->inputStackNr].data = data;
2443 exec->inputStackNr++;
2444 exec->inputStack[exec->inputStackNr].value = NULL;
2445 exec->inputStack[exec->inputStackNr].data = NULL;
2446}
2447
2448
2449/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002450 * xmlRegCompactPushString:
2451 * @exec: a regexp execution context
2452 * @comp: the precompiled exec with a compact table
2453 * @value: a string token input
2454 * @data: data associated to the token to reuse in callbacks
2455 *
2456 * Push one input token in the execution context
2457 *
2458 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2459 * a negative value in case of error.
2460 */
2461static int
2462xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2463 xmlRegexpPtr comp,
2464 const xmlChar *value,
2465 void *data) {
2466 int state = exec->index;
2467 int i, target;
2468
2469 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2470 return(-1);
2471
2472 if (value == NULL) {
2473 /*
2474 * are we at a final state ?
2475 */
2476 if (comp->compact[state * (comp->nbstrings + 1)] ==
2477 XML_REGEXP_FINAL_STATE)
2478 return(1);
2479 return(0);
2480 }
2481
2482#ifdef DEBUG_PUSH
2483 printf("value pushed: %s\n", value);
2484#endif
2485
2486 /*
2487 * Examine all outside transition from current state
2488 */
2489 for (i = 0;i < comp->nbstrings;i++) {
2490 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2491 if ((target > 0) && (target <= comp->nbstates)) {
2492 target--; /* to avoid 0 */
2493 if (xmlStrEqual(comp->stringMap[i], value)) {
2494 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002495 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2496 exec->callback(exec->data, value,
2497 comp->transdata[state * comp->nbstrings + i], data);
2498 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002499#ifdef DEBUG_PUSH
2500 printf("entering state %d\n", target);
2501#endif
2502 if (comp->compact[target * (comp->nbstrings + 1)] ==
2503 XML_REGEXP_FINAL_STATE)
2504 return(1);
2505 return(0);
2506 }
2507 }
2508 }
2509 /*
2510 * Failed to find an exit transition out from current state for the
2511 * current token
2512 */
2513#ifdef DEBUG_PUSH
2514 printf("failed to find a transition for %s on state %d\n", value, state);
2515#endif
2516 exec->status = -1;
2517 return(-1);
2518}
2519
2520/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002521 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002522 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002523 * @value: a string token input
2524 * @data: data associated to the token to reuse in callbacks
2525 *
2526 * Push one input token in the execution context
2527 *
2528 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2529 * a negative value in case of error.
2530 */
2531int
2532xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2533 void *data) {
2534 xmlRegTransPtr trans;
2535 xmlRegAtomPtr atom;
2536 int ret;
2537 int final = 0;
2538
2539 if (exec == NULL)
2540 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002541 if (exec->comp == NULL)
2542 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002543 if (exec->status != 0)
2544 return(exec->status);
2545
Daniel Veillard23e73572002-09-19 19:56:43 +00002546 if (exec->comp->compact != NULL)
2547 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2548
Daniel Veillard4255d502002-04-16 15:50:10 +00002549 if (value == NULL) {
2550 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2551 return(1);
2552 final = 1;
2553 }
2554
2555#ifdef DEBUG_PUSH
2556 printf("value pushed: %s\n", value);
2557#endif
2558 /*
2559 * If we have an active rollback stack push the new value there
2560 * and get back to where we were left
2561 */
2562 if ((value != NULL) && (exec->inputStackNr > 0)) {
2563 xmlFARegExecSaveInputString(exec, value, data);
2564 value = exec->inputStack[exec->index].value;
2565 data = exec->inputStack[exec->index].data;
2566#ifdef DEBUG_PUSH
2567 printf("value loaded: %s\n", value);
2568#endif
2569 }
2570
2571 while ((exec->status == 0) &&
2572 ((value != NULL) ||
2573 ((final == 1) &&
2574 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2575
2576 /*
2577 * End of input on non-terminal state, rollback, however we may
2578 * still have epsilon like transition for counted transitions
2579 * on counters, in that case don't break too early.
2580 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002581 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002582 goto rollback;
2583
2584 exec->transcount = 0;
2585 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2586 trans = &exec->state->trans[exec->transno];
2587 if (trans->to < 0)
2588 continue;
2589 atom = trans->atom;
2590 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002591 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2592 int i;
2593 int count;
2594 xmlRegTransPtr t;
2595 xmlRegCounterPtr counter;
2596
2597 ret = 0;
2598
2599#ifdef DEBUG_PUSH
2600 printf("testing all lax %d\n", trans->count);
2601#endif
2602 /*
2603 * Check all counted transitions from the current state
2604 */
2605 if ((value == NULL) && (final)) {
2606 ret = 1;
2607 } else if (value != NULL) {
2608 for (i = 0;i < exec->state->nbTrans;i++) {
2609 t = &exec->state->trans[i];
2610 if ((t->counter < 0) || (t == trans))
2611 continue;
2612 counter = &exec->comp->counters[t->counter];
2613 count = exec->counts[t->counter];
2614 if ((count < counter->max) &&
2615 (t->atom != NULL) &&
2616 (xmlStrEqual(value, t->atom->valuep))) {
2617 ret = 0;
2618 break;
2619 }
2620 if ((count >= counter->min) &&
2621 (count < counter->max) &&
2622 (xmlStrEqual(value, t->atom->valuep))) {
2623 ret = 1;
2624 break;
2625 }
2626 }
2627 }
2628 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002629 int i;
2630 int count;
2631 xmlRegTransPtr t;
2632 xmlRegCounterPtr counter;
2633
2634 ret = 1;
2635
2636#ifdef DEBUG_PUSH
2637 printf("testing all %d\n", trans->count);
2638#endif
2639 /*
2640 * Check all counted transitions from the current state
2641 */
2642 for (i = 0;i < exec->state->nbTrans;i++) {
2643 t = &exec->state->trans[i];
2644 if ((t->counter < 0) || (t == trans))
2645 continue;
2646 counter = &exec->comp->counters[t->counter];
2647 count = exec->counts[t->counter];
2648 if ((count < counter->min) || (count > counter->max)) {
2649 ret = 0;
2650 break;
2651 }
2652 }
2653 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002654 int count;
2655 xmlRegCounterPtr counter;
2656
2657 /*
2658 * A counted transition.
2659 */
2660
2661 count = exec->counts[trans->count];
2662 counter = &exec->comp->counters[trans->count];
2663#ifdef DEBUG_PUSH
2664 printf("testing count %d: val %d, min %d, max %d\n",
2665 trans->count, count, counter->min, counter->max);
2666#endif
2667 ret = ((count >= counter->min) && (count <= counter->max));
2668 } else if (atom == NULL) {
2669 fprintf(stderr, "epsilon transition left at runtime\n");
2670 exec->status = -2;
2671 break;
2672 } else if (value != NULL) {
2673 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002674 if ((ret == 1) && (trans->counter >= 0)) {
2675 xmlRegCounterPtr counter;
2676 int count;
2677
2678 count = exec->counts[trans->counter];
2679 counter = &exec->comp->counters[trans->counter];
2680 if (count >= counter->max)
2681 ret = 0;
2682 }
2683
Daniel Veillard4255d502002-04-16 15:50:10 +00002684 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2685 xmlRegStatePtr to = exec->comp->states[trans->to];
2686
2687 /*
2688 * this is a multiple input sequence
2689 */
2690 if (exec->state->nbTrans > exec->transno + 1) {
2691 if (exec->inputStackNr <= 0) {
2692 xmlFARegExecSaveInputString(exec, value, data);
2693 }
2694 xmlFARegExecSave(exec);
2695 }
2696 exec->transcount = 1;
2697 do {
2698 /*
2699 * Try to progress as much as possible on the input
2700 */
2701 if (exec->transcount == atom->max) {
2702 break;
2703 }
2704 exec->index++;
2705 value = exec->inputStack[exec->index].value;
2706 data = exec->inputStack[exec->index].data;
2707#ifdef DEBUG_PUSH
2708 printf("value loaded: %s\n", value);
2709#endif
2710
2711 /*
2712 * End of input: stop here
2713 */
2714 if (value == NULL) {
2715 exec->index --;
2716 break;
2717 }
2718 if (exec->transcount >= atom->min) {
2719 int transno = exec->transno;
2720 xmlRegStatePtr state = exec->state;
2721
2722 /*
2723 * The transition is acceptable save it
2724 */
2725 exec->transno = -1; /* trick */
2726 exec->state = to;
2727 if (exec->inputStackNr <= 0) {
2728 xmlFARegExecSaveInputString(exec, value, data);
2729 }
2730 xmlFARegExecSave(exec);
2731 exec->transno = transno;
2732 exec->state = state;
2733 }
2734 ret = xmlStrEqual(value, atom->valuep);
2735 exec->transcount++;
2736 } while (ret == 1);
2737 if (exec->transcount < atom->min)
2738 ret = 0;
2739
2740 /*
2741 * If the last check failed but one transition was found
2742 * possible, rollback
2743 */
2744 if (ret < 0)
2745 ret = 0;
2746 if (ret == 0) {
2747 goto rollback;
2748 }
2749 }
2750 }
2751 if (ret == 1) {
2752 if ((exec->callback != NULL) && (atom != NULL)) {
2753 exec->callback(exec->data, atom->valuep,
2754 atom->data, data);
2755 }
2756 if (exec->state->nbTrans > exec->transno + 1) {
2757 if (exec->inputStackNr <= 0) {
2758 xmlFARegExecSaveInputString(exec, value, data);
2759 }
2760 xmlFARegExecSave(exec);
2761 }
2762 if (trans->counter >= 0) {
2763#ifdef DEBUG_PUSH
2764 printf("Increasing count %d\n", trans->counter);
2765#endif
2766 exec->counts[trans->counter]++;
2767 }
2768#ifdef DEBUG_PUSH
2769 printf("entering state %d\n", trans->to);
2770#endif
2771 exec->state = exec->comp->states[trans->to];
2772 exec->transno = 0;
2773 if (trans->atom != NULL) {
2774 if (exec->inputStack != NULL) {
2775 exec->index++;
2776 if (exec->index < exec->inputStackNr) {
2777 value = exec->inputStack[exec->index].value;
2778 data = exec->inputStack[exec->index].data;
2779#ifdef DEBUG_PUSH
2780 printf("value loaded: %s\n", value);
2781#endif
2782 } else {
2783 value = NULL;
2784 data = NULL;
2785#ifdef DEBUG_PUSH
2786 printf("end of input\n");
2787#endif
2788 }
2789 } else {
2790 value = NULL;
2791 data = NULL;
2792#ifdef DEBUG_PUSH
2793 printf("end of input\n");
2794#endif
2795 }
2796 }
2797 goto progress;
2798 } else if (ret < 0) {
2799 exec->status = -4;
2800 break;
2801 }
2802 }
2803 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2804rollback:
2805 /*
2806 * Failed to find a way out
2807 */
2808 exec->determinist = 0;
2809 xmlFARegExecRollBack(exec);
2810 if (exec->status == 0) {
2811 value = exec->inputStack[exec->index].value;
2812 data = exec->inputStack[exec->index].data;
2813#ifdef DEBUG_PUSH
2814 printf("value loaded: %s\n", value);
2815#endif
2816 }
2817 }
2818progress:
2819 continue;
2820 }
2821 if (exec->status == 0) {
2822 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2823 }
2824 return(exec->status);
2825}
2826
Daniel Veillard52b48c72003-04-13 19:53:42 +00002827/**
2828 * xmlRegExecPushString2:
2829 * @exec: a regexp execution context or NULL to indicate the end
2830 * @value: the first string token input
2831 * @value2: the second string token input
2832 * @data: data associated to the token to reuse in callbacks
2833 *
2834 * Push one input token in the execution context
2835 *
2836 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2837 * a negative value in case of error.
2838 */
2839int
2840xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
2841 const xmlChar *value2, void *data) {
2842 xmlChar buf[150];
2843 int lenn, lenp, ret;
2844 xmlChar *str;
2845
2846 if (exec == NULL)
2847 return(-1);
2848 if (exec->comp == NULL)
2849 return(-1);
2850 if (exec->status != 0)
2851 return(exec->status);
2852
2853 if (value2 == NULL)
2854 return(xmlRegExecPushString(exec, value, data));
2855
2856 lenn = strlen((char *) value2);
2857 lenp = strlen((char *) value);
2858
2859 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00002860 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00002861 if (str == NULL) {
2862 exec->status = -1;
2863 return(-1);
2864 }
2865 } else {
2866 str = buf;
2867 }
2868 memcpy(&str[0], value, lenp);
2869 str[lenp] = '|';
2870 memcpy(&str[lenp + 1], value2, lenn);
2871 str[lenn + lenp + 1] = 0;
2872
2873 if (exec->comp->compact != NULL)
2874 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
2875 else
2876 ret = xmlRegExecPushString(exec, str, data);
2877
2878 if (str != buf)
2879 xmlFree(buf);
2880 return(ret);
2881}
2882
Daniel Veillard4255d502002-04-16 15:50:10 +00002883#if 0
2884static int
2885xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2886 xmlRegTransPtr trans;
2887 xmlRegAtomPtr atom;
2888 int ret;
2889 int codepoint, len;
2890
2891 if (exec == NULL)
2892 return(-1);
2893 if (exec->status != 0)
2894 return(exec->status);
2895
2896 while ((exec->status == 0) &&
2897 ((exec->inputString[exec->index] != 0) ||
2898 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2899
2900 /*
2901 * End of input on non-terminal state, rollback, however we may
2902 * still have epsilon like transition for counted transitions
2903 * on counters, in that case don't break too early.
2904 */
2905 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2906 goto rollback;
2907
2908 exec->transcount = 0;
2909 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2910 trans = &exec->state->trans[exec->transno];
2911 if (trans->to < 0)
2912 continue;
2913 atom = trans->atom;
2914 ret = 0;
2915 if (trans->count >= 0) {
2916 int count;
2917 xmlRegCounterPtr counter;
2918
2919 /*
2920 * A counted transition.
2921 */
2922
2923 count = exec->counts[trans->count];
2924 counter = &exec->comp->counters[trans->count];
2925#ifdef DEBUG_REGEXP_EXEC
2926 printf("testing count %d: val %d, min %d, max %d\n",
2927 trans->count, count, counter->min, counter->max);
2928#endif
2929 ret = ((count >= counter->min) && (count <= counter->max));
2930 } else if (atom == NULL) {
2931 fprintf(stderr, "epsilon transition left at runtime\n");
2932 exec->status = -2;
2933 break;
2934 } else if (exec->inputString[exec->index] != 0) {
2935 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2936 ret = xmlRegCheckCharacter(atom, codepoint);
2937 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2938 xmlRegStatePtr to = exec->comp->states[trans->to];
2939
2940 /*
2941 * this is a multiple input sequence
2942 */
2943 if (exec->state->nbTrans > exec->transno + 1) {
2944 xmlFARegExecSave(exec);
2945 }
2946 exec->transcount = 1;
2947 do {
2948 /*
2949 * Try to progress as much as possible on the input
2950 */
2951 if (exec->transcount == atom->max) {
2952 break;
2953 }
2954 exec->index += len;
2955 /*
2956 * End of input: stop here
2957 */
2958 if (exec->inputString[exec->index] == 0) {
2959 exec->index -= len;
2960 break;
2961 }
2962 if (exec->transcount >= atom->min) {
2963 int transno = exec->transno;
2964 xmlRegStatePtr state = exec->state;
2965
2966 /*
2967 * The transition is acceptable save it
2968 */
2969 exec->transno = -1; /* trick */
2970 exec->state = to;
2971 xmlFARegExecSave(exec);
2972 exec->transno = transno;
2973 exec->state = state;
2974 }
2975 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2976 len);
2977 ret = xmlRegCheckCharacter(atom, codepoint);
2978 exec->transcount++;
2979 } while (ret == 1);
2980 if (exec->transcount < atom->min)
2981 ret = 0;
2982
2983 /*
2984 * If the last check failed but one transition was found
2985 * possible, rollback
2986 */
2987 if (ret < 0)
2988 ret = 0;
2989 if (ret == 0) {
2990 goto rollback;
2991 }
2992 }
2993 }
2994 if (ret == 1) {
2995 if (exec->state->nbTrans > exec->transno + 1) {
2996 xmlFARegExecSave(exec);
2997 }
2998 if (trans->counter >= 0) {
2999#ifdef DEBUG_REGEXP_EXEC
3000 printf("Increasing count %d\n", trans->counter);
3001#endif
3002 exec->counts[trans->counter]++;
3003 }
3004#ifdef DEBUG_REGEXP_EXEC
3005 printf("entering state %d\n", trans->to);
3006#endif
3007 exec->state = exec->comp->states[trans->to];
3008 exec->transno = 0;
3009 if (trans->atom != NULL) {
3010 exec->index += len;
3011 }
3012 goto progress;
3013 } else if (ret < 0) {
3014 exec->status = -4;
3015 break;
3016 }
3017 }
3018 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3019rollback:
3020 /*
3021 * Failed to find a way out
3022 */
3023 exec->determinist = 0;
3024 xmlFARegExecRollBack(exec);
3025 }
3026progress:
3027 continue;
3028 }
3029}
3030#endif
3031/************************************************************************
3032 * *
3033 * Parser for the Shemas Datatype Regular Expressions *
3034 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3035 * *
3036 ************************************************************************/
3037
3038/**
3039 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003040 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003041 *
3042 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3043 */
3044static int
3045xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3046 int cur;
3047 int len;
3048
3049 cur = CUR_SCHAR(ctxt->cur, len);
3050 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3051 (cur == '*') || (cur == '+') || (cur == '(') ||
3052 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3053 (cur == 0x5D) || (cur == 0))
3054 return(-1);
3055 return(cur);
3056}
3057
3058/**
3059 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003060 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003061 *
3062 * [27] charProp ::= IsCategory | IsBlock
3063 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3064 * Separators | Symbols | Others
3065 * [29] Letters ::= 'L' [ultmo]?
3066 * [30] Marks ::= 'M' [nce]?
3067 * [31] Numbers ::= 'N' [dlo]?
3068 * [32] Punctuation ::= 'P' [cdseifo]?
3069 * [33] Separators ::= 'Z' [slp]?
3070 * [34] Symbols ::= 'S' [mcko]?
3071 * [35] Others ::= 'C' [cfon]?
3072 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3073 */
3074static void
3075xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3076 int cur;
3077 xmlRegAtomType type = 0;
3078 xmlChar *blockName = NULL;
3079
3080 cur = CUR;
3081 if (cur == 'L') {
3082 NEXT;
3083 cur = CUR;
3084 if (cur == 'u') {
3085 NEXT;
3086 type = XML_REGEXP_LETTER_UPPERCASE;
3087 } else if (cur == 'l') {
3088 NEXT;
3089 type = XML_REGEXP_LETTER_LOWERCASE;
3090 } else if (cur == 't') {
3091 NEXT;
3092 type = XML_REGEXP_LETTER_TITLECASE;
3093 } else if (cur == 'm') {
3094 NEXT;
3095 type = XML_REGEXP_LETTER_MODIFIER;
3096 } else if (cur == 'o') {
3097 NEXT;
3098 type = XML_REGEXP_LETTER_OTHERS;
3099 } else {
3100 type = XML_REGEXP_LETTER;
3101 }
3102 } else if (cur == 'M') {
3103 NEXT;
3104 cur = CUR;
3105 if (cur == 'n') {
3106 NEXT;
3107 /* nonspacing */
3108 type = XML_REGEXP_MARK_NONSPACING;
3109 } else if (cur == 'c') {
3110 NEXT;
3111 /* spacing combining */
3112 type = XML_REGEXP_MARK_SPACECOMBINING;
3113 } else if (cur == 'e') {
3114 NEXT;
3115 /* enclosing */
3116 type = XML_REGEXP_MARK_ENCLOSING;
3117 } else {
3118 /* all marks */
3119 type = XML_REGEXP_MARK;
3120 }
3121 } else if (cur == 'N') {
3122 NEXT;
3123 cur = CUR;
3124 if (cur == 'd') {
3125 NEXT;
3126 /* digital */
3127 type = XML_REGEXP_NUMBER_DECIMAL;
3128 } else if (cur == 'l') {
3129 NEXT;
3130 /* letter */
3131 type = XML_REGEXP_NUMBER_LETTER;
3132 } else if (cur == 'o') {
3133 NEXT;
3134 /* other */
3135 type = XML_REGEXP_NUMBER_OTHERS;
3136 } else {
3137 /* all numbers */
3138 type = XML_REGEXP_NUMBER;
3139 }
3140 } else if (cur == 'P') {
3141 NEXT;
3142 cur = CUR;
3143 if (cur == 'c') {
3144 NEXT;
3145 /* connector */
3146 type = XML_REGEXP_PUNCT_CONNECTOR;
3147 } else if (cur == 'd') {
3148 NEXT;
3149 /* dash */
3150 type = XML_REGEXP_PUNCT_DASH;
3151 } else if (cur == 's') {
3152 NEXT;
3153 /* open */
3154 type = XML_REGEXP_PUNCT_OPEN;
3155 } else if (cur == 'e') {
3156 NEXT;
3157 /* close */
3158 type = XML_REGEXP_PUNCT_CLOSE;
3159 } else if (cur == 'i') {
3160 NEXT;
3161 /* initial quote */
3162 type = XML_REGEXP_PUNCT_INITQUOTE;
3163 } else if (cur == 'f') {
3164 NEXT;
3165 /* final quote */
3166 type = XML_REGEXP_PUNCT_FINQUOTE;
3167 } else if (cur == 'o') {
3168 NEXT;
3169 /* other */
3170 type = XML_REGEXP_PUNCT_OTHERS;
3171 } else {
3172 /* all punctuation */
3173 type = XML_REGEXP_PUNCT;
3174 }
3175 } else if (cur == 'Z') {
3176 NEXT;
3177 cur = CUR;
3178 if (cur == 's') {
3179 NEXT;
3180 /* space */
3181 type = XML_REGEXP_SEPAR_SPACE;
3182 } else if (cur == 'l') {
3183 NEXT;
3184 /* line */
3185 type = XML_REGEXP_SEPAR_LINE;
3186 } else if (cur == 'p') {
3187 NEXT;
3188 /* paragraph */
3189 type = XML_REGEXP_SEPAR_PARA;
3190 } else {
3191 /* all separators */
3192 type = XML_REGEXP_SEPAR;
3193 }
3194 } else if (cur == 'S') {
3195 NEXT;
3196 cur = CUR;
3197 if (cur == 'm') {
3198 NEXT;
3199 type = XML_REGEXP_SYMBOL_MATH;
3200 /* math */
3201 } else if (cur == 'c') {
3202 NEXT;
3203 type = XML_REGEXP_SYMBOL_CURRENCY;
3204 /* currency */
3205 } else if (cur == 'k') {
3206 NEXT;
3207 type = XML_REGEXP_SYMBOL_MODIFIER;
3208 /* modifiers */
3209 } else if (cur == 'o') {
3210 NEXT;
3211 type = XML_REGEXP_SYMBOL_OTHERS;
3212 /* other */
3213 } else {
3214 /* all symbols */
3215 type = XML_REGEXP_SYMBOL;
3216 }
3217 } else if (cur == 'C') {
3218 NEXT;
3219 cur = CUR;
3220 if (cur == 'c') {
3221 NEXT;
3222 /* control */
3223 type = XML_REGEXP_OTHER_CONTROL;
3224 } else if (cur == 'f') {
3225 NEXT;
3226 /* format */
3227 type = XML_REGEXP_OTHER_FORMAT;
3228 } else if (cur == 'o') {
3229 NEXT;
3230 /* private use */
3231 type = XML_REGEXP_OTHER_PRIVATE;
3232 } else if (cur == 'n') {
3233 NEXT;
3234 /* not assigned */
3235 type = XML_REGEXP_OTHER_NA;
3236 } else {
3237 /* all others */
3238 type = XML_REGEXP_OTHER;
3239 }
3240 } else if (cur == 'I') {
3241 const xmlChar *start;
3242 NEXT;
3243 cur = CUR;
3244 if (cur != 's') {
3245 ERROR("IsXXXX expected");
3246 return;
3247 }
3248 NEXT;
3249 start = ctxt->cur;
3250 cur = CUR;
3251 if (((cur >= 'a') && (cur <= 'z')) ||
3252 ((cur >= 'A') && (cur <= 'Z')) ||
3253 ((cur >= '0') && (cur <= '9')) ||
3254 (cur == 0x2D)) {
3255 NEXT;
3256 cur = CUR;
3257 while (((cur >= 'a') && (cur <= 'z')) ||
3258 ((cur >= 'A') && (cur <= 'Z')) ||
3259 ((cur >= '0') && (cur <= '9')) ||
3260 (cur == 0x2D)) {
3261 NEXT;
3262 cur = CUR;
3263 }
3264 }
3265 type = XML_REGEXP_BLOCK_NAME;
3266 blockName = xmlStrndup(start, ctxt->cur - start);
3267 } else {
3268 ERROR("Unknown char property");
3269 return;
3270 }
3271 if (ctxt->atom == NULL) {
3272 ctxt->atom = xmlRegNewAtom(ctxt, type);
3273 if (ctxt->atom != NULL)
3274 ctxt->atom->valuep = blockName;
3275 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3276 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3277 type, 0, 0, blockName);
3278 }
3279}
3280
3281/**
3282 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003283 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003284 *
3285 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3286 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3287 * [25] catEsc ::= '\p{' charProp '}'
3288 * [26] complEsc ::= '\P{' charProp '}'
3289 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3290 */
3291static void
3292xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3293 int cur;
3294
3295 if (CUR == '.') {
3296 if (ctxt->atom == NULL) {
3297 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3298 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3299 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3300 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3301 }
3302 NEXT;
3303 return;
3304 }
3305 if (CUR != '\\') {
3306 ERROR("Escaped sequence: expecting \\");
3307 return;
3308 }
3309 NEXT;
3310 cur = CUR;
3311 if (cur == 'p') {
3312 NEXT;
3313 if (CUR != '{') {
3314 ERROR("Expecting '{'");
3315 return;
3316 }
3317 NEXT;
3318 xmlFAParseCharProp(ctxt);
3319 if (CUR != '}') {
3320 ERROR("Expecting '}'");
3321 return;
3322 }
3323 NEXT;
3324 } else if (cur == 'P') {
3325 NEXT;
3326 if (CUR != '{') {
3327 ERROR("Expecting '{'");
3328 return;
3329 }
3330 NEXT;
3331 xmlFAParseCharProp(ctxt);
3332 ctxt->atom->neg = 1;
3333 if (CUR != '}') {
3334 ERROR("Expecting '}'");
3335 return;
3336 }
3337 NEXT;
3338 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3339 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3340 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3341 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3342 (cur == 0x5E)) {
3343 if (ctxt->atom == NULL) {
3344 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3345 if (ctxt->atom != NULL)
3346 ctxt->atom->codepoint = cur;
3347 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3348 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3349 XML_REGEXP_CHARVAL, cur, cur, NULL);
3350 }
3351 NEXT;
3352 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3353 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3354 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003355 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003356
3357 switch (cur) {
3358 case 's':
3359 type = XML_REGEXP_ANYSPACE;
3360 break;
3361 case 'S':
3362 type = XML_REGEXP_NOTSPACE;
3363 break;
3364 case 'i':
3365 type = XML_REGEXP_INITNAME;
3366 break;
3367 case 'I':
3368 type = XML_REGEXP_NOTINITNAME;
3369 break;
3370 case 'c':
3371 type = XML_REGEXP_NAMECHAR;
3372 break;
3373 case 'C':
3374 type = XML_REGEXP_NOTNAMECHAR;
3375 break;
3376 case 'd':
3377 type = XML_REGEXP_DECIMAL;
3378 break;
3379 case 'D':
3380 type = XML_REGEXP_NOTDECIMAL;
3381 break;
3382 case 'w':
3383 type = XML_REGEXP_REALCHAR;
3384 break;
3385 case 'W':
3386 type = XML_REGEXP_NOTREALCHAR;
3387 break;
3388 }
3389 NEXT;
3390 if (ctxt->atom == NULL) {
3391 ctxt->atom = xmlRegNewAtom(ctxt, type);
3392 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3393 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3394 type, 0, 0, NULL);
3395 }
3396 }
3397}
3398
3399/**
3400 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003401 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003402 *
3403 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3404 */
3405static int
3406xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3407 int ret = 0, cur;
3408
3409 if ((CUR != '&') || (NXT(1) != '#'))
3410 return(-1);
3411 NEXT;
3412 NEXT;
3413 cur = CUR;
3414 if (cur == 'x') {
3415 NEXT;
3416 cur = CUR;
3417 if (((cur >= '0') && (cur <= '9')) ||
3418 ((cur >= 'a') && (cur <= 'f')) ||
3419 ((cur >= 'A') && (cur <= 'F'))) {
3420 while (((cur >= '0') && (cur <= '9')) ||
3421 ((cur >= 'A') && (cur <= 'F'))) {
3422 if ((cur >= '0') && (cur <= '9'))
3423 ret = ret * 16 + cur - '0';
3424 else if ((cur >= 'a') && (cur <= 'f'))
3425 ret = ret * 16 + 10 + (cur - 'a');
3426 else
3427 ret = ret * 16 + 10 + (cur - 'A');
3428 NEXT;
3429 cur = CUR;
3430 }
3431 } else {
3432 ERROR("Char ref: expecting [0-9A-F]");
3433 return(-1);
3434 }
3435 } else {
3436 if ((cur >= '0') && (cur <= '9')) {
3437 while ((cur >= '0') && (cur <= '9')) {
3438 ret = ret * 10 + cur - '0';
3439 NEXT;
3440 cur = CUR;
3441 }
3442 } else {
3443 ERROR("Char ref: expecting [0-9]");
3444 return(-1);
3445 }
3446 }
3447 if (cur != ';') {
3448 ERROR("Char ref: expecting ';'");
3449 return(-1);
3450 } else {
3451 NEXT;
3452 }
3453 return(ret);
3454}
3455
3456/**
3457 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003458 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003459 *
3460 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3461 * [18] seRange ::= charOrEsc '-' charOrEsc
3462 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3463 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3464 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3465 */
3466static void
3467xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
3468 int cur;
3469 int start = -1;
3470 int end = -1;
3471
3472 if ((CUR == '&') && (NXT(1) == '#')) {
3473 end = start = xmlFAParseCharRef(ctxt);
3474 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3475 XML_REGEXP_CHARVAL, start, end, NULL);
3476 return;
3477 }
3478 cur = CUR;
3479 if (cur == '\\') {
3480 NEXT;
3481 cur = CUR;
3482 switch (cur) {
3483 case 'n': start = 0xA; break;
3484 case 'r': start = 0xD; break;
3485 case 't': start = 0x9; break;
3486 case '\\': case '|': case '.': case '-': case '^': case '?':
3487 case '*': case '+': case '{': case '}': case '(': case ')':
3488 case '[': case ']':
3489 start = cur; break;
3490 default:
3491 ERROR("Invalid escape value");
3492 return;
3493 }
3494 end = start;
3495 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3496 end = start = cur;
3497 } else {
3498 ERROR("Expecting a char range");
3499 return;
3500 }
3501 NEXT;
3502 if (start == '-') {
3503 return;
3504 }
3505 cur = CUR;
3506 if (cur != '-') {
3507 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3508 XML_REGEXP_CHARVAL, start, end, NULL);
3509 return;
3510 }
3511 NEXT;
3512 cur = CUR;
3513 if (cur == '\\') {
3514 NEXT;
3515 cur = CUR;
3516 switch (cur) {
3517 case 'n': end = 0xA; break;
3518 case 'r': end = 0xD; break;
3519 case 't': end = 0x9; break;
3520 case '\\': case '|': case '.': case '-': case '^': case '?':
3521 case '*': case '+': case '{': case '}': case '(': case ')':
3522 case '[': case ']':
3523 end = cur; break;
3524 default:
3525 ERROR("Invalid escape value");
3526 return;
3527 }
3528 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3529 end = cur;
3530 } else {
3531 ERROR("Expecting the end of a char range");
3532 return;
3533 }
3534 NEXT;
3535 /* TODO check that the values are acceptable character ranges for XML */
3536 if (end < start) {
3537 ERROR("End of range is before start of range");
3538 } else {
3539 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3540 XML_REGEXP_CHARVAL, start, end, NULL);
3541 }
3542 return;
3543}
3544
3545/**
3546 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003547 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003548 *
3549 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3550 */
3551static void
3552xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3553 do {
3554 if ((CUR == '\\') || (CUR == '.')) {
3555 xmlFAParseCharClassEsc(ctxt);
3556 } else {
3557 xmlFAParseCharRange(ctxt);
3558 }
3559 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3560 (ctxt->error == 0));
3561}
3562
3563/**
3564 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003565 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003566 *
3567 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3568 * [15] negCharGroup ::= '^' posCharGroup
3569 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3570 * [12] charClassExpr ::= '[' charGroup ']'
3571 */
3572static void
3573xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3574 int n = ctxt->neg;
3575 while ((CUR != ']') && (ctxt->error == 0)) {
3576 if (CUR == '^') {
3577 int neg = ctxt->neg;
3578
3579 NEXT;
3580 ctxt->neg = !ctxt->neg;
3581 xmlFAParsePosCharGroup(ctxt);
3582 ctxt->neg = neg;
3583 } else if (CUR == '-') {
3584 NEXT;
3585 ctxt->neg = !ctxt->neg;
3586 if (CUR != '[') {
3587 ERROR("charClassExpr: '[' expected");
3588 break;
3589 }
3590 NEXT;
3591 xmlFAParseCharGroup(ctxt);
3592 if (CUR == ']') {
3593 NEXT;
3594 } else {
3595 ERROR("charClassExpr: ']' expected");
3596 break;
3597 }
3598 break;
3599 } else if (CUR != ']') {
3600 xmlFAParsePosCharGroup(ctxt);
3601 }
3602 }
3603 ctxt->neg = n;
3604}
3605
3606/**
3607 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003608 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003609 *
3610 * [11] charClass ::= charClassEsc | charClassExpr
3611 * [12] charClassExpr ::= '[' charGroup ']'
3612 */
3613static void
3614xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3615 if (CUR == '[') {
3616 NEXT;
3617 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3618 if (ctxt->atom == NULL)
3619 return;
3620 xmlFAParseCharGroup(ctxt);
3621 if (CUR == ']') {
3622 NEXT;
3623 } else {
3624 ERROR("xmlFAParseCharClass: ']' expected");
3625 }
3626 } else {
3627 xmlFAParseCharClassEsc(ctxt);
3628 }
3629}
3630
3631/**
3632 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003633 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003634 *
3635 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003636 *
3637 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003638 */
3639static int
3640xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3641 int ret = 0;
3642 int ok = 0;
3643
3644 while ((CUR >= '0') && (CUR <= '9')) {
3645 ret = ret * 10 + (CUR - '0');
3646 ok = 1;
3647 NEXT;
3648 }
3649 if (ok != 1) {
3650 return(-1);
3651 }
3652 return(ret);
3653}
3654
3655/**
3656 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003657 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003658 *
3659 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3660 * [5] quantity ::= quantRange | quantMin | QuantExact
3661 * [6] quantRange ::= QuantExact ',' QuantExact
3662 * [7] quantMin ::= QuantExact ','
3663 * [8] QuantExact ::= [0-9]+
3664 */
3665static int
3666xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3667 int cur;
3668
3669 cur = CUR;
3670 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3671 if (ctxt->atom != NULL) {
3672 if (cur == '?')
3673 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3674 else if (cur == '*')
3675 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3676 else if (cur == '+')
3677 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3678 }
3679 NEXT;
3680 return(1);
3681 }
3682 if (cur == '{') {
3683 int min = 0, max = 0;
3684
3685 NEXT;
3686 cur = xmlFAParseQuantExact(ctxt);
3687 if (cur >= 0)
3688 min = cur;
3689 if (CUR == ',') {
3690 NEXT;
3691 cur = xmlFAParseQuantExact(ctxt);
3692 if (cur >= 0)
3693 max = cur;
3694 }
3695 if (CUR == '}') {
3696 NEXT;
3697 } else {
3698 ERROR("Unterminated quantifier");
3699 }
3700 if (max == 0)
3701 max = min;
3702 if (ctxt->atom != NULL) {
3703 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3704 ctxt->atom->min = min;
3705 ctxt->atom->max = max;
3706 }
3707 return(1);
3708 }
3709 return(0);
3710}
3711
3712/**
3713 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003714 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003715 *
3716 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3717 */
3718static int
3719xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3720 int codepoint, len;
3721
3722 codepoint = xmlFAIsChar(ctxt);
3723 if (codepoint > 0) {
3724 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3725 if (ctxt->atom == NULL)
3726 return(-1);
3727 codepoint = CUR_SCHAR(ctxt->cur, len);
3728 ctxt->atom->codepoint = codepoint;
3729 NEXTL(len);
3730 return(1);
3731 } else if (CUR == '|') {
3732 return(0);
3733 } else if (CUR == 0) {
3734 return(0);
3735 } else if (CUR == ')') {
3736 return(0);
3737 } else if (CUR == '(') {
3738 xmlRegStatePtr start, oldend;
3739
3740 NEXT;
3741 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3742 start = ctxt->state;
3743 oldend = ctxt->end;
3744 ctxt->end = NULL;
3745 ctxt->atom = NULL;
3746 xmlFAParseRegExp(ctxt, 0);
3747 if (CUR == ')') {
3748 NEXT;
3749 } else {
3750 ERROR("xmlFAParseAtom: expecting ')'");
3751 }
3752 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3753 if (ctxt->atom == NULL)
3754 return(-1);
3755 ctxt->atom->start = start;
3756 ctxt->atom->stop = ctxt->state;
3757 ctxt->end = oldend;
3758 return(1);
3759 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3760 xmlFAParseCharClass(ctxt);
3761 return(1);
3762 }
3763 return(0);
3764}
3765
3766/**
3767 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003768 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003769 *
3770 * [3] piece ::= atom quantifier?
3771 */
3772static int
3773xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3774 int ret;
3775
3776 ctxt->atom = NULL;
3777 ret = xmlFAParseAtom(ctxt);
3778 if (ret == 0)
3779 return(0);
3780 if (ctxt->atom == NULL) {
3781 ERROR("internal: no atom generated");
3782 }
3783 xmlFAParseQuantifier(ctxt);
3784 return(1);
3785}
3786
3787/**
3788 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003789 * @ctxt: a regexp parser context
3790 * @first: is taht the first
Daniel Veillard4255d502002-04-16 15:50:10 +00003791 *
3792 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003793 8
Daniel Veillard4255d502002-04-16 15:50:10 +00003794 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003795static int
Daniel Veillard4255d502002-04-16 15:50:10 +00003796xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, int first) {
3797 xmlRegStatePtr previous;
3798 xmlRegAtomPtr prevatom = NULL;
3799 int ret;
3800
3801 previous = ctxt->state;
3802 ret = xmlFAParsePiece(ctxt);
3803 if (ret != 0) {
3804 if (first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003805 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
3806 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003807 previous = ctxt->state;
3808 } else {
3809 prevatom = ctxt->atom;
3810 }
3811 ctxt->atom = NULL;
3812 }
3813 while ((ret != 0) && (ctxt->error == 0)) {
3814 ret = xmlFAParsePiece(ctxt);
3815 if (ret != 0) {
3816 if (first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003817 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3818 ctxt->atom) < 0)
3819 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003820 } else {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003821 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3822 prevatom) < 0)
3823 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003824 prevatom = ctxt->atom;
3825 }
3826 previous = ctxt->state;
3827 ctxt->atom = NULL;
3828 }
3829 }
3830 if (!first) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003831 if (xmlFAGenerateTransitions(ctxt, previous, ctxt->end, prevatom) < 0)
3832 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003833 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003834 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00003835}
3836
3837/**
3838 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003839 * @ctxt: a regexp parser context
3840 * @top: is that the top-level expressions ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003841 *
3842 * [1] regExp ::= branch ( '|' branch )*
3843 */
3844static void
3845xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
3846 xmlRegStatePtr start, end, oldend;
3847
3848 oldend = ctxt->end;
3849
3850 start = ctxt->state;
3851 xmlFAParseBranch(ctxt, (ctxt->end == NULL));
3852 if (CUR != '|') {
3853 ctxt->end = ctxt->state;
3854 return;
3855 }
3856 end = ctxt->state;
3857 while ((CUR == '|') && (ctxt->error == 0)) {
3858 NEXT;
3859 ctxt->state = start;
3860 ctxt->end = end;
3861 xmlFAParseBranch(ctxt, 0);
3862 }
3863 if (!top)
3864 ctxt->end = oldend;
3865}
3866
3867/************************************************************************
3868 * *
3869 * The basic API *
3870 * *
3871 ************************************************************************/
3872
3873/**
3874 * xmlRegexpPrint:
3875 * @output: the file for the output debug
3876 * @regexp: the compiled regexp
3877 *
3878 * Print the content of the compiled regular expression
3879 */
3880void
3881xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3882 int i;
3883
3884 fprintf(output, " regexp: ");
3885 if (regexp == NULL) {
3886 fprintf(output, "NULL\n");
3887 return;
3888 }
3889 fprintf(output, "'%s' ", regexp->string);
3890 fprintf(output, "\n");
3891 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3892 for (i = 0;i < regexp->nbAtoms; i++) {
3893 fprintf(output, " %02d ", i);
3894 xmlRegPrintAtom(output, regexp->atoms[i]);
3895 }
3896 fprintf(output, "%d states:", regexp->nbStates);
3897 fprintf(output, "\n");
3898 for (i = 0;i < regexp->nbStates; i++) {
3899 xmlRegPrintState(output, regexp->states[i]);
3900 }
3901 fprintf(output, "%d counters:\n", regexp->nbCounters);
3902 for (i = 0;i < regexp->nbCounters; i++) {
3903 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
3904 regexp->counters[i].max);
3905 }
3906}
3907
3908/**
3909 * xmlRegexpCompile:
3910 * @regexp: a regular expression string
3911 *
3912 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
3913 * Appendix F and build an automata suitable for testing strings against
3914 * that regular expression
3915 *
3916 * Returns the compiled expression or NULL in case of error
3917 */
3918xmlRegexpPtr
3919xmlRegexpCompile(const xmlChar *regexp) {
3920 xmlRegexpPtr ret;
3921 xmlRegParserCtxtPtr ctxt;
3922
3923 ctxt = xmlRegNewParserCtxt(regexp);
3924 if (ctxt == NULL)
3925 return(NULL);
3926
3927 /* initialize the parser */
3928 ctxt->end = NULL;
3929 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3930 xmlRegStatePush(ctxt, ctxt->start);
3931
3932 /* parse the expression building an automata */
3933 xmlFAParseRegExp(ctxt, 1);
3934 if (CUR != 0) {
3935 ERROR("xmlFAParseRegExp: extra characters");
3936 }
3937 ctxt->end = ctxt->state;
3938 ctxt->start->type = XML_REGEXP_START_STATE;
3939 ctxt->end->type = XML_REGEXP_FINAL_STATE;
3940
3941 /* remove the Epsilon except for counted transitions */
3942 xmlFAEliminateEpsilonTransitions(ctxt);
3943
3944
3945 if (ctxt->error != 0) {
3946 xmlRegFreeParserCtxt(ctxt);
3947 return(NULL);
3948 }
3949 ret = xmlRegEpxFromParse(ctxt);
3950 xmlRegFreeParserCtxt(ctxt);
3951 return(ret);
3952}
3953
3954/**
3955 * xmlRegexpExec:
3956 * @comp: the compiled regular expression
3957 * @content: the value to check against the regular expression
3958 *
3959 * Check if the regular expression generate the value
3960 *
3961 * Returns 1 if it matches, 0 if not and a negativa value in case of error
3962 */
3963int
3964xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
3965 if ((comp == NULL) || (content == NULL))
3966 return(-1);
3967 return(xmlFARegExec(comp, content));
3968}
3969
3970/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003971 * xmlRegexpIsDeterminist:
3972 * @comp: the compiled regular expression
3973 *
3974 * Check if the regular expression is determinist
3975 *
3976 * Returns 1 if it yes, 0 if not and a negativa value in case of error
3977 */
3978int
3979xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
3980 xmlAutomataPtr am;
3981 int ret;
3982
3983 if (comp == NULL)
3984 return(-1);
3985 if (comp->determinist != -1)
3986 return(comp->determinist);
3987
3988 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00003989 if (am->states != NULL) {
3990 int i;
3991
3992 for (i = 0;i < am->nbStates;i++)
3993 xmlRegFreeState(am->states[i]);
3994 xmlFree(am->states);
3995 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003996 am->nbAtoms = comp->nbAtoms;
3997 am->atoms = comp->atoms;
3998 am->nbStates = comp->nbStates;
3999 am->states = comp->states;
4000 am->determinist = -1;
4001 ret = xmlFAComputesDeterminism(am);
4002 am->atoms = NULL;
4003 am->states = NULL;
4004 xmlFreeAutomata(am);
4005 return(ret);
4006}
4007
4008/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004009 * xmlRegFreeRegexp:
4010 * @regexp: the regexp
4011 *
4012 * Free a regexp
4013 */
4014void
4015xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4016 int i;
4017 if (regexp == NULL)
4018 return;
4019
4020 if (regexp->string != NULL)
4021 xmlFree(regexp->string);
4022 if (regexp->states != NULL) {
4023 for (i = 0;i < regexp->nbStates;i++)
4024 xmlRegFreeState(regexp->states[i]);
4025 xmlFree(regexp->states);
4026 }
4027 if (regexp->atoms != NULL) {
4028 for (i = 0;i < regexp->nbAtoms;i++)
4029 xmlRegFreeAtom(regexp->atoms[i]);
4030 xmlFree(regexp->atoms);
4031 }
4032 if (regexp->counters != NULL)
4033 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004034 if (regexp->compact != NULL)
4035 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004036 if (regexp->transdata != NULL)
4037 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004038 if (regexp->stringMap != NULL) {
4039 for (i = 0; i < regexp->nbstrings;i++)
4040 xmlFree(regexp->stringMap[i]);
4041 xmlFree(regexp->stringMap);
4042 }
4043
Daniel Veillard4255d502002-04-16 15:50:10 +00004044 xmlFree(regexp);
4045}
4046
4047#ifdef LIBXML_AUTOMATA_ENABLED
4048/************************************************************************
4049 * *
4050 * The Automata interface *
4051 * *
4052 ************************************************************************/
4053
4054/**
4055 * xmlNewAutomata:
4056 *
4057 * Create a new automata
4058 *
4059 * Returns the new object or NULL in case of failure
4060 */
4061xmlAutomataPtr
4062xmlNewAutomata(void) {
4063 xmlAutomataPtr ctxt;
4064
4065 ctxt = xmlRegNewParserCtxt(NULL);
4066 if (ctxt == NULL)
4067 return(NULL);
4068
4069 /* initialize the parser */
4070 ctxt->end = NULL;
4071 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004072 if (ctxt->start == NULL) {
4073 xmlFreeAutomata(ctxt);
4074 return(NULL);
4075 }
4076 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4077 xmlRegFreeState(ctxt->start);
4078 xmlFreeAutomata(ctxt);
4079 return(NULL);
4080 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004081
4082 return(ctxt);
4083}
4084
4085/**
4086 * xmlFreeAutomata:
4087 * @am: an automata
4088 *
4089 * Free an automata
4090 */
4091void
4092xmlFreeAutomata(xmlAutomataPtr am) {
4093 if (am == NULL)
4094 return;
4095 xmlRegFreeParserCtxt(am);
4096}
4097
4098/**
4099 * xmlAutomataGetInitState:
4100 * @am: an automata
4101 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004102 * Initial state lookup
4103 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004104 * Returns the initial state of the automata
4105 */
4106xmlAutomataStatePtr
4107xmlAutomataGetInitState(xmlAutomataPtr am) {
4108 if (am == NULL)
4109 return(NULL);
4110 return(am->start);
4111}
4112
4113/**
4114 * xmlAutomataSetFinalState:
4115 * @am: an automata
4116 * @state: a state in this automata
4117 *
4118 * Makes that state a final state
4119 *
4120 * Returns 0 or -1 in case of error
4121 */
4122int
4123xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4124 if ((am == NULL) || (state == NULL))
4125 return(-1);
4126 state->type = XML_REGEXP_FINAL_STATE;
4127 return(0);
4128}
4129
4130/**
4131 * xmlAutomataNewTransition:
4132 * @am: an automata
4133 * @from: the starting point of the transition
4134 * @to: the target point of the transition or NULL
4135 * @token: the input string associated to that transition
4136 * @data: data passed to the callback function if the transition is activated
4137 *
4138 * If @to is NULL, this create first a new target state in the automata
4139 * and then adds a transition from the @from state to the target state
4140 * activated by the value of @token
4141 *
4142 * Returns the target state or NULL in case of error
4143 */
4144xmlAutomataStatePtr
4145xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4146 xmlAutomataStatePtr to, const xmlChar *token,
4147 void *data) {
4148 xmlRegAtomPtr atom;
4149
4150 if ((am == NULL) || (from == NULL) || (token == NULL))
4151 return(NULL);
4152 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004153 if (atom == NULL)
4154 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004155 atom->data = data;
4156 if (atom == NULL)
4157 return(NULL);
4158 atom->valuep = xmlStrdup(token);
4159
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004160 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4161 xmlRegFreeAtom(atom);
4162 return(NULL);
4163 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004164 if (to == NULL)
4165 return(am->state);
4166 return(to);
4167}
4168
4169/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004170 * xmlAutomataNewTransition2:
4171 * @am: an automata
4172 * @from: the starting point of the transition
4173 * @to: the target point of the transition or NULL
4174 * @token: the first input string associated to that transition
4175 * @token2: the second input string associated to that transition
4176 * @data: data passed to the callback function if the transition is activated
4177 *
4178 * If @to is NULL, this create first a new target state in the automata
4179 * and then adds a transition from the @from state to the target state
4180 * activated by the value of @token
4181 *
4182 * Returns the target state or NULL in case of error
4183 */
4184xmlAutomataStatePtr
4185xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4186 xmlAutomataStatePtr to, const xmlChar *token,
4187 const xmlChar *token2, void *data) {
4188 xmlRegAtomPtr atom;
4189
4190 if ((am == NULL) || (from == NULL) || (token == NULL))
4191 return(NULL);
4192 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4193 atom->data = data;
4194 if (atom == NULL)
4195 return(NULL);
4196 if ((token2 == NULL) || (*token2 == 0)) {
4197 atom->valuep = xmlStrdup(token);
4198 } else {
4199 int lenn, lenp;
4200 xmlChar *str;
4201
4202 lenn = strlen((char *) token2);
4203 lenp = strlen((char *) token);
4204
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004205 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004206 if (str == NULL) {
4207 xmlRegFreeAtom(atom);
4208 return(NULL);
4209 }
4210 memcpy(&str[0], token, lenp);
4211 str[lenp] = '|';
4212 memcpy(&str[lenp + 1], token2, lenn);
4213 str[lenn + lenp + 1] = 0;
4214
4215 atom->valuep = str;
4216 }
4217
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004218 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4219 xmlRegFreeAtom(atom);
4220 return(NULL);
4221 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004222 if (to == NULL)
4223 return(am->state);
4224 return(to);
4225}
4226
4227/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004228 * xmlAutomataNewCountTrans:
4229 * @am: an automata
4230 * @from: the starting point of the transition
4231 * @to: the target point of the transition or NULL
4232 * @token: the input string associated to that transition
4233 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004234 * @max: the maximum successive occurences of token
4235 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004236 *
4237 * If @to is NULL, this create first a new target state in the automata
4238 * and then adds a transition from the @from state to the target state
4239 * activated by a succession of input of value @token and whose number
4240 * is between @min and @max
4241 *
4242 * Returns the target state or NULL in case of error
4243 */
4244xmlAutomataStatePtr
4245xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4246 xmlAutomataStatePtr to, const xmlChar *token,
4247 int min, int max, void *data) {
4248 xmlRegAtomPtr atom;
4249
4250 if ((am == NULL) || (from == NULL) || (token == NULL))
4251 return(NULL);
4252 if (min < 0)
4253 return(NULL);
4254 if ((max < min) || (max < 1))
4255 return(NULL);
4256 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4257 if (atom == NULL)
4258 return(NULL);
4259 atom->valuep = xmlStrdup(token);
4260 atom->data = data;
4261 if (min == 0)
4262 atom->min = 1;
4263 else
4264 atom->min = min;
4265 atom->max = max;
4266
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004267 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4268 xmlRegFreeAtom(atom);
4269 return(NULL);
4270 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004271 if (to == NULL)
4272 to = am->state;
4273 if (to == NULL)
4274 return(NULL);
4275 if (min == 0)
4276 xmlFAGenerateEpsilonTransition(am, from, to);
4277 return(to);
4278}
4279
4280/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004281 * xmlAutomataNewOnceTrans:
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 Veillard7646b182002-04-20 06:41:40 +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, moreover that transistion can only be crossed
4294 * once.
4295 *
4296 * Returns the target state or NULL in case of error
4297 */
4298xmlAutomataStatePtr
4299xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4300 xmlAutomataStatePtr to, const xmlChar *token,
4301 int min, int max, void *data) {
4302 xmlRegAtomPtr atom;
4303 int counter;
4304
4305 if ((am == NULL) || (from == NULL) || (token == NULL))
4306 return(NULL);
4307 if (min < 1)
4308 return(NULL);
4309 if ((max < min) || (max < 1))
4310 return(NULL);
4311 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4312 if (atom == NULL)
4313 return(NULL);
4314 atom->valuep = xmlStrdup(token);
4315 atom->data = data;
4316 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4317 if (min == 0)
4318 atom->min = 1;
4319 else
4320 atom->min = min;
4321 atom->max = max;
4322 /*
4323 * associate a counter to the transition.
4324 */
4325 counter = xmlRegGetCounter(am);
4326 am->counters[counter].min = 1;
4327 am->counters[counter].max = 1;
4328
4329 /* xmlFAGenerateTransitions(am, from, to, atom); */
4330 if (to == NULL) {
4331 to = xmlRegNewState(am);
4332 xmlRegStatePush(am, to);
4333 }
4334 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4335 xmlRegAtomPush(am, atom);
4336 am->state = to;
4337 if (to == NULL)
4338 to = am->state;
4339 if (to == NULL)
4340 return(NULL);
4341 return(to);
4342}
4343
4344/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004345 * xmlAutomataNewState:
4346 * @am: an automata
4347 *
4348 * Create a new disconnected state in the automata
4349 *
4350 * Returns the new state or NULL in case of error
4351 */
4352xmlAutomataStatePtr
4353xmlAutomataNewState(xmlAutomataPtr am) {
4354 xmlAutomataStatePtr to;
4355
4356 if (am == NULL)
4357 return(NULL);
4358 to = xmlRegNewState(am);
4359 xmlRegStatePush(am, to);
4360 return(to);
4361}
4362
4363/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004364 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004365 * @am: an automata
4366 * @from: the starting point of the transition
4367 * @to: the target point of the transition or NULL
4368 *
4369 * If @to is NULL, this create first a new target state in the automata
4370 * and then adds a an epsilon transition from the @from state to the
4371 * target state
4372 *
4373 * Returns the target state or NULL in case of error
4374 */
4375xmlAutomataStatePtr
4376xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4377 xmlAutomataStatePtr to) {
4378 if ((am == NULL) || (from == NULL))
4379 return(NULL);
4380 xmlFAGenerateEpsilonTransition(am, from, to);
4381 if (to == NULL)
4382 return(am->state);
4383 return(to);
4384}
4385
Daniel Veillardb509f152002-04-17 16:28:10 +00004386/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004387 * xmlAutomataNewAllTrans:
4388 * @am: an automata
4389 * @from: the starting point of the transition
4390 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004391 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004392 *
4393 * If @to is NULL, this create first a new target state in the automata
4394 * and then adds a an ALL transition from the @from state to the
4395 * target state. That transition is an epsilon transition allowed only when
4396 * all transitions from the @from node have been activated.
4397 *
4398 * Returns the target state or NULL in case of error
4399 */
4400xmlAutomataStatePtr
4401xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004402 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004403 if ((am == NULL) || (from == NULL))
4404 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004405 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004406 if (to == NULL)
4407 return(am->state);
4408 return(to);
4409}
4410
4411/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004412 * xmlAutomataNewCounter:
4413 * @am: an automata
4414 * @min: the minimal value on the counter
4415 * @max: the maximal value on the counter
4416 *
4417 * Create a new counter
4418 *
4419 * Returns the counter number or -1 in case of error
4420 */
4421int
4422xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4423 int ret;
4424
4425 if (am == NULL)
4426 return(-1);
4427
4428 ret = xmlRegGetCounter(am);
4429 if (ret < 0)
4430 return(-1);
4431 am->counters[ret].min = min;
4432 am->counters[ret].max = max;
4433 return(ret);
4434}
4435
4436/**
4437 * xmlAutomataNewCountedTrans:
4438 * @am: an automata
4439 * @from: the starting point of the transition
4440 * @to: the target point of the transition or NULL
4441 * @counter: the counter associated to that transition
4442 *
4443 * If @to is NULL, this create first a new target state in the automata
4444 * and then adds an epsilon transition from the @from state to the target state
4445 * which will increment the counter provided
4446 *
4447 * Returns the target state or NULL in case of error
4448 */
4449xmlAutomataStatePtr
4450xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4451 xmlAutomataStatePtr to, int counter) {
4452 if ((am == NULL) || (from == NULL) || (counter < 0))
4453 return(NULL);
4454 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4455 if (to == NULL)
4456 return(am->state);
4457 return(to);
4458}
4459
4460/**
4461 * xmlAutomataNewCounterTrans:
4462 * @am: an automata
4463 * @from: the starting point of the transition
4464 * @to: the target point of the transition or NULL
4465 * @counter: the counter associated to that transition
4466 *
4467 * If @to is NULL, this create first a new target state in the automata
4468 * and then adds an epsilon transition from the @from state to the target state
4469 * which will be allowed only if the counter is within the right range.
4470 *
4471 * Returns the target state or NULL in case of error
4472 */
4473xmlAutomataStatePtr
4474xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4475 xmlAutomataStatePtr to, int counter) {
4476 if ((am == NULL) || (from == NULL) || (counter < 0))
4477 return(NULL);
4478 xmlFAGenerateCountedTransition(am, from, to, counter);
4479 if (to == NULL)
4480 return(am->state);
4481 return(to);
4482}
Daniel Veillard4255d502002-04-16 15:50:10 +00004483
4484/**
4485 * xmlAutomataCompile:
4486 * @am: an automata
4487 *
4488 * Compile the automata into a Reg Exp ready for being executed.
4489 * The automata should be free after this point.
4490 *
4491 * Returns the compiled regexp or NULL in case of error
4492 */
4493xmlRegexpPtr
4494xmlAutomataCompile(xmlAutomataPtr am) {
4495 xmlRegexpPtr ret;
4496
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004497 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004498 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004499 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004500 ret = xmlRegEpxFromParse(am);
4501
4502 return(ret);
4503}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004504
4505/**
4506 * xmlAutomataIsDeterminist:
4507 * @am: an automata
4508 *
4509 * Checks if an automata is determinist.
4510 *
4511 * Returns 1 if true, 0 if not, and -1 in case of error
4512 */
4513int
4514xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4515 int ret;
4516
4517 if (am == NULL)
4518 return(-1);
4519
4520 ret = xmlFAComputesDeterminism(am);
4521 return(ret);
4522}
Daniel Veillard4255d502002-04-16 15:50:10 +00004523#endif /* LIBXML_AUTOMATA_ENABLED */
4524#endif /* LIBXML_REGEXP_ENABLED */