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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
//! The lexer for the OftLisp reader.

#[cfg(test)]
mod test;

use std::borrow::Cow;
use std::char;
use std::fmt::{Display, Formatter, Result as FmtResult, Write};
use std::path::PathBuf;

use gc::Gc;
use smallvec::SmallVec;
use unicode_segmentation::UnicodeSegmentation;

use reader::{AcceptSet, ReadError, ReadErrorKind, SourceLocation};

/// The unit of lexical syntax.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Lexeme<'a> {
    /// An closing bracket.
    BracketClose,

    /// An opening bracket.
    BracketOpen,

    /// A comment. The numeric parameter is the number of "extra" semicolons
    /// used.
    ///
    /// For example, the comment
    ///
    /// ```text
    /// ;; foo
    /// ```
    ///
    /// would become:
    ///
    /// ```text
    /// Lexeme::Comment(1, " foo")
    /// ```
    Comment(usize, &'a str),

    /// A center dot (U+2022), which is used for dotted pairs.
    Dot,

    /// An closing parenthesis.
    ParenClose,

    /// An opening parenthesis.
    ParenOpen,

    /// A single quote.
    Quote,

    /// A backtick.
    Quasiquote,

    /// A string.
    String(Gc<String>),

    /// A symbol or number. The disambiguation is performed when constructing
    /// the CST (the `Vec<Value>`).
    Symbolish(&'a str),

    /// A comma, possibly followed by an at-sign. The value is `true` if `,@`
    /// was read, `false` for just `,`.
    Unquote(bool),
}

impl<'a> Display for Lexeme<'a> {
    fn fmt(&self, fmt: &mut Formatter) -> FmtResult {
        match *self {
            Lexeme::BracketClose => fmt.write_char(']'),
            Lexeme::BracketOpen => fmt.write_char('['),
            Lexeme::Comment(n, s) => {
                fmt.write_char(';')?;
                for _ in 0..n {
                    fmt.write_char(';')?;
                }
                fmt.write_str(s)
            }
            Lexeme::Dot => fmt.write_char('\u{2022}'),
            Lexeme::ParenClose => fmt.write_char(')'),
            Lexeme::ParenOpen => fmt.write_char('('),
            Lexeme::Quote => fmt.write_char('\''),
            Lexeme::Quasiquote => fmt.write_char('`'),
            Lexeme::String(ref s) => Display::fmt(s, fmt),
            Lexeme::Symbolish(s) => fmt.write_str(s),
            Lexeme::Unquote(splicing) => {
                fmt.write_char(',')?;
                if splicing {
                    fmt.write_char('@')
                } else {
                    Ok(())
                }
            }
        }
    }
}

/// A single parsed lexeme, including metadata.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct Token<'a> {
    /// The lexeme.
    pub lexeme: Lexeme<'a>,

    /// The position in the source file the lexeme starts at.
    pub start: usize,

    /// The position in the source file the lexeme ends at.
    pub end: usize,
}

/// An iterator that converts a `&'a str` to a token iterator.
pub struct Lexer<'a> {
    /// The string being lexed.
    buf: &'a str,

    /// The position in bytes that the iterator is currently at.
    ///
    /// Invariant: Must be a grapheme cluster boundary in buf.
    pos: usize,

    /// The path of the data being parsed.
    path: Option<Gc<PathBuf>>,
}

impl<'a> Lexer<'a> {
    /// Creates a new Lexer that wraps the given string.
    pub fn new(src: &'a str) -> Lexer<'a> {
        Lexer {
            buf: src,
            pos: 0,
            path: None,
        }
    }

    /// Creates a new Lexer that wraps the given string, with the given path
    /// added for errors.
    pub fn new_path(src: &'a str, path: Option<Gc<PathBuf>>) -> Lexer<'a> {
        Lexer {
            buf: src,
            pos: 0,
            path,
        }
    }

    /// Returns the original input.
    pub fn input(&self) -> &'a str {
        &self.buf
    }

    /// Reads a single grapheme cluster forwards, advancing the iterator's
    /// position.
    pub fn advance(&mut self) {
        let tok = self.peek();
        if let Some(tok) = tok {
            self.pos += tok.len();
        } else {
            warn!("Illegal lexer advance");
        }
    }

    /// Reads a single string character.
    fn parse_strch(&mut self) -> Option<Result<Cow<'a, str>, ReadError>> {
        let ch = if let Some(ch) = self.peek() {
            self.advance();
            ch
        } else {
            return Some(Err(ReadError {
                kind: ReadErrorKind::EOF(AcceptSet::StringChar),
                location: SourceLocation {
                    start: self.pos,
                    end: self.pos,
                    path: self.path.clone(),
                },
            }));
        };
        match ch {
            "\"" => None,
            "\\" => Some(self.parse_stresc()),
            _ => Some(Ok(ch.into())),
        }
    }

