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
//! Trees

use crate::choices::{Choice, First, Second};
use crate::span::Spanned;
use crate::symbols::{Equivalence, Metasymbol, TerminalSymbol};
use std::fmt;

/// Leaf Node
pub type Leaf<O = ()> = TerminalSymbol<O>;
/// Internal Node
pub type Internal<V, S, O = ()> = Equivalence<(V, Option<O>), Box<Choice<AST<V, S, O>>>>;

impl<V, S, O> Internal<V, S, O> {
    pub fn from_first(value: (V, Option<O>), l: AST<V, S, O>, r: AST<V, S, O>) -> Self {
        Equivalence::new(value, Box::new((l, r).into()))
    }

    pub fn from_second(value: (V, Option<O>), e: AST<V, S, O>) -> Self {
        Equivalence::new(value, Box::new(e.into()))
    }

    pub fn as_first(&self) -> Option<&First<AST<V, S, O>>> {
        self.equal.as_first()
    }

    pub fn as_second(&self) -> Option<&Second<AST<V, S, O>>> {
        self.equal.as_second()
    }

    pub fn into_first(self) -> Option<First<AST<V, S, O>>> {
        self.equal.into_first()
    }

    pub fn into_second(self) -> Option<Second<AST<V, S, O>>> {
        self.equal.into_second()
    }
}

#[derive(Clone, Debug, PartialEq)]
pub enum Node<V, S, O = ()> {
    /// Leaf Node
    Leaf(Leaf<O>),
    /// Internal Node
    Internal(Internal<V, S, O>),
}

impl<V, S, O> From<Leaf<O>> for Node<V, S, O> {
    fn from(leaf: Leaf<O>) -> Self {
        Self::Leaf(leaf)
    }
}

impl<V, S, O> From<Internal<V, S, O>> for Node<V, S, O> {
    fn from(internal: Internal<V, S, O>) -> Self {
        Self::Internal(internal)
    }
}

impl<V, S, O> Node<V, S, O> {
    pub fn as_leaf(&self) -> Option<&Leaf<O>> {
        match self {
            Self::Leaf(leaf) => Some(leaf),
            Self::Internal(_) => None,
        }
    }

    pub fn as_internal(&self) -> Option<&Internal<V, S, O>> {
        match self {
            Self::Leaf(_) => None,
            Self::Internal(internal) => Some(internal),
        }
    }

    pub fn into_leaf(self) -> Option<Leaf<O>> {
        match self {
            Self::Leaf(leaf) => Some(leaf),
            Self::Internal(_) => None,
        }
    }

    pub fn into_internal(self) -> Option<Internal<V, S, O>> {
        match self {
            Self::Leaf(_) => None,
            Self::Internal(internal) => Some(internal),
        }
    }
}

/// AST
pub type AST<V, S, O = ()> = Spanned<Node<V, S, O>, S>;
/// CST
pub type CST<V, S, O = ()> = Spanned<Equivalence<V, Choice<AST<V, S, O>>>, S>;

impl<V, S, O> AST<V, S, O> {
    // Leaf Node
    pub fn from_leaf(leaf: Leaf<O>, span: S) -> Self {
        Self::new(leaf.into(), span)
    }

    // Internal Node
    pub fn from_internal(internal: Internal<V, S, O>, span: S) -> Self {
        Self::new(internal.into(), span)
    }

    pub fn from_cst_and_output(cst: CST<V, S, O>, output: Option<O>) -> Self {
        match cst.node.equal {
            Choice::First(first) => Self::from_internal(
                Internal::from_first((cst.node.value, output), first.lhs, first.rhs),
                cst.span,
            ),
            Choice::Second(second) => Self::from_internal(
                Internal::from_second((cst.node.value, output), second.0),
                cst.span,
            ),
        }
    }

    pub fn from_cst(cst: CST<V, S, O>) -> Self {
        Self::from_cst_and_output(cst, None)
    }

    pub fn as_leaf(&self) -> Option<&Leaf<O>> {
        self.node.as_leaf()
    }

    pub fn as_internal(&self) -> Option<&Internal<V, S, O>> {
        self.node.as_internal()
    }

    pub fn into_leaf(self) -> Option<Leaf<O>> {
        self.node.into_leaf()
    }

    pub fn into_internal(self) -> Option<Internal<V, S, O>> {
        self.node.into_internal()
    }

    pub fn as_first(&self) -> Option<&First<AST<V, S, O>>> {
        self.as_internal().and_then(|n| n.as_first())
    }

    pub fn as_second(&self) -> Option<&Second<AST<V, S, O>>> {
        self.as_internal().and_then(|n| n.as_second())
    }

    pub fn into_first(self) -> Option<First<AST<V, S, O>>> {
        self.into_internal().and_then(|n| n.into_first())
    }

    pub fn into_second(self) -> Option<Second<AST<V, S, O>>> {
        self.into_internal().and_then(|n| n.into_second())
    }

    pub fn as_original(&self) -> Option<&O> {
        self.as_leaf().and_then(|n| n.as_original())
    }

    pub fn as_metasymbol(&self) -> Option<&Metasymbol> {
        self.as_leaf().and_then(|n| n.as_metasymbol())
    }

    pub fn into_original(self) -> Option<O> {
        self.into_leaf().and_then(|n| n.into_original())
    }

    pub fn into_metasymbol(self) -> Option<Metasymbol> {
        self.into_leaf().and_then(|n| n.into_metasymbol())
    }
}

impl<V: fmt::Debug, S, O: fmt::Debug> AST<V, S, O> {
    fn write_tree(&self, f: &mut fmt::Formatter<'_>, mut prefix: String) -> fmt::Result {
        match &self.node {
            Node::Leaf(leaf) => match leaf {
                TerminalSymbol::Original(o) => {
                    writeln!(f, "{:?}", o)
                }
                TerminalSymbol::Metasymbol(m) => {
                    writeln!(f, "{:?}", m)
                }
            },
            Node::Internal(internal) => {
                writeln!(f, "{:?}", internal.value.0)?;
                match &*internal.equal {
                    Choice::First(first) => {
                        // Left
                        write!(f, "{}├── ", prefix)?;
                        let mut lhs_prefix = prefix.clone();
                        lhs_prefix.push('|');
                        first.lhs.write_tree(f, lhs_prefix)?;
                        // Right
                        write!(f, "{}└── ", prefix)?;
                        prefix.push(' ');
                        first.rhs.write_tree(f, prefix)
                    }
                    Choice::Second(second) => {
                        write!(f, "{}└── ", prefix)?;
                        prefix.push(' ');
                        second.0.write_tree(f, prefix)
                    }
                }
            }
        }
    }
}

impl<V: fmt::Debug, S, O: fmt::Debug> fmt::Display for AST<V, S, O> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.write_tree(f, String::new())
    }
}

impl<V, S, O> CST<V, S, O> {
    pub fn as_first(&self) -> Option<&First<AST<V, S, O>>> {
        self.node.equal.as_first()
    }

    pub fn as_second(&self) -> Option<&Second<AST<V, S, O>>> {
        self.node.equal.as_second()
    }

    pub fn into_first(self) -> Option<First<AST<V, S, O>>> {
        self.node.equal.into_first()
    }

    pub fn into_second(self) -> Option<Second<AST<V, S, O>>> {
        self.node.equal.into_second()
    }
}

impl<O, V, S: Clone> CST<V, S, O> {
    pub fn into_omit(mut self) -> Self {
        self.node.equal = AST::from_leaf(Metasymbol::Omit.into(), self.span.clone()).into();
        self
    }
}