1
0
Fork 0
This repository has been archived on 2025-03-30. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
compiler-course/src/compiler/token.rs

63 lines
1.2 KiB
Rust
Raw Normal View History

2025-01-18 18:58:14 +02:00
#[derive(Debug, Copy, Clone)]
pub struct CodeLocation {
2025-01-18 19:13:55 +02:00
start: usize,
end: usize,
2025-01-18 18:58:14 +02:00
}
impl CodeLocation {
2025-01-18 19:13:55 +02:00
pub fn new(start: usize, end: usize) -> Self {
Self { start, end }
2025-01-18 18:58:14 +02:00
}
}
impl PartialEq for CodeLocation {
fn eq(&self, other: &Self) -> bool {
2025-01-18 19:13:55 +02:00
let true_match = self.start == other.start && self.end == other.end;
2025-01-18 18:58:14 +02:00
// For testing purposes
2025-01-18 19:13:55 +02:00
let simulated_match = self.start == usize::MAX
|| self.end == usize::MAX
|| other.start == usize::MAX
|| other.end == usize::MAX;
2025-01-18 18:58:14 +02:00
true_match || simulated_match
}
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum TokenType {
Comment,
Integer,
Identifier,
Operator,
Punctuation,
Whitespace,
}
impl TokenType {
pub fn ignore(&self) -> bool {
use TokenType::*;
match self {
Whitespace | Comment => true,
_ => false,
}
}
}
#[derive(Debug, PartialEq)]
pub struct Token {
text: String,
token_type: TokenType,
loc: CodeLocation,
}
impl Token {
pub fn new(text: &str, token_type: TokenType, loc: CodeLocation) -> Self {
Self {
text: text.to_string(),
token_type,
loc,
}
}
}