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