From df45b9f034add39e0d68a3ee1fcee47870528925 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Fri, 9 Dec 2022 22:35:00 +0000 Subject: [PATCH] chore: fmt --- src/parser/ast/mod.rs | 6 +- src/parser/ast/operators.rs | 8 +- src/parser/expressions.rs | 338 +++++++++++++++++++++++++----------- 3 files changed, 246 insertions(+), 106 deletions(-) diff --git a/src/parser/ast/mod.rs b/src/parser/ast/mod.rs index 3b32b99..966381a 100644 --- a/src/parser/ast/mod.rs +++ b/src/parser/ast/mod.rs @@ -7,11 +7,11 @@ pub mod functions; pub mod identifiers; pub mod interfaces; pub mod modifiers; +pub mod operators; pub mod properties; pub mod traits; pub mod try_block; pub mod variables; -pub mod operators; use std::fmt::Display; @@ -588,9 +588,7 @@ pub struct ListItem { } #[derive(Debug, Eq, PartialEq, Clone)] -pub enum InfixOp { - -} +pub enum InfixOp {} impl From for InfixOp { fn from(k: TokenKind) -> Self { diff --git a/src/parser/ast/operators.rs b/src/parser/ast/operators.rs index 932be80..affaa70 100644 --- a/src/parser/ast/operators.rs +++ b/src/parser/ast/operators.rs @@ -131,7 +131,7 @@ pub enum AssignmentOperation { left: Box, span: Span, right: Box, - } + }, } #[derive(Debug, Clone, PartialEq)] @@ -164,7 +164,7 @@ pub enum BitwiseOperation { Not { span: Span, right: Box, - } + }, } #[derive(Debug, Clone, PartialEq)] @@ -218,7 +218,7 @@ pub enum ComparisonOperation { left: Box, span: Span, right: Box, - } + }, } #[derive(Debug, Clone, PartialEq)] @@ -252,4 +252,4 @@ pub enum LogicalOperation { span: Span, right: Box, }, -} \ No newline at end of file +} diff --git a/src/parser/expressions.rs b/src/parser/expressions.rs index fa8e009..39b5262 100644 --- a/src/parser/expressions.rs +++ b/src/parser/expressions.rs @@ -110,116 +110,252 @@ fn for_precedence(state: &mut State, precedence: Precedence) -> ParseResult { // FIXME: Hacky, should probably be refactored. - left = match kind { - TokenKind::Equals if state.current.kind == TokenKind::Ampersand => { - let amper_span = state.current.span; - state.next(); + left = + match kind { + TokenKind::Equals if state.current.kind == TokenKind::Ampersand => { + let amper_span = state.current.span; + state.next(); - // FIXME: You should only be allowed to assign a referencable variable, - // here, not any old expression. - let right = Box::new(for_precedence(state, rpred)?); + // FIXME: You should only be allowed to assign a referencable variable, + // here, not any old expression. + let right = Box::new(for_precedence(state, rpred)?); - Expression::AssignmentOperation(AssignmentOperation::Assign { left: Box::new(left), span, right: Box::new(Expression::Reference { span: amper_span, right }) }) - } - TokenKind::Instanceof if state.current.kind == TokenKind::Self_ => { - if !state.has_class_scope { - return Err(ParseError::CannotFindTypeInCurrentScope( - state.current.kind.to_string(), - state.current.span, - )); + Expression::AssignmentOperation(AssignmentOperation::Assign { + left: Box::new(left), + span, + right: Box::new(Expression::Reference { + span: amper_span, + right, + }), + }) } + TokenKind::Instanceof if state.current.kind == TokenKind::Self_ => { + if !state.has_class_scope { + return Err(ParseError::CannotFindTypeInCurrentScope( + state.current.kind.to_string(), + state.current.span, + )); + } - state.next(); + state.next(); - Expression::Instanceof { - left: Box::new(left), - span, - right: Box::new(Expression::Self_), - } - } - TokenKind::Instanceof if state.current.kind == TokenKind::Parent => { - if !state.has_class_scope { - return Err(ParseError::CannotFindTypeInCurrentScope( - state.current.kind.to_string(), - state.current.span, - )); + Expression::Instanceof { + left: Box::new(left), + span, + right: Box::new(Expression::Self_), + } } + TokenKind::Instanceof if state.current.kind == TokenKind::Parent => { + if !state.has_class_scope { + return Err(ParseError::CannotFindTypeInCurrentScope( + state.current.kind.to_string(), + state.current.span, + )); + } - state.next(); + state.next(); - Expression::Instanceof { - left: Box::new(left), - span, - right: Box::new(Expression::Self_), - } - } - TokenKind::Instanceof if state.current.kind == TokenKind::Static => { - if !state.has_class_scope { - return Err(ParseError::CannotFindTypeInCurrentScope( - state.current.kind.to_string(), - state.current.span, - )); + Expression::Instanceof { + left: Box::new(left), + span, + right: Box::new(Expression::Self_), + } } + TokenKind::Instanceof if state.current.kind == TokenKind::Static => { + if !state.has_class_scope { + return Err(ParseError::CannotFindTypeInCurrentScope( + state.current.kind.to_string(), + state.current.span, + )); + } - state.next(); + state.next(); - Expression::Instanceof { - left: Box::new(left), - span, - right: Box::new(Expression::Self_), + Expression::Instanceof { + left: Box::new(left), + span, + right: Box::new(Expression::Self_), + } } - } - _ => { - let left = Box::new(left); - let right = Box::new(for_precedence(state, rpred)?); + _ => { + let left = Box::new(left); + let right = Box::new(for_precedence(state, rpred)?); - match kind { - TokenKind::Plus => Expression::ArithmeticOperation(ArithmeticOperation::Addition { left, span, right }), - TokenKind::Minus => Expression::ArithmeticOperation(ArithmeticOperation::Subtraction { left, span, right }), - TokenKind::Asterisk => Expression::ArithmeticOperation(ArithmeticOperation::Multiplication { left, span, right }), - TokenKind::Slash => Expression::ArithmeticOperation(ArithmeticOperation::Division { left, span, right }), - TokenKind::Percent => Expression::ArithmeticOperation(ArithmeticOperation::Modulo { left, span, right }), - TokenKind::Pow => Expression::ArithmeticOperation(ArithmeticOperation::Exponentiation { left, span, right }), - TokenKind::Equals => Expression::AssignmentOperation(AssignmentOperation::Assign { left, span, right }), - TokenKind::PlusEquals => Expression::AssignmentOperation(AssignmentOperation::Addition { left, span, right }), - TokenKind::MinusEquals => Expression::AssignmentOperation(AssignmentOperation::Subtraction { left, span, right }), - TokenKind::AsteriskEqual => Expression::AssignmentOperation(AssignmentOperation::Multiplication { left, span, right }), - TokenKind::SlashEquals => Expression::AssignmentOperation(AssignmentOperation::Division { left, span, right }), - TokenKind::PercentEquals => Expression::AssignmentOperation(AssignmentOperation::Modulo { left, span, right }), - TokenKind::PowEquals => Expression::AssignmentOperation(AssignmentOperation::Exponentiation { left, span, right }), - TokenKind::AmpersandEquals => Expression::AssignmentOperation(AssignmentOperation::BitwiseAnd { left, span, right }), - TokenKind::PipeEquals => Expression::AssignmentOperation(AssignmentOperation::BitwiseOr { left, span, right }), - TokenKind::CaretEquals => Expression::AssignmentOperation(AssignmentOperation::BitwiseXor { left, span, right }), - TokenKind::LeftShiftEquals => Expression::AssignmentOperation(AssignmentOperation::LeftShift { left, span, right }), - TokenKind::RightShiftEquals => Expression::AssignmentOperation(AssignmentOperation::RightShift { left, span, right }), - TokenKind::CoalesceEqual => Expression::AssignmentOperation(AssignmentOperation::Coalesce { left, span, right }), - TokenKind::DotEquals => Expression::AssignmentOperation(AssignmentOperation::Concat { left, span, right }), - TokenKind::Ampersand => Expression::BitwiseOperation(BitwiseOperation::And { left, span, right }), - TokenKind::Pipe => Expression::BitwiseOperation(BitwiseOperation::Or { left, span, right }), - TokenKind::Caret => Expression::BitwiseOperation(BitwiseOperation::Xor { left, span, right }), - TokenKind::LeftShift => Expression::BitwiseOperation(BitwiseOperation::LeftShift { left, span, right }), - TokenKind::RightShift => Expression::BitwiseOperation(BitwiseOperation::RightShift { left, span, right }), - TokenKind::DoubleEquals => Expression::ComparisonOperation(ComparisonOperation::Equal { left, span, right }), - TokenKind::TripleEquals => Expression::ComparisonOperation(ComparisonOperation::Identical { left, span, right }), - TokenKind::BangEquals => Expression::ComparisonOperation(ComparisonOperation::NotEqual { left, span, right }), - TokenKind::AngledLeftRight => Expression::ComparisonOperation(ComparisonOperation::AngledNotEqual { left, span, right }), - TokenKind::BangDoubleEquals => Expression::ComparisonOperation(ComparisonOperation::NotIdentical { left, span, right }), - TokenKind::LessThan => Expression::ComparisonOperation(ComparisonOperation::LessThan { left, span, right }), - TokenKind::GreaterThan => Expression::ComparisonOperation(ComparisonOperation::GreaterThan { left, span, right }), - TokenKind::LessThanEquals => Expression::ComparisonOperation(ComparisonOperation::LessThanOrEqual { left, span, right }), - TokenKind::GreaterThanEquals => Expression::ComparisonOperation(ComparisonOperation::GreaterThanOrEqual { left, span, right }), - TokenKind::Spaceship => Expression::ComparisonOperation(ComparisonOperation::Spaceship { left, span, right }), - TokenKind::BooleanAnd => Expression::LogicalOperation(LogicalOperation::And { left, span, right }), - TokenKind::BooleanOr => Expression::LogicalOperation(LogicalOperation::Or { left, span, right }), - TokenKind::LogicalAnd => Expression::LogicalOperation(LogicalOperation::LogicalAnd { left, span, right }), - TokenKind::LogicalOr => Expression::LogicalOperation(LogicalOperation::LogicalOr { left, span, right }), - TokenKind::LogicalXor => Expression::LogicalOperation(LogicalOperation::LogicalXor { left, span, right }), - TokenKind::Dot => Expression::Concat { left, span, right }, - TokenKind::Instanceof => Expression::Instanceof { left, span, right }, - _ => todo!(), + match kind { + TokenKind::Plus => Expression::ArithmeticOperation( + ArithmeticOperation::Addition { left, span, right }, + ), + TokenKind::Minus => Expression::ArithmeticOperation( + ArithmeticOperation::Subtraction { left, span, right }, + ), + TokenKind::Asterisk => Expression::ArithmeticOperation( + ArithmeticOperation::Multiplication { left, span, right }, + ), + TokenKind::Slash => Expression::ArithmeticOperation( + ArithmeticOperation::Division { left, span, right }, + ), + TokenKind::Percent => Expression::ArithmeticOperation( + ArithmeticOperation::Modulo { left, span, right }, + ), + TokenKind::Pow => Expression::ArithmeticOperation( + ArithmeticOperation::Exponentiation { left, span, right }, + ), + TokenKind::Equals => Expression::AssignmentOperation( + AssignmentOperation::Assign { left, span, right }, + ), + TokenKind::PlusEquals => Expression::AssignmentOperation( + AssignmentOperation::Addition { left, span, right }, + ), + TokenKind::MinusEquals => Expression::AssignmentOperation( + AssignmentOperation::Subtraction { left, span, right }, + ), + TokenKind::AsteriskEqual => Expression::AssignmentOperation( + AssignmentOperation::Multiplication { left, span, right }, + ), + TokenKind::SlashEquals => Expression::AssignmentOperation( + AssignmentOperation::Division { left, span, right }, + ), + TokenKind::PercentEquals => Expression::AssignmentOperation( + AssignmentOperation::Modulo { left, span, right }, + ), + TokenKind::PowEquals => Expression::AssignmentOperation( + AssignmentOperation::Exponentiation { left, span, right }, + ), + TokenKind::AmpersandEquals => Expression::AssignmentOperation( + AssignmentOperation::BitwiseAnd { left, span, right }, + ), + TokenKind::PipeEquals => Expression::AssignmentOperation( + AssignmentOperation::BitwiseOr { left, span, right }, + ), + TokenKind::CaretEquals => Expression::AssignmentOperation( + AssignmentOperation::BitwiseXor { left, span, right }, + ), + TokenKind::LeftShiftEquals => Expression::AssignmentOperation( + AssignmentOperation::LeftShift { left, span, right }, + ), + TokenKind::RightShiftEquals => Expression::AssignmentOperation( + AssignmentOperation::RightShift { left, span, right }, + ), + TokenKind::CoalesceEqual => Expression::AssignmentOperation( + AssignmentOperation::Coalesce { left, span, right }, + ), + TokenKind::DotEquals => Expression::AssignmentOperation( + AssignmentOperation::Concat { left, span, right }, + ), + TokenKind::Ampersand => { + Expression::BitwiseOperation(BitwiseOperation::And { + left, + span, + right, + }) + } + TokenKind::Pipe => { + Expression::BitwiseOperation(BitwiseOperation::Or { + left, + span, + right, + }) + } + TokenKind::Caret => { + Expression::BitwiseOperation(BitwiseOperation::Xor { + left, + span, + right, + }) + } + TokenKind::LeftShift => { + Expression::BitwiseOperation(BitwiseOperation::LeftShift { + left, + span, + right, + }) + } + TokenKind::RightShift => { + Expression::BitwiseOperation(BitwiseOperation::RightShift { + left, + span, + right, + }) + } + TokenKind::DoubleEquals => Expression::ComparisonOperation( + ComparisonOperation::Equal { left, span, right }, + ), + TokenKind::TripleEquals => Expression::ComparisonOperation( + ComparisonOperation::Identical { left, span, right }, + ), + TokenKind::BangEquals => Expression::ComparisonOperation( + ComparisonOperation::NotEqual { left, span, right }, + ), + TokenKind::AngledLeftRight => Expression::ComparisonOperation( + ComparisonOperation::AngledNotEqual { left, span, right }, + ), + TokenKind::BangDoubleEquals => Expression::ComparisonOperation( + ComparisonOperation::NotIdentical { left, span, right }, + ), + TokenKind::LessThan => Expression::ComparisonOperation( + ComparisonOperation::LessThan { left, span, right }, + ), + TokenKind::GreaterThan => Expression::ComparisonOperation( + ComparisonOperation::GreaterThan { left, span, right }, + ), + TokenKind::LessThanEquals => Expression::ComparisonOperation( + ComparisonOperation::LessThanOrEqual { left, span, right }, + ), + TokenKind::GreaterThanEquals => { + Expression::ComparisonOperation( + ComparisonOperation::GreaterThanOrEqual { + left, + span, + right, + }, + ) + } + TokenKind::Spaceship => Expression::ComparisonOperation( + ComparisonOperation::Spaceship { left, span, right }, + ), + TokenKind::BooleanAnd => { + Expression::LogicalOperation(LogicalOperation::And { + left, + span, + right, + }) + } + TokenKind::BooleanOr => { + Expression::LogicalOperation(LogicalOperation::Or { + left, + span, + right, + }) + } + TokenKind::LogicalAnd => { + Expression::LogicalOperation(LogicalOperation::LogicalAnd { + left, + span, + right, + }) + } + TokenKind::LogicalOr => { + Expression::LogicalOperation(LogicalOperation::LogicalOr { + left, + span, + right, + }) + } + TokenKind::LogicalXor => { + Expression::LogicalOperation(LogicalOperation::LogicalXor { + left, + span, + right, + }) + } + TokenKind::Dot => Expression::Concat { left, span, right }, + TokenKind::Instanceof => { + Expression::Instanceof { left, span, right } + } + _ => todo!(), + } } - }, - }; + }; } } @@ -982,13 +1118,19 @@ fn postfix(state: &mut State, lhs: Expression, op: &TokenKind) -> Result { let span = state.current.span; state.next(); - Expression::ArithmeticOperation(ArithmeticOperation::PostDecrement { left: Box::new(lhs), span }) + Expression::ArithmeticOperation(ArithmeticOperation::PostDecrement { + left: Box::new(lhs), + span, + }) } _ => todo!("postfix: {:?}", op), })