@@ -1419,37 +1419,22 @@ fn parse_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14191419
14201420fn parse_unary_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
14211421 let mut inner = pair. into_inner ( ) ;
1422- let first = inner. next ( ) . unwrap ( ) ;
1422+ let first = inner. next ( ) . ok_or_else ( || miette ! ( "Expected unary operator" ) ) ? ;
14231423
14241424 match first. as_rule ( ) {
14251425 Rule :: unary_pre_arithmetic_expr => unary_pre_arithmetic_expr ( first) ,
14261426 Rule :: unary_post_arithmetic_expr => unary_post_arithmetic_expr ( first) ,
1427- _ => {
1428- let operand = parse_arithmetic_expr ( first) ?;
1429- let op = parse_post_arithmetic_op ( inner. next ( ) . unwrap ( ) ) ?;
1430- Ok ( ArithmeticPart :: PostArithmeticExpr {
1431- operand : Box :: new ( operand) ,
1432- operator : op,
1433- } )
1434- }
1435- }
1436- }
1437-
1438- fn parse_unary_arithmetic_op_type ( pair : Pair < Rule > ) -> Result < bool > {
1439- match pair. as_rule ( ) {
1440- Rule :: unary_arithmetic_op => Ok ( true ) ,
1441- Rule :: post_arithmetic_op => Ok ( false ) ,
14421427 _ => Err ( miette ! (
1443- "Unexpected rule in unary arithmetic operator : {:?}" ,
1444- pair . as_rule( )
1428+ "Unexpected rule in unary arithmetic expression : {:?}" ,
1429+ first . as_rule( )
14451430 ) ) ,
14461431 }
14471432}
14481433
14491434fn unary_pre_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
14501435 let mut inner = pair. into_inner ( ) ;
1451- let first = inner. next ( ) . unwrap ( ) ;
1452- let second = inner. next ( ) . unwrap ( ) ;
1436+ let first = inner. next ( ) . ok_or_else ( || miette ! ( "Expected unary pre operator" ) ) ? ;
1437+ let second = inner. next ( ) . ok_or_else ( || miette ! ( "Expected operand" ) ) ? ;
14531438 let operand = match second. as_rule ( ) {
14541439 Rule :: parentheses_expr => {
14551440 let inner = second. into_inner ( ) . next ( ) . unwrap ( ) ;
@@ -1466,25 +1451,33 @@ fn unary_pre_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14661451 ) ) ,
14671452 } ?;
14681453
1469- if parse_unary_arithmetic_op_type ( first. clone ( ) ) ? {
1470- let op = parse_unary_arithmetic_op ( first) ?;
1471- Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1472- operator : ( op) ,
1473- operand : ( Box :: new ( operand) ) ,
1474- } )
1475- } else {
1476- let op = parse_post_arithmetic_op ( first) ?;
1477- Ok ( ArithmeticPart :: PostArithmeticExpr {
1478- operator : ( op) ,
1479- operand : ( Box :: new ( operand) ) ,
1480- } )
1454+ match first. as_rule ( ) {
1455+ Rule :: pre_arithmetic_op => {
1456+ let op = parse_unary_arithmetic_op ( first) ?;
1457+ Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1458+ operator : ( op) ,
1459+ operand : ( Box :: new ( operand) ) ,
1460+ } )
1461+ } ,
1462+ Rule :: post_arithmetic_op => {
1463+ let op = parse_post_arithmetic_op ( first) ?;
1464+ Ok ( ArithmeticPart :: PostArithmeticExpr {
1465+ operator : ( op) ,
1466+ operand : ( Box :: new ( operand) ) ,
1467+ } )
1468+ } ,
1469+ _ => Err ( miette ! (
1470+ "Unexpected rule in unary arithmetic operator: {:?}" ,
1471+ first. as_rule( )
1472+ ) ) ,
14811473 }
14821474}
14831475
14841476fn unary_post_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
14851477 let mut inner = pair. into_inner ( ) ;
1486- let first = inner. next ( ) . unwrap ( ) ;
1487- let second = inner. next ( ) . unwrap ( ) ;
1478+ let first = inner. next ( ) . ok_or_else ( || miette ! ( "Expected unary post operator" ) ) ?;
1479+ let second = inner. next ( ) . ok_or_else ( || miette ! ( "Expected operand" ) ) ?;
1480+
14881481 let operand = match first. as_rule ( ) {
14891482 Rule :: parentheses_expr => {
14901483 let inner = first. into_inner ( ) . next ( ) . unwrap ( ) ;
@@ -1508,7 +1501,8 @@ fn unary_post_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
15081501}
15091502
15101503fn parse_unary_arithmetic_op ( pair : Pair < Rule > ) -> Result < UnaryArithmeticOp > {
1511- let first = pair. into_inner ( ) . next ( ) . unwrap ( ) ;
1504+ let first = pair. into_inner ( ) . next ( ) . ok_or_else ( || miette ! ( "Expected unary operator" ) ) ?;
1505+
15121506 match first. as_rule ( ) {
15131507 Rule :: add => Ok ( UnaryArithmeticOp :: Plus ) ,
15141508 Rule :: subtract => Ok ( UnaryArithmeticOp :: Minus ) ,
@@ -1522,7 +1516,7 @@ fn parse_unary_arithmetic_op(pair: Pair<Rule>) -> Result<UnaryArithmeticOp> {
15221516}
15231517
15241518fn parse_post_arithmetic_op ( pair : Pair < Rule > ) -> Result < PostArithmeticOp > {
1525- let first = pair. into_inner ( ) . next ( ) . unwrap ( ) ;
1519+ let first = pair. into_inner ( ) . next ( ) . ok_or_else ( || miette ! ( "Expected increament or decreament operator" ) ) ? ;
15261520 match first. as_rule ( ) {
15271521 Rule :: increment => Ok ( PostArithmeticOp :: Increment ) ,
15281522 Rule :: decrement => Ok ( PostArithmeticOp :: Decrement ) ,
0 commit comments