From 7ead97a5e7b2dca1fb5c558fa0d6cb4f5478d05b Mon Sep 17 00:00:00 2001 From: Roberto Alsina Date: Thu, 13 Jul 2023 22:02:45 -0300 Subject: [PATCH] Finished with error reporting for now --- shortcodes.h | 7 +++++++ shortcodes.rl | 5 ++--- tests.c | 31 +++++++++++++++---------------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/shortcodes.h b/shortcodes.h index 254ae73..4b7336a 100644 --- a/shortcodes.h +++ b/shortcodes.h @@ -33,6 +33,7 @@ typedef struct shortcode shortcode; struct sc_result { shortcode sc[100]; + unsigned int sccount; sc_error errors[10]; unsigned int errcount; }; @@ -46,6 +47,12 @@ Example: {{% foo %}} {{% /bar %}} */ #define ERR_MISMATCHED_CLOSING_TAG 1 + +/* You are using mismatched brackets. +Example: + +{{% foo >}} +*/ #define ERR_MISMATCHED_BRACKET 2 diff --git a/shortcodes.rl b/shortcodes.rl index 15a7ad0..1588bce 100644 --- a/shortcodes.rl +++ b/shortcodes.rl @@ -103,9 +103,7 @@ sc_list[c_sc-1].matching = 1; sc_list[c_sc-1].whole.len = p-start-sc_list[c_sc-1].whole.start + 1; } - // Reuse the closing shortcode tag entry for next one - sc_list[c_sc].name.start = 0; - sc_list[c_sc].name.len=0; + // Do NOT increase c_sc }; main := (any* (shortcode | matched_shortcode | mismatched))*; @@ -132,5 +130,6 @@ sc_result parse(char *input) { %% write init; %% write exec; + result.sccount = c_sc; return result; } diff --git a/tests.c b/tests.c index f4753b0..43e5184 100644 --- a/tests.c +++ b/tests.c @@ -19,7 +19,7 @@ Ensure(parse, empty_string) char *input = ""; result = parse(input); // This means no shortcodes in it - assert_that(result.sc[0].name.len, is_equal_to(0)); + assert_that(result.sccount, is_equal_to(0)); assert_that(result.errcount, is_equal_to(0)); } @@ -28,7 +28,7 @@ Ensure(parse, simple_shortcode) char *input = "foobar {{% shortcode %}}blah"; result = parse(input); // Only 1 shortcode - assert_that(result.sc[1].name.len, is_equal_to(0)); + assert_that(result.sccount, is_equal_to(1)); // It's a simple one called shortcode, no args chunk_s(input, result.sc[0].name); @@ -45,25 +45,24 @@ Ensure(parse, mismatched_tags) { char *input = "foobar {{% shortcode %}}blah{{% /foo %}}"; result = parse(input); - // No shortcodes - assert_that(result.sc[0].name.len, is_equal_to(0)); + // One shortcode, one error + assert_that(result.sccount, is_equal_to(1)); assert_that(result.errcount, is_equal_to(1)); assert_that(result.errors[0].code, is_equal_to(ERR_MISMATCHED_CLOSING_TAG)); str_copyb(&s, input + result.errors[0].position, 8); - assert_that(s.s, is_equal_to_string("{{% /foo")); + assert_that(s.s, is_equal_to_string("{{% /foo")); } - Ensure(parse, mismatched_brackets) { char *input = "foobar {{% shortcode >}}blah"; result = parse(input); - // No shortcodes - assert_that(result.sc[0].name.len, is_equal_to(0)); + // No shortcodes, 1 error + assert_that(result.sccount, 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(">}}")); + assert_that(s.s, is_equal_to_string(">}}")); } Ensure(parse, mismatched_brackets_inside_data_are_ok) @@ -71,7 +70,7 @@ Ensure(parse, mismatched_brackets_inside_data_are_ok) char *input = "foobar {{% sc %}} >}}blah {{% /sc %}} "; result = parse(input); // 1 shortcode - assert_that(result.sc[1].name.len, is_equal_to(0)); + assert_that(result.sccount, is_equal_to(1)); chunk_s(input, result.sc[0].whole); assert_that(s.s, is_equal_to_string("{{% sc %}} >}}blah {{% /sc %}}")); chunk_s(input, result.sc[0].data); @@ -84,7 +83,7 @@ Ensure(parse, mismatched_brackets_in_qval_are_ok) char *input = "foobar {{% sc \">}}blah\" %}} {{% /sc %}}"; result = parse(input); // 1 shortcode - assert_that(result.sc[1].name.len, is_equal_to(0)); + assert_that(result.sccount, is_equal_to(1)); chunk_s(input, result.sc[0].whole); assert_that(s.s, is_equal_to_string("{{% sc \">}}blah\" %}} {{% /sc %}}")); chunk_s(input, result.sc[0].argvals[0]); @@ -97,7 +96,7 @@ Ensure(parse, inner_spaces_optional) char *input = "foobar {{% shortcode%}}blah"; result = parse(input); // Only 1 shortcode - assert_that(result.sc[1].name.len, is_equal_to(0)); + assert_that(result.sccount, is_equal_to(1)); // It's a simple one called shortcode, no args chunk_s(input, result.sc[0].name); @@ -115,7 +114,7 @@ Ensure(parse, name_can_be_path) char *input = "foobar {{% shortcode/foo/bar %}}blah"; result = parse(input); // Only 1 shortcode - assert_that(result.sc[1].name.len, is_equal_to(0)); + assert_that(result.sccount, is_equal_to(1)); // It's a simple one called shortcode, no args chunk_s(input, result.sc[0].name); @@ -130,7 +129,7 @@ Ensure(parse, multiple_shortcodes) char *input = "foobar {{% shortcode %}}blah {{}}blahblah"; result = parse(input); // 2 shortcodes - assert_that(result.sc[2].name.len, is_equal_to(0)); + assert_that(result.sccount, is_equal_to(2)); // It's a simple one called shortcode, no args chunk_s(input, result.sc[0].name); @@ -157,7 +156,7 @@ Ensure(parse, matching_shortcode) result = parse(input); // Only 1 shortcode - assert_that(result.sc[1].name.len, is_equal_to(0)); + assert_that(result.sccount, is_equal_to(1)); // It's a matching one called shortcode, no args chunk_s(input, result.sc[0].name); @@ -178,7 +177,7 @@ Ensure(parse, shortcode_args) char *input = "foobar {{% shortcode foo \"bar\" 42 bat=v1 baz=\"v2\" %}}blah"; result = parse(input); // Only 1 shortcode - assert_that(result.sc[1].name.len, is_equal_to(0)); + assert_that(result.sccount, is_equal_to(1)); // The whole shortcode is the whole thing chunk_s(input, result.sc[0].whole);