Start extracting data properly
This commit is contained in:
parent
b0fde035c7
commit
a4f98c56bc
@ -1,7 +1,6 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "bglibs/str.h"
|
||||
#include "bglibs/gstack.h"
|
||||
|
||||
%%{
|
||||
machine shortcode;
|
||||
@ -16,9 +15,9 @@
|
||||
|
||||
name = (alpha+ path?)
|
||||
> mark
|
||||
% {str_cats(&output, "N ");
|
||||
str_catb(&output, mark, p-mark);
|
||||
str_cats(&output, "\n");
|
||||
%{ shortcodes[c_sc].name.start = mark-start;
|
||||
shortcodes[c_sc].name.len = p-mark;
|
||||
shortcodes[c_sc].matching = 0;
|
||||
str_copyb(&new_name, mark, p-mark);
|
||||
};
|
||||
argname = alpha+
|
||||
@ -53,28 +52,33 @@
|
||||
end = end_p | end_b ;
|
||||
|
||||
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);
|
||||
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)
|
||||
> {
|
||||
// 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)
|
||||
@ {
|
||||
str_cats(&output, "--- closing\n");
|
||||
printf("closing from %s to %s\n", open_name.s, new_name.s);
|
||||
printf("data: %s\n", data.s);
|
||||
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;
|
||||
if (str_cmp(&open_name, 0, &new_name, 0) != 0) {
|
||||
// Closing the wrong code
|
||||
// TODO error reporting
|
||||
return output;
|
||||
return;
|
||||
}
|
||||
str_truncate(&open_name,0);
|
||||
};
|
||||
@ -82,7 +86,23 @@
|
||||
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;
|
||||
char *eof, *ts, *te = 0;
|
||||
int cs, act = 0;
|
||||
@ -96,37 +116,37 @@ str parse(char *input) {
|
||||
str_init(&output);
|
||||
str_init(&data);
|
||||
|
||||
shortcode shortcodes[1000];
|
||||
int c_sc = 0;
|
||||
|
||||
int sc_start = 0;
|
||||
int sc_end = 0;
|
||||
|
||||
char *mark = p;
|
||||
char *data_mark = p;
|
||||
str sc;
|
||||
str_init(&sc);
|
||||
|
||||
%% write init;
|
||||
%% 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) {
|
||||
str output = parse("
|
||||
sarasa
|
||||
{{< o1 arg1 >}}
|
||||
stuff
|
||||
{{< c1 arg2 >}}foobar{{% /c1%}}
|
||||
{{< o2 arg1 >}}
|
||||
{{< o3 arg1 >}}
|
||||
more stuff
|
||||
");
|
||||
parse("{{< c1 arg2 >}}foobar{{% /c1%}}{{% sarasa %}}");
|
||||
// if (output == 0) {
|
||||
// printf("parse error\n");
|
||||
// return 1;
|
||||
// }
|
||||
printf("\n%s\n", output.s);
|
||||
return 0;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user