From 2986636e03e5726e4935ac1b17735b5b47760b0e Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 13 Sep 2009 18:40:35 +0200 Subject: [PATCH] Fix some parsing problems, retab! --- Makefile | 2 +- src/cfgparse.l | 102 ++++++++++++++--------- src/cfgparse.y | 215 +++++++++++++++++++++++++------------------------ 3 files changed, 174 insertions(+), 145 deletions(-) diff --git a/Makefile b/Makefile index f14cdb44..3baea500 100644 --- a/Makefile +++ b/Makefile @@ -27,7 +27,7 @@ src/cfgparse.yy.o: src/cfgparse.l src/cfgparse.y.o: src/cfgparse.y echo "YACC $<" - yacc -b $(basename $< .y) -d $< + yacc --debug --verbose -b $(basename $< .y) -d $< $(CC) $(CFLAGS) -c -o $@ $(<:.y=.tab.c) install: all diff --git a/src/cfgparse.l b/src/cfgparse.l index c11028a6..e3bacae5 100644 --- a/src/cfgparse.l +++ b/src/cfgparse.l @@ -4,49 +4,71 @@ #include #include "data.h" +#include "config.h" %} -%Start BIND_COND BIND_AWS_COND BIND_A2WS_COND ASSIGN_COND COLOR_COND +%Start BIND_COND +%Start BIND_AWS_COND +%Start BIND_A2WS_COND +%Start ASSIGN_COND +%Start COLOR_COND +%Start SET_COND +%Start SET_AWS_COND +%Start SCREEN_COND +%Start SCREEN_AWS_COND %% -[^\n]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR; } -#[^\n]* return TOKCOMMENT; -[0-9]+ yylval.number = atoi(yytext); return NUMBER; -[0-9a-fA-F]+ { yylval.string = strdup(yytext); return HEX; } -bind { BEGIN(BIND_COND); return TOKBIND; } -bindsym { BEGIN(BIND_COND); return TOKBINDSYM; } -floating_modifier return TOKFLOATING_MODIFIER; -workspace return TOKWORKSPACE; -screen return TOKSCREEN; -terminal { BEGIN(BIND_AWS_COND); return TOKTERMINAL; } -font { BEGIN(BIND_AWS_COND); return TOKFONT; } -assign { BEGIN(ASSIGN_COND); return TOKASSIGN; } -set { BEGIN(BIND_COND); return TOKSET; } -ipc-socket { BEGIN(BIND_AWS_COND); return TOKIPCSOCKET; } -exec { BEGIN(BIND_AWS_COND); return TOKEXEC; } -client.focused { BEGIN(COLOR_COND); /* TODO: lval pointer */ return TOKCOLOR; } -Mod1 yylval.number = BIND_MOD1; return MODIFIER; -Mod2 yylval.number = BIND_MOD2; return MODIFIER; -Mod3 yylval.number = BIND_MOD3; return MODIFIER; -Mod4 yylval.number = BIND_MOD4; return MODIFIER; -Mod5 yylval.number = BIND_MOD5; return MODIFIER; -control return TOKCONTROL; -shift return TOKSHIFT; -→ return TOKARROW; -\n /* ignore end of line */; -[ \t]+ { BEGIN(BIND_AWS_COND); return WHITESPACE; } -[ \t]+ { BEGIN(BIND_A2WS_COND); return WHITESPACE; } -[ \t]+ return WHITESPACE; -[^ \t]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR_NG; } -[a-zA-Z]+ yylval.string = strdup(yytext); return WORD; -[a-zA-Z0-9_-]+ yylval.string = strdup(yytext); return VARNAME; -\"[^\"]+\" { - /* yylval will be the string, but without quotes */ - char *copy = strdup(yytext+1); - copy[strlen(copy)-1] = '\0'; - yylval.string = copy; - return QUOTEDSTRING; - } -. return (int) yytext[0]; +[^\n]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR; } +^#[^\n]* { return TOKCOMMENT; } +[0-9]+ { yylval.number = atoi(yytext); return NUMBER; } +[0-9a-fA-F]+ { yylval.string = strdup(yytext); return HEX; } +bind { BEGIN(BIND_COND); return TOKBIND; } +bindsym { BEGIN(BIND_COND); return TOKBINDSYM; } +floating_modifier { return TOKFLOATING_MODIFIER; } +workspace { BEGIN(INITIAL); return TOKWORKSPACE; } +screen { BEGIN(SCREEN_COND); return TOKSCREEN; } +terminal { BEGIN(BIND_AWS_COND); return TOKTERMINAL; } +font { BEGIN(BIND_AWS_COND); return TOKFONT; } +assign { BEGIN(ASSIGN_COND); return TOKASSIGN; } +set { BEGIN(SET_COND); return TOKSET; } +ipc-socket { BEGIN(BIND_AWS_COND); return TOKIPCSOCKET; } +exec { BEGIN(BIND_AWS_COND); return TOKEXEC; } +client.focused { BEGIN(COLOR_COND); yylval.color = &config.client.focused; return TOKCOLOR; } +client.focused_inactive { BEGIN(COLOR_COND); yylval.color = &config.client.focused_inactive; return TOKCOLOR; } +client.unfocused { BEGIN(COLOR_COND); yylval.color = &config.client.unfocused; return TOKCOLOR; } +client.urgent { BEGIN(COLOR_COND); yylval.color = &config.client.urgent; return TOKCOLOR; } +bar.focused { BEGIN(COLOR_COND); yylval.color = &config.client.focused; return TOKCOLOR; } +bar.unfocused { BEGIN(COLOR_COND); yylval.color = &config.client.unfocused; return TOKCOLOR; } +bar.urgent { BEGIN(COLOR_COND); yylval.color = &config.client.urgent; return TOKCOLOR; } +Mod1 { yylval.number = BIND_MOD1; return MODIFIER; } +Mod2 { yylval.number = BIND_MOD2; return MODIFIER; } +Mod3 { yylval.number = BIND_MOD3; return MODIFIER; } +Mod4 { yylval.number = BIND_MOD4; return MODIFIER; } +Mod5 { yylval.number = BIND_MOD5; return MODIFIER; } +control { return TOKCONTROL; } +shift { return TOKSHIFT; } +→ { return TOKARROW; } +\n /* ignore end of line */; +x { return (int)yytext[0]; } +[ \t]+ { BEGIN(BIND_AWS_COND); return WHITESPACE; } +[ \t]+ { BEGIN(SET_AWS_COND); return WHITESPACE; } +[ \t]+ { BEGIN(BIND_A2WS_COND); return WHITESPACE; } +[ \t]+ { BEGIN(BIND_A2WS_COND); return WHITESPACE; } +[ \t]+ { BEGIN(SCREEN_AWS_COND); return WHITESPACE; } +[ \t]+ { BEGIN(BIND_A2WS_COND); return WHITESPACE; } +[ \t]+ { return WHITESPACE; } +\"[^\"]+\" { + /* if ASSIGN_COND then */ + BEGIN(INITIAL); + /* yylval will be the string, but without quotes */ + char *copy = strdup(yytext+1); + copy[strlen(copy)-1] = '\0'; + yylval.string = copy; + return QUOTEDSTRING; + } +[^ \t]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR_NG; } +[a-zA-Z]+ { yylval.string = strdup(yytext); return WORD; } +[a-zA-Z0-9_-]+ { yylval.string = strdup(yytext); return VARNAME; } +. { return (int)yytext[0]; } %% diff --git a/src/cfgparse.y b/src/cfgparse.y index 4e09e2b1..eab11484 100644 --- a/src/cfgparse.y +++ b/src/cfgparse.y @@ -4,6 +4,7 @@ #include #include "data.h" +#include "config.h" extern int yylex(void); extern FILE *yyin; @@ -19,31 +20,32 @@ int yywrap() { } void parse_file(const char *f) { - printf("opening %s\n", f); + printf("opening %s\n", f); if ((yyin = fopen(f, "r")) == NULL) { - perror("fopen"); - exit(1); - } - if (yyparse() != 0) { - fprintf(stderr, "Could not parse configfile\n"); - exit(1); - } - fclose(yyin); + perror("fopen"); + exit(1); + } + if (yyparse() != 0) { + fprintf(stderr, "Could not parse configfile\n"); + exit(1); + } + fclose(yyin); } #if 0 main() { yyparse(); - printf("parsing done\n"); + printf("parsing done\n"); } #endif %} %union { - int number; - char *string; + int number; + char *string; + struct Colortriple *color; } %token NUMBER @@ -76,146 +78,151 @@ main() lines: /* empty */ | lines WHITESPACE line - | lines line + | lines line ; line: bind - | bindsym - | floating_modifier - | workspace - | assign - | set - | ipcsocket - | exec - | color - | terminal - | font - | comment + | bindsym + | floating_modifier + | workspace + | assign + | set + | ipcsocket + | exec + | color + | terminal + | font + | comment ; comment: - TOKCOMMENT - ; + TOKCOMMENT + ; command: - STR - ; + STR + ; bind: - TOKBIND WHITESPACE binding_modifiers '+' NUMBER WHITESPACE command + TOKBIND WHITESPACE binding_modifiers NUMBER WHITESPACE command { - printf("\tFound binding mod%d with key %d and command %s\n", $3, $5, $7); + printf("\tFound binding mod%d with key %d and command %s\n", $3, $4, $6); } ; bindsym: - TOKBINDSYM WHITESPACE binding_modifiers '+' WORD WHITESPACE command + TOKBINDSYM WHITESPACE binding_modifiers WORD WHITESPACE command { - printf("\tFound symbolic mod%d with key %s and command %s\n", $3, $5, $7); + printf("\tFound symbolic mod%d with key %s and command %s\n", $3, $4, $6); } ; floating_modifier: - TOKFLOATING_MODIFIER WHITESPACE binding_modifiers - { - printf("\tfloating modifier %d\n", $3); - } - ; + TOKFLOATING_MODIFIER WHITESPACE binding_modifiers + { + printf("\tfloating modifier %d\n", $3); + } + ; workspace: - TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen - { - printf("\t workspace %d to screen %d\n", $3, $7); - } - | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen WHITESPACE QUOTEDSTRING - { - printf("\t quoted: %s\n", $9); - } - ; + TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen + { + printf("\t workspace %d to screen %s\n", $3, $7); + } + | TOKWORKSPACE WHITESPACE NUMBER WHITESPACE TOKSCREEN WHITESPACE screen WHITESPACE workspace_name + { + printf("\t quoted: %s\n", $9); + } + ; -screen: - NUMBER - ; +workspace_name: + QUOTEDSTRING + | STR + ; +screen: + NUMBER { asprintf(&$$, "%d", $1); } + | NUMBER 'x' { asprintf(&$$, "%d", $1); } + | NUMBER 'x' NUMBER { asprintf(&$$, "%dx%d", $1, $3); } + | 'x' NUMBER { asprintf(&$$, "x%d", $2); } + ; assign: - TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow NUMBER - { - printf("assignment of %s to %d\n", $3, $6); - } - ; + TOKASSIGN WHITESPACE window_class WHITESPACE optional_arrow NUMBER + { + printf("assignment of %s to %d\n", $3, $6); + } + ; window_class: - QUOTEDSTRING - | STR_NG - ; + QUOTEDSTRING + | STR_NG + ; optional_arrow: - /* NULL */ - | TOKARROW WHITESPACE - ; + /* NULL */ + | TOKARROW WHITESPACE + ; set: - TOKSET WHITESPACE variable WHITESPACE STR - { - printf("set %s to %s\n", $3, $5); - } - ; + TOKSET WHITESPACE variable WHITESPACE STR + { + printf("set %s to %s\n", $3, $5); + } + ; variable: - '$' WORD { asprintf(&$$, "$%s", $2); } - | '$' VARNAME { asprintf(&$$, "$%s", $2); } - ; + '$' WORD { asprintf(&$$, "$%s", $2); } + | '$' VARNAME { asprintf(&$$, "$%s", $2); } + ; ipcsocket: - TOKIPCSOCKET WHITESPACE STR - { - printf("ipc %s\n", $3); - } - ; + TOKIPCSOCKET WHITESPACE STR + { + printf("ipc %s\n", $3); + } + ; exec: - TOKEXEC WHITESPACE STR - { - printf("exec %s\n", $3); - } - ; + TOKEXEC WHITESPACE STR + { + printf("exec %s\n", $3); + } + ; terminal: - TOKTERMINAL WHITESPACE STR - { - printf("terminal %s\n", $3); - } - ; + TOKTERMINAL WHITESPACE STR + { + printf("terminal %s\n", $3); + } + ; font: - TOKFONT WHITESPACE STR - { - printf("font %s\n", $3); - } - ; + TOKFONT WHITESPACE STR + { + printf("font %s\n", $3); + } + ; color: - TOKCOLOR WHITESPACE '#' HEX - { - printf("color %s\n", $4); - } - ; + TOKCOLOR WHITESPACE '#' HEX WHITESPACE '#' HEX WHITESPACE '#' HEX + { + printf("color %p, %s and %s and %s\n", $1, $4, $7, $10); + } + ; binding_modifiers: - binding_modifier - | - binding_modifiers '+' binding_modifier - { - $$ = $1 | $3; - } - ; + /* NULL */ { $$ = 0; } + | binding_modifier + | binding_modifiers '+' binding_modifier { $$ = $1 | $3; } + | binding_modifiers '+' { $$ = $1; } + ; binding_modifier: - MODIFIER { $$ = $1; } - | TOKCONTROL { $$ = BIND_CONTROL; } - | TOKSHIFT { $$ = BIND_SHIFT; } - ; + MODIFIER { $$ = $1; } + | TOKCONTROL { $$ = BIND_CONTROL; } + | TOKSHIFT { $$ = BIND_SHIFT; } + ; -- 2.39.5