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
use super::data::{Atom, GroupKind};
use super::loc::{Position, Span, Spanned};
use super::tokenizer::{Token, TokenError, Tokenizer, TokenizerConfig};

/// Element of S-Expr
#[derive(Debug, Clone)]
pub enum Element<'a> {
    Group(GroupKind, Vec<SpannedElement<'a>>),
    Atom(Atom<'a>),
    Comment(&'a str),
}

impl<'a> Element<'a> {
    /// Return the atom if the element is an atom, otherwise None
    pub fn atom(&self) -> Option<&Atom<'a>> {
        match self {
            Element::Group(_, _) => None,
            Element::Comment(_) => None,
            Element::Atom(a) => Some(a),
        }
    }

    /// Return the group elements if the element is a group of the right type, otherwise None
    pub fn group(&self, grp: GroupKind) -> Option<&[SpannedElement<'a>]> {
        match self {
            Element::Group(got_grp, elements) if *got_grp == grp => Some(elements),
            Element::Group(_, _) => None,
            Element::Comment(_) => None,
            Element::Atom(_) => None,
        }
    }

    /// Return the group elements if the element is a paren group, otherwise None
    pub fn paren(&self) -> Option<&[SpannedElement<'a>]> {
        self.group(GroupKind::Paren)
    }

    /// Return the group elements if the element is a bracket group, otherwise None
    pub fn bracket(&self) -> Option<&[SpannedElement<'a>]> {
        self.group(GroupKind::Bracket)
    }

    /// Return the group elements if the element is a brace group, otherwise None
    pub fn brace(&self) -> Option<&[SpannedElement<'a>]> {
        self.group(GroupKind::Brace)
    }
}

/// Spanned Element
pub type SpannedElement<'a> = Spanned<Element<'a>>;

/// S-Expr Parser
pub struct Parser<'a> {
    tokenizer: Tokenizer<'a>,
}

/// Parser Error, which are either token error or some error related to group balancing
/// like unterminated group, or mismatch of group
#[derive(Debug, Clone)]
pub enum ParserError {
    UnbalancedEmpty(Position, GroupKind),
    UnbalancedMismatch {
        span: Span,
        expected: GroupKind,
        got: GroupKind,
    },
    UnfinishedGroup(GroupKind),
    TokenizerError(TokenError),
}

impl From<TokenError> for ParserError {
    fn from(t: TokenError) -> ParserError {
        ParserError::TokenizerError(t)
    }
}

impl<'a> Parser<'a> {
    pub fn new_with_config(data: &'a str, cfg: TokenizerConfig) -> Self {
        Parser {
            tokenizer: Tokenizer::new_with_config(data, cfg),
        }
    }

    pub fn new(data: &'a str) -> Self {
        Parser {
            tokenizer: Tokenizer::new(data),
        }
    }

    pub fn next(&mut self) -> Result<Option<SpannedElement<'a>>, ParserError> {
        let mut out: Vec<(GroupKind, Span, Vec<SpannedElement<'a>>)> = vec![];
        loop {
            match self.tokenizer.next()? {
                None => match out.last() {
                    None => return Ok(None),
                    Some((grp, _, _)) => return Err(ParserError::UnfinishedGroup(*grp)),
                },
                Some(tok) => match tok.inner {
                    Token::Comment(comment) => {
                        let el = Spanned {
                            span: tok.span,
                            inner: Element::Comment(comment),
                        };
                        match out.last_mut() {
                            None => return Ok(Some(el)),
                            Some((_, _, elements)) => {
                                elements.push(el);
                            }
                        }
                    }
                    Token::Atom(atom) => {
                        let el = Spanned {
                            span: tok.span,
                            inner: Element::Atom(atom),
                        };
                        match out.last_mut() {
                            None => return Ok(Some(el)),
                            Some((_, _, elements)) => {
                                elements.push(el);
                            }
                        }
                    }
                    Token::Left(grp) => {
                        // create a new group
                        out.push((grp, tok.span, Vec::new()));
                    }
                    Token::Right(grp) => match out.pop() {
                        None => {
                            return Err(ParserError::UnbalancedEmpty(tok.span.start, grp));
                        }
                        Some((inner_grp, inner_start, inner_elements)) => {
                            if inner_grp != grp {
                                return Err(ParserError::UnbalancedMismatch {
                                    span: inner_start.extend(&tok.span),
                                    expected: inner_grp,
                                    got: grp,
                                });
                            }
                            let inner = Spanned {
                                span: inner_start.extend(&tok.span),
                                inner: Element::Group(grp, inner_elements),
                            };
                            match out.last_mut() {
                                None => return Ok(Some(inner)),
                                Some((_, _, elements)) => {
                                    elements.push(inner);
                                }
                            }
                        }
                    },
                },
            }
        }
    }
}