return errors from parse() #1

Merged
ralsina merged 12 commits from return-errors into main 2023-07-14 01:05:13 +00:00
3 changed files with 24 additions and 19 deletions
Showing only changes of commit 7ead97a5e7 - Show all commits

View File

@ -33,6 +33,7 @@ typedef struct shortcode shortcode;
struct sc_result struct sc_result
{ {
shortcode sc[100]; shortcode sc[100];
unsigned int sccount;
sc_error errors[10]; sc_error errors[10];
unsigned int errcount; unsigned int errcount;
}; };
@ -46,6 +47,12 @@ Example:
{{% foo %}} {{% /bar %}} {{% foo %}} {{% /bar %}}
*/ */
#define ERR_MISMATCHED_CLOSING_TAG 1 #define ERR_MISMATCHED_CLOSING_TAG 1
/* You are using mismatched brackets.
Example:
{{% foo >}}
*/
#define ERR_MISMATCHED_BRACKET 2 #define ERR_MISMATCHED_BRACKET 2

View File

@ -103,9 +103,7 @@
sc_list[c_sc-1].matching = 1; sc_list[c_sc-1].matching = 1;
sc_list[c_sc-1].whole.len = p-start-sc_list[c_sc-1].whole.start + 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 // Do NOT increase c_sc
sc_list[c_sc].name.start = 0;
sc_list[c_sc].name.len=0;
}; };
main := (any* (shortcode | matched_shortcode | mismatched))*; main := (any* (shortcode | matched_shortcode | mismatched))*;
@ -132,5 +130,6 @@ sc_result parse(char *input) {
%% write init; %% write init;
%% write exec; %% write exec;
result.sccount = c_sc;
return result; return result;
} }

31
tests.c
View File

@ -19,7 +19,7 @@ Ensure(parse, empty_string)
char *input = ""; char *input = "";
result = parse(input); result = parse(input);
// This means no shortcodes in it // 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)); assert_that(result.errcount, is_equal_to(0));
} }
@ -28,7 +28,7 @@ Ensure(parse, simple_shortcode)
char *input = "foobar {{% shortcode %}}blah"; char *input = "foobar {{% shortcode %}}blah";
result = parse(input); result = parse(input);
// Only 1 shortcode // 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 // It's a simple one called shortcode, no args
chunk_s(input, result.sc[0].name); chunk_s(input, result.sc[0].name);
@ -45,25 +45,24 @@ Ensure(parse, mismatched_tags)
{ {
char *input = "foobar {{% shortcode %}}blah{{% /foo %}}"; char *input = "foobar {{% shortcode %}}blah{{% /foo %}}";
result = parse(input); result = parse(input);
// No shortcodes // One shortcode, one error
assert_that(result.sc[0].name.len, is_equal_to(0)); assert_that(result.sccount, is_equal_to(1));
assert_that(result.errcount, 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)); assert_that(result.errors[0].code, is_equal_to(ERR_MISMATCHED_CLOSING_TAG));
str_copyb(&s, input + result.errors[0].position, 8); 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) Ensure(parse, mismatched_brackets)
{ {
char *input = "foobar {{% shortcode >}}blah"; char *input = "foobar {{% shortcode >}}blah";
result = parse(input); result = parse(input);
// No shortcodes // No shortcodes, 1 error
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(1)); assert_that(result.errcount, is_equal_to(1));
assert_that(result.errors[0].code, is_equal_to(ERR_MISMATCHED_BRACKET)); assert_that(result.errors[0].code, is_equal_to(ERR_MISMATCHED_BRACKET));
str_copyb(&s, input + result.errors[0].position, 3); 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) 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 %}} "; char *input = "foobar {{% sc %}} >}}blah {{% /sc %}} ";
result = parse(input); result = parse(input);
// 1 shortcode // 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); chunk_s(input, result.sc[0].whole);
assert_that(s.s, is_equal_to_string("{{% sc %}} >}}blah {{% /sc %}}")); assert_that(s.s, is_equal_to_string("{{% sc %}} >}}blah {{% /sc %}}"));
chunk_s(input, result.sc[0].data); 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 %}}"; char *input = "foobar {{% sc \">}}blah\" %}} {{% /sc %}}";
result = parse(input); result = parse(input);
// 1 shortcode // 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); chunk_s(input, result.sc[0].whole);
assert_that(s.s, is_equal_to_string("{{% sc \">}}blah\" %}} {{% /sc %}}")); assert_that(s.s, is_equal_to_string("{{% sc \">}}blah\" %}} {{% /sc %}}"));
chunk_s(input, result.sc[0].argvals[0]); chunk_s(input, result.sc[0].argvals[0]);
@ -97,7 +96,7 @@ Ensure(parse, inner_spaces_optional)
char *input = "foobar {{% shortcode%}}blah"; char *input = "foobar {{% shortcode%}}blah";
result = parse(input); result = parse(input);
// Only 1 shortcode // 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 // It's a simple one called shortcode, no args
chunk_s(input, result.sc[0].name); chunk_s(input, result.sc[0].name);
@ -115,7 +114,7 @@ Ensure(parse, name_can_be_path)
char *input = "foobar {{% shortcode/foo/bar %}}blah"; char *input = "foobar {{% shortcode/foo/bar %}}blah";
result = parse(input); result = parse(input);
// Only 1 shortcode // 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 // It's a simple one called shortcode, no args
chunk_s(input, result.sc[0].name); chunk_s(input, result.sc[0].name);
@ -130,7 +129,7 @@ Ensure(parse, multiple_shortcodes)
char *input = "foobar {{% shortcode %}}blah {{<sc2 >}}blahblah"; char *input = "foobar {{% shortcode %}}blah {{<sc2 >}}blahblah";
result = parse(input); result = parse(input);
// 2 shortcodes // 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 // It's a simple one called shortcode, no args
chunk_s(input, result.sc[0].name); chunk_s(input, result.sc[0].name);
@ -157,7 +156,7 @@ Ensure(parse, matching_shortcode)
result = parse(input); result = parse(input);
// Only 1 shortcode // 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 // It's a matching one called shortcode, no args
chunk_s(input, result.sc[0].name); 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"; char *input = "foobar {{% shortcode foo \"bar\" 42 bat=v1 baz=\"v2\" %}}blah";
result = parse(input); result = parse(input);
// Only 1 shortcode // 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 // The whole shortcode is the whole thing
chunk_s(input, result.sc[0].whole); chunk_s(input, result.sc[0].whole);