blob: 61c016fe22e1d448a701bdb00ab6106f641a068f (
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
192
193
194
195
196
197
198
199
200
|
/* definitions */
%x sComment
%x sID
%x sValue
%x sQStr
%option noyywrap
ID ([-[:alnum:]_]+)
SPACE ([[:space:]]{-}[\n])
%{
/* get rid of compiler warning */
#define YY_NO_INPUT 1
#include <string.h>
#include "cparse_def.h"
#include "cparse.h"
#define STR_BUF_INC 4096
static int node_deactivated = 0;
static char *str_buf = NULL;
static char *out_buf = NULL;
static char *str_ptr = NULL;
static size_t str_buf_len = 0;
static void
prepare_buffers(size_t add_len)
{
size_t slen = str_ptr - str_buf;
if (str_buf && (slen + add_len) < str_buf_len) {
// nothing to do
return;
}
str_buf_len += STR_BUF_INC;
str_buf = realloc(str_buf, str_buf_len);
out_buf = realloc(out_buf, str_buf_len);
if (!str_buf || !out_buf) {
printf("realloc failed\n");
exit(1);
}
str_ptr = str_buf + slen;
}
static void
append_str(char *text)
{
size_t tlen = strlen(text);
prepare_buffers(tlen);
strcpy(str_ptr, text);
str_ptr += tlen;
}
static void
set_ret_str()
{
prepare_buffers(0);
*str_ptr = 0;
strcpy(out_buf, str_buf);
str_ptr = str_buf;
}
%}
%%
<INITIAL>"/*" {
BEGIN(sComment);
}
<sComment>[^*\n]* {
append_str(cparse_text);
}
<sComment>\*[^/] {
append_str(cparse_text);
}
<sComment>\n {
append_str(cparse_text);
++cparse_lineno;
}
<sComment>"*/" {
char *tmp;
size_t tlen;
set_ret_str();
/* need to strip out leading or trailing space */
tmp = out_buf;
tlen = strlen(tmp);
if (tlen > 0 && tmp[tlen - 1] == ' ') {
tmp[tlen - 1] = 0;
--tlen;
}
if (tlen > 0 && tmp[0] == ' ') {
++tmp;
}
cparse_lval.str = strdup(tmp);
BEGIN(INITIAL);
return COMMENT;
}
<INITIAL>! {
node_deactivated = 1;
}
<INITIAL>{SPACE}+ {
}
<INITIAL>\n {
++cparse_lineno;
}
<INITIAL>\} {
node_deactivated = 0;
return RIGHTB;
}
<INITIAL>{ID} {
cparse_lval.str = strdup(cparse_text);
cparse_lval.deactivated = node_deactivated;
node_deactivated = 0;
BEGIN(sID);
return NODE;
}
<sID>:?{SPACE}+[^{\n] {
unput(cparse_text[cparse_leng - 1]);
BEGIN(sValue);
}
<sID>{SPACE}+ {
}
<sID>\{ {
BEGIN(INITIAL);
return LEFTB;
}
<sID>\n {
++cparse_lineno;
BEGIN(INITIAL);
}
<sValue>{SPACE}+ {
/* ignore spaces */
}
<sValue>\" {
/* quoted string */
BEGIN(sQStr);
}
<sQStr>[^\"\\\n]+ {
append_str(cparse_text);
}
<sQStr>\\. {
/* this will consume the \" sequence */
append_str(cparse_text);
}
<sQStr>\n {
append_str(cparse_text);
++cparse_lineno;
}
<sQStr>\" {
set_ret_str();
cparse_lval.str = strdup(out_buf);
BEGIN(sValue);
return VALUE;
}
<sValue>[^{"[:space:]][^{[:space:]]* {
/* unquoted string */
cparse_lval.str = strdup(cparse_text);
return VALUE;
}
<sValue>\{ {
BEGIN(INITIAL);
return LEFTB;
}
<sValue>\n {
++cparse_lineno;
BEGIN(INITIAL);
}
<*>. {
return SYNTAX_ERROR;
}
%%
/* code */
|