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
//! Definitions of types used in the libslide scanner.

use crate::common::Span;
use core::fmt;

/// The type of a [Token](self::Token).
#[derive(PartialEq, Clone, Debug)]
pub enum TokenType {
    /// Stores a scanned number in double precision.
    Float(f64),

    /// + symbol
    Plus,

    /// - symbol
    Minus,

    /// * symbol
    Mult,

    /// / symbol
    Div,

    /// % symbol
    Mod,

    /// ^ symbol
    Exp,

    /// = symbol
    Equal,

    /// := symbol
    AssignDefine,

    /// ( symbol
    OpenParen,

    /// ) symbol
    CloseParen,

    /// [ symbol
    OpenBracket,

    /// ] symbol
    CloseBracket,

    /// A variable name.
    Variable(String),

    /// A variable pattern, of form $name.
    VariablePattern(String),

    /// A constant pattern, of form #name.
    ConstPattern(String),

    /// An any pattern, of form _name.
    AnyPattern(String),

    /// An invalid token.
    Invalid(String),

    /// End of file.
    EOF,
}

impl TokenType {
    pub(crate) fn matcher(&self) -> Self {
        match self {
            TokenType::OpenParen => TokenType::CloseParen,
            TokenType::CloseParen => TokenType::OpenParen,
            TokenType::OpenBracket => TokenType::CloseBracket,
            TokenType::CloseBracket => TokenType::OpenBracket,
            els => unreachable!("{} has no matcher", els),
        }
    }
}

impl fmt::Display for TokenType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        use TokenType::*;
        write!(
            f,
            "{}",
            match self {
                Float(num) => num.to_string(),
                Plus => "+".into(),
                Minus => "-".into(),
                Mult => "*".into(),
                Div => "/".into(),
                Mod => "%".into(),
                Exp => "^".into(),
                Equal => "=".into(),
                AssignDefine => ":=".into(),
                OpenParen => "(".into(),
                CloseParen => ")".into(),
                OpenBracket => "[".into(),
                CloseBracket => "]".into(),
                Variable(s) => s.to_string(),
                VariablePattern(s) => s.to_string(),
                ConstPattern(s) => s.to_string(),
                AnyPattern(s) => s.to_string(),
                Invalid(s) => s.to_string(),
                EOF => "end of file".into(),
            }
        )
    }
}

/// Describes a token in a slide program.
#[derive(PartialEq, Clone, Debug)]
pub struct Token {
    /// The type of the token.
    pub ty: TokenType,
    /// The source span of the token.
    pub span: Span,
    /// The full span of the token including its leading trivia.
    pub full_span: Span,
}

impl Token {
    /// Creates a new token.
    pub fn new<Sp1, Sp2>(ty: TokenType, span: Sp1, full_span: Sp2) -> Self
    where
        Sp1: Into<Span>,
        Sp2: Into<Span>,
    {
        Self {
            ty,
            span: span.into(),
            full_span: full_span.into(),
        }
    }
}

impl fmt::Display for Token {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.ty.to_string())
    }
}

#[cfg(test)]
mod tests {
    mod format {
        use crate::scanner::types::*;

        macro_rules! format_tests {
            ($($name:ident: $ty:expr, $format_str:expr)*) => {
            $(
                #[test]
                fn $name() {
                    use TokenType::*;
                    let tok = Token::new($ty, (0..0), (0..0));
                    assert_eq!(tok.to_string(), $format_str);
                }
            )*
            }
        }

        format_tests! {
            float: Float(1.3), "1.3"
            plus: Plus, "+"
            minus: Minus, "-"
            mult: Mult, "*"
            div: Div, "/"
            modulo: Mod, "%"
            exp: Exp, "^"
            equal: Equal, "="
            open_paren: OpenParen, "("
            close_paren: CloseParen, ")"
            open_bracket: OpenBracket, "["
            close_bracket: CloseBracket, "]"
            variable: Variable("ab".into()), "ab"
            invalid: Invalid("@&@".into()), "@&@"
        }
    }
}