libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
parse_statement_bitfield.c
Go to the documentation of this file.
1#include "ast.h"
2#include "macro_utils.h"
3#include "module.h"
4#include "parser.h"
5
6static unsigned int
7_parse_bitfield_body(module_t* module, ast_node_t* parent, token_t* token);
8
9unsigned int
11{
12 ast_node_t* node_statement = ast_add_children(parent, NT_STATEMENT, token);
13 token_t* tkid = token + 1;
14
15 if (tkid->type != TK_IDENTIFIER) {
17 module,
18 tkid->line,
19 tkid->column,
20 tkid->lexeme.length,
21 "Expected an identifier here. Example: bitfield Name[4]"
22 );
23
24 return 1;
25 }
26
27 ast_add_children(node_statement, NT_IDENTIFIER, tkid);
28 unsigned int ntokens = 2 + parse_size_spec(module, node_statement, token + 2, "bitfield Name[4]");
29
30 token_t* tkopen_braces = token + ntokens;
31
32 if (tkopen_braces->type == TK_OPEN_BRACES) {
33 ast_node_t* body = ast_add_children(node_statement, NT_BITFIELD_BODY, tkopen_braces);
34 ntokens += 1 + _parse_bitfield_body(module, body, tkopen_braces + 1);
35 }
36
37 return ntokens;
38}
39
40static unsigned int
41_parse_bitfield_body(module_t* module, ast_node_t* parent, token_t* token)
42{
43 unsigned int ntokens = 0;
44 token_t* current;
45 ast_node_t* field;
46 unsigned int nerrors;
47
48 for (;;) {
49 nerrors = module->errors_count;
50 current = &token[ntokens];
51
52 switch (current->type) {
53 case TK_IDENTIFIER:
54 field = ast_add_children(parent, NT_FIELD, current);
55
56 ntokens += 1 + parse_size_spec(module, field, current + 1, "opcode[5]");
57 break;
58 case TK_CLOSE_BRACES:
59 return ntokens + 1;
60 default:
62 module,
63 current->line,
64 current->column,
65 current->lexeme.length,
66 "Unexpected token inside a bitfield body. It should only have field lists with size specification. Example:\n"
67 " bitfield Opcode[4] {\n"
68 " opcode[5]\n"
69 " reg[3]\n"
70 " }"
71 );
72
73 ntokens++;
74 break;
75 }
76
77 if (module->errors_count > nerrors) {
78 return ntokens + parse_advance(&token[ntokens], ARR_TT(TK_CLOSE_BRACES));
79 }
80 }
81
82 return ntokens;
83}
ast_node_t * ast_add_children(ast_node_t *parent, node_type_t type, token_t *token)
Add a new children for the given AST node.
Definition ast.c:40
#define ARR_TT(...)
Same as ARR_ARG() macro, but with the specific type token_type_t.
Definition macro_utils.h:17
void module_add_error(module_t *module, unsigned int line, unsigned int column, unsigned int length, const char *message)
Add error for the given module.
Definition module.c:97
unsigned int parse_statement_bitfield(module_t *module, ast_node_t *parent, token_t *token)
Parse a bitfield statement adding it as a children on parent AST node.
unsigned int parse_advance(token_t *token, token_type_t *types, size_t ntypes)
Finds the next token TK_EOF or any of the specified types and then returns the number of tokens trave...
unsigned int parse_size_spec(module_t *module, ast_node_t *parent, token_t *token, const char *example)
Parse a size specification in the format [ EXPRESSION ] adding it as a children on parent AST node.
Definition parse_common.c:7
unsigned int length
The length of the string.
Definition dstring.h:13
Struct that represents a Mya module.
Definition module.h:34
unsigned int errors_count
Number of errors on errors list.
Definition module.h:41
Struct for a Mya token.
Definition token.h:34
token_type_t type
Token type.
Definition token.h:35
dstring_t lexeme
Lexeme of the token.
Definition token.h:41
unsigned int line
Token line inside the module.
Definition token.h:36
unsigned int column
Column of the token position on the line.
Definition token.h:37
struct ast_node ast_node_t
@ NT_IDENTIFIER
Definition ast.h:17
@ NT_STATEMENT
Definition ast.h:21
@ NT_FIELD
Definition ast.h:16
@ NT_BITFIELD_BODY
Definition ast.h:12
struct module module_t
Struct that represents a Mya module.
struct token token_t
Struct for a Mya token.
@ TK_IDENTIFIER
Definition token.h:19
@ TK_OPEN_BRACES
Definition token.h:22
@ TK_CLOSE_BRACES
Definition token.h:12