libmya 0.1.0
Library to parse Mya language.
Loading...
Searching...
No Matches
lexer.c File Reference
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "debug.h"
#include "dstring.h"
#include "module.h"
#include "queue.h"
#include "token.h"
#include "types/err.h"
#include "types/keywords.h"
#include "types/operators.h"
Include dependency graph for lexer.c:

Go to the source code of this file.

Macros

#define MOD_ADD(lexeme, type)
 

Functions

error_code_t mya_lexer (module_t *module)
 Make the lexical analysis on the given module.
 

Macro Definition Documentation

◆ MOD_ADD

#define MOD_ADD ( lexeme,
type )
Value:
token_init(token, lexeme, type, line, column); \
DPRINTF3("Added token `%s` at %s:%d:%d.\n", lexeme, module->filepath, line, column)
token_t * module_add_token(module_t *module)
Add a new token to module.
Definition module.c:44
Struct that represents a Mya module.
Definition module.h:34
char filepath[MODULE_MAX_FILEPATH_SIZE+1]
Module's filepath.
Definition module.h:44
Struct for a Mya token.
Definition token.h:32

Definition at line 47 of file lexer.c.

47#define MOD_ADD(lexeme, type) \
48 token = module_add_token(module); \
49 token_init(token, lexeme, type, line, column); \
50 DPRINTF3("Added token `%s` at %s:%d:%d.\n", lexeme, module->filepath, line, column)

Function Documentation

◆ mya_lexer()

error_code_t mya_lexer ( module_t * module)

Make the lexical analysis on the given module.

It will construct the module->tokens list and maybe registry some errors on module->errors list.

Parameters
moduleThe module for make the lexical analysis.

Definition at line 53 of file lexer.c.

54{
55 char message[128];
56 int ch;
57 unsigned int line = 1;
58 unsigned int column = 1;
60
61 for (; module_lookup(module, &ch, 0) == ERR_OK; column++) {
62 if (isblank(ch)) {
63 module_getc(module, &ch);
64 continue;
65 }
66
67 switch (ch) {
68 case '#':
69 _mod_rm_line(module);
70 line++;
71 column = 0;
72 continue;
73 case '\n':
74 line++;
75 column = 0;
76 break;
77 case '{':
79 break;
80 case '}':
82 break;
83 case '(':
85 break;
86 case ')':
88 break;
89 case '[':
91 break;
92 case ']':
94 break;
95 case ';':
97 break;
98 case ':':
99 MOD_ADD(":", TK_COLON);
100 break;
101 case ',':
102 MOD_ADD(",", TK_COMMA);
103 break;
104 case '=':
105 MOD_ADD("=", TK_EQUAL);
106 break;
107 case '"':
108 column += _mod_read_string(module, line, column) - 1;
109 continue;
110 default:
111 if (isdigit(ch)) {
112 column += _mod_read_number(module, line, column) - 1;
113 continue;
114 }
115
116 if (ispunct(ch)) {
117 column += _mod_read_operator(module, line, column) - 1;
118 continue;
119 }
120
121 if (isalnum(ch)) {
122 column += _mod_read_identifier(module, line, column) - 1;
123 continue;
124 }
125
126 sprintf(message, "Character '%c' is unexpected here!\n", ch);
127
128 module_add_error(module, line, column, 1, message);
129 break;
130 }
131
132 module_getc(module, &ch);
133 }
134
135 MOD_ADD(":EOF:", TK_EOF);
136
137 return ERR_OK;
138}
@ ERR_OK
Definition err.h:15
#define MOD_ADD(lexeme, type)
Definition lexer.c:47
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
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.
Definition module.c:63
error_code_t module_getc(module_t *module, int *chret)
Get next character on module's file, removing it from the queue.
Definition module.c:55
struct token token_t
Struct for a Mya token.
@ TK_OPEN_BRACKET
Definition token.h:21
@ TK_COLON
Definition token.h:13
@ TK_OPEN_BRACES
Definition token.h:20
@ TK_OPEN_PARENS
Definition token.h:22
@ TK_COMMA
Definition token.h:14
@ TK_CLOSE_PARENS
Definition token.h:12
@ TK_CLOSE_BRACKET
Definition token.h:11
@ TK_EOF
Definition token.h:15
@ TK_CLOSE_BRACES
Definition token.h:10
@ TK_EQUAL
Definition token.h:16
@ TK_SEMICOLON
Definition token.h:24