forked from SpaceManiac/SpacemanDMM
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpreprocessor.rs
More file actions
1199 lines (1105 loc) · 50.5 KB
/
preprocessor.rs
File metadata and controls
1199 lines (1105 loc) · 50.5 KB
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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! The preprocessor.
use std::collections::{HashMap, VecDeque};
use std::{io, fmt};
use std::fs::File;
use std::path::{Path, PathBuf};
use std::borrow::Cow;
use interval_tree::{IntervalTree, range};
use super::{DMError, Location, HasLocation, FileId, Context, Severity};
use super::lexer::*;
use super::docs::{DocComment, DocTarget, DocCollection};
use super::annotation::*;
/// The maximum recursion depth of macro expansion.
const MAX_RECURSION_DEPTH: usize = 32;
// ----------------------------------------------------------------------------
// Macro representation and predefined macros
#[derive(Debug, Clone, PartialEq)]
pub enum Define {
Constant {
subst: Vec<Token>,
docs: DocCollection,
},
Function {
params: Vec<String>,
subst: Vec<Token>,
variadic: bool,
docs: DocCollection,
},
}
impl Define {
/// Get the documentation associated with this define.
pub fn docs(&self) -> &DocCollection {
match self {
Define::Constant { docs, .. } => docs,
Define::Function { docs, .. } => docs,
}
}
/// Get this define's substitution. May be empty.
pub fn substitution(&self) -> &[Token] {
match self {
Define::Constant { subst, .. } => subst,
Define::Function { subst, .. } => subst,
}
}
}
impl fmt::Display for Define {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
let subst = self.substitution();
if subst.is_empty() {
fmt.write_str("(macro)")
} else if subst.len() == 1 {
write!(fmt, "{}", subst[0])
} else {
fmt.write_str("(macro...)")
}
}
}
type InnerDefineHistory = IntervalTree<Location, (String, Define)>;
/// An interval tree representing historic macro definitions.
#[derive(Debug)]
pub struct DefineHistory {
env_file: PathBuf,
last_input_loc: Location,
tree: InnerDefineHistory,
}
impl DefineHistory {
/// Branch a child preprocessor from this preprocessor's historic state at
/// the start of the given file.
pub fn branch_at_file<'ctx2>(&self, file: FileId, context: &'ctx2 Context) -> Preprocessor<'ctx2> {
let location = Location { file, line: 0, column: 0 };
let defines = DefineMap::from_history(self, location);
Preprocessor {
context,
env_file: self.env_file.clone(),
include_stack: Default::default(),
include_locations: Default::default(),
history: Default::default(), // TODO: support branching a second time
defines,
maps: Default::default(),
skins: Default::default(),
scripts: Default::default(),
ifdef_stack: Default::default(), // should be fine
ifdef_history: Default::default(),
last_input_loc: location,
last_printable_input_loc: location,
output: Default::default(),
danger_idents: Default::default(),
docs_in: Default::default(),
docs_out: Default::default(),
in_interp_string: 0,
annotations: None,
}
}
/// Branch a child preprocessor from this preprocessor's current state.
pub fn branch_at_end<'ctx2>(&self, context: &'ctx2 Context) -> Preprocessor<'ctx2> {
Preprocessor {
context,
env_file: self.env_file.clone(),
include_stack: Default::default(),
include_locations: Default::default(),
history: Default::default(), // TODO: support branching a second time
defines: DefineMap::from_history(self, self.last_input_loc),
maps: Default::default(),
skins: Default::default(),
scripts: Default::default(),
ifdef_stack: Default::default(), // should be fine
ifdef_history: Default::default(),
last_input_loc: self.last_input_loc,
last_printable_input_loc: self.last_input_loc,
output: Default::default(),
danger_idents: Default::default(),
docs_in: Default::default(),
docs_out: Default::default(),
in_interp_string: 0,
annotations: None,
}
}
}
impl std::ops::Deref for DefineHistory {
type Target = IntervalTree<Location, (String, Define)>;
fn deref(&self) -> &Self::Target {
&self.tree
}
}
impl std::ops::DerefMut for DefineHistory {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.tree
}
}
/// A map from macro names to their locations and definitions.
///
/// Redefinitions of macros push to a stack, and undefining the macro returns
/// it to the previous entry in the stack, only fully undefining it when the
/// stack is exhausted.
#[derive(Debug, Clone, Default)]
pub struct DefineMap {
inner: HashMap<String, Vec<(Location, Define)>>,
}
impl DefineMap {
pub fn with_builtins() -> DefineMap {
let mut this = Default::default();
super::builtins::default_defines(&mut this);
this
}
/// Returns the number of elements in the map.
#[inline]
pub fn len(&self) -> usize {
self.inner.len()
}
/// Returns true if the map is empty.
#[inline]
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
/// Returns true if the map contains a value for the specified key.
pub fn contains_key(&self, key: &str) -> bool {
self.inner.get(key).map_or(false, |v| !v.is_empty())
}
/// Returns a reference to the value corresponding to the key.
pub fn get(&self, key: &str) -> Option<&(Location, Define)> {
self.inner.get(key).and_then(|v| v.last())
}
/// Inserts a key-value pair into the map.
///
/// Returns `None` if the key was not present, or its most recent location
/// if it was.
pub fn insert(&mut self, key: String, value: (Location, Define)) -> Option<Location> {
let stack = self.inner.entry(key).or_insert_with(Default::default);
let result = stack.last().map(|&(loc, _)| loc);
stack.push(value);
result
}
/// Removes a key from the map, returning the value at the key if the key
/// was previously in the map.
pub fn remove(&mut self, key: &str) -> Option<(Location, Define)> {
let (remove, result);
match self.inner.get_mut(key) {
None => return None,
Some(stack) => {
result = stack.pop();
remove = stack.is_empty();
}
}
if remove {
self.inner.remove(key);
}
result
}
/// Cut a DefineMap from the state of a DefineHistory at the given location.
fn from_history(history: &InnerDefineHistory, location: Location) -> DefineMap {
let mut map = DefineMap::default();
for (range, &(ref name, ref define)) in history.range(range(location, location)) {
map.insert(name.clone(), (range.start, define.clone()));
}
map
}
/*
/// Test whether two DefineMaps are equal, ignoring definition locations.
fn equals(&self, rhs: &DefineMap) -> bool {
if self.len() != rhs.len() {
return false;
}
self.inner.iter().all(|(key, value)| {
rhs.inner.get(key).map_or(false, |v| {
if value.len() != v.len() {
return false;
}
value.iter().zip(v.iter()).all(|(lhs, rhs)| lhs.1 == rhs.1)
})
})
}
*/
}
// ----------------------------------------------------------------------------
// The stack of currently #included files
#[derive(Debug)]
enum Include<'ctx> {
File {
path: PathBuf,
file: FileId,
lexer: Lexer<'ctx, io::Bytes<Box<dyn io::Read>>>,
},
Expansion {
name: String,
location: Location,
tokens: VecDeque<Token>,
},
}
impl<'ctx> Include<'ctx> {
fn from_read(context: &'ctx Context, path: PathBuf, read: Box<dyn io::Read>) -> Include {
let idx = context.register_file(&path);
Include::File {
file: idx,
lexer: Lexer::from_read(context, idx, read),
path,
}
}
}
#[derive(Debug, Default)]
struct IncludeStack<'ctx> {
stack: Vec<Include<'ctx>>,
}
impl<'ctx> IncludeStack<'ctx> {
fn top_file_path(&self) -> &Path {
for each in self.stack.iter().rev() {
if let Include::File { ref path, .. } = each {
return path;
}
}
"".as_ref()
}
fn in_expansion(&self) -> bool {
match self.stack.last() {
Some(Include::Expansion { .. }) => true,
_ => false,
}
}
}
impl<'ctx> Iterator for IncludeStack<'ctx> {
type Item = LocatedToken;
fn next(&mut self) -> Option<LocatedToken> {
loop {
match self.stack.last_mut() {
Some(&mut Include::File { ref mut lexer, .. }) => match lexer.next() {
//Some(Err(e)) => return Some(Err(e)),
Some(t) => return Some(t),
None => {} // fall through
},
Some(&mut Include::Expansion {
ref mut tokens,
location,
..
}) => match tokens.pop_front() {
Some(token) => return Some(LocatedToken { location, token }),
None => {} // fall through
},
None => return None,
}
self.stack.pop();
}
}
}
// ----------------------------------------------------------------------------
// The main preprocessor
#[derive(Debug)]
struct Ifdef {
location: Location,
active: bool,
chain_active: bool,
}
impl Ifdef {
fn new(location: Location, active: bool) -> Ifdef {
Ifdef {
location,
active,
chain_active: active,
}
}
fn else_(self, location: Location) -> Ifdef {
Ifdef {
location,
active: !self.chain_active,
chain_active: true,
}
}
fn else_if(self, location: Location, active: bool) -> Ifdef {
Ifdef {
location,
active: !self.chain_active && active,
chain_active: self.chain_active || active,
}
}
}
#[derive(Debug)]
/// C-like preprocessor for DM. Expands directives and macro invocations.
pub struct Preprocessor<'ctx> {
context: &'ctx Context,
env_file: PathBuf,
include_stack: IncludeStack<'ctx>,
include_locations: HashMap<FileId, Location>,
last_input_loc: Location,
output: VecDeque<Token>,
ifdef_stack: Vec<Ifdef>,
ifdef_history: IntervalTree<Location, bool>,
annotations: Option<AnnotationTree>,
history: InnerDefineHistory,
defines: DefineMap,
maps: Vec<PathBuf>,
skins: Vec<PathBuf>,
scripts: Vec<PathBuf>,
last_printable_input_loc: Location,
danger_idents: HashMap<String, Location>,
in_interp_string: u32,
docs_in: VecDeque<(Location, DocComment)>,
docs_out: VecDeque<(Location, DocComment)>,
}
impl<'ctx> HasLocation for Preprocessor<'ctx> {
fn location(&self) -> Location {
match self.include_stack.stack.last() {
Some(&Include::File { ref lexer, .. }) => lexer.location(),
Some(&Include::Expansion { location, .. }) => location,
None => Location::default()
}
}
}
impl<'ctx> Preprocessor<'ctx> {
/// Create a new preprocessor from the given Context and environment file.
pub fn new(context: &'ctx Context, env_file: PathBuf) -> io::Result<Self> {
// Buffer the entire environment file. Large environments take a while
// to load and locking it for the whole time is somewhat inconvenient.
let mut buffer = Vec::new();
{
use std::io::Read;
let mut file = File::open(&env_file)?;
file.read_to_end(&mut buffer)?;
}
let include = Include::from_read(context, env_file.clone(), Box::new(io::Cursor::new(buffer)));
Ok(Preprocessor {
context,
env_file,
include_stack: IncludeStack { stack: vec![include] },
include_locations: Default::default(),
history: Default::default(),
defines: DefineMap::with_builtins(),
maps: Default::default(),
skins: Default::default(),
scripts: Default::default(),
ifdef_stack: Default::default(),
ifdef_history: Default::default(),
last_input_loc: Default::default(),
last_printable_input_loc: Default::default(),
output: Default::default(),
danger_idents: Default::default(),
docs_in: Default::default(),
docs_out: Default::default(),
in_interp_string: 0,
annotations: None,
})
}
pub fn from_buffer<S: Into<Cow<'static, str>>>(context: &'ctx Context, env_file: PathBuf, buffer: S) -> Self {
let cow_u8 = match buffer.into() {
Cow::Borrowed(s) => Cow::Borrowed(s.as_bytes()),
Cow::Owned(s) => Cow::Owned(s.into_bytes()),
};
let include = Include::from_read(context, env_file.clone(), Box::new(io::Cursor::new(cow_u8)));
Preprocessor {
context,
env_file,
include_stack: IncludeStack { stack: vec![include] },
include_locations: Default::default(),
history: Default::default(),
defines: DefineMap::with_builtins(),
maps: Default::default(),
skins: Default::default(),
scripts: Default::default(),
ifdef_stack: Default::default(),
ifdef_history: Default::default(),
last_input_loc: Default::default(),
last_printable_input_loc: Default::default(),
output: Default::default(),
danger_idents: Default::default(),
docs_in: Default::default(),
docs_out: Default::default(),
in_interp_string: 0,
annotations: None,
}
}
/// Finalize this preprocessor into its complete define history.
pub fn finalize(mut self) -> DefineHistory {
let mut i = 0;
for (name, vector) in self.defines.inner.drain() {
for (start, define) in vector {
// Give each define its own end column in order to avoid key
// collisions in the interval tree.
i += 1;
let end = Location {
file: FileId::default(),
line: !0,
column: i,
};
self.history.insert(range(start, end), (name.clone(), define));
}
}
DefineHistory {
env_file: self.env_file,
last_input_loc: self.last_input_loc,
tree: self.history,
}
}
/// Access the ifdef history.
pub fn ifdef_history(&self) -> &IntervalTree<Location, bool> {
&self.ifdef_history
}
/*
/// Check whether this preprocessor's state as of the end of the given file
/// matches the given child preprocessor.
pub fn matches_end_of_file(&self, file: FileId, other: &Preprocessor) -> bool {
let location = Location { file, line: !0, column: !0 };
let defines = DefineMap::from_history(&self.history, location);
defines.equals(&other.defines)
}
*/
/// Push a DM file to the top of this preprocessor's stack.
pub fn push_file<R: io::Read + 'static>(&mut self, path: PathBuf, read: R) -> FileId {
let idx = self.context.register_file(&path);
self.include_stack.stack.push(Include::File {
lexer: Lexer::from_read(self.context, idx, Box::new(read)),
file: idx,
path,
});
idx
}
/// Enable source file annotations.
pub fn enable_annotations(&mut self) {
self.annotations = Some(AnnotationTree::default());
}
/// Retrieve computer annotations.
pub fn take_annotations(&mut self) -> Option<AnnotationTree> {
self.annotations.take()
}
// ------------------------------------------------------------------------
// Macro definition handling
fn annotate_macro(&mut self, ident: &str, def_loc: Location) {
if self.include_stack.in_expansion() {
return;
}
if let Some(annotations) = self.annotations.as_mut() {
annotations.insert(
self.last_input_loc .. self.last_input_loc.add_columns(ident.len() as u16),
Annotation::MacroUse(ident.to_owned(), def_loc));
}
}
fn in_environment(&self) -> bool {
for include in self.include_stack.stack.iter().rev() {
if let Include::File { ref path, .. } = *include {
return *path == self.env_file;
}
}
false
}
fn is_defined(&self, name: &str) -> bool {
match name {
"__MAIN__" => self.in_environment(),
_ => self.defines.contains_key(name),
}
}
fn move_to_history(&mut self, name: String, previous: (Location, Define)) {
self.history.insert(range(previous.0, self.last_input_loc), (name, previous.1));
}
// ------------------------------------------------------------------------
// Conditional compilation handling
fn is_disabled(&self) -> bool {
self.ifdef_stack.iter().any(|x| !x.active)
}
fn pop_ifdef(&mut self) -> Option<Ifdef> {
self.ifdef_stack.pop().map(|ifdef| {
self.ifdef_history.insert(range(ifdef.location, self.last_input_loc), ifdef.active);
ifdef
})
}
fn evaluate_inner(&mut self) -> Result<bool, DMError> {
// pump real_next to fill output until we get a real newline on input
let start = self.last_input_loc;
while let Some(tok) = self.inner_next() {
self.last_input_loc = tok.location;
if let Token::Punct(Punctuation::Newline) = tok.token {
break;
}
if let Err(e) = self.real_next(tok.token, true) {
self.context.register_error(e);
}
}
// If we're inside an inactive #if, don't actually evaluate.
// #ifdef FOO -- #if FOO == 5 will error otherwise.
if self.is_disabled() {
self.output.clear();
return Ok(false);
}
let expr = crate::parser::parse_expression(
self.context,
start,
self.output.drain(..).map(|token| LocatedToken::new(start, token))
)?;
Ok(crate::constants::preprocessor_evaluate(start, expr, &self.defines)?.to_bool())
}
fn evaluate(&mut self) -> bool {
// always succeed in order to avoid phantom "unmatched #endif" messages
match self.evaluate_inner() {
Ok(value) => value,
Err(err) => {
self.context.register_error(err);
false
}
}
}
// ------------------------------------------------------------------------
// Doc comments
/// Something other than a `#define` was encountered, docs are not for us.
fn flush_docs(&mut self) {
self.docs_out.extend(self.docs_in.drain(..));
}
// ------------------------------------------------------------------------
// Internal utilities
fn prepare_include_file(&mut self, path: PathBuf) -> Result<Include<'ctx>, DMError> {
// Attempt to open the file.
let read = io::BufReader::new(File::open(&path).map_err(|e|
DMError::new(self.last_input_loc, format!("failed to open file: #include {:?}", path))
.with_cause(e))?);
// Get the path relative to the environment root, for easy lookup later.
let register = path.strip_prefix(self.env_file.parent().unwrap()).unwrap_or(&path);
// Make sure the file hasn't already been included.
// All DM source is effectively `#pragma once`.
let file_id = self.context.register_file(®ister);
if let Some(&loc) = self.include_locations.get(&file_id) {
Err(DMError::new(self.last_input_loc, format!("duplicate #include {:?}", path))
.set_severity(Severity::Warning)
.with_note(loc, "previously included here")
.with_errortype("duplicate_include"))
} else {
self.include_locations.insert(file_id, self.last_input_loc);
Ok(Include::File {
path,
file: file_id,
lexer: Lexer::from_read(&self.context, file_id, Box::new(read)),
})
}
}
fn check_danger_ident(&mut self, name: &str, kind: &str) {
if let Some(loc) = self.danger_idents.get(name) {
self.context.register_error(DMError::new(*loc, format!(
"macro {:?} used immediately before being {}:\n\
https://secure.byond.com/forum/?post=2072419", name, kind
)).set_severity(Severity::Warning));
}
}
fn inner_next(&mut self) -> Option<LocatedToken> {
self.include_stack.next()
}
#[allow(unreachable_code)]
fn real_next(&mut self, read: Token, inside_condition: bool) -> Result<(), DMError> {
let mut _last_expected_loc = self.last_input_loc;
macro_rules! next {
() => {
match self.inner_next() {
Some(x) => {
_last_expected_loc = x.location;
x.token
}
None => return Err(self.error("unexpected EOF")),
}
};
}
macro_rules! expect_token {
(($($i:ident),*) = $p:pat) => {
let ($($i,)*) = match next!() {
$p => ($($i,)*),
other => return Err(self.error(format!("unexpected token {:?}, expecting {}", other, stringify!($p))))
};
}
}
const ALL_DIRECTIVES: &[&str] = &[
"if", "ifdef", "ifndef", "elif", "else", "endif",
"include", "define", "undef", "warn", "error",
];
let disabled = !inside_condition && self.is_disabled();
match read {
Token::Punct(Punctuation::Hash) => {
// preprocessor directive, next thing ought to be an ident
expect_token!((ident) = Token::Ident(ident, _));
match &ident[..] {
// ifdefs
"endif" => {
self.pop_ifdef().ok_or_else(||
DMError::new(self.last_input_loc, "unmatched #endif"))?;
}
"else" => {
let last = self.pop_ifdef().ok_or_else(||
DMError::new(self.last_input_loc, "unmatched #else"))?;
self.ifdef_stack.push(last.else_(self.last_input_loc));
}
"ifdef" => {
expect_token!((define_name) = Token::Ident(define_name, _));
expect_token!(() = Token::Punct(Punctuation::Newline));
let enabled = self.is_defined(&define_name);
self.ifdef_stack.push(Ifdef::new(self.last_input_loc, enabled));
}
"ifndef" => {
expect_token!((define_name) = Token::Ident(define_name, _));
expect_token!(() = Token::Punct(Punctuation::Newline));
let enabled = !self.is_defined(&define_name);
self.ifdef_stack.push(Ifdef::new(self.last_input_loc, enabled));
}
"if" => {
let enabled = self.evaluate();
self.ifdef_stack.push(Ifdef::new(self.last_input_loc, enabled));
}
"elif" => {
let last = self.pop_ifdef().ok_or_else(||
DMError::new(self.last_input_loc, "unmatched #elif"))?;
let enabled = self.evaluate();
self.ifdef_stack.push(last.else_if(self.last_input_loc, enabled));
}
// --------------------------------------------------------
// anything other than ifdefs may be ifdef'd out
// --------------------------------------------------------
// include searches relevant paths for files
"include" if disabled => {}
"include" => {
expect_token!((path) = Token::String(path));
expect_token!(() = Token::Punct(Punctuation::Newline));
let path = PathBuf::from(path.replace("\\", "/"));
for candidate in vec![
// 1. relative to file in which `#include` appears.
self.include_stack.top_file_path().parent().unwrap().join(&path),
// 2. relative to root `.dme` file.
self.env_file.parent().unwrap().join(&path),
] {
if !candidate.exists() {
continue;
}
// Double-match is used to let go of the borrow of
// `candidate` so it can be used in the second half.
enum FileType {
DMM,
DMF,
DMS,
DM,
}
match match candidate.extension().and_then(|s| s.to_str()) {
Some("dmm") => FileType::DMM,
Some("dmf") => FileType::DMF,
Some("dms") => FileType::DMS,
Some("dm") => FileType::DM,
Some("dme") => FileType::DM,
Some(ext) => {
self.context.register_error(DMError::new(
self.last_input_loc,
format!("unknown extension {:?}", ext),
));
return Ok(());
}
None => {
self.context.register_error(DMError::new(self.last_input_loc, "filename has no extension"));
return Ok(());
}
} {
FileType::DMM => self.maps.push(candidate),
FileType::DMF => self.skins.push(candidate),
FileType::DMS => self.scripts.push(candidate),
FileType::DM => match self.prepare_include_file(candidate) {
Ok(include) => {
// A phantom newline keeps the include
// directive being indented from making
// the first line of the file indented.
self.output.push_back(Token::Punct(Punctuation::Newline));
self.include_stack.stack.push(include);
},
Err(e) => self.context.register_error(e),
},
}
return Ok(());
}
self.context.register_error(DMError::new(self.last_input_loc, format!("failed to find #include {:?}", path)));
return Ok(());
}
// both constant and function defines
"define" if disabled => {
// Skip to the end of the line, or else we'll catch
// stringify operators `#X` as unknown directives.
loop {
match next!() {
Token::Punct(Punctuation::Newline) => break,
_ => {}
}
}
}
"define" => {
// accumulate just-seen Following doc comments
let mut our_docs = Vec::new();
while let Some((loc, doc)) = self.docs_in.pop_back() {
if doc.target == DocTarget::FollowingItem {
our_docs.push(doc);
} else {
self.docs_in.push_back((loc, doc));
break;
}
}
let mut docs = DocCollection::default();
for each in our_docs.into_iter().rev() {
docs.push(each);
}
// flush all docs which do not apply to this define
self.flush_docs();
expect_token!((define_name, ws) = Token::Ident(define_name, ws));
let define_name_loc = _last_expected_loc;
if let Some(annotations) = self.annotations.as_mut() {
annotations.insert(
define_name_loc .. define_name_loc.add_columns(define_name.len() as u16),
Annotation::MacroDefinition(define_name.to_owned()));
}
self.check_danger_ident(&define_name, "defined");
let mut params = Vec::new();
let mut subst = Vec::new();
let mut variadic = false;
'outer: loop {
match next!() {
Token::Punct(Punctuation::LParen) if !ws => {
loop {
if variadic {
return Err(self.error("only the last parameter of a macro may be variadic"));
}
match next!() {
Token::Ident(name, _) => params.push(name),
Token::Punct(Punctuation::Ellipsis) => {
params.push("__VA_ARGS__".to_owned()); // default
variadic = true;
}
_ => return Err(self.error("malformed macro parameters, expected name")),
}
match next!() {
Token::Punct(Punctuation::Comma) => {}
Token::Punct(Punctuation::RParen) => break,
Token::Punct(Punctuation::Ellipsis) => {
variadic = true;
match next!() {
Token::Punct(Punctuation::RParen) => break,
_ => return Err(self.error("only the last parameter of a macro may be variadic"))
}
}
_ => return Err(self.error("malformed macro parameters, expected comma")),
}
}
}
Token::Punct(Punctuation::Newline) => break 'outer,
Token::DocComment(doc) => docs.push(doc),
other => {
subst.push(other);
}
}
loop {
match next!() {
Token::Punct(Punctuation::Newline) => break 'outer,
Token::DocComment(doc) => docs.push(doc),
other => subst.push(other),
}
}
}
let define = if params.is_empty() {
Define::Constant { subst, docs }
} else {
Define::Function { params, subst, variadic, docs }
};
// DEBUG can only be defined in the root .dme file
if define_name != "DEBUG" || self.in_environment() {
if let Some(previous_loc) = self.defines.insert(define_name.clone(), (define_name_loc, define)) {
// DM doesn't issue a warning for this, but it's usually a mistake, so let's.
// FILE_DIR is handled specially and sometimes makes sense to define multiple times.
if define_name != "FILE_DIR" {
DMError::new(define_name_loc, format!("macro redefined: {}", define_name))
.set_severity(Severity::Warning)
.with_note(previous_loc, format!("previous definition of {}", define_name))
.with_errortype("macro_redefined")
.register(self.context);
}
}
}
}
"undef" if disabled => {}
"undef" => {
expect_token!((define_name) = Token::Ident(define_name, _));
let define_name_loc = _last_expected_loc;
self.check_danger_ident(&define_name, "undefined");
expect_token!(() = Token::Punct(Punctuation::Newline));
if let Some(previous) = self.defines.remove(&define_name) {
self.move_to_history(define_name, previous);
} else {
DMError::new(define_name_loc, format!("macro undefined while not defined: {}", define_name))
.with_errortype("macro_undefined_no_definition")
.set_severity(Severity::Warning)
.register(self.context);
}
}
"warn" if disabled => {}
"warn" => {
expect_token!((text) = Token::String(text));
DMError::new(self.last_input_loc, format!("#{} {}", ident, text))
.set_severity(Severity::Warning)
.register(self.context);
}
"error" if disabled => {}
"error" => {
expect_token!((text) = Token::String(text));
self.context.register_error(DMError::new(self.last_input_loc, format!("#{} {}", ident, text)));
}
// none of this other stuff should even exist
other => {
let mut meant = "";
for each in ALL_DIRECTIVES {
if other.starts_with(each) && each.len() > meant.len() {
meant = each;
}
}
return Err(DMError::new(self.last_input_loc, format!("unknown directive: #{}{}{}", ident,
if !meant.is_empty() { ", did you mean #" } else { "" }, meant)));
}
}
// yield a newline
self.output.push_back(Token::Punct(Punctuation::Newline));
return Ok(());
}
// anything other than directives may be ifdef'd out
_ if disabled => return Ok(()),
// identifiers may be macros
Token::Ident(ref ident, whitespace) => {
self.flush_docs();
// lint for BYOND bug
if self.in_interp_string > 0 {
self.danger_idents.insert(ident.clone(), self.last_input_loc);
}
// substitute special macros
if ident == "__FILE__" {
self.annotate_macro(ident, Location::builtins());
for include in self.include_stack.stack.iter().rev() {
if let Include::File { ref path, .. } = *include {
self.output.push_back(Token::String(path.display().to_string()));
return Ok(());
}
}
self.output.push_back(Token::String(String::new()));
return Ok(());
} else if ident == "__LINE__" {
self.annotate_macro(ident, Location::builtins());
self.output.push_back(Token::Int(self.last_input_loc.line as i32));
return Ok(());
}
// special case for inside a defined() call
if let Some(Token::Punct(Punctuation::LParen)) = self.output.back() {
if let Some(idx) = self.output.len().checked_sub(2) {
if let Some(Token::Ident(identname, _)) = self.output.get(idx) {
if identname.as_str() == "defined" {
self.output.push_back(Token::Ident(ident.to_owned(), whitespace));
return Ok(());
}
}
}
}
// if it's a define, perform the substitution
let mut expansion = self.defines.get(ident).cloned(); // TODO: don't clone?
if expansion.is_some() && self.include_stack.stack.len() > MAX_RECURSION_DEPTH {
self.error(format!("expanding {:?} would exceed max recursion depth of {} levels",
ident, MAX_RECURSION_DEPTH)).register(self.context);
expansion = None;
}
match expansion {
Some((location, Define::Constant { subst, docs: _ })) => {
self.annotate_macro(ident, location);
self.include_stack.stack.push(Include::Expansion {
name: ident.to_owned(),
tokens: subst.into_iter().collect(),
location: self.last_input_loc,
});
return Ok(());
}
Some((location, Define::Function { ref params, ref subst, variadic, docs: _ })) => {
// if it's not followed by an LParen, it isn't really a function call
match next!() {
Token::Punct(Punctuation::LParen) => {}
other => {
self.output.push_back(Token::Ident(ident.to_owned(), false));
match other {
Token::InterpStringBegin(_) => self.in_interp_string += 1,
Token::InterpStringEnd(_) => self.in_interp_string -= 1,
_ => {}
}
self.output.push_back(other);
return Ok(());