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

Further simplification enabled by chained conversions #11682

Merged
merged 1 commit into from
Dec 3, 2024
Merged
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
36 changes: 6 additions & 30 deletions lib/rust/parser/src/syntax/token/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ impl OperatorProperties {

/// Return a copy of this operator, with unary prefix parsing allowed.
pub fn with_unary_prefix_mode(self, precedence: Precedence) -> Self {
debug_assert!(precedence > Precedence::min());
Self { unary_prefix_precedence: Some(precedence), ..self }
}

Expand Down Expand Up @@ -203,7 +202,7 @@ impl HasOperatorProperties for variant::AnnotationOperator {
impl HasOperatorProperties for variant::AutoscopeOperator {
fn operator_properties(&self) -> OperatorProperties {
OperatorProperties {
unary_prefix_precedence: Some(Precedence::min_valid()),
unary_prefix_precedence: Some(Precedence::Assignment),
is_compile_time: true,
rhs_is_non_expression: true,
..default()
Expand All @@ -224,7 +223,7 @@ impl HasOperatorProperties for variant::NegationOperator {
impl HasOperatorProperties for variant::LambdaOperator {
fn operator_properties(&self) -> OperatorProperties {
OperatorProperties {
unary_prefix_precedence: Some(Precedence::min_valid()),
unary_prefix_precedence: Some(Precedence::Assignment),
is_compile_time: true,
..default()
}
Expand All @@ -240,7 +239,7 @@ impl HasOperatorProperties for variant::DotOperator {
impl HasOperatorProperties for variant::SuspensionOperator {
fn operator_properties(&self) -> OperatorProperties {
OperatorProperties {
unary_prefix_precedence: Some(Precedence::max()),
unary_prefix_precedence: Some(Precedence::Annotation),
is_compile_time: true,
rhs_is_non_expression: true,
..default()
Expand All @@ -265,9 +264,7 @@ impl HasOperatorProperties for variant::CommaOperator {
#[repr(u8)]
#[allow(missing_docs)]
pub enum Precedence {
/// A value that is lower than the precedence of any operator.
Min = 0,
Assignment,
Assignment = 1,
TypeAnnotation,
Arrow,
Not,
Expand All @@ -284,34 +281,13 @@ pub enum Precedence {
Negation,
Application,
Annotation,
/// A value that is higher than the precedence of any operator.
Max,
// NOTE: The highest value must not exceed 0x7e--see usage of `into_u8`.
Copy link
Contributor

Choose a reason for hiding this comment

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

I see only one usage of into_u8: in ModifiedPrecedence::new; there's no docs, and I don't know why 0x7f is not allowed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's unlikely we would ever want to use high numeric values for these, but out of an abundance of caution I put a note here to make sure that anyone considering doing so knows what that could affect. If they are really determined to make that sort of change, the reference to into_u8 points them in the right direction to find out why they can't use 0x7f.

}

impl Precedence {
/// Return a precedence that is lower than the precedence of any operator.
pub fn min() -> Self {
Precedence::Min
}

/// Return the lowest precedence for any operator.
pub fn min_valid() -> Self {
debug_assert_eq!(Precedence::Assignment as u8, Precedence::Min as u8 + 1);
Precedence::Assignment
}

/// Return a precedence that is not lower than any other precedence.
pub fn max() -> Self {
Precedence::Max
}

/// Return the value as a number.
pub fn into_u8(self) -> u8 {
let value = self as u8;
debug_assert!(value > Precedence::Min as u8);
debug_assert!(value & 0x80 == 0);
debug_assert!((value + 1) & 0x80 == 0);
value
self as u8
}
}

Expand Down
Loading