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
use super::{error::ParseError, token::Token};
use crate::{
ast::{self, well_known},
builtins::PRELUDE,
};
use chumsky::prelude::*;
pub fn parser() -> impl Parser<Token, ast::Annotation, Error = ParseError> {
recursive(|expression| {
choice((
// Type hole
select! {Token::DiscardName { name } => name}.map_with_span(|name, span| {
ast::Annotation::Hole {
location: span,
name,
}
}),
// Pair
select! {Token::Name { name } if name == PRELUDE => name}
.then_ignore(just(Token::Dot))
.or_not()
.then_ignore(select! {Token::UpName { name } if name == well_known::PAIR => name})
.ignore_then(
expression
.clone()
.separated_by(just(Token::Comma))
.exactly(2)
.delimited_by(just(Token::Less), just(Token::Greater)),
)
.map_with_span(|elems: Vec<ast::Annotation>, span| ast::Annotation::Pair {
location: span,
fst: elems
.first()
.expect("Pair should have exactly 2 elements")
.to_owned()
.into(),
snd: elems
.last()
.expect("Pair should have exactly 2 elements")
.to_owned()
.into(),
}),
// Tuple
expression
.clone()
.separated_by(just(Token::Comma))
.at_least(2)
.allow_trailing()
.delimited_by(
choice((just(Token::LeftParen), just(Token::NewLineLeftParen))),
just(Token::RightParen),
)
.map_with_span(|elems, span| ast::Annotation::Tuple {
location: span,
elems,
}),
// Function
just(Token::Fn)
.ignore_then(
expression
.clone()
.separated_by(just(Token::Comma))
.allow_trailing()
.delimited_by(just(Token::LeftParen), just(Token::RightParen)),
)
.then_ignore(just(Token::RArrow))
.then(expression.clone())
.map_with_span(|(arguments, ret), span| ast::Annotation::Fn {
location: span,
arguments,
ret: Box::new(ret),
}),
// Constructor function
select! {Token::UpName { name } => name}
.then(
expression
.clone()
.separated_by(just(Token::Comma))
.allow_trailing()
.delimited_by(just(Token::Less), just(Token::Greater))
.or_not(),
)
.map_with_span(|(name, arguments), span| ast::Annotation::Constructor {
location: span,
module: None,
name,
arguments: arguments.unwrap_or_default(),
}),
// Constructor Module or type Variable
select! {Token::Name { name } => name}
.then(
just(Token::Dot)
.ignore_then(select! {Token::UpName {name} => name})
.then(
expression
.separated_by(just(Token::Comma))
.allow_trailing()
.delimited_by(just(Token::Less), just(Token::Greater))
.or_not(),
)
.or_not(),
)
.map_with_span(|(mod_name, opt_dot), span| {
if let Some((name, arguments)) = opt_dot {
ast::Annotation::Constructor {
location: span,
module: Some(mod_name),
name,
arguments: arguments.unwrap_or_default(),
}
} else {
// TODO: parse_error(ParseErrorType::NotConstType, SrcSpan { start, end })
ast::Annotation::Var {
location: span,
name: mod_name,
}
}
}),
))
})
}
#[cfg(test)]
mod tests {
use crate::assert_annotation;
#[test]
fn type_annotation_with_module_prefix() {
assert_annotation!("aiken.Option<Int>");
}
}