From 34a16bd6bdf1d5cdf0586fbd43cdcc02b145042c Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Thu, 13 Jul 2023 21:24:37 -0300 Subject: [PATCH] Add error reporting/checking --- Makefile | 6 ++---- shortcodes.h | 8 +++----- shortcodes.rl | 3 +++ tests.c | 4 ++++ 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index fcb96f2..51674c2 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,8 @@ CC=gcc all: test shortcodes.c: shortcodes.rl ragel -G2 -L shortcodes.rl -o shortcodes.c -%.o: %.c - $(CC) -fPIC -c -o $@ $^ -g -tests.so: shortcodes.o tests.o - $(CC) -shared -g -o $@ $^ -lbg -lcgreen +tests.so: shortcodes.c tests.c + $(CC) -fPIC -shared -g -o $@ $^ -lbg -lcgreen clean: rm -f shortcodes.c *.o *.so tests test: tests.so diff --git a/shortcodes.h b/shortcodes.h index 3153a00..01840b9 100644 --- a/shortcodes.h +++ b/shortcodes.h @@ -1,5 +1,4 @@ -#ifndef SHORTCODES_H -#define SHORTCODES_H +#pragma once // A chunk is a reference to a piece of string // from "start" relative to its start @@ -46,9 +45,8 @@ Example: {{% foo %}} {{% /bar %}} */ -#define ERR_MISMATCHED_CLOSING_TAG 1; +#define ERR_MISMATCHED_CLOSING_TAG 1 +#define ERR_MISMATCHED_BRACKET 2 sc_result parse(char *); - -#endif diff --git a/shortcodes.rl b/shortcodes.rl index 075c395..cd76fb4 100644 --- a/shortcodes.rl +++ b/shortcodes.rl @@ -58,6 +58,9 @@ // Since it's mismatched, remove the name sc_list[c_sc].name.start = 0; sc_list[c_sc].name.len=0; + result.errors[result.errcount].position = p-start-2; + result.errors[result.errcount].code = ERR_MISMATCHED_BRACKET; + result.errcount++; }; shortcode = ((start_p content end_p) | (start_b content end_b)) diff --git a/tests.c b/tests.c index 1dfca35..8526604 100644 --- a/tests.c +++ b/tests.c @@ -45,6 +45,10 @@ Ensure(parse, mismatched_brackets) result = parse(input); // No shortcodes assert_that(result.sc[0].name.len, is_equal_to(0)); + assert_that(result.errcount, is_equal_to(1)); + assert_that(result.errors[0].code, is_equal_to(ERR_MISMATCHED_BRACKET)); + str_copyb(&s, input + result.errors[0].position, 3); + assert_that(s.s, is_equal_to_string(">}}")); } Ensure(parse, mismatched_brackets_inside_data_are_ok)