commit 13cc0916743705e32211da7fcf5499197e36e883 from: Omar Polo date: Wed Sep 30 12:47:14 2020 UTC improved error messages yyerror now is a printf-like variadic function. This helps to generate custom error message like "invalid keysym %s" or "invalid key %s" during parsing. commit - ecc6fa05e1c8a3082eb9612873a27f1ebf8ffcce commit + 13cc0916743705e32211da7fcf5499197e36e883 blob - 4e5955af2fe1ed655c3bba62b1fc22931d2df5d4 blob + 943a39c29b407715da6bf55e5a0d43d66ee83837 --- lex.l +++ lex.l @@ -67,9 +67,6 @@ KeySym key = 0; "\"" { BEGIN(INITIAL); - if (key == NoSymbol) - return TERR; - yylval.key = (struct key){ state, key }; state = 0; key = 0; @@ -81,7 +78,7 @@ KeySym key = 0; M- { state |= Mod1Mask; } s- { state |= Mod4Mask; } -<[_a-zA-Z]+> { +<[^>]+> { char *c; if ((c = strdup(yytext)) == NULL) @@ -90,7 +87,8 @@ KeySym key = 0; c++; /* skip the < */ c[strlen(c)-1] = '\0'; /* trim the > */ - key = XStringToKeysym(c); + if ((key = XStringToKeysym(c)) == NoSymbol) + yyerror("invalid keysym %s", c); free(--c); } @@ -101,7 +99,10 @@ KeySym key = 0; "(" { key = XK_parenleft; } ")" { key = XK_parenright; } -. { key = XStringToKeysym(yytext); } +. { + if ((key = XStringToKeysym(yytext)) == NoSymbol) + yyerror("invalid key %s", yytext); + } %% blob - 38d035c9d29844ffb29ddd28df94616f0668778f blob + ed7bd95c1204a39113a29c6b727504fe3c8a9cc5 --- parse.y +++ parse.y @@ -19,12 +19,8 @@ #include -#include - #include "star-platinum.h" -void yyerror(const char*); - /* * #define YYDEBUG 1 * int yydebug = 1; blob - 5c6cd545dc077b1bb4937d047c90389712674942 blob + 232d734c950fdfca53cb00a1cac2b6ed52927914 --- star-platinum.c +++ star-platinum.c @@ -17,6 +17,7 @@ #include "star-platinum.h" #include +#include #include #include #include @@ -49,11 +50,24 @@ int ignored_modifiers[] = { #define IGNORE_MODIFIERS (LockMask | Mod2Mask | Mod3Mask | Mod5Mask) +__attribute__((__format__ (__printf__, 1, 2))) void -yyerror(const char *e) +yyerror(const char *fmt, ...) { + va_list ap; + size_t l; + + va_start(ap, fmt); + goterror = 1; - fprintf(stderr, "%s:%d %s\n", fname, yylineno, e); + fprintf(stderr, "%s:%d ", fname, yylineno); + vfprintf(stderr, fmt, ap); + + l = strlen(fmt); + if (l > 0 && fmt[l-1] != '\n') + fprintf(stderr, "\n"); + + va_end(ap); } char * blob - 4ed031b1a95ecbcdd4753c4ea0b3a6a416ac9298 blob + 88db9ef46ddebea1d3579a804908d39b992ff23c --- star-platinum.h +++ star-platinum.h @@ -27,6 +27,8 @@ #include #include +void yyerror(const char*, ...); + struct key { unsigned int modifier; KeySym key;