1#ifndef re2c_re_h
2#define re2c_re_h
3
4#include <stdio.h>
5#include "tools/re2c/token.h"
6#include "tools/re2c/ins.h"
7
8typedef struct extop {
9    char    op;
10    int	    minsize;
11    int	    maxsize;
12} ExtOp;
13
14typedef struct CharPtn {
15    unsigned int	card;
16    struct CharPtn	*fix;
17    struct CharPtn	*nxt;
18} CharPtn;
19
20typedef struct CharSet {
21    CharPtn	*fix;
22    CharPtn	*freeHead, **freeTail;
23    CharPtn	*rep[nChars];
24    CharPtn	ptn[nChars];
25} CharSet;
26
27typedef struct Range {
28    struct Range	*next;
29    unsigned int	lb, ub;		/* [lb,ub) */
30} Range;
31
32static void
33Range_init(Range *r, unsigned int l, unsigned int u)
34{
35    r->next = NULL;
36    r->lb = l;
37    r->ub = u;
38}
39
40static Range *
41Range_new(unsigned int l, unsigned int u)
42{
43    Range *r = malloc(sizeof(Range));
44    r->next = NULL;
45    r->lb = l;
46    r->ub = u;
47    return r;
48}
49
50static void
51Range_copy(Range *ro, const Range *r)
52{
53    ro->next = NULL;
54    ro->lb = r->lb;
55    ro->ub = r->ub;
56}
57
58static Range *
59Range_new_copy(Range *r)
60{
61    Range *ro = malloc(sizeof(Range));
62    ro->next = NULL;
63    ro->lb = r->lb;
64    ro->ub = r->ub;
65    return ro;
66}
67
68void Range_out(FILE *, const Range *);
69
70typedef enum {
71	NULLOP = 1,
72	MATCHOP,
73	RULEOP,
74	ALTOP,
75	CATOP,
76	CLOSEOP,
77	CLOSEVOP
78} RegExpType;
79
80typedef struct RegExp {
81    RegExpType	type;
82    unsigned int	size;
83    union {
84	/* for MatchOp */
85	Range	*match;
86	/* for RuleOp */
87	struct {
88	    struct RegExp	*exp;
89	    struct RegExp	*ctx;
90	    Ins		*ins;
91	    unsigned int	accept;
92	    Token	*code;
93	    unsigned int	line;
94	} RuleOp;
95	/* for AltOp and CatOp*/
96	struct {
97	    struct RegExp	*exp1, *exp2;
98	} AltCatOp;
99	/* for CloseOp */
100	struct RegExp	*exp;
101	/* for CloseVOp*/
102	struct {
103	    struct RegExp	*exp;
104	    int		min;
105	    int		max;
106	} CloseVOp;
107    } d;
108} RegExp;
109
110static RegExp *
111RegExp_isA(RegExp *r, RegExpType t)
112{
113    return r->type == t ? r : NULL;
114}
115
116void RegExp_split(RegExp*, CharSet*);
117void RegExp_calcSize(RegExp*, Char*);
118unsigned int RegExp_fixedLength(RegExp*);
119void RegExp_compile(RegExp*, Char*, Ins*);
120void RegExp_display(RegExp*, FILE *);
121
122static RegExp *
123RegExp_new_NullOp(void)
124{
125    RegExp *r = malloc(sizeof(RegExp));
126    r->type = NULLOP;
127    return r;
128}
129
130static RegExp *
131RegExp_new_MatchOp(Range *m)
132{
133    RegExp *r = malloc(sizeof(RegExp));
134    r->type = MATCHOP;
135    r->d.match = m;
136    return r;
137}
138
139RegExp *RegExp_new_RuleOp(RegExp*, RegExp*, Token*, unsigned int);
140
141static RegExp *
142RegExp_new_AltOp(RegExp *e1, RegExp *e2)
143{
144    RegExp *r = malloc(sizeof(RegExp));
145    r->type = ALTOP;
146    r->d.AltCatOp.exp1 = e1;
147    r->d.AltCatOp.exp2 = e2;
148    return r;
149}
150
151static RegExp *
152RegExp_new_CatOp(RegExp *e1, RegExp *e2)
153{
154    RegExp *r = malloc(sizeof(RegExp));
155    r->type = CATOP;
156    r->d.AltCatOp.exp1 = e1;
157    r->d.AltCatOp.exp2 = e2;
158    return r;
159}
160
161static RegExp *
162RegExp_new_CloseOp(RegExp *e)
163{
164    RegExp *r = malloc(sizeof(RegExp));
165    r->type = CLOSEOP;
166    r->d.exp = e;
167    return r;
168}
169
170static RegExp *
171RegExp_new_CloseVOp(RegExp *e, int lb, int ub)
172{
173    RegExp *r = malloc(sizeof(RegExp));
174    r->type = CLOSEVOP;
175    r->d.CloseVOp.exp = e;
176    r->d.CloseVOp.min = lb;
177    r->d.CloseVOp.max = ub;
178    return r;
179}
180
181extern void genCode(FILE *, RegExp*);
182extern RegExp *mkDiff(RegExp*, RegExp*);
183extern RegExp *mkDot(void);
184extern RegExp *strToRE(SubStr);
185extern RegExp *strToCaseInsensitiveRE(SubStr);
186extern RegExp *ranToRE(SubStr);
187extern RegExp *invToRE(SubStr);
188
189extern RegExp *mkAlt(RegExp*, RegExp*);
190
191#endif
192