]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.l
cleanup cmdparse lexer/parser (ignore whitespace, solves conflicts)
[i3/i3] / src / cfgparse.l
1 %option nounput
2 %option noinput
3 %option noyy_top_state
4 %option stack
5
6 %{
7 /*
8  * vim:ts=8:expandtab
9  *
10  */
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <xcb/xcb.h>
15
16 #include "data.h"
17 #include "config.h"
18 #include "log.h"
19 #include "util.h"
20
21 #include "cfgparse.tab.h"
22
23 int yycolumn = 1;
24
25 #define YY_DECL int yylex (struct context *context)
26
27 #define YY_USER_ACTION { \
28         context->first_column = yycolumn; \
29         context->last_column = yycolumn+yyleng-1; \
30         yycolumn += yyleng; \
31 }
32
33 %}
34
35 EOL     (\r?\n)
36
37 %s BINDCODE_COND
38 %s BINDSYM_COND
39 %s BIND_AWS_COND
40 %s BINDSYM_AWS_COND
41 %s BIND_A2WS_COND
42 %s ASSIGN_COND
43 %s COLOR_COND
44 %s OUTPUT_COND
45 %s OUTPUT_AWS_COND
46 %s WANT_QSTRING
47 %s FOR_WINDOW_COND
48 %s REQUIRE_WS
49 %x BUFFER_LINE
50
51 %%
52
53         {
54                 /* This is called when a new line is lexed. We only want the
55                  * first line to match to go into state BUFFER_LINE */
56                 if (context->line_number == 0) {
57                         context->line_number = 1;
58                         BEGIN(INITIAL);
59                         yy_push_state(BUFFER_LINE);
60                 }
61         }
62
63 <BUFFER_LINE>^[^\r\n]*/{EOL}? {
64         /* save whole line */
65         context->line_copy = strdup(yytext);
66
67         yyless(0);
68         yy_pop_state();
69         yy_set_bol(true);
70         yycolumn = 1;
71 }
72
73
74 <FOR_WINDOW_COND>"]"             { yy_pop_state(); return ']'; }
75 <REQUIRE_WS>[ \t]*               { yy_pop_state(); return WHITESPACE; }
76 <WANT_QSTRING>\"[^\"]+\"         {
77                                   yy_pop_state();
78                                   /* strip quotes */
79                                   char *copy = sstrdup(yytext+1);
80                                   copy[strlen(copy)-1] = '\0';
81                                   yylval.string = copy;
82                                   return STR;
83                                  }
84 <BIND_A2WS_COND>[^\n]+          { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR; }
85 <OUTPUT_AWS_COND>[a-zA-Z0-9_-]+ { yylval.string = strdup(yytext); return OUTPUT; }
86 ^[ \t]*#[^\n]*                  { return TOKCOMMENT; }
87 <COLOR_COND>[0-9a-fA-F]+        { yylval.string = strdup(yytext); return HEX; }
88 [0-9]+                          { yylval.number = atoi(yytext); return NUMBER; }
89 mode                            { return TOKMODE; }
90 bind                            { BEGIN(BINDCODE_COND); return TOKBINDCODE; }
91 bindcode                        { BEGIN(BINDCODE_COND); return TOKBINDCODE; }
92 bindsym                         { BEGIN(BINDSYM_COND); return TOKBINDSYM; }
93 floating_modifier               { BEGIN(INITIAL); return TOKFLOATING_MODIFIER; }
94 workspace                       { BEGIN(INITIAL); return TOKWORKSPACE; }
95 output                          { BEGIN(OUTPUT_COND); return TOKOUTPUT; }
96 screen                          {
97                                   /* for compatibility until v3.φ */
98                                   ELOG("Assignments to screens are DEPRECATED and will not work. " \
99                                        "Please replace them with assignments to outputs.\n");
100                                   BEGIN(OUTPUT_COND);
101                                   return TOKOUTPUT;
102                                 }
103 terminal                        { BEGIN(BIND_AWS_COND); return TOKTERMINAL; }
104 font                            { BEGIN(BIND_AWS_COND); return TOKFONT; }
105 assign                          { BEGIN(ASSIGN_COND); return TOKASSIGN; }
106 set[^\n]*                       { return TOKCOMMENT; }
107 ipc-socket                      { BEGIN(BIND_AWS_COND); return TOKIPCSOCKET; }
108 ipc_socket                      { BEGIN(BIND_AWS_COND); return TOKIPCSOCKET; }
109 restart_state                   { BEGIN(BIND_AWS_COND); return TOKRESTARTSTATE; }
110 default_orientation             { return TOK_ORIENTATION; }
111 horizontal                      { return TOK_HORIZ; }
112 vertical                        { return TOK_VERT; }
113 auto                            { return TOK_AUTO; }
114 workspace_layout                { return TOK_WORKSPACE_LAYOUT; }
115 new_window                      { return TOKNEWWINDOW; }
116 normal                          { return TOK_NORMAL; }
117 none                            { return TOK_NONE; }
118 1pixel                          { return TOK_1PIXEL; }
119 focus_follows_mouse             { return TOKFOCUSFOLLOWSMOUSE; }
120 workspace_bar                   { return TOKWORKSPACEBAR; }
121 popup_during_fullscreen         { return TOK_POPUP_DURING_FULLSCREEN; }
122 ignore                          { return TOK_IGNORE; }
123 leave_fullscreen                { return TOK_LEAVE_FULLSCREEN; }
124 for_window                      {
125                                   /* Example: for_window [class="urxvt"] border none
126                                    *
127                                    * First, we wait for the ']' that finishes a match (FOR_WINDOW_COND)
128                                    * Then, we require a whitespace (REQUIRE_WS)
129                                    * And the rest of the line is parsed as a string
130                                    */
131                                   yy_push_state(BIND_A2WS_COND);
132                                   yy_push_state(REQUIRE_WS);
133                                   yy_push_state(FOR_WINDOW_COND);
134                                   return TOK_FOR_WINDOW;
135                                 }
136 default                         { /* yylval.number = MODE_DEFAULT; */return TOK_DEFAULT; }
137 stacking                        { /* yylval.number = MODE_STACK; */return TOK_STACKING; }
138 stacked                         { return TOK_STACKING; }
139 tabbed                          { /* yylval.number = MODE_TABBED; */return TOK_TABBED; }
140 stack-limit                     { return TOKSTACKLIMIT; }
141 cols                            { /* yylval.number = STACK_LIMIT_COLS; */return TOKSTACKLIMIT; }
142 rows                            { /* yylval.number = STACK_LIMIT_ROWS; */return TOKSTACKLIMIT; }
143 exec                            { BEGIN(BIND_AWS_COND); return TOKEXEC; }
144 client.background               { BEGIN(COLOR_COND); yylval.single_color = &config.client.background; return TOKSINGLECOLOR; }
145 client.focused                  { BEGIN(COLOR_COND); yylval.color = &config.client.focused; return TOKCOLOR; }
146 client.focused_inactive         { BEGIN(COLOR_COND); yylval.color = &config.client.focused_inactive; return TOKCOLOR; }
147 client.unfocused                { BEGIN(COLOR_COND); yylval.color = &config.client.unfocused; return TOKCOLOR; }
148 client.urgent                   { BEGIN(COLOR_COND); yylval.color = &config.client.urgent; return TOKCOLOR; }
149 bar.focused                     { BEGIN(COLOR_COND); yylval.color = &config.bar.focused; return TOKCOLOR; }
150 bar.unfocused                   { BEGIN(COLOR_COND); yylval.color = &config.bar.unfocused; return TOKCOLOR; }
151 bar.urgent                      { BEGIN(COLOR_COND); yylval.color = &config.bar.urgent; return TOKCOLOR; }
152 Mod1                            { yylval.number = BIND_MOD1; return MODIFIER; }
153 Mod2                            { yylval.number = BIND_MOD2; return MODIFIER; }
154 Mod3                            { yylval.number = BIND_MOD3; return MODIFIER; }
155 Mod4                            { yylval.number = BIND_MOD4; return MODIFIER; }
156 Mod5                            { yylval.number = BIND_MOD5; return MODIFIER; }
157 Mode_switch                     { yylval.number = BIND_MODE_SWITCH; return MODIFIER; }
158 control                         { return TOKCONTROL; }
159 ctrl                            { return TOKCONTROL; }
160 shift                           { return TOKSHIFT; }
161 →                               { return TOKARROW; }
162
163 class                           { yy_push_state(WANT_QSTRING); return TOK_CLASS; }
164 id                              { yy_push_state(WANT_QSTRING); return TOK_ID; }
165 con_id                          { yy_push_state(WANT_QSTRING); return TOK_CON_ID; }
166 con_mark                        { yy_push_state(WANT_QSTRING); return TOK_MARK; }
167 title                           { yy_push_state(WANT_QSTRING); return TOK_TITLE; }
168
169 {EOL}                           {
170                                   FREE(context->line_copy);
171                                   context->line_number++;
172                                   BEGIN(INITIAL);
173                                   yy_push_state(BUFFER_LINE);
174                                 }
175 <BINDCODE_COND>[ \t]+           { BEGIN(BIND_AWS_COND); return WHITESPACE; }
176 <BINDSYM_COND>[ \t]+            { BEGIN(BINDSYM_AWS_COND); return WHITESPACE; }
177 <BIND_AWS_COND>[ \t]+           { BEGIN(BIND_A2WS_COND); return WHITESPACE; }
178 <BINDSYM_AWS_COND>[ \t]+        { BEGIN(BIND_A2WS_COND); return WHITESPACE; }
179 <OUTPUT_COND>[ \t]+             { BEGIN(OUTPUT_AWS_COND); return WHITESPACE; }
180 <OUTPUT_AWS_COND>[ \t]+         { BEGIN(BIND_A2WS_COND); return WHITESPACE; }
181 [ \t]+                          { return WHITESPACE; }
182 \"[^\"]+\"                      {
183                                   /* if ASSIGN_COND then */
184                                   BEGIN(INITIAL);
185                                   /* yylval will be the string, but without quotes */
186                                   char *copy = strdup(yytext+1);
187                                   copy[strlen(copy)-1] = '\0';
188                                   yylval.string = copy;
189                                   return QUOTEDSTRING;
190                                 }
191 <ASSIGN_COND>[^ \t]+            { BEGIN(INITIAL); yylval.string = strdup(yytext); return STR_NG; }
192 <BINDSYM_AWS_COND>[a-zA-Z0-9_]+ { yylval.string = strdup(yytext); return WORD; }
193 [a-zA-Z]+                       { yylval.string = strdup(yytext); return WORD; }
194 .                               { return (int)yytext[0]; }
195
196 <<EOF>> {
197         while (yy_start_stack_ptr > 0)
198                 yy_pop_state();
199         yyterminate();
200 }
201
202 %%