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 | 99c394d | 2005-07-14 12:58:49 +0000 | [diff] [blame^] | 1470 | } else if ((atom->min == 0) && (atom->max == 0) && |
| 1471 | (atom->quant == XML_REGEXP_QUANT_RANGE)) { |
| 1472 | /* |
| 1473 | * we can discard the atom and generate an epsilon transition instead |
| 1474 | */ |
| 1475 | if (to == NULL) { |
| 1476 | to = xmlRegNewState(ctxt); |
| 1477 | if (to != NULL) |
| 1478 | xmlRegStatePush(ctxt, to); |
| 1479 | else { |
| 1480 | return(-1); |
| 1481 | } |
| 1482 | } |
| 1483 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
| 1484 | ctxt->state = to; |
| 1485 | xmlRegFreeAtom(atom); |
| 1486 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1487 | } else { |
| 1488 | if (to == NULL) { |
| 1489 | to = xmlRegNewState(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1490 | if (to != NULL) |
| 1491 | xmlRegStatePush(ctxt, to); |
| 1492 | else { |
| 1493 | return(-1); |
| 1494 | } |
| 1495 | } |
| 1496 | if (xmlRegAtomPush(ctxt, atom) < 0) { |
| 1497 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1498 | } |
| 1499 | xmlRegStateAddTrans(ctxt, from, atom, to, -1, -1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1500 | ctxt->state = to; |
| 1501 | } |
| 1502 | switch (atom->quant) { |
| 1503 | case XML_REGEXP_QUANT_OPT: |
| 1504 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1505 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
| 1506 | break; |
| 1507 | case XML_REGEXP_QUANT_MULT: |
| 1508 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1509 | xmlFAGenerateEpsilonTransition(ctxt, from, to); |
| 1510 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
| 1511 | break; |
| 1512 | case XML_REGEXP_QUANT_PLUS: |
| 1513 | atom->quant = XML_REGEXP_QUANT_ONCE; |
| 1514 | xmlRegStateAddTrans(ctxt, to, atom, to, -1, -1); |
| 1515 | break; |
| 1516 | default: |
| 1517 | break; |
| 1518 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1519 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | /** |
| 1523 | * xmlFAReduceEpsilonTransitions: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1524 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1525 | * @fromnr: the from state |
| 1526 | * @tonr: the to state |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1527 | * @counter: should that transition be associated to a counted |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1528 | * |
| 1529 | */ |
| 1530 | static void |
| 1531 | xmlFAReduceEpsilonTransitions(xmlRegParserCtxtPtr ctxt, int fromnr, |
| 1532 | int tonr, int counter) { |
| 1533 | int transnr; |
| 1534 | xmlRegStatePtr from; |
| 1535 | xmlRegStatePtr to; |
| 1536 | |
| 1537 | #ifdef DEBUG_REGEXP_GRAPH |
| 1538 | printf("xmlFAReduceEpsilonTransitions(%d, %d)\n", fromnr, tonr); |
| 1539 | #endif |
| 1540 | from = ctxt->states[fromnr]; |
| 1541 | if (from == NULL) |
| 1542 | return; |
| 1543 | to = ctxt->states[tonr]; |
| 1544 | if (to == NULL) |
| 1545 | return; |
| 1546 | if ((to->mark == XML_REGEXP_MARK_START) || |
| 1547 | (to->mark == XML_REGEXP_MARK_VISITED)) |
| 1548 | return; |
| 1549 | |
| 1550 | to->mark = XML_REGEXP_MARK_VISITED; |
| 1551 | if (to->type == XML_REGEXP_FINAL_STATE) { |
| 1552 | #ifdef DEBUG_REGEXP_GRAPH |
| 1553 | printf("State %d is final, so %d becomes final\n", tonr, fromnr); |
| 1554 | #endif |
| 1555 | from->type = XML_REGEXP_FINAL_STATE; |
| 1556 | } |
| 1557 | for (transnr = 0;transnr < to->nbTrans;transnr++) { |
| 1558 | if (to->trans[transnr].atom == NULL) { |
| 1559 | /* |
| 1560 | * Don't remove counted transitions |
| 1561 | * Don't loop either |
| 1562 | */ |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1563 | if (to->trans[transnr].to != fromnr) { |
| 1564 | if (to->trans[transnr].count >= 0) { |
| 1565 | int newto = to->trans[transnr].to; |
| 1566 | |
| 1567 | xmlRegStateAddTrans(ctxt, from, NULL, |
| 1568 | ctxt->states[newto], |
| 1569 | -1, to->trans[transnr].count); |
| 1570 | } else { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1571 | #ifdef DEBUG_REGEXP_GRAPH |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1572 | printf("Found epsilon trans %d from %d to %d\n", |
| 1573 | transnr, tonr, to->trans[transnr].to); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1574 | #endif |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1575 | if (to->trans[transnr].counter >= 0) { |
| 1576 | xmlFAReduceEpsilonTransitions(ctxt, fromnr, |
| 1577 | to->trans[transnr].to, |
| 1578 | to->trans[transnr].counter); |
| 1579 | } else { |
| 1580 | xmlFAReduceEpsilonTransitions(ctxt, fromnr, |
| 1581 | to->trans[transnr].to, |
| 1582 | counter); |
| 1583 | } |
| 1584 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1585 | } |
| 1586 | } else { |
| 1587 | int newto = to->trans[transnr].to; |
| 1588 | |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 1589 | if (to->trans[transnr].counter >= 0) { |
| 1590 | xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom, |
| 1591 | ctxt->states[newto], |
| 1592 | to->trans[transnr].counter, -1); |
| 1593 | } else { |
| 1594 | xmlRegStateAddTrans(ctxt, from, to->trans[transnr].atom, |
| 1595 | ctxt->states[newto], counter, -1); |
| 1596 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1597 | } |
| 1598 | } |
| 1599 | to->mark = XML_REGEXP_MARK_NORMAL; |
| 1600 | } |
| 1601 | |
| 1602 | /** |
| 1603 | * xmlFAEliminateEpsilonTransitions: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 1604 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1605 | * |
| 1606 | */ |
| 1607 | static void |
| 1608 | xmlFAEliminateEpsilonTransitions(xmlRegParserCtxtPtr ctxt) { |
| 1609 | int statenr, transnr; |
| 1610 | xmlRegStatePtr state; |
| 1611 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 1612 | if (ctxt->states == NULL) return; |
| 1613 | |
| 1614 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1615 | /* |
| 1616 | * build the completed transitions bypassing the epsilons |
| 1617 | * Use a marking algorithm to avoid loops |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1618 | * mark sink states too. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1619 | */ |
| 1620 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1621 | state = ctxt->states[statenr]; |
| 1622 | if (state == NULL) |
| 1623 | continue; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1624 | if ((state->nbTrans == 0) && |
| 1625 | (state->type != XML_REGEXP_FINAL_STATE)) { |
| 1626 | state->type = XML_REGEXP_SINK_STATE; |
| 1627 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1628 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1629 | if ((state->trans[transnr].atom == NULL) && |
| 1630 | (state->trans[transnr].to >= 0)) { |
| 1631 | if (state->trans[transnr].to == statenr) { |
| 1632 | state->trans[transnr].to = -1; |
| 1633 | #ifdef DEBUG_REGEXP_GRAPH |
| 1634 | printf("Removed loopback epsilon trans %d on %d\n", |
| 1635 | transnr, statenr); |
| 1636 | #endif |
| 1637 | } else if (state->trans[transnr].count < 0) { |
| 1638 | int newto = state->trans[transnr].to; |
| 1639 | |
| 1640 | #ifdef DEBUG_REGEXP_GRAPH |
| 1641 | printf("Found epsilon trans %d from %d to %d\n", |
| 1642 | transnr, statenr, newto); |
| 1643 | #endif |
| 1644 | state->mark = XML_REGEXP_MARK_START; |
| 1645 | xmlFAReduceEpsilonTransitions(ctxt, statenr, |
| 1646 | newto, state->trans[transnr].counter); |
| 1647 | state->mark = XML_REGEXP_MARK_NORMAL; |
| 1648 | #ifdef DEBUG_REGEXP_GRAPH |
| 1649 | } else { |
| 1650 | printf("Found counted transition %d on %d\n", |
| 1651 | transnr, statenr); |
| 1652 | #endif |
| 1653 | } |
| 1654 | } |
| 1655 | } |
| 1656 | } |
| 1657 | /* |
| 1658 | * Eliminate the epsilon transitions |
| 1659 | */ |
| 1660 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1661 | state = ctxt->states[statenr]; |
| 1662 | if (state == NULL) |
| 1663 | continue; |
| 1664 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1665 | if ((state->trans[transnr].atom == NULL) && |
| 1666 | (state->trans[transnr].count < 0) && |
| 1667 | (state->trans[transnr].to >= 0)) { |
| 1668 | state->trans[transnr].to = -1; |
| 1669 | } |
| 1670 | } |
| 1671 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1672 | |
| 1673 | /* |
| 1674 | * Use this pass to detect unreachable states too |
| 1675 | */ |
| 1676 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1677 | state = ctxt->states[statenr]; |
| 1678 | if (state != NULL) |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1679 | state->reached = XML_REGEXP_MARK_NORMAL; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1680 | } |
| 1681 | state = ctxt->states[0]; |
| 1682 | if (state != NULL) |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1683 | state->reached = XML_REGEXP_MARK_START; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1684 | while (state != NULL) { |
| 1685 | xmlRegStatePtr target = NULL; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1686 | state->reached = XML_REGEXP_MARK_VISITED; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1687 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1688 | * Mark all states reachable from the current reachable state |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1689 | */ |
| 1690 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1691 | if ((state->trans[transnr].to >= 0) && |
| 1692 | ((state->trans[transnr].atom != NULL) || |
| 1693 | (state->trans[transnr].count >= 0))) { |
| 1694 | int newto = state->trans[transnr].to; |
| 1695 | |
| 1696 | if (ctxt->states[newto] == NULL) |
| 1697 | continue; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1698 | if (ctxt->states[newto]->reached == XML_REGEXP_MARK_NORMAL) { |
| 1699 | ctxt->states[newto]->reached = XML_REGEXP_MARK_START; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1700 | target = ctxt->states[newto]; |
| 1701 | } |
| 1702 | } |
| 1703 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 1704 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1705 | /* |
| 1706 | * find the next accessible state not explored |
| 1707 | */ |
| 1708 | if (target == NULL) { |
| 1709 | for (statenr = 1;statenr < ctxt->nbStates;statenr++) { |
| 1710 | state = ctxt->states[statenr]; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1711 | if ((state != NULL) && (state->reached == |
| 1712 | XML_REGEXP_MARK_START)) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1713 | target = state; |
| 1714 | break; |
| 1715 | } |
| 1716 | } |
| 1717 | } |
| 1718 | state = target; |
| 1719 | } |
| 1720 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1721 | state = ctxt->states[statenr]; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 1722 | if ((state != NULL) && (state->reached == XML_REGEXP_MARK_NORMAL)) { |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 1723 | #ifdef DEBUG_REGEXP_GRAPH |
| 1724 | printf("Removed unreachable state %d\n", statenr); |
| 1725 | #endif |
| 1726 | xmlRegFreeState(state); |
| 1727 | ctxt->states[statenr] = NULL; |
| 1728 | } |
| 1729 | } |
| 1730 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 1733 | /** |
| 1734 | * xmlFACompareAtoms: |
| 1735 | * @atom1: an atom |
| 1736 | * @atom2: an atom |
| 1737 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1738 | * Compares two atoms to check whether they are equivalents |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 1739 | * |
| 1740 | * Returns 1 if yes and 0 otherwise |
| 1741 | */ |
| 1742 | static int |
| 1743 | xmlFACompareAtoms(xmlRegAtomPtr atom1, xmlRegAtomPtr atom2) { |
| 1744 | if (atom1 == atom2) |
| 1745 | return(1); |
| 1746 | if ((atom1 == NULL) || (atom2 == NULL)) |
| 1747 | return(0); |
| 1748 | |
| 1749 | if (atom1->type != atom2->type) |
| 1750 | return(0); |
| 1751 | switch (atom1->type) { |
| 1752 | case XML_REGEXP_STRING: |
| 1753 | return(xmlStrEqual((xmlChar *)atom1->valuep, |
| 1754 | (xmlChar *)atom2->valuep)); |
| 1755 | case XML_REGEXP_EPSILON: |
| 1756 | return(1); |
| 1757 | case XML_REGEXP_CHARVAL: |
| 1758 | return(atom1->codepoint == atom2->codepoint); |
| 1759 | case XML_REGEXP_RANGES: |
| 1760 | TODO; |
| 1761 | return(0); |
| 1762 | default: |
| 1763 | break; |
| 1764 | } |
| 1765 | return(1); |
| 1766 | } |
| 1767 | |
| 1768 | /** |
| 1769 | * xmlFARecurseDeterminism: |
| 1770 | * @ctxt: a regexp parser context |
| 1771 | * |
| 1772 | * Check whether the associated regexp is determinist, |
| 1773 | * should be called after xmlFAEliminateEpsilonTransitions() |
| 1774 | * |
| 1775 | */ |
| 1776 | static int |
| 1777 | xmlFARecurseDeterminism(xmlRegParserCtxtPtr ctxt, xmlRegStatePtr state, |
| 1778 | int to, xmlRegAtomPtr atom) { |
| 1779 | int ret = 1; |
| 1780 | int transnr; |
| 1781 | xmlRegTransPtr t1; |
| 1782 | |
| 1783 | if (state == NULL) |
| 1784 | return(ret); |
| 1785 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1786 | t1 = &(state->trans[transnr]); |
| 1787 | /* |
| 1788 | * check transitions conflicting with the one looked at |
| 1789 | */ |
| 1790 | if (t1->atom == NULL) { |
| 1791 | if (t1->to == -1) |
| 1792 | continue; |
| 1793 | ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
| 1794 | to, atom); |
| 1795 | if (ret == 0) |
| 1796 | return(0); |
| 1797 | continue; |
| 1798 | } |
| 1799 | if (t1->to != to) |
| 1800 | continue; |
| 1801 | if (xmlFACompareAtoms(t1->atom, atom)) |
| 1802 | return(0); |
| 1803 | } |
| 1804 | return(ret); |
| 1805 | } |
| 1806 | |
| 1807 | /** |
| 1808 | * xmlFAComputesDeterminism: |
| 1809 | * @ctxt: a regexp parser context |
| 1810 | * |
| 1811 | * Check whether the associated regexp is determinist, |
| 1812 | * should be called after xmlFAEliminateEpsilonTransitions() |
| 1813 | * |
| 1814 | */ |
| 1815 | static int |
| 1816 | xmlFAComputesDeterminism(xmlRegParserCtxtPtr ctxt) { |
| 1817 | int statenr, transnr; |
| 1818 | xmlRegStatePtr state; |
| 1819 | xmlRegTransPtr t1, t2; |
| 1820 | int i; |
| 1821 | int ret = 1; |
| 1822 | |
Daniel Veillard | 4402ab4 | 2002-09-12 16:02:56 +0000 | [diff] [blame] | 1823 | #ifdef DEBUG_REGEXP_GRAPH |
| 1824 | printf("xmlFAComputesDeterminism\n"); |
| 1825 | xmlRegPrintCtxt(stdout, ctxt); |
| 1826 | #endif |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 1827 | if (ctxt->determinist != -1) |
| 1828 | return(ctxt->determinist); |
| 1829 | |
| 1830 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1831 | * Check for all states that there aren't 2 transitions |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 1832 | * with the same atom and a different target. |
| 1833 | */ |
| 1834 | for (statenr = 0;statenr < ctxt->nbStates;statenr++) { |
| 1835 | state = ctxt->states[statenr]; |
| 1836 | if (state == NULL) |
| 1837 | continue; |
| 1838 | for (transnr = 0;transnr < state->nbTrans;transnr++) { |
| 1839 | t1 = &(state->trans[transnr]); |
| 1840 | /* |
| 1841 | * Determinism checks in case of counted or all transitions |
| 1842 | * will have to be handled separately |
| 1843 | */ |
| 1844 | if (t1->atom == NULL) |
| 1845 | continue; |
| 1846 | if (t1->to == -1) /* eliminated */ |
| 1847 | continue; |
| 1848 | for (i = 0;i < transnr;i++) { |
| 1849 | t2 = &(state->trans[i]); |
| 1850 | if (t2->to == -1) /* eliminated */ |
| 1851 | continue; |
| 1852 | if (t2->atom != NULL) { |
| 1853 | if (t1->to == t2->to) { |
| 1854 | if (xmlFACompareAtoms(t1->atom, t2->atom)) |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 1855 | t2->to = -1; /* eliminated */ |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 1856 | } else { |
| 1857 | /* not determinist ! */ |
| 1858 | if (xmlFACompareAtoms(t1->atom, t2->atom)) |
| 1859 | ret = 0; |
| 1860 | } |
| 1861 | } else if (t1->to != -1) { |
| 1862 | /* |
| 1863 | * do the closure in case of remaining specific |
| 1864 | * epsilon transitions like choices or all |
| 1865 | */ |
| 1866 | ret = xmlFARecurseDeterminism(ctxt, ctxt->states[t1->to], |
| 1867 | t2->to, t2->atom); |
| 1868 | if (ret == 0) |
| 1869 | return(0); |
| 1870 | } |
| 1871 | } |
| 1872 | if (ret == 0) |
| 1873 | break; |
| 1874 | } |
| 1875 | if (ret == 0) |
| 1876 | break; |
| 1877 | } |
| 1878 | ctxt->determinist = ret; |
| 1879 | return(ret); |
| 1880 | } |
| 1881 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1882 | /************************************************************************ |
| 1883 | * * |
| 1884 | * Routines to check input against transition atoms * |
| 1885 | * * |
| 1886 | ************************************************************************/ |
| 1887 | |
| 1888 | static int |
| 1889 | xmlRegCheckCharacterRange(xmlRegAtomType type, int codepoint, int neg, |
| 1890 | int start, int end, const xmlChar *blockName) { |
| 1891 | int ret = 0; |
| 1892 | |
| 1893 | switch (type) { |
| 1894 | case XML_REGEXP_STRING: |
| 1895 | case XML_REGEXP_SUBREG: |
| 1896 | case XML_REGEXP_RANGES: |
| 1897 | case XML_REGEXP_EPSILON: |
| 1898 | return(-1); |
| 1899 | case XML_REGEXP_ANYCHAR: |
| 1900 | ret = ((codepoint != '\n') && (codepoint != '\r')); |
| 1901 | break; |
| 1902 | case XML_REGEXP_CHARVAL: |
| 1903 | ret = ((codepoint >= start) && (codepoint <= end)); |
| 1904 | break; |
| 1905 | case XML_REGEXP_NOTSPACE: |
| 1906 | neg = !neg; |
| 1907 | case XML_REGEXP_ANYSPACE: |
| 1908 | ret = ((codepoint == '\n') || (codepoint == '\r') || |
| 1909 | (codepoint == '\t') || (codepoint == ' ')); |
| 1910 | break; |
| 1911 | case XML_REGEXP_NOTINITNAME: |
| 1912 | neg = !neg; |
| 1913 | case XML_REGEXP_INITNAME: |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 1914 | ret = (IS_LETTER(codepoint) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1915 | (codepoint == '_') || (codepoint == ':')); |
| 1916 | break; |
| 1917 | case XML_REGEXP_NOTNAMECHAR: |
| 1918 | neg = !neg; |
| 1919 | case XML_REGEXP_NAMECHAR: |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 1920 | ret = (IS_LETTER(codepoint) || IS_DIGIT(codepoint) || |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1921 | (codepoint == '.') || (codepoint == '-') || |
| 1922 | (codepoint == '_') || (codepoint == ':') || |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 1923 | IS_COMBINING(codepoint) || IS_EXTENDER(codepoint)); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 1924 | break; |
| 1925 | case XML_REGEXP_NOTDECIMAL: |
| 1926 | neg = !neg; |
| 1927 | case XML_REGEXP_DECIMAL: |
| 1928 | ret = xmlUCSIsCatNd(codepoint); |
| 1929 | break; |
| 1930 | case XML_REGEXP_REALCHAR: |
| 1931 | neg = !neg; |
| 1932 | case XML_REGEXP_NOTREALCHAR: |
| 1933 | ret = xmlUCSIsCatP(codepoint); |
| 1934 | if (ret == 0) |
| 1935 | ret = xmlUCSIsCatZ(codepoint); |
| 1936 | if (ret == 0) |
| 1937 | ret = xmlUCSIsCatC(codepoint); |
| 1938 | break; |
| 1939 | case XML_REGEXP_LETTER: |
| 1940 | ret = xmlUCSIsCatL(codepoint); |
| 1941 | break; |
| 1942 | case XML_REGEXP_LETTER_UPPERCASE: |
| 1943 | ret = xmlUCSIsCatLu(codepoint); |
| 1944 | break; |
| 1945 | case XML_REGEXP_LETTER_LOWERCASE: |
| 1946 | ret = xmlUCSIsCatLl(codepoint); |
| 1947 | break; |
| 1948 | case XML_REGEXP_LETTER_TITLECASE: |
| 1949 | ret = xmlUCSIsCatLt(codepoint); |
| 1950 | break; |
| 1951 | case XML_REGEXP_LETTER_MODIFIER: |
| 1952 | ret = xmlUCSIsCatLm(codepoint); |
| 1953 | break; |
| 1954 | case XML_REGEXP_LETTER_OTHERS: |
| 1955 | ret = xmlUCSIsCatLo(codepoint); |
| 1956 | break; |
| 1957 | case XML_REGEXP_MARK: |
| 1958 | ret = xmlUCSIsCatM(codepoint); |
| 1959 | break; |
| 1960 | case XML_REGEXP_MARK_NONSPACING: |
| 1961 | ret = xmlUCSIsCatMn(codepoint); |
| 1962 | break; |
| 1963 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 1964 | ret = xmlUCSIsCatMc(codepoint); |
| 1965 | break; |
| 1966 | case XML_REGEXP_MARK_ENCLOSING: |
| 1967 | ret = xmlUCSIsCatMe(codepoint); |
| 1968 | break; |
| 1969 | case XML_REGEXP_NUMBER: |
| 1970 | ret = xmlUCSIsCatN(codepoint); |
| 1971 | break; |
| 1972 | case XML_REGEXP_NUMBER_DECIMAL: |
| 1973 | ret = xmlUCSIsCatNd(codepoint); |
| 1974 | break; |
| 1975 | case XML_REGEXP_NUMBER_LETTER: |
| 1976 | ret = xmlUCSIsCatNl(codepoint); |
| 1977 | break; |
| 1978 | case XML_REGEXP_NUMBER_OTHERS: |
| 1979 | ret = xmlUCSIsCatNo(codepoint); |
| 1980 | break; |
| 1981 | case XML_REGEXP_PUNCT: |
| 1982 | ret = xmlUCSIsCatP(codepoint); |
| 1983 | break; |
| 1984 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 1985 | ret = xmlUCSIsCatPc(codepoint); |
| 1986 | break; |
| 1987 | case XML_REGEXP_PUNCT_DASH: |
| 1988 | ret = xmlUCSIsCatPd(codepoint); |
| 1989 | break; |
| 1990 | case XML_REGEXP_PUNCT_OPEN: |
| 1991 | ret = xmlUCSIsCatPs(codepoint); |
| 1992 | break; |
| 1993 | case XML_REGEXP_PUNCT_CLOSE: |
| 1994 | ret = xmlUCSIsCatPe(codepoint); |
| 1995 | break; |
| 1996 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 1997 | ret = xmlUCSIsCatPi(codepoint); |
| 1998 | break; |
| 1999 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2000 | ret = xmlUCSIsCatPf(codepoint); |
| 2001 | break; |
| 2002 | case XML_REGEXP_PUNCT_OTHERS: |
| 2003 | ret = xmlUCSIsCatPo(codepoint); |
| 2004 | break; |
| 2005 | case XML_REGEXP_SEPAR: |
| 2006 | ret = xmlUCSIsCatZ(codepoint); |
| 2007 | break; |
| 2008 | case XML_REGEXP_SEPAR_SPACE: |
| 2009 | ret = xmlUCSIsCatZs(codepoint); |
| 2010 | break; |
| 2011 | case XML_REGEXP_SEPAR_LINE: |
| 2012 | ret = xmlUCSIsCatZl(codepoint); |
| 2013 | break; |
| 2014 | case XML_REGEXP_SEPAR_PARA: |
| 2015 | ret = xmlUCSIsCatZp(codepoint); |
| 2016 | break; |
| 2017 | case XML_REGEXP_SYMBOL: |
| 2018 | ret = xmlUCSIsCatS(codepoint); |
| 2019 | break; |
| 2020 | case XML_REGEXP_SYMBOL_MATH: |
| 2021 | ret = xmlUCSIsCatSm(codepoint); |
| 2022 | break; |
| 2023 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2024 | ret = xmlUCSIsCatSc(codepoint); |
| 2025 | break; |
| 2026 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2027 | ret = xmlUCSIsCatSk(codepoint); |
| 2028 | break; |
| 2029 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2030 | ret = xmlUCSIsCatSo(codepoint); |
| 2031 | break; |
| 2032 | case XML_REGEXP_OTHER: |
| 2033 | ret = xmlUCSIsCatC(codepoint); |
| 2034 | break; |
| 2035 | case XML_REGEXP_OTHER_CONTROL: |
| 2036 | ret = xmlUCSIsCatCc(codepoint); |
| 2037 | break; |
| 2038 | case XML_REGEXP_OTHER_FORMAT: |
| 2039 | ret = xmlUCSIsCatCf(codepoint); |
| 2040 | break; |
| 2041 | case XML_REGEXP_OTHER_PRIVATE: |
| 2042 | ret = xmlUCSIsCatCo(codepoint); |
| 2043 | break; |
| 2044 | case XML_REGEXP_OTHER_NA: |
| 2045 | /* ret = xmlUCSIsCatCn(codepoint); */ |
| 2046 | /* Seems it doesn't exist anymore in recent Unicode releases */ |
| 2047 | ret = 0; |
| 2048 | break; |
| 2049 | case XML_REGEXP_BLOCK_NAME: |
| 2050 | ret = xmlUCSIsBlock(codepoint, (const char *) blockName); |
| 2051 | break; |
| 2052 | } |
| 2053 | if (neg) |
| 2054 | return(!ret); |
| 2055 | return(ret); |
| 2056 | } |
| 2057 | |
| 2058 | static int |
| 2059 | xmlRegCheckCharacter(xmlRegAtomPtr atom, int codepoint) { |
| 2060 | int i, ret = 0; |
| 2061 | xmlRegRangePtr range; |
| 2062 | |
William M. Brack | 871611b | 2003-10-18 04:53:14 +0000 | [diff] [blame] | 2063 | if ((atom == NULL) || (!IS_CHAR(codepoint))) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2064 | return(-1); |
| 2065 | |
| 2066 | switch (atom->type) { |
| 2067 | case XML_REGEXP_SUBREG: |
| 2068 | case XML_REGEXP_EPSILON: |
| 2069 | return(-1); |
| 2070 | case XML_REGEXP_CHARVAL: |
| 2071 | return(codepoint == atom->codepoint); |
| 2072 | case XML_REGEXP_RANGES: { |
| 2073 | int accept = 0; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 2074 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2075 | for (i = 0;i < atom->nbRanges;i++) { |
| 2076 | range = atom->ranges[i]; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2077 | if (range->neg == 2) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2078 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2079 | 0, range->start, range->end, |
| 2080 | range->blockName); |
| 2081 | if (ret != 0) |
| 2082 | return(0); /* excluded char */ |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2083 | } else if (range->neg) { |
| 2084 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2085 | 0, range->start, range->end, |
| 2086 | range->blockName); |
| 2087 | if (ret == 0) |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 2088 | accept = 1; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 2089 | else |
| 2090 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2091 | } else { |
| 2092 | ret = xmlRegCheckCharacterRange(range->type, codepoint, |
| 2093 | 0, range->start, range->end, |
| 2094 | range->blockName); |
| 2095 | if (ret != 0) |
| 2096 | accept = 1; /* might still be excluded */ |
| 2097 | } |
| 2098 | } |
| 2099 | return(accept); |
| 2100 | } |
| 2101 | case XML_REGEXP_STRING: |
| 2102 | printf("TODO: XML_REGEXP_STRING\n"); |
| 2103 | return(-1); |
| 2104 | case XML_REGEXP_ANYCHAR: |
| 2105 | case XML_REGEXP_ANYSPACE: |
| 2106 | case XML_REGEXP_NOTSPACE: |
| 2107 | case XML_REGEXP_INITNAME: |
| 2108 | case XML_REGEXP_NOTINITNAME: |
| 2109 | case XML_REGEXP_NAMECHAR: |
| 2110 | case XML_REGEXP_NOTNAMECHAR: |
| 2111 | case XML_REGEXP_DECIMAL: |
| 2112 | case XML_REGEXP_NOTDECIMAL: |
| 2113 | case XML_REGEXP_REALCHAR: |
| 2114 | case XML_REGEXP_NOTREALCHAR: |
| 2115 | case XML_REGEXP_LETTER: |
| 2116 | case XML_REGEXP_LETTER_UPPERCASE: |
| 2117 | case XML_REGEXP_LETTER_LOWERCASE: |
| 2118 | case XML_REGEXP_LETTER_TITLECASE: |
| 2119 | case XML_REGEXP_LETTER_MODIFIER: |
| 2120 | case XML_REGEXP_LETTER_OTHERS: |
| 2121 | case XML_REGEXP_MARK: |
| 2122 | case XML_REGEXP_MARK_NONSPACING: |
| 2123 | case XML_REGEXP_MARK_SPACECOMBINING: |
| 2124 | case XML_REGEXP_MARK_ENCLOSING: |
| 2125 | case XML_REGEXP_NUMBER: |
| 2126 | case XML_REGEXP_NUMBER_DECIMAL: |
| 2127 | case XML_REGEXP_NUMBER_LETTER: |
| 2128 | case XML_REGEXP_NUMBER_OTHERS: |
| 2129 | case XML_REGEXP_PUNCT: |
| 2130 | case XML_REGEXP_PUNCT_CONNECTOR: |
| 2131 | case XML_REGEXP_PUNCT_DASH: |
| 2132 | case XML_REGEXP_PUNCT_OPEN: |
| 2133 | case XML_REGEXP_PUNCT_CLOSE: |
| 2134 | case XML_REGEXP_PUNCT_INITQUOTE: |
| 2135 | case XML_REGEXP_PUNCT_FINQUOTE: |
| 2136 | case XML_REGEXP_PUNCT_OTHERS: |
| 2137 | case XML_REGEXP_SEPAR: |
| 2138 | case XML_REGEXP_SEPAR_SPACE: |
| 2139 | case XML_REGEXP_SEPAR_LINE: |
| 2140 | case XML_REGEXP_SEPAR_PARA: |
| 2141 | case XML_REGEXP_SYMBOL: |
| 2142 | case XML_REGEXP_SYMBOL_MATH: |
| 2143 | case XML_REGEXP_SYMBOL_CURRENCY: |
| 2144 | case XML_REGEXP_SYMBOL_MODIFIER: |
| 2145 | case XML_REGEXP_SYMBOL_OTHERS: |
| 2146 | case XML_REGEXP_OTHER: |
| 2147 | case XML_REGEXP_OTHER_CONTROL: |
| 2148 | case XML_REGEXP_OTHER_FORMAT: |
| 2149 | case XML_REGEXP_OTHER_PRIVATE: |
| 2150 | case XML_REGEXP_OTHER_NA: |
| 2151 | case XML_REGEXP_BLOCK_NAME: |
| 2152 | ret = xmlRegCheckCharacterRange(atom->type, codepoint, 0, 0, 0, |
| 2153 | (const xmlChar *)atom->valuep); |
| 2154 | if (atom->neg) |
| 2155 | ret = !ret; |
| 2156 | break; |
| 2157 | } |
| 2158 | return(ret); |
| 2159 | } |
| 2160 | |
| 2161 | /************************************************************************ |
| 2162 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2163 | * Saving and restoring state of an execution context * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2164 | * * |
| 2165 | ************************************************************************/ |
| 2166 | |
| 2167 | #ifdef DEBUG_REGEXP_EXEC |
| 2168 | static void |
| 2169 | xmlFARegDebugExec(xmlRegExecCtxtPtr exec) { |
| 2170 | printf("state: %d:%d:idx %d", exec->state->no, exec->transno, exec->index); |
| 2171 | if (exec->inputStack != NULL) { |
| 2172 | int i; |
| 2173 | printf(": "); |
| 2174 | for (i = 0;(i < 3) && (i < exec->inputStackNr);i++) |
| 2175 | printf("%s ", exec->inputStack[exec->inputStackNr - (i + 1)]); |
| 2176 | } else { |
| 2177 | printf(": %s", &(exec->inputString[exec->index])); |
| 2178 | } |
| 2179 | printf("\n"); |
| 2180 | } |
| 2181 | #endif |
| 2182 | |
| 2183 | static void |
| 2184 | xmlFARegExecSave(xmlRegExecCtxtPtr exec) { |
| 2185 | #ifdef DEBUG_REGEXP_EXEC |
| 2186 | printf("saving "); |
| 2187 | exec->transno++; |
| 2188 | xmlFARegDebugExec(exec); |
| 2189 | exec->transno--; |
| 2190 | #endif |
| 2191 | |
| 2192 | if (exec->maxRollbacks == 0) { |
| 2193 | exec->maxRollbacks = 4; |
| 2194 | exec->rollbacks = (xmlRegExecRollback *) xmlMalloc(exec->maxRollbacks * |
| 2195 | sizeof(xmlRegExecRollback)); |
| 2196 | if (exec->rollbacks == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2197 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2198 | exec->maxRollbacks = 0; |
| 2199 | return; |
| 2200 | } |
| 2201 | memset(exec->rollbacks, 0, |
| 2202 | exec->maxRollbacks * sizeof(xmlRegExecRollback)); |
| 2203 | } else if (exec->nbRollbacks >= exec->maxRollbacks) { |
| 2204 | xmlRegExecRollback *tmp; |
| 2205 | int len = exec->maxRollbacks; |
| 2206 | |
| 2207 | exec->maxRollbacks *= 2; |
| 2208 | tmp = (xmlRegExecRollback *) xmlRealloc(exec->rollbacks, |
| 2209 | exec->maxRollbacks * sizeof(xmlRegExecRollback)); |
| 2210 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2211 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2212 | exec->maxRollbacks /= 2; |
| 2213 | return; |
| 2214 | } |
| 2215 | exec->rollbacks = tmp; |
| 2216 | tmp = &exec->rollbacks[len]; |
| 2217 | memset(tmp, 0, (exec->maxRollbacks - len) * sizeof(xmlRegExecRollback)); |
| 2218 | } |
| 2219 | exec->rollbacks[exec->nbRollbacks].state = exec->state; |
| 2220 | exec->rollbacks[exec->nbRollbacks].index = exec->index; |
| 2221 | exec->rollbacks[exec->nbRollbacks].nextbranch = exec->transno + 1; |
| 2222 | if (exec->comp->nbCounters > 0) { |
| 2223 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
| 2224 | exec->rollbacks[exec->nbRollbacks].counts = (int *) |
| 2225 | xmlMalloc(exec->comp->nbCounters * sizeof(int)); |
| 2226 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2227 | xmlRegexpErrMemory(NULL, "saving regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2228 | exec->status = -5; |
| 2229 | return; |
| 2230 | } |
| 2231 | } |
| 2232 | memcpy(exec->rollbacks[exec->nbRollbacks].counts, exec->counts, |
| 2233 | exec->comp->nbCounters * sizeof(int)); |
| 2234 | } |
| 2235 | exec->nbRollbacks++; |
| 2236 | } |
| 2237 | |
| 2238 | static void |
| 2239 | xmlFARegExecRollBack(xmlRegExecCtxtPtr exec) { |
| 2240 | if (exec->nbRollbacks <= 0) { |
| 2241 | exec->status = -1; |
| 2242 | #ifdef DEBUG_REGEXP_EXEC |
| 2243 | printf("rollback failed on empty stack\n"); |
| 2244 | #endif |
| 2245 | return; |
| 2246 | } |
| 2247 | exec->nbRollbacks--; |
| 2248 | exec->state = exec->rollbacks[exec->nbRollbacks].state; |
| 2249 | exec->index = exec->rollbacks[exec->nbRollbacks].index; |
| 2250 | exec->transno = exec->rollbacks[exec->nbRollbacks].nextbranch; |
| 2251 | if (exec->comp->nbCounters > 0) { |
| 2252 | if (exec->rollbacks[exec->nbRollbacks].counts == NULL) { |
| 2253 | fprintf(stderr, "exec save: allocation failed"); |
| 2254 | exec->status = -6; |
| 2255 | return; |
| 2256 | } |
| 2257 | memcpy(exec->counts, exec->rollbacks[exec->nbRollbacks].counts, |
| 2258 | exec->comp->nbCounters * sizeof(int)); |
| 2259 | } |
| 2260 | |
| 2261 | #ifdef DEBUG_REGEXP_EXEC |
| 2262 | printf("restored "); |
| 2263 | xmlFARegDebugExec(exec); |
| 2264 | #endif |
| 2265 | } |
| 2266 | |
| 2267 | /************************************************************************ |
| 2268 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2269 | * Verifier, running an input against a compiled regexp * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2270 | * * |
| 2271 | ************************************************************************/ |
| 2272 | |
| 2273 | static int |
| 2274 | xmlFARegExec(xmlRegexpPtr comp, const xmlChar *content) { |
| 2275 | xmlRegExecCtxt execval; |
| 2276 | xmlRegExecCtxtPtr exec = &execval; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 2277 | int ret, codepoint = 0, len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2278 | |
| 2279 | exec->inputString = content; |
| 2280 | exec->index = 0; |
| 2281 | exec->determinist = 1; |
| 2282 | exec->maxRollbacks = 0; |
| 2283 | exec->nbRollbacks = 0; |
| 2284 | exec->rollbacks = NULL; |
| 2285 | exec->status = 0; |
| 2286 | exec->comp = comp; |
| 2287 | exec->state = comp->states[0]; |
| 2288 | exec->transno = 0; |
| 2289 | exec->transcount = 0; |
Daniel Veillard | f2a1283 | 2003-11-24 13:04:35 +0000 | [diff] [blame] | 2290 | exec->inputStack = NULL; |
| 2291 | exec->inputStackMax = 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2292 | if (comp->nbCounters > 0) { |
| 2293 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int)); |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2294 | if (exec->counts == NULL) { |
| 2295 | xmlRegexpErrMemory(NULL, "running regexp"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2296 | return(-1); |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2297 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2298 | memset(exec->counts, 0, comp->nbCounters * sizeof(int)); |
| 2299 | } else |
| 2300 | exec->counts = NULL; |
| 2301 | while ((exec->status == 0) && |
| 2302 | ((exec->inputString[exec->index] != 0) || |
| 2303 | (exec->state->type != XML_REGEXP_FINAL_STATE))) { |
| 2304 | xmlRegTransPtr trans; |
| 2305 | xmlRegAtomPtr atom; |
| 2306 | |
| 2307 | /* |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 2308 | * If end of input on non-terminal state, rollback, however we may |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2309 | * still have epsilon like transition for counted transitions |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 2310 | * on counters, in that case don't break too early. Additionally, |
| 2311 | * if we are working on a range like "AB{0,2}", where B is not present, |
| 2312 | * we don't want to break. |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2313 | */ |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 2314 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) { |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2315 | /* |
| 2316 | * if there is a transition, we must check if |
| 2317 | * atom allows minOccurs of 0 |
| 2318 | */ |
| 2319 | if (exec->transno < exec->state->nbTrans) { |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 2320 | trans = &exec->state->trans[exec->transno]; |
| 2321 | if (trans->to >=0) { |
| 2322 | atom = trans->atom; |
| 2323 | if (!((atom->min == 0) && (atom->max > 0))) |
| 2324 | goto rollback; |
| 2325 | } |
| 2326 | } else |
| 2327 | goto rollback; |
| 2328 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2329 | |
| 2330 | exec->transcount = 0; |
| 2331 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 2332 | trans = &exec->state->trans[exec->transno]; |
| 2333 | if (trans->to < 0) |
| 2334 | continue; |
| 2335 | atom = trans->atom; |
| 2336 | ret = 0; |
| 2337 | if (trans->count >= 0) { |
| 2338 | int count; |
| 2339 | xmlRegCounterPtr counter; |
| 2340 | |
| 2341 | /* |
| 2342 | * A counted transition. |
| 2343 | */ |
| 2344 | |
| 2345 | count = exec->counts[trans->count]; |
| 2346 | counter = &exec->comp->counters[trans->count]; |
| 2347 | #ifdef DEBUG_REGEXP_EXEC |
| 2348 | printf("testing count %d: val %d, min %d, max %d\n", |
| 2349 | trans->count, count, counter->min, counter->max); |
| 2350 | #endif |
| 2351 | ret = ((count >= counter->min) && (count <= counter->max)); |
| 2352 | } else if (atom == NULL) { |
| 2353 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 2354 | exec->status = -2; |
| 2355 | break; |
| 2356 | } else if (exec->inputString[exec->index] != 0) { |
| 2357 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len); |
| 2358 | ret = xmlRegCheckCharacter(atom, codepoint); |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 2359 | if ((ret == 1) && (atom->min >= 0) && (atom->max > 0)) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2360 | xmlRegStatePtr to = comp->states[trans->to]; |
| 2361 | |
| 2362 | /* |
| 2363 | * this is a multiple input sequence |
| 2364 | */ |
| 2365 | if (exec->state->nbTrans > exec->transno + 1) { |
| 2366 | xmlFARegExecSave(exec); |
| 2367 | } |
| 2368 | exec->transcount = 1; |
| 2369 | do { |
| 2370 | /* |
| 2371 | * Try to progress as much as possible on the input |
| 2372 | */ |
| 2373 | if (exec->transcount == atom->max) { |
| 2374 | break; |
| 2375 | } |
| 2376 | exec->index += len; |
| 2377 | /* |
| 2378 | * End of input: stop here |
| 2379 | */ |
| 2380 | if (exec->inputString[exec->index] == 0) { |
| 2381 | exec->index -= len; |
| 2382 | break; |
| 2383 | } |
| 2384 | if (exec->transcount >= atom->min) { |
| 2385 | int transno = exec->transno; |
| 2386 | xmlRegStatePtr state = exec->state; |
| 2387 | |
| 2388 | /* |
| 2389 | * The transition is acceptable save it |
| 2390 | */ |
| 2391 | exec->transno = -1; /* trick */ |
| 2392 | exec->state = to; |
| 2393 | xmlFARegExecSave(exec); |
| 2394 | exec->transno = transno; |
| 2395 | exec->state = state; |
| 2396 | } |
| 2397 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), |
| 2398 | len); |
| 2399 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 2400 | exec->transcount++; |
| 2401 | } while (ret == 1); |
| 2402 | if (exec->transcount < atom->min) |
| 2403 | ret = 0; |
| 2404 | |
| 2405 | /* |
| 2406 | * If the last check failed but one transition was found |
| 2407 | * possible, rollback |
| 2408 | */ |
| 2409 | if (ret < 0) |
| 2410 | ret = 0; |
| 2411 | if (ret == 0) { |
| 2412 | goto rollback; |
| 2413 | } |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 2414 | } else if ((ret == 0) && (atom->min == 0) && (atom->max > 0)) { |
| 2415 | /* |
| 2416 | * we don't match on the codepoint, but minOccurs of 0 |
| 2417 | * says that's ok. Setting len to 0 inhibits stepping |
| 2418 | * over the codepoint. |
| 2419 | */ |
| 2420 | exec->transcount = 1; |
| 2421 | len = 0; |
| 2422 | ret = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2423 | } |
William M. Brack | 0e00b28 | 2004-04-26 15:40:47 +0000 | [diff] [blame] | 2424 | } else if ((atom->min == 0) && (atom->max > 0)) { |
| 2425 | /* another spot to match when minOccurs is 0 */ |
| 2426 | exec->transcount = 1; |
| 2427 | len = 0; |
| 2428 | ret = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2429 | } |
| 2430 | if (ret == 1) { |
| 2431 | if (exec->state->nbTrans > exec->transno + 1) { |
| 2432 | xmlFARegExecSave(exec); |
| 2433 | } |
| 2434 | if (trans->counter >= 0) { |
| 2435 | #ifdef DEBUG_REGEXP_EXEC |
| 2436 | printf("Increasing count %d\n", trans->counter); |
| 2437 | #endif |
| 2438 | exec->counts[trans->counter]++; |
| 2439 | } |
| 2440 | #ifdef DEBUG_REGEXP_EXEC |
| 2441 | printf("entering state %d\n", trans->to); |
| 2442 | #endif |
| 2443 | exec->state = comp->states[trans->to]; |
| 2444 | exec->transno = 0; |
| 2445 | if (trans->atom != NULL) { |
| 2446 | exec->index += len; |
| 2447 | } |
| 2448 | goto progress; |
| 2449 | } else if (ret < 0) { |
| 2450 | exec->status = -4; |
| 2451 | break; |
| 2452 | } |
| 2453 | } |
| 2454 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 2455 | rollback: |
| 2456 | /* |
| 2457 | * Failed to find a way out |
| 2458 | */ |
| 2459 | exec->determinist = 0; |
| 2460 | xmlFARegExecRollBack(exec); |
| 2461 | } |
| 2462 | progress: |
| 2463 | continue; |
| 2464 | } |
| 2465 | if (exec->rollbacks != NULL) { |
| 2466 | if (exec->counts != NULL) { |
| 2467 | int i; |
| 2468 | |
| 2469 | for (i = 0;i < exec->maxRollbacks;i++) |
| 2470 | if (exec->rollbacks[i].counts != NULL) |
| 2471 | xmlFree(exec->rollbacks[i].counts); |
| 2472 | } |
| 2473 | xmlFree(exec->rollbacks); |
| 2474 | } |
| 2475 | if (exec->counts != NULL) |
| 2476 | xmlFree(exec->counts); |
| 2477 | if (exec->status == 0) |
| 2478 | return(1); |
| 2479 | if (exec->status == -1) |
| 2480 | return(0); |
| 2481 | return(exec->status); |
| 2482 | } |
| 2483 | |
| 2484 | /************************************************************************ |
| 2485 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2486 | * Progressive interface to the verifier one atom at a time * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2487 | * * |
| 2488 | ************************************************************************/ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 2489 | #ifdef DEBUG_ERR |
| 2490 | static void testerr(xmlRegExecCtxtPtr exec); |
| 2491 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2492 | |
| 2493 | /** |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2494 | * xmlRegNewExecCtxt: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2495 | * @comp: a precompiled regular expression |
| 2496 | * @callback: a callback function used for handling progresses in the |
| 2497 | * automata matching phase |
| 2498 | * @data: the context data associated to the callback in this context |
| 2499 | * |
| 2500 | * Build a context used for progressive evaluation of a regexp. |
Daniel Veillard | 01c13b5 | 2002-12-10 15:19:08 +0000 | [diff] [blame] | 2501 | * |
| 2502 | * Returns the new context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2503 | */ |
| 2504 | xmlRegExecCtxtPtr |
| 2505 | xmlRegNewExecCtxt(xmlRegexpPtr comp, xmlRegExecCallbacks callback, void *data) { |
| 2506 | xmlRegExecCtxtPtr exec; |
| 2507 | |
| 2508 | if (comp == NULL) |
| 2509 | return(NULL); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 2510 | if ((comp->compact == NULL) && (comp->states == NULL)) |
| 2511 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2512 | exec = (xmlRegExecCtxtPtr) xmlMalloc(sizeof(xmlRegExecCtxt)); |
| 2513 | if (exec == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2514 | xmlRegexpErrMemory(NULL, "creating execution context"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2515 | return(NULL); |
| 2516 | } |
| 2517 | memset(exec, 0, sizeof(xmlRegExecCtxt)); |
| 2518 | exec->inputString = NULL; |
| 2519 | exec->index = 0; |
| 2520 | exec->determinist = 1; |
| 2521 | exec->maxRollbacks = 0; |
| 2522 | exec->nbRollbacks = 0; |
| 2523 | exec->rollbacks = NULL; |
| 2524 | exec->status = 0; |
| 2525 | exec->comp = comp; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 2526 | if (comp->compact == NULL) |
| 2527 | exec->state = comp->states[0]; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2528 | exec->transno = 0; |
| 2529 | exec->transcount = 0; |
| 2530 | exec->callback = callback; |
| 2531 | exec->data = data; |
| 2532 | if (comp->nbCounters > 0) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 2533 | /* |
| 2534 | * For error handling, exec->counts is allocated twice the size |
| 2535 | * the second half is used to store the data in case of rollback |
| 2536 | */ |
| 2537 | exec->counts = (int *) xmlMalloc(comp->nbCounters * sizeof(int) |
| 2538 | * 2); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2539 | if (exec->counts == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2540 | xmlRegexpErrMemory(NULL, "creating execution context"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2541 | xmlFree(exec); |
| 2542 | return(NULL); |
| 2543 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 2544 | memset(exec->counts, 0, comp->nbCounters * sizeof(int) * 2); |
| 2545 | exec->errCounts = &exec->counts[comp->nbCounters]; |
| 2546 | } else { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2547 | exec->counts = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 2548 | exec->errCounts = NULL; |
| 2549 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2550 | exec->inputStackMax = 0; |
| 2551 | exec->inputStackNr = 0; |
| 2552 | exec->inputStack = NULL; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 2553 | exec->errStateNo = -1; |
| 2554 | exec->errString = NULL; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2555 | return(exec); |
| 2556 | } |
| 2557 | |
| 2558 | /** |
| 2559 | * xmlRegFreeExecCtxt: |
| 2560 | * @exec: a regular expression evaulation context |
| 2561 | * |
| 2562 | * Free the structures associated to a regular expression evaulation context. |
| 2563 | */ |
| 2564 | void |
| 2565 | xmlRegFreeExecCtxt(xmlRegExecCtxtPtr exec) { |
| 2566 | if (exec == NULL) |
| 2567 | return; |
| 2568 | |
| 2569 | if (exec->rollbacks != NULL) { |
| 2570 | if (exec->counts != NULL) { |
| 2571 | int i; |
| 2572 | |
| 2573 | for (i = 0;i < exec->maxRollbacks;i++) |
| 2574 | if (exec->rollbacks[i].counts != NULL) |
| 2575 | xmlFree(exec->rollbacks[i].counts); |
| 2576 | } |
| 2577 | xmlFree(exec->rollbacks); |
| 2578 | } |
| 2579 | if (exec->counts != NULL) |
| 2580 | xmlFree(exec->counts); |
| 2581 | if (exec->inputStack != NULL) { |
| 2582 | int i; |
| 2583 | |
Daniel Veillard | 3237023 | 2002-10-16 14:08:14 +0000 | [diff] [blame] | 2584 | for (i = 0;i < exec->inputStackNr;i++) { |
| 2585 | if (exec->inputStack[i].value != NULL) |
| 2586 | xmlFree(exec->inputStack[i].value); |
| 2587 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2588 | xmlFree(exec->inputStack); |
| 2589 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 2590 | if (exec->errString != NULL) |
| 2591 | xmlFree(exec->errString); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2592 | xmlFree(exec); |
| 2593 | } |
| 2594 | |
| 2595 | static void |
| 2596 | xmlFARegExecSaveInputString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 2597 | void *data) { |
| 2598 | #ifdef DEBUG_PUSH |
| 2599 | printf("saving value: %d:%s\n", exec->inputStackNr, value); |
| 2600 | #endif |
| 2601 | if (exec->inputStackMax == 0) { |
| 2602 | exec->inputStackMax = 4; |
| 2603 | exec->inputStack = (xmlRegInputTokenPtr) |
| 2604 | xmlMalloc(exec->inputStackMax * sizeof(xmlRegInputToken)); |
| 2605 | if (exec->inputStack == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2606 | xmlRegexpErrMemory(NULL, "pushing input string"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2607 | exec->inputStackMax = 0; |
| 2608 | return; |
| 2609 | } |
| 2610 | } else if (exec->inputStackNr + 1 >= exec->inputStackMax) { |
| 2611 | xmlRegInputTokenPtr tmp; |
| 2612 | |
| 2613 | exec->inputStackMax *= 2; |
| 2614 | tmp = (xmlRegInputTokenPtr) xmlRealloc(exec->inputStack, |
| 2615 | exec->inputStackMax * sizeof(xmlRegInputToken)); |
| 2616 | if (tmp == NULL) { |
Daniel Veillard | ff46a04 | 2003-10-08 08:53:17 +0000 | [diff] [blame] | 2617 | xmlRegexpErrMemory(NULL, "pushing input string"); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2618 | exec->inputStackMax /= 2; |
| 2619 | return; |
| 2620 | } |
| 2621 | exec->inputStack = tmp; |
| 2622 | } |
| 2623 | exec->inputStack[exec->inputStackNr].value = xmlStrdup(value); |
| 2624 | exec->inputStack[exec->inputStackNr].data = data; |
| 2625 | exec->inputStackNr++; |
| 2626 | exec->inputStack[exec->inputStackNr].value = NULL; |
| 2627 | exec->inputStack[exec->inputStackNr].data = NULL; |
| 2628 | } |
| 2629 | |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 2630 | /** |
| 2631 | * xmlRegStrEqualWildcard: |
| 2632 | * @expStr: the string to be evaluated |
| 2633 | * @valStr: the validation string |
| 2634 | * |
| 2635 | * Checks if both strings are equal or have the same content. "*" |
| 2636 | * can be used as a wildcard in @valStr; "|" is used as a seperator of |
| 2637 | * substrings in both @expStr and @valStr. |
| 2638 | * |
| 2639 | * Returns 1 if the comparison is satisfied and the number of substrings |
| 2640 | * is equal, 0 otherwise. |
| 2641 | */ |
| 2642 | |
| 2643 | static int |
| 2644 | xmlRegStrEqualWildcard(const xmlChar *expStr, const xmlChar *valStr) { |
| 2645 | if (expStr == valStr) return(1); |
| 2646 | if (expStr == NULL) return(0); |
| 2647 | if (valStr == NULL) return(0); |
| 2648 | do { |
| 2649 | /* |
| 2650 | * Eval if we have a wildcard for the current item. |
| 2651 | */ |
| 2652 | if (*expStr != *valStr) { |
| 2653 | if ((*valStr != 0) && (*expStr != 0) && (*expStr++ == '*')) { |
| 2654 | do { |
| 2655 | if (*valStr == XML_REG_STRING_SEPARATOR) |
| 2656 | break; |
Kasimier T. Buchcik | c0e833f | 2005-04-19 15:02:20 +0000 | [diff] [blame] | 2657 | valStr++; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 2658 | } while (*valStr != 0); |
| 2659 | continue; |
| 2660 | } else |
| 2661 | return(0); |
| 2662 | } |
Kasimier T. Buchcik | c0e833f | 2005-04-19 15:02:20 +0000 | [diff] [blame] | 2663 | expStr++; |
| 2664 | valStr++; |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 2665 | } while (*valStr != 0); |
| 2666 | if (*expStr != 0) |
| 2667 | return (0); |
| 2668 | else |
| 2669 | return (1); |
| 2670 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2671 | |
| 2672 | /** |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 2673 | * xmlRegCompactPushString: |
| 2674 | * @exec: a regexp execution context |
| 2675 | * @comp: the precompiled exec with a compact table |
| 2676 | * @value: a string token input |
| 2677 | * @data: data associated to the token to reuse in callbacks |
| 2678 | * |
| 2679 | * Push one input token in the execution context |
| 2680 | * |
| 2681 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 2682 | * a negative value in case of error. |
| 2683 | */ |
| 2684 | static int |
| 2685 | xmlRegCompactPushString(xmlRegExecCtxtPtr exec, |
| 2686 | xmlRegexpPtr comp, |
| 2687 | const xmlChar *value, |
| 2688 | void *data) { |
| 2689 | int state = exec->index; |
| 2690 | int i, target; |
| 2691 | |
| 2692 | if ((comp == NULL) || (comp->compact == NULL) || (comp->stringMap == NULL)) |
| 2693 | return(-1); |
| 2694 | |
| 2695 | if (value == NULL) { |
| 2696 | /* |
| 2697 | * are we at a final state ? |
| 2698 | */ |
| 2699 | if (comp->compact[state * (comp->nbstrings + 1)] == |
| 2700 | XML_REGEXP_FINAL_STATE) |
| 2701 | return(1); |
| 2702 | return(0); |
| 2703 | } |
| 2704 | |
| 2705 | #ifdef DEBUG_PUSH |
| 2706 | printf("value pushed: %s\n", value); |
| 2707 | #endif |
| 2708 | |
| 2709 | /* |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 2710 | * Examine all outside transitions from current state |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 2711 | */ |
| 2712 | for (i = 0;i < comp->nbstrings;i++) { |
| 2713 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
| 2714 | if ((target > 0) && (target <= comp->nbstates)) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 2715 | target--; /* to avoid 0 */ |
| 2716 | if (xmlRegStrEqualWildcard(comp->stringMap[i], value)) { |
| 2717 | exec->index = target; |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 2718 | if ((exec->callback != NULL) && (comp->transdata != NULL)) { |
| 2719 | exec->callback(exec->data, value, |
| 2720 | comp->transdata[state * comp->nbstrings + i], data); |
| 2721 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 2722 | #ifdef DEBUG_PUSH |
| 2723 | printf("entering state %d\n", target); |
| 2724 | #endif |
| 2725 | if (comp->compact[target * (comp->nbstrings + 1)] == |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 2726 | XML_REGEXP_SINK_STATE) |
| 2727 | goto error; |
| 2728 | |
| 2729 | if (comp->compact[target * (comp->nbstrings + 1)] == |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 2730 | XML_REGEXP_FINAL_STATE) |
| 2731 | return(1); |
| 2732 | return(0); |
| 2733 | } |
| 2734 | } |
| 2735 | } |
| 2736 | /* |
| 2737 | * Failed to find an exit transition out from current state for the |
| 2738 | * current token |
| 2739 | */ |
| 2740 | #ifdef DEBUG_PUSH |
| 2741 | printf("failed to find a transition for %s on state %d\n", value, state); |
| 2742 | #endif |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 2743 | error: |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 2744 | if (exec->errString != NULL) |
| 2745 | xmlFree(exec->errString); |
| 2746 | exec->errString = xmlStrdup(value); |
| 2747 | exec->errStateNo = state; |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 2748 | exec->status = -1; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 2749 | #ifdef DEBUG_ERR |
| 2750 | testerr(exec); |
| 2751 | #endif |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 2752 | return(-1); |
| 2753 | } |
| 2754 | |
| 2755 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2756 | * xmlRegExecPushString: |
Daniel Veillard | ea7751d | 2002-12-20 00:16:24 +0000 | [diff] [blame] | 2757 | * @exec: a regexp execution context or NULL to indicate the end |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2758 | * @value: a string token input |
| 2759 | * @data: data associated to the token to reuse in callbacks |
| 2760 | * |
| 2761 | * Push one input token in the execution context |
| 2762 | * |
| 2763 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 2764 | * a negative value in case of error. |
| 2765 | */ |
| 2766 | int |
| 2767 | xmlRegExecPushString(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 2768 | void *data) { |
| 2769 | xmlRegTransPtr trans; |
| 2770 | xmlRegAtomPtr atom; |
| 2771 | int ret; |
| 2772 | int final = 0; |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 2773 | int progress = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2774 | |
| 2775 | if (exec == NULL) |
| 2776 | return(-1); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 2777 | if (exec->comp == NULL) |
| 2778 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2779 | if (exec->status != 0) |
| 2780 | return(exec->status); |
| 2781 | |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 2782 | if (exec->comp->compact != NULL) |
| 2783 | return(xmlRegCompactPushString(exec, exec->comp, value, data)); |
| 2784 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2785 | if (value == NULL) { |
| 2786 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
| 2787 | return(1); |
| 2788 | final = 1; |
| 2789 | } |
| 2790 | |
| 2791 | #ifdef DEBUG_PUSH |
| 2792 | printf("value pushed: %s\n", value); |
| 2793 | #endif |
| 2794 | /* |
| 2795 | * If we have an active rollback stack push the new value there |
| 2796 | * and get back to where we were left |
| 2797 | */ |
| 2798 | if ((value != NULL) && (exec->inputStackNr > 0)) { |
| 2799 | xmlFARegExecSaveInputString(exec, value, data); |
| 2800 | value = exec->inputStack[exec->index].value; |
| 2801 | data = exec->inputStack[exec->index].data; |
| 2802 | #ifdef DEBUG_PUSH |
| 2803 | printf("value loaded: %s\n", value); |
| 2804 | #endif |
| 2805 | } |
| 2806 | |
| 2807 | while ((exec->status == 0) && |
| 2808 | ((value != NULL) || |
| 2809 | ((final == 1) && |
| 2810 | (exec->state->type != XML_REGEXP_FINAL_STATE)))) { |
| 2811 | |
| 2812 | /* |
| 2813 | * End of input on non-terminal state, rollback, however we may |
| 2814 | * still have epsilon like transition for counted transitions |
| 2815 | * on counters, in that case don't break too early. |
| 2816 | */ |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 2817 | if ((value == NULL) && (exec->counts == NULL)) |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2818 | goto rollback; |
| 2819 | |
| 2820 | exec->transcount = 0; |
| 2821 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 2822 | trans = &exec->state->trans[exec->transno]; |
| 2823 | if (trans->to < 0) |
| 2824 | continue; |
| 2825 | atom = trans->atom; |
| 2826 | ret = 0; |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 2827 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
| 2828 | int i; |
| 2829 | int count; |
| 2830 | xmlRegTransPtr t; |
| 2831 | xmlRegCounterPtr counter; |
| 2832 | |
| 2833 | ret = 0; |
| 2834 | |
| 2835 | #ifdef DEBUG_PUSH |
| 2836 | printf("testing all lax %d\n", trans->count); |
| 2837 | #endif |
| 2838 | /* |
| 2839 | * Check all counted transitions from the current state |
| 2840 | */ |
| 2841 | if ((value == NULL) && (final)) { |
| 2842 | ret = 1; |
| 2843 | } else if (value != NULL) { |
| 2844 | for (i = 0;i < exec->state->nbTrans;i++) { |
| 2845 | t = &exec->state->trans[i]; |
| 2846 | if ((t->counter < 0) || (t == trans)) |
| 2847 | continue; |
| 2848 | counter = &exec->comp->counters[t->counter]; |
| 2849 | count = exec->counts[t->counter]; |
| 2850 | if ((count < counter->max) && |
| 2851 | (t->atom != NULL) && |
| 2852 | (xmlStrEqual(value, t->atom->valuep))) { |
| 2853 | ret = 0; |
| 2854 | break; |
| 2855 | } |
| 2856 | if ((count >= counter->min) && |
| 2857 | (count < counter->max) && |
| 2858 | (xmlStrEqual(value, t->atom->valuep))) { |
| 2859 | ret = 1; |
| 2860 | break; |
| 2861 | } |
| 2862 | } |
| 2863 | } |
| 2864 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
Daniel Veillard | 8a001f6 | 2002-04-20 07:24:11 +0000 | [diff] [blame] | 2865 | int i; |
| 2866 | int count; |
| 2867 | xmlRegTransPtr t; |
| 2868 | xmlRegCounterPtr counter; |
| 2869 | |
| 2870 | ret = 1; |
| 2871 | |
| 2872 | #ifdef DEBUG_PUSH |
| 2873 | printf("testing all %d\n", trans->count); |
| 2874 | #endif |
| 2875 | /* |
| 2876 | * Check all counted transitions from the current state |
| 2877 | */ |
| 2878 | for (i = 0;i < exec->state->nbTrans;i++) { |
| 2879 | t = &exec->state->trans[i]; |
| 2880 | if ((t->counter < 0) || (t == trans)) |
| 2881 | continue; |
| 2882 | counter = &exec->comp->counters[t->counter]; |
| 2883 | count = exec->counts[t->counter]; |
| 2884 | if ((count < counter->min) || (count > counter->max)) { |
| 2885 | ret = 0; |
| 2886 | break; |
| 2887 | } |
| 2888 | } |
| 2889 | } else if (trans->count >= 0) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2890 | int count; |
| 2891 | xmlRegCounterPtr counter; |
| 2892 | |
| 2893 | /* |
| 2894 | * A counted transition. |
| 2895 | */ |
| 2896 | |
| 2897 | count = exec->counts[trans->count]; |
| 2898 | counter = &exec->comp->counters[trans->count]; |
| 2899 | #ifdef DEBUG_PUSH |
| 2900 | printf("testing count %d: val %d, min %d, max %d\n", |
| 2901 | trans->count, count, counter->min, counter->max); |
| 2902 | #endif |
| 2903 | ret = ((count >= counter->min) && (count <= counter->max)); |
| 2904 | } else if (atom == NULL) { |
| 2905 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 2906 | exec->status = -2; |
| 2907 | break; |
| 2908 | } else if (value != NULL) { |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 2909 | ret = xmlRegStrEqualWildcard(atom->valuep, value); |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 2910 | if ((ret == 1) && (trans->counter >= 0)) { |
| 2911 | xmlRegCounterPtr counter; |
| 2912 | int count; |
| 2913 | |
| 2914 | count = exec->counts[trans->counter]; |
| 2915 | counter = &exec->comp->counters[trans->counter]; |
| 2916 | if (count >= counter->max) |
| 2917 | ret = 0; |
| 2918 | } |
| 2919 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2920 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
| 2921 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
| 2922 | |
| 2923 | /* |
| 2924 | * this is a multiple input sequence |
| 2925 | */ |
| 2926 | if (exec->state->nbTrans > exec->transno + 1) { |
| 2927 | if (exec->inputStackNr <= 0) { |
| 2928 | xmlFARegExecSaveInputString(exec, value, data); |
| 2929 | } |
| 2930 | xmlFARegExecSave(exec); |
| 2931 | } |
| 2932 | exec->transcount = 1; |
| 2933 | do { |
| 2934 | /* |
| 2935 | * Try to progress as much as possible on the input |
| 2936 | */ |
| 2937 | if (exec->transcount == atom->max) { |
| 2938 | break; |
| 2939 | } |
| 2940 | exec->index++; |
| 2941 | value = exec->inputStack[exec->index].value; |
| 2942 | data = exec->inputStack[exec->index].data; |
| 2943 | #ifdef DEBUG_PUSH |
| 2944 | printf("value loaded: %s\n", value); |
| 2945 | #endif |
| 2946 | |
| 2947 | /* |
| 2948 | * End of input: stop here |
| 2949 | */ |
| 2950 | if (value == NULL) { |
| 2951 | exec->index --; |
| 2952 | break; |
| 2953 | } |
| 2954 | if (exec->transcount >= atom->min) { |
| 2955 | int transno = exec->transno; |
| 2956 | xmlRegStatePtr state = exec->state; |
| 2957 | |
| 2958 | /* |
| 2959 | * The transition is acceptable save it |
| 2960 | */ |
| 2961 | exec->transno = -1; /* trick */ |
| 2962 | exec->state = to; |
| 2963 | if (exec->inputStackNr <= 0) { |
| 2964 | xmlFARegExecSaveInputString(exec, value, data); |
| 2965 | } |
| 2966 | xmlFARegExecSave(exec); |
| 2967 | exec->transno = transno; |
| 2968 | exec->state = state; |
| 2969 | } |
| 2970 | ret = xmlStrEqual(value, atom->valuep); |
| 2971 | exec->transcount++; |
| 2972 | } while (ret == 1); |
| 2973 | if (exec->transcount < atom->min) |
| 2974 | ret = 0; |
| 2975 | |
| 2976 | /* |
| 2977 | * If the last check failed but one transition was found |
| 2978 | * possible, rollback |
| 2979 | */ |
| 2980 | if (ret < 0) |
| 2981 | ret = 0; |
| 2982 | if (ret == 0) { |
| 2983 | goto rollback; |
| 2984 | } |
| 2985 | } |
| 2986 | } |
| 2987 | if (ret == 1) { |
William M. Brack | 9887395 | 2003-12-26 06:03:14 +0000 | [diff] [blame] | 2988 | if ((exec->callback != NULL) && (atom != NULL) && |
| 2989 | (data != NULL)) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 2990 | exec->callback(exec->data, atom->valuep, |
| 2991 | atom->data, data); |
| 2992 | } |
| 2993 | if (exec->state->nbTrans > exec->transno + 1) { |
| 2994 | if (exec->inputStackNr <= 0) { |
| 2995 | xmlFARegExecSaveInputString(exec, value, data); |
| 2996 | } |
| 2997 | xmlFARegExecSave(exec); |
| 2998 | } |
| 2999 | if (trans->counter >= 0) { |
| 3000 | #ifdef DEBUG_PUSH |
| 3001 | printf("Increasing count %d\n", trans->counter); |
| 3002 | #endif |
| 3003 | exec->counts[trans->counter]++; |
| 3004 | } |
| 3005 | #ifdef DEBUG_PUSH |
| 3006 | printf("entering state %d\n", trans->to); |
| 3007 | #endif |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3008 | if ((exec->comp->states[trans->to] != NULL) && |
| 3009 | (exec->comp->states[trans->to]->type == |
| 3010 | XML_REGEXP_SINK_STATE)) { |
| 3011 | /* |
| 3012 | * entering a sink state, save the current state as error |
| 3013 | * state. |
| 3014 | */ |
| 3015 | if (exec->errString != NULL) |
| 3016 | xmlFree(exec->errString); |
| 3017 | exec->errString = xmlStrdup(value); |
| 3018 | exec->errState = exec->state; |
| 3019 | memcpy(exec->errCounts, exec->counts, |
| 3020 | exec->comp->nbCounters * sizeof(int)); |
| 3021 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3022 | exec->state = exec->comp->states[trans->to]; |
| 3023 | exec->transno = 0; |
| 3024 | if (trans->atom != NULL) { |
| 3025 | if (exec->inputStack != NULL) { |
| 3026 | exec->index++; |
| 3027 | if (exec->index < exec->inputStackNr) { |
| 3028 | value = exec->inputStack[exec->index].value; |
| 3029 | data = exec->inputStack[exec->index].data; |
| 3030 | #ifdef DEBUG_PUSH |
| 3031 | printf("value loaded: %s\n", value); |
| 3032 | #endif |
| 3033 | } else { |
| 3034 | value = NULL; |
| 3035 | data = NULL; |
| 3036 | #ifdef DEBUG_PUSH |
| 3037 | printf("end of input\n"); |
| 3038 | #endif |
| 3039 | } |
| 3040 | } else { |
| 3041 | value = NULL; |
| 3042 | data = NULL; |
| 3043 | #ifdef DEBUG_PUSH |
| 3044 | printf("end of input\n"); |
| 3045 | #endif |
| 3046 | } |
| 3047 | } |
| 3048 | goto progress; |
| 3049 | } else if (ret < 0) { |
| 3050 | exec->status = -4; |
| 3051 | break; |
| 3052 | } |
| 3053 | } |
| 3054 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 3055 | rollback: |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3056 | /* |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3057 | * if we didn't yet rollback on the current input |
| 3058 | * store the current state as the error state. |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3059 | */ |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3060 | if ((progress) && (exec->state != NULL) && |
| 3061 | (exec->state->type != XML_REGEXP_SINK_STATE)) { |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3062 | progress = 0; |
| 3063 | if (exec->errString != NULL) |
| 3064 | xmlFree(exec->errString); |
| 3065 | exec->errString = xmlStrdup(value); |
| 3066 | exec->errState = exec->state; |
| 3067 | memcpy(exec->errCounts, exec->counts, |
| 3068 | exec->comp->nbCounters * sizeof(int)); |
| 3069 | } |
| 3070 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3071 | /* |
| 3072 | * Failed to find a way out |
| 3073 | */ |
| 3074 | exec->determinist = 0; |
| 3075 | xmlFARegExecRollBack(exec); |
| 3076 | if (exec->status == 0) { |
| 3077 | value = exec->inputStack[exec->index].value; |
| 3078 | data = exec->inputStack[exec->index].data; |
| 3079 | #ifdef DEBUG_PUSH |
| 3080 | printf("value loaded: %s\n", value); |
| 3081 | #endif |
| 3082 | } |
| 3083 | } |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3084 | continue; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3085 | progress: |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3086 | progress = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3087 | continue; |
| 3088 | } |
| 3089 | if (exec->status == 0) { |
| 3090 | return(exec->state->type == XML_REGEXP_FINAL_STATE); |
| 3091 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3092 | #ifdef DEBUG_ERR |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3093 | if (exec->status < 0) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3094 | testerr(exec); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3095 | } |
Daniel Veillard | 9070015 | 2005-01-08 22:05:09 +0000 | [diff] [blame] | 3096 | #endif |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3097 | return(exec->status); |
| 3098 | } |
| 3099 | |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3100 | /** |
| 3101 | * xmlRegExecPushString2: |
| 3102 | * @exec: a regexp execution context or NULL to indicate the end |
| 3103 | * @value: the first string token input |
| 3104 | * @value2: the second string token input |
| 3105 | * @data: data associated to the token to reuse in callbacks |
| 3106 | * |
| 3107 | * Push one input token in the execution context |
| 3108 | * |
| 3109 | * Returns: 1 if the regexp reached a final state, 0 if non-final, and |
| 3110 | * a negative value in case of error. |
| 3111 | */ |
| 3112 | int |
| 3113 | xmlRegExecPushString2(xmlRegExecCtxtPtr exec, const xmlChar *value, |
| 3114 | const xmlChar *value2, void *data) { |
| 3115 | xmlChar buf[150]; |
| 3116 | int lenn, lenp, ret; |
| 3117 | xmlChar *str; |
| 3118 | |
| 3119 | if (exec == NULL) |
| 3120 | return(-1); |
| 3121 | if (exec->comp == NULL) |
| 3122 | return(-1); |
| 3123 | if (exec->status != 0) |
| 3124 | return(exec->status); |
| 3125 | |
| 3126 | if (value2 == NULL) |
| 3127 | return(xmlRegExecPushString(exec, value, data)); |
| 3128 | |
| 3129 | lenn = strlen((char *) value2); |
| 3130 | lenp = strlen((char *) value); |
| 3131 | |
| 3132 | if (150 < lenn + lenp + 2) { |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 3133 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3134 | if (str == NULL) { |
| 3135 | exec->status = -1; |
| 3136 | return(-1); |
| 3137 | } |
| 3138 | } else { |
| 3139 | str = buf; |
| 3140 | } |
| 3141 | memcpy(&str[0], value, lenp); |
Daniel Veillard | c0826a7 | 2004-08-10 14:17:33 +0000 | [diff] [blame] | 3142 | str[lenp] = XML_REG_STRING_SEPARATOR; |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 3143 | memcpy(&str[lenp + 1], value2, lenn); |
| 3144 | str[lenn + lenp + 1] = 0; |
| 3145 | |
| 3146 | if (exec->comp->compact != NULL) |
| 3147 | ret = xmlRegCompactPushString(exec, exec->comp, str, data); |
| 3148 | else |
| 3149 | ret = xmlRegExecPushString(exec, str, data); |
| 3150 | |
| 3151 | if (str != buf) |
| 3152 | xmlFree(buf); |
| 3153 | return(ret); |
| 3154 | } |
| 3155 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3156 | /** |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3157 | * xmlRegExecGetalues: |
| 3158 | * @exec: a regexp execution context |
| 3159 | * @err: error extraction or normal one |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3160 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3161 | * @nbneg: return number of negative transitions |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3162 | * @values: pointer to the array of acceptable values |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3163 | * @terminal: return value if this was a terminal state |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3164 | * |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3165 | * Extract informations from the regexp execution, internal routine to |
| 3166 | * implement xmlRegExecNextValues() and xmlRegExecErrInfo() |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3167 | * |
| 3168 | * Returns: 0 in case of success or -1 in case of error. |
| 3169 | */ |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3170 | static int |
| 3171 | xmlRegExecGetValues(xmlRegExecCtxtPtr exec, int err, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3172 | int *nbval, int *nbneg, |
| 3173 | xmlChar **values, int *terminal) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3174 | int maxval; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3175 | int nb = 0; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3176 | |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3177 | if ((exec == NULL) || (nbval == NULL) || (nbneg == NULL) || |
| 3178 | (values == NULL) || (*nbval <= 0)) |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3179 | return(-1); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3180 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3181 | maxval = *nbval; |
| 3182 | *nbval = 0; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3183 | *nbneg = 0; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3184 | if ((exec->comp != NULL) && (exec->comp->compact != NULL)) { |
| 3185 | xmlRegexpPtr comp; |
| 3186 | int target, i, state; |
| 3187 | |
| 3188 | comp = exec->comp; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3189 | |
| 3190 | if (err) { |
| 3191 | if (exec->errStateNo == -1) return(-1); |
| 3192 | state = exec->errStateNo; |
| 3193 | } else { |
| 3194 | state = exec->index; |
| 3195 | } |
| 3196 | if (terminal != NULL) { |
| 3197 | if (comp->compact[state * (comp->nbstrings + 1)] == |
| 3198 | XML_REGEXP_FINAL_STATE) |
| 3199 | *terminal = 1; |
| 3200 | else |
| 3201 | *terminal = 0; |
| 3202 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3203 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3204 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3205 | if ((target > 0) && (target <= comp->nbstates) && |
| 3206 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] != |
| 3207 | XML_REGEXP_SINK_STATE)) { |
| 3208 | values[nb++] = comp->stringMap[i]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3209 | (*nbval)++; |
| 3210 | } |
| 3211 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3212 | for (i = 0;(i < comp->nbstrings) && (nb < maxval);i++) { |
| 3213 | target = comp->compact[state * (comp->nbstrings + 1) + i + 1]; |
| 3214 | if ((target > 0) && (target <= comp->nbstates) && |
| 3215 | (comp->compact[(target - 1) * (comp->nbstrings + 1)] == |
| 3216 | XML_REGEXP_SINK_STATE)) { |
| 3217 | values[nb++] = comp->stringMap[i]; |
| 3218 | (*nbneg)++; |
| 3219 | } |
| 3220 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3221 | } else { |
| 3222 | int transno; |
| 3223 | xmlRegTransPtr trans; |
| 3224 | xmlRegAtomPtr atom; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3225 | xmlRegStatePtr state; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3226 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3227 | if (terminal != NULL) { |
| 3228 | if (exec->state->type == XML_REGEXP_FINAL_STATE) |
| 3229 | *terminal = 1; |
| 3230 | else |
| 3231 | *terminal = 0; |
| 3232 | } |
| 3233 | |
| 3234 | if (err) { |
| 3235 | if (exec->errState == NULL) return(-1); |
| 3236 | state = exec->errState; |
| 3237 | } else { |
| 3238 | if (exec->state == NULL) return(-1); |
| 3239 | state = exec->state; |
| 3240 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3241 | for (transno = 0; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3242 | (transno < state->nbTrans) && (nb < maxval); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3243 | transno++) { |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3244 | trans = &state->trans[transno]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3245 | if (trans->to < 0) |
| 3246 | continue; |
| 3247 | atom = trans->atom; |
| 3248 | if ((atom == NULL) || (atom->valuep == NULL)) |
| 3249 | continue; |
| 3250 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3251 | /* this should not be reached but ... */ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3252 | TODO; |
| 3253 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3254 | /* this should not be reached but ... */ |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3255 | TODO; |
| 3256 | } else if (trans->counter >= 0) { |
| 3257 | xmlRegCounterPtr counter; |
| 3258 | int count; |
| 3259 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3260 | if (err) |
| 3261 | count = exec->errCounts[trans->counter]; |
| 3262 | else |
| 3263 | count = exec->counts[trans->counter]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3264 | counter = &exec->comp->counters[trans->counter]; |
| 3265 | if (count < counter->max) { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3266 | values[nb++] = (xmlChar *) atom->valuep; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3267 | (*nbval)++; |
| 3268 | } |
| 3269 | } else { |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3270 | if ((exec->comp->states[trans->to] != NULL) && |
| 3271 | (exec->comp->states[trans->to]->type != |
| 3272 | XML_REGEXP_SINK_STATE)) { |
| 3273 | values[nb++] = (xmlChar *) atom->valuep; |
| 3274 | (*nbval)++; |
| 3275 | } |
| 3276 | } |
| 3277 | } |
| 3278 | for (transno = 0; |
| 3279 | (transno < state->nbTrans) && (nb < maxval); |
| 3280 | transno++) { |
| 3281 | trans = &state->trans[transno]; |
| 3282 | if (trans->to < 0) |
| 3283 | continue; |
| 3284 | atom = trans->atom; |
| 3285 | if ((atom == NULL) || (atom->valuep == NULL)) |
| 3286 | continue; |
| 3287 | if (trans->count == REGEXP_ALL_LAX_COUNTER) { |
| 3288 | continue; |
| 3289 | } else if (trans->count == REGEXP_ALL_COUNTER) { |
| 3290 | continue; |
| 3291 | } else if (trans->counter >= 0) { |
| 3292 | continue; |
| 3293 | } else { |
| 3294 | if ((exec->comp->states[trans->to] != NULL) && |
| 3295 | (exec->comp->states[trans->to]->type == |
| 3296 | XML_REGEXP_SINK_STATE)) { |
| 3297 | values[nb++] = (xmlChar *) atom->valuep; |
| 3298 | (*nbneg)++; |
| 3299 | } |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3300 | } |
| 3301 | } |
| 3302 | } |
| 3303 | return(0); |
| 3304 | } |
| 3305 | |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3306 | /** |
| 3307 | * xmlRegExecNextValues: |
| 3308 | * @exec: a regexp execution context |
| 3309 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3310 | * @nbneg: return number of negative transitions |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3311 | * @values: pointer to the array of acceptable values |
| 3312 | * @terminal: return value if this was a terminal state |
| 3313 | * |
| 3314 | * Extract informations from the regexp execution, |
| 3315 | * the parameter @values must point to an array of @nbval string pointers |
| 3316 | * on return nbval will contain the number of possible strings in that |
| 3317 | * state and the @values array will be updated with them. The string values |
| 3318 | * returned will be freed with the @exec context and don't need to be |
| 3319 | * deallocated. |
| 3320 | * |
| 3321 | * Returns: 0 in case of success or -1 in case of error. |
| 3322 | */ |
| 3323 | int |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3324 | xmlRegExecNextValues(xmlRegExecCtxtPtr exec, int *nbval, int *nbneg, |
| 3325 | xmlChar **values, int *terminal) { |
| 3326 | return(xmlRegExecGetValues(exec, 0, nbval, nbneg, values, terminal)); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
| 3329 | /** |
| 3330 | * xmlRegExecErrInfo: |
| 3331 | * @exec: a regexp execution context generating an error |
| 3332 | * @string: return value for the error string |
| 3333 | * @nbval: pointer to the number of accepted values IN/OUT |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3334 | * @nbneg: return number of negative transitions |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3335 | * @values: pointer to the array of acceptable values |
| 3336 | * @terminal: return value if this was a terminal state |
| 3337 | * |
| 3338 | * Extract error informations from the regexp execution, the parameter |
| 3339 | * @string will be updated with the value pushed and not accepted, |
| 3340 | * the parameter @values must point to an array of @nbval string pointers |
| 3341 | * on return nbval will contain the number of possible strings in that |
| 3342 | * state and the @values array will be updated with them. The string values |
| 3343 | * returned will be freed with the @exec context and don't need to be |
| 3344 | * deallocated. |
| 3345 | * |
| 3346 | * Returns: 0 in case of success or -1 in case of error. |
| 3347 | */ |
| 3348 | int |
| 3349 | xmlRegExecErrInfo(xmlRegExecCtxtPtr exec, const xmlChar **string, |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3350 | int *nbval, int *nbneg, xmlChar **values, int *terminal) { |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3351 | if (exec == NULL) |
| 3352 | return(-1); |
| 3353 | if (string != NULL) { |
| 3354 | if (exec->status != 0) |
| 3355 | *string = exec->errString; |
| 3356 | else |
| 3357 | *string = NULL; |
| 3358 | } |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3359 | return(xmlRegExecGetValues(exec, 1, nbval, nbneg, values, terminal)); |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3360 | } |
| 3361 | |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3362 | #ifdef DEBUG_ERR |
| 3363 | static void testerr(xmlRegExecCtxtPtr exec) { |
| 3364 | const xmlChar *string; |
Daniel Veillard | cee2b3a | 2005-01-25 00:22:52 +0000 | [diff] [blame] | 3365 | xmlChar *values[5]; |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3366 | int nb = 5; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3367 | int nbneg; |
Daniel Veillard | fc0b6f6 | 2005-01-09 17:48:02 +0000 | [diff] [blame] | 3368 | int terminal; |
Daniel Veillard | cc026dc | 2005-01-12 13:21:17 +0000 | [diff] [blame] | 3369 | xmlRegExecErrInfo(exec, &string, &nb, &nbneg, &values[0], &terminal); |
Daniel Veillard | 7bd8b4b | 2005-01-07 13:56:19 +0000 | [diff] [blame] | 3370 | } |
| 3371 | #endif |
| 3372 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3373 | #if 0 |
| 3374 | static int |
| 3375 | xmlRegExecPushChar(xmlRegExecCtxtPtr exec, int UCS) { |
| 3376 | xmlRegTransPtr trans; |
| 3377 | xmlRegAtomPtr atom; |
| 3378 | int ret; |
| 3379 | int codepoint, len; |
| 3380 | |
| 3381 | if (exec == NULL) |
| 3382 | return(-1); |
| 3383 | if (exec->status != 0) |
| 3384 | return(exec->status); |
| 3385 | |
| 3386 | while ((exec->status == 0) && |
| 3387 | ((exec->inputString[exec->index] != 0) || |
| 3388 | (exec->state->type != XML_REGEXP_FINAL_STATE))) { |
| 3389 | |
| 3390 | /* |
| 3391 | * End of input on non-terminal state, rollback, however we may |
| 3392 | * still have epsilon like transition for counted transitions |
| 3393 | * on counters, in that case don't break too early. |
| 3394 | */ |
| 3395 | if ((exec->inputString[exec->index] == 0) && (exec->counts == NULL)) |
| 3396 | goto rollback; |
| 3397 | |
| 3398 | exec->transcount = 0; |
| 3399 | for (;exec->transno < exec->state->nbTrans;exec->transno++) { |
| 3400 | trans = &exec->state->trans[exec->transno]; |
| 3401 | if (trans->to < 0) |
| 3402 | continue; |
| 3403 | atom = trans->atom; |
| 3404 | ret = 0; |
| 3405 | if (trans->count >= 0) { |
| 3406 | int count; |
| 3407 | xmlRegCounterPtr counter; |
| 3408 | |
| 3409 | /* |
| 3410 | * A counted transition. |
| 3411 | */ |
| 3412 | |
| 3413 | count = exec->counts[trans->count]; |
| 3414 | counter = &exec->comp->counters[trans->count]; |
| 3415 | #ifdef DEBUG_REGEXP_EXEC |
| 3416 | printf("testing count %d: val %d, min %d, max %d\n", |
| 3417 | trans->count, count, counter->min, counter->max); |
| 3418 | #endif |
| 3419 | ret = ((count >= counter->min) && (count <= counter->max)); |
| 3420 | } else if (atom == NULL) { |
| 3421 | fprintf(stderr, "epsilon transition left at runtime\n"); |
| 3422 | exec->status = -2; |
| 3423 | break; |
| 3424 | } else if (exec->inputString[exec->index] != 0) { |
| 3425 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), len); |
| 3426 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 3427 | if ((ret == 1) && (atom->min > 0) && (atom->max > 0)) { |
| 3428 | xmlRegStatePtr to = exec->comp->states[trans->to]; |
| 3429 | |
| 3430 | /* |
| 3431 | * this is a multiple input sequence |
| 3432 | */ |
| 3433 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3434 | xmlFARegExecSave(exec); |
| 3435 | } |
| 3436 | exec->transcount = 1; |
| 3437 | do { |
| 3438 | /* |
| 3439 | * Try to progress as much as possible on the input |
| 3440 | */ |
| 3441 | if (exec->transcount == atom->max) { |
| 3442 | break; |
| 3443 | } |
| 3444 | exec->index += len; |
| 3445 | /* |
| 3446 | * End of input: stop here |
| 3447 | */ |
| 3448 | if (exec->inputString[exec->index] == 0) { |
| 3449 | exec->index -= len; |
| 3450 | break; |
| 3451 | } |
| 3452 | if (exec->transcount >= atom->min) { |
| 3453 | int transno = exec->transno; |
| 3454 | xmlRegStatePtr state = exec->state; |
| 3455 | |
| 3456 | /* |
| 3457 | * The transition is acceptable save it |
| 3458 | */ |
| 3459 | exec->transno = -1; /* trick */ |
| 3460 | exec->state = to; |
| 3461 | xmlFARegExecSave(exec); |
| 3462 | exec->transno = transno; |
| 3463 | exec->state = state; |
| 3464 | } |
| 3465 | codepoint = CUR_SCHAR(&(exec->inputString[exec->index]), |
| 3466 | len); |
| 3467 | ret = xmlRegCheckCharacter(atom, codepoint); |
| 3468 | exec->transcount++; |
| 3469 | } while (ret == 1); |
| 3470 | if (exec->transcount < atom->min) |
| 3471 | ret = 0; |
| 3472 | |
| 3473 | /* |
| 3474 | * If the last check failed but one transition was found |
| 3475 | * possible, rollback |
| 3476 | */ |
| 3477 | if (ret < 0) |
| 3478 | ret = 0; |
| 3479 | if (ret == 0) { |
| 3480 | goto rollback; |
| 3481 | } |
| 3482 | } |
| 3483 | } |
| 3484 | if (ret == 1) { |
| 3485 | if (exec->state->nbTrans > exec->transno + 1) { |
| 3486 | xmlFARegExecSave(exec); |
| 3487 | } |
| 3488 | if (trans->counter >= 0) { |
| 3489 | #ifdef DEBUG_REGEXP_EXEC |
| 3490 | printf("Increasing count %d\n", trans->counter); |
| 3491 | #endif |
| 3492 | exec->counts[trans->counter]++; |
| 3493 | } |
| 3494 | #ifdef DEBUG_REGEXP_EXEC |
| 3495 | printf("entering state %d\n", trans->to); |
| 3496 | #endif |
| 3497 | exec->state = exec->comp->states[trans->to]; |
| 3498 | exec->transno = 0; |
| 3499 | if (trans->atom != NULL) { |
| 3500 | exec->index += len; |
| 3501 | } |
| 3502 | goto progress; |
| 3503 | } else if (ret < 0) { |
| 3504 | exec->status = -4; |
| 3505 | break; |
| 3506 | } |
| 3507 | } |
| 3508 | if ((exec->transno != 0) || (exec->state->nbTrans == 0)) { |
| 3509 | rollback: |
| 3510 | /* |
| 3511 | * Failed to find a way out |
| 3512 | */ |
| 3513 | exec->determinist = 0; |
| 3514 | xmlFARegExecRollBack(exec); |
| 3515 | } |
| 3516 | progress: |
| 3517 | continue; |
| 3518 | } |
| 3519 | } |
| 3520 | #endif |
| 3521 | /************************************************************************ |
| 3522 | * * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 3523 | * Parser for the Schemas Datatype Regular Expressions * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3524 | * http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#regexs * |
| 3525 | * * |
| 3526 | ************************************************************************/ |
| 3527 | |
| 3528 | /** |
| 3529 | * xmlFAIsChar: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3530 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3531 | * |
| 3532 | * [10] Char ::= [^.\?*+()|#x5B#x5D] |
| 3533 | */ |
| 3534 | static int |
| 3535 | xmlFAIsChar(xmlRegParserCtxtPtr ctxt) { |
| 3536 | int cur; |
| 3537 | int len; |
| 3538 | |
| 3539 | cur = CUR_SCHAR(ctxt->cur, len); |
| 3540 | if ((cur == '.') || (cur == '\\') || (cur == '?') || |
| 3541 | (cur == '*') || (cur == '+') || (cur == '(') || |
| 3542 | (cur == ')') || (cur == '|') || (cur == 0x5B) || |
| 3543 | (cur == 0x5D) || (cur == 0)) |
| 3544 | return(-1); |
| 3545 | return(cur); |
| 3546 | } |
| 3547 | |
| 3548 | /** |
| 3549 | * xmlFAParseCharProp: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3550 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3551 | * |
| 3552 | * [27] charProp ::= IsCategory | IsBlock |
| 3553 | * [28] IsCategory ::= Letters | Marks | Numbers | Punctuation | |
| 3554 | * Separators | Symbols | Others |
| 3555 | * [29] Letters ::= 'L' [ultmo]? |
| 3556 | * [30] Marks ::= 'M' [nce]? |
| 3557 | * [31] Numbers ::= 'N' [dlo]? |
| 3558 | * [32] Punctuation ::= 'P' [cdseifo]? |
| 3559 | * [33] Separators ::= 'Z' [slp]? |
| 3560 | * [34] Symbols ::= 'S' [mcko]? |
| 3561 | * [35] Others ::= 'C' [cfon]? |
| 3562 | * [36] IsBlock ::= 'Is' [a-zA-Z0-9#x2D]+ |
| 3563 | */ |
| 3564 | static void |
| 3565 | xmlFAParseCharProp(xmlRegParserCtxtPtr ctxt) { |
| 3566 | int cur; |
William M. Brack | 779af00 | 2003-08-01 15:55:39 +0000 | [diff] [blame] | 3567 | xmlRegAtomType type = (xmlRegAtomType) 0; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3568 | xmlChar *blockName = NULL; |
| 3569 | |
| 3570 | cur = CUR; |
| 3571 | if (cur == 'L') { |
| 3572 | NEXT; |
| 3573 | cur = CUR; |
| 3574 | if (cur == 'u') { |
| 3575 | NEXT; |
| 3576 | type = XML_REGEXP_LETTER_UPPERCASE; |
| 3577 | } else if (cur == 'l') { |
| 3578 | NEXT; |
| 3579 | type = XML_REGEXP_LETTER_LOWERCASE; |
| 3580 | } else if (cur == 't') { |
| 3581 | NEXT; |
| 3582 | type = XML_REGEXP_LETTER_TITLECASE; |
| 3583 | } else if (cur == 'm') { |
| 3584 | NEXT; |
| 3585 | type = XML_REGEXP_LETTER_MODIFIER; |
| 3586 | } else if (cur == 'o') { |
| 3587 | NEXT; |
| 3588 | type = XML_REGEXP_LETTER_OTHERS; |
| 3589 | } else { |
| 3590 | type = XML_REGEXP_LETTER; |
| 3591 | } |
| 3592 | } else if (cur == 'M') { |
| 3593 | NEXT; |
| 3594 | cur = CUR; |
| 3595 | if (cur == 'n') { |
| 3596 | NEXT; |
| 3597 | /* nonspacing */ |
| 3598 | type = XML_REGEXP_MARK_NONSPACING; |
| 3599 | } else if (cur == 'c') { |
| 3600 | NEXT; |
| 3601 | /* spacing combining */ |
| 3602 | type = XML_REGEXP_MARK_SPACECOMBINING; |
| 3603 | } else if (cur == 'e') { |
| 3604 | NEXT; |
| 3605 | /* enclosing */ |
| 3606 | type = XML_REGEXP_MARK_ENCLOSING; |
| 3607 | } else { |
| 3608 | /* all marks */ |
| 3609 | type = XML_REGEXP_MARK; |
| 3610 | } |
| 3611 | } else if (cur == 'N') { |
| 3612 | NEXT; |
| 3613 | cur = CUR; |
| 3614 | if (cur == 'd') { |
| 3615 | NEXT; |
| 3616 | /* digital */ |
| 3617 | type = XML_REGEXP_NUMBER_DECIMAL; |
| 3618 | } else if (cur == 'l') { |
| 3619 | NEXT; |
| 3620 | /* letter */ |
| 3621 | type = XML_REGEXP_NUMBER_LETTER; |
| 3622 | } else if (cur == 'o') { |
| 3623 | NEXT; |
| 3624 | /* other */ |
| 3625 | type = XML_REGEXP_NUMBER_OTHERS; |
| 3626 | } else { |
| 3627 | /* all numbers */ |
| 3628 | type = XML_REGEXP_NUMBER; |
| 3629 | } |
| 3630 | } else if (cur == 'P') { |
| 3631 | NEXT; |
| 3632 | cur = CUR; |
| 3633 | if (cur == 'c') { |
| 3634 | NEXT; |
| 3635 | /* connector */ |
| 3636 | type = XML_REGEXP_PUNCT_CONNECTOR; |
| 3637 | } else if (cur == 'd') { |
| 3638 | NEXT; |
| 3639 | /* dash */ |
| 3640 | type = XML_REGEXP_PUNCT_DASH; |
| 3641 | } else if (cur == 's') { |
| 3642 | NEXT; |
| 3643 | /* open */ |
| 3644 | type = XML_REGEXP_PUNCT_OPEN; |
| 3645 | } else if (cur == 'e') { |
| 3646 | NEXT; |
| 3647 | /* close */ |
| 3648 | type = XML_REGEXP_PUNCT_CLOSE; |
| 3649 | } else if (cur == 'i') { |
| 3650 | NEXT; |
| 3651 | /* initial quote */ |
| 3652 | type = XML_REGEXP_PUNCT_INITQUOTE; |
| 3653 | } else if (cur == 'f') { |
| 3654 | NEXT; |
| 3655 | /* final quote */ |
| 3656 | type = XML_REGEXP_PUNCT_FINQUOTE; |
| 3657 | } else if (cur == 'o') { |
| 3658 | NEXT; |
| 3659 | /* other */ |
| 3660 | type = XML_REGEXP_PUNCT_OTHERS; |
| 3661 | } else { |
| 3662 | /* all punctuation */ |
| 3663 | type = XML_REGEXP_PUNCT; |
| 3664 | } |
| 3665 | } else if (cur == 'Z') { |
| 3666 | NEXT; |
| 3667 | cur = CUR; |
| 3668 | if (cur == 's') { |
| 3669 | NEXT; |
| 3670 | /* space */ |
| 3671 | type = XML_REGEXP_SEPAR_SPACE; |
| 3672 | } else if (cur == 'l') { |
| 3673 | NEXT; |
| 3674 | /* line */ |
| 3675 | type = XML_REGEXP_SEPAR_LINE; |
| 3676 | } else if (cur == 'p') { |
| 3677 | NEXT; |
| 3678 | /* paragraph */ |
| 3679 | type = XML_REGEXP_SEPAR_PARA; |
| 3680 | } else { |
| 3681 | /* all separators */ |
| 3682 | type = XML_REGEXP_SEPAR; |
| 3683 | } |
| 3684 | } else if (cur == 'S') { |
| 3685 | NEXT; |
| 3686 | cur = CUR; |
| 3687 | if (cur == 'm') { |
| 3688 | NEXT; |
| 3689 | type = XML_REGEXP_SYMBOL_MATH; |
| 3690 | /* math */ |
| 3691 | } else if (cur == 'c') { |
| 3692 | NEXT; |
| 3693 | type = XML_REGEXP_SYMBOL_CURRENCY; |
| 3694 | /* currency */ |
| 3695 | } else if (cur == 'k') { |
| 3696 | NEXT; |
| 3697 | type = XML_REGEXP_SYMBOL_MODIFIER; |
| 3698 | /* modifiers */ |
| 3699 | } else if (cur == 'o') { |
| 3700 | NEXT; |
| 3701 | type = XML_REGEXP_SYMBOL_OTHERS; |
| 3702 | /* other */ |
| 3703 | } else { |
| 3704 | /* all symbols */ |
| 3705 | type = XML_REGEXP_SYMBOL; |
| 3706 | } |
| 3707 | } else if (cur == 'C') { |
| 3708 | NEXT; |
| 3709 | cur = CUR; |
| 3710 | if (cur == 'c') { |
| 3711 | NEXT; |
| 3712 | /* control */ |
| 3713 | type = XML_REGEXP_OTHER_CONTROL; |
| 3714 | } else if (cur == 'f') { |
| 3715 | NEXT; |
| 3716 | /* format */ |
| 3717 | type = XML_REGEXP_OTHER_FORMAT; |
| 3718 | } else if (cur == 'o') { |
| 3719 | NEXT; |
| 3720 | /* private use */ |
| 3721 | type = XML_REGEXP_OTHER_PRIVATE; |
| 3722 | } else if (cur == 'n') { |
| 3723 | NEXT; |
| 3724 | /* not assigned */ |
| 3725 | type = XML_REGEXP_OTHER_NA; |
| 3726 | } else { |
| 3727 | /* all others */ |
| 3728 | type = XML_REGEXP_OTHER; |
| 3729 | } |
| 3730 | } else if (cur == 'I') { |
| 3731 | const xmlChar *start; |
| 3732 | NEXT; |
| 3733 | cur = CUR; |
| 3734 | if (cur != 's') { |
| 3735 | ERROR("IsXXXX expected"); |
| 3736 | return; |
| 3737 | } |
| 3738 | NEXT; |
| 3739 | start = ctxt->cur; |
| 3740 | cur = CUR; |
| 3741 | if (((cur >= 'a') && (cur <= 'z')) || |
| 3742 | ((cur >= 'A') && (cur <= 'Z')) || |
| 3743 | ((cur >= '0') && (cur <= '9')) || |
| 3744 | (cur == 0x2D)) { |
| 3745 | NEXT; |
| 3746 | cur = CUR; |
| 3747 | while (((cur >= 'a') && (cur <= 'z')) || |
| 3748 | ((cur >= 'A') && (cur <= 'Z')) || |
| 3749 | ((cur >= '0') && (cur <= '9')) || |
| 3750 | (cur == 0x2D)) { |
| 3751 | NEXT; |
| 3752 | cur = CUR; |
| 3753 | } |
| 3754 | } |
| 3755 | type = XML_REGEXP_BLOCK_NAME; |
| 3756 | blockName = xmlStrndup(start, ctxt->cur - start); |
| 3757 | } else { |
| 3758 | ERROR("Unknown char property"); |
| 3759 | return; |
| 3760 | } |
| 3761 | if (ctxt->atom == NULL) { |
| 3762 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
| 3763 | if (ctxt->atom != NULL) |
| 3764 | ctxt->atom->valuep = blockName; |
| 3765 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 3766 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 3767 | type, 0, 0, blockName); |
| 3768 | } |
| 3769 | } |
| 3770 | |
| 3771 | /** |
| 3772 | * xmlFAParseCharClassEsc: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3773 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3774 | * |
| 3775 | * [23] charClassEsc ::= ( SingleCharEsc | MultiCharEsc | catEsc | complEsc ) |
| 3776 | * [24] SingleCharEsc ::= '\' [nrt\|.?*+(){}#x2D#x5B#x5D#x5E] |
| 3777 | * [25] catEsc ::= '\p{' charProp '}' |
| 3778 | * [26] complEsc ::= '\P{' charProp '}' |
| 3779 | * [37] MultiCharEsc ::= '.' | ('\' [sSiIcCdDwW]) |
| 3780 | */ |
| 3781 | static void |
| 3782 | xmlFAParseCharClassEsc(xmlRegParserCtxtPtr ctxt) { |
| 3783 | int cur; |
| 3784 | |
| 3785 | if (CUR == '.') { |
| 3786 | if (ctxt->atom == NULL) { |
| 3787 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_ANYCHAR); |
| 3788 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 3789 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 3790 | XML_REGEXP_ANYCHAR, 0, 0, NULL); |
| 3791 | } |
| 3792 | NEXT; |
| 3793 | return; |
| 3794 | } |
| 3795 | if (CUR != '\\') { |
| 3796 | ERROR("Escaped sequence: expecting \\"); |
| 3797 | return; |
| 3798 | } |
| 3799 | NEXT; |
| 3800 | cur = CUR; |
| 3801 | if (cur == 'p') { |
| 3802 | NEXT; |
| 3803 | if (CUR != '{') { |
| 3804 | ERROR("Expecting '{'"); |
| 3805 | return; |
| 3806 | } |
| 3807 | NEXT; |
| 3808 | xmlFAParseCharProp(ctxt); |
| 3809 | if (CUR != '}') { |
| 3810 | ERROR("Expecting '}'"); |
| 3811 | return; |
| 3812 | } |
| 3813 | NEXT; |
| 3814 | } else if (cur == 'P') { |
| 3815 | NEXT; |
| 3816 | if (CUR != '{') { |
| 3817 | ERROR("Expecting '{'"); |
| 3818 | return; |
| 3819 | } |
| 3820 | NEXT; |
| 3821 | xmlFAParseCharProp(ctxt); |
| 3822 | ctxt->atom->neg = 1; |
| 3823 | if (CUR != '}') { |
| 3824 | ERROR("Expecting '}'"); |
| 3825 | return; |
| 3826 | } |
| 3827 | NEXT; |
| 3828 | } else if ((cur == 'n') || (cur == 'r') || (cur == 't') || (cur == '\\') || |
| 3829 | (cur == '|') || (cur == '.') || (cur == '?') || (cur == '*') || |
| 3830 | (cur == '+') || (cur == '(') || (cur == ')') || (cur == '{') || |
| 3831 | (cur == '}') || (cur == 0x2D) || (cur == 0x5B) || (cur == 0x5D) || |
| 3832 | (cur == 0x5E)) { |
| 3833 | if (ctxt->atom == NULL) { |
| 3834 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
Daniel Veillard | 99c394d | 2005-07-14 12:58:49 +0000 | [diff] [blame^] | 3835 | if (ctxt->atom != NULL) { |
| 3836 | switch (cur) { |
| 3837 | case 'n': |
| 3838 | ctxt->atom->codepoint = '\n'; |
| 3839 | break; |
| 3840 | case 'r': |
| 3841 | ctxt->atom->codepoint = '\r'; |
| 3842 | break; |
| 3843 | case 't': |
| 3844 | ctxt->atom->codepoint = '\t'; |
| 3845 | break; |
| 3846 | default: |
| 3847 | ctxt->atom->codepoint = cur; |
| 3848 | } |
| 3849 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3850 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 3851 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 3852 | XML_REGEXP_CHARVAL, cur, cur, NULL); |
| 3853 | } |
| 3854 | NEXT; |
| 3855 | } else if ((cur == 's') || (cur == 'S') || (cur == 'i') || (cur == 'I') || |
| 3856 | (cur == 'c') || (cur == 'C') || (cur == 'd') || (cur == 'D') || |
| 3857 | (cur == 'w') || (cur == 'W')) { |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 3858 | xmlRegAtomType type = XML_REGEXP_ANYSPACE; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3859 | |
| 3860 | switch (cur) { |
| 3861 | case 's': |
| 3862 | type = XML_REGEXP_ANYSPACE; |
| 3863 | break; |
| 3864 | case 'S': |
| 3865 | type = XML_REGEXP_NOTSPACE; |
| 3866 | break; |
| 3867 | case 'i': |
| 3868 | type = XML_REGEXP_INITNAME; |
| 3869 | break; |
| 3870 | case 'I': |
| 3871 | type = XML_REGEXP_NOTINITNAME; |
| 3872 | break; |
| 3873 | case 'c': |
| 3874 | type = XML_REGEXP_NAMECHAR; |
| 3875 | break; |
| 3876 | case 'C': |
| 3877 | type = XML_REGEXP_NOTNAMECHAR; |
| 3878 | break; |
| 3879 | case 'd': |
| 3880 | type = XML_REGEXP_DECIMAL; |
| 3881 | break; |
| 3882 | case 'D': |
| 3883 | type = XML_REGEXP_NOTDECIMAL; |
| 3884 | break; |
| 3885 | case 'w': |
| 3886 | type = XML_REGEXP_REALCHAR; |
| 3887 | break; |
| 3888 | case 'W': |
| 3889 | type = XML_REGEXP_NOTREALCHAR; |
| 3890 | break; |
| 3891 | } |
| 3892 | NEXT; |
| 3893 | if (ctxt->atom == NULL) { |
| 3894 | ctxt->atom = xmlRegNewAtom(ctxt, type); |
| 3895 | } else if (ctxt->atom->type == XML_REGEXP_RANGES) { |
| 3896 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 3897 | type, 0, 0, NULL); |
| 3898 | } |
| 3899 | } |
| 3900 | } |
| 3901 | |
| 3902 | /** |
| 3903 | * xmlFAParseCharRef: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3904 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3905 | * |
| 3906 | * [19] XmlCharRef ::= ( '&#' [0-9]+ ';' ) | (' &#x' [0-9a-fA-F]+ ';' ) |
| 3907 | */ |
| 3908 | static int |
| 3909 | xmlFAParseCharRef(xmlRegParserCtxtPtr ctxt) { |
| 3910 | int ret = 0, cur; |
| 3911 | |
| 3912 | if ((CUR != '&') || (NXT(1) != '#')) |
| 3913 | return(-1); |
| 3914 | NEXT; |
| 3915 | NEXT; |
| 3916 | cur = CUR; |
| 3917 | if (cur == 'x') { |
| 3918 | NEXT; |
| 3919 | cur = CUR; |
| 3920 | if (((cur >= '0') && (cur <= '9')) || |
| 3921 | ((cur >= 'a') && (cur <= 'f')) || |
| 3922 | ((cur >= 'A') && (cur <= 'F'))) { |
| 3923 | while (((cur >= '0') && (cur <= '9')) || |
| 3924 | ((cur >= 'A') && (cur <= 'F'))) { |
| 3925 | if ((cur >= '0') && (cur <= '9')) |
| 3926 | ret = ret * 16 + cur - '0'; |
| 3927 | else if ((cur >= 'a') && (cur <= 'f')) |
| 3928 | ret = ret * 16 + 10 + (cur - 'a'); |
| 3929 | else |
| 3930 | ret = ret * 16 + 10 + (cur - 'A'); |
| 3931 | NEXT; |
| 3932 | cur = CUR; |
| 3933 | } |
| 3934 | } else { |
| 3935 | ERROR("Char ref: expecting [0-9A-F]"); |
| 3936 | return(-1); |
| 3937 | } |
| 3938 | } else { |
| 3939 | if ((cur >= '0') && (cur <= '9')) { |
| 3940 | while ((cur >= '0') && (cur <= '9')) { |
| 3941 | ret = ret * 10 + cur - '0'; |
| 3942 | NEXT; |
| 3943 | cur = CUR; |
| 3944 | } |
| 3945 | } else { |
| 3946 | ERROR("Char ref: expecting [0-9]"); |
| 3947 | return(-1); |
| 3948 | } |
| 3949 | } |
| 3950 | if (cur != ';') { |
| 3951 | ERROR("Char ref: expecting ';'"); |
| 3952 | return(-1); |
| 3953 | } else { |
| 3954 | NEXT; |
| 3955 | } |
| 3956 | return(ret); |
| 3957 | } |
| 3958 | |
| 3959 | /** |
| 3960 | * xmlFAParseCharRange: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 3961 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3962 | * |
| 3963 | * [17] charRange ::= seRange | XmlCharRef | XmlCharIncDash |
| 3964 | * [18] seRange ::= charOrEsc '-' charOrEsc |
| 3965 | * [20] charOrEsc ::= XmlChar | SingleCharEsc |
| 3966 | * [21] XmlChar ::= [^\#x2D#x5B#x5D] |
| 3967 | * [22] XmlCharIncDash ::= [^\#x5B#x5D] |
| 3968 | */ |
| 3969 | static void |
| 3970 | xmlFAParseCharRange(xmlRegParserCtxtPtr ctxt) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 3971 | int cur, len; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3972 | int start = -1; |
| 3973 | int end = -1; |
| 3974 | |
| 3975 | if ((CUR == '&') && (NXT(1) == '#')) { |
| 3976 | end = start = xmlFAParseCharRef(ctxt); |
| 3977 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 3978 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 3979 | return; |
| 3980 | } |
| 3981 | cur = CUR; |
| 3982 | if (cur == '\\') { |
| 3983 | NEXT; |
| 3984 | cur = CUR; |
| 3985 | switch (cur) { |
| 3986 | case 'n': start = 0xA; break; |
| 3987 | case 'r': start = 0xD; break; |
| 3988 | case 't': start = 0x9; break; |
| 3989 | case '\\': case '|': case '.': case '-': case '^': case '?': |
| 3990 | case '*': case '+': case '{': case '}': case '(': case ')': |
| 3991 | case '[': case ']': |
| 3992 | start = cur; break; |
| 3993 | default: |
| 3994 | ERROR("Invalid escape value"); |
| 3995 | return; |
| 3996 | } |
| 3997 | end = start; |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 3998 | len = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 3999 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4000 | end = start = CUR_SCHAR(ctxt->cur, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4001 | } else { |
| 4002 | ERROR("Expecting a char range"); |
| 4003 | return; |
| 4004 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4005 | NEXTL(len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4006 | if (start == '-') { |
| 4007 | return; |
| 4008 | } |
| 4009 | cur = CUR; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4010 | if ((cur != '-') || (NXT(1) == ']')) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4011 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4012 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4013 | return; |
| 4014 | } |
| 4015 | NEXT; |
| 4016 | cur = CUR; |
| 4017 | if (cur == '\\') { |
| 4018 | NEXT; |
| 4019 | cur = CUR; |
| 4020 | switch (cur) { |
| 4021 | case 'n': end = 0xA; break; |
| 4022 | case 'r': end = 0xD; break; |
| 4023 | case 't': end = 0x9; break; |
| 4024 | case '\\': case '|': case '.': case '-': case '^': case '?': |
| 4025 | case '*': case '+': case '{': case '}': case '(': case ')': |
| 4026 | case '[': case ']': |
| 4027 | end = cur; break; |
| 4028 | default: |
| 4029 | ERROR("Invalid escape value"); |
| 4030 | return; |
| 4031 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4032 | len = 1; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4033 | } else if ((cur != 0x5B) && (cur != 0x5D)) { |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4034 | end = CUR_SCHAR(ctxt->cur, len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4035 | } else { |
| 4036 | ERROR("Expecting the end of a char range"); |
| 4037 | return; |
| 4038 | } |
William M. Brack | dc99df9 | 2003-12-27 01:54:25 +0000 | [diff] [blame] | 4039 | NEXTL(len); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4040 | /* TODO check that the values are acceptable character ranges for XML */ |
| 4041 | if (end < start) { |
| 4042 | ERROR("End of range is before start of range"); |
| 4043 | } else { |
| 4044 | xmlRegAtomAddRange(ctxt, ctxt->atom, ctxt->neg, |
| 4045 | XML_REGEXP_CHARVAL, start, end, NULL); |
| 4046 | } |
| 4047 | return; |
| 4048 | } |
| 4049 | |
| 4050 | /** |
| 4051 | * xmlFAParsePosCharGroup: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4052 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4053 | * |
| 4054 | * [14] posCharGroup ::= ( charRange | charClassEsc )+ |
| 4055 | */ |
| 4056 | static void |
| 4057 | xmlFAParsePosCharGroup(xmlRegParserCtxtPtr ctxt) { |
| 4058 | do { |
| 4059 | if ((CUR == '\\') || (CUR == '.')) { |
| 4060 | xmlFAParseCharClassEsc(ctxt); |
| 4061 | } else { |
| 4062 | xmlFAParseCharRange(ctxt); |
| 4063 | } |
| 4064 | } while ((CUR != ']') && (CUR != '^') && (CUR != '-') && |
| 4065 | (ctxt->error == 0)); |
| 4066 | } |
| 4067 | |
| 4068 | /** |
| 4069 | * xmlFAParseCharGroup: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4070 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4071 | * |
| 4072 | * [13] charGroup ::= posCharGroup | negCharGroup | charClassSub |
| 4073 | * [15] negCharGroup ::= '^' posCharGroup |
| 4074 | * [16] charClassSub ::= ( posCharGroup | negCharGroup ) '-' charClassExpr |
| 4075 | * [12] charClassExpr ::= '[' charGroup ']' |
| 4076 | */ |
| 4077 | static void |
| 4078 | xmlFAParseCharGroup(xmlRegParserCtxtPtr ctxt) { |
| 4079 | int n = ctxt->neg; |
| 4080 | while ((CUR != ']') && (ctxt->error == 0)) { |
| 4081 | if (CUR == '^') { |
| 4082 | int neg = ctxt->neg; |
| 4083 | |
| 4084 | NEXT; |
| 4085 | ctxt->neg = !ctxt->neg; |
| 4086 | xmlFAParsePosCharGroup(ctxt); |
| 4087 | ctxt->neg = neg; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4088 | } else if ((CUR == '-') && (NXT(1) == '[')) { |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4089 | int neg = ctxt->neg; |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4090 | ctxt->neg = 2; |
William M. Brack | 10f1ef4 | 2004-03-20 14:51:25 +0000 | [diff] [blame] | 4091 | NEXT; /* eat the '-' */ |
| 4092 | NEXT; /* eat the '[' */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4093 | xmlFAParseCharGroup(ctxt); |
| 4094 | if (CUR == ']') { |
| 4095 | NEXT; |
| 4096 | } else { |
| 4097 | ERROR("charClassExpr: ']' expected"); |
| 4098 | break; |
| 4099 | } |
Daniel Veillard | f8b9de3 | 2003-11-24 14:27:26 +0000 | [diff] [blame] | 4100 | ctxt->neg = neg; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4101 | break; |
| 4102 | } else if (CUR != ']') { |
| 4103 | xmlFAParsePosCharGroup(ctxt); |
| 4104 | } |
| 4105 | } |
| 4106 | ctxt->neg = n; |
| 4107 | } |
| 4108 | |
| 4109 | /** |
| 4110 | * xmlFAParseCharClass: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4111 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4112 | * |
| 4113 | * [11] charClass ::= charClassEsc | charClassExpr |
| 4114 | * [12] charClassExpr ::= '[' charGroup ']' |
| 4115 | */ |
| 4116 | static void |
| 4117 | xmlFAParseCharClass(xmlRegParserCtxtPtr ctxt) { |
| 4118 | if (CUR == '[') { |
| 4119 | NEXT; |
| 4120 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_RANGES); |
| 4121 | if (ctxt->atom == NULL) |
| 4122 | return; |
| 4123 | xmlFAParseCharGroup(ctxt); |
| 4124 | if (CUR == ']') { |
| 4125 | NEXT; |
| 4126 | } else { |
| 4127 | ERROR("xmlFAParseCharClass: ']' expected"); |
| 4128 | } |
| 4129 | } else { |
| 4130 | xmlFAParseCharClassEsc(ctxt); |
| 4131 | } |
| 4132 | } |
| 4133 | |
| 4134 | /** |
| 4135 | * xmlFAParseQuantExact: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4136 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4137 | * |
| 4138 | * [8] QuantExact ::= [0-9]+ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4139 | * |
| 4140 | * Returns 0 if success or -1 in case of error |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4141 | */ |
| 4142 | static int |
| 4143 | xmlFAParseQuantExact(xmlRegParserCtxtPtr ctxt) { |
| 4144 | int ret = 0; |
| 4145 | int ok = 0; |
| 4146 | |
| 4147 | while ((CUR >= '0') && (CUR <= '9')) { |
| 4148 | ret = ret * 10 + (CUR - '0'); |
| 4149 | ok = 1; |
| 4150 | NEXT; |
| 4151 | } |
| 4152 | if (ok != 1) { |
| 4153 | return(-1); |
| 4154 | } |
| 4155 | return(ret); |
| 4156 | } |
| 4157 | |
| 4158 | /** |
| 4159 | * xmlFAParseQuantifier: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4160 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4161 | * |
| 4162 | * [4] quantifier ::= [?*+] | ( '{' quantity '}' ) |
| 4163 | * [5] quantity ::= quantRange | quantMin | QuantExact |
| 4164 | * [6] quantRange ::= QuantExact ',' QuantExact |
| 4165 | * [7] quantMin ::= QuantExact ',' |
| 4166 | * [8] QuantExact ::= [0-9]+ |
| 4167 | */ |
| 4168 | static int |
| 4169 | xmlFAParseQuantifier(xmlRegParserCtxtPtr ctxt) { |
| 4170 | int cur; |
| 4171 | |
| 4172 | cur = CUR; |
| 4173 | if ((cur == '?') || (cur == '*') || (cur == '+')) { |
| 4174 | if (ctxt->atom != NULL) { |
| 4175 | if (cur == '?') |
| 4176 | ctxt->atom->quant = XML_REGEXP_QUANT_OPT; |
| 4177 | else if (cur == '*') |
| 4178 | ctxt->atom->quant = XML_REGEXP_QUANT_MULT; |
| 4179 | else if (cur == '+') |
| 4180 | ctxt->atom->quant = XML_REGEXP_QUANT_PLUS; |
| 4181 | } |
| 4182 | NEXT; |
| 4183 | return(1); |
| 4184 | } |
| 4185 | if (cur == '{') { |
| 4186 | int min = 0, max = 0; |
| 4187 | |
| 4188 | NEXT; |
| 4189 | cur = xmlFAParseQuantExact(ctxt); |
| 4190 | if (cur >= 0) |
| 4191 | min = cur; |
| 4192 | if (CUR == ',') { |
| 4193 | NEXT; |
Daniel Veillard | ebe48c6 | 2003-12-03 12:12:27 +0000 | [diff] [blame] | 4194 | if (CUR == '}') |
| 4195 | max = INT_MAX; |
| 4196 | else { |
| 4197 | cur = xmlFAParseQuantExact(ctxt); |
| 4198 | if (cur >= 0) |
| 4199 | max = cur; |
| 4200 | else { |
| 4201 | ERROR("Improper quantifier"); |
| 4202 | } |
| 4203 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4204 | } |
| 4205 | if (CUR == '}') { |
| 4206 | NEXT; |
| 4207 | } else { |
| 4208 | ERROR("Unterminated quantifier"); |
| 4209 | } |
| 4210 | if (max == 0) |
| 4211 | max = min; |
| 4212 | if (ctxt->atom != NULL) { |
| 4213 | ctxt->atom->quant = XML_REGEXP_QUANT_RANGE; |
| 4214 | ctxt->atom->min = min; |
| 4215 | ctxt->atom->max = max; |
| 4216 | } |
| 4217 | return(1); |
| 4218 | } |
| 4219 | return(0); |
| 4220 | } |
| 4221 | |
| 4222 | /** |
| 4223 | * xmlFAParseAtom: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4224 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4225 | * |
| 4226 | * [9] atom ::= Char | charClass | ( '(' regExp ')' ) |
| 4227 | */ |
| 4228 | static int |
| 4229 | xmlFAParseAtom(xmlRegParserCtxtPtr ctxt) { |
| 4230 | int codepoint, len; |
| 4231 | |
| 4232 | codepoint = xmlFAIsChar(ctxt); |
| 4233 | if (codepoint > 0) { |
| 4234 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_CHARVAL); |
| 4235 | if (ctxt->atom == NULL) |
| 4236 | return(-1); |
| 4237 | codepoint = CUR_SCHAR(ctxt->cur, len); |
| 4238 | ctxt->atom->codepoint = codepoint; |
| 4239 | NEXTL(len); |
| 4240 | return(1); |
| 4241 | } else if (CUR == '|') { |
| 4242 | return(0); |
| 4243 | } else if (CUR == 0) { |
| 4244 | return(0); |
| 4245 | } else if (CUR == ')') { |
| 4246 | return(0); |
| 4247 | } else if (CUR == '(') { |
| 4248 | xmlRegStatePtr start, oldend; |
| 4249 | |
| 4250 | NEXT; |
| 4251 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, NULL); |
| 4252 | start = ctxt->state; |
| 4253 | oldend = ctxt->end; |
| 4254 | ctxt->end = NULL; |
| 4255 | ctxt->atom = NULL; |
| 4256 | xmlFAParseRegExp(ctxt, 0); |
| 4257 | if (CUR == ')') { |
| 4258 | NEXT; |
| 4259 | } else { |
| 4260 | ERROR("xmlFAParseAtom: expecting ')'"); |
| 4261 | } |
| 4262 | ctxt->atom = xmlRegNewAtom(ctxt, XML_REGEXP_SUBREG); |
| 4263 | if (ctxt->atom == NULL) |
| 4264 | return(-1); |
| 4265 | ctxt->atom->start = start; |
| 4266 | ctxt->atom->stop = ctxt->state; |
| 4267 | ctxt->end = oldend; |
| 4268 | return(1); |
| 4269 | } else if ((CUR == '[') || (CUR == '\\') || (CUR == '.')) { |
| 4270 | xmlFAParseCharClass(ctxt); |
| 4271 | return(1); |
| 4272 | } |
| 4273 | return(0); |
| 4274 | } |
| 4275 | |
| 4276 | /** |
| 4277 | * xmlFAParsePiece: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4278 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4279 | * |
| 4280 | * [3] piece ::= atom quantifier? |
| 4281 | */ |
| 4282 | static int |
| 4283 | xmlFAParsePiece(xmlRegParserCtxtPtr ctxt) { |
| 4284 | int ret; |
| 4285 | |
| 4286 | ctxt->atom = NULL; |
| 4287 | ret = xmlFAParseAtom(ctxt); |
| 4288 | if (ret == 0) |
| 4289 | return(0); |
| 4290 | if (ctxt->atom == NULL) { |
| 4291 | ERROR("internal: no atom generated"); |
| 4292 | } |
| 4293 | xmlFAParseQuantifier(ctxt); |
| 4294 | return(1); |
| 4295 | } |
| 4296 | |
| 4297 | /** |
| 4298 | * xmlFAParseBranch: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4299 | * @ctxt: a regexp parser context |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4300 | * |
| 4301 | * [2] branch ::= piece* |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4302 | 8 |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4303 | */ |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4304 | static int |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 4305 | xmlFAParseBranch(xmlRegParserCtxtPtr ctxt) { |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4306 | xmlRegStatePtr previous; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4307 | int ret; |
| 4308 | |
| 4309 | previous = ctxt->state; |
| 4310 | ret = xmlFAParsePiece(ctxt); |
| 4311 | if (ret != 0) { |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 4312 | if (xmlFAGenerateTransitions(ctxt, previous, NULL, ctxt->atom) < 0) |
| 4313 | return(-1); |
| 4314 | previous = ctxt->state; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4315 | ctxt->atom = NULL; |
| 4316 | } |
| 4317 | while ((ret != 0) && (ctxt->error == 0)) { |
| 4318 | ret = xmlFAParsePiece(ctxt); |
| 4319 | if (ret != 0) { |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 4320 | if (xmlFAGenerateTransitions(ctxt, previous, NULL, |
| 4321 | ctxt->atom) < 0) |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4322 | return(-1); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4323 | previous = ctxt->state; |
| 4324 | ctxt->atom = NULL; |
| 4325 | } |
| 4326 | } |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4327 | return(0); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4328 | } |
| 4329 | |
| 4330 | /** |
| 4331 | * xmlFAParseRegExp: |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 4332 | * @ctxt: a regexp parser context |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4333 | * @top: is this the top-level expression ? |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4334 | * |
| 4335 | * [1] regExp ::= branch ( '|' branch )* |
| 4336 | */ |
| 4337 | static void |
| 4338 | xmlFAParseRegExp(xmlRegParserCtxtPtr ctxt, int top) { |
Daniel Veillard | c7e3cc4 | 2004-09-28 12:33:52 +0000 | [diff] [blame] | 4339 | xmlRegStatePtr start, end; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4340 | |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 4341 | /* if not top start should have been generated by an epsilon trans */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4342 | start = ctxt->state; |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 4343 | ctxt->end = NULL; |
| 4344 | xmlFAParseBranch(ctxt); |
| 4345 | if (top) { |
| 4346 | #ifdef DEBUG_REGEXP_GRAPH |
| 4347 | printf("State %d is final\n", ctxt->state->no); |
| 4348 | #endif |
| 4349 | ctxt->state->type = XML_REGEXP_FINAL_STATE; |
| 4350 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4351 | if (CUR != '|') { |
| 4352 | ctxt->end = ctxt->state; |
| 4353 | return; |
| 4354 | } |
| 4355 | end = ctxt->state; |
| 4356 | while ((CUR == '|') && (ctxt->error == 0)) { |
| 4357 | NEXT; |
| 4358 | ctxt->state = start; |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 4359 | ctxt->end = NULL; |
| 4360 | xmlFAParseBranch(ctxt); |
| 4361 | if (top) { |
| 4362 | ctxt->state->type = XML_REGEXP_FINAL_STATE; |
| 4363 | #ifdef DEBUG_REGEXP_GRAPH |
| 4364 | printf("State %d is final\n", ctxt->state->no); |
| 4365 | #endif |
| 4366 | } else { |
| 4367 | xmlFAGenerateEpsilonTransition(ctxt, ctxt->state, end); |
| 4368 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4369 | } |
Daniel Veillard | 2cbf596 | 2004-03-31 15:50:43 +0000 | [diff] [blame] | 4370 | if (!top) { |
| 4371 | ctxt->state = end; |
| 4372 | ctxt->end = end; |
| 4373 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4374 | } |
| 4375 | |
| 4376 | /************************************************************************ |
| 4377 | * * |
| 4378 | * The basic API * |
| 4379 | * * |
| 4380 | ************************************************************************/ |
| 4381 | |
| 4382 | /** |
| 4383 | * xmlRegexpPrint: |
| 4384 | * @output: the file for the output debug |
| 4385 | * @regexp: the compiled regexp |
| 4386 | * |
| 4387 | * Print the content of the compiled regular expression |
| 4388 | */ |
| 4389 | void |
| 4390 | xmlRegexpPrint(FILE *output, xmlRegexpPtr regexp) { |
| 4391 | int i; |
| 4392 | |
Daniel Veillard | a82b182 | 2004-11-08 16:24:57 +0000 | [diff] [blame] | 4393 | if (output == NULL) |
| 4394 | return; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4395 | fprintf(output, " regexp: "); |
| 4396 | if (regexp == NULL) { |
| 4397 | fprintf(output, "NULL\n"); |
| 4398 | return; |
| 4399 | } |
| 4400 | fprintf(output, "'%s' ", regexp->string); |
| 4401 | fprintf(output, "\n"); |
| 4402 | fprintf(output, "%d atoms:\n", regexp->nbAtoms); |
| 4403 | for (i = 0;i < regexp->nbAtoms; i++) { |
| 4404 | fprintf(output, " %02d ", i); |
| 4405 | xmlRegPrintAtom(output, regexp->atoms[i]); |
| 4406 | } |
| 4407 | fprintf(output, "%d states:", regexp->nbStates); |
| 4408 | fprintf(output, "\n"); |
| 4409 | for (i = 0;i < regexp->nbStates; i++) { |
| 4410 | xmlRegPrintState(output, regexp->states[i]); |
| 4411 | } |
| 4412 | fprintf(output, "%d counters:\n", regexp->nbCounters); |
| 4413 | for (i = 0;i < regexp->nbCounters; i++) { |
| 4414 | fprintf(output, " %d: min %d max %d\n", i, regexp->counters[i].min, |
| 4415 | regexp->counters[i].max); |
| 4416 | } |
| 4417 | } |
| 4418 | |
| 4419 | /** |
| 4420 | * xmlRegexpCompile: |
| 4421 | * @regexp: a regular expression string |
| 4422 | * |
| 4423 | * Parses a regular expression conforming to XML Schemas Part 2 Datatype |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4424 | * Appendix F and builds an automata suitable for testing strings against |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4425 | * that regular expression |
| 4426 | * |
| 4427 | * Returns the compiled expression or NULL in case of error |
| 4428 | */ |
| 4429 | xmlRegexpPtr |
| 4430 | xmlRegexpCompile(const xmlChar *regexp) { |
| 4431 | xmlRegexpPtr ret; |
| 4432 | xmlRegParserCtxtPtr ctxt; |
| 4433 | |
| 4434 | ctxt = xmlRegNewParserCtxt(regexp); |
| 4435 | if (ctxt == NULL) |
| 4436 | return(NULL); |
| 4437 | |
| 4438 | /* initialize the parser */ |
| 4439 | ctxt->end = NULL; |
| 4440 | ctxt->start = ctxt->state = xmlRegNewState(ctxt); |
| 4441 | xmlRegStatePush(ctxt, ctxt->start); |
| 4442 | |
| 4443 | /* parse the expression building an automata */ |
| 4444 | xmlFAParseRegExp(ctxt, 1); |
| 4445 | if (CUR != 0) { |
| 4446 | ERROR("xmlFAParseRegExp: extra characters"); |
| 4447 | } |
| 4448 | ctxt->end = ctxt->state; |
| 4449 | ctxt->start->type = XML_REGEXP_START_STATE; |
| 4450 | ctxt->end->type = XML_REGEXP_FINAL_STATE; |
| 4451 | |
| 4452 | /* remove the Epsilon except for counted transitions */ |
| 4453 | xmlFAEliminateEpsilonTransitions(ctxt); |
| 4454 | |
| 4455 | |
| 4456 | if (ctxt->error != 0) { |
| 4457 | xmlRegFreeParserCtxt(ctxt); |
| 4458 | return(NULL); |
| 4459 | } |
| 4460 | ret = xmlRegEpxFromParse(ctxt); |
| 4461 | xmlRegFreeParserCtxt(ctxt); |
| 4462 | return(ret); |
| 4463 | } |
| 4464 | |
| 4465 | /** |
| 4466 | * xmlRegexpExec: |
| 4467 | * @comp: the compiled regular expression |
| 4468 | * @content: the value to check against the regular expression |
| 4469 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4470 | * Check if the regular expression generates the value |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4471 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4472 | * 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] | 4473 | */ |
| 4474 | int |
| 4475 | xmlRegexpExec(xmlRegexpPtr comp, const xmlChar *content) { |
| 4476 | if ((comp == NULL) || (content == NULL)) |
| 4477 | return(-1); |
| 4478 | return(xmlFARegExec(comp, content)); |
| 4479 | } |
| 4480 | |
| 4481 | /** |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4482 | * xmlRegexpIsDeterminist: |
| 4483 | * @comp: the compiled regular expression |
| 4484 | * |
| 4485 | * Check if the regular expression is determinist |
| 4486 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4487 | * 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] | 4488 | */ |
| 4489 | int |
| 4490 | xmlRegexpIsDeterminist(xmlRegexpPtr comp) { |
| 4491 | xmlAutomataPtr am; |
| 4492 | int ret; |
| 4493 | |
| 4494 | if (comp == NULL) |
| 4495 | return(-1); |
| 4496 | if (comp->determinist != -1) |
| 4497 | return(comp->determinist); |
| 4498 | |
| 4499 | am = xmlNewAutomata(); |
Daniel Veillard | bd9afb5 | 2002-09-25 22:25:35 +0000 | [diff] [blame] | 4500 | if (am->states != NULL) { |
| 4501 | int i; |
| 4502 | |
| 4503 | for (i = 0;i < am->nbStates;i++) |
| 4504 | xmlRegFreeState(am->states[i]); |
| 4505 | xmlFree(am->states); |
| 4506 | } |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4507 | am->nbAtoms = comp->nbAtoms; |
| 4508 | am->atoms = comp->atoms; |
| 4509 | am->nbStates = comp->nbStates; |
| 4510 | am->states = comp->states; |
| 4511 | am->determinist = -1; |
| 4512 | ret = xmlFAComputesDeterminism(am); |
| 4513 | am->atoms = NULL; |
| 4514 | am->states = NULL; |
| 4515 | xmlFreeAutomata(am); |
| 4516 | return(ret); |
| 4517 | } |
| 4518 | |
| 4519 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4520 | * xmlRegFreeRegexp: |
| 4521 | * @regexp: the regexp |
| 4522 | * |
| 4523 | * Free a regexp |
| 4524 | */ |
| 4525 | void |
| 4526 | xmlRegFreeRegexp(xmlRegexpPtr regexp) { |
| 4527 | int i; |
| 4528 | if (regexp == NULL) |
| 4529 | return; |
| 4530 | |
| 4531 | if (regexp->string != NULL) |
| 4532 | xmlFree(regexp->string); |
| 4533 | if (regexp->states != NULL) { |
| 4534 | for (i = 0;i < regexp->nbStates;i++) |
| 4535 | xmlRegFreeState(regexp->states[i]); |
| 4536 | xmlFree(regexp->states); |
| 4537 | } |
| 4538 | if (regexp->atoms != NULL) { |
| 4539 | for (i = 0;i < regexp->nbAtoms;i++) |
| 4540 | xmlRegFreeAtom(regexp->atoms[i]); |
| 4541 | xmlFree(regexp->atoms); |
| 4542 | } |
| 4543 | if (regexp->counters != NULL) |
| 4544 | xmlFree(regexp->counters); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4545 | if (regexp->compact != NULL) |
| 4546 | xmlFree(regexp->compact); |
Daniel Veillard | 118aed7 | 2002-09-24 14:13:13 +0000 | [diff] [blame] | 4547 | if (regexp->transdata != NULL) |
| 4548 | xmlFree(regexp->transdata); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 4549 | if (regexp->stringMap != NULL) { |
| 4550 | for (i = 0; i < regexp->nbstrings;i++) |
| 4551 | xmlFree(regexp->stringMap[i]); |
| 4552 | xmlFree(regexp->stringMap); |
| 4553 | } |
| 4554 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4555 | xmlFree(regexp); |
| 4556 | } |
| 4557 | |
| 4558 | #ifdef LIBXML_AUTOMATA_ENABLED |
| 4559 | /************************************************************************ |
| 4560 | * * |
| 4561 | * The Automata interface * |
| 4562 | * * |
| 4563 | ************************************************************************/ |
| 4564 | |
| 4565 | /** |
| 4566 | * xmlNewAutomata: |
| 4567 | * |
| 4568 | * Create a new automata |
| 4569 | * |
| 4570 | * Returns the new object or NULL in case of failure |
| 4571 | */ |
| 4572 | xmlAutomataPtr |
| 4573 | xmlNewAutomata(void) { |
| 4574 | xmlAutomataPtr ctxt; |
| 4575 | |
| 4576 | ctxt = xmlRegNewParserCtxt(NULL); |
| 4577 | if (ctxt == NULL) |
| 4578 | return(NULL); |
| 4579 | |
| 4580 | /* initialize the parser */ |
| 4581 | ctxt->end = NULL; |
| 4582 | ctxt->start = ctxt->state = xmlRegNewState(ctxt); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4583 | if (ctxt->start == NULL) { |
| 4584 | xmlFreeAutomata(ctxt); |
| 4585 | return(NULL); |
| 4586 | } |
| 4587 | if (xmlRegStatePush(ctxt, ctxt->start) < 0) { |
| 4588 | xmlRegFreeState(ctxt->start); |
| 4589 | xmlFreeAutomata(ctxt); |
| 4590 | return(NULL); |
| 4591 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4592 | |
| 4593 | return(ctxt); |
| 4594 | } |
| 4595 | |
| 4596 | /** |
| 4597 | * xmlFreeAutomata: |
| 4598 | * @am: an automata |
| 4599 | * |
| 4600 | * Free an automata |
| 4601 | */ |
| 4602 | void |
| 4603 | xmlFreeAutomata(xmlAutomataPtr am) { |
| 4604 | if (am == NULL) |
| 4605 | return; |
| 4606 | xmlRegFreeParserCtxt(am); |
| 4607 | } |
| 4608 | |
| 4609 | /** |
| 4610 | * xmlAutomataGetInitState: |
| 4611 | * @am: an automata |
| 4612 | * |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4613 | * Initial state lookup |
| 4614 | * |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4615 | * Returns the initial state of the automata |
| 4616 | */ |
| 4617 | xmlAutomataStatePtr |
| 4618 | xmlAutomataGetInitState(xmlAutomataPtr am) { |
| 4619 | if (am == NULL) |
| 4620 | return(NULL); |
| 4621 | return(am->start); |
| 4622 | } |
| 4623 | |
| 4624 | /** |
| 4625 | * xmlAutomataSetFinalState: |
| 4626 | * @am: an automata |
| 4627 | * @state: a state in this automata |
| 4628 | * |
| 4629 | * Makes that state a final state |
| 4630 | * |
| 4631 | * Returns 0 or -1 in case of error |
| 4632 | */ |
| 4633 | int |
| 4634 | xmlAutomataSetFinalState(xmlAutomataPtr am, xmlAutomataStatePtr state) { |
| 4635 | if ((am == NULL) || (state == NULL)) |
| 4636 | return(-1); |
| 4637 | state->type = XML_REGEXP_FINAL_STATE; |
| 4638 | return(0); |
| 4639 | } |
| 4640 | |
| 4641 | /** |
| 4642 | * xmlAutomataNewTransition: |
| 4643 | * @am: an automata |
| 4644 | * @from: the starting point of the transition |
| 4645 | * @to: the target point of the transition or NULL |
| 4646 | * @token: the input string associated to that transition |
| 4647 | * @data: data passed to the callback function if the transition is activated |
| 4648 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4649 | * 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] | 4650 | * and then adds a transition from the @from state to the target state |
| 4651 | * activated by the value of @token |
| 4652 | * |
| 4653 | * Returns the target state or NULL in case of error |
| 4654 | */ |
| 4655 | xmlAutomataStatePtr |
| 4656 | xmlAutomataNewTransition(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 4657 | xmlAutomataStatePtr to, const xmlChar *token, |
| 4658 | void *data) { |
| 4659 | xmlRegAtomPtr atom; |
| 4660 | |
| 4661 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 4662 | return(NULL); |
| 4663 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4664 | if (atom == NULL) |
| 4665 | return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4666 | atom->data = data; |
| 4667 | if (atom == NULL) |
| 4668 | return(NULL); |
| 4669 | atom->valuep = xmlStrdup(token); |
| 4670 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4671 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 4672 | xmlRegFreeAtom(atom); |
| 4673 | return(NULL); |
| 4674 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4675 | if (to == NULL) |
| 4676 | return(am->state); |
| 4677 | return(to); |
| 4678 | } |
| 4679 | |
| 4680 | /** |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 4681 | * xmlAutomataNewTransition2: |
| 4682 | * @am: an automata |
| 4683 | * @from: the starting point of the transition |
| 4684 | * @to: the target point of the transition or NULL |
| 4685 | * @token: the first input string associated to that transition |
| 4686 | * @token2: the second input string associated to that transition |
| 4687 | * @data: data passed to the callback function if the transition is activated |
| 4688 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4689 | * 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] | 4690 | * and then adds a transition from the @from state to the target state |
| 4691 | * activated by the value of @token |
| 4692 | * |
| 4693 | * Returns the target state or NULL in case of error |
| 4694 | */ |
| 4695 | xmlAutomataStatePtr |
| 4696 | xmlAutomataNewTransition2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 4697 | xmlAutomataStatePtr to, const xmlChar *token, |
| 4698 | const xmlChar *token2, void *data) { |
| 4699 | xmlRegAtomPtr atom; |
| 4700 | |
| 4701 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 4702 | return(NULL); |
| 4703 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 4704 | atom->data = data; |
| 4705 | if (atom == NULL) |
| 4706 | return(NULL); |
| 4707 | if ((token2 == NULL) || (*token2 == 0)) { |
| 4708 | atom->valuep = xmlStrdup(token); |
| 4709 | } else { |
| 4710 | int lenn, lenp; |
| 4711 | xmlChar *str; |
| 4712 | |
| 4713 | lenn = strlen((char *) token2); |
| 4714 | lenp = strlen((char *) token); |
| 4715 | |
Daniel Veillard | 3c908dc | 2003-04-19 00:07:51 +0000 | [diff] [blame] | 4716 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 4717 | if (str == NULL) { |
| 4718 | xmlRegFreeAtom(atom); |
| 4719 | return(NULL); |
| 4720 | } |
| 4721 | memcpy(&str[0], token, lenp); |
| 4722 | str[lenp] = '|'; |
| 4723 | memcpy(&str[lenp + 1], token2, lenn); |
| 4724 | str[lenn + lenp + 1] = 0; |
| 4725 | |
| 4726 | atom->valuep = str; |
| 4727 | } |
| 4728 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4729 | if (xmlFAGenerateTransitions(am, from, to, atom) < 0) { |
| 4730 | xmlRegFreeAtom(atom); |
| 4731 | return(NULL); |
| 4732 | } |
Daniel Veillard | 52b48c7 | 2003-04-13 19:53:42 +0000 | [diff] [blame] | 4733 | if (to == NULL) |
| 4734 | return(am->state); |
| 4735 | return(to); |
| 4736 | } |
| 4737 | |
| 4738 | /** |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 4739 | * xmlAutomataNewCountTrans2: |
| 4740 | * @am: an automata |
| 4741 | * @from: the starting point of the transition |
| 4742 | * @to: the target point of the transition or NULL |
| 4743 | * @token: the input string associated to that transition |
| 4744 | * @token2: the second input string associated to that transition |
| 4745 | * @min: the minimum successive occurences of token |
| 4746 | * @max: the maximum successive occurences of token |
| 4747 | * @data: data associated to the transition |
| 4748 | * |
| 4749 | * If @to is NULL, this creates first a new target state in the automata |
| 4750 | * and then adds a transition from the @from state to the target state |
| 4751 | * activated by a succession of input of value @token and @token2 and |
| 4752 | * whose number is between @min and @max |
| 4753 | * |
| 4754 | * Returns the target state or NULL in case of error |
| 4755 | */ |
| 4756 | xmlAutomataStatePtr |
| 4757 | xmlAutomataNewCountTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 4758 | xmlAutomataStatePtr to, const xmlChar *token, |
| 4759 | const xmlChar *token2, |
| 4760 | int min, int max, void *data) { |
| 4761 | xmlRegAtomPtr atom; |
| 4762 | int counter; |
| 4763 | |
| 4764 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 4765 | return(NULL); |
| 4766 | if (min < 0) |
| 4767 | return(NULL); |
| 4768 | if ((max < min) || (max < 1)) |
| 4769 | return(NULL); |
| 4770 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 4771 | if (atom == NULL) |
| 4772 | return(NULL); |
| 4773 | if ((token2 == NULL) || (*token2 == 0)) { |
| 4774 | atom->valuep = xmlStrdup(token); |
| 4775 | } else { |
| 4776 | int lenn, lenp; |
| 4777 | xmlChar *str; |
| 4778 | |
| 4779 | lenn = strlen((char *) token2); |
| 4780 | lenp = strlen((char *) token); |
| 4781 | |
| 4782 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 4783 | if (str == NULL) { |
| 4784 | xmlRegFreeAtom(atom); |
| 4785 | return(NULL); |
| 4786 | } |
| 4787 | memcpy(&str[0], token, lenp); |
| 4788 | str[lenp] = '|'; |
| 4789 | memcpy(&str[lenp + 1], token2, lenn); |
| 4790 | str[lenn + lenp + 1] = 0; |
| 4791 | |
| 4792 | atom->valuep = str; |
| 4793 | } |
| 4794 | atom->data = data; |
| 4795 | if (min == 0) |
| 4796 | atom->min = 1; |
| 4797 | else |
| 4798 | atom->min = min; |
| 4799 | atom->max = max; |
| 4800 | |
| 4801 | /* |
| 4802 | * associate a counter to the transition. |
| 4803 | */ |
| 4804 | counter = xmlRegGetCounter(am); |
| 4805 | am->counters[counter].min = min; |
| 4806 | am->counters[counter].max = max; |
| 4807 | |
| 4808 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 4809 | if (to == NULL) { |
| 4810 | to = xmlRegNewState(am); |
| 4811 | xmlRegStatePush(am, to); |
| 4812 | } |
| 4813 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
| 4814 | xmlRegAtomPush(am, atom); |
| 4815 | am->state = to; |
| 4816 | |
| 4817 | if (to == NULL) |
| 4818 | to = am->state; |
| 4819 | if (to == NULL) |
| 4820 | return(NULL); |
| 4821 | if (min == 0) |
| 4822 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 4823 | return(to); |
| 4824 | } |
| 4825 | |
| 4826 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4827 | * xmlAutomataNewCountTrans: |
| 4828 | * @am: an automata |
| 4829 | * @from: the starting point of the transition |
| 4830 | * @to: the target point of the transition or NULL |
| 4831 | * @token: the input string associated to that transition |
| 4832 | * @min: the minimum successive occurences of token |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4833 | * @max: the maximum successive occurences of token |
| 4834 | * @data: data associated to the transition |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4835 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4836 | * 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] | 4837 | * and then adds a transition from the @from state to the target state |
| 4838 | * activated by a succession of input of value @token and whose number |
| 4839 | * is between @min and @max |
| 4840 | * |
| 4841 | * Returns the target state or NULL in case of error |
| 4842 | */ |
| 4843 | xmlAutomataStatePtr |
| 4844 | xmlAutomataNewCountTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 4845 | xmlAutomataStatePtr to, const xmlChar *token, |
| 4846 | int min, int max, void *data) { |
| 4847 | xmlRegAtomPtr atom; |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 4848 | int counter; |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4849 | |
| 4850 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 4851 | return(NULL); |
| 4852 | if (min < 0) |
| 4853 | return(NULL); |
| 4854 | if ((max < min) || (max < 1)) |
| 4855 | return(NULL); |
| 4856 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 4857 | if (atom == NULL) |
| 4858 | return(NULL); |
| 4859 | atom->valuep = xmlStrdup(token); |
| 4860 | atom->data = data; |
| 4861 | if (min == 0) |
| 4862 | atom->min = 1; |
| 4863 | else |
| 4864 | atom->min = min; |
| 4865 | atom->max = max; |
| 4866 | |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 4867 | /* |
| 4868 | * associate a counter to the transition. |
| 4869 | */ |
| 4870 | counter = xmlRegGetCounter(am); |
| 4871 | am->counters[counter].min = min; |
| 4872 | am->counters[counter].max = max; |
| 4873 | |
| 4874 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 4875 | if (to == NULL) { |
| 4876 | to = xmlRegNewState(am); |
| 4877 | xmlRegStatePush(am, to); |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 4878 | } |
Daniel Veillard | 0ddb21c | 2004-02-12 12:43:49 +0000 | [diff] [blame] | 4879 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
| 4880 | xmlRegAtomPush(am, atom); |
| 4881 | am->state = to; |
| 4882 | |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 4883 | if (to == NULL) |
| 4884 | to = am->state; |
| 4885 | if (to == NULL) |
| 4886 | return(NULL); |
| 4887 | if (min == 0) |
| 4888 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 4889 | return(to); |
| 4890 | } |
| 4891 | |
| 4892 | /** |
Kasimier T. Buchcik | 8787640 | 2004-09-29 13:29:03 +0000 | [diff] [blame] | 4893 | * xmlAutomataNewOnceTrans2: |
| 4894 | * @am: an automata |
| 4895 | * @from: the starting point of the transition |
| 4896 | * @to: the target point of the transition or NULL |
| 4897 | * @token: the input string associated to that transition |
| 4898 | * @token2: the second input string associated to that transition |
| 4899 | * @min: the minimum successive occurences of token |
| 4900 | * @max: the maximum successive occurences of token |
| 4901 | * @data: data associated to the transition |
| 4902 | * |
| 4903 | * If @to is NULL, this creates first a new target state in the automata |
| 4904 | * and then adds a transition from the @from state to the target state |
| 4905 | * activated by a succession of input of value @token and @token2 and whose |
| 4906 | * number is between @min and @max, moreover that transition can only be |
| 4907 | * crossed once. |
| 4908 | * |
| 4909 | * Returns the target state or NULL in case of error |
| 4910 | */ |
| 4911 | xmlAutomataStatePtr |
| 4912 | xmlAutomataNewOnceTrans2(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 4913 | xmlAutomataStatePtr to, const xmlChar *token, |
| 4914 | const xmlChar *token2, |
| 4915 | int min, int max, void *data) { |
| 4916 | xmlRegAtomPtr atom; |
| 4917 | int counter; |
| 4918 | |
| 4919 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 4920 | return(NULL); |
| 4921 | if (min < 1) |
| 4922 | return(NULL); |
| 4923 | if ((max < min) || (max < 1)) |
| 4924 | return(NULL); |
| 4925 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 4926 | if (atom == NULL) |
| 4927 | return(NULL); |
| 4928 | if ((token2 == NULL) || (*token2 == 0)) { |
| 4929 | atom->valuep = xmlStrdup(token); |
| 4930 | } else { |
| 4931 | int lenn, lenp; |
| 4932 | xmlChar *str; |
| 4933 | |
| 4934 | lenn = strlen((char *) token2); |
| 4935 | lenp = strlen((char *) token); |
| 4936 | |
| 4937 | str = (xmlChar *) xmlMallocAtomic(lenn + lenp + 2); |
| 4938 | if (str == NULL) { |
| 4939 | xmlRegFreeAtom(atom); |
| 4940 | return(NULL); |
| 4941 | } |
| 4942 | memcpy(&str[0], token, lenp); |
| 4943 | str[lenp] = '|'; |
| 4944 | memcpy(&str[lenp + 1], token2, lenn); |
| 4945 | str[lenn + lenp + 1] = 0; |
| 4946 | |
| 4947 | atom->valuep = str; |
| 4948 | } |
| 4949 | atom->data = data; |
| 4950 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
| 4951 | if (min == 0) |
| 4952 | atom->min = 1; |
| 4953 | else |
| 4954 | atom->min = min; |
| 4955 | atom->max = max; |
| 4956 | /* |
| 4957 | * associate a counter to the transition. |
| 4958 | */ |
| 4959 | counter = xmlRegGetCounter(am); |
| 4960 | am->counters[counter].min = 1; |
| 4961 | am->counters[counter].max = 1; |
| 4962 | |
| 4963 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 4964 | if (to == NULL) { |
| 4965 | to = xmlRegNewState(am); |
| 4966 | xmlRegStatePush(am, to); |
| 4967 | } |
| 4968 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
| 4969 | xmlRegAtomPush(am, atom); |
| 4970 | am->state = to; |
| 4971 | return(to); |
| 4972 | } |
| 4973 | |
| 4974 | |
| 4975 | |
| 4976 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4977 | * xmlAutomataNewOnceTrans: |
| 4978 | * @am: an automata |
| 4979 | * @from: the starting point of the transition |
| 4980 | * @to: the target point of the transition or NULL |
| 4981 | * @token: the input string associated to that transition |
| 4982 | * @min: the minimum successive occurences of token |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 4983 | * @max: the maximum successive occurences of token |
| 4984 | * @data: data associated to the transition |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4985 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 4986 | * 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] | 4987 | * and then adds a transition from the @from state to the target state |
| 4988 | * 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] | 4989 | * is between @min and @max, moreover that transition can only be crossed |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 4990 | * once. |
| 4991 | * |
| 4992 | * Returns the target state or NULL in case of error |
| 4993 | */ |
| 4994 | xmlAutomataStatePtr |
| 4995 | xmlAutomataNewOnceTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 4996 | xmlAutomataStatePtr to, const xmlChar *token, |
| 4997 | int min, int max, void *data) { |
| 4998 | xmlRegAtomPtr atom; |
| 4999 | int counter; |
| 5000 | |
| 5001 | if ((am == NULL) || (from == NULL) || (token == NULL)) |
| 5002 | return(NULL); |
| 5003 | if (min < 1) |
| 5004 | return(NULL); |
| 5005 | if ((max < min) || (max < 1)) |
| 5006 | return(NULL); |
| 5007 | atom = xmlRegNewAtom(am, XML_REGEXP_STRING); |
| 5008 | if (atom == NULL) |
| 5009 | return(NULL); |
| 5010 | atom->valuep = xmlStrdup(token); |
| 5011 | atom->data = data; |
| 5012 | atom->quant = XML_REGEXP_QUANT_ONCEONLY; |
| 5013 | if (min == 0) |
| 5014 | atom->min = 1; |
| 5015 | else |
| 5016 | atom->min = min; |
| 5017 | atom->max = max; |
| 5018 | /* |
| 5019 | * associate a counter to the transition. |
| 5020 | */ |
| 5021 | counter = xmlRegGetCounter(am); |
| 5022 | am->counters[counter].min = 1; |
| 5023 | am->counters[counter].max = 1; |
| 5024 | |
| 5025 | /* xmlFAGenerateTransitions(am, from, to, atom); */ |
| 5026 | if (to == NULL) { |
| 5027 | to = xmlRegNewState(am); |
| 5028 | xmlRegStatePush(am, to); |
| 5029 | } |
| 5030 | xmlRegStateAddTrans(am, from, atom, to, counter, -1); |
| 5031 | xmlRegAtomPush(am, atom); |
| 5032 | am->state = to; |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5033 | return(to); |
| 5034 | } |
| 5035 | |
| 5036 | /** |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5037 | * xmlAutomataNewState: |
| 5038 | * @am: an automata |
| 5039 | * |
| 5040 | * Create a new disconnected state in the automata |
| 5041 | * |
| 5042 | * Returns the new state or NULL in case of error |
| 5043 | */ |
| 5044 | xmlAutomataStatePtr |
| 5045 | xmlAutomataNewState(xmlAutomataPtr am) { |
| 5046 | xmlAutomataStatePtr to; |
| 5047 | |
| 5048 | if (am == NULL) |
| 5049 | return(NULL); |
| 5050 | to = xmlRegNewState(am); |
| 5051 | xmlRegStatePush(am, to); |
| 5052 | return(to); |
| 5053 | } |
| 5054 | |
| 5055 | /** |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5056 | * xmlAutomataNewEpsilon: |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5057 | * @am: an automata |
| 5058 | * @from: the starting point of the transition |
| 5059 | * @to: the target point of the transition or NULL |
| 5060 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5061 | * If @to is NULL, this creates first a new target state in the automata |
| 5062 | * and then adds an epsilon transition from the @from state to the |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5063 | * target state |
| 5064 | * |
| 5065 | * Returns the target state or NULL in case of error |
| 5066 | */ |
| 5067 | xmlAutomataStatePtr |
| 5068 | xmlAutomataNewEpsilon(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5069 | xmlAutomataStatePtr to) { |
| 5070 | if ((am == NULL) || (from == NULL)) |
| 5071 | return(NULL); |
| 5072 | xmlFAGenerateEpsilonTransition(am, from, to); |
| 5073 | if (to == NULL) |
| 5074 | return(am->state); |
| 5075 | return(to); |
| 5076 | } |
| 5077 | |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 5078 | /** |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5079 | * xmlAutomataNewAllTrans: |
| 5080 | * @am: an automata |
| 5081 | * @from: the starting point of the transition |
| 5082 | * @to: the target point of the transition or NULL |
Daniel Veillard | a9b66d0 | 2002-12-11 14:23:49 +0000 | [diff] [blame] | 5083 | * @lax: allow to transition if not all all transitions have been activated |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5084 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5085 | * 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] | 5086 | * and then adds a an ALL transition from the @from state to the |
| 5087 | * target state. That transition is an epsilon transition allowed only when |
| 5088 | * all transitions from the @from node have been activated. |
| 5089 | * |
| 5090 | * Returns the target state or NULL in case of error |
| 5091 | */ |
| 5092 | xmlAutomataStatePtr |
| 5093 | xmlAutomataNewAllTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5094 | xmlAutomataStatePtr to, int lax) { |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5095 | if ((am == NULL) || (from == NULL)) |
| 5096 | return(NULL); |
Daniel Veillard | 441bc32 | 2002-04-20 17:38:48 +0000 | [diff] [blame] | 5097 | xmlFAGenerateAllTransition(am, from, to, lax); |
Daniel Veillard | 7646b18 | 2002-04-20 06:41:40 +0000 | [diff] [blame] | 5098 | if (to == NULL) |
| 5099 | return(am->state); |
| 5100 | return(to); |
| 5101 | } |
| 5102 | |
| 5103 | /** |
Daniel Veillard | b509f15 | 2002-04-17 16:28:10 +0000 | [diff] [blame] | 5104 | * xmlAutomataNewCounter: |
| 5105 | * @am: an automata |
| 5106 | * @min: the minimal value on the counter |
| 5107 | * @max: the maximal value on the counter |
| 5108 | * |
| 5109 | * Create a new counter |
| 5110 | * |
| 5111 | * Returns the counter number or -1 in case of error |
| 5112 | */ |
| 5113 | int |
| 5114 | xmlAutomataNewCounter(xmlAutomataPtr am, int min, int max) { |
| 5115 | int ret; |
| 5116 | |
| 5117 | if (am == NULL) |
| 5118 | return(-1); |
| 5119 | |
| 5120 | ret = xmlRegGetCounter(am); |
| 5121 | if (ret < 0) |
| 5122 | return(-1); |
| 5123 | am->counters[ret].min = min; |
| 5124 | am->counters[ret].max = max; |
| 5125 | return(ret); |
| 5126 | } |
| 5127 | |
| 5128 | /** |
| 5129 | * xmlAutomataNewCountedTrans: |
| 5130 | * @am: an automata |
| 5131 | * @from: the starting point of the transition |
| 5132 | * @to: the target point of the transition or NULL |
| 5133 | * @counter: the counter associated to that transition |
| 5134 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5135 | * 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] | 5136 | * and then adds an epsilon transition from the @from state to the target state |
| 5137 | * which will increment the counter provided |
| 5138 | * |
| 5139 | * Returns the target state or NULL in case of error |
| 5140 | */ |
| 5141 | xmlAutomataStatePtr |
| 5142 | xmlAutomataNewCountedTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5143 | xmlAutomataStatePtr to, int counter) { |
| 5144 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
| 5145 | return(NULL); |
| 5146 | xmlFAGenerateCountedEpsilonTransition(am, from, to, counter); |
| 5147 | if (to == NULL) |
| 5148 | return(am->state); |
| 5149 | return(to); |
| 5150 | } |
| 5151 | |
| 5152 | /** |
| 5153 | * xmlAutomataNewCounterTrans: |
| 5154 | * @am: an automata |
| 5155 | * @from: the starting point of the transition |
| 5156 | * @to: the target point of the transition or NULL |
| 5157 | * @counter: the counter associated to that transition |
| 5158 | * |
William M. Brack | ddf71d6 | 2004-05-06 04:17:26 +0000 | [diff] [blame] | 5159 | * 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] | 5160 | * and then adds an epsilon transition from the @from state to the target state |
| 5161 | * which will be allowed only if the counter is within the right range. |
| 5162 | * |
| 5163 | * Returns the target state or NULL in case of error |
| 5164 | */ |
| 5165 | xmlAutomataStatePtr |
| 5166 | xmlAutomataNewCounterTrans(xmlAutomataPtr am, xmlAutomataStatePtr from, |
| 5167 | xmlAutomataStatePtr to, int counter) { |
| 5168 | if ((am == NULL) || (from == NULL) || (counter < 0)) |
| 5169 | return(NULL); |
| 5170 | xmlFAGenerateCountedTransition(am, from, to, counter); |
| 5171 | if (to == NULL) |
| 5172 | return(am->state); |
| 5173 | return(to); |
| 5174 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5175 | |
| 5176 | /** |
| 5177 | * xmlAutomataCompile: |
| 5178 | * @am: an automata |
| 5179 | * |
| 5180 | * Compile the automata into a Reg Exp ready for being executed. |
| 5181 | * The automata should be free after this point. |
| 5182 | * |
| 5183 | * Returns the compiled regexp or NULL in case of error |
| 5184 | */ |
| 5185 | xmlRegexpPtr |
| 5186 | xmlAutomataCompile(xmlAutomataPtr am) { |
| 5187 | xmlRegexpPtr ret; |
| 5188 | |
Daniel Veillard | a76fe5c | 2003-04-24 16:06:47 +0000 | [diff] [blame] | 5189 | if ((am == NULL) || (am->error != 0)) return(NULL); |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5190 | xmlFAEliminateEpsilonTransitions(am); |
Daniel Veillard | 23e7357 | 2002-09-19 19:56:43 +0000 | [diff] [blame] | 5191 | /* xmlFAComputesDeterminism(am); */ |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5192 | ret = xmlRegEpxFromParse(am); |
| 5193 | |
| 5194 | return(ret); |
| 5195 | } |
Daniel Veillard | e19fc23 | 2002-04-22 16:01:24 +0000 | [diff] [blame] | 5196 | |
| 5197 | /** |
| 5198 | * xmlAutomataIsDeterminist: |
| 5199 | * @am: an automata |
| 5200 | * |
| 5201 | * Checks if an automata is determinist. |
| 5202 | * |
| 5203 | * Returns 1 if true, 0 if not, and -1 in case of error |
| 5204 | */ |
| 5205 | int |
| 5206 | xmlAutomataIsDeterminist(xmlAutomataPtr am) { |
| 5207 | int ret; |
| 5208 | |
| 5209 | if (am == NULL) |
| 5210 | return(-1); |
| 5211 | |
| 5212 | ret = xmlFAComputesDeterminism(am); |
| 5213 | return(ret); |
| 5214 | } |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5215 | #endif /* LIBXML_AUTOMATA_ENABLED */ |
Daniel Veillard | 5d4644e | 2005-04-01 13:11:58 +0000 | [diff] [blame] | 5216 | #define bottom_xmlregexp |
| 5217 | #include "elfgcchack.h" |
Daniel Veillard | 4255d50 | 2002-04-16 15:50:10 +0000 | [diff] [blame] | 5218 | #endif /* LIBXML_REGEXP_ENABLED */ |