rsleigh 0.4.2

SLEIGH (.slaspec) parser and Rust decoder/P-code emitter codegen — Ghidra-compatible disassembly in pure Rust
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
pub mod ifs;
pub mod macros;
pub mod parser;
pub mod token;

use nom::branch::alt;
use nom::bytes::complete::tag;
use nom::character::complete::{line_ending, space0};
use nom::combinator::{consumed, eof, map, value};
use nom::sequence::pair;
use nom::IResult;
use thiserror::Error;

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::rc::Rc;

use parser::empty_space0;

use crate::preprocessor::macros::MacroLine;
use crate::{FileLocation, FileSpan, Location, MacroLocation, Span};

use ifs::IfCheckOwned;
use macros::{expansion, DefineDataOwned};
use parser::display_token;
use token::{Token, TokenType};

const MACRO_IF: &str = "@if";
const MACRO_IFDEF: &str = "@ifdef";
const MACRO_IFNDEF: &str = "@ifndef";
const MACRO_ELIF: &str = "@elif";
const MACRO_ELSE: &str = "@else";
const MACRO_ENDIF: &str = "@endif";

const MACRO_DEFINE: &str = "@define";
const MACRO_UNDEFINE: &str = "@undef";
const MACRO_INCLUDE: &str = "@include";

