aboutsummaryrefslogtreecommitdiff
path: root/src/parser.c
blob: d16418687df79dcef65ad1a0b7be934bc1720f22 (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
#include "parser.h"

#include <ctype.h>
#include <stdlib.h>

static void skip_spaces(Parser *ctx) {
    while (isspace(parser_peek(ctx))) {
        parser_getchar(ctx);
    }
}

ParseResult ParseOk(SExpRef ref) {
    return (ParseResult){ .val = ref, .errmsg = NULL };
}

ParseResult ParseErr(const char *msg) {
    return (ParseResult){ .val = {-1}, .errmsg = msg };
}

ParseResult parse_sexp(Parser *ctx) {
    skip_spaces(ctx);
    int next = parser_peek(ctx);
    if (next == '(') {
        return parse_list(ctx);
    } else if (next == ',') {
        parser_getchar(ctx);
        if (parser_peek(ctx) == '@') {
            return parse_slicing_unquote(ctx);
        }
        return parse_unquote(ctx);
    } else if (next == '`') {
        return parse_quasi(ctx);
    } else if (next == '\'') {
        return parse_quote(ctx);
    }
    return parse_atom(ctx);
}