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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
use crate::{
    lisp,
    model::{FloatType, IntType, List, Symbol, Value},
};

use std::fmt::Display;

/// Parse a string of Lisp code into a series of s-expressions. There
/// are more than one expressions when the base string has more than one
/// independent parenthesized lists at its root.
pub fn parse(code: &str) -> impl Iterator<Item = Result<Value, ParseError>> + '_ {
    let mut index = 0;
    index = consume_whitespace_and_comments(code, index);

    std::iter::from_fn(move || {
        if let Some(res) = parse_expression(code, index) {
            if let Ok(res) = res {
                index = res.index;
                index = consume_whitespace_and_comments(code, index);

                Some(Ok(res.parsed.into_value()))
            } else {
                Some(Err(res.unwrap_err()))
            }
        } else {
            None // TODO: Err if we don't parse the whole input?
        }
    })
}

/// A slightly more convenient data structure for building the parse tree, before
/// eventually converting it into proper s-expressions.
#[derive(Debug, Clone)]
enum ParseTree {
    Atom(Value),
    List(Vec<ParseTree>),
    Quoted(Box<ParseTree>),
    Comma(Box<ParseTree>),
}

impl ParseTree {
    pub fn into_value(self) -> Value {
        match self {
            ParseTree::Atom(value) => value,
            ParseTree::List(vec) => Value::List(
                vec.into_iter()
                    .map(|parse_tree| parse_tree.into_value())
                    .collect::<List>(),
            ),
            ParseTree::Quoted(inner) => lisp! { (quote {inner.into_value()}) },
            ParseTree::Comma(inner) => lisp! { (comma {inner.into_value()}) },
        }
    }
}

/**
 * An error that occurred while parsing a string as lisp code
 */
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
    pub msg: String,
    // pub line: i32,
}

impl Display for ParseError {
    fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        return write!(formatter, "Parse error: {}", self.msg);
    }
}

#[derive(Clone, Debug)]
struct ParsedAndIndex {
    pub parsed: ParseTree,
    pub index: usize,
}

type ParseResult = Option<Result<ParsedAndIndex, ParseError>>;
type ConsumeResult = Option<usize>;

fn parse_expression(code: &str, index: usize) -> ParseResult {
    for func in [parse_list, parse_atom] {
        let res = func(code, index);

        if res.is_some() {
            return res;
        }
    }

    None
}

fn parse_list(code: &str, index: usize) -> ParseResult {
    let mut index = consume(code, index, "(")?;
    let mut members = vec![];

    index = consume_whitespace_and_comments(code, index);

    while let Some(res) = parse_expression(code, index) {
        if let Ok(res) = res {
            index = res.index;
            members.push(res.parsed);
            index = consume_whitespace_and_comments(code, index);
        } else {
            return Some(res);
        }
    }

    if let Some(index) = consume(code, index, ")") {
        Some(Ok(ParsedAndIndex {
            parsed: ParseTree::List(members),
            index,
        }))
    } else {
        Some(Err(ParseError {
            msg: format!("Unclosed list at index {}", index),
        }))
    }
}

fn parse_atom(code: &str, index: usize) -> ParseResult {
    for func in [
        parse_quoted,
        parse_comma,
        parse_nil,
        parse_false,
        parse_true,
        parse_number,
        parse_string,
        parse_symbol,
    ] {
        let res = func(code, index);

        if res.is_some() {
            return res;
        }
    }

    None
}

fn parse_quoted(code: &str, index: usize) -> ParseResult {
    let index = consume(code, index, "'")?;
    let res = parse_expression(code, index)?;

    if let Ok(ParsedAndIndex { parsed, index }) = res {
        Some(Ok(ParsedAndIndex {
            parsed: ParseTree::Quoted(Box::new(parsed)),
            index,
        }))
    } else {
        Some(res)
    }
}

fn parse_comma(code: &str, index: usize) -> ParseResult {
    let index = consume(code, index, ",")?;
    let res = parse_expression(code, index)?;

    if let Ok(ParsedAndIndex { parsed, index }) = res {
        Some(Ok(ParsedAndIndex {
            parsed: ParseTree::Comma(Box::new(parsed)),
            index,
        }))
    } else {
        Some(res)
    }
}

fn parse_nil(code: &str, index: usize) -> ParseResult {
    let index = consume(code, index, "nil")?;

    if next_char_is_break(code, index) {
        Some(Ok(ParsedAndIndex {
            parsed: ParseTree::Atom(Value::NIL),
            index,
        }))
    } else {
        None
    }
}

fn parse_false(code: &str, index: usize) -> ParseResult {
    let index = consume(code, index, "f")?;

    if next_char_is_break(code, index) {
        Some(Ok(ParsedAndIndex {
            parsed: ParseTree::Atom(Value::False),
            index,
        }))
    } else {
        None
    }
}

