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
use std::sync::Arc;

/// `ParseContainer` is intermediate representation to parse.
///
/// The `ParseContainer` has a shared pointer of the original `String` and the pair of sub-string position (`begin` and `end`),
#[derive(Clone, Debug)]
pub struct ParseContainer {
    raw: Arc<String>,
    begin: usize,
    end: usize,
}

impl ParseContainer {
    pub fn new(raw: Arc<String>) -> Self {
        let end = raw.len();
        Self { raw, begin: 0, end }
    }

    pub fn is_empty(&self) -> bool {
        self.as_str().is_empty()
    }

    pub fn len(&self) -> usize {
        self.as_str().len()
    }

    /// sub-string
    pub fn as_str(&self) -> &str {
        &self.raw[self.begin..self.end]
    }

    /// split into two sub-strings by `step`/
    pub fn split_at(&self, step: usize) -> (Self, Self) {
        let rest = Self {
            begin: self.begin + step,
            ..self.clone()
        };

        let parsed = Self {
            end: self.begin + step,
            ..self.clone()
        };

        (rest, parsed)
    }
}

impl PartialEq<ParseContainer> for ParseContainer {
    fn eq(&self, rhs: &ParseContainer) -> bool {
        self.as_str() == rhs.as_str()
    }
}

impl PartialEq<&str> for ParseContainer {
    fn eq(&self, rhs: &&str) -> bool {
        self.as_str() == *rhs
    }
}

impl PartialEq<ParseContainer> for &str {
    fn eq(&self, rhs: &ParseContainer) -> bool {
        *self == rhs.as_str()
    }
}

impl std::borrow::Borrow<str> for ParseContainer {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl std::fmt::Display for ParseContainer {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        self.as_str().fmt(formatter)
    }
}

impl From<&str> for ParseContainer {
    fn from(s: &str) -> Self {
        Self::new(Arc::new(s.into()))
    }
}

impl From<Vec<ParseContainer>> for ParseContainer {
    fn from(x: Vec<ParseContainer>) -> Self {
        if x.is_empty() {
            return Self::new(Arc::new("".into()));
        }

        let x0 = x.get(0).unwrap();
        let len = x.iter().fold(0, |stack, item| stack + item.len());

        Self {
            end: x0.begin + len,
            ..x0.clone()
        }
    }
}

/// wrap nom's parser
macro_rules! wr {
    ($func:expr) => {
        |input: ParseContainer| {
            use std::sync::Arc;

            let input_str = input.as_str();
            let (_rest, parsed) =
                $func(input_str).map_err(|e: nom::Err<nom::error::Error<&str>>| {
                    e.map_input(|x| ParseContainer::new(Arc::new(x.to_owned())))
                })?;
            let (rest, parsed) = input.split_at(parsed.len());
            Ok((rest, parsed))
        }
    };
}

pub(crate) use wr;