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
use text_scanner::{ext::ScssScannerExt, Scanner};

use crate::{impl_lexer_from_scanner, CssToken, ScanToken, ScannerExt, TokenSpan};

#[derive(PartialEq, Eq, Clone, Copy, Debug)]
pub enum ScssToken {
    Space,
    LineComment,
    BlockComment,
    Ident,
    AtKeyword,
    Hash,
    String,
    Number,
    /// Punctuation e.g. `:`, `,`.
    Punct,
    /// Delimiter e.g. `{`, `}`, `[`, and `]`.
    Delim,
    /// Given valid SCSS, then this variant should never be encountered. If is
    /// is encountered, then check if an issue has already been submitted,
    /// otherwise please [submit an issue].
    ///
    /// [submit an issue]: https://github.com/vallentin/colorblast/issues
    Unknown,
}

impl ScanToken for ScssToken {
    fn scan_token<'text>(scanner: &mut Scanner<'text>) -> Option<(Self, TokenSpan<'text>)> {
        if let Ok((r, _s)) = scanner.scan_scss_line_comment() {
            return Some((Self::LineComment, scanner.span(r)));
        }

        let (tok, span) = CssToken::scan_token(scanner)?;
        match tok {
            CssToken::Space => Some((ScssToken::Space, span)),
            CssToken::BlockComment => Some((ScssToken::BlockComment, span)),
            CssToken::Ident => Some((ScssToken::Ident, span)),
            CssToken::AtKeyword => Some((ScssToken::AtKeyword, span)),
            CssToken::Hash => Some((ScssToken::Hash, span)),
            CssToken::String => Some((ScssToken::String, span)),
            CssToken::Number => Some((ScssToken::Number, span)),
            CssToken::Punct => Some((ScssToken::Punct, span)),
            CssToken::Delim => Some((ScssToken::Delim, span)),
            CssToken::Unknown => Some((ScssToken::Unknown, span)),
        }
    }
}

/// SCSS lexer producing [`ScssToken`]s.
///
/// **Note:** Cloning `ScssLexer` is essentially a copy, as it just contains
/// a `&str` and a `usize` for its `cursor`. However, `Copy` is not
/// implemented, to avoid accidentally copying immutable `ScssLexer`s.
///
/// See also [`CssLexer`].
///
/// [`CssLexer`]: super::CssLexer
#[derive(Clone, Debug)]
pub struct ScssLexer<'text> {
    scanner: Scanner<'text>,
}

impl<'text> ScssLexer<'text> {
    #[inline]
    pub fn new(text: &'text str) -> Self {
        Self {
            scanner: Scanner::new(text),
        }
    }
}

impl_lexer_from_scanner!('text, ScssLexer<'text>, ScssToken, scanner);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_scss_lexer_spans() {
        // This intentionally uses Rust code as input, as it is
        // only testing that ScssLexer returns all characters
        let input = include_str!("../../../text-scanner/src/ext/rust.rs");
        let mut output = String::new();

        let lexer = ScssLexer::new(input);
        for (_tok, span) in lexer {
            output.push_str(span.as_str());
        }

        assert_eq!(input, output);
    }
}