blob: 627da51e24ef88726ef9a2b5b269a5173ba24aad [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
2338 for (i = 0;i < exec->inputStackNr;i++)
2339 xmlFree(exec->inputStack[i].value);
2340 xmlFree(exec->inputStack);
2341 }
2342 xmlFree(exec);
2343}
2344
2345static void
2346xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2347 void *data) {
2348#ifdef DEBUG_PUSH
2349 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2350#endif
2351 if (exec->inputStackMax == 0) {
2352 exec->inputStackMax = 4;
2353 exec->inputStack = (xmlRegInputTokenPtr)
2354 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2355 if (exec->inputStack == NULL) {
2356 fprintf(stderr, "push input: allocation failed");
2357 exec->inputStackMax = 0;
2358 return;
2359 }
2360 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2361 xmlRegInputTokenPtr tmp;
2362
2363 exec->inputStackMax *= 2;
2364 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2365 exec->inputStackMax * sizeof(xmlRegInputToken));
2366 if (tmp == NULL) {
2367 fprintf(stderr, "push input: allocation failed");
2368 exec->inputStackMax /= 2;
2369 return;
2370 }
2371 exec->inputStack = tmp;
2372 }
2373 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2374 exec->inputStack[exec->inputStackNr].data = data;
2375 exec->inputStackNr++;
2376 exec->inputStack[exec->inputStackNr].value = NULL;
2377 exec->inputStack[exec->inputStackNr].data = NULL;
2378}
2379
2380
2381/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002382 * xmlRegCompactPushString:
2383 * @exec: a regexp execution context
2384 * @comp: the precompiled exec with a compact table
2385 * @value: a string token input
2386 * @data: data associated to the token to reuse in callbacks
2387 *
2388 * Push one input token in the execution context
2389 *
2390 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2391 * a negative value in case of error.
2392 */
2393static int
2394xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2395 xmlRegexpPtr comp,
2396 const xmlChar *value,
2397 void *data) {
2398 int state = exec->index;
2399 int i, target;
2400
2401 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2402 return(-1);
2403
2404 if (value == NULL) {
2405 /*
2406 * are we at a final state ?
2407 */
2408 if (comp->compact[state * (comp->nbstrings + 1)] ==
2409 XML_REGEXP_FINAL_STATE)
2410 return(1);
2411 return(0);
2412 }
2413
2414#ifdef DEBUG_PUSH
2415 printf("value pushed: %s\n", value);
2416#endif
2417
2418 /*
2419 * Examine all outside transition from current state
2420 */
2421 for (i = 0;i < comp->nbstrings;i++) {
2422 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2423 if ((target > 0) && (target <= comp->nbstates)) {
2424 target--; /* to avoid 0 */
2425 if (xmlStrEqual(comp->stringMap[i], value)) {
2426 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002427 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2428 exec->callback(exec->data, value,
2429 comp->transdata[state * comp->nbstrings + i], data);
2430 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002431#ifdef DEBUG_PUSH
2432 printf("entering state %d\n", target);
2433#endif
2434 if (comp->compact[target * (comp->nbstrings + 1)] ==
2435 XML_REGEXP_FINAL_STATE)
2436 return(1);
2437 return(0);
2438 }
2439 }
2440 }
2441 /*
2442 * Failed to find an exit transition out from current state for the
2443 * current token
2444 */
2445#ifdef DEBUG_PUSH
2446 printf("failed to find a transition for %s on state %d\n", value, state);
2447#endif
2448 exec->status = -1;
2449 return(-1);
2450}
2451
2452/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002453 * xmlRegExecPushString:
2454 * @exec: a regexp execution context
2455 * @value: a string token input
2456 * @data: data associated to the token to reuse in callbacks
2457 *
2458 * Push one input token in the execution context
2459 *
2460 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2461 * a negative value in case of error.
2462 */
2463int
2464xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2465 void *data) {
2466 xmlRegTransPtr trans;
2467 xmlRegAtomPtr atom;
2468 int ret;
2469 int final = 0;
2470
2471 if (exec == NULL)
2472 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002473 if (exec->comp == NULL)
2474 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002475 if (exec->status != 0)
2476 return(exec->status);
2477
Daniel Veillard23e73572002-09-19 19:56:43 +00002478 if (exec->comp->compact != NULL)
2479 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2480
Daniel Veillard4255d502002-04-16 15:50:10 +00002481 if (value == NULL) {
2482 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2483 return(1);
2484 final = 1;
2485 }
2486
2487#ifdef DEBUG_PUSH
2488 printf("value pushed: %s\n", value);
2489#endif
2490 /*
2491 * If we have an active rollback stack push the new value there
2492 * and get back to where we were left
2493 */
2494 if ((value != NULL) && (exec->inputStackNr > 0)) {
2495 xmlFARegExecSaveInputString(exec, value, data);
2496 value = exec->inputStack[exec->index].value;
2497 data = exec->inputStack[exec->index].data;
2498#ifdef DEBUG_PUSH
2499 printf("value loaded: %s\n", value);
2500#endif
2501 }
2502
2503 while ((exec->status == 0) &&
2504 ((value != NULL) ||
2505 ((final == 1) &&
2506 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2507
2508 /*
2509 * End of input on non-terminal state, rollback, however we may
2510 * still have epsilon like transition for counted transitions
2511 * on counters, in that case don't break too early.
2512 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002513 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002514 goto rollback;
2515
2516 exec->transcount = 0;
2517 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2518 trans = &exec->state->trans[exec->transno];
2519 if (trans->to < 0)
2520 continue;
2521 atom = trans->atom;
2522 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002523 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2524 int i;
2525 int count;
2526 xmlRegTransPtr t;
2527 xmlRegCounterPtr counter;
2528
2529 ret = 0;
2530
2531#ifdef DEBUG_PUSH
2532 printf("testing all lax %d\n", trans->count);
2533#endif
2534 /*
2535 * Check all counted transitions from the current state
2536 */
2537 if ((value == NULL) && (final)) {
2538 ret = 1;
2539 } else if (value != NULL) {
2540 for (i = 0;i < exec->state->nbTrans;i++) {
2541 t = &exec->state->trans[i];
2542 if ((t->counter < 0) || (t == trans))
2543 continue;
2544 counter = &exec->comp->counters[t->counter];
2545 count = exec->counts[t->counter];
2546 if ((count < counter->max) &&
2547 (t->atom != NULL) &&
2548 (xmlStrEqual(value, t->atom->valuep))) {
2549 ret = 0;
2550 break;
2551 }
2552 if ((count >= counter->min) &&
2553 (count < counter->max) &&
2554 (xmlStrEqual(value, t->atom->valuep))) {
2555 ret = 1;
2556 break;
2557 }
2558 }
2559 }
2560 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002561 int i;
2562 int count;
2563 xmlRegTransPtr t;
2564 xmlRegCounterPtr counter;
2565
2566 ret = 1;
2567
2568#ifdef DEBUG_PUSH
2569 printf("testing all %d\n", trans->count);
2570#endif
2571 /*
2572 * Check all counted transitions from the current state
2573 */
2574 for (i = 0;i < exec->state->nbTrans;i++) {
2575 t = &exec->state->trans[i];
2576 if ((t->counter < 0) || (t == trans))
2577 continue;
2578 counter = &exec->comp->counters[t->counter];
2579 count = exec->counts[t->counter];
2580 if ((count < counter->min) || (count > counter->max)) {
2581 ret = 0;
2582 break;
2583 }
2584 }
2585 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002586 int count;
2587 xmlRegCounterPtr counter;
2588
2589 /*
2590 * A counted transition.
2591 */
2592
2593 count = exec->counts[trans->count];
2594 counter = &exec->comp->counters[trans->count];
2595#ifdef DEBUG_PUSH
2596 printf("testing count %d: val %d, min %d, max %d\n",
2597 trans->count, count, counter->min, counter->max);
2598#endif
2599 ret = ((count >= counter->min) && (count <= counter->max));
2600 } else if (atom == NULL) {
2601 fprintf(stderr, "epsilon transition left at runtime\n");
2602 exec->status = -2;
2603 break;
2604 } else if (value != NULL) {
2605 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002606 if ((ret == 1) && (trans->counter >= 0)) {
2607 xmlRegCounterPtr counter;
2608 int count;
2609
2610 count = exec->counts[trans->counter];
2611 counter = &exec->comp->counters[trans->counter];
2612 if (count >= counter->max)
2613 ret = 0;
2614 }
2615
Daniel Veillard4255d502002-04-16 15:50:10 +00002616 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2617 xmlRegStatePtr to = exec->comp->states[trans->to];
2618
2619 /*
2620 * this is a multiple input sequence
2621 */
2622 if (exec->state->nbTrans > exec->transno + 1) {
2623 if (exec->inputStackNr <= 0) {
2624 xmlFARegExecSaveInputString(exec, value, data);
2625 }
2626 xmlFARegExecSave(exec);
2627 }
2628 exec->transcount = 1;
2629 do {
2630 /*
2631 * Try to progress as much as possible on the input
2632 */
2633 if (exec->transcount == atom->max) {
2634 break;
2635 }
2636 exec->index++;
2637 value = exec->inputStack[exec->index].value;
2638 data = exec->inputStack[exec->index].data;
2639#ifdef DEBUG_PUSH
2640 printf("value loaded: %s\n", value);
2641#endif
2642
2643 /*
2644 * End of input: stop here
2645 */
2646 if (value == NULL) {
2647 exec->index --;
2648 break;
2649 }
2650 if (exec->transcount >= atom->min) {
2651 int transno = exec->transno;
2652 xmlRegStatePtr state = exec->state;
2653
2654 /*
2655 * The transition is acceptable save it
2656 */
2657 exec->transno = -1; /* trick */
2658 exec->state = to;
2659 if (exec->inputStackNr <= 0) {
2660 xmlFARegExecSaveInputString(exec, value, data);
2661 }
2662 xmlFARegExecSave(exec);
2663 exec->transno = transno;
2664 exec->state = state;
2665 }
2666 ret = xmlStrEqual(value, atom->valuep);
2667 exec->transcount++;
2668 } while (ret == 1);
2669 if (exec->transcount < atom->min)
2670 ret = 0;
2671
2672 /*
2673 * If the last check failed but one transition was found
2674 * possible, rollback
2675 */
2676 if (ret < 0)
2677 ret = 0;
2678 if (ret == 0) {
2679 goto rollback;
2680 }
2681 }
2682 }
2683 if (ret == 1) {
2684 if ((exec->callback != NULL) && (atom != NULL)) {
2685 exec->callback(exec->data, atom->valuep,
2686 atom->data, data);
2687 }
2688 if (exec->state->nbTrans > exec->transno + 1) {
2689 if (exec->inputStackNr <= 0) {
2690 xmlFARegExecSaveInputString(exec, value, data);
2691 }
2692 xmlFARegExecSave(exec);
2693 }
2694 if (trans->counter >= 0) {
2695#ifdef DEBUG_PUSH
2696 printf("Increasing count %d\n", trans->counter);
2697#endif
2698 exec->counts[trans->counter]++;
2699 }
2700#ifdef DEBUG_PUSH
2701 printf("entering state %d\n", trans->to);
2702#endif
2703 exec->state = exec->comp->states[trans->to];
2704 exec->transno = 0;
2705 if (trans->atom != NULL) {
2706 if (exec->inputStack != NULL) {
2707 exec->index++;
2708 if (exec->index < exec->inputStackNr) {
2709 value = exec->inputStack[exec->index].value;
2710 data = exec->inputStack[exec->index].data;
2711#ifdef DEBUG_PUSH
2712 printf("value loaded: %s\n", value);
2713#endif
2714 } else {
2715 value = NULL;
2716 data = NULL;
2717#ifdef DEBUG_PUSH
2718 printf("end of input\n");
2719#endif
2720 }
2721 } else {
2722 value = NULL;
2723 data = NULL;
2724#ifdef DEBUG_PUSH
2725 printf("end of input\n");
2726#endif
2727 }
2728 }
2729 goto progress;
2730 } else if (ret < 0) {
2731 exec->status = -4;
2732 break;
2733 }
2734 }
2735 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2736rollback:
2737 /*
2738 * Failed to find a way out
2739 */
2740 exec->determinist = 0;
2741 xmlFARegExecRollBack(exec);
2742 if (exec->status == 0) {
2743 value = exec->inputStack[exec->index].value;
2744 data = exec->inputStack[exec->index].data;
2745#ifdef DEBUG_PUSH
2746 printf("value loaded: %s\n", value);
2747#endif
2748 }
2749 }
2750progress:
2751 continue;
2752 }
2753 if (exec->status == 0) {
2754 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2755 }
2756 return(exec->status);
2757}
2758
2759#if 0
2760static int
2761xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2762 xmlRegTransPtr trans;
2763 xmlRegAtomPtr atom;
2764 int ret;
2765 int codepoint, len;
2766
2767 if (exec == NULL)
2768 return(-1);
2769 if (exec->status != 0)
2770 return(exec->status);
2771
2772 while ((exec->status == 0) &&
2773 ((exec->inputString[exec->index] != 0) ||
2774 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2775
2776 /*
2777 * End of input on non-terminal state, rollback, however we may
2778 * still have epsilon like transition for counted transitions
2779 * on counters, in that case don't break too early.
2780 */
2781 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2782 goto rollback;
2783
2784 exec->transcount = 0;
2785 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2786 trans = &exec->state->trans[exec->transno];
2787 if (trans->to < 0)
2788 continue;
2789 atom = trans->atom;
2790 ret = 0;
2791 if (trans->count >= 0) {
2792 int count;
2793 xmlRegCounterPtr counter;
2794
2795 /*
2796 * A counted transition.
2797 */
2798
2799 count = exec->counts[trans->count];
2800 counter = &exec->comp->counters[trans->count];
2801#ifdef DEBUG_REGEXP_EXEC
2802 printf("testing count %d: val %d, min %d, max %d\n",
2803 trans->count, count, counter->min, counter->max);
2804#endif
2805 ret = ((count >= counter->min) && (count <= counter->max));
2806 } else if (atom == NULL) {
2807 fprintf(stderr, "epsilon transition left at runtime\n");
2808 exec->status = -2;
2809 break;
2810 } else if (exec->inputString[exec->index] != 0) {
2811 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2812 ret = xmlRegCheckCharacter(atom, codepoint);
2813 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2814 xmlRegStatePtr to = exec->comp->states[trans->to];
2815
2816 /*
2817 * this is a multiple input sequence
2818 */
2819 if (exec->state->nbTrans > exec->transno + 1) {
2820 xmlFARegExecSave(exec);
2821 }
2822 exec->transcount = 1;
2823 do {
2824 /*
2825 * Try to progress as much as possible on the input
2826 */
2827 if (exec->transcount == atom->max) {
2828 break;
2829 }
2830 exec->index += len;
2831 /*
2832 * End of input: stop here
2833 */
2834 if (exec->inputString[exec->index] == 0) {
2835 exec->index -= len;
2836 break;
2837 }
2838 if (exec->transcount >= atom->min) {
2839 int transno = exec->transno;
2840 xmlRegStatePtr state = exec->state;
2841
2842 /*
2843 * The transition is acceptable save it
2844 */
2845 exec->transno = -1; /* trick */
2846 exec->state = to;
2847 xmlFARegExecSave(exec);
2848 exec->transno = transno;
2849 exec->state = state;
2850 }
2851 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2852 len);
2853 ret = xmlRegCheckCharacter(atom, codepoint);
2854 exec->transcount++;
2855 } while (ret == 1);
2856 if (exec->transcount < atom->min)
2857 ret = 0;
2858
2859 /*
2860 * If the last check failed but one transition was found
2861 * possible, rollback
2862 */
2863 if (ret < 0)
2864 ret = 0;
2865 if (ret == 0) {
2866 goto rollback;
2867 }
2868 }
2869 }
2870 if (ret == 1) {
2871 if (exec->state->nbTrans > exec->transno + 1) {
2872 xmlFARegExecSave(exec);
2873 }
2874 if (trans->counter >= 0) {
2875#ifdef DEBUG_REGEXP_EXEC
2876 printf("Increasing count %d\n", trans->counter);
2877#endif
2878 exec->counts[trans->counter]++;
2879 }
2880#ifdef DEBUG_REGEXP_EXEC
2881 printf("entering state %d\n", trans->to);
2882#endif
2883 exec->state = exec->comp->states[trans->to];
2884 exec->transno = 0;
2885 if (trans->atom != NULL) {
2886 exec->index += len;
2887 }
2888 goto progress;
2889 } else if (ret < 0) {
2890 exec->status = -4;
2891 break;
2892 }
2893 }
2894 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2895rollback:
2896 /*
2897 * Failed to find a way out
2898 */
2899 exec->determinist = 0;
2900 xmlFARegExecRollBack(exec);
2901 }
2902progress:
2903 continue;
2904 }
2905}
2906#endif
2907/************************************************************************
2908 * *
2909 * Parser for the Shemas Datatype Regular Expressions *
2910 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
2911 * *
2912 ************************************************************************/
2913
2914/**
2915 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00002916 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002917 *
2918 * [10] Char ::= [^.\?*+()|#x5B#x5D]
2919 */
2920static int
2921xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
2922 int cur;
2923 int len;
2924
2925 cur = CUR_SCHAR(ctxt->cur, len);
2926 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
2927 (cur == '*') || (cur == '+') || (cur == '(') ||
2928 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
2929 (cur == 0x5D) || (cur == 0))
2930 return(-1);
2931 return(cur);
2932}
2933
2934/**
2935 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00002936 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002937 *
2938 * [27] charProp ::= IsCategory | IsBlock
2939 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
2940 * Separators | Symbols | Others
2941 * [29] Letters ::= 'L' [ultmo]?
2942 * [30] Marks ::= 'M' [nce]?
2943 * [31] Numbers ::= 'N' [dlo]?
2944 * [32] Punctuation ::= 'P' [cdseifo]?
2945 * [33] Separators ::= 'Z' [slp]?
2946 * [34] Symbols ::= 'S' [mcko]?
2947 * [35] Others ::= 'C' [cfon]?
2948 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
2949 */
2950static void
2951xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
2952 int cur;
2953 xmlRegAtomType type = 0;
2954 xmlChar *blockName = NULL;
2955
2956 cur = CUR;
2957 if (cur == 'L') {
2958 NEXT;
2959 cur = CUR;
2960 if (cur == 'u') {
2961 NEXT;
2962 type = XML_REGEXP_LETTER_UPPERCASE;
2963 } else if (cur == 'l') {
2964 NEXT;
2965 type = XML_REGEXP_LETTER_LOWERCASE;
2966 } else if (cur == 't') {
2967 NEXT;
2968 type = XML_REGEXP_LETTER_TITLECASE;
2969 } else if (cur == 'm') {
2970 NEXT;
2971 type = XML_REGEXP_LETTER_MODIFIER;
2972 } else if (cur == 'o') {
2973 NEXT;
2974 type = XML_REGEXP_LETTER_OTHERS;
2975 } else {
2976 type = XML_REGEXP_LETTER;
2977 }
2978 } else if (cur == 'M') {
2979 NEXT;
2980 cur = CUR;
2981 if (cur == 'n') {
2982 NEXT;
2983 /* nonspacing */
2984 type = XML_REGEXP_MARK_NONSPACING;
2985 } else if (cur == 'c') {
2986 NEXT;
2987 /* spacing combining */
2988 type = XML_REGEXP_MARK_SPACECOMBINING;
2989 } else if (cur == 'e') {
2990 NEXT;
2991 /* enclosing */
2992 type = XML_REGEXP_MARK_ENCLOSING;
2993 } else {
2994 /* all marks */
2995 type = XML_REGEXP_MARK;
2996 }
2997 } else if (cur == 'N') {
2998 NEXT;
2999 cur = CUR;
3000 if (cur == 'd') {
3001 NEXT;
3002 /* digital */
3003 type = XML_REGEXP_NUMBER_DECIMAL;
3004 } else if (cur == 'l') {
3005 NEXT;
3006 /* letter */
3007 type = XML_REGEXP_NUMBER_LETTER;
3008 } else if (cur == 'o') {
3009 NEXT;
3010 /* other */
3011 type = XML_REGEXP_NUMBER_OTHERS;
3012 } else {
3013 /* all numbers */
3014 type = XML_REGEXP_NUMBER;
3015 }
3016 } else if (cur == 'P') {
3017 NEXT;
3018 cur = CUR;
3019 if (cur == 'c') {
3020 NEXT;
3021 /* connector */
3022 type = XML_REGEXP_PUNCT_CONNECTOR;
3023 } else if (cur == 'd') {
3024 NEXT;
3025 /* dash */
3026 type = XML_REGEXP_PUNCT_DASH;
3027 } else if (cur == 's') {
3028 NEXT;
3029 /* open */
3030 type = XML_REGEXP_PUNCT_OPEN;
3031 } else if (cur == 'e') {
3032 NEXT;
3033 /* close */
3034 type = XML_REGEXP_PUNCT_CLOSE;
3035 } else if (cur == 'i') {
3036 NEXT;
3037 /* initial quote */
3038 type = XML_REGEXP_PUNCT_INITQUOTE;
3039 } else if (cur == 'f') {
3040 NEXT;
3041 /* final quote */
3042 type = XML_REGEXP_PUNCT_FINQUOTE;
3043 } else if (cur == 'o') {
3044 NEXT;
3045 /* other */
3046 type = XML_REGEXP_PUNCT_OTHERS;
3047 } else {
3048 /* all punctuation */
3049 type = XML_REGEXP_PUNCT;
3050 }
3051 } else if (cur == 'Z') {
3052 NEXT;
3053 cur = CUR;
3054 if (cur == 's') {
3055 NEXT;
3056 /* space */
3057 type = XML_REGEXP_SEPAR_SPACE;
3058 } else if (cur == 'l') {
3059 NEXT;
3060 /* line */
3061 type = XML_REGEXP_SEPAR_LINE;
3062 } else if (cur == 'p') {
3063 NEXT;
3064 /* paragraph */
3065 type = XML_REGEXP_SEPAR_PARA;
3066 } else {
3067 /* all separators */
3068 type = XML_REGEXP_SEPAR;
3069 }
3070 } else if (cur == 'S') {
3071 NEXT;
3072 cur = CUR;
3073 if (cur == 'm') {
3074 NEXT;
3075 type = XML_REGEXP_SYMBOL_MATH;
3076 /* math */
3077 } else if (cur == 'c') {
3078 NEXT;
3079 type = XML_REGEXP_SYMBOL_CURRENCY;
3080 /* currency */
3081 } else if (cur == 'k') {
3082 NEXT;
3083 type = XML_REGEXP_SYMBOL_MODIFIER;
3084 /* modifiers */
3085 } else if (cur == 'o') {
3086 NEXT;
3087 type = XML_REGEXP_SYMBOL_OTHERS;
3088 /* other */
3089 } else {
3090 /* all symbols */
3091 type = XML_REGEXP_SYMBOL;
3092 }
3093 } else if (cur == 'C') {
3094 NEXT;
3095 cur = CUR;
3096 if (cur == 'c') {
3097 NEXT;
3098 /* control */
3099 type = XML_REGEXP_OTHER_CONTROL;
3100 } else if (cur == 'f') {
3101 NEXT;
3102 /* format */
3103 type = XML_REGEXP_OTHER_FORMAT;
3104 } else if (cur == 'o') {
3105 NEXT;
3106 /* private use */
3107 type = XML_REGEXP_OTHER_PRIVATE;
3108 } else if (cur == 'n') {
3109 NEXT;
3110 /* not assigned */
3111 type = XML_REGEXP_OTHER_NA;
3112 } else {
3113 /* all others */
3114 type = XML_REGEXP_OTHER;
3115 }
3116 } else if (cur == 'I') {
3117 const xmlChar *start;
3118 NEXT;
3119 cur = CUR;
3120 if (cur != 's') {
3121 ERROR("IsXXXX expected");
3122 return;
3123 }
3124 NEXT;
3125 start = ctxt->cur;
3126 cur = CUR;
3127 if (((cur >= 'a') && (cur <= 'z')) ||
3128 ((cur >= 'A') && (cur <= 'Z')) ||
3129 ((cur >= '0') && (cur <= '9')) ||
3130 (cur == 0x2D)) {
3131 NEXT;
3132 cur = CUR;
3133 while (((cur >= 'a') && (cur <= 'z')) ||
3134 ((cur >= 'A') && (cur <= 'Z')) ||
3135 ((cur >= '0') && (cur <= '9')) ||
3136 (cur == 0x2D)) {
3137 NEXT;
3138 cur = CUR;
3139 }
3140 }
3141 type = XML_REGEXP_BLOCK_NAME;
3142 blockName = xmlStrndup(start, ctxt->cur - start);
3143 } else {
3144 ERROR("Unknown char property");
3145 return;
3146 }
3147 if (ctxt->atom == NULL) {
3148 ctxt->atom = xmlRegNewAtom(ctxt, type);
3149 if (ctxt->atom != NULL)
3150 ctxt->atom->valuep = blockName;
3151 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3152 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3153 type, 0, 0, blockName);
3154 }
3155}
3156
3157/**
3158 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003159 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003160 *
3161 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3162 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3163 * [25] catEsc ::= '\p{' charProp '}'
3164 * [26] complEsc ::= '\P{' charProp '}'
3165 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3166 */
3167static void
3168xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3169 int cur;
3170
3171 if (CUR == '.') {
3172 if (ctxt->atom == NULL) {
3173 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3174 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3175 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3176 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3177 }
3178 NEXT;
3179 return;
3180 }
3181 if (CUR != '\\') {
3182 ERROR("Escaped sequence: expecting \\");
3183 return;
3184 }
3185 NEXT;
3186 cur = CUR;
3187 if (cur == 'p') {
3188 NEXT;
3189 if (CUR != '{') {
3190 ERROR("Expecting '{'");
3191 return;
3192 }
3193 NEXT;
3194 xmlFAParseCharProp(ctxt);
3195 if (CUR != '}') {
3196 ERROR("Expecting '}'");
3197 return;
3198 }
3199 NEXT;
3200 } else if (cur == 'P') {
3201 NEXT;
3202 if (CUR != '{') {
3203 ERROR("Expecting '{'");
3204 return;
3205 }
3206 NEXT;
3207 xmlFAParseCharProp(ctxt);
3208 ctxt->atom->neg = 1;
3209 if (CUR != '}') {
3210 ERROR("Expecting '}'");
3211 return;
3212 }
3213 NEXT;
3214 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3215 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3216 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3217 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3218 (cur == 0x5E)) {
3219 if (ctxt->atom == NULL) {
3220 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3221 if (ctxt->atom != NULL)
3222 ctxt->atom->codepoint = cur;
3223 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3224 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3225 XML_REGEXP_CHARVAL, cur, cur, NULL);
3226 }
3227 NEXT;
3228 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3229 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3230 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003231 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003232
3233 switch (cur) {
3234 case 's':
3235 type = XML_REGEXP_ANYSPACE;
3236 break;
3237 case 'S':
3238 type = XML_REGEXP_NOTSPACE;
3239 break;
3240 case 'i':
3241 type = XML_REGEXP_INITNAME;
3242 break;
3243 case 'I':
3244 type = XML_REGEXP_NOTINITNAME;
3245 break;
3246 case 'c':
3247 type = XML_REGEXP_NAMECHAR;
3248 break;
3249 case 'C':
3250 type = XML_REGEXP_NOTNAMECHAR;
3251 break;
3252 case 'd':
3253 type = XML_REGEXP_DECIMAL;
3254 break;
3255 case 'D':
3256 type = XML_REGEXP_NOTDECIMAL;
3257 break;
3258 case 'w':
3259 type = XML_REGEXP_REALCHAR;
3260 break;
3261 case 'W':
3262 type = XML_REGEXP_NOTREALCHAR;
3263 break;
3264 }
3265 NEXT;
3266 if (ctxt->atom == NULL) {
3267 ctxt->atom = xmlRegNewAtom(ctxt, type);
3268 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3269 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3270 type, 0, 0, NULL);
3271 }
3272 }
3273}
3274
3275/**
3276 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003277 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003278 *
3279 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3280 */
3281static int
3282xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3283 int ret = 0, cur;
3284
3285 if ((CUR != '&') || (NXT(1) != '#'))
3286 return(-1);
3287 NEXT;
3288 NEXT;
3289 cur = CUR;
3290 if (cur == 'x') {
3291 NEXT;
3292 cur = CUR;
3293 if (((cur >= '0') && (cur <= '9')) ||
3294 ((cur >= 'a') && (cur <= 'f')) ||
3295 ((cur >= 'A') && (cur <= 'F'))) {
3296 while (((cur >= '0') && (cur <= '9')) ||
3297 ((cur >= 'A') && (cur <= 'F'))) {
3298 if ((cur >= '0') && (cur <= '9'))
3299 ret = ret * 16 + cur - '0';
3300 else if ((cur >= 'a') && (cur <= 'f'))
3301 ret = ret * 16 + 10 + (cur - 'a');
3302 else
3303 ret = ret * 16 + 10 + (cur - 'A');
3304 NEXT;
3305 cur = CUR;
3306 }
3307 } else {
3308 ERROR("Char ref: expecting [0-9A-F]");
3309 return(-1);
3310 }
3311 } else {
3312 if ((cur >= '0') && (cur <= '9')) {
3313 while ((cur >= '0') && (cur <= '9')) {
3314 ret = ret * 10 + cur - '0';
3315 NEXT;
3316 cur = CUR;
3317 }
3318 } else {
3319 ERROR("Char ref: expecting [0-9]");
3320 return(-1);
3321 }
3322 }
3323 if (cur != ';') {
3324 ERROR("Char ref: expecting ';'");
3325 return(-1);
3326 } else {
3327 NEXT;
3328 }
3329 return(ret);
3330}
3331
3332/**
3333 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003334 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003335 *
3336 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3337 * [18] seRange ::= charOrEsc '-' charOrEsc
3338 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3339 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3340 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3341 */
3342static void
3343xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
3344 int cur;
3345 int start = -1;
3346 int end = -1;
3347
3348 if ((CUR == '&') && (NXT(1) == '#')) {
3349 end = start = xmlFAParseCharRef(ctxt);
3350 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3351 XML_REGEXP_CHARVAL, start, end, NULL);
3352 return;
3353 }
3354 cur = CUR;
3355 if (cur == '\\') {
3356 NEXT;
3357 cur = CUR;
3358 switch (cur) {
3359 case 'n': start = 0xA; break;
3360 case 'r': start = 0xD; break;
3361 case 't': start = 0x9; break;
3362 case '\\': case '|': case '.': case '-': case '^': case '?':
3363 case '*': case '+': case '{': case '}': case '(': case ')':
3364 case '[': case ']':
3365 start = cur; break;
3366 default:
3367 ERROR("Invalid escape value");
3368 return;
3369 }
3370 end = start;
3371 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3372 end = start = cur;
3373 } else {
3374 ERROR("Expecting a char range");
3375 return;
3376 }
3377 NEXT;
3378 if (start == '-') {
3379 return;
3380 }
3381 cur = CUR;
3382 if (cur != '-') {
3383 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3384 XML_REGEXP_CHARVAL, start, end, NULL);
3385 return;
3386 }
3387 NEXT;
3388 cur = CUR;
3389 if (cur == '\\') {
3390 NEXT;
3391 cur = CUR;
3392 switch (cur) {
3393 case 'n': end = 0xA; break;
3394 case 'r': end = 0xD; break;
3395 case 't': end = 0x9; break;
3396 case '\\': case '|': case '.': case '-': case '^': case '?':
3397 case '*': case '+': case '{': case '}': case '(': case ')':
3398 case '[': case ']':
3399 end = cur; break;
3400 default:
3401 ERROR("Invalid escape value");
3402 return;
3403 }
3404 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3405 end = cur;
3406 } else {
3407 ERROR("Expecting the end of a char range");
3408 return;
3409 }
3410 NEXT;
3411 /* TODO check that the values are acceptable character ranges for XML */
3412 if (end < start) {
3413 ERROR("End of range is before start of range");
3414 } else {
3415 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3416 XML_REGEXP_CHARVAL, start, end, NULL);
3417 }
3418 return;
3419}
3420
3421/**
3422 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003423 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003424 *
3425 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3426 */
3427static void
3428xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3429 do {
3430 if ((CUR == '\\') || (CUR == '.')) {
3431 xmlFAParseCharClassEsc(ctxt);
3432 } else {
3433 xmlFAParseCharRange(ctxt);
3434 }
3435 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3436 (ctxt->error == 0));
3437}
3438
3439/**
3440 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003441 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003442 *
3443 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3444 * [15] negCharGroup ::= '^' posCharGroup
3445 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3446 * [12] charClassExpr ::= '[' charGroup ']'
3447 */
3448static void
3449xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3450 int n = ctxt->neg;
3451 while ((CUR != ']') && (ctxt->error == 0)) {
3452 if (CUR == '^') {
3453 int neg = ctxt->neg;
3454
3455 NEXT;
3456 ctxt->neg = !ctxt->neg;
3457 xmlFAParsePosCharGroup(ctxt);
3458 ctxt->neg = neg;
3459 } else if (CUR == '-') {
3460 NEXT;
3461 ctxt->neg = !ctxt->neg;
3462 if (CUR != '[') {
3463 ERROR("charClassExpr: '[' expected");
3464 break;
3465 }
3466 NEXT;
3467 xmlFAParseCharGroup(ctxt);
3468 if (CUR == ']') {
3469 NEXT;
3470 } else {
3471 ERROR("charClassExpr: ']' expected");
3472 break;
3473 }
3474 break;
3475 } else if (CUR != ']') {
3476 xmlFAParsePosCharGroup(ctxt);
3477 }
3478 }
3479 ctxt->neg = n;
3480}
3481
3482/**
3483 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003484 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003485 *
3486 * [11] charClass ::= charClassEsc | charClassExpr
3487 * [12] charClassExpr ::= '[' charGroup ']'
3488 */
3489static void
3490xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3491 if (CUR == '[') {
3492 NEXT;
3493 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3494 if (ctxt->atom == NULL)
3495 return;
3496 xmlFAParseCharGroup(ctxt);
3497 if (CUR == ']') {
3498 NEXT;
3499 } else {
3500 ERROR("xmlFAParseCharClass: ']' expected");
3501 }
3502 } else {
3503 xmlFAParseCharClassEsc(ctxt);
3504 }
3505}
3506
3507/**
3508 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003509 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003510 *
3511 * [8] QuantExact ::= [0-9]+
3512 */
3513static int
3514xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3515 int ret = 0;
3516 int ok = 0;
3517
3518 while ((CUR >= '0') && (CUR <= '9')) {
3519 ret = ret * 10 + (CUR - '0');
3520 ok = 1;
3521 NEXT;
3522 }
3523 if (ok != 1) {
3524 return(-1);
3525 }
3526 return(ret);
3527}
3528
3529/**
3530 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003531 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003532 *
3533 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3534 * [5] quantity ::= quantRange | quantMin | QuantExact
3535 * [6] quantRange ::= QuantExact ',' QuantExact
3536 * [7] quantMin ::= QuantExact ','
3537 * [8] QuantExact ::= [0-9]+
3538 */
3539static int
3540xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3541 int cur;
3542
3543 cur = CUR;
3544 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3545 if (ctxt->atom != NULL) {
3546 if (cur == '?')
3547 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3548 else if (cur == '*')
3549 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3550 else if (cur == '+')
3551 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3552 }
3553 NEXT;
3554 return(1);
3555 }
3556 if (cur == '{') {
3557 int min = 0, max = 0;
3558
3559 NEXT;
3560 cur = xmlFAParseQuantExact(ctxt);
3561 if (cur >= 0)
3562 min = cur;
3563 if (CUR == ',') {
3564 NEXT;
3565 cur = xmlFAParseQuantExact(ctxt);
3566 if (cur >= 0)
3567 max = cur;
3568 }
3569 if (CUR == '}') {
3570 NEXT;
3571 } else {
3572 ERROR("Unterminated quantifier");
3573 }
3574 if (max == 0)
3575 max = min;
3576 if (ctxt->atom != NULL) {
3577 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3578 ctxt->atom->min = min;
3579 ctxt->atom->max = max;
3580 }
3581 return(1);
3582 }
3583 return(0);
3584}
3585
3586/**
3587 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003588 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003589 *
3590 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3591 */
3592static int
3593xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3594 int codepoint, len;
3595
3596 codepoint = xmlFAIsChar(ctxt);
3597 if (codepoint > 0) {
3598 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3599 if (ctxt->atom == NULL)
3600 return(-1);
3601 codepoint = CUR_SCHAR(ctxt->cur, len);
3602 ctxt->atom->codepoint = codepoint;
3603 NEXTL(len);
3604 return(1);
3605 } else if (CUR == '|') {
3606 return(0);
3607 } else if (CUR == 0) {
3608 return(0);
3609 } else if (CUR == ')') {
3610 return(0);
3611 } else if (CUR == '(') {
3612 xmlRegStatePtr start, oldend;
3613
3614 NEXT;
3615 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3616 start = ctxt->state;
3617 oldend = ctxt->end;
3618 ctxt->end = NULL;
3619 ctxt->atom = NULL;
3620 xmlFAParseRegExp(ctxt, 0);
3621 if (CUR == ')') {
3622 NEXT;
3623 } else {
3624 ERROR("xmlFAParseAtom: expecting ')'");
3625 }
3626 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3627 if (ctxt->atom == NULL)
3628 return(-1);
3629 ctxt->atom->start = start;
3630 ctxt->atom->stop = ctxt->state;
3631 ctxt->end = oldend;
3632 return(1);
3633 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3634 xmlFAParseCharClass(ctxt);
3635 return(1);
3636 }
3637 return(0);
3638}
3639
3640/**
3641 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003642 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003643 *
3644 * [3] piece ::= atom quantifier?
3645 */
3646static int
3647xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3648 int ret;
3649
3650 ctxt->atom = NULL;
3651 ret = xmlFAParseAtom(ctxt);
3652 if (ret == 0)
3653 return(0);
3654 if (ctxt->atom == NULL) {
3655 ERROR("internal: no atom generated");
3656 }
3657 xmlFAParseQuantifier(ctxt);
3658 return(1);
3659}
3660
3661/**
3662 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003663 * @ctxt: a regexp parser context
3664 * @first: is taht the first
Daniel Veillard4255d502002-04-16 15:50:10 +00003665 *
3666 * [2] branch ::= piece*
3667 */
3668static void
3669xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, int first) {
3670 xmlRegStatePtr previous;
3671 xmlRegAtomPtr prevatom = NULL;
3672 int ret;
3673
3674 previous = ctxt->state;
3675 ret = xmlFAParsePiece(ctxt);
3676 if (ret != 0) {
3677 if (first) {
3678 xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom);
3679 previous = ctxt->state;
3680 } else {
3681 prevatom = ctxt->atom;
3682 }
3683 ctxt->atom = NULL;
3684 }
3685 while ((ret != 0) && (ctxt->error == 0)) {
3686 ret = xmlFAParsePiece(ctxt);
3687 if (ret != 0) {
3688 if (first) {
3689 xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom);
3690 } else {
3691 xmlFAGenerateTransitions(ctxt, previous, NULL, prevatom);
3692 prevatom = ctxt->atom;
3693 }
3694 previous = ctxt->state;
3695 ctxt->atom = NULL;
3696 }
3697 }
3698 if (!first) {
3699 xmlFAGenerateTransitions(ctxt, previous, ctxt->end, prevatom);
3700 }
3701}
3702
3703/**
3704 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003705 * @ctxt: a regexp parser context
3706 * @top: is that the top-level expressions ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003707 *
3708 * [1] regExp ::= branch ( '|' branch )*
3709 */
3710static void
3711xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
3712 xmlRegStatePtr start, end, oldend;
3713
3714 oldend = ctxt->end;
3715
3716 start = ctxt->state;
3717 xmlFAParseBranch(ctxt, (ctxt->end == NULL));
3718 if (CUR != '|') {
3719 ctxt->end = ctxt->state;
3720 return;
3721 }
3722 end = ctxt->state;
3723 while ((CUR == '|') && (ctxt->error == 0)) {
3724 NEXT;
3725 ctxt->state = start;
3726 ctxt->end = end;
3727 xmlFAParseBranch(ctxt, 0);
3728 }
3729 if (!top)
3730 ctxt->end = oldend;
3731}
3732
3733/************************************************************************
3734 * *
3735 * The basic API *
3736 * *
3737 ************************************************************************/
3738
3739/**
3740 * xmlRegexpPrint:
3741 * @output: the file for the output debug
3742 * @regexp: the compiled regexp
3743 *
3744 * Print the content of the compiled regular expression
3745 */
3746void
3747xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3748 int i;
3749
3750 fprintf(output, " regexp: ");
3751 if (regexp == NULL) {
3752 fprintf(output, "NULL\n");
3753 return;
3754 }
3755 fprintf(output, "'%s' ", regexp->string);
3756 fprintf(output, "\n");
3757 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3758 for (i = 0;i < regexp->nbAtoms; i++) {
3759 fprintf(output, " %02d ", i);
3760 xmlRegPrintAtom(output, regexp->atoms[i]);
3761 }
3762 fprintf(output, "%d states:", regexp->nbStates);
3763 fprintf(output, "\n");
3764 for (i = 0;i < regexp->nbStates; i++) {
3765 xmlRegPrintState(output, regexp->states[i]);
3766 }
3767 fprintf(output, "%d counters:\n", regexp->nbCounters);
3768 for (i = 0;i < regexp->nbCounters; i++) {
3769 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
3770 regexp->counters[i].max);
3771 }
3772}
3773
3774/**
3775 * xmlRegexpCompile:
3776 * @regexp: a regular expression string
3777 *
3778 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
3779 * Appendix F and build an automata suitable for testing strings against
3780 * that regular expression
3781 *
3782 * Returns the compiled expression or NULL in case of error
3783 */
3784xmlRegexpPtr
3785xmlRegexpCompile(const xmlChar *regexp) {
3786 xmlRegexpPtr ret;
3787 xmlRegParserCtxtPtr ctxt;
3788
3789 ctxt = xmlRegNewParserCtxt(regexp);
3790 if (ctxt == NULL)
3791 return(NULL);
3792
3793 /* initialize the parser */
3794 ctxt->end = NULL;
3795 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3796 xmlRegStatePush(ctxt, ctxt->start);
3797
3798 /* parse the expression building an automata */
3799 xmlFAParseRegExp(ctxt, 1);
3800 if (CUR != 0) {
3801 ERROR("xmlFAParseRegExp: extra characters");
3802 }
3803 ctxt->end = ctxt->state;
3804 ctxt->start->type = XML_REGEXP_START_STATE;
3805 ctxt->end->type = XML_REGEXP_FINAL_STATE;
3806
3807 /* remove the Epsilon except for counted transitions */
3808 xmlFAEliminateEpsilonTransitions(ctxt);
3809
3810
3811 if (ctxt->error != 0) {
3812 xmlRegFreeParserCtxt(ctxt);
3813 return(NULL);
3814 }
3815 ret = xmlRegEpxFromParse(ctxt);
3816 xmlRegFreeParserCtxt(ctxt);
3817 return(ret);
3818}
3819
3820/**
3821 * xmlRegexpExec:
3822 * @comp: the compiled regular expression
3823 * @content: the value to check against the regular expression
3824 *
3825 * Check if the regular expression generate the value
3826 *
3827 * Returns 1 if it matches, 0 if not and a negativa value in case of error
3828 */
3829int
3830xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
3831 if ((comp == NULL) || (content == NULL))
3832 return(-1);
3833 return(xmlFARegExec(comp, content));
3834}
3835
3836/**
Daniel Veillard23e73572002-09-19 19:56:43 +00003837 * xmlRegexpIsDeterminist:
3838 * @comp: the compiled regular expression
3839 *
3840 * Check if the regular expression is determinist
3841 *
3842 * Returns 1 if it yes, 0 if not and a negativa value in case of error
3843 */
3844int
3845xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
3846 xmlAutomataPtr am;
3847 int ret;
3848
3849 if (comp == NULL)
3850 return(-1);
3851 if (comp->determinist != -1)
3852 return(comp->determinist);
3853
3854 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00003855 if (am->states != NULL) {
3856 int i;
3857
3858 for (i = 0;i < am->nbStates;i++)
3859 xmlRegFreeState(am->states[i]);
3860 xmlFree(am->states);
3861 }
Daniel Veillard23e73572002-09-19 19:56:43 +00003862 am->nbAtoms = comp->nbAtoms;
3863 am->atoms = comp->atoms;
3864 am->nbStates = comp->nbStates;
3865 am->states = comp->states;
3866 am->determinist = -1;
3867 ret = xmlFAComputesDeterminism(am);
3868 am->atoms = NULL;
3869 am->states = NULL;
3870 xmlFreeAutomata(am);
3871 return(ret);
3872}
3873
3874/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003875 * xmlRegFreeRegexp:
3876 * @regexp: the regexp
3877 *
3878 * Free a regexp
3879 */
3880void
3881xmlRegFreeRegexp(xmlRegexpPtr regexp) {
3882 int i;
3883 if (regexp == NULL)
3884 return;
3885
3886 if (regexp->string != NULL)
3887 xmlFree(regexp->string);
3888 if (regexp->states != NULL) {
3889 for (i = 0;i < regexp->nbStates;i++)
3890 xmlRegFreeState(regexp->states[i]);
3891 xmlFree(regexp->states);
3892 }
3893 if (regexp->atoms != NULL) {
3894 for (i = 0;i < regexp->nbAtoms;i++)
3895 xmlRegFreeAtom(regexp->atoms[i]);
3896 xmlFree(regexp->atoms);
3897 }
3898 if (regexp->counters != NULL)
3899 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00003900 if (regexp->compact != NULL)
3901 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00003902 if (regexp->transdata != NULL)
3903 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00003904 if (regexp->stringMap != NULL) {
3905 for (i = 0; i < regexp->nbstrings;i++)
3906 xmlFree(regexp->stringMap[i]);
3907 xmlFree(regexp->stringMap);
3908 }
3909
Daniel Veillard4255d502002-04-16 15:50:10 +00003910 xmlFree(regexp);
3911}
3912
3913#ifdef LIBXML_AUTOMATA_ENABLED
3914/************************************************************************
3915 * *
3916 * The Automata interface *
3917 * *
3918 ************************************************************************/
3919
3920/**
3921 * xmlNewAutomata:
3922 *
3923 * Create a new automata
3924 *
3925 * Returns the new object or NULL in case of failure
3926 */
3927xmlAutomataPtr
3928xmlNewAutomata(void) {
3929 xmlAutomataPtr ctxt;
3930
3931 ctxt = xmlRegNewParserCtxt(NULL);
3932 if (ctxt == NULL)
3933 return(NULL);
3934
3935 /* initialize the parser */
3936 ctxt->end = NULL;
3937 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3938 xmlRegStatePush(ctxt, ctxt->start);
3939
3940 return(ctxt);
3941}
3942
3943/**
3944 * xmlFreeAutomata:
3945 * @am: an automata
3946 *
3947 * Free an automata
3948 */
3949void
3950xmlFreeAutomata(xmlAutomataPtr am) {
3951 if (am == NULL)
3952 return;
3953 xmlRegFreeParserCtxt(am);
3954}
3955
3956/**
3957 * xmlAutomataGetInitState:
3958 * @am: an automata
3959 *
3960 * Returns the initial state of the automata
3961 */
3962xmlAutomataStatePtr
3963xmlAutomataGetInitState(xmlAutomataPtr am) {
3964 if (am == NULL)
3965 return(NULL);
3966 return(am->start);
3967}
3968
3969/**
3970 * xmlAutomataSetFinalState:
3971 * @am: an automata
3972 * @state: a state in this automata
3973 *
3974 * Makes that state a final state
3975 *
3976 * Returns 0 or -1 in case of error
3977 */
3978int
3979xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
3980 if ((am == NULL) || (state == NULL))
3981 return(-1);
3982 state->type = XML_REGEXP_FINAL_STATE;
3983 return(0);
3984}
3985
3986/**
3987 * xmlAutomataNewTransition:
3988 * @am: an automata
3989 * @from: the starting point of the transition
3990 * @to: the target point of the transition or NULL
3991 * @token: the input string associated to that transition
3992 * @data: data passed to the callback function if the transition is activated
3993 *
3994 * If @to is NULL, this create first a new target state in the automata
3995 * and then adds a transition from the @from state to the target state
3996 * activated by the value of @token
3997 *
3998 * Returns the target state or NULL in case of error
3999 */
4000xmlAutomataStatePtr
4001xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4002 xmlAutomataStatePtr to, const xmlChar *token,
4003 void *data) {
4004 xmlRegAtomPtr atom;
4005
4006 if ((am == NULL) || (from == NULL) || (token == NULL))
4007 return(NULL);
4008 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4009 atom->data = data;
4010 if (atom == NULL)
4011 return(NULL);
4012 atom->valuep = xmlStrdup(token);
4013
4014 xmlFAGenerateTransitions(am, from, to, atom);
4015 if (to == NULL)
4016 return(am->state);
4017 return(to);
4018}
4019
4020/**
4021 * xmlAutomataNewCountTrans:
4022 * @am: an automata
4023 * @from: the starting point of the transition
4024 * @to: the target point of the transition or NULL
4025 * @token: the input string associated to that transition
4026 * @min: the minimum successive occurences of token
4027 * @min: the maximum successive occurences of token
4028 *
4029 * If @to is NULL, this create first a new target state in the automata
4030 * and then adds a transition from the @from state to the target state
4031 * activated by a succession of input of value @token and whose number
4032 * is between @min and @max
4033 *
4034 * Returns the target state or NULL in case of error
4035 */
4036xmlAutomataStatePtr
4037xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4038 xmlAutomataStatePtr to, const xmlChar *token,
4039 int min, int max, void *data) {
4040 xmlRegAtomPtr atom;
4041
4042 if ((am == NULL) || (from == NULL) || (token == NULL))
4043 return(NULL);
4044 if (min < 0)
4045 return(NULL);
4046 if ((max < min) || (max < 1))
4047 return(NULL);
4048 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4049 if (atom == NULL)
4050 return(NULL);
4051 atom->valuep = xmlStrdup(token);
4052 atom->data = data;
4053 if (min == 0)
4054 atom->min = 1;
4055 else
4056 atom->min = min;
4057 atom->max = max;
4058
4059 xmlFAGenerateTransitions(am, from, to, atom);
4060 if (to == NULL)
4061 to = am->state;
4062 if (to == NULL)
4063 return(NULL);
4064 if (min == 0)
4065 xmlFAGenerateEpsilonTransition(am, from, to);
4066 return(to);
4067}
4068
4069/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004070 * xmlAutomataNewOnceTrans:
4071 * @am: an automata
4072 * @from: the starting point of the transition
4073 * @to: the target point of the transition or NULL
4074 * @token: the input string associated to that transition
4075 * @min: the minimum successive occurences of token
4076 * @min: the maximum successive occurences of token
4077 *
4078 * If @to is NULL, this create first a new target state in the automata
4079 * and then adds a transition from the @from state to the target state
4080 * activated by a succession of input of value @token and whose number
4081 * is between @min and @max, moreover that transistion can only be crossed
4082 * once.
4083 *
4084 * Returns the target state or NULL in case of error
4085 */
4086xmlAutomataStatePtr
4087xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4088 xmlAutomataStatePtr to, const xmlChar *token,
4089 int min, int max, void *data) {
4090 xmlRegAtomPtr atom;
4091 int counter;
4092
4093 if ((am == NULL) || (from == NULL) || (token == NULL))
4094 return(NULL);
4095 if (min < 1)
4096 return(NULL);
4097 if ((max < min) || (max < 1))
4098 return(NULL);
4099 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4100 if (atom == NULL)
4101 return(NULL);
4102 atom->valuep = xmlStrdup(token);
4103 atom->data = data;
4104 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4105 if (min == 0)
4106 atom->min = 1;
4107 else
4108 atom->min = min;
4109 atom->max = max;
4110 /*
4111 * associate a counter to the transition.
4112 */
4113 counter = xmlRegGetCounter(am);
4114 am->counters[counter].min = 1;
4115 am->counters[counter].max = 1;
4116
4117 /* xmlFAGenerateTransitions(am, from, to, atom); */
4118 if (to == NULL) {
4119 to = xmlRegNewState(am);
4120 xmlRegStatePush(am, to);
4121 }
4122 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4123 xmlRegAtomPush(am, atom);
4124 am->state = to;
4125 if (to == NULL)
4126 to = am->state;
4127 if (to == NULL)
4128 return(NULL);
4129 return(to);
4130}
4131
4132/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004133 * xmlAutomataNewState:
4134 * @am: an automata
4135 *
4136 * Create a new disconnected state in the automata
4137 *
4138 * Returns the new state or NULL in case of error
4139 */
4140xmlAutomataStatePtr
4141xmlAutomataNewState(xmlAutomataPtr am) {
4142 xmlAutomataStatePtr to;
4143
4144 if (am == NULL)
4145 return(NULL);
4146 to = xmlRegNewState(am);
4147 xmlRegStatePush(am, to);
4148 return(to);
4149}
4150
4151/**
4152 * xmlAutomataNewTransition:
4153 * @am: an automata
4154 * @from: the starting point of the transition
4155 * @to: the target point of the transition or NULL
4156 *
4157 * If @to is NULL, this create first a new target state in the automata
4158 * and then adds a an epsilon transition from the @from state to the
4159 * target state
4160 *
4161 * Returns the target state or NULL in case of error
4162 */
4163xmlAutomataStatePtr
4164xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4165 xmlAutomataStatePtr to) {
4166 if ((am == NULL) || (from == NULL))
4167 return(NULL);
4168 xmlFAGenerateEpsilonTransition(am, from, to);
4169 if (to == NULL)
4170 return(am->state);
4171 return(to);
4172}
4173
Daniel Veillardb509f152002-04-17 16:28:10 +00004174/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004175 * xmlAutomataNewAllTrans:
4176 * @am: an automata
4177 * @from: the starting point of the transition
4178 * @to: the target point of the transition or NULL
4179 *
4180 * If @to is NULL, this create first a new target state in the automata
4181 * and then adds a an ALL transition from the @from state to the
4182 * target state. That transition is an epsilon transition allowed only when
4183 * all transitions from the @from node have been activated.
4184 *
4185 * Returns the target state or NULL in case of error
4186 */
4187xmlAutomataStatePtr
4188xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004189 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004190 if ((am == NULL) || (from == NULL))
4191 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004192 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004193 if (to == NULL)
4194 return(am->state);
4195 return(to);
4196}
4197
4198/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004199 * xmlAutomataNewCounter:
4200 * @am: an automata
4201 * @min: the minimal value on the counter
4202 * @max: the maximal value on the counter
4203 *
4204 * Create a new counter
4205 *
4206 * Returns the counter number or -1 in case of error
4207 */
4208int
4209xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4210 int ret;
4211
4212 if (am == NULL)
4213 return(-1);
4214
4215 ret = xmlRegGetCounter(am);
4216 if (ret < 0)
4217 return(-1);
4218 am->counters[ret].min = min;
4219 am->counters[ret].max = max;
4220 return(ret);
4221}
4222
4223/**
4224 * xmlAutomataNewCountedTrans:
4225 * @am: an automata
4226 * @from: the starting point of the transition
4227 * @to: the target point of the transition or NULL
4228 * @counter: the counter associated to that transition
4229 *
4230 * If @to is NULL, this create first a new target state in the automata
4231 * and then adds an epsilon transition from the @from state to the target state
4232 * which will increment the counter provided
4233 *
4234 * Returns the target state or NULL in case of error
4235 */
4236xmlAutomataStatePtr
4237xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4238 xmlAutomataStatePtr to, int counter) {
4239 if ((am == NULL) || (from == NULL) || (counter < 0))
4240 return(NULL);
4241 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4242 if (to == NULL)
4243 return(am->state);
4244 return(to);
4245}
4246
4247/**
4248 * xmlAutomataNewCounterTrans:
4249 * @am: an automata
4250 * @from: the starting point of the transition
4251 * @to: the target point of the transition or NULL
4252 * @counter: the counter associated to that transition
4253 *
4254 * If @to is NULL, this create first a new target state in the automata
4255 * and then adds an epsilon transition from the @from state to the target state
4256 * which will be allowed only if the counter is within the right range.
4257 *
4258 * Returns the target state or NULL in case of error
4259 */
4260xmlAutomataStatePtr
4261xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4262 xmlAutomataStatePtr to, int counter) {
4263 if ((am == NULL) || (from == NULL) || (counter < 0))
4264 return(NULL);
4265 xmlFAGenerateCountedTransition(am, from, to, counter);
4266 if (to == NULL)
4267 return(am->state);
4268 return(to);
4269}
Daniel Veillard4255d502002-04-16 15:50:10 +00004270
4271/**
4272 * xmlAutomataCompile:
4273 * @am: an automata
4274 *
4275 * Compile the automata into a Reg Exp ready for being executed.
4276 * The automata should be free after this point.
4277 *
4278 * Returns the compiled regexp or NULL in case of error
4279 */
4280xmlRegexpPtr
4281xmlAutomataCompile(xmlAutomataPtr am) {
4282 xmlRegexpPtr ret;
4283
4284 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004285 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004286 ret = xmlRegEpxFromParse(am);
4287
4288 return(ret);
4289}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004290
4291/**
4292 * xmlAutomataIsDeterminist:
4293 * @am: an automata
4294 *
4295 * Checks if an automata is determinist.
4296 *
4297 * Returns 1 if true, 0 if not, and -1 in case of error
4298 */
4299int
4300xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4301 int ret;
4302
4303 if (am == NULL)
4304 return(-1);
4305
4306 ret = xmlFAComputesDeterminism(am);
4307 return(ret);
4308}
Daniel Veillard4255d502002-04-16 15:50:10 +00004309#endif /* LIBXML_AUTOMATA_ENABLED */
4310#endif /* LIBXML_REGEXP_ENABLED */