]> git.sur5r.net Git - i3/i3/blob - i3-config-wizard/cfgparse.y
Merge branch 'tree' into next
[i3/i3] / i3-config-wizard / cfgparse.y
1 %{
2 /*
3  * vim:ts=4:sw=4:expandtab
4  *
5  */
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <unistd.h>
9 #include <fcntl.h>
10 #include <stdint.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14
15 #include <X11/Xlib.h>
16
17 extern Display *dpy;
18
19 struct context {
20         int line_number;
21         char *line_copy;
22
23         char *compact_error;
24
25         /* These are the same as in YYLTYPE */
26         int first_column;
27         int last_column;
28
29         char *result;
30 };
31
32 typedef struct yy_buffer_state *YY_BUFFER_STATE;
33 extern int yylex(struct context *context);
34 extern int yyparse(void);
35 extern FILE *yyin;
36 YY_BUFFER_STATE yy_scan_string(const char *);
37
38 static struct context *context;
39
40 /* We don’t need yydebug for now, as we got decent error messages using
41  * yyerror(). Should you ever want to extend the parser, it might be handy
42  * to just comment it in again, so it stays here. */
43 //int yydebug = 1;
44
45 void yyerror(const char *error_message) {
46     fprintf(stderr, "\n");
47     fprintf(stderr, "CONFIG: %s\n", error_message);
48     fprintf(stderr, "CONFIG: line %d:\n",
49         context->line_number);
50     fprintf(stderr, "CONFIG:   %s\n", context->line_copy);
51     fprintf(stderr, "CONFIG:   ");
52     for (int c = 1; c <= context->last_column; c++)
53         if (c >= context->first_column)
54             fprintf(stderr, "^");
55         else fprintf(stderr, " ");
56     fprintf(stderr, "\n");
57     fprintf(stderr, "\n");
58 }
59
60 int yywrap() {
61     return 1;
62 }
63
64 char *rewrite_binding(const char *bindingline) {
65     char *result = NULL;
66
67     context = calloc(sizeof(struct context), 1);
68
69     yy_scan_string(bindingline);
70
71     if (yyparse() != 0) {
72         fprintf(stderr, "Could not parse configfile\n");
73         exit(1);
74     }
75
76     result = context->result;
77
78     if (context->line_copy)
79         free(context->line_copy);
80     free(context);
81
82     return result;
83 }
84
85 /* XXX: does not work for combinations of modifiers yet */
86 static char *modifier_to_string(int modifiers) {
87     //printf("should convert %d to string\n", modifiers);
88     if (modifiers == (1 << 3))
89         return strdup("$mod+");
90     else if (modifiers == ((1 << 3) | (1 << 0)))
91         return strdup("$mod+Shift+");
92     else if (modifiers == (1 << 9))
93         return strdup("$mod+");
94     else if (modifiers == ((1 << 9) | (1 << 0)))
95         return strdup("$mod+Shift+");
96     else if (modifiers == (1 << 0))
97         return strdup("Shift+");
98     else return strdup("");
99 }
100
101 %}
102
103 %error-verbose
104 %lex-param { struct context *context }
105
106 %union {
107     int number;
108     char *string;
109 }
110
111 %token <number>NUMBER "<number>"
112 %token <string>STR "<string>"
113 %token TOKBINDCODE
114 %token TOKMODVAR "$mod"
115 %token MODIFIER "<modifier>"
116 %token TOKCONTROL "control"
117 %token TOKSHIFT "shift"
118 %token WHITESPACE "<whitespace>"
119
120 %%
121
122 lines: /* empty */
123     | lines WHITESPACE bindcode
124     | lines error
125     | lines bindcode
126     ;
127
128 bindcode:
129     TOKBINDCODE WHITESPACE binding_modifiers NUMBER WHITESPACE STR
130     {
131         //printf("\tFound keycode binding mod%d with key %d and command %s\n", $<number>3, $4, $<string>6);
132         int level = 0;
133         if (($<number>3 & (1 << 0))) {
134             /* When shift is included, we really need to use the second-level
135              * symbol (upper-case). The lower-case symbol could be on a
136              * different key than the upper-case one (unlikely for letters, but
137              * more likely for special characters). */
138             level = 1;
139         }
140         KeySym sym = XKeycodeToKeysym(dpy, $4, level);
141         char *str = XKeysymToString(sym);
142         char *modifiers = modifier_to_string($<number>3);
143         // TODO: modifier to string
144         asprintf(&(context->result), "bindsym %s%s %s\n", modifiers, str, $<string>6);
145         free(modifiers);
146     }
147     ;
148
149 binding_modifiers:
150     /* NULL */                               { $<number>$ = 0; }
151     | binding_modifier
152     | binding_modifiers '+' binding_modifier { $<number>$ = $<number>1 | $<number>3; }
153     | binding_modifiers '+'                  { $<number>$ = $<number>1; }
154     ;
155
156 binding_modifier:
157     MODIFIER        { $<number>$ = $<number>1; }
158     | TOKMODVAR     { $<number>$ = $<number>1; }
159     | TOKCONTROL    { $<number>$ = (1 << 2); }
160     | TOKSHIFT      { $<number>$ = (1 << 0); }
161     ;