145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#ifndef re2c_dfa_h
245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#define re2c_dfa_h
345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include <stdio.h>
545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#include "tools/re2c/re.h"
645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgextern void prtCh(FILE *, unsigned char);
845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgextern void printSpan(FILE *, unsigned int, unsigned int);
945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstruct DFA;
1145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstruct State;
1245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
1345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef enum {
1445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    MATCHACT = 1,
1545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    ENTERACT,
1645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    SAVEMATCHACT,
1745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    MOVEACT,
1845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    ACCEPTACT,
1945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    RULEACT
2045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} ActionType;
2145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
2245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct Action {
2345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    struct State	*state;
2445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    ActionType		type;
2545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    union {
2645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	/* data for Enter */
2745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	unsigned int		label;
2845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	/* data for SaveMatch */
2945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	unsigned int		selector;
3045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	/* data for Accept */
3145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	struct {
3245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    unsigned int		nRules;
3345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    unsigned int		*saves;
3445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	    struct State	**rules;
3545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	} Accept;
3645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	/* data for Rule */
3745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	RegExp		*rule;	/* RuleOp */
3845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    } d;
3945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} Action;
4045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid Action_emit(Action*, FILE *, int *);
4245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct Span {
4445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int		ub;
4545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    struct State	*to;
4645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} Span;
4745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
4845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgunsigned int Span_show(Span*, FILE *, unsigned int);
4945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct Go {
5145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int	nSpans;
5245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Span		*span;
5345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} Go;
5445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
5545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct State {
5645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int	label;
5745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    RegExp		*rule;	/* RuleOp */
5845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    struct State	*next;
5945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    struct State	*link;
6045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int	depth;		/* for finding SCCs */
6145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int	kCount;
6245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Ins			**kernel;
6345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int	isBase:1;
6445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Go			go;
6545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Action		*action;
6645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} State;
6745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
6845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid Go_genGoto(Go*, FILE *, State*, State*, int*);
6945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid Go_genBase(Go*, FILE *, State*, State*, int*);
7045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid Go_genLinear(Go*, FILE *, State*, State*, int*);
7145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid Go_genBinary(Go*, FILE *, State*, State*, int*);
7245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid Go_genSwitch(Go*, FILE *, State*, State*, int*);
7345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid Go_compact(Go*);
7445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid Go_unmap(Go*, Go*, State*);
7545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
7645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgState *State_new(void);
7745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid State_delete(State*);
7845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid State_emit(State*, FILE *, int *);
7945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid State_out(FILE *, const State*);
8045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgtypedef struct DFA {
8245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int	lbChar;
8345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int	ubChar;
8445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    unsigned int	nStates;
8545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    State		*head, **tail;
8645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    State		*toDo;
8745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org} DFA;
8845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
8945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgDFA *DFA_new(Ins*, unsigned int, unsigned int, unsigned int, Char*);
9045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid DFA_delete(DFA*);
9145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid DFA_addState(DFA*, State**, State*);
9245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgState *DFA_findState(DFA*, Ins**, unsigned int);
9345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid DFA_split(DFA*, State*);
9445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid DFA_findSCCs(DFA*);
9645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid DFA_emit(DFA*, FILE *);
9745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgvoid DFA_out(FILE *, const DFA*);
9845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
9945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic Action *
10045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgAction_new_Match(State *s)
10145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
10245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Action *a = malloc(sizeof(Action));
10345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->type = MATCHACT;
10445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->state = s;
10545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    s->action = a;
10645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return a;
10745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
10845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
10945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic Action *
11045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgAction_new_Enter(State *s, unsigned int l)
11145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
11245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Action *a = malloc(sizeof(Action));
11345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->type = ENTERACT;
11445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->state = s;
11545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->d.label = l;
11645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    s->action = a;
11745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return a;
11845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
11945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
12045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic Action *
12145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgAction_new_Save(State *s, unsigned int i)
12245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
12345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Action *a = malloc(sizeof(Action));
12445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->type = SAVEMATCHACT;
12545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->state = s;
12645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->d.selector = i;
12745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    s->action = a;
12845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return a;
12945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
13045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
13145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic Action *
13245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgAction_new_Move(State *s)
13345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
13445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Action *a = malloc(sizeof(Action));
13545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->type = MOVEACT;
13645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->state = s;
13745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    s->action = a;
13845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return a;
13945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
14045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgAction *Action_new_Accept(State*, unsigned int, unsigned int*, State**);
14245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
14345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic Action *
14445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgAction_new_Rule(State *s, RegExp *r) /* RuleOp */
14545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
14645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    Action *a = malloc(sizeof(Action));
14745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->type = RULEACT;
14845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->state = s;
14945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    a->d.rule = r;
15045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    s->action = a;
15145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return a;
15245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
15345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
15445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int
15545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgAction_isRule(Action *a)
15645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
15745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return a->type == RULEACT;
15845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
15945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int
16145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgAction_isMatch(Action *a)
16245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
16345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return a->type == MATCHACT;
16445afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
16545afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
16645afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgstatic int
16745afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.orgAction_readAhead(Action *a)
16845afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org{
16945afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org    return !Action_isMatch(a) ||
17045afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org	(a->state && a->state->next && !Action_isRule(a->state->next->action));
17145afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org}
17245afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org
17345afe016bed87b9c6946184709058b39ede3f77ajwong@chromium.org#endif
174