Skip to content

Commit

Permalink
Simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
kazcw committed Nov 27, 2024
1 parent 736134e commit 932d2f4
Showing 1 changed file with 6 additions and 30 deletions.
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`.
}

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

0 comments on commit 932d2f4

Please sign in to comment.