X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fcfgparse.l;h=2e1d240af58bf21a76e5f5e3e75f2b720c4ee422;hb=72d2b2c9bddf4df87b1ac3db7dfc5ec1e1ece10e;hp=11d67dd744322e6011fd821cc3f57ca827d91d95;hpb=fe2e4991d8a5cb6c9b5d2813f5544caf8e5552b3;p=i3%2Fi3 diff --git a/src/cfgparse.l b/src/cfgparse.l index 11d67dd7..2e1d240a 100644 --- a/src/cfgparse.l +++ b/src/cfgparse.l @@ -1,55 +1,154 @@ +/* + * vim:ts=4:sw=4:expandtab + * + */ %option nounput %option noinput +%option noyy_top_state +%option stack %{ -/* - * vim:ts=8:expandtab - * - */ #include #include -#include "cfgparse.tab.h" +#include #include #include "data.h" #include "config.h" +#include "log.h" +#include "util.h" + +#include "cfgparse.tab.h" + +int yycolumn = 1; + +#define YY_DECL int yylex (struct context *context) + +#define YY_USER_ACTION { \ + context->first_column = yycolumn; \ + context->last_column = yycolumn+yyleng-1; \ + yycolumn += 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) + %} -%Start BIND_COND -%Start BINDSYM_COND -%Start BIND_AWS_COND -%Start BINDSYM_AWS_COND -%Start BIND_A2WS_COND -%Start ASSIGN_COND -%Start COLOR_COND -%Start SCREEN_COND -%Start SCREEN_AWS_COND +EOL (\r?\n) + +%s WANT_STRING +%s WANT_QSTRING +%s BINDSYM_COND +%s ASSIGN_COND +%s ASSIGN_TARGET_COND +%s COLOR_COND +%s OUTPUT_COND +%s FOR_WINDOW_COND +%s EAT_WHITESPACE +%x BUFFER_LINE %% -[^\n]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR; } + + { + /* This is called when a new line is lexed. We only want the + * first line to match to go into state BUFFER_LINE */ + if (context->line_number == 0) { + context->line_number = 1; + BEGIN(INITIAL); + yy_push_state(BUFFER_LINE); + } + } + +^[^\r\n]*/{EOL}? { + /* save whole line */ + context->line_copy = sstrdup(yytext); + + yyless(0); + yy_pop_state(); + yy_set_bol(true); + yycolumn = 1; +} + + +"]" { yy_pop_state(); return ']'; } +[ \t]* { yy_pop_state(); } +\"[^\"]+\" { + yy_pop_state(); + /* strip quotes */ + char *copy = sstrdup(yytext+1); + copy[strlen(copy)-1] = '\0'; + yylval.string = copy; + return STR; + } +[^\n]+ { BEGIN(INITIAL); yylval.string = sstrdup(yytext); return STR; } +[a-zA-Z0-9_-]+ { yylval.string = sstrdup(yytext); return OUTPUT; } ^[ \t]*#[^\n]* { return TOKCOMMENT; } -[0-9a-fA-F]+ { yylval.string = strdup(yytext); return HEX; } +[0-9a-fA-F]+ { yylval.string = sstrdup(yytext); return HEX; } +[ \t]*→[ \t]* { BEGIN(WANT_STRING); } +[ \t]+ { BEGIN(WANT_STRING); } [0-9]+ { yylval.number = atoi(yytext); return NUMBER; } mode { return TOKMODE; } -bind { BEGIN(BIND_COND); return TOKBIND; } -bindsym { BEGIN(BINDSYM_COND); return TOKBINDSYM; } -floating_modifier { return TOKFLOATING_MODIFIER; } +bind { yy_push_state(WANT_STRING); yy_push_state(EAT_WHITESPACE); yy_push_state(EAT_WHITESPACE); return TOKBINDCODE; } +bindcode { yy_push_state(WANT_STRING); yy_push_state(EAT_WHITESPACE); yy_push_state(EAT_WHITESPACE); return TOKBINDCODE; } +bindsym { yy_push_state(BINDSYM_COND); yy_push_state(EAT_WHITESPACE); return TOKBINDSYM; } +floating_modifier { BEGIN(INITIAL); 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; } +output { yy_push_state(OUTPUT_COND); yy_push_state(EAT_WHITESPACE); return TOKOUTPUT; } +screen { + /* for compatibility until v3.φ */ + ELOG("Assignments to screens are DEPRECATED and will not work. " \ + "Please replace them with assignments to outputs.\n"); + yy_push_state(OUTPUT_COND); yy_push_state(EAT_WHITESPACE); + return TOKOUTPUT; + } +terminal { WS_STRING; return TOKTERMINAL; } +font { WS_STRING; return TOKFONT; } +assign { yy_push_state(ASSIGN_TARGET_COND); yy_push_state(ASSIGN_COND); return TOKASSIGN; } set[^\n]* { return TOKCOMMENT; } -ipc-socket { BEGIN(BIND_AWS_COND); return TOKIPCSOCKET; } -ipc_socket { BEGIN(BIND_AWS_COND); return TOKIPCSOCKET; } -new_container { return TOKNEWCONTAINER; } -default { yylval.number = MODE_DEFAULT; return TOKCONTAINERMODE; } -stacking { yylval.number = MODE_STACK; return TOKCONTAINERMODE; } -tabbed { yylval.number = MODE_TABBED; return TOKCONTAINERMODE; } +ipc-socket { WS_STRING; return TOKIPCSOCKET; } +ipc_socket { WS_STRING; return TOKIPCSOCKET; } +restart_state { WS_STRING; return TOKRESTARTSTATE; } +default_orientation { return TOK_ORIENTATION; } +horizontal { return TOK_HORIZ; } +vertical { return TOK_VERT; } +auto { return TOK_AUTO; } +workspace_layout { return TOK_WORKSPACE_LAYOUT; } +new_window { return TOKNEWWINDOW; } +normal { return TOK_NORMAL; } +none { return TOK_NONE; } +1pixel { return TOK_1PIXEL; } +focus_follows_mouse { return TOKFOCUSFOLLOWSMOUSE; } +force_focus_wrapping { return TOK_FORCE_FOCUS_WRAPPING; } +workspace_bar { return TOKWORKSPACEBAR; } +popup_during_fullscreen { return TOK_POPUP_DURING_FULLSCREEN; } +ignore { return TOK_IGNORE; } +leave_fullscreen { return TOK_LEAVE_FULLSCREEN; } +for_window { + /* Example: for_window [class="urxvt"] border none + * + * First, we wait for the ']' that finishes a match (FOR_WINDOW_COND) + * Then, we require a whitespace (EAT_WHITESPACE) + * And the rest of the line is parsed as a string + */ + yy_push_state(WANT_STRING); + yy_push_state(EAT_WHITESPACE); + yy_push_state(FOR_WINDOW_COND); + return TOK_FOR_WINDOW; + } +default { /* yylval.number = MODE_DEFAULT; */return TOK_DEFAULT; } +stacking { /* yylval.number = MODE_STACK; */return TOK_STACKING; } +stacked { return TOK_STACKING; } +tabbed { /* yylval.number = MODE_TABBED; */return TOK_TABBED; } stack-limit { return TOKSTACKLIMIT; } -cols { yylval.number = STACK_LIMIT_COLS; return TOKSTACKLIMIT; } -rows { yylval.number = STACK_LIMIT_ROWS; return TOKSTACKLIMIT; } -exec { BEGIN(BIND_AWS_COND); return TOKEXEC; } +cols { /* yylval.number = STACK_LIMIT_COLS; */return TOKSTACKLIMIT; } +rows { /* yylval.number = STACK_LIMIT_ROWS; */return TOKSTACKLIMIT; } +exec { WS_STRING; return TOKEXEC; } +exec_always { WS_STRING; return TOKEXEC_ALWAYS; } +client.background { BEGIN(COLOR_COND); yylval.single_color = &config.client.background; return TOKSINGLECOLOR; } 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; } @@ -64,28 +163,44 @@ Mod4 { yylval.number = BIND_MOD4; return MODIFIER; } Mod5 { yylval.number = BIND_MOD5; return MODIFIER; } Mode_switch { yylval.number = BIND_MODE_SWITCH; return MODIFIER; } control { return TOKCONTROL; } +ctrl { 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(BINDSYM_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; } + +class { yy_push_state(WANT_QSTRING); return TOK_CLASS; } +id { yy_push_state(WANT_QSTRING); return TOK_ID; } +con_id { yy_push_state(WANT_QSTRING); return TOK_CON_ID; } +con_mark { yy_push_state(WANT_QSTRING); return TOK_MARK; } +title { yy_push_state(WANT_QSTRING); return TOK_TITLE; } + +{EOL} { + FREE(context->line_copy); + context->line_number++; + BEGIN(INITIAL); + yy_push_state(BUFFER_LINE); + } +[ \t]+ { BEGIN(WANT_STRING); } +[ \t]+ { BEGIN(WANT_STRING); } +[ \t]+ { /* ignore whitespace */ ; } \"[^\"]+\" { /* if ASSIGN_COND then */ - BEGIN(INITIAL); + if (yy_start_stack_ptr > 0) + yy_pop_state(); + else BEGIN(INITIAL); /* yylval will be the string, but without quotes */ - char *copy = strdup(yytext+1); + char *copy = sstrdup(yytext+1); copy[strlen(copy)-1] = '\0'; yylval.string = copy; return QUOTEDSTRING; } -[^ \t]+ { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR_NG; } -[a-zA-Z0-9]+ { yylval.string = strdup(yytext); return WORD; } -[a-zA-Z]+ { yylval.string = strdup(yytext); return WORD; } +[^ \t\"]+ { BEGIN(ASSIGN_TARGET_COND); yylval.string = sstrdup(yytext); return STR_NG; } +[a-zA-Z0-9_]+ { yylval.string = sstrdup(yytext); return WORD; } +[a-zA-Z]+ { yylval.string = sstrdup(yytext); return WORD; } . { return (int)yytext[0]; } + +<> { + while (yy_start_stack_ptr > 0) + yy_pop_state(); + yyterminate(); +} + %%