mirror of
https://github.com/danog/parser.git
synced 2025-01-22 13:01:32 +01:00
lexer: punctuation and math ops
This commit is contained in:
parent
3178a53680
commit
cdfff5210b
@ -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!();
|
||||
|
Loading…
x
Reference in New Issue
Block a user