mirror of
https://github.com/astral-sh/ruff.git
synced 2025-07-15 17:15:10 +00:00
Remove parser dependency from ruff-python-ast (#6096)
This commit is contained in:
parent
99127243f4
commit
2cf00fee96
658 changed files with 1714 additions and 1546 deletions
19
crates/ruff_python_codegen/src/lib.rs
Normal file
19
crates/ruff_python_codegen/src/lib.rs
Normal file
|
@ -0,0 +1,19 @@
|
|||
mod generator;
|
||||
mod stylist;
|
||||
|
||||
pub use generator::Generator;
|
||||
use ruff_source_file::Locator;
|
||||
use rustpython_ast::Suite;
|
||||
use rustpython_parser::{lexer, Mode, Parse, ParseError};
|
||||
pub use stylist::{Quote, Stylist};
|
||||
|
||||
/// Run round-trip source code generation on a given Python code.
|
||||
pub fn round_trip(code: &str, source_path: &str) -> Result<String, ParseError> {
|
||||
let locator = Locator::new(code);
|
||||
let python_ast = Suite::parse(code, source_path)?;
|
||||
let tokens: Vec<_> = lexer::lex(code, Mode::Module).collect();
|
||||
let stylist = Stylist::from_tokens(&tokens, &locator);
|
||||
let mut generator: Generator = (&stylist).into();
|
||||
generator.unparse_suite(&python_ast);
|
||||
Ok(generator.generate())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue