]> git.sur5r.net Git - i3/i3/blob - src/cfgparse.l
Implement 'workspace back_and_forth' (Patch by Michael Walle)
[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 "data.h"
17 #include "config.h"
18 #include "log.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 %}
41
42 EOL     (\r?\n)
43
44 %s WANT_STRING
45 %s WANT_QSTRING
46 %s BINDSYM_COND
47 %s ASSIGN_COND
48 %s ASSIGN_TARGET_COND
49 %s COLOR_COND
50 %s OUTPUT_COND
51 %s FOR_WINDOW_COND
52 %s EAT_WHITESPACE
53 %x BUFFER_LINE
54
55 %%
56
57     {
58         /* This is called when a new line is lexed. We only want the
59          * first line to match to go into state BUFFER_LINE */
60         if (context->line_number == 0) {
61             context->line_number = 1;
62             BEGIN(INITIAL);
63             yy_push_state(BUFFER_LINE);
64         }
65     }
66
67 <BUFFER_LINE>^[^\r\n]*/{EOL}? {
68     /* save whole line */
69     context->line_copy = sstrdup(yytext);
70
71     yyless(0);
72     yy_pop_state();
73     yy_set_bol(true);
74     yycolumn = 1;
75 }
76
77
78 <FOR_WINDOW_COND>"]"            { yy_pop_state(); return ']'; }
79 <ASSIGN_COND>"["                {
80                                   /* this is the case for the new assign syntax
81                                    * that uses criteria */
82                                   yy_pop_state();
83                                   yy_push_state(FOR_WINDOW_COND);
84                                   /* afterwards we will be in ASSIGN_TARGET_COND */
85                                   return '[';
86                                 }
87 <EAT_WHITESPACE>[ \t]*          { yy_pop_state(); }
88 <WANT_QSTRING>\"[^\"]+\"        {
89                                   yy_pop_state();
90                                   /* strip quotes */
91                                   char *copy = sstrdup(yytext+1);
92                                   copy[strlen(copy)-1] = '\0';
93                                   yylval.string = copy;
94                                   return STR;
95                                 }
96 <WANT_STRING>[^\n]+             { BEGIN(INITIAL); yylval.string = sstrdup(yytext); return STR; }
97 <OUTPUT_COND>[a-zA-Z0-9_-]+     { yylval.string = sstrdup(yytext); return OUTPUT; }
98 ^[ \t]*#[^\n]*                  { return TOKCOMMENT; }
99 <COLOR_COND>[0-9a-fA-F]+        { yylval.string = sstrdup(yytext); return HEX; }
100 <ASSIGN_TARGET_COND>[ \t]*→[ \t]*     { BEGIN(WANT_STRING); }
101 <ASSIGN_TARGET_COND>[ \t]+      { BEGIN(WANT_STRING); }
102 [0-9]+                          { yylval.number = atoi(yytext); return NUMBER; }
103 mode                            { return TOKMODE; }
104 bind                            { yy_push_state(WANT_STRING); yy_push_state(EAT_WHITESPACE); yy_push_state(EAT_WHITESPACE); return TOKBINDCODE; }
105 bindcode                        { yy_push_state(WANT_STRING); yy_push_state(EAT_WHITESPACE); yy_push_state(EAT_WHITESPACE); return TOKBINDCODE; }
106 bindsym                         { yy_push_state(BINDSYM_COND); yy_push_state(EAT_WHITESPACE); return TOKBINDSYM; }
107 floating_modifier               { BEGIN(INITIAL); return TOKFLOATING_MODIFIER; }
108 workspace                       { BEGIN(INITIAL); return TOKWORKSPACE; }
109 output                          { yy_push_state(OUTPUT_COND); yy_push_state(EAT_WHITESPACE); return TOKOUTPUT; }
110 terminal                        { WS_STRING; return TOKTERMINAL; }
111 font                            { WS_STRING; return TOKFONT; }
112 assign                          { yy_push_state(ASSIGN_TARGET_COND); yy_push_state(ASSIGN_COND); return TOKASSIGN; }
113 set[^\n]*                       { return TOKCOMMENT; }
114 ipc-socket                      { WS_STRING; return TOKIPCSOCKET; }
115 ipc_socket                      { WS_STRING; return TOKIPCSOCKET; }
116 restart_state                   { WS_STRING; return TOKRESTARTSTATE; }
117 default_orientation             { return TOK_ORIENTATION; }
118 horizontal                      { return TOK_HORIZ; }
119 vertical                        { return TOK_VERT; }
120 auto                            { return TOK_AUTO; }
121 workspace_layout                { return TOK_WORKSPACE_LAYOUT; }
122 new_window                      { return TOKNEWWINDOW; }
123 new_float                       { return TOKNEWFLOAT; }
124 normal                          { return TOK_NORMAL; }
125 none                            { return TOK_NONE; }
126 1pixel                          { return TOK_1PIXEL; }
127 focus_follows_mouse             { return TOKFOCUSFOLLOWSMOUSE; }
128 force_focus_wrapping            { return TOK_FORCE_FOCUS_WRAPPING; }
129 force_xinerama                  { return TOK_FORCE_XINERAMA; }
130 workspace_auto_back_and_forth   { return TOK_WORKSPACE_AUTO_BAF; }
131 workspace_bar                   { return TOKWORKSPACEBAR; }
132 popup_during_fullscreen         { return TOK_POPUP_DURING_FULLSCREEN; }
133 ignore                          { return TOK_IGNORE; }
134 leave_fullscreen                { return TOK_LEAVE_FULLSCREEN; }
135 for_window                      {
136                                   /* Example: for_window [class="urxvt"] border none
137                                    *
138                                    * First, we wait for the ']' that finishes a match (FOR_WINDOW_COND)
139                                    * Then, we require a whitespace (EAT_WHITESPACE)
140                                    * And the rest of the line is parsed as a string
141                                    */
142                                   yy_push_state(WANT_STRING);
143                                   yy_push_state(EAT_WHITESPACE);
144                                   yy_push_state(FOR_WINDOW_COND);
145                                   return TOK_FOR_WINDOW;
146                                 }
147 default                         { /* yylval.number = MODE_DEFAULT; */return TOK_DEFAULT; }
148 stacking                        { /* yylval.number = MODE_STACK; */return TOK_STACKING; }
149 stacked                         { return TOK_STACKING; }
150 tabbed                          { /* yylval.number = MODE_TABBED; */return TOK_TABBED; }
151 stack-limit                     { return TOKSTACKLIMIT; }
152 cols                            { /* yylval.number = STACK_LIMIT_COLS; */return TOKSTACKLIMIT; }
153 rows                            { /* yylval.number = STACK_LIMIT_ROWS; */return TOKSTACKLIMIT; }
154 exec                            { WS_STRING; return TOKEXEC; }
155 exec_always                     { WS_STRING; return TOKEXEC_ALWAYS; }
156 client.background               { BEGIN(COLOR_COND); yylval.single_color = &config.client.background; return TOKSINGLECOLOR; }
157 client.focused                  { BEGIN(COLOR_COND); yylval.color = &config.client.focused; return TOKCOLOR; }
158 client.focused_inactive         { BEGIN(COLOR_COND); yylval.color = &config.client.focused_inactive; return TOKCOLOR; }
159 client.unfocused                { BEGIN(COLOR_COND); yylval.color = &config.client.unfocused; return TOKCOLOR; }
160 client.urgent                   { BEGIN(COLOR_COND); yylval.color = &config.client.urgent; return TOKCOLOR; }
161 bar.focused                     { BEGIN(COLOR_COND); yylval.color = &config.bar.focused; return TOKCOLOR; }
162 bar.unfocused                   { BEGIN(COLOR_COND); yylval.color = &config.bar.unfocused; return TOKCOLOR; }
163 bar.urgent                      { BEGIN(COLOR_COND); yylval.color = &config.bar.urgent; return TOKCOLOR; }
164 Mod1                            { yylval.number = BIND_MOD1; return MODIFIER; }
165 Mod2                            { yylval.number = BIND_MOD2; return MODIFIER; }
166 Mod3                            { yylval.number = BIND_MOD3; return MODIFIER; }
167 Mod4                            { yylval.number = BIND_MOD4; return MODIFIER; }
168 Mod5                            { yylval.number = BIND_MOD5; return MODIFIER; }
169 Mode_switch                     { yylval.number = BIND_MODE_SWITCH; return MODIFIER; }
170 control                         { return TOKCONTROL; }
171 ctrl                            { return TOKCONTROL; }
172 shift                           { return TOKSHIFT; }
173
174 class                           { yy_push_state(WANT_QSTRING); return TOK_CLASS; }
175 instance                        { yy_push_state(WANT_QSTRING); return TOK_INSTANCE; }
176 window_role                     { yy_push_state(WANT_QSTRING); return TOK_WINDOW_ROLE; }
177 id                              { yy_push_state(WANT_QSTRING); return TOK_ID; }
178 con_id                          { yy_push_state(WANT_QSTRING); return TOK_CON_ID; }
179 con_mark                        { yy_push_state(WANT_QSTRING); return TOK_MARK; }
180 title                           { yy_push_state(WANT_QSTRING); return TOK_TITLE; }
181
182 {EOL}                           {
183                                   FREE(context->line_copy);
184                                   context->line_number++;
185                                   BEGIN(INITIAL);
186                                   yy_push_state(BUFFER_LINE);
187                                 }
188 <BINDSYM_COND>[ \t]+            { BEGIN(WANT_STRING); }
189 <OUTPUT_COND>[ \t]+             { BEGIN(WANT_STRING); }
190 [ \t]+                          { /* ignore whitespace */ ; }
191 \"[^\"]+\"                      {
192                                   /* if ASSIGN_COND then */
193                                   if (yy_start_stack_ptr > 0)
194                                       yy_pop_state();
195                                   else BEGIN(INITIAL);
196                                   /* yylval will be the string, but without quotes */
197                                   char *copy = sstrdup(yytext+1);
198                                   copy[strlen(copy)-1] = '\0';
199                                   yylval.string = copy;
200                                   return QUOTEDSTRING;
201                                 }
202 <ASSIGN_COND>[^ \t\"\[]+        { BEGIN(ASSIGN_TARGET_COND); yylval.string = sstrdup(yytext); return STR_NG; }
203 <BINDSYM_COND>[a-zA-Z0-9_]+     { yylval.string = sstrdup(yytext); return WORD; }
204 [a-zA-Z]+                       { yylval.string = sstrdup(yytext); return WORD; }
205 .                               { return (int)yytext[0]; }
206
207 <<EOF>> {
208     while (yy_start_stack_ptr > 0)
209         yy_pop_state();
210     yyterminate();
211 }
212
213 %%