From: Andy Spencer Date: Wed, 21 Dec 2011 06:28:52 +0000 (+0000) Subject: I forget what these changes do X-Git-Url: http://pileus.org/git/?p=~andy%2Fct;a=commitdiff_plain;h=dbf3732e1deb583f22cd97f20e01715de7679df9 I forget what these changes do (but they're obviously better) --- diff --git a/ct.c b/ct.c index 93eac2e..b9114a9 100644 --- a/ct.c +++ b/ct.c @@ -4,10 +4,12 @@ #include "parse.h" -gpointer parse(FILE *input, GList **data, GList **code); +gpointer parse(FILE *input, const char *name, + GList **data, GList **code); int main(int argc, char **argv) { + const char *name = "stdin"; /* Parse arguments */ char *option_output = NULL; GOptionEntry entries[] = { @@ -21,8 +23,10 @@ int main(int argc, char **argv) /* Handle input and output */ FILE *input = stdin; - if (argv[1] && !g_str_equal(input, "-")) + if (argv[1] && !g_str_equal(input, "-")) { + name = argv[1]; input = fopen(argv[1], "r"); + } if (!input) g_error("invalid input file"); @@ -41,7 +45,7 @@ int main(int argc, char **argv) /* Start compiling */ GList *data = NULL; GList *code = NULL; - parse(input, &data, &code); + parse(input, name, &data, &code); data = g_list_reverse(data); code = g_list_reverse(code); diff --git a/example/html.ct b/example/html.ct index d08df11..7f36cc9 100644 --- a/example/html.ct +++ b/example/html.ct @@ -11,6 +11,8 @@ Content-Type: application/xhtml+xml; charset=UTF-8 <% body(); %> + + <%= "%05.2f" %> <% } %> diff --git a/knot/overhand.txt b/knot/overhand.txt index 6dc976d..de5057b 100644 --- a/knot/overhand.txt +++ b/knot/overhand.txt @@ -2,3 +2,18 @@ .-|- '|' | + + _ _ +( \ ) + / / +/ " \ + +-. _ _ .- + \ \ \ + \" "/ + '-' + + _ _ + / \ \ +--' '|- + '---' diff --git a/parse.y b/parse.y index 84a62be..f4ce105 100644 --- a/parse.y +++ b/parse.y @@ -5,12 +5,14 @@ extern FILE *yyin; int yylex(void); void yyerror(gpointer node, char const *s); -static GList *code = NULL; -static GList *data = NULL; +extern int yylineno; +static const char *name; +static GList *code; +static GList *data; %} %error-verbose %parse-param {gpointer root}; -%token START END DATA OUT +%token START END DATA FMT OUT %% input : all | all input ; @@ -20,11 +22,13 @@ all : data | code | print ; data : DATA { static int i = 0; data = g_list_prepend(data, g_strdup_printf( + "#line %d \"%s\"\n" "static char data%d[] = \"%s\";\n", - i, g_strescape($1, ""))); + yylineno, name, i, g_strescape($1, ""))); code = g_list_prepend(code, g_strdup_printf( + "#line %d \"%s\"\n" "fwrite(data%d, sizeof(data%d)-1, 1, stdout);\n", - i, i)); + yylineno, name, i, i)); i++; }; @@ -32,18 +36,26 @@ code : START DATA END { code = g_list_prepend(code, g_strdup_printf("%s\n", $2)); }; -print : START OUT DATA END { +print : START FMT DATA END { code = g_list_prepend(code, g_strdup_printf("printf(%s);\n", $3)); }; +print : START OUT DATA END { + code = g_list_prepend(code, g_strdup_printf("printf(\"%%s\", %s);\n", $3)); +}; + %% -gpointer parse(FILE *input, GList **_data, GList **_code) { +gpointer parse(FILE *input, const char *_name, + GList **_data, GList **_code) +{ yyin = input; + name = _name; yyparse(NULL); *_data = data; *_code = code; return NULL; } -void yyerror(gpointer root, char const *s) { +void yyerror(gpointer root, char const *s) +{ g_error("[%s]\n", s); } diff --git a/scan.l b/scan.l index c1b61e7..44e7af6 100644 --- a/scan.l +++ b/scan.l @@ -7,17 +7,19 @@ %option noyywrap %option nounput %option noinput +%option yylineno /* %option nodebug */ START [ \t]*<% -END [ \t]*%>[ \t]* +END [ \t]*%> DATA ([^<\n]|<[^%])*\n* CODE [ \t\n]([^%\n]|%[^>])*\n* -%s IN FMT +%s IN %% [[:space:]]*\n { debug("NL [%s]"); } {START} { debug("START [%s]"); yylval = g_strdup(yytext); BEGIN(IN); return START; } {DATA} { debug("DATA [%s]"); yylval = g_strdup(yytext); return DATA; } {END} { debug("END [%s]"); yylval = g_strdup(yytext); BEGIN(INITIAL); return END; } = { debug("OUT [%s]"); yylval = g_strdup(yytext); return OUT; } +% { debug("FMT [%s]"); yylval = g_strdup(yytext); return FMT; } {CODE} { debug("CODE [%s]"); yylval = g_strdup(yytext); return DATA; } %%