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

Go to the source code of this file.

Functions

unsigned int parse_statement_include (module_t *module, ast_node_t *parent, token_t *token)
 Parse a include statement adding it as a children on parent AST node.
 

Function Documentation

◆ parse_statement_include()

unsigned int parse_statement_include ( module_t * module,
ast_node_t * parent,
token_t * token )

Parse a include 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 6 of file parse_statement_include.c.

7{
8 ast_node_t* node_statement = ast_add_children(parent, NT_STATEMENT, token);
9 token_t* tkstring = token + 1;
10 token_t* tksemicolon = token + 2;
11
12 if (tkstring->type != TK_STRING) {
14 module,
15 tkstring->line,
16 tkstring->column,
17 tkstring->lexeme.length,
18 "Expected literal string here. Example: include \"module.mya\";"
19 );
20
21 return 2 + (tksemicolon->type == TK_SEMICOLON);
22 }
23
24 if (tksemicolon->type != TK_SEMICOLON) {
26 module,
27 tkstring->line,
28 tkstring->column + tkstring->lexeme.length + 1,
29 1,
30 "Expected semicolon at end of the include command. Example: include \"module.mya\";"
31 );
32 }
33
34 ast_add_children(node_statement, NT_STRING, tkstring);
35
36 return 3;
37}
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:92
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:32
token_type_t type
Token type.
Definition token.h:33
dstring_t lexeme
Lexeme of the token.
Definition token.h:37
unsigned int line
Token line inside the module.
Definition token.h:34
unsigned int column
Column of the token position on the line.
Definition token.h:35
struct ast_node ast_node_t
@ NT_STRING
Definition ast.h:18
@ NT_STATEMENT
Definition ast.h:17
struct token token_t
Struct for a Mya token.
@ TK_STRING
Definition token.h:25
@ TK_SEMICOLON
Definition token.h:24