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