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
|
/* Copyright (C) 2025 Aryadev Chavali
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License Version 2 for
* details.
* You may distribute and modify this code under the terms of the GNU General
* Public License Version 2, which you should have received a copy of along with
* this program. If not, please go to <https://www.gnu.org/licenses/>.
* Created: 2025-04-16
* Description: Parsing Lisp!
*/
#ifndef READER_H
#define READER_H
#include <lib/vec.h>
#include <lisp/context.h>
#include <lisp/lisp.h>
#define INPUT_CHUNK_SIZE 512
static const char SYM_CHARS[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
"¬!£$%^&*_-+={[]}:@~#<,>.?/";
typedef struct
{
const char *name;
u64 offset;
sv_t str;
} input_t;
void input_from_sv(context_t *ctx, input_t *inp, const char *name, sv_t sv);
void input_from_fp(context_t *ctx, input_t *input, const char *name, FILE *fp);
bool input_eof(input_t *input);
typedef struct
{
u64 col, line;
} pos_t;
pos_t input_offset_to_pos(input_t *inp);
typedef enum
{
PERR_OK = 0,
PERR_EOF,
PERR_UNEXPECTED_CHAR,
PERR_EXPECTED_CLOSE_BRACKET,
PERR_EXPECTED_SPEECH_MARK,
PERR_UNEXPECTED_READER_MACRO_SYMBOL,
PERR_EXPECTED_BOOLEAN,
} perr_t;
static inline const char *perr_to_cstr(perr_t perr)
{
switch (perr)
{
case PERR_OK:
return "OK";
break;
case PERR_EOF:
return "EOF";
break;
case PERR_UNEXPECTED_CHAR:
return "UNEXPECTED_CHAR";
break;
case PERR_EXPECTED_CLOSE_BRACKET:
return "EXPECTED_CLOSE_BRACKET";
break;
case PERR_EXPECTED_SPEECH_MARK:
return "EXPECTED_SPEECH_MARK";
break;
case PERR_UNEXPECTED_READER_MACRO_SYMBOL:
return "UNEXPECTED_READER_MACRO_SYMBOL";
break;
case PERR_EXPECTED_BOOLEAN:
return "EXPECTED_BOOLEAN";
break;
}
assert(false && "perr_to_cstr: unreachable");
return "";
}
typedef struct
{
lisp_t *result;
perr_t error;
} pres_t;
perr_t parse(context_t *ctx, input_t *str, lisp_t **ret);
perr_t parse_all(context_t *ctx, input_t *str, vec_t *vec);
int print_perror(FILE *fp, input_t *inp, perr_t error);
#endif
|