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
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Keyword {
    /// `let`
    Let,
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub enum Punct {
    /// `+`
    Add,

    /// `-`
    Sub,

    /// `*`
    Mul,

    /// `/`
    Div,

    /// `^`
    Pow,

    /// `:`
    Colon,

    /// `;`
    SemiColon,

    /// `=`
    Assign,

    /// `,`
    Comma,

    /// `(`
    OpenParen,

    /// `)`
    CloseParen,

    /// `[`
    OpenBracket,

    /// `]`
    CloseBracket,

    /// `{`
    OpenCurly,

    /// `}`
    CloseCurly,

    /// `...`
    Spread,

    /// `..`
    Range,

    /// `.`
    Dot,

    /// `|`
    Pipe,

    /// `->`
    Arrow,

    /// `\`
    Lambda,
}

impl Punct {
    // Lower is better
    pub fn precedence(self) -> u8 {
        use self::Punct::*;
        match self {
            Pow => 1,
            Mul | Div => 2,
            Add | Sub => 3,
            _ => 9,
        }
    }

    pub fn left_associative(self) -> bool {
        use self::Punct::*;
        match self {
            Add | Sub | Mul | Div => true,
            _ => false,
        }
    }

    pub fn is_arithmetic(self) -> bool {
        use self::Punct::*;
        match self {
            Add | Sub | Mul | Div | Pow => true,
            _ => false,
        }
    }
}

/// The various types of tokens.
///
/// Comments, strings and identifiers keep the range of their values within the source code.
/// The start of that range is the index of the first character of the token.
/// The end is the index after the last character of the token.
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum TokenType {
    Keyword(Keyword),
    Ident { start: usize, end: usize },
    Num(i32),
    Str { start: usize, end: usize },
    Bool(bool),
    Comment { start: usize, end: usize },
    Discard,
    Punct(Punct),
    Term,
}

impl TokenType {
    // ew
    pub fn type_eq(self, other: TokenType) -> bool {
        use self::TokenType::*;
        match (self, other) {
            (Keyword(_), Keyword(_))
            | (Ident { .. }, Ident { .. })
            | (Num(_), Num(_))
            | (Str { .. }, Str { .. })
            | (Bool(_), Bool(_))
            | (Comment { .. }, Comment { .. })
            | (Punct(_), Punct(_))
            | (Term, Term) => true,
            _ => false,
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq)]
pub struct Token {
    pub col: usize,
    pub ln: usize,
    pub r#type: TokenType,
}

impl Token {
    pub fn new(ln: usize, col: usize, t: TokenType) -> Self {
        Token { col, ln, r#type: t }
    }
}