-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnfa-parse-re.c
More file actions
194 lines (147 loc) · 3.03 KB
/
nfa-parse-re.c
File metadata and controls
194 lines (147 loc) · 3.03 KB
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
192
193
194
/*
* Regular Expression to Thompson NFA compiler
*
* Copyright (c) 2020-2024 Alexei A. Smekalkine <ikle@ikle.ru>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <limits.h>
#include <peruse/nfa-parse.h>
#include "re-lexer.h"
/* RE recursive descent parser */
static struct nfa_state *re_char (struct re_lexer *o)
{
int c;
if ((c = re_lexer_next (o)) == '\0')
return NULL;
return nfa_state_atom (c);
}
static struct nfa_state *re_range (struct re_lexer *o)
{
int a, b;
if ((a = re_lexer_next (o)) == '\0')
return NULL;
if (re_lexer_peek (o) != '-')
return nfa_state_atom (a);
re_lexer_next (o);
if ((b = re_lexer_next (o)) == '\0')
return NULL;
return nfa_state_range (a, b);
}
static struct nfa_state *re_set (struct re_lexer *o)
{
int c;
struct nfa_state *a, *b;
if ((a = re_range (o)) == NULL)
return NULL;
while ((c = re_lexer_peek (o)) != ']' && c != '\0') {
if ((b = re_range (o)) == NULL)
goto error;
a = nfa_state_union (a, b);
}
return a;
error:
nfa_state_free (a);
return NULL;
}
static struct nfa_state *re_exp (struct re_lexer *o);
static struct nfa_state *re_atom (struct re_lexer *o)
{
int c;
struct nfa_state *a;
if ((c = re_lexer_peek (o)) == '(') {
re_lexer_next (o);
if ((a = re_exp (o)) == NULL)
return NULL;
if (!re_lexer_eat (o, ')'))
goto error;
return a;
}
else if (c == '[') {
re_lexer_next (o);
if ((a = re_set (o)) == NULL)
return NULL;
if (!re_lexer_eat (o, ']'))
goto error;
return a;
}
else if (c == '\\') {
re_lexer_next (o);
return re_char (o);
}
else if (c == '.') {
re_lexer_next (o);
return nfa_state_range (0, INT_MAX);
}
return re_char (o);
error:
nfa_state_free (a);
return NULL;
}
static struct nfa_state *re_piece (struct re_lexer *o)
{
struct nfa_state *a;
int c;
if ((a = re_atom (o)) == NULL)
return NULL;
while ((c = re_lexer_peek (o)) != '\0')
switch (c) {
case '?':
re_lexer_next (o);
a = nfa_state_opt (a);
break;
case '*':
re_lexer_next (o);
a = nfa_state_star (a);
break;
case '+':
re_lexer_next (o);
a = nfa_state_plus (a);
break;
default:
return a;
}
return a;
}
static struct nfa_state *re_branch (struct re_lexer *o)
{
struct nfa_state *a, *b;
int c;
if ((a = re_piece (o)) == NULL)
return NULL;
while ((c = re_lexer_peek (o)) != '\0' && c != ')' && c != '|') {
if ((b = re_piece (o)) == NULL)
goto error;
a = nfa_state_cat (a, b);
}
return a;
error:
nfa_state_free (a);
return NULL;
}
static struct nfa_state *re_exp (struct re_lexer *o)
{
struct nfa_state *a, *b;
if ((a = re_branch (o)) == NULL)
return NULL;
while (re_lexer_peek (o) == '|') {
re_lexer_next (o);
if ((b = re_branch (o)) == NULL)
goto error;
a = nfa_state_union (a, b);
}
return a;
error:
nfa_state_free (a);
return NULL;
}
struct nfa_state *nfa_parse_re (const char *re, int color)
{
struct re_lexer o;
struct nfa_state *start;
re_lexer_init (&o, re);
if ((start = re_exp (&o)) == NULL)
return NULL;
nfa_state_color (start, color);
return start;
}