@@ -431,6 +431,11 @@ pub enum ArithmeticPart {
431431 operator : UnaryArithmeticOp ,
432432 operand : Box < ArithmeticPart > ,
433433 } ,
434+ #[ error( "Invalid pre arithmetic expression" ) ]
435+ PreArithmeticExpr {
436+ operator : PreArithmeticOp ,
437+ operand : Box < ArithmeticPart > ,
438+ } ,
434439 #[ error( "Invalid post arithmetic expression" ) ]
435440 PostArithmeticExpr {
436441 operand : Box < ArithmeticPart > ,
@@ -490,7 +495,15 @@ pub enum UnaryArithmeticOp {
490495
491496#[ cfg_attr( feature = "serialization" , derive( serde:: Serialize ) ) ]
492497#[ cfg_attr( feature = "serialization" , serde( rename_all = "camelCase" ) ) ]
493- #[ derive( Debug , Clone , PartialEq , Eq ) ]
498+ #[ derive( Debug , Clone , PartialEq , Eq , Copy ) ]
499+ pub enum PreArithmeticOp {
500+ Increment , // ++
501+ Decrement , // --
502+ }
503+
504+ #[ cfg_attr( feature = "serialization" , derive( serde:: Serialize ) ) ]
505+ #[ cfg_attr( feature = "serialization" , serde( rename_all = "camelCase" ) ) ]
506+ #[ derive( Debug , Clone , PartialEq , Eq , Copy ) ]
494507pub enum PostArithmeticOp {
495508 Increment , // ++
496509 Decrement , // --
@@ -1419,7 +1432,9 @@ fn parse_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14191432
14201433fn parse_unary_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
14211434 let mut inner = pair. into_inner ( ) ;
1422- let first = inner. next ( ) . ok_or_else ( || miette ! ( "Expected unary operator" ) ) ?;
1435+ let first = inner
1436+ . next ( )
1437+ . ok_or_else ( || miette ! ( "Expected unary operator" ) ) ?;
14231438
14241439 match first. as_rule ( ) {
14251440 Rule :: unary_pre_arithmetic_expr => unary_pre_arithmetic_expr ( first) ,
@@ -1433,7 +1448,9 @@ fn parse_unary_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14331448
14341449fn unary_pre_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
14351450 let mut inner = pair. into_inner ( ) ;
1436- let first = inner. next ( ) . ok_or_else ( || miette ! ( "Expected unary pre operator" ) ) ?;
1451+ let first = inner
1452+ . next ( )
1453+ . ok_or_else ( || miette ! ( "Expected unary pre operator" ) ) ?;
14371454 let second = inner. next ( ) . ok_or_else ( || miette ! ( "Expected operand" ) ) ?;
14381455 let operand = match second. as_rule ( ) {
14391456 Rule :: parentheses_expr => {
@@ -1458,14 +1475,14 @@ fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14581475 operator : ( op) ,
14591476 operand : ( Box :: new ( operand) ) ,
14601477 } )
1461- } ,
1478+ }
14621479 Rule :: post_arithmetic_op => {
1463- let op = parse_post_arithmetic_op ( first) ?;
1464- Ok ( ArithmeticPart :: PostArithmeticExpr {
1480+ let op = parse_pre_arithmetic_op ( first) ?;
1481+ Ok ( ArithmeticPart :: PreArithmeticExpr {
14651482 operator : ( op) ,
14661483 operand : ( Box :: new ( operand) ) ,
14671484 } )
1468- } ,
1485+ }
14691486 _ => Err ( miette ! (
14701487 "Unexpected rule in unary arithmetic operator: {:?}" ,
14711488 first. as_rule( )
@@ -1475,7 +1492,9 @@ fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14751492
14761493fn unary_post_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
14771494 let mut inner = pair. into_inner ( ) ;
1478- let first = inner. next ( ) . ok_or_else ( || miette ! ( "Expected unary post operator" ) ) ?;
1495+ let first = inner
1496+ . next ( )
1497+ . ok_or_else ( || miette ! ( "Expected unary post operator" ) ) ?;
14791498 let second = inner. next ( ) . ok_or_else ( || miette ! ( "Expected operand" ) ) ?;
14801499
14811500 let operand = match first. as_rule ( ) {
@@ -1501,7 +1520,10 @@ fn unary_post_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
15011520}
15021521
15031522fn parse_unary_arithmetic_op ( pair : Pair < Rule > ) -> Result < UnaryArithmeticOp > {
1504- let first = pair. into_inner ( ) . next ( ) . ok_or_else ( || miette ! ( "Expected unary operator" ) ) ?;
1523+ let first = pair
1524+ . into_inner ( )
1525+ . next ( )
1526+ . ok_or_else ( || miette ! ( "Expected unary operator" ) ) ?;
15051527
15061528 match first. as_rule ( ) {
15071529 Rule :: add => Ok ( UnaryArithmeticOp :: Plus ) ,
@@ -1515,8 +1537,26 @@ fn parse_unary_arithmetic_op(pair: Pair<Rule>) -> Result<UnaryArithmeticOp> {
15151537 }
15161538}
15171539
1540+ fn parse_pre_arithmetic_op ( pair : Pair < Rule > ) -> Result < PreArithmeticOp > {
1541+ let first = pair
1542+ . into_inner ( )
1543+ . next ( )
1544+ . ok_or_else ( || miette ! ( "Expected increament or decreament operator" ) ) ?;
1545+ match first. as_rule ( ) {
1546+ Rule :: increment => Ok ( PreArithmeticOp :: Increment ) ,
1547+ Rule :: decrement => Ok ( PreArithmeticOp :: Decrement ) ,
1548+ _ => Err ( miette ! (
1549+ "Unexpected rule in post arithmetic operator: {:?}" ,
1550+ first. as_rule( )
1551+ ) ) ,
1552+ }
1553+ }
1554+
15181555fn parse_post_arithmetic_op ( pair : Pair < Rule > ) -> Result < PostArithmeticOp > {
1519- let first = pair. into_inner ( ) . next ( ) . ok_or_else ( || miette ! ( "Expected increament or decreament operator" ) ) ?;
1556+ let first = pair
1557+ . into_inner ( )
1558+ . next ( )
1559+ . ok_or_else ( || miette ! ( "Expected increament or decreament operator" ) ) ?;
15201560 match first. as_rule ( ) {
15211561 Rule :: increment => Ok ( PostArithmeticOp :: Increment ) ,
15221562 Rule :: decrement => Ok ( PostArithmeticOp :: Decrement ) ,
0 commit comments