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

Go to the source code of this file.

Functions

error_code_t module_init (module_t *module, const char *filepath)
 Initialize a module struct.
 
token_tmodule_add_token (module_t *module)
 Add a new token to module.
 
error_code_t module_getc (module_t *module, int *chret)
 Get next character on module's file, removing it from the queue.
 
error_code_t module_lookup (module_t *module, int *chret, unsigned int seek)
 Get a character on module's file, without removing it from the queue.
 
void module_close (module_t *module)
 Close the given module.
 
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.
 
void module_print_errors (module_t *module)
 Print all errors on the module.
 

Function Documentation

◆ module_add_error()

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.

Parameters
moduleThe module where to add the error.
lineThe module's line where the error is.
columnThe line's column where the error starts.
lengthThe length of the part where has an error in the line. It's starts on column and ends on column + length.
messageThe error message.

Definition at line 92 of file module.c.

93{
95 module->_errors_length += MODULE_ERRORS_LENGTH_INCREMENT;
96 module->errors = realloc(module->errors, sizeof(module_error_t) * module->_errors_length);
97 }
98
99 module_error_t* error = &module->errors[module->errors_count++];
100
101 error->line = line;
102 error->column = column;
103 error->length = length;
104
105 dstring_init(&error->message, 100);
106 dstring_copy(&error->message, message);
107
108 DPRINTF1("Error at %s:%d:%d -> %s\n", module->filepath, line, column, message);
109}
#define DPRINTF1(fmt,...)
Definition debug.h:16
void dstring_init(dstring_t *string, unsigned int buffer_size)
Initializes a dynamic string (dstring).
Definition dstring.c:10
void dstring_copy(dstring_t *string, const char *source)
Copies the content of source to the dstring.
Definition dstring.c:46
dstring_t message
The error message to print.
Definition module.h:27
unsigned int line
The module's line where the error is.
Definition module.h:23
unsigned int length
The length of the part where has an error in the line.
Definition module.h:25
unsigned int column
The line's column where the error starts.
Definition module.h:24
Struct that represents a Mya module.
Definition module.h:34
char filepath[MODULE_MAX_FILEPATH_SIZE+1]
Module's filepath.
Definition module.h:44
unsigned int _errors_length
Internal field with the size of errors allocated memory.
Definition module.h:42
unsigned int errors_count
Number of errors on errors list.
Definition module.h:41
struct module_error module_error_t
Struct that represents a error inside a Mya module.

◆ module_add_token()

token_t * module_add_token ( module_t * module)

Add a new token to module.

Parameters
moduleThe module where the token will be added.
Returns
A pointer to the added token.

Definition at line 44 of file module.c.

45{
47 module->_tokens_length += MODULE_TOKENS_LENGTH_INCREMENT;
48 module->tokens = realloc(module->tokens, sizeof(token_t) * module->_tokens_length);
49 }
50
51 return &module->tokens[module->tokens_count++];
52}
unsigned int _tokens_length
Internal field with the size of tokens allocated memory.
Definition module.h:40
unsigned int tokens_count
Number of tokens on tokens list.
Definition module.h:39

◆ module_close()

void module_close ( module_t * module)

Close the given module.

Parameters
moduleThe module to be closed.

Definition at line 71 of file module.c.

72{
73 fclose(module->file);
74 module->file = NULL;
75
76 free(module->tokens);
77 module->tokens = NULL;
78
80
81 for (int i = 0; i < module->errors_count; i++) {
83 }
84
85 free(module->errors);
86 module->errors = NULL;
87
89}
void ast_close(ast_node_t *root)
Closes the given AST root node and all children.
Definition ast.c:29
void dstring_close(dstring_t *string)
Closes the dynamic string.
Definition dstring.c:18
void cqueue_close(cqueue_t *queue)
Close the given circular queue.
Definition cqueue.c:80
ast_node_t ast
AST of the module.
Definition module.h:37
module_error_t * errors
List of errors inside the module.
Definition module.h:38
cqueue_t _queue
Internal queue to read file character by character.
Definition module.h:43
token_t * tokens
List of tokens inside the module.
Definition module.h:36
FILE * file
Module's file handler.
Definition module.h:35

