1 #ifndef re2c_dfa_h
2 #define re2c_dfa_h
3
4 #include <stdio.h>
5 #include "tools/re2c/re.h"
6
7 extern void prtCh(FILE *, unsigned char);
8 extern void printSpan(FILE *, unsigned int, unsigned int);
9
10 struct DFA;
11 struct State;
12
13 typedef enum {
14 MATCHACT = 1,
15 ENTERACT,
16 SAVEMATCHACT,
17 MOVEACT,
18 ACCEPTACT,
19 RULEACT
20 } ActionType;
21
22 typedef struct Action {
23 struct State *state;
24 ActionType type;
25 union {
26 /* data for Enter */
27 unsigned int label;
28 /* data for SaveMatch */
29 unsigned int selector;
30 /* data for Accept */
31 struct {
32 unsigned int nRules;
33 unsigned int *saves;
34 struct State **rules;
35 } Accept;
36 /* data for Rule */
37 RegExp *rule; /* RuleOp */
38 } d;
39 } Action;
40
41 void Action_emit(Action*, FILE *, int *);
42
43 typedef struct Span {
44 unsigned int ub;
45 struct State *to;
46 } Span;
47
48 unsigned int Span_show(Span*, FILE *, unsigned int);
49
50 typedef struct Go {
51 unsigned int nSpans;
52 Span *span;
53 } Go;
54
55 typedef struct State {
56 unsigned int label;
57 RegExp *rule; /* RuleOp */
58 struct State *next;
59 struct State *link;
60 unsigned int depth; /* for finding SCCs */
61 unsigned int kCount;
62 Ins **kernel;
63 unsigned int isBase:1;
64 Go go;
65 Action *action;
66 } State;
67
68 void Go_genGoto(Go*, FILE *, State*, State*, int*);
69 void Go_genBase(Go*, FILE *, State*, State*, int*);
70 void Go_genLinear(Go*, FILE *, State*, State*, int*);
71 void Go_genBinary(Go*, FILE *, State*, State*, int*);
72 void Go_genSwitch(Go*, FILE *, State*, State*, int*);
73 void Go_compact(Go*);
74 void Go_unmap(Go*, Go*, State*);
75
76 State *State_new(void);
77 void State_delete(State*);
78 void State_emit(State*, FILE *, int *);
79 void State_out(FILE *, const State*);
80
81 typedef struct DFA {
82 unsigned int lbChar;
83 unsigned int ubChar;
84 unsigned int nStates;
85 State *head, **tail;
86 State *toDo;
87 } DFA;
88
89 DFA *DFA_new(Ins*, unsigned int, unsigned int, unsigned int, Char*);
90 void DFA_delete(DFA*);
91 void DFA_addState(DFA*, State**, State*);
92 State *DFA_findState(DFA*, Ins**, unsigned int);
93 void DFA_split(DFA*, State*);
94
95 void DFA_findSCCs(DFA*);
96 void DFA_emit(DFA*, FILE *);
97 void DFA_out(FILE *, const DFA*);
98
99 static Action *
Action_new_Match(State * s)100 Action_new_Match(State *s)
101 {
102 Action *a = malloc(sizeof(Action));
103 a->type = MATCHACT;
104 a->state = s;
105 s->action = a;
106 return a;
107 }
108
109 static Action *
Action_new_Enter(State * s,unsigned int l)110 Action_new_Enter(State *s, unsigned int l)
111 {
112 Action *a = malloc(sizeof(Action));
113 a->type = ENTERACT;
114 a->state = s;
115 a->d.label = l;
116 s->action = a;
117 return a;
118 }
119
120 static Action *
Action_new_Save(State * s,unsigned int i)121 Action_new_Save(State *s, unsigned int i)
122 {
123 Action *a = malloc(sizeof(Action));
124 a->type = SAVEMATCHACT;
125 a->state = s;
126 a->d.selector = i;
127 s->action = a;
128 return a;
129 }
130
131 static Action *
Action_new_Move(State * s)132 Action_new_Move(State *s)
133 {
134 Action *a = malloc(sizeof(Action));
135 a->type = MOVEACT;
136 a->state = s;
137 s->action = a;
138 return a;
139 }
140
141 Action *Action_new_Accept(State*, unsigned int, unsigned int*, State**);
142
143 static Action *
Action_new_Rule(State * s,RegExp * r)144 Action_new_Rule(State *s, RegExp *r) /* RuleOp */
145 {
146 Action *a = malloc(sizeof(Action));
147 a->type = RULEACT;
148 a->state = s;
149 a->d.rule = r;
150 s->action = a;
151 return a;
152 }
153
154 static int
Action_isRule(Action * a)155 Action_isRule(Action *a)
156 {
157 return a->type == RULEACT;
158 }
159
160 static int
Action_isMatch(Action * a)161 Action_isMatch(Action *a)
162 {
163 return a->type == MATCHACT;
164 }
165
166 static int
Action_readAhead(Action * a)167 Action_readAhead(Action *a)
168 {
169 return !Action_isMatch(a) ||
170 (a->state && a->state->next && !Action_isRule(a->state->next->action));
171 }
172
173 #endif
174