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
#![doc = include_str!("readme.md")]

use core::{
    fmt::{Debug, Display, Formatter},
    ops::Range,
};

use crate::{IdentifierNode, NumberLiteralNode, StringTextNode};
#[cfg(feature = "lispify")]
pub use lispify::{Lisp, Lispify};
use nyar_error::{NyarError, Validation};
#[cfg(feature = "pretty-print")]
pub use pretty_print::{PrettyPrint, PrettyProvider, PrettyTree};

/// A node in the AST
pub trait ValkyrieNode {
    /// The range of the node
    fn get_range(&self) -> Range<usize>;
    // fn mut_range(&mut self) -> &mut Range<u32>;
    /// Get the start of the node
    fn get_start(&self) -> usize {
        self.get_range().start
    }
    /// Get the end of the node
    fn get_end(&self) -> usize {
        self.get_range().end
    }
}

/// A string interpreter
pub trait StringInterpreter {
    /// The output type of the interpreter
    type Output;
    /// Interpret the string
    fn interpret(&mut self, text: &StringTextNode) -> Validation<Self::Output>;
}

/// A string interpreter
pub trait NumberInterpreter {
    /// The output type of the interpreter
    type Output;
    /// Interpret the string
    fn interpret(&mut self, n: &NumberLiteralNode) -> Result<Self::Output, NyarError>;
}

pub struct WrapDisplay<'a, T> {
    inner: &'a T,
}
impl<'a, T> WrapDisplay<'a, T> {
    pub fn new(wrap: &'a T) -> Self {
        Self { inner: wrap }
    }
}

impl<'a, T: Display> Debug for WrapDisplay<'a, T> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        Display::fmt(self.inner, f)
    }
}

pub(crate) struct IdentifiersDisplay<'i> {
    inner: &'i [IdentifierNode],
}

impl<'i> IdentifiersDisplay<'i> {
    pub fn new(identifiers: &'i [IdentifierNode]) -> Self {
        Self { inner: identifiers }
    }
}
impl<'i> Debug for IdentifiersDisplay<'i> {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        for (index, id) in self.inner.iter().enumerate() {
            if index != 0 {
                f.write_str("∷")?;
            }
            f.write_str(&id.name)?
        }
        Ok(())
    }
}