Skip to content
← Back

src/syntax/parsers/bodies/body.ghul

1
namespace Syntax.Parsers.Bodies is
2
use IO.Std
3
4
5
class BODY(
6
expression_parser: Parser[Trees.Expressions.Expression],
7
statement_parser: Parser[Trees.Statements.Statement],
8
statement_list_parser: Parser[Trees.Statements.LIST],
9
identifier_qualified_parser: Parser[Trees.Identifiers.Identifier]
10
): Base[Trees.Bodies.Body] is
11
description: string => "function body"
12
13
super()
14
15
init(..) is
16
add_parsers()
17
si
18
19
add_parsers() is
20
add_parser(
21
(context: CONTEXT) is
22
let arrow = context.location
23
context.next_token(Lexical.TOKEN.ARROW_FAT)
24
25
// `=> throw E` stubs out a body that always diverges.
26
// `throw` is a statement, not an expression, so wrap
27
// it the way `if`/`case` bodies are wrapped; the body
28
// takes its type from the declared return type (or
29
// settles void when that is inferred).
30
if context.current.token == Lexical.TOKEN.THROW then
31
let statement = statement_parser.parse(context)!
32
return Trees.Bodies.EXPRESSION(statement.location, Trees.Expressions.STATEMENT(statement.location, statement))
33
fi
34
35
let expression = expression_parser.parse(context)!
36
let body = Trees.Bodies.EXPRESSION(expression.location, expression)
37
body.arrow = arrow
38
return body
39
si,
40
Lexical.TOKEN.ARROW_FAT
41
)
42
43
add_parser(
44
(context: CONTEXT) is
45
let start = context.location
46
context.next_token(Lexical.TOKEN.IS)
47
48
return _block(context, start)
49
si,
50
Lexical.TOKEN.IS
51
)
52
53
add_parser(
54
(context: CONTEXT) is
55
let start = context.location
56
context.next_token(Lexical.TOKEN.INNATE)
57
let identifier = identifier_qualified_parser.parse(context)!
58
return Trees.Bodies.INNATE(start::identifier.location, identifier)
59
si,
60
Lexical.TOKEN.INNATE
61
)
62
si
63
64
// The statements of a block body, once its `is` has been read or found
65
// missing.
66
_block(context: CONTEXT, start: Source.LOCATION) -> Trees.Bodies.Body is
67
let use open = context.open_construct(Lexical.TOKEN.SI)
68
let statement_list = statement_list_parser.parse(context)!
69
let end = context.location
70
context.expect_closer(Lexical.TOKEN.SI)
71
return Trees.Bodies.BLOCK(start::end, statement_list)
72
si
73
74
// No body opener, which a declaration with no body leaves. A header
75
// whose `is` was left out has its body below it instead: a keyword
76
// that can only begin a statement, on a line indented deeper than the
77
// header, cannot begin the next member, so the lines are read as the
78
// body they were written as, with the missing `is` reported, rather
79
// than one at a time as members that are not there. A correct
80
// program never has such a line here, so none reads differently.
81
other_token(context: CONTEXT) -> Trees.Bodies.Body is
82
let current = context.current
83
84
if
85
_starts_only_a_statement(current.token) /\
86
current.first_on_line /\
87
current.location.start_column > context.tokenizer.indent_of_line(context.previous_end.start_line)
88
then
89
context.error(context.location, "syntax error: expected is but found {context.current_token_name}")
90
91
return _block(context, context.location)
92
fi
93
94
return Trees.Bodies.NULL(context.location)
95
si
96
97
_starts_only_a_statement(token: Lexical.TOKEN) -> bool =>
98
token == Lexical.TOKEN.LET \/
99
token == Lexical.TOKEN.IF \/
100
token == Lexical.TOKEN.WHILE \/
101
token == Lexical.TOKEN.FOR \/
102
token == Lexical.TOKEN.TRY \/
103
token == Lexical.TOKEN.RETURN \/
104
token == Lexical.TOKEN.YIELD \/
105
token == Lexical.TOKEN.THROW \/
106
token == Lexical.TOKEN.ASSERT
107
si
108
si