Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

awk: #[cfg_attr(test, derive(Debug))] and some clippy #324

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions awk/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ fn normalize_builtin_function_arguments(
}
}

#[derive(Debug, PartialEq, Eq)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq, Eq)]
enum ExprKind {
LValue,
Number,
Expand Down Expand Up @@ -373,7 +374,8 @@ pub enum GlobalName {
type NameMap = HashMap<String, GlobalName>;
type LocalMap = HashMap<String, VarId>;

#[derive(Debug, Default)]
#[cfg_attr(test, derive(Debug))]
#[derive(Default)]
struct Instructions {
opcodes: Vec<OpCode>,
source_locations: Vec<SourceLocation>,
Expand Down Expand Up @@ -1702,7 +1704,8 @@ fn location_end(loc: &InputLocation) -> usize {
}
}

#[derive(Debug, Clone)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone)]
pub struct CompilerErrors {
errors: Vec<PestError>,
}
Expand Down
9 changes: 6 additions & 3 deletions awk/src/interpreter/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,24 @@ use std::{

use super::AwkValue;

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, PartialEq)]
pub struct KeyIterator {
index: usize,
}

pub type Key = Rc<str>;

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, Copy, PartialEq)]
pub struct ValueIndex {
index: usize,
}

pub type KeyValuePair = (Key, AwkValue);

