1 #if !defined(_RX_H) || defined(RX_WANT_SE_DEFS)
4 /* Copyright (C) 1992, 1993 Free Software Foundation, Inc.
6 This file is part of the librx library.
8 Librx is free software; you can redistribute it and/or modify it under
9 the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 Librx is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU Library General Public
19 License along with this software; see the file COPYING.LIB. If not,
20 write to the Free Software Foundation, 675 Mass Ave, Cambridge, MA
22 /* t. lord Wed Sep 23 18:20:57 1992 */
34 #if RX_WANT_SE_DEFS != 1
38 #ifndef RX_WANT_SE_DEFS
40 /* This page: Bitsets */
43 typedef unsigned int RX_subset;
44 #define RX_subset_bits (32)
45 #define RX_subset_mask (RX_subset_bits - 1)
48 typedef RX_subset * rx_Bitset;
51 typedef void (*rx_bitset_iterator) (void *, int member_index);
53 typedef void (*rx_bitset_iterator) ();
56 #define rx_bitset_subset(N) ((N) / RX_subset_bits)
57 #define rx_bitset_subset_val(B,N) ((B)[rx_bitset_subset(N)])
58 #define RX_bitset_access(B,N,OP) \
59 ((B)[rx_bitset_subset(N)] OP rx_subset_singletons[(N) & RX_subset_mask])
60 #define RX_bitset_member(B,N) RX_bitset_access(B, N, &)
61 #define RX_bitset_enjoin(B,N) RX_bitset_access(B, N, |=)
62 #define RX_bitset_remove(B,N) RX_bitset_access(B, N, &= ~)
63 #define RX_bitset_toggle(B,N) RX_bitset_access(B, N, ^= )
64 #define rx_bitset_numb_subsets(N) (((N) + RX_subset_bits - 1) / RX_subset_bits)
65 #define rx_sizeof_bitset(N) (rx_bitset_numb_subsets(N) * sizeof(RX_subset))
69 /* This page: Splay trees. */
72 typedef int (*rx_sp_comparer) (void * a, void * b);
74 typedef int (*rx_sp_comparer) ();
81 struct rx_sp_node * kids[2];
85 typedef void (*rx_sp_key_data_freer) (struct rx_sp_node *);
87 typedef void (*rx_sp_key_data_freer) ();
91 /* giant inflatable hash trees */
95 struct rx_hash_item * next_same_hash;
96 struct rx_hash * table;
104 struct rx_hash * parent;
106 struct rx_hash * children[13];
107 struct rx_hash_item * buckets [13];
108 int bucket_size [13];
111 struct rx_hash_rules;
114 /* should return like == */
115 typedef int (*rx_hash_eq)(void *, void *);
116 typedef struct rx_hash * (*rx_alloc_hash)(struct rx_hash_rules *);
117 typedef void (*rx_free_hash)(struct rx_hash *,
118 struct rx_hash_rules *);
119 typedef struct rx_hash_item * (*rx_alloc_hash_item)(struct rx_hash_rules *,
121 typedef void (*rx_free_hash_item)(struct rx_hash_item *,
122 struct rx_hash_rules *);
124 typedef int (*rx_hash_eq)();
125 typedef struct rx_hash * (*rx_alloc_hash)();
126 typedef void (*rx_free_hash)();
127 typedef struct rx_hash_item * (*rx_alloc_hash_item)();
128 typedef void (*rx_free_hash_item)();
134 rx_alloc_hash hash_alloc;
135 rx_free_hash free_hash;
136 rx_alloc_hash_item hash_item_alloc;
137 rx_free_hash_item free_hash_item;
141 /* Forward declarations */
156 * pattern - a `regular' expression. The expression
157 * need not be formally regular -- it can contain
158 * constructs that don't correspond to purely regular
162 * string - the string (or strings) being searched or matched.
164 * pattern buffer - a structure of type `struct re_pattern_buffer'
165 * This in turn contains a `struct rx', which holds the
166 * NFA compiled from a pattern, as well as some of the state
167 * of a matcher using the pattern.
169 * NFA - nondeterministic finite automata. Some people
170 * use this term to a member of the class of
171 * regular automata (those corresponding to a regular
172 * language). However, in this code, the meaning is
173 * more general. The automata used by Rx are comperable
174 * in power to what are usually called `push down automata'.
176 * Two NFA are built by rx for every pattern. One is built
177 * by the compiler. The other is built from the first, on
178 * the fly, by the matcher. The latter is called the `superstate
179 * NFA' because its states correspond to sets of states from
180 * the first NFA. (Joe Keane gets credit for the name
185 * side-effect edges - The NFA compiled from a pattern can have three
186 * kinds of edges. Epsilon edges can be taken freely anytime
187 * their source state is reached. Character set edges can be
188 * taken when their source state is reached and when the next
189 * character in the buffer is a member of the set. Side effect
190 * edges imply a transition that can only be taken after the
191 * indicated side effect has been successfully accomplished.
192 * Some examples of side effects are:
194 * Storing the current match position to record the
195 * location of a parentesized subexpression.
197 * Advancing the matcher over N characters if they
198 * match the N characters previously matched by a
199 * parentesized subexpression.
201 * Both of those kinds of edges occur in the NFA generated
202 * by the pattern: \(.\)\1
204 * Epsilon and side effect edges are similar. Unfortunately,
205 * some of the code uses the name `epsilon edge' to mean
206 * both epsilon and side effect edges. For example, the
207 * function has_non_idempotent_epsilon_path computes the existance
208 * of a non-trivial path containing only a mix of epsilon and
209 * side effect edges. In that case `nonidempotent epsilon' is being
210 * used to mean `side effect'.
217 /* LOW LEVEL PATTERN BUFFERS */
219 /* Suppose that from some NFA state, more than one path through
220 * side-effect edges is possible. In what order should the paths
221 * be tried? A function of type rx_se_list_order answers that
222 * question. It compares two lists of side effects, and says
223 * which list comes first.
227 typedef int (*rx_se_list_order) (struct rx *,
229 struct rx_se_list *);
231 typedef int (*rx_se_list_order) ();
236 /* Struct RX holds a compiled regular expression - that is, an nfa
237 * ready to be converted on demand to a more efficient superstate nfa.
238 * This is for the low level interface. The high-level interfaces enclose
239 * this in a `struct re_pattern_buffer'.
243 /* The compiler assigns a unique id to every pattern.
244 * Like sequence numbers in X, there is a subtle bug here
245 * if you use Rx in a system that runs for a long time.
246 * But, because of the way the caches work out, it is almost
247 * impossible to trigger the Rx version of this bug.
249 * The id is used to validate superstates found in a cache
250 * of superstates. It isn't sufficient to let a superstate
251 * point back to the rx for which it was compiled -- the caller
252 * may be re-using a `struct rx' in which case the superstate
253 * is not really valid. So instead, superstates are validated
254 * by checking the sequence number of the pattern for which
259 /* This is memory mgt. state for superstates. This may be
260 * shared by more than one struct rx.
262 struct rx_cache * cache;
264 /* Every regex defines the size of its own character set.
265 * A superstate has an array of this size, with each element
266 * a `struct rx_inx'. So, don't make this number too large.
267 * In particular, don't make it 2^16.
271 /* After the NFA is built, it is copied into a contiguous region
272 * of memory (mostly for compatability with GNU regex).
273 * Here is that region, and it's size:
276 unsigned long allocated;
278 /* Clients of RX can ask for some extra storage in the space pointed
279 * to by BUFFER. The field RESERVED is an input parameter to the
280 * compiler. After compilation, this much space will be available
281 * at (buffer + allocated - reserved)
283 unsigned long reserved;
285 /* --------- The remaining fields are for internal use only. --------- */
286 /* --------- But! they must be initialized to 0. --------- */
288 /* NODEC is the number of nodes in the NFA with non-epsilon
293 /* EPSNODEC is the number of nodes with only epsilon transitions. */
296 /* The sum (NODEC + EPSNODEC) is the total number of states in the
300 /* Lists of side effects as stored in the NFA are `hash consed'..meaning
301 * that lists with the same elements are ==. During compilation,
302 * this table facilitates hash-consing.
304 struct rx_hash se_list_memo;
306 /* Lists of NFA states are also hashed.
308 struct rx_hash set_list_memo;
313 /* The compiler and matcher must build a number of instruction frames.
314 * The format of these frames is fixed (c.f. struct rx_inx). The values
315 * of the instructions is not fixed.
317 * An enumerated type (enum rx_opcode) defines the set of instructions
318 * that the compiler or matcher might generate. When filling an instruction
319 * frame, the INX field is found by indexing this instruction table
322 void ** instruction_table;
324 /* The list of all states in an NFA.
325 * During compilation, the NEXT field of NFA states links this list.
326 * After compilation, all the states are compacted into an array,
327 * ordered by state id numbers. At that time, this points to the base
330 struct rx_nfa_state *nfa_states;
332 /* Every nfa begins with one distinguished starting state:
334 struct rx_nfa_state *start;
336 /* This orders the search through super-nfa paths.
337 * See the comment near the typedef of rx_se_list_order.
339 rx_se_list_order se_list_cmp;
341 struct rx_superset * start_set;
349 /* Compilation is in stages.
351 * In the first stage, a pattern specified by a string is
352 * translated into a syntax tree. Later stages will convert
353 * the syntax tree into an NFA optimized for conversion to a
356 * This page is about syntax trees.
361 r_cset, /* Match from a character set. `a' or `[a-z]'*/
362 r_concat, /* Concat two subexpressions. `ab' */
363 r_alternate, /* Choose one of two subexpressions. `a\|b' */
364 r_opt, /* Optional subexpression. `a?' */
365 r_star, /* Repeated subexpression. `a*' */
368 /* A 2phase-star is a variation on a repeated subexpression.
369 * In this case, there are two subexpressions. The first, if matched,
370 * begins a repitition (otherwise, the whole expression is matches the
373 * After matching the first subexpression, a 2phase star either finishes,
374 * or matches the second subexpression. If the second subexpression is
375 * matched, then the whole construct repeats.
377 * 2phase stars are used in two circumstances. First, they
378 * are used as part of the implementation of POSIX intervals (counted
379 * repititions). Second, they are used to implement proper star
380 * semantics when the repeated subexpression contains paths of
381 * only side effects. See rx_compile for more information.
386 /* c.f. "typedef void * rx_side_effect" */
389 /* This is an extension type: It is for transient use in source->source
390 * transformations (implemented over syntax trees).
395 /* A side effect is a matcher-specific action associated with
396 * transitions in the NFA. The details of side effects are up
397 * to the matcher. To the compiler and superstate constructors
398 * side effects are opaque:
401 typedef void * rx_side_effect;
403 /* Nodes in a syntax tree are of this type:
407 enum rexp_node_type type;
411 rx_side_effect side_effect;
414 struct rexp_node *left;
415 struct rexp_node *right;
425 * A syntax tree is compiled into an NFA. This page defines the structure
431 /* These are kept in a list as the NFA is being built. */
432 struct rx_nfa_state *next;
434 /* After the NFA is built, states are given integer id's.
435 * States whose outgoing transitions are all either epsilon or
436 * side effect edges are given ids less than 0. Other states
437 * are given successive non-negative ids starting from 0.
441 /* The list of NFA edges that go from this state to some other. */
442 struct rx_nfa_edge *edges;
444 /* If you land in this state, then you implicitly land
445 * in all other states reachable by only epsilon translations.
446 * Call the set of maximal paths to such states the epsilon closure
449 * There may be other states that are reachable by a mixture of
450 * epsilon and side effect edges. Consider the set of maximal paths
451 * of that sort from this state. Call it the epsilon-side-effect
452 * closure of the state.
454 * The epsilon closure of the state is a subset of the epsilon-side-
455 * effect closure. It consists of all the paths that contain
456 * no side effects -- only epsilon edges.
458 * The paths in the epsilon-side-effect closure can be partitioned
459 * into equivalance sets. Two paths are equivalant if they have the
460 * same set of side effects, in the same order. The epsilon-closure
461 * is one of these equivalance sets. Let's call these equivalance
462 * sets: observably equivalant path sets. That name is chosen
463 * because equivalance of two paths means they cause the same side
464 * effects -- so they lead to the same subsequent observations other
465 * than that they may wind up in different target states.
467 * The superstate nfa, which is derived from this nfa, is based on
468 * the observation that all of the paths in an observably equivalant
469 * path set can be explored at the same time, provided that the
470 * matcher keeps track not of a single nfa state, but of a set of
471 * states. In particular, after following all the paths in an
472 * observably equivalant set, you wind up at a set of target states.
473 * That set of target states corresponds to one state in the
476 * Staticly, before matching begins, it is convenient to analyze the
477 * nfa. Each state is labeled with a list of the observably
478 * equivalant path sets who's union covers all the
479 * epsilon-side-effect paths beginning in this state. This list is
480 * called the possible futures of the state.
482 * A trivial example is this NFA:
490 * ---------> D ------> E
493 * In this example, A has two possible futures.
494 * One invokes the side effect `s1' and contains two paths,
495 * one ending in state B, the other in state E.
496 * The other invokes the side effect `s2' and contains only
497 * one path, landing in state C.
499 struct rx_possible_future *futures;
502 /* There are exactly two distinguished states in every NFA: */
503 unsigned int is_final:1;
504 unsigned int is_start:1;
506 /* These are used during NFA construction... */
507 unsigned int eclosure_needed:1;
512 /* An edge in an NFA is typed: */
515 /* A cset edge is labled with a set of characters one of which
516 * must be matched for the edge to be taken.
520 /* An epsilon edge is taken whenever its starting state is
525 /* A side effect edge is taken whenever its starting state is
526 * reached. Side effects may cause the match to fail or the
527 * position of the matcher to advance.
529 ne_side_effect /* A special kind of epsilon. */
534 struct rx_nfa_edge *next;
535 enum rx_nfa_etype type;
536 struct rx_nfa_state *dest;
540 rx_side_effect side_effect;
546 /* A possible future consists of a list of side effects
547 * and a set of destination states. Below are their
548 * representations. These structures are hash-consed which
549 * means that lists with the same elements share a representation
550 * (their addresses are ==).
553 struct rx_nfa_state_set
555 struct rx_nfa_state * car;
556 struct rx_nfa_state_set * cdr;
562 struct rx_se_list * cdr;
565 struct rx_possible_future
567 struct rx_possible_future *next;
568 struct rx_se_list * effects;
569 struct rx_nfa_state_set * destset;
574 /* This begins the description of the superstate NFA.
576 * The superstate NFA corresponds to the NFA in these ways:
578 * Every superstate NFA states SUPER correspond to sets of NFA states,
581 * Superstate edges correspond to NFA paths.
583 * The superstate has no epsilon transitions;
584 * every edge has a character label, and a (possibly empty) side
585 * effect label. The side effect label corresponds to a list of
586 * side effects that occur in the NFA. These parts are referred
587 * to as: superedge_character(EDGE) and superedge_sides(EDGE).
589 * For a superstate edge EDGE starting in some superstate SUPER,
590 * the following is true (in pseudo-notation :-):
592 * exists DEST in nfa_states s.t.
593 * exists nfaEDGE in nfa_edges s.t.
594 * origin (nfaEDGE) == DEST
595 * && origin (nfaEDGE) is a member of nfa_states(SUPER)
596 * && exists PF in possible_futures(dest(nfaEDGE)) s.t.
597 * sides_of_possible_future (PF) == superedge_sides (EDGE)
601 * let SUPER2 := superedge_destination(EDGE)
603 * == union of all nfa state sets S s.t.
604 * exists PF in possible_futures(dest(nfaEDGE)) s.t.
605 * sides_of_possible_future (PF) == superedge_sides (EDGE)
606 * && S == dests_of_possible_future (PF) }
608 * Or in english, every superstate is a set of nfa states. A given
609 * character and a superstate implies many transitions in the NFA --
610 * those that begin with an edge labeled with that character from a
611 * state in the set corresponding to the superstate.
613 * The destinations of those transitions each have a set of possible
614 * futures. A possible future is a list of side effects and a set of
615 * destination NFA states. Two sets of possible futures can be
616 * `merged' by combining all pairs of possible futures that have the
617 * same side effects. A pair is combined by creating a new future
618 * with the same side effect but the union of the two destination sets.
619 * In this way, all the possible futures suggested by a superstate
620 * and a character can be merged into a set of possible futures where
621 * no two elements of the set have the same set of side effects.
623 * The destination of a possible future, being a set of NFA states,
624 * corresponds to a supernfa state. So, the merged set of possible
625 * futures we just created can serve as a set of edges in the
628 * The representation of the superstate nfa and the nfa is critical.
629 * The nfa has to be compact, but has to facilitate the rapid
630 * computation of missing superstates. The superstate nfa has to
631 * be fast to interpret, lazilly constructed, and bounded in space.
633 * To facilitate interpretation, the superstate data structures are
634 * peppered with `instruction frames'. There is an instruction set
635 * defined below which matchers using the supernfa must be able to
638 * We'd like to make it possible but not mandatory to use code
639 * addresses to represent instructions (c.f. gcc's computed goto).
640 * Therefore, we define an enumerated type of opcodes, and when
641 * writing one of these instructions into a data structure, use
642 * the opcode as an index into a table of instruction values.
644 * Here are the opcodes that occur in the superstate nfa:
648 /* Every superstate contains a table of instruction frames indexed
649 * by characters. A normal `move' in a matcher is to fetch the next
650 * character and use it as an index into a superstates transition
653 * In the fasted case, only one edge follows from that character.
654 * In other cases there is more work to do.
656 * The descriptions of the opcodes refer to data structures that are
657 * described further below.
663 * BACKTRACK_POINT is invoked when a character transition in
664 * a superstate leads to more than one edge. In that case,
665 * the edges have to be explored independently using a backtracking
668 * A BACKTRACK_POINT instruction is stored in a superstate's
669 * transition table for some character when it is known that that
670 * character crosses more than one edge. On encountering this
671 * instruction, the matcher saves enough state to backtrack to this
672 * point in the match later.
674 rx_backtrack_point = 0, /* data is (struct transition_class *) */
677 * RX_DO_SIDE_EFFECTS evaluates the side effects of an epsilon path.
678 * There is one occurence of this instruction per rx_distinct_future.
679 * This instruction is skipped if a rx_distinct_future has no side effects.
681 rx_do_side_effects = rx_backtrack_point + 1,
683 /* data is (struct rx_distinct_future *) */
686 * RX_CACHE_MISS instructions are stored in rx_distinct_futures whose
687 * destination superstate has been reclaimed (or was never built).
688 * It recomputes the destination superstate.
689 * RX_CACHE_MISS is also stored in a superstate transition table before
690 * any of its edges have been built.
692 rx_cache_miss = rx_do_side_effects + 1,
693 /* data is (struct rx_distinct_future *) */
696 * RX_NEXT_CHAR is called to consume the next character and take the
697 * corresponding transition. This is the only instruction that uses
698 * the DATA field of the instruction frame instead of DATA_2.
699 * (see EXPLORE_FUTURE in regex.c).
701 rx_next_char = rx_cache_miss + 1, /* data is (struct superstate *) */
703 /* RX_BACKTRACK indicates that a transition fails.
705 rx_backtrack = rx_next_char + 1, /* no data */
708 * RX_ERROR_INX is stored only in places that should never be executed.
710 rx_error_inx = rx_backtrack + 1, /* Not supposed to occur. */
712 rx_num_instructions = rx_error_inx + 1
715 /* An id_instruction_table holds the values stored in instruction
716 * frames. The table is indexed by the enums declared above.
718 extern void * rx_id_instruction_table[rx_num_instructions];
720 /* The heart of the matcher is a `word-code-interpreter'
721 * (like a byte-code interpreter, except that instructions
722 * are a full word wide).
724 * Instructions are not stored in a vector of code, instead,
725 * they are scattered throughout the data structures built
726 * by the regexp compiler and the matcher. One word-code instruction,
727 * together with the arguments to that instruction, constitute
728 * an instruction frame (struct rx_inx).
730 * This structure type is padded by hand to a power of 2 because
731 * in one of the dominant cases, we dispatch by indexing a table
732 * of instruction frames. If that indexing can be accomplished
733 * by just a shift of the index, we're happy.
735 * Instructions take at most one argument, but there are two
736 * slots in an instruction frame that might hold that argument.
737 * These are called data and data_2. The data slot is only
738 * used for one instruction (RX_NEXT_CHAR). For all other
739 * instructions, data should be set to 0.
741 * RX_NEXT_CHAR is the most important instruction by far.
742 * By reserving the data field for its exclusive use,
743 * instruction dispatch is sped up in that case. There is
744 * no need to fetch both the instruction and the data,
745 * only the data is needed. In other words, a `cycle' begins
746 * by fetching the field data. If that is non-0, then it must
747 * be the destination state of a next_char transition, so
748 * make that value the current state, advance the match position
749 * by one character, and start a new cycle. On the other hand,
750 * if data is 0, fetch the instruction and do a more complicated
762 #ifndef RX_TAIL_ARRAY
763 #define RX_TAIL_ARRAY 1
766 /* A superstate corresponds to a set of nfa states. Those sets are
767 * represented by STRUCT RX_SUPERSET. The constructors
768 * guarantee that only one (shared) structure is created for a given set.
772 int refs; /* This is a reference counted structure. */
774 /* We keep these sets in a cache because (in an unpredictable way),
775 * the same set is often created again and again. But that is also
776 * problematic -- compatibility with POSIX and GNU regex requires
777 * that we not be able to tell when a program discards a particular
778 * NFA (thus invalidating the supersets created from it).
780 * But when a cache hit appears to occur, we will have in hand the
781 * nfa for which it may have happened. That is why every nfa is given
782 * its own sequence number. On a cache hit, the cache is validated
783 * by comparing the nfa sequence number to this field:
787 struct rx_nfa_state * car; /* May or may not be a valid addr. */
788 struct rx_superset * cdr;
790 /* If the corresponding superstate exists: */
791 struct rx_superstate * superstate;
794 /* There is another bookkeeping problem. It is expensive to
795 * compute the starting nfa state set for an nfa. So, once computed,
796 * it is cached in the `struct rx'.
798 * But, the state set can be flushed from the superstate cache.
799 * When that happens, we can't know if the corresponding `struct rx'
800 * is still alive or if it has been freed or re-used by the program.
801 * So, the cached pointer to this set in a struct rx might be invalid
802 * and we need a way to validate it.
804 * Fortunately, even if this set is flushed from the cache, it is
805 * not freed. It just goes on the free-list of supersets.
806 * So we can still examine it.
808 * So to validate a starting set memo, check to see if the
809 * starts_for field still points back to the struct rx in question,
810 * and if the ID matches the rx sequence number.
812 struct rx * starts_for;
814 /* This is used to link into a hash bucket so these objects can
817 struct rx_hash_item hash_item;
820 #define rx_protect_superset(RX,CON) (++(CON)->refs)
822 /* The terminology may be confusing (rename this structure?).
823 * Every character occurs in at most one rx_super_edge per super-state.
824 * But, that structure might have more than one option, indicating a point
825 * of non-determinism.
827 * In other words, this structure holds a list of superstate edges
828 * sharing a common starting state and character label. The edges
829 * are in the field OPTIONS. All superstate edges sharing the same
830 * starting state and character are in this list.
834 struct rx_super_edge *next;
835 struct rx_inx rx_backtrack_frame;
838 struct rx_distinct_future *options;
841 /* A superstate is a set of nfa states (RX_SUPERSET) along
842 * with a transition table. Superstates are built on demand and reclaimed
843 * without warning. To protect a superstate from this ghastly fate,
844 * use LOCK_SUPERSTATE.
848 int rx_id; /* c.f. the id field of rx_superset */
849 int locks; /* protection from reclamation */
851 /* Within a superstate cache, all the superstates are kept in a big
852 * queue. The tail of the queue is the state most likely to be
853 * reclaimed. The *recyclable fields hold the queue position of
856 struct rx_superstate * next_recyclable;
857 struct rx_superstate * prev_recyclable;
859 /* The supernfa edges that exist in the cache and that have
860 * this state as their destination are kept in this list:
862 struct rx_distinct_future * transition_refs;
864 /* The list of nfa states corresponding to this superstate: */
865 struct rx_superset * contents;
867 /* The list of edges in the cache beginning from this state. */
868 struct rx_super_edge * edges;
870 /* A tail of the recyclable queue is marked as semifree. A semifree
871 * state has no incoming next_char transitions -- any transition
872 * into a semifree state causes a complex dispatch with the side
873 * effect of rescuing the state from its semifree state.
875 * An alternative to this might be to make next_char more expensive,
876 * and to move a state to the head of the recyclable queue whenever
877 * it is entered. That way, popular states would never be recycled.
879 * But unilaterally making next_char more expensive actually loses.
880 * So, incoming transitions are only made expensive for states near
881 * the tail of the recyclable queue. The more cache contention
882 * there is, the more frequently a state will have to prove itself
883 * and be moved back to the front of the queue. If there is less
884 * contention, then popular states just aggregate in the front of
885 * the queue and stay there.
890 /* This keeps track of the size of the transition table for this
891 * state. There is a half-hearted attempt to support variable sized
896 /* Indexed by characters... */
897 struct rx_inx transitions[RX_TAIL_ARRAY];
901 /* A list of distinct futures define the edges that leave from a
902 * given superstate on a given character. c.f. rx_super_edge.
905 struct rx_distinct_future
907 struct rx_distinct_future * next_same_super_edge[2];
908 struct rx_distinct_future * next_same_dest;
909 struct rx_distinct_future * prev_same_dest;
910 struct rx_superstate * present; /* source state */
911 struct rx_superstate * future; /* destination state */
912 struct rx_super_edge * edge;
915 /* The future_frame holds the instruction that should be executed
916 * after all the side effects are done, when it is time to complete
917 * the transition to the next state.
919 * Normally this is a next_char instruction, but it may be a
920 * cache_miss instruction as well, depending on whether or not
921 * the superstate is in the cache and semifree.
923 * If this is the only future for a given superstate/char, and
924 * if there are no side effects to be performed, this frame is
925 * not used (directly) at all. Instead, its contents are copied
926 * into the transition table of the starting state of this dist. future.
928 struct rx_inx future_frame;
930 struct rx_inx side_effects_frame;
931 struct rx_se_list * effects;
934 #define rx_lock_superstate(R,S) ((S)->locks++)
935 #define rx_unlock_superstate(R,S) (--(S)->locks)
938 /* This page destined for rx.h */
942 struct rx_blocklist * next;
948 struct rx_freelist * next;
954 typedef void (*rx_morecore_fn)(struct rx_cache *);
956 typedef void (*rx_morecore_fn)();
959 /* You use this to control the allocation of superstate data
960 * during matching. Most of it should be initialized to 0.
962 * A MORECORE function is necessary. It should allocate
963 * a new block of memory or return 0.
964 * A default that uses malloc is called `rx_morecore'.
966 * The number of SUPERSTATES_ALLOWED indirectly limits how much memory
967 * the system will try to allocate. The default is 128. Batch style
968 * applications that are very regexp intensive should use as high a number
969 * as possible without thrashing.
971 * The LOCAL_CSET_SIZE is the number of characters in a character set.
972 * It is therefore the number of entries in a superstate transition table.
973 * Generally, it should be 256. If your character set has 16 bits,
974 * it is better to translate your regexps into equivalent 8 bit patterns.
979 struct rx_hash_rules superset_hash_rules;
981 /* Objects are allocated by incrementing a pointer that
982 * scans across rx_blocklists.
984 struct rx_blocklist * memory;
985 struct rx_blocklist * memory_pos;
988 rx_morecore_fn morecore;
991 struct rx_freelist * free_superstates;
992 struct rx_freelist * free_transition_classes;
993 struct rx_freelist * free_discernable_futures;
994 struct rx_freelist * free_supersets;
995 struct rx_freelist * free_hash;
997 /* Two sets of superstates -- those that are semifreed, and those
998 * that are being used.
1000 struct rx_superstate * lru_superstate;
1001 struct rx_superstate * semifree_superstate;
1003 struct rx_superset * empty_superset;
1006 int semifree_superstates;
1009 int superstates_allowed;
1011 int local_cset_size;
1012 void ** instruction_table;
1014 struct rx_hash superset_table;
1019 /* The lowest-level search function supports arbitrarily fragmented
1020 * strings and (optionally) suspendable/resumable searches.
1022 * Callers have to provide a few hooks.
1027 #define __const__ const
1033 /* This holds a matcher position */
1034 struct rx_string_position
1036 __const__ unsigned char * pos; /* The current pos. */
1037 __const__ unsigned char * string; /* The current string burst. */
1038 __const__ unsigned char * end; /* First invalid position >= POS. */
1039 int offset; /* Integer address of the current burst. */
1040 int size; /* Current string's size. */
1041 int search_direction; /* 1 or -1 */
1042 int search_end; /* First position to not try. */
1046 enum rx_get_burst_return
1048 rx_get_burst_continuation,
1051 rx_get_burst_no_more
1055 /* A call to get burst should make POS valid. It might be invalid
1056 * if the STRING field doesn't point to a burst that actually
1059 * GET_BURST should take a clue from SEARCH_DIRECTION (1 or -1) as to
1060 * whether or not to pad to the left. Padding to the right is always
1061 * appropriate, but need not go past the point indicated by STOP.
1063 * If a continuation is returned, then the reentering call to
1064 * a search function will retry the get_burst.
1068 typedef enum rx_get_burst_return
1069 (*rx_get_burst_fn) (struct rx_string_position * pos,
1074 typedef enum rx_get_burst_return (*rx_get_burst_fn) ();
1078 enum rx_back_check_return
1080 rx_back_check_continuation,
1081 rx_back_check_error,
1086 /* Back_check should advance the position it is passed
1087 * over rparen - lparen characters and return pass iff
1088 * the characters starting at POS match those indexed
1089 * by [LPAREN..RPAREN].
1091 * If a continuation is returned, then the reentering call to
1092 * a search function will retry the back_check.
1096 typedef enum rx_back_check_return
1097 (*rx_back_check_fn) (struct rx_string_position * pos,
1100 unsigned char * translate,
1105 typedef enum rx_back_check_return (*rx_back_check_fn) ();
1111 /* A call to fetch_char should return the character at POS or POS + 1.
1112 * Returning continuations here isn't supported. OFFSET is either 0 or 1
1113 * and indicates which characters is desired.
1117 typedef int (*rx_fetch_char_fn) (struct rx_string_position * pos,
1122 typedef int (*rx_fetch_char_fn) ();
1126 enum rx_search_return
1128 rx_search_continuation = -4,
1129 rx_search_error = -3,
1130 rx_search_soft_fail = -2, /* failed by running out of string */
1131 rx_search_fail = -1 /* failed only by reaching failure states */
1132 /* return values >= 0 indicate the position of a successful match */
1142 * The remaining declarations replace regex.h.
1145 /* This is an array of error messages corresponding to the error codes.
1147 extern __const__ char *re_error_msg[];
1149 /* If any error codes are removed, changed, or added, update the
1150 `re_error_msg' table in regex.c. */
1153 REG_NOERROR = 0, /* Success. */
1154 REG_NOMATCH, /* Didn't find a match (for regexec). */
1156 /* POSIX regcomp return error codes. (In the order listed in the
1158 REG_BADPAT, /* Invalid pattern. */
1159 REG_ECOLLATE, /* Not implemented. */
1160 REG_ECTYPE, /* Invalid character class name. */
1161 REG_EESCAPE, /* Trailing backslash. */
1162 REG_ESUBREG, /* Invalid back reference. */
1163 REG_EBRACK, /* Unmatched left bracket. */
1164 REG_EPAREN, /* Parenthesis imbalance. */
1165 REG_EBRACE, /* Unmatched \{. */
1166 REG_BADBR, /* Invalid contents of \{\}. */
1167 REG_ERANGE, /* Invalid range end. */
1168 REG_ESPACE, /* Ran out of memory. */
1169 REG_BADRPT, /* No preceding re for repetition op. */
1171 /* Error codes we've added. */
1172 REG_EEND, /* Premature end. */
1173 REG_ESIZE, /* Compiled pattern bigger than 2^16 bytes. */
1174 REG_ERPAREN /* Unmatched ) or \); not returned from regcomp. */
1177 /* The regex.c support, as a client of rx, defines a set of possible
1178 * side effects that can be added to the edge lables of nfa edges.
1179 * Here is the list of sidef effects in use.
1182 enum re_side_effects
1184 #define RX_WANT_SE_DEFS 1
1186 #undef RX_DEF_CPLX_SE
1187 #define RX_DEF_SE(IDEM, NAME, VALUE) NAME VALUE,
1188 #define RX_DEF_CPLX_SE(IDEM, NAME, VALUE) NAME VALUE,
1191 #undef RX_DEF_CPLX_SE
1192 #undef RX_WANT_SE_DEFS
1193 re_floogle_flap = 65533
1196 /* These hold paramaters for the kinds of side effects that are possible
1197 * in the supported pattern languages. These include things like the
1198 * numeric bounds of {} operators and the index of paren registers for
1199 * subexpression measurement or backreferencing.
1203 enum re_side_effects se;
1208 typedef unsigned reg_syntax_t;
1210 struct re_pattern_buffer
1213 reg_syntax_t syntax; /* See below for syntax bit definitions. */
1215 unsigned int no_sub:1; /* If set, don't return register offsets. */
1216 unsigned int not_bol:1; /* If set, the anchors ('^' and '$') don't */
1217 unsigned int not_eol:1; /* match at the ends of the string. */
1218 unsigned int newline_anchor:1;/* If true, an anchor at a newline matches.*/
1219 unsigned int least_subs:1; /* If set, and returning registers, return
1220 * as few values as possible. Only
1221 * backreferenced groups and group 0 (the whole
1222 * match) will be returned.
1225 /* If true, this says that the matcher should keep registers on its
1226 * backtracking stack. For many patterns, we can easily determine that
1227 * this isn't necessary.
1229 unsigned int match_regs_on_stack:1;
1230 unsigned int search_regs_on_stack:1;
1232 /* is_anchored and begbuf_only are filled in by rx_compile. */
1233 unsigned int is_anchored:1; /* Anchorded by ^? */
1234 unsigned int begbuf_only:1; /* Anchored to char position 0? */
1237 /* If REGS_UNALLOCATED, allocate space in the `regs' structure
1238 * for `max (RE_NREGS, re_nsub + 1)' groups.
1239 * If REGS_REALLOCATE, reallocate space if necessary.
1240 * If REGS_FIXED, use what's there.
1242 #define REGS_UNALLOCATED 0
1243 #define REGS_REALLOCATE 1
1244 #define REGS_FIXED 2
1245 unsigned int regs_allocated:2;
1248 /* Either a translate table to apply to all characters before
1249 * comparing them, or zero for no translation. The translation
1250 * is applied to a pattern when it is compiled and to a string
1251 * when it is matched.
1253 unsigned char * translate;
1255 /* If this is a valid pointer, it tells rx not to store the extents of
1256 * certain subexpressions (those corresponding to non-zero entries).
1257 * Passing 0x1 is the same as passing an array of all ones. Passing 0x0
1258 * is the same as passing an array of all zeros.
1259 * The array should contain as many entries as their are subexps in the
1262 * For POSIX compatability, when using regcomp and regexec this field
1263 * is zeroed and ignored.
1265 char * syntax_parens;
1267 /* Number of subexpressions found by the compiler. */
1270 void * buffer; /* Malloced memory for the nfa. */
1271 unsigned long allocated; /* Size of that memory. */
1273 /* Pointer to a fastmap, if any, otherwise zero. re_search uses
1274 * the fastmap, if there is one, to skip over impossible
1275 * starting points for matches. */
1278 unsigned int fastmap_accurate:1; /* These three are internal. */
1279 unsigned int can_match_empty:1;
1280 struct rx_nfa_state * start; /* The nfa starting state. */
1282 /* This is the list of iterator bounds for {lo,hi} constructs.
1283 * The memory pointed to is part of the rx->buffer.
1285 struct re_se_params *se_params;
1287 /* This is a bitset representation of the fastmap.
1288 * This is a true fastmap that already takes the translate
1289 * table into account.
1294 /* Type for byte offsets within the string. POSIX mandates this. */
1295 typedef int regoff_t;
1297 /* This is the structure we store register match data in. See
1298 regex.texinfo for a full description of what registers match. */
1306 typedef struct re_pattern_buffer regex_t;
1308 /* POSIX specification for registers. Aside from the different names than
1309 `re_registers', POSIX uses an array of structures, instead of a
1310 structure of arrays. */
1313 regoff_t rm_so; /* Byte offset from string's start to substring's start. */
1314 regoff_t rm_eo; /* Byte offset from string's start to substring's end. */
1318 /* The following bits are used to determine the regexp syntax we
1319 recognize. The set/not-set meanings are chosen so that Emacs syntax
1320 remains the value 0. The bits are given in alphabetical order, and
1321 the definitions shifted by one from the previous bit; thus, when we
1322 add or remove a bit, only one other definition need change. */
1324 /* If this bit is not set, then \ inside a bracket expression is literal.
1325 If set, then such a \ quotes the following character. */
1326 #define RE_BACKSLASH_ESCAPE_IN_LISTS (1)
1328 /* If this bit is not set, then + and ? are operators, and \+ and \? are
1330 If set, then \+ and \? are operators and + and ? are literals. */
1331 #define RE_BK_PLUS_QM (RE_BACKSLASH_ESCAPE_IN_LISTS << 1)
1333 /* If this bit is set, then character classes are supported. They are:
1334 [:alpha:], [:upper:], [:lower:], [:digit:], [:alnum:], [:xdigit:],
1335 [:space:], [:print:], [:punct:], [:graph:], and [:cntrl:].
1336 If not set, then character classes are not supported. */
1337 #define RE_CHAR_CLASSES (RE_BK_PLUS_QM << 1)
1339 /* If this bit is set, then ^ and $ are always anchors (outside bracket
1340 expressions, of course).
1341 If this bit is not set, then it depends:
1342 ^ is an anchor if it is at the beginning of a regular
1343 expression or after an open-group or an alternation operator;
1344 $ is an anchor if it is at the end of a regular expression, or
1345 before a close-group or an alternation operator.
1347 This bit could be (re)combined with RE_CONTEXT_INDEP_OPS, because
1348 POSIX draft 11.2 says that * etc. in leading positions is undefined.
1349 We already implemented a previous draft which made those constructs
1350 invalid, though, so we haven't changed the code back. */
1351 #define RE_CONTEXT_INDEP_ANCHORS (RE_CHAR_CLASSES << 1)
1353 /* If this bit is set, then special characters are always special
1354 regardless of where they are in the pattern.
1355 If this bit is not set, then special characters are special only in
1356 some contexts; otherwise they are ordinary. Specifically,
1357 * + ? and intervals are only special when not after the beginning,
1358 open-group, or alternation operator. */
1359 #define RE_CONTEXT_INDEP_OPS (RE_CONTEXT_INDEP_ANCHORS << 1)
1361 /* If this bit is set, then *, +, ?, and { cannot be first in an re or
1362 immediately after an alternation or begin-group operator. */
1363 #define RE_CONTEXT_INVALID_OPS (RE_CONTEXT_INDEP_OPS << 1)
1365 /* If this bit is set, then . matches newline.
1366 If not set, then it doesn't. */
1367 #define RE_DOT_NEWLINE (RE_CONTEXT_INVALID_OPS << 1)
1369 /* If this bit is set, then . doesn't match NUL.
1370 If not set, then it does. */
1371 #define RE_DOT_NOT_NULL (RE_DOT_NEWLINE << 1)
1373 /* If this bit is set, nonmatching lists [^...] do not match newline.
1374 If not set, they do. */
1375 #define RE_HAT_LISTS_NOT_NEWLINE (RE_DOT_NOT_NULL << 1)
1377 /* If this bit is set, either \{...\} or {...} defines an
1378 interval, depending on RE_NO_BK_BRACES.
1379 If not set, \{, \}, {, and } are literals. */
1380 #define RE_INTERVALS (RE_HAT_LISTS_NOT_NEWLINE << 1)
1382 /* If this bit is set, +, ? and | aren't recognized as operators.
1383 If not set, they are. */
1384 #define RE_LIMITED_OPS (RE_INTERVALS << 1)
1386 /* If this bit is set, newline is an alternation operator.
1387 If not set, newline is literal. */
1388 #define RE_NEWLINE_ALT (RE_LIMITED_OPS << 1)
1390 /* If this bit is set, then `{...}' defines an interval, and \{ and \}
1392 If not set, then `\{...\}' defines an interval. */
1393 #define RE_NO_BK_BRACES (RE_NEWLINE_ALT << 1)
1395 /* If this bit is set, (...) defines a group, and \( and \) are literals.
1396 If not set, \(...\) defines a group, and ( and ) are literals. */
1397 #define RE_NO_BK_PARENS (RE_NO_BK_BRACES << 1)
1399 /* If this bit is set, then \<digit> matches <digit>.
1400 If not set, then \<digit> is a back-reference. */
1401 #define RE_NO_BK_REFS (RE_NO_BK_PARENS << 1)
1403 /* If this bit is set, then | is an alternation operator, and \| is literal.
1404 If not set, then \| is an alternation operator, and | is literal. */
1405 #define RE_NO_BK_VBAR (RE_NO_BK_REFS << 1)
1407 /* If this bit is set, then an ending range point collating higher
1408 than the starting range point, as in [z-a], is invalid.
1409 If not set, then when ending range point collates higher than the
1410 starting range point, the range is ignored. */
1411 #define RE_NO_EMPTY_RANGES (RE_NO_BK_VBAR << 1)
1413 /* If this bit is set, then an unmatched ) is ordinary.
1414 If not set, then an unmatched ) is invalid. */
1415 #define RE_UNMATCHED_RIGHT_PAREN_ORD (RE_NO_EMPTY_RANGES << 1)
1417 /* If this bit is set, do not process the GNU regex operators.
1418 IF not set, then the GNU regex operators are recognized. */
1419 #define RE_NO_GNU_OPS (RE_UNMATCHED_RIGHT_PAREN_ORD << 1)
1421 /* This global variable defines the particular regexp syntax to use (for
1422 some interfaces). When a regexp is compiled, the syntax used is
1423 stored in the pattern buffer, so changing this does not affect
1424 already-compiled regexps. */
1425 extern reg_syntax_t re_syntax_options;
1427 /* Define combinations of the above bits for the standard possibilities.
1428 (The [[[ comments delimit what gets put into the Texinfo file, so
1429 don't delete them!) */
1430 /* [[[begin syntaxes]]] */
1431 #define RE_SYNTAX_EMACS 0
1433 #define RE_SYNTAX_AWK \
1434 (RE_BACKSLASH_ESCAPE_IN_LISTS | RE_DOT_NOT_NULL \
1435 | RE_NO_BK_PARENS | RE_NO_BK_REFS \
1436 | RE_NO_BK_VBAR | RE_NO_EMPTY_RANGES \
1438 | RE_UNMATCHED_RIGHT_PAREN_ORD | RE_NO_GNU_OPS)
1440 #define RE_SYNTAX_GNU_AWK \
1441 ((RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS) \
1442 & ~(RE_DOT_NOT_NULL|RE_INTERVALS))
1444 #define RE_SYNTAX_POSIX_AWK \
1445 (RE_SYNTAX_POSIX_EXTENDED | RE_BACKSLASH_ESCAPE_IN_LISTS | RE_NO_GNU_OPS)
1447 #define RE_SYNTAX_GREP \
1448 (RE_BK_PLUS_QM | RE_CHAR_CLASSES \
1449 | RE_HAT_LISTS_NOT_NEWLINE | RE_INTERVALS \
1452 #define RE_SYNTAX_EGREP \
1453 (RE_CHAR_CLASSES | RE_CONTEXT_INDEP_ANCHORS \
1454 | RE_CONTEXT_INDEP_OPS | RE_HAT_LISTS_NOT_NEWLINE \
1455 | RE_NEWLINE_ALT | RE_NO_BK_PARENS \
1458 #define RE_SYNTAX_POSIX_EGREP \
1459 (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
1461 /* P1003.2/D11.2, section 4.20.7.1, lines 5078ff. */
1462 #define RE_SYNTAX_ED RE_SYNTAX_POSIX_BASIC
1464 #define RE_SYNTAX_SED RE_SYNTAX_POSIX_BASIC
1466 /* Syntax bits common to both basic and extended POSIX regex syntax. */
1467 #define _RE_SYNTAX_POSIX_COMMON \
1468 (RE_CHAR_CLASSES | RE_DOT_NEWLINE | RE_DOT_NOT_NULL \
1469 | RE_INTERVALS | RE_NO_EMPTY_RANGES)
1471 #define RE_SYNTAX_POSIX_BASIC \
1472 (_RE_SYNTAX_POSIX_COMMON | RE_BK_PLUS_QM)
1474 /* Differs from ..._POSIX_BASIC only in that RE_BK_PLUS_QM becomes
1475 RE_LIMITED_OPS, i.e., \? \+ \| are not recognized. Actually, this
1476 isn't minimal, since other operators, such as \`, aren't disabled. */
1477 #define RE_SYNTAX_POSIX_MINIMAL_BASIC \
1478 (_RE_SYNTAX_POSIX_COMMON | RE_LIMITED_OPS)
1480 #define RE_SYNTAX_POSIX_EXTENDED \
1481 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
1482 | RE_CONTEXT_INDEP_OPS | RE_NO_BK_BRACES \
1483 | RE_NO_BK_PARENS | RE_NO_BK_VBAR \
1484 | RE_UNMATCHED_RIGHT_PAREN_ORD)
1486 /* Differs from ..._POSIX_EXTENDED in that RE_CONTEXT_INVALID_OPS
1487 replaces RE_CONTEXT_INDEP_OPS and RE_NO_BK_REFS is added. */
1488 #define RE_SYNTAX_POSIX_MINIMAL_EXTENDED \
1489 (_RE_SYNTAX_POSIX_COMMON | RE_CONTEXT_INDEP_ANCHORS \
1490 | RE_CONTEXT_INVALID_OPS | RE_NO_BK_BRACES \
1491 | RE_NO_BK_PARENS | RE_NO_BK_REFS \
1492 | RE_NO_BK_VBAR | RE_UNMATCHED_RIGHT_PAREN_ORD)
1493 /* [[[end syntaxes]]] */
1495 /* Maximum number of duplicates an interval can allow. Some systems
1496 (erroneously) define this in other header files, but we want our
1497 value, so remove any previous define. */
1501 /* if sizeof(int) == 2, then ((1 << 15) - 1) overflows */
1502 #define RE_DUP_MAX (0x7fff)
1505 /* POSIX `cflags' bits (i.e., information for `regcomp'). */
1507 /* If this bit is set, then use extended regular expression syntax.
1508 If not set, then use basic regular expression syntax. */
1509 #define REG_EXTENDED 1
1511 /* If this bit is set, then ignore case when matching.
1512 If not set, then case is significant. */
1513 #define REG_ICASE (REG_EXTENDED << 1)
1515 /* If this bit is set, then anchors do not match at newline
1516 characters in the string.
1517 If not set, then anchors do match at newlines. */
1518 #define REG_NEWLINE (REG_ICASE << 1)
1520 /* If this bit is set, then report only success or fail in regexec.
1521 If not set, then returns differ between not matching and errors. */
1522 #define REG_NOSUB (REG_NEWLINE << 1)
1525 /* POSIX `eflags' bits (i.e., information for regexec). */
1527 /* If this bit is set, then the beginning-of-line operator doesn't match
1528 the beginning of the string (presumably because it's not the
1529 beginning of a line).
1530 If not set, then the beginning-of-line operator does match the
1531 beginning of the string. */
1532 #define REG_NOTBOL 1
1534 /* Like REG_NOTBOL, except for the end-of-line. */
1535 #define REG_NOTEOL (1 << 1)
1537 /* If `regs_allocated' is REGS_UNALLOCATED in the pattern buffer,
1538 * `re_match_2' returns information about at least this many registers
1539 * the first time a `regs' structure is passed.
1541 * Also, this is the greatest number of backreferenced subexpressions
1542 * allowed in a pattern being matched without caller-supplied registers.
1548 extern int rx_cache_bound;
1549 extern char rx_version_string[];
1553 #ifdef RX_WANT_RX_DEFS
1555 /* This is decls to the interesting subsystems and lower layers
1556 * of rx. Everything which doesn't have a public counterpart in
1557 * regex.c is declared here.
1562 typedef void (*rx_hash_freefn) (struct rx_hash_item * it);
1563 #else /* ndef __STDC__ */
1564 typedef void (*rx_hash_freefn) ();
1565 #endif /* ndef __STDC__ */
1571 RX_DECL int rx_bitset_is_equal (int size, rx_Bitset a, rx_Bitset b);
1572 RX_DECL int rx_bitset_is_subset (int size, rx_Bitset a, rx_Bitset b);
1573 RX_DECL int rx_bitset_empty (int size, rx_Bitset set);
1574 RX_DECL void rx_bitset_null (int size, rx_Bitset b);
1575 RX_DECL void rx_bitset_universe (int size, rx_Bitset b);
1576 RX_DECL void rx_bitset_complement (int size, rx_Bitset b);
1577 RX_DECL void rx_bitset_assign (int size, rx_Bitset a, rx_Bitset b);
1578 RX_DECL void rx_bitset_union (int size, rx_Bitset a, rx_Bitset b);
1579 RX_DECL void rx_bitset_intersection (int size,
1580 rx_Bitset a, rx_Bitset b);
1581 RX_DECL void rx_bitset_difference (int size, rx_Bitset a, rx_Bitset b);
1582 RX_DECL void rx_bitset_revdifference (int size,
1583 rx_Bitset a, rx_Bitset b);
1584 RX_DECL void rx_bitset_xor (int size, rx_Bitset a, rx_Bitset b);
1585 RX_DECL unsigned long rx_bitset_hash (int size, rx_Bitset b);
1586 RX_DECL struct rx_hash_item * rx_hash_find (struct rx_hash * table,
1589 struct rx_hash_rules * rules);
1590 RX_DECL struct rx_hash_item * rx_hash_store (struct rx_hash * table,
1593 struct rx_hash_rules * rules);
1594 RX_DECL void rx_hash_free (struct rx_hash_item * it, struct rx_hash_rules * rules);
1595 RX_DECL void rx_free_hash_table (struct rx_hash * tab, rx_hash_freefn freefn,
1596 struct rx_hash_rules * rules);
1597 RX_DECL rx_Bitset rx_cset (struct rx *rx);
1598 RX_DECL rx_Bitset rx_copy_cset (struct rx *rx, rx_Bitset a);
1599 RX_DECL void rx_free_cset (struct rx * rx, rx_Bitset c);
1600 RX_DECL struct rexp_node * rexp_node (struct rx *rx,
1601 enum rexp_node_type type);
1602 RX_DECL struct rexp_node * rx_mk_r_cset (struct rx * rx,
1604 RX_DECL struct rexp_node * rx_mk_r_concat (struct rx * rx,
1605 struct rexp_node * a,
1606 struct rexp_node * b);
1607 RX_DECL struct rexp_node * rx_mk_r_alternate (struct rx * rx,
1608 struct rexp_node * a,
1609 struct rexp_node * b);
1610 RX_DECL struct rexp_node * rx_mk_r_opt (struct rx * rx,
1611 struct rexp_node * a);
1612 RX_DECL struct rexp_node * rx_mk_r_star (struct rx * rx,
1613 struct rexp_node * a);
1614 RX_DECL struct rexp_node * rx_mk_r_2phase_star (struct rx * rx,
1615 struct rexp_node * a,
1616 struct rexp_node * b);
1617 RX_DECL struct rexp_node * rx_mk_r_side_effect (struct rx * rx,
1619 RX_DECL struct rexp_node * rx_mk_r_data (struct rx * rx,
1621 RX_DECL void rx_free_rexp (struct rx * rx, struct rexp_node * node);
1622 RX_DECL struct rexp_node * rx_copy_rexp (struct rx *rx,
1623 struct rexp_node *node);
1624 RX_DECL struct rx_nfa_state * rx_nfa_state (struct rx *rx);
1625 RX_DECL void rx_free_nfa_state (struct rx_nfa_state * n);
1626 RX_DECL struct rx_nfa_state * rx_id_to_nfa_state (struct rx * rx,
1628 RX_DECL struct rx_nfa_edge * rx_nfa_edge (struct rx *rx,
1629 enum rx_nfa_etype type,
1630 struct rx_nfa_state *start,
1631 struct rx_nfa_state *dest);
1632 RX_DECL void rx_free_nfa_edge (struct rx_nfa_edge * e);
1633 RX_DECL void rx_free_nfa (struct rx *rx);
1634 RX_DECL int rx_build_nfa (struct rx *rx,
1635 struct rexp_node *rexp,
1636 struct rx_nfa_state **start,
1637 struct rx_nfa_state **end);
1638 RX_DECL void rx_name_nfa_states (struct rx *rx);
1639 RX_DECL int rx_eclose_nfa (struct rx *rx);
1640 RX_DECL void rx_delete_epsilon_transitions (struct rx *rx);
1641 RX_DECL int rx_compactify_nfa (struct rx *rx,
1642 void **mem, unsigned long *size);
1643 RX_DECL void rx_release_superset (struct rx *rx,
1644 struct rx_superset *set);
1645 RX_DECL struct rx_superset * rx_superset_cons (struct rx * rx,
1646 struct rx_nfa_state *car, struct rx_superset *cdr);
1647 RX_DECL struct rx_superset * rx_superstate_eclosure_union
1648 (struct rx * rx, struct rx_superset *set, struct rx_nfa_state_set *ecl);
1649 RX_DECL struct rx_superstate * rx_superstate (struct rx *rx,
1650 struct rx_superset *set);
1651 RX_DECL struct rx_inx * rx_handle_cache_miss
1652 (struct rx *rx, struct rx_superstate *super, unsigned char chr, void *data);
1653 RX_DECL reg_errcode_t rx_compile (__const__ char *pattern, int size,
1654 reg_syntax_t syntax,
1655 struct re_pattern_buffer * rxb);
1656 RX_DECL void rx_blow_up_fastmap (struct re_pattern_buffer * rxb);
1658 RX_DECL int rx_bitset_is_equal ();
1659 RX_DECL int rx_bitset_is_subset ();
1660 RX_DECL int rx_bitset_empty ();
1661 RX_DECL void rx_bitset_null ();
1662 RX_DECL void rx_bitset_universe ();
1663 RX_DECL void rx_bitset_complement ();
1664 RX_DECL void rx_bitset_assign ();
1665 RX_DECL void rx_bitset_union ();
1666 RX_DECL void rx_bitset_intersection ();
1667 RX_DECL void rx_bitset_difference ();
1668 RX_DECL void rx_bitset_revdifference ();
1669 RX_DECL void rx_bitset_xor ();
1670 RX_DECL unsigned long rx_bitset_hash ();
1671 RX_DECL struct rx_hash_item * rx_hash_find ();
1672 RX_DECL struct rx_hash_item * rx_hash_store ();
1673 RX_DECL void rx_hash_free ();
1674 RX_DECL void rx_free_hash_table ();
1675 RX_DECL rx_Bitset rx_cset ();
1676 RX_DECL rx_Bitset rx_copy_cset ();
1677 RX_DECL void rx_free_cset ();
1678 RX_DECL struct rexp_node * rexp_node ();
1679 RX_DECL struct rexp_node * rx_mk_r_cset ();
1680 RX_DECL struct rexp_node * rx_mk_r_concat ();
1681 RX_DECL struct rexp_node * rx_mk_r_alternate ();
1682 RX_DECL struct rexp_node * rx_mk_r_opt ();
1683 RX_DECL struct rexp_node * rx_mk_r_star ();
1684 RX_DECL struct rexp_node * rx_mk_r_2phase_star ();
1685 RX_DECL struct rexp_node * rx_mk_r_side_effect ();
1686 RX_DECL struct rexp_node * rx_mk_r_data ();
1687 RX_DECL void rx_free_rexp ();
1688 RX_DECL struct rexp_node * rx_copy_rexp ();
1689 RX_DECL struct rx_nfa_state * rx_nfa_state ();
1690 RX_DECL void rx_free_nfa_state ();
1691 RX_DECL struct rx_nfa_state * rx_id_to_nfa_state ();
1692 RX_DECL struct rx_nfa_edge * rx_nfa_edge ();
1693 RX_DECL void rx_free_nfa_edge ();
1694 RX_DECL void rx_free_nfa ();
1695 RX_DECL int rx_build_nfa ();
1696 RX_DECL void rx_name_nfa_states ();
1697 RX_DECL int rx_eclose_nfa ();
1698 RX_DECL void rx_delete_epsilon_transitions ();
1699 RX_DECL int rx_compactify_nfa ();
1700 RX_DECL void rx_release_superset ();
1701 RX_DECL struct rx_superset * rx_superset_cons ();
1702 RX_DECL struct rx_superset * rx_superstate_eclosure_union ();
1703 RX_DECL struct rx_superstate * rx_superstate ();
1704 RX_DECL struct rx_inx * rx_handle_cache_miss ();
1705 RX_DECL reg_errcode_t rx_compile ();
1706 RX_DECL void rx_blow_up_fastmap ();
1710 #endif /* RX_WANT_RX_DEFS */
1715 extern int re_search_2 (struct re_pattern_buffer *rxb,
1716 __const__ char * string1, int size1,
1717 __const__ char * string2, int size2,
1718 int startpos, int range,
1719 struct re_registers *regs,
1721 extern int re_search (struct re_pattern_buffer * rxb, __const__ char *string,
1722 int size, int startpos, int range,
1723 struct re_registers *regs);
1724 extern int re_match_2 (struct re_pattern_buffer * rxb,
1725 __const__ char * string1, int size1,
1726 __const__ char * string2, int size2,
1727 int pos, struct re_registers *regs, int stop);
1728 extern int re_match (struct re_pattern_buffer * rxb,
1729 __const__ char * string,
1731 struct re_registers *regs);
1732 extern reg_syntax_t re_set_syntax (reg_syntax_t syntax);
1733 extern void re_set_registers (struct re_pattern_buffer *bufp,
1734 struct re_registers *regs,
1736 regoff_t * starts, regoff_t * ends);
1737 extern __const__ char * re_compile_pattern (__const__ char *pattern,
1739 struct re_pattern_buffer * rxb);
1740 extern int re_compile_fastmap (struct re_pattern_buffer * rxb);
1741 extern char * re_comp (__const__ char *s);
1742 extern int re_exec (__const__ char *s);
1743 extern int regcomp (regex_t * preg, __const__ char * pattern, int cflags);
1744 extern int regexec (__const__ regex_t *preg, __const__ char *string,
1745 size_t nmatch, regmatch_t pmatch[],
1747 extern size_t regerror (int errcode, __const__ regex_t *preg,
1748 char *errbuf, size_t errbuf_size);
1749 extern void regfree (regex_t *preg);
1752 extern int re_search_2 ();
1753 extern int re_search ();
1754 extern int re_match_2 ();
1755 extern int re_match ();
1756 extern reg_syntax_t re_set_syntax ();
1757 extern void re_set_registers ();
1758 extern __const__ char * re_compile_pattern ();
1759 extern int re_compile_fastmap ();
1760 extern char * re_comp ();
1761 extern int re_exec ();
1762 extern int regcomp ();
1763 extern int regexec ();
1764 extern size_t regerror ();
1765 extern void regfree ();
1771 #ifdef RX_WANT_RX_DEFS
1773 struct rx_counter_frame
1777 struct rx_counter_frame * inherited_from; /* If this is a copy. */
1778 struct rx_counter_frame * cdr;
1781 struct rx_backtrack_frame
1783 char * counter_stack_sp;
1785 /* A frame is used to save the matchers state when it crosses a
1786 * backtracking point. The `stk_' fields correspond to variables
1787 * in re_search_2 (just strip off thes `stk_'). They are documented
1790 struct rx_superstate * stk_super;
1792 struct rx_string_position stk_test_pos;
1797 /* This is the list of options left to explore at the backtrack
1798 * point for which this frame was created.
1800 struct rx_distinct_future * df;
1801 struct rx_distinct_future * first_df;
1808 struct rx_stack_chunk
1810 struct rx_stack_chunk * next_chunk;
1820 rx_outer_restore_pos
1823 enum rx_fastmap_return
1825 rx_fastmap_continuation,
1831 enum rx_fastmap_entry
1834 rx_fastmap_string_break
1839 rx_test_continuation,
1845 enum rx_test_internal_return
1847 rx_test_internal_error,
1848 rx_test_found_first,
1849 rx_test_line_finished
1852 enum rx_test_match_entry
1855 rx_test_cache_hit_loop,
1856 rx_test_backreference_check,
1857 rx_test_backtrack_return
1860 struct rx_search_state
1862 /* Two groups of registers are kept. The group with the register state
1863 * of the current test match, and the group that holds the state at the end
1864 * of the best known match, if any.
1866 * For some patterns, there may also be registers saved on the stack.
1868 unsigned num_regs; /* Includes an element for register zero. */
1869 regoff_t * lparen; /* scratch space for register returns */
1871 regoff_t * best_lpspace; /* in case the user doesn't want these */
1872 regoff_t * best_rpspace; /* values, we still need space to store
1873 * them. Normally, this memoryis unused
1874 * and the space pointed to by REGS is
1878 int last_l; /* Highest index of a valid lparen. */
1879 int last_r; /* It's dual. */
1881 int * best_lparen; /* This contains the best known register */
1882 int * best_rparen; /* assignments.
1883 * This may point to the same mem as
1884 * best_lpspace, or it might point to memory
1885 * passed by the caller.
1887 int best_last_l; /* best_last_l:best_lparen::last_l:lparen */
1891 unsigned char * translate;
1893 struct rx_string_position outer_pos;
1895 struct rx_superstate * start_super;
1897 int first_found; /* If true, return after finding any match. */
1900 /* For continuations... */
1901 enum rx_outer_entry outer_search_resume_pt;
1902 struct re_pattern_buffer * saved_rxb;
1906 int saved_total_size;
1907 rx_get_burst_fn saved_get_burst;
1908 rx_back_check_fn saved_back_check;
1909 struct re_registers * saved_regs;
1912 ** state for fastmap
1918 /* for continuations in the fastmap procedure: */
1919 enum rx_fastmap_entry fastmap_resume_pt;
1922 ** state for test_match
1925 /* The current superNFA position of the matcher. */
1926 struct rx_superstate * super;
1928 /* The matcher interprets a series of instruction frames.
1929 * This is the `instruction counter' for the interpretation.
1931 struct rx_inx * ifr;
1933 /* We insert a ghost character in the string to prime
1934 * the nfa. test_pos.pos, test_pos.str_half, and test_pos.end_half
1935 * keep track of the test-match position and string-half.
1939 /* Position within the string. */
1940 struct rx_string_position test_pos;
1942 struct rx_stack_chunk * counter_stack;
1943 struct rx_stack_chunk * backtrack_stack;
1944 int backtrack_frame_bytes;
1946 struct rx_stack_chunk * free_chunks;
1948 /* To return from this function, set test_ret and
1949 * `goto test_do_return'.
1951 * Possible return values are:
1952 * 1 --- end of string while the superNFA is still going
1953 * 0 --- internal error (out of memory)
1954 * -1 --- search completed by reaching the superNFA fail state
1955 * -2 --- a match was found, maybe not the longest.
1957 * When the search is complete (-1), best_last_r indicates whether
1958 * a match was found.
1960 * -2 is return only if search_state.first_found is non-zero.
1962 * if search_state.first_found is non-zero, a return of -1 indicates no match,
1963 * otherwise, best_last_r has to be checked.
1967 int could_have_continued;
1970 int backtrack_depth;
1971 /* There is a search tree with every node as set of deterministic
1972 * transitions in the super nfa. For every branch of a
1973 * backtrack point is an edge in the tree.
1974 * This counts up a pre-order of nodes in that tree.
1975 * It's saved on the search stack and printed when debugging.
1982 /* For continuations within the match tester */
1983 enum rx_test_match_entry test_match_resume_pt;
1984 struct rx_inx * saved_next_tr_table;
1985 struct rx_inx * saved_this_tr_table;
1987 struct rx_backtrack_frame * saved_bf;
1990 static __inline__ void init_fastmap( struct re_pattern_buffer *,
1991 struct rx_search_state * );
1994 extern char rx_slowmap[];
1995 extern unsigned char rx_id_translation[];
1997 static __inline__ void
1998 init_fastmap( struct re_pattern_buffer * rxb,
1999 struct rx_search_state * search_state )
2001 search_state->fastmap = (rxb->fastmap
2002 ? (char *)rxb->fastmap
2003 : (char *)rx_slowmap);
2004 /* Update the fastmap now if not correct already.
2005 * When the regexp was compiled, the fastmap was computed
2006 * and stored in a bitset. This expands the bitset into a
2007 * character array containing 1s and 0s.
2009 if ((search_state->fastmap == rxb->fastmap) && !rxb->fastmap_accurate)
2010 rx_blow_up_fastmap (rxb);
2011 search_state->fastmap_chr = -1;
2012 search_state->fastmap_val = 0;
2013 search_state->fastmap_resume_pt = rx_fastmap_start;
2016 static __inline__ void
2017 uninit_fastmap ( struct re_pattern_buffer * rxb,
2018 struct rx_search_state * search_state )
2020 /* Unset the fastmap sentinel */
2021 if (search_state->fastmap_chr >= 0)
2022 search_state->fastmap[search_state->fastmap_chr]
2023 = search_state->fastmap_val;
2026 static __inline__ int
2027 fastmap_search ( struct re_pattern_buffer * rxb, int stop,
2028 rx_get_burst_fn get_burst, void * app_closure,
2029 struct rx_search_state * search_state )
2031 enum rx_fastmap_entry pc;
2035 return_continuation:
2036 search_state->fastmap_resume_pt = pc;
2037 return rx_fastmap_continuation;
2040 pc = search_state->fastmap_resume_pt;
2045 return rx_fastmap_error;
2046 case rx_fastmap_start:
2047 init_fastmap_sentinal:
2048 /* For the sake of fast fastmapping, set a sentinal in the fastmap.
2049 * This sentinal will trap the fastmap loop when it reaches the last
2050 * valid character in a string half.
2052 * This must be reset when the fastmap/search loop crosses a string
2053 * boundry, and before returning to the caller. So sometimes,
2054 * the fastmap loop is restarted with `continue', othertimes by
2055 * `goto init_fastmap_sentinal'.
2057 if (search_state->outer_pos.size)
2059 search_state->fastmap_chr = ((search_state->outer_pos.search_direction == 1)
2060 ? *(search_state->outer_pos.end - 1)
2061 : *search_state->outer_pos.string);
2062 search_state->fastmap_val
2063 = search_state->fastmap[search_state->fastmap_chr];
2064 search_state->fastmap[search_state->fastmap_chr] = 1;
2068 search_state->fastmap_chr = -1;
2069 search_state->fastmap_val = 0;
2072 if (search_state->outer_pos.pos >= search_state->outer_pos.end)
2073 goto fastmap_hit_bound;
2076 if (search_state->outer_pos.search_direction == 1)
2078 if (search_state->fastmap_val)
2082 while (!search_state->fastmap[*search_state->outer_pos.pos])
2083 ++search_state->outer_pos.pos;
2084 return rx_fastmap_ok;
2091 while (!search_state->fastmap[*search_state->outer_pos.pos])
2092 ++search_state->outer_pos.pos;
2093 if (*search_state->outer_pos.pos != search_state->fastmap_chr)
2094 return rx_fastmap_ok;
2097 ++search_state->outer_pos.pos;
2098 if (search_state->outer_pos.pos == search_state->outer_pos.end)
2099 goto fastmap_hit_bound;
2106 __const__ unsigned char * bound;
2107 bound = search_state->outer_pos.string - 1;
2108 if (search_state->fastmap_val)
2112 while (!search_state->fastmap[*search_state->outer_pos.pos])
2113 --search_state->outer_pos.pos;
2114 return rx_fastmap_ok;
2121 while (!search_state->fastmap[*search_state->outer_pos.pos])
2122 --search_state->outer_pos.pos;
2123 if ((*search_state->outer_pos.pos != search_state->fastmap_chr) || search_state->fastmap_val)
2124 return rx_fastmap_ok;
2127 --search_state->outer_pos.pos;
2128 if (search_state->outer_pos.pos == bound)
2129 goto fastmap_hit_bound;
2136 case rx_fastmap_string_break:
2139 /* If we hit a bound, it may be time to fetch another burst
2140 * of string, or it may be time to return a continuation to
2141 * the caller, or it might be time to fail.
2145 burst_state = get_burst (&search_state->outer_pos, app_closure, stop);
2146 switch (burst_state)
2149 case rx_get_burst_error:
2150 return rx_fastmap_error;
2151 case rx_get_burst_continuation:
2153 pc = rx_fastmap_string_break;
2154 goto return_continuation;
2156 case rx_get_burst_ok:
2157 goto init_fastmap_sentinal;
2158 case rx_get_burst_no_more:
2159 /* ...not a string split, simply no more string.
2161 * When searching backward, running out of string
2162 * is reason to quit.
2164 * When searching forward, we allow the possibility
2165 * of an (empty) match after the last character in the
2166 * virtual string. So, fall through to the matcher
2168 return ( (search_state->outer_pos.search_direction == 1)
2180 /* The `emacs' switch turns on certain matching commands
2181 * that make sense only in Emacs.
2189 /* Setting RX_MEMDBUG is useful if you have dbmalloc. Maybe with similar
2194 #endif /* RX_RX_MEMDBUG */
2196 /* We used to test for `BSTRING' here, but only GCC and Emacs define
2197 * `BSTRING', as far as I know, and neither of them use this code.
2199 #if HAVE_STRING_H || __STDC__
2203 #define bcmp(s1, s2, n) memcmp ((s1), (s2), (n))
2207 #define bcopy(s, d, n) memcpy ((d), (s), (n))
2211 #define bzero(s, n) memset ((s), 0, (n))
2214 #else /* HAVE_STRING_H || __STDC__ */
2215 #include <strings.h>
2216 #endif /* not (HAVE_STRING_H || __STDC__) */
2220 #else /* not __STDC__ */
2223 #endif /* not __STDC__ */
2228 /* How many characters in the character set. */
2229 #define CHAR_SET_SIZE (1 << CHARBITS)
2232 /* Define the syntax basics for \<, \>, etc.
2233 * This must be nonzero for the wordchar and notwordchar pattern
2234 * commands in re_match_2.
2239 #define SYNTAX(c) re_syntax_table[c]
2240 RX_DECL char re_syntax_table[CHAR_SET_SIZE];
2241 #endif /* not emacs */
2244 /* Test if at very beginning or at very end of the virtual concatenation
2245 * of `string1' and `string2'. If only one string, it's `string2'.
2248 #define AT_STRINGS_BEG() \
2250 == ((search_state.test_pos.pos - search_state.test_pos.string) \
2251 + search_state.test_pos.offset))
2253 #define AT_STRINGS_END() \
2254 ( (total_size - 1) \
2255 == ((search_state.test_pos.pos - search_state.test_pos.string) \
2256 + search_state.test_pos.offset))
2259 /* Test if POS + 1 points to a character which is word-constituent. We have
2260 * two special cases to check for: if past the end of string1, look at
2261 * the first character in string2; and if before the beginning of
2262 * string2, look at the last character in string1.
2264 * Assumes `string1' exists, so use in conjunction with AT_STRINGS_BEG ().
2266 #define LETTER_P(POS,OFF) \
2267 ( SYNTAX (fetch_char(POS, OFF, app_closure, stop)) \
2270 /* Test if the character at D and the one after D differ with respect
2271 * to being word-constituent.
2273 #define AT_WORD_BOUNDARY(d) \
2274 (AT_STRINGS_BEG () || AT_STRINGS_END () || LETTER_P (d,0) != LETTER_P (d, 1))
2277 #ifdef RX_SUPPORT_CONTINUATIONS
2278 #define RX_STACK_ALLOC(BYTES) malloc(BYTES)
2279 #define RX_STACK_FREE(MEM) free(MEM)
2281 #define RX_STACK_ALLOC(BYTES) alloca(BYTES)
2282 #define RX_STACK_FREE(MEM) \
2283 ((struct rx_stack_chunk *)MEM)->next_chunk = search_state.free_chunks; \
2284 search_state.free_chunks = ((struct rx_stack_chunk *)MEM);
2288 #define PUSH(CHUNK_VAR,BYTES) \
2289 if (!CHUNK_VAR || (CHUNK_VAR->bytes_left < (BYTES))) \
2291 struct rx_stack_chunk * new_chunk; \
2292 if (search_state.free_chunks) \
2294 new_chunk = search_state.free_chunks; \
2295 search_state.free_chunks = search_state.free_chunks->next_chunk; \
2299 new_chunk = (struct rx_stack_chunk *)RX_STACK_ALLOC(search_state.chunk_bytes); \
2302 search_state.ret_val = 0; \
2303 goto test_do_return; \
2306 new_chunk->sp = (char *)new_chunk + sizeof (struct rx_stack_chunk); \
2307 new_chunk->bytes_left = (search_state.chunk_bytes \
2309 - sizeof (struct rx_stack_chunk)); \
2310 new_chunk->next_chunk = CHUNK_VAR; \
2311 CHUNK_VAR = new_chunk; \
2314 (CHUNK_VAR->sp += (BYTES)), (CHUNK_VAR->bytes_left -= (BYTES))
2316 #define POP(CHUNK_VAR,BYTES) \
2317 if (CHUNK_VAR->sp == ((char *)CHUNK_VAR + sizeof(*CHUNK_VAR))) \
2319 struct rx_stack_chunk * new_chunk = CHUNK_VAR->next_chunk; \
2320 RX_STACK_FREE(CHUNK_VAR); \
2321 CHUNK_VAR = new_chunk; \
2324 (CHUNK_VAR->sp -= BYTES), (CHUNK_VAR->bytes_left += BYTES)
2328 #define SRCH_TRANSLATE(C) search_state.translate[(unsigned char) (C)]
2334 RX_DECL __inline__ int
2335 rx_search (struct re_pattern_buffer * rxb,
2340 rx_get_burst_fn get_burst,
2341 rx_back_check_fn back_check,
2342 rx_fetch_char_fn fetch_char,
2344 struct re_registers * regs,
2345 struct rx_search_state * resume_state,
2346 struct rx_search_state * save_state)
2348 RX_DECL __inline__ int
2349 rx_search (rxb, startpos, range, stop, total_size,
2350 get_burst, back_check, fetch_char,
2351 app_closure, regs, resume_state, save_state)
2352 struct re_pattern_buffer * rxb;
2357 rx_get_burst_fn get_burst;
2358 rx_back_check_fn back_check;
2359 rx_fetch_char_fn fetch_char;
2361 struct re_registers * regs;
2362 struct rx_search_state * resume_state;
2363 struct rx_search_state * save_state;
2368 struct rx_search_state search_state;
2370 search_state.free_chunks = 0;
2372 pc = rx_outer_start;
2375 search_state = *resume_state;
2376 regs = search_state.saved_regs;
2377 rxb = search_state.saved_rxb;
2378 startpos = search_state.saved_startpos;
2379 range = search_state.saved_range;
2380 stop = search_state.saved_stop;
2381 total_size = search_state.saved_total_size;
2382 get_burst = search_state.saved_get_burst;
2383 back_check = search_state.saved_back_check;
2384 pc = search_state.outer_search_resume_pt;
2387 return_continuation:
2390 *save_state = search_state;
2391 save_state->saved_regs = regs;
2392 save_state->saved_rxb = rxb;
2393 save_state->saved_startpos = startpos;
2394 save_state->saved_range = range;
2395 save_state->saved_stop = stop;
2396 save_state->saved_total_size = total_size;
2397 save_state->saved_get_burst = get_burst;
2398 save_state->saved_back_check = back_check;
2399 save_state->outer_search_resume_pt = pc;
2401 return rx_search_continuation;
2407 case rx_outer_start:
2408 search_state.ret_val = rx_search_fail;
2409 ( search_state.lparen
2410 = search_state.rparen
2411 = search_state.best_lpspace
2412 = search_state.best_rpspace
2415 /* figure the number of registers we may need for use in backreferences.
2416 * the number here includes an element for register zero.
2418 search_state.num_regs = rxb->re_nsub + 1;
2421 /* check for out-of-range startpos. */
2422 if ((startpos < 0) || (startpos > total_size))
2423 return rx_search_fail;
2425 /* fix up range if it might eventually take us outside the string. */
2428 endpos = startpos + range;
2430 range = (-1 - startpos);
2431 else if (endpos > (total_size + 1))
2432 range = total_size - startpos;
2435 /* if the search isn't to be a backwards one, don't waste time in a
2436 * long search for a pattern that says it is anchored.
2438 if (rxb->begbuf_only && (range > 0))
2441 return rx_search_fail;
2446 /* decide whether to use internal or user-provided reg buffers. */
2447 if (!regs || rxb->no_sub)
2449 search_state.best_lpspace =
2450 (regoff_t *)REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
2451 search_state.best_rpspace =
2452 (regoff_t *)REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
2453 search_state.best_lparen = search_state.best_lpspace;
2454 search_state.best_rparen = search_state.best_rpspace;
2458 /* have the register data arrays been allocated? */
2459 if (rxb->regs_allocated == REGS_UNALLOCATED)
2460 { /* no. so allocate them with malloc. we need one
2461 extra element beyond `search_state.num_regs' for the `-1' marker
2463 regs->num_regs = MAX (RE_NREGS, rxb->re_nsub + 1);
2464 regs->start = ((regoff_t *)
2465 malloc (regs->num_regs * sizeof ( regoff_t)));
2466 regs->end = ((regoff_t *)
2467 malloc (regs->num_regs * sizeof ( regoff_t)));
2468 if (regs->start == 0 || regs->end == 0)
2469 return rx_search_error;
2470 rxb->regs_allocated = REGS_REALLOCATE;
2472 else if (rxb->regs_allocated == REGS_REALLOCATE)
2473 { /* yes. if we need more elements than were already
2474 allocated, reallocate them. if we need fewer, just
2476 if (regs->num_regs < search_state.num_regs + 1)
2478 regs->num_regs = search_state.num_regs + 1;
2479 regs->start = ((regoff_t *)
2480 realloc (regs->start,
2481 regs->num_regs * sizeof (regoff_t)));
2482 regs->end = ((regoff_t *)
2484 regs->num_regs * sizeof ( regoff_t)));
2485 if (regs->start == 0 || regs->end == 0)
2486 return rx_search_error;
2489 else if (rxb->regs_allocated != REGS_FIXED)
2490 return rx_search_error;
2492 if (regs->num_regs < search_state.num_regs + 1)
2494 search_state.best_lpspace =
2496 REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)));
2497 search_state.best_rpspace =
2499 REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t)));
2500 search_state.best_lparen = search_state.best_lpspace;
2501 search_state.best_rparen = search_state.best_rpspace;
2505 search_state.best_lparen = regs->start;
2506 search_state.best_rparen = regs->end;
2510 search_state.lparen =
2511 (regoff_t *) REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
2512 search_state.rparen =
2513 (regoff_t *) REGEX_ALLOCATE (search_state.num_regs * sizeof(regoff_t));
2515 if (! ( search_state.best_rparen
2516 && search_state.best_lparen
2517 && search_state.lparen && search_state.rparen))
2518 return rx_search_error;
2520 search_state.best_last_l = search_state.best_last_r = -1;
2522 search_state.translate = (rxb->translate
2524 : rx_id_translation);
2529 * two nfa's were compiled.
2531 * `1' faster but gets registers wrong and ends too soon.
2533 search_state.nfa_choice = (regs && !rxb->least_subs) ? '\0' : '\1';
2535 /* we have the option to look for the best match or the first
2536 * one we can find. if the user isn't asking for register information,
2537 * we don't need to find the best match.
2539 search_state.first_found = !regs;
2543 search_state.outer_pos.search_end = startpos + range;
2544 search_state.outer_pos.search_direction = 1;
2548 search_state.outer_pos.search_end = startpos + range;
2549 search_state.outer_pos.search_direction = -1;
2552 /* the vacuous search always turns up nothing. */
2553 if ((search_state.outer_pos.search_direction == 1)
2554 ? (startpos > search_state.outer_pos.search_end)
2555 : (startpos < search_state.outer_pos.search_end))
2556 return rx_search_fail;
2558 /* now we build the starting state of the supernfa. */
2560 struct rx_superset * start_contents;
2561 struct rx_nfa_state_set * start_nfa_set;
2563 /* we presume here that the nfa start state has only one
2564 * possible future with no side effects.
2566 start_nfa_set = rxb->start->futures->destset;
2567 if ( rxb->rx.start_set
2568 && (rxb->rx.start_set->starts_for == &rxb->rx))
2569 start_contents = rxb->rx.start_set;
2573 rx_superstate_eclosure_union (&rxb->rx,
2574 rx_superset_cons (&rxb->rx, 0, 0),
2577 if (!start_contents)
2578 return rx_search_fail;
2580 start_contents->starts_for = &rxb->rx;
2581 rxb->rx.start_set = start_contents;
2583 if ( start_contents->superstate
2584 && (start_contents->superstate->rx_id == rxb->rx.rx_id))
2586 search_state.start_super = start_contents->superstate;
2587 rx_lock_superstate (&rxb->rx, search_state.start_super);
2591 rx_protect_superset (&rxb->rx, start_contents);
2593 search_state.start_super = rx_superstate (&rxb->rx, start_contents);
2594 if (!search_state.start_super)
2595 return rx_search_fail;
2596 rx_lock_superstate (&rxb->rx, search_state.start_super);
2597 rx_release_superset (&rxb->rx, start_contents);
2602 /* The outer_pos tracks the position within the strings
2603 * as seen by loop that calls fastmap_search.
2605 * The caller supplied get_burst function actually
2606 * gives us pointers to chars.
2608 * Communication with the get_burst function is through an
2609 * rx_string_position structure. Here, the structure for
2610 * outer_pos is initialized. It is set to point to the
2611 * NULL string, at an offset of STARTPOS. STARTPOS is out
2612 * of range of the NULL string, so the first call to
2613 * getburst will patch up the rx_string_position to point
2614 * to valid characters.
2617 ( search_state.outer_pos.string
2618 = search_state.outer_pos.end
2621 search_state.outer_pos.offset = 0;
2622 search_state.outer_pos.size = 0;
2623 search_state.outer_pos.pos = (unsigned char *)startpos;
2624 init_fastmap (rxb, &search_state);
2626 search_state.fastmap_resume_pt = rx_fastmap_start;
2627 case rx_outer_fastmap:
2633 fastmap_state = fastmap_search (rxb, stop, get_burst, app_closure,
2635 switch (fastmap_state)
2637 case rx_fastmap_continuation:
2638 pc = rx_outer_fastmap;
2639 goto return_continuation;
2640 case rx_fastmap_fail:
2647 /* now the fastmap loop has brought us to a plausible
2648 * starting point for a match. so, it's time to run the
2649 * nfa and see if a match occured.
2651 startpos = ( search_state.outer_pos.pos
2652 - search_state.outer_pos.string
2653 + search_state.outer_pos.offset);
2655 /*|*/ if ((range > 0) && (startpos == search_state.outer_pos.search_end))
2660 search_state.test_match_resume_pt = rx_test_start;
2661 /* do interrupted for entry point... */
2663 /* ...do continued */
2666 test_returns_to_search:
2669 case rx_test_continuation:
2671 goto return_continuation;
2673 search_state.ret_val = rx_search_error;
2680 search_state.outer_pos.pos += search_state.outer_pos.search_direction;
2681 startpos += search_state.outer_pos.search_direction;
2683 /*|*/ if (search_state.test_pos.pos < search_state.test_pos.end)
2687 /* do interrupted for entry point... */
2688 case rx_outer_restore_pos:
2691 x = get_burst (&search_state.outer_pos, app_closure, stop);
2694 case rx_get_burst_continuation:
2695 pc = rx_outer_restore_pos;
2696 goto return_continuation;
2697 case rx_get_burst_error:
2698 search_state.ret_val = rx_search_error;
2700 case rx_get_burst_no_more:
2701 if (rxb->can_match_empty)
2704 case rx_get_burst_ok:
2707 } /* } while (...see below...) */
2709 if ((search_state.outer_pos.search_direction == 1)
2710 ? (startpos <= search_state.outer_pos.search_end)
2711 : (startpos > search_state.outer_pos.search_end))
2716 uninit_fastmap (rxb, &search_state);
2717 if (search_state.start_super)
2718 rx_unlock_superstate (&rxb->rx, search_state.start_super);
2721 if (search_state.lparen) free (search_state.lparen);
2722 if (search_state.rparen) free (search_state.rparen);
2723 if (search_state.best_lpspace) free (search_state.best_lpspace);
2724 if (search_state.best_rpspace) free (search_state.best_rpspace);
2726 return search_state.ret_val;
2732 enum rx_test_match_entry test_pc;
2734 test_pc = search_state.test_match_resume_pt;
2735 if (test_pc == rx_test_start)
2738 search_state.backtrack_depth = 0;
2740 search_state.last_l = search_state.last_r = 0;
2741 search_state.lparen[0] = startpos;
2742 search_state.super = search_state.start_super;
2743 search_state.c = search_state.nfa_choice;
2744 search_state.test_pos.pos = search_state.outer_pos.pos - 1;
2745 search_state.test_pos.string = search_state.outer_pos.string;
2746 search_state.test_pos.end = search_state.outer_pos.end;
2747 search_state.test_pos.offset = search_state.outer_pos.offset;
2748 search_state.test_pos.size = search_state.outer_pos.size;
2749 search_state.test_pos.search_direction = 1;
2750 search_state.counter_stack = 0;
2751 search_state.backtrack_stack = 0;
2752 search_state.backtrack_frame_bytes =
2753 (sizeof (struct rx_backtrack_frame)
2754 + (rxb->match_regs_on_stack
2755 ? sizeof (regoff_t) * (search_state.num_regs + 1) * 2
2757 search_state.chunk_bytes = search_state.backtrack_frame_bytes * 64;
2758 search_state.test_ret = rx_test_line_finished;
2759 search_state.could_have_continued = 0;
2761 /* This is while (1)...except that the body of the loop is interrupted
2762 * by some alternative entry points.
2767 case rx_test_cache_hit_loop:
2768 goto resume_continuation_1;
2769 case rx_test_backreference_check:
2770 goto resume_continuation_2;
2771 case rx_test_backtrack_return:
2772 goto resume_continuation_3;
2775 /* There is a search tree with every node as set of deterministic
2776 * transitions in the super nfa. For every branch of a
2777 * backtrack point is an edge in the tree.
2778 * This counts up a pre-order of nodes in that tree.
2779 * It's saved on the search stack and printed when debugging.
2781 search_state.line_no = 0;
2782 search_state.lines_found = 0;
2786 /* A superstate is basicly a transition table, indexed by
2787 * characters from the string being tested, and containing
2788 * RX_INX (`instruction frame') structures.
2790 search_state.ifr = &search_state.super->transitions [search_state.c];
2793 /* This is the point to which control is sent when the
2794 * test matcher `recurses'. Before jumping here, some variables
2795 * need to be saved on the stack and the next instruction frame
2796 * has to be computed.
2800 /* Some instructions don't advance the matcher, but just
2801 * carry out some side effects and fetch a new instruction.
2802 * To dispatch that new instruction, `goto restart'.
2806 struct rx_inx * next_tr_table;
2807 struct rx_inx * this_tr_table;
2809 /* The fastest route through the loop is when the instruction
2810 * is RX_NEXT_CHAR. This case is detected when SEARCH_STATE.IFR->DATA
2811 * is non-zero. In that case, it points to the next
2814 * This allows us to not bother fetching the bytecode.
2816 next_tr_table = (struct rx_inx *)search_state.ifr->data;
2817 this_tr_table = search_state.super->transitions;
2818 while (next_tr_table)
2823 struct rx_superset * setp;
2825 fprintf (stderr, "%d %d>> re_next_char @ %d (%d)",
2826 search_state.line_no,
2827 search_state.backtrack_depth,
2828 (search_state.test_pos.pos - search_state.test_pos.string
2829 + search_state.test_pos.offset), search_state.c);
2831 search_state.super =
2832 ((struct rx_superstate *)
2833 ((char *)this_tr_table
2835 ((struct rx_superstate *)0)->transitions)));
2837 setp = search_state.super->contents;
2838 fprintf (stderr, " superstet (rx=%d, &=%x: ",
2839 rxb->rx.rx_id, setp);
2842 fprintf (stderr, "%d ", setp->id);
2845 fprintf (stderr, "\n");
2848 this_tr_table = next_tr_table;
2849 ++search_state.test_pos.pos;
2850 if (search_state.test_pos.pos == search_state.test_pos.end)
2854 burst_state = get_burst (&search_state.test_pos,
2856 switch (burst_state)
2858 case rx_get_burst_continuation:
2859 search_state.saved_this_tr_table = this_tr_table;
2860 search_state.saved_next_tr_table = next_tr_table;
2861 test_pc = rx_test_cache_hit_loop;
2862 goto test_return_continuation;
2864 resume_continuation_1:
2865 /* Continuation one jumps here to do its work: */
2866 search_state.saved_this_tr_table = this_tr_table;
2867 search_state.saved_next_tr_table = next_tr_table;
2870 case rx_get_burst_ok:
2871 /* get_burst succeeded...keep going */
2874 case rx_get_burst_no_more:
2875 search_state.test_ret = rx_test_line_finished;
2876 search_state.could_have_continued = 1;
2877 goto test_do_return;
2879 case rx_get_burst_error:
2881 search_state.test_ret = rx_test_internal_error;
2882 goto test_do_return;
2885 search_state.c = *search_state.test_pos.pos;
2886 search_state.ifr = this_tr_table + search_state.c;
2887 next_tr_table = (struct rx_inx *)search_state.ifr->data;
2888 } /* Fast loop through cached transition tables */
2890 /* Here when we ran out of cached next-char transitions.
2891 * So, it will be necessary to do a more expensive
2892 * dispatch on the current instruction. The superstate
2893 * pointer is allowed to become invalid during next-char
2894 * transitions -- now we must bring it up to date.
2896 search_state.super =
2897 ((struct rx_superstate *)
2898 ((char *)this_tr_table
2900 ((struct rx_superstate *)0)->transitions)));
2903 /* We've encountered an instruction other than next-char.
2904 * Dispatch that instruction:
2906 inx = (int)search_state.ifr->inx;
2910 struct rx_superset * setp = search_state.super->contents;
2912 fprintf (stderr, "%d %d>> %s @ %d (%d)", search_state.line_no,
2913 search_state.backtrack_depth,
2915 (search_state.test_pos.pos - search_state.test_pos.string
2916 + (test_pos.half == 0 ? 0 : size1)), search_state.c);
2918 fprintf (stderr, " superstet (rx=%d, &=%x: ",
2919 rxb->rx.rx_id, setp);
2922 fprintf (stderr, "%d ", setp->id);
2925 fprintf (stderr, "\n");
2928 switch ((enum rx_opcode)inx)
2930 case rx_do_side_effects:
2932 /* RX_DO_SIDE_EFFECTS occurs when we cross epsilon
2933 * edges associated with parentheses, backreferencing, etc.
2936 struct rx_distinct_future * df =
2937 (struct rx_distinct_future *)search_state.ifr->data_2;
2938 struct rx_se_list * el = df->effects;
2939 /* Side effects come in lists. This walks down
2940 * a list, dispatching.
2945 effect = (long)el->car;
2951 struct rx_superset * setp = search_state.super->contents;
2953 fprintf (stderr, "....%d %d>> %s\n", search_state.line_no,
2954 search_state.backtrack_depth,
2958 switch ((enum re_side_effects) effect)
2961 case re_se_pushback:
2962 search_state.ifr = &df->future_frame;
2963 if (!search_state.ifr->data)
2965 struct rx_superstate * sup;
2966 sup = search_state.super;
2967 rx_lock_superstate (rx, sup);
2968 if (!rx_handle_cache_miss (&rxb->rx,
2974 rx_unlock_superstate (rx, sup);
2975 search_state.test_ret = rx_test_internal_error;
2976 goto test_do_return;
2978 rx_unlock_superstate (rx, sup);
2980 /* --search_state.test_pos.pos; */
2981 search_state.c = 't';
2983 = ((struct rx_superstate *)
2984 ((char *)search_state.ifr->data
2985 - (long)(((struct rx_superstate *)0)
2991 struct rx_counter_frame * old_cf
2992 = (search_state.counter_stack
2993 ? ((struct rx_counter_frame *)
2994 search_state.counter_stack->sp)
2996 struct rx_counter_frame * cf;
2997 PUSH (search_state.counter_stack,
2998 sizeof (struct rx_counter_frame));
2999 cf = ((struct rx_counter_frame *)
3000 search_state.counter_stack->sp);
3001 cf->tag = re_se_iter;
3003 cf->inherited_from = 0;
3008 goto test_do_return;
3010 if (!AT_STRINGS_BEG ())
3011 goto test_do_return;
3014 if (!AT_STRINGS_END ())
3015 goto test_do_return;
3018 if ( LETTER_P (&search_state.test_pos, 1)
3019 && ( AT_STRINGS_BEG()
3020 || !LETTER_P (&search_state.test_pos, 0)))
3023 goto test_do_return;
3025 if ( !AT_STRINGS_BEG ()
3026 && LETTER_P (&search_state.test_pos, 0)
3027 && (AT_STRINGS_END ()
3028 || !LETTER_P (&search_state.test_pos, 1)))
3031 goto test_do_return;
3032 case re_se_wordbound:
3033 if (AT_WORD_BOUNDARY (&search_state.test_pos))
3036 goto test_do_return;
3037 case re_se_notwordbound:
3038 if (!AT_WORD_BOUNDARY (&search_state.test_pos))
3041 goto test_do_return;
3043 if (AT_STRINGS_BEG ())
3046 goto test_do_return;
3052 char pos_c = *search_state.test_pos.pos;
3053 if ( (SRCH_TRANSLATE (pos_c)
3054 == SRCH_TRANSLATE('\n'))
3055 && rxb->newline_anchor)
3058 goto test_do_return;
3061 if (AT_STRINGS_END ())
3064 goto test_do_return;
3070 if ( ( SRCH_TRANSLATE (fetch_char
3071 (&search_state.test_pos, 1,
3073 == SRCH_TRANSLATE ('\n'))
3074 && rxb->newline_anchor)
3077 goto test_do_return;
3081 /* This is the first side effect in every
3084 * FOR NO GOOD REASON...get rid of it...
3091 ((int)(search_state.test_pos.pos
3092 - search_state.test_pos.string)
3093 + search_state.test_pos.offset);
3094 struct rx_counter_frame * old_cf
3095 = (search_state.counter_stack
3096 ? ((struct rx_counter_frame *)
3097 search_state.counter_stack->sp)
3099 struct rx_counter_frame * cf;
3100 PUSH(search_state.counter_stack,
3101 sizeof (struct rx_counter_frame));
3102 cf = ((struct rx_counter_frame *)
3103 search_state.counter_stack->sp);
3104 cf->tag = re_se_pushpos;
3106 cf->inherited_from = 0;
3114 ((int)(search_state.test_pos.pos
3115 - search_state.test_pos.string)
3116 + search_state.test_pos.offset);
3117 struct rx_counter_frame * cf
3118 = ((struct rx_counter_frame *)
3119 search_state.counter_stack->sp);
3120 if (cf->val == urhere)
3121 goto test_do_return;
3128 POP(search_state.counter_stack,
3129 sizeof (struct rx_counter_frame));
3135 case re_se_not_syntax:
3138 * this release lacks emacs support
3147 case re_se_end_iter:
3149 case re_floogle_flap:
3150 search_state.ret_val = 0;
3151 goto test_do_return;
3158 fprintf (stderr, "....%d %d>> %s %d %d\n", search_state.line_no,
3159 search_state.backtrack_depth,
3160 efnames2[rxb->se_params [effect].se],
3161 rxb->se_params [effect].op1,
3162 rxb->se_params [effect].op2);
3164 switch (rxb->se_params [effect].se)
3167 /* This side effect indicates that we've
3168 * found a match, though not necessarily the
3169 * best match. This is a fancy assignment to
3170 * register 0 unless the caller didn't
3171 * care about registers. In which case,
3172 * this stops the match.
3176 ((int)(search_state.test_pos.pos
3177 - search_state.test_pos.string)
3178 + search_state.test_pos.offset);
3180 if ( (search_state.best_last_r < 0)
3181 || (urhere + 1 > search_state.best_rparen[0]))
3183 /* Record the best known and keep
3187 for (x = 0; x <= search_state.last_l; ++x)
3188 search_state.best_lparen[x] = search_state.lparen[x];
3189 search_state.best_last_l = search_state.last_l;
3190 for (x = 0; x <= search_state.last_r; ++x)
3191 search_state.best_rparen[x] = search_state.rparen[x];
3192 search_state.best_rparen[0] = urhere + 1;
3193 search_state.best_last_r = search_state.last_r;
3195 /* If we're not reporting the match-length
3196 * or other register info, we need look no
3199 if (search_state.first_found)
3201 search_state.test_ret = rx_test_found_first;
3202 goto test_do_return;
3209 ((int)(search_state.test_pos.pos
3210 - search_state.test_pos.string)
3211 + search_state.test_pos.offset);
3213 int reg = rxb->se_params [effect].op1;
3215 if (reg > search_state.last_l)
3218 search_state.lparen[reg] = urhere + 1;
3219 /* In addition to making this assignment,
3220 * we now know that lower numbered regs
3221 * that haven't already been assigned,
3222 * won't be. We make sure they're
3223 * filled with -1, so they can be
3224 * recognized as unassigned.
3226 if (search_state.last_l < reg)
3227 while (++search_state.last_l < reg)
3228 search_state.lparen[search_state.last_l] = -1;
3236 ((int)(search_state.test_pos.pos
3237 - search_state.test_pos.string)
3238 + search_state.test_pos.offset);
3239 int reg = rxb->se_params [effect].op1;
3240 search_state.rparen[reg] = urhere + 1;
3241 if (search_state.last_r < reg)
3243 while (++search_state.last_r < reg)
3244 search_state.rparen[search_state.last_r]
3252 int reg = rxb->se_params [effect].op1;
3253 if ( reg > search_state.last_r
3254 || search_state.rparen[reg] < 0)
3255 goto test_do_return;
3259 check_backreference:
3261 = back_check (&search_state.test_pos,
3262 search_state.lparen[reg],
3263 search_state.rparen[reg],
3264 search_state.translate,
3267 switch (backref_status)
3269 case rx_back_check_continuation:
3270 search_state.saved_reg = reg;
3271 test_pc = rx_test_backreference_check;
3272 goto test_return_continuation;
3273 resume_continuation_2:
3274 reg = search_state.saved_reg;
3275 goto check_backreference;
3276 case rx_back_check_fail:
3278 goto test_do_return;
3279 case rx_back_check_pass:
3281 * test_pos now advanced to last
3282 * char matched by backref
3291 struct rx_counter_frame * csp
3292 = ((struct rx_counter_frame *)
3293 search_state.counter_stack->sp);
3294 if (csp->val == rxb->se_params[effect].op2)
3295 goto test_do_return;
3300 case re_se_end_iter:
3302 struct rx_counter_frame * csp
3303 = ((struct rx_counter_frame *)
3304 search_state.counter_stack->sp);
3305 if (csp->val < rxb->se_params[effect].op1)
3306 goto test_do_return;
3309 struct rx_counter_frame * source = csp;
3310 while (source->inherited_from)
3311 source = source->inherited_from;
3312 if (!source || !source->cdr)
3314 POP(search_state.counter_stack,
3315 sizeof(struct rx_counter_frame));
3319 source = source->cdr;
3320 csp->val = source->val;
3321 csp->tag = source->tag;
3323 csp->inherited_from = source;
3332 case re_se_pushback:
3339 case re_se_not_syntax:
3343 case re_se_wordbound:
3344 case re_se_notwordbound:
3349 case re_floogle_flap:
3350 search_state.ret_val = 0;
3351 goto test_do_return;
3356 /* Now the side effects are done,
3357 * so get the next instruction.
3360 search_state.ifr = &df->future_frame;
3364 case rx_backtrack_point:
3366 /* A backtrack point indicates that we've reached a
3367 * non-determinism in the superstate NFA. This is a
3368 * loop that exhaustively searches the possibilities.
3370 * A backtracking strategy is used. We keep track of what
3371 * registers are valid so we can erase side effects.
3373 * First, make sure there is some stack space to hold
3377 struct rx_backtrack_frame * bf;
3379 PUSH(search_state.backtrack_stack,
3380 search_state.backtrack_frame_bytes);
3382 ++search_state.backtrack_depth;
3385 bf = ((struct rx_backtrack_frame *)
3386 search_state.backtrack_stack->sp);
3388 bf->stk_super = search_state.super;
3389 /* We prevent the current superstate from being
3390 * deleted from the superstate cache.
3392 rx_lock_superstate (&rxb->rx, search_state.super);
3394 bf->stk_search_state.line_no = search_state.line_no;
3396 bf->stk_c = search_state.c;
3397 bf->stk_test_pos = search_state.test_pos;
3398 bf->stk_last_l = search_state.last_l;
3399 bf->stk_last_r = search_state.last_r;
3400 bf->df = ((struct rx_super_edge *)
3401 search_state.ifr->data_2)->options;
3402 bf->first_df = bf->df;
3403 bf->counter_stack_sp = (search_state.counter_stack
3404 ? search_state.counter_stack->sp
3406 bf->stk_test_ret = search_state.test_ret;
3407 if (rxb->match_regs_on_stack)
3411 (regoff_t *)((char *)bf + sizeof (*bf));
3412 for (x = 0; x <= search_state.last_l; ++x)
3413 stk[x] = search_state.lparen[x];
3415 for (x = 0; x <= search_state.last_r; ++x)
3416 stk[x] = search_state.rparen[x];
3420 /* Here is a while loop whose body is mainly a function
3421 * call and some code to handle a return from that
3424 * From here on for the rest of `case backtrack_point' it
3425 * is unsafe to assume that the search_state copies of
3426 * variables saved on the backtracking stack are valid
3427 * -- so read their values from the backtracking stack.
3429 * This lets us use one generation fewer stack saves in
3430 * the call-graph of a search.
3433 while_non_det_options:
3435 ++search_state.lines_found;
3437 fprintf (stderr, "@@@ %d calls %d @@@\n",
3438 search_state.line_no, search_state.lines_found);
3440 search_state.line_no = search_state.lines_found;
3443 if (bf->df->next_same_super_edge[0] == bf->first_df)
3445 /* This is a tail-call optimization -- we don't recurse
3446 * for the last of the possible futures.
3448 search_state.ifr = (bf->df->effects
3449 ? &bf->df->side_effects_frame
3450 : &bf->df->future_frame);
3452 rx_unlock_superstate (&rxb->rx, search_state.super);
3453 POP(search_state.backtrack_stack,
3454 search_state.backtrack_frame_bytes);
3456 --search_state.backtrack_depth;
3462 if (search_state.counter_stack)
3464 struct rx_counter_frame * old_cf
3465 = ((struct rx_counter_frame *)search_state.counter_stack->sp);
3466 struct rx_counter_frame * cf;
3467 PUSH(search_state.counter_stack, sizeof (struct rx_counter_frame));
3468 cf = ((struct rx_counter_frame *)search_state.counter_stack->sp);
3469 cf->tag = old_cf->tag;
3470 cf->val = old_cf->val;
3471 cf->inherited_from = old_cf;
3474 /* `Call' this test-match block */
3475 search_state.ifr = (bf->df->effects
3476 ? &bf->df->side_effects_frame
3477 : &bf->df->future_frame);
3478 goto recurse_test_match;
3481 /* Returns in this block are accomplished by
3482 * goto test_do_return. There are two cases.
3483 * If there is some search-stack left,
3484 * then it is a return from a `recursive' call.
3485 * If there is no search-stack left, then
3486 * we should return to the fastmap/search loop.
3491 if (!search_state.backtrack_stack)
3495 fprintf (stderr, "!!! %d bails returning %d !!!\n",
3496 search_state.line_no, search_state.test_ret);
3499 /* No more search-stack -- this test is done. */
3500 if (search_state.test_ret != rx_test_internal_error)
3501 goto return_from_test_match;
3503 goto error_in_testing_match;
3506 /* Returning from a recursive call to
3507 * the test match block:
3510 bf = ((struct rx_backtrack_frame *)
3511 search_state.backtrack_stack->sp);
3514 fprintf (stderr, "+++ %d returns %d (to %d)+++\n",
3515 search_state.line_no,
3516 search_state.test_ret,
3517 bf->stk_search_state.line_no);
3520 while (search_state.counter_stack
3521 && (!bf->counter_stack_sp
3522 || (bf->counter_stack_sp
3523 != search_state.counter_stack->sp)))
3525 POP(search_state.counter_stack,
3526 sizeof (struct rx_counter_frame));
3529 if (search_state.test_ret == rx_test_internal_error)
3531 POP (search_state.backtrack_stack,
3532 search_state.backtrack_frame_bytes);
3533 goto test_do_return;
3536 /* If a non-longest match was found and that is good
3537 * enough, return immediately.
3539 if ( (search_state.test_ret == rx_test_found_first)
3540 && search_state.first_found)
3542 rx_unlock_superstate (&rxb->rx, bf->stk_super);
3543 POP (search_state.backtrack_stack,
3544 search_state.backtrack_frame_bytes);
3545 goto test_do_return;
3548 search_state.test_ret = bf->stk_test_ret;
3549 search_state.last_l = bf->stk_last_l;
3550 search_state.last_r = bf->stk_last_r;
3551 bf->df = bf->df->next_same_super_edge[0];
3552 search_state.super = bf->stk_super;
3553 search_state.c = bf->stk_c;
3555 search_state.line_no = bf->stk_search_state.line_no;
3558 if (rxb->match_regs_on_stack)
3562 (regoff_t *)((char *)bf + sizeof (*bf));
3563 for (x = 0; x <= search_state.last_l; ++x)
3564 search_state.lparen[x] = stk[x];
3566 for (x = 0; x <= search_state.last_r; ++x)
3567 search_state.rparen[x] = stk[x];
3570 if ((search_state.test_ret != rx_test_line_finished) &&
3571 (search_state.test_ret != rx_test_internal_error))
3575 x = get_burst (&bf->stk_test_pos, app_closure, stop);
3578 case rx_get_burst_continuation:
3579 search_state.saved_bf = bf;
3580 test_pc = rx_test_backtrack_return;
3581 goto test_return_continuation;
3582 resume_continuation_3:
3583 bf = search_state.saved_bf;
3585 case rx_get_burst_no_more:
3586 /* Since we've been here before, it is some kind of
3587 * error that we can't return.
3589 case rx_get_burst_error:
3590 search_state.test_ret = rx_test_internal_error;
3591 goto test_do_return;
3592 case rx_get_burst_ok:
3596 search_state.test_pos = bf->stk_test_pos;
3597 goto while_non_det_options;
3602 /* Because the superstate NFA is lazily constructed,
3603 * and in fact may erode from underneath us, we sometimes
3604 * have to construct the next instruction from the hard way.
3605 * This invokes one step in the lazy-conversion.
3607 search_state.ifr = rx_handle_cache_miss (&rxb->rx,
3610 search_state.ifr->data_2);
3611 if (!search_state.ifr)
3613 search_state.test_ret = rx_test_internal_error;
3614 goto test_do_return;
3619 /* RX_BACKTRACK means that we've reached the empty
3620 * superstate, indicating that match can't succeed
3623 goto test_do_return;
3627 case rx_num_instructions:
3628 search_state.ret_val = 0;
3629 goto test_do_return;
3631 goto pseudo_while_1;
3634 /* Healthy exits from the test-match loop do a
3635 * `goto return_from_test_match' On the other hand,
3636 * we might end up here.
3638 error_in_testing_match:
3639 test_state = rx_test_error;
3640 goto test_returns_to_search;
3642 /***** fastmap/search loop body
3643 * considering the results testing for a match
3646 return_from_test_match:
3648 if (search_state.best_last_l >= 0)
3650 if (regs && (regs->start != search_state.best_lparen))
3652 bcopy (search_state.best_lparen, regs->start,
3653 regs->num_regs * sizeof (int));
3654 bcopy (search_state.best_rparen, regs->end,
3655 regs->num_regs * sizeof (int));
3657 if (regs && !rxb->no_sub)
3660 int bound = (regs->num_regs < search_state.num_regs
3662 : search_state.num_regs);
3663 regoff_t * s = regs->start;
3664 regoff_t * e = regs->end;
3665 for (q = search_state.best_last_l + 1; q < bound; ++q)
3668 search_state.ret_val = search_state.best_lparen[0];
3669 test_state = rx_test_ok;
3670 goto test_returns_to_search;
3674 test_state = rx_test_fail;
3675 goto test_returns_to_search;
3678 test_return_continuation:
3679 search_state.test_match_resume_pt = test_pc;
3680 test_state = rx_test_continuation;
3681 goto test_returns_to_search;
3687 #endif /* RX_WANT_RX_DEFS */
3691 #else /* RX_WANT_SE_DEFS */
3692 /* Integers are used to represent side effects.
3694 * Simple side effects are given negative integer names by these enums.
3696 * Non-negative names are reserved for complex effects.
3698 * Complex effects are those that take arguments. For example,
3699 * a register assignment associated with a group is complex because
3700 * it requires an argument to tell which group is being matched.
3702 * The integer name of a complex effect is an index into rxb->se_params.
3705 RX_DEF_SE(1, re_se_try, = -1) /* Epsilon from start state */
3707 RX_DEF_SE(0, re_se_pushback, = re_se_try - 1)
3708 RX_DEF_SE(0, re_se_push0, = re_se_pushback -1)
3709 RX_DEF_SE(0, re_se_pushpos, = re_se_push0 - 1)
3710 RX_DEF_SE(0, re_se_chkpos, = re_se_pushpos -1)
3711 RX_DEF_SE(0, re_se_poppos, = re_se_chkpos - 1)
3713 RX_DEF_SE(1, re_se_at_dot, = re_se_poppos - 1) /* Emacs only */
3714 RX_DEF_SE(0, re_se_syntax, = re_se_at_dot - 1) /* Emacs only */
3715 RX_DEF_SE(0, re_se_not_syntax, = re_se_syntax - 1) /* Emacs only */
3717 RX_DEF_SE(1, re_se_begbuf, = re_se_not_syntax - 1) /* match beginning of buffer */
3718 RX_DEF_SE(1, re_se_hat, = re_se_begbuf - 1) /* match beginning of line */
3720 RX_DEF_SE(1, re_se_wordbeg, = re_se_hat - 1)
3721 RX_DEF_SE(1, re_se_wordbound, = re_se_wordbeg - 1)
3722 RX_DEF_SE(1, re_se_notwordbound, = re_se_wordbound - 1)
3724 RX_DEF_SE(1, re_se_wordend, = re_se_notwordbound - 1)
3725 RX_DEF_SE(1, re_se_endbuf, = re_se_wordend - 1)
3727 /* This fails except at the end of a line.
3728 * It deserves to go here since it is typicly one of the last steps
3731 RX_DEF_SE(1, re_se_dollar, = re_se_endbuf - 1)
3733 /* Simple effects: */
3734 RX_DEF_SE(1, re_se_fail, = re_se_dollar - 1)
3736 /* Complex effects. These are used in the 'se' field of
3737 * a struct re_se_params. Indexes into the se array
3738 * are stored as instructions on nfa edges.
3740 RX_DEF_CPLX_SE(1, re_se_win, = 0)
3741 RX_DEF_CPLX_SE(1, re_se_lparen, = re_se_win + 1)
3742 RX_DEF_CPLX_SE(1, re_se_rparen, = re_se_lparen + 1)
3743 RX_DEF_CPLX_SE(0, re_se_backref, = re_se_rparen + 1)
3744 RX_DEF_CPLX_SE(0, re_se_iter, = re_se_backref + 1)
3745 RX_DEF_CPLX_SE(0, re_se_end_iter, = re_se_iter + 1)
3746 RX_DEF_CPLX_SE(0, re_se_tv, = re_se_end_iter + 1)
3750 #if RX_WANT_SE_DEFS != 1