libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
parse_statement_bitfield.c File Reference
#include "ast.h"
#include "macro_utils.h"
#include "module.h"
#include "parser.h"
Include dependency graph for parse_statement_bitfield.c:

Go to the source code of this file.

Functions

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.
 

Function Documentation

◆ parse_statement_bitfield()

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.

Parameters
moduleThe module where the AST is.
parentThe AST node parent for the statement.
tokenThe token where to start parsing the statement.
Returns
The number of tokens used on the statement.

Definition at line 10 of file parse_statement_bitfield.c.

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}
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
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_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
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_BITFIELD_BODY
Definition ast.h:12
struct token token_t
Struct for a Mya token.
@ TK_IDENTIFIER
Definition token.h:19
@ TK_OPEN_BRACES
Definition token.h:22