summaryrefslogtreecommitdiff
path: root/parser.c
blob: d9f45438040bcb8923e69eee0453d7d3e1b3c01b (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
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
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#include "parser.h"
#include "util.h"

typedef struct Cursor{
	const char *s;
	int l;
} Cursor;

typedef enum Tokentype{
	TT_EOF, //str=NULL, len=-1
	TT_SYMBOL,
	TT_WORD,
	TT_QUOTEDWORD,
	TT_NUMBER,
	TT_STRING,

	TT_ERR=-1 //str is an error description, len=-1
} Tokentype;

typedef struct Token{
	Tokentype type;
	const char *str; //pointer into source
	int len;
} Token;

#define SYMBOLCHARS "'()[]"


static Token tt_make(Tokentype type,const char *str,int len){
	Token tok={type,str,len};
	return tok;
}

static Token tt_eof(void){
	return tt_make(TT_EOF,NULL,-1);
}

static Token tt_err(const char *errstr){
	return tt_make(TT_ERR,errstr,-1);
}


static void advance(Cursor *cursor,int n){
	assert(cursor->l>=n);
	cursor->s+=n;
	cursor->l-=n;
}

static bool iswordchar(char c){
	return strchr(SYMBOLCHARS,c)==NULL&&c>=33&&c<=126;
}

static Token nexttoken(Cursor *cursor){
	while(cursor->l>=1&&isspace(*cursor->s))advance(cursor,1);
	if(cursor->l==0)return tt_eof();

	bool acted;
	do {
		acted=false;
		if(*cursor->s==';'){
			acted=true;
			int i;
			for(i=1;i<cursor->l;i++){
				if(cursor->s[i]=='\n')break;
			}
			if(i>=cursor->l-1){
				advance(cursor,cursor->l);
				return tt_eof();
			}
			advance(cursor,i+1);
		}

		if(cursor->l>=4&&cursor->s[0]=='#'&&cursor->s[1]=='|'){
			acted=true;
			int i;
			for(i=3;i<cursor->l;i++){
				if(cursor->s[i-1]=='|'&&cursor->s[i]=='#')break;
			}
			if(i>=cursor->l-1){
				advance(cursor,cursor->l);
				return tt_eof();
			}
			advance(cursor,i+1);
		}
	} while(acted);

	while(cursor->l>=1&&isspace(*cursor->s))advance(cursor,1);
	if(cursor->l==0)return tt_eof();

	if(strchr(SYMBOLCHARS,*cursor->s)!=NULL){
		advance(cursor,1);
		return tt_make(TT_SYMBOL,cursor->s-1,1);
	}

	if(isdigit(*cursor->s)||(cursor->l>=2&&cursor->s[0]=='-'&&isdigit(cursor->s[1]))){
		char *endp;
		strtod(cursor->s,&endp);
		assert(endp>cursor->s);
		int len=endp-cursor->s;
		advance(cursor,len);
		return tt_make(TT_NUMBER,cursor->s-len,len);
	}

	if(*cursor->s=='"'){
		int i;
		for(i=1;i<cursor->l;i++){
			if(cursor->s[i]=='"')break;
			if(cursor->s[i]=='\\')i++;
		}
		if(i==cursor->l){
			return tt_err("Unclosed string in source");
		}
		i++;
		advance(cursor,i);
		return tt_make(TT_STRING,cursor->s-i,i);
	}

	bool isquoted=false;
	if(*cursor->s=='\''){
		isquoted=true;
		advance(cursor,1);
		if(cursor->l==0||!iswordchar(*cursor->s)){
			return tt_err("Lone single quote in source");
		}
	}

	int i;
	for(i=0;i<cursor->l;i++){
		if(!iswordchar(cursor->s[i]))break;
	}
	if(i==0){
		return tt_err("Unrecognised character while looking for next token");
	}
	advance(cursor,i);
	return tt_make(isquoted?TT_QUOTEDWORD:TT_WORD,cursor->s-i,i);
}


static bool ishexdigit(char c){
	return (c>='0'&&c<='9')||(c>='a'&&c<='f')||(c>='A'&&c<='F');
}

static int hexnum(char c){
	return c<='9'?c-'0':(c&~32)-'A';
}


static ParseRet pr_ast(AST *ast){
	ParseRet pr={ast,NULL};
	return pr;
}

static ParseRet pr_err(char *errstr){
	ParseRet pr={NULL,errstr};
	return pr;
}

static ParseRet pr_err_c(const char *errstr){
	return pr_err(copystring(errstr));
}

static ParseRet parse_(Cursor *cursor){
	Token tok=nexttoken(cursor);
	switch(tok.type){
		case TT_EOF:
			return pr_err_c("Unexpected end-of-file");

		case TT_SYMBOL:{
			char closing;
			if(tok.len!=1)assert(false);
			if(tok.str[0]=='\''){
				ParseRet pr=parse_(cursor);
				if(pr.errstr)return pr;
				if(pr.ast->type==AST_WORD){
					char *word=pr.ast->wo.word;
					pr.ast->type=AST_SYMBOL;
					pr.ast->sy.name=word;
					pr.ast->sy.symid=-1;
					return pr;
				}
				return pr_ast(ast_quoted(pr.ast));
			}

			if(tok.str[0]=='(')closing=')';
			else if(tok.str[0]=='[')closing=']';
			else if(tok.str[0]==')'||tok.str[0]==']'){
				return pr_err_c("Unexpected closing paren in source");
			} else assert(false);

			int sz=2,len=0;
			AST **nodes=malloc(sz,AST*);
			while(true){
				Cursor cur2v=*cursor,*cur2=&cur2v;
				Token t=nexttoken(cur2);
				if(t.type==TT_SYMBOL&&t.len==1&&t.str[0]==closing){
					*cursor=cur2v;
					break;
				}
				if(t.type==TT_EOF){
					for(int i=0;i<len;i++)ast_free(nodes[i]);
					free(nodes);
					return pr_err_c("Unexpected end-of-file while parsing list");
				}

				ParseRet pr=parse_(cursor);
				if(pr.errstr){
					for(int i=0;i<len;i++)ast_free(nodes[i]);
					free(nodes);
					return pr;
				}
				if(len==sz){
					sz*=2;
					nodes=realloc(nodes,sz,AST*);
				}
				nodes[len++]=pr.ast;
			}
			return pr_ast(ast_list(len,nodes));
		}

		case TT_WORD:
			return pr_ast(ast_word(copybufasstring(tok.str,tok.len)));

		case TT_QUOTEDWORD:
			return pr_ast(ast_symbol(copybufasstring(tok.str,tok.len)));

		case TT_NUMBER:
			return pr_ast(ast_number(strtod(tok.str,NULL)));

		case TT_STRING:{
			assert(tok.len>=2&&tok.str[0]=='"'&&tok.str[tok.len-1]=='"');
			int len=0;
			for(int i=1;i<tok.len-1;i++){
				if(tok.str[i]=='\\'){
					i++;
					assert(i<tok.len-1);
					if(tok.str[i]=='x'){
						if(i>=tok.len-3||!ishexdigit(tok.str[i+1])||!ishexdigit(tok.str[i+2])){
							return pr_err_c("\"\\x\" in string needs two hexadecimal digits");
						}
						i+=2;
					}
				}
				len++;
			}
			char *buf=malloc(len==0?1:len,char);
			int j=0;
			for(int i=1;i<tok.len-1;i++){
				if(tok.str[i]=='\\'){
					i++;
					switch(tok.str[i]){
						case 'x':
							buf[j++]=16*hexnum(tok.str[i+1])+hexnum(tok.str[i+2]);
							i+=2;
							break;

						case 'n': buf[j++]='\n'; break;
						case 't': buf[j++]='\t'; break;
						case 'r': buf[j++]='\r'; break;
						case 'b': buf[j++]='\b'; break;
						case 'a': buf[j++]='\a'; break;
						default: buf[j++]=tok.str[i]; break;
					}
				} else {
					buf[j++]=tok.str[i];
				}
			}
			return pr_ast(ast_string(buf,len));
		}

		case TT_ERR:
			return pr_err_c(tok.str);

		default:
			assert(false);
	}
}


ParseRet parse(const char *source,int length){
	Cursor cursor={source,length};
	return parse_(&cursor);
}