20static inline unsigned int
21_mod_read_operator(
module_t*
module,
unsigned int line,
unsigned int column);
23static inline unsigned int
24_mod_read_number(
module_t*
module,
unsigned int line,
unsigned int column);
26static inline unsigned int
27_mod_read_string(
module_t*
module,
unsigned int line,
unsigned int column);
29static inline unsigned int
30_mod_read_identifier(
module_t*
module,
unsigned int line,
unsigned int column);
33_get_keyword(
const char* lexeme);
36_get_operator(
const char* lexeme);
41static inline unsigned int
45_isbasedigit(
unsigned int base,
int ch);
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)
57 unsigned int line = 1;
58 unsigned int column = 1;
108 column += _mod_read_string(
module, line, column) - 1;
112 column += _mod_read_number(
module, line, column) - 1;
117 column += _mod_read_operator(
module, line, column) - 1;
122 column += _mod_read_identifier(
module, line, column) - 1;
126 sprintf(message,
"Character '%c' is unexpected here!\n", ch);
148 }
while (err ==
ERR_OK && ch !=
'\n' && ch != EOF);
151static inline unsigned int
152_mod_read_operator(
module_t*
module,
unsigned int line,
unsigned int column)
163 while (ispunct(ch)) {
170 int op = _get_operator(lexeme->
data);
172 sprintf(message,
"Operator `%s` is invalid!", lexeme->
data);
182static inline unsigned int
183_mod_read_number(
module_t*
module,
unsigned int line,
unsigned int column)
189 unsigned int base = 10;
198 if (! isalnum(secondchar)) {
205 if (firstchar ==
'0') {
206 base = _char2base(secondchar);
215 "Character '%c' is an invalid base indicator. It should be 'x' (hexadecimal), 'b' (binary) or 'o' (octal).",
233 while (_isbasedigit(base, firstchar)) {
240 if (isalnum(firstchar)) {
241 sprintf(message,
"Character '%c' is invalid for base %d literal number.", firstchar, base);
246 char* number_start = lexeme->
data + 2 * (base != 10);
247 token->
value = strtoll(number_start, NULL, base);
253static inline unsigned int
254_mod_read_string(
module_t*
module,
unsigned int line,
unsigned int column)
270 ch = _char_escape(ch);
276 sprintf(message,
"End of file reached when trying to find the end of the string starting at line %d.\n", line);
281 return lexeme->
length + 1;
286 return lexeme->
length + 2;
289static inline unsigned int
290_mod_read_identifier(
module_t*
module,
unsigned int line,
unsigned int column)
304 int key = _get_keyword(lexeme->
data);
316_get_keyword(
const char* lexeme)
328_get_operator(
const char* lexeme)
343static inline unsigned int
354 return isdigit(ch) ? 10 : 0;
359_isbasedigit(
unsigned int base,
int ch)
367 return isdigit(ch) && (ch -
'0') <= 7;
369 return ch ==
'1' || ch ==
'0';
#define DPRINTF3(fmt,...)
void dstring_putchar(dstring_t *string, int character)
Concatenates a character on the end of the dstring.
enum error_code error_code_t
Enumeration of error codes.
const char * mya_operators[]
const char * mya_keywords[]
error_code_t mya_lexer(module_t *module)
Make the lexical analysis on the given module.
#define MOD_ADD(lexeme, type)
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.
token_t * module_add_token(module_t *module)
Add a new token to module.
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.
error_code_t module_getc(module_t *module, int *chret)
Get next character on module's file, removing it from the queue.
unsigned int length
The length of the string.
char * data
Pointer for the raw string content (a normal C string).
Struct that represents a Mya module.
char filepath[MODULE_MAX_FILEPATH_SIZE+1]
Module's filepath.
long long int value
Integer value of the token on TK_NUMBER tokens.
token_type_t type
Token type.
dstring_t lexeme
Lexeme of the token.
void token_init(token_t *token, const char *lexeme, token_type_t type, unsigned int line, unsigned int column)
Initializes a token struct.
struct dstring dstring_t
A dynamic string (dstring) that automatically reallocate her buffer when needed.
struct module module_t
Struct that represents a Mya module.
struct token token_t
Struct for a Mya token.