aboutsummaryrefslogtreecommitdiffstats
path: root/preparser.c
blob: 840209fe94cf9904a5dc7811ac8a1fde7f4ca842 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "common.h"


size_t
parse(struct parser_context *ctx, char *code, size_t code_len, size_t *nremovedp)
{
	int end_of_file_reached;
	size_t bytes_parsed = 0;

	end_of_file_reached = ctx->end_of_file_reached;
	ctx->end_of_file_reached = 0;
	*nremovedp = 0;

	while (ctx->preparser_offset < code_len) {
		if (code[ctx->preparser_offset] == '\0') {
			if (!ctx->tty_input)
				weprintf("ignoring NUL byte at line %zu\n", ctx->preparser_line_number);
			memmove(&code[ctx->preparser_offset],
			        &code[ctx->preparser_offset + 1],
			        (code_len -= 1) - ctx->preparser_offset);
			*nremovedp += 1;

		} else if (code[ctx->preparser_offset] == '\n') {
			ctx->preparser_line_number += 1;
			ctx->preparser_offset += 1;

		} else if (code[ctx->preparser_offset] == '\\') {
			if (ctx->preparser_offset + 1 == code_len)
				break;
			if (code[ctx->preparser_offset + 1] == '\n') {
				bytes_parsed += parse_preparsed(ctx, &code[bytes_parsed], ctx->preparser_offset - bytes_parsed);
				memmove(&code[ctx->preparser_offset],
				        &code[ctx->preparser_offset + 2],
				        (code_len -= 2) - ctx->preparser_offset);
				*nremovedp += 2;
				ctx->line_continuations += 1;
			} else {
				ctx->preparser_offset += 2;
			}

		} else {
			ctx->preparser_offset += 1;
		}
	}

	ctx->end_of_file_reached = end_of_file_reached;
	bytes_parsed += parse_preparsed(ctx, &code[bytes_parsed], ctx->preparser_offset - bytes_parsed);
	ctx->preparser_offset -= bytes_parsed;
	return bytes_parsed;
}