]> Pileus Git - ~andy/ct/blob - parse.y
I forget what these changes do
[~andy/ct] / parse.y
1 %{
2 #define YYSTYPE char *
3 #include <stdio.h>
4 #include <glib.h>
5 extern FILE *yyin;
6 int yylex(void);
7 void yyerror(gpointer node, char const *s);
8 extern int yylineno;
9 static const char *name;
10 static GList *code;
11 static GList *data;
12 %}
13 %error-verbose
14 %parse-param {gpointer root};
15 %token START END DATA FMT OUT
16 %%
17
18 input : all | all input ;
19
20 all   : data | code | print ;
21
22 data  : DATA {
23         static int i = 0;
24         data = g_list_prepend(data, g_strdup_printf(
25                 "#line %d \"%s\"\n"
26                 "static char data%d[] = \"%s\";\n",
27                 yylineno, name, i, g_strescape($1, "")));
28         code = g_list_prepend(code, g_strdup_printf(
29                 "#line %d \"%s\"\n"
30                 "fwrite(data%d, sizeof(data%d)-1, 1, stdout);\n",
31                 yylineno, name, i, i));
32         i++;
33 };
34
35 code  : START DATA END {
36         code = g_list_prepend(code, g_strdup_printf("%s\n", $2));
37 };
38
39 print : START FMT DATA END {
40         code = g_list_prepend(code, g_strdup_printf("printf(%s);\n", $3));
41 };
42
43 print : START OUT DATA END {
44         code = g_list_prepend(code, g_strdup_printf("printf(\"%%s\", %s);\n", $3));
45 };
46
47 %%
48 gpointer parse(FILE *input, const char *_name,
49                 GList **_data, GList **_code)
50 {
51         yyin = input;
52         name = _name;
53         yyparse(NULL);
54         *_data = data;
55         *_code = code;
56         return NULL;
57 }
58 void yyerror(gpointer root, char const *s)
59 {
60         g_error("[%s]\n", s);
61 }