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

chore(lib): add debug feature #382

Merged
merged 2 commits into from
Feb 20, 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
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
Loading