scanner: more verbose parser error messages
authorStefan Kost <ensonic@users.sf.net>
Sat, 5 Dec 2009 21:00:32 +0000 (23:00 +0200)
committerStefan Kost <ensonic@users.sf.net>
Sat, 5 Dec 2009 21:00:32 +0000 (23:00 +0200)
Keep track of the current line (the first 2000 chars of it) and include that in
syntax error messages. Also print that failed token in the error message.

giscanner/scannerlexer.l
giscanner/scannerparser.y

index cee9160..ff1ee0f 100644 (file)
@@ -37,6 +37,7 @@
 #include "grealpath.h"
 
 int lineno;
+char linebuf[2000];
 
 #undef YY_BUF_SIZE
 #define YY_BUF_SIZE 1048576
@@ -61,7 +62,12 @@ stringtext                           ([^\\\"])|(\\.)
 
 %%
 
-"\n"                                   { ++lineno; } /* " */
+\n.*                                   { strncpy(linebuf, yytext+1, sizeof(linebuf)); /* save the next line */
+                                               linebuf[sizeof(linebuf)-1]='\0';
+                                               /* printf("%4d:%s\n",lineno,linebuf); */
+                                               yyless(1);      /* give back all but the \n to rescan */
+                                               ++lineno;
+                                       }
 "\\\n"                                 { ++lineno; }
 [\t\f\v\r ]+                           { /* Ignore whitespace. */ }
 
index c92a538..7b18f94 100644 (file)
@@ -38,6 +38,7 @@
 
 extern FILE *yyin;
 extern int lineno;
+extern char linebuf[2000];
 extern char *yytext;
 
 extern int yylex (GISourceScanner *scanner);
@@ -1281,8 +1282,8 @@ yyerror (GISourceScanner *scanner, const char *s)
    * have valid expressions */
   if (!scanner->macro_scan)
     {
-      fprintf(stderr, "%s:%d: %s\n",
-             scanner->current_filename, lineno, s);
+      fprintf(stderr, "%s:%d: %s in '%s' at '%s'\n",
+             scanner->current_filename, lineno, s, linebuf, yytext);
     }
 }