    /// Reads a string escape.
    fn parse_stresc(&mut self) -> Result<Cow<'a, str>, ReadError> {
        let ch = if let Some(ch) = self.peek() {
            self.advance();
            ch
        } else {
            return Err(ReadError {
                kind: ReadErrorKind::EOF(AcceptSet::StringEscape),
                location: SourceLocation {
                    start: self.pos,
                    end: self.pos,
                    path: self.path.clone(),
                },
            });
        };
        match ch {
            "a" => Ok("\x07".into()),
            "b" => Ok("\x08".into()),
            "e" => Ok("\x1b".into()),
            "n" => Ok("\n".into()),
            "r" => Ok("\r".into()),
            "t" => Ok("\t".into()),
            "u" => self.parse_strhex(4),
            "U" => self.parse_strhex(8),
            "x" => self.parse_strhex(2),
            "\"" => Ok("\"".into()),
            _ => Err(ReadError {
                kind: ReadErrorKind::Char(ch.to_string(), AcceptSet::StringEscape),
                location: SourceLocation {
                    start: self.pos,
                    end: self.pos,
                    path: self.path.clone(),
                },
            }),
        }
    }

    /// Reads some hex characters from a string escape.
    fn parse_strhex(&mut self, n: usize) -> Result<Cow<'a, str>, ReadError> {
        let c = self.parse_strhex_real(n)?;
        let mut s = String::new();
        s.push(c);
        Ok(s.into())
    }

    /// Reads a hex character from a string escape as its numeric value.
    fn parse_strhex_one(&mut self) -> Result<u8, ReadError> {
        let ch = if let Some(ch) = self.peek() {
            self.advance();
            ch
        } else {
            return Err(ReadError {
                kind: ReadErrorKind::EOF(AcceptSet::HexChar),
                location: SourceLocation {
                    start: self.pos,
                    end: self.pos,
                    path: self.path.clone(),
                },
            });
        };
        match ch {
            "0" => Ok(0),
            "1" => Ok(1),
            "2" => Ok(2),
            "3" => Ok(3),
            "4" => Ok(4),
            "5" => Ok(5),
            "6" => Ok(6),
            "7" => Ok(7),
            "8" => Ok(8),
            "9" => Ok(9),
            "a" | "A" => Ok(10),
            "b" | "B" => Ok(11),
            "c" | "C" => Ok(12),
            "d" | "D" => Ok(13),
            "e" | "E" => Ok(14),
            "f" | "F" => Ok(15),
            _ => Err(ReadError {
                kind: ReadErrorKind::Char(ch.to_string(), AcceptSet::HexChar),
                location: SourceLocation {
                    start: self.pos,
                    end: self.pos,
                    path: self.path.clone(),
                },
            }),
        }
    }

    /// Reads some hex characters from a string escape into a char.
    fn parse_strhex_real(&mut self, n: usize) -> Result<char, ReadError> {
        let mut code = 0u32;
        for _ in 0..n {
            code <<= 4;
            code += self.parse_strhex_one()? as u32;
        }
        char::from_u32(code).ok_or_else(|| unimplemented!("bad char in hex esc"))
    }

    /// Peeks a single grapheme cluster forwards.
    pub fn peek(&self) -> Option<&'a str> {
        self.rest().graphemes(true).next()
    }

    /// Returns the unconsumed portion of the input.
    pub fn rest(&self) -> &'a str {
        &self.buf[self.pos..]
    }

    /// Skips any whitespace characters in the input.
    pub fn skip_whitespace(&mut self) {
        while let Some(tok) = self.peek() {
            if tok.chars().all(|c| c.is_whitespace()) {
                self.pos += tok.len();
            } else {
                return;
            }
        }
    }

    /// Advances a certain number of grapheme clusters, then returns a token
    /// for the given lexeme. For the start position to be correct, assumes
    /// that the position when this function is called is at the start of the
    /// token.
    pub fn token(&mut self, len: usize, lexeme: Lexeme<'a>) -> Token<'a> {
        self.token_fn(len, |_| lexeme)
    }

    /// A version of `token` that constructs the lexeme from the string
    /// covered by `len` grapheme clusters, advancing through it..
    pub fn token_fn<F>(&mut self, len: usize, f: F) -> Token<'a>
    where
        F: FnOnce(&'a str) -> Lexeme<'a>,
    {
        let start = self.pos;
        for _ in 0..len {
            self.advance();
        }
        let end = self.pos;
        let lexeme = f(&self.buf[start..end]);
        Token { lexeme, start, end }
    }

    /// A version of `token` that constructs the lexeme from the string
    /// covered by the grapheme clusters starting at start.
    pub fn token_from<F>(&mut self, start: usize, len: usize, f: F) -> Token<'a>
    where
        F: FnOnce(&'a str) -> Lexeme<'a>,
    {
        let old_pos = self.pos;
        self.pos = start;
        let tok = self.token_fn(len, f);
        self.pos = old_pos;
        tok
    }
}

impl<'a> Iterator for Lexer<'a> {
    type Item = Result<Token<'a>, ReadError>;
    fn next(&mut self) -> Option<Self::Item> {
        self.skip_whitespace();
        let start = self.pos;
        match self.peek() {
            Some("[") => Some(Ok(self.token(1, Lexeme::BracketOpen))),
            Some("]") => Some(Ok(self.token(1, Lexeme::BracketClose))),
            Some("(") => Some(Ok(self.token(1, Lexeme::ParenOpen))),
            Some(")") => Some(Ok(self.token(1, Lexeme::ParenClose))),
            Some("\u{2022}") => Some(Ok(self.token(1, Lexeme::Dot))),
            Some("'") => Some(Ok(self.token(1, Lexeme::Quote))),
            Some("`") => Some(Ok(self.token(1, Lexeme::Quasiquote))),
            Some(",") => {
                self.advance();
                Some(Ok(if self.peek() == Some("@") {
                    self.advance();
                    self.token_from(start, 2, |_| Lexeme::Unquote(true))
                } else {
                    self.token_from(start, 1, |_| Lexeme::Unquote(false))
                }))
            }
            Some(";") => {
                self.advance();
                let mut str_start = start;
                let mut len = 0;
                let mut n = 0;
                while self.peek().map(|s| s == ";").unwrap_or(false) {
                    str_start += 1;
                    n += 1;
                    self.advance();
                }
                while self.peek().map(|s| !is_newline(s)).unwrap_or(false) {
                    len += 1;
                    self.advance();
                }
                let s = &self.buf[(str_start + 1)..(str_start + len + 1)];
                Some(Ok(Token {
                    lexeme: Lexeme::Comment(n, s),
                    start,
                    end: start + len + n + 1,
                }))
            }
            Some("\"") => {
                // Skip the opening paren.
                self.advance();

                // Read the contents of the string.
                let mut string = String::new();
                while let Some(r) = self.parse_strch() {
                    let cow = match r {
                        Ok(c) => c,
                        Err(err) => return Some(Err(err)),
                    };
                    string += cow.as_ref();
                }

                // Return.
                let l = Lexeme::String(Gc::new(string));
                let len = self.pos - start;
                Some(Ok(self.token_from(start, len, |_| l)))
            }
            Some(ch) => Some({
                let mut len = 0;
                while self.peek().map(|s| is_symbolish_str(s)).unwrap_or(false) {
                    len += 1;
                    self.advance();
                }
                if len == 0 {
                    Err(ReadError {
                        kind: ReadErrorKind::Char(ch.to_string(), AcceptSet::Value),
                        location: SourceLocation {
                            start,
                            end: self.pos,
                            path: self.path.clone(),
                        },
                    })
                } else {
                    Ok(self.token_from(start, len, Lexeme::Symbolish))
                }
            }),
            None => None,
        }
    }
}

fn is_newline(s: &str) -> bool {
    s.chars().all(|c| c == '\n' || c == '\r')
}

fn is_symbolish(c: char) -> bool {
    lazy_static! {
        static ref LEGAL: Vec<char> = {
            let letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
            let digits = "0123456789";
            let syms = "+-./$?*#=<>_";

            let mut chars = letters.chars()
                .chain(digits.chars())
                .chain(syms.chars())
                .collect::<Vec<_>>();
            chars.sort();
            chars
        };
    }
    LEGAL.binary_search(&c).is_ok()
}

fn is_symbolish_str(s: &str) -> bool {
    let mut chs: SmallVec<[char; 4]> = SmallVec::new();
    chs.extend(s.chars());
    chs.len() == 1 && is_symbolish(chs[0])
}