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
|
#ifndef PARSER_H
#define PARSER_H
#include "util.h"
#include "tree.h"
#include "token.h"
/* Parser states. */
typedef enum PARSER_STATE {
PARSER_STATE_BLOCK,
PARSER_STATE_EXPR,
PARSER_STATE_LINT,
PARSER_STATE_LSTR,
PARSER_STATE_TAG,
PARSER_STATE_DARG,
PARSER_STATE_CARG,
PARSER_STATE_DEF,
PARSER_STATE_CALL
} parser_state_t;
typedef struct PARSER {
/* What the parser's looking at. */
parser_state_t state;
/* The token list being consumed. */
token_t* token;
/* The AST being produced. */
tree_t* tree;
} parser_t;
/* Creates a new parser. */
parser_t* parser_init(token_t* token);
/*
Destroys a parser.
- Does not free the token list.
- Does not free the AST.
*/
void parser_destroy(parser_t* parser);
/* Step the parser forward by 1 token. */
int parser_nxt_token(parser_t* parser);
/* Get tree for first (implied) block. */
tree_t* parser_parse_init(parser_t* parser);
/* Get tree for a block. */
tree_t* parser_parse_block(parser_t* parser);
/* Get tree for a keyword. */
tree_t* parser_parse_kwd(parser_t* parser);
/* Get tree for an expression.*/
tree_t* parser_parse_expr(parser_t* parser);
/* Return the tree for an integer. */
tree_t* parser_parse_lint(parser_t* parser);
/* Return the tree for a string. */
tree_t* parser_parse_lstr(parser_t* parser);
/* Return the tree for a call. */
tree_t* parser_parse_call(parser_t* parser);
/* Return the tree for a call's arguments. */
tree_t* parser_parse_carg(parser_t* parser);
/* Return the tree for a definition. */
tree_t* parser_parse_def(parser_t* parser);
/* Return the tree for a definition tag. */
tree_t* parser_parse_tag(parser_t* parser);
/* Return the tree for a definition's arguments. */
tree_t* parser_parse_darg(parser_t* parser);
/*
Check whether the current token matches the given type.
- If it doesn't, return 0 and throw error.
*/
int parser_match(parser_t* parser, token_type_t type);
/* Steps the parser forward by one token, then check whether the new token matches the given type. */
int parser_nxt_token_match(parser_t* parser, token_type_t type);
/* Parse. */
void parser_parse(parser_t* parser);
/* Parse with the given parser. */
void parser_run(parser_t* parser);
#endif
|