shortcode/shortcodes.rl

62 lines
1.1 KiB
Plaintext
Raw Normal View History

2023-07-10 19:39:01 +00:00
#include <stdio.h>
#include "bstrlib/bstrlib.h"
2023-07-10 21:11:54 +00:00
bstring grab_chunk(char *start, char *end) {
blk2bstr(start, end-start);
}
2023-07-10 19:39:01 +00:00
%%{
machine shortcode;
2023-07-10 21:11:54 +00:00
action mark {
mark = p;
}
spc = space*;
sep = space+;
label = alpha+
> mark
% {printf("label: '%s'\n", grab_chunk(mark, p)->data);};
value = (('"' [^"]* '"') | alnum+)
> mark
% {printf("value: '%s'\n", grab_chunk(mark, p)->data);};
arg = (label '=' value)
> mark
% {printf("arg: '%s'\n", grab_chunk(mark, p)->data);};
2023-07-10 19:39:01 +00:00
2023-07-10 21:11:54 +00:00
start = '{{';
end = '}}';
2023-07-10 19:39:01 +00:00
2023-07-10 21:11:54 +00:00
main := (start spc label (sep (value | arg))* spc end);
2023-07-10 19:39:01 +00:00
}%%
bstring parse(char *_input) {
%%write data;
char *eof, *ts, *te = 0;
int cs, act = 0;
bstring output = bfromcstr("out");
bstring input = bfromcstr(_input);
char *p = input->data;
char *pe = p + blength(input);
2023-07-10 21:11:54 +00:00
char *mark = 0;
bstring label;
2023-07-10 19:39:01 +00:00
%% write init;
%% write exec;
2023-07-10 21:11:54 +00:00
printf("\nLabel is: '%s'\n", label->data);
2023-07-10 19:39:01 +00:00
return output;
}
int main(int argc, char **argv) {
2023-07-10 21:11:54 +00:00
bstring output = parse("{{ foo \"sarasa\" foobar foo=\"bar\" }}");
2023-07-10 19:39:01 +00:00
printf("\n%s\n", output->data);
return 0;
}