File: decl.ha

package info (click to toggle)
hare-update 0.26.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,044 kB
  • sloc: makefile: 37; sh: 14
file content (278 lines) | stat: -rw-r--r-- 6,660 bytes parent folder | download
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
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use ascii;
use common;
use common::{ltok, nonterminal};
use strings;
use vNEXT::ast;
use vNEXT::lex;

fn attr_symbol(lexer: *lex::lexer) (str | error) = {
	want(lexer, ltok::LPAREN)?;
	let t = want(lexer, ltok::LIT_STR)?;
	let s = t.1 as str;
	let d = strings::iter(s);
	match (strings::next(&d)) {
	case done => void;
	case let r: rune =>
		synassert(t.2, ascii::isalpha(r) || r == '.'
			|| r == '_', "Invalid symbol")?;
	};
	for (let r => strings::next(&d)) {
		synassert(t.2, ascii::isalnum(r) || r == '$'
			|| r == '.' || r == '_', "Invalid symbol")?;
	};
	want(lexer, ltok::RPAREN)?;
	return s;
};

// Parses a command-line definition
export fn define(lexer: *lex::lexer) (ast::decl_const | error) = {
	const ident = ident(lexer)?;
	const _type: nullable *ast::_type = match (try(lexer, ltok::COLON)?) {
	case common::token => yield alloc(_type(lexer)?)!;
	case void => yield null;
	};
	want(lexer, ltok::EQUAL)?;
	const init: *ast::expr = alloc(expr(lexer)?)!;
	return ast::decl_const {
		ident = ident,
		_type = _type,
		init = init,
	};
};

fn decl_const(
	lexer: *lex::lexer,
	tok: ltok,
) ([]ast::decl_const | error) = {
	let decl: []ast::decl_const = [];
	for (true) {
		append(decl, define(lexer)?)!;

		if (try(lexer, ltok::COMMA)? is void) {
			break;
		};
	};
	return decl;

};

export type global_hook = struct {
	tok: ltok,
	sym: str,
	threadlocal: bool,
};

export fn decl_global(
	lexer: *lex::lexer,
	tok: ltok,
	sym: str,
	threadlocal: bool,
) ([]ast::decl_global | error) = {
	on(lexer, nonterminal::GLOBAL_DECLARATION, &global_hook {
		tok = tok,
		sym = sym,
		threadlocal = threadlocal,
	})?;

	let decl: []ast::decl_global = [];
	for (true) {
		const start = lex::mkloc(lexer);
		match (try(lexer, ltok::ATTR_SYMBOL, ltok::ATTR_THREADLOCAL)?) {
		case void => void;
		case let t: common::token =>
			switch (t.0) {
			case ltok::ATTR_SYMBOL =>
				sym = attr_symbol(lexer)?;
			case ltok::ATTR_THREADLOCAL =>
				threadlocal = true;
			case => abort();
			};
		};
		const ident = ident(lexer)?;
		const _type: nullable *ast::_type =
			match (try(lexer, ltok::COLON)?) {
			case common::token =>
				yield alloc(_type(lexer)?)!;
			case void =>
				yield null;
			};
		const init: nullable *ast::expr =
			match (try(lexer, ltok::EQUAL)?) {
			case common::token =>
				yield alloc(initializer(lexer)?)!;
			case void =>
				yield null;
			};
		const end = lex::mkloc(lexer);
		const btok = try(lexer, ltok::COMMA)?;
		append(decl, ast::decl_global {
			is_const = tok == ltok::CONST,
			is_threadlocal = threadlocal,
			symbol = sym,
			ident = ident,
			_type = _type,
			init = init,
			start = start,
			end = end,
		})!;
		if (btok is void) {
			break;
		};
	};
	return decl;
};

fn decl_type(lexer: *lex::lexer) ([]ast::decl_type | error) = {
	let decl: []ast::decl_type = [];
	for (true) {
		let ident = ident(lexer)?;
		want(lexer, ltok::EQUAL)?;
		let _type = _type(lexer)?;
		let btok = try(lexer, ltok::COMMA)?;
		append(decl, ast::decl_type {
			ident = ident,
			_type = alloc(_type)!,
		})!;
		if (btok is void) {
			break;
		};
	};
	return decl;
};

