2020-11-23 02:00:02 +01:00
|
|
|
use crate::errors::{Error, ErrorKind};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum TokenKind {
|
|
|
|
// Single-character tokens.
|
|
|
|
LeftParen,
|
|
|
|
RightParen,
|
|
|
|
LeftBrace,
|
|
|
|
RightBrace,
|
|
|
|
Comma,
|
|
|
|
Dot,
|
|
|
|
Minus,
|
|
|
|
Plus,
|
|
|
|
Semicolon,
|
|
|
|
Slash,
|
|
|
|
Star,
|
|
|
|
|
|
|
|
// One or two character tokens.
|
|
|
|
Bang,
|
|
|
|
BangEqual,
|
|
|
|
Equal,
|
|
|
|
EqualEqual,
|
|
|
|
Greater,
|
|
|
|
GreaterEqual,
|
|
|
|
Less,
|
|
|
|
LessEqual,
|
|
|
|
|
|
|
|
// Literals.
|
|
|
|
Identifier,
|
|
|
|
String,
|
|
|
|
Number,
|
|
|
|
|
|
|
|
// Keywords.
|
|
|
|
And,
|
|
|
|
Class,
|
|
|
|
Else,
|
|
|
|
False,
|
|
|
|
Fun,
|
|
|
|
For,
|
|
|
|
If,
|
|
|
|
Nil,
|
|
|
|
Or,
|
|
|
|
Print,
|
|
|
|
Return,
|
|
|
|
Super,
|
|
|
|
This,
|
|
|
|
True,
|
|
|
|
Var,
|
|
|
|
While,
|
|
|
|
|
|
|
|
// Special things
|
|
|
|
Eof,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct Token<'a> {
|
|
|
|
kind: TokenKind,
|
2020-11-27 17:55:38 +01:00
|
|
|
lexeme: &'a [char],
|
2020-11-23 02:00:02 +01:00
|
|
|
// literal: Object, // TODO(tazjin): Uhh?
|
|
|
|
line: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Scanner<'a> {
|
2020-11-27 17:55:38 +01:00
|
|
|
source: &'a [char],
|
2020-11-23 02:00:02 +01:00
|
|
|
tokens: Vec<Token<'a>>,
|
|
|
|
errors: Vec<Error>,
|
|
|
|
start: usize, // offset of first character in current lexeme
|
|
|
|
current: usize, // current offset into source
|
|
|
|
line: usize, // current line in source
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Scanner<'a> {
|
|
|
|
fn is_at_end(&self) -> bool {
|
|
|
|
return self.current >= self.source.len();
|
|
|
|
}
|
|
|
|
|
|
|
|
fn advance(&mut self) -> char {
|
|
|
|
self.current += 1;
|
2020-11-27 17:55:38 +01:00
|
|
|
self.source[self.current-1]
|
2020-11-23 02:00:02 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn add_token(&mut self, kind: TokenKind) {
|
|
|
|
let lexeme = &self.source[self.start..self.current];
|
|
|
|
self.tokens.push(Token {
|
|
|
|
kind,
|
|
|
|
lexeme,
|
|
|
|
line: self.line,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn scan_token(&mut self) {
|
|
|
|
match self.advance() {
|
2020-11-27 18:10:32 +01:00
|
|
|
// simple single-character tokens
|
2020-11-23 02:00:02 +01:00
|
|
|
'(' => self.add_token(TokenKind::LeftParen),
|
|
|
|
')' => self.add_token(TokenKind::RightParen),
|
|
|
|
'{' => self.add_token(TokenKind::LeftBrace),
|
|
|
|
'}' => self.add_token(TokenKind::RightBrace),
|
|
|
|
',' => self.add_token(TokenKind::Comma),
|
|
|
|
'.' => self.add_token(TokenKind::Dot),
|
|
|
|
'-' => self.add_token(TokenKind::Minus),
|
|
|
|
'+' => self.add_token(TokenKind::Plus),
|
|
|
|
';' => self.add_token(TokenKind::Semicolon),
|
|
|
|
'*' => self.add_token(TokenKind::Star),
|
|
|
|
|
2020-11-27 18:10:32 +01:00
|
|
|
// possible multi-character tokens
|
|
|
|
'!' => self.add_if_next('=', TokenKind::BangEqual, TokenKind::Bang),
|
|
|
|
'=' => self.add_if_next('=', TokenKind::EqualEqual, TokenKind::Equal),
|
|
|
|
'<' => self.add_if_next('=', TokenKind::LessEqual, TokenKind::Less),
|
|
|
|
'>' => self.add_if_next('=', TokenKind::GreaterEqual, TokenKind::Greater),
|
|
|
|
|
2020-11-28 16:52:13 +01:00
|
|
|
'/' => {
|
|
|
|
// support comments until EOL by discarding characters
|
|
|
|
if self.match_next('/') {
|
|
|
|
while self.peek() != '\n' && !self.is_at_end() {
|
|
|
|
self.advance();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
self.add_token(TokenKind::Slash);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-11-28 16:54:59 +01:00
|
|
|
// ignore whitespace
|
|
|
|
' ' => {},
|
|
|
|
'\r' => {},
|
|
|
|
'\t' => {},
|
|
|
|
'\n' => self.line += 1,
|
|
|
|
|
2020-11-23 02:00:02 +01:00
|
|
|
unexpected => self.errors.push(Error {
|
|
|
|
line: self.line,
|
|
|
|
kind: ErrorKind::UnexpectedChar(unexpected),
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-11-28 16:52:13 +01:00
|
|
|
fn match_next(&mut self, expected: char) -> bool {
|
2020-11-27 18:10:32 +01:00
|
|
|
if self.is_at_end() || self.source[self.current] != expected {
|
2020-11-28 16:52:13 +01:00
|
|
|
false
|
2020-11-27 18:10:32 +01:00
|
|
|
} else {
|
|
|
|
self.current += 1;
|
2020-11-28 16:52:13 +01:00
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn add_if_next(&mut self, expected: char, then: TokenKind, or: TokenKind) {
|
|
|
|
if self.match_next(expected) {
|
2020-11-27 18:10:32 +01:00
|
|
|
self.add_token(then);
|
2020-11-28 16:52:13 +01:00
|
|
|
} else {
|
|
|
|
self.add_token(or);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn peek(&self) -> char {
|
|
|
|
if self.is_at_end() {
|
|
|
|
return '\0';
|
|
|
|
} else {
|
|
|
|
return self.source[self.current];
|
2020-11-27 18:10:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 02:00:02 +01:00
|
|
|
fn scan_tokens(mut self) -> Vec<Token<'a>> {
|
|
|
|
while !self.is_at_end() {
|
|
|
|
self.start = self.current;
|
|
|
|
self.scan_token();
|
|
|
|
}
|
|
|
|
|
|
|
|
return self.tokens;
|
|
|
|
}
|
|
|
|
}
|
2020-11-27 18:16:42 +01:00
|
|
|
|
|
|
|
pub fn scan<'a>(input: &'a [char]) -> Vec<Token<'a>> {
|
|
|
|
let scanner = Scanner {
|
|
|
|
source: &input,
|
|
|
|
tokens: vec![],
|
|
|
|
errors: vec![],
|
|
|
|
start: 0,
|
|
|
|
current: 0,
|
|
|
|
line: 0,
|
|
|
|
};
|
|
|
|
|
|
|
|
return scanner.scan_tokens();
|
|
|
|
}
|