]> 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 "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_bar                   { return TOKWORKSPACEBAR; }
131 popup_during_fullscreen         { return TOK_POPUP_DURING_FULLSCREEN; }
132 ignore                          { return TOK_IGNORE; }
133 leave_fullscreen                { return TOK_LEAVE_FULLSCREEN; }
134 for_window                      {
135                                   /* Example: for_window [class="urxvt"] border none
136                                    *
137                                    * First, we wait for the ']' that finishes a match (FOR_WINDOW_COND)
138                                    * Then, we require a whitespace (EAT_WHITESPACE)
139                                    * And the rest of the line is parsed as a string
140                                    */
141                                   yy_push_state(WANT_STRING);
142                                   yy_push_state(EAT_WHITESPACE);
143                                   yy_push_state(FOR_WINDOW_COND);
144                                   return TOK_FOR_WINDOW;
145                                 }
146 default                         { /* yylval.number = MODE_DEFAULT; */return TOK_DEFAULT; }
147 stacking                        { /* yylval.number = MODE_STACK; */return TOK_STACKING; }
148 stacked                         { return TOK_STACKING; }
149 tabbed                          { /* yylval.number = MODE_TABBED; */return TOK_TABBED; }
150 stack-limit                     { return TOKSTACKLIMIT; }
151 cols                            { /* yylval.number = STACK_LIMIT_COLS; */return TOKSTACKLIMIT; }
152 rows                            { /* yylval.number = STACK_LIMIT_ROWS; */return TOKSTACKLIMIT; }
153 exec                            { WS_STRING; return TOKEXEC; }
154 exec_always                     { WS_STRING; return TOKEXEC_ALWAYS; }
155 client.background               { BEGIN(COLOR_COND); yylval.single_color = &config.client.background; return TOKSINGLECOLOR; }
156 client.focused                  { BEGIN(COLOR_COND); yylval.color = &config.client.focused; return TOKCOLOR; }
157 client.focused_inactive         { BEGIN(COLOR_COND); yylval.color = &config.client.focused_inactive; return TOKCOLOR; }
158 client.unfocused                { BEGIN(COLOR_COND); yylval.color = &config.client.unfocused; return TOKCOLOR; }
159 client.urgent                   { BEGIN(COLOR_COND); yylval.color = &config.client.urgent; return TOKCOLOR; }
160 bar.focused                     { BEGIN(COLOR_COND); yylval.color = &config.bar.focused; return TOKCOLOR; }
161 bar.unfocused                   { BEGIN(COLOR_COND); yylval.color = &config.bar.unfocused; return TOKCOLOR; }
162 bar.urgent                      { BEGIN(COLOR_COND); yylval.color = &config.bar.urgent; return TOKCOLOR; }
163 Mod1                            { yylval.number = BIND_MOD1; return MODIFIER; }
164 Mod2                            { yylval.number = BIND_MOD2; return MODIFIER; }
165 Mod3                            { yylval.number = BIND_MOD3; return MODIFIER; }
166 Mod4                            { yylval.number = BIND_MOD4; return MODIFIER; }
167 Mod5                            { yylval.number = BIND_MOD5; return MODIFIER; }
168 Mode_switch                     { yylval.number = BIND_MODE_SWITCH; return MODIFIER; }
169 control                         { return TOKCONTROL; }
170 ctrl                            { return TOKCONTROL; }
171 shift                           { return TOKSHIFT; }
172
173 class                           { yy_push_state(WANT_QSTRING); return TOK_CLASS; }
174 instance                        { yy_push_state(WANT_QSTRING); return TOK_INSTANCE; }
175 window_role                     { yy_push_state(WANT_QSTRING); return TOK_WINDOW_ROLE; }
176 id                              { yy_push_state(WANT_QSTRING); return TOK_ID; }
177 con_id                          { yy_push_state(WANT_QSTRING); return TOK_CON_ID; }
178 con_mark                        { yy_push_state(WANT_QSTRING); return TOK_MARK; }
179 title                           { yy_push_state(WANT_QSTRING); return TOK_TITLE; }
180
181 {EOL}                           {
182                                   FREE(context->line_copy);
183                                   context->line_number++;
184                                   BEGIN(INITIAL);
185                                   yy_push_state(BUFFER_LINE);
186                                 }
187 <BINDSYM_COND>[ \t]+            { BEGIN(WANT_STRING); }
188 <OUTPUT_COND>[ \t]+             { BEGIN(WANT_STRING); }
189 [ \t]+                          { /* ignore whitespace */ ; }
190 \"[^\"]+\"                      {
191                                   /* if ASSIGN_COND then */
192                                   if (yy_start_stack_ptr > 0)
193                                       yy_pop_state();
194                                   else BEGIN(INITIAL);
195                                   /* yylval will be the string, but without quotes */
196                                   char *copy = sstrdup(yytext+1);
197                                   copy[strlen(copy)-1] = '\0';
198                                   yylval.string = copy;
199                                   return QUOTEDSTRING;
200                                 }
201 <ASSIGN_COND>[^ \t\"\[]+        { BEGIN(ASSIGN_TARGET_COND); yylval.string = sstrdup(yytext); return STR_NG; }
202 <BINDSYM_COND>[a-zA-Z0-9_]+     { yylval.string = sstrdup(yytext); return WORD; }
203 [a-zA-Z]+                       { yylval.string = sstrdup(yytext); return WORD; }
204 .                               { return (int)yytext[0]; }
205
206 <<EOF>> {
207     while (yy_start_stack_ptr > 0)
208         yy_pop_state();
209     yyterminate();
210 }
211
212 %%