-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.c
More file actions
88 lines (77 loc) · 1.71 KB
/
parser.c
File metadata and controls
88 lines (77 loc) · 1.71 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
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "parser.h"
void parser_destroy(struct parser *p)
{
if (p != NULL)
{
free(p);
}
}
struct parser *
parser_init(const unsigned *classes,
const struct parser_definition *def)
{
struct parser *ret = malloc(sizeof(*ret));
if (ret != NULL)
{
memset(ret, 0, sizeof(*ret));
ret->classes = classes;
ret->def = def;
ret->state = def->start_state;
}
return ret;
}
void parser_reset(struct parser *p)
{
p->state = p->def->start_state;
}
const struct parser_event *
parser_feed(struct parser *p, const uint8_t c)
{
const unsigned type = p->classes[c];
p->e1.next = p->e2.next = 0;
const struct parser_state_transition *state = p->def->states[p->state];
const size_t n = p->def->states_n[p->state];
bool matched = false;
for (unsigned i = 0; i < n; i++)
{
const int when = state[i].when;
if (state[i].when <= 0xFF)
{
matched = (c == when);
}
else if (state[i].when == ANY)
{
matched = true;
}
else if (state[i].when > 0xFF)
{
matched = (type & when);
}
else
{
matched = false;
}
if (matched)
{
state[i].act1(&p->e1, c);
if (state[i].act2 != NULL)
{
p->e1.next = &p->e2;
state[i].act2(&p->e2, c);
}
p->state = state[i].dest;
break;
}
}
return &p->e1;
}
static const unsigned classes[0xFF] = {0x00};
const unsigned *
parser_no_classes(void)
{
return classes;
}