thp/src/token.rs

92 lines
1.7 KiB
Rust
Raw Normal View History

2022-12-01 17:17:17 +00:00
#[derive(PartialEq, Debug, Clone)]
2022-11-21 21:58:51 +00:00
pub enum TokenType {
Identifier,
Datatype,
2022-11-21 21:58:51 +00:00
Number,
String,
Operator,
LeftParen,
RightParen,
LeftBracket,
RightBracket,
LeftBrace,
RightBrace,
2023-02-14 20:22:29 +00:00
Semicolon,
2022-11-21 21:58:51 +00:00
VAR,
VAL,
EOF,
}
pub struct Token {
pub token_type: TokenType,
// The token as a raw string
pub value: String,
/// The absolute position of this token, from the
/// start of the file
pub position: usize,
2022-11-21 21:58:51 +00:00
}
pub fn new_eof(position: usize) -> Token {
2022-11-21 21:58:51 +00:00
Token {
token_type: TokenType::EOF,
value: String::from(""),
position,
2022-11-21 21:58:51 +00:00
}
}
pub fn new_number(value: String, position: usize) -> Token {
2022-11-21 21:58:51 +00:00
Token {
token_type: TokenType::Number,
value,
position,
2022-11-21 21:58:51 +00:00
}
}
2022-11-28 23:33:34 +00:00
pub fn new_operator(value: String, position: usize) -> Token {
2022-11-28 23:33:34 +00:00
Token {
token_type: TokenType::Operator,
value,
position,
2022-11-28 23:33:34 +00:00
}
}
2022-11-30 13:38:43 +00:00
pub fn new(value: String, position: usize, token_type: TokenType) -> Token {
Token {
token_type,
value,
position,
}
2022-11-30 13:38:43 +00:00
}
2022-12-01 13:33:48 +00:00
pub fn new_identifier(value: String, position: usize) -> Token {
2022-12-01 13:33:48 +00:00
Token {
token_type: TokenType::Identifier,
value,
position,
2022-12-01 13:33:48 +00:00
}
}
pub fn new_string(value: String, position: usize) -> Token {
Token {
token_type: TokenType::String,
value,
position,
}
}
pub fn new_semicolon(position: usize) -> Token {
Token {
token_type: TokenType::Semicolon,
value: String::from(";"),
position,
}
}
pub fn new_datatype(value: String, position: usize) -> Token {
Token {
token_type: TokenType::Datatype,
value,
position,
}
}