]>
sourceware.org Git - systemtap.git/blob - stapregex-dfa.h
2 // Copyright (C) 2012-2013 Red Hat Inc.
4 // This file is part of systemtap, and is free software. You can
5 // redistribute it and/or modify it under the terms of the GNU General
6 // Public License (GPL); either version 2, or (at your option) any
11 // This file incorporates code from the re2c project; please see
12 // the file README.stapregex for details.
14 #ifndef STAPREGEX_DFA_H
15 #define STAPREGEX_DFA_H
25 class translator_output
; /* from translator-output.h */
29 struct regexp
; /* from stapregex-tree.h */
30 union ins
; /* from stapregex-tree.h */
35 /* Coordinates of a subexpression map item m[t,s]: */
36 typedef std::pair
<unsigned, unsigned> map_item
;
38 /* A tagged DFA transition can have a number of these instructions
39 attached, which are able to assign the current position to specific
40 map items, or to reorder the existing elements of the map: */
43 bool save_pos
; // -- if true, assign position; if false, copy from's value
45 typedef std::list
<tdfa_insn
> tdfa_action
;
47 std::ostream
& operator << (std::ostream
&o
, const tdfa_action
& a
);
49 /* The arc_priority data type is a cunning way to represent transition
50 priorities, necessitated by the fact that we have FORK opcodes (two
51 outgoing e-transitions) which can lead to further FORK opcodes, &c,
52 requiring a binary-subdivision style of priority assignment:
54 -> 3/4 ... and so forth
56 -> 1/2 --> 2/4 ... and so forth
58 / ---> 1/4 ... and so forth
60 0 ----> 0 ----> 0 ... and so forth
62 Our trick is pretty much just to allocate the possible values of an
63 unsigned long in binary-search fashion.
65 XXX: For a 64-bit unsigned long type, this allows a chain of FORK
66 opcodes around 64 units long (without intervening CHAR match
67 insns), at which point things start to get funky. Be sure to keep
68 an eye on whether this turns out to be enough in practice. */
69 typedef std::pair
<unsigned long, unsigned> arc_priority
;
70 arc_priority
refine_higher(const arc_priority
& a
);
71 arc_priority
refine_lower(const arc_priority
& a
);
72 int arc_compare(const arc_priority
& a
, const arc_priority
& b
);
74 std::ostream
& operator << (std::ostream
&o
, const arc_priority
& p
);
76 /* When constructing tagged DFA sets from ins, we need to keep track
77 of a set of instructions together with further bookkeeping
78 information (relative preference/priority, map items affected). */
81 arc_priority priority
; // -- used in tagged e-closure computation
82 std::list
<map_item
> map_items
;
83 std::set
<ins
*> parents
; // -- used for infinite-loop-detection
85 typedef std::list
<kernel_point
> state_kernel
;
87 /* Corresponds to a tagged-DFA transition arc, complete with
88 subexpression map reordering and such. */
90 char lb
, ub
; // -- segment [lb, ub]
93 state_kernel
*reach_pairs
; // -- for the subset-construction
96 void emit_jump (translator_output
*o
, const dfa
*d
) const;
97 void emit_final (translator_output
*o
, const dfa
*d
) const;
101 unsigned label
; // -- index of state in dfa
102 state
*next
; // -- store dfa states as a linked list
103 state_kernel
*kernel
; // -- set of corresponding ins coordinates
104 /* NB: our usage of the term 'kernel' differs from re2c's slightly
105 -- there is no real need to distinguish NFA edges inside the
106 state from outgoing edges, (XXX) as far as I am aware. */
108 bool accepts
; // -- is this a final state?
109 unsigned accept_outcome
;
110 tdfa_action finalizer
; // -- run after accepting
112 std::list
<span
> spans
;
114 state (state_kernel
*kernel
);
116 void emit (translator_output
*o
, const dfa
*d
) const;
118 void print (translator_output
*o
) const;
121 // ------------------------------------------------------------------------
125 state
*first
, *last
; // -- store dfa states as a linked list
128 // Infrastructure to deal with tagging:
130 tdfa_action initializer
; // -- run before entering start state
131 std::vector
<std::string
> outcome_snippets
;
133 dfa (ins
*i
, int ntags
, std::vector
<std::string
>& outcome_snippets
);
136 void emit (translator_output
*o
) const;
137 void emit_tagsave (translator_output
*o
, std::string tag_states
,
138 std::string tag_vals
, std::string tag_count
) const;
140 void print (translator_output
*o
) const;
141 void print (std::ostream
& o
) const;
144 state
*add_state (state
* s
);
145 state
*find_equivalent (state
*s
, tdfa_action
&r
);
148 std::ostream
& operator << (std::ostream
&o
, const dfa
& d
);
149 std::ostream
& operator << (std::ostream
&o
, const dfa
* d
);
151 /* Produces a dfa that runs the specified code snippets based on match
152 or fail outcomes for an unanchored (by default) match of re. */
153 dfa
*stapregex_compile (regexp
*re
, const std::string
& match_snippet
, const std::string
& fail_snippet
);
159 /* vim: set sw=2 ts=8 cino=>4,n-2,{2,^-2,t0,(0,u0,w1,M1 : */
This page took 0.042894 seconds and 5 git commands to generate.