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
use super::*;
use nyar_error::{FileSpan, Validation};

mod display;

/// Pure text of a string literal.
#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StringTextNode {
    /// The unescaped text of the string.
    pub text: String,
    /// The range of the node
    pub span: Range<u32>,
}

/// `handler"text"`, a string literal with a handler.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StringLiteralNode {
    /// The raw string of the number.
    pub literal: StringTextNode,
    /// The unit of the number, if any.
    pub handler: Option<IdentifierNode>,
}

impl ValkyrieNode for StringTextNode {
    fn get_range(&self) -> Range<usize> {
        Range { start: self.span.start as usize, end: self.span.end as usize }
    }
}
impl ValkyrieNode for StringLiteralNode {
    fn get_range(&self) -> Range<usize> {
        match &self.handler {
            Some(s) => Range { start: s.span.get_start(), end: self.literal.span.end as usize },
            None => self.literal.get_range(),
        }
    }
}

impl StringTextNode {
    /// Create a new raw text node
    pub fn new<S: ToString>(value: S, span: Range<u32>) -> Self {
        Self { text: value.to_string(), span }
    }
    /// Convert to an identifier
    pub fn as_identifier(&self) -> IdentifierNode {
        IdentifierNode { name: self.text.clone(), span: FileSpan::default().with_range(self.get_range()) }
    }
}

impl StringLiteralNode {
    /// Convert to a raw string
    pub fn as_raw(&self) -> StringTextNode {
        self.literal.clone()
    }

    /// Attack a handler to the unit of the number.
    pub fn with_handler(self, handler: IdentifierNode) -> Self {
        Self { handler: Some(handler), ..self }
    }
}