diff --git a/rust/src/nasl/syntax/error.rs b/rust/src/nasl/syntax/error.rs index 62a6642f9..fd5ad648c 100644 --- a/rust/src/nasl/syntax/error.rs +++ b/rust/src/nasl/syntax/error.rs @@ -46,7 +46,7 @@ pub enum ErrorKind { #[error("{kind}")] pub struct SyntaxError { /// A human readable reason why this error is returned - kind: ErrorKind, + pub kind: ErrorKind, pub(crate) line: u32, pub(crate) file: String, } diff --git a/rust/src/nasl/syntax/token.rs b/rust/src/nasl/syntax/token.rs index 8a5744c03..0d35069bf 100644 --- a/rust/src/nasl/syntax/token.rs +++ b/rust/src/nasl/syntax/token.rs @@ -436,11 +436,7 @@ impl Token { impl Display for Token { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!( - f, - "{}:{} {}", - self.line_column.0, self.line_column.1, self.category - ) + write!(f, "'{}'", self.category,) } } @@ -450,6 +446,16 @@ impl Token { &self.category } + /// Returns the line number of the token + pub fn line(&self) -> usize { + self.line_column.0 + } + + /// Returns the column number of the token + pub fn column(&self) -> usize { + self.line_column.1 + } + /// Returns true when an Token is faulty /// /// A Token is faulty when it is a syntactical error like diff --git a/rust/src/scannerctl/error.rs b/rust/src/scannerctl/error.rs index 96848bfdd..3eb6dfa1c 100644 --- a/rust/src/scannerctl/error.rs +++ b/rust/src/scannerctl/error.rs @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: GPL-2.0-or-later WITH x11vnc-openssl-exception +use std::fmt::Display; use std::path::{Path, PathBuf}; use feed::VerifyError; @@ -62,12 +63,20 @@ impl CliErrorKind { } #[derive(Debug, thiserror::Error)] -#[error("{kind} ({filename})")] pub struct CliError { pub filename: String, pub kind: CliErrorKind, } +impl Display for CliError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + if !self.filename.is_empty() { + write!(f, "{}: ", self.filename)?; + } + write!(f, "{}", self.kind) + } +} + impl CliError { pub fn load_error(err: std::io::Error, path: &Path) -> Self { Self { diff --git a/rust/src/scannerctl/syntax/check.rs b/rust/src/scannerctl/syntax/check.rs index 83b7aa0b9..e630dd119 100644 --- a/rust/src/scannerctl/syntax/check.rs +++ b/rust/src/scannerctl/syntax/check.rs @@ -10,16 +10,6 @@ use walkdir::WalkDir; use crate::{CliError, CliErrorKind}; -fn read_errors>(path: P) -> Result, CliErrorKind> { - let code = load_non_utf8_path(path.as_ref())?; - Ok(parse(&code) - .filter_map(|r| match r { - Ok(_) => None, - Err(err) => Some(err), - }) - .collect()) -} - fn read>(path: P) -> Result>, CliErrorKind> { let code = load_non_utf8_path(path.as_ref())?; Ok(parse(&code).collect()) @@ -28,28 +18,47 @@ fn read>(path: P) -> Result>, fn print_results(path: &Path, verbose: bool) -> Result { let mut errors = 0; - if verbose { - println!("# {path:?}"); - let results = read(path).map_err(|kind| CliError { - kind, - filename: format!("{path:?}"), - })?; - for r in results { - match r { - Ok(stmt) => println!("{stmt:?}"), - Err(err) => eprintln!("{err}"), - } + let print_error = |err: &SyntaxError| { + if let Some(token) = err.as_token() { + eprintln!( + "{}:{}:{}: {}", + path.to_string_lossy(), + token.line(), + token.column(), + err.kind + ) + } else { + eprintln!("{}:{}", path.to_string_lossy(), err) } - } else { - let err = read_errors(path).map_err(|kind| CliError { - kind, - filename: format!("{path:?}"), - })?; - if !err.is_empty() { - eprintln!("# Error in {path:?}"); + }; + let print_stmt = |stmt: Statement| { + println!( + "{}:{}:{}: {}", + path.to_string_lossy(), + stmt.as_token().line(), + stmt.as_token().column(), + stmt + ) + }; + + let results = read(path).map_err(|kind| CliError { + kind, + filename: path.to_string_lossy().to_string(), + })?; + for r in results { + match r { + Ok(stmt) if verbose => print_stmt(stmt), + Ok(_) => {} + Err(err) => { + // when we run in interactive mode we should print a new line to + // not interfere with the count display. + if errors == 0 { + eprintln!() + } + errors += 1; + print_error(&err) + } } - errors += err.len(); - err.iter().for_each(|r| eprintln!("{r}")); } Ok(errors) }