harec

[hare] Hare compiler, written in C11 for POSIX OSs
Log | Files | Refs | README | LICENSE

parse.c (58117B)


      1 #include <assert.h>
      2 #include <ctype.h>
      3 #include <stdarg.h>
      4 #include <stdbool.h>
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <stdnoreturn.h>
      8 #include <string.h>
      9 #include "ast.h"
     10 #include "check.h"
     11 #include "identifier.h"
     12 #include "lex.h"
     13 #include "parse.h"
     14 #include "types.h"
     15 #include "utf8.h"
     16 #include "util.h"
     17 
     18 static void
     19 synassert_msg(bool cond, const char *msg, struct token *tok)
     20 {
     21 	if (!cond) {
     22 		fprintf(stderr, "Syntax error: %s at %s:%d:%d (found '%s')\n", msg,
     23 			sources[tok->loc.file], tok->loc.lineno, tok->loc.colno,
     24 			token_str(tok));
     25 		errline(sources[tok->loc.file], tok->loc.lineno, tok->loc.colno);
     26 		exit(EXIT_FAILURE);
     27 	}
     28 }
     29 
     30 static noreturn void
     31 vsynerr(struct token *tok, va_list ap)
     32 {
     33 	enum lexical_token t = va_arg(ap, enum lexical_token);
     34 	fprintf(stderr,
     35 		"Syntax error: unexpected '%s' at %s:%d:%d%s",
     36 		token_str(tok), sources[tok->loc.file], tok->loc.lineno,
     37 		tok->loc.colno, t == T_EOF ? "\n" : ", expected " );
     38 	while (t != T_EOF) {
     39 		if (t == T_LITERAL || t == T_NAME) {
     40 			fprintf(stderr, "%s", lexical_token_str(t));
     41 		} else {
     42 			fprintf(stderr, "'%s'", lexical_token_str(t));
     43 		}
     44 		t = va_arg(ap, enum lexical_token);
     45 		fprintf(stderr, "%s", t == T_EOF ? "\n" : ", ");
     46 	}
     47 	errline(sources[tok->loc.file], tok->loc.lineno, tok->loc.colno);
     48 	exit(EXIT_FAILURE);
     49 }
     50 
     51 static noreturn void
     52 synerr(struct token *tok, ...)
     53 {
     54 	va_list ap;
     55 	va_start(ap, tok);
     56 	vsynerr(tok, ap);
     57 }
     58 
     59 static void
     60 synassert(bool cond, struct token *tok, ...)
     61 {
     62 	if (!cond) {
     63 		va_list ap;
     64 		va_start(ap, tok);
     65 		vsynerr(tok, ap);
     66 	}
     67 }
     68 
     69 static void
     70 want(struct lexer *lexer, enum lexical_token ltok, struct token *tok)
     71 {
     72 	struct token _tok = {0};
     73 	struct token *out = tok ? tok : &_tok;
     74 	lex(lexer, out);
     75 	synassert(out->token == ltok, out, ltok, T_EOF);
     76 	if (!tok) {
     77 		token_finish(out);
     78 	}
     79 }
     80 
     81 static struct ast_expression *
     82 mkexpr(const struct location *loc)
     83 {
     84 	struct ast_expression *exp = xcalloc(1, sizeof(struct ast_expression));
     85 	exp->loc = *loc;
     86 	return exp;
     87 }
     88 
     89 static struct ast_type *
     90 mktype(const struct location *loc)
     91 {
     92 	struct ast_type *t = xcalloc(1, sizeof(struct ast_type));
     93 	t->loc = *loc;
     94 	return t;
     95 }
     96 
     97 static struct ast_function_parameters *
     98 mkfuncparams(const struct location *loc)
     99 {
    100 	struct ast_function_parameters *p =
    101 		xcalloc(1, sizeof(struct ast_function_parameters));
    102 	p->loc = *loc;
    103 	return p;
    104 }
    105 
    106 bool
    107 parse_identifier(struct lexer *lexer, struct identifier *ident, bool trailing)
    108 {
    109 	struct token tok = {0};
    110 	struct identifier *i = ident;
    111 	*ident = (struct identifier){0};
    112 	bool found_trailing = false;
    113 	while (!i->name) {
    114 		switch (lex(lexer, &tok)) {
    115 		case T_NAME:
    116 			i->name = xstrdup(tok.name);
    117 			token_finish(&tok);
    118 			break;
    119 		default:
    120 			synassert(trailing && i->ns, &tok, T_NAME, T_EOF);
    121 			unlex(lexer, &tok);
    122 			struct identifier *ns = i->ns;
    123 			*i = *ns;
    124 			free(ns);
    125 			found_trailing = true;
    126 			continue;
    127 		}
    128 
    129 		struct identifier *ns;
    130 		switch (lex(lexer, &tok)) {
    131 		case T_DOUBLE_COLON:
    132 			ns = xcalloc(1, sizeof(struct identifier));
    133 			*ns = *i;
    134 			i->ns = ns;
    135 			i->name = NULL;
    136 			break;
    137 		default:
    138 			unlex(lexer, &tok);
    139 			break;
    140 		}
    141 	}
    142 	return found_trailing;
    143 }
    144 
    145 static void
    146 parse_name_list(struct lexer *lexer, struct ast_imports *name)
    147 {
    148 	bool more = true;
    149 	struct ast_imports **next = &name->next;
    150 	while (more) {
    151 		struct token tok = {0};
    152 		want(lexer, T_NAME, &tok);
    153 		name->ident.name = xstrdup(tok.name);
    154 		name->loc = tok.loc;
    155 		token_finish(&tok);
    156 
    157 		switch (lex(lexer, &tok)) {
    158 		case T_COMMA:
    159 			switch (lex(lexer, &tok)) {
    160 			case T_RBRACE:
    161 				more = false;
    162 				break;
    163 			default:
    164 				unlex(lexer, &tok);
    165 				name = xcalloc(1, sizeof(struct ast_imports));
    166 				*next = name;
    167 				next = &name->next;
    168 			}
    169 			break;
    170 		case T_EQUAL:
    171 			name->alias = xcalloc(1, sizeof(struct identifier));
    172 			*name->alias = name->ident;
    173 			break;
    174 		case T_RBRACE:
    175 			more = false;
    176 			break;
    177 		default:
    178 			synerr(&tok, T_RBRACE, T_EQUAL, T_COMMA, T_EOF);
    179 			break;
    180 		}
    181 	}
    182 }
    183 
    184 static void
    185 parse_import(struct lexer *lexer, struct ast_imports *imports)
    186 {
    187 	struct identifier ident = {0};
    188 	struct token tok = {0};
    189 	imports->mode = 0;
    190 	while (true) {
    191 		bool trailing_colon = parse_identifier(lexer, &ident, true);
    192 		switch (lex(lexer, &tok)) {
    193 		case T_EQUAL:
    194 			synassert(!trailing_colon, &tok, T_NAME, T_EOF);
    195 			synassert(!(imports->mode & AST_IMPORT_ALIAS), &tok,
    196 					T_SEMICOLON, T_LBRACE, T_EOF);
    197 			imports->mode |= AST_IMPORT_ALIAS;
    198 			imports->alias = xcalloc(1, sizeof(struct identifier));
    199 			*imports->alias = ident;
    200 			break;
    201 		case T_LBRACE:
    202 			synassert(trailing_colon, &tok, T_DOUBLE_COLON, T_EOF);
    203 			imports->mode |= AST_IMPORT_MEMBERS;
    204 			imports->ident = ident;
    205 			imports->members = xcalloc(1, sizeof(struct ast_imports));
    206 			parse_name_list(lexer, imports->members);
    207 			want(lexer, T_SEMICOLON, &tok);
    208 			return;
    209 		case T_SEMICOLON:
    210 			synassert(!trailing_colon, &tok, T_NAME, T_EOF);
    211 			imports->ident = ident;
    212 			return;
    213 		case T_TIMES:
    214 			synassert(trailing_colon, &tok, T_DOUBLE_COLON, T_EOF);
    215 			synassert(!(imports->mode & AST_IMPORT_ALIAS), &tok,
    216 					T_SEMICOLON, T_LBRACE, T_EOF);
    217 			imports->mode |= AST_IMPORT_WILDCARD;
    218 			imports->alias = NULL;
    219 			imports->ident = ident;
    220 			want(lexer, T_SEMICOLON, &tok);
    221 			return;
    222 		default:
    223 			synassert(!trailing_colon, &tok, T_EQUAL, T_SEMICOLON, T_EOF);
    224 			synassert(trailing_colon, &tok, T_NAME, T_LBRACE, T_EOF);
    225 			break;
    226 		}
    227 	}
    228 }
    229 
    230 static void
    231 parse_imports(struct lexer *lexer, struct ast_subunit *subunit)
    232 {
    233 	struct token tok = {0};
    234 	struct ast_imports **next = &subunit->imports;
    235 
    236 	bool more = true;
    237 	while (more) {
    238 		struct ast_imports *imports;
    239 		switch (lex(lexer, &tok)) {
    240 		case T_USE:
    241 			imports = xcalloc(1, sizeof(struct ast_imports));
    242 			parse_import(lexer, imports);
    243 			*next = imports;
    244 			next = &imports->next;
    245 			break;
    246 		default:
    247 			unlex(lexer, &tok);
    248 			more = false;
    249 			break;
    250 		}
    251 	}
    252 }
    253 
    254 static void
    255 parse_parameter_list(struct lexer *lexer, struct ast_function_type *type)
    256 {
    257 	struct token tok = {0};
    258 	bool more = true;
    259 	type->params = mkfuncparams(&lexer->loc);
    260 	struct ast_function_parameters *next = type->params;
    261 	while (more) {
    262 		switch (lex(lexer, &tok)) {
    263 		case T_UNDERSCORE:
    264 			break;
    265 		case T_NAME:
    266 			next->name = tok.name; // Assumes ownership
    267 			break;
    268 		default:
    269 			synerr(&tok, T_UNDERSCORE, T_NAME, T_EOF);
    270 		}
    271 		want(lexer, T_COLON, NULL);
    272 		next->type = parse_type(lexer);
    273 
    274 		switch (lex(lexer, &tok)) {
    275 		case T_COMMA:
    276 			switch (lex(lexer, &tok)) {
    277 			case T_ELLIPSIS:
    278 				type->variadism = VARIADISM_C;
    279 				if (lex(lexer, &tok) != T_COMMA) {
    280 					unlex(lexer, &tok);
    281 				}
    282 				more = false;
    283 				break;
    284 			case T_RPAREN:
    285 				more = false;
    286 				unlex(lexer, &tok);
    287 				break;
    288 			default:
    289 				unlex(lexer, &tok);
    290 				next->next = mkfuncparams(&lexer->loc);
    291 				next = next->next;
    292 				break;
    293 			}
    294 			break;
    295 		case T_ELLIPSIS:
    296 			type->variadism = VARIADISM_HARE;
    297 			if (lex(lexer, &tok) != T_COMMA) {
    298 				unlex(lexer, &tok);
    299 			}
    300 			more = false;
    301 			break;
    302 		default:
    303 			more = false;
    304 			unlex(lexer, &tok);
    305 			break;
    306 		}
    307 	}
    308 }
    309 
    310 static void
    311 parse_prototype(struct lexer *lexer, struct ast_function_type *type)
    312 {
    313 	want(lexer, T_LPAREN, NULL);
    314 	struct token tok = {0};
    315 	if (lex(lexer, &tok) != T_RPAREN) {
    316 		unlex(lexer, &tok);
    317 		parse_parameter_list(lexer, type);
    318 		want(lexer, T_RPAREN, NULL);
    319 	}
    320 	type->result = parse_type(lexer);
    321 }
    322 
    323 static enum type_storage
    324 parse_integer_type(struct lexer *lexer)
    325 {
    326 	struct token tok = {0};
    327 	switch (lex(lexer, &tok)) {
    328 	case T_I8:
    329 		return STORAGE_I8;
    330 	case T_I16:
    331 		return STORAGE_I16;
    332 	case T_I32:
    333 		return STORAGE_I32;
    334 	case T_I64:
    335 		return STORAGE_I64;
    336 	case T_U8:
    337 		return STORAGE_U8;
    338 	case T_U16:
    339 		return STORAGE_U16;
    340 	case T_U32:
    341 		return STORAGE_U32;
    342 	case T_U64:
    343 		return STORAGE_U64;
    344 	case T_INT:
    345 		return STORAGE_INT;
    346 	case T_UINT:
    347 		return STORAGE_UINT;
    348 	case T_SIZE:
    349 		return STORAGE_SIZE;
    350 	case T_UINTPTR:
    351 		return STORAGE_UINTPTR;
    352 	case T_CHAR:
    353 		return STORAGE_CHAR;
    354 	default:
    355 		assert(0);
    356 	}
    357 }
    358 
    359 static struct ast_type *
    360 parse_primitive_type(struct lexer *lexer)
    361 {
    362 	struct token tok = {0};
    363 	struct ast_type *type = mktype(&lexer->loc);
    364 	switch (lex(lexer, &tok)) {
    365 	case T_I8:
    366 	case T_I16:
    367 	case T_I32:
    368 	case T_I64:
    369 	case T_U8:
    370 	case T_U16:
    371 	case T_U32:
    372 	case T_U64:
    373 	case T_INT:
    374 	case T_UINT:
    375 	case T_SIZE:
    376 	case T_UINTPTR:
    377 	case T_CHAR:
    378 		unlex(lexer, &tok);
    379 		type->storage = parse_integer_type(lexer);
    380 		break;
    381 	case T_RUNE:
    382 		type->storage = STORAGE_RUNE;
    383 		break;
    384 	case T_STR:
    385 		type->storage = STORAGE_STRING;
    386 		break;
    387 	case T_F32:
    388 		type->storage = STORAGE_F32;
    389 		break;
    390 	case T_F64:
    391 		type->storage = STORAGE_F64;
    392 		break;
    393 	case T_BOOL:
    394 		type->storage = STORAGE_BOOL;
    395 		break;
    396 	case T_VOID:
    397 		type->storage = STORAGE_VOID;
    398 		break;
    399 	case T_VALIST:
    400 		type->storage = STORAGE_VALIST;
    401 		break;
    402 	default:
    403 		assert(0);
    404 	}
    405 	return type;
    406 }
    407 
    408 static struct ast_expression *parse_binding_list(
    409 		struct lexer *lexer, bool is_static);
    410 static struct ast_expression *parse_object_selector(struct lexer *lexer);
    411 
    412 static struct ast_type *
    413 parse_enum_type(struct identifier *ident, struct lexer *lexer)
    414 {
    415 	struct token tok = {0};
    416 	struct ast_type *type = mktype(&lexer->loc);
    417 	type->storage = STORAGE_ENUM;
    418 	identifier_dup(&type->alias, ident);
    419 	struct ast_enum_field **next = &type->_enum.values;
    420 	switch (lex(lexer, &tok)) {
    421 	case T_LBRACE:
    422 		type->_enum.storage = STORAGE_INT;
    423 		unlex(lexer, &tok);
    424 		break;
    425 	case T_I8:
    426 	case T_I16:
    427 	case T_I32:
    428 	case T_I64:
    429 	case T_U8:
    430 	case T_U16:
    431 	case T_U32:
    432 	case T_U64:
    433 	case T_INT:
    434 	case T_UINT:
    435 	case T_SIZE:
    436 	case T_UINTPTR:
    437 	case T_CHAR:
    438 		unlex(lexer, &tok);
    439 		type->_enum.storage = parse_integer_type(lexer);
    440 		break;
    441 	case T_RUNE:
    442 		type->_enum.storage = STORAGE_RUNE;
    443 		break;
    444 	default:
    445 		synassert_msg(false, "Enum storage must be an integer or rune", &tok);
    446 	}
    447 	want(lexer, T_LBRACE, NULL);
    448 	while (tok.token != T_RBRACE) {
    449 		*next = xcalloc(1, sizeof(struct ast_enum_field));
    450 		want(lexer, T_NAME, &tok);
    451 		(*next)->name = tok.name;
    452 		(*next)->loc = tok.loc;
    453 		if (lex(lexer, &tok) == T_EQUAL) {
    454 			(*next)->value = parse_expression(lexer);
    455 		} else {
    456 			unlex(lexer, &tok);
    457 		}
    458 		next = &(*next)->next;
    459 		switch (lex(lexer, &tok)) {
    460 		case T_COMMA:
    461 			if (lex(lexer, &tok) != T_RBRACE) {
    462 				unlex(lexer, &tok);
    463 			}
    464 			break;
    465 		case T_RBRACE:
    466 			break;
    467 		default:
    468 			synerr(&tok, T_COMMA, T_RBRACE, T_EOF);
    469 		}
    470 	}
    471 	return type;
    472 }
    473 
    474 static struct ast_type *
    475 parse_struct_union_type(struct lexer *lexer)
    476 {
    477 	struct token tok = {0};
    478 	struct ast_type *type = mktype(&lexer->loc);
    479 	struct ast_struct_union_field *next = &type->struct_union.fields;
    480 	switch (lex(lexer, &tok)) {
    481 	case T_STRUCT:
    482 		type->storage = STORAGE_STRUCT;
    483 		if (lex(lexer, &tok) == T_ATTR_PACKED) {
    484 			type->struct_union.packed = true;
    485 		} else {
    486 			unlex(lexer, &tok);
    487 		}
    488 		break;
    489 	case T_UNION:
    490 		type->storage = STORAGE_UNION;
    491 		break;
    492 	default:
    493 		synerr(&tok, T_STRUCT, T_UNION, T_EOF);
    494 		break;
    495 	}
    496 	want(lexer, T_LBRACE, NULL);
    497 	while (tok.token != T_RBRACE) {
    498 		if (lex(lexer, &tok) == T_ATTR_OFFSET) {
    499 			want(lexer, T_LPAREN, NULL);
    500 			next->offset = parse_expression(lexer);
    501 			want(lexer, T_RPAREN, NULL);
    502 		} else {
    503 			unlex(lexer, &tok);
    504 		}
    505 
    506 		char *name;
    507 		switch (lex(lexer, &tok)) {
    508 		case T_NAME:
    509 			name = tok.name;
    510 			struct location loc = tok.loc;
    511 			switch (lex(lexer, &tok)) {
    512 			case T_COLON:
    513 				next->name = name;
    514 				next->type = parse_type(lexer);
    515 				break;
    516 			case T_DOUBLE_COLON:
    517 				next->type = mktype(&loc);
    518 				next->type->storage = STORAGE_ALIAS;
    519 				next->type->unwrap = false;
    520 				parse_identifier(lexer, &next->type->alias, false);
    521 				struct identifier *i = &next->type->alias;
    522 				while (i->ns != NULL) {
    523 					i = i->ns;
    524 				}
    525 				i->ns = xcalloc(sizeof(struct identifier), 1);
    526 				i->ns->name = name;
    527 				break;
    528 			default:
    529 				unlex(lexer, &tok);
    530 				next->type = mktype(&loc);
    531 				next->type->storage = STORAGE_ALIAS;
    532 				next->type->alias.name = name;
    533 				next->type->unwrap = false;
    534 				break;
    535 			}
    536 			break;
    537 		case T_STRUCT:
    538 		case T_UNION:
    539 			unlex(lexer, &tok);
    540 			next->name = NULL;
    541 			next->type = parse_type(lexer);
    542 			break;
    543 		default:
    544 			synerr(&tok, T_NAME, T_STRUCT, T_UNION, T_EOF);
    545 		}
    546 		switch (lex(lexer, &tok)) {
    547 		case T_COMMA:
    548 			if (lex(lexer, &tok) != T_RBRACE) {
    549 				unlex(lexer, &tok);
    550 				next->next = xcalloc(1,
    551 					sizeof(struct ast_struct_union_field));
    552 				next = next->next;
    553 			}
    554 			break;
    555 		case T_RBRACE:
    556 			break;
    557 		default:
    558 			synerr(&tok, T_COMMA, T_RBRACE, T_EOF);
    559 		}
    560 	}
    561 	return type;
    562 }
    563 
    564 static struct ast_type *
    565 parse_tagged_type(struct lexer *lexer, struct ast_type *first)
    566 {
    567 	struct ast_type *type = mktype(&first->loc);
    568 	type->storage = STORAGE_TAGGED;
    569 	struct ast_tagged_union_type *next = &type->tagged_union;
    570 	next->type = first;
    571 	struct token tok = {0};
    572 	while (tok.token != T_RPAREN) {
    573 		next->next = xcalloc(sizeof(struct ast_tagged_union_type), 1);
    574 		next = next->next;
    575 		next->type = parse_type(lexer);
    576 		switch (lex(lexer, &tok)) {
    577 		case T_BOR:
    578 			if (lex(lexer, &tok) != T_RPAREN) {
    579 				unlex(lexer, &tok);
    580 			}
    581 			break;
    582 		case T_RPAREN:
    583 			break;
    584 		default:
    585 			synerr(&tok, T_BOR, T_RPAREN, T_EOF);
    586 		}
    587 	}
    588 	return type;
    589 }
    590 
    591 static struct ast_type *
    592 parse_tuple_type(struct lexer *lexer, struct ast_type *first)
    593 {
    594 	struct ast_type *type = mktype(&first->loc);
    595 	type->storage = STORAGE_TUPLE;
    596 	struct ast_tuple_type *next = &type->tuple;
    597 	next->type = first;
    598 	struct token tok = {0};
    599 	while (tok.token != T_RPAREN) {
    600 		next->next = xcalloc(sizeof(struct ast_tuple_type), 1);
    601 		next = next->next;
    602 		next->type = parse_type(lexer);
    603 		switch (lex(lexer, &tok)) {
    604 		case T_COMMA:
    605 			if (lex(lexer, &tok) != T_RPAREN) {
    606 				unlex(lexer, &tok);
    607 			}
    608 			break;
    609 		case T_RPAREN:
    610 			break;
    611 		default:
    612 			synerr(&tok, T_COMMA, T_RPAREN, T_EOF);
    613 		}
    614 	}
    615 	return type;
    616 }
    617 
    618 static struct ast_type *
    619 parse_tagged_or_tuple_type(struct lexer *lexer)
    620 {
    621 	struct ast_type *type = parse_type(lexer);
    622 	struct token tok = {0};
    623 	switch (lex(lexer, &tok)) {
    624 	case T_BOR:
    625 		return parse_tagged_type(lexer, type);
    626 	case T_COMMA:
    627 		return parse_tuple_type(lexer, type);
    628 	default:
    629 		synerr(&tok, T_BOR, T_COMMA, T_EOF);
    630 	}
    631 	assert(0); // Unreachable
    632 }
    633 
    634 struct ast_type *
    635 parse_type(struct lexer *lexer)
    636 {
    637 	struct token tok = {0};
    638 	uint32_t flags = 0;
    639 	switch (lex(lexer, &tok)) {
    640 	case T_CONST:
    641 		flags |= TYPE_CONST;
    642 		break;
    643 	default:
    644 		unlex(lexer, &tok);
    645 		break;
    646 	}
    647 	switch (lex(lexer, &tok)) {
    648 	case T_LNOT:
    649 		flags |= TYPE_ERROR;
    650 		break;
    651 	default:
    652 		unlex(lexer, &tok);
    653 		break;
    654 	}
    655 	struct ast_type *type = NULL;
    656 	bool _noreturn = false, nullable = false, unwrap = false;
    657 	switch (lex(lexer, &tok)) {
    658 	case T_BOOL:
    659 	case T_CHAR:
    660 	case T_F32:
    661 	case T_F64:
    662 	case T_I16:
    663 	case T_I32:
    664 	case T_I64:
    665 	case T_I8:
    666 	case T_INT:
    667 	case T_RUNE:
    668 	case T_SIZE:
    669 	case T_STR:
    670 	case T_U16:
    671 	case T_U32:
    672 	case T_U64:
    673 	case T_U8:
    674 	case T_UINT:
    675 	case T_UINTPTR:
    676 	case T_VALIST:
    677 	case T_VOID:
    678 		unlex(lexer, &tok);
    679 		type = parse_primitive_type(lexer);
    680 		break;
    681 	case T_NULLABLE:
    682 		nullable = true;
    683 		want(lexer, T_TIMES, NULL);
    684 		/* fallthrough */
    685 	case T_TIMES:
    686 		type = mktype(&lexer->loc);
    687 		type->storage = STORAGE_POINTER;
    688 		type->pointer.referent = parse_type(lexer);
    689 		if (nullable) {
    690 			type->pointer.flags |= PTR_NULLABLE;
    691 		}
    692 		break;
    693 	case T_STRUCT:
    694 	case T_UNION:
    695 		unlex(lexer, &tok);
    696 		type = parse_struct_union_type(lexer);
    697 		break;
    698 	case T_LPAREN:
    699 		type = parse_tagged_or_tuple_type(lexer);
    700 		break;
    701 	case T_LBRACKET:
    702 		type = mktype(&lexer->loc);
    703 		switch (lex(lexer, &tok)) {
    704 		case T_RBRACKET:
    705 			type->storage = STORAGE_SLICE;
    706 			type->slice.members = parse_type(lexer);
    707 			break;
    708 		case T_TIMES:
    709 			type->storage = STORAGE_ARRAY;
    710 			type->array.length = NULL;
    711 			want(lexer, T_RBRACKET, NULL);
    712 			type->array.members = parse_type(lexer);
    713 			break;
    714 		case T_UNDERSCORE:
    715 			type->storage = STORAGE_ARRAY;
    716 			type->array.length = NULL;
    717 			type->array.contextual = true;
    718 			want(lexer, T_RBRACKET, NULL);
    719 			type->array.members = parse_type(lexer);
    720 			break;
    721 		default:
    722 			type->storage = STORAGE_ARRAY;
    723 			unlex(lexer, &tok);
    724 			type->array.length = parse_expression(lexer);
    725 			want(lexer, T_RBRACKET, NULL);
    726 			type->array.members = parse_type(lexer);
    727 			break;
    728 		}
    729 		break;
    730 	case T_ATTR_NORETURN:
    731 		_noreturn = true;
    732 		want(lexer, T_FN, NULL);
    733 		// fallthrough
    734 	case T_FN:
    735 		type = mktype(&lexer->loc);
    736 		type->storage = STORAGE_FUNCTION;
    737 		parse_prototype(lexer, &type->func);
    738 		if (_noreturn) {
    739 			type->func.flags |= FN_NORETURN;
    740 		}
    741 		break;
    742 	case T_ELLIPSIS:
    743 		unwrap = true;
    744 		want(lexer, T_NAME, &tok);
    745 		// Fallthrough
    746 	case T_NAME:
    747 		unlex(lexer, &tok);
    748 		type = mktype(&lexer->loc);
    749 		type->storage = STORAGE_ALIAS;
    750 		type->unwrap = unwrap;
    751 		parse_identifier(lexer, &type->alias, false);
    752 		break;
    753 	default:
    754 		synassert_msg(false, "expected type", &tok);
    755 		break;
    756 	}
    757 	type->flags |= flags;
    758 
    759 	return type;
    760 }
    761 
    762 static struct ast_expression *
    763 parse_access(struct lexer *lexer, struct identifier ident)
    764 {
    765 	struct ast_expression *exp = mkexpr(&lexer->loc);
    766 	exp->type = EXPR_ACCESS;
    767 	exp->access.type = ACCESS_IDENTIFIER;
    768 	exp->access.ident = ident;
    769 	return exp;
    770 }
    771 
    772 static struct ast_expression *
    773 parse_constant(struct lexer *lexer)
    774 {
    775 	struct ast_expression *exp = mkexpr(&lexer->loc);
    776 	exp->type = EXPR_CONSTANT;
    777 
    778 	struct token tok = {0};
    779 	switch (lex(lexer, &tok)) {
    780 	case T_TRUE:
    781 		exp->constant.storage = STORAGE_BOOL;
    782 		exp->constant.bval = true;
    783 		return exp;
    784 	case T_FALSE:
    785 		exp->constant.storage = STORAGE_BOOL;
    786 		exp->constant.bval = false;
    787 		return exp;
    788 	case T_NULL:
    789 		exp->constant.storage = STORAGE_NULL;
    790 		return exp;
    791 	case T_VOID:
    792 		exp->constant.storage = STORAGE_VOID;
    793 		return exp;
    794 	case T_LITERAL:
    795 		exp->constant.storage = tok.storage;
    796 		break;
    797 	default:
    798 		synerr(&tok, T_LITERAL, T_TRUE,
    799 			T_FALSE, T_NULL, T_VOID, T_EOF);
    800 		break;
    801 	}
    802 
    803 	switch (tok.storage) {
    804 	case STORAGE_CHAR:
    805 	case STORAGE_U8:
    806 	case STORAGE_U16:
    807 	case STORAGE_U32:
    808 	case STORAGE_U64:
    809 	case STORAGE_UINT:
    810 	case STORAGE_UINTPTR:
    811 	case STORAGE_SIZE:
    812 		exp->constant.uval = (uintmax_t)tok.uval;
    813 		break;
    814 	case STORAGE_I8:
    815 	case STORAGE_I16:
    816 	case STORAGE_I32:
    817 	case STORAGE_I64:
    818 	case STORAGE_ICONST:
    819 	case STORAGE_INT:
    820 		exp->constant.ival = (intmax_t)tok.ival;
    821 		break;
    822 	case STORAGE_F32:
    823 	case STORAGE_F64:
    824 	case STORAGE_FCONST:
    825 		exp->constant.fval = tok.fval;
    826 		break;
    827 	case STORAGE_RCONST:
    828 		exp->constant.rune = tok.rune;
    829 		break;
    830 	case STORAGE_STRING:
    831 		exp->constant.string.len = tok.string.len;
    832 		exp->constant.string.value = tok.string.value;
    833 		while (lex(lexer, &tok) == T_LITERAL
    834 				&& tok.storage == STORAGE_STRING) {
    835 			size_t len = exp->constant.string.len;
    836 			exp->constant.string.value = xrealloc(
    837 				exp->constant.string.value,
    838 				len + tok.string.len);
    839 			memcpy(exp->constant.string.value + len,
    840 				tok.string.value, tok.string.len);
    841 			exp->constant.string.len += tok.string.len;
    842 		}
    843 		unlex(lexer, &tok);
    844 		break;
    845 	case STORAGE_BOOL:
    846 	case STORAGE_NULL:
    847 	case STORAGE_VOID:
    848 		assert(0); // Handled above
    849 	case STORAGE_ALIAS:
    850 	case STORAGE_ARRAY:
    851 	case STORAGE_ENUM:
    852 	case STORAGE_FUNCTION:
    853 	case STORAGE_POINTER:
    854 	case STORAGE_RUNE:
    855 	case STORAGE_SLICE:
    856 	case STORAGE_STRUCT:
    857 	case STORAGE_TAGGED:
    858 	case STORAGE_TUPLE:
    859 	case STORAGE_UNION:
    860 	case STORAGE_VALIST:
    861 		assert(0); // Handled in a different nonterminal
    862 	case STORAGE_ERROR:
    863 		assert(0); // Invariant
    864 	}
    865 	return exp;
    866 }
    867 
    868 static struct ast_expression *
    869 parse_array_literal(struct lexer *lexer)
    870 {
    871 	struct token tok;
    872 	want(lexer, T_LBRACKET, &tok);
    873 
    874 	struct ast_expression *exp = mkexpr(&lexer->loc);
    875 	exp->type = EXPR_CONSTANT;
    876 	exp->constant.storage = STORAGE_ARRAY;
    877 
    878 	struct ast_array_constant *item, **next = &exp->constant.array;
    879 
    880 	while (lex(lexer, &tok) != T_RBRACKET) {
    881 		unlex(lexer, &tok);
    882 
    883 		item = *next = xcalloc(1, sizeof(struct ast_array_constant));
    884 		item->value = parse_expression(lexer);
    885 		next = &item->next;
    886 
    887 		switch (lex(lexer, &tok)) {
    888 		case T_ELLIPSIS:
    889 			item->expand = true;
    890 			lex(lexer, &tok);
    891 			if (tok.token == T_COMMA) {
    892 				want(lexer, T_RBRACKET, &tok);
    893 				unlex(lexer, &tok);
    894 			} else if (tok.token == T_RBRACKET) {
    895 				unlex(lexer, &tok);
    896 			} else {
    897 				synerr(&tok, T_COMMA, T_RBRACKET, T_EOF);
    898 			}
    899 			break;
    900 		case T_COMMA:
    901 			// Move on
    902 			break;
    903 		case T_RBRACKET:
    904 			unlex(lexer, &tok);
    905 			break;
    906 		default:
    907 			synerr(&tok, T_ELLIPSIS, T_COMMA, T_RBRACKET, T_EOF);
    908 		}
    909 	}
    910 	return exp;
    911 }
    912 
    913 static struct ast_expression *parse_struct_literal(struct lexer *lexer,
    914 	struct identifier ident);
    915 
    916 static struct ast_field_value *
    917 parse_field_value(struct lexer *lexer)
    918 {
    919 	struct ast_field_value *exp =
    920 		xcalloc(sizeof(struct ast_field_value), 1);
    921 	char *name;
    922 	struct token tok = {0};
    923 	struct identifier ident = {0};
    924 	struct identifier *i;
    925 	switch (lex(lexer, &tok)) {
    926 	case T_NAME:
    927 		name = tok.name;
    928 		switch (lex(lexer, &tok)) {
    929 		case T_COLON:
    930 			exp->name = name;
    931 			exp->type = parse_type(lexer);
    932 			want(lexer, T_EQUAL, NULL);
    933 			exp->initializer = parse_expression(lexer);
    934 			break;
    935 		case T_EQUAL:
    936 			exp->name = name;
    937 			exp->initializer = parse_expression(lexer);
    938 			break;
    939 		case T_DOUBLE_COLON:
    940 			i = &ident;
    941 			parse_identifier(lexer, i, false);
    942 			while (i->ns != NULL) {
    943 				i = i->ns;
    944 			}
    945 			i->ns = xcalloc(sizeof(struct identifier), 1);
    946 			i->ns->name = name;
    947 			exp->initializer = parse_struct_literal(lexer, ident);
    948 			break;
    949 		default:
    950 			unlex(lexer, &tok);
    951 			ident.name = name;
    952 			ident.ns = NULL;
    953 			exp->initializer = parse_struct_literal(lexer, ident);
    954 			break;
    955 		}
    956 		break;
    957 	case T_STRUCT:
    958 		exp->initializer = parse_struct_literal(lexer, ident);
    959 		break;
    960 	default:
    961 		assert(0);
    962 	}
    963 	return exp;
    964 }
    965 
    966 static struct ast_expression *
    967 parse_struct_literal(struct lexer *lexer, struct identifier ident)
    968 {
    969 	want(lexer, T_LBRACE, NULL);
    970 	struct ast_expression *exp = mkexpr(&lexer->loc);
    971 	exp->type = EXPR_STRUCT;
    972 	exp->_struct.type = ident;
    973 	struct ast_field_value **next = &exp->_struct.fields;
    974 	struct token tok = {0};
    975 	while (tok.token != T_RBRACE) {
    976 		switch (lex(lexer, &tok)) {
    977 		case T_ELLIPSIS:
    978 			synassert(ident.name != NULL, &tok, T_RBRACE, T_EOF);
    979 			exp->_struct.autofill = true;
    980 			if (lex(lexer, &tok) != T_COMMA) {
    981 				unlex(lexer, &tok);
    982 			}
    983 			want(lexer, T_RBRACE, &tok);
    984 			unlex(lexer, &tok);
    985 			break;
    986 		case T_NAME:
    987 		case T_STRUCT:
    988 			unlex(lexer, &tok);
    989 			*next = parse_field_value(lexer);
    990 			next = &(*next)->next;
    991 			break;
    992 		default:
    993 			synerr(&tok, T_ELLIPSIS, T_NAME, T_RBRACE,
    994 				T_STRUCT, T_EOF);
    995 			break;
    996 		}
    997 		switch (lex(lexer, &tok)) {
    998 		case T_COMMA:
    999 			if (lex(lexer, &tok) != T_RBRACE) {
   1000 				unlex(lexer, &tok);
   1001 			}
   1002 			break;
   1003 		case T_RBRACE:
   1004 			break;
   1005 		default:
   1006 			synerr(&tok, T_COMMA, T_RBRACE, T_EOF);
   1007 		}
   1008 	}
   1009 	return exp;
   1010 }
   1011 
   1012 static struct ast_expression *
   1013 parse_tuple_expression(struct lexer *lexer, struct ast_expression *first)
   1014 {
   1015 	struct ast_expression *exp = mkexpr(&first->loc);
   1016 	exp->type = EXPR_TUPLE;
   1017 
   1018 	bool more = true;
   1019 	struct token tok = {0};
   1020 	struct ast_expression_tuple *tuple = &exp->tuple;
   1021 	tuple->expr = first;
   1022 	tuple->next = xcalloc(1, sizeof(struct ast_expression_tuple));
   1023 	tuple = tuple->next;
   1024 
   1025 	while (more) {
   1026 		tuple->expr = parse_expression(lexer);
   1027 
   1028 		switch (lex(lexer, &tok)) {
   1029 		case T_RPAREN:
   1030 			more = false;
   1031 			break;
   1032 		case T_COMMA:
   1033 			if (lex(lexer, &tok) == T_RPAREN) {
   1034 				more = false;
   1035 			} else {
   1036 				unlex(lexer, &tok);
   1037 				tuple->next = xcalloc(1,
   1038 					sizeof(struct ast_expression_tuple));
   1039 				tuple = tuple->next;
   1040 			}
   1041 			break;
   1042 		default:
   1043 			synerr(&tok, T_RPAREN, T_COMMA, T_EOF);
   1044 		}
   1045 	}
   1046 
   1047 	return exp;
   1048 }
   1049 
   1050 static struct ast_expression *
   1051 parse_plain_expression(struct lexer *lexer)
   1052 {
   1053 	struct token tok = {0};
   1054 	struct ast_expression *exp;
   1055 	switch (lex(lexer, &tok)) {
   1056 	// plain-expression
   1057 	case T_LITERAL:
   1058 	case T_TRUE:
   1059 	case T_FALSE:
   1060 	case T_NULL:
   1061 	case T_VOID:
   1062 		unlex(lexer, &tok);
   1063 		return parse_constant(lexer);
   1064 	case T_NAME:
   1065 		unlex(lexer, &tok);
   1066 		struct identifier ident = {0};
   1067 		parse_identifier(lexer, &ident, false);
   1068 		switch (lex(lexer, &tok)) {
   1069 		case T_LBRACE:
   1070 			unlex(lexer, &tok);
   1071 			return parse_struct_literal(lexer, ident);
   1072 		default:
   1073 			unlex(lexer, &tok);
   1074 			return parse_access(lexer, ident);
   1075 		}
   1076 		assert(0);
   1077 	case T_LBRACKET:
   1078 		unlex(lexer, &tok);
   1079 		return parse_array_literal(lexer);
   1080 	case T_STRUCT:
   1081 		ident.name = NULL;
   1082 		ident.ns = NULL;
   1083 		return parse_struct_literal(lexer, ident);
   1084 	// nested-expression
   1085 	case T_LPAREN:
   1086 		exp = parse_expression(lexer);
   1087 		switch (lex(lexer, &tok)) {
   1088 		case T_RPAREN:
   1089 			return exp;
   1090 		case T_COMMA:
   1091 			return parse_tuple_expression(lexer, exp);
   1092 		default:
   1093 			synerr(&tok, T_RPAREN, T_COMMA, T_EOF);
   1094 		};
   1095 		assert(0); // Unreachable
   1096 	default:
   1097 		synerr(&tok, T_LITERAL, T_NAME,
   1098 			T_LBRACKET, T_STRUCT, T_LPAREN, T_EOF);
   1099 	}
   1100 	assert(0); // Unreachable
   1101 }
   1102 
   1103 static struct ast_expression *
   1104 parse_assertion_expression(struct lexer *lexer, bool is_static)
   1105 {
   1106 	struct ast_expression *exp = mkexpr(&lexer->loc);
   1107 	exp->type = EXPR_ASSERT;
   1108 	exp->assert.is_static = is_static;
   1109 
   1110 	struct token tok;
   1111 	switch (lex(lexer, &tok)) {
   1112 	case T_ASSERT:
   1113 	case T_ABORT:
   1114 		break;
   1115 	default:
   1116 		synerr(&tok, T_ASSERT, T_ABORT, T_EOF);
   1117 	}
   1118 
   1119 	switch (tok.token) {
   1120 	case T_ASSERT:
   1121 		want(lexer, T_LPAREN, &tok);
   1122 		exp->assert.cond = parse_expression(lexer);
   1123 		if (lex(lexer, &tok) == T_COMMA) {
   1124 			exp->assert.message = parse_constant(lexer);
   1125 		} else {
   1126 			unlex(lexer, &tok);
   1127 		}
   1128 		want(lexer, T_RPAREN, &tok);
   1129 		break;
   1130 	case T_ABORT:
   1131 		want(lexer, T_LPAREN, &tok);
   1132 		if (lex(lexer, &tok) != T_RPAREN) {
   1133 			unlex(lexer, &tok);
   1134 			exp->assert.message = parse_constant(lexer);
   1135 			want(lexer, T_RPAREN, &tok);
   1136 		}
   1137 		break;
   1138 	default:
   1139 		assert(0); // Invariant
   1140 	}
   1141 
   1142 	return exp;
   1143 }
   1144 
   1145 static struct ast_expression *
   1146 parse_measurement_expression(struct lexer *lexer)
   1147 {
   1148 	struct ast_expression *exp = mkexpr(&lexer->loc);
   1149 	exp->type = EXPR_MEASURE;
   1150 
   1151 	struct token tok;
   1152 	lex(lexer, &tok);
   1153 
   1154 	want(lexer, T_LPAREN, NULL);
   1155 	switch (tok.token) {
   1156 	case T_ALIGN:
   1157 		exp->measure.op = M_ALIGN;
   1158 		exp->measure.type = parse_type(lexer);
   1159 		break;
   1160 	case T_SIZE:
   1161 		exp->measure.op = M_SIZE;
   1162 		exp->measure.type = parse_type(lexer);
   1163 		break;
   1164 	case T_LEN:
   1165 		exp->measure.op = M_LEN;
   1166 		exp->measure.value = parse_expression(lexer);
   1167 		break;
   1168 	case T_OFFSET:
   1169 		exp->measure.op = M_OFFSET;
   1170 		// Let check error out on non-field-accesses
   1171 		exp->measure.value = parse_expression(lexer);
   1172 		break;
   1173 	default:
   1174 		synerr(&tok, T_SIZE, T_LEN, T_OFFSET, T_EOF);
   1175 	}
   1176 
   1177 	want(lexer, T_RPAREN, NULL);
   1178 	return exp;
   1179 }
   1180 
   1181 static struct ast_expression *
   1182 parse_call_expression(struct lexer *lexer, struct ast_expression *lvalue)
   1183 {
   1184 	struct token tok;
   1185 	want(lexer, T_LPAREN, &tok);
   1186 
   1187 	struct ast_expression *expr = mkexpr(&lexer->loc);
   1188 	expr->type = EXPR_CALL;
   1189 	expr->call.lvalue = lvalue;
   1190 
   1191 	struct ast_call_argument *arg, **next = &expr->call.args;
   1192 	while (lex(lexer, &tok) != T_RPAREN) {
   1193 		unlex(lexer, &tok);
   1194 		arg = *next = xcalloc(1, sizeof(struct ast_call_argument));
   1195 		arg->value = parse_expression(lexer);
   1196 
   1197 		if (lex(lexer, &tok) == T_ELLIPSIS) {
   1198 			arg->variadic = true;
   1199 		} else {
   1200 			unlex(lexer, &tok);
   1201 		}
   1202 
   1203 		switch (lex(lexer, &tok)) {
   1204 		case T_COMMA:
   1205 			break;
   1206 		case T_RPAREN:
   1207 			unlex(lexer, &tok);
   1208 			break;
   1209 		default:
   1210 			synerr(&tok, T_COMMA, T_RPAREN, T_EOF);
   1211 		}
   1212 
   1213 		next = &arg->next;
   1214 	}
   1215 	return expr;
   1216 }
   1217 
   1218 static struct ast_expression *
   1219 parse_index_slice_expression(struct lexer *lexer, struct ast_expression *lvalue)
   1220 {
   1221 	struct ast_expression *exp = mkexpr(&lexer->loc);
   1222 	struct ast_expression *start = NULL, *end = NULL;
   1223 	struct token tok;
   1224 	want(lexer, T_LBRACKET, &tok);
   1225 
   1226 	bool is_slice = false;
   1227 	switch (lex(lexer, &tok)) {
   1228 	case T_SLICE:
   1229 		is_slice = true;
   1230 		break;
   1231 	default:
   1232 		unlex(lexer, &tok);
   1233 		start = parse_expression(lexer);
   1234 		break;
   1235 	}
   1236 
   1237 	switch (lex(lexer, &tok)) {
   1238 	case T_SLICE:
   1239 		is_slice = true;
   1240 		break;
   1241 	case T_RBRACKET:
   1242 		break;
   1243 	default:
   1244 		if (is_slice) {
   1245 			unlex(lexer, &tok);
   1246 			break;
   1247 		}
   1248 		synerr(&tok, T_SLICE, T_RBRACKET, T_EOF);
   1249 		break;
   1250 	}
   1251 
   1252 	if (!is_slice) {
   1253 		exp->type = EXPR_ACCESS;
   1254 		exp->access.type = ACCESS_INDEX;
   1255 		exp->access.array = lvalue;
   1256 		exp->access.index = start;
   1257 		return exp;
   1258 	} else if (tok.token == T_RBRACKET) {
   1259 		unlex(lexer, &tok);
   1260 	}
   1261 
   1262 	switch (lex(lexer, &tok)) {
   1263 	case T_RBRACKET:
   1264 		break;
   1265 	default:
   1266 		unlex(lexer, &tok);
   1267 		end = parse_expression(lexer);
   1268 		want(lexer, T_RBRACKET, &tok);
   1269 		break;
   1270 	}
   1271 
   1272 	exp->type = EXPR_SLICE;
   1273 	exp->slice.object = lvalue;
   1274 	exp->slice.start = start;
   1275 	exp->slice.end = end;
   1276 	return exp;
   1277 }
   1278 
   1279 static struct ast_expression *
   1280 parse_allocation_expression(struct lexer *lexer)
   1281 {
   1282 	struct ast_expression *exp = NULL;
   1283 	struct token tok = {0};
   1284 	switch (lex(lexer, &tok)) {
   1285 	case T_ALLOC:
   1286 		exp = mkexpr(&tok.loc);
   1287 		exp->type = EXPR_ALLOC;
   1288 		exp->alloc.kind = ALLOC_OBJECT;
   1289 		want(lexer, T_LPAREN, NULL);
   1290 		exp->alloc.init = parse_expression(lexer);
   1291 		switch (lex(lexer, &tok)) {
   1292 		case T_COMMA:
   1293 			// alloc(init, cap)
   1294 			exp->alloc.cap = parse_expression(lexer);
   1295 			exp->alloc.kind = ALLOC_WITH_CAP;
   1296 			want(lexer, T_RPAREN, NULL);
   1297 			break;
   1298 		case T_ELLIPSIS:
   1299 			// alloc(init...)
   1300 			exp->alloc.kind = ALLOC_COPY;
   1301 			want(lexer, T_RPAREN, NULL);
   1302 			break;
   1303 		case T_RPAREN:
   1304 			// alloc(init)
   1305 			break;
   1306 		default:
   1307 			synerr(&tok, T_COMMA, T_RPAREN, T_ELLIPSIS, T_EOF);
   1308 		}
   1309 		break;
   1310 	case T_FREE:
   1311 		exp = mkexpr(&tok.loc);
   1312 		exp->type = EXPR_FREE;
   1313 		want(lexer, T_LPAREN, NULL);
   1314 		exp->free.expr = parse_expression(lexer);
   1315 		want(lexer, T_RPAREN, NULL);
   1316 		break;
   1317 	default:
   1318 		assert(0);
   1319 	}
   1320 	return exp;
   1321 }
   1322 
   1323 static struct ast_expression *
   1324 parse_append_insert(struct lexer *lexer, struct location *loc,
   1325 		bool is_static, enum expr_type etype)
   1326 {
   1327 	struct token tok = {0};
   1328 	struct ast_expression *expr = mkexpr(loc);
   1329 	expr->type = etype;
   1330 
   1331 	want(lexer, T_LPAREN, NULL);
   1332 	expr->append.object = parse_object_selector(lexer);
   1333 	want(lexer, T_COMMA, NULL);
   1334 	expr->append.value = parse_expression(lexer);
   1335 	expr->append.is_static = is_static;
   1336 
   1337 	switch (lex(lexer, &tok)) {
   1338 	case T_ELLIPSIS:
   1339 		expr->append.is_multi = true;
   1340 		break;
   1341 	default:
   1342 		unlex(lexer, &tok);
   1343 		break;
   1344 	}
   1345 
   1346 	if (etype == EXPR_INSERT) {
   1347 		synassert_msg(expr->append.object->access.type == ACCESS_INDEX,
   1348 				"expected indexing expression", &tok);
   1349 		want(lexer, T_RPAREN, NULL);
   1350 		return expr;
   1351 	}
   1352 
   1353 	switch (lex(lexer, &tok)) {
   1354 	case T_RPAREN:
   1355 		// This space deliberately left blank
   1356 		break;
   1357 	case T_COMMA:
   1358 		expr->append.length = parse_expression(lexer);
   1359 		want(lexer, T_RPAREN, NULL);
   1360 		break;
   1361 	default:
   1362 		synerr(&tok, T_RPAREN, T_COMMA, T_EOF);
   1363 	}
   1364 
   1365 	return expr;
   1366 }
   1367 
   1368 static struct ast_expression *
   1369 parse_delete(struct lexer *lexer, struct location *loc, bool is_static)
   1370 {
   1371 	struct ast_expression *exp = mkexpr(loc);
   1372 	exp->type = EXPR_DELETE;
   1373 	want(lexer, T_LPAREN, NULL);
   1374 	exp->delete.expr = parse_expression(lexer);
   1375 	exp->delete.is_static = is_static;
   1376 	want(lexer, T_RPAREN, NULL);
   1377 	return exp;
   1378 }
   1379 
   1380 static struct ast_expression *
   1381 parse_slice_mutation(struct lexer *lexer, bool is_static)
   1382 {
   1383 	struct token tok = {0};
   1384 	switch (lex(lexer, &tok)) {
   1385 	case T_APPEND:
   1386 	case T_INSERT:
   1387 		return parse_append_insert(lexer, &tok.loc, is_static,
   1388 			tok.token == T_APPEND ? EXPR_APPEND : EXPR_INSERT);
   1389 	case T_DELETE:
   1390 		return parse_delete(lexer, &tok.loc, is_static);
   1391 	default:
   1392 		abort(); // Invariant
   1393 	}
   1394 }
   1395 
   1396 static struct ast_expression *
   1397 parse_static_expression(struct lexer *lexer, bool allowbinding)
   1398 {
   1399 	struct token tok = {0};
   1400 	switch (lex(lexer, &tok)) {
   1401 	case T_LET:
   1402 	case T_CONST:
   1403 		synassert(allowbinding, &tok, T_ABORT, T_ASSERT, T_APPEND,
   1404 			T_INSERT, T_DELETE, T_EOF);
   1405 		unlex(lexer, &tok);
   1406 		return parse_binding_list(lexer, true);
   1407 	case T_ABORT:
   1408 	case T_ASSERT:
   1409 		unlex(lexer, &tok);
   1410 		return parse_assertion_expression(lexer, true);
   1411 	case T_APPEND:
   1412 	case T_INSERT:
   1413 	case T_DELETE:
   1414 		unlex(lexer, &tok);
   1415 		return parse_slice_mutation(lexer, true);
   1416 	default:
   1417 		if (allowbinding) {
   1418 			synerr(&tok, T_LET, T_CONST, T_ABORT,
   1419 				T_ASSERT, T_APPEND, T_INSERT, T_DELETE, T_EOF);
   1420 		} else {
   1421 			synerr(&tok, T_ABORT, T_ASSERT, T_APPEND,
   1422 				T_INSERT, T_DELETE, T_EOF);
   1423 		}
   1424 	}
   1425 	assert(0); // Unreachable
   1426 }
   1427 
   1428 static struct ast_expression *
   1429 parse_postfix_expression(struct lexer *lexer, struct ast_expression *lvalue)
   1430 {
   1431 	if (lvalue == NULL) {
   1432 		lvalue = parse_plain_expression(lexer);
   1433 	}
   1434 
   1435 	struct token tok;
   1436 	struct ast_expression *exp;
   1437 	switch (lex(lexer, &tok)) {
   1438 	case T_LPAREN:
   1439 		unlex(lexer, &tok);
   1440 		lvalue = parse_call_expression(lexer, lvalue);
   1441 		break;
   1442 	case T_DOT:
   1443 		exp = mkexpr(&lexer->loc);
   1444 		exp->type = EXPR_ACCESS;
   1445 
   1446 		switch (lex(lexer, &tok)) {
   1447 		case T_NAME:
   1448 			exp->access.type = ACCESS_FIELD;
   1449 			exp->access._struct = lvalue;
   1450 			exp->access.field = tok.name;
   1451 			break;
   1452 		case T_LITERAL:
   1453 			exp->access.type = ACCESS_TUPLE;
   1454 			exp->access.tuple = lvalue;
   1455 			unlex(lexer, &tok);
   1456 			exp->access.value = parse_constant(lexer);
   1457 			break;
   1458 		default:
   1459 			synerr(&tok, T_NAME, T_LITERAL, T_EOF);
   1460 		}
   1461 
   1462 		lvalue = exp;
   1463 		break;
   1464 	case T_LBRACKET:
   1465 		unlex(lexer, &tok);
   1466 		lvalue = parse_index_slice_expression(lexer, lvalue);
   1467 		break;
   1468 	case T_QUESTION:
   1469 	case T_LNOT:
   1470 		exp = mkexpr(&lexer->loc);
   1471 		exp->type = EXPR_PROPAGATE;
   1472 		exp->propagate.value = lvalue;
   1473 		exp->propagate.abort = tok.token == T_LNOT;
   1474 		lvalue = exp;
   1475 		break;
   1476 	default:
   1477 		unlex(lexer, &tok);
   1478 		return lvalue;
   1479 	}
   1480 
   1481 	return parse_postfix_expression(lexer, lvalue);
   1482 }
   1483 
   1484 static enum unarithm_operator
   1485 unop_for_token(enum lexical_token tok)
   1486 {
   1487 	switch (tok) {
   1488 	case T_PLUS:	// +
   1489 		return UN_PLUS;
   1490 	case T_MINUS:	// -
   1491 		return UN_MINUS;
   1492 	case T_BNOT:	// ~
   1493 		return UN_BNOT;
   1494 	case T_LNOT:	// !
   1495 		return UN_LNOT;
   1496 	case T_TIMES:	// *
   1497 		return UN_DEREF;
   1498 	case T_BAND:	// &
   1499 		return UN_ADDRESS;
   1500 	default:
   1501 		assert(0); // Invariant
   1502 	}
   1503 	assert(0); // Unreachable
   1504 }
   1505 
   1506 static struct ast_expression *
   1507 parse_object_selector(struct lexer *lexer)
   1508 {
   1509 	struct token tok;
   1510 	lex(lexer, &tok);
   1511 	unlex(lexer, &tok);
   1512 	struct ast_expression *exp = parse_postfix_expression(lexer, NULL);
   1513 	synassert_msg(exp->type == EXPR_ACCESS, "expected object", &tok);
   1514 	return exp;
   1515 }
   1516 
   1517 static struct ast_expression *
   1518 parse_va_expression(struct lexer *lexer)
   1519 {
   1520 	struct ast_expression *expr;
   1521 	struct token tok;
   1522 	switch (lex(lexer, &tok)) {
   1523 	case T_VASTART:
   1524 		expr = mkexpr(&lexer->loc);
   1525 		expr->type = EXPR_VASTART;
   1526 		want(lexer, T_LPAREN, NULL);
   1527 		want(lexer, T_RPAREN, NULL);
   1528 		return expr;
   1529 	case T_VAARG:
   1530 		expr = mkexpr(&lexer->loc);
   1531 		expr->type = EXPR_VAARG;
   1532 		want(lexer, T_LPAREN, NULL);
   1533 		expr->vaarg.ap = parse_object_selector(lexer);
   1534 		want(lexer, T_RPAREN, NULL);
   1535 		return expr;
   1536 	case T_VAEND:
   1537 		expr = mkexpr(&lexer->loc);
   1538 		expr->type = EXPR_VAEND;
   1539 		want(lexer, T_LPAREN, NULL);
   1540 		expr->vaarg.ap = parse_object_selector(lexer);
   1541 		want(lexer, T_RPAREN, NULL);
   1542 		return expr;
   1543 	default:
   1544 		assert(0);
   1545 	}
   1546 }
   1547 
   1548 static struct ast_expression *
   1549 parse_builtin_expression(struct lexer *lexer)
   1550 {
   1551 	struct token tok;
   1552 	switch (lex(lexer, &tok)) {
   1553 	case T_ALLOC:
   1554 	case T_FREE:
   1555 		unlex(lexer, &tok);
   1556 		return parse_allocation_expression(lexer);
   1557 	case T_APPEND:
   1558 	case T_DELETE:
   1559 	case T_INSERT:
   1560 		unlex(lexer, &tok);
   1561 		return parse_slice_mutation(lexer, false);
   1562 	case T_STATIC:
   1563 		return parse_static_expression(lexer, false);
   1564 	case T_ABORT:
   1565 	case T_ASSERT:
   1566 		unlex(lexer, &tok);
   1567 		return parse_assertion_expression(lexer, false);
   1568 	case T_ALIGN:
   1569 	case T_SIZE:
   1570 	case T_LEN:
   1571 	case T_OFFSET:
   1572 		unlex(lexer, &tok);
   1573 		return parse_measurement_expression(lexer);
   1574 	case T_VAARG:
   1575 	case T_VAEND:
   1576 	case T_VASTART:
   1577 		unlex(lexer, &tok);
   1578 		return parse_va_expression(lexer);
   1579 	default:
   1580 		unlex(lexer, &tok);
   1581 		break;
   1582 	}
   1583 	return parse_postfix_expression(lexer, NULL);
   1584 }
   1585 
   1586 static struct ast_expression *
   1587 parse_unary_expression(struct lexer *lexer)
   1588 {
   1589 	struct token tok;
   1590 	struct ast_expression *exp;
   1591 	switch (lex(lexer, &tok)) {
   1592 	case T_PLUS:	// +
   1593 	case T_MINUS:	// -
   1594 	case T_BNOT:	// ~
   1595 	case T_LNOT:	// !
   1596 	case T_TIMES:	// *
   1597 	case T_BAND:	// &
   1598 		exp = mkexpr(&lexer->loc);
   1599 		exp->type = EXPR_UNARITHM;
   1600 		exp->unarithm.op = unop_for_token(tok.token);
   1601 		exp->unarithm.operand = parse_unary_expression(lexer);
   1602 		return exp;
   1603 	default:
   1604 		unlex(lexer, &tok);
   1605 		return parse_builtin_expression(lexer);
   1606 	}
   1607 }
   1608 
   1609 static struct ast_expression *
   1610 parse_cast_expression(struct lexer *lexer, struct ast_expression *value)
   1611 {
   1612 	if (value == NULL) {
   1613 		value = parse_unary_expression(lexer);
   1614 	}
   1615 	enum cast_kind kind;
   1616 	struct token tok;
   1617 	switch (lex(lexer, &tok)) {
   1618 	case T_COLON:
   1619 		kind = C_CAST;
   1620 		break;
   1621 	case T_AS:
   1622 		kind = C_ASSERTION;
   1623 		break;
   1624 	case T_IS:
   1625 		kind = C_TEST;
   1626 		break;
   1627 	default:
   1628 		unlex(lexer, &tok);
   1629 		return value;
   1630 	}
   1631 
   1632 	struct ast_expression *exp = mkexpr(&lexer->loc);
   1633 	exp->type = EXPR_CAST;
   1634 	exp->cast.kind = kind;
   1635 	exp->cast.value = value;
   1636 	if (lex(lexer, &tok) == T_NULL) {
   1637 		exp->cast.type = mktype(&tok.loc);
   1638 		exp->cast.type->storage = STORAGE_NULL;
   1639 	} else {
   1640 		unlex(lexer, &tok);
   1641 		exp->cast.type = parse_type(lexer);
   1642 	}
   1643 	return parse_cast_expression(lexer, exp);
   1644 }
   1645 
   1646 static int
   1647 precedence(enum lexical_token token)
   1648 {
   1649 	switch (token) {
   1650 	case T_LOR:
   1651 		return 0;
   1652 	case T_LXOR:
   1653 		return 1;
   1654 	case T_LAND:
   1655 		return 2;
   1656 	case T_LEQUAL:
   1657 	case T_NEQUAL:
   1658 		return 3;
   1659 	case T_LESS:
   1660 	case T_LESSEQ:
   1661 	case T_GREATER:
   1662 	case T_GREATEREQ:
   1663 		return 4;
   1664 	case T_BOR:
   1665 		return 5;
   1666 	case T_BXOR:
   1667 		return 6;
   1668 	case T_BAND:
   1669 		return 7;
   1670 	case T_LSHIFT:
   1671 	case T_RSHIFT:
   1672 		return 8;
   1673 	case T_PLUS:
   1674 	case T_MINUS:
   1675 		return 9;
   1676 	case T_TIMES:
   1677 	case T_DIV:
   1678 	case T_MODULO:
   1679 		return 10;
   1680 	default:
   1681 		return -1;
   1682 	}
   1683 	assert(0); // Unreachable
   1684 }
   1685 
   1686 static enum binarithm_operator
   1687 binop_for_token(enum lexical_token tok)
   1688 {
   1689 	switch (tok) {
   1690 	case T_LOR:
   1691 		return BIN_LOR;
   1692 	case T_LAND:
   1693 		return BIN_LAND;
   1694 	case T_LXOR:
   1695 		return BIN_LXOR;
   1696 	case T_BOR:
   1697 		return BIN_BOR;
   1698 	case T_BXOR:
   1699 		return BIN_BXOR;
   1700 	case T_BAND:
   1701 		return BIN_BAND;
   1702 	case T_LEQUAL:
   1703 		return BIN_LEQUAL;
   1704 	case T_NEQUAL:
   1705 		return BIN_NEQUAL;
   1706 	case T_LESS:
   1707 		return BIN_LESS;
   1708 	case T_LESSEQ:
   1709 		return BIN_LESSEQ;
   1710 	case T_GREATER:
   1711 		return BIN_GREATER;
   1712 	case T_GREATEREQ:
   1713 		return BIN_GREATEREQ;
   1714 	case T_LSHIFT:
   1715 		return BIN_LSHIFT;
   1716 	case T_RSHIFT:
   1717 		return BIN_RSHIFT;
   1718 	case T_PLUS:
   1719 		return BIN_PLUS;
   1720 	case T_MINUS:
   1721 		return BIN_MINUS;
   1722 	case T_TIMES:
   1723 		return BIN_TIMES;
   1724 	case T_DIV:
   1725 		return BIN_DIV;
   1726 	case T_MODULO:
   1727 		return BIN_MODULO;
   1728 	default:
   1729 		assert(0); // Invariant
   1730 	}
   1731 	assert(0); // Unreachable
   1732 }
   1733 
   1734 static struct ast_expression *
   1735 parse_bin_expression(struct lexer *lexer, struct ast_expression *lvalue, int i)
   1736 {
   1737 	if (!lvalue) {
   1738 		lvalue = parse_cast_expression(lexer, NULL);
   1739 	}
   1740 
   1741 	struct token tok;
   1742 	lex(lexer, &tok);
   1743 
   1744 	int j;
   1745 	while ((j = precedence(tok.token)) >= i) {
   1746 		enum binarithm_operator op = binop_for_token(tok.token);
   1747 
   1748 		struct ast_expression *rvalue =
   1749 			parse_cast_expression(lexer, NULL);
   1750 		lex(lexer, &tok);
   1751 
   1752 		int k;
   1753 		while ((k = precedence(tok.token)) > j) {
   1754 			unlex(lexer, &tok);
   1755 			rvalue = parse_bin_expression(lexer, rvalue, k);
   1756 			lex(lexer, &tok);
   1757 		}
   1758 
   1759 		struct ast_expression *e = mkexpr(&lexer->loc);
   1760 		e->type = EXPR_BINARITHM;
   1761 		e->binarithm.op = op;
   1762 		e->binarithm.lvalue = lvalue;
   1763 		e->binarithm.rvalue = rvalue;
   1764 		lvalue = e;
   1765 	}
   1766 
   1767 	unlex(lexer, &tok);
   1768 	return lvalue;
   1769 }
   1770 
   1771 static struct ast_expression *
   1772 parse_if_expression(struct lexer *lexer)
   1773 {
   1774 	struct ast_expression *exp = mkexpr(&lexer->loc);
   1775 	exp->type = EXPR_IF;
   1776 
   1777 	struct token tok = {0};
   1778 
   1779 	want(lexer, T_LPAREN, &tok);
   1780 	exp->_if.cond = parse_expression(lexer);
   1781 	want(lexer, T_RPAREN, &tok);
   1782 
   1783 	exp->_if.true_branch = parse_expression(lexer);
   1784 
   1785 	switch (lex(lexer, &tok)) {
   1786 	case T_ELSE:
   1787 		if (lex(lexer, &tok) == T_IF) {
   1788 			exp->_if.false_branch = parse_if_expression(lexer);
   1789 		} else {
   1790 			unlex(lexer, &tok);
   1791 			exp->_if.false_branch = parse_expression(lexer);
   1792 		}
   1793 		break;
   1794 	default:
   1795 		unlex(lexer, &tok);
   1796 		break;
   1797 	}
   1798 	return exp;
   1799 }
   1800 
   1801 static struct ast_expression *
   1802 parse_for_expression(struct lexer *lexer)
   1803 {
   1804 	struct ast_expression *exp = mkexpr(&lexer->loc);
   1805 	exp->type = EXPR_FOR;
   1806 
   1807 	struct token tok = {0};
   1808 	want(lexer, T_FOR, &tok);
   1809 	want(lexer, T_LPAREN, &tok);
   1810 	switch (lex(lexer, &tok)) {
   1811 	case T_LET:
   1812 	case T_CONST:
   1813 		unlex(lexer, &tok);
   1814 		exp->_for.bindings = parse_binding_list(lexer, false);
   1815 		want(lexer, T_SEMICOLON, &tok);
   1816 		exp->_for.cond = parse_expression(lexer);
   1817 		break;
   1818 	case T_STATIC:
   1819 		switch (lex(lexer, &tok)) {
   1820 		case T_LET:
   1821 		case T_CONST:
   1822 			unlex(lexer, &tok);
   1823 			exp->_for.bindings = parse_binding_list(lexer, true);
   1824 			want(lexer, T_SEMICOLON, &tok);
   1825 			exp->_for.cond = parse_expression(lexer);
   1826 			break;
   1827 		default:
   1828 			unlex(lexer, &tok);
   1829 			exp->_for.cond = parse_static_expression(lexer, false);
   1830 			break;
   1831 		}
   1832 		break;
   1833 	default:
   1834 		unlex(lexer, &tok);
   1835 		exp->_for.cond = parse_expression(lexer);
   1836 		break;
   1837 	}
   1838 
   1839 	switch (lex(lexer, &tok)) {
   1840 	case T_SEMICOLON:
   1841 		exp->_for.afterthought = parse_expression(lexer);
   1842 		want(lexer, T_RPAREN, &tok);
   1843 		break;
   1844 	case T_RPAREN:
   1845 		break;
   1846 	default:
   1847 		synerr(&tok, T_SEMICOLON, T_RPAREN, T_EOF);
   1848 	}
   1849 
   1850 	exp->_for.body = parse_expression(lexer);
   1851 	return exp;
   1852 }
   1853 
   1854 static struct ast_case_option *
   1855 parse_case_options(struct lexer *lexer)
   1856 {
   1857 	struct token tok = {0};
   1858 	switch (lex(lexer, &tok)) {
   1859 	case T_ARROW:
   1860 		return NULL; // Default case
   1861 	default:
   1862 		unlex(lexer, &tok);
   1863 		break;
   1864 	}
   1865 
   1866 	bool more = true;
   1867 	struct ast_case_option *opt = xcalloc(1, sizeof(struct ast_case_option));
   1868 	struct ast_case_option *opts = opt;
   1869 	struct ast_case_option **next = &opt->next;
   1870 	while (more) {
   1871 		opt->value = parse_expression(lexer);
   1872 		switch (lex(lexer, &tok)) {
   1873 		case T_COMMA:
   1874 			switch (lex(lexer, &tok)) {
   1875 			case T_ARROW:
   1876 				more = false;
   1877 				break;
   1878 			default:
   1879 				unlex(lexer, &tok);
   1880 				opt = xcalloc(1, sizeof(struct ast_case_option));
   1881 				*next = opt;
   1882 				next = &opt->next;
   1883 				break;
   1884 			}
   1885 			break;
   1886 		case T_ARROW:
   1887 			more = false;
   1888 			break;
   1889 		default:
   1890 			synerr(&tok, T_COMMA, T_ARROW, T_EOF);
   1891 			break;
   1892 		}
   1893 	}
   1894 
   1895 	return opts;
   1896 }
   1897 
   1898 static struct ast_expression *
   1899 parse_switch_expression(struct lexer *lexer)
   1900 {
   1901 	struct ast_expression *exp = mkexpr(&lexer->loc);
   1902 	exp->type = EXPR_SWITCH;
   1903 
   1904 	struct token tok = {0};
   1905 	want(lexer, T_LPAREN, &tok);
   1906 	exp->_switch.value = parse_expression(lexer);
   1907 	want(lexer, T_RPAREN, &tok);
   1908 	want(lexer, T_LBRACE, &tok);
   1909 
   1910 	bool more = true;
   1911 	struct ast_switch_case **next_case = &exp->_switch.cases;
   1912 	while (more) {
   1913 		struct ast_switch_case *_case =
   1914 			*next_case = xcalloc(1, sizeof(struct ast_switch_case));
   1915 		want(lexer, T_CASE, &tok);
   1916 		_case->options = parse_case_options(lexer);
   1917 
   1918 		bool exprs = true;
   1919 		struct ast_expression_list *cur = &_case->exprs;
   1920 		struct ast_expression_list **next = &cur->next;
   1921 		while (exprs) {
   1922 			cur->expr = parse_statement(lexer);
   1923 			want(lexer, T_SEMICOLON, &tok);
   1924 
   1925 			switch (lex(lexer, &tok)) {
   1926 			case T_CASE:
   1927 			case T_RBRACE:
   1928 				exprs = false;
   1929 				break;
   1930 			default:
   1931 				break;
   1932 			}
   1933 			unlex(lexer, &tok);
   1934 
   1935 			if (exprs) {
   1936 				*next = xcalloc(1, sizeof(struct ast_expression_list));
   1937 				cur = *next;
   1938 				next = &cur->next;
   1939 			}
   1940 		}
   1941 
   1942 		switch (lex(lexer, &tok)) {
   1943 		case T_CASE:
   1944 			unlex(lexer, &tok);
   1945 			break;
   1946 		case T_RBRACE:
   1947 			more = false;
   1948 			break;
   1949 		default:
   1950 			synerr(&tok, T_CASE, T_RBRACE, T_EOF);
   1951 		}
   1952 
   1953 		next_case = &_case->next;
   1954 	}
   1955 
   1956 	return exp;
   1957 }
   1958 
   1959 static struct ast_expression *
   1960 parse_match_expression(struct lexer *lexer)
   1961 {
   1962 	struct ast_expression *exp = mkexpr(&lexer->loc);
   1963 	exp->type = EXPR_MATCH;
   1964 
   1965 	struct token tok = {0};
   1966 	want(lexer, T_LPAREN, &tok);
   1967 	exp->match.value = parse_expression(lexer);
   1968 	want(lexer, T_RPAREN, &tok);
   1969 	want(lexer, T_LBRACE, &tok);
   1970 
   1971 	bool more = true;
   1972 	struct ast_match_case **next_case = &exp->match.cases;
   1973 	while (more) {
   1974 		struct ast_match_case *_case =
   1975 			*next_case = xcalloc(1, sizeof(struct ast_match_case));
   1976 		want(lexer, T_CASE, &tok);
   1977 
   1978 		struct ast_type *type = NULL;
   1979 		switch (lex(lexer, &tok)) {
   1980 		case T_LET:
   1981 			want(lexer, T_NAME, &tok);
   1982 			_case->name = tok.name;
   1983 			want(lexer, T_COLON, NULL);
   1984 			_case->type = parse_type(lexer);
   1985 			break;
   1986 		case T_ARROW:
   1987 			// Default case
   1988 			unlex(lexer, &tok);
   1989 			break;
   1990 		case T_NULL:
   1991 			type = mktype(&tok.loc);
   1992 			type->storage = STORAGE_NULL;
   1993 			_case->type = type;
   1994 			break;
   1995 		default:
   1996 			unlex(lexer, &tok);
   1997 			_case->type = parse_type(lexer);
   1998 			break;
   1999 		}
   2000 
   2001 		want(lexer, T_ARROW, &tok);
   2002 
   2003 		bool exprs = true;
   2004 		struct ast_expression_list *cur = &_case->exprs;
   2005 		struct ast_expression_list **next = &cur->next;
   2006 		while (exprs) {
   2007 			cur->expr = parse_statement(lexer);
   2008 			want(lexer, T_SEMICOLON, &tok);
   2009 
   2010 			switch (lex(lexer, &tok)) {
   2011 			case T_CASE:
   2012 			case T_RBRACE:
   2013 				exprs = false;
   2014 				break;
   2015 			default:
   2016 				break;
   2017 			}
   2018 			unlex(lexer, &tok);
   2019 
   2020 			if (exprs) {
   2021 				*next = xcalloc(1, sizeof(struct ast_expression_list));
   2022 				cur = *next;
   2023 				next = &cur->next;
   2024 			}
   2025 		}
   2026 
   2027 		switch (lex(lexer, &tok)) {
   2028 		case T_CASE:
   2029 			unlex(lexer, &tok);
   2030 			break;
   2031 		case T_RBRACE:
   2032 			more = false;
   2033 			break;
   2034 		default:
   2035 			synerr(&tok, T_CASE, T_RBRACE, T_EOF);
   2036 		}
   2037 
   2038 		next_case = &_case->next;
   2039 	}
   2040 	return exp;
   2041 }
   2042 
   2043 static void
   2044 parse_binding_unpack(struct lexer *lexer, struct ast_binding_unpack **next)
   2045 {
   2046 	struct token tok = {0};
   2047 
   2048 	bool more = true;
   2049 	while (more) {
   2050 		char *name = NULL;
   2051 		switch (lex(lexer, &tok)) {
   2052 		case T_NAME:
   2053 			name = tok.name;
   2054 			break;
   2055 		case T_UNDERSCORE:
   2056 			break;
   2057 		default:
   2058 			synerr(&tok, T_NAME, T_UNDERSCORE, T_EOF);
   2059 		}
   2060 
   2061 		struct ast_binding_unpack *new = xcalloc(1, sizeof *new);
   2062 		*next = new;
   2063 		next = &new->next;
   2064 
   2065 		new->name = name;
   2066 
   2067 		switch (lex(lexer, &tok)) {
   2068 		case T_COMMA:
   2069 			break;
   2070 		case T_RPAREN:
   2071 			more = false;
   2072 			break;
   2073 		default:
   2074 			synerr(&tok, T_COMMA, T_RPAREN, T_EOF);
   2075 		}
   2076 	}
   2077 }
   2078 
   2079 static struct ast_expression *
   2080 parse_binding_list(struct lexer *lexer, bool is_static)
   2081 {
   2082 	struct ast_expression *exp = mkexpr(&lexer->loc);
   2083 	exp->type = EXPR_BINDING;
   2084 	unsigned int flags = 0;
   2085 
   2086 	struct token tok = {0};
   2087 	switch (lex(lexer, &tok)) {
   2088 	case T_CONST:
   2089 		flags = TYPE_CONST;
   2090 		// fallthrough
   2091 	case T_LET:
   2092 		// no-op
   2093 		break;
   2094 	default:
   2095 		synerr(&tok, T_LET, T_CONST, T_EOF);
   2096 	}
   2097 
   2098 	struct ast_expression_binding *binding = &exp->binding;
   2099 	struct ast_expression_binding **next = &exp->binding.next;
   2100 
   2101 	bool more = true;
   2102 	while (more) {
   2103 		switch (lex(lexer, &tok)) {
   2104 		case T_NAME:
   2105 			binding->name = tok.name;
   2106 			break;
   2107 		case T_LPAREN:
   2108 			parse_binding_unpack(lexer, &binding->unpack);
   2109 			break;
   2110 		default:
   2111 			synerr(&tok, T_NAME, T_LPAREN, T_EOF);
   2112 		}
   2113 		binding->initializer = mkexpr(&lexer->loc);
   2114 		binding->flags = flags;
   2115 		binding->is_static = is_static;
   2116 
   2117 		switch (lex(lexer, &tok)) {
   2118 		case T_COLON:
   2119 			binding->type = parse_type(lexer);
   2120 			binding->type->flags |= flags;
   2121 			want(lexer, T_EQUAL, &tok);
   2122 			binding->initializer = parse_expression(lexer);
   2123 			break;
   2124 		case T_EQUAL:
   2125 			binding->initializer = parse_expression(lexer);
   2126 			break;
   2127 		default:
   2128 			synerr(&tok, T_COLON, T_EQUAL, T_EOF);
   2129 		}
   2130 
   2131 		switch (lex(lexer, &tok)) {
   2132 		case T_COMMA:
   2133 			*next = xcalloc(1, sizeof(struct ast_expression_binding));
   2134 			binding = *next;
   2135 			next = &binding->next;
   2136 			break;
   2137 		default:
   2138 			unlex(lexer, &tok);
   2139 			more = false;
   2140 			break;
   2141 		}
   2142 	}
   2143 	return exp;
   2144 }
   2145 
   2146 static struct ast_expression *
   2147 parse_assignment(struct lexer *lexer, struct ast_expression *object,
   2148 	enum binarithm_operator op)
   2149 {
   2150 	struct ast_expression *value = parse_expression(lexer);
   2151 	struct ast_expression *expr = mkexpr(&lexer->loc);
   2152 	expr->type = EXPR_ASSIGN;
   2153 	expr->assign.op = op;
   2154 	expr->assign.object = object;
   2155 	expr->assign.value = value;
   2156 	return expr;
   2157 }
   2158 
   2159 static struct ast_expression *
   2160 parse_deferred_expression(struct lexer *lexer)
   2161 {
   2162 	struct ast_expression *exp = mkexpr(&lexer->loc);
   2163 	exp->type = EXPR_DEFER;
   2164 	exp->defer.deferred = parse_expression(lexer);
   2165 	return exp;
   2166 }
   2167 
   2168 static struct ast_expression *
   2169 parse_control_expression(struct lexer *lexer)
   2170 {
   2171 	struct ast_expression *exp = mkexpr(&lexer->loc);
   2172 
   2173 	struct token tok;
   2174 	switch (lex(lexer, &tok)) {
   2175 	case T_BREAK:
   2176 	case T_CONTINUE:
   2177 		exp->type = tok.token == T_BREAK ? EXPR_BREAK : EXPR_CONTINUE;
   2178 		exp->control.label = NULL;
   2179 		switch (lex(lexer, &tok)) {
   2180 		case T_COLON:
   2181 			want(lexer, T_NAME, &tok);
   2182 			exp->control.label = tok.name;
   2183 			break;
   2184 		default:
   2185 			unlex(lexer, &tok);
   2186 			break;
   2187 		}
   2188 		break;
   2189 	case T_RETURN:
   2190 		exp->type = EXPR_RETURN;
   2191 		exp->_return.value = NULL;
   2192 		switch (lex(lexer, &tok)) {
   2193 		case T_SEMICOLON:
   2194 		case T_RPAREN:
   2195 			unlex(lexer, &tok);
   2196 			break;
   2197 		default:
   2198 			unlex(lexer, &tok);
   2199 			exp->_return.value = parse_expression(lexer);
   2200 			break;
   2201 		}
   2202 		break;
   2203 	case T_YIELD:
   2204 		exp->type = EXPR_YIELD;
   2205 		exp->control.value = NULL;
   2206 		switch (lex(lexer, &tok)) {
   2207 		case T_SEMICOLON:
   2208 		case T_RPAREN:
   2209 			unlex(lexer, &tok);
   2210 			break;
   2211 		case T_COLON:
   2212 			want(lexer, T_NAME, &tok);
   2213 			exp->control.label = tok.name;
   2214 			switch (lex(lexer, &tok)) {
   2215 			case T_COMMA:
   2216 				exp->control.value = parse_expression(lexer);
   2217 				break;
   2218 			default:
   2219 				unlex(lexer, &tok);
   2220 				break;
   2221 			}
   2222 			break;
   2223 		default:
   2224 			unlex(lexer, &tok);
   2225 			exp->control.value = parse_expression(lexer);
   2226 			break;
   2227 		}
   2228 		break;
   2229 	default:
   2230 		synerr(&tok,
   2231 			T_BREAK, T_CONTINUE, T_RETURN, T_YIELD, T_EOF);
   2232 	}
   2233 	return exp;
   2234 }
   2235 
   2236 static struct ast_expression *
   2237 parse_compound_expression(struct lexer *lexer)
   2238 {
   2239 	struct ast_expression *exp = mkexpr(&lexer->loc);
   2240 	exp->type = EXPR_COMPOUND;
   2241 
   2242 	struct ast_expression_list *cur = &exp->compound.list;
   2243 	struct ast_expression_list **next = &cur->next;
   2244 
   2245 	struct token tok = {0};
   2246 	switch (lex(lexer, &tok)) {
   2247 	case T_COLON:
   2248 		want(lexer, T_NAME, &tok);
   2249 		exp->compound.label = tok.name;
   2250 		want(lexer, T_LBRACE, &tok);
   2251 		break;
   2252 	case T_LBRACE:
   2253 		break; // no-op
   2254 	default:
   2255 		synerr(&tok, T_LBRACE, T_COLON, T_EOF);
   2256 		break;
   2257 	};
   2258 
   2259 	bool more = true;
   2260 	while (more) {
   2261 		cur->expr = parse_statement(lexer);
   2262 
   2263 		want(lexer, T_SEMICOLON, &tok);
   2264 
   2265 		if (more) {
   2266 			lex(lexer, &tok);
   2267 			if (tok.token == T_RBRACE) {
   2268 				more = false;
   2269 			} else {
   2270 				unlex(lexer, &tok);
   2271 				*next = xcalloc(1, sizeof(struct ast_expression_list));
   2272 				cur = *next;
   2273 				next = &cur->next;
   2274 			}
   2275 		} else {
   2276 			want(lexer, T_RBRACE, &tok);
   2277 		}
   2278 	}
   2279 	return exp;
   2280 }
   2281 
   2282 struct ast_expression *
   2283 parse_expression(struct lexer *lexer)
   2284 {
   2285 	// This is one of the more complicated non-terminals to parse.
   2286 	struct token tok;
   2287 	bool indirect = false;
   2288 	switch (lex(lexer, &tok)) {
   2289 	case T_TIMES: // *ptr = value (or unary-expression)
   2290 		indirect = true;
   2291 		break;
   2292 	default:
   2293 		unlex(lexer, &tok);
   2294 		break;
   2295 	}
   2296 
   2297 	struct ast_expression *value;
   2298 	switch (lex(lexer, &tok)) {
   2299 	case T_STATIC:
   2300 		return parse_static_expression(lexer, false);
   2301 	case T_BREAK:
   2302 	case T_CONTINUE:
   2303 	case T_RETURN:
   2304 	case T_YIELD:
   2305 	case T_FOR:
   2306 	case T_IF:
   2307 	case T_LBRACE:
   2308 	case T_MATCH:
   2309 	case T_SWITCH:
   2310 	case T_COLON:
   2311 		switch (tok.token) {
   2312 		case T_BREAK:
   2313 		case T_CONTINUE:
   2314 		case T_RETURN:
   2315 		case T_YIELD:
   2316 			unlex(lexer, &tok);
   2317 			value = parse_control_expression(lexer);
   2318 			break;
   2319 		case T_FOR:
   2320 			unlex(lexer, &tok);
   2321 			value = parse_for_expression(lexer);
   2322 			break;
   2323 		case T_IF:
   2324 			value = parse_if_expression(lexer);
   2325 			break;
   2326 		case T_LBRACE:
   2327 		case T_COLON:
   2328 			unlex(lexer, &tok);
   2329 			value = parse_compound_expression(lexer);
   2330 			value = parse_cast_expression(lexer, value);
   2331 			value = parse_bin_expression(lexer, value, 0);
   2332 			break;
   2333 		case T_MATCH:
   2334 			value = parse_match_expression(lexer);
   2335 			value = parse_cast_expression(lexer, value);
   2336 			value = parse_bin_expression(lexer, value, 0);
   2337 			break;
   2338 		case T_SWITCH:
   2339 			value = parse_switch_expression(lexer);
   2340 			value = parse_cast_expression(lexer, value);
   2341 			value = parse_bin_expression(lexer, value, 0);
   2342 			break;
   2343 		default:
   2344 			assert(0);
   2345 		}
   2346 		if (indirect) {
   2347 			struct ast_expression *deref = mkexpr(&value->loc);
   2348 			deref->type = EXPR_UNARITHM;
   2349 			deref->unarithm.op = UN_DEREF;
   2350 			deref->unarithm.operand = value;
   2351 			return deref;
   2352 		}
   2353 		return value;
   2354 	default:
   2355 		unlex(lexer, &tok);
   2356 		value = parse_unary_expression(lexer);
   2357 		if (!indirect && value->type != EXPR_ACCESS
   2358 				&& value->type != EXPR_SLICE) {
   2359 			value = parse_cast_expression(lexer, value);
   2360 			return parse_bin_expression(lexer, value, 0);
   2361 		}
   2362 		// Is possible object-selector, try for assignment
   2363 		break;
   2364 	}
   2365 
   2366 	if (indirect) {
   2367 		struct ast_expression *deref = mkexpr(&value->loc);
   2368 		deref->type = EXPR_UNARITHM;
   2369 		deref->unarithm.op = UN_DEREF;
   2370 		deref->unarithm.operand = value;
   2371 		value = deref;
   2372 	}
   2373 
   2374 	switch (lex(lexer, &tok)) {
   2375 	case T_EQUAL:
   2376 		return parse_assignment(lexer, value, BIN_LEQUAL);
   2377 	case T_BANDEQ:
   2378 		return parse_assignment(lexer, value, BIN_BAND);
   2379 	case T_LANDEQ:
   2380 		return parse_assignment(lexer, value, BIN_LAND);
   2381 	case T_DIVEQ:
   2382 		return parse_assignment(lexer, value, BIN_DIV);
   2383 	case T_LSHIFTEQ:
   2384 		return parse_assignment(lexer, value, BIN_LSHIFT);
   2385 	case T_MINUSEQ:
   2386 		return parse_assignment(lexer, value, BIN_MINUS);
   2387 	case T_MODEQ:
   2388 		return parse_assignment(lexer, value, BIN_MODULO);
   2389 	case T_BOREQ:
   2390 		return parse_assignment(lexer, value, BIN_BOR);
   2391 	case T_LOREQ:
   2392 		return parse_assignment(lexer, value, BIN_LOR);
   2393 	case T_PLUSEQ:
   2394 		return parse_assignment(lexer, value, BIN_PLUS);
   2395 	case T_RSHIFTEQ:
   2396 		return parse_assignment(lexer, value, BIN_RSHIFT);
   2397 	case T_TIMESEQ:
   2398 		return parse_assignment(lexer, value, BIN_TIMES);
   2399 	case T_BXOREQ:
   2400 		return parse_assignment(lexer, value, BIN_BXOR);
   2401 	case T_LXOREQ:
   2402 		return parse_assignment(lexer, value, BIN_LXOR);
   2403 	default:
   2404 		unlex(lexer, &tok);
   2405 		value = parse_cast_expression(lexer, value);
   2406 		value = parse_bin_expression(lexer, value, 0);
   2407 		return value;
   2408 	}
   2409 }
   2410 
   2411 struct ast_expression *
   2412 parse_statement(struct lexer *lexer)
   2413 {
   2414 	struct token tok;
   2415 	switch (lex(lexer, &tok)) {
   2416 	case T_LET:
   2417 	case T_CONST:
   2418 		unlex(lexer, &tok);
   2419 		return parse_binding_list(lexer, false);
   2420 	case T_STATIC:
   2421 		return parse_static_expression(lexer, true);
   2422 	case T_DEFER:
   2423 		return parse_deferred_expression(lexer);
   2424 	default:
   2425 		unlex(lexer, &tok);
   2426 		return parse_expression(lexer);
   2427 	}
   2428 }
   2429 
   2430 static char *
   2431 parse_attr_symbol(struct lexer *lexer)
   2432 {
   2433 	struct token tok = {0};
   2434 	want(lexer, T_LPAREN, NULL);
   2435 	want(lexer, T_LITERAL, &tok);
   2436 	synassert_msg(tok.storage == STORAGE_STRING,
   2437 		"expected string literal", &tok);
   2438 	for (size_t i = 0; i < tok.string.len; i++) {
   2439 		uint32_t c = tok.string.value[i];
   2440 		synassert_msg(c <= 0x7F && (isalnum(c) || c == '_' || c == '$'
   2441 			|| c == '.'), "invalid symbol", &tok);
   2442 		synassert_msg(i != 0 || (!isdigit(c) && c != '$'),
   2443 			"invalid symbol", &tok);
   2444 	}
   2445 	want(lexer, T_RPAREN, NULL);
   2446 	return tok.string.value;
   2447 }
   2448 
   2449 static void
   2450 parse_global_decl(struct lexer *lexer, enum lexical_token mode,
   2451 		struct ast_global_decl *decl)
   2452 {
   2453 	struct token tok = {0};
   2454 	struct ast_global_decl *i = decl;
   2455 	assert(mode == T_LET || mode == T_CONST || mode == T_DEF);
   2456 	bool more = true;
   2457 	while (more) {
   2458 		if (mode == T_LET || mode == T_CONST) {
   2459 			switch (lex(lexer, &tok)) {
   2460 			case T_ATTR_SYMBOL:
   2461 				i->symbol = parse_attr_symbol(lexer);
   2462 				break;
   2463 			default:
   2464 				unlex(lexer, &tok);
   2465 				break;
   2466 			}
   2467 			switch (lex(lexer, &tok)) {
   2468 			case T_ATTR_THREADLOCAL:
   2469 				i->threadlocal = true;
   2470 				break;
   2471 			default:
   2472 				unlex(lexer, &tok);
   2473 				break;
   2474 			}
   2475 		}
   2476 		parse_identifier(lexer, &i->ident, false);
   2477 		if (lex(lexer, &tok) == T_COLON) {
   2478 			i->type = parse_type(lexer);
   2479 			if (mode == T_CONST) {
   2480 				i->type->flags |= TYPE_CONST;
   2481 			}
   2482 		} else {
   2483 			unlex(lexer, &tok);
   2484 		}
   2485 
   2486 		if (mode == T_DEF) {
   2487 			want(lexer, T_EQUAL, NULL);
   2488 			i->init = parse_expression(lexer);
   2489 		} else if (lex(lexer, &tok) == T_EQUAL) {
   2490 			i->init = parse_expression(lexer);
   2491 		} else {
   2492 			unlex(lexer, &tok);
   2493 		}
   2494 
   2495 		switch (lex(lexer, &tok)) {
   2496 		case T_COMMA:
   2497 			lex(lexer, &tok);
   2498 			if (tok.token == T_NAME
   2499 					|| tok.token == T_ATTR_SYMBOL) {
   2500 				i->next = xcalloc(1, sizeof(struct ast_global_decl));
   2501 				i = i->next;
   2502 				unlex(lexer, &tok);
   2503 				break;
   2504 			}
   2505 			/* fallthrough */
   2506 		default:
   2507 			more = false;
   2508 			unlex(lexer, &tok);
   2509 			break;
   2510 		}
   2511 	}
   2512 }
   2513 
   2514 static void
   2515 parse_type_decl(struct lexer *lexer, struct ast_type_decl *decl)
   2516 {
   2517 	struct token tok = {0};
   2518 	struct ast_type_decl *i = decl;
   2519 	bool more = true;
   2520 	while (more) {
   2521 		parse_identifier(lexer, &i->ident, false);
   2522 		want(lexer, T_EQUAL, NULL);
   2523 		switch (lex(lexer, &tok)) {
   2524 		case T_ENUM:
   2525 			i->type = parse_enum_type(&i->ident, lexer);
   2526 			break;
   2527 		default:
   2528 			unlex(lexer, &tok);
   2529 			i->type = parse_type(lexer);
   2530 		}
   2531 		switch (lex(lexer, &tok)) {
   2532 		case T_COMMA:
   2533 			if (lex(lexer, &tok) == T_NAME) {
   2534 				i->next = xcalloc(1, sizeof(struct ast_type_decl));
   2535 				i = i->next;
   2536 				unlex(lexer, &tok);
   2537 				break;
   2538 			}
   2539 			/* fallthrough */
   2540 		default:
   2541 			more = false;
   2542 			unlex(lexer, &tok);
   2543 			break;
   2544 		}
   2545 	}
   2546 }
   2547 
   2548 static void
   2549 parse_fn_decl(struct lexer *lexer, struct ast_function_decl *decl)
   2550 {
   2551 	struct token tok = {0};
   2552 	bool more = true;
   2553 	bool _noreturn = false;
   2554 	while (more) {
   2555 		switch (lex(lexer, &tok)) {
   2556 		case T_ATTR_FINI:
   2557 			decl->flags |= FN_FINI;
   2558 			break;
   2559 		case T_ATTR_INIT:
   2560 			decl->flags |= FN_INIT;
   2561 			break;
   2562 		case T_ATTR_SYMBOL:
   2563 			decl->symbol = parse_attr_symbol(lexer);
   2564 			break;
   2565 		case T_ATTR_TEST:
   2566 			decl->flags |= FN_TEST;
   2567 			break;
   2568 		case T_ATTR_NORETURN:
   2569 			_noreturn = true;
   2570 			break;
   2571 		default:
   2572 			more = false;
   2573 			unlex(lexer, &tok);
   2574 			break;
   2575 		}
   2576 	}
   2577 	want(lexer, T_FN, NULL);
   2578 	parse_identifier(lexer, &decl->ident, false);
   2579 	parse_prototype(lexer, &decl->prototype);
   2580 	if (_noreturn) {
   2581 		decl->prototype.flags |= FN_NORETURN;
   2582 	}
   2583 
   2584 	switch (lex(lexer, &tok)) {
   2585 	case T_EQUAL:
   2586 		decl->body = parse_expression(lexer);
   2587 		break;
   2588 	case T_SEMICOLON:
   2589 		unlex(lexer, &tok);
   2590 		decl->body = NULL; // Prototype
   2591 		break;
   2592 	default:
   2593 		synerr(&tok, T_EQUAL, T_SEMICOLON, T_EOF);
   2594 	}
   2595 }
   2596 
   2597 static void
   2598 parse_decl(struct lexer *lexer, struct ast_decl *decl)
   2599 {
   2600 	struct token tok = {0};
   2601 	decl->loc = lexer->loc;
   2602 	switch (lex(lexer, &tok)) {
   2603 	case T_CONST:
   2604 	case T_LET:
   2605 		decl->decl_type = AST_DECL_GLOBAL;
   2606 		parse_global_decl(lexer, tok.token, &decl->global);
   2607 		break;
   2608 	case T_DEF:
   2609 		decl->decl_type = AST_DECL_CONST;
   2610 		parse_global_decl(lexer, tok.token, &decl->constant);
   2611 		break;
   2612 	case T_TYPE:
   2613 		decl->decl_type = AST_DECL_TYPE;
   2614 		parse_type_decl(lexer, &decl->type);
   2615 		break;
   2616 	default:
   2617 		unlex(lexer, &tok);
   2618 		decl->decl_type = AST_DECL_FUNC;
   2619 		parse_fn_decl(lexer, &decl->function);
   2620 		break;
   2621 	}
   2622 }
   2623 
   2624 static void
   2625 parse_decls(struct lexer *lexer, struct ast_decls **decls)
   2626 {
   2627 	struct token tok = {0};
   2628 	struct ast_decls **next = decls;
   2629 	while (tok.token != T_EOF) {
   2630 		struct ast_decls *decl = *next =
   2631 			xcalloc(1, sizeof(struct ast_decls));
   2632 		switch (lex(lexer, &tok)) {
   2633 		case T_EXPORT:
   2634 			decl->decl.exported = true;
   2635 			break;
   2636 		default:
   2637 			unlex(lexer, &tok);
   2638 			break;
   2639 		}
   2640 		if (tok.token == T_EOF) {
   2641 			break;
   2642 		}
   2643 		parse_decl(lexer, &decl->decl);
   2644 		next = &decl->next;
   2645 		want(lexer, T_SEMICOLON, NULL);
   2646 		if (lex(lexer, &tok) != T_EOF) {
   2647 			unlex(lexer, &tok);
   2648 		}
   2649 	}
   2650 	free(*next);
   2651 	*next = 0;
   2652 }
   2653 
   2654 void
   2655 parse(struct lexer *lexer, struct ast_subunit *subunit)
   2656 {
   2657 	parse_imports(lexer, subunit);
   2658 	parse_decls(lexer, &subunit->decls);
   2659 	want(lexer, T_EOF, NULL);
   2660 }