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