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