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
/*
 * mini-haskell: light-weight Haskell for fun
 * Copyright (C) 2020  Xie Ruifeng
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

//! Haskell layout: see "Haskell 2010 Report, 10.3 Layout".

use super::{Range, LexError, Scanner, Location};
use crate::lexeme::{Lexeme, Lexeme::*, RId::Module};
use crate::utils::Result3::*;
use std::fmt::{Display, Formatter};
use crate::scanner::layout::AugmentedLexeme::{PhantomCloseCurlyBracket, PhantomSemicolon, PhantomOpenCurlyBracket, Real};
use crate::utils::iter::IterStream;
use std::collections::VecDeque;

/// An iterator of lexemes from an [`Input`](crate::input::Input) stream.
pub struct RawLexemeIterator<I: std::io::Read> {
    scanner: Scanner<I>,
    error: Option<LexError>,
}

impl<I: std::io::Read> Iterator for RawLexemeIterator<I> {
    type Item = Lexeme;
    fn next(&mut self) -> Option<Lexeme> {
        self.enriched_next(|_| ()).map(|t| t.0)
    }
}

impl<I: std::io::Read> From<Scanner<I>> for RawLexemeIterator<I> {
    fn from(scanner: Scanner<I>) -> Self {
        Self {
            error: None,
            scanner,
        }
    }
}

impl<I: std::io::Read> RawLexemeIterator<I> {
    /// Create a new lexeme iterator from raw input.
    pub fn new(input: I) -> Self { Self::from(Scanner::new(input)) }
    /// Get back the internal scanner of this iterator.
    pub fn into_scanner(self) -> (Option<LexError>, Scanner<I>) { (self.error, self.scanner) }
    fn enriched_next<T>(&mut self, proc: impl FnOnce(&Scanner<I>) -> T) -> Option<(Lexeme, T)> {
        if self.error.is_some() { return None; }
        // possibly consume whitespaces and ignore errors.
        let _ = self.scanner.whitespace();
        // for the fat iterator to insert a statement to get the location.
        let val = proc(&mut self.scanner);
        // produce a lexeme.
        match self.scanner.next_lexeme() {
            Success(x) => Some((x, val)),
            RetryLater(_) => None,
            FailFast(err) => {
                self.error = Some(err);
                None
            }
        }
    }
}

/// A "fat" lexeme iterator, i.e. iterator for lexemes with their location ranges.
pub struct FatLexemeIterator<I: std::io::Read> {
    iterator: RawLexemeIterator<I>,
    location: Location,
}

impl<I: std::io::Read> Iterator for FatLexemeIterator<I> {
    type Item = (Lexeme, Range);
    fn next(&mut self) -> Option<(Lexeme, Range)> {
        let (x, location) = self.iterator.enriched_next(|s| s.location)?;
        self.location = location;
        Some((x, Range {
            begin: location,
            end: self.iterator.scanner.location,
        }))
    }
}

impl<I: std::io::Read> From<RawLexemeIterator<I>> for FatLexemeIterator<I> {
    fn from(iterator: RawLexemeIterator<I>) -> Self {
        Self {
            location: iterator.scanner.location,
            iterator,
        }
    }
}

impl<I: std::io::Read> FatLexemeIterator<I> {
    /// Create a new lexeme iterator from raw input.
    pub fn new(input: I) -> Self { Self::from(RawLexemeIterator::<I>::new(input)) }
    /// Get back the internal scanner of this iterator.
    pub fn into_scanner(self) -> (Option<LexError>, Scanner<I>) { self.iterator.into_scanner() }
}

enum LastLexeme {
    LetWhereDoOf,
    StartOfFile,
    // this means we have already handled the following lexeme.
    PassThrough,
    Other,
}

/// Enriched lexemes: a normal lexeme, a `{n}`, or an `<n>`.
#[derive(Debug, Eq, PartialEq)]
pub enum EnrichedLexeme {
    /// a `{n}`.
    CurlyN(usize),
    /// an `<n>`.
    AngleN(usize),
    /// a normal lexeme with a source range.
    Normal(Lexeme, Range),
}

impl Display for EnrichedLexeme {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        use EnrichedLexeme::*;
        match self {
            CurlyN(n) => write!(f, "{{{}}}", n),
            AngleN(n) => write!(f, "<{}>", n),
            Normal(lexeme, range) => write!(f, "{}: {}", range, lexeme)
        }
    }
}

impl From<(Lexeme, Range)> for EnrichedLexeme {
    fn from((lexeme, range): (Lexeme, Range)) -> Self {
        EnrichedLexeme::Normal(lexeme, range)
    }
}

/// Lexeme stream enriched with `{n}` and `<n>`.
/// See "Haskell 2010 Report, 10.3 Layout".
pub struct EnrichedLexemeIterator<I: std::io::Read> {
    iterator: IterStream<FatLexemeIterator<I>>,
    last_lexeme: LastLexeme,
    last_line: usize,
}

impl<I: std::io::Read> EnrichedLexemeIterator<I> {
    /// Create a new enriched lexeme iterator from raw input.
    pub fn new(input: I) -> Self { Self::from(FatLexemeIterator::<I>::new(input)) }
    /// Get back the internal scanner of this iterator.
    pub fn into_scanner(self) -> (Option<LexError>, Scanner<I>) { self.iterator.unwrap().into_scanner() }
}

impl<I: std::io::Read> From<FatLexemeIterator<I>> for EnrichedLexemeIterator<I> {
    fn from(iterator: FatLexemeIterator<I>) -> Self {
        Self {
            iterator: IterStream::from(iterator),
            last_lexeme: LastLexeme::StartOfFile,
            last_line: 0,
        }
    }
}

impl<I: std::io::Read> Iterator for EnrichedLexemeIterator<I> {
    type Item = EnrichedLexeme;
    fn next(&mut self) -> Option<Self::Item> {
        use LastLexeme::*;
        use EnrichedLexeme::*;
        let next = self.iterator.peek(0);
        match self.last_lexeme {
            // If a `let`, `where`, `do`, or `of` keyword is not followed by the lexeme `{`
            LetWhereDoOf if next.is_none() || next.unwrap().0 != OpenCurlyBracket => {
                self.last_lexeme = PassThrough;
                // where n is the indentation of the next lexeme if there is one
                // or 0 if the end of file has been reached
                let n = next.map_or(0, |t| t.1.begin.column);
                // the token `{n}` is inserted after the keyword
                Some(CurlyN(n))
            }
            // If the first lexeme of a module is not `{` or `module`
            StartOfFile if next.is_some()
                && ![OpenCurlyBracket, ReservedId(Module)]
                .contains(&next.unwrap().0) => {
                self.last_lexeme = PassThrough;
                // where n is the indentation of the lexeme
                let n = next.unwrap().1.begin.column;
                // then it is preceded by `{n}`
                Some(CurlyN(n))
            }
            // Where the start of a lexeme is preceded only by white space on the same line
            // provided that it is not, as a consequence of the first two rules, preceded by `{n}`
            Other if next.is_some() && next.unwrap().1.begin.line > self.last_line => {
                self.last_line = next.unwrap().1.begin.line;
                // where n is the indentation of the lexeme
                let n = next.unwrap().1.begin.column;
                // this lexeme is preceded by `<n>`
                Some(AngleN(n))
            }
            // otherwise we just return the normal lexeme
            _ => {
                let (lexeme, range) = self.iterator.next()?;
                // update last line for "preceded only by white space on the same line" test
                self.last_line = range.end.line;
                // update last lexeme for "4 keywords not followed by {" test
                use crate::lexeme::Lexeme::ReservedId as R;
                use crate::lexeme::RId::*;
                self.last_lexeme = match lexeme {
                    R(Let) | R(Where) | R(Do) | R(Of) => LetWhereDoOf,
                    _ => Other,
                };
                // return as a normal lexeme
                Some(Normal(lexeme, range))
            }
        }
    }
}

/// Augmented lexemes: normal lexemes or phantom `{`s, `;`s, and `}`s.
pub enum AugmentedLexeme {
    /// Real lexemes.
    Real(Lexeme, Range),
    /// Phantom `{`.
    PhantomOpenCurlyBracket,
    /// Phantom `}`.
    PhantomCloseCurlyBracket,
    /// Phantom `;`.
    PhantomSemicolon,
}

impl Display for AugmentedLexeme {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            Real(t, range) => write!(f, "{}: {}", range, t),
            PhantomOpenCurlyBracket => write!(f, "<phantom>: {{"),
            PhantomCloseCurlyBracket => write!(f, "<phantom>: }}"),
            PhantomSemicolon => write!(f, "<phantom>: ;"),
        }
    }
}

/// Lexeme streams augmented with phantom `{`, `;`, and `}`.
pub struct AugmentedLexemeIterator<I: std::io::Read> {
    iterator: IterStream<EnrichedLexemeIterator<I>>,
    indents: Vec<usize>,
    buffer: VecDeque<AugmentedLexeme>,
}

impl<'a, I: std::io::Read> AugmentedLexemeIterator<I> {
    /// Create a new enriched lexeme iterator from raw input.
    pub fn new(input: I) -> Self { Self::from(EnrichedLexemeIterator::new(input)) }
    /// Get back the internal scanner of this iterator.
    pub fn into_scanner(self) -> (Option<LexError>, Scanner<I>) { self.iterator.unwrap().into_scanner() }

    fn prepare_next(&mut self) {
        let t = self.iterator.next();
        // L [] []                = []
        // L [] (m : ms)          = } : L [] ms if m /= 0 (Note 6)
        // Note 6. At the end of the input, any pending close-braces are inserted.
        // It is an error at this point to be within a non-layout context (i.e. m = 0).
        if t.is_none() {
            if let Some(k) = self.indents.pop() {
                if k == 0 { panic!("mismatched curly brackets.") }
                self.buffer.push_back(PhantomCloseCurlyBracket)
            }
            return;
        }
        use EnrichedLexeme::*;
        match (t.unwrap(), self.indents.last().copied()) {
            // L (<n>: ts) (m : ms)   = ; : (L ts (m : ms)) if m = n
            //                        = } : (L (<n>: ts) ms) if n < m
            (AngleN(n), Some(m)) if m == n =>
                self.buffer.push_back(PhantomSemicolon),
            (AngleN(n), Some(m)) if n < m => {
                self.iterator.put_back(AngleN(n));
                self.indents.pop();
                self.buffer.push_back(PhantomCloseCurlyBracket)
            }
            // L (<n>: ts) ms         = L ts ms
            (AngleN(_), _) => self.prepare_next(),
            // L ({n} : ts) (m : ms)  = { : (L ts (n : m : ms)) if n > m (Note 1)
            // L ({n} : ts) []        = { : (L ts [n]) if n > 0 (Note 1)
            (CurlyN(n), m) if m.is_none() || n > m.unwrap() => {
                self.indents.push(n);
                self.buffer.push_back(PhantomOpenCurlyBracket)
            }
            // L ({n} : ts) ms        = { : } : (L (<n>: ts) ms) (Note 2)
            (CurlyN(n), _) => {
                self.buffer.push_back(PhantomOpenCurlyBracket);
                self.buffer.push_back(PhantomCloseCurlyBracket);
                self.iterator.put_back(AngleN(n))
            }
            // L (} : ts) (0 : ms)    = } : (L ts ms) (Note 3)
            // L (} : ts) ms          = parse-error (Note 3)
            // Note 3.By matching against 0 for the current layout context, we ensure that an
            // explicit close brace can only match an explicit open brace. A parse error results
            // if an explicit close brace matches an implicit open brace.
            (Normal(CloseCurlyBracket, loc), Some(k)) => {
                assert_eq!(k, 0, "mismatched curly brackets.");
                self.indents.pop();
                self.buffer.push_back(Real(CloseCurlyBracket, loc))
            }
            // L ({ : ts) ms          = { : (L ts (0 : ms)) (Note 4)
            (Normal(OpenCurlyBracket, loc), _) => {
                self.indents.push(0);
                self.buffer.push_back(Real(OpenCurlyBracket, loc))
            }
            // L (t : ts) (m : ms)    = } : (L (t : ts) ms) if m /= 0 and parse-error(t) (Note 5)
            // TODO: implement this `parse-error(t)` rule.
            // L (t : ts) ms          = t : (L ts ms)
            (Normal(t, loc), _) => {
                self.buffer.push_back(Real(t, loc))
            }
        }
    }
}

impl<'a, I: std::io::Read> From<EnrichedLexemeIterator<I>> for AugmentedLexemeIterator<I> {
    fn from(iterator: EnrichedLexemeIterator<I>) -> Self {
        AugmentedLexemeIterator {
            iterator: IterStream::from(iterator),
            buffer: VecDeque::new(),
            indents: Vec::new(),
        }
    }
}

impl<'a, I: std::io::Read> Iterator for AugmentedLexemeIterator<I> {
    type Item = AugmentedLexeme;
    fn next(&mut self) -> Option<AugmentedLexeme> {
        self.prepare_next();
        self.buffer.pop_front()
    }
}

#[cfg(test)]
mod tests {
    use indoc::indoc;
    use super::RawLexemeIterator;
    use super::EnrichedLexemeIterator;
    use crate::lexeme::Lexeme::*;
    use crate::lexeme::RId::*;
    use crate::lexeme::ROp::*;

    const TEST_SOURCE: &str = indoc! {r#"
        module Main where
        import Prelude hiding (Integer)
        main :: IO ()
        main = do
            name <- getLine
            putStrLn ("Hello, " <> name <> "!")
            pure ()
    "#};

    #[test]
    fn test_raw_iterator() {
        let mut it = RawLexemeIterator::new(TEST_SOURCE.as_bytes());
        assert!(it.by_ref().eq([
            ReservedId(Module),
            Identifier("Main".to_string()),
            ReservedId(Where),
            ReservedId(Import),
            Identifier("Prelude".to_string()),
            Identifier("hiding".to_string()),
            OpenParenthesis,
            Identifier("Integer".to_string()),
            CloseParenthesis,
            Identifier("main".to_string()),
            ReservedOp(ColonColon),
            Identifier("IO".to_string()),
            OpenParenthesis,
            CloseParenthesis,
            Identifier("main".to_string()),
            ReservedOp(EqualSign),
            ReservedId(Do),
            Identifier("name".to_string()),
            ReservedOp(LeftArrow),
            Identifier("getLine".to_string()),
            Identifier("putStrLn".to_string()),
            OpenParenthesis,
            StringLiteral("Hello, ".to_string()),
            Operator("<>".to_string()),
            Identifier("name".to_string()),
            Operator("<>".to_string()),
            StringLiteral("!".to_string()),
            CloseParenthesis,
            Identifier("pure".to_string()),
            OpenParenthesis,
            CloseParenthesis,
        ].iter().cloned()));
        let (err, _) = it.into_scanner();
        assert_eq!(err, None);
    }

    #[test]
    fn test_enriched_iterator() {
        use expect_test::expect;
        let mut it = EnrichedLexemeIterator::new(TEST_SOURCE.as_bytes());
        let mut res = String::new();
        for t in it.by_ref() { res += &format!("{}\n", t) }
        expect![[r#"
            1:1-1:7: module
            1:8-1:12: Main
            1:13-1:18: where
            {1}
            2:1-2:7: import
            2:8-2:15: Prelude
            2:16-2:22: hiding
            2:23-2:24: (
            2:24-2:31: Integer
            2:31-2:32: )
            <1>
            3:1-3:5: main
            3:6-3:8: ::
            3:9-3:11: IO
            3:12-3:13: (
            3:13-3:14: )
            <1>
            4:1-4:5: main
            4:6-4:7: =
            4:8-4:10: do
            {5}
            5:5-5:9: name
            5:10-5:12: <-
            5:13-5:20: getLine
            <5>
            6:5-6:13: putStrLn
            6:14-6:15: (
            6:15-6:24: "Hello, "
            6:25-6:27: <>
            6:28-6:32: name
            6:33-6:35: <>
            6:36-6:39: "!"
            6:39-6:40: )
            <5>
            7:5-7:9: pure
            7:10-7:11: (
            7:11-7:12: )
        "#]].assert_eq(&res);
        let (err, _) = it.into_scanner();
        assert_eq!(err, None);
    }
}