thp/src/syntax/utils.rs

99 lines
3.1 KiB
Rust
Raw Normal View History

2023-09-09 01:17:46 +00:00
use crate::{
lexic::token::{Token, TokenType},
utils::Result3,
};
2023-10-01 23:41:00 +00:00
use super::ParseResult;
pub trait Tokenizer {
fn get_significant<'a>(&'a self, index: usize) -> Option<(&'a Token, usize)>;
}
impl Tokenizer for Vec<Token> {
/// Returns the first non whitespace token at index & the position the found token
fn get_significant<'a>(&'a self, index: usize) -> Option<(&'a Token, usize)> {
let mut current_pos = index;
// Ignore all whitespace and newlines
loop {
match self.get(current_pos) {
Some(token) => {
if token.token_type == TokenType::INDENT
|| token.token_type == TokenType::DEDENT
|| token.token_type == TokenType::NewLine
{
current_pos += 1;
} else {
return Some((token, current_pos));
}
}
None => return None,
}
}
}
}
2024-03-15 21:07:28 +00:00
/// Expects the token at `pos` to be of type `token_type`.
///
/// **Doesn't ignore whitespace or newlines**
pub fn parse_immediate_token_type(
tokens: &Vec<Token>,
pos: usize,
token_type: TokenType,
) -> Result3<&Token> {
2023-09-09 01:17:46 +00:00
match tokens.get(pos) {
Some(t) if t.token_type == token_type => Result3::Ok(t),
Some(t) if t.token_type == TokenType::EOF || t.token_type == TokenType::NewLine => {
Result3::None
}
Some(t) => Result3::Err(t),
None => Result3::None,
}
}
2024-03-15 20:49:02 +00:00
/// Expects the token at `pos` to be an operator of value `operator`. Doesn't ignore whitespace or newlines
2023-10-06 01:26:47 +00:00
pub fn try_operator(tokens: &Vec<Token>, pos: usize, operator: String) -> Result3<&Token> {
match tokens.get(pos) {
Some(t) if t.token_type == TokenType::Operator && t.value == operator => Result3::Ok(t),
Some(t) if t.token_type == TokenType::NewLine || t.token_type == TokenType::EOF => {
Result3::None
}
Some(t) => Result3::Err(t),
None => Result3::None,
}
}
2024-03-15 21:07:28 +00:00
/// Expects the token at `pos` to be of type `token_type`, and returns the token and the next position.
///
2024-03-15 21:44:29 +00:00
/// Ignores all whitespace and newlines.
///
/// Only returns: Ok, Unmatched, Mismatched
2023-09-21 00:53:46 +00:00
pub fn parse_token_type(
tokens: &Vec<Token>,
pos: usize,
token_type: TokenType,
2024-03-15 20:49:02 +00:00
) -> ParseResult<&Token> {
2023-09-21 00:53:46 +00:00
let mut current_pos = pos;
// Ignore all whitespace and newlines
while let Some(t) = tokens.get(current_pos) {
if t.token_type == TokenType::INDENT
|| t.token_type == TokenType::DEDENT
|| t.token_type == TokenType::NewLine
{
current_pos += 1;
} else {
break;
}
}
match tokens.get(current_pos) {
Some(t) if t.token_type == token_type => ParseResult::Ok(t, current_pos + 1),
Some(t) if t.token_type == TokenType::EOF || t.token_type == TokenType::NewLine => {
ParseResult::Unmatched
}
2024-03-15 21:07:28 +00:00
Some(t) => ParseResult::Mismatch(t),
2023-09-21 00:53:46 +00:00
None => ParseResult::Unmatched,
}
}