fn parse_true(code: &str, index: usize) -> ParseResult {
    let index = consume(code, index, "t")?;

    if next_char_is_break(code, index) {
        Some(Ok(ParsedAndIndex {
            parsed: ParseTree::Atom(Value::True),
            index,
        }))
    } else {
        None
    }
}

fn parse_number(code: &str, index: usize) -> ParseResult {
    let (front_last_index, front_last_char) = consume_while(code, index, |(index, ch)| {
        (index == 0 && ch == '-') || ch.is_numeric()
    })?;

    if front_last_char.is_numeric() {
        let front_last_index = front_last_index + 1;

        let back_end = consume_while(code, front_last_index, |(index, ch)| {
            (index == 0 && ch == '.') || ch.is_numeric()
        });

        if let Some((back_last_index, _)) = back_end {
            let back_last_index = back_last_index + 1;

            if back_last_index >= front_last_index + 2 {
                if next_char_is_break(code, back_last_index) {
                    if let Ok(float) = code
                        .get(index..back_last_index)
                        .unwrap_or("")
                        .parse::<FloatType>()
                    {
                        return Some(Ok(ParsedAndIndex {
                            parsed: ParseTree::Atom(Value::Float(float)),
                            index: back_last_index,
                        }));
                    }
                }
            } else if code.as_bytes().get(back_last_index - 1) == Some(&b'.') {
                return Some(Err(ParseError {
                    msg: format!(
                        "Expected decimal value after '.' at index {}",
                        back_last_index - 1
                    ),
                }));
            }
        }

        if next_char_is_break(code, front_last_index) {
            if let Ok(int) = code
                .get(index..front_last_index)
                .unwrap_or("")
                .parse::<IntType>()
            {
                return Some(Ok(ParsedAndIndex {
                    parsed: ParseTree::Atom(Value::Int(int)),
                    index: front_last_index,
                }));
            }
        }
    }

    None
}

fn parse_string(code: &str, index: usize) -> ParseResult {
    let (last_index, _) = consume_while(code, index, |(index, ch)| {
        (index == 0 && ch == '"') || (index > 0 && ch != '"')
    })?;

    if last_index > index {
        if code.as_bytes().get(last_index + 1) == Some(&b'"') {
            Some(Ok(ParsedAndIndex {
                parsed: ParseTree::Atom(Value::String(
                    code.get(index + 1..last_index + 1).unwrap_or("").to_owned(),
                )),
                index: last_index + 2,
            }))
        } else {
            Some(Err(ParseError {
                msg: format!("Unclosed string at index {}", last_index),
            }))
        }
    } else {
        None
    }
}

fn parse_symbol(code: &str, index: usize) -> ParseResult {
    let (last_index, _) = consume_while(code, index, |(index, ch)| {
        (index == 0 && is_symbol_start(ch)) || (index > 0 && is_symbolic(ch))
    })?;
    let last_index = last_index + 1;

    if last_index > index {
        Some(Ok(ParsedAndIndex {
            parsed: ParseTree::Atom(Value::Symbol(Symbol(
                code.get(index..last_index).unwrap_or("").to_owned(),
            ))),
            index: last_index,
        }))
    } else {
        None
    }
}

fn consume(code: &str, index: usize, s: &str) -> ConsumeResult {
    let slice = code.get(index..).unwrap_or("");

    if slice.len() >= s.len()
        && slice
            .chars()
            .zip(s.chars())
            .all(|(a, b)| a.to_ascii_lowercase() == b.to_ascii_lowercase())
    {
        return Some(index + s.len());
    } else {
        return None;
    }
}

fn consume_whitespace_and_comments(code: &str, index: usize) -> usize {
    let mut semicolons = 0;

    consume_while(code, index, move |(_, ch)| {
        if ch == ';' {
            semicolons += 1;
        } else if semicolons < 2 || ch == '\n' {
            semicolons = 0;
        }

        return ch.is_whitespace() || ch == ';' || semicolons >= 2;
    })
    .map(|(index, _)| index + 1)
    .unwrap_or(index)
}

fn consume_while<F: FnMut((usize, char)) -> bool>(
    code: &str,
    index: usize,
    mut pred: F,
) -> Option<(usize, char)> {
    code.get(index..)
        .unwrap_or("")
        .char_indices()
        .take_while(|(i, c)| pred((*i, *c)))
        .last()
        .map(|(last_index, ch)| (last_index + index, ch))
}

fn is_symbol_start(c: char) -> bool {
    !c.is_numeric() && is_symbolic(c)
}

fn is_symbolic(c: char) -> bool {
    !c.is_whitespace() && !SPECIAL_TOKENS.iter().any(|t| *t == c)
}

fn next_char_is_break(code: &str, index: usize) -> bool {
    code.get(index..)
        .map(|s| s.chars().next())
        .flatten()
        .map(|ch| ch.is_whitespace() || SPECIAL_TOKENS.iter().any(|t| *t == ch))
        .unwrap_or(true)
}

const SPECIAL_TOKENS: [char; 5] = ['(', ')', '\'', ',', ';'];