◆ module_getc()

error_code_t module_getc ( module_t * module,
int * chret )

Get next character on module's file, removing it from the queue.

Parameters
moduleThe module to get the character.
chretPointer where the character will be saved.
Returns
ERR_EMPTY on file doesn't have more characters.
ERR_OK on character as get successful.

Definition at line 55 of file module.c.

56{
57 _module_fill_queue(module);
58
59 return cqueue_get(&module->_queue, chret);
60}
error_code_t cqueue_get(cqueue_t *queue, int *item)
Get the current value from the circular queue, removing it from the queue.
Definition cqueue.c:35

◆ module_init()

error_code_t module_init ( module_t * module,
const char * filepath )

Initialize a module struct.

Open the filepath and initializes the tokens list.

Parameters
moduleThe module to be initialized.
filepathThe filepath for the module file.
Returns
ERR_FILE_NOT_FOUND on module file is not found.
ERR_OK on success.

Definition at line 17 of file module.c.

18{
19 module->file = fopen(filepath, "r");
20
21 if (! module->file) {
22 return ERR_FILE_NOT_FOUND;
23 }
24
25 strncpy(module->filepath, filepath, MODULE_MAX_FILEPATH_SIZE);
26 module->filepath[MODULE_MAX_FILEPATH_SIZE] = '\0';
27
28 ast_node_init(&module->ast, NULL, NT_ROOT, NULL);
29
30 module->tokens = malloc(sizeof(token_t) * MODULE_INITIAL_TOKENS_LENGTH);
31 module->tokens_count = 0;
32 module->_tokens_length = MODULE_INITIAL_TOKENS_LENGTH;
33
34 module->errors = malloc(sizeof(module_error_t) * MODULE_INITIAL_ERRORS_LENGTH);
35 module->errors_count = 0;
36 module->_errors_length = MODULE_INITIAL_ERRORS_LENGTH;
37
39
40 return ERR_OK;
41}
void ast_node_init(ast_node_t *node, ast_node_t *parent, node_type_t type, token_t *token)
Initializes an AST node.
Definition ast.c:17
@ ERR_FILE_NOT_FOUND
Definition err.h:17
@ ERR_OK
Definition err.h:15
void cqueue_init(cqueue_t *queue, unsigned int max_length)
Initializes a circular queue.
Definition cqueue.c:10
@ NT_ROOT
Definition ast.h:15
#define MODULE_MAX_FILEPATH_SIZE
Definition module.h:10
#define MODULE_MAX_QUEUE_LENGTH
Definition module.h:15

◆ module_lookup()

error_code_t module_lookup ( module_t * module,
int * chret,
unsigned int seek )

Get a character on module's file, without removing it from the queue.

Parameters
moduleThe module to get the character.
chretPointer where the character will be saved.
seekThe index of the character to get (0 means the current, 1 the next and so one). Should not be >= MODULE_MIN_QUEUE_LENGTH to avoid possible errors.
Returns
ERR_EMPTY on file doesn't have more characters.
ERR_INVALID_INDEX on the seek is invalid.
ERR_OK on character as get successful.

Definition at line 63 of file module.c.

64{
65 _module_fill_queue(module);
66
67 return cqueue_lookup(&module->_queue, chret, seek);
68}
error_code_t cqueue_lookup(const cqueue_t *queue, int *item, unsigned int seek)
Get a value from the circular queue, without removing it from the queue.
Definition cqueue.c:50

◆ module_print_errors()

void module_print_errors ( module_t * module)

Print all errors on the module.

Parameters
moduleThe module to print all errors.

Definition at line 112 of file module.c.

113{
114 module_error_t* error;
115
116 for (int i = 0; i < module->errors_count; i++) {
117 error = &module->errors[i];
118
119 error_module_ctx(module, error->line, error->column, error->length, error->message.data);
120 fputc('\n', stderr);
121 }
122}
void error_module_ctx(module_t *module, unsigned int line, unsigned int column, unsigned int length, const char *message)
Print a error message formated to show the code context inside the module.
char * data
Pointer for the raw string content (a normal C string).
Definition dstring.h:12