blob: e9acd5d07918ab893d361a0b66f6ab5ffe89553c [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 */
33
34#define ERROR(str) ctxt->error = 1; \
35 xmlGenericError(xmlGenericErrorContext, "Regexp: %s: %s\n", str, ctxt->cur)
36#define NEXT ctxt->cur++
37#define CUR (*(ctxt->cur))
38#define NXT(index) (ctxt->cur[index])
39
40#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
41#define NEXTL(l) ctxt->cur += l;
42
Daniel Veillarde19fc232002-04-22 16:01:24 +000043/**
44 * TODO:
45 *
46 * macro to flag unimplemented blocks
47 */
48#define TODO \
49 xmlGenericError(xmlGenericErrorContext, \
50 "Unimplemented block at %s:%d\n", \
51 __FILE__, __LINE__);
52
Daniel Veillard4255d502002-04-16 15:50:10 +000053
54/************************************************************************
55 * *
56 * Datatypes and structures *
57 * *
58 ************************************************************************/
59
60typedef enum {
61 XML_REGEXP_EPSILON = 1,
62 XML_REGEXP_CHARVAL,
63 XML_REGEXP_RANGES,
64 XML_REGEXP_SUBREG,
65 XML_REGEXP_STRING,
66 XML_REGEXP_ANYCHAR, /* . */
67 XML_REGEXP_ANYSPACE, /* \s */
68 XML_REGEXP_NOTSPACE, /* \S */
69 XML_REGEXP_INITNAME, /* \l */
70 XML_REGEXP_NOTINITNAME, /* \l */
71 XML_REGEXP_NAMECHAR, /* \c */
72 XML_REGEXP_NOTNAMECHAR, /* \C */
73 XML_REGEXP_DECIMAL, /* \d */
74 XML_REGEXP_NOTDECIMAL, /* \d */
75 XML_REGEXP_REALCHAR, /* \w */
76 XML_REGEXP_NOTREALCHAR, /* \w */
77 XML_REGEXP_LETTER,
78 XML_REGEXP_LETTER_UPPERCASE,
79 XML_REGEXP_LETTER_LOWERCASE,
80 XML_REGEXP_LETTER_TITLECASE,
81 XML_REGEXP_LETTER_MODIFIER,
82 XML_REGEXP_LETTER_OTHERS,
83 XML_REGEXP_MARK,
84 XML_REGEXP_MARK_NONSPACING,
85 XML_REGEXP_MARK_SPACECOMBINING,
86 XML_REGEXP_MARK_ENCLOSING,
87 XML_REGEXP_NUMBER,
88 XML_REGEXP_NUMBER_DECIMAL,
89 XML_REGEXP_NUMBER_LETTER,
90 XML_REGEXP_NUMBER_OTHERS,
91 XML_REGEXP_PUNCT,
92 XML_REGEXP_PUNCT_CONNECTOR,
93 XML_REGEXP_PUNCT_DASH,
94 XML_REGEXP_PUNCT_OPEN,
95 XML_REGEXP_PUNCT_CLOSE,
96 XML_REGEXP_PUNCT_INITQUOTE,
97 XML_REGEXP_PUNCT_FINQUOTE,
98 XML_REGEXP_PUNCT_OTHERS,
99 XML_REGEXP_SEPAR,
100 XML_REGEXP_SEPAR_SPACE,
101 XML_REGEXP_SEPAR_LINE,
102 XML_REGEXP_SEPAR_PARA,
103 XML_REGEXP_SYMBOL,
104 XML_REGEXP_SYMBOL_MATH,
105 XML_REGEXP_SYMBOL_CURRENCY,
106 XML_REGEXP_SYMBOL_MODIFIER,
107 XML_REGEXP_SYMBOL_OTHERS,
108 XML_REGEXP_OTHER,
109 XML_REGEXP_OTHER_CONTROL,
110 XML_REGEXP_OTHER_FORMAT,
111 XML_REGEXP_OTHER_PRIVATE,
112 XML_REGEXP_OTHER_NA,
113 XML_REGEXP_BLOCK_NAME
114} xmlRegAtomType;
115
116typedef enum {
117 XML_REGEXP_QUANT_EPSILON = 1,
118 XML_REGEXP_QUANT_ONCE,
119 XML_REGEXP_QUANT_OPT,
120 XML_REGEXP_QUANT_MULT,
121 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000122 XML_REGEXP_QUANT_ONCEONLY,
123 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000124 XML_REGEXP_QUANT_RANGE
125} xmlRegQuantType;
126
127typedef enum {
128 XML_REGEXP_START_STATE = 1,
129 XML_REGEXP_FINAL_STATE,
130 XML_REGEXP_TRANS_STATE
131} xmlRegStateType;
132
133typedef enum {
134 XML_REGEXP_MARK_NORMAL = 0,
135 XML_REGEXP_MARK_START,
136 XML_REGEXP_MARK_VISITED
137} xmlRegMarkedType;
138
139typedef struct _xmlRegRange xmlRegRange;
140typedef xmlRegRange *xmlRegRangePtr;
141
142struct _xmlRegRange {
143 int neg;
144 xmlRegAtomType type;
145 int start;
146 int end;
147 xmlChar *blockName;
148};
149
150typedef struct _xmlRegAtom xmlRegAtom;
151typedef xmlRegAtom *xmlRegAtomPtr;
152
153typedef struct _xmlAutomataState xmlRegState;
154typedef xmlRegState *xmlRegStatePtr;
155
156struct _xmlRegAtom {
157 int no;
158 xmlRegAtomType type;
159 xmlRegQuantType quant;
160 int min;
161 int max;
162
163 void *valuep;
164 int neg;
165 int codepoint;
166 xmlRegStatePtr start;
167 xmlRegStatePtr stop;
168 int maxRanges;
169 int nbRanges;
170 xmlRegRangePtr *ranges;
171 void *data;
172};
173
174typedef struct _xmlRegCounter xmlRegCounter;
175typedef xmlRegCounter *xmlRegCounterPtr;
176
177struct _xmlRegCounter {
178 int min;
179 int max;
180};
181
182typedef struct _xmlRegTrans xmlRegTrans;
183typedef xmlRegTrans *xmlRegTransPtr;
184
185struct _xmlRegTrans {
186 xmlRegAtomPtr atom;
187 int to;
188 int counter;
189 int count;
190};
191
192struct _xmlAutomataState {
193 xmlRegStateType type;
194 xmlRegMarkedType mark;
195 int no;
196
197 int maxTrans;
198 int nbTrans;
199 xmlRegTrans *trans;
200};
201
202typedef struct _xmlAutomata xmlRegParserCtxt;
203typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
204
205struct _xmlAutomata {
206 xmlChar *string;
207 xmlChar *cur;
208
209 int error;
210 int neg;
211
212 xmlRegStatePtr start;
213 xmlRegStatePtr end;
214 xmlRegStatePtr state;
215
216 xmlRegAtomPtr atom;
217
218 int maxAtoms;
219 int nbAtoms;
220 xmlRegAtomPtr *atoms;
221
222 int maxStates;
223 int nbStates;
224 xmlRegStatePtr *states;
225
226 int maxCounters;
227 int nbCounters;
228 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000229
230 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000231};
232
233struct _xmlRegexp {
234 xmlChar *string;
235 int nbStates;
236 xmlRegStatePtr *states;
237 int nbAtoms;
238 xmlRegAtomPtr *atoms;
239 int nbCounters;
240 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000241 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000242};
243
244typedef struct _xmlRegExecRollback xmlRegExecRollback;
245typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
246
247struct _xmlRegExecRollback {
248 xmlRegStatePtr state;/* the current state */
249 int index; /* the index in the input stack */
250 int nextbranch; /* the next transition to explore in that state */
251 int *counts; /* save the automate state if it has some */
252};
253
254typedef struct _xmlRegInputToken xmlRegInputToken;
255typedef xmlRegInputToken *xmlRegInputTokenPtr;
256
257struct _xmlRegInputToken {
258 xmlChar *value;
259 void *data;
260};
261
262struct _xmlRegExecCtxt {
263 int status; /* execution status != 0 indicate an error */
264 int determinist; /* did we found an inderterministic behaviour */
265 xmlRegexpPtr comp; /* the compiled regexp */
266 xmlRegExecCallbacks callback;
267 void *data;
268
269 xmlRegStatePtr state;/* the current state */
270 int transno; /* the current transition on that state */
271 int transcount; /* the number of char in char counted transitions */
272
273 /*
274 * A stack of rollback states
275 */
276 int maxRollbacks;
277 int nbRollbacks;
278 xmlRegExecRollback *rollbacks;
279
280 /*
281 * The state of the automata if any
282 */
283 int *counts;
284
285 /*
286 * The input stack
287 */
288 int inputStackMax;
289 int inputStackNr;
290 int index;
291 int *charStack;
292 const xmlChar *inputString; /* when operating on characters */
293 xmlRegInputTokenPtr inputStack;/* when operating on strings */
294
295};
296
Daniel Veillard441bc322002-04-20 17:38:48 +0000297#define REGEXP_ALL_COUNTER 0x123456
298#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000299
Daniel Veillard4255d502002-04-16 15:50:10 +0000300static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
301
302/************************************************************************
303 * *
304 * Allocation/Deallocation *
305 * *
306 ************************************************************************/
307
308/**
309 * xmlRegEpxFromParse:
310 * @ctxt: the parser context used to build it
311 *
312 * Allocate a new regexp and fill it with the reult from the parser
313 *
314 * Returns the new regexp or NULL in case of error
315 */
316static xmlRegexpPtr
317xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
318 xmlRegexpPtr ret;
319
320 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
321 if (ret == NULL)
322 return(NULL);
323 memset(ret, 0, sizeof(xmlRegexp));
324 ret->string = ctxt->string;
325 ctxt->string = NULL;
326 ret->nbStates = ctxt->nbStates;
327 ctxt->nbStates = 0;
328 ret->states = ctxt->states;
329 ctxt->states = NULL;
330 ret->nbAtoms = ctxt->nbAtoms;
331 ctxt->nbAtoms = 0;
332 ret->atoms = ctxt->atoms;
333 ctxt->atoms = NULL;
334 ret->nbCounters = ctxt->nbCounters;
335 ctxt->nbCounters = 0;
336 ret->counters = ctxt->counters;
337 ctxt->counters = NULL;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000338 ret->determinist = ctxt->determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000339 return(ret);
340}
341
342/**
343 * xmlRegNewParserCtxt:
344 * @string: the string to parse
345 *
346 * Allocate a new regexp parser context
347 *
348 * Returns the new context or NULL in case of error
349 */
350static xmlRegParserCtxtPtr
351xmlRegNewParserCtxt(const xmlChar *string) {
352 xmlRegParserCtxtPtr ret;
353
354 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
355 if (ret == NULL)
356 return(NULL);
357 memset(ret, 0, sizeof(xmlRegParserCtxt));
358 if (string != NULL)
359 ret->string = xmlStrdup(string);
360 ret->cur = ret->string;
361 ret->neg = 0;
362 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000363 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000364 return(ret);
365}
366
367/**
368 * xmlRegNewRange:
369 * @ctxt: the regexp parser context
370 * @neg: is that negative
371 * @type: the type of range
372 * @start: the start codepoint
373 * @end: the end codepoint
374 *
375 * Allocate a new regexp range
376 *
377 * Returns the new range or NULL in case of error
378 */
379static xmlRegRangePtr
380xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
381 int neg, xmlRegAtomType type, int start, int end) {
382 xmlRegRangePtr ret;
383
384 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
385 if (ret == NULL) {
386 ERROR("failed to allocate regexp range");
387 return(NULL);
388 }
389 ret->neg = neg;
390 ret->type = type;
391 ret->start = start;
392 ret->end = end;
393 return(ret);
394}
395
396/**
397 * xmlRegFreeRange:
398 * @range: the regexp range
399 *
400 * Free a regexp range
401 */
402static void
403xmlRegFreeRange(xmlRegRangePtr range) {
404 if (range == NULL)
405 return;
406
407 if (range->blockName != NULL)
408 xmlFree(range->blockName);
409 xmlFree(range);
410}
411
412/**
413 * xmlRegNewAtom:
414 * @ctxt: the regexp parser context
415 * @type: the type of atom
416 *
417 * Allocate a new regexp range
418 *
419 * Returns the new atom or NULL in case of error
420 */
421static xmlRegAtomPtr
422xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
423 xmlRegAtomPtr ret;
424
425 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
426 if (ret == NULL) {
427 ERROR("failed to allocate regexp atom");
428 return(NULL);
429 }
430 memset(ret, 0, sizeof(xmlRegAtom));
431 ret->type = type;
432 ret->quant = XML_REGEXP_QUANT_ONCE;
433 ret->min = 0;
434 ret->max = 0;
435 return(ret);
436}
437
438/**
439 * xmlRegFreeAtom:
440 * @atom: the regexp atom
441 *
442 * Free a regexp atom
443 */
444static void
445xmlRegFreeAtom(xmlRegAtomPtr atom) {
446 int i;
447
448 if (atom == NULL)
449 return;
450
451 for (i = 0;i < atom->nbRanges;i++)
452 xmlRegFreeRange(atom->ranges[i]);
453 if (atom->ranges != NULL)
454 xmlFree(atom->ranges);
455 if (atom->type == XML_REGEXP_STRING)
456 xmlFree(atom->valuep);
457 xmlFree(atom);
458}
459
460static xmlRegStatePtr
461xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
462 xmlRegStatePtr ret;
463
464 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
465 if (ret == NULL) {
466 ERROR("failed to allocate regexp state");
467 return(NULL);
468 }
469 memset(ret, 0, sizeof(xmlRegState));
470 ret->type = XML_REGEXP_TRANS_STATE;
471 ret->mark = XML_REGEXP_MARK_NORMAL;
472 return(ret);
473}
474
475/**
476 * xmlRegFreeState:
477 * @state: the regexp state
478 *
479 * Free a regexp state
480 */
481static void
482xmlRegFreeState(xmlRegStatePtr state) {
483 if (state == NULL)
484 return;
485
486 if (state->trans != NULL)
487 xmlFree(state->trans);
488 xmlFree(state);
489}
490
491/**
492 * xmlRegFreeParserCtxt:
493 * @ctxt: the regexp parser context
494 *
495 * Free a regexp parser context
496 */
497static void
498xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
499 int i;
500 if (ctxt == NULL)
501 return;
502
503 if (ctxt->string != NULL)
504 xmlFree(ctxt->string);
505 if (ctxt->states != NULL) {
506 for (i = 0;i < ctxt->nbStates;i++)
507 xmlRegFreeState(ctxt->states[i]);
508 xmlFree(ctxt->states);
509 }
510 if (ctxt->atoms != NULL) {
511 for (i = 0;i < ctxt->nbAtoms;i++)
512 xmlRegFreeAtom(ctxt->atoms[i]);
513 xmlFree(ctxt->atoms);
514 }
515 if (ctxt->counters != NULL)
516 xmlFree(ctxt->counters);
517 xmlFree(ctxt);
518}
519
520/************************************************************************
521 * *
522 * Display of Data structures *
523 * *
524 ************************************************************************/
525
526static void
527xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
528 switch (type) {
529 case XML_REGEXP_EPSILON:
530 fprintf(output, "epsilon "); break;
531 case XML_REGEXP_CHARVAL:
532 fprintf(output, "charval "); break;
533 case XML_REGEXP_RANGES:
534 fprintf(output, "ranges "); break;
535 case XML_REGEXP_SUBREG:
536 fprintf(output, "subexpr "); break;
537 case XML_REGEXP_STRING:
538 fprintf(output, "string "); break;
539 case XML_REGEXP_ANYCHAR:
540 fprintf(output, "anychar "); break;
541 case XML_REGEXP_ANYSPACE:
542 fprintf(output, "anyspace "); break;
543 case XML_REGEXP_NOTSPACE:
544 fprintf(output, "notspace "); break;
545 case XML_REGEXP_INITNAME:
546 fprintf(output, "initname "); break;
547 case XML_REGEXP_NOTINITNAME:
548 fprintf(output, "notinitname "); break;
549 case XML_REGEXP_NAMECHAR:
550 fprintf(output, "namechar "); break;
551 case XML_REGEXP_NOTNAMECHAR:
552 fprintf(output, "notnamechar "); break;
553 case XML_REGEXP_DECIMAL:
554 fprintf(output, "decimal "); break;
555 case XML_REGEXP_NOTDECIMAL:
556 fprintf(output, "notdecimal "); break;
557 case XML_REGEXP_REALCHAR:
558 fprintf(output, "realchar "); break;
559 case XML_REGEXP_NOTREALCHAR:
560 fprintf(output, "notrealchar "); break;
561 case XML_REGEXP_LETTER:
562 fprintf(output, "LETTER "); break;
563 case XML_REGEXP_LETTER_UPPERCASE:
564 fprintf(output, "LETTER_UPPERCASE "); break;
565 case XML_REGEXP_LETTER_LOWERCASE:
566 fprintf(output, "LETTER_LOWERCASE "); break;
567 case XML_REGEXP_LETTER_TITLECASE:
568 fprintf(output, "LETTER_TITLECASE "); break;
569 case XML_REGEXP_LETTER_MODIFIER:
570 fprintf(output, "LETTER_MODIFIER "); break;
571 case XML_REGEXP_LETTER_OTHERS:
572 fprintf(output, "LETTER_OTHERS "); break;
573 case XML_REGEXP_MARK:
574 fprintf(output, "MARK "); break;
575 case XML_REGEXP_MARK_NONSPACING:
576 fprintf(output, "MARK_NONSPACING "); break;
577 case XML_REGEXP_MARK_SPACECOMBINING:
578 fprintf(output, "MARK_SPACECOMBINING "); break;
579 case XML_REGEXP_MARK_ENCLOSING:
580 fprintf(output, "MARK_ENCLOSING "); break;
581 case XML_REGEXP_NUMBER:
582 fprintf(output, "NUMBER "); break;
583 case XML_REGEXP_NUMBER_DECIMAL:
584 fprintf(output, "NUMBER_DECIMAL "); break;
585 case XML_REGEXP_NUMBER_LETTER:
586 fprintf(output, "NUMBER_LETTER "); break;
587 case XML_REGEXP_NUMBER_OTHERS:
588 fprintf(output, "NUMBER_OTHERS "); break;
589 case XML_REGEXP_PUNCT:
590 fprintf(output, "PUNCT "); break;
591 case XML_REGEXP_PUNCT_CONNECTOR:
592 fprintf(output, "PUNCT_CONNECTOR "); break;
593 case XML_REGEXP_PUNCT_DASH:
594 fprintf(output, "PUNCT_DASH "); break;
595 case XML_REGEXP_PUNCT_OPEN:
596 fprintf(output, "PUNCT_OPEN "); break;
597 case XML_REGEXP_PUNCT_CLOSE:
598 fprintf(output, "PUNCT_CLOSE "); break;
599 case XML_REGEXP_PUNCT_INITQUOTE:
600 fprintf(output, "PUNCT_INITQUOTE "); break;
601 case XML_REGEXP_PUNCT_FINQUOTE:
602 fprintf(output, "PUNCT_FINQUOTE "); break;
603 case XML_REGEXP_PUNCT_OTHERS:
604 fprintf(output, "PUNCT_OTHERS "); break;
605 case XML_REGEXP_SEPAR:
606 fprintf(output, "SEPAR "); break;
607 case XML_REGEXP_SEPAR_SPACE:
608 fprintf(output, "SEPAR_SPACE "); break;
609 case XML_REGEXP_SEPAR_LINE:
610 fprintf(output, "SEPAR_LINE "); break;
611 case XML_REGEXP_SEPAR_PARA:
612 fprintf(output, "SEPAR_PARA "); break;
613 case XML_REGEXP_SYMBOL:
614 fprintf(output, "SYMBOL "); break;
615 case XML_REGEXP_SYMBOL_MATH:
616 fprintf(output, "SYMBOL_MATH "); break;
617 case XML_REGEXP_SYMBOL_CURRENCY:
618 fprintf(output, "SYMBOL_CURRENCY "); break;
619 case XML_REGEXP_SYMBOL_MODIFIER:
620 fprintf(output, "SYMBOL_MODIFIER "); break;
621 case XML_REGEXP_SYMBOL_OTHERS:
622 fprintf(output, "SYMBOL_OTHERS "); break;
623 case XML_REGEXP_OTHER:
624 fprintf(output, "OTHER "); break;
625 case XML_REGEXP_OTHER_CONTROL:
626 fprintf(output, "OTHER_CONTROL "); break;
627 case XML_REGEXP_OTHER_FORMAT:
628 fprintf(output, "OTHER_FORMAT "); break;
629 case XML_REGEXP_OTHER_PRIVATE:
630 fprintf(output, "OTHER_PRIVATE "); break;
631 case XML_REGEXP_OTHER_NA:
632 fprintf(output, "OTHER_NA "); break;
633 case XML_REGEXP_BLOCK_NAME:
634 fprintf(output, "BLOCK "); break;
635 }
636}
637
638static void
639xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
640 switch (type) {
641 case XML_REGEXP_QUANT_EPSILON:
642 fprintf(output, "epsilon "); break;
643 case XML_REGEXP_QUANT_ONCE:
644 fprintf(output, "once "); break;
645 case XML_REGEXP_QUANT_OPT:
646 fprintf(output, "? "); break;
647 case XML_REGEXP_QUANT_MULT:
648 fprintf(output, "* "); break;
649 case XML_REGEXP_QUANT_PLUS:
650 fprintf(output, "+ "); break;
651 case XML_REGEXP_QUANT_RANGE:
652 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000653 case XML_REGEXP_QUANT_ONCEONLY:
654 fprintf(output, "onceonly "); break;
655 case XML_REGEXP_QUANT_ALL:
656 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000657 }
658}
659static void
660xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
661 fprintf(output, " range: ");
662 if (range->neg)
663 fprintf(output, "negative ");
664 xmlRegPrintAtomType(output, range->type);
665 fprintf(output, "%c - %c\n", range->start, range->end);
666}
667
668static void
669xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
670 fprintf(output, " atom: ");
671 if (atom == NULL) {
672 fprintf(output, "NULL\n");
673 return;
674 }
675 xmlRegPrintAtomType(output, atom->type);
676 xmlRegPrintQuantType(output, atom->quant);
677 if (atom->quant == XML_REGEXP_QUANT_RANGE)
678 fprintf(output, "%d-%d ", atom->min, atom->max);
679 if (atom->type == XML_REGEXP_STRING)
680 fprintf(output, "'%s' ", (char *) atom->valuep);
681 if (atom->type == XML_REGEXP_CHARVAL)
682 fprintf(output, "char %c\n", atom->codepoint);
683 else if (atom->type == XML_REGEXP_RANGES) {
684 int i;
685 fprintf(output, "%d entries\n", atom->nbRanges);
686 for (i = 0; i < atom->nbRanges;i++)
687 xmlRegPrintRange(output, atom->ranges[i]);
688 } else if (atom->type == XML_REGEXP_SUBREG) {
689 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
690 } else {
691 fprintf(output, "\n");
692 }
693}
694
695static void
696xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
697 fprintf(output, " trans: ");
698 if (trans == NULL) {
699 fprintf(output, "NULL\n");
700 return;
701 }
702 if (trans->to < 0) {
703 fprintf(output, "removed\n");
704 return;
705 }
706 if (trans->counter >= 0) {
707 fprintf(output, "counted %d, ", trans->counter);
708 }
Daniel Veillard8a001f62002-04-20 07:24:11 +0000709 if (trans->count == REGEXP_ALL_COUNTER) {
710 fprintf(output, "all transition, ");
711 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +0000712 fprintf(output, "count based %d, ", trans->count);
713 }
714 if (trans->atom == NULL) {
715 fprintf(output, "epsilon to %d\n", trans->to);
716 return;
717 }
718 if (trans->atom->type == XML_REGEXP_CHARVAL)
719 fprintf(output, "char %c ", trans->atom->codepoint);
720 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
721}
722
723static void
724xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
725 int i;
726
727 fprintf(output, " state: ");
728 if (state == NULL) {
729 fprintf(output, "NULL\n");
730 return;
731 }
732 if (state->type == XML_REGEXP_START_STATE)
733 fprintf(output, "START ");
734 if (state->type == XML_REGEXP_FINAL_STATE)
735 fprintf(output, "FINAL ");
736
737 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
738 for (i = 0;i < state->nbTrans; i++) {
739 xmlRegPrintTrans(output, &(state->trans[i]));
740 }
741}
742
743#if 0
744static void
745xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
746 int i;
747
748 fprintf(output, " ctxt: ");
749 if (ctxt == NULL) {
750 fprintf(output, "NULL\n");
751 return;
752 }
753 fprintf(output, "'%s' ", ctxt->string);
754 if (ctxt->error)
755 fprintf(output, "error ");
756 if (ctxt->neg)
757 fprintf(output, "neg ");
758 fprintf(output, "\n");
759 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
760 for (i = 0;i < ctxt->nbAtoms; i++) {
761 fprintf(output, " %02d ", i);
762 xmlRegPrintAtom(output, ctxt->atoms[i]);
763 }
764 if (ctxt->atom != NULL) {
765 fprintf(output, "current atom:\n");
766 xmlRegPrintAtom(output, ctxt->atom);
767 }
768 fprintf(output, "%d states:", ctxt->nbStates);
769 if (ctxt->start != NULL)
770 fprintf(output, " start: %d", ctxt->start->no);
771 if (ctxt->end != NULL)
772 fprintf(output, " end: %d", ctxt->end->no);
773 fprintf(output, "\n");
774 for (i = 0;i < ctxt->nbStates; i++) {
775 xmlRegPrintState(output, ctxt->states[i]);
776 }
777 fprintf(output, "%d counters:\n", ctxt->nbCounters);
778 for (i = 0;i < ctxt->nbCounters; i++) {
779 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
780 ctxt->counters[i].max);
781 }
782}
783#endif
784
785/************************************************************************
786 * *
787 * Finite Automata structures manipulations *
788 * *
789 ************************************************************************/
790
791static void
792xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
793 int neg, xmlRegAtomType type, int start, int end,
794 xmlChar *blockName) {
795 xmlRegRangePtr range;
796
797 if (atom == NULL) {
798 ERROR("add range: atom is NULL");
799 return;
800 }
801 if (atom->type != XML_REGEXP_RANGES) {
802 ERROR("add range: atom is not ranges");
803 return;
804 }
805 if (atom->maxRanges == 0) {
806 atom->maxRanges = 4;
807 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
808 sizeof(xmlRegRangePtr));
809 if (atom->ranges == NULL) {
810 ERROR("add range: allocation failed");
811 atom->maxRanges = 0;
812 return;
813 }
814 } else if (atom->nbRanges >= atom->maxRanges) {
815 xmlRegRangePtr *tmp;
816 atom->maxRanges *= 2;
817 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
818 sizeof(xmlRegRangePtr));
819 if (tmp == NULL) {
820 ERROR("add range: allocation failed");
821 atom->maxRanges /= 2;
822 return;
823 }
824 atom->ranges = tmp;
825 }
826 range = xmlRegNewRange(ctxt, neg, type, start, end);
827 if (range == NULL)
828 return;
829 range->blockName = blockName;
830 atom->ranges[atom->nbRanges++] = range;
831
832}
833
834static int
835xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
836 if (ctxt->maxCounters == 0) {
837 ctxt->maxCounters = 4;
838 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
839 sizeof(xmlRegCounter));
840 if (ctxt->counters == NULL) {
841 ERROR("reg counter: allocation failed");
842 ctxt->maxCounters = 0;
843 return(-1);
844 }
845 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
846 xmlRegCounter *tmp;
847 ctxt->maxCounters *= 2;
848 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
849 sizeof(xmlRegCounter));
850 if (tmp == NULL) {
851 ERROR("reg counter: allocation failed");
852 ctxt->maxCounters /= 2;
853 return(-1);
854 }
855 ctxt->counters = tmp;
856 }
857 ctxt->counters[ctxt->nbCounters].min = -1;
858 ctxt->counters[ctxt->nbCounters].max = -1;
859 return(ctxt->nbCounters++);
860}
861
862static void
863xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
864 if (atom == NULL) {
865 ERROR("atom push: atom is NULL");
866 return;
867 }
868 if (ctxt->maxAtoms == 0) {
869 ctxt->maxAtoms = 4;
870 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
871 sizeof(xmlRegAtomPtr));
872 if (ctxt->atoms == NULL) {
873 ERROR("atom push: allocation failed");
874 ctxt->maxAtoms = 0;
875 return;
876 }
877 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
878 xmlRegAtomPtr *tmp;
879 ctxt->maxAtoms *= 2;
880 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
881 sizeof(xmlRegAtomPtr));
882 if (tmp == NULL) {
883 ERROR("atom push: allocation failed");
884 ctxt->maxAtoms /= 2;
885 return;
886 }
887 ctxt->atoms = tmp;
888 }
889 atom->no = ctxt->nbAtoms;
890 ctxt->atoms[ctxt->nbAtoms++] = atom;
891}
892
893static void
894xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
895 xmlRegAtomPtr atom, xmlRegStatePtr target,
896 int counter, int count) {
897 if (state == NULL) {
898 ERROR("add state: state is NULL");
899 return;
900 }
901 if (target == NULL) {
902 ERROR("add state: target is NULL");
903 return;
904 }
905 if (state->maxTrans == 0) {
906 state->maxTrans = 4;
907 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
908 sizeof(xmlRegTrans));
909 if (state->trans == NULL) {
910 ERROR("add range: allocation failed");
911 state->maxTrans = 0;
912 return;
913 }
914 } else if (state->nbTrans >= state->maxTrans) {
915 xmlRegTrans *tmp;
916 state->maxTrans *= 2;
917 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
918 sizeof(xmlRegTrans));
919 if (tmp == NULL) {
920 ERROR("add range: allocation failed");
921 state->maxTrans /= 2;
922 return;
923 }
924 state->trans = tmp;
925 }
926#ifdef DEBUG_REGEXP_GRAPH
927 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +0000928 if (count == REGEXP_ALL_COUNTER)
929 printf("all transition");
930 else (count >= 0)
Daniel Veillard4255d502002-04-16 15:50:10 +0000931 printf("count based %d", count);
932 else if (counter >= 0)
933 printf("counted %d", counter);
934 else if (atom == NULL)
935 printf("epsilon transition");
936 printf("\n");
937#endif
938
939 state->trans[state->nbTrans].atom = atom;
940 state->trans[state->nbTrans].to = target->no;
941 state->trans[state->nbTrans].counter = counter;
942 state->trans[state->nbTrans].count = count;
943 state->nbTrans++;
944}
945
946static void
947xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
948 if (ctxt->maxStates == 0) {
949 ctxt->maxStates = 4;
950 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
951 sizeof(xmlRegStatePtr));
952 if (ctxt->states == NULL) {
953 ERROR("add range: allocation failed");
954 ctxt->maxStates = 0;
955 return;
956 }
957 } else if (ctxt->nbStates >= ctxt->maxStates) {
958 xmlRegStatePtr *tmp;
959 ctxt->maxStates *= 2;
960 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
961 sizeof(xmlRegStatePtr));
962 if (tmp == NULL) {
963 ERROR("add range: allocation failed");
964 ctxt->maxStates /= 2;
965 return;
966 }
967 ctxt->states = tmp;
968 }
969 state->no = ctxt->nbStates;
970 ctxt->states[ctxt->nbStates++] = state;
971}
972
973/**
Daniel Veillard7646b182002-04-20 06:41:40 +0000974 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +0000975 * @ctxt: a regexp parser context
976 * @from: the from state
977 * @to: the target state or NULL for building a new one
978 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +0000979 *
980 */
981static void
982xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +0000983 xmlRegStatePtr from, xmlRegStatePtr to,
984 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +0000985 if (to == NULL) {
986 to = xmlRegNewState(ctxt);
987 xmlRegStatePush(ctxt, to);
988 ctxt->state = to;
989 }
Daniel Veillard441bc322002-04-20 17:38:48 +0000990 if (lax)
991 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
992 else
993 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +0000994}
995
996/**
Daniel Veillard4255d502002-04-16 15:50:10 +0000997 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +0000998 * @ctxt: a regexp parser context
999 * @from: the from state
1000 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001001 *
1002 */
1003static void
1004xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1005 xmlRegStatePtr from, xmlRegStatePtr to) {
1006 if (to == NULL) {
1007 to = xmlRegNewState(ctxt);
1008 xmlRegStatePush(ctxt, to);
1009 ctxt->state = to;
1010 }
1011 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1012}
1013
1014/**
1015 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001016 * @ctxt: a regexp parser context
1017 * @from: the from state
1018 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001019 * counter: the counter for that transition
1020 *
1021 */
1022static void
1023xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1024 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1025 if (to == NULL) {
1026 to = xmlRegNewState(ctxt);
1027 xmlRegStatePush(ctxt, to);
1028 ctxt->state = to;
1029 }
1030 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1031}
1032
1033/**
1034 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001035 * @ctxt: a regexp parser context
1036 * @from: the from state
1037 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001038 * counter: the counter for that transition
1039 *
1040 */
1041static void
1042xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1043 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1044 if (to == NULL) {
1045 to = xmlRegNewState(ctxt);
1046 xmlRegStatePush(ctxt, to);
1047 ctxt->state = to;
1048 }
1049 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1050}
1051
1052/**
1053 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001054 * @ctxt: a regexp parser context
1055 * @from: the from state
1056 * @to: the target state or NULL for building a new one
1057 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001058 *
1059 */
1060static void
1061xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1062 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1063 if (atom == NULL) {
1064 ERROR("genrate transition: atom == NULL");
1065 return;
1066 }
1067 if (atom->type == XML_REGEXP_SUBREG) {
1068 /*
1069 * this is a subexpression handling one should not need to
1070 * create a new node excep for XML_REGEXP_QUANT_RANGE.
1071 */
1072 xmlRegAtomPush(ctxt, atom);
1073 if ((to != NULL) && (atom->stop != to) &&
1074 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1075 /*
1076 * Generate an epsilon transition to link to the target
1077 */
1078 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1079 }
1080 switch (atom->quant) {
1081 case XML_REGEXP_QUANT_OPT:
1082 atom->quant = XML_REGEXP_QUANT_ONCE;
1083 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1084 break;
1085 case XML_REGEXP_QUANT_MULT:
1086 atom->quant = XML_REGEXP_QUANT_ONCE;
1087 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1088 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1089 break;
1090 case XML_REGEXP_QUANT_PLUS:
1091 atom->quant = XML_REGEXP_QUANT_ONCE;
1092 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1093 break;
1094 case XML_REGEXP_QUANT_RANGE: {
1095 int counter;
1096 xmlRegStatePtr newstate;
1097
1098 /*
1099 * This one is nasty:
1100 * 1/ register a new counter
1101 * 2/ register an epsilon transition associated to
1102 * this counter going from atom->stop to atom->start
1103 * 3/ create a new state
1104 * 4/ generate a counted transition from atom->stop to
1105 * that state
1106 */
1107 counter = xmlRegGetCounter(ctxt);
1108 ctxt->counters[counter].min = atom->min - 1;
1109 ctxt->counters[counter].max = atom->max - 1;
1110 atom->min = 0;
1111 atom->max = 0;
1112 atom->quant = XML_REGEXP_QUANT_ONCE;
1113 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1114 atom->start, counter);
1115 if (to != NULL) {
1116 newstate = to;
1117 } else {
1118 newstate = xmlRegNewState(ctxt);
1119 xmlRegStatePush(ctxt, newstate);
1120 ctxt->state = newstate;
1121 }
1122 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1123 newstate, counter);
1124 }
1125 default:
1126 break;
1127 }
1128 return;
1129 } else {
1130 if (to == NULL) {
1131 to = xmlRegNewState(ctxt);
1132 xmlRegStatePush(ctxt, to);
1133 }
1134 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
1135 xmlRegAtomPush(ctxt, atom);
1136 ctxt->state = to;
1137 }
1138 switch (atom->quant) {
1139 case XML_REGEXP_QUANT_OPT:
1140 atom->quant = XML_REGEXP_QUANT_ONCE;
1141 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1142 break;
1143 case XML_REGEXP_QUANT_MULT:
1144 atom->quant = XML_REGEXP_QUANT_ONCE;
1145 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1146 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1147 break;
1148 case XML_REGEXP_QUANT_PLUS:
1149 atom->quant = XML_REGEXP_QUANT_ONCE;
1150 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1151 break;
1152 default:
1153 break;
1154 }
1155}
1156
1157/**
1158 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001159 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001160 * @fromnr: the from state
1161 * @tonr: the to state
1162 * @cpunter: should that transition be associted to a counted
1163 *
1164 */
1165static void
1166xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1167 int tonr, int counter) {
1168 int transnr;
1169 xmlRegStatePtr from;
1170 xmlRegStatePtr to;
1171
1172#ifdef DEBUG_REGEXP_GRAPH
1173 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1174#endif
1175 from = ctxt->states[fromnr];
1176 if (from == NULL)
1177 return;
1178 to = ctxt->states[tonr];
1179 if (to == NULL)
1180 return;
1181 if ((to->mark == XML_REGEXP_MARK_START) ||
1182 (to->mark == XML_REGEXP_MARK_VISITED))
1183 return;
1184
1185 to->mark = XML_REGEXP_MARK_VISITED;
1186 if (to->type == XML_REGEXP_FINAL_STATE) {
1187#ifdef DEBUG_REGEXP_GRAPH
1188 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1189#endif
1190 from->type = XML_REGEXP_FINAL_STATE;
1191 }
1192 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1193 if (to->trans[transnr].atom == NULL) {
1194 /*
1195 * Don't remove counted transitions
1196 * Don't loop either
1197 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001198 if (to->trans[transnr].to != fromnr) {
1199 if (to->trans[transnr].count >= 0) {
1200 int newto = to->trans[transnr].to;
1201
1202 xmlRegStateAddTrans(ctxt, from, NULL,
1203 ctxt->states[newto],
1204 -1, to->trans[transnr].count);
1205 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001206#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001207 printf("Found epsilon trans %d from %d to %d\n",
1208 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001209#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001210 if (to->trans[transnr].counter >= 0) {
1211 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1212 to->trans[transnr].to,
1213 to->trans[transnr].counter);
1214 } else {
1215 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1216 to->trans[transnr].to,
1217 counter);
1218 }
1219 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001220 }
1221 } else {
1222 int newto = to->trans[transnr].to;
1223
Daniel Veillardb509f152002-04-17 16:28:10 +00001224 if (to->trans[transnr].counter >= 0) {
1225 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1226 ctxt->states[newto],
1227 to->trans[transnr].counter, -1);
1228 } else {
1229 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1230 ctxt->states[newto], counter, -1);
1231 }
1232
Daniel Veillard4255d502002-04-16 15:50:10 +00001233 }
1234 }
1235 to->mark = XML_REGEXP_MARK_NORMAL;
1236}
1237
1238/**
1239 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001240 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001241 *
1242 */
1243static void
1244xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1245 int statenr, transnr;
1246 xmlRegStatePtr state;
1247
1248 /*
1249 * build the completed transitions bypassing the epsilons
1250 * Use a marking algorithm to avoid loops
1251 */
1252 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1253 state = ctxt->states[statenr];
1254 if (state == NULL)
1255 continue;
1256 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1257 if ((state->trans[transnr].atom == NULL) &&
1258 (state->trans[transnr].to >= 0)) {
1259 if (state->trans[transnr].to == statenr) {
1260 state->trans[transnr].to = -1;
1261#ifdef DEBUG_REGEXP_GRAPH
1262 printf("Removed loopback epsilon trans %d on %d\n",
1263 transnr, statenr);
1264#endif
1265 } else if (state->trans[transnr].count < 0) {
1266 int newto = state->trans[transnr].to;
1267
1268#ifdef DEBUG_REGEXP_GRAPH
1269 printf("Found epsilon trans %d from %d to %d\n",
1270 transnr, statenr, newto);
1271#endif
1272 state->mark = XML_REGEXP_MARK_START;
1273 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1274 newto, state->trans[transnr].counter);
1275 state->mark = XML_REGEXP_MARK_NORMAL;
1276#ifdef DEBUG_REGEXP_GRAPH
1277 } else {
1278 printf("Found counted transition %d on %d\n",
1279 transnr, statenr);
1280#endif
1281 }
1282 }
1283 }
1284 }
1285 /*
1286 * Eliminate the epsilon transitions
1287 */
1288 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1289 state = ctxt->states[statenr];
1290 if (state == NULL)
1291 continue;
1292 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1293 if ((state->trans[transnr].atom == NULL) &&
1294 (state->trans[transnr].count < 0) &&
1295 (state->trans[transnr].to >= 0)) {
1296 state->trans[transnr].to = -1;
1297 }
1298 }
1299 }
1300}
1301
Daniel Veillarde19fc232002-04-22 16:01:24 +00001302/**
1303 * xmlFACompareAtoms:
1304 * @atom1: an atom
1305 * @atom2: an atom
1306 *
1307 * Compares two atoms to check whether they are equivatents
1308 *
1309 * Returns 1 if yes and 0 otherwise
1310 */
1311static int
1312xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1313 if (atom1 == atom2)
1314 return(1);
1315 if ((atom1 == NULL) || (atom2 == NULL))
1316 return(0);
1317
1318 if (atom1->type != atom2->type)
1319 return(0);
1320 switch (atom1->type) {
1321 case XML_REGEXP_STRING:
1322 return(xmlStrEqual((xmlChar *)atom1->valuep,
1323 (xmlChar *)atom2->valuep));
1324 case XML_REGEXP_EPSILON:
1325 return(1);
1326 case XML_REGEXP_CHARVAL:
1327 return(atom1->codepoint == atom2->codepoint);
1328 case XML_REGEXP_RANGES:
1329 TODO;
1330 return(0);
1331 default:
1332 break;
1333 }
1334 return(1);
1335}
1336
1337/**
1338 * xmlFARecurseDeterminism:
1339 * @ctxt: a regexp parser context
1340 *
1341 * Check whether the associated regexp is determinist,
1342 * should be called after xmlFAEliminateEpsilonTransitions()
1343 *
1344 */
1345static int
1346xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1347 int to, xmlRegAtomPtr atom) {
1348 int ret = 1;
1349 int transnr;
1350 xmlRegTransPtr t1;
1351
1352 if (state == NULL)
1353 return(ret);
1354 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1355 t1 = &(state->trans[transnr]);
1356 /*
1357 * check transitions conflicting with the one looked at
1358 */
1359 if (t1->atom == NULL) {
1360 if (t1->to == -1)
1361 continue;
1362 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1363 to, atom);
1364 if (ret == 0)
1365 return(0);
1366 continue;
1367 }
1368 if (t1->to != to)
1369 continue;
1370 if (xmlFACompareAtoms(t1->atom, atom))
1371 return(0);
1372 }
1373 return(ret);
1374}
1375
1376/**
1377 * xmlFAComputesDeterminism:
1378 * @ctxt: a regexp parser context
1379 *
1380 * Check whether the associated regexp is determinist,
1381 * should be called after xmlFAEliminateEpsilonTransitions()
1382 *
1383 */
1384static int
1385xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1386 int statenr, transnr;
1387 xmlRegStatePtr state;
1388 xmlRegTransPtr t1, t2;
1389 int i;
1390 int ret = 1;
1391
1392 if (ctxt->determinist != -1)
1393 return(ctxt->determinist);
1394
1395 /*
1396 * Check for all states that there isn't 2 transitions
1397 * with the same atom and a different target.
1398 */
1399 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1400 state = ctxt->states[statenr];
1401 if (state == NULL)
1402 continue;
1403 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1404 t1 = &(state->trans[transnr]);
1405 /*
1406 * Determinism checks in case of counted or all transitions
1407 * will have to be handled separately
1408 */
1409 if (t1->atom == NULL)
1410 continue;
1411 if (t1->to == -1) /* eliminated */
1412 continue;
1413 for (i = 0;i < transnr;i++) {
1414 t2 = &(state->trans[i]);
1415 if (t2->to == -1) /* eliminated */
1416 continue;
1417 if (t2->atom != NULL) {
1418 if (t1->to == t2->to) {
1419 if (xmlFACompareAtoms(t1->atom, t2->atom))
1420 t2->to = -1; /* eliminate */
1421 } else {
1422 /* not determinist ! */
1423 if (xmlFACompareAtoms(t1->atom, t2->atom))
1424 ret = 0;
1425 }
1426 } else if (t1->to != -1) {
1427 /*
1428 * do the closure in case of remaining specific
1429 * epsilon transitions like choices or all
1430 */
1431 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1432 t2->to, t2->atom);
1433 if (ret == 0)
1434 return(0);
1435 }
1436 }
1437 if (ret == 0)
1438 break;
1439 }
1440 if (ret == 0)
1441 break;
1442 }
1443 ctxt->determinist = ret;
1444 return(ret);
1445}
1446
Daniel Veillard4255d502002-04-16 15:50:10 +00001447/************************************************************************
1448 * *
1449 * Routines to check input against transition atoms *
1450 * *
1451 ************************************************************************/
1452
1453static int
1454xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1455 int start, int end, const xmlChar *blockName) {
1456 int ret = 0;
1457
1458 switch (type) {
1459 case XML_REGEXP_STRING:
1460 case XML_REGEXP_SUBREG:
1461 case XML_REGEXP_RANGES:
1462 case XML_REGEXP_EPSILON:
1463 return(-1);
1464 case XML_REGEXP_ANYCHAR:
1465 ret = ((codepoint != '\n') && (codepoint != '\r'));
1466 break;
1467 case XML_REGEXP_CHARVAL:
1468 ret = ((codepoint >= start) && (codepoint <= end));
1469 break;
1470 case XML_REGEXP_NOTSPACE:
1471 neg = !neg;
1472 case XML_REGEXP_ANYSPACE:
1473 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1474 (codepoint == '\t') || (codepoint == ' '));
1475 break;
1476 case XML_REGEXP_NOTINITNAME:
1477 neg = !neg;
1478 case XML_REGEXP_INITNAME:
1479 ret = (xmlIsLetter(codepoint) ||
1480 (codepoint == '_') || (codepoint == ':'));
1481 break;
1482 case XML_REGEXP_NOTNAMECHAR:
1483 neg = !neg;
1484 case XML_REGEXP_NAMECHAR:
1485 ret = (xmlIsLetter(codepoint) || xmlIsDigit(codepoint) ||
1486 (codepoint == '.') || (codepoint == '-') ||
1487 (codepoint == '_') || (codepoint == ':') ||
1488 xmlIsCombining(codepoint) || xmlIsExtender(codepoint));
1489 break;
1490 case XML_REGEXP_NOTDECIMAL:
1491 neg = !neg;
1492 case XML_REGEXP_DECIMAL:
1493 ret = xmlUCSIsCatNd(codepoint);
1494 break;
1495 case XML_REGEXP_REALCHAR:
1496 neg = !neg;
1497 case XML_REGEXP_NOTREALCHAR:
1498 ret = xmlUCSIsCatP(codepoint);
1499 if (ret == 0)
1500 ret = xmlUCSIsCatZ(codepoint);
1501 if (ret == 0)
1502 ret = xmlUCSIsCatC(codepoint);
1503 break;
1504 case XML_REGEXP_LETTER:
1505 ret = xmlUCSIsCatL(codepoint);
1506 break;
1507 case XML_REGEXP_LETTER_UPPERCASE:
1508 ret = xmlUCSIsCatLu(codepoint);
1509 break;
1510 case XML_REGEXP_LETTER_LOWERCASE:
1511 ret = xmlUCSIsCatLl(codepoint);
1512 break;
1513 case XML_REGEXP_LETTER_TITLECASE:
1514 ret = xmlUCSIsCatLt(codepoint);
1515 break;
1516 case XML_REGEXP_LETTER_MODIFIER:
1517 ret = xmlUCSIsCatLm(codepoint);
1518 break;
1519 case XML_REGEXP_LETTER_OTHERS:
1520 ret = xmlUCSIsCatLo(codepoint);
1521 break;
1522 case XML_REGEXP_MARK:
1523 ret = xmlUCSIsCatM(codepoint);
1524 break;
1525 case XML_REGEXP_MARK_NONSPACING:
1526 ret = xmlUCSIsCatMn(codepoint);
1527 break;
1528 case XML_REGEXP_MARK_SPACECOMBINING:
1529 ret = xmlUCSIsCatMc(codepoint);
1530 break;
1531 case XML_REGEXP_MARK_ENCLOSING:
1532 ret = xmlUCSIsCatMe(codepoint);
1533 break;
1534 case XML_REGEXP_NUMBER:
1535 ret = xmlUCSIsCatN(codepoint);
1536 break;
1537 case XML_REGEXP_NUMBER_DECIMAL:
1538 ret = xmlUCSIsCatNd(codepoint);
1539 break;
1540 case XML_REGEXP_NUMBER_LETTER:
1541 ret = xmlUCSIsCatNl(codepoint);
1542 break;
1543 case XML_REGEXP_NUMBER_OTHERS:
1544 ret = xmlUCSIsCatNo(codepoint);
1545 break;
1546 case XML_REGEXP_PUNCT:
1547 ret = xmlUCSIsCatP(codepoint);
1548 break;
1549 case XML_REGEXP_PUNCT_CONNECTOR:
1550 ret = xmlUCSIsCatPc(codepoint);
1551 break;
1552 case XML_REGEXP_PUNCT_DASH:
1553 ret = xmlUCSIsCatPd(codepoint);
1554 break;
1555 case XML_REGEXP_PUNCT_OPEN:
1556 ret = xmlUCSIsCatPs(codepoint);
1557 break;
1558 case XML_REGEXP_PUNCT_CLOSE:
1559 ret = xmlUCSIsCatPe(codepoint);
1560 break;
1561 case XML_REGEXP_PUNCT_INITQUOTE:
1562 ret = xmlUCSIsCatPi(codepoint);
1563 break;
1564 case XML_REGEXP_PUNCT_FINQUOTE:
1565 ret = xmlUCSIsCatPf(codepoint);
1566 break;
1567 case XML_REGEXP_PUNCT_OTHERS:
1568 ret = xmlUCSIsCatPo(codepoint);
1569 break;
1570 case XML_REGEXP_SEPAR:
1571 ret = xmlUCSIsCatZ(codepoint);
1572 break;
1573 case XML_REGEXP_SEPAR_SPACE:
1574 ret = xmlUCSIsCatZs(codepoint);
1575 break;
1576 case XML_REGEXP_SEPAR_LINE:
1577 ret = xmlUCSIsCatZl(codepoint);
1578 break;
1579 case XML_REGEXP_SEPAR_PARA:
1580 ret = xmlUCSIsCatZp(codepoint);
1581 break;
1582 case XML_REGEXP_SYMBOL:
1583 ret = xmlUCSIsCatS(codepoint);
1584 break;
1585 case XML_REGEXP_SYMBOL_MATH:
1586 ret = xmlUCSIsCatSm(codepoint);
1587 break;
1588 case XML_REGEXP_SYMBOL_CURRENCY:
1589 ret = xmlUCSIsCatSc(codepoint);
1590 break;
1591 case XML_REGEXP_SYMBOL_MODIFIER:
1592 ret = xmlUCSIsCatSk(codepoint);
1593 break;
1594 case XML_REGEXP_SYMBOL_OTHERS:
1595 ret = xmlUCSIsCatSo(codepoint);
1596 break;
1597 case XML_REGEXP_OTHER:
1598 ret = xmlUCSIsCatC(codepoint);
1599 break;
1600 case XML_REGEXP_OTHER_CONTROL:
1601 ret = xmlUCSIsCatCc(codepoint);
1602 break;
1603 case XML_REGEXP_OTHER_FORMAT:
1604 ret = xmlUCSIsCatCf(codepoint);
1605 break;
1606 case XML_REGEXP_OTHER_PRIVATE:
1607 ret = xmlUCSIsCatCo(codepoint);
1608 break;
1609 case XML_REGEXP_OTHER_NA:
1610 /* ret = xmlUCSIsCatCn(codepoint); */
1611 /* Seems it doesn't exist anymore in recent Unicode releases */
1612 ret = 0;
1613 break;
1614 case XML_REGEXP_BLOCK_NAME:
1615 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
1616 break;
1617 }
1618 if (neg)
1619 return(!ret);
1620 return(ret);
1621}
1622
1623static int
1624xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
1625 int i, ret = 0;
1626 xmlRegRangePtr range;
1627
1628 if ((atom == NULL) || (!xmlIsChar(codepoint)))
1629 return(-1);
1630
1631 switch (atom->type) {
1632 case XML_REGEXP_SUBREG:
1633 case XML_REGEXP_EPSILON:
1634 return(-1);
1635 case XML_REGEXP_CHARVAL:
1636 return(codepoint == atom->codepoint);
1637 case XML_REGEXP_RANGES: {
1638 int accept = 0;
1639 for (i = 0;i < atom->nbRanges;i++) {
1640 range = atom->ranges[i];
1641 if (range->neg) {
1642 ret = xmlRegCheckCharacterRange(range->type, codepoint,
1643 0, range->start, range->end,
1644 range->blockName);
1645 if (ret != 0)
1646 return(0); /* excluded char */
1647 } else {
1648 ret = xmlRegCheckCharacterRange(range->type, codepoint,
1649 0, range->start, range->end,
1650 range->blockName);
1651 if (ret != 0)
1652 accept = 1; /* might still be excluded */
1653 }
1654 }
1655 return(accept);
1656 }
1657 case XML_REGEXP_STRING:
1658 printf("TODO: XML_REGEXP_STRING\n");
1659 return(-1);
1660 case XML_REGEXP_ANYCHAR:
1661 case XML_REGEXP_ANYSPACE:
1662 case XML_REGEXP_NOTSPACE:
1663 case XML_REGEXP_INITNAME:
1664 case XML_REGEXP_NOTINITNAME:
1665 case XML_REGEXP_NAMECHAR:
1666 case XML_REGEXP_NOTNAMECHAR:
1667 case XML_REGEXP_DECIMAL:
1668 case XML_REGEXP_NOTDECIMAL:
1669 case XML_REGEXP_REALCHAR:
1670 case XML_REGEXP_NOTREALCHAR:
1671 case XML_REGEXP_LETTER:
1672 case XML_REGEXP_LETTER_UPPERCASE:
1673 case XML_REGEXP_LETTER_LOWERCASE:
1674 case XML_REGEXP_LETTER_TITLECASE:
1675 case XML_REGEXP_LETTER_MODIFIER:
1676 case XML_REGEXP_LETTER_OTHERS:
1677 case XML_REGEXP_MARK:
1678 case XML_REGEXP_MARK_NONSPACING:
1679 case XML_REGEXP_MARK_SPACECOMBINING:
1680 case XML_REGEXP_MARK_ENCLOSING:
1681 case XML_REGEXP_NUMBER:
1682 case XML_REGEXP_NUMBER_DECIMAL:
1683 case XML_REGEXP_NUMBER_LETTER:
1684 case XML_REGEXP_NUMBER_OTHERS:
1685 case XML_REGEXP_PUNCT:
1686 case XML_REGEXP_PUNCT_CONNECTOR:
1687 case XML_REGEXP_PUNCT_DASH:
1688 case XML_REGEXP_PUNCT_OPEN:
1689 case XML_REGEXP_PUNCT_CLOSE:
1690 case XML_REGEXP_PUNCT_INITQUOTE:
1691 case XML_REGEXP_PUNCT_FINQUOTE:
1692 case XML_REGEXP_PUNCT_OTHERS:
1693 case XML_REGEXP_SEPAR:
1694 case XML_REGEXP_SEPAR_SPACE:
1695 case XML_REGEXP_SEPAR_LINE:
1696 case XML_REGEXP_SEPAR_PARA:
1697 case XML_REGEXP_SYMBOL:
1698 case XML_REGEXP_SYMBOL_MATH:
1699 case XML_REGEXP_SYMBOL_CURRENCY:
1700 case XML_REGEXP_SYMBOL_MODIFIER:
1701 case XML_REGEXP_SYMBOL_OTHERS:
1702 case XML_REGEXP_OTHER:
1703 case XML_REGEXP_OTHER_CONTROL:
1704 case XML_REGEXP_OTHER_FORMAT:
1705 case XML_REGEXP_OTHER_PRIVATE:
1706 case XML_REGEXP_OTHER_NA:
1707 case XML_REGEXP_BLOCK_NAME:
1708 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
1709 (const xmlChar *)atom->valuep);
1710 if (atom->neg)
1711 ret = !ret;
1712 break;
1713 }
1714 return(ret);
1715}
1716
1717/************************************************************************
1718 * *
1719 * Saving an restoring state of an execution context *
1720 * *
1721 ************************************************************************/
1722
1723#ifdef DEBUG_REGEXP_EXEC
1724static void
1725xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
1726 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
1727 if (exec->inputStack != NULL) {
1728 int i;
1729 printf(": ");
1730 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
1731 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
1732 } else {
1733 printf(": %s", &(exec->inputString[exec->index]));
1734 }
1735 printf("\n");
1736}
1737#endif
1738
1739static void
1740xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
1741#ifdef DEBUG_REGEXP_EXEC
1742 printf("saving ");
1743 exec->transno++;
1744 xmlFARegDebugExec(exec);
1745 exec->transno--;
1746#endif
1747
1748 if (exec->maxRollbacks == 0) {
1749 exec->maxRollbacks = 4;
1750 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
1751 sizeof(xmlRegExecRollback));
1752 if (exec->rollbacks == NULL) {
1753 fprintf(stderr, "exec save: allocation failed");
1754 exec->maxRollbacks = 0;
1755 return;
1756 }
1757 memset(exec->rollbacks, 0,
1758 exec->maxRollbacks * sizeof(xmlRegExecRollback));
1759 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
1760 xmlRegExecRollback *tmp;
1761 int len = exec->maxRollbacks;
1762
1763 exec->maxRollbacks *= 2;
1764 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
1765 exec->maxRollbacks * sizeof(xmlRegExecRollback));
1766 if (tmp == NULL) {
1767 fprintf(stderr, "exec save: allocation failed");
1768 exec->maxRollbacks /= 2;
1769 return;
1770 }
1771 exec->rollbacks = tmp;
1772 tmp = &exec->rollbacks[len];
1773 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
1774 }
1775 exec->rollbacks[exec->nbRollbacks].state = exec->state;
1776 exec->rollbacks[exec->nbRollbacks].index = exec->index;
1777 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
1778 if (exec->comp->nbCounters > 0) {
1779 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
1780 exec->rollbacks[exec->nbRollbacks].counts = (int *)
1781 xmlMalloc(exec->comp->nbCounters * sizeof(int));
1782 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
1783 fprintf(stderr, "exec save: allocation failed");
1784 exec->status = -5;
1785 return;
1786 }
1787 }
1788 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
1789 exec->comp->nbCounters * sizeof(int));
1790 }
1791 exec->nbRollbacks++;
1792}
1793
1794static void
1795xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
1796 if (exec->nbRollbacks <= 0) {
1797 exec->status = -1;
1798#ifdef DEBUG_REGEXP_EXEC
1799 printf("rollback failed on empty stack\n");
1800#endif
1801 return;
1802 }
1803 exec->nbRollbacks--;
1804 exec->state = exec->rollbacks[exec->nbRollbacks].state;
1805 exec->index = exec->rollbacks[exec->nbRollbacks].index;
1806 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
1807 if (exec->comp->nbCounters > 0) {
1808 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
1809 fprintf(stderr, "exec save: allocation failed");
1810 exec->status = -6;
1811 return;
1812 }
1813 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
1814 exec->comp->nbCounters * sizeof(int));
1815 }
1816
1817#ifdef DEBUG_REGEXP_EXEC
1818 printf("restored ");
1819 xmlFARegDebugExec(exec);
1820#endif
1821}
1822
1823/************************************************************************
1824 * *
1825 * Verifyer, running an input against a compiled regexp *
1826 * *
1827 ************************************************************************/
1828
1829static int
1830xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
1831 xmlRegExecCtxt execval;
1832 xmlRegExecCtxtPtr exec = &execval;
1833 int ret, codepoint, len;
1834
1835 exec->inputString = content;
1836 exec->index = 0;
1837 exec->determinist = 1;
1838 exec->maxRollbacks = 0;
1839 exec->nbRollbacks = 0;
1840 exec->rollbacks = NULL;
1841 exec->status = 0;
1842 exec->comp = comp;
1843 exec->state = comp->states[0];
1844 exec->transno = 0;
1845 exec->transcount = 0;
1846 if (comp->nbCounters > 0) {
1847 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
1848 if (exec->counts == NULL)
1849 return(-1);
1850 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
1851 } else
1852 exec->counts = NULL;
1853 while ((exec->status == 0) &&
1854 ((exec->inputString[exec->index] != 0) ||
1855 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
1856 xmlRegTransPtr trans;
1857 xmlRegAtomPtr atom;
1858
1859 /*
1860 * End of input on non-terminal state, rollback, however we may
1861 * still have epsilon like transition for counted transitions
1862 * on counters, in that case don't break too early.
1863 */
1864 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
1865 goto rollback;
1866
1867 exec->transcount = 0;
1868 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
1869 trans = &exec->state->trans[exec->transno];
1870 if (trans->to < 0)
1871 continue;
1872 atom = trans->atom;
1873 ret = 0;
1874 if (trans->count >= 0) {
1875 int count;
1876 xmlRegCounterPtr counter;
1877
1878 /*
1879 * A counted transition.
1880 */
1881
1882 count = exec->counts[trans->count];
1883 counter = &exec->comp->counters[trans->count];
1884#ifdef DEBUG_REGEXP_EXEC
1885 printf("testing count %d: val %d, min %d, max %d\n",
1886 trans->count, count, counter->min, counter->max);
1887#endif
1888 ret = ((count >= counter->min) && (count <= counter->max));
1889 } else if (atom == NULL) {
1890 fprintf(stderr, "epsilon transition left at runtime\n");
1891 exec->status = -2;
1892 break;
1893 } else if (exec->inputString[exec->index] != 0) {
1894 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
1895 ret = xmlRegCheckCharacter(atom, codepoint);
1896 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
1897 xmlRegStatePtr to = comp->states[trans->to];
1898
1899 /*
1900 * this is a multiple input sequence
1901 */
1902 if (exec->state->nbTrans > exec->transno + 1) {
1903 xmlFARegExecSave(exec);
1904 }
1905 exec->transcount = 1;
1906 do {
1907 /*
1908 * Try to progress as much as possible on the input
1909 */
1910 if (exec->transcount == atom->max) {
1911 break;
1912 }
1913 exec->index += len;
1914 /*
1915 * End of input: stop here
1916 */
1917 if (exec->inputString[exec->index] == 0) {
1918 exec->index -= len;
1919 break;
1920 }
1921 if (exec->transcount >= atom->min) {
1922 int transno = exec->transno;
1923 xmlRegStatePtr state = exec->state;
1924
1925 /*
1926 * The transition is acceptable save it
1927 */
1928 exec->transno = -1; /* trick */
1929 exec->state = to;
1930 xmlFARegExecSave(exec);
1931 exec->transno = transno;
1932 exec->state = state;
1933 }
1934 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
1935 len);
1936 ret = xmlRegCheckCharacter(atom, codepoint);
1937 exec->transcount++;
1938 } while (ret == 1);
1939 if (exec->transcount < atom->min)
1940 ret = 0;
1941
1942 /*
1943 * If the last check failed but one transition was found
1944 * possible, rollback
1945 */
1946 if (ret < 0)
1947 ret = 0;
1948 if (ret == 0) {
1949 goto rollback;
1950 }
1951 }
1952 }
1953 if (ret == 1) {
1954 if (exec->state->nbTrans > exec->transno + 1) {
1955 xmlFARegExecSave(exec);
1956 }
1957 if (trans->counter >= 0) {
1958#ifdef DEBUG_REGEXP_EXEC
1959 printf("Increasing count %d\n", trans->counter);
1960#endif
1961 exec->counts[trans->counter]++;
1962 }
1963#ifdef DEBUG_REGEXP_EXEC
1964 printf("entering state %d\n", trans->to);
1965#endif
1966 exec->state = comp->states[trans->to];
1967 exec->transno = 0;
1968 if (trans->atom != NULL) {
1969 exec->index += len;
1970 }
1971 goto progress;
1972 } else if (ret < 0) {
1973 exec->status = -4;
1974 break;
1975 }
1976 }
1977 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
1978rollback:
1979 /*
1980 * Failed to find a way out
1981 */
1982 exec->determinist = 0;
1983 xmlFARegExecRollBack(exec);
1984 }
1985progress:
1986 continue;
1987 }
1988 if (exec->rollbacks != NULL) {
1989 if (exec->counts != NULL) {
1990 int i;
1991
1992 for (i = 0;i < exec->maxRollbacks;i++)
1993 if (exec->rollbacks[i].counts != NULL)
1994 xmlFree(exec->rollbacks[i].counts);
1995 }
1996 xmlFree(exec->rollbacks);
1997 }
1998 if (exec->counts != NULL)
1999 xmlFree(exec->counts);
2000 if (exec->status == 0)
2001 return(1);
2002 if (exec->status == -1)
2003 return(0);
2004 return(exec->status);
2005}
2006
2007/************************************************************************
2008 * *
2009 * Progressive interface to the verifyer one atom at a time *
2010 * *
2011 ************************************************************************/
2012
2013/**
2014 * xmlRegExecCtxtPtr:
2015 * @comp: a precompiled regular expression
2016 * @callback: a callback function used for handling progresses in the
2017 * automata matching phase
2018 * @data: the context data associated to the callback in this context
2019 *
2020 * Build a context used for progressive evaluation of a regexp.
2021 */
2022xmlRegExecCtxtPtr
2023xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2024 xmlRegExecCtxtPtr exec;
2025
2026 if (comp == NULL)
2027 return(NULL);
2028 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2029 if (exec == NULL) {
2030 return(NULL);
2031 }
2032 memset(exec, 0, sizeof(xmlRegExecCtxt));
2033 exec->inputString = NULL;
2034 exec->index = 0;
2035 exec->determinist = 1;
2036 exec->maxRollbacks = 0;
2037 exec->nbRollbacks = 0;
2038 exec->rollbacks = NULL;
2039 exec->status = 0;
2040 exec->comp = comp;
2041 exec->state = comp->states[0];
2042 exec->transno = 0;
2043 exec->transcount = 0;
2044 exec->callback = callback;
2045 exec->data = data;
2046 if (comp->nbCounters > 0) {
2047 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2048 if (exec->counts == NULL) {
2049 xmlFree(exec);
2050 return(NULL);
2051 }
2052 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2053 } else
2054 exec->counts = NULL;
2055 exec->inputStackMax = 0;
2056 exec->inputStackNr = 0;
2057 exec->inputStack = NULL;
2058 return(exec);
2059}
2060
2061/**
2062 * xmlRegFreeExecCtxt:
2063 * @exec: a regular expression evaulation context
2064 *
2065 * Free the structures associated to a regular expression evaulation context.
2066 */
2067void
2068xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2069 if (exec == NULL)
2070 return;
2071
2072 if (exec->rollbacks != NULL) {
2073 if (exec->counts != NULL) {
2074 int i;
2075
2076 for (i = 0;i < exec->maxRollbacks;i++)
2077 if (exec->rollbacks[i].counts != NULL)
2078 xmlFree(exec->rollbacks[i].counts);
2079 }
2080 xmlFree(exec->rollbacks);
2081 }
2082 if (exec->counts != NULL)
2083 xmlFree(exec->counts);
2084 if (exec->inputStack != NULL) {
2085 int i;
2086
2087 for (i = 0;i < exec->inputStackNr;i++)
2088 xmlFree(exec->inputStack[i].value);
2089 xmlFree(exec->inputStack);
2090 }
2091 xmlFree(exec);
2092}
2093
2094static void
2095xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2096 void *data) {
2097#ifdef DEBUG_PUSH
2098 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2099#endif
2100 if (exec->inputStackMax == 0) {
2101 exec->inputStackMax = 4;
2102 exec->inputStack = (xmlRegInputTokenPtr)
2103 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2104 if (exec->inputStack == NULL) {
2105 fprintf(stderr, "push input: allocation failed");
2106 exec->inputStackMax = 0;
2107 return;
2108 }
2109 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2110 xmlRegInputTokenPtr tmp;
2111
2112 exec->inputStackMax *= 2;
2113 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2114 exec->inputStackMax * sizeof(xmlRegInputToken));
2115 if (tmp == NULL) {
2116 fprintf(stderr, "push input: allocation failed");
2117 exec->inputStackMax /= 2;
2118 return;
2119 }
2120 exec->inputStack = tmp;
2121 }
2122 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2123 exec->inputStack[exec->inputStackNr].data = data;
2124 exec->inputStackNr++;
2125 exec->inputStack[exec->inputStackNr].value = NULL;
2126 exec->inputStack[exec->inputStackNr].data = NULL;
2127}
2128
2129
2130/**
2131 * xmlRegExecPushString:
2132 * @exec: a regexp execution context
2133 * @value: a string token input
2134 * @data: data associated to the token to reuse in callbacks
2135 *
2136 * Push one input token in the execution context
2137 *
2138 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2139 * a negative value in case of error.
2140 */
2141int
2142xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2143 void *data) {
2144 xmlRegTransPtr trans;
2145 xmlRegAtomPtr atom;
2146 int ret;
2147 int final = 0;
2148
2149 if (exec == NULL)
2150 return(-1);
2151 if (exec->status != 0)
2152 return(exec->status);
2153
2154 if (value == NULL) {
2155 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2156 return(1);
2157 final = 1;
2158 }
2159
2160#ifdef DEBUG_PUSH
2161 printf("value pushed: %s\n", value);
2162#endif
2163 /*
2164 * If we have an active rollback stack push the new value there
2165 * and get back to where we were left
2166 */
2167 if ((value != NULL) && (exec->inputStackNr > 0)) {
2168 xmlFARegExecSaveInputString(exec, value, data);
2169 value = exec->inputStack[exec->index].value;
2170 data = exec->inputStack[exec->index].data;
2171#ifdef DEBUG_PUSH
2172 printf("value loaded: %s\n", value);
2173#endif
2174 }
2175
2176 while ((exec->status == 0) &&
2177 ((value != NULL) ||
2178 ((final == 1) &&
2179 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2180
2181 /*
2182 * End of input on non-terminal state, rollback, however we may
2183 * still have epsilon like transition for counted transitions
2184 * on counters, in that case don't break too early.
2185 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002186 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002187 goto rollback;
2188
2189 exec->transcount = 0;
2190 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2191 trans = &exec->state->trans[exec->transno];
2192 if (trans->to < 0)
2193 continue;
2194 atom = trans->atom;
2195 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002196 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2197 int i;
2198 int count;
2199 xmlRegTransPtr t;
2200 xmlRegCounterPtr counter;
2201
2202 ret = 0;
2203
2204#ifdef DEBUG_PUSH
2205 printf("testing all lax %d\n", trans->count);
2206#endif
2207 /*
2208 * Check all counted transitions from the current state
2209 */
2210 if ((value == NULL) && (final)) {
2211 ret = 1;
2212 } else if (value != NULL) {
2213 for (i = 0;i < exec->state->nbTrans;i++) {
2214 t = &exec->state->trans[i];
2215 if ((t->counter < 0) || (t == trans))
2216 continue;
2217 counter = &exec->comp->counters[t->counter];
2218 count = exec->counts[t->counter];
2219 if ((count < counter->max) &&
2220 (t->atom != NULL) &&
2221 (xmlStrEqual(value, t->atom->valuep))) {
2222 ret = 0;
2223 break;
2224 }
2225 if ((count >= counter->min) &&
2226 (count < counter->max) &&
2227 (xmlStrEqual(value, t->atom->valuep))) {
2228 ret = 1;
2229 break;
2230 }
2231 }
2232 }
2233 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002234 int i;
2235 int count;
2236 xmlRegTransPtr t;
2237 xmlRegCounterPtr counter;
2238
2239 ret = 1;
2240
2241#ifdef DEBUG_PUSH
2242 printf("testing all %d\n", trans->count);
2243#endif
2244 /*
2245 * Check all counted transitions from the current state
2246 */
2247 for (i = 0;i < exec->state->nbTrans;i++) {
2248 t = &exec->state->trans[i];
2249 if ((t->counter < 0) || (t == trans))
2250 continue;
2251 counter = &exec->comp->counters[t->counter];
2252 count = exec->counts[t->counter];
2253 if ((count < counter->min) || (count > counter->max)) {
2254 ret = 0;
2255 break;
2256 }
2257 }
2258 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002259 int count;
2260 xmlRegCounterPtr counter;
2261
2262 /*
2263 * A counted transition.
2264 */
2265
2266 count = exec->counts[trans->count];
2267 counter = &exec->comp->counters[trans->count];
2268#ifdef DEBUG_PUSH
2269 printf("testing count %d: val %d, min %d, max %d\n",
2270 trans->count, count, counter->min, counter->max);
2271#endif
2272 ret = ((count >= counter->min) && (count <= counter->max));
2273 } else if (atom == NULL) {
2274 fprintf(stderr, "epsilon transition left at runtime\n");
2275 exec->status = -2;
2276 break;
2277 } else if (value != NULL) {
2278 ret = xmlStrEqual(value, atom->valuep);
Daniel Veillard441bc322002-04-20 17:38:48 +00002279 if ((ret == 1) && (trans->counter >= 0)) {
2280 xmlRegCounterPtr counter;
2281 int count;
2282
2283 count = exec->counts[trans->counter];
2284 counter = &exec->comp->counters[trans->counter];
2285 if (count >= counter->max)
2286 ret = 0;
2287 }
2288
Daniel Veillard4255d502002-04-16 15:50:10 +00002289 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2290 xmlRegStatePtr to = exec->comp->states[trans->to];
2291
2292 /*
2293 * this is a multiple input sequence
2294 */
2295 if (exec->state->nbTrans > exec->transno + 1) {
2296 if (exec->inputStackNr <= 0) {
2297 xmlFARegExecSaveInputString(exec, value, data);
2298 }
2299 xmlFARegExecSave(exec);
2300 }
2301 exec->transcount = 1;
2302 do {
2303 /*
2304 * Try to progress as much as possible on the input
2305 */
2306 if (exec->transcount == atom->max) {
2307 break;
2308 }
2309 exec->index++;
2310 value = exec->inputStack[exec->index].value;
2311 data = exec->inputStack[exec->index].data;
2312#ifdef DEBUG_PUSH
2313 printf("value loaded: %s\n", value);
2314#endif
2315
2316 /*
2317 * End of input: stop here
2318 */
2319 if (value == NULL) {
2320 exec->index --;
2321 break;
2322 }
2323 if (exec->transcount >= atom->min) {
2324 int transno = exec->transno;
2325 xmlRegStatePtr state = exec->state;
2326
2327 /*
2328 * The transition is acceptable save it
2329 */
2330 exec->transno = -1; /* trick */
2331 exec->state = to;
2332 if (exec->inputStackNr <= 0) {
2333 xmlFARegExecSaveInputString(exec, value, data);
2334 }
2335 xmlFARegExecSave(exec);
2336 exec->transno = transno;
2337 exec->state = state;
2338 }
2339 ret = xmlStrEqual(value, atom->valuep);
2340 exec->transcount++;
2341 } while (ret == 1);
2342 if (exec->transcount < atom->min)
2343 ret = 0;
2344
2345 /*
2346 * If the last check failed but one transition was found
2347 * possible, rollback
2348 */
2349 if (ret < 0)
2350 ret = 0;
2351 if (ret == 0) {
2352 goto rollback;
2353 }
2354 }
2355 }
2356 if (ret == 1) {
2357 if ((exec->callback != NULL) && (atom != NULL)) {
2358 exec->callback(exec->data, atom->valuep,
2359 atom->data, data);
2360 }
2361 if (exec->state->nbTrans > exec->transno + 1) {
2362 if (exec->inputStackNr <= 0) {
2363 xmlFARegExecSaveInputString(exec, value, data);
2364 }
2365 xmlFARegExecSave(exec);
2366 }
2367 if (trans->counter >= 0) {
2368#ifdef DEBUG_PUSH
2369 printf("Increasing count %d\n", trans->counter);
2370#endif
2371 exec->counts[trans->counter]++;
2372 }
2373#ifdef DEBUG_PUSH
2374 printf("entering state %d\n", trans->to);
2375#endif
2376 exec->state = exec->comp->states[trans->to];
2377 exec->transno = 0;
2378 if (trans->atom != NULL) {
2379 if (exec->inputStack != NULL) {
2380 exec->index++;
2381 if (exec->index < exec->inputStackNr) {
2382 value = exec->inputStack[exec->index].value;
2383 data = exec->inputStack[exec->index].data;
2384#ifdef DEBUG_PUSH
2385 printf("value loaded: %s\n", value);
2386#endif
2387 } else {
2388 value = NULL;
2389 data = NULL;
2390#ifdef DEBUG_PUSH
2391 printf("end of input\n");
2392#endif
2393 }
2394 } else {
2395 value = NULL;
2396 data = NULL;
2397#ifdef DEBUG_PUSH
2398 printf("end of input\n");
2399#endif
2400 }
2401 }
2402 goto progress;
2403 } else if (ret < 0) {
2404 exec->status = -4;
2405 break;
2406 }
2407 }
2408 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2409rollback:
2410 /*
2411 * Failed to find a way out
2412 */
2413 exec->determinist = 0;
2414 xmlFARegExecRollBack(exec);
2415 if (exec->status == 0) {
2416 value = exec->inputStack[exec->index].value;
2417 data = exec->inputStack[exec->index].data;
2418#ifdef DEBUG_PUSH
2419 printf("value loaded: %s\n", value);
2420#endif
2421 }
2422 }
2423progress:
2424 continue;
2425 }
2426 if (exec->status == 0) {
2427 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2428 }
2429 return(exec->status);
2430}
2431
2432#if 0
2433static int
2434xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
2435 xmlRegTransPtr trans;
2436 xmlRegAtomPtr atom;
2437 int ret;
2438 int codepoint, len;
2439
2440 if (exec == NULL)
2441 return(-1);
2442 if (exec->status != 0)
2443 return(exec->status);
2444
2445 while ((exec->status == 0) &&
2446 ((exec->inputString[exec->index] != 0) ||
2447 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2448
2449 /*
2450 * End of input on non-terminal state, rollback, however we may
2451 * still have epsilon like transition for counted transitions
2452 * on counters, in that case don't break too early.
2453 */
2454 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
2455 goto rollback;
2456
2457 exec->transcount = 0;
2458 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2459 trans = &exec->state->trans[exec->transno];
2460 if (trans->to < 0)
2461 continue;
2462 atom = trans->atom;
2463 ret = 0;
2464 if (trans->count >= 0) {
2465 int count;
2466 xmlRegCounterPtr counter;
2467
2468 /*
2469 * A counted transition.
2470 */
2471
2472 count = exec->counts[trans->count];
2473 counter = &exec->comp->counters[trans->count];
2474#ifdef DEBUG_REGEXP_EXEC
2475 printf("testing count %d: val %d, min %d, max %d\n",
2476 trans->count, count, counter->min, counter->max);
2477#endif
2478 ret = ((count >= counter->min) && (count <= counter->max));
2479 } else if (atom == NULL) {
2480 fprintf(stderr, "epsilon transition left at runtime\n");
2481 exec->status = -2;
2482 break;
2483 } else if (exec->inputString[exec->index] != 0) {
2484 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2485 ret = xmlRegCheckCharacter(atom, codepoint);
2486 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2487 xmlRegStatePtr to = exec->comp->states[trans->to];
2488
2489 /*
2490 * this is a multiple input sequence
2491 */
2492 if (exec->state->nbTrans > exec->transno + 1) {
2493 xmlFARegExecSave(exec);
2494 }
2495 exec->transcount = 1;
2496 do {
2497 /*
2498 * Try to progress as much as possible on the input
2499 */
2500 if (exec->transcount == atom->max) {
2501 break;
2502 }
2503 exec->index += len;
2504 /*
2505 * End of input: stop here
2506 */
2507 if (exec->inputString[exec->index] == 0) {
2508 exec->index -= len;
2509 break;
2510 }
2511 if (exec->transcount >= atom->min) {
2512 int transno = exec->transno;
2513 xmlRegStatePtr state = exec->state;
2514
2515 /*
2516 * The transition is acceptable save it
2517 */
2518 exec->transno = -1; /* trick */
2519 exec->state = to;
2520 xmlFARegExecSave(exec);
2521 exec->transno = transno;
2522 exec->state = state;
2523 }
2524 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2525 len);
2526 ret = xmlRegCheckCharacter(atom, codepoint);
2527 exec->transcount++;
2528 } while (ret == 1);
2529 if (exec->transcount < atom->min)
2530 ret = 0;
2531
2532 /*
2533 * If the last check failed but one transition was found
2534 * possible, rollback
2535 */
2536 if (ret < 0)
2537 ret = 0;
2538 if (ret == 0) {
2539 goto rollback;
2540 }
2541 }
2542 }
2543 if (ret == 1) {
2544 if (exec->state->nbTrans > exec->transno + 1) {
2545 xmlFARegExecSave(exec);
2546 }
2547 if (trans->counter >= 0) {
2548#ifdef DEBUG_REGEXP_EXEC
2549 printf("Increasing count %d\n", trans->counter);
2550#endif
2551 exec->counts[trans->counter]++;
2552 }
2553#ifdef DEBUG_REGEXP_EXEC
2554 printf("entering state %d\n", trans->to);
2555#endif
2556 exec->state = exec->comp->states[trans->to];
2557 exec->transno = 0;
2558 if (trans->atom != NULL) {
2559 exec->index += len;
2560 }
2561 goto progress;
2562 } else if (ret < 0) {
2563 exec->status = -4;
2564 break;
2565 }
2566 }
2567 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2568rollback:
2569 /*
2570 * Failed to find a way out
2571 */
2572 exec->determinist = 0;
2573 xmlFARegExecRollBack(exec);
2574 }
2575progress:
2576 continue;
2577 }
2578}
2579#endif
2580/************************************************************************
2581 * *
2582 * Parser for the Shemas Datatype Regular Expressions *
2583 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
2584 * *
2585 ************************************************************************/
2586
2587/**
2588 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00002589 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002590 *
2591 * [10] Char ::= [^.\?*+()|#x5B#x5D]
2592 */
2593static int
2594xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
2595 int cur;
2596 int len;
2597
2598 cur = CUR_SCHAR(ctxt->cur, len);
2599 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
2600 (cur == '*') || (cur == '+') || (cur == '(') ||
2601 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
2602 (cur == 0x5D) || (cur == 0))
2603 return(-1);
2604 return(cur);
2605}
2606
2607/**
2608 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00002609 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002610 *
2611 * [27] charProp ::= IsCategory | IsBlock
2612 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
2613 * Separators | Symbols | Others
2614 * [29] Letters ::= 'L' [ultmo]?
2615 * [30] Marks ::= 'M' [nce]?
2616 * [31] Numbers ::= 'N' [dlo]?
2617 * [32] Punctuation ::= 'P' [cdseifo]?
2618 * [33] Separators ::= 'Z' [slp]?
2619 * [34] Symbols ::= 'S' [mcko]?
2620 * [35] Others ::= 'C' [cfon]?
2621 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
2622 */
2623static void
2624xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
2625 int cur;
2626 xmlRegAtomType type = 0;
2627 xmlChar *blockName = NULL;
2628
2629 cur = CUR;
2630 if (cur == 'L') {
2631 NEXT;
2632 cur = CUR;
2633 if (cur == 'u') {
2634 NEXT;
2635 type = XML_REGEXP_LETTER_UPPERCASE;
2636 } else if (cur == 'l') {
2637 NEXT;
2638 type = XML_REGEXP_LETTER_LOWERCASE;
2639 } else if (cur == 't') {
2640 NEXT;
2641 type = XML_REGEXP_LETTER_TITLECASE;
2642 } else if (cur == 'm') {
2643 NEXT;
2644 type = XML_REGEXP_LETTER_MODIFIER;
2645 } else if (cur == 'o') {
2646 NEXT;
2647 type = XML_REGEXP_LETTER_OTHERS;
2648 } else {
2649 type = XML_REGEXP_LETTER;
2650 }
2651 } else if (cur == 'M') {
2652 NEXT;
2653 cur = CUR;
2654 if (cur == 'n') {
2655 NEXT;
2656 /* nonspacing */
2657 type = XML_REGEXP_MARK_NONSPACING;
2658 } else if (cur == 'c') {
2659 NEXT;
2660 /* spacing combining */
2661 type = XML_REGEXP_MARK_SPACECOMBINING;
2662 } else if (cur == 'e') {
2663 NEXT;
2664 /* enclosing */
2665 type = XML_REGEXP_MARK_ENCLOSING;
2666 } else {
2667 /* all marks */
2668 type = XML_REGEXP_MARK;
2669 }
2670 } else if (cur == 'N') {
2671 NEXT;
2672 cur = CUR;
2673 if (cur == 'd') {
2674 NEXT;
2675 /* digital */
2676 type = XML_REGEXP_NUMBER_DECIMAL;
2677 } else if (cur == 'l') {
2678 NEXT;
2679 /* letter */
2680 type = XML_REGEXP_NUMBER_LETTER;
2681 } else if (cur == 'o') {
2682 NEXT;
2683 /* other */
2684 type = XML_REGEXP_NUMBER_OTHERS;
2685 } else {
2686 /* all numbers */
2687 type = XML_REGEXP_NUMBER;
2688 }
2689 } else if (cur == 'P') {
2690 NEXT;
2691 cur = CUR;
2692 if (cur == 'c') {
2693 NEXT;
2694 /* connector */
2695 type = XML_REGEXP_PUNCT_CONNECTOR;
2696 } else if (cur == 'd') {
2697 NEXT;
2698 /* dash */
2699 type = XML_REGEXP_PUNCT_DASH;
2700 } else if (cur == 's') {
2701 NEXT;
2702 /* open */
2703 type = XML_REGEXP_PUNCT_OPEN;
2704 } else if (cur == 'e') {
2705 NEXT;
2706 /* close */
2707 type = XML_REGEXP_PUNCT_CLOSE;
2708 } else if (cur == 'i') {
2709 NEXT;
2710 /* initial quote */
2711 type = XML_REGEXP_PUNCT_INITQUOTE;
2712 } else if (cur == 'f') {
2713 NEXT;
2714 /* final quote */
2715 type = XML_REGEXP_PUNCT_FINQUOTE;
2716 } else if (cur == 'o') {
2717 NEXT;
2718 /* other */
2719 type = XML_REGEXP_PUNCT_OTHERS;
2720 } else {
2721 /* all punctuation */
2722 type = XML_REGEXP_PUNCT;
2723 }
2724 } else if (cur == 'Z') {
2725 NEXT;
2726 cur = CUR;
2727 if (cur == 's') {
2728 NEXT;
2729 /* space */
2730 type = XML_REGEXP_SEPAR_SPACE;
2731 } else if (cur == 'l') {
2732 NEXT;
2733 /* line */
2734 type = XML_REGEXP_SEPAR_LINE;
2735 } else if (cur == 'p') {
2736 NEXT;
2737 /* paragraph */
2738 type = XML_REGEXP_SEPAR_PARA;
2739 } else {
2740 /* all separators */
2741 type = XML_REGEXP_SEPAR;
2742 }
2743 } else if (cur == 'S') {
2744 NEXT;
2745 cur = CUR;
2746 if (cur == 'm') {
2747 NEXT;
2748 type = XML_REGEXP_SYMBOL_MATH;
2749 /* math */
2750 } else if (cur == 'c') {
2751 NEXT;
2752 type = XML_REGEXP_SYMBOL_CURRENCY;
2753 /* currency */
2754 } else if (cur == 'k') {
2755 NEXT;
2756 type = XML_REGEXP_SYMBOL_MODIFIER;
2757 /* modifiers */
2758 } else if (cur == 'o') {
2759 NEXT;
2760 type = XML_REGEXP_SYMBOL_OTHERS;
2761 /* other */
2762 } else {
2763 /* all symbols */
2764 type = XML_REGEXP_SYMBOL;
2765 }
2766 } else if (cur == 'C') {
2767 NEXT;
2768 cur = CUR;
2769 if (cur == 'c') {
2770 NEXT;
2771 /* control */
2772 type = XML_REGEXP_OTHER_CONTROL;
2773 } else if (cur == 'f') {
2774 NEXT;
2775 /* format */
2776 type = XML_REGEXP_OTHER_FORMAT;
2777 } else if (cur == 'o') {
2778 NEXT;
2779 /* private use */
2780 type = XML_REGEXP_OTHER_PRIVATE;
2781 } else if (cur == 'n') {
2782 NEXT;
2783 /* not assigned */
2784 type = XML_REGEXP_OTHER_NA;
2785 } else {
2786 /* all others */
2787 type = XML_REGEXP_OTHER;
2788 }
2789 } else if (cur == 'I') {
2790 const xmlChar *start;
2791 NEXT;
2792 cur = CUR;
2793 if (cur != 's') {
2794 ERROR("IsXXXX expected");
2795 return;
2796 }
2797 NEXT;
2798 start = ctxt->cur;
2799 cur = CUR;
2800 if (((cur >= 'a') && (cur <= 'z')) ||
2801 ((cur >= 'A') && (cur <= 'Z')) ||
2802 ((cur >= '0') && (cur <= '9')) ||
2803 (cur == 0x2D)) {
2804 NEXT;
2805 cur = CUR;
2806 while (((cur >= 'a') && (cur <= 'z')) ||
2807 ((cur >= 'A') && (cur <= 'Z')) ||
2808 ((cur >= '0') && (cur <= '9')) ||
2809 (cur == 0x2D)) {
2810 NEXT;
2811 cur = CUR;
2812 }
2813 }
2814 type = XML_REGEXP_BLOCK_NAME;
2815 blockName = xmlStrndup(start, ctxt->cur - start);
2816 } else {
2817 ERROR("Unknown char property");
2818 return;
2819 }
2820 if (ctxt->atom == NULL) {
2821 ctxt->atom = xmlRegNewAtom(ctxt, type);
2822 if (ctxt->atom != NULL)
2823 ctxt->atom->valuep = blockName;
2824 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
2825 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
2826 type, 0, 0, blockName);
2827 }
2828}
2829
2830/**
2831 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00002832 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002833 *
2834 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
2835 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
2836 * [25] catEsc ::= '\p{' charProp '}'
2837 * [26] complEsc ::= '\P{' charProp '}'
2838 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
2839 */
2840static void
2841xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
2842 int cur;
2843
2844 if (CUR == '.') {
2845 if (ctxt->atom == NULL) {
2846 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
2847 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
2848 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
2849 XML_REGEXP_ANYCHAR, 0, 0, NULL);
2850 }
2851 NEXT;
2852 return;
2853 }
2854 if (CUR != '\\') {
2855 ERROR("Escaped sequence: expecting \\");
2856 return;
2857 }
2858 NEXT;
2859 cur = CUR;
2860 if (cur == 'p') {
2861 NEXT;
2862 if (CUR != '{') {
2863 ERROR("Expecting '{'");
2864 return;
2865 }
2866 NEXT;
2867 xmlFAParseCharProp(ctxt);
2868 if (CUR != '}') {
2869 ERROR("Expecting '}'");
2870 return;
2871 }
2872 NEXT;
2873 } else if (cur == 'P') {
2874 NEXT;
2875 if (CUR != '{') {
2876 ERROR("Expecting '{'");
2877 return;
2878 }
2879 NEXT;
2880 xmlFAParseCharProp(ctxt);
2881 ctxt->atom->neg = 1;
2882 if (CUR != '}') {
2883 ERROR("Expecting '}'");
2884 return;
2885 }
2886 NEXT;
2887 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
2888 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
2889 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
2890 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
2891 (cur == 0x5E)) {
2892 if (ctxt->atom == NULL) {
2893 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
2894 if (ctxt->atom != NULL)
2895 ctxt->atom->codepoint = cur;
2896 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
2897 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
2898 XML_REGEXP_CHARVAL, cur, cur, NULL);
2899 }
2900 NEXT;
2901 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
2902 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
2903 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00002904 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00002905
2906 switch (cur) {
2907 case 's':
2908 type = XML_REGEXP_ANYSPACE;
2909 break;
2910 case 'S':
2911 type = XML_REGEXP_NOTSPACE;
2912 break;
2913 case 'i':
2914 type = XML_REGEXP_INITNAME;
2915 break;
2916 case 'I':
2917 type = XML_REGEXP_NOTINITNAME;
2918 break;
2919 case 'c':
2920 type = XML_REGEXP_NAMECHAR;
2921 break;
2922 case 'C':
2923 type = XML_REGEXP_NOTNAMECHAR;
2924 break;
2925 case 'd':
2926 type = XML_REGEXP_DECIMAL;
2927 break;
2928 case 'D':
2929 type = XML_REGEXP_NOTDECIMAL;
2930 break;
2931 case 'w':
2932 type = XML_REGEXP_REALCHAR;
2933 break;
2934 case 'W':
2935 type = XML_REGEXP_NOTREALCHAR;
2936 break;
2937 }
2938 NEXT;
2939 if (ctxt->atom == NULL) {
2940 ctxt->atom = xmlRegNewAtom(ctxt, type);
2941 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
2942 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
2943 type, 0, 0, NULL);
2944 }
2945 }
2946}
2947
2948/**
2949 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00002950 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00002951 *
2952 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
2953 */
2954static int
2955xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
2956 int ret = 0, cur;
2957
2958 if ((CUR != '&') || (NXT(1) != '#'))
2959 return(-1);
2960 NEXT;
2961 NEXT;
2962 cur = CUR;
2963 if (cur == 'x') {
2964 NEXT;
2965 cur = CUR;
2966 if (((cur >= '0') && (cur <= '9')) ||
2967 ((cur >= 'a') && (cur <= 'f')) ||
2968 ((cur >= 'A') && (cur <= 'F'))) {
2969 while (((cur >= '0') && (cur <= '9')) ||
2970 ((cur >= 'A') && (cur <= 'F'))) {
2971 if ((cur >= '0') && (cur <= '9'))
2972 ret = ret * 16 + cur - '0';
2973 else if ((cur >= 'a') && (cur <= 'f'))
2974 ret = ret * 16 + 10 + (cur - 'a');
2975 else
2976 ret = ret * 16 + 10 + (cur - 'A');
2977 NEXT;
2978 cur = CUR;
2979 }
2980 } else {
2981 ERROR("Char ref: expecting [0-9A-F]");
2982 return(-1);
2983 }
2984 } else {
2985 if ((cur >= '0') && (cur <= '9')) {
2986 while ((cur >= '0') && (cur <= '9')) {
2987 ret = ret * 10 + cur - '0';
2988 NEXT;
2989 cur = CUR;
2990 }
2991 } else {
2992 ERROR("Char ref: expecting [0-9]");
2993 return(-1);
2994 }
2995 }
2996 if (cur != ';') {
2997 ERROR("Char ref: expecting ';'");
2998 return(-1);
2999 } else {
3000 NEXT;
3001 }
3002 return(ret);
3003}
3004
3005/**
3006 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003007 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003008 *
3009 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3010 * [18] seRange ::= charOrEsc '-' charOrEsc
3011 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3012 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3013 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3014 */
3015static void
3016xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
3017 int cur;
3018 int start = -1;
3019 int end = -1;
3020
3021 if ((CUR == '&') && (NXT(1) == '#')) {
3022 end = start = xmlFAParseCharRef(ctxt);
3023 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3024 XML_REGEXP_CHARVAL, start, end, NULL);
3025 return;
3026 }
3027 cur = CUR;
3028 if (cur == '\\') {
3029 NEXT;
3030 cur = CUR;
3031 switch (cur) {
3032 case 'n': start = 0xA; break;
3033 case 'r': start = 0xD; break;
3034 case 't': start = 0x9; break;
3035 case '\\': case '|': case '.': case '-': case '^': case '?':
3036 case '*': case '+': case '{': case '}': case '(': case ')':
3037 case '[': case ']':
3038 start = cur; break;
3039 default:
3040 ERROR("Invalid escape value");
3041 return;
3042 }
3043 end = start;
3044 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3045 end = start = cur;
3046 } else {
3047 ERROR("Expecting a char range");
3048 return;
3049 }
3050 NEXT;
3051 if (start == '-') {
3052 return;
3053 }
3054 cur = CUR;
3055 if (cur != '-') {
3056 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3057 XML_REGEXP_CHARVAL, start, end, NULL);
3058 return;
3059 }
3060 NEXT;
3061 cur = CUR;
3062 if (cur == '\\') {
3063 NEXT;
3064 cur = CUR;
3065 switch (cur) {
3066 case 'n': end = 0xA; break;
3067 case 'r': end = 0xD; break;
3068 case 't': end = 0x9; break;
3069 case '\\': case '|': case '.': case '-': case '^': case '?':
3070 case '*': case '+': case '{': case '}': case '(': case ')':
3071 case '[': case ']':
3072 end = cur; break;
3073 default:
3074 ERROR("Invalid escape value");
3075 return;
3076 }
3077 } else if ((cur != 0x5B) && (cur != 0x5D)) {
3078 end = cur;
3079 } else {
3080 ERROR("Expecting the end of a char range");
3081 return;
3082 }
3083 NEXT;
3084 /* TODO check that the values are acceptable character ranges for XML */
3085 if (end < start) {
3086 ERROR("End of range is before start of range");
3087 } else {
3088 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3089 XML_REGEXP_CHARVAL, start, end, NULL);
3090 }
3091 return;
3092}
3093
3094/**
3095 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003096 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003097 *
3098 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3099 */
3100static void
3101xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3102 do {
3103 if ((CUR == '\\') || (CUR == '.')) {
3104 xmlFAParseCharClassEsc(ctxt);
3105 } else {
3106 xmlFAParseCharRange(ctxt);
3107 }
3108 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3109 (ctxt->error == 0));
3110}
3111
3112/**
3113 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003114 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003115 *
3116 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3117 * [15] negCharGroup ::= '^' posCharGroup
3118 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3119 * [12] charClassExpr ::= '[' charGroup ']'
3120 */
3121static void
3122xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3123 int n = ctxt->neg;
3124 while ((CUR != ']') && (ctxt->error == 0)) {
3125 if (CUR == '^') {
3126 int neg = ctxt->neg;
3127
3128 NEXT;
3129 ctxt->neg = !ctxt->neg;
3130 xmlFAParsePosCharGroup(ctxt);
3131 ctxt->neg = neg;
3132 } else if (CUR == '-') {
3133 NEXT;
3134 ctxt->neg = !ctxt->neg;
3135 if (CUR != '[') {
3136 ERROR("charClassExpr: '[' expected");
3137 break;
3138 }
3139 NEXT;
3140 xmlFAParseCharGroup(ctxt);
3141 if (CUR == ']') {
3142 NEXT;
3143 } else {
3144 ERROR("charClassExpr: ']' expected");
3145 break;
3146 }
3147 break;
3148 } else if (CUR != ']') {
3149 xmlFAParsePosCharGroup(ctxt);
3150 }
3151 }
3152 ctxt->neg = n;
3153}
3154
3155/**
3156 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003157 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003158 *
3159 * [11] charClass ::= charClassEsc | charClassExpr
3160 * [12] charClassExpr ::= '[' charGroup ']'
3161 */
3162static void
3163xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3164 if (CUR == '[') {
3165 NEXT;
3166 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3167 if (ctxt->atom == NULL)
3168 return;
3169 xmlFAParseCharGroup(ctxt);
3170 if (CUR == ']') {
3171 NEXT;
3172 } else {
3173 ERROR("xmlFAParseCharClass: ']' expected");
3174 }
3175 } else {
3176 xmlFAParseCharClassEsc(ctxt);
3177 }
3178}
3179
3180/**
3181 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003182 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003183 *
3184 * [8] QuantExact ::= [0-9]+
3185 */
3186static int
3187xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3188 int ret = 0;
3189 int ok = 0;
3190
3191 while ((CUR >= '0') && (CUR <= '9')) {
3192 ret = ret * 10 + (CUR - '0');
3193 ok = 1;
3194 NEXT;
3195 }
3196 if (ok != 1) {
3197 return(-1);
3198 }
3199 return(ret);
3200}
3201
3202/**
3203 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003204 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003205 *
3206 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3207 * [5] quantity ::= quantRange | quantMin | QuantExact
3208 * [6] quantRange ::= QuantExact ',' QuantExact
3209 * [7] quantMin ::= QuantExact ','
3210 * [8] QuantExact ::= [0-9]+
3211 */
3212static int
3213xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3214 int cur;
3215
3216 cur = CUR;
3217 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3218 if (ctxt->atom != NULL) {
3219 if (cur == '?')
3220 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3221 else if (cur == '*')
3222 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3223 else if (cur == '+')
3224 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3225 }
3226 NEXT;
3227 return(1);
3228 }
3229 if (cur == '{') {
3230 int min = 0, max = 0;
3231
3232 NEXT;
3233 cur = xmlFAParseQuantExact(ctxt);
3234 if (cur >= 0)
3235 min = cur;
3236 if (CUR == ',') {
3237 NEXT;
3238 cur = xmlFAParseQuantExact(ctxt);
3239 if (cur >= 0)
3240 max = cur;
3241 }
3242 if (CUR == '}') {
3243 NEXT;
3244 } else {
3245 ERROR("Unterminated quantifier");
3246 }
3247 if (max == 0)
3248 max = min;
3249 if (ctxt->atom != NULL) {
3250 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3251 ctxt->atom->min = min;
3252 ctxt->atom->max = max;
3253 }
3254 return(1);
3255 }
3256 return(0);
3257}
3258
3259/**
3260 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003261 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003262 *
3263 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3264 */
3265static int
3266xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3267 int codepoint, len;
3268
3269 codepoint = xmlFAIsChar(ctxt);
3270 if (codepoint > 0) {
3271 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3272 if (ctxt->atom == NULL)
3273 return(-1);
3274 codepoint = CUR_SCHAR(ctxt->cur, len);
3275 ctxt->atom->codepoint = codepoint;
3276 NEXTL(len);
3277 return(1);
3278 } else if (CUR == '|') {
3279 return(0);
3280 } else if (CUR == 0) {
3281 return(0);
3282 } else if (CUR == ')') {
3283 return(0);
3284 } else if (CUR == '(') {
3285 xmlRegStatePtr start, oldend;
3286
3287 NEXT;
3288 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3289 start = ctxt->state;
3290 oldend = ctxt->end;
3291 ctxt->end = NULL;
3292 ctxt->atom = NULL;
3293 xmlFAParseRegExp(ctxt, 0);
3294 if (CUR == ')') {
3295 NEXT;
3296 } else {
3297 ERROR("xmlFAParseAtom: expecting ')'");
3298 }
3299 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3300 if (ctxt->atom == NULL)
3301 return(-1);
3302 ctxt->atom->start = start;
3303 ctxt->atom->stop = ctxt->state;
3304 ctxt->end = oldend;
3305 return(1);
3306 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3307 xmlFAParseCharClass(ctxt);
3308 return(1);
3309 }
3310 return(0);
3311}
3312
3313/**
3314 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003315 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003316 *
3317 * [3] piece ::= atom quantifier?
3318 */
3319static int
3320xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3321 int ret;
3322
3323 ctxt->atom = NULL;
3324 ret = xmlFAParseAtom(ctxt);
3325 if (ret == 0)
3326 return(0);
3327 if (ctxt->atom == NULL) {
3328 ERROR("internal: no atom generated");
3329 }
3330 xmlFAParseQuantifier(ctxt);
3331 return(1);
3332}
3333
3334/**
3335 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003336 * @ctxt: a regexp parser context
3337 * @first: is taht the first
Daniel Veillard4255d502002-04-16 15:50:10 +00003338 *
3339 * [2] branch ::= piece*
3340 */
3341static void
3342xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, int first) {
3343 xmlRegStatePtr previous;
3344 xmlRegAtomPtr prevatom = NULL;
3345 int ret;
3346
3347 previous = ctxt->state;
3348 ret = xmlFAParsePiece(ctxt);
3349 if (ret != 0) {
3350 if (first) {
3351 xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom);
3352 previous = ctxt->state;
3353 } else {
3354 prevatom = ctxt->atom;
3355 }
3356 ctxt->atom = NULL;
3357 }
3358 while ((ret != 0) && (ctxt->error == 0)) {
3359 ret = xmlFAParsePiece(ctxt);
3360 if (ret != 0) {
3361 if (first) {
3362 xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom);
3363 } else {
3364 xmlFAGenerateTransitions(ctxt, previous, NULL, prevatom);
3365 prevatom = ctxt->atom;
3366 }
3367 previous = ctxt->state;
3368 ctxt->atom = NULL;
3369 }
3370 }
3371 if (!first) {
3372 xmlFAGenerateTransitions(ctxt, previous, ctxt->end, prevatom);
3373 }
3374}
3375
3376/**
3377 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003378 * @ctxt: a regexp parser context
3379 * @top: is that the top-level expressions ?
Daniel Veillard4255d502002-04-16 15:50:10 +00003380 *
3381 * [1] regExp ::= branch ( '|' branch )*
3382 */
3383static void
3384xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
3385 xmlRegStatePtr start, end, oldend;
3386
3387 oldend = ctxt->end;
3388
3389 start = ctxt->state;
3390 xmlFAParseBranch(ctxt, (ctxt->end == NULL));
3391 if (CUR != '|') {
3392 ctxt->end = ctxt->state;
3393 return;
3394 }
3395 end = ctxt->state;
3396 while ((CUR == '|') && (ctxt->error == 0)) {
3397 NEXT;
3398 ctxt->state = start;
3399 ctxt->end = end;
3400 xmlFAParseBranch(ctxt, 0);
3401 }
3402 if (!top)
3403 ctxt->end = oldend;
3404}
3405
3406/************************************************************************
3407 * *
3408 * The basic API *
3409 * *
3410 ************************************************************************/
3411
3412/**
3413 * xmlRegexpPrint:
3414 * @output: the file for the output debug
3415 * @regexp: the compiled regexp
3416 *
3417 * Print the content of the compiled regular expression
3418 */
3419void
3420xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
3421 int i;
3422
3423 fprintf(output, " regexp: ");
3424 if (regexp == NULL) {
3425 fprintf(output, "NULL\n");
3426 return;
3427 }
3428 fprintf(output, "'%s' ", regexp->string);
3429 fprintf(output, "\n");
3430 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
3431 for (i = 0;i < regexp->nbAtoms; i++) {
3432 fprintf(output, " %02d ", i);
3433 xmlRegPrintAtom(output, regexp->atoms[i]);
3434 }
3435 fprintf(output, "%d states:", regexp->nbStates);
3436 fprintf(output, "\n");
3437 for (i = 0;i < regexp->nbStates; i++) {
3438 xmlRegPrintState(output, regexp->states[i]);
3439 }
3440 fprintf(output, "%d counters:\n", regexp->nbCounters);
3441 for (i = 0;i < regexp->nbCounters; i++) {
3442 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
3443 regexp->counters[i].max);
3444 }
3445}
3446
3447/**
3448 * xmlRegexpCompile:
3449 * @regexp: a regular expression string
3450 *
3451 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
3452 * Appendix F and build an automata suitable for testing strings against
3453 * that regular expression
3454 *
3455 * Returns the compiled expression or NULL in case of error
3456 */
3457xmlRegexpPtr
3458xmlRegexpCompile(const xmlChar *regexp) {
3459 xmlRegexpPtr ret;
3460 xmlRegParserCtxtPtr ctxt;
3461
3462 ctxt = xmlRegNewParserCtxt(regexp);
3463 if (ctxt == NULL)
3464 return(NULL);
3465
3466 /* initialize the parser */
3467 ctxt->end = NULL;
3468 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3469 xmlRegStatePush(ctxt, ctxt->start);
3470
3471 /* parse the expression building an automata */
3472 xmlFAParseRegExp(ctxt, 1);
3473 if (CUR != 0) {
3474 ERROR("xmlFAParseRegExp: extra characters");
3475 }
3476 ctxt->end = ctxt->state;
3477 ctxt->start->type = XML_REGEXP_START_STATE;
3478 ctxt->end->type = XML_REGEXP_FINAL_STATE;
3479
3480 /* remove the Epsilon except for counted transitions */
3481 xmlFAEliminateEpsilonTransitions(ctxt);
3482
3483
3484 if (ctxt->error != 0) {
3485 xmlRegFreeParserCtxt(ctxt);
3486 return(NULL);
3487 }
3488 ret = xmlRegEpxFromParse(ctxt);
3489 xmlRegFreeParserCtxt(ctxt);
3490 return(ret);
3491}
3492
3493/**
3494 * xmlRegexpExec:
3495 * @comp: the compiled regular expression
3496 * @content: the value to check against the regular expression
3497 *
3498 * Check if the regular expression generate the value
3499 *
3500 * Returns 1 if it matches, 0 if not and a negativa value in case of error
3501 */
3502int
3503xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
3504 if ((comp == NULL) || (content == NULL))
3505 return(-1);
3506 return(xmlFARegExec(comp, content));
3507}
3508
3509/**
3510 * xmlRegFreeRegexp:
3511 * @regexp: the regexp
3512 *
3513 * Free a regexp
3514 */
3515void
3516xmlRegFreeRegexp(xmlRegexpPtr regexp) {
3517 int i;
3518 if (regexp == NULL)
3519 return;
3520
3521 if (regexp->string != NULL)
3522 xmlFree(regexp->string);
3523 if (regexp->states != NULL) {
3524 for (i = 0;i < regexp->nbStates;i++)
3525 xmlRegFreeState(regexp->states[i]);
3526 xmlFree(regexp->states);
3527 }
3528 if (regexp->atoms != NULL) {
3529 for (i = 0;i < regexp->nbAtoms;i++)
3530 xmlRegFreeAtom(regexp->atoms[i]);
3531 xmlFree(regexp->atoms);
3532 }
3533 if (regexp->counters != NULL)
3534 xmlFree(regexp->counters);
3535 xmlFree(regexp);
3536}
3537
3538#ifdef LIBXML_AUTOMATA_ENABLED
3539/************************************************************************
3540 * *
3541 * The Automata interface *
3542 * *
3543 ************************************************************************/
3544
3545/**
3546 * xmlNewAutomata:
3547 *
3548 * Create a new automata
3549 *
3550 * Returns the new object or NULL in case of failure
3551 */
3552xmlAutomataPtr
3553xmlNewAutomata(void) {
3554 xmlAutomataPtr ctxt;
3555
3556 ctxt = xmlRegNewParserCtxt(NULL);
3557 if (ctxt == NULL)
3558 return(NULL);
3559
3560 /* initialize the parser */
3561 ctxt->end = NULL;
3562 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
3563 xmlRegStatePush(ctxt, ctxt->start);
3564
3565 return(ctxt);
3566}
3567
3568/**
3569 * xmlFreeAutomata:
3570 * @am: an automata
3571 *
3572 * Free an automata
3573 */
3574void
3575xmlFreeAutomata(xmlAutomataPtr am) {
3576 if (am == NULL)
3577 return;
3578 xmlRegFreeParserCtxt(am);
3579}
3580
3581/**
3582 * xmlAutomataGetInitState:
3583 * @am: an automata
3584 *
3585 * Returns the initial state of the automata
3586 */
3587xmlAutomataStatePtr
3588xmlAutomataGetInitState(xmlAutomataPtr am) {
3589 if (am == NULL)
3590 return(NULL);
3591 return(am->start);
3592}
3593
3594/**
3595 * xmlAutomataSetFinalState:
3596 * @am: an automata
3597 * @state: a state in this automata
3598 *
3599 * Makes that state a final state
3600 *
3601 * Returns 0 or -1 in case of error
3602 */
3603int
3604xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
3605 if ((am == NULL) || (state == NULL))
3606 return(-1);
3607 state->type = XML_REGEXP_FINAL_STATE;
3608 return(0);
3609}
3610
3611/**
3612 * xmlAutomataNewTransition:
3613 * @am: an automata
3614 * @from: the starting point of the transition
3615 * @to: the target point of the transition or NULL
3616 * @token: the input string associated to that transition
3617 * @data: data passed to the callback function if the transition is activated
3618 *
3619 * If @to is NULL, this create first a new target state in the automata
3620 * and then adds a transition from the @from state to the target state
3621 * activated by the value of @token
3622 *
3623 * Returns the target state or NULL in case of error
3624 */
3625xmlAutomataStatePtr
3626xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
3627 xmlAutomataStatePtr to, const xmlChar *token,
3628 void *data) {
3629 xmlRegAtomPtr atom;
3630
3631 if ((am == NULL) || (from == NULL) || (token == NULL))
3632 return(NULL);
3633 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
3634 atom->data = data;
3635 if (atom == NULL)
3636 return(NULL);
3637 atom->valuep = xmlStrdup(token);
3638
3639 xmlFAGenerateTransitions(am, from, to, atom);
3640 if (to == NULL)
3641 return(am->state);
3642 return(to);
3643}
3644
3645/**
3646 * xmlAutomataNewCountTrans:
3647 * @am: an automata
3648 * @from: the starting point of the transition
3649 * @to: the target point of the transition or NULL
3650 * @token: the input string associated to that transition
3651 * @min: the minimum successive occurences of token
3652 * @min: the maximum successive occurences of token
3653 *
3654 * If @to is NULL, this create first a new target state in the automata
3655 * and then adds a transition from the @from state to the target state
3656 * activated by a succession of input of value @token and whose number
3657 * is between @min and @max
3658 *
3659 * Returns the target state or NULL in case of error
3660 */
3661xmlAutomataStatePtr
3662xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
3663 xmlAutomataStatePtr to, const xmlChar *token,
3664 int min, int max, void *data) {
3665 xmlRegAtomPtr atom;
3666
3667 if ((am == NULL) || (from == NULL) || (token == NULL))
3668 return(NULL);
3669 if (min < 0)
3670 return(NULL);
3671 if ((max < min) || (max < 1))
3672 return(NULL);
3673 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
3674 if (atom == NULL)
3675 return(NULL);
3676 atom->valuep = xmlStrdup(token);
3677 atom->data = data;
3678 if (min == 0)
3679 atom->min = 1;
3680 else
3681 atom->min = min;
3682 atom->max = max;
3683
3684 xmlFAGenerateTransitions(am, from, to, atom);
3685 if (to == NULL)
3686 to = am->state;
3687 if (to == NULL)
3688 return(NULL);
3689 if (min == 0)
3690 xmlFAGenerateEpsilonTransition(am, from, to);
3691 return(to);
3692}
3693
3694/**
Daniel Veillard7646b182002-04-20 06:41:40 +00003695 * xmlAutomataNewOnceTrans:
3696 * @am: an automata
3697 * @from: the starting point of the transition
3698 * @to: the target point of the transition or NULL
3699 * @token: the input string associated to that transition
3700 * @min: the minimum successive occurences of token
3701 * @min: the maximum successive occurences of token
3702 *
3703 * If @to is NULL, this create first a new target state in the automata
3704 * and then adds a transition from the @from state to the target state
3705 * activated by a succession of input of value @token and whose number
3706 * is between @min and @max, moreover that transistion can only be crossed
3707 * once.
3708 *
3709 * Returns the target state or NULL in case of error
3710 */
3711xmlAutomataStatePtr
3712xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
3713 xmlAutomataStatePtr to, const xmlChar *token,
3714 int min, int max, void *data) {
3715 xmlRegAtomPtr atom;
3716 int counter;
3717
3718 if ((am == NULL) || (from == NULL) || (token == NULL))
3719 return(NULL);
3720 if (min < 1)
3721 return(NULL);
3722 if ((max < min) || (max < 1))
3723 return(NULL);
3724 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
3725 if (atom == NULL)
3726 return(NULL);
3727 atom->valuep = xmlStrdup(token);
3728 atom->data = data;
3729 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
3730 if (min == 0)
3731 atom->min = 1;
3732 else
3733 atom->min = min;
3734 atom->max = max;
3735 /*
3736 * associate a counter to the transition.
3737 */
3738 counter = xmlRegGetCounter(am);
3739 am->counters[counter].min = 1;
3740 am->counters[counter].max = 1;
3741
3742 /* xmlFAGenerateTransitions(am, from, to, atom); */
3743 if (to == NULL) {
3744 to = xmlRegNewState(am);
3745 xmlRegStatePush(am, to);
3746 }
3747 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
3748 xmlRegAtomPush(am, atom);
3749 am->state = to;
3750 if (to == NULL)
3751 to = am->state;
3752 if (to == NULL)
3753 return(NULL);
3754 return(to);
3755}
3756
3757/**
Daniel Veillard4255d502002-04-16 15:50:10 +00003758 * xmlAutomataNewState:
3759 * @am: an automata
3760 *
3761 * Create a new disconnected state in the automata
3762 *
3763 * Returns the new state or NULL in case of error
3764 */
3765xmlAutomataStatePtr
3766xmlAutomataNewState(xmlAutomataPtr am) {
3767 xmlAutomataStatePtr to;
3768
3769 if (am == NULL)
3770 return(NULL);
3771 to = xmlRegNewState(am);
3772 xmlRegStatePush(am, to);
3773 return(to);
3774}
3775
3776/**
3777 * xmlAutomataNewTransition:
3778 * @am: an automata
3779 * @from: the starting point of the transition
3780 * @to: the target point of the transition or NULL
3781 *
3782 * If @to is NULL, this create first a new target state in the automata
3783 * and then adds a an epsilon transition from the @from state to the
3784 * target state
3785 *
3786 * Returns the target state or NULL in case of error
3787 */
3788xmlAutomataStatePtr
3789xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
3790 xmlAutomataStatePtr to) {
3791 if ((am == NULL) || (from == NULL))
3792 return(NULL);
3793 xmlFAGenerateEpsilonTransition(am, from, to);
3794 if (to == NULL)
3795 return(am->state);
3796 return(to);
3797}
3798
Daniel Veillardb509f152002-04-17 16:28:10 +00003799/**
Daniel Veillard7646b182002-04-20 06:41:40 +00003800 * xmlAutomataNewAllTrans:
3801 * @am: an automata
3802 * @from: the starting point of the transition
3803 * @to: the target point of the transition or NULL
3804 *
3805 * If @to is NULL, this create first a new target state in the automata
3806 * and then adds a an ALL transition from the @from state to the
3807 * target state. That transition is an epsilon transition allowed only when
3808 * all transitions from the @from node have been activated.
3809 *
3810 * Returns the target state or NULL in case of error
3811 */
3812xmlAutomataStatePtr
3813xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00003814 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00003815 if ((am == NULL) || (from == NULL))
3816 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00003817 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00003818 if (to == NULL)
3819 return(am->state);
3820 return(to);
3821}
3822
3823/**
Daniel Veillardb509f152002-04-17 16:28:10 +00003824 * xmlAutomataNewCounter:
3825 * @am: an automata
3826 * @min: the minimal value on the counter
3827 * @max: the maximal value on the counter
3828 *
3829 * Create a new counter
3830 *
3831 * Returns the counter number or -1 in case of error
3832 */
3833int
3834xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
3835 int ret;
3836
3837 if (am == NULL)
3838 return(-1);
3839
3840 ret = xmlRegGetCounter(am);
3841 if (ret < 0)
3842 return(-1);
3843 am->counters[ret].min = min;
3844 am->counters[ret].max = max;
3845 return(ret);
3846}
3847
3848/**
3849 * xmlAutomataNewCountedTrans:
3850 * @am: an automata
3851 * @from: the starting point of the transition
3852 * @to: the target point of the transition or NULL
3853 * @counter: the counter associated to that transition
3854 *
3855 * If @to is NULL, this create first a new target state in the automata
3856 * and then adds an epsilon transition from the @from state to the target state
3857 * which will increment the counter provided
3858 *
3859 * Returns the target state or NULL in case of error
3860 */
3861xmlAutomataStatePtr
3862xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
3863 xmlAutomataStatePtr to, int counter) {
3864 if ((am == NULL) || (from == NULL) || (counter < 0))
3865 return(NULL);
3866 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
3867 if (to == NULL)
3868 return(am->state);
3869 return(to);
3870}
3871
3872/**
3873 * xmlAutomataNewCounterTrans:
3874 * @am: an automata
3875 * @from: the starting point of the transition
3876 * @to: the target point of the transition or NULL
3877 * @counter: the counter associated to that transition
3878 *
3879 * If @to is NULL, this create first a new target state in the automata
3880 * and then adds an epsilon transition from the @from state to the target state
3881 * which will be allowed only if the counter is within the right range.
3882 *
3883 * Returns the target state or NULL in case of error
3884 */
3885xmlAutomataStatePtr
3886xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
3887 xmlAutomataStatePtr to, int counter) {
3888 if ((am == NULL) || (from == NULL) || (counter < 0))
3889 return(NULL);
3890 xmlFAGenerateCountedTransition(am, from, to, counter);
3891 if (to == NULL)
3892 return(am->state);
3893 return(to);
3894}
Daniel Veillard4255d502002-04-16 15:50:10 +00003895
3896/**
3897 * xmlAutomataCompile:
3898 * @am: an automata
3899 *
3900 * Compile the automata into a Reg Exp ready for being executed.
3901 * The automata should be free after this point.
3902 *
3903 * Returns the compiled regexp or NULL in case of error
3904 */
3905xmlRegexpPtr
3906xmlAutomataCompile(xmlAutomataPtr am) {
3907 xmlRegexpPtr ret;
3908
3909 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillarde19fc232002-04-22 16:01:24 +00003910 xmlFAComputesDeterminism(am);
Daniel Veillard4255d502002-04-16 15:50:10 +00003911 ret = xmlRegEpxFromParse(am);
3912
3913 return(ret);
3914}
Daniel Veillarde19fc232002-04-22 16:01:24 +00003915
3916/**
3917 * xmlAutomataIsDeterminist:
3918 * @am: an automata
3919 *
3920 * Checks if an automata is determinist.
3921 *
3922 * Returns 1 if true, 0 if not, and -1 in case of error
3923 */
3924int
3925xmlAutomataIsDeterminist(xmlAutomataPtr am) {
3926 int ret;
3927
3928 if (am == NULL)
3929 return(-1);
3930
3931 ret = xmlFAComputesDeterminism(am);
3932 return(ret);
3933}
Daniel Veillard4255d502002-04-16 15:50:10 +00003934#endif /* LIBXML_AUTOMATA_ENABLED */
3935#endif /* LIBXML_REGEXP_ENABLED */