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
//! optional syntax tree nodes ([`Option`], [`Exists`])
use std::borrow::Cow;
use std::marker::PhantomData;

use crate::syntax::{Epsilon, Metadata, MetadataBuilder, Result as SynResult};
use crate::{Lexicon, Parser, Produce, Production, ToSpan};

use super::Node;

// Option<T> => T | epsilon
#[doc(hidden)]
pub struct OptionProd<T: Production>(PhantomData<T>);

impl<T: Production> Production for OptionProd<T> {
    type L = T::L;
    #[inline]
    fn debug() -> Cow<'static, str> {
        let inner = T::debug();
        if let Some(rest) = inner.strip_prefix('(') {
            if let Some(inner) = rest.strip_suffix(")+") {
                return Cow::Owned(format!("({})*", inner));
            }
            if let Some(inner) = rest.strip_suffix("]+") {
                return Cow::Owned(format!("({}]*", inner));
            }
        }
        Cow::Owned(format!("({})?", T::debug()))
    }

    fn register(meta: &mut MetadataBuilder<Self::L>) {
        crate::register_union!(meta, T, Epsilon<T::L>)
    }
}

/// Node that stores an optional subtree
#[derive(Node, ToSpan, Clone, PartialEq)]
#[doc(alias = "Option")]
pub struct Optional<T: Produce>(pub Node<Option<T>>);

impl<T: std::fmt::Debug + Produce> std::fmt::Debug for Optional<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self.0.value {
            Some(node) => f.debug_tuple("Some").field(&node).finish(),
            None => f.debug_tuple("None").field(&self.0.span).finish(),
        }
    }
}

impl<T: Produce> Produce for Optional<T> {
    type Prod = OptionProd<T::Prod>;
    fn produce(
        parser: &mut Parser<'_, <Self::Prod as Production>::L>,
        meta: &Metadata<<Self::Prod as Production>::L>,
    ) -> SynResult<Self, <Self::Prod as Production>::L> {
        produce_option(parser, meta, |x| x).map(Self::from)
    }
}

/// Node that stores if an optional subtree is produced
#[derive(Node, ToSpan, Clone, PartialEq)]
pub struct Exists<T: Produce>(Node<bool>, PhantomData<T>);

impl<T: Produce> std::fmt::Debug for Exists<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.0.fmt(f)
    }
}

impl<T: Produce> Produce for Exists<T> {
    type Prod = OptionProd<T::Prod>;
    fn produce(
        parser: &mut Parser<'_, <Self::Prod as Production>::L>,
        meta: &Metadata<<Self::Prod as Production>::L>,
    ) -> SynResult<Self, <Self::Prod as Production>::L> {
        produce_option(parser, meta, |x: Option<T>| x.is_some()).map(Self::from)
    }
}
fn produce_option<T, O, F: FnOnce(Option<T>) -> O, L: Lexicon>(
    parser: &mut Parser<'_, L>,
    meta: &Metadata<L>,
    f: F,
) -> SynResult<Node<O>, L>
where
    T: Produce + ToSpan,
    T::Prod: Production<L = L>,
{
    let token = parser.peek_token_src();
    if token.is_none() {
        // produces epsilon
        return SynResult::Success(Node::new(parser.current_span_empty(), f(None)));
    }
    let first = meta.first.get(&T::prod_id());
    if !first.contains(token) {
        // produces epsilon
        return SynResult::Success(Node::new(parser.current_span_empty(), f(None)));
    }
    // if parse fails, delay to parent to panic
    match T::produce(parser, meta) {
        SynResult::Success(t) => SynResult::Success(Node::new(t.span(), f(Some(t)))),
        SynResult::Recovered(t, error) => {
            SynResult::Recovered(Node::new(t.span(), f(Some(t))), error)
        }
        SynResult::Panic(error) => {
            SynResult::Recovered(Node::new(parser.current_span_empty(), f(None)), error)
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::prelude::*;
    use crate::GrammarError;

    use crate::lex::Token;
    use crate::test::prelude::*;
    use crate::test::MathTokenType as T;
    use crate::test::{Ident, OpAdd};

    #[derive_syntax]
    #[teleparse(root)]
    #[derive(Debug, PartialEq, Clone)]
    struct OptIdent(tp::Option<Ident>);

    #[test]
    fn test_none() -> Result<(), GrammarError> {
        let t = OptIdent::parse("+")?.unwrap();
        let t_str = format!("{:?}", t.0);
        assert_eq!(t_str, "None(0)");
        assert_eq!(t, OptIdent(Node::new(0..0, None).into()));

        Ok(())
    }

    #[test]
    fn test_some() {
        let t = OptIdent::parse("a").unwrap().unwrap();
        let t_str = format!("{:?}", t.0);
        assert_eq!(t_str, "Some(token Ident(0..1))");
        assert_eq!(
            t,
            OptIdent(Node::new(0..1, Some(Ident(Token::new(0..1, T::Ident)))).into())
        );
    }

    #[test]
    fn test_use_as_option() -> Result<(), GrammarError> {
        let t = OptIdent::parse("a")?.unwrap();
        assert!(t.0.is_some());

        Ok(())
    }

    #[derive_syntax]
    #[teleparse(root, no_test)]
    struct Seq(tp::Option<OpAdd>, OpAdd);

    #[test]
    fn test_seq_not_ll1() {
        assert_not_ll1!(
            Seq,
            GrammarError::FirstFollowSeqConflict(
                "Seq".to_string(),
                "(+)?".to_string(),
                "+".to_string(),
                "\"+\"".to_string()
            )
        );
    }

    #[derive_syntax]
    #[teleparse(root, no_test)]
    struct Nested(super::Optional<super::Optional<Ident>>);

    #[test]
    fn test_nested_not_ll1() {
        assert_not_ll1!(
            Nested,
            GrammarError::FirstFirstConflict(
                "((Ident)?)?".to_string(),
                "(Ident)?".to_string(),
                "()".to_string(),
                "<empty>".to_string(),
            )
        );
    }

    #[derive_syntax]
    #[teleparse(root)]
    #[derive(Debug, PartialEq, Clone)]
    struct ExistIdent(tp::Exists<Ident>);

    #[test]
    #[allow(clippy::bool_assert_comparison)]
    fn parse_exist() {
        let t = ExistIdent::parse("a").unwrap().unwrap();
        let t_str = format!("{:?}", t.0);
        assert_eq!(t_str, "0..1 => true");
        assert_eq!(t, ExistIdent(Node::new(0..1, true).into()));
        assert_eq!(*t.0, true);

        let t = ExistIdent::parse("+").unwrap().unwrap();
        let t_str = format!("{:?}", t.0);
        assert_eq!(t_str, "0 => false");
        assert_eq!(t, ExistIdent(Node::new(0..0, false).into()));
        assert_eq!(*t.0, false);
    }
}