1 /* dfa.c - deterministic extended regexp routines for GNU
2 Copyright (C) 1988, 1998, 2000, 2002, 2004-2005, 2007-2020 Free Software
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)
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.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
18 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA */
20 /* Written June, 1988 by Mike Haertel
21 Modified July, 1988 by Arthur David Olson to assist BMG speedups */
27 #include "flexmember.h"
37 /* Another name for ptrdiff_t, for sizes of objects and nonnegative
38 indexes into objects. It is signed to help catch integer overflow.
39 It has its own name because it is for nonnegative values only. */
40 typedef ptrdiff_t idx_t
;
41 static idx_t
const IDX_MAX
= PTRDIFF_MAX
;
44 streq (char const *a
, char const *b
)
46 return strcmp (a
, b
) == 0;
52 return '0' <= c
&& c
<= '9';
56 #define _(str) gettext (str)
62 #include "localeinfo.h"
65 # if 201710L < __STDC_VERSION__
66 # define FALLTHROUGH [[__fallthrough__]]
67 # elif (__GNUC__ >= 7) || (__clang_major__ >= 10)
68 # define FALLTHROUGH __attribute__ ((__fallthrough__))
70 # define FALLTHROUGH ((void) 0)
75 # define MIN(a,b) ((a) < (b) ? (a) : (b))
78 /* HPUX defines these as macros in sys/param.h. */
86 /* For code that does not use Gnulib’s isblank module. */
87 #if !defined isblank && !defined HAVE_ISBLANK && !defined GNULIB_ISBLANK
88 # define isblank dfa_isblank
92 return c
== ' ' || c
== '\t';
96 /* First integer value that is greater than any character code. */
97 enum { NOTCHAR
= 1 << CHAR_BIT
};
99 #ifdef UINT_LEAST64_MAX
101 /* Number of bits used in a charclass word. */
102 enum { CHARCLASS_WORD_BITS
= 64 };
104 /* This represents part of a character class. It must be unsigned and
105 at least CHARCLASS_WORD_BITS wide. Any excess bits are zero. */
106 typedef uint_least64_t charclass_word
;
108 /* Part of a charclass initializer that represents 64 bits' worth of a
109 charclass, where LO and HI are the low and high-order 32 bits of
110 the 64-bit quantity. */
111 # define CHARCLASS_PAIR(lo, hi) (((charclass_word) (hi) << 32) + (lo))
114 /* Fallbacks for pre-C99 hosts that lack 64-bit integers. */
115 enum { CHARCLASS_WORD_BITS
= 32 };
116 typedef unsigned long charclass_word
;
117 # define CHARCLASS_PAIR(lo, hi) lo, hi
120 /* An initializer for a charclass whose 32-bit words are A through H. */
121 #define CHARCLASS_INIT(a, b, c, d, e, f, g, h) \
123 CHARCLASS_PAIR (a, b), CHARCLASS_PAIR (c, d), \
124 CHARCLASS_PAIR (e, f), CHARCLASS_PAIR (g, h) \
127 /* The maximum useful value of a charclass_word; all used bits are 1. */
128 static charclass_word
const CHARCLASS_WORD_MASK
129 = ((charclass_word
) 1 << (CHARCLASS_WORD_BITS
- 1) << 1) - 1;
131 /* Number of words required to hold a bit for every character. */
134 CHARCLASS_WORDS
= (NOTCHAR
+ CHARCLASS_WORD_BITS
- 1) / CHARCLASS_WORD_BITS
137 /* Sets of unsigned characters are stored as bit vectors in arrays of ints. */
138 typedef struct { charclass_word w
[CHARCLASS_WORDS
]; } charclass
;
140 /* Convert a possibly-signed character to an unsigned character. This is
141 a bit safer than casting to unsigned char, since it catches some type
142 errors that the cast doesn't. */
149 /* Contexts tell us whether a character is a newline or a word constituent.
150 Word-constituent characters are those that satisfy iswalnum, plus '_'.
151 Each character has a single CTX_* value; bitmasks of CTX_* values denote
152 a particular character class.
154 A state also stores a context value, which is a bitmask of CTX_* values.
155 A state's context represents a set of characters that the state's
156 predecessors must match. For example, a state whose context does not
157 include CTX_LETTER will never have transitions where the previous
158 character is a word constituent. A state whose context is CTX_ANY
159 might have transitions from any character. */
169 /* Sometimes characters can only be matched depending on the surrounding
170 context. Such context decisions depend on what the previous character
171 was, and the value of the current (lookahead) character. Context
172 dependent constraints are encoded as 9-bit integers. Each bit that
173 is set indicates that the constraint succeeds in the corresponding
176 bit 6-8 - valid contexts when next character is CTX_NEWLINE
177 bit 3-5 - valid contexts when next character is CTX_LETTER
178 bit 0-2 - valid contexts when next character is CTX_NONE
180 succeeds_in_context determines whether a given constraint
181 succeeds in a particular context. Prev is a bitmask of possible
182 context values for the previous character, curr is the (single-bit)
183 context value for the lookahead character. */
185 newline_constraint (int constraint
)
187 return (constraint
>> 6) & 7;
190 letter_constraint (int constraint
)
192 return (constraint
>> 3) & 7;
195 other_constraint (int constraint
)
197 return constraint
& 7;
201 succeeds_in_context (int constraint
, int prev
, int curr
)
203 return !! (((curr
& CTX_NONE
? other_constraint (constraint
) : 0) \
204 | (curr
& CTX_LETTER
? letter_constraint (constraint
) : 0) \
205 | (curr
& CTX_NEWLINE
? newline_constraint (constraint
) : 0)) \
209 /* The following describe what a constraint depends on. */
211 prev_newline_dependent (int constraint
)
213 return ((constraint
^ constraint
>> 2) & 0111) != 0;
216 prev_letter_dependent (int constraint
)
218 return ((constraint
^ constraint
>> 1) & 0111) != 0;
221 /* Tokens that match the empty string subject to some constraint actually
222 work by applying that constraint to determine what may follow them,
223 taking into account what has gone before. The following values are
224 the constraints corresponding to the special tokens previously defined. */
227 NO_CONSTRAINT
= 0777,
228 BEGLINE_CONSTRAINT
= 0444,
229 ENDLINE_CONSTRAINT
= 0700,
230 BEGWORD_CONSTRAINT
= 0050,
231 ENDWORD_CONSTRAINT
= 0202,
232 LIMWORD_CONSTRAINT
= 0252,
233 NOTLIMWORD_CONSTRAINT
= 0525
236 /* The regexp is parsed into an array of tokens in postfix form. Some tokens
237 are operators and others are terminal symbols. Most (but not all) of these
238 codes are returned by the lexical analyzer. */
240 typedef ptrdiff_t token
;
241 static token
const TOKEN_MAX
= PTRDIFF_MAX
;
243 /* States are indexed by state_num values. These are normally
244 nonnegative but -1 is used as a special value. */
245 typedef ptrdiff_t state_num
;
247 /* Predefined token values. */
250 END
= -1, /* END is a terminal symbol that matches the
251 end of input; any value of END or less in
252 the parse tree is such a symbol. Accepting
253 states of the DFA are those that would have
254 a transition on END. This is -1, not some
255 more-negative value, to tweak the speed of
256 comparisons to END. */
258 /* Ordinary character values are terminal symbols that match themselves. */
260 /* CSET must come last in the following list of special tokens. Otherwise,
261 the list order matters only for performance. Related special tokens
262 should have nearby values so that code like (t == ANYCHAR || t == MBCSET
263 || CSET <= t) can be done with a single machine-level comparison. */
265 EMPTY
= NOTCHAR
, /* EMPTY is a terminal symbol that matches
268 QMARK
, /* QMARK is an operator of one argument that
269 matches zero or one occurrences of its
272 STAR
, /* STAR is an operator of one argument that
273 matches the Kleene closure (zero or more
274 occurrences) of its argument. */
276 PLUS
, /* PLUS is an operator of one argument that
277 matches the positive closure (one or more
278 occurrences) of its argument. */
280 REPMN
, /* REPMN is a lexical token corresponding
281 to the {m,n} construct. REPMN never
282 appears in the compiled token vector. */
284 CAT
, /* CAT is an operator of two arguments that
285 matches the concatenation of its
286 arguments. CAT is never returned by the
289 OR
, /* OR is an operator of two arguments that
290 matches either of its arguments. */
292 LPAREN
, /* LPAREN never appears in the parse tree,
293 it is only a lexeme. */
295 RPAREN
, /* RPAREN never appears in the parse tree. */
297 WCHAR
, /* Only returned by lex. wctok contains
298 the wide character representation. */
300 ANYCHAR
, /* ANYCHAR is a terminal symbol that matches
301 a valid multibyte (or single byte) character.
302 It is used only if MB_CUR_MAX > 1. */
304 BEG
, /* BEG is an initial symbol that matches the
305 beginning of input. */
307 BEGLINE
, /* BEGLINE is a terminal symbol that matches
308 the empty string at the beginning of a
311 ENDLINE
, /* ENDLINE is a terminal symbol that matches
312 the empty string at the end of a line. */
314 BEGWORD
, /* BEGWORD is a terminal symbol that matches
315 the empty string at the beginning of a
318 ENDWORD
, /* ENDWORD is a terminal symbol that matches
319 the empty string at the end of a word. */
321 LIMWORD
, /* LIMWORD is a terminal symbol that matches
322 the empty string at the beginning or the
325 NOTLIMWORD
, /* NOTLIMWORD is a terminal symbol that
326 matches the empty string not at
327 the beginning or end of a word. */
329 BACKREF
, /* BACKREF is generated by \<digit>
330 or by any other construct that
331 is not completely handled. If the scanner
332 detects a transition on backref, it returns
333 a kind of "semi-success" indicating that
334 the match will have to be verified with
335 a backtracking matcher. */
337 MBCSET
, /* MBCSET is similar to CSET, but for
338 multibyte characters. */
340 CSET
/* CSET and (and any value greater) is a
341 terminal symbol that matches any of a
342 class of characters. */
346 /* States of the recognizer correspond to sets of positions in the parse
347 tree, together with the constraints under which they may be matched.
348 So a position is encoded as an index into the parse tree together with
352 idx_t index
; /* Index into the parse array. */
353 unsigned int constraint
; /* Constraint for matching this position. */
356 /* Sets of positions are stored as arrays. */
359 position
*elems
; /* Elements of this position set. */
360 idx_t nelem
; /* Number of elements in this set. */
361 idx_t alloc
; /* Number of elements allocated in ELEMS. */
364 /* A state of the dfa consists of a set of positions, some flags,
365 and the token value of the lowest-numbered position of the state that
366 contains an END token. */
369 size_t hash
; /* Hash of the positions of this state. */
370 position_set elems
; /* Positions this state could match. */
371 unsigned char context
; /* Context from previous state. */
372 unsigned short constraint
; /* Constraint for this state to accept. */
373 position_set mbps
; /* Positions which can match multibyte
374 characters or the follows, e.g., period.
375 Used only if MB_CUR_MAX > 1. */
376 state_num mb_trindex
; /* Index of this state in MB_TRANS, or
377 negative if the state does not have
381 /* Maximum for any transition table count. This should be at least 3,
382 for the initial state setup. */
383 enum { MAX_TRCOUNT
= 1024 };
385 /* A bracket operator.
386 e.g., [a-c], [[:alpha:]], etc. */
387 struct mb_char_classes
391 wchar_t *chars
; /* Normal characters. */
398 /* Syntax bits controlling the behavior of the lexical analyzer. */
399 reg_syntax_t syntax_bits
;
400 bool syntax_bits_set
;
402 /* Flag for case-folding letters into sets. */
405 /* True if ^ and $ match only the start and end of data, and do not match
406 end-of-line within data. */
409 /* End-of-line byte in data. */
410 unsigned char eolbyte
;
412 /* Cache of char-context values. */
415 /* If never_trail[B], the byte B cannot be a non-initial byte in a
416 multibyte character. */
417 bool never_trail
[NOTCHAR
];
419 /* Set of characters considered letters. */
422 /* Set of characters that are newline. */
426 /* Lexical analyzer. All the dross that deals with the obnoxious
427 GNU Regex syntax bits is located here. The poor, suffering
428 reader is referred to the GNU Regex documentation for the
429 meaning of the @#%!@#%^!@ syntax bits. */
432 char const *ptr
; /* Pointer to next input character. */
433 idx_t left
; /* Number of characters remaining. */
434 token lasttok
; /* Previous token returned; initially END. */
435 idx_t parens
; /* Count of outstanding left parens. */
436 int minrep
, maxrep
; /* Repeat counts for {m,n}. */
438 /* Wide character representation of the current multibyte character,
439 or WEOF if there was an encoding error. Used only if
443 /* The most recently analyzed multibyte bracket expression. */
444 struct mb_char_classes brack
;
446 /* We're separated from beginning or (, | only by zero-width characters. */
450 /* Recursive descent parser for regular expressions. */
454 token tok
; /* Lookahead token. */
455 idx_t depth
; /* Current depth of a hypothetical stack
456 holding deferred productions. This is
457 used to determine the depth that will be
458 required of the real stack later on in
462 /* A compiled regular expression. */
465 /* Fields filled by the scanner. */
466 charclass
*charclasses
; /* Array of character sets for CSET tokens. */
467 idx_t cindex
; /* Index for adding new charclasses. */
468 idx_t calloc
; /* Number of charclasses allocated. */
469 ptrdiff_t canychar
; /* Index of anychar class, or -1. */
472 struct lexer_state lex
;
475 struct parser_state parse
;
477 /* Fields filled by the parser. */
478 token
*tokens
; /* Postfix parse array. */
479 idx_t tindex
; /* Index for adding new tokens. */
480 idx_t talloc
; /* Number of tokens currently allocated. */
481 idx_t depth
; /* Depth required of an evaluation stack
482 used for depth-first traversal of the
484 idx_t nleaves
; /* Number of non-EMPTY leaves
485 in the parse tree. */
486 idx_t nregexps
; /* Count of parallel regexps being built
488 bool fast
; /* The DFA is fast. */
489 bool epsilon
; /* Does a token match only the empty string? */
490 token utf8_anychar_classes
[9]; /* To lower ANYCHAR in UTF-8 locales. */
491 mbstate_t mbs
; /* Multibyte conversion state. */
493 /* The following are valid only if MB_CUR_MAX > 1. */
495 /* The value of multibyte_prop[i] is defined by following rule.
496 if tokens[i] < NOTCHAR
497 bit 0 : tokens[i] is the first byte of a character, including
498 single-byte characters.
499 bit 1 : tokens[i] is the last byte of a character, including
500 single-byte characters.
504 = 'single_byte_a', 'multi_byte_A', single_byte_b'
505 = 'sb_a', 'mb_A(1st byte)', 'mb_A(2nd byte)', 'mb_A(3rd byte)', 'sb_b'
509 char *multibyte_prop
;
511 /* Fields filled by the superset. */
512 struct dfa
*superset
; /* Hint of the dfa. */
514 /* Fields filled by the state builder. */
515 dfa_state
*states
; /* States of the dfa. */
516 state_num sindex
; /* Index for adding new states. */
517 idx_t salloc
; /* Number of states currently allocated. */
519 /* Fields filled by the parse tree->NFA conversion. */
520 position_set
*follows
; /* Array of follow sets, indexed by position
521 index. The follow of a position is the set
522 of positions containing characters that
523 could conceivably follow a character
524 matching the given position in a string
525 matching the regexp. Allocated to the
526 maximum possible position index. */
527 bool searchflag
; /* We are supposed to build a searching
528 as opposed to an exact matcher. A searching
529 matcher finds the first and shortest string
530 matching a regexp anywhere in the buffer,
531 whereas an exact matcher finds the longest
532 string matching, but anchored to the
533 beginning of the buffer. */
535 /* Fields filled by dfaanalyze. */
536 int *constraints
; /* Array of union of accepting constraints
537 in the follow of a position. */
538 int *separates
; /* Array of contexts on follow of a
541 /* Fields filled by dfaexec. */
542 state_num tralloc
; /* Number of transition tables that have
543 slots so far, not counting trans[-1] and
545 int trcount
; /* Number of transition tables that have
546 been built, other than for initial
548 int min_trcount
; /* Number of initial states. Equivalently,
549 the minimum state number for which trcount
550 counts transitions. */
551 state_num
**trans
; /* Transition tables for states that can
552 never accept. If the transitions for a
553 state have not yet been computed, or the
554 state could possibly accept, its entry in
555 this table is NULL. This points to two
556 past the start of the allocated array,
557 and trans[-1] and trans[-2] are always
559 state_num
**fails
; /* Transition tables after failing to accept
560 on a state that potentially could do so.
561 If trans[i] is non-null, fails[i] must
563 char *success
; /* Table of acceptance conditions used in
564 dfaexec and computed in build_state. */
565 state_num
*newlines
; /* Transitions on newlines. The entry for a
566 newline in any transition table is always
567 -1 so we can count lines without wasting
568 too many cycles. The transition for a
569 newline is stored separately and handled
570 as a special case. Newline is also used
571 as a sentinel at the end of the buffer. */
572 state_num initstate_notbol
; /* Initial state for CTX_LETTER and CTX_NONE
573 context in multibyte locales, in which we
574 do not distinguish between their contexts,
575 as not supported word. */
576 position_set mb_follows
; /* Follow set added by ANYCHAR on demand. */
577 state_num
**mb_trans
; /* Transition tables for states with
579 state_num mb_trcount
; /* Number of transition tables for states with
580 ANYCHAR that have actually been built. */
582 /* Syntax configuration. This is near the end so that dfacopysyntax
583 can memset up to here. */
584 struct regex_syntax syntax
;
586 /* Information derived from the locale. This is at the end so that
587 a quick memset need not clear it specially. */
589 /* dfaexec implementation. */
590 char *(*dfaexec
) (struct dfa
*, char const *, char *,
591 bool, ptrdiff_t *, bool *);
593 /* Other cached information derived from the locale. */
594 struct localeinfo localeinfo
;
597 /* User access to dfa internals. */
599 /* S could possibly be an accepting state of R. */
601 accepting (state_num s
, struct dfa
const *r
)
603 return r
->states
[s
].constraint
!= 0;
606 /* STATE accepts in the specified context. */
608 accepts_in_context (int prev
, int curr
, state_num state
, struct dfa
const *dfa
)
610 return succeeds_in_context (dfa
->states
[state
].constraint
, prev
, curr
);
613 static void regexp (struct dfa
*dfa
);
615 /* Store into *PWC the result of converting the leading bytes of the
616 multibyte buffer S of length N bytes, using D->localeinfo.sbctowc
617 and updating the conversion state in *D. On conversion error,
618 convert just a single byte, to WEOF. Return the number of bytes
621 This differs from mbrtowc (PWC, S, N, &D->mbs) as follows:
623 * PWC points to wint_t, not to wchar_t.
624 * The last arg is a dfa *D instead of merely a multibyte conversion
626 * N must be at least 1.
627 * S[N - 1] must be a sentinel byte.
628 * Shift encodings are not supported.
629 * The return value is always in the range 1..N.
630 * D->mbs is always valid afterwards.
631 * *PWC is always set to something. */
633 mbs_to_wchar (wint_t *pwc
, char const *s
, size_t n
, struct dfa
*d
)
635 unsigned char uc
= s
[0];
636 wint_t wc
= d
->localeinfo
.sbctowc
[uc
];
641 size_t nbytes
= mbrtowc (&wch
, s
, n
, &d
->mbs
);
642 if (0 < nbytes
&& nbytes
< (size_t) -2)
647 memset (&d
->mbs
, 0, sizeof d
->mbs
);
660 fprintf (stderr
, "END");
661 else if (0 <= t
&& t
< NOTCHAR
)
664 fprintf (stderr
, "0x%02x", ch
);
729 fprintf (stderr
, "%s", s
);
734 /* Stuff pertaining to charclasses. */
737 tstbit (unsigned int b
, charclass
const *c
)
739 return c
->w
[b
/ CHARCLASS_WORD_BITS
] >> b
% CHARCLASS_WORD_BITS
& 1;
743 setbit (unsigned int b
, charclass
*c
)
745 charclass_word one
= 1;
746 c
->w
[b
/ CHARCLASS_WORD_BITS
] |= one
<< b
% CHARCLASS_WORD_BITS
;
750 clrbit (unsigned int b
, charclass
*c
)
752 charclass_word one
= 1;
753 c
->w
[b
/ CHARCLASS_WORD_BITS
] &= ~(one
<< b
% CHARCLASS_WORD_BITS
);
757 zeroset (charclass
*s
)
759 memset (s
, 0, sizeof *s
);
763 fillset (charclass
*s
)
765 for (int i
= 0; i
< CHARCLASS_WORDS
; i
++)
766 s
->w
[i
] = CHARCLASS_WORD_MASK
;
770 notset (charclass
*s
)
772 for (int i
= 0; i
< CHARCLASS_WORDS
; ++i
)
773 s
->w
[i
] = CHARCLASS_WORD_MASK
& ~s
->w
[i
];
777 equal (charclass
const *s1
, charclass
const *s2
)
779 charclass_word w
= 0;
780 for (int i
= 0; i
< CHARCLASS_WORDS
; i
++)
781 w
|= s1
->w
[i
] ^ s2
->w
[i
];
786 emptyset (charclass
const *s
)
788 charclass_word w
= 0;
789 for (int i
= 0; i
< CHARCLASS_WORDS
; i
++)
794 /* Grow PA, which points to an array of *NITEMS items, and return the
795 location of the reallocated array, updating *NITEMS to reflect its
796 new size. The new array will contain at least NITEMS_INCR_MIN more
797 items, but will not contain more than NITEMS_MAX items total.
798 ITEM_SIZE is the size of each item, in bytes.
800 ITEM_SIZE and NITEMS_INCR_MIN must be positive. *NITEMS must be
801 nonnegative. If NITEMS_MAX is -1, it is treated as if it were
804 If PA is null, then allocate a new array instead of reallocating
807 Thus, to grow an array A without saving its old contents, do
808 { free (A); A = xpalloc (NULL, &AITEMS, ...); }. */
811 xpalloc (void *pa
, idx_t
*nitems
, idx_t nitems_incr_min
,
812 ptrdiff_t nitems_max
, idx_t item_size
)
816 /* The approximate size to use for initial small allocation
817 requests. This is the largest "small" request for the GNU C
819 enum { DEFAULT_MXFAST
= 64 * sizeof (size_t) / 4 };
821 /* If the array is tiny, grow it to about (but no greater than)
822 DEFAULT_MXFAST bytes. Otherwise, grow it by about 50%.
823 Adjust the growth according to three constraints: NITEMS_INCR_MIN,
824 NITEMS_MAX, and what the C language can represent safely. */
827 if (INT_ADD_WRAPV (n0
, n0
>> 1, &n
))
829 if (0 <= nitems_max
&& nitems_max
< n
)
832 idx_t adjusted_nbytes
833 = ((INT_MULTIPLY_WRAPV (n
, item_size
, &nbytes
) || SIZE_MAX
< nbytes
)
834 ? MIN (IDX_MAX
, SIZE_MAX
)
835 : nbytes
< DEFAULT_MXFAST
? DEFAULT_MXFAST
: 0);
838 n
= adjusted_nbytes
/ item_size
;
839 nbytes
= adjusted_nbytes
- adjusted_nbytes
% item_size
;
844 if (n
- n0
< nitems_incr_min
845 && (INT_ADD_WRAPV (n0
, nitems_incr_min
, &n
)
846 || (0 <= nitems_max
&& nitems_max
< n
)
847 || INT_MULTIPLY_WRAPV (n
, item_size
, &nbytes
)))
849 pa
= xrealloc (pa
, nbytes
);
854 /* Ensure that the array addressed by PA holds at least I + 1 items.
855 Either return PA, or reallocate the array and return its new address.
856 Although PA may be null, the returned value is never null.
858 The array holds *NITEMS items, where 0 <= I <= *NITEMS; *NITEMS
859 is updated on reallocation. If PA is null, *NITEMS must be zero.
860 Do not allocate more than NITEMS_MAX items total; -1 means no limit.
861 ITEM_SIZE is the size of one item; it must be positive.
862 Avoid O(N**2) behavior on arrays growing linearly. */
864 maybe_realloc (void *pa
, idx_t i
, idx_t
*nitems
,
865 ptrdiff_t nitems_max
, idx_t item_size
)
869 return xpalloc (pa
, nitems
, 1, nitems_max
, item_size
);
872 /* In DFA D, find the index of charclass S, or allocate a new one. */
874 charclass_index (struct dfa
*d
, charclass
const *s
)
878 for (i
= 0; i
< d
->cindex
; ++i
)
879 if (equal (s
, &d
->charclasses
[i
]))
881 d
->charclasses
= maybe_realloc (d
->charclasses
, d
->cindex
, &d
->calloc
,
882 TOKEN_MAX
- CSET
, sizeof *d
->charclasses
);
884 d
->charclasses
[i
] = *s
;
889 unibyte_word_constituent (struct dfa
const *dfa
, unsigned char c
)
891 return dfa
->localeinfo
.sbctowc
[c
] != WEOF
&& (isalnum (c
) || (c
) == '_');
895 char_context (struct dfa
const *dfa
, unsigned char c
)
897 if (c
== dfa
->syntax
.eolbyte
&& !dfa
->syntax
.anchor
)
899 if (unibyte_word_constituent (dfa
, c
))
904 /* Set a bit in the charclass for the given wchar_t. Do nothing if WC
905 is represented by a multi-byte sequence. Even for MB_CUR_MAX == 1,
906 this may happen when folding case in weird Turkish locales where
907 dotless i/dotted I are not included in the chosen character set.
908 Return whether a bit was set in the charclass. */
910 setbit_wc (wint_t wc
, charclass
*c
)
920 /* Set a bit for B and its case variants in the charclass C.
921 MB_CUR_MAX must be 1. */
923 setbit_case_fold_c (int b
, charclass
*c
)
925 int ub
= toupper (b
);
926 for (int i
= 0; i
< NOTCHAR
; i
++)
927 if (toupper (i
) == ub
)
931 /* Fetch the next lexical input character from the pattern. There
932 must at least one byte of pattern input. Set DFA->lex.wctok to the
933 value of the character or to WEOF depending on whether the input is
934 a valid multibyte character (possibly of length 1). Then return
935 the next input byte value, except return EOF if the input is a
936 multibyte character of length greater than 1. */
938 fetch_wc (struct dfa
*dfa
)
940 int nbytes
= mbs_to_wchar (&dfa
->lex
.wctok
, dfa
->lex
.ptr
, dfa
->lex
.left
,
942 int c
= nbytes
== 1 ? to_uchar (dfa
->lex
.ptr
[0]) : EOF
;
943 dfa
->lex
.ptr
+= nbytes
;
944 dfa
->lex
.left
-= nbytes
;
948 /* If there is no more input, report an error about unbalanced brackets.
949 Otherwise, behave as with fetch_wc (DFA). */
951 bracket_fetch_wc (struct dfa
*dfa
)
954 dfaerror (_("unbalanced ["));
955 return fetch_wc (dfa
);
958 typedef int predicate (int);
960 /* The following list maps the names of the Posix named character classes
961 to predicate functions that determine whether a given character is in
962 the class. The leading [ has already been eaten by the lexical
968 bool single_byte_only
;
971 static const struct dfa_ctype prednames
[] = {
972 {"alpha", isalpha
, false},
973 {"upper", isupper
, false},
974 {"lower", islower
, false},
975 {"digit", isdigit
, true},
976 {"xdigit", isxdigit
, false},
977 {"space", isspace
, false},
978 {"punct", ispunct
, false},
979 {"alnum", isalnum
, false},
980 {"print", isprint
, false},
981 {"graph", isgraph
, false},
982 {"cntrl", iscntrl
, false},
983 {"blank", isblank
, false},
987 static const struct dfa_ctype
*_GL_ATTRIBUTE_PURE
988 find_pred (const char *str
)
990 for (int i
= 0; prednames
[i
].name
; i
++)
991 if (streq (str
, prednames
[i
].name
))
992 return &prednames
[i
];
996 /* Parse a bracket expression, which possibly includes multibyte
999 parse_bracket_exp (struct dfa
*dfa
)
1001 /* This is a bracket expression that dfaexec is known to
1002 process correctly. */
1003 bool known_bracket_exp
= true;
1005 /* Used to warn about [:space:].
1006 Bit 0 = first character is a colon.
1007 Bit 1 = last character is a colon.
1008 Bit 2 = includes any other character but a colon.
1009 Bit 3 = includes ranges, char/equiv classes or collation elements. */
1010 int colon_warning_state
;
1012 dfa
->lex
.brack
.nchars
= 0;
1015 int c
= bracket_fetch_wc (dfa
);
1016 bool invert
= c
== '^';
1019 c
= bracket_fetch_wc (dfa
);
1020 known_bracket_exp
= dfa
->localeinfo
.simple
;
1022 wint_t wc
= dfa
->lex
.wctok
;
1025 colon_warning_state
= (c
== ':');
1028 c1
= NOTCHAR
; /* Mark c1 as not initialized. */
1029 colon_warning_state
&= ~2;
1031 /* Note that if we're looking at some other [:...:] construct,
1032 we just treat it as a bunch of ordinary characters. We can do
1033 this because we assume regex has checked for syntax errors before
1034 dfa is ever called. */
1037 c1
= bracket_fetch_wc (dfa
);
1038 wc1
= dfa
->lex
.wctok
;
1040 if ((c1
== ':' && (dfa
->syntax
.syntax_bits
& RE_CHAR_CLASSES
))
1041 || c1
== '.' || c1
== '=')
1043 enum { MAX_BRACKET_STRING_LEN
= 32 };
1044 char str
[MAX_BRACKET_STRING_LEN
+ 1];
1048 c
= bracket_fetch_wc (dfa
);
1049 if (dfa
->lex
.left
== 0
1050 || (c
== c1
&& dfa
->lex
.ptr
[0] == ']'))
1052 if (len
< MAX_BRACKET_STRING_LEN
)
1055 /* This is in any case an invalid class name. */
1060 /* Fetch bracket. */
1061 c
= bracket_fetch_wc (dfa
);
1062 wc
= dfa
->lex
.wctok
;
1064 /* Build character class. POSIX allows character
1065 classes to match multicharacter collating elements,
1066 but the regex code does not support that, so do not
1067 worry about that possibility. */
1070 = (dfa
->syntax
.case_fold
&& (streq (str
, "upper")
1071 || streq (str
, "lower"))
1073 const struct dfa_ctype
*pred
= find_pred (class);
1075 dfaerror (_("invalid character class"));
1077 if (dfa
->localeinfo
.multibyte
&& !pred
->single_byte_only
)
1078 known_bracket_exp
= false;
1080 for (int c2
= 0; c2
< NOTCHAR
; ++c2
)
1081 if (pred
->func (c2
))
1085 known_bracket_exp
= false;
1087 colon_warning_state
|= 8;
1089 /* Fetch new lookahead character. */
1090 c1
= bracket_fetch_wc (dfa
);
1091 wc1
= dfa
->lex
.wctok
;
1095 /* We treat '[' as a normal character here. c/c1/wc/wc1
1096 are already set up. */
1100 && (dfa
->syntax
.syntax_bits
& RE_BACKSLASH_ESCAPE_IN_LISTS
))
1102 c
= bracket_fetch_wc (dfa
);
1103 wc
= dfa
->lex
.wctok
;
1108 c1
= bracket_fetch_wc (dfa
);
1109 wc1
= dfa
->lex
.wctok
;
1113 /* build range characters. */
1115 int c2
= bracket_fetch_wc (dfa
);
1116 wint_t wc2
= dfa
->lex
.wctok
;
1118 /* A bracket expression like [a-[.aa.]] matches an unknown set.
1119 Treat it like [-a[.aa.]] while parsing it, and
1120 remember that the set is unknown. */
1121 if (c2
== '[' && dfa
->lex
.ptr
[0] == '.')
1123 known_bracket_exp
= false;
1129 /* In the case [x-], the - is an ordinary hyphen,
1130 which is left in c1, the lookahead character. */
1136 if (c2
== '\\' && (dfa
->syntax
.syntax_bits
1137 & RE_BACKSLASH_ESCAPE_IN_LISTS
))
1139 c2
= bracket_fetch_wc (dfa
);
1140 wc2
= dfa
->lex
.wctok
;
1143 colon_warning_state
|= 8;
1144 c1
= bracket_fetch_wc (dfa
);
1145 wc1
= dfa
->lex
.wctok
;
1147 /* Treat [x-y] as a range if x != y. */
1148 if (wc
!= wc2
|| wc
== WEOF
)
1150 if (dfa
->localeinfo
.simple
1151 || (isasciidigit (c
) & isasciidigit (c2
)))
1153 for (int ci
= c
; ci
<= c2
; ci
++)
1154 if (dfa
->syntax
.case_fold
&& isalpha (ci
))
1155 setbit_case_fold_c (ci
, &ccl
);
1160 known_bracket_exp
= false;
1167 colon_warning_state
|= (c
== ':') ? 2 : 4;
1169 if (!dfa
->localeinfo
.multibyte
)
1171 if (dfa
->syntax
.case_fold
&& isalpha (c
))
1172 setbit_case_fold_c (c
, &ccl
);
1179 known_bracket_exp
= false;
1182 wchar_t folded
[CASE_FOLDED_BUFSIZE
+ 1];
1183 int n
= (dfa
->syntax
.case_fold
1184 ? case_folded_counterparts (wc
, folded
+ 1) + 1
1187 for (int i
= 0; i
< n
; i
++)
1188 if (!setbit_wc (folded
[i
], &ccl
))
1190 dfa
->lex
.brack
.chars
1191 = maybe_realloc (dfa
->lex
.brack
.chars
, dfa
->lex
.brack
.nchars
,
1192 &dfa
->lex
.brack
.nchars_alloc
, -1,
1193 sizeof *dfa
->lex
.brack
.chars
);
1194 dfa
->lex
.brack
.chars
[dfa
->lex
.brack
.nchars
++] = folded
[i
];
1198 while ((wc
= wc1
, (c
= c1
) != ']'));
1200 if (colon_warning_state
== 7)
1201 dfawarn (_("character class syntax is [[:space:]], not [:space:]"));
1203 if (! known_bracket_exp
)
1206 if (dfa
->localeinfo
.multibyte
&& (invert
|| dfa
->lex
.brack
.nchars
!= 0))
1208 dfa
->lex
.brack
.invert
= invert
;
1209 dfa
->lex
.brack
.cset
= emptyset (&ccl
) ? -1 : charclass_index (dfa
, &ccl
);
1216 if (dfa
->syntax
.syntax_bits
& RE_HAT_LISTS_NOT_NEWLINE
)
1217 clrbit ('\n', &ccl
);
1220 return CSET
+ charclass_index (dfa
, &ccl
);
1230 push_lex_state (struct dfa
*dfa
, struct lexptr
*ls
, char const *s
)
1232 ls
->ptr
= dfa
->lex
.ptr
;
1233 ls
->left
= dfa
->lex
.left
;
1235 dfa
->lex
.left
= strlen (s
);
1239 pop_lex_state (struct dfa
*dfa
, struct lexptr
const *ls
)
1241 dfa
->lex
.ptr
= ls
->ptr
;
1242 dfa
->lex
.left
= ls
->left
;
1246 lex (struct dfa
*dfa
)
1248 bool backslash
= false;
1250 /* Basic plan: We fetch a character. If it's a backslash,
1251 we set the backslash flag and go through the loop again.
1252 On the plus side, this avoids having a duplicate of the
1253 main switch inside the backslash case. On the minus side,
1254 it means that just about every case begins with
1255 "if (backslash) ...". */
1256 for (int i
= 0; i
< 2; ++i
)
1258 if (! dfa
->lex
.left
)
1259 return dfa
->lex
.lasttok
= END
;
1260 int c
= fetch_wc (dfa
);
1267 if (dfa
->lex
.left
== 0)
1268 dfaerror (_("unfinished \\ escape"));
1275 if (dfa
->syntax
.syntax_bits
& RE_CONTEXT_INDEP_ANCHORS
1276 || dfa
->lex
.lasttok
== END
|| dfa
->lex
.lasttok
== LPAREN
1277 || dfa
->lex
.lasttok
== OR
)
1278 return dfa
->lex
.lasttok
= BEGLINE
;
1284 if (dfa
->syntax
.syntax_bits
& RE_CONTEXT_INDEP_ANCHORS
1285 || dfa
->lex
.left
== 0
1287 > !(dfa
->syntax
.syntax_bits
& RE_NO_BK_PARENS
))
1288 && (dfa
->lex
.ptr
[!(dfa
->syntax
.syntax_bits
& RE_NO_BK_PARENS
)
1289 & (dfa
->lex
.ptr
[0] == '\\')]
1292 > !(dfa
->syntax
.syntax_bits
& RE_NO_BK_VBAR
))
1293 && (dfa
->lex
.ptr
[!(dfa
->syntax
.syntax_bits
& RE_NO_BK_VBAR
)
1294 & (dfa
->lex
.ptr
[0] == '\\')]
1296 || ((dfa
->syntax
.syntax_bits
& RE_NEWLINE_ALT
)
1297 && dfa
->lex
.left
> 0 && dfa
->lex
.ptr
[0] == '\n'))
1298 return dfa
->lex
.lasttok
= ENDLINE
;
1310 if (backslash
&& !(dfa
->syntax
.syntax_bits
& RE_NO_BK_REFS
))
1312 dfa
->lex
.laststart
= false;
1313 return dfa
->lex
.lasttok
= BACKREF
;
1318 if (backslash
&& !(dfa
->syntax
.syntax_bits
& RE_NO_GNU_OPS
))
1320 /* FIXME: should be beginning of string */
1321 return dfa
->lex
.lasttok
= BEGLINE
;
1326 if (backslash
&& !(dfa
->syntax
.syntax_bits
& RE_NO_GNU_OPS
))
1328 /* FIXME: should be end of string */
1329 return dfa
->lex
.lasttok
= ENDLINE
;
1334 if (backslash
&& !(dfa
->syntax
.syntax_bits
& RE_NO_GNU_OPS
))
1335 return dfa
->lex
.lasttok
= BEGWORD
;
1339 if (backslash
&& !(dfa
->syntax
.syntax_bits
& RE_NO_GNU_OPS
))
1340 return dfa
->lex
.lasttok
= ENDWORD
;
1344 if (backslash
&& !(dfa
->syntax
.syntax_bits
& RE_NO_GNU_OPS
))
1345 return dfa
->lex
.lasttok
= LIMWORD
;
1349 if (backslash
&& !(dfa
->syntax
.syntax_bits
& RE_NO_GNU_OPS
))
1350 return dfa
->lex
.lasttok
= NOTLIMWORD
;
1354 if (dfa
->syntax
.syntax_bits
& RE_LIMITED_OPS
)
1356 if (backslash
!= ((dfa
->syntax
.syntax_bits
& RE_BK_PLUS_QM
) != 0))
1358 if (!(dfa
->syntax
.syntax_bits
& RE_CONTEXT_INDEP_OPS
)
1359 && dfa
->lex
.laststart
)
1361 return dfa
->lex
.lasttok
= QMARK
;
1366 if (!(dfa
->syntax
.syntax_bits
& RE_CONTEXT_INDEP_OPS
)
1367 && dfa
->lex
.laststart
)
1369 return dfa
->lex
.lasttok
= STAR
;
1372 if (dfa
->syntax
.syntax_bits
& RE_LIMITED_OPS
)
1374 if (backslash
!= ((dfa
->syntax
.syntax_bits
& RE_BK_PLUS_QM
) != 0))
1376 if (!(dfa
->syntax
.syntax_bits
& RE_CONTEXT_INDEP_OPS
)
1377 && dfa
->lex
.laststart
)
1379 return dfa
->lex
.lasttok
= PLUS
;
1382 if (!(dfa
->syntax
.syntax_bits
& RE_INTERVALS
))
1384 if (backslash
!= ((dfa
->syntax
.syntax_bits
& RE_NO_BK_BRACES
) == 0))
1386 if (!(dfa
->syntax
.syntax_bits
& RE_CONTEXT_INDEP_OPS
)
1387 && dfa
->lex
.laststart
)
1392 {M,} - minimum count, maximum is infinity
1394 {,} - 0 to infinity (same as '*')
1395 {M,N} - M through N */
1397 char const *p
= dfa
->lex
.ptr
;
1398 char const *lim
= p
+ dfa
->lex
.left
;
1399 dfa
->lex
.minrep
= dfa
->lex
.maxrep
= -1;
1400 for (; p
!= lim
&& isasciidigit (*p
); p
++)
1401 dfa
->lex
.minrep
= (dfa
->lex
.minrep
< 0
1403 : MIN (RE_DUP_MAX
+ 1,
1404 dfa
->lex
.minrep
* 10 + *p
- '0'));
1408 dfa
->lex
.maxrep
= dfa
->lex
.minrep
;
1411 if (dfa
->lex
.minrep
< 0)
1412 dfa
->lex
.minrep
= 0;
1413 while (++p
!= lim
&& isasciidigit (*p
))
1415 = (dfa
->lex
.maxrep
< 0
1417 : MIN (RE_DUP_MAX
+ 1,
1418 dfa
->lex
.maxrep
* 10 + *p
- '0'));
1421 if (! ((! backslash
|| (p
!= lim
&& *p
++ == '\\'))
1422 && p
!= lim
&& *p
++ == '}'
1423 && 0 <= dfa
->lex
.minrep
1424 && (dfa
->lex
.maxrep
< 0
1425 || dfa
->lex
.minrep
<= dfa
->lex
.maxrep
)))
1427 if (dfa
->syntax
.syntax_bits
& RE_INVALID_INTERVAL_ORD
)
1429 dfaerror (_("invalid content of \\{\\}"));
1431 if (RE_DUP_MAX
< dfa
->lex
.maxrep
)
1432 dfaerror (_("regular expression too big"));
1434 dfa
->lex
.left
= lim
- p
;
1436 dfa
->lex
.laststart
= false;
1437 return dfa
->lex
.lasttok
= REPMN
;
1440 if (dfa
->syntax
.syntax_bits
& RE_LIMITED_OPS
)
1442 if (backslash
!= ((dfa
->syntax
.syntax_bits
& RE_NO_BK_VBAR
) == 0))
1444 dfa
->lex
.laststart
= true;
1445 return dfa
->lex
.lasttok
= OR
;
1448 if (dfa
->syntax
.syntax_bits
& RE_LIMITED_OPS
1449 || backslash
|| !(dfa
->syntax
.syntax_bits
& RE_NEWLINE_ALT
))
1451 dfa
->lex
.laststart
= true;
1452 return dfa
->lex
.lasttok
= OR
;
1455 if (backslash
!= ((dfa
->syntax
.syntax_bits
& RE_NO_BK_PARENS
) == 0))
1458 dfa
->lex
.laststart
= true;
1459 return dfa
->lex
.lasttok
= LPAREN
;
1462 if (backslash
!= ((dfa
->syntax
.syntax_bits
& RE_NO_BK_PARENS
) == 0))
1464 if (dfa
->lex
.parens
== 0
1465 && dfa
->syntax
.syntax_bits
& RE_UNMATCHED_RIGHT_PAREN_ORD
)
1468 dfa
->lex
.laststart
= false;
1469 return dfa
->lex
.lasttok
= RPAREN
;
1474 if (dfa
->canychar
< 0)
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
)
1486 dfa
->canychar
= charclass_index (dfa
, &ccl
);
1488 dfa
->lex
.laststart
= false;
1489 return dfa
->lex
.lasttok
= (dfa
->localeinfo
.multibyte
1491 : CSET
+ dfa
->canychar
);
1495 if (!backslash
|| (dfa
->syntax
.syntax_bits
& RE_NO_GNU_OPS
))
1497 if (!dfa
->localeinfo
.multibyte
)
1501 for (int c2
= 0; c2
< NOTCHAR
; ++c2
)
1506 dfa
->lex
.laststart
= false;
1507 return dfa
->lex
.lasttok
= CSET
+ charclass_index (dfa
, &ccl
);
1510 /* FIXME: see if optimizing this, as is done with ANYCHAR and
1511 add_utf8_anychar, makes sense. */
1513 /* \s and \S are documented to be equivalent to [[:space:]] and
1514 [^[:space:]] respectively, so tell the lexer to process those
1515 strings, each minus its "already processed" '['. */
1518 push_lex_state (dfa
, &ls
, &"^[:space:]]"[c
== 's']);
1519 dfa
->lex
.lasttok
= parse_bracket_exp (dfa
);
1520 pop_lex_state (dfa
, &ls
);
1523 dfa
->lex
.laststart
= false;
1524 return dfa
->lex
.lasttok
;
1528 if (!backslash
|| (dfa
->syntax
.syntax_bits
& RE_NO_GNU_OPS
))
1531 if (!dfa
->localeinfo
.multibyte
)
1535 for (int c2
= 0; c2
< NOTCHAR
; ++c2
)
1536 if (dfa
->syntax
.sbit
[c2
] == CTX_LETTER
)
1540 dfa
->lex
.laststart
= false;
1541 return dfa
->lex
.lasttok
= CSET
+ charclass_index (dfa
, &ccl
);
1544 /* FIXME: see if optimizing this, as is done with ANYCHAR and
1545 add_utf8_anychar, makes sense. */
1547 /* \w and \W are documented to be equivalent to [_[:alnum:]] and
1548 [^_[:alnum:]] respectively, so tell the lexer to process those
1549 strings, each minus its "already processed" '['. */
1552 push_lex_state (dfa
, &ls
, &"^_[:alnum:]]"[c
== 'w']);
1553 dfa
->lex
.lasttok
= parse_bracket_exp (dfa
);
1554 pop_lex_state (dfa
, &ls
);
1557 dfa
->lex
.laststart
= false;
1558 return dfa
->lex
.lasttok
;
1563 dfa
->lex
.laststart
= false;
1564 return dfa
->lex
.lasttok
= parse_bracket_exp (dfa
);
1568 dfa
->lex
.laststart
= false;
1569 /* For multibyte character sets, folding is done in atom. Always
1571 if (dfa
->localeinfo
.multibyte
)
1572 return dfa
->lex
.lasttok
= WCHAR
;
1574 if (dfa
->syntax
.case_fold
&& isalpha (c
))
1578 setbit_case_fold_c (c
, &ccl
);
1579 return dfa
->lex
.lasttok
= CSET
+ charclass_index (dfa
, &ccl
);
1582 return dfa
->lex
.lasttok
= c
;
1586 /* The above loop should consume at most a backslash
1587 and some other character. */
1589 return END
; /* keeps pedantic compilers happy. */
1593 addtok_mb (struct dfa
*dfa
, token t
, char mbprop
)
1595 if (dfa
->talloc
== dfa
->tindex
)
1597 dfa
->tokens
= xpalloc (dfa
->tokens
, &dfa
->talloc
, 1, -1,
1598 sizeof *dfa
->tokens
);
1599 if (dfa
->localeinfo
.multibyte
)
1600 dfa
->multibyte_prop
= xnrealloc (dfa
->multibyte_prop
, dfa
->talloc
,
1601 sizeof *dfa
->multibyte_prop
);
1603 if (dfa
->localeinfo
.multibyte
)
1604 dfa
->multibyte_prop
[dfa
->tindex
] = mbprop
;
1605 dfa
->tokens
[dfa
->tindex
++] = t
;
1620 dfa
->epsilon
= true;
1621 goto increment_depth
;
1625 goto increment_nleaves
;
1633 dfa
->epsilon
= true;
1640 if (dfa
->depth
< dfa
->parse
.depth
)
1641 dfa
->depth
= dfa
->parse
.depth
;
1646 static void addtok_wc (struct dfa
*dfa
, wint_t wc
);
1648 /* Add the given token to the parse tree, maintaining the depth count and
1649 updating the maximum depth if necessary. */
1651 addtok (struct dfa
*dfa
, token t
)
1653 if (dfa
->localeinfo
.multibyte
&& t
== MBCSET
)
1655 bool need_or
= false;
1657 /* Extract wide characters into alternations for better performance.
1658 This does not require UTF-8. */
1659 for (idx_t i
= 0; i
< dfa
->lex
.brack
.nchars
; i
++)
1661 addtok_wc (dfa
, dfa
->lex
.brack
.chars
[i
]);
1666 dfa
->lex
.brack
.nchars
= 0;
1668 /* Wide characters have been handled above, so it is possible
1669 that the set is empty now. Do nothing in that case. */
1670 if (dfa
->lex
.brack
.cset
!= -1)
1672 addtok (dfa
, CSET
+ dfa
->lex
.brack
.cset
);
1679 addtok_mb (dfa
, t
, 3);
1683 /* We treat a multibyte character as a single atom, so that DFA
1684 can treat a multibyte character as a single expression.
1686 e.g., we construct the following tree from "<mb1><mb2>".
1687 <mb1(1st-byte)><mb1(2nd-byte)><CAT><mb1(3rd-byte)><CAT>
1688 <mb2(1st-byte)><mb2(2nd-byte)><CAT><mb2(3rd-byte)><CAT><CAT> */
1690 addtok_wc (struct dfa
*dfa
, wint_t wc
)
1692 unsigned char buf
[MB_LEN_MAX
];
1693 mbstate_t s
= { 0 };
1694 size_t stored_bytes
= wcrtomb ((char *) buf
, wc
, &s
);
1697 if (stored_bytes
!= (size_t) -1)
1698 buflen
= stored_bytes
;
1701 /* This is merely stop-gap. buf[0] is undefined, yet skipping
1702 the addtok_mb call altogether can corrupt the heap. */
1707 addtok_mb (dfa
, buf
[0], buflen
== 1 ? 3 : 1);
1708 for (int i
= 1; i
< buflen
; i
++)
1710 addtok_mb (dfa
, buf
[i
], i
== buflen
- 1 ? 2 : 0);
1716 add_utf8_anychar (struct dfa
*dfa
)
1718 /* Since the Unicode Standard Version 4.0.0 (2003), a well-formed
1719 UTF-8 byte sequence has been defined as follows:
1722 |[\xc2-\xdf][\x80-\xbf]
1723 |[\xe0][\xa0-\xbf][\x80-\xbf]
1724 |[\xe1-\xec\xee-\xef][\x80-\xbf][\x80-\xbf]
1725 |[\xed][\x80-\x9f][\x80-\xbf]
1726 |[\xf0][\x90-\xbf][\x80-\xbf][\x80-\xbf])
1727 |[\xf1-\xf3][\x80-\xbf][\x80-\xbf][\x80-\xbf]
1728 |[\xf4][\x80-\x8f][\x80-\xbf][\x80-\xbf])
1730 which I'll write more concisely "A|BC|DEC|FCC|GHC|IJCC|KCCC|LMCC",
1731 where A = [\x00-\x7f], B = [\xc2-\xdf], C = [\x80-\xbf],
1732 D = [\xe0], E = [\xa0-\xbf], F = [\xe1-\xec\xee-\xef], G = [\xed],
1733 H = [\x80-\x9f], I = [\xf0],
1734 J = [\x90-\xbf], K = [\xf1-\xf3], L = [\xf4], M = [\x80-\x8f].
1736 This can be refactored to "A|(B|DE|GH|(F|IJ|LM|KC)C)C". */
1738 /* Mnemonics for classes containing two or more bytes. */
1739 enum { A
, B
, C
, E
, F
, H
, J
, K
, M
};
1741 /* Mnemonics for single-byte tokens. */
1742 enum { D_token
= 0xe0, G_token
= 0xed, I_token
= 0xf0, L_token
= 0xf4 };
1744 static charclass
const utf8_classes
[] = {
1745 /* A. 00-7f: 1-byte sequence. */
1746 CHARCLASS_INIT (0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff, 0, 0, 0, 0),
1748 /* B. c2-df: 1st byte of a 2-byte sequence. */
1749 CHARCLASS_INIT (0, 0, 0, 0, 0, 0, 0xfffffffc, 0),
1751 /* C. 80-bf: non-leading bytes. */
1752 CHARCLASS_INIT (0, 0, 0, 0, 0xffffffff, 0xffffffff, 0, 0),
1754 /* D. e0 (just a token). */
1756 /* E. a0-bf: 2nd byte of a "DEC" sequence. */
1757 CHARCLASS_INIT (0, 0, 0, 0, 0, 0xffffffff, 0, 0),
1759 /* F. e1-ec + ee-ef: 1st byte of an "FCC" sequence. */
1760 CHARCLASS_INIT (0, 0, 0, 0, 0, 0, 0, 0xdffe),
1762 /* G. ed (just a token). */
1764 /* H. 80-9f: 2nd byte of a "GHC" sequence. */
1765 CHARCLASS_INIT (0, 0, 0, 0, 0xffff, 0, 0, 0),
1767 /* I. f0 (just a token). */
1769 /* J. 90-bf: 2nd byte of an "IJCC" sequence. */
1770 CHARCLASS_INIT (0, 0, 0, 0, 0xffff0000, 0xffffffff, 0, 0),
1772 /* K. f1-f3: 1st byte of a "KCCC" sequence. */
1773 CHARCLASS_INIT (0, 0, 0, 0, 0, 0, 0, 0xe0000),
1775 /* L. f4 (just a token). */
1777 /* M. 80-8f: 2nd byte of a "LMCC" sequence. */
1778 CHARCLASS_INIT (0, 0, 0, 0, 0xff, 0, 0, 0),
1781 /* Define the character classes that are needed below. */
1782 if (dfa
->utf8_anychar_classes
[0] == 0)
1784 charclass c
= utf8_classes
[0];
1785 if (! (dfa
->syntax
.syntax_bits
& RE_DOT_NEWLINE
))
1787 if (dfa
->syntax
.syntax_bits
& RE_DOT_NOT_NULL
)
1789 dfa
->utf8_anychar_classes
[0] = CSET
+ charclass_index (dfa
, &c
);
1791 for (int i
= 1; i
< sizeof utf8_classes
/ sizeof *utf8_classes
; i
++)
1792 dfa
->utf8_anychar_classes
[i
]
1793 = CSET
+ charclass_index (dfa
, &utf8_classes
[i
]);
1796 /* Implement the "A|(B|DE|GH|(F|IJ|LM|KC)C)C" pattern mentioned above.
1797 The token buffer is in reverse Polish order, so we get
1798 "A B D E CAT OR G H CAT OR F I J CAT OR L M CAT OR K
1799 C CAT OR C CAT OR C CAT OR". */
1800 addtok (dfa
, dfa
->utf8_anychar_classes
[A
]);
1801 addtok (dfa
, dfa
->utf8_anychar_classes
[B
]);
1802 addtok (dfa
, D_token
);
1803 addtok (dfa
, dfa
->utf8_anychar_classes
[E
]);
1806 addtok (dfa
, G_token
);
1807 addtok (dfa
, dfa
->utf8_anychar_classes
[H
]);
1810 addtok (dfa
, dfa
->utf8_anychar_classes
[F
]);
1811 addtok (dfa
, I_token
);
1812 addtok (dfa
, dfa
->utf8_anychar_classes
[J
]);
1815 addtok (dfa
, L_token
);
1816 addtok (dfa
, dfa
->utf8_anychar_classes
[M
]);
1819 addtok (dfa
, dfa
->utf8_anychar_classes
[K
]);
1820 for (int i
= 0; i
< 3; i
++)
1822 addtok (dfa
, dfa
->utf8_anychar_classes
[C
]);
1828 /* The grammar understood by the parser is as follows.
1847 <multibyte character>
1858 LPAREN regexp RPAREN
1861 The parser builds a parse tree in postfix form in an array of tokens. */
1864 atom (struct dfa
*dfa
)
1866 if ((0 <= dfa
->parse
.tok
&& dfa
->parse
.tok
< NOTCHAR
)
1867 || dfa
->parse
.tok
>= CSET
1868 || dfa
->parse
.tok
== BEG
|| dfa
->parse
.tok
== BACKREF
1869 || dfa
->parse
.tok
== BEGLINE
|| dfa
->parse
.tok
== ENDLINE
1870 || dfa
->parse
.tok
== BEGWORD
|| dfa
->parse
.tok
== ENDWORD
1871 || dfa
->parse
.tok
== LIMWORD
|| dfa
->parse
.tok
== NOTLIMWORD
1872 || dfa
->parse
.tok
== ANYCHAR
|| dfa
->parse
.tok
== MBCSET
)
1874 if (dfa
->parse
.tok
== ANYCHAR
&& dfa
->localeinfo
.using_utf8
)
1876 /* For UTF-8 expand the period to a series of CSETs that define a
1877 valid UTF-8 character. This avoids using the slow multibyte
1878 path. I'm pretty sure it would be both profitable and correct to
1879 do it for any encoding; however, the optimization must be done
1880 manually as it is done above in add_utf8_anychar. So, let's
1881 start with UTF-8: it is the most used, and the structure of the
1882 encoding makes the correctness more obvious. */
1883 add_utf8_anychar (dfa
);
1886 addtok (dfa
, dfa
->parse
.tok
);
1887 dfa
->parse
.tok
= lex (dfa
);
1889 else if (dfa
->parse
.tok
== WCHAR
)
1891 if (dfa
->lex
.wctok
== WEOF
)
1892 addtok (dfa
, BACKREF
);
1895 addtok_wc (dfa
, dfa
->lex
.wctok
);
1897 if (dfa
->syntax
.case_fold
)
1899 wchar_t folded
[CASE_FOLDED_BUFSIZE
];
1900 int n
= case_folded_counterparts (dfa
->lex
.wctok
, folded
);
1901 for (int i
= 0; i
< n
; i
++)
1903 addtok_wc (dfa
, folded
[i
]);
1909 dfa
->parse
.tok
= lex (dfa
);
1911 else if (dfa
->parse
.tok
== LPAREN
)
1913 dfa
->parse
.tok
= lex (dfa
);
1915 if (dfa
->parse
.tok
!= RPAREN
)
1916 dfaerror (_("unbalanced ("));
1917 dfa
->parse
.tok
= lex (dfa
);
1920 addtok (dfa
, EMPTY
);
1923 /* Return the number of tokens in the given subexpression. */
1924 static idx_t _GL_ATTRIBUTE_PURE
1925 nsubtoks (struct dfa
const *dfa
, idx_t tindex
)
1927 switch (dfa
->tokens
[tindex
- 1])
1934 return 1 + nsubtoks (dfa
, tindex
- 1);
1938 idx_t ntoks1
= nsubtoks (dfa
, tindex
- 1);
1939 return 1 + ntoks1
+ nsubtoks (dfa
, tindex
- 1 - ntoks1
);
1944 /* Copy the given subexpression to the top of the tree. */
1946 copytoks (struct dfa
*dfa
, idx_t tindex
, idx_t ntokens
)
1948 if (dfa
->localeinfo
.multibyte
)
1949 for (idx_t i
= 0; i
< ntokens
; i
++)
1950 addtok_mb (dfa
, dfa
->tokens
[tindex
+ i
],
1951 dfa
->multibyte_prop
[tindex
+ i
]);
1953 for (idx_t i
= 0; i
< ntokens
; i
++)
1954 addtok_mb (dfa
, dfa
->tokens
[tindex
+ i
], 3);
1958 closure (struct dfa
*dfa
)
1961 while (dfa
->parse
.tok
== QMARK
|| dfa
->parse
.tok
== STAR
1962 || dfa
->parse
.tok
== PLUS
|| dfa
->parse
.tok
== REPMN
)
1963 if (dfa
->parse
.tok
== REPMN
&& (dfa
->lex
.minrep
|| dfa
->lex
.maxrep
))
1965 idx_t ntokens
= nsubtoks (dfa
, dfa
->tindex
);
1966 idx_t tindex
= dfa
->tindex
- ntokens
;
1967 if (dfa
->lex
.maxrep
< 0)
1969 if (dfa
->lex
.minrep
== 0)
1970 addtok (dfa
, QMARK
);
1972 for (i
= 1; i
< dfa
->lex
.minrep
; i
++)
1974 copytoks (dfa
, tindex
, ntokens
);
1977 for (; i
< dfa
->lex
.maxrep
; i
++)
1979 copytoks (dfa
, tindex
, ntokens
);
1980 addtok (dfa
, QMARK
);
1983 dfa
->parse
.tok
= lex (dfa
);
1985 else if (dfa
->parse
.tok
== REPMN
)
1987 dfa
->tindex
-= nsubtoks (dfa
, dfa
->tindex
);
1988 dfa
->parse
.tok
= lex (dfa
);
1993 addtok (dfa
, dfa
->parse
.tok
);
1994 dfa
->parse
.tok
= lex (dfa
);
1999 branch (struct dfa
* dfa
)
2002 while (dfa
->parse
.tok
!= RPAREN
&& dfa
->parse
.tok
!= OR
2003 && dfa
->parse
.tok
>= 0)
2011 regexp (struct dfa
*dfa
)
2014 while (dfa
->parse
.tok
== OR
)
2016 dfa
->parse
.tok
= lex (dfa
);
2022 /* Parse a string S of length LEN into D. S can include NUL characters.
2023 This is the main entry point for the parser. */
2025 dfaparse (char const *s
, idx_t len
, struct dfa
*d
)
2029 d
->lex
.lasttok
= END
;
2030 d
->lex
.laststart
= true;
2032 if (!d
->syntax
.syntax_bits_set
)
2033 dfaerror (_("no syntax specified"));
2038 d
->parse
.tok
= lex (d
);
2039 d
->parse
.depth
= d
->depth
;
2043 if (d
->parse
.tok
!= END
)
2044 dfaerror (_("unbalanced )"));
2046 addtok (d
, END
- d
->nregexps
);
2055 /* Some primitives for operating on sets of positions. */
2057 /* Copy one set to another. */
2059 copy (position_set
const *src
, position_set
*dst
)
2061 if (dst
->alloc
< src
->nelem
)
2064 dst
->elems
= xpalloc (NULL
, &dst
->alloc
, src
->nelem
- dst
->alloc
, -1,
2065 sizeof *dst
->elems
);
2067 dst
->nelem
= src
->nelem
;
2068 if (src
->nelem
!= 0)
2069 memcpy (dst
->elems
, src
->elems
, src
->nelem
* sizeof *dst
->elems
);
2073 alloc_position_set (position_set
*s
, idx_t size
)
2075 s
->elems
= xnmalloc (size
, sizeof *s
->elems
);
2080 /* Insert position P in set S. S is maintained in sorted order on
2081 decreasing index. If there is already an entry in S with P.index
2082 then merge (logically-OR) P's constraints into the one in S.
2083 S->elems must point to an array large enough to hold the resulting set. */
2085 insert (position p
, position_set
*s
)
2087 idx_t count
= s
->nelem
;
2088 idx_t lo
= 0, hi
= count
;
2091 idx_t mid
= (lo
+ hi
) >> 1;
2092 if (s
->elems
[mid
].index
< p
.index
)
2094 else if (s
->elems
[mid
].index
== p
.index
)
2096 s
->elems
[mid
].constraint
|= p
.constraint
;
2103 s
->elems
= maybe_realloc (s
->elems
, count
, &s
->alloc
, -1, sizeof *s
->elems
);
2104 for (idx_t i
= count
; i
> lo
; i
--)
2105 s
->elems
[i
] = s
->elems
[i
- 1];
2111 append (position p
, position_set
*s
)
2113 idx_t count
= s
->nelem
;
2114 s
->elems
= maybe_realloc (s
->elems
, count
, &s
->alloc
, -1, sizeof *s
->elems
);
2115 s
->elems
[s
->nelem
++] = p
;
2118 /* Merge S1 and S2 (with the additional constraint C2) into M. The
2119 result is as if the positions of S1, and of S2 with the additional
2120 constraint C2, were inserted into an initially empty set. */
2122 merge_constrained (position_set
const *s1
, position_set
const *s2
,
2123 unsigned int c2
, position_set
*m
)
2127 if (m
->alloc
- s1
->nelem
< s2
->nelem
)
2130 m
->alloc
= s1
->nelem
;
2131 m
->elems
= xpalloc (NULL
, &m
->alloc
, s2
->nelem
, -1, sizeof *m
->elems
);
2134 while (i
< s1
->nelem
|| j
< s2
->nelem
)
2135 if (! (j
< s2
->nelem
)
2136 || (i
< s1
->nelem
&& s1
->elems
[i
].index
<= s2
->elems
[j
].index
))
2138 unsigned int c
= ((i
< s1
->nelem
&& j
< s2
->nelem
2139 && s1
->elems
[i
].index
== s2
->elems
[j
].index
)
2140 ? s2
->elems
[j
++].constraint
& c2
2142 m
->elems
[m
->nelem
].index
= s1
->elems
[i
].index
;
2143 m
->elems
[m
->nelem
++].constraint
= s1
->elems
[i
++].constraint
| c
;
2147 if (s2
->elems
[j
].constraint
& c2
)
2149 m
->elems
[m
->nelem
].index
= s2
->elems
[j
].index
;
2150 m
->elems
[m
->nelem
++].constraint
= s2
->elems
[j
].constraint
& c2
;
2156 /* Merge two sets of positions into a third. The result is exactly as if
2157 the positions of both sets were inserted into an initially empty set. */
2159 merge (position_set
const *s1
, position_set
const *s2
, position_set
*m
)
2161 merge_constrained (s1
, s2
, -1, m
);
2164 /* Merge into DST all the elements of SRC, possibly destroying
2165 the contents of the temporary M. */
2167 merge2 (position_set
*dst
, position_set
const *src
, position_set
*m
)
2171 for (idx_t i
= 0; i
< src
->nelem
; i
++)
2172 insert (src
->elems
[i
], dst
);
2176 merge (src
, dst
, m
);
2181 /* Delete a position from a set. Return the nonzero constraint of the
2182 deleted position, or zero if there was no such position. */
2184 delete (idx_t del
, position_set
*s
)
2186 idx_t count
= s
->nelem
;
2187 idx_t lo
= 0, hi
= count
;
2190 idx_t mid
= (lo
+ hi
) >> 1;
2191 if (s
->elems
[mid
].index
< del
)
2193 else if (s
->elems
[mid
].index
== del
)
2195 unsigned int c
= s
->elems
[mid
].constraint
;
2197 for (i
= mid
; i
+ 1 < count
; i
++)
2198 s
->elems
[i
] = s
->elems
[i
+ 1];
2208 /* Replace a position with the followed set. */
2210 replace (position_set
*dst
, idx_t del
, position_set
*add
,
2211 unsigned int constraint
, position_set
*tmp
)
2213 unsigned int c
= delete (del
, dst
) & constraint
;
2218 merge_constrained (tmp
, add
, c
, dst
);
2222 /* Find the index of the state corresponding to the given position set with
2223 the given preceding context, or create a new state if there is no such
2224 state. Context tells whether we got here on a newline or letter. */
2226 state_index (struct dfa
*d
, position_set
const *s
, int context
)
2232 for (i
= 0; i
< s
->nelem
; ++i
)
2234 size_t ind
= s
->elems
[i
].index
;
2235 hash
^= ind
+ s
->elems
[i
].constraint
;
2238 /* Try to find a state that exactly matches the proposed one. */
2239 for (i
= 0; i
< d
->sindex
; ++i
)
2241 if (hash
!= d
->states
[i
].hash
|| s
->nelem
!= d
->states
[i
].elems
.nelem
2242 || context
!= d
->states
[i
].context
)
2245 for (j
= 0; j
< s
->nelem
; ++j
)
2246 if (s
->elems
[j
].constraint
!= d
->states
[i
].elems
.elems
[j
].constraint
2247 || s
->elems
[j
].index
!= d
->states
[i
].elems
.elems
[j
].index
)
2254 fprintf (stderr
, "new state %td\n nextpos:", i
);
2255 for (state_num j
= 0; j
< s
->nelem
; j
++)
2257 fprintf (stderr
, " %td:", s
->elems
[j
].index
);
2258 prtok (d
->tokens
[s
->elems
[j
].index
]);
2260 fprintf (stderr
, "\n context:");
2261 if (context
^ CTX_ANY
)
2263 if (context
& CTX_NONE
)
2264 fprintf (stderr
, " CTX_NONE");
2265 if (context
& CTX_LETTER
)
2266 fprintf (stderr
, " CTX_LETTER");
2267 if (context
& CTX_NEWLINE
)
2268 fprintf (stderr
, " CTX_NEWLINE");
2271 fprintf (stderr
, " CTX_ANY");
2272 fprintf (stderr
, "\n");
2275 for (state_num j
= 0; j
< s
->nelem
; j
++)
2277 int c
= d
->constraints
[s
->elems
[j
].index
];
2281 if (succeeds_in_context (c
, context
, CTX_ANY
))
2284 else if (d
->tokens
[s
->elems
[j
].index
] == BACKREF
)
2285 constraint
= NO_CONSTRAINT
;
2289 /* Create a new state. */
2290 d
->states
= maybe_realloc (d
->states
, d
->sindex
, &d
->salloc
, -1,
2292 d
->states
[i
].hash
= hash
;
2293 alloc_position_set (&d
->states
[i
].elems
, s
->nelem
);
2294 copy (s
, &d
->states
[i
].elems
);
2295 d
->states
[i
].context
= context
;
2296 d
->states
[i
].constraint
= constraint
;
2297 d
->states
[i
].mbps
.nelem
= 0;
2298 d
->states
[i
].mbps
.elems
= NULL
;
2299 d
->states
[i
].mb_trindex
= -1;
2306 /* Find the epsilon closure of D's set of positions. If any position of the set
2307 contains a symbol that matches the empty string in some context, replace
2308 that position with the elements of its follow labeled with an appropriate
2309 constraint. Repeat exhaustively until no funny positions are left.
2310 S->elems must be large enough to hold the result. BACKWARD is D's
2311 backward set; use and update it too. */
2313 epsclosure (struct dfa
const *d
, position_set
*backward
)
2316 alloc_position_set (&tmp
, d
->nleaves
);
2317 for (idx_t i
= 0; i
< d
->tindex
; i
++)
2318 if (0 < d
->follows
[i
].nelem
)
2320 unsigned int constraint
;
2321 switch (d
->tokens
[i
])
2327 constraint
= BEGLINE_CONSTRAINT
;
2330 constraint
= ENDLINE_CONSTRAINT
;
2333 constraint
= BEGWORD_CONSTRAINT
;
2336 constraint
= ENDWORD_CONSTRAINT
;
2339 constraint
= LIMWORD_CONSTRAINT
;
2342 constraint
= NOTLIMWORD_CONSTRAINT
;
2345 constraint
= NO_CONSTRAINT
;
2349 delete (i
, &d
->follows
[i
]);
2351 for (idx_t j
= 0; j
< backward
[i
].nelem
; j
++)
2352 replace (&d
->follows
[backward
[i
].elems
[j
].index
], i
, &d
->follows
[i
],
2354 for (idx_t j
= 0; j
< d
->follows
[i
].nelem
; j
++)
2355 replace (&backward
[d
->follows
[i
].elems
[j
].index
], i
, &backward
[i
],
2356 NO_CONSTRAINT
, &tmp
);
2361 /* Returns the set of contexts for which there is at least one
2362 character included in C. */
2365 charclass_context (struct dfa
const *dfa
, charclass
const *c
)
2369 for (int j
= 0; j
< CHARCLASS_WORDS
; j
++)
2371 if (c
->w
[j
] & dfa
->syntax
.newline
.w
[j
])
2372 context
|= CTX_NEWLINE
;
2373 if (c
->w
[j
] & dfa
->syntax
.letters
.w
[j
])
2374 context
|= CTX_LETTER
;
2375 if (c
->w
[j
] & ~(dfa
->syntax
.letters
.w
[j
] | dfa
->syntax
.newline
.w
[j
]))
2376 context
|= CTX_NONE
;
2382 /* Returns the contexts on which the position set S depends. Each context
2383 in the set of returned contexts (let's call it SC) may have a different
2384 follow set than other contexts in SC, and also different from the
2385 follow set of the complement set (sc ^ CTX_ANY). However, all contexts
2386 in the complement set will have the same follow set. */
2388 static int _GL_ATTRIBUTE_PURE
2389 state_separate_contexts (struct dfa
*d
, position_set
const *s
)
2391 int separate_contexts
= 0;
2393 for (idx_t j
= 0; j
< s
->nelem
; j
++)
2394 separate_contexts
|= d
->separates
[s
->elems
[j
].index
];
2396 return separate_contexts
;
2401 /* Single token is repeated. It is distinguished from non-repeated. */
2402 OPT_REPEAT
= (1 << 0),
2404 /* Multiple tokens are repeated. This flag is on at head of tokens. The
2405 node is not merged. */
2406 OPT_LPAREN
= (1 << 1),
2408 /* Multiple branches are joined. The node is not merged. */
2409 OPT_RPAREN
= (1 << 2),
2411 /* The node is walked. If the node is found in walking again, OPT_RPAREN
2412 flag is turned on. */
2413 OPT_WALKED
= (1 << 3),
2415 /* The node is queued. The node is not queued again. */
2416 OPT_QUEUED
= (1 << 4)
2420 merge_nfa_state (struct dfa
*d
, idx_t tindex
, char *flags
,
2421 position_set
*merged
)
2423 position_set
*follows
= d
->follows
;
2426 for (idx_t i
= 0; i
< follows
[tindex
].nelem
; i
++)
2428 idx_t sindex
= follows
[tindex
].elems
[i
].index
;
2430 /* Skip the node as pruned in future. */
2431 unsigned int iconstraint
= follows
[tindex
].elems
[i
].constraint
;
2432 if (iconstraint
== 0)
2435 if (d
->tokens
[follows
[tindex
].elems
[i
].index
] <= END
)
2437 d
->constraints
[tindex
] |= follows
[tindex
].elems
[i
].constraint
;
2441 if (!(flags
[sindex
] & (OPT_LPAREN
| OPT_RPAREN
)))
2445 for (j
= 0; j
< nelem
; j
++)
2447 idx_t dindex
= follows
[tindex
].elems
[j
].index
;
2449 if (follows
[tindex
].elems
[j
].constraint
!= iconstraint
)
2452 if (flags
[dindex
] & (OPT_LPAREN
| OPT_RPAREN
))
2455 if (d
->tokens
[sindex
] != d
->tokens
[dindex
])
2458 if ((flags
[sindex
] ^ flags
[dindex
]) & OPT_REPEAT
)
2461 if (flags
[sindex
] & OPT_REPEAT
)
2462 delete (sindex
, &follows
[sindex
]);
2464 merge2 (&follows
[dindex
], &follows
[sindex
], merged
);
2473 follows
[tindex
].elems
[nelem
++] = follows
[tindex
].elems
[i
];
2474 flags
[sindex
] |= OPT_QUEUED
;
2477 follows
[tindex
].nelem
= nelem
;
2481 compare (const void *a
, const void *b
)
2483 position
const *p
= a
, *q
= b
;
2484 return (p
->index
> q
->index
) - (p
->index
< q
->index
);
2488 reorder_tokens (struct dfa
*d
)
2491 ptrdiff_t *map
= xnmalloc (d
->tindex
, sizeof *map
);
2493 for (idx_t i
= 1; i
< d
->tindex
; i
++)
2496 token
*tokens
= xnmalloc (d
->nleaves
, sizeof *tokens
);
2497 position_set
*follows
= xnmalloc (d
->nleaves
, sizeof *follows
);
2498 int *constraints
= xnmalloc (d
->nleaves
, sizeof *constraints
);
2499 char *multibyte_prop
= (d
->localeinfo
.multibyte
2500 ? xnmalloc (d
->nleaves
, sizeof *multibyte_prop
)
2503 for (idx_t i
= 0; i
< d
->tindex
; i
++)
2507 free (d
->follows
[i
].elems
);
2508 d
->follows
[i
].elems
= NULL
;
2509 d
->follows
[i
].nelem
= 0;
2513 tokens
[map
[i
]] = d
->tokens
[i
];
2514 follows
[map
[i
]] = d
->follows
[i
];
2515 constraints
[map
[i
]] = d
->constraints
[i
];
2517 if (multibyte_prop
!= NULL
)
2518 multibyte_prop
[map
[i
]] = d
->multibyte_prop
[i
];
2520 for (idx_t j
= 0; j
< d
->follows
[i
].nelem
; j
++)
2522 if (map
[d
->follows
[i
].elems
[j
].index
] == -1)
2523 map
[d
->follows
[i
].elems
[j
].index
] = nleaves
++;
2525 d
->follows
[i
].elems
[j
].index
= map
[d
->follows
[i
].elems
[j
].index
];
2528 qsort (d
->follows
[i
].elems
, d
->follows
[i
].nelem
,
2529 sizeof *d
->follows
[i
].elems
, compare
);
2532 for (idx_t i
= 0; i
< nleaves
; i
++)
2534 d
->tokens
[i
] = tokens
[i
];
2535 d
->follows
[i
] = follows
[i
];
2536 d
->constraints
[i
] = constraints
[i
];
2538 if (multibyte_prop
!= NULL
)
2539 d
->multibyte_prop
[i
] = multibyte_prop
[i
];
2542 d
->tindex
= d
->nleaves
= nleaves
;
2547 free (multibyte_prop
);
2552 dfaoptimize (struct dfa
*d
)
2554 char *flags
= xzalloc (d
->tindex
);
2556 for (idx_t i
= 0; i
< d
->tindex
; i
++)
2558 for (idx_t j
= 0; j
< d
->follows
[i
].nelem
; j
++)
2560 if (d
->follows
[i
].elems
[j
].index
== i
)
2561 flags
[d
->follows
[i
].elems
[j
].index
] |= OPT_REPEAT
;
2562 else if (d
->follows
[i
].elems
[j
].index
< i
)
2563 flags
[d
->follows
[i
].elems
[j
].index
] |= OPT_LPAREN
;
2564 else if (flags
[d
->follows
[i
].elems
[j
].index
] &= OPT_WALKED
)
2565 flags
[d
->follows
[i
].elems
[j
].index
] |= OPT_RPAREN
;
2567 flags
[d
->follows
[i
].elems
[j
].index
] |= OPT_WALKED
;
2571 flags
[0] |= OPT_QUEUED
;
2573 position_set merged0
;
2574 position_set
*merged
= &merged0
;
2575 alloc_position_set (merged
, d
->nleaves
);
2577 d
->constraints
= xcalloc (d
->tindex
, sizeof *d
->constraints
);
2579 for (idx_t i
= 0; i
< d
->tindex
; i
++)
2580 if (flags
[i
] & OPT_QUEUED
)
2581 merge_nfa_state (d
, i
, flags
, merged
);
2585 free (merged
->elems
);
2589 /* Perform bottom-up analysis on the parse tree, computing various functions.
2590 Note that at this point, we're pretending constructs like \< are real
2591 characters rather than constraints on what can follow them.
2593 Nullable: A node is nullable if it is at the root of a regexp that can
2594 match the empty string.
2595 * EMPTY leaves are nullable.
2596 * No other leaf is nullable.
2597 * A QMARK or STAR node is nullable.
2598 * A PLUS node is nullable if its argument is nullable.
2599 * A CAT node is nullable if both its arguments are nullable.
2600 * An OR node is nullable if either argument is nullable.
2602 Firstpos: The firstpos of a node is the set of positions (nonempty leaves)
2603 that could correspond to the first character of a string matching the
2604 regexp rooted at the given node.
2605 * EMPTY leaves have empty firstpos.
2606 * The firstpos of a nonempty leaf is that leaf itself.
2607 * The firstpos of a QMARK, STAR, or PLUS node is the firstpos of its
2609 * The firstpos of a CAT node is the firstpos of the left argument, union
2610 the firstpos of the right if the left argument is nullable.
2611 * The firstpos of an OR node is the union of firstpos of each argument.
2613 Lastpos: The lastpos of a node is the set of positions that could
2614 correspond to the last character of a string matching the regexp at
2616 * EMPTY leaves have empty lastpos.
2617 * The lastpos of a nonempty leaf is that leaf itself.
2618 * The lastpos of a QMARK, STAR, or PLUS node is the lastpos of its
2620 * The lastpos of a CAT node is the lastpos of its right argument, union
2621 the lastpos of the left if the right argument is nullable.
2622 * The lastpos of an OR node is the union of the lastpos of each argument.
2624 Follow: The follow of a position is the set of positions that could
2625 correspond to the character following a character matching the node in
2626 a string matching the regexp. At this point we consider special symbols
2627 that match the empty string in some context to be just normal characters.
2628 Later, if we find that a special symbol is in a follow set, we will
2629 replace it with the elements of its follow, labeled with an appropriate
2631 * Every node in the firstpos of the argument of a STAR or PLUS node is in
2632 the follow of every node in the lastpos.
2633 * Every node in the firstpos of the second argument of a CAT node is in
2634 the follow of every node in the lastpos of the first argument.
2636 Because of the postfix representation of the parse tree, the depth-first
2637 analysis is conveniently done by a linear scan with the aid of a stack.
2638 Sets are stored as arrays of the elements, obeying a stack-like allocation
2639 scheme; the number of elements in each set deeper in the stack can be
2640 used to determine the address of a particular set's array. */
2642 dfaanalyze (struct dfa
*d
, bool searchflag
)
2644 /* Array allocated to hold position sets. */
2645 position
*posalloc
= xnmalloc (d
->nleaves
, 2 * sizeof *posalloc
);
2646 /* Firstpos and lastpos elements. */
2647 position
*firstpos
= posalloc
;
2648 position
*lastpos
= firstpos
+ d
->nleaves
;
2652 /* Stack for element counts and nullable flags. */
2655 /* Whether the entry is nullable. */
2658 /* Counts of firstpos and lastpos sets. */
2661 } *stkalloc
= xnmalloc (d
->depth
, sizeof *stkalloc
), *stk
= stkalloc
;
2663 position_set merged
; /* Result of merging sets. */
2666 idx_t tindex
= d
->tindex
;
2669 fprintf (stderr
, "dfaanalyze:\n");
2670 for (idx_t i
= 0; i
< tindex
; i
++)
2672 fprintf (stderr
, " %td:", i
);
2673 prtok (d
->tokens
[i
]);
2675 putc ('\n', stderr
);
2678 d
->searchflag
= searchflag
;
2679 alloc_position_set (&merged
, d
->nleaves
);
2680 d
->follows
= xcalloc (tindex
, sizeof *d
->follows
);
2681 position_set
*backward
2682 = d
->epsilon
? xcalloc (tindex
, sizeof *backward
) : NULL
;
2684 for (idx_t i
= 0; i
< tindex
; i
++)
2686 switch (d
->tokens
[i
])
2689 /* The empty set is nullable. */
2690 stk
->nullable
= true;
2692 /* The firstpos and lastpos of the empty leaf are both empty. */
2693 stk
->nfirstpos
= stk
->nlastpos
= 0;
2699 /* Every element in the lastpos of the argument is in the backward
2700 set of every element in the firstpos. */
2703 tmp
.elems
= lastpos
- stk
[-1].nlastpos
;
2704 tmp
.nelem
= stk
[-1].nlastpos
;
2705 for (position
*p
= firstpos
- stk
[-1].nfirstpos
;
2707 merge2 (&backward
[p
->index
], &tmp
, &merged
);
2710 /* Every element in the firstpos of the argument is in the follow
2711 of every element in the lastpos. */
2713 tmp
.elems
= firstpos
- stk
[-1].nfirstpos
;
2714 tmp
.nelem
= stk
[-1].nfirstpos
;
2715 for (position
*p
= lastpos
- stk
[-1].nlastpos
; p
< lastpos
; p
++)
2716 merge2 (&d
->follows
[p
->index
], &tmp
, &merged
);
2720 /* A QMARK or STAR node is automatically nullable. */
2721 if (d
->tokens
[i
] != PLUS
)
2722 stk
[-1].nullable
= true;
2726 /* Every element in the lastpos of the first argument is in
2727 the backward set of every element in the firstpos of the
2731 tmp
.nelem
= stk
[-2].nlastpos
;
2732 tmp
.elems
= lastpos
- stk
[-1].nlastpos
- stk
[-2].nlastpos
;
2733 for (position
*p
= firstpos
- stk
[-1].nfirstpos
;
2735 merge2 (&backward
[p
->index
], &tmp
, &merged
);
2738 /* Every element in the firstpos of the second argument is in the
2739 follow of every element in the lastpos of the first argument. */
2741 tmp
.nelem
= stk
[-1].nfirstpos
;
2742 tmp
.elems
= firstpos
- stk
[-1].nfirstpos
;
2743 for (position
*plim
= lastpos
- stk
[-1].nlastpos
,
2744 *p
= plim
- stk
[-2].nlastpos
;
2746 merge2 (&d
->follows
[p
->index
], &tmp
, &merged
);
2749 /* The firstpos of a CAT node is the firstpos of the first argument,
2750 union that of the second argument if the first is nullable. */
2751 if (stk
[-2].nullable
)
2752 stk
[-2].nfirstpos
+= stk
[-1].nfirstpos
;
2754 firstpos
-= stk
[-1].nfirstpos
;
2756 /* The lastpos of a CAT node is the lastpos of the second argument,
2757 union that of the first argument if the second is nullable. */
2758 if (stk
[-1].nullable
)
2759 stk
[-2].nlastpos
+= stk
[-1].nlastpos
;
2762 position
*p
= lastpos
- stk
[-1].nlastpos
- stk
[-2].nlastpos
;
2763 for (idx_t j
= 0; j
< stk
[-1].nlastpos
; j
++)
2764 p
[j
] = p
[j
+ stk
[-2].nlastpos
];
2765 lastpos
-= stk
[-2].nlastpos
;
2766 stk
[-2].nlastpos
= stk
[-1].nlastpos
;
2769 /* A CAT node is nullable if both arguments are nullable. */
2770 stk
[-2].nullable
&= stk
[-1].nullable
;
2775 /* The firstpos is the union of the firstpos of each argument. */
2776 stk
[-2].nfirstpos
+= stk
[-1].nfirstpos
;
2778 /* The lastpos is the union of the lastpos of each argument. */
2779 stk
[-2].nlastpos
+= stk
[-1].nlastpos
;
2781 /* An OR node is nullable if either argument is nullable. */
2782 stk
[-2].nullable
|= stk
[-1].nullable
;
2787 /* Anything else is a nonempty position. (Note that special
2788 constructs like \< are treated as nonempty strings here;
2789 an "epsilon closure" effectively makes them nullable later.
2790 Backreferences have to get a real position so we can detect
2791 transitions on them later. But they are nullable. */
2792 stk
->nullable
= d
->tokens
[i
] == BACKREF
;
2794 /* This position is in its own firstpos and lastpos. */
2795 stk
->nfirstpos
= stk
->nlastpos
= 1;
2798 firstpos
->index
= lastpos
->index
= i
;
2799 firstpos
->constraint
= lastpos
->constraint
= NO_CONSTRAINT
;
2800 firstpos
++, lastpos
++;
2805 /* ... balance the above nonsyntactic #ifdef goo... */
2806 fprintf (stderr
, "node %td:", i
);
2807 prtok (d
->tokens
[i
]);
2808 putc ('\n', stderr
);
2810 stk
[-1].nullable
? " nullable: yes\n" : " nullable: no\n");
2811 fprintf (stderr
, " firstpos:");
2812 for (idx_t j
= 0; j
< stk
[-1].nfirstpos
; j
++)
2814 fprintf (stderr
, " %td:", firstpos
[j
- stk
[-1].nfirstpos
].index
);
2815 prtok (d
->tokens
[firstpos
[j
- stk
[-1].nfirstpos
].index
]);
2817 fprintf (stderr
, "\n lastpos:");
2818 for (idx_t j
= 0; j
< stk
[-1].nlastpos
; j
++)
2820 fprintf (stderr
, " %td:", lastpos
[j
- stk
[-1].nlastpos
].index
);
2821 prtok (d
->tokens
[lastpos
[j
- stk
[-1].nlastpos
].index
]);
2823 putc ('\n', stderr
);
2829 /* For each follow set that is the follow set of a real position,
2830 replace it with its epsilon closure. */
2831 epsclosure (d
, backward
);
2833 for (idx_t i
= 0; i
< tindex
; i
++)
2834 free (backward
[i
].elems
);
2841 for (idx_t i
= 0; i
< tindex
; i
++)
2842 if (d
->tokens
[i
] == BEG
|| d
->tokens
[i
] < NOTCHAR
2843 || d
->tokens
[i
] == BACKREF
|| d
->tokens
[i
] == ANYCHAR
2844 || d
->tokens
[i
] == MBCSET
|| d
->tokens
[i
] >= CSET
)
2846 fprintf (stderr
, "follows(%td:", i
);
2847 prtok (d
->tokens
[i
]);
2848 fprintf (stderr
, "):");
2849 for (idx_t j
= 0; j
< d
->follows
[i
].nelem
; j
++)
2851 fprintf (stderr
, " %td:", d
->follows
[i
].elems
[j
].index
);
2852 prtok (d
->tokens
[d
->follows
[i
].elems
[j
].index
]);
2854 putc ('\n', stderr
);
2859 pos
.constraint
= NO_CONSTRAINT
;
2861 alloc_position_set (&tmp
, 1);
2865 d
->separates
= xcalloc (tindex
, sizeof *d
->separates
);
2867 for (idx_t i
= 0; i
< tindex
; i
++)
2869 if (prev_newline_dependent (d
->constraints
[i
]))
2870 d
->separates
[i
] |= CTX_NEWLINE
;
2871 if (prev_letter_dependent (d
->constraints
[i
]))
2872 d
->separates
[i
] |= CTX_LETTER
;
2874 for (idx_t j
= 0; j
< d
->follows
[i
].nelem
; j
++)
2876 if (prev_newline_dependent (d
->follows
[i
].elems
[j
].constraint
))
2877 d
->separates
[i
] |= CTX_NEWLINE
;
2878 if (prev_letter_dependent (d
->follows
[i
].elems
[j
].constraint
))
2879 d
->separates
[i
] |= CTX_LETTER
;
2883 /* Context wanted by some position. */
2884 int separate_contexts
= state_separate_contexts (d
, &tmp
);
2886 /* Build the initial state. */
2887 if (separate_contexts
& CTX_NEWLINE
)
2888 state_index (d
, &tmp
, CTX_NEWLINE
);
2889 d
->initstate_notbol
= d
->min_trcount
2890 = state_index (d
, &tmp
, separate_contexts
^ CTX_ANY
);
2891 if (separate_contexts
& CTX_LETTER
)
2892 d
->min_trcount
= state_index (d
, &tmp
, CTX_LETTER
);
2898 free (merged
.elems
);
2902 /* Make sure D's state arrays are large enough to hold NEW_STATE. */
2904 realloc_trans_if_necessary (struct dfa
*d
)
2906 state_num oldalloc
= d
->tralloc
;
2907 if (oldalloc
< d
->sindex
)
2909 state_num
**realtrans
= d
->trans
? d
->trans
- 2 : NULL
;
2910 idx_t newalloc1
= realtrans
? d
->tralloc
+ 2 : 0;
2911 realtrans
= xpalloc (realtrans
, &newalloc1
, d
->sindex
- oldalloc
,
2912 -1, sizeof *realtrans
);
2913 realtrans
[0] = realtrans
[1] = NULL
;
2914 d
->trans
= realtrans
+ 2;
2915 idx_t newalloc
= d
->tralloc
= newalloc1
- 2;
2916 d
->fails
= xnrealloc (d
->fails
, newalloc
, sizeof *d
->fails
);
2917 d
->success
= xnrealloc (d
->success
, newalloc
, sizeof *d
->success
);
2918 d
->newlines
= xnrealloc (d
->newlines
, newalloc
, sizeof *d
->newlines
);
2919 if (d
->localeinfo
.multibyte
)
2921 realtrans
= d
->mb_trans
? d
->mb_trans
- 2 : NULL
;
2922 realtrans
= xnrealloc (realtrans
, newalloc1
, sizeof *realtrans
);
2924 realtrans
[0] = realtrans
[1] = NULL
;
2925 d
->mb_trans
= realtrans
+ 2;
2927 for (; oldalloc
< newalloc
; oldalloc
++)
2929 d
->trans
[oldalloc
] = NULL
;
2930 d
->fails
[oldalloc
] = NULL
;
2931 if (d
->localeinfo
.multibyte
)
2932 d
->mb_trans
[oldalloc
] = NULL
;
2938 Calculate the transition table for a new state derived from state s
2939 for a compiled dfa d after input character uc, and return the new
2942 Do not worry about all possible input characters; calculate just the group
2943 of positions that match uc. Label it with the set of characters that
2944 every position in the group matches (taking into account, if necessary,
2945 preceding context information of s). Then find the union
2946 of these positions' follows, i.e., the set of positions of the
2947 new state. For each character in the group's label, set the transition
2948 on this character to be to a state corresponding to the set's positions,
2949 and its associated backward context information, if necessary.
2951 When building a searching matcher, include the positions of state
2954 The group is constructed by building an equivalence-class
2955 partition of the positions of s.
2957 For each position, find the set of characters C that it matches. Eliminate
2958 any characters from C that fail on grounds of backward context.
2960 Check whether the group's label L has nonempty
2961 intersection with C. If L - C is nonempty, create a new group labeled
2962 L - C and having the same positions as the current group, and set L to
2963 the intersection of L and C. Insert the position in the group, set
2964 C = C - L, and resume scanning.
2966 If after comparing with every group there are characters remaining in C,
2967 create a new group labeled with the characters of C and insert this
2968 position in that group. */
2971 build_state (state_num s
, struct dfa
*d
, unsigned char uc
)
2973 position_set follows
; /* Union of the follows for each
2974 position of the current state. */
2975 position_set group
; /* Positions that match the input char. */
2976 position_set tmp
; /* Temporary space for merging sets. */
2977 state_num state
; /* New state. */
2978 state_num state_newline
; /* New state on a newline transition. */
2979 state_num state_letter
; /* New state on a letter transition. */
2982 fprintf (stderr
, "build state %td\n", s
);
2985 /* A pointer to the new transition table, and the table itself. */
2986 state_num
**ptrans
= (accepting (s
, d
) ? d
->fails
: d
->trans
) + s
;
2987 state_num
*trans
= *ptrans
;
2991 /* MAX_TRCOUNT is an arbitrary upper limit on the number of
2992 transition tables that can exist at once, other than for
2993 initial states. Often-used transition tables are quickly
2994 rebuilt, whereas rarely-used ones are cleared away. */
2995 if (MAX_TRCOUNT
<= d
->trcount
)
2997 for (state_num i
= d
->min_trcount
; i
< d
->tralloc
; i
++)
3001 d
->trans
[i
] = d
->fails
[i
] = NULL
;
3007 *ptrans
= trans
= xmalloc (NOTCHAR
* sizeof *trans
);
3009 /* Fill transition table with a default value which means that the
3010 transited state has not been calculated yet. */
3011 for (int i
= 0; i
< NOTCHAR
; i
++)
3015 /* Set up the success bits for this state. */
3017 if (accepts_in_context (d
->states
[s
].context
, CTX_NEWLINE
, s
, d
))
3018 d
->success
[s
] |= CTX_NEWLINE
;
3019 if (accepts_in_context (d
->states
[s
].context
, CTX_LETTER
, s
, d
))
3020 d
->success
[s
] |= CTX_LETTER
;
3021 if (accepts_in_context (d
->states
[s
].context
, CTX_NONE
, s
, d
))
3022 d
->success
[s
] |= CTX_NONE
;
3024 alloc_position_set (&follows
, d
->nleaves
);
3026 /* Find the union of the follows of the positions of the group.
3027 This is a hideously inefficient loop. Fix it someday. */
3028 for (idx_t j
= 0; j
< d
->states
[s
].elems
.nelem
; j
++)
3030 k
< d
->follows
[d
->states
[s
].elems
.elems
[j
].index
].nelem
; ++k
)
3031 insert (d
->follows
[d
->states
[s
].elems
.elems
[j
].index
].elems
[k
],
3034 /* Positions that match the input char. */
3035 alloc_position_set (&group
, d
->nleaves
);
3037 /* The group's label. */
3041 for (idx_t i
= 0; i
< follows
.nelem
; i
++)
3043 charclass matches
; /* Set of matching characters. */
3044 position pos
= follows
.elems
[i
];
3045 bool matched
= false;
3046 if (d
->tokens
[pos
.index
] >= 0 && d
->tokens
[pos
.index
] < NOTCHAR
)
3049 setbit (d
->tokens
[pos
.index
], &matches
);
3050 if (d
->tokens
[pos
.index
] == uc
)
3053 else if (d
->tokens
[pos
.index
] >= CSET
)
3055 matches
= d
->charclasses
[d
->tokens
[pos
.index
] - CSET
];
3056 if (tstbit (uc
, &matches
))
3059 else if (d
->tokens
[pos
.index
] == ANYCHAR
)
3061 matches
= d
->charclasses
[d
->canychar
];
3062 if (tstbit (uc
, &matches
))
3065 /* ANYCHAR must match with a single character, so we must put
3066 it to D->states[s].mbps which contains the positions which
3067 can match with a single character not a byte. If all
3068 positions which has ANYCHAR does not depend on context of
3069 next character, we put the follows instead of it to
3070 D->states[s].mbps to optimize. */
3071 if (succeeds_in_context (pos
.constraint
, d
->states
[s
].context
,
3074 if (d
->states
[s
].mbps
.nelem
== 0)
3075 alloc_position_set (&d
->states
[s
].mbps
, 1);
3076 insert (pos
, &d
->states
[s
].mbps
);
3082 /* Some characters may need to be eliminated from matches because
3083 they fail in the current context. */
3084 if (pos
.constraint
!= NO_CONSTRAINT
)
3086 if (!succeeds_in_context (pos
.constraint
,
3087 d
->states
[s
].context
, CTX_NEWLINE
))
3088 for (int j
= 0; j
< CHARCLASS_WORDS
; j
++)
3089 matches
.w
[j
] &= ~d
->syntax
.newline
.w
[j
];
3090 if (!succeeds_in_context (pos
.constraint
,
3091 d
->states
[s
].context
, CTX_LETTER
))
3092 for (int j
= 0; j
< CHARCLASS_WORDS
; ++j
)
3093 matches
.w
[j
] &= ~d
->syntax
.letters
.w
[j
];
3094 if (!succeeds_in_context (pos
.constraint
,
3095 d
->states
[s
].context
, CTX_NONE
))
3096 for (int j
= 0; j
< CHARCLASS_WORDS
; ++j
)
3097 matches
.w
[j
] &= d
->syntax
.letters
.w
[j
] | d
->syntax
.newline
.w
[j
];
3099 /* If there are no characters left, there's no point in going on. */
3100 if (emptyset (&matches
))
3103 /* If we have reset the bit that made us declare "matched", reset
3104 that indicator, too. This is required to avoid an infinite loop
3105 with this command: echo cx | LC_ALL=C grep -E 'c\b[x ]' */
3106 if (!tstbit (uc
, &matches
))
3111 fprintf (stderr
, " nextpos %td:", pos
.index
);
3112 prtok (d
->tokens
[pos
.index
]);
3113 fprintf (stderr
, " of");
3114 for (unsigned j
= 0; j
< NOTCHAR
; j
++)
3115 if (tstbit (j
, &matches
))
3116 fprintf (stderr
, " 0x%02x", j
);
3117 fprintf (stderr
, "\n");
3122 for (int k
= 0; k
< CHARCLASS_WORDS
; ++k
)
3123 label
.w
[k
] &= matches
.w
[k
];
3124 append (pos
, &group
);
3128 for (int k
= 0; k
< CHARCLASS_WORDS
; ++k
)
3129 label
.w
[k
] &= ~matches
.w
[k
];
3133 alloc_position_set (&tmp
, d
->nleaves
);
3135 if (group
.nelem
> 0)
3137 /* If we are building a searching matcher, throw in the positions
3138 of state 0 as well, if possible. */
3141 /* If a token in follows.elems is not 1st byte of a multibyte
3142 character, or the states of follows must accept the bytes
3143 which are not 1st byte of the multibyte character.
3144 Then, if a state of follows encounters a byte, it must not be
3145 a 1st byte of a multibyte character nor a single byte character.
3146 In this case, do not add state[0].follows to next state, because
3147 state[0] must accept 1st-byte.
3149 For example, suppose <sb a> is a certain single byte character,
3150 <mb A> is a certain multibyte character, and the codepoint of
3151 <sb a> equals the 2nd byte of the codepoint of <mb A>. When
3152 state[0] accepts <sb a>, state[i] transits to state[i+1] by
3153 accepting the 1st byte of <mb A>, and state[i+1] accepts the
3154 2nd byte of <mb A>, if state[i+1] encounters the codepoint of
3155 <sb a>, it must not be <sb a> but the 2nd byte of <mb A>, so do
3156 not add state[0]. */
3158 bool mergeit
= !d
->localeinfo
.multibyte
;
3162 for (idx_t j
= 0; mergeit
&& j
< group
.nelem
; j
++)
3163 mergeit
&= d
->multibyte_prop
[group
.elems
[j
].index
];
3166 merge2 (&group
, &d
->states
[0].elems
, &tmp
);
3169 /* Find out if the new state will want any context information,
3170 by calculating possible contexts that the group can match,
3171 and separate contexts that the new state wants to know. */
3172 int possible_contexts
= charclass_context (d
, &label
);
3173 int separate_contexts
= state_separate_contexts (d
, &group
);
3175 /* Find the state(s) corresponding to the union of the follows. */
3176 if (possible_contexts
& ~separate_contexts
)
3177 state
= state_index (d
, &group
, separate_contexts
^ CTX_ANY
);
3180 if (separate_contexts
& possible_contexts
& CTX_NEWLINE
)
3181 state_newline
= state_index (d
, &group
, CTX_NEWLINE
);
3183 state_newline
= state
;
3184 if (separate_contexts
& possible_contexts
& CTX_LETTER
)
3185 state_letter
= state_index (d
, &group
, CTX_LETTER
);
3187 state_letter
= state
;
3189 /* Reallocate now, to reallocate any newline transition properly. */
3190 realloc_trans_if_necessary (d
);
3193 /* If we are a searching matcher, the default transition is to a state
3194 containing the positions of state 0, otherwise the default transition
3195 is to fail miserably. */
3196 else if (d
->searchflag
)
3199 state_letter
= d
->min_trcount
- 1;
3200 state
= d
->initstate_notbol
;
3209 /* Set the transitions for each character in the label. */
3210 for (int i
= 0; i
< NOTCHAR
; i
++)
3211 if (tstbit (i
, &label
))
3212 switch (d
->syntax
.sbit
[i
])
3215 trans
[i
] = state_newline
;
3218 trans
[i
] = state_letter
;
3226 fprintf (stderr
, "trans table %td", s
);
3227 for (int i
= 0; i
< NOTCHAR
; ++i
)
3230 fprintf (stderr
, "\n");
3231 fprintf (stderr
, " %2td", trans
[i
]);
3233 fprintf (stderr
, "\n");
3237 free (follows
.elems
);
3240 /* Keep the newline transition in a special place so we can use it as
3242 if (tstbit (d
->syntax
.eolbyte
, &label
))
3244 d
->newlines
[s
] = trans
[d
->syntax
.eolbyte
];
3245 trans
[d
->syntax
.eolbyte
] = -1;
3251 /* Multibyte character handling sub-routines for dfaexec. */
3253 /* Consume a single byte and transit state from 's' to '*next_state'.
3254 This function is almost same as the state transition routin in dfaexec.
3255 But state transition is done just once, otherwise matching succeed or
3256 reach the end of the buffer. */
3258 transit_state_singlebyte (struct dfa
*d
, state_num s
, unsigned char const **pp
)
3264 else if (d
->fails
[s
])
3268 build_state (s
, d
, **pp
);
3279 build_state (s
, d
, **pp
);
3284 /* Transit state from s, then return new state and update the pointer of
3285 the buffer. This function is for a period operator which can match a
3286 multi-byte character. */
3288 transit_state (struct dfa
*d
, state_num s
, unsigned char const **pp
,
3289 unsigned char const *end
)
3293 int mbclen
= mbs_to_wchar (&wc
, (char const *) *pp
, end
- *pp
, d
);
3295 /* This state has some operators which can match a multibyte character. */
3296 d
->mb_follows
.nelem
= 0;
3298 /* Calculate the state which can be reached from the state 's' by
3299 consuming 'mbclen' single bytes from the buffer. */
3302 for (mbci
= 0; mbci
< mbclen
&& (mbci
== 0 || d
->min_trcount
<= s
); mbci
++)
3303 s
= transit_state_singlebyte (d
, s
, pp
);
3304 *pp
+= mbclen
- mbci
;
3308 /* It is an invalid character, so ANYCHAR is not accepted. */
3312 /* If all positions which have ANYCHAR do not depend on the context
3313 of the next character, calculate the next state with
3314 pre-calculated follows and cache the result. */
3315 if (d
->states
[s1
].mb_trindex
< 0)
3317 if (MAX_TRCOUNT
<= d
->mb_trcount
)
3320 for (s3
= -1; s3
< d
->tralloc
; s3
++)
3322 free (d
->mb_trans
[s3
]);
3323 d
->mb_trans
[s3
] = NULL
;
3326 for (state_num i
= 0; i
< d
->sindex
; i
++)
3327 d
->states
[i
].mb_trindex
= -1;
3330 d
->states
[s1
].mb_trindex
= d
->mb_trcount
++;
3333 if (! d
->mb_trans
[s
])
3335 enum { TRANSPTR_SIZE
= sizeof *d
->mb_trans
[s
] };
3336 enum { TRANSALLOC_SIZE
= MAX_TRCOUNT
* TRANSPTR_SIZE
};
3337 d
->mb_trans
[s
] = xmalloc (TRANSALLOC_SIZE
);
3338 for (int i
= 0; i
< MAX_TRCOUNT
; i
++)
3339 d
->mb_trans
[s
][i
] = -1;
3341 else if (d
->mb_trans
[s
][d
->states
[s1
].mb_trindex
] >= 0)
3342 return d
->mb_trans
[s
][d
->states
[s1
].mb_trindex
];
3345 copy (&d
->states
[s1
].mbps
, &d
->mb_follows
);
3347 merge (&d
->states
[s1
].mbps
, &d
->states
[s
].elems
, &d
->mb_follows
);
3349 int separate_contexts
= state_separate_contexts (d
, &d
->mb_follows
);
3350 state_num s2
= state_index (d
, &d
->mb_follows
, separate_contexts
^ CTX_ANY
);
3351 realloc_trans_if_necessary (d
);
3353 d
->mb_trans
[s
][d
->states
[s1
].mb_trindex
] = s2
;
3358 /* The initial state may encounter a byte which is not a single byte character
3359 nor the first byte of a multibyte character. But it is incorrect for the
3360 initial state to accept such a byte. For example, in Shift JIS the regular
3361 expression "\\" accepts the codepoint 0x5c, but should not accept the second
3362 byte of the codepoint 0x815c. Then the initial state must skip the bytes
3363 that are not a single byte character nor the first byte of a multibyte
3366 Given DFA state d, use mbs_to_wchar to advance MBP until it reaches
3367 or exceeds P, and return the advanced MBP. If WCP is non-NULL and
3368 the result is greater than P, set *WCP to the final wide character
3369 processed, or to WEOF if no wide character is processed. Otherwise,
3370 if WCP is non-NULL, *WCP may or may not be updated.
3372 Both P and MBP must be no larger than END. */
3373 static unsigned char const *
3374 skip_remains_mb (struct dfa
*d
, unsigned char const *p
,
3375 unsigned char const *mbp
, char const *end
)
3377 if (d
->syntax
.never_trail
[*p
])
3382 mbp
+= mbs_to_wchar (&wc
, (char const *) mbp
,
3383 end
- (char const *) mbp
, d
);
3388 /* Search through a buffer looking for a match to the struct dfa *D.
3389 Find the first occurrence of a string matching the regexp in the
3390 buffer, and the shortest possible version thereof. Return a pointer to
3391 the first character after the match, or NULL if none is found. BEGIN
3392 points to the beginning of the buffer, and END points to the first byte
3393 after its end. Note however that we store a sentinel byte (usually
3394 newline) in *END, so the actual buffer must be one byte longer.
3395 When ALLOW_NL, newlines may appear in the matching string.
3396 If COUNT is non-NULL, increment *COUNT once for each newline processed.
3397 If MULTIBYTE, the input consists of multibyte characters and/or
3398 encoding-error bytes. Otherwise, it consists of single-byte characters.
3399 Here is the list of features that make this DFA matcher punt:
3400 - [M-N] range in non-simple locale: regex is up to 25% faster on [a-z]
3401 - [^...] in non-simple locale
3402 - [[=foo=]] or [[.foo.]]
3403 - [[:alpha:]] etc. in multibyte locale (except [[:digit:]] works OK)
3404 - back-reference: (.)\1
3405 - word-delimiter in multibyte locale: \<, \>, \b, \B
3406 See struct localeinfo.simple for the definition of "simple locale". */
3408 static inline char *
3409 dfaexec_main (struct dfa
*d
, char const *begin
, char *end
, bool allow_nl
,
3410 ptrdiff_t *count
, bool multibyte
)
3412 if (MAX_TRCOUNT
<= d
->sindex
)
3414 for (state_num s
= d
->min_trcount
; s
< d
->sindex
; s
++)
3416 free (d
->states
[s
].elems
.elems
);
3417 free (d
->states
[s
].mbps
.elems
);
3419 d
->sindex
= d
->min_trcount
;
3423 for (state_num s
= 0; s
< d
->tralloc
; s
++)
3427 d
->trans
[s
] = d
->fails
[s
] = NULL
;
3432 if (d
->localeinfo
.multibyte
&& d
->mb_trans
)
3434 for (state_num s
= -1; s
< d
->tralloc
; s
++)
3436 free (d
->mb_trans
[s
]);
3437 d
->mb_trans
[s
] = NULL
;
3439 for (state_num s
= 0; s
< d
->min_trcount
; s
++)
3440 d
->states
[s
].mb_trindex
= -1;
3446 realloc_trans_if_necessary (d
);
3448 /* Current state. */
3449 state_num s
= 0, s1
= 0;
3451 /* Current input character. */
3452 unsigned char const *p
= (unsigned char const *) begin
;
3453 unsigned char const *mbp
= p
;
3455 /* Copy of d->trans so it can be optimized into a register. */
3456 state_num
**trans
= d
->trans
;
3457 unsigned char eol
= d
->syntax
.eolbyte
; /* Likewise for eolbyte. */
3458 unsigned char saved_end
= *(unsigned char *) end
;
3463 memset (&d
->mbs
, 0, sizeof d
->mbs
);
3464 if (d
->mb_follows
.alloc
== 0)
3465 alloc_position_set (&d
->mb_follows
, d
->nleaves
);
3472 while ((t
= trans
[s
]) != NULL
)
3474 if (s
< d
->min_trcount
)
3476 if (!multibyte
|| d
->states
[s
].mbps
.nelem
== 0)
3482 p
= mbp
= skip_remains_mb (d
, p
, mbp
, end
);
3489 if (d
->states
[s
].mbps
.nelem
== 0
3490 || d
->localeinfo
.sbctowc
[*p
] != WEOF
|| (char *) p
>= end
)
3492 /* If an input character does not match ANYCHAR, do it
3493 like a single-byte character. */
3498 s
= transit_state (d
, s
, &p
, (unsigned char *) end
);
3511 s1
= tmp
; /* swap */
3514 if (s
< d
->min_trcount
)
3527 s
= build_state (s1
, d
, p
[-1]);
3530 else if ((char *) p
<= end
&& p
[-1] == eol
&& 0 <= d
->newlines
[s1
])
3532 /* The previous character was a newline. Count it, and skip
3533 checking of multibyte character boundary until here. */
3537 s
= (allow_nl
? d
->newlines
[s1
]
3538 : d
->syntax
.sbit
[eol
] == CTX_NEWLINE
? 0
3539 : d
->syntax
.sbit
[eol
] == CTX_LETTER
? d
->min_trcount
- 1
3540 : d
->initstate_notbol
);
3548 else if (d
->fails
[s
])
3550 if ((d
->success
[s
] & d
->syntax
.sbit
[*p
])
3551 || ((char *) p
== end
3552 && accepts_in_context (d
->states
[s
].context
, CTX_NEWLINE
, s
,
3556 if (multibyte
&& s
< d
->min_trcount
)
3557 p
= mbp
= skip_remains_mb (d
, p
, mbp
, end
);
3560 if (!multibyte
|| d
->states
[s
].mbps
.nelem
== 0
3561 || d
->localeinfo
.sbctowc
[*p
] != WEOF
|| (char *) p
>= end
)
3563 /* If a input character does not match ANYCHAR, do it
3564 like a single-byte character. */
3565 s
= d
->fails
[s
][*p
++];
3569 s
= transit_state (d
, s
, &p
, (unsigned char *) end
);
3576 build_state (s
, d
, p
[0]);
3588 /* Specialized versions of dfaexec for multibyte and single-byte cases.
3589 This is for performance, as dfaexec_main is an inline function. */
3592 dfaexec_mb (struct dfa
*d
, char const *begin
, char *end
,
3593 bool allow_nl
, ptrdiff_t *count
, bool *backref
)
3595 return dfaexec_main (d
, begin
, end
, allow_nl
, count
, true);
3599 dfaexec_sb (struct dfa
*d
, char const *begin
, char *end
,
3600 bool allow_nl
, ptrdiff_t *count
, bool *backref
)
3602 return dfaexec_main (d
, begin
, end
, allow_nl
, count
, false);
3605 /* Always set *BACKREF and return BEGIN. Use this wrapper for
3606 any regexp that uses a construct not supported by this code. */
3608 dfaexec_noop (struct dfa
*d
, char const *begin
, char *end
,
3609 bool allow_nl
, ptrdiff_t *count
, bool *backref
)
3612 return (char *) begin
;
3615 /* Like dfaexec_main (D, BEGIN, END, ALLOW_NL, COUNT, D->localeinfo.multibyte),
3616 but faster and set *BACKREF if the DFA code does not support this
3620 dfaexec (struct dfa
*d
, char const *begin
, char *end
,
3621 bool allow_nl
, ptrdiff_t *count
, bool *backref
)
3623 return d
->dfaexec (d
, begin
, end
, allow_nl
, count
, backref
);
3627 dfasuperset (struct dfa
const *d
)
3633 dfaisfast (struct dfa
const *d
)
3639 free_mbdata (struct dfa
*d
)
3641 free (d
->multibyte_prop
);
3642 free (d
->lex
.brack
.chars
);
3643 free (d
->mb_follows
.elems
);
3648 for (s
= -1; s
< d
->tralloc
; s
++)
3649 free (d
->mb_trans
[s
]);
3650 free (d
->mb_trans
- 2);
3654 /* Return true if every construct in D is supported by this DFA matcher. */
3656 dfasupported (struct dfa
const *d
)
3658 for (idx_t i
= 0; i
< d
->tindex
; i
++)
3660 switch (d
->tokens
[i
])
3666 if (!d
->localeinfo
.multibyte
)
3677 /* Disable use of the superset DFA if it is not likely to help
3680 maybe_disable_superset_dfa (struct dfa
*d
)
3682 if (!d
->localeinfo
.using_utf8
)
3685 bool have_backref
= false;
3686 for (idx_t i
= 0; i
< d
->tindex
; i
++)
3688 switch (d
->tokens
[i
])
3694 have_backref
= true;
3697 /* Requires multi-byte algorithm. */
3704 if (!have_backref
&& d
->superset
)
3706 /* The superset DFA is not likely to be much faster, so remove it. */
3707 dfafree (d
->superset
);
3713 d
->localeinfo
.multibyte
= false;
3714 d
->dfaexec
= dfaexec_sb
;
3719 dfassbuild (struct dfa
*d
)
3721 struct dfa
*sup
= dfaalloc ();
3724 sup
->localeinfo
.multibyte
= false;
3725 sup
->dfaexec
= dfaexec_sb
;
3726 sup
->multibyte_prop
= NULL
;
3727 sup
->superset
= NULL
;
3730 sup
->constraints
= NULL
;
3731 sup
->separates
= NULL
;
3732 sup
->follows
= NULL
;
3736 sup
->success
= NULL
;
3737 sup
->newlines
= NULL
;
3739 sup
->charclasses
= xnmalloc (sup
->calloc
, sizeof *sup
->charclasses
);
3742 memcpy (sup
->charclasses
, d
->charclasses
,
3743 d
->cindex
* sizeof *sup
->charclasses
);
3746 sup
->tokens
= xnmalloc (d
->tindex
, 2 * sizeof *sup
->tokens
);
3747 sup
->talloc
= d
->tindex
* 2;
3749 bool have_achar
= false;
3750 bool have_nchar
= false;
3752 for (idx_t i
= j
= 0; i
< d
->tindex
; i
++)
3754 switch (d
->tokens
[i
])
3762 sup
->tokens
[j
++] = CSET
+ charclass_index (sup
, &ccl
);
3763 sup
->tokens
[j
++] = STAR
;
3764 if (d
->tokens
[i
+ 1] == QMARK
|| d
->tokens
[i
+ 1] == STAR
3765 || d
->tokens
[i
+ 1] == PLUS
)
3774 if (d
->localeinfo
.multibyte
)
3776 /* These constraints aren't supported in a multibyte locale.
3777 Ignore them in the superset DFA. */
3778 sup
->tokens
[j
++] = EMPTY
;
3783 sup
->tokens
[j
++] = d
->tokens
[i
];
3784 if ((0 <= d
->tokens
[i
] && d
->tokens
[i
] < NOTCHAR
)
3785 || d
->tokens
[i
] >= CSET
)
3792 if (have_nchar
&& (have_achar
|| d
->localeinfo
.multibyte
))
3801 /* Parse a string S of length LEN into D (but skip this step if S is null).
3802 Then analyze D and build a matcher for it.
3803 SEARCHFLAG says whether to build a searching or an exact matcher. */
3805 dfacomp (char const *s
, idx_t len
, struct dfa
*d
, bool searchflag
)
3808 dfaparse (s
, len
, d
);
3812 if (dfasupported (d
))
3814 maybe_disable_superset_dfa (d
);
3815 dfaanalyze (d
, searchflag
);
3819 d
->dfaexec
= dfaexec_noop
;
3825 dfaanalyze (d
->superset
, searchflag
);
3829 /* Free the storage held by the components of a dfa. */
3831 dfafree (struct dfa
*d
)
3833 free (d
->charclasses
);
3836 if (d
->localeinfo
.multibyte
)
3839 free (d
->constraints
);
3840 free (d
->separates
);
3842 for (idx_t i
= 0; i
< d
->sindex
; i
++)
3844 free (d
->states
[i
].elems
.elems
);
3845 free (d
->states
[i
].mbps
.elems
);
3851 for (idx_t i
= 0; i
< d
->tindex
; i
++)
3852 free (d
->follows
[i
].elems
);
3858 for (idx_t i
= 0; i
< d
->tralloc
; i
++)
3864 free (d
->trans
- 2);
3872 dfafree (d
->superset
);
3877 /* Having found the postfix representation of the regular expression,
3878 try to find a long sequence of characters that must appear in any line
3880 Finding a "longest" sequence is beyond the scope here;
3881 we take an easy way out and hope for the best.
3882 (Take "(ab|a)b"--please.)
3884 We do a bottom-up calculation of sequences of characters that must appear
3885 in matches of r.e.'s represented by trees rooted at the nodes of the postfix
3887 sequences that must appear at the left of the match ("left")
3888 sequences that must appear at the right of the match ("right")
3889 lists of sequences that must appear somewhere in the match ("in")
3890 sequences that must constitute the match ("is")
3892 When we get to the root of the tree, we use one of the longest of its
3893 calculated "in" sequences as our answer.
3895 The sequences calculated for the various types of node (in pseudo ANSI c)
3896 are shown below. "p" is the operand of unary operators (and the left-hand
3897 operand of binary operators); "q" is the right-hand operand of binary
3900 "ZERO" means "a zero-length sequence" below.
3902 Type left right is in
3903 ---- ---- ----- -- --
3904 char c # c # c # c # c
3906 ANYCHAR ZERO ZERO ZERO ZERO
3908 MBCSET ZERO ZERO ZERO ZERO
3910 CSET ZERO ZERO ZERO ZERO
3912 STAR ZERO ZERO ZERO ZERO
3914 QMARK ZERO ZERO ZERO ZERO
3916 PLUS p->left p->right ZERO p->in
3918 CAT (p->is==ZERO)? (q->is==ZERO)? (p->is!=ZERO && p->in plus
3919 p->left : q->right : q->is!=ZERO) ? q->in plus
3920 p->is##q->left p->right##q->is p->is##q->is : p->right##q->left
3923 OR longest common longest common (do p->is and substrings common
3924 leading trailing to q->is have same p->in and
3925 (sub)sequence (sub)sequence q->in length and content) ?
3926 of p->left of p->right
3927 and q->left and q->right p->is : NULL
3929 If there's anything else we recognize in the tree, all four sequences get set
3930 to zero-length sequences. If there's something we don't recognize in the
3931 tree, we just return a zero-length sequence.
3933 Break ties in favor of infrequent letters (choosing 'zzz' in preference to
3936 And ... is it here or someplace that we might ponder "optimizations" such as
3937 egrep 'psi|epsilon' -> egrep 'psi'
3938 egrep 'pepsi|epsilon' -> egrep 'epsi'
3939 (Yes, we now find "epsi" as a "string
3940 that must occur", but we might also
3941 simplify the *entire* r.e. being sought)
3942 grep '[c]' -> grep 'c'
3943 grep '(ab|a)b' -> grep 'ab'
3944 grep 'ab*' -> grep 'a'
3945 grep 'a*b' -> grep 'b'
3947 There are several issues:
3949 Is optimization easy (enough)?
3951 Does optimization actually accomplish anything,
3952 or is the automaton you get from "psi|epsilon" (for example)
3953 the same as the one you get from "psi" (for example)?
3955 Are optimizable r.e.'s likely to be used in real-life situations
3956 (something like 'ab*' is probably unlikely; something like is
3957 'psi|epsilon' is likelier)? */
3960 icatalloc (char *old
, char const *new)
3962 idx_t newsize
= strlen (new);
3965 idx_t oldsize
= strlen (old
);
3966 char *result
= xrealloc (old
, oldsize
+ newsize
+ 1);
3967 memcpy (result
+ oldsize
, new, newsize
+ 1);
3972 freelist (char **cpp
)
3979 enlist (char **cpp
, char *new, idx_t len
)
3981 new = memcpy (xmalloc (len
+ 1), new, len
);
3983 /* Is there already something in the list that's new (or longer)? */
3985 for (i
= 0; cpp
[i
] != NULL
; i
++)
3986 if (strstr (cpp
[i
], new) != NULL
)
3991 /* Eliminate any obsoleted strings. */
3992 for (idx_t j
= 0; cpp
[j
] != NULL
; )
3993 if (strstr (new, cpp
[j
]) == NULL
)
4003 /* Add the new string. */
4004 cpp
= xnrealloc (cpp
, i
+ 2, sizeof *cpp
);
4010 /* Given pointers to two strings, return a pointer to an allocated
4011 list of their distinct common substrings. */
4013 comsubs (char *left
, char const *right
)
4015 char **cpp
= xzalloc (sizeof *cpp
);
4017 for (char *lcp
= left
; *lcp
!= '\0'; lcp
++)
4020 char *rcp
= strchr (right
, *lcp
);
4024 for (i
= 1; lcp
[i
] != '\0' && lcp
[i
] == rcp
[i
]; ++i
)
4028 rcp
= strchr (rcp
+ 1, *lcp
);
4031 cpp
= enlist (cpp
, lcp
, len
);
4037 addlists (char **old
, char **new)
4040 old
= enlist (old
, *new, strlen (*new));
4044 /* Given two lists of substrings, return a new list giving substrings
4047 inboth (char **left
, char **right
)
4049 char **both
= xzalloc (sizeof *both
);
4051 for (idx_t lnum
= 0; left
[lnum
] != NULL
; lnum
++)
4053 for (idx_t rnum
= 0; right
[rnum
] != NULL
; rnum
++)
4055 char **temp
= comsubs (left
[lnum
], right
[rnum
]);
4056 both
= addlists (both
, temp
);
4064 typedef struct must must
;
4078 allocmust (must
*mp
, idx_t size
)
4080 must
*new_mp
= xmalloc (sizeof *new_mp
);
4081 new_mp
->in
= xzalloc (sizeof *new_mp
->in
);
4082 new_mp
->left
= xzalloc (size
);
4083 new_mp
->right
= xzalloc (size
);
4084 new_mp
->is
= xzalloc (size
);
4085 new_mp
->begline
= false;
4086 new_mp
->endline
= false;
4092 resetmust (must
*mp
)
4096 mp
->left
[0] = mp
->right
[0] = mp
->is
[0] = '\0';
4097 mp
->begline
= false;
4098 mp
->endline
= false;
4113 dfamust (struct dfa
const *d
)
4116 char const *result
= "";
4118 bool begline
= false;
4119 bool endline
= false;
4120 bool need_begline
= false;
4121 bool need_endline
= false;
4122 bool case_fold_unibyte
= d
->syntax
.case_fold
& !d
->localeinfo
.multibyte
;
4124 for (idx_t ri
= 1; ri
+ 1 < d
->tindex
; ri
++)
4126 token t
= d
->tokens
[ri
];
4130 mp
= allocmust (mp
, 2);
4132 need_begline
= true;
4135 mp
= allocmust (mp
, 2);
4137 need_endline
= true;
4141 assert (!"neither LPAREN nor RPAREN may appear here");
4151 mp
= allocmust (mp
, 2);
4163 must
*lmp
= mp
= mp
->prev
;
4166 /* Guaranteed to be. Unlikely, but ... */
4167 if (streq (lmp
->is
, rmp
->is
))
4169 lmp
->begline
&= rmp
->begline
;
4170 lmp
->endline
&= rmp
->endline
;
4175 lmp
->begline
= false;
4176 lmp
->endline
= false;
4178 /* Left side--easy */
4180 while (lmp
->left
[i
] != '\0' && lmp
->left
[i
] == rmp
->left
[i
])
4182 lmp
->left
[i
] = '\0';
4184 ln
= strlen (lmp
->right
);
4185 rn
= strlen (rmp
->right
);
4189 for (i
= 0; i
< n
; ++i
)
4190 if (lmp
->right
[ln
- i
- 1] != rmp
->right
[rn
- i
- 1])
4192 for (j
= 0; j
< i
; ++j
)
4193 lmp
->right
[j
] = lmp
->right
[(ln
- i
) + j
];
4194 lmp
->right
[j
] = '\0';
4195 new = inboth (lmp
->in
, rmp
->in
);
4209 for (idx_t i
= 0; mp
->in
[i
] != NULL
; i
++)
4210 if (strlen (mp
->in
[i
]) > strlen (result
))
4212 if (streq (result
, mp
->is
))
4214 if ((!need_begline
|| mp
->begline
) && (!need_endline
4217 begline
= mp
->begline
;
4218 endline
= mp
->endline
;
4225 must
*lmp
= mp
= mp
->prev
;
4227 /* In. Everything in left, plus everything in
4228 right, plus concatenation of
4229 left's right and right's left. */
4230 lmp
->in
= addlists (lmp
->in
, rmp
->in
);
4231 if (lmp
->right
[0] != '\0' && rmp
->left
[0] != '\0')
4233 idx_t lrlen
= strlen (lmp
->right
);
4234 idx_t rllen
= strlen (rmp
->left
);
4235 char *tp
= xmalloc (lrlen
+ rllen
);
4236 memcpy (tp
, lmp
->right
, lrlen
);
4237 memcpy (tp
+ lrlen
, rmp
->left
, rllen
);
4238 lmp
->in
= enlist (lmp
->in
, tp
, lrlen
+ rllen
);
4242 if (lmp
->is
[0] != '\0')
4243 lmp
->left
= icatalloc (lmp
->left
, rmp
->left
);
4245 if (rmp
->is
[0] == '\0')
4246 lmp
->right
[0] = '\0';
4247 lmp
->right
= icatalloc (lmp
->right
, rmp
->right
);
4248 /* Guaranteed to be */
4249 if ((lmp
->is
[0] != '\0' || lmp
->begline
)
4250 && (rmp
->is
[0] != '\0' || rmp
->endline
))
4252 lmp
->is
= icatalloc (lmp
->is
, rmp
->is
);
4253 lmp
->endline
= rmp
->endline
;
4258 lmp
->begline
= false;
4259 lmp
->endline
= false;
4266 /* Not on *my* shift. */
4272 /* If T is a singleton, or if case-folding in a unibyte
4273 locale and T's members all case-fold to the same char,
4274 convert T to one of its members. Otherwise, do
4275 nothing further with T. */
4276 charclass
*ccl
= &d
->charclasses
[t
- CSET
];
4278 for (j
= 0; j
< NOTCHAR
; j
++)
4279 if (tstbit (j
, ccl
))
4281 if (! (j
< NOTCHAR
))
4283 mp
= allocmust (mp
, 2);
4287 while (++j
< NOTCHAR
)
4289 && ! (case_fold_unibyte
4290 && toupper (j
) == toupper (t
)))
4294 mp
= allocmust (mp
, 2);
4300 if (d
->tokens
[ri
+ 1] == CAT
)
4302 for (; rj
< d
->tindex
- 1; rj
+= 2)
4304 if ((rj
!= ri
&& (d
->tokens
[rj
] <= 0
4305 || NOTCHAR
<= d
->tokens
[rj
]))
4306 || d
->tokens
[rj
+ 1] != CAT
)
4310 mp
= allocmust (mp
, ((rj
- ri
) >> 1) + 1);
4311 mp
->is
[0] = mp
->left
[0] = mp
->right
[0]
4312 = case_fold_unibyte
? toupper (t
) : t
;
4315 for (i
= 1; ri
+ 2 < rj
; i
++)
4319 mp
->is
[i
] = mp
->left
[i
] = mp
->right
[i
]
4320 = case_fold_unibyte
? toupper (t
) : t
;
4322 mp
->is
[i
] = mp
->left
[i
] = mp
->right
[i
] = '\0';
4323 mp
->in
= enlist (mp
->in
, mp
->is
, i
);
4329 struct dfamust
*dm
= NULL
;
4332 dm
= xmalloc (FLEXSIZEOF (struct dfamust
, must
, strlen (result
) + 1));
4334 dm
->begline
= begline
;
4335 dm
->endline
= endline
;
4336 strcpy (dm
->must
, result
);
4341 must
*prev
= mp
->prev
;
4350 dfamustfree (struct dfamust
*dm
)
4358 return xmalloc (sizeof (struct dfa
));
4361 /* Initialize DFA. */
4363 dfasyntax (struct dfa
*dfa
, struct localeinfo
const *linfo
,
4364 reg_syntax_t bits
, int dfaopts
)
4366 memset (dfa
, 0, offsetof (struct dfa
, dfaexec
));
4367 dfa
->dfaexec
= linfo
->multibyte
? dfaexec_mb
: dfaexec_sb
;
4368 dfa
->localeinfo
= *linfo
;
4370 dfa
->fast
= !dfa
->localeinfo
.multibyte
;
4373 dfa
->syntax
.syntax_bits_set
= true;
4374 dfa
->syntax
.case_fold
= (bits
& RE_ICASE
) != 0;
4375 dfa
->syntax
.anchor
= (dfaopts
& DFA_ANCHOR
) != 0;
4376 dfa
->syntax
.eolbyte
= dfaopts
& DFA_EOL_NUL
? '\0' : '\n';
4377 dfa
->syntax
.syntax_bits
= bits
;
4379 for (int i
= CHAR_MIN
; i
<= CHAR_MAX
; ++i
)
4381 unsigned char uc
= i
;
4383 dfa
->syntax
.sbit
[uc
] = char_context (dfa
, uc
);
4384 switch (dfa
->syntax
.sbit
[uc
])
4387 setbit (uc
, &dfa
->syntax
.letters
);
4390 setbit (uc
, &dfa
->syntax
.newline
);
4394 /* POSIX requires that the five bytes in "\n\r./" (including the
4395 terminating NUL) cannot occur inside a multibyte character. */
4396 dfa
->syntax
.never_trail
[uc
] = (dfa
->localeinfo
.using_utf8
4397 ? (uc
& 0xc0) != 0x80
4398 : strchr ("\n\r./", uc
) != NULL
);
4402 /* Initialize TO by copying FROM's syntax settings. */
4404 dfacopysyntax (struct dfa
*to
, struct dfa
const *from
)
4406 memset (to
, 0, offsetof (struct dfa
, syntax
));
4408 to
->fast
= from
->fast
;
4409 to
->syntax
= from
->syntax
;
4410 to
->dfaexec
= from
->dfaexec
;
4411 to
->localeinfo
= from
->localeinfo
;
4414 /* vim:set shiftwidth=2: */