shortcode/shortcodes.rl

108 lines
2.2 KiB
Plaintext
Raw Normal View History

2023-07-10 16:39:01 -03:00
#include <stdio.h>
#include "bstrlib/bstrlib.h"
2023-07-10 18:11:54 -03:00
bstring grab_chunk(char *start, char *end) {
blk2bstr(start, end-start);
}
2023-07-10 16:39:01 -03:00
%%{
machine shortcode;
2023-07-10 18:11:54 -03:00
action mark {
mark = p;
}
spc = space*;
sep = space+;
2023-07-10 20:36:50 -03:00
path = (alnum | '/' )+;
2023-07-10 18:11:54 -03:00
2023-07-10 21:55:45 -03:00
name = (alpha+ path?)
2023-07-10 18:11:54 -03:00
> mark
2023-07-10 21:55:45 -03:00
% {bcatStatic(output, "N ");
bcatblk(output, mark, p-mark);
2023-07-10 21:55:45 -03:00
bcatStatic(output, "\n");
2023-07-10 22:11:43 -03:00
bassignblk (new_name, mark, p-mark);
};
2023-07-10 21:55:45 -03:00
argname = alpha+
2023-07-10 18:17:17 -03:00
> mark
2023-07-10 21:55:45 -03:00
% {bcatStatic(output, "A ");
bcatblk(output, mark, p-mark);
2023-07-10 21:55:45 -03:00
bcatStatic(output, "\n");
};
qvalue = ('"' [^"]* '"')
2023-07-10 18:11:54 -03:00
> mark
2023-07-10 21:55:45 -03:00
% {bcatStatic(output, "V ");
bcatblk(output, mark+1, p-mark-2);
2023-07-10 21:55:45 -03:00
bcatStatic(output, "\n");
};
value = alnum+
> mark
2023-07-10 21:55:45 -03:00
% {bcatStatic(output, "V ");
bcatblk(output, mark, p-mark);
2023-07-10 21:55:45 -03:00
bcatStatic(output, "\n");
};
arg = ((argname '=')? (value|qvalue));
2023-07-10 16:39:01 -03:00
start_p = '{{%';
end_p = '%}}';
2023-07-10 16:39:01 -03:00
start_b = '{{<';
end_b = '>}}';
start = start_p | start_b ;
end = end_p | end_b ;
shortcode = (start spc name (sep arg)* spc end)
2023-07-10 22:11:43 -03:00
@ {
bcatStatic(output, "+++ opening\n");
bassign(open_name, new_name);
};
2023-07-10 21:55:45 -03:00
closing_shortcode = (start spc '/' name spc end)
2023-07-10 22:11:43 -03:00
@ {
bcatStatic(output, "--- closing\n");
2023-07-10 22:18:40 -03:00
// TODO: Use a stack of open shortcodes rather than 1
2023-07-10 22:26:19 -03:00
printf("closing from %s to %s\n", open_name->data, new_name->data);
if (bstrcmp(open_name, new_name) != 0) {
// Closing the wrong code
return 0;
}
btrunc(open_name,0);
2023-07-10 22:11:43 -03:00
};
2023-07-10 20:36:50 -03:00
2023-07-10 21:55:45 -03:00
main := (any* (shortcode | closing_shortcode))*;
2023-07-10 16:39:01 -03:00
}%%
bstring parse(char *input) {
2023-07-10 16:39:01 -03:00
%%write data;
char *eof, *ts, *te = 0;
int cs, act = 0;
char *p = input;
char *pe = p + strlen(input);
2023-07-10 22:11:43 -03:00
bstring open_name = bfromcstr("");
bstring new_name = bfromcstr("");
2023-07-10 16:39:01 -03:00
2023-07-10 18:11:54 -03:00
char *mark = 0;
bstring output = bfromcstr("");
2023-07-10 16:39:01 -03:00
%% write init;
%% write exec;
return output;
}
int main(int argc, char **argv) {
2023-07-10 21:55:45 -03:00
bstring output = parse("
{{< o1 arg1 >}}
2023-07-10 22:26:19 -03:00
{{< c1 arg2 >}}
2023-07-10 21:55:45 -03:00
{{% /c1%}}");
2023-07-10 22:26:19 -03:00
if (output == 0) {
printf("parse error\n");
return 1;
}
2023-07-10 16:39:01 -03:00
printf("\n%s\n", output->data);
return 0;
}