rustfmt with nightly options

also some other minor formatting changes
This commit is contained in:
Moritz Gmeiner 2024-09-03 16:48:00 +02:00
commit f8b59e6034
17 changed files with 91 additions and 291 deletions

View file

@ -3,7 +3,7 @@ use smol_str::SmolStr;
use super::{CodePos, LexerError, Token, TokenType};
/*====================================================================================================================*/
// =================================================================================================
static KEYWORDS: phf::Map<&'static str, TokenType> = phf_map! {
"and" => TokenType::And,
@ -25,7 +25,7 @@ static KEYWORDS: phf::Map<&'static str, TokenType> = phf_map! {
"while" => TokenType::While
};
/*====================================================================================================================*/
// =================================================================================================
pub fn scan_tokens(source_code: &str) -> Result<Vec<Token>, Vec<LexerError>> {
let lexer = Lexer::new(source_code);
@ -33,7 +33,7 @@ pub fn scan_tokens(source_code: &str) -> Result<Vec<Token>, Vec<LexerError>> {
lexer.scan_tokens()
}
/*====================================================================================================================*/
// =================================================================================================
#[derive(Debug)]
struct Lexer {
@ -242,18 +242,6 @@ impl Lexer {
}
fn try_parse_string(&mut self) {
// first '"' already consumed
// advance until second "
/* while self.advance() != '"' {
if self.source_is_empty() {
self.errors.push(LexerError::UnterminatedStringLiteral {
code_pos: self.code_pos,
});
return;
}
} */
let mut s = String::new();
let starting_pos = self.code_pos;
@ -356,11 +344,6 @@ impl Lexer {
let lexeme: String = self.source[self.start..self.current].iter().collect();
/* let token_type = KEYWORDS
.get(&lexeme)
.cloned()
.unwrap_or(TokenType::Identifier(Box::new(lexeme))); */
if let Some(token_type) = KEYWORDS.get(&lexeme) {
// Token::new(token_type, self.code_pos)
self.push_token(token_type.clone());

View file

@ -1,12 +1,11 @@
use smol_str::SmolStr;
use crate::lexer::{Token, TokenType};
use crate::parser::expr::BinaryOp;
use super::expr::{Expr, UnaryOp};
use super::{LogicalOp, ParserError, Stmt};
use crate::lexer::{Token, TokenType};
use crate::parser::expr::BinaryOp;
/*====================================================================================================================*/
// =================================================================================================
type ParserResult<T> = Result<T, ParserError>;
@ -14,7 +13,7 @@ pub fn parse_tokens(tokens: Vec<Token>) -> Result<Vec<Stmt>, Vec<ParserError>> {
Parser::new(tokens).parse()
}
/*====================================================================================================================*/
// =================================================================================================
// takes care of token iteration
struct TokenIter {
@ -71,7 +70,7 @@ impl Iterator for TokenIter {
}
}
/*====================================================================================================================*/
// =================================================================================================
struct Parser {
token_iter: TokenIter,
@ -413,6 +412,7 @@ impl Parser {
})?;
let is_in_init = self.is_in_init;
if &*method_name == "init" {
self.is_in_init = true;
}
@ -841,21 +841,7 @@ impl Parser {
}
fn next_token(&mut self) -> Token {
/* let token = self.token_iter.next().unwrap();
// println!("Next token: {next:?}");
if token.token_type == TokenType::EOF {
panic!("Someone ate a EOF token");
}
// if token.token_type == TokenType::Print {
// panic!("Found the print");
// }
token */
self.token_iter.next().unwrap() // .ok_or(ParserError::TokenStreamEnded)
self.token_iter.next().unwrap()
}
fn peek_token(&mut self) -> &Token {
@ -866,14 +852,6 @@ impl Parser {
where
F: Fn(&Token) -> ParserError,
{
/* let token = self.next_token();
if token.token_type == token_type {
Ok(())
} else {
Err(err_fn(token))
} */
match &self.peek_token().token_type {
tt if tt == &token_type => {
let _ = self.next_token();

View file

@ -1,8 +1,7 @@
use thiserror::Error;
use crate::lexer::{CodePos, Token};
use super::Expr;
use crate::lexer::{CodePos, Token};
#[derive(Error, Debug)]
pub enum ParserError {

View file

@ -155,10 +155,9 @@ impl Expr {
pub fn function(name: impl Into<SmolStr>, param_names: Vec<SmolStr>, body: Stmt) -> Self {
let name = name.into();
#[allow(clippy::box_default)]
// let closure_vars = Box::new(Vec::new());
let closure_vars = Vec::new();
let body = Box::new(body);
Self::Function {
name,
param_names,
@ -264,7 +263,7 @@ impl Display for Expr {
}
}
/*====================================================================================================================*/
// =================================================================================================
#[derive(Debug, Clone)]
pub enum Literal {
@ -285,7 +284,7 @@ impl Display for Literal {
}
}
/*====================================================================================================================*/
// =================================================================================================
#[derive(Debug, Clone, Copy)]
pub enum UnaryOp {
@ -302,7 +301,7 @@ impl Display for UnaryOp {
}
}
/*====================================================================================================================*/
// =================================================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[rustfmt::skip]