blob: 0435d674f4650f78b2e191a1872f721c99dcdb51 [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
22#include <stdio.h>
23#include <string.h>
Daniel Veillardebe48c62003-12-03 12:12:27 +000024#ifdef HAVE_LIMITS_H
25#include <limits.h>
26#endif
27
Daniel Veillard4255d502002-04-16 15:50:10 +000028#include <libxml/tree.h>
29#include <libxml/parserInternals.h>
30#include <libxml/xmlregexp.h>
31#include <libxml/xmlautomata.h>
32#include <libxml/xmlunicode.h>
33
Daniel Veillardebe48c62003-12-03 12:12:27 +000034#ifndef INT_MAX
35#define INT_MAX 123456789 /* easy to flag and big enough for our needs */
36#endif
37
Daniel Veillardc0826a72004-08-10 14:17:33 +000038/* #define DEBUG_REGEXP_GRAPH */
39/* #define DEBUG_REGEXP_EXEC */
Daniel Veillard4255d502002-04-16 15:50:10 +000040/* #define DEBUG_PUSH */
Daniel Veillard23e73572002-09-19 19:56:43 +000041/* #define DEBUG_COMPACTION */
Daniel Veillard4255d502002-04-16 15:50:10 +000042
Daniel Veillardff46a042003-10-08 08:53:17 +000043#define ERROR(str) \
44 ctxt->error = XML_REGEXP_COMPILE_ERROR; \
45 xmlRegexpErrCompile(ctxt, str);
Daniel Veillard4255d502002-04-16 15:50:10 +000046#define NEXT ctxt->cur++
47#define CUR (*(ctxt->cur))
48#define NXT(index) (ctxt->cur[index])
49
50#define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
51#define NEXTL(l) ctxt->cur += l;
Daniel Veillardc0826a72004-08-10 14:17:33 +000052#define XML_REG_STRING_SEPARATOR '|'
Daniel Veillard4255d502002-04-16 15:50:10 +000053
Daniel Veillarde19fc232002-04-22 16:01:24 +000054/**
55 * TODO:
56 *
57 * macro to flag unimplemented blocks
58 */
59#define TODO \
60 xmlGenericError(xmlGenericErrorContext, \
61 "Unimplemented block at %s:%d\n", \
62 __FILE__, __LINE__);
63
Daniel Veillard4255d502002-04-16 15:50:10 +000064/************************************************************************
65 * *
66 * Datatypes and structures *
67 * *
68 ************************************************************************/
69
70typedef enum {
71 XML_REGEXP_EPSILON = 1,
72 XML_REGEXP_CHARVAL,
73 XML_REGEXP_RANGES,
74 XML_REGEXP_SUBREG,
75 XML_REGEXP_STRING,
76 XML_REGEXP_ANYCHAR, /* . */
77 XML_REGEXP_ANYSPACE, /* \s */
78 XML_REGEXP_NOTSPACE, /* \S */
79 XML_REGEXP_INITNAME, /* \l */
80 XML_REGEXP_NOTINITNAME, /* \l */
81 XML_REGEXP_NAMECHAR, /* \c */
82 XML_REGEXP_NOTNAMECHAR, /* \C */
83 XML_REGEXP_DECIMAL, /* \d */
84 XML_REGEXP_NOTDECIMAL, /* \d */
85 XML_REGEXP_REALCHAR, /* \w */
86 XML_REGEXP_NOTREALCHAR, /* \w */
87 XML_REGEXP_LETTER,
88 XML_REGEXP_LETTER_UPPERCASE,
89 XML_REGEXP_LETTER_LOWERCASE,
90 XML_REGEXP_LETTER_TITLECASE,
91 XML_REGEXP_LETTER_MODIFIER,
92 XML_REGEXP_LETTER_OTHERS,
93 XML_REGEXP_MARK,
94 XML_REGEXP_MARK_NONSPACING,
95 XML_REGEXP_MARK_SPACECOMBINING,
96 XML_REGEXP_MARK_ENCLOSING,
97 XML_REGEXP_NUMBER,
98 XML_REGEXP_NUMBER_DECIMAL,
99 XML_REGEXP_NUMBER_LETTER,
100 XML_REGEXP_NUMBER_OTHERS,
101 XML_REGEXP_PUNCT,
102 XML_REGEXP_PUNCT_CONNECTOR,
103 XML_REGEXP_PUNCT_DASH,
104 XML_REGEXP_PUNCT_OPEN,
105 XML_REGEXP_PUNCT_CLOSE,
106 XML_REGEXP_PUNCT_INITQUOTE,
107 XML_REGEXP_PUNCT_FINQUOTE,
108 XML_REGEXP_PUNCT_OTHERS,
109 XML_REGEXP_SEPAR,
110 XML_REGEXP_SEPAR_SPACE,
111 XML_REGEXP_SEPAR_LINE,
112 XML_REGEXP_SEPAR_PARA,
113 XML_REGEXP_SYMBOL,
114 XML_REGEXP_SYMBOL_MATH,
115 XML_REGEXP_SYMBOL_CURRENCY,
116 XML_REGEXP_SYMBOL_MODIFIER,
117 XML_REGEXP_SYMBOL_OTHERS,
118 XML_REGEXP_OTHER,
119 XML_REGEXP_OTHER_CONTROL,
120 XML_REGEXP_OTHER_FORMAT,
121 XML_REGEXP_OTHER_PRIVATE,
122 XML_REGEXP_OTHER_NA,
123 XML_REGEXP_BLOCK_NAME
124} xmlRegAtomType;
125
126typedef enum {
127 XML_REGEXP_QUANT_EPSILON = 1,
128 XML_REGEXP_QUANT_ONCE,
129 XML_REGEXP_QUANT_OPT,
130 XML_REGEXP_QUANT_MULT,
131 XML_REGEXP_QUANT_PLUS,
Daniel Veillard7646b182002-04-20 06:41:40 +0000132 XML_REGEXP_QUANT_ONCEONLY,
133 XML_REGEXP_QUANT_ALL,
Daniel Veillard4255d502002-04-16 15:50:10 +0000134 XML_REGEXP_QUANT_RANGE
135} xmlRegQuantType;
136
137typedef enum {
138 XML_REGEXP_START_STATE = 1,
139 XML_REGEXP_FINAL_STATE,
140 XML_REGEXP_TRANS_STATE
141} xmlRegStateType;
142
143typedef enum {
144 XML_REGEXP_MARK_NORMAL = 0,
145 XML_REGEXP_MARK_START,
146 XML_REGEXP_MARK_VISITED
147} xmlRegMarkedType;
148
149typedef struct _xmlRegRange xmlRegRange;
150typedef xmlRegRange *xmlRegRangePtr;
151
152struct _xmlRegRange {
Daniel Veillardf8b9de32003-11-24 14:27:26 +0000153 int neg; /* 0 normal, 1 not, 2 exclude */
Daniel Veillard4255d502002-04-16 15:50:10 +0000154 xmlRegAtomType type;
155 int start;
156 int end;
157 xmlChar *blockName;
158};
159
160typedef struct _xmlRegAtom xmlRegAtom;
161typedef xmlRegAtom *xmlRegAtomPtr;
162
163typedef struct _xmlAutomataState xmlRegState;
164typedef xmlRegState *xmlRegStatePtr;
165
166struct _xmlRegAtom {
167 int no;
168 xmlRegAtomType type;
169 xmlRegQuantType quant;
170 int min;
171 int max;
172
173 void *valuep;
Daniel Veillarda646cfd2002-09-17 21:50:03 +0000174 void *valuep2;
Daniel Veillard4255d502002-04-16 15:50:10 +0000175 int neg;
176 int codepoint;
177 xmlRegStatePtr start;
178 xmlRegStatePtr stop;
179 int maxRanges;
180 int nbRanges;
181 xmlRegRangePtr *ranges;
182 void *data;
183};
184
185typedef struct _xmlRegCounter xmlRegCounter;
186typedef xmlRegCounter *xmlRegCounterPtr;
187
188struct _xmlRegCounter {
189 int min;
190 int max;
191};
192
193typedef struct _xmlRegTrans xmlRegTrans;
194typedef xmlRegTrans *xmlRegTransPtr;
195
196struct _xmlRegTrans {
197 xmlRegAtomPtr atom;
198 int to;
199 int counter;
200 int count;
201};
202
203struct _xmlAutomataState {
204 xmlRegStateType type;
205 xmlRegMarkedType mark;
Daniel Veillard23e73572002-09-19 19:56:43 +0000206 xmlRegMarkedType reached;
Daniel Veillard4255d502002-04-16 15:50:10 +0000207 int no;
208
209 int maxTrans;
210 int nbTrans;
211 xmlRegTrans *trans;
212};
213
214typedef struct _xmlAutomata xmlRegParserCtxt;
215typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
216
217struct _xmlAutomata {
218 xmlChar *string;
219 xmlChar *cur;
220
221 int error;
222 int neg;
223
224 xmlRegStatePtr start;
225 xmlRegStatePtr end;
226 xmlRegStatePtr state;
227
228 xmlRegAtomPtr atom;
229
230 int maxAtoms;
231 int nbAtoms;
232 xmlRegAtomPtr *atoms;
233
234 int maxStates;
235 int nbStates;
236 xmlRegStatePtr *states;
237
238 int maxCounters;
239 int nbCounters;
240 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000241
242 int determinist;
Daniel Veillard4255d502002-04-16 15:50:10 +0000243};
244
245struct _xmlRegexp {
246 xmlChar *string;
247 int nbStates;
248 xmlRegStatePtr *states;
249 int nbAtoms;
250 xmlRegAtomPtr *atoms;
251 int nbCounters;
252 xmlRegCounter *counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000253 int determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000254 /*
255 * That's the compact form for determinists automatas
256 */
257 int nbstates;
258 int *compact;
Daniel Veillard118aed72002-09-24 14:13:13 +0000259 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000260 int nbstrings;
261 xmlChar **stringMap;
Daniel Veillard4255d502002-04-16 15:50:10 +0000262};
263
264typedef struct _xmlRegExecRollback xmlRegExecRollback;
265typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
266
267struct _xmlRegExecRollback {
268 xmlRegStatePtr state;/* the current state */
269 int index; /* the index in the input stack */
270 int nextbranch; /* the next transition to explore in that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000271 int *counts; /* save the automata state if it has some */
Daniel Veillard4255d502002-04-16 15:50:10 +0000272};
273
274typedef struct _xmlRegInputToken xmlRegInputToken;
275typedef xmlRegInputToken *xmlRegInputTokenPtr;
276
277struct _xmlRegInputToken {
278 xmlChar *value;
279 void *data;
280};
281
282struct _xmlRegExecCtxt {
283 int status; /* execution status != 0 indicate an error */
William M. Brackddf71d62004-05-06 04:17:26 +0000284 int determinist; /* did we find an indeterministic behaviour */
Daniel Veillard4255d502002-04-16 15:50:10 +0000285 xmlRegexpPtr comp; /* the compiled regexp */
286 xmlRegExecCallbacks callback;
287 void *data;
288
289 xmlRegStatePtr state;/* the current state */
290 int transno; /* the current transition on that state */
William M. Brackddf71d62004-05-06 04:17:26 +0000291 int transcount; /* the number of chars in char counted transitions */
Daniel Veillard4255d502002-04-16 15:50:10 +0000292
293 /*
294 * A stack of rollback states
295 */
296 int maxRollbacks;
297 int nbRollbacks;
298 xmlRegExecRollback *rollbacks;
299
300 /*
301 * The state of the automata if any
302 */
303 int *counts;
304
305 /*
306 * The input stack
307 */
308 int inputStackMax;
309 int inputStackNr;
310 int index;
311 int *charStack;
312 const xmlChar *inputString; /* when operating on characters */
313 xmlRegInputTokenPtr inputStack;/* when operating on strings */
314
315};
316
Daniel Veillard441bc322002-04-20 17:38:48 +0000317#define REGEXP_ALL_COUNTER 0x123456
318#define REGEXP_ALL_LAX_COUNTER 0x123457
Daniel Veillard7646b182002-04-20 06:41:40 +0000319
Daniel Veillard4255d502002-04-16 15:50:10 +0000320static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
Daniel Veillard23e73572002-09-19 19:56:43 +0000321static void xmlRegFreeState(xmlRegStatePtr state);
322static void xmlRegFreeAtom(xmlRegAtomPtr atom);
Daniel Veillard4255d502002-04-16 15:50:10 +0000323
324/************************************************************************
Daniel Veillardff46a042003-10-08 08:53:17 +0000325 * *
326 * Regexp memory error handler *
327 * *
328 ************************************************************************/
329/**
330 * xmlRegexpErrMemory:
William M. Brackddf71d62004-05-06 04:17:26 +0000331 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000332 *
333 * Handle an out of memory condition
334 */
335static void
336xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
337{
338 const char *regexp = NULL;
339 if (ctxt != NULL) {
340 regexp = (const char *) ctxt->string;
341 ctxt->error = XML_ERR_NO_MEMORY;
342 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000343 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000344 XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
345 regexp, NULL, 0, 0,
346 "Memory allocation failed : %s\n", extra);
347}
348
349/**
350 * xmlRegexpErrCompile:
William M. Brackddf71d62004-05-06 04:17:26 +0000351 * @extra: extra information
Daniel Veillardff46a042003-10-08 08:53:17 +0000352 *
William M. Brackddf71d62004-05-06 04:17:26 +0000353 * Handle a compilation failure
Daniel Veillardff46a042003-10-08 08:53:17 +0000354 */
355static void
356xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
357{
358 const char *regexp = NULL;
359 int idx = 0;
360
361 if (ctxt != NULL) {
362 regexp = (const char *) ctxt->string;
363 idx = ctxt->cur - ctxt->string;
364 ctxt->error = XML_REGEXP_COMPILE_ERROR;
365 }
Daniel Veillard659e71e2003-10-10 14:10:40 +0000366 __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
Daniel Veillardff46a042003-10-08 08:53:17 +0000367 XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
368 regexp, NULL, idx, 0,
369 "failed to compile: %s\n", extra);
370}
371
372/************************************************************************
Daniel Veillard4255d502002-04-16 15:50:10 +0000373 * *
374 * Allocation/Deallocation *
375 * *
376 ************************************************************************/
377
Daniel Veillard23e73572002-09-19 19:56:43 +0000378static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
Daniel Veillard4255d502002-04-16 15:50:10 +0000379/**
380 * xmlRegEpxFromParse:
381 * @ctxt: the parser context used to build it
382 *
William M. Brackddf71d62004-05-06 04:17:26 +0000383 * Allocate a new regexp and fill it with the result from the parser
Daniel Veillard4255d502002-04-16 15:50:10 +0000384 *
385 * Returns the new regexp or NULL in case of error
386 */
387static xmlRegexpPtr
388xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
389 xmlRegexpPtr ret;
390
391 ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000392 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000393 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +0000394 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000395 }
Daniel Veillard4255d502002-04-16 15:50:10 +0000396 memset(ret, 0, sizeof(xmlRegexp));
397 ret->string = ctxt->string;
Daniel Veillard4255d502002-04-16 15:50:10 +0000398 ret->nbStates = ctxt->nbStates;
Daniel Veillard4255d502002-04-16 15:50:10 +0000399 ret->states = ctxt->states;
Daniel Veillard4255d502002-04-16 15:50:10 +0000400 ret->nbAtoms = ctxt->nbAtoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000401 ret->atoms = ctxt->atoms;
Daniel Veillard4255d502002-04-16 15:50:10 +0000402 ret->nbCounters = ctxt->nbCounters;
Daniel Veillard4255d502002-04-16 15:50:10 +0000403 ret->counters = ctxt->counters;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000404 ret->determinist = ctxt->determinist;
Daniel Veillard23e73572002-09-19 19:56:43 +0000405
406 if ((ret->determinist != 0) &&
407 (ret->nbCounters == 0) &&
Daniel Veillard118aed72002-09-24 14:13:13 +0000408 (ret->atoms != NULL) &&
Daniel Veillard23e73572002-09-19 19:56:43 +0000409 (ret->atoms[0] != NULL) &&
410 (ret->atoms[0]->type == XML_REGEXP_STRING)) {
411 int i, j, nbstates = 0, nbatoms = 0;
412 int *stateRemap;
413 int *stringRemap;
414 int *transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000415 void **transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000416 xmlChar **stringMap;
417 xmlChar *value;
418
419 /*
420 * Switch to a compact representation
421 * 1/ counting the effective number of states left
William M. Brackddf71d62004-05-06 04:17:26 +0000422 * 2/ counting the unique number of atoms, and check that
Daniel Veillard23e73572002-09-19 19:56:43 +0000423 * they are all of the string type
424 * 3/ build a table state x atom for the transitions
425 */
426
427 stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000428 if (stateRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000429 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000430 xmlFree(ret);
431 return(NULL);
432 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000433 for (i = 0;i < ret->nbStates;i++) {
434 if (ret->states[i] != NULL) {
435 stateRemap[i] = nbstates;
436 nbstates++;
437 } else {
438 stateRemap[i] = -1;
439 }
440 }
441#ifdef DEBUG_COMPACTION
442 printf("Final: %d states\n", nbstates);
443#endif
444 stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000445 if (stringMap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000446 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000447 xmlFree(stateRemap);
448 xmlFree(ret);
449 return(NULL);
450 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000451 stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000452 if (stringRemap == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000453 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000454 xmlFree(stringMap);
455 xmlFree(stateRemap);
456 xmlFree(ret);
457 return(NULL);
458 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000459 for (i = 0;i < ret->nbAtoms;i++) {
460 if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
461 (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
462 value = ret->atoms[i]->valuep;
463 for (j = 0;j < nbatoms;j++) {
464 if (xmlStrEqual(stringMap[j], value)) {
465 stringRemap[i] = j;
466 break;
467 }
468 }
469 if (j >= nbatoms) {
470 stringRemap[i] = nbatoms;
471 stringMap[nbatoms] = xmlStrdup(value);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000472 if (stringMap[nbatoms] == NULL) {
473 for (i = 0;i < nbatoms;i++)
474 xmlFree(stringMap[i]);
475 xmlFree(stringRemap);
476 xmlFree(stringMap);
477 xmlFree(stateRemap);
478 xmlFree(ret);
479 return(NULL);
480 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000481 nbatoms++;
482 }
483 } else {
484 xmlFree(stateRemap);
485 xmlFree(stringRemap);
486 for (i = 0;i < nbatoms;i++)
487 xmlFree(stringMap[i]);
488 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000489 xmlFree(ret);
490 return(NULL);
Daniel Veillard23e73572002-09-19 19:56:43 +0000491 }
492 }
493#ifdef DEBUG_COMPACTION
494 printf("Final: %d atoms\n", nbatoms);
495#endif
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000496 transitions = (int *) xmlMalloc((nbstates + 1) *
497 (nbatoms + 1) * sizeof(int));
498 if (transitions == NULL) {
499 xmlFree(stateRemap);
500 xmlFree(stringRemap);
501 xmlFree(stringMap);
502 xmlFree(ret);
503 return(NULL);
504 }
505 memset(transitions, 0, (nbstates + 1) * (nbatoms + 1) * sizeof(int));
Daniel Veillard23e73572002-09-19 19:56:43 +0000506
507 /*
508 * Allocate the transition table. The first entry for each
William M. Brackddf71d62004-05-06 04:17:26 +0000509 * state corresponds to the state type.
Daniel Veillard23e73572002-09-19 19:56:43 +0000510 */
Daniel Veillard118aed72002-09-24 14:13:13 +0000511 transdata = NULL;
Daniel Veillard23e73572002-09-19 19:56:43 +0000512
513 for (i = 0;i < ret->nbStates;i++) {
514 int stateno, atomno, targetno, prev;
515 xmlRegStatePtr state;
516 xmlRegTransPtr trans;
517
518 stateno = stateRemap[i];
519 if (stateno == -1)
520 continue;
521 state = ret->states[i];
522
523 transitions[stateno * (nbatoms + 1)] = state->type;
524
525 for (j = 0;j < state->nbTrans;j++) {
526 trans = &(state->trans[j]);
527 if ((trans->to == -1) || (trans->atom == NULL))
528 continue;
529 atomno = stringRemap[trans->atom->no];
Daniel Veillard118aed72002-09-24 14:13:13 +0000530 if ((trans->atom->data != NULL) && (transdata == NULL)) {
531 transdata = (void **) xmlMalloc(nbstates * nbatoms *
532 sizeof(void *));
533 if (transdata != NULL)
534 memset(transdata, 0,
535 nbstates * nbatoms * sizeof(void *));
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000536 else {
Daniel Veillardff46a042003-10-08 08:53:17 +0000537 xmlRegexpErrMemory(ctxt, "compiling regexp");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000538 break;
539 }
Daniel Veillard118aed72002-09-24 14:13:13 +0000540 }
Daniel Veillard23e73572002-09-19 19:56:43 +0000541 targetno = stateRemap[trans->to];
542 /*
William M. Brackddf71d62004-05-06 04:17:26 +0000543 * if the same atom can generate transitions to 2 different
Daniel Veillard23e73572002-09-19 19:56:43 +0000544 * states then it means the automata is not determinist and
545 * the compact form can't be used !
546 */
547 prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
548 if (prev != 0) {
549 if (prev != targetno + 1) {
Daniel Veillard23e73572002-09-19 19:56:43 +0000550 ret->determinist = 0;
551#ifdef DEBUG_COMPACTION
552 printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
553 i, j, trans->atom->no, trans->to, atomno, targetno);
554 printf(" previous to is %d\n", prev);
555#endif
556 ret->determinist = 0;
Daniel Veillard118aed72002-09-24 14:13:13 +0000557 if (transdata != NULL)
558 xmlFree(transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +0000559 xmlFree(transitions);
560 xmlFree(stateRemap);
561 xmlFree(stringRemap);
562 for (i = 0;i < nbatoms;i++)
563 xmlFree(stringMap[i]);
564 xmlFree(stringMap);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000565 goto not_determ;
Daniel Veillard23e73572002-09-19 19:56:43 +0000566 }
567 } else {
568#if 0
569 printf("State %d trans %d: atom %d to %d : %d to %d\n",
570 i, j, trans->atom->no, trans->to, atomno, targetno);
571#endif
572 transitions[stateno * (nbatoms + 1) + atomno + 1] =
Daniel Veillard118aed72002-09-24 14:13:13 +0000573 targetno + 1; /* to avoid 0 */
574 if (transdata != NULL)
575 transdata[stateno * nbatoms + atomno] =
576 trans->atom->data;
Daniel Veillard23e73572002-09-19 19:56:43 +0000577 }
578 }
579 }
580 ret->determinist = 1;
581#ifdef DEBUG_COMPACTION
582 /*
583 * Debug
584 */
585 for (i = 0;i < nbstates;i++) {
586 for (j = 0;j < nbatoms + 1;j++) {
587 printf("%02d ", transitions[i * (nbatoms + 1) + j]);
588 }
589 printf("\n");
590 }
591 printf("\n");
592#endif
593 /*
594 * Cleanup of the old data
595 */
596 if (ret->states != NULL) {
597 for (i = 0;i < ret->nbStates;i++)
598 xmlRegFreeState(ret->states[i]);
599 xmlFree(ret->states);
600 }
601 ret->states = NULL;
602 ret->nbStates = 0;
603 if (ret->atoms != NULL) {
604 for (i = 0;i < ret->nbAtoms;i++)
605 xmlRegFreeAtom(ret->atoms[i]);
606 xmlFree(ret->atoms);
607 }
608 ret->atoms = NULL;
609 ret->nbAtoms = 0;
610
611 ret->compact = transitions;
Daniel Veillard118aed72002-09-24 14:13:13 +0000612 ret->transdata = transdata;
Daniel Veillard23e73572002-09-19 19:56:43 +0000613 ret->stringMap = stringMap;
614 ret->nbstrings = nbatoms;
615 ret->nbstates = nbstates;
616 xmlFree(stateRemap);
617 xmlFree(stringRemap);
618 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +0000619not_determ:
620 ctxt->string = NULL;
621 ctxt->nbStates = 0;
622 ctxt->states = NULL;
623 ctxt->nbAtoms = 0;
624 ctxt->atoms = NULL;
625 ctxt->nbCounters = 0;
626 ctxt->counters = NULL;
Daniel Veillard4255d502002-04-16 15:50:10 +0000627 return(ret);
628}
629
630/**
631 * xmlRegNewParserCtxt:
632 * @string: the string to parse
633 *
634 * Allocate a new regexp parser context
635 *
636 * Returns the new context or NULL in case of error
637 */
638static xmlRegParserCtxtPtr
639xmlRegNewParserCtxt(const xmlChar *string) {
640 xmlRegParserCtxtPtr ret;
641
642 ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
643 if (ret == NULL)
644 return(NULL);
645 memset(ret, 0, sizeof(xmlRegParserCtxt));
646 if (string != NULL)
647 ret->string = xmlStrdup(string);
648 ret->cur = ret->string;
649 ret->neg = 0;
650 ret->error = 0;
Daniel Veillarde19fc232002-04-22 16:01:24 +0000651 ret->determinist = -1;
Daniel Veillard4255d502002-04-16 15:50:10 +0000652 return(ret);
653}
654
655/**
656 * xmlRegNewRange:
657 * @ctxt: the regexp parser context
658 * @neg: is that negative
659 * @type: the type of range
660 * @start: the start codepoint
661 * @end: the end codepoint
662 *
663 * Allocate a new regexp range
664 *
665 * Returns the new range or NULL in case of error
666 */
667static xmlRegRangePtr
668xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
669 int neg, xmlRegAtomType type, int start, int end) {
670 xmlRegRangePtr ret;
671
672 ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
673 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000674 xmlRegexpErrMemory(ctxt, "allocating range");
Daniel Veillard4255d502002-04-16 15:50:10 +0000675 return(NULL);
676 }
677 ret->neg = neg;
678 ret->type = type;
679 ret->start = start;
680 ret->end = end;
681 return(ret);
682}
683
684/**
685 * xmlRegFreeRange:
686 * @range: the regexp range
687 *
688 * Free a regexp range
689 */
690static void
691xmlRegFreeRange(xmlRegRangePtr range) {
692 if (range == NULL)
693 return;
694
695 if (range->blockName != NULL)
696 xmlFree(range->blockName);
697 xmlFree(range);
698}
699
700/**
701 * xmlRegNewAtom:
702 * @ctxt: the regexp parser context
703 * @type: the type of atom
704 *
705 * Allocate a new regexp range
706 *
707 * Returns the new atom or NULL in case of error
708 */
709static xmlRegAtomPtr
710xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
711 xmlRegAtomPtr ret;
712
713 ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
714 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000715 xmlRegexpErrMemory(ctxt, "allocating atom");
Daniel Veillard4255d502002-04-16 15:50:10 +0000716 return(NULL);
717 }
718 memset(ret, 0, sizeof(xmlRegAtom));
719 ret->type = type;
720 ret->quant = XML_REGEXP_QUANT_ONCE;
721 ret->min = 0;
722 ret->max = 0;
723 return(ret);
724}
725
726/**
727 * xmlRegFreeAtom:
728 * @atom: the regexp atom
729 *
730 * Free a regexp atom
731 */
732static void
733xmlRegFreeAtom(xmlRegAtomPtr atom) {
734 int i;
735
736 if (atom == NULL)
737 return;
738
739 for (i = 0;i < atom->nbRanges;i++)
740 xmlRegFreeRange(atom->ranges[i]);
741 if (atom->ranges != NULL)
742 xmlFree(atom->ranges);
743 if (atom->type == XML_REGEXP_STRING)
744 xmlFree(atom->valuep);
745 xmlFree(atom);
746}
747
748static xmlRegStatePtr
749xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
750 xmlRegStatePtr ret;
751
752 ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
753 if (ret == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +0000754 xmlRegexpErrMemory(ctxt, "allocating state");
Daniel Veillard4255d502002-04-16 15:50:10 +0000755 return(NULL);
756 }
757 memset(ret, 0, sizeof(xmlRegState));
758 ret->type = XML_REGEXP_TRANS_STATE;
759 ret->mark = XML_REGEXP_MARK_NORMAL;
760 return(ret);
761}
762
763/**
764 * xmlRegFreeState:
765 * @state: the regexp state
766 *
767 * Free a regexp state
768 */
769static void
770xmlRegFreeState(xmlRegStatePtr state) {
771 if (state == NULL)
772 return;
773
774 if (state->trans != NULL)
775 xmlFree(state->trans);
776 xmlFree(state);
777}
778
779/**
780 * xmlRegFreeParserCtxt:
781 * @ctxt: the regexp parser context
782 *
783 * Free a regexp parser context
784 */
785static void
786xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
787 int i;
788 if (ctxt == NULL)
789 return;
790
791 if (ctxt->string != NULL)
792 xmlFree(ctxt->string);
793 if (ctxt->states != NULL) {
794 for (i = 0;i < ctxt->nbStates;i++)
795 xmlRegFreeState(ctxt->states[i]);
796 xmlFree(ctxt->states);
797 }
798 if (ctxt->atoms != NULL) {
799 for (i = 0;i < ctxt->nbAtoms;i++)
800 xmlRegFreeAtom(ctxt->atoms[i]);
801 xmlFree(ctxt->atoms);
802 }
803 if (ctxt->counters != NULL)
804 xmlFree(ctxt->counters);
805 xmlFree(ctxt);
806}
807
808/************************************************************************
809 * *
810 * Display of Data structures *
811 * *
812 ************************************************************************/
813
814static void
815xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
816 switch (type) {
817 case XML_REGEXP_EPSILON:
818 fprintf(output, "epsilon "); break;
819 case XML_REGEXP_CHARVAL:
820 fprintf(output, "charval "); break;
821 case XML_REGEXP_RANGES:
822 fprintf(output, "ranges "); break;
823 case XML_REGEXP_SUBREG:
824 fprintf(output, "subexpr "); break;
825 case XML_REGEXP_STRING:
826 fprintf(output, "string "); break;
827 case XML_REGEXP_ANYCHAR:
828 fprintf(output, "anychar "); break;
829 case XML_REGEXP_ANYSPACE:
830 fprintf(output, "anyspace "); break;
831 case XML_REGEXP_NOTSPACE:
832 fprintf(output, "notspace "); break;
833 case XML_REGEXP_INITNAME:
834 fprintf(output, "initname "); break;
835 case XML_REGEXP_NOTINITNAME:
836 fprintf(output, "notinitname "); break;
837 case XML_REGEXP_NAMECHAR:
838 fprintf(output, "namechar "); break;
839 case XML_REGEXP_NOTNAMECHAR:
840 fprintf(output, "notnamechar "); break;
841 case XML_REGEXP_DECIMAL:
842 fprintf(output, "decimal "); break;
843 case XML_REGEXP_NOTDECIMAL:
844 fprintf(output, "notdecimal "); break;
845 case XML_REGEXP_REALCHAR:
846 fprintf(output, "realchar "); break;
847 case XML_REGEXP_NOTREALCHAR:
848 fprintf(output, "notrealchar "); break;
849 case XML_REGEXP_LETTER:
850 fprintf(output, "LETTER "); break;
851 case XML_REGEXP_LETTER_UPPERCASE:
852 fprintf(output, "LETTER_UPPERCASE "); break;
853 case XML_REGEXP_LETTER_LOWERCASE:
854 fprintf(output, "LETTER_LOWERCASE "); break;
855 case XML_REGEXP_LETTER_TITLECASE:
856 fprintf(output, "LETTER_TITLECASE "); break;
857 case XML_REGEXP_LETTER_MODIFIER:
858 fprintf(output, "LETTER_MODIFIER "); break;
859 case XML_REGEXP_LETTER_OTHERS:
860 fprintf(output, "LETTER_OTHERS "); break;
861 case XML_REGEXP_MARK:
862 fprintf(output, "MARK "); break;
863 case XML_REGEXP_MARK_NONSPACING:
864 fprintf(output, "MARK_NONSPACING "); break;
865 case XML_REGEXP_MARK_SPACECOMBINING:
866 fprintf(output, "MARK_SPACECOMBINING "); break;
867 case XML_REGEXP_MARK_ENCLOSING:
868 fprintf(output, "MARK_ENCLOSING "); break;
869 case XML_REGEXP_NUMBER:
870 fprintf(output, "NUMBER "); break;
871 case XML_REGEXP_NUMBER_DECIMAL:
872 fprintf(output, "NUMBER_DECIMAL "); break;
873 case XML_REGEXP_NUMBER_LETTER:
874 fprintf(output, "NUMBER_LETTER "); break;
875 case XML_REGEXP_NUMBER_OTHERS:
876 fprintf(output, "NUMBER_OTHERS "); break;
877 case XML_REGEXP_PUNCT:
878 fprintf(output, "PUNCT "); break;
879 case XML_REGEXP_PUNCT_CONNECTOR:
880 fprintf(output, "PUNCT_CONNECTOR "); break;
881 case XML_REGEXP_PUNCT_DASH:
882 fprintf(output, "PUNCT_DASH "); break;
883 case XML_REGEXP_PUNCT_OPEN:
884 fprintf(output, "PUNCT_OPEN "); break;
885 case XML_REGEXP_PUNCT_CLOSE:
886 fprintf(output, "PUNCT_CLOSE "); break;
887 case XML_REGEXP_PUNCT_INITQUOTE:
888 fprintf(output, "PUNCT_INITQUOTE "); break;
889 case XML_REGEXP_PUNCT_FINQUOTE:
890 fprintf(output, "PUNCT_FINQUOTE "); break;
891 case XML_REGEXP_PUNCT_OTHERS:
892 fprintf(output, "PUNCT_OTHERS "); break;
893 case XML_REGEXP_SEPAR:
894 fprintf(output, "SEPAR "); break;
895 case XML_REGEXP_SEPAR_SPACE:
896 fprintf(output, "SEPAR_SPACE "); break;
897 case XML_REGEXP_SEPAR_LINE:
898 fprintf(output, "SEPAR_LINE "); break;
899 case XML_REGEXP_SEPAR_PARA:
900 fprintf(output, "SEPAR_PARA "); break;
901 case XML_REGEXP_SYMBOL:
902 fprintf(output, "SYMBOL "); break;
903 case XML_REGEXP_SYMBOL_MATH:
904 fprintf(output, "SYMBOL_MATH "); break;
905 case XML_REGEXP_SYMBOL_CURRENCY:
906 fprintf(output, "SYMBOL_CURRENCY "); break;
907 case XML_REGEXP_SYMBOL_MODIFIER:
908 fprintf(output, "SYMBOL_MODIFIER "); break;
909 case XML_REGEXP_SYMBOL_OTHERS:
910 fprintf(output, "SYMBOL_OTHERS "); break;
911 case XML_REGEXP_OTHER:
912 fprintf(output, "OTHER "); break;
913 case XML_REGEXP_OTHER_CONTROL:
914 fprintf(output, "OTHER_CONTROL "); break;
915 case XML_REGEXP_OTHER_FORMAT:
916 fprintf(output, "OTHER_FORMAT "); break;
917 case XML_REGEXP_OTHER_PRIVATE:
918 fprintf(output, "OTHER_PRIVATE "); break;
919 case XML_REGEXP_OTHER_NA:
920 fprintf(output, "OTHER_NA "); break;
921 case XML_REGEXP_BLOCK_NAME:
922 fprintf(output, "BLOCK "); break;
923 }
924}
925
926static void
927xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
928 switch (type) {
929 case XML_REGEXP_QUANT_EPSILON:
930 fprintf(output, "epsilon "); break;
931 case XML_REGEXP_QUANT_ONCE:
932 fprintf(output, "once "); break;
933 case XML_REGEXP_QUANT_OPT:
934 fprintf(output, "? "); break;
935 case XML_REGEXP_QUANT_MULT:
936 fprintf(output, "* "); break;
937 case XML_REGEXP_QUANT_PLUS:
938 fprintf(output, "+ "); break;
939 case XML_REGEXP_QUANT_RANGE:
940 fprintf(output, "range "); break;
Daniel Veillard7646b182002-04-20 06:41:40 +0000941 case XML_REGEXP_QUANT_ONCEONLY:
942 fprintf(output, "onceonly "); break;
943 case XML_REGEXP_QUANT_ALL:
944 fprintf(output, "all "); break;
Daniel Veillard4255d502002-04-16 15:50:10 +0000945 }
946}
947static void
948xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
949 fprintf(output, " range: ");
950 if (range->neg)
951 fprintf(output, "negative ");
952 xmlRegPrintAtomType(output, range->type);
953 fprintf(output, "%c - %c\n", range->start, range->end);
954}
955
956static void
957xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
958 fprintf(output, " atom: ");
959 if (atom == NULL) {
960 fprintf(output, "NULL\n");
961 return;
962 }
963 xmlRegPrintAtomType(output, atom->type);
964 xmlRegPrintQuantType(output, atom->quant);
965 if (atom->quant == XML_REGEXP_QUANT_RANGE)
966 fprintf(output, "%d-%d ", atom->min, atom->max);
967 if (atom->type == XML_REGEXP_STRING)
968 fprintf(output, "'%s' ", (char *) atom->valuep);
969 if (atom->type == XML_REGEXP_CHARVAL)
970 fprintf(output, "char %c\n", atom->codepoint);
971 else if (atom->type == XML_REGEXP_RANGES) {
972 int i;
973 fprintf(output, "%d entries\n", atom->nbRanges);
974 for (i = 0; i < atom->nbRanges;i++)
975 xmlRegPrintRange(output, atom->ranges[i]);
976 } else if (atom->type == XML_REGEXP_SUBREG) {
977 fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
978 } else {
979 fprintf(output, "\n");
980 }
981}
982
983static void
984xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
985 fprintf(output, " trans: ");
986 if (trans == NULL) {
987 fprintf(output, "NULL\n");
988 return;
989 }
990 if (trans->to < 0) {
991 fprintf(output, "removed\n");
992 return;
993 }
994 if (trans->counter >= 0) {
995 fprintf(output, "counted %d, ", trans->counter);
996 }
Daniel Veillard8a001f62002-04-20 07:24:11 +0000997 if (trans->count == REGEXP_ALL_COUNTER) {
998 fprintf(output, "all transition, ");
999 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00001000 fprintf(output, "count based %d, ", trans->count);
1001 }
1002 if (trans->atom == NULL) {
1003 fprintf(output, "epsilon to %d\n", trans->to);
1004 return;
1005 }
1006 if (trans->atom->type == XML_REGEXP_CHARVAL)
1007 fprintf(output, "char %c ", trans->atom->codepoint);
1008 fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
1009}
1010
1011static void
1012xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
1013 int i;
1014
1015 fprintf(output, " state: ");
1016 if (state == NULL) {
1017 fprintf(output, "NULL\n");
1018 return;
1019 }
1020 if (state->type == XML_REGEXP_START_STATE)
1021 fprintf(output, "START ");
1022 if (state->type == XML_REGEXP_FINAL_STATE)
1023 fprintf(output, "FINAL ");
1024
1025 fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
1026 for (i = 0;i < state->nbTrans; i++) {
1027 xmlRegPrintTrans(output, &(state->trans[i]));
1028 }
1029}
1030
Daniel Veillard23e73572002-09-19 19:56:43 +00001031#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillard4255d502002-04-16 15:50:10 +00001032static void
1033xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
1034 int i;
1035
1036 fprintf(output, " ctxt: ");
1037 if (ctxt == NULL) {
1038 fprintf(output, "NULL\n");
1039 return;
1040 }
1041 fprintf(output, "'%s' ", ctxt->string);
1042 if (ctxt->error)
1043 fprintf(output, "error ");
1044 if (ctxt->neg)
1045 fprintf(output, "neg ");
1046 fprintf(output, "\n");
1047 fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
1048 for (i = 0;i < ctxt->nbAtoms; i++) {
1049 fprintf(output, " %02d ", i);
1050 xmlRegPrintAtom(output, ctxt->atoms[i]);
1051 }
1052 if (ctxt->atom != NULL) {
1053 fprintf(output, "current atom:\n");
1054 xmlRegPrintAtom(output, ctxt->atom);
1055 }
1056 fprintf(output, "%d states:", ctxt->nbStates);
1057 if (ctxt->start != NULL)
1058 fprintf(output, " start: %d", ctxt->start->no);
1059 if (ctxt->end != NULL)
1060 fprintf(output, " end: %d", ctxt->end->no);
1061 fprintf(output, "\n");
1062 for (i = 0;i < ctxt->nbStates; i++) {
1063 xmlRegPrintState(output, ctxt->states[i]);
1064 }
1065 fprintf(output, "%d counters:\n", ctxt->nbCounters);
1066 for (i = 0;i < ctxt->nbCounters; i++) {
1067 fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
1068 ctxt->counters[i].max);
1069 }
1070}
Daniel Veillard23e73572002-09-19 19:56:43 +00001071#endif
Daniel Veillard4255d502002-04-16 15:50:10 +00001072
1073/************************************************************************
1074 * *
1075 * Finite Automata structures manipulations *
1076 * *
1077 ************************************************************************/
1078
1079static void
1080xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
1081 int neg, xmlRegAtomType type, int start, int end,
1082 xmlChar *blockName) {
1083 xmlRegRangePtr range;
1084
1085 if (atom == NULL) {
1086 ERROR("add range: atom is NULL");
1087 return;
1088 }
1089 if (atom->type != XML_REGEXP_RANGES) {
1090 ERROR("add range: atom is not ranges");
1091 return;
1092 }
1093 if (atom->maxRanges == 0) {
1094 atom->maxRanges = 4;
1095 atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
1096 sizeof(xmlRegRangePtr));
1097 if (atom->ranges == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001098 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001099 atom->maxRanges = 0;
1100 return;
1101 }
1102 } else if (atom->nbRanges >= atom->maxRanges) {
1103 xmlRegRangePtr *tmp;
1104 atom->maxRanges *= 2;
1105 tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
1106 sizeof(xmlRegRangePtr));
1107 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001108 xmlRegexpErrMemory(ctxt, "adding ranges");
Daniel Veillard4255d502002-04-16 15:50:10 +00001109 atom->maxRanges /= 2;
1110 return;
1111 }
1112 atom->ranges = tmp;
1113 }
1114 range = xmlRegNewRange(ctxt, neg, type, start, end);
1115 if (range == NULL)
1116 return;
1117 range->blockName = blockName;
1118 atom->ranges[atom->nbRanges++] = range;
1119
1120}
1121
1122static int
1123xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
1124 if (ctxt->maxCounters == 0) {
1125 ctxt->maxCounters = 4;
1126 ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
1127 sizeof(xmlRegCounter));
1128 if (ctxt->counters == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001129 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001130 ctxt->maxCounters = 0;
1131 return(-1);
1132 }
1133 } else if (ctxt->nbCounters >= ctxt->maxCounters) {
1134 xmlRegCounter *tmp;
1135 ctxt->maxCounters *= 2;
1136 tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
1137 sizeof(xmlRegCounter));
1138 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001139 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001140 ctxt->maxCounters /= 2;
1141 return(-1);
1142 }
1143 ctxt->counters = tmp;
1144 }
1145 ctxt->counters[ctxt->nbCounters].min = -1;
1146 ctxt->counters[ctxt->nbCounters].max = -1;
1147 return(ctxt->nbCounters++);
1148}
1149
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001150static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001151xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
1152 if (atom == NULL) {
1153 ERROR("atom push: atom is NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001154 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001155 }
1156 if (ctxt->maxAtoms == 0) {
1157 ctxt->maxAtoms = 4;
1158 ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
1159 sizeof(xmlRegAtomPtr));
1160 if (ctxt->atoms == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001161 xmlRegexpErrMemory(ctxt, "pushing atom");
Daniel Veillard4255d502002-04-16 15:50:10 +00001162 ctxt->maxAtoms = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001163 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001164 }
1165 } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
1166 xmlRegAtomPtr *tmp;
1167 ctxt->maxAtoms *= 2;
1168 tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
1169 sizeof(xmlRegAtomPtr));
1170 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001171 xmlRegexpErrMemory(ctxt, "allocating counter");
Daniel Veillard4255d502002-04-16 15:50:10 +00001172 ctxt->maxAtoms /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001173 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001174 }
1175 ctxt->atoms = tmp;
1176 }
1177 atom->no = ctxt->nbAtoms;
1178 ctxt->atoms[ctxt->nbAtoms++] = atom;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001179 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001180}
1181
1182static void
1183xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1184 xmlRegAtomPtr atom, xmlRegStatePtr target,
1185 int counter, int count) {
William M. Brackf9b5fa22004-05-10 07:52:15 +00001186
1187 int nrtrans;
1188
Daniel Veillard4255d502002-04-16 15:50:10 +00001189 if (state == NULL) {
1190 ERROR("add state: state is NULL");
1191 return;
1192 }
1193 if (target == NULL) {
1194 ERROR("add state: target is NULL");
1195 return;
1196 }
William M. Brackf9b5fa22004-05-10 07:52:15 +00001197 /*
1198 * Other routines follow the philosophy 'When in doubt, add a transition'
1199 * so we check here whether such a transition is already present and, if
1200 * so, silently ignore this request.
1201 */
1202
1203 for (nrtrans=0; nrtrans<state->nbTrans; nrtrans++) {
1204 if ((state->trans[nrtrans].atom == atom) &&
1205 (state->trans[nrtrans].to == target->no) &&
1206 (state->trans[nrtrans].counter == counter) &&
1207 (state->trans[nrtrans].count == count)) {
1208#ifdef DEBUG_REGEXP_GRAPH
1209 printf("Ignoring duplicate transition from %d to %d\n",
1210 state->no, target->no);
1211#endif
1212 return;
1213 }
1214 }
1215
Daniel Veillard4255d502002-04-16 15:50:10 +00001216 if (state->maxTrans == 0) {
1217 state->maxTrans = 4;
1218 state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
1219 sizeof(xmlRegTrans));
1220 if (state->trans == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001221 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001222 state->maxTrans = 0;
1223 return;
1224 }
1225 } else if (state->nbTrans >= state->maxTrans) {
1226 xmlRegTrans *tmp;
1227 state->maxTrans *= 2;
1228 tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
1229 sizeof(xmlRegTrans));
1230 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001231 xmlRegexpErrMemory(ctxt, "adding transition");
Daniel Veillard4255d502002-04-16 15:50:10 +00001232 state->maxTrans /= 2;
1233 return;
1234 }
1235 state->trans = tmp;
1236 }
1237#ifdef DEBUG_REGEXP_GRAPH
1238 printf("Add trans from %d to %d ", state->no, target->no);
Daniel Veillard8a001f62002-04-20 07:24:11 +00001239 if (count == REGEXP_ALL_COUNTER)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001240 printf("all transition\n");
Daniel Veillard4402ab42002-09-12 16:02:56 +00001241 else if (count >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001242 printf("count based %d\n", count);
Daniel Veillard4255d502002-04-16 15:50:10 +00001243 else if (counter >= 0)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001244 printf("counted %d\n", counter);
Daniel Veillard4255d502002-04-16 15:50:10 +00001245 else if (atom == NULL)
Daniel Veillard2cbf5962004-03-31 15:50:43 +00001246 printf("epsilon transition\n");
1247 else if (atom != NULL)
1248 xmlRegPrintAtom(stdout, atom);
Daniel Veillard4255d502002-04-16 15:50:10 +00001249#endif
1250
1251 state->trans[state->nbTrans].atom = atom;
1252 state->trans[state->nbTrans].to = target->no;
1253 state->trans[state->nbTrans].counter = counter;
1254 state->trans[state->nbTrans].count = count;
1255 state->nbTrans++;
1256}
1257
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001258static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001259xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001260 if (state == NULL) return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001261 if (ctxt->maxStates == 0) {
1262 ctxt->maxStates = 4;
1263 ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
1264 sizeof(xmlRegStatePtr));
1265 if (ctxt->states == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001266 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001267 ctxt->maxStates = 0;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001268 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001269 }
1270 } else if (ctxt->nbStates >= ctxt->maxStates) {
1271 xmlRegStatePtr *tmp;
1272 ctxt->maxStates *= 2;
1273 tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
1274 sizeof(xmlRegStatePtr));
1275 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00001276 xmlRegexpErrMemory(ctxt, "adding state");
Daniel Veillard4255d502002-04-16 15:50:10 +00001277 ctxt->maxStates /= 2;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001278 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001279 }
1280 ctxt->states = tmp;
1281 }
1282 state->no = ctxt->nbStates;
1283 ctxt->states[ctxt->nbStates++] = state;
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001284 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001285}
1286
1287/**
Daniel Veillard7646b182002-04-20 06:41:40 +00001288 * xmlFAGenerateAllTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001289 * @ctxt: a regexp parser context
1290 * @from: the from state
1291 * @to: the target state or NULL for building a new one
1292 * @lax:
Daniel Veillard7646b182002-04-20 06:41:40 +00001293 *
1294 */
1295static void
1296xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
Daniel Veillard441bc322002-04-20 17:38:48 +00001297 xmlRegStatePtr from, xmlRegStatePtr to,
1298 int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00001299 if (to == NULL) {
1300 to = xmlRegNewState(ctxt);
1301 xmlRegStatePush(ctxt, to);
1302 ctxt->state = to;
1303 }
Daniel Veillard441bc322002-04-20 17:38:48 +00001304 if (lax)
1305 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
1306 else
1307 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
Daniel Veillard7646b182002-04-20 06:41:40 +00001308}
1309
1310/**
Daniel Veillard4255d502002-04-16 15:50:10 +00001311 * xmlFAGenerateEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001312 * @ctxt: a regexp parser context
1313 * @from: the from state
1314 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001315 *
1316 */
1317static void
1318xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1319 xmlRegStatePtr from, xmlRegStatePtr to) {
1320 if (to == NULL) {
1321 to = xmlRegNewState(ctxt);
1322 xmlRegStatePush(ctxt, to);
1323 ctxt->state = to;
1324 }
1325 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
1326}
1327
1328/**
1329 * xmlFAGenerateCountedEpsilonTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001330 * @ctxt: a regexp parser context
1331 * @from: the from state
1332 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001333 * counter: the counter for that transition
1334 *
1335 */
1336static void
1337xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
1338 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1339 if (to == NULL) {
1340 to = xmlRegNewState(ctxt);
1341 xmlRegStatePush(ctxt, to);
1342 ctxt->state = to;
1343 }
1344 xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
1345}
1346
1347/**
1348 * xmlFAGenerateCountedTransition:
Daniel Veillard441bc322002-04-20 17:38:48 +00001349 * @ctxt: a regexp parser context
1350 * @from: the from state
1351 * @to: the target state or NULL for building a new one
Daniel Veillard4255d502002-04-16 15:50:10 +00001352 * counter: the counter for that transition
1353 *
1354 */
1355static void
1356xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
1357 xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
1358 if (to == NULL) {
1359 to = xmlRegNewState(ctxt);
1360 xmlRegStatePush(ctxt, to);
1361 ctxt->state = to;
1362 }
1363 xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
1364}
1365
1366/**
1367 * xmlFAGenerateTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001368 * @ctxt: a regexp parser context
1369 * @from: the from state
1370 * @to: the target state or NULL for building a new one
1371 * @atom: the atom generating the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00001372 *
William M. Brackddf71d62004-05-06 04:17:26 +00001373 * Returns 0 if success and -1 in case of error.
Daniel Veillard4255d502002-04-16 15:50:10 +00001374 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001375static int
Daniel Veillard4255d502002-04-16 15:50:10 +00001376xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
1377 xmlRegStatePtr to, xmlRegAtomPtr atom) {
1378 if (atom == NULL) {
1379 ERROR("genrate transition: atom == NULL");
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001380 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001381 }
1382 if (atom->type == XML_REGEXP_SUBREG) {
1383 /*
1384 * this is a subexpression handling one should not need to
William M. Brackddf71d62004-05-06 04:17:26 +00001385 * create a new node except for XML_REGEXP_QUANT_RANGE.
Daniel Veillard4255d502002-04-16 15:50:10 +00001386 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001387 if (xmlRegAtomPush(ctxt, atom) < 0) {
1388 return(-1);
1389 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001390 if ((to != NULL) && (atom->stop != to) &&
1391 (atom->quant != XML_REGEXP_QUANT_RANGE)) {
1392 /*
1393 * Generate an epsilon transition to link to the target
1394 */
1395 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
1396 }
1397 switch (atom->quant) {
1398 case XML_REGEXP_QUANT_OPT:
1399 atom->quant = XML_REGEXP_QUANT_ONCE;
1400 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1401 break;
1402 case XML_REGEXP_QUANT_MULT:
1403 atom->quant = XML_REGEXP_QUANT_ONCE;
1404 xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
1405 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1406 break;
1407 case XML_REGEXP_QUANT_PLUS:
1408 atom->quant = XML_REGEXP_QUANT_ONCE;
1409 xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
1410 break;
1411 case XML_REGEXP_QUANT_RANGE: {
1412 int counter;
1413 xmlRegStatePtr newstate;
1414
1415 /*
1416 * This one is nasty:
William M. Brackddf71d62004-05-06 04:17:26 +00001417 * 1/ if range has minOccurs == 0, create a new state
1418 * and create epsilon transitions from atom->start
1419 * to atom->stop, as well as atom->start to the new
1420 * state
1421 * 2/ register a new counter
1422 * 3/ register an epsilon transition associated to
Daniel Veillard4255d502002-04-16 15:50:10 +00001423 * this counter going from atom->stop to atom->start
William M. Brackddf71d62004-05-06 04:17:26 +00001424 * 4/ create a new state
1425 * 5/ generate a counted transition from atom->stop to
Daniel Veillard4255d502002-04-16 15:50:10 +00001426 * that state
1427 */
William M. Brackddf71d62004-05-06 04:17:26 +00001428 if (atom->min == 0) {
1429 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1430 atom->stop);
1431 newstate = xmlRegNewState(ctxt);
1432 xmlRegStatePush(ctxt, newstate);
1433 ctxt->state = newstate;
1434 xmlFAGenerateEpsilonTransition(ctxt, atom->start,
1435 newstate);
1436 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001437 counter = xmlRegGetCounter(ctxt);
1438 ctxt->counters[counter].min = atom->min - 1;
1439 ctxt->counters[counter].max = atom->max - 1;
1440 atom->min = 0;
1441 atom->max = 0;
1442 atom->quant = XML_REGEXP_QUANT_ONCE;
1443 xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
1444 atom->start, counter);
1445 if (to != NULL) {
1446 newstate = to;
1447 } else {
1448 newstate = xmlRegNewState(ctxt);
1449 xmlRegStatePush(ctxt, newstate);
1450 ctxt->state = newstate;
1451 }
1452 xmlFAGenerateCountedTransition(ctxt, atom->stop,
1453 newstate, counter);
1454 }
1455 default:
1456 break;
1457 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001458 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001459 } else {
1460 if (to == NULL) {
1461 to = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001462 if (to != NULL)
1463 xmlRegStatePush(ctxt, to);
1464 else {
1465 return(-1);
1466 }
1467 }
1468 if (xmlRegAtomPush(ctxt, atom) < 0) {
1469 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001470 }
1471 xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
Daniel Veillard4255d502002-04-16 15:50:10 +00001472 ctxt->state = to;
1473 }
1474 switch (atom->quant) {
1475 case XML_REGEXP_QUANT_OPT:
1476 atom->quant = XML_REGEXP_QUANT_ONCE;
1477 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1478 break;
1479 case XML_REGEXP_QUANT_MULT:
1480 atom->quant = XML_REGEXP_QUANT_ONCE;
1481 xmlFAGenerateEpsilonTransition(ctxt, from, to);
1482 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1483 break;
1484 case XML_REGEXP_QUANT_PLUS:
1485 atom->quant = XML_REGEXP_QUANT_ONCE;
1486 xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
1487 break;
1488 default:
1489 break;
1490 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001491 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00001492}
1493
1494/**
1495 * xmlFAReduceEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001496 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001497 * @fromnr: the from state
1498 * @tonr: the to state
William M. Brackddf71d62004-05-06 04:17:26 +00001499 * @counter: should that transition be associated to a counted
Daniel Veillard4255d502002-04-16 15:50:10 +00001500 *
1501 */
1502static void
1503xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
1504 int tonr, int counter) {
1505 int transnr;
1506 xmlRegStatePtr from;
1507 xmlRegStatePtr to;
1508
1509#ifdef DEBUG_REGEXP_GRAPH
1510 printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
1511#endif
1512 from = ctxt->states[fromnr];
1513 if (from == NULL)
1514 return;
1515 to = ctxt->states[tonr];
1516 if (to == NULL)
1517 return;
1518 if ((to->mark == XML_REGEXP_MARK_START) ||
1519 (to->mark == XML_REGEXP_MARK_VISITED))
1520 return;
1521
1522 to->mark = XML_REGEXP_MARK_VISITED;
1523 if (to->type == XML_REGEXP_FINAL_STATE) {
1524#ifdef DEBUG_REGEXP_GRAPH
1525 printf("State %d is final, so %d becomes final\n", tonr, fromnr);
1526#endif
1527 from->type = XML_REGEXP_FINAL_STATE;
1528 }
1529 for (transnr = 0;transnr < to->nbTrans;transnr++) {
1530 if (to->trans[transnr].atom == NULL) {
1531 /*
1532 * Don't remove counted transitions
1533 * Don't loop either
1534 */
Daniel Veillardb509f152002-04-17 16:28:10 +00001535 if (to->trans[transnr].to != fromnr) {
1536 if (to->trans[transnr].count >= 0) {
1537 int newto = to->trans[transnr].to;
1538
1539 xmlRegStateAddTrans(ctxt, from, NULL,
1540 ctxt->states[newto],
1541 -1, to->trans[transnr].count);
1542 } else {
Daniel Veillard4255d502002-04-16 15:50:10 +00001543#ifdef DEBUG_REGEXP_GRAPH
Daniel Veillardb509f152002-04-17 16:28:10 +00001544 printf("Found epsilon trans %d from %d to %d\n",
1545 transnr, tonr, to->trans[transnr].to);
Daniel Veillard4255d502002-04-16 15:50:10 +00001546#endif
Daniel Veillardb509f152002-04-17 16:28:10 +00001547 if (to->trans[transnr].counter >= 0) {
1548 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1549 to->trans[transnr].to,
1550 to->trans[transnr].counter);
1551 } else {
1552 xmlFAReduceEpsilonTransitions(ctxt, fromnr,
1553 to->trans[transnr].to,
1554 counter);
1555 }
1556 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001557 }
1558 } else {
1559 int newto = to->trans[transnr].to;
1560
Daniel Veillardb509f152002-04-17 16:28:10 +00001561 if (to->trans[transnr].counter >= 0) {
1562 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1563 ctxt->states[newto],
1564 to->trans[transnr].counter, -1);
1565 } else {
1566 xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
1567 ctxt->states[newto], counter, -1);
1568 }
Daniel Veillard4255d502002-04-16 15:50:10 +00001569 }
1570 }
1571 to->mark = XML_REGEXP_MARK_NORMAL;
1572}
1573
1574/**
1575 * xmlFAEliminateEpsilonTransitions:
Daniel Veillard441bc322002-04-20 17:38:48 +00001576 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00001577 *
1578 */
1579static void
1580xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
1581 int statenr, transnr;
1582 xmlRegStatePtr state;
1583
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00001584 if (ctxt->states == NULL) return;
1585
1586
Daniel Veillard4255d502002-04-16 15:50:10 +00001587 /*
1588 * build the completed transitions bypassing the epsilons
1589 * Use a marking algorithm to avoid loops
1590 */
1591 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1592 state = ctxt->states[statenr];
1593 if (state == NULL)
1594 continue;
1595 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1596 if ((state->trans[transnr].atom == NULL) &&
1597 (state->trans[transnr].to >= 0)) {
1598 if (state->trans[transnr].to == statenr) {
1599 state->trans[transnr].to = -1;
1600#ifdef DEBUG_REGEXP_GRAPH
1601 printf("Removed loopback epsilon trans %d on %d\n",
1602 transnr, statenr);
1603#endif
1604 } else if (state->trans[transnr].count < 0) {
1605 int newto = state->trans[transnr].to;
1606
1607#ifdef DEBUG_REGEXP_GRAPH
1608 printf("Found epsilon trans %d from %d to %d\n",
1609 transnr, statenr, newto);
1610#endif
1611 state->mark = XML_REGEXP_MARK_START;
1612 xmlFAReduceEpsilonTransitions(ctxt, statenr,
1613 newto, state->trans[transnr].counter);
1614 state->mark = XML_REGEXP_MARK_NORMAL;
1615#ifdef DEBUG_REGEXP_GRAPH
1616 } else {
1617 printf("Found counted transition %d on %d\n",
1618 transnr, statenr);
1619#endif
1620 }
1621 }
1622 }
1623 }
1624 /*
1625 * Eliminate the epsilon transitions
1626 */
1627 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1628 state = ctxt->states[statenr];
1629 if (state == NULL)
1630 continue;
1631 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1632 if ((state->trans[transnr].atom == NULL) &&
1633 (state->trans[transnr].count < 0) &&
1634 (state->trans[transnr].to >= 0)) {
1635 state->trans[transnr].to = -1;
1636 }
1637 }
1638 }
Daniel Veillard23e73572002-09-19 19:56:43 +00001639
1640 /*
1641 * Use this pass to detect unreachable states too
1642 */
1643 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1644 state = ctxt->states[statenr];
1645 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001646 state->reached = XML_REGEXP_MARK_NORMAL;
Daniel Veillard23e73572002-09-19 19:56:43 +00001647 }
1648 state = ctxt->states[0];
1649 if (state != NULL)
William M. Brack779af002003-08-01 15:55:39 +00001650 state->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001651 while (state != NULL) {
1652 xmlRegStatePtr target = NULL;
William M. Brack779af002003-08-01 15:55:39 +00001653 state->reached = XML_REGEXP_MARK_VISITED;
Daniel Veillard23e73572002-09-19 19:56:43 +00001654 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001655 * Mark all states reachable from the current reachable state
Daniel Veillard23e73572002-09-19 19:56:43 +00001656 */
1657 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1658 if ((state->trans[transnr].to >= 0) &&
1659 ((state->trans[transnr].atom != NULL) ||
1660 (state->trans[transnr].count >= 0))) {
1661 int newto = state->trans[transnr].to;
1662
1663 if (ctxt->states[newto] == NULL)
1664 continue;
William M. Brack779af002003-08-01 15:55:39 +00001665 if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
1666 ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
Daniel Veillard23e73572002-09-19 19:56:43 +00001667 target = ctxt->states[newto];
1668 }
1669 }
1670 }
1671 /*
1672 * find the next accessible state not explored
1673 */
1674 if (target == NULL) {
1675 for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
1676 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001677 if ((state != NULL) && (state->reached ==
1678 XML_REGEXP_MARK_START)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001679 target = state;
1680 break;
1681 }
1682 }
1683 }
1684 state = target;
1685 }
1686 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1687 state = ctxt->states[statenr];
William M. Brack779af002003-08-01 15:55:39 +00001688 if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
Daniel Veillard23e73572002-09-19 19:56:43 +00001689#ifdef DEBUG_REGEXP_GRAPH
1690 printf("Removed unreachable state %d\n", statenr);
1691#endif
1692 xmlRegFreeState(state);
1693 ctxt->states[statenr] = NULL;
1694 }
1695 }
1696
Daniel Veillard4255d502002-04-16 15:50:10 +00001697}
1698
Daniel Veillarde19fc232002-04-22 16:01:24 +00001699/**
1700 * xmlFACompareAtoms:
1701 * @atom1: an atom
1702 * @atom2: an atom
1703 *
William M. Brackddf71d62004-05-06 04:17:26 +00001704 * Compares two atoms to check whether they are equivalents
Daniel Veillarde19fc232002-04-22 16:01:24 +00001705 *
1706 * Returns 1 if yes and 0 otherwise
1707 */
1708static int
1709xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) {
1710 if (atom1 == atom2)
1711 return(1);
1712 if ((atom1 == NULL) || (atom2 == NULL))
1713 return(0);
1714
1715 if (atom1->type != atom2->type)
1716 return(0);
1717 switch (atom1->type) {
1718 case XML_REGEXP_STRING:
1719 return(xmlStrEqual((xmlChar *)atom1->valuep,
1720 (xmlChar *)atom2->valuep));
1721 case XML_REGEXP_EPSILON:
1722 return(1);
1723 case XML_REGEXP_CHARVAL:
1724 return(atom1->codepoint == atom2->codepoint);
1725 case XML_REGEXP_RANGES:
1726 TODO;
1727 return(0);
1728 default:
1729 break;
1730 }
1731 return(1);
1732}
1733
1734/**
1735 * xmlFARecurseDeterminism:
1736 * @ctxt: a regexp parser context
1737 *
1738 * Check whether the associated regexp is determinist,
1739 * should be called after xmlFAEliminateEpsilonTransitions()
1740 *
1741 */
1742static int
1743xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
1744 int to, xmlRegAtomPtr atom) {
1745 int ret = 1;
1746 int transnr;
1747 xmlRegTransPtr t1;
1748
1749 if (state == NULL)
1750 return(ret);
1751 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1752 t1 = &(state->trans[transnr]);
1753 /*
1754 * check transitions conflicting with the one looked at
1755 */
1756 if (t1->atom == NULL) {
1757 if (t1->to == -1)
1758 continue;
1759 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1760 to, atom);
1761 if (ret == 0)
1762 return(0);
1763 continue;
1764 }
1765 if (t1->to != to)
1766 continue;
1767 if (xmlFACompareAtoms(t1->atom, atom))
1768 return(0);
1769 }
1770 return(ret);
1771}
1772
1773/**
1774 * xmlFAComputesDeterminism:
1775 * @ctxt: a regexp parser context
1776 *
1777 * Check whether the associated regexp is determinist,
1778 * should be called after xmlFAEliminateEpsilonTransitions()
1779 *
1780 */
1781static int
1782xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
1783 int statenr, transnr;
1784 xmlRegStatePtr state;
1785 xmlRegTransPtr t1, t2;
1786 int i;
1787 int ret = 1;
1788
Daniel Veillard4402ab42002-09-12 16:02:56 +00001789#ifdef DEBUG_REGEXP_GRAPH
1790 printf("xmlFAComputesDeterminism\n");
1791 xmlRegPrintCtxt(stdout, ctxt);
1792#endif
Daniel Veillarde19fc232002-04-22 16:01:24 +00001793 if (ctxt->determinist != -1)
1794 return(ctxt->determinist);
1795
1796 /*
William M. Brackddf71d62004-05-06 04:17:26 +00001797 * Check for all states that there aren't 2 transitions
Daniel Veillarde19fc232002-04-22 16:01:24 +00001798 * with the same atom and a different target.
1799 */
1800 for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
1801 state = ctxt->states[statenr];
1802 if (state == NULL)
1803 continue;
1804 for (transnr = 0;transnr < state->nbTrans;transnr++) {
1805 t1 = &(state->trans[transnr]);
1806 /*
1807 * Determinism checks in case of counted or all transitions
1808 * will have to be handled separately
1809 */
1810 if (t1->atom == NULL)
1811 continue;
1812 if (t1->to == -1) /* eliminated */
1813 continue;
1814 for (i = 0;i < transnr;i++) {
1815 t2 = &(state->trans[i]);
1816 if (t2->to == -1) /* eliminated */
1817 continue;
1818 if (t2->atom != NULL) {
1819 if (t1->to == t2->to) {
1820 if (xmlFACompareAtoms(t1->atom, t2->atom))
William M. Brackddf71d62004-05-06 04:17:26 +00001821 t2->to = -1; /* eliminated */
Daniel Veillarde19fc232002-04-22 16:01:24 +00001822 } else {
1823 /* not determinist ! */
1824 if (xmlFACompareAtoms(t1->atom, t2->atom))
1825 ret = 0;
1826 }
1827 } else if (t1->to != -1) {
1828 /*
1829 * do the closure in case of remaining specific
1830 * epsilon transitions like choices or all
1831 */
1832 ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
1833 t2->to, t2->atom);
1834 if (ret == 0)
1835 return(0);
1836 }
1837 }
1838 if (ret == 0)
1839 break;
1840 }
1841 if (ret == 0)
1842 break;
1843 }
1844 ctxt->determinist = ret;
1845 return(ret);
1846}
1847
Daniel Veillard4255d502002-04-16 15:50:10 +00001848/************************************************************************
1849 * *
1850 * Routines to check input against transition atoms *
1851 * *
1852 ************************************************************************/
1853
1854static int
1855xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
1856 int start, int end, const xmlChar *blockName) {
1857 int ret = 0;
1858
1859 switch (type) {
1860 case XML_REGEXP_STRING:
1861 case XML_REGEXP_SUBREG:
1862 case XML_REGEXP_RANGES:
1863 case XML_REGEXP_EPSILON:
1864 return(-1);
1865 case XML_REGEXP_ANYCHAR:
1866 ret = ((codepoint != '\n') && (codepoint != '\r'));
1867 break;
1868 case XML_REGEXP_CHARVAL:
1869 ret = ((codepoint >= start) && (codepoint <= end));
1870 break;
1871 case XML_REGEXP_NOTSPACE:
1872 neg = !neg;
1873 case XML_REGEXP_ANYSPACE:
1874 ret = ((codepoint == '\n') || (codepoint == '\r') ||
1875 (codepoint == '\t') || (codepoint == ' '));
1876 break;
1877 case XML_REGEXP_NOTINITNAME:
1878 neg = !neg;
1879 case XML_REGEXP_INITNAME:
William M. Brack871611b2003-10-18 04:53:14 +00001880 ret = (IS_LETTER(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001881 (codepoint == '_') || (codepoint == ':'));
1882 break;
1883 case XML_REGEXP_NOTNAMECHAR:
1884 neg = !neg;
1885 case XML_REGEXP_NAMECHAR:
William M. Brack871611b2003-10-18 04:53:14 +00001886 ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
Daniel Veillard4255d502002-04-16 15:50:10 +00001887 (codepoint == '.') || (codepoint == '-') ||
1888 (codepoint == '_') || (codepoint == ':') ||
William M. Brack871611b2003-10-18 04:53:14 +00001889 IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
Daniel Veillard4255d502002-04-16 15:50:10 +00001890 break;
1891 case XML_REGEXP_NOTDECIMAL:
1892 neg = !neg;
1893 case XML_REGEXP_DECIMAL:
1894 ret = xmlUCSIsCatNd(codepoint);
1895 break;
1896 case XML_REGEXP_REALCHAR:
1897 neg = !neg;
1898 case XML_REGEXP_NOTREALCHAR:
1899 ret = xmlUCSIsCatP(codepoint);
1900 if (ret == 0)
1901 ret = xmlUCSIsCatZ(codepoint);
1902 if (ret == 0)
1903 ret = xmlUCSIsCatC(codepoint);
1904 break;
1905 case XML_REGEXP_LETTER:
1906 ret = xmlUCSIsCatL(codepoint);
1907 break;
1908 case XML_REGEXP_LETTER_UPPERCASE:
1909 ret = xmlUCSIsCatLu(codepoint);
1910 break;
1911 case XML_REGEXP_LETTER_LOWERCASE:
1912 ret = xmlUCSIsCatLl(codepoint);
1913 break;
1914 case XML_REGEXP_LETTER_TITLECASE:
1915 ret = xmlUCSIsCatLt(codepoint);
1916 break;
1917 case XML_REGEXP_LETTER_MODIFIER:
1918 ret = xmlUCSIsCatLm(codepoint);
1919 break;
1920 case XML_REGEXP_LETTER_OTHERS:
1921 ret = xmlUCSIsCatLo(codepoint);
1922 break;
1923 case XML_REGEXP_MARK:
1924 ret = xmlUCSIsCatM(codepoint);
1925 break;
1926 case XML_REGEXP_MARK_NONSPACING:
1927 ret = xmlUCSIsCatMn(codepoint);
1928 break;
1929 case XML_REGEXP_MARK_SPACECOMBINING:
1930 ret = xmlUCSIsCatMc(codepoint);
1931 break;
1932 case XML_REGEXP_MARK_ENCLOSING:
1933 ret = xmlUCSIsCatMe(codepoint);
1934 break;
1935 case XML_REGEXP_NUMBER:
1936 ret = xmlUCSIsCatN(codepoint);
1937 break;
1938 case XML_REGEXP_NUMBER_DECIMAL:
1939 ret = xmlUCSIsCatNd(codepoint);
1940 break;
1941 case XML_REGEXP_NUMBER_LETTER:
1942 ret = xmlUCSIsCatNl(codepoint);
1943 break;
1944 case XML_REGEXP_NUMBER_OTHERS:
1945 ret = xmlUCSIsCatNo(codepoint);
1946 break;
1947 case XML_REGEXP_PUNCT:
1948 ret = xmlUCSIsCatP(codepoint);
1949 break;
1950 case XML_REGEXP_PUNCT_CONNECTOR:
1951 ret = xmlUCSIsCatPc(codepoint);
1952 break;
1953 case XML_REGEXP_PUNCT_DASH:
1954 ret = xmlUCSIsCatPd(codepoint);
1955 break;
1956 case XML_REGEXP_PUNCT_OPEN:
1957 ret = xmlUCSIsCatPs(codepoint);
1958 break;
1959 case XML_REGEXP_PUNCT_CLOSE:
1960 ret = xmlUCSIsCatPe(codepoint);
1961 break;
1962 case XML_REGEXP_PUNCT_INITQUOTE:
1963 ret = xmlUCSIsCatPi(codepoint);
1964 break;
1965 case XML_REGEXP_PUNCT_FINQUOTE:
1966 ret = xmlUCSIsCatPf(codepoint);
1967 break;
1968 case XML_REGEXP_PUNCT_OTHERS:
1969 ret = xmlUCSIsCatPo(codepoint);
1970 break;
1971 case XML_REGEXP_SEPAR:
1972 ret = xmlUCSIsCatZ(codepoint);
1973 break;
1974 case XML_REGEXP_SEPAR_SPACE:
1975 ret = xmlUCSIsCatZs(codepoint);
1976 break;
1977 case XML_REGEXP_SEPAR_LINE:
1978 ret = xmlUCSIsCatZl(codepoint);
1979 break;
1980 case XML_REGEXP_SEPAR_PARA:
1981 ret = xmlUCSIsCatZp(codepoint);
1982 break;
1983 case XML_REGEXP_SYMBOL:
1984 ret = xmlUCSIsCatS(codepoint);
1985 break;
1986 case XML_REGEXP_SYMBOL_MATH:
1987 ret = xmlUCSIsCatSm(codepoint);
1988 break;
1989 case XML_REGEXP_SYMBOL_CURRENCY:
1990 ret = xmlUCSIsCatSc(codepoint);
1991 break;
1992 case XML_REGEXP_SYMBOL_MODIFIER:
1993 ret = xmlUCSIsCatSk(codepoint);
1994 break;
1995 case XML_REGEXP_SYMBOL_OTHERS:
1996 ret = xmlUCSIsCatSo(codepoint);
1997 break;
1998 case XML_REGEXP_OTHER:
1999 ret = xmlUCSIsCatC(codepoint);
2000 break;
2001 case XML_REGEXP_OTHER_CONTROL:
2002 ret = xmlUCSIsCatCc(codepoint);
2003 break;
2004 case XML_REGEXP_OTHER_FORMAT:
2005 ret = xmlUCSIsCatCf(codepoint);
2006 break;
2007 case XML_REGEXP_OTHER_PRIVATE:
2008 ret = xmlUCSIsCatCo(codepoint);
2009 break;
2010 case XML_REGEXP_OTHER_NA:
2011 /* ret = xmlUCSIsCatCn(codepoint); */
2012 /* Seems it doesn't exist anymore in recent Unicode releases */
2013 ret = 0;
2014 break;
2015 case XML_REGEXP_BLOCK_NAME:
2016 ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
2017 break;
2018 }
2019 if (neg)
2020 return(!ret);
2021 return(ret);
2022}
2023
2024static int
2025xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
2026 int i, ret = 0;
2027 xmlRegRangePtr range;
2028
William M. Brack871611b2003-10-18 04:53:14 +00002029 if ((atom == NULL) || (!IS_CHAR(codepoint)))
Daniel Veillard4255d502002-04-16 15:50:10 +00002030 return(-1);
2031
2032 switch (atom->type) {
2033 case XML_REGEXP_SUBREG:
2034 case XML_REGEXP_EPSILON:
2035 return(-1);
2036 case XML_REGEXP_CHARVAL:
2037 return(codepoint == atom->codepoint);
2038 case XML_REGEXP_RANGES: {
2039 int accept = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002040
Daniel Veillard4255d502002-04-16 15:50:10 +00002041 for (i = 0;i < atom->nbRanges;i++) {
2042 range = atom->ranges[i];
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002043 if (range->neg == 2) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002044 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2045 0, range->start, range->end,
2046 range->blockName);
2047 if (ret != 0)
2048 return(0); /* excluded char */
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002049 } else if (range->neg) {
2050 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2051 0, range->start, range->end,
2052 range->blockName);
2053 if (ret == 0)
Daniel Veillardf2a12832003-11-24 13:04:35 +00002054 accept = 1;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00002055 else
2056 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00002057 } else {
2058 ret = xmlRegCheckCharacterRange(range->type, codepoint,
2059 0, range->start, range->end,
2060 range->blockName);
2061 if (ret != 0)
2062 accept = 1; /* might still be excluded */
2063 }
2064 }
2065 return(accept);
2066 }
2067 case XML_REGEXP_STRING:
2068 printf("TODO: XML_REGEXP_STRING\n");
2069 return(-1);
2070 case XML_REGEXP_ANYCHAR:
2071 case XML_REGEXP_ANYSPACE:
2072 case XML_REGEXP_NOTSPACE:
2073 case XML_REGEXP_INITNAME:
2074 case XML_REGEXP_NOTINITNAME:
2075 case XML_REGEXP_NAMECHAR:
2076 case XML_REGEXP_NOTNAMECHAR:
2077 case XML_REGEXP_DECIMAL:
2078 case XML_REGEXP_NOTDECIMAL:
2079 case XML_REGEXP_REALCHAR:
2080 case XML_REGEXP_NOTREALCHAR:
2081 case XML_REGEXP_LETTER:
2082 case XML_REGEXP_LETTER_UPPERCASE:
2083 case XML_REGEXP_LETTER_LOWERCASE:
2084 case XML_REGEXP_LETTER_TITLECASE:
2085 case XML_REGEXP_LETTER_MODIFIER:
2086 case XML_REGEXP_LETTER_OTHERS:
2087 case XML_REGEXP_MARK:
2088 case XML_REGEXP_MARK_NONSPACING:
2089 case XML_REGEXP_MARK_SPACECOMBINING:
2090 case XML_REGEXP_MARK_ENCLOSING:
2091 case XML_REGEXP_NUMBER:
2092 case XML_REGEXP_NUMBER_DECIMAL:
2093 case XML_REGEXP_NUMBER_LETTER:
2094 case XML_REGEXP_NUMBER_OTHERS:
2095 case XML_REGEXP_PUNCT:
2096 case XML_REGEXP_PUNCT_CONNECTOR:
2097 case XML_REGEXP_PUNCT_DASH:
2098 case XML_REGEXP_PUNCT_OPEN:
2099 case XML_REGEXP_PUNCT_CLOSE:
2100 case XML_REGEXP_PUNCT_INITQUOTE:
2101 case XML_REGEXP_PUNCT_FINQUOTE:
2102 case XML_REGEXP_PUNCT_OTHERS:
2103 case XML_REGEXP_SEPAR:
2104 case XML_REGEXP_SEPAR_SPACE:
2105 case XML_REGEXP_SEPAR_LINE:
2106 case XML_REGEXP_SEPAR_PARA:
2107 case XML_REGEXP_SYMBOL:
2108 case XML_REGEXP_SYMBOL_MATH:
2109 case XML_REGEXP_SYMBOL_CURRENCY:
2110 case XML_REGEXP_SYMBOL_MODIFIER:
2111 case XML_REGEXP_SYMBOL_OTHERS:
2112 case XML_REGEXP_OTHER:
2113 case XML_REGEXP_OTHER_CONTROL:
2114 case XML_REGEXP_OTHER_FORMAT:
2115 case XML_REGEXP_OTHER_PRIVATE:
2116 case XML_REGEXP_OTHER_NA:
2117 case XML_REGEXP_BLOCK_NAME:
2118 ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
2119 (const xmlChar *)atom->valuep);
2120 if (atom->neg)
2121 ret = !ret;
2122 break;
2123 }
2124 return(ret);
2125}
2126
2127/************************************************************************
2128 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002129 * Saving and restoring state of an execution context *
Daniel Veillard4255d502002-04-16 15:50:10 +00002130 * *
2131 ************************************************************************/
2132
2133#ifdef DEBUG_REGEXP_EXEC
2134static void
2135xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
2136 printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
2137 if (exec->inputStack != NULL) {
2138 int i;
2139 printf(": ");
2140 for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
2141 printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]);
2142 } else {
2143 printf(": %s", &(exec->inputString[exec->index]));
2144 }
2145 printf("\n");
2146}
2147#endif
2148
2149static void
2150xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
2151#ifdef DEBUG_REGEXP_EXEC
2152 printf("saving ");
2153 exec->transno++;
2154 xmlFARegDebugExec(exec);
2155 exec->transno--;
2156#endif
2157
2158 if (exec->maxRollbacks == 0) {
2159 exec->maxRollbacks = 4;
2160 exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
2161 sizeof(xmlRegExecRollback));
2162 if (exec->rollbacks == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002163 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002164 exec->maxRollbacks = 0;
2165 return;
2166 }
2167 memset(exec->rollbacks, 0,
2168 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2169 } else if (exec->nbRollbacks >= exec->maxRollbacks) {
2170 xmlRegExecRollback *tmp;
2171 int len = exec->maxRollbacks;
2172
2173 exec->maxRollbacks *= 2;
2174 tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
2175 exec->maxRollbacks * sizeof(xmlRegExecRollback));
2176 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002177 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002178 exec->maxRollbacks /= 2;
2179 return;
2180 }
2181 exec->rollbacks = tmp;
2182 tmp = &exec->rollbacks[len];
2183 memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
2184 }
2185 exec->rollbacks[exec->nbRollbacks].state = exec->state;
2186 exec->rollbacks[exec->nbRollbacks].index = exec->index;
2187 exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
2188 if (exec->comp->nbCounters > 0) {
2189 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2190 exec->rollbacks[exec->nbRollbacks].counts = (int *)
2191 xmlMalloc(exec->comp->nbCounters * sizeof(int));
2192 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002193 xmlRegexpErrMemory(NULL, "saving regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002194 exec->status = -5;
2195 return;
2196 }
2197 }
2198 memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
2199 exec->comp->nbCounters * sizeof(int));
2200 }
2201 exec->nbRollbacks++;
2202}
2203
2204static void
2205xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
2206 if (exec->nbRollbacks <= 0) {
2207 exec->status = -1;
2208#ifdef DEBUG_REGEXP_EXEC
2209 printf("rollback failed on empty stack\n");
2210#endif
2211 return;
2212 }
2213 exec->nbRollbacks--;
2214 exec->state = exec->rollbacks[exec->nbRollbacks].state;
2215 exec->index = exec->rollbacks[exec->nbRollbacks].index;
2216 exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
2217 if (exec->comp->nbCounters > 0) {
2218 if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
2219 fprintf(stderr, "exec save: allocation failed");
2220 exec->status = -6;
2221 return;
2222 }
2223 memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
2224 exec->comp->nbCounters * sizeof(int));
2225 }
2226
2227#ifdef DEBUG_REGEXP_EXEC
2228 printf("restored ");
2229 xmlFARegDebugExec(exec);
2230#endif
2231}
2232
2233/************************************************************************
2234 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002235 * Verifier, running an input against a compiled regexp *
Daniel Veillard4255d502002-04-16 15:50:10 +00002236 * *
2237 ************************************************************************/
2238
2239static int
2240xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
2241 xmlRegExecCtxt execval;
2242 xmlRegExecCtxtPtr exec = &execval;
2243 int ret, codepoint, len;
2244
2245 exec->inputString = content;
2246 exec->index = 0;
2247 exec->determinist = 1;
2248 exec->maxRollbacks = 0;
2249 exec->nbRollbacks = 0;
2250 exec->rollbacks = NULL;
2251 exec->status = 0;
2252 exec->comp = comp;
2253 exec->state = comp->states[0];
2254 exec->transno = 0;
2255 exec->transcount = 0;
Daniel Veillardf2a12832003-11-24 13:04:35 +00002256 exec->inputStack = NULL;
2257 exec->inputStackMax = 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00002258 if (comp->nbCounters > 0) {
2259 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
Daniel Veillardff46a042003-10-08 08:53:17 +00002260 if (exec->counts == NULL) {
2261 xmlRegexpErrMemory(NULL, "running regexp");
Daniel Veillard4255d502002-04-16 15:50:10 +00002262 return(-1);
Daniel Veillardff46a042003-10-08 08:53:17 +00002263 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002264 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2265 } else
2266 exec->counts = NULL;
2267 while ((exec->status == 0) &&
2268 ((exec->inputString[exec->index] != 0) ||
2269 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
2270 xmlRegTransPtr trans;
2271 xmlRegAtomPtr atom;
2272
2273 /*
William M. Brack0e00b282004-04-26 15:40:47 +00002274 * If end of input on non-terminal state, rollback, however we may
Daniel Veillard4255d502002-04-16 15:50:10 +00002275 * still have epsilon like transition for counted transitions
William M. Brack0e00b282004-04-26 15:40:47 +00002276 * on counters, in that case don't break too early. Additionally,
2277 * if we are working on a range like "AB{0,2}", where B is not present,
2278 * we don't want to break.
Daniel Veillard4255d502002-04-16 15:50:10 +00002279 */
William M. Brack0e00b282004-04-26 15:40:47 +00002280 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
William M. Brackddf71d62004-05-06 04:17:26 +00002281 /*
2282 * if there is a transition, we must check if
2283 * atom allows minOccurs of 0
2284 */
2285 if (exec->transno < exec->state->nbTrans) {
William M. Brack0e00b282004-04-26 15:40:47 +00002286 trans = &exec->state->trans[exec->transno];
2287 if (trans->to >=0) {
2288 atom = trans->atom;
2289 if (!((atom->min == 0) && (atom->max > 0)))
2290 goto rollback;
2291 }
2292 } else
2293 goto rollback;
2294 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002295
2296 exec->transcount = 0;
2297 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2298 trans = &exec->state->trans[exec->transno];
2299 if (trans->to < 0)
2300 continue;
2301 atom = trans->atom;
2302 ret = 0;
2303 if (trans->count >= 0) {
2304 int count;
2305 xmlRegCounterPtr counter;
2306
2307 /*
2308 * A counted transition.
2309 */
2310
2311 count = exec->counts[trans->count];
2312 counter = &exec->comp->counters[trans->count];
2313#ifdef DEBUG_REGEXP_EXEC
2314 printf("testing count %d: val %d, min %d, max %d\n",
2315 trans->count, count, counter->min, counter->max);
2316#endif
2317 ret = ((count >= counter->min) && (count <= counter->max));
2318 } else if (atom == NULL) {
2319 fprintf(stderr, "epsilon transition left at runtime\n");
2320 exec->status = -2;
2321 break;
2322 } else if (exec->inputString[exec->index] != 0) {
2323 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
2324 ret = xmlRegCheckCharacter(atom, codepoint);
William M. Brack0e00b282004-04-26 15:40:47 +00002325 if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002326 xmlRegStatePtr to = comp->states[trans->to];
2327
2328 /*
2329 * this is a multiple input sequence
2330 */
2331 if (exec->state->nbTrans > exec->transno + 1) {
2332 xmlFARegExecSave(exec);
2333 }
2334 exec->transcount = 1;
2335 do {
2336 /*
2337 * Try to progress as much as possible on the input
2338 */
2339 if (exec->transcount == atom->max) {
2340 break;
2341 }
2342 exec->index += len;
2343 /*
2344 * End of input: stop here
2345 */
2346 if (exec->inputString[exec->index] == 0) {
2347 exec->index -= len;
2348 break;
2349 }
2350 if (exec->transcount >= atom->min) {
2351 int transno = exec->transno;
2352 xmlRegStatePtr state = exec->state;
2353
2354 /*
2355 * The transition is acceptable save it
2356 */
2357 exec->transno = -1; /* trick */
2358 exec->state = to;
2359 xmlFARegExecSave(exec);
2360 exec->transno = transno;
2361 exec->state = state;
2362 }
2363 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
2364 len);
2365 ret = xmlRegCheckCharacter(atom, codepoint);
2366 exec->transcount++;
2367 } while (ret == 1);
2368 if (exec->transcount < atom->min)
2369 ret = 0;
2370
2371 /*
2372 * If the last check failed but one transition was found
2373 * possible, rollback
2374 */
2375 if (ret < 0)
2376 ret = 0;
2377 if (ret == 0) {
2378 goto rollback;
2379 }
William M. Brack0e00b282004-04-26 15:40:47 +00002380 } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
2381 /*
2382 * we don't match on the codepoint, but minOccurs of 0
2383 * says that's ok. Setting len to 0 inhibits stepping
2384 * over the codepoint.
2385 */
2386 exec->transcount = 1;
2387 len = 0;
2388 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002389 }
William M. Brack0e00b282004-04-26 15:40:47 +00002390 } else if ((atom->min == 0) && (atom->max > 0)) {
2391 /* another spot to match when minOccurs is 0 */
2392 exec->transcount = 1;
2393 len = 0;
2394 ret = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00002395 }
2396 if (ret == 1) {
2397 if (exec->state->nbTrans > exec->transno + 1) {
2398 xmlFARegExecSave(exec);
2399 }
2400 if (trans->counter >= 0) {
2401#ifdef DEBUG_REGEXP_EXEC
2402 printf("Increasing count %d\n", trans->counter);
2403#endif
2404 exec->counts[trans->counter]++;
2405 }
2406#ifdef DEBUG_REGEXP_EXEC
2407 printf("entering state %d\n", trans->to);
2408#endif
2409 exec->state = comp->states[trans->to];
2410 exec->transno = 0;
2411 if (trans->atom != NULL) {
2412 exec->index += len;
2413 }
2414 goto progress;
2415 } else if (ret < 0) {
2416 exec->status = -4;
2417 break;
2418 }
2419 }
2420 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2421rollback:
2422 /*
2423 * Failed to find a way out
2424 */
2425 exec->determinist = 0;
2426 xmlFARegExecRollBack(exec);
2427 }
2428progress:
2429 continue;
2430 }
2431 if (exec->rollbacks != NULL) {
2432 if (exec->counts != NULL) {
2433 int i;
2434
2435 for (i = 0;i < exec->maxRollbacks;i++)
2436 if (exec->rollbacks[i].counts != NULL)
2437 xmlFree(exec->rollbacks[i].counts);
2438 }
2439 xmlFree(exec->rollbacks);
2440 }
2441 if (exec->counts != NULL)
2442 xmlFree(exec->counts);
2443 if (exec->status == 0)
2444 return(1);
2445 if (exec->status == -1)
2446 return(0);
2447 return(exec->status);
2448}
2449
2450/************************************************************************
2451 * *
William M. Brackddf71d62004-05-06 04:17:26 +00002452 * Progressive interface to the verifier one atom at a time *
Daniel Veillard4255d502002-04-16 15:50:10 +00002453 * *
2454 ************************************************************************/
2455
2456/**
Daniel Veillard01c13b52002-12-10 15:19:08 +00002457 * xmlRegNewExecCtxt:
Daniel Veillard4255d502002-04-16 15:50:10 +00002458 * @comp: a precompiled regular expression
2459 * @callback: a callback function used for handling progresses in the
2460 * automata matching phase
2461 * @data: the context data associated to the callback in this context
2462 *
2463 * Build a context used for progressive evaluation of a regexp.
Daniel Veillard01c13b52002-12-10 15:19:08 +00002464 *
2465 * Returns the new context
Daniel Veillard4255d502002-04-16 15:50:10 +00002466 */
2467xmlRegExecCtxtPtr
2468xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
2469 xmlRegExecCtxtPtr exec;
2470
2471 if (comp == NULL)
2472 return(NULL);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00002473 if ((comp->compact == NULL) && (comp->states == NULL))
2474 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00002475 exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
2476 if (exec == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002477 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002478 return(NULL);
2479 }
2480 memset(exec, 0, sizeof(xmlRegExecCtxt));
2481 exec->inputString = NULL;
2482 exec->index = 0;
2483 exec->determinist = 1;
2484 exec->maxRollbacks = 0;
2485 exec->nbRollbacks = 0;
2486 exec->rollbacks = NULL;
2487 exec->status = 0;
2488 exec->comp = comp;
Daniel Veillard23e73572002-09-19 19:56:43 +00002489 if (comp->compact == NULL)
2490 exec->state = comp->states[0];
Daniel Veillard4255d502002-04-16 15:50:10 +00002491 exec->transno = 0;
2492 exec->transcount = 0;
2493 exec->callback = callback;
2494 exec->data = data;
2495 if (comp->nbCounters > 0) {
2496 exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
2497 if (exec->counts == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002498 xmlRegexpErrMemory(NULL, "creating execution context");
Daniel Veillard4255d502002-04-16 15:50:10 +00002499 xmlFree(exec);
2500 return(NULL);
2501 }
2502 memset(exec->counts, 0, comp->nbCounters * sizeof(int));
2503 } else
2504 exec->counts = NULL;
2505 exec->inputStackMax = 0;
2506 exec->inputStackNr = 0;
2507 exec->inputStack = NULL;
2508 return(exec);
2509}
2510
2511/**
2512 * xmlRegFreeExecCtxt:
2513 * @exec: a regular expression evaulation context
2514 *
2515 * Free the structures associated to a regular expression evaulation context.
2516 */
2517void
2518xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
2519 if (exec == NULL)
2520 return;
2521
2522 if (exec->rollbacks != NULL) {
2523 if (exec->counts != NULL) {
2524 int i;
2525
2526 for (i = 0;i < exec->maxRollbacks;i++)
2527 if (exec->rollbacks[i].counts != NULL)
2528 xmlFree(exec->rollbacks[i].counts);
2529 }
2530 xmlFree(exec->rollbacks);
2531 }
2532 if (exec->counts != NULL)
2533 xmlFree(exec->counts);
2534 if (exec->inputStack != NULL) {
2535 int i;
2536
Daniel Veillard32370232002-10-16 14:08:14 +00002537 for (i = 0;i < exec->inputStackNr;i++) {
2538 if (exec->inputStack[i].value != NULL)
2539 xmlFree(exec->inputStack[i].value);
2540 }
Daniel Veillard4255d502002-04-16 15:50:10 +00002541 xmlFree(exec->inputStack);
2542 }
2543 xmlFree(exec);
2544}
2545
2546static void
2547xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2548 void *data) {
2549#ifdef DEBUG_PUSH
2550 printf("saving value: %d:%s\n", exec->inputStackNr, value);
2551#endif
2552 if (exec->inputStackMax == 0) {
2553 exec->inputStackMax = 4;
2554 exec->inputStack = (xmlRegInputTokenPtr)
2555 xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
2556 if (exec->inputStack == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002557 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002558 exec->inputStackMax = 0;
2559 return;
2560 }
2561 } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
2562 xmlRegInputTokenPtr tmp;
2563
2564 exec->inputStackMax *= 2;
2565 tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
2566 exec->inputStackMax * sizeof(xmlRegInputToken));
2567 if (tmp == NULL) {
Daniel Veillardff46a042003-10-08 08:53:17 +00002568 xmlRegexpErrMemory(NULL, "pushing input string");
Daniel Veillard4255d502002-04-16 15:50:10 +00002569 exec->inputStackMax /= 2;
2570 return;
2571 }
2572 exec->inputStack = tmp;
2573 }
2574 exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
2575 exec->inputStack[exec->inputStackNr].data = data;
2576 exec->inputStackNr++;
2577 exec->inputStack[exec->inputStackNr].value = NULL;
2578 exec->inputStack[exec->inputStackNr].data = NULL;
2579}
2580
Daniel Veillardc0826a72004-08-10 14:17:33 +00002581/**
2582 * xmlRegStrEqualWildcard:
2583 * @expStr: the string to be evaluated
2584 * @valStr: the validation string
2585 *
2586 * Checks if both strings are equal or have the same content. "*"
2587 * can be used as a wildcard in @valStr; "|" is used as a seperator of
2588 * substrings in both @expStr and @valStr.
2589 *
2590 * Returns 1 if the comparison is satisfied and the number of substrings
2591 * is equal, 0 otherwise.
2592 */
2593
2594static int
2595xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
2596 if (expStr == valStr) return(1);
2597 if (expStr == NULL) return(0);
2598 if (valStr == NULL) return(0);
2599 do {
2600 /*
2601 * Eval if we have a wildcard for the current item.
2602 */
2603 if (*expStr != *valStr) {
2604 if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
2605 do {
2606 if (*valStr == XML_REG_STRING_SEPARATOR)
2607 break;
2608 *valStr++;
2609 } while (*valStr != 0);
2610 continue;
2611 } else
2612 return(0);
2613 }
2614 *expStr++;
2615 *valStr++;
2616 } while (*valStr != 0);
2617 if (*expStr != 0)
2618 return (0);
2619 else
2620 return (1);
2621}
Daniel Veillard4255d502002-04-16 15:50:10 +00002622
2623/**
Daniel Veillard23e73572002-09-19 19:56:43 +00002624 * xmlRegCompactPushString:
2625 * @exec: a regexp execution context
2626 * @comp: the precompiled exec with a compact table
2627 * @value: a string token input
2628 * @data: data associated to the token to reuse in callbacks
2629 *
2630 * Push one input token in the execution context
2631 *
2632 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2633 * a negative value in case of error.
2634 */
2635static int
2636xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
2637 xmlRegexpPtr comp,
2638 const xmlChar *value,
2639 void *data) {
2640 int state = exec->index;
2641 int i, target;
2642
2643 if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
2644 return(-1);
2645
2646 if (value == NULL) {
2647 /*
2648 * are we at a final state ?
2649 */
2650 if (comp->compact[state * (comp->nbstrings + 1)] ==
2651 XML_REGEXP_FINAL_STATE)
2652 return(1);
2653 return(0);
2654 }
2655
2656#ifdef DEBUG_PUSH
2657 printf("value pushed: %s\n", value);
2658#endif
2659
2660 /*
William M. Brackddf71d62004-05-06 04:17:26 +00002661 * Examine all outside transitions from current state
Daniel Veillard23e73572002-09-19 19:56:43 +00002662 */
2663 for (i = 0;i < comp->nbstrings;i++) {
2664 target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
2665 if ((target > 0) && (target <= comp->nbstates)) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002666 target--; /* to avoid 0 */
2667 if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
2668 exec->index = target;
Daniel Veillard118aed72002-09-24 14:13:13 +00002669 if ((exec->callback != NULL) && (comp->transdata != NULL)) {
2670 exec->callback(exec->data, value,
2671 comp->transdata[state * comp->nbstrings + i], data);
2672 }
Daniel Veillard23e73572002-09-19 19:56:43 +00002673#ifdef DEBUG_PUSH
2674 printf("entering state %d\n", target);
2675#endif
2676 if (comp->compact[target * (comp->nbstrings + 1)] ==
2677 XML_REGEXP_FINAL_STATE)
2678 return(1);
2679 return(0);
2680 }
2681 }
2682 }
2683 /*
2684 * Failed to find an exit transition out from current state for the
2685 * current token
2686 */
2687#ifdef DEBUG_PUSH
2688 printf("failed to find a transition for %s on state %d\n", value, state);
2689#endif
2690 exec->status = -1;
2691 return(-1);
2692}
2693
2694/**
Daniel Veillard4255d502002-04-16 15:50:10 +00002695 * xmlRegExecPushString:
Daniel Veillardea7751d2002-12-20 00:16:24 +00002696 * @exec: a regexp execution context or NULL to indicate the end
Daniel Veillard4255d502002-04-16 15:50:10 +00002697 * @value: a string token input
2698 * @data: data associated to the token to reuse in callbacks
2699 *
2700 * Push one input token in the execution context
2701 *
2702 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
2703 * a negative value in case of error.
2704 */
2705int
2706xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
2707 void *data) {
2708 xmlRegTransPtr trans;
2709 xmlRegAtomPtr atom;
2710 int ret;
2711 int final = 0;
2712
2713 if (exec == NULL)
2714 return(-1);
Daniel Veillard23e73572002-09-19 19:56:43 +00002715 if (exec->comp == NULL)
2716 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00002717 if (exec->status != 0)
2718 return(exec->status);
2719
Daniel Veillard23e73572002-09-19 19:56:43 +00002720 if (exec->comp->compact != NULL)
2721 return(xmlRegCompactPushString(exec, exec->comp, value, data));
2722
Daniel Veillard4255d502002-04-16 15:50:10 +00002723 if (value == NULL) {
2724 if (exec->state->type == XML_REGEXP_FINAL_STATE)
2725 return(1);
2726 final = 1;
2727 }
2728
2729#ifdef DEBUG_PUSH
2730 printf("value pushed: %s\n", value);
2731#endif
2732 /*
2733 * If we have an active rollback stack push the new value there
2734 * and get back to where we were left
2735 */
2736 if ((value != NULL) && (exec->inputStackNr > 0)) {
2737 xmlFARegExecSaveInputString(exec, value, data);
2738 value = exec->inputStack[exec->index].value;
2739 data = exec->inputStack[exec->index].data;
2740#ifdef DEBUG_PUSH
2741 printf("value loaded: %s\n", value);
2742#endif
2743 }
2744
2745 while ((exec->status == 0) &&
2746 ((value != NULL) ||
2747 ((final == 1) &&
2748 (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
2749
2750 /*
2751 * End of input on non-terminal state, rollback, however we may
2752 * still have epsilon like transition for counted transitions
2753 * on counters, in that case don't break too early.
2754 */
Daniel Veillardb509f152002-04-17 16:28:10 +00002755 if ((value == NULL) && (exec->counts == NULL))
Daniel Veillard4255d502002-04-16 15:50:10 +00002756 goto rollback;
2757
2758 exec->transcount = 0;
2759 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
2760 trans = &exec->state->trans[exec->transno];
2761 if (trans->to < 0)
2762 continue;
2763 atom = trans->atom;
2764 ret = 0;
Daniel Veillard441bc322002-04-20 17:38:48 +00002765 if (trans->count == REGEXP_ALL_LAX_COUNTER) {
2766 int i;
2767 int count;
2768 xmlRegTransPtr t;
2769 xmlRegCounterPtr counter;
2770
2771 ret = 0;
2772
2773#ifdef DEBUG_PUSH
2774 printf("testing all lax %d\n", trans->count);
2775#endif
2776 /*
2777 * Check all counted transitions from the current state
2778 */
2779 if ((value == NULL) && (final)) {
2780 ret = 1;
2781 } else if (value != NULL) {
2782 for (i = 0;i < exec->state->nbTrans;i++) {
2783 t = &exec->state->trans[i];
2784 if ((t->counter < 0) || (t == trans))
2785 continue;
2786 counter = &exec->comp->counters[t->counter];
2787 count = exec->counts[t->counter];
2788 if ((count < counter->max) &&
2789 (t->atom != NULL) &&
2790 (xmlStrEqual(value, t->atom->valuep))) {
2791 ret = 0;
2792 break;
2793 }
2794 if ((count >= counter->min) &&
2795 (count < counter->max) &&
2796 (xmlStrEqual(value, t->atom->valuep))) {
2797 ret = 1;
2798 break;
2799 }
2800 }
2801 }
2802 } else if (trans->count == REGEXP_ALL_COUNTER) {
Daniel Veillard8a001f62002-04-20 07:24:11 +00002803 int i;
2804 int count;
2805 xmlRegTransPtr t;
2806 xmlRegCounterPtr counter;
2807
2808 ret = 1;
2809
2810#ifdef DEBUG_PUSH
2811 printf("testing all %d\n", trans->count);
2812#endif
2813 /*
2814 * Check all counted transitions from the current state
2815 */
2816 for (i = 0;i < exec->state->nbTrans;i++) {
2817 t = &exec->state->trans[i];
2818 if ((t->counter < 0) || (t == trans))
2819 continue;
2820 counter = &exec->comp->counters[t->counter];
2821 count = exec->counts[t->counter];
2822 if ((count < counter->min) || (count > counter->max)) {
2823 ret = 0;
2824 break;
2825 }
2826 }
2827 } else if (trans->count >= 0) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002828 int count;
2829 xmlRegCounterPtr counter;
2830
2831 /*
2832 * A counted transition.
2833 */
2834
2835 count = exec->counts[trans->count];
2836 counter = &exec->comp->counters[trans->count];
2837#ifdef DEBUG_PUSH
2838 printf("testing count %d: val %d, min %d, max %d\n",
2839 trans->count, count, counter->min, counter->max);
2840#endif
2841 ret = ((count >= counter->min) && (count <= counter->max));
2842 } else if (atom == NULL) {
2843 fprintf(stderr, "epsilon transition left at runtime\n");
2844 exec->status = -2;
2845 break;
2846 } else if (value != NULL) {
Daniel Veillardc0826a72004-08-10 14:17:33 +00002847 ret = xmlRegStrEqualWildcard(atom->valuep, value);
Daniel Veillard441bc322002-04-20 17:38:48 +00002848 if ((ret == 1) && (trans->counter >= 0)) {
2849 xmlRegCounterPtr counter;
2850 int count;
2851
2852 count = exec->counts[trans->counter];
2853 counter = &exec->comp->counters[trans->counter];
2854 if (count >= counter->max)
2855 ret = 0;
2856 }
2857
Daniel Veillard4255d502002-04-16 15:50:10 +00002858 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
2859 xmlRegStatePtr to = exec->comp->states[trans->to];
2860
2861 /*
2862 * this is a multiple input sequence
2863 */
2864 if (exec->state->nbTrans > exec->transno + 1) {
2865 if (exec->inputStackNr <= 0) {
2866 xmlFARegExecSaveInputString(exec, value, data);
2867 }
2868 xmlFARegExecSave(exec);
2869 }
2870 exec->transcount = 1;
2871 do {
2872 /*
2873 * Try to progress as much as possible on the input
2874 */
2875 if (exec->transcount == atom->max) {
2876 break;
2877 }
2878 exec->index++;
2879 value = exec->inputStack[exec->index].value;
2880 data = exec->inputStack[exec->index].data;
2881#ifdef DEBUG_PUSH
2882 printf("value loaded: %s\n", value);
2883#endif
2884
2885 /*
2886 * End of input: stop here
2887 */
2888 if (value == NULL) {
2889 exec->index --;
2890 break;
2891 }
2892 if (exec->transcount >= atom->min) {
2893 int transno = exec->transno;
2894 xmlRegStatePtr state = exec->state;
2895
2896 /*
2897 * The transition is acceptable save it
2898 */
2899 exec->transno = -1; /* trick */
2900 exec->state = to;
2901 if (exec->inputStackNr <= 0) {
2902 xmlFARegExecSaveInputString(exec, value, data);
2903 }
2904 xmlFARegExecSave(exec);
2905 exec->transno = transno;
2906 exec->state = state;
2907 }
2908 ret = xmlStrEqual(value, atom->valuep);
2909 exec->transcount++;
2910 } while (ret == 1);
2911 if (exec->transcount < atom->min)
2912 ret = 0;
2913
2914 /*
2915 * If the last check failed but one transition was found
2916 * possible, rollback
2917 */
2918 if (ret < 0)
2919 ret = 0;
2920 if (ret == 0) {
2921 goto rollback;
2922 }
2923 }
2924 }
2925 if (ret == 1) {
William M. Brack98873952003-12-26 06:03:14 +00002926 if ((exec->callback != NULL) && (atom != NULL) &&
2927 (data != NULL)) {
Daniel Veillard4255d502002-04-16 15:50:10 +00002928 exec->callback(exec->data, atom->valuep,
2929 atom->data, data);
2930 }
2931 if (exec->state->nbTrans > exec->transno + 1) {
2932 if (exec->inputStackNr <= 0) {
2933 xmlFARegExecSaveInputString(exec, value, data);
2934 }
2935 xmlFARegExecSave(exec);
2936 }
2937 if (trans->counter >= 0) {
2938#ifdef DEBUG_PUSH
2939 printf("Increasing count %d\n", trans->counter);
2940#endif
2941 exec->counts[trans->counter]++;
2942 }
2943#ifdef DEBUG_PUSH
2944 printf("entering state %d\n", trans->to);
2945#endif
2946 exec->state = exec->comp->states[trans->to];
2947 exec->transno = 0;
2948 if (trans->atom != NULL) {
2949 if (exec->inputStack != NULL) {
2950 exec->index++;
2951 if (exec->index < exec->inputStackNr) {
2952 value = exec->inputStack[exec->index].value;
2953 data = exec->inputStack[exec->index].data;
2954#ifdef DEBUG_PUSH
2955 printf("value loaded: %s\n", value);
2956#endif
2957 } else {
2958 value = NULL;
2959 data = NULL;
2960#ifdef DEBUG_PUSH
2961 printf("end of input\n");
2962#endif
2963 }
2964 } else {
2965 value = NULL;
2966 data = NULL;
2967#ifdef DEBUG_PUSH
2968 printf("end of input\n");
2969#endif
2970 }
2971 }
2972 goto progress;
2973 } else if (ret < 0) {
2974 exec->status = -4;
2975 break;
2976 }
2977 }
2978 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
2979rollback:
2980 /*
2981 * Failed to find a way out
2982 */
2983 exec->determinist = 0;
2984 xmlFARegExecRollBack(exec);
2985 if (exec->status == 0) {
2986 value = exec->inputStack[exec->index].value;
2987 data = exec->inputStack[exec->index].data;
2988#ifdef DEBUG_PUSH
2989 printf("value loaded: %s\n", value);
2990#endif
2991 }
2992 }
2993progress:
2994 continue;
2995 }
2996 if (exec->status == 0) {
2997 return(exec->state->type == XML_REGEXP_FINAL_STATE);
2998 }
2999 return(exec->status);
3000}
3001
Daniel Veillard52b48c72003-04-13 19:53:42 +00003002/**
3003 * xmlRegExecPushString2:
3004 * @exec: a regexp execution context or NULL to indicate the end
3005 * @value: the first string token input
3006 * @value2: the second string token input
3007 * @data: data associated to the token to reuse in callbacks
3008 *
3009 * Push one input token in the execution context
3010 *
3011 * Returns: 1 if the regexp reached a final state, 0 if non-final, and
3012 * a negative value in case of error.
3013 */
3014int
3015xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
3016 const xmlChar *value2, void *data) {
3017 xmlChar buf[150];
3018 int lenn, lenp, ret;
3019 xmlChar *str;
3020
3021 if (exec == NULL)
3022 return(-1);
3023 if (exec->comp == NULL)
3024 return(-1);
3025 if (exec->status != 0)
3026 return(exec->status);
3027
3028 if (value2 == NULL)
3029 return(xmlRegExecPushString(exec, value, data));
3030
3031 lenn = strlen((char *) value2);
3032 lenp = strlen((char *) value);
3033
3034 if (150 < lenn + lenp + 2) {
Daniel Veillard3c908dc2003-04-19 00:07:51 +00003035 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00003036 if (str == NULL) {
3037 exec->status = -1;
3038 return(-1);
3039 }
3040 } else {
3041 str = buf;
3042 }
3043 memcpy(&str[0], value, lenp);
Daniel Veillardc0826a72004-08-10 14:17:33 +00003044 str[lenp] = XML_REG_STRING_SEPARATOR;
Daniel Veillard52b48c72003-04-13 19:53:42 +00003045 memcpy(&str[lenp + 1], value2, lenn);
3046 str[lenn + lenp + 1] = 0;
3047
3048 if (exec->comp->compact != NULL)
3049 ret = xmlRegCompactPushString(exec, exec->comp, str, data);
3050 else
3051 ret = xmlRegExecPushString(exec, str, data);
3052
3053 if (str != buf)
3054 xmlFree(buf);
3055 return(ret);
3056}
3057
Daniel Veillard4255d502002-04-16 15:50:10 +00003058#if 0
3059static int
3060xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
3061 xmlRegTransPtr trans;
3062 xmlRegAtomPtr atom;
3063 int ret;
3064 int codepoint, len;
3065
3066 if (exec == NULL)
3067 return(-1);
3068 if (exec->status != 0)
3069 return(exec->status);
3070
3071 while ((exec->status == 0) &&
3072 ((exec->inputString[exec->index] != 0) ||
3073 (exec->state->type != XML_REGEXP_FINAL_STATE))) {
3074
3075 /*
3076 * End of input on non-terminal state, rollback, however we may
3077 * still have epsilon like transition for counted transitions
3078 * on counters, in that case don't break too early.
3079 */
3080 if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
3081 goto rollback;
3082
3083 exec->transcount = 0;
3084 for (;exec->transno < exec->state->nbTrans;exec->transno++) {
3085 trans = &exec->state->trans[exec->transno];
3086 if (trans->to < 0)
3087 continue;
3088 atom = trans->atom;
3089 ret = 0;
3090 if (trans->count >= 0) {
3091 int count;
3092 xmlRegCounterPtr counter;
3093
3094 /*
3095 * A counted transition.
3096 */
3097
3098 count = exec->counts[trans->count];
3099 counter = &exec->comp->counters[trans->count];
3100#ifdef DEBUG_REGEXP_EXEC
3101 printf("testing count %d: val %d, min %d, max %d\n",
3102 trans->count, count, counter->min, counter->max);
3103#endif
3104 ret = ((count >= counter->min) && (count <= counter->max));
3105 } else if (atom == NULL) {
3106 fprintf(stderr, "epsilon transition left at runtime\n");
3107 exec->status = -2;
3108 break;
3109 } else if (exec->inputString[exec->index] != 0) {
3110 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
3111 ret = xmlRegCheckCharacter(atom, codepoint);
3112 if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
3113 xmlRegStatePtr to = exec->comp->states[trans->to];
3114
3115 /*
3116 * this is a multiple input sequence
3117 */
3118 if (exec->state->nbTrans > exec->transno + 1) {
3119 xmlFARegExecSave(exec);
3120 }
3121 exec->transcount = 1;
3122 do {
3123 /*
3124 * Try to progress as much as possible on the input
3125 */
3126 if (exec->transcount == atom->max) {
3127 break;
3128 }
3129 exec->index += len;
3130 /*
3131 * End of input: stop here
3132 */
3133 if (exec->inputString[exec->index] == 0) {
3134 exec->index -= len;
3135 break;
3136 }
3137 if (exec->transcount >= atom->min) {
3138 int transno = exec->transno;
3139 xmlRegStatePtr state = exec->state;
3140
3141 /*
3142 * The transition is acceptable save it
3143 */
3144 exec->transno = -1; /* trick */
3145 exec->state = to;
3146 xmlFARegExecSave(exec);
3147 exec->transno = transno;
3148 exec->state = state;
3149 }
3150 codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
3151 len);
3152 ret = xmlRegCheckCharacter(atom, codepoint);
3153 exec->transcount++;
3154 } while (ret == 1);
3155 if (exec->transcount < atom->min)
3156 ret = 0;
3157
3158 /*
3159 * If the last check failed but one transition was found
3160 * possible, rollback
3161 */
3162 if (ret < 0)
3163 ret = 0;
3164 if (ret == 0) {
3165 goto rollback;
3166 }
3167 }
3168 }
3169 if (ret == 1) {
3170 if (exec->state->nbTrans > exec->transno + 1) {
3171 xmlFARegExecSave(exec);
3172 }
3173 if (trans->counter >= 0) {
3174#ifdef DEBUG_REGEXP_EXEC
3175 printf("Increasing count %d\n", trans->counter);
3176#endif
3177 exec->counts[trans->counter]++;
3178 }
3179#ifdef DEBUG_REGEXP_EXEC
3180 printf("entering state %d\n", trans->to);
3181#endif
3182 exec->state = exec->comp->states[trans->to];
3183 exec->transno = 0;
3184 if (trans->atom != NULL) {
3185 exec->index += len;
3186 }
3187 goto progress;
3188 } else if (ret < 0) {
3189 exec->status = -4;
3190 break;
3191 }
3192 }
3193 if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
3194rollback:
3195 /*
3196 * Failed to find a way out
3197 */
3198 exec->determinist = 0;
3199 xmlFARegExecRollBack(exec);
3200 }
3201progress:
3202 continue;
3203 }
3204}
3205#endif
3206/************************************************************************
3207 * *
William M. Brackddf71d62004-05-06 04:17:26 +00003208 * Parser for the Schemas Datatype Regular Expressions *
Daniel Veillard4255d502002-04-16 15:50:10 +00003209 * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
3210 * *
3211 ************************************************************************/
3212
3213/**
3214 * xmlFAIsChar:
Daniel Veillard441bc322002-04-20 17:38:48 +00003215 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003216 *
3217 * [10] Char ::= [^.\?*+()|#x5B#x5D]
3218 */
3219static int
3220xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
3221 int cur;
3222 int len;
3223
3224 cur = CUR_SCHAR(ctxt->cur, len);
3225 if ((cur == '.') || (cur == '\\') || (cur == '?') ||
3226 (cur == '*') || (cur == '+') || (cur == '(') ||
3227 (cur == ')') || (cur == '|') || (cur == 0x5B) ||
3228 (cur == 0x5D) || (cur == 0))
3229 return(-1);
3230 return(cur);
3231}
3232
3233/**
3234 * xmlFAParseCharProp:
Daniel Veillard441bc322002-04-20 17:38:48 +00003235 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003236 *
3237 * [27] charProp ::= IsCategory | IsBlock
3238 * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
3239 * Separators | Symbols | Others
3240 * [29] Letters ::= 'L' [ultmo]?
3241 * [30] Marks ::= 'M' [nce]?
3242 * [31] Numbers ::= 'N' [dlo]?
3243 * [32] Punctuation ::= 'P' [cdseifo]?
3244 * [33] Separators ::= 'Z' [slp]?
3245 * [34] Symbols ::= 'S' [mcko]?
3246 * [35] Others ::= 'C' [cfon]?
3247 * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
3248 */
3249static void
3250xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
3251 int cur;
William M. Brack779af002003-08-01 15:55:39 +00003252 xmlRegAtomType type = (xmlRegAtomType) 0;
Daniel Veillard4255d502002-04-16 15:50:10 +00003253 xmlChar *blockName = NULL;
3254
3255 cur = CUR;
3256 if (cur == 'L') {
3257 NEXT;
3258 cur = CUR;
3259 if (cur == 'u') {
3260 NEXT;
3261 type = XML_REGEXP_LETTER_UPPERCASE;
3262 } else if (cur == 'l') {
3263 NEXT;
3264 type = XML_REGEXP_LETTER_LOWERCASE;
3265 } else if (cur == 't') {
3266 NEXT;
3267 type = XML_REGEXP_LETTER_TITLECASE;
3268 } else if (cur == 'm') {
3269 NEXT;
3270 type = XML_REGEXP_LETTER_MODIFIER;
3271 } else if (cur == 'o') {
3272 NEXT;
3273 type = XML_REGEXP_LETTER_OTHERS;
3274 } else {
3275 type = XML_REGEXP_LETTER;
3276 }
3277 } else if (cur == 'M') {
3278 NEXT;
3279 cur = CUR;
3280 if (cur == 'n') {
3281 NEXT;
3282 /* nonspacing */
3283 type = XML_REGEXP_MARK_NONSPACING;
3284 } else if (cur == 'c') {
3285 NEXT;
3286 /* spacing combining */
3287 type = XML_REGEXP_MARK_SPACECOMBINING;
3288 } else if (cur == 'e') {
3289 NEXT;
3290 /* enclosing */
3291 type = XML_REGEXP_MARK_ENCLOSING;
3292 } else {
3293 /* all marks */
3294 type = XML_REGEXP_MARK;
3295 }
3296 } else if (cur == 'N') {
3297 NEXT;
3298 cur = CUR;
3299 if (cur == 'd') {
3300 NEXT;
3301 /* digital */
3302 type = XML_REGEXP_NUMBER_DECIMAL;
3303 } else if (cur == 'l') {
3304 NEXT;
3305 /* letter */
3306 type = XML_REGEXP_NUMBER_LETTER;
3307 } else if (cur == 'o') {
3308 NEXT;
3309 /* other */
3310 type = XML_REGEXP_NUMBER_OTHERS;
3311 } else {
3312 /* all numbers */
3313 type = XML_REGEXP_NUMBER;
3314 }
3315 } else if (cur == 'P') {
3316 NEXT;
3317 cur = CUR;
3318 if (cur == 'c') {
3319 NEXT;
3320 /* connector */
3321 type = XML_REGEXP_PUNCT_CONNECTOR;
3322 } else if (cur == 'd') {
3323 NEXT;
3324 /* dash */
3325 type = XML_REGEXP_PUNCT_DASH;
3326 } else if (cur == 's') {
3327 NEXT;
3328 /* open */
3329 type = XML_REGEXP_PUNCT_OPEN;
3330 } else if (cur == 'e') {
3331 NEXT;
3332 /* close */
3333 type = XML_REGEXP_PUNCT_CLOSE;
3334 } else if (cur == 'i') {
3335 NEXT;
3336 /* initial quote */
3337 type = XML_REGEXP_PUNCT_INITQUOTE;
3338 } else if (cur == 'f') {
3339 NEXT;
3340 /* final quote */
3341 type = XML_REGEXP_PUNCT_FINQUOTE;
3342 } else if (cur == 'o') {
3343 NEXT;
3344 /* other */
3345 type = XML_REGEXP_PUNCT_OTHERS;
3346 } else {
3347 /* all punctuation */
3348 type = XML_REGEXP_PUNCT;
3349 }
3350 } else if (cur == 'Z') {
3351 NEXT;
3352 cur = CUR;
3353 if (cur == 's') {
3354 NEXT;
3355 /* space */
3356 type = XML_REGEXP_SEPAR_SPACE;
3357 } else if (cur == 'l') {
3358 NEXT;
3359 /* line */
3360 type = XML_REGEXP_SEPAR_LINE;
3361 } else if (cur == 'p') {
3362 NEXT;
3363 /* paragraph */
3364 type = XML_REGEXP_SEPAR_PARA;
3365 } else {
3366 /* all separators */
3367 type = XML_REGEXP_SEPAR;
3368 }
3369 } else if (cur == 'S') {
3370 NEXT;
3371 cur = CUR;
3372 if (cur == 'm') {
3373 NEXT;
3374 type = XML_REGEXP_SYMBOL_MATH;
3375 /* math */
3376 } else if (cur == 'c') {
3377 NEXT;
3378 type = XML_REGEXP_SYMBOL_CURRENCY;
3379 /* currency */
3380 } else if (cur == 'k') {
3381 NEXT;
3382 type = XML_REGEXP_SYMBOL_MODIFIER;
3383 /* modifiers */
3384 } else if (cur == 'o') {
3385 NEXT;
3386 type = XML_REGEXP_SYMBOL_OTHERS;
3387 /* other */
3388 } else {
3389 /* all symbols */
3390 type = XML_REGEXP_SYMBOL;
3391 }
3392 } else if (cur == 'C') {
3393 NEXT;
3394 cur = CUR;
3395 if (cur == 'c') {
3396 NEXT;
3397 /* control */
3398 type = XML_REGEXP_OTHER_CONTROL;
3399 } else if (cur == 'f') {
3400 NEXT;
3401 /* format */
3402 type = XML_REGEXP_OTHER_FORMAT;
3403 } else if (cur == 'o') {
3404 NEXT;
3405 /* private use */
3406 type = XML_REGEXP_OTHER_PRIVATE;
3407 } else if (cur == 'n') {
3408 NEXT;
3409 /* not assigned */
3410 type = XML_REGEXP_OTHER_NA;
3411 } else {
3412 /* all others */
3413 type = XML_REGEXP_OTHER;
3414 }
3415 } else if (cur == 'I') {
3416 const xmlChar *start;
3417 NEXT;
3418 cur = CUR;
3419 if (cur != 's') {
3420 ERROR("IsXXXX expected");
3421 return;
3422 }
3423 NEXT;
3424 start = ctxt->cur;
3425 cur = CUR;
3426 if (((cur >= 'a') && (cur <= 'z')) ||
3427 ((cur >= 'A') && (cur <= 'Z')) ||
3428 ((cur >= '0') && (cur <= '9')) ||
3429 (cur == 0x2D)) {
3430 NEXT;
3431 cur = CUR;
3432 while (((cur >= 'a') && (cur <= 'z')) ||
3433 ((cur >= 'A') && (cur <= 'Z')) ||
3434 ((cur >= '0') && (cur <= '9')) ||
3435 (cur == 0x2D)) {
3436 NEXT;
3437 cur = CUR;
3438 }
3439 }
3440 type = XML_REGEXP_BLOCK_NAME;
3441 blockName = xmlStrndup(start, ctxt->cur - start);
3442 } else {
3443 ERROR("Unknown char property");
3444 return;
3445 }
3446 if (ctxt->atom == NULL) {
3447 ctxt->atom = xmlRegNewAtom(ctxt, type);
3448 if (ctxt->atom != NULL)
3449 ctxt->atom->valuep = blockName;
3450 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3451 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3452 type, 0, 0, blockName);
3453 }
3454}
3455
3456/**
3457 * xmlFAParseCharClassEsc:
Daniel Veillard441bc322002-04-20 17:38:48 +00003458 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003459 *
3460 * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
3461 * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
3462 * [25] catEsc ::= '\p{' charProp '}'
3463 * [26] complEsc ::= '\P{' charProp '}'
3464 * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
3465 */
3466static void
3467xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
3468 int cur;
3469
3470 if (CUR == '.') {
3471 if (ctxt->atom == NULL) {
3472 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
3473 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3474 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3475 XML_REGEXP_ANYCHAR, 0, 0, NULL);
3476 }
3477 NEXT;
3478 return;
3479 }
3480 if (CUR != '\\') {
3481 ERROR("Escaped sequence: expecting \\");
3482 return;
3483 }
3484 NEXT;
3485 cur = CUR;
3486 if (cur == 'p') {
3487 NEXT;
3488 if (CUR != '{') {
3489 ERROR("Expecting '{'");
3490 return;
3491 }
3492 NEXT;
3493 xmlFAParseCharProp(ctxt);
3494 if (CUR != '}') {
3495 ERROR("Expecting '}'");
3496 return;
3497 }
3498 NEXT;
3499 } else if (cur == 'P') {
3500 NEXT;
3501 if (CUR != '{') {
3502 ERROR("Expecting '{'");
3503 return;
3504 }
3505 NEXT;
3506 xmlFAParseCharProp(ctxt);
3507 ctxt->atom->neg = 1;
3508 if (CUR != '}') {
3509 ERROR("Expecting '}'");
3510 return;
3511 }
3512 NEXT;
3513 } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
3514 (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
3515 (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
3516 (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
3517 (cur == 0x5E)) {
3518 if (ctxt->atom == NULL) {
3519 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3520 if (ctxt->atom != NULL)
3521 ctxt->atom->codepoint = cur;
3522 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3523 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3524 XML_REGEXP_CHARVAL, cur, cur, NULL);
3525 }
3526 NEXT;
3527 } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
3528 (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
3529 (cur == 'w') || (cur == 'W')) {
Daniel Veillardb509f152002-04-17 16:28:10 +00003530 xmlRegAtomType type = XML_REGEXP_ANYSPACE;
Daniel Veillard4255d502002-04-16 15:50:10 +00003531
3532 switch (cur) {
3533 case 's':
3534 type = XML_REGEXP_ANYSPACE;
3535 break;
3536 case 'S':
3537 type = XML_REGEXP_NOTSPACE;
3538 break;
3539 case 'i':
3540 type = XML_REGEXP_INITNAME;
3541 break;
3542 case 'I':
3543 type = XML_REGEXP_NOTINITNAME;
3544 break;
3545 case 'c':
3546 type = XML_REGEXP_NAMECHAR;
3547 break;
3548 case 'C':
3549 type = XML_REGEXP_NOTNAMECHAR;
3550 break;
3551 case 'd':
3552 type = XML_REGEXP_DECIMAL;
3553 break;
3554 case 'D':
3555 type = XML_REGEXP_NOTDECIMAL;
3556 break;
3557 case 'w':
3558 type = XML_REGEXP_REALCHAR;
3559 break;
3560 case 'W':
3561 type = XML_REGEXP_NOTREALCHAR;
3562 break;
3563 }
3564 NEXT;
3565 if (ctxt->atom == NULL) {
3566 ctxt->atom = xmlRegNewAtom(ctxt, type);
3567 } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
3568 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3569 type, 0, 0, NULL);
3570 }
3571 }
3572}
3573
3574/**
3575 * xmlFAParseCharRef:
Daniel Veillard441bc322002-04-20 17:38:48 +00003576 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003577 *
3578 * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' )
3579 */
3580static int
3581xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) {
3582 int ret = 0, cur;
3583
3584 if ((CUR != '&') || (NXT(1) != '#'))
3585 return(-1);
3586 NEXT;
3587 NEXT;
3588 cur = CUR;
3589 if (cur == 'x') {
3590 NEXT;
3591 cur = CUR;
3592 if (((cur >= '0') && (cur <= '9')) ||
3593 ((cur >= 'a') && (cur <= 'f')) ||
3594 ((cur >= 'A') && (cur <= 'F'))) {
3595 while (((cur >= '0') && (cur <= '9')) ||
3596 ((cur >= 'A') && (cur <= 'F'))) {
3597 if ((cur >= '0') && (cur <= '9'))
3598 ret = ret * 16 + cur - '0';
3599 else if ((cur >= 'a') && (cur <= 'f'))
3600 ret = ret * 16 + 10 + (cur - 'a');
3601 else
3602 ret = ret * 16 + 10 + (cur - 'A');
3603 NEXT;
3604 cur = CUR;
3605 }
3606 } else {
3607 ERROR("Char ref: expecting [0-9A-F]");
3608 return(-1);
3609 }
3610 } else {
3611 if ((cur >= '0') && (cur <= '9')) {
3612 while ((cur >= '0') && (cur <= '9')) {
3613 ret = ret * 10 + cur - '0';
3614 NEXT;
3615 cur = CUR;
3616 }
3617 } else {
3618 ERROR("Char ref: expecting [0-9]");
3619 return(-1);
3620 }
3621 }
3622 if (cur != ';') {
3623 ERROR("Char ref: expecting ';'");
3624 return(-1);
3625 } else {
3626 NEXT;
3627 }
3628 return(ret);
3629}
3630
3631/**
3632 * xmlFAParseCharRange:
Daniel Veillard441bc322002-04-20 17:38:48 +00003633 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003634 *
3635 * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
3636 * [18] seRange ::= charOrEsc '-' charOrEsc
3637 * [20] charOrEsc ::= XmlChar | SingleCharEsc
3638 * [21] XmlChar ::= [^\#x2D#x5B#x5D]
3639 * [22] XmlCharIncDash ::= [^\#x5B#x5D]
3640 */
3641static void
3642xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
William M. Brackdc99df92003-12-27 01:54:25 +00003643 int cur, len;
Daniel Veillard4255d502002-04-16 15:50:10 +00003644 int start = -1;
3645 int end = -1;
3646
3647 if ((CUR == '&') && (NXT(1) == '#')) {
3648 end = start = xmlFAParseCharRef(ctxt);
3649 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3650 XML_REGEXP_CHARVAL, start, end, NULL);
3651 return;
3652 }
3653 cur = CUR;
3654 if (cur == '\\') {
3655 NEXT;
3656 cur = CUR;
3657 switch (cur) {
3658 case 'n': start = 0xA; break;
3659 case 'r': start = 0xD; break;
3660 case 't': start = 0x9; break;
3661 case '\\': case '|': case '.': case '-': case '^': case '?':
3662 case '*': case '+': case '{': case '}': case '(': case ')':
3663 case '[': case ']':
3664 start = cur; break;
3665 default:
3666 ERROR("Invalid escape value");
3667 return;
3668 }
3669 end = start;
William M. Brackdc99df92003-12-27 01:54:25 +00003670 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003671 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003672 end = start = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003673 } else {
3674 ERROR("Expecting a char range");
3675 return;
3676 }
William M. Brackdc99df92003-12-27 01:54:25 +00003677 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003678 if (start == '-') {
3679 return;
3680 }
3681 cur = CUR;
William M. Brack10f1ef42004-03-20 14:51:25 +00003682 if ((cur != '-') || (NXT(1) == ']')) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003683 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3684 XML_REGEXP_CHARVAL, start, end, NULL);
3685 return;
3686 }
3687 NEXT;
3688 cur = CUR;
3689 if (cur == '\\') {
3690 NEXT;
3691 cur = CUR;
3692 switch (cur) {
3693 case 'n': end = 0xA; break;
3694 case 'r': end = 0xD; break;
3695 case 't': end = 0x9; break;
3696 case '\\': case '|': case '.': case '-': case '^': case '?':
3697 case '*': case '+': case '{': case '}': case '(': case ')':
3698 case '[': case ']':
3699 end = cur; break;
3700 default:
3701 ERROR("Invalid escape value");
3702 return;
3703 }
William M. Brackdc99df92003-12-27 01:54:25 +00003704 len = 1;
Daniel Veillard4255d502002-04-16 15:50:10 +00003705 } else if ((cur != 0x5B) && (cur != 0x5D)) {
William M. Brackdc99df92003-12-27 01:54:25 +00003706 end = CUR_SCHAR(ctxt->cur, len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003707 } else {
3708 ERROR("Expecting the end of a char range");
3709 return;
3710 }
William M. Brackdc99df92003-12-27 01:54:25 +00003711 NEXTL(len);
Daniel Veillard4255d502002-04-16 15:50:10 +00003712 /* TODO check that the values are acceptable character ranges for XML */
3713 if (end < start) {
3714 ERROR("End of range is before start of range");
3715 } else {
3716 xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
3717 XML_REGEXP_CHARVAL, start, end, NULL);
3718 }
3719 return;
3720}
3721
3722/**
3723 * xmlFAParsePosCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003724 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003725 *
3726 * [14] posCharGroup ::= ( charRange | charClassEsc )+
3727 */
3728static void
3729xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
3730 do {
3731 if ((CUR == '\\') || (CUR == '.')) {
3732 xmlFAParseCharClassEsc(ctxt);
3733 } else {
3734 xmlFAParseCharRange(ctxt);
3735 }
3736 } while ((CUR != ']') && (CUR != '^') && (CUR != '-') &&
3737 (ctxt->error == 0));
3738}
3739
3740/**
3741 * xmlFAParseCharGroup:
Daniel Veillard441bc322002-04-20 17:38:48 +00003742 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003743 *
3744 * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
3745 * [15] negCharGroup ::= '^' posCharGroup
3746 * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
3747 * [12] charClassExpr ::= '[' charGroup ']'
3748 */
3749static void
3750xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
3751 int n = ctxt->neg;
3752 while ((CUR != ']') && (ctxt->error == 0)) {
3753 if (CUR == '^') {
3754 int neg = ctxt->neg;
3755
3756 NEXT;
3757 ctxt->neg = !ctxt->neg;
3758 xmlFAParsePosCharGroup(ctxt);
3759 ctxt->neg = neg;
William M. Brack10f1ef42004-03-20 14:51:25 +00003760 } else if ((CUR == '-') && (NXT(1) == '[')) {
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003761 int neg = ctxt->neg;
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003762 ctxt->neg = 2;
William M. Brack10f1ef42004-03-20 14:51:25 +00003763 NEXT; /* eat the '-' */
3764 NEXT; /* eat the '[' */
Daniel Veillard4255d502002-04-16 15:50:10 +00003765 xmlFAParseCharGroup(ctxt);
3766 if (CUR == ']') {
3767 NEXT;
3768 } else {
3769 ERROR("charClassExpr: ']' expected");
3770 break;
3771 }
Daniel Veillardf8b9de32003-11-24 14:27:26 +00003772 ctxt->neg = neg;
Daniel Veillard4255d502002-04-16 15:50:10 +00003773 break;
3774 } else if (CUR != ']') {
3775 xmlFAParsePosCharGroup(ctxt);
3776 }
3777 }
3778 ctxt->neg = n;
3779}
3780
3781/**
3782 * xmlFAParseCharClass:
Daniel Veillard441bc322002-04-20 17:38:48 +00003783 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003784 *
3785 * [11] charClass ::= charClassEsc | charClassExpr
3786 * [12] charClassExpr ::= '[' charGroup ']'
3787 */
3788static void
3789xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
3790 if (CUR == '[') {
3791 NEXT;
3792 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
3793 if (ctxt->atom == NULL)
3794 return;
3795 xmlFAParseCharGroup(ctxt);
3796 if (CUR == ']') {
3797 NEXT;
3798 } else {
3799 ERROR("xmlFAParseCharClass: ']' expected");
3800 }
3801 } else {
3802 xmlFAParseCharClassEsc(ctxt);
3803 }
3804}
3805
3806/**
3807 * xmlFAParseQuantExact:
Daniel Veillard441bc322002-04-20 17:38:48 +00003808 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003809 *
3810 * [8] QuantExact ::= [0-9]+
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003811 *
3812 * Returns 0 if success or -1 in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00003813 */
3814static int
3815xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
3816 int ret = 0;
3817 int ok = 0;
3818
3819 while ((CUR >= '0') && (CUR <= '9')) {
3820 ret = ret * 10 + (CUR - '0');
3821 ok = 1;
3822 NEXT;
3823 }
3824 if (ok != 1) {
3825 return(-1);
3826 }
3827 return(ret);
3828}
3829
3830/**
3831 * xmlFAParseQuantifier:
Daniel Veillard441bc322002-04-20 17:38:48 +00003832 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003833 *
3834 * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
3835 * [5] quantity ::= quantRange | quantMin | QuantExact
3836 * [6] quantRange ::= QuantExact ',' QuantExact
3837 * [7] quantMin ::= QuantExact ','
3838 * [8] QuantExact ::= [0-9]+
3839 */
3840static int
3841xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
3842 int cur;
3843
3844 cur = CUR;
3845 if ((cur == '?') || (cur == '*') || (cur == '+')) {
3846 if (ctxt->atom != NULL) {
3847 if (cur == '?')
3848 ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
3849 else if (cur == '*')
3850 ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
3851 else if (cur == '+')
3852 ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
3853 }
3854 NEXT;
3855 return(1);
3856 }
3857 if (cur == '{') {
3858 int min = 0, max = 0;
3859
3860 NEXT;
3861 cur = xmlFAParseQuantExact(ctxt);
3862 if (cur >= 0)
3863 min = cur;
3864 if (CUR == ',') {
3865 NEXT;
Daniel Veillardebe48c62003-12-03 12:12:27 +00003866 if (CUR == '}')
3867 max = INT_MAX;
3868 else {
3869 cur = xmlFAParseQuantExact(ctxt);
3870 if (cur >= 0)
3871 max = cur;
3872 else {
3873 ERROR("Improper quantifier");
3874 }
3875 }
Daniel Veillard4255d502002-04-16 15:50:10 +00003876 }
3877 if (CUR == '}') {
3878 NEXT;
3879 } else {
3880 ERROR("Unterminated quantifier");
3881 }
3882 if (max == 0)
3883 max = min;
3884 if (ctxt->atom != NULL) {
3885 ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
3886 ctxt->atom->min = min;
3887 ctxt->atom->max = max;
3888 }
3889 return(1);
3890 }
3891 return(0);
3892}
3893
3894/**
3895 * xmlFAParseAtom:
Daniel Veillard441bc322002-04-20 17:38:48 +00003896 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003897 *
3898 * [9] atom ::= Char | charClass | ( '(' regExp ')' )
3899 */
3900static int
3901xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
3902 int codepoint, len;
3903
3904 codepoint = xmlFAIsChar(ctxt);
3905 if (codepoint > 0) {
3906 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
3907 if (ctxt->atom == NULL)
3908 return(-1);
3909 codepoint = CUR_SCHAR(ctxt->cur, len);
3910 ctxt->atom->codepoint = codepoint;
3911 NEXTL(len);
3912 return(1);
3913 } else if (CUR == '|') {
3914 return(0);
3915 } else if (CUR == 0) {
3916 return(0);
3917 } else if (CUR == ')') {
3918 return(0);
3919 } else if (CUR == '(') {
3920 xmlRegStatePtr start, oldend;
3921
3922 NEXT;
3923 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
3924 start = ctxt->state;
3925 oldend = ctxt->end;
3926 ctxt->end = NULL;
3927 ctxt->atom = NULL;
3928 xmlFAParseRegExp(ctxt, 0);
3929 if (CUR == ')') {
3930 NEXT;
3931 } else {
3932 ERROR("xmlFAParseAtom: expecting ')'");
3933 }
3934 ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
3935 if (ctxt->atom == NULL)
3936 return(-1);
3937 ctxt->atom->start = start;
3938 ctxt->atom->stop = ctxt->state;
3939 ctxt->end = oldend;
3940 return(1);
3941 } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
3942 xmlFAParseCharClass(ctxt);
3943 return(1);
3944 }
3945 return(0);
3946}
3947
3948/**
3949 * xmlFAParsePiece:
Daniel Veillard441bc322002-04-20 17:38:48 +00003950 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003951 *
3952 * [3] piece ::= atom quantifier?
3953 */
3954static int
3955xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
3956 int ret;
3957
3958 ctxt->atom = NULL;
3959 ret = xmlFAParseAtom(ctxt);
3960 if (ret == 0)
3961 return(0);
3962 if (ctxt->atom == NULL) {
3963 ERROR("internal: no atom generated");
3964 }
3965 xmlFAParseQuantifier(ctxt);
3966 return(1);
3967}
3968
3969/**
3970 * xmlFAParseBranch:
Daniel Veillard441bc322002-04-20 17:38:48 +00003971 * @ctxt: a regexp parser context
Daniel Veillard4255d502002-04-16 15:50:10 +00003972 *
3973 * [2] branch ::= piece*
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003974 8
Daniel Veillard4255d502002-04-16 15:50:10 +00003975 */
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003976static int
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003977xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) {
Daniel Veillard4255d502002-04-16 15:50:10 +00003978 xmlRegStatePtr previous;
Daniel Veillard4255d502002-04-16 15:50:10 +00003979 int ret;
3980
3981 previous = ctxt->state;
3982 ret = xmlFAParsePiece(ctxt);
3983 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003984 if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0)
3985 return(-1);
3986 previous = ctxt->state;
Daniel Veillard4255d502002-04-16 15:50:10 +00003987 ctxt->atom = NULL;
3988 }
3989 while ((ret != 0) && (ctxt->error == 0)) {
3990 ret = xmlFAParsePiece(ctxt);
3991 if (ret != 0) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00003992 if (xmlFAGenerateTransitions(ctxt, previous, NULL,
3993 ctxt->atom) < 0)
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003994 return(-1);
Daniel Veillard4255d502002-04-16 15:50:10 +00003995 previous = ctxt->state;
3996 ctxt->atom = NULL;
3997 }
3998 }
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00003999 return(0);
Daniel Veillard4255d502002-04-16 15:50:10 +00004000}
4001
4002/**
4003 * xmlFAParseRegExp:
Daniel Veillard441bc322002-04-20 17:38:48 +00004004 * @ctxt: a regexp parser context
William M. Brackddf71d62004-05-06 04:17:26 +00004005 * @top: is this the top-level expression ?
Daniel Veillard4255d502002-04-16 15:50:10 +00004006 *
4007 * [1] regExp ::= branch ( '|' branch )*
4008 */
4009static void
4010xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004011 xmlRegStatePtr start, end, oldend, oldstart;
Daniel Veillard4255d502002-04-16 15:50:10 +00004012
4013 oldend = ctxt->end;
4014
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004015 oldstart = ctxt->state;
4016 /* if not top start should have been generated by an epsilon trans */
Daniel Veillard4255d502002-04-16 15:50:10 +00004017 start = ctxt->state;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004018 ctxt->end = NULL;
4019 xmlFAParseBranch(ctxt);
4020 if (top) {
4021#ifdef DEBUG_REGEXP_GRAPH
4022 printf("State %d is final\n", ctxt->state->no);
4023#endif
4024 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4025 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004026 if (CUR != '|') {
4027 ctxt->end = ctxt->state;
4028 return;
4029 }
4030 end = ctxt->state;
4031 while ((CUR == '|') && (ctxt->error == 0)) {
4032 NEXT;
4033 ctxt->state = start;
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004034 ctxt->end = NULL;
4035 xmlFAParseBranch(ctxt);
4036 if (top) {
4037 ctxt->state->type = XML_REGEXP_FINAL_STATE;
4038#ifdef DEBUG_REGEXP_GRAPH
4039 printf("State %d is final\n", ctxt->state->no);
4040#endif
4041 } else {
4042 xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end);
4043 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004044 }
Daniel Veillard2cbf5962004-03-31 15:50:43 +00004045 if (!top) {
4046 ctxt->state = end;
4047 ctxt->end = end;
4048 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004049}
4050
4051/************************************************************************
4052 * *
4053 * The basic API *
4054 * *
4055 ************************************************************************/
4056
4057/**
4058 * xmlRegexpPrint:
4059 * @output: the file for the output debug
4060 * @regexp: the compiled regexp
4061 *
4062 * Print the content of the compiled regular expression
4063 */
4064void
4065xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
4066 int i;
4067
4068 fprintf(output, " regexp: ");
4069 if (regexp == NULL) {
4070 fprintf(output, "NULL\n");
4071 return;
4072 }
4073 fprintf(output, "'%s' ", regexp->string);
4074 fprintf(output, "\n");
4075 fprintf(output, "%d atoms:\n", regexp->nbAtoms);
4076 for (i = 0;i < regexp->nbAtoms; i++) {
4077 fprintf(output, " %02d ", i);
4078 xmlRegPrintAtom(output, regexp->atoms[i]);
4079 }
4080 fprintf(output, "%d states:", regexp->nbStates);
4081 fprintf(output, "\n");
4082 for (i = 0;i < regexp->nbStates; i++) {
4083 xmlRegPrintState(output, regexp->states[i]);
4084 }
4085 fprintf(output, "%d counters:\n", regexp->nbCounters);
4086 for (i = 0;i < regexp->nbCounters; i++) {
4087 fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
4088 regexp->counters[i].max);
4089 }
4090}
4091
4092/**
4093 * xmlRegexpCompile:
4094 * @regexp: a regular expression string
4095 *
4096 * Parses a regular expression conforming to XML Schemas Part 2 Datatype
William M. Brackddf71d62004-05-06 04:17:26 +00004097 * Appendix F and builds an automata suitable for testing strings against
Daniel Veillard4255d502002-04-16 15:50:10 +00004098 * that regular expression
4099 *
4100 * Returns the compiled expression or NULL in case of error
4101 */
4102xmlRegexpPtr
4103xmlRegexpCompile(const xmlChar *regexp) {
4104 xmlRegexpPtr ret;
4105 xmlRegParserCtxtPtr ctxt;
4106
4107 ctxt = xmlRegNewParserCtxt(regexp);
4108 if (ctxt == NULL)
4109 return(NULL);
4110
4111 /* initialize the parser */
4112 ctxt->end = NULL;
4113 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
4114 xmlRegStatePush(ctxt, ctxt->start);
4115
4116 /* parse the expression building an automata */
4117 xmlFAParseRegExp(ctxt, 1);
4118 if (CUR != 0) {
4119 ERROR("xmlFAParseRegExp: extra characters");
4120 }
4121 ctxt->end = ctxt->state;
4122 ctxt->start->type = XML_REGEXP_START_STATE;
4123 ctxt->end->type = XML_REGEXP_FINAL_STATE;
4124
4125 /* remove the Epsilon except for counted transitions */
4126 xmlFAEliminateEpsilonTransitions(ctxt);
4127
4128
4129 if (ctxt->error != 0) {
4130 xmlRegFreeParserCtxt(ctxt);
4131 return(NULL);
4132 }
4133 ret = xmlRegEpxFromParse(ctxt);
4134 xmlRegFreeParserCtxt(ctxt);
4135 return(ret);
4136}
4137
4138/**
4139 * xmlRegexpExec:
4140 * @comp: the compiled regular expression
4141 * @content: the value to check against the regular expression
4142 *
William M. Brackddf71d62004-05-06 04:17:26 +00004143 * Check if the regular expression generates the value
Daniel Veillard4255d502002-04-16 15:50:10 +00004144 *
William M. Brackddf71d62004-05-06 04:17:26 +00004145 * Returns 1 if it matches, 0 if not and a negative value in case of error
Daniel Veillard4255d502002-04-16 15:50:10 +00004146 */
4147int
4148xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
4149 if ((comp == NULL) || (content == NULL))
4150 return(-1);
4151 return(xmlFARegExec(comp, content));
4152}
4153
4154/**
Daniel Veillard23e73572002-09-19 19:56:43 +00004155 * xmlRegexpIsDeterminist:
4156 * @comp: the compiled regular expression
4157 *
4158 * Check if the regular expression is determinist
4159 *
William M. Brackddf71d62004-05-06 04:17:26 +00004160 * Returns 1 if it yes, 0 if not and a negative value in case of error
Daniel Veillard23e73572002-09-19 19:56:43 +00004161 */
4162int
4163xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
4164 xmlAutomataPtr am;
4165 int ret;
4166
4167 if (comp == NULL)
4168 return(-1);
4169 if (comp->determinist != -1)
4170 return(comp->determinist);
4171
4172 am = xmlNewAutomata();
Daniel Veillardbd9afb52002-09-25 22:25:35 +00004173 if (am->states != NULL) {
4174 int i;
4175
4176 for (i = 0;i < am->nbStates;i++)
4177 xmlRegFreeState(am->states[i]);
4178 xmlFree(am->states);
4179 }
Daniel Veillard23e73572002-09-19 19:56:43 +00004180 am->nbAtoms = comp->nbAtoms;
4181 am->atoms = comp->atoms;
4182 am->nbStates = comp->nbStates;
4183 am->states = comp->states;
4184 am->determinist = -1;
4185 ret = xmlFAComputesDeterminism(am);
4186 am->atoms = NULL;
4187 am->states = NULL;
4188 xmlFreeAutomata(am);
4189 return(ret);
4190}
4191
4192/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004193 * xmlRegFreeRegexp:
4194 * @regexp: the regexp
4195 *
4196 * Free a regexp
4197 */
4198void
4199xmlRegFreeRegexp(xmlRegexpPtr regexp) {
4200 int i;
4201 if (regexp == NULL)
4202 return;
4203
4204 if (regexp->string != NULL)
4205 xmlFree(regexp->string);
4206 if (regexp->states != NULL) {
4207 for (i = 0;i < regexp->nbStates;i++)
4208 xmlRegFreeState(regexp->states[i]);
4209 xmlFree(regexp->states);
4210 }
4211 if (regexp->atoms != NULL) {
4212 for (i = 0;i < regexp->nbAtoms;i++)
4213 xmlRegFreeAtom(regexp->atoms[i]);
4214 xmlFree(regexp->atoms);
4215 }
4216 if (regexp->counters != NULL)
4217 xmlFree(regexp->counters);
Daniel Veillard23e73572002-09-19 19:56:43 +00004218 if (regexp->compact != NULL)
4219 xmlFree(regexp->compact);
Daniel Veillard118aed72002-09-24 14:13:13 +00004220 if (regexp->transdata != NULL)
4221 xmlFree(regexp->transdata);
Daniel Veillard23e73572002-09-19 19:56:43 +00004222 if (regexp->stringMap != NULL) {
4223 for (i = 0; i < regexp->nbstrings;i++)
4224 xmlFree(regexp->stringMap[i]);
4225 xmlFree(regexp->stringMap);
4226 }
4227
Daniel Veillard4255d502002-04-16 15:50:10 +00004228 xmlFree(regexp);
4229}
4230
4231#ifdef LIBXML_AUTOMATA_ENABLED
4232/************************************************************************
4233 * *
4234 * The Automata interface *
4235 * *
4236 ************************************************************************/
4237
4238/**
4239 * xmlNewAutomata:
4240 *
4241 * Create a new automata
4242 *
4243 * Returns the new object or NULL in case of failure
4244 */
4245xmlAutomataPtr
4246xmlNewAutomata(void) {
4247 xmlAutomataPtr ctxt;
4248
4249 ctxt = xmlRegNewParserCtxt(NULL);
4250 if (ctxt == NULL)
4251 return(NULL);
4252
4253 /* initialize the parser */
4254 ctxt->end = NULL;
4255 ctxt->start = ctxt->state = xmlRegNewState(ctxt);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004256 if (ctxt->start == NULL) {
4257 xmlFreeAutomata(ctxt);
4258 return(NULL);
4259 }
4260 if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
4261 xmlRegFreeState(ctxt->start);
4262 xmlFreeAutomata(ctxt);
4263 return(NULL);
4264 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004265
4266 return(ctxt);
4267}
4268
4269/**
4270 * xmlFreeAutomata:
4271 * @am: an automata
4272 *
4273 * Free an automata
4274 */
4275void
4276xmlFreeAutomata(xmlAutomataPtr am) {
4277 if (am == NULL)
4278 return;
4279 xmlRegFreeParserCtxt(am);
4280}
4281
4282/**
4283 * xmlAutomataGetInitState:
4284 * @am: an automata
4285 *
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004286 * Initial state lookup
4287 *
Daniel Veillard4255d502002-04-16 15:50:10 +00004288 * Returns the initial state of the automata
4289 */
4290xmlAutomataStatePtr
4291xmlAutomataGetInitState(xmlAutomataPtr am) {
4292 if (am == NULL)
4293 return(NULL);
4294 return(am->start);
4295}
4296
4297/**
4298 * xmlAutomataSetFinalState:
4299 * @am: an automata
4300 * @state: a state in this automata
4301 *
4302 * Makes that state a final state
4303 *
4304 * Returns 0 or -1 in case of error
4305 */
4306int
4307xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
4308 if ((am == NULL) || (state == NULL))
4309 return(-1);
4310 state->type = XML_REGEXP_FINAL_STATE;
4311 return(0);
4312}
4313
4314/**
4315 * xmlAutomataNewTransition:
4316 * @am: an automata
4317 * @from: the starting point of the transition
4318 * @to: the target point of the transition or NULL
4319 * @token: the input string associated to that transition
4320 * @data: data passed to the callback function if the transition is activated
4321 *
William M. Brackddf71d62004-05-06 04:17:26 +00004322 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004323 * and then adds a transition from the @from state to the target state
4324 * activated by the value of @token
4325 *
4326 * Returns the target state or NULL in case of error
4327 */
4328xmlAutomataStatePtr
4329xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
4330 xmlAutomataStatePtr to, const xmlChar *token,
4331 void *data) {
4332 xmlRegAtomPtr atom;
4333
4334 if ((am == NULL) || (from == NULL) || (token == NULL))
4335 return(NULL);
4336 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004337 if (atom == NULL)
4338 return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004339 atom->data = data;
4340 if (atom == NULL)
4341 return(NULL);
4342 atom->valuep = xmlStrdup(token);
4343
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004344 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4345 xmlRegFreeAtom(atom);
4346 return(NULL);
4347 }
Daniel Veillard4255d502002-04-16 15:50:10 +00004348 if (to == NULL)
4349 return(am->state);
4350 return(to);
4351}
4352
4353/**
Daniel Veillard52b48c72003-04-13 19:53:42 +00004354 * xmlAutomataNewTransition2:
4355 * @am: an automata
4356 * @from: the starting point of the transition
4357 * @to: the target point of the transition or NULL
4358 * @token: the first input string associated to that transition
4359 * @token2: the second input string associated to that transition
4360 * @data: data passed to the callback function if the transition is activated
4361 *
William M. Brackddf71d62004-05-06 04:17:26 +00004362 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard52b48c72003-04-13 19:53:42 +00004363 * and then adds a transition from the @from state to the target state
4364 * activated by the value of @token
4365 *
4366 * Returns the target state or NULL in case of error
4367 */
4368xmlAutomataStatePtr
4369xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
4370 xmlAutomataStatePtr to, const xmlChar *token,
4371 const xmlChar *token2, void *data) {
4372 xmlRegAtomPtr atom;
4373
4374 if ((am == NULL) || (from == NULL) || (token == NULL))
4375 return(NULL);
4376 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4377 atom->data = data;
4378 if (atom == NULL)
4379 return(NULL);
4380 if ((token2 == NULL) || (*token2 == 0)) {
4381 atom->valuep = xmlStrdup(token);
4382 } else {
4383 int lenn, lenp;
4384 xmlChar *str;
4385
4386 lenn = strlen((char *) token2);
4387 lenp = strlen((char *) token);
4388
Daniel Veillard3c908dc2003-04-19 00:07:51 +00004389 str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
Daniel Veillard52b48c72003-04-13 19:53:42 +00004390 if (str == NULL) {
4391 xmlRegFreeAtom(atom);
4392 return(NULL);
4393 }
4394 memcpy(&str[0], token, lenp);
4395 str[lenp] = '|';
4396 memcpy(&str[lenp + 1], token2, lenn);
4397 str[lenn + lenp + 1] = 0;
4398
4399 atom->valuep = str;
4400 }
4401
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004402 if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
4403 xmlRegFreeAtom(atom);
4404 return(NULL);
4405 }
Daniel Veillard52b48c72003-04-13 19:53:42 +00004406 if (to == NULL)
4407 return(am->state);
4408 return(to);
4409}
4410
4411/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004412 * xmlAutomataNewCountTrans:
4413 * @am: an automata
4414 * @from: the starting point of the transition
4415 * @to: the target point of the transition or NULL
4416 * @token: the input string associated to that transition
4417 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004418 * @max: the maximum successive occurences of token
4419 * @data: data associated to the transition
Daniel Veillard4255d502002-04-16 15:50:10 +00004420 *
William M. Brackddf71d62004-05-06 04:17:26 +00004421 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard4255d502002-04-16 15:50:10 +00004422 * and then adds a transition from the @from state to the target state
4423 * activated by a succession of input of value @token and whose number
4424 * is between @min and @max
4425 *
4426 * Returns the target state or NULL in case of error
4427 */
4428xmlAutomataStatePtr
4429xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4430 xmlAutomataStatePtr to, const xmlChar *token,
4431 int min, int max, void *data) {
4432 xmlRegAtomPtr atom;
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004433 int counter;
Daniel Veillard4255d502002-04-16 15:50:10 +00004434
4435 if ((am == NULL) || (from == NULL) || (token == NULL))
4436 return(NULL);
4437 if (min < 0)
4438 return(NULL);
4439 if ((max < min) || (max < 1))
4440 return(NULL);
4441 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4442 if (atom == NULL)
4443 return(NULL);
4444 atom->valuep = xmlStrdup(token);
4445 atom->data = data;
4446 if (min == 0)
4447 atom->min = 1;
4448 else
4449 atom->min = min;
4450 atom->max = max;
4451
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004452 /*
4453 * associate a counter to the transition.
4454 */
4455 counter = xmlRegGetCounter(am);
4456 am->counters[counter].min = min;
4457 am->counters[counter].max = max;
4458
4459 /* xmlFAGenerateTransitions(am, from, to, atom); */
4460 if (to == NULL) {
4461 to = xmlRegNewState(am);
4462 xmlRegStatePush(am, to);
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004463 }
Daniel Veillard0ddb21c2004-02-12 12:43:49 +00004464 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4465 xmlRegAtomPush(am, atom);
4466 am->state = to;
4467
Daniel Veillard4255d502002-04-16 15:50:10 +00004468 if (to == NULL)
4469 to = am->state;
4470 if (to == NULL)
4471 return(NULL);
4472 if (min == 0)
4473 xmlFAGenerateEpsilonTransition(am, from, to);
4474 return(to);
4475}
4476
4477/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004478 * xmlAutomataNewOnceTrans:
4479 * @am: an automata
4480 * @from: the starting point of the transition
4481 * @to: the target point of the transition or NULL
4482 * @token: the input string associated to that transition
4483 * @min: the minimum successive occurences of token
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004484 * @max: the maximum successive occurences of token
4485 * @data: data associated to the transition
Daniel Veillard7646b182002-04-20 06:41:40 +00004486 *
William M. Brackddf71d62004-05-06 04:17:26 +00004487 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00004488 * and then adds a transition from the @from state to the target state
4489 * activated by a succession of input of value @token and whose number
William M. Brackddf71d62004-05-06 04:17:26 +00004490 * is between @min and @max, moreover that transition can only be crossed
Daniel Veillard7646b182002-04-20 06:41:40 +00004491 * once.
4492 *
4493 * Returns the target state or NULL in case of error
4494 */
4495xmlAutomataStatePtr
4496xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4497 xmlAutomataStatePtr to, const xmlChar *token,
4498 int min, int max, void *data) {
4499 xmlRegAtomPtr atom;
4500 int counter;
4501
4502 if ((am == NULL) || (from == NULL) || (token == NULL))
4503 return(NULL);
4504 if (min < 1)
4505 return(NULL);
4506 if ((max < min) || (max < 1))
4507 return(NULL);
4508 atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
4509 if (atom == NULL)
4510 return(NULL);
4511 atom->valuep = xmlStrdup(token);
4512 atom->data = data;
4513 atom->quant = XML_REGEXP_QUANT_ONCEONLY;
4514 if (min == 0)
4515 atom->min = 1;
4516 else
4517 atom->min = min;
4518 atom->max = max;
4519 /*
4520 * associate a counter to the transition.
4521 */
4522 counter = xmlRegGetCounter(am);
4523 am->counters[counter].min = 1;
4524 am->counters[counter].max = 1;
4525
4526 /* xmlFAGenerateTransitions(am, from, to, atom); */
4527 if (to == NULL) {
4528 to = xmlRegNewState(am);
4529 xmlRegStatePush(am, to);
4530 }
4531 xmlRegStateAddTrans(am, from, atom, to, counter, -1);
4532 xmlRegAtomPush(am, atom);
4533 am->state = to;
Daniel Veillard7646b182002-04-20 06:41:40 +00004534 return(to);
4535}
4536
4537/**
Daniel Veillard4255d502002-04-16 15:50:10 +00004538 * xmlAutomataNewState:
4539 * @am: an automata
4540 *
4541 * Create a new disconnected state in the automata
4542 *
4543 * Returns the new state or NULL in case of error
4544 */
4545xmlAutomataStatePtr
4546xmlAutomataNewState(xmlAutomataPtr am) {
4547 xmlAutomataStatePtr to;
4548
4549 if (am == NULL)
4550 return(NULL);
4551 to = xmlRegNewState(am);
4552 xmlRegStatePush(am, to);
4553 return(to);
4554}
4555
4556/**
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004557 * xmlAutomataNewEpsilon:
Daniel Veillard4255d502002-04-16 15:50:10 +00004558 * @am: an automata
4559 * @from: the starting point of the transition
4560 * @to: the target point of the transition or NULL
4561 *
William M. Brackddf71d62004-05-06 04:17:26 +00004562 * If @to is NULL, this creates first a new target state in the automata
4563 * and then adds an epsilon transition from the @from state to the
Daniel Veillard4255d502002-04-16 15:50:10 +00004564 * target state
4565 *
4566 * Returns the target state or NULL in case of error
4567 */
4568xmlAutomataStatePtr
4569xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
4570 xmlAutomataStatePtr to) {
4571 if ((am == NULL) || (from == NULL))
4572 return(NULL);
4573 xmlFAGenerateEpsilonTransition(am, from, to);
4574 if (to == NULL)
4575 return(am->state);
4576 return(to);
4577}
4578
Daniel Veillardb509f152002-04-17 16:28:10 +00004579/**
Daniel Veillard7646b182002-04-20 06:41:40 +00004580 * xmlAutomataNewAllTrans:
4581 * @am: an automata
4582 * @from: the starting point of the transition
4583 * @to: the target point of the transition or NULL
Daniel Veillarda9b66d02002-12-11 14:23:49 +00004584 * @lax: allow to transition if not all all transitions have been activated
Daniel Veillard7646b182002-04-20 06:41:40 +00004585 *
William M. Brackddf71d62004-05-06 04:17:26 +00004586 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillard7646b182002-04-20 06:41:40 +00004587 * and then adds a an ALL transition from the @from state to the
4588 * target state. That transition is an epsilon transition allowed only when
4589 * all transitions from the @from node have been activated.
4590 *
4591 * Returns the target state or NULL in case of error
4592 */
4593xmlAutomataStatePtr
4594xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
Daniel Veillard441bc322002-04-20 17:38:48 +00004595 xmlAutomataStatePtr to, int lax) {
Daniel Veillard7646b182002-04-20 06:41:40 +00004596 if ((am == NULL) || (from == NULL))
4597 return(NULL);
Daniel Veillard441bc322002-04-20 17:38:48 +00004598 xmlFAGenerateAllTransition(am, from, to, lax);
Daniel Veillard7646b182002-04-20 06:41:40 +00004599 if (to == NULL)
4600 return(am->state);
4601 return(to);
4602}
4603
4604/**
Daniel Veillardb509f152002-04-17 16:28:10 +00004605 * xmlAutomataNewCounter:
4606 * @am: an automata
4607 * @min: the minimal value on the counter
4608 * @max: the maximal value on the counter
4609 *
4610 * Create a new counter
4611 *
4612 * Returns the counter number or -1 in case of error
4613 */
4614int
4615xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
4616 int ret;
4617
4618 if (am == NULL)
4619 return(-1);
4620
4621 ret = xmlRegGetCounter(am);
4622 if (ret < 0)
4623 return(-1);
4624 am->counters[ret].min = min;
4625 am->counters[ret].max = max;
4626 return(ret);
4627}
4628
4629/**
4630 * xmlAutomataNewCountedTrans:
4631 * @am: an automata
4632 * @from: the starting point of the transition
4633 * @to: the target point of the transition or NULL
4634 * @counter: the counter associated to that transition
4635 *
William M. Brackddf71d62004-05-06 04:17:26 +00004636 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00004637 * and then adds an epsilon transition from the @from state to the target state
4638 * which will increment the counter provided
4639 *
4640 * Returns the target state or NULL in case of error
4641 */
4642xmlAutomataStatePtr
4643xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4644 xmlAutomataStatePtr to, int counter) {
4645 if ((am == NULL) || (from == NULL) || (counter < 0))
4646 return(NULL);
4647 xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
4648 if (to == NULL)
4649 return(am->state);
4650 return(to);
4651}
4652
4653/**
4654 * xmlAutomataNewCounterTrans:
4655 * @am: an automata
4656 * @from: the starting point of the transition
4657 * @to: the target point of the transition or NULL
4658 * @counter: the counter associated to that transition
4659 *
William M. Brackddf71d62004-05-06 04:17:26 +00004660 * If @to is NULL, this creates first a new target state in the automata
Daniel Veillardb509f152002-04-17 16:28:10 +00004661 * and then adds an epsilon transition from the @from state to the target state
4662 * which will be allowed only if the counter is within the right range.
4663 *
4664 * Returns the target state or NULL in case of error
4665 */
4666xmlAutomataStatePtr
4667xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
4668 xmlAutomataStatePtr to, int counter) {
4669 if ((am == NULL) || (from == NULL) || (counter < 0))
4670 return(NULL);
4671 xmlFAGenerateCountedTransition(am, from, to, counter);
4672 if (to == NULL)
4673 return(am->state);
4674 return(to);
4675}
Daniel Veillard4255d502002-04-16 15:50:10 +00004676
4677/**
4678 * xmlAutomataCompile:
4679 * @am: an automata
4680 *
4681 * Compile the automata into a Reg Exp ready for being executed.
4682 * The automata should be free after this point.
4683 *
4684 * Returns the compiled regexp or NULL in case of error
4685 */
4686xmlRegexpPtr
4687xmlAutomataCompile(xmlAutomataPtr am) {
4688 xmlRegexpPtr ret;
4689
Daniel Veillarda76fe5c2003-04-24 16:06:47 +00004690 if ((am == NULL) || (am->error != 0)) return(NULL);
Daniel Veillard4255d502002-04-16 15:50:10 +00004691 xmlFAEliminateEpsilonTransitions(am);
Daniel Veillard23e73572002-09-19 19:56:43 +00004692 /* xmlFAComputesDeterminism(am); */
Daniel Veillard4255d502002-04-16 15:50:10 +00004693 ret = xmlRegEpxFromParse(am);
4694
4695 return(ret);
4696}
Daniel Veillarde19fc232002-04-22 16:01:24 +00004697
4698/**
4699 * xmlAutomataIsDeterminist:
4700 * @am: an automata
4701 *
4702 * Checks if an automata is determinist.
4703 *
4704 * Returns 1 if true, 0 if not, and -1 in case of error
4705 */
4706int
4707xmlAutomataIsDeterminist(xmlAutomataPtr am) {
4708 int ret;
4709
4710 if (am == NULL)
4711 return(-1);
4712
4713 ret = xmlFAComputesDeterminism(am);
4714 return(ret);
4715}
Daniel Veillard4255d502002-04-16 15:50:10 +00004716#endif /* LIBXML_AUTOMATA_ENABLED */
4717#endif /* LIBXML_REGEXP_ENABLED */