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

Remove less used parser dependencies #11718

Merged
merged 1 commit into from
Jun 3, 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: 0 additions & 2 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions crates/ruff_python_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,8 @@ ruff_python_ast = { workspace = true }
ruff_python_trivia = { workspace = true }
ruff_text_size = { workspace = true }

anyhow = { workspace = true }
bitflags = { workspace = true }
bstr = { workspace = true }
is-macro = { workspace = true }
itertools = { workspace = true }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes me happy :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew it would :)

memchr = { workspace = true }
rustc-hash = { workspace = true }
static_assertions = { workspace = true }
Expand All @@ -33,6 +30,7 @@ unicode-normalization = { workspace = true }
ruff_source_file = { workspace = true }

annotate-snippets = { workspace = true }
anyhow = { workspace = true }
insta = { workspace = true, features = ["glob"] }
walkdir = { workspace = true }

Expand Down
9 changes: 5 additions & 4 deletions crates/ruff_python_parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@
//!
//! [Lexical analysis]: https://docs.python.org/3/reference/lexical_analysis.html

use std::{char, cmp::Ordering, str::FromStr};
use std::cmp::Ordering;
use std::str::FromStr;

use bitflags::bitflags;
use unicode_ident::{is_xid_continue, is_xid_start};
use unicode_normalization::UnicodeNormalization;

use ruff_python_ast::str::Quote;
use ruff_python_ast::str_prefix::{
AnyStringPrefix, ByteStringPrefix, FStringPrefix, StringLiteralPrefix,
};
use unicode_ident::{is_xid_continue, is_xid_start};
use unicode_normalization::UnicodeNormalization;

use ruff_python_ast::{AnyStringFlags, Int, IpyEscapeKind, StringFlags};
use ruff_text_size::{Ranged, TextLen, TextRange, TextSize};

Expand Down
6 changes: 2 additions & 4 deletions crates/ruff_python_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ pub use crate::token::TokenKind;

use crate::parser::Parser;

use itertools::Itertools;
use ruff_python_ast::{Expr, Mod, ModExpression, ModModule, PySourceType, Suite};
use ruff_python_trivia::CommentRanges;
use ruff_text_size::{Ranged, TextRange, TextSize};
Expand Down Expand Up @@ -388,9 +387,8 @@ impl Tokens {
let end = *self.first_unknown_or_len.get_or_init(|| {
self.raw
.iter()
.find_position(|token| token.kind() == TokenKind::Unknown)
.map(|(idx, _)| idx)
.unwrap_or_else(|| self.raw.len())
.position(|token| token.kind() == TokenKind::Unknown)
.unwrap_or(self.raw.len())
});
Comment on lines 388 to 392
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is on me as I didn't realize that find_position was coming from itertools 😬

&self.raw[..end]
}
Expand Down
19 changes: 12 additions & 7 deletions crates/ruff_python_parser/src/typing.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
//! This module takes care of parsing a type annotation.

use anyhow::Result;

use ruff_python_ast::relocate::relocate_expr;
use ruff_python_ast::str::raw_contents;
use ruff_python_ast::{Expr, ExprStringLiteral, StringFlags, StringLiteral};
use ruff_text_size::Ranged;

use crate::{parse_expression, parse_expression_range};
use crate::{parse_expression, parse_expression_range, ParseError};

#[derive(is_macro::Is, Copy, Clone, Debug)]
#[derive(Copy, Clone, Debug)]
pub enum AnnotationKind {
/// The annotation is defined as part a simple string literal,
/// e.g. `x: "List[int]" = []`. Annotations within simple literals
Expand All @@ -24,12 +22,19 @@ pub enum AnnotationKind {
Complex,
}

impl AnnotationKind {
/// Returns `true` if the annotation kind is simple.
pub const fn is_simple(self) -> bool {
matches!(self, AnnotationKind::Simple)
}
}

/// Parses the given string expression node as a type annotation. The given `source` is the entire
/// source code.
pub fn parse_type_annotation(
string_expr: &ExprStringLiteral,
source: &str,
) -> Result<(Expr, AnnotationKind)> {
) -> Result<(Expr, AnnotationKind), ParseError> {
let expr_text = &source[string_expr.range()];

if let [string_literal] = string_expr.value.as_slice() {
Expand All @@ -53,7 +58,7 @@ pub fn parse_type_annotation(
fn parse_simple_type_annotation(
string_literal: &StringLiteral,
source: &str,
) -> Result<(Expr, AnnotationKind)> {
) -> Result<(Expr, AnnotationKind), ParseError> {
Ok((
parse_expression_range(
source,
Expand All @@ -69,7 +74,7 @@ fn parse_simple_type_annotation(

fn parse_complex_type_annotation(
string_expr: &ExprStringLiteral,
) -> Result<(Expr, AnnotationKind)> {
) -> Result<(Expr, AnnotationKind), ParseError> {
let mut parsed = parse_expression(string_expr.value.to_str())?.into_expr();
relocate_expr(&mut parsed, string_expr.range());
Ok((parsed, AnnotationKind::Complex))
Expand Down
Loading