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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
//! The Preprocessor is responsible for evaluating the step 4 of the C++
//! translation.
use std::{
collections::{HashMap, VecDeque},
sync::{Arc, Mutex},
};
use crate::{
grammars::defineast::DefineAst,
utils::{
filemap::FileMap,
parameters::Parameters,
structs::{CompileError, CompileMsg, CompileWarning, FileTokPos},
},
};
use multiset::HashMultiSet;
use super::{
multilexer::MultiLexer,
pretoken::{PreToken, PreprocessingOperator},
};
mod custommacros;
mod defineparse;
mod includer;
mod macroexpand;
mod macroexpression;
#[derive(Debug, PartialEq)]
/// The current #if scope is in status...
enum ScopeStatus {
/// Successful. We return all tokens
Success,
/// Failure. We return no tokens
Failure,
/// Already been successful. Can happen in:
/// Example:
/// #if 1
/// #else
/// <here>
/// #endif
/// We return no tokens, and won't return any in any future scope change.
AlreadySucceeded,
}
#[derive(Debug)]
/// The preprocessor is an iterable object that generates tokens from the
/// original input file. It will report any preprocessing errors as it
/// encounters them, previous to returning the possibly incorrect tokens.
pub struct Preprocessor {
/// Parameters of the compilation
parameters: Arc<Parameters>,
/// The multilexer is the object that will generate the pretokens tokens
multilexer: MultiLexer,
/// The generated preprocessing tokens to be returned. This is a stash, as
/// some tokens may generate more than one token.
generated: VecDeque<FileTokPos<PreToken>>,
/// The generated errors be returned. This is a stash, as some tokens may
/// generate more than one error.
errors: VecDeque<CompileMsg>,
/// The #if scope status. Keeps track of what to do in this scope.
scope: Vec<ScopeStatus>,
/// Current definitions in the preprocessor.
definitions: HashMap<String, DefineAst>,
/// Macros that are disabled in the preprocessor at this point in the evaluation.
disabledMacros: HashMultiSet<String>,
/// The preprocessor is at the start of a line. No tokens have been found
/// yet in this one, except for whitespace.
atStartLine: bool,
}
impl Preprocessor {
/// Creates a new preprocessor from the given parameters, filemap and path
pub fn new(data: (Arc<Parameters>, Arc<Mutex<FileMap>>, &str)) -> Self {
Self {
parameters: data.0,
multilexer: MultiLexer::new((data.1, data.2)),
generated: VecDeque::new(),
errors: VecDeque::new(),
scope: vec![],
definitions: HashMap::new(),
disabledMacros: HashMultiSet::new(),
atStartLine: true,
}
.initCustomMacros()
}
/// Creates a new preprocessor from the given parameters, filemap and path
fn undefineMacro(&mut self, preToken: FileTokPos<PreToken>) {
let vecPrepro = Iterator::take_while(&mut self.multilexer, |pre| {
pre.tokPos.tok != PreToken::Newline
});
match vecPrepro.into_iter().find(|e| !e.tokPos.tok.isWhitespace()) {
None => {
self.errors.push_back(CompileError::from_preTo(
"Expected an identifier to undefine",
&preToken,
));
}
Some(e) => match e.tokPos.tok {
PreToken::Ident(id) => {
if self.definitions.remove(&id).is_none() {
self.errors.push_back(CompileError::from_preTo(
format!("Macro {} is not defined when reached", id),
&preToken,
));
}
}
_ => {
self.errors.push_back(CompileError::from_preTo(
format!("Expected an identifier, found: {}", e.tokPos.tok.to_str()),
&preToken,
));
}
},
}
log::trace!("Macros:");
for defi in self.definitions.values() {
log::trace!("{:?}", defi);
}
return;
}
/// Consume a #ifdef or #ifndef and return the macro name if it exists
fn consumeMacroDef(&mut self, _PreToken: FileTokPos<PreToken>) -> Option<String> {
let identStr;
loop {
let inIdent = self.multilexer.next();
match inIdent {
None => {
return None;
}
Some(ident) => match ident.tokPos.tok {
PreToken::Ident(str) => {
identStr = str;
break;
}
PreToken::Whitespace(_) => {
continue;
}
PreToken::Newline => {
return None;
}
_ => {
self.reachNl();
return None;
}
},
}
}
self.reachNl();
return Some(identStr);
}
/// Reach the nl.
fn reachNl(&mut self) {
loop {
let inIdent = self.multilexer.next();
match inIdent {
None => {
return;
}
Some(ident) => {
if ident.tokPos.tok == PreToken::Newline {
return;
}
}
}
}
}
/// Evaluate the macro in an #ifdef/#ifndef directive
fn evalIfDef(&self, def: Option<String>) -> bool {
if let Some(macroName) = def {
return self.definitions.contains_key(¯oName);
}
return false;
}
/// Encountered a preprocessor directive. Evaluate it accordingly, alering
/// the state of the preprocessor.
fn preprocessorDirective(&mut self, _PreToken: FileTokPos<PreToken>) {
let operation;
let enabledBlock = matches!(self.scope.last(), Some(ScopeStatus::Success) | None);
loop {
match self.multilexer.next() {
None => {
return;
}
Some(tok) => match tok.tokPos.tok {
PreToken::Newline => {
return;
}
PreToken::Whitespace(_) => {}
_ => {
operation = tok;
break;
}
},
}
}
if enabledBlock {
match operation.tokPos.tok.to_str() {
"include" => {
self.multilexer.expectHeader();
match self.consumeMacroInclude(&operation) {
Ok(path) => {
if let Err(err) = self.includeFile(&operation, path) {
self.errors.push_back(err);
}
}
Err(err) => {
self.errors.push_back(err);
}
}
}
"define" => {
self.defineMacro(operation);
}
"undef" => {
self.undefineMacro(operation);
}
"if" => {
let sequenceToEval = self.consumeMacroExpr();
match sequenceToEval {
Err(err) => {
self.errors.push_back(err);
}
Ok(sequenceToEval) => match Self::evalIfScope(sequenceToEval, &operation) {
Ok(true) => {
self.scope.push(ScopeStatus::Success);
}
Ok(false) => {
self.scope.push(ScopeStatus::Failure);
}
Err(err) => {
self.errors.extend(err);
}
},
}
}
"ifdef" => {
let t = self.consumeMacroDef(operation);
self.scope.push(if self.evalIfDef(t) {
ScopeStatus::Success
} else {
ScopeStatus::Failure
});
}
"ifndef" => {
let t = self.consumeMacroDef(operation);
let t2 = if self.evalIfDef(t) {
ScopeStatus::Failure
} else {
ScopeStatus::Success
};
self.scope.push(t2);
}
"elif" | "else" => {
if let Some(scope) = self.scope.last_mut() {
*scope = ScopeStatus::AlreadySucceeded;
self.reachNl(); // TODO: Check empty in else
} else {
self.errors.push_back(CompileError::from_preTo(
"Missmatched preprocessor conditional block",
&operation,
));
}
}
"pragma" => {
self.errors.push_back(CompileError::from_preTo("LMAO, you really expected me to implement this now XD. No worries, we'll get there :D", &operation));
self.reachNl();
}
"endif" => {
if self.scope.is_empty() {
self.errors.push_back(CompileError::from_preTo(
"Missmatched preprocessor conditional block",
&operation,
));
} else {
self.scope.pop();
}
self.reachNl(); // TODO: Check empty
}
"error" => {
let mut msg = String::new();
for t in Iterator::take_while(&mut self.multilexer, |pre| {
pre.tokPos.tok != PreToken::Newline
}) {
msg.push_str(t.tokPos.tok.to_str());
}
self.errors
.push_back(CompileError::from_preTo(msg, &operation));
}
"warning" => {
let mut msg = String::new();
for t in Iterator::take_while(&mut self.multilexer, |pre| {
pre.tokPos.tok != PreToken::Newline
}) {
msg.push_str(t.tokPos.tok.to_str());
}
self.errors
.push_back(CompileWarning::from_preTo(msg, &operation));
}
_ => {
self.errors.push_back(CompileError::from_preTo(
"I do not know this preprocessing expression yet! I'm learning though :)",
&operation,
));
self.reachNl();
}
}
} else if &ScopeStatus::Failure == self.scope.last().unwrap() {
match operation.tokPos.tok.to_str() {
"if" | "ifdef" | "ifndef" => {
self.scope.push(ScopeStatus::AlreadySucceeded);
}
"elif" => {
let sequenceToEval = self.consumeMacroExpr();
match sequenceToEval {
Err(err) => {
self.errors.push_back(err);
}
Ok(sequenceToEval) => {
match Self::evalIfScope(sequenceToEval, &operation) {
Ok(true) => {
let scope = self.scope.last_mut().unwrap();
*scope = ScopeStatus::Success;
}
Ok(false) => {}
Err(err) => {
self.errors.extend(err);
}
};
}
}
}
"else" => {
let scope = self.scope.last_mut().unwrap();
*scope = ScopeStatus::Success;
self.reachNl(); // TODO: Check it is empty
}
"endif" => {
self.reachNl(); // TODO: Check it is empty
self.scope.pop();
}
_ => {
self.reachNl();
}
}
} else if &ScopeStatus::AlreadySucceeded == self.scope.last().unwrap() {
match operation.tokPos.tok.to_str() {
"if" | "ifdef" | "ifndef" => {
self.reachNl();
self.scope.push(ScopeStatus::AlreadySucceeded);
}
"endif" => {
self.reachNl(); // TODO: Check empty
self.scope.pop();
}
_ => {
self.reachNl();
}
}
}
}
/// Consumes a new token generated, and depending on the state of the
/// preprocessor, does something with it. Might consume more tokens from the
/// lexer.
fn consume(&mut self, newToken: FileTokPos<PreToken>) {
loop {
match self.scope.last() {
Some(ScopeStatus::Success) | None => {
if self.atStartLine {
match newToken.tokPos.tok {
PreToken::Whitespace(_) | PreToken::Newline => {
self.generated.push_back(newToken);
break;
}
PreToken::PreprocessingOperator(PreprocessingOperator::Hash) => {
self.preprocessorDirective(newToken);
break;
}
_ => {
self.atStartLine = false;
continue;
}
}
} else {
match newToken.tokPos.tok {
PreToken::EnableMacro(macroName) => {
self.disabledMacros.remove(¯oName);
break;
}
PreToken::DisableMacro(macroName) => {
self.disabledMacros.insert(macroName);
break;
}
PreToken::Newline => {
self.atStartLine = true;
self.generated.push_back(newToken);
break;
}
PreToken::Ident(_) => {
let toks = self.macroExpand(newToken);
match toks {
Ok(toks) => {
self.generated.append(
&mut toks
.into_iter()
.collect::<VecDeque<FileTokPos<PreToken>>>(),
);
}
Err(err) => {
self.errors.push_back(err);
}
};
break;
}
_ => {
self.generated.push_back(newToken);
break;
}
}
}
}
_ => {
if self.atStartLine {
match newToken.tokPos.tok {
PreToken::Whitespace(_) | PreToken::Newline => {
break;
}
PreToken::PreprocessingOperator(PreprocessingOperator::Hash) => {
self.preprocessorDirective(newToken);
break;
}
_ => {
self.atStartLine = false;
break;
}
}
} else {
match newToken.tokPos.tok {
PreToken::Newline => {
self.atStartLine = true;
break;
}
_ => {
break;
}
}
}
}
}
}
}
}
impl Iterator for Preprocessor {
type Item = Result<FileTokPos<PreToken>, CompileMsg>;
fn next(&mut self) -> Option<Self::Item> {
loop {
if let Some(err) = self.errors.pop_front() {
return Some(Err(err));
}
match self.generated.pop_front() {
Some(tok) => {
return Some(Ok(tok));
}
None => match self.multilexer.next() {
None => {
return None;
}
Some(token) => {
self.consume(token);
}
},
}
}
}
}