| 1 |
/* dfa.c - deterministic extended regexp routines for GNU |
| 2 |
Copyright (C) 1988, 1998, 2000, 2002, 2004-2005, 2007-2025 Free Software |
| 3 |
Foundation, Inc. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it and/or modify |
| 6 |
it under the terms of the GNU General Public License as published by |
| 7 |
the Free Software Foundation, either version 3, or (at your option) |
| 8 |
any later version. |
| 9 |
|
| 10 |
This program is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
GNU General Public License for more details. |
| 14 |
|
| 15 |
You should have received a copy of the GNU General Public License |
| 16 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 17 |
|
| 18 |
/* Written June, 1988 by Mike Haertel |
| 19 |
Modified July, 1988 by Arthur David Olson to assist BMG speedups */ |
| 20 |
|
| 21 |
#include <config.h> |
| 22 |
|
| 23 |
#include "dfa.h" |
| 24 |
|
| 25 |
#include "flexmember.h" |
| 26 |
#include "idx.h" |
| 27 |
#include "verify.h" |
| 28 |
|
| 29 |
#include <assert.h> |
| 30 |
#include <ctype.h> |
| 31 |
#include <stdint.h> |
| 32 |
#include <stdio.h> |
| 33 |
#include <stdlib.h> |
| 34 |
#include <limits.h> |
| 35 |
#include <string.h> |
| 36 |
#include <wchar.h> |
| 37 |
|
| 38 |
#include "gettext.h" |
| 39 |
#define _(str) gettext (str) |
| 40 |
|
| 41 |
#include "xalloc.h" |
| 42 |
#include "localeinfo.h" |
| 43 |
|
| 44 |
#if GAWK |
| 45 |
/* Use ISO C 99 API. */ |
| 46 |
# include <wctype.h> |
| 47 |
# define char32_t wchar_t |
| 48 |
# define mbrtoc32 mbrtowc |
| 49 |
# define c32rtomb wcrtomb |
| 50 |
# define c32tob wctob |
| 51 |
# define c32isprint iswprint |
| 52 |
# define c32isspace iswspace |
| 53 |
# define mbszero(p) memset ((p), 0, sizeof (mbstate_t)) |
| 54 |
#else |
| 55 |
/* Use ISO C 11 + gnulib API. */ |
| 56 |
# include <uchar.h> |
| 57 |
#endif |
| 58 |
|
| 59 |
/* Pacify gcc -Wanalyzer-null-dereference in areas where GCC |
| 60 |
understandably cannot deduce that the input comes from a |
| 61 |
well-formed regular expression. There's little point to the |
| 62 |
runtime overhead of 'assert' instead of 'assume_nonnull' when the |
| 63 |
MMU will check anyway. */ |
| 64 |
#define assume_nonnull(x) assume ((x) != NULL) |
| 65 |
|
| 66 |
static bool |
| 67 |
str_eq (char const *a, char const *b) |
| 68 |
{ |
| 69 |
return strcmp (a, b) == 0; |
| 70 |
} |
| 71 |
|
| 72 |
static bool |
| 73 |
c_isdigit (char c) |
| 74 |
{ |
| 75 |
return '0' <= c && c <= '9'; |
| 76 |
} |
| 77 |
|
| 78 |
#ifndef FALLTHROUGH |
| 79 |
# if 201710L < __STDC_VERSION__ |
| 80 |
# define FALLTHROUGH [[__fallthrough__]] |
| 81 |
# elif ((__GNUC__ >= 7 && !defined __clang__) \ |
| 82 |
|| (defined __apple_build_version__ \ |
| 83 |
? __apple_build_version__ >= 12000000 \ |
| 84 |
: __clang_major__ >= 10)) |
| 85 |
# define FALLTHROUGH __attribute__ ((__fallthrough__)) |
| 86 |
# else |
| 87 |
# define FALLTHROUGH ((void) 0) |
| 88 |
# endif |
| 89 |
#endif |
| 90 |
|
| 91 |
#ifndef MIN |
| 92 |
# define MIN(a,b) ((a) < (b) ? (a) : (b)) |
| 93 |
#endif |
| 94 |
|
| 95 |
/* HPUX defines these as macros in sys/param.h. */ |
| 96 |
#ifdef setbit |
| 97 |
# undef setbit |
| 98 |
#endif |
| 99 |
#ifdef clrbit |
| 100 |
# undef clrbit |
| 101 |
#endif |
| 102 |
|
| 103 |
/* For code that does not use Gnulib’s isblank module. */ |
| 104 |
#if !defined isblank && !defined HAVE_ISBLANK && !defined GNULIB_ISBLANK |
| 105 |
# define isblank dfa_isblank |
| 106 |
static int |
| 107 |
isblank (int c) |
| 108 |
{ |
| 109 |
return c == ' ' || c == '\t'; |
| 110 |
} |
| 111 |
#endif |
| 112 |
|
| 113 |
/* First integer value that is greater than any character code. */ |
| 114 |
enum { NOTCHAR = 1 << CHAR_BIT }; |
| 115 |
|
| 116 |
#ifdef UINT_LEAST64_MAX |
| 117 |
|
| 118 |
/* Number of bits used in a charclass word. */ |
| 119 |
enum { CHARCLASS_WORD_BITS = 64 }; |
| 120 |
|
| 121 |
/* This represents part of a character class. It must be unsigned and |
| 122 |
at least CHARCLASS_WORD_BITS wide. Any excess bits are zero. */ |
| 123 |
typedef uint_least64_t charclass_word; |
| 124 |
|
| 125 |
/* Part of a charclass initializer that represents 64 bits' worth of a |
| 126 |
charclass, where LO and HI are the low and high-order 32 bits of |
| 127 |
the 64-bit quantity. */ |
| 128 |
# define CHARCLASS_PAIR(lo, hi) (((charclass_word) (hi) << 32) + (lo)) |
| 129 |
|
| 130 |
#else |
| 131 |
/* Fallbacks for pre-C99 hosts that lack 64-bit integers. */ |
| 132 |
enum { CHARCLASS_WORD_BITS = 32 }; |
| 133 |
typedef unsigned long charclass_word; |
| 134 |
# define CHARCLASS_PAIR(lo, hi) lo, hi |
| 135 |
#endif |
| 136 |
|
| 137 |
/* An initializer for a charclass whose 32-bit words are A through H. */ |
| 138 |
#define CHARCLASS_INIT(a, b, c, d, e, f, g, h) \ |
| 139 |
{{ \ |
| 140 |
CHARCLASS_PAIR (a, b), CHARCLASS_PAIR (c, d), \ |
| 141 |
CHARCLASS_PAIR (e, f), CHARCLASS_PAIR (g, h) \ |
| 142 |
}} |
| 143 |
|
| 144 |
/* The maximum useful value of a charclass_word; all used bits are 1. */ |
| 145 |
static charclass_word const CHARCLASS_WORD_MASK |
| 146 |
= ((charclass_word) 1 << (CHARCLASS_WORD_BITS - 1) << 1) - 1; |
| 147 |
|
| 148 |
/* Number of words required to hold a bit for every character. */ |
| 149 |
enum |
| 150 |
{ |
| 151 |
CHARCLASS_WORDS = (NOTCHAR + CHARCLASS_WORD_BITS - 1) / CHARCLASS_WORD_BITS |
| 152 |
}; |
| 153 |
|
| 154 |
/* Sets of unsigned characters are stored as bit vectors in arrays of ints. */ |
| 155 |
typedef struct { charclass_word w[CHARCLASS_WORDS]; } charclass; |
| 156 |
|
| 157 |
/* Convert a possibly-signed character to an unsigned character. This is |
| 158 |
a bit safer than casting to unsigned char, since it catches some type |
| 159 |
errors that the cast doesn't. */ |
| 160 |
static unsigned char |
| 161 |
to_uchar (char ch) |
| 162 |
{ |
| 163 |
return ch; |
| 164 |
} |
| 165 |
|
| 166 |
/* Contexts tell us whether a character is a newline or a word constituent. |
| 167 |
Word-constituent characters are those that satisfy iswalnum, plus '_'. |
| 168 |
Each character has a single CTX_* value; bitmasks of CTX_* values denote |
| 169 |
a particular character class. |
| 170 |
|
| 171 |
A state also stores a context value, which is a bitmask of CTX_* values. |
| 172 |
A state's context represents a set of characters that the state's |
| 173 |
predecessors must match. For example, a state whose context does not |
| 174 |
include CTX_LETTER will never have transitions where the previous |
| 175 |
character is a word constituent. A state whose context is CTX_ANY |
| 176 |
might have transitions from any character. */ |
| 177 |
|
| 178 |
enum |
| 179 |
{ |
| 180 |
CTX_NONE = 1, |
| 181 |
CTX_LETTER = 2, |
| 182 |
CTX_NEWLINE = 4, |
| 183 |
CTX_ANY = 7 |
| 184 |
}; |
| 185 |
|
| 186 |
/* Sometimes characters can only be matched depending on the surrounding |
| 187 |
context. Such context decisions depend on what the previous character |
| 188 |
was, and the value of the current (lookahead) character. Context |
| 189 |
dependent constraints are encoded as 9-bit integers. Each bit that |
| 190 |
is set indicates that the constraint succeeds in the corresponding |
| 191 |
context. |
| 192 |
|
| 193 |
bit 6-8 - valid contexts when next character is CTX_NEWLINE |
| 194 |
bit 3-5 - valid contexts when next character is CTX_LETTER |
| 195 |
bit 0-2 - valid contexts when next character is CTX_NONE |
| 196 |
|
| 197 |
succeeds_in_context determines whether a given constraint |
| 198 |
succeeds in a particular context. Prev is a bitmask of possible |
| 199 |
context values for the previous character, curr is the (single-bit) |
| 200 |
context value for the lookahead character. */ |
| 201 |
static int |
| 202 |
newline_constraint (int constraint) |
| 203 |
{ |
| 204 |
return (constraint >> 6) & 7; |
| 205 |
} |
| 206 |
static int |
| 207 |
letter_constraint (int constraint) |
| 208 |
{ |
| 209 |
return (constraint >> 3) & 7; |
| 210 |
} |
| 211 |
static int |
| 212 |
other_constraint (int constraint) |
| 213 |
{ |
| 214 |
return constraint & 7; |
| 215 |
} |
| 216 |
|
| 217 |
static bool |
| 218 |
succeeds_in_context (int constraint, int prev, int curr) |
| 219 |
{ |
| 220 |
return !! (((curr & CTX_NONE ? other_constraint (constraint) : 0) \ |
| 221 |
| (curr & CTX_LETTER ? letter_constraint (constraint) : 0) \ |
| 222 |
| (curr & CTX_NEWLINE ? newline_constraint (constraint) : 0)) \ |
| 223 |
& prev); |
| 224 |
} |
| 225 |
|
| 226 |
/* The following describe what a constraint depends on. */ |
| 227 |
static bool |
| 228 |
prev_newline_dependent (int constraint) |
| 229 |
{ |
| 230 |
return ((constraint ^ constraint >> 2) & 0111) != 0; |
| 231 |
} |
| 232 |
static bool |
| 233 |
prev_letter_dependent (int constraint) |
| 234 |
{ |
| 235 |
return ((constraint ^ constraint >> 1) & 0111) != 0; |
| 236 |
} |
| 237 |
|
| 238 |
/* Tokens that match the empty string subject to some constraint actually |
| 239 |
work by applying that constraint to determine what may follow them, |
| 240 |
taking into account what has gone before. The following values are |
| 241 |
the constraints corresponding to the special tokens previously defined. */ |
| 242 |
enum |
| 243 |
{ |
| 244 |
NO_CONSTRAINT = 0777, |
| 245 |
BEGLINE_CONSTRAINT = 0444, |
| 246 |
ENDLINE_CONSTRAINT = 0700, |
| 247 |
BEGWORD_CONSTRAINT = 0050, |
| 248 |
ENDWORD_CONSTRAINT = 0202, |
| 249 |
LIMWORD_CONSTRAINT = 0252, |
| 250 |
NOTLIMWORD_CONSTRAINT = 0525 |
| 251 |
}; |
| 252 |
|
| 253 |
/* The regexp is parsed into an array of tokens in postfix form. Some tokens |
| 254 |
are operators and others are terminal symbols. Most (but not all) of these |
| 255 |
codes are returned by the lexical analyzer. */ |
| 256 |
|
| 257 |
typedef ptrdiff_t token; |
| 258 |
static token const TOKEN_MAX = PTRDIFF_MAX; |
| 259 |
|
| 260 |
/* States are indexed by state_num values. These are normally |
| 261 |
nonnegative but -1 is used as a special value. */ |
| 262 |
typedef ptrdiff_t state_num; |
| 263 |
|
| 264 |
/* Predefined token values. */ |
| 265 |
enum |
| 266 |
{ |
| 267 |
END = -1, /* END is a terminal symbol that matches the |
| 268 |
end of input; any value of END or less in |
| 269 |
the parse tree is such a symbol. Accepting |
| 270 |
states of the DFA are those that would have |
| 271 |
a transition on END. This is -1, not some |
| 272 |
more-negative value, to tweak the speed of |
| 273 |
comparisons to END. */ |
| 274 |
|
| 275 |
/* Ordinary character values are terminal symbols that match themselves. */ |
| 276 |
|
| 277 |
/* CSET must come last in the following list of special tokens. Otherwise, |
| 278 |
the list order matters only for performance. Related special tokens |
| 279 |
should have nearby values so that code like (t == ANYCHAR || t == MBCSET |
| 280 |
|| CSET <= t) can be done with a single machine-level comparison. */ |
| 281 |
|
| 282 |
EMPTY = NOTCHAR, /* EMPTY is a terminal symbol that matches |
| 283 |
the empty string. */ |
| 284 |
|
| 285 |
QMARK, /* QMARK is an operator of one argument that |
| 286 |
matches zero or one occurrences of its |
| 287 |
argument. */ |
| 288 |
|
| 289 |
STAR, /* STAR is an operator of one argument that |
| 290 |
matches the Kleene closure (zero or more |
| 291 |
occurrences) of its argument. */ |
| 292 |
|
| 293 |
PLUS, /* PLUS is an operator of one argument that |
| 294 |
matches the positive closure (one or more |
| 295 |
occurrences) of its argument. */ |
| 296 |
|
| 297 |
REPMN, /* REPMN is a lexical token corresponding |
| 298 |
to the {m,n} construct. REPMN never |
| 299 |
appears in the compiled token vector. */ |
| 300 |
|
| 301 |
CAT, /* CAT is an operator of two arguments that |
| 302 |
matches the concatenation of its |
| 303 |
arguments. CAT is never returned by the |
| 304 |
lexical analyzer. */ |
| 305 |
|
| 306 |
OR, /* OR is an operator of two arguments that |
| 307 |
matches either of its arguments. */ |
| 308 |
|
| 309 |
LPAREN, /* LPAREN never appears in the parse tree, |
| 310 |
it is only a lexeme. */ |
| 311 |
|
| 312 |
RPAREN, /* RPAREN never appears in the parse tree. */ |
| 313 |
|
| 314 |
WCHAR, /* Only returned by lex. wctok contains the |
| 315 |
32-bit wide character representation. */ |
| 316 |
|
| 317 |
ANYCHAR, /* ANYCHAR is a terminal symbol that matches |
| 318 |
a valid multibyte (or single byte) character. |
| 319 |
It is used only if MB_CUR_MAX > 1. */ |
| 320 |
|
| 321 |
BEG, /* BEG is an initial symbol that matches the |
| 322 |
beginning of input. */ |
| 323 |
|
| 324 |
BEGLINE, /* BEGLINE is a terminal symbol that matches |
| 325 |
the empty string at the beginning of a |
| 326 |
line. */ |
| 327 |
|
| 328 |
ENDLINE, /* ENDLINE is a terminal symbol that matches |
| 329 |
the empty string at the end of a line. */ |
| 330 |
|
| 331 |
BEGWORD, /* BEGWORD is a terminal symbol that matches |
| 332 |
the empty string at the beginning of a |
| 333 |
word. */ |
| 334 |
|
| 335 |
ENDWORD, /* ENDWORD is a terminal symbol that matches |
| 336 |
the empty string at the end of a word. */ |
| 337 |
|
| 338 |
LIMWORD, /* LIMWORD is a terminal symbol that matches |
| 339 |
the empty string at the beginning or the |
| 340 |
end of a word. */ |
| 341 |
|
| 342 |
NOTLIMWORD, /* NOTLIMWORD is a terminal symbol that |
| 343 |
matches the empty string not at |
| 344 |
the beginning or end of a word. */ |
| 345 |
|
| 346 |
BACKREF, /* BACKREF is generated by \<digit> |
| 347 |
or by any other construct that |
| 348 |
is not completely handled. If the scanner |
| 349 |
detects a transition on backref, it returns |
| 350 |
a kind of "semi-success" indicating that |
| 351 |
the match will have to be verified with |
| 352 |
a backtracking matcher. */ |
| 353 |
|
| 354 |
MBCSET, /* MBCSET is similar to CSET, but for |
| 355 |
multibyte characters. */ |
| 356 |
|
| 357 |
CSET /* CSET and (and any value greater) is a |
| 358 |
terminal symbol that matches any of a |
| 359 |
class of characters. */ |
| 360 |
}; |
| 361 |
|
| 362 |
|
| 363 |
/* States of the recognizer correspond to sets of positions in the parse |
| 364 |
tree, together with the constraints under which they may be matched. |
| 365 |
So a position is encoded as an index into the parse tree together with |
| 366 |
a constraint. */ |
| 367 |
typedef struct |
| 368 |
{ |
| 369 |
idx_t index; /* Index into the parse array. */ |
| 370 |
unsigned int constraint; /* Constraint for matching this position. */ |
| 371 |
} position; |
| 372 |
|
| 373 |
/* Sets of positions are stored as arrays. */ |
| 374 |
typedef struct |
| 375 |
{ |
| 376 |
position *elems; /* Elements of this position set. */ |
| 377 |
idx_t nelem; /* Number of elements in this set. */ |
| 378 |
idx_t alloc; /* Number of elements allocated in ELEMS. */ |
| 379 |
} position_set; |
| 380 |
|
| 381 |
/* A state of the dfa consists of a set of positions, some flags, |
| 382 |
and the token value of the lowest-numbered position of the state that |
| 383 |
contains an END token. */ |
| 384 |
typedef struct |
| 385 |
{ |
| 386 |
size_t hash; /* Hash of the positions of this state. */ |
| 387 |
position_set elems; /* Positions this state could match. */ |
| 388 |
unsigned char context; /* Context from previous state. */ |
| 389 |
unsigned short constraint; /* Constraint for this state to accept. */ |
| 390 |
position_set mbps; /* Positions which can match multibyte |
| 391 |
characters or the follows, e.g., period. |
| 392 |
Used only if MB_CUR_MAX > 1. */ |
| 393 |
state_num mb_trindex; /* Index of this state in MB_TRANS, or |
| 394 |
negative if the state does not have |
| 395 |
ANYCHAR. */ |
| 396 |
} dfa_state; |
| 397 |
|
| 398 |
/* Maximum for any transition table count. This should be at least 3, |
| 399 |
for the initial state setup. */ |
| 400 |
enum { MAX_TRCOUNT = 1024 }; |
| 401 |
|
| 402 |
/* A bracket operator. |
| 403 |
e.g., [a-c], [[:alpha:]], etc. */ |
| 404 |
struct mb_char_classes |
| 405 |
{ |
| 406 |
ptrdiff_t cset; |
| 407 |
bool invert; |
| 408 |
char32_t *chars; /* Normal characters. */ |
| 409 |
idx_t nchars; |
| 410 |
idx_t nchars_alloc; |
| 411 |
}; |
| 412 |
|
| 413 |
struct regex_syntax |
| 414 |
{ |
| 415 |
/* Syntax bits controlling the behavior of the lexical analyzer. */ |
| 416 |
reg_syntax_t syntax_bits; |
| 417 |
int dfaopts; |
| 418 |
bool syntax_bits_set; |
| 419 |
|
| 420 |
/* Flag for case-folding letters into sets. */ |
| 421 |
bool case_fold; |
| 422 |
|
| 423 |
/* End-of-line byte in data. */ |
| 424 |
unsigned char eolbyte; |
| 425 |
|
| 426 |
/* Cache of char-context values. */ |
| 427 |
char sbit[NOTCHAR]; |
| 428 |
|
| 429 |
/* If never_trail[B], the byte B cannot be a non-initial byte in a |
| 430 |
multibyte character. */ |
| 431 |
bool never_trail[NOTCHAR]; |
| 432 |
|
| 433 |
/* Set of characters considered letters. */ |
| 434 |
charclass letters; |
| 435 |
|
| 436 |
/* Set of characters that are newline. */ |
| 437 |
charclass newline; |
| 438 |
}; |
| 439 |
|
| 440 |
/* Lexical analyzer. All the dross that deals with the obnoxious |
| 441 |
GNU Regex syntax bits is located here. The poor, suffering |
| 442 |
reader is referred to the GNU Regex documentation for the |
| 443 |
meaning of the @#%!@#%^!@ syntax bits. */ |
| 444 |
struct lexer_state |
| 445 |
{ |
| 446 |
char const *ptr; /* Pointer to next input character. */ |
| 447 |
idx_t left; /* Number of characters remaining. */ |
| 448 |
token lasttok; /* Previous token returned; initially END. */ |
| 449 |
idx_t parens; /* Count of outstanding left parens. */ |
| 450 |
int minrep, maxrep; /* Repeat counts for {m,n}. */ |
| 451 |
|
| 452 |
/* 32-bit wide character representation of the current multibyte character, |
| 453 |
or WEOF if there was an encoding error. Used only if |
| 454 |
MB_CUR_MAX > 1. */ |
| 455 |
wint_t wctok; |
| 456 |
|
| 457 |
/* The most recently analyzed multibyte bracket expression. */ |
| 458 |
struct mb_char_classes brack; |
| 459 |
|
| 460 |
/* We're separated from beginning or (, | only by zero-width characters. */ |
| 461 |
bool laststart; |
| 462 |
}; |
| 463 |
|
| 464 |
/* Recursive descent parser for regular expressions. */ |
| 465 |
|
| 466 |
struct parser_state |
| 467 |
{ |
| 468 |
token tok; /* Lookahead token. */ |
| 469 |
idx_t depth; /* Current depth of a hypothetical stack |
| 470 |
holding deferred productions. This is |
| 471 |
used to determine the depth that will be |
| 472 |
required of the real stack later on in |
| 473 |
dfaanalyze. */ |
| 474 |
}; |
| 475 |
|
| 476 |
/* A compiled regular expression. */ |
| 477 |
struct dfa |
| 478 |
{ |
| 479 |
/* Fields filled by the scanner. */ |
| 480 |
charclass *charclasses; /* Array of character sets for CSET tokens. */ |
| 481 |
idx_t cindex; /* Index for adding new charclasses. */ |
| 482 |
idx_t calloc; /* Number of charclasses allocated. */ |
| 483 |
ptrdiff_t canychar; /* Index of anychar class, or -1. */ |
| 484 |
|
| 485 |
/* Scanner state */ |
| 486 |
struct lexer_state lex; |
| 487 |
|
| 488 |
/* Parser state */ |
| 489 |
struct parser_state parse; |
| 490 |
|
| 491 |
/* Fields filled by the parser. */ |
| 492 |
token *tokens; /* Postfix parse array. */ |
| 493 |
idx_t tindex; /* Index for adding new tokens. */ |
| 494 |
idx_t talloc; /* Number of tokens currently allocated. */ |
| 495 |
idx_t depth; /* Depth required of an evaluation stack |
| 496 |
used for depth-first traversal of the |
| 497 |
parse tree. */ |
| 498 |
idx_t nleaves; /* Number of non-EMPTY leaves |
| 499 |
in the parse tree. */ |
| 500 |
idx_t nregexps; /* Count of parallel regexps being built |
| 501 |
with dfaparse. */ |
| 502 |
bool fast; /* The DFA is fast. */ |
| 503 |
bool epsilon; /* Does a token match only the empty string? */ |
| 504 |
token utf8_anychar_classes[9]; /* To lower ANYCHAR in UTF-8 locales. */ |
| 505 |
mbstate_t mbs; /* Multibyte conversion state. */ |
| 506 |
|
| 507 |
/* The following are valid only if MB_CUR_MAX > 1. */ |
| 508 |
|
| 509 |
/* The value of multibyte_prop[i] is defined by following rule. |
| 510 |
if tokens[i] < NOTCHAR |
| 511 |
bit 0 : tokens[i] is the first byte of a character, including |
| 512 |
single-byte characters. |
| 513 |
bit 1 : tokens[i] is the last byte of a character, including |
| 514 |
single-byte characters. |
| 515 |
|
| 516 |
e.g. |
| 517 |
tokens |
| 518 |
= 'single_byte_a', 'multi_byte_A', single_byte_b' |
| 519 |
= 'sb_a', 'mb_A(1st byte)', 'mb_A(2nd byte)', 'mb_A(3rd byte)', 'sb_b' |
| 520 |
multibyte_prop |
| 521 |
= 3 , 1 , 0 , 2 , 3 |
| 522 |
*/ |
| 523 |
char *multibyte_prop; |
| 524 |
|
| 525 |
/* Fields filled by the superset. */ |
| 526 |
struct dfa *superset; /* Hint of the dfa. */ |
| 527 |
|
| 528 |
/* Fields filled by the state builder. */ |
| 529 |
dfa_state *states; /* States of the dfa. */ |
| 530 |
state_num sindex; /* Index for adding new states. */ |
| 531 |
idx_t salloc; /* Number of states currently allocated. */ |
| 532 |
|
| 533 |
/* Fields filled by the parse tree->NFA conversion. */ |
| 534 |
position_set *follows; /* Array of follow sets, indexed by position |
| 535 |
index. The follow of a position is the set |
| 536 |
of positions containing characters that |
| 537 |
could conceivably follow a character |
| 538 |
matching the given position in a string |
| 539 |
matching the regexp. Allocated to the |
| 540 |
maximum possible position index. */ |
| 541 |
bool searchflag; /* We are supposed to build a searching |
| 542 |
as opposed to an exact matcher. A searching |
| 543 |
matcher finds the first and shortest string |
| 544 |
matching a regexp anywhere in the buffer, |
| 545 |
whereas an exact matcher finds the longest |
| 546 |
string matching, but anchored to the |
| 547 |
beginning of the buffer. */ |
| 548 |
|
| 549 |
/* Fields filled by dfaanalyze. */ |
| 550 |
int *constraints; /* Array of union of accepting constraints |
| 551 |
in the follow of a position. */ |
| 552 |
int *separates; /* Array of contexts on follow of a |
| 553 |
position. */ |
| 554 |
|
| 555 |
/* Fields filled by dfaexec. */ |
| 556 |
state_num tralloc; /* Number of transition tables that have |
| 557 |
slots so far, not counting trans[-1] and |
| 558 |
trans[-2]. */ |
| 559 |
int trcount; /* Number of transition tables that have |
| 560 |
been built, other than for initial |
| 561 |
states. */ |
| 562 |
int min_trcount; /* Number of initial states. Equivalently, |
| 563 |
the minimum state number for which trcount |
| 564 |
counts transitions. */ |
| 565 |
state_num **trans; /* Transition tables for states that can |
| 566 |
never accept. If the transitions for a |
| 567 |
state have not yet been computed, or the |
| 568 |
state could possibly accept, its entry in |
| 569 |
this table is NULL. This points to two |
| 570 |
past the start of the allocated array, |
| 571 |
and trans[-1] and trans[-2] are always |
| 572 |
NULL. */ |
| 573 |
state_num **fails; /* Transition tables after failing to accept |
| 574 |
on a state that potentially could do so. |
| 575 |
If trans[i] is non-null, fails[i] must |
| 576 |
be null. */ |
| 577 |
char *success; /* Table of acceptance conditions used in |
| 578 |
dfaexec and computed in build_state. */ |
| 579 |
state_num *newlines; /* Transitions on newlines. The entry for a |
| 580 |
newline in any transition table is always |
| 581 |
-1 so we can count lines without wasting |
| 582 |
too many cycles. The transition for a |
| 583 |
newline is stored separately and handled |
| 584 |
as a special case. Newline is also used |
| 585 |
as a sentinel at the end of the buffer. */ |
| 586 |
state_num initstate_notbol; /* Initial state for CTX_LETTER and CTX_NONE |
| 587 |
context in multibyte locales, in which we |
| 588 |
do not distinguish between their contexts, |
| 589 |
as not supported word. */ |
| 590 |
position_set mb_follows; /* Follow set added by ANYCHAR on demand. */ |
| 591 |
state_num **mb_trans; /* Transition tables for states with |
| 592 |
ANYCHAR. */ |
| 593 |
state_num mb_trcount; /* Number of transition tables for states with |
| 594 |
ANYCHAR that have actually been built. */ |
| 595 |
|
| 596 |
/* Syntax configuration. This is near the end so that dfacopysyntax |
| 597 |
can memset up to here. */ |
| 598 |
struct regex_syntax syntax; |
| 599 |
|
| 600 |
/* Information derived from the locale. This is at the end so that |
| 601 |
a quick memset need not clear it specially. */ |
| 602 |
|
| 603 |
/* dfaexec implementation. */ |
| 604 |
char *(*dfaexec) (struct dfa *, char const *, char *, |
| 605 |
bool, idx_t *, bool *); |
| 606 |
|
| 607 |
/* Other cached information derived from the locale. */ |
| 608 |
struct localeinfo localeinfo; |
| 609 |
}; |
| 610 |
|
| 611 |
/* User access to dfa internals. */ |
| 612 |
|
| 613 |
/* S could possibly be an accepting state of R. */ |
| 614 |
static bool |
| 615 |
accepting (state_num s, struct dfa const *r) |
| 616 |
{ |
| 617 |
return r->states[s].constraint != 0; |
| 618 |
} |
| 619 |
|
| 620 |
/* STATE accepts in the specified context. */ |
| 621 |
static bool |
| 622 |
accepts_in_context (int prev, int curr, state_num state, struct dfa const *dfa) |
| 623 |
{ |
| 624 |
return succeeds_in_context (dfa->states[state].constraint, prev, curr); |
| 625 |
} |
| 626 |
|
| 627 |
static void regexp (struct dfa *dfa); |
| 628 |
|
| 629 |
/* Store into *PWC the result of converting the leading bytes of the |
| 630 |
multibyte buffer S of length N bytes, using D->localeinfo.sbctowc |
| 631 |
and updating the conversion state in *D. On conversion error, |
| 632 |
convert just a single byte, to WEOF. Return the number of bytes |
| 633 |
converted. |
| 634 |
|
| 635 |
This differs from mbrtoc32 (PWC, S, N, &D->mbs) as follows: |
| 636 |
|
| 637 |
* PWC points to wint_t, not to char32_t. |
| 638 |
* The last arg is a dfa *D instead of merely a multibyte conversion |
| 639 |
state D->mbs. |
| 640 |
* N is idx_t not size_t, and must be at least 1. |
| 641 |
* S[N - 1] must be a sentinel byte. |
| 642 |
* Shift encodings are not supported. |
| 643 |
* The return value is always in the range 1..N. |
| 644 |
* D->mbs is always valid afterwards. |
| 645 |
* *PWC is always set to something. */ |
| 646 |
static int |
| 647 |
mbs_to_wchar (wint_t *pwc, char const *s, idx_t n, struct dfa *d) |
| 648 |
{ |
| 649 |
unsigned char uc = s[0]; |
| 650 |
wint_t wc = d->localeinfo.sbctowc[uc]; |
| 651 |
|
| 652 |
if (wc == WEOF) |
| 653 |
{ |
| 654 |
char32_t wch; |
| 655 |
size_t nbytes = mbrtoc32 (&wch, s, n, &d->mbs); |
| 656 |
if (0 < nbytes && nbytes < (size_t) -2) |
| 657 |
{ |
| 658 |
*pwc = wch; |
| 659 |
/* nbytes cannot be == (size) -3 here, since we rely on the |
| 660 |
'mbrtoc32-regular' module. */ |
| 661 |
return nbytes; |
| 662 |
} |
| 663 |
mbszero (&d->mbs); |
| 664 |
} |
| 665 |
|
| 666 |
*pwc = wc; |
| 667 |
return 1; |
| 668 |
} |
| 669 |
|
| 670 |
#ifdef DEBUG |
| 671 |
|
| 672 |
static void |
| 673 |
prtok (token t) |
| 674 |
{ |
| 675 |
if (t <= END) |
| 676 |
fprintf (stderr, "END"); |
| 677 |
else if (0 <= t && t < NOTCHAR) |
| 678 |
{ |
| 679 |
unsigned int ch = t; |
| 680 |
fprintf (stderr, "0x%02x", ch); |
| 681 |
} |
| 682 |
else |
| 683 |
{ |
| 684 |
char const *s; |
| 685 |
switch (t) |
| 686 |
{ |
| 687 |
case BEG: |
| 688 |
s = "BEG"; |
| 689 |
break; |
| 690 |
case EMPTY: |
| 691 |
s = "EMPTY"; |
| 692 |
break; |
| 693 |
case BACKREF: |
| 694 |
s = "BACKREF"; |
| 695 |
break; |
| 696 |
case BEGLINE: |
| 697 |
s = "BEGLINE"; |
| 698 |
break; |
| 699 |
case ENDLINE: |
| 700 |
s = "ENDLINE"; |
| 701 |
break; |
| 702 |
case BEGWORD: |
| 703 |
s = "BEGWORD"; |
| 704 |
break; |
| 705 |
case ENDWORD: |
| 706 |
s = "ENDWORD"; |
| 707 |
break; |
| 708 |
case LIMWORD: |
| 709 |
s = "LIMWORD"; |
| 710 |
break; |
| 711 |
case NOTLIMWORD: |
| 712 |
s = "NOTLIMWORD"; |
| 713 |
break; |
| 714 |
case QMARK: |
| 715 |
s = "QMARK"; |
| 716 |
break; |
| 717 |
case STAR: |
| 718 |
s = "STAR"; |
| 719 |
break; |
| 720 |
case PLUS: |
| 721 |
s = "PLUS"; |
| 722 |
break; |
| 723 |
case CAT: |
| 724 |
s = "CAT"; |
| 725 |
break; |
| 726 |
case OR: |
| 727 |
s = "OR"; |
| 728 |
break; |
| 729 |
case LPAREN: |
| 730 |
s = "LPAREN"; |
| 731 |
break; |
| 732 |
case RPAREN: |
| 733 |
s = "RPAREN"; |
| 734 |
break; |
| 735 |
case ANYCHAR: |
| 736 |
s = "ANYCHAR"; |
| 737 |
break; |
| 738 |
case MBCSET: |
| 739 |
s = "MBCSET"; |
| 740 |
break; |
| 741 |
default: |
| 742 |
s = "CSET"; |
| 743 |
break; |
| 744 |
} |
| 745 |
fprintf (stderr, "%s", s); |
| 746 |
} |
| 747 |
} |
| 748 |
#endif /* DEBUG */ |
| 749 |
|
| 750 |
/* Stuff pertaining to charclasses. */ |
| 751 |
|
| 752 |
static bool |
| 753 |
tstbit (unsigned int b, charclass const *c) |
| 754 |
{ |
| 755 |
return c->w[b / CHARCLASS_WORD_BITS] >> b % CHARCLASS_WORD_BITS & 1; |
| 756 |
} |
| 757 |
|
| 758 |
static void |
| 759 |
setbit (unsigned int b, charclass *c) |
| 760 |
{ |
| 761 |
charclass_word one = 1; |
| 762 |
c->w[b / CHARCLASS_WORD_BITS] |= one << b % CHARCLASS_WORD_BITS; |
| 763 |
} |
| 764 |
|
| 765 |
static void |
| 766 |
clrbit (unsigned int b, charclass *c) |
| 767 |
{ |
| 768 |
charclass_word one = 1; |
| 769 |
c->w[b / CHARCLASS_WORD_BITS] &= ~(one << b % CHARCLASS_WORD_BITS); |
| 770 |
} |
| 771 |
|
| 772 |
static void |
| 773 |
zeroset (charclass *s) |
| 774 |
{ |
| 775 |
memset (s, 0, sizeof *s); |
| 776 |
} |
| 777 |
|
| 778 |
static void |
| 779 |
fillset (charclass *s) |
| 780 |
{ |
| 781 |
for (int i = 0; i < CHARCLASS_WORDS; i++) |
| 782 |
s->w[i] = CHARCLASS_WORD_MASK; |
| 783 |
} |
| 784 |
|
| 785 |
static void |
| 786 |
notset (charclass *s) |
| 787 |
{ |
| 788 |
for (int i = 0; i < CHARCLASS_WORDS; ++i) |
| 789 |
s->w[i] = CHARCLASS_WORD_MASK & ~s->w[i]; |
| 790 |
} |
| 791 |
|
| 792 |
static bool |
| 793 |
equal (charclass const *s1, charclass const *s2) |
| 794 |
{ |
| 795 |
charclass_word w = 0; |
| 796 |
for (int i = 0; i < CHARCLASS_WORDS; i++) |
| 797 |
w |= s1->w[i] ^ s2->w[i]; |
| 798 |
return w == 0; |
| 799 |
} |
| 800 |
|
| 801 |
static bool |
| 802 |
emptyset (charclass const *s) |
| 803 |
{ |
| 804 |
charclass_word w = 0; |
| 805 |
for (int i = 0; i < CHARCLASS_WORDS; i++) |
| 806 |
w |= s->w[i]; |
| 807 |
return w == 0; |
| 808 |
} |
| 809 |
|
| 810 |
/* Ensure that the array addressed by PA holds at least I + 1 items. |
| 811 |
Either return PA, or reallocate the array and return its new address. |
| 812 |
Although PA may be null, the returned value is never null. |
| 813 |
|
| 814 |
The array holds *NITEMS items, where 0 <= I <= *NITEMS; *NITEMS |
| 815 |
is updated on reallocation. If PA is null, *NITEMS must be zero. |
| 816 |
Do not allocate more than NITEMS_MAX items total; -1 means no limit. |
| 817 |
ITEM_SIZE is the size of one item; it must be positive. |
| 818 |
Avoid O(N**2) behavior on arrays growing linearly. */ |
| 819 |
static void * |
| 820 |
maybe_realloc (void *pa, idx_t i, idx_t *nitems, |
| 821 |
ptrdiff_t nitems_max, idx_t item_size) |
| 822 |
{ |
| 823 |
if (i < *nitems) |
| 824 |
return pa; |
| 825 |
return xpalloc (pa, nitems, 1, nitems_max, item_size); |
| 826 |
} |
| 827 |
|
| 828 |
/* In DFA D, find the index of charclass S, or allocate a new one. */ |
| 829 |
static idx_t |
| 830 |
charclass_index (struct dfa *d, charclass const *s) |
| 831 |
{ |
| 832 |
idx_t i; |
| 833 |
|
| 834 |
for (i = 0; i < d->cindex; ++i) |
| 835 |
if (equal (s, &d->charclasses[i])) |
| 836 |
return i; |
| 837 |
d->charclasses = maybe_realloc (d->charclasses, d->cindex, &d->calloc, |
| 838 |
TOKEN_MAX - CSET, sizeof *d->charclasses); |
| 839 |
++d->cindex; |
| 840 |
d->charclasses[i] = *s; |
| 841 |
return i; |
| 842 |
} |
| 843 |
|
| 844 |
static bool |
| 845 |
unibyte_word_constituent (struct dfa const *dfa, unsigned char c) |
| 846 |
{ |
| 847 |
return dfa->localeinfo.sbctowc[c] != WEOF && (isalnum (c) || (c) == '_'); |
| 848 |
} |
| 849 |
|
| 850 |
static int |
| 851 |
char_context (struct dfa const *dfa, unsigned char c) |
| 852 |
{ |
| 853 |
if (c == dfa->syntax.eolbyte && !(dfa->syntax.dfaopts & DFA_ANCHOR)) |
| 854 |
return CTX_NEWLINE; |
| 855 |
if (unibyte_word_constituent (dfa, c)) |
| 856 |
return CTX_LETTER; |
| 857 |
return CTX_NONE; |
| 858 |
} |
| 859 |
|
| 860 |
/* Set a bit in the charclass for the given char32_t. Do nothing if WC |
| 861 |
is represented by a multi-byte sequence. Even for MB_CUR_MAX == 1, |
| 862 |
this may happen when folding case in weird Turkish locales where |
| 863 |
dotless i/dotted I are not included in the chosen character set. |
| 864 |
Return whether a bit was set in the charclass. */ |
| 865 |
static bool |
| 866 |
setbit_wc (char32_t wc, charclass *c) |
| 867 |
{ |
| 868 |
int b = c32tob (wc); |
| 869 |
if (b < 0) |
| 870 |
return false; |
| 871 |
|
| 872 |
setbit (b, c); |
| 873 |
return true; |
| 874 |
} |
| 875 |
|
| 876 |
/* Set a bit for B and its case variants in the charclass C. |
| 877 |
MB_CUR_MAX must be 1. */ |
| 878 |
static void |
| 879 |
setbit_case_fold_c (int b, charclass *c) |
| 880 |
{ |
| 881 |
int ub = toupper (b); |
| 882 |
for (int i = 0; i < NOTCHAR; i++) |
| 883 |
if (toupper (i) == ub) |
| 884 |
setbit (i, c); |
| 885 |
} |
| 886 |
|
| 887 |
/* Fetch the next lexical input character from the pattern. There |
| 888 |
must at least one byte of pattern input. Set DFA->lex.wctok to the |
| 889 |
value of the character or to WEOF depending on whether the input is |
| 890 |
a valid multibyte character (possibly of length 1). Then return |
| 891 |
the next input byte value, except return EOF if the input is a |
| 892 |
multibyte character of length greater than 1. */ |
| 893 |
static int |
| 894 |
fetch_wc (struct dfa *dfa) |
| 895 |
{ |
| 896 |
int nbytes = mbs_to_wchar (&dfa->lex.wctok, dfa->lex.ptr, dfa->lex.left, |
| 897 |
dfa); |
| 898 |
int c = nbytes == 1 ? to_uchar (dfa->lex.ptr[0]) : EOF; |
| 899 |
dfa->lex.ptr += nbytes; |
| 900 |
dfa->lex.left -= nbytes; |
| 901 |
return c; |
| 902 |
} |
| 903 |
|
| 904 |
/* If there is no more input, report an error about unbalanced brackets. |
| 905 |
Otherwise, behave as with fetch_wc (DFA). */ |
| 906 |
static int |
| 907 |
bracket_fetch_wc (struct dfa *dfa) |
| 908 |
{ |
| 909 |
if (! dfa->lex.left) |
| 910 |
dfaerror (_("unbalanced [")); |
| 911 |
return fetch_wc (dfa); |
| 912 |
} |
| 913 |
|
| 914 |
typedef int predicate (int); |
| 915 |
|
| 916 |
/* The following list maps the names of the Posix named character classes |
| 917 |
to predicate functions that determine whether a given character is in |
| 918 |
the class. The leading [ has already been eaten by the lexical |
| 919 |
analyzer. */ |
| 920 |
struct dfa_ctype |
| 921 |
{ |
| 922 |
const char *name; |
| 923 |
predicate *func; |
| 924 |
bool single_byte_only; |
| 925 |
}; |
| 926 |
|
| 927 |
static const struct dfa_ctype prednames[] = { |
| 928 |
{"alpha", isalpha, false}, |
| 929 |
{"upper", isupper, false}, |
| 930 |
{"lower", islower, false}, |
| 931 |
{"digit", isdigit, true}, |
| 932 |
{"xdigit", isxdigit, false}, |
| 933 |
{"space", isspace, false}, |
| 934 |
{"punct", ispunct, false}, |
| 935 |
{"alnum", isalnum, false}, |
| 936 |
{"print", isprint, false}, |
| 937 |
{"graph", isgraph, false}, |
| 938 |
{"cntrl", iscntrl, false}, |
| 939 |
{"blank", isblank, false}, |
| 940 |
{NULL, NULL, false} |
| 941 |
}; |
| 942 |
|
| 943 |
static const struct dfa_ctype *_GL_ATTRIBUTE_PURE |
| 944 |
find_pred (const char *str) |
| 945 |
{ |
| 946 |
for (int i = 0; prednames[i].name; i++) |
| 947 |
if (str_eq (str, prednames[i].name)) |
| 948 |
return &prednames[i]; |
| 949 |
return NULL; |
| 950 |
} |
| 951 |
|
| 952 |
/* Parse a bracket expression, which possibly includes multibyte |
| 953 |
characters. */ |
| 954 |
static token |
| 955 |
parse_bracket_exp (struct dfa *dfa) |
| 956 |
{ |
| 957 |
/* This is a bracket expression that dfaexec is known to |
| 958 |
process correctly. */ |
| 959 |
bool known_bracket_exp = true; |
| 960 |
|
| 961 |
/* Used to warn about [:space:]. |
| 962 |
Bit 0 = first character is a colon. |
| 963 |
Bit 1 = last character is a colon. |
| 964 |
Bit 2 = includes any other character but a colon. |
| 965 |
Bit 3 = includes ranges, char/equiv classes or collation elements. */ |
| 966 |
int colon_warning_state; |
| 967 |
|
| 968 |
dfa->lex.brack.nchars = 0; |
| 969 |
charclass ccl; |
| 970 |
zeroset (&ccl); |
| 971 |
int c = bracket_fetch_wc (dfa); |
| 972 |
bool invert = c == '^'; |
| 973 |
if (invert) |
| 974 |
{ |
| 975 |
c = bracket_fetch_wc (dfa); |
| 976 |
known_bracket_exp = dfa->localeinfo.simple; |
| 977 |
} |
| 978 |
wint_t wc = dfa->lex.wctok; |
| 979 |
int c1; |
| 980 |
wint_t wc1; |
| 981 |
colon_warning_state = (c == ':'); |
| 982 |
do |
| 983 |
{ |
| 984 |
c1 = NOTCHAR; /* Mark c1 as not initialized. */ |
| 985 |
colon_warning_state &= ~2; |
| 986 |
|
| 987 |
/* Note that if we're looking at some other [:...:] construct, |
| 988 |
we just treat it as a bunch of ordinary characters. We can do |
| 989 |
this because we assume regex has checked for syntax errors before |
| 990 |
dfa is ever called. */ |
| 991 |
if (c == '[') |
| 992 |
{ |
| 993 |
c1 = bracket_fetch_wc (dfa); |
| 994 |
wc1 = dfa->lex.wctok; |
| 995 |
|
| 996 |
if ((c1 == ':' && (dfa->syntax.syntax_bits & RE_CHAR_CLASSES)) |
| 997 |
|| c1 == '.' || c1 == '=') |
| 998 |
{ |
| 999 |
enum { MAX_BRACKET_STRING_LEN = 32 }; |
| 1000 |
char str[MAX_BRACKET_STRING_LEN + 1]; |
| 1001 |
int len = 0; |
| 1002 |
for (;;) |
| 1003 |
{ |
| 1004 |
c = bracket_fetch_wc (dfa); |
| 1005 |
if (dfa->lex.left == 0 |
| 1006 |
|| (c == c1 && dfa->lex.ptr[0] == ']')) |
| 1007 |
break; |
| 1008 |
if (len < MAX_BRACKET_STRING_LEN) |
| 1009 |
str[len++] = c; |
| 1010 |
else |
| 1011 |
/* This is in any case an invalid class name. */ |
| 1012 |
str[0] = '\0'; |
| 1013 |
} |
| 1014 |
str[len] = '\0'; |
| 1015 |
|
| 1016 |
/* Fetch bracket. */ |
| 1017 |
c = bracket_fetch_wc (dfa); |
| 1018 |
wc = dfa->lex.wctok; |
| 1019 |
if (c1 == ':') |
| 1020 |
/* Build character class. POSIX allows character |
| 1021 |
classes to match multicharacter collating elements, |
| 1022 |
but the regex code does not support that, so do not |
| 1023 |
worry about that possibility. */ |
| 1024 |
{ |
| 1025 |
char const *class |
| 1026 |
= (dfa->syntax.case_fold && (str_eq (str, "upper") |
| 1027 |
|| str_eq (str, "lower")) |
| 1028 |
? "alpha" : str); |
| 1029 |
const struct dfa_ctype *pred = find_pred (class); |
| 1030 |
if (!pred) |
| 1031 |
dfaerror (_("invalid character class")); |
| 1032 |
|
| 1033 |
if (dfa->localeinfo.multibyte && !pred->single_byte_only) |
| 1034 |
known_bracket_exp = false; |
| 1035 |
else |
| 1036 |
for (int c2 = 0; c2 < NOTCHAR; ++c2) |
| 1037 |
if (pred->func (c2)) |
| 1038 |
setbit (c2, &ccl); |
| 1039 |
} |
| 1040 |
else |
| 1041 |
known_bracket_exp = false; |
| 1042 |
|
| 1043 |
colon_warning_state |= 8; |
| 1044 |
|
| 1045 |
/* Fetch new lookahead character. */ |
| 1046 |
c1 = bracket_fetch_wc (dfa); |
| 1047 |
wc1 = dfa->lex.wctok; |
| 1048 |
continue; |
| 1049 |
} |
| 1050 |
|
| 1051 |
/* We treat '[' as a normal character here. c/c1/wc/wc1 |
| 1052 |
are already set up. */ |
| 1053 |
} |
| 1054 |
|
| 1055 |
if (c == '\\' |
| 1056 |
&& (dfa->syntax.syntax_bits & RE_BACKSLASH_ESCAPE_IN_LISTS)) |
| 1057 |
{ |
| 1058 |
c = bracket_fetch_wc (dfa); |
| 1059 |
wc = dfa->lex.wctok; |
| 1060 |
} |
| 1061 |
|
| 1062 |
if (c1 == NOTCHAR) |
| 1063 |
{ |
| 1064 |
c1 = bracket_fetch_wc (dfa); |
| 1065 |
wc1 = dfa->lex.wctok; |
| 1066 |
} |
| 1067 |
|
| 1068 |
if (c1 == '-') |
| 1069 |
/* build range characters. */ |
| 1070 |
{ |
| 1071 |
int c2 = bracket_fetch_wc (dfa); |
| 1072 |
wint_t wc2 = dfa->lex.wctok; |
| 1073 |
|
| 1074 |
/* A bracket expression like [a-[.aa.]] matches an unknown set. |
| 1075 |
Treat it like [-a[.aa.]] while parsing it, and |
| 1076 |
remember that the set is unknown. */ |
| 1077 |
if (c2 == '[' && dfa->lex.ptr[0] == '.') |
| 1078 |
{ |
| 1079 |
known_bracket_exp = false; |
| 1080 |
c2 = ']'; |
| 1081 |
} |
| 1082 |
|
| 1083 |
if (c2 == ']') |
| 1084 |
{ |
| 1085 |
/* In the case [x-], the - is an ordinary hyphen, |
| 1086 |
which is left in c1, the lookahead character. */ |
| 1087 |
dfa->lex.ptr--; |
| 1088 |
dfa->lex.left++; |
| 1089 |
} |
| 1090 |
else |
| 1091 |
{ |
| 1092 |
if (c2 == '\\' && (dfa->syntax.syntax_bits |
| 1093 |
& RE_BACKSLASH_ESCAPE_IN_LISTS)) |
| 1094 |
{ |
| 1095 |
c2 = bracket_fetch_wc (dfa); |
| 1096 |
wc2 = dfa->lex.wctok; |
| 1097 |
} |
| 1098 |
|
| 1099 |
colon_warning_state |= 8; |
| 1100 |
c1 = bracket_fetch_wc (dfa); |
| 1101 |
wc1 = dfa->lex.wctok; |
| 1102 |
|
| 1103 |
/* Treat [x-y] as a range if x != y. */ |
| 1104 |
if (wc != wc2 || wc == WEOF) |
| 1105 |
{ |
| 1106 |
if (dfa->localeinfo.simple |
| 1107 |
|| (c_isdigit (c) & c_isdigit (c2))) |
| 1108 |
{ |
| 1109 |
for (int ci = c; ci <= c2; ci++) |
| 1110 |
if (dfa->syntax.case_fold && isalpha (ci)) |
| 1111 |
setbit_case_fold_c (ci, &ccl); |
| 1112 |
else |
| 1113 |
setbit (ci, &ccl); |
| 1114 |
} |
| 1115 |
else |
| 1116 |
known_bracket_exp = false; |
| 1117 |
|
| 1118 |
continue; |
| 1119 |
} |
| 1120 |
} |
| 1121 |
} |
| 1122 |
|
| 1123 |
colon_warning_state |= (c == ':') ? 2 : 4; |
| 1124 |
|
| 1125 |
if (!dfa->localeinfo.multibyte) |
| 1126 |
{ |
| 1127 |
if (dfa->syntax.case_fold && isalpha (c)) |
| 1128 |
setbit_case_fold_c (c, &ccl); |
| 1129 |
else |
| 1130 |
setbit (c, &ccl); |
| 1131 |
continue; |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (wc == WEOF) |
| 1135 |
known_bracket_exp = false; |
| 1136 |
else |
| 1137 |
{ |
| 1138 |
char32_t folded[CASE_FOLDED_BUFSIZE + 1]; |
| 1139 |
int n = (dfa->syntax.case_fold |
| 1140 |
? case_folded_counterparts (wc, folded + 1) + 1 |
| 1141 |
: 1); |
| 1142 |
folded[0] = wc; |
| 1143 |
for (int i = 0; i < n; i++) |
| 1144 |
if (!setbit_wc (folded[i], &ccl)) |
| 1145 |
{ |
| 1146 |
dfa->lex.brack.chars |
| 1147 |
= maybe_realloc (dfa->lex.brack.chars, dfa->lex.brack.nchars, |
| 1148 |
&dfa->lex.brack.nchars_alloc, -1, |
| 1149 |
sizeof *dfa->lex.brack.chars); |
| 1150 |
dfa->lex.brack.chars[dfa->lex.brack.nchars++] = folded[i]; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
} |
| 1154 |
while ((wc = wc1, (c = c1) != ']')); |
| 1155 |
|
| 1156 |
if (colon_warning_state == 7) |
| 1157 |
{ |
| 1158 |
char const *msg |
| 1159 |
= _("character class syntax is [[:space:]], not [:space:]"); |
| 1160 |
if (dfa->syntax.dfaopts & DFA_CONFUSING_BRACKETS_ERROR) |
| 1161 |
dfaerror (msg); |
| 1162 |
else |
| 1163 |
dfawarn (msg); |
| 1164 |
} |
| 1165 |
|
| 1166 |
if (! known_bracket_exp) |
| 1167 |
return BACKREF; |
| 1168 |
|
| 1169 |
if (dfa->localeinfo.multibyte && (invert || dfa->lex.brack.nchars != 0)) |
| 1170 |
{ |
| 1171 |
dfa->lex.brack.invert = invert; |
| 1172 |
dfa->lex.brack.cset = emptyset (&ccl) ? -1 : charclass_index (dfa, &ccl); |
| 1173 |
return MBCSET; |
| 1174 |
} |
| 1175 |
|
| 1176 |
if (invert) |
| 1177 |
{ |
| 1178 |
notset (&ccl); |
| 1179 |
if (dfa->syntax.syntax_bits & RE_HAT_LISTS_NOT_NEWLINE) |
| 1180 |
clrbit ('\n', &ccl); |
| 1181 |
} |
| 1182 |
|
| 1183 |
return CSET + charclass_index (dfa, &ccl); |
| 1184 |
} |
| 1185 |
|
| 1186 |
struct lexptr |
| 1187 |
{ |
| 1188 |
char const *ptr; |
| 1189 |
idx_t left; |
| 1190 |
}; |
| 1191 |
|
| 1192 |
static void |
| 1193 |
push_lex_state (struct dfa *dfa, struct lexptr *ls, char const *s) |
| 1194 |
{ |
| 1195 |
ls->ptr = dfa->lex.ptr; |
| 1196 |
ls->left = dfa->lex.left; |
| 1197 |
dfa->lex.ptr = s; |
| 1198 |
dfa->lex.left = strlen (s); |
| 1199 |
} |
| 1200 |
|
| 1201 |
static void |
| 1202 |
pop_lex_state (struct dfa *dfa, struct lexptr const *ls) |
| 1203 |
{ |
| 1204 |
dfa->lex.ptr = ls->ptr; |
| 1205 |
dfa->lex.left = ls->left; |
| 1206 |
} |
| 1207 |
|
| 1208 |
static token |
| 1209 |
lex (struct dfa *dfa) |
| 1210 |
{ |
| 1211 |
bool backslash = false; |
| 1212 |
|
| 1213 |
/* Basic plan: We fetch a character. If it's a backslash, |
| 1214 |
we set the backslash flag and go through the loop again. |
| 1215 |
On the plus side, this avoids having a duplicate of the |
| 1216 |
main switch inside the backslash case. On the minus side, |
| 1217 |
it means that just about every case tests the backslash flag. */ |
| 1218 |
for (int i = 0; ; i++) |
| 1219 |
{ |
| 1220 |
/* This loop should consume at most a backslash and some other |
| 1221 |
character. */ |
| 1222 |
if (2 <= i) |
| 1223 |
abort (); |
| 1224 |
|
| 1225 |
if (! dfa->lex.left) |
| 1226 |
return dfa->lex.lasttok = END; |
| 1227 |
int c = fetch_wc (dfa); |
| 1228 |
|
| 1229 |
switch (c) |
| 1230 |
{ |
| 1231 |
case '\\': |
| 1232 |
if (backslash) |
| 1233 |
goto normal_char; |
| 1234 |
if (dfa->lex.left == 0) |
| 1235 |
dfaerror (_("unfinished \\ escape")); |
| 1236 |
backslash = true; |
| 1237 |
break; |
| 1238 |
|
| 1239 |
case '^': |
| 1240 |
if (backslash) |
| 1241 |
goto normal_char; |
| 1242 |
if (dfa->syntax.syntax_bits & RE_CONTEXT_INDEP_ANCHORS |
| 1243 |
|| dfa->lex.lasttok == END || dfa->lex.lasttok == LPAREN |
| 1244 |
|| dfa->lex.lasttok == OR) |
| 1245 |
return dfa->lex.lasttok = BEGLINE; |
| 1246 |
goto normal_char; |
| 1247 |
|
| 1248 |
case '$': |
| 1249 |
if (backslash) |
| 1250 |
goto normal_char; |
| 1251 |
if (dfa->syntax.syntax_bits & RE_CONTEXT_INDEP_ANCHORS |
| 1252 |
|| dfa->lex.left == 0 |
| 1253 |
|| ((dfa->lex.left |
| 1254 |
> !(dfa->syntax.syntax_bits & RE_NO_BK_PARENS)) |
| 1255 |
&& (dfa->lex.ptr[!(dfa->syntax.syntax_bits & RE_NO_BK_PARENS) |
| 1256 |
& (dfa->lex.ptr[0] == '\\')] |
| 1257 |
== ')')) |
| 1258 |
|| ((dfa->lex.left |
| 1259 |
> !(dfa->syntax.syntax_bits & RE_NO_BK_VBAR)) |
| 1260 |
&& (dfa->lex.ptr[!(dfa->syntax.syntax_bits & RE_NO_BK_VBAR) |
| 1261 |
& (dfa->lex.ptr[0] == '\\')] |
| 1262 |
== '|')) |
| 1263 |
|| ((dfa->syntax.syntax_bits & RE_NEWLINE_ALT) |
| 1264 |
&& dfa->lex.left > 0 && dfa->lex.ptr[0] == '\n')) |
| 1265 |
return dfa->lex.lasttok = ENDLINE; |
| 1266 |
goto normal_char; |
| 1267 |
|
| 1268 |
case '1': |
| 1269 |
case '2': |
| 1270 |
case '3': |
| 1271 |
case '4': |
| 1272 |
case '5': |
| 1273 |
case '6': |
| 1274 |
case '7': |
| 1275 |
case '8': |
| 1276 |
case '9': |
| 1277 |
if (!backslash) |
| 1278 |
goto normal_char; |
| 1279 |
if (dfa->syntax.syntax_bits & RE_NO_BK_REFS) |
| 1280 |
goto stray_backslash; |
| 1281 |
|
| 1282 |
dfa->lex.laststart = false; |
| 1283 |
return dfa->lex.lasttok = BACKREF; |
| 1284 |
|
| 1285 |
case '`': |
| 1286 |
if (!backslash) |
| 1287 |
goto normal_char; |
| 1288 |
if (dfa->syntax.syntax_bits & RE_NO_GNU_OPS) |
| 1289 |
goto stray_backslash; |
| 1290 |
|
| 1291 |
/* FIXME: should be beginning of string */ |
| 1292 |
return dfa->lex.lasttok = BEGLINE; |
| 1293 |
|
| 1294 |
case '\'': |
| 1295 |
if (!backslash) |
| 1296 |
goto normal_char; |
| 1297 |
if (dfa->syntax.syntax_bits & RE_NO_GNU_OPS) |
| 1298 |
goto stray_backslash; |
| 1299 |
|
| 1300 |
/* FIXME: should be end of string */ |
| 1301 |
return dfa->lex.lasttok = ENDLINE; |
| 1302 |
|
| 1303 |
case '<': |
| 1304 |
if (!backslash) |
| 1305 |
goto normal_char; |
| 1306 |
if (dfa->syntax.syntax_bits & RE_NO_GNU_OPS) |
| 1307 |
goto stray_backslash; |
| 1308 |
|
| 1309 |
return dfa->lex.lasttok = BEGWORD; |
| 1310 |
|
| 1311 |
case '>': |
| 1312 |
if (!backslash) |
| 1313 |
goto normal_char; |
| 1314 |
if (dfa->syntax.syntax_bits & RE_NO_GNU_OPS) |
| 1315 |
goto stray_backslash; |
| 1316 |
|
| 1317 |
return dfa->lex.lasttok = ENDWORD; |
| 1318 |
|
| 1319 |
case 'b': |
| 1320 |
if (!backslash) |
| 1321 |
goto normal_char; |
| 1322 |
if (dfa->syntax.syntax_bits & RE_NO_GNU_OPS) |
| 1323 |
goto stray_backslash; |
| 1324 |
|
| 1325 |
return dfa->lex.lasttok = LIMWORD; |
| 1326 |
|
| 1327 |
case 'B': |
| 1328 |
if (!backslash) |
| 1329 |
goto normal_char; |
| 1330 |
if (dfa->syntax.syntax_bits & RE_NO_GNU_OPS) |
| 1331 |
goto stray_backslash; |
| 1332 |
|
| 1333 |
return dfa->lex.lasttok = NOTLIMWORD; |
| 1334 |
|
| 1335 |
case '?': |
| 1336 |
if (dfa->syntax.syntax_bits & RE_LIMITED_OPS) |
| 1337 |
goto default_case; |
| 1338 |
if (backslash != ((dfa->syntax.syntax_bits & RE_BK_PLUS_QM) != 0)) |
| 1339 |
goto normal_char; |
| 1340 |
if (dfa->lex.laststart) |
| 1341 |
{ |
| 1342 |
if (!(dfa->syntax.syntax_bits & RE_CONTEXT_INDEP_OPS)) |
| 1343 |
goto default_case; |
| 1344 |
if (dfa->syntax.dfaopts & DFA_PLUS_WARN) |
| 1345 |
dfawarn (_("? at start of expression")); |
| 1346 |
} |
| 1347 |
return dfa->lex.lasttok = QMARK; |
| 1348 |
|
| 1349 |
case '*': |
| 1350 |
if (backslash) |
| 1351 |
goto normal_char; |
| 1352 |
if (dfa->lex.laststart) |
| 1353 |
{ |
| 1354 |
if (!(dfa->syntax.syntax_bits & RE_CONTEXT_INDEP_OPS)) |
| 1355 |
goto default_case; |
| 1356 |
if (dfa->syntax.dfaopts & DFA_STAR_WARN) |
| 1357 |
dfawarn (_("* at start of expression")); |
| 1358 |
} |
| 1359 |
return dfa->lex.lasttok = STAR; |
| 1360 |
|
| 1361 |
case '+': |
| 1362 |
if (dfa->syntax.syntax_bits & RE_LIMITED_OPS) |
| 1363 |
goto default_case; |
| 1364 |
if (backslash != ((dfa->syntax.syntax_bits & RE_BK_PLUS_QM) != 0)) |
| 1365 |
goto normal_char; |
| 1366 |
if (dfa->lex.laststart) |
| 1367 |
{ |
| 1368 |
if (!(dfa->syntax.syntax_bits & RE_CONTEXT_INDEP_OPS)) |
| 1369 |
goto default_case; |
| 1370 |
if (dfa->syntax.dfaopts & DFA_PLUS_WARN) |
| 1371 |
dfawarn (_("+ at start of expression")); |
| 1372 |
} |
| 1373 |
return dfa->lex.lasttok = PLUS; |
| 1374 |
|
| 1375 |
case '{': |
| 1376 |
if (!(dfa->syntax.syntax_bits & RE_INTERVALS)) |
| 1377 |
goto default_case; |
| 1378 |
if (backslash != ((dfa->syntax.syntax_bits & RE_NO_BK_BRACES) == 0)) |
| 1379 |
goto normal_char; |
| 1380 |
|
| 1381 |
/* Cases: |
| 1382 |
{M} - exact count |
| 1383 |
{M,} - minimum count, maximum is infinity |
| 1384 |
{,N} - 0 through N |
| 1385 |
{,} - 0 to infinity (same as '*') |
| 1386 |
{M,N} - M through N */ |
| 1387 |
{ |
| 1388 |
char const *p = dfa->lex.ptr; |
| 1389 |
char const *lim = p + dfa->lex.left; |
| 1390 |
dfa->lex.minrep = dfa->lex.maxrep = -1; |
| 1391 |
for (; p != lim && c_isdigit (*p); p++) |
| 1392 |
dfa->lex.minrep = (dfa->lex.minrep < 0 |
| 1393 |
? *p - '0' |
| 1394 |
: MIN (RE_DUP_MAX + 1, |
| 1395 |
dfa->lex.minrep * 10 + *p - '0')); |
| 1396 |
if (p != lim) |
| 1397 |
{ |
| 1398 |
if (*p != ',') |
| 1399 |
dfa->lex.maxrep = dfa->lex.minrep; |
| 1400 |
else |
| 1401 |
{ |
| 1402 |
if (dfa->lex.minrep < 0) |
| 1403 |
dfa->lex.minrep = 0; |
| 1404 |
while (++p != lim && c_isdigit (*p)) |
| 1405 |
dfa->lex.maxrep |
| 1406 |
= (dfa->lex.maxrep < 0 |
| 1407 |
? *p - '0' |
| 1408 |
: MIN (RE_DUP_MAX + 1, |
| 1409 |
dfa->lex.maxrep * 10 + *p - '0')); |
| 1410 |
} |
| 1411 |
} |
| 1412 |
bool invalid_content |
| 1413 |
= ! ((! backslash || (p != lim && *p++ == '\\')) |
| 1414 |
&& p != lim && *p++ == '}' |
| 1415 |
&& 0 <= dfa->lex.minrep |
| 1416 |
&& (dfa->lex.maxrep < 0 |
| 1417 |
|| dfa->lex.minrep <= dfa->lex.maxrep)); |
| 1418 |
if (invalid_content |
| 1419 |
&& (dfa->syntax.syntax_bits & RE_INVALID_INTERVAL_ORD)) |
| 1420 |
goto normal_char; |
| 1421 |
if (dfa->lex.laststart) |
| 1422 |
{ |
| 1423 |
if (!(dfa->syntax.syntax_bits & RE_CONTEXT_INDEP_OPS)) |
| 1424 |
goto default_case; |
| 1425 |
if (dfa->syntax.dfaopts & DFA_PLUS_WARN) |
| 1426 |
dfawarn (_("{...} at start of expression")); |
| 1427 |
} |
| 1428 |
if (invalid_content) |
| 1429 |
dfaerror (_("invalid content of \\{\\}")); |
| 1430 |
if (RE_DUP_MAX < dfa->lex.maxrep) |
| 1431 |
dfaerror (_("regular expression too big")); |
| 1432 |
dfa->lex.ptr = p; |
| 1433 |
dfa->lex.left = lim - p; |
| 1434 |
} |
| 1435 |
dfa->lex.laststart = false; |
| 1436 |
return dfa->lex.lasttok = REPMN; |
| 1437 |
|
| 1438 |
case '|': |
| 1439 |
if (dfa->syntax.syntax_bits & RE_LIMITED_OPS) |
| 1440 |
goto default_case; |
| 1441 |
if (backslash != ((dfa->syntax.syntax_bits & RE_NO_BK_VBAR) == 0)) |
| 1442 |
goto normal_char; |
| 1443 |
dfa->lex.laststart = true; |
| 1444 |
return dfa->lex.lasttok = OR; |
| 1445 |
|
| 1446 |
case '\n': |
| 1447 |
if (!(dfa->syntax.syntax_bits & RE_NEWLINE_ALT)) |
| 1448 |
goto default_case; |
| 1449 |
if (backslash) |
| 1450 |
goto normal_char; |
| 1451 |
dfa->lex.laststart = true; |
| 1452 |
return dfa->lex.lasttok = OR; |
| 1453 |
|
| 1454 |
case '(': |
| 1455 |
if (backslash != ((dfa->syntax.syntax_bits & RE_NO_BK_PARENS) == 0)) |
| 1456 |
goto normal_char; |
| 1457 |
dfa->lex.parens++; |
| 1458 |
dfa->lex.laststart = true; |
| 1459 |
return dfa->lex.lasttok = LPAREN; |
| 1460 |
|
| 1461 |
case ')': |
| 1462 |
if (backslash != ((dfa->syntax.syntax_bits & RE_NO_BK_PARENS) == 0)) |
| 1463 |
goto normal_char; |
| 1464 |
if (dfa->lex.parens == 0 |
| 1465 |
&& dfa->syntax.syntax_bits & RE_UNMATCHED_RIGHT_PAREN_ORD) |
| 1466 |
goto normal_char; |
| 1467 |
dfa->lex.parens--; |
| 1468 |
dfa->lex.laststart = false; |
| 1469 |
return dfa->lex.lasttok = RPAREN; |
| 1470 |
|
| 1471 |
case '.': |
| 1472 |
if (backslash) |
| 1473 |
goto normal_char; |
| 1474 |
if (dfa->canychar < 0) |
| 1475 |
{ |
| 1476 |
charclass ccl; |
| 1477 |
fillset (&ccl); |
| 1478 |
if (!(dfa->syntax.syntax_bits & RE_DOT_NEWLINE)) |
| 1479 |
clrbit ('\n', &ccl); |
| 1480 |
if (dfa->syntax.syntax_bits & RE_DOT_NOT_NULL) |
| 1481 |
clrbit ('\0', &ccl); |
| 1482 |
if (dfa->localeinfo.multibyte) |
| 1483 |
for (int c2 = 0; c2 < NOTCHAR; c2++) |
| 1484 |
if (dfa->localeinfo.sbctowc[c2] == WEOF) |
| 1485 |
clrbit (c2, &ccl); |
| 1486 |
dfa->canychar = charclass_index (dfa, &ccl); |
| 1487 |
} |
| 1488 |
dfa->lex.laststart = false; |
| 1489 |
return dfa->lex.lasttok = (dfa->localeinfo.multibyte |
| 1490 |
? ANYCHAR |
| 1491 |
: CSET + dfa->canychar); |
| 1492 |
|
| 1493 |
case 's': |
| 1494 |
case 'S': |
| 1495 |
if (!backslash) |
| 1496 |
goto normal_char; |
| 1497 |
if (dfa->syntax.syntax_bits & RE_NO_GNU_OPS) |
| 1498 |
goto stray_backslash; |
| 1499 |
|
| 1500 |
if (!dfa->localeinfo.multibyte) |
| 1501 |
{ |
| 1502 |
charclass ccl; |
| 1503 |
zeroset (&ccl); |
| 1504 |
for (int c2 = 0; c2 < NOTCHAR; ++c2) |
| 1505 |
if (isspace (c2)) |
| 1506 |
setbit (c2, &ccl); |
| 1507 |
if (c == 'S') |
| 1508 |
notset (&ccl); |
| 1509 |
dfa->lex.laststart = false; |
| 1510 |
return dfa->lex.lasttok = CSET + charclass_index (dfa, &ccl); |
| 1511 |
} |
| 1512 |
|
| 1513 |
/* FIXME: see if optimizing this, as is done with ANYCHAR and |
| 1514 |
add_utf8_anychar, makes sense. */ |
| 1515 |
|
| 1516 |
/* \s and \S are documented to be equivalent to [[:space:]] and |
| 1517 |
[^[:space:]] respectively, so tell the lexer to process those |
| 1518 |
strings, each minus its "already processed" '['. */ |
| 1519 |
{ |
| 1520 |
struct lexptr ls; |
| 1521 |
push_lex_state (dfa, &ls, &"^[:space:]]"[c == 's']); |
| 1522 |
dfa->lex.lasttok = parse_bracket_exp (dfa); |
| 1523 |
pop_lex_state (dfa, &ls); |
| 1524 |
} |
| 1525 |
|
| 1526 |
dfa->lex.laststart = false; |
| 1527 |
return dfa->lex.lasttok; |
| 1528 |
|
| 1529 |
case 'w': |
| 1530 |
case 'W': |
| 1531 |
if (!backslash) |
| 1532 |
goto normal_char; |
| 1533 |
if (dfa->syntax.syntax_bits & RE_NO_GNU_OPS) |
| 1534 |
goto stray_backslash; |
| 1535 |
|
| 1536 |
if (!dfa->localeinfo.multibyte) |
| 1537 |
{ |
| 1538 |
charclass ccl; |
| 1539 |
zeroset (&ccl); |
| 1540 |
for (int c2 = 0; c2 < NOTCHAR; ++c2) |
| 1541 |
if (dfa->syntax.sbit[c2] == CTX_LETTER) |
| 1542 |
setbit (c2, &ccl); |
| 1543 |
if (c == 'W') |
| 1544 |
notset (&ccl); |
| 1545 |
dfa->lex.laststart = false; |
| 1546 |
return dfa->lex.lasttok = CSET + charclass_index (dfa, &ccl); |
| 1547 |
} |
| 1548 |
|
| 1549 |
/* FIXME: see if optimizing this, as is done with ANYCHAR and |
| 1550 |
add_utf8_anychar, makes sense. */ |
| 1551 |
|
| 1552 |
/* \w and \W are documented to be equivalent to [_[:alnum:]] and |
| 1553 |
[^_[:alnum:]] respectively, so tell the lexer to process those |
| 1554 |
strings, each minus its "already processed" '['. */ |
| 1555 |
{ |
| 1556 |
struct lexptr ls; |
| 1557 |
push_lex_state (dfa, &ls, &"^_[:alnum:]]"[c == 'w']); |
| 1558 |
dfa->lex.lasttok = parse_bracket_exp (dfa); |
| 1559 |
pop_lex_state (dfa, &ls); |
| 1560 |
} |
| 1561 |
|
| 1562 |
dfa->lex.laststart = false; |
| 1563 |
return dfa->lex.lasttok; |
| 1564 |
|
| 1565 |
case '[': |
| 1566 |
if (backslash) |
| 1567 |
goto normal_char; |
| 1568 |
dfa->lex.laststart = false; |
| 1569 |
return dfa->lex.lasttok = parse_bracket_exp (dfa); |
| 1570 |
|
| 1571 |
default: |
| 1572 |
default_case: |
| 1573 |
if (!backslash) |
| 1574 |
goto normal_char; |
| 1575 |
stray_backslash: |
| 1576 |
if (dfa->syntax.dfaopts & DFA_STRAY_BACKSLASH_WARN) |
| 1577 |
{ |
| 1578 |
char const *msg; |
| 1579 |
char msgbuf[100]; |
| 1580 |
if (!c32isprint (dfa->lex.wctok)) |
| 1581 |
msg = _("stray \\ before unprintable character"); |
| 1582 |
else if (c32isspace (dfa->lex.wctok)) |
| 1583 |
msg = _("stray \\ before white space"); |
| 1584 |
else |
| 1585 |
{ |
| 1586 |
char buf[MB_LEN_MAX + 1]; |
| 1587 |
mbstate_t s; |
| 1588 |
mbszero (&s); |
| 1589 |
size_t stored_bytes = c32rtomb (buf, dfa->lex.wctok, &s); |
| 1590 |
if (stored_bytes < (size_t) -1) |
| 1591 |
{ |
| 1592 |
buf[stored_bytes] = '\0'; |
| 1593 |
int n = snprintf (msgbuf, sizeof msgbuf, |
| 1594 |
_("stray \\ before %s"), buf); |
| 1595 |
msg = 0 <= n && n < sizeof msgbuf ? msgbuf : _("stray \\"); |
| 1596 |
} |
| 1597 |
else |
| 1598 |
msg = _("stray \\"); |
| 1599 |
} |
| 1600 |
dfawarn (msg); |
| 1601 |
} |
| 1602 |
FALLTHROUGH; |
| 1603 |
case ']': case '}': |
| 1604 |
normal_char: |
| 1605 |
dfa->lex.laststart = false; |
| 1606 |
/* For multibyte character sets, folding is done in atom. Always |
| 1607 |
return WCHAR. */ |
| 1608 |
if (dfa->localeinfo.multibyte) |
| 1609 |
return dfa->lex.lasttok = WCHAR; |
| 1610 |
|
| 1611 |
if (dfa->syntax.case_fold && isalpha (c)) |
| 1612 |
{ |
| 1613 |
charclass ccl; |
| 1614 |
zeroset (&ccl); |
| 1615 |
setbit_case_fold_c (c, &ccl); |
| 1616 |
return dfa->lex.lasttok = CSET + charclass_index (dfa, &ccl); |
| 1617 |
} |
| 1618 |
|
| 1619 |
return dfa->lex.lasttok = c; |
| 1620 |
} |
| 1621 |
} |
| 1622 |
} |
| 1623 |
|
| 1624 |
static void |
| 1625 |
addtok_mb (struct dfa *dfa, token t, char mbprop) |
| 1626 |
{ |
| 1627 |
if (dfa->talloc == dfa->tindex) |
| 1628 |
{ |
| 1629 |
dfa->tokens = xpalloc (dfa->tokens, &dfa->talloc, 1, -1, |
| 1630 |
sizeof *dfa->tokens); |
| 1631 |
if (dfa->localeinfo.multibyte) |
| 1632 |
dfa->multibyte_prop = xreallocarray (dfa->multibyte_prop, dfa->talloc, |
| 1633 |
sizeof *dfa->multibyte_prop); |
| 1634 |
} |
| 1635 |
if (dfa->localeinfo.multibyte) |
| 1636 |
dfa->multibyte_prop[dfa->tindex] = mbprop; |
| 1637 |
dfa->tokens[dfa->tindex++] = t; |
| 1638 |
|
| 1639 |
switch (t) |
| 1640 |
{ |
| 1641 |
case QMARK: |
| 1642 |
case STAR: |
| 1643 |
case PLUS: |
| 1644 |
break; |
| 1645 |
|
| 1646 |
case CAT: |
| 1647 |
case OR: |
| 1648 |
dfa->parse.depth--; |
| 1649 |
break; |
| 1650 |
|
| 1651 |
case EMPTY: |
| 1652 |
dfa->epsilon = true; |
| 1653 |
goto increment_depth; |
| 1654 |
|
| 1655 |
case BACKREF: |
| 1656 |
dfa->fast = false; |
| 1657 |
goto increment_nleaves; |
| 1658 |
|
| 1659 |
case BEGLINE: |
| 1660 |
case ENDLINE: |
| 1661 |
case BEGWORD: |
| 1662 |
case ENDWORD: |
| 1663 |
case LIMWORD: |
| 1664 |
case NOTLIMWORD: |
| 1665 |
dfa->epsilon = true; |
| 1666 |
FALLTHROUGH; |
| 1667 |
default: |
| 1668 |
increment_nleaves: |
| 1669 |
dfa->nleaves++; |
| 1670 |
increment_depth: |
| 1671 |
dfa->parse.depth++; |
| 1672 |
if (dfa->depth < dfa->parse.depth) |
| 1673 |
dfa->depth = dfa->parse.depth; |
| 1674 |
break; |
| 1675 |
} |
| 1676 |
} |
| 1677 |
|
| 1678 |
static void addtok_wc (struct dfa *dfa, wint_t wc); |
| 1679 |
|
| 1680 |
/* Add the given token to the parse tree, maintaining the depth count and |
| 1681 |
updating the maximum depth if necessary. */ |
| 1682 |
static void |
| 1683 |
addtok (struct dfa *dfa, token t) |
| 1684 |
{ |
| 1685 |
if (dfa->localeinfo.multibyte && t == MBCSET) |
| 1686 |
{ |
| 1687 |
bool need_or = false; |
| 1688 |
|
| 1689 |
/* Extract wide characters into alternations for better performance. |
| 1690 |
This does not require UTF-8. */ |
| 1691 |
for (idx_t i = 0; i < dfa->lex.brack.nchars; i++) |
| 1692 |
{ |
| 1693 |
addtok_wc (dfa, dfa->lex.brack.chars[i]); |
| 1694 |
if (need_or) |
| 1695 |
addtok (dfa, OR); |
| 1696 |
need_or = true; |
| 1697 |
} |
| 1698 |
dfa->lex.brack.nchars = 0; |
| 1699 |
|
| 1700 |
/* Wide characters have been handled above, so it is possible |
| 1701 |
that the set is empty now. Do nothing in that case. */ |
| 1702 |
if (dfa->lex.brack.cset != -1) |
| 1703 |
{ |
| 1704 |
addtok (dfa, CSET + dfa->lex.brack.cset); |
| 1705 |
if (need_or) |
| 1706 |
addtok (dfa, OR); |
| 1707 |
} |
| 1708 |
} |
| 1709 |
else |
| 1710 |
{ |
| 1711 |
addtok_mb (dfa, t, 3); |
| 1712 |
} |
| 1713 |
} |
| 1714 |
|
| 1715 |
/* We treat a multibyte character as a single atom, so that DFA |
| 1716 |
can treat a multibyte character as a single expression. |
| 1717 |
|
| 1718 |
e.g., we construct the following tree from "<mb1><mb2>". |
| 1719 |
<mb1(1st-byte)><mb1(2nd-byte)><CAT><mb1(3rd-byte)><CAT> |
| 1720 |
<mb2(1st-byte)><mb2(2nd-byte)><CAT><mb2(3rd-byte)><CAT><CAT> */ |
| 1721 |
static void |
| 1722 |
addtok_wc (struct dfa *dfa, wint_t wc) |
| 1723 |
{ |
| 1724 |
unsigned char buf[MB_LEN_MAX]; |
| 1725 |
mbstate_t s; |
| 1726 |
mbszero (&s); |
| 1727 |
size_t stored_bytes = c32rtomb ((char *) buf, wc, &s); |
| 1728 |
int buflen; |
| 1729 |
|
| 1730 |
if (stored_bytes != (size_t) -1) |
| 1731 |
buflen = stored_bytes; |
| 1732 |
else |
| 1733 |
{ |
| 1734 |
/* This is merely stop-gap. buf[0] is undefined, yet skipping |
| 1735 |
the addtok_mb call altogether can corrupt the heap. */ |
| 1736 |
buflen = 1; |
| 1737 |
buf[0] = 0; |
| 1738 |
} |
| 1739 |
|
| 1740 |
addtok_mb (dfa, buf[0], buflen == 1 ? 3 : 1); |
| 1741 |
for (int i = 1; i < buflen; i++) |
| 1742 |
{ |
| 1743 |
addtok_mb (dfa, buf[i], i == buflen - 1 ? 2 : 0); |
| 1744 |
addtok (dfa, CAT); |
| 1745 |
} |
| 1746 |
} |
| 1747 |
|
| 1748 |
static void |
| 1749 |
add_utf8_anychar (struct dfa *dfa) |
| 1750 |
{ |
| 1751 |
/* Since the Unicode Standard Version 4.0.0 (2003), a well-formed |
| 1752 |
UTF-8 byte sequence has been defined as follows: |
| 1753 |
|
| 1754 |
([\x00-\x7f] |
| 1755 |
|[\xc2-\xdf][\x80-\xbf] |
| 1756 |
|[\xe0][\xa0-\xbf][\x80-\xbf] |
| 1757 |
|[\xe1-\xec\xee-\xef][\x80-\xbf][\x80-\xbf] |
| 1758 |
|[\xed][\x80-\x9f][\x80-\xbf] |
| 1759 |
|[\xf0][\x90-\xbf][\x80-\xbf][\x80-\xbf]) |
| 1760 |
|[\xf1-\xf3][\x80-\xbf][\x80-\xbf][\x80-\xbf] |
| 1761 |
|[\xf4][\x80-\x8f][\x80-\xbf][\x80-\xbf]) |
| 1762 |
|
| 1763 |
which I'll write more concisely "A|BC|DEC|FCC|GHC|IJCC|KCCC|LMCC", |
| 1764 |
where A = [\x00-\x7f], B = [\xc2-\xdf], C = [\x80-\xbf], |
| 1765 |
D = [\xe0], E = [\xa0-\xbf], F = [\xe1-\xec\xee-\xef], G = [\xed], |
| 1766 |
H = [\x80-\x9f], I = [\xf0], |
| 1767 |
J = [\x90-\xbf], K = [\xf1-\xf3], L = [\xf4], M = [\x80-\x8f]. |
| 1768 |
|
| 1769 |
This can be refactored to "A|(B|DE|GH|(F|IJ|LM|KC)C)C". */ |
| 1770 |
|
| 1771 |
/* Mnemonics for classes containing two or more bytes. */ |
| 1772 |
enum { A, B, C, E, F, H, J, K, M }; |
| 1773 |
|
| 1774 |
/* Mnemonics for single-byte tokens. */ |
| 1775 |
enum { D_token = 0xe0, G_token = 0xed, I_token = 0xf0, L_token = 0xf4 }; |
| 1776 |
|
| 1777 |
static charclass const utf8_classes[] = { |
| 1778 |
/* A. 00-7f: 1-byte sequence. */ |
| 1779 |
CHARCLASS_INIT (0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0, 0, 0, 0), |
| 1780 |
|
| 1781 |
/* B. c2-df: 1st byte of a 2-byte sequence. */ |
| 1782 |
CHARCLASS_INIT (0, 0, 0, 0, 0, 0, 0xfffffffc, 0), |
| 1783 |
|
| 1784 |
/* C. 80-bf: non-leading bytes. */ |
| 1785 |
CHARCLASS_INIT (0, 0, 0, 0, 0xffffffff, 0xffffffff, 0, 0), |
| 1786 |
|
| 1787 |
/* D. e0 (just a token). */ |
| 1788 |
|
| 1789 |
/* E. a0-bf: 2nd byte of a "DEC" sequence. */ |
| 1790 |
CHARCLASS_INIT (0, 0, 0, 0, 0, 0xffffffff, 0, 0), |
| 1791 |
|
| 1792 |
/* F. e1-ec + ee-ef: 1st byte of an "FCC" sequence. */ |
| 1793 |
CHARCLASS_INIT (0, 0, 0, 0, 0, 0, 0, 0xdffe), |
| 1794 |
|
| 1795 |
/* G. ed (just a token). */ |
| 1796 |
|
| 1797 |
/* H. 80-9f: 2nd byte of a "GHC" sequence. */ |
| 1798 |
CHARCLASS_INIT (0, 0, 0, 0, 0xffffffff, 0, 0, 0), |
| 1799 |
|
| 1800 |
/* I. f0 (just a token). */ |
| 1801 |
|
| 1802 |
/* J. 90-bf: 2nd byte of an "IJCC" sequence. */ |
| 1803 |
CHARCLASS_INIT (0, 0, 0, 0, 0xffff0000, 0xffffffff, 0, 0), |
| 1804 |
|
| 1805 |
/* K. f1-f3: 1st byte of a "KCCC" sequence. */ |
| 1806 |
CHARCLASS_INIT (0, 0, 0, 0, 0, 0, 0, 0xe0000), |
| 1807 |
|
| 1808 |
/* L. f4 (just a token). */ |
| 1809 |
|
| 1810 |
/* M. 80-8f: 2nd byte of a "LMCC" sequence. */ |
| 1811 |
CHARCLASS_INIT (0, 0, 0, 0, 0xffff, 0, 0, 0), |
| 1812 |
}; |
| 1813 |
|
| 1814 |
/* Define the character classes that are needed below. */ |
| 1815 |
if (dfa->utf8_anychar_classes[0] == 0) |
| 1816 |
{ |
| 1817 |
charclass c = utf8_classes[0]; |
| 1818 |
if (! (dfa->syntax.syntax_bits & RE_DOT_NEWLINE)) |
| 1819 |
clrbit ('\n', &c); |
| 1820 |
if (dfa->syntax.syntax_bits & RE_DOT_NOT_NULL) |
| 1821 |
clrbit ('\0', &c); |
| 1822 |
dfa->utf8_anychar_classes[0] = CSET + charclass_index (dfa, &c); |
| 1823 |
|
| 1824 |
for (int i = 1; i < sizeof utf8_classes / sizeof *utf8_classes; i++) |
| 1825 |
dfa->utf8_anychar_classes[i] |
| 1826 |
= CSET + charclass_index (dfa, &utf8_classes[i]); |
| 1827 |
} |
| 1828 |
|
| 1829 |
/* Implement the "A|(B|DE|GH|(F|IJ|LM|KC)C)C" pattern mentioned above. |
| 1830 |
The token buffer is in reverse Polish order, so we get |
| 1831 |
"A B D E CAT OR G H CAT OR F I J CAT OR L M CAT OR K |
| 1832 |
C CAT OR C CAT OR C CAT OR". */ |
| 1833 |
addtok (dfa, dfa->utf8_anychar_classes[A]); |
| 1834 |
addtok (dfa, dfa->utf8_anychar_classes[B]); |
| 1835 |
addtok (dfa, D_token); |
| 1836 |
addtok (dfa, dfa->utf8_anychar_classes[E]); |
| 1837 |
addtok (dfa, CAT); |
| 1838 |
addtok (dfa, OR); |
| 1839 |
addtok (dfa, G_token); |
| 1840 |
addtok (dfa, dfa->utf8_anychar_classes[H]); |
| 1841 |
addtok (dfa, CAT); |
| 1842 |
addtok (dfa, OR); |
| 1843 |
addtok (dfa, dfa->utf8_anychar_classes[F]); |
| 1844 |
addtok (dfa, I_token); |
| 1845 |
addtok (dfa, dfa->utf8_anychar_classes[J]); |
| 1846 |
addtok (dfa, CAT); |
| 1847 |
addtok (dfa, OR); |
| 1848 |
addtok (dfa, L_token); |
| 1849 |
addtok (dfa, dfa->utf8_anychar_classes[M]); |
| 1850 |
addtok (dfa, CAT); |
| 1851 |
addtok (dfa, OR); |
| 1852 |
addtok (dfa, dfa->utf8_anychar_classes[K]); |
| 1853 |
for (int i = 0; i < 3; i++) |
| 1854 |
{ |
| 1855 |
addtok (dfa, dfa->utf8_anychar_classes[C]); |
| 1856 |
addtok (dfa, CAT); |
| 1857 |
addtok (dfa, OR); |
| 1858 |
} |
| 1859 |
} |
| 1860 |
|
| 1861 |
/* The grammar understood by the parser is as follows. |
| 1862 |
|
| 1863 |
regexp: |
| 1864 |
regexp OR branch |
| 1865 |
branch |
| 1866 |
|
| 1867 |
branch: |
| 1868 |
branch closure |
| 1869 |
closure |
| 1870 |
|
| 1871 |
closure: |
| 1872 |
closure QMARK |
| 1873 |
closure STAR |
| 1874 |
closure PLUS |
| 1875 |
closure REPMN |
| 1876 |
atom |
| 1877 |
|
| 1878 |
atom: |
| 1879 |
<normal character> |
| 1880 |
<multibyte character> |
| 1881 |
ANYCHAR |
| 1882 |
MBCSET |
| 1883 |
CSET |
| 1884 |
BACKREF |
| 1885 |
BEGLINE |
| 1886 |
ENDLINE |
| 1887 |
BEGWORD |
| 1888 |
ENDWORD |
| 1889 |
LIMWORD |
| 1890 |
NOTLIMWORD |
| 1891 |
LPAREN regexp RPAREN |
| 1892 |
<empty> |
| 1893 |
|
| 1894 |
The parser builds a parse tree in postfix form in an array of tokens. */ |
| 1895 |
|
| 1896 |
static void |
| 1897 |
atom (struct dfa *dfa) |
| 1898 |
{ |
| 1899 |
if ((0 <= dfa->parse.tok && dfa->parse.tok < NOTCHAR) |
| 1900 |
|| dfa->parse.tok >= CSET |
| 1901 |
|| dfa->parse.tok == BEG || dfa->parse.tok == BACKREF |
| 1902 |
|| dfa->parse.tok == BEGLINE || dfa->parse.tok == ENDLINE |
| 1903 |
|| dfa->parse.tok == BEGWORD || dfa->parse.tok == ENDWORD |
| 1904 |
|| dfa->parse.tok == LIMWORD || dfa->parse.tok == NOTLIMWORD |
| 1905 |
|| dfa->parse.tok == ANYCHAR || dfa->parse.tok == MBCSET) |
| 1906 |
{ |
| 1907 |
if (dfa->parse.tok == ANYCHAR && dfa->localeinfo.using_utf8) |
| 1908 |
{ |
| 1909 |
/* For UTF-8 expand the period to a series of CSETs that define a |
| 1910 |
valid UTF-8 character. This avoids using the slow multibyte |
| 1911 |
path. I'm pretty sure it would be both profitable and correct to |
| 1912 |
do it for any encoding; however, the optimization must be done |
| 1913 |
manually as it is done above in add_utf8_anychar. So, let's |
| 1914 |
start with UTF-8: it is the most used, and the structure of the |
| 1915 |
encoding makes the correctness more obvious. */ |
| 1916 |
add_utf8_anychar (dfa); |
| 1917 |
} |
| 1918 |
else |
| 1919 |
addtok (dfa, dfa->parse.tok); |
| 1920 |
dfa->parse.tok = lex (dfa); |
| 1921 |
} |
| 1922 |
else if (dfa->parse.tok == WCHAR) |
| 1923 |
{ |
| 1924 |
if (dfa->lex.wctok == WEOF) |
| 1925 |
addtok (dfa, BACKREF); |
| 1926 |
else |
| 1927 |
{ |
| 1928 |
addtok_wc (dfa, dfa->lex.wctok); |
| 1929 |
|
| 1930 |
if (dfa->syntax.case_fold) |
| 1931 |
{ |
| 1932 |
char32_t folded[CASE_FOLDED_BUFSIZE]; |
| 1933 |
int n = case_folded_counterparts (dfa->lex.wctok, folded); |
| 1934 |
for (int i = 0; i < n; i++) |
| 1935 |
{ |
| 1936 |
addtok_wc (dfa, folded[i]); |
| 1937 |
addtok (dfa, OR); |
| 1938 |
} |
| 1939 |
} |
| 1940 |
} |
| 1941 |
|
| 1942 |
dfa->parse.tok = lex (dfa); |
| 1943 |
} |
| 1944 |
else if (dfa->parse.tok == LPAREN) |
| 1945 |
{ |
| 1946 |
dfa->parse.tok = lex (dfa); |
| 1947 |
regexp (dfa); |
| 1948 |
if (dfa->parse.tok != RPAREN) |
| 1949 |
dfaerror (_("unbalanced (")); |
| 1950 |
dfa->parse.tok = lex (dfa); |
| 1951 |
} |
| 1952 |
else |
| 1953 |
addtok (dfa, EMPTY); |
| 1954 |
} |
| 1955 |
|
| 1956 |
/* Return the number of tokens in the given subexpression. */ |
| 1957 |
static idx_t _GL_ATTRIBUTE_PURE |
| 1958 |
nsubtoks (struct dfa const *dfa, idx_t tindex) |
| 1959 |
{ |
| 1960 |
switch (dfa->tokens[tindex - 1]) |
| 1961 |
{ |
| 1962 |
default: |
| 1963 |
return 1; |
| 1964 |
case QMARK: |
| 1965 |
case STAR: |
| 1966 |
case PLUS: |
| 1967 |
return 1 + nsubtoks (dfa, tindex - 1); |
| 1968 |
case CAT: |
| 1969 |
case OR: |
| 1970 |
{ |
| 1971 |
idx_t ntoks1 = nsubtoks (dfa, tindex - 1); |
| 1972 |
return 1 + ntoks1 + nsubtoks (dfa, tindex - 1 - ntoks1); |
| 1973 |
} |
| 1974 |
} |
| 1975 |
} |
| 1976 |
|
| 1977 |
/* Copy the given subexpression to the top of the tree. */ |
| 1978 |
static void |
| 1979 |
copytoks (struct dfa *dfa, idx_t tindex, idx_t ntokens) |
| 1980 |
{ |
| 1981 |
if (dfa->localeinfo.multibyte) |
| 1982 |
for (idx_t i = 0; i < ntokens; i++) |
| 1983 |
addtok_mb (dfa, dfa->tokens[tindex + i], |
| 1984 |
dfa->multibyte_prop[tindex + i]); |
| 1985 |
else |
| 1986 |
for (idx_t i = 0; i < ntokens; i++) |
| 1987 |
addtok_mb (dfa, dfa->tokens[tindex + i], 3); |
| 1988 |
} |
| 1989 |
|
| 1990 |
static void |
| 1991 |
closure (struct dfa *dfa) |
| 1992 |
{ |
| 1993 |
atom (dfa); |
| 1994 |
while (dfa->parse.tok == QMARK || dfa->parse.tok == STAR |
| 1995 |
|| dfa->parse.tok == PLUS || dfa->parse.tok == REPMN) |
| 1996 |
if (dfa->parse.tok == REPMN && (dfa->lex.minrep || dfa->lex.maxrep)) |
| 1997 |
{ |
| 1998 |
idx_t ntokens = nsubtoks (dfa, dfa->tindex); |
| 1999 |
idx_t tindex = dfa->tindex - ntokens; |
| 2000 |
if (dfa->lex.maxrep < 0) |
| 2001 |
addtok (dfa, PLUS); |
| 2002 |
if (dfa->lex.minrep == 0) |
| 2003 |
addtok (dfa, QMARK); |
| 2004 |
int i; |
| 2005 |
for (i = 1; i < dfa->lex.minrep; i++) |
| 2006 |
{ |
| 2007 |
copytoks (dfa, tindex, ntokens); |
| 2008 |
addtok (dfa, CAT); |
| 2009 |
} |
| 2010 |
for (; i < dfa->lex.maxrep; i++) |
| 2011 |
{ |
| 2012 |
copytoks (dfa, tindex, ntokens); |
| 2013 |
addtok (dfa, QMARK); |
| 2014 |
addtok (dfa, CAT); |
| 2015 |
} |
| 2016 |
dfa->parse.tok = lex (dfa); |
| 2017 |
} |
| 2018 |
else if (dfa->parse.tok == REPMN) |
| 2019 |
{ |
| 2020 |
dfa->tindex -= nsubtoks (dfa, dfa->tindex); |
| 2021 |
dfa->parse.tok = lex (dfa); |
| 2022 |
closure (dfa); |
| 2023 |
} |
| 2024 |
else |
| 2025 |
{ |
| 2026 |
addtok (dfa, dfa->parse.tok); |
| 2027 |
dfa->parse.tok = lex (dfa); |
| 2028 |
} |
| 2029 |
} |
| 2030 |
|
| 2031 |
static void |
| 2032 |
branch (struct dfa* dfa) |
| 2033 |
{ |
| 2034 |
closure (dfa); |
| 2035 |
while (dfa->parse.tok != RPAREN && dfa->parse.tok != OR |
| 2036 |
&& dfa->parse.tok >= 0) |
| 2037 |
{ |
| 2038 |
closure (dfa); |
| 2039 |
addtok (dfa, CAT); |
| 2040 |
} |
| 2041 |
} |
| 2042 |
|
| 2043 |
static void |
| 2044 |
regexp (struct dfa *dfa) |
| 2045 |
{ |
| 2046 |
branch (dfa); |
| 2047 |
while (dfa->parse.tok == OR) |
| 2048 |
{ |
| 2049 |
dfa->parse.tok = lex (dfa); |
| 2050 |
branch (dfa); |
| 2051 |
addtok (dfa, OR); |
| 2052 |
} |
| 2053 |
} |
| 2054 |
|
| 2055 |
/* Parse a string S of length LEN into D. S can include NUL characters. |
| 2056 |
This is the main entry point for the parser. */ |
| 2057 |
void |
| 2058 |
dfaparse (char const *s, idx_t len, struct dfa *d) |
| 2059 |
{ |
| 2060 |
d->lex.ptr = s; |
| 2061 |
d->lex.left = len; |
| 2062 |
d->lex.lasttok = END; |
| 2063 |
d->lex.laststart = true; |
| 2064 |
|
| 2065 |
if (!d->syntax.syntax_bits_set) |
| 2066 |
dfaerror (_("no syntax specified")); |
| 2067 |
|
| 2068 |
if (!d->nregexps) |
| 2069 |
addtok (d, BEG); |
| 2070 |
|
| 2071 |
d->parse.tok = lex (d); |
| 2072 |
d->parse.depth = d->depth; |
| 2073 |
|
| 2074 |
regexp (d); |
| 2075 |
|
| 2076 |
if (d->parse.tok != END) |
| 2077 |
dfaerror (_("unbalanced )")); |
| 2078 |
|
| 2079 |
addtok (d, END - d->nregexps); |
| 2080 |
addtok (d, CAT); |
| 2081 |
|
| 2082 |
if (d->nregexps) |
| 2083 |
addtok (d, OR); |
| 2084 |
|
| 2085 |
++d->nregexps; |
| 2086 |
} |
| 2087 |
|
| 2088 |
/* Some primitives for operating on sets of positions. */ |
| 2089 |
|
| 2090 |
/* Copy one set to another. */ |
| 2091 |
static void |
| 2092 |
copy (position_set const *src, position_set *dst) |
| 2093 |
{ |
| 2094 |
if (dst->alloc < src->nelem) |
| 2095 |
{ |
| 2096 |
free (dst->elems); |
| 2097 |
dst->elems = xpalloc (NULL, &dst->alloc, src->nelem - dst->alloc, -1, |
| 2098 |
sizeof *dst->elems); |
| 2099 |
} |
| 2100 |
dst->nelem = src->nelem; |
| 2101 |
if (src->nelem != 0) |
| 2102 |
memcpy (dst->elems, src->elems, src->nelem * sizeof *dst->elems); |
| 2103 |
} |
| 2104 |
|
| 2105 |
static void |
| 2106 |
alloc_position_set (position_set *s, idx_t size) |
| 2107 |
{ |
| 2108 |
s->elems = xnmalloc (size, sizeof *s->elems); |
| 2109 |
s->alloc = size; |
| 2110 |
s->nelem = 0; |
| 2111 |
} |
| 2112 |
|
| 2113 |
/* Insert position P in set S. S is maintained in sorted order on |
| 2114 |
decreasing index. If there is already an entry in S with P.index |
| 2115 |
then merge (logically-OR) P's constraints into the one in S. |
| 2116 |
S->elems must point to an array large enough to hold the resulting set. */ |
| 2117 |
static void |
| 2118 |
insert (position p, position_set *s) |
| 2119 |
{ |
| 2120 |
idx_t count = s->nelem; |
| 2121 |
idx_t lo = 0, hi = count; |
| 2122 |
while (lo < hi) |
| 2123 |
{ |
| 2124 |
idx_t mid = (lo + hi) >> 1; |
| 2125 |
if (s->elems[mid].index < p.index) |
| 2126 |
lo = mid + 1; |
| 2127 |
else if (s->elems[mid].index == p.index) |
| 2128 |
{ |
| 2129 |
s->elems[mid].constraint |= p.constraint; |
| 2130 |
return; |
| 2131 |
} |
| 2132 |
else |
| 2133 |
hi = mid; |
| 2134 |
} |
| 2135 |
|
| 2136 |
s->elems = maybe_realloc (s->elems, count, &s->alloc, -1, sizeof *s->elems); |
| 2137 |
for (idx_t i = count; i > lo; i--) |
| 2138 |
s->elems[i] = s->elems[i - 1]; |
| 2139 |
s->elems[lo] = p; |
| 2140 |
++s->nelem; |
| 2141 |
} |
| 2142 |
|
| 2143 |
static void |
| 2144 |
append (position p, position_set *s) |
| 2145 |
{ |
| 2146 |
idx_t count = s->nelem; |
| 2147 |
s->elems = maybe_realloc (s->elems, count, &s->alloc, -1, sizeof *s->elems); |
| 2148 |
s->elems[s->nelem++] = p; |
| 2149 |
} |
| 2150 |
|
| 2151 |
/* Merge S1 and S2 (with the additional constraint C2) into M. The |
| 2152 |
result is as if the positions of S1, and of S2 with the additional |
| 2153 |
constraint C2, were inserted into an initially empty set. */ |
| 2154 |
static void |
| 2155 |
merge_constrained (position_set const *s1, position_set const *s2, |
| 2156 |
unsigned int c2, position_set *m) |
| 2157 |
{ |
| 2158 |
idx_t i = 0, j = 0; |
| 2159 |
|
| 2160 |
if (m->alloc - s1->nelem < s2->nelem) |
| 2161 |
{ |
| 2162 |
free (m->elems); |
| 2163 |
m->alloc = s1->nelem; |
| 2164 |
m->elems = xpalloc (NULL, &m->alloc, s2->nelem, -1, sizeof *m->elems); |
| 2165 |
} |
| 2166 |
m->nelem = 0; |
| 2167 |
while (i < s1->nelem || j < s2->nelem) |
| 2168 |
if (! (j < s2->nelem) |
| 2169 |
|| (i < s1->nelem && s1->elems[i].index <= s2->elems[j].index)) |
| 2170 |
{ |
| 2171 |
unsigned int c = ((i < s1->nelem && j < s2->nelem |
| 2172 |
&& s1->elems[i].index == s2->elems[j].index) |
| 2173 |
? s2->elems[j++].constraint & c2 |
| 2174 |
: 0); |
| 2175 |
m->elems[m->nelem].index = s1->elems[i].index; |
| 2176 |
m->elems[m->nelem++].constraint = s1->elems[i++].constraint | c; |
| 2177 |
} |
| 2178 |
else |
| 2179 |
{ |
| 2180 |
if (s2->elems[j].constraint & c2) |
| 2181 |
{ |
| 2182 |
m->elems[m->nelem].index = s2->elems[j].index; |
| 2183 |
m->elems[m->nelem++].constraint = s2->elems[j].constraint & c2; |
| 2184 |
} |
| 2185 |
j++; |
| 2186 |
} |
| 2187 |
} |
| 2188 |
|
| 2189 |
/* Merge two sets of positions into a third. The result is exactly as if |
| 2190 |
the positions of both sets were inserted into an initially empty set. */ |
| 2191 |
static void |
| 2192 |
merge (position_set const *s1, position_set const *s2, position_set *m) |
| 2193 |
{ |
| 2194 |
merge_constrained (s1, s2, -1, m); |
| 2195 |
} |
| 2196 |
|
| 2197 |
/* Merge into DST all the elements of SRC, possibly destroying |
| 2198 |
the contents of the temporary M. */ |
| 2199 |
static void |
| 2200 |
merge2 (position_set *dst, position_set const *src, position_set *m) |
| 2201 |
{ |
| 2202 |
if (src->nelem < 4) |
| 2203 |
{ |
| 2204 |
for (idx_t i = 0; i < src->nelem; i++) |
| 2205 |
insert (src->elems[i], dst); |
| 2206 |
} |
| 2207 |
else |
| 2208 |
{ |
| 2209 |
merge (src, dst, m); |
| 2210 |
copy (m, dst); |
| 2211 |
} |
| 2212 |
} |
| 2213 |
|
| 2214 |
/* Delete a position from a set. Return the nonzero constraint of the |
| 2215 |
deleted position, or zero if there was no such position. */ |
| 2216 |
static unsigned int |
| 2217 |
delete (idx_t del, position_set *s) |
| 2218 |
{ |
| 2219 |
idx_t count = s->nelem; |
| 2220 |
idx_t lo = 0, hi = count; |
| 2221 |
while (lo < hi) |
| 2222 |
{ |
| 2223 |
idx_t mid = (lo + hi) >> 1; |
| 2224 |
if (s->elems[mid].index < del) |
| 2225 |
lo = mid + 1; |
| 2226 |
else if (s->elems[mid].index == del) |
| 2227 |
{ |
| 2228 |
unsigned int c = s->elems[mid].constraint; |
| 2229 |
idx_t i; |
| 2230 |
for (i = mid; i + 1 < count; i++) |
| 2231 |
s->elems[i] = s->elems[i + 1]; |
| 2232 |
s->nelem = i; |
| 2233 |
return c; |
| 2234 |
} |
| 2235 |
else |
| 2236 |
hi = mid; |
| 2237 |
} |
| 2238 |
return 0; |
| 2239 |
} |
| 2240 |
|
| 2241 |
/* Replace a position with the followed set. */ |
| 2242 |
static void |
| 2243 |
replace (position_set *dst, idx_t del, position_set *add, |
| 2244 |
unsigned int constraint, position_set *tmp) |
| 2245 |
{ |
| 2246 |
unsigned int c = delete (del, dst) & constraint; |
| 2247 |
|
| 2248 |
if (c) |
| 2249 |
{ |
| 2250 |
copy (dst, tmp); |
| 2251 |
merge_constrained (tmp, add, c, dst); |
| 2252 |
} |
| 2253 |
} |
| 2254 |
|
| 2255 |
/* Find the index of the state corresponding to the given position set with |
| 2256 |
the given preceding context, or create a new state if there is no such |
| 2257 |
state. Context tells whether we got here on a newline or letter. */ |
| 2258 |
static state_num |
| 2259 |
state_index (struct dfa *d, position_set const *s, int context) |
| 2260 |
{ |
| 2261 |
size_t hash = 0; |
| 2262 |
int constraint = 0; |
| 2263 |
state_num i; |
| 2264 |
|
| 2265 |
for (i = 0; i < s->nelem; ++i) |
| 2266 |
{ |
| 2267 |
idx_t ind = s->elems[i].index; |
| 2268 |
hash ^= ind + s->elems[i].constraint; |
| 2269 |
} |
| 2270 |
|
| 2271 |
/* Try to find a state that exactly matches the proposed one. */ |
| 2272 |
for (i = 0; i < d->sindex; ++i) |
| 2273 |
{ |
| 2274 |
if (hash != d->states[i].hash || s->nelem != d->states[i].elems.nelem |
| 2275 |
|| context != d->states[i].context) |
| 2276 |
continue; |
| 2277 |
state_num j; |
| 2278 |
for (j = 0; j < s->nelem; ++j) |
| 2279 |
if (s->elems[j].constraint != d->states[i].elems.elems[j].constraint |
| 2280 |
|| s->elems[j].index != d->states[i].elems.elems[j].index) |
| 2281 |
break; |
| 2282 |
if (j == s->nelem) |
| 2283 |
return i; |
| 2284 |
} |
| 2285 |
|
| 2286 |
#ifdef DEBUG |
| 2287 |
fprintf (stderr, "new state %td\n nextpos:", i); |
| 2288 |
for (state_num j = 0; j < s->nelem; j++) |
| 2289 |
{ |
| 2290 |
fprintf (stderr, " %td:", s->elems[j].index); |
| 2291 |
prtok (d->tokens[s->elems[j].index]); |
| 2292 |
} |
| 2293 |
fprintf (stderr, "\n context:"); |
| 2294 |
if (context ^ CTX_ANY) |
| 2295 |
{ |
| 2296 |
if (context & CTX_NONE) |
| 2297 |
fprintf (stderr, " CTX_NONE"); |
| 2298 |
if (context & CTX_LETTER) |
| 2299 |
fprintf (stderr, " CTX_LETTER"); |
| 2300 |
if (context & CTX_NEWLINE) |
| 2301 |
fprintf (stderr, " CTX_NEWLINE"); |
| 2302 |
} |
| 2303 |
else |
| 2304 |
fprintf (stderr, " CTX_ANY"); |
| 2305 |
fprintf (stderr, "\n"); |
| 2306 |
#endif |
| 2307 |
|
| 2308 |
for (state_num j = 0; j < s->nelem; j++) |
| 2309 |
{ |
| 2310 |
int c = d->constraints[s->elems[j].index]; |
| 2311 |
|
| 2312 |
if (c != 0) |
| 2313 |
{ |
| 2314 |
if (succeeds_in_context (c, context, CTX_ANY)) |
| 2315 |
constraint |= c; |
| 2316 |
} |
| 2317 |
else if (d->tokens[s->elems[j].index] == BACKREF) |
| 2318 |
constraint = NO_CONSTRAINT; |
| 2319 |
} |
| 2320 |
|
| 2321 |
|
| 2322 |
/* Create a new state. */ |
| 2323 |
d->states = maybe_realloc (d->states, d->sindex, &d->salloc, -1, |
| 2324 |
sizeof *d->states); |
| 2325 |
d->states[i].hash = hash; |
| 2326 |
alloc_position_set (&d->states[i].elems, s->nelem); |
| 2327 |
copy (s, &d->states[i].elems); |
| 2328 |
d->states[i].context = context; |
| 2329 |
d->states[i].constraint = constraint; |
| 2330 |
d->states[i].mbps.nelem = 0; |
| 2331 |
d->states[i].mbps.elems = NULL; |
| 2332 |
d->states[i].mb_trindex = -1; |
| 2333 |
|
| 2334 |
++d->sindex; |
| 2335 |
|
| 2336 |
return i; |
| 2337 |
} |
| 2338 |
|
| 2339 |
/* Find the epsilon closure of D's set of positions. If any position of the set |
| 2340 |
contains a symbol that matches the empty string in some context, replace |
| 2341 |
that position with the elements of its follow labeled with an appropriate |
| 2342 |
constraint. Repeat exhaustively until no funny positions are left. |
| 2343 |
S->elems must be large enough to hold the result. BACKWARD is D's |
| 2344 |
backward set; use and update it too. */ |
| 2345 |
static void |
| 2346 |
epsclosure (struct dfa const *d, position_set *backward) |
| 2347 |
{ |
| 2348 |
position_set tmp; |
| 2349 |
alloc_position_set (&tmp, d->nleaves); |
| 2350 |
for (idx_t i = 0; i < d->tindex; i++) |
| 2351 |
if (0 < d->follows[i].nelem) |
| 2352 |
{ |
| 2353 |
unsigned int constraint; |
| 2354 |
switch (d->tokens[i]) |
| 2355 |
{ |
| 2356 |
default: |
| 2357 |
continue; |
| 2358 |
|
| 2359 |
case BEGLINE: |
| 2360 |
constraint = BEGLINE_CONSTRAINT; |
| 2361 |
break; |
| 2362 |
case ENDLINE: |
| 2363 |
constraint = ENDLINE_CONSTRAINT; |
| 2364 |
break; |
| 2365 |
case BEGWORD: |
| 2366 |
constraint = BEGWORD_CONSTRAINT; |
| 2367 |
break; |
| 2368 |
case ENDWORD: |
| 2369 |
constraint = ENDWORD_CONSTRAINT; |
| 2370 |
break; |
| 2371 |
case LIMWORD: |
| 2372 |
constraint = LIMWORD_CONSTRAINT; |
| 2373 |
break; |
| 2374 |
case NOTLIMWORD: |
| 2375 |
constraint = NOTLIMWORD_CONSTRAINT; |
| 2376 |
break; |
| 2377 |
case EMPTY: |
| 2378 |
constraint = NO_CONSTRAINT; |
| 2379 |
break; |
| 2380 |
} |
| 2381 |
|
| 2382 |
delete (i, &d->follows[i]); |
| 2383 |
|
| 2384 |
for (idx_t j = 0; j < backward[i].nelem; j++) |
| 2385 |
replace (&d->follows[backward[i].elems[j].index], i, &d->follows[i], |
| 2386 |
constraint, &tmp); |
| 2387 |
for (idx_t j = 0; j < d->follows[i].nelem; j++) |
| 2388 |
replace (&backward[d->follows[i].elems[j].index], i, &backward[i], |
| 2389 |
NO_CONSTRAINT, &tmp); |
| 2390 |
} |
| 2391 |
free (tmp.elems); |
| 2392 |
} |
| 2393 |
|
| 2394 |
/* Returns the set of contexts for which there is at least one |
| 2395 |
character included in C. */ |
| 2396 |
|
| 2397 |
static int |
| 2398 |
charclass_context (struct dfa const *dfa, charclass const *c) |
| 2399 |
{ |
| 2400 |
int context = 0; |
| 2401 |
|
| 2402 |
for (int j = 0; j < CHARCLASS_WORDS; j++) |
| 2403 |
{ |
| 2404 |
if (c->w[j] & dfa->syntax.newline.w[j]) |
| 2405 |
context |= CTX_NEWLINE; |
| 2406 |
if (c->w[j] & dfa->syntax.letters.w[j]) |
| 2407 |
context |= CTX_LETTER; |
| 2408 |
if (c->w[j] & ~(dfa->syntax.letters.w[j] | dfa->syntax.newline.w[j])) |
| 2409 |
context |= CTX_NONE; |
| 2410 |
} |
| 2411 |
|
| 2412 |
return context; |
| 2413 |
} |
| 2414 |
|
| 2415 |
/* Returns the contexts on which the position set S depends. Each context |
| 2416 |
in the set of returned contexts (let's call it SC) may have a different |
| 2417 |
follow set than other contexts in SC, and also different from the |
| 2418 |
follow set of the complement set (sc ^ CTX_ANY). However, all contexts |
| 2419 |
in the complement set will have the same follow set. */ |
| 2420 |
|
| 2421 |
static int _GL_ATTRIBUTE_PURE |
| 2422 |
state_separate_contexts (struct dfa *d, position_set const *s) |
| 2423 |
{ |
| 2424 |
int separate_contexts = 0; |
| 2425 |
|
| 2426 |
for (idx_t j = 0; j < s->nelem; j++) |
| 2427 |
separate_contexts |= d->separates[s->elems[j].index]; |
| 2428 |
|
| 2429 |
return separate_contexts; |
| 2430 |
} |
| 2431 |
|
| 2432 |
enum |
| 2433 |
{ |
| 2434 |
/* Single token is repeated. It is distinguished from non-repeated. */ |
| 2435 |
OPT_REPEAT = (1 << 0), |
| 2436 |
|
| 2437 |
/* Multiple tokens are repeated. This flag is on at head of tokens. The |
| 2438 |
node is not merged. */ |
| 2439 |
OPT_LPAREN = (1 << 1), |
| 2440 |
|
| 2441 |
/* Multiple branches are joined. The node is not merged. */ |
| 2442 |
OPT_RPAREN = (1 << 2), |
| 2443 |
|
| 2444 |
/* The node is walked. If the node is found in walking again, OPT_RPAREN |
| 2445 |
flag is turned on. */ |
| 2446 |
OPT_WALKED = (1 << 3), |
| 2447 |
|
| 2448 |
/* The node is queued. The node is not queued again. */ |
| 2449 |
OPT_QUEUED = (1 << 4) |
| 2450 |
}; |
| 2451 |
|
| 2452 |
static void |
| 2453 |
merge_nfa_state (struct dfa *d, idx_t tindex, char *flags, |
| 2454 |
position_set *merged) |
| 2455 |
{ |
| 2456 |
position_set *follows = d->follows; |
| 2457 |
idx_t nelem = 0; |
| 2458 |
|
| 2459 |
for (idx_t i = 0; i < follows[tindex].nelem; i++) |
| 2460 |
{ |
| 2461 |
idx_t sindex = follows[tindex].elems[i].index; |
| 2462 |
|
| 2463 |
/* Skip the node as pruned in future. */ |
| 2464 |
unsigned int iconstraint = follows[tindex].elems[i].constraint; |
| 2465 |
if (iconstraint == 0) |
| 2466 |
continue; |
| 2467 |
|
| 2468 |
if (d->tokens[follows[tindex].elems[i].index] <= END) |
| 2469 |
{ |
| 2470 |
d->constraints[tindex] |= follows[tindex].elems[i].constraint; |
| 2471 |
continue; |
| 2472 |
} |
| 2473 |
|
| 2474 |
if (sindex != tindex && !(flags[sindex] & (OPT_LPAREN | OPT_RPAREN))) |
| 2475 |
{ |
| 2476 |
idx_t j; |
| 2477 |
|
| 2478 |
for (j = 0; j < nelem; j++) |
| 2479 |
{ |
| 2480 |
idx_t dindex = follows[tindex].elems[j].index; |
| 2481 |
|
| 2482 |
if (dindex == tindex) |
| 2483 |
continue; |
| 2484 |
|
| 2485 |
if (follows[tindex].elems[j].constraint != iconstraint) |
| 2486 |
continue; |
| 2487 |
|
| 2488 |
if (flags[dindex] & (OPT_LPAREN | OPT_RPAREN)) |
| 2489 |
continue; |
| 2490 |
|
| 2491 |
if (d->tokens[sindex] != d->tokens[dindex]) |
| 2492 |
continue; |
| 2493 |
|
| 2494 |
if ((flags[sindex] ^ flags[dindex]) & OPT_REPEAT) |
| 2495 |
continue; |
| 2496 |
|
| 2497 |
if (flags[sindex] & OPT_REPEAT) |
| 2498 |
delete (sindex, &follows[sindex]); |
| 2499 |
|
| 2500 |
merge2 (&follows[dindex], &follows[sindex], merged); |
| 2501 |
|
| 2502 |
break; |
| 2503 |
} |
| 2504 |
|
| 2505 |
if (j < nelem) |
| 2506 |
continue; |
| 2507 |
} |
| 2508 |
|
| 2509 |
follows[tindex].elems[nelem++] = follows[tindex].elems[i]; |
| 2510 |
flags[sindex] |= OPT_QUEUED; |
| 2511 |
} |
| 2512 |
|
| 2513 |
follows[tindex].nelem = nelem; |
| 2514 |
} |
| 2515 |
|
| 2516 |
static int |
| 2517 |
compare (const void *a, const void *b) |
| 2518 |
{ |
| 2519 |
position const *p = a, *q = b; |
| 2520 |
return (p->index > q->index) - (p->index < q->index); |
| 2521 |
} |
| 2522 |
|
| 2523 |
static void |
| 2524 |
reorder_tokens (struct dfa *d) |
| 2525 |
{ |
| 2526 |
idx_t nleaves = 0; |
| 2527 |
ptrdiff_t *map = xnmalloc (d->tindex, sizeof *map); |
| 2528 |
map[0] = nleaves++; |
| 2529 |
for (idx_t i = 1; i < d->tindex; i++) |
| 2530 |
map[i] = -1; |
| 2531 |
|
| 2532 |
token *tokens = xnmalloc (d->nleaves, sizeof *tokens); |
| 2533 |
position_set *follows = xnmalloc (d->nleaves, sizeof *follows); |
| 2534 |
int *constraints = xnmalloc (d->nleaves, sizeof *constraints); |
| 2535 |
char *multibyte_prop = (d->localeinfo.multibyte |
| 2536 |
? xnmalloc (d->nleaves, sizeof *multibyte_prop) |
| 2537 |
: NULL); |
| 2538 |
|
| 2539 |
for (idx_t i = 0; i < d->tindex; i++) |
| 2540 |
{ |
| 2541 |
if (map[i] < 0) |
| 2542 |
{ |
| 2543 |
free (d->follows[i].elems); |
| 2544 |
d->follows[i].elems = NULL; |
| 2545 |
d->follows[i].nelem = 0; |
| 2546 |
continue; |
| 2547 |
} |
| 2548 |
|
| 2549 |
tokens[map[i]] = d->tokens[i]; |
| 2550 |
follows[map[i]] = d->follows[i]; |
| 2551 |
constraints[map[i]] = d->constraints[i]; |
| 2552 |
|
| 2553 |
if (multibyte_prop != NULL) |
| 2554 |
multibyte_prop[map[i]] = d->multibyte_prop[i]; |
| 2555 |
|
| 2556 |
for (idx_t j = 0; j < d->follows[i].nelem; j++) |
| 2557 |
{ |
| 2558 |
if (map[d->follows[i].elems[j].index] == -1) |
| 2559 |
map[d->follows[i].elems[j].index] = nleaves++; |
| 2560 |
|
| 2561 |
d->follows[i].elems[j].index = map[d->follows[i].elems[j].index]; |
| 2562 |
} |
| 2563 |
|
| 2564 |
qsort (d->follows[i].elems, d->follows[i].nelem, |
| 2565 |
sizeof *d->follows[i].elems, compare); |
| 2566 |
} |
| 2567 |
|
| 2568 |
for (idx_t i = 0; i < nleaves; i++) |
| 2569 |
{ |
| 2570 |
d->tokens[i] = tokens[i]; |
| 2571 |
d->follows[i] = follows[i]; |
| 2572 |
d->constraints[i] = constraints[i]; |
| 2573 |
|
| 2574 |
if (multibyte_prop != NULL) |
| 2575 |
d->multibyte_prop[i] = multibyte_prop[i]; |
| 2576 |
} |
| 2577 |
|
| 2578 |
d->tindex = d->nleaves = nleaves; |
| 2579 |
|
| 2580 |
free (tokens); |
| 2581 |
free (follows); |
| 2582 |
free (constraints); |
| 2583 |
free (multibyte_prop); |
| 2584 |
free (map); |
| 2585 |
} |
| 2586 |
|
| 2587 |
static void |
| 2588 |
dfaoptimize (struct dfa *d) |
| 2589 |
{ |
| 2590 |
char *flags = xizalloc (d->tindex); |
| 2591 |
|
| 2592 |
for (idx_t i = 0; i < d->tindex; i++) |
| 2593 |
{ |
| 2594 |
for (idx_t j = 0; j < d->follows[i].nelem; j++) |
| 2595 |
{ |
| 2596 |
if (d->follows[i].elems[j].index == i) |
| 2597 |
flags[d->follows[i].elems[j].index] |= OPT_REPEAT; |
| 2598 |
else if (d->follows[i].elems[j].index < i) |
| 2599 |
flags[d->follows[i].elems[j].index] |= OPT_LPAREN; |
| 2600 |
else if (flags[d->follows[i].elems[j].index] &= OPT_WALKED) |
| 2601 |
flags[d->follows[i].elems[j].index] |= OPT_RPAREN; |
| 2602 |
else |
| 2603 |
flags[d->follows[i].elems[j].index] |= OPT_WALKED; |
| 2604 |
} |
| 2605 |
} |
| 2606 |
|
| 2607 |
flags[0] |= OPT_QUEUED; |
| 2608 |
|
| 2609 |
position_set merged0; |
| 2610 |
position_set *merged = &merged0; |
| 2611 |
alloc_position_set (merged, d->nleaves); |
| 2612 |
|
| 2613 |
d->constraints = xicalloc (d->tindex, sizeof *d->constraints); |
| 2614 |
|
| 2615 |
for (idx_t i = 0; i < d->tindex; i++) |
| 2616 |
if (flags[i] & OPT_QUEUED) |
| 2617 |
merge_nfa_state (d, i, flags, merged); |
| 2618 |
|
| 2619 |
reorder_tokens (d); |
| 2620 |
|
| 2621 |
free (merged->elems); |
| 2622 |
free (flags); |
| 2623 |
} |
| 2624 |
|
| 2625 |
/* Perform bottom-up analysis on the parse tree, computing various functions. |
| 2626 |
Note that at this point, we're pretending constructs like \< are real |
| 2627 |
characters rather than constraints on what can follow them. |
| 2628 |
|
| 2629 |
Nullable: A node is nullable if it is at the root of a regexp that can |
| 2630 |
match the empty string. |
| 2631 |
* EMPTY leaves are nullable. |
| 2632 |
* No other leaf is nullable. |
| 2633 |
* A QMARK or STAR node is nullable. |
| 2634 |
* A PLUS node is nullable if its argument is nullable. |
| 2635 |
* A CAT node is nullable if both its arguments are nullable. |
| 2636 |
* An OR node is nullable if either argument is nullable. |
| 2637 |
|
| 2638 |
Firstpos: The firstpos of a node is the set of positions (nonempty leaves) |
| 2639 |
that could correspond to the first character of a string matching the |
| 2640 |
regexp rooted at the given node. |
| 2641 |
* EMPTY leaves have empty firstpos. |
| 2642 |
* The firstpos of a nonempty leaf is that leaf itself. |
| 2643 |
* The firstpos of a QMARK, STAR, or PLUS node is the firstpos of its |
| 2644 |
argument. |
| 2645 |
* The firstpos of a CAT node is the firstpos of the left argument, union |
| 2646 |
the firstpos of the right if the left argument is nullable. |
| 2647 |
* The firstpos of an OR node is the union of firstpos of each argument. |
| 2648 |
|
| 2649 |
Lastpos: The lastpos of a node is the set of positions that could |
| 2650 |
correspond to the last character of a string matching the regexp at |
| 2651 |
the given node. |
| 2652 |
* EMPTY leaves have empty lastpos. |
| 2653 |
* The lastpos of a nonempty leaf is that leaf itself. |
| 2654 |
* The lastpos of a QMARK, STAR, or PLUS node is the lastpos of its |
| 2655 |
argument. |
| 2656 |
* The lastpos of a CAT node is the lastpos of its right argument, union |
| 2657 |
the lastpos of the left if the right argument is nullable. |
| 2658 |
* The lastpos of an OR node is the union of the lastpos of each argument. |
| 2659 |
|
| 2660 |
Follow: The follow of a position is the set of positions that could |
| 2661 |
correspond to the character following a character matching the node in |
| 2662 |
a string matching the regexp. At this point we consider special symbols |
| 2663 |
that match the empty string in some context to be just normal characters. |
| 2664 |
Later, if we find that a special symbol is in a follow set, we will |
| 2665 |
replace it with the elements of its follow, labeled with an appropriate |
| 2666 |
constraint. |
| 2667 |
* Every node in the firstpos of the argument of a STAR or PLUS node is in |
| 2668 |
the follow of every node in the lastpos. |
| 2669 |
* Every node in the firstpos of the second argument of a CAT node is in |
| 2670 |
the follow of every node in the lastpos of the first argument. |
| 2671 |
|
| 2672 |
Because of the postfix representation of the parse tree, the depth-first |
| 2673 |
analysis is conveniently done by a linear scan with the aid of a stack. |
| 2674 |
Sets are stored as arrays of the elements, obeying a stack-like allocation |
| 2675 |
scheme; the number of elements in each set deeper in the stack can be |
| 2676 |
used to determine the address of a particular set's array. */ |
| 2677 |
static void |
| 2678 |
dfaanalyze (struct dfa *d, bool searchflag) |
| 2679 |
{ |
| 2680 |
/* Array allocated to hold position sets. */ |
| 2681 |
position *posalloc = xnmalloc (d->nleaves, 2 * sizeof *posalloc); |
| 2682 |
/* Firstpos and lastpos elements. */ |
| 2683 |
position *firstpos = posalloc; |
| 2684 |
position *lastpos = firstpos + d->nleaves; |
| 2685 |
position pos; |
| 2686 |
position_set tmp; |
| 2687 |
|
| 2688 |
/* Stack for element counts and nullable flags. */ |
| 2689 |
struct |
| 2690 |
{ |
| 2691 |
/* Whether the entry is nullable. */ |
| 2692 |
bool nullable; |
| 2693 |
|
| 2694 |
/* Counts of firstpos and lastpos sets. */ |
| 2695 |
idx_t nfirstpos; |
| 2696 |
idx_t nlastpos; |
| 2697 |
} *stkalloc = xnmalloc (d->depth, sizeof *stkalloc), *stk = stkalloc; |
| 2698 |
|
| 2699 |
position_set merged; /* Result of merging sets. */ |
| 2700 |
|
| 2701 |
addtok (d, CAT); |
| 2702 |
idx_t tindex = d->tindex; |
| 2703 |
|
| 2704 |
#ifdef DEBUG |
| 2705 |
fprintf (stderr, "dfaanalyze:\n"); |
| 2706 |
for (idx_t i = 0; i < tindex; i++) |
| 2707 |
{ |
| 2708 |
fprintf (stderr, " %td:", i); |
| 2709 |
prtok (d->tokens[i]); |
| 2710 |
} |
| 2711 |
putc ('\n', stderr); |
| 2712 |
#endif |
| 2713 |
|
| 2714 |
d->searchflag = searchflag; |
| 2715 |
alloc_position_set (&merged, d->nleaves); |
| 2716 |
d->follows = xicalloc (tindex, sizeof *d->follows); |
| 2717 |
position_set *backward |
| 2718 |
= d->epsilon ? xicalloc (tindex, sizeof *backward) : NULL; |
| 2719 |
|
| 2720 |
for (idx_t i = 0; i < tindex; i++) |
| 2721 |
{ |
| 2722 |
switch (d->tokens[i]) |
| 2723 |
{ |
| 2724 |
case EMPTY: |
| 2725 |
/* The empty set is nullable. */ |
| 2726 |
stk->nullable = true; |
| 2727 |
|
| 2728 |
/* The firstpos and lastpos of the empty leaf are both empty. */ |
| 2729 |
stk->nfirstpos = stk->nlastpos = 0; |
| 2730 |
stk++; |
| 2731 |
break; |
| 2732 |
|
| 2733 |
case STAR: |
| 2734 |
case PLUS: |
| 2735 |
/* Every element in the lastpos of the argument is in the backward |
| 2736 |
set of every element in the firstpos. */ |
| 2737 |
if (d->epsilon) |
| 2738 |
{ |
| 2739 |
tmp.elems = lastpos - stk[-1].nlastpos; |
| 2740 |
tmp.nelem = stk[-1].nlastpos; |
| 2741 |
for (position *p = firstpos - stk[-1].nfirstpos; |
| 2742 |
p < firstpos; p++) |
| 2743 |
merge2 (&backward[p->index], &tmp, &merged); |
| 2744 |
} |
| 2745 |
|
| 2746 |
/* Every element in the firstpos of the argument is in the follow |
| 2747 |
of every element in the lastpos. */ |
| 2748 |
{ |
| 2749 |
tmp.elems = firstpos - stk[-1].nfirstpos; |
| 2750 |
tmp.nelem = stk[-1].nfirstpos; |
| 2751 |
for (position *p = lastpos - stk[-1].nlastpos; p < lastpos; p++) |
| 2752 |
merge2 (&d->follows[p->index], &tmp, &merged); |
| 2753 |
} |
| 2754 |
FALLTHROUGH; |
| 2755 |
case QMARK: |
| 2756 |
/* A QMARK or STAR node is automatically nullable. */ |
| 2757 |
if (d->tokens[i] != PLUS) |
| 2758 |
stk[-1].nullable = true; |
| 2759 |
break; |
| 2760 |
|
| 2761 |
case CAT: |
| 2762 |
/* Every element in the lastpos of the first argument is in |
| 2763 |
the backward set of every element in the firstpos of the |
| 2764 |
second argument. */ |
| 2765 |
if (backward) |
| 2766 |
{ |
| 2767 |
tmp.nelem = stk[-2].nlastpos; |
| 2768 |
tmp.elems = lastpos - stk[-1].nlastpos - stk[-2].nlastpos; |
| 2769 |
for (position *p = firstpos - stk[-1].nfirstpos; |
| 2770 |
p < firstpos; p++) |
| 2771 |
merge2 (&backward[p->index], &tmp, &merged); |
| 2772 |
} |
| 2773 |
|
| 2774 |
/* Every element in the firstpos of the second argument is in the |
| 2775 |
follow of every element in the lastpos of the first argument. */ |
| 2776 |
{ |
| 2777 |
tmp.nelem = stk[-1].nfirstpos; |
| 2778 |
tmp.elems = firstpos - stk[-1].nfirstpos; |
| 2779 |
for (position *plim = lastpos - stk[-1].nlastpos, |
| 2780 |
*p = plim - stk[-2].nlastpos; |
| 2781 |
p < plim; p++) |
| 2782 |
merge2 (&d->follows[p->index], &tmp, &merged); |
| 2783 |
} |
| 2784 |
|
| 2785 |
/* The firstpos of a CAT node is the firstpos of the first argument, |
| 2786 |
union that of the second argument if the first is nullable. */ |
| 2787 |
if (stk[-2].nullable) |
| 2788 |
stk[-2].nfirstpos += stk[-1].nfirstpos; |
| 2789 |
else |
| 2790 |
firstpos -= stk[-1].nfirstpos; |
| 2791 |
|
| 2792 |
/* The lastpos of a CAT node is the lastpos of the second argument, |
| 2793 |
union that of the first argument if the second is nullable. */ |
| 2794 |
if (stk[-1].nullable) |
| 2795 |
stk[-2].nlastpos += stk[-1].nlastpos; |
| 2796 |
else |
| 2797 |
{ |
| 2798 |
position *p = lastpos - stk[-1].nlastpos - stk[-2].nlastpos; |
| 2799 |
for (idx_t j = 0; j < stk[-1].nlastpos; j++) |
| 2800 |
p[j] = p[j + stk[-2].nlastpos]; |
| 2801 |
lastpos -= stk[-2].nlastpos; |
| 2802 |
stk[-2].nlastpos = stk[-1].nlastpos; |
| 2803 |
} |
| 2804 |
|
| 2805 |
/* A CAT node is nullable if both arguments are nullable. */ |
| 2806 |
stk[-2].nullable &= stk[-1].nullable; |
| 2807 |
stk--; |
| 2808 |
break; |
| 2809 |
|
| 2810 |
case OR: |
| 2811 |
/* The firstpos is the union of the firstpos of each argument. */ |
| 2812 |
stk[-2].nfirstpos += stk[-1].nfirstpos; |
| 2813 |
|
| 2814 |
/* The lastpos is the union of the lastpos of each argument. */ |
| 2815 |
stk[-2].nlastpos += stk[-1].nlastpos; |
| 2816 |
|
| 2817 |
/* An OR node is nullable if either argument is nullable. */ |
| 2818 |
stk[-2].nullable |= stk[-1].nullable; |
| 2819 |
stk--; |
| 2820 |
break; |
| 2821 |
|
| 2822 |
default: |
| 2823 |
/* Anything else is a nonempty position. (Note that special |
| 2824 |
constructs like \< are treated as nonempty strings here; |
| 2825 |
an "epsilon closure" effectively makes them nullable later. |
| 2826 |
Backreferences have to get a real position so we can detect |
| 2827 |
transitions on them later. But they are nullable. */ |
| 2828 |
stk->nullable = d->tokens[i] == BACKREF; |
| 2829 |
|
| 2830 |
/* This position is in its own firstpos and lastpos. */ |
| 2831 |
stk->nfirstpos = stk->nlastpos = 1; |
| 2832 |
stk++; |
| 2833 |
|
| 2834 |
firstpos->index = lastpos->index = i; |
| 2835 |
firstpos->constraint = lastpos->constraint = NO_CONSTRAINT; |
| 2836 |
firstpos++, lastpos++; |
| 2837 |
|
| 2838 |
break; |
| 2839 |
} |
| 2840 |
#ifdef DEBUG |
| 2841 |
/* ... balance the above nonsyntactic #ifdef goo... */ |
| 2842 |
fprintf (stderr, "node %td:", i); |
| 2843 |
prtok (d->tokens[i]); |
| 2844 |
putc ('\n', stderr); |
| 2845 |
fprintf (stderr, |
| 2846 |
stk[-1].nullable ? " nullable: yes\n" : " nullable: no\n"); |
| 2847 |
fprintf (stderr, " firstpos:"); |
| 2848 |
for (idx_t j = 0; j < stk[-1].nfirstpos; j++) |
| 2849 |
{ |
| 2850 |
fprintf (stderr, " %td:", firstpos[j - stk[-1].nfirstpos].index); |
| 2851 |
prtok (d->tokens[firstpos[j - stk[-1].nfirstpos].index]); |
| 2852 |
} |
| 2853 |
fprintf (stderr, "\n lastpos:"); |
| 2854 |
for (idx_t j = 0; j < stk[-1].nlastpos; j++) |
| 2855 |
{ |
| 2856 |
fprintf (stderr, " %td:", lastpos[j - stk[-1].nlastpos].index); |
| 2857 |
prtok (d->tokens[lastpos[j - stk[-1].nlastpos].index]); |
| 2858 |
} |
| 2859 |
putc ('\n', stderr); |
| 2860 |
#endif |
| 2861 |
} |
| 2862 |
|
| 2863 |
if (backward) |
| 2864 |
{ |
| 2865 |
/* For each follow set that is the follow set of a real position, |
| 2866 |
replace it with its epsilon closure. */ |
| 2867 |
epsclosure (d, backward); |
| 2868 |
|
| 2869 |
for (idx_t i = 0; i < tindex; i++) |
| 2870 |
free (backward[i].elems); |
| 2871 |
free (backward); |
| 2872 |
} |
| 2873 |
|
| 2874 |
dfaoptimize (d); |
| 2875 |
|
| 2876 |
#ifdef DEBUG |
| 2877 |
for (idx_t i = 0; i < tindex; i++) |
| 2878 |
if (d->tokens[i] == BEG || d->tokens[i] < NOTCHAR |
| 2879 |
|| d->tokens[i] == BACKREF || d->tokens[i] == ANYCHAR |
| 2880 |
|| d->tokens[i] == MBCSET || d->tokens[i] >= CSET) |
| 2881 |
{ |
| 2882 |
fprintf (stderr, "follows(%td:", i); |
| 2883 |
prtok (d->tokens[i]); |
| 2884 |
fprintf (stderr, "):"); |
| 2885 |
for (idx_t j = 0; j < d->follows[i].nelem; j++) |
| 2886 |
{ |
| 2887 |
fprintf (stderr, " %td:", d->follows[i].elems[j].index); |
| 2888 |
prtok (d->tokens[d->follows[i].elems[j].index]); |
| 2889 |
} |
| 2890 |
putc ('\n', stderr); |
| 2891 |
} |
| 2892 |
#endif |
| 2893 |
|
| 2894 |
pos.index = 0; |
| 2895 |
pos.constraint = NO_CONSTRAINT; |
| 2896 |
|
| 2897 |
alloc_position_set (&tmp, 1); |
| 2898 |
|
| 2899 |
append (pos, &tmp); |
| 2900 |
|
| 2901 |
d->separates = xicalloc (tindex, sizeof *d->separates); |
| 2902 |
|
| 2903 |
for (idx_t i = 0; i < tindex; i++) |
| 2904 |
{ |
| 2905 |
if (prev_newline_dependent (d->constraints[i])) |
| 2906 |
d->separates[i] |= CTX_NEWLINE; |
| 2907 |
if (prev_letter_dependent (d->constraints[i])) |
| 2908 |
d->separates[i] |= CTX_LETTER; |
| 2909 |
|
| 2910 |
for (idx_t j = 0; j < d->follows[i].nelem; j++) |
| 2911 |
{ |
| 2912 |
if (prev_newline_dependent (d->follows[i].elems[j].constraint)) |
| 2913 |
d->separates[i] |= CTX_NEWLINE; |
| 2914 |
if (prev_letter_dependent (d->follows[i].elems[j].constraint)) |
| 2915 |
d->separates[i] |= CTX_LETTER; |
| 2916 |
} |
| 2917 |
} |
| 2918 |
|
| 2919 |
/* Context wanted by some position. */ |
| 2920 |
int separate_contexts = state_separate_contexts (d, &tmp); |
| 2921 |
|
| 2922 |
/* Build the initial state. */ |
| 2923 |
if (separate_contexts & CTX_NEWLINE) |
| 2924 |
state_index (d, &tmp, CTX_NEWLINE); |
| 2925 |
d->initstate_notbol = d->min_trcount |
| 2926 |
= state_index (d, &tmp, separate_contexts ^ CTX_ANY); |
| 2927 |
if (separate_contexts & CTX_LETTER) |
| 2928 |
d->min_trcount = state_index (d, &tmp, CTX_LETTER); |
| 2929 |
d->min_trcount++; |
| 2930 |
d->trcount = 0; |
| 2931 |
|
| 2932 |
free (posalloc); |
| 2933 |
free (stkalloc); |
| 2934 |
free (merged.elems); |
| 2935 |
free (tmp.elems); |
| 2936 |
} |
| 2937 |
|
| 2938 |
/* Make sure D's state arrays are large enough to hold NEW_STATE. */ |
| 2939 |
static void |
| 2940 |
realloc_trans_if_necessary (struct dfa *d) |
| 2941 |
{ |
| 2942 |
state_num oldalloc = d->tralloc; |
| 2943 |
if (oldalloc < d->sindex) |
| 2944 |
{ |
| 2945 |
state_num **realtrans = d->trans ? d->trans - 2 : NULL; |
| 2946 |
idx_t newalloc1 = realtrans ? d->tralloc + 2 : 0; |
| 2947 |
realtrans = xpalloc (realtrans, &newalloc1, d->sindex - oldalloc, |
| 2948 |
-1, sizeof *realtrans); |
| 2949 |
realtrans[0] = realtrans[1] = NULL; |
| 2950 |
d->trans = realtrans + 2; |
| 2951 |
idx_t newalloc = d->tralloc = newalloc1 - 2; |
| 2952 |
d->fails = xreallocarray (d->fails, newalloc, sizeof *d->fails); |
| 2953 |
d->success = xreallocarray (d->success, newalloc, sizeof *d->success); |
| 2954 |
d->newlines = xreallocarray (d->newlines, newalloc, sizeof *d->newlines); |
| 2955 |
if (d->localeinfo.multibyte) |
| 2956 |
{ |
| 2957 |
realtrans = d->mb_trans ? d->mb_trans - 2 : NULL; |
| 2958 |
realtrans = xreallocarray (realtrans, newalloc1, sizeof *realtrans); |
| 2959 |
if (oldalloc == 0) |
| 2960 |
realtrans[0] = realtrans[1] = NULL; |
| 2961 |
d->mb_trans = realtrans + 2; |
| 2962 |
} |
| 2963 |
for (; oldalloc < newalloc; oldalloc++) |
| 2964 |
{ |
| 2965 |
d->trans[oldalloc] = NULL; |
| 2966 |
d->fails[oldalloc] = NULL; |
| 2967 |
if (d->localeinfo.multibyte) |
| 2968 |
d->mb_trans[oldalloc] = NULL; |
| 2969 |
} |
| 2970 |
} |
| 2971 |
} |
| 2972 |
|
| 2973 |
/* |
| 2974 |
Calculate the transition table for a new state derived from state s |
| 2975 |
for a compiled dfa d after input character uc, and return the new |
| 2976 |
state number. |
| 2977 |
|
| 2978 |
Do not worry about all possible input characters; calculate just the group |
| 2979 |
of positions that match uc. Label it with the set of characters that |
| 2980 |
every position in the group matches (taking into account, if necessary, |
| 2981 |
preceding context information of s). Then find the union |
| 2982 |
of these positions' follows, i.e., the set of positions of the |
| 2983 |
new state. For each character in the group's label, set the transition |
| 2984 |
on this character to be to a state corresponding to the set's positions, |
| 2985 |
and its associated backward context information, if necessary. |
| 2986 |
|
| 2987 |
When building a searching matcher, include the positions of state |
| 2988 |
0 in every state. |
| 2989 |
|
| 2990 |
The group is constructed by building an equivalence-class |
| 2991 |
partition of the positions of s. |
| 2992 |
|
| 2993 |
For each position, find the set of characters C that it matches. Eliminate |
| 2994 |
any characters from C that fail on grounds of backward context. |
| 2995 |
|
| 2996 |
Check whether the group's label L has nonempty |
| 2997 |
intersection with C. If L - C is nonempty, create a new group labeled |
| 2998 |
L - C and having the same positions as the current group, and set L to |
| 2999 |
the intersection of L and C. Insert the position in the group, set |
| 3000 |
C = C - L, and resume scanning. |
| 3001 |
|
| 3002 |
If after comparing with every group there are characters remaining in C, |
| 3003 |
create a new group labeled with the characters of C and insert this |
| 3004 |
position in that group. */ |
| 3005 |
|
| 3006 |
static state_num |
| 3007 |
build_state (state_num s, struct dfa *d, unsigned char uc) |
| 3008 |
{ |
| 3009 |
position_set follows; /* Union of the follows for each |
| 3010 |
position of the current state. */ |
| 3011 |
position_set group; /* Positions that match the input char. */ |
| 3012 |
position_set tmp; /* Temporary space for merging sets. */ |
| 3013 |
state_num state; /* New state. */ |
| 3014 |
state_num state_newline; /* New state on a newline transition. */ |
| 3015 |
state_num state_letter; /* New state on a letter transition. */ |
| 3016 |
|
| 3017 |
#ifdef DEBUG |
| 3018 |
fprintf (stderr, "build state %td\n", s); |
| 3019 |
#endif |
| 3020 |
|
| 3021 |
/* A pointer to the new transition table, and the table itself. */ |
| 3022 |
state_num **ptrans = (accepting (s, d) ? d->fails : d->trans) + s; |
| 3023 |
state_num *trans = *ptrans; |
| 3024 |
|
| 3025 |
if (!trans) |
| 3026 |
{ |
| 3027 |
/* MAX_TRCOUNT is an arbitrary upper limit on the number of |
| 3028 |
transition tables that can exist at once, other than for |
| 3029 |
initial states. Often-used transition tables are quickly |
| 3030 |
rebuilt, whereas rarely-used ones are cleared away. */ |
| 3031 |
if (MAX_TRCOUNT <= d->trcount) |
| 3032 |
{ |
| 3033 |
for (state_num i = d->min_trcount; i < d->tralloc; i++) |
| 3034 |
{ |
| 3035 |
free (d->trans[i]); |
| 3036 |
free (d->fails[i]); |
| 3037 |
d->trans[i] = d->fails[i] = NULL; |
| 3038 |
} |
| 3039 |
d->trcount = 0; |
| 3040 |
} |
| 3041 |
|
| 3042 |
d->trcount++; |
| 3043 |
*ptrans = trans = xmalloc (NOTCHAR * sizeof *trans); |
| 3044 |
|
| 3045 |
/* Fill transition table with a default value which means that the |
| 3046 |
transited state has not been calculated yet. */ |
| 3047 |
for (int i = 0; i < NOTCHAR; i++) |
| 3048 |
trans[i] = -2; |
| 3049 |
} |
| 3050 |
|
| 3051 |
/* Set up the success bits for this state. */ |
| 3052 |
d->success[s] = 0; |
| 3053 |
if (accepts_in_context (d->states[s].context, CTX_NEWLINE, s, d)) |
| 3054 |
d->success[s] |= CTX_NEWLINE; |
| 3055 |
if (accepts_in_context (d->states[s].context, CTX_LETTER, s, d)) |
| 3056 |
d->success[s] |= CTX_LETTER; |
| 3057 |
if (accepts_in_context (d->states[s].context, CTX_NONE, s, d)) |
| 3058 |
d->success[s] |= CTX_NONE; |
| 3059 |
|
| 3060 |
alloc_position_set (&follows, d->nleaves); |
| 3061 |
|
| 3062 |
/* Find the union of the follows of the positions of the group. |
| 3063 |
This is a hideously inefficient loop. Fix it someday. */ |
| 3064 |
for (idx_t j = 0; j < d->states[s].elems.nelem; j++) |
| 3065 |
for (idx_t k = 0; |
| 3066 |
k < d->follows[d->states[s].elems.elems[j].index].nelem; ++k) |
| 3067 |
insert (d->follows[d->states[s].elems.elems[j].index].elems[k], |
| 3068 |
&follows); |
| 3069 |
|
| 3070 |
/* Positions that match the input char. */ |
| 3071 |
alloc_position_set (&group, d->nleaves); |
| 3072 |
|
| 3073 |
/* The group's label. */ |
| 3074 |
charclass label; |
| 3075 |
fillset (&label); |
| 3076 |
|
| 3077 |
for (idx_t i = 0; i < follows.nelem; i++) |
| 3078 |
{ |
| 3079 |
charclass matches; /* Set of matching characters. */ |
| 3080 |
position pos = follows.elems[i]; |
| 3081 |
bool matched = false; |
| 3082 |
if (d->tokens[pos.index] >= 0 && d->tokens[pos.index] < NOTCHAR) |
| 3083 |
{ |
| 3084 |
zeroset (&matches); |
| 3085 |
setbit (d->tokens[pos.index], &matches); |
| 3086 |
if (d->tokens[pos.index] == uc) |
| 3087 |
matched = true; |
| 3088 |
} |
| 3089 |
else if (d->tokens[pos.index] >= CSET) |
| 3090 |
{ |
| 3091 |
matches = d->charclasses[d->tokens[pos.index] - CSET]; |
| 3092 |
if (tstbit (uc, &matches)) |
| 3093 |
matched = true; |
| 3094 |
} |
| 3095 |
else if (d->tokens[pos.index] == ANYCHAR) |
| 3096 |
{ |
| 3097 |
matches = d->charclasses[d->canychar]; |
| 3098 |
if (tstbit (uc, &matches)) |
| 3099 |
matched = true; |
| 3100 |
|
| 3101 |
/* ANYCHAR must match with a single character, so we must put |
| 3102 |
it to D->states[s].mbps which contains the positions which |
| 3103 |
can match with a single character not a byte. If all |
| 3104 |
positions which has ANYCHAR does not depend on context of |
| 3105 |
next character, we put the follows instead of it to |
| 3106 |
D->states[s].mbps to optimize. */ |
| 3107 |
if (succeeds_in_context (pos.constraint, d->states[s].context, |
| 3108 |
CTX_NONE)) |
| 3109 |
{ |
| 3110 |
if (d->states[s].mbps.nelem == 0) |
| 3111 |
alloc_position_set (&d->states[s].mbps, 1); |
| 3112 |
insert (pos, &d->states[s].mbps); |
| 3113 |
} |
| 3114 |
} |
| 3115 |
else |
| 3116 |
continue; |
| 3117 |
|
| 3118 |
/* Some characters may need to be eliminated from matches because |
| 3119 |
they fail in the current context. */ |
| 3120 |
if (pos.constraint != NO_CONSTRAINT) |
| 3121 |
{ |
| 3122 |
if (!succeeds_in_context (pos.constraint, |
| 3123 |
d->states[s].context, CTX_NEWLINE)) |
| 3124 |
for (int j = 0; j < CHARCLASS_WORDS; j++) |
| 3125 |
matches.w[j] &= ~d->syntax.newline.w[j]; |
| 3126 |
if (!succeeds_in_context (pos.constraint, |
| 3127 |
d->states[s].context, CTX_LETTER)) |
| 3128 |
for (int j = 0; j < CHARCLASS_WORDS; ++j) |
| 3129 |
matches.w[j] &= ~d->syntax.letters.w[j]; |
| 3130 |
if (!succeeds_in_context (pos.constraint, |
| 3131 |
d->states[s].context, CTX_NONE)) |
| 3132 |
for (int j = 0; j < CHARCLASS_WORDS; ++j) |
| 3133 |
matches.w[j] &= d->syntax.letters.w[j] | d->syntax.newline.w[j]; |
| 3134 |
|
| 3135 |
/* If there are no characters left, there's no point in going on. */ |
| 3136 |
if (emptyset (&matches)) |
| 3137 |
continue; |
| 3138 |
|
| 3139 |
/* If we have reset the bit that made us declare "matched", reset |
| 3140 |
that indicator, too. This is required to avoid an infinite loop |
| 3141 |
with this command: echo cx | LC_ALL=C grep -E 'c\b[x ]' */ |
| 3142 |
if (!tstbit (uc, &matches)) |
| 3143 |
matched = false; |
| 3144 |
} |
| 3145 |
|
| 3146 |
#ifdef DEBUG |
| 3147 |
fprintf (stderr, " nextpos %td:", pos.index); |
| 3148 |
prtok (d->tokens[pos.index]); |
| 3149 |
fprintf (stderr, " of"); |
| 3150 |
for (unsigned j = 0; j < NOTCHAR; j++) |
| 3151 |
if (tstbit (j, &matches)) |
| 3152 |
fprintf (stderr, " 0x%02x", j); |
| 3153 |
fprintf (stderr, "\n"); |
| 3154 |
#endif |
| 3155 |
|
| 3156 |
if (matched) |
| 3157 |
{ |
| 3158 |
for (int k = 0; k < CHARCLASS_WORDS; ++k) |
| 3159 |
label.w[k] &= matches.w[k]; |
| 3160 |
append (pos, &group); |
| 3161 |
} |
| 3162 |
else |
| 3163 |
{ |
| 3164 |
for (int k = 0; k < CHARCLASS_WORDS; ++k) |
| 3165 |
label.w[k] &= ~matches.w[k]; |
| 3166 |
} |
| 3167 |
} |
| 3168 |
|
| 3169 |
alloc_position_set (&tmp, d->nleaves); |
| 3170 |
|
| 3171 |
if (group.nelem > 0) |
| 3172 |
{ |
| 3173 |
/* If we are building a searching matcher, throw in the positions |
| 3174 |
of state 0 as well, if possible. */ |
| 3175 |
if (d->searchflag) |
| 3176 |
{ |
| 3177 |
/* If a token in follows.elems is not 1st byte of a multibyte |
| 3178 |
character, or the states of follows must accept the bytes |
| 3179 |
which are not 1st byte of the multibyte character. |
| 3180 |
Then, if a state of follows encounters a byte, it must not be |
| 3181 |
a 1st byte of a multibyte character nor a single byte character. |
| 3182 |
In this case, do not add state[0].follows to next state, because |
| 3183 |
state[0] must accept 1st-byte. |
| 3184 |
|
| 3185 |
For example, suppose <sb a> is a certain single byte character, |
| 3186 |
<mb A> is a certain multibyte character, and the codepoint of |
| 3187 |
<sb a> equals the 2nd byte of the codepoint of <mb A>. When |
| 3188 |
state[0] accepts <sb a>, state[i] transits to state[i+1] by |
| 3189 |
accepting the 1st byte of <mb A>, and state[i+1] accepts the |
| 3190 |
2nd byte of <mb A>, if state[i+1] encounters the codepoint of |
| 3191 |
<sb a>, it must not be <sb a> but the 2nd byte of <mb A>, so do |
| 3192 |
not add state[0]. */ |
| 3193 |
|
| 3194 |
bool mergeit = !d->localeinfo.multibyte; |
| 3195 |
if (!mergeit) |
| 3196 |
{ |
| 3197 |
mergeit = true; |
| 3198 |
for (idx_t j = 0; mergeit && j < group.nelem; j++) |
| 3199 |
mergeit &= d->multibyte_prop[group.elems[j].index]; |
| 3200 |
} |
| 3201 |
if (mergeit) |
| 3202 |
merge2 (&group, &d->states[0].elems, &tmp); |
| 3203 |
} |
| 3204 |
|
| 3205 |
/* Find out if the new state will want any context information, |
| 3206 |
by calculating possible contexts that the group can match, |
| 3207 |
and separate contexts that the new state wants to know. */ |
| 3208 |
int possible_contexts = charclass_context (d, &label); |
| 3209 |
int separate_contexts = state_separate_contexts (d, &group); |
| 3210 |
|
| 3211 |
/* Find the state(s) corresponding to the union of the follows. */ |
| 3212 |
if (possible_contexts & ~separate_contexts) |
| 3213 |
state = state_index (d, &group, separate_contexts ^ CTX_ANY); |
| 3214 |
else |
| 3215 |
state = -1; |
| 3216 |
if (separate_contexts & possible_contexts & CTX_NEWLINE) |
| 3217 |
state_newline = state_index (d, &group, CTX_NEWLINE); |
| 3218 |
else |
| 3219 |
state_newline = state; |
| 3220 |
if (separate_contexts & possible_contexts & CTX_LETTER) |
| 3221 |
state_letter = state_index (d, &group, CTX_LETTER); |
| 3222 |
else |
| 3223 |
state_letter = state; |
| 3224 |
|
| 3225 |
/* Reallocate now, to reallocate any newline transition properly. */ |
| 3226 |
realloc_trans_if_necessary (d); |
| 3227 |
} |
| 3228 |
|
| 3229 |
/* If we are a searching matcher, the default transition is to a state |
| 3230 |
containing the positions of state 0, otherwise the default transition |
| 3231 |
is to fail miserably. */ |
| 3232 |
else if (d->searchflag) |
| 3233 |
{ |
| 3234 |
state_newline = 0; |
| 3235 |
state_letter = d->min_trcount - 1; |
| 3236 |
state = d->initstate_notbol; |
| 3237 |
} |
| 3238 |
else |
| 3239 |
{ |
| 3240 |
state_newline = -1; |
| 3241 |
state_letter = -1; |
| 3242 |
state = -1; |
| 3243 |
} |
| 3244 |
|
| 3245 |
/* Set the transitions for each character in the label. */ |
| 3246 |
for (int i = 0; i < NOTCHAR; i++) |
| 3247 |
if (tstbit (i, &label)) |
| 3248 |
switch (d->syntax.sbit[i]) |
| 3249 |
{ |
| 3250 |
case CTX_NEWLINE: |
| 3251 |
trans[i] = state_newline; |
| 3252 |
break; |
| 3253 |
case CTX_LETTER: |
| 3254 |
trans[i] = state_letter; |
| 3255 |
break; |
| 3256 |
default: |
| 3257 |
trans[i] = state; |
| 3258 |
break; |
| 3259 |
} |
| 3260 |
|
| 3261 |
#ifdef DEBUG |
| 3262 |
fprintf (stderr, "trans table %td", s); |
| 3263 |
for (int i = 0; i < NOTCHAR; ++i) |
| 3264 |
{ |
| 3265 |
if (!(i & 0xf)) |
| 3266 |
fprintf (stderr, "\n"); |
| 3267 |
fprintf (stderr, " %2td", trans[i]); |
| 3268 |
} |
| 3269 |
fprintf (stderr, "\n"); |
| 3270 |
#endif |
| 3271 |
|
| 3272 |
free (group.elems); |
| 3273 |
free (follows.elems); |
| 3274 |
free (tmp.elems); |
| 3275 |
|
| 3276 |
/* Keep the newline transition in a special place so we can use it as |
| 3277 |
a sentinel. */ |
| 3278 |
if (tstbit (d->syntax.eolbyte, &label)) |
| 3279 |
{ |
| 3280 |
d->newlines[s] = trans[d->syntax.eolbyte]; |
| 3281 |
trans[d->syntax.eolbyte] = -1; |
| 3282 |
} |
| 3283 |
|
| 3284 |
return trans[uc]; |
| 3285 |
} |
| 3286 |
|
| 3287 |
/* Multibyte character handling sub-routines for dfaexec. */ |
| 3288 |
|
| 3289 |
/* Consume a single byte and transit state from 's' to '*next_state'. |
| 3290 |
This is almost the same as the state transition routine in dfaexec. |
| 3291 |
But the state transition is done just once; otherwise, matching succeeds or |
| 3292 |
we reach the end of the buffer. */ |
| 3293 |
static state_num |
| 3294 |
transit_state_singlebyte (struct dfa *d, state_num s, unsigned char const **pp) |
| 3295 |
{ |
| 3296 |
state_num *t; |
| 3297 |
|
| 3298 |
if (d->trans[s]) |
| 3299 |
t = d->trans[s]; |
| 3300 |
else if (d->fails[s]) |
| 3301 |
t = d->fails[s]; |
| 3302 |
else |
| 3303 |
{ |
| 3304 |
build_state (s, d, **pp); |
| 3305 |
if (d->trans[s]) |
| 3306 |
t = d->trans[s]; |
| 3307 |
else |
| 3308 |
{ |
| 3309 |
t = d->fails[s]; |
| 3310 |
assert (t); |
| 3311 |
} |
| 3312 |
} |
| 3313 |
|
| 3314 |
if (t[**pp] == -2) |
| 3315 |
build_state (s, d, **pp); |
| 3316 |
|
| 3317 |
return t[*(*pp)++]; |
| 3318 |
} |
| 3319 |
|
| 3320 |
/* Transit state from s, then return new state and update the pointer of |
| 3321 |
the buffer. This function is for a period operator which can match a |
| 3322 |
multi-byte character. */ |
| 3323 |
static state_num |
| 3324 |
transit_state (struct dfa *d, state_num s, unsigned char const **pp, |
| 3325 |
unsigned char const *end) |
| 3326 |
{ |
| 3327 |
wint_t wc; |
| 3328 |
|
| 3329 |
int mbclen = mbs_to_wchar (&wc, (char const *) *pp, end - *pp, d); |
| 3330 |
|
| 3331 |
/* This state has some operators which can match a multibyte character. */ |
| 3332 |
d->mb_follows.nelem = 0; |
| 3333 |
|
| 3334 |
/* Calculate the state which can be reached from the state 's' by |
| 3335 |
consuming 'mbclen' single bytes from the buffer. */ |
| 3336 |
state_num s1 = s; |
| 3337 |
int mbci; |
| 3338 |
for (mbci = 0; mbci < mbclen && (mbci == 0 || d->min_trcount <= s); mbci++) |
| 3339 |
s = transit_state_singlebyte (d, s, pp); |
| 3340 |
*pp += mbclen - mbci; |
| 3341 |
|
| 3342 |
if (wc == WEOF) |
| 3343 |
{ |
| 3344 |
/* It is an invalid character, so ANYCHAR is not accepted. */ |
| 3345 |
return s; |
| 3346 |
} |
| 3347 |
|
| 3348 |
/* If all positions which have ANYCHAR do not depend on the context |
| 3349 |
of the next character, calculate the next state with |
| 3350 |
pre-calculated follows and cache the result. */ |
| 3351 |
if (d->states[s1].mb_trindex < 0) |
| 3352 |
{ |
| 3353 |
if (MAX_TRCOUNT <= d->mb_trcount) |
| 3354 |
{ |
| 3355 |
state_num s3; |
| 3356 |
for (s3 = -1; s3 < d->tralloc; s3++) |
| 3357 |
{ |
| 3358 |
free (d->mb_trans[s3]); |
| 3359 |
d->mb_trans[s3] = NULL; |
| 3360 |
} |
| 3361 |
|
| 3362 |
for (state_num i = 0; i < d->sindex; i++) |
| 3363 |
d->states[i].mb_trindex = -1; |
| 3364 |
d->mb_trcount = 0; |
| 3365 |
} |
| 3366 |
d->states[s1].mb_trindex = d->mb_trcount++; |
| 3367 |
} |
| 3368 |
|
| 3369 |
if (! d->mb_trans[s]) |
| 3370 |
{ |
| 3371 |
enum { TRANSPTR_SIZE = sizeof *d->mb_trans[s] }; |
| 3372 |
enum { TRANSALLOC_SIZE = MAX_TRCOUNT * TRANSPTR_SIZE }; |
| 3373 |
d->mb_trans[s] = xmalloc (TRANSALLOC_SIZE); |
| 3374 |
for (int i = 0; i < MAX_TRCOUNT; i++) |
| 3375 |
d->mb_trans[s][i] = -1; |
| 3376 |
} |
| 3377 |
else if (d->mb_trans[s][d->states[s1].mb_trindex] >= 0) |
| 3378 |
return d->mb_trans[s][d->states[s1].mb_trindex]; |
| 3379 |
|
| 3380 |
if (s == -1) |
| 3381 |
copy (&d->states[s1].mbps, &d->mb_follows); |
| 3382 |
else |
| 3383 |
merge (&d->states[s1].mbps, &d->states[s].elems, &d->mb_follows); |
| 3384 |
|
| 3385 |
int separate_contexts = state_separate_contexts (d, &d->mb_follows); |
| 3386 |
state_num s2 = state_index (d, &d->mb_follows, separate_contexts ^ CTX_ANY); |
| 3387 |
realloc_trans_if_necessary (d); |
| 3388 |
|
| 3389 |
d->mb_trans[s][d->states[s1].mb_trindex] = s2; |
| 3390 |
|
| 3391 |
return s2; |
| 3392 |
} |
| 3393 |
|
| 3394 |
/* The initial state may encounter a byte which is not a single byte character |
| 3395 |
nor the first byte of a multibyte character. But it is incorrect for the |
| 3396 |
initial state to accept such a byte. For example, in Shift JIS the regular |
| 3397 |
expression "\\" accepts the codepoint 0x5c, but should not accept the second |
| 3398 |
byte of the codepoint 0x815c. Then the initial state must skip the bytes |
| 3399 |
that are not a single byte character nor the first byte of a multibyte |
| 3400 |
character. |
| 3401 |
|
| 3402 |
Given DFA state d, use mbs_to_wchar to advance MBP until it reaches |
| 3403 |
or exceeds P, and return the advanced MBP. If WCP is non-NULL and |
| 3404 |
the result is greater than P, set *WCP to the final wide character |
| 3405 |
processed, or to WEOF if no wide character is processed. Otherwise, |
| 3406 |
if WCP is non-NULL, *WCP may or may not be updated. |
| 3407 |
|
| 3408 |
Both P and MBP must be no larger than END. */ |
| 3409 |
static unsigned char const * |
| 3410 |
skip_remains_mb (struct dfa *d, unsigned char const *p, |
| 3411 |
unsigned char const *mbp, char const *end) |
| 3412 |
{ |
| 3413 |
if (d->syntax.never_trail[*p]) |
| 3414 |
return p; |
| 3415 |
while (mbp < p) |
| 3416 |
{ |
| 3417 |
wint_t wc; |
| 3418 |
mbp += mbs_to_wchar (&wc, (char const *) mbp, |
| 3419 |
end - (char const *) mbp, d); |
| 3420 |
} |
| 3421 |
return mbp; |
| 3422 |
} |
| 3423 |
|
| 3424 |
/* Search through a buffer looking for a match to the struct dfa *D. |
| 3425 |
Find the first occurrence of a string matching the regexp in the |
| 3426 |
buffer, and the shortest possible version thereof. Return a pointer to |
| 3427 |
the first character after the match, or NULL if none is found. BEGIN |
| 3428 |
points to the beginning of the buffer, and END points to the first byte |
| 3429 |
after its end. Note however that we store a sentinel byte (usually |
| 3430 |
newline) in *END, so the actual buffer must be one byte longer. |
| 3431 |
When ALLOW_NL, newlines may appear in the matching string. |
| 3432 |
If COUNT is non-NULL, increment *COUNT once for each newline processed. |
| 3433 |
If MULTIBYTE, the input consists of multibyte characters and/or |
| 3434 |
encoding-error bytes. Otherwise, it consists of single-byte characters. |
| 3435 |
Here is the list of features that make this DFA matcher punt: |
| 3436 |
- [M-N] range in non-simple locale: regex is up to 25% faster on [a-z] |
| 3437 |
- [^...] in non-simple locale |
| 3438 |
- [[=foo=]] or [[.foo.]] |
| 3439 |
- [[:alpha:]] etc. in multibyte locale (except [[:digit:]] works OK) |
| 3440 |
- back-reference: (.)\1 |
| 3441 |
- word-delimiter in multibyte locale: \<, \>, \b, \B |
| 3442 |
See struct localeinfo.simple for the definition of "simple locale". */ |
| 3443 |
|
| 3444 |
static inline char * |
| 3445 |
dfaexec_main (struct dfa *d, char const *begin, char *end, bool allow_nl, |
| 3446 |
idx_t *count, bool multibyte) |
| 3447 |
{ |
| 3448 |
if (MAX_TRCOUNT <= d->sindex) |
| 3449 |
{ |
| 3450 |
for (state_num s = d->min_trcount; s < d->sindex; s++) |
| 3451 |
{ |
| 3452 |
free (d->states[s].elems.elems); |
| 3453 |
free (d->states[s].mbps.elems); |
| 3454 |
} |
| 3455 |
d->sindex = d->min_trcount; |
| 3456 |
|
| 3457 |
if (d->trans) |
| 3458 |
{ |
| 3459 |
for (state_num s = 0; s < d->tralloc; s++) |
| 3460 |
{ |
| 3461 |
free (d->trans[s]); |
| 3462 |
free (d->fails[s]); |
| 3463 |
d->trans[s] = d->fails[s] = NULL; |
| 3464 |
} |
| 3465 |
d->trcount = 0; |
| 3466 |
} |
| 3467 |
|
| 3468 |
if (d->localeinfo.multibyte && d->mb_trans) |
| 3469 |
{ |
| 3470 |
for (state_num s = -1; s < d->tralloc; s++) |
| 3471 |
{ |
| 3472 |
free (d->mb_trans[s]); |
| 3473 |
d->mb_trans[s] = NULL; |
| 3474 |
} |
| 3475 |
for (state_num s = 0; s < d->min_trcount; s++) |
| 3476 |
d->states[s].mb_trindex = -1; |
| 3477 |
d->mb_trcount = 0; |
| 3478 |
} |
| 3479 |
} |
| 3480 |
|
| 3481 |
if (!d->tralloc) |
| 3482 |
realloc_trans_if_necessary (d); |
| 3483 |
|
| 3484 |
/* Current state. */ |
| 3485 |
state_num s = 0, s1 = 0; |
| 3486 |
|
| 3487 |
/* Current input character. */ |
| 3488 |
unsigned char const *p = (unsigned char const *) begin; |
| 3489 |
unsigned char const *mbp = p; |
| 3490 |
|
| 3491 |
/* Copy of d->trans so it can be optimized into a register. */ |
| 3492 |
state_num **trans = d->trans; |
| 3493 |
unsigned char eol = d->syntax.eolbyte; /* Likewise for eolbyte. */ |
| 3494 |
unsigned char saved_end = *(unsigned char *) end; |
| 3495 |
*end = eol; |
| 3496 |
|
| 3497 |
if (multibyte) |
| 3498 |
{ |
| 3499 |
mbszero (&d->mbs); |
| 3500 |
if (d->mb_follows.alloc == 0) |
| 3501 |
alloc_position_set (&d->mb_follows, d->nleaves); |
| 3502 |
} |
| 3503 |
|
| 3504 |
idx_t nlcount = 0; |
| 3505 |
for (;;) |
| 3506 |
{ |
| 3507 |
state_num *t; |
| 3508 |
while ((t = trans[s]) != NULL) |
| 3509 |
{ |
| 3510 |
if (s < d->min_trcount) |
| 3511 |
{ |
| 3512 |
if (!multibyte || d->states[s].mbps.nelem == 0) |
| 3513 |
{ |
| 3514 |
while (t[*p] == s) |
| 3515 |
p++; |
| 3516 |
} |
| 3517 |
if (multibyte) |
| 3518 |
p = mbp = skip_remains_mb (d, p, mbp, end); |
| 3519 |
} |
| 3520 |
|
| 3521 |
if (multibyte) |
| 3522 |
{ |
| 3523 |
s1 = s; |
| 3524 |
|
| 3525 |
if (d->states[s].mbps.nelem == 0 |
| 3526 |
|| d->localeinfo.sbctowc[*p] != WEOF || (char *) p >= end) |
| 3527 |
{ |
| 3528 |
/* If an input character does not match ANYCHAR, do it |
| 3529 |
like a single-byte character. */ |
| 3530 |
s = t[*p++]; |
| 3531 |
} |
| 3532 |
else |
| 3533 |
{ |
| 3534 |
s = transit_state (d, s, &p, (unsigned char *) end); |
| 3535 |
mbp = p; |
| 3536 |
trans = d->trans; |
| 3537 |
} |
| 3538 |
} |
| 3539 |
else |
| 3540 |
{ |
| 3541 |
s1 = t[*p++]; |
| 3542 |
t = trans[s1]; |
| 3543 |
if (! t) |
| 3544 |
{ |
| 3545 |
state_num tmp = s; |
| 3546 |
s = s1; |
| 3547 |
s1 = tmp; /* swap */ |
| 3548 |
break; |
| 3549 |
} |
| 3550 |
if (s < d->min_trcount) |
| 3551 |
{ |
| 3552 |
while (t[*p] == s1) |
| 3553 |
p++; |
| 3554 |
} |
| 3555 |
s = t[*p++]; |
| 3556 |
} |
| 3557 |
} |
| 3558 |
|
| 3559 |
if (s < 0) |
| 3560 |
{ |
| 3561 |
if (s == -2) |
| 3562 |
{ |
| 3563 |
s = build_state (s1, d, p[-1]); |
| 3564 |
trans = d->trans; |
| 3565 |
} |
| 3566 |
else if ((char *) p <= end && p[-1] == eol && 0 <= d->newlines[s1]) |
| 3567 |
{ |
| 3568 |
/* The previous character was a newline. Count it, and skip |
| 3569 |
checking of multibyte character boundary until here. */ |
| 3570 |
nlcount++; |
| 3571 |
mbp = p; |
| 3572 |
|
| 3573 |
s = (allow_nl ? d->newlines[s1] |
| 3574 |
: d->syntax.sbit[eol] == CTX_NEWLINE ? 0 |
| 3575 |
: d->syntax.sbit[eol] == CTX_LETTER ? d->min_trcount - 1 |
| 3576 |
: d->initstate_notbol); |
| 3577 |
} |
| 3578 |
else |
| 3579 |
{ |
| 3580 |
p = NULL; |
| 3581 |
goto done; |
| 3582 |
} |
| 3583 |
} |
| 3584 |
else if (d->fails[s]) |
| 3585 |
{ |
| 3586 |
if ((d->success[s] & d->syntax.sbit[*p]) |
| 3587 |
|| ((char *) p == end |
| 3588 |
&& accepts_in_context (d->states[s].context, CTX_NEWLINE, s, |
| 3589 |
d))) |
| 3590 |
goto done; |
| 3591 |
|
| 3592 |
if (multibyte && s < d->min_trcount) |
| 3593 |
p = mbp = skip_remains_mb (d, p, mbp, end); |
| 3594 |
|
| 3595 |
s1 = s; |
| 3596 |
if (!multibyte || d->states[s].mbps.nelem == 0 |
| 3597 |
|| d->localeinfo.sbctowc[*p] != WEOF || (char *) p >= end) |
| 3598 |
{ |
| 3599 |
/* If a input character does not match ANYCHAR, do it |
| 3600 |
like a single-byte character. */ |
| 3601 |
s = d->fails[s][*p++]; |
| 3602 |
} |
| 3603 |
else |
| 3604 |
{ |
| 3605 |
s = transit_state (d, s, &p, (unsigned char *) end); |
| 3606 |
mbp = p; |
| 3607 |
trans = d->trans; |
| 3608 |
} |
| 3609 |
} |
| 3610 |
else |
| 3611 |
{ |
| 3612 |
build_state (s, d, p[0]); |
| 3613 |
trans = d->trans; |
| 3614 |
} |
| 3615 |
} |
| 3616 |
|
| 3617 |
done: |
| 3618 |
if (count) |
| 3619 |
*count += nlcount; |
| 3620 |
*end = saved_end; |
| 3621 |
return (char *) p; |
| 3622 |
} |
| 3623 |
|
| 3624 |
/* Specialized versions of dfaexec for multibyte and single-byte cases. |
| 3625 |
This is for performance, as dfaexec_main is an inline function. */ |
| 3626 |
|
| 3627 |
static char * |
| 3628 |
dfaexec_mb (struct dfa *d, char const *begin, char *end, |
| 3629 |
bool allow_nl, idx_t *count, bool *backref) |
| 3630 |
{ |
| 3631 |
return dfaexec_main (d, begin, end, allow_nl, count, true); |
| 3632 |
} |
| 3633 |
|
| 3634 |
static char * |
| 3635 |
dfaexec_sb (struct dfa *d, char const *begin, char *end, |
| 3636 |
bool allow_nl, idx_t *count, bool *backref) |
| 3637 |
{ |
| 3638 |
return dfaexec_main (d, begin, end, allow_nl, count, false); |
| 3639 |
} |
| 3640 |
|
| 3641 |
/* Always set *BACKREF and return BEGIN. Use this wrapper for |
| 3642 |
any regexp that uses a construct not supported by this code. */ |
| 3643 |
static char * |
| 3644 |
dfaexec_noop (struct dfa *d, char const *begin, char *end, |
| 3645 |
bool allow_nl, idx_t *count, bool *backref) |
| 3646 |
{ |
| 3647 |
*backref = true; |
| 3648 |
return (char *) begin; |
| 3649 |
} |
| 3650 |
|
| 3651 |
/* Like dfaexec_main (D, BEGIN, END, ALLOW_NL, COUNT, D->localeinfo.multibyte), |
| 3652 |
but faster and set *BACKREF if the DFA code does not support this |
| 3653 |
regexp usage. */ |
| 3654 |
|
| 3655 |
char * |
| 3656 |
dfaexec (struct dfa *d, char const *begin, char *end, |
| 3657 |
bool allow_nl, idx_t *count, bool *backref) |
| 3658 |
{ |
| 3659 |
return d->dfaexec (d, begin, end, allow_nl, count, backref); |
| 3660 |
} |
| 3661 |
|
| 3662 |
struct dfa * |
| 3663 |
dfasuperset (struct dfa const *d) |
| 3664 |
{ |
| 3665 |
return d->superset; |
| 3666 |
} |
| 3667 |
|
| 3668 |
bool |
| 3669 |
dfaisfast (struct dfa const *d) |
| 3670 |
{ |
| 3671 |
return d->fast; |
| 3672 |
} |
| 3673 |
|
| 3674 |
static void |
| 3675 |
free_mbdata (struct dfa *d) |
| 3676 |
{ |
| 3677 |
free (d->multibyte_prop); |
| 3678 |
free (d->lex.brack.chars); |
| 3679 |
free (d->mb_follows.elems); |
| 3680 |
|
| 3681 |
if (d->mb_trans) |
| 3682 |
{ |
| 3683 |
state_num s; |
| 3684 |
for (s = -1; s < d->tralloc; s++) |
| 3685 |
free (d->mb_trans[s]); |
| 3686 |
free (d->mb_trans - 2); |
| 3687 |
} |
| 3688 |
} |
| 3689 |
|
| 3690 |
/* Return true if every construct in D is supported by this DFA matcher. */ |
| 3691 |
bool |
| 3692 |
dfasupported (struct dfa const *d) |
| 3693 |
{ |
| 3694 |
for (idx_t i = 0; i < d->tindex; i++) |
| 3695 |
{ |
| 3696 |
switch (d->tokens[i]) |
| 3697 |
{ |
| 3698 |
case BEGWORD: |
| 3699 |
case ENDWORD: |
| 3700 |
case LIMWORD: |
| 3701 |
case NOTLIMWORD: |
| 3702 |
if (!d->localeinfo.multibyte) |
| 3703 |
continue; |
| 3704 |
FALLTHROUGH; |
| 3705 |
case BACKREF: |
| 3706 |
case MBCSET: |
| 3707 |
return false; |
| 3708 |
} |
| 3709 |
} |
| 3710 |
return true; |
| 3711 |
} |
| 3712 |
|
| 3713 |
/* Disable use of the superset DFA if it is not likely to help |
| 3714 |
performance. */ |
| 3715 |
static void |
| 3716 |
maybe_disable_superset_dfa (struct dfa *d) |
| 3717 |
{ |
| 3718 |
if (!d->localeinfo.using_utf8) |
| 3719 |
return; |
| 3720 |
|
| 3721 |
bool have_backref = false; |
| 3722 |
for (idx_t i = 0; i < d->tindex; i++) |
| 3723 |
{ |
| 3724 |
switch (d->tokens[i]) |
| 3725 |
{ |
| 3726 |
case ANYCHAR: |
| 3727 |
/* Lowered. */ |
| 3728 |
abort (); |
| 3729 |
case BACKREF: |
| 3730 |
have_backref = true; |
| 3731 |
break; |
| 3732 |
case MBCSET: |
| 3733 |
/* Requires multi-byte algorithm. */ |
| 3734 |
return; |
| 3735 |
default: |
| 3736 |
break; |
| 3737 |
} |
| 3738 |
} |
| 3739 |
|
| 3740 |
if (!have_backref && d->superset) |
| 3741 |
{ |
| 3742 |
/* The superset DFA is not likely to be much faster, so remove it. */ |
| 3743 |
dfafree (d->superset); |
| 3744 |
free (d->superset); |
| 3745 |
d->superset = NULL; |
| 3746 |
} |
| 3747 |
|
| 3748 |
free_mbdata (d); |
| 3749 |
d->localeinfo.multibyte = false; |
| 3750 |
d->dfaexec = dfaexec_sb; |
| 3751 |
d->fast = true; |
| 3752 |
} |
| 3753 |
|
| 3754 |
static void |
| 3755 |
dfassbuild (struct dfa *d) |
| 3756 |
{ |
| 3757 |
struct dfa *sup = dfaalloc (); |
| 3758 |
|
| 3759 |
*sup = *d; |
| 3760 |
sup->localeinfo.multibyte = false; |
| 3761 |
sup->dfaexec = dfaexec_sb; |
| 3762 |
sup->multibyte_prop = NULL; |
| 3763 |
sup->superset = NULL; |
| 3764 |
sup->states = NULL; |
| 3765 |
sup->sindex = 0; |
| 3766 |
sup->constraints = NULL; |
| 3767 |
sup->separates = NULL; |
| 3768 |
sup->follows = NULL; |
| 3769 |
sup->tralloc = 0; |
| 3770 |
sup->trans = NULL; |
| 3771 |
sup->fails = NULL; |
| 3772 |
sup->success = NULL; |
| 3773 |
sup->newlines = NULL; |
| 3774 |
|
| 3775 |
sup->charclasses = xnmalloc (sup->calloc, sizeof *sup->charclasses); |
| 3776 |
if (d->cindex) |
| 3777 |
{ |
| 3778 |
memcpy (sup->charclasses, d->charclasses, |
| 3779 |
d->cindex * sizeof *sup->charclasses); |
| 3780 |
} |
| 3781 |
|
| 3782 |
sup->tokens = xnmalloc (d->tindex, 2 * sizeof *sup->tokens); |
| 3783 |
sup->talloc = d->tindex * 2; |
| 3784 |
|
| 3785 |
bool have_achar = false; |
| 3786 |
bool have_nchar = false; |
| 3787 |
idx_t j; |
| 3788 |
for (idx_t i = j = 0; i < d->tindex; i++) |
| 3789 |
{ |
| 3790 |
switch (d->tokens[i]) |
| 3791 |
{ |
| 3792 |
case ANYCHAR: |
| 3793 |
case MBCSET: |
| 3794 |
case BACKREF: |
| 3795 |
{ |
| 3796 |
charclass ccl; |
| 3797 |
fillset (&ccl); |
| 3798 |
sup->tokens[j++] = CSET + charclass_index (sup, &ccl); |
| 3799 |
sup->tokens[j++] = STAR; |
| 3800 |
if (d->tokens[i + 1] == QMARK || d->tokens[i + 1] == STAR |
| 3801 |
|| d->tokens[i + 1] == PLUS) |
| 3802 |
i++; |
| 3803 |
have_achar = true; |
| 3804 |
} |
| 3805 |
break; |
| 3806 |
case BEGWORD: |
| 3807 |
case ENDWORD: |
| 3808 |
case LIMWORD: |
| 3809 |
case NOTLIMWORD: |
| 3810 |
if (d->localeinfo.multibyte) |
| 3811 |
{ |
| 3812 |
/* These constraints aren't supported in a multibyte locale. |
| 3813 |
Ignore them in the superset DFA. */ |
| 3814 |
sup->tokens[j++] = EMPTY; |
| 3815 |
break; |
| 3816 |
} |
| 3817 |
FALLTHROUGH; |
| 3818 |
default: |
| 3819 |
sup->tokens[j++] = d->tokens[i]; |
| 3820 |
if ((0 <= d->tokens[i] && d->tokens[i] < NOTCHAR) |
| 3821 |
|| d->tokens[i] >= CSET) |
| 3822 |
have_nchar = true; |
| 3823 |
break; |
| 3824 |
} |
| 3825 |
} |
| 3826 |
sup->tindex = j; |
| 3827 |
|
| 3828 |
if (have_nchar && (have_achar || d->localeinfo.multibyte)) |
| 3829 |
d->superset = sup; |
| 3830 |
else |
| 3831 |
{ |
| 3832 |
dfafree (sup); |
| 3833 |
free (sup); |
| 3834 |
} |
| 3835 |
} |
| 3836 |
|
| 3837 |
/* Parse a string S of length LEN into D (but skip this step if S is null). |
| 3838 |
Then analyze D and build a matcher for it. |
| 3839 |
SEARCHFLAG says whether to build a searching or an exact matcher. */ |
| 3840 |
void |
| 3841 |
dfacomp (char const *s, idx_t len, struct dfa *d, bool searchflag) |
| 3842 |
{ |
| 3843 |
if (s != NULL) |
| 3844 |
dfaparse (s, len, d); |
| 3845 |
|
| 3846 |
dfassbuild (d); |
| 3847 |
|
| 3848 |
if (dfasupported (d)) |
| 3849 |
{ |
| 3850 |
maybe_disable_superset_dfa (d); |
| 3851 |
dfaanalyze (d, searchflag); |
| 3852 |
} |
| 3853 |
else |
| 3854 |
{ |
| 3855 |
d->dfaexec = dfaexec_noop; |
| 3856 |
} |
| 3857 |
|
| 3858 |
if (d->superset) |
| 3859 |
{ |
| 3860 |
d->fast = true; |
| 3861 |
dfaanalyze (d->superset, searchflag); |
| 3862 |
} |
| 3863 |
} |
| 3864 |
|
| 3865 |
/* Free the storage held by the components of a dfa. */ |
| 3866 |
void |
| 3867 |
dfafree (struct dfa *d) |
| 3868 |
{ |
| 3869 |
free (d->charclasses); |
| 3870 |
free (d->tokens); |
| 3871 |
|
| 3872 |
if (d->localeinfo.multibyte) |
| 3873 |
free_mbdata (d); |
| 3874 |
|
| 3875 |
free (d->constraints); |
| 3876 |
free (d->separates); |
| 3877 |
|
| 3878 |
for (idx_t i = 0; i < d->sindex; i++) |
| 3879 |
{ |
| 3880 |
free (d->states[i].elems.elems); |
| 3881 |
free (d->states[i].mbps.elems); |
| 3882 |
} |
| 3883 |
free (d->states); |
| 3884 |
|
| 3885 |
if (d->follows) |
| 3886 |
{ |
| 3887 |
for (idx_t i = 0; i < d->tindex; i++) |
| 3888 |
free (d->follows[i].elems); |
| 3889 |
free (d->follows); |
| 3890 |
} |
| 3891 |
|
| 3892 |
if (d->trans) |
| 3893 |
{ |
| 3894 |
for (idx_t i = 0; i < d->tralloc; i++) |
| 3895 |
{ |
| 3896 |
free (d->trans[i]); |
| 3897 |
free (d->fails[i]); |
| 3898 |
} |
| 3899 |
|
| 3900 |
free (d->trans - 2); |
| 3901 |
free (d->fails); |
| 3902 |
free (d->newlines); |
| 3903 |
free (d->success); |
| 3904 |
} |
| 3905 |
|
| 3906 |
if (d->superset) |
| 3907 |
{ |
| 3908 |
dfafree (d->superset); |
| 3909 |
free (d->superset); |
| 3910 |
} |
| 3911 |
} |
| 3912 |
|
| 3913 |
/* Having found the postfix representation of the regular expression, |
| 3914 |
try to find a long sequence of characters that must appear in any line |
| 3915 |
containing the r.e. |
| 3916 |
Finding a "longest" sequence is beyond the scope here; |
| 3917 |
we take an easy way out and hope for the best. |
| 3918 |
(Take "(ab|a)b"--please.) |
| 3919 |
|
| 3920 |
We do a bottom-up calculation of sequences of characters that must appear |
| 3921 |
in matches of r.e.'s represented by trees rooted at the nodes of the postfix |
| 3922 |
representation: |
| 3923 |
sequences that must appear at the left of the match ("left") |
| 3924 |
sequences that must appear at the right of the match ("right") |
| 3925 |
lists of sequences that must appear somewhere in the match ("in") |
| 3926 |
sequences that must constitute the match ("is") |
| 3927 |
|
| 3928 |
When we get to the root of the tree, we use one of the longest of its |
| 3929 |
calculated "in" sequences as our answer. |
| 3930 |
|
| 3931 |
The sequences calculated for the various types of node (in pseudo ANSI c) |
| 3932 |
are shown below. "p" is the operand of unary operators (and the left-hand |
| 3933 |
operand of binary operators); "q" is the right-hand operand of binary |
| 3934 |
operators. |
| 3935 |
|
| 3936 |
"ZERO" means "a zero-length sequence" below. |
| 3937 |
|
| 3938 |
Type left right is in |
| 3939 |
---- ---- ----- -- -- |
| 3940 |
char c # c # c # c # c |
| 3941 |
|
| 3942 |
ANYCHAR ZERO ZERO ZERO ZERO |
| 3943 |
|
| 3944 |
MBCSET ZERO ZERO ZERO ZERO |
| 3945 |
|
| 3946 |
CSET ZERO ZERO ZERO ZERO |
| 3947 |
|
| 3948 |
STAR ZERO ZERO ZERO ZERO |
| 3949 |
|
| 3950 |
QMARK ZERO ZERO ZERO ZERO |
| 3951 |
|
| 3952 |
PLUS p->left p->right ZERO p->in |
| 3953 |
|
| 3954 |
CAT (p->is==ZERO)? (q->is==ZERO)? (p->is!=ZERO && p->in plus |
| 3955 |
p->left : q->right : q->is!=ZERO) ? q->in plus |
| 3956 |
p->is##q->left p->right##q->is p->is##q->is : p->right##q->left |
| 3957 |
ZERO |
| 3958 |
|
| 3959 |
OR longest common longest common (do p->is and substrings common |
| 3960 |
leading trailing to q->is have same p->in and |
| 3961 |
(sub)sequence (sub)sequence q->in length and content) ? |
| 3962 |
of p->left of p->right |
| 3963 |
and q->left and q->right p->is : NULL |
| 3964 |
|
| 3965 |
If there's anything else we recognize in the tree, all four sequences get set |
| 3966 |
to zero-length sequences. If there's something we don't recognize in the |
| 3967 |
tree, we just return a zero-length sequence. |
| 3968 |
|
| 3969 |
Break ties in favor of infrequent letters (choosing 'zzz' in preference to |
| 3970 |
'aaa')? |
| 3971 |
|
| 3972 |
And ... is it here or someplace that we might ponder "optimizations" such as |
| 3973 |
egrep 'psi|epsilon' -> egrep 'psi' |
| 3974 |
egrep 'pepsi|epsilon' -> egrep 'epsi' |
| 3975 |
(Yes, we now find "epsi" as a "string |
| 3976 |
that must occur", but we might also |
| 3977 |
simplify the *entire* r.e. being sought) |
| 3978 |
grep '[c]' -> grep 'c' |
| 3979 |
grep '(ab|a)b' -> grep 'ab' |
| 3980 |
grep 'ab*' -> grep 'a' |
| 3981 |
grep 'a*b' -> grep 'b' |
| 3982 |
|
| 3983 |
There are several issues: |
| 3984 |
|
| 3985 |
Is optimization easy (enough)? |
| 3986 |
|
| 3987 |
Does optimization actually accomplish anything, |
| 3988 |
or is the automaton you get from "psi|epsilon" (for example) |
| 3989 |
the same as the one you get from "psi" (for example)? |
| 3990 |
|
| 3991 |
Are optimizable r.e.'s likely to be used in real-life situations |
| 3992 |
(something like 'ab*' is probably unlikely; something like is |
| 3993 |
'psi|epsilon' is likelier)? */ |
| 3994 |
|
| 3995 |
static char * |
| 3996 |
icatalloc (char *old, char const *new) |
| 3997 |
{ |
| 3998 |
idx_t newsize = strlen (new); |
| 3999 |
if (newsize == 0) |
| 4000 |
return old; |
| 4001 |
idx_t oldsize = strlen (old); |
| 4002 |
char *result = xirealloc (old, oldsize + newsize + 1); |
| 4003 |
memcpy (result + oldsize, new, newsize + 1); |
| 4004 |
return result; |
| 4005 |
} |
| 4006 |
|
| 4007 |
static void |
| 4008 |
freelist (char **cpp) |
| 4009 |
{ |
| 4010 |
while (*cpp) |
| 4011 |
free (*cpp++); |
| 4012 |
} |
| 4013 |
|
| 4014 |
static char ** |
| 4015 |
enlistnew (char **cpp, char *new) |
| 4016 |
{ |
| 4017 |
/* Is there already something in the list that's new (or longer)? */ |
| 4018 |
idx_t i; |
| 4019 |
for (i = 0; cpp[i] != NULL; i++) |
| 4020 |
if (strstr (cpp[i], new) != NULL) |
| 4021 |
{ |
| 4022 |
free (new); |
| 4023 |
return cpp; |
| 4024 |
} |
| 4025 |
/* Eliminate any obsoleted strings. */ |
| 4026 |
for (idx_t j = 0; cpp[j] != NULL; ) |
| 4027 |
if (strstr (new, cpp[j]) == NULL) |
| 4028 |
++j; |
| 4029 |
else |
| 4030 |
{ |
| 4031 |
free (cpp[j]); |
| 4032 |
if (--i == j) |
| 4033 |
break; |
| 4034 |
cpp[j] = cpp[i]; |
| 4035 |
cpp[i] = NULL; |
| 4036 |
} |
| 4037 |
/* Add the new string. */ |
| 4038 |
cpp = xreallocarray (cpp, i + 2, sizeof *cpp); |
| 4039 |
cpp[i] = new; |
| 4040 |
cpp[i + 1] = NULL; |
| 4041 |
return cpp; |
| 4042 |
} |
| 4043 |
|
| 4044 |
static char ** |
| 4045 |
enlist (char **cpp, char const *str, idx_t len) |
| 4046 |
{ |
| 4047 |
return enlistnew (cpp, ximemdup0 (str, len)); |
| 4048 |
} |
| 4049 |
|
| 4050 |
/* Given pointers to two strings, return a pointer to an allocated |
| 4051 |
list of their distinct common substrings. */ |
| 4052 |
static char ** |
| 4053 |
comsubs (char *left, char const *right) |
| 4054 |
{ |
| 4055 |
char **cpp = xzalloc (sizeof *cpp); |
| 4056 |
|
| 4057 |
for (char *lcp = left; *lcp != '\0'; lcp++) |
| 4058 |
{ |
| 4059 |
idx_t len = 0; |
| 4060 |
char *rcp = strchr (right, *lcp); |
| 4061 |
while (rcp != NULL) |
| 4062 |
{ |
| 4063 |
idx_t i; |
| 4064 |
for (i = 1; lcp[i] != '\0' && lcp[i] == rcp[i]; ++i) |
| 4065 |
continue; |
| 4066 |
if (i > len) |
| 4067 |
len = i; |
| 4068 |
rcp = strchr (rcp + 1, *lcp); |
| 4069 |
} |
| 4070 |
if (len != 0) |
| 4071 |
cpp = enlist (cpp, lcp, len); |
| 4072 |
} |
| 4073 |
return cpp; |
| 4074 |
} |
| 4075 |
|
| 4076 |
static char ** |
| 4077 |
addlists (char **old, char **new) |
| 4078 |
{ |
| 4079 |
for (; *new; new++) |
| 4080 |
old = enlistnew (old, xstrdup (*new)); |
| 4081 |
return old; |
| 4082 |
} |
| 4083 |
|
| 4084 |
/* Given two lists of substrings, return a new list giving substrings |
| 4085 |
common to both. */ |
| 4086 |
static char ** |
| 4087 |
inboth (char **left, char **right) |
| 4088 |
{ |
| 4089 |
char **both = xzalloc (sizeof *both); |
| 4090 |
|
| 4091 |
for (idx_t lnum = 0; left[lnum] != NULL; lnum++) |
| 4092 |
{ |
| 4093 |
for (idx_t rnum = 0; right[rnum] != NULL; rnum++) |
| 4094 |
{ |
| 4095 |
char **temp = comsubs (left[lnum], right[rnum]); |
| 4096 |
both = addlists (both, temp); |
| 4097 |
freelist (temp); |
| 4098 |
free (temp); |
| 4099 |
} |
| 4100 |
} |
| 4101 |
return both; |
| 4102 |
} |
| 4103 |
|
| 4104 |
typedef struct must must; |
| 4105 |
|
| 4106 |
struct must |
| 4107 |
{ |
| 4108 |
char **in; |
| 4109 |
char *left; |
| 4110 |
char *right; |
| 4111 |
char *is; |
| 4112 |
bool begline; |
| 4113 |
bool endline; |
| 4114 |
must *prev; |
| 4115 |
}; |
| 4116 |
|
| 4117 |
static must * |
| 4118 |
allocmust (must *mp, idx_t size) |
| 4119 |
{ |
| 4120 |
must *new_mp = xmalloc (sizeof *new_mp); |
| 4121 |
new_mp->in = xzalloc (sizeof *new_mp->in); |
| 4122 |
new_mp->left = xizalloc (size); |
| 4123 |
new_mp->right = xizalloc (size); |
| 4124 |
new_mp->is = xizalloc (size); |
| 4125 |
new_mp->begline = false; |
| 4126 |
new_mp->endline = false; |
| 4127 |
new_mp->prev = mp; |
| 4128 |
return new_mp; |
| 4129 |
} |
| 4130 |
|
| 4131 |
static void |
| 4132 |
resetmust (must *mp) |
| 4133 |
{ |
| 4134 |
freelist (mp->in); |
| 4135 |
mp->in[0] = NULL; |
| 4136 |
mp->left[0] = mp->right[0] = mp->is[0] = '\0'; |
| 4137 |
mp->begline = false; |
| 4138 |
mp->endline = false; |
| 4139 |
} |
| 4140 |
|
| 4141 |
static void |
| 4142 |
freemust (must *mp) |
| 4143 |
{ |
| 4144 |
freelist (mp->in); |
| 4145 |
free (mp->in); |
| 4146 |
free (mp->left); |
| 4147 |
free (mp->right); |
| 4148 |
free (mp->is); |
| 4149 |
free (mp); |
| 4150 |
} |
| 4151 |
|
| 4152 |
struct dfamust * |
| 4153 |
dfamust (struct dfa const *d) |
| 4154 |
{ |
| 4155 |
must *mp = NULL; |
| 4156 |
char const *result = ""; |
| 4157 |
bool exact = false; |
| 4158 |
bool begline = false; |
| 4159 |
bool endline = false; |
| 4160 |
bool need_begline = false; |
| 4161 |
bool need_endline = false; |
| 4162 |
bool case_fold_unibyte = d->syntax.case_fold & !d->localeinfo.multibyte; |
| 4163 |
|
| 4164 |
for (idx_t ri = 1; ri + 1 < d->tindex; ri++) |
| 4165 |
{ |
| 4166 |
token t = d->tokens[ri]; |
| 4167 |
switch (t) |
| 4168 |
{ |
| 4169 |
case BEGLINE: |
| 4170 |
mp = allocmust (mp, 2); |
| 4171 |
mp->begline = true; |
| 4172 |
need_begline = true; |
| 4173 |
break; |
| 4174 |
case ENDLINE: |
| 4175 |
mp = allocmust (mp, 2); |
| 4176 |
mp->endline = true; |
| 4177 |
need_endline = true; |
| 4178 |
break; |
| 4179 |
case LPAREN: |
| 4180 |
case RPAREN: |
| 4181 |
assert (!"neither LPAREN nor RPAREN may appear here"); |
| 4182 |
|
| 4183 |
case EMPTY: |
| 4184 |
case BEGWORD: |
| 4185 |
case ENDWORD: |
| 4186 |
case LIMWORD: |
| 4187 |
case NOTLIMWORD: |
| 4188 |
case BACKREF: |
| 4189 |
case ANYCHAR: |
| 4190 |
case MBCSET: |
| 4191 |
mp = allocmust (mp, 2); |
| 4192 |
break; |
| 4193 |
|
| 4194 |
case STAR: |
| 4195 |
case QMARK: |
| 4196 |
assume_nonnull (mp); |
| 4197 |
resetmust (mp); |
| 4198 |
break; |
| 4199 |
|
| 4200 |
case OR: |
| 4201 |
{ |
| 4202 |
char **new; |
| 4203 |
must *rmp = mp; |
| 4204 |
assume_nonnull (rmp); |
| 4205 |
must *lmp = mp = mp->prev; |
| 4206 |
assume_nonnull (lmp); |
| 4207 |
idx_t j, ln, rn, n; |
| 4208 |
|
| 4209 |
/* Guaranteed to be. Unlikely, but ... */ |
| 4210 |
if (str_eq (lmp->is, rmp->is)) |
| 4211 |
{ |
| 4212 |
lmp->begline &= rmp->begline; |
| 4213 |
lmp->endline &= rmp->endline; |
| 4214 |
} |
| 4215 |
else |
| 4216 |
{ |
| 4217 |
lmp->is[0] = '\0'; |
| 4218 |
lmp->begline = false; |
| 4219 |
lmp->endline = false; |
| 4220 |
} |
| 4221 |
/* Left side--easy */ |
| 4222 |
idx_t i = 0; |
| 4223 |
while (lmp->left[i] != '\0' && lmp->left[i] == rmp->left[i]) |
| 4224 |
++i; |
| 4225 |
lmp->left[i] = '\0'; |
| 4226 |
/* Right side */ |
| 4227 |
ln = strlen (lmp->right); |
| 4228 |
rn = strlen (rmp->right); |
| 4229 |
n = ln; |
| 4230 |
if (n > rn) |
| 4231 |
n = rn; |
| 4232 |
for (i = 0; i < n; ++i) |
| 4233 |
if (lmp->right[ln - i - 1] != rmp->right[rn - i - 1]) |
| 4234 |
break; |
| 4235 |
for (j = 0; j < i; ++j) |
| 4236 |
lmp->right[j] = lmp->right[(ln - i) + j]; |
| 4237 |
lmp->right[j] = '\0'; |
| 4238 |
new = inboth (lmp->in, rmp->in); |
| 4239 |
freelist (lmp->in); |
| 4240 |
free (lmp->in); |
| 4241 |
lmp->in = new; |
| 4242 |
freemust (rmp); |
| 4243 |
} |
| 4244 |
break; |
| 4245 |
|
| 4246 |
case PLUS: |
| 4247 |
assume_nonnull (mp); |
| 4248 |
mp->is[0] = '\0'; |
| 4249 |
break; |
| 4250 |
|
| 4251 |
case END: |
| 4252 |
assume_nonnull (mp); |
| 4253 |
assert (!mp->prev); |
| 4254 |
for (idx_t i = 0; mp->in[i] != NULL; i++) |
| 4255 |
if (strlen (mp->in[i]) > strlen (result)) |
| 4256 |
result = mp->in[i]; |
| 4257 |
if (str_eq (result, mp->is)) |
| 4258 |
{ |
| 4259 |
if ((!need_begline || mp->begline) && (!need_endline |
| 4260 |
|| mp->endline)) |
| 4261 |
exact = true; |
| 4262 |
begline = mp->begline; |
| 4263 |
endline = mp->endline; |
| 4264 |
} |
| 4265 |
goto done; |
| 4266 |
|
| 4267 |
case CAT: |
| 4268 |
{ |
| 4269 |
must *rmp = mp; |
| 4270 |
assume_nonnull (rmp); |
| 4271 |
must *lmp = mp = mp->prev; |
| 4272 |
assume_nonnull (lmp); |
| 4273 |
|
| 4274 |
/* In. Everything in left, plus everything in |
| 4275 |
right, plus concatenation of |
| 4276 |
left's right and right's left. */ |
| 4277 |
lmp->in = addlists (lmp->in, rmp->in); |
| 4278 |
if (lmp->right[0] != '\0' && rmp->left[0] != '\0') |
| 4279 |
{ |
| 4280 |
idx_t lrlen = strlen (lmp->right); |
| 4281 |
idx_t rllen = strlen (rmp->left); |
| 4282 |
char *tp = ximalloc (lrlen + rllen + 1); |
| 4283 |
memcpy (tp + lrlen, rmp->left, rllen + 1); |
| 4284 |
memcpy (tp, lmp->right, lrlen); |
| 4285 |
lmp->in = enlistnew (lmp->in, tp); |
| 4286 |
} |
| 4287 |
/* Left-hand */ |
| 4288 |
if (lmp->is[0] != '\0') |
| 4289 |
lmp->left = icatalloc (lmp->left, rmp->left); |
| 4290 |
/* Right-hand */ |
| 4291 |
if (rmp->is[0] == '\0') |
| 4292 |
lmp->right[0] = '\0'; |
| 4293 |
lmp->right = icatalloc (lmp->right, rmp->right); |
| 4294 |
/* Guaranteed to be */ |
| 4295 |
if ((lmp->is[0] != '\0' || lmp->begline) |
| 4296 |
&& (rmp->is[0] != '\0' || rmp->endline)) |
| 4297 |
{ |
| 4298 |
lmp->is = icatalloc (lmp->is, rmp->is); |
| 4299 |
lmp->endline = rmp->endline; |
| 4300 |
} |
| 4301 |
else |
| 4302 |
{ |
| 4303 |
lmp->is[0] = '\0'; |
| 4304 |
lmp->begline = false; |
| 4305 |
lmp->endline = false; |
| 4306 |
} |
| 4307 |
freemust (rmp); |
| 4308 |
} |
| 4309 |
break; |
| 4310 |
|
| 4311 |
case '\0': |
| 4312 |
/* Not on *my* shift. */ |
| 4313 |
goto done; |
| 4314 |
|
| 4315 |
default: |
| 4316 |
if (CSET <= t) |
| 4317 |
{ |
| 4318 |
/* If T is a singleton, or if case-folding in a unibyte |
| 4319 |
locale and T's members all case-fold to the same char, |
| 4320 |
convert T to one of its members. Otherwise, do |
| 4321 |
nothing further with T. */ |
| 4322 |
charclass *ccl = &d->charclasses[t - CSET]; |
| 4323 |
int j; |
| 4324 |
for (j = 0; j < NOTCHAR; j++) |
| 4325 |
if (tstbit (j, ccl)) |
| 4326 |
break; |
| 4327 |
if (! (j < NOTCHAR)) |
| 4328 |
{ |
| 4329 |
mp = allocmust (mp, 2); |
| 4330 |
break; |
| 4331 |
} |
| 4332 |
t = j; |
| 4333 |
while (++j < NOTCHAR) |
| 4334 |
if (tstbit (j, ccl) |
| 4335 |
&& ! (case_fold_unibyte |
| 4336 |
&& toupper (j) == toupper (t))) |
| 4337 |
break; |
| 4338 |
if (j < NOTCHAR) |
| 4339 |
{ |
| 4340 |
mp = allocmust (mp, 2); |
| 4341 |
break; |
| 4342 |
} |
| 4343 |
} |
| 4344 |
|
| 4345 |
idx_t rj = ri + 2; |
| 4346 |
if (d->tokens[ri + 1] == CAT) |
| 4347 |
{ |
| 4348 |
for (; rj < d->tindex - 1; rj += 2) |
| 4349 |
{ |
| 4350 |
if ((rj != ri && (d->tokens[rj] <= 0 |
| 4351 |
|| NOTCHAR <= d->tokens[rj])) |
| 4352 |
|| d->tokens[rj + 1] != CAT) |
| 4353 |
break; |
| 4354 |
} |
| 4355 |
} |
| 4356 |
mp = allocmust (mp, ((rj - ri) >> 1) + 1); |
| 4357 |
mp->is[0] = mp->left[0] = mp->right[0] |
| 4358 |
= case_fold_unibyte ? toupper (t) : t; |
| 4359 |
|
| 4360 |
idx_t i; |
| 4361 |
for (i = 1; ri + 2 < rj; i++) |
| 4362 |
{ |
| 4363 |
ri += 2; |
| 4364 |
t = d->tokens[ri]; |
| 4365 |
mp->is[i] = mp->left[i] = mp->right[i] |
| 4366 |
= case_fold_unibyte ? toupper (t) : t; |
| 4367 |
} |
| 4368 |
mp->is[i] = mp->left[i] = mp->right[i] = '\0'; |
| 4369 |
mp->in = enlist (mp->in, mp->is, i); |
| 4370 |
break; |
| 4371 |
} |
| 4372 |
} |
| 4373 |
done:; |
| 4374 |
|
| 4375 |
struct dfamust *dm = NULL; |
| 4376 |
if (*result) |
| 4377 |
{ |
| 4378 |
dm = xmalloc (FLEXSIZEOF (struct dfamust, must, strlen (result) + 1)); |
| 4379 |
dm->exact = exact; |
| 4380 |
dm->begline = begline; |
| 4381 |
dm->endline = endline; |
| 4382 |
strcpy (dm->must, result); |
| 4383 |
} |
| 4384 |
|
| 4385 |
while (mp) |
| 4386 |
{ |
| 4387 |
must *prev = mp->prev; |
| 4388 |
freemust (mp); |
| 4389 |
mp = prev; |
| 4390 |
} |
| 4391 |
|
| 4392 |
return dm; |
| 4393 |
} |
| 4394 |
|
| 4395 |
void |
| 4396 |
dfamustfree (struct dfamust *dm) |
| 4397 |
{ |
| 4398 |
free (dm); |
| 4399 |
} |
| 4400 |
|
| 4401 |
struct dfa * |
| 4402 |
dfaalloc (void) |
| 4403 |
{ |
| 4404 |
return xmalloc (sizeof (struct dfa)); |
| 4405 |
} |
| 4406 |
|
| 4407 |
/* Initialize DFA. */ |
| 4408 |
void |
| 4409 |
dfasyntax (struct dfa *dfa, struct localeinfo const *linfo, |
| 4410 |
reg_syntax_t bits, int dfaopts) |
| 4411 |
{ |
| 4412 |
memset (dfa, 0, offsetof (struct dfa, dfaexec)); |
| 4413 |
dfa->dfaexec = linfo->multibyte ? dfaexec_mb : dfaexec_sb; |
| 4414 |
dfa->localeinfo = *linfo; |
| 4415 |
|
| 4416 |
dfa->fast = !dfa->localeinfo.multibyte; |
| 4417 |
|
| 4418 |
dfa->canychar = -1; |
| 4419 |
dfa->syntax.syntax_bits_set = true; |
| 4420 |
dfa->syntax.case_fold = (bits & RE_ICASE) != 0; |
| 4421 |
dfa->syntax.eolbyte = dfaopts & DFA_EOL_NUL ? '\0' : '\n'; |
| 4422 |
dfa->syntax.syntax_bits = bits; |
| 4423 |
dfa->syntax.dfaopts = dfaopts; |
| 4424 |
|
| 4425 |
for (int i = CHAR_MIN; i <= CHAR_MAX; ++i) |
| 4426 |
{ |
| 4427 |
unsigned char uc = i; |
| 4428 |
|
| 4429 |
dfa->syntax.sbit[uc] = char_context (dfa, uc); |
| 4430 |
switch (dfa->syntax.sbit[uc]) |
| 4431 |
{ |
| 4432 |
case CTX_LETTER: |
| 4433 |
setbit (uc, &dfa->syntax.letters); |
| 4434 |
break; |
| 4435 |
case CTX_NEWLINE: |
| 4436 |
setbit (uc, &dfa->syntax.newline); |
| 4437 |
break; |
| 4438 |
} |
| 4439 |
|
| 4440 |
/* POSIX requires that the five bytes in "\n\r./" (including the |
| 4441 |
terminating NUL) cannot occur inside a multibyte character. */ |
| 4442 |
dfa->syntax.never_trail[uc] = (dfa->localeinfo.using_utf8 |
| 4443 |
? (uc & 0xc0) != 0x80 |
| 4444 |
: strchr ("\n\r./", uc) != NULL); |
| 4445 |
} |
| 4446 |
} |
| 4447 |
|
| 4448 |
/* Initialize TO by copying FROM's syntax settings. */ |
| 4449 |
void |
| 4450 |
dfacopysyntax (struct dfa *to, struct dfa const *from) |
| 4451 |
{ |
| 4452 |
memset (to, 0, offsetof (struct dfa, syntax)); |
| 4453 |
to->canychar = -1; |
| 4454 |
to->fast = from->fast; |
| 4455 |
to->syntax = from->syntax; |
| 4456 |
to->dfaexec = from->dfaexec; |
| 4457 |
to->localeinfo = from->localeinfo; |
| 4458 |
} |
| 4459 |
|
| 4460 |
/* vim:set shiftwidth=2: */ |