mirror of
https://github.com/astral-sh/ruff
synced 2026-01-21 05:20:49 -05:00
This lets you test the ruff linters or use the ruff library without having to compile the ~100 additional dependencies that are needed by the CLI. Because we set the following in the [workspace] section of Cargo.toml: default-members = [".", "ruff_cli"] `cargo run` still runs the CLI and `cargo test` still tests the code in src/ as well as the code in the new ruff_cli crate. (But you can now also run `cargo test -p ruff` to only test the linters.)
30 lines
858 B
Rust
30 lines
858 B
Rust
use rustpython_ast::{Mod, Suite};
|
|
use rustpython_parser::error::ParseError;
|
|
use rustpython_parser::lexer::LexResult;
|
|
use rustpython_parser::mode::Mode;
|
|
use rustpython_parser::{lexer, parser};
|
|
|
|
/// Collect tokens up to and including the first error.
|
|
pub fn tokenize(contents: &str) -> Vec<LexResult> {
|
|
let mut tokens: Vec<LexResult> = vec![];
|
|
for tok in lexer::make_tokenizer(contents) {
|
|
let is_err = tok.is_err();
|
|
tokens.push(tok);
|
|
if is_err {
|
|
break;
|
|
}
|
|
}
|
|
tokens
|
|
}
|
|
|
|
/// Parse a full Python program from its tokens.
|
|
pub(crate) fn parse_program_tokens(
|
|
lxr: Vec<LexResult>,
|
|
source_path: &str,
|
|
) -> anyhow::Result<Suite, ParseError> {
|
|
parser::parse_tokens(lxr, Mode::Module, source_path).map(|top| match top {
|
|
Mod::Module { body, .. } => body,
|
|
_ => unreachable!(),
|
|
})
|
|
}
|