#[derive(Error, Debug)]
pub enum PreprocessorError {
    #[error("IO Error {0}")]
    File(#[from] std::io::Error),
    #[error("Preprocessor Parsing error at\n{0}\n")]
    Parse(#[from] nom::Err<nom::error::Error<Span>>),
    #[error("Preprocessor Execute at {message} {src}")]
    Execute { message: &'static str, src: Span },
    #[error("Comparing undefined Value {0}\n")]
    ComparingUndefined(FileSpan),
    #[error("Comparing Empty Value {0}\n")]
    ComparingEmpty(FileSpan),
    #[error("Expanding Undefined Value {0}\n")]
    ExpandingUndefined(FileSpan),
    #[error("Expanding Empty Value {0}\n")]
    ExpandingEmpty(FileSpan),
    #[error("Alias undefined Value {0}\n")]
    AliasUndefined(FileSpan),
    #[error("Alias with empty Value {0}\n")]
    AliasEmpty(FileSpan),
    #[error("Deleting undefined Value {0}\n")]
    DeleteUndefined(FileSpan),
    //NOTE this is allowed, just overwride the value
    //#[error("Defined value already exists {0}\n")]
    //DuplicatedDefine(FileSpan),
    #[error("Unable to parse Token at {0}\n")]
    PreprocessorToken(Location),
    #[error("Block was never opened {0}\n")]
    InvalidEndIf(FileSpan),
    #[error("Display was not closed correctly\n")]
    UnclosedDisplay,
    #[error("Unable to find If-Block closing {0}\n")]
    NotFoundEndIf(FileSpan),
}

#[derive(Debug)]
pub struct Define {
    pub name: Rc<str>,
    pub value: Option<String>,
    // TODO field never used, propably should be fixed during the debug messages rework
    #[allow(dead_code)]
    pub macro_location: FileSpan,
    pub value_location: FileSpan,
}

#[derive(Debug, Copy, Clone)]
enum IfStatus {
    If,
    IfElse,
    Else,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum IfBorders {
    If,
    IfDef,
    IfNDef,
    IfElse,
    Else,
    EndIf,
}

#[derive(Debug)]
enum DrainingSource {
    File(DrainingFile),
    Macro(DrainingMacro),
}
#[derive(Debug)]
pub(crate) struct DrainingMacro {
    location: MacroLocation,
    data: String,
    position: usize,
}
impl DrainingMacro {
    fn new(defined: &Define, location: FileSpan) -> Result<Self, Box<PreprocessorError>> {
        let Some(defined_value) = &defined.value else {
            return Err(Box::new(PreprocessorError::ExpandingEmpty(location)));
        };
        Ok(Self {
            location: MacroLocation {
                value: defined.value_location.clone(),
                expansion: location,
                line: 0,
                column: 0,
            },
            data: defined_value.clone(),
            position: 0,
        })
    }
    fn location(&self) -> &MacroLocation {
        &self.location
    }
    fn nom_it<'a, O, F, E>(&'a mut self, nom: F) -> Result<(O, Span), Box<PreprocessorError>>
    where
        O: 'a,
        F: FnMut(&'a str) -> IResult<&'a str, O, E>,
        E: nom::error::ParseError<&'a str>,
    {
        //extract the value
        let (_, (skip, value)) = consumed(nom)(&self.data[self.position..]).map_err(|_| {
            Box::new(PreprocessorError::PreprocessorToken(Location::Macro(
                self.location().clone(),
            )))
        })?;
        //calculate the currect line/column after consumption
        let end_line = self.location.line + skip.split('\n').count() as u64;
        let end_column = skip
            .rsplit_once(&['\n', '\r'])
            .map(|x| x.1.chars().count() as u64)
            .unwrap_or(self.location.column + skip.chars().count() as u64);
        // return this token location
        let location = self.location().end_span(end_line, end_column);
        //update the currect drain location
        self.location.line = end_line;
        self.location.column = end_column;
        self.position += skip.len();
        //return values
        Ok((value, Span::Macro(location)))
    }
    fn parse_display(&mut self) -> Result<Option<DisplayToken>, Box<PreprocessorError>> {
        self.nom_it(display_token)
            .map(|(token, span)| match token? {
                parser::Display::End => Some(DisplayToken::End),
                parser::Display::Concat => Some(DisplayToken::Concat),
                parser::Display::Ident(ident) => Some(DisplayToken::Ident(span, ident)),
                parser::Display::Other(c) => Some(DisplayToken::Other(c)),
                parser::Display::Literal(lit) => Some(DisplayToken::Literal(lit)),
            })
    }
    fn parse(&mut self) -> Result<Option<Token>, Box<PreprocessorError>> {
        //eat all the empty spaces
        let _ = self.nom_it::<_, _, ()>(space0).unwrap();
        //parse a token or None if eof
        self.nom_it(alt((value(None, eof), map(TokenType::parse, Option::Some))))
            .map(|(token_type, location)| Some(Token::new(location, token_type?)))
    }
}

#[derive(Clone, Debug)]
enum DrainingFileProduct {
    Token(Token),
    File(String, FileSpan),
    Expand(String, FileSpan),
    End,
}

#[derive(Clone, Debug)]
enum DrainingFileBody {
    Macro(MacroLine, FileSpan),
    Expansion(String, FileSpan),
    Token(Token),
    End,
}
#[derive(Debug)]
pub(crate) struct DrainingFile {
    location: FileLocation,
    if_stack: Vec<(FileSpan, IfStatus)>,
    data: String,
    position: usize,
}
impl DrainingFile {
    fn new(file: &Path) -> Result<Self, Box<PreprocessorError>> {
        let data = std::fs::read_to_string(file).map_err(|e| Box::new(e.into()))?;
        Ok(Self {
            data,
            position: 0,
            location: FileLocation::new_start(file),
            if_stack: vec![],
        })
    }
    fn location(&self) -> &FileLocation {
        &self.location
    }
    fn data(&self) -> &str {
        &self.data[self.position..]
    }
    fn nom_it<'a, O, F, E>(&'a mut self, nom: F) -> Result<(O, FileSpan), Box<PreprocessorError>>
    where
        O: 'a,
        F: FnMut(&'a str) -> IResult<&'a str, O, E>,
        E: nom::error::ParseError<&'a str>,
    {
        //extract the value
        let (_, (skip, value)) = consumed(nom)(&self.data[self.position..]).map_err(|_| {
            Box::new(PreprocessorError::PreprocessorToken(Location::File(
                self.location().clone(),
            )))
        })?;
        //calculate the currect line/column after consumption
        let end_line = self.location.line + skip.split('\n').count() as u64 - 1;
        let end_column = skip
            .rsplit_once(&['\n', '\r'])
            .map(|x| x.1.chars().count() as u64)
            .unwrap_or(self.location.column + skip.chars().count() as u64);
        // return this token location
        let location = self.location().end_span(end_line, end_column);
        //update the currect drain location
        self.location.line = end_line;
        self.location.column = end_column;
        self.position += skip.len();
        //return values
        Ok((value, location))
    }
    fn next_if_block<F>(
        &mut self,
        src: &FileSpan,
        mut f: F,
    ) -> Result<IfBorders, Box<PreprocessorError>>
    where
        F: FnMut(IfBorders) -> bool,
    {
        use IfBorders::*;
        let mut counter = 0u32;
        let data = &self.data[self.position..];
        let found = data
            .lines()
            .enumerate()
            .filter_map(|(line_num, line)| {
                //check this line is a macro
                let (_rest, found_macro) = alt::<_, _, (), _>((
                    value(If, pair(space0, tag(MACRO_IF))),
                    value(IfDef, pair(space0, tag(MACRO_IFDEF))),
                    value(IfNDef, pair(space0, tag(MACRO_IFNDEF))),
                    value(IfElse, pair(space0, tag(MACRO_ELIF))),
                    value(Else, pair(space0, tag(MACRO_ELSE))),
                    value(EndIf, pair(space0, tag(MACRO_ENDIF))),
                ))(line)
                .ok()?;
                Some((line_num, line, found_macro))
            })
            .find_map(|(line_num, line, found)| {
                match (counter, found) {
                    //ignore the sub if
                    (_, If | IfDef | IfNDef) => counter += 1,
                    //if in a sub if, just close it
                    (1.., EndIf) => counter -= 1,
                    //if sub-if-block ifElse or Else, just ignore it
                    (1.., IfElse | Else) => (),
                    //found the end of the block, don't consume macro, just exit
                    (0, block) if f(block) => return Some((line_num, line, block)),
                    //found a block that we don't care about
                    (0, _block) => (),
                }
                None
            });
        let Some((line_num, line, block)) = found else {
            return Err(Box::new(PreprocessorError::NotFoundEndIf(src.clone())));
        };

        let skip = line.as_ptr() as usize - data.as_ptr() as usize;
        self.position += skip;
        self.location.line += line_num as u64;
        self.location.column = 0;
        Ok(block)
    }

    fn enter_if_block(
        &mut self,
        cond: bool,
        state: &PreProcessorState,
        src: &FileSpan,
    ) -> Result<(), Box<PreprocessorError>> {
        //if this if is true, just to inside it
        if cond {
            self.if_stack.push((src.clone(), IfStatus::If));
            return Ok(());
        }
        //othewise find the next block and check if we will enter it
        loop {
            let _next_block = self.next_if_block(src, |_| true)?;
            match self.nom_it(MacroLine::parse)? {
                (MacroLine::ElIf(cond), src) => {
                    if check_cond(state, cond, &src)? {
                        self.if_stack.push((src.clone(), IfStatus::IfElse));
                        break;
                    } else {
                        //not the block that we can enter, search the next
                        continue;
                    }
                }
                (MacroLine::Else, _) => {
                    self.if_stack.push((src.clone(), IfStatus::Else));
                    break;
                }
                (MacroLine::EndIf, _) => {
                    //we never entered any if-block
                    break;
                }
                (_, _) => unreachable!(),
            }
        }
        Ok(())
    }
    fn parse_macro_line(&mut self) -> Option<DrainingFileBody> {
        let (macro_line, location) = self.nom_it(MacroLine::parse).ok()?;
        Some(DrainingFileBody::Macro(macro_line, location))
    }
    fn parse_display(&mut self) -> Result<Option<DisplayToken>, Box<PreprocessorError>> {
        self.nom_it(display_token)
            .map(|(token, span)| match token? {
                parser::Display::End => Some(DisplayToken::End),
                parser::Display::Concat => Some(DisplayToken::Concat),
                parser::Display::Ident(ident) => Some(DisplayToken::Ident(Span::File(span), ident)),
                parser::Display::Other(c) => Some(DisplayToken::Other(c)),
                parser::Display::Literal(lit) => Some(DisplayToken::Literal(lit)),
            })
    }
    fn parse_body(&mut self) -> Result<DrainingFileBody, Box<PreprocessorError>> {
        //a macro need to be after a new line or the first thing in the file
        if self.position == 0 {
            if let Some(x) = self.parse_macro_line() {
                return Ok(x);
            }
        }
        loop {
            //first consume the empty space, but not the new line
            //never fails
            let _ = self.nom_it(empty_space0).unwrap();

            //then check if is a newline, if so, check if there is a macro after
            if self.nom_it::<_, _, ()>(line_ending).is_ok() {
                match self.parse_macro_line() {
                    Some(x) => return Ok(x),
                    None => {
                        //if there was a new line, but not macro, go back and clean
                        //the empty space on the start of the line and check again
                        continue;
                    }
                }
            } else {
                //not newline, it need to have a valid token, expansion or eof
                if self.data().is_empty() {
                    return Ok(DrainingFileBody::End);
                } else if let Ok((expansion, location)) = self.nom_it(expansion) {
                    return Ok(DrainingFileBody::Expansion(expansion.to_owned(), location));
                } else {
                    return self
                        .nom_it(TokenType::parse)
                        .map(|(location, token_type)| Token::new(Span::File(token_type), location))
                        .map(DrainingFileBody::Token);
                }
            }
        }
    }
    fn parse(
        &mut self,
        state: &mut PreProcessorState,
    ) -> Result<DrainingFileProduct, Box<PreprocessorError>> {
        use DrainingFileBody::*;
        use MacroLine::*;
        loop {
            let status = self.if_stack.last().cloned();
            let next_block = self.parse_body()?;
            match (status, next_block) {
                (None, End) => return Ok(DrainingFileProduct::End),

                (Some((location, _)), End) => {
                    return Err(Box::new(PreprocessorError::NotFoundEndIf(location)))
                }

                //found a token, just return it
                (_, Token(token)) => return Ok(DrainingFileProduct::Token(token)),

                // expand dong
                (_, Expansion(exp, src)) => return Ok(DrainingFileProduct::Expand(exp, src)),

                //include this sub-file
                (_, Macro(Include(file), src)) => return Ok(DrainingFileProduct::File(file, src)),

                (_, Macro(Define { name, value }, src)) => state.set_define(&name, value, src)?,
                (_, Macro(Undefine(name), src)) => {
                    //TODO ignore if try to delete non existing value?
                    if !state.del_value(&name) {
                        return Err(Box::new(PreprocessorError::DeleteUndefined(src)));
                    }
                }

                //invalid block, such closing a block that was not open
                (None, Macro(ElIf(_), location))
                | (None, Macro(Else, location))
                | (None, Macro(EndIf, location))
                | (Some((_, IfStatus::Else)), Macro(ElIf(_) | Else, location)) => {
                    return Err(Box::new(PreprocessorError::InvalidEndIf(location)))
                }

                //end of the current block, remove the status and continue parsing
                (Some(_), Macro(EndIf, _)) => {
                    self.if_stack.pop();
                }

                //found a inner if, just push the block or skip it
                (_, Macro(If(cond), src)) => {
                    let cond = check_cond(state, cond, &src)?;
                    self.enter_if_block(cond, state, &src)?
                }

                //found the end of the currently executing if-block
                (Some((_, IfStatus::If | IfStatus::IfElse)), Macro(ElIf(_) | Else, src)) => {
                    let _next_block =
                        self.next_if_block(&src, |x| matches!(x, IfBorders::EndIf))?;
                    match self.nom_it(MacroLine::parse)? {
                        (MacroLine::EndIf, _) => (),
                        (_, _) => unreachable!(),
                    }
                    self.if_stack.pop();
                }
            }
        }
    }
}

fn check_cond(
    state: &PreProcessorState,
    cond: IfCheckOwned,
    src: &FileSpan,
) -> Result<bool, Box<PreprocessorError>> {
    match cond {
        IfCheckOwned::Defined(def) => Ok(state.exists(def.as_str())),
        IfCheckOwned::NotDefined(ndef) => Ok(!state.exists(ndef.as_str())),
        IfCheckOwned::Cmp { name, op, value } => {
            //TODO what if comparing a value that don't exists?
            let Some(defined) = state.get_value(name.as_str()) else {
                return Err(Box::new(PreprocessorError::ComparingUndefined(src.clone())));
            };
            //TODO what if compring a define that have no value?
            let Some(defined_value) = &defined.value else {
                return Err(Box::new(PreprocessorError::ComparingEmpty(src.clone())));
            };
            Ok(op.cmp(&value, defined_value))
        }
        IfCheckOwned::Op { left, op, right } => {
            let left = check_cond(state, *left, src)?;
            let right = check_cond(state, *right, src)?;
            Ok(op.check(left, right))
        }
    }
}

#[derive(Debug, Default)]
struct PreProcessorState(HashMap<Rc<str>, Define>);
impl PreProcessorState {
    fn exists(&self, ident: &str) -> bool {
        self.0.contains_key(ident)
    }
    fn get_value(&self, ident: &str) -> Option<&Define> {
        self.0.get(ident)
    }
    fn set_define(
        &mut self,
        name: &str,
        value: Option<DefineDataOwned>,
        src: FileSpan,
    ) -> Result<(), Box<PreprocessorError>> {
        use macros::DefineDataOwned::*;
        let (value, value_location) = match value {
            None => (None, src.clone()),
            Some(Alias(alias)) => {
                let (value, location) =
                    self.get_value(&alias)
                        .ok_or_else(|| Box::new(PreprocessorError::AliasUndefined(src.clone())))
                        .and_then(|define| {
                            let value = define.value.clone().ok_or_else(|| {
                                Box::new(PreprocessorError::AliasEmpty(src.clone()))
                            })?;
                            Ok((value, define.value_location.clone()))
                        })?;
                (Some(value), location)
            }
            Some(Value(value)) => (Some(value), src.clone()),
        };
        let define = Define {
            name: Rc::from(name),
            value,
            macro_location: src.clone(),
            value_location,
        };
        self.set_value(define);
        Ok(())
    }
    fn set_value(&mut self, define: Define) {
        let name = Rc::clone(&define.name);
        self.0.insert(name, define);
    }
    fn del_value(&mut self, ident: &str) -> bool {
        self.0.remove(ident).is_some()
    }
}

#[derive(Clone, Debug)]
pub enum DisplayToken {
    End,
    Concat,
    Ident(Span, String),
    Literal(String),
    Other(char),
}

#[derive(Debug)]
pub struct FilePreProcessor {
    pub root_path: Option<PathBuf>,
    file_stack: Vec<DrainingSource>,
    defines: PreProcessorState,
}
impl FilePreProcessor {
    pub fn new(file: &Path) -> Result<Self, Box<PreprocessorError>> {
        let root_path = file.parent().map(Path::to_path_buf);
        let file = DrainingFile::new(file)?;
        Ok(Self {
            root_path,
            defines: PreProcessorState::default(),
            file_stack: vec![DrainingSource::File(file)],
        })
    }
    pub fn is_finished(&self) -> bool {
        self.file_stack.is_empty()
    }
    /// process a display token
    pub fn parse_display(&mut self) -> Result<DisplayToken, Box<PreprocessorError>> {
        //TODO does the display should expand macros?
        //TODO does display can exist in between files? is this loop required?
        loop {
            let Some(file) = self.file_stack.last_mut() else {
                return Err(Box::new(PreprocessorError::UnclosedDisplay));
            };
            //try to parse a token from this source
            let token = match file {
                DrainingSource::File(file) => file.parse_display()?,
                DrainingSource::Macro(source) => source.parse_display()?,
            };
            if let Some(token) = token {
                return Ok(token);
            } else {
                //end of this file
                self.file_stack.pop();
            }
        }
    }
    /// process the accumulated buffer
    pub fn parse(&mut self) -> Result<Option<Token>, Box<PreprocessorError>> {
        // read files untill is able to process a token
        loop {
            let Some(file) = self.file_stack.last_mut() else {
                return Ok(None);
            };
            //try to parse a token from this source
            let token = match file {
                DrainingSource::File(file) => match file.parse(&mut self.defines)? {
                    DrainingFileProduct::End => None,
                    DrainingFileProduct::Token(token) => Some(token),
                    DrainingFileProduct::File(file, _src) => {
                        let fullpath = self.root_path.clone().unwrap_or_default().join(file);
                        self.file_stack
                            .push(DrainingSource::File(DrainingFile::new(&fullpath)?));
                        continue;
                    }
                    DrainingFileProduct::Expand(exp, src) => {
                        let define = self.defines.get_value(&exp).ok_or_else(|| {
                            Box::new(PreprocessorError::ExpandingUndefined(src.clone()))
                        })?;
                        self.file_stack
                            .push(DrainingSource::Macro(DrainingMacro::new(define, src)?));
                        continue;
                    }
                },
                DrainingSource::Macro(source) => source.parse()?,
            };
            //return this token, if unable, just pop it and goes to the next one
            match token {
                Some(token) => return Ok(Some(token)),
                None => {
                    self.file_stack.pop();
                }
            }
        }
    }
}
impl Iterator for FilePreProcessor {
    type Item = Result<Token, Box<PreprocessorError>>;

    fn next(&mut self) -> Option<Self::Item> {
        self.parse().transpose()
    }
}