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
/*
 * 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/>.
 */

//! lexical scanner for mini-haskell.

pub mod basic;
pub mod identifier;
pub mod whitespace;
pub mod numeric;
pub mod char_string;
pub mod special;
pub mod layout;

use std::fmt::{Formatter, Display};
use crate::utils::*;
use crate::utils::Result3::{FailFast, RetryLater};
use crate::utils::char::{CharPredicate, Stream};
use crate::input::Input;
use crate::lexeme::{LexemeType, Lexeme};
use crate::error::{
    Diagnostic, DiagnosticsEngine, DiagnosticMessage::Error,
    Error::{InvalidUTF8, InputFailure, InvalidChar},
};
use crate::scanner::basic::Any;

/// Source location.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Location {
    /// line number, starting from 1.
    pub line: usize,
    /// column number, starting from 1.
    pub column: usize,
    /// offset into the source file, starting from 0.
    pub offset: usize,
}

impl Default for Location {
    fn default() -> Self { Location { line: 1, column: 1, offset: 0 } }
}

impl Display for Location {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.line, self.column)
    }
}

impl Location {
    /// Size of a Tab stop.
    pub const TAB_SIZE: usize = 8;

    /// Create a new location, the same as `Location::default()`.
    pub fn new() -> Self { Self::default() }

    /// Step one character.
    pub fn step(&mut self) {
        self.column += 1;
        self.offset += 1;
    }

    /// Start a new line.
    pub fn newline(&mut self) {
        self.column = 1;
        self.line += 1;
    }

    /// Align to the next tab stop.
    pub fn tablise(&mut self) {
        self.step();
        self.column = round_to(self.column, Self::TAB_SIZE);
    }
}

/// A half-open source range: a pair of `Location`s.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Range {
    /// Where the range begins (inclusive).
    pub begin: Location,
    /// Where the range ends (non-inclusive).
    pub end: Location,
}

impl Display for Range {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}-{}", self.begin, self.end)
    }
}

/// Scanner with a back buffer.
pub struct Scanner<I> {
    input: Input<I>,
    location: Location,
    diagnostics: DiagnosticsEngine,
}

impl<I: std::io::Read> Stream for Scanner<I> {
    fn peek(&mut self) -> Option<char> {
        match self.input.clone().next(|s| Diagnostic::new(
            self.location, Error(InvalidUTF8(Vec::from(s))))
            .report(&mut self.diagnostics)) {
            Ok((c, _)) => Some(c),
            Err(_) => None,
        }
    }

    fn next(&mut self) -> Option<char> {
        let res = self.next_input();
        if let Some(x) = res {
            self.location.step();
            // ANY        -> graphic | whitechar
            if !Any.check(x) {
                Diagnostic::new(self.location, Error(InvalidChar(x)))
                    .report(&mut self.diagnostics);
            }
        }
        res
    }

    fn r#match<'a>(&mut self, s: &'a str) -> Option<&'a str> {
        self.input.clone().r#match(s, |s|
            Diagnostic::new(self.location, Error(InvalidUTF8(Vec::from(s))))
                .report(&mut self.diagnostics),
        ).map(|rest| {
            self.input = rest;
            s
        })
    }
}

impl<I: std::io::Read> Scanner<I> {
    fn next_input(&mut self) -> Option<char> {
        let diagnostics = &mut self.diagnostics;
        let location = self.location;
        match self.input.clone().next(move |s| Diagnostic::new(
            location, Error(InvalidUTF8(Vec::from(s))))
            .report(diagnostics))
            .map_err(Into::into) {
            Ok((c, rest)) => {
                self.input = rest;
                Some(c)
            }
            Err(e) => {
                if let Some(e) = e {
                    Diagnostic::new(self.location, Error(InputFailure(e)))
                        .report(&mut self.diagnostics);
                }
                None
            }
        }
    }

    /// Fail fast with `t` as the expected lexeme type.
    pub fn expected<T>(&mut self, t: LexemeType) -> Result<T> {
        FailFast(self.err_expected(t))
    }

    /// Fail for future recovery from `alt!`.
    pub fn keep_trying<T>() -> Result<T> { RetryLater(()) }

    /// Create a `LexError` with the expected lexeme type.
    pub fn err_expected(&mut self, t: LexemeType) -> LexError {
        LexError { expected: t, unexpected: self.peek() }
    }
}

/// Lexical error.
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct LexError {
    /// The expected lexeme type at the error.
    pub expected: LexemeType,
    /// The character at which tokenization fails.
    pub unexpected: Option<char>,
}

/// Lexer result.
pub type Result<T> = crate::utils::Result3<T, LexError, ()>;

impl<I> Scanner<I> {
    /// Create a new scanner from the back buffer.
    pub fn new(input: I) -> Self {
        Scanner {
            input: Input::new(input),
            location: Location::new(),
            diagnostics: DiagnosticsEngine::new(),
        }
    }

