start adding error message support

This commit is contained in:
2025-07-09 01:55:40 +01:00
parent 9e5e932d39
commit e234ea074b
19 changed files with 293 additions and 115 deletions

View File

@@ -108,8 +108,6 @@ int yywrap(void * unused_param) {
#[^\n]* { /* skip comment */ }
. {
GET_STATE
fprintf(stderr, "%s:%zu:%zu error: unexpected character '%s'\n", state->path, state->current_line+1, COLUMN_NO+1, yytext);
exit(1);
return TOKEN_INVALID;
}
%%

View File

@@ -1,8 +1,8 @@
#include "lexer.h"
#include "lex.yy.h"
#include "../string/string.h"
#include "lex.yy.h"
void lexer(LexerState state) {
ArErr lexer(LexerState state) {
size_t line = 1;
size_t column = 1;
int ch;
@@ -32,13 +32,16 @@ void lexer(LexerState state) {
int token;
while ((token = yylex(scanner)) != 0) {
Token token_struct = (Token){
token,
state.current_line+1,
state.current_column+1,
yyget_leng(scanner),
cloneString(yyget_text(scanner))
};
if (token == TOKEN_INVALID) {
ArErr err = create_err(state.current_line + 1, state.current_column + 1,
yyget_leng(scanner), state.path, "Syntax Error",
"Invalid Token '%s'", yyget_text(scanner));
yylex_destroy(scanner);
return err;
}
Token token_struct =
(Token){token, state.current_line + 1, state.current_column + 1,
yyget_leng(scanner), cloneString(yyget_text(scanner))};
darray_push(state.tokens, &token_struct);
if (token == TOKEN_NEW_LINE) {
state.current_line += 1;
@@ -48,4 +51,5 @@ void lexer(LexerState state) {
}
}
yylex_destroy(scanner);
return no_err;
}

View File

@@ -4,9 +4,10 @@
#include "../dynamic_array/darray.h"
#include "token.h"
#include <stdio.h>
#include "../err.h"
typedef struct {
const char *path;
char *path;
FILE *file;
size_t current_line;
size_t current_column;
@@ -14,6 +15,6 @@ typedef struct {
// add more fields as needed
} LexerState;
void lexer(LexerState state);
ArErr lexer(LexerState state);
#endif // LEXER_H

View File

@@ -72,6 +72,7 @@ typedef enum {
TOKEN_COMMA,
TOKEN_COLON,
TOKEN_EXCLAMATION,
TOKEN_INVALID,
} TokenType;
typedef struct {