From 268bf712221de86612ec628a44d1e85ba83db519 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Sun, 13 Sep 2009 14:54:27 +0200 Subject: [PATCH] Implement option -p to test if the lexer/parser can parse your configfile (specify -c before) --- src/cfgparse.l | 25 +++++++----------- src/cfgparse.y | 72 +++++++++++++++++++++++++++++++++++--------------- src/mainx.c | 8 +++++- 3 files changed, 66 insertions(+), 39 deletions(-) diff --git a/src/cfgparse.l b/src/cfgparse.l index af26c6ae..c11028a6 100644 --- a/src/cfgparse.l +++ b/src/cfgparse.l @@ -3,35 +3,29 @@ #include "cfgparse.tab.h" #include -enum { - BIND_NONE = 0, - BIND_SHIFT = XCB_MOD_MASK_SHIFT, /* (1 << 0) */ - BIND_CONTROL = XCB_MOD_MASK_CONTROL, /* (1 << 2) */ - BIND_MOD1 = XCB_MOD_MASK_1, /* (1 << 3) */ - BIND_MOD2 = XCB_MOD_MASK_2, /* (1 << 4) */ - BIND_MOD3 = XCB_MOD_MASK_3, /* (1 << 5) */ - BIND_MOD4 = XCB_MOD_MASK_4, /* (1 << 6) */ - BIND_MOD5 = XCB_MOD_MASK_5, /* (1 << 7) */ - BIND_MODE_SWITCH = (1 << 8) -}; +#include "data.h" %} -%Start BIND_COND BIND_AWS_COND BIND_A2WS_COND ASSIGN_COND +%Start BIND_COND BIND_AWS_COND BIND_A2WS_COND ASSIGN_COND COLOR_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; } +[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 { /* TODO: lval pointer */ return TOKCOLOR; } +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; @@ -44,8 +38,7 @@ shift return TOKSHIFT; [ \t]+ { BEGIN(BIND_AWS_COND); return WHITESPACE; } [ \t]+ { BEGIN(BIND_A2WS_COND); return WHITESPACE; } [ \t]+ return WHITESPACE; -[^\n]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STRING; } -[^ \t]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STRING_NG; } +[^ \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; \"[^\"]+\" { diff --git a/src/cfgparse.y b/src/cfgparse.y index 2cf408a7..4e09e2b1 100644 --- a/src/cfgparse.y +++ b/src/cfgparse.y @@ -3,31 +3,34 @@ #include #include -enum { - BIND_NONE = 0, - BIND_SHIFT = XCB_MOD_MASK_SHIFT, /* (1 << 0) */ - BIND_CONTROL = XCB_MOD_MASK_CONTROL, /* (1 << 2) */ - BIND_MOD1 = XCB_MOD_MASK_1, /* (1 << 3) */ - BIND_MOD2 = XCB_MOD_MASK_2, /* (1 << 4) */ - BIND_MOD3 = XCB_MOD_MASK_3, /* (1 << 5) */ - BIND_MOD4 = XCB_MOD_MASK_4, /* (1 << 6) */ - BIND_MOD5 = XCB_MOD_MASK_5, /* (1 << 7) */ - BIND_MODE_SWITCH = (1 << 8) -}; +#include "data.h" +extern int yylex(void); +extern FILE *yyin; int yydebug = 1; -void yyerror(const char *str) -{ +void yyerror(const char *str) { fprintf(stderr,"error: %s\n",str); } -int yywrap() -{ +int yywrap() { return 1; } +void parse_file(const char *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); +} + #if 0 main() { @@ -45,11 +48,14 @@ main() %token NUMBER %token WORD -%token STRING -%token STRING_NG +%token STR +%token STR_NG %token VARNAME %token HEX %token TOKBIND +%token TOKTERMINAL +%token TOKCOMMENT +%token TOKFONT %token TOKBINDSYM %token MODIFIER %token TOKCONTROL @@ -83,10 +89,17 @@ line: | ipcsocket | exec | color + | terminal + | font + | comment ; +comment: + TOKCOMMENT + ; + command: - STRING + STR ; bind: @@ -135,7 +148,7 @@ assign: window_class: QUOTEDSTRING - | STRING_NG + | STR_NG ; optional_arrow: @@ -144,7 +157,7 @@ optional_arrow: ; set: - TOKSET WHITESPACE variable WHITESPACE STRING + TOKSET WHITESPACE variable WHITESPACE STR { printf("set %s to %s\n", $3, $5); } @@ -156,19 +169,34 @@ variable: ; ipcsocket: - TOKIPCSOCKET WHITESPACE STRING + TOKIPCSOCKET WHITESPACE STR { printf("ipc %s\n", $3); } ; exec: - TOKEXEC WHITESPACE STRING + TOKEXEC WHITESPACE STR { printf("exec %s\n", $3); } ; +terminal: + TOKTERMINAL WHITESPACE STR + { + printf("terminal %s\n", $3); + } + ; + +font: + TOKFONT WHITESPACE STR + { + printf("font %s\n", $3); + } + ; + + color: TOKCOLOR WHITESPACE '#' HEX { diff --git a/src/mainx.c b/src/mainx.c index db3d184a..cb385c89 100644 --- a/src/mainx.c +++ b/src/mainx.c @@ -165,7 +165,7 @@ int main(int argc, char *argv[], char *env[]) { start_argv = argv; - while ((opt = getopt_long(argc, argv, "c:vah", long_options, &option_index)) != -1) { + while ((opt = getopt_long(argc, argv, "c:vahp", long_options, &option_index)) != -1) { switch (opt) { case 'a': LOG("Autostart disabled using -a\n"); @@ -177,6 +177,12 @@ int main(int argc, char *argv[], char *env[]) { case 'v': printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n"); exit(EXIT_SUCCESS); + case 'p': + { + printf("parsing\n"); + parse_file(override_configpath); + exit(0); + } default: fprintf(stderr, "Usage: %s [-c configfile] [-a] [-v]\n", argv[0]); fprintf(stderr, "\n"); -- 2.39.5