From 47c7735a67fd7b61ef4ecb2dedba1f79761643e9 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 21 Jun 2024 04:16:03 +0200 Subject: [PATCH 1/7] First draft of the makepdf feature --- Cargo.toml | 2 ++ src/main.rs | 5 +++ src/makepdf.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 src/makepdf.rs diff --git a/Cargo.toml b/Cargo.toml index 2e83686..e196ff6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,11 +13,13 @@ csv = "1.3.0" debug_print = "1.0.0" dynfmt = { version = "0.1.5", features = ["curly"] } enum_dispatch = "0.3.13" +futures = "0.3.30" glob = "0.3.1" phf = { version = "0.11.2", features = ["macros"] } regex = "1.10.4" regex_static = "0.1.1" serde = { version = "1.0.198", features = ["derive"] } +tokio = { version = "1.38.0", features = ["full"] } toml = "0.8.12" [profile.dev] diff --git a/src/main.rs b/src/main.rs index 1b5deb9..4618302 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,7 @@ // #![allow(dead_code)] #![warn(missing_docs)] #![warn(clippy::missing_docs_in_private_items)] +mod makepdf; mod stat_algo; mod stat_io; mod stat_structs; @@ -21,6 +22,7 @@ use std::{ use anyhow::{Context, Result}; use clap::{Args, Parser, Subcommand}; +use makepdf::{make_pdf, MakeConfig}; use serde::Deserialize; use stats::{stat_solver, StatConfig}; use table_structs::Craft; @@ -45,6 +47,8 @@ enum Commands { Config(ConfigArgs), /// Handle stat repartition Stats(StatConfig), + /// Make a pdf via pdflatex + Make(MakeConfig), } #[derive(Debug, Args)] @@ -90,6 +94,7 @@ fn main() -> Result<()> { Commands::Tablegen(args) => table_gen(&args, &config), Commands::Config(args) => write_config(&args, &config, config_str), Commands::Stats(args) => stat_solver(&args, &config), + Commands::Make(args) => make_pdf(&args, &config), } } diff --git a/src/makepdf.rs b/src/makepdf.rs new file mode 100644 index 0000000..c336b89 --- /dev/null +++ b/src/makepdf.rs @@ -0,0 +1,92 @@ +//! Make a pdf using pdflatex + +use anyhow::Result; +use clap::Args; + +use crate::Config; + +#[derive(Args, Debug, Clone)] +/// The configuration struct for the PdfMaker +pub(crate) struct MakeConfig { + /// The filename to compile + pub(crate) filename: String, + /// The verbosity level + #[arg(short, long, default_value_t = false)] + pub(crate) verbosity: bool, +} + +/// Make a pdf spawning a subprocess +pub(crate) fn make_pdf(args: &MakeConfig, config: &Config) -> Result<()> { + let _ = config; + launch_subprocess(args).unwrap(); + Ok(()) +} + +use tokio::io::{AsyncBufReadExt, BufReader}; +use tokio::process::Command; + +use std::process::Stdio; + +#[tokio::main] +/// Pdfreading and curating +async fn launch_subprocess(args: &MakeConfig) -> Result<(), Box> { + let mut cmd = Command::new("pdflatex"); + // cmd.arg("-8bit"); + cmd.arg(&args.filename); + + // Specify that we want the command's standard output piped back to us. + // By default, standard input/output/error will be inherited from the + // current process (for example, this means that standard input will + // come from the keyboard and standard output/error will go directly to + // the terminal if this process is invoked from the command line). + cmd.stdout(Stdio::piped()); + + let mut child = cmd.spawn().expect("failed to spawn command"); + + let stdout = child + .stdout + .take() + .expect("child did not have a handle to stdout"); + + let mut reader = BufReader::new(stdout).split(0xA); + + // Ensure the child process is spawned in the runtime so it can + // make progress on its own while we await for any output. + tokio::spawn(async move { + let status = child + .wait() + .await + .expect("child process encountered an error"); + + println!("child status was: {}", status); + }); + + // while let Some(line) = reader.next_line().await? { + // // let vec: Vec = line.into(); + // println!("Line: {:?}", line); + // } + loop { + let res = reader.next_segment().await; + if let Err(e) = res { + println!("{}", e); + break; + } else if let Ok(line) = res { + if let Some(line) = line { + use std::str; + let s = match str::from_utf8(&line) { + Ok(v) => v, + Err(e) => { + println!("Invalid UTF-8 sequence: {}", e); + continue; + } + }; + println!("Line: {:?}", s); + } else { + break; + } + } + // let vec: Vec = line.into(); + } + + Ok(()) +} -- 2.45.1 From 2cc32f41ce071a3c520f6683478ac29dfd7d0a13 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 21 Jun 2024 15:42:45 +0200 Subject: [PATCH 2/7] Weirdness --- src/makepdf.rs | 108 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 82 insertions(+), 26 deletions(-) diff --git a/src/makepdf.rs b/src/makepdf.rs index c336b89..be56e15 100644 --- a/src/makepdf.rs +++ b/src/makepdf.rs @@ -1,7 +1,13 @@ //! Make a pdf using pdflatex -use anyhow::Result; +// use std::fmt::Display; +use std::str; +use tokio::io::{AsyncBufReadExt, BufReader}; +use tokio::process::Command; + +use anyhow::{anyhow, Result}; use clap::Args; +use std::process::Stdio; use crate::Config; @@ -22,31 +28,63 @@ pub(crate) fn make_pdf(args: &MakeConfig, config: &Config) -> Result<()> { Ok(()) } -use tokio::io::{AsyncBufReadExt, BufReader}; -use tokio::process::Command; +#[derive(Debug)] +/// The current status of a run, holding all final info +struct Status { + /// Number of encountered info (mainly o/ufull) + info: usize, + /// Number of warnings, mainly label/reference rerun + warnings: usize, + /// Only makes sense with non-interactie builds + errors: usize, + /// List of all recoverable + v_errors: Vec, +} -use std::process::Stdio; +impl Status { + /// Add a recoverable error to the list of errors + fn add_error(&mut self, error_msg: String) { + self.errors += 1; + self.v_errors.push(error_msg); + } +} + +// impl Display for Status { +// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +// todo!() +// } +// } + +impl Status { + /// Initialize a new status + fn zero() -> Self { + Self { + info: 0, + warnings: 0, + errors: 0, + v_errors: Vec::new(), + } + } +} #[tokio::main] /// Pdfreading and curating -async fn launch_subprocess(args: &MakeConfig) -> Result<(), Box> { +async fn launch_subprocess(args: &MakeConfig) -> Result { let mut cmd = Command::new("pdflatex"); - // cmd.arg("-8bit"); + let mut status = Status::zero(); + let mut context = Vec::new(); cmd.arg(&args.filename); + cmd.env("max_print_line", "1000"); // Specify that we want the command's standard output piped back to us. - // By default, standard input/output/error will be inherited from the - // current process (for example, this means that standard input will - // come from the keyboard and standard output/error will go directly to - // the terminal if this process is invoked from the command line). cmd.stdout(Stdio::piped()); - let mut child = cmd.spawn().expect("failed to spawn command"); + let mut child = cmd.spawn().expect("Failed to spawn command"); let stdout = child .stdout .take() - .expect("child did not have a handle to stdout"); + .expect("Child did not have a handle to stdout"); let mut reader = BufReader::new(stdout).split(0xA); @@ -56,37 +94,55 @@ async fn launch_subprocess(args: &MakeConfig) -> Result<(), Box = line.into(); - // println!("Line: {:?}", line); - // } loop { let res = reader.next_segment().await; if let Err(e) = res { - println!("{}", e); - break; + return Err(anyhow!("[CORE] Runtime error : {}", e)); } else if let Ok(line) = res { if let Some(line) = line { - use std::str; let s = match str::from_utf8(&line) { Ok(v) => v, Err(e) => { - println!("Invalid UTF-8 sequence: {}", e); + // status.errors += 1; + status.add_error(format!("[CORE] Invalid UTF-8 sequence: {}", e)); continue; } }; - println!("Line: {:?}", s); + // println!("Line: {:?}", s); + checkline(s, &mut status, &mut context).expect("Error while checking line"); } else { break; } } - // let vec: Vec = line.into(); } - - Ok(()) + // println!("{:?}", status); + Ok(status) } + +// /// The line encountered +// enum LineType { +// /// The tex building mode +// Mode, +// Info, +// BoxFullness, +// /// Unknown type +// Unknown(String), +// } +// /// Check each line and changes the status and/or display according warnings +// fn checkline(s: &str, status: &mut Status, context: &mut Vec) -> Result<()> { +// // Match the line to guess what is happening +// // match s { +// // s if s.starts_with("entering_extended_mode") => LineType::Mode, +// // _ => LineType::Unknown(s.to_string()), +// // } +// // status.warnings += 1; +// // if status.warnings % 3 == 0 { +// // println!("{}", s); +// // } +// Ok(()) +// } -- 2.45.1 From 9ee72e03b3ab4b8ef740a0c61aebe08179d44863 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 21 Jun 2024 16:47:08 +0200 Subject: [PATCH 3/7] WZ2 ? --- Cargo.toml | 10 +++++----- src/makepdf.rs | 43 +++++++++++++++++++++---------------------- 2 files changed, 26 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e196ff6..95fec29 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,8 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1.0.82" -clap = { version = "4.5.4", features = ["derive"] } +anyhow = "1.0.86" +clap = { version = "4.5.7", features = ["derive"] } csv = "1.3.0" debug_print = "1.0.0" dynfmt = { version = "0.1.5", features = ["curly"] } @@ -16,11 +16,11 @@ enum_dispatch = "0.3.13" futures = "0.3.30" glob = "0.3.1" phf = { version = "0.11.2", features = ["macros"] } -regex = "1.10.4" +regex = "1.10.5" regex_static = "0.1.1" -serde = { version = "1.0.198", features = ["derive"] } +serde = { version = "1.0.203", features = ["derive"] } tokio = { version = "1.38.0", features = ["full"] } -toml = "0.8.12" +toml = "0.8.14" [profile.dev] debug = 0 diff --git a/src/makepdf.rs b/src/makepdf.rs index be56e15..799655f 100644 --- a/src/makepdf.rs +++ b/src/makepdf.rs @@ -124,25 +124,24 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { Ok(status) } -// /// The line encountered -// enum LineType { -// /// The tex building mode -// Mode, -// Info, -// BoxFullness, -// /// Unknown type -// Unknown(String), -// } -// /// Check each line and changes the status and/or display according warnings -// fn checkline(s: &str, status: &mut Status, context: &mut Vec) -> Result<()> { -// // Match the line to guess what is happening -// // match s { -// // s if s.starts_with("entering_extended_mode") => LineType::Mode, -// // _ => LineType::Unknown(s.to_string()), -// // } -// // status.warnings += 1; -// // if status.warnings % 3 == 0 { -// // println!("{}", s); -// // } -// Ok(()) -// } +/// The line encountered +#[derive(Debug)] +enum LineType { + /// The tex building mode + Mode, + Info, + BoxFullness, + /// Unknown type + Unknown(String), +} +/// Check each line and changes the status and/or display according warnings +fn checkline(s: &str, status: &mut Status, context: &mut Vec) -> Result<()> { + // Match the line to guess what is happening + let line = match s { + s if s.starts_with("entering_extended_mode") => LineType::Mode, + _ => LineType::Unknown(s.to_string()), + }; + // status.warnings += 1; + println!("{:?}", line); + Ok(()) +} -- 2.45.1 From 8b110c9d6d5bd2ebe1d176910db3a3f931a3e62a Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Fri, 21 Jun 2024 19:40:42 +0200 Subject: [PATCH 4/7] Adds some q&d line identifiers --- src/makepdf.rs | 48 +++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/makepdf.rs b/src/makepdf.rs index 799655f..e750fa3 100644 --- a/src/makepdf.rs +++ b/src/makepdf.rs @@ -125,23 +125,69 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { } /// The line encountered +// #[allow(unused)] #[derive(Debug)] enum LineType { /// The tex building mode Mode, + /// Info level Info, + /// TabularArray + //TODO: Change to size and not full line ? + TabularWarn(String), + /// Warning level + Warning(String), + /// PdfTex Version + PdfTexVersion(String), + /// Context, mosty for boxes + Context(String), + /// Overfull and underfull boxes BoxFullness, /// Unknown type Unknown(String), + /// Empty line, to discard + Empty, + /// The Latex Version + LaTexVersion(String), + /// The write18 mode, whatever that is + WriteMode(String), + /// The class of document (book, article, etc) + DocumentClass(String), + /// Signals that the parser entered a document + EnteringDocument(String), + /// Info on the loading of a library + Library(String), + /// Loading (Load something) + Loading(String), } + /// Check each line and changes the status and/or display according warnings fn checkline(s: &str, status: &mut Status, context: &mut Vec) -> Result<()> { // Match the line to guess what is happening let line = match s { s if s.starts_with("entering_extended_mode") => LineType::Mode, + s if s.starts_with("entering extended mode") => LineType::Mode, + s if s.starts_with("This is pdfTeX") => LineType::PdfTexVersion(s.to_string()), + s if s.starts_with("LaTeX2e") => LineType::LaTexVersion(s.to_string()), + s if s.starts_with("Package tabularray Warning") => LineType::TabularWarn(s.to_string()), + s if s.starts_with("Overfull") => LineType::BoxFullness, + s if s.starts_with("Underfull") => LineType::BoxFullness, + s if s.starts_with("L3 programming layer") => LineType::Info, + s if s.starts_with(" restricted \\write18 enabled.") => LineType::Context(s.to_string()), + s if s.starts_with(" [") => LineType::WriteMode(s.to_string()), + s if s.starts_with("Document Class") => LineType::DocumentClass(s.to_string()), + s if s.starts_with("Library") => LineType::Library(s.to_string()), + s if s.starts_with("(./") => LineType::EnteringDocument(s.to_string()), + s if s.starts_with("(/usr/share/texlive/") => LineType::Loading(s.to_string()), + // "LaTeX Warning: Reference" + "" => LineType::Empty, _ => LineType::Unknown(s.to_string()), }; // status.warnings += 1; - println!("{:?}", line); + match line { + LineType::Warning(w) => println!("{}", w), + LineType::Unknown(w) => println!("Unknown: {}", w), + _ => (), + } Ok(()) } -- 2.45.1 From 8f113ffdc3c362595e932d2434f2c5f6aef1950c Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 22 Jun 2024 00:07:24 +0200 Subject: [PATCH 5/7] Cleaning up the mess with logos --- Cargo.toml | 1 + src/makepdf.rs | 93 +++++++++++++++++++++++++++++++------------------- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 95fec29..235f299 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,6 +15,7 @@ dynfmt = { version = "0.1.5", features = ["curly"] } enum_dispatch = "0.3.13" futures = "0.3.30" glob = "0.3.1" +logos = "0.14.0" phf = { version = "0.11.2", features = ["macros"] } regex = "1.10.5" regex_static = "0.1.1" diff --git a/src/makepdf.rs b/src/makepdf.rs index e750fa3..8d8e972 100644 --- a/src/makepdf.rs +++ b/src/makepdf.rs @@ -1,6 +1,7 @@ //! Make a pdf using pdflatex // use std::fmt::Display; +use logos::Logos; use std::str; use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::process::Command; @@ -74,7 +75,7 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { let mut status = Status::zero(); let mut context = Vec::new(); cmd.arg(&args.filename); - cmd.env("max_print_line", "1000"); + cmd.env("max_print_line", "3000"); // Specify that we want the command's standard output piped back to us. cmd.stdout(Stdio::piped()); @@ -126,68 +127,88 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { /// The line encountered // #[allow(unused)] -#[derive(Debug)] +#[derive(Debug, Logos, PartialEq)] +// #[logos()] enum LineType { /// The tex building mode + #[regex("entering[_ ]extended[_ ]mode")] Mode, + // s if s.starts_with("Package tabularray Warning") => LineType::TabularWarn(s.to_string()), + // s if s.starts_with(" [") => LineType::WriteMode(s.to_string()), + // s if s.starts_with("Library") => LineType::Library(s.to_string()), + // s if s.starts_with("(/usr/share/texlive/") => LineType::Loading(s.to_string()), + // // "LaTeX Warning: Reference" /// Info level + #[token("L3 programming layer")] Info, + /// TabularArray - //TODO: Change to size and not full line ? TabularWarn(String), + /// Warning level Warning(String), - /// PdfTex Version - PdfTexVersion(String), + + /// PdfTex VersionThis is pdfTeX, Version 3.141592653-2.6-1.40.22 + #[token("This is pdfTeX")] + PdfTexVersion, + /// Context, mosty for boxes Context(String), + /// Overfull and underfull boxes + #[regex("(over|under)full")] BoxFullness, + /// The Latex Version + #[regex("LaTeX2e")] + LaTexVersion, + /// The write18 mode, whatever that is + #[token(" restricted \\write18 enabled.")] + WriteMode, + + /// The class of document (book, article, etc) + // s if s.starts_with("Document Class") => LineType::DocumentClass(s.to_string()), + #[token("Document Class:")] + DocumentClass, + + /// Signals that the parser entered a document + #[regex(r"\(\./\w+\.tex", |lex| lex.slice()[3..].to_string())] + EnteringDocument(String), + + /// Info on the loading of a library + Library(String), + + /// Loading (Load something) + #[regex(r"(\) )?\(/usr[^)]*", |lex| lex.slice().split("/").last().unwrap_or("").to_string() )] + Loading(String), + /// Unknown type Unknown(String), /// Empty line, to discard Empty, - /// The Latex Version - LaTexVersion(String), - /// The write18 mode, whatever that is - WriteMode(String), - /// The class of document (book, article, etc) - DocumentClass(String), - /// Signals that the parser entered a document - EnteringDocument(String), - /// Info on the loading of a library - Library(String), - /// Loading (Load something) - Loading(String), } /// Check each line and changes the status and/or display according warnings fn checkline(s: &str, status: &mut Status, context: &mut Vec) -> Result<()> { // Match the line to guess what is happening - let line = match s { - s if s.starts_with("entering_extended_mode") => LineType::Mode, - s if s.starts_with("entering extended mode") => LineType::Mode, - s if s.starts_with("This is pdfTeX") => LineType::PdfTexVersion(s.to_string()), - s if s.starts_with("LaTeX2e") => LineType::LaTexVersion(s.to_string()), - s if s.starts_with("Package tabularray Warning") => LineType::TabularWarn(s.to_string()), - s if s.starts_with("Overfull") => LineType::BoxFullness, - s if s.starts_with("Underfull") => LineType::BoxFullness, - s if s.starts_with("L3 programming layer") => LineType::Info, - s if s.starts_with(" restricted \\write18 enabled.") => LineType::Context(s.to_string()), - s if s.starts_with(" [") => LineType::WriteMode(s.to_string()), - s if s.starts_with("Document Class") => LineType::DocumentClass(s.to_string()), - s if s.starts_with("Library") => LineType::Library(s.to_string()), - s if s.starts_with("(./") => LineType::EnteringDocument(s.to_string()), - s if s.starts_with("(/usr/share/texlive/") => LineType::Loading(s.to_string()), - // "LaTeX Warning: Reference" - "" => LineType::Empty, + let mut lex = LineType::lexer(s); + // println!("{:?}", lex); + // println!("{:?}", lex.next()); + let line = match lex.next() { + Some(Ok(m)) => m, + None => LineType::Empty, + // Some(Err(e)) => { + // return Err(anyhow!( + // "Encountered an error while parsing line {}: {:?}", + // s.to_string(), + // e + // )) + // } _ => LineType::Unknown(s.to_string()), }; - // status.warnings += 1; match line { LineType::Warning(w) => println!("{}", w), LineType::Unknown(w) => println!("Unknown: {}", w), - _ => (), + _ => println!("{:?}", line), } Ok(()) } -- 2.45.1 From a05b8c309b72a4db7886c5cb2ef32e57e2dedc6b Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 22 Jun 2024 05:47:56 +0200 Subject: [PATCH 6/7] Adds Goodenough catching --- .cargo/config.toml | 2 +- Cargo.toml | 9 +++ src/makepdf.rs | 137 +++++++++++++++++++++++++++++++++++---------- 3 files changed, 118 insertions(+), 30 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index c4bb6e0..aef34ed 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,2 +1,2 @@ [build] -rustflags = ["-Clink-arg=-fuse-ld=lld", "-Ctarget-cpu=native"] +rustflags = ["-Clink-arg=-fuse-ld=lld", "-Ctarget-cpu=native", "-Zthreads=24"] diff --git a/Cargo.toml b/Cargo.toml index 235f299..f3b76e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -42,3 +42,12 @@ lto = true codegen-units = 1 incremental = false panic = "abort" + +[profile.base] +inherits = "dev" +opt-level = 0 +debug = 0 +strip = "none" +lto = false +codegen-units = 256 +incremental = false diff --git a/src/makepdf.rs b/src/makepdf.rs index 8d8e972..e6f7f39 100644 --- a/src/makepdf.rs +++ b/src/makepdf.rs @@ -1,7 +1,7 @@ //! Make a pdf using pdflatex // use std::fmt::Display; -use logos::Logos; +use logos::{Lexer, Logos}; use std::str; use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::process::Command; @@ -128,35 +128,55 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { /// The line encountered // #[allow(unused)] #[derive(Debug, Logos, PartialEq)] -// #[logos()] +#[logos(skip r"[) ]+")] enum LineType { /// The tex building mode #[regex("entering[_ ]extended[_ ]mode")] Mode, - // s if s.starts_with("Package tabularray Warning") => LineType::TabularWarn(s.to_string()), - // s if s.starts_with(" [") => LineType::WriteMode(s.to_string()), - // s if s.starts_with("Library") => LineType::Library(s.to_string()), - // s if s.starts_with("(/usr/share/texlive/") => LineType::Loading(s.to_string()), - // // "LaTeX Warning: Reference" /// Info level - #[token("L3 programming layer")] - Info, + #[token("L3 programming layer", |lex| lex.slice().to_string())] + #[token("For additional information", |lex| lex.slice().to_string())] + #[token("Pictures in Paragraphs", |lex| lex.slice().to_string())] + #[token("2021/01/20 v 2.00 tkz-graph.sty", |lex| lex.slice().to_string())] + Info(String), - /// TabularArray - TabularWarn(String), + /// Strange starting sequence + #[token("? [1{")] + StartingSequence, - /// Warning level + /// Strange end sequence + #[token(" ){/")] + EndSequence, + + /// Converter [Loading MPS to PDF converter (version 2006.09.02).] + #[token("[Loading MPS to PDF converter")] + MPSConverter, + + /// A chapter was processed + #[regex(r"Chapitre [0-9]+", |lex| lex.slice()[9..].to_string())] + Chapter(String), + + /// Missing reference + #[token(r"LaTeX Warning: Reference")] + MissingReference, + + /// Generic Latex Warning + #[regex(r"(LaTex |Warning:){2}[^\n]+", |lex| lex.slice()[14..].to_string())] Warning(String), + // /// Runaway argument + // #[token("Runaway argument?")] + // RunAway, /// PdfTex VersionThis is pdfTeX, Version 3.141592653-2.6-1.40.22 #[token("This is pdfTeX")] PdfTexVersion, /// Context, mosty for boxes + #[regex(r"\[\d[^\]]+", |lex| lex.slice()[1..lex.slice().len()-1].to_string())] Context(String), /// Overfull and underfull boxes - #[regex("(over|under)full")] + #[regex("(Over|Under)full")] BoxFullness, /// The Latex Version #[regex("LaTeX2e")] @@ -171,44 +191,103 @@ enum LineType { DocumentClass, /// Signals that the parser entered a document - #[regex(r"\(\./\w+\.tex", |lex| lex.slice()[3..].to_string())] + //TODO: Fix name collection + // #[regex(r"(\)+ )?\(\./[A-z/]+\.tex", |lex| lex.slice().split("(").next().unwrap_or("").to_string())] + // #[regex(r"(\)+ )?(\[\w+\])?\(\./[A-z/]+\.tex", |lex| lex.slice().split("(").next().unwrap_or("").to_string())] + + // #[regex(r"[\) ]*\(\./[A-z/]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())] + // #[regex(r"[\) ]*(\[[0-9]+\] )?\(\./[A-z/-_]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())] + // #[regex(r"[\) 0-9\[\]]*\(\./[A-z/-_]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())] + #[regex(r"\(\./[A-z/_-]+\.tex", |lex| lex.slice().split("(").nth(1).unwrap_or("").to_string())] EnteringDocument(String), /// Info on the loading of a library - Library(String), + #[regex(r"Library \(\w+\)[^\n]+", |lex| split_by(":",&lex.slice()[8..]))] + Library((String, String)), + + /// The warning generated by a package + #[regex(r"Package [A-z_-]+ Warning: [^\n]+", |lex| split_by(":",lex.slice()))] + PackageWarning((String, String)), + + /// The warning context generated by a package + #[regex(r"\([A-z_-]+\) [^\n]+", |lex| split_by(")",&lex.slice()[1..]))] + PackageContextWarning((String, String)), /// Loading (Load something) - #[regex(r"(\) )?\(/usr[^)]*", |lex| lex.slice().split("/").last().unwrap_or("").to_string() )] + #[regex(r"(\)+ )?\(/usr[^)]*", |lex| lex.slice().split("/").last().unwrap_or("").to_string())] Loading(String), /// Unknown type Unknown(String), + + /// Building blocks, signals pages being rendered ? + #[token(" []")] + #[token("[]")] + PageGen, + + /// Output + // #[regex(r"Output written on", |lex| lex.slice()[18..].to_string())] + #[token(r"Output written on")] + Output, + + /// Log + // #[regex("Transcript written on [^\n]+", |lex| lex.slice()[18..].to_string())] + #[token("Transcript written on")] + Log, + + // /// Barely parsable data + // #[regex(r"\)+")] + // Garbage, /// Empty line, to discard Empty, + + /// Geometry package + #[token("*geometry* driver: auto-detecting")] + GeometryResolution, + + /// Geometry found + #[token("*geometry* detected driver: pdftex")] + GeometryFound, + + /// Transcript + #[token("(see the transcript file for additional information)")] + Transcript, +} + +/// Tries to split the token and return two string +fn split_by(delim: &str, text: &str) -> Option<(String, String)> { + let mut slice = text.split(delim); + // println!("{:?}", text); + if let Some(one) = slice.next() { + // println!("{:?}", one); + if let Some(two) = slice.next() { + // println!("{} {:?}", one, slice.next()); + return Some((one.to_string(), two.trim_start().to_string())); + } + } + None } /// Check each line and changes the status and/or display according warnings -fn checkline(s: &str, status: &mut Status, context: &mut Vec) -> Result<()> { +fn checkline(s: &str, status: &mut Status, context: &mut [String]) -> Result<()> { // Match the line to guess what is happening let mut lex = LineType::lexer(s); - // println!("{:?}", lex); - // println!("{:?}", lex.next()); let line = match lex.next() { Some(Ok(m)) => m, None => LineType::Empty, - // Some(Err(e)) => { - // return Err(anyhow!( - // "Encountered an error while parsing line {}: {:?}", - // s.to_string(), - // e - // )) - // } _ => LineType::Unknown(s.to_string()), }; match line { - LineType::Warning(w) => println!("{}", w), - LineType::Unknown(w) => println!("Unknown: {}", w), - _ => println!("{:?}", line), + LineType::Unknown(w) => println!("[UKWN] {}", w), + LineType::Empty => (), + LineType::EnteringDocument(_) => println!("{:?}", line), + // LineType::TabWarn(_) => println!("{:?}", line), + // LineType::Library(_) => println!("{:?}", line), + // LineType::PackageWarning(_) => println!("{:?},{}", line, s), + LineType::PackageContextWarning(_) => println!("{:?}", line), + LineType::MissingReference => println!("{:?} {}", line, s), + // LineType::=> (), + _ => (), } Ok(()) } -- 2.45.1 From cfa16aa1c85feb3074683c1272ff0c23e8de6c13 Mon Sep 17 00:00:00 2001 From: Dandaedre Date: Sat, 22 Jun 2024 19:29:23 +0200 Subject: [PATCH 7/7] Good first draft of the pdf maker --- src/makepdf.rs | 176 +++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 156 insertions(+), 20 deletions(-) diff --git a/src/makepdf.rs b/src/makepdf.rs index e6f7f39..7354a75 100644 --- a/src/makepdf.rs +++ b/src/makepdf.rs @@ -2,6 +2,7 @@ // use std::fmt::Display; use logos::{Lexer, Logos}; +use phf::phf_map; use std::str; use tokio::io::{AsyncBufReadExt, BufReader}; use tokio::process::Command; @@ -18,8 +19,59 @@ pub(crate) struct MakeConfig { /// The filename to compile pub(crate) filename: String, /// The verbosity level - #[arg(short, long, default_value_t = false)] - pub(crate) verbosity: bool, + #[arg(short, long, default_value = "warning")] + pub(crate) verbosity: VerboseLevel, + // /// The list of whitelisted verbosity levels + // pub(crate) whitelist: Vec, + // /// The list of blacklisted verbosity levels + // pub(crate) blacklist: Vec, +} + +/// The verbosity level +#[derive(Clone, Debug, PartialEq, PartialOrd)] +pub enum VerboseLevel { + /// Shouldn't be printed no matter what + None, + /// Garbage + Misc, + /// Boxness, barely above Misc + Box, + /// Context info (Debug info) + Context, + /// Usefull info + Info, + /// Important warnings + Warning, + /// Errors encountered requiring user input + Error, + /// Uncatachable errors + Critical, + /// Literrally uncatchable things + Uncatchable, +} + +static STR_TO_TARGET: phf::Map<&'static str, VerboseLevel> = phf_map! { + "misc" =>VerboseLevel::Misc, + "box" => VerboseLevel::Box, + "context" => VerboseLevel::Context, + "info" => VerboseLevel::Info, + "warning" => VerboseLevel::Warning, + "error" => VerboseLevel::Error, + "critical" => VerboseLevel::Critical, +}; + +impl From<&str> for VerboseLevel { + fn from(value: &str) -> Self { + if let Some(target) = STR_TO_TARGET.get(value).cloned() { + target + } else { + panic!( + "Invalid debug level {}. The debug level available are: {:?}", + value, + STR_TO_TARGET.keys() + ) + } + } } /// Make a pdf spawning a subprocess @@ -115,7 +167,7 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { } }; // println!("Line: {:?}", s); - checkline(s, &mut status, &mut context).expect("Error while checking line"); + checkline(args, s, &mut status, &mut context).expect("Error while checking line"); } else { break; } @@ -130,7 +182,7 @@ async fn launch_subprocess(args: &MakeConfig) -> Result { #[derive(Debug, Logos, PartialEq)] #[logos(skip r"[) ]+")] enum LineType { - /// The tex building mode + /// The tex building mode : entering extended mode #[regex("entering[_ ]extended[_ ]mode")] Mode, /// Info level @@ -141,6 +193,7 @@ enum LineType { Info(String), /// Strange starting sequence + #[token("? ) [")] #[token("? [1{")] StartingSequence, @@ -158,7 +211,10 @@ enum LineType { /// Missing reference #[token(r"LaTeX Warning: Reference")] - MissingReference, + MissingReferencePrs, + + /// Missed reference, completed with all info + MissingReference(String), /// Generic Latex Warning #[regex(r"(LaTex |Warning:){2}[^\n]+", |lex| lex.slice()[14..].to_string())] @@ -172,6 +228,7 @@ enum LineType { PdfTexVersion, /// Context, mosty for boxes + // TODO: False positives / Reparsing Context("10") )) [107] (./Ecoles/Protecteur.tex #[regex(r"\[\d[^\]]+", |lex| lex.slice()[1..lex.slice().len()-1].to_string())] Context(String), @@ -254,6 +311,42 @@ enum LineType { Transcript, } +impl LineType { + /// Decides all verbose levels, for output + fn vlvl(&self) -> VerboseLevel { + match &self { + LineType::Mode => VerboseLevel::Misc, + LineType::Info(_) => VerboseLevel::Misc, + LineType::StartingSequence => VerboseLevel::Misc, + LineType::EndSequence => VerboseLevel::Misc, + LineType::MPSConverter => VerboseLevel::Misc, + LineType::Chapter(_) => VerboseLevel::Context, + LineType::MissingReferencePrs => VerboseLevel::Warning, + LineType::Warning(_) => VerboseLevel::Warning, + LineType::MissingReference(_) => VerboseLevel::Warning, + LineType::PdfTexVersion => VerboseLevel::Misc, + LineType::Context(_) => VerboseLevel::Misc, + LineType::BoxFullness => VerboseLevel::Box, + LineType::LaTexVersion => VerboseLevel::Misc, + LineType::WriteMode => VerboseLevel::Misc, + LineType::DocumentClass => VerboseLevel::Misc, + LineType::EnteringDocument(_) => VerboseLevel::Info, + LineType::Library(_) => VerboseLevel::Context, + LineType::PackageWarning(_) => VerboseLevel::Box, + LineType::PackageContextWarning(_) => VerboseLevel::Warning, + LineType::Loading(_) => VerboseLevel::Context, + LineType::Unknown(_) => VerboseLevel::Uncatchable, + LineType::PageGen => VerboseLevel::None, + LineType::Output => VerboseLevel::Misc, + LineType::Log => VerboseLevel::Misc, + LineType::Empty => VerboseLevel::None, + LineType::GeometryResolution => VerboseLevel::Misc, + LineType::GeometryFound => VerboseLevel::Misc, + LineType::Transcript => VerboseLevel::Misc, + } + } +} + /// Tries to split the token and return two string fn split_by(delim: &str, text: &str) -> Option<(String, String)> { let mut slice = text.split(delim); @@ -269,25 +362,68 @@ fn split_by(delim: &str, text: &str) -> Option<(String, String)> { } /// Check each line and changes the status and/or display according warnings -fn checkline(s: &str, status: &mut Status, context: &mut [String]) -> Result<()> { +fn checkline( + args: &MakeConfig, + orginal_line: &str, + status: &mut Status, + context: &mut [String], +) -> Result<()> { // Match the line to guess what is happening - let mut lex = LineType::lexer(s); - let line = match lex.next() { + let mut lex = LineType::lexer(orginal_line); + let parsed_line = match lex.next() { Some(Ok(m)) => m, None => LineType::Empty, - _ => LineType::Unknown(s.to_string()), + _ => LineType::Unknown(orginal_line.to_string()), }; - match line { - LineType::Unknown(w) => println!("[UKWN] {}", w), - LineType::Empty => (), - LineType::EnteringDocument(_) => println!("{:?}", line), - // LineType::TabWarn(_) => println!("{:?}", line), - // LineType::Library(_) => println!("{:?}", line), - // LineType::PackageWarning(_) => println!("{:?},{}", line, s), - LineType::PackageContextWarning(_) => println!("{:?}", line), - LineType::MissingReference => println!("{:?} {}", line, s), - // LineType::=> (), - _ => (), + + // Early exit if the verbosity level doenst match + if parsed_line.vlvl() < args.verbosity { + return Ok(()); + } + + // Post processing for some lines that can't be properly handlend by the lexer + let final_line = match parsed_line { + // LineType::Mode => todo!(), + // LineType::Info(_) => todo!(), + // LineType::StartingSequence => todo!(), + // LineType::EndSequence => todo!(), + // LineType::MPSConverter => todo!(), + // LineType::Chapter(_) => todo!(), + LineType::MissingReferencePrs => &LineType::MissingReference(orginal_line.to_string()), + // LineType::Warning(_) => todo!(), + // LineType::PdfTexVersion => todo!(), + // LineType::Context(_) => todo!(), + // TODO: Add + // LineType::BoxFullness => todo!(), + // LineType::LaTexVersion => todo!(), + // LineType::WriteMode => todo!(), + // LineType::DocumentClass => todo!(), + // LineType::EnteringDocument(_) => todo!(), + // LineType::Library(_) => todo!(), + // LineType::PackageWarning(_) => todo!(), + // LineType::PackageContextWarning(_) => todo!(), + // LineType::Loading(_) => todo!(), + // LineType::Unknown(_) => todo!(), + // LineType::PageGen => todo!(), + // LineType::Output => todo!(), + // LineType::Log => todo!(), + // LineType::Empty => todo!(), + // LineType::GeometryResolution => todo!(), + // LineType::GeometryFound => todo!(), + // LineType::Transcript => todo!(), + _ => &parsed_line, + }; + + match &parsed_line.vlvl() { + VerboseLevel::None => (), + VerboseLevel::Misc => println!("[MISC] {:?}", final_line), + VerboseLevel::Box => println!("[BOXX] {:?}", final_line), + VerboseLevel::Context => println!("[CNTX] {:?}", final_line), // Context tends to be shit ? + VerboseLevel::Info => println!("[INFO] {:?}", final_line), + VerboseLevel::Warning => println!("[WARN] {:?}", final_line), + VerboseLevel::Error => println!("[CRIT] {:?}", final_line), + VerboseLevel::Critical => println!("[CRIT] {:?}", final_line), + VerboseLevel::Uncatchable => println!("[UKNW] {}", orginal_line), } Ok(()) } -- 2.45.1