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