changeset 64:f712161c0bf9

gen: Disable debug output for production use in generated parser
author Lewin Bormann <lbo@spheniscida.de>
date Sun, 25 Aug 2019 10:05:37 +0200
parents fed8eb63d9ab
children dd0c65ded420
files gen/y.yy
diffstat 1 files changed, 28 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/gen/y.yy	Sun Aug 25 08:28:08 2019 +0200
+++ b/gen/y.yy	Sun Aug 25 10:05:37 2019 +0200
@@ -15,8 +15,12 @@
 
 #include <gen/y.tab.h>
 
+bool YENABLE_DEBUG_PRINT = false;
+
+#define ydbg_print(...) if (YENABLE_DEBUG_PRINT) fprintf(stderr, __VA_ARGS__)
+
 // Our error handler
-void yyerror(void* ignored, void* ignored2, char const* s);
+void yyerror(void* ignored, void* ignored2, ystr_t* ystr_error, char const* s);
 
 // Declare flex-generated functions
 int yylex(YYSTYPE* v, void* scanner);
@@ -35,6 +39,10 @@
 %}
 %parse-param {yvec_t* output_yexprs}
 
+%{ // The ystr_error parameter is only present in yyparse.
+%}
+%parse-param {ystr_t* ystr_error}
+
 %{ // Supply yylval argument to yylex().
 %}
 %define api.pure full
@@ -84,14 +92,12 @@
             // Assign overall result (list of yexpr_t) to parser's output
             // argument.
             *output_yexprs = $1;
-            printf("finished\n");
+            ydbg_print("finished\n");
     }
 
 many_sexpr: many_sexpr sexpr {
             // Push found sexpr to previous sexprs.
             YVEC_PUSH(&$1, &$2);
-            yexpr_debug(&$2);
-            fputs("\n", stderr);
             // Push result on stack.
             $$ = $1;
     }
@@ -102,11 +108,11 @@
     };
 
 sexpr: TOK_QUOTE TOK_LEFTP inner_quoted_sexpr TOK_RIGHTP {
-                printf("found quoted sexpr\n");
+                ydbg_print("found quoted sexpr\n");
                 $$ = $3;
     }
     | TOK_LEFTP inner_sexpr TOK_RIGHTP {
-                printf("found sexpr\n");
+                ydbg_print("found sexpr\n");
                 $$ = $2;
     };
 
@@ -120,7 +126,7 @@
                   $$ = $1;
             }
             | %empty {
-                  printf("finished quoted sexpr\n");
+                  ydbg_print("finished quoted sexpr\n");
                   yexpr_t e;
                   yexpr_init(&e);
                   yvec_t v;
@@ -138,7 +144,7 @@
                 $$ = $1;
            }
            | %empty {
-                printf("finished sexpr\n");
+                ydbg_print("finished sexpr\n");
                 yexpr_t e;
                 yexpr_init(&e);
                 yvec_t v;
@@ -148,11 +154,11 @@
            };
 
 value: TOK_QUOTE quoted_value {
-        printf("found quoted value\n");
+        ydbg_print("found quoted value\n");
         $$ = $2;
      }
      | TOK_ATOM {
-        printf("found ID %s\n", $1);
+        ydbg_print("found ID %s\n", $1);
         yref_t ref = yref_new_c($1);
         yexpr_t expr;
         yexpr_set_ref(&expr, ref);
@@ -161,20 +167,20 @@
      | literal;
 
 quoted_value: TOK_ATOM {
-        printf("found atom %s\n", $1);
+        ydbg_print("found atom %s\n", $1);
         yexpr_t expr;
         yexpr_set_atom_name(&expr, $1);
         $$ = expr;
      } | literal;
 
 literal: TOK_NUMBER_LITERAL {
-        printf("found number %d\n", $1);
+        ydbg_print("found number %d\n", $1);
         yexpr_t expr;
         yexpr_set_number(&expr, $1);
         $$ = expr;
      }
      | TOK_STRING_LITERAL {
-        printf("found string %s\n", $1);
+        ydbg_print("found string %s\n", $1);
         yexpr_t expr;
         ystr_t str = ystr_new($1);
         yexpr_set_str(&expr, str);
@@ -183,7 +189,7 @@
 
 %%
 
-int parse(FILE* in, yvec_t* out_exprs) {
+int parse(FILE* in, yvec_t* out_exprs, ystr_t* ystr_error) {
     void* scanner;
     int r;
     if (0 != (r = yylex_init(&scanner))) {
@@ -201,10 +207,15 @@
 
     YVEC_INIT(out_exprs, 16, yexpr_t);
 
-    yyparse(scanner, out_exprs);
+    int ret = yyparse(scanner, out_exprs, ystr_error);
 
-    fclose(in);
     fclose(out);
     assert(0 == yylex_destroy(scanner));
-    return 0;
+    return ret;
 }
+
+void yyerror(void* _scanner, void* _output, ystr_t* ystr_error, char const* s) {
+    ystr_t s2 = ystr_new(NULL);
+    ystr_set_owned(&s2, (char*)s); // no destroy!
+    ystr_append(ystr_error, &s2);
+}