File: gsx_parse.c

package info (click to toggle)
librnd 4.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 12,812 kB
  • sloc: ansic: 126,990; sh: 2,602; makefile: 2,145; awk: 7
file content (282 lines) | stat: -rw-r--r-- 6,500 bytes parent folder | download | duplicates (4)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
Copyright (c) 2016 Tibor 'Igor2' Palinkas
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.
3. Neither the name of the Author nor the names of contributors
   may be used to endorse or promote products derived from this software
   without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.

Contact: gensexpr {at} igor2.repo.hu
Project page: http://repo.hu/projects/gensexpr
*/

/* Event parser */

#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "gsx_parse.h"

enum {
	ST_NORMAL,
	ST_ATOM, /* unquoted atom */
	ST_DQ,   /* in double quote */
	ST_SQ,   /* in single quote */
	ST_BRC,  /* in brace */
	ST_DQBS, /* in double quote, previous char was a backslash */
	ST_SQBS, /* in single quote, previous char was a backslash */
	ST_QEND, /* quote end - need to be followed by a whitepsace or ) */
	ST_ERR,  /* error condition (stopped parsing) */
	ST_EOE   /* end-of-file condition (stopped parsing) */
};

static void update_loc(gsx_parse_t *ctx, int chr)
{
	ctx->offs++;
	if (chr == '\n') {
		ctx->col = 0;
		ctx->line++;
	}
	else
		ctx->col++;
}

#define error(ctx, chr, errmsg) \
do { \
	ctx->cb(ctx, GSX_EV_ERROR, errmsg); \
	ctx->pstate = ST_ERR; \
	update_loc(ctx, chr); \
	return GSX_RES_ERROR; \
} while(0)

static void flush_atom(gsx_parse_t *ctx)
{
	ctx->atom[ctx->used] = '\0';
	ctx->cb(ctx, GSX_EV_ATOM, ctx->atom);
	ctx->used = 0;
}

int gsx_parse_char(gsx_parse_t *ctx, int chr)
{
	int qc, stbs;

	if (ctx->pstate == ST_ERR) {
		update_loc(ctx, chr);
		return GSX_RES_ERROR;
	}

	if (ctx->pstate == ST_EOE) {
		update_loc(ctx, chr);
		return GSX_RES_EOE;
	}

	if (chr == EOF) /* we are not in ST_EOE if we got here */
			error(ctx, chr, "premature end of expression");

	if ((ctx->line_comment_char != 0) && (chr == ctx->line_comment_char) && (ctx->last_newline || (ctx->offs == 0)))
		ctx->in_comment = 1;
	ctx->last_newline = (chr == '\r') || (chr == '\n');
	if (ctx->last_newline && ctx->in_comment) {
		ctx->in_comment = 0;
		goto skip_comment;
	}
	if (ctx->in_comment)
		goto skip_comment;

	switch(ctx->pstate) {
		case ST_DQBS:
			ctx->pstate = ST_DQ;
			goto append;

		case ST_SQBS:
			ctx->pstate = ST_SQ;
			goto append;

		case ST_DQ:
			qc = '"';
			stbs = ST_DQBS;
			goto quote;

		case ST_SQ:
			qc = '\'';
			stbs = ST_SQBS;
			quote:;
			if (chr == qc)
				ctx->pstate = ST_QEND;
			else if (chr == '\\')
				ctx->pstate = stbs;
			else
				goto append;
			break;

		case ST_BRC:
			stbs = ST_BRC;
			qc = '}';
			goto quote;

		case ST_QEND:
			if (isspace(chr)) {
				if (ctx->depth == 0)
					goto err_atom0;
				ctx->pstate = ST_NORMAL;
				flush_atom(ctx);
				break;
			}
			if (chr == ')') {
				if (ctx->depth == 0)
					goto err_atom0;
				flush_atom(ctx);
				goto close;
			}

			if (chr == '(') {
				flush_atom(ctx);
				goto open;
			}

			/* some dialects allow missing space after quote close - in which case we need to glue the atoms */
			ctx->pstate = ST_ATOM;
			goto append;

		case ST_ATOM:
			if (isspace(chr)) {
				if (ctx->depth == 0)
					goto err_atom0;
				ctx->pstate = ST_NORMAL;
				flush_atom(ctx);
				break;
			}
			if (chr == ')') {
				if (ctx->depth == 0)
					goto err_atom0;
				flush_atom(ctx);
				goto close;
			}
			if (chr == '"') {
				/* glue "foo"bar to become foobar */
				qc = '"';
				stbs = ST_DQBS;
				ctx->pstate = ST_DQ;
				break;
			}
			if (ctx->allow_nospace_open && (chr == '(')) {
				if (ctx->depth == 0)
					goto err_atom0;
				flush_atom(ctx);
				goto open;
			}
			if ((chr == '(') || (chr == '\''))
				error(ctx, chr, "unquoted atom with special character in it");
			goto append;

		case ST_NORMAL:
			if (isspace(chr))
				break; /* excess whitepsace between atoms */

			switch(chr) {
				case '"':
					ctx->pstate = ST_DQ;
					break;

				case '\'':
					ctx->pstate = ST_SQ;
					break;

				case '{':
					if (ctx->brace_quote) {
						ctx->pstate = ST_BRC;
						break;
					}
					ctx->pstate = ST_ATOM;
					goto append;

				case '(':
					open:;
					ctx->cb(ctx, GSX_EV_OPEN, "(");
					ctx->depth++;
					break;

				case ')':
					close:;
					ctx->depth--;
					ctx->cb(ctx, GSX_EV_CLOSE, ")");
					if (ctx->depth == 0) {
						ctx->pstate = ST_EOE;
						update_loc(ctx, chr);
						return GSX_RES_EOE;
					}
					ctx->pstate = ST_NORMAL;
					break;

				default: /* normal character - start of a new, unquoted atom */
					ctx->pstate = ST_ATOM;
					goto append;
			}
			break;
			
	}

	skip_comment:;
	update_loc(ctx, chr);
	return GSX_RES_NEXT;

	append:;
	if (ctx->used >= ctx->alloced) {
		/* growth strategy */
		if (ctx->alloced < 1024)
			ctx->alloced += 128;
		else if (ctx->alloced > 1024*1024)
			ctx->alloced += 1024*1024;
		else
			ctx->alloced *= 2;

		/* realloc, make sure there's 1 byte of extra room for the \0 */
		ctx->atom = realloc(ctx->atom, ctx->alloced+1);
	}
	ctx->atom[ctx->used] = chr;
	ctx->used++;
	update_loc(ctx, chr);
	return GSX_RES_NEXT;

	err_atom0:
	error(ctx, ' ', "Expression not wrapped in ()");
}


void gsx_parse_init(gsx_parse_t *ctx)
{
	ctx->offs = ctx->line = ctx->col = ctx->depth = 0;

	ctx->atom = NULL;
	ctx->used = ctx->alloced = 0;
	ctx->pstate = ST_NORMAL;
	ctx->in_comment = 0;
	ctx->last_newline = 0;
}

void gsx_parse_uninit(gsx_parse_t *ctx)
{
	free(ctx->atom);
	ctx->atom = NULL;
	ctx->used = ctx->alloced = 0;
}