1#ifndef re2c_dfa_h
2#define re2c_dfa_h
3
4#include <stdio.h>
5#include "tools/re2c/re.h"
6
7extern void prtCh(FILE *, unsigned char);
8extern void printSpan(FILE *, unsigned int, unsigned int);
9
10struct DFA;
11struct State;
12
13typedef enum {
14    MATCHACT = 1,
15    ENTERACT,
16    SAVEMATCHACT,
17    MOVEACT,
18    ACCEPTACT,
19    RULEACT
20} ActionType;
21
22typedef 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
41void Action_emit(Action*, FILE *, int *);
42
43typedef struct Span {
44    unsigned int		ub;
45    struct State	*to;
46} Span;
47
48unsigned int Span_show(Span*, FILE *, unsigned int);
49
50typedef struct Go {
51    unsigned int	nSpans;
52    Span		*span;
53} Go;
54
55typedef 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
68void Go_genGoto(Go*, FILE *, State*, State*, int*);
69void Go_genBase(Go*, FILE *, State*, State*, int*);
70void Go_genLinear(Go*, FILE *, State*, State*, int*);
71void Go_genBinary(Go*, FILE *, State*, State*, int*);
72void Go_genSwitch(Go*, FILE *, State*, State*, int*);
73void Go_compact(Go*);
74void Go_unmap(Go*, Go*, State*);
75
76State *State_new(void);
77void State_delete(State*);
78void State_emit(State*, FILE *, int *);
79void State_out(FILE *, const State*);
80
81typedef struct DFA {
82    unsigned int	lbChar;
83    unsigned int	ubChar;
84    unsigned int	nStates;
85    State		*head, **tail;
86    State		*toDo;
87} DFA;
88
89DFA *DFA_new(Ins*, unsigned int, unsigned int, unsigned int, Char*);
90void DFA_delete(DFA*);
91void DFA_addState(DFA*, State**, State*);
92State *DFA_findState(DFA*, Ins**, unsigned int);
93void DFA_split(DFA*, State*);
94
95void DFA_findSCCs(DFA*);
96void DFA_emit(DFA*, FILE *);
97void DFA_out(FILE *, const DFA*);
98
99static Action *
100Action_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
109static Action *
110Action_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
120static Action *
121Action_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
131static Action *
132Action_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
141Action *Action_new_Accept(State*, unsigned int, unsigned int*, State**);
142
143static Action *
144Action_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
154static int
155Action_isRule(Action *a)
156{
157    return a->type == RULEACT;
158}
159
160static int
161Action_isMatch(Action *a)
162{
163    return a->type == MATCHACT;
164}
165
166static int
167Action_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