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