blob: a92c6cb4e4811070e10a37ff93e6b68e80495267 (
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
|
/*
* Copyright (c) 2026, Apollo Telephone Laboratories.
* Provided under the BSD-3 clause.
*/
#include <stdint.h>
#include <stddef.h>
#include <atas/lexer.h>
/* Symbolic token */
#define SYMTOK(TOK) \
"[" TOK "]"
/* Literal token */
#define LITTOK(TOK) \
"'" TOK "'"
/*
* Safe macro to index toktab
*/
#define TOK_STR(TT) \
((TT) >= TT_MAX) \
? "bad" \
: toktab[(TT)]
/*
* Lookup table to convert token constants into
* strings.
*/
static const char *toktab[] = {
[TT_NONE] = SYMTOK("NONE"),
[TT_COMMA] = LITTOK(",")
};
/*
* Parse a specific token
*
* @state: Assembler state machine
* @tok: Token to parse at
*
* Returns zero on success
*/
static int
parse_token(struct atas_state *state, struct atas_token *tok)
{
if (state == NULL) {
return -1;
}
printf("got token %s\n", TOK_STR(tok->type));
return 0;
}
int
atas_parse(struct atas_state *state)
{
struct atas_token tok;
int error;
if (state == NULL) {
return -1;
}
for (;;) {
error = atas_lexer_scan(state, &tok);
if (error < 0) {
break;
}
error = parse_token(state, &tok);
if (error < 0) {
return -1;
}
}
return 0;
}
|