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

62 lines
1.3 KiB
Rust

#[derive(Debug, Copy, Clone)]
pub struct CodeLocation {
line: usize,
char: usize,
}
impl CodeLocation {
pub fn new(line: usize, char: usize) -> Self {
Self { line, char }
}
}
impl PartialEq for CodeLocation {
fn eq(&self, other: &Self) -> bool {
let true_match = self.line == other.line && self.char == other.char;
// For testing purposes
let simulated_match = self.line == usize::MAX
|| self.char == usize::MAX
|| other.line == usize::MAX
|| other.char == usize::MAX;
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<'source> {
text: &'source str,
token_type: TokenType,
loc: CodeLocation,
}
impl<'source> Token<'source> {
pub fn new(text: &'source str, token_type: TokenType, loc: CodeLocation) -> Self {
Self {
text,
token_type,
loc,
}
}
}