summaryrefslogtreecommitdiff
path: root/core/lexer.c
blob: 72aa94271370ed59ed9a6e646e51442b060f8406 (plain)
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
/*
 * Copyright (c) 2026, Apollo Telephone Laboratories.
 * Provided under the BSD-3 clause.
 */

#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "defconf/lexer.h"
#include "defconf/ptrbox.h"

/* Constants */
#define MAX_KW_LEN 32

/*
 * Returns true if the given character is a whitespace character
 *
 * @c:  Character to check
 */
static inline bool
lexer_is_ws(char c)
{
    switch (c) {
    case '\t':
    case ' ':
    case '\f':
    case '\r':
    case '\n':
        return true;
    }

    return false;
}

/*
 * Place a character back in the putback buffer
 *
 * @state:  Defconf state
 * @c:      Character to put back
 */
static inline void
lexer_putback(struct defconf_state *state, int c)
{
    if (state == NULL) {
        return;
    }

    state->lex_cache = c;
}

/*
 * Nom a single character from the input source file
 *
 * @state:      Defconf state machine
 * @skip_ws:    If true, skip whitespace
 *
 * Returns the character on success, otherwise EOF on failure
 */
static int
lexer_nom(struct defconf_state *state, bool skip_ws)
{
    int c;

    if (state == NULL) {
        return EOF;
    }

    if ((c = state->lex_cache) != '\0') {
        state->lex_cache = '\0';
        if (lexer_is_ws(c) && !skip_ws)
            return c;
        if (!lexer_is_ws(c))
            return c;
    }

    while ((c = fgetc(state->in_fp)) != EOF) {
        if (lexer_is_ws(c) && skip_ws)
            continue;

        break;
    }

    return c;
}

/*
 * Scan a keyword
 *
 * @state: Defconf state
 * @lc:    Last character provided
 */
static char *
lexer_scan_ident(struct defconf_state *state, int lc)
{
    char c, buf[MAX_KW_LEN];
    size_t bufind = 0;

    if (state == NULL) {
        return NULL;
    }

    if (!isalpha(lc)) {
        return NULL;
    }

    buf[bufind++] = lc;
    for (;;) {
        if (bufind >= sizeof(buf) - 1) {
            printf("fatal: token exceeds maximum length\n");
            return NULL;
        }

        c = lexer_nom(state, false);
        if (!isalnum(c) && c != '_') {
            lexer_putback(state, c);
            buf[bufind] = '\0';
            return ptrbox_strdup(&state->ptrbox, buf);
        }

        buf[bufind++] = c;
    }

    return NULL;
}

/*
 * Check if a keyword matches against known keywords
 *
 * @kw:         Keyword to check
 * @result:     Result is written here
 *
 * Returns zero on success
 */
static int
lexer_check_kw(const char *kw, struct token *result)
{
    if (kw == NULL || result == NULL) {
        return -1;
    }

    switch (*kw) {
    case 'd':
        if (strcmp(kw, "define") == 0) {
            result->type = TT_DEFINE;
            return 0;
        }

        break;
    case 't':
        if (strcmp(kw, "true") == 0) {
            result->type = TT_TRUE;
            return 0;
        }

        break;
    case 'f':
        if (strcmp(kw, "false") == 0) {
            result->type = TT_FALSE;
            return 0;
        }

        break;
    }

    return -1;
}

int
lexer_scan(struct defconf_state *state, struct token *result)
{
    int c;
    char *ident;

    if (state == NULL || result == NULL) {
        return -1;
    }

    if ((c = lexer_nom(state, true)) == EOF) {
        return -1;
    }

    /* Scan for an identifier */
    if ((ident = lexer_scan_ident(state, c)) != NULL) {
        if (lexer_check_kw(ident, result) == 0) {
            return 0;
        }

        result->type = TT_IDENT;
        result->s = ident;
        return 0;
    }

    printf("fatal: unexpected token '%c'\n", c);
    return -1;
}