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
use std::{
collections::{HashMap, HashSet},
ops::RangeInclusive,
path::{Path, PathBuf},
pin::Pin,
};
use crate::{
lexer::{MultilineString, SpannedToken, Token},
parser::{AstErrors, Construct, Delimited, Node},
};
use crate::typechecker::luaurc::Luaurc;
use crate::typechecker::normalize_path::NormalizePath;
use crate::typechecker::{ReportTypeError, Typechecker, type_error::*};
impl<'a> Typechecker<'a> {
pub(super) fn typecheck_derive<'b>(
&'b self,
body: &'b Construct<'a>,
ast_errors: &'b mut AstErrors,
current_path: &'b Path,
mut luaurc: Option<&'b mut Luaurc>,
dependencies: &'b mut HashSet<PathBuf>,
derives: &'b mut HashMap<PathBuf, RangeInclusive<usize>>,
) -> Pin<Box<dyn Future<Output = ()> + 'b + Send>> {
Box::pin(async move {
match body {
Construct::Node {
node:
Node {
token:
SpannedToken(
span_start,
Token::StringSingle(content)
| Token::StringMulti(MultilineString { content, .. }),
span_end,
),
..
},
} => {
self.resolve_derive(
content,
(*span_start, *span_end),
ast_errors,
current_path,
luaurc.as_deref_mut(),
dependencies,
derives,
)
.await;
}
Construct::Table {
body: Delimited { content, .. },
} => 'table: {
let Some(content) = content.as_ref() else {
break 'table;
};
for item in content {
let datatype = if let Construct::Node {
node:
Node {
token: SpannedToken(_, Token::SemiColon, _),
..
},
..
} = item
{
continue;
} else {
item
};
self.typecheck_derive(
&datatype,
ast_errors,
current_path,
luaurc.as_deref_mut(),
dependencies,
derives,
)
.await;
}
}
Construct::Node {
node:
Node {
token: SpannedToken(_, Token::Comma, _),
..
},
} => (),
_ => ast_errors.report(
TypeError::InvalidType {
expected: Some(ExpectedDatatype::String),
},
self.parsed.range_from_span(body.span()),
),
}
})
}
fn resolve_derive_alias(
&self,
derived_path: &str,
current_path: &Path,
luaurc: Option<&mut Luaurc>,
) -> PathBuf {
let path = 'core: {
let derived_path = PathBuf::from(derived_path).normalize();
let Some(luaurc) = luaurc else {
break 'core derived_path;
};
let mut components = derived_path.components();
let Some(component) = components.next() else {
break 'core derived_path;
};
let component_str = component.as_os_str().to_string_lossy();
if component_str.starts_with("@") {
let alias = &component_str.as_ref()[1..];
luaurc
.dependants
.insert(alias.to_string(), current_path.to_path_buf());
if let Some(alias) = luaurc.aliases.get(alias) {
let mut derived_path = PathBuf::from(alias);
derived_path.push(components);
return derived_path;
} else {
derived_path
}
} else {
derived_path
}
};
current_path.join("../").join(path)
}
async fn resolve_derive(
&self,
content: &str,
span: (usize, usize),
ast_errors: &mut AstErrors,
current_path: &Path,
luaurc: Option<&mut Luaurc>,
dependencies: &mut HashSet<PathBuf>,
derives: &mut HashMap<PathBuf, RangeInclusive<usize>>,
) {
let mut path = self.resolve_derive_alias(content.trim(), current_path, luaurc);
path.set_extension("rsml");
match path.canonicalize() {
Ok(canonicalized) => {
if &canonicalized == current_path {
ast_errors.report(
TypeError::CyclicDerive {
kind: CyclicKind::Internal,
},
self.parsed.range_from_span(span),
);
} else {
dependencies.insert(canonicalized.clone());
derives.insert(canonicalized, span.0..=span.1);
}
}
Err(_) => {
let normalized_path = path.normalize();
ast_errors.report(
TypeError::UnknownDerive {
path: Some(&normalized_path.to_string_lossy()),
},
self.parsed.range_from_span(span),
);
}
}
}
}