Skip to content

Commit

Permalink
chore(lib): add debug feature (#382)
Browse files Browse the repository at this point in the history
* chore(lib): add debug feature

This adds a debug feature, similar to what Clap does, as requested by #379.

The current implementation only debugs a few parts of the code, and it may be interesting to add more debug messages in the different crates.

* chore(lint): run cargo fmt
  • Loading branch information
jeertmans committed Feb 20, 2024
1 parent f8322a3 commit 066e125
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ shared-version = true
bench = false

[features]
# Enables debug messages
debug = ["logos-derive?/debug"]
default = ["export_derive", "std"]
# Re-exports the `Logos` derive macro, so that end user only needs to
# import this crate and `use logos::Logos` to get both the trait and
Expand Down
4 changes: 4 additions & 0 deletions logos-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ assert_cmd = "2.0.4"
assert_fs = "1.0.7"
predicates = "2.1.1"

[features]
# Enables debug messages
debug = ["logos-codegen/debug"]

[package]
name = "logos-cli"
authors.workspace = true
Expand Down
4 changes: 4 additions & 0 deletions logos-codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ syn = { version = "2.0.13", features = ["full"] }
[dev-dependencies]
pretty_assertions = "1.4.0"

[features]
# Enables debug messages
debug = []

[lib]
bench = false

Expand Down
14 changes: 14 additions & 0 deletions logos-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ mod mir;
mod parser;
mod util;

#[macro_use]
#[allow(missing_docs)]
mod macros;

use generator::Generator;
use graph::{DisambiguationError, Fork, Graph, Rope};
use leaf::Leaf;
Expand All @@ -36,6 +40,8 @@ const REGEX_ATTR: &str = "regex";

/// Generate a `Logos` implementation for the given struct, provided as a stream of rust tokens.
pub fn generate(input: TokenStream) -> TokenStream {
debug!("Reading input token streams");

let mut item: ItemEnum = syn::parse2(input).expect("Logos can be only be derived for enums");

let name = &item.ident;
Expand Down Expand Up @@ -72,6 +78,8 @@ pub fn generate(input: TokenStream) -> TokenStream {
}
}

debug!("Iterating through enum variants");

for variant in &mut item.variants {
let field = match &mut variant.fields {
Fields::Unit => MaybeVoid::Void,
Expand Down Expand Up @@ -200,6 +208,8 @@ pub fn generate(input: TokenStream) -> TokenStream {

let mut root = Fork::new();

debug!("Parsing additional options (extras, source, ...)");

let error_type = parser.error_type.take();
let extras = parser.extras.take();
let source = parser
Expand Down Expand Up @@ -252,6 +262,8 @@ pub fn generate(input: TokenStream) -> TokenStream {
}
}

debug!("Checking if any two tokens have the same priority");

for &DisambiguationError(a, b) in graph.errors() {
let a = graph[a].unwrap_leaf();
let b = graph[b].unwrap_leaf();
Expand Down Expand Up @@ -283,6 +295,8 @@ pub fn generate(input: TokenStream) -> TokenStream {

graph.shake(root);

debug!("Generating code from graph: {graph:#?}");

let generator = Generator::new(name, &this, root, &graph);

let body = generator.generate();
Expand Down
12 changes: 12 additions & 0 deletions logos-codegen/src/macros.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#[cfg(feature = "debug")]
macro_rules! debug {
($($arg:tt)*) => {
eprint!("[{}:{}:{}] ", file!(), line!(), column!());
eprintln!($($arg)*)
}
}

#[cfg(not(feature = "debug"))]
macro_rules! debug {
($($arg:tt)*) => {};
}
4 changes: 4 additions & 0 deletions logos-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
[dependencies]
logos-codegen = {version = "0.14.0", path = "../logos-codegen"}

[features]
# Enables debug messages
debug = ["logos-codegen/debug"]

[lib]
bench = false
proc-macro = true
Expand Down

0 comments on commit 066e125

Please sign in to comment.