cleanup cmdparse lexer/parser (ignore whitespace, solves conflicts)

This commit is contained in:
Michael Stapelberg 2011-05-22 21:26:50 +02:00
parent 607fd7d024
commit 08f64f011d
2 changed files with 87 additions and 95 deletions

View File

@ -30,14 +30,20 @@ int cmdyycolumn = 1;
cmdyycolumn += yyleng; \ cmdyycolumn += yyleng; \
} }
/* macro to first eat whitespace, then expect a string */
#define WS_STRING do { \
yy_push_state(WANT_STRING); \
yy_push_state(EAT_WHITESPACE); \
} while (0)
%} %}
EOL (\r?\n) EOL (\r?\n)
/* handle everything up to \n as a string */ /* handle everything up to \n as a string */
%s WANT_STRING %s WANT_STRING
/* first expect a whitespace, then a string */ /* eat a whitespace, then go to the next state on the stack */
%s WANT_WS_STRING %s EAT_WHITESPACE
/* handle a quoted string or everything up to the next whitespace */ /* handle a quoted string or everything up to the next whitespace */
%s WANT_QSTRING %s WANT_QSTRING
@ -65,7 +71,6 @@ EOL (\r?\n)
cmdyycolumn = 1; cmdyycolumn = 1;
} }
<WANT_WS_STRING>[ \t]* { BEGIN(WANT_STRING); return WHITESPACE; }
<WANT_STRING>\"[^\"]+\" { <WANT_STRING>\"[^\"]+\" {
BEGIN(INITIAL); BEGIN(INITIAL);
/* strip quotes */ /* strip quotes */
@ -85,9 +90,12 @@ EOL (\r?\n)
<WANT_STRING>[^;\n]+ { BEGIN(INITIAL); cmdyylval.string = sstrdup(yytext); return STR; } <WANT_STRING>[^;\n]+ { BEGIN(INITIAL); cmdyylval.string = sstrdup(yytext); return STR; }
[ \t]* { return WHITESPACE; } <EAT_WHITESPACE>[;\n] { BEGIN(INITIAL); return ';'; }
<EAT_WHITESPACE>[ \t]* { yy_pop_state(); }
[ \t]* { /* ignore whitespace */ ; }
attach { return TOK_ATTACH; } attach { return TOK_ATTACH; }
exec { BEGIN(WANT_WS_STRING); return TOK_EXEC; } exec { WS_STRING; return TOK_EXEC; }
exit { return TOK_EXIT; } exit { return TOK_EXIT; }
reload { return TOK_RELOAD; } reload { return TOK_RELOAD; }
restart { return TOK_RESTART; } restart { return TOK_RESTART; }
@ -109,7 +117,7 @@ mode { return TOK_MODE; }
tiling { return TOK_TILING; } tiling { return TOK_TILING; }
floating { return TOK_FLOATING; } floating { return TOK_FLOATING; }
toggle { return TOK_TOGGLE; } toggle { return TOK_TOGGLE; }
workspace { BEGIN(WANT_WS_STRING); return TOK_WORKSPACE; } workspace { WS_STRING; return TOK_WORKSPACE; }
focus { return TOK_FOCUS; } focus { return TOK_FOCUS; }
move { return TOK_MOVE; } move { return TOK_MOVE; }
open { return TOK_OPEN; } open { return TOK_OPEN; }
@ -129,9 +137,9 @@ grow { return TOK_GROW; }
px { return TOK_PX; } px { return TOK_PX; }
or { return TOK_OR; } or { return TOK_OR; }
ppt { return TOK_PPT; } ppt { return TOK_PPT; }
nop { BEGIN(WANT_WS_STRING); return TOK_NOP; } nop { WS_STRING; return TOK_NOP; }
restore { BEGIN(WANT_WS_STRING); return TOK_RESTORE; } restore { WS_STRING; return TOK_RESTORE; }
mark { BEGIN(WANT_WS_STRING); return TOK_MARK; } mark { WS_STRING; return TOK_MARK; }
class { BEGIN(WANT_QSTRING); return TOK_CLASS; } class { BEGIN(WANT_QSTRING); return TOK_CLASS; }
id { BEGIN(WANT_QSTRING); return TOK_ID; } id { BEGIN(WANT_QSTRING); return TOK_ID; }

