-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtmc-parse.c
103 lines (83 loc) · 1.54 KB
/
tmc-parse.c
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
#include <ctype.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <str.h>
#include <tmc.h>
#include <tmc-parse.h>
enum id {
tok_null,
tok_eof,
tok_err,
tok_colon,
tok_ignore,
tok_invert,
tok_left,
tok_name,
tok_pipe,
tok_right,
tok_semi,
};
enum context {
st_error,
st_name,
st_write,
st_shift,
st_chld,
};
struct token {
enum id type;
uint8_t *value;
size_t length;
};
/* lex - store the next token from buffer in token */
static size_t lex(struct token *token, uint8_t *buffer, size_t length);
size_t
lex(struct token *token, uint8_t *buffer, size_t length)
{
static enum id table[256] = {
[':'] = tok_colon,
['|'] = tok_pipe,
[';'] = tok_semi,
['>'] = tok_left,
['<'] = tok_right,
['^'] = tok_invert,
['_'] = tok_ignore,
};
size_t extent;
*token = (struct token){0};
extent = eat_spaces(buffer, length);
if (extent == length) {
token->type = tok_eof;
return extent;
}
buffer += extent, length -= extent;
token->value = buffer;
token->type = table[buffer[extent]];
if (token->type) {
token->length = 1;
return extent + 1;
}
token->type = tok_name;
extent += token->length = eat_ident(buffer, length);
return extent;
}
void
parse(struct parse *result, uint8_t *buffer, size_t length)
{
struct token token[1];
size_t offset=0;
*result = (struct parse){0};
if (!length) return;
while (!result->error) {
offset += lex(token, buffer, length);
if (token->type == tok_eof) return;
}
return;
}
void
parse_error(struct parse *result, char *msg)
{
return;
}