Start extracting data properly

This commit is contained in:
Roberto Alsina 2023-07-11 20:18:05 -03:00
parent b0fde035c7
commit a4f98c56bc

View File

@ -1,7 +1,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "bglibs/str.h" #include "bglibs/str.h"
#include "bglibs/gstack.h"
%%{ %%{
machine shortcode; machine shortcode;
@ -16,9 +15,9 @@
name = (alpha+ path?) name = (alpha+ path?)
> mark > mark
% {str_cats(&output, "N "); %{ shortcodes[c_sc].name.start = mark-start;
str_catb(&output, mark, p-mark); shortcodes[c_sc].name.len = p-mark;
str_cats(&output, "\n"); shortcodes[c_sc].matching = 0;
str_copyb(&new_name, mark, p-mark); str_copyb(&new_name, mark, p-mark);
}; };
argname = alpha+ argname = alpha+
@ -53,28 +52,33 @@
end = end_p | end_b ; end = end_p | end_b ;
shortcode = (start spc name (sep arg)* spc end) shortcode = (start spc name (sep arg)* spc end)
> {sc_start = p-start;} > {
shortcodes[c_sc].start = p-start-1;
}
@ { @ {
str_cats(&output, "+++ opening\n");
str_copy(&open_name, &new_name); str_copy(&open_name, &new_name);
data_mark = p; shortcodes[c_sc].len = p-start-shortcodes[c_sc].start+1;
data_mark = p+1;
c_sc++;
}; };
closing_shortcode = (start spc '/' name spc end) closing_shortcode = (start spc '/' name spc end)
> { > {
// Starting a shortcode, close data // Starting a shortcode, close data
str_copyb(&data, data_mark+1, p-data_mark-2); 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);
}; };
matched_shortcode = (shortcode any* closing_shortcode) matched_shortcode = (shortcode any* closing_shortcode)
@ { @ {
str_cats(&output, "--- closing\n"); shortcodes[c_sc-1].matching = 1;
printf("closing from %s to %s\n", open_name.s, new_name.s); shortcodes[c_sc-1].len = p-start-shortcodes[c_sc-1].start + 1;
printf("data: %s\n", data.s); shortcodes[c_sc].name.start = 0;
if (str_cmp(&open_name, 0, &new_name, 0) != 0) { if (str_cmp(&open_name, 0, &new_name, 0) != 0) {
// Closing the wrong code // Closing the wrong code
// TODO error reporting // TODO error reporting
return output; return;
} }
str_truncate(&open_name,0); str_truncate(&open_name,0);
}; };
@ -82,7 +86,23 @@
main := (any* (shortcode | matched_shortcode))*; main := (any* (shortcode | matched_shortcode))*;
}%% }%%
str parse(char *input) {
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) {
%%write data; %%write data;
char *eof, *ts, *te = 0; char *eof, *ts, *te = 0;
int cs, act = 0; int cs, act = 0;
@ -96,37 +116,37 @@ str parse(char *input) {
str_init(&output); str_init(&output);
str_init(&data); str_init(&data);
shortcode shortcodes[1000];
int c_sc = 0;
int sc_start = 0; int sc_start = 0;
int sc_end = 0; int sc_end = 0;
char *mark = p; char *mark = p;
char *data_mark = p; char *data_mark = p;
str sc;
str_init(&sc);
%% write init; %% write init;
%% write exec; %% write exec;
return output;
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);
}
}
} }
struct shortcode {
str name;
int start;
int end;
};
int main(int argc, char **argv) { int main(int argc, char **argv) {
str output = parse(" parse("{{< c1 arg2 >}}foobar{{% /c1%}}{{% sarasa %}}");
sarasa
{{< o1 arg1 >}}
stuff
{{< c1 arg2 >}}foobar{{% /c1%}}
{{< o2 arg1 >}}
{{< o3 arg1 >}}
more stuff
");
// if (output == 0) { // if (output == 0) {
// printf("parse error\n"); // printf("parse error\n");
// return 1; // return 1;
// } // }
printf("\n%s\n", output.s);
return 0; return 0;
} }