blob: 099f68ca45addd937a91c5d307a8209725b04ad7 [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
William M. Brackddf71d62004-05-06 04:17:26 +00006 * XML related specifications these include:
Daniel Veillard4255d502002-04-16 15:50:10 +00007 * - 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
Daniel Veillardfc0b6f62005-01-09 17:48:02 +000022#define DEBUG_ERR
23
Daniel Veillard4255d502002-04-16 15:50:10 +000024#include <stdio.h>
25#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000026#ifdef HAVE_LIMITS_H
27#include <limits.h>
28#endif
29
Daniel Veillard4255d502002-04-16 15:50:10 +000030#include <libxml/tree.h>
31#include <libxml/parserInternals.h>
32#include <libxml/xmlregexp.h>
33#include <libxml/xmlautomata.h>
34#include <libxml/xmlunicode.h>
35
Daniel Veillardebe48c62003-12-03 12:12:27 +000036#ifndef INT_MAX
37#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
38#endif
39
Daniel Veillardc0826a72004-08-10 14:17:33 +000040/* #define DEBUG_REGEXP_GRAPH */
41/* #define DEBUG_REGEXP_EXEC */
Daniel Veillard4255d502002-04-16 15:50:10 +000042/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000043/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000044
Daniel Veillardff46a042003-10-08 08:53:17 +000045#define ERROR(str) \
46 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
47 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000048#define NEXT ctxt->cur++
49#define CUR (*(ctxt->cur))
50#define NXT(index) (ctxt->cur[index])
51
52#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
53#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000054#define XML_REG_STRING_SEPARATOR '|'
Daniel Veillard4255d502002-04-16 15:50:10 +000055
Daniel Veillarde19fc232002-04-22 16:01:24 +000056/**
57 * TODO:
58 *
59 * macro to flag unimplemented blocks
60 */
61#define TODO \
62 xmlGenericError(xmlGenericErrorContext, \
63 "Unimplemented block at %s:%d\n", \
64 __FILE__, __LINE__);
65
Daniel Veillard4255d502002-04-16 15:50:10 +000066/************************************************************************
67 * *
68 * Datatypes and structures *
69 * *
70 ************************************************************************/
71
72typedef enum {
73 XML_REGEXP_EPSILON = 1,
74 XML_REGEXP_CHARVAL,
75 XML_REGEXP_RANGES,
76 XML_REGEXP_SUBREG,
77 XML_REGEXP_STRING,
78 XML_REGEXP_ANYCHAR, /* . */
79 XML_REGEXP_ANYSPACE, /* \s */
80 XML_REGEXP_NOTSPACE, /* \S */
81 XML_REGEXP_INITNAME, /* \l */
82 XML_REGEXP_NOTINITNAME, /* \l */
83 XML_REGEXP_NAMECHAR, /* \c */
84 XML_REGEXP_NOTNAMECHAR, /* \C */
85 XML_REGEXP_DECIMAL, /* \d */
86 XML_REGEXP_NOTDECIMAL, /* \d */
87 XML_REGEXP_REALCHAR, /* \w */
88 XML_REGEXP_NOTREALCHAR, /* \w */
89 XML_REGEXP_LETTER,
90 XML_REGEXP_LETTER_UPPERCASE,
91 XML_REGEXP_LETTER_LOWERCASE,
92 XML_REGEXP_LETTER_TITLECASE,
93 XML_REGEXP_LETTER_MODIFIER,
94 XML_REGEXP_LETTER_OTHERS,
95 XML_REGEXP_MARK,
96 XML_REGEXP_MARK_NONSPACING,
97 XML_REGEXP_MARK_SPACECOMBINING,
98 XML_REGEXP_MARK_ENCLOSING,
99 XML_REGEXP_NUMBER,
100 XML_REGEXP_NUMBER_DECIMAL,
101 XML_REGEXP_NUMBER_LETTER,
102 XML_REGEXP_NUMBER_OTHERS,
103 XML_REGEXP_PUNCT,
104 XML_REGEXP_PUNCT_CONNECTOR,
105 XML_REGEXP_PUNCT_DASH,
106 XML_REGEXP_PUNCT_OPEN,
107 XML_REGEXP_PUNCT_CLOSE,
108 XML_REGEXP_PUNCT_INITQUOTE,
109 XML_REGEXP_PUNCT_FINQUOTE,
110 XML_REGEXP_PUNCT_OTHERS,
111 XML_REGEXP_SEPAR,
112 XML_REGEXP_SEPAR_SPACE,
113 XML_REGEXP_SEPAR_LINE,
114 XML_REGEXP_SEPAR_PARA,
115 XML_REGEXP_SYMBOL,
116 XML_REGEXP_SYMBOL_MATH,
117 XML_REGEXP_SYMBOL_CURRENCY,
118 XML_REGEXP_SYMBOL_MODIFIER,
119 XML_REGEXP_SYMBOL_OTHERS,
120 XML_REGEXP_OTHER,
121 XML_REGEXP_OTHER_CONTROL,
122 XML_REGEXP_OTHER_FORMAT,
123 XML_REGEXP_OTHER_PRIVATE,
124 XML_REGEXP_OTHER_NA,
125 XML_REGEXP_BLOCK_NAME
126} xmlRegAtomType;
127
128typedef enum {
129 XML_REGEXP_QUANT_EPSILON = 1,
130 XML_REGEXP_QUANT_ONCE,
131 XML_REGEXP_QUANT_OPT,
132 XML_REGEXP_QUANT_MULT,
133 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000134 XML_REGEXP_QUANT_ONCEONLY,
135 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000136 XML_REGEXP_QUANT_RANGE
137} xmlRegQuantType;
138
139typedef enum {
140 XML_REGEXP_START_STATE = 1,
141 XML_REGEXP_FINAL_STATE,
142 XML_REGEXP_TRANS_STATE
143} xmlRegStateType;
144
145typedef enum {
146 XML_REGEXP_MARK_NORMAL = 0,
147 XML_REGEXP_MARK_START,
148 XML_REGEXP_MARK_VISITED
149} xmlRegMarkedType;
150
151typedef struct _xmlRegRange xmlRegRange;
152typedef xmlRegRange *xmlRegRangePtr;
153
154struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000155 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000156 xmlRegAtomType type;
157 int start;
158 int end;
159 xmlChar *blockName;
160};
161
162typedef struct _xmlRegAtom xmlRegAtom;
163typedef xmlRegAtom *xmlRegAtomPtr;
164
165typedef struct _xmlAutomataState xmlRegState;
166typedef xmlRegState *xmlRegStatePtr;
167
168struct _xmlRegAtom {
169 int no;
170 xmlRegAtomType type;
171 xmlRegQuantType quant;
172 int min;
173 int max;
174
175 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000176 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000177 int neg;
178 int codepoint;
179 xmlRegStatePtr start;
180 xmlRegStatePtr stop;
181 int maxRanges;
182 int nbRanges;
183 xmlRegRangePtr *ranges;
184 void *data;
185};
186
187typedef struct _xmlRegCounter xmlRegCounter;
188typedef xmlRegCounter *xmlRegCounterPtr;
189
190struct _xmlRegCounter {
191 int min;
192 int max;
193};
194
195typedef struct _xmlRegTrans xmlRegTrans;
196typedef xmlRegTrans *xmlRegTransPtr;
197
198struct _xmlRegTrans {
199 xmlRegAtomPtr atom;
200 int to;
201 int counter;
202 int count;
203};
204
205struct _xmlAutomataState {
206 xmlRegStateType type;
207 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000208 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000209 int no;
210
211 int maxTrans;
212 int nbTrans;
213 xmlRegTrans *trans;
214};
215
216typedef struct _xmlAutomata xmlRegParserCtxt;
217typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
218
219struct _xmlAutomata {
220 xmlChar *string;
221 xmlChar *cur;
222
223 int error;
224 int neg;
225
226 xmlRegStatePtr start;
227 xmlRegStatePtr end;
228 xmlRegStatePtr state;
229
230 xmlRegAtomPtr atom;
231
232 int maxAtoms;
233 int nbAtoms;
234 xmlRegAtomPtr *atoms;
235
236 int maxStates;
237 int nbStates;
238 xmlRegStatePtr *states;
239
240 int maxCounters;
241 int nbCounters;
242 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000243
244 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000245};
246
247struct _xmlRegexp {
248 xmlChar *string;
249 int nbStates;
250 xmlRegStatePtr *states;
251 int nbAtoms;
252 xmlRegAtomPtr *atoms;
253 int nbCounters;
254 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000255 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000256 /*
257 * That's the compact form for determinists automatas
258 */
259 int nbstates;
260 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000261 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000262 int nbstrings;
263 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000264};
265
266typedef struct _xmlRegExecRollback xmlRegExecRollback;
267typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
268
269struct _xmlRegExecRollback {
270 xmlRegStatePtr state;/* the current state */
271 int index; /* the index in the input stack */
272 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000273 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000274};
275
276typedef struct _xmlRegInputToken xmlRegInputToken;
277typedef xmlRegInputToken *xmlRegInputTokenPtr;
278
279struct _xmlRegInputToken {
280 xmlChar *value;
281 void *data;
282};
283
284struct _xmlRegExecCtxt {
285 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000286 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000287 xmlRegexpPtr comp; /* the compiled regexp */
288 xmlRegExecCallbacks callback;
289 void *data;
290
291 xmlRegStatePtr state;/* the current state */
292 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000293 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000294
295 /*
296 * A stack of rollback states
297 */
298 int maxRollbacks;
299 int nbRollbacks;
300 xmlRegExecRollback *rollbacks;
301
302 /*
303 * The state of the automata if any
304 */
305 int *counts;
306
307 /*
308 * The input stack
309 */
310 int inputStackMax;
311 int inputStackNr;
312 int index;
313 int *charStack;
314 const xmlChar *inputString; /* when operating on characters */
315 xmlRegInputTokenPtr inputStack;/* when operating on strings */
316
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +0000317 /*
318 * error handling
319 */
320 int errStateNo; /* the error state number */
321 xmlRegStatePtr errState; /* the error state */
322 xmlChar *errString; /* the string raising the error */
323 int *errCounts; /* counters at the error state */
Daniel Veillard4255d502002-04-16 15:50:10 +0000324};
325
Daniel Veillard441bc322002-04-20 17:38:48 +0000326#define REGEXP_ALL_COUNTER 0x123456
327#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000328
Daniel Veillard4255d502002-04-16 15:50:10 +0000329static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000330static void xmlRegFreeState(xmlRegStatePtr state);
331static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard4255d502002-04-16 15:50:10 +0000332
333/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000334 * *
335 * Regexp memory error handler *
336 * *
337 ************************************************************************/
338/**
339 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000340 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000341 *
342 * Handle an out of memory condition
343 */
344static void
345xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
346{
347 const char *regexp = NULL;
348 if (ctxt != NULL) {
349 regexp = (const char *) ctxt->string;
350 ctxt->error = XML_ERR_NO_MEMORY;
351 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000352 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000353 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
354 regexp, NULL, 0, 0,
355 "Memory allocation failed : %s\n", extra);
356}
357
358/**
359 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000360 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000361 *
William M. Brackddf71d62004-05-06 04:17:26 +0000362 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000363 */
364static void
365xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
366{
367 const char *regexp = NULL;
368 int idx = 0;
369
370 if (ctxt != NULL) {
371 regexp = (const char *) ctxt->string;
372 idx = ctxt->cur - ctxt->string;
373 ctxt->error = XML_REGEXP_COMPILE_ERROR;
374 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000375 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000376 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
377 regexp, NULL, idx, 0,
378 "failed to compile: %s\n", extra);
379}
380
381/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000382 * *
383 * Allocation/Deallocation *
384 * *
385 ************************************************************************/
386
Daniel Veillard23e73572002-09-19 19:56:43 +0000387static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000388/**
389 * xmlRegEpxFromParse:
390 * @ctxt: the parser context used to build it
391 *
William M. Brackddf71d62004-05-06 04:17:26 +0000392 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000393 *
394 * Returns the new regexp or NULL in case of error
395 */
396static xmlRegexpPtr
397xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
398 xmlRegexpPtr ret;
399
400 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000401 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000402 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000403 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000404 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000405 memset(ret, 0, sizeof(xmlRegexp));
406 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000407 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000408 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000409 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000410 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000411 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000412 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000413 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000414
415 if ((ret->determinist != 0) &&
416 (ret->nbCounters == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000417 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000418 (ret->atoms[0] != NULL) &&
419 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
420 int i, j, nbstates = 0, nbatoms = 0;
421 int *stateRemap;
422 int *stringRemap;
423 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000424 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000425 xmlChar **stringMap;
426 xmlChar *value;
427
428 /*
429 * Switch to a compact representation
430 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000431 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000432 * they are all of the string type
433 * 3/ build a table state x atom for the transitions
434 */
435
436 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000437 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000438 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000439 xmlFree(ret);
440 return(NULL);
441 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000442 for (i = 0;i < ret->nbStates;i++) {
443 if (ret->states[i] != NULL) {
444 stateRemap[i] = nbstates;
445 nbstates++;
446 } else {
447 stateRemap[i] = -1;
448 }
449 }
450#ifdef DEBUG_COMPACTION
451 printf("Final: %d states\n", nbstates);
452#endif
453 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000454 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000455 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000456 xmlFree(stateRemap);
457 xmlFree(ret);
458 return(NULL);
459 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000460 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000461 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000462 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000463 xmlFree(stringMap);
464 xmlFree(stateRemap);
465 xmlFree(ret);
466 return(NULL);
467 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000468 for (i = 0;i < ret->nbAtoms;i++) {
469 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
470 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
471 value = ret->atoms[i]->valuep;
472 for (j = 0;j < nbatoms;j++) {
473 if (xmlStrEqual(stringMap[j], value)) {
474 stringRemap[i] = j;
475 break;
476 }
477 }
478 if (j >= nbatoms) {
479 stringRemap[i] = nbatoms;
480 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000481 if (stringMap[nbatoms] == NULL) {
482 for (i = 0;i < nbatoms;i++)
483 xmlFree(stringMap[i]);
484 xmlFree(stringRemap);
485 xmlFree(stringMap);
486 xmlFree(stateRemap);
487 xmlFree(ret);
488 return(NULL);
489 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000490 nbatoms++;
491 }
492 } else {
493 xmlFree(stateRemap);
494 xmlFree(stringRemap);
495 for (i = 0;i < nbatoms;i++)
496 xmlFree(stringMap[i]);
497 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000498 xmlFree(ret);
499 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000500 }
501 }
502#ifdef DEBUG_COMPACTION
503 printf("Final: %d atoms\n", nbatoms);
504#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000505 transitions = (int *) xmlMalloc((nbstates + 1) *
506 (nbatoms + 1) * sizeof(int));
507 if (transitions == NULL) {
508 xmlFree(stateRemap);
509 xmlFree(stringRemap);
510 xmlFree(stringMap);
511 xmlFree(ret);
512 return(NULL);
513 }
514 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000515
516 /*
517 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000518 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000519 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000520 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000521
522 for (i = 0;i < ret->nbStates;i++) {
523 int stateno, atomno, targetno, prev;
524 xmlRegStatePtr state;
525 xmlRegTransPtr trans;
526
527 stateno = stateRemap[i];
528 if (stateno == -1)
529 continue;
530 state = ret->states[i];
531
532 transitions[stateno * (nbatoms + 1)] = state->type;
533
534 for (j = 0;j < state->nbTrans;j++) {
535 trans = &(state->trans[j]);
536 if ((trans->to == -1) || (trans->atom == NULL))
537 continue;
538 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000539 if ((trans->atom->data != NULL) && (transdata == NULL)) {
540 transdata = (void **) xmlMalloc(nbstates * nbatoms *
541 sizeof(void *));
542 if (transdata != NULL)
543 memset(transdata, 0,
544 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000545 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000546 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000547 break;
548 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000549 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000550 targetno = stateRemap[trans->to];
551 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000552 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000553 * states then it means the automata is not determinist and
554 * the compact form can't be used !
555 */
556 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
557 if (prev != 0) {
558 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000559 ret->determinist = 0;
560#ifdef DEBUG_COMPACTION
561 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
562 i, j, trans->atom->no, trans->to, atomno, targetno);
563 printf(" previous to is %d\n", prev);
564#endif
565 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000566 if (transdata != NULL)
567 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000568 xmlFree(transitions);
569 xmlFree(stateRemap);
570 xmlFree(stringRemap);
571 for (i = 0;i < nbatoms;i++)
572 xmlFree(stringMap[i]);
573 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000574 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000575 }
576 } else {
577#if 0
578 printf("State %d trans %d: atom %d to %d : %d to %d\n",
579 i, j, trans->atom->no, trans->to, atomno, targetno);
580#endif
581 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000582 targetno + 1; /* to avoid 0 */
583 if (transdata != NULL)
584 transdata[stateno * nbatoms + atomno] =
585 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000586 }
587 }
588 }
589 ret->determinist = 1;
590#ifdef DEBUG_COMPACTION
591 /*
592 * Debug
593 */
594 for (i = 0;i < nbstates;i++) {
595 for (j = 0;j < nbatoms + 1;j++) {
596 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
597 }
598 printf("\n");
599 }
600 printf("\n");
601#endif
602 /*
603 * Cleanup of the old data
604 */
605 if (ret->states != NULL) {
606 for (i = 0;i < ret->nbStates;i++)
607 xmlRegFreeState(ret->states[i]);
608 xmlFree(ret->states);
609 }
610 ret->states = NULL;
611 ret->nbStates = 0;
612 if (ret->atoms != NULL) {
613 for (i = 0;i < ret->nbAtoms;i++)
614 xmlRegFreeAtom(ret->atoms[i]);
615 xmlFree(ret->atoms);
616 }
617 ret->atoms = NULL;
618 ret->nbAtoms = 0;
619
620 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000621 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000622 ret->stringMap = stringMap;
623 ret->nbstrings = nbatoms;
624 ret->nbstates = nbstates;
625 xmlFree(stateRemap);
626 xmlFree(stringRemap);
627 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000628not_determ:
629 ctxt->string = NULL;
630 ctxt->nbStates = 0;
631 ctxt->states = NULL;
632 ctxt->nbAtoms = 0;
633 ctxt->atoms = NULL;
634 ctxt->nbCounters = 0;
635 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000636 return(ret);
637}
638
639/**
640 * xmlRegNewParserCtxt:
641 * @string: the string to parse
642 *
643 * Allocate a new regexp parser context
644 *
645 * Returns the new context or NULL in case of error
646 */
647static xmlRegParserCtxtPtr
648xmlRegNewParserCtxt(const xmlChar *string) {
649 xmlRegParserCtxtPtr ret;
650
651 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
652 if (ret == NULL)
653 return(NULL);
654 memset(ret, 0, sizeof(xmlRegParserCtxt));
655 if (string != NULL)
656 ret->string = xmlStrdup(string);
657 ret->cur = ret->string;
658 ret->neg = 0;
659 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000660 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000661 return(ret);
662}
663
664/**
665 * xmlRegNewRange:
666 * @ctxt: the regexp parser context
667 * @neg: is that negative
668 * @type: the type of range
669 * @start: the start codepoint
670 * @end: the end codepoint
671 *
672 * Allocate a new regexp range
673 *
674 * Returns the new range or NULL in case of error
675 */
676static xmlRegRangePtr
677xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
678 int neg, xmlRegAtomType type, int start, int end) {
679 xmlRegRangePtr ret;
680
681 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
682 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000683 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000684 return(NULL);
685 }
686 ret->neg = neg;
687 ret->type = type;
688 ret->start = start;
689 ret->end = end;
690 return(ret);
691}
692
693/**
694 * xmlRegFreeRange:
695 * @range: the regexp range
696 *
697 * Free a regexp range
698 */
699static void
700xmlRegFreeRange(xmlRegRangePtr range) {
701 if (range == NULL)
702 return;
703
704 if (range->blockName != NULL)
705 xmlFree(range->blockName);
706 xmlFree(range);
707}
708
709/**
710 * xmlRegNewAtom:
711 * @ctxt: the regexp parser context
712 * @type: the type of atom
713 *
714 * Allocate a new regexp range
715 *
716 * Returns the new atom or NULL in case of error
717 */
718static xmlRegAtomPtr
719xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
720 xmlRegAtomPtr ret;
721
722 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
723 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000724 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000725 return(NULL);
726 }
727 memset(ret, 0, sizeof(xmlRegAtom));
728 ret->type = type;
729 ret->quant = XML_REGEXP_QUANT_ONCE;
730 ret->min = 0;
731 ret->max = 0;
732 return(ret);
733}
734
735/**
736 * xmlRegFreeAtom:
737 * @atom: the regexp atom
738 *
739 * Free a regexp atom
740 */
741static void
742xmlRegFreeAtom(xmlRegAtomPtr atom) {
743 int i;
744
745 if (atom == NULL)
746 return;
747
748 for (i = 0;i < atom->nbRanges;i++)
749 xmlRegFreeRange(atom->ranges[i]);
750 if (atom->ranges != NULL)
751 xmlFree(atom->ranges);
752 if (atom->type == XML_REGEXP_STRING)
753 xmlFree(atom->valuep);
754 xmlFree(atom);
755}
756
757static xmlRegStatePtr
758xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
759 xmlRegStatePtr ret;
760
761 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
762 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000763 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000764 return(NULL);
765 }
766 memset(ret, 0, sizeof(xmlRegState));
767 ret->type = XML_REGEXP_TRANS_STATE;
768 ret->mark = XML_REGEXP_MARK_NORMAL;
769 return(ret);
770}
771
772/**
773 * xmlRegFreeState:
774 * @state: the regexp state
775 *
776 * Free a regexp state
777 */
778static void
779xmlRegFreeState(xmlRegStatePtr state) {
780 if (state == NULL)
781 return;
782
783 if (state->trans != NULL)
784 xmlFree(state->trans);
785 xmlFree(state);
786}
787
788/**
789 * xmlRegFreeParserCtxt:
790 * @ctxt: the regexp parser context
791 *
792 * Free a regexp parser context
793 */
794static void
795xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
796 int i;
797 if (ctxt == NULL)
798 return;
799
800 if (ctxt->string != NULL)
801 xmlFree(ctxt->string);
802 if (ctxt->states != NULL) {
803 for (i = 0;i < ctxt->nbStates;i++)
804 xmlRegFreeState(ctxt->states[i]);
805 xmlFree(ctxt->states);
806 }
807 if (ctxt->atoms != NULL) {
808 for (i = 0;i < ctxt->nbAtoms;i++)
809 xmlRegFreeAtom(ctxt->atoms[i]);
810 xmlFree(ctxt->atoms);
811 }
812 if (ctxt->counters != NULL)
813 xmlFree(ctxt->counters);
814 xmlFree(ctxt);
815}
816
817/************************************************************************
818 * *
819 * Display of Data structures *
820 * *
821 ************************************************************************/
822
823static void
824xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
825 switch (type) {
826 case XML_REGEXP_EPSILON:
827 fprintf(output, "epsilon "); break;
828 case XML_REGEXP_CHARVAL:
829 fprintf(output, "charval "); break;
830 case XML_REGEXP_RANGES:
831 fprintf(output, "ranges "); break;
832 case XML_REGEXP_SUBREG:
833 fprintf(output, "subexpr "); break;
834 case XML_REGEXP_STRING:
835 fprintf(output, "string "); break;
836 case XML_REGEXP_ANYCHAR:
837 fprintf(output, "anychar "); break;
838 case XML_REGEXP_ANYSPACE:
839 fprintf(output, "anyspace "); break;
840 case XML_REGEXP_NOTSPACE:
841 fprintf(output, "notspace "); break;
842 case XML_REGEXP_INITNAME:
843 fprintf(output, "initname "); break;
844 case XML_REGEXP_NOTINITNAME:
845 fprintf(output, "notinitname "); break;
846 case XML_REGEXP_NAMECHAR:
847 fprintf(output, "namechar "); break;
848 case XML_REGEXP_NOTNAMECHAR:
849 fprintf(output, "notnamechar "); break;
850 case XML_REGEXP_DECIMAL:
851 fprintf(output, "decimal "); break;
852 case XML_REGEXP_NOTDECIMAL:
853 fprintf(output, "notdecimal "); break;
854 case XML_REGEXP_REALCHAR:
855 fprintf(output, "realchar "); break;
856 case XML_REGEXP_NOTREALCHAR:
857 fprintf(output, "notrealchar "); break;
858 case XML_REGEXP_LETTER:
859 fprintf(output, "LETTER "); break;
860 case XML_REGEXP_LETTER_UPPERCASE:
861 fprintf(output, "LETTER_UPPERCASE "); break;
862 case XML_REGEXP_LETTER_LOWERCASE:
863 fprintf(output, "LETTER_LOWERCASE "); break;
864 case XML_REGEXP_LETTER_TITLECASE:
865 fprintf(output, "LETTER_TITLECASE "); break;
866 case XML_REGEXP_LETTER_MODIFIER:
867 fprintf(output, "LETTER_MODIFIER "); break;
868 case XML_REGEXP_LETTER_OTHERS:
869 fprintf(output, "LETTER_OTHERS "); break;
870 case XML_REGEXP_MARK:
871 fprintf(output, "MARK "); break;
872 case XML_REGEXP_MARK_NONSPACING:
873 fprintf(output, "MARK_NONSPACING "); break;
874 case XML_REGEXP_MARK_SPACECOMBINING:
875 fprintf(output, "MARK_SPACECOMBINING "); break;
876 case XML_REGEXP_MARK_ENCLOSING:
877 fprintf(output, "MARK_ENCLOSING "); break;
878 case XML_REGEXP_NUMBER:
879 fprintf(output, "NUMBER "); break;
880 case XML_REGEXP_NUMBER_DECIMAL:
881 fprintf(output, "NUMBER_DECIMAL "); break;
882 case XML_REGEXP_NUMBER_LETTER:
883 fprintf(output, "NUMBER_LETTER "); break;
884 case XML_REGEXP_NUMBER_OTHERS:
885 fprintf(output, "NUMBER_OTHERS "); break;
886 case XML_REGEXP_PUNCT:
887 fprintf(output, "PUNCT "); break;
888 case XML_REGEXP_PUNCT_CONNECTOR:
889 fprintf(output, "PUNCT_CONNECTOR "); break;
890 case XML_REGEXP_PUNCT_DASH:
891 fprintf(output, "PUNCT_DASH "); break;
892 case XML_REGEXP_PUNCT_OPEN:
893 fprintf(output, "PUNCT_OPEN "); break;
894 case XML_REGEXP_PUNCT_CLOSE:
895 fprintf(output, "PUNCT_CLOSE "); break;
896 case XML_REGEXP_PUNCT_INITQUOTE:
897 fprintf(output, "PUNCT_INITQUOTE "); break;
898 case XML_REGEXP_PUNCT_FINQUOTE:
899 fprintf(output, "PUNCT_FINQUOTE "); break;
900 case XML_REGEXP_PUNCT_OTHERS:
901 fprintf(output, "PUNCT_OTHERS "); break;
902 case XML_REGEXP_SEPAR:
903 fprintf(output, "SEPAR "); break;
904 case XML_REGEXP_SEPAR_SPACE:
905 fprintf(output, "SEPAR_SPACE "); break;
906 case XML_REGEXP_SEPAR_LINE:
907 fprintf(output, "SEPAR_LINE "); break;
908 case XML_REGEXP_SEPAR_PARA:
909 fprintf(output, "SEPAR_PARA "); break;
910 case XML_REGEXP_SYMBOL:
911 fprintf(output, "SYMBOL "); break;
912 case XML_REGEXP_SYMBOL_MATH:
913 fprintf(output, "SYMBOL_MATH "); break;
914 case XML_REGEXP_SYMBOL_CURRENCY:
915 fprintf(output, "SYMBOL_CURRENCY "); break;
916 case XML_REGEXP_SYMBOL_MODIFIER:
917 fprintf(output, "SYMBOL_MODIFIER "); break;
918 case XML_REGEXP_SYMBOL_OTHERS:
919 fprintf(output, "SYMBOL_OTHERS "); break;
920 case XML_REGEXP_OTHER:
921 fprintf(output, "OTHER "); break;
922 case XML_REGEXP_OTHER_CONTROL:
923 fprintf(output, "OTHER_CONTROL "); break;
924 case XML_REGEXP_OTHER_FORMAT:
925 fprintf(output, "OTHER_FORMAT "); break;
926 case XML_REGEXP_OTHER_PRIVATE:
927 fprintf(output, "OTHER_PRIVATE "); break;
928 case XML_REGEXP_OTHER_NA:
929 fprintf(output, "OTHER_NA "); break;
930 case XML_REGEXP_BLOCK_NAME:
931 fprintf(output, "BLOCK "); break;
932 }
933}
934
935static void
936xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
937 switch (type) {
938 case XML_REGEXP_QUANT_EPSILON:
939 fprintf(output, "epsilon "); break;
940 case XML_REGEXP_QUANT_ONCE:
941 fprintf(output, "once "); break;
942 case XML_REGEXP_QUANT_OPT:
943 fprintf(output, "? "); break;
944 case XML_REGEXP_QUANT_MULT:
945 fprintf(output, "* "); break;
946 case XML_REGEXP_QUANT_PLUS:
947 fprintf(output, "+ "); break;
948 case XML_REGEXP_QUANT_RANGE:
949 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000950 case XML_REGEXP_QUANT_ONCEONLY:
951 fprintf(output, "onceonly "); break;
952 case XML_REGEXP_QUANT_ALL:
953 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000954 }
955}
956static void
957xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
958 fprintf(output, " range: ");
959 if (range->neg)
960 fprintf(output, "negative ");
961 xmlRegPrintAtomType(output, range->type);
962 fprintf(output, "%c - %c\n", range->start, range->end);
963}
964
965static void
966xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
967 fprintf(output, " atom: ");
968 if (atom == NULL) {
969 fprintf(output, "NULL\n");
970 return;
971 }
972 xmlRegPrintAtomType(output, atom->type);
973 xmlRegPrintQuantType(output, atom->quant);
974 if (atom->quant == XML_REGEXP_QUANT_RANGE)
975 fprintf(output, "%d-%d ", atom->min, atom->max);
976 if (atom->type == XML_REGEXP_STRING)
977 fprintf(output, "'%s' ", (char *) atom->valuep);
978 if (atom->type == XML_REGEXP_CHARVAL)
979 fprintf(output, "char %c\n", atom->codepoint);
980 else if (atom->type == XML_REGEXP_RANGES) {
981 int i;
982 fprintf(output, "%d entries\n", atom->nbRanges);
983 for (i = 0; i < atom->nbRanges;i++)
984 xmlRegPrintRange(output, atom->ranges[i]);
985 } else if (atom->type == XML_REGEXP_SUBREG) {
986 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
987 } else {
988 fprintf(output, "\n");
989 }
990}
991
992static void
993xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
994 fprintf(output, " trans: ");
995 if (trans == NULL) {
996 fprintf(output, "NULL\n");
997 return;
998 }
999 if (trans->to < 0) {
1000 fprintf(output, "removed\n");
1001 return;
1002 }
1003 if (trans->counter >= 0) {
1004 fprintf(output, "counted %d, ", trans->counter);
1005 }
Daniel Veillard8a001f62002-04-20 07:24:11 +00001006 if (trans->count == REGEXP_ALL_COUNTER) {
1007 fprintf(output, "all transition, ");
1008 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001009 fprintf(output, "count based %d, ", trans->count);
1010 }
1011 if (trans->atom == NULL) {
1012 fprintf(output, "epsilon to %d\n", trans->to);
1013 return;
1014 }
1015 if (trans->atom->type == XML_REGEXP_CHARVAL)
1016 fprintf(output, "char %c ", trans->atom->codepoint);
1017 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1018}
1019
1020static void
1021xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1022 int i;
1023
1024 fprintf(output, " state: ");
1025 if (state == NULL) {
1026 fprintf(output, "NULL\n");
1027 return;
1028 }
1029 if (state->type == XML_REGEXP_START_STATE)
1030 fprintf(output, "START ");
1031 if (state->type == XML_REGEXP_FINAL_STATE)
1032 fprintf(output, "FINAL ");
1033
1034 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1035 for (i = 0;i < state->nbTrans; i++) {
1036 xmlRegPrintTrans(output, &(state->trans[i]));
1037 }
1038}
1039
Daniel Veillard23e73572002-09-19 19:56:43 +00001040#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001041static void
1042xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1043 int i;
1044
1045 fprintf(output, " ctxt: ");
1046 if (ctxt == NULL) {
1047 fprintf(output, "NULL\n");
1048 return;
1049 }
1050 fprintf(output, "'%s' ", ctxt->string);
1051 if (ctxt->error)
1052 fprintf(output, "error ");
1053 if (ctxt->neg)
1054 fprintf(output, "neg ");
1055 fprintf(output, "\n");
1056 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1057 for (i = 0;i < ctxt->nbAtoms; i++) {
1058 fprintf(output, " %02d ", i);
1059 xmlRegPrintAtom(output, ctxt->atoms[i]);
1060 }
1061 if (ctxt->atom != NULL) {
1062 fprintf(output, "current atom:\n");
1063 xmlRegPrintAtom(output, ctxt->atom);
1064 }
1065 fprintf(output, "%d states:", ctxt->nbStates);
1066 if (ctxt->start != NULL)
1067 fprintf(output, " start: %d", ctxt->start->no);
1068 if (ctxt->end != NULL)
1069 fprintf(output, " end: %d", ctxt->end->no);
1070 fprintf(output, "\n");
1071 for (i = 0;i < ctxt->nbStates; i++) {
1072 xmlRegPrintState(output, ctxt->states[i]);
1073 }
1074 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1075 for (i = 0;i < ctxt->nbCounters; i++) {
1076 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1077 ctxt->counters[i].max);
1078 }
1079}
Daniel Veillard23e73572002-09-19 19:56:43 +00001080#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001081
1082/************************************************************************
1083 * *
1084 * Finite Automata structures manipulations *
1085 * *
1086 ************************************************************************/
1087
1088static void
1089xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1090 int neg, xmlRegAtomType type, int start, int end,
1091 xmlChar *blockName) {
1092 xmlRegRangePtr range;
1093
1094 if (atom == NULL) {
1095 ERROR("add range: atom is NULL");
1096 return;
1097 }
1098 if (atom->type != XML_REGEXP_RANGES) {
1099 ERROR("add range: atom is not ranges");
1100 return;
1101 }
1102 if (atom->maxRanges == 0) {
1103 atom->maxRanges = 4;
1104 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1105 sizeof(xmlRegRangePtr));
1106 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001107 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001108 atom->maxRanges = 0;
1109 return;
1110 }
1111 } else if (atom->nbRanges >= atom->maxRanges) {
1112 xmlRegRangePtr *tmp;
1113 atom->maxRanges *= 2;
1114 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1115 sizeof(xmlRegRangePtr));
1116 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001117 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001118 atom->maxRanges /= 2;
1119 return;
1120 }
1121 atom->ranges = tmp;
1122 }
1123 range = xmlRegNewRange(ctxt, neg, type, start, end);
1124 if (range == NULL)
1125 return;
1126 range->blockName = blockName;
1127 atom->ranges[atom->nbRanges++] = range;
1128
1129}
1130
1131static int
1132xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1133 if (ctxt->maxCounters == 0) {
1134 ctxt->maxCounters = 4;
1135 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1136 sizeof(xmlRegCounter));
1137 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001138 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001139 ctxt->maxCounters = 0;
1140 return(-1);
1141 }
1142 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1143 xmlRegCounter *tmp;
1144 ctxt->maxCounters *= 2;
1145 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1146 sizeof(xmlRegCounter));
1147 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001148 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001149 ctxt->maxCounters /= 2;
1150 return(-1);
1151 }
1152 ctxt->counters = tmp;
1153 }
1154 ctxt->counters[ctxt->nbCounters].min = -1;
1155 ctxt->counters[ctxt->nbCounters].max = -1;
1156 return(ctxt->nbCounters++);
1157}
1158
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001159static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001160xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1161 if (atom == NULL) {
1162 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001163 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001164 }
1165 if (ctxt->maxAtoms == 0) {
1166 ctxt->maxAtoms = 4;
1167 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1168 sizeof(xmlRegAtomPtr));
1169 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001170 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001171 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001172 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001173 }
1174 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1175 xmlRegAtomPtr *tmp;
1176 ctxt->maxAtoms *= 2;
1177 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1178 sizeof(xmlRegAtomPtr));
1179 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001180 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001181 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001182 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001183 }
1184 ctxt->atoms = tmp;
1185 }
1186 atom->no = ctxt->nbAtoms;
1187 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001188 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001189}
1190
1191static void
1192xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1193 xmlRegAtomPtr atom, xmlRegStatePtr target,
1194 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001195
1196 int nrtrans;
1197
Daniel Veillard4255d502002-04-16 15:50:10 +00001198 if (state == NULL) {
1199 ERROR("add state: state is NULL");
1200 return;
1201 }
1202 if (target == NULL) {
1203 ERROR("add state: target is NULL");
1204 return;
1205 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001206 /*
1207 * Other routines follow the philosophy 'When in doubt, add a transition'
1208 * so we check here whether such a transition is already present and, if
1209 * so, silently ignore this request.
1210 */
1211
1212 for (nrtrans=0; nrtrans<state->nbTrans; nrtrans++) {
1213 if ((state->trans[nrtrans].atom == atom) &&
1214 (state->trans[nrtrans].to == target->no) &&
1215 (state->trans[nrtrans].counter == counter) &&
1216 (state->trans[nrtrans].count == count)) {
1217#ifdef DEBUG_REGEXP_GRAPH
1218 printf("Ignoring duplicate transition from %d to %d\n",
1219 state->no, target->no);
1220#endif
1221 return;
1222 }
1223 }
1224
Daniel Veillard4255d502002-04-16 15:50:10 +00001225 if (state->maxTrans == 0) {
1226 state->maxTrans = 4;
1227 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1228 sizeof(xmlRegTrans));
1229 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001230 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001231 state->maxTrans = 0;
1232 return;
1233 }
1234 } else if (state->nbTrans >= state->maxTrans) {
1235 xmlRegTrans *tmp;
1236 state->maxTrans *= 2;
1237 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1238 sizeof(xmlRegTrans));
1239 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001240 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001241 state->maxTrans /= 2;
1242 return;
1243 }
1244 state->trans = tmp;
1245 }
1246#ifdef DEBUG_REGEXP_GRAPH
1247 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001248 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001249 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001250 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001251 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001252 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001253 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001254 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001255 printf("epsilon transition\n");
1256 else if (atom != NULL)
1257 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001258#endif
1259
1260 state->trans[state->nbTrans].atom = atom;
1261 state->trans[state->nbTrans].to = target->no;
1262 state->trans[state->nbTrans].counter = counter;
1263 state->trans[state->nbTrans].count = count;
1264 state->nbTrans++;
1265}
1266
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001267static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001268xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001269 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001270 if (ctxt->maxStates == 0) {
1271 ctxt->maxStates = 4;
1272 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1273 sizeof(xmlRegStatePtr));
1274 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001275 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001276 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001277 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001278 }
1279 } else if (ctxt->nbStates >= ctxt->maxStates) {
1280 xmlRegStatePtr *tmp;
1281 ctxt->maxStates *= 2;
1282 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1283 sizeof(xmlRegStatePtr));
1284 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001285 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001286 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001287 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001288 }
1289 ctxt->states = tmp;
1290 }
1291 state->no = ctxt->nbStates;
1292 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001293 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001294}
1295
1296/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001297 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001298 * @ctxt: a regexp parser context
1299 * @from: the from state
1300 * @to: the target state or NULL for building a new one
1301 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001302 *
1303 */
1304static void
1305xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001306 xmlRegStatePtr from, xmlRegStatePtr to,
1307 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001308 if (to == NULL) {
1309 to = xmlRegNewState(ctxt);
1310 xmlRegStatePush(ctxt, to);
1311 ctxt->state = to;
1312 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001313 if (lax)
1314 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1315 else
1316 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001317}
1318
1319/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001320 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001321 * @ctxt: a regexp parser context
1322 * @from: the from state
1323 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001324 *
1325 */
1326static void
1327xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1328 xmlRegStatePtr from, xmlRegStatePtr to) {
1329 if (to == NULL) {
1330 to = xmlRegNewState(ctxt);
1331 xmlRegStatePush(ctxt, to);
1332 ctxt->state = to;
1333 }
1334 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1335}
1336
1337/**
1338 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001339 * @ctxt: a regexp parser context
1340 * @from: the from state
1341 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001342 * counter: the counter for that transition
1343 *
1344 */
1345static void
1346xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1347 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1348 if (to == NULL) {
1349 to = xmlRegNewState(ctxt);
1350 xmlRegStatePush(ctxt, to);
1351 ctxt->state = to;
1352 }
1353 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1354}
1355
1356/**
1357 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001358 * @ctxt: a regexp parser context
1359 * @from: the from state
1360 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001361 * counter: the counter for that transition
1362 *
1363 */
1364static void
1365xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1366 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1367 if (to == NULL) {
1368 to = xmlRegNewState(ctxt);
1369 xmlRegStatePush(ctxt, to);
1370 ctxt->state = to;
1371 }
1372 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1373}
1374
1375/**
1376 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001377 * @ctxt: a regexp parser context
1378 * @from: the from state
1379 * @to: the target state or NULL for building a new one
1380 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001381 *
William M. Brackddf71d62004-05-06 04:17:26 +00001382 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001383 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001384static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001385xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1386 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1387 if (atom == NULL) {
1388 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001389 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001390 }
1391 if (atom->type == XML_REGEXP_SUBREG) {
1392 /*
1393 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001394 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001395 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001396 if (xmlRegAtomPush(ctxt, atom) < 0) {
1397 return(-1);
1398 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001399 if ((to != NULL) && (atom->stop != to) &&
1400 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1401 /*
1402 * Generate an epsilon transition to link to the target
1403 */
1404 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1405 }
1406 switch (atom->quant) {
1407 case XML_REGEXP_QUANT_OPT:
1408 atom->quant = XML_REGEXP_QUANT_ONCE;
1409 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1410 break;
1411 case XML_REGEXP_QUANT_MULT:
1412 atom->quant = XML_REGEXP_QUANT_ONCE;
1413 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1414 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1415 break;
1416 case XML_REGEXP_QUANT_PLUS:
1417 atom->quant = XML_REGEXP_QUANT_ONCE;
1418 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1419 break;
1420 case XML_REGEXP_QUANT_RANGE: {
1421 int counter;
1422 xmlRegStatePtr newstate;
1423
1424 /*
1425 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001426 * 1/ if range has minOccurs == 0, create a new state
1427 * and create epsilon transitions from atom->start
1428 * to atom->stop, as well as atom->start to the new
1429 * state
1430 * 2/ register a new counter
1431 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001432 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001433 * 4/ create a new state
1434 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001435 * that state
1436 */
William M. Brackddf71d62004-05-06 04:17:26 +00001437 if (atom->min == 0) {
1438 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1439 atom->stop);
1440 newstate = xmlRegNewState(ctxt);
1441 xmlRegStatePush(ctxt, newstate);
1442 ctxt->state = newstate;
1443 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1444 newstate);
1445 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001446 counter = xmlRegGetCounter(ctxt);
1447 ctxt->counters[counter].min = atom->min - 1;
1448 ctxt->counters[counter].max = atom->max - 1;
1449 atom->min = 0;
1450 atom->max = 0;
1451 atom->quant = XML_REGEXP_QUANT_ONCE;
1452 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1453 atom->start, counter);
1454 if (to != NULL) {
1455 newstate = to;
1456 } else {
1457 newstate = xmlRegNewState(ctxt);
1458 xmlRegStatePush(ctxt, newstate);
1459 ctxt->state = newstate;
1460 }
1461 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1462 newstate, counter);
1463 }
1464 default:
1465 break;
1466 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001467 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001468 } else {
1469 if (to == NULL) {
1470 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001471 if (to != NULL)
1472 xmlRegStatePush(ctxt, to);
1473 else {
1474 return(-1);
1475 }
1476 }
1477 if (xmlRegAtomPush(ctxt, atom) < 0) {
1478 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001479 }
1480 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001481 ctxt->state = to;
1482 }
1483 switch (atom->quant) {
1484 case XML_REGEXP_QUANT_OPT:
1485 atom->quant = XML_REGEXP_QUANT_ONCE;
1486 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1487 break;
1488 case XML_REGEXP_QUANT_MULT:
1489 atom->quant = XML_REGEXP_QUANT_ONCE;
1490 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1491 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1492 break;
1493 case XML_REGEXP_QUANT_PLUS:
1494 atom->quant = XML_REGEXP_QUANT_ONCE;
1495 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1496 break;
1497 default:
1498 break;
1499 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001500 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001501}
1502
1503/**
1504 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001505 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001506 * @fromnr: the from state
1507 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001508 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001509 *
1510 */
1511static void
1512xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1513 int tonr, int counter) {
1514 int transnr;
1515 xmlRegStatePtr from;
1516 xmlRegStatePtr to;
1517
1518#ifdef DEBUG_REGEXP_GRAPH
1519 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1520#endif
1521 from = ctxt->states[fromnr];
1522 if (from == NULL)
1523 return;
1524 to = ctxt->states[tonr];
1525 if (to == NULL)
1526 return;
1527 if ((to->mark == XML_REGEXP_MARK_START) ||
1528 (to->mark == XML_REGEXP_MARK_VISITED))
1529 return;
1530
1531 to->mark = XML_REGEXP_MARK_VISITED;
1532 if (to->type == XML_REGEXP_FINAL_STATE) {
1533#ifdef DEBUG_REGEXP_GRAPH
1534 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1535#endif
1536 from->type = XML_REGEXP_FINAL_STATE;
1537 }
1538 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1539 if (to->trans[transnr].atom == NULL) {
1540 /*
1541 * Don't remove counted transitions
1542 * Don't loop either
1543 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001544 if (to->trans[transnr].to != fromnr) {
1545 if (to->trans[transnr].count >= 0) {
1546 int newto = to->trans[transnr].to;
1547
1548 xmlRegStateAddTrans(ctxt, from, NULL,
1549 ctxt->states[newto],
1550 -1, to->trans[transnr].count);
1551 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001552#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001553 printf("Found epsilon trans %d from %d to %d\n",
1554 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001555#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001556 if (to->trans[transnr].counter >= 0) {
1557 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1558 to->trans[transnr].to,
1559 to->trans[transnr].counter);
1560 } else {
1561 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1562 to->trans[transnr].to,
1563 counter);
1564 }
1565 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001566 }
1567 } else {
1568 int newto = to->trans[transnr].to;
1569
Daniel Veillardb509f152002-04-17 16:28:10 +00001570 if (to->trans[transnr].counter >= 0) {
1571 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1572 ctxt->states[newto],
1573 to->trans[transnr].counter, -1);
1574 } else {
1575 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1576 ctxt->states[newto], counter, -1);
1577 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001578 }
1579 }
1580 to->mark = XML_REGEXP_MARK_NORMAL;
1581}
1582
1583/**
1584 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001585 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001586 *
1587 */
1588static void
1589xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1590 int statenr, transnr;
1591 xmlRegStatePtr state;
1592
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001593 if (ctxt->states == NULL) return;
1594
1595
Daniel Veillard4255d502002-04-16 15:50:10 +00001596 /*
1597 * build the completed transitions bypassing the epsilons
1598 * Use a marking algorithm to avoid loops
1599 */
1600 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1601 state = ctxt->states[statenr];
1602 if (state == NULL)
1603 continue;
1604 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1605 if ((state->trans[transnr].atom == NULL) &&
1606 (state->trans[transnr].to >= 0)) {
1607 if (state->trans[transnr].to == statenr) {
1608 state->trans[transnr].to = -1;
1609#ifdef DEBUG_REGEXP_GRAPH
1610 printf("Removed loopback epsilon trans %d on %d\n",
1611 transnr, statenr);
1612#endif
1613 } else if (state->trans[transnr].count < 0) {
1614 int newto = state->trans[transnr].to;
1615
1616#ifdef DEBUG_REGEXP_GRAPH
1617 printf("Found epsilon trans %d from %d to %d\n",
1618 transnr, statenr, newto);
1619#endif
1620 state->mark = XML_REGEXP_MARK_START;
1621 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1622 newto, state->trans[transnr].counter);
1623 state->mark = XML_REGEXP_MARK_NORMAL;
1624#ifdef DEBUG_REGEXP_GRAPH
1625 } else {
1626 printf("Found counted transition %d on %d\n",
1627 transnr, statenr);
1628#endif
1629 }
1630 }
1631 }
1632 }
1633 /*
1634 * Eliminate the epsilon transitions
1635 */
1636 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1637 state = ctxt->states[statenr];
1638 if (state == NULL)
1639 continue;
1640 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1641 if ((state->trans[transnr].atom == NULL) &&
1642 (state->trans[transnr].count < 0) &&
1643 (state->trans[transnr].to >= 0)) {
1644 state->trans[transnr].to = -1;
1645 }
1646 }
1647 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001648
1649 /*
1650 * Use this pass to detect unreachable states too
1651 */
1652 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1653 state = ctxt->states[statenr];
1654 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001655 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001656 }
1657 state = ctxt->states[0];
1658 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001659 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001660 while (state != NULL) {
1661 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001662 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001663 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001664 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001665 */
1666 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1667 if ((state->trans[transnr].to >= 0) &&
1668 ((state->trans[transnr].atom != NULL) ||
1669 (state->trans[transnr].count >= 0))) {
1670 int newto = state->trans[transnr].to;
1671
1672 if (ctxt->states[newto] == NULL)
1673 continue;
William M. Brack779af002003-08-01 15:55:39 +00001674 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1675 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001676 target = ctxt->states[newto];
1677 }
1678 }
1679 }
1680 /*
1681 * find the next accessible state not explored
1682 */
1683 if (target == NULL) {
1684 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1685 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001686 if ((state != NULL) && (state->reached ==
1687 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001688 target = state;
1689 break;
1690 }
1691 }
1692 }
1693 state = target;
1694 }
1695 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1696 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001697 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001698#ifdef DEBUG_REGEXP_GRAPH
1699 printf("Removed unreachable state %d\n", statenr);
1700#endif
1701 xmlRegFreeState(state);
1702 ctxt->states[statenr] = NULL;
1703 }
1704 }
1705
Daniel Veillard4255d502002-04-16 15:50:10 +00001706}
1707
Daniel Veillarde19fc232002-04-22 16:01:24 +00001708/**
1709 * xmlFACompareAtoms:
1710 * @atom1: an atom
1711 * @atom2: an atom
1712 *
William M. Brackddf71d62004-05-06 04:17:26 +00001713 * Compares two atoms to check whether they are equivalents
Daniel Veillarde19fc232002-04-22 16:01:24 +00001714 *
1715 * Returns 1 if yes and 0 otherwise
1716 */
1717static int
1718xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1719 if (atom1 == atom2)
1720 return(1);
1721 if ((atom1 == NULL) || (atom2 == NULL))
1722 return(0);
1723
1724 if (atom1->type != atom2->type)
1725 return(0);
1726 switch (atom1->type) {
1727 case XML_REGEXP_STRING:
1728 return(xmlStrEqual((xmlChar *)atom1->valuep,
1729 (xmlChar *)atom2->valuep));
1730 case XML_REGEXP_EPSILON:
1731 return(1);
1732 case XML_REGEXP_CHARVAL:
1733 return(atom1->codepoint == atom2->codepoint);
1734 case XML_REGEXP_RANGES:
1735 TODO;
1736 return(0);
1737 default:
1738 break;
1739 }
1740 return(1);
1741}
1742
1743/**
1744 * xmlFARecurseDeterminism:
1745 * @ctxt: a regexp parser context
1746 *
1747 * Check whether the associated regexp is determinist,
1748 * should be called after xmlFAEliminateEpsilonTransitions()
1749 *
1750 */
1751static int
1752xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1753 int to, xmlRegAtomPtr atom) {
1754 int ret = 1;
1755 int transnr;
1756 xmlRegTransPtr t1;
1757
1758 if (state == NULL)
1759 return(ret);
1760 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1761 t1 = &(state->trans[transnr]);
1762 /*
1763 * check transitions conflicting with the one looked at
1764 */
1765 if (t1->atom == NULL) {
1766 if (t1->to == -1)
1767 continue;
1768 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1769 to, atom);
1770 if (ret == 0)
1771 return(0);
1772 continue;
1773 }
1774 if (t1->to != to)
1775 continue;
1776 if (xmlFACompareAtoms(t1->atom, atom))
1777 return(0);
1778 }
1779 return(ret);
1780}
1781
1782/**
1783 * xmlFAComputesDeterminism:
1784 * @ctxt: a regexp parser context
1785 *
1786 * Check whether the associated regexp is determinist,
1787 * should be called after xmlFAEliminateEpsilonTransitions()
1788 *
1789 */
1790static int
1791xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1792 int statenr, transnr;
1793 xmlRegStatePtr state;
1794 xmlRegTransPtr t1, t2;
1795 int i;
1796 int ret = 1;
1797
Daniel Veillard4402ab42002-09-12 16:02:56 +00001798#ifdef DEBUG_REGEXP_GRAPH
1799 printf("xmlFAComputesDeterminism\n");
1800 xmlRegPrintCtxt(stdout, ctxt);
1801#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001802 if (ctxt->determinist != -1)
1803 return(ctxt->determinist);
1804
1805 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001806 * Check for all states that there aren't 2 transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00001807 * with the same atom and a different target.
1808 */
1809 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1810 state = ctxt->states[statenr];
1811 if (state == NULL)
1812 continue;
1813 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1814 t1 = &(state->trans[transnr]);
1815 /*
1816 * Determinism checks in case of counted or all transitions
1817 * will have to be handled separately
1818 */
1819 if (t1->atom == NULL)
1820 continue;
1821 if (t1->to == -1) /* eliminated */
1822 continue;
1823 for (i = 0;i < transnr;i++) {
1824 t2 = &(state->trans[i]);
1825 if (t2->to == -1) /* eliminated */
1826 continue;
1827 if (t2->atom != NULL) {
1828 if (t1->to == t2->to) {
1829 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00001830 t2->to = -1; /* eliminated */
Daniel Veillarde19fc232002-04-22 16:01:24 +00001831 } else {
1832 /* not determinist ! */
1833 if (xmlFACompareAtoms(t1->atom, t2->atom))
1834 ret = 0;
1835 }
1836 } else if (t1->to != -1) {
1837 /*
1838 * do the closure in case of remaining specific
1839 * epsilon transitions like choices or all
1840 */
1841 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1842 t2->to, t2->atom);
1843 if (ret == 0)
1844 return(0);
1845 }
1846 }
1847 if (ret == 0)
1848 break;
1849 }
1850 if (ret == 0)
1851 break;
1852 }
1853 ctxt->determinist = ret;
1854 return(ret);
1855}
1856
Daniel Veillard4255d502002-04-16 15:50:10 +00001857/************************************************************************
1858 * *
1859 * Routines to check input against transition atoms *
1860 * *
1861 ************************************************************************/
1862
1863static int
1864xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1865 int start, int end, const xmlChar *blockName) {
1866 int ret = 0;
1867
1868 switch (type) {
1869 case XML_REGEXP_STRING:
1870 case XML_REGEXP_SUBREG:
1871 case XML_REGEXP_RANGES:
1872 case XML_REGEXP_EPSILON:
1873 return(-1);
1874 case XML_REGEXP_ANYCHAR:
1875 ret = ((codepoint != '\n') && (codepoint != '\r'));
1876 break;
1877 case XML_REGEXP_CHARVAL:
1878 ret = ((codepoint >= start) && (codepoint <= end));
1879 break;
1880 case XML_REGEXP_NOTSPACE:
1881 neg = !neg;
1882 case XML_REGEXP_ANYSPACE:
1883 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1884 (codepoint == '\t') || (codepoint == ' '));
1885 break;
1886 case XML_REGEXP_NOTINITNAME:
1887 neg = !neg;
1888 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00001889 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001890 (codepoint == '_') || (codepoint == ':'));
1891 break;
1892 case XML_REGEXP_NOTNAMECHAR:
1893 neg = !neg;
1894 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00001895 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001896 (codepoint == '.') || (codepoint == '-') ||
1897 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00001898 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00001899 break;
1900 case XML_REGEXP_NOTDECIMAL:
1901 neg = !neg;
1902 case XML_REGEXP_DECIMAL:
1903 ret = xmlUCSIsCatNd(codepoint);
1904 break;
1905 case XML_REGEXP_REALCHAR:
1906 neg = !neg;
1907 case XML_REGEXP_NOTREALCHAR:
1908 ret = xmlUCSIsCatP(codepoint);
1909 if (ret == 0)
1910 ret = xmlUCSIsCatZ(codepoint);
1911 if (ret == 0)
1912 ret = xmlUCSIsCatC(codepoint);
1913 break;
1914 case XML_REGEXP_LETTER:
1915 ret = xmlUCSIsCatL(codepoint);
1916 break;
1917 case XML_REGEXP_LETTER_UPPERCASE:
1918 ret = xmlUCSIsCatLu(codepoint);
1919 break;
1920 case XML_REGEXP_LETTER_LOWERCASE:
1921 ret = xmlUCSIsCatLl(codepoint);
1922 break;
1923 case XML_REGEXP_LETTER_TITLECASE:
1924 ret = xmlUCSIsCatLt(codepoint);
1925 break;
1926 case XML_REGEXP_LETTER_MODIFIER:
1927 ret = xmlUCSIsCatLm(codepoint);
1928 break;
1929 case XML_REGEXP_LETTER_OTHERS:
1930 ret = xmlUCSIsCatLo(codepoint);
1931 break;
1932 case XML_REGEXP_MARK:
1933 ret = xmlUCSIsCatM(codepoint);
1934 break;
1935 case XML_REGEXP_MARK_NONSPACING:
1936 ret = xmlUCSIsCatMn(codepoint);
1937 break;
1938 case XML_REGEXP_MARK_SPACECOMBINING:
1939 ret = xmlUCSIsCatMc(codepoint);
1940 break;
1941 case XML_REGEXP_MARK_ENCLOSING:
1942 ret = xmlUCSIsCatMe(codepoint);
1943 break;
1944 case XML_REGEXP_NUMBER:
1945 ret = xmlUCSIsCatN(codepoint);
1946 break;
1947 case XML_REGEXP_NUMBER_DECIMAL:
1948 ret = xmlUCSIsCatNd(codepoint);
1949 break;
1950 case XML_REGEXP_NUMBER_LETTER:
1951 ret = xmlUCSIsCatNl(codepoint);
1952 break;
1953 case XML_REGEXP_NUMBER_OTHERS:
1954 ret = xmlUCSIsCatNo(codepoint);
1955 break;
1956 case XML_REGEXP_PUNCT:
1957 ret = xmlUCSIsCatP(codepoint);
1958 break;
1959 case XML_REGEXP_PUNCT_CONNECTOR:
1960 ret = xmlUCSIsCatPc(codepoint);
1961 break;
1962 case XML_REGEXP_PUNCT_DASH:
1963 ret = xmlUCSIsCatPd(codepoint);
1964 break;
1965 case XML_REGEXP_PUNCT_OPEN:
1966 ret = xmlUCSIsCatPs(codepoint);
1967 break;
1968 case XML_REGEXP_PUNCT_CLOSE:
1969 ret = xmlUCSIsCatPe(codepoint);
1970 break;
1971 case XML_REGEXP_PUNCT_INITQUOTE:
1972 ret = xmlUCSIsCatPi(codepoint);
1973 break;
1974 case XML_REGEXP_PUNCT_FINQUOTE:
1975 ret = xmlUCSIsCatPf(codepoint);
1976 break;
1977 case XML_REGEXP_PUNCT_OTHERS:
1978 ret = xmlUCSIsCatPo(codepoint);
1979 break;
1980 case XML_REGEXP_SEPAR:
1981 ret = xmlUCSIsCatZ(codepoint);
1982 break;
1983 case XML_REGEXP_SEPAR_SPACE:
1984 ret = xmlUCSIsCatZs(codepoint);
1985 break;
1986 case XML_REGEXP_SEPAR_LINE:
1987 ret = xmlUCSIsCatZl(codepoint);
1988 break;
1989 case XML_REGEXP_SEPAR_PARA:
1990 ret = xmlUCSIsCatZp(codepoint);
1991 break;
1992 case XML_REGEXP_SYMBOL:
1993 ret = xmlUCSIsCatS(codepoint);
1994 break;
1995 case XML_REGEXP_SYMBOL_MATH:
1996 ret = xmlUCSIsCatSm(codepoint);
1997 break;
1998 case XML_REGEXP_SYMBOL_CURRENCY:
1999 ret = xmlUCSIsCatSc(codepoint);
2000 break;
2001 case XML_REGEXP_SYMBOL_MODIFIER:
2002 ret = xmlUCSIsCatSk(codepoint);
2003 break;
2004 case XML_REGEXP_SYMBOL_OTHERS:
2005 ret = xmlUCSIsCatSo(codepoint);
2006 break;
2007 case XML_REGEXP_OTHER:
2008 ret = xmlUCSIsCatC(codepoint);
2009 break;
2010 case XML_REGEXP_OTHER_CONTROL:
2011 ret = xmlUCSIsCatCc(codepoint);
2012 break;
2013 case XML_REGEXP_OTHER_FORMAT:
2014 ret = xmlUCSIsCatCf(codepoint);
2015 break;
2016 case XML_REGEXP_OTHER_PRIVATE:
2017 ret = xmlUCSIsCatCo(codepoint);
2018 break;
2019 case XML_REGEXP_OTHER_NA:
2020 /* ret = xmlUCSIsCatCn(codepoint); */
2021 /* Seems it doesn't exist anymore in recent Unicode releases */
2022 ret = 0;
2023 break;
2024 case XML_REGEXP_BLOCK_NAME:
2025 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2026 break;
2027 }
2028 if (neg)
2029 return(!ret);
2030 return(ret);
2031}
2032
2033static int
2034xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2035 int i, ret = 0;
2036 xmlRegRangePtr range;
2037
William M. Brack871611b2003-10-18 04:53:14 +00002038 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002039 return(-1);
2040
2041 switch (atom->type) {
2042 case XML_REGEXP_SUBREG:
2043 case XML_REGEXP_EPSILON:
2044 return(-1);
2045 case XML_REGEXP_CHARVAL:
2046 return(codepoint == atom->codepoint);
2047 case XML_REGEXP_RANGES: {
2048 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002049
Daniel Veillard4255d502002-04-16 15:50:10 +00002050 for (i = 0;i < atom->nbRanges;i++) {
2051 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002052 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002053 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2054 0, range->start, range->end,
2055 range->blockName);
2056 if (ret != 0)
2057 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002058 } else if (range->neg) {
2059 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2060 0, range->start, range->end,
2061 range->blockName);
2062 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002063 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002064 else
2065 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002066 } else {
2067 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2068 0, range->start, range->end,
2069 range->blockName);
2070 if (ret != 0)
2071 accept = 1; /* might still be excluded */
2072 }
2073 }
2074 return(accept);
2075 }
2076 case XML_REGEXP_STRING:
2077 printf("TODO: XML_REGEXP_STRING\n");
2078 return(-1);
2079 case XML_REGEXP_ANYCHAR:
2080 case XML_REGEXP_ANYSPACE:
2081 case XML_REGEXP_NOTSPACE:
2082 case XML_REGEXP_INITNAME:
2083 case XML_REGEXP_NOTINITNAME:
2084 case XML_REGEXP_NAMECHAR:
2085 case XML_REGEXP_NOTNAMECHAR:
2086 case XML_REGEXP_DECIMAL:
2087 case XML_REGEXP_NOTDECIMAL:
2088 case XML_REGEXP_REALCHAR:
2089 case XML_REGEXP_NOTREALCHAR:
2090 case XML_REGEXP_LETTER:
2091 case XML_REGEXP_LETTER_UPPERCASE:
2092 case XML_REGEXP_LETTER_LOWERCASE:
2093 case XML_REGEXP_LETTER_TITLECASE:
2094 case XML_REGEXP_LETTER_MODIFIER:
2095 case XML_REGEXP_LETTER_OTHERS:
2096 case XML_REGEXP_MARK:
2097 case XML_REGEXP_MARK_NONSPACING:
2098 case XML_REGEXP_MARK_SPACECOMBINING:
2099 case XML_REGEXP_MARK_ENCLOSING:
2100 case XML_REGEXP_NUMBER:
2101 case XML_REGEXP_NUMBER_DECIMAL:
2102 case XML_REGEXP_NUMBER_LETTER:
2103 case XML_REGEXP_NUMBER_OTHERS:
2104 case XML_REGEXP_PUNCT:
2105 case XML_REGEXP_PUNCT_CONNECTOR:
2106 case XML_REGEXP_PUNCT_DASH:
2107 case XML_REGEXP_PUNCT_OPEN:
2108 case XML_REGEXP_PUNCT_CLOSE:
2109 case XML_REGEXP_PUNCT_INITQUOTE:
2110 case XML_REGEXP_PUNCT_FINQUOTE:
2111 case XML_REGEXP_PUNCT_OTHERS:
2112 case XML_REGEXP_SEPAR:
2113 case XML_REGEXP_SEPAR_SPACE:
2114 case XML_REGEXP_SEPAR_LINE:
2115 case XML_REGEXP_SEPAR_PARA:
2116 case XML_REGEXP_SYMBOL:
2117 case XML_REGEXP_SYMBOL_MATH:
2118 case XML_REGEXP_SYMBOL_CURRENCY:
2119 case XML_REGEXP_SYMBOL_MODIFIER:
2120 case XML_REGEXP_SYMBOL_OTHERS:
2121 case XML_REGEXP_OTHER:
2122 case XML_REGEXP_OTHER_CONTROL:
2123 case XML_REGEXP_OTHER_FORMAT:
2124 case XML_REGEXP_OTHER_PRIVATE:
2125 case XML_REGEXP_OTHER_NA:
2126 case XML_REGEXP_BLOCK_NAME:
2127 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2128 (const xmlChar *)atom->valuep);
2129 if (atom->neg)
2130 ret = !ret;
2131 break;
2132 }
2133 return(ret);
2134}
2135
2136/************************************************************************
2137 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002138 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002139 * *
2140 ************************************************************************/
2141
2142#ifdef DEBUG_REGEXP_EXEC
2143static void
2144xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2145 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2146 if (exec->inputStack != NULL) {
2147 int i;
2148 printf(": ");
2149 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2150 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2151 } else {
2152 printf(": %s", &(exec->inputString[exec->index]));
2153 }
2154 printf("\n");
2155}
2156#endif
2157
2158static void
2159xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2160#ifdef DEBUG_REGEXP_EXEC
2161 printf("saving ");
2162 exec->transno++;
2163 xmlFARegDebugExec(exec);
2164 exec->transno--;
2165#endif
2166
2167 if (exec->maxRollbacks == 0) {
2168 exec->maxRollbacks = 4;
2169 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2170 sizeof(xmlRegExecRollback));
2171 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002172 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002173 exec->maxRollbacks = 0;
2174 return;
2175 }
2176 memset(exec->rollbacks, 0,
2177 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2178 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2179 xmlRegExecRollback *tmp;
2180 int len = exec->maxRollbacks;
2181
2182 exec->maxRollbacks *= 2;
2183 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2184 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2185 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002186 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002187 exec->maxRollbacks /= 2;
2188 return;
2189 }
2190 exec->rollbacks = tmp;
2191 tmp = &exec->rollbacks[len];
2192 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2193 }
2194 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2195 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2196 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2197 if (exec->comp->nbCounters > 0) {
2198 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2199 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2200 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2201 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002202 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002203 exec->status = -5;
2204 return;
2205 }
2206 }
2207 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2208 exec->comp->nbCounters * sizeof(int));
2209 }
2210 exec->nbRollbacks++;
2211}
2212
2213static void
2214xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2215 if (exec->nbRollbacks <= 0) {
2216 exec->status = -1;
2217#ifdef DEBUG_REGEXP_EXEC
2218 printf("rollback failed on empty stack\n");
2219#endif
2220 return;
2221 }
2222 exec->nbRollbacks--;
2223 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2224 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2225 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2226 if (exec->comp->nbCounters > 0) {
2227 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2228 fprintf(stderr, "exec save: allocation failed");
2229 exec->status = -6;
2230 return;
2231 }
2232 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2233 exec->comp->nbCounters * sizeof(int));
2234 }
2235
2236#ifdef DEBUG_REGEXP_EXEC
2237 printf("restored ");
2238 xmlFARegDebugExec(exec);
2239#endif
2240}
2241
2242/************************************************************************
2243 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002244 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002245 * *
2246 ************************************************************************/
2247
2248static int
2249xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2250 xmlRegExecCtxt execval;
2251 xmlRegExecCtxtPtr exec = &execval;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002252 int ret, codepoint = 0, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00002253
2254 exec->inputString = content;
2255 exec->index = 0;
2256 exec->determinist = 1;
2257 exec->maxRollbacks = 0;
2258 exec->nbRollbacks = 0;
2259 exec->rollbacks = NULL;
2260 exec->status = 0;
2261 exec->comp = comp;
2262 exec->state = comp->states[0];
2263 exec->transno = 0;
2264 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002265 exec->inputStack = NULL;
2266 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002267 if (comp->nbCounters > 0) {
2268 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002269 if (exec->counts == NULL) {
2270 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002271 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002272 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002273 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2274 } else
2275 exec->counts = NULL;
2276 while ((exec->status == 0) &&
2277 ((exec->inputString[exec->index] != 0) ||
2278 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2279 xmlRegTransPtr trans;
2280 xmlRegAtomPtr atom;
2281
2282 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002283 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002284 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002285 * on counters, in that case don't break too early. Additionally,
2286 * if we are working on a range like "AB{0,2}", where B is not present,
2287 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002288 */
William M. Brack0e00b282004-04-26 15:40:47 +00002289 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002290 /*
2291 * if there is a transition, we must check if
2292 * atom allows minOccurs of 0
2293 */
2294 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002295 trans = &exec->state->trans[exec->transno];
2296 if (trans->to >=0) {
2297 atom = trans->atom;
2298 if (!((atom->min == 0) && (atom->max > 0)))
2299 goto rollback;
2300 }
2301 } else
2302 goto rollback;
2303 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002304
2305 exec->transcount = 0;
2306 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2307 trans = &exec->state->trans[exec->transno];
2308 if (trans->to < 0)
2309 continue;
2310 atom = trans->atom;
2311 ret = 0;
2312 if (trans->count >= 0) {
2313 int count;
2314 xmlRegCounterPtr counter;
2315
2316 /*
2317 * A counted transition.
2318 */
2319
2320 count = exec->counts[trans->count];
2321 counter = &exec->comp->counters[trans->count];
2322#ifdef DEBUG_REGEXP_EXEC
2323 printf("testing count %d: val %d, min %d, max %d\n",
2324 trans->count, count, counter->min, counter->max);
2325#endif
2326 ret = ((count >= counter->min) && (count <= counter->max));
2327 } else if (atom == NULL) {
2328 fprintf(stderr, "epsilon transition left at runtime\n");
2329 exec->status = -2;
2330 break;
2331 } else if (exec->inputString[exec->index] != 0) {
2332 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2333 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002334 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002335 xmlRegStatePtr to = comp->states[trans->to];
2336
2337 /*
2338 * this is a multiple input sequence
2339 */
2340 if (exec->state->nbTrans > exec->transno + 1) {
2341 xmlFARegExecSave(exec);
2342 }
2343 exec->transcount = 1;
2344 do {
2345 /*
2346 * Try to progress as much as possible on the input
2347 */
2348 if (exec->transcount == atom->max) {
2349 break;
2350 }
2351 exec->index += len;
2352 /*
2353 * End of input: stop here
2354 */
2355 if (exec->inputString[exec->index] == 0) {
2356 exec->index -= len;
2357 break;
2358 }
2359 if (exec->transcount >= atom->min) {
2360 int transno = exec->transno;
2361 xmlRegStatePtr state = exec->state;
2362
2363 /*
2364 * The transition is acceptable save it
2365 */
2366 exec->transno = -1; /* trick */
2367 exec->state = to;
2368 xmlFARegExecSave(exec);
2369 exec->transno = transno;
2370 exec->state = state;
2371 }
2372 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2373 len);
2374 ret = xmlRegCheckCharacter(atom, codepoint);
2375 exec->transcount++;
2376 } while (ret == 1);
2377 if (exec->transcount < atom->min)
2378 ret = 0;
2379
2380 /*
2381 * If the last check failed but one transition was found
2382 * possible, rollback
2383 */
2384 if (ret < 0)
2385 ret = 0;
2386 if (ret == 0) {
2387 goto rollback;
2388 }
William M. Brack0e00b282004-04-26 15:40:47 +00002389 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2390 /*
2391 * we don't match on the codepoint, but minOccurs of 0
2392 * says that's ok. Setting len to 0 inhibits stepping
2393 * over the codepoint.
2394 */
2395 exec->transcount = 1;
2396 len = 0;
2397 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002398 }
William M. Brack0e00b282004-04-26 15:40:47 +00002399 } else if ((atom->min == 0) && (atom->max > 0)) {
2400 /* another spot to match when minOccurs is 0 */
2401 exec->transcount = 1;
2402 len = 0;
2403 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002404 }
2405 if (ret == 1) {
2406 if (exec->state->nbTrans > exec->transno + 1) {
2407 xmlFARegExecSave(exec);
2408 }
2409 if (trans->counter >= 0) {
2410#ifdef DEBUG_REGEXP_EXEC
2411 printf("Increasing count %d\n", trans->counter);
2412#endif
2413 exec->counts[trans->counter]++;
2414 }
2415#ifdef DEBUG_REGEXP_EXEC
2416 printf("entering state %d\n", trans->to);
2417#endif
2418 exec->state = comp->states[trans->to];
2419 exec->transno = 0;
2420 if (trans->atom != NULL) {
2421 exec->index += len;
2422 }
2423 goto progress;
2424 } else if (ret < 0) {
2425 exec->status = -4;
2426 break;
2427 }
2428 }
2429 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2430rollback:
2431 /*
2432 * Failed to find a way out
2433 */
2434 exec->determinist = 0;
2435 xmlFARegExecRollBack(exec);
2436 }
2437progress:
2438 continue;
2439 }
2440 if (exec->rollbacks != NULL) {
2441 if (exec->counts != NULL) {
2442 int i;
2443
2444 for (i = 0;i < exec->maxRollbacks;i++)
2445 if (exec->rollbacks[i].counts != NULL)
2446 xmlFree(exec->rollbacks[i].counts);
2447 }
2448 xmlFree(exec->rollbacks);
2449 }
2450 if (exec->counts != NULL)
2451 xmlFree(exec->counts);
2452 if (exec->status == 0)
2453 return(1);
2454 if (exec->status == -1)
2455 return(0);
2456 return(exec->status);
2457}
2458
2459/************************************************************************
2460 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002461 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002462 * *
2463 ************************************************************************/
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002464#ifdef DEBUG_ERR
2465static void testerr(xmlRegExecCtxtPtr exec);
2466#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00002467
2468/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002469 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002470 * @comp: a precompiled regular expression
2471 * @callback: a callback function used for handling progresses in the
2472 * automata matching phase
2473 * @data: the context data associated to the callback in this context
2474 *
2475 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002476 *
2477 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002478 */
2479xmlRegExecCtxtPtr
2480xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2481 xmlRegExecCtxtPtr exec;
2482
2483 if (comp == NULL)
2484 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002485 if ((comp->compact == NULL) && (comp->states == NULL))
2486 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002487 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2488 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002489 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002490 return(NULL);
2491 }
2492 memset(exec, 0, sizeof(xmlRegExecCtxt));
2493 exec->inputString = NULL;
2494 exec->index = 0;
2495 exec->determinist = 1;
2496 exec->maxRollbacks = 0;
2497 exec->nbRollbacks = 0;
2498 exec->rollbacks = NULL;
2499 exec->status = 0;
2500 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002501 if (comp->compact == NULL)
2502 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002503 exec->transno = 0;
2504 exec->transcount = 0;
2505 exec->callback = callback;
2506 exec->data = data;
2507 if (comp->nbCounters > 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002508 /*
2509 * For error handling, exec->counts is allocated twice the size
2510 * the second half is used to store the data in case of rollback
2511 */
2512 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
2513 * 2);
Daniel Veillard4255d502002-04-16 15:50:10 +00002514 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002515 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002516 xmlFree(exec);
2517 return(NULL);
2518 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002519 memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
2520 exec->errCounts = &exec->counts[comp->nbCounters];
2521 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00002522 exec->counts = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002523 exec->errCounts = NULL;
2524 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002525 exec->inputStackMax = 0;
2526 exec->inputStackNr = 0;
2527 exec->inputStack = NULL;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002528 exec->errStateNo = -1;
2529 exec->errString = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +00002530 return(exec);
2531}
2532
2533/**
2534 * xmlRegFreeExecCtxt:
2535 * @exec: a regular expression evaulation context
2536 *
2537 * Free the structures associated to a regular expression evaulation context.
2538 */
2539void
2540xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2541 if (exec == NULL)
2542 return;
2543
2544 if (exec->rollbacks != NULL) {
2545 if (exec->counts != NULL) {
2546 int i;
2547
2548 for (i = 0;i < exec->maxRollbacks;i++)
2549 if (exec->rollbacks[i].counts != NULL)
2550 xmlFree(exec->rollbacks[i].counts);
2551 }
2552 xmlFree(exec->rollbacks);
2553 }
2554 if (exec->counts != NULL)
2555 xmlFree(exec->counts);
2556 if (exec->inputStack != NULL) {
2557 int i;
2558
Daniel Veillard32370232002-10-16 14:08:14 +00002559 for (i = 0;i < exec->inputStackNr;i++) {
2560 if (exec->inputStack[i].value != NULL)
2561 xmlFree(exec->inputStack[i].value);
2562 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002563 xmlFree(exec->inputStack);
2564 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002565 if (exec->errString != NULL)
2566 xmlFree(exec->errString);
Daniel Veillard4255d502002-04-16 15:50:10 +00002567 xmlFree(exec);
2568}
2569
2570static void
2571xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2572 void *data) {
2573#ifdef DEBUG_PUSH
2574 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2575#endif
2576 if (exec->inputStackMax == 0) {
2577 exec->inputStackMax = 4;
2578 exec->inputStack = (xmlRegInputTokenPtr)
2579 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2580 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002581 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002582 exec->inputStackMax = 0;
2583 return;
2584 }
2585 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2586 xmlRegInputTokenPtr tmp;
2587
2588 exec->inputStackMax *= 2;
2589 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2590 exec->inputStackMax * sizeof(xmlRegInputToken));
2591 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002592 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002593 exec->inputStackMax /= 2;
2594 return;
2595 }
2596 exec->inputStack = tmp;
2597 }
2598 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2599 exec->inputStack[exec->inputStackNr].data = data;
2600 exec->inputStackNr++;
2601 exec->inputStack[exec->inputStackNr].value = NULL;
2602 exec->inputStack[exec->inputStackNr].data = NULL;
2603}
2604
Daniel Veillardc0826a72004-08-10 14:17:33 +00002605/**
2606 * xmlRegStrEqualWildcard:
2607 * @expStr: the string to be evaluated
2608 * @valStr: the validation string
2609 *
2610 * Checks if both strings are equal or have the same content. "*"
2611 * can be used as a wildcard in @valStr; "|" is used as a seperator of
2612 * substrings in both @expStr and @valStr.
2613 *
2614 * Returns 1 if the comparison is satisfied and the number of substrings
2615 * is equal, 0 otherwise.
2616 */
2617
2618static int
2619xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
2620 if (expStr == valStr) return(1);
2621 if (expStr == NULL) return(0);
2622 if (valStr == NULL) return(0);
2623 do {
2624 /*
2625 * Eval if we have a wildcard for the current item.
2626 */
2627 if (*expStr != *valStr) {
2628 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
2629 do {
2630 if (*valStr == XML_REG_STRING_SEPARATOR)
2631 break;
2632 *valStr++;
2633 } while (*valStr != 0);
2634 continue;
2635 } else
2636 return(0);
2637 }
2638 *expStr++;
2639 *valStr++;
2640 } while (*valStr != 0);
2641 if (*expStr != 0)
2642 return (0);
2643 else
2644 return (1);
2645}
Daniel Veillard4255d502002-04-16 15:50:10 +00002646
2647/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002648 * xmlRegCompactPushString:
2649 * @exec: a regexp execution context
2650 * @comp: the precompiled exec with a compact table
2651 * @value: a string token input
2652 * @data: data associated to the token to reuse in callbacks
2653 *
2654 * Push one input token in the execution context
2655 *
2656 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2657 * a negative value in case of error.
2658 */
2659static int
2660xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2661 xmlRegexpPtr comp,
2662 const xmlChar *value,
2663 void *data) {
2664 int state = exec->index;
2665 int i, target;
2666
2667 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2668 return(-1);
2669
2670 if (value == NULL) {
2671 /*
2672 * are we at a final state ?
2673 */
2674 if (comp->compact[state * (comp->nbstrings + 1)] ==
2675 XML_REGEXP_FINAL_STATE)
2676 return(1);
2677 return(0);
2678 }
2679
2680#ifdef DEBUG_PUSH
2681 printf("value pushed: %s\n", value);
2682#endif
2683
2684 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002685 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00002686 */
2687 for (i = 0;i < comp->nbstrings;i++) {
2688 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2689 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002690 target--; /* to avoid 0 */
2691 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
2692 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002693 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2694 exec->callback(exec->data, value,
2695 comp->transdata[state * comp->nbstrings + i], data);
2696 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002697#ifdef DEBUG_PUSH
2698 printf("entering state %d\n", target);
2699#endif
2700 if (comp->compact[target * (comp->nbstrings + 1)] ==
2701 XML_REGEXP_FINAL_STATE)
2702 return(1);
2703 return(0);
2704 }
2705 }
2706 }
2707 /*
2708 * Failed to find an exit transition out from current state for the
2709 * current token
2710 */
2711#ifdef DEBUG_PUSH
2712 printf("failed to find a transition for %s on state %d\n", value, state);
2713#endif
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002714 if (exec->errString != NULL)
2715 xmlFree(exec->errString);
2716 exec->errString = xmlStrdup(value);
2717 exec->errStateNo = state;
Daniel Veillard23e73572002-09-19 19:56:43 +00002718 exec->status = -1;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00002719#ifdef DEBUG_ERR
2720 testerr(exec);
2721#endif
Daniel Veillard23e73572002-09-19 19:56:43 +00002722 return(-1);
2723}
2724
2725/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002726 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002727 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002728 * @value: a string token input
2729 * @data: data associated to the token to reuse in callbacks
2730 *
2731 * Push one input token in the execution context
2732 *
2733 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2734 * a negative value in case of error.
2735 */
2736int
2737xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2738 void *data) {
2739 xmlRegTransPtr trans;
2740 xmlRegAtomPtr atom;
2741 int ret;
2742 int final = 0;
Daniel Veillard90700152005-01-08 22:05:09 +00002743 int progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002744
2745 if (exec == NULL)
2746 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002747 if (exec->comp == NULL)
2748 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002749 if (exec->status != 0)
2750 return(exec->status);
2751
Daniel Veillard23e73572002-09-19 19:56:43 +00002752 if (exec->comp->compact != NULL)
2753 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2754
Daniel Veillard4255d502002-04-16 15:50:10 +00002755 if (value == NULL) {
2756 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2757 return(1);
2758 final = 1;
2759 }
2760
2761#ifdef DEBUG_PUSH
2762 printf("value pushed: %s\n", value);
2763#endif
2764 /*
2765 * If we have an active rollback stack push the new value there
2766 * and get back to where we were left
2767 */
2768 if ((value != NULL) && (exec->inputStackNr > 0)) {
2769 xmlFARegExecSaveInputString(exec, value, data);
2770 value = exec->inputStack[exec->index].value;
2771 data = exec->inputStack[exec->index].data;
2772#ifdef DEBUG_PUSH
2773 printf("value loaded: %s\n", value);
2774#endif
2775 }
2776
2777 while ((exec->status == 0) &&
2778 ((value != NULL) ||
2779 ((final == 1) &&
2780 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2781
2782 /*
2783 * End of input on non-terminal state, rollback, however we may
2784 * still have epsilon like transition for counted transitions
2785 * on counters, in that case don't break too early.
2786 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002787 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002788 goto rollback;
2789
2790 exec->transcount = 0;
2791 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2792 trans = &exec->state->trans[exec->transno];
2793 if (trans->to < 0)
2794 continue;
2795 atom = trans->atom;
2796 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002797 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2798 int i;
2799 int count;
2800 xmlRegTransPtr t;
2801 xmlRegCounterPtr counter;
2802
2803 ret = 0;
2804
2805#ifdef DEBUG_PUSH
2806 printf("testing all lax %d\n", trans->count);
2807#endif
2808 /*
2809 * Check all counted transitions from the current state
2810 */
2811 if ((value == NULL) && (final)) {
2812 ret = 1;
2813 } else if (value != NULL) {
2814 for (i = 0;i < exec->state->nbTrans;i++) {
2815 t = &exec->state->trans[i];
2816 if ((t->counter < 0) || (t == trans))
2817 continue;
2818 counter = &exec->comp->counters[t->counter];
2819 count = exec->counts[t->counter];
2820 if ((count < counter->max) &&
2821 (t->atom != NULL) &&
2822 (xmlStrEqual(value, t->atom->valuep))) {
2823 ret = 0;
2824 break;
2825 }
2826 if ((count >= counter->min) &&
2827 (count < counter->max) &&
2828 (xmlStrEqual(value, t->atom->valuep))) {
2829 ret = 1;
2830 break;
2831 }
2832 }
2833 }
2834 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002835 int i;
2836 int count;
2837 xmlRegTransPtr t;
2838 xmlRegCounterPtr counter;
2839
2840 ret = 1;
2841
2842#ifdef DEBUG_PUSH
2843 printf("testing all %d\n", trans->count);
2844#endif
2845 /*
2846 * Check all counted transitions from the current state
2847 */
2848 for (i = 0;i < exec->state->nbTrans;i++) {
2849 t = &exec->state->trans[i];
2850 if ((t->counter < 0) || (t == trans))
2851 continue;
2852 counter = &exec->comp->counters[t->counter];
2853 count = exec->counts[t->counter];
2854 if ((count < counter->min) || (count > counter->max)) {
2855 ret = 0;
2856 break;
2857 }
2858 }
2859 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002860 int count;
2861 xmlRegCounterPtr counter;
2862
2863 /*
2864 * A counted transition.
2865 */
2866
2867 count = exec->counts[trans->count];
2868 counter = &exec->comp->counters[trans->count];
2869#ifdef DEBUG_PUSH
2870 printf("testing count %d: val %d, min %d, max %d\n",
2871 trans->count, count, counter->min, counter->max);
2872#endif
2873 ret = ((count >= counter->min) && (count <= counter->max));
2874 } else if (atom == NULL) {
2875 fprintf(stderr, "epsilon transition left at runtime\n");
2876 exec->status = -2;
2877 break;
2878 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002879 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard441bc322002-04-20 17:38:48 +00002880 if ((ret == 1) && (trans->counter >= 0)) {
2881 xmlRegCounterPtr counter;
2882 int count;
2883
2884 count = exec->counts[trans->counter];
2885 counter = &exec->comp->counters[trans->counter];
2886 if (count >= counter->max)
2887 ret = 0;
2888 }
2889
Daniel Veillard4255d502002-04-16 15:50:10 +00002890 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2891 xmlRegStatePtr to = exec->comp->states[trans->to];
2892
2893 /*
2894 * this is a multiple input sequence
2895 */
2896 if (exec->state->nbTrans > exec->transno + 1) {
2897 if (exec->inputStackNr <= 0) {
2898 xmlFARegExecSaveInputString(exec, value, data);
2899 }
2900 xmlFARegExecSave(exec);
2901 }
2902 exec->transcount = 1;
2903 do {
2904 /*
2905 * Try to progress as much as possible on the input
2906 */
2907 if (exec->transcount == atom->max) {
2908 break;
2909 }
2910 exec->index++;
2911 value = exec->inputStack[exec->index].value;
2912 data = exec->inputStack[exec->index].data;
2913#ifdef DEBUG_PUSH
2914 printf("value loaded: %s\n", value);
2915#endif
2916
2917 /*
2918 * End of input: stop here
2919 */
2920 if (value == NULL) {
2921 exec->index --;
2922 break;
2923 }
2924 if (exec->transcount >= atom->min) {
2925 int transno = exec->transno;
2926 xmlRegStatePtr state = exec->state;
2927
2928 /*
2929 * The transition is acceptable save it
2930 */
2931 exec->transno = -1; /* trick */
2932 exec->state = to;
2933 if (exec->inputStackNr <= 0) {
2934 xmlFARegExecSaveInputString(exec, value, data);
2935 }
2936 xmlFARegExecSave(exec);
2937 exec->transno = transno;
2938 exec->state = state;
2939 }
2940 ret = xmlStrEqual(value, atom->valuep);
2941 exec->transcount++;
2942 } while (ret == 1);
2943 if (exec->transcount < atom->min)
2944 ret = 0;
2945
2946 /*
2947 * If the last check failed but one transition was found
2948 * possible, rollback
2949 */
2950 if (ret < 0)
2951 ret = 0;
2952 if (ret == 0) {
2953 goto rollback;
2954 }
2955 }
2956 }
2957 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00002958 if ((exec->callback != NULL) && (atom != NULL) &&
2959 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002960 exec->callback(exec->data, atom->valuep,
2961 atom->data, data);
2962 }
2963 if (exec->state->nbTrans > exec->transno + 1) {
2964 if (exec->inputStackNr <= 0) {
2965 xmlFARegExecSaveInputString(exec, value, data);
2966 }
2967 xmlFARegExecSave(exec);
2968 }
2969 if (trans->counter >= 0) {
2970#ifdef DEBUG_PUSH
2971 printf("Increasing count %d\n", trans->counter);
2972#endif
2973 exec->counts[trans->counter]++;
2974 }
2975#ifdef DEBUG_PUSH
2976 printf("entering state %d\n", trans->to);
2977#endif
2978 exec->state = exec->comp->states[trans->to];
2979 exec->transno = 0;
2980 if (trans->atom != NULL) {
2981 if (exec->inputStack != NULL) {
2982 exec->index++;
2983 if (exec->index < exec->inputStackNr) {
2984 value = exec->inputStack[exec->index].value;
2985 data = exec->inputStack[exec->index].data;
2986#ifdef DEBUG_PUSH
2987 printf("value loaded: %s\n", value);
2988#endif
2989 } else {
2990 value = NULL;
2991 data = NULL;
2992#ifdef DEBUG_PUSH
2993 printf("end of input\n");
2994#endif
2995 }
2996 } else {
2997 value = NULL;
2998 data = NULL;
2999#ifdef DEBUG_PUSH
3000 printf("end of input\n");
3001#endif
3002 }
3003 }
3004 goto progress;
3005 } else if (ret < 0) {
3006 exec->status = -4;
3007 break;
3008 }
3009 }
3010 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3011rollback:
Daniel Veillard90700152005-01-08 22:05:09 +00003012 /*
3013 * if we didn't yet rollback on the current input store
3014 * the current state as the error state.
3015 */
3016 if (progress) {
3017 progress = 0;
3018 if (exec->errString != NULL)
3019 xmlFree(exec->errString);
3020 exec->errString = xmlStrdup(value);
3021 exec->errState = exec->state;
3022 memcpy(exec->errCounts, exec->counts,
3023 exec->comp->nbCounters * sizeof(int));
3024 }
3025
Daniel Veillard4255d502002-04-16 15:50:10 +00003026 /*
3027 * Failed to find a way out
3028 */
3029 exec->determinist = 0;
3030 xmlFARegExecRollBack(exec);
3031 if (exec->status == 0) {
3032 value = exec->inputStack[exec->index].value;
3033 data = exec->inputStack[exec->index].data;
3034#ifdef DEBUG_PUSH
3035 printf("value loaded: %s\n", value);
3036#endif
3037 }
3038 }
Daniel Veillard90700152005-01-08 22:05:09 +00003039 continue;
Daniel Veillard4255d502002-04-16 15:50:10 +00003040progress:
Daniel Veillard90700152005-01-08 22:05:09 +00003041 progress = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003042 continue;
3043 }
3044 if (exec->status == 0) {
3045 return(exec->state->type == XML_REGEXP_FINAL_STATE);
3046 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003047#ifdef DEBUG_ERR
Daniel Veillard90700152005-01-08 22:05:09 +00003048 if (exec->status < 0) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003049 testerr(exec);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003050 }
Daniel Veillard90700152005-01-08 22:05:09 +00003051#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00003052 return(exec->status);
3053}
3054
Daniel Veillard52b48c72003-04-13 19:53:42 +00003055/**
3056 * xmlRegExecPushString2:
3057 * @exec: a regexp execution context or NULL to indicate the end
3058 * @value: the first string token input
3059 * @value2: the second string token input
3060 * @data: data associated to the token to reuse in callbacks
3061 *
3062 * Push one input token in the execution context
3063 *
3064 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3065 * a negative value in case of error.
3066 */
3067int
3068xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3069 const xmlChar *value2, void *data) {
3070 xmlChar buf[150];
3071 int lenn, lenp, ret;
3072 xmlChar *str;
3073
3074 if (exec == NULL)
3075 return(-1);
3076 if (exec->comp == NULL)
3077 return(-1);
3078 if (exec->status != 0)
3079 return(exec->status);
3080
3081 if (value2 == NULL)
3082 return(xmlRegExecPushString(exec, value, data));
3083
3084 lenn = strlen((char *) value2);
3085 lenp = strlen((char *) value);
3086
3087 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003088 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003089 if (str == NULL) {
3090 exec->status = -1;
3091 return(-1);
3092 }
3093 } else {
3094 str = buf;
3095 }
3096 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003097 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003098 memcpy(&str[lenp + 1], value2, lenn);
3099 str[lenn + lenp + 1] = 0;
3100
3101 if (exec->comp->compact != NULL)
3102 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3103 else
3104 ret = xmlRegExecPushString(exec, str, data);
3105
3106 if (str != buf)
3107 xmlFree(buf);
3108 return(ret);
3109}
3110
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003111/**
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003112 * xmlRegExecGetalues:
3113 * @exec: a regexp execution context
3114 * @err: error extraction or normal one
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003115 * @nbval: pointer to the number of accepted values IN/OUT
3116 * @values: pointer to the array of acceptable values
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003117 * @terminal: return value if this was a terminal state
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003118 *
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003119 * Extract informations from the regexp execution, internal routine to
3120 * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003121 *
3122 * Returns: 0 in case of success or -1 in case of error.
3123 */
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003124static int
3125xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
3126 int *nbval, xmlChar **values, int *terminal) {
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003127 int maxval;
3128
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003129 if ((exec == NULL) || (nbval == NULL) || (values == NULL) || (*nbval <= 0))
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003130 return(-1);
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003131
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003132 maxval = *nbval;
3133 *nbval = 0;
3134 if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
3135 xmlRegexpPtr comp;
3136 int target, i, state;
3137
3138 comp = exec->comp;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003139
3140 if (err) {
3141 if (exec->errStateNo == -1) return(-1);
3142 state = exec->errStateNo;
3143 } else {
3144 state = exec->index;
3145 }
3146 if (terminal != NULL) {
3147 if (comp->compact[state * (comp->nbstrings + 1)] ==
3148 XML_REGEXP_FINAL_STATE)
3149 *terminal = 1;
3150 else
3151 *terminal = 0;
3152 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003153 for (i = 0;(i < comp->nbstrings) && (*nbval < maxval);i++) {
3154 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
3155 if ((target > 0) && (target <= comp->nbstates)) {
3156 values[*nbval] = comp->stringMap[i];
3157 (*nbval)++;
3158 }
3159 }
3160 } else {
3161 int transno;
3162 xmlRegTransPtr trans;
3163 xmlRegAtomPtr atom;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003164 xmlRegStatePtr state;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003165
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003166 if (terminal != NULL) {
3167 if (exec->state->type == XML_REGEXP_FINAL_STATE)
3168 *terminal = 1;
3169 else
3170 *terminal = 0;
3171 }
3172
3173 if (err) {
3174 if (exec->errState == NULL) return(-1);
3175 state = exec->errState;
3176 } else {
3177 if (exec->state == NULL) return(-1);
3178 state = exec->state;
3179 }
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003180 for (transno = 0;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003181 (transno < state->nbTrans) && (*nbval < maxval);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003182 transno++) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003183 trans = &state->trans[transno];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003184 if (trans->to < 0)
3185 continue;
3186 atom = trans->atom;
3187 if ((atom == NULL) || (atom->valuep == NULL))
3188 continue;
3189 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
3190 TODO;
3191 } else if (trans->count == REGEXP_ALL_COUNTER) {
3192 TODO;
3193 } else if (trans->counter >= 0) {
3194 xmlRegCounterPtr counter;
3195 int count;
3196
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003197 if (err)
3198 count = exec->errCounts[trans->counter];
3199 else
3200 count = exec->counts[trans->counter];
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003201 counter = &exec->comp->counters[trans->counter];
3202 if (count < counter->max) {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003203 values[*nbval] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003204 (*nbval)++;
3205 }
3206 } else {
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003207 values[*nbval] = (xmlChar *) atom->valuep;
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003208 (*nbval)++;
3209 }
3210 }
3211 }
3212 return(0);
3213}
3214
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003215/**
3216 * xmlRegExecNextValues:
3217 * @exec: a regexp execution context
3218 * @nbval: pointer to the number of accepted values IN/OUT
3219 * @values: pointer to the array of acceptable values
3220 * @terminal: return value if this was a terminal state
3221 *
3222 * Extract informations from the regexp execution,
3223 * the parameter @values must point to an array of @nbval string pointers
3224 * on return nbval will contain the number of possible strings in that
3225 * state and the @values array will be updated with them. The string values
3226 * returned will be freed with the @exec context and don't need to be
3227 * deallocated.
3228 *
3229 * Returns: 0 in case of success or -1 in case of error.
3230 */
3231int
3232xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, xmlChar **values,
3233 int *terminal) {
3234 return(xmlRegExecGetValues(exec, 0, nbval, values, terminal));
3235}
3236
3237/**
3238 * xmlRegExecErrInfo:
3239 * @exec: a regexp execution context generating an error
3240 * @string: return value for the error string
3241 * @nbval: pointer to the number of accepted values IN/OUT
3242 * @values: pointer to the array of acceptable values
3243 * @terminal: return value if this was a terminal state
3244 *
3245 * Extract error informations from the regexp execution, the parameter
3246 * @string will be updated with the value pushed and not accepted,
3247 * the parameter @values must point to an array of @nbval string pointers
3248 * on return nbval will contain the number of possible strings in that
3249 * state and the @values array will be updated with them. The string values
3250 * returned will be freed with the @exec context and don't need to be
3251 * deallocated.
3252 *
3253 * Returns: 0 in case of success or -1 in case of error.
3254 */
3255int
3256xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
3257 int *nbval, xmlChar **values, int *terminal) {
3258 if (exec == NULL)
3259 return(-1);
3260 if (string != NULL) {
3261 if (exec->status != 0)
3262 *string = exec->errString;
3263 else
3264 *string = NULL;
3265 }
3266 return(xmlRegExecGetValues(exec, 1, nbval, values, terminal));
3267}
3268
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003269#ifdef DEBUG_ERR
3270static void testerr(xmlRegExecCtxtPtr exec) {
3271 const xmlChar *string;
3272 const xmlChar *values[5];
3273 int nb = 5;
Daniel Veillardfc0b6f62005-01-09 17:48:02 +00003274 int terminal;
3275 xmlRegExecErrInfo(exec, &string, &nb, &values[0], &terminal);
Daniel Veillard7bd8b4b2005-01-07 13:56:19 +00003276}
3277#endif
3278
Daniel Veillard4255d502002-04-16 15:50:10 +00003279#if 0
3280static int
3281xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3282 xmlRegTransPtr trans;
3283 xmlRegAtomPtr atom;
3284 int ret;
3285 int codepoint, len;
3286
3287 if (exec == NULL)
3288 return(-1);
3289 if (exec->status != 0)
3290 return(exec->status);
3291
3292 while ((exec->status == 0) &&
3293 ((exec->inputString[exec->index] != 0) ||
3294 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3295
3296 /*
3297 * End of input on non-terminal state, rollback, however we may
3298 * still have epsilon like transition for counted transitions
3299 * on counters, in that case don't break too early.
3300 */
3301 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3302 goto rollback;
3303
3304 exec->transcount = 0;
3305 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3306 trans = &exec->state->trans[exec->transno];
3307 if (trans->to < 0)
3308 continue;
3309 atom = trans->atom;
3310 ret = 0;
3311 if (trans->count >= 0) {
3312 int count;
3313 xmlRegCounterPtr counter;
3314
3315 /*
3316 * A counted transition.
3317 */
3318
3319 count = exec->counts[trans->count];
3320 counter = &exec->comp->counters[trans->count];
3321#ifdef DEBUG_REGEXP_EXEC
3322 printf("testing count %d: val %d, min %d, max %d\n",
3323 trans->count, count, counter->min, counter->max);
3324#endif
3325 ret = ((count >= counter->min) && (count <= counter->max));
3326 } else if (atom == NULL) {
3327 fprintf(stderr, "epsilon transition left at runtime\n");
3328 exec->status = -2;
3329 break;
3330 } else if (exec->inputString[exec->index] != 0) {
3331 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3332 ret = xmlRegCheckCharacter(atom, codepoint);
3333 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3334 xmlRegStatePtr to = exec->comp->states[trans->to];
3335
3336 /*
3337 * this is a multiple input sequence
3338 */
3339 if (exec->state->nbTrans > exec->transno + 1) {
3340 xmlFARegExecSave(exec);
3341 }
3342 exec->transcount = 1;
3343 do {
3344 /*
3345 * Try to progress as much as possible on the input
3346 */
3347 if (exec->transcount == atom->max) {
3348 break;
3349 }
3350 exec->index += len;
3351 /*
3352 * End of input: stop here
3353 */
3354 if (exec->inputString[exec->index] == 0) {
3355 exec->index -= len;
3356 break;
3357 }
3358 if (exec->transcount >= atom->min) {
3359 int transno = exec->transno;
3360 xmlRegStatePtr state = exec->state;
3361
3362 /*
3363 * The transition is acceptable save it
3364 */
3365 exec->transno = -1; /* trick */
3366 exec->state = to;
3367 xmlFARegExecSave(exec);
3368 exec->transno = transno;
3369 exec->state = state;
3370 }
3371 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3372 len);
3373 ret = xmlRegCheckCharacter(atom, codepoint);
3374 exec->transcount++;
3375 } while (ret == 1);
3376 if (exec->transcount < atom->min)
3377 ret = 0;
3378
3379 /*
3380 * If the last check failed but one transition was found
3381 * possible, rollback
3382 */
3383 if (ret < 0)
3384 ret = 0;
3385 if (ret == 0) {
3386 goto rollback;
3387 }
3388 }
3389 }
3390 if (ret == 1) {
3391 if (exec->state->nbTrans > exec->transno + 1) {
3392 xmlFARegExecSave(exec);
3393 }
3394 if (trans->counter >= 0) {
3395#ifdef DEBUG_REGEXP_EXEC
3396 printf("Increasing count %d\n", trans->counter);
3397#endif
3398 exec->counts[trans->counter]++;
3399 }
3400#ifdef DEBUG_REGEXP_EXEC
3401 printf("entering state %d\n", trans->to);
3402#endif
3403 exec->state = exec->comp->states[trans->to];
3404 exec->transno = 0;
3405 if (trans->atom != NULL) {
3406 exec->index += len;
3407 }
3408 goto progress;
3409 } else if (ret < 0) {
3410 exec->status = -4;
3411 break;
3412 }
3413 }
3414 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3415rollback:
3416 /*
3417 * Failed to find a way out
3418 */
3419 exec->determinist = 0;
3420 xmlFARegExecRollBack(exec);
3421 }
3422progress:
3423 continue;
3424 }
3425}
3426#endif
3427/************************************************************************
3428 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003429 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00003430 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3431 * *
3432 ************************************************************************/
3433
3434/**
3435 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003436 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003437 *
3438 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3439 */
3440static int
3441xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3442 int cur;
3443 int len;
3444
3445 cur = CUR_SCHAR(ctxt->cur, len);
3446 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3447 (cur == '*') || (cur == '+') || (cur == '(') ||
3448 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3449 (cur == 0x5D) || (cur == 0))
3450 return(-1);
3451 return(cur);
3452}
3453
3454/**
3455 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003456 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003457 *
3458 * [27] charProp ::= IsCategory | IsBlock
3459 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3460 * Separators | Symbols | Others
3461 * [29] Letters ::= 'L' [ultmo]?
3462 * [30] Marks ::= 'M' [nce]?
3463 * [31] Numbers ::= 'N' [dlo]?
3464 * [32] Punctuation ::= 'P' [cdseifo]?
3465 * [33] Separators ::= 'Z' [slp]?
3466 * [34] Symbols ::= 'S' [mcko]?
3467 * [35] Others ::= 'C' [cfon]?
3468 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3469 */
3470static void
3471xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3472 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003473 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003474 xmlChar *blockName = NULL;
3475
3476 cur = CUR;
3477 if (cur == 'L') {
3478 NEXT;
3479 cur = CUR;
3480 if (cur == 'u') {
3481 NEXT;
3482 type = XML_REGEXP_LETTER_UPPERCASE;
3483 } else if (cur == 'l') {
3484 NEXT;
3485 type = XML_REGEXP_LETTER_LOWERCASE;
3486 } else if (cur == 't') {
3487 NEXT;
3488 type = XML_REGEXP_LETTER_TITLECASE;
3489 } else if (cur == 'm') {
3490 NEXT;
3491 type = XML_REGEXP_LETTER_MODIFIER;
3492 } else if (cur == 'o') {
3493 NEXT;
3494 type = XML_REGEXP_LETTER_OTHERS;
3495 } else {
3496 type = XML_REGEXP_LETTER;
3497 }
3498 } else if (cur == 'M') {
3499 NEXT;
3500 cur = CUR;
3501 if (cur == 'n') {
3502 NEXT;
3503 /* nonspacing */
3504 type = XML_REGEXP_MARK_NONSPACING;
3505 } else if (cur == 'c') {
3506 NEXT;
3507 /* spacing combining */
3508 type = XML_REGEXP_MARK_SPACECOMBINING;
3509 } else if (cur == 'e') {
3510 NEXT;
3511 /* enclosing */
3512 type = XML_REGEXP_MARK_ENCLOSING;
3513 } else {
3514 /* all marks */
3515 type = XML_REGEXP_MARK;
3516 }
3517 } else if (cur == 'N') {
3518 NEXT;
3519 cur = CUR;
3520 if (cur == 'd') {
3521 NEXT;
3522 /* digital */
3523 type = XML_REGEXP_NUMBER_DECIMAL;
3524 } else if (cur == 'l') {
3525 NEXT;
3526 /* letter */
3527 type = XML_REGEXP_NUMBER_LETTER;
3528 } else if (cur == 'o') {
3529 NEXT;
3530 /* other */
3531 type = XML_REGEXP_NUMBER_OTHERS;
3532 } else {
3533 /* all numbers */
3534 type = XML_REGEXP_NUMBER;
3535 }
3536 } else if (cur == 'P') {
3537 NEXT;
3538 cur = CUR;
3539 if (cur == 'c') {
3540 NEXT;
3541 /* connector */
3542 type = XML_REGEXP_PUNCT_CONNECTOR;
3543 } else if (cur == 'd') {
3544 NEXT;
3545 /* dash */
3546 type = XML_REGEXP_PUNCT_DASH;
3547 } else if (cur == 's') {
3548 NEXT;
3549 /* open */
3550 type = XML_REGEXP_PUNCT_OPEN;
3551 } else if (cur == 'e') {
3552 NEXT;
3553 /* close */
3554 type = XML_REGEXP_PUNCT_CLOSE;
3555 } else if (cur == 'i') {
3556 NEXT;
3557 /* initial quote */
3558 type = XML_REGEXP_PUNCT_INITQUOTE;
3559 } else if (cur == 'f') {
3560 NEXT;
3561 /* final quote */
3562 type = XML_REGEXP_PUNCT_FINQUOTE;
3563 } else if (cur == 'o') {
3564 NEXT;
3565 /* other */
3566 type = XML_REGEXP_PUNCT_OTHERS;
3567 } else {
3568 /* all punctuation */
3569 type = XML_REGEXP_PUNCT;
3570 }
3571 } else if (cur == 'Z') {
3572 NEXT;
3573 cur = CUR;
3574 if (cur == 's') {
3575 NEXT;
3576 /* space */
3577 type = XML_REGEXP_SEPAR_SPACE;
3578 } else if (cur == 'l') {
3579 NEXT;
3580 /* line */
3581 type = XML_REGEXP_SEPAR_LINE;
3582 } else if (cur == 'p') {
3583 NEXT;
3584 /* paragraph */
3585 type = XML_REGEXP_SEPAR_PARA;
3586 } else {
3587 /* all separators */
3588 type = XML_REGEXP_SEPAR;
3589 }
3590 } else if (cur == 'S') {
3591 NEXT;
3592 cur = CUR;
3593 if (cur == 'm') {
3594 NEXT;
3595 type = XML_REGEXP_SYMBOL_MATH;
3596 /* math */
3597 } else if (cur == 'c') {
3598 NEXT;
3599 type = XML_REGEXP_SYMBOL_CURRENCY;
3600 /* currency */
3601 } else if (cur == 'k') {
3602 NEXT;
3603 type = XML_REGEXP_SYMBOL_MODIFIER;
3604 /* modifiers */
3605 } else if (cur == 'o') {
3606 NEXT;
3607 type = XML_REGEXP_SYMBOL_OTHERS;
3608 /* other */
3609 } else {
3610 /* all symbols */
3611 type = XML_REGEXP_SYMBOL;
3612 }
3613 } else if (cur == 'C') {
3614 NEXT;
3615 cur = CUR;
3616 if (cur == 'c') {
3617 NEXT;
3618 /* control */
3619 type = XML_REGEXP_OTHER_CONTROL;
3620 } else if (cur == 'f') {
3621 NEXT;
3622 /* format */
3623 type = XML_REGEXP_OTHER_FORMAT;
3624 } else if (cur == 'o') {
3625 NEXT;
3626 /* private use */
3627 type = XML_REGEXP_OTHER_PRIVATE;
3628 } else if (cur == 'n') {
3629 NEXT;
3630 /* not assigned */
3631 type = XML_REGEXP_OTHER_NA;
3632 } else {
3633 /* all others */
3634 type = XML_REGEXP_OTHER;
3635 }
3636 } else if (cur == 'I') {
3637 const xmlChar *start;
3638 NEXT;
3639 cur = CUR;
3640 if (cur != 's') {
3641 ERROR("IsXXXX expected");
3642 return;
3643 }
3644 NEXT;
3645 start = ctxt->cur;
3646 cur = CUR;
3647 if (((cur >= 'a') && (cur <= 'z')) ||
3648 ((cur >= 'A') && (cur <= 'Z')) ||
3649 ((cur >= '0') && (cur <= '9')) ||
3650 (cur == 0x2D)) {
3651 NEXT;
3652 cur = CUR;
3653 while (((cur >= 'a') && (cur <= 'z')) ||
3654 ((cur >= 'A') && (cur <= 'Z')) ||
3655 ((cur >= '0') && (cur <= '9')) ||
3656 (cur == 0x2D)) {
3657 NEXT;
3658 cur = CUR;
3659 }
3660 }
3661 type = XML_REGEXP_BLOCK_NAME;
3662 blockName = xmlStrndup(start, ctxt->cur - start);
3663 } else {
3664 ERROR("Unknown char property");
3665 return;
3666 }
3667 if (ctxt->atom == NULL) {
3668 ctxt->atom = xmlRegNewAtom(ctxt, type);
3669 if (ctxt->atom != NULL)
3670 ctxt->atom->valuep = blockName;
3671 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3672 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3673 type, 0, 0, blockName);
3674 }
3675}
3676
3677/**
3678 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003679 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003680 *
3681 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3682 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3683 * [25] catEsc ::= '\p{' charProp '}'
3684 * [26] complEsc ::= '\P{' charProp '}'
3685 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3686 */
3687static void
3688xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3689 int cur;
3690
3691 if (CUR == '.') {
3692 if (ctxt->atom == NULL) {
3693 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3694 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3695 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3696 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3697 }
3698 NEXT;
3699 return;
3700 }
3701 if (CUR != '\\') {
3702 ERROR("Escaped sequence: expecting \\");
3703 return;
3704 }
3705 NEXT;
3706 cur = CUR;
3707 if (cur == 'p') {
3708 NEXT;
3709 if (CUR != '{') {
3710 ERROR("Expecting '{'");
3711 return;
3712 }
3713 NEXT;
3714 xmlFAParseCharProp(ctxt);
3715 if (CUR != '}') {
3716 ERROR("Expecting '}'");
3717 return;
3718 }
3719 NEXT;
3720 } else if (cur == 'P') {
3721 NEXT;
3722 if (CUR != '{') {
3723 ERROR("Expecting '{'");
3724 return;
3725 }
3726 NEXT;
3727 xmlFAParseCharProp(ctxt);
3728 ctxt->atom->neg = 1;
3729 if (CUR != '}') {
3730 ERROR("Expecting '}'");
3731 return;
3732 }
3733 NEXT;
3734 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3735 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3736 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3737 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3738 (cur == 0x5E)) {
3739 if (ctxt->atom == NULL) {
3740 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3741 if (ctxt->atom != NULL)
3742 ctxt->atom->codepoint = cur;
3743 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3744 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3745 XML_REGEXP_CHARVAL, cur, cur, NULL);
3746 }
3747 NEXT;
3748 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3749 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3750 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003751 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003752
3753 switch (cur) {
3754 case 's':
3755 type = XML_REGEXP_ANYSPACE;
3756 break;
3757 case 'S':
3758 type = XML_REGEXP_NOTSPACE;
3759 break;
3760 case 'i':
3761 type = XML_REGEXP_INITNAME;
3762 break;
3763 case 'I':
3764 type = XML_REGEXP_NOTINITNAME;
3765 break;
3766 case 'c':
3767 type = XML_REGEXP_NAMECHAR;
3768 break;
3769 case 'C':
3770 type = XML_REGEXP_NOTNAMECHAR;
3771 break;
3772 case 'd':
3773 type = XML_REGEXP_DECIMAL;
3774 break;
3775 case 'D':
3776 type = XML_REGEXP_NOTDECIMAL;
3777 break;
3778 case 'w':
3779 type = XML_REGEXP_REALCHAR;
3780 break;
3781 case 'W':
3782 type = XML_REGEXP_NOTREALCHAR;
3783 break;
3784 }
3785 NEXT;
3786 if (ctxt->atom == NULL) {
3787 ctxt->atom = xmlRegNewAtom(ctxt, type);
3788 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3789 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3790 type, 0, 0, NULL);
3791 }
3792 }
3793}
3794
3795/**
3796 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003797 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003798 *
3799 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3800 */
3801static int
3802xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3803 int ret = 0, cur;
3804
3805 if ((CUR != '&') || (NXT(1) != '#'))
3806 return(-1);
3807 NEXT;
3808 NEXT;
3809 cur = CUR;
3810 if (cur == 'x') {
3811 NEXT;
3812 cur = CUR;
3813 if (((cur >= '0') && (cur <= '9')) ||
3814 ((cur >= 'a') && (cur <= 'f')) ||
3815 ((cur >= 'A') && (cur <= 'F'))) {
3816 while (((cur >= '0') && (cur <= '9')) ||
3817 ((cur >= 'A') && (cur <= 'F'))) {
3818 if ((cur >= '0') && (cur <= '9'))
3819 ret = ret * 16 + cur - '0';
3820 else if ((cur >= 'a') && (cur <= 'f'))
3821 ret = ret * 16 + 10 + (cur - 'a');
3822 else
3823 ret = ret * 16 + 10 + (cur - 'A');
3824 NEXT;
3825 cur = CUR;
3826 }
3827 } else {
3828 ERROR("Char ref: expecting [0-9A-F]");
3829 return(-1);
3830 }
3831 } else {
3832 if ((cur >= '0') && (cur <= '9')) {
3833 while ((cur >= '0') && (cur <= '9')) {
3834 ret = ret * 10 + cur - '0';
3835 NEXT;
3836 cur = CUR;
3837 }
3838 } else {
3839 ERROR("Char ref: expecting [0-9]");
3840 return(-1);
3841 }
3842 }
3843 if (cur != ';') {
3844 ERROR("Char ref: expecting ';'");
3845 return(-1);
3846 } else {
3847 NEXT;
3848 }
3849 return(ret);
3850}
3851
3852/**
3853 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003854 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003855 *
3856 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3857 * [18] seRange ::= charOrEsc '-' charOrEsc
3858 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3859 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3860 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3861 */
3862static void
3863xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00003864 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00003865 int start = -1;
3866 int end = -1;
3867
3868 if ((CUR == '&') && (NXT(1) == '#')) {
3869 end = start = xmlFAParseCharRef(ctxt);
3870 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3871 XML_REGEXP_CHARVAL, start, end, NULL);
3872 return;
3873 }
3874 cur = CUR;
3875 if (cur == '\\') {
3876 NEXT;
3877 cur = CUR;
3878 switch (cur) {
3879 case 'n': start = 0xA; break;
3880 case 'r': start = 0xD; break;
3881 case 't': start = 0x9; break;
3882 case '\\': case '|': case '.': case '-': case '^': case '?':
3883 case '*': case '+': case '{': case '}': case '(': case ')':
3884 case '[': case ']':
3885 start = cur; break;
3886 default:
3887 ERROR("Invalid escape value");
3888 return;
3889 }
3890 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00003891 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003892 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003893 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003894 } else {
3895 ERROR("Expecting a char range");
3896 return;
3897 }
William M. Brackdc99df92003-12-27 01:54:25 +00003898 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003899 if (start == '-') {
3900 return;
3901 }
3902 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00003903 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003904 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3905 XML_REGEXP_CHARVAL, start, end, NULL);
3906 return;
3907 }
3908 NEXT;
3909 cur = CUR;
3910 if (cur == '\\') {
3911 NEXT;
3912 cur = CUR;
3913 switch (cur) {
3914 case 'n': end = 0xA; break;
3915 case 'r': end = 0xD; break;
3916 case 't': end = 0x9; break;
3917 case '\\': case '|': case '.': case '-': case '^': case '?':
3918 case '*': case '+': case '{': case '}': case '(': case ')':
3919 case '[': case ']':
3920 end = cur; break;
3921 default:
3922 ERROR("Invalid escape value");
3923 return;
3924 }
William M. Brackdc99df92003-12-27 01:54:25 +00003925 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003926 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003927 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003928 } else {
3929 ERROR("Expecting the end of a char range");
3930 return;
3931 }
William M. Brackdc99df92003-12-27 01:54:25 +00003932 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003933 /* TODO check that the values are acceptable character ranges for XML */
3934 if (end < start) {
3935 ERROR("End of range is before start of range");
3936 } else {
3937 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3938 XML_REGEXP_CHARVAL, start, end, NULL);
3939 }
3940 return;
3941}
3942
3943/**
3944 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003945 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003946 *
3947 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3948 */
3949static void
3950xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3951 do {
3952 if ((CUR == '\\') || (CUR == '.')) {
3953 xmlFAParseCharClassEsc(ctxt);
3954 } else {
3955 xmlFAParseCharRange(ctxt);
3956 }
3957 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3958 (ctxt->error == 0));
3959}
3960
3961/**
3962 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003963 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003964 *
3965 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3966 * [15] negCharGroup ::= '^' posCharGroup
3967 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3968 * [12] charClassExpr ::= '[' charGroup ']'
3969 */
3970static void
3971xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3972 int n = ctxt->neg;
3973 while ((CUR != ']') && (ctxt->error == 0)) {
3974 if (CUR == '^') {
3975 int neg = ctxt->neg;
3976
3977 NEXT;
3978 ctxt->neg = !ctxt->neg;
3979 xmlFAParsePosCharGroup(ctxt);
3980 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00003981 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003982 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003983 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00003984 NEXT; /* eat the '-' */
3985 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00003986 xmlFAParseCharGroup(ctxt);
3987 if (CUR == ']') {
3988 NEXT;
3989 } else {
3990 ERROR("charClassExpr: ']' expected");
3991 break;
3992 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003993 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00003994 break;
3995 } else if (CUR != ']') {
3996 xmlFAParsePosCharGroup(ctxt);
3997 }
3998 }
3999 ctxt->neg = n;
4000}
4001
4002/**
4003 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00004004 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004005 *
4006 * [11] charClass ::= charClassEsc | charClassExpr
4007 * [12] charClassExpr ::= '[' charGroup ']'
4008 */
4009static void
4010xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
4011 if (CUR == '[') {
4012 NEXT;
4013 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
4014 if (ctxt->atom == NULL)
4015 return;
4016 xmlFAParseCharGroup(ctxt);
4017 if (CUR == ']') {
4018 NEXT;
4019 } else {
4020 ERROR("xmlFAParseCharClass: ']' expected");
4021 }
4022 } else {
4023 xmlFAParseCharClassEsc(ctxt);
4024 }
4025}
4026
4027/**
4028 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00004029 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004030 *
4031 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004032 *
4033 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004034 */
4035static int
4036xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
4037 int ret = 0;
4038 int ok = 0;
4039
4040 while ((CUR >= '0') && (CUR <= '9')) {
4041 ret = ret * 10 + (CUR - '0');
4042 ok = 1;
4043 NEXT;
4044 }
4045 if (ok != 1) {
4046 return(-1);
4047 }
4048 return(ret);
4049}
4050
4051/**
4052 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00004053 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004054 *
4055 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
4056 * [5] quantity ::= quantRange | quantMin | QuantExact
4057 * [6] quantRange ::= QuantExact ',' QuantExact
4058 * [7] quantMin ::= QuantExact ','
4059 * [8] QuantExact ::= [0-9]+
4060 */
4061static int
4062xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
4063 int cur;
4064
4065 cur = CUR;
4066 if ((cur == '?') || (cur == '*') || (cur == '+')) {
4067 if (ctxt->atom != NULL) {
4068 if (cur == '?')
4069 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
4070 else if (cur == '*')
4071 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
4072 else if (cur == '+')
4073 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
4074 }
4075 NEXT;
4076 return(1);
4077 }
4078 if (cur == '{') {
4079 int min = 0, max = 0;
4080
4081 NEXT;
4082 cur = xmlFAParseQuantExact(ctxt);
4083 if (cur >= 0)
4084 min = cur;
4085 if (CUR == ',') {
4086 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00004087 if (CUR == '}')
4088 max = INT_MAX;
4089 else {
4090 cur = xmlFAParseQuantExact(ctxt);
4091 if (cur >= 0)
4092 max = cur;
4093 else {
4094 ERROR("Improper quantifier");
4095 }
4096 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004097 }
4098 if (CUR == '}') {
4099 NEXT;
4100 } else {
4101 ERROR("Unterminated quantifier");
4102 }
4103 if (max == 0)
4104 max = min;
4105 if (ctxt->atom != NULL) {
4106 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
4107 ctxt->atom->min = min;
4108 ctxt->atom->max = max;
4109 }
4110 return(1);
4111 }
4112 return(0);
4113}
4114
4115/**
4116 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00004117 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004118 *
4119 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
4120 */
4121static int
4122xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
4123 int codepoint, len;
4124
4125 codepoint = xmlFAIsChar(ctxt);
4126 if (codepoint > 0) {
4127 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
4128 if (ctxt->atom == NULL)
4129 return(-1);
4130 codepoint = CUR_SCHAR(ctxt->cur, len);
4131 ctxt->atom->codepoint = codepoint;
4132 NEXTL(len);
4133 return(1);
4134 } else if (CUR == '|') {
4135 return(0);
4136 } else if (CUR == 0) {
4137 return(0);
4138 } else if (CUR == ')') {
4139 return(0);
4140 } else if (CUR == '(') {
4141 xmlRegStatePtr start, oldend;
4142
4143 NEXT;
4144 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
4145 start = ctxt->state;
4146 oldend = ctxt->end;
4147 ctxt->end = NULL;
4148 ctxt->atom = NULL;
4149 xmlFAParseRegExp(ctxt, 0);
4150 if (CUR == ')') {
4151 NEXT;
4152 } else {
4153 ERROR("xmlFAParseAtom: expecting ')'");
4154 }
4155 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
4156 if (ctxt->atom == NULL)
4157 return(-1);
4158 ctxt->atom->start = start;
4159 ctxt->atom->stop = ctxt->state;
4160 ctxt->end = oldend;
4161 return(1);
4162 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
4163 xmlFAParseCharClass(ctxt);
4164 return(1);
4165 }
4166 return(0);
4167}
4168
4169/**
4170 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00004171 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004172 *
4173 * [3] piece ::= atom quantifier?
4174 */
4175static int
4176xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
4177 int ret;
4178
4179 ctxt->atom = NULL;
4180 ret = xmlFAParseAtom(ctxt);
4181 if (ret == 0)
4182 return(0);
4183 if (ctxt->atom == NULL) {
4184 ERROR("internal: no atom generated");
4185 }
4186 xmlFAParseQuantifier(ctxt);
4187 return(1);
4188}
4189
4190/**
4191 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00004192 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00004193 *
4194 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004195 8
Daniel Veillard4255d502002-04-16 15:50:10 +00004196 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004197static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004198xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00004199 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00004200 int ret;
4201
4202 previous = ctxt->state;
4203 ret = xmlFAParsePiece(ctxt);
4204 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004205 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
4206 return(-1);
4207 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00004208 ctxt->atom = NULL;
4209 }
4210 while ((ret != 0) && (ctxt->error == 0)) {
4211 ret = xmlFAParsePiece(ctxt);
4212 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004213 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
4214 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004215 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00004216 previous = ctxt->state;
4217 ctxt->atom = NULL;
4218 }
4219 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004220 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00004221}
4222
4223/**
4224 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004225 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00004226 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00004227 *
4228 * [1] regExp ::= branch ( '|' branch )*
4229 */
4230static void
4231xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillardc7e3cc42004-09-28 12:33:52 +00004232 xmlRegStatePtr start, end;
Daniel Veillard4255d502002-04-16 15:50:10 +00004233
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004234 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00004235 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004236 ctxt->end = NULL;
4237 xmlFAParseBranch(ctxt);
4238 if (top) {
4239#ifdef DEBUG_REGEXP_GRAPH
4240 printf("State %d is final\n", ctxt->state->no);
4241#endif
4242 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4243 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004244 if (CUR != '|') {
4245 ctxt->end = ctxt->state;
4246 return;
4247 }
4248 end = ctxt->state;
4249 while ((CUR == '|') && (ctxt->error == 0)) {
4250 NEXT;
4251 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004252 ctxt->end = NULL;
4253 xmlFAParseBranch(ctxt);
4254 if (top) {
4255 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4256#ifdef DEBUG_REGEXP_GRAPH
4257 printf("State %d is final\n", ctxt->state->no);
4258#endif
4259 } else {
4260 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4261 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004262 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004263 if (!top) {
4264 ctxt->state = end;
4265 ctxt->end = end;
4266 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004267}
4268
4269/************************************************************************
4270 * *
4271 * The basic API *
4272 * *
4273 ************************************************************************/
4274
4275/**
4276 * xmlRegexpPrint:
4277 * @output: the file for the output debug
4278 * @regexp: the compiled regexp
4279 *
4280 * Print the content of the compiled regular expression
4281 */
4282void
4283xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4284 int i;
4285
Daniel Veillarda82b1822004-11-08 16:24:57 +00004286 if (output == NULL)
4287 return;
Daniel Veillard4255d502002-04-16 15:50:10 +00004288 fprintf(output, " regexp: ");
4289 if (regexp == NULL) {
4290 fprintf(output, "NULL\n");
4291 return;
4292 }
4293 fprintf(output, "'%s' ", regexp->string);
4294 fprintf(output, "\n");
4295 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4296 for (i = 0;i < regexp->nbAtoms; i++) {
4297 fprintf(output, " %02d ", i);
4298 xmlRegPrintAtom(output, regexp->atoms[i]);
4299 }
4300 fprintf(output, "%d states:", regexp->nbStates);
4301 fprintf(output, "\n");
4302 for (i = 0;i < regexp->nbStates; i++) {
4303 xmlRegPrintState(output, regexp->states[i]);
4304 }
4305 fprintf(output, "%d counters:\n", regexp->nbCounters);
4306 for (i = 0;i < regexp->nbCounters; i++) {
4307 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4308 regexp->counters[i].max);
4309 }
4310}
4311
4312/**
4313 * xmlRegexpCompile:
4314 * @regexp: a regular expression string
4315 *
4316 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004317 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004318 * that regular expression
4319 *
4320 * Returns the compiled expression or NULL in case of error
4321 */
4322xmlRegexpPtr
4323xmlRegexpCompile(const xmlChar *regexp) {
4324 xmlRegexpPtr ret;
4325 xmlRegParserCtxtPtr ctxt;
4326
4327 ctxt = xmlRegNewParserCtxt(regexp);
4328 if (ctxt == NULL)
4329 return(NULL);
4330
4331 /* initialize the parser */
4332 ctxt->end = NULL;
4333 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4334 xmlRegStatePush(ctxt, ctxt->start);
4335
4336 /* parse the expression building an automata */
4337 xmlFAParseRegExp(ctxt, 1);
4338 if (CUR != 0) {
4339 ERROR("xmlFAParseRegExp: extra characters");
4340 }
4341 ctxt->end = ctxt->state;
4342 ctxt->start->type = XML_REGEXP_START_STATE;
4343 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4344
4345 /* remove the Epsilon except for counted transitions */
4346 xmlFAEliminateEpsilonTransitions(ctxt);
4347
4348
4349 if (ctxt->error != 0) {
4350 xmlRegFreeParserCtxt(ctxt);
4351 return(NULL);
4352 }
4353 ret = xmlRegEpxFromParse(ctxt);
4354 xmlRegFreeParserCtxt(ctxt);
4355 return(ret);
4356}
4357
4358/**
4359 * xmlRegexpExec:
4360 * @comp: the compiled regular expression
4361 * @content: the value to check against the regular expression
4362 *
William M. Brackddf71d62004-05-06 04:17:26 +00004363 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004364 *
William M. Brackddf71d62004-05-06 04:17:26 +00004365 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004366 */
4367int
4368xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4369 if ((comp == NULL) || (content == NULL))
4370 return(-1);
4371 return(xmlFARegExec(comp, content));
4372}
4373
4374/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004375 * xmlRegexpIsDeterminist:
4376 * @comp: the compiled regular expression
4377 *
4378 * Check if the regular expression is determinist
4379 *
William M. Brackddf71d62004-05-06 04:17:26 +00004380 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004381 */
4382int
4383xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4384 xmlAutomataPtr am;
4385 int ret;
4386
4387 if (comp == NULL)
4388 return(-1);
4389 if (comp->determinist != -1)
4390 return(comp->determinist);
4391
4392 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004393 if (am->states != NULL) {
4394 int i;
4395
4396 for (i = 0;i < am->nbStates;i++)
4397 xmlRegFreeState(am->states[i]);
4398 xmlFree(am->states);
4399 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004400 am->nbAtoms = comp->nbAtoms;
4401 am->atoms = comp->atoms;
4402 am->nbStates = comp->nbStates;
4403 am->states = comp->states;
4404 am->determinist = -1;
4405 ret = xmlFAComputesDeterminism(am);
4406 am->atoms = NULL;
4407 am->states = NULL;
4408 xmlFreeAutomata(am);
4409 return(ret);
4410}
4411
4412/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004413 * xmlRegFreeRegexp:
4414 * @regexp: the regexp
4415 *
4416 * Free a regexp
4417 */
4418void
4419xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4420 int i;
4421 if (regexp == NULL)
4422 return;
4423
4424 if (regexp->string != NULL)
4425 xmlFree(regexp->string);
4426 if (regexp->states != NULL) {
4427 for (i = 0;i < regexp->nbStates;i++)
4428 xmlRegFreeState(regexp->states[i]);
4429 xmlFree(regexp->states);
4430 }
4431 if (regexp->atoms != NULL) {
4432 for (i = 0;i < regexp->nbAtoms;i++)
4433 xmlRegFreeAtom(regexp->atoms[i]);
4434 xmlFree(regexp->atoms);
4435 }
4436 if (regexp->counters != NULL)
4437 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004438 if (regexp->compact != NULL)
4439 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004440 if (regexp->transdata != NULL)
4441 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004442 if (regexp->stringMap != NULL) {
4443 for (i = 0; i < regexp->nbstrings;i++)
4444 xmlFree(regexp->stringMap[i]);
4445 xmlFree(regexp->stringMap);
4446 }
4447
Daniel Veillard4255d502002-04-16 15:50:10 +00004448 xmlFree(regexp);
4449}
4450
4451#ifdef LIBXML_AUTOMATA_ENABLED
4452/************************************************************************
4453 * *
4454 * The Automata interface *
4455 * *
4456 ************************************************************************/
4457
4458/**
4459 * xmlNewAutomata:
4460 *
4461 * Create a new automata
4462 *
4463 * Returns the new object or NULL in case of failure
4464 */
4465xmlAutomataPtr
4466xmlNewAutomata(void) {
4467 xmlAutomataPtr ctxt;
4468
4469 ctxt = xmlRegNewParserCtxt(NULL);
4470 if (ctxt == NULL)
4471 return(NULL);
4472
4473 /* initialize the parser */
4474 ctxt->end = NULL;
4475 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004476 if (ctxt->start == NULL) {
4477 xmlFreeAutomata(ctxt);
4478 return(NULL);
4479 }
4480 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4481 xmlRegFreeState(ctxt->start);
4482 xmlFreeAutomata(ctxt);
4483 return(NULL);
4484 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004485
4486 return(ctxt);
4487}
4488
4489/**
4490 * xmlFreeAutomata:
4491 * @am: an automata
4492 *
4493 * Free an automata
4494 */
4495void
4496xmlFreeAutomata(xmlAutomataPtr am) {
4497 if (am == NULL)
4498 return;
4499 xmlRegFreeParserCtxt(am);
4500}
4501
4502/**
4503 * xmlAutomataGetInitState:
4504 * @am: an automata
4505 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004506 * Initial state lookup
4507 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004508 * Returns the initial state of the automata
4509 */
4510xmlAutomataStatePtr
4511xmlAutomataGetInitState(xmlAutomataPtr am) {
4512 if (am == NULL)
4513 return(NULL);
4514 return(am->start);
4515}
4516
4517/**
4518 * xmlAutomataSetFinalState:
4519 * @am: an automata
4520 * @state: a state in this automata
4521 *
4522 * Makes that state a final state
4523 *
4524 * Returns 0 or -1 in case of error
4525 */
4526int
4527xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4528 if ((am == NULL) || (state == NULL))
4529 return(-1);
4530 state->type = XML_REGEXP_FINAL_STATE;
4531 return(0);
4532}
4533
4534/**
4535 * xmlAutomataNewTransition:
4536 * @am: an automata
4537 * @from: the starting point of the transition
4538 * @to: the target point of the transition or NULL
4539 * @token: the input string associated to that transition
4540 * @data: data passed to the callback function if the transition is activated
4541 *
William M. Brackddf71d62004-05-06 04:17:26 +00004542 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004543 * and then adds a transition from the @from state to the target state
4544 * activated by the value of @token
4545 *
4546 * Returns the target state or NULL in case of error
4547 */
4548xmlAutomataStatePtr
4549xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4550 xmlAutomataStatePtr to, const xmlChar *token,
4551 void *data) {
4552 xmlRegAtomPtr atom;
4553
4554 if ((am == NULL) || (from == NULL) || (token == NULL))
4555 return(NULL);
4556 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004557 if (atom == NULL)
4558 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004559 atom->data = data;
4560 if (atom == NULL)
4561 return(NULL);
4562 atom->valuep = xmlStrdup(token);
4563
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004564 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4565 xmlRegFreeAtom(atom);
4566 return(NULL);
4567 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004568 if (to == NULL)
4569 return(am->state);
4570 return(to);
4571}
4572
4573/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004574 * xmlAutomataNewTransition2:
4575 * @am: an automata
4576 * @from: the starting point of the transition
4577 * @to: the target point of the transition or NULL
4578 * @token: the first input string associated to that transition
4579 * @token2: the second input string associated to that transition
4580 * @data: data passed to the callback function if the transition is activated
4581 *
William M. Brackddf71d62004-05-06 04:17:26 +00004582 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00004583 * and then adds a transition from the @from state to the target state
4584 * activated by the value of @token
4585 *
4586 * Returns the target state or NULL in case of error
4587 */
4588xmlAutomataStatePtr
4589xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4590 xmlAutomataStatePtr to, const xmlChar *token,
4591 const xmlChar *token2, void *data) {
4592 xmlRegAtomPtr atom;
4593
4594 if ((am == NULL) || (from == NULL) || (token == NULL))
4595 return(NULL);
4596 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4597 atom->data = data;
4598 if (atom == NULL)
4599 return(NULL);
4600 if ((token2 == NULL) || (*token2 == 0)) {
4601 atom->valuep = xmlStrdup(token);
4602 } else {
4603 int lenn, lenp;
4604 xmlChar *str;
4605
4606 lenn = strlen((char *) token2);
4607 lenp = strlen((char *) token);
4608
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004609 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004610 if (str == NULL) {
4611 xmlRegFreeAtom(atom);
4612 return(NULL);
4613 }
4614 memcpy(&str[0], token, lenp);
4615 str[lenp] = '|';
4616 memcpy(&str[lenp + 1], token2, lenn);
4617 str[lenn + lenp + 1] = 0;
4618
4619 atom->valuep = str;
4620 }
4621
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004622 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4623 xmlRegFreeAtom(atom);
4624 return(NULL);
4625 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004626 if (to == NULL)
4627 return(am->state);
4628 return(to);
4629}
4630
4631/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00004632 * xmlAutomataNewCountTrans2:
4633 * @am: an automata
4634 * @from: the starting point of the transition
4635 * @to: the target point of the transition or NULL
4636 * @token: the input string associated to that transition
4637 * @token2: the second input string associated to that transition
4638 * @min: the minimum successive occurences of token
4639 * @max: the maximum successive occurences of token
4640 * @data: data associated to the transition
4641 *
4642 * If @to is NULL, this creates first a new target state in the automata
4643 * and then adds a transition from the @from state to the target state
4644 * activated by a succession of input of value @token and @token2 and
4645 * whose number is between @min and @max
4646 *
4647 * Returns the target state or NULL in case of error
4648 */
4649xmlAutomataStatePtr
4650xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4651 xmlAutomataStatePtr to, const xmlChar *token,
4652 const xmlChar *token2,
4653 int min, int max, void *data) {
4654 xmlRegAtomPtr atom;
4655 int counter;
4656
4657 if ((am == NULL) || (from == NULL) || (token == NULL))
4658 return(NULL);
4659 if (min < 0)
4660 return(NULL);
4661 if ((max < min) || (max < 1))
4662 return(NULL);
4663 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4664 if (atom == NULL)
4665 return(NULL);
4666 if ((token2 == NULL) || (*token2 == 0)) {
4667 atom->valuep = xmlStrdup(token);
4668 } else {
4669 int lenn, lenp;
4670 xmlChar *str;
4671
4672 lenn = strlen((char *) token2);
4673 lenp = strlen((char *) token);
4674
4675 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4676 if (str == NULL) {
4677 xmlRegFreeAtom(atom);
4678 return(NULL);
4679 }
4680 memcpy(&str[0], token, lenp);
4681 str[lenp] = '|';
4682 memcpy(&str[lenp + 1], token2, lenn);
4683 str[lenn + lenp + 1] = 0;
4684
4685 atom->valuep = str;
4686 }
4687 atom->data = data;
4688 if (min == 0)
4689 atom->min = 1;
4690 else
4691 atom->min = min;
4692 atom->max = max;
4693
4694 /*
4695 * associate a counter to the transition.
4696 */
4697 counter = xmlRegGetCounter(am);
4698 am->counters[counter].min = min;
4699 am->counters[counter].max = max;
4700
4701 /* xmlFAGenerateTransitions(am, from, to, atom); */
4702 if (to == NULL) {
4703 to = xmlRegNewState(am);
4704 xmlRegStatePush(am, to);
4705 }
4706 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4707 xmlRegAtomPush(am, atom);
4708 am->state = to;
4709
4710 if (to == NULL)
4711 to = am->state;
4712 if (to == NULL)
4713 return(NULL);
4714 if (min == 0)
4715 xmlFAGenerateEpsilonTransition(am, from, to);
4716 return(to);
4717}
4718
4719/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004720 * xmlAutomataNewCountTrans:
4721 * @am: an automata
4722 * @from: the starting point of the transition
4723 * @to: the target point of the transition or NULL
4724 * @token: the input string associated to that transition
4725 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004726 * @max: the maximum successive occurences of token
4727 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004728 *
William M. Brackddf71d62004-05-06 04:17:26 +00004729 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004730 * and then adds a transition from the @from state to the target state
4731 * activated by a succession of input of value @token and whose number
4732 * is between @min and @max
4733 *
4734 * Returns the target state or NULL in case of error
4735 */
4736xmlAutomataStatePtr
4737xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4738 xmlAutomataStatePtr to, const xmlChar *token,
4739 int min, int max, void *data) {
4740 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004741 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00004742
4743 if ((am == NULL) || (from == NULL) || (token == NULL))
4744 return(NULL);
4745 if (min < 0)
4746 return(NULL);
4747 if ((max < min) || (max < 1))
4748 return(NULL);
4749 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4750 if (atom == NULL)
4751 return(NULL);
4752 atom->valuep = xmlStrdup(token);
4753 atom->data = data;
4754 if (min == 0)
4755 atom->min = 1;
4756 else
4757 atom->min = min;
4758 atom->max = max;
4759
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004760 /*
4761 * associate a counter to the transition.
4762 */
4763 counter = xmlRegGetCounter(am);
4764 am->counters[counter].min = min;
4765 am->counters[counter].max = max;
4766
4767 /* xmlFAGenerateTransitions(am, from, to, atom); */
4768 if (to == NULL) {
4769 to = xmlRegNewState(am);
4770 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004771 }
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004772 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4773 xmlRegAtomPush(am, atom);
4774 am->state = to;
4775
Daniel Veillard4255d502002-04-16 15:50:10 +00004776 if (to == NULL)
4777 to = am->state;
4778 if (to == NULL)
4779 return(NULL);
4780 if (min == 0)
4781 xmlFAGenerateEpsilonTransition(am, from, to);
4782 return(to);
4783}
4784
4785/**
Kasimier T. Buchcik87876402004-09-29 13:29:03 +00004786 * xmlAutomataNewOnceTrans2:
4787 * @am: an automata
4788 * @from: the starting point of the transition
4789 * @to: the target point of the transition or NULL
4790 * @token: the input string associated to that transition
4791 * @token2: the second input string associated to that transition
4792 * @min: the minimum successive occurences of token
4793 * @max: the maximum successive occurences of token
4794 * @data: data associated to the transition
4795 *
4796 * If @to is NULL, this creates first a new target state in the automata
4797 * and then adds a transition from the @from state to the target state
4798 * activated by a succession of input of value @token and @token2 and whose
4799 * number is between @min and @max, moreover that transition can only be
4800 * crossed once.
4801 *
4802 * Returns the target state or NULL in case of error
4803 */
4804xmlAutomataStatePtr
4805xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4806 xmlAutomataStatePtr to, const xmlChar *token,
4807 const xmlChar *token2,
4808 int min, int max, void *data) {
4809 xmlRegAtomPtr atom;
4810 int counter;
4811
4812 if ((am == NULL) || (from == NULL) || (token == NULL))
4813 return(NULL);
4814 if (min < 1)
4815 return(NULL);
4816 if ((max < min) || (max < 1))
4817 return(NULL);
4818 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4819 if (atom == NULL)
4820 return(NULL);
4821 if ((token2 == NULL) || (*token2 == 0)) {
4822 atom->valuep = xmlStrdup(token);
4823 } else {
4824 int lenn, lenp;
4825 xmlChar *str;
4826
4827 lenn = strlen((char *) token2);
4828 lenp = strlen((char *) token);
4829
4830 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
4831 if (str == NULL) {
4832 xmlRegFreeAtom(atom);
4833 return(NULL);
4834 }
4835 memcpy(&str[0], token, lenp);
4836 str[lenp] = '|';
4837 memcpy(&str[lenp + 1], token2, lenn);
4838 str[lenn + lenp + 1] = 0;
4839
4840 atom->valuep = str;
4841 }
4842 atom->data = data;
4843 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4844 if (min == 0)
4845 atom->min = 1;
4846 else
4847 atom->min = min;
4848 atom->max = max;
4849 /*
4850 * associate a counter to the transition.
4851 */
4852 counter = xmlRegGetCounter(am);
4853 am->counters[counter].min = 1;
4854 am->counters[counter].max = 1;
4855
4856 /* xmlFAGenerateTransitions(am, from, to, atom); */
4857 if (to == NULL) {
4858 to = xmlRegNewState(am);
4859 xmlRegStatePush(am, to);
4860 }
4861 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4862 xmlRegAtomPush(am, atom);
4863 am->state = to;
4864 return(to);
4865}
4866
4867
4868
4869/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004870 * xmlAutomataNewOnceTrans:
4871 * @am: an automata
4872 * @from: the starting point of the transition
4873 * @to: the target point of the transition or NULL
4874 * @token: the input string associated to that transition
4875 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004876 * @max: the maximum successive occurences of token
4877 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00004878 *
William M. Brackddf71d62004-05-06 04:17:26 +00004879 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00004880 * and then adds a transition from the @from state to the target state
4881 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00004882 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00004883 * once.
4884 *
4885 * Returns the target state or NULL in case of error
4886 */
4887xmlAutomataStatePtr
4888xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4889 xmlAutomataStatePtr to, const xmlChar *token,
4890 int min, int max, void *data) {
4891 xmlRegAtomPtr atom;
4892 int counter;
4893
4894 if ((am == NULL) || (from == NULL) || (token == NULL))
4895 return(NULL);
4896 if (min < 1)
4897 return(NULL);
4898 if ((max < min) || (max < 1))
4899 return(NULL);
4900 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4901 if (atom == NULL)
4902 return(NULL);
4903 atom->valuep = xmlStrdup(token);
4904 atom->data = data;
4905 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4906 if (min == 0)
4907 atom->min = 1;
4908 else
4909 atom->min = min;
4910 atom->max = max;
4911 /*
4912 * associate a counter to the transition.
4913 */
4914 counter = xmlRegGetCounter(am);
4915 am->counters[counter].min = 1;
4916 am->counters[counter].max = 1;
4917
4918 /* xmlFAGenerateTransitions(am, from, to, atom); */
4919 if (to == NULL) {
4920 to = xmlRegNewState(am);
4921 xmlRegStatePush(am, to);
4922 }
4923 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4924 xmlRegAtomPush(am, atom);
4925 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00004926 return(to);
4927}
4928
4929/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004930 * xmlAutomataNewState:
4931 * @am: an automata
4932 *
4933 * Create a new disconnected state in the automata
4934 *
4935 * Returns the new state or NULL in case of error
4936 */
4937xmlAutomataStatePtr
4938xmlAutomataNewState(xmlAutomataPtr am) {
4939 xmlAutomataStatePtr to;
4940
4941 if (am == NULL)
4942 return(NULL);
4943 to = xmlRegNewState(am);
4944 xmlRegStatePush(am, to);
4945 return(to);
4946}
4947
4948/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004949 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004950 * @am: an automata
4951 * @from: the starting point of the transition
4952 * @to: the target point of the transition or NULL
4953 *
William M. Brackddf71d62004-05-06 04:17:26 +00004954 * If @to is NULL, this creates first a new target state in the automata
4955 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00004956 * target state
4957 *
4958 * Returns the target state or NULL in case of error
4959 */
4960xmlAutomataStatePtr
4961xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4962 xmlAutomataStatePtr to) {
4963 if ((am == NULL) || (from == NULL))
4964 return(NULL);
4965 xmlFAGenerateEpsilonTransition(am, from, to);
4966 if (to == NULL)
4967 return(am->state);
4968 return(to);
4969}
4970
Daniel Veillardb509f152002-04-17 16:28:10 +00004971/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004972 * xmlAutomataNewAllTrans:
4973 * @am: an automata
4974 * @from: the starting point of the transition
4975 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004976 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004977 *
William M. Brackddf71d62004-05-06 04:17:26 +00004978 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00004979 * and then adds a an ALL transition from the @from state to the
4980 * target state. That transition is an epsilon transition allowed only when
4981 * all transitions from the @from node have been activated.
4982 *
4983 * Returns the target state or NULL in case of error
4984 */
4985xmlAutomataStatePtr
4986xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004987 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004988 if ((am == NULL) || (from == NULL))
4989 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004990 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004991 if (to == NULL)
4992 return(am->state);
4993 return(to);
4994}
4995
4996/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004997 * xmlAutomataNewCounter:
4998 * @am: an automata
4999 * @min: the minimal value on the counter
5000 * @max: the maximal value on the counter
5001 *
5002 * Create a new counter
5003 *
5004 * Returns the counter number or -1 in case of error
5005 */
5006int
5007xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
5008 int ret;
5009
5010 if (am == NULL)
5011 return(-1);
5012
5013 ret = xmlRegGetCounter(am);
5014 if (ret < 0)
5015 return(-1);
5016 am->counters[ret].min = min;
5017 am->counters[ret].max = max;
5018 return(ret);
5019}
5020
5021/**
5022 * xmlAutomataNewCountedTrans:
5023 * @am: an automata
5024 * @from: the starting point of the transition
5025 * @to: the target point of the transition or NULL
5026 * @counter: the counter associated to that transition
5027 *
William M. Brackddf71d62004-05-06 04:17:26 +00005028 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005029 * and then adds an epsilon transition from the @from state to the target state
5030 * which will increment the counter provided
5031 *
5032 * Returns the target state or NULL in case of error
5033 */
5034xmlAutomataStatePtr
5035xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5036 xmlAutomataStatePtr to, int counter) {
5037 if ((am == NULL) || (from == NULL) || (counter < 0))
5038 return(NULL);
5039 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
5040 if (to == NULL)
5041 return(am->state);
5042 return(to);
5043}
5044
5045/**
5046 * xmlAutomataNewCounterTrans:
5047 * @am: an automata
5048 * @from: the starting point of the transition
5049 * @to: the target point of the transition or NULL
5050 * @counter: the counter associated to that transition
5051 *
William M. Brackddf71d62004-05-06 04:17:26 +00005052 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00005053 * and then adds an epsilon transition from the @from state to the target state
5054 * which will be allowed only if the counter is within the right range.
5055 *
5056 * Returns the target state or NULL in case of error
5057 */
5058xmlAutomataStatePtr
5059xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
5060 xmlAutomataStatePtr to, int counter) {
5061 if ((am == NULL) || (from == NULL) || (counter < 0))
5062 return(NULL);
5063 xmlFAGenerateCountedTransition(am, from, to, counter);
5064 if (to == NULL)
5065 return(am->state);
5066 return(to);
5067}
Daniel Veillard4255d502002-04-16 15:50:10 +00005068
5069/**
5070 * xmlAutomataCompile:
5071 * @am: an automata
5072 *
5073 * Compile the automata into a Reg Exp ready for being executed.
5074 * The automata should be free after this point.
5075 *
5076 * Returns the compiled regexp or NULL in case of error
5077 */
5078xmlRegexpPtr
5079xmlAutomataCompile(xmlAutomataPtr am) {
5080 xmlRegexpPtr ret;
5081
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00005082 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00005083 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00005084 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00005085 ret = xmlRegEpxFromParse(am);
5086
5087 return(ret);
5088}
Daniel Veillarde19fc232002-04-22 16:01:24 +00005089
5090/**
5091 * xmlAutomataIsDeterminist:
5092 * @am: an automata
5093 *
5094 * Checks if an automata is determinist.
5095 *
5096 * Returns 1 if true, 0 if not, and -1 in case of error
5097 */
5098int
5099xmlAutomataIsDeterminist(xmlAutomataPtr am) {
5100 int ret;
5101
5102 if (am == NULL)
5103 return(-1);
5104
5105 ret = xmlFAComputesDeterminism(am);
5106 return(ret);
5107}
Daniel Veillard4255d502002-04-16 15:50:10 +00005108#endif /* LIBXML_AUTOMATA_ENABLED */
5109#endif /* LIBXML_REGEXP_ENABLED */