2015-12-25 02:03:11 -08:00
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
2015-07-22 03:02:55 -07:00
|
|
|
pub enum Token {
|
|
|
|
EOF,
|
2015-12-31 22:20:59 -08:00
|
|
|
Newline,
|
|
|
|
Semicolon,
|
2015-07-22 03:02:55 -07:00
|
|
|
LParen,
|
|
|
|
RParen,
|
|
|
|
Comma,
|
2015-07-26 01:51:15 -07:00
|
|
|
Period,
|
2016-01-05 22:00:29 -08:00
|
|
|
Colon,
|
2015-07-22 03:02:55 -07:00
|
|
|
NumLiteral(f64),
|
|
|
|
StrLiteral(String),
|
2015-07-22 04:01:56 -07:00
|
|
|
Identifier(String),
|
|
|
|
Keyword(Kw)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq)]
|
|
|
|
pub enum Kw {
|
|
|
|
If,
|
|
|
|
Then,
|
|
|
|
Else,
|
|
|
|
While,
|
|
|
|
End,
|
|
|
|
Let,
|
|
|
|
Fn,
|
2015-08-08 00:27:40 -07:00
|
|
|
Null,
|
|
|
|
Assign
|
2015-07-22 03:02:55 -07:00
|
|
|
}
|
2015-07-22 03:12:01 -07:00
|
|
|
|
2016-01-05 22:00:29 -08:00
|
|
|
fn is_digit(c: &char) -> bool {
|
|
|
|
c.is_digit(10)
|
|
|
|
}
|
|
|
|
|
2015-07-22 03:12:01 -07:00
|
|
|
pub fn tokenize(input: &str) -> Vec<Token> {
|
2016-01-05 22:00:29 -08:00
|
|
|
use self::Token::*;
|
2015-07-22 03:12:01 -07:00
|
|
|
let mut tokens = Vec::new();
|
2016-01-05 22:00:29 -08:00
|
|
|
let mut iter = input.chars().peekable();
|
|
|
|
|
|
|
|
while let Some(c) = iter.next() {
|
|
|
|
if char::is_whitespace(c) && c != '\n' {
|
|
|
|
continue;
|
|
|
|
} else if c == '#' {
|
|
|
|
while let Some(c) = iter.next() {
|
|
|
|
if c == '\n' { break; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let cur_tok =
|
|
|
|
if c == '\n' {
|
|
|
|
Newline
|
|
|
|
} else if c == ';' {
|
|
|
|
Semicolon
|
|
|
|
} else if c == '(' {
|
|
|
|
LParen
|
|
|
|
} else if c == ')' {
|
|
|
|
RParen
|
|
|
|
} else if c == ':' {
|
|
|
|
Colon
|
|
|
|
} else if c == '"' {
|
|
|
|
let mut buffer = String::with_capacity(20);
|
|
|
|
loop {
|
|
|
|
//TODO handle string escapes, interpolation
|
|
|
|
match iter.next() {
|
|
|
|
Some(x) if x == '"' => break,
|
|
|
|
Some(x) => buffer.push(x),
|
|
|
|
None => return tokens,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StrLiteral(buffer)
|
|
|
|
} else {
|
|
|
|
StrLiteral("DUMMY".to_string())
|
|
|
|
};
|
|
|
|
|
|
|
|
tokens.push(cur_tok);
|
|
|
|
}
|
|
|
|
|
2015-07-22 03:12:01 -07:00
|
|
|
tokens
|
|
|
|
}
|
2015-07-22 04:01:56 -07:00
|
|
|
|
2015-12-20 17:03:03 -08:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn tokeniziation_tests() {
|
|
|
|
let t1 = "let a = 3\n";
|
|
|
|
assert_eq!(format!("{:?}", tokenize(t1)),
|
2015-12-31 22:20:59 -08:00
|
|
|
"[Keyword(Let), Identifier(\"a\"), Keyword(Assign), NumLiteral(3), Newline, EOF]");
|
2015-12-20 19:29:26 -08:00
|
|
|
|
|
|
|
// this is intentional
|
|
|
|
let t2 = "a + b*c\n";
|
|
|
|
assert_eq!(format!("{:?}", tokenize(t2)),
|
2015-12-31 22:20:59 -08:00
|
|
|
"[Identifier(\"a\"), Identifier(\"+\"), Identifier(\"b*c\"), Newline, EOF]");
|
2015-12-20 19:29:26 -08:00
|
|
|
|
2015-12-20 17:03:03 -08:00
|
|
|
}
|
|
|
|
}
|