detect mismatched

This commit is contained in:
Roberto Alsina 2023-07-12 19:40:25 -03:00
parent cead5d075f
commit 1d4e60ec55
3 changed files with 23 additions and 26 deletions

View File

@ -3,8 +3,10 @@
## Things That May Get Done ## Things That May Get Done
* Produce decent error structures * Produce decent error structures
* Detect mismatched start/end like {{% foo >}}
* Inform tag type (% or <) in result * Inform tag type (% or <) in result
* Handle other kinds of quotes * Handle other kinds of quotes
* Handle escaping quote characters * Handle escaping quote characters
* Handle escaping the shortcode itself like \{{% foo %}} (check with hugo docs) * Handle escaping the shortcode itself like \{{% foo %}} (check with hugo docs)
* Report errors on mismatched {{% foo >}}
* ~~Detect mismatched start/end like {{% foo >}}~~

View File

@ -49,10 +49,19 @@
start_b = '{{<'; start_b = '{{<';
end_b = '>}}'; end_b = '>}}';
content = spc name (sep arg)* spc;
start = start_p | start_b ; start = start_p | start_b ;
end = end_p | end_b ; end = end_p | end_b ;
shortcode = (start spc name (sep arg)* spc end) mismatched = ((start_p content end_b) | (start_b content end_p))
@{
// Since it's mismatched, remove the name
sc_list[c_sc].name.start = 0;
sc_list[c_sc].name.len=0;
};
shortcode = ((start_p content end_p) | (start_b content end_b))
> { > {
sc_list[c_sc].whole.start = p-start-1; sc_list[c_sc].whole.start = p-start-1;
} }
@ -83,11 +92,6 @@
start + sc_list[c_sc].name.start, start + sc_list[c_sc].name.start,
sc_list[c_sc-1].name.len) !=0) sc_list[c_sc-1].name.len) !=0)
{ {
// printf("Mismatched tags!\n");
// str_copyb(&sc,start + sc_list[c_sc-1].name.start, sc_list[c_sc-1].name.len);
// printf("opened: %s\n", sc.s);
// str_copyb(&sc,start + sc_list[c_sc].name.start, sc_list[c_sc].name.len);
// printf("closed: %s\n", sc.s);
return NULL; return NULL;
} }
// Reuse this shortcode entry for next one // Reuse this shortcode entry for next one
@ -95,7 +99,7 @@
sc_list[c_sc].name.len=0; sc_list[c_sc].name.len=0;
}; };
main := (any* (shortcode | matched_shortcode))*; main := (any* (shortcode | matched_shortcode | mismatched))*;
}%% }%%
@ -121,24 +125,6 @@ shortcode *parse(char *input) {
%% write init; %% write init;
%% write exec; %% write exec;
// for (int i=0; sc_list[i].name.start!=0; i++) {
// str_copyb(&sc, start + sc_list[i].name.start, sc_list[i].name.len);
// printf("sc_name: %s (%d)\n", sc.s, sc_list[i].matching );
// str_copyb(&sc, start + sc_list[i].start, sc_list[i].len);
// printf("full_sc: %s\n", sc.s);
// if (sc_list[i].matching) {
// str_copyb(&sc, start + sc_list[i].data.start, sc_list[i].data.len);
// printf("sc_data: %s\n", sc.s);
// }
// for (int j=0; j< sc_list[i].argcount; j++) {
// str_copyb(&sc, start + sc_list[i].argnames[j].start, sc_list[i].argnames[j].len);
// printf("argname %d: %s\n", j, sc.s);
// str_copyb(&sc, start + sc_list[i].argvals[j].start, sc_list[i].argvals[j].len);
// printf("argval %d: %s\n", j, sc.s);
// }
// }
// TODO: handle errors
return sc_list; return sc_list;
} }

View File

@ -38,6 +38,14 @@ Ensure(parse, simple_shortcode)
assert_that(s.s, is_equal_to_string("{{% shortcode %}}")); assert_that(s.s, is_equal_to_string("{{% shortcode %}}"));
} }
Ensure(parse, mismatched_brackets)
{
char *input = "foobar {{% shortcode >}}blah";
result = parse(input);
// No shortcodes
assert_that(result[0].name.len, is_equal_to(0));
}
Ensure(parse, inner_spaces_optional) Ensure(parse, inner_spaces_optional)
{ {
char *input = "foobar {{% shortcode%}}blah"; char *input = "foobar {{% shortcode%}}blah";
@ -164,6 +172,7 @@ int main(int argc, char **argv)
str_init(&s); str_init(&s);
TestSuite *suite = create_test_suite(); TestSuite *suite = create_test_suite();
add_test_with_context(suite, parse, empty_string); add_test_with_context(suite, parse, empty_string);
add_test_with_context(suite, parse, mismatched_brackets);
add_test_with_context(suite, parse, simple_shortcode); add_test_with_context(suite, parse, simple_shortcode);
add_test_with_context(suite, parse, name_can_be_path); add_test_with_context(suite, parse, name_can_be_path);
add_test_with_context(suite, parse, inner_spaces_optional); add_test_with_context(suite, parse, inner_spaces_optional);