]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.l
Merge branch 'fix-split'
[i3/i3] / src / cmdparse.l
1 /*
2  * vim:ts=4:sw=4:expandtab
3  *
4  * i3 - an improved dynamic tiling window manager
5  * © 2009-2011 Michael Stapelberg and contributors (see also: LICENSE)
6  *
7  * cmdparse.l: the lexer for commands you send to i3 (or bind on keys)
8  *
9  */
10 %option nounput
11 %option noinput
12 %option noyy_top_state
13 %option stack
14
15 %{
16 #include <stdio.h>
17 #include <string.h>
18 #include "cmdparse.tab.h"
19
20 #include "config.h"
21 #include "util.h"
22 #include "libi3.h"
23
24 int cmdyycolumn = 1;
25
26 #define YY_DECL int yylex (struct context *context)
27
28 #define YY_USER_ACTION { \
29     context->first_column = cmdyycolumn; \
30     context->last_column = cmdyycolumn+yyleng-1; \
31     cmdyycolumn += 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 /* handle everything up to \n as a string */
45 %s WANT_STRING
46 /* eat a whitespace, then go to the next state on the stack */
47 %s EAT_WHITESPACE
48 /* handle a quoted string or everything up to the next whitespace */
49 %s WANT_QSTRING
50
51 %x EXEC
52
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     cmdyycolumn = 1;
75 }
76
77     /* the next/prev/back_and_forth tokens are here to recognize them *before*
78      * handling strings ('workspace' command) */
79 next                            { BEGIN(INITIAL); return TOK_NEXT; }
80 prev                            { BEGIN(INITIAL); return TOK_PREV; }
81 back_and_forth                  { BEGIN(INITIAL); return TOK_BACK_AND_FORTH; }
82
83 <WANT_STRING>\"[^\"]+\"         {
84                                   BEGIN(INITIAL);
85                                   /* strip quotes */
86                                   char *copy = sstrdup(yytext+1);
87                                   copy[strlen(copy)-1] = '\0';
88                                   cmdyylval.string = copy;
89                                   return STR;
90                                 }
91 <WANT_QSTRING>\"[^\"]+\"        {
92                                   BEGIN(INITIAL);
93                                   /* strip quotes */
94                                   char *copy = sstrdup(yytext+1);
95                                   copy[strlen(copy)-1] = '\0';
96                                   cmdyylval.string = copy;
97                                   return STR;
98                                 }
99
100 <WANT_STRING>[^;\n]+            { BEGIN(INITIAL); cmdyylval.string = sstrdup(yytext); return STR; }
101
102 <EAT_WHITESPACE>[;\n]           { BEGIN(INITIAL); return ';'; }
103 <EAT_WHITESPACE>[ \t]*          { yy_pop_state(); }
104
105 [ \t]*                          { /* ignore whitespace */ ; }
106 <EXEC>--no-startup-id           { printf("no startup id\n"); yy_pop_state(); return TOK_NO_STARTUP_ID; }
107 <EXEC>.                         { printf("anything else: *%s*\n", yytext); yyless(0); yy_pop_state(); yy_pop_state(); }
108 exec                            { WS_STRING; yy_push_state(EXEC); yy_push_state(EAT_WHITESPACE); return TOK_EXEC; }
109 exit                            { return TOK_EXIT; }
110 reload                          { return TOK_RELOAD; }
111 restart                         { return TOK_RESTART; }
112 kill                            { return TOK_KILL; }
113 window                          { return TOK_WINDOW; }
114 client                          { return TOK_CLIENT; }
115 fullscreen                      { return TOK_FULLSCREEN; }
116 global                          { return TOK_GLOBAL; }
117 layout                          { return TOK_LAYOUT; }
118 default                         { return TOK_DEFAULT; }
119 stacked                         { return TOK_STACKED; }
120 stacking                        { return TOK_STACKED; }
121 tabbed                          { return TOK_TABBED; }
122 border                          { return TOK_BORDER; }
123 normal                          { return TOK_NORMAL; }
124 none                            { return TOK_NONE; }
125 1pixel                          { return TOK_1PIXEL; }
126 mode                            { BEGIN(WANT_QSTRING); return TOK_MODE; }
127 tiling                          { return TOK_TILING; }
128 floating                        { return TOK_FLOATING; }
129 toggle                          { return TOK_TOGGLE; }
130 mode_toggle                     { return TOK_MODE_TOGGLE; }
131 workspace                       { WS_STRING; return TOK_WORKSPACE; }
132 output                          { WS_STRING; return TOK_OUTPUT; }
133 focus                           { return TOK_FOCUS; }
134 move                            { return TOK_MOVE; }
135 open                            { return TOK_OPEN; }
136 split                           { return TOK_SPLIT; }
137 horizontal                      { return TOK_HORIZONTAL; }
138 vertical                        { return TOK_VERTICAL; }
139 up                              { return TOK_UP; }
140 down                            { return TOK_DOWN; }
141 left                            { return TOK_LEFT; }
142 right                           { return TOK_RIGHT; }
143 parent                          { return TOK_PARENT; }
144 child                           { return TOK_CHILD; }
145 resize                          { return TOK_RESIZE; }
146 shrink                          { return TOK_SHRINK; }
147 grow                            { return TOK_GROW; }
148 px                              { return TOK_PX; }
149 or                              { return TOK_OR; }
150 ppt                             { return TOK_PPT; }
151 nop                             { WS_STRING; return TOK_NOP; }
152 append_layout                   { WS_STRING; return TOK_APPEND_LAYOUT; }
153 mark                            { WS_STRING; return TOK_MARK; }
154
155 enable                          { return TOK_ENABLE; }
156 true                            { return TOK_ENABLE; }
157 yes                             { return TOK_ENABLE; }
158 disable                         { return TOK_DISABLE; }
159 false                           { return TOK_DISABLE; }
160 no                              { return TOK_DISABLE; }
161
162 class                           { BEGIN(WANT_QSTRING); return TOK_CLASS; }
163 instance                        { BEGIN(WANT_QSTRING); return TOK_INSTANCE; }
164 window_role                     { BEGIN(WANT_QSTRING); return TOK_WINDOW_ROLE; }
165 id                              { BEGIN(WANT_QSTRING); return TOK_ID; }
166 con_id                          { BEGIN(WANT_QSTRING); return TOK_CON_ID; }
167 con_mark                        { BEGIN(WANT_QSTRING); return TOK_MARK; }
168 title                           { BEGIN(WANT_QSTRING); return TOK_TITLE; }
169
170 [0-9]+                          { cmdyylval.number = atoi(yytext); return NUMBER; }
171
172 .                               { return (int)yytext[0]; }
173
174 <<EOF>> {
175     while (yy_start_stack_ptr > 0)
176         yy_pop_state();
177     yyterminate();
178 }
179
180 %%