2023-07-10 19:39:01 +00:00
|
|
|
#include <stdio.h>
|
2023-07-11 12:58:58 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include "bglibs/str.h"
|
2023-07-10 21:11:54 +00:00
|
|
|
|
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+;
|
2023-07-10 23:36:50 +00:00
|
|
|
path = (alnum | '/' )+;
|
2023-07-10 21:11:54 +00:00
|
|
|
|
2023-07-11 00:55:45 +00:00
|
|
|
name = (alpha+ path?)
|
2023-07-10 21:11:54 +00:00
|
|
|
> mark
|
2023-07-11 23:18:05 +00:00
|
|
|
%{ shortcodes[c_sc].name.start = mark-start;
|
|
|
|
shortcodes[c_sc].name.len = p-mark;
|
|
|
|
shortcodes[c_sc].matching = 0;
|
2023-07-11 12:58:58 +00:00
|
|
|
str_copyb(&new_name, mark, p-mark);
|
2023-07-10 23:30:50 +00:00
|
|
|
};
|
2023-07-11 00:55:45 +00:00
|
|
|
argname = alpha+
|
2023-07-10 21:17:17 +00:00
|
|
|
> mark
|
2023-07-11 12:58:58 +00:00
|
|
|
% {str_cats(&output, "A ");
|
|
|
|
str_catb(&output, mark, p-mark);
|
|
|
|
str_cats(&output, "\n");
|
2023-07-10 23:30:50 +00:00
|
|
|
};
|
|
|
|
qvalue = ('"' [^"]* '"')
|
2023-07-10 21:11:54 +00:00
|
|
|
> mark
|
2023-07-11 12:58:58 +00:00
|
|
|
% {str_cats(&output, "V ");
|
|
|
|
str_catb(&output, mark+1, p-mark-2);
|
|
|
|
str_cats(&output, "\n");
|
2023-07-10 23:30:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
value = alnum+
|
|
|
|
> mark
|
2023-07-11 12:58:58 +00:00
|
|
|
% {str_cats(&output, "V ");
|
|
|
|
str_catb(&output, mark, p-mark);
|
|
|
|
str_cats(&output, "\n");
|
2023-07-10 23:30:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
arg = ((argname '=')? (value|qvalue));
|
2023-07-10 19:39:01 +00:00
|
|
|
|
2023-07-10 23:30:50 +00:00
|
|
|
start_p = '{{%';
|
|
|
|
end_p = '%}}';
|
2023-07-10 19:39:01 +00:00
|
|
|
|
2023-07-10 23:30:50 +00:00
|
|
|
start_b = '{{<';
|
|
|
|
end_b = '>}}';
|
|
|
|
|
|
|
|
start = start_p | start_b ;
|
2023-07-11 19:51:11 +00:00
|
|
|
end = end_p | end_b ;
|
2023-07-10 23:30:50 +00:00
|
|
|
|
|
|
|
shortcode = (start spc name (sep arg)* spc end)
|
2023-07-11 23:18:05 +00:00
|
|
|
> {
|
|
|
|
shortcodes[c_sc].start = p-start-1;
|
|
|
|
}
|
2023-07-11 01:11:43 +00:00
|
|
|
@ {
|
2023-07-11 12:58:58 +00:00
|
|
|
str_copy(&open_name, &new_name);
|
2023-07-11 23:18:05 +00:00
|
|
|
shortcodes[c_sc].len = p-start-shortcodes[c_sc].start+1;
|
|
|
|
data_mark = p+1;
|
|
|
|
c_sc++;
|
2023-07-11 01:11:43 +00:00
|
|
|
};
|
2023-07-10 23:30:50 +00:00
|
|
|
|
2023-07-11 13:22:18 +00:00
|
|
|
closing_shortcode = (start spc '/' name spc end)
|
|
|
|
> {
|
|
|
|
// Starting a shortcode, close data
|
2023-07-11 23:18:05 +00:00
|
|
|
shortcodes[c_sc-1].data.start = data_mark-start;
|
|
|
|
shortcodes[c_sc-1].data.len = p-data_mark-1;
|
|
|
|
str_copyb(&sc, start+shortcodes[c_sc-1].data.start, shortcodes[c_sc-1].data.len);
|
2023-07-11 13:22:18 +00:00
|
|
|
};
|
|
|
|
|
2023-07-11 01:29:53 +00:00
|
|
|
matched_shortcode = (shortcode any* closing_shortcode)
|
2023-07-11 01:11:43 +00:00
|
|
|
@ {
|
2023-07-11 23:18:05 +00:00
|
|
|
shortcodes[c_sc-1].matching = 1;
|
|
|
|
shortcodes[c_sc-1].len = p-start-shortcodes[c_sc-1].start + 1;
|
|
|
|
shortcodes[c_sc].name.start = 0;
|
2023-07-11 12:58:58 +00:00
|
|
|
if (str_cmp(&open_name, 0, &new_name, 0) != 0) {
|
2023-07-11 01:26:19 +00:00
|
|
|
// Closing the wrong code
|
2023-07-11 12:58:58 +00:00
|
|
|
// TODO error reporting
|
2023-07-11 23:18:05 +00:00
|
|
|
return;
|
2023-07-11 01:26:19 +00:00
|
|
|
}
|
2023-07-11 12:58:58 +00:00
|
|
|
str_truncate(&open_name,0);
|
2023-07-11 01:11:43 +00:00
|
|
|
};
|
2023-07-10 23:36:50 +00:00
|
|
|
|
2023-07-11 01:29:53 +00:00
|
|
|
main := (any* (shortcode | matched_shortcode))*;
|
2023-07-10 19:39:01 +00:00
|
|
|
}%%
|
|
|
|
|
2023-07-11 23:18:05 +00:00
|
|
|
|
|
|
|
struct chunk {
|
|
|
|
int start, len;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct chunk chunk;
|
|
|
|
|
|
|
|
struct shortcode {
|
|
|
|
int start;
|
|
|
|
int len;
|
|
|
|
chunk name;
|
|
|
|
chunk data;
|
|
|
|
char matching;
|
|
|
|
};
|
|
|
|
typedef struct shortcode shortcode;
|
|
|
|
|
|
|
|
void parse(char *input) {
|
2023-07-10 19:39:01 +00:00
|
|
|
%%write data;
|
|
|
|
char *eof, *ts, *te = 0;
|
|
|
|
int cs, act = 0;
|
|
|
|
|
2023-07-11 19:48:02 +00:00
|
|
|
char *start = input;
|
2023-07-10 23:30:50 +00:00
|
|
|
char *p = input;
|
|
|
|
char *pe = p + strlen(input);
|
2023-07-11 13:22:18 +00:00
|
|
|
str open_name, new_name, output, data;
|
2023-07-11 12:58:58 +00:00
|
|
|
str_init(&open_name);
|
|
|
|
str_init(&new_name);
|
|
|
|
str_init(&output);
|
2023-07-11 13:22:18 +00:00
|
|
|
str_init(&data);
|
|
|
|
|
2023-07-11 23:18:05 +00:00
|
|
|
shortcode shortcodes[1000];
|
|
|
|
int c_sc = 0;
|
|
|
|
|
2023-07-11 19:48:02 +00:00
|
|
|
int sc_start = 0;
|
|
|
|
int sc_end = 0;
|
|
|
|
|
2023-07-11 13:22:18 +00:00
|
|
|
char *mark = p;
|
|
|
|
char *data_mark = p;
|
2023-07-11 23:18:05 +00:00
|
|
|
str sc;
|
|
|
|
str_init(&sc);
|
2023-07-10 19:39:01 +00:00
|
|
|
|
|
|
|
%% write init;
|
|
|
|
%% write exec;
|
|
|
|
|
2023-07-11 23:18:05 +00:00
|
|
|
for (int i=0; shortcodes[i].name.start!=0; i++) {
|
|
|
|
str_copyb(&sc, start + shortcodes[i].name.start, shortcodes[i].name.len);
|
|
|
|
printf("sc_name: %s (%d)\n", sc.s, shortcodes[i].matching );
|
|
|
|
str_copyb(&sc, start + shortcodes[i].start, shortcodes[i].len);
|
|
|
|
printf("full_sc: %s\n", sc.s);
|
|
|
|
if (shortcodes[i].matching) {
|
|
|
|
str_copyb(&sc, start + shortcodes[i].data.start, shortcodes[i].data.len);
|
|
|
|
printf("sc_data: %s\n", sc.s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-07-11 13:22:18 +00:00
|
|
|
|
2023-07-10 19:39:01 +00:00
|
|
|
int main(int argc, char **argv) {
|
2023-07-11 23:18:05 +00:00
|
|
|
parse("{{< c1 arg2 >}}foobar{{% /c1%}}{{% sarasa %}}");
|
2023-07-11 12:58:58 +00:00
|
|
|
// if (output == 0) {
|
|
|
|
// printf("parse error\n");
|
|
|
|
// return 1;
|
|
|
|
// }
|
2023-07-10 19:39:01 +00:00
|
|
|
return 0;
|
|
|
|
}
|