From 8b29b2605011c6d5f6f654a9b2adc4e428dd6752 Mon Sep 17 00:00:00 2001 From: "Guillem L. Jara" <4lon3ly0@tutanota.com> Date: Sun, 24 May 2026 02:46:09 +0200 Subject: [PATCH] chore: add rustfmt.toml Pretty sure nobody was working on anything atm, sorry otherwise... --- interpreter/src/ir.rs | 20 +++++---------- interpreter/src/ir/lower.rs | 29 +++++----------------- lexer/src/lib.rs | 8 ++---- lexer/src/tests.rs | 48 +++++++++--------------------------- parser/src/idempotency.rs | 47 ++++++----------------------------- parser/src/lex.rs | 3 +-- parser/src/lib.rs | 49 +++++++------------------------------ parser/src/pratt.rs | 5 +--- parser/src/sexpr.rs | 47 ++++++----------------------------- rustfmt.toml | 5 ++++ src/main.rs | 12 ++++----- src/utils.rs | 10 +++++--- tests/by-util/test_awk.rs | 6 +++-- 13 files changed, 72 insertions(+), 217 deletions(-) create mode 100644 rustfmt.toml diff --git a/interpreter/src/ir.rs b/interpreter/src/ir.rs index 53abc88..85fa458 100644 --- a/interpreter/src/ir.rs +++ b/interpreter/src/ir.rs @@ -12,10 +12,10 @@ pub mod lower; -pub use lower::test_interpreter; - use std::fmt::{Debug, Display}; +pub use lower::test_interpreter; + #[derive(Clone, Copy, Debug)] #[repr(transparent)] pub struct NonLocal(pub u16); @@ -113,9 +113,7 @@ impl Instruction { debug_assert!(opcode.is_unary()); Self { opcode, - args: Arguments { - unary_local: (dest, src.reg()), - }, + args: Arguments { unary_local: (dest, src.reg()) }, hint: src.hint(), } } @@ -137,9 +135,7 @@ impl Instruction { }; Self { opcode, - args: Arguments { - binary_local: (dest, lhs.reg(), rhs.reg()), - }, + args: Arguments { binary_local: (dest, lhs.reg(), rhs.reg()) }, hint, } } @@ -149,9 +145,7 @@ impl Instruction { debug_assert!(opcode.is_load_store()); Self { opcode, - args: Arguments { - load_store: (dest, src), - }, + args: Arguments { load_store: (dest, src) }, hint: Hint::None, } } @@ -167,9 +161,7 @@ impl Instruction { fn branch(cond: Reg, true_to: Label, false_to: Label) -> Self { Self { opcode: OpCode::Branch, - args: Arguments { - branch: (cond, true_to, false_to), - }, + args: Arguments { branch: (cond, true_to, false_to) }, hint: Hint::None, } } diff --git a/interpreter/src/ir/lower.rs b/interpreter/src/ir/lower.rs index 2718d32..bb02cd4 100644 --- a/interpreter/src/ir/lower.rs +++ b/interpreter/src/ir/lower.rs @@ -3,9 +3,7 @@ // For the full copyright and license information, please view the LICENSE // files that was distributed with this source code. -use std::fmt::Display; -use std::hash::Hash; -use std::mem::forget; +use std::{fmt::Display, hash::Hash, mem::forget}; use bumpalo::{Bump, collections::Vec}; use indexmap::IndexSet; @@ -42,11 +40,7 @@ impl Code<'_> { fn lower_statement(&mut self, stmnt: &Statement) { match stmnt { - Statement::If { - condition, - then_body, - else_body, - } => { + Statement::If { condition, then_body, else_body } => { let mut state = RegsState::new(self); let condition = self.lower_expr(condition); let label_then = self.following_instr(1); @@ -65,10 +59,7 @@ impl Code<'_> { self.bc.nth(end_label).args.jump = self.following_instr(0); } } - Statement::While { - condition, - then_body, - } => { + Statement::While { condition, then_body } => { let cond_label = self.following_instr(0); let condition = self.lower_expr(condition); let while_label = self.bc.emit(Instruction::branch( @@ -81,10 +72,7 @@ impl Code<'_> { self.bc.emit(Instruction::jump(cond_label)); self.bc.nth(while_label).args.branch.2 = self.following_instr(0); } - Statement::DoWhile { - then_body, - condition, - } => { + Statement::DoWhile { then_body, condition } => { let do_label = self.following_instr(0); self.lower_body(then_body); let condition = self.lower_expr(condition); @@ -209,9 +197,7 @@ struct RegsState { impl<'a> Bytecode<'a> { fn new_in(bump: &'a Bump) -> Self { - Self { - code: Vec::with_capacity_in(64, bump), - } + Self { code: Vec::with_capacity_in(64, bump) } } #[inline(always)] @@ -241,10 +227,7 @@ impl RegsState { let old = code.reg_pointer; code.reg_pointer = self.reg_pointer; code.free_regs.truncate(self.n_free_regs); - Self { - reg_pointer: old, - n_free_regs: self.n_free_regs, - } + Self { reg_pointer: old, n_free_regs: self.n_free_regs } } fn scope_hwm(self, code: &mut Code, f: impl FnOnce(&mut Code) -> T) { f(code); diff --git a/lexer/src/lib.rs b/lexer/src/lib.rs index f982b0f..950ff43 100644 --- a/lexer/src/lib.rs +++ b/lexer/src/lib.rs @@ -15,8 +15,7 @@ use std::{ }; use bumpalo::{Bump, collections::Vec}; -use logos::Logos; -use logos::Skip; +use logos::{Logos, Skip}; pub use logos::{Span, SpannedIter}; use memchr::{memchr, memchr3}; use thiserror::Error; @@ -524,10 +523,7 @@ fn parse_indirect_call<'a, const QUALIFIED: bool>(lex: &mut Lexer<'a>) -> Result impl<'a> Identifier<'a> { fn without_namespace(lex: &mut Lexer<'a>) -> Self { - Self { - namespace: None, - literal: parse_ident(lex, SKIP..), - } + Self { namespace: None, literal: parse_ident(lex, SKIP..) } } fn with_namespace(lex: &mut Lexer<'a>) -> Result { diff --git a/lexer/src/tests.rs b/lexer/src/tests.rs index d56e8c1..f62e514 100644 --- a/lexer/src/tests.rs +++ b/lexer/src/tests.rs @@ -5,12 +5,13 @@ use std::io::Write; -use crate::{Identifier, Token}; use bumpalo::{ Bump, collections::{CollectIn, Vec}, }; +use crate::{Identifier, Token}; + fn lex<'a>( src: &'a [u8], arena: &'a Bump, @@ -113,14 +114,8 @@ fn lexer_test_gnu_pattern() { assert_eq!( &lex(b"BEGINFILE ENDFILE", &arena, true, false), &[ - Token::Identifier(Identifier { - namespace: None, - literal: "BEGINFILE" - }), - Token::Identifier(Identifier { - namespace: None, - literal: "ENDFILE" - }) + Token::Identifier(Identifier { namespace: None, literal: "BEGINFILE" }), + Token::Identifier(Identifier { namespace: None, literal: "ENDFILE" }) ] ); } @@ -166,25 +161,13 @@ fn lexer_test_ident_rules_non_posix() { &lex(b"1a::a a::1a _a", &arena, false, false), &[ Token::Number(1.), - Token::Identifier(Identifier { - namespace: Some("a"), - literal: "a" - }), - Token::Identifier(Identifier { - namespace: None, - literal: "a" - }), + Token::Identifier(Identifier { namespace: Some("a"), literal: "a" }), + Token::Identifier(Identifier { namespace: None, literal: "a" }), Token::Colon, Token::Colon, Token::Number(1.), - Token::Identifier(Identifier { - namespace: None, - literal: "a" - }), - Token::Identifier(Identifier { - namespace: None, - literal: "_a" - }) + Token::Identifier(Identifier { namespace: None, literal: "a" }), + Token::Identifier(Identifier { namespace: None, literal: "_a" }) ] ); } @@ -215,10 +198,7 @@ fn lexer_test_general_tokens() { Token::BeginPattern, Token::OpenBrace, Token::Print, - Token::Identifier(Identifier { - namespace: None, - literal: "a" - }), + Token::Identifier(Identifier { namespace: None, literal: "a" }), Token::Plus, Token::Number(1.), Token::ClosedBrace, @@ -231,10 +211,7 @@ fn lexer_test_general_tokens() { Token::Record, Token::Number(1.), Token::EqualTo, - Token::Identifier(Identifier { - namespace: Some("foo"), - literal: "bar" - }), + Token::Identifier(Identifier { namespace: Some("foo"), literal: "bar" }), Token::ClosedBrace, Token::Newline ] @@ -250,10 +227,7 @@ fn lexer_test_regex_ambiguity() { Token::Number(1.), Token::SlashAssign, Token::Number(1.), - Token::Identifier(Identifier { - namespace: None, - literal: "a" - }), + Token::Identifier(Identifier { namespace: None, literal: "a" }), Token::SlashAssign, Token::Number(1.) ] diff --git a/parser/src/idempotency.rs b/parser/src/idempotency.rs index 87cbf1c..160165f 100644 --- a/parser/src/idempotency.rs +++ b/parser/src/idempotency.rs @@ -84,11 +84,7 @@ impl Display for Statement<'_> { match self { Self::Simple(simple) => <_ as Display>::fmt(simple, f), - Self::If { - condition, - then_body, - else_body, - } => { + Self::If { condition, then_body, else_body } => { write!(f, "if ({condition:ew$}) ")?; write_body(f, then_body, indent)?; if let Some(else_body) = else_body { @@ -97,27 +93,16 @@ impl Display for Statement<'_> { } Ok(()) } - Self::While { - condition, - then_body, - } => { + Self::While { condition, then_body } => { write!(f, "while ({condition:ew$}) ")?; write_body(f, then_body, indent) } - Self::DoWhile { - then_body, - condition, - } => { + Self::DoWhile { then_body, condition } => { write!(f, "do ")?; write_body(f, then_body, indent)?; write!(f, " while ({condition:ew$})") } - Self::For { - init, - condition, - update, - body, - } => { + Self::For { init, condition, update, body } => { write!(f, "for (")?; if let Some(e) = init { write!(f, "{e:ew$}")?; @@ -133,19 +118,11 @@ impl Display for Statement<'_> { write!(f, ") ")?; write_body(f, body, indent) } - Self::ForEach { - variable, - array, - body, - } => { + Self::ForEach { variable, array, body } => { write!(f, "for ({variable} in {array}) ")?; write_body(f, body, indent) } - Self::Switch { - scrutinee, - branches, - default, - } => { + Self::Switch { scrutinee, branches, default } => { writeln!(f, "switch ({scrutinee:ew$}) {{")?; let default_pos = default.as_ref().map_or(branches.len(), |x| x.1); for i in 0..default_pos { @@ -187,21 +164,13 @@ impl Display for SimpleStatement<'_> { match self { SimpleStatement::Expression(expr) => write!(f, "{expr:ew$}"), - SimpleStatement::Command { - name, - args, - redirection: Some((rx, expr)), - } => { + SimpleStatement::Command { name, args, redirection: Some((rx, expr)) } => { write!(f, "{name}(")?; write_args(f, args, indent)?; write!(f, "){rx}{expr}")?; Ok(()) } - SimpleStatement::Command { - name, - args, - redirection: None, - } => { + SimpleStatement::Command { name, args, redirection: None } => { write!(f, "{name} ")?; write_args(f, args, indent) } diff --git a/parser/src/lex.rs b/parser/src/lex.rs index 920b5cb..f2ab28b 100644 --- a/parser/src/lex.rs +++ b/parser/src/lex.rs @@ -8,13 +8,12 @@ use std::{fmt::Debug, iter::Peekable}; use bumpalo::Bump; use lexer::{Identifier, LexingError, Slice, Span, SpannedIter, Token}; +use super::Result; use crate::{ ParsingError, ast::{Command, SpecialPattern}, }; -use super::Result; - pub struct Lexer<'a> { inner: Peekable>>, span: Span, diff --git a/parser/src/lib.rs b/parser/src/lib.rs index be6f613..52aa338 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -21,8 +21,7 @@ use either::Either::{Left, Right}; use hashbrown::HashMap; use lexer::{LexingError, Span, Token}; -pub use crate::ast::*; -pub use crate::lex::Lexer; +pub use crate::{ast::*, lex::Lexer}; use crate::{ diagnostics::{ParsingError, report_error}, lex::TokenExt, @@ -108,10 +107,7 @@ impl<'a> Parser<'a> { } } else if lex.peek_is(&Token::OpenBrace) { let actions = Some(self.parse_body(lex)?); - self.add_rule(Rule { - pattern: None, - actions, - }); + self.add_rule(Rule { pattern: None, actions }); } else { match lex.expect_next()? { Token::LoadDirective => { @@ -260,11 +256,7 @@ impl<'a> Parser<'a> { .consume(&Token::Else) .then(|| self.parse_statement_body(lex)) .transpose()?; - Statement::If { - condition, - then_body, - else_body, - } + Statement::If { condition, then_body, else_body } } Token::For => { lex.expect(&Token::OpenParent, ParsingError::ExpectedOpeningParenthesis)?; @@ -337,28 +329,18 @@ impl<'a> Parser<'a> { _ => {} } - Statement::Switch { - scrutinee, - branches, - default, - } + Statement::Switch { scrutinee, branches, default } } Token::While => { let condition = self.parse_parenthesized_expr(lex)?; let then_body = self.parse_statement_body(lex)?; - Statement::While { - condition, - then_body, - } + Statement::While { condition, then_body } } Token::Do => { let then_body = self.parse_body(lex)?; lex.expect(&Token::While, ParsingError::MissingWhileAfterDo)?; let condition = self.parse_parenthesized_expr(lex)?; - Statement::DoWhile { - then_body, - condition, - } + Statement::DoWhile { then_body, condition } } Token::Break => Statement::Break, Token::Continue => Statement::Continue, @@ -426,12 +408,7 @@ impl<'a> Parser<'a> { lex.expect(&Token::ClosedParent, ParsingError::InvalidForLoop)?; let body = self.parse_statement_body(lex)?; - Ok(Statement::For { - init, - condition, - update, - body, - }) + Ok(Statement::For { init, condition, update, body }) } #[tracing::instrument] @@ -462,11 +439,7 @@ impl<'a> Parser<'a> { )?; let body = self.parse_statement_body(lex)?; - Ok(Statement::ForEach { - variable, - array, - body, - }) + Ok(Statement::ForEach { variable, array, body }) } #[tracing::instrument] @@ -504,11 +477,7 @@ impl<'a> Parser<'a> { self.parse_command_args(lex)? }; let redirection = self.parse_command_redirection(lex)?; - Ok(SimpleStatement::Command { - name, - args, - redirection, - }) + Ok(SimpleStatement::Command { name, args, redirection }) } /// Parses arguments to command or function calls; consumes to the end of diff --git a/parser/src/pratt.rs b/parser/src/pratt.rs index a619165..d235cbd 100644 --- a/parser/src/pratt.rs +++ b/parser/src/pratt.rs @@ -24,10 +24,7 @@ pub struct Pratt<'a, 'b> { impl<'a, 'b> Pratt<'a, 'b> { pub fn new(parser: &'b mut Parser<'a>, typed_regex: bool) -> Self { - Self { - parser, - typed_regex, - } + Self { parser, typed_regex } } pub fn parse(&mut self, lex: &mut Lexer<'a>) -> Result> { diff --git a/parser/src/sexpr.rs b/parser/src/sexpr.rs index 680bb08..837ed51 100644 --- a/parser/src/sexpr.rs +++ b/parser/src/sexpr.rs @@ -23,11 +23,7 @@ impl Debug for Statement<'_> { match self { Statement::Simple(simple) => <_ as Debug>::fmt(simple, f), - Self::If { - condition, - then_body, - else_body, - } => { + Self::If { condition, then_body, else_body } => { if alt { write!(f, "(if {condition:?}\n{pad}")?; write!(f, "{then_body:#ni$?}")?; @@ -46,32 +42,21 @@ impl Debug for Statement<'_> { write!(f, "(if {condition:?} {then_body:?})") } } - Self::While { - condition, - then_body, - } => { + Self::While { condition, then_body } => { if alt { write!(f, "(while {condition:?}\n{pad}{then_body:#ni$?})") } else { write!(f, "(while {condition:?} {then_body:?})") } } - Self::DoWhile { - then_body, - condition, - } => { + Self::DoWhile { then_body, condition } => { if alt { write!(f, "(do-while\n{pad}{then_body:#ni$?}\n{pad}{condition:?})") } else { write!(f, "(do-while {then_body:?} {condition:?})") } } - Self::For { - init, - condition, - update, - body, - } => { + Self::For { init, condition, update, body } => { write!(f, "(for")?; if alt { let write_fragment = |f: &mut Formatter, x: Option<&dyn Debug>| { @@ -99,22 +84,14 @@ impl Debug for Statement<'_> { write!(f, " {body:?})") } } - Self::ForEach { - variable, - array, - body, - } => { + Self::ForEach { variable, array, body } => { if alt { write!(f, "(for-each {variable:?} {array:?}\n{pad}{body:#ni$?})") } else { write!(f, "(for-each {variable:?} {array:?} {body:?})") } } - Self::Switch { - scrutinee, - branches, - default, - } => { + Self::Switch { scrutinee, branches, default } => { if alt { if let Some((dx, i)) = default { write!( @@ -166,11 +143,7 @@ impl Debug for SimpleStatement<'_> { write!(f, "{expr:?}") } } - Self::Command { - name, - args, - redirection: Some((rx, expr)), - } => { + Self::Command { name, args, redirection: Some((rx, expr)) } => { if alt { write!( f, @@ -182,11 +155,7 @@ impl Debug for SimpleStatement<'_> { write!(f, "({name:?}{:?} ({rx:?} {expr:?}))", ListLispFmt(args)) } } - Self::Command { - name, - args, - redirection: None, - } => { + Self::Command { name, args, redirection: None } => { write!(f, "({name:?}{:?})", ListLispFmt(args)) } diff --git a/rustfmt.toml b/rustfmt.toml new file mode 100644 index 0000000..dab503d --- /dev/null +++ b/rustfmt.toml @@ -0,0 +1,5 @@ +imports_granularity = "Crate" +group_imports = "StdExternalCrate" +max_width = 100 +struct_lit_width = 50 +newline_style = "Unix" diff --git a/src/main.rs b/src/main.rs index 0fadf68..1f553ef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,8 +8,10 @@ mod cli; mod utils; -use std::env::args_os; -use std::io::{self, Write}; +use std::{ + env::args_os, + io::{self, Write}, +}; use bumpalo::Bump; use clap::Parser as _; @@ -54,11 +56,7 @@ fn uu_main() -> Result<()> { } dbg!(arena.chunk_capacity()); - if let Some(Rule { - actions: Some(body), - pattern: _, - }) = ast.rules.first() - { + if let Some(Rule { actions: Some(body), pattern: _ }) = ast.rules.first() { let x = test_interpreter(body); if let Err(e) = writeln!(io::stdout(), "---\n{x}") && e.kind() != io::ErrorKind::BrokenPipe diff --git a/src/utils.rs b/src/utils.rs index 092f064..0147350 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -3,10 +3,12 @@ // For the full copyright and license information, please view the LICENSE // files that was distributed with this source code. -use std::fmt::{Debug, Display}; -use std::io::{self, Write}; -use std::panic::{UnwindSafe, catch_unwind, set_hook, take_hook}; -use std::process::exit; +use std::{ + fmt::{Debug, Display}, + io::{self, Write}, + panic::{UnwindSafe, catch_unwind, set_hook, take_hook}, + process::exit, +}; use color_eyre::config::HookBuilder; use tracing_error::ErrorLayer; diff --git a/tests/by-util/test_awk.rs b/tests/by-util/test_awk.rs index 02bc986..142deae 100644 --- a/tests/by-util/test_awk.rs +++ b/tests/by-util/test_awk.rs @@ -24,8 +24,10 @@ fn no_args_fails_code_one() { #[cfg(target_os = "linux")] #[test] fn write_to_dev_full_does_not_panic() { - use std::fs::OpenOptions; - use std::process::{Command, Stdio}; + use std::{ + fs::OpenOptions, + process::{Command, Stdio}, + }; let Ok(dev_full) = OpenOptions::new().write(true).open("/dev/full") else { return; // /dev/full not available; skip.