fn decl_func(
	lexer: *lex::lexer,
	sym: str,
	attr: ast::fndecl_attr,
) (ast::decl_func | error) = {
	on(lexer, nonterminal::FUNCTION_DECLARATION, null)?;

	let ident_loc = lex::mkloc(lexer);
	let ident = ident(lexer)?;
	let proto_start = lex::mkloc(lexer);
	let prototype = prototype(lexer)?;
	let proto_end = lex::prevloc(lexer);

	let tok = want(lexer, ltok::EQUAL, ltok::SEMICOLON)?;
	let body = switch (tok.0) {
	case ltok::EQUAL =>
		yield alloc(expr(lexer)?)!;
	case ltok::SEMICOLON =>
		lex::unlex(lexer, tok);
		yield null;
	case => abort(); // unreachable
	};

	return ast::decl_func {
		symbol = sym,
		ident = ident,
		prototype = alloc(ast::_type {
			start = proto_start,
			end = proto_end,
			flags = 0,
			repr = prototype,
		})!,
		body = body,
		attrs = attr,
	};
};

// Parses a declaration.
export fn decl(lexer: *lex::lexer) (ast::decl | error) = {
	on(lexer, nonterminal::DECLARATION, null)?;

	const start = lex::mkloc(lexer);
	let comment = "";
	if (try(lexer, ltok::STATIC)? is common::token) {
		comment = lex::comment(lexer);
		let expr = assert_expr(lexer, true)?;
		want(lexer, ltok::SEMICOLON)?;
		return ast::decl {
			exported = false,
			start = start,
			end = expr.end,
			decl = expr.expr as ast::assert_expr,
			docs = comment,
		};
	};
	let exported = match (try(lexer, ltok::EXPORT)?) {
	case void =>
		yield false;
	case common::token =>
		comment = lex::comment(lexer);
		yield true;
	};
	const attrs = [
		ltok::ATTR_FINI, ltok::ATTR_INIT, ltok::ATTR_TEST,
		ltok::ATTR_SYMBOL, ltok::ATTR_THREADLOCAL,
	];
	let attr = ast::fndecl_attr::NONE, sym = "", threadlocal = false;
	for (true) match (try(lexer, attrs...)?) {
	case void =>
		break;
	case let t: common::token =>
		synassert(t.2, t.0 == ltok::ATTR_SYMBOL
			|| t.0 == ltok::ATTR_THREADLOCAL || attr == 0,
			"Only one of @init, @fini, or @test may be provided")?;
		switch (t.0) {
		case ltok::ATTR_FINI =>
			attr = ast::fndecl_attr::FINI;
		case ltok::ATTR_INIT =>
			attr = ast::fndecl_attr::INIT;
		case ltok::ATTR_TEST =>
			attr = ast::fndecl_attr::TEST;
		case ltok::ATTR_SYMBOL =>
			sym = attr_symbol(lexer)?;
		case ltok::ATTR_THREADLOCAL =>
			threadlocal = true;
		case =>
			abort("unreachable");
		};
	};
	const t = want(lexer, ltok::CONST, ltok::LET,
		ltok::DEF, ltok::TYPE, ltok::FN)?;
	if (comment == "") {
		comment = lex::comment(lexer);
	};
	synassert(t.2, !threadlocal || t.0 == ltok::LET || t.0 == ltok::CONST,
		"@threadlocal is only valid on globals")?;
	synassert(t.2, sym == "" || t.0 == ltok::LET
			|| t.0 == ltok::CONST || t.0 == ltok::FN,
		"@symbol is only valid on globals and functions")?;
	synassert(t.2, attr == ast::fndecl_attr::NONE || t.0 == ltok::FN,
		"@init, @fini, and @test are only valid on functions")?;

	let decl = switch (t.0) {
	case ltok::FN =>
		yield decl_func(lexer, sym, attr)?;
	case ltok::TYPE =>
		yield decl_type(lexer)?;
	case ltok::LET, ltok::CONST =>
		yield decl_global(lexer, t.0, sym, threadlocal)?;
	case ltok::DEF =>
		yield decl_const(lexer, t.0)?;
	case => abort();
	};
	want(lexer, ltok::SEMICOLON)?;
	return ast::decl {
		exported = exported,
		start = start,
		end = lex::mkloc(lexer),
		decl = decl,
		docs = comment,
	};
};

// Parses the declarations for a sub-unit.
export fn decls(lexer: *lex::lexer) ([]ast::decl | error) = {
	let decls: []ast::decl = [];
	for (true) {
		if (peek(lexer, ltok::EOF)? is common::token) break;
		append(decls, decl(lexer)?)!;
	};
	free(lex::comment(lexer));
	return decls;
};