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