@@ -1422,21 +1422,11 @@ fn parse_unary_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14221422 let first = inner. next ( ) . unwrap ( ) ;
14231423
14241424 match first. as_rule ( ) {
1425- Rule :: unary_arithmetic_op => {
1426- let op = parse_unary_arithmetic_op ( first) ?;
1427- let operand = parse_arithmetic_expr ( inner. next ( ) . unwrap ( ) ) ?;
1428- Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1429- operator : op,
1430- operand : Box :: new ( operand) ,
1431- } )
1425+ Rule :: unary_pre_arithmetic_expr => {
1426+ unary_pre_arithmetic_expr ( first)
14321427 }
1433- Rule :: post_arithmetic_op => {
1434- let operand = parse_arithmetic_expr ( inner. next ( ) . unwrap ( ) ) ?;
1435- let op = parse_post_arithmetic_op ( first) ?;
1436- Ok ( ArithmeticPart :: PostArithmeticExpr {
1437- operand : Box :: new ( operand) ,
1438- operator : op,
1439- } )
1428+ Rule :: unary_post_arithmatic_expr => {
1429+ unary_post_arithmatic_expr ( first)
14401430 }
14411431 _ => {
14421432 let operand = parse_arithmetic_expr ( first) ?;
@@ -1449,26 +1439,108 @@ fn parse_unary_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
14491439 }
14501440}
14511441
1442+ fn parse_unary_arithmetic_op_type ( pair : Pair < Rule > ) -> Result < bool > {
1443+ match pair. as_rule ( ) {
1444+ Rule :: unary_arithmetic_op => Ok ( true ) ,
1445+ Rule :: post_arithmetic_op => Ok ( false ) ,
1446+ _ => {
1447+ Err ( miette ! (
1448+ "Unexpected rule in unary arithmetic operator: {:?}" ,
1449+ pair. as_rule( )
1450+ ) )
1451+ }
1452+ }
1453+ }
1454+
1455+ fn unary_pre_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
1456+ let mut inner = pair. into_inner ( ) ;
1457+ let first = inner. next ( ) . unwrap ( ) ;
1458+ let second = inner. next ( ) . unwrap ( ) ;
1459+ let operand =
1460+ match second. as_rule ( ) {
1461+ Rule :: parentheses_expr => {
1462+ let inner = second. into_inner ( ) . next ( ) . unwrap ( ) ;
1463+ let parts = parse_arithmetic_sequence ( inner) ?;
1464+ Ok ( ArithmeticPart :: ParenthesesExpr ( Box :: new ( Arithmetic {
1465+ parts,
1466+ } ) ) )
1467+ } ,
1468+ Rule :: VARIABLE => {
1469+ Ok ( ArithmeticPart :: Variable ( second. as_str ( ) . to_string ( ) ) )
1470+ }
1471+ Rule :: NUMBER => Ok ( ArithmeticPart :: Number ( second. as_str ( ) . to_string ( ) ) ) ,
1472+ _ => Err ( miette ! (
1473+ "Unexpected rule in arithmetic expression: {:?}" ,
1474+ second. as_rule( )
1475+ ) ) ,
1476+ } ?;
1477+
1478+ if parse_unary_arithmetic_op_type ( first. clone ( ) ) ? {
1479+ let op = parse_unary_arithmetic_op ( first) ?;
1480+ Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1481+ operator : ( op) ,
1482+ operand : ( Box :: new ( operand) )
1483+ } )
1484+ } else {
1485+ let op = parse_post_arithmetic_op ( first) ?;
1486+ Ok ( ArithmeticPart :: PostArithmeticExpr {
1487+ operator : ( op) ,
1488+ operand : ( Box :: new ( operand) )
1489+ } )
1490+ }
1491+ }
1492+
1493+ fn unary_post_arithmatic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
1494+ let mut inner = pair. into_inner ( ) ;
1495+ let first = inner. next ( ) . unwrap ( ) ;
1496+ let second = inner. next ( ) . unwrap ( ) ;
1497+ let operand =
1498+ match first. as_rule ( ) {
1499+ Rule :: parentheses_expr => {
1500+ let inner = first. into_inner ( ) . next ( ) . unwrap ( ) ;
1501+ let parts = parse_arithmetic_sequence ( inner) ?;
1502+ Ok ( ArithmeticPart :: ParenthesesExpr ( Box :: new ( Arithmetic {
1503+ parts,
1504+ } ) ) )
1505+ } ,
1506+ Rule :: VARIABLE => {
1507+ Ok ( ArithmeticPart :: Variable ( first. as_str ( ) . to_string ( ) ) )
1508+ }
1509+ Rule :: NUMBER => Ok ( ArithmeticPart :: Number ( first. as_str ( ) . to_string ( ) ) ) ,
1510+ _ => Err ( miette ! (
1511+ "Unexpected rule in arithmetic expression: {:?}" ,
1512+ first. as_rule( )
1513+ ) ) ,
1514+ } ?;
1515+ let op = parse_post_arithmetic_op ( second) ?;
1516+ Ok ( ArithmeticPart :: PostArithmeticExpr {
1517+ operator : ( op) ,
1518+ operand : ( Box :: new ( operand) )
1519+ } )
1520+ }
1521+
14521522fn parse_unary_arithmetic_op ( pair : Pair < Rule > ) -> Result < UnaryArithmeticOp > {
1453- match pair. as_str ( ) {
1454- "+" => Ok ( UnaryArithmeticOp :: Plus ) ,
1455- "-" => Ok ( UnaryArithmeticOp :: Minus ) ,
1456- "!" => Ok ( UnaryArithmeticOp :: LogicalNot ) ,
1457- "~" => Ok ( UnaryArithmeticOp :: BitwiseNot ) ,
1523+ let first = pair. into_inner ( ) . next ( ) . unwrap ( ) ;
1524+ match first. as_rule ( ) {
1525+ Rule :: add => Ok ( UnaryArithmeticOp :: Plus ) ,
1526+ Rule :: subtract => Ok ( UnaryArithmeticOp :: Minus ) ,
1527+ Rule :: logical_not => Ok ( UnaryArithmeticOp :: LogicalNot ) ,
1528+ Rule :: bitwise_not => Ok ( UnaryArithmeticOp :: BitwiseNot ) ,
14581529 _ => Err ( miette ! (
1459- "Invalid unary arithmetic operator: {}" ,
1460- pair . as_str ( )
1530+ "Unexpected rule in unary arithmetic operator: {:? }" ,
1531+ first . as_rule ( )
14611532 ) ) ,
14621533 }
14631534}
14641535
14651536fn parse_post_arithmetic_op ( pair : Pair < Rule > ) -> Result < PostArithmeticOp > {
1466- match pair. as_str ( ) {
1467- "++" => Ok ( PostArithmeticOp :: Increment ) ,
1468- "--" => Ok ( PostArithmeticOp :: Decrement ) ,
1537+ let first = pair. into_inner ( ) . next ( ) . unwrap ( ) ;
1538+ match first. as_rule ( ) {
1539+ Rule :: increment => Ok ( PostArithmeticOp :: Increment ) ,
1540+ Rule :: decrement => Ok ( PostArithmeticOp :: Decrement ) ,
14691541 _ => Err ( miette ! (
1470- "Invalid post arithmetic operator: {}" ,
1471- pair . as_str ( )
1542+ "Unexpected rule in post arithmetic operator: {:? }" ,
1543+ first . as_rule ( )
14721544 ) ) ,
14731545 }
14741546}
0 commit comments