summaryrefslogtreecommitdiff
path: root/tools/re2c/re.h
blob: b45208b9509e8513cd19a48e7a2588e3beabf6f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#ifndef re2c_re_h
#define re2c_re_h

#include <stdio.h>
#include "tools/re2c/token.h"
#include "tools/re2c/ins.h"

typedef struct extop {
    char    op;
    int	    minsize;
    int	    maxsize;
} ExtOp;

typedef struct CharPtn {
    unsigned int	card;
    struct CharPtn	*fix;
    struct CharPtn	*nxt;
} CharPtn;

typedef struct CharSet {
    CharPtn	*fix;
    CharPtn	*freeHead, **freeTail;
    CharPtn	*rep[nChars];
    CharPtn	ptn[nChars];
} CharSet;

typedef struct Range {
    struct Range	*next;
    unsigned int	lb, ub;		/* [lb,ub) */
} Range;

static void
Range_init(Range *r, unsigned int l, unsigned int u)
{
    r->next = NULL;
    r->lb = l;
    r->ub = u;
}

static Range *
Range_new(unsigned int l, unsigned int u)
{
    Range *r = malloc(sizeof(Range));
    r->next = NULL;
    r->lb = l;
    r->ub = u;
    return r;
}

static void
Range_copy(Range *ro, const Range *r)
{
    ro->next = NULL;
    ro->lb = r->lb;
    ro->ub = r->ub;
}

static Range *
Range_new_copy(Range *r)
{
    Range *ro = malloc(sizeof(Range));
    ro->next = NULL;
    ro->lb = r->lb;
    ro->ub = r->ub;
    return ro;
}

void Range_out(FILE *, const Range *);

typedef enum {
	NULLOP = 1,
	MATCHOP,
	RULEOP,
	ALTOP,
	CATOP,
	CLOSEOP,
	CLOSEVOP
} RegExpType;

typedef struct RegExp {
    RegExpType	type;
    unsigned int	size;
    union {
	/* for MatchOp */
	Range	*match;
	/* for RuleOp */
	struct {
	    struct RegExp	*exp;
	    struct RegExp	*ctx;
	    Ins		*ins;
	    unsigned int	accept;
	    Token	*code;
	    unsigned int	line;
	} RuleOp;
	/* for AltOp and CatOp*/
	struct {
	    struct RegExp	*exp1, *exp2;
	} AltCatOp;
	/* for CloseOp */
	struct RegExp	*exp;
	/* for CloseVOp*/
	struct {
	    struct RegExp	*exp;
	    int		min;
	    int		max;
	} CloseVOp;
    } d;
} RegExp;

static RegExp *
RegExp_isA(RegExp *r, RegExpType t)
{
    return r->type == t ? r : NULL;
}

void RegExp_split(RegExp*, CharSet*);
void RegExp_calcSize(RegExp*, Char*);
unsigned int RegExp_fixedLength(RegExp*);
void RegExp_compile(RegExp*, Char*, Ins*);
void RegExp_display(RegExp*, FILE *);

static RegExp *
RegExp_new_NullOp(void)
{
    RegExp *r = malloc(sizeof(RegExp));
    r->type = NULLOP;
    return r;
}

static RegExp *
RegExp_new_MatchOp(Range *m)
{
    RegExp *r = malloc(sizeof(RegExp));
    r->type = MATCHOP;
    r->d.match = m;
    return r;
}

RegExp *RegExp_new_RuleOp(RegExp*, RegExp*, Token*, unsigned int);

static RegExp *
RegExp_new_AltOp(RegExp *e1, RegExp *e2)
{
    RegExp *r = malloc(sizeof(RegExp));
    r->type = ALTOP;
    r->d.AltCatOp.exp1 = e1;
    r->d.AltCatOp.exp2 = e2;
    return r;
}

static RegExp *
RegExp_new_CatOp(RegExp *e1, RegExp *e2)
{
    RegExp *r = malloc(sizeof(RegExp));
    r->type = CATOP;
    r->d.AltCatOp.exp1 = e1;
    r->d.AltCatOp.exp2 = e2;
    return r;
}

static RegExp *
RegExp_new_CloseOp(RegExp *e)
{
    RegExp *r = malloc(sizeof(RegExp));
    r->type = CLOSEOP;
    r->d.exp = e;
    return r;
}

static RegExp *
RegExp_new_CloseVOp(RegExp *e, int lb, int ub)
{
    RegExp *r = malloc(sizeof(RegExp));
    r->type = CLOSEVOP;
    r->d.CloseVOp.exp = e;
    r->d.CloseVOp.min = lb;
    r->d.CloseVOp.max = ub;
    return r;
}

extern void genCode(FILE *, RegExp*);
extern RegExp *mkDiff(RegExp*, RegExp*);
extern RegExp *mkDot(void);
extern RegExp *strToRE(SubStr);
extern RegExp *strToCaseInsensitiveRE(SubStr);
extern RegExp *ranToRE(SubStr);
extern RegExp *invToRE(SubStr);

extern RegExp *mkAlt(RegExp*, RegExp*);

#endif