libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
parser.c
Go to the documentation of this file.
1#include <stdlib.h>
2
3#include "ast.h"
4#include "debug.h"
5#include "macro_utils.h"
6#include "mya.h"
7#include "parser.h"
8#include "types/keywords.h"
9#include "types/token.h"
10
13{
15
16 for (unsigned int tk_index = 0; tk_index < module->tokens_count;) {
17 token = &module->tokens[tk_index];
18
19 switch (token->type) {
20 case TK_KEYWORD:
22 "Parsing statement: %s at %s:%d:%d.\n",
25 token->line,
27 );
28
29 tk_index += parse_statement(module, &module->ast, token);
30 tk_index += parse_advance(&module->tokens[tk_index], ARR_TT(TK_KEYWORD));
31 break;
32 case TK_EOF:
33 goto finish;
34 default:
36 module,
37 token->line,
40 "Unexpected token here. It's expected to be a valid statement keyword."
41 );
42
43 tk_index++;
44 break;
45 }
46 }
47
48finish:
49 return (module->errors_count == 0) ? ERR_OK : ERR_INVALID_CODE;
50}
#define DPRINTF2(fmt,...)
Definition debug.h:23
enum error_code error_code_t
Enumeration of error codes.
@ ERR_INVALID_CODE
Definition err.h:18
@ ERR_OK
Definition err.h:15
#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
error_code_t mya_parser(module_t *module)
Make the syntactical analysis on the given module and construct the AST.
Definition parser.c:12
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_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 length
The length of the string.
Definition dstring.h:13
char * data
Pointer for the raw string content (a normal C string).
Definition dstring.h:12
Struct that represents a Mya module.
Definition module.h:34
ast_node_t ast
AST of the module.
Definition module.h:37
char filepath[MODULE_MAX_FILEPATH_SIZE+1]
Module's filepath.
Definition module.h:44
unsigned int errors_count
Number of errors on errors list.
Definition module.h:41
token_t * tokens
List of tokens inside the module.
Definition module.h:36
unsigned int tokens_count
Number of tokens on tokens list.
Definition module.h:39
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 module module_t
Struct that represents a Mya module.
struct token token_t
Struct for a Mya token.
@ TK_KEYWORD
Definition token.h:20
@ TK_EOF
Definition token.h:17