Skip to content

Add utilities to enum #44

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

Merged
merged 2 commits into from
May 16, 2023
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
5 changes: 3 additions & 2 deletions ast/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fold = []
unparse = ["rustpython-literal"]
visitor = []
all-nodes-with-ranges = []
pyo3 = ["dep:pyo3", "num-complex", "once_cell"]

[dependencies]
rustpython-parser-core = { workspace = true }
Expand All @@ -23,6 +24,6 @@ rustpython-literal = { workspace = true, optional = true }
is-macro = { workspace = true }
num-bigint = { workspace = true }
static_assertions = "1.1.0"
num-complex = { workspace = true }
once_cell = { workspace = true }
num-complex = { workspace = true, optional = true }
once_cell = { workspace = true, optional = true }
pyo3 = { workspace = true, optional = true, features = ["num-bigint", "num-complex"] }
12 changes: 12 additions & 0 deletions core/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,15 @@ pub enum ConversionFlag {
/// Converts by calling `repr(<value>)`.
Repr = b'r' as i8,
}

impl ConversionFlag {
pub fn to_byte(&self) -> Option<u8> {
match self {
Self::None => None,
flag => Some(*flag as u8),
}
}
pub fn to_char(&self) -> Option<char> {
Some(self.to_byte()? as char)
}
}
2 changes: 1 addition & 1 deletion core/src/mode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Control in the different modes by which a source file can be parsed.

/// The mode argument specifies in what way code must be parsed.
#[derive(Clone, Copy)]
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub enum Mode {
/// The code consists of a sequence of statements.
Module,
Expand Down
1 change: 1 addition & 0 deletions literal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "MIT"

[dependencies]
hexf-parse = "0.2.1"
is-macro.workspace = true
lexical-parse-float = { version = "0.8.0", features = ["format"] }
num-traits = { workspace = true }
unic-ucd-category = "0.9"
Expand Down
2 changes: 1 addition & 1 deletion literal/src/escape.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, is_macro::Is)]
pub enum Quote {
Single,
Double,
Expand Down
2 changes: 1 addition & 1 deletion literal/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone, Copy, is_macro::Is, Hash)]
pub enum Case {
Lower,
Upper,
Expand Down
2 changes: 1 addition & 1 deletion parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ rustc-hash = "1.1.0"
serde = { version = "1.0.133", optional = true, default-features = false, features = ["derive"] }

[dev-dependencies]
insta = { workspace = true }
insta = { workspace = true }
18 changes: 10 additions & 8 deletions parser/src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ impl<'a> StringParser<'a> {
'v' => '\x0b',
o @ '0'..='7' => self.parse_octet(o),
'x' => self.parse_unicode_literal(2)?,
'u' if !self.kind.is_bytes() => self.parse_unicode_literal(4)?,
'U' if !self.kind.is_bytes() => self.parse_unicode_literal(8)?,
'N' if !self.kind.is_bytes() => self.parse_unicode_name()?,
'u' if !self.kind.is_any_bytes() => self.parse_unicode_literal(4)?,
'U' if !self.kind.is_any_bytes() => self.parse_unicode_literal(8)?,
'N' if !self.kind.is_any_bytes() => self.parse_unicode_name()?,
// Special cases where the escape sequence is not a single character
'\n' => return Ok("".to_string()),
c => {
if self.kind.is_bytes() && !c.is_ascii() {
if self.kind.is_any_bytes() && !c.is_ascii() {
return Err(LexicalError {
error: LexicalErrorType::OtherError(
"bytes can only contain ASCII literal characters".to_owned(),
Expand Down Expand Up @@ -578,9 +578,9 @@ impl<'a> StringParser<'a> {
}

fn parse(&mut self) -> Result<Vec<Expr>, LexicalError> {
if self.kind.is_fstring() {
if self.kind.is_any_fstring() {
self.parse_fstring(0)
} else if self.kind.is_bytes() {
} else if self.kind.is_any_bytes() {
self.parse_bytes().map(|expr| vec![expr])
} else {
self.parse_string().map(|expr| vec![expr])
Expand Down Expand Up @@ -611,10 +611,12 @@ pub(crate) fn parse_strings(
let initial_start = values[0].0;
let last_end = values.last().unwrap().2;
let initial_kind = (values[0].1 .1 == StringKind::Unicode).then(|| "u".to_owned());
let has_fstring = values.iter().any(|(_, (_, kind, ..), _)| kind.is_fstring());
let has_fstring = values
.iter()
.any(|(_, (_, kind, ..), _)| kind.is_any_fstring());
let num_bytes = values
.iter()
.filter(|(_, (_, kind, ..), _)| kind.is_bytes())
.filter(|(_, (_, kind, ..), _)| kind.is_any_bytes())
.count();
let has_bytes = num_bytes > 0;

Expand Down
6 changes: 3 additions & 3 deletions parser/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl fmt::Display for Tok {
/// section of the Python reference.
///
/// [String and Bytes literals]: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
#[derive(PartialEq, Eq, Debug, Clone)]
#[derive(PartialEq, Eq, Debug, Clone, Hash, Copy)] // TODO: is_macro::Is
pub enum StringKind {
/// A normal string literal with no prefix.
String,
Expand Down Expand Up @@ -398,14 +398,14 @@ impl StringKind {

/// Returns true if the string is an f-string, i,e one of
/// [`StringKind::FString`] or [`StringKind::RawFString`].
pub fn is_fstring(&self) -> bool {
pub fn is_any_fstring(&self) -> bool {
use StringKind::{FString, RawFString};
matches!(self, FString | RawFString)
}

/// Returns true if the string is a byte string, i,e one of
/// [`StringKind::Bytes`] or [`StringKind::RawBytes`].
pub fn is_bytes(&self) -> bool {
pub fn is_any_bytes(&self) -> bool {
use StringKind::{Bytes, RawBytes};
matches!(self, Bytes | RawBytes)
}
Expand Down