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
// the 'rust' tokenizer

pub struct TokenizerState<'a> {
    pub prev: char,
    pub cur: char,
    pub next: char,
    pub lines: &'a [Vec<char>],
    pub line_start: usize,
    pub line_counter: usize,
    pub eof: bool,
    pub offset: usize,
    iter: std::slice::Iter<'a, char>,
}

impl<'a> TokenizerState<'a> {
    pub fn new(lines: &'a [Vec<char>]) -> Self {
        let mut ret = Self {
            lines,
            line_start: 0,
            line_counter: 0,
            offset: 0,
            eof: false,
            prev: '\0',
            cur: '\0',
            next: '\0',
            iter: lines[0].iter(),
        };
        ret.advance_with_cur();
        ret
    }

    pub fn advance(&mut self) {
        if let Some(next) = self.iter.next() {
            self.next = *next;
            self.offset += 1;
        } else {
            self.next_line();
        }
    }

    pub fn next_line(&mut self) {
        if self.line_counter < self.lines.len() - 1 {
            self.line_counter += 1;
            self.line_start = self.offset;
            self.offset += 1;
            self.iter = self.lines[self.line_counter].iter();
            self.next = '\n'
        } else {
            self.offset += 1;
            self.eof = true;
            self.next = '\0'
        }
    }

    pub fn next_is_digit(&self) -> bool {
        self.next >= '0' && self.next <= '9'
    }

    pub fn next_is_letter(&self) -> bool {
        self.next >= 'a' && self.next <= 'z' || self.next >= 'A' && self.next <= 'Z'
    }

    pub fn next_is_lowercase_letter(&self) -> bool {
        self.next >= 'a' && self.next <= 'z'
    }

    pub fn next_is_uppercase_letter(&self) -> bool {
        self.next >= 'A' && self.next <= 'Z'
    }

    pub fn next_is_hex(&self) -> bool {
        self.next >= '0' && self.next <= '9' || self.next >= 'a' && self.next <= 'f' || self.next >= 'A' && self.next <= 'F'
    }

    pub fn advance_with_cur(&mut self) {
        self.cur = self.next;
        self.advance();
    }

    pub fn advance_with_prev(&mut self) {
        self.prev = self.cur;
        self.cur = self.next;
        self.advance();
    }

    pub fn keyword(&mut self, chunk: &mut Vec<char>, word: &str) -> bool {
        for m in word.chars() {
            if m == self.next {
                chunk.push(m);
                self.advance();
            } else {
                return false;
            }
        }
        true
    }
}

#[derive(Clone, Debug)]
pub struct TokenChunk {
    pub token_type: TokenType,
    pub offset: usize,
    pub pair_token: usize,
    pub len: usize,
    pub next: char,
    //    pub chunk: Vec<char>
}

impl TokenChunk {
    pub fn scan_last_token(token_chunks: &[TokenChunk]) -> TokenType {
        let mut prev_tok_index = token_chunks.len();
        while prev_tok_index > 0 {
            let tt = &token_chunks[prev_tok_index - 1].token_type;
            if !tt.should_ignore() {
                return *tt;
            }
            prev_tok_index -= 1;
        }
        TokenType::Unexpected
    }

    pub fn push_with_pairing(
        token_chunks: &mut Vec<TokenChunk>,
        pair_stack: &mut Vec<usize>,
        next: char,
        offset: usize,
        offset2: usize,
        token_type: TokenType,
    ) -> bool {
        let mut invalid_pair = false;
        let pair_token = if token_type == TokenType::ParenOpen {
            pair_stack.push(token_chunks.len());
            token_chunks.len()
        } else if token_type == TokenType::ParenClose {
            if !pair_stack.is_empty() {
                let other = pair_stack.pop().unwrap();
                token_chunks[other].pair_token = token_chunks.len();
                other
            } else {
                invalid_pair = true;
                token_chunks.len()
            }
        } else {
            token_chunks.len()
        };
        token_chunks.push(TokenChunk { offset, pair_token, len: offset2 - offset, next, token_type });
        invalid_pair
    }
}

#[derive(Clone, PartialEq, Copy, Debug)]
pub enum TokenType {
    Whitespace,
    Newline,
    Keyword,
    Flow,
    Fn,
    TypeDef,
    Impl,
    Looping,
    Identifier,
    Call,
    Macro,
    TypeName,
    ThemeName,
    BuiltinType,
    Hash,
    Color,
    Regex,
    String,
    Number,
    Bool,

    StringMultiBegin,
    StringChunk,
    StringMultiEnd,

    CommentLine,
    CommentMultiBegin,
    CommentChunk,
    CommentMultiEnd,

    ParenOpen,
    ParenClose,
    Operator,
    Namespace,
    Splat,
    Delimiter,
    Colon,

    Warning,
    Error,
    Defocus,

    Unexpected,
    Eof,
}

impl TokenType {
    pub fn should_ignore(&self) -> bool {
        matches!(
            self,
            TokenType::Whitespace
                | TokenType::Newline
                | TokenType::CommentLine
                | TokenType::CommentMultiBegin
                | TokenType::CommentChunk
                | TokenType::CommentMultiEnd
        )
    }
}