| 1 | /* |
| 2 | * Copyright (c) 1998, 2012 Michael J. Roberts. All Rights Reserved. |
| 3 | * |
| 4 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 5 | * on using and copying this software. |
| 6 | */ |
| 7 | /* |
| 8 | Name |
| 9 | regex.c - Regular Expression Parser and Recognizer for T3 |
| 10 | Function |
| 11 | Parses and recognizes regular expressions. Adapted to C++ and UTF-8 |
| 12 | from the TADS 2 implementation. |
| 13 | Notes |
| 14 | Regular expression syntax: |
| 15 | |
| 16 | abc|def either abc or def |
| 17 | (abc) abc |
| 18 | abc+ abc, abcc, abccc, ... |
| 19 | abc* ab, abc, abcc, ... |
| 20 | abc? ab or abc |
| 21 | x{n} x exactly 'n' times |
| 22 | x{n,} x at least 'n' times |
| 23 | x{,n} x 0 to 'n' times |
| 24 | x{n,m} x from 'n' to 'm' times |
| 25 | . any single character |
| 26 | abc$ abc at the end of the line |
| 27 | ^abc abc at the beginning of the line |
| 28 | %^abc literally ^abc |
| 29 | [abcx-z] matches a, b, c, x, y, or z |
| 30 | [^abcx-z] matches any character except a, b, c, x, y, or z |
| 31 | [^]-q] matches any character except ], -, or q |
| 32 | x+? use *shortest* working match to x+ |
| 33 | x*? use shortest match to x* |
| 34 | x{}? use shortest match to x{} |
| 35 | |
| 36 | Note that using ']' or '-' in a character range expression requires |
| 37 | special ordering. If ']' is to be used, it must be the first character |
| 38 | after the '^', if present, within the brackets. If '-' is to be used, |
| 39 | it must be the first character after the '^' and/or ']', if present. |
| 40 | |
| 41 | The following special sequences match literal characters by name (the |
| 42 | names insensitive to case): |
| 43 | |
| 44 | <lparen> ( |
| 45 | <rparen> ) |
| 46 | <lsquare> [ |
| 47 | <rsquare> ] |
| 48 | <lbrace> { |
| 49 | <rbrace> } |
| 50 | <langle> < |
| 51 | <rangle> > |
| 52 | <vbar> | |
| 53 | <caret> ^ |
| 54 | <period> . |
| 55 | <dot> . |
| 56 | <squote> ' |
| 57 | <dquote> " |
| 58 | <star> * |
| 59 | <plus> + |
| 60 | <percent> % |
| 61 | <question> ? |
| 62 | <dollar> $ |
| 63 | <backslash> \ |
| 64 | <return> carriage return (character code 0x000D) |
| 65 | <linefeed> line feed (character code 0x000A) |
| 66 | <tab> tab (character code 0x0009) |
| 67 | <nul> null character (character code 0x0000) |
| 68 | <null> null character (character code 0x0000) |
| 69 | |
| 70 | The following special sequences match character classes (these class |
| 71 | names, like the character literal names above, are insensitive to case): |
| 72 | |
| 73 | <Alpha> any alphabetic character |
| 74 | <Upper> any upper-case alphabetic character |
| 75 | <Lower> any lower-case alphabetic character |
| 76 | <Digit> any digit |
| 77 | <AlphaNum> any alphabetic character or digit |
| 78 | <Space> space character |
| 79 | <VSpace> vertical whitespace |
| 80 | <Punct> punctuation character |
| 81 | <Newline> any newline character: \r, \n, \b, 0x2028, 0x2029 |
| 82 | |
| 83 | Character classes can be combined by separating class names with the '|' |
| 84 | delimiter. In addition, a class expression can be complemented to make |
| 85 | it an exclusion expression by putting a '^' after the opening bracket. |
| 86 | |
| 87 | <Upper|Digit> any uppercase letter or any digit |
| 88 | <^Alpha> anything except an alphabetic character |
| 89 | <^Upper|Digit> anything except an uppercase letter or a digit |
| 90 | (note that the exclusion applies to the ENTIRE |
| 91 | expression, so in this case both uppercase letters |
| 92 | and digits are excluded) |
| 93 | |
| 94 | In addition, character classes and named literals can be combined: |
| 95 | |
| 96 | <Upper|Star|Plus> Any uppercase letter, or '*' or '+' |
| 97 | |
| 98 | Finally, literal characters and literal ranges resembling '[]' ranges |
| 99 | can be used in angle-bracket expressions, with the limitation that each |
| 100 | character or range must be separated with the '|' delimiter: |
| 101 | |
| 102 | <a|b|c> 'a' or 'b' or 'c' |
| 103 | <Upper|a|b|c> any uppercase letter, or 'a', 'b', or 'c' |
| 104 | <Upper|a-m> any uppercase letter, or lowercase letters 'a' to 'm' |
| 105 | |
| 106 | '%' is used to escape the special characters: | . ( ) * ? + ^ $ % [ |
| 107 | (We use '%' rather than a backslash because it's less trouble to |
| 108 | enter in a TADS string -- a backslash needs to be quoted with another |
| 109 | backslash, which is error-prone and hard to read. '%' doesn't need |
| 110 | any special quoting in a TADS string, which makes it a lot more |
| 111 | readable.) |
| 112 | |
| 113 | In addition, '%' is used to introduce additional special sequences: |
| 114 | |
| 115 | %1 text matching first parenthesized expression |
| 116 | %9 text matching ninth parenthesized experssion |
| 117 | %< matches at the beginning of a word only |
| 118 | %> matches at the end of a word only |
| 119 | %w matches any word character |
| 120 | %W matches any non-word character |
| 121 | %b matches at any word boundary (beginning or end of word) |
| 122 | %B matches except at a word boundary |
| 123 | %s <space> |
| 124 | %S <^space> |
| 125 | %v <vspace> |
| 126 | %V <^vspace> |
| 127 | %d <digit> |
| 128 | %D <^digit> |
| 129 | |
| 130 | For the word matching sequences, a word is any sequence of letters and |
| 131 | numbers. |
| 132 | |
| 133 | The following special sequences modify the search rules: |
| 134 | |
| 135 | <Case> - makes the search case-sensitive. This is the default. |
| 136 | <NoCase> - makes the search case-insensitive. <Case> and <NoCase> |
| 137 | are both global - the entire expression is either |
| 138 | case-sensitive or case-insensitive. |
| 139 | |
| 140 | <FE> or <FirstEnd> - select the substring that ends earliest, in |
| 141 | case of an ambiguous match. <FirstBegin> is the default. |
| 142 | <FB> or <FirstBegin> - select the substring that starts earliest, in |
| 143 | case of an ambiguous match. This is the default. |
| 144 | |
| 145 | <Min> - select the shortest matching substring, in case of an |
| 146 | ambiguous match. <Max> is the default. |
| 147 | <Max> - select the longest matching substring, in case of an |
| 148 | ambiguous match. |
| 149 | |
| 150 | Modified |
| 151 | 09/06/98 MJRoberts - Creation |
| 152 | */ |
| 153 | |
| 154 | |
| 155 | #include <ctype.h> |
| 156 | #include <stdio.h> |
| 157 | #include <stdlib.h> |
| 158 | #include <string.h> |
| 159 | #include <assert.h> |
| 160 | |
| 161 | #include "t3std.h" |
| 162 | #include "vmregex.h" |
| 163 | #include "utf8.h" |
| 164 | #include "vmerr.h" |
| 165 | #include "vmerrnum.h" |
| 166 | #include "vmuni.h" |
| 167 | #include "vmfile.h" |
| 168 | |
| 169 | |
| 170 | /* ------------------------------------------------------------------------ */ |
| 171 | /* |
| 172 | * Initialize. |
| 173 | */ |
| 174 | CRegexParser::CRegexParser() |
| 175 | { |
| 176 | /* no tuple array yet */ |
| 177 | tuple_arr_ = 0; |
| 178 | tuples_alloc_ = 0; |
| 179 | |
| 180 | /* clear states */ |
| 181 | next_state_ = RE_STATE_FIRST_VALID; |
| 182 | |
| 183 | /* no range buffer yet */ |
| 184 | range_buf_ = 0; |
| 185 | range_buf_cnt_ = 0; |
| 186 | range_buf_max_ = 0; |
| 187 | } |
| 188 | |
| 189 | /* ------------------------------------------------------------------------ */ |
| 190 | /* |
| 191 | * Reset compiler - clears states and tuples |
| 192 | */ |
| 193 | void CRegexParser::reset() |
| 194 | { |
| 195 | /* delete any range tables we've allocated */ |
| 196 | int i; |
| 197 | re_tuple *t; |
| 198 | for (i = 0, t = tuple_arr_ ; i < next_state_ ; ++i, ++t) |
| 199 | { |
| 200 | /* if it's a range, delete the allocated range */ |
| 201 | if ((t->typ == RE_RANGE || t->typ == RE_RANGE_EXCL) |
| 202 | && t->info.range.char_range != 0) |
| 203 | { |
| 204 | delete [] t->info.range.char_range; |
| 205 | t->info.range.char_range = 0; |
| 206 | } |
| 207 | else if (t->typ == RE_LITSTR && t->info.str.str != 0) |
| 208 | { |
| 209 | delete [] t->info.str.str; |
| 210 | t->info.str.str = 0; |
| 211 | } |
| 212 | |
| 213 | /* clear the tuple type */ |
| 214 | t->typ = RE_INVALID; |
| 215 | } |
| 216 | |
| 217 | /* clear states */ |
| 218 | next_state_ = RE_STATE_FIRST_VALID; |
| 219 | } |
| 220 | |
| 221 | /* ------------------------------------------------------------------------ */ |
| 222 | /* |
| 223 | * Delete |
| 224 | */ |
| 225 | CRegexParser::~CRegexParser() |
| 226 | { |
| 227 | /* reset state (to delete most of our memory structures) */ |
| 228 | reset(); |
| 229 | |
| 230 | /* if we've allocated an array, delete it */ |
| 231 | if (tuple_arr_ != 0) |
| 232 | { |
| 233 | t3free(tuple_arr_); |
| 234 | tuple_arr_ = 0; |
| 235 | } |
| 236 | |
| 237 | /* delete our range buffer if we have one */ |
| 238 | if (range_buf_ != 0) |
| 239 | { |
| 240 | t3free(range_buf_); |
| 241 | range_buf_ = 0; |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | /* ------------------------------------------------------------------------ */ |
| 246 | /* |
| 247 | * Add an entry to our range buffer |
| 248 | */ |
| 249 | void CRegexParser::add_range_char(wchar_t ch1, wchar_t ch2) |
| 250 | { |
| 251 | /* |
| 252 | * if the first character in the range is null, it requires special |
| 253 | * representation |
| 254 | */ |
| 255 | if (ch1 == 0) |
| 256 | { |
| 257 | /* |
| 258 | * A null character must be represented using the character class |
| 259 | * notation for RE_NULLCHAR, because a null lead character in a |
| 260 | * pair has the special meaning of introducing a class specifier |
| 261 | * in the second character of the pair. So, first add the null |
| 262 | * character as a class character. |
| 263 | */ |
| 264 | add_range_class(RE_NULLCHAR); |
| 265 | |
| 266 | /* |
| 267 | * if the second character of the range is null, we're done, since |
| 268 | * all we needed was the null itself |
| 269 | */ |
| 270 | if (ch2 == 0) |
| 271 | return; |
| 272 | |
| 273 | /* |
| 274 | * we've added the null character, so simply add the rest of the |
| 275 | * range starting at character code 1 |
| 276 | */ |
| 277 | ch1 = 1; |
| 278 | } |
| 279 | |
| 280 | /* make sure we have space in the range buffer for the added entry */ |
| 281 | ensure_range_buf_space(); |
| 282 | |
| 283 | /* add the new entry */ |
| 284 | range_buf_[range_buf_cnt_++] = ch1; |
| 285 | range_buf_[range_buf_cnt_++] = ch2; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * Add a class to a character range. The class identifier is one of the |
| 290 | * RE_xxx recognizer types representing a class: RE_ALPHA, RE_DIGIT, and |
| 291 | * so on. |
| 292 | */ |
| 293 | void CRegexParser::add_range_class(re_recog_type cl) |
| 294 | { |
| 295 | /* make sure we have space in the range buffer */ |
| 296 | ensure_range_buf_space(); |
| 297 | |
| 298 | /* |
| 299 | * add the new entry - the first character of the pair is a null |
| 300 | * character to indicate that the entry represents a class, and the |
| 301 | * second of the pair is the type code |
| 302 | */ |
| 303 | range_buf_[range_buf_cnt_++] = 0; |
| 304 | range_buf_[range_buf_cnt_++] = (wchar_t)cl; |
| 305 | } |
| 306 | |
| 307 | /* |
| 308 | * Check for space in the range buffer for a new range, expanding the |
| 309 | * buffer if necessary. |
| 310 | */ |
| 311 | void CRegexParser::ensure_range_buf_space() |
| 312 | { |
| 313 | /* make sure we have room for another entry */ |
| 314 | if (range_buf_ == 0) |
| 315 | { |
| 316 | /* |
| 317 | * allocate an initial size - it must be even, since we always |
| 318 | * consume elements in pairs |
| 319 | */ |
| 320 | range_buf_max_ = 128; |
| 321 | range_buf_ = (wchar_t *)t3malloc(range_buf_max_ * sizeof(wchar_t)); |
| 322 | |
| 323 | /* no entries yet */ |
| 324 | range_buf_cnt_ = 0; |
| 325 | } |
| 326 | else if (range_buf_cnt_ == range_buf_max_) |
| 327 | { |
| 328 | /* |
| 329 | * reallocate the buffer at a larger size (the size must always be |
| 330 | * even, since we always add to the buffer in pairs) |
| 331 | */ |
| 332 | range_buf_max_ += 128; |
| 333 | range_buf_ = (wchar_t *)t3realloc(range_buf_, |
| 334 | range_buf_max_ * sizeof(wchar_t)); |
| 335 | } |
| 336 | |
| 337 | /* if the range buffer is null, throw an error */ |
| 338 | if (range_buf_ == 0) |
| 339 | err_throw(VMERR_OUT_OF_MEMORY); |
| 340 | } |
| 341 | |
| 342 | |
| 343 | /* ------------------------------------------------------------------------ */ |
| 344 | /* |
| 345 | * Allocate a new state ID |
| 346 | */ |
| 347 | re_state_id CRegexParser::alloc_state() |
| 348 | { |
| 349 | /* |
| 350 | * If we don't have enough room for another state, expand the array |
| 351 | */ |
| 352 | if (next_state_ >= tuples_alloc_) |
| 353 | { |
| 354 | int new_alloc; |
| 355 | |
| 356 | /* bump the size by a bit */ |
| 357 | new_alloc = tuples_alloc_ + 100; |
| 358 | |
| 359 | /* allocate or expand the array */ |
| 360 | if (tuples_alloc_ == 0) |
| 361 | { |
| 362 | /* allocate the initial memory block */ |
| 363 | tuple_arr_ = (re_tuple *)t3malloc(new_alloc * sizeof(re_tuple)); |
| 364 | } |
| 365 | else |
| 366 | { |
| 367 | /* re-allocate the existing memory block */ |
| 368 | tuple_arr_ = (re_tuple *)t3realloc(tuple_arr_, |
| 369 | new_alloc * sizeof(re_tuple)); |
| 370 | } |
| 371 | |
| 372 | /* remember the new allocation size */ |
| 373 | tuples_alloc_ = new_alloc; |
| 374 | } |
| 375 | |
| 376 | /* initialize the next state */ |
| 377 | tuple_arr_[next_state_].next_state_1 = RE_STATE_INVALID; |
| 378 | tuple_arr_[next_state_].next_state_2 = RE_STATE_INVALID; |
| 379 | tuple_arr_[next_state_].info.ch = RE_EPSILON; |
| 380 | tuple_arr_[next_state_].flags = 0; |
| 381 | tuple_arr_[next_state_].typ = RE_INVALID; |
| 382 | |
| 383 | /* return the new state's ID */ |
| 384 | return next_state_++; |
| 385 | } |
| 386 | |
| 387 | |
| 388 | /* ------------------------------------------------------------------------ */ |
| 389 | /* |
| 390 | * Set a transition from a state to a given destination state. |
| 391 | */ |
| 392 | void CRegexParser::set_trans(re_state_id id, re_state_id dest_id, |
| 393 | re_recog_type typ, wchar_t ch) |
| 394 | { |
| 395 | /* ignore invalid states */ |
| 396 | if (id == RE_STATE_INVALID) |
| 397 | return; |
| 398 | |
| 399 | /* |
| 400 | * get the tuple containing the transitions for this state ID - the |
| 401 | * state ID is the index of the state's transition tuple in the |
| 402 | * array |
| 403 | */ |
| 404 | re_tuple *tuple = &tuple_arr_[id]; |
| 405 | |
| 406 | /* |
| 407 | * If the first state pointer hasn't been set yet, set it to the new |
| 408 | * destination. Otherwise, set the second state pointer. |
| 409 | * |
| 410 | * Only set the character on setting the first state. When setting |
| 411 | * the second state, we must assume that the character for the state |
| 412 | * has already been set, since any given state can have only one |
| 413 | * character setting. |
| 414 | */ |
| 415 | if (tuple->next_state_1 == RE_STATE_INVALID) |
| 416 | { |
| 417 | /* set the transition type and character ID */ |
| 418 | tuple->typ = typ; |
| 419 | tuple->info.ch = ch; |
| 420 | |
| 421 | /* set the first transition */ |
| 422 | tuple->next_state_1 = dest_id; |
| 423 | } |
| 424 | else |
| 425 | { |
| 426 | /* |
| 427 | * set only the second transition state - don't set the character |
| 428 | * ID or transition type |
| 429 | */ |
| 430 | tuple->next_state_2 = dest_id; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | /* ------------------------------------------------------------------------ */ |
| 435 | /* |
| 436 | * Initialize a new machine, giving it an initial and final state |
| 437 | */ |
| 438 | void CRegexParser::init_machine(re_machine *machine) |
| 439 | { |
| 440 | machine->init = alloc_state(); |
| 441 | machine->final = alloc_state(); |
| 442 | } |
| 443 | |
| 444 | /* |
| 445 | * Build a character recognizer |
| 446 | */ |
| 447 | void CRegexParser::build_char(re_machine *machine, wchar_t ch) |
| 448 | { |
| 449 | /* initialize our new machine */ |
| 450 | init_machine(machine); |
| 451 | |
| 452 | /* allocate a transition tuple for the new state */ |
| 453 | set_trans(machine->init, machine->final, RE_LITERAL, ch); |
| 454 | } |
| 455 | |
| 456 | /* |
| 457 | * Build a special type recognizer |
| 458 | */ |
| 459 | void CRegexParser::build_special(re_machine *machine, re_recog_type typ, |
| 460 | wchar_t ch) |
| 461 | { |
| 462 | /* initialize our new machine */ |
| 463 | init_machine(machine); |
| 464 | |
| 465 | /* allocate a transition tuple for the new state */ |
| 466 | set_trans(machine->init, machine->final, typ, ch); |
| 467 | } |
| 468 | |
| 469 | /* |
| 470 | * Build a character range recognizer. |
| 471 | */ |
| 472 | void CRegexParser::build_char_range(re_machine *machine, |
| 473 | int exclusion) |
| 474 | { |
| 475 | /* initialize our new machine */ |
| 476 | init_machine(machine); |
| 477 | |
| 478 | /* allocate a transition table for the new state */ |
| 479 | set_trans(machine->init, machine->final, |
| 480 | (exclusion ? RE_RANGE_EXCL : RE_RANGE), 0); |
| 481 | |
| 482 | /* allocate a copy of the range vector */ |
| 483 | wchar_t *range_copy = new wchar_t[range_buf_cnt_]; |
| 484 | |
| 485 | /* copy the caller's range */ |
| 486 | memcpy(range_copy, range_buf_, range_buf_cnt_ * sizeof(wchar_t)); |
| 487 | |
| 488 | /* store it in the tuple */ |
| 489 | tuple_arr_[machine->init].info.range.char_range = range_copy; |
| 490 | tuple_arr_[machine->init].info.range.char_range_cnt = range_buf_cnt_; |
| 491 | } |
| 492 | |
| 493 | |
| 494 | /* |
| 495 | * Build a group recognizer. This is almost the same as a character |
| 496 | * recognizer, but matches a previous group rather than a literal |
| 497 | * character. |
| 498 | */ |
| 499 | void CRegexParser::build_group_matcher(re_machine *machine, int group_num) |
| 500 | { |
| 501 | /* initialize our new machine */ |
| 502 | init_machine(machine); |
| 503 | |
| 504 | /* |
| 505 | * Allocate a transition tuple for the new state for the group ID. |
| 506 | * Rather than storing a literal character code, store the group ID |
| 507 | * in the character code slot. |
| 508 | */ |
| 509 | set_trans(machine->init, machine->final, |
| 510 | RE_GROUP_MATCH, (wchar_t)group_num); |
| 511 | } |
| 512 | |
| 513 | |
| 514 | /* |
| 515 | * Build a concatenation recognizer |
| 516 | */ |
| 517 | void CRegexParser::build_concat(re_machine *new_machine, |
| 518 | re_machine *lhs, re_machine *rhs) |
| 519 | { |
| 520 | /* initialize the new machine */ |
| 521 | init_machine(new_machine); |
| 522 | |
| 523 | /* |
| 524 | * set up an epsilon transition from the new machine's initial state |
| 525 | * to the first submachine's initial state |
| 526 | */ |
| 527 | set_trans(new_machine->init, lhs->init, RE_EPSILON, 0); |
| 528 | |
| 529 | /* |
| 530 | * Set up an epsilon transition from the first submachine's final |
| 531 | * state to the second submachine's initial state |
| 532 | */ |
| 533 | set_trans(lhs->final, rhs->init, RE_EPSILON, 0); |
| 534 | |
| 535 | /* |
| 536 | * Set up an epsilon transition from the second submachine's final |
| 537 | * state to our new machine's final state |
| 538 | */ |
| 539 | set_trans(rhs->final, new_machine->final, RE_EPSILON, 0); |
| 540 | } |
| 541 | |
| 542 | /* |
| 543 | * Build a group machine. sub_machine contains the machine that |
| 544 | * expresses the group's contents; we'll fill in new_machine with a |
| 545 | * newly-created machine that encloses and marks the group. |
| 546 | */ |
| 547 | void CRegexParser::build_group(re_machine *new_machine, |
| 548 | re_machine *sub_machine, int group_id) |
| 549 | { |
| 550 | /* initialize the container machine */ |
| 551 | init_machine(new_machine); |
| 552 | |
| 553 | /* |
| 554 | * Set up a group-entry transition from the new machine's initial |
| 555 | * state into the initial state of the group, and a group-exit |
| 556 | * transition from the group's final state into the container's final |
| 557 | * state. For both transitions, store the group ID in the character |
| 558 | * field of the transition, to identify which group is affected. |
| 559 | */ |
| 560 | set_trans(new_machine->init, sub_machine->init, |
| 561 | RE_GROUP_ENTER, (wchar_t)group_id); |
| 562 | set_trans(sub_machine->final, new_machine->final, |
| 563 | RE_GROUP_EXIT, (wchar_t)group_id); |
| 564 | } |
| 565 | |
| 566 | /* |
| 567 | * Build an assertion machine |
| 568 | */ |
| 569 | void CRegexParser::build_assert(re_machine *new_machine, |
| 570 | re_machine *sub_machine, |
| 571 | int is_negative, int is_lookback) |
| 572 | { |
| 573 | /* initialize the container machine */ |
| 574 | init_machine(new_machine); |
| 575 | |
| 576 | /* get a pointer to its initial state */ |
| 577 | re_tuple *t = &tuple_arr_[new_machine->init]; |
| 578 | |
| 579 | /* allocate a transition tuple for the new state */ |
| 580 | set_trans(new_machine->init, new_machine->final, |
| 581 | (is_lookback |
| 582 | ? (is_negative ? RE_ASSERT_BACKNEG : RE_ASSERT_BACKPOS) |
| 583 | : (is_negative ? RE_ASSERT_NEG : RE_ASSERT_POS)), |
| 584 | 0); |
| 585 | |
| 586 | /* set the sub-state */ |
| 587 | t->info.sub.init = sub_machine->init; |
| 588 | t->info.sub.final = sub_machine->final; |
| 589 | |
| 590 | /* |
| 591 | * For lookback assertions, we need to know the range of possible match |
| 592 | * lengths, to optimize the reverse search for a match. The |
| 593 | * sub-machine for the assertion is fully built now and is by nature |
| 594 | * self-contained, so we can calculate its length range now. |
| 595 | */ |
| 596 | if (is_lookback) |
| 597 | get_match_length(sub_machine->init, sub_machine->final, |
| 598 | &t->info.sub.minlen, &t->info.sub.maxlen, 0); |
| 599 | } |
| 600 | |
| 601 | |
| 602 | /* |
| 603 | * Build an alternation recognizer |
| 604 | */ |
| 605 | void CRegexParser::build_alter(re_machine *new_machine, |
| 606 | re_machine *lhs, re_machine *rhs) |
| 607 | { |
| 608 | /* initialize the new machine */ |
| 609 | init_machine(new_machine); |
| 610 | |
| 611 | /* |
| 612 | * Set up an epsilon transition from our new machine's initial state |
| 613 | * to the initial state of each submachine |
| 614 | */ |
| 615 | set_trans(new_machine->init, lhs->init, RE_EPSILON, 0); |
| 616 | set_trans(new_machine->init, rhs->init, RE_EPSILON, 0); |
| 617 | |
| 618 | /* |
| 619 | * Set up an epsilon transition from the final state of each |
| 620 | * submachine to our final state |
| 621 | */ |
| 622 | set_trans(lhs->final, new_machine->final, RE_EPSILON, 0); |
| 623 | set_trans(rhs->final, new_machine->final, RE_EPSILON, 0); |
| 624 | } |
| 625 | |
| 626 | /* |
| 627 | * Build a closure recognizer |
| 628 | */ |
| 629 | void CRegexParser::build_closure(re_machine *new_machine, re_machine *sub, |
| 630 | wchar_t specifier, int shortest) |
| 631 | { |
| 632 | /* initialize the new machine */ |
| 633 | init_machine(new_machine); |
| 634 | |
| 635 | /* |
| 636 | * Set up an epsilon transition from our initial state to the |
| 637 | * submachine's initial state. However, if we're in shortest mode, |
| 638 | * wait to do this until after we've generated the rest of the |
| 639 | * machine, and instead set up the transition from the submachine's |
| 640 | * final state to our final state. The order is important because |
| 641 | * we will favor the first branch taken when we find two matches of |
| 642 | * equal total length; hence we want to make the branch that will |
| 643 | * give us either the longest match or shortest match for this |
| 644 | * closure first, depending on which way we want to go. |
| 645 | */ |
| 646 | if (!shortest) |
| 647 | set_trans(new_machine->init, sub->init, RE_EPSILON, 0); |
| 648 | else |
| 649 | set_trans(sub->final, new_machine->final, RE_EPSILON, 0); |
| 650 | |
| 651 | /* |
| 652 | * If this is an unbounded closure ('*' or '+', but not '?'), set up |
| 653 | * the loop transition that takes us from the new machine's final |
| 654 | * state back to its initial state. We don't do this on the |
| 655 | * zero-or-one closure, because we can only match the expression |
| 656 | * once. |
| 657 | */ |
| 658 | if (specifier != '?') |
| 659 | { |
| 660 | /* set the transition */ |
| 661 | set_trans(sub->final, sub->init, RE_EPSILON, 0); |
| 662 | |
| 663 | /* if we have a 'shortest' modifier, flag it in this branch */ |
| 664 | if (shortest) |
| 665 | tuple_arr_[sub->final].flags |= RE_STATE_SHORTEST; |
| 666 | } |
| 667 | |
| 668 | /* |
| 669 | * If this is a zero-or-one closure or a zero-or-more closure, set |
| 670 | * up an epsilon transition from our initial state to our final |
| 671 | * state, since we can skip the entire subexpression. We don't do |
| 672 | * this on the one-or-more closure, because we can't skip the |
| 673 | * subexpression in this case. |
| 674 | */ |
| 675 | if (specifier != '+') |
| 676 | { |
| 677 | /* set the transition */ |
| 678 | set_trans(new_machine->init, new_machine->final, RE_EPSILON, 0); |
| 679 | |
| 680 | /* if we have a 'shortest' modifier, flag it in this branch */ |
| 681 | if (shortest) |
| 682 | tuple_arr_[new_machine->init].flags |= RE_STATE_SHORTEST; |
| 683 | } |
| 684 | |
| 685 | /* |
| 686 | * Set up a transition from the submachine's final state to our |
| 687 | * final state. We waited until here to ensure proper ordering for |
| 688 | * longest-preferred. If we're in shortest-preferred mode, though, |
| 689 | * set up the initial transition to the submachine instead. |
| 690 | */ |
| 691 | if (!shortest) |
| 692 | set_trans(sub->final, new_machine->final, RE_EPSILON, 0); |
| 693 | else |
| 694 | set_trans(new_machine->init, sub->init, RE_EPSILON, 0); |
| 695 | } |
| 696 | |
| 697 | void CRegexParser::build_interval(re_machine *new_machine, |
| 698 | re_machine *sub, int min_val, int max_val, |
| 699 | int var_id, int shortest) |
| 700 | { |
| 701 | re_machine inner_machine; |
| 702 | |
| 703 | /* initialize the outer (new) machine */ |
| 704 | init_machine(new_machine); |
| 705 | |
| 706 | /* initialize the inner machine */ |
| 707 | init_machine(&inner_machine); |
| 708 | |
| 709 | /* |
| 710 | * Set the loop transition into the submachine, and set the other to |
| 711 | * bypass the submachine. If we have a 'shortest' modifier, take |
| 712 | * the bypass branch first, otherwise take the enter branch first. |
| 713 | */ |
| 714 | if (shortest) |
| 715 | { |
| 716 | set_trans(inner_machine.init, new_machine->final, RE_LOOP_BRANCH, 0); |
| 717 | set_trans(inner_machine.init, sub->init, RE_LOOP_BRANCH, 0); |
| 718 | } |
| 719 | else |
| 720 | { |
| 721 | set_trans(inner_machine.init, sub->init, RE_LOOP_BRANCH, 0); |
| 722 | set_trans(inner_machine.init, new_machine->final, RE_LOOP_BRANCH, 0); |
| 723 | } |
| 724 | |
| 725 | /* |
| 726 | * set the final transition of the submachine to come back to the |
| 727 | * loop branch point |
| 728 | */ |
| 729 | set_trans(sub->final, inner_machine.init, RE_EPSILON, 0); |
| 730 | |
| 731 | /* |
| 732 | * set the outer machine to transition into the inner machine and |
| 733 | * zero the loop variable |
| 734 | */ |
| 735 | set_trans(new_machine->init, inner_machine.init, RE_ZERO_VAR, 0); |
| 736 | |
| 737 | /* set the variable ID in the ZERO_VAR node */ |
| 738 | tuple_arr_[new_machine->init].info.loop.loop_var = var_id; |
| 739 | |
| 740 | /* set up the loop parameters in the loop node */ |
| 741 | tuple_arr_[inner_machine.init].info.loop.loop_var = var_id; |
| 742 | tuple_arr_[inner_machine.init].info.loop.loop_min = min_val; |
| 743 | tuple_arr_[inner_machine.init].info.loop.loop_max = max_val; |
| 744 | |
| 745 | /* |
| 746 | * if there's a 'shortest' modifier, note it in the loop node, so |
| 747 | * that we can take the bypass branch first whenever possible |
| 748 | */ |
| 749 | if (shortest) |
| 750 | tuple_arr_[inner_machine.init].flags |= RE_STATE_SHORTEST; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Build a null machine |
| 755 | */ |
| 756 | void CRegexParser::build_null_machine(re_machine *machine) |
| 757 | { |
| 758 | machine->init = machine->final = RE_STATE_INVALID; |
| 759 | } |
| 760 | |
| 761 | /* ------------------------------------------------------------------------ */ |
| 762 | /* |
| 763 | * Determine if a machine is null |
| 764 | */ |
| 765 | int CRegexParser::is_machine_null(re_machine *machine) |
| 766 | { |
| 767 | return (machine->init == RE_STATE_INVALID); |
| 768 | } |
| 769 | |
| 770 | |
| 771 | /* ------------------------------------------------------------------------ */ |
| 772 | /* |
| 773 | * Concatenate the second machine onto the first machine, replacing the |
| 774 | * first machine with the resulting machine. If the first machine is a |
| 775 | * null machine (created with re_build_null_machine), we'll simply copy |
| 776 | * the second machine into the first. |
| 777 | */ |
| 778 | void CRegexParser::concat_onto(re_machine *dest, re_machine *rhs) |
| 779 | { |
| 780 | /* check for a null destination machine */ |
| 781 | if (is_machine_null(dest)) |
| 782 | { |
| 783 | /* |
| 784 | * the first machine is null - simply copy the second machine |
| 785 | * onto the first unchanged |
| 786 | */ |
| 787 | *dest = *rhs; |
| 788 | } |
| 789 | else |
| 790 | { |
| 791 | re_machine new_machine; |
| 792 | |
| 793 | /* build the concatenated machine */ |
| 794 | build_concat(&new_machine, dest, rhs); |
| 795 | |
| 796 | /* copy the concatenated machine onto the first machine */ |
| 797 | *dest = new_machine; |
| 798 | } |
| 799 | } |
| 800 | |
| 801 | /* |
| 802 | * Alternate the second machine onto the first machine, replacing the |
| 803 | * first machine with the resulting machine. If the first machine is a |
| 804 | * null machine, this simply replaces the first machine with the second |
| 805 | * machine. If the second machine is null, this simply leaves the first |
| 806 | * machine unchanged. |
| 807 | */ |
| 808 | void CRegexParser::alternate_onto(re_machine *dest, re_machine *rhs) |
| 809 | { |
| 810 | /* check to see if the first machine is null */ |
| 811 | if (is_machine_null(dest)) |
| 812 | { |
| 813 | /* |
| 814 | * the first machine is null - simply copy the second machine |
| 815 | * onto the first |
| 816 | */ |
| 817 | *dest = *rhs; |
| 818 | } |
| 819 | else |
| 820 | { |
| 821 | /* |
| 822 | * if the second machine is null, don't do anything; otherwise, |
| 823 | * build the alternation |
| 824 | */ |
| 825 | if (!is_machine_null(rhs)) |
| 826 | { |
| 827 | re_machine new_machine; |
| 828 | |
| 829 | /* build the alternation */ |
| 830 | build_alter(&new_machine, dest, rhs); |
| 831 | |
| 832 | /* replace the first machine with the alternation */ |
| 833 | *dest = new_machine; |
| 834 | } |
| 835 | } |
| 836 | } |
| 837 | |
| 838 | /* ------------------------------------------------------------------------ */ |
| 839 | /* |
| 840 | * Compile an expression |
| 841 | */ |
| 842 | re_status_t CRegexParser::compile(const char *expr_str, size_t exprlen, |
| 843 | re_compiled_pattern_base *pat) |
| 844 | { |
| 845 | re_machine cur_machine; |
| 846 | re_machine alter_machine; |
| 847 | re_machine new_machine; |
| 848 | int group_stack_level; |
| 849 | struct |
| 850 | { |
| 851 | re_machine old_cur; |
| 852 | re_machine old_alter; |
| 853 | int group_id; |
| 854 | int capturing; |
| 855 | int neg_assertion; |
| 856 | int pos_assertion; |
| 857 | int back_assertion; |
| 858 | } group_stack[RE_GROUP_NESTING_MAX]; |
| 859 | utf8_ptr expr; |
| 860 | |
| 861 | /* reset everything */ |
| 862 | reset(); |
| 863 | |
| 864 | /* we have no groups yet */ |
| 865 | pat->group_cnt = 0; |
| 866 | |
| 867 | /* we have no looping variables yet */ |
| 868 | pat->loop_var_cnt = 0; |
| 869 | |
| 870 | /* get the length of the expression in characters */ |
| 871 | size_t exprchars = utf8_ptr::s_len(expr_str, exprlen); |
| 872 | |
| 873 | /* |
| 874 | * set the default match modes - maximum, first-beginning, |
| 875 | * case-sensitive |
| 876 | */ |
| 877 | pat->longest_match = TRUE; |
| 878 | pat->first_begin = TRUE; |
| 879 | pat->case_sensitive = TRUE; |
| 880 | pat->case_sensitivity_specified = FALSE; |
| 881 | |
| 882 | /* start out with no current machine and no alternate machine */ |
| 883 | build_null_machine(&cur_machine); |
| 884 | build_null_machine(&alter_machine); |
| 885 | |
| 886 | /* nothing on the stack yet */ |
| 887 | group_stack_level = 0; |
| 888 | |
| 889 | /* loop until we run out of expression to parse */ |
| 890 | for (expr.set((char *)expr_str) ; exprchars != 0 ; expr.inc(), --exprchars) |
| 891 | { |
| 892 | switch(expr.getch()) |
| 893 | { |
| 894 | case '^': |
| 895 | /* |
| 896 | * beginning of line - if we're not at the beginning of the |
| 897 | * current expression (i.e., we already have some |
| 898 | * concatentations accumulated), treat it as an ordinary |
| 899 | * character |
| 900 | */ |
| 901 | if (!is_machine_null(&cur_machine)) |
| 902 | goto normal_char; |
| 903 | |
| 904 | /* build a new start-of-text recognizer */ |
| 905 | build_special(&new_machine, RE_TEXT_BEGIN, 0); |
| 906 | |
| 907 | /* |
| 908 | * concatenate it onto the string - note that this can't |
| 909 | * have any postfix operators |
| 910 | */ |
| 911 | concat_onto(&cur_machine, &new_machine); |
| 912 | break; |
| 913 | |
| 914 | case '$': |
| 915 | /* |
| 916 | * End of line specifier - if there's anything left after |
| 917 | * the '$' other than a close parens or alternation |
| 918 | * specifier, treat it as a normal character |
| 919 | */ |
| 920 | if (exprchars > 1 |
| 921 | && (expr.getch_at(1) != ')' && expr.getch_at(1) != '|')) |
| 922 | goto normal_char; |
| 923 | |
| 924 | /* build a new end-of-text recognizer */ |
| 925 | build_special(&new_machine, RE_TEXT_END, 0); |
| 926 | |
| 927 | /* |
| 928 | * concatenate it onto the string - note that this can't |
| 929 | * have any postfix operators |
| 930 | */ |
| 931 | concat_onto(&cur_machine, &new_machine); |
| 932 | break; |
| 933 | |
| 934 | case '(': |
| 935 | { |
| 936 | int capturing; |
| 937 | int pos_assertion; |
| 938 | int neg_assertion; |
| 939 | int back_assertion; |
| 940 | |
| 941 | /* presume it's a capturing group */ |
| 942 | capturing = TRUE; |
| 943 | |
| 944 | /* presume it's not an assertion */ |
| 945 | pos_assertion = FALSE; |
| 946 | neg_assertion = FALSE; |
| 947 | back_assertion = FALSE; |
| 948 | |
| 949 | /* |
| 950 | * Add a nesting level. Push the current machine and |
| 951 | * alternate machines onto the group stack, and clear |
| 952 | * everything out for the new group. |
| 953 | */ |
| 954 | if (group_stack_level > RE_GROUP_NESTING_MAX) |
| 955 | { |
| 956 | /* we cannot proceed - return an error */ |
| 957 | return RE_STATUS_GROUP_NESTING_TOO_DEEP; |
| 958 | } |
| 959 | |
| 960 | /* save the current state on the stack */ |
| 961 | group_stack[group_stack_level].old_cur = cur_machine; |
| 962 | group_stack[group_stack_level].old_alter = alter_machine; |
| 963 | |
| 964 | /* |
| 965 | * Assign the group a group ID - groups are numbered in |
| 966 | * order of their opening (left) parentheses, so we want |
| 967 | * to assign a group number now. We won't actually need |
| 968 | * to know the group number until we get to the matching |
| 969 | * close paren, but we need to assign it now, so store |
| 970 | * it in the group stack. |
| 971 | */ |
| 972 | group_stack[group_stack_level].group_id = pat->group_cnt; |
| 973 | |
| 974 | /* check for special group flags */ |
| 975 | if (exprchars > 2 && expr.getch_at(1) == '?') |
| 976 | { |
| 977 | switch(expr.getch_at(2)) |
| 978 | { |
| 979 | case '<': |
| 980 | /* look-back assertion */ |
| 981 | if (exprchars > 3 |
| 982 | && (expr.getch_at(3) == '=' |
| 983 | || expr.getch_at(3) == '!')) |
| 984 | { |
| 985 | /* it's a look-back assertion */ |
| 986 | back_assertion = TRUE; |
| 987 | |
| 988 | /* assertions don't capture */ |
| 989 | capturing = FALSE; |
| 990 | |
| 991 | /* note whether it's positive or negative */ |
| 992 | if (expr.getch_at(3) == '=') |
| 993 | pos_assertion = TRUE; |
| 994 | else |
| 995 | neg_assertion = TRUE; |
| 996 | |
| 997 | /* skip the '?<=' or '?<!' part */ |
| 998 | expr.inc(); |
| 999 | expr.inc(); |
| 1000 | expr.inc(); |
| 1001 | exprchars -= 3; |
| 1002 | } |
| 1003 | break; |
| 1004 | |
| 1005 | case ':': |
| 1006 | /* it's a non-capturing group */ |
| 1007 | capturing = FALSE; |
| 1008 | |
| 1009 | /* skip two extra characters for the '?:' part */ |
| 1010 | expr.inc(); |
| 1011 | expr.inc(); |
| 1012 | exprchars -= 2; |
| 1013 | break; |
| 1014 | |
| 1015 | case '=': |
| 1016 | /* it's a positive assertion group */ |
| 1017 | pos_assertion = TRUE; |
| 1018 | |
| 1019 | /* assertions don't capture */ |
| 1020 | capturing = FALSE; |
| 1021 | |
| 1022 | /* skip two extra characters for the '?=' part */ |
| 1023 | expr.inc(); |
| 1024 | expr.inc(); |
| 1025 | exprchars -= 2; |
| 1026 | break; |
| 1027 | |
| 1028 | case '!': |
| 1029 | /* it's a negative assertion group */ |
| 1030 | neg_assertion = TRUE; |
| 1031 | |
| 1032 | /* assertions don't capture */ |
| 1033 | capturing = FALSE; |
| 1034 | |
| 1035 | /* skip two extra characters for the '?!' part */ |
| 1036 | expr.inc(); |
| 1037 | expr.inc(); |
| 1038 | exprchars -= 2; |
| 1039 | break; |
| 1040 | |
| 1041 | default: |
| 1042 | /* it's not a recognized sequence - ignore it */ |
| 1043 | break; |
| 1044 | } |
| 1045 | } |
| 1046 | |
| 1047 | /* remember if the group is capturing */ |
| 1048 | group_stack[group_stack_level].capturing = capturing; |
| 1049 | |
| 1050 | /* remember if it's an assertion of some kind */ |
| 1051 | group_stack[group_stack_level].pos_assertion = pos_assertion; |
| 1052 | group_stack[group_stack_level].neg_assertion = neg_assertion; |
| 1053 | group_stack[group_stack_level].back_assertion = back_assertion; |
| 1054 | |
| 1055 | /* consume the group number if it's a capturing group */ |
| 1056 | if (capturing) |
| 1057 | ++(pat->group_cnt); |
| 1058 | |
| 1059 | /* push the level */ |
| 1060 | ++group_stack_level; |
| 1061 | |
| 1062 | /* start the new group with empty machines */ |
| 1063 | build_null_machine(&cur_machine); |
| 1064 | build_null_machine(&alter_machine); |
| 1065 | } |
| 1066 | break; |
| 1067 | |
| 1068 | case ')': |
| 1069 | do_close_paren: |
| 1070 | /* if there's nothing on the stack, ignore this */ |
| 1071 | if (group_stack_level == 0) |
| 1072 | break; |
| 1073 | |
| 1074 | /* take a level off the stack */ |
| 1075 | --group_stack_level; |
| 1076 | |
| 1077 | /* |
| 1078 | * For a lookback assertion, add the lookback parent position |
| 1079 | * match assertion to the group. This ensures that we can only |
| 1080 | * match the group when the match leaves us at the same |
| 1081 | * position where the lookback assertion was matched. |
| 1082 | */ |
| 1083 | if (group_stack[group_stack_level].back_assertion) |
| 1084 | { |
| 1085 | build_special(&new_machine, RE_LOOKBACK_POS, 0); |
| 1086 | concat_onto(&cur_machine, &new_machine); |
| 1087 | } |
| 1088 | |
| 1089 | /* |
| 1090 | * Remove a nesting level. If we have a pending alternate |
| 1091 | * expression, build the alternation expression. This will |
| 1092 | * leave the entire group expression in alter_machine, |
| 1093 | * regardless of whether an alternation was in progress or |
| 1094 | * not. |
| 1095 | */ |
| 1096 | alternate_onto(&alter_machine, &cur_machine); |
| 1097 | |
| 1098 | /* |
| 1099 | * Create a group machine that encloses the group and marks |
| 1100 | * it with a group number. We assigned the group number |
| 1101 | * when we parsed the open paren, so read that group number |
| 1102 | * from the stack. |
| 1103 | * |
| 1104 | * Note that this will leave 'new_machine' with the entire |
| 1105 | * group machine. |
| 1106 | * |
| 1107 | * If this is a non-capturing group, don't bother building |
| 1108 | * the new machine - just copy the current alternation |
| 1109 | * machine onto the new machine. |
| 1110 | */ |
| 1111 | if (group_stack[group_stack_level].capturing) |
| 1112 | { |
| 1113 | /* it's a regular capturing group - add the group machine */ |
| 1114 | build_group(&new_machine, &alter_machine, |
| 1115 | group_stack[group_stack_level].group_id); |
| 1116 | } |
| 1117 | else if (group_stack[group_stack_level].pos_assertion |
| 1118 | || group_stack[group_stack_level].neg_assertion) |
| 1119 | { |
| 1120 | /* it's an assertion - build the assertion group */ |
| 1121 | build_assert(&new_machine, &alter_machine, |
| 1122 | group_stack[group_stack_level].neg_assertion, |
| 1123 | group_stack[group_stack_level].back_assertion); |
| 1124 | } |
| 1125 | else |
| 1126 | { |
| 1127 | /* it's a non-capturing group - just add the group tree */ |
| 1128 | new_machine = alter_machine; |
| 1129 | } |
| 1130 | |
| 1131 | /* |
| 1132 | * Pop the stack - restore the alternation and current |
| 1133 | * machines that were in progress before the group started. |
| 1134 | */ |
| 1135 | cur_machine = group_stack[group_stack_level].old_cur; |
| 1136 | alter_machine = group_stack[group_stack_level].old_alter; |
| 1137 | |
| 1138 | /* |
| 1139 | * Check the group expression (in new_machine) for postfix |
| 1140 | * expressions |
| 1141 | */ |
| 1142 | goto apply_postfix; |
| 1143 | |
| 1144 | case '|': |
| 1145 | /* |
| 1146 | * Start a new alternation. This ends the current |
| 1147 | * alternation; if we have a previous pending alternate, |
| 1148 | * build an alternation machine out of the previous |
| 1149 | * alternate and the current machine and move that to the |
| 1150 | * alternate; otherwise, simply move the current machine to |
| 1151 | * the pending alternate. |
| 1152 | */ |
| 1153 | alternate_onto(&alter_machine, &cur_machine); |
| 1154 | |
| 1155 | /* |
| 1156 | * the alternation starts out with a blank slate, so null |
| 1157 | * out the current machine |
| 1158 | */ |
| 1159 | build_null_machine(&cur_machine); |
| 1160 | break; |
| 1161 | |
| 1162 | case '<': |
| 1163 | /* check for our various special directives */ |
| 1164 | if ((exprchars >= 4 && memicmp(expr.getptr(), "<FE>", 4) == 0) |
| 1165 | || (exprchars >= 10 |
| 1166 | && memicmp(expr.getptr(), "<FirstEnd>", 10) == 0)) |
| 1167 | { |
| 1168 | /* turn off first-begin mode */ |
| 1169 | pat->first_begin = FALSE; |
| 1170 | } |
| 1171 | else if ((exprchars >= 4 && memicmp(expr.getptr(), "<FB>", 4) == 0) |
| 1172 | || (exprchars >= 12 |
| 1173 | && memicmp(expr.getptr(), "<FirstBegin>", 12) == 0)) |
| 1174 | { |
| 1175 | /* turn on first-begin mode */ |
| 1176 | pat->first_begin = TRUE; |
| 1177 | } |
| 1178 | else if (exprchars >= 5 && memicmp(expr.getptr(), "<Max>", 5) == 0) |
| 1179 | { |
| 1180 | /* turn on longest-match mode */ |
| 1181 | pat->longest_match = TRUE; |
| 1182 | } |
| 1183 | else if (exprchars >= 5 && memicmp(expr.getptr(), "<Min>", 5) == 0) |
| 1184 | { |
| 1185 | /* turn off longest-match mode */ |
| 1186 | pat->longest_match = FALSE; |
| 1187 | } |
| 1188 | else if (exprchars >= 6 |
| 1189 | && memicmp(expr.getptr(), "<Case>", 6) == 0) |
| 1190 | { |
| 1191 | /* turn on case sensitivity */ |
| 1192 | pat->case_sensitive = TRUE; |
| 1193 | pat->case_sensitivity_specified = TRUE; |
| 1194 | } |
| 1195 | else if (exprchars >= 8 |
| 1196 | && memicmp(expr.getptr(), "<NoCase>", 8) == 0) |
| 1197 | { |
| 1198 | /* turn off case sensitivity */ |
| 1199 | pat->case_sensitive = FALSE; |
| 1200 | pat->case_sensitivity_specified = TRUE; |
| 1201 | } |
| 1202 | else |
| 1203 | { |
| 1204 | /* |
| 1205 | * It's nothing else we recognize, so it must be a |
| 1206 | * character class or class range expression, which |
| 1207 | * consists of one or more classes, single characters, or |
| 1208 | * character ranges separated by '|' delimiters. |
| 1209 | */ |
| 1210 | if (compile_char_class_expr(&expr, &exprchars, &new_machine)) |
| 1211 | { |
| 1212 | /* success - look for postfix operators */ |
| 1213 | goto apply_postfix; |
| 1214 | } |
| 1215 | else |
| 1216 | { |
| 1217 | /* |
| 1218 | * failure - treat the whole thing as ordinary |
| 1219 | * characters |
| 1220 | */ |
| 1221 | goto normal_char; |
| 1222 | } |
| 1223 | } |
| 1224 | |
| 1225 | /* skip everything up to the closing ">" */ |
| 1226 | while (exprchars > 0 && expr.getch() != '>') |
| 1227 | expr.inc(), --exprchars ; |
| 1228 | break; |
| 1229 | |
| 1230 | case '%': |
| 1231 | /* |
| 1232 | * quoted character - skip the quote mark and see what we |
| 1233 | * have |
| 1234 | */ |
| 1235 | expr.inc(); |
| 1236 | --exprchars; |
| 1237 | |
| 1238 | /* check to see if we're at the end of the expression */ |
| 1239 | if (exprchars == 0) |
| 1240 | { |
| 1241 | /* |
| 1242 | * end of the string - ignore it, but undo the extra |
| 1243 | * increment of the expression index so that we exit the |
| 1244 | * enclosing loop properly |
| 1245 | */ |
| 1246 | expr.dec(); |
| 1247 | ++exprchars; |
| 1248 | break; |
| 1249 | } |
| 1250 | |
| 1251 | /* see what we have */ |
| 1252 | switch(expr.getch()) |
| 1253 | { |
| 1254 | case '1': |
| 1255 | case '2': |
| 1256 | case '3': |
| 1257 | case '4': |
| 1258 | case '5': |
| 1259 | case '6': |
| 1260 | case '7': |
| 1261 | case '8': |
| 1262 | case '9': |
| 1263 | /* group match - build a new literal group recognizer */ |
| 1264 | build_group_matcher(&new_machine, |
| 1265 | value_of_digit(expr.getch()) - 1); |
| 1266 | |
| 1267 | /* apply any postfix expression to the group recognizer */ |
| 1268 | goto apply_postfix; |
| 1269 | |
| 1270 | case '<': |
| 1271 | /* build a beginning-of-word recognizer */ |
| 1272 | build_special(&new_machine, RE_WORD_BEGIN, 0); |
| 1273 | |
| 1274 | /* it can't be postfixed - just concatenate it */ |
| 1275 | concat_onto(&cur_machine, &new_machine); |
| 1276 | break; |
| 1277 | |
| 1278 | case '>': |
| 1279 | /* build an end-of-word recognizer */ |
| 1280 | build_special(&new_machine, RE_WORD_END, 0); |
| 1281 | |
| 1282 | /* it can't be postfixed - just concatenate it */ |
| 1283 | concat_onto(&cur_machine, &new_machine); |
| 1284 | break; |
| 1285 | |
| 1286 | case 'w': |
| 1287 | /* word character */ |
| 1288 | build_special(&new_machine, RE_WORD_CHAR, 0); |
| 1289 | goto apply_postfix; |
| 1290 | |
| 1291 | case 'W': |
| 1292 | /* non-word character */ |
| 1293 | build_special(&new_machine, RE_NON_WORD_CHAR, 0); |
| 1294 | goto apply_postfix; |
| 1295 | |
| 1296 | case 'b': |
| 1297 | /* word boundary */ |
| 1298 | build_special(&new_machine, RE_WORD_BOUNDARY, 0); |
| 1299 | |
| 1300 | /* it can't be postfixed */ |
| 1301 | concat_onto(&cur_machine, &new_machine); |
| 1302 | break; |
| 1303 | |
| 1304 | case 'B': |
| 1305 | /* not a word boundary */ |
| 1306 | build_special(&new_machine, RE_NON_WORD_BOUNDARY, 0); |
| 1307 | |
| 1308 | /* it can't be postfixed */ |
| 1309 | concat_onto(&cur_machine, &new_machine); |
| 1310 | break; |
| 1311 | |
| 1312 | case 's': |
| 1313 | /* %s -> same as <space> */ |
| 1314 | build_special(&new_machine, RE_SPACE, 0); |
| 1315 | goto apply_postfix; |
| 1316 | |
| 1317 | case 'S': |
| 1318 | /* %S -> same as <^space> */ |
| 1319 | build_special(&new_machine, RE_NON_SPACE, 0); |
| 1320 | goto apply_postfix; |
| 1321 | |
| 1322 | case 'd': |
| 1323 | /* %d -> same as <digit> */ |
| 1324 | build_special(&new_machine, RE_DIGIT, 0); |
| 1325 | goto apply_postfix; |
| 1326 | |
| 1327 | case 'D': |
| 1328 | /* %D -> same as <^digit> */ |
| 1329 | build_special(&new_machine, RE_NON_DIGIT, 0); |
| 1330 | goto apply_postfix; |
| 1331 | |
| 1332 | case 'v': |
| 1333 | /* %v -> same as <vspace> */ |
| 1334 | build_special(&new_machine, RE_VSPACE, 0); |
| 1335 | goto apply_postfix; |
| 1336 | |
| 1337 | case 'V': |
| 1338 | /* %V -> same as <^vspace> */ |
| 1339 | build_special(&new_machine, RE_NON_VSPACE, 0); |
| 1340 | goto apply_postfix; |
| 1341 | |
| 1342 | default: |
| 1343 | /* build a new literal character recognizer */ |
| 1344 | build_char(&new_machine, expr.getch()); |
| 1345 | |
| 1346 | /* apply any postfix expression to the character */ |
| 1347 | goto apply_postfix; |
| 1348 | } |
| 1349 | break; |
| 1350 | |
| 1351 | case '.': |
| 1352 | /* |
| 1353 | * wildcard character - build a single character recognizer |
| 1354 | * for the special wildcard symbol, then go check it for a |
| 1355 | * postfix operator |
| 1356 | */ |
| 1357 | build_special(&new_machine, RE_WILDCARD, 0); |
| 1358 | goto apply_postfix; |
| 1359 | break; |
| 1360 | |
| 1361 | case '[': |
| 1362 | /* range expression */ |
| 1363 | { |
| 1364 | int is_exclusive = FALSE; |
| 1365 | |
| 1366 | /* we have no entries yet */ |
| 1367 | range_buf_cnt_ = 0; |
| 1368 | |
| 1369 | /* first, skip the open bracket */ |
| 1370 | expr.inc(); |
| 1371 | --exprchars; |
| 1372 | |
| 1373 | /* check to see if starts with the exclusion character */ |
| 1374 | if (exprchars != 0 && expr.getch() == '^') |
| 1375 | { |
| 1376 | /* skip the exclusion specifier */ |
| 1377 | expr.inc(); |
| 1378 | --exprchars; |
| 1379 | |
| 1380 | /* note it */ |
| 1381 | is_exclusive = TRUE; |
| 1382 | } |
| 1383 | |
| 1384 | /* |
| 1385 | * if the first character is a ']', include it in the |
| 1386 | * range |
| 1387 | */ |
| 1388 | if (exprchars != 0 && expr.getch() == ']') |
| 1389 | { |
| 1390 | add_range_char(']'); |
| 1391 | expr.inc(); |
| 1392 | --exprchars; |
| 1393 | } |
| 1394 | |
| 1395 | /* |
| 1396 | * if the next character is a '-', include it in the |
| 1397 | * range |
| 1398 | */ |
| 1399 | if (exprchars != 0 && expr.getch() == '-') |
| 1400 | { |
| 1401 | add_range_char('-'); |
| 1402 | expr.inc(); |
| 1403 | --exprchars; |
| 1404 | } |
| 1405 | |
| 1406 | /* scan the character set */ |
| 1407 | while (exprchars != 0 && expr.getch() != ']') |
| 1408 | { |
| 1409 | /* note this character */ |
| 1410 | wchar_t ch = expr.getch(); |
| 1411 | |
| 1412 | /* skip this character of the expression */ |
| 1413 | expr.inc(); |
| 1414 | --exprchars; |
| 1415 | |
| 1416 | /* check for a range */ |
| 1417 | if (exprchars != 0 && expr.getch() == '-') |
| 1418 | { |
| 1419 | /* skip the '-' */ |
| 1420 | expr.inc(); |
| 1421 | --exprchars; |
| 1422 | if (exprchars != 0) |
| 1423 | { |
| 1424 | /* get the other end of the range */ |
| 1425 | wchar_t ch2 = expr.getch(); |
| 1426 | |
| 1427 | /* skip the second character */ |
| 1428 | expr.inc(); |
| 1429 | --exprchars; |
| 1430 | |
| 1431 | /* if the range is reversed, swap it */ |
| 1432 | if (ch > ch2) |
| 1433 | { |
| 1434 | wchar_t tmp = ch; |
| 1435 | ch = ch2; |
| 1436 | ch2 = tmp; |
| 1437 | } |
| 1438 | |
| 1439 | /* add the range */ |
| 1440 | add_range_char(ch, ch2); |
| 1441 | } |
| 1442 | } |
| 1443 | else |
| 1444 | { |
| 1445 | /* no range - add the one-character range */ |
| 1446 | add_range_char(ch); |
| 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | /* create a character range machine */ |
| 1451 | build_char_range(&new_machine, is_exclusive); |
| 1452 | |
| 1453 | /* apply any postfix operator */ |
| 1454 | goto apply_postfix; |
| 1455 | } |
| 1456 | break; |
| 1457 | |
| 1458 | default: |
| 1459 | normal_char: |
| 1460 | /* |
| 1461 | * it's an ordinary character - build a single character |
| 1462 | * recognizer machine, and then concatenate it onto any |
| 1463 | * existing machine |
| 1464 | */ |
| 1465 | build_char(&new_machine, expr.getch()); |
| 1466 | |
| 1467 | apply_postfix: |
| 1468 | /* |
| 1469 | * Check for a postfix operator, and apply it to the machine |
| 1470 | * in 'new_machine' if present. In any case, concatenate |
| 1471 | * the 'new_machine' (modified by a postix operator or not) |
| 1472 | * to the current machien. |
| 1473 | */ |
| 1474 | if (exprchars > 1) |
| 1475 | { |
| 1476 | switch(expr.getch_at(1)) |
| 1477 | { |
| 1478 | case '*': |
| 1479 | case '+': |
| 1480 | case '?': |
| 1481 | /* |
| 1482 | * We have a postfix closure operator. Build a new |
| 1483 | * closure machine out of 'new_machine'. |
| 1484 | */ |
| 1485 | { |
| 1486 | /* move onto the closure operator */ |
| 1487 | expr.inc(); |
| 1488 | --exprchars; |
| 1489 | |
| 1490 | /* |
| 1491 | * if the next character is '?', it's a modifier |
| 1492 | * that indicates that we are to use the |
| 1493 | * shortest match - note it if so |
| 1494 | */ |
| 1495 | int shortest = |
| 1496 | (exprchars > 1 && expr.getch_at(1) == '?'); |
| 1497 | |
| 1498 | /* build the closure machine */ |
| 1499 | re_machine closure_machine; |
| 1500 | build_closure(&closure_machine, |
| 1501 | &new_machine, expr.getch(), shortest); |
| 1502 | |
| 1503 | /* replace the original machine with the closure */ |
| 1504 | new_machine = closure_machine; |
| 1505 | } |
| 1506 | |
| 1507 | /* |
| 1508 | * skip any redundant closure symbols, keeping only the |
| 1509 | * first one we saw |
| 1510 | */ |
| 1511 | skip_closures: |
| 1512 | while (exprchars > 1) |
| 1513 | { |
| 1514 | /* check for a simple closure suffix */ |
| 1515 | if (expr.getch_at(1) == '?' |
| 1516 | || expr.getch_at(1) == '+' |
| 1517 | || expr.getch_at(1) == '*') |
| 1518 | { |
| 1519 | /* skip it and keep looping */ |
| 1520 | expr.inc(); |
| 1521 | --exprchars; |
| 1522 | continue; |
| 1523 | } |
| 1524 | |
| 1525 | /* check for an interval */ |
| 1526 | if (expr.getch_at(1) == '{') |
| 1527 | { |
| 1528 | /* skip until we find the matching '}' */ |
| 1529 | while (exprchars > 1 && expr.getch_at(1) != '}') |
| 1530 | expr.inc(), --exprchars; |
| 1531 | |
| 1532 | /* go back for anything that follows */ |
| 1533 | continue; |
| 1534 | } |
| 1535 | |
| 1536 | /* if it's anything else, we're done discarding */ |
| 1537 | break; |
| 1538 | } |
| 1539 | break; |
| 1540 | |
| 1541 | case '{': |
| 1542 | /* interval specifier */ |
| 1543 | { |
| 1544 | int min_val; |
| 1545 | int max_val; |
| 1546 | re_machine interval_machine; |
| 1547 | int shortest; |
| 1548 | int var_id; |
| 1549 | |
| 1550 | /* |
| 1551 | * loops can never overlap, but can be nested; |
| 1552 | * so the only thing we have to worry about in |
| 1553 | * assigning a loop variable is the group |
| 1554 | * nesting depth |
| 1555 | */ |
| 1556 | var_id = group_stack_level; |
| 1557 | |
| 1558 | /* note the highest variable ID we've seen */ |
| 1559 | if (var_id >= pat->loop_var_cnt) |
| 1560 | pat->loop_var_cnt = var_id + 1; |
| 1561 | |
| 1562 | /* presume neither min nor max will be specified */ |
| 1563 | min_val = -1; |
| 1564 | max_val = -1; |
| 1565 | |
| 1566 | /* skip the current character and the '{' */ |
| 1567 | expr.inc(); |
| 1568 | expr.inc(); |
| 1569 | exprchars -= 2; |
| 1570 | |
| 1571 | /* parse the minimum count, if provided */ |
| 1572 | min_val = parse_int(&expr, &exprchars); |
| 1573 | |
| 1574 | /* if there's a comma, parse the maximum value */ |
| 1575 | if (exprchars >= 1 && expr.getch() == ',') |
| 1576 | { |
| 1577 | /* skip the ',' and parse the number */ |
| 1578 | expr.inc(); |
| 1579 | --exprchars; |
| 1580 | max_val = parse_int(&expr, &exprchars); |
| 1581 | } |
| 1582 | else |
| 1583 | { |
| 1584 | /* |
| 1585 | * there's no other value, so this is a |
| 1586 | * simple loop with the same value for min |
| 1587 | * and max |
| 1588 | */ |
| 1589 | max_val = min_val; |
| 1590 | } |
| 1591 | |
| 1592 | /* |
| 1593 | * if we're not looking at a '}', skip |
| 1594 | * characters until we are |
| 1595 | */ |
| 1596 | while (exprchars != 0 && expr.getch() != '}') |
| 1597 | expr.inc(&exprchars); |
| 1598 | |
| 1599 | /* |
| 1600 | * if there's a '?' following, it's a 'shortest' |
| 1601 | * modifier - note it |
| 1602 | */ |
| 1603 | shortest = FALSE; |
| 1604 | if (exprchars > 1 && expr.getch_at(1) == '?') |
| 1605 | { |
| 1606 | /* note the modifier */ |
| 1607 | shortest = TRUE; |
| 1608 | |
| 1609 | /* skip another character for the modifier */ |
| 1610 | expr.inc(); |
| 1611 | --exprchars; |
| 1612 | } |
| 1613 | |
| 1614 | /* set up an interval node */ |
| 1615 | build_interval(&interval_machine, &new_machine, |
| 1616 | min_val, max_val, var_id, shortest); |
| 1617 | |
| 1618 | /* replace the original machine with the interval */ |
| 1619 | new_machine = interval_machine; |
| 1620 | |
| 1621 | /* skip any closure modifiers that follow */ |
| 1622 | goto skip_closures; |
| 1623 | } |
| 1624 | break; |
| 1625 | |
| 1626 | default: |
| 1627 | /* no postfix operator */ |
| 1628 | break; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | /* |
| 1633 | * Concatenate the new machine onto the current machine |
| 1634 | * under construction. |
| 1635 | */ |
| 1636 | concat_onto(&cur_machine, &new_machine); |
| 1637 | break; |
| 1638 | } |
| 1639 | |
| 1640 | /* if we've run down the expression string, go no further */ |
| 1641 | if (exprchars == 0) |
| 1642 | break; |
| 1643 | } |
| 1644 | |
| 1645 | /* if there are any open parens outstanding, close them */ |
| 1646 | if (group_stack_level != 0) |
| 1647 | goto do_close_paren; |
| 1648 | |
| 1649 | /* complete any pending alternation */ |
| 1650 | alternate_onto(&alter_machine, &cur_machine); |
| 1651 | |
| 1652 | /* check for and break any infinite loops */ |
| 1653 | break_loops(&alter_machine); |
| 1654 | |
| 1655 | /* remove meaningless branch-to-branch transitions */ |
| 1656 | remove_branch_to_branch(&alter_machine); |
| 1657 | |
| 1658 | /* consolidate consecutive character transitions into strings */ |
| 1659 | consolidate_strings(&alter_machine); |
| 1660 | |
| 1661 | /* store the results in the caller's base pattern description */ |
| 1662 | pat->machine = alter_machine; |
| 1663 | pat->tuple_cnt = next_state_; |
| 1664 | |
| 1665 | /* limit the group count to the maximum */ |
| 1666 | if (pat->group_cnt > RE_GROUP_REG_CNT) |
| 1667 | pat->group_cnt = RE_GROUP_REG_CNT; |
| 1668 | |
| 1669 | /* limit the variable count to the maximum */ |
| 1670 | if (pat->loop_var_cnt > RE_LOOP_VARS_MAX) |
| 1671 | pat->loop_var_cnt = RE_LOOP_VARS_MAX; |
| 1672 | |
| 1673 | /* no errors encountered */ |
| 1674 | return RE_STATUS_SUCCESS; |
| 1675 | } |
| 1676 | |
| 1677 | |
| 1678 | /* |
| 1679 | * Parse a character class or class range expresion. Returns true on |
| 1680 | * success, in which case we will have built a class range expression in |
| 1681 | * new_machine and updated expr and exprlen to skip the expression. |
| 1682 | * Returns false on syntax error or other failure, in which case expr and |
| 1683 | * exprlen will be unchanged. |
| 1684 | */ |
| 1685 | int CRegexParser::compile_char_class_expr(utf8_ptr *expr, size_t *exprchars, |
| 1686 | re_machine *result_machine) |
| 1687 | { |
| 1688 | /* start at the character after the '<' */ |
| 1689 | utf8_ptr p = *expr; |
| 1690 | size_t chrem = *exprchars; |
| 1691 | p.inc(); |
| 1692 | --chrem; |
| 1693 | |
| 1694 | /* presume it won't be exclusive */ |
| 1695 | int is_exclusive = FALSE; |
| 1696 | |
| 1697 | /* check for an exclusion flag */ |
| 1698 | if (chrem != 0 && p.getch() == '^') |
| 1699 | { |
| 1700 | /* note the exclusion */ |
| 1701 | is_exclusive = TRUE; |
| 1702 | |
| 1703 | /* skip the exclusion prefix */ |
| 1704 | p.inc(); |
| 1705 | --chrem; |
| 1706 | } |
| 1707 | |
| 1708 | /* clear out the range builder buffer */ |
| 1709 | range_buf_cnt_ = 0; |
| 1710 | |
| 1711 | /* |
| 1712 | * keep going until we find the closing delimiter (or run out of |
| 1713 | * expression string) |
| 1714 | */ |
| 1715 | for (;;) |
| 1716 | { |
| 1717 | utf8_ptr start; |
| 1718 | wchar_t delim; |
| 1719 | size_t curcharlen; |
| 1720 | size_t curbytelen; |
| 1721 | |
| 1722 | /* remember where the current part starts */ |
| 1723 | start = p; |
| 1724 | |
| 1725 | /* scan for the '>' or '|' */ |
| 1726 | for (curcharlen = 0 ; chrem != 0 ; p.inc(), --chrem, ++curcharlen) |
| 1727 | { |
| 1728 | /* get the next character */ |
| 1729 | delim = p.getch(); |
| 1730 | |
| 1731 | /* if it's '>' or '|', we're done */ |
| 1732 | if (delim == '>' || delim == '|') |
| 1733 | break; |
| 1734 | } |
| 1735 | |
| 1736 | /* |
| 1737 | * If we reached the end of the expression without finding a |
| 1738 | * closing delimiter, the expression is invalid - treat the whole |
| 1739 | * thing (starting with the '<') as ordinary characters. |
| 1740 | */ |
| 1741 | if (chrem == 0) |
| 1742 | return FALSE; |
| 1743 | |
| 1744 | /* get the length of this part */ |
| 1745 | curbytelen = (size_t)(p.getptr() - start.getptr()); |
| 1746 | |
| 1747 | /* |
| 1748 | * See what we have. If we have a single character, it's a |
| 1749 | * literal. If we have a character, a hyphen, and another |
| 1750 | * character, it's a literal range. Otherwise, it must be one of |
| 1751 | * our named classes. |
| 1752 | */ |
| 1753 | if (curcharlen == 1) |
| 1754 | { |
| 1755 | /* it's a literal - add the character */ |
| 1756 | add_range_char(start.getch()); |
| 1757 | } |
| 1758 | else if (curcharlen == 3 && start.getch_at(1) == '-') |
| 1759 | { |
| 1760 | /* it's a literal range - add it */ |
| 1761 | add_range_char(start.getch(), start.getch_at(2)); |
| 1762 | } |
| 1763 | else |
| 1764 | { |
| 1765 | struct char_class_t |
| 1766 | { |
| 1767 | /* expression name for the class */ |
| 1768 | const char *name; |
| 1769 | |
| 1770 | /* length of the class name */ |
| 1771 | size_t name_len; |
| 1772 | |
| 1773 | /* |
| 1774 | * literal character, if the name represents a character |
| 1775 | * rather than a class - this is used only if code == |
| 1776 | * RE_LITERAL |
| 1777 | */ |
| 1778 | wchar_t ch; |
| 1779 | |
| 1780 | /* RE_xxx code for the class */ |
| 1781 | re_recog_type code; |
| 1782 | }; |
| 1783 | static const char_class_t classes[] = |
| 1784 | { |
| 1785 | { "alpha", 5, 0, RE_ALPHA }, |
| 1786 | { "digit", 5, 0, RE_DIGIT }, |
| 1787 | { "upper", 5, 0, RE_UPPER }, |
| 1788 | { "lower", 5, 0, RE_LOWER }, |
| 1789 | { "alphanum", 8, 0, RE_ALPHANUM }, |
| 1790 | { "space", 5, 0, RE_SPACE }, |
| 1791 | { "punct", 5, 0, RE_PUNCT }, |
| 1792 | { "newline", 7, 0, RE_NEWLINE }, |
| 1793 | { "vspace", 6, 0, RE_VSPACE }, |
| 1794 | { "langle", 6, '<', RE_LITERAL }, |
| 1795 | { "rangle", 6, '>', RE_LITERAL }, |
| 1796 | { "vbar", 4, '|', RE_LITERAL }, |
| 1797 | { "caret", 5, '^', RE_LITERAL }, |
| 1798 | { "squote", 6, '\'', RE_LITERAL }, |
| 1799 | { "dquote", 6, '"', RE_LITERAL }, |
| 1800 | { "star", 4, '*', RE_LITERAL }, |
| 1801 | { "question", 8, '?', RE_LITERAL }, |
| 1802 | { "percent", 7, '%', RE_LITERAL }, |
| 1803 | { "dot", 3, '.', RE_LITERAL }, |
| 1804 | { "period", 6, '.', RE_LITERAL }, |
| 1805 | { "plus", 4, '+', RE_LITERAL }, |
| 1806 | { "lsquare", 7, '[', RE_LITERAL }, |
| 1807 | { "rsquare", 7, ']', RE_LITERAL }, |
| 1808 | { "lparen", 6, '(', RE_LITERAL }, |
| 1809 | { "rparen", 6, ')', RE_LITERAL}, |
| 1810 | { "lbrace", 6, '{', RE_LITERAL }, |
| 1811 | { "rbrace", 6, '}', RE_LITERAL }, |
| 1812 | { "dollar", 6, '$', RE_LITERAL }, |
| 1813 | { "backslash",9, '\\', RE_LITERAL }, |
| 1814 | { "return", 6, 0x000D, RE_LITERAL }, |
| 1815 | { "linefeed", 8, 0x000A, RE_LITERAL }, |
| 1816 | { "tab", 3, 0x0009, RE_LITERAL }, |
| 1817 | { "nul", 3, 0x0000, RE_LITERAL }, |
| 1818 | { "null", 4, 0x0000, RE_LITERAL } |
| 1819 | }; |
| 1820 | const char_class_t *cp; |
| 1821 | size_t i; |
| 1822 | int found; |
| 1823 | |
| 1824 | /* scan our name list for a match */ |
| 1825 | for (cp = classes, i = 0, found = FALSE ; |
| 1826 | i < sizeof(classes)/sizeof(classes[0]) ; |
| 1827 | ++i, ++cp) |
| 1828 | { |
| 1829 | /* |
| 1830 | * if the length matches and the name matches (ignoring |
| 1831 | * case), this is the one we want |
| 1832 | */ |
| 1833 | if (curbytelen == cp->name_len |
| 1834 | && memicmp(start.getptr(), cp->name, curbytelen) == 0) |
| 1835 | { |
| 1836 | /* |
| 1837 | * this is it - add either a class range or literal |
| 1838 | * range, depending on the meaning of the name |
| 1839 | */ |
| 1840 | if (cp->code == RE_LITERAL) |
| 1841 | { |
| 1842 | /* it's a name for a literal */ |
| 1843 | add_range_char(cp->ch); |
| 1844 | } |
| 1845 | else |
| 1846 | { |
| 1847 | /* |
| 1848 | * It's a name for a character class. As a |
| 1849 | * special case for efficiency, if this is the one |
| 1850 | * and only thing in this class expression, don't |
| 1851 | * create a range expression but instead create a |
| 1852 | * special for the class. |
| 1853 | * |
| 1854 | * Note that we can't do this for an exclusive |
| 1855 | * class, since we don't have any special matcher |
| 1856 | * for those - implement those with a character |
| 1857 | * range as usual. |
| 1858 | */ |
| 1859 | if (range_buf_cnt_ == 0 |
| 1860 | && delim == '>' |
| 1861 | && !is_exclusive) |
| 1862 | { |
| 1863 | /* |
| 1864 | * This is the only thing, so build a special |
| 1865 | * to match this class - this is more |
| 1866 | * efficient to store and to match than a |
| 1867 | * range expression. |
| 1868 | */ |
| 1869 | build_special(result_machine, cp->code, 0); |
| 1870 | |
| 1871 | /* skip to the '>' */ |
| 1872 | *expr = p; |
| 1873 | *exprchars = chrem; |
| 1874 | |
| 1875 | /* |
| 1876 | * we're done with the expresion - tell the |
| 1877 | * caller we were successful |
| 1878 | */ |
| 1879 | return TRUE; |
| 1880 | } |
| 1881 | else |
| 1882 | { |
| 1883 | /* |
| 1884 | * it's not the only thing, so add the class |
| 1885 | * to the range list |
| 1886 | */ |
| 1887 | add_range_class(cp->code); |
| 1888 | } |
| 1889 | } |
| 1890 | |
| 1891 | /* note that we found a match */ |
| 1892 | found = TRUE; |
| 1893 | |
| 1894 | /* no need to scan further in our table */ |
| 1895 | break; |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | /* if we didn't find a match, the whole expression is invalid */ |
| 1900 | if (!found) |
| 1901 | return FALSE; |
| 1902 | } |
| 1903 | |
| 1904 | /* |
| 1905 | * if we found the '>', we're done; if we found a '|', skip it and |
| 1906 | * keep going |
| 1907 | */ |
| 1908 | if (delim == '|') |
| 1909 | { |
| 1910 | /* skip the delimiter, and back for another round */ |
| 1911 | p.inc(&chrem); |
| 1912 | } |
| 1913 | else |
| 1914 | { |
| 1915 | /* we found the '>', so we're done - add the range recognizer */ |
| 1916 | build_char_range(result_machine, is_exclusive); |
| 1917 | |
| 1918 | /* skip up to the '>' */ |
| 1919 | *expr = p; |
| 1920 | *exprchars = chrem; |
| 1921 | |
| 1922 | /* tell the caller we were successful */ |
| 1923 | return TRUE; |
| 1924 | } |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | |
| 1929 | /* |
| 1930 | * Parse an integer value. Returns -1 if there's no number. |
| 1931 | */ |
| 1932 | int CRegexParser::parse_int(utf8_ptr *p, size_t *chrem) |
| 1933 | { |
| 1934 | /* if it's not a number to start with, simply return -1 */ |
| 1935 | if (*chrem == 0 || !is_digit(p->getch())) |
| 1936 | return -1; |
| 1937 | |
| 1938 | /* keep going as long as we find digits */ |
| 1939 | int acc; |
| 1940 | for (acc = 0 ; *chrem > 0 && is_digit(p->getch()) ; p->inc(), --*chrem) |
| 1941 | { |
| 1942 | /* add this digit into the accumulator */ |
| 1943 | acc *= 10; |
| 1944 | acc += value_of_digit(p->getch()); |
| 1945 | } |
| 1946 | |
| 1947 | /* return the accumulated result */ |
| 1948 | return acc; |
| 1949 | } |
| 1950 | |
| 1951 | /* |
| 1952 | * Expression scan stack frame. We use this to keep track of the callers |
| 1953 | * in a recursive scan of the expression array, so that we can detect when |
| 1954 | * we're entering an infinite recursion. |
| 1955 | */ |
| 1956 | struct regex_scan_frame |
| 1957 | { |
| 1958 | regex_scan_frame(regex_scan_frame *parent) |
| 1959 | { |
| 1960 | this->st = RE_STATE_INVALID; |
| 1961 | this->parent = parent; |
| 1962 | } |
| 1963 | |
| 1964 | /* |
| 1965 | * Check to see if the current state is recursing. If the current |
| 1966 | * frame's state also appears in a parent frame, the state is |
| 1967 | * recursing. |
| 1968 | */ |
| 1969 | int is_recursing() |
| 1970 | { |
| 1971 | /* search up the parent chain for a duplicate of my state */ |
| 1972 | for (regex_scan_frame *fr = parent ; fr != 0 ; fr = fr->parent) |
| 1973 | { |
| 1974 | if (fr->st == st) |
| 1975 | return TRUE; |
| 1976 | } |
| 1977 | |
| 1978 | /* didn't find it */ |
| 1979 | return FALSE; |
| 1980 | } |
| 1981 | |
| 1982 | /* the state ID we're visiting in this frame */ |
| 1983 | re_state_id st; |
| 1984 | |
| 1985 | /* the parent frame */ |
| 1986 | regex_scan_frame *parent; |
| 1987 | }; |
| 1988 | |
| 1989 | /* |
| 1990 | * Break any infinite loops in the machine. Check for cycles that |
| 1991 | * consist solely of epsilon transitions, and break each cycle at the |
| 1992 | * last alternating transition. |
| 1993 | */ |
| 1994 | void CRegexParser::break_loops(re_machine *machine) |
| 1995 | { |
| 1996 | /* break loops starting at the initial state */ |
| 1997 | if (break_loops(machine->init, machine->final, 0)) |
| 1998 | { |
| 1999 | /* |
| 2000 | * The initial state was a looping state, so the entire expression |
| 2001 | * will consume no input and will just loop forever. Consider this |
| 2002 | * an always-matching zero-length expression, so set the initial |
| 2003 | * state to point to the final state. |
| 2004 | */ |
| 2005 | machine->final = machine->init; |
| 2006 | } |
| 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * Find and break loops from the current state back to the given initial |
| 2011 | * state completely via epsilon transitions. Returns true if we found a |
| 2012 | * loop back to the initial state, false if not. |
| 2013 | */ |
| 2014 | int CRegexParser::break_loops(re_state_id init, re_state_id final, |
| 2015 | regex_scan_frame *stack) |
| 2016 | { |
| 2017 | /* set up the scan frame for this level */ |
| 2018 | regex_scan_frame fr(stack); |
| 2019 | |
| 2020 | /* scan the chain of states from the current state */ |
| 2021 | for (re_state_id cur_state = init ; ; ) |
| 2022 | { |
| 2023 | /* if this is the final state, we're done */ |
| 2024 | if (cur_state == final) |
| 2025 | return FALSE; |
| 2026 | |
| 2027 | /* get the tuple for this state */ |
| 2028 | re_tuple *tuple = &tuple_arr_[cur_state]; |
| 2029 | |
| 2030 | /* set the current state in the recursion frame */ |
| 2031 | fr.st = cur_state; |
| 2032 | |
| 2033 | /* if it's a two-transition epsilon state, recurse */ |
| 2034 | if (tuple->typ == RE_EPSILON |
| 2035 | && tuple->next_state_2 != RE_STATE_INVALID) |
| 2036 | { |
| 2037 | /* |
| 2038 | * This is a branching state, so we need to recursively break |
| 2039 | * loops in each branch. If we're looping on the current |
| 2040 | * state, tell the caller that this whole branch is a loop. |
| 2041 | */ |
| 2042 | if (fr.is_recursing()) |
| 2043 | return TRUE; |
| 2044 | |
| 2045 | /* try breaking loops in the first branch */ |
| 2046 | if (break_loops(tuple->next_state_2, final, &fr)) |
| 2047 | { |
| 2048 | /* the second branch loops - skip it */ |
| 2049 | tuple->next_state_2 = RE_STATE_INVALID; |
| 2050 | } |
| 2051 | |
| 2052 | /* check the other branch */ |
| 2053 | if (break_loops(tuple->next_state_1, final, &fr)) |
| 2054 | { |
| 2055 | /* |
| 2056 | * the first branch loops - move the second branch to |
| 2057 | * the first slot, and clear the second slot (if we only |
| 2058 | * have one branch, it must always be in the first slot) |
| 2059 | */ |
| 2060 | tuple->next_state_1 = tuple->next_state_2; |
| 2061 | tuple->next_state_2 = RE_STATE_INVALID; |
| 2062 | } |
| 2063 | |
| 2064 | /* |
| 2065 | * If both branches loop, this entire state is a loop, so tell |
| 2066 | * the caller to break its branch to here. Otherwise, we've |
| 2067 | * finished with the rest of this chain. |
| 2068 | */ |
| 2069 | return (tuple->next_state_1 == RE_STATE_INVALID); |
| 2070 | } |
| 2071 | |
| 2072 | /* see what kind of transition this is */ |
| 2073 | switch(tuple->typ) |
| 2074 | { |
| 2075 | case RE_EPSILON: |
| 2076 | case RE_GROUP_ENTER: |
| 2077 | case RE_GROUP_EXIT: |
| 2078 | /* |
| 2079 | * epsilon or group transition - this could definitely be part |
| 2080 | * of a loop, so move on to the next state and keep looking |
| 2081 | */ |
| 2082 | cur_state = tuple->next_state_1; |
| 2083 | |
| 2084 | /* |
| 2085 | * if this took us to an invalid state, we must have reached |
| 2086 | * the final state of a sub-machine - we can go no further |
| 2087 | * from here, so there are no loops in this branch |
| 2088 | */ |
| 2089 | if (cur_state == RE_STATE_INVALID) |
| 2090 | return FALSE; |
| 2091 | break; |
| 2092 | |
| 2093 | case RE_TEXT_BEGIN: |
| 2094 | case RE_TEXT_END: |
| 2095 | case RE_LOOKBACK_POS: |
| 2096 | case RE_WORD_BEGIN: |
| 2097 | case RE_WORD_END: |
| 2098 | case RE_WORD_BOUNDARY: |
| 2099 | case RE_NON_WORD_BOUNDARY: |
| 2100 | case RE_ZERO_VAR: |
| 2101 | /* |
| 2102 | * none of these transitions consumes input, so any of these |
| 2103 | * could result in an infinite loop - continue down the |
| 2104 | * current path |
| 2105 | */ |
| 2106 | cur_state = tuple->next_state_1; |
| 2107 | break; |
| 2108 | |
| 2109 | case RE_ASSERT_POS: |
| 2110 | case RE_ASSERT_NEG: |
| 2111 | case RE_ASSERT_BACKPOS: |
| 2112 | case RE_ASSERT_BACKNEG: |
| 2113 | /* |
| 2114 | * Assertions have sub-machines that are independent of the |
| 2115 | * main machine - there are no state transitions between the |
| 2116 | * main machine and a sub-machine. Break loops in the |
| 2117 | * sub-machine recursively. Note that this is a whole new loop |
| 2118 | * break test - we don't share the stack with the enclosing |
| 2119 | * machine. |
| 2120 | */ |
| 2121 | if (break_loops(tuple->info.sub.init, tuple->info.sub.final, 0)) |
| 2122 | { |
| 2123 | /* |
| 2124 | * The whole sub-machine was a loop, which makes it an |
| 2125 | * automatic zero-length match - jump immediately to its |
| 2126 | * final state on evaluation. |
| 2127 | */ |
| 2128 | tuple->info.sub.init = tuple->info.sub.final; |
| 2129 | } |
| 2130 | |
| 2131 | /* assertions don't consume input, so continue on this path */ |
| 2132 | cur_state = tuple->next_state_1; |
| 2133 | break; |
| 2134 | |
| 2135 | default: |
| 2136 | /* |
| 2137 | * all other states consume input, so this branch definitely |
| 2138 | * can't loop back to the original state without consuming |
| 2139 | * input - we do not need to proceed any further down the |
| 2140 | * current branch, since it's not an infinite epsilon loop |
| 2141 | * even if it does ultimately find its way back to the |
| 2142 | * initial state |
| 2143 | */ |
| 2144 | return FALSE; |
| 2145 | } |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | /* |
| 2150 | * Optimization: remove meaningless branch-to-branch transitions. An |
| 2151 | * "epsilon" state that has only one outgoing transition does nothing |
| 2152 | * except move to the next state, so any transition that points to such a |
| 2153 | * state could just as well point to the destination of the epsilon's one |
| 2154 | * outgoing transition. |
| 2155 | */ |
| 2156 | void CRegexParser::remove_branch_to_branch(re_machine *machine) |
| 2157 | { |
| 2158 | /* make sure the initial state points to the first meaningful state */ |
| 2159 | optimize_transition(machine, &machine->init); |
| 2160 | |
| 2161 | /* scan all active states */ |
| 2162 | re_state_id cur; |
| 2163 | re_tuple *tuple; |
| 2164 | for (cur = 0, tuple = tuple_arr_ ; cur < next_state_ ; ++cur, ++tuple) |
| 2165 | { |
| 2166 | /* if this isn't the final state, check it */ |
| 2167 | if (cur != machine->final) |
| 2168 | { |
| 2169 | /* check both of our outgoing transitions */ |
| 2170 | optimize_transition(machine, &tuple->next_state_1); |
| 2171 | optimize_transition(machine, &tuple->next_state_2); |
| 2172 | } |
| 2173 | } |
| 2174 | } |
| 2175 | |
| 2176 | /* |
| 2177 | * Optimize a single transition to remove meaningless branch-to-branch |
| 2178 | * transitions. |
| 2179 | */ |
| 2180 | void CRegexParser::optimize_transition(const re_machine *machine, |
| 2181 | re_state_id *trans) |
| 2182 | { |
| 2183 | /* keep going as long as we find meaningless forwarding branches */ |
| 2184 | for (;;) |
| 2185 | { |
| 2186 | re_tuple *tuple_nxt; |
| 2187 | |
| 2188 | /* |
| 2189 | * if this transition points to the machine's final state, there's |
| 2190 | * nowhere else to go from here |
| 2191 | */ |
| 2192 | if (*trans == RE_STATE_INVALID || *trans == machine->final) |
| 2193 | return; |
| 2194 | |
| 2195 | /* get the state the '*trans' is currently pointing to */ |
| 2196 | tuple_nxt = &tuple_arr_[*trans]; |
| 2197 | |
| 2198 | /* |
| 2199 | * if the transition points to anything other than a single-branch |
| 2200 | * epsilon, we point to a meaningful next state, so there's no |
| 2201 | * further branch-to-branch elimination we can perform |
| 2202 | */ |
| 2203 | if (tuple_nxt->typ != RE_EPSILON |
| 2204 | || tuple_nxt->next_state_2 != RE_STATE_INVALID) |
| 2205 | return; |
| 2206 | |
| 2207 | /* |
| 2208 | * This transition points to a meaningless intermediate state, so |
| 2209 | * we can simply skip the intermediate state and go directly from |
| 2210 | * the current state to the target state's single target. Once |
| 2211 | * we've done this, continue scanning, because we might find that |
| 2212 | * the new target state itself is a meaningless intermediate state |
| 2213 | * that we can skip past as well (and so on, and so on - keep |
| 2214 | * going until we find a real target state). |
| 2215 | */ |
| 2216 | *trans = tuple_nxt->next_state_1; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | /* |
| 2221 | * Sub-machine match length arithmetic. This is just like ordinary |
| 2222 | * arithmetic, but with the added twist of infinities (-1 represents |
| 2223 | * infinity in our scheme). |
| 2224 | */ |
| 2225 | inline static int gml_add(int a, int b) |
| 2226 | { |
| 2227 | /* infinity + x == infinity for all x; otherwise return the normal sum */ |
| 2228 | return (a < 0 || b < 0 ? -1 : a + b); |
| 2229 | } |
| 2230 | inline static void gml_add(int *a, int b) |
| 2231 | { |
| 2232 | *a = gml_add(*a, b); |
| 2233 | } |
| 2234 | inline static int gml_mul(int a, int b) |
| 2235 | { |
| 2236 | /* infinity * x == infinity for all x */ |
| 2237 | return (a < 0 || b < 0 ? -1 : a * b); |
| 2238 | } |
| 2239 | inline static int gml_min(int a, int b) |
| 2240 | { |
| 2241 | /* x < infinity for all x */ |
| 2242 | return (a < 0 ? b : b < 0 || a < b ? a : b); |
| 2243 | } |
| 2244 | inline static int gml_max(int a, int b) |
| 2245 | { |
| 2246 | /* infinity > x for all x */ |
| 2247 | return (a < 0 || b < 0 ? -1 : a > b ? a : b); |
| 2248 | } |
| 2249 | |
| 2250 | /* calculate the match length of a submachine */ |
| 2251 | void CRegexParser::get_match_length(re_state_id init, re_state_id final, |
| 2252 | int *minlen, int *maxlen, |
| 2253 | regex_scan_frame *stack) |
| 2254 | { |
| 2255 | /* set up a stack frame in case we need to recurse */ |
| 2256 | regex_scan_frame fr(stack); |
| 2257 | |
| 2258 | /* start with zero lengths */ |
| 2259 | *minlen = *maxlen = 0; |
| 2260 | |
| 2261 | /* iterate through the machine */ |
| 2262 | for (re_state_id i = init ; i != final ; ) |
| 2263 | { |
| 2264 | /* remember the current state in our stack frame */ |
| 2265 | fr.st = i; |
| 2266 | |
| 2267 | /* get this tuple */ |
| 2268 | re_tuple *t = &tuple_arr_[i]; |
| 2269 | |
| 2270 | /* mark this state as visited, to detect infinite loops */ |
| 2271 | t->flags |= RE_STATE_CYCLE_TEST; |
| 2272 | |
| 2273 | /* check what we have */ |
| 2274 | switch (t->typ) |
| 2275 | { |
| 2276 | case RE_TEXT_BEGIN: |
| 2277 | case RE_TEXT_END: |
| 2278 | case RE_LOOKBACK_POS: |
| 2279 | case RE_WORD_BEGIN: |
| 2280 | case RE_WORD_END: |
| 2281 | case RE_WORD_BOUNDARY: |
| 2282 | case RE_NON_WORD_BOUNDARY: |
| 2283 | case RE_GROUP_ENTER: |
| 2284 | case RE_GROUP_EXIT: |
| 2285 | case RE_ASSERT_POS: |
| 2286 | case RE_ASSERT_NEG: |
| 2287 | case RE_ASSERT_BACKPOS: |
| 2288 | case RE_ASSERT_BACKNEG: |
| 2289 | case RE_ZERO_VAR: |
| 2290 | /* |
| 2291 | * assertions, group entries, and loop entries contribute |
| 2292 | * nothing to the match length - continue to the onward branch |
| 2293 | */ |
| 2294 | i = t->next_state_1; |
| 2295 | break; |
| 2296 | |
| 2297 | case RE_EPSILON: |
| 2298 | /* |
| 2299 | * for single-transition epsilons, simply proceed to the next |
| 2300 | * state |
| 2301 | */ |
| 2302 | if (t->next_state_2 == RE_STATE_INVALID) |
| 2303 | { |
| 2304 | i = t->next_state_1; |
| 2305 | break; |
| 2306 | } |
| 2307 | |
| 2308 | /* |
| 2309 | * Epsilon transitions have two lengths: the length of the |
| 2310 | * first branch, and the length of the second branch. |
| 2311 | * Calculate the two branches separately, and take the lower of |
| 2312 | * the minimum values and the higher of the maximum values. |
| 2313 | * |
| 2314 | * Before recursing, check to see if we're looping. If we've |
| 2315 | * recursed from the current state already in this search, |
| 2316 | * we're in a loop, so we have a maximum length of infinity. |
| 2317 | */ |
| 2318 | if (fr.is_recursing()) |
| 2319 | { |
| 2320 | /* we're looping, so we have an unbounded maximum length */ |
| 2321 | *maxlen = -1; |
| 2322 | } |
| 2323 | else |
| 2324 | { |
| 2325 | /* |
| 2326 | * we're not looping yet, so we can recursively calculate |
| 2327 | * the length of each sub-branch |
| 2328 | */ |
| 2329 | int min1, min2, max1, max2; |
| 2330 | get_match_length(t->next_state_1, final, &min1, &max1, &fr); |
| 2331 | get_match_length(t->next_state_2, final, &min2, &max2, &fr); |
| 2332 | |
| 2333 | /* add the lower min and the higher max to the total */ |
| 2334 | gml_add(minlen, gml_min(min1, min2)); |
| 2335 | gml_add(maxlen, gml_max(max1, max2)); |
| 2336 | } |
| 2337 | |
| 2338 | /* we've fully considered both branches, so we're done */ |
| 2339 | return; |
| 2340 | |
| 2341 | case RE_LOOP_BRANCH: |
| 2342 | /* |
| 2343 | * A loop branch contributes (min..max) * the length of the |
| 2344 | * "enter" branch (the first epsilon transition). Check that |
| 2345 | * we're not recursing first; if so, we have an unbounded |
| 2346 | * maximum length. |
| 2347 | */ |
| 2348 | if (fr.is_recursing()) |
| 2349 | { |
| 2350 | /* looping - the maximum is unbounded */ |
| 2351 | *maxlen = -1; |
| 2352 | } |
| 2353 | else |
| 2354 | { |
| 2355 | /* |
| 2356 | * Calculate the length of the loop branch. Note that the |
| 2357 | * loop ends at the current state. |
| 2358 | */ |
| 2359 | int loopmin, loopmax; |
| 2360 | get_match_length(t->next_state_1, i, &loopmin, &loopmax, &fr); |
| 2361 | |
| 2362 | /* add the loop min and max to the running total */ |
| 2363 | gml_add(minlen, gml_mul(loopmin, t->info.loop.loop_min)); |
| 2364 | gml_add(maxlen, gml_mul(loopmax, t->info.loop.loop_max)); |
| 2365 | } |
| 2366 | |
| 2367 | /* continue from the post-loop state */ |
| 2368 | i = t->next_state_2; |
| 2369 | break; |
| 2370 | |
| 2371 | case RE_GROUP_MATCH: |
| 2372 | /* |
| 2373 | * A group matcher matches the same length as its corresponding |
| 2374 | * group. Find the group entry state, then measure its length |
| 2375 | * and use it as the result. |
| 2376 | * |
| 2377 | * As usual, before recursively calculating the group length, |
| 2378 | * we need to make sure we're not recursing from the current |
| 2379 | * state. |
| 2380 | */ |
| 2381 | if (fr.is_recursing()) |
| 2382 | { |
| 2383 | /* looping, so the length of this branch is unbounded */ |
| 2384 | *maxlen = -1; |
| 2385 | } |
| 2386 | else |
| 2387 | { |
| 2388 | /* locate the enter/exit states for the group */ |
| 2389 | re_state_id g, genter, gexit; |
| 2390 | re_tuple *gt; |
| 2391 | for (g = 0, gt = tuple_arr_, genter = gexit = -1 ; |
| 2392 | g < next_state_ ; ++g, ++gt) |
| 2393 | { |
| 2394 | if (gt->typ == RE_GROUP_ENTER |
| 2395 | && gt->info.ch == t->info.ch) |
| 2396 | genter = g; |
| 2397 | else if (gt->typ == RE_GROUP_EXIT |
| 2398 | && gt->info.ch == t->info.ch) |
| 2399 | gexit = g; |
| 2400 | } |
| 2401 | |
| 2402 | /* if we found the group, figure its length */ |
| 2403 | if (genter >= 0 && gexit >= 0) |
| 2404 | { |
| 2405 | /* figure the group length range */ |
| 2406 | int gmin, gmax; |
| 2407 | get_match_length(genter, gexit, &gmin, &gmax, &fr); |
| 2408 | |
| 2409 | /* add the group min and max to the running totals */ |
| 2410 | gml_add(minlen, gmin); |
| 2411 | gml_add(maxlen, gmax); |
| 2412 | } |
| 2413 | } |
| 2414 | |
| 2415 | /* move on to the next state */ |
| 2416 | i = t->next_state_1; |
| 2417 | break; |
| 2418 | |
| 2419 | default: |
| 2420 | /* everything else matches a single character */ |
| 2421 | gml_add(minlen, 1); |
| 2422 | gml_add(maxlen, 1); |
| 2423 | |
| 2424 | /* on to the next state */ |
| 2425 | i = t->next_state_1; |
| 2426 | break; |
| 2427 | } |
| 2428 | } |
| 2429 | } |
| 2430 | |
| 2431 | /* |
| 2432 | * Consolidate consecutive character transitions into strings |
| 2433 | */ |
| 2434 | void CRegexParser::consolidate_strings(re_machine *machine) |
| 2435 | { |
| 2436 | /* run through the tuples looking for runs of characters */ |
| 2437 | re_state_id i; |
| 2438 | re_tuple *t; |
| 2439 | for (i = 0, t = tuple_arr_ ; i < next_state_ ; ++i, ++t) |
| 2440 | { |
| 2441 | /* if this is a literal, try consolidating it */ |
| 2442 | if (t->typ == RE_LITERAL) |
| 2443 | { |
| 2444 | /* count up the literals that follow */ |
| 2445 | int cnt = 1; |
| 2446 | for (re_state_id j = t->next_state_1 ; |
| 2447 | j != RE_STATE_INVALID && tuple_arr_[j].typ == RE_LITERAL ; |
| 2448 | j = tuple_arr_[j].next_state_1, ++cnt) ; |
| 2449 | |
| 2450 | /* if we found at least one more literal, consolidate it */ |
| 2451 | if (cnt > 1) |
| 2452 | { |
| 2453 | /* convert this node to a string node */ |
| 2454 | wchar_t ch = t->info.ch; |
| 2455 | t->typ = RE_LITSTR; |
| 2456 | t->info.str.str = new wchar_t[cnt+1]; |
| 2457 | t->info.str.str[0] = ch; |
| 2458 | t->info.str.str[cnt] = 0; |
| 2459 | |
| 2460 | /* copy the string characters */ |
| 2461 | int k = 1; |
| 2462 | for (re_tuple *tj = &tuple_arr_[t->next_state_1] ; |
| 2463 | k < cnt ; tj = &tuple_arr_[tj->next_state_1], ++k) |
| 2464 | { |
| 2465 | /* add this character to the string */ |
| 2466 | t->info.str.str[k] = tj->info.ch; |
| 2467 | |
| 2468 | /* skip the state in the starting state */ |
| 2469 | t->next_state_1 = tj->next_state_1; |
| 2470 | |
| 2471 | /* make this state an alias of the original */ |
| 2472 | tj->typ = RE_LITSTRA; |
| 2473 | tj->info.str.str = &t->info.str.str[k]; |
| 2474 | tj->info.str.src = i; |
| 2475 | } |
| 2476 | } |
| 2477 | } |
| 2478 | } |
| 2479 | } |
| 2480 | |
| 2481 | /* ------------------------------------------------------------------------ */ |
| 2482 | /* |
| 2483 | * Compile an expression and return a newly-allocated pattern object. |
| 2484 | */ |
| 2485 | re_status_t CRegexParser::compile_pattern(const char *expr_str, |
| 2486 | size_t exprlen, |
| 2487 | re_compiled_pattern **pattern) |
| 2488 | { |
| 2489 | /* presume we won't allocate a new pattern object */ |
| 2490 | *pattern = 0; |
| 2491 | |
| 2492 | /* compile the pattern */ |
| 2493 | re_compiled_pattern_base base_pat; |
| 2494 | re_status_t stat = compile(expr_str, exprlen, &base_pat); |
| 2495 | if (stat != RE_STATUS_SUCCESS) |
| 2496 | return stat; |
| 2497 | |
| 2498 | /* |
| 2499 | * Start off with our basic space needs: we need space for the base |
| 2500 | * structure, plus space for re_tuple array (actually, space for the |
| 2501 | * number of allocated tuples minus one, since there's one built into |
| 2502 | * the base structure). |
| 2503 | */ |
| 2504 | re_compiled_pattern *pat = 0; |
| 2505 | size_t siz = sizeof(re_compiled_pattern) |
| 2506 | + (base_pat.tuple_cnt - 1)*sizeof(pat->tuples[0]); |
| 2507 | |
| 2508 | /* |
| 2509 | * Run through the tuples in the result machine and add up the amount |
| 2510 | * of extra space we'll need for extra allocations (specifically, |
| 2511 | * character ranges). |
| 2512 | */ |
| 2513 | re_state_id i; |
| 2514 | re_tuple *t; |
| 2515 | for (i = 0, t = tuple_arr_ ; i < base_pat.tuple_cnt ; ++i, ++t) |
| 2516 | { |
| 2517 | /* check what kind of tuple this is */ |
| 2518 | switch(t->typ) |
| 2519 | { |
| 2520 | case RE_RANGE: |
| 2521 | case RE_RANGE_EXCL: |
| 2522 | /* range - add in space for the character range data */ |
| 2523 | siz += sizeof(t->info.range.char_range[0]) |
| 2524 | * t->info.range.char_range_cnt; |
| 2525 | break; |
| 2526 | |
| 2527 | case RE_LITSTR: |
| 2528 | /* literal string - add in space for the string data */ |
| 2529 | siz += (wcslen(t->info.str.str)+1) * sizeof(t->info.str.str); |
| 2530 | break; |
| 2531 | |
| 2532 | default: |
| 2533 | /* other types require no extra storage */ |
| 2534 | break; |
| 2535 | } |
| 2536 | } |
| 2537 | |
| 2538 | /* allocate space for the structure */ |
| 2539 | *pattern = pat = (re_compiled_pattern *)t3malloc(siz); |
| 2540 | |
| 2541 | /* copy the base pattern to the result */ |
| 2542 | memcpy(pat, &base_pat, sizeof(base_pat)); |
| 2543 | |
| 2544 | /* copy the tuple array */ |
| 2545 | memcpy(pat->tuples, tuple_arr_, pat->tuple_cnt * sizeof(pat->tuples[0])); |
| 2546 | |
| 2547 | /* |
| 2548 | * Pack the range data onto the end of the tuple array in the data |
| 2549 | * structure. We already calculated how much space we'll need for this |
| 2550 | * and included the space in the structure's allocation, so we simply |
| 2551 | * need to find the location of the range data at the end of our tuple |
| 2552 | * array. |
| 2553 | */ |
| 2554 | wchar_t *rp = (wchar_t *)&pat->tuples[pat->tuple_cnt]; |
| 2555 | |
| 2556 | /* |
| 2557 | * run through the new tuple array and pack the range data into the new |
| 2558 | * allocation unit |
| 2559 | */ |
| 2560 | for (i = 0, t = pat->tuples ; i < next_state_ ; ++i, ++t) |
| 2561 | { |
| 2562 | /* check what kind of tuple this is */ |
| 2563 | switch(t->typ) |
| 2564 | { |
| 2565 | case RE_RANGE: |
| 2566 | case RE_RANGE_EXCL: |
| 2567 | /* |
| 2568 | * Character range. Copy the original character range data |
| 2569 | * into the new allocation unit, at the next available location |
| 2570 | * in the allocation unit. |
| 2571 | */ |
| 2572 | memcpy(rp, t->info.range.char_range, |
| 2573 | t->info.range.char_range_cnt |
| 2574 | * sizeof(t->info.range.char_range[0])); |
| 2575 | |
| 2576 | /* fix up the tuple to point to the packed copy */ |
| 2577 | t->info.range.char_range = rp; |
| 2578 | |
| 2579 | /* move past the space in our allocation unit */ |
| 2580 | rp += t->info.range.char_range_cnt; |
| 2581 | break; |
| 2582 | |
| 2583 | case RE_LITSTR: |
| 2584 | /* literal string - copy it into the allocation unit */ |
| 2585 | wcscpy(rp, t->info.str.str); |
| 2586 | |
| 2587 | /* fix up the tuple to point to the packed copy */ |
| 2588 | t->info.str.str = (wchar_t *)rp; |
| 2589 | |
| 2590 | /* move past the space in our allocation unit */ |
| 2591 | rp += wcslen(rp) + 1; |
| 2592 | break; |
| 2593 | |
| 2594 | case RE_LITSTRA: |
| 2595 | /* literal string alias */ |
| 2596 | { |
| 2597 | /* get the original base string from the source state */ |
| 2598 | re_state_id src = t->info.str.src; |
| 2599 | wchar_t *str = tuple_arr_[src].info.str.str; |
| 2600 | |
| 2601 | /* get the offset */ |
| 2602 | int ofs = t->info.str.str - str; |
| 2603 | |
| 2604 | /* point to the the copied string the new state copy */ |
| 2605 | t->info.str.str = pat->tuples[src].info.str.str + ofs; |
| 2606 | } |
| 2607 | break; |
| 2608 | |
| 2609 | default: |
| 2610 | /* other types contain no range data */ |
| 2611 | break; |
| 2612 | } |
| 2613 | } |
| 2614 | |
| 2615 | /* success */ |
| 2616 | return stat; |
| 2617 | } |
| 2618 | |
| 2619 | /* |
| 2620 | * free a pattern previously created with compile_pattern() |
| 2621 | */ |
| 2622 | void CRegexParser::free_pattern(re_compiled_pattern *pattern) |
| 2623 | { |
| 2624 | /* we allocate each pattern as a single unit, so it's easy to free */ |
| 2625 | t3free(pattern); |
| 2626 | } |
| 2627 | |
| 2628 | /* ------------------------------------------------------------------------ */ |
| 2629 | /* |
| 2630 | * Register delta list. |
| 2631 | * |
| 2632 | * When we discard a stack frame, we need to propagate its register deltas |
| 2633 | * to the parent frame. Each frame only stores the deltas from its initial |
| 2634 | * state, so whenever we decide to discard a frame and keep that branch, we |
| 2635 | * have to add its deltas to its parent frame. |
| 2636 | * |
| 2637 | * This class is a little tricky. It points directly into the CRegexStack |
| 2638 | * object's memory, so it's only valid as long as you don't push another |
| 2639 | * frame. For our immediate purposes this is fine, since we only need to |
| 2640 | * preserve an old frame long enough to propagate its changes into its |
| 2641 | * parent. But be aware if you're repurposing this class that an instance |
| 2642 | * lifetime is limited by external factors (i.e., pushing a new stack |
| 2643 | * frame). (Also note that we're NOT talking about the native machine |
| 2644 | * stack - there are no native interrupt reentrancy issues to worry about, |
| 2645 | * for example.) |
| 2646 | */ |
| 2647 | class reg_deltas |
| 2648 | { |
| 2649 | public: |
| 2650 | reg_deltas(CRegexStack &s) |
| 2651 | { |
| 2652 | /* record the current top frame information */ |
| 2653 | regex_stack_entry *fp = (regex_stack_entry *)(s.buf_ + s.sp_); |
| 2654 | varp = (regex_stack_var *)(fp + 1); |
| 2655 | varend = (regex_stack_var *)(s.buf_ + s.used_); |
| 2656 | } |
| 2657 | |
| 2658 | /* |
| 2659 | * Propagate the initial values from our stack frame to the current top |
| 2660 | * of stack. This pushes the saved initial values from this frame into |
| 2661 | * the current frame. |
| 2662 | */ |
| 2663 | void propagate(CRegexStack &s) |
| 2664 | { |
| 2665 | for (regex_stack_var *v = varp ; v < varend ; ++v) |
| 2666 | s.propagate(v); |
| 2667 | } |
| 2668 | |
| 2669 | protected: |
| 2670 | /* variable list range */ |
| 2671 | regex_stack_var *varp, *varend; |
| 2672 | }; |
| 2673 | |
| 2674 | |
| 2675 | |
| 2676 | /* ------------------------------------------------------------------------ */ |
| 2677 | /* |
| 2678 | * Pattern recognizer |
| 2679 | */ |
| 2680 | |
| 2681 | /* |
| 2682 | * construct |
| 2683 | */ |
| 2684 | CRegexSearcher::CRegexSearcher() |
| 2685 | { |
| 2686 | /* by default, use case-sensitive searches if not otherwise specified */ |
| 2687 | default_case_sensitive_ = TRUE; |
| 2688 | } |
| 2689 | |
| 2690 | /* |
| 2691 | * delete |
| 2692 | */ |
| 2693 | CRegexSearcher::~CRegexSearcher() |
| 2694 | { |
| 2695 | } |
| 2696 | |
| 2697 | /* |
| 2698 | * Match a string to a compiled expression. Returns the length of the |
| 2699 | * match if successful, or -1 if no match was found. |
| 2700 | */ |
| 2701 | int CRegexSearcher::match(const char *entire_str, size_t entire_str_len, |
| 2702 | const char *str, const size_t origlen, |
| 2703 | const re_compiled_pattern_base *pattern, |
| 2704 | const re_tuple *tuple_arr, |
| 2705 | const re_machine *machine, |
| 2706 | re_group_register *regs, |
| 2707 | short *loop_vars) |
| 2708 | { |
| 2709 | /* |
| 2710 | * if the case sensitivity was specified, it overrides the current |
| 2711 | * search defaults; otherwise apply the search defaults |
| 2712 | */ |
| 2713 | int case_sensitive = (pattern->case_sensitivity_specified |
| 2714 | ? pattern->case_sensitive |
| 2715 | : default_case_sensitive_); |
| 2716 | |
| 2717 | /* macro to perform a "local return" */ |
| 2718 | int _retval_; |
| 2719 | #define local_return(retval) \ |
| 2720 | _retval_ = (retval); \ |
| 2721 | goto do_local_return; |
| 2722 | |
| 2723 | /* reset the stack */ |
| 2724 | stack_.reset(); |
| 2725 | |
| 2726 | /* start at the machine's initial and final states */ |
| 2727 | re_state_id cur_state = machine->init; |
| 2728 | re_state_id final_state = machine->final; |
| 2729 | |
| 2730 | /* |
| 2731 | * if we're starting in the final state, this is a zero-length |
| 2732 | * pattern, which matches anything - immediately return success with a |
| 2733 | * zero-length match |
| 2734 | */ |
| 2735 | if (cur_state == final_state) |
| 2736 | return 0; |
| 2737 | |
| 2738 | /* start at the beginning of the string */ |
| 2739 | utf8_ptr p((char *)str); |
| 2740 | size_t curlen = origlen; |
| 2741 | |
| 2742 | /* note the offset from the start of the entire string */ |
| 2743 | int start_ofs = str - entire_str; |
| 2744 | |
| 2745 | /* note where the overall string ends */ |
| 2746 | const char *entire_str_end = entire_str + entire_str_len; |
| 2747 | |
| 2748 | /* run the machine */ |
| 2749 | for (;;) |
| 2750 | { |
| 2751 | /* get the tuple for this state */ |
| 2752 | const re_tuple *tuple = &tuple_arr[cur_state]; |
| 2753 | |
| 2754 | /* check the type of state we're processing */ |
| 2755 | switch(tuple->typ) |
| 2756 | { |
| 2757 | case RE_ZERO_VAR: |
| 2758 | /* save the variable in the stack frame if necessary */ |
| 2759 | stack_.save_loop_var(tuple->info.loop.loop_var, loop_vars); |
| 2760 | |
| 2761 | /* zero the loop variable and proceed */ |
| 2762 | loop_vars[tuple->info.loop.loop_var] = 0; |
| 2763 | break; |
| 2764 | |
| 2765 | case RE_LOOP_BRANCH: |
| 2766 | /* |
| 2767 | * This is a loop branch. Check the variable to see if we've |
| 2768 | * satisfied the loop condition yet: |
| 2769 | * |
| 2770 | * - If we haven't yet reached the minimum loop count, simply |
| 2771 | * transition into the loop (i.e., take the 'enter' branch |
| 2772 | * unconditionally). |
| 2773 | * |
| 2774 | * - If we have reached the maximum loop count (if there is |
| 2775 | * one - if max is -1 then there's no upper bound), simply |
| 2776 | * transition past the loop (i.e., take the 'bypass' branch |
| 2777 | * unconditionally). |
| 2778 | * |
| 2779 | * - Otherwise, we must treat this just like an ordinary |
| 2780 | * two-way epsilon branch. |
| 2781 | * |
| 2782 | * Note that, if we have a 'shortest' modifier, the bypass |
| 2783 | * branch will be first, to encourage the indeterminate check |
| 2784 | * to choose the short branch whenever possible; otherwise the |
| 2785 | * enter branch will be first, so we take the long branch |
| 2786 | * whenever possible. |
| 2787 | */ |
| 2788 | { |
| 2789 | short *varptr; |
| 2790 | |
| 2791 | /* get the variable value */ |
| 2792 | varptr = &loop_vars[tuple->info.loop.loop_var]; |
| 2793 | |
| 2794 | /* save the variable in the stack frame if necessary */ |
| 2795 | stack_.save_loop_var(tuple->info.loop.loop_var, loop_vars); |
| 2796 | |
| 2797 | /* |
| 2798 | * if we're not at the loop minimum yet, transition into |
| 2799 | * the loop body |
| 2800 | */ |
| 2801 | if (*varptr < tuple->info.loop.loop_min) |
| 2802 | { |
| 2803 | /* increment the loop counter */ |
| 2804 | ++(*varptr); |
| 2805 | |
| 2806 | /* unconditionally transfer into the loop */ |
| 2807 | if ((tuple->flags & RE_STATE_SHORTEST) == 0) |
| 2808 | cur_state = tuple->next_state_1; |
| 2809 | else |
| 2810 | cur_state = tuple->next_state_2; |
| 2811 | |
| 2812 | /* we're done processing this state */ |
| 2813 | goto got_next_state; |
| 2814 | } |
| 2815 | |
| 2816 | /* |
| 2817 | * if we've reached the loop maximum (if there is one), |
| 2818 | * transition past the loop |
| 2819 | */ |
| 2820 | if (tuple->info.loop.loop_max >= 0 |
| 2821 | && *varptr >= tuple->info.loop.loop_max) |
| 2822 | { |
| 2823 | /* unconditionally skip the loop */ |
| 2824 | if ((tuple->flags & RE_STATE_SHORTEST) == 0) |
| 2825 | cur_state = tuple->next_state_2; |
| 2826 | else |
| 2827 | cur_state = tuple->next_state_1; |
| 2828 | |
| 2829 | /* we're done with this state */ |
| 2830 | goto got_next_state; |
| 2831 | } |
| 2832 | |
| 2833 | /* |
| 2834 | * We don't know which way to go, so we must treat this as |
| 2835 | * a two-way epsilon branch. Count this as another loop |
| 2836 | * iteration, since the branch that enters the loop will |
| 2837 | * come back here for another try. The branch that skips |
| 2838 | * the loop doesn't care about the loop counter any more, |
| 2839 | * so we can just increment it and ignore the skip branch. |
| 2840 | */ |
| 2841 | ++(*varptr); |
| 2842 | goto two_way_epsilon; |
| 2843 | } |
| 2844 | /* not reached */ |
| 2845 | |
| 2846 | case RE_GROUP_MATCH: |
| 2847 | { |
| 2848 | int group_num; |
| 2849 | re_group_register *group_reg; |
| 2850 | size_t reg_len; |
| 2851 | |
| 2852 | /* it's a group - get the group number */ |
| 2853 | group_num = (int)tuple->info.ch; |
| 2854 | group_reg = ®s[group_num]; |
| 2855 | |
| 2856 | /* |
| 2857 | * if this register isn't defined, there's nothing to |
| 2858 | * match, so fail |
| 2859 | */ |
| 2860 | if (group_reg->start_ofs == -1 |
| 2861 | || group_reg->end_ofs == -1) |
| 2862 | { |
| 2863 | local_return(-1); |
| 2864 | } |
| 2865 | |
| 2866 | /* calculate the length of the register value */ |
| 2867 | reg_len = group_reg->end_ofs - group_reg->start_ofs; |
| 2868 | |
| 2869 | /* if we don't have enough left to match, it fails */ |
| 2870 | if (curlen < reg_len) |
| 2871 | { |
| 2872 | local_return(-1); |
| 2873 | } |
| 2874 | |
| 2875 | /* if the string doesn't match exactly, we fail */ |
| 2876 | if (memcmp(p.getptr(), entire_str + group_reg->start_ofs, |
| 2877 | reg_len) != 0) |
| 2878 | { |
| 2879 | local_return(-1); |
| 2880 | } |
| 2881 | |
| 2882 | /* |
| 2883 | * It matches exactly - skip the entire byte length of the |
| 2884 | * register in the source string |
| 2885 | */ |
| 2886 | p.set(p.getptr() + reg_len); |
| 2887 | curlen -= reg_len; |
| 2888 | } |
| 2889 | break; |
| 2890 | |
| 2891 | case RE_TEXT_BEGIN: |
| 2892 | /* |
| 2893 | * Match only the exact beginning of the string - if we're |
| 2894 | * anywhere else, this isn't a match. If this succeeds, we |
| 2895 | * don't skip any characters. |
| 2896 | */ |
| 2897 | if (p.getptr() != entire_str) |
| 2898 | { |
| 2899 | local_return(-1); |
| 2900 | } |
| 2901 | break; |
| 2902 | |
| 2903 | case RE_TEXT_END: |
| 2904 | /* |
| 2905 | * Match only the exact end of the string - if we're anywhere |
| 2906 | * else, this isn't a match. Don't skip any characters on |
| 2907 | * success. |
| 2908 | */ |
| 2909 | if (p.getptr() != entire_str_end) |
| 2910 | { |
| 2911 | local_return(-1); |
| 2912 | } |
| 2913 | break; |
| 2914 | |
| 2915 | case RE_LOOKBACK_POS: |
| 2916 | /* |
| 2917 | * Match the position of the parent lookback assertion. If |
| 2918 | * we're anywhere else, this isn't a match. This ensures that |
| 2919 | * we only match lookback assertions that end up at the match |
| 2920 | * position of the assertion. |
| 2921 | */ |
| 2922 | if (p.getptr() - entire_str != stack_.get_lookback_pos()) |
| 2923 | { |
| 2924 | local_return(-1); |
| 2925 | } |
| 2926 | break; |
| 2927 | |
| 2928 | case RE_WORD_BEGIN: |
| 2929 | /* |
| 2930 | * If the previous character is a word character, we're not at |
| 2931 | * the beginning of a word. If we're at the beginning of the |
| 2932 | * entire string, we need not check anything previous - |
| 2933 | * there's no previous character, so we can't have a preceding |
| 2934 | * word character. |
| 2935 | */ |
| 2936 | if (p.getptr() != entire_str |
| 2937 | && is_word_char(p.getch_before(1))) |
| 2938 | { |
| 2939 | local_return(-1); |
| 2940 | } |
| 2941 | |
| 2942 | /* |
| 2943 | * if we're at the end of the string, or the current character |
| 2944 | * isn't a word character, we're not at the beginning of a |
| 2945 | * word |
| 2946 | */ |
| 2947 | if (p.getptr() == entire_str_end |
| 2948 | || !is_word_char(p.getch())) |
| 2949 | { |
| 2950 | local_return(-1); |
| 2951 | } |
| 2952 | break; |
| 2953 | |
| 2954 | case RE_WORD_END: |
| 2955 | /* |
| 2956 | * if the current character is a word character, we're not at |
| 2957 | * the end of a word |
| 2958 | */ |
| 2959 | if (p.getptr() != entire_str_end |
| 2960 | && is_word_char(p.getch())) |
| 2961 | { |
| 2962 | local_return(-1); |
| 2963 | } |
| 2964 | |
| 2965 | /* |
| 2966 | * if we're at the beginning of the string, or the previous |
| 2967 | * character is not a word character, we're not at the end of |
| 2968 | * a word |
| 2969 | */ |
| 2970 | if (p.getptr() == entire_str |
| 2971 | || !is_word_char(p.getch_before(1))) |
| 2972 | { |
| 2973 | local_return(-1); |
| 2974 | } |
| 2975 | break; |
| 2976 | |
| 2977 | case RE_WORD_CHAR: |
| 2978 | /* if it's not a word character, it's a failure */ |
| 2979 | if (curlen == 0 || !is_word_char(p.getch())) |
| 2980 | { |
| 2981 | local_return(-1); |
| 2982 | } |
| 2983 | |
| 2984 | /* skip this character of input */ |
| 2985 | p.inc(&curlen); |
| 2986 | break; |
| 2987 | |
| 2988 | case RE_NON_WORD_CHAR: |
| 2989 | /* if it's a word character, it's a failure */ |
| 2990 | if (curlen == 0 || is_word_char(p.getch())) |
| 2991 | { |
| 2992 | local_return(-1); |
| 2993 | } |
| 2994 | |
| 2995 | /* skip the input */ |
| 2996 | p.inc(&curlen); |
| 2997 | break; |
| 2998 | |
| 2999 | case RE_WORD_BOUNDARY: |
| 3000 | case RE_NON_WORD_BOUNDARY: |
| 3001 | { |
| 3002 | /* |
| 3003 | * Determine if the previous character is a word character. |
| 3004 | * If we're at the beginning of the string, it's obviously |
| 3005 | * not, otherwise check the classification of the previous |
| 3006 | * character. |
| 3007 | */ |
| 3008 | int prev_is_word = (p.getptr() != entire_str |
| 3009 | && is_word_char(p.getch_before(1))); |
| 3010 | |
| 3011 | /* make the same check for the current character */ |
| 3012 | int next_is_word = (p.getptr() != entire_str_end |
| 3013 | && is_word_char(p.getch())); |
| 3014 | |
| 3015 | /* |
| 3016 | * Determine if this is a boundary - it is if the two |
| 3017 | * states are different |
| 3018 | */ |
| 3019 | int boundary = ((prev_is_word != 0) ^ (next_is_word != 0)); |
| 3020 | |
| 3021 | /* |
| 3022 | * make sure it matches what was desired, and return |
| 3023 | * failure if not |
| 3024 | */ |
| 3025 | if ((tuple->typ == RE_WORD_BOUNDARY && !boundary) |
| 3026 | || (tuple->typ == RE_NON_WORD_BOUNDARY && boundary)) |
| 3027 | { |
| 3028 | local_return(-1); |
| 3029 | } |
| 3030 | } |
| 3031 | break; |
| 3032 | |
| 3033 | case RE_WILDCARD: |
| 3034 | /* make sure we have a character to match */ |
| 3035 | if (curlen == 0) |
| 3036 | { |
| 3037 | local_return(-1); |
| 3038 | } |
| 3039 | |
| 3040 | /* skip this character */ |
| 3041 | p.inc(&curlen); |
| 3042 | break; |
| 3043 | |
| 3044 | case RE_RANGE: |
| 3045 | case RE_RANGE_EXCL: |
| 3046 | { |
| 3047 | /* make sure we have a character to match */ |
| 3048 | if (curlen == 0) |
| 3049 | { |
| 3050 | local_return(-1); |
| 3051 | } |
| 3052 | |
| 3053 | /* get this character */ |
| 3054 | wchar_t ch = p.getch(); |
| 3055 | |
| 3056 | /* search for the character in the range */ |
| 3057 | size_t i; |
| 3058 | wchar_t *rp; |
| 3059 | for (i = tuple->info.range.char_range_cnt, |
| 3060 | rp = tuple->info.range.char_range ; |
| 3061 | i != 0 ; i -= 2, rp += 2) |
| 3062 | { |
| 3063 | /* |
| 3064 | * check for a class specifier; if it's not a class |
| 3065 | * specifier, treat it as a literal range, and check |
| 3066 | * case sensitivity |
| 3067 | */ |
| 3068 | if (rp[0] == '\0') |
| 3069 | { |
| 3070 | /* |
| 3071 | * The first character of the range pair is null, |
| 3072 | * which means that this isn't a literal range but |
| 3073 | * rather a class. Check for a match to the |
| 3074 | * class. |
| 3075 | */ |
| 3076 | int match; |
| 3077 | switch(rp[1]) |
| 3078 | { |
| 3079 | case RE_ALPHA: |
| 3080 | match = t3_is_alpha(ch); |
| 3081 | break; |
| 3082 | |
| 3083 | case RE_DIGIT: |
| 3084 | match = t3_is_digit(ch); |
| 3085 | break; |
| 3086 | |
| 3087 | case RE_UPPER: |
| 3088 | match = t3_is_upper(ch); |
| 3089 | break; |
| 3090 | |
| 3091 | case RE_LOWER: |
| 3092 | match = t3_is_lower(ch); |
| 3093 | break; |
| 3094 | |
| 3095 | case RE_ALPHANUM: |
| 3096 | match = t3_is_alpha(ch) || t3_is_digit(ch); |
| 3097 | break; |
| 3098 | |
| 3099 | case RE_SPACE: |
| 3100 | match = t3_is_space(ch); |
| 3101 | break; |
| 3102 | |
| 3103 | case RE_VSPACE: |
| 3104 | match = t3_is_vspace(ch); |
| 3105 | break; |
| 3106 | |
| 3107 | case RE_PUNCT: |
| 3108 | match = t3_is_punct(ch); |
| 3109 | break; |
| 3110 | |
| 3111 | case RE_NEWLINE: |
| 3112 | match = (ch == 0x000A |
| 3113 | || ch == 0x000D |
| 3114 | || ch == 0x000B |
| 3115 | || ch == 0x2028 |
| 3116 | || ch == 0x2029); |
| 3117 | break; |
| 3118 | |
| 3119 | case RE_NULLCHAR: |
| 3120 | match = (ch == 0); |
| 3121 | break; |
| 3122 | |
| 3123 | default: |
| 3124 | /* this shouldn't happen */ |
| 3125 | match = FALSE; |
| 3126 | break; |
| 3127 | } |
| 3128 | |
| 3129 | /* |
| 3130 | * if we matched, we can stop looking; otherwise, |
| 3131 | * simply keep going, since there might be another |
| 3132 | * entry that does match |
| 3133 | */ |
| 3134 | if (match) |
| 3135 | break; |
| 3136 | } |
| 3137 | else if (case_sensitive) |
| 3138 | { |
| 3139 | /* |
| 3140 | * the search is case-sensitive - compare the |
| 3141 | * character to the range without case conversion |
| 3142 | */ |
| 3143 | if (ch >= rp[0] && ch <= rp[1]) |
| 3144 | break; |
| 3145 | } |
| 3146 | else if (rp[0] == rp[1]) |
| 3147 | { |
| 3148 | /* |
| 3149 | * single character, case-insensitive - try |
| 3150 | * matching literally, and against the full case |
| 3151 | * folding of this one character |
| 3152 | */ |
| 3153 | size_t matchlen; |
| 3154 | if (ch == rp[0]) |
| 3155 | { |
| 3156 | /* exact match to the literal */ |
| 3157 | break; |
| 3158 | } |
| 3159 | if (t3_compare_case_fold( |
| 3160 | &rp[0], 1, p.getptr(), curlen, &matchlen) == 0) |
| 3161 | { |
| 3162 | /* |
| 3163 | * matched - skip the folded match, then back |
| 3164 | * up one to make up for the common inc() we |
| 3165 | * always do |
| 3166 | */ |
| 3167 | p.inc_bytes(matchlen); |
| 3168 | curlen -= matchlen; |
| 3169 | p.dec(&curlen); |
| 3170 | |
| 3171 | /* stop looking */ |
| 3172 | break; |
| 3173 | } |
| 3174 | } |
| 3175 | else |
| 3176 | { |
| 3177 | /* |
| 3178 | * code point range, case-sensitive - use simple |
| 3179 | * case folding for all three characters |
| 3180 | */ |
| 3181 | wchar_t fch = t3_simple_case_fold(ch); |
| 3182 | if (fch >= t3_simple_case_fold(rp[0]) |
| 3183 | && fch <= t3_simple_case_fold(rp[1])) |
| 3184 | break; |
| 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | /* we matched if we stopped before exhausting the list */ |
| 3189 | int match = (i != 0); |
| 3190 | |
| 3191 | /* make sure we got what we wanted */ |
| 3192 | if ((tuple->typ == RE_RANGE && !match) |
| 3193 | || (tuple->typ == RE_RANGE_EXCL && match)) |
| 3194 | { |
| 3195 | local_return(-1); |
| 3196 | } |
| 3197 | |
| 3198 | /* skip this character of the input */ |
| 3199 | p.inc(&curlen); |
| 3200 | } |
| 3201 | break; |
| 3202 | |
| 3203 | case RE_ALPHA: |
| 3204 | /* check for an alphabetic character */ |
| 3205 | if (curlen == 0 || !t3_is_alpha(p.getch())) |
| 3206 | { |
| 3207 | local_return(-1); |
| 3208 | } |
| 3209 | |
| 3210 | /* skip this character of the input, and continue */ |
| 3211 | p.inc(&curlen); |
| 3212 | break; |
| 3213 | |
| 3214 | case RE_DIGIT: |
| 3215 | /* check for a digit character */ |
| 3216 | if (curlen == 0 || !t3_is_digit(p.getch())) |
| 3217 | { |
| 3218 | local_return(-1); |
| 3219 | } |
| 3220 | |
| 3221 | /* skip this character of the input, and continue */ |
| 3222 | p.inc(&curlen); |
| 3223 | break; |
| 3224 | |
| 3225 | case RE_NON_DIGIT: |
| 3226 | /* check for a non-digit character */ |
| 3227 | if (curlen == 0 || t3_is_digit(p.getch())) |
| 3228 | { |
| 3229 | local_return(-1); |
| 3230 | } |
| 3231 | |
| 3232 | /* skip this character of the input, and continue */ |
| 3233 | p.inc(&curlen); |
| 3234 | break; |
| 3235 | |
| 3236 | case RE_UPPER: |
| 3237 | /* check for an upper-case alphabetic character */ |
| 3238 | if (curlen == 0 || !t3_is_upper(p.getch())) |
| 3239 | { |
| 3240 | local_return(-1); |
| 3241 | } |
| 3242 | |
| 3243 | /* skip this character of the input, and continue */ |
| 3244 | p.inc(&curlen); |
| 3245 | break; |
| 3246 | |
| 3247 | case RE_LOWER: |
| 3248 | /* check for a lower-case alphabetic character */ |
| 3249 | if (curlen == 0 || !t3_is_lower(p.getch())) |
| 3250 | { |
| 3251 | local_return(-1); |
| 3252 | } |
| 3253 | |
| 3254 | /* skip this character of the input, and continue */ |
| 3255 | p.inc(&curlen); |
| 3256 | break; |
| 3257 | |
| 3258 | case RE_ALPHANUM: |
| 3259 | /* check for an alphabetic or digit character */ |
| 3260 | if (curlen == 0 |
| 3261 | || (!t3_is_alpha(p.getch()) && !t3_is_digit(p.getch()))) |
| 3262 | { |
| 3263 | local_return(-1); |
| 3264 | } |
| 3265 | |
| 3266 | /* skip this character of the input, and continue */ |
| 3267 | p.inc(&curlen); |
| 3268 | break; |
| 3269 | |
| 3270 | case RE_SPACE: |
| 3271 | /* check for a space of some kind */ |
| 3272 | if (curlen == 0 || !t3_is_space(p.getch())) |
| 3273 | { |
| 3274 | local_return(-1); |
| 3275 | } |
| 3276 | |
| 3277 | /* skip this character of the input, and continue */ |
| 3278 | p.inc(&curlen); |
| 3279 | break; |
| 3280 | |
| 3281 | case RE_NON_SPACE: |
| 3282 | /* check for a space of some kind */ |
| 3283 | if (curlen == 0 || t3_is_space(p.getch())) |
| 3284 | { |
| 3285 | local_return(-1); |
| 3286 | } |
| 3287 | |
| 3288 | /* skip this character of the input, and continue */ |
| 3289 | p.inc(&curlen); |
| 3290 | break; |
| 3291 | |
| 3292 | case RE_VSPACE: |
| 3293 | /* check for a vertical space of some kind */ |
| 3294 | if (curlen == 0 || !t3_is_vspace(p.getch())) |
| 3295 | { |
| 3296 | local_return(-1); |
| 3297 | } |
| 3298 | |
| 3299 | /* skip this character of the input, and continue */ |
| 3300 | p.inc(&curlen); |
| 3301 | break; |
| 3302 | |
| 3303 | case RE_NON_VSPACE: |
| 3304 | /* check for a vertical space of some kind */ |
| 3305 | if (curlen == 0 || t3_is_vspace(p.getch())) |
| 3306 | { |
| 3307 | local_return(-1); |
| 3308 | } |
| 3309 | |
| 3310 | /* skip this character of the input, and continue */ |
| 3311 | p.inc(&curlen); |
| 3312 | break; |
| 3313 | |
| 3314 | case RE_PUNCT: |
| 3315 | /* check for a punctuation character of some kind */ |
| 3316 | if (curlen == 0 || !t3_is_punct(p.getch())) |
| 3317 | { |
| 3318 | local_return(-1); |
| 3319 | } |
| 3320 | |
| 3321 | /* skip this character of the input, and continue */ |
| 3322 | p.inc(&curlen); |
| 3323 | break; |
| 3324 | |
| 3325 | case RE_NEWLINE: |
| 3326 | /* check for a newline character of some kind */ |
| 3327 | { |
| 3328 | wchar_t ch; |
| 3329 | |
| 3330 | /* if we're out of characters, we don't have a match */ |
| 3331 | if (curlen == 0) |
| 3332 | { |
| 3333 | local_return(-1); |
| 3334 | } |
| 3335 | |
| 3336 | /* get the character */ |
| 3337 | ch = p.getch(); |
| 3338 | |
| 3339 | /* if it's not a newline, we fail this match */ |
| 3340 | if (ch != 0x000A && ch != 0x000d && ch != 0x000b |
| 3341 | && ch != 0x2028 && ch != 0x2029) |
| 3342 | { |
| 3343 | local_return(-1); |
| 3344 | } |
| 3345 | } |
| 3346 | |
| 3347 | /* skip this character of input and continue */ |
| 3348 | p.inc(&curlen); |
| 3349 | break; |
| 3350 | |
| 3351 | case RE_ASSERT_POS: |
| 3352 | case RE_ASSERT_NEG: |
| 3353 | case RE_ASSERT_BACKPOS: |
| 3354 | case RE_ASSERT_BACKNEG: |
| 3355 | /* |
| 3356 | * It's an assertion. Run this as a sub-state: push the |
| 3357 | * current state so that we can come back to it later. |
| 3358 | */ |
| 3359 | stack_.push(ST_ASSERT, start_ofs, |
| 3360 | p.getptr() - entire_str, curlen, |
| 3361 | cur_state, final_state, 0); |
| 3362 | |
| 3363 | /* |
| 3364 | * In the sub-state, start with the sub-machine's initial |
| 3365 | * state and finish with the sub-machine's final state. |
| 3366 | */ |
| 3367 | cur_state = tuple->info.sub.init; |
| 3368 | final_state = tuple->info.sub.final; |
| 3369 | |
| 3370 | /* in the sub-state, the sub-string to match starts here */ |
| 3371 | start_ofs = p.getptr() - entire_str; |
| 3372 | |
| 3373 | /* |
| 3374 | * in the sub-state, we can use the entire source string, since |
| 3375 | * it's only an assertion - any text the assertion matches |
| 3376 | * after the end of the search region won't be consumed in the |
| 3377 | * enclosing state, so it's fair game within the assertion |
| 3378 | */ |
| 3379 | curlen = entire_str_len - start_ofs; |
| 3380 | |
| 3381 | /* |
| 3382 | * If this is a back assertion, figure the range of match |
| 3383 | * lengths of the assertion. We will need to back up by the |
| 3384 | * minimum match length and try the assertion once; if that |
| 3385 | * fails for a positive assertion or succeeds for a negative |
| 3386 | * assertion, we will then need to back up one more character |
| 3387 | * and try again, and iterate until we've exhausted the range |
| 3388 | * or found a success (positive) or failure (negative). |
| 3389 | */ |
| 3390 | if (tuple->typ == RE_ASSERT_BACKPOS |
| 3391 | || tuple->typ == RE_ASSERT_BACKNEG) |
| 3392 | { |
| 3393 | /* |
| 3394 | * Back up by the minimum match length. We can't possibly |
| 3395 | * have a match for the assertion that starts after this |
| 3396 | * point, because it has to end by the current position and |
| 3397 | * be at least the minimum length long. |
| 3398 | */ |
| 3399 | int l = tuple->info.sub.minlen; |
| 3400 | for ( ; l > 0 && p.getptr() != entire_str ; |
| 3401 | p.dec(&curlen), --l) ; |
| 3402 | |
| 3403 | /* |
| 3404 | * If we were unable to back up by the required minimum |
| 3405 | * length, we definitely cannot have a match for the |
| 3406 | * assertion. If this is a positive assertion, return |
| 3407 | * failure; if this is a negative assertion, it succeeds by |
| 3408 | * virtue of being unable to match. |
| 3409 | */ |
| 3410 | if (l != 0) |
| 3411 | { |
| 3412 | /* not enough characters -> definitely can't match */ |
| 3413 | local_return(-1); |
| 3414 | } |
| 3415 | } |
| 3416 | |
| 3417 | /* |
| 3418 | * just proceed from here; we'll finish up with the assertion |
| 3419 | * test when we reach the final sub-machine state and pop the |
| 3420 | * stack state we pushed |
| 3421 | */ |
| 3422 | goto got_next_state; |
| 3423 | |
| 3424 | case RE_LITERAL: |
| 3425 | /* |
| 3426 | * ordinary character match - if there's not another |
| 3427 | * character, we obviously fail |
| 3428 | */ |
| 3429 | if (curlen == 0) |
| 3430 | { |
| 3431 | local_return(-1); |
| 3432 | } |
| 3433 | |
| 3434 | /* |
| 3435 | * check case sensitivity - if we're not in case-sensitive |
| 3436 | * mode, and both characters are alphabetic, perform a |
| 3437 | * case-insensitive comparison; otherwise, perform an exact |
| 3438 | * comparison |
| 3439 | */ |
| 3440 | if (tuple->info.ch == p.getch()) |
| 3441 | { |
| 3442 | /* |
| 3443 | * we have an exact match; there's no need to check for |
| 3444 | * any case conversions |
| 3445 | */ |
| 3446 | p.inc(&curlen); |
| 3447 | } |
| 3448 | else if (!case_sensitive) |
| 3449 | { |
| 3450 | /* |
| 3451 | * case-insensitive - compare the single pattern character |
| 3452 | * against the source string using full case folding |
| 3453 | */ |
| 3454 | size_t matchlen; |
| 3455 | if (t3_compare_case_fold( |
| 3456 | &tuple->info.ch, 1, p.getptr(), curlen, &matchlen) != 0) |
| 3457 | { |
| 3458 | local_return(-1); |
| 3459 | } |
| 3460 | |
| 3461 | /* matched - skip the matched string characters */ |
| 3462 | p.inc_bytes(matchlen); |
| 3463 | curlen -= matchlen; |
| 3464 | } |
| 3465 | else |
| 3466 | { |
| 3467 | /* |
| 3468 | * the search is case-sensitive, or this pattern character |
| 3469 | * is non-alphabetic; since we didn't find an exact match, |
| 3470 | * the string does not match the pattern |
| 3471 | */ |
| 3472 | local_return(-1); |
| 3473 | } |
| 3474 | break; |
| 3475 | |
| 3476 | case RE_LITSTR: |
| 3477 | case RE_LITSTRA: |
| 3478 | /* literal string - if we're out of characters, fail */ |
| 3479 | if (curlen == 0) |
| 3480 | { |
| 3481 | local_return(-1); |
| 3482 | } |
| 3483 | |
| 3484 | /* compare identically or with case folding, as appropriate */ |
| 3485 | if (case_sensitive) |
| 3486 | { |
| 3487 | /* do an exact character-by-character match */ |
| 3488 | utf8_ptr p2 = p; |
| 3489 | size_t curlen2 = curlen; |
| 3490 | wchar_t *cp = tuple->info.str.str; |
| 3491 | for ( ; *cp != 0 && curlen2 != 0 ; ++cp, p2.inc(&curlen2)) |
| 3492 | { |
| 3493 | if (p2.getch() != *cp) |
| 3494 | { |
| 3495 | local_return(-1); |
| 3496 | } |
| 3497 | } |
| 3498 | |
| 3499 | /* if we didn't match the whole pattern literal, fail */ |
| 3500 | if (*cp != 0) |
| 3501 | { |
| 3502 | local_return(-1); |
| 3503 | } |
| 3504 | |
| 3505 | /* matched - consume the matched source text */ |
| 3506 | p = p2; |
| 3507 | curlen = curlen2; |
| 3508 | } |
| 3509 | else |
| 3510 | { |
| 3511 | /* match with case folding */ |
| 3512 | size_t matchlen; |
| 3513 | if (t3_compare_case_fold( |
| 3514 | tuple->info.str.str, wcslen(tuple->info.str.str), |
| 3515 | p.getptr(), curlen, &matchlen) != 0) |
| 3516 | { |
| 3517 | local_return(-1); |
| 3518 | } |
| 3519 | |
| 3520 | /* matched - skip the matched string characters */ |
| 3521 | p.inc_bytes(matchlen); |
| 3522 | curlen -= matchlen; |
| 3523 | } |
| 3524 | break; |
| 3525 | |
| 3526 | case RE_GROUP_ENTER: |
| 3527 | /* |
| 3528 | * if the group index (given by the state's character ID) is |
| 3529 | * in range, note the location in the string where the group |
| 3530 | * starts |
| 3531 | */ |
| 3532 | if (tuple->info.ch < RE_GROUP_REG_CNT) |
| 3533 | { |
| 3534 | /* save the register in the stack frame if necessary */ |
| 3535 | stack_.save_group_reg(tuple->info.ch, regs); |
| 3536 | |
| 3537 | /* store the new value */ |
| 3538 | regs[tuple->info.ch].start_ofs = p.getptr() - entire_str; |
| 3539 | } |
| 3540 | break; |
| 3541 | |
| 3542 | case RE_GROUP_EXIT: |
| 3543 | /* |
| 3544 | * note the string location of the end of the group, if the |
| 3545 | * group ID is in range |
| 3546 | */ |
| 3547 | if (tuple->info.ch < RE_GROUP_REG_CNT) |
| 3548 | { |
| 3549 | /* save the register in the stack frame if necessary */ |
| 3550 | stack_.save_group_reg(tuple->info.ch, regs); |
| 3551 | |
| 3552 | /* store the new value */ |
| 3553 | regs[tuple->info.ch].end_ofs = p.getptr() - entire_str; |
| 3554 | } |
| 3555 | break; |
| 3556 | |
| 3557 | case RE_EPSILON: |
| 3558 | /* it's an epsilon transition */ |
| 3559 | if (tuple->next_state_2 == RE_STATE_INVALID) |
| 3560 | { |
| 3561 | /* |
| 3562 | * We have only one transition, so this state is entirely |
| 3563 | * deterministic. Simply move on to the next state. (We |
| 3564 | * try to optimize these types of transitions out of the |
| 3565 | * machine when compiling, but we handle them anyway in |
| 3566 | * case any survive optimization.) |
| 3567 | */ |
| 3568 | cur_state = tuple->next_state_1; |
| 3569 | } |
| 3570 | else |
| 3571 | { |
| 3572 | two_way_epsilon: |
| 3573 | /* |
| 3574 | * This state has two possible transitions, and we don't |
| 3575 | * know which one to take. So, try both, see which one |
| 3576 | * works better, and return the result. Try the first |
| 3577 | * transition first. |
| 3578 | * |
| 3579 | * To test both branches, we push the machine state onto |
| 3580 | * the stack, test the first branch, then restore the |
| 3581 | * machine state and test the second branch. We return |
| 3582 | * the better of the two branches. |
| 3583 | * |
| 3584 | * Set up to test the first branch by pushing the current |
| 3585 | * machine state, noting that we're in the first branch of |
| 3586 | * a two-branch epsilon so that we'll know to proceed to |
| 3587 | * the second branch when we finish with the first one. |
| 3588 | */ |
| 3589 | stack_.push(ST_EPS1, start_ofs, |
| 3590 | p.getptr() - entire_str, curlen, |
| 3591 | cur_state, final_state, 0); |
| 3592 | |
| 3593 | /* the next state is the first branch of the epsilon */ |
| 3594 | cur_state = tuple->next_state_1; |
| 3595 | |
| 3596 | /* match the sub-string from here */ |
| 3597 | start_ofs = p.getptr() - entire_str; |
| 3598 | |
| 3599 | /* we have the next state set to go */ |
| 3600 | goto got_next_state; |
| 3601 | } |
| 3602 | break; |
| 3603 | |
| 3604 | default: |
| 3605 | /* invalid node type */ |
| 3606 | assert(FALSE); |
| 3607 | local_return(-1); |
| 3608 | } |
| 3609 | |
| 3610 | /* |
| 3611 | * if we got this far, we were successful - move on to the next |
| 3612 | * state |
| 3613 | */ |
| 3614 | cur_state = tuple->next_state_1; |
| 3615 | |
| 3616 | got_next_state: |
| 3617 | /* we come here when we've already figured out the next state */ |
| 3618 | ; |
| 3619 | |
| 3620 | /* |
| 3621 | * If we're in the final state, it means we've matched the |
| 3622 | * pattern. Return success by indicating the length of the string |
| 3623 | * we matched. |
| 3624 | */ |
| 3625 | if (cur_state == final_state) |
| 3626 | { |
| 3627 | local_return(p.getptr() - (entire_str + start_ofs)); |
| 3628 | } |
| 3629 | |
| 3630 | /* |
| 3631 | * if we're in an invalid state, the expression must have been |
| 3632 | * ill-formed; return failure |
| 3633 | */ |
| 3634 | if (cur_state == RE_STATE_INVALID) |
| 3635 | { |
| 3636 | local_return(-1); |
| 3637 | } |
| 3638 | |
| 3639 | /* resume the loop */ |
| 3640 | continue; |
| 3641 | |
| 3642 | do_local_return: |
| 3643 | /* check what kind of state we're returning to */ |
| 3644 | switch(stack_.get_top_type()) |
| 3645 | { |
| 3646 | int str_ofs; |
| 3647 | int ret1, ret2; |
| 3648 | int ret1_is_winner; |
| 3649 | int iter; |
| 3650 | re_recog_type typ; |
| 3651 | int lblen; |
| 3652 | |
| 3653 | case -1: |
| 3654 | /* |
| 3655 | * There's nothing left on the state stack, so we're actually |
| 3656 | * returning to our caller. Simply return the given return |
| 3657 | * value. |
| 3658 | */ |
| 3659 | return _retval_; |
| 3660 | |
| 3661 | case ST_EPS1: |
| 3662 | /* |
| 3663 | * We're finishing the first branch of a two-branch epsilon. |
| 3664 | * Swap the current state with the saved state on the stack, |
| 3665 | * and push a new state for the second branch. |
| 3666 | */ |
| 3667 | str_ofs = p.getptr() - entire_str; |
| 3668 | stack_.swap_and_push(_retval_, ST_EPS2, |
| 3669 | &start_ofs, &str_ofs, &curlen, |
| 3670 | &cur_state, &final_state, regs, loop_vars); |
| 3671 | |
| 3672 | /* the next state is the second branch */ |
| 3673 | cur_state = tuple_arr[cur_state].next_state_2; |
| 3674 | |
| 3675 | /* set up at the original string position */ |
| 3676 | p.set((char *)entire_str + str_ofs); |
| 3677 | |
| 3678 | /* the second branch substring starts at the current character */ |
| 3679 | start_ofs = p.getptr() - entire_str; |
| 3680 | |
| 3681 | /* continue from here */ |
| 3682 | break; |
| 3683 | |
| 3684 | case ST_EPS2: |
| 3685 | /* |
| 3686 | * We're finishing the second branch of a two-branch epsilon. |
| 3687 | * First, get the two return values - the first branch is the |
| 3688 | * return value saved in the second-from-the-top stack frame, |
| 3689 | * and the second branch is the current return value. |
| 3690 | */ |
| 3691 | ret1 = stack_.get_frame(1)->retval; |
| 3692 | ret2 = _retval_; |
| 3693 | |
| 3694 | /* |
| 3695 | * If they both failed, the whole thing failed. Otherwise, |
| 3696 | * return the longer or shorter of the two, depending on the |
| 3697 | * current match mode, plus the length we ourselves matched |
| 3698 | * previously. Note that we return the register set from the |
| 3699 | * winning match. |
| 3700 | */ |
| 3701 | if (ret1 < 0 && ret2 < 0) |
| 3702 | { |
| 3703 | /* |
| 3704 | * They both failed - backtrack to the initial state by |
| 3705 | * popping the second branch's frame (which stores the |
| 3706 | * branch initial state) and discarding the first branch's |
| 3707 | * frame (which stores the first branch's *final* state). |
| 3708 | * Note that the only thing we care about from the stack is |
| 3709 | * the group registers and loop variables; everything else |
| 3710 | * will be restored from the enclosing frame that we're |
| 3711 | * returning to. |
| 3712 | */ |
| 3713 | stack_.pop(&start_ofs, &str_ofs, &curlen, |
| 3714 | &cur_state, &final_state, regs, loop_vars, &iter); |
| 3715 | stack_.discard(); |
| 3716 | |
| 3717 | /* set the string pointer */ |
| 3718 | p.set((char *)entire_str + str_ofs); |
| 3719 | |
| 3720 | /* return failure */ |
| 3721 | local_return(-1); |
| 3722 | } |
| 3723 | |
| 3724 | /* |
| 3725 | * Choose the winner based on the match mode. The match mode |
| 3726 | * of interest is the one in the original two-way epsilon, |
| 3727 | * which is the state in the stack frame at the top of the |
| 3728 | * stack. |
| 3729 | */ |
| 3730 | if (pattern->longest_match |
| 3731 | && !(tuple_arr[stack_.get_frame(0)->state].flags |
| 3732 | & RE_STATE_SHORTEST)) |
| 3733 | { |
| 3734 | /* |
| 3735 | * longest match - choose ret1 if ret2 isn't a good match, |
| 3736 | * or ret1 is longer than ret2 |
| 3737 | */ |
| 3738 | ret1_is_winner = (ret2 < 0 || ret1 >= ret2); |
| 3739 | } |
| 3740 | else |
| 3741 | { |
| 3742 | /* |
| 3743 | * shortest match - choose ret1 if it's the only good |
| 3744 | * match, or if they're both good and it's the shorter one |
| 3745 | */ |
| 3746 | ret1_is_winner = ((ret1 >= 0 && ret2 < 0) |
| 3747 | || (ret1 >= 0 && ret2 >= 0 |
| 3748 | && ret1 <= ret2)); |
| 3749 | } |
| 3750 | |
| 3751 | /* choose the winner */ |
| 3752 | if (ret1_is_winner) |
| 3753 | { |
| 3754 | /* |
| 3755 | * The winner is the first branch. First, pop the second |
| 3756 | * branch's initial state, since this is the baseline for |
| 3757 | * the first branch's final state. |
| 3758 | */ |
| 3759 | stack_.pop(&start_ofs, &str_ofs, &curlen, |
| 3760 | &cur_state, &final_state, regs, loop_vars, &iter); |
| 3761 | |
| 3762 | /* add in the length up to the initial state at the epsilon */ |
| 3763 | ret1 += str_ofs - start_ofs; |
| 3764 | |
| 3765 | /* |
| 3766 | * Get the list of register deltas in this stack frame. |
| 3767 | * We're keeping the final state for this branch for now, |
| 3768 | * so we'll need to propagate its deltas to the parent |
| 3769 | * frame. |
| 3770 | */ |
| 3771 | reg_deltas d(stack_); |
| 3772 | |
| 3773 | /* |
| 3774 | * Swap and pop the first branch's final state. This is |
| 3775 | * stored relative to the initial state of the branch, so |
| 3776 | * we're ready to restore it now. We swap the state so |
| 3777 | * that we have the initial values of the group registers |
| 3778 | * back in the frame, for propagation to the parent frame. |
| 3779 | */ |
| 3780 | stack_.swap_and_pop(&start_ofs, &str_ofs, &curlen, |
| 3781 | &cur_state, &final_state, |
| 3782 | regs, loop_vars, &iter); |
| 3783 | |
| 3784 | /* propagate the register deltas to the parent frame */ |
| 3785 | d.propagate(stack_); |
| 3786 | |
| 3787 | /* set the string pointer */ |
| 3788 | p.set((char *)entire_str + str_ofs); |
| 3789 | |
| 3790 | /* return the result */ |
| 3791 | local_return(ret1); |
| 3792 | } |
| 3793 | else |
| 3794 | { |
| 3795 | /* |
| 3796 | * The winner is the second branch. The current machine |
| 3797 | * state is the final state for the second branch, so we |
| 3798 | * simply need to discard the saved state for the two |
| 3799 | * branches. |
| 3800 | */ |
| 3801 | |
| 3802 | /* add in the length up to the initial state at the epsilon */ |
| 3803 | regex_stack_entry *fp = stack_.get_frame(0); |
| 3804 | ret2 += fp->str_ofs - fp->start_ofs; |
| 3805 | |
| 3806 | /* |
| 3807 | * Get the list of register deltas in the top frame. The |
| 3808 | * top frame is the initial state for the second branch, |
| 3809 | * which is the branch we're keeping, so we'll need to |
| 3810 | * propagate its deltas up to the parent frame. |
| 3811 | */ |
| 3812 | reg_deltas d(stack_); |
| 3813 | |
| 3814 | /* discard the two branch states */ |
| 3815 | stack_.discard(); |
| 3816 | stack_.discard(); |
| 3817 | |
| 3818 | /* propagate the register deltas to the parent frame */ |
| 3819 | d.propagate(stack_); |
| 3820 | |
| 3821 | /* return the result */ |
| 3822 | local_return(ret2); |
| 3823 | } |
| 3824 | |
| 3825 | case ST_ASSERT: |
| 3826 | /* |
| 3827 | * We're finishing an assertion sub-machine. First, pop the |
| 3828 | * machine state to get back to where we were. |
| 3829 | */ |
| 3830 | stack_.pop(&start_ofs, &str_ofs, &curlen, |
| 3831 | &cur_state, &final_state, regs, loop_vars, &iter); |
| 3832 | |
| 3833 | /* we look at the type a lot, so get it into a local */ |
| 3834 | typ = tuple_arr[cur_state].typ; |
| 3835 | |
| 3836 | /* |
| 3837 | * For a look-back assertion, figure the look-back length of |
| 3838 | * the current iteration. We start at iteration zero and work |
| 3839 | * up from there, so the lookback length is the minimum length |
| 3840 | * plus the iteration number. |
| 3841 | */ |
| 3842 | lblen = iter + tuple_arr[cur_state].info.sub.minlen; |
| 3843 | |
| 3844 | /* |
| 3845 | * set the string pointer and remaining length for the string |
| 3846 | * offset we popped from the state |
| 3847 | */ |
| 3848 | p.set((char *)entire_str + str_ofs); |
| 3849 | |
| 3850 | /* |
| 3851 | * If this is a look-back assertion, and it's not a match, |
| 3852 | * proceed to the next iteration. We have to do this for both |
| 3853 | * positive and negative look-backs: if it's positive and it's |
| 3854 | * not a match, we can't assume failure until we've looked for |
| 3855 | * the other possible match lengths; if it's negative and it's |
| 3856 | * not a match, we can't assume success until we've looked for |
| 3857 | * other possible matches. |
| 3858 | */ |
| 3859 | if (_retval_ < 0 |
| 3860 | && (typ == RE_ASSERT_BACKPOS || typ == RE_ASSERT_BACKNEG)) |
| 3861 | { |
| 3862 | /* get the next look-back length */ |
| 3863 | ++iter; |
| 3864 | ++lblen; |
| 3865 | |
| 3866 | /* |
| 3867 | * If this is still in range, go back for another try at |
| 3868 | * the new length. Note that a maximum of -1 means that |
| 3869 | * the range is unbounded. |
| 3870 | */ |
| 3871 | int maxlen = tuple_arr[cur_state].info.sub.maxlen; |
| 3872 | utf8_ptr ep((char *)entire_str); |
| 3873 | if ((maxlen < 0 || lblen <= maxlen) |
| 3874 | && ep.len(p.getptr() - entire_str) >= (size_t)lblen) |
| 3875 | { |
| 3876 | /* re-push our assertion sub-state */ |
| 3877 | stack_.push(ST_ASSERT, start_ofs, |
| 3878 | p.getptr() - entire_str, curlen, |
| 3879 | cur_state, final_state, iter); |
| 3880 | |
| 3881 | /* back up by lblen characters */ |
| 3882 | for ( ; lblen > 0 && p.getptr() != entire_str ; |
| 3883 | p.dec(&curlen), --lblen) ; |
| 3884 | |
| 3885 | /* recurse into the group */ |
| 3886 | final_state = tuple_arr[cur_state].info.sub.final; |
| 3887 | cur_state = tuple_arr[cur_state].info.sub.init; |
| 3888 | goto got_next_state; |
| 3889 | } |
| 3890 | } |
| 3891 | |
| 3892 | /* |
| 3893 | * If this is a positive assertion and it didn't match, OR it's |
| 3894 | * a negative assertion and it DID match, return failure |
| 3895 | */ |
| 3896 | if ((typ == RE_ASSERT_POS || typ == RE_ASSERT_BACKPOS) |
| 3897 | ? _retval_ < 0 : _retval_ >= 0) |
| 3898 | { |
| 3899 | local_return(-1); |
| 3900 | } |
| 3901 | |
| 3902 | /* resume where we left off */ |
| 3903 | cur_state = tuple_arr[cur_state].next_state_1; |
| 3904 | break; |
| 3905 | } |
| 3906 | |
| 3907 | /* resume from here */ |
| 3908 | goto got_next_state; |
| 3909 | } |
| 3910 | } |
| 3911 | |
| 3912 | /* ------------------------------------------------------------------------ */ |
| 3913 | /* |
| 3914 | * Search for a regular expression within a string. Returns -1 if the |
| 3915 | * string cannot be found, otherwise returns the offset from the start |
| 3916 | * of the string to be searched of the start of the first match for the |
| 3917 | * pattern. |
| 3918 | */ |
| 3919 | int CRegexSearcher::search(const char *entirestr, const char *str, size_t len, |
| 3920 | const re_compiled_pattern_base *pattern, |
| 3921 | const re_tuple *tuple_arr, |
| 3922 | const re_machine *machine, re_group_register *regs, |
| 3923 | int *result_len) |
| 3924 | { |
| 3925 | re_group_register best_match_regs[RE_GROUP_REG_CNT]; |
| 3926 | short loop_vars[RE_LOOP_VARS_MAX]; |
| 3927 | |
| 3928 | /* we don't have a match yet */ |
| 3929 | int best_match_len = -1; |
| 3930 | int best_match_start = -1; |
| 3931 | |
| 3932 | /* search the entire string */ |
| 3933 | const char *max_start_pos = str + len; |
| 3934 | |
| 3935 | /* figure the length of the overall string */ |
| 3936 | size_t entirelen = len + (str - entirestr); |
| 3937 | |
| 3938 | /* |
| 3939 | * Starting at the first character in the string, search for the |
| 3940 | * pattern at each subsequent character until we either find the |
| 3941 | * pattern or run out of string to test. |
| 3942 | */ |
| 3943 | utf8_ptr p; |
| 3944 | for (p.set((char *)str) ; p.getptr() <= max_start_pos ; p.inc(&len)) |
| 3945 | { |
| 3946 | /* check for a match */ |
| 3947 | int matchlen = match(entirestr, entirelen, p.getptr(), len, |
| 3948 | pattern, tuple_arr, machine, regs, loop_vars); |
| 3949 | if (matchlen >= 0) |
| 3950 | { |
| 3951 | /* check our first-begin/first-end mode */ |
| 3952 | if (pattern->first_begin) |
| 3953 | { |
| 3954 | /* |
| 3955 | * We're in first-begin mode: return immediately, |
| 3956 | * because we want to find the match that starts at the |
| 3957 | * earliest point in the string; having found a match |
| 3958 | * here, there's no point in looking for a later match, |
| 3959 | * since it obviously won't start any earlier than this |
| 3960 | * one does. |
| 3961 | */ |
| 3962 | *result_len = matchlen; |
| 3963 | return p.getptr() - str; |
| 3964 | } |
| 3965 | else |
| 3966 | { |
| 3967 | int keep; |
| 3968 | |
| 3969 | /* |
| 3970 | * We're in first-end mode. We can't return yet, |
| 3971 | * because we might find something that ends before this |
| 3972 | * string does. |
| 3973 | * |
| 3974 | * If this is the first or best match so far, note it; |
| 3975 | * otherwise, ignore it. In any case, continue looking. |
| 3976 | */ |
| 3977 | if (best_match_len == -1) |
| 3978 | { |
| 3979 | /* |
| 3980 | * this is our first match - it's definitely the |
| 3981 | * best so far |
| 3982 | */ |
| 3983 | keep = TRUE; |
| 3984 | } |
| 3985 | else |
| 3986 | { |
| 3987 | /* calculate the ending index of this match */ |
| 3988 | int end_idx = (p.getptr() - str) + matchlen; |
| 3989 | |
| 3990 | /* see what we have */ |
| 3991 | if (end_idx < best_match_start + best_match_len) |
| 3992 | { |
| 3993 | /* |
| 3994 | * this one ends earlier than the previous best |
| 3995 | * match so far -- we definitely want to keep |
| 3996 | * this one |
| 3997 | */ |
| 3998 | keep = TRUE; |
| 3999 | } |
| 4000 | else if (end_idx == best_match_start + best_match_len) |
| 4001 | { |
| 4002 | /* |
| 4003 | * This one ends at exactly the same point as |
| 4004 | * our best match so far. Decide which one to |
| 4005 | * keep based on the lengths. If we're in |
| 4006 | * longest-match mode, keep the longer one, |
| 4007 | * otherwise keep the shorter one. |
| 4008 | */ |
| 4009 | if (pattern->longest_match) |
| 4010 | { |
| 4011 | /* |
| 4012 | * longest-match mode - keep the old one, |
| 4013 | * since it's longer (it starts earlier and |
| 4014 | * ends at the same point) |
| 4015 | */ |
| 4016 | keep = FALSE; |
| 4017 | } |
| 4018 | else |
| 4019 | { |
| 4020 | /* |
| 4021 | * shortest-match mode - keep the new one, |
| 4022 | * since it's shorter (it starts later and |
| 4023 | * ends at the same point) |
| 4024 | */ |
| 4025 | keep = TRUE; |
| 4026 | } |
| 4027 | } |
| 4028 | else |
| 4029 | { |
| 4030 | /* |
| 4031 | * this one isn't as good as the previous best |
| 4032 | * -- ignore this one and keep our previous |
| 4033 | * winner |
| 4034 | */ |
| 4035 | keep = FALSE; |
| 4036 | } |
| 4037 | } |
| 4038 | |
| 4039 | /* if we're keeping this match, save it */ |
| 4040 | if (keep) |
| 4041 | { |
| 4042 | /* save the start index, length, and register contents */ |
| 4043 | best_match_start = p.getptr() - str; |
| 4044 | best_match_len = matchlen; |
| 4045 | memcpy(best_match_regs, regs, sizeof(best_match_regs)); |
| 4046 | |
| 4047 | /* |
| 4048 | * There's no point in looking for strings that |
| 4049 | * start beyond the end of the current match, |
| 4050 | * because they certainly won't end before this |
| 4051 | * match ends. So, adjust our end-of-loop marker to |
| 4052 | * point to the next character past the end of our |
| 4053 | * match. |
| 4054 | */ |
| 4055 | max_start_pos = p.getptr() + matchlen; |
| 4056 | } |
| 4057 | } |
| 4058 | } |
| 4059 | } |
| 4060 | |
| 4061 | /* if we found a previous match, return it */ |
| 4062 | if (best_match_len != -1) |
| 4063 | { |
| 4064 | /* set the caller's match length */ |
| 4065 | *result_len = best_match_len; |
| 4066 | |
| 4067 | /* copy the saved match registers into the caller's registers */ |
| 4068 | memcpy(regs, best_match_regs, sizeof(best_match_regs)); |
| 4069 | |
| 4070 | /* return the starting index of the match */ |
| 4071 | return best_match_start; |
| 4072 | } |
| 4073 | |
| 4074 | /* we didn't find a match */ |
| 4075 | return -1; |
| 4076 | } |
| 4077 | |
| 4078 | /* |
| 4079 | * Search backwards. In reverse searches, everything is mirrored. The |
| 4080 | * starting position 'str' is actually the boundary for the right side of |
| 4081 | * the match, so the match must end before this character. Likewise, the |
| 4082 | * endpoints and directions for <FirstBegin> and <FirstEnd> are mirrored. |
| 4083 | * The "beginning" endpoint is the right side of the match, and the |
| 4084 | * "ending" endpoint is the left side. "First" means closest to the |
| 4085 | * starting position, so a higher index is firstier than a lower index. |
| 4086 | */ |
| 4087 | int CRegexSearcher::search_back( |
| 4088 | const char *entirestr, const char *str, size_t len, |
| 4089 | const re_compiled_pattern_base *pattern, |
| 4090 | const re_tuple *tuple_arr, |
| 4091 | const re_machine *machine, re_group_register *regs, |
| 4092 | int *result_len) |
| 4093 | { |
| 4094 | re_group_register best_match_regs[RE_GROUP_REG_CNT]; |
| 4095 | short loop_vars[RE_LOOP_VARS_MAX]; |
| 4096 | |
| 4097 | /* we don't have a match yet */ |
| 4098 | int best_match_len = -1; |
| 4099 | int best_match_start = -1; |
| 4100 | |
| 4101 | /* figure the overall string length */ |
| 4102 | size_t entirelen = len + (str - entirestr); |
| 4103 | |
| 4104 | /* |
| 4105 | * For a reverse search, we only want matches up to the starting |
| 4106 | * position, so the length of the matchable portion for the first |
| 4107 | * search at the starting position is zero. Note that we still need |
| 4108 | * the entire string length, since patterns can have assertions that go |
| 4109 | * past the end of the matched text. |
| 4110 | */ |
| 4111 | len = 0; |
| 4112 | |
| 4113 | /* |
| 4114 | * Starting at the current position, search for the pattern at each |
| 4115 | * earlier character until we either find the pattern or run out of |
| 4116 | * string to test. |
| 4117 | */ |
| 4118 | utf8_ptr p; |
| 4119 | for (p.set((char *)str) ; ; p.dec(&len)) |
| 4120 | { |
| 4121 | /* check for a match */ |
| 4122 | int matchlen = match(entirestr, entirelen, p.getptr(), len, |
| 4123 | pattern, tuple_arr, machine, regs, loop_vars); |
| 4124 | if (matchlen >= 0) |
| 4125 | { |
| 4126 | /* check our first-begin/first-end mode */ |
| 4127 | if (pattern->first_begin) |
| 4128 | { |
| 4129 | /* |
| 4130 | * We're in first-beginning mode, which in a reverse search |
| 4131 | * means that we want the *end* of the match to have the |
| 4132 | * highest index. The end of the match is the endpoint |
| 4133 | * facing the starting point, so we want that endpoint to |
| 4134 | * be as close to the starting point as possible, which |
| 4135 | * means the one with the highest index. |
| 4136 | */ |
| 4137 | int keep; |
| 4138 | int end_idx = (p.getptr() - str) + matchlen; |
| 4139 | if (best_match_len == -1 |
| 4140 | || end_idx > best_match_start + best_match_len) |
| 4141 | { |
| 4142 | /* |
| 4143 | * this is the first match, or it's closer to the |
| 4144 | * starting point than the previous match - keep it |
| 4145 | */ |
| 4146 | keep = TRUE; |
| 4147 | } |
| 4148 | else if (end_idx == best_match_start + best_match_len |
| 4149 | && pattern->longest_match) |
| 4150 | { |
| 4151 | /* |
| 4152 | * this match has the same end index as the previous |
| 4153 | * match; and we know it has an earlier start index |
| 4154 | * (because our loop walks backwards through the |
| 4155 | * string), which means it's longer than the previous |
| 4156 | * match; and we're in longest match mode - so this is |
| 4157 | * a longer match that's equally close, so it's a |
| 4158 | * keeper |
| 4159 | */ |
| 4160 | keep = TRUE; |
| 4161 | } |
| 4162 | else |
| 4163 | { |
| 4164 | /* otherwise, we're not keeping this match */ |
| 4165 | keep = FALSE; |
| 4166 | } |
| 4167 | |
| 4168 | /* if we're keeping this match, save it */ |
| 4169 | if (keep) |
| 4170 | { |
| 4171 | /* save the start index, length, and register contents */ |
| 4172 | best_match_start = p.getptr() - str; |
| 4173 | best_match_len = matchlen; |
| 4174 | memcpy(best_match_regs, regs, sizeof(best_match_regs)); |
| 4175 | } |
| 4176 | } |
| 4177 | else |
| 4178 | { |
| 4179 | /* |
| 4180 | * We're in first-ending mode. In a reverse search, this |
| 4181 | * means that we want the *start* of the match (the end |
| 4182 | * farther from the starting point) to have the *highest* |
| 4183 | * index (so it's closer to the starting point). This is |
| 4184 | * the easy case for a reverse search, because the start |
| 4185 | * point is the loop iteration variable: every future match |
| 4186 | * will have a lower starting index (further away from the |
| 4187 | * starting point) since that's how we walk through the |
| 4188 | * string. So we'll never find a match with a starting |
| 4189 | * point as close as this one - meaning we can return this |
| 4190 | * one without doing any more looking. |
| 4191 | */ |
| 4192 | *result_len = matchlen; |
| 4193 | return str - p.getptr(); |
| 4194 | } |
| 4195 | } |
| 4196 | |
| 4197 | /* if we've reached the start of the string, there's nowhere to go */ |
| 4198 | if (p.getptr() == entirestr) |
| 4199 | break; |
| 4200 | } |
| 4201 | |
| 4202 | /* if we found a previous match, return it */ |
| 4203 | if (best_match_len != -1) |
| 4204 | { |
| 4205 | /* set the caller's match length */ |
| 4206 | *result_len = best_match_len; |
| 4207 | |
| 4208 | /* copy the saved match registers into the caller's registers */ |
| 4209 | memcpy(regs, best_match_regs, sizeof(best_match_regs)); |
| 4210 | |
| 4211 | /* |
| 4212 | * Return the starting index of the match. Note that the return |
| 4213 | * value is the number of bytes *before* the starting point, |
| 4214 | * whereas our internal counter is the index offset, which is |
| 4215 | * negative; so simply negate it for the return value. |
| 4216 | */ |
| 4217 | return -best_match_start; |
| 4218 | } |
| 4219 | |
| 4220 | /* we didn't find a match */ |
| 4221 | return -1; |
| 4222 | } |
| 4223 | |
| 4224 | /* ------------------------------------------------------------------------ */ |
| 4225 | /* |
| 4226 | * Search for a previously-compiled pattern within the given string. |
| 4227 | * Returns the offset of the match, or -1 if no match was found. |
| 4228 | */ |
| 4229 | int CRegexSearcher::search_for_pattern( |
| 4230 | const re_compiled_pattern *pattern, |
| 4231 | const char *entirestr, const char *searchstr, size_t searchlen, |
| 4232 | int *result_len, re_group_register *regs) |
| 4233 | { |
| 4234 | /* |
| 4235 | * search for the pattern in our copy of the string - use the copy so |
| 4236 | * that the group registers stay valid even if the caller deallocates |
| 4237 | * the original string after we return |
| 4238 | */ |
| 4239 | return search(entirestr, searchstr, searchlen, pattern, pattern->tuples, |
| 4240 | &pattern->machine, regs, result_len); |
| 4241 | } |
| 4242 | |
| 4243 | /* |
| 4244 | * Search backwards for a pattern |
| 4245 | */ |
| 4246 | int CRegexSearcher::search_back_for_pattern( |
| 4247 | const re_compiled_pattern *pattern, |
| 4248 | const char *entirestr, const char *searchstr, size_t searchlen, |
| 4249 | int *result_len, re_group_register *regs) |
| 4250 | { |
| 4251 | /* |
| 4252 | * search for the pattern in our copy of the string - use the copy so |
| 4253 | * that the group registers stay valid even if the caller deallocates |
| 4254 | * the original string after we return |
| 4255 | */ |
| 4256 | return search_back(entirestr, searchstr, searchlen, pattern, |
| 4257 | pattern->tuples, &pattern->machine, regs, result_len); |
| 4258 | } |
| 4259 | |
| 4260 | /* ------------------------------------------------------------------------ */ |
| 4261 | /* |
| 4262 | * Check for a match to a previously compiled expression. Returns the |
| 4263 | * length of the match if we found a match, -1 if we found no match. This |
| 4264 | * is not a search function; we merely match the leading substring of the |
| 4265 | * given string to the given pattern. |
| 4266 | */ |
| 4267 | int CRegexSearcher::match_pattern( |
| 4268 | const re_compiled_pattern *pattern, |
| 4269 | const char *entirestr, const char *searchstr, size_t searchlen, |
| 4270 | re_group_register *regs) |
| 4271 | { |
| 4272 | short loop_vars[RE_LOOP_VARS_MAX]; |
| 4273 | |
| 4274 | /* match the string */ |
| 4275 | return match(entirestr, searchlen + (searchstr - entirestr), |
| 4276 | searchstr, searchlen, |
| 4277 | pattern, pattern->tuples, &pattern->machine, |
| 4278 | regs, loop_vars); |
| 4279 | } |
| 4280 | |
| 4281 | /* ------------------------------------------------------------------------ */ |
| 4282 | /* |
| 4283 | * Compile an expression and check for a match. Returns the length of the |
| 4284 | * match if we found a match, -1 if we found no match. This is not a |
| 4285 | * search function; we merely match the leading substring of the given |
| 4286 | * string to the given pattern. |
| 4287 | * |
| 4288 | * If a pattern pattern will be used repeatedly in multiple searches or |
| 4289 | * matches, it is more efficient to compile the pattern once and then |
| 4290 | * re-use the compiled pattern for each search or match, since compiling a |
| 4291 | * pattern takes time. |
| 4292 | */ |
| 4293 | int CRegexSearcherSimple::compile_and_match( |
| 4294 | const char *patstr, size_t patlen, |
| 4295 | const char *entirestr, const char *searchstr, size_t searchlen) |
| 4296 | { |
| 4297 | re_compiled_pattern_base pat; |
| 4298 | short loop_vars[RE_LOOP_VARS_MAX]; |
| 4299 | |
| 4300 | /* no groups yet */ |
| 4301 | group_cnt_ = 0; |
| 4302 | |
| 4303 | /* clear the group registers */ |
| 4304 | clear_group_regs(); |
| 4305 | |
| 4306 | /* compile the expression - return failure if we get an error */ |
| 4307 | if (parser_->compile(patstr, patlen, &pat) != RE_STATUS_SUCCESS) |
| 4308 | return FALSE; |
| 4309 | |
| 4310 | /* remember the group count from the compiled pattern */ |
| 4311 | group_cnt_ = pat.group_cnt; |
| 4312 | |
| 4313 | /* match the string */ |
| 4314 | int m = match(entirestr, searchlen + (searchstr - entirestr), |
| 4315 | searchstr, searchlen, |
| 4316 | &pat, parser_->tuple_arr_, &pat.machine, regs_, loop_vars); |
| 4317 | |
| 4318 | /* save the match information on success */ |
| 4319 | if (m >= 0) |
| 4320 | { |
| 4321 | match_.start_ofs = searchstr - entirestr; |
| 4322 | match_.end_ofs = match_.start_ofs + m; |
| 4323 | } |
| 4324 | |
| 4325 | /* return the result */ |
| 4326 | return m; |
| 4327 | } |
| 4328 | |
| 4329 | /* ------------------------------------------------------------------------ */ |
| 4330 | /* |
| 4331 | * Compile an expression and search for a match within the given string. |
| 4332 | * Returns the offset of the match, or -1 if no match was found. |
| 4333 | * |
| 4334 | * If a pattern will be used repeatedly in multiple searches or matches, it |
| 4335 | * is more efficient to compile the pattern once, using compile_pattern(), |
| 4336 | * and then re-use the compiled pattern for each search, since compiling a |
| 4337 | * pattern takes time. |
| 4338 | */ |
| 4339 | int CRegexSearcherSimple::compile_and_search( |
| 4340 | const char *patstr, size_t patlen, |
| 4341 | const char *entirestr, const char *searchstr, size_t searchlen, |
| 4342 | int *result_len) |
| 4343 | { |
| 4344 | /* no groups yet */ |
| 4345 | group_cnt_ = 0; |
| 4346 | |
| 4347 | /* clear the group registers */ |
| 4348 | clear_group_regs(); |
| 4349 | |
| 4350 | /* compile the expression - return failure if we get an error */ |
| 4351 | re_compiled_pattern_base pat; |
| 4352 | if (parser_->compile(patstr, patlen, &pat) != RE_STATUS_SUCCESS) |
| 4353 | return -1; |
| 4354 | |
| 4355 | /* remember the group count from the compiled pattern */ |
| 4356 | group_cnt_ = pat.group_cnt; |
| 4357 | |
| 4358 | /* |
| 4359 | * search for the pattern in our copy of the string - use the copy so |
| 4360 | * that the group registers stay valid even if the caller deallocates |
| 4361 | * the original string after we return |
| 4362 | */ |
| 4363 | int m = search(entirestr, searchstr, searchlen, |
| 4364 | &pat, parser_->tuple_arr_, &pat.machine, |
| 4365 | regs_, result_len); |
| 4366 | |
| 4367 | /* save the match information on success */ |
| 4368 | if (m >= 0) |
| 4369 | { |
| 4370 | match_.start_ofs = m; |
| 4371 | match_.end_ofs = m + *result_len; |
| 4372 | } |
| 4373 | |
| 4374 | /* return the result */ |
| 4375 | return m; |
| 4376 | } |
| 4377 | |
| 4378 | /* |
| 4379 | * compile and search backwards |
| 4380 | */ |
| 4381 | int CRegexSearcherSimple::compile_and_search_back( |
| 4382 | const char *patstr, size_t patlen, |
| 4383 | const char *entirestr, const char *searchstr, size_t searchlen, |
| 4384 | int *result_len) |
| 4385 | { |
| 4386 | /* no groups yet */ |
| 4387 | group_cnt_ = 0; |
| 4388 | |
| 4389 | /* clear the group registers */ |
| 4390 | clear_group_regs(); |
| 4391 | |
| 4392 | /* compile the expression - return failure if we get an error */ |
| 4393 | re_compiled_pattern_base pat; |
| 4394 | if (parser_->compile(patstr, patlen, &pat) != RE_STATUS_SUCCESS) |
| 4395 | return -1; |
| 4396 | |
| 4397 | /* remember the group count from the compiled pattern */ |
| 4398 | group_cnt_ = pat.group_cnt; |
| 4399 | |
| 4400 | /* |
| 4401 | * search for the pattern in our copy of the string - use the copy so |
| 4402 | * that the group registers stay valid even if the caller deallocates |
| 4403 | * the original string after we return |
| 4404 | */ |
| 4405 | int m = search_back(entirestr, searchstr, searchlen, |
| 4406 | &pat, parser_->tuple_arr_, &pat.machine, |
| 4407 | regs_, result_len); |
| 4408 | |
| 4409 | /* save the match information on success */ |
| 4410 | if (m >= 0) |
| 4411 | { |
| 4412 | match_.start_ofs = searchstr - entirestr - m; |
| 4413 | match_.end_ofs = match_.start_ofs + *result_len; |
| 4414 | } |
| 4415 | |
| 4416 | /* return the result */ |
| 4417 | return m; |
| 4418 | } |