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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use hemtt_error::{make_source, thiserror, PrettyError, Source};
use hemtt_tokens::{Symbol, Token};
use crate::{
defines::{Defines, DefinitionLibrary},
parse::Rule,
};
#[derive(thiserror::Error, Debug)]
/// Errors that can occur during preprocessing
pub enum Error {
#[error("Expected `{expected:?}`, found `{token:?}`,")]
/// Expected a token, found something else
UnexpectedToken {
/// The [`Token`] that was found
token: Box<Token>,
/// The valid [`Symbol`]s that were expected
expected: Vec<Symbol>,
/// The [`Token`] stack trace
trace: Vec<Token>,
},
#[error("Unexpected EOF at `{token:?}`")]
/// Unexpected end of file
UnexpectedEOF {
/// The token that was found
token: Box<Token>,
},
#[error("Expected `{{ident}}`, found `{token:?}`, ")]
/// Expected an identifier, found something else
ExpectedIdent {
/// The [`Token`] that was found
token: Box<Token>,
/// The [`Token`] stack trace
trace: Vec<Token>,
},
#[error("Unknown directive `{directive:?}`, ")]
/// Unknown directive
UnknownDirective {
/// The [`Token`] that was found
directive: Box<Token>,
/// The [`Token`] stack trace
trace: Vec<Token>,
},
#[error("Function definition has multi-token arguments, `{token:?}`")]
/// Tried to create a [`FunctionDefinition`](crate::context::FunctionDefinition) that has multi-token arguments
///
/// ```cpp
/// #define FUNC(my arg) ...
/// ```
DefineMultiTokenArgument {
/// The [`Token`] that was found
token: Box<Token>,
/// The [`Token`] stack trace
trace: Vec<Token>,
},
#[error("Can not change built-in macros `{token:?}`")]
/// Tried to change a built-in macro
ChangeBuiltin {
/// The [`Token`] that was found
token: Box<Token>,
/// The [`Token`] stack trace
trace: Vec<Token>,
},
#[error("Attempted to use `#if` on a unit or function macro, `{token:?}`")]
/// Tried to use `#if` on a [`Unit`](crate::context::Definition::Unit) or [`FunctionDefinition`](crate::context::Definition::Function)
IfUnitOrFunction {
/// The [`Token`] that was found
token: Box<Token>,
/// The [`Token`] stack trace
trace: Vec<Token>,
},
#[error("Attempted to use `#if` on an undefined macro, `{token:?}`")]
/// Tried to use `#if` on an undefined macro
IfUndefined {
/// The [`Token`] that was found
token: Box<Token>,
/// The [`Token`] stack trace
trace: Vec<Token>,
},
#[error("Function call with incorrect number of arguments, expected `{expected}` got `{got}`. `{token:?}`")]
/// Tried to call a [`FunctionDefinition`](crate::context::FunctionDefinition) with the wrong number of arguments
FunctionCallArgumentCount {
/// The [`Token`] that was found
token: Box<Token>,
/// The number of arguments that were expected
expected: usize,
/// The number of arguments that were found
got: usize,
/// The [`Token`] stack trace
trace: Vec<Token>,
/// The defines at the point of the error
defines: Defines,
},
#[error("Expected Function or Value, found Unit, `{token:?}`")]
/// Tried to use a [`Unit`](crate::context::Definition::Unit) as a function or value
ExpectedFunctionOrValue {
/// The [`Token`] that was found
token: Box<Token>,
/// The [`Token`] stack trace
trace: Vec<Token>,
/// Skipped tokens of Unit
skipped: Vec<Token>,
},
#[error("`#include` was encountered while using `NoResolver`")]
/// Tried to use `#include` with [`NoResolver`](crate::resolver::resolvers::NoResolver)
ResolveWithNoResolver {
/// The [`Token`] stack trace
token: Box<Token>,
/// The [`Token`] stack trace
trace: Vec<Token>,
},
#[error("`#include` target `{target:?}` was not found")]
/// The [`Resolver`](crate::resolver::Resolver) could not find the target
IncludeNotFound {
/// The target that was not found
target: Vec<Token>,
/// The [`Token`] stack trace
trace: Vec<Token>,
},
#[error("IO Error: {0}")]
/// [`std::io::Error`]
Io(Box<std::io::Error>),
#[error("Pest Error: {0}")]
/// [`pest::error::Error`]
Pest(Box<pest::error::Error<Rule>>),
}
impl From<std::io::Error> for Error {
fn from(e: std::io::Error) -> Self {
Self::Io(Box::new(e))
}
}
impl From<pest::error::Error<Rule>> for Error {
fn from(e: pest::error::Error<Rule>) -> Self {
Self::Pest(Box::new(e))
}
}
impl PrettyError for Error {
fn brief(&self) -> String {
match self {
Self::UnexpectedToken {
token,
expected,
trace: _,
} => {
format!(
"Expected `{expected:?}`, found `{symbol:?}`,",
symbol = token.symbol(),
expected = expected
)
}
Self::UnexpectedEOF { token } => {
format!("Unexpected EOF near `{token:?}`,")
}
Self::ExpectedIdent { token, trace: _ } => {
format!(
"Expected `{{ident}}`, found `{symbol:?}`,",
symbol = token.symbol()
)
}
Self::UnknownDirective {
directive,
trace: _,
} => {
format!(
"Unknown directive `{directive:?}`,",
directive = directive.symbol()
)
}
Self::DefineMultiTokenArgument { .. } => {
"Function definition has multi-token arguments".to_string()
}
Self::ChangeBuiltin { token, trace: _ } => {
format!(
"Can not change built-in macros `{symbol:?}`",
symbol = token.symbol()
)
}
Self::IfUnitOrFunction { token, trace: _ } => {
format!(
"Attempted to use `#if` on a unit or function macro, `{symbol:?}`",
symbol = token.symbol()
)
}
Self::IfUndefined { token, trace: _ } => {
format!(
"Attempted to use `#if` on an undefined macro, `{symbol:?}`",
symbol = token.symbol()
)
}
Self::FunctionCallArgumentCount {
token,
expected,
got,
trace: _,
defines: _,
} => {
format!("Function call with incorrect number of arguments, expected `{expected}` got `{got}`. `{symbol:?}`", symbol = token.symbol())
}
Self::ExpectedFunctionOrValue { token, .. } => {
format!(
"Expected Function or Value, found Unit, `{symbol:?}`",
symbol = token.symbol()
)
}
Self::ResolveWithNoResolver { token: _, trace: _ } => {
"`#include` was encountered while using `NoResolver`".to_string()
}
Self::IncludeNotFound { target, trace: _ } => {
let target = target
.iter()
.map(|t| t.symbol().to_string())
.collect::<String>();
format!("`#include` target `{target:?}` was not found")
}
Self::Io(e) => {
format!("IO Error: {e}")
}
Self::Pest(e) => {
format!("Pest Error: {e}")
}
}
}
fn details(&self) -> Option<String> {
match self {
Self::ExpectedFunctionOrValue { skipped, .. } => {
let empty_comment = skipped.iter().all(|t| {
matches!(t.symbol(), Symbol::Comment(_))
|| matches!(t.symbol(), Symbol::Whitespace(_))
});
if empty_comment {
Some(String::from("`#define` with only a comment is considered a Unit (flag). This differs from other preprocessors."))
} else {
None
}
}
_ => None,
}
}
fn help(&self) -> Option<String> {
match self {
Self::ExpectedFunctionOrValue { token, skipped, .. } => {
let empty_comment = skipped.iter().all(|t| {
matches!(t.symbol(), Symbol::Comment(_))
|| matches!(t.symbol(), Symbol::Whitespace(_))
});
if empty_comment {
Some(format!(
"Try using `#define {} ; /* .. */`",
token.symbol().output()
))
} else {
None
}
}
Self::FunctionCallArgumentCount {
token,
expected: _,
got,
trace: _,
defines,
} => {
let Symbol::Word(function) = token.symbol() else {
return None;
};
let did_you_mean = defines.similar_function(function, Some(*got));
Some(format!("Did you mean `{}`?", did_you_mean.join("`, `")))
}
_ => None,
}
}
fn source(&self) -> Option<Box<Source>> {
match self {
Self::UnexpectedToken {
token,
expected,
trace: _,
} => make_source(token, format!("expected one of: {expected:?}"))
.ok()
.map(Box::new),
Self::ExpectedIdent { token, trace: _ } => {
make_source(token, "expected an identifier".to_string())
.ok()
.map(Box::new)
}
Self::UnknownDirective {
directive,
trace: _,
} => make_source(directive, "unknown directive".to_string())
.ok()
.map(Box::new),
Self::DefineMultiTokenArgument { token, trace: _ } => {
make_source(token, "invalid arguments".to_string())
.ok()
.map(Box::new)
}
Self::ChangeBuiltin { token, trace: _ } => {
make_source(token, "build-in macro".to_string())
.ok()
.map(Box::new)
}
Self::IfUnitOrFunction { token, trace: _ } => {
make_source(token, "invalid macro type".to_string())
.ok()
.map(Box::new)
}
Self::IfUndefined { token, trace: _ } => {
make_source(token, "macro is undefined".to_string())
.ok()
.map(Box::new)
}
Self::FunctionCallArgumentCount {
token, expected, ..
} => make_source(token, format!("Expects {expected} arguments"))
.ok()
.map(Box::new),
Self::ExpectedFunctionOrValue { token, .. } => {
make_source(token, "expects function or value".to_string())
.ok()
.map(Box::new)
}
_ => None,
}
}
fn trace(&self) -> Vec<Source> {
let trace = match self {
Self::UnexpectedToken { trace, .. }
| Self::ExpectedIdent { trace, .. }
| Self::UnknownDirective { trace, .. }
| Self::DefineMultiTokenArgument { trace, .. }
| Self::ChangeBuiltin { trace, .. }
| Self::IfUnitOrFunction { trace, .. }
| Self::IfUndefined { trace, .. }
| Self::FunctionCallArgumentCount { trace, .. }
| Self::ExpectedFunctionOrValue { trace, .. }
| Self::ResolveWithNoResolver { trace, .. }
| Self::IncludeNotFound { trace, .. } => trace.clone(),
_ => vec![],
};
trace
.into_iter()
.map(|t| make_source(&t, String::new()).unwrap()) // TODO remove unwrap
.collect()
}
}