From abe5dbf50505ae310c858c4e0be9d2cc6698efe2 Mon Sep 17 00:00:00 2001 From: Christopher Chang <51393127+chriscerie@users.noreply.github.com> Date: Sat, 1 Jun 2024 12:42:00 -0700 Subject: [PATCH] Move precedence back to unary --- CHANGELOG.md | 1 - full-moon/src/ast/mod.rs | 14 ++++++++------ full-moon/src/ast/parsers.rs | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dc9a745a..a37f4308 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `Punctuated` now implements `Default` for all `T`, rather than if `T: Default`. ### Removed -- Removed `UnOp::precedence`, as unary operators do not traditionally use precedence in the same way binary operators do. - Removed `TokenizerErrorType::UnexpectedShebang`. - Removed `stacker` feature flag, as rewrites to the parser should make it unnecessary. diff --git a/full-moon/src/ast/mod.rs b/full-moon/src/ast/mod.rs index 7386908a..ff23a1e6 100644 --- a/full-moon/src/ast/mod.rs +++ b/full-moon/src/ast/mod.rs @@ -2055,7 +2055,8 @@ macro_rules! make_bin_op { } impl BinOp { - /// The precedence of non-unary operator, from a scale of 1 to 10. The larger the number, the higher the precedence. + /// The precedence of non-unary operator. The larger the number, the higher the precedence. + /// Shares the same precedence table as unary operators. pub fn precedence_of_token(token: &TokenReference) -> Option { match token.token_type() { TokenType::Symbol { symbol } => match symbol { @@ -2074,11 +2075,6 @@ macro_rules! make_bin_op { } } - /// The precedence of unary operators - pub fn precedence_of_unary() -> u8 { - 11 - } - /// The token associated with this operator pub fn token(&self) -> &TokenReference { match self { @@ -2208,6 +2204,12 @@ impl UnOp { UnOp::Tilde(token) => token, } } + + /// The precedence of unary operator. The larger the number, the higher the precedence. + /// Shares the same precedence table as binary operators. + pub fn precedence() -> u8 { + 11 + } } /// An error that occurs when creating the AST. diff --git a/full-moon/src/ast/parsers.rs b/full-moon/src/ast/parsers.rs index e5c2278c..25af61a2 100644 --- a/full-moon/src/ast/parsers.rs +++ b/full-moon/src/ast/parsers.rs @@ -1833,7 +1833,7 @@ fn parse_unary_expression( let expression = match parse_expression_with_precedence( state, primary_expression, - ast::BinOp::precedence_of_unary(), + ast::UnOp::precedence(), ) { ParserResult::Value(expression) => expression, ParserResult::LexerMoved => return ParserResult::LexerMoved,