1 | /*
|
---|
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/schemas mechanisms now available in
|
---|
6 | * XML related specifications these include:
|
---|
7 | * - XML-1.0 DTD validation
|
---|
8 | * - XML Schemas structure part 1
|
---|
9 | * - XML Schemas Datatypes part 2 especially Appendix F
|
---|
10 | * - RELAX-NG/TREX i.e. the counter proposal
|
---|
11 | *
|
---|
12 | * See Copyright for the status of this software.
|
---|
13 | *
|
---|
14 | * Daniel Veillard <[email protected]>
|
---|
15 | */
|
---|
16 |
|
---|
17 | #define IN_LIBXML
|
---|
18 | #include "libxml.h"
|
---|
19 |
|
---|
20 | #ifdef LIBXML_REGEXP_ENABLED
|
---|
21 |
|
---|
22 | /* #define DEBUG_ERR */
|
---|
23 |
|
---|
24 | #include <stdio.h>
|
---|
25 | #include <string.h>
|
---|
26 | #ifdef HAVE_LIMITS_H
|
---|
27 | #include <limits.h>
|
---|
28 | #endif
|
---|
29 | #ifdef HAVE_STDINT_H
|
---|
30 | #include <stdint.h>
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | #include <libxml/tree.h>
|
---|
34 | #include <libxml/parserInternals.h>
|
---|
35 | #include <libxml/xmlregexp.h>
|
---|
36 | #include <libxml/xmlautomata.h>
|
---|
37 | #include <libxml/xmlunicode.h>
|
---|
38 |
|
---|
39 | #ifndef INT_MAX
|
---|
40 | #define INT_MAX 123456789 /* easy to flag and big enough for our needs */
|
---|
41 | #endif
|
---|
42 | #ifndef SIZE_MAX
|
---|
43 | #define SIZE_MAX ((size_t) -1)
|
---|
44 | #endif
|
---|
45 |
|
---|
46 | /* #define DEBUG_REGEXP_GRAPH */
|
---|
47 | /* #define DEBUG_REGEXP_EXEC */
|
---|
48 | /* #define DEBUG_PUSH */
|
---|
49 | /* #define DEBUG_COMPACTION */
|
---|
50 |
|
---|
51 | #define MAX_PUSH 10000000
|
---|
52 |
|
---|
53 | #ifdef ERROR
|
---|
54 | #undef ERROR
|
---|
55 | #endif
|
---|
56 | #define ERROR(str) \
|
---|
57 | ctxt->error = XML_REGEXP_COMPILE_ERROR; \
|
---|
58 | xmlRegexpErrCompile(ctxt, str);
|
---|
59 | #define NEXT ctxt->cur++
|
---|
60 | #define CUR (*(ctxt->cur))
|
---|
61 | #define NXT(index) (ctxt->cur[index])
|
---|
62 |
|
---|
63 | #define CUR_SCHAR(s, l) xmlStringCurrentChar(NULL, s, &l)
|
---|
64 | #define NEXTL(l) ctxt->cur += l;
|
---|
65 | #define XML_REG_STRING_SEPARATOR '|'
|
---|
66 | /*
|
---|
67 | * Need PREV to check on a '-' within a Character Group. May only be used
|
---|
68 | * when it's guaranteed that cur is not at the beginning of ctxt->string!
|
---|
69 | */
|
---|
70 | #define PREV (ctxt->cur[-1])
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * TODO:
|
---|
74 | *
|
---|
75 | * macro to flag unimplemented blocks
|
---|
76 | */
|
---|
77 | #define TODO \
|
---|
78 | xmlGenericError(xmlGenericErrorContext, \
|
---|
79 | "Unimplemented block at %s:%d\n", \
|
---|
80 | __FILE__, __LINE__);
|
---|
81 |
|
---|
82 | /************************************************************************
|
---|
83 | * *
|
---|
84 | * Datatypes and structures *
|
---|
85 | * *
|
---|
86 | ************************************************************************/
|
---|
87 |
|
---|
88 | /*
|
---|
89 | * Note: the order of the enums below is significant, do not shuffle
|
---|
90 | */
|
---|
91 | typedef enum {
|
---|
92 | XML_REGEXP_EPSILON = 1,
|
---|
93 | XML_REGEXP_CHARVAL,
|
---|
94 | XML_REGEXP_RANGES,
|
---|
95 | XML_REGEXP_SUBREG, /* used for () sub regexps */
|
---|
96 | XML_REGEXP_STRING,
|
---|
97 | XML_REGEXP_ANYCHAR, /* . */
|
---|
98 | XML_REGEXP_ANYSPACE, /* \s */
|
---|
99 | XML_REGEXP_NOTSPACE, /* \S */
|
---|
100 | XML_REGEXP_INITNAME, /* \l */
|
---|
101 | XML_REGEXP_NOTINITNAME, /* \L */
|
---|
102 | XML_REGEXP_NAMECHAR, /* \c */
|
---|
103 | XML_REGEXP_NOTNAMECHAR, /* \C */
|
---|
104 | XML_REGEXP_DECIMAL, /* \d */
|
---|
105 | XML_REGEXP_NOTDECIMAL, /* \D */
|
---|
106 | XML_REGEXP_REALCHAR, /* \w */
|
---|
107 | XML_REGEXP_NOTREALCHAR, /* \W */
|
---|
108 | XML_REGEXP_LETTER = 100,
|
---|
109 | XML_REGEXP_LETTER_UPPERCASE,
|
---|
110 | XML_REGEXP_LETTER_LOWERCASE,
|
---|
111 | XML_REGEXP_LETTER_TITLECASE,
|
---|
112 | XML_REGEXP_LETTER_MODIFIER,
|
---|
113 | XML_REGEXP_LETTER_OTHERS,
|
---|
114 | XML_REGEXP_MARK,
|
---|
115 | XML_REGEXP_MARK_NONSPACING,
|
---|
116 | XML_REGEXP_MARK_SPACECOMBINING,
|
---|
117 | XML_REGEXP_MARK_ENCLOSING,
|
---|
118 | XML_REGEXP_NUMBER,
|
---|
119 | XML_REGEXP_NUMBER_DECIMAL,
|
---|
120 | XML_REGEXP_NUMBER_LETTER,
|
---|
121 | XML_REGEXP_NUMBER_OTHERS,
|
---|
122 | XML_REGEXP_PUNCT,
|
---|
123 | XML_REGEXP_PUNCT_CONNECTOR,
|
---|
124 | XML_REGEXP_PUNCT_DASH,
|
---|
125 | XML_REGEXP_PUNCT_OPEN,
|
---|
126 | XML_REGEXP_PUNCT_CLOSE,
|
---|
127 | XML_REGEXP_PUNCT_INITQUOTE,
|
---|
128 | XML_REGEXP_PUNCT_FINQUOTE,
|
---|
129 | XML_REGEXP_PUNCT_OTHERS,
|
---|
130 | XML_REGEXP_SEPAR,
|
---|
131 | XML_REGEXP_SEPAR_SPACE,
|
---|
132 | XML_REGEXP_SEPAR_LINE,
|
---|
133 | XML_REGEXP_SEPAR_PARA,
|
---|
134 | XML_REGEXP_SYMBOL,
|
---|
135 | XML_REGEXP_SYMBOL_MATH,
|
---|
136 | XML_REGEXP_SYMBOL_CURRENCY,
|
---|
137 | XML_REGEXP_SYMBOL_MODIFIER,
|
---|
138 | XML_REGEXP_SYMBOL_OTHERS,
|
---|
139 | XML_REGEXP_OTHER,
|
---|
140 | XML_REGEXP_OTHER_CONTROL,
|
---|
141 | XML_REGEXP_OTHER_FORMAT,
|
---|
142 | XML_REGEXP_OTHER_PRIVATE,
|
---|
143 | XML_REGEXP_OTHER_NA,
|
---|
144 | XML_REGEXP_BLOCK_NAME
|
---|
145 | } xmlRegAtomType;
|
---|
146 |
|
---|
147 | typedef enum {
|
---|
148 | XML_REGEXP_QUANT_EPSILON = 1,
|
---|
149 | XML_REGEXP_QUANT_ONCE,
|
---|
150 | XML_REGEXP_QUANT_OPT,
|
---|
151 | XML_REGEXP_QUANT_MULT,
|
---|
152 | XML_REGEXP_QUANT_PLUS,
|
---|
153 | XML_REGEXP_QUANT_ONCEONLY,
|
---|
154 | XML_REGEXP_QUANT_ALL,
|
---|
155 | XML_REGEXP_QUANT_RANGE
|
---|
156 | } xmlRegQuantType;
|
---|
157 |
|
---|
158 | typedef enum {
|
---|
159 | XML_REGEXP_START_STATE = 1,
|
---|
160 | XML_REGEXP_FINAL_STATE,
|
---|
161 | XML_REGEXP_TRANS_STATE,
|
---|
162 | XML_REGEXP_SINK_STATE,
|
---|
163 | XML_REGEXP_UNREACH_STATE
|
---|
164 | } xmlRegStateType;
|
---|
165 |
|
---|
166 | typedef enum {
|
---|
167 | XML_REGEXP_MARK_NORMAL = 0,
|
---|
168 | XML_REGEXP_MARK_START,
|
---|
169 | XML_REGEXP_MARK_VISITED
|
---|
170 | } xmlRegMarkedType;
|
---|
171 |
|
---|
172 | typedef struct _xmlRegRange xmlRegRange;
|
---|
173 | typedef xmlRegRange *xmlRegRangePtr;
|
---|
174 |
|
---|
175 | struct _xmlRegRange {
|
---|
176 | int neg; /* 0 normal, 1 not, 2 exclude */
|
---|
177 | xmlRegAtomType type;
|
---|
178 | int start;
|
---|
179 | int end;
|
---|
180 | xmlChar *blockName;
|
---|
181 | };
|
---|
182 |
|
---|
183 | typedef struct _xmlRegAtom xmlRegAtom;
|
---|
184 | typedef xmlRegAtom *xmlRegAtomPtr;
|
---|
185 |
|
---|
186 | typedef struct _xmlAutomataState xmlRegState;
|
---|
187 | typedef xmlRegState *xmlRegStatePtr;
|
---|
188 |
|
---|
189 | struct _xmlRegAtom {
|
---|
190 | int no;
|
---|
191 | xmlRegAtomType type;
|
---|
192 | xmlRegQuantType quant;
|
---|
193 | int min;
|
---|
194 | int max;
|
---|
195 |
|
---|
196 | void *valuep;
|
---|
197 | void *valuep2;
|
---|
198 | int neg;
|
---|
199 | int codepoint;
|
---|
200 | xmlRegStatePtr start;
|
---|
201 | xmlRegStatePtr start0;
|
---|
202 | xmlRegStatePtr stop;
|
---|
203 | int maxRanges;
|
---|
204 | int nbRanges;
|
---|
205 | xmlRegRangePtr *ranges;
|
---|
206 | void *data;
|
---|
207 | };
|
---|
208 |
|
---|
209 | typedef struct _xmlRegCounter xmlRegCounter;
|
---|
210 | typedef xmlRegCounter *xmlRegCounterPtr;
|
---|
211 |
|
---|
212 | struct _xmlRegCounter {
|
---|
213 | int min;
|
---|
214 | int max;
|
---|
215 | };
|
---|
216 |
|
---|
217 | typedef struct _xmlRegTrans xmlRegTrans;
|
---|
218 | typedef xmlRegTrans *xmlRegTransPtr;
|
---|
219 |
|
---|
220 | struct _xmlRegTrans {
|
---|
221 | xmlRegAtomPtr atom;
|
---|
222 | int to;
|
---|
223 | int counter;
|
---|
224 | int count;
|
---|
225 | int nd;
|
---|
226 | };
|
---|
227 |
|
---|
228 | struct _xmlAutomataState {
|
---|
229 | xmlRegStateType type;
|
---|
230 | xmlRegMarkedType mark;
|
---|
231 | xmlRegMarkedType markd;
|
---|
232 | xmlRegMarkedType reached;
|
---|
233 | int no;
|
---|
234 | int maxTrans;
|
---|
235 | int nbTrans;
|
---|
236 | xmlRegTrans *trans;
|
---|
237 | /* knowing states pointing to us can speed things up */
|
---|
238 | int maxTransTo;
|
---|
239 | int nbTransTo;
|
---|
240 | int *transTo;
|
---|
241 | };
|
---|
242 |
|
---|
243 | typedef struct _xmlAutomata xmlRegParserCtxt;
|
---|
244 | typedef xmlRegParserCtxt *xmlRegParserCtxtPtr;
|
---|
245 |
|
---|
246 | #define AM_AUTOMATA_RNG 1
|
---|
247 |
|
---|
248 | struct _xmlAutomata {
|
---|
249 | xmlChar *string;
|
---|
250 | xmlChar *cur;
|
---|
251 |
|
---|
252 | int error;
|
---|
253 | int neg;
|
---|
254 |
|
---|
255 | xmlRegStatePtr start;
|
---|
256 | xmlRegStatePtr end;
|
---|
257 | xmlRegStatePtr state;
|
---|
258 |
|
---|
259 | xmlRegAtomPtr atom;
|
---|
260 |
|
---|
261 | int maxAtoms;
|
---|
262 | int nbAtoms;
|
---|
263 | xmlRegAtomPtr *atoms;
|
---|
264 |
|
---|
265 | int maxStates;
|
---|
266 | int nbStates;
|
---|
267 | xmlRegStatePtr *states;
|
---|
268 |
|
---|
269 | int maxCounters;
|
---|
270 | int nbCounters;
|
---|
271 | xmlRegCounter *counters;
|
---|
272 |
|
---|
273 | int determinist;
|
---|
274 | int negs;
|
---|
275 | int flags;
|
---|
276 |
|
---|
277 | int depth;
|
---|
278 | };
|
---|
279 |
|
---|
280 | struct _xmlRegexp {
|
---|
281 | xmlChar *string;
|
---|
282 | int nbStates;
|
---|
283 | xmlRegStatePtr *states;
|
---|
284 | int nbAtoms;
|
---|
285 | xmlRegAtomPtr *atoms;
|
---|
286 | int nbCounters;
|
---|
287 | xmlRegCounter *counters;
|
---|
288 | int determinist;
|
---|
289 | int flags;
|
---|
290 | /*
|
---|
291 | * That's the compact form for determinists automatas
|
---|
292 | */
|
---|
293 | int nbstates;
|
---|
294 | int *compact;
|
---|
295 | void **transdata;
|
---|
296 | int nbstrings;
|
---|
297 | xmlChar **stringMap;
|
---|
298 | };
|
---|
299 |
|
---|
300 | typedef struct _xmlRegExecRollback xmlRegExecRollback;
|
---|
301 | typedef xmlRegExecRollback *xmlRegExecRollbackPtr;
|
---|
302 |
|
---|
303 | struct _xmlRegExecRollback {
|
---|
304 | xmlRegStatePtr state;/* the current state */
|
---|
305 | int index; /* the index in the input stack */
|
---|
306 | int nextbranch; /* the next transition to explore in that state */
|
---|
307 | int *counts; /* save the automata state if it has some */
|
---|
308 | };
|
---|
309 |
|
---|
310 | typedef struct _xmlRegInputToken xmlRegInputToken;
|
---|
311 | typedef xmlRegInputToken *xmlRegInputTokenPtr;
|
---|
312 |
|
---|
313 | struct _xmlRegInputToken {
|
---|
314 | xmlChar *value;
|
---|
315 | void *data;
|
---|
316 | };
|
---|
317 |
|
---|
318 | struct _xmlRegExecCtxt {
|
---|
319 | int status; /* execution status != 0 indicate an error */
|
---|
320 | int determinist; /* did we find an indeterministic behaviour */
|
---|
321 | xmlRegexpPtr comp; /* the compiled regexp */
|
---|
322 | xmlRegExecCallbacks callback;
|
---|
323 | void *data;
|
---|
324 |
|
---|
325 | xmlRegStatePtr state;/* the current state */
|
---|
326 | int transno; /* the current transition on that state */
|
---|
327 | int transcount; /* the number of chars in char counted transitions */
|
---|
328 |
|
---|
329 | /*
|
---|
330 | * A stack of rollback states
|
---|
331 | */
|
---|
332 | int maxRollbacks;
|
---|
333 | int nbRollbacks;
|
---|
334 | xmlRegExecRollback *rollbacks;
|
---|
335 |
|
---|
336 | /*
|
---|
337 | * The state of the automata if any
|
---|
338 | */
|
---|
339 | int *counts;
|
---|
340 |
|
---|
341 | /*
|
---|
342 | * The input stack
|
---|
343 | */
|
---|
344 | int inputStackMax;
|
---|
345 | int inputStackNr;
|
---|
346 | int index;
|
---|
347 | int *charStack;
|
---|
348 | const xmlChar *inputString; /* when operating on characters */
|
---|
349 | xmlRegInputTokenPtr inputStack;/* when operating on strings */
|
---|
350 |
|
---|
351 | /*
|
---|
352 | * error handling
|
---|
353 | */
|
---|
354 | int errStateNo; /* the error state number */
|
---|
355 | xmlRegStatePtr errState; /* the error state */
|
---|
356 | xmlChar *errString; /* the string raising the error */
|
---|
357 | int *errCounts; /* counters at the error state */
|
---|
358 | int nbPush;
|
---|
359 | };
|
---|
360 |
|
---|
361 | #define REGEXP_ALL_COUNTER 0x123456
|
---|
362 | #define REGEXP_ALL_LAX_COUNTER 0x123457
|
---|
363 |
|
---|
364 | static void xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top);
|
---|
365 | static void xmlRegFreeState(xmlRegStatePtr state);
|
---|
366 | static void xmlRegFreeAtom(xmlRegAtomPtr atom);
|
---|
367 | static int xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr);
|
---|
368 | static int xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint);
|
---|
369 | static int xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint,
|
---|
370 | int neg, int start, int end, const xmlChar *blockName);
|
---|
371 |
|
---|
372 | void xmlAutomataSetFlags(xmlAutomataPtr am, int flags);
|
---|
373 |
|
---|
374 | /************************************************************************
|
---|
375 | * *
|
---|
376 | * Regexp memory error handler *
|
---|
377 | * *
|
---|
378 | ************************************************************************/
|
---|
379 | /**
|
---|
380 | * xmlRegexpErrMemory:
|
---|
381 | * @extra: extra information
|
---|
382 | *
|
---|
383 | * Handle an out of memory condition
|
---|
384 | */
|
---|
385 | static void
|
---|
386 | xmlRegexpErrMemory(xmlRegParserCtxtPtr ctxt, const char *extra)
|
---|
387 | {
|
---|
388 | const char *regexp = NULL;
|
---|
389 | if (ctxt != NULL) {
|
---|
390 | regexp = (const char *) ctxt->string;
|
---|
391 | ctxt->error = XML_ERR_NO_MEMORY;
|
---|
392 | }
|
---|
393 | __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
|
---|
394 | XML_ERR_NO_MEMORY, XML_ERR_FATAL, NULL, 0, extra,
|
---|
395 | regexp, NULL, 0, 0,
|
---|
396 | "Memory allocation failed : %s\n", extra);
|
---|
397 | }
|
---|
398 |
|
---|
399 | /**
|
---|
400 | * xmlRegexpErrCompile:
|
---|
401 | * @extra: extra information
|
---|
402 | *
|
---|
403 | * Handle a compilation failure
|
---|
404 | */
|
---|
405 | static void
|
---|
406 | xmlRegexpErrCompile(xmlRegParserCtxtPtr ctxt, const char *extra)
|
---|
407 | {
|
---|
408 | const char *regexp = NULL;
|
---|
409 | int idx = 0;
|
---|
410 |
|
---|
411 | if (ctxt != NULL) {
|
---|
412 | regexp = (const char *) ctxt->string;
|
---|
413 | idx = ctxt->cur - ctxt->string;
|
---|
414 | ctxt->error = XML_REGEXP_COMPILE_ERROR;
|
---|
415 | }
|
---|
416 | __xmlRaiseError(NULL, NULL, NULL, NULL, NULL, XML_FROM_REGEXP,
|
---|
417 | XML_REGEXP_COMPILE_ERROR, XML_ERR_FATAL, NULL, 0, extra,
|
---|
418 | regexp, NULL, idx, 0,
|
---|
419 | "failed to compile: %s\n", extra);
|
---|
420 | }
|
---|
421 |
|
---|
422 | /************************************************************************
|
---|
423 | * *
|
---|
424 | * Allocation/Deallocation *
|
---|
425 | * *
|
---|
426 | ************************************************************************/
|
---|
427 |
|
---|
428 | static int xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt);
|
---|
429 |
|
---|
430 | /**
|
---|
431 | * xmlRegCalloc2:
|
---|
432 | * @dim1: size of first dimension
|
---|
433 | * @dim2: size of second dimension
|
---|
434 | * @elemSize: size of element
|
---|
435 | *
|
---|
436 | * Allocate a two-dimensional array and set all elements to zero.
|
---|
437 | *
|
---|
438 | * Returns the new array or NULL in case of error.
|
---|
439 | */
|
---|
440 | static void*
|
---|
441 | xmlRegCalloc2(size_t dim1, size_t dim2, size_t elemSize) {
|
---|
442 | size_t totalSize;
|
---|
443 | void *ret;
|
---|
444 |
|
---|
445 | /* Check for overflow */
|
---|
446 | if (dim1 > SIZE_MAX / dim2 / elemSize)
|
---|
447 | return (NULL);
|
---|
448 | totalSize = dim1 * dim2 * elemSize;
|
---|
449 | ret = xmlMalloc(totalSize);
|
---|
450 | if (ret != NULL)
|
---|
451 | memset(ret, 0, totalSize);
|
---|
452 | return (ret);
|
---|
453 | }
|
---|
454 |
|
---|
455 | /**
|
---|
456 | * xmlRegEpxFromParse:
|
---|
457 | * @ctxt: the parser context used to build it
|
---|
458 | *
|
---|
459 | * Allocate a new regexp and fill it with the result from the parser
|
---|
460 | *
|
---|
461 | * Returns the new regexp or NULL in case of error
|
---|
462 | */
|
---|
463 | static xmlRegexpPtr
|
---|
464 | xmlRegEpxFromParse(xmlRegParserCtxtPtr ctxt) {
|
---|
465 | xmlRegexpPtr ret;
|
---|
466 |
|
---|
467 | ret = (xmlRegexpPtr) xmlMalloc(sizeof(xmlRegexp));
|
---|
468 | if (ret == NULL) {
|
---|
469 | xmlRegexpErrMemory(ctxt, "compiling regexp");
|
---|
470 | return(NULL);
|
---|
471 | }
|
---|
472 | memset(ret, 0, sizeof(xmlRegexp));
|
---|
473 | ret->string = ctxt->string;
|
---|
474 | ret->nbStates = ctxt->nbStates;
|
---|
475 | ret->states = ctxt->states;
|
---|
476 | ret->nbAtoms = ctxt->nbAtoms;
|
---|
477 | ret->atoms = ctxt->atoms;
|
---|
478 | ret->nbCounters = ctxt->nbCounters;
|
---|
479 | ret->counters = ctxt->counters;
|
---|
480 | ret->determinist = ctxt->determinist;
|
---|
481 | ret->flags = ctxt->flags;
|
---|
482 | if (ret->determinist == -1) {
|
---|
483 | xmlRegexpIsDeterminist(ret);
|
---|
484 | }
|
---|
485 |
|
---|
486 | if ((ret->determinist != 0) &&
|
---|
487 | (ret->nbCounters == 0) &&
|
---|
488 | (ctxt->negs == 0) &&
|
---|
489 | (ret->atoms != NULL) &&
|
---|
490 | (ret->atoms[0] != NULL) &&
|
---|
491 | (ret->atoms[0]->type == XML_REGEXP_STRING)) {
|
---|
492 | int i, j, nbstates = 0, nbatoms = 0;
|
---|
493 | int *stateRemap;
|
---|
494 | int *stringRemap;
|
---|
495 | int *transitions;
|
---|
496 | void **transdata;
|
---|
497 | xmlChar **stringMap;
|
---|
498 | xmlChar *value;
|
---|
499 |
|
---|
500 | /*
|
---|
501 | * Switch to a compact representation
|
---|
502 | * 1/ counting the effective number of states left
|
---|
503 | * 2/ counting the unique number of atoms, and check that
|
---|
504 | * they are all of the string type
|
---|
505 | * 3/ build a table state x atom for the transitions
|
---|
506 | */
|
---|
507 |
|
---|
508 | stateRemap = xmlMalloc(ret->nbStates * sizeof(int));
|
---|
509 | if (stateRemap == NULL) {
|
---|
510 | xmlRegexpErrMemory(ctxt, "compiling regexp");
|
---|
511 | xmlFree(ret);
|
---|
512 | return(NULL);
|
---|
513 | }
|
---|
514 | for (i = 0;i < ret->nbStates;i++) {
|
---|
515 | if (ret->states[i] != NULL) {
|
---|
516 | stateRemap[i] = nbstates;
|
---|
517 | nbstates++;
|
---|
518 | } else {
|
---|
519 | stateRemap[i] = -1;
|
---|
520 | }
|
---|
521 | }
|
---|
522 | #ifdef DEBUG_COMPACTION
|
---|
523 | printf("Final: %d states\n", nbstates);
|
---|
524 | #endif
|
---|
525 | stringMap = xmlMalloc(ret->nbAtoms * sizeof(char *));
|
---|
526 | if (stringMap == NULL) {
|
---|
527 | xmlRegexpErrMemory(ctxt, "compiling regexp");
|
---|
528 | xmlFree(stateRemap);
|
---|
529 | xmlFree(ret);
|
---|
530 | return(NULL);
|
---|
531 | }
|
---|
532 | stringRemap = xmlMalloc(ret->nbAtoms * sizeof(int));
|
---|
533 | if (stringRemap == NULL) {
|
---|
534 | xmlRegexpErrMemory(ctxt, "compiling regexp");
|
---|
535 | xmlFree(stringMap);
|
---|
536 | xmlFree(stateRemap);
|
---|
537 | xmlFree(ret);
|
---|
538 | return(NULL);
|
---|
539 | }
|
---|
540 | for (i = 0;i < ret->nbAtoms;i++) {
|
---|
541 | if ((ret->atoms[i]->type == XML_REGEXP_STRING) &&
|
---|
542 | (ret->atoms[i]->quant == XML_REGEXP_QUANT_ONCE)) {
|
---|
543 | value = ret->atoms[i]->valuep;
|
---|
544 | for (j = 0;j < nbatoms;j++) {
|
---|
545 | if (xmlStrEqual(stringMap[j], value)) {
|
---|
546 | stringRemap[i] = j;
|
---|
547 | break;
|
---|
548 | }
|
---|
549 | }
|
---|
550 | if (j >= nbatoms) {
|
---|
551 | stringRemap[i] = nbatoms;
|
---|
552 | stringMap[nbatoms] = xmlStrdup(value);
|
---|
553 | if (stringMap[nbatoms] == NULL) {
|
---|
554 | for (i = 0;i < nbatoms;i++)
|
---|
555 | xmlFree(stringMap[i]);
|
---|
556 | xmlFree(stringRemap);
|
---|
557 | xmlFree(stringMap);
|
---|
558 | xmlFree(stateRemap);
|
---|
559 | xmlFree(ret);
|
---|
560 | return(NULL);
|
---|
561 | }
|
---|
562 | nbatoms++;
|
---|
563 | }
|
---|
564 | } else {
|
---|
565 | xmlFree(stateRemap);
|
---|
566 | xmlFree(stringRemap);
|
---|
567 | for (i = 0;i < nbatoms;i++)
|
---|
568 | xmlFree(stringMap[i]);
|
---|
569 | xmlFree(stringMap);
|
---|
570 | xmlFree(ret);
|
---|
571 | return(NULL);
|
---|
572 | }
|
---|
573 | }
|
---|
574 | #ifdef DEBUG_COMPACTION
|
---|
575 | printf("Final: %d atoms\n", nbatoms);
|
---|
576 | #endif
|
---|
577 | transitions = (int *) xmlRegCalloc2(nbstates + 1, nbatoms + 1,
|
---|
578 | sizeof(int));
|
---|
579 | if (transitions == NULL) {
|
---|
580 | xmlFree(stateRemap);
|
---|
581 | xmlFree(stringRemap);
|
---|
582 | for (i = 0;i < nbatoms;i++)
|
---|
583 | xmlFree(stringMap[i]);
|
---|
584 | xmlFree(stringMap);
|
---|
585 | xmlFree(ret);
|
---|
586 | return(NULL);
|
---|
587 | }
|
---|
588 |
|
---|
589 | /*
|
---|
590 | * Allocate the transition table. The first entry for each
|
---|
591 | * state corresponds to the state type.
|
---|
592 | */
|
---|
593 | transdata = NULL;
|
---|
594 |
|
---|
595 | for (i = 0;i < ret->nbStates;i++) {
|
---|
596 | int stateno, atomno, targetno, prev;
|
---|
597 | xmlRegStatePtr state;
|
---|
598 | xmlRegTransPtr trans;
|
---|
599 |
|
---|
600 | stateno = stateRemap[i];
|
---|
601 | if (stateno == -1)
|
---|
602 | continue;
|
---|
603 | state = ret->states[i];
|
---|
604 |
|
---|
605 | transitions[stateno * (nbatoms + 1)] = state->type;
|
---|
606 |
|
---|
607 | for (j = 0;j < state->nbTrans;j++) {
|
---|
608 | trans = &(state->trans[j]);
|
---|
609 | if ((trans->to == -1) || (trans->atom == NULL))
|
---|
610 | continue;
|
---|
611 | atomno = stringRemap[trans->atom->no];
|
---|
612 | if ((trans->atom->data != NULL) && (transdata == NULL)) {
|
---|
613 | transdata = (void **) xmlRegCalloc2(nbstates, nbatoms,
|
---|
614 | sizeof(void *));
|
---|
615 | if (transdata == NULL) {
|
---|
616 | xmlRegexpErrMemory(ctxt, "compiling regexp");
|
---|
617 | break;
|
---|
618 | }
|
---|
619 | }
|
---|
620 | targetno = stateRemap[trans->to];
|
---|
621 | /*
|
---|
622 | * if the same atom can generate transitions to 2 different
|
---|
623 | * states then it means the automata is not deterministic and
|
---|
624 | * the compact form can't be used !
|
---|
625 | */
|
---|
626 | prev = transitions[stateno * (nbatoms + 1) + atomno + 1];
|
---|
627 | if (prev != 0) {
|
---|
628 | if (prev != targetno + 1) {
|
---|
629 | ret->determinist = 0;
|
---|
630 | #ifdef DEBUG_COMPACTION
|
---|
631 | printf("Indet: state %d trans %d, atom %d to %d : %d to %d\n",
|
---|
632 | i, j, trans->atom->no, trans->to, atomno, targetno);
|
---|
633 | printf(" previous to is %d\n", prev);
|
---|
634 | #endif
|
---|
635 | if (transdata != NULL)
|
---|
636 | xmlFree(transdata);
|
---|
637 | xmlFree(transitions);
|
---|
638 | xmlFree(stateRemap);
|
---|
639 | xmlFree(stringRemap);
|
---|
640 | for (i = 0;i < nbatoms;i++)
|
---|
641 | xmlFree(stringMap[i]);
|
---|
642 | xmlFree(stringMap);
|
---|
643 | goto not_determ;
|
---|
644 | }
|
---|
645 | } else {
|
---|
646 | #if 0
|
---|
647 | printf("State %d trans %d: atom %d to %d : %d to %d\n",
|
---|
648 | i, j, trans->atom->no, trans->to, atomno, targetno);
|
---|
649 | #endif
|
---|
650 | transitions[stateno * (nbatoms + 1) + atomno + 1] =
|
---|
651 | targetno + 1; /* to avoid 0 */
|
---|
652 | if (transdata != NULL)
|
---|
653 | transdata[stateno * nbatoms + atomno] =
|
---|
654 | trans->atom->data;
|
---|
655 | }
|
---|
656 | }
|
---|
657 | }
|
---|
658 | ret->determinist = 1;
|
---|
659 | #ifdef DEBUG_COMPACTION
|
---|
660 | /*
|
---|
661 | * Debug
|
---|
662 | */
|
---|
663 | for (i = 0;i < nbstates;i++) {
|
---|
664 | for (j = 0;j < nbatoms + 1;j++) {
|
---|
665 | printf("%02d ", transitions[i * (nbatoms + 1) + j]);
|
---|
666 | }
|
---|
667 | printf("\n");
|
---|
668 | }
|
---|
669 | printf("\n");
|
---|
670 | #endif
|
---|
671 | /*
|
---|
672 | * Cleanup of the old data
|
---|
673 | */
|
---|
674 | if (ret->states != NULL) {
|
---|
675 | for (i = 0;i < ret->nbStates;i++)
|
---|
676 | xmlRegFreeState(ret->states[i]);
|
---|
677 | xmlFree(ret->states);
|
---|
678 | }
|
---|
679 | ret->states = NULL;
|
---|
680 | ret->nbStates = 0;
|
---|
681 | if (ret->atoms != NULL) {
|
---|
682 | for (i = 0;i < ret->nbAtoms;i++)
|
---|
683 | xmlRegFreeAtom(ret->atoms[i]);
|
---|
684 | xmlFree(ret->atoms);
|
---|
685 | }
|
---|
686 | ret->atoms = NULL;
|
---|
687 | ret->nbAtoms = 0;
|
---|
688 |
|
---|
689 | ret->compact = transitions;
|
---|
690 | ret->transdata = transdata;
|
---|
691 | ret->stringMap = stringMap;
|
---|
692 | ret->nbstrings = nbatoms;
|
---|
693 | ret->nbstates = nbstates;
|
---|
694 | xmlFree(stateRemap);
|
---|
695 | xmlFree(stringRemap);
|
---|
696 | }
|
---|
697 | not_determ:
|
---|
698 | ctxt->string = NULL;
|
---|
699 | ctxt->nbStates = 0;
|
---|
700 | ctxt->states = NULL;
|
---|
701 | ctxt->nbAtoms = 0;
|
---|
702 | ctxt->atoms = NULL;
|
---|
703 | ctxt->nbCounters = 0;
|
---|
704 | ctxt->counters = NULL;
|
---|
705 | return(ret);
|
---|
706 | }
|
---|
707 |
|
---|
708 | /**
|
---|
709 | * xmlRegNewParserCtxt:
|
---|
710 | * @string: the string to parse
|
---|
711 | *
|
---|
712 | * Allocate a new regexp parser context
|
---|
713 | *
|
---|
714 | * Returns the new context or NULL in case of error
|
---|
715 | */
|
---|
716 | static xmlRegParserCtxtPtr
|
---|
717 | xmlRegNewParserCtxt(const xmlChar *string) {
|
---|
718 | xmlRegParserCtxtPtr ret;
|
---|
719 |
|
---|
720 | ret = (xmlRegParserCtxtPtr) xmlMalloc(sizeof(xmlRegParserCtxt));
|
---|
721 | if (ret == NULL)
|
---|
722 | return(NULL);
|
---|
723 | memset(ret, 0, sizeof(xmlRegParserCtxt));
|
---|
724 | if (string != NULL)
|
---|
725 | ret->string = xmlStrdup(string);
|
---|
726 | ret->cur = ret->string;
|
---|
727 | ret->neg = 0;
|
---|
728 | ret->negs = 0;
|
---|
729 | ret->error = 0;
|
---|
730 | ret->determinist = -1;
|
---|
731 | return(ret);
|
---|
732 | }
|
---|
733 |
|
---|
734 | /**
|
---|
735 | * xmlRegNewRange:
|
---|
736 | * @ctxt: the regexp parser context
|
---|
737 | * @neg: is that negative
|
---|
738 | * @type: the type of range
|
---|
739 | * @start: the start codepoint
|
---|
740 | * @end: the end codepoint
|
---|
741 | *
|
---|
742 | * Allocate a new regexp range
|
---|
743 | *
|
---|
744 | * Returns the new range or NULL in case of error
|
---|
745 | */
|
---|
746 | static xmlRegRangePtr
|
---|
747 | xmlRegNewRange(xmlRegParserCtxtPtr ctxt,
|
---|
748 | int neg, xmlRegAtomType type, int start, int end) {
|
---|
749 | xmlRegRangePtr ret;
|
---|
750 |
|
---|
751 | ret = (xmlRegRangePtr) xmlMalloc(sizeof(xmlRegRange));
|
---|
752 | if (ret == NULL) {
|
---|
753 | xmlRegexpErrMemory(ctxt, "allocating range");
|
---|
754 | return(NULL);
|
---|
755 | }
|
---|
756 | ret->neg = neg;
|
---|
757 | ret->type = type;
|
---|
758 | ret->start = start;
|
---|
759 | ret->end = end;
|
---|
760 | return(ret);
|
---|
761 | }
|
---|
762 |
|
---|
763 | /**
|
---|
764 | * xmlRegFreeRange:
|
---|
765 | * @range: the regexp range
|
---|
766 | *
|
---|
767 | * Free a regexp range
|
---|
768 | */
|
---|
769 | static void
|
---|
770 | xmlRegFreeRange(xmlRegRangePtr range) {
|
---|
771 | if (range == NULL)
|
---|
772 | return;
|
---|
773 |
|
---|
774 | if (range->blockName != NULL)
|
---|
775 | xmlFree(range->blockName);
|
---|
776 | xmlFree(range);
|
---|
777 | }
|
---|
778 |
|
---|
779 | /**
|
---|
780 | * xmlRegCopyRange:
|
---|
781 | * @range: the regexp range
|
---|
782 | *
|
---|
783 | * Copy a regexp range
|
---|
784 | *
|
---|
785 | * Returns the new copy or NULL in case of error.
|
---|
786 | */
|
---|
787 | static xmlRegRangePtr
|
---|
788 | xmlRegCopyRange(xmlRegParserCtxtPtr ctxt, xmlRegRangePtr range) {
|
---|
789 | xmlRegRangePtr ret;
|
---|
790 |
|
---|
791 | if (range == NULL)
|
---|
792 | return(NULL);
|
---|
793 |
|
---|
794 | ret = xmlRegNewRange(ctxt, range->neg, range->type, range->start,
|
---|
795 | range->end);
|
---|
796 | if (ret == NULL)
|
---|
797 | return(NULL);
|
---|
798 | if (range->blockName != NULL) {
|
---|
799 | ret->blockName = xmlStrdup(range->blockName);
|
---|
800 | if (ret->blockName == NULL) {
|
---|
801 | xmlRegexpErrMemory(ctxt, "allocating range");
|
---|
802 | xmlRegFreeRange(ret);
|
---|
803 | return(NULL);
|
---|
804 | }
|
---|
805 | }
|
---|
806 | return(ret);
|
---|
807 | }
|
---|
808 |
|
---|
809 | /**
|
---|
810 | * xmlRegNewAtom:
|
---|
811 | * @ctxt: the regexp parser context
|
---|
812 | * @type: the type of atom
|
---|
813 | *
|
---|
814 | * Allocate a new atom
|
---|
815 | *
|
---|
816 | * Returns the new atom or NULL in case of error
|
---|
817 | */
|
---|
818 | static xmlRegAtomPtr
|
---|
819 | xmlRegNewAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomType type) {
|
---|
820 | xmlRegAtomPtr ret;
|
---|
821 |
|
---|
822 | ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
|
---|
823 | if (ret == NULL) {
|
---|
824 | xmlRegexpErrMemory(ctxt, "allocating atom");
|
---|
825 | return(NULL);
|
---|
826 | }
|
---|
827 | memset(ret, 0, sizeof(xmlRegAtom));
|
---|
828 | ret->type = type;
|
---|
829 | ret->quant = XML_REGEXP_QUANT_ONCE;
|
---|
830 | ret->min = 0;
|
---|
831 | ret->max = 0;
|
---|
832 | return(ret);
|
---|
833 | }
|
---|
834 |
|
---|
835 | /**
|
---|
836 | * xmlRegFreeAtom:
|
---|
837 | * @atom: the regexp atom
|
---|
838 | *
|
---|
839 | * Free a regexp atom
|
---|
840 | */
|
---|
841 | static void
|
---|
842 | xmlRegFreeAtom(xmlRegAtomPtr atom) {
|
---|
843 | int i;
|
---|
844 |
|
---|
845 | if (atom == NULL)
|
---|
846 | return;
|
---|
847 |
|
---|
848 | for (i = 0;i < atom->nbRanges;i++)
|
---|
849 | xmlRegFreeRange(atom->ranges[i]);
|
---|
850 | if (atom->ranges != NULL)
|
---|
851 | xmlFree(atom->ranges);
|
---|
852 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep != NULL))
|
---|
853 | xmlFree(atom->valuep);
|
---|
854 | if ((atom->type == XML_REGEXP_STRING) && (atom->valuep2 != NULL))
|
---|
855 | xmlFree(atom->valuep2);
|
---|
856 | if ((atom->type == XML_REGEXP_BLOCK_NAME) && (atom->valuep != NULL))
|
---|
857 | xmlFree(atom->valuep);
|
---|
858 | xmlFree(atom);
|
---|
859 | }
|
---|
860 |
|
---|
861 | /**
|
---|
862 | * xmlRegCopyAtom:
|
---|
863 | * @ctxt: the regexp parser context
|
---|
864 | * @atom: the original atom
|
---|
865 | *
|
---|
866 | * Allocate a new regexp range
|
---|
867 | *
|
---|
868 | * Returns the new atom or NULL in case of error
|
---|
869 | */
|
---|
870 | static xmlRegAtomPtr
|
---|
871 | xmlRegCopyAtom(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
|
---|
872 | xmlRegAtomPtr ret;
|
---|
873 |
|
---|
874 | ret = (xmlRegAtomPtr) xmlMalloc(sizeof(xmlRegAtom));
|
---|
875 | if (ret == NULL) {
|
---|
876 | xmlRegexpErrMemory(ctxt, "copying atom");
|
---|
877 | return(NULL);
|
---|
878 | }
|
---|
879 | memset(ret, 0, sizeof(xmlRegAtom));
|
---|
880 | ret->type = atom->type;
|
---|
881 | ret->quant = atom->quant;
|
---|
882 | ret->min = atom->min;
|
---|
883 | ret->max = atom->max;
|
---|
884 | if (atom->nbRanges > 0) {
|
---|
885 | int i;
|
---|
886 |
|
---|
887 | ret->ranges = (xmlRegRangePtr *) xmlMalloc(sizeof(xmlRegRangePtr) *
|
---|
888 | atom->nbRanges);
|
---|
889 | if (ret->ranges == NULL) {
|
---|
890 | xmlRegexpErrMemory(ctxt, "copying atom");
|
---|
891 | goto error;
|
---|
892 | }
|
---|
893 | for (i = 0;i < atom->nbRanges;i++) {
|
---|
894 | ret->ranges[i] = xmlRegCopyRange(ctxt, atom->ranges[i]);
|
---|
895 | if (ret->ranges[i] == NULL)
|
---|
896 | goto error;
|
---|
897 | ret->nbRanges = i + 1;
|
---|
898 | }
|
---|
899 | }
|
---|
900 | return(ret);
|
---|
901 |
|
---|
902 | error:
|
---|
903 | xmlRegFreeAtom(ret);
|
---|
904 | return(NULL);
|
---|
905 | }
|
---|
906 |
|
---|
907 | static xmlRegStatePtr
|
---|
908 | xmlRegNewState(xmlRegParserCtxtPtr ctxt) {
|
---|
909 | xmlRegStatePtr ret;
|
---|
910 |
|
---|
911 | ret = (xmlRegStatePtr) xmlMalloc(sizeof(xmlRegState));
|
---|
912 | if (ret == NULL) {
|
---|
913 | xmlRegexpErrMemory(ctxt, "allocating state");
|
---|
914 | return(NULL);
|
---|
915 | }
|
---|
916 | memset(ret, 0, sizeof(xmlRegState));
|
---|
917 | ret->type = XML_REGEXP_TRANS_STATE;
|
---|
918 | ret->mark = XML_REGEXP_MARK_NORMAL;
|
---|
919 | return(ret);
|
---|
920 | }
|
---|
921 |
|
---|
922 | /**
|
---|
923 | * xmlRegFreeState:
|
---|
924 | * @state: the regexp state
|
---|
925 | *
|
---|
926 | * Free a regexp state
|
---|
927 | */
|
---|
928 | static void
|
---|
929 | xmlRegFreeState(xmlRegStatePtr state) {
|
---|
930 | if (state == NULL)
|
---|
931 | return;
|
---|
932 |
|
---|
933 | if (state->trans != NULL)
|
---|
934 | xmlFree(state->trans);
|
---|
935 | if (state->transTo != NULL)
|
---|
936 | xmlFree(state->transTo);
|
---|
937 | xmlFree(state);
|
---|
938 | }
|
---|
939 |
|
---|
940 | /**
|
---|
941 | * xmlRegFreeParserCtxt:
|
---|
942 | * @ctxt: the regexp parser context
|
---|
943 | *
|
---|
944 | * Free a regexp parser context
|
---|
945 | */
|
---|
946 | static void
|
---|
947 | xmlRegFreeParserCtxt(xmlRegParserCtxtPtr ctxt) {
|
---|
948 | int i;
|
---|
949 | if (ctxt == NULL)
|
---|
950 | return;
|
---|
951 |
|
---|
952 | if (ctxt->string != NULL)
|
---|
953 | xmlFree(ctxt->string);
|
---|
954 | if (ctxt->states != NULL) {
|
---|
955 | for (i = 0;i < ctxt->nbStates;i++)
|
---|
956 | xmlRegFreeState(ctxt->states[i]);
|
---|
957 | xmlFree(ctxt->states);
|
---|
958 | }
|
---|
959 | if (ctxt->atoms != NULL) {
|
---|
960 | for (i = 0;i < ctxt->nbAtoms;i++)
|
---|
961 | xmlRegFreeAtom(ctxt->atoms[i]);
|
---|
962 | xmlFree(ctxt->atoms);
|
---|
963 | }
|
---|
964 | if (ctxt->counters != NULL)
|
---|
965 | xmlFree(ctxt->counters);
|
---|
966 | xmlFree(ctxt);
|
---|
967 | }
|
---|
968 |
|
---|
969 | /************************************************************************
|
---|
970 | * *
|
---|
971 | * Display of Data structures *
|
---|
972 | * *
|
---|
973 | ************************************************************************/
|
---|
974 |
|
---|
975 | static void
|
---|
976 | xmlRegPrintAtomType(FILE *output, xmlRegAtomType type) {
|
---|
977 | switch (type) {
|
---|
978 | case XML_REGEXP_EPSILON:
|
---|
979 | fprintf(output, "epsilon "); break;
|
---|
980 | case XML_REGEXP_CHARVAL:
|
---|
981 | fprintf(output, "charval "); break;
|
---|
982 | case XML_REGEXP_RANGES:
|
---|
983 | fprintf(output, "ranges "); break;
|
---|
984 | case XML_REGEXP_SUBREG:
|
---|
985 | fprintf(output, "subexpr "); break;
|
---|
986 | case XML_REGEXP_STRING:
|
---|
987 | fprintf(output, "string "); break;
|
---|
988 | case XML_REGEXP_ANYCHAR:
|
---|
989 | fprintf(output, "anychar "); break;
|
---|
990 | case XML_REGEXP_ANYSPACE:
|
---|
991 | fprintf(output, "anyspace "); break;
|
---|
992 | case XML_REGEXP_NOTSPACE:
|
---|
993 | fprintf(output, "notspace "); break;
|
---|
994 | case XML_REGEXP_INITNAME:
|
---|
995 | fprintf(output, "initname "); break;
|
---|
996 | case XML_REGEXP_NOTINITNAME:
|
---|
997 | fprintf(output, "notinitname "); break;
|
---|
998 | case XML_REGEXP_NAMECHAR:
|
---|
999 | fprintf(output, "namechar "); break;
|
---|
1000 | case XML_REGEXP_NOTNAMECHAR:
|
---|
1001 | fprintf(output, "notnamechar "); break;
|
---|
1002 | case XML_REGEXP_DECIMAL:
|
---|
1003 | fprintf(output, "decimal "); break;
|
---|
1004 | case XML_REGEXP_NOTDECIMAL:
|
---|
1005 | fprintf(output, "notdecimal "); break;
|
---|
1006 | case XML_REGEXP_REALCHAR:
|
---|
1007 | fprintf(output, "realchar "); break;
|
---|
1008 | case XML_REGEXP_NOTREALCHAR:
|
---|
1009 | fprintf(output, "notrealchar "); break;
|
---|
1010 | case XML_REGEXP_LETTER:
|
---|
1011 | fprintf(output, "LETTER "); break;
|
---|
1012 | case XML_REGEXP_LETTER_UPPERCASE:
|
---|
1013 | fprintf(output, "LETTER_UPPERCASE "); break;
|
---|
1014 | case XML_REGEXP_LETTER_LOWERCASE:
|
---|
1015 | fprintf(output, "LETTER_LOWERCASE "); break;
|
---|
1016 | case XML_REGEXP_LETTER_TITLECASE:
|
---|
1017 | fprintf(output, "LETTER_TITLECASE "); break;
|
---|
1018 | case XML_REGEXP_LETTER_MODIFIER:
|
---|
1019 | fprintf(output, "LETTER_MODIFIER "); break;
|
---|
1020 | case XML_REGEXP_LETTER_OTHERS:
|
---|
1021 | fprintf(output, "LETTER_OTHERS "); break;
|
---|
1022 | case XML_REGEXP_MARK:
|
---|
1023 | fprintf(output, "MARK "); break;
|
---|
1024 | case XML_REGEXP_MARK_NONSPACING:
|
---|
1025 | fprintf(output, "MARK_NONSPACING "); break;
|
---|
1026 | case XML_REGEXP_MARK_SPACECOMBINING:
|
---|
1027 | fprintf(output, "MARK_SPACECOMBINING "); break;
|
---|
1028 | case XML_REGEXP_MARK_ENCLOSING:
|
---|
1029 | fprintf(output, "MARK_ENCLOSING "); break;
|
---|
1030 | case XML_REGEXP_NUMBER:
|
---|
1031 | fprintf(output, "NUMBER "); break;
|
---|
1032 | case XML_REGEXP_NUMBER_DECIMAL:
|
---|
1033 | fprintf(output, "NUMBER_DECIMAL "); break;
|
---|
1034 | case XML_REGEXP_NUMBER_LETTER:
|
---|
1035 | fprintf(output, "NUMBER_LETTER "); break;
|
---|
1036 | case XML_REGEXP_NUMBER_OTHERS:
|
---|
1037 | fprintf(output, "NUMBER_OTHERS "); break;
|
---|
1038 | case XML_REGEXP_PUNCT:
|
---|
1039 | fprintf(output, "PUNCT "); break;
|
---|
1040 | case XML_REGEXP_PUNCT_CONNECTOR:
|
---|
1041 | fprintf(output, "PUNCT_CONNECTOR "); break;
|
---|
1042 | case XML_REGEXP_PUNCT_DASH:
|
---|
1043 | fprintf(output, "PUNCT_DASH "); break;
|
---|
1044 | case XML_REGEXP_PUNCT_OPEN:
|
---|
1045 | fprintf(output, "PUNCT_OPEN "); break;
|
---|
1046 | case XML_REGEXP_PUNCT_CLOSE:
|
---|
1047 | fprintf(output, "PUNCT_CLOSE "); break;
|
---|
1048 | case XML_REGEXP_PUNCT_INITQUOTE:
|
---|
1049 | fprintf(output, "PUNCT_INITQUOTE "); break;
|
---|
1050 | case XML_REGEXP_PUNCT_FINQUOTE:
|
---|
1051 | fprintf(output, "PUNCT_FINQUOTE "); break;
|
---|
1052 | case XML_REGEXP_PUNCT_OTHERS:
|
---|
1053 | fprintf(output, "PUNCT_OTHERS "); break;
|
---|
1054 | case XML_REGEXP_SEPAR:
|
---|
1055 | fprintf(output, "SEPAR "); break;
|
---|
1056 | case XML_REGEXP_SEPAR_SPACE:
|
---|
1057 | fprintf(output, "SEPAR_SPACE "); break;
|
---|
1058 | case XML_REGEXP_SEPAR_LINE:
|
---|
1059 | fprintf(output, "SEPAR_LINE "); break;
|
---|
1060 | case XML_REGEXP_SEPAR_PARA:
|
---|
1061 | fprintf(output, "SEPAR_PARA "); break;
|
---|
1062 | case XML_REGEXP_SYMBOL:
|
---|
1063 | fprintf(output, "SYMBOL "); break;
|
---|
1064 | case XML_REGEXP_SYMBOL_MATH:
|
---|
1065 | fprintf(output, "SYMBOL_MATH "); break;
|
---|
1066 | case XML_REGEXP_SYMBOL_CURRENCY:
|
---|
1067 | fprintf(output, "SYMBOL_CURRENCY "); break;
|
---|
1068 | case XML_REGEXP_SYMBOL_MODIFIER:
|
---|
1069 | fprintf(output, "SYMBOL_MODIFIER "); break;
|
---|
1070 | case XML_REGEXP_SYMBOL_OTHERS:
|
---|
1071 | fprintf(output, "SYMBOL_OTHERS "); break;
|
---|
1072 | case XML_REGEXP_OTHER:
|
---|
1073 | fprintf(output, "OTHER "); break;
|
---|
1074 | case XML_REGEXP_OTHER_CONTROL:
|
---|
1075 | fprintf(output, "OTHER_CONTROL "); break;
|
---|
1076 | case XML_REGEXP_OTHER_FORMAT:
|
---|
1077 | fprintf(output, "OTHER_FORMAT "); break;
|
---|
1078 | case XML_REGEXP_OTHER_PRIVATE:
|
---|
1079 | fprintf(output, "OTHER_PRIVATE "); break;
|
---|
1080 | case XML_REGEXP_OTHER_NA:
|
---|
1081 | fprintf(output, "OTHER_NA "); break;
|
---|
1082 | case XML_REGEXP_BLOCK_NAME:
|
---|
1083 | fprintf(output, "BLOCK "); break;
|
---|
1084 | }
|
---|
1085 | }
|
---|
1086 |
|
---|
1087 | static void
|
---|
1088 | xmlRegPrintQuantType(FILE *output, xmlRegQuantType type) {
|
---|
1089 | switch (type) {
|
---|
1090 | case XML_REGEXP_QUANT_EPSILON:
|
---|
1091 | fprintf(output, "epsilon "); break;
|
---|
1092 | case XML_REGEXP_QUANT_ONCE:
|
---|
1093 | fprintf(output, "once "); break;
|
---|
1094 | case XML_REGEXP_QUANT_OPT:
|
---|
1095 | fprintf(output, "? "); break;
|
---|
1096 | case XML_REGEXP_QUANT_MULT:
|
---|
1097 | fprintf(output, "* "); break;
|
---|
1098 | case XML_REGEXP_QUANT_PLUS:
|
---|
1099 | fprintf(output, "+ "); break;
|
---|
1100 | case XML_REGEXP_QUANT_RANGE:
|
---|
1101 | fprintf(output, "range "); break;
|
---|
1102 | case XML_REGEXP_QUANT_ONCEONLY:
|
---|
1103 | fprintf(output, "onceonly "); break;
|
---|
1104 | case XML_REGEXP_QUANT_ALL:
|
---|
1105 | fprintf(output, "all "); break;
|
---|
1106 | }
|
---|
1107 | }
|
---|
1108 | static void
|
---|
1109 | xmlRegPrintRange(FILE *output, xmlRegRangePtr range) {
|
---|
1110 | fprintf(output, " range: ");
|
---|
1111 | if (range->neg)
|
---|
1112 | fprintf(output, "negative ");
|
---|
1113 | xmlRegPrintAtomType(output, range->type);
|
---|
1114 | fprintf(output, "%c - %c\n", range->start, range->end);
|
---|
1115 | }
|
---|
1116 |
|
---|
1117 | static void
|
---|
1118 | xmlRegPrintAtom(FILE *output, xmlRegAtomPtr atom) {
|
---|
1119 | fprintf(output, " atom: ");
|
---|
1120 | if (atom == NULL) {
|
---|
1121 | fprintf(output, "NULL\n");
|
---|
1122 | return;
|
---|
1123 | }
|
---|
1124 | if (atom->neg)
|
---|
1125 | fprintf(output, "not ");
|
---|
1126 | xmlRegPrintAtomType(output, atom->type);
|
---|
1127 | xmlRegPrintQuantType(output, atom->quant);
|
---|
1128 | if (atom->quant == XML_REGEXP_QUANT_RANGE)
|
---|
1129 | fprintf(output, "%d-%d ", atom->min, atom->max);
|
---|
1130 | if (atom->type == XML_REGEXP_STRING)
|
---|
1131 | fprintf(output, "'%s' ", (char *) atom->valuep);
|
---|
1132 | if (atom->type == XML_REGEXP_CHARVAL)
|
---|
1133 | fprintf(output, "char %c\n", atom->codepoint);
|
---|
1134 | else if (atom->type == XML_REGEXP_RANGES) {
|
---|
1135 | int i;
|
---|
1136 | fprintf(output, "%d entries\n", atom->nbRanges);
|
---|
1137 | for (i = 0; i < atom->nbRanges;i++)
|
---|
1138 | xmlRegPrintRange(output, atom->ranges[i]);
|
---|
1139 | } else if (atom->type == XML_REGEXP_SUBREG) {
|
---|
1140 | fprintf(output, "start %d end %d\n", atom->start->no, atom->stop->no);
|
---|
1141 | } else {
|
---|
1142 | fprintf(output, "\n");
|
---|
1143 | }
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | static void
|
---|
1147 | xmlRegPrintTrans(FILE *output, xmlRegTransPtr trans) {
|
---|
1148 | fprintf(output, " trans: ");
|
---|
1149 | if (trans == NULL) {
|
---|
1150 | fprintf(output, "NULL\n");
|
---|
1151 | return;
|
---|
1152 | }
|
---|
1153 | if (trans->to < 0) {
|
---|
1154 | fprintf(output, "removed\n");
|
---|
1155 | return;
|
---|
1156 | }
|
---|
1157 | if (trans->nd != 0) {
|
---|
1158 | if (trans->nd == 2)
|
---|
1159 | fprintf(output, "last not determinist, ");
|
---|
1160 | else
|
---|
1161 | fprintf(output, "not determinist, ");
|
---|
1162 | }
|
---|
1163 | if (trans->counter >= 0) {
|
---|
1164 | fprintf(output, "counted %d, ", trans->counter);
|
---|
1165 | }
|
---|
1166 | if (trans->count == REGEXP_ALL_COUNTER) {
|
---|
1167 | fprintf(output, "all transition, ");
|
---|
1168 | } else if (trans->count >= 0) {
|
---|
1169 | fprintf(output, "count based %d, ", trans->count);
|
---|
1170 | }
|
---|
1171 | if (trans->atom == NULL) {
|
---|
1172 | fprintf(output, "epsilon to %d\n", trans->to);
|
---|
1173 | return;
|
---|
1174 | }
|
---|
1175 | if (trans->atom->type == XML_REGEXP_CHARVAL)
|
---|
1176 | fprintf(output, "char %c ", trans->atom->codepoint);
|
---|
1177 | fprintf(output, "atom %d, to %d\n", trans->atom->no, trans->to);
|
---|
1178 | }
|
---|
1179 |
|
---|
1180 | static void
|
---|
1181 | xmlRegPrintState(FILE *output, xmlRegStatePtr state) {
|
---|
1182 | int i;
|
---|
1183 |
|
---|
1184 | fprintf(output, " state: ");
|
---|
1185 | if (state == NULL) {
|
---|
1186 | fprintf(output, "NULL\n");
|
---|
1187 | return;
|
---|
1188 | }
|
---|
1189 | if (state->type == XML_REGEXP_START_STATE)
|
---|
1190 | fprintf(output, "START ");
|
---|
1191 | if (state->type == XML_REGEXP_FINAL_STATE)
|
---|
1192 | fprintf(output, "FINAL ");
|
---|
1193 |
|
---|
1194 | fprintf(output, "%d, %d transitions:\n", state->no, state->nbTrans);
|
---|
1195 | for (i = 0;i < state->nbTrans; i++) {
|
---|
1196 | xmlRegPrintTrans(output, &(state->trans[i]));
|
---|
1197 | }
|
---|
1198 | }
|
---|
1199 |
|
---|
1200 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1201 | static void
|
---|
1202 | xmlRegPrintCtxt(FILE *output, xmlRegParserCtxtPtr ctxt) {
|
---|
1203 | int i;
|
---|
1204 |
|
---|
1205 | fprintf(output, " ctxt: ");
|
---|
1206 | if (ctxt == NULL) {
|
---|
1207 | fprintf(output, "NULL\n");
|
---|
1208 | return;
|
---|
1209 | }
|
---|
1210 | fprintf(output, "'%s' ", ctxt->string);
|
---|
1211 | if (ctxt->error)
|
---|
1212 | fprintf(output, "error ");
|
---|
1213 | if (ctxt->neg)
|
---|
1214 | fprintf(output, "neg ");
|
---|
1215 | fprintf(output, "\n");
|
---|
1216 | fprintf(output, "%d atoms:\n", ctxt->nbAtoms);
|
---|
1217 | for (i = 0;i < ctxt->nbAtoms; i++) {
|
---|
1218 | fprintf(output, " %02d ", i);
|
---|
1219 | xmlRegPrintAtom(output, ctxt->atoms[i]);
|
---|
1220 | }
|
---|
1221 | if (ctxt->atom != NULL) {
|
---|
1222 | fprintf(output, "current atom:\n");
|
---|
1223 | xmlRegPrintAtom(output, ctxt->atom);
|
---|
1224 | }
|
---|
1225 | fprintf(output, "%d states:", ctxt->nbStates);
|
---|
1226 | if (ctxt->start != NULL)
|
---|
1227 | fprintf(output, " start: %d", ctxt->start->no);
|
---|
1228 | if (ctxt->end != NULL)
|
---|
1229 | fprintf(output, " end: %d", ctxt->end->no);
|
---|
1230 | fprintf(output, "\n");
|
---|
1231 | for (i = 0;i < ctxt->nbStates; i++) {
|
---|
1232 | xmlRegPrintState(output, ctxt->states[i]);
|
---|
1233 | }
|
---|
1234 | fprintf(output, "%d counters:\n", ctxt->nbCounters);
|
---|
1235 | for (i = 0;i < ctxt->nbCounters; i++) {
|
---|
1236 | fprintf(output, " %d: min %d max %d\n", i, ctxt->counters[i].min,
|
---|
1237 | ctxt->counters[i].max);
|
---|
1238 | }
|
---|
1239 | }
|
---|
1240 | #endif
|
---|
1241 |
|
---|
1242 | /************************************************************************
|
---|
1243 | * *
|
---|
1244 | * Finite Automata structures manipulations *
|
---|
1245 | * *
|
---|
1246 | ************************************************************************/
|
---|
1247 |
|
---|
1248 | static void
|
---|
1249 | xmlRegAtomAddRange(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom,
|
---|
1250 | int neg, xmlRegAtomType type, int start, int end,
|
---|
1251 | xmlChar *blockName) {
|
---|
1252 | xmlRegRangePtr range;
|
---|
1253 |
|
---|
1254 | if (atom == NULL) {
|
---|
1255 | ERROR("add range: atom is NULL");
|
---|
1256 | return;
|
---|
1257 | }
|
---|
1258 | if (atom->type != XML_REGEXP_RANGES) {
|
---|
1259 | ERROR("add range: atom is not ranges");
|
---|
1260 | return;
|
---|
1261 | }
|
---|
1262 | if (atom->maxRanges == 0) {
|
---|
1263 | atom->maxRanges = 4;
|
---|
1264 | atom->ranges = (xmlRegRangePtr *) xmlMalloc(atom->maxRanges *
|
---|
1265 | sizeof(xmlRegRangePtr));
|
---|
1266 | if (atom->ranges == NULL) {
|
---|
1267 | xmlRegexpErrMemory(ctxt, "adding ranges");
|
---|
1268 | atom->maxRanges = 0;
|
---|
1269 | return;
|
---|
1270 | }
|
---|
1271 | } else if (atom->nbRanges >= atom->maxRanges) {
|
---|
1272 | xmlRegRangePtr *tmp;
|
---|
1273 | atom->maxRanges *= 2;
|
---|
1274 | tmp = (xmlRegRangePtr *) xmlRealloc(atom->ranges, atom->maxRanges *
|
---|
1275 | sizeof(xmlRegRangePtr));
|
---|
1276 | if (tmp == NULL) {
|
---|
1277 | xmlRegexpErrMemory(ctxt, "adding ranges");
|
---|
1278 | atom->maxRanges /= 2;
|
---|
1279 | return;
|
---|
1280 | }
|
---|
1281 | atom->ranges = tmp;
|
---|
1282 | }
|
---|
1283 | range = xmlRegNewRange(ctxt, neg, type, start, end);
|
---|
1284 | if (range == NULL)
|
---|
1285 | return;
|
---|
1286 | range->blockName = blockName;
|
---|
1287 | atom->ranges[atom->nbRanges++] = range;
|
---|
1288 |
|
---|
1289 | }
|
---|
1290 |
|
---|
1291 | static int
|
---|
1292 | xmlRegGetCounter(xmlRegParserCtxtPtr ctxt) {
|
---|
1293 | if (ctxt->maxCounters == 0) {
|
---|
1294 | ctxt->maxCounters = 4;
|
---|
1295 | ctxt->counters = (xmlRegCounter *) xmlMalloc(ctxt->maxCounters *
|
---|
1296 | sizeof(xmlRegCounter));
|
---|
1297 | if (ctxt->counters == NULL) {
|
---|
1298 | xmlRegexpErrMemory(ctxt, "allocating counter");
|
---|
1299 | ctxt->maxCounters = 0;
|
---|
1300 | return(-1);
|
---|
1301 | }
|
---|
1302 | } else if (ctxt->nbCounters >= ctxt->maxCounters) {
|
---|
1303 | xmlRegCounter *tmp;
|
---|
1304 | ctxt->maxCounters *= 2;
|
---|
1305 | tmp = (xmlRegCounter *) xmlRealloc(ctxt->counters, ctxt->maxCounters *
|
---|
1306 | sizeof(xmlRegCounter));
|
---|
1307 | if (tmp == NULL) {
|
---|
1308 | xmlRegexpErrMemory(ctxt, "allocating counter");
|
---|
1309 | ctxt->maxCounters /= 2;
|
---|
1310 | return(-1);
|
---|
1311 | }
|
---|
1312 | ctxt->counters = tmp;
|
---|
1313 | }
|
---|
1314 | ctxt->counters[ctxt->nbCounters].min = -1;
|
---|
1315 | ctxt->counters[ctxt->nbCounters].max = -1;
|
---|
1316 | return(ctxt->nbCounters++);
|
---|
1317 | }
|
---|
1318 |
|
---|
1319 | static int
|
---|
1320 | xmlRegAtomPush(xmlRegParserCtxtPtr ctxt, xmlRegAtomPtr atom) {
|
---|
1321 | if (atom == NULL) {
|
---|
1322 | ERROR("atom push: atom is NULL");
|
---|
1323 | return(-1);
|
---|
1324 | }
|
---|
1325 | if (ctxt->maxAtoms == 0) {
|
---|
1326 | ctxt->maxAtoms = 4;
|
---|
1327 | ctxt->atoms = (xmlRegAtomPtr *) xmlMalloc(ctxt->maxAtoms *
|
---|
1328 | sizeof(xmlRegAtomPtr));
|
---|
1329 | if (ctxt->atoms == NULL) {
|
---|
1330 | xmlRegexpErrMemory(ctxt, "pushing atom");
|
---|
1331 | ctxt->maxAtoms = 0;
|
---|
1332 | return(-1);
|
---|
1333 | }
|
---|
1334 | } else if (ctxt->nbAtoms >= ctxt->maxAtoms) {
|
---|
1335 | xmlRegAtomPtr *tmp;
|
---|
1336 | ctxt->maxAtoms *= 2;
|
---|
1337 | tmp = (xmlRegAtomPtr *) xmlRealloc(ctxt->atoms, ctxt->maxAtoms *
|
---|
1338 | sizeof(xmlRegAtomPtr));
|
---|
1339 | if (tmp == NULL) {
|
---|
1340 | xmlRegexpErrMemory(ctxt, "allocating counter");
|
---|
1341 | ctxt->maxAtoms /= 2;
|
---|
1342 | return(-1);
|
---|
1343 | }
|
---|
1344 | ctxt->atoms = tmp;
|
---|
1345 | }
|
---|
1346 | atom->no = ctxt->nbAtoms;
|
---|
1347 | ctxt->atoms[ctxt->nbAtoms++] = atom;
|
---|
1348 | return(0);
|
---|
1349 | }
|
---|
1350 |
|
---|
1351 | static void
|
---|
1352 | xmlRegStateAddTransTo(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr target,
|
---|
1353 | int from) {
|
---|
1354 | if (target->maxTransTo == 0) {
|
---|
1355 | target->maxTransTo = 8;
|
---|
1356 | target->transTo = (int *) xmlMalloc(target->maxTransTo *
|
---|
1357 | sizeof(int));
|
---|
1358 | if (target->transTo == NULL) {
|
---|
1359 | xmlRegexpErrMemory(ctxt, "adding transition");
|
---|
1360 | target->maxTransTo = 0;
|
---|
1361 | return;
|
---|
1362 | }
|
---|
1363 | } else if (target->nbTransTo >= target->maxTransTo) {
|
---|
1364 | int *tmp;
|
---|
1365 | target->maxTransTo *= 2;
|
---|
1366 | tmp = (int *) xmlRealloc(target->transTo, target->maxTransTo *
|
---|
1367 | sizeof(int));
|
---|
1368 | if (tmp == NULL) {
|
---|
1369 | xmlRegexpErrMemory(ctxt, "adding transition");
|
---|
1370 | target->maxTransTo /= 2;
|
---|
1371 | return;
|
---|
1372 | }
|
---|
1373 | target->transTo = tmp;
|
---|
1374 | }
|
---|
1375 | target->transTo[target->nbTransTo] = from;
|
---|
1376 | target->nbTransTo++;
|
---|
1377 | }
|
---|
1378 |
|
---|
1379 | static void
|
---|
1380 | xmlRegStateAddTrans(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
|
---|
1381 | xmlRegAtomPtr atom, xmlRegStatePtr target,
|
---|
1382 | int counter, int count) {
|
---|
1383 |
|
---|
1384 | int nrtrans;
|
---|
1385 |
|
---|
1386 | if (state == NULL) {
|
---|
1387 | ERROR("add state: state is NULL");
|
---|
1388 | return;
|
---|
1389 | }
|
---|
1390 | if (target == NULL) {
|
---|
1391 | ERROR("add state: target is NULL");
|
---|
1392 | return;
|
---|
1393 | }
|
---|
1394 | /*
|
---|
1395 | * Other routines follow the philosophy 'When in doubt, add a transition'
|
---|
1396 | * so we check here whether such a transition is already present and, if
|
---|
1397 | * so, silently ignore this request.
|
---|
1398 | */
|
---|
1399 |
|
---|
1400 | for (nrtrans = state->nbTrans - 1; nrtrans >= 0; nrtrans--) {
|
---|
1401 | xmlRegTransPtr trans = &(state->trans[nrtrans]);
|
---|
1402 | if ((trans->atom == atom) &&
|
---|
1403 | (trans->to == target->no) &&
|
---|
1404 | (trans->counter == counter) &&
|
---|
1405 | (trans->count == count)) {
|
---|
1406 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1407 | printf("Ignoring duplicate transition from %d to %d\n",
|
---|
1408 | state->no, target->no);
|
---|
1409 | #endif
|
---|
1410 | return;
|
---|
1411 | }
|
---|
1412 | }
|
---|
1413 |
|
---|
1414 | if (state->maxTrans == 0) {
|
---|
1415 | state->maxTrans = 8;
|
---|
1416 | state->trans = (xmlRegTrans *) xmlMalloc(state->maxTrans *
|
---|
1417 | sizeof(xmlRegTrans));
|
---|
1418 | if (state->trans == NULL) {
|
---|
1419 | xmlRegexpErrMemory(ctxt, "adding transition");
|
---|
1420 | state->maxTrans = 0;
|
---|
1421 | return;
|
---|
1422 | }
|
---|
1423 | } else if (state->nbTrans >= state->maxTrans) {
|
---|
1424 | xmlRegTrans *tmp;
|
---|
1425 | state->maxTrans *= 2;
|
---|
1426 | tmp = (xmlRegTrans *) xmlRealloc(state->trans, state->maxTrans *
|
---|
1427 | sizeof(xmlRegTrans));
|
---|
1428 | if (tmp == NULL) {
|
---|
1429 | xmlRegexpErrMemory(ctxt, "adding transition");
|
---|
1430 | state->maxTrans /= 2;
|
---|
1431 | return;
|
---|
1432 | }
|
---|
1433 | state->trans = tmp;
|
---|
1434 | }
|
---|
1435 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1436 | printf("Add trans from %d to %d ", state->no, target->no);
|
---|
1437 | if (count == REGEXP_ALL_COUNTER)
|
---|
1438 | printf("all transition\n");
|
---|
1439 | else if (count >= 0)
|
---|
1440 | printf("count based %d\n", count);
|
---|
1441 | else if (counter >= 0)
|
---|
1442 | printf("counted %d\n", counter);
|
---|
1443 | else if (atom == NULL)
|
---|
1444 | printf("epsilon transition\n");
|
---|
1445 | else if (atom != NULL)
|
---|
1446 | xmlRegPrintAtom(stdout, atom);
|
---|
1447 | #endif
|
---|
1448 |
|
---|
1449 | state->trans[state->nbTrans].atom = atom;
|
---|
1450 | state->trans[state->nbTrans].to = target->no;
|
---|
1451 | state->trans[state->nbTrans].counter = counter;
|
---|
1452 | state->trans[state->nbTrans].count = count;
|
---|
1453 | state->trans[state->nbTrans].nd = 0;
|
---|
1454 | state->nbTrans++;
|
---|
1455 | xmlRegStateAddTransTo(ctxt, target, state->no);
|
---|
1456 | }
|
---|
1457 |
|
---|
1458 | static int
|
---|
1459 | xmlRegStatePush(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
|
---|
1460 | if (state == NULL) return(-1);
|
---|
1461 | if (ctxt->maxStates == 0) {
|
---|
1462 | ctxt->maxStates = 4;
|
---|
1463 | ctxt->states = (xmlRegStatePtr *) xmlMalloc(ctxt->maxStates *
|
---|
1464 | sizeof(xmlRegStatePtr));
|
---|
1465 | if (ctxt->states == NULL) {
|
---|
1466 | xmlRegexpErrMemory(ctxt, "adding state");
|
---|
1467 | ctxt->maxStates = 0;
|
---|
1468 | return(-1);
|
---|
1469 | }
|
---|
1470 | } else if (ctxt->nbStates >= ctxt->maxStates) {
|
---|
1471 | xmlRegStatePtr *tmp;
|
---|
1472 | ctxt->maxStates *= 2;
|
---|
1473 | tmp = (xmlRegStatePtr *) xmlRealloc(ctxt->states, ctxt->maxStates *
|
---|
1474 | sizeof(xmlRegStatePtr));
|
---|
1475 | if (tmp == NULL) {
|
---|
1476 | xmlRegexpErrMemory(ctxt, "adding state");
|
---|
1477 | ctxt->maxStates /= 2;
|
---|
1478 | return(-1);
|
---|
1479 | }
|
---|
1480 | ctxt->states = tmp;
|
---|
1481 | }
|
---|
1482 | state->no = ctxt->nbStates;
|
---|
1483 | ctxt->states[ctxt->nbStates++] = state;
|
---|
1484 | return(0);
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | /**
|
---|
1488 | * xmlFAGenerateAllTransition:
|
---|
1489 | * @ctxt: a regexp parser context
|
---|
1490 | * @from: the from state
|
---|
1491 | * @to: the target state or NULL for building a new one
|
---|
1492 | * @lax:
|
---|
1493 | *
|
---|
1494 | */
|
---|
1495 | static void
|
---|
1496 | xmlFAGenerateAllTransition(xmlRegParserCtxtPtr ctxt,
|
---|
1497 | xmlRegStatePtr from, xmlRegStatePtr to,
|
---|
1498 | int lax) {
|
---|
1499 | if (to == NULL) {
|
---|
1500 | to = xmlRegNewState(ctxt);
|
---|
1501 | xmlRegStatePush(ctxt, to);
|
---|
1502 | ctxt->state = to;
|
---|
1503 | }
|
---|
1504 | if (lax)
|
---|
1505 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_LAX_COUNTER);
|
---|
1506 | else
|
---|
1507 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, REGEXP_ALL_COUNTER);
|
---|
1508 | }
|
---|
1509 |
|
---|
1510 | /**
|
---|
1511 | * xmlFAGenerateEpsilonTransition:
|
---|
1512 | * @ctxt: a regexp parser context
|
---|
1513 | * @from: the from state
|
---|
1514 | * @to: the target state or NULL for building a new one
|
---|
1515 | *
|
---|
1516 | */
|
---|
1517 | static void
|
---|
1518 | xmlFAGenerateEpsilonTransition(xmlRegParserCtxtPtr ctxt,
|
---|
1519 | xmlRegStatePtr from, xmlRegStatePtr to) {
|
---|
1520 | if (to == NULL) {
|
---|
1521 | to = xmlRegNewState(ctxt);
|
---|
1522 | xmlRegStatePush(ctxt, to);
|
---|
1523 | ctxt->state = to;
|
---|
1524 | }
|
---|
1525 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, -1);
|
---|
1526 | }
|
---|
1527 |
|
---|
1528 | /**
|
---|
1529 | * xmlFAGenerateCountedEpsilonTransition:
|
---|
1530 | * @ctxt: a regexp parser context
|
---|
1531 | * @from: the from state
|
---|
1532 | * @to: the target state or NULL for building a new one
|
---|
1533 | * counter: the counter for that transition
|
---|
1534 | *
|
---|
1535 | */
|
---|
1536 | static void
|
---|
1537 | xmlFAGenerateCountedEpsilonTransition(xmlRegParserCtxtPtr ctxt,
|
---|
1538 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
|
---|
1539 | if (to == NULL) {
|
---|
1540 | to = xmlRegNewState(ctxt);
|
---|
1541 | xmlRegStatePush(ctxt, to);
|
---|
1542 | ctxt->state = to;
|
---|
1543 | }
|
---|
1544 | xmlRegStateAddTrans(ctxt, from, NULL, to, counter, -1);
|
---|
1545 | }
|
---|
1546 |
|
---|
1547 | /**
|
---|
1548 | * xmlFAGenerateCountedTransition:
|
---|
1549 | * @ctxt: a regexp parser context
|
---|
1550 | * @from: the from state
|
---|
1551 | * @to: the target state or NULL for building a new one
|
---|
1552 | * counter: the counter for that transition
|
---|
1553 | *
|
---|
1554 | */
|
---|
1555 | static void
|
---|
1556 | xmlFAGenerateCountedTransition(xmlRegParserCtxtPtr ctxt,
|
---|
1557 | xmlRegStatePtr from, xmlRegStatePtr to, int counter) {
|
---|
1558 | if (to == NULL) {
|
---|
1559 | to = xmlRegNewState(ctxt);
|
---|
1560 | xmlRegStatePush(ctxt, to);
|
---|
1561 | ctxt->state = to;
|
---|
1562 | }
|
---|
1563 | xmlRegStateAddTrans(ctxt, from, NULL, to, -1, counter);
|
---|
1564 | }
|
---|
1565 |
|
---|
1566 | /**
|
---|
1567 | * xmlFAGenerateTransitions:
|
---|
1568 | * @ctxt: a regexp parser context
|
---|
1569 | * @from: the from state
|
---|
1570 | * @to: the target state or NULL for building a new one
|
---|
1571 | * @atom: the atom generating the transition
|
---|
1572 | *
|
---|
1573 | * Returns 0 if success and -1 in case of error.
|
---|
1574 | */
|
---|
1575 | static int
|
---|
1576 | xmlFAGenerateTransitions(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr from,
|
---|
1577 | xmlRegStatePtr to, xmlRegAtomPtr atom) {
|
---|
1578 | xmlRegStatePtr end;
|
---|
1579 | int nullable = 0;
|
---|
1580 |
|
---|
1581 | if (atom == NULL) {
|
---|
1582 | ERROR("generate transition: atom == NULL");
|
---|
1583 | return(-1);
|
---|
1584 | }
|
---|
1585 | if (atom->type == XML_REGEXP_SUBREG) {
|
---|
1586 | /*
|
---|
1587 | * this is a subexpression handling one should not need to
|
---|
1588 | * create a new node except for XML_REGEXP_QUANT_RANGE.
|
---|
1589 | */
|
---|
1590 | if (xmlRegAtomPush(ctxt, atom) < 0) {
|
---|
1591 | return(-1);
|
---|
1592 | }
|
---|
1593 | if ((to != NULL) && (atom->stop != to) &&
|
---|
1594 | (atom->quant != XML_REGEXP_QUANT_RANGE)) {
|
---|
1595 | /*
|
---|
1596 | * Generate an epsilon transition to link to the target
|
---|
1597 | */
|
---|
1598 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
|
---|
1599 | #ifdef DV
|
---|
1600 | } else if ((to == NULL) && (atom->quant != XML_REGEXP_QUANT_RANGE) &&
|
---|
1601 | (atom->quant != XML_REGEXP_QUANT_ONCE)) {
|
---|
1602 | to = xmlRegNewState(ctxt);
|
---|
1603 | xmlRegStatePush(ctxt, to);
|
---|
1604 | ctxt->state = to;
|
---|
1605 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, to);
|
---|
1606 | #endif
|
---|
1607 | }
|
---|
1608 | switch (atom->quant) {
|
---|
1609 | case XML_REGEXP_QUANT_OPT:
|
---|
1610 | atom->quant = XML_REGEXP_QUANT_ONCE;
|
---|
1611 | /*
|
---|
1612 | * transition done to the state after end of atom.
|
---|
1613 | * 1. set transition from atom start to new state
|
---|
1614 | * 2. set transition from atom end to this state.
|
---|
1615 | */
|
---|
1616 | if (to == NULL) {
|
---|
1617 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, 0);
|
---|
1618 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop,
|
---|
1619 | ctxt->state);
|
---|
1620 | } else {
|
---|
1621 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, to);
|
---|
1622 | }
|
---|
1623 | break;
|
---|
1624 | case XML_REGEXP_QUANT_MULT:
|
---|
1625 | atom->quant = XML_REGEXP_QUANT_ONCE;
|
---|
1626 | xmlFAGenerateEpsilonTransition(ctxt, atom->start, atom->stop);
|
---|
1627 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
|
---|
1628 | break;
|
---|
1629 | case XML_REGEXP_QUANT_PLUS:
|
---|
1630 | atom->quant = XML_REGEXP_QUANT_ONCE;
|
---|
1631 | xmlFAGenerateEpsilonTransition(ctxt, atom->stop, atom->start);
|
---|
1632 | break;
|
---|
1633 | case XML_REGEXP_QUANT_RANGE: {
|
---|
1634 | int counter;
|
---|
1635 | xmlRegStatePtr inter, newstate;
|
---|
1636 |
|
---|
1637 | /*
|
---|
1638 | * create the final state now if needed
|
---|
1639 | */
|
---|
1640 | if (to != NULL) {
|
---|
1641 | newstate = to;
|
---|
1642 | } else {
|
---|
1643 | newstate = xmlRegNewState(ctxt);
|
---|
1644 | xmlRegStatePush(ctxt, newstate);
|
---|
1645 | }
|
---|
1646 |
|
---|
1647 | /*
|
---|
1648 | * The principle here is to use counted transition
|
---|
1649 | * to avoid explosion in the number of states in the
|
---|
1650 | * graph. This is clearly more complex but should not
|
---|
1651 | * be exploitable at runtime.
|
---|
1652 | */
|
---|
1653 | if ((atom->min == 0) && (atom->start0 == NULL)) {
|
---|
1654 | xmlRegAtomPtr copy;
|
---|
1655 | /*
|
---|
1656 | * duplicate a transition based on atom to count next
|
---|
1657 | * occurrences after 1. We cannot loop to atom->start
|
---|
1658 | * directly because we need an epsilon transition to
|
---|
1659 | * newstate.
|
---|
1660 | */
|
---|
1661 | /* ???? For some reason it seems we never reach that
|
---|
1662 | case, I suppose this got optimized out before when
|
---|
1663 | building the automata */
|
---|
1664 | copy = xmlRegCopyAtom(ctxt, atom);
|
---|
1665 | if (copy == NULL)
|
---|
1666 | return(-1);
|
---|
1667 | copy->quant = XML_REGEXP_QUANT_ONCE;
|
---|
1668 | copy->min = 0;
|
---|
1669 | copy->max = 0;
|
---|
1670 |
|
---|
1671 | if (xmlFAGenerateTransitions(ctxt, atom->start, NULL, copy)
|
---|
1672 | < 0)
|
---|
1673 | return(-1);
|
---|
1674 | inter = ctxt->state;
|
---|
1675 | counter = xmlRegGetCounter(ctxt);
|
---|
1676 | ctxt->counters[counter].min = atom->min - 1;
|
---|
1677 | ctxt->counters[counter].max = atom->max - 1;
|
---|
1678 | /* count the number of times we see it again */
|
---|
1679 | xmlFAGenerateCountedEpsilonTransition(ctxt, inter,
|
---|
1680 | atom->stop, counter);
|
---|
1681 | /* allow a way out based on the count */
|
---|
1682 | xmlFAGenerateCountedTransition(ctxt, inter,
|
---|
1683 | newstate, counter);
|
---|
1684 | /* and also allow a direct exit for 0 */
|
---|
1685 | xmlFAGenerateEpsilonTransition(ctxt, atom->start,
|
---|
1686 | newstate);
|
---|
1687 | } else {
|
---|
1688 | /*
|
---|
1689 | * either we need the atom at least once or there
|
---|
1690 | * is an atom->start0 allowing to easily plug the
|
---|
1691 | * epsilon transition.
|
---|
1692 | */
|
---|
1693 | counter = xmlRegGetCounter(ctxt);
|
---|
1694 | ctxt->counters[counter].min = atom->min - 1;
|
---|
1695 | ctxt->counters[counter].max = atom->max - 1;
|
---|
1696 | /* allow a way out based on the count */
|
---|
1697 | xmlFAGenerateCountedTransition(ctxt, atom->stop,
|
---|
1698 | newstate, counter);
|
---|
1699 | /* count the number of times we see it again */
|
---|
1700 | xmlFAGenerateCountedEpsilonTransition(ctxt, atom->stop,
|
---|
1701 | atom->start, counter);
|
---|
1702 | /* and if needed allow a direct exit for 0 */
|
---|
1703 | if (atom->min == 0)
|
---|
1704 | xmlFAGenerateEpsilonTransition(ctxt, atom->start0,
|
---|
1705 | newstate);
|
---|
1706 |
|
---|
1707 | }
|
---|
1708 | atom->min = 0;
|
---|
1709 | atom->max = 0;
|
---|
1710 | atom->quant = XML_REGEXP_QUANT_ONCE;
|
---|
1711 | ctxt->state = newstate;
|
---|
1712 | }
|
---|
1713 | default:
|
---|
1714 | break;
|
---|
1715 | }
|
---|
1716 | return(0);
|
---|
1717 | }
|
---|
1718 | if ((atom->min == 0) && (atom->max == 0) &&
|
---|
1719 | (atom->quant == XML_REGEXP_QUANT_RANGE)) {
|
---|
1720 | /*
|
---|
1721 | * we can discard the atom and generate an epsilon transition instead
|
---|
1722 | */
|
---|
1723 | if (to == NULL) {
|
---|
1724 | to = xmlRegNewState(ctxt);
|
---|
1725 | if (to != NULL)
|
---|
1726 | xmlRegStatePush(ctxt, to);
|
---|
1727 | else {
|
---|
1728 | return(-1);
|
---|
1729 | }
|
---|
1730 | }
|
---|
1731 | xmlFAGenerateEpsilonTransition(ctxt, from, to);
|
---|
1732 | ctxt->state = to;
|
---|
1733 | xmlRegFreeAtom(atom);
|
---|
1734 | return(0);
|
---|
1735 | }
|
---|
1736 | if (to == NULL) {
|
---|
1737 | to = xmlRegNewState(ctxt);
|
---|
1738 | if (to != NULL)
|
---|
1739 | xmlRegStatePush(ctxt, to);
|
---|
1740 | else {
|
---|
1741 | return(-1);
|
---|
1742 | }
|
---|
1743 | }
|
---|
1744 | end = to;
|
---|
1745 | if ((atom->quant == XML_REGEXP_QUANT_MULT) ||
|
---|
1746 | (atom->quant == XML_REGEXP_QUANT_PLUS)) {
|
---|
1747 | /*
|
---|
1748 | * Do not pollute the target state by adding transitions from
|
---|
1749 | * it as it is likely to be the shared target of multiple branches.
|
---|
1750 | * So isolate with an epsilon transition.
|
---|
1751 | */
|
---|
1752 | xmlRegStatePtr tmp;
|
---|
1753 |
|
---|
1754 | tmp = xmlRegNewState(ctxt);
|
---|
1755 | if (tmp != NULL)
|
---|
1756 | xmlRegStatePush(ctxt, tmp);
|
---|
1757 | else {
|
---|
1758 | return(-1);
|
---|
1759 | }
|
---|
1760 | xmlFAGenerateEpsilonTransition(ctxt, tmp, to);
|
---|
1761 | to = tmp;
|
---|
1762 | }
|
---|
1763 | if (xmlRegAtomPush(ctxt, atom) < 0) {
|
---|
1764 | return(-1);
|
---|
1765 | }
|
---|
1766 | if ((atom->quant == XML_REGEXP_QUANT_RANGE) &&
|
---|
1767 | (atom->min == 0) && (atom->max > 0)) {
|
---|
1768 | nullable = 1;
|
---|
1769 | atom->min = 1;
|
---|
1770 | if (atom->max == 1)
|
---|
1771 | atom->quant = XML_REGEXP_QUANT_OPT;
|
---|
1772 | }
|
---|
1773 | xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1);
|
---|
1774 | ctxt->state = end;
|
---|
1775 | switch (atom->quant) {
|
---|
1776 | case XML_REGEXP_QUANT_OPT:
|
---|
1777 | atom->quant = XML_REGEXP_QUANT_ONCE;
|
---|
1778 | xmlFAGenerateEpsilonTransition(ctxt, from, to);
|
---|
1779 | break;
|
---|
1780 | case XML_REGEXP_QUANT_MULT:
|
---|
1781 | atom->quant = XML_REGEXP_QUANT_ONCE;
|
---|
1782 | xmlFAGenerateEpsilonTransition(ctxt, from, to);
|
---|
1783 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
|
---|
1784 | break;
|
---|
1785 | case XML_REGEXP_QUANT_PLUS:
|
---|
1786 | atom->quant = XML_REGEXP_QUANT_ONCE;
|
---|
1787 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1);
|
---|
1788 | break;
|
---|
1789 | case XML_REGEXP_QUANT_RANGE:
|
---|
1790 | if (nullable)
|
---|
1791 | xmlFAGenerateEpsilonTransition(ctxt, from, to);
|
---|
1792 | break;
|
---|
1793 | default:
|
---|
1794 | break;
|
---|
1795 | }
|
---|
1796 | return(0);
|
---|
1797 | }
|
---|
1798 |
|
---|
1799 | /**
|
---|
1800 | * xmlFAReduceEpsilonTransitions:
|
---|
1801 | * @ctxt: a regexp parser context
|
---|
1802 | * @fromnr: the from state
|
---|
1803 | * @tonr: the to state
|
---|
1804 | * @counter: should that transition be associated to a counted
|
---|
1805 | *
|
---|
1806 | */
|
---|
1807 | static void
|
---|
1808 | xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr,
|
---|
1809 | int tonr, int counter) {
|
---|
1810 | int transnr;
|
---|
1811 | xmlRegStatePtr from;
|
---|
1812 | xmlRegStatePtr to;
|
---|
1813 |
|
---|
1814 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1815 | printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr);
|
---|
1816 | #endif
|
---|
1817 | from = ctxt->states[fromnr];
|
---|
1818 | if (from == NULL)
|
---|
1819 | return;
|
---|
1820 | to = ctxt->states[tonr];
|
---|
1821 | if (to == NULL)
|
---|
1822 | return;
|
---|
1823 | if ((to->mark == XML_REGEXP_MARK_START) ||
|
---|
1824 | (to->mark == XML_REGEXP_MARK_VISITED))
|
---|
1825 | return;
|
---|
1826 |
|
---|
1827 | to->mark = XML_REGEXP_MARK_VISITED;
|
---|
1828 | if (to->type == XML_REGEXP_FINAL_STATE) {
|
---|
1829 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1830 | printf("State %d is final, so %d becomes final\n", tonr, fromnr);
|
---|
1831 | #endif
|
---|
1832 | from->type = XML_REGEXP_FINAL_STATE;
|
---|
1833 | }
|
---|
1834 | for (transnr = 0;transnr < to->nbTrans;transnr++) {
|
---|
1835 | if (to->trans[transnr].to < 0)
|
---|
1836 | continue;
|
---|
1837 | if (to->trans[transnr].atom == NULL) {
|
---|
1838 | /*
|
---|
1839 | * Don't remove counted transitions
|
---|
1840 | * Don't loop either
|
---|
1841 | */
|
---|
1842 | if (to->trans[transnr].to != fromnr) {
|
---|
1843 | if (to->trans[transnr].count >= 0) {
|
---|
1844 | int newto = to->trans[transnr].to;
|
---|
1845 |
|
---|
1846 | xmlRegStateAddTrans(ctxt, from, NULL,
|
---|
1847 | ctxt->states[newto],
|
---|
1848 | -1, to->trans[transnr].count);
|
---|
1849 | } else {
|
---|
1850 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1851 | printf("Found epsilon trans %d from %d to %d\n",
|
---|
1852 | transnr, tonr, to->trans[transnr].to);
|
---|
1853 | #endif
|
---|
1854 | if (to->trans[transnr].counter >= 0) {
|
---|
1855 | xmlFAReduceEpsilonTransitions(ctxt, fromnr,
|
---|
1856 | to->trans[transnr].to,
|
---|
1857 | to->trans[transnr].counter);
|
---|
1858 | } else {
|
---|
1859 | xmlFAReduceEpsilonTransitions(ctxt, fromnr,
|
---|
1860 | to->trans[transnr].to,
|
---|
1861 | counter);
|
---|
1862 | }
|
---|
1863 | }
|
---|
1864 | }
|
---|
1865 | } else {
|
---|
1866 | int newto = to->trans[transnr].to;
|
---|
1867 |
|
---|
1868 | if (to->trans[transnr].counter >= 0) {
|
---|
1869 | xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
|
---|
1870 | ctxt->states[newto],
|
---|
1871 | to->trans[transnr].counter, -1);
|
---|
1872 | } else {
|
---|
1873 | xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom,
|
---|
1874 | ctxt->states[newto], counter, -1);
|
---|
1875 | }
|
---|
1876 | }
|
---|
1877 | }
|
---|
1878 | to->mark = XML_REGEXP_MARK_NORMAL;
|
---|
1879 | }
|
---|
1880 |
|
---|
1881 | /**
|
---|
1882 | * xmlFAEliminateSimpleEpsilonTransitions:
|
---|
1883 | * @ctxt: a regexp parser context
|
---|
1884 | *
|
---|
1885 | * Eliminating general epsilon transitions can get costly in the general
|
---|
1886 | * algorithm due to the large amount of generated new transitions and
|
---|
1887 | * associated comparisons. However for simple epsilon transition used just
|
---|
1888 | * to separate building blocks when generating the automata this can be
|
---|
1889 | * reduced to state elimination:
|
---|
1890 | * - if there exists an epsilon from X to Y
|
---|
1891 | * - if there is no other transition from X
|
---|
1892 | * then X and Y are semantically equivalent and X can be eliminated
|
---|
1893 | * If X is the start state then make Y the start state, else replace the
|
---|
1894 | * target of all transitions to X by transitions to Y.
|
---|
1895 | *
|
---|
1896 | * If X is a final state, skip it.
|
---|
1897 | * Otherwise it would be necessary to manipulate counters for this case when
|
---|
1898 | * eliminating state 2:
|
---|
1899 | * State 1 has a transition with an atom to state 2.
|
---|
1900 | * State 2 is final and has an epsilon transition to state 1.
|
---|
1901 | */
|
---|
1902 | static void
|
---|
1903 | xmlFAEliminateSimpleEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
|
---|
1904 | int statenr, i, j, newto;
|
---|
1905 | xmlRegStatePtr state, tmp;
|
---|
1906 |
|
---|
1907 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
|
---|
1908 | state = ctxt->states[statenr];
|
---|
1909 | if (state == NULL)
|
---|
1910 | continue;
|
---|
1911 | if (state->nbTrans != 1)
|
---|
1912 | continue;
|
---|
1913 | if (state->type == XML_REGEXP_UNREACH_STATE ||
|
---|
1914 | state->type == XML_REGEXP_FINAL_STATE)
|
---|
1915 | continue;
|
---|
1916 | /* is the only transition out a basic transition */
|
---|
1917 | if ((state->trans[0].atom == NULL) &&
|
---|
1918 | (state->trans[0].to >= 0) &&
|
---|
1919 | (state->trans[0].to != statenr) &&
|
---|
1920 | (state->trans[0].counter < 0) &&
|
---|
1921 | (state->trans[0].count < 0)) {
|
---|
1922 | newto = state->trans[0].to;
|
---|
1923 |
|
---|
1924 | if (state->type == XML_REGEXP_START_STATE) {
|
---|
1925 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1926 | printf("Found simple epsilon trans from start %d to %d\n",
|
---|
1927 | statenr, newto);
|
---|
1928 | #endif
|
---|
1929 | } else {
|
---|
1930 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1931 | printf("Found simple epsilon trans from %d to %d\n",
|
---|
1932 | statenr, newto);
|
---|
1933 | #endif
|
---|
1934 | for (i = 0;i < state->nbTransTo;i++) {
|
---|
1935 | tmp = ctxt->states[state->transTo[i]];
|
---|
1936 | for (j = 0;j < tmp->nbTrans;j++) {
|
---|
1937 | if (tmp->trans[j].to == statenr) {
|
---|
1938 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1939 | printf("Changed transition %d on %d to go to %d\n",
|
---|
1940 | j, tmp->no, newto);
|
---|
1941 | #endif
|
---|
1942 | tmp->trans[j].to = -1;
|
---|
1943 | xmlRegStateAddTrans(ctxt, tmp, tmp->trans[j].atom,
|
---|
1944 | ctxt->states[newto],
|
---|
1945 | tmp->trans[j].counter,
|
---|
1946 | tmp->trans[j].count);
|
---|
1947 | }
|
---|
1948 | }
|
---|
1949 | }
|
---|
1950 | if (state->type == XML_REGEXP_FINAL_STATE)
|
---|
1951 | ctxt->states[newto]->type = XML_REGEXP_FINAL_STATE;
|
---|
1952 | /* eliminate the transition completely */
|
---|
1953 | state->nbTrans = 0;
|
---|
1954 |
|
---|
1955 | state->type = XML_REGEXP_UNREACH_STATE;
|
---|
1956 |
|
---|
1957 | }
|
---|
1958 |
|
---|
1959 | }
|
---|
1960 | }
|
---|
1961 | }
|
---|
1962 | /**
|
---|
1963 | * xmlFAEliminateEpsilonTransitions:
|
---|
1964 | * @ctxt: a regexp parser context
|
---|
1965 | *
|
---|
1966 | */
|
---|
1967 | static void
|
---|
1968 | xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) {
|
---|
1969 | int statenr, transnr;
|
---|
1970 | xmlRegStatePtr state;
|
---|
1971 | int has_epsilon;
|
---|
1972 |
|
---|
1973 | if (ctxt->states == NULL) return;
|
---|
1974 |
|
---|
1975 | /*
|
---|
1976 | * Eliminate simple epsilon transition and the associated unreachable
|
---|
1977 | * states.
|
---|
1978 | */
|
---|
1979 | xmlFAEliminateSimpleEpsilonTransitions(ctxt);
|
---|
1980 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
|
---|
1981 | state = ctxt->states[statenr];
|
---|
1982 | if ((state != NULL) && (state->type == XML_REGEXP_UNREACH_STATE)) {
|
---|
1983 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
1984 | printf("Removed unreachable state %d\n", statenr);
|
---|
1985 | #endif
|
---|
1986 | xmlRegFreeState(state);
|
---|
1987 | ctxt->states[statenr] = NULL;
|
---|
1988 | }
|
---|
1989 | }
|
---|
1990 |
|
---|
1991 | has_epsilon = 0;
|
---|
1992 |
|
---|
1993 | /*
|
---|
1994 | * Build the completed transitions bypassing the epsilons
|
---|
1995 | * Use a marking algorithm to avoid loops
|
---|
1996 | * Mark sink states too.
|
---|
1997 | * Process from the latest states backward to the start when
|
---|
1998 | * there is long cascading epsilon chains this minimize the
|
---|
1999 | * recursions and transition compares when adding the new ones
|
---|
2000 | */
|
---|
2001 | for (statenr = ctxt->nbStates - 1;statenr >= 0;statenr--) {
|
---|
2002 | state = ctxt->states[statenr];
|
---|
2003 | if (state == NULL)
|
---|
2004 | continue;
|
---|
2005 | if ((state->nbTrans == 0) &&
|
---|
2006 | (state->type != XML_REGEXP_FINAL_STATE)) {
|
---|
2007 | state->type = XML_REGEXP_SINK_STATE;
|
---|
2008 | }
|
---|
2009 | for (transnr = 0;transnr < state->nbTrans;transnr++) {
|
---|
2010 | if ((state->trans[transnr].atom == NULL) &&
|
---|
2011 | (state->trans[transnr].to >= 0)) {
|
---|
2012 | if (state->trans[transnr].to == statenr) {
|
---|
2013 | state->trans[transnr].to = -1;
|
---|
2014 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
2015 | printf("Removed loopback epsilon trans %d on %d\n",
|
---|
2016 | transnr, statenr);
|
---|
2017 | #endif
|
---|
2018 | } else if (state->trans[transnr].count < 0) {
|
---|
2019 | int newto = state->trans[transnr].to;
|
---|
2020 |
|
---|
2021 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
2022 | printf("Found epsilon trans %d from %d to %d\n",
|
---|
2023 | transnr, statenr, newto);
|
---|
2024 | #endif
|
---|
2025 | has_epsilon = 1;
|
---|
2026 | state->trans[transnr].to = -2;
|
---|
2027 | state->mark = XML_REGEXP_MARK_START;
|
---|
2028 | xmlFAReduceEpsilonTransitions(ctxt, statenr,
|
---|
2029 | newto, state->trans[transnr].counter);
|
---|
2030 | state->mark = XML_REGEXP_MARK_NORMAL;
|
---|
2031 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
2032 | } else {
|
---|
2033 | printf("Found counted transition %d on %d\n",
|
---|
2034 | transnr, statenr);
|
---|
2035 | #endif
|
---|
2036 | }
|
---|
2037 | }
|
---|
2038 | }
|
---|
2039 | }
|
---|
2040 | /*
|
---|
2041 | * Eliminate the epsilon transitions
|
---|
2042 | */
|
---|
2043 | if (has_epsilon) {
|
---|
2044 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
|
---|
2045 | state = ctxt->states[statenr];
|
---|
2046 | if (state == NULL)
|
---|
2047 | continue;
|
---|
2048 | for (transnr = 0;transnr < state->nbTrans;transnr++) {
|
---|
2049 | xmlRegTransPtr trans = &(state->trans[transnr]);
|
---|
2050 | if ((trans->atom == NULL) &&
|
---|
2051 | (trans->count < 0) &&
|
---|
2052 | (trans->to >= 0)) {
|
---|
2053 | trans->to = -1;
|
---|
2054 | }
|
---|
2055 | }
|
---|
2056 | }
|
---|
2057 | }
|
---|
2058 |
|
---|
2059 | /*
|
---|
2060 | * Use this pass to detect unreachable states too
|
---|
2061 | */
|
---|
2062 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
|
---|
2063 | state = ctxt->states[statenr];
|
---|
2064 | if (state != NULL)
|
---|
2065 | state->reached = XML_REGEXP_MARK_NORMAL;
|
---|
2066 | }
|
---|
2067 | state = ctxt->states[0];
|
---|
2068 | if (state != NULL)
|
---|
2069 | state->reached = XML_REGEXP_MARK_START;
|
---|
2070 | while (state != NULL) {
|
---|
2071 | xmlRegStatePtr target = NULL;
|
---|
2072 | state->reached = XML_REGEXP_MARK_VISITED;
|
---|
2073 | /*
|
---|
2074 | * Mark all states reachable from the current reachable state
|
---|
2075 | */
|
---|
2076 | for (transnr = 0;transnr < state->nbTrans;transnr++) {
|
---|
2077 | if ((state->trans[transnr].to >= 0) &&
|
---|
2078 | ((state->trans[transnr].atom != NULL) ||
|
---|
2079 | (state->trans[transnr].count >= 0))) {
|
---|
2080 | int newto = state->trans[transnr].to;
|
---|
2081 |
|
---|
2082 | if (ctxt->states[newto] == NULL)
|
---|
2083 | continue;
|
---|
2084 | if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) {
|
---|
2085 | ctxt->states[newto]->reached = XML_REGEXP_MARK_START;
|
---|
2086 | target = ctxt->states[newto];
|
---|
2087 | }
|
---|
2088 | }
|
---|
2089 | }
|
---|
2090 |
|
---|
2091 | /*
|
---|
2092 | * find the next accessible state not explored
|
---|
2093 | */
|
---|
2094 | if (target == NULL) {
|
---|
2095 | for (statenr = 1;statenr < ctxt->nbStates;statenr++) {
|
---|
2096 | state = ctxt->states[statenr];
|
---|
2097 | if ((state != NULL) && (state->reached ==
|
---|
2098 | XML_REGEXP_MARK_START)) {
|
---|
2099 | target = state;
|
---|
2100 | break;
|
---|
2101 | }
|
---|
2102 | }
|
---|
2103 | }
|
---|
2104 | state = target;
|
---|
2105 | }
|
---|
2106 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
|
---|
2107 | state = ctxt->states[statenr];
|
---|
2108 | if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) {
|
---|
2109 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
2110 | printf("Removed unreachable state %d\n", statenr);
|
---|
2111 | #endif
|
---|
2112 | xmlRegFreeState(state);
|
---|
2113 | ctxt->states[statenr] = NULL;
|
---|
2114 | }
|
---|
2115 | }
|
---|
2116 |
|
---|
2117 | }
|
---|
2118 |
|
---|
2119 | static int
|
---|
2120 | xmlFACompareRanges(xmlRegRangePtr range1, xmlRegRangePtr range2) {
|
---|
2121 | int ret = 0;
|
---|
2122 |
|
---|
2123 | if ((range1->type == XML_REGEXP_RANGES) ||
|
---|
2124 | (range2->type == XML_REGEXP_RANGES) ||
|
---|
2125 | (range2->type == XML_REGEXP_SUBREG) ||
|
---|
2126 | (range1->type == XML_REGEXP_SUBREG) ||
|
---|
2127 | (range1->type == XML_REGEXP_STRING) ||
|
---|
2128 | (range2->type == XML_REGEXP_STRING))
|
---|
2129 | return(-1);
|
---|
2130 |
|
---|
2131 | /* put them in order */
|
---|
2132 | if (range1->type > range2->type) {
|
---|
2133 | xmlRegRangePtr tmp;
|
---|
2134 |
|
---|
2135 | tmp = range1;
|
---|
2136 | range1 = range2;
|
---|
2137 | range2 = tmp;
|
---|
2138 | }
|
---|
2139 | if ((range1->type == XML_REGEXP_ANYCHAR) ||
|
---|
2140 | (range2->type == XML_REGEXP_ANYCHAR)) {
|
---|
2141 | ret = 1;
|
---|
2142 | } else if ((range1->type == XML_REGEXP_EPSILON) ||
|
---|
2143 | (range2->type == XML_REGEXP_EPSILON)) {
|
---|
2144 | return(0);
|
---|
2145 | } else if (range1->type == range2->type) {
|
---|
2146 | if (range1->type != XML_REGEXP_CHARVAL)
|
---|
2147 | ret = 1;
|
---|
2148 | else if ((range1->end < range2->start) ||
|
---|
2149 | (range2->end < range1->start))
|
---|
2150 | ret = 0;
|
---|
2151 | else
|
---|
2152 | ret = 1;
|
---|
2153 | } else if (range1->type == XML_REGEXP_CHARVAL) {
|
---|
2154 | int codepoint;
|
---|
2155 | int neg = 0;
|
---|
2156 |
|
---|
2157 | /*
|
---|
2158 | * just check all codepoints in the range for acceptance,
|
---|
2159 | * this is usually way cheaper since done only once at
|
---|
2160 | * compilation than testing over and over at runtime or
|
---|
2161 | * pushing too many states when evaluating.
|
---|
2162 | */
|
---|
2163 | if (((range1->neg == 0) && (range2->neg != 0)) ||
|
---|
2164 | ((range1->neg != 0) && (range2->neg == 0)))
|
---|
2165 | neg = 1;
|
---|
2166 |
|
---|
2167 | for (codepoint = range1->start;codepoint <= range1->end ;codepoint++) {
|
---|
2168 | ret = xmlRegCheckCharacterRange(range2->type, codepoint,
|
---|
2169 | 0, range2->start, range2->end,
|
---|
2170 | range2->blockName);
|
---|
2171 | if (ret < 0)
|
---|
2172 | return(-1);
|
---|
2173 | if (((neg == 1) && (ret == 0)) ||
|
---|
2174 | ((neg == 0) && (ret == 1)))
|
---|
2175 | return(1);
|
---|
2176 | }
|
---|
2177 | return(0);
|
---|
2178 | } else if ((range1->type == XML_REGEXP_BLOCK_NAME) ||
|
---|
2179 | (range2->type == XML_REGEXP_BLOCK_NAME)) {
|
---|
2180 | if (range1->type == range2->type) {
|
---|
2181 | ret = xmlStrEqual(range1->blockName, range2->blockName);
|
---|
2182 | } else {
|
---|
2183 | /*
|
---|
2184 | * comparing a block range with anything else is way
|
---|
2185 | * too costly, and maintaining the table is like too much
|
---|
2186 | * memory too, so let's force the automata to save state
|
---|
2187 | * here.
|
---|
2188 | */
|
---|
2189 | return(1);
|
---|
2190 | }
|
---|
2191 | } else if ((range1->type < XML_REGEXP_LETTER) ||
|
---|
2192 | (range2->type < XML_REGEXP_LETTER)) {
|
---|
2193 | if ((range1->type == XML_REGEXP_ANYSPACE) &&
|
---|
2194 | (range2->type == XML_REGEXP_NOTSPACE))
|
---|
2195 | ret = 0;
|
---|
2196 | else if ((range1->type == XML_REGEXP_INITNAME) &&
|
---|
2197 | (range2->type == XML_REGEXP_NOTINITNAME))
|
---|
2198 | ret = 0;
|
---|
2199 | else if ((range1->type == XML_REGEXP_NAMECHAR) &&
|
---|
2200 | (range2->type == XML_REGEXP_NOTNAMECHAR))
|
---|
2201 | ret = 0;
|
---|
2202 | else if ((range1->type == XML_REGEXP_DECIMAL) &&
|
---|
2203 | (range2->type == XML_REGEXP_NOTDECIMAL))
|
---|
2204 | ret = 0;
|
---|
2205 | else if ((range1->type == XML_REGEXP_REALCHAR) &&
|
---|
2206 | (range2->type == XML_REGEXP_NOTREALCHAR))
|
---|
2207 | ret = 0;
|
---|
2208 | else {
|
---|
2209 | /* same thing to limit complexity */
|
---|
2210 | return(1);
|
---|
2211 | }
|
---|
2212 | } else {
|
---|
2213 | ret = 0;
|
---|
2214 | /* range1->type < range2->type here */
|
---|
2215 | switch (range1->type) {
|
---|
2216 | case XML_REGEXP_LETTER:
|
---|
2217 | /* all disjoint except in the subgroups */
|
---|
2218 | if ((range2->type == XML_REGEXP_LETTER_UPPERCASE) ||
|
---|
2219 | (range2->type == XML_REGEXP_LETTER_LOWERCASE) ||
|
---|
2220 | (range2->type == XML_REGEXP_LETTER_TITLECASE) ||
|
---|
2221 | (range2->type == XML_REGEXP_LETTER_MODIFIER) ||
|
---|
2222 | (range2->type == XML_REGEXP_LETTER_OTHERS))
|
---|
2223 | ret = 1;
|
---|
2224 | break;
|
---|
2225 | case XML_REGEXP_MARK:
|
---|
2226 | if ((range2->type == XML_REGEXP_MARK_NONSPACING) ||
|
---|
2227 | (range2->type == XML_REGEXP_MARK_SPACECOMBINING) ||
|
---|
2228 | (range2->type == XML_REGEXP_MARK_ENCLOSING))
|
---|
2229 | ret = 1;
|
---|
2230 | break;
|
---|
2231 | case XML_REGEXP_NUMBER:
|
---|
2232 | if ((range2->type == XML_REGEXP_NUMBER_DECIMAL) ||
|
---|
2233 | (range2->type == XML_REGEXP_NUMBER_LETTER) ||
|
---|
2234 | (range2->type == XML_REGEXP_NUMBER_OTHERS))
|
---|
2235 | ret = 1;
|
---|
2236 | break;
|
---|
2237 | case XML_REGEXP_PUNCT:
|
---|
2238 | if ((range2->type == XML_REGEXP_PUNCT_CONNECTOR) ||
|
---|
2239 | (range2->type == XML_REGEXP_PUNCT_DASH) ||
|
---|
2240 | (range2->type == XML_REGEXP_PUNCT_OPEN) ||
|
---|
2241 | (range2->type == XML_REGEXP_PUNCT_CLOSE) ||
|
---|
2242 | (range2->type == XML_REGEXP_PUNCT_INITQUOTE) ||
|
---|
2243 | (range2->type == XML_REGEXP_PUNCT_FINQUOTE) ||
|
---|
2244 | (range2->type == XML_REGEXP_PUNCT_OTHERS))
|
---|
2245 | ret = 1;
|
---|
2246 | break;
|
---|
2247 | case XML_REGEXP_SEPAR:
|
---|
2248 | if ((range2->type == XML_REGEXP_SEPAR_SPACE) ||
|
---|
2249 | (range2->type == XML_REGEXP_SEPAR_LINE) ||
|
---|
2250 | (range2->type == XML_REGEXP_SEPAR_PARA))
|
---|
2251 | ret = 1;
|
---|
2252 | break;
|
---|
2253 | case XML_REGEXP_SYMBOL:
|
---|
2254 | if ((range2->type == XML_REGEXP_SYMBOL_MATH) ||
|
---|
2255 | (range2->type == XML_REGEXP_SYMBOL_CURRENCY) ||
|
---|
2256 | (range2->type == XML_REGEXP_SYMBOL_MODIFIER) ||
|
---|
2257 | (range2->type == XML_REGEXP_SYMBOL_OTHERS))
|
---|
2258 | ret = 1;
|
---|
2259 | break;
|
---|
2260 | case XML_REGEXP_OTHER:
|
---|
2261 | if ((range2->type == XML_REGEXP_OTHER_CONTROL) ||
|
---|
2262 | (range2->type == XML_REGEXP_OTHER_FORMAT) ||
|
---|
2263 | (range2->type == XML_REGEXP_OTHER_PRIVATE))
|
---|
2264 | ret = 1;
|
---|
2265 | break;
|
---|
2266 | default:
|
---|
2267 | if ((range2->type >= XML_REGEXP_LETTER) &&
|
---|
2268 | (range2->type < XML_REGEXP_BLOCK_NAME))
|
---|
2269 | ret = 0;
|
---|
2270 | else {
|
---|
2271 | /* safety net ! */
|
---|
2272 | return(1);
|
---|
2273 | }
|
---|
2274 | }
|
---|
2275 | }
|
---|
2276 | if (((range1->neg == 0) && (range2->neg != 0)) ||
|
---|
2277 | ((range1->neg != 0) && (range2->neg == 0)))
|
---|
2278 | ret = !ret;
|
---|
2279 | return(ret);
|
---|
2280 | }
|
---|
2281 |
|
---|
2282 | /**
|
---|
2283 | * xmlFACompareAtomTypes:
|
---|
2284 | * @type1: an atom type
|
---|
2285 | * @type2: an atom type
|
---|
2286 | *
|
---|
2287 | * Compares two atoms type to check whether they intersect in some ways,
|
---|
2288 | * this is used by xmlFACompareAtoms only
|
---|
2289 | *
|
---|
2290 | * Returns 1 if they may intersect and 0 otherwise
|
---|
2291 | */
|
---|
2292 | static int
|
---|
2293 | xmlFACompareAtomTypes(xmlRegAtomType type1, xmlRegAtomType type2) {
|
---|
2294 | if ((type1 == XML_REGEXP_EPSILON) ||
|
---|
2295 | (type1 == XML_REGEXP_CHARVAL) ||
|
---|
2296 | (type1 == XML_REGEXP_RANGES) ||
|
---|
2297 | (type1 == XML_REGEXP_SUBREG) ||
|
---|
2298 | (type1 == XML_REGEXP_STRING) ||
|
---|
2299 | (type1 == XML_REGEXP_ANYCHAR))
|
---|
2300 | return(1);
|
---|
2301 | if ((type2 == XML_REGEXP_EPSILON) ||
|
---|
2302 | (type2 == XML_REGEXP_CHARVAL) ||
|
---|
2303 | (type2 == XML_REGEXP_RANGES) ||
|
---|
2304 | (type2 == XML_REGEXP_SUBREG) ||
|
---|
2305 | (type2 == XML_REGEXP_STRING) ||
|
---|
2306 | (type2 == XML_REGEXP_ANYCHAR))
|
---|
2307 | return(1);
|
---|
2308 |
|
---|
2309 | if (type1 == type2) return(1);
|
---|
2310 |
|
---|
2311 | /* simplify subsequent compares by making sure type1 < type2 */
|
---|
2312 | if (type1 > type2) {
|
---|
2313 | xmlRegAtomType tmp = type1;
|
---|
2314 | type1 = type2;
|
---|
2315 | type2 = tmp;
|
---|
2316 | }
|
---|
2317 | switch (type1) {
|
---|
2318 | case XML_REGEXP_ANYSPACE: /* \s */
|
---|
2319 | /* can't be a letter, number, mark, punctuation, symbol */
|
---|
2320 | if ((type2 == XML_REGEXP_NOTSPACE) ||
|
---|
2321 | ((type2 >= XML_REGEXP_LETTER) &&
|
---|
2322 | (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
|
---|
2323 | ((type2 >= XML_REGEXP_NUMBER) &&
|
---|
2324 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
|
---|
2325 | ((type2 >= XML_REGEXP_MARK) &&
|
---|
2326 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
|
---|
2327 | ((type2 >= XML_REGEXP_PUNCT) &&
|
---|
2328 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
|
---|
2329 | ((type2 >= XML_REGEXP_SYMBOL) &&
|
---|
2330 | (type2 <= XML_REGEXP_SYMBOL_OTHERS))
|
---|
2331 | ) return(0);
|
---|
2332 | break;
|
---|
2333 | case XML_REGEXP_NOTSPACE: /* \S */
|
---|
2334 | break;
|
---|
2335 | case XML_REGEXP_INITNAME: /* \l */
|
---|
2336 | /* can't be a number, mark, separator, punctuation, symbol or other */
|
---|
2337 | if ((type2 == XML_REGEXP_NOTINITNAME) ||
|
---|
2338 | ((type2 >= XML_REGEXP_NUMBER) &&
|
---|
2339 | (type2 <= XML_REGEXP_NUMBER_OTHERS)) ||
|
---|
2340 | ((type2 >= XML_REGEXP_MARK) &&
|
---|
2341 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
|
---|
2342 | ((type2 >= XML_REGEXP_SEPAR) &&
|
---|
2343 | (type2 <= XML_REGEXP_SEPAR_PARA)) ||
|
---|
2344 | ((type2 >= XML_REGEXP_PUNCT) &&
|
---|
2345 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
|
---|
2346 | ((type2 >= XML_REGEXP_SYMBOL) &&
|
---|
2347 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
|
---|
2348 | ((type2 >= XML_REGEXP_OTHER) &&
|
---|
2349 | (type2 <= XML_REGEXP_OTHER_NA))
|
---|
2350 | ) return(0);
|
---|
2351 | break;
|
---|
2352 | case XML_REGEXP_NOTINITNAME: /* \L */
|
---|
2353 | break;
|
---|
2354 | case XML_REGEXP_NAMECHAR: /* \c */
|
---|
2355 | /* can't be a mark, separator, punctuation, symbol or other */
|
---|
2356 | if ((type2 == XML_REGEXP_NOTNAMECHAR) ||
|
---|
2357 | ((type2 >= XML_REGEXP_MARK) &&
|
---|
2358 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
|
---|
2359 | ((type2 >= XML_REGEXP_PUNCT) &&
|
---|
2360 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
|
---|
2361 | ((type2 >= XML_REGEXP_SEPAR) &&
|
---|
2362 | (type2 <= XML_REGEXP_SEPAR_PARA)) ||
|
---|
2363 | ((type2 >= XML_REGEXP_SYMBOL) &&
|
---|
2364 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
|
---|
2365 | ((type2 >= XML_REGEXP_OTHER) &&
|
---|
2366 | (type2 <= XML_REGEXP_OTHER_NA))
|
---|
2367 | ) return(0);
|
---|
2368 | break;
|
---|
2369 | case XML_REGEXP_NOTNAMECHAR: /* \C */
|
---|
2370 | break;
|
---|
2371 | case XML_REGEXP_DECIMAL: /* \d */
|
---|
2372 | /* can't be a letter, mark, separator, punctuation, symbol or other */
|
---|
2373 | if ((type2 == XML_REGEXP_NOTDECIMAL) ||
|
---|
2374 | (type2 == XML_REGEXP_REALCHAR) ||
|
---|
2375 | ((type2 >= XML_REGEXP_LETTER) &&
|
---|
2376 | (type2 <= XML_REGEXP_LETTER_OTHERS)) ||
|
---|
2377 | ((type2 >= XML_REGEXP_MARK) &&
|
---|
2378 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
|
---|
2379 | ((type2 >= XML_REGEXP_PUNCT) &&
|
---|
2380 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
|
---|
2381 | ((type2 >= XML_REGEXP_SEPAR) &&
|
---|
2382 | (type2 <= XML_REGEXP_SEPAR_PARA)) ||
|
---|
2383 | ((type2 >= XML_REGEXP_SYMBOL) &&
|
---|
2384 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
|
---|
2385 | ((type2 >= XML_REGEXP_OTHER) &&
|
---|
2386 | (type2 <= XML_REGEXP_OTHER_NA))
|
---|
2387 | )return(0);
|
---|
2388 | break;
|
---|
2389 | case XML_REGEXP_NOTDECIMAL: /* \D */
|
---|
2390 | break;
|
---|
2391 | case XML_REGEXP_REALCHAR: /* \w */
|
---|
2392 | /* can't be a mark, separator, punctuation, symbol or other */
|
---|
2393 | if ((type2 == XML_REGEXP_NOTDECIMAL) ||
|
---|
2394 | ((type2 >= XML_REGEXP_MARK) &&
|
---|
2395 | (type2 <= XML_REGEXP_MARK_ENCLOSING)) ||
|
---|
2396 | ((type2 >= XML_REGEXP_PUNCT) &&
|
---|
2397 | (type2 <= XML_REGEXP_PUNCT_OTHERS)) ||
|
---|
2398 | ((type2 >= XML_REGEXP_SEPAR) &&
|
---|
2399 | (type2 <= XML_REGEXP_SEPAR_PARA)) ||
|
---|
2400 | ((type2 >= XML_REGEXP_SYMBOL) &&
|
---|
2401 | (type2 <= XML_REGEXP_SYMBOL_OTHERS)) ||
|
---|
2402 | ((type2 >= XML_REGEXP_OTHER) &&
|
---|
2403 | (type2 <= XML_REGEXP_OTHER_NA))
|
---|
2404 | )return(0);
|
---|
2405 | break;
|
---|
2406 | case XML_REGEXP_NOTREALCHAR: /* \W */
|
---|
2407 | break;
|
---|
2408 | /*
|
---|
2409 | * at that point we know both type 1 and type2 are from
|
---|
2410 | * character categories are ordered and are different,
|
---|
2411 | * it becomes simple because this is a partition
|
---|
2412 | */
|
---|
2413 | case XML_REGEXP_LETTER:
|
---|
2414 | if (type2 <= XML_REGEXP_LETTER_OTHERS)
|
---|
2415 | return(1);
|
---|
2416 | return(0);
|
---|
2417 | case XML_REGEXP_LETTER_UPPERCASE:
|
---|
2418 | case XML_REGEXP_LETTER_LOWERCASE:
|
---|
2419 | case XML_REGEXP_LETTER_TITLECASE:
|
---|
2420 | case XML_REGEXP_LETTER_MODIFIER:
|
---|
2421 | case XML_REGEXP_LETTER_OTHERS:
|
---|
2422 | return(0);
|
---|
2423 | case XML_REGEXP_MARK:
|
---|
2424 | if (type2 <= XML_REGEXP_MARK_ENCLOSING)
|
---|
2425 | return(1);
|
---|
2426 | return(0);
|
---|
2427 | case XML_REGEXP_MARK_NONSPACING:
|
---|
2428 | case XML_REGEXP_MARK_SPACECOMBINING:
|
---|
2429 | case XML_REGEXP_MARK_ENCLOSING:
|
---|
2430 | return(0);
|
---|
2431 | case XML_REGEXP_NUMBER:
|
---|
2432 | if (type2 <= XML_REGEXP_NUMBER_OTHERS)
|
---|
2433 | return(1);
|
---|
2434 | return(0);
|
---|
2435 | case XML_REGEXP_NUMBER_DECIMAL:
|
---|
2436 | case XML_REGEXP_NUMBER_LETTER:
|
---|
2437 | case XML_REGEXP_NUMBER_OTHERS:
|
---|
2438 | return(0);
|
---|
2439 | case XML_REGEXP_PUNCT:
|
---|
2440 | if (type2 <= XML_REGEXP_PUNCT_OTHERS)
|
---|
2441 | return(1);
|
---|
2442 | return(0);
|
---|
2443 | case XML_REGEXP_PUNCT_CONNECTOR:
|
---|
2444 | case XML_REGEXP_PUNCT_DASH:
|
---|
2445 | case XML_REGEXP_PUNCT_OPEN:
|
---|
2446 | case XML_REGEXP_PUNCT_CLOSE:
|
---|
2447 | case XML_REGEXP_PUNCT_INITQUOTE:
|
---|
2448 | case XML_REGEXP_PUNCT_FINQUOTE:
|
---|
2449 | case XML_REGEXP_PUNCT_OTHERS:
|
---|
2450 | return(0);
|
---|
2451 | case XML_REGEXP_SEPAR:
|
---|
2452 | if (type2 <= XML_REGEXP_SEPAR_PARA)
|
---|
2453 | return(1);
|
---|
2454 | return(0);
|
---|
2455 | case XML_REGEXP_SEPAR_SPACE:
|
---|
2456 | case XML_REGEXP_SEPAR_LINE:
|
---|
2457 | case XML_REGEXP_SEPAR_PARA:
|
---|
2458 | return(0);
|
---|
2459 | case XML_REGEXP_SYMBOL:
|
---|
2460 | if (type2 <= XML_REGEXP_SYMBOL_OTHERS)
|
---|
2461 | return(1);
|
---|
2462 | return(0);
|
---|
2463 | case XML_REGEXP_SYMBOL_MATH:
|
---|
2464 | case XML_REGEXP_SYMBOL_CURRENCY:
|
---|
2465 | case XML_REGEXP_SYMBOL_MODIFIER:
|
---|
2466 | case XML_REGEXP_SYMBOL_OTHERS:
|
---|
2467 | return(0);
|
---|
2468 | case XML_REGEXP_OTHER:
|
---|
2469 | if (type2 <= XML_REGEXP_OTHER_NA)
|
---|
2470 | return(1);
|
---|
2471 | return(0);
|
---|
2472 | case XML_REGEXP_OTHER_CONTROL:
|
---|
2473 | case XML_REGEXP_OTHER_FORMAT:
|
---|
2474 | case XML_REGEXP_OTHER_PRIVATE:
|
---|
2475 | case XML_REGEXP_OTHER_NA:
|
---|
2476 | return(0);
|
---|
2477 | default:
|
---|
2478 | break;
|
---|
2479 | }
|
---|
2480 | return(1);
|
---|
2481 | }
|
---|
2482 |
|
---|
2483 | /**
|
---|
2484 | * xmlFAEqualAtoms:
|
---|
2485 | * @atom1: an atom
|
---|
2486 | * @atom2: an atom
|
---|
2487 | * @deep: if not set only compare string pointers
|
---|
2488 | *
|
---|
2489 | * Compares two atoms to check whether they are the same exactly
|
---|
2490 | * this is used to remove equivalent transitions
|
---|
2491 | *
|
---|
2492 | * Returns 1 if same and 0 otherwise
|
---|
2493 | */
|
---|
2494 | static int
|
---|
2495 | xmlFAEqualAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
|
---|
2496 | int ret = 0;
|
---|
2497 |
|
---|
2498 | if (atom1 == atom2)
|
---|
2499 | return(1);
|
---|
2500 | if ((atom1 == NULL) || (atom2 == NULL))
|
---|
2501 | return(0);
|
---|
2502 |
|
---|
2503 | if (atom1->type != atom2->type)
|
---|
2504 | return(0);
|
---|
2505 | switch (atom1->type) {
|
---|
2506 | case XML_REGEXP_EPSILON:
|
---|
2507 | ret = 0;
|
---|
2508 | break;
|
---|
2509 | case XML_REGEXP_STRING:
|
---|
2510 | if (!deep)
|
---|
2511 | ret = (atom1->valuep == atom2->valuep);
|
---|
2512 | else
|
---|
2513 | ret = xmlStrEqual((xmlChar *)atom1->valuep,
|
---|
2514 | (xmlChar *)atom2->valuep);
|
---|
2515 | break;
|
---|
2516 | case XML_REGEXP_CHARVAL:
|
---|
2517 | ret = (atom1->codepoint == atom2->codepoint);
|
---|
2518 | break;
|
---|
2519 | case XML_REGEXP_RANGES:
|
---|
2520 | /* too hard to do in the general case */
|
---|
2521 | ret = 0;
|
---|
2522 | default:
|
---|
2523 | break;
|
---|
2524 | }
|
---|
2525 | return(ret);
|
---|
2526 | }
|
---|
2527 |
|
---|
2528 | /**
|
---|
2529 | * xmlFACompareAtoms:
|
---|
2530 | * @atom1: an atom
|
---|
2531 | * @atom2: an atom
|
---|
2532 | * @deep: if not set only compare string pointers
|
---|
2533 | *
|
---|
2534 | * Compares two atoms to check whether they intersect in some ways,
|
---|
2535 | * this is used by xmlFAComputesDeterminism and xmlFARecurseDeterminism only
|
---|
2536 | *
|
---|
2537 | * Returns 1 if yes and 0 otherwise
|
---|
2538 | */
|
---|
2539 | static int
|
---|
2540 | xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2, int deep) {
|
---|
2541 | int ret = 1;
|
---|
2542 |
|
---|
2543 | if (atom1 == atom2)
|
---|
2544 | return(1);
|
---|
2545 | if ((atom1 == NULL) || (atom2 == NULL))
|
---|
2546 | return(0);
|
---|
2547 |
|
---|
2548 | if ((atom1->type == XML_REGEXP_ANYCHAR) ||
|
---|
2549 | (atom2->type == XML_REGEXP_ANYCHAR))
|
---|
2550 | return(1);
|
---|
2551 |
|
---|
2552 | if (atom1->type > atom2->type) {
|
---|
2553 | xmlRegAtomPtr tmp;
|
---|
2554 | tmp = atom1;
|
---|
2555 | atom1 = atom2;
|
---|
2556 | atom2 = tmp;
|
---|
2557 | }
|
---|
2558 | if (atom1->type != atom2->type) {
|
---|
2559 | ret = xmlFACompareAtomTypes(atom1->type, atom2->type);
|
---|
2560 | /* if they can't intersect at the type level break now */
|
---|
2561 | if (ret == 0)
|
---|
2562 | return(0);
|
---|
2563 | }
|
---|
2564 | switch (atom1->type) {
|
---|
2565 | case XML_REGEXP_STRING:
|
---|
2566 | if (!deep)
|
---|
2567 | ret = (atom1->valuep != atom2->valuep);
|
---|
2568 | else {
|
---|
2569 | xmlChar *val1 = (xmlChar *)atom1->valuep;
|
---|
2570 | xmlChar *val2 = (xmlChar *)atom2->valuep;
|
---|
2571 | int compound1 = (xmlStrchr(val1, '|') != NULL);
|
---|
2572 | int compound2 = (xmlStrchr(val2, '|') != NULL);
|
---|
2573 |
|
---|
2574 | /* Ignore negative match flag for ##other namespaces */
|
---|
2575 | if (compound1 != compound2)
|
---|
2576 | return(0);
|
---|
2577 |
|
---|
2578 | ret = xmlRegStrEqualWildcard(val1, val2);
|
---|
2579 | }
|
---|
2580 | break;
|
---|
2581 | case XML_REGEXP_EPSILON:
|
---|
2582 | goto not_determinist;
|
---|
2583 | case XML_REGEXP_CHARVAL:
|
---|
2584 | if (atom2->type == XML_REGEXP_CHARVAL) {
|
---|
2585 | ret = (atom1->codepoint == atom2->codepoint);
|
---|
2586 | } else {
|
---|
2587 | ret = xmlRegCheckCharacter(atom2, atom1->codepoint);
|
---|
2588 | if (ret < 0)
|
---|
2589 | ret = 1;
|
---|
2590 | }
|
---|
2591 | break;
|
---|
2592 | case XML_REGEXP_RANGES:
|
---|
2593 | if (atom2->type == XML_REGEXP_RANGES) {
|
---|
2594 | int i, j, res;
|
---|
2595 | xmlRegRangePtr r1, r2;
|
---|
2596 |
|
---|
2597 | /*
|
---|
2598 | * need to check that none of the ranges eventually matches
|
---|
2599 | */
|
---|
2600 | for (i = 0;i < atom1->nbRanges;i++) {
|
---|
2601 | for (j = 0;j < atom2->nbRanges;j++) {
|
---|
2602 | r1 = atom1->ranges[i];
|
---|
2603 | r2 = atom2->ranges[j];
|
---|
2604 | res = xmlFACompareRanges(r1, r2);
|
---|
2605 | if (res == 1) {
|
---|
2606 | ret = 1;
|
---|
2607 | goto done;
|
---|
2608 | }
|
---|
2609 | }
|
---|
2610 | }
|
---|
2611 | ret = 0;
|
---|
2612 | }
|
---|
2613 | break;
|
---|
2614 | default:
|
---|
2615 | goto not_determinist;
|
---|
2616 | }
|
---|
2617 | done:
|
---|
2618 | if (atom1->neg != atom2->neg) {
|
---|
2619 | ret = !ret;
|
---|
2620 | }
|
---|
2621 | if (ret == 0)
|
---|
2622 | return(0);
|
---|
2623 | not_determinist:
|
---|
2624 | return(1);
|
---|
2625 | }
|
---|
2626 |
|
---|
2627 | /**
|
---|
2628 | * xmlFARecurseDeterminism:
|
---|
2629 | * @ctxt: a regexp parser context
|
---|
2630 | *
|
---|
2631 | * Check whether the associated regexp is determinist,
|
---|
2632 | * should be called after xmlFAEliminateEpsilonTransitions()
|
---|
2633 | *
|
---|
2634 | */
|
---|
2635 | static int
|
---|
2636 | xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state,
|
---|
2637 | int to, xmlRegAtomPtr atom) {
|
---|
2638 | int ret = 1;
|
---|
2639 | int res;
|
---|
2640 | int transnr, nbTrans;
|
---|
2641 | xmlRegTransPtr t1;
|
---|
2642 | int deep = 1;
|
---|
2643 |
|
---|
2644 | if (state == NULL)
|
---|
2645 | return(ret);
|
---|
2646 | if (state->markd == XML_REGEXP_MARK_VISITED)
|
---|
2647 | return(ret);
|
---|
2648 |
|
---|
2649 | if (ctxt->flags & AM_AUTOMATA_RNG)
|
---|
2650 | deep = 0;
|
---|
2651 |
|
---|
2652 | /*
|
---|
2653 | * don't recurse on transitions potentially added in the course of
|
---|
2654 | * the elimination.
|
---|
2655 | */
|
---|
2656 | nbTrans = state->nbTrans;
|
---|
2657 | for (transnr = 0;transnr < nbTrans;transnr++) {
|
---|
2658 | t1 = &(state->trans[transnr]);
|
---|
2659 | /*
|
---|
2660 | * check transitions conflicting with the one looked at
|
---|
2661 | */
|
---|
2662 | if (t1->atom == NULL) {
|
---|
2663 | if (t1->to < 0)
|
---|
2664 | continue;
|
---|
2665 | state->markd = XML_REGEXP_MARK_VISITED;
|
---|
2666 | res = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
|
---|
2667 | to, atom);
|
---|
2668 | if (res == 0) {
|
---|
2669 | ret = 0;
|
---|
2670 | /* t1->nd = 1; */
|
---|
2671 | }
|
---|
2672 | continue;
|
---|
2673 | }
|
---|
2674 | if (t1->to != to)
|
---|
2675 | continue;
|
---|
2676 | if (xmlFACompareAtoms(t1->atom, atom, deep)) {
|
---|
2677 | ret = 0;
|
---|
2678 | /* mark the transition as non-deterministic */
|
---|
2679 | t1->nd = 1;
|
---|
2680 | }
|
---|
2681 | }
|
---|
2682 | return(ret);
|
---|
2683 | }
|
---|
2684 |
|
---|
2685 | /**
|
---|
2686 | * xmlFAFinishRecurseDeterminism:
|
---|
2687 | * @ctxt: a regexp parser context
|
---|
2688 | *
|
---|
2689 | * Reset flags after checking determinism.
|
---|
2690 | */
|
---|
2691 | static void
|
---|
2692 | xmlFAFinishRecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state) {
|
---|
2693 | int transnr, nbTrans;
|
---|
2694 |
|
---|
2695 | if (state == NULL)
|
---|
2696 | return;
|
---|
2697 | if (state->markd != XML_REGEXP_MARK_VISITED)
|
---|
2698 | return;
|
---|
2699 | state->markd = 0;
|
---|
2700 |
|
---|
2701 | nbTrans = state->nbTrans;
|
---|
2702 | for (transnr = 0; transnr < nbTrans; transnr++) {
|
---|
2703 | xmlRegTransPtr t1 = &state->trans[transnr];
|
---|
2704 | if ((t1->atom == NULL) && (t1->to >= 0))
|
---|
2705 | xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]);
|
---|
2706 | }
|
---|
2707 | }
|
---|
2708 |
|
---|
2709 | /**
|
---|
2710 | * xmlFAComputesDeterminism:
|
---|
2711 | * @ctxt: a regexp parser context
|
---|
2712 | *
|
---|
2713 | * Check whether the associated regexp is determinist,
|
---|
2714 | * should be called after xmlFAEliminateEpsilonTransitions()
|
---|
2715 | *
|
---|
2716 | */
|
---|
2717 | static int
|
---|
2718 | xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) {
|
---|
2719 | int statenr, transnr;
|
---|
2720 | xmlRegStatePtr state;
|
---|
2721 | xmlRegTransPtr t1, t2, last;
|
---|
2722 | int i;
|
---|
2723 | int ret = 1;
|
---|
2724 | int deep = 1;
|
---|
2725 |
|
---|
2726 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
2727 | printf("xmlFAComputesDeterminism\n");
|
---|
2728 | xmlRegPrintCtxt(stdout, ctxt);
|
---|
2729 | #endif
|
---|
2730 | if (ctxt->determinist != -1)
|
---|
2731 | return(ctxt->determinist);
|
---|
2732 |
|
---|
2733 | if (ctxt->flags & AM_AUTOMATA_RNG)
|
---|
2734 | deep = 0;
|
---|
2735 |
|
---|
2736 | /*
|
---|
2737 | * First cleanup the automata removing cancelled transitions
|
---|
2738 | */
|
---|
2739 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
|
---|
2740 | state = ctxt->states[statenr];
|
---|
2741 | if (state == NULL)
|
---|
2742 | continue;
|
---|
2743 | if (state->nbTrans < 2)
|
---|
2744 | continue;
|
---|
2745 | for (transnr = 0;transnr < state->nbTrans;transnr++) {
|
---|
2746 | t1 = &(state->trans[transnr]);
|
---|
2747 | /*
|
---|
2748 | * Determinism checks in case of counted or all transitions
|
---|
2749 | * will have to be handled separately
|
---|
2750 | */
|
---|
2751 | if (t1->atom == NULL) {
|
---|
2752 | /* t1->nd = 1; */
|
---|
2753 | continue;
|
---|
2754 | }
|
---|
2755 | if (t1->to == -1) /* eliminated */
|
---|
2756 | continue;
|
---|
2757 | for (i = 0;i < transnr;i++) {
|
---|
2758 | t2 = &(state->trans[i]);
|
---|
2759 | if (t2->to == -1) /* eliminated */
|
---|
2760 | continue;
|
---|
2761 | if (t2->atom != NULL) {
|
---|
2762 | if (t1->to == t2->to) {
|
---|
2763 | /*
|
---|
2764 | * Here we use deep because we want to keep the
|
---|
2765 | * transitions which indicate a conflict
|
---|
2766 | */
|
---|
2767 | if (xmlFAEqualAtoms(t1->atom, t2->atom, deep) &&
|
---|
2768 | (t1->counter == t2->counter) &&
|
---|
2769 | (t1->count == t2->count))
|
---|
2770 | t2->to = -1; /* eliminated */
|
---|
2771 | }
|
---|
2772 | }
|
---|
2773 | }
|
---|
2774 | }
|
---|
2775 | }
|
---|
2776 |
|
---|
2777 | /*
|
---|
2778 | * Check for all states that there aren't 2 transitions
|
---|
2779 | * with the same atom and a different target.
|
---|
2780 | */
|
---|
2781 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) {
|
---|
2782 | state = ctxt->states[statenr];
|
---|
2783 | if (state == NULL)
|
---|
2784 | continue;
|
---|
2785 | if (state->nbTrans < 2)
|
---|
2786 | continue;
|
---|
2787 | last = NULL;
|
---|
2788 | for (transnr = 0;transnr < state->nbTrans;transnr++) {
|
---|
2789 | t1 = &(state->trans[transnr]);
|
---|
2790 | /*
|
---|
2791 | * Determinism checks in case of counted or all transitions
|
---|
2792 | * will have to be handled separately
|
---|
2793 | */
|
---|
2794 | if (t1->atom == NULL) {
|
---|
2795 | continue;
|
---|
2796 | }
|
---|
2797 | if (t1->to == -1) /* eliminated */
|
---|
2798 | continue;
|
---|
2799 | for (i = 0;i < transnr;i++) {
|
---|
2800 | t2 = &(state->trans[i]);
|
---|
2801 | if (t2->to == -1) /* eliminated */
|
---|
2802 | continue;
|
---|
2803 | if (t2->atom != NULL) {
|
---|
2804 | /*
|
---|
2805 | * But here we don't use deep because we want to
|
---|
2806 | * find transitions which indicate a conflict
|
---|
2807 | */
|
---|
2808 | if (xmlFACompareAtoms(t1->atom, t2->atom, 1)) {
|
---|
2809 | ret = 0;
|
---|
2810 | /* mark the transitions as non-deterministic ones */
|
---|
2811 | t1->nd = 1;
|
---|
2812 | t2->nd = 1;
|
---|
2813 | last = t1;
|
---|
2814 | }
|
---|
2815 | } else if (t1->to != -1) {
|
---|
2816 | /*
|
---|
2817 | * do the closure in case of remaining specific
|
---|
2818 | * epsilon transitions like choices or all
|
---|
2819 | */
|
---|
2820 | ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to],
|
---|
2821 | t2->to, t2->atom);
|
---|
2822 | xmlFAFinishRecurseDeterminism(ctxt, ctxt->states[t1->to]);
|
---|
2823 | /* don't shortcut the computation so all non deterministic
|
---|
2824 | transition get marked down
|
---|
2825 | if (ret == 0)
|
---|
2826 | return(0);
|
---|
2827 | */
|
---|
2828 | if (ret == 0) {
|
---|
2829 | t1->nd = 1;
|
---|
2830 | /* t2->nd = 1; */
|
---|
2831 | last = t1;
|
---|
2832 | }
|
---|
2833 | }
|
---|
2834 | }
|
---|
2835 | /* don't shortcut the computation so all non deterministic
|
---|
2836 | transition get marked down
|
---|
2837 | if (ret == 0)
|
---|
2838 | break; */
|
---|
2839 | }
|
---|
2840 |
|
---|
2841 | /*
|
---|
2842 | * mark specifically the last non-deterministic transition
|
---|
2843 | * from a state since there is no need to set-up rollback
|
---|
2844 | * from it
|
---|
2845 | */
|
---|
2846 | if (last != NULL) {
|
---|
2847 | last->nd = 2;
|
---|
2848 | }
|
---|
2849 |
|
---|
2850 | /* don't shortcut the computation so all non deterministic
|
---|
2851 | transition get marked down
|
---|
2852 | if (ret == 0)
|
---|
2853 | break; */
|
---|
2854 | }
|
---|
2855 |
|
---|
2856 | ctxt->determinist = ret;
|
---|
2857 | return(ret);
|
---|
2858 | }
|
---|
2859 |
|
---|
2860 | /************************************************************************
|
---|
2861 | * *
|
---|
2862 | * Routines to check input against transition atoms *
|
---|
2863 | * *
|
---|
2864 | ************************************************************************/
|
---|
2865 |
|
---|
2866 | static int
|
---|
2867 | xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg,
|
---|
2868 | int start, int end, const xmlChar *blockName) {
|
---|
2869 | int ret = 0;
|
---|
2870 |
|
---|
2871 | switch (type) {
|
---|
2872 | case XML_REGEXP_STRING:
|
---|
2873 | case XML_REGEXP_SUBREG:
|
---|
2874 | case XML_REGEXP_RANGES:
|
---|
2875 | case XML_REGEXP_EPSILON:
|
---|
2876 | return(-1);
|
---|
2877 | case XML_REGEXP_ANYCHAR:
|
---|
2878 | ret = ((codepoint != '\n') && (codepoint != '\r'));
|
---|
2879 | break;
|
---|
2880 | case XML_REGEXP_CHARVAL:
|
---|
2881 | ret = ((codepoint >= start) && (codepoint <= end));
|
---|
2882 | break;
|
---|
2883 | case XML_REGEXP_NOTSPACE:
|
---|
2884 | neg = !neg;
|
---|
2885 | /* Falls through. */
|
---|
2886 | case XML_REGEXP_ANYSPACE:
|
---|
2887 | ret = ((codepoint == '\n') || (codepoint == '\r') ||
|
---|
2888 | (codepoint == '\t') || (codepoint == ' '));
|
---|
2889 | break;
|
---|
2890 | case XML_REGEXP_NOTINITNAME:
|
---|
2891 | neg = !neg;
|
---|
2892 | /* Falls through. */
|
---|
2893 | case XML_REGEXP_INITNAME:
|
---|
2894 | ret = (IS_LETTER(codepoint) ||
|
---|
2895 | (codepoint == '_') || (codepoint == ':'));
|
---|
2896 | break;
|
---|
2897 | case XML_REGEXP_NOTNAMECHAR:
|
---|
2898 | neg = !neg;
|
---|
2899 | /* Falls through. */
|
---|
2900 | case XML_REGEXP_NAMECHAR:
|
---|
2901 | ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) ||
|
---|
2902 | (codepoint == '.') || (codepoint == '-') ||
|
---|
2903 | (codepoint == '_') || (codepoint == ':') ||
|
---|
2904 | IS_COMBINING(codepoint) || IS_EXTENDER(codepoint));
|
---|
2905 | break;
|
---|
2906 | case XML_REGEXP_NOTDECIMAL:
|
---|
2907 | neg = !neg;
|
---|
2908 | /* Falls through. */
|
---|
2909 | case XML_REGEXP_DECIMAL:
|
---|
2910 | ret = xmlUCSIsCatNd(codepoint);
|
---|
2911 | break;
|
---|
2912 | case XML_REGEXP_REALCHAR:
|
---|
2913 | neg = !neg;
|
---|
2914 | /* Falls through. */
|
---|
2915 | case XML_REGEXP_NOTREALCHAR:
|
---|
2916 | ret = xmlUCSIsCatP(codepoint);
|
---|
2917 | if (ret == 0)
|
---|
2918 | ret = xmlUCSIsCatZ(codepoint);
|
---|
2919 | if (ret == 0)
|
---|
2920 | ret = xmlUCSIsCatC(codepoint);
|
---|
2921 | break;
|
---|
2922 | case XML_REGEXP_LETTER:
|
---|
2923 | ret = xmlUCSIsCatL(codepoint);
|
---|
2924 | break;
|
---|
2925 | case XML_REGEXP_LETTER_UPPERCASE:
|
---|
2926 | ret = xmlUCSIsCatLu(codepoint);
|
---|
2927 | break;
|
---|
2928 | case XML_REGEXP_LETTER_LOWERCASE:
|
---|
2929 | ret = xmlUCSIsCatLl(codepoint);
|
---|
2930 | break;
|
---|
2931 | case XML_REGEXP_LETTER_TITLECASE:
|
---|
2932 | ret = xmlUCSIsCatLt(codepoint);
|
---|
2933 | break;
|
---|
2934 | case XML_REGEXP_LETTER_MODIFIER:
|
---|
2935 | ret = xmlUCSIsCatLm(codepoint);
|
---|
2936 | break;
|
---|
2937 | case XML_REGEXP_LETTER_OTHERS:
|
---|
2938 | ret = xmlUCSIsCatLo(codepoint);
|
---|
2939 | break;
|
---|
2940 | case XML_REGEXP_MARK:
|
---|
2941 | ret = xmlUCSIsCatM(codepoint);
|
---|
2942 | break;
|
---|
2943 | case XML_REGEXP_MARK_NONSPACING:
|
---|
2944 | ret = xmlUCSIsCatMn(codepoint);
|
---|
2945 | break;
|
---|
2946 | case XML_REGEXP_MARK_SPACECOMBINING:
|
---|
2947 | ret = xmlUCSIsCatMc(codepoint);
|
---|
2948 | break;
|
---|
2949 | case XML_REGEXP_MARK_ENCLOSING:
|
---|
2950 | ret = xmlUCSIsCatMe(codepoint);
|
---|
2951 | break;
|
---|
2952 | case XML_REGEXP_NUMBER:
|
---|
2953 | ret = xmlUCSIsCatN(codepoint);
|
---|
2954 | break;
|
---|
2955 | case XML_REGEXP_NUMBER_DECIMAL:
|
---|
2956 | ret = xmlUCSIsCatNd(codepoint);
|
---|
2957 | break;
|
---|
2958 | case XML_REGEXP_NUMBER_LETTER:
|
---|
2959 | ret = xmlUCSIsCatNl(codepoint);
|
---|
2960 | break;
|
---|
2961 | case XML_REGEXP_NUMBER_OTHERS:
|
---|
2962 | ret = xmlUCSIsCatNo(codepoint);
|
---|
2963 | break;
|
---|
2964 | case XML_REGEXP_PUNCT:
|
---|
2965 | ret = xmlUCSIsCatP(codepoint);
|
---|
2966 | break;
|
---|
2967 | case XML_REGEXP_PUNCT_CONNECTOR:
|
---|
2968 | ret = xmlUCSIsCatPc(codepoint);
|
---|
2969 | break;
|
---|
2970 | case XML_REGEXP_PUNCT_DASH:
|
---|
2971 | ret = xmlUCSIsCatPd(codepoint);
|
---|
2972 | break;
|
---|
2973 | case XML_REGEXP_PUNCT_OPEN:
|
---|
2974 | ret = xmlUCSIsCatPs(codepoint);
|
---|
2975 | break;
|
---|
2976 | case XML_REGEXP_PUNCT_CLOSE:
|
---|
2977 | ret = xmlUCSIsCatPe(codepoint);
|
---|
2978 | break;
|
---|
2979 | case XML_REGEXP_PUNCT_INITQUOTE:
|
---|
2980 | ret = xmlUCSIsCatPi(codepoint);
|
---|
2981 | break;
|
---|
2982 | case XML_REGEXP_PUNCT_FINQUOTE:
|
---|
2983 | ret = xmlUCSIsCatPf(codepoint);
|
---|
2984 | break;
|
---|
2985 | case XML_REGEXP_PUNCT_OTHERS:
|
---|
2986 | ret = xmlUCSIsCatPo(codepoint);
|
---|
2987 | break;
|
---|
2988 | case XML_REGEXP_SEPAR:
|
---|
2989 | ret = xmlUCSIsCatZ(codepoint);
|
---|
2990 | break;
|
---|
2991 | case XML_REGEXP_SEPAR_SPACE:
|
---|
2992 | ret = xmlUCSIsCatZs(codepoint);
|
---|
2993 | break;
|
---|
2994 | case XML_REGEXP_SEPAR_LINE:
|
---|
2995 | ret = xmlUCSIsCatZl(codepoint);
|
---|
2996 | break;
|
---|
2997 | case XML_REGEXP_SEPAR_PARA:
|
---|
2998 | ret = xmlUCSIsCatZp(codepoint);
|
---|
2999 | break;
|
---|
3000 | case XML_REGEXP_SYMBOL:
|
---|
3001 | ret = xmlUCSIsCatS(codepoint);
|
---|
3002 | break;
|
---|
3003 | case XML_REGEXP_SYMBOL_MATH:
|
---|
3004 | ret = xmlUCSIsCatSm(codepoint);
|
---|
3005 | break;
|
---|
3006 | case XML_REGEXP_SYMBOL_CURRENCY:
|
---|
3007 | ret = xmlUCSIsCatSc(codepoint);
|
---|
3008 | break;
|
---|
3009 | case XML_REGEXP_SYMBOL_MODIFIER:
|
---|
3010 | ret = xmlUCSIsCatSk(codepoint);
|
---|
3011 | break;
|
---|
3012 | case XML_REGEXP_SYMBOL_OTHERS:
|
---|
3013 | ret = xmlUCSIsCatSo(codepoint);
|
---|
3014 | break;
|
---|
3015 | case XML_REGEXP_OTHER:
|
---|
3016 | ret = xmlUCSIsCatC(codepoint);
|
---|
3017 | break;
|
---|
3018 | case XML_REGEXP_OTHER_CONTROL:
|
---|
3019 | ret = xmlUCSIsCatCc(codepoint);
|
---|
3020 | break;
|
---|
3021 | case XML_REGEXP_OTHER_FORMAT:
|
---|
3022 | ret = xmlUCSIsCatCf(codepoint);
|
---|
3023 | break;
|
---|
3024 | case XML_REGEXP_OTHER_PRIVATE:
|
---|
3025 | ret = xmlUCSIsCatCo(codepoint);
|
---|
3026 | break;
|
---|
3027 | case XML_REGEXP_OTHER_NA:
|
---|
3028 | /* ret = xmlUCSIsCatCn(codepoint); */
|
---|
3029 | /* Seems it doesn't exist anymore in recent Unicode releases */
|
---|
3030 | ret = 0;
|
---|
3031 | break;
|
---|
3032 | case XML_REGEXP_BLOCK_NAME:
|
---|
3033 | ret = xmlUCSIsBlock(codepoint, (const char *) blockName);
|
---|
3034 | break;
|
---|
3035 | }
|
---|
3036 | if (neg)
|
---|
3037 | return(!ret);
|
---|
3038 | return(ret);
|
---|
3039 | }
|
---|
3040 |
|
---|
3041 | static int
|
---|
3042 | xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) {
|
---|
3043 | int i, ret = 0;
|
---|
3044 | xmlRegRangePtr range;
|
---|
3045 |
|
---|
3046 | if ((atom == NULL) || (!IS_CHAR(codepoint)))
|
---|
3047 | return(-1);
|
---|
3048 |
|
---|
3049 | switch (atom->type) {
|
---|
3050 | case XML_REGEXP_SUBREG:
|
---|
3051 | case XML_REGEXP_EPSILON:
|
---|
3052 | return(-1);
|
---|
3053 | case XML_REGEXP_CHARVAL:
|
---|
3054 | return(codepoint == atom->codepoint);
|
---|
3055 | case XML_REGEXP_RANGES: {
|
---|
3056 | int accept = 0;
|
---|
3057 |
|
---|
3058 | for (i = 0;i < atom->nbRanges;i++) {
|
---|
3059 | range = atom->ranges[i];
|
---|
3060 | if (range->neg == 2) {
|
---|
3061 | ret = xmlRegCheckCharacterRange(range->type, codepoint,
|
---|
3062 | 0, range->start, range->end,
|
---|
3063 | range->blockName);
|
---|
3064 | if (ret != 0)
|
---|
3065 | return(0); /* excluded char */
|
---|
3066 | } else if (range->neg) {
|
---|
3067 | ret = xmlRegCheckCharacterRange(range->type, codepoint,
|
---|
3068 | 0, range->start, range->end,
|
---|
3069 | range->blockName);
|
---|
3070 | if (ret == 0)
|
---|
3071 | accept = 1;
|
---|
3072 | else
|
---|
3073 | return(0);
|
---|
3074 | } else {
|
---|
3075 | ret = xmlRegCheckCharacterRange(range->type, codepoint,
|
---|
3076 | 0, range->start, range->end,
|
---|
3077 | range->blockName);
|
---|
3078 | if (ret != 0)
|
---|
3079 | accept = 1; /* might still be excluded */
|
---|
3080 | }
|
---|
3081 | }
|
---|
3082 | return(accept);
|
---|
3083 | }
|
---|
3084 | case XML_REGEXP_STRING:
|
---|
3085 | printf("TODO: XML_REGEXP_STRING\n");
|
---|
3086 | return(-1);
|
---|
3087 | case XML_REGEXP_ANYCHAR:
|
---|
3088 | case XML_REGEXP_ANYSPACE:
|
---|
3089 | case XML_REGEXP_NOTSPACE:
|
---|
3090 | case XML_REGEXP_INITNAME:
|
---|
3091 | case XML_REGEXP_NOTINITNAME:
|
---|
3092 | case XML_REGEXP_NAMECHAR:
|
---|
3093 | case XML_REGEXP_NOTNAMECHAR:
|
---|
3094 | case XML_REGEXP_DECIMAL:
|
---|
3095 | case XML_REGEXP_NOTDECIMAL:
|
---|
3096 | case XML_REGEXP_REALCHAR:
|
---|
3097 | case XML_REGEXP_NOTREALCHAR:
|
---|
3098 | case XML_REGEXP_LETTER:
|
---|
3099 | case XML_REGEXP_LETTER_UPPERCASE:
|
---|
3100 | case XML_REGEXP_LETTER_LOWERCASE:
|
---|
3101 | case XML_REGEXP_LETTER_TITLECASE:
|
---|
3102 | case XML_REGEXP_LETTER_MODIFIER:
|
---|
3103 | case XML_REGEXP_LETTER_OTHERS:
|
---|
3104 | case XML_REGEXP_MARK:
|
---|
3105 | case XML_REGEXP_MARK_NONSPACING:
|
---|
3106 | case XML_REGEXP_MARK_SPACECOMBINING:
|
---|
3107 | case XML_REGEXP_MARK_ENCLOSING:
|
---|
3108 | case XML_REGEXP_NUMBER:
|
---|
3109 | case XML_REGEXP_NUMBER_DECIMAL:
|
---|
3110 | case XML_REGEXP_NUMBER_LETTER:
|
---|
3111 | case XML_REGEXP_NUMBER_OTHERS:
|
---|
3112 | case XML_REGEXP_PUNCT:
|
---|
3113 | case XML_REGEXP_PUNCT_CONNECTOR:
|
---|
3114 | case XML_REGEXP_PUNCT_DASH:
|
---|
3115 | case XML_REGEXP_PUNCT_OPEN:
|
---|
3116 | case XML_REGEXP_PUNCT_CLOSE:
|
---|
3117 | case XML_REGEXP_PUNCT_INITQUOTE:
|
---|
3118 | case XML_REGEXP_PUNCT_FINQUOTE:
|
---|
3119 | case XML_REGEXP_PUNCT_OTHERS:
|
---|
3120 | case XML_REGEXP_SEPAR:
|
---|
3121 | case XML_REGEXP_SEPAR_SPACE:
|
---|
3122 | case XML_REGEXP_SEPAR_LINE:
|
---|
3123 | case XML_REGEXP_SEPAR_PARA:
|
---|
3124 | case XML_REGEXP_SYMBOL:
|
---|
3125 | case XML_REGEXP_SYMBOL_MATH:
|
---|
3126 | case XML_REGEXP_SYMBOL_CURRENCY:
|
---|
3127 | case XML_REGEXP_SYMBOL_MODIFIER:
|
---|
3128 | case XML_REGEXP_SYMBOL_OTHERS:
|
---|
3129 | case XML_REGEXP_OTHER:
|
---|
3130 | case XML_REGEXP_OTHER_CONTROL:
|
---|
3131 | case XML_REGEXP_OTHER_FORMAT:
|
---|
3132 | case XML_REGEXP_OTHER_PRIVATE:
|
---|
3133 | case XML_REGEXP_OTHER_NA:
|
---|
3134 | case XML_REGEXP_BLOCK_NAME:
|
---|
3135 | ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0,
|
---|
3136 | (const xmlChar *)atom->valuep);
|
---|
3137 | if (atom->neg)
|
---|
3138 | ret = !ret;
|
---|
3139 | break;
|
---|
3140 | }
|
---|
3141 | return(ret);
|
---|
3142 | }
|
---|
3143 |
|
---|
3144 | /************************************************************************
|
---|
3145 | * *
|
---|
3146 | * Saving and restoring state of an execution context *
|
---|
3147 | * *
|
---|
3148 | ************************************************************************/
|
---|
3149 |
|
---|
3150 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3151 | static void
|
---|
3152 | xmlFARegDebugExec(xmlRegExecCtxtPtr exec) {
|
---|
3153 | printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index);
|
---|
3154 | if (exec->inputStack != NULL) {
|
---|
3155 | int i;
|
---|
3156 | printf(": ");
|
---|
3157 | for (i = 0;(i < 3) && (i < exec->inputStackNr);i++)
|
---|
3158 | printf("%s ", (const char *)
|
---|
3159 | exec->inputStack[exec->inputStackNr - (i + 1)].value);
|
---|
3160 | } else {
|
---|
3161 | printf(": %s", &(exec->inputString[exec->index]));
|
---|
3162 | }
|
---|
3163 | printf("\n");
|
---|
3164 | }
|
---|
3165 | #endif
|
---|
3166 |
|
---|
3167 | static void
|
---|
3168 | xmlFARegExecSave(xmlRegExecCtxtPtr exec) {
|
---|
3169 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3170 | printf("saving ");
|
---|
3171 | exec->transno++;
|
---|
3172 | xmlFARegDebugExec(exec);
|
---|
3173 | exec->transno--;
|
---|
3174 | #endif
|
---|
3175 | #ifdef MAX_PUSH
|
---|
3176 | if (exec->nbPush > MAX_PUSH) {
|
---|
3177 | return;
|
---|
3178 | }
|
---|
3179 | exec->nbPush++;
|
---|
3180 | #endif
|
---|
3181 |
|
---|
3182 | if (exec->maxRollbacks == 0) {
|
---|
3183 | exec->maxRollbacks = 4;
|
---|
3184 | exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks *
|
---|
3185 | sizeof(xmlRegExecRollback));
|
---|
3186 | if (exec->rollbacks == NULL) {
|
---|
3187 | xmlRegexpErrMemory(NULL, "saving regexp");
|
---|
3188 | exec->maxRollbacks = 0;
|
---|
3189 | return;
|
---|
3190 | }
|
---|
3191 | memset(exec->rollbacks, 0,
|
---|
3192 | exec->maxRollbacks * sizeof(xmlRegExecRollback));
|
---|
3193 | } else if (exec->nbRollbacks >= exec->maxRollbacks) {
|
---|
3194 | xmlRegExecRollback *tmp;
|
---|
3195 | int len = exec->maxRollbacks;
|
---|
3196 |
|
---|
3197 | exec->maxRollbacks *= 2;
|
---|
3198 | tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks,
|
---|
3199 | exec->maxRollbacks * sizeof(xmlRegExecRollback));
|
---|
3200 | if (tmp == NULL) {
|
---|
3201 | xmlRegexpErrMemory(NULL, "saving regexp");
|
---|
3202 | exec->maxRollbacks /= 2;
|
---|
3203 | return;
|
---|
3204 | }
|
---|
3205 | exec->rollbacks = tmp;
|
---|
3206 | tmp = &exec->rollbacks[len];
|
---|
3207 | memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback));
|
---|
3208 | }
|
---|
3209 | exec->rollbacks[exec->nbRollbacks].state = exec->state;
|
---|
3210 | exec->rollbacks[exec->nbRollbacks].index = exec->index;
|
---|
3211 | exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1;
|
---|
3212 | if (exec->comp->nbCounters > 0) {
|
---|
3213 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
|
---|
3214 | exec->rollbacks[exec->nbRollbacks].counts = (int *)
|
---|
3215 | xmlMalloc(exec->comp->nbCounters * sizeof(int));
|
---|
3216 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
|
---|
3217 | xmlRegexpErrMemory(NULL, "saving regexp");
|
---|
3218 | exec->status = -5;
|
---|
3219 | return;
|
---|
3220 | }
|
---|
3221 | }
|
---|
3222 | memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts,
|
---|
3223 | exec->comp->nbCounters * sizeof(int));
|
---|
3224 | }
|
---|
3225 | exec->nbRollbacks++;
|
---|
3226 | }
|
---|
3227 |
|
---|
3228 | static void
|
---|
3229 | xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) {
|
---|
3230 | if (exec->nbRollbacks <= 0) {
|
---|
3231 | exec->status = -1;
|
---|
3232 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3233 | printf("rollback failed on empty stack\n");
|
---|
3234 | #endif
|
---|
3235 | return;
|
---|
3236 | }
|
---|
3237 | exec->nbRollbacks--;
|
---|
3238 | exec->state = exec->rollbacks[exec->nbRollbacks].state;
|
---|
3239 | exec->index = exec->rollbacks[exec->nbRollbacks].index;
|
---|
3240 | exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch;
|
---|
3241 | if (exec->comp->nbCounters > 0) {
|
---|
3242 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) {
|
---|
3243 | fprintf(stderr, "exec save: allocation failed");
|
---|
3244 | exec->status = -6;
|
---|
3245 | return;
|
---|
3246 | }
|
---|
3247 | if (exec->counts) {
|
---|
3248 | memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts,
|
---|
3249 | exec->comp->nbCounters * sizeof(int));
|
---|
3250 | }
|
---|
3251 | }
|
---|
3252 |
|
---|
3253 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3254 | printf("restored ");
|
---|
3255 | xmlFARegDebugExec(exec);
|
---|
3256 | #endif
|
---|
3257 | }
|
---|
3258 |
|
---|
3259 | /************************************************************************
|
---|
3260 | * *
|
---|
3261 | * Verifier, running an input against a compiled regexp *
|
---|
3262 | * *
|
---|
3263 | ************************************************************************/
|
---|
3264 |
|
---|
3265 | static int
|
---|
3266 | xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) {
|
---|
3267 | xmlRegExecCtxt execval;
|
---|
3268 | xmlRegExecCtxtPtr exec = &execval;
|
---|
3269 | int ret, codepoint = 0, len, deter;
|
---|
3270 |
|
---|
3271 | exec->inputString = content;
|
---|
3272 | exec->index = 0;
|
---|
3273 | exec->nbPush = 0;
|
---|
3274 | exec->determinist = 1;
|
---|
3275 | exec->maxRollbacks = 0;
|
---|
3276 | exec->nbRollbacks = 0;
|
---|
3277 | exec->rollbacks = NULL;
|
---|
3278 | exec->status = 0;
|
---|
3279 | exec->comp = comp;
|
---|
3280 | exec->state = comp->states[0];
|
---|
3281 | exec->transno = 0;
|
---|
3282 | exec->transcount = 0;
|
---|
3283 | exec->inputStack = NULL;
|
---|
3284 | exec->inputStackMax = 0;
|
---|
3285 | if (comp->nbCounters > 0) {
|
---|
3286 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int));
|
---|
3287 | if (exec->counts == NULL) {
|
---|
3288 | xmlRegexpErrMemory(NULL, "running regexp");
|
---|
3289 | return(-1);
|
---|
3290 | }
|
---|
3291 | memset(exec->counts, 0, comp->nbCounters * sizeof(int));
|
---|
3292 | } else
|
---|
3293 | exec->counts = NULL;
|
---|
3294 | while ((exec->status == 0) && (exec->state != NULL) &&
|
---|
3295 | ((exec->inputString[exec->index] != 0) ||
|
---|
3296 | ((exec->state != NULL) &&
|
---|
3297 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
|
---|
3298 | xmlRegTransPtr trans;
|
---|
3299 | xmlRegAtomPtr atom;
|
---|
3300 |
|
---|
3301 | /*
|
---|
3302 | * If end of input on non-terminal state, rollback, however we may
|
---|
3303 | * still have epsilon like transition for counted transitions
|
---|
3304 | * on counters, in that case don't break too early. Additionally,
|
---|
3305 | * if we are working on a range like "AB{0,2}", where B is not present,
|
---|
3306 | * we don't want to break.
|
---|
3307 | */
|
---|
3308 | len = 1;
|
---|
3309 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) {
|
---|
3310 | /*
|
---|
3311 | * if there is a transition, we must check if
|
---|
3312 | * atom allows minOccurs of 0
|
---|
3313 | */
|
---|
3314 | if (exec->transno < exec->state->nbTrans) {
|
---|
3315 | trans = &exec->state->trans[exec->transno];
|
---|
3316 | if (trans->to >=0) {
|
---|
3317 | atom = trans->atom;
|
---|
3318 | if (!((atom->min == 0) && (atom->max > 0)))
|
---|
3319 | goto rollback;
|
---|
3320 | }
|
---|
3321 | } else
|
---|
3322 | goto rollback;
|
---|
3323 | }
|
---|
3324 |
|
---|
3325 | exec->transcount = 0;
|
---|
3326 | for (;exec->transno < exec->state->nbTrans;exec->transno++) {
|
---|
3327 | trans = &exec->state->trans[exec->transno];
|
---|
3328 | if (trans->to < 0)
|
---|
3329 | continue;
|
---|
3330 | atom = trans->atom;
|
---|
3331 | ret = 0;
|
---|
3332 | deter = 1;
|
---|
3333 | if (trans->count >= 0) {
|
---|
3334 | int count;
|
---|
3335 | xmlRegCounterPtr counter;
|
---|
3336 |
|
---|
3337 | if (exec->counts == NULL) {
|
---|
3338 | exec->status = -1;
|
---|
3339 | goto error;
|
---|
3340 | }
|
---|
3341 | /*
|
---|
3342 | * A counted transition.
|
---|
3343 | */
|
---|
3344 |
|
---|
3345 | count = exec->counts[trans->count];
|
---|
3346 | counter = &exec->comp->counters[trans->count];
|
---|
3347 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3348 | printf("testing count %d: val %d, min %d, max %d\n",
|
---|
3349 | trans->count, count, counter->min, counter->max);
|
---|
3350 | #endif
|
---|
3351 | ret = ((count >= counter->min) && (count <= counter->max));
|
---|
3352 | if ((ret) && (counter->min != counter->max))
|
---|
3353 | deter = 0;
|
---|
3354 | } else if (atom == NULL) {
|
---|
3355 | fprintf(stderr, "epsilon transition left at runtime\n");
|
---|
3356 | exec->status = -2;
|
---|
3357 | break;
|
---|
3358 | } else if (exec->inputString[exec->index] != 0) {
|
---|
3359 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
|
---|
3360 | ret = xmlRegCheckCharacter(atom, codepoint);
|
---|
3361 | if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) {
|
---|
3362 | xmlRegStatePtr to = comp->states[trans->to];
|
---|
3363 |
|
---|
3364 | /*
|
---|
3365 | * this is a multiple input sequence
|
---|
3366 | * If there is a counter associated increment it now.
|
---|
3367 | * do not increment if the counter is already over the
|
---|
3368 | * maximum limit in which case get to next transition
|
---|
3369 | */
|
---|
3370 | if (trans->counter >= 0) {
|
---|
3371 | xmlRegCounterPtr counter;
|
---|
3372 |
|
---|
3373 | if ((exec->counts == NULL) ||
|
---|
3374 | (exec->comp == NULL) ||
|
---|
3375 | (exec->comp->counters == NULL)) {
|
---|
3376 | exec->status = -1;
|
---|
3377 | goto error;
|
---|
3378 | }
|
---|
3379 | counter = &exec->comp->counters[trans->counter];
|
---|
3380 | if (exec->counts[trans->counter] >= counter->max)
|
---|
3381 | continue; /* for loop on transitions */
|
---|
3382 | }
|
---|
3383 | /* Save before incrementing */
|
---|
3384 | if (exec->state->nbTrans > exec->transno + 1) {
|
---|
3385 | xmlFARegExecSave(exec);
|
---|
3386 | }
|
---|
3387 | if (trans->counter >= 0) {
|
---|
3388 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3389 | printf("Increasing count %d\n", trans->counter);
|
---|
3390 | #endif
|
---|
3391 | exec->counts[trans->counter]++;
|
---|
3392 | }
|
---|
3393 | exec->transcount = 1;
|
---|
3394 | do {
|
---|
3395 | /*
|
---|
3396 | * Try to progress as much as possible on the input
|
---|
3397 | */
|
---|
3398 | if (exec->transcount == atom->max) {
|
---|
3399 | break;
|
---|
3400 | }
|
---|
3401 | exec->index += len;
|
---|
3402 | /*
|
---|
3403 | * End of input: stop here
|
---|
3404 | */
|
---|
3405 | if (exec->inputString[exec->index] == 0) {
|
---|
3406 | exec->index -= len;
|
---|
3407 | break;
|
---|
3408 | }
|
---|
3409 | if (exec->transcount >= atom->min) {
|
---|
3410 | int transno = exec->transno;
|
---|
3411 | xmlRegStatePtr state = exec->state;
|
---|
3412 |
|
---|
3413 | /*
|
---|
3414 | * The transition is acceptable save it
|
---|
3415 | */
|
---|
3416 | exec->transno = -1; /* trick */
|
---|
3417 | exec->state = to;
|
---|
3418 | xmlFARegExecSave(exec);
|
---|
3419 | exec->transno = transno;
|
---|
3420 | exec->state = state;
|
---|
3421 | }
|
---|
3422 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
|
---|
3423 | len);
|
---|
3424 | ret = xmlRegCheckCharacter(atom, codepoint);
|
---|
3425 | exec->transcount++;
|
---|
3426 | } while (ret == 1);
|
---|
3427 | if (exec->transcount < atom->min)
|
---|
3428 | ret = 0;
|
---|
3429 |
|
---|
3430 | /*
|
---|
3431 | * If the last check failed but one transition was found
|
---|
3432 | * possible, rollback
|
---|
3433 | */
|
---|
3434 | if (ret < 0)
|
---|
3435 | ret = 0;
|
---|
3436 | if (ret == 0) {
|
---|
3437 | goto rollback;
|
---|
3438 | }
|
---|
3439 | if (trans->counter >= 0) {
|
---|
3440 | if (exec->counts == NULL) {
|
---|
3441 | exec->status = -1;
|
---|
3442 | goto error;
|
---|
3443 | }
|
---|
3444 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3445 | printf("Decreasing count %d\n", trans->counter);
|
---|
3446 | #endif
|
---|
3447 | exec->counts[trans->counter]--;
|
---|
3448 | }
|
---|
3449 | } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) {
|
---|
3450 | /*
|
---|
3451 | * we don't match on the codepoint, but minOccurs of 0
|
---|
3452 | * says that's ok. Setting len to 0 inhibits stepping
|
---|
3453 | * over the codepoint.
|
---|
3454 | */
|
---|
3455 | exec->transcount = 1;
|
---|
3456 | len = 0;
|
---|
3457 | ret = 1;
|
---|
3458 | }
|
---|
3459 | } else if ((atom->min == 0) && (atom->max > 0)) {
|
---|
3460 | /* another spot to match when minOccurs is 0 */
|
---|
3461 | exec->transcount = 1;
|
---|
3462 | len = 0;
|
---|
3463 | ret = 1;
|
---|
3464 | }
|
---|
3465 | if (ret == 1) {
|
---|
3466 | if ((trans->nd == 1) ||
|
---|
3467 | ((trans->count >= 0) && (deter == 0) &&
|
---|
3468 | (exec->state->nbTrans > exec->transno + 1))) {
|
---|
3469 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3470 | if (trans->nd == 1)
|
---|
3471 | printf("Saving on nd transition atom %d for %c at %d\n",
|
---|
3472 | trans->atom->no, codepoint, exec->index);
|
---|
3473 | else
|
---|
3474 | printf("Saving on counted transition count %d for %c at %d\n",
|
---|
3475 | trans->count, codepoint, exec->index);
|
---|
3476 | #endif
|
---|
3477 | xmlFARegExecSave(exec);
|
---|
3478 | }
|
---|
3479 | if (trans->counter >= 0) {
|
---|
3480 | xmlRegCounterPtr counter;
|
---|
3481 |
|
---|
3482 | /* make sure we don't go over the counter maximum value */
|
---|
3483 | if ((exec->counts == NULL) ||
|
---|
3484 | (exec->comp == NULL) ||
|
---|
3485 | (exec->comp->counters == NULL)) {
|
---|
3486 | exec->status = -1;
|
---|
3487 | goto error;
|
---|
3488 | }
|
---|
3489 | counter = &exec->comp->counters[trans->counter];
|
---|
3490 | if (exec->counts[trans->counter] >= counter->max)
|
---|
3491 | continue; /* for loop on transitions */
|
---|
3492 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3493 | printf("Increasing count %d\n", trans->counter);
|
---|
3494 | #endif
|
---|
3495 | exec->counts[trans->counter]++;
|
---|
3496 | }
|
---|
3497 | if ((trans->count >= 0) &&
|
---|
3498 | (trans->count < REGEXP_ALL_COUNTER)) {
|
---|
3499 | if (exec->counts == NULL) {
|
---|
3500 | exec->status = -1;
|
---|
3501 | goto error;
|
---|
3502 | }
|
---|
3503 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3504 | printf("resetting count %d on transition\n",
|
---|
3505 | trans->count);
|
---|
3506 | #endif
|
---|
3507 | exec->counts[trans->count] = 0;
|
---|
3508 | }
|
---|
3509 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3510 | printf("entering state %d\n", trans->to);
|
---|
3511 | #endif
|
---|
3512 | exec->state = comp->states[trans->to];
|
---|
3513 | exec->transno = 0;
|
---|
3514 | if (trans->atom != NULL) {
|
---|
3515 | exec->index += len;
|
---|
3516 | }
|
---|
3517 | goto progress;
|
---|
3518 | } else if (ret < 0) {
|
---|
3519 | exec->status = -4;
|
---|
3520 | break;
|
---|
3521 | }
|
---|
3522 | }
|
---|
3523 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
|
---|
3524 | rollback:
|
---|
3525 | /*
|
---|
3526 | * Failed to find a way out
|
---|
3527 | */
|
---|
3528 | exec->determinist = 0;
|
---|
3529 | #ifdef DEBUG_REGEXP_EXEC
|
---|
3530 | printf("rollback from state %d on %d:%c\n", exec->state->no,
|
---|
3531 | codepoint,codepoint);
|
---|
3532 | #endif
|
---|
3533 | xmlFARegExecRollBack(exec);
|
---|
3534 | }
|
---|
3535 | progress:
|
---|
3536 | continue;
|
---|
3537 | }
|
---|
3538 | error:
|
---|
3539 | if (exec->rollbacks != NULL) {
|
---|
3540 | if (exec->counts != NULL) {
|
---|
3541 | int i;
|
---|
3542 |
|
---|
3543 | for (i = 0;i < exec->maxRollbacks;i++)
|
---|
3544 | if (exec->rollbacks[i].counts != NULL)
|
---|
3545 | xmlFree(exec->rollbacks[i].counts);
|
---|
3546 | }
|
---|
3547 | xmlFree(exec->rollbacks);
|
---|
3548 | }
|
---|
3549 | if (exec->state == NULL)
|
---|
3550 | return(-1);
|
---|
3551 | if (exec->counts != NULL)
|
---|
3552 | xmlFree(exec->counts);
|
---|
3553 | if (exec->status == 0)
|
---|
3554 | return(1);
|
---|
3555 | if (exec->status == -1) {
|
---|
3556 | if (exec->nbPush > MAX_PUSH)
|
---|
3557 | return(-1);
|
---|
3558 | return(0);
|
---|
3559 | }
|
---|
3560 | return(exec->status);
|
---|
3561 | }
|
---|
3562 |
|
---|
3563 | /************************************************************************
|
---|
3564 | * *
|
---|
3565 | * Progressive interface to the verifier one atom at a time *
|
---|
3566 | * *
|
---|
3567 | ************************************************************************/
|
---|
3568 | #ifdef DEBUG_ERR
|
---|
3569 | static void testerr(xmlRegExecCtxtPtr exec);
|
---|
3570 | #endif
|
---|
3571 |
|
---|
3572 | /**
|
---|
3573 | * xmlRegNewExecCtxt:
|
---|
3574 | * @comp: a precompiled regular expression
|
---|
3575 | * @callback: a callback function used for handling progresses in the
|
---|
3576 | * automata matching phase
|
---|
3577 | * @data: the context data associated to the callback in this context
|
---|
3578 | *
|
---|
3579 | * Build a context used for progressive evaluation of a regexp.
|
---|
3580 | *
|
---|
3581 | * Returns the new context
|
---|
3582 | */
|
---|
3583 | xmlRegExecCtxtPtr
|
---|
3584 | xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) {
|
---|
3585 | xmlRegExecCtxtPtr exec;
|
---|
3586 |
|
---|
3587 | if (comp == NULL)
|
---|
3588 | return(NULL);
|
---|
3589 | if ((comp->compact == NULL) && (comp->states == NULL))
|
---|
3590 | return(NULL);
|
---|
3591 | exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt));
|
---|
3592 | if (exec == NULL) {
|
---|
3593 | xmlRegexpErrMemory(NULL, "creating execution context");
|
---|
3594 | return(NULL);
|
---|
3595 | }
|
---|
3596 | memset(exec, 0, sizeof(xmlRegExecCtxt));
|
---|
3597 | exec->inputString = NULL;
|
---|
3598 | exec->index = 0;
|
---|
3599 | exec->determinist = 1;
|
---|
3600 | exec->maxRollbacks = 0;
|
---|
3601 | exec->nbRollbacks = 0;
|
---|
3602 | exec->rollbacks = NULL;
|
---|
3603 | exec->status = 0;
|
---|
3604 | exec->comp = comp;
|
---|
3605 | if (comp->compact == NULL)
|
---|
3606 | exec->state = comp->states[0];
|
---|
3607 | exec->transno = 0;
|
---|
3608 | exec->transcount = 0;
|
---|
3609 | exec->callback = callback;
|
---|
3610 | exec->data = data;
|
---|
3611 | if (comp->nbCounters > 0) {
|
---|
3612 | /*
|
---|
3613 | * For error handling, exec->counts is allocated twice the size
|
---|
3614 | * the second half is used to store the data in case of rollback
|
---|
3615 | */
|
---|
3616 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)
|
---|
3617 | * 2);
|
---|
3618 | if (exec->counts == NULL) {
|
---|
3619 | xmlRegexpErrMemory(NULL, "creating execution context");
|
---|
3620 | xmlFree(exec);
|
---|
3621 | return(NULL);
|
---|
3622 | }
|
---|
3623 | memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2);
|
---|
3624 | exec->errCounts = &exec->counts[comp->nbCounters];
|
---|
3625 | } else {
|
---|
3626 | exec->counts = NULL;
|
---|
3627 | exec->errCounts = NULL;
|
---|
3628 | }
|
---|
3629 | exec->inputStackMax = 0;
|
---|
3630 | exec->inputStackNr = 0;
|
---|
3631 | exec->inputStack = NULL;
|
---|
3632 | exec->errStateNo = -1;
|
---|
3633 | exec->errString = NULL;
|
---|
3634 | exec->nbPush = 0;
|
---|
3635 | return(exec);
|
---|
3636 | }
|
---|
3637 |
|
---|
3638 | /**
|
---|
3639 | * xmlRegFreeExecCtxt:
|
---|
3640 | * @exec: a regular expression evaluation context
|
---|
3641 | *
|
---|
3642 | * Free the structures associated to a regular expression evaluation context.
|
---|
3643 | */
|
---|
3644 | void
|
---|
3645 | xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) {
|
---|
3646 | if (exec == NULL)
|
---|
3647 | return;
|
---|
3648 |
|
---|
3649 | if (exec->rollbacks != NULL) {
|
---|
3650 | if (exec->counts != NULL) {
|
---|
3651 | int i;
|
---|
3652 |
|
---|
3653 | for (i = 0;i < exec->maxRollbacks;i++)
|
---|
3654 | if (exec->rollbacks[i].counts != NULL)
|
---|
3655 | xmlFree(exec->rollbacks[i].counts);
|
---|
3656 | }
|
---|
3657 | xmlFree(exec->rollbacks);
|
---|
3658 | }
|
---|
3659 | if (exec->counts != NULL)
|
---|
3660 | xmlFree(exec->counts);
|
---|
3661 | if (exec->inputStack != NULL) {
|
---|
3662 | int i;
|
---|
3663 |
|
---|
3664 | for (i = 0;i < exec->inputStackNr;i++) {
|
---|
3665 | if (exec->inputStack[i].value != NULL)
|
---|
3666 | xmlFree(exec->inputStack[i].value);
|
---|
3667 | }
|
---|
3668 | xmlFree(exec->inputStack);
|
---|
3669 | }
|
---|
3670 | if (exec->errString != NULL)
|
---|
3671 | xmlFree(exec->errString);
|
---|
3672 | xmlFree(exec);
|
---|
3673 | }
|
---|
3674 |
|
---|
3675 | static void
|
---|
3676 | xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value,
|
---|
3677 | void *data) {
|
---|
3678 | #ifdef DEBUG_PUSH
|
---|
3679 | printf("saving value: %d:%s\n", exec->inputStackNr, value);
|
---|
3680 | #endif
|
---|
3681 | if (exec->inputStackMax == 0) {
|
---|
3682 | exec->inputStackMax = 4;
|
---|
3683 | exec->inputStack = (xmlRegInputTokenPtr)
|
---|
3684 | xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken));
|
---|
3685 | if (exec->inputStack == NULL) {
|
---|
3686 | xmlRegexpErrMemory(NULL, "pushing input string");
|
---|
3687 | exec->inputStackMax = 0;
|
---|
3688 | return;
|
---|
3689 | }
|
---|
3690 | } else if (exec->inputStackNr + 1 >= exec->inputStackMax) {
|
---|
3691 | xmlRegInputTokenPtr tmp;
|
---|
3692 |
|
---|
3693 | exec->inputStackMax *= 2;
|
---|
3694 | tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack,
|
---|
3695 | exec->inputStackMax * sizeof(xmlRegInputToken));
|
---|
3696 | if (tmp == NULL) {
|
---|
3697 | xmlRegexpErrMemory(NULL, "pushing input string");
|
---|
3698 | exec->inputStackMax /= 2;
|
---|
3699 | return;
|
---|
3700 | }
|
---|
3701 | exec->inputStack = tmp;
|
---|
3702 | }
|
---|
3703 | exec->inputStack[exec->inputStackNr].value = xmlStrdup(value);
|
---|
3704 | exec->inputStack[exec->inputStackNr].data = data;
|
---|
3705 | exec->inputStackNr++;
|
---|
3706 | exec->inputStack[exec->inputStackNr].value = NULL;
|
---|
3707 | exec->inputStack[exec->inputStackNr].data = NULL;
|
---|
3708 | }
|
---|
3709 |
|
---|
3710 | /**
|
---|
3711 | * xmlRegStrEqualWildcard:
|
---|
3712 | * @expStr: the string to be evaluated
|
---|
3713 | * @valStr: the validation string
|
---|
3714 | *
|
---|
3715 | * Checks if both strings are equal or have the same content. "*"
|
---|
3716 | * can be used as a wildcard in @valStr; "|" is used as a separator of
|
---|
3717 | * substrings in both @expStr and @valStr.
|
---|
3718 | *
|
---|
3719 | * Returns 1 if the comparison is satisfied and the number of substrings
|
---|
3720 | * is equal, 0 otherwise.
|
---|
3721 | */
|
---|
3722 |
|
---|
3723 | static int
|
---|
3724 | xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) {
|
---|
3725 | if (expStr == valStr) return(1);
|
---|
3726 | if (expStr == NULL) return(0);
|
---|
3727 | if (valStr == NULL) return(0);
|
---|
3728 | do {
|
---|
3729 | /*
|
---|
3730 | * Eval if we have a wildcard for the current item.
|
---|
3731 | */
|
---|
3732 | if (*expStr != *valStr) {
|
---|
3733 | /* if one of them starts with a wildcard make valStr be it */
|
---|
3734 | if (*valStr == '*') {
|
---|
3735 | const xmlChar *tmp;
|
---|
3736 |
|
---|
3737 | tmp = valStr;
|
---|
3738 | valStr = expStr;
|
---|
3739 | expStr = tmp;
|
---|
3740 | }
|
---|
3741 | if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) {
|
---|
3742 | do {
|
---|
3743 | if (*valStr == XML_REG_STRING_SEPARATOR)
|
---|
3744 | break;
|
---|
3745 | valStr++;
|
---|
3746 | } while (*valStr != 0);
|
---|
3747 | continue;
|
---|
3748 | } else
|
---|
3749 | return(0);
|
---|
3750 | }
|
---|
3751 | expStr++;
|
---|
3752 | valStr++;
|
---|
3753 | } while (*valStr != 0);
|
---|
3754 | if (*expStr != 0)
|
---|
3755 | return (0);
|
---|
3756 | else
|
---|
3757 | return (1);
|
---|
3758 | }
|
---|
3759 |
|
---|
3760 | /**
|
---|
3761 | * xmlRegCompactPushString:
|
---|
3762 | * @exec: a regexp execution context
|
---|
3763 | * @comp: the precompiled exec with a compact table
|
---|
3764 | * @value: a string token input
|
---|
3765 | * @data: data associated to the token to reuse in callbacks
|
---|
3766 | *
|
---|
3767 | * Push one input token in the execution context
|
---|
3768 | *
|
---|
3769 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and
|
---|
3770 | * a negative value in case of error.
|
---|
3771 | */
|
---|
3772 | static int
|
---|
3773 | xmlRegCompactPushString(xmlRegExecCtxtPtr exec,
|
---|
3774 | xmlRegexpPtr comp,
|
---|
3775 | const xmlChar *value,
|
---|
3776 | void *data) {
|
---|
3777 | int state = exec->index;
|
---|
3778 | int i, target;
|
---|
3779 |
|
---|
3780 | if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL))
|
---|
3781 | return(-1);
|
---|
3782 |
|
---|
3783 | if (value == NULL) {
|
---|
3784 | /*
|
---|
3785 | * are we at a final state ?
|
---|
3786 | */
|
---|
3787 | if (comp->compact[state * (comp->nbstrings + 1)] ==
|
---|
3788 | XML_REGEXP_FINAL_STATE)
|
---|
3789 | return(1);
|
---|
3790 | return(0);
|
---|
3791 | }
|
---|
3792 |
|
---|
3793 | #ifdef DEBUG_PUSH
|
---|
3794 | printf("value pushed: %s\n", value);
|
---|
3795 | #endif
|
---|
3796 |
|
---|
3797 | /*
|
---|
3798 | * Examine all outside transitions from current state
|
---|
3799 | */
|
---|
3800 | for (i = 0;i < comp->nbstrings;i++) {
|
---|
3801 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
|
---|
3802 | if ((target > 0) && (target <= comp->nbstates)) {
|
---|
3803 | target--; /* to avoid 0 */
|
---|
3804 | if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) {
|
---|
3805 | exec->index = target;
|
---|
3806 | if ((exec->callback != NULL) && (comp->transdata != NULL)) {
|
---|
3807 | exec->callback(exec->data, value,
|
---|
3808 | comp->transdata[state * comp->nbstrings + i], data);
|
---|
3809 | }
|
---|
3810 | #ifdef DEBUG_PUSH
|
---|
3811 | printf("entering state %d\n", target);
|
---|
3812 | #endif
|
---|
3813 | if (comp->compact[target * (comp->nbstrings + 1)] ==
|
---|
3814 | XML_REGEXP_SINK_STATE)
|
---|
3815 | goto error;
|
---|
3816 |
|
---|
3817 | if (comp->compact[target * (comp->nbstrings + 1)] ==
|
---|
3818 | XML_REGEXP_FINAL_STATE)
|
---|
3819 | return(1);
|
---|
3820 | return(0);
|
---|
3821 | }
|
---|
3822 | }
|
---|
3823 | }
|
---|
3824 | /*
|
---|
3825 | * Failed to find an exit transition out from current state for the
|
---|
3826 | * current token
|
---|
3827 | */
|
---|
3828 | #ifdef DEBUG_PUSH
|
---|
3829 | printf("failed to find a transition for %s on state %d\n", value, state);
|
---|
3830 | #endif
|
---|
3831 | error:
|
---|
3832 | if (exec->errString != NULL)
|
---|
3833 | xmlFree(exec->errString);
|
---|
3834 | exec->errString = xmlStrdup(value);
|
---|
3835 | exec->errStateNo = state;
|
---|
3836 | exec->status = -1;
|
---|
3837 | #ifdef DEBUG_ERR
|
---|
3838 | testerr(exec);
|
---|
3839 | #endif
|
---|
3840 | return(-1);
|
---|
3841 | }
|
---|
3842 |
|
---|
3843 | /**
|
---|
3844 | * xmlRegExecPushStringInternal:
|
---|
3845 | * @exec: a regexp execution context or NULL to indicate the end
|
---|
3846 | * @value: a string token input
|
---|
3847 | * @data: data associated to the token to reuse in callbacks
|
---|
3848 | * @compound: value was assembled from 2 strings
|
---|
3849 | *
|
---|
3850 | * Push one input token in the execution context
|
---|
3851 | *
|
---|
3852 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and
|
---|
3853 | * a negative value in case of error.
|
---|
3854 | */
|
---|
3855 | static int
|
---|
3856 | xmlRegExecPushStringInternal(xmlRegExecCtxtPtr exec, const xmlChar *value,
|
---|
3857 | void *data, int compound) {
|
---|
3858 | xmlRegTransPtr trans;
|
---|
3859 | xmlRegAtomPtr atom;
|
---|
3860 | int ret;
|
---|
3861 | int final = 0;
|
---|
3862 | int progress = 1;
|
---|
3863 |
|
---|
3864 | if (exec == NULL)
|
---|
3865 | return(-1);
|
---|
3866 | if (exec->comp == NULL)
|
---|
3867 | return(-1);
|
---|
3868 | if (exec->status != 0)
|
---|
3869 | return(exec->status);
|
---|
3870 |
|
---|
3871 | if (exec->comp->compact != NULL)
|
---|
3872 | return(xmlRegCompactPushString(exec, exec->comp, value, data));
|
---|
3873 |
|
---|
3874 | if (value == NULL) {
|
---|
3875 | if (exec->state->type == XML_REGEXP_FINAL_STATE)
|
---|
3876 | return(1);
|
---|
3877 | final = 1;
|
---|
3878 | }
|
---|
3879 |
|
---|
3880 | #ifdef DEBUG_PUSH
|
---|
3881 | printf("value pushed: %s\n", value);
|
---|
3882 | #endif
|
---|
3883 | /*
|
---|
3884 | * If we have an active rollback stack push the new value there
|
---|
3885 | * and get back to where we were left
|
---|
3886 | */
|
---|
3887 | if ((value != NULL) && (exec->inputStackNr > 0)) {
|
---|
3888 | xmlFARegExecSaveInputString(exec, value, data);
|
---|
3889 | value = exec->inputStack[exec->index].value;
|
---|
3890 | data = exec->inputStack[exec->index].data;
|
---|
3891 | #ifdef DEBUG_PUSH
|
---|
3892 | printf("value loaded: %s\n", value);
|
---|
3893 | #endif
|
---|
3894 | }
|
---|
3895 |
|
---|
3896 | while ((exec->status == 0) &&
|
---|
3897 | ((value != NULL) ||
|
---|
3898 | ((final == 1) &&
|
---|
3899 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) {
|
---|
3900 |
|
---|
3901 | /*
|
---|
3902 | * End of input on non-terminal state, rollback, however we may
|
---|
3903 | * still have epsilon like transition for counted transitions
|
---|
3904 | * on counters, in that case don't break too early.
|
---|
3905 | */
|
---|
3906 | if ((value == NULL) && (exec->counts == NULL))
|
---|
3907 | goto rollback;
|
---|
3908 |
|
---|
3909 | exec->transcount = 0;
|
---|
3910 | for (;exec->transno < exec->state->nbTrans;exec->transno++) {
|
---|
3911 | trans = &exec->state->trans[exec->transno];
|
---|
3912 | if (trans->to < 0)
|
---|
3913 | continue;
|
---|
3914 | atom = trans->atom;
|
---|
3915 | ret = 0;
|
---|
3916 | if (trans->count == REGEXP_ALL_LAX_COUNTER) {
|
---|
3917 | int i;
|
---|
3918 | int count;
|
---|
3919 | xmlRegTransPtr t;
|
---|
3920 | xmlRegCounterPtr counter;
|
---|
3921 |
|
---|
3922 | ret = 0;
|
---|
3923 |
|
---|
3924 | #ifdef DEBUG_PUSH
|
---|
3925 | printf("testing all lax %d\n", trans->count);
|
---|
3926 | #endif
|
---|
3927 | /*
|
---|
3928 | * Check all counted transitions from the current state
|
---|
3929 | */
|
---|
3930 | if ((value == NULL) && (final)) {
|
---|
3931 | ret = 1;
|
---|
3932 | } else if (value != NULL) {
|
---|
3933 | for (i = 0;i < exec->state->nbTrans;i++) {
|
---|
3934 | t = &exec->state->trans[i];
|
---|
3935 | if ((t->counter < 0) || (t == trans))
|
---|
3936 | continue;
|
---|
3937 | counter = &exec->comp->counters[t->counter];
|
---|
3938 | count = exec->counts[t->counter];
|
---|
3939 | if ((count < counter->max) &&
|
---|
3940 | (t->atom != NULL) &&
|
---|
3941 | (xmlStrEqual(value, t->atom->valuep))) {
|
---|
3942 | ret = 0;
|
---|
3943 | break;
|
---|
3944 | }
|
---|
3945 | if ((count >= counter->min) &&
|
---|
3946 | (count < counter->max) &&
|
---|
3947 | (t->atom != NULL) &&
|
---|
3948 | (xmlStrEqual(value, t->atom->valuep))) {
|
---|
3949 | ret = 1;
|
---|
3950 | break;
|
---|
3951 | }
|
---|
3952 | }
|
---|
3953 | }
|
---|
3954 | } else if (trans->count == REGEXP_ALL_COUNTER) {
|
---|
3955 | int i;
|
---|
3956 | int count;
|
---|
3957 | xmlRegTransPtr t;
|
---|
3958 | xmlRegCounterPtr counter;
|
---|
3959 |
|
---|
3960 | ret = 1;
|
---|
3961 |
|
---|
3962 | #ifdef DEBUG_PUSH
|
---|
3963 | printf("testing all %d\n", trans->count);
|
---|
3964 | #endif
|
---|
3965 | /*
|
---|
3966 | * Check all counted transitions from the current state
|
---|
3967 | */
|
---|
3968 | for (i = 0;i < exec->state->nbTrans;i++) {
|
---|
3969 | t = &exec->state->trans[i];
|
---|
3970 | if ((t->counter < 0) || (t == trans))
|
---|
3971 | continue;
|
---|
3972 | counter = &exec->comp->counters[t->counter];
|
---|
3973 | count = exec->counts[t->counter];
|
---|
3974 | if ((count < counter->min) || (count > counter->max)) {
|
---|
3975 | ret = 0;
|
---|
3976 | break;
|
---|
3977 | }
|
---|
3978 | }
|
---|
3979 | } else if (trans->count >= 0) {
|
---|
3980 | int count;
|
---|
3981 | xmlRegCounterPtr counter;
|
---|
3982 |
|
---|
3983 | /*
|
---|
3984 | * A counted transition.
|
---|
3985 | */
|
---|
3986 |
|
---|
3987 | count = exec->counts[trans->count];
|
---|
3988 | counter = &exec->comp->counters[trans->count];
|
---|
3989 | #ifdef DEBUG_PUSH
|
---|
3990 | printf("testing count %d: val %d, min %d, max %d\n",
|
---|
3991 | trans->count, count, counter->min, counter->max);
|
---|
3992 | #endif
|
---|
3993 | ret = ((count >= counter->min) && (count <= counter->max));
|
---|
3994 | } else if (atom == NULL) {
|
---|
3995 | fprintf(stderr, "epsilon transition left at runtime\n");
|
---|
3996 | exec->status = -2;
|
---|
3997 | break;
|
---|
3998 | } else if (value != NULL) {
|
---|
3999 | ret = xmlRegStrEqualWildcard(atom->valuep, value);
|
---|
4000 | if (atom->neg) {
|
---|
4001 | ret = !ret;
|
---|
4002 | if (!compound)
|
---|
4003 | ret = 0;
|
---|
4004 | }
|
---|
4005 | if ((ret == 1) && (trans->counter >= 0)) {
|
---|
4006 | xmlRegCounterPtr counter;
|
---|
4007 | int count;
|
---|
4008 |
|
---|
4009 | count = exec->counts[trans->counter];
|
---|
4010 | counter = &exec->comp->counters[trans->counter];
|
---|
4011 | if (count >= counter->max)
|
---|
4012 | ret = 0;
|
---|
4013 | }
|
---|
4014 |
|
---|
4015 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
|
---|
4016 | xmlRegStatePtr to = exec->comp->states[trans->to];
|
---|
4017 |
|
---|
4018 | /*
|
---|
4019 | * this is a multiple input sequence
|
---|
4020 | */
|
---|
4021 | if (exec->state->nbTrans > exec->transno + 1) {
|
---|
4022 | if (exec->inputStackNr <= 0) {
|
---|
4023 | xmlFARegExecSaveInputString(exec, value, data);
|
---|
4024 | }
|
---|
4025 | xmlFARegExecSave(exec);
|
---|
4026 | }
|
---|
4027 | exec->transcount = 1;
|
---|
4028 | do {
|
---|
4029 | /*
|
---|
4030 | * Try to progress as much as possible on the input
|
---|
4031 | */
|
---|
4032 | if (exec->transcount == atom->max) {
|
---|
4033 | break;
|
---|
4034 | }
|
---|
4035 | exec->index++;
|
---|
4036 | value = exec->inputStack[exec->index].value;
|
---|
4037 | data = exec->inputStack[exec->index].data;
|
---|
4038 | #ifdef DEBUG_PUSH
|
---|
4039 | printf("value loaded: %s\n", value);
|
---|
4040 | #endif
|
---|
4041 |
|
---|
4042 | /*
|
---|
4043 | * End of input: stop here
|
---|
4044 | */
|
---|
4045 | if (value == NULL) {
|
---|
4046 | exec->index --;
|
---|
4047 | break;
|
---|
4048 | }
|
---|
4049 | if (exec->transcount >= atom->min) {
|
---|
4050 | int transno = exec->transno;
|
---|
4051 | xmlRegStatePtr state = exec->state;
|
---|
4052 |
|
---|
4053 | /*
|
---|
4054 | * The transition is acceptable save it
|
---|
4055 | */
|
---|
4056 | exec->transno = -1; /* trick */
|
---|
4057 | exec->state = to;
|
---|
4058 | if (exec->inputStackNr <= 0) {
|
---|
4059 | xmlFARegExecSaveInputString(exec, value, data);
|
---|
4060 | }
|
---|
4061 | xmlFARegExecSave(exec);
|
---|
4062 | exec->transno = transno;
|
---|
4063 | exec->state = state;
|
---|
4064 | }
|
---|
4065 | ret = xmlStrEqual(value, atom->valuep);
|
---|
4066 | exec->transcount++;
|
---|
4067 | } while (ret == 1);
|
---|
4068 | if (exec->transcount < atom->min)
|
---|
4069 | ret = 0;
|
---|
4070 |
|
---|
4071 | /*
|
---|
4072 | * If the last check failed but one transition was found
|
---|
4073 | * possible, rollback
|
---|
4074 | */
|
---|
4075 | if (ret < 0)
|
---|
4076 | ret = 0;
|
---|
4077 | if (ret == 0) {
|
---|
4078 | goto rollback;
|
---|
4079 | }
|
---|
4080 | }
|
---|
4081 | }
|
---|
4082 | if (ret == 1) {
|
---|
4083 | if ((exec->callback != NULL) && (atom != NULL) &&
|
---|
4084 | (data != NULL)) {
|
---|
4085 | exec->callback(exec->data, atom->valuep,
|
---|
4086 | atom->data, data);
|
---|
4087 | }
|
---|
4088 | if (exec->state->nbTrans > exec->transno + 1) {
|
---|
4089 | if (exec->inputStackNr <= 0) {
|
---|
4090 | xmlFARegExecSaveInputString(exec, value, data);
|
---|
4091 | }
|
---|
4092 | xmlFARegExecSave(exec);
|
---|
4093 | }
|
---|
4094 | if (trans->counter >= 0) {
|
---|
4095 | #ifdef DEBUG_PUSH
|
---|
4096 | printf("Increasing count %d\n", trans->counter);
|
---|
4097 | #endif
|
---|
4098 | exec->counts[trans->counter]++;
|
---|
4099 | }
|
---|
4100 | if ((trans->count >= 0) &&
|
---|
4101 | (trans->count < REGEXP_ALL_COUNTER)) {
|
---|
4102 | #ifdef DEBUG_REGEXP_EXEC
|
---|
4103 | printf("resetting count %d on transition\n",
|
---|
4104 | trans->count);
|
---|
4105 | #endif
|
---|
4106 | exec->counts[trans->count] = 0;
|
---|
4107 | }
|
---|
4108 | #ifdef DEBUG_PUSH
|
---|
4109 | printf("entering state %d\n", trans->to);
|
---|
4110 | #endif
|
---|
4111 | if ((exec->comp->states[trans->to] != NULL) &&
|
---|
4112 | (exec->comp->states[trans->to]->type ==
|
---|
4113 | XML_REGEXP_SINK_STATE)) {
|
---|
4114 | /*
|
---|
4115 | * entering a sink state, save the current state as error
|
---|
4116 | * state.
|
---|
4117 | */
|
---|
4118 | if (exec->errString != NULL)
|
---|
4119 | xmlFree(exec->errString);
|
---|
4120 | exec->errString = xmlStrdup(value);
|
---|
4121 | exec->errState = exec->state;
|
---|
4122 | memcpy(exec->errCounts, exec->counts,
|
---|
4123 | exec->comp->nbCounters * sizeof(int));
|
---|
4124 | }
|
---|
4125 | exec->state = exec->comp->states[trans->to];
|
---|
4126 | exec->transno = 0;
|
---|
4127 | if (trans->atom != NULL) {
|
---|
4128 | if (exec->inputStack != NULL) {
|
---|
4129 | exec->index++;
|
---|
4130 | if (exec->index < exec->inputStackNr) {
|
---|
4131 | value = exec->inputStack[exec->index].value;
|
---|
4132 | data = exec->inputStack[exec->index].data;
|
---|
4133 | #ifdef DEBUG_PUSH
|
---|
4134 | printf("value loaded: %s\n", value);
|
---|
4135 | #endif
|
---|
4136 | } else {
|
---|
4137 | value = NULL;
|
---|
4138 | data = NULL;
|
---|
4139 | #ifdef DEBUG_PUSH
|
---|
4140 | printf("end of input\n");
|
---|
4141 | #endif
|
---|
4142 | }
|
---|
4143 | } else {
|
---|
4144 | value = NULL;
|
---|
4145 | data = NULL;
|
---|
4146 | #ifdef DEBUG_PUSH
|
---|
4147 | printf("end of input\n");
|
---|
4148 | #endif
|
---|
4149 | }
|
---|
4150 | }
|
---|
4151 | goto progress;
|
---|
4152 | } else if (ret < 0) {
|
---|
4153 | exec->status = -4;
|
---|
4154 | break;
|
---|
4155 | }
|
---|
4156 | }
|
---|
4157 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
|
---|
4158 | rollback:
|
---|
4159 | /*
|
---|
4160 | * if we didn't yet rollback on the current input
|
---|
4161 | * store the current state as the error state.
|
---|
4162 | */
|
---|
4163 | if ((progress) && (exec->state != NULL) &&
|
---|
4164 | (exec->state->type != XML_REGEXP_SINK_STATE)) {
|
---|
4165 | progress = 0;
|
---|
4166 | if (exec->errString != NULL)
|
---|
4167 | xmlFree(exec->errString);
|
---|
4168 | exec->errString = xmlStrdup(value);
|
---|
4169 | exec->errState = exec->state;
|
---|
4170 | if (exec->comp->nbCounters)
|
---|
4171 | memcpy(exec->errCounts, exec->counts,
|
---|
4172 | exec->comp->nbCounters * sizeof(int));
|
---|
4173 | }
|
---|
4174 |
|
---|
4175 | /*
|
---|
4176 | * Failed to find a way out
|
---|
4177 | */
|
---|
4178 | exec->determinist = 0;
|
---|
4179 | xmlFARegExecRollBack(exec);
|
---|
4180 | if ((exec->inputStack != NULL ) && (exec->status == 0)) {
|
---|
4181 | value = exec->inputStack[exec->index].value;
|
---|
4182 | data = exec->inputStack[exec->index].data;
|
---|
4183 | #ifdef DEBUG_PUSH
|
---|
4184 | printf("value loaded: %s\n", value);
|
---|
4185 | #endif
|
---|
4186 | }
|
---|
4187 | }
|
---|
4188 | continue;
|
---|
4189 | progress:
|
---|
4190 | progress = 1;
|
---|
4191 | continue;
|
---|
4192 | }
|
---|
4193 | if (exec->status == 0) {
|
---|
4194 | return(exec->state->type == XML_REGEXP_FINAL_STATE);
|
---|
4195 | }
|
---|
4196 | #ifdef DEBUG_ERR
|
---|
4197 | if (exec->status < 0) {
|
---|
4198 | testerr(exec);
|
---|
4199 | }
|
---|
4200 | #endif
|
---|
4201 | return(exec->status);
|
---|
4202 | }
|
---|
4203 |
|
---|
4204 | /**
|
---|
4205 | * xmlRegExecPushString:
|
---|
4206 | * @exec: a regexp execution context or NULL to indicate the end
|
---|
4207 | * @value: a string token input
|
---|
4208 | * @data: data associated to the token to reuse in callbacks
|
---|
4209 | *
|
---|
4210 | * Push one input token in the execution context
|
---|
4211 | *
|
---|
4212 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and
|
---|
4213 | * a negative value in case of error.
|
---|
4214 | */
|
---|
4215 | int
|
---|
4216 | xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value,
|
---|
4217 | void *data) {
|
---|
4218 | return(xmlRegExecPushStringInternal(exec, value, data, 0));
|
---|
4219 | }
|
---|
4220 |
|
---|
4221 | /**
|
---|
4222 | * xmlRegExecPushString2:
|
---|
4223 | * @exec: a regexp execution context or NULL to indicate the end
|
---|
4224 | * @value: the first string token input
|
---|
4225 | * @value2: the second string token input
|
---|
4226 | * @data: data associated to the token to reuse in callbacks
|
---|
4227 | *
|
---|
4228 | * Push one input token in the execution context
|
---|
4229 | *
|
---|
4230 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and
|
---|
4231 | * a negative value in case of error.
|
---|
4232 | */
|
---|
4233 | int
|
---|
4234 | xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value,
|
---|
4235 | const xmlChar *value2, void *data) {
|
---|
4236 | xmlChar buf[150];
|
---|
4237 | int lenn, lenp, ret;
|
---|
4238 | xmlChar *str;
|
---|
4239 |
|
---|
4240 | if (exec == NULL)
|
---|
4241 | return(-1);
|
---|
4242 | if (exec->comp == NULL)
|
---|
4243 | return(-1);
|
---|
4244 | if (exec->status != 0)
|
---|
4245 | return(exec->status);
|
---|
4246 |
|
---|
4247 | if (value2 == NULL)
|
---|
4248 | return(xmlRegExecPushString(exec, value, data));
|
---|
4249 |
|
---|
4250 | lenn = strlen((char *) value2);
|
---|
4251 | lenp = strlen((char *) value);
|
---|
4252 |
|
---|
4253 | if (150 < lenn + lenp + 2) {
|
---|
4254 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
|
---|
4255 | if (str == NULL) {
|
---|
4256 | exec->status = -1;
|
---|
4257 | return(-1);
|
---|
4258 | }
|
---|
4259 | } else {
|
---|
4260 | str = buf;
|
---|
4261 | }
|
---|
4262 | memcpy(&str[0], value, lenp);
|
---|
4263 | str[lenp] = XML_REG_STRING_SEPARATOR;
|
---|
4264 | memcpy(&str[lenp + 1], value2, lenn);
|
---|
4265 | str[lenn + lenp + 1] = 0;
|
---|
4266 |
|
---|
4267 | if (exec->comp->compact != NULL)
|
---|
4268 | ret = xmlRegCompactPushString(exec, exec->comp, str, data);
|
---|
4269 | else
|
---|
4270 | ret = xmlRegExecPushStringInternal(exec, str, data, 1);
|
---|
4271 |
|
---|
4272 | if (str != buf)
|
---|
4273 | xmlFree(str);
|
---|
4274 | return(ret);
|
---|
4275 | }
|
---|
4276 |
|
---|
4277 | /**
|
---|
4278 | * xmlRegExecGetValues:
|
---|
4279 | * @exec: a regexp execution context
|
---|
4280 | * @err: error extraction or normal one
|
---|
4281 | * @nbval: pointer to the number of accepted values IN/OUT
|
---|
4282 | * @nbneg: return number of negative transitions
|
---|
4283 | * @values: pointer to the array of acceptable values
|
---|
4284 | * @terminal: return value if this was a terminal state
|
---|
4285 | *
|
---|
4286 | * Extract information from the regexp execution, internal routine to
|
---|
4287 | * implement xmlRegExecNextValues() and xmlRegExecErrInfo()
|
---|
4288 | *
|
---|
4289 | * Returns: 0 in case of success or -1 in case of error.
|
---|
4290 | */
|
---|
4291 | static int
|
---|
4292 | xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err,
|
---|
4293 | int *nbval, int *nbneg,
|
---|
4294 | xmlChar **values, int *terminal) {
|
---|
4295 | int maxval;
|
---|
4296 | int nb = 0;
|
---|
4297 |
|
---|
4298 | if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) ||
|
---|
4299 | (values == NULL) || (*nbval <= 0))
|
---|
4300 | return(-1);
|
---|
4301 |
|
---|
4302 | maxval = *nbval;
|
---|
4303 | *nbval = 0;
|
---|
4304 | *nbneg = 0;
|
---|
4305 | if ((exec->comp != NULL) && (exec->comp->compact != NULL)) {
|
---|
4306 | xmlRegexpPtr comp;
|
---|
4307 | int target, i, state;
|
---|
4308 |
|
---|
4309 | comp = exec->comp;
|
---|
4310 |
|
---|
4311 | if (err) {
|
---|
4312 | if (exec->errStateNo == -1) return(-1);
|
---|
4313 | state = exec->errStateNo;
|
---|
4314 | } else {
|
---|
4315 | state = exec->index;
|
---|
4316 | }
|
---|
4317 | if (terminal != NULL) {
|
---|
4318 | if (comp->compact[state * (comp->nbstrings + 1)] ==
|
---|
4319 | XML_REGEXP_FINAL_STATE)
|
---|
4320 | *terminal = 1;
|
---|
4321 | else
|
---|
4322 | *terminal = 0;
|
---|
4323 | }
|
---|
4324 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
|
---|
4325 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
|
---|
4326 | if ((target > 0) && (target <= comp->nbstates) &&
|
---|
4327 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] !=
|
---|
4328 | XML_REGEXP_SINK_STATE)) {
|
---|
4329 | values[nb++] = comp->stringMap[i];
|
---|
4330 | (*nbval)++;
|
---|
4331 | }
|
---|
4332 | }
|
---|
4333 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) {
|
---|
4334 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1];
|
---|
4335 | if ((target > 0) && (target <= comp->nbstates) &&
|
---|
4336 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] ==
|
---|
4337 | XML_REGEXP_SINK_STATE)) {
|
---|
4338 | values[nb++] = comp->stringMap[i];
|
---|
4339 | (*nbneg)++;
|
---|
4340 | }
|
---|
4341 | }
|
---|
4342 | } else {
|
---|
4343 | int transno;
|
---|
4344 | xmlRegTransPtr trans;
|
---|
4345 | xmlRegAtomPtr atom;
|
---|
4346 | xmlRegStatePtr state;
|
---|
4347 |
|
---|
4348 | if (terminal != NULL) {
|
---|
4349 | if (exec->state->type == XML_REGEXP_FINAL_STATE)
|
---|
4350 | *terminal = 1;
|
---|
4351 | else
|
---|
4352 | *terminal = 0;
|
---|
4353 | }
|
---|
4354 |
|
---|
4355 | if (err) {
|
---|
4356 | if (exec->errState == NULL) return(-1);
|
---|
4357 | state = exec->errState;
|
---|
4358 | } else {
|
---|
4359 | if (exec->state == NULL) return(-1);
|
---|
4360 | state = exec->state;
|
---|
4361 | }
|
---|
4362 | for (transno = 0;
|
---|
4363 | (transno < state->nbTrans) && (nb < maxval);
|
---|
4364 | transno++) {
|
---|
4365 | trans = &state->trans[transno];
|
---|
4366 | if (trans->to < 0)
|
---|
4367 | continue;
|
---|
4368 | atom = trans->atom;
|
---|
4369 | if ((atom == NULL) || (atom->valuep == NULL))
|
---|
4370 | continue;
|
---|
4371 | if (trans->count == REGEXP_ALL_LAX_COUNTER) {
|
---|
4372 | /* this should not be reached but ... */
|
---|
4373 | TODO;
|
---|
4374 | } else if (trans->count == REGEXP_ALL_COUNTER) {
|
---|
4375 | /* this should not be reached but ... */
|
---|
4376 | TODO;
|
---|
4377 | } else if (trans->counter >= 0) {
|
---|
4378 | xmlRegCounterPtr counter = NULL;
|
---|
4379 | int count;
|
---|
4380 |
|
---|
4381 | if (err)
|
---|
4382 | count = exec->errCounts[trans->counter];
|
---|
4383 | else
|
---|
4384 | count = exec->counts[trans->counter];
|
---|
4385 | if (exec->comp != NULL)
|
---|
4386 | counter = &exec->comp->counters[trans->counter];
|
---|
4387 | if ((counter == NULL) || (count < counter->max)) {
|
---|
4388 | if (atom->neg)
|
---|
4389 | values[nb++] = (xmlChar *) atom->valuep2;
|
---|
4390 | else
|
---|
4391 | values[nb++] = (xmlChar *) atom->valuep;
|
---|
4392 | (*nbval)++;
|
---|
4393 | }
|
---|
4394 | } else {
|
---|
4395 | if ((exec->comp != NULL) && (exec->comp->states[trans->to] != NULL) &&
|
---|
4396 | (exec->comp->states[trans->to]->type !=
|
---|
4397 | XML_REGEXP_SINK_STATE)) {
|
---|
4398 | if (atom->neg)
|
---|
4399 | values[nb++] = (xmlChar *) atom->valuep2;
|
---|
4400 | else
|
---|
4401 | values[nb++] = (xmlChar *) atom->valuep;
|
---|
4402 | (*nbval)++;
|
---|
4403 | }
|
---|
4404 | }
|
---|
4405 | }
|
---|
4406 | for (transno = 0;
|
---|
4407 | (transno < state->nbTrans) && (nb < maxval);
|
---|
4408 | transno++) {
|
---|
4409 | trans = &state->trans[transno];
|
---|
4410 | if (trans->to < 0)
|
---|
4411 | continue;
|
---|
4412 | atom = trans->atom;
|
---|
4413 | if ((atom == NULL) || (atom->valuep == NULL))
|
---|
4414 | continue;
|
---|
4415 | if (trans->count == REGEXP_ALL_LAX_COUNTER) {
|
---|
4416 | continue;
|
---|
4417 | } else if (trans->count == REGEXP_ALL_COUNTER) {
|
---|
4418 | continue;
|
---|
4419 | } else if (trans->counter >= 0) {
|
---|
4420 | continue;
|
---|
4421 | } else {
|
---|
4422 | if ((exec->comp->states[trans->to] != NULL) &&
|
---|
4423 | (exec->comp->states[trans->to]->type ==
|
---|
4424 | XML_REGEXP_SINK_STATE)) {
|
---|
4425 | if (atom->neg)
|
---|
4426 | values[nb++] = (xmlChar *) atom->valuep2;
|
---|
4427 | else
|
---|
4428 | values[nb++] = (xmlChar *) atom->valuep;
|
---|
4429 | (*nbneg)++;
|
---|
4430 | }
|
---|
4431 | }
|
---|
4432 | }
|
---|
4433 | }
|
---|
4434 | return(0);
|
---|
4435 | }
|
---|
4436 |
|
---|
4437 | /**
|
---|
4438 | * xmlRegExecNextValues:
|
---|
4439 | * @exec: a regexp execution context
|
---|
4440 | * @nbval: pointer to the number of accepted values IN/OUT
|
---|
4441 | * @nbneg: return number of negative transitions
|
---|
4442 | * @values: pointer to the array of acceptable values
|
---|
4443 | * @terminal: return value if this was a terminal state
|
---|
4444 | *
|
---|
4445 | * Extract information from the regexp execution,
|
---|
4446 | * the parameter @values must point to an array of @nbval string pointers
|
---|
4447 | * on return nbval will contain the number of possible strings in that
|
---|
4448 | * state and the @values array will be updated with them. The string values
|
---|
4449 | * returned will be freed with the @exec context and don't need to be
|
---|
4450 | * deallocated.
|
---|
4451 | *
|
---|
4452 | * Returns: 0 in case of success or -1 in case of error.
|
---|
4453 | */
|
---|
4454 | int
|
---|
4455 | xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg,
|
---|
4456 | xmlChar **values, int *terminal) {
|
---|
4457 | return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal));
|
---|
4458 | }
|
---|
4459 |
|
---|
4460 | /**
|
---|
4461 | * xmlRegExecErrInfo:
|
---|
4462 | * @exec: a regexp execution context generating an error
|
---|
4463 | * @string: return value for the error string
|
---|
4464 | * @nbval: pointer to the number of accepted values IN/OUT
|
---|
4465 | * @nbneg: return number of negative transitions
|
---|
4466 | * @values: pointer to the array of acceptable values
|
---|
4467 | * @terminal: return value if this was a terminal state
|
---|
4468 | *
|
---|
4469 | * Extract error information from the regexp execution, the parameter
|
---|
4470 | * @string will be updated with the value pushed and not accepted,
|
---|
4471 | * the parameter @values must point to an array of @nbval string pointers
|
---|
4472 | * on return nbval will contain the number of possible strings in that
|
---|
4473 | * state and the @values array will be updated with them. The string values
|
---|
4474 | * returned will be freed with the @exec context and don't need to be
|
---|
4475 | * deallocated.
|
---|
4476 | *
|
---|
4477 | * Returns: 0 in case of success or -1 in case of error.
|
---|
4478 | */
|
---|
4479 | int
|
---|
4480 | xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string,
|
---|
4481 | int *nbval, int *nbneg, xmlChar **values, int *terminal) {
|
---|
4482 | if (exec == NULL)
|
---|
4483 | return(-1);
|
---|
4484 | if (string != NULL) {
|
---|
4485 | if (exec->status != 0)
|
---|
4486 | *string = exec->errString;
|
---|
4487 | else
|
---|
4488 | *string = NULL;
|
---|
4489 | }
|
---|
4490 | return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal));
|
---|
4491 | }
|
---|
4492 |
|
---|
4493 | #ifdef DEBUG_ERR
|
---|
4494 | static void testerr(xmlRegExecCtxtPtr exec) {
|
---|
4495 | const xmlChar *string;
|
---|
4496 | xmlChar *values[5];
|
---|
4497 | int nb = 5;
|
---|
4498 | int nbneg;
|
---|
4499 | int terminal;
|
---|
4500 | xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal);
|
---|
4501 | }
|
---|
4502 | #endif
|
---|
4503 |
|
---|
4504 | #if 0
|
---|
4505 | static int
|
---|
4506 | xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) {
|
---|
4507 | xmlRegTransPtr trans;
|
---|
4508 | xmlRegAtomPtr atom;
|
---|
4509 | int ret;
|
---|
4510 | int codepoint, len;
|
---|
4511 |
|
---|
4512 | if (exec == NULL)
|
---|
4513 | return(-1);
|
---|
4514 | if (exec->status != 0)
|
---|
4515 | return(exec->status);
|
---|
4516 |
|
---|
4517 | while ((exec->status == 0) &&
|
---|
4518 | ((exec->inputString[exec->index] != 0) ||
|
---|
4519 | (exec->state->type != XML_REGEXP_FINAL_STATE))) {
|
---|
4520 |
|
---|
4521 | /*
|
---|
4522 | * End of input on non-terminal state, rollback, however we may
|
---|
4523 | * still have epsilon like transition for counted transitions
|
---|
4524 | * on counters, in that case don't break too early.
|
---|
4525 | */
|
---|
4526 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL))
|
---|
4527 | goto rollback;
|
---|
4528 |
|
---|
4529 | exec->transcount = 0;
|
---|
4530 | for (;exec->transno < exec->state->nbTrans;exec->transno++) {
|
---|
4531 | trans = &exec->state->trans[exec->transno];
|
---|
4532 | if (trans->to < 0)
|
---|
4533 | continue;
|
---|
4534 | atom = trans->atom;
|
---|
4535 | ret = 0;
|
---|
4536 | if (trans->count >= 0) {
|
---|
4537 | int count;
|
---|
4538 | xmlRegCounterPtr counter;
|
---|
4539 |
|
---|
4540 | /*
|
---|
4541 | * A counted transition.
|
---|
4542 | */
|
---|
4543 |
|
---|
4544 | count = exec->counts[trans->count];
|
---|
4545 | counter = &exec->comp->counters[trans->count];
|
---|
4546 | #ifdef DEBUG_REGEXP_EXEC
|
---|
4547 | printf("testing count %d: val %d, min %d, max %d\n",
|
---|
4548 | trans->count, count, counter->min, counter->max);
|
---|
4549 | #endif
|
---|
4550 | ret = ((count >= counter->min) && (count <= counter->max));
|
---|
4551 | } else if (atom == NULL) {
|
---|
4552 | fprintf(stderr, "epsilon transition left at runtime\n");
|
---|
4553 | exec->status = -2;
|
---|
4554 | break;
|
---|
4555 | } else if (exec->inputString[exec->index] != 0) {
|
---|
4556 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len);
|
---|
4557 | ret = xmlRegCheckCharacter(atom, codepoint);
|
---|
4558 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) {
|
---|
4559 | xmlRegStatePtr to = exec->comp->states[trans->to];
|
---|
4560 |
|
---|
4561 | /*
|
---|
4562 | * this is a multiple input sequence
|
---|
4563 | */
|
---|
4564 | if (exec->state->nbTrans > exec->transno + 1) {
|
---|
4565 | xmlFARegExecSave(exec);
|
---|
4566 | }
|
---|
4567 | exec->transcount = 1;
|
---|
4568 | do {
|
---|
4569 | /*
|
---|
4570 | * Try to progress as much as possible on the input
|
---|
4571 | */
|
---|
4572 | if (exec->transcount == atom->max) {
|
---|
4573 | break;
|
---|
4574 | }
|
---|
4575 | exec->index += len;
|
---|
4576 | /*
|
---|
4577 | * End of input: stop here
|
---|
4578 | */
|
---|
4579 | if (exec->inputString[exec->index] == 0) {
|
---|
4580 | exec->index -= len;
|
---|
4581 | break;
|
---|
4582 | }
|
---|
4583 | if (exec->transcount >= atom->min) {
|
---|
4584 | int transno = exec->transno;
|
---|
4585 | xmlRegStatePtr state = exec->state;
|
---|
4586 |
|
---|
4587 | /*
|
---|
4588 | * The transition is acceptable save it
|
---|
4589 | */
|
---|
4590 | exec->transno = -1; /* trick */
|
---|
4591 | exec->state = to;
|
---|
4592 | xmlFARegExecSave(exec);
|
---|
4593 | exec->transno = transno;
|
---|
4594 | exec->state = state;
|
---|
4595 | }
|
---|
4596 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]),
|
---|
4597 | len);
|
---|
4598 | ret = xmlRegCheckCharacter(atom, codepoint);
|
---|
4599 | exec->transcount++;
|
---|
4600 | } while (ret == 1);
|
---|
4601 | if (exec->transcount < atom->min)
|
---|
4602 | ret = 0;
|
---|
4603 |
|
---|
4604 | /*
|
---|
4605 | * If the last check failed but one transition was found
|
---|
4606 | * possible, rollback
|
---|
4607 | */
|
---|
4608 | if (ret < 0)
|
---|
4609 | ret = 0;
|
---|
4610 | if (ret == 0) {
|
---|
4611 | goto rollback;
|
---|
4612 | }
|
---|
4613 | }
|
---|
4614 | }
|
---|
4615 | if (ret == 1) {
|
---|
4616 | if (exec->state->nbTrans > exec->transno + 1) {
|
---|
4617 | xmlFARegExecSave(exec);
|
---|
4618 | }
|
---|
4619 | /*
|
---|
4620 | * restart count for expressions like this ((abc){2})*
|
---|
4621 | */
|
---|
4622 | if (trans->count >= 0) {
|
---|
4623 | #ifdef DEBUG_REGEXP_EXEC
|
---|
4624 | printf("Reset count %d\n", trans->count);
|
---|
4625 | #endif
|
---|
4626 | exec->counts[trans->count] = 0;
|
---|
4627 | }
|
---|
4628 | if (trans->counter >= 0) {
|
---|
4629 | #ifdef DEBUG_REGEXP_EXEC
|
---|
4630 | printf("Increasing count %d\n", trans->counter);
|
---|
4631 | #endif
|
---|
4632 | exec->counts[trans->counter]++;
|
---|
4633 | }
|
---|
4634 | #ifdef DEBUG_REGEXP_EXEC
|
---|
4635 | printf("entering state %d\n", trans->to);
|
---|
4636 | #endif
|
---|
4637 | exec->state = exec->comp->states[trans->to];
|
---|
4638 | exec->transno = 0;
|
---|
4639 | if (trans->atom != NULL) {
|
---|
4640 | exec->index += len;
|
---|
4641 | }
|
---|
4642 | goto progress;
|
---|
4643 | } else if (ret < 0) {
|
---|
4644 | exec->status = -4;
|
---|
4645 | break;
|
---|
4646 | }
|
---|
4647 | }
|
---|
4648 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) {
|
---|
4649 | rollback:
|
---|
4650 | /*
|
---|
4651 | * Failed to find a way out
|
---|
4652 | */
|
---|
4653 | exec->determinist = 0;
|
---|
4654 | xmlFARegExecRollBack(exec);
|
---|
4655 | }
|
---|
4656 | progress:
|
---|
4657 | continue;
|
---|
4658 | }
|
---|
4659 | }
|
---|
4660 | #endif
|
---|
4661 | /************************************************************************
|
---|
4662 | * *
|
---|
4663 | * Parser for the Schemas Datatype Regular Expressions *
|
---|
4664 | * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs *
|
---|
4665 | * *
|
---|
4666 | ************************************************************************/
|
---|
4667 |
|
---|
4668 | /**
|
---|
4669 | * xmlFAIsChar:
|
---|
4670 | * @ctxt: a regexp parser context
|
---|
4671 | *
|
---|
4672 | * [10] Char ::= [^.\?*+()|#x5B#x5D]
|
---|
4673 | */
|
---|
4674 | static int
|
---|
4675 | xmlFAIsChar(xmlRegParserCtxtPtr ctxt) {
|
---|
4676 | int cur;
|
---|
4677 | int len;
|
---|
4678 |
|
---|
4679 | cur = CUR_SCHAR(ctxt->cur, len);
|
---|
4680 | if ((cur == '.') || (cur == '\\') || (cur == '?') ||
|
---|
4681 | (cur == '*') || (cur == '+') || (cur == '(') ||
|
---|
4682 | (cur == ')') || (cur == '|') || (cur == 0x5B) ||
|
---|
4683 | (cur == 0x5D) || (cur == 0))
|
---|
4684 | return(-1);
|
---|
4685 | return(cur);
|
---|
4686 | }
|
---|
4687 |
|
---|
4688 | /**
|
---|
4689 | * xmlFAParseCharProp:
|
---|
4690 | * @ctxt: a regexp parser context
|
---|
4691 | *
|
---|
4692 | * [27] charProp ::= IsCategory | IsBlock
|
---|
4693 | * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation |
|
---|
4694 | * Separators | Symbols | Others
|
---|
4695 | * [29] Letters ::= 'L' [ultmo]?
|
---|
4696 | * [30] Marks ::= 'M' [nce]?
|
---|
4697 | * [31] Numbers ::= 'N' [dlo]?
|
---|
4698 | * [32] Punctuation ::= 'P' [cdseifo]?
|
---|
4699 | * [33] Separators ::= 'Z' [slp]?
|
---|
4700 | * [34] Symbols ::= 'S' [mcko]?
|
---|
4701 | * [35] Others ::= 'C' [cfon]?
|
---|
4702 | * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+
|
---|
4703 | */
|
---|
4704 | static void
|
---|
4705 | xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) {
|
---|
4706 | int cur;
|
---|
4707 | xmlRegAtomType type = (xmlRegAtomType) 0;
|
---|
4708 | xmlChar *blockName = NULL;
|
---|
4709 |
|
---|
4710 | cur = CUR;
|
---|
4711 | if (cur == 'L') {
|
---|
4712 | NEXT;
|
---|
4713 | cur = CUR;
|
---|
4714 | if (cur == 'u') {
|
---|
4715 | NEXT;
|
---|
4716 | type = XML_REGEXP_LETTER_UPPERCASE;
|
---|
4717 | } else if (cur == 'l') {
|
---|
4718 | NEXT;
|
---|
4719 | type = XML_REGEXP_LETTER_LOWERCASE;
|
---|
4720 | } else if (cur == 't') {
|
---|
4721 | NEXT;
|
---|
4722 | type = XML_REGEXP_LETTER_TITLECASE;
|
---|
4723 | } else if (cur == 'm') {
|
---|
4724 | NEXT;
|
---|
4725 | type = XML_REGEXP_LETTER_MODIFIER;
|
---|
4726 | } else if (cur == 'o') {
|
---|
4727 | NEXT;
|
---|
4728 | type = XML_REGEXP_LETTER_OTHERS;
|
---|
4729 | } else {
|
---|
4730 | type = XML_REGEXP_LETTER;
|
---|
4731 | }
|
---|
4732 | } else if (cur == 'M') {
|
---|
4733 | NEXT;
|
---|
4734 | cur = CUR;
|
---|
4735 | if (cur == 'n') {
|
---|
4736 | NEXT;
|
---|
4737 | /* nonspacing */
|
---|
4738 | type = XML_REGEXP_MARK_NONSPACING;
|
---|
4739 | } else if (cur == 'c') {
|
---|
4740 | NEXT;
|
---|
4741 | /* spacing combining */
|
---|
4742 | type = XML_REGEXP_MARK_SPACECOMBINING;
|
---|
4743 | } else if (cur == 'e') {
|
---|
4744 | NEXT;
|
---|
4745 | /* enclosing */
|
---|
4746 | type = XML_REGEXP_MARK_ENCLOSING;
|
---|
4747 | } else {
|
---|
4748 | /* all marks */
|
---|
4749 | type = XML_REGEXP_MARK;
|
---|
4750 | }
|
---|
4751 | } else if (cur == 'N') {
|
---|
4752 | NEXT;
|
---|
4753 | cur = CUR;
|
---|
4754 | if (cur == 'd') {
|
---|
4755 | NEXT;
|
---|
4756 | /* digital */
|
---|
4757 | type = XML_REGEXP_NUMBER_DECIMAL;
|
---|
4758 | } else if (cur == 'l') {
|
---|
4759 | NEXT;
|
---|
4760 | /* letter */
|
---|
4761 | type = XML_REGEXP_NUMBER_LETTER;
|
---|
4762 | } else if (cur == 'o') {
|
---|
4763 | NEXT;
|
---|
4764 | /* other */
|
---|
4765 | type = XML_REGEXP_NUMBER_OTHERS;
|
---|
4766 | } else {
|
---|
4767 | /* all numbers */
|
---|
4768 | type = XML_REGEXP_NUMBER;
|
---|
4769 | }
|
---|
4770 | } else if (cur == 'P') {
|
---|
4771 | NEXT;
|
---|
4772 | cur = CUR;
|
---|
4773 | if (cur == 'c') {
|
---|
4774 | NEXT;
|
---|
4775 | /* connector */
|
---|
4776 | type = XML_REGEXP_PUNCT_CONNECTOR;
|
---|
4777 | } else if (cur == 'd') {
|
---|
4778 | NEXT;
|
---|
4779 | /* dash */
|
---|
4780 | type = XML_REGEXP_PUNCT_DASH;
|
---|
4781 | } else if (cur == 's') {
|
---|
4782 | NEXT;
|
---|
4783 | /* open */
|
---|
4784 | type = XML_REGEXP_PUNCT_OPEN;
|
---|
4785 | } else if (cur == 'e') {
|
---|
4786 | NEXT;
|
---|
4787 | /* close */
|
---|
4788 | type = XML_REGEXP_PUNCT_CLOSE;
|
---|
4789 | } else if (cur == 'i') {
|
---|
4790 | NEXT;
|
---|
4791 | /* initial quote */
|
---|
4792 | type = XML_REGEXP_PUNCT_INITQUOTE;
|
---|
4793 | } else if (cur == 'f') {
|
---|
4794 | NEXT;
|
---|
4795 | /* final quote */
|
---|
4796 | type = XML_REGEXP_PUNCT_FINQUOTE;
|
---|
4797 | } else if (cur == 'o') {
|
---|
4798 | NEXT;
|
---|
4799 | /* other */
|
---|
4800 | type = XML_REGEXP_PUNCT_OTHERS;
|
---|
4801 | } else {
|
---|
4802 | /* all punctuation */
|
---|
4803 | type = XML_REGEXP_PUNCT;
|
---|
4804 | }
|
---|
4805 | } else if (cur == 'Z') {
|
---|
4806 | NEXT;
|
---|
4807 | cur = CUR;
|
---|
4808 | if (cur == 's') {
|
---|
4809 | NEXT;
|
---|
4810 | /* space */
|
---|
4811 | type = XML_REGEXP_SEPAR_SPACE;
|
---|
4812 | } else if (cur == 'l') {
|
---|
4813 | NEXT;
|
---|
4814 | /* line */
|
---|
4815 | type = XML_REGEXP_SEPAR_LINE;
|
---|
4816 | } else if (cur == 'p') {
|
---|
4817 | NEXT;
|
---|
4818 | /* paragraph */
|
---|
4819 | type = XML_REGEXP_SEPAR_PARA;
|
---|
4820 | } else {
|
---|
4821 | /* all separators */
|
---|
4822 | type = XML_REGEXP_SEPAR;
|
---|
4823 | }
|
---|
4824 | } else if (cur == 'S') {
|
---|
4825 | NEXT;
|
---|
4826 | cur = CUR;
|
---|
4827 | if (cur == 'm') {
|
---|
4828 | NEXT;
|
---|
4829 | type = XML_REGEXP_SYMBOL_MATH;
|
---|
4830 | /* math */
|
---|
4831 | } else if (cur == 'c') {
|
---|
4832 | NEXT;
|
---|
4833 | type = XML_REGEXP_SYMBOL_CURRENCY;
|
---|
4834 | /* currency */
|
---|
4835 | } else if (cur == 'k') {
|
---|
4836 | NEXT;
|
---|
4837 | type = XML_REGEXP_SYMBOL_MODIFIER;
|
---|
4838 | /* modifiers */
|
---|
4839 | } else if (cur == 'o') {
|
---|
4840 | NEXT;
|
---|
4841 | type = XML_REGEXP_SYMBOL_OTHERS;
|
---|
4842 | /* other */
|
---|
4843 | } else {
|
---|
4844 | /* all symbols */
|
---|
4845 | type = XML_REGEXP_SYMBOL;
|
---|
4846 | }
|
---|
4847 | } else if (cur == 'C') {
|
---|
4848 | NEXT;
|
---|
4849 | cur = CUR;
|
---|
4850 | if (cur == 'c') {
|
---|
4851 | NEXT;
|
---|
4852 | /* control */
|
---|
4853 | type = XML_REGEXP_OTHER_CONTROL;
|
---|
4854 | } else if (cur == 'f') {
|
---|
4855 | NEXT;
|
---|
4856 | /* format */
|
---|
4857 | type = XML_REGEXP_OTHER_FORMAT;
|
---|
4858 | } else if (cur == 'o') {
|
---|
4859 | NEXT;
|
---|
4860 | /* private use */
|
---|
4861 | type = XML_REGEXP_OTHER_PRIVATE;
|
---|
4862 | } else if (cur == 'n') {
|
---|
4863 | NEXT;
|
---|
4864 | /* not assigned */
|
---|
4865 | type = XML_REGEXP_OTHER_NA;
|
---|
4866 | } else {
|
---|
4867 | /* all others */
|
---|
4868 | type = XML_REGEXP_OTHER;
|
---|
4869 | }
|
---|
4870 | } else if (cur == 'I') {
|
---|
4871 | const xmlChar *start;
|
---|
4872 | NEXT;
|
---|
4873 | cur = CUR;
|
---|
4874 | if (cur != 's') {
|
---|
4875 | ERROR("IsXXXX expected");
|
---|
4876 | return;
|
---|
4877 | }
|
---|
4878 | NEXT;
|
---|
4879 | start = ctxt->cur;
|
---|
4880 | cur = CUR;
|
---|
4881 | if (((cur >= 'a') && (cur <= 'z')) ||
|
---|
4882 | ((cur >= 'A') && (cur <= 'Z')) ||
|
---|
4883 | ((cur >= '0') && (cur <= '9')) ||
|
---|
4884 | (cur == 0x2D)) {
|
---|
4885 | NEXT;
|
---|
4886 | cur = CUR;
|
---|
4887 | while (((cur >= 'a') && (cur <= 'z')) ||
|
---|
4888 | ((cur >= 'A') && (cur <= 'Z')) ||
|
---|
4889 | ((cur >= '0') && (cur <= '9')) ||
|
---|
4890 | (cur == 0x2D)) {
|
---|
4891 | NEXT;
|
---|
4892 | cur = CUR;
|
---|
4893 | }
|
---|
4894 | }
|
---|
4895 | type = XML_REGEXP_BLOCK_NAME;
|
---|
4896 | blockName = xmlStrndup(start, ctxt->cur - start);
|
---|
4897 | } else {
|
---|
4898 | ERROR("Unknown char property");
|
---|
4899 | return;
|
---|
4900 | }
|
---|
4901 | if (ctxt->atom == NULL) {
|
---|
4902 | ctxt->atom = xmlRegNewAtom(ctxt, type);
|
---|
4903 | if (ctxt->atom != NULL)
|
---|
4904 | ctxt->atom->valuep = blockName;
|
---|
4905 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
|
---|
4906 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
|
---|
4907 | type, 0, 0, blockName);
|
---|
4908 | }
|
---|
4909 | }
|
---|
4910 |
|
---|
4911 | /**
|
---|
4912 | * xmlFAParseCharClassEsc:
|
---|
4913 | * @ctxt: a regexp parser context
|
---|
4914 | *
|
---|
4915 | * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc )
|
---|
4916 | * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E]
|
---|
4917 | * [25] catEsc ::= '\p{' charProp '}'
|
---|
4918 | * [26] complEsc ::= '\P{' charProp '}'
|
---|
4919 | * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW])
|
---|
4920 | */
|
---|
4921 | static void
|
---|
4922 | xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) {
|
---|
4923 | int cur;
|
---|
4924 |
|
---|
4925 | if (CUR == '.') {
|
---|
4926 | if (ctxt->atom == NULL) {
|
---|
4927 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR);
|
---|
4928 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
|
---|
4929 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
|
---|
4930 | XML_REGEXP_ANYCHAR, 0, 0, NULL);
|
---|
4931 | }
|
---|
4932 | NEXT;
|
---|
4933 | return;
|
---|
4934 | }
|
---|
4935 | if (CUR != '\\') {
|
---|
4936 | ERROR("Escaped sequence: expecting \\");
|
---|
4937 | return;
|
---|
4938 | }
|
---|
4939 | NEXT;
|
---|
4940 | cur = CUR;
|
---|
4941 | if (cur == 'p') {
|
---|
4942 | NEXT;
|
---|
4943 | if (CUR != '{') {
|
---|
4944 | ERROR("Expecting '{'");
|
---|
4945 | return;
|
---|
4946 | }
|
---|
4947 | NEXT;
|
---|
4948 | xmlFAParseCharProp(ctxt);
|
---|
4949 | if (CUR != '}') {
|
---|
4950 | ERROR("Expecting '}'");
|
---|
4951 | return;
|
---|
4952 | }
|
---|
4953 | NEXT;
|
---|
4954 | } else if (cur == 'P') {
|
---|
4955 | NEXT;
|
---|
4956 | if (CUR != '{') {
|
---|
4957 | ERROR("Expecting '{'");
|
---|
4958 | return;
|
---|
4959 | }
|
---|
4960 | NEXT;
|
---|
4961 | xmlFAParseCharProp(ctxt);
|
---|
4962 | if (ctxt->atom != NULL)
|
---|
4963 | ctxt->atom->neg = 1;
|
---|
4964 | if (CUR != '}') {
|
---|
4965 | ERROR("Expecting '}'");
|
---|
4966 | return;
|
---|
4967 | }
|
---|
4968 | NEXT;
|
---|
4969 | } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') ||
|
---|
4970 | (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') ||
|
---|
4971 | (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') ||
|
---|
4972 | (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) ||
|
---|
4973 | (cur == 0x5E)) {
|
---|
4974 | if (ctxt->atom == NULL) {
|
---|
4975 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
|
---|
4976 | if (ctxt->atom != NULL) {
|
---|
4977 | switch (cur) {
|
---|
4978 | case 'n':
|
---|
4979 | ctxt->atom->codepoint = '\n';
|
---|
4980 | break;
|
---|
4981 | case 'r':
|
---|
4982 | ctxt->atom->codepoint = '\r';
|
---|
4983 | break;
|
---|
4984 | case 't':
|
---|
4985 | ctxt->atom->codepoint = '\t';
|
---|
4986 | break;
|
---|
4987 | default:
|
---|
4988 | ctxt->atom->codepoint = cur;
|
---|
4989 | }
|
---|
4990 | }
|
---|
4991 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
|
---|
4992 | switch (cur) {
|
---|
4993 | case 'n':
|
---|
4994 | cur = '\n';
|
---|
4995 | break;
|
---|
4996 | case 'r':
|
---|
4997 | cur = '\r';
|
---|
4998 | break;
|
---|
4999 | case 't':
|
---|
5000 | cur = '\t';
|
---|
5001 | break;
|
---|
5002 | }
|
---|
5003 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
|
---|
5004 | XML_REGEXP_CHARVAL, cur, cur, NULL);
|
---|
5005 | }
|
---|
5006 | NEXT;
|
---|
5007 | } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') ||
|
---|
5008 | (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') ||
|
---|
5009 | (cur == 'w') || (cur == 'W')) {
|
---|
5010 | xmlRegAtomType type = XML_REGEXP_ANYSPACE;
|
---|
5011 |
|
---|
5012 | switch (cur) {
|
---|
5013 | case 's':
|
---|
5014 | type = XML_REGEXP_ANYSPACE;
|
---|
5015 | break;
|
---|
5016 | case 'S':
|
---|
5017 | type = XML_REGEXP_NOTSPACE;
|
---|
5018 | break;
|
---|
5019 | case 'i':
|
---|
5020 | type = XML_REGEXP_INITNAME;
|
---|
5021 | break;
|
---|
5022 | case 'I':
|
---|
5023 | type = XML_REGEXP_NOTINITNAME;
|
---|
5024 | break;
|
---|
5025 | case 'c':
|
---|
5026 | type = XML_REGEXP_NAMECHAR;
|
---|
5027 | break;
|
---|
5028 | case 'C':
|
---|
5029 | type = XML_REGEXP_NOTNAMECHAR;
|
---|
5030 | break;
|
---|
5031 | case 'd':
|
---|
5032 | type = XML_REGEXP_DECIMAL;
|
---|
5033 | break;
|
---|
5034 | case 'D':
|
---|
5035 | type = XML_REGEXP_NOTDECIMAL;
|
---|
5036 | break;
|
---|
5037 | case 'w':
|
---|
5038 | type = XML_REGEXP_REALCHAR;
|
---|
5039 | break;
|
---|
5040 | case 'W':
|
---|
5041 | type = XML_REGEXP_NOTREALCHAR;
|
---|
5042 | break;
|
---|
5043 | }
|
---|
5044 | NEXT;
|
---|
5045 | if (ctxt->atom == NULL) {
|
---|
5046 | ctxt->atom = xmlRegNewAtom(ctxt, type);
|
---|
5047 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) {
|
---|
5048 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
|
---|
5049 | type, 0, 0, NULL);
|
---|
5050 | }
|
---|
5051 | } else {
|
---|
5052 | ERROR("Wrong escape sequence, misuse of character '\\'");
|
---|
5053 | }
|
---|
5054 | }
|
---|
5055 |
|
---|
5056 | /**
|
---|
5057 | * xmlFAParseCharRange:
|
---|
5058 | * @ctxt: a regexp parser context
|
---|
5059 | *
|
---|
5060 | * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash
|
---|
5061 | * [18] seRange ::= charOrEsc '-' charOrEsc
|
---|
5062 | * [20] charOrEsc ::= XmlChar | SingleCharEsc
|
---|
5063 | * [21] XmlChar ::= [^\#x2D#x5B#x5D]
|
---|
5064 | * [22] XmlCharIncDash ::= [^\#x5B#x5D]
|
---|
5065 | */
|
---|
5066 | static void
|
---|
5067 | xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) {
|
---|
5068 | int cur, len;
|
---|
5069 | int start = -1;
|
---|
5070 | int end = -1;
|
---|
5071 |
|
---|
5072 | if (CUR == '\0') {
|
---|
5073 | ERROR("Expecting ']'");
|
---|
5074 | return;
|
---|
5075 | }
|
---|
5076 |
|
---|
5077 | cur = CUR;
|
---|
5078 | if (cur == '\\') {
|
---|
5079 | NEXT;
|
---|
5080 | cur = CUR;
|
---|
5081 | switch (cur) {
|
---|
5082 | case 'n': start = 0xA; break;
|
---|
5083 | case 'r': start = 0xD; break;
|
---|
5084 | case 't': start = 0x9; break;
|
---|
5085 | case '\\': case '|': case '.': case '-': case '^': case '?':
|
---|
5086 | case '*': case '+': case '{': case '}': case '(': case ')':
|
---|
5087 | case '[': case ']':
|
---|
5088 | start = cur; break;
|
---|
5089 | default:
|
---|
5090 | ERROR("Invalid escape value");
|
---|
5091 | return;
|
---|
5092 | }
|
---|
5093 | end = start;
|
---|
5094 | len = 1;
|
---|
5095 | } else if ((cur != 0x5B) && (cur != 0x5D)) {
|
---|
5096 | end = start = CUR_SCHAR(ctxt->cur, len);
|
---|
5097 | } else {
|
---|
5098 | ERROR("Expecting a char range");
|
---|
5099 | return;
|
---|
5100 | }
|
---|
5101 | /*
|
---|
5102 | * Since we are "inside" a range, we can assume ctxt->cur is past
|
---|
5103 | * the start of ctxt->string, and PREV should be safe
|
---|
5104 | */
|
---|
5105 | if ((start == '-') && (NXT(1) != ']') && (PREV != '[') && (PREV != '^')) {
|
---|
5106 | NEXTL(len);
|
---|
5107 | return;
|
---|
5108 | }
|
---|
5109 | NEXTL(len);
|
---|
5110 | cur = CUR;
|
---|
5111 | if ((cur != '-') || (NXT(1) == '[') || (NXT(1) == ']')) {
|
---|
5112 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
|
---|
5113 | XML_REGEXP_CHARVAL, start, end, NULL);
|
---|
5114 | return;
|
---|
5115 | }
|
---|
5116 | NEXT;
|
---|
5117 | cur = CUR;
|
---|
5118 | if (cur == '\\') {
|
---|
5119 | NEXT;
|
---|
5120 | cur = CUR;
|
---|
5121 | switch (cur) {
|
---|
5122 | case 'n': end = 0xA; break;
|
---|
5123 | case 'r': end = 0xD; break;
|
---|
5124 | case 't': end = 0x9; break;
|
---|
5125 | case '\\': case '|': case '.': case '-': case '^': case '?':
|
---|
5126 | case '*': case '+': case '{': case '}': case '(': case ')':
|
---|
5127 | case '[': case ']':
|
---|
5128 | end = cur; break;
|
---|
5129 | default:
|
---|
5130 | ERROR("Invalid escape value");
|
---|
5131 | return;
|
---|
5132 | }
|
---|
5133 | len = 1;
|
---|
5134 | } else if ((cur != '\0') && (cur != 0x5B) && (cur != 0x5D)) {
|
---|
5135 | end = CUR_SCHAR(ctxt->cur, len);
|
---|
5136 | } else {
|
---|
5137 | ERROR("Expecting the end of a char range");
|
---|
5138 | return;
|
---|
5139 | }
|
---|
5140 |
|
---|
5141 | /* TODO check that the values are acceptable character ranges for XML */
|
---|
5142 | if (end < start) {
|
---|
5143 | ERROR("End of range is before start of range");
|
---|
5144 | } else {
|
---|
5145 | NEXTL(len);
|
---|
5146 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg,
|
---|
5147 | XML_REGEXP_CHARVAL, start, end, NULL);
|
---|
5148 | }
|
---|
5149 | return;
|
---|
5150 | }
|
---|
5151 |
|
---|
5152 | /**
|
---|
5153 | * xmlFAParsePosCharGroup:
|
---|
5154 | * @ctxt: a regexp parser context
|
---|
5155 | *
|
---|
5156 | * [14] posCharGroup ::= ( charRange | charClassEsc )+
|
---|
5157 | */
|
---|
5158 | static void
|
---|
5159 | xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) {
|
---|
5160 | do {
|
---|
5161 | if (CUR == '\\') {
|
---|
5162 | xmlFAParseCharClassEsc(ctxt);
|
---|
5163 | } else {
|
---|
5164 | xmlFAParseCharRange(ctxt);
|
---|
5165 | }
|
---|
5166 | } while ((CUR != ']') && (CUR != '-') &&
|
---|
5167 | (CUR != 0) && (ctxt->error == 0));
|
---|
5168 | }
|
---|
5169 |
|
---|
5170 | /**
|
---|
5171 | * xmlFAParseCharGroup:
|
---|
5172 | * @ctxt: a regexp parser context
|
---|
5173 | *
|
---|
5174 | * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub
|
---|
5175 | * [15] negCharGroup ::= '^' posCharGroup
|
---|
5176 | * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr
|
---|
5177 | * [12] charClassExpr ::= '[' charGroup ']'
|
---|
5178 | */
|
---|
5179 | static void
|
---|
5180 | xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) {
|
---|
5181 | int neg = ctxt->neg;
|
---|
5182 |
|
---|
5183 | if (CUR == '^') {
|
---|
5184 | NEXT;
|
---|
5185 | ctxt->neg = !ctxt->neg;
|
---|
5186 | xmlFAParsePosCharGroup(ctxt);
|
---|
5187 | ctxt->neg = neg;
|
---|
5188 | }
|
---|
5189 | while ((CUR != ']') && (ctxt->error == 0)) {
|
---|
5190 | if ((CUR == '-') && (NXT(1) == '[')) {
|
---|
5191 | NEXT; /* eat the '-' */
|
---|
5192 | NEXT; /* eat the '[' */
|
---|
5193 | ctxt->neg = 2;
|
---|
5194 | xmlFAParseCharGroup(ctxt);
|
---|
5195 | ctxt->neg = neg;
|
---|
5196 | if (CUR == ']') {
|
---|
5197 | NEXT;
|
---|
5198 | } else {
|
---|
5199 | ERROR("charClassExpr: ']' expected");
|
---|
5200 | }
|
---|
5201 | break;
|
---|
5202 | } else {
|
---|
5203 | xmlFAParsePosCharGroup(ctxt);
|
---|
5204 | }
|
---|
5205 | }
|
---|
5206 | }
|
---|
5207 |
|
---|
5208 | /**
|
---|
5209 | * xmlFAParseCharClass:
|
---|
5210 | * @ctxt: a regexp parser context
|
---|
5211 | *
|
---|
5212 | * [11] charClass ::= charClassEsc | charClassExpr
|
---|
5213 | * [12] charClassExpr ::= '[' charGroup ']'
|
---|
5214 | */
|
---|
5215 | static void
|
---|
5216 | xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) {
|
---|
5217 | if (CUR == '[') {
|
---|
5218 | NEXT;
|
---|
5219 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES);
|
---|
5220 | if (ctxt->atom == NULL)
|
---|
5221 | return;
|
---|
5222 | xmlFAParseCharGroup(ctxt);
|
---|
5223 | if (CUR == ']') {
|
---|
5224 | NEXT;
|
---|
5225 | } else {
|
---|
5226 | ERROR("xmlFAParseCharClass: ']' expected");
|
---|
5227 | }
|
---|
5228 | } else {
|
---|
5229 | xmlFAParseCharClassEsc(ctxt);
|
---|
5230 | }
|
---|
5231 | }
|
---|
5232 |
|
---|
5233 | /**
|
---|
5234 | * xmlFAParseQuantExact:
|
---|
5235 | * @ctxt: a regexp parser context
|
---|
5236 | *
|
---|
5237 | * [8] QuantExact ::= [0-9]+
|
---|
5238 | *
|
---|
5239 | * Returns 0 if success or -1 in case of error
|
---|
5240 | */
|
---|
5241 | static int
|
---|
5242 | xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) {
|
---|
5243 | int ret = 0;
|
---|
5244 | int ok = 0;
|
---|
5245 | int overflow = 0;
|
---|
5246 |
|
---|
5247 | while ((CUR >= '0') && (CUR <= '9')) {
|
---|
5248 | if (ret > INT_MAX / 10) {
|
---|
5249 | overflow = 1;
|
---|
5250 | } else {
|
---|
5251 | int digit = CUR - '0';
|
---|
5252 |
|
---|
5253 | ret *= 10;
|
---|
5254 | if (ret > INT_MAX - digit)
|
---|
5255 | overflow = 1;
|
---|
5256 | else
|
---|
5257 | ret += digit;
|
---|
5258 | }
|
---|
5259 | ok = 1;
|
---|
5260 | NEXT;
|
---|
5261 | }
|
---|
5262 | if ((ok != 1) || (overflow == 1)) {
|
---|
5263 | return(-1);
|
---|
5264 | }
|
---|
5265 | return(ret);
|
---|
5266 | }
|
---|
5267 |
|
---|
5268 | /**
|
---|
5269 | * xmlFAParseQuantifier:
|
---|
5270 | * @ctxt: a regexp parser context
|
---|
5271 | *
|
---|
5272 | * [4] quantifier ::= [?*+] | ( '{' quantity '}' )
|
---|
5273 | * [5] quantity ::= quantRange | quantMin | QuantExact
|
---|
5274 | * [6] quantRange ::= QuantExact ',' QuantExact
|
---|
5275 | * [7] quantMin ::= QuantExact ','
|
---|
5276 | * [8] QuantExact ::= [0-9]+
|
---|
5277 | */
|
---|
5278 | static int
|
---|
5279 | xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) {
|
---|
5280 | int cur;
|
---|
5281 |
|
---|
5282 | cur = CUR;
|
---|
5283 | if ((cur == '?') || (cur == '*') || (cur == '+')) {
|
---|
5284 | if (ctxt->atom != NULL) {
|
---|
5285 | if (cur == '?')
|
---|
5286 | ctxt->atom->quant = XML_REGEXP_QUANT_OPT;
|
---|
5287 | else if (cur == '*')
|
---|
5288 | ctxt->atom->quant = XML_REGEXP_QUANT_MULT;
|
---|
5289 | else if (cur == '+')
|
---|
5290 | ctxt->atom->quant = XML_REGEXP_QUANT_PLUS;
|
---|
5291 | }
|
---|
5292 | NEXT;
|
---|
5293 | return(1);
|
---|
5294 | }
|
---|
5295 | if (cur == '{') {
|
---|
5296 | int min = 0, max = 0;
|
---|
5297 |
|
---|
5298 | NEXT;
|
---|
5299 | cur = xmlFAParseQuantExact(ctxt);
|
---|
5300 | if (cur >= 0)
|
---|
5301 | min = cur;
|
---|
5302 | else {
|
---|
5303 | ERROR("Improper quantifier");
|
---|
5304 | }
|
---|
5305 | if (CUR == ',') {
|
---|
5306 | NEXT;
|
---|
5307 | if (CUR == '}')
|
---|
5308 | max = INT_MAX;
|
---|
5309 | else {
|
---|
5310 | cur = xmlFAParseQuantExact(ctxt);
|
---|
5311 | if (cur >= 0)
|
---|
5312 | max = cur;
|
---|
5313 | else {
|
---|
5314 | ERROR("Improper quantifier");
|
---|
5315 | }
|
---|
5316 | }
|
---|
5317 | }
|
---|
5318 | if (CUR == '}') {
|
---|
5319 | NEXT;
|
---|
5320 | } else {
|
---|
5321 | ERROR("Unterminated quantifier");
|
---|
5322 | }
|
---|
5323 | if (max == 0)
|
---|
5324 | max = min;
|
---|
5325 | if (ctxt->atom != NULL) {
|
---|
5326 | ctxt->atom->quant = XML_REGEXP_QUANT_RANGE;
|
---|
5327 | ctxt->atom->min = min;
|
---|
5328 | ctxt->atom->max = max;
|
---|
5329 | }
|
---|
5330 | return(1);
|
---|
5331 | }
|
---|
5332 | return(0);
|
---|
5333 | }
|
---|
5334 |
|
---|
5335 | /**
|
---|
5336 | * xmlFAParseAtom:
|
---|
5337 | * @ctxt: a regexp parser context
|
---|
5338 | *
|
---|
5339 | * [9] atom ::= Char | charClass | ( '(' regExp ')' )
|
---|
5340 | */
|
---|
5341 | static int
|
---|
5342 | xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) {
|
---|
5343 | int codepoint, len;
|
---|
5344 |
|
---|
5345 | codepoint = xmlFAIsChar(ctxt);
|
---|
5346 | if (codepoint > 0) {
|
---|
5347 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL);
|
---|
5348 | if (ctxt->atom == NULL)
|
---|
5349 | return(-1);
|
---|
5350 | codepoint = CUR_SCHAR(ctxt->cur, len);
|
---|
5351 | ctxt->atom->codepoint = codepoint;
|
---|
5352 | NEXTL(len);
|
---|
5353 | return(1);
|
---|
5354 | } else if (CUR == '|') {
|
---|
5355 | return(0);
|
---|
5356 | } else if (CUR == 0) {
|
---|
5357 | return(0);
|
---|
5358 | } else if (CUR == ')') {
|
---|
5359 | return(0);
|
---|
5360 | } else if (CUR == '(') {
|
---|
5361 | xmlRegStatePtr start, oldend, start0;
|
---|
5362 |
|
---|
5363 | NEXT;
|
---|
5364 | if (ctxt->depth >= 50) {
|
---|
5365 | ERROR("xmlFAParseAtom: maximum nesting depth exceeded");
|
---|
5366 | return(-1);
|
---|
5367 | }
|
---|
5368 | /*
|
---|
5369 | * this extra Epsilon transition is needed if we count with 0 allowed
|
---|
5370 | * unfortunately this can't be known at that point
|
---|
5371 | */
|
---|
5372 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
|
---|
5373 | start0 = ctxt->state;
|
---|
5374 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL);
|
---|
5375 | start = ctxt->state;
|
---|
5376 | oldend = ctxt->end;
|
---|
5377 | ctxt->end = NULL;
|
---|
5378 | ctxt->atom = NULL;
|
---|
5379 | ctxt->depth++;
|
---|
5380 | xmlFAParseRegExp(ctxt, 0);
|
---|
5381 | ctxt->depth--;
|
---|
5382 | if (CUR == ')') {
|
---|
5383 | NEXT;
|
---|
5384 | } else {
|
---|
5385 | ERROR("xmlFAParseAtom: expecting ')'");
|
---|
5386 | }
|
---|
5387 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG);
|
---|
5388 | if (ctxt->atom == NULL)
|
---|
5389 | return(-1);
|
---|
5390 | ctxt->atom->start = start;
|
---|
5391 | ctxt->atom->start0 = start0;
|
---|
5392 | ctxt->atom->stop = ctxt->state;
|
---|
5393 | ctxt->end = oldend;
|
---|
5394 | return(1);
|
---|
5395 | } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) {
|
---|
5396 | xmlFAParseCharClass(ctxt);
|
---|
5397 | return(1);
|
---|
5398 | }
|
---|
5399 | return(0);
|
---|
5400 | }
|
---|
5401 |
|
---|
5402 | /**
|
---|
5403 | * xmlFAParsePiece:
|
---|
5404 | * @ctxt: a regexp parser context
|
---|
5405 | *
|
---|
5406 | * [3] piece ::= atom quantifier?
|
---|
5407 | */
|
---|
5408 | static int
|
---|
5409 | xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) {
|
---|
5410 | int ret;
|
---|
5411 |
|
---|
5412 | ctxt->atom = NULL;
|
---|
5413 | ret = xmlFAParseAtom(ctxt);
|
---|
5414 | if (ret == 0)
|
---|
5415 | return(0);
|
---|
5416 | if (ctxt->atom == NULL) {
|
---|
5417 | ERROR("internal: no atom generated");
|
---|
5418 | }
|
---|
5419 | xmlFAParseQuantifier(ctxt);
|
---|
5420 | return(1);
|
---|
5421 | }
|
---|
5422 |
|
---|
5423 | /**
|
---|
5424 | * xmlFAParseBranch:
|
---|
5425 | * @ctxt: a regexp parser context
|
---|
5426 | * @to: optional target to the end of the branch
|
---|
5427 | *
|
---|
5428 | * @to is used to optimize by removing duplicate path in automata
|
---|
5429 | * in expressions like (a|b)(c|d)
|
---|
5430 | *
|
---|
5431 | * [2] branch ::= piece*
|
---|
5432 | */
|
---|
5433 | static int
|
---|
5434 | xmlFAParseBranch(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr to) {
|
---|
5435 | xmlRegStatePtr previous;
|
---|
5436 | int ret;
|
---|
5437 |
|
---|
5438 | previous = ctxt->state;
|
---|
5439 | ret = xmlFAParsePiece(ctxt);
|
---|
5440 | if (ret == 0) {
|
---|
5441 | /* Empty branch */
|
---|
5442 | xmlFAGenerateEpsilonTransition(ctxt, previous, to);
|
---|
5443 | } else {
|
---|
5444 | if (xmlFAGenerateTransitions(ctxt, previous,
|
---|
5445 | (CUR=='|' || CUR==')' || CUR==0) ? to : NULL, ctxt->atom) < 0)
|
---|
5446 | return(-1);
|
---|
5447 | previous = ctxt->state;
|
---|
5448 | ctxt->atom = NULL;
|
---|
5449 | }
|
---|
5450 | while ((ret != 0) && (ctxt->error == 0)) {
|
---|
5451 | ret = xmlFAParsePiece(ctxt);
|
---|
5452 | if (ret != 0) {
|
---|
5453 | if (xmlFAGenerateTransitions(ctxt, previous,
|
---|
5454 | (CUR=='|' || CUR==')' || CUR==0) ? to : NULL,
|
---|
5455 | ctxt->atom) < 0)
|
---|
5456 | return(-1);
|
---|
5457 | previous = ctxt->state;
|
---|
5458 | ctxt->atom = NULL;
|
---|
5459 | }
|
---|
5460 | }
|
---|
5461 | return(0);
|
---|
5462 | }
|
---|
5463 |
|
---|
5464 | /**
|
---|
5465 | * xmlFAParseRegExp:
|
---|
5466 | * @ctxt: a regexp parser context
|
---|
5467 | * @top: is this the top-level expression ?
|
---|
5468 | *
|
---|
5469 | * [1] regExp ::= branch ( '|' branch )*
|
---|
5470 | */
|
---|
5471 | static void
|
---|
5472 | xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) {
|
---|
5473 | xmlRegStatePtr start, end;
|
---|
5474 |
|
---|
5475 | /* if not top start should have been generated by an epsilon trans */
|
---|
5476 | start = ctxt->state;
|
---|
5477 | ctxt->end = NULL;
|
---|
5478 | xmlFAParseBranch(ctxt, NULL);
|
---|
5479 | if (top) {
|
---|
5480 | #ifdef DEBUG_REGEXP_GRAPH
|
---|
5481 | printf("State %d is final\n", ctxt->state->no);
|
---|
5482 | #endif
|
---|
5483 | ctxt->state->type = XML_REGEXP_FINAL_STATE;
|
---|
5484 | }
|
---|
5485 | if (CUR != '|') {
|
---|
5486 | ctxt->end = ctxt->state;
|
---|
5487 | return;
|
---|
5488 | }
|
---|
5489 | end = ctxt->state;
|
---|
5490 | while ((CUR == '|') && (ctxt->error == 0)) {
|
---|
5491 | NEXT;
|
---|
5492 | ctxt->state = start;
|
---|
5493 | ctxt->end = NULL;
|
---|
5494 | xmlFAParseBranch(ctxt, end);
|
---|
5495 | }
|
---|
5496 | if (!top) {
|
---|
5497 | ctxt->state = end;
|
---|
5498 | ctxt->end = end;
|
---|
5499 | }
|
---|
5500 | }
|
---|
5501 |
|
---|
5502 | /************************************************************************
|
---|
5503 | * *
|
---|
5504 | * The basic API *
|
---|
5505 | * *
|
---|
5506 | ************************************************************************/
|
---|
5507 |
|
---|
5508 | /**
|
---|
5509 | * xmlRegexpPrint:
|
---|
5510 | * @output: the file for the output debug
|
---|
5511 | * @regexp: the compiled regexp
|
---|
5512 | *
|
---|
5513 | * Print the content of the compiled regular expression
|
---|
5514 | */
|
---|
5515 | void
|
---|
5516 | xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) {
|
---|
5517 | int i;
|
---|
5518 |
|
---|
5519 | if (output == NULL)
|
---|
5520 | return;
|
---|
5521 | fprintf(output, " regexp: ");
|
---|
5522 | if (regexp == NULL) {
|
---|
5523 | fprintf(output, "NULL\n");
|
---|
5524 | return;
|
---|
5525 | }
|
---|
5526 | fprintf(output, "'%s' ", regexp->string);
|
---|
5527 | fprintf(output, "\n");
|
---|
5528 | fprintf(output, "%d atoms:\n", regexp->nbAtoms);
|
---|
5529 | for (i = 0;i < regexp->nbAtoms; i++) {
|
---|
5530 | fprintf(output, " %02d ", i);
|
---|
5531 | xmlRegPrintAtom(output, regexp->atoms[i]);
|
---|
5532 | }
|
---|
5533 | fprintf(output, "%d states:", regexp->nbStates);
|
---|
5534 | fprintf(output, "\n");
|
---|
5535 | for (i = 0;i < regexp->nbStates; i++) {
|
---|
5536 | xmlRegPrintState(output, regexp->states[i]);
|
---|
5537 | }
|
---|
5538 | fprintf(output, "%d counters:\n", regexp->nbCounters);
|
---|
5539 | for (i = 0;i < regexp->nbCounters; i++) {
|
---|
5540 | fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min,
|
---|
5541 | regexp->counters[i].max);
|
---|
5542 | }
|
---|
5543 | }
|
---|
5544 |
|
---|
5545 | /**
|
---|
5546 | * xmlRegexpCompile:
|
---|
5547 | * @regexp: a regular expression string
|
---|
5548 | *
|
---|
5549 | * Parses a regular expression conforming to XML Schemas Part 2 Datatype
|
---|
5550 | * Appendix F and builds an automata suitable for testing strings against
|
---|
5551 | * that regular expression
|
---|
5552 | *
|
---|
5553 | * Returns the compiled expression or NULL in case of error
|
---|
5554 | */
|
---|
5555 | xmlRegexpPtr
|
---|
5556 | xmlRegexpCompile(const xmlChar *regexp) {
|
---|
5557 | xmlRegexpPtr ret;
|
---|
5558 | xmlRegParserCtxtPtr ctxt;
|
---|
5559 |
|
---|
5560 | ctxt = xmlRegNewParserCtxt(regexp);
|
---|
5561 | if (ctxt == NULL)
|
---|
5562 | return(NULL);
|
---|
5563 |
|
---|
5564 | /* initialize the parser */
|
---|
5565 | ctxt->end = NULL;
|
---|
5566 | ctxt->start = ctxt->state = xmlRegNewState(ctxt);
|
---|
5567 | xmlRegStatePush(ctxt, ctxt->start);
|
---|
5568 |
|
---|
5569 | /* parse the expression building an automata */
|
---|
5570 | xmlFAParseRegExp(ctxt, 1);
|
---|
5571 | if (CUR != 0) {
|
---|
5572 | ERROR("xmlFAParseRegExp: extra characters");
|
---|
5573 | }
|
---|
5574 | if (ctxt->error != 0) {
|
---|
5575 | xmlRegFreeParserCtxt(ctxt);
|
---|
5576 | return(NULL);
|
---|
5577 | }
|
---|
5578 | ctxt->end = ctxt->state;
|
---|
5579 | ctxt->start->type = XML_REGEXP_START_STATE;
|
---|
5580 | ctxt->end->type = XML_REGEXP_FINAL_STATE;
|
---|
5581 |
|
---|
5582 | /* remove the Epsilon except for counted transitions */
|
---|
5583 | xmlFAEliminateEpsilonTransitions(ctxt);
|
---|
5584 |
|
---|
5585 |
|
---|
5586 | if (ctxt->error != 0) {
|
---|
5587 | xmlRegFreeParserCtxt(ctxt);
|
---|
5588 | return(NULL);
|
---|
5589 | }
|
---|
5590 | ret = xmlRegEpxFromParse(ctxt);
|
---|
5591 | xmlRegFreeParserCtxt(ctxt);
|
---|
5592 | return(ret);
|
---|
5593 | }
|
---|
5594 |
|
---|
5595 | /**
|
---|
5596 | * xmlRegexpExec:
|
---|
5597 | * @comp: the compiled regular expression
|
---|
5598 | * @content: the value to check against the regular expression
|
---|
5599 | *
|
---|
5600 | * Check if the regular expression generates the value
|
---|
5601 | *
|
---|
5602 | * Returns 1 if it matches, 0 if not and a negative value in case of error
|
---|
5603 | */
|
---|
5604 | int
|
---|
5605 | xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) {
|
---|
5606 | if ((comp == NULL) || (content == NULL))
|
---|
5607 | return(-1);
|
---|
5608 | return(xmlFARegExec(comp, content));
|
---|
5609 | }
|
---|
5610 |
|
---|
5611 | /**
|
---|
5612 | * xmlRegexpIsDeterminist:
|
---|
5613 | * @comp: the compiled regular expression
|
---|
5614 | *
|
---|
5615 | * Check if the regular expression is determinist
|
---|
5616 | *
|
---|
5617 | * Returns 1 if it yes, 0 if not and a negative value in case of error
|
---|
5618 | */
|
---|
5619 | int
|
---|
5620 | xmlRegexpIsDeterminist(xmlRegexpPtr comp) {
|
---|
5621 | xmlAutomataPtr am;
|
---|
5622 | int ret;
|
---|
5623 |
|
---|
5624 | if (comp == NULL)
|
---|
5625 | return(-1);
|
---|
5626 | if (comp->determinist != -1)
|
---|
5627 | return(comp->determinist);
|
---|
5628 |
|
---|
5629 | am = xmlNewAutomata();
|
---|
5630 | if (am == NULL)
|
---|
5631 | return(-1);
|
---|
5632 | if (am->states != NULL) {
|
---|
5633 | int i;
|
---|
5634 |
|
---|
5635 | for (i = 0;i < am->nbStates;i++)
|
---|
5636 | xmlRegFreeState(am->states[i]);
|
---|
5637 | xmlFree(am->states);
|
---|
5638 | }
|
---|
5639 | am->nbAtoms = comp->nbAtoms;
|
---|
5640 | am->atoms = comp->atoms;
|
---|
5641 | am->nbStates = comp->nbStates;
|
---|
5642 | am->states = comp->states;
|
---|
5643 | am->determinist = -1;
|
---|
5644 | am->flags = comp->flags;
|
---|
5645 | ret = xmlFAComputesDeterminism(am);
|
---|
5646 | am->atoms = NULL;
|
---|
5647 | am->states = NULL;
|
---|
5648 | xmlFreeAutomata(am);
|
---|
5649 | comp->determinist = ret;
|
---|
5650 | return(ret);
|
---|
5651 | }
|
---|
5652 |
|
---|
5653 | /**
|
---|
5654 | * xmlRegFreeRegexp:
|
---|
5655 | * @regexp: the regexp
|
---|
5656 | *
|
---|
5657 | * Free a regexp
|
---|
5658 | */
|
---|
5659 | void
|
---|
5660 | xmlRegFreeRegexp(xmlRegexpPtr regexp) {
|
---|
5661 | int i;
|
---|
5662 | if (regexp == NULL)
|
---|
5663 | return;
|
---|
5664 |
|
---|
5665 | if (regexp->string != NULL)
|
---|
5666 | xmlFree(regexp->string);
|
---|
5667 | if (regexp->states != NULL) {
|
---|
5668 | for (i = 0;i < regexp->nbStates;i++)
|
---|
5669 | xmlRegFreeState(regexp->states[i]);
|
---|
5670 | xmlFree(regexp->states);
|
---|
5671 | }
|
---|
5672 | if (regexp->atoms != NULL) {
|
---|
5673 | for (i = 0;i < regexp->nbAtoms;i++)
|
---|
5674 | xmlRegFreeAtom(regexp->atoms[i]);
|
---|
5675 | xmlFree(regexp->atoms);
|
---|
5676 | }
|
---|
5677 | if (regexp->counters != NULL)
|
---|
5678 | xmlFree(regexp->counters);
|
---|
5679 | if (regexp->compact != NULL)
|
---|
5680 | xmlFree(regexp->compact);
|
---|
5681 | if (regexp->transdata != NULL)
|
---|
5682 | xmlFree(regexp->transdata);
|
---|
5683 | if (regexp->stringMap != NULL) {
|
---|
5684 | for (i = 0; i < regexp->nbstrings;i++)
|
---|
5685 | xmlFree(regexp->stringMap[i]);
|
---|
5686 | xmlFree(regexp->stringMap);
|
---|
5687 | }
|
---|
5688 |
|
---|
5689 | xmlFree(regexp);
|
---|
5690 | }
|
---|
5691 |
|
---|
5692 | #ifdef LIBXML_AUTOMATA_ENABLED
|
---|
5693 | /************************************************************************
|
---|
5694 | * *
|
---|
5695 | * The Automata interface *
|
---|
5696 | * *
|
---|
5697 | ************************************************************************/
|
---|
5698 |
|
---|
5699 | /**
|
---|
5700 | * xmlNewAutomata:
|
---|
5701 | *
|
---|
5702 | * Create a new automata
|
---|
5703 | *
|
---|
5704 | * Returns the new object or NULL in case of failure
|
---|
5705 | */
|
---|
5706 | xmlAutomataPtr
|
---|
5707 | xmlNewAutomata(void) {
|
---|
5708 | xmlAutomataPtr ctxt;
|
---|
5709 |
|
---|
5710 | ctxt = xmlRegNewParserCtxt(NULL);
|
---|
5711 | if (ctxt == NULL)
|
---|
5712 | return(NULL);
|
---|
5713 |
|
---|
5714 | /* initialize the parser */
|
---|
5715 | ctxt->end = NULL;
|
---|
5716 | ctxt->start = ctxt->state = xmlRegNewState(ctxt);
|
---|
5717 | if (ctxt->start == NULL) {
|
---|
5718 | xmlFreeAutomata(ctxt);
|
---|
5719 | return(NULL);
|
---|
5720 | }
|
---|
5721 | ctxt->start->type = XML_REGEXP_START_STATE;
|
---|
5722 | if (xmlRegStatePush(ctxt, ctxt->start) < 0) {
|
---|
5723 | xmlRegFreeState(ctxt->start);
|
---|
5724 | xmlFreeAutomata(ctxt);
|
---|
5725 | return(NULL);
|
---|
5726 | }
|
---|
5727 | ctxt->flags = 0;
|
---|
5728 |
|
---|
5729 | return(ctxt);
|
---|
5730 | }
|
---|
5731 |
|
---|
5732 | /**
|
---|
5733 | * xmlFreeAutomata:
|
---|
5734 | * @am: an automata
|
---|
5735 | *
|
---|
5736 | * Free an automata
|
---|
5737 | */
|
---|
5738 | void
|
---|
5739 | xmlFreeAutomata(xmlAutomataPtr am) {
|
---|
5740 | if (am == NULL)
|
---|
5741 | return;
|
---|
5742 | xmlRegFreeParserCtxt(am);
|
---|
5743 | }
|
---|
5744 |
|
---|
5745 | /**
|
---|
5746 | * xmlAutomataSetFlags:
|
---|
5747 | * @am: an automata
|
---|
5748 | * @flags: a set of internal flags
|
---|
5749 | *
|
---|
5750 | * Set some flags on the automata
|
---|
5751 | */
|
---|
5752 | void
|
---|
5753 | xmlAutomataSetFlags(xmlAutomataPtr am, int flags) {
|
---|
5754 | if (am == NULL)
|
---|
5755 | return;
|
---|
5756 | am->flags |= flags;
|
---|
5757 | }
|
---|
5758 |
|
---|
5759 | /**
|
---|
5760 | * xmlAutomataGetInitState:
|
---|
5761 | * @am: an automata
|
---|
5762 | *
|
---|
5763 | * Initial state lookup
|
---|
5764 | *
|
---|
5765 | * Returns the initial state of the automata
|
---|
5766 | */
|
---|
5767 | xmlAutomataStatePtr
|
---|
5768 | xmlAutomataGetInitState(xmlAutomataPtr am) {
|
---|
5769 | if (am == NULL)
|
---|
5770 | return(NULL);
|
---|
5771 | return(am->start);
|
---|
5772 | }
|
---|
5773 |
|
---|
5774 | /**
|
---|
5775 | * xmlAutomataSetFinalState:
|
---|
5776 | * @am: an automata
|
---|
5777 | * @state: a state in this automata
|
---|
5778 | *
|
---|
5779 | * Makes that state a final state
|
---|
5780 | *
|
---|
5781 | * Returns 0 or -1 in case of error
|
---|
5782 | */
|
---|
5783 | int
|
---|
5784 | xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) {
|
---|
5785 | if ((am == NULL) || (state == NULL))
|
---|
5786 | return(-1);
|
---|
5787 | state->type = XML_REGEXP_FINAL_STATE;
|
---|
5788 | return(0);
|
---|
5789 | }
|
---|
5790 |
|
---|
5791 | /**
|
---|
5792 | * xmlAutomataNewTransition:
|
---|
5793 | * @am: an automata
|
---|
5794 | * @from: the starting point of the transition
|
---|
5795 | * @to: the target point of the transition or NULL
|
---|
5796 | * @token: the input string associated to that transition
|
---|
5797 | * @data: data passed to the callback function if the transition is activated
|
---|
5798 | *
|
---|
5799 | * If @to is NULL, this creates first a new target state in the automata
|
---|
5800 | * and then adds a transition from the @from state to the target state
|
---|
5801 | * activated by the value of @token
|
---|
5802 | *
|
---|
5803 | * Returns the target state or NULL in case of error
|
---|
5804 | */
|
---|
5805 | xmlAutomataStatePtr
|
---|
5806 | xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
5807 | xmlAutomataStatePtr to, const xmlChar *token,
|
---|
5808 | void *data) {
|
---|
5809 | xmlRegAtomPtr atom;
|
---|
5810 |
|
---|
5811 | if ((am == NULL) || (from == NULL) || (token == NULL))
|
---|
5812 | return(NULL);
|
---|
5813 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
|
---|
5814 | if (atom == NULL)
|
---|
5815 | return(NULL);
|
---|
5816 | atom->data = data;
|
---|
5817 | atom->valuep = xmlStrdup(token);
|
---|
5818 |
|
---|
5819 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
|
---|
5820 | xmlRegFreeAtom(atom);
|
---|
5821 | return(NULL);
|
---|
5822 | }
|
---|
5823 | if (to == NULL)
|
---|
5824 | return(am->state);
|
---|
5825 | return(to);
|
---|
5826 | }
|
---|
5827 |
|
---|
5828 | /**
|
---|
5829 | * xmlAutomataNewTransition2:
|
---|
5830 | * @am: an automata
|
---|
5831 | * @from: the starting point of the transition
|
---|
5832 | * @to: the target point of the transition or NULL
|
---|
5833 | * @token: the first input string associated to that transition
|
---|
5834 | * @token2: the second input string associated to that transition
|
---|
5835 | * @data: data passed to the callback function if the transition is activated
|
---|
5836 | *
|
---|
5837 | * If @to is NULL, this creates first a new target state in the automata
|
---|
5838 | * and then adds a transition from the @from state to the target state
|
---|
5839 | * activated by the value of @token
|
---|
5840 | *
|
---|
5841 | * Returns the target state or NULL in case of error
|
---|
5842 | */
|
---|
5843 | xmlAutomataStatePtr
|
---|
5844 | xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
5845 | xmlAutomataStatePtr to, const xmlChar *token,
|
---|
5846 | const xmlChar *token2, void *data) {
|
---|
5847 | xmlRegAtomPtr atom;
|
---|
5848 |
|
---|
5849 | if ((am == NULL) || (from == NULL) || (token == NULL))
|
---|
5850 | return(NULL);
|
---|
5851 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
|
---|
5852 | if (atom == NULL)
|
---|
5853 | return(NULL);
|
---|
5854 | atom->data = data;
|
---|
5855 | if ((token2 == NULL) || (*token2 == 0)) {
|
---|
5856 | atom->valuep = xmlStrdup(token);
|
---|
5857 | } else {
|
---|
5858 | int lenn, lenp;
|
---|
5859 | xmlChar *str;
|
---|
5860 |
|
---|
5861 | lenn = strlen((char *) token2);
|
---|
5862 | lenp = strlen((char *) token);
|
---|
5863 |
|
---|
5864 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
|
---|
5865 | if (str == NULL) {
|
---|
5866 | xmlRegFreeAtom(atom);
|
---|
5867 | return(NULL);
|
---|
5868 | }
|
---|
5869 | memcpy(&str[0], token, lenp);
|
---|
5870 | str[lenp] = '|';
|
---|
5871 | memcpy(&str[lenp + 1], token2, lenn);
|
---|
5872 | str[lenn + lenp + 1] = 0;
|
---|
5873 |
|
---|
5874 | atom->valuep = str;
|
---|
5875 | }
|
---|
5876 |
|
---|
5877 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
|
---|
5878 | xmlRegFreeAtom(atom);
|
---|
5879 | return(NULL);
|
---|
5880 | }
|
---|
5881 | if (to == NULL)
|
---|
5882 | return(am->state);
|
---|
5883 | return(to);
|
---|
5884 | }
|
---|
5885 |
|
---|
5886 | /**
|
---|
5887 | * xmlAutomataNewNegTrans:
|
---|
5888 | * @am: an automata
|
---|
5889 | * @from: the starting point of the transition
|
---|
5890 | * @to: the target point of the transition or NULL
|
---|
5891 | * @token: the first input string associated to that transition
|
---|
5892 | * @token2: the second input string associated to that transition
|
---|
5893 | * @data: data passed to the callback function if the transition is activated
|
---|
5894 | *
|
---|
5895 | * If @to is NULL, this creates first a new target state in the automata
|
---|
5896 | * and then adds a transition from the @from state to the target state
|
---|
5897 | * activated by any value except (@token,@token2)
|
---|
5898 | * Note that if @token2 is not NULL, then (X, NULL) won't match to follow
|
---|
5899 | # the semantic of XSD ##other
|
---|
5900 | *
|
---|
5901 | * Returns the target state or NULL in case of error
|
---|
5902 | */
|
---|
5903 | xmlAutomataStatePtr
|
---|
5904 | xmlAutomataNewNegTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
5905 | xmlAutomataStatePtr to, const xmlChar *token,
|
---|
5906 | const xmlChar *token2, void *data) {
|
---|
5907 | xmlRegAtomPtr atom;
|
---|
5908 | xmlChar err_msg[200];
|
---|
5909 |
|
---|
5910 | if ((am == NULL) || (from == NULL) || (token == NULL))
|
---|
5911 | return(NULL);
|
---|
5912 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
|
---|
5913 | if (atom == NULL)
|
---|
5914 | return(NULL);
|
---|
5915 | atom->data = data;
|
---|
5916 | atom->neg = 1;
|
---|
5917 | if ((token2 == NULL) || (*token2 == 0)) {
|
---|
5918 | atom->valuep = xmlStrdup(token);
|
---|
5919 | } else {
|
---|
5920 | int lenn, lenp;
|
---|
5921 | xmlChar *str;
|
---|
5922 |
|
---|
5923 | lenn = strlen((char *) token2);
|
---|
5924 | lenp = strlen((char *) token);
|
---|
5925 |
|
---|
5926 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
|
---|
5927 | if (str == NULL) {
|
---|
5928 | xmlRegFreeAtom(atom);
|
---|
5929 | return(NULL);
|
---|
5930 | }
|
---|
5931 | memcpy(&str[0], token, lenp);
|
---|
5932 | str[lenp] = '|';
|
---|
5933 | memcpy(&str[lenp + 1], token2, lenn);
|
---|
5934 | str[lenn + lenp + 1] = 0;
|
---|
5935 |
|
---|
5936 | atom->valuep = str;
|
---|
5937 | }
|
---|
5938 | snprintf((char *) err_msg, 199, "not %s", (const char *) atom->valuep);
|
---|
5939 | err_msg[199] = 0;
|
---|
5940 | atom->valuep2 = xmlStrdup(err_msg);
|
---|
5941 |
|
---|
5942 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) {
|
---|
5943 | xmlRegFreeAtom(atom);
|
---|
5944 | return(NULL);
|
---|
5945 | }
|
---|
5946 | am->negs++;
|
---|
5947 | if (to == NULL)
|
---|
5948 | return(am->state);
|
---|
5949 | return(to);
|
---|
5950 | }
|
---|
5951 |
|
---|
5952 | /**
|
---|
5953 | * xmlAutomataNewCountTrans2:
|
---|
5954 | * @am: an automata
|
---|
5955 | * @from: the starting point of the transition
|
---|
5956 | * @to: the target point of the transition or NULL
|
---|
5957 | * @token: the input string associated to that transition
|
---|
5958 | * @token2: the second input string associated to that transition
|
---|
5959 | * @min: the minimum successive occurrences of token
|
---|
5960 | * @max: the maximum successive occurrences of token
|
---|
5961 | * @data: data associated to the transition
|
---|
5962 | *
|
---|
5963 | * If @to is NULL, this creates first a new target state in the automata
|
---|
5964 | * and then adds a transition from the @from state to the target state
|
---|
5965 | * activated by a succession of input of value @token and @token2 and
|
---|
5966 | * whose number is between @min and @max
|
---|
5967 | *
|
---|
5968 | * Returns the target state or NULL in case of error
|
---|
5969 | */
|
---|
5970 | xmlAutomataStatePtr
|
---|
5971 | xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
5972 | xmlAutomataStatePtr to, const xmlChar *token,
|
---|
5973 | const xmlChar *token2,
|
---|
5974 | int min, int max, void *data) {
|
---|
5975 | xmlRegAtomPtr atom;
|
---|
5976 | int counter;
|
---|
5977 |
|
---|
5978 | if ((am == NULL) || (from == NULL) || (token == NULL))
|
---|
5979 | return(NULL);
|
---|
5980 | if (min < 0)
|
---|
5981 | return(NULL);
|
---|
5982 | if ((max < min) || (max < 1))
|
---|
5983 | return(NULL);
|
---|
5984 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
|
---|
5985 | if (atom == NULL)
|
---|
5986 | return(NULL);
|
---|
5987 | if ((token2 == NULL) || (*token2 == 0)) {
|
---|
5988 | atom->valuep = xmlStrdup(token);
|
---|
5989 | } else {
|
---|
5990 | int lenn, lenp;
|
---|
5991 | xmlChar *str;
|
---|
5992 |
|
---|
5993 | lenn = strlen((char *) token2);
|
---|
5994 | lenp = strlen((char *) token);
|
---|
5995 |
|
---|
5996 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
|
---|
5997 | if (str == NULL) {
|
---|
5998 | xmlRegFreeAtom(atom);
|
---|
5999 | return(NULL);
|
---|
6000 | }
|
---|
6001 | memcpy(&str[0], token, lenp);
|
---|
6002 | str[lenp] = '|';
|
---|
6003 | memcpy(&str[lenp + 1], token2, lenn);
|
---|
6004 | str[lenn + lenp + 1] = 0;
|
---|
6005 |
|
---|
6006 | atom->valuep = str;
|
---|
6007 | }
|
---|
6008 | atom->data = data;
|
---|
6009 | if (min == 0)
|
---|
6010 | atom->min = 1;
|
---|
6011 | else
|
---|
6012 | atom->min = min;
|
---|
6013 | atom->max = max;
|
---|
6014 |
|
---|
6015 | /*
|
---|
6016 | * associate a counter to the transition.
|
---|
6017 | */
|
---|
6018 | counter = xmlRegGetCounter(am);
|
---|
6019 | am->counters[counter].min = min;
|
---|
6020 | am->counters[counter].max = max;
|
---|
6021 |
|
---|
6022 | /* xmlFAGenerateTransitions(am, from, to, atom); */
|
---|
6023 | if (to == NULL) {
|
---|
6024 | to = xmlRegNewState(am);
|
---|
6025 | xmlRegStatePush(am, to);
|
---|
6026 | }
|
---|
6027 | xmlRegStateAddTrans(am, from, atom, to, counter, -1);
|
---|
6028 | xmlRegAtomPush(am, atom);
|
---|
6029 | am->state = to;
|
---|
6030 |
|
---|
6031 | if (to == NULL)
|
---|
6032 | to = am->state;
|
---|
6033 | if (to == NULL)
|
---|
6034 | return(NULL);
|
---|
6035 | if (min == 0)
|
---|
6036 | xmlFAGenerateEpsilonTransition(am, from, to);
|
---|
6037 | return(to);
|
---|
6038 | }
|
---|
6039 |
|
---|
6040 | /**
|
---|
6041 | * xmlAutomataNewCountTrans:
|
---|
6042 | * @am: an automata
|
---|
6043 | * @from: the starting point of the transition
|
---|
6044 | * @to: the target point of the transition or NULL
|
---|
6045 | * @token: the input string associated to that transition
|
---|
6046 | * @min: the minimum successive occurrences of token
|
---|
6047 | * @max: the maximum successive occurrences of token
|
---|
6048 | * @data: data associated to the transition
|
---|
6049 | *
|
---|
6050 | * If @to is NULL, this creates first a new target state in the automata
|
---|
6051 | * and then adds a transition from the @from state to the target state
|
---|
6052 | * activated by a succession of input of value @token and whose number
|
---|
6053 | * is between @min and @max
|
---|
6054 | *
|
---|
6055 | * Returns the target state or NULL in case of error
|
---|
6056 | */
|
---|
6057 | xmlAutomataStatePtr
|
---|
6058 | xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
6059 | xmlAutomataStatePtr to, const xmlChar *token,
|
---|
6060 | int min, int max, void *data) {
|
---|
6061 | xmlRegAtomPtr atom;
|
---|
6062 | int counter;
|
---|
6063 |
|
---|
6064 | if ((am == NULL) || (from == NULL) || (token == NULL))
|
---|
6065 | return(NULL);
|
---|
6066 | if (min < 0)
|
---|
6067 | return(NULL);
|
---|
6068 | if ((max < min) || (max < 1))
|
---|
6069 | return(NULL);
|
---|
6070 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
|
---|
6071 | if (atom == NULL)
|
---|
6072 | return(NULL);
|
---|
6073 | atom->valuep = xmlStrdup(token);
|
---|
6074 | atom->data = data;
|
---|
6075 | if (min == 0)
|
---|
6076 | atom->min = 1;
|
---|
6077 | else
|
---|
6078 | atom->min = min;
|
---|
6079 | atom->max = max;
|
---|
6080 |
|
---|
6081 | /*
|
---|
6082 | * associate a counter to the transition.
|
---|
6083 | */
|
---|
6084 | counter = xmlRegGetCounter(am);
|
---|
6085 | am->counters[counter].min = min;
|
---|
6086 | am->counters[counter].max = max;
|
---|
6087 |
|
---|
6088 | /* xmlFAGenerateTransitions(am, from, to, atom); */
|
---|
6089 | if (to == NULL) {
|
---|
6090 | to = xmlRegNewState(am);
|
---|
6091 | xmlRegStatePush(am, to);
|
---|
6092 | }
|
---|
6093 | xmlRegStateAddTrans(am, from, atom, to, counter, -1);
|
---|
6094 | xmlRegAtomPush(am, atom);
|
---|
6095 | am->state = to;
|
---|
6096 |
|
---|
6097 | if (to == NULL)
|
---|
6098 | to = am->state;
|
---|
6099 | if (to == NULL)
|
---|
6100 | return(NULL);
|
---|
6101 | if (min == 0)
|
---|
6102 | xmlFAGenerateEpsilonTransition(am, from, to);
|
---|
6103 | return(to);
|
---|
6104 | }
|
---|
6105 |
|
---|
6106 | /**
|
---|
6107 | * xmlAutomataNewOnceTrans2:
|
---|
6108 | * @am: an automata
|
---|
6109 | * @from: the starting point of the transition
|
---|
6110 | * @to: the target point of the transition or NULL
|
---|
6111 | * @token: the input string associated to that transition
|
---|
6112 | * @token2: the second input string associated to that transition
|
---|
6113 | * @min: the minimum successive occurrences of token
|
---|
6114 | * @max: the maximum successive occurrences of token
|
---|
6115 | * @data: data associated to the transition
|
---|
6116 | *
|
---|
6117 | * If @to is NULL, this creates first a new target state in the automata
|
---|
6118 | * and then adds a transition from the @from state to the target state
|
---|
6119 | * activated by a succession of input of value @token and @token2 and whose
|
---|
6120 | * number is between @min and @max, moreover that transition can only be
|
---|
6121 | * crossed once.
|
---|
6122 | *
|
---|
6123 | * Returns the target state or NULL in case of error
|
---|
6124 | */
|
---|
6125 | xmlAutomataStatePtr
|
---|
6126 | xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
6127 | xmlAutomataStatePtr to, const xmlChar *token,
|
---|
6128 | const xmlChar *token2,
|
---|
6129 | int min, int max, void *data) {
|
---|
6130 | xmlRegAtomPtr atom;
|
---|
6131 | int counter;
|
---|
6132 |
|
---|
6133 | if ((am == NULL) || (from == NULL) || (token == NULL))
|
---|
6134 | return(NULL);
|
---|
6135 | if (min < 1)
|
---|
6136 | return(NULL);
|
---|
6137 | if (max < min)
|
---|
6138 | return(NULL);
|
---|
6139 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
|
---|
6140 | if (atom == NULL)
|
---|
6141 | return(NULL);
|
---|
6142 | if ((token2 == NULL) || (*token2 == 0)) {
|
---|
6143 | atom->valuep = xmlStrdup(token);
|
---|
6144 | } else {
|
---|
6145 | int lenn, lenp;
|
---|
6146 | xmlChar *str;
|
---|
6147 |
|
---|
6148 | lenn = strlen((char *) token2);
|
---|
6149 | lenp = strlen((char *) token);
|
---|
6150 |
|
---|
6151 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2);
|
---|
6152 | if (str == NULL) {
|
---|
6153 | xmlRegFreeAtom(atom);
|
---|
6154 | return(NULL);
|
---|
6155 | }
|
---|
6156 | memcpy(&str[0], token, lenp);
|
---|
6157 | str[lenp] = '|';
|
---|
6158 | memcpy(&str[lenp + 1], token2, lenn);
|
---|
6159 | str[lenn + lenp + 1] = 0;
|
---|
6160 |
|
---|
6161 | atom->valuep = str;
|
---|
6162 | }
|
---|
6163 | atom->data = data;
|
---|
6164 | atom->quant = XML_REGEXP_QUANT_ONCEONLY;
|
---|
6165 | atom->min = min;
|
---|
6166 | atom->max = max;
|
---|
6167 | /*
|
---|
6168 | * associate a counter to the transition.
|
---|
6169 | */
|
---|
6170 | counter = xmlRegGetCounter(am);
|
---|
6171 | am->counters[counter].min = 1;
|
---|
6172 | am->counters[counter].max = 1;
|
---|
6173 |
|
---|
6174 | /* xmlFAGenerateTransitions(am, from, to, atom); */
|
---|
6175 | if (to == NULL) {
|
---|
6176 | to = xmlRegNewState(am);
|
---|
6177 | xmlRegStatePush(am, to);
|
---|
6178 | }
|
---|
6179 | xmlRegStateAddTrans(am, from, atom, to, counter, -1);
|
---|
6180 | xmlRegAtomPush(am, atom);
|
---|
6181 | am->state = to;
|
---|
6182 | return(to);
|
---|
6183 | }
|
---|
6184 |
|
---|
6185 |
|
---|
6186 |
|
---|
6187 | /**
|
---|
6188 | * xmlAutomataNewOnceTrans:
|
---|
6189 | * @am: an automata
|
---|
6190 | * @from: the starting point of the transition
|
---|
6191 | * @to: the target point of the transition or NULL
|
---|
6192 | * @token: the input string associated to that transition
|
---|
6193 | * @min: the minimum successive occurrences of token
|
---|
6194 | * @max: the maximum successive occurrences of token
|
---|
6195 | * @data: data associated to the transition
|
---|
6196 | *
|
---|
6197 | * If @to is NULL, this creates first a new target state in the automata
|
---|
6198 | * and then adds a transition from the @from state to the target state
|
---|
6199 | * activated by a succession of input of value @token and whose number
|
---|
6200 | * is between @min and @max, moreover that transition can only be crossed
|
---|
6201 | * once.
|
---|
6202 | *
|
---|
6203 | * Returns the target state or NULL in case of error
|
---|
6204 | */
|
---|
6205 | xmlAutomataStatePtr
|
---|
6206 | xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
6207 | xmlAutomataStatePtr to, const xmlChar *token,
|
---|
6208 | int min, int max, void *data) {
|
---|
6209 | xmlRegAtomPtr atom;
|
---|
6210 | int counter;
|
---|
6211 |
|
---|
6212 | if ((am == NULL) || (from == NULL) || (token == NULL))
|
---|
6213 | return(NULL);
|
---|
6214 | if (min < 1)
|
---|
6215 | return(NULL);
|
---|
6216 | if (max < min)
|
---|
6217 | return(NULL);
|
---|
6218 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING);
|
---|
6219 | if (atom == NULL)
|
---|
6220 | return(NULL);
|
---|
6221 | atom->valuep = xmlStrdup(token);
|
---|
6222 | atom->data = data;
|
---|
6223 | atom->quant = XML_REGEXP_QUANT_ONCEONLY;
|
---|
6224 | atom->min = min;
|
---|
6225 | atom->max = max;
|
---|
6226 | /*
|
---|
6227 | * associate a counter to the transition.
|
---|
6228 | */
|
---|
6229 | counter = xmlRegGetCounter(am);
|
---|
6230 | am->counters[counter].min = 1;
|
---|
6231 | am->counters[counter].max = 1;
|
---|
6232 |
|
---|
6233 | /* xmlFAGenerateTransitions(am, from, to, atom); */
|
---|
6234 | if (to == NULL) {
|
---|
6235 | to = xmlRegNewState(am);
|
---|
6236 | xmlRegStatePush(am, to);
|
---|
6237 | }
|
---|
6238 | xmlRegStateAddTrans(am, from, atom, to, counter, -1);
|
---|
6239 | xmlRegAtomPush(am, atom);
|
---|
6240 | am->state = to;
|
---|
6241 | return(to);
|
---|
6242 | }
|
---|
6243 |
|
---|
6244 | /**
|
---|
6245 | * xmlAutomataNewState:
|
---|
6246 | * @am: an automata
|
---|
6247 | *
|
---|
6248 | * Create a new disconnected state in the automata
|
---|
6249 | *
|
---|
6250 | * Returns the new state or NULL in case of error
|
---|
6251 | */
|
---|
6252 | xmlAutomataStatePtr
|
---|
6253 | xmlAutomataNewState(xmlAutomataPtr am) {
|
---|
6254 | xmlAutomataStatePtr to;
|
---|
6255 |
|
---|
6256 | if (am == NULL)
|
---|
6257 | return(NULL);
|
---|
6258 | to = xmlRegNewState(am);
|
---|
6259 | xmlRegStatePush(am, to);
|
---|
6260 | return(to);
|
---|
6261 | }
|
---|
6262 |
|
---|
6263 | /**
|
---|
6264 | * xmlAutomataNewEpsilon:
|
---|
6265 | * @am: an automata
|
---|
6266 | * @from: the starting point of the transition
|
---|
6267 | * @to: the target point of the transition or NULL
|
---|
6268 | *
|
---|
6269 | * If @to is NULL, this creates first a new target state in the automata
|
---|
6270 | * and then adds an epsilon transition from the @from state to the
|
---|
6271 | * target state
|
---|
6272 | *
|
---|
6273 | * Returns the target state or NULL in case of error
|
---|
6274 | */
|
---|
6275 | xmlAutomataStatePtr
|
---|
6276 | xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
6277 | xmlAutomataStatePtr to) {
|
---|
6278 | if ((am == NULL) || (from == NULL))
|
---|
6279 | return(NULL);
|
---|
6280 | xmlFAGenerateEpsilonTransition(am, from, to);
|
---|
6281 | if (to == NULL)
|
---|
6282 | return(am->state);
|
---|
6283 | return(to);
|
---|
6284 | }
|
---|
6285 |
|
---|
6286 | /**
|
---|
6287 | * xmlAutomataNewAllTrans:
|
---|
6288 | * @am: an automata
|
---|
6289 | * @from: the starting point of the transition
|
---|
6290 | * @to: the target point of the transition or NULL
|
---|
6291 | * @lax: allow to transition if not all all transitions have been activated
|
---|
6292 | *
|
---|
6293 | * If @to is NULL, this creates first a new target state in the automata
|
---|
6294 | * and then adds a an ALL transition from the @from state to the
|
---|
6295 | * target state. That transition is an epsilon transition allowed only when
|
---|
6296 | * all transitions from the @from node have been activated.
|
---|
6297 | *
|
---|
6298 | * Returns the target state or NULL in case of error
|
---|
6299 | */
|
---|
6300 | xmlAutomataStatePtr
|
---|
6301 | xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
6302 | xmlAutomataStatePtr to, int lax) {
|
---|
6303 | if ((am == NULL) || (from == NULL))
|
---|
6304 | return(NULL);
|
---|
6305 | xmlFAGenerateAllTransition(am, from, to, lax);
|
---|
6306 | if (to == NULL)
|
---|
6307 | return(am->state);
|
---|
6308 | return(to);
|
---|
6309 | }
|
---|
6310 |
|
---|
6311 | /**
|
---|
6312 | * xmlAutomataNewCounter:
|
---|
6313 | * @am: an automata
|
---|
6314 | * @min: the minimal value on the counter
|
---|
6315 | * @max: the maximal value on the counter
|
---|
6316 | *
|
---|
6317 | * Create a new counter
|
---|
6318 | *
|
---|
6319 | * Returns the counter number or -1 in case of error
|
---|
6320 | */
|
---|
6321 | int
|
---|
6322 | xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) {
|
---|
6323 | int ret;
|
---|
6324 |
|
---|
6325 | if (am == NULL)
|
---|
6326 | return(-1);
|
---|
6327 |
|
---|
6328 | ret = xmlRegGetCounter(am);
|
---|
6329 | if (ret < 0)
|
---|
6330 | return(-1);
|
---|
6331 | am->counters[ret].min = min;
|
---|
6332 | am->counters[ret].max = max;
|
---|
6333 | return(ret);
|
---|
6334 | }
|
---|
6335 |
|
---|
6336 | /**
|
---|
6337 | * xmlAutomataNewCountedTrans:
|
---|
6338 | * @am: an automata
|
---|
6339 | * @from: the starting point of the transition
|
---|
6340 | * @to: the target point of the transition or NULL
|
---|
6341 | * @counter: the counter associated to that transition
|
---|
6342 | *
|
---|
6343 | * If @to is NULL, this creates first a new target state in the automata
|
---|
6344 | * and then adds an epsilon transition from the @from state to the target state
|
---|
6345 | * which will increment the counter provided
|
---|
6346 | *
|
---|
6347 | * Returns the target state or NULL in case of error
|
---|
6348 | */
|
---|
6349 | xmlAutomataStatePtr
|
---|
6350 | xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
6351 | xmlAutomataStatePtr to, int counter) {
|
---|
6352 | if ((am == NULL) || (from == NULL) || (counter < 0))
|
---|
6353 | return(NULL);
|
---|
6354 | xmlFAGenerateCountedEpsilonTransition(am, from, to, counter);
|
---|
6355 | if (to == NULL)
|
---|
6356 | return(am->state);
|
---|
6357 | return(to);
|
---|
6358 | }
|
---|
6359 |
|
---|
6360 | /**
|
---|
6361 | * xmlAutomataNewCounterTrans:
|
---|
6362 | * @am: an automata
|
---|
6363 | * @from: the starting point of the transition
|
---|
6364 | * @to: the target point of the transition or NULL
|
---|
6365 | * @counter: the counter associated to that transition
|
---|
6366 | *
|
---|
6367 | * If @to is NULL, this creates first a new target state in the automata
|
---|
6368 | * and then adds an epsilon transition from the @from state to the target state
|
---|
6369 | * which will be allowed only if the counter is within the right range.
|
---|
6370 | *
|
---|
6371 | * Returns the target state or NULL in case of error
|
---|
6372 | */
|
---|
6373 | xmlAutomataStatePtr
|
---|
6374 | xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from,
|
---|
6375 | xmlAutomataStatePtr to, int counter) {
|
---|
6376 | if ((am == NULL) || (from == NULL) || (counter < 0))
|
---|
6377 | return(NULL);
|
---|
6378 | xmlFAGenerateCountedTransition(am, from, to, counter);
|
---|
6379 | if (to == NULL)
|
---|
6380 | return(am->state);
|
---|
6381 | return(to);
|
---|
6382 | }
|
---|
6383 |
|
---|
6384 | /**
|
---|
6385 | * xmlAutomataCompile:
|
---|
6386 | * @am: an automata
|
---|
6387 | *
|
---|
6388 | * Compile the automata into a Reg Exp ready for being executed.
|
---|
6389 | * The automata should be free after this point.
|
---|
6390 | *
|
---|
6391 | * Returns the compiled regexp or NULL in case of error
|
---|
6392 | */
|
---|
6393 | xmlRegexpPtr
|
---|
6394 | xmlAutomataCompile(xmlAutomataPtr am) {
|
---|
6395 | xmlRegexpPtr ret;
|
---|
6396 |
|
---|
6397 | if ((am == NULL) || (am->error != 0)) return(NULL);
|
---|
6398 | xmlFAEliminateEpsilonTransitions(am);
|
---|
6399 | /* xmlFAComputesDeterminism(am); */
|
---|
6400 | ret = xmlRegEpxFromParse(am);
|
---|
6401 |
|
---|
6402 | return(ret);
|
---|
6403 | }
|
---|
6404 |
|
---|
6405 | /**
|
---|
6406 | * xmlAutomataIsDeterminist:
|
---|
6407 | * @am: an automata
|
---|
6408 | *
|
---|
6409 | * Checks if an automata is determinist.
|
---|
6410 | *
|
---|
6411 | * Returns 1 if true, 0 if not, and -1 in case of error
|
---|
6412 | */
|
---|
6413 | int
|
---|
6414 | xmlAutomataIsDeterminist(xmlAutomataPtr am) {
|
---|
6415 | int ret;
|
---|
6416 |
|
---|
6417 | if (am == NULL)
|
---|
6418 | return(-1);
|
---|
6419 |
|
---|
6420 | ret = xmlFAComputesDeterminism(am);
|
---|
6421 | return(ret);
|
---|
6422 | }
|
---|
6423 | #endif /* LIBXML_AUTOMATA_ENABLED */
|
---|
6424 |
|
---|
6425 | #ifdef LIBXML_EXPR_ENABLED
|
---|
6426 | /************************************************************************
|
---|
6427 | * *
|
---|
6428 | * Formal Expression handling code *
|
---|
6429 | * *
|
---|
6430 | ************************************************************************/
|
---|
6431 | /************************************************************************
|
---|
6432 | * *
|
---|
6433 | * Expression handling context *
|
---|
6434 | * *
|
---|
6435 | ************************************************************************/
|
---|
6436 |
|
---|
6437 | struct _xmlExpCtxt {
|
---|
6438 | xmlDictPtr dict;
|
---|
6439 | xmlExpNodePtr *table;
|
---|
6440 | int size;
|
---|
6441 | int nbElems;
|
---|
6442 | int nb_nodes;
|
---|
6443 | int maxNodes;
|
---|
6444 | const char *expr;
|
---|
6445 | const char *cur;
|
---|
6446 | int nb_cons;
|
---|
6447 | int tabSize;
|
---|
6448 | };
|
---|
6449 |
|
---|
6450 | /**
|
---|
6451 | * xmlExpNewCtxt:
|
---|
6452 | * @maxNodes: the maximum number of nodes
|
---|
6453 | * @dict: optional dictionary to use internally
|
---|
6454 | *
|
---|
6455 | * Creates a new context for manipulating expressions
|
---|
6456 | *
|
---|
6457 | * Returns the context or NULL in case of error
|
---|
6458 | */
|
---|
6459 | xmlExpCtxtPtr
|
---|
6460 | xmlExpNewCtxt(int maxNodes, xmlDictPtr dict) {
|
---|
6461 | xmlExpCtxtPtr ret;
|
---|
6462 | int size = 256;
|
---|
6463 |
|
---|
6464 | if (maxNodes <= 4096)
|
---|
6465 | maxNodes = 4096;
|
---|
6466 |
|
---|
6467 | ret = (xmlExpCtxtPtr) xmlMalloc(sizeof(xmlExpCtxt));
|
---|
6468 | if (ret == NULL)
|
---|
6469 | return(NULL);
|
---|
6470 | memset(ret, 0, sizeof(xmlExpCtxt));
|
---|
6471 | ret->size = size;
|
---|
6472 | ret->nbElems = 0;
|
---|
6473 | ret->maxNodes = maxNodes;
|
---|
6474 | ret->table = xmlMalloc(size * sizeof(xmlExpNodePtr));
|
---|
6475 | if (ret->table == NULL) {
|
---|
6476 | xmlFree(ret);
|
---|
6477 | return(NULL);
|
---|
6478 | }
|
---|
6479 | memset(ret->table, 0, size * sizeof(xmlExpNodePtr));
|
---|
6480 | if (dict == NULL) {
|
---|
6481 | ret->dict = xmlDictCreate();
|
---|
6482 | if (ret->dict == NULL) {
|
---|
6483 | xmlFree(ret->table);
|
---|
6484 | xmlFree(ret);
|
---|
6485 | return(NULL);
|
---|
6486 | }
|
---|
6487 | } else {
|
---|
6488 | ret->dict = dict;
|
---|
6489 | xmlDictReference(ret->dict);
|
---|
6490 | }
|
---|
6491 | return(ret);
|
---|
6492 | }
|
---|
6493 |
|
---|
6494 | /**
|
---|
6495 | * xmlExpFreeCtxt:
|
---|
6496 | * @ctxt: an expression context
|
---|
6497 | *
|
---|
6498 | * Free an expression context
|
---|
6499 | */
|
---|
6500 | void
|
---|
6501 | xmlExpFreeCtxt(xmlExpCtxtPtr ctxt) {
|
---|
6502 | if (ctxt == NULL)
|
---|
6503 | return;
|
---|
6504 | xmlDictFree(ctxt->dict);
|
---|
6505 | if (ctxt->table != NULL)
|
---|
6506 | xmlFree(ctxt->table);
|
---|
6507 | xmlFree(ctxt);
|
---|
6508 | }
|
---|
6509 |
|
---|
6510 | /************************************************************************
|
---|
6511 | * *
|
---|
6512 | * Structure associated to an expression node *
|
---|
6513 | * *
|
---|
6514 | ************************************************************************/
|
---|
6515 | #define MAX_NODES 10000
|
---|
6516 |
|
---|
6517 | /* #define DEBUG_DERIV */
|
---|
6518 |
|
---|
6519 | /*
|
---|
6520 | * TODO:
|
---|
6521 | * - Wildcards
|
---|
6522 | * - public API for creation
|
---|
6523 | *
|
---|
6524 | * Started
|
---|
6525 | * - regression testing
|
---|
6526 | *
|
---|
6527 | * Done
|
---|
6528 | * - split into module and test tool
|
---|
6529 | * - memleaks
|
---|
6530 | */
|
---|
6531 |
|
---|
6532 | typedef enum {
|
---|
6533 | XML_EXP_NILABLE = (1 << 0)
|
---|
6534 | } xmlExpNodeInfo;
|
---|
6535 |
|
---|
6536 | #define IS_NILLABLE(node) ((node)->info & XML_EXP_NILABLE)
|
---|
6537 |
|
---|
6538 | struct _xmlExpNode {
|
---|
6539 | unsigned char type;/* xmlExpNodeType */
|
---|
6540 | unsigned char info;/* OR of xmlExpNodeInfo */
|
---|
6541 | unsigned short key; /* the hash key */
|
---|
6542 | unsigned int ref; /* The number of references */
|
---|
6543 | int c_max; /* the maximum length it can consume */
|
---|
6544 | xmlExpNodePtr exp_left;
|
---|
6545 | xmlExpNodePtr next;/* the next node in the hash table or free list */
|
---|
6546 | union {
|
---|
6547 | struct {
|
---|
6548 | int f_min;
|
---|
6549 | int f_max;
|
---|
6550 | } count;
|
---|
6551 | struct {
|
---|
6552 | xmlExpNodePtr f_right;
|
---|
6553 | } children;
|
---|
6554 | const xmlChar *f_str;
|
---|
6555 | } field;
|
---|
6556 | };
|
---|
6557 |
|
---|
6558 | #define exp_min field.count.f_min
|
---|
6559 | #define exp_max field.count.f_max
|
---|
6560 | /* #define exp_left field.children.f_left */
|
---|
6561 | #define exp_right field.children.f_right
|
---|
6562 | #define exp_str field.f_str
|
---|
6563 |
|
---|
6564 | static xmlExpNodePtr xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type);
|
---|
6565 | static xmlExpNode forbiddenExpNode = {
|
---|
6566 | XML_EXP_FORBID, 0, 0, 0, 0, NULL, NULL, {{ 0, 0}}
|
---|
6567 | };
|
---|
6568 | xmlExpNodePtr forbiddenExp = &forbiddenExpNode;
|
---|
6569 | static xmlExpNode emptyExpNode = {
|
---|
6570 | XML_EXP_EMPTY, 1, 0, 0, 0, NULL, NULL, {{ 0, 0}}
|
---|
6571 | };
|
---|
6572 | xmlExpNodePtr emptyExp = &emptyExpNode;
|
---|
6573 |
|
---|
6574 | /************************************************************************
|
---|
6575 | * *
|
---|
6576 | * The custom hash table for unicity and canonicalization *
|
---|
6577 | * of sub-expressions pointers *
|
---|
6578 | * *
|
---|
6579 | ************************************************************************/
|
---|
6580 | /*
|
---|
6581 | * xmlExpHashNameComputeKey:
|
---|
6582 | * Calculate the hash key for a token
|
---|
6583 | */
|
---|
6584 | static unsigned short
|
---|
6585 | xmlExpHashNameComputeKey(const xmlChar *name) {
|
---|
6586 | unsigned short value = 0L;
|
---|
6587 | char ch;
|
---|
6588 |
|
---|
6589 | if (name != NULL) {
|
---|
6590 | value += 30 * (*name);
|
---|
6591 | while ((ch = *name++) != 0) {
|
---|
6592 | value = value ^ ((value << 5) + (value >> 3) + (unsigned long)ch);
|
---|
6593 | }
|
---|
6594 | }
|
---|
6595 | return (value);
|
---|
6596 | }
|
---|
6597 |
|
---|
6598 | /*
|
---|
6599 | * xmlExpHashComputeKey:
|
---|
6600 | * Calculate the hash key for a compound expression
|
---|
6601 | */
|
---|
6602 | static unsigned short
|
---|
6603 | xmlExpHashComputeKey(xmlExpNodeType type, xmlExpNodePtr left,
|
---|
6604 | xmlExpNodePtr right) {
|
---|
6605 | unsigned long value;
|
---|
6606 | unsigned short ret;
|
---|
6607 |
|
---|
6608 | switch (type) {
|
---|
6609 | case XML_EXP_SEQ:
|
---|
6610 | value = left->key;
|
---|
6611 | value += right->key;
|
---|
6612 | value *= 3;
|
---|
6613 | ret = (unsigned short) value;
|
---|
6614 | break;
|
---|
6615 | case XML_EXP_OR:
|
---|
6616 | value = left->key;
|
---|
6617 | value += right->key;
|
---|
6618 | value *= 7;
|
---|
6619 | ret = (unsigned short) value;
|
---|
6620 | break;
|
---|
6621 | case XML_EXP_COUNT:
|
---|
6622 | value = left->key;
|
---|
6623 | value += right->key;
|
---|
6624 | ret = (unsigned short) value;
|
---|
6625 | break;
|
---|
6626 | default:
|
---|
6627 | ret = 0;
|
---|
6628 | }
|
---|
6629 | return(ret);
|
---|
6630 | }
|
---|
6631 |
|
---|
6632 |
|
---|
6633 | static xmlExpNodePtr
|
---|
6634 | xmlExpNewNode(xmlExpCtxtPtr ctxt, xmlExpNodeType type) {
|
---|
6635 | xmlExpNodePtr ret;
|
---|
6636 |
|
---|
6637 | if (ctxt->nb_nodes >= MAX_NODES)
|
---|
6638 | return(NULL);
|
---|
6639 | ret = (xmlExpNodePtr) xmlMalloc(sizeof(xmlExpNode));
|
---|
6640 | if (ret == NULL)
|
---|
6641 | return(NULL);
|
---|
6642 | memset(ret, 0, sizeof(xmlExpNode));
|
---|
6643 | ret->type = type;
|
---|
6644 | ret->next = NULL;
|
---|
6645 | ctxt->nb_nodes++;
|
---|
6646 | ctxt->nb_cons++;
|
---|
6647 | return(ret);
|
---|
6648 | }
|
---|
6649 |
|
---|
6650 | /**
|
---|
6651 | * xmlExpHashGetEntry:
|
---|
6652 | * @table: the hash table
|
---|
6653 | *
|
---|
6654 | * Get the unique entry from the hash table. The entry is created if
|
---|
6655 | * needed. @left and @right are consumed, i.e. their ref count will
|
---|
6656 | * be decremented by the operation.
|
---|
6657 | *
|
---|
6658 | * Returns the pointer or NULL in case of error
|
---|
6659 | */
|
---|
6660 | static xmlExpNodePtr
|
---|
6661 | xmlExpHashGetEntry(xmlExpCtxtPtr ctxt, xmlExpNodeType type,
|
---|
6662 | xmlExpNodePtr left, xmlExpNodePtr right,
|
---|
6663 | const xmlChar *name, int min, int max) {
|
---|
6664 | unsigned short kbase, key;
|
---|
6665 | xmlExpNodePtr entry;
|
---|
6666 | xmlExpNodePtr insert;
|
---|
6667 |
|
---|
6668 | if (ctxt == NULL)
|
---|
6669 | return(NULL);
|
---|
6670 |
|
---|
6671 | /*
|
---|
6672 | * Check for duplicate and insertion location.
|
---|
6673 | */
|
---|
6674 | if (type == XML_EXP_ATOM) {
|
---|
6675 | kbase = xmlExpHashNameComputeKey(name);
|
---|
6676 | } else if (type == XML_EXP_COUNT) {
|
---|
6677 | /* COUNT reduction rule 1 */
|
---|
6678 | /* a{1} -> a */
|
---|
6679 | if (min == max) {
|
---|
6680 | if (min == 1) {
|
---|
6681 | return(left);
|
---|
6682 | }
|
---|
6683 | if (min == 0) {
|
---|
6684 | xmlExpFree(ctxt, left);
|
---|
6685 | return(emptyExp);
|
---|
6686 | }
|
---|
6687 | }
|
---|
6688 | if (min < 0) {
|
---|
6689 | xmlExpFree(ctxt, left);
|
---|
6690 | return(forbiddenExp);
|
---|
6691 | }
|
---|
6692 | if (max == -1)
|
---|
6693 | kbase = min + 79;
|
---|
6694 | else
|
---|
6695 | kbase = max - min;
|
---|
6696 | kbase += left->key;
|
---|
6697 | } else if (type == XML_EXP_OR) {
|
---|
6698 | /* Forbid reduction rules */
|
---|
6699 | if (left->type == XML_EXP_FORBID) {
|
---|
6700 | xmlExpFree(ctxt, left);
|
---|
6701 | return(right);
|
---|
6702 | }
|
---|
6703 | if (right->type == XML_EXP_FORBID) {
|
---|
6704 | xmlExpFree(ctxt, right);
|
---|
6705 | return(left);
|
---|
6706 | }
|
---|
6707 |
|
---|
6708 | /* OR reduction rule 1 */
|
---|
6709 | /* a | a reduced to a */
|
---|
6710 | if (left == right) {
|
---|
6711 | xmlExpFree(ctxt, right);
|
---|
6712 | return(left);
|
---|
6713 | }
|
---|
6714 | /* OR canonicalization rule 1 */
|
---|
6715 | /* linearize (a | b) | c into a | (b | c) */
|
---|
6716 | if ((left->type == XML_EXP_OR) && (right->type != XML_EXP_OR)) {
|
---|
6717 | xmlExpNodePtr tmp = left;
|
---|
6718 | left = right;
|
---|
6719 | right = tmp;
|
---|
6720 | }
|
---|
6721 | /* OR reduction rule 2 */
|
---|
6722 | /* a | (a | b) and b | (a | b) are reduced to a | b */
|
---|
6723 | if (right->type == XML_EXP_OR) {
|
---|
6724 | if ((left == right->exp_left) ||
|
---|
6725 | (left == right->exp_right)) {
|
---|
6726 | xmlExpFree(ctxt, left);
|
---|
6727 | return(right);
|
---|
6728 | }
|
---|
6729 | }
|
---|
6730 | /* OR canonicalization rule 2 */
|
---|
6731 | /* linearize (a | b) | c into a | (b | c) */
|
---|
6732 | if (left->type == XML_EXP_OR) {
|
---|
6733 | xmlExpNodePtr tmp;
|
---|
6734 |
|
---|
6735 | /* OR canonicalization rule 2 */
|
---|
6736 | if ((left->exp_right->type != XML_EXP_OR) &&
|
---|
6737 | (left->exp_right->key < left->exp_left->key)) {
|
---|
6738 | tmp = left->exp_right;
|
---|
6739 | left->exp_right = left->exp_left;
|
---|
6740 | left->exp_left = tmp;
|
---|
6741 | }
|
---|
6742 | left->exp_right->ref++;
|
---|
6743 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_right, right,
|
---|
6744 | NULL, 0, 0);
|
---|
6745 | left->exp_left->ref++;
|
---|
6746 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left->exp_left, tmp,
|
---|
6747 | NULL, 0, 0);
|
---|
6748 |
|
---|
6749 | xmlExpFree(ctxt, left);
|
---|
6750 | return(tmp);
|
---|
6751 | }
|
---|
6752 | if (right->type == XML_EXP_OR) {
|
---|
6753 | /* Ordering in the tree */
|
---|
6754 | /* C | (A | B) -> A | (B | C) */
|
---|
6755 | if (left->key > right->exp_right->key) {
|
---|
6756 | xmlExpNodePtr tmp;
|
---|
6757 | right->exp_right->ref++;
|
---|
6758 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_right,
|
---|
6759 | left, NULL, 0, 0);
|
---|
6760 | right->exp_left->ref++;
|
---|
6761 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
|
---|
6762 | tmp, NULL, 0, 0);
|
---|
6763 | xmlExpFree(ctxt, right);
|
---|
6764 | return(tmp);
|
---|
6765 | }
|
---|
6766 | /* Ordering in the tree */
|
---|
6767 | /* B | (A | C) -> A | (B | C) */
|
---|
6768 | if (left->key > right->exp_left->key) {
|
---|
6769 | xmlExpNodePtr tmp;
|
---|
6770 | right->exp_right->ref++;
|
---|
6771 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, left,
|
---|
6772 | right->exp_right, NULL, 0, 0);
|
---|
6773 | right->exp_left->ref++;
|
---|
6774 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_OR, right->exp_left,
|
---|
6775 | tmp, NULL, 0, 0);
|
---|
6776 | xmlExpFree(ctxt, right);
|
---|
6777 | return(tmp);
|
---|
6778 | }
|
---|
6779 | }
|
---|
6780 | /* we know both types are != XML_EXP_OR here */
|
---|
6781 | else if (left->key > right->key) {
|
---|
6782 | xmlExpNodePtr tmp = left;
|
---|
6783 | left = right;
|
---|
6784 | right = tmp;
|
---|
6785 | }
|
---|
6786 | kbase = xmlExpHashComputeKey(type, left, right);
|
---|
6787 | } else if (type == XML_EXP_SEQ) {
|
---|
6788 | /* Forbid reduction rules */
|
---|
6789 | if (left->type == XML_EXP_FORBID) {
|
---|
6790 | xmlExpFree(ctxt, right);
|
---|
6791 | return(left);
|
---|
6792 | }
|
---|
6793 | if (right->type == XML_EXP_FORBID) {
|
---|
6794 | xmlExpFree(ctxt, left);
|
---|
6795 | return(right);
|
---|
6796 | }
|
---|
6797 | /* Empty reduction rules */
|
---|
6798 | if (right->type == XML_EXP_EMPTY) {
|
---|
6799 | return(left);
|
---|
6800 | }
|
---|
6801 | if (left->type == XML_EXP_EMPTY) {
|
---|
6802 | return(right);
|
---|
6803 | }
|
---|
6804 | kbase = xmlExpHashComputeKey(type, left, right);
|
---|
6805 | } else
|
---|
6806 | return(NULL);
|
---|
6807 |
|
---|
6808 | key = kbase % ctxt->size;
|
---|
6809 | if (ctxt->table[key] != NULL) {
|
---|
6810 | for (insert = ctxt->table[key]; insert != NULL;
|
---|
6811 | insert = insert->next) {
|
---|
6812 | if ((insert->key == kbase) &&
|
---|
6813 | (insert->type == type)) {
|
---|
6814 | if (type == XML_EXP_ATOM) {
|
---|
6815 | if (name == insert->exp_str) {
|
---|
6816 | insert->ref++;
|
---|
6817 | return(insert);
|
---|
6818 | }
|
---|
6819 | } else if (type == XML_EXP_COUNT) {
|
---|
6820 | if ((insert->exp_min == min) && (insert->exp_max == max) &&
|
---|
6821 | (insert->exp_left == left)) {
|
---|
6822 | insert->ref++;
|
---|
6823 | left->ref--;
|
---|
6824 | return(insert);
|
---|
6825 | }
|
---|
6826 | } else if ((insert->exp_left == left) &&
|
---|
6827 | (insert->exp_right == right)) {
|
---|
6828 | insert->ref++;
|
---|
6829 | left->ref--;
|
---|
6830 | right->ref--;
|
---|
6831 | return(insert);
|
---|
6832 | }
|
---|
6833 | }
|
---|
6834 | }
|
---|
6835 | }
|
---|
6836 |
|
---|
6837 | entry = xmlExpNewNode(ctxt, type);
|
---|
6838 | if (entry == NULL)
|
---|
6839 | return(NULL);
|
---|
6840 | entry->key = kbase;
|
---|
6841 | if (type == XML_EXP_ATOM) {
|
---|
6842 | entry->exp_str = name;
|
---|
6843 | entry->c_max = 1;
|
---|
6844 | } else if (type == XML_EXP_COUNT) {
|
---|
6845 | entry->exp_min = min;
|
---|
6846 | entry->exp_max = max;
|
---|
6847 | entry->exp_left = left;
|
---|
6848 | if ((min == 0) || (IS_NILLABLE(left)))
|
---|
6849 | entry->info |= XML_EXP_NILABLE;
|
---|
6850 | if (max < 0)
|
---|
6851 | entry->c_max = -1;
|
---|
6852 | else
|
---|
6853 | entry->c_max = max * entry->exp_left->c_max;
|
---|
6854 | } else {
|
---|
6855 | entry->exp_left = left;
|
---|
6856 | entry->exp_right = right;
|
---|
6857 | if (type == XML_EXP_OR) {
|
---|
6858 | if ((IS_NILLABLE(left)) || (IS_NILLABLE(right)))
|
---|
6859 | entry->info |= XML_EXP_NILABLE;
|
---|
6860 | if ((entry->exp_left->c_max == -1) ||
|
---|
6861 | (entry->exp_right->c_max == -1))
|
---|
6862 | entry->c_max = -1;
|
---|
6863 | else if (entry->exp_left->c_max > entry->exp_right->c_max)
|
---|
6864 | entry->c_max = entry->exp_left->c_max;
|
---|
6865 | else
|
---|
6866 | entry->c_max = entry->exp_right->c_max;
|
---|
6867 | } else {
|
---|
6868 | if ((IS_NILLABLE(left)) && (IS_NILLABLE(right)))
|
---|
6869 | entry->info |= XML_EXP_NILABLE;
|
---|
6870 | if ((entry->exp_left->c_max == -1) ||
|
---|
6871 | (entry->exp_right->c_max == -1))
|
---|
6872 | entry->c_max = -1;
|
---|
6873 | else
|
---|
6874 | entry->c_max = entry->exp_left->c_max + entry->exp_right->c_max;
|
---|
6875 | }
|
---|
6876 | }
|
---|
6877 | entry->ref = 1;
|
---|
6878 | if (ctxt->table[key] != NULL)
|
---|
6879 | entry->next = ctxt->table[key];
|
---|
6880 |
|
---|
6881 | ctxt->table[key] = entry;
|
---|
6882 | ctxt->nbElems++;
|
---|
6883 |
|
---|
6884 | return(entry);
|
---|
6885 | }
|
---|
6886 |
|
---|
6887 | /**
|
---|
6888 | * xmlExpFree:
|
---|
6889 | * @ctxt: the expression context
|
---|
6890 | * @exp: the expression
|
---|
6891 | *
|
---|
6892 | * Dereference the expression
|
---|
6893 | */
|
---|
6894 | void
|
---|
6895 | xmlExpFree(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp) {
|
---|
6896 | if ((exp == NULL) || (exp == forbiddenExp) || (exp == emptyExp))
|
---|
6897 | return;
|
---|
6898 | exp->ref--;
|
---|
6899 | if (exp->ref == 0) {
|
---|
6900 | unsigned short key;
|
---|
6901 |
|
---|
6902 | /* Unlink it first from the hash table */
|
---|
6903 | key = exp->key % ctxt->size;
|
---|
6904 | if (ctxt->table[key] == exp) {
|
---|
6905 | ctxt->table[key] = exp->next;
|
---|
6906 | } else {
|
---|
6907 | xmlExpNodePtr tmp;
|
---|
6908 |
|
---|
6909 | tmp = ctxt->table[key];
|
---|
6910 | while (tmp != NULL) {
|
---|
6911 | if (tmp->next == exp) {
|
---|
6912 | tmp->next = exp->next;
|
---|
6913 | break;
|
---|
6914 | }
|
---|
6915 | tmp = tmp->next;
|
---|
6916 | }
|
---|
6917 | }
|
---|
6918 |
|
---|
6919 | if ((exp->type == XML_EXP_SEQ) || (exp->type == XML_EXP_OR)) {
|
---|
6920 | xmlExpFree(ctxt, exp->exp_left);
|
---|
6921 | xmlExpFree(ctxt, exp->exp_right);
|
---|
6922 | } else if (exp->type == XML_EXP_COUNT) {
|
---|
6923 | xmlExpFree(ctxt, exp->exp_left);
|
---|
6924 | }
|
---|
6925 | xmlFree(exp);
|
---|
6926 | ctxt->nb_nodes--;
|
---|
6927 | }
|
---|
6928 | }
|
---|
6929 |
|
---|
6930 | /**
|
---|
6931 | * xmlExpRef:
|
---|
6932 | * @exp: the expression
|
---|
6933 | *
|
---|
6934 | * Increase the reference count of the expression
|
---|
6935 | */
|
---|
6936 | void
|
---|
6937 | xmlExpRef(xmlExpNodePtr exp) {
|
---|
6938 | if (exp != NULL)
|
---|
6939 | exp->ref++;
|
---|
6940 | }
|
---|
6941 |
|
---|
6942 | /**
|
---|
6943 | * xmlExpNewAtom:
|
---|
6944 | * @ctxt: the expression context
|
---|
6945 | * @name: the atom name
|
---|
6946 | * @len: the atom name length in byte (or -1);
|
---|
6947 | *
|
---|
6948 | * Get the atom associated to this name from that context
|
---|
6949 | *
|
---|
6950 | * Returns the node or NULL in case of error
|
---|
6951 | */
|
---|
6952 | xmlExpNodePtr
|
---|
6953 | xmlExpNewAtom(xmlExpCtxtPtr ctxt, const xmlChar *name, int len) {
|
---|
6954 | if ((ctxt == NULL) || (name == NULL))
|
---|
6955 | return(NULL);
|
---|
6956 | name = xmlDictLookup(ctxt->dict, name, len);
|
---|
6957 | if (name == NULL)
|
---|
6958 | return(NULL);
|
---|
6959 | return(xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, name, 0, 0));
|
---|
6960 | }
|
---|
6961 |
|
---|
6962 | /**
|
---|
6963 | * xmlExpNewOr:
|
---|
6964 | * @ctxt: the expression context
|
---|
6965 | * @left: left expression
|
---|
6966 | * @right: right expression
|
---|
6967 | *
|
---|
6968 | * Get the atom associated to the choice @left | @right
|
---|
6969 | * Note that @left and @right are consumed in the operation, to keep
|
---|
6970 | * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
|
---|
6971 | * this is true even in case of failure (unless ctxt == NULL).
|
---|
6972 | *
|
---|
6973 | * Returns the node or NULL in case of error
|
---|
6974 | */
|
---|
6975 | xmlExpNodePtr
|
---|
6976 | xmlExpNewOr(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
|
---|
6977 | if (ctxt == NULL)
|
---|
6978 | return(NULL);
|
---|
6979 | if ((left == NULL) || (right == NULL)) {
|
---|
6980 | xmlExpFree(ctxt, left);
|
---|
6981 | xmlExpFree(ctxt, right);
|
---|
6982 | return(NULL);
|
---|
6983 | }
|
---|
6984 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, left, right, NULL, 0, 0));
|
---|
6985 | }
|
---|
6986 |
|
---|
6987 | /**
|
---|
6988 | * xmlExpNewSeq:
|
---|
6989 | * @ctxt: the expression context
|
---|
6990 | * @left: left expression
|
---|
6991 | * @right: right expression
|
---|
6992 | *
|
---|
6993 | * Get the atom associated to the sequence @left , @right
|
---|
6994 | * Note that @left and @right are consumed in the operation, to keep
|
---|
6995 | * an handle on them use xmlExpRef() and use xmlExpFree() to release them,
|
---|
6996 | * this is true even in case of failure (unless ctxt == NULL).
|
---|
6997 | *
|
---|
6998 | * Returns the node or NULL in case of error
|
---|
6999 | */
|
---|
7000 | xmlExpNodePtr
|
---|
7001 | xmlExpNewSeq(xmlExpCtxtPtr ctxt, xmlExpNodePtr left, xmlExpNodePtr right) {
|
---|
7002 | if (ctxt == NULL)
|
---|
7003 | return(NULL);
|
---|
7004 | if ((left == NULL) || (right == NULL)) {
|
---|
7005 | xmlExpFree(ctxt, left);
|
---|
7006 | xmlExpFree(ctxt, right);
|
---|
7007 | return(NULL);
|
---|
7008 | }
|
---|
7009 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, left, right, NULL, 0, 0));
|
---|
7010 | }
|
---|
7011 |
|
---|
7012 | /**
|
---|
7013 | * xmlExpNewRange:
|
---|
7014 | * @ctxt: the expression context
|
---|
7015 | * @subset: the expression to be repeated
|
---|
7016 | * @min: the lower bound for the repetition
|
---|
7017 | * @max: the upper bound for the repetition, -1 means infinite
|
---|
7018 | *
|
---|
7019 | * Get the atom associated to the range (@subset){@min, @max}
|
---|
7020 | * Note that @subset is consumed in the operation, to keep
|
---|
7021 | * an handle on it use xmlExpRef() and use xmlExpFree() to release it,
|
---|
7022 | * this is true even in case of failure (unless ctxt == NULL).
|
---|
7023 | *
|
---|
7024 | * Returns the node or NULL in case of error
|
---|
7025 | */
|
---|
7026 | xmlExpNodePtr
|
---|
7027 | xmlExpNewRange(xmlExpCtxtPtr ctxt, xmlExpNodePtr subset, int min, int max) {
|
---|
7028 | if (ctxt == NULL)
|
---|
7029 | return(NULL);
|
---|
7030 | if ((subset == NULL) || (min < 0) || (max < -1) ||
|
---|
7031 | ((max >= 0) && (min > max))) {
|
---|
7032 | xmlExpFree(ctxt, subset);
|
---|
7033 | return(NULL);
|
---|
7034 | }
|
---|
7035 | return(xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, subset,
|
---|
7036 | NULL, NULL, min, max));
|
---|
7037 | }
|
---|
7038 |
|
---|
7039 | /************************************************************************
|
---|
7040 | * *
|
---|
7041 | * Public API for operations on expressions *
|
---|
7042 | * *
|
---|
7043 | ************************************************************************/
|
---|
7044 |
|
---|
7045 | static int
|
---|
7046 | xmlExpGetLanguageInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
|
---|
7047 | const xmlChar**list, int len, int nb) {
|
---|
7048 | int tmp, tmp2;
|
---|
7049 | tail:
|
---|
7050 | switch (exp->type) {
|
---|
7051 | case XML_EXP_EMPTY:
|
---|
7052 | return(0);
|
---|
7053 | case XML_EXP_ATOM:
|
---|
7054 | for (tmp = 0;tmp < nb;tmp++)
|
---|
7055 | if (list[tmp] == exp->exp_str)
|
---|
7056 | return(0);
|
---|
7057 | if (nb >= len)
|
---|
7058 | return(-2);
|
---|
7059 | list[nb] = exp->exp_str;
|
---|
7060 | return(1);
|
---|
7061 | case XML_EXP_COUNT:
|
---|
7062 | exp = exp->exp_left;
|
---|
7063 | goto tail;
|
---|
7064 | case XML_EXP_SEQ:
|
---|
7065 | case XML_EXP_OR:
|
---|
7066 | tmp = xmlExpGetLanguageInt(ctxt, exp->exp_left, list, len, nb);
|
---|
7067 | if (tmp < 0)
|
---|
7068 | return(tmp);
|
---|
7069 | tmp2 = xmlExpGetLanguageInt(ctxt, exp->exp_right, list, len,
|
---|
7070 | nb + tmp);
|
---|
7071 | if (tmp2 < 0)
|
---|
7072 | return(tmp2);
|
---|
7073 | return(tmp + tmp2);
|
---|
7074 | }
|
---|
7075 | return(-1);
|
---|
7076 | }
|
---|
7077 |
|
---|
7078 | /**
|
---|
7079 | * xmlExpGetLanguage:
|
---|
7080 | * @ctxt: the expression context
|
---|
7081 | * @exp: the expression
|
---|
7082 | * @langList: where to store the tokens
|
---|
7083 | * @len: the allocated length of @list
|
---|
7084 | *
|
---|
7085 | * Find all the strings used in @exp and store them in @list
|
---|
7086 | *
|
---|
7087 | * Returns the number of unique strings found, -1 in case of errors and
|
---|
7088 | * -2 if there is more than @len strings
|
---|
7089 | */
|
---|
7090 | int
|
---|
7091 | xmlExpGetLanguage(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
|
---|
7092 | const xmlChar**langList, int len) {
|
---|
7093 | if ((ctxt == NULL) || (exp == NULL) || (langList == NULL) || (len <= 0))
|
---|
7094 | return(-1);
|
---|
7095 | return(xmlExpGetLanguageInt(ctxt, exp, langList, len, 0));
|
---|
7096 | }
|
---|
7097 |
|
---|
7098 | static int
|
---|
7099 | xmlExpGetStartInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
|
---|
7100 | const xmlChar**list, int len, int nb) {
|
---|
7101 | int tmp, tmp2;
|
---|
7102 | tail:
|
---|
7103 | switch (exp->type) {
|
---|
7104 | case XML_EXP_FORBID:
|
---|
7105 | return(0);
|
---|
7106 | case XML_EXP_EMPTY:
|
---|
7107 | return(0);
|
---|
7108 | case XML_EXP_ATOM:
|
---|
7109 | for (tmp = 0;tmp < nb;tmp++)
|
---|
7110 | if (list[tmp] == exp->exp_str)
|
---|
7111 | return(0);
|
---|
7112 | if (nb >= len)
|
---|
7113 | return(-2);
|
---|
7114 | list[nb] = exp->exp_str;
|
---|
7115 | return(1);
|
---|
7116 | case XML_EXP_COUNT:
|
---|
7117 | exp = exp->exp_left;
|
---|
7118 | goto tail;
|
---|
7119 | case XML_EXP_SEQ:
|
---|
7120 | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
|
---|
7121 | if (tmp < 0)
|
---|
7122 | return(tmp);
|
---|
7123 | if (IS_NILLABLE(exp->exp_left)) {
|
---|
7124 | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
|
---|
7125 | nb + tmp);
|
---|
7126 | if (tmp2 < 0)
|
---|
7127 | return(tmp2);
|
---|
7128 | tmp += tmp2;
|
---|
7129 | }
|
---|
7130 | return(tmp);
|
---|
7131 | case XML_EXP_OR:
|
---|
7132 | tmp = xmlExpGetStartInt(ctxt, exp->exp_left, list, len, nb);
|
---|
7133 | if (tmp < 0)
|
---|
7134 | return(tmp);
|
---|
7135 | tmp2 = xmlExpGetStartInt(ctxt, exp->exp_right, list, len,
|
---|
7136 | nb + tmp);
|
---|
7137 | if (tmp2 < 0)
|
---|
7138 | return(tmp2);
|
---|
7139 | return(tmp + tmp2);
|
---|
7140 | }
|
---|
7141 | return(-1);
|
---|
7142 | }
|
---|
7143 |
|
---|
7144 | /**
|
---|
7145 | * xmlExpGetStart:
|
---|
7146 | * @ctxt: the expression context
|
---|
7147 | * @exp: the expression
|
---|
7148 | * @tokList: where to store the tokens
|
---|
7149 | * @len: the allocated length of @list
|
---|
7150 | *
|
---|
7151 | * Find all the strings that appears at the start of the languages
|
---|
7152 | * accepted by @exp and store them in @list. E.g. for (a, b) | c
|
---|
7153 | * it will return the list [a, c]
|
---|
7154 | *
|
---|
7155 | * Returns the number of unique strings found, -1 in case of errors and
|
---|
7156 | * -2 if there is more than @len strings
|
---|
7157 | */
|
---|
7158 | int
|
---|
7159 | xmlExpGetStart(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
|
---|
7160 | const xmlChar**tokList, int len) {
|
---|
7161 | if ((ctxt == NULL) || (exp == NULL) || (tokList == NULL) || (len <= 0))
|
---|
7162 | return(-1);
|
---|
7163 | return(xmlExpGetStartInt(ctxt, exp, tokList, len, 0));
|
---|
7164 | }
|
---|
7165 |
|
---|
7166 | /**
|
---|
7167 | * xmlExpIsNillable:
|
---|
7168 | * @exp: the expression
|
---|
7169 | *
|
---|
7170 | * Finds if the expression is nillable, i.e. if it accepts the empty sequence
|
---|
7171 | *
|
---|
7172 | * Returns 1 if nillable, 0 if not and -1 in case of error
|
---|
7173 | */
|
---|
7174 | int
|
---|
7175 | xmlExpIsNillable(xmlExpNodePtr exp) {
|
---|
7176 | if (exp == NULL)
|
---|
7177 | return(-1);
|
---|
7178 | return(IS_NILLABLE(exp) != 0);
|
---|
7179 | }
|
---|
7180 |
|
---|
7181 | static xmlExpNodePtr
|
---|
7182 | xmlExpStringDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, const xmlChar *str)
|
---|
7183 | {
|
---|
7184 | xmlExpNodePtr ret;
|
---|
7185 |
|
---|
7186 | switch (exp->type) {
|
---|
7187 | case XML_EXP_EMPTY:
|
---|
7188 | return(forbiddenExp);
|
---|
7189 | case XML_EXP_FORBID:
|
---|
7190 | return(forbiddenExp);
|
---|
7191 | case XML_EXP_ATOM:
|
---|
7192 | if (exp->exp_str == str) {
|
---|
7193 | #ifdef DEBUG_DERIV
|
---|
7194 | printf("deriv atom: equal => Empty\n");
|
---|
7195 | #endif
|
---|
7196 | ret = emptyExp;
|
---|
7197 | } else {
|
---|
7198 | #ifdef DEBUG_DERIV
|
---|
7199 | printf("deriv atom: mismatch => forbid\n");
|
---|
7200 | #endif
|
---|
7201 | /* TODO wildcards here */
|
---|
7202 | ret = forbiddenExp;
|
---|
7203 | }
|
---|
7204 | return(ret);
|
---|
7205 | case XML_EXP_OR: {
|
---|
7206 | xmlExpNodePtr tmp;
|
---|
7207 |
|
---|
7208 | #ifdef DEBUG_DERIV
|
---|
7209 | printf("deriv or: => or(derivs)\n");
|
---|
7210 | #endif
|
---|
7211 | tmp = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
|
---|
7212 | if (tmp == NULL) {
|
---|
7213 | return(NULL);
|
---|
7214 | }
|
---|
7215 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
|
---|
7216 | if (ret == NULL) {
|
---|
7217 | xmlExpFree(ctxt, tmp);
|
---|
7218 | return(NULL);
|
---|
7219 | }
|
---|
7220 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret,
|
---|
7221 | NULL, 0, 0);
|
---|
7222 | return(ret);
|
---|
7223 | }
|
---|
7224 | case XML_EXP_SEQ:
|
---|
7225 | #ifdef DEBUG_DERIV
|
---|
7226 | printf("deriv seq: starting with left\n");
|
---|
7227 | #endif
|
---|
7228 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
|
---|
7229 | if (ret == NULL) {
|
---|
7230 | return(NULL);
|
---|
7231 | } else if (ret == forbiddenExp) {
|
---|
7232 | if (IS_NILLABLE(exp->exp_left)) {
|
---|
7233 | #ifdef DEBUG_DERIV
|
---|
7234 | printf("deriv seq: left failed but nillable\n");
|
---|
7235 | #endif
|
---|
7236 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_right, str);
|
---|
7237 | }
|
---|
7238 | } else {
|
---|
7239 | #ifdef DEBUG_DERIV
|
---|
7240 | printf("deriv seq: left match => sequence\n");
|
---|
7241 | #endif
|
---|
7242 | exp->exp_right->ref++;
|
---|
7243 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, exp->exp_right,
|
---|
7244 | NULL, 0, 0);
|
---|
7245 | }
|
---|
7246 | return(ret);
|
---|
7247 | case XML_EXP_COUNT: {
|
---|
7248 | int min, max;
|
---|
7249 | xmlExpNodePtr tmp;
|
---|
7250 |
|
---|
7251 | if (exp->exp_max == 0)
|
---|
7252 | return(forbiddenExp);
|
---|
7253 | ret = xmlExpStringDeriveInt(ctxt, exp->exp_left, str);
|
---|
7254 | if (ret == NULL)
|
---|
7255 | return(NULL);
|
---|
7256 | if (ret == forbiddenExp) {
|
---|
7257 | #ifdef DEBUG_DERIV
|
---|
7258 | printf("deriv count: pattern mismatch => forbid\n");
|
---|
7259 | #endif
|
---|
7260 | return(ret);
|
---|
7261 | }
|
---|
7262 | if (exp->exp_max == 1)
|
---|
7263 | return(ret);
|
---|
7264 | if (exp->exp_max < 0) /* unbounded */
|
---|
7265 | max = -1;
|
---|
7266 | else
|
---|
7267 | max = exp->exp_max - 1;
|
---|
7268 | if (exp->exp_min > 0)
|
---|
7269 | min = exp->exp_min - 1;
|
---|
7270 | else
|
---|
7271 | min = 0;
|
---|
7272 | exp->exp_left->ref++;
|
---|
7273 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left, NULL,
|
---|
7274 | NULL, min, max);
|
---|
7275 | if (ret == emptyExp) {
|
---|
7276 | #ifdef DEBUG_DERIV
|
---|
7277 | printf("deriv count: match to empty => new count\n");
|
---|
7278 | #endif
|
---|
7279 | return(tmp);
|
---|
7280 | }
|
---|
7281 | #ifdef DEBUG_DERIV
|
---|
7282 | printf("deriv count: match => sequence with new count\n");
|
---|
7283 | #endif
|
---|
7284 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, tmp,
|
---|
7285 | NULL, 0, 0));
|
---|
7286 | }
|
---|
7287 | }
|
---|
7288 | return(NULL);
|
---|
7289 | }
|
---|
7290 |
|
---|
7291 | /**
|
---|
7292 | * xmlExpStringDerive:
|
---|
7293 | * @ctxt: the expression context
|
---|
7294 | * @exp: the expression
|
---|
7295 | * @str: the string
|
---|
7296 | * @len: the string len in bytes if available
|
---|
7297 | *
|
---|
7298 | * Do one step of Brzozowski derivation of the expression @exp with
|
---|
7299 | * respect to the input string
|
---|
7300 | *
|
---|
7301 | * Returns the resulting expression or NULL in case of internal error
|
---|
7302 | */
|
---|
7303 | xmlExpNodePtr
|
---|
7304 | xmlExpStringDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
|
---|
7305 | const xmlChar *str, int len) {
|
---|
7306 | const xmlChar *input;
|
---|
7307 |
|
---|
7308 | if ((exp == NULL) || (ctxt == NULL) || (str == NULL)) {
|
---|
7309 | return(NULL);
|
---|
7310 | }
|
---|
7311 | /*
|
---|
7312 | * check the string is in the dictionary, if yes use an interned
|
---|
7313 | * copy, otherwise we know it's not an acceptable input
|
---|
7314 | */
|
---|
7315 | input = xmlDictExists(ctxt->dict, str, len);
|
---|
7316 | if (input == NULL) {
|
---|
7317 | return(forbiddenExp);
|
---|
7318 | }
|
---|
7319 | return(xmlExpStringDeriveInt(ctxt, exp, input));
|
---|
7320 | }
|
---|
7321 |
|
---|
7322 | static int
|
---|
7323 | xmlExpCheckCard(xmlExpNodePtr exp, xmlExpNodePtr sub) {
|
---|
7324 | int ret = 1;
|
---|
7325 |
|
---|
7326 | if (sub->c_max == -1) {
|
---|
7327 | if (exp->c_max != -1)
|
---|
7328 | ret = 0;
|
---|
7329 | } else if ((exp->c_max >= 0) && (exp->c_max < sub->c_max)) {
|
---|
7330 | ret = 0;
|
---|
7331 | }
|
---|
7332 | #if 0
|
---|
7333 | if ((IS_NILLABLE(sub)) && (!IS_NILLABLE(exp)))
|
---|
7334 | ret = 0;
|
---|
7335 | #endif
|
---|
7336 | return(ret);
|
---|
7337 | }
|
---|
7338 |
|
---|
7339 | static xmlExpNodePtr xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp,
|
---|
7340 | xmlExpNodePtr sub);
|
---|
7341 | /**
|
---|
7342 | * xmlExpDivide:
|
---|
7343 | * @ctxt: the expressions context
|
---|
7344 | * @exp: the englobing expression
|
---|
7345 | * @sub: the subexpression
|
---|
7346 | * @mult: the multiple expression
|
---|
7347 | * @remain: the remain from the derivation of the multiple
|
---|
7348 | *
|
---|
7349 | * Check if exp is a multiple of sub, i.e. if there is a finite number n
|
---|
7350 | * so that sub{n} subsume exp
|
---|
7351 | *
|
---|
7352 | * Returns the multiple value if successful, 0 if it is not a multiple
|
---|
7353 | * and -1 in case of internal error.
|
---|
7354 | */
|
---|
7355 |
|
---|
7356 | static int
|
---|
7357 | xmlExpDivide(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub,
|
---|
7358 | xmlExpNodePtr *mult, xmlExpNodePtr *remain) {
|
---|
7359 | int i;
|
---|
7360 | xmlExpNodePtr tmp, tmp2;
|
---|
7361 |
|
---|
7362 | if (mult != NULL) *mult = NULL;
|
---|
7363 | if (remain != NULL) *remain = NULL;
|
---|
7364 | if (exp->c_max == -1) return(0);
|
---|
7365 | if (IS_NILLABLE(exp) && (!IS_NILLABLE(sub))) return(0);
|
---|
7366 |
|
---|
7367 | for (i = 1;i <= exp->c_max;i++) {
|
---|
7368 | sub->ref++;
|
---|
7369 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
|
---|
7370 | sub, NULL, NULL, i, i);
|
---|
7371 | if (tmp == NULL) {
|
---|
7372 | return(-1);
|
---|
7373 | }
|
---|
7374 | if (!xmlExpCheckCard(tmp, exp)) {
|
---|
7375 | xmlExpFree(ctxt, tmp);
|
---|
7376 | continue;
|
---|
7377 | }
|
---|
7378 | tmp2 = xmlExpExpDeriveInt(ctxt, tmp, exp);
|
---|
7379 | if (tmp2 == NULL) {
|
---|
7380 | xmlExpFree(ctxt, tmp);
|
---|
7381 | return(-1);
|
---|
7382 | }
|
---|
7383 | if ((tmp2 != forbiddenExp) && (IS_NILLABLE(tmp2))) {
|
---|
7384 | if (remain != NULL)
|
---|
7385 | *remain = tmp2;
|
---|
7386 | else
|
---|
7387 | xmlExpFree(ctxt, tmp2);
|
---|
7388 | if (mult != NULL)
|
---|
7389 | *mult = tmp;
|
---|
7390 | else
|
---|
7391 | xmlExpFree(ctxt, tmp);
|
---|
7392 | #ifdef DEBUG_DERIV
|
---|
7393 | printf("Divide succeeded %d\n", i);
|
---|
7394 | #endif
|
---|
7395 | return(i);
|
---|
7396 | }
|
---|
7397 | xmlExpFree(ctxt, tmp);
|
---|
7398 | xmlExpFree(ctxt, tmp2);
|
---|
7399 | }
|
---|
7400 | #ifdef DEBUG_DERIV
|
---|
7401 | printf("Divide failed\n");
|
---|
7402 | #endif
|
---|
7403 | return(0);
|
---|
7404 | }
|
---|
7405 |
|
---|
7406 | /**
|
---|
7407 | * xmlExpExpDeriveInt:
|
---|
7408 | * @ctxt: the expressions context
|
---|
7409 | * @exp: the englobing expression
|
---|
7410 | * @sub: the subexpression
|
---|
7411 | *
|
---|
7412 | * Try to do a step of Brzozowski derivation but at a higher level
|
---|
7413 | * the input being a subexpression.
|
---|
7414 | *
|
---|
7415 | * Returns the resulting expression or NULL in case of internal error
|
---|
7416 | */
|
---|
7417 | static xmlExpNodePtr
|
---|
7418 | xmlExpExpDeriveInt(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
|
---|
7419 | xmlExpNodePtr ret, tmp, tmp2, tmp3;
|
---|
7420 | const xmlChar **tab;
|
---|
7421 | int len, i;
|
---|
7422 |
|
---|
7423 | /*
|
---|
7424 | * In case of equality and if the expression can only consume a finite
|
---|
7425 | * amount, then the derivation is empty
|
---|
7426 | */
|
---|
7427 | if ((exp == sub) && (exp->c_max >= 0)) {
|
---|
7428 | #ifdef DEBUG_DERIV
|
---|
7429 | printf("Equal(exp, sub) and finite -> Empty\n");
|
---|
7430 | #endif
|
---|
7431 | return(emptyExp);
|
---|
7432 | }
|
---|
7433 | /*
|
---|
7434 | * decompose sub sequence first
|
---|
7435 | */
|
---|
7436 | if (sub->type == XML_EXP_EMPTY) {
|
---|
7437 | #ifdef DEBUG_DERIV
|
---|
7438 | printf("Empty(sub) -> Empty\n");
|
---|
7439 | #endif
|
---|
7440 | exp->ref++;
|
---|
7441 | return(exp);
|
---|
7442 | }
|
---|
7443 | if (sub->type == XML_EXP_SEQ) {
|
---|
7444 | #ifdef DEBUG_DERIV
|
---|
7445 | printf("Seq(sub) -> decompose\n");
|
---|
7446 | #endif
|
---|
7447 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
|
---|
7448 | if (tmp == NULL)
|
---|
7449 | return(NULL);
|
---|
7450 | if (tmp == forbiddenExp)
|
---|
7451 | return(tmp);
|
---|
7452 | ret = xmlExpExpDeriveInt(ctxt, tmp, sub->exp_right);
|
---|
7453 | xmlExpFree(ctxt, tmp);
|
---|
7454 | return(ret);
|
---|
7455 | }
|
---|
7456 | if (sub->type == XML_EXP_OR) {
|
---|
7457 | #ifdef DEBUG_DERIV
|
---|
7458 | printf("Or(sub) -> decompose\n");
|
---|
7459 | #endif
|
---|
7460 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub->exp_left);
|
---|
7461 | if (tmp == forbiddenExp)
|
---|
7462 | return(tmp);
|
---|
7463 | if (tmp == NULL)
|
---|
7464 | return(NULL);
|
---|
7465 | ret = xmlExpExpDeriveInt(ctxt, exp, sub->exp_right);
|
---|
7466 | if ((ret == NULL) || (ret == forbiddenExp)) {
|
---|
7467 | xmlExpFree(ctxt, tmp);
|
---|
7468 | return(ret);
|
---|
7469 | }
|
---|
7470 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, tmp, ret, NULL, 0, 0));
|
---|
7471 | }
|
---|
7472 | if (!xmlExpCheckCard(exp, sub)) {
|
---|
7473 | #ifdef DEBUG_DERIV
|
---|
7474 | printf("CheckCard(exp, sub) failed -> Forbid\n");
|
---|
7475 | #endif
|
---|
7476 | return(forbiddenExp);
|
---|
7477 | }
|
---|
7478 | switch (exp->type) {
|
---|
7479 | case XML_EXP_EMPTY:
|
---|
7480 | if (sub == emptyExp)
|
---|
7481 | return(emptyExp);
|
---|
7482 | #ifdef DEBUG_DERIV
|
---|
7483 | printf("Empty(exp) -> Forbid\n");
|
---|
7484 | #endif
|
---|
7485 | return(forbiddenExp);
|
---|
7486 | case XML_EXP_FORBID:
|
---|
7487 | #ifdef DEBUG_DERIV
|
---|
7488 | printf("Forbid(exp) -> Forbid\n");
|
---|
7489 | #endif
|
---|
7490 | return(forbiddenExp);
|
---|
7491 | case XML_EXP_ATOM:
|
---|
7492 | if (sub->type == XML_EXP_ATOM) {
|
---|
7493 | /* TODO: handle wildcards */
|
---|
7494 | if (exp->exp_str == sub->exp_str) {
|
---|
7495 | #ifdef DEBUG_DERIV
|
---|
7496 | printf("Atom match -> Empty\n");
|
---|
7497 | #endif
|
---|
7498 | return(emptyExp);
|
---|
7499 | }
|
---|
7500 | #ifdef DEBUG_DERIV
|
---|
7501 | printf("Atom mismatch -> Forbid\n");
|
---|
7502 | #endif
|
---|
7503 | return(forbiddenExp);
|
---|
7504 | }
|
---|
7505 | if ((sub->type == XML_EXP_COUNT) &&
|
---|
7506 | (sub->exp_max == 1) &&
|
---|
7507 | (sub->exp_left->type == XML_EXP_ATOM)) {
|
---|
7508 | /* TODO: handle wildcards */
|
---|
7509 | if (exp->exp_str == sub->exp_left->exp_str) {
|
---|
7510 | #ifdef DEBUG_DERIV
|
---|
7511 | printf("Atom match -> Empty\n");
|
---|
7512 | #endif
|
---|
7513 | return(emptyExp);
|
---|
7514 | }
|
---|
7515 | #ifdef DEBUG_DERIV
|
---|
7516 | printf("Atom mismatch -> Forbid\n");
|
---|
7517 | #endif
|
---|
7518 | return(forbiddenExp);
|
---|
7519 | }
|
---|
7520 | #ifdef DEBUG_DERIV
|
---|
7521 | printf("Complex exp vs Atom -> Forbid\n");
|
---|
7522 | #endif
|
---|
7523 | return(forbiddenExp);
|
---|
7524 | case XML_EXP_SEQ:
|
---|
7525 | /* try to get the sequence consumed only if possible */
|
---|
7526 | if (xmlExpCheckCard(exp->exp_left, sub)) {
|
---|
7527 | /* See if the sequence can be consumed directly */
|
---|
7528 | #ifdef DEBUG_DERIV
|
---|
7529 | printf("Seq trying left only\n");
|
---|
7530 | #endif
|
---|
7531 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
|
---|
7532 | if ((ret != forbiddenExp) && (ret != NULL)) {
|
---|
7533 | #ifdef DEBUG_DERIV
|
---|
7534 | printf("Seq trying left only worked\n");
|
---|
7535 | #endif
|
---|
7536 | /*
|
---|
7537 | * TODO: assumption here that we are determinist
|
---|
7538 | * i.e. we won't get to a nillable exp left
|
---|
7539 | * subset which could be matched by the right
|
---|
7540 | * part too.
|
---|
7541 | * e.g.: (a | b)+,(a | c) and 'a+,a'
|
---|
7542 | */
|
---|
7543 | exp->exp_right->ref++;
|
---|
7544 | return(xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
|
---|
7545 | exp->exp_right, NULL, 0, 0));
|
---|
7546 | }
|
---|
7547 | #ifdef DEBUG_DERIV
|
---|
7548 | } else {
|
---|
7549 | printf("Seq: left too short\n");
|
---|
7550 | #endif
|
---|
7551 | }
|
---|
7552 | /* Try instead to decompose */
|
---|
7553 | if (sub->type == XML_EXP_COUNT) {
|
---|
7554 | int min, max;
|
---|
7555 |
|
---|
7556 | #ifdef DEBUG_DERIV
|
---|
7557 | printf("Seq: sub is a count\n");
|
---|
7558 | #endif
|
---|
7559 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
|
---|
7560 | if (ret == NULL)
|
---|
7561 | return(NULL);
|
---|
7562 | if (ret != forbiddenExp) {
|
---|
7563 | #ifdef DEBUG_DERIV
|
---|
7564 | printf("Seq , Count match on left\n");
|
---|
7565 | #endif
|
---|
7566 | if (sub->exp_max < 0)
|
---|
7567 | max = -1;
|
---|
7568 | else
|
---|
7569 | max = sub->exp_max -1;
|
---|
7570 | if (sub->exp_min > 0)
|
---|
7571 | min = sub->exp_min -1;
|
---|
7572 | else
|
---|
7573 | min = 0;
|
---|
7574 | exp->exp_right->ref++;
|
---|
7575 | tmp = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret,
|
---|
7576 | exp->exp_right, NULL, 0, 0);
|
---|
7577 | if (tmp == NULL)
|
---|
7578 | return(NULL);
|
---|
7579 |
|
---|
7580 | sub->exp_left->ref++;
|
---|
7581 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT,
|
---|
7582 | sub->exp_left, NULL, NULL, min, max);
|
---|
7583 | if (tmp2 == NULL) {
|
---|
7584 | xmlExpFree(ctxt, tmp);
|
---|
7585 | return(NULL);
|
---|
7586 | }
|
---|
7587 | ret = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
|
---|
7588 | xmlExpFree(ctxt, tmp);
|
---|
7589 | xmlExpFree(ctxt, tmp2);
|
---|
7590 | return(ret);
|
---|
7591 | }
|
---|
7592 | }
|
---|
7593 | /* we made no progress on structured operations */
|
---|
7594 | break;
|
---|
7595 | case XML_EXP_OR:
|
---|
7596 | #ifdef DEBUG_DERIV
|
---|
7597 | printf("Or , trying both side\n");
|
---|
7598 | #endif
|
---|
7599 | ret = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
|
---|
7600 | if (ret == NULL)
|
---|
7601 | return(NULL);
|
---|
7602 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_right, sub);
|
---|
7603 | if (tmp == NULL) {
|
---|
7604 | xmlExpFree(ctxt, ret);
|
---|
7605 | return(NULL);
|
---|
7606 | }
|
---|
7607 | return(xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp, NULL, 0, 0));
|
---|
7608 | case XML_EXP_COUNT: {
|
---|
7609 | int min, max;
|
---|
7610 |
|
---|
7611 | if (sub->type == XML_EXP_COUNT) {
|
---|
7612 | /*
|
---|
7613 | * Try to see if the loop is completely subsumed
|
---|
7614 | */
|
---|
7615 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub->exp_left);
|
---|
7616 | if (tmp == NULL)
|
---|
7617 | return(NULL);
|
---|
7618 | if (tmp == forbiddenExp) {
|
---|
7619 | int mult;
|
---|
7620 |
|
---|
7621 | #ifdef DEBUG_DERIV
|
---|
7622 | printf("Count, Count inner don't subsume\n");
|
---|
7623 | #endif
|
---|
7624 | mult = xmlExpDivide(ctxt, sub->exp_left, exp->exp_left,
|
---|
7625 | NULL, &tmp);
|
---|
7626 | if (mult <= 0) {
|
---|
7627 | #ifdef DEBUG_DERIV
|
---|
7628 | printf("Count, Count not multiple => forbidden\n");
|
---|
7629 | #endif
|
---|
7630 | return(forbiddenExp);
|
---|
7631 | }
|
---|
7632 | if (sub->exp_max == -1) {
|
---|
7633 | max = -1;
|
---|
7634 | if (exp->exp_max == -1) {
|
---|
7635 | if (exp->exp_min <= sub->exp_min * mult)
|
---|
7636 | min = 0;
|
---|
7637 | else
|
---|
7638 | min = exp->exp_min - sub->exp_min * mult;
|
---|
7639 | } else {
|
---|
7640 | #ifdef DEBUG_DERIV
|
---|
7641 | printf("Count, Count finite can't subsume infinite\n");
|
---|
7642 | #endif
|
---|
7643 | xmlExpFree(ctxt, tmp);
|
---|
7644 | return(forbiddenExp);
|
---|
7645 | }
|
---|
7646 | } else {
|
---|
7647 | if (exp->exp_max == -1) {
|
---|
7648 | #ifdef DEBUG_DERIV
|
---|
7649 | printf("Infinite loop consume mult finite loop\n");
|
---|
7650 | #endif
|
---|
7651 | if (exp->exp_min > sub->exp_min * mult) {
|
---|
7652 | max = -1;
|
---|
7653 | min = exp->exp_min - sub->exp_min * mult;
|
---|
7654 | } else {
|
---|
7655 | max = -1;
|
---|
7656 | min = 0;
|
---|
7657 | }
|
---|
7658 | } else {
|
---|
7659 | if (exp->exp_max < sub->exp_max * mult) {
|
---|
7660 | #ifdef DEBUG_DERIV
|
---|
7661 | printf("loops max mult mismatch => forbidden\n");
|
---|
7662 | #endif
|
---|
7663 | xmlExpFree(ctxt, tmp);
|
---|
7664 | return(forbiddenExp);
|
---|
7665 | }
|
---|
7666 | if (sub->exp_max * mult > exp->exp_min)
|
---|
7667 | min = 0;
|
---|
7668 | else
|
---|
7669 | min = exp->exp_min - sub->exp_max * mult;
|
---|
7670 | max = exp->exp_max - sub->exp_max * mult;
|
---|
7671 | }
|
---|
7672 | }
|
---|
7673 | } else if (!IS_NILLABLE(tmp)) {
|
---|
7674 | /*
|
---|
7675 | * TODO: loop here to try to grow if working on finite
|
---|
7676 | * blocks.
|
---|
7677 | */
|
---|
7678 | #ifdef DEBUG_DERIV
|
---|
7679 | printf("Count, Count remain not nillable => forbidden\n");
|
---|
7680 | #endif
|
---|
7681 | xmlExpFree(ctxt, tmp);
|
---|
7682 | return(forbiddenExp);
|
---|
7683 | } else if (sub->exp_max == -1) {
|
---|
7684 | if (exp->exp_max == -1) {
|
---|
7685 | if (exp->exp_min <= sub->exp_min) {
|
---|
7686 | #ifdef DEBUG_DERIV
|
---|
7687 | printf("Infinite loops Okay => COUNT(0,Inf)\n");
|
---|
7688 | #endif
|
---|
7689 | max = -1;
|
---|
7690 | min = 0;
|
---|
7691 | } else {
|
---|
7692 | #ifdef DEBUG_DERIV
|
---|
7693 | printf("Infinite loops min => Count(X,Inf)\n");
|
---|
7694 | #endif
|
---|
7695 | max = -1;
|
---|
7696 | min = exp->exp_min - sub->exp_min;
|
---|
7697 | }
|
---|
7698 | } else if (exp->exp_min > sub->exp_min) {
|
---|
7699 | #ifdef DEBUG_DERIV
|
---|
7700 | printf("loops min mismatch 1 => forbidden ???\n");
|
---|
7701 | #endif
|
---|
7702 | xmlExpFree(ctxt, tmp);
|
---|
7703 | return(forbiddenExp);
|
---|
7704 | } else {
|
---|
7705 | max = -1;
|
---|
7706 | min = 0;
|
---|
7707 | }
|
---|
7708 | } else {
|
---|
7709 | if (exp->exp_max == -1) {
|
---|
7710 | #ifdef DEBUG_DERIV
|
---|
7711 | printf("Infinite loop consume finite loop\n");
|
---|
7712 | #endif
|
---|
7713 | if (exp->exp_min > sub->exp_min) {
|
---|
7714 | max = -1;
|
---|
7715 | min = exp->exp_min - sub->exp_min;
|
---|
7716 | } else {
|
---|
7717 | max = -1;
|
---|
7718 | min = 0;
|
---|
7719 | }
|
---|
7720 | } else {
|
---|
7721 | if (exp->exp_max < sub->exp_max) {
|
---|
7722 | #ifdef DEBUG_DERIV
|
---|
7723 | printf("loops max mismatch => forbidden\n");
|
---|
7724 | #endif
|
---|
7725 | xmlExpFree(ctxt, tmp);
|
---|
7726 | return(forbiddenExp);
|
---|
7727 | }
|
---|
7728 | if (sub->exp_max > exp->exp_min)
|
---|
7729 | min = 0;
|
---|
7730 | else
|
---|
7731 | min = exp->exp_min - sub->exp_max;
|
---|
7732 | max = exp->exp_max - sub->exp_max;
|
---|
7733 | }
|
---|
7734 | }
|
---|
7735 | #ifdef DEBUG_DERIV
|
---|
7736 | printf("loops match => SEQ(COUNT())\n");
|
---|
7737 | #endif
|
---|
7738 | exp->exp_left->ref++;
|
---|
7739 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
|
---|
7740 | NULL, NULL, min, max);
|
---|
7741 | if (tmp2 == NULL) {
|
---|
7742 | return(NULL);
|
---|
7743 | }
|
---|
7744 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
|
---|
7745 | NULL, 0, 0);
|
---|
7746 | return(ret);
|
---|
7747 | }
|
---|
7748 | tmp = xmlExpExpDeriveInt(ctxt, exp->exp_left, sub);
|
---|
7749 | if (tmp == NULL)
|
---|
7750 | return(NULL);
|
---|
7751 | if (tmp == forbiddenExp) {
|
---|
7752 | #ifdef DEBUG_DERIV
|
---|
7753 | printf("loop mismatch => forbidden\n");
|
---|
7754 | #endif
|
---|
7755 | return(forbiddenExp);
|
---|
7756 | }
|
---|
7757 | if (exp->exp_min > 0)
|
---|
7758 | min = exp->exp_min - 1;
|
---|
7759 | else
|
---|
7760 | min = 0;
|
---|
7761 | if (exp->exp_max < 0)
|
---|
7762 | max = -1;
|
---|
7763 | else
|
---|
7764 | max = exp->exp_max - 1;
|
---|
7765 |
|
---|
7766 | #ifdef DEBUG_DERIV
|
---|
7767 | printf("loop match => SEQ(COUNT())\n");
|
---|
7768 | #endif
|
---|
7769 | exp->exp_left->ref++;
|
---|
7770 | tmp2 = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, exp->exp_left,
|
---|
7771 | NULL, NULL, min, max);
|
---|
7772 | if (tmp2 == NULL)
|
---|
7773 | return(NULL);
|
---|
7774 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, tmp, tmp2,
|
---|
7775 | NULL, 0, 0);
|
---|
7776 | return(ret);
|
---|
7777 | }
|
---|
7778 | }
|
---|
7779 |
|
---|
7780 | #ifdef DEBUG_DERIV
|
---|
7781 | printf("Fallback to derivative\n");
|
---|
7782 | #endif
|
---|
7783 | if (IS_NILLABLE(sub)) {
|
---|
7784 | if (!(IS_NILLABLE(exp)))
|
---|
7785 | return(forbiddenExp);
|
---|
7786 | else
|
---|
7787 | ret = emptyExp;
|
---|
7788 | } else
|
---|
7789 | ret = NULL;
|
---|
7790 | /*
|
---|
7791 | * here the structured derivation made no progress so
|
---|
7792 | * we use the default token based derivation to force one more step
|
---|
7793 | */
|
---|
7794 | if (ctxt->tabSize == 0)
|
---|
7795 | ctxt->tabSize = 40;
|
---|
7796 |
|
---|
7797 | tab = (const xmlChar **) xmlMalloc(ctxt->tabSize *
|
---|
7798 | sizeof(const xmlChar *));
|
---|
7799 | if (tab == NULL) {
|
---|
7800 | return(NULL);
|
---|
7801 | }
|
---|
7802 |
|
---|
7803 | /*
|
---|
7804 | * collect all the strings accepted by the subexpression on input
|
---|
7805 | */
|
---|
7806 | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
|
---|
7807 | while (len < 0) {
|
---|
7808 | const xmlChar **temp;
|
---|
7809 | temp = (const xmlChar **) xmlRealloc((xmlChar **) tab, ctxt->tabSize * 2 *
|
---|
7810 | sizeof(const xmlChar *));
|
---|
7811 | if (temp == NULL) {
|
---|
7812 | xmlFree((xmlChar **) tab);
|
---|
7813 | return(NULL);
|
---|
7814 | }
|
---|
7815 | tab = temp;
|
---|
7816 | ctxt->tabSize *= 2;
|
---|
7817 | len = xmlExpGetStartInt(ctxt, sub, tab, ctxt->tabSize, 0);
|
---|
7818 | }
|
---|
7819 | for (i = 0;i < len;i++) {
|
---|
7820 | tmp = xmlExpStringDeriveInt(ctxt, exp, tab[i]);
|
---|
7821 | if ((tmp == NULL) || (tmp == forbiddenExp)) {
|
---|
7822 | xmlExpFree(ctxt, ret);
|
---|
7823 | xmlFree((xmlChar **) tab);
|
---|
7824 | return(tmp);
|
---|
7825 | }
|
---|
7826 | tmp2 = xmlExpStringDeriveInt(ctxt, sub, tab[i]);
|
---|
7827 | if ((tmp2 == NULL) || (tmp2 == forbiddenExp)) {
|
---|
7828 | xmlExpFree(ctxt, tmp);
|
---|
7829 | xmlExpFree(ctxt, ret);
|
---|
7830 | xmlFree((xmlChar **) tab);
|
---|
7831 | return(tmp);
|
---|
7832 | }
|
---|
7833 | tmp3 = xmlExpExpDeriveInt(ctxt, tmp, tmp2);
|
---|
7834 | xmlExpFree(ctxt, tmp);
|
---|
7835 | xmlExpFree(ctxt, tmp2);
|
---|
7836 |
|
---|
7837 | if ((tmp3 == NULL) || (tmp3 == forbiddenExp)) {
|
---|
7838 | xmlExpFree(ctxt, ret);
|
---|
7839 | xmlFree((xmlChar **) tab);
|
---|
7840 | return(tmp3);
|
---|
7841 | }
|
---|
7842 |
|
---|
7843 | if (ret == NULL)
|
---|
7844 | ret = tmp3;
|
---|
7845 | else {
|
---|
7846 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, tmp3, NULL, 0, 0);
|
---|
7847 | if (ret == NULL) {
|
---|
7848 | xmlFree((xmlChar **) tab);
|
---|
7849 | return(NULL);
|
---|
7850 | }
|
---|
7851 | }
|
---|
7852 | }
|
---|
7853 | xmlFree((xmlChar **) tab);
|
---|
7854 | return(ret);
|
---|
7855 | }
|
---|
7856 |
|
---|
7857 | /**
|
---|
7858 | * xmlExpExpDerive:
|
---|
7859 | * @ctxt: the expressions context
|
---|
7860 | * @exp: the englobing expression
|
---|
7861 | * @sub: the subexpression
|
---|
7862 | *
|
---|
7863 | * Evaluates the expression resulting from @exp consuming a sub expression @sub
|
---|
7864 | * Based on algebraic derivation and sometimes direct Brzozowski derivation
|
---|
7865 | * it usually takes less than linear time and can handle expressions generating
|
---|
7866 | * infinite languages.
|
---|
7867 | *
|
---|
7868 | * Returns the resulting expression or NULL in case of internal error, the
|
---|
7869 | * result must be freed
|
---|
7870 | */
|
---|
7871 | xmlExpNodePtr
|
---|
7872 | xmlExpExpDerive(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
|
---|
7873 | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
|
---|
7874 | return(NULL);
|
---|
7875 |
|
---|
7876 | /*
|
---|
7877 | * O(1) speedups
|
---|
7878 | */
|
---|
7879 | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
|
---|
7880 | #ifdef DEBUG_DERIV
|
---|
7881 | printf("Sub nillable and not exp : can't subsume\n");
|
---|
7882 | #endif
|
---|
7883 | return(forbiddenExp);
|
---|
7884 | }
|
---|
7885 | if (xmlExpCheckCard(exp, sub) == 0) {
|
---|
7886 | #ifdef DEBUG_DERIV
|
---|
7887 | printf("sub generate longer sequences than exp : can't subsume\n");
|
---|
7888 | #endif
|
---|
7889 | return(forbiddenExp);
|
---|
7890 | }
|
---|
7891 | return(xmlExpExpDeriveInt(ctxt, exp, sub));
|
---|
7892 | }
|
---|
7893 |
|
---|
7894 | /**
|
---|
7895 | * xmlExpSubsume:
|
---|
7896 | * @ctxt: the expressions context
|
---|
7897 | * @exp: the englobing expression
|
---|
7898 | * @sub: the subexpression
|
---|
7899 | *
|
---|
7900 | * Check whether @exp accepts all the languages accepted by @sub
|
---|
7901 | * the input being a subexpression.
|
---|
7902 | *
|
---|
7903 | * Returns 1 if true 0 if false and -1 in case of failure.
|
---|
7904 | */
|
---|
7905 | int
|
---|
7906 | xmlExpSubsume(xmlExpCtxtPtr ctxt, xmlExpNodePtr exp, xmlExpNodePtr sub) {
|
---|
7907 | xmlExpNodePtr tmp;
|
---|
7908 |
|
---|
7909 | if ((exp == NULL) || (ctxt == NULL) || (sub == NULL))
|
---|
7910 | return(-1);
|
---|
7911 |
|
---|
7912 | /*
|
---|
7913 | * TODO: speedup by checking the language of sub is a subset of the
|
---|
7914 | * language of exp
|
---|
7915 | */
|
---|
7916 | /*
|
---|
7917 | * O(1) speedups
|
---|
7918 | */
|
---|
7919 | if (IS_NILLABLE(sub) && (!IS_NILLABLE(exp))) {
|
---|
7920 | #ifdef DEBUG_DERIV
|
---|
7921 | printf("Sub nillable and not exp : can't subsume\n");
|
---|
7922 | #endif
|
---|
7923 | return(0);
|
---|
7924 | }
|
---|
7925 | if (xmlExpCheckCard(exp, sub) == 0) {
|
---|
7926 | #ifdef DEBUG_DERIV
|
---|
7927 | printf("sub generate longer sequences than exp : can't subsume\n");
|
---|
7928 | #endif
|
---|
7929 | return(0);
|
---|
7930 | }
|
---|
7931 | tmp = xmlExpExpDeriveInt(ctxt, exp, sub);
|
---|
7932 | #ifdef DEBUG_DERIV
|
---|
7933 | printf("Result derivation :\n");
|
---|
7934 | PRINT_EXP(tmp);
|
---|
7935 | #endif
|
---|
7936 | if (tmp == NULL)
|
---|
7937 | return(-1);
|
---|
7938 | if (tmp == forbiddenExp)
|
---|
7939 | return(0);
|
---|
7940 | if (tmp == emptyExp)
|
---|
7941 | return(1);
|
---|
7942 | if ((tmp != NULL) && (IS_NILLABLE(tmp))) {
|
---|
7943 | xmlExpFree(ctxt, tmp);
|
---|
7944 | return(1);
|
---|
7945 | }
|
---|
7946 | xmlExpFree(ctxt, tmp);
|
---|
7947 | return(0);
|
---|
7948 | }
|
---|
7949 |
|
---|
7950 | /************************************************************************
|
---|
7951 | * *
|
---|
7952 | * Parsing expression *
|
---|
7953 | * *
|
---|
7954 | ************************************************************************/
|
---|
7955 |
|
---|
7956 | static xmlExpNodePtr xmlExpParseExpr(xmlExpCtxtPtr ctxt);
|
---|
7957 |
|
---|
7958 | #undef CUR
|
---|
7959 | #define CUR (*ctxt->cur)
|
---|
7960 | #undef NEXT
|
---|
7961 | #define NEXT ctxt->cur++;
|
---|
7962 | #undef IS_BLANK
|
---|
7963 | #define IS_BLANK(c) ((c == ' ') || (c == '\n') || (c == '\r') || (c == '\t'))
|
---|
7964 | #define SKIP_BLANKS while (IS_BLANK(*ctxt->cur)) ctxt->cur++;
|
---|
7965 |
|
---|
7966 | static int
|
---|
7967 | xmlExpParseNumber(xmlExpCtxtPtr ctxt) {
|
---|
7968 | int ret = 0;
|
---|
7969 |
|
---|
7970 | SKIP_BLANKS
|
---|
7971 | if (CUR == '*') {
|
---|
7972 | NEXT
|
---|
7973 | return(-1);
|
---|
7974 | }
|
---|
7975 | if ((CUR < '0') || (CUR > '9'))
|
---|
7976 | return(-1);
|
---|
7977 | while ((CUR >= '0') && (CUR <= '9')) {
|
---|
7978 | ret = ret * 10 + (CUR - '0');
|
---|
7979 | NEXT
|
---|
7980 | }
|
---|
7981 | return(ret);
|
---|
7982 | }
|
---|
7983 |
|
---|
7984 | static xmlExpNodePtr
|
---|
7985 | xmlExpParseOr(xmlExpCtxtPtr ctxt) {
|
---|
7986 | const char *base;
|
---|
7987 | xmlExpNodePtr ret;
|
---|
7988 | const xmlChar *val;
|
---|
7989 |
|
---|
7990 | SKIP_BLANKS
|
---|
7991 | base = ctxt->cur;
|
---|
7992 | if (*ctxt->cur == '(') {
|
---|
7993 | NEXT
|
---|
7994 | ret = xmlExpParseExpr(ctxt);
|
---|
7995 | SKIP_BLANKS
|
---|
7996 | if (*ctxt->cur != ')') {
|
---|
7997 | fprintf(stderr, "unbalanced '(' : %s\n", base);
|
---|
7998 | xmlExpFree(ctxt, ret);
|
---|
7999 | return(NULL);
|
---|
8000 | }
|
---|
8001 | NEXT;
|
---|
8002 | SKIP_BLANKS
|
---|
8003 | goto parse_quantifier;
|
---|
8004 | }
|
---|
8005 | while ((CUR != 0) && (!(IS_BLANK(CUR))) && (CUR != '(') &&
|
---|
8006 | (CUR != ')') && (CUR != '|') && (CUR != ',') && (CUR != '{') &&
|
---|
8007 | (CUR != '*') && (CUR != '+') && (CUR != '?') && (CUR != '}'))
|
---|
8008 | NEXT;
|
---|
8009 | val = xmlDictLookup(ctxt->dict, BAD_CAST base, ctxt->cur - base);
|
---|
8010 | if (val == NULL)
|
---|
8011 | return(NULL);
|
---|
8012 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_ATOM, NULL, NULL, val, 0, 0);
|
---|
8013 | if (ret == NULL)
|
---|
8014 | return(NULL);
|
---|
8015 | SKIP_BLANKS
|
---|
8016 | parse_quantifier:
|
---|
8017 | if (CUR == '{') {
|
---|
8018 | int min, max;
|
---|
8019 |
|
---|
8020 | NEXT
|
---|
8021 | min = xmlExpParseNumber(ctxt);
|
---|
8022 | if (min < 0) {
|
---|
8023 | xmlExpFree(ctxt, ret);
|
---|
8024 | return(NULL);
|
---|
8025 | }
|
---|
8026 | SKIP_BLANKS
|
---|
8027 | if (CUR == ',') {
|
---|
8028 | NEXT
|
---|
8029 | max = xmlExpParseNumber(ctxt);
|
---|
8030 | SKIP_BLANKS
|
---|
8031 | } else
|
---|
8032 | max = min;
|
---|
8033 | if (CUR != '}') {
|
---|
8034 | xmlExpFree(ctxt, ret);
|
---|
8035 | return(NULL);
|
---|
8036 | }
|
---|
8037 | NEXT
|
---|
8038 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
|
---|
8039 | min, max);
|
---|
8040 | SKIP_BLANKS
|
---|
8041 | } else if (CUR == '?') {
|
---|
8042 | NEXT
|
---|
8043 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
|
---|
8044 | 0, 1);
|
---|
8045 | SKIP_BLANKS
|
---|
8046 | } else if (CUR == '+') {
|
---|
8047 | NEXT
|
---|
8048 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
|
---|
8049 | 1, -1);
|
---|
8050 | SKIP_BLANKS
|
---|
8051 | } else if (CUR == '*') {
|
---|
8052 | NEXT
|
---|
8053 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_COUNT, ret, NULL, NULL,
|
---|
8054 | 0, -1);
|
---|
8055 | SKIP_BLANKS
|
---|
8056 | }
|
---|
8057 | return(ret);
|
---|
8058 | }
|
---|
8059 |
|
---|
8060 |
|
---|
8061 | static xmlExpNodePtr
|
---|
8062 | xmlExpParseSeq(xmlExpCtxtPtr ctxt) {
|
---|
8063 | xmlExpNodePtr ret, right;
|
---|
8064 |
|
---|
8065 | ret = xmlExpParseOr(ctxt);
|
---|
8066 | SKIP_BLANKS
|
---|
8067 | while (CUR == '|') {
|
---|
8068 | NEXT
|
---|
8069 | right = xmlExpParseOr(ctxt);
|
---|
8070 | if (right == NULL) {
|
---|
8071 | xmlExpFree(ctxt, ret);
|
---|
8072 | return(NULL);
|
---|
8073 | }
|
---|
8074 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_OR, ret, right, NULL, 0, 0);
|
---|
8075 | if (ret == NULL)
|
---|
8076 | return(NULL);
|
---|
8077 | }
|
---|
8078 | return(ret);
|
---|
8079 | }
|
---|
8080 |
|
---|
8081 | static xmlExpNodePtr
|
---|
8082 | xmlExpParseExpr(xmlExpCtxtPtr ctxt) {
|
---|
8083 | xmlExpNodePtr ret, right;
|
---|
8084 |
|
---|
8085 | ret = xmlExpParseSeq(ctxt);
|
---|
8086 | SKIP_BLANKS
|
---|
8087 | while (CUR == ',') {
|
---|
8088 | NEXT
|
---|
8089 | right = xmlExpParseSeq(ctxt);
|
---|
8090 | if (right == NULL) {
|
---|
8091 | xmlExpFree(ctxt, ret);
|
---|
8092 | return(NULL);
|
---|
8093 | }
|
---|
8094 | ret = xmlExpHashGetEntry(ctxt, XML_EXP_SEQ, ret, right, NULL, 0, 0);
|
---|
8095 | if (ret == NULL)
|
---|
8096 | return(NULL);
|
---|
8097 | }
|
---|
8098 | return(ret);
|
---|
8099 | }
|
---|
8100 |
|
---|
8101 | /**
|
---|
8102 | * xmlExpParse:
|
---|
8103 | * @ctxt: the expressions context
|
---|
8104 | * @expr: the 0 terminated string
|
---|
8105 | *
|
---|
8106 | * Minimal parser for regexps, it understand the following constructs
|
---|
8107 | * - string terminals
|
---|
8108 | * - choice operator |
|
---|
8109 | * - sequence operator ,
|
---|
8110 | * - subexpressions (...)
|
---|
8111 | * - usual cardinality operators + * and ?
|
---|
8112 | * - finite sequences { min, max }
|
---|
8113 | * - infinite sequences { min, * }
|
---|
8114 | * There is minimal checkings made especially no checking on strings values
|
---|
8115 | *
|
---|
8116 | * Returns a new expression or NULL in case of failure
|
---|
8117 | */
|
---|
8118 | xmlExpNodePtr
|
---|
8119 | xmlExpParse(xmlExpCtxtPtr ctxt, const char *expr) {
|
---|
8120 | xmlExpNodePtr ret;
|
---|
8121 |
|
---|
8122 | ctxt->expr = expr;
|
---|
8123 | ctxt->cur = expr;
|
---|
8124 |
|
---|
8125 | ret = xmlExpParseExpr(ctxt);
|
---|
8126 | SKIP_BLANKS
|
---|
8127 | if (*ctxt->cur != 0) {
|
---|
8128 | xmlExpFree(ctxt, ret);
|
---|
8129 | return(NULL);
|
---|
8130 | }
|
---|
8131 | return(ret);
|
---|
8132 | }
|
---|
8133 |
|
---|
8134 | static void
|
---|
8135 | xmlExpDumpInt(xmlBufferPtr buf, xmlExpNodePtr expr, int glob) {
|
---|
8136 | xmlExpNodePtr c;
|
---|
8137 |
|
---|
8138 | if (expr == NULL) return;
|
---|
8139 | if (glob) xmlBufferWriteChar(buf, "(");
|
---|
8140 | switch (expr->type) {
|
---|
8141 | case XML_EXP_EMPTY:
|
---|
8142 | xmlBufferWriteChar(buf, "empty");
|
---|
8143 | break;
|
---|
8144 | case XML_EXP_FORBID:
|
---|
8145 | xmlBufferWriteChar(buf, "forbidden");
|
---|
8146 | break;
|
---|
8147 | case XML_EXP_ATOM:
|
---|
8148 | xmlBufferWriteCHAR(buf, expr->exp_str);
|
---|
8149 | break;
|
---|
8150 | case XML_EXP_SEQ:
|
---|
8151 | c = expr->exp_left;
|
---|
8152 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
|
---|
8153 | xmlExpDumpInt(buf, c, 1);
|
---|
8154 | else
|
---|
8155 | xmlExpDumpInt(buf, c, 0);
|
---|
8156 | xmlBufferWriteChar(buf, " , ");
|
---|
8157 | c = expr->exp_right;
|
---|
8158 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
|
---|
8159 | xmlExpDumpInt(buf, c, 1);
|
---|
8160 | else
|
---|
8161 | xmlExpDumpInt(buf, c, 0);
|
---|
8162 | break;
|
---|
8163 | case XML_EXP_OR:
|
---|
8164 | c = expr->exp_left;
|
---|
8165 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
|
---|
8166 | xmlExpDumpInt(buf, c, 1);
|
---|
8167 | else
|
---|
8168 | xmlExpDumpInt(buf, c, 0);
|
---|
8169 | xmlBufferWriteChar(buf, " | ");
|
---|
8170 | c = expr->exp_right;
|
---|
8171 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
|
---|
8172 | xmlExpDumpInt(buf, c, 1);
|
---|
8173 | else
|
---|
8174 | xmlExpDumpInt(buf, c, 0);
|
---|
8175 | break;
|
---|
8176 | case XML_EXP_COUNT: {
|
---|
8177 | char rep[40];
|
---|
8178 |
|
---|
8179 | c = expr->exp_left;
|
---|
8180 | if ((c->type == XML_EXP_SEQ) || (c->type == XML_EXP_OR))
|
---|
8181 | xmlExpDumpInt(buf, c, 1);
|
---|
8182 | else
|
---|
8183 | xmlExpDumpInt(buf, c, 0);
|
---|
8184 | if ((expr->exp_min == 0) && (expr->exp_max == 1)) {
|
---|
8185 | rep[0] = '?';
|
---|
8186 | rep[1] = 0;
|
---|
8187 | } else if ((expr->exp_min == 0) && (expr->exp_max == -1)) {
|
---|
8188 | rep[0] = '*';
|
---|
8189 | rep[1] = 0;
|
---|
8190 | } else if ((expr->exp_min == 1) && (expr->exp_max == -1)) {
|
---|
8191 | rep[0] = '+';
|
---|
8192 | rep[1] = 0;
|
---|
8193 | } else if (expr->exp_max == expr->exp_min) {
|
---|
8194 | snprintf(rep, 39, "{%d}", expr->exp_min);
|
---|
8195 | } else if (expr->exp_max < 0) {
|
---|
8196 | snprintf(rep, 39, "{%d,inf}", expr->exp_min);
|
---|
8197 | } else {
|
---|
8198 | snprintf(rep, 39, "{%d,%d}", expr->exp_min, expr->exp_max);
|
---|
8199 | }
|
---|
8200 | rep[39] = 0;
|
---|
8201 | xmlBufferWriteChar(buf, rep);
|
---|
8202 | break;
|
---|
8203 | }
|
---|
8204 | default:
|
---|
8205 | fprintf(stderr, "Error in tree\n");
|
---|
8206 | }
|
---|
8207 | if (glob)
|
---|
8208 | xmlBufferWriteChar(buf, ")");
|
---|
8209 | }
|
---|
8210 | /**
|
---|
8211 | * xmlExpDump:
|
---|
8212 | * @buf: a buffer to receive the output
|
---|
8213 | * @expr: the compiled expression
|
---|
8214 | *
|
---|
8215 | * Serialize the expression as compiled to the buffer
|
---|
8216 | */
|
---|
8217 | void
|
---|
8218 | xmlExpDump(xmlBufferPtr buf, xmlExpNodePtr expr) {
|
---|
8219 | if ((buf == NULL) || (expr == NULL))
|
---|
8220 | return;
|
---|
8221 | xmlExpDumpInt(buf, expr, 0);
|
---|
8222 | }
|
---|
8223 |
|
---|
8224 | /**
|
---|
8225 | * xmlExpMaxToken:
|
---|
8226 | * @expr: a compiled expression
|
---|
8227 | *
|
---|
8228 | * Indicate the maximum number of input a expression can accept
|
---|
8229 | *
|
---|
8230 | * Returns the maximum length or -1 in case of error
|
---|
8231 | */
|
---|
8232 | int
|
---|
8233 | xmlExpMaxToken(xmlExpNodePtr expr) {
|
---|
8234 | if (expr == NULL)
|
---|
8235 | return(-1);
|
---|
8236 | return(expr->c_max);
|
---|
8237 | }
|
---|
8238 |
|
---|
8239 | /**
|
---|
8240 | * xmlExpCtxtNbNodes:
|
---|
8241 | * @ctxt: an expression context
|
---|
8242 | *
|
---|
8243 | * Debugging facility provides the number of allocated nodes at a that point
|
---|
8244 | *
|
---|
8245 | * Returns the number of nodes in use or -1 in case of error
|
---|
8246 | */
|
---|
8247 | int
|
---|
8248 | xmlExpCtxtNbNodes(xmlExpCtxtPtr ctxt) {
|
---|
8249 | if (ctxt == NULL)
|
---|
8250 | return(-1);
|
---|
8251 | return(ctxt->nb_nodes);
|
---|
8252 | }
|
---|
8253 |
|
---|
8254 | /**
|
---|
8255 | * xmlExpCtxtNbCons:
|
---|
8256 | * @ctxt: an expression context
|
---|
8257 | *
|
---|
8258 | * Debugging facility provides the number of allocated nodes over lifetime
|
---|
8259 | *
|
---|
8260 | * Returns the number of nodes ever allocated or -1 in case of error
|
---|
8261 | */
|
---|
8262 | int
|
---|
8263 | xmlExpCtxtNbCons(xmlExpCtxtPtr ctxt) {
|
---|
8264 | if (ctxt == NULL)
|
---|
8265 | return(-1);
|
---|
8266 | return(ctxt->nb_cons);
|
---|
8267 | }
|
---|
8268 |
|
---|
8269 | #endif /* LIBXML_EXPR_ENABLED */
|
---|
8270 | #define bottom_xmlregexp
|
---|
8271 | #include "elfgcchack.h"
|
---|
8272 | #endif /* LIBXML_REGEXP_ENABLED */
|
---|