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