Skip to content
← Back

src/syntax/parsers/definitions/member.ghul

1
namespace Syntax.Parsers.Definitions is
2
use IO.Std
3
use Ghul.Disposable
4
5
use Source
6
use Logging
7
8
use Ghul.Pipes
9
10
class MEMBER(
11
function_parser: Parser[Trees.Definitions.FUNCTION],
12
property_parser: Parser[Trees.Definitions.PROPERTY],
13
indexer_parser: Parser[Trees.Definitions.INDEXER],
14
pragma_parser: Parser[Trees.Definitions.PRAGMA],
15
super_call_parser: Parser[Trees.Definitions.SUPER_CALL]
16
): Base[Trees.Definitions.Definition] is
17
description: string => "member"
18
19
expected_tokens: Collections.Iterable[Lexical.TOKEN] => property_tokens
20
21
property_tokens: Collections.List[Lexical.TOKEN]
22
23
super()
24
25
init(..) is
26
property_tokens = Collections.LIST([Lexical.TOKEN.COLON, Lexical.TOKEN.ASSIGN, Lexical.TOKEN.ARROW_FAT, Lexical.TOKEN.SEMICOLON])
27
si
28
29
parse(context: CONTEXT) -> Trees.Definitions.Definition? is
30
if context.current.token == Lexical.TOKEN.SQUARE_OPEN then
31
return indexer_parser.parse(context)
32
elif context.current.token == Lexical.TOKEN.AT then
33
return pragma_parser.parse(context)
34
elif context.current.token == Lexical.TOKEN.SUPER then
35
return super_call_parser.parse(context)
36
fi
37
38
let use tokenizer_snapshot = context.tokenizer_speculate_then_commit()
39
40
context.next_token()
41
42
if
43
property_tokens |> any(t => t == context.current.token) \/
44
context.at_inferred_terminator
45
then
46
tokenizer_snapshot.backtrack()
47
48
return property_parser.parse(context)
49
elif
50
context.current.token == Lexical.TOKEN.SQUARE_OPEN \/
51
context.current.token == Lexical.TOKEN.PAREN_OPEN
52
then
53
tokenizer_snapshot.backtrack()
54
55
return function_parser.parse(context)
56
fi
57
58
// A damaged function header, `measure) -> int is`, reaches a
59
// body opener on its own line. The property parser reports the
60
// damage once and keeps the body, where anything else would read
61
// each of its statements as a member.
62
if !context.current.first_on_line /\ _reaches_body_opener(context) then
63
tokenizer_snapshot.backtrack()
64
65
return property_parser.parse(context)
66
fi
67
68
tokenizer_snapshot.commit()
69
70
return other_token(context)
71
si
72
73
_reaches_body_opener(context: CONTEXT) -> bool is
74
let use lookahead = context.tokenizer_speculate_then_commit()
75
let reaches = _scan_to_body_opener(context)
76
77
lookahead.backtrack()
78
79
return reaches
80
si
81
82
_scan_to_body_opener(context: CONTEXT) -> bool is
83
while !context.is_end_of_file /\ !context.current.first_on_line do
84
if context.current.token == Lexical.TOKEN.IS \/ context.current.token == Lexical.TOKEN.ARROW_FAT then
85
return true
86
fi
87
88
context.next_token()
89
od
90
91
return false
92
si
93
si
94
si