Finished with error reporting for now

This commit is contained in:
Roberto Alsina 2023-07-13 22:02:45 -03:00
parent 62fd9b5029
commit 7ead97a5e7
3 changed files with 24 additions and 19 deletions

View File

@ -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

View File

@ -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;
}

31
tests.c
View File

@ -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 {{<sc2 >}}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);