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