lexer: punctuation and math ops

This commit is contained in:
Ryan Chandler 2022-07-16 14:29:21 +01:00
parent 3178a53680
commit cdfff5210b
No known key found for this signature in database
GPG Key ID: F113BCADDB3B0CCA

View File

@ -166,6 +166,14 @@ impl Lexer {
identifier_to_keyword(&buffer).unwrap_or(TokenKind::Identifier(buffer))
},
'{' => TokenKind::LeftBrace,
'}' => TokenKind::RightBrace,
'(' => TokenKind::LeftParen,
')' => TokenKind::RightParen,
';' => TokenKind::SemiColon,
'+' => TokenKind::Plus,
'-' => TokenKind::Minus,
'<' => TokenKind::LessThan,
_ => unimplemented!("<scripting> char: {}", char),
};
@ -180,6 +188,7 @@ impl Lexer {
}
}
#[allow(dead_code)]
fn identifier_to_keyword(ident: &str) -> Option<TokenKind> {
Some(match ident {
"function" => TokenKind::Function,
@ -234,6 +243,28 @@ mod tests {
]);
}
#[test]
fn punct() {
assert_tokens("<?php {}();", &[
open!(),
TokenKind::LeftBrace,
TokenKind::RightBrace,
TokenKind::LeftParen,
TokenKind::RightParen,
TokenKind::SemiColon,
]);
}
#[test]
fn math() {
assert_tokens("<?php + - <", &[
open!(),
TokenKind::Plus,
TokenKind::Minus,
TokenKind::LessThan,
]);
}
fn assert_tokens(source: &str, expected: &[TokenKind]) {
let mut lexer = Lexer::new(None);
let mut kinds = vec!();