Implement option -p to test if the lexer/parser can parse your configfile (specify -c before)

This commit is contained in:
Michael Stapelberg 2009-09-13 14:54:27 +02:00
parent 9dd975e974
commit 268bf71222
3 changed files with 66 additions and 39 deletions

View File

@ -3,35 +3,29 @@
#include "cfgparse.tab.h" #include "cfgparse.tab.h"
#include <xcb/xcb.h> #include <xcb/xcb.h>
enum { #include "data.h"
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)
};
%} %}
%Start BIND_COND BIND_AWS_COND BIND_A2WS_COND ASSIGN_COND %Start BIND_COND BIND_AWS_COND BIND_A2WS_COND ASSIGN_COND COLOR_COND
%% %%
<BIND_A2WS_COND>[^\n]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR; }
#[^\n]* return TOKCOMMENT;
[0-9]+ yylval.number = atoi(yytext); return NUMBER; [0-9]+ yylval.number = atoi(yytext); return NUMBER;
[0-9a-fA-F]+ { yylval.string = strdup(yytext); return HEX; } <COLOR_COND>[0-9a-fA-F]+ { yylval.string = strdup(yytext); return HEX; }
bind { BEGIN(BIND_COND); return TOKBIND; } bind { BEGIN(BIND_COND); return TOKBIND; }
bindsym { BEGIN(BIND_COND); return TOKBINDSYM; } bindsym { BEGIN(BIND_COND); return TOKBINDSYM; }
floating_modifier return TOKFLOATING_MODIFIER; floating_modifier return TOKFLOATING_MODIFIER;
workspace return TOKWORKSPACE; workspace return TOKWORKSPACE;
screen return TOKSCREEN; screen return TOKSCREEN;
terminal { BEGIN(BIND_AWS_COND); return TOKTERMINAL; }
font { BEGIN(BIND_AWS_COND); return TOKFONT; }
assign { BEGIN(ASSIGN_COND); return TOKASSIGN; } assign { BEGIN(ASSIGN_COND); return TOKASSIGN; }
set { BEGIN(BIND_COND); return TOKSET; } set { BEGIN(BIND_COND); return TOKSET; }
ipc-socket { BEGIN(BIND_AWS_COND); return TOKIPCSOCKET; } ipc-socket { BEGIN(BIND_AWS_COND); return TOKIPCSOCKET; }
exec { BEGIN(BIND_AWS_COND); return TOKEXEC; } 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; Mod1 yylval.number = BIND_MOD1; return MODIFIER;
Mod2 yylval.number = BIND_MOD2; return MODIFIER; Mod2 yylval.number = BIND_MOD2; return MODIFIER;
Mod3 yylval.number = BIND_MOD3; return MODIFIER; Mod3 yylval.number = BIND_MOD3; return MODIFIER;
@ -44,8 +38,7 @@ shift return TOKSHIFT;
<BIND_COND>[ \t]+ { BEGIN(BIND_AWS_COND); return WHITESPACE; } <BIND_COND>[ \t]+ { BEGIN(BIND_AWS_COND); return WHITESPACE; }
<BIND_AWS_COND>[ \t]+ { BEGIN(BIND_A2WS_COND); return WHITESPACE; } <BIND_AWS_COND>[ \t]+ { BEGIN(BIND_A2WS_COND); return WHITESPACE; }
[ \t]+ return WHITESPACE; [ \t]+ return WHITESPACE;
<BIND_A2WS_COND>[^\n]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STRING; } <ASSIGN_COND>[^ \t]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR_NG; }
<ASSIGN_COND>[^ \t]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STRING_NG; }
[a-zA-Z]+ yylval.string = strdup(yytext); return WORD; [a-zA-Z]+ yylval.string = strdup(yytext); return WORD;
[a-zA-Z0-9_-]+ yylval.string = strdup(yytext); return VARNAME; [a-zA-Z0-9_-]+ yylval.string = strdup(yytext); return VARNAME;
\"[^\"]+\" { \"[^\"]+\" {

View File

@ -3,31 +3,34 @@
#include <string.h> #include <string.h>
#include <xcb/xcb.h> #include <xcb/xcb.h>
enum { #include "data.h"
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)
};
extern int yylex(void);
extern FILE *yyin;
int yydebug = 1; int yydebug = 1;
void yyerror(const char *str) void yyerror(const char *str) {
{
fprintf(stderr,"error: %s\n",str); fprintf(stderr,"error: %s\n",str);
} }
int yywrap() int yywrap() {
{
return 1; 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 #if 0
main() main()
{ {
@ -45,11 +48,14 @@ main()
%token <number>NUMBER %token <number>NUMBER
%token <string>WORD %token <string>WORD
%token <string>STRING %token <string>STR
%token <string>STRING_NG %token <string>STR_NG
%token <string>VARNAME %token <string>VARNAME
%token <string>HEX %token <string>HEX
%token TOKBIND %token TOKBIND
%token TOKTERMINAL
%token TOKCOMMENT
%token TOKFONT
%token TOKBINDSYM %token TOKBINDSYM
%token MODIFIER %token MODIFIER
%token TOKCONTROL %token TOKCONTROL
@ -83,10 +89,17 @@ line:
| ipcsocket | ipcsocket
| exec | exec
| color | color
| terminal
| font
| comment
; ;
comment:
TOKCOMMENT
;
command: command:
STRING STR
; ;
bind: bind:
@ -135,7 +148,7 @@ assign:
window_class: window_class:
QUOTEDSTRING QUOTEDSTRING
| STRING_NG | STR_NG
; ;
optional_arrow: optional_arrow:
@ -144,7 +157,7 @@ optional_arrow:
; ;
set: set:
TOKSET WHITESPACE variable WHITESPACE STRING TOKSET WHITESPACE variable WHITESPACE STR
{ {
printf("set %s to %s\n", $<string>3, $<string>5); printf("set %s to %s\n", $<string>3, $<string>5);
} }
@ -156,19 +169,34 @@ variable:
; ;
ipcsocket: ipcsocket:
TOKIPCSOCKET WHITESPACE STRING TOKIPCSOCKET WHITESPACE STR
{ {
printf("ipc %s\n", $<string>3); printf("ipc %s\n", $<string>3);
} }
; ;
exec: exec:
TOKEXEC WHITESPACE STRING TOKEXEC WHITESPACE STR
{ {
printf("exec %s\n", $<string>3); printf("exec %s\n", $<string>3);
} }
; ;
terminal:
TOKTERMINAL WHITESPACE STR
{
printf("terminal %s\n", $<string>3);
}
;
font:
TOKFONT WHITESPACE STR
{
printf("font %s\n", $<string>3);
}
;
color: color:
TOKCOLOR WHITESPACE '#' HEX TOKCOLOR WHITESPACE '#' HEX
{ {

View File

@ -165,7 +165,7 @@ int main(int argc, char *argv[], char *env[]) {
start_argv = argv; 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) { switch (opt) {
case 'a': case 'a':
LOG("Autostart disabled using -a\n"); LOG("Autostart disabled using -a\n");
@ -177,6 +177,12 @@ int main(int argc, char *argv[], char *env[]) {
case 'v': case 'v':
printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n"); printf("i3 version " I3_VERSION " © 2009 Michael Stapelberg and contributors\n");
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
case 'p':
{
printf("parsing\n");
parse_file(override_configpath);
exit(0);
}
default: default:
fprintf(stderr, "Usage: %s [-c configfile] [-a] [-v]\n", argv[0]); fprintf(stderr, "Usage: %s [-c configfile] [-a] [-v]\n", argv[0]);
fprintf(stderr, "\n"); fprintf(stderr, "\n");