Skip to content

Commit

Permalink
Manual impl of Debug on Token (#11958)
Browse files Browse the repository at this point in the history
## Summary

I look at the token stream a lot, not specifically in the playground but
in the terminal output and it's annoying to scroll a lot to find
specific location. Most of the information is also redundant.

The final format we end up with is: `<kind> <range> (flags = ...)` e.g.,
`String 0..4 (flags = BYTE_STRING)` where the flags part is only
populated if there are any flags set.
  • Loading branch information
dhruvmanila committed Jun 22, 2024
1 parent b1e7bf7 commit 8116032
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion crates/ruff_dev/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ ruff_python_formatter = { workspace = true }
ruff_python_parser = { workspace = true }
ruff_python_stdlib = { workspace = true }
ruff_python_trivia = { workspace = true }
ruff_text_size = { workspace = true }
ruff_workspace = { workspace = true, features = ["schemars"] }

anyhow = { workspace = true }
Expand Down
8 changes: 1 addition & 7 deletions crates/ruff_dev/src/print_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ use anyhow::Result;
use ruff_linter::source_kind::SourceKind;
use ruff_python_ast::PySourceType;
use ruff_python_parser::parse_unchecked_source;
use ruff_text_size::Ranged;

#[derive(clap::Args)]
pub(crate) struct Args {
Expand All @@ -27,12 +26,7 @@ pub(crate) fn main(args: &Args) -> Result<()> {
})?;
let parsed = parse_unchecked_source(source_kind.source_code(), source_type);
for token in parsed.tokens() {
println!(
"{start:#?} {kind:#?} {end:#?}",
start = token.start(),
end = token.end(),
kind = token.kind(),
);
println!("{token:#?}");
}
Ok(())
}
22 changes: 21 additions & 1 deletion crates/ruff_python_parser/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use ruff_python_ast::str_prefix::{
use ruff_python_ast::{AnyStringFlags, BoolOp, Int, IpyEscapeKind, Operator, StringFlags, UnaryOp};
use ruff_text_size::{Ranged, TextRange};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Token {
/// The kind of the token.
kind: TokenKind,
Expand Down Expand Up @@ -81,6 +81,26 @@ impl Ranged for Token {
}
}

impl fmt::Debug for Token {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?} {:?}", self.kind, self.range)?;
if !self.flags.is_empty() {
f.write_str(" (flags = ")?;
let mut first = true;
for (name, _) in self.flags.iter_names() {
if first {
first = false;
} else {
f.write_str(" | ")?;
}
f.write_str(name)?;
}
f.write_str(")")?;
}
Ok(())
}
}

/// A kind of a token.
#[derive(Copy, Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
pub enum TokenKind {
Expand Down
2 changes: 1 addition & 1 deletion crates/ruff_wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ impl Workspace {
pub fn tokens(&self, contents: &str) -> Result<String, Error> {
let parsed = parse_unchecked(contents, Mode::Module);

Ok(format!("{:#?}", parsed.tokens()))
Ok(format!("{:#?}", parsed.tokens().as_ref()))
}
}

Expand Down

0 comments on commit 8116032

Please sign in to comment.