aboutsummaryrefslogtreecommitdiff
path: root/src/parse.c
blob: cc5250c67526be3b7352547a814c1cf611425a85 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#include "parse.h"
#include "tokenize.h"
#include "locale.h"
#include "hotkey.h"
#include "hashtable.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#define internal static

internal char *
read_file(const char *file)
{
    unsigned length;
    char *buffer = NULL;
    FILE *handle = fopen(file, "r");

    if (handle) {
        fseek(handle, 0, SEEK_END);
        length = ftell(handle);
        fseek(handle, 0, SEEK_SET);
        buffer = malloc(length + 1);
        fread(buffer, length, 1, handle);
        buffer[length] = '\0';
        fclose(handle);
    }

    return buffer;
}

internal char *
copy_string_count(char *s, int length)
{
    char *result = malloc(length + 1);
    memcpy(result, s, length);
    result[length] = '\0';
    return result;
}

internal uint32_t
keycode_from_hex(char *hex)
{
    uint32_t result;
    sscanf(hex, "%x", &result);
    return result;
}

internal char *
parse_command(struct parser *parser)
{
    struct token command = parser_previous(parser);
    char *result = copy_string_count(command.text, command.length);
    printf("\tcmd: '%s'\n", result);
    return result;
}

internal uint32_t
parse_key_hex(struct parser *parser)
{
    struct token key = parser_previous(parser);
    char *hex = copy_string_count(key.text, key.length);
    uint32_t keycode = keycode_from_hex(hex);
    free(hex);
    printf("\tkey: '%.*s' (0x%02x)\n", key.length, key.text, keycode);
    return keycode;
}

internal uint32_t
parse_key(struct parser *parser)
{
    uint32_t keycode;
    struct token key = parser_previous(parser);
    keycode = keycode_from_char(*key.text);
    printf("\tkey: '%c' (0x%02x)\n", *key.text, keycode);
    return keycode;
}

internal uint32_t literal_keycode_value[] =
{
    kVK_Return,     kVK_Tab,           kVK_Space,
    kVK_Delete,     kVK_ForwardDelete, kVK_Escape,
    kVK_Home,       kVK_End,           kVK_PageUp,
    kVK_PageDown,   kVK_Help,          kVK_LeftArrow,
    kVK_RightArrow, kVK_UpArrow,       kVK_DownArrow,
    kVK_F1,         kVK_F2,            kVK_F3,
    kVK_F4,         kVK_F5,            kVK_F6,
    kVK_F7,         kVK_F8,            kVK_F9,
    kVK_F10,        kVK_F11,           kVK_F12,
    kVK_F13,        kVK_F14,           kVK_F15,
    kVK_F16,        kVK_F17,           kVK_F18,
    kVK_F19,        kVK_F20,
};

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wsometimes-uninitialized"
// NOTE(koekeishiya): shut up compiler !!!
// if we get to this point, we already KNOW that the input is valid..
internal uint32_t
parse_key_literal(struct parser *parser)
{
    uint32_t keycode;
    struct token key = parser_previous(parser);

    for (int i = 0; i < array_count(literal_keycode_str); ++i) {
        if (token_equals(key, literal_keycode_str[i])) {
            keycode = literal_keycode_value[i];
            printf("\tkey: '%.*s' (0x%02x)\n", key.length, key.text, keycode);
            break;
        }
    }

    return keycode;
}
#pragma clang diagnostic pop

internal enum hotkey_flag modifier_flags_value[] =
{
    Hotkey_Flag_Alt,        Hotkey_Flag_LAlt,       Hotkey_Flag_RAlt,
    Hotkey_Flag_Shift,      Hotkey_Flag_LShift,     Hotkey_Flag_RShift,
    Hotkey_Flag_Cmd,        Hotkey_Flag_LCmd,       Hotkey_Flag_RCmd,
    Hotkey_Flag_Control,    Hotkey_Flag_LControl,   Hotkey_Flag_RControl,
    Hotkey_Flag_Fn,         Hotkey_Flag_Hyper,
};

internal uint32_t
parse_modifier(struct parser *parser)
{
    uint32_t flags = 0;
    struct token modifier = parser_previous(parser);

    for (int i = 0; i < array_count(modifier_flags_str); ++i) {
        if (token_equals(modifier, modifier_flags_str[i])) {
            flags |= modifier_flags_value[i];
            printf("\tmod: '%s'\n", modifier_flags_str[i]);
            break;
        }
    }

    if (parser_match(parser, Token_Plus)) {
        if (parser_match(parser, Token_Modifier)) {
            flags |= parse_modifier(parser);
        } else {
            fprintf(stderr, "(#%d:%d) expected modifier, but got '%.*s'\n",
                    parser->current_token.line, parser->current_token.cursor,
                    parser->current_token.length, parser->current_token.text);
            parser->error = true;
        }
    }

    return flags;
}

internal struct hotkey *
parse_hotkey(struct parser *parser)
{
    struct hotkey *hotkey = malloc(sizeof(struct hotkey));
    int found_modifier;

    printf("(#%d) hotkey :: {\n", parser->current_token.line);

    if (parser_match(parser, Token_Modifier)) {
        hotkey->flags = parse_modifier(parser);
        if (parser->error) {
            return NULL;
        }
        found_modifier = 1;
    } else {
        hotkey->flags = found_modifier = 0;
    }

    if (found_modifier) {
        if (!parser_match(parser, Token_Dash)) {
            fprintf(stderr, "(#%d:%d) expected '-', but got '%.*s'\n",
                    parser->current_token.line, parser->current_token.cursor,
                    parser->current_token.length, parser->current_token.text);
            parser->error = true;
            return NULL;
        }
    }

    if (parser_match(parser, Token_Key)) {
        hotkey->key = parse_key(parser);
    } else if (parser_match(parser, Token_Key_Hex)) {
        hotkey->key = parse_key_hex(parser);
    } else if (parser_match(parser, Token_Literal)) {
        hotkey->key = parse_key_literal(parser);
    } else {
        fprintf(stderr, "(#%d:%d) expected key-literal, but got '%.*s'\n",
                parser->current_token.line, parser->current_token.cursor,
                parser->current_token.length, parser->current_token.text);
        parser->error = true;
        return NULL;
    }

    if (parser_match(parser, Token_Arrow)) {
        hotkey->flags |= Hotkey_Flag_Passthrough;
    }

    if (parser_match(parser, Token_Command)) {
        hotkey->command = parse_command(parser);
    } else {
        fprintf(stderr, "(#%d:%d) expected ':' followed by command, but got '%.*s'\n",
                parser->current_token.line, parser->current_token.cursor,
                parser->current_token.length, parser->current_token.text);
        parser->error = true;
        return NULL;
    }

    printf("}\n");

    return hotkey;
}

void parse_config(struct parser *parser, struct table *hotkey_map)
{
    struct hotkey *hotkey;
    while (!parser_eof(parser)) {
        if ((parser_check(parser, Token_Modifier)) ||
            (parser_check(parser, Token_Literal)) ||
            (parser_check(parser, Token_Key_Hex)) ||
            (parser_check(parser, Token_Key))) {
            hotkey = parse_hotkey(parser);
            if (parser->error) {
                free_hotkeys(hotkey_map);
                return;
            }
            table_add(hotkey_map, hotkey, hotkey);
        } else {
            fprintf(stderr, "(#%d:%d) expected modifier or key-literal, but got '%.*s'\n",
                    parser->current_token.line, parser->current_token.cursor,
                    parser->current_token.length, parser->current_token.text);
            parser->error = true;
            return;
        }
    }
}

struct token
parser_peek(struct parser *parser)
{
    return parser->current_token;
}

struct token
parser_previous(struct parser *parser)
{
    return parser->previous_token;
}

bool parser_eof(struct parser *parser)
{
    struct token token = parser_peek(parser);
    return token.type == Token_EndOfStream;
}

struct token
parser_advance(struct parser *parser)
{
    if (!parser_eof(parser)) {
        parser->previous_token = parser->current_token;
        parser->current_token = get_token(&parser->tokenizer);
    }
    return parser_previous(parser);
}

bool parser_check(struct parser *parser, enum token_type type)
{
    if (parser_eof(parser)) return false;
    struct token token = parser_peek(parser);
    return token.type == type;
}

bool parser_match(struct parser *parser, enum token_type type)
{
    if (parser_check(parser, type)) {
        parser_advance(parser);
        return true;
    }
    return false;
}

bool parser_init(struct parser *parser, char *file)
{
    memset(parser, 0, sizeof(struct parser));
    char *buffer = read_file(file);
    if (buffer) {
        tokenizer_init(&parser->tokenizer, buffer);
        parser_advance(parser);
        return true;
    }
    return false;
}

void parser_destroy(struct parser *parser)
{
    free(parser->tokenizer.buffer);
}