cpclib_asm/parser/
context.rs

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
use std::borrow::{Borrow, Cow};
use std::collections::HashSet;
use std::ops::Deref;
use std::path::PathBuf;
use std::sync::{LazyLock, RwLock};

use cpclib_common::camino::{Utf8Path, Utf8PathBuf};
use cpclib_common::winnow::BStr;
use either::Either;
use regex::Regex;

use super::line_col::LineColLookup;
use crate::error::AssemblerError;
use crate::preamble::*;
use crate::LocatedToken;

/// State to limit the parsing abilities depending on the parsing context
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ParsingState {
    /// Parse of a standard Z80 code
    Standard,
    /// Parse of the content of a function
    FunctionLimited,
    /// Parse of the content of a struct
    StructLimited,
    /// Forbid directives
    GeneratedLimited, // TODO rename
    /// Parse of a symbols file
    SymbolsLimited
}

pub trait ParsingStateVerified {
    fn is_accepted(&self, state: &ParsingState) -> bool;
}

impl ParsingStateVerified for LocatedToken {
    fn is_accepted(&self, state: &ParsingState) -> bool {
        self.deref().is_accepted(state)
    }
}

macro_rules! parsing_state_verified_inner {
    () => {
        fn is_accepted(&self, state: &ParsingState) -> bool {
            match state {
                ParsingState::GeneratedLimited => !self.is_directive(),
                ParsingState::Standard => {
                    match self {
                        Self::Return(..) => false,
                        _ => true
                    }
                },
                ParsingState::FunctionLimited => {
                    match self {
                        Self::Equ { .. } | Self::Let(..) => true,
                        Self::If { .. }
                        | Self::Repeat { .. }
                        | Self::Break
                        | Self::Switch { .. }
                        | Self::Iterate { .. } => true,
                        Self::Return(_) => true,
                        Self::Assert(..) | Self::Print(_) | Self::Fail(_) | Self::Comment(_) => {
                            true
                        },
                        _ => false
                    }
                },
                ParsingState::StructLimited => {
                    match self {
                        Self::Defb(..) | Self::Defw(..) | Self::Str(..) | Self::MacroCall(..) => {
                            true
                        },
                        _ => false
                    }
                },
                ParsingState::SymbolsLimited => {
                    match self {
                        Self::Equ { .. } | Self::Let(..) | Self::Comment(_) => true,
                        _ => false
                    }
                },
            }
        }
    };
}

impl ParsingStateVerified for LocatedTokenInner {
    parsing_state_verified_inner!();
}

impl ParsingStateVerified for Token {
    parsing_state_verified_inner!();
}

#[derive(Debug, PartialEq, Eq, Clone)]
pub struct ParserOptions {
    /// Search path to find files
    pub search_path: Vec<Utf8PathBuf>,
    /// When activated, the parser also read and parse the include-like directives (deactivated by default)
    pub read_referenced_files: bool,
    pub show_progress: bool,
    /// Set to true when directives must start by a dot
    pub dotted_directive: bool,
    pub assembler_flavor: AssemblerFlavor
}

impl Default for ParserOptions {
    fn default() -> Self {
        ParserOptions {
            search_path: Default::default(),
            read_referenced_files: true,
            dotted_directive: false,
            show_progress: false,
            assembler_flavor: AssemblerFlavor::Basm
        }
    }
}

impl ParserOptions {
    pub fn context_builder(self) -> ParserContextBuilder {
        ParserContextBuilder {
            options: self,
            current_filename: None,
            context_name: None,
            state: ParsingState::Standard
        }
    }
}

pub struct ParserContextBuilder {
    options: ParserOptions,
    current_filename: Option<Utf8PathBuf>,
    context_name: Option<String>,
    state: ParsingState
}

impl Default for ParserContextBuilder {
    fn default() -> Self {
        ParserOptions::default().context_builder()
    }
}

impl From<ParserContext> for ParserContextBuilder {
    fn from(ctx: ParserContext) -> Self {
        Self {
            state: ctx.state,
            current_filename: ctx.current_filename,
            context_name: ctx.context_name,
            options: ctx.options
        }
    }
}

impl ParserContextBuilder {
    pub fn current_filename(&self) -> Option<&Utf8Path> {
        self.current_filename.as_ref().map(|p| p.as_path())
    }

    pub fn context_name(&self) -> Option<&str> {
        self.context_name.as_deref()
    }

    pub fn set_current_filename<S: Into<Utf8PathBuf>>(mut self, fname: S) -> ParserContextBuilder {
        self.current_filename = Some(fname.into());
        self
    }

    pub fn remove_filename(mut self) -> Self {
        self.current_filename.take();
        self
    }

    pub fn set_context_name<S: Into<String>>(mut self, name: S) -> ParserContextBuilder {
        self.context_name = Some(name.into());
        self
    }

    pub fn set_state(mut self, state: ParsingState) -> Self {
        self.state = state;
        self
    }

    pub fn set_options(mut self, options: ParserOptions) -> Self {
        self.options = options;
        self
    }