View File

@ -106,7 +106,6 @@ char *parse_cmd(const char *new) {
%} %}
%expect 5
%error-verbose %error-verbose
%lex-param { struct context *context } %lex-param { struct context *context }
@ -116,7 +115,6 @@ char *parse_cmd(const char *new) {
int number; int number;
} }
%token TOK_ATTACH "attach"
%token TOK_EXEC "exec" %token TOK_EXEC "exec"
%token TOK_EXIT "exit" %token TOK_EXIT "exit"
%token TOK_RELOAD "reload" %token TOK_RELOAD "reload"
@ -166,7 +164,6 @@ char *parse_cmd(const char *new) {
%token TOK_ID "id" %token TOK_ID "id"
%token TOK_CON_ID "con_id" %token TOK_CON_ID "con_id"
%token WHITESPACE "<whitespace>"
%token <string> STR "<string>" %token <string> STR "<string>"
%token <number> NUMBER "<number>" %token <number> NUMBER "<number>"
@ -183,7 +180,7 @@ char *parse_cmd(const char *new) {
%% %%
commands: commands:
commands optwhitespace ';' optwhitespace command commands ';' command
| command | command
{ {
owindow *current; owindow *current;
@ -198,16 +195,12 @@ commands:
} }
; ;
optwhitespace:
| WHITESPACE
;
command: command:
match optwhitespace operations match operations
; ;
match: match:
| matchstart optwhitespace criteria optwhitespace matchend | matchstart criteria matchend
{ {
printf("match parsed\n"); printf("match parsed\n");
} }
@ -322,8 +315,8 @@ criteria:
; ;
operations: operations:
operation optwhitespace operation
| operations ',' optwhitespace operation | operations ',' operation
; ;
operation: operation:
@ -336,7 +329,6 @@ operation:
| restore | restore
| move | move
| workspace | workspace
| attach
| focus | focus
| kill | kill
| open | open
@ -352,11 +344,11 @@ operation:
; ;
exec: exec:
TOK_EXEC WHITESPACE STR TOK_EXEC STR
{ {
printf("should execute %s\n", $3); printf("should execute %s\n", $2);
start_application($3); start_application($2);
free($3); free($2);
} }
; ;
@ -387,13 +379,6 @@ restart:
} }
; ;
attach:
TOK_ATTACH
{
printf("should attach\n");
}
;
focus: focus:
TOK_FOCUS TOK_FOCUS
{ {
@ -436,17 +421,16 @@ kill:
optional_kill_mode: optional_kill_mode:
/* empty */ { $$ = KILL_WINDOW; } /* empty */ { $$ = KILL_WINDOW; }
| WHITESPACE { $$ = KILL_WINDOW; } | TOK_WINDOW { $$ = KILL_WINDOW; }
| WHITESPACE TOK_WINDOW { $$ = KILL_WINDOW; } | TOK_CLIENT { $$ = KILL_CLIENT; }
| WHITESPACE TOK_CLIENT { $$ = KILL_CLIENT; }
; ;
workspace: workspace:
TOK_WORKSPACE WHITESPACE STR TOK_WORKSPACE STR
{ {
printf("should switch to workspace %s\n", $3); printf("should switch to workspace %s\n", $2);
workspace_show($3); workspace_show($2);
free($3); free($2);
} }
; ;
@ -477,29 +461,29 @@ fullscreen:
; ;
next: next:
TOK_NEXT WHITESPACE direction TOK_NEXT direction
{ {
/* TODO: use matches */ /* TODO: use matches */
printf("should select next window in direction %c\n", $3); printf("should select next window in direction %c\n", $2);
tree_next('n', ($3 == 'v' ? VERT : HORIZ)); tree_next('n', ($2 == 'v' ? VERT : HORIZ));
} }
; ;
prev: prev:
TOK_PREV WHITESPACE direction TOK_PREV direction
{ {
/* TODO: use matches */ /* TODO: use matches */
printf("should select prev window in direction %c\n", $3); printf("should select prev window in direction %c\n", $2);
tree_next('p', ($3 == 'v' ? VERT : HORIZ)); tree_next('p', ($2 == 'v' ? VERT : HORIZ));
} }
; ;
split: split:
TOK_SPLIT WHITESPACE direction TOK_SPLIT direction
{ {
/* TODO: use matches */ /* TODO: use matches */
printf("splitting in direction %c\n", $3); printf("splitting in direction %c\n", $2);
tree_split(focused, ($3 == 'v' ? VERT : HORIZ)); tree_split(focused, ($2 == 'v' ? VERT : HORIZ));
} }
; ;
@ -511,19 +495,19 @@ direction:
; ;
mode: mode:
TOK_MODE WHITESPACE window_mode TOK_MODE window_mode
{ {
HANDLE_EMPTY_MATCH; HANDLE_EMPTY_MATCH;
owindow *current; owindow *current;
TAILQ_FOREACH(current, &owindows, owindows) { TAILQ_FOREACH(current, &owindows, owindows) {
printf("matching: %p / %s\n", current->con, current->con->name); printf("matching: %p / %s\n", current->con, current->con->name);
if ($3 == TOK_TOGGLE) { if ($2 == TOK_TOGGLE) {
printf("should toggle mode\n"); printf("should toggle mode\n");
toggle_floating_mode(current->con, false); toggle_floating_mode(current->con, false);
} else { } else {
printf("should switch mode to %s\n", ($3 == TOK_FLOATING ? "floating" : "tiling")); printf("should switch mode to %s\n", ($2 == TOK_FLOATING ? "floating" : "tiling"));
if ($3 == TOK_FLOATING) { if ($2 == TOK_FLOATING) {
floating_enable(current->con, false); floating_enable(current->con, false);
} else { } else {
floating_disable(current->con, false); floating_disable(current->con, false);
@ -540,16 +524,16 @@ window_mode:
; ;
border: border:
TOK_BORDER WHITESPACE border_style TOK_BORDER border_style
{ {
printf("border style should be changed to %d\n", $3); printf("border style should be changed to %d\n", $2);
owindow *current; owindow *current;
HANDLE_EMPTY_MATCH; HANDLE_EMPTY_MATCH;
TAILQ_FOREACH(current, &owindows, owindows) { TAILQ_FOREACH(current, &owindows, owindows) {
printf("matching: %p / %s\n", current->con, current->con->name); printf("matching: %p / %s\n", current->con, current->con->name);
current->con->border_style = $3; current->con->border_style = $2;
} }
} }
; ;
@ -562,10 +546,10 @@ border_style:
level: level:
TOK_LEVEL WHITESPACE level_direction TOK_LEVEL level_direction
{ {
printf("level %c\n", $3); printf("level %c\n", $2);
if ($3 == 'u') if ($2 == 'u')
level_up(); level_up();
else level_down(); else level_down();
} }
@ -577,19 +561,19 @@ level_direction:
; ;
move: move:
TOK_MOVE WHITESPACE direction TOK_MOVE direction
{ {
printf("moving in direction %d\n", $3); printf("moving in direction %d\n", $2);
tree_move($3); tree_move($2);
} }
| TOK_MOVE WHITESPACE TOK_WORKSPACE WHITESPACE STR | TOK_MOVE TOK_WORKSPACE STR
{ {
owindow *current; owindow *current;
printf("should move window to workspace %s\n", $5); printf("should move window to workspace %s\n", $3);
/* get the workspace */ /* get the workspace */
Con *ws = workspace_get($5, NULL); Con *ws = workspace_get($3, NULL);
free($5); free($3);
HANDLE_EMPTY_MATCH; HANDLE_EMPTY_MATCH;
@ -601,27 +585,27 @@ move:
; ;
restore: restore:
TOK_RESTORE WHITESPACE STR TOK_RESTORE STR
{ {
printf("restoring \"%s\"\n", $3); printf("restoring \"%s\"\n", $2);
tree_append_json($3); tree_append_json($2);
free($3); free($2);
} }
; ;
layout: layout:
TOK_LAYOUT WHITESPACE layout_mode TOK_LAYOUT layout_mode
{ {
printf("changing layout to %d\n", $3); printf("changing layout to %d\n", $2);
owindow *current; owindow *current;
/* check if the match is empty, not if the result is empty */ /* check if the match is empty, not if the result is empty */
if (match_is_empty(&current_match)) if (match_is_empty(&current_match))
con_set_layout(focused->parent, $3); con_set_layout(focused->parent, $2);
else { else {
TAILQ_FOREACH(current, &owindows, owindows) { TAILQ_FOREACH(current, &owindows, owindows) {
printf("matching: %p / %s\n", current->con, current->con->name); printf("matching: %p / %s\n", current->con, current->con->name);
con_set_layout(current->con, $3); con_set_layout(current->con, $2);
} }
} }
} }
@ -634,41 +618,41 @@ layout_mode:
; ;
mark: mark:
TOK_MARK WHITESPACE STR TOK_MARK STR
{ {
printf("marking window with str %s\n", $3); printf("marking window with str %s\n", $2);
owindow *current; owindow *current;
HANDLE_EMPTY_MATCH; HANDLE_EMPTY_MATCH;
TAILQ_FOREACH(current, &owindows, owindows) { TAILQ_FOREACH(current, &owindows, owindows) {
printf("matching: %p / %s\n", current->con, current->con->name); printf("matching: %p / %s\n", current->con, current->con->name);
current->con->mark = sstrdup($3); current->con->mark = sstrdup($2);
} }
free($<string>3); free($<string>2);
} }
; ;
nop: nop:
TOK_NOP WHITESPACE STR TOK_NOP STR
{ {
printf("-------------------------------------------------\n"); printf("-------------------------------------------------\n");
printf(" NOP: %s\n", $3); printf(" NOP: %s\n", $2);
printf("-------------------------------------------------\n"); printf("-------------------------------------------------\n");
free($3); free($2);
} }
; ;
resize: resize:
TOK_RESIZE WHITESPACE resize_way WHITESPACE direction resize_px resize_tiling TOK_RESIZE resize_way direction resize_px resize_tiling
{ {
/* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */ /* resize <grow|shrink> <direction> [<px> px] [or <ppt> ppt] */
printf("resizing in way %d, direction %d, px %d or ppt %d\n", $3, $5, $6, $7); printf("resizing in way %d, direction %d, px %d or ppt %d\n", $2, $3, $4, $5);
int direction = $5; int direction = $3;
int px = $6; int px = $4;
int ppt = $7; int ppt = $5;
if ($3 == TOK_SHRINK) { if ($2 == TOK_SHRINK) {
px *= -1; px *= -1;
ppt *= -1; ppt *= -1;
} }
@ -723,9 +707,9 @@ resize_px:
{ {
$$ = 10; $$ = 10;
} }
| WHITESPACE NUMBER WHITESPACE TOK_PX | NUMBER TOK_PX
{ {
$$ = $2; $$ = $1;
} }
; ;
@ -734,9 +718,9 @@ resize_tiling:
{ {
$$ = 10; $$ = 10;
} }
| WHITESPACE TOK_OR WHITESPACE NUMBER WHITESPACE TOK_PPT | TOK_OR NUMBER TOK_PPT
{ {
$$ = $4; $$ = $2;
} }
; ;