pass file into flex instead of content buffer
This commit is contained in:
@@ -8,8 +8,7 @@ void lexer(LexerState state) {
|
||||
|
||||
yyset_extra(&state, scanner);
|
||||
|
||||
void* buffer = yy_scan_string(state.content, scanner);
|
||||
yy_switch_to_buffer(buffer, scanner);
|
||||
yyset_in(state.file, scanner);
|
||||
|
||||
int token;
|
||||
while ((token = yylex(scanner)) != 0) {
|
||||
@@ -26,7 +25,5 @@ void lexer(LexerState state) {
|
||||
state.current_column += yyget_leng(scanner);
|
||||
}
|
||||
}
|
||||
|
||||
yy_delete_buffer(buffer, scanner);
|
||||
yylex_destroy(scanner);
|
||||
}
|
||||
@@ -1,9 +1,10 @@
|
||||
#include "token.h"
|
||||
#include "../list/list.h"
|
||||
#include <stdio.h>
|
||||
|
||||
typedef struct {
|
||||
const char *path;
|
||||
const char *content;
|
||||
FILE *file;
|
||||
int current_column;
|
||||
LinkedList* tokens;
|
||||
// add more fields as needed
|
||||
|
||||
37
src/main.c
37
src/main.c
@@ -4,49 +4,18 @@
|
||||
#include "memory.h"
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char* read_file_as_text(const char* filename) {
|
||||
FILE *file = fopen(filename, "r");
|
||||
if (!file) {
|
||||
perror("Failed to open file");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Seek to the end to find the file size
|
||||
fseek(file, 0, SEEK_END);
|
||||
long length = ftell(file);
|
||||
rewind(file); // Go back to the beginning
|
||||
|
||||
// Allocate buffer (+1 for null terminator)
|
||||
char *buffer = malloc(length + 1);
|
||||
if (!buffer) {
|
||||
perror("Failed to allocate memory");
|
||||
fclose(file);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Read the whole file into the buffer
|
||||
size_t read_size = fread(buffer, 1, length, file);
|
||||
buffer[read_size] = '\0'; // Null-terminate
|
||||
|
||||
fclose(file);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
int main() {
|
||||
ar_memory_init();
|
||||
const char * path = "test.ar";
|
||||
|
||||
char *content = read_file_as_text(path);
|
||||
LinkedList* tokens = create_list(sizeof(Token));
|
||||
if (!content) return 1;
|
||||
|
||||
LexerState state = {
|
||||
path,
|
||||
content,
|
||||
fopen(path, "r"),
|
||||
0,
|
||||
tokens
|
||||
};
|
||||
@@ -60,5 +29,7 @@ int main() {
|
||||
|
||||
free_list(parsed,free_tagged_value);
|
||||
|
||||
ar_memory_init();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user