    /// Set an anchor for possible revert in future. Use an `Either` for error indication.
    pub fn anchored<R: Either>(&mut self, f: impl FnOnce(&mut Scanner<I>) -> R) -> R {
        let old_input = self.input.clone();
        let old_location = self.location;
        let old_diagnostics_count = self.diagnostics.len();
        match f(self).into_result() {
            Ok(res) => Either::right(res),
            Err(err) => {
                self.input = old_input;
                self.location = old_location;
                self.diagnostics.truncate(old_diagnostics_count);
                Either::left(err)
            }
        }
    }

    /// Match many of this rule.
    pub fn many<ET: Either<Left=E>, EU: Either<Left=E>, E>(
        &mut self, mut f: impl FnMut(&mut Scanner<I>) -> ET,
        init: EU::Right, mut join: impl FnMut(&mut EU::Right, ET::Right)) -> EU {
        let mut res = init;
        while let Ok(x) = self.anchored(&mut f).into_result() {
            join(&mut res, x);
        }
        Either::right(res)
    }

    /// Match many of this rule, ignore the results.
    pub fn many_<ET: Either<Left=E>, ER: Either<Left=E>, E>(
        &mut self, f: impl FnMut(&mut Scanner<I>) -> ET) -> ER
        where ER::Right: From<()> {
        let unit: ER::Right = From::from(());
        self.many(f, unit, |_, _| ())
    }

    /// Match many of this rule.
    pub fn some<ET: Either<Left=E>, EU: Either<Left=E>, E>(
        &mut self, mut f: impl FnMut(&mut Scanner<I>) -> ET,
        mut init: EU::Right, mut join: impl FnMut(&mut EU::Right, ET::Right)) -> EU {
        join(&mut init, unwrap!(f(self)));
        self.many(f, init, join)
    }

    /// Match many of this rule, ignore the results.
    pub fn some_<ET: Either<Left=E>, ER: Either<Left=E>, E>(
        &mut self, f: impl FnMut(&mut Scanner<I>) -> ET) -> ER
        where ER::Right: From<()> {
        let unit: ER::Right = From::from(());
        self.some(f, unit, |_, _| ())
    }

    /// Match many of this rule separated by some other rule.
    pub fn sep_by<ET: Either<Left=E>, EU: Either<Left=E>, ER: Either<Left=E>, E>(
        &mut self,
        mut f: impl FnMut(&mut Scanner<I>) -> ET,
        mut g: impl FnMut(&mut Scanner<I>) -> ER,
        mut init: EU::Right, mut join: impl FnMut(&mut EU::Right, ET::Right)) -> EU {
        join(&mut init, unwrap!(f(self)));
        self.many(move |scanner| {
            unwrap!(g(scanner));
            f(scanner)
        }, init, join)
    }

    /// Match many of this rule separated by some other rule, ignore the results.
    pub fn sep_by_<ET, EU, ER, E>(
        &mut self,
        f: impl FnMut(&mut Scanner<I>) -> ET,
        g: impl FnMut(&mut Scanner<I>) -> ER) -> EU
        where ET: Either<Left=E>,
              ER: Either<Left=E>,
              EU: Either<Left=E>,
              EU::Right: From<()> {
        let unit: EU::Right = From::from(());
        self.sep_by(f, g, unit, |_, _| ())
    }

    /// Match many of this rule ended by some other rule.
    pub fn end_by<ET: Either<Left=E>, EU: Either<Left=E>, ER: Either<Left=E>, E>(
        &mut self,
        mut f: impl FnMut(&mut Scanner<I>) -> ET,
        mut g: impl FnMut(&mut Scanner<I>) -> ER,
        init: EU::Right, join: impl FnMut(&mut EU::Right, ET::Right)) -> EU {
        self.many(move |scanner| scanner.anchored(|scanner| {
            let res = f(scanner);
            unwrap!(g(scanner));
            res
        }), init, join)
    }
}

impl<I: std::io::Read> Scanner<I> {
    /// Get the next lexeme from the [`Scanner`].
    pub fn next_lexeme(&mut self) -> Result<Lexeme> {
        alt!(self, Self::numeric_literal,
                   Self::id_or_sym,
                   Self::char_or_string,
                   Self::special);
        Self::keep_trying()
    }
}

#[cfg(test)]
fn test_scanner_on<U: Eq + std::fmt::Debug>(
    input: &str,
    f: impl FnOnce(&mut Scanner<&[u8]>) -> U,
    res: U, next: Option<char>) {
    let mut scanner = Scanner::new(input.as_bytes());
    assert_eq!(f(&mut scanner), res);
    assert_eq!(scanner.next(), next);
}