libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
parser.h File Reference
#include "types/err.h"
#include "types/module.h"
#include "types/token.h"
Include dependency graph for parser.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

unsigned int parse_statement (module_t *module, ast_node_t *parent, token_t *token)
 Parse a statement adding it as a children on parent AST node.
 
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.
 
unsigned int parse_statement_set (module_t *module, ast_node_t *parent, token_t *token)
 Parse a set statement adding it as a children on parent AST node.
 
unsigned int parse_expression (module_t *module, ast_node_t *parent, token_t *token)
 Parse a mathematical expression adding it as a children on parent AST node.
 

Function Documentation

◆ parse_expression()

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

Parse a mathematical expression adding it as a children on parent AST node.

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

Definition at line 17 of file parse_expression.c.

18{
20 ast_node_t node;
21 token_t* current_token;
22 ast_node_t* child;
23 unsigned int ntokens = 0;
24
26
27 for (;;) {
28 current_token = &token[ntokens];
29
30 switch (current_token->type) {
31 case TK_OPEN_PARENS:
32 child = stack_push(&stack, NT_EXPRESSION, current_token);
33 ntokens += parse_expression(module, child, token + 1);
34
35 if (token[ntokens].type != TK_CLOSE_PARENS) {
37 module,
38 child->token->line,
39 child->token->column,
40 child->token->lexeme.length,
41 "Open parentheses starting here has not a expected close parentheses matching it."
42 );
43
44 return ntokens;
45 }
46
47 ntokens++;
48 break;
49 case TK_IDENTIFIER:
50 case TK_NUMBER:
51 stack_push(&stack, NT_EXPRESSION, current_token);
52 ntokens++;
53 break;
54 case TK_OPERATOR:
55 ntokens += _parse_operator(module, parent, &stack, current_token);
56 break;
57 case TK_SEMICOLON:
58 ntokens++;
59 case TK_CLOSE_PARENS:
60 case TK_COMMA:
61 case TK_EOF:
62 goto finish_expression;
63 default:
65 module,
66 current_token->line,
67 current_token->column,
68 current_token->lexeme.length,
69 "Unexpected token inside an expression."
70 );
71
72 ntokens++;
73 goto finish_expression;
74 }
75 }
76
77
78finish_expression:
79 // We expect to only have one expression on stack at end. It's the root node of the expression.
80
81 if (stack_isempty(&stack)) {
82 module_add_error(module, token->line, token->column, token->lexeme.length, "Malformed expression starting here.");
83 return ntokens;
84 }
85
86 stack_pop(&stack, &node);
87
88 if (! stack_isempty(&stack)) {
89 stack_pop(&stack, &node);
90
92 module,
93 node.token->line,
94 node.token->column,
95 node.token->lexeme.length,
96 "Unexpected expression starting here."
97 );
98
99 return ntokens + 1;
100 }
101
102 ast_insert_children(parent, &node);
103
104 return ntokens + 1;
105}
ast_node_t * ast_insert_children(ast_node_t *parent, ast_node_t *child)
Insert a exitent AST node as children of the given parent node.
Definition ast.c:51
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 parse_expression(module_t *module, ast_node_t *parent, token_t *token)
Parse a mathematical expression adding it as a children on parent AST node.
void stack_init(stack_t *stack)
Initializes the given stack.
Definition stack.c:11
ast_node_t * stack_push(stack_t *stack, node_type_t type, token_t *token)
Push a new value on the stack.
Definition stack.c:27
error_code_t stack_pop(stack_t *stack, ast_node_t *value)
Pop a value from the stack.
Definition stack.c:47
bool stack_isempty(stack_t *stack)
Check if the given stack is empty.
Definition stack.c:59
token_t * token
Definition ast.h:26
unsigned int length
The length of the string.
Definition dstring.h:13
Struct that represents a Mya module.
Definition module.h:34
A struct representing a dynamic stack.
Definition stack.h:12
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_EXPRESSION
Definition ast.h:12
struct stack stack_t
A struct representing a dynamic stack.
struct token token_t
Struct for a Mya token.
@ TK_NUMBER
Definition token.h:19
@ TK_IDENTIFIER
Definition token.h:17
@ TK_OPEN_PARENS
Definition token.h:22
@ TK_COMMA
Definition token.h:14
@ TK_CLOSE_PARENS
Definition token.h:12
@ TK_OPERATOR
Definition token.h:23
@ TK_EOF
Definition token.h:15
@ TK_SEMICOLON
Definition token.h:24

◆ parse_statement()

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

Parse a 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 8 of file parse_statement.c.

9{
10 if (token->type != TK_KEYWORD) {
12 module,
13 token->line,
16 "This token is unexpected here. It's expected to be a valid statement keyword.\n"
17 );
18
19 return 1;
20 }
21
22 switch (token->value) {
23 case KEY_INCLUDE:
24 return parse_statement_include(module, parent, token);
25 case KEY_SET:
26 return parse_statement_set(module, parent, token);
27 default:
28 return 1;
29 }
30}
@ KEY_SET
Definition keywords.h:10
@ KEY_INCLUDE
Definition keywords.h:7
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.
unsigned int parse_statement_set(module_t *module, ast_node_t *parent, token_t *token)
Parse a set statement adding it as a children on parent AST node.
long long int value
Integer value of the token on TK_NUMBER tokens.
Definition token.h:36
@ TK_KEYWORD
Definition token.h:18

◆ 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
@ NT_STRING
Definition ast.h:18
@ NT_STATEMENT
Definition ast.h:17
@ TK_STRING
Definition token.h:25

◆ parse_statement_set()

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

Parse a set 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_set.c.

7{
8 ast_node_t* node_statement = ast_add_children(parent, NT_STATEMENT, token);
9 token_t* tkid = token + 1;
10 token_t* tkequal = token + 2;
11 token_t* tkexpr = token + 3;
12
13 if (tkid->type != TK_IDENTIFIER) {
15 module,
16 tkid->line,
17 tkid->column,
18 tkid->lexeme.length,
19 "Expected an identifier here. Example: set NAME = value;"
20 );
21
22 return 1;
23 }
24
25 if (tkequal->type != TK_EQUAL) {
27 module,
28 tkequal->line,
29 tkequal->column,
30 tkequal->lexeme.length,
31 "Expected an equal operator here. Example: set NAME = value;"
32 );
33 }
34
35 ast_add_children(node_statement, NT_IDENTIFIER, tkid);
36 parse_expression(module, node_statement, tkexpr);
37
38 return 3;
39}
unsigned int parse_expression(module_t *module, ast_node_t *parent, token_t *token)
Parse a mathematical expression adding it as a children on parent AST node.
@ NT_IDENTIFIER
Definition ast.h:13
@ TK_EQUAL
Definition token.h:16