]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.l
Merge branch 'master' into next
[i3/i3] / src / cfgparse.l
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  */
5 %option nounput
6 %option noinput
7 %option noyy_top_state
8 %option stack
9
10 %{
11 #include <stdio.h>
12 #include <string.h>
13 #include <stdint.h>
14 #include <xcb/xcb.h>
15
16 #include "log.h"
17 #include "data.h"
18 #include "config.h"
19 #include "util.h"
20 #include "libi3.h"
21
22 #include "cfgparse.tab.h"
23
24 int yycolumn = 1;
25
26 #define YY_DECL int yylex (struct context *context)
27
28 #define YY_USER_ACTION { \
29     context->first_column = yycolumn; \
30     context->last_column = yycolumn+yyleng-1; \
31     yycolumn += yyleng; \
32 }
33
34 /* macro to first eat whitespace, then expect a string */
35 #define WS_STRING do { \
36     yy_push_state(WANT_STRING); \
37     yy_push_state(EAT_WHITESPACE); \
38 } while (0)
39
40 #define BAR_TRIPLE_COLOR do { \
41     yy_push_state(BAR_COLOR); \
42     yy_push_state(BAR_COLOR); \
43     yy_push_state(BAR_COLOR); \
44 } while (0)
45
46 %}
47
48 EOL     (\r?\n)
49
50 %s WANT_STRING
51 %s WANT_QSTRING
52 %s BINDSYM_COND
53 %s ASSIGN_COND
54 %s ASSIGN_TARGET_COND
55 %s COLOR_COND
56 %s OUTPUT_COND
57 %s FOR_WINDOW_COND
58 %s EAT_WHITESPACE
59 %s BORDER_WIDTH
60
61 %x BUFFER_LINE
62 %x BAR
63 %x BAR_MODE
64 %x BAR_MODIFIER
65 %x BAR_POSITION
66 %x BAR_COLORS
67 %x BAR_COLOR
68
69 %x EXEC
70 %x OPTRELEASE
71
72 %%
73
74     {
75         /* This is called when a new line is lexed. We only want the
76          * first line to match to go into state BUFFER_LINE */
77         if (context->line_number == 0) {
78             context->line_number = 1;
79             BEGIN(INITIAL);
80             yy_push_state(BUFFER_LINE);
81         }
82     }
83
84 <BUFFER_LINE>^[^\r\n]*/{EOL}? {
85     /* save whole line */
86     context->line_copy = sstrdup(yytext);
87
88     yyless(0);
89     yy_pop_state();
90     yy_set_bol(true);
91     yycolumn = 1;
92 }
93
94  /* This part of the lexer handles the bar {} blocks */
95 <BAR,BAR_MODE,BAR_MODIFIER,BAR_POSITION,BAR_COLORS,BAR_COLOR>[ \t]+ { /* ignore whitespace */ ; }
96 <BAR>"{"                        { return '{'; }
97 <BAR>"}"                        { yy_pop_state(); return '}'; }
98 <BAR>^[ \t]*#[^\n]*             { return TOKCOMMENT; }
99 <BAR>output                     { WS_STRING; return TOK_BAR_OUTPUT; }
100 <BAR>tray_output                { WS_STRING; return TOK_BAR_TRAY_OUTPUT; }
101 <BAR>socket_path                { WS_STRING; return TOK_BAR_SOCKET_PATH; }
102 <BAR>mode                       { yy_push_state(BAR_MODE); return TOK_BAR_MODE; }
103 <BAR_MODE>hide                  { yy_pop_state(); return TOK_BAR_HIDE; }
104 <BAR_MODE>dock                  { yy_pop_state(); return TOK_BAR_DOCK; }
105 <BAR>modifier                   { yy_push_state(BAR_MODIFIER); return TOK_BAR_MODIFIER; }
106 <BAR_MODIFIER>control           { yy_pop_state(); return TOK_BAR_CONTROL; }
107 <BAR_MODIFIER>ctrl              { yy_pop_state(); return TOK_BAR_CONTROL; }
108 <BAR_MODIFIER>shift             { yy_pop_state(); return TOK_BAR_SHIFT; }
109 <BAR_MODIFIER>Mod1              { yy_pop_state(); return TOK_BAR_MOD1; }
110 <BAR_MODIFIER>Mod2              { yy_pop_state(); return TOK_BAR_MOD2; }
111 <BAR_MODIFIER>Mod3              { yy_pop_state(); return TOK_BAR_MOD3; }
112 <BAR_MODIFIER>Mod4              { yy_pop_state(); return TOK_BAR_MOD4; }
113 <BAR_MODIFIER>Mod5              { yy_pop_state(); return TOK_BAR_MOD5; }
114 <BAR>position                   { yy_push_state(BAR_POSITION); return TOK_BAR_POSITION; }
115 <BAR_POSITION>bottom            { yy_pop_state(); return TOK_BAR_BOTTOM; }
116 <BAR_POSITION>top               { yy_pop_state(); return TOK_BAR_TOP; }
117 <BAR>status_command             { WS_STRING; return TOK_BAR_STATUS_COMMAND; }
118 <BAR>i3bar_command              { WS_STRING; return TOK_BAR_I3BAR_COMMAND; }
119 <BAR>font                       { WS_STRING; return TOK_BAR_FONT; }
120 <BAR>workspace_buttons          { return TOK_BAR_WORKSPACE_BUTTONS; }
121 <BAR>verbose                    { return TOK_BAR_VERBOSE; }
122 <BAR>colors                     { yy_push_state(BAR_COLORS); return TOK_BAR_COLORS; }
123 <BAR_COLORS>"{"                 { return '{'; }
124 <BAR_COLORS>"}"                 { yy_pop_state(); return '}'; }
125 <BAR_COLORS>^[ \t]*#[^\n]*      { return TOKCOMMENT; }
126 <BAR_COLORS>background          { yy_push_state(BAR_COLOR); return TOK_BAR_COLOR_BACKGROUND; }
127 <BAR_COLORS>statusline          { yy_push_state(BAR_COLOR); return TOK_BAR_COLOR_STATUSLINE; }
128 <BAR_COLORS>focused_workspace   { BAR_TRIPLE_COLOR; return TOK_BAR_COLOR_FOCUSED_WORKSPACE; }
129 <BAR_COLORS>active_workspace    { BAR_TRIPLE_COLOR; return TOK_BAR_COLOR_ACTIVE_WORKSPACE; }
130 <BAR_COLORS>inactive_workspace  { BAR_TRIPLE_COLOR; return TOK_BAR_COLOR_INACTIVE_WORKSPACE; }
131 <BAR_COLORS>urgent_workspace    { BAR_TRIPLE_COLOR; return TOK_BAR_COLOR_URGENT_WORKSPACE; }
132 <BAR_COLOR>#[0-9a-fA-F]+        { yy_pop_state(); yylval.string = sstrdup(yytext); return HEXCOLOR; }
133 <BAR_COLOR>{EOL}                {
134                                   yy_pop_state();
135                                   FREE(context->line_copy);
136                                   context->line_number++;
137                                   yy_push_state(BUFFER_LINE);
138                                 }
139 <BAR,BAR_COLORS,BAR_MODE,BAR_MODIFIER,BAR_POSITION>[a-zA-Z]+ { yylval.string = sstrdup(yytext); return WORD; }
140
141
142
143 <FOR_WINDOW_COND>"]"            { yy_pop_state(); return ']'; }
144 <ASSIGN_COND>"["                {
145                                   /* this is the case for the new assign syntax
146                                    * that uses criteria */
147                                   yy_pop_state();
148                                   yy_push_state(FOR_WINDOW_COND);
149                                   /* afterwards we will be in ASSIGN_TARGET_COND */
150                                   return '[';
151                                 }
152 <EAT_WHITESPACE>[ \t]*          { yy_pop_state(); }
153 <EAT_WHITESPACE>{EOL}           { yy_pop_state(); }
154 <BINDSYM_COND>{EOL}             { yy_pop_state(); }
155 <WANT_QSTRING>\"[^\"]+\"        {
156                                   yy_pop_state();
157                                   /* strip quotes */
158                                   char *copy = sstrdup(yytext+1);
159                                   copy[strlen(copy)-1] = '\0';
160                                   yylval.string = copy;
161                                   return STR;
162                                 }
163 <WANT_STRING>[^\n]+             { yy_pop_state(); yylval.string = sstrdup(yytext); return STR; }
164 <OUTPUT_COND>[a-zA-Z0-9\/_-]+   { yy_pop_state(); yylval.string = sstrdup(yytext); return OUTPUT; }
165 ^[ \t]*#[^\n]*                  { return TOKCOMMENT; }
166 <COLOR_COND>#[0-9a-fA-F]+       { yy_pop_state(); yylval.string = sstrdup(yytext); return HEXCOLOR; }
167 <COLOR_COND>{EOL}               {
168                                   yy_pop_state();
169                                   FREE(context->line_copy);
170                                   context->line_number++;
171                                   yy_push_state(BUFFER_LINE);
172                                 }
173 <ASSIGN_TARGET_COND>[ \t]*→[ \t]*     { BEGIN(WANT_STRING); }
174 <ASSIGN_TARGET_COND>[ \t]+      { BEGIN(WANT_STRING); }
175 <BORDER_WIDTH>[^\n][0-9]+       { printf("Border width set to: %s\n", yytext); yylval.number = atoi(yytext); return NUMBER;}
176 <EXEC>--no-startup-id           { printf("no startup id\n"); yy_pop_state(); return TOK_NO_STARTUP_ID; }
177 <EXEC>.                         { printf("anything else: *%s*\n", yytext); yyless(0); yy_pop_state(); yy_pop_state(); }
178 <OPTRELEASE>--release           { printf("--release\n"); yy_pop_state(); return TOK_RELEASE; }
179 <OPTRELEASE>.                   { printf("anything else (optrelease): *%s*\n", yytext); yyless(0); yy_pop_state(); yy_pop_state(); }
180 [0-9-]+                         { yylval.number = atoi(yytext); return NUMBER; }
181 bar                             { yy_push_state(BAR); return TOK_BAR; }
182 mode                            { return TOKMODE; }
183 bind                            { yy_push_state(WANT_STRING); yy_push_state(EAT_WHITESPACE); yy_push_state(EAT_WHITESPACE); return TOKBINDCODE; }
184 bindcode                        { yy_push_state(WANT_STRING); yy_push_state(EAT_WHITESPACE); yy_push_state(EAT_WHITESPACE); yy_push_state(OPTRELEASE); yy_push_state(EAT_WHITESPACE); return TOKBINDCODE; }
185 bindsym                         { yy_push_state(BINDSYM_COND); yy_push_state(EAT_WHITESPACE); yy_push_state(OPTRELEASE); yy_push_state(EAT_WHITESPACE); return TOKBINDSYM; }
186 floating_maximum_size           { return TOKFLOATING_MAXIMUM_SIZE; }
187 floating_minimum_size           { return TOKFLOATING_MINIMUM_SIZE; }
188 floating_modifier               { return TOKFLOATING_MODIFIER; }
189 workspace                       { return TOKWORKSPACE; }
190 output                          { yy_push_state(OUTPUT_COND); yy_push_state(EAT_WHITESPACE); return TOKOUTPUT; }
191 terminal                        { WS_STRING; return TOKTERMINAL; }
192 font                            { WS_STRING; return TOKFONT; }
193 assign                          { yy_push_state(ASSIGN_TARGET_COND); yy_push_state(ASSIGN_COND); return TOKASSIGN; }
194 set[^\n]*                       { return TOKCOMMENT; }
195 ipc-socket                      { WS_STRING; return TOKIPCSOCKET; }
196 ipc_socket                      { WS_STRING; return TOKIPCSOCKET; }
197 restart_state                   { WS_STRING; return TOKRESTARTSTATE; }
198 default_orientation             { return TOK_ORIENTATION; }
199 horizontal                      { return TOK_HORIZ; }
200 vertical                        { return TOK_VERT; }
201 auto                            { return TOK_AUTO; }
202 workspace_layout                { return TOK_WORKSPACE_LAYOUT; }
203 new_window                      { return TOKNEWWINDOW; }
204 new_float                       { return TOKNEWFLOAT; }
205 normal                          { yy_push_state(BORDER_WIDTH); return TOK_NORMAL; }
206 none                            { return TOK_NONE; }
207 1pixel                          { return TOK_1PIXEL; }
208 pixel                           { yy_push_state(BORDER_WIDTH); return TOK_PIXEL; }
209 hide_edge_borders               { return TOK_HIDE_EDGE_BORDERS; }
210 both                            { return TOK_BOTH; }
211 focus_follows_mouse             { return TOKFOCUSFOLLOWSMOUSE; }
212 force_focus_wrapping            { return TOK_FORCE_FOCUS_WRAPPING; }
213 force_xinerama                  { return TOK_FORCE_XINERAMA; }
214 force-xinerama                  { return TOK_FORCE_XINERAMA; }
215 fake_outputs                    { WS_STRING; return TOK_FAKE_OUTPUTS; }
216 fake-outputs                    { WS_STRING; return TOK_FAKE_OUTPUTS; }
217 workspace_auto_back_and_forth   { return TOK_WORKSPACE_AUTO_BAF; }
218 force_display_urgency_hint      { return TOK_WORKSPACE_URGENCY_TIMER; }
219 ms                              { return TOK_TIME_MS; }
220 workspace_bar                   { return TOKWORKSPACEBAR; }
221 popup_during_fullscreen         { return TOK_POPUP_DURING_FULLSCREEN; }
222 ignore                          { return TOK_IGNORE; }
223 leave_fullscreen                { return TOK_LEAVE_FULLSCREEN; }
224 for_window                      {
225                                   /* Example: for_window [class="urxvt"] border none
226                                    *
227                                    * First, we wait for the ']' that finishes a match (FOR_WINDOW_COND)
228                                    * Then, we require a whitespace (EAT_WHITESPACE)
229                                    * And the rest of the line is parsed as a string
230                                    */
231                                   yy_push_state(WANT_STRING);
232                                   yy_push_state(EAT_WHITESPACE);
233                                   yy_push_state(FOR_WINDOW_COND);
234                                   return TOK_FOR_WINDOW;
235                                 }
236 default                         { /* yylval.number = MODE_DEFAULT; */return TOK_DEFAULT; }
237 stacking                        { /* yylval.number = MODE_STACK; */return TOK_STACKING; }
238 stacked                         { return TOK_STACKING; }
239 tabbed                          { /* yylval.number = MODE_TABBED; */return TOK_TABBED; }
240 stack-limit                     { return TOKSTACKLIMIT; }
241 cols                            { /* yylval.number = STACK_LIMIT_COLS; */return TOKSTACKLIMIT; }
242 rows                            { /* yylval.number = STACK_LIMIT_ROWS; */return TOKSTACKLIMIT; }
243 exec                            { WS_STRING; yy_push_state(EXEC); yy_push_state(EAT_WHITESPACE); return TOKEXEC; }
244 exec_always                     { WS_STRING; yy_push_state(EXEC); yy_push_state(EAT_WHITESPACE); return TOKEXEC_ALWAYS; }
245 client.background               { yy_push_state(COLOR_COND); yylval.single_color = &config.client.background; return TOKSINGLECOLOR; }
246 client.focused                  { yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yylval.color = &config.client.focused; return TOKCOLOR; }
247 client.focused_inactive         { yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yylval.color = &config.client.focused_inactive; return TOKCOLOR; }
248 client.unfocused                { yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yylval.color = &config.client.unfocused; return TOKCOLOR; }
249 client.urgent                   { yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yy_push_state(COLOR_COND); yylval.color = &config.client.urgent; return TOKCOLOR; }
250 bar.focused                     { yy_push_state(COLOR_COND); yylval.color = &config.bar.focused; return TOKCOLOR; }
251 bar.unfocused                   { yy_push_state(COLOR_COND); yylval.color = &config.bar.unfocused; return TOKCOLOR; }
252 bar.urgent                      { yy_push_state(COLOR_COND); yylval.color = &config.bar.urgent; return TOKCOLOR; }
253 Mod1                            { yylval.number = BIND_MOD1; return MODIFIER; }
254 Mod2                            { yylval.number = BIND_MOD2; return MODIFIER; }
255 Mod3                            { yylval.number = BIND_MOD3; return MODIFIER; }
256 Mod4                            { yylval.number = BIND_MOD4; return MODIFIER; }
257 Mod5                            { yylval.number = BIND_MOD5; return MODIFIER; }
258 Mode_switch                     { yylval.number = BIND_MODE_SWITCH; return MODIFIER; }
259 control                         { return TOKCONTROL; }
260 ctrl                            { return TOKCONTROL; }
261 shift                           { return TOKSHIFT; }
262
263 class                           { yy_push_state(WANT_QSTRING); return TOK_CLASS; }
264 instance                        { yy_push_state(WANT_QSTRING); return TOK_INSTANCE; }
265 window_role                     { yy_push_state(WANT_QSTRING); return TOK_WINDOW_ROLE; }
266 id                              { yy_push_state(WANT_QSTRING); return TOK_ID; }
267 con_id                          { yy_push_state(WANT_QSTRING); return TOK_CON_ID; }
268 con_mark                        { yy_push_state(WANT_QSTRING); return TOK_MARK; }
269 title                           { yy_push_state(WANT_QSTRING); return TOK_TITLE; }
270 urgent                          { yy_push_state(WANT_QSTRING); return TOK_URGENT; }
271
272 <*>{EOL}                        {
273                                   FREE(context->line_copy);
274                                   context->line_number++;
275                                   yy_push_state(BUFFER_LINE);
276                                 }
277 <BINDSYM_COND>[ \t]+            { yy_pop_state(); yy_push_state(WANT_STRING); }
278 <OUTPUT_COND>[ \t]+             { yy_pop_state(); yy_push_state(WANT_STRING); }
279 [ \t]+                          { /* ignore whitespace */ ; }
280 \"[^\"]+\"                      {
281                                   /* if ASSIGN_COND then */
282                                   if (yy_start_stack_ptr > 0)
283                                       yy_pop_state();
284                                   /* yylval will be the string, but without quotes */
285                                   char *copy = sstrdup(yytext+1);
286                                   copy[strlen(copy)-1] = '\0';
287                                   yylval.string = copy;
288                                   return QUOTEDSTRING;
289                                 }
290 <ASSIGN_COND>[^ \t\"\[]+        { BEGIN(ASSIGN_TARGET_COND); yylval.string = sstrdup(yytext); return STR_NG; }
291 <BINDSYM_COND>[a-zA-Z0-9_]+     { yylval.string = sstrdup(yytext); return WORD; }
292 [a-zA-Z]+                       { yylval.string = sstrdup(yytext); return WORD; }
293 .                               { return (int)yytext[0]; }
294
295 <<EOF>> {
296     while (yy_start_stack_ptr > 0)
297         yy_pop_state();
298     yyterminate();
299 }
300
301 %%