#[derive(Debug, Clone, PartialEq, Default)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, PartialEq, Default)]
pub struct Array {
key_map: HashMap<Key, usize>,
pairs: Vec<Option<KeyValuePair>>,
Expand Down
3 changes: 2 additions & 1 deletion awk/src/interpreter/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ pub fn parse_conversion_specifier_args(iter: &mut Chars) -> Result<(char, Format
Ok((next, result))
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum IntegerFormat {
Decimal,
Octal,
Expand Down
15 changes: 10 additions & 5 deletions awk/src/interpreter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,8 @@ fn is_valid_record_index(index: usize) -> Result<(), String> {
}
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, PartialEq)]
enum AwkValueVariant {
Number(f64),
String(AwkString),
Expand All @@ -688,14 +689,16 @@ enum AwkValueVariant {
UninitializedScalar,
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, Copy, PartialEq)]
enum AwkRefType {
None,
Field(u16),
SpecialGlobalVar(SpecialVar),
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, PartialEq)]
struct AwkValue {
value: AwkValueVariant,
ref_type: AwkRefType,
Expand Down Expand Up @@ -889,14 +892,16 @@ impl From<Array> for AwkValue {
}
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, PartialEq)]
struct ArrayIterator {
array: *mut AwkValue,
iter_var: *mut AwkValue,
key_iter: KeyIterator,
}

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, PartialEq)]
struct ArrayElementRef {
array: *mut AwkValue,
value_index: ValueIndex,
Expand Down
6 changes: 4 additions & 2 deletions awk/src/interpreter/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use core::fmt;
use std::{ffi::CString, ops::Deref, rc::Rc};

#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, PartialEq)]
enum AwkStringVariant {
Owned(String),
Shared(Rc<str>),
}

#[derive(Debug, Clone)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone)]
pub struct AwkString {
value: AwkStringVariant,
pub is_numeric: bool,
Expand Down
8 changes: 4 additions & 4 deletions awk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn exit_if_error<T, U: Display>(r: Result<T, U>) -> T {
match r {
Ok(v) => v,
Err(err) => {
eprintln!("{}", err);
eprintln!("{err}");
std::process::exit(1);
}
}
Expand All @@ -65,10 +65,10 @@ fn main() -> Result<(), Box<dyn Error>> {
let mut sources = Vec::new();
for source_file in &args.program_files {
let mut file = std::fs::File::open(source_file)
.map_err(|_| format!("could not open file '{}'", source_file))?;
.map_err(|_| gettext!("could not open file '{}'", source_file))?;
let mut contents = String::new();
file.read_to_string(&mut contents)
.map_err(|_| format!("could not read file '{}'", source_file))?;
.map_err(|_| gettext!("could not read file '{}'", source_file))?;
sources.push(SourceFile {
contents,
filename: source_file.clone(),
Expand All @@ -92,7 +92,7 @@ fn main() -> Result<(), Box<dyn Error>> {
args.separator_string,
))
} else {
eprintln!("missing program argument");
eprintln!("{}", gettext("missing program argument"));
1
};
std::process::exit(return_status);
Expand Down
52 changes: 31 additions & 21 deletions awk/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
//

use crate::regex::Regex;
use core::fmt;
use std::{collections::HashMap, rc::Rc};

pub type VarId = u32;

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum OpCode {
// binary operations
Add,
Expand Down Expand Up @@ -108,7 +108,8 @@ pub enum OpCode {
Invalid,
}

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Clone, PartialEq)]
pub enum Constant {
Number(f64),
String(Rc<str>),
Expand All @@ -133,38 +134,44 @@ impl From<Rc<Regex>> for Constant {
}
}

#[derive(Debug, PartialEq, Clone, Copy)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq, Clone, Copy)]
pub struct SourceLocation {
pub line: u32,
pub column: u32,
}

#[derive(Debug, PartialEq, Default)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq, Default)]
pub struct DebugInfo {
pub file: Rc<str>,
pub source_locations: Vec<SourceLocation>,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq)]
pub struct Action {
pub instructions: Vec<OpCode>,
pub debug_info: DebugInfo,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq)]
pub enum Pattern {
Expr(Action),
Range { start: Action, end: Action },
All,
}

#[derive(Debug, PartialEq)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq)]
pub struct AwkRule {
pub pattern: Pattern,
pub action: Action,
}

#[derive(Debug, PartialEq, Default)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq, Default)]
pub struct Function {
pub name: Rc<str>,
pub parameters_count: usize,
Expand All @@ -182,13 +189,14 @@ pub struct Program {
pub functions: Vec<Function>,
}

impl fmt::Debug for Program {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
#[cfg(test)]
impl core::fmt::Debug for Program {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
writeln!(f, "Program:")?;
for action in &self.begin_actions {
writeln!(f, " BEGIN {{")?;
for instruction in &action.instructions {
writeln!(f, " {:?}", instruction)?;
writeln!(f, " {instruction:?}")?;
}
writeln!(f, " }}")?;
}
Expand All @@ -198,18 +206,18 @@ impl fmt::Debug for Program {
Pattern::Expr(action) => {
writeln!(f, " /")?;
for instruction in &action.instructions {
writeln!(f, " {:?}", instruction)?;
writeln!(f, " {instruction:?}")?;
}
writeln!(f, " / {{")?;
}
Pattern::Range { start, end } => {
writeln!(f, " /")?;
for instruction in &start.instructions {
writeln!(f, " {:?}", instruction)?;
writeln!(f, " {instruction:?}")?;
}
writeln!(f, " /, /")?;
for instruction in &end.instructions {
writeln!(f, " {:?}", instruction)?;
writeln!(f, " {instruction:?}")?;
}
writeln!(f, " / {{")?;
}
Expand All @@ -218,36 +226,37 @@ impl fmt::Debug for Program {
}
}
for instruction in &rule.action.instructions {
writeln!(f, " {:?}", instruction)?;
writeln!(f, " {instruction:?}")?;
}
writeln!(f, " }}")?;
}

for action in &self.end_actions {
writeln!(f, " END {{")?;
for instruction in &action.instructions {
writeln!(f, " {:?}", instruction)?;
writeln!(f, " {instruction:?}")?;
}
writeln!(f, " }}")?;
}
writeln!(f, "Functions:")?;
for function in &self.functions {
writeln!(f, " {}({}) {{", function.name, function.parameters_count)?;
for instruction in &function.instructions {
writeln!(f, " {:?}", instruction)?;
writeln!(f, " {instruction:?}")?;
}
writeln!(f, " }}")?;
}
writeln!(f, "Constants:")?;
for (i, constant) in self.constants.iter().enumerate() {
writeln!(f, " {}: {:?}", i, constant)?;
writeln!(f, " {i}: {constant:?}")?;
}
Ok(())
}
}

#[repr(u32)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum SpecialVar {
Argc,
Argv,
Expand All @@ -271,7 +280,8 @@ pub enum SpecialVar {
}

#[repr(u32)]
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[cfg_attr(test, derive(Debug))]
#[derive(PartialEq, Eq, Clone, Copy)]
pub enum BuiltinFunction {
// arithmetic functions
Atan2,
Expand Down
10 changes: 5 additions & 5 deletions awk/src/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
// SPDX-License-Identifier: MIT
//

use core::fmt;
use std::ffi::CString;
use std::fmt::Formatter;
use std::ptr;

fn regex_compilation_result(
Expand Down Expand Up @@ -41,7 +39,8 @@ pub struct Regex {
regex_string: CString,
}

#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
#[cfg_attr(test, derive(Debug))]
#[derive(Copy, Clone, Default, PartialEq, Eq)]
pub struct RegexMatch {
pub start: usize,
pub end: usize,
Expand Down Expand Up @@ -126,8 +125,9 @@ impl Drop for Regex {
}
}

impl fmt::Debug for Regex {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
#[cfg(test)]
impl core::fmt::Debug for Regex {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
writeln!(f, "/{}/", self.regex_string.to_str().unwrap())
}
}
Expand Down