diff options
author | Marvin Borner | 2021-04-27 16:24:24 +0200 |
---|---|---|
committer | Marvin Borner | 2021-04-27 16:24:24 +0200 |
commit | 7c047df78502d53411fef09c6e39540d2b7e796a (patch) | |
tree | 00a72bb40f81e7294cb1dde776159ed219ee4967 | |
parent | 60e7b5069d5936546356053f85008a5a02bb473a (diff) |
Fixed some overflows and enabled live parsing
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | src/gui.c | 15 | ||||
-rw-r--r-- | src/inc/lexer.h | 1 | ||||
-rw-r--r-- | src/inc/warnings.h | 1 | ||||
-rw-r--r-- | src/lexer.c | 11 | ||||
-rw-r--r-- | src/parser.c | 14 | ||||
-rw-r--r-- | src/syntax.c | 4 | ||||
-rw-r--r-- | src/warnings.c | 18 |
8 files changed, 49 insertions, 17 deletions
@@ -5,7 +5,7 @@ OBJS = $(patsubst $(SOURCEDIR)/%.c, $(BUILDDIR)/%.o, $(SOURCES)) CC = gcc WARNINGS = -Wall -Wextra -Wshadow -Wpointer-arith -Wwrite-strings -Wredundant-decls -Wnested-externs -Wformat=2 -Wmissing-declarations -Wstrict-prototypes -Wmissing-prototypes -Wcast-qual -Wswitch-default -Wswitch-enum -Wlogical-op -Wunreachable-code -Wundef -Wold-style-definition -Wvla -pedantic -DEBUG = -fsanitize=undefined -fsanitize=address -ggdb3 -g3 -g -s -Og +DEBUG = -fsanitize=undefined -ggdb3 -g3 -g -s -Og CFLAGS = -Ofast $(WARNINGS) -I$(SOURCEDIR)/inc/ $(shell pkg-config --cflags --libs gtk+-3.0 gtksourceview-4) $(DEBUG) all: $(OBJS) @@ -52,8 +52,8 @@ static void gui_call_parser(void) { char *buf = gui_text_buffer(); - if (parse(buf, BUFFER_SIZE)) - gui_show_info("Compiled successfully!"); + if (!parse(buf, BUFFER_SIZE)) { + } // Oh, parsing failed :( (TODO: Do something?) g_free(buf); } @@ -139,8 +139,8 @@ static gchar *gui_show_tt(GtkSourceMarkAttributes *attributes, GtkSourceMark *ma return strdup(message); } -void gui_add_line_marker(u32 line_number, const char *message, const char *name, const char *category, - const char *icon, GdkRGBA rgba) +void gui_add_line_marker(u32 line_number, const char *message, const char *name, + const char *category, const char *icon, GdkRGBA rgba) { GtkSourceMarkAttributes *attributes = gtk_source_mark_attributes_new(); gtk_source_mark_attributes_set_background(attributes, &rgba); @@ -156,7 +156,8 @@ void gui_add_line_marker(u32 line_number, const char *message, const char *name, strdup(message)); } -void gui_remove_line_marker(const char *category) { +void gui_remove_line_marker(const char *category) +{ GtkTextIter start, end; GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(text_view)); @@ -242,7 +243,9 @@ static gboolean gui_key_release_handler(GtkWidget *widget, GdkEventKey *event, g UNUSED(event); UNUSED(data); - gui_call_syntax_highlighter(); + // TODO: Integrate highlighting into parser? + //gui_call_syntax_highlighter(); + gui_call_parser(); return FALSE; } diff --git a/src/inc/lexer.h b/src/inc/lexer.h index f323f58..33b56af 100644 --- a/src/inc/lexer.h +++ b/src/inc/lexer.h @@ -6,6 +6,7 @@ enum token_type { UNKNOWN, NEWLINE, + END, SPACE, INSTR_START, diff --git a/src/inc/warnings.h b/src/inc/warnings.h index f8c06bb..76da8b4 100644 --- a/src/inc/warnings.h +++ b/src/inc/warnings.h @@ -11,6 +11,7 @@ struct context { void warnings_print(void); void warnings_add(struct context *ctx, const char *fmt, ...); void warnings_clear(void); +void warnings_remove_marks(void); u8 warnings_exist(void); #endif diff --git a/src/lexer.c b/src/lexer.c index 5b43cb5..9efe957 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -1,3 +1,4 @@ +#include <assert.h> #include <def.h> #include <lexer.h> #include <stdio.h> @@ -16,8 +17,13 @@ void token_print(struct token *tok) { - if (!tok->length) + // Something terrible needs to happen to make this true + assert(tok->length || tok->type == END); + + if (tok->type == END) { + printf("\\0 (%d)\n", tok->type); return; + } char swp = tok->start[tok->length]; tok->start[tok->length] = 0; @@ -47,6 +53,9 @@ struct token token_resolve(char *str, u32 size) } else if (str[0] == '\n') { type = NEWLINE; length = 1; + } else if (str[0] == '\0') { + type = END; + length = 0; // Well } else if (str[0] == ' ') { type = SPACE; length = 1; diff --git a/src/parser.c b/src/parser.c index 2f16323..e998135 100644 --- a/src/parser.c +++ b/src/parser.c @@ -31,7 +31,7 @@ static void rom_add(u8 byte) static u32 toks_count(struct token *toks) { struct token *p = toks; - while (p && p->type) + while (p && p->type != END && p->type != UNKNOWN && p->type != NEWLINE) p++; return p - toks; } @@ -387,7 +387,11 @@ static u32 parse_line(struct context *ctx, char *str, u32 size) break; } - if (tok.type == UNKNOWN) { + if (tok.type == UNKNOWN) + break; + + if (tok.type == END) { + ctx->line++; break; } @@ -609,6 +613,7 @@ static u32 parse_line(struct context *ctx, char *str, u32 size) case NUM_END: case REGS_START: case REGS_END: + case END: warnings_add(ctx, "Got enum boundary"); break; default: @@ -645,11 +650,16 @@ u8 parse(char *buf, u32 size) u32 len = parse_line(&ctx, buf + i, size - i); i += len; ctx.column += len; + + if (buf[i] == '\0') + break; } if (warnings_exist()) { warnings_print(); return 0; + } else { + warnings_remove_marks(); } return 1; diff --git a/src/syntax.c b/src/syntax.c index 4367394..8dbc16f 100644 --- a/src/syntax.c +++ b/src/syntax.c @@ -38,9 +38,8 @@ static void syntax_highlight_line(struct pos *pos, char *str, u32 size) if (tok.type > INSTR_START && tok.type < INSTR_END) gui_highlight(pos->x, pos->y, tok.length, "instr"); - else if (tok.type > REGS_START && tok.type < REGS_END) { + else if (tok.type > REGS_START && tok.type < REGS_END) gui_highlight(pos->x, pos->y, tok.length, "regs"); - } pos->x += tok.length; } @@ -48,6 +47,7 @@ static void syntax_highlight_line(struct pos *pos, char *str, u32 size) void syntax_highlight(char *buf, u32 size) { + return; struct pos pos = { 0 }; u32 diff = 0; diff --git a/src/warnings.c b/src/warnings.c index da8d11b..47566ba 100644 --- a/src/warnings.c +++ b/src/warnings.c @@ -33,14 +33,20 @@ void warnings_add(struct context *ctx, const char *fmt, ...) warning_index++; } -static void warnings_generate_name(u32 i) { +static void warnings_generate_name(u32 i) +{ static u32 ctr = 0; snprintf(warnings[i].name, 8, "%d", ctr++); } +void warnings_remove_marks(void) +{ + gui_remove_line_marker("warning"); +} + void warnings_print(void) { - gui_remove_line_marker("error"); + warnings_remove_marks(); for (u32 i = 0; i < WARNING_COUNT; i++) { if (!warnings[i].exists) continue; @@ -48,8 +54,8 @@ void warnings_print(void) printf("Line %d:%d: %s\n", warnings[i].ctx.line, warnings[i].ctx.column, warnings[i].text); warnings_generate_name(i); - gui_add_line_marker(warnings[i].ctx.line - 1, warnings[i].name, warnings[i].text, "error", - "dialog-warning", (GdkRGBA){ 1, 0, 0, .3 }); + gui_add_line_marker(warnings[i].ctx.line - 1, warnings[i].text, warnings[i].name, + "warning", "dialog-warning", (GdkRGBA){ 1, 0, 0, .3 }); } } @@ -60,6 +66,8 @@ u8 warnings_exist(void) void warnings_clear(void) { - if (warning_index) + if (warnings_exist()) { memset(warnings, 0, sizeof(warnings)); + warning_index = 0; + } } |