]> git.sur5r.net Git - i3/i3/blob - src/cmdparse.l
Merge branch 'msg' into next
[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 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     cmdyycolumn = 1;
73 }
74
75     /* the next/prev tokens are here to recognize them *before* handling
76      * strings ('workspace' command) */
77 next                            { return TOK_NEXT; }
78 prev                            { return TOK_PREV; }
79
80 <WANT_STRING>\"[^\"]+\"         {
81                                   BEGIN(INITIAL);
82                                   /* strip quotes */
83                                   char *copy = sstrdup(yytext+1);
84                                   copy[strlen(copy)-1] = '\0';
85                                   cmdyylval.string = copy;
86                                   return STR;
87                                 }
88 <WANT_QSTRING>\"[^\"]+\"        {
89                                   BEGIN(INITIAL);
90                                   /* strip quotes */
91                                   char *copy = sstrdup(yytext+1);
92                                   copy[strlen(copy)-1] = '\0';
93                                   cmdyylval.string = copy;
94                                   return STR;
95                                 }
96
97 <WANT_STRING>[^;\n]+            { BEGIN(INITIAL); cmdyylval.string = sstrdup(yytext); return STR; }
98
99 <EAT_WHITESPACE>[;\n]           { BEGIN(INITIAL); return ';'; }
100 <EAT_WHITESPACE>[ \t]*          { yy_pop_state(); }
101
102 [ \t]*                          { /* ignore whitespace */ ; }
103 exec                            { WS_STRING; return TOK_EXEC; }
104 exit                            { return TOK_EXIT; }
105 reload                          { return TOK_RELOAD; }
106 restart                         { return TOK_RESTART; }
107 kill                            { return TOK_KILL; }
108 window                          { return TOK_WINDOW; }
109 client                          { return TOK_CLIENT; }
110 fullscreen                      { return TOK_FULLSCREEN; }
111 global                          { return TOK_GLOBAL; }
112 layout                          { return TOK_LAYOUT; }
113 default                         { return TOK_DEFAULT; }
114 stacked                         { return TOK_STACKED; }
115 stacking                        { return TOK_STACKED; }
116 tabbed                          { return TOK_TABBED; }
117 border                          { return TOK_BORDER; }
118 normal                          { return TOK_NORMAL; }
119 none                            { return TOK_NONE; }
120 1pixel                          { return TOK_1PIXEL; }
121 mode                            { BEGIN(WANT_QSTRING); return TOK_MODE; }
122 tiling                          { return TOK_TILING; }
123 floating                        { return TOK_FLOATING; }
124 toggle                          { return TOK_TOGGLE; }
125 mode_toggle                     { return TOK_MODE_TOGGLE; }
126 workspace                       { WS_STRING; return TOK_WORKSPACE; }
127 output                          { WS_STRING; return TOK_OUTPUT; }
128 focus                           { return TOK_FOCUS; }
129 move                            { return TOK_MOVE; }
130 open                            { return TOK_OPEN; }
131 split                           { return TOK_SPLIT; }
132 horizontal                      { return TOK_HORIZONTAL; }
133 vertical                        { return TOK_VERTICAL; }
134 up                              { return TOK_UP; }
135 down                            { return TOK_DOWN; }
136 left                            { return TOK_LEFT; }
137 right                           { return TOK_RIGHT; }
138 parent                          { return TOK_PARENT; }
139 child                           { return TOK_CHILD; }
140 resize                          { return TOK_RESIZE; }
141 shrink                          { return TOK_SHRINK; }
142 grow                            { return TOK_GROW; }
143 px                              { return TOK_PX; }
144 or                              { return TOK_OR; }
145 ppt                             { return TOK_PPT; }
146 nop                             { WS_STRING; return TOK_NOP; }
147 append_layout                   { WS_STRING; return TOK_APPEND_LAYOUT; }
148 mark                            { WS_STRING; return TOK_MARK; }
149
150 enable                          { return TOK_ENABLE; }
151 true                            { return TOK_ENABLE; }
152 yes                             { return TOK_ENABLE; }
153 disable                         { return TOK_DISABLE; }
154 false                           { return TOK_DISABLE; }
155 no                              { return TOK_DISABLE; }
156
157 class                           { BEGIN(WANT_QSTRING); return TOK_CLASS; }
158 instance                        { BEGIN(WANT_QSTRING); return TOK_INSTANCE; }
159 window_role                     { BEGIN(WANT_QSTRING); return TOK_WINDOW_ROLE; }
160 id                              { BEGIN(WANT_QSTRING); return TOK_ID; }
161 con_id                          { BEGIN(WANT_QSTRING); return TOK_CON_ID; }
162 con_mark                        { BEGIN(WANT_QSTRING); return TOK_MARK; }
163 title                           { BEGIN(WANT_QSTRING); return TOK_TITLE; }
164
165 [0-9]+                          { cmdyylval.number = atoi(yytext); return NUMBER; }
166
167 .                               { return (int)yytext[0]; }
168
169 <<EOF>> {
170     while (yy_start_stack_ptr > 0)
171         yy_pop_state();
172     yyterminate();
173 }
174
175 %%