    /// Build a ParserContext for the given source code
    #[inline]
    pub fn build(self, code: &str) -> ParserContext {
        let code: &'static str = unsafe { std::mem::transmute(code) };
        let str: &'static BStr = unsafe { std::mem::transmute(BStr::new(code)) };
        ParserContext {
            options: self.options,
            current_filename: self.current_filename,
            context_name: self.context_name,
            state: self.state,
            source: str,
            line_col_lut: Default::default()
        }
    }
}

impl ParserOptions {
    pub fn set_read_referenced_files(&mut self, tag: bool) {
        self.read_referenced_files = tag;
    }

    pub fn set_dotted_directives(&mut self, tag: bool) {
        self.dotted_directive = tag;
    }

    /// Add a search path and ensure it is ABSOLUTE
    /// Method crashes if the search path does not exist
    pub fn add_search_path<P: Into<PathBuf>>(&mut self, path: P) -> Result<(), AssemblerError> {
        let path = path.into();

        if path.is_dir() {
            let path = path.canonicalize().unwrap();

            // manual fix for for windows. No idea why
            let path = path.to_str().unwrap();
            const PREFIX: &str = "\\\\?\\";
            let path = if path.starts_with(PREFIX) {
                path[PREFIX.len()..].to_string()
            }
            else {
                path.to_string()
            };

            // Really add
            self.search_path.push(path.into());
            Ok(())
        }
        else {
            Err(AssemblerError::IOError {
                msg: format!(
                    "{} is not a path and cannot be added in the search path",
                    path.to_str().unwrap()
                )
            })
        }
    }

    /// Add the folder that contains the given file. Ignore if there are issues with the filename
    pub fn add_search_path_from_file<P: Into<PathBuf>>(
        &mut self,
        file: P
    ) -> Result<(), AssemblerError> {
        let file = file.into();
        let path = file.canonicalize();

        match path {
            Ok(path) => {
                let path = path.parent().unwrap().to_owned();
                self.add_search_path(path)
            },

            Err(err) => {
                Err(AssemblerError::IOError {
                    msg: format!(
                        "Unable to add search path for {}. {}",
                        file.to_str().unwrap(),
                        err
                    )
                })
            },
        }
    }

    /// Return the real path name that correspond to the requested file.
    /// Do it in a case insensitive way (for compatibility reasons)
    pub fn get_path_for(
        &self,
        fname: &str,
        env: Option<&Env>
    ) -> Result<Utf8PathBuf, either::Either<AssemblerError, Vec<String>>> {
        use globset::*;
        let mut does_not_exists = Vec::new();
        static RE: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\{+[^\}]+\}+").unwrap());

        let re = RE.deref();
        // Make the expansion in the filename
        let fname: Cow<str> = if let Some(env) = env {
            let mut fname = fname.to_owned();

            let mut replace = HashSet::new();
            for cap in re.captures_iter(&fname) {
                if cap[0] != fname {
                    replace.insert(cap[0].to_owned());
                }
            }

            // make the replacement
            for model in replace.iter() {
                let local_symbol = &model[1..model.len() - 1]; // remove {}
                let local_value = match env.symbols().value(local_symbol) {
                    Ok(Some(Value::String(s))) => s.to_string(),
                    Ok(Some(Value::Expr(e))) => e.to_string(),
                    Ok(Some(Value::Counter(e))) => e.to_string(),
                    Ok(Some(unkn)) => {
                        unimplemented!("{:?}", unkn)
                    },
                    Ok(None) => {
                        return Err(Either::Left(AssemblerError::UnknownSymbol {
                            symbol: model.into(),
                            closest: env.symbols().closest_symbol(model, SymbolFor::Any).unwrap()
                        }))
                    },
                    Err(e) => return Err(Either::Left(e.into()))
                };
                fname = fname.replace(model, &local_value);
            }
            Cow::Owned(fname)
        }
        else {
            Cow::Borrowed(fname)
        };

        let fname: &str = fname.borrow();

        // early exit if the fname goes in an embedding file
        if fname.starts_with("inner://") {
            return Ok(Utf8Path::new(fname).into());
        }

        let fname = Utf8Path::new(fname);

        // check if file exists
        if fname.is_file() {
            return Ok(fname.into());
        }
        does_not_exists.push(fname.as_str().to_owned());

        // otherwhise, try with the current directory of the environment
        if let Some(env) = env.as_ref() {
            if let Some(search) = env.get_current_working_directory() {
                let current_path = search.join(fname);
                if current_path.is_file() {
                    return Ok(current_path.try_into().unwrap());
                }
                else {
                    does_not_exists.push(current_path.to_string());
                }
            }
        }

        // otherwhise try with the folder set up at the beginning
        {
            // loop over all possibilities
            for search in &self.search_path {
                assert!(Utf8Path::new(&search).is_dir());
                let current_path = search.join(fname);

                if current_path.is_file() {
                    return Ok(current_path);
                }
                else {
                    let glob = GlobBuilder::new(current_path.as_path().as_str())
                        .case_insensitive(true)
                        .literal_separator(true)
                        .build()
                        .unwrap();
                    let matcher = glob.compile_matcher();

                    for entry in std::fs::read_dir(search).unwrap() {
                        let entry = entry.unwrap();
                        let path = entry.path();
                        if matcher.is_match(&path) {
                            return Ok(path.try_into().unwrap());
                        }
                    }

                    does_not_exists.push(current_path.as_str().to_owned());
                }
            }
        }

        // No file found
        Err(Either::Right(does_not_exists))
    }

    pub fn set_flavor(&mut self, flavor: AssemblerFlavor) -> &mut Self {
        self.assembler_flavor = flavor;
        self
    }

    #[inline(always)]
    pub fn is_orgams(&self) -> bool {
        self.assembler_flavor == AssemblerFlavor::Orgams
    }
}
/// Context information that can guide the parser
/// TODO add assembling flags
#[derive(Debug)]
pub struct ParserContext {
    /// Limitation on the kind of intruction to parse.
    /// The current state is at the end (it is modified when in a struct)
    pub state: ParsingState,
    /// Filename that is currently parsed
    pub current_filename: Option<Utf8PathBuf>,
    /// Current context (mainly when playing with macros)
    pub context_name: Option<String>,
    pub options: ParserOptions,
    /// Full source code of the parsing state
    pub source: &'static BStr,
    pub line_col_lut: RwLock<Option<LineColLookup<'static>>>
}

impl Eq for ParserContext {}

impl PartialEq for ParserContext {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        self.state == other.state
            && self.current_filename == other.current_filename
            && self.context_name == other.context_name
            && self.source == other.source
            && self.options == other.options
    }
}

impl Clone for ParserContext {
    fn clone(&self) -> Self {
        panic!();

        Self {
            current_filename: self.current_filename.clone(),
            context_name: self.context_name.clone(),
            state: self.state,
            source: self.source,
            options: self.options.clone(),
            line_col_lut: RwLock::default() /* no need to copy paste the datastructure if it is never used */
        }
    }
}

// impl Default for ParserContext {
// fn default() -> Self {
// ParserContext {
// current_filename: None,
// context_name: None,
// search_path: Default::default(),
// read_referenced_files: true,
// parse_warning: Default::default(),
// state: ParsingState::Standard,
// dotted_directive: false,
// source: &NO_CODE,
// show_progress: false
// }
// }
// }

impl ParserContext {
    pub fn clone_with_state(&self, state: ParsingState) -> Self {
        Self {
            current_filename: self.current_filename.clone(),
            context_name: self.context_name.clone(),
            source: self.source,
            options: self.options.clone(),
            line_col_lut: Default::default(), // no need to duplicate the structure
            state
        }
    }
}

#[allow(missing_docs)]
impl ParserContext {
    #[inline]
    pub fn context_name(&self) -> Option<&str> {
        self.context_name.as_deref()
    }

    #[inline]
    pub fn filename(&self) -> Option<&Utf8Path> {
        self.current_filename.as_ref().map(|p| p.as_path())
    }

    //#[deprecated(note="Totally unsafe. Every test should be modified to not use it")]
    #[inline]
    pub fn build_span<S: ?Sized + AsRef<[u8]>>(&self, src: &S) -> Z80Span {
        Z80Span::new_extra(src, self)
    }

    /// Specify the path that contains the code
    #[inline]
    pub fn set_current_filename<P: Into<Utf8PathBuf>>(&mut self, file: P) {
        let file = file.into();
        self.current_filename = Some(
            file.canonicalize()
                .map(|p| Utf8PathBuf::from_path_buf(p).unwrap())
                .unwrap_or(file)
        )
    }

    #[inline]
    pub fn remove_filename(&mut self) {
        self.current_filename = None;
    }

    #[inline]
    pub fn set_context_name(&mut self, name: &str) {
        self.context_name = Some(name.to_owned());
    }

    #[inline]
    pub fn complete_source(&self) -> &str {
        unsafe { std::mem::transmute(self.source.deref()) }
    }

    #[inline(always)]
    pub fn options(&self) -> &ParserOptions {
        &self.options
    }

    #[inline]
    pub fn state(&self) -> &ParsingState {
        &self.state
    }

    #[inline]
    pub fn relative_line_and_column(&self, offset: usize) -> (usize, usize) {
        if self.line_col_lut.read().unwrap().is_none() {
            let src: &'static str = unsafe { std::mem::transmute(self.source.deref()) };

            self.line_col_lut
                .write()
                .unwrap()
                .replace(LineColLookup::new(src));
        }

        let res = self
            .line_col_lut
            .read()
            .unwrap()
            .as_ref()
            .unwrap()
            .get(offset);

        res
    }
}
// pub(crate) static DEFAULT_CTX: ParserContext = ParserContext {
// context_name: None,
// current_filename: None,
// read_referenced_files: false,
// search_path: Vec::new(),
// parse_warning: Default::default()
// };

#[cfg(test)]
mod test_super {
    use super::*;

    #[test]
    fn test_function_state() {
        assert!(Token::Return(0.into()).is_accepted(&ParsingState::FunctionLimited));
    }
    #[test]

    fn test_normal_state() {
        assert!(!Token::Return(0.into()).is_accepted(&ParsingState::Standard));
    }
}