@@ -453,41 +453,41 @@ pub struct Arithmetic {
453453#[ derive( Debug , Clone , PartialEq , Eq , Error ) ]
454454#[ error( "Invalid arithmetic part" ) ]
455455pub enum ArithmeticPart {
456- #[ error( "Invalid parentheses expression" ) ]
457- ParenthesesExpr ( Box < Arithmetic > ) ,
458- #[ error( "Invalid variable assignment" ) ]
459- VariableAssignment {
460- name : String ,
461- op : AssignmentOp ,
462- value : Box < ArithmeticPart > ,
463- } ,
464- #[ error( "Invalid triple conditional expression" ) ]
465- TripleConditionalExpr {
466- condition : Box < ArithmeticPart > ,
467- true_expr : Box < ArithmeticPart > ,
468- false_expr : Box < ArithmeticPart > ,
469- } ,
470- #[ error( "Invalid binary arithmetic expression" ) ]
471- BinaryArithmeticExpr {
472- left : Box < ArithmeticPart > ,
473- operator : BinaryArithmeticOp ,
474- right : Box < ArithmeticPart > ,
475- } ,
476- #[ error( "Invalid binary conditional expression" ) ]
477- BinaryConditionalExpr {
478- left : Box < ArithmeticPart > ,
479- operator : BinaryOp ,
480- right : Box < ArithmeticPart > ,
481- } ,
482- #[ error( "Invalid unary arithmetic expression" ) ]
483- UnaryArithmeticExpr {
484- operator : UnaryArithmeticOp ,
485- operand : Box < ArithmeticPart > ,
486- } ,
487- #[ error( "Invalid variable" ) ]
488- Variable ( String ) ,
489- #[ error( "Invalid number" ) ]
490- Number ( String ) ,
456+ #[ error( "Invalid parentheses expression" ) ]
457+ ParenthesesExpr ( Box < Arithmetic > ) ,
458+ #[ error( "Invalid variable assignment" ) ]
459+ VariableAssignment {
460+ name : String ,
461+ op : AssignmentOp ,
462+ value : Box < ArithmeticPart > ,
463+ } ,
464+ #[ error( "Invalid triple conditional expression" ) ]
465+ TripleConditionalExpr {
466+ condition : Box < ArithmeticPart > ,
467+ true_expr : Box < ArithmeticPart > ,
468+ false_expr : Box < ArithmeticPart > ,
469+ } ,
470+ #[ error( "Invalid binary arithmetic expression" ) ]
471+ BinaryArithmeticExpr {
472+ left : Box < ArithmeticPart > ,
473+ operator : BinaryArithmeticOp ,
474+ right : Box < ArithmeticPart > ,
475+ } ,
476+ #[ error( "Invalid binary conditional expression" ) ]
477+ BinaryConditionalExpr {
478+ left : Box < ArithmeticPart > ,
479+ operator : BinaryOp ,
480+ right : Box < ArithmeticPart > ,
481+ } ,
482+ #[ error( "Invalid unary arithmetic expression" ) ]
483+ UnaryArithmeticExpr {
484+ operator : UnaryArithmeticOp ,
485+ operand : Box < ArithmeticPart > ,
486+ } ,
487+ #[ error( "Invalid variable" ) ]
488+ Variable ( String ) ,
489+ #[ error( "Invalid number" ) ]
490+ Number ( String ) ,
491491}
492492
493493#[ cfg_attr( feature = "serialization" , derive( serde:: Serialize ) ) ]
@@ -530,12 +530,12 @@ pub enum AssignmentOp {
530530#[ cfg_attr( feature = "serialization" , serde( rename_all = "camelCase" ) ) ]
531531#[ derive( Debug , Clone , PartialEq , Eq , Copy ) ]
532532pub enum PreArithmeticOp {
533- Increment , // ++
534- Decrement , // --
535- Plus , // +
536- Minus , // -
537- LogicalNot , // !
538- BitwiseNot , // ~
533+ Increment , // ++
534+ Decrement , // --
535+ Plus , // +
536+ Minus , // -
537+ LogicalNot , // !
538+ BitwiseNot , // ~
539539}
540540
541541#[ cfg_attr( feature = "serialization" , derive( serde:: Serialize ) ) ]
@@ -550,8 +550,8 @@ pub enum PostArithmeticOp {
550550#[ cfg_attr( feature = "serialization" , serde( rename_all = "camelCase" ) ) ]
551551#[ derive( Debug , Clone , PartialEq , Eq , Copy ) ]
552552pub enum UnaryArithmeticOp {
553- Pre ( PreArithmeticOp ) ,
554- Post ( PostArithmeticOp ) ,
553+ Pre ( PreArithmeticOp ) ,
554+ Post ( PostArithmeticOp ) ,
555555}
556556
557557#[ cfg_attr( feature = "serialization" , derive( serde:: Serialize ) ) ]
@@ -1712,148 +1712,154 @@ fn parse_arithmetic_expr(pair: Pair<Rule>) -> Result<ArithmeticPart> {
17121712}
17131713
17141714fn parse_unary_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
1715- let mut inner = pair. into_inner ( ) ;
1716- let first = inner
1717- . next ( )
1718- . ok_or_else ( || miette ! ( "Expected unary operator" ) ) ?;
1715+ let mut inner = pair. into_inner ( ) ;
1716+ let first = inner
1717+ . next ( )
1718+ . ok_or_else ( || miette ! ( "Expected unary operator" ) ) ?;
17191719
1720- match first. as_rule ( ) {
1721- Rule :: unary_pre_arithmetic_expr => unary_pre_arithmetic_expr ( first) ,
1722- Rule :: unary_post_arithmetic_expr => unary_post_arithmetic_expr ( first) ,
1723- _ => Err ( miette ! (
1724- "Unexpected rule in unary arithmetic expression: {:?}" ,
1725- first. as_rule( )
1726- ) ) ,
1727- }
1720+ match first. as_rule ( ) {
1721+ Rule :: unary_pre_arithmetic_expr => unary_pre_arithmetic_expr ( first) ,
1722+ Rule :: unary_post_arithmetic_expr => unary_post_arithmetic_expr ( first) ,
1723+ _ => Err ( miette ! (
1724+ "Unexpected rule in unary arithmetic expression: {:?}" ,
1725+ first. as_rule( )
1726+ ) ) ,
1727+ }
17281728}
17291729
17301730fn unary_pre_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
1731- let mut inner = pair. into_inner ( ) ;
1732- let first = inner
1733- . next ( )
1734- . ok_or_else ( || miette ! ( "Expected unary pre operator" ) ) ?;
1735- let second = inner. next ( ) . ok_or_else ( || miette ! ( "Expected operand" ) ) ?;
1736- let operand = match second. as_rule ( ) {
1737- Rule :: parentheses_expr => {
1738- let inner = second
1739- . into_inner ( )
1731+ let mut inner = pair. into_inner ( ) ;
1732+ let first = inner
17401733 . next ( )
1741- . ok_or_else ( || miette ! ( "Expected expression in parentheses" ) ) ?;
1742- let parts = parse_arithmetic_sequence ( inner) ?;
1743- Ok ( ArithmeticPart :: ParenthesesExpr ( Box :: new ( Arithmetic {
1744- parts,
1745- } ) ) )
1746- }
1747- Rule :: VARIABLE => Ok ( ArithmeticPart :: Variable ( second. as_str ( ) . to_string ( ) ) ) ,
1748- Rule :: NUMBER => Ok ( ArithmeticPart :: Number ( second. as_str ( ) . to_string ( ) ) ) ,
1749- _ => Err ( miette ! (
1750- "Unexpected rule in arithmetic expression: {:?}" ,
1751- second. as_rule( )
1752- ) ) ,
1753- } ?;
1754-
1755- match first. as_rule ( ) {
1756- Rule :: pre_arithmetic_op => {
1757- let op = parse_pre_arithmetic_op ( first) ?;
1758- // Validate that increment/decrement are only applied to variables or expressions
1759- if matches ! ( op, PreArithmeticOp :: Increment | PreArithmeticOp :: Decrement ) {
1760- if matches ! ( operand, ArithmeticPart :: Number ( _) ) {
1761- return Err ( miette ! (
1734+ . ok_or_else ( || miette ! ( "Expected unary pre operator" ) ) ?;
1735+ let second = inner. next ( ) . ok_or_else ( || miette ! ( "Expected operand" ) ) ?;
1736+ let operand = match second. as_rule ( ) {
1737+ Rule :: parentheses_expr => {
1738+ let inner = second
1739+ . into_inner ( )
1740+ . next ( )
1741+ . ok_or_else ( || miette ! ( "Expected expression in parentheses" ) ) ?;
1742+ let parts = parse_arithmetic_sequence ( inner) ?;
1743+ Ok ( ArithmeticPart :: ParenthesesExpr ( Box :: new ( Arithmetic {
1744+ parts,
1745+ } ) ) )
1746+ }
1747+ Rule :: VARIABLE => {
1748+ Ok ( ArithmeticPart :: Variable ( second. as_str ( ) . to_string ( ) ) )
1749+ }
1750+ Rule :: NUMBER => Ok ( ArithmeticPart :: Number ( second. as_str ( ) . to_string ( ) ) ) ,
1751+ _ => Err ( miette ! (
1752+ "Unexpected rule in arithmetic expression: {:?}" ,
1753+ second. as_rule( )
1754+ ) ) ,
1755+ } ?;
1756+
1757+ match first. as_rule ( ) {
1758+ Rule :: pre_arithmetic_op => {
1759+ let op = parse_pre_arithmetic_op ( first) ?;
1760+ // Validate that increment/decrement are only applied to variables or expressions
1761+ if matches ! (
1762+ op,
1763+ PreArithmeticOp :: Increment | PreArithmeticOp :: Decrement
1764+ ) && matches ! ( operand, ArithmeticPart :: Number ( _) )
1765+ {
1766+ return Err ( miette ! (
17621767 "Increment/decrement operators cannot be applied to literal numbers"
17631768 ) ) ;
1769+ }
1770+ Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1771+ operator : UnaryArithmeticOp :: Pre ( op) ,
1772+ operand : Box :: new ( operand) ,
1773+ } )
17641774 }
1765- }
1766- Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1767- operator : UnaryArithmeticOp :: Pre ( op) ,
1768- operand : Box :: new ( operand) ,
1769- } )
1770- }
1771- Rule :: post_arithmetic_op => {
1772- let op = parse_post_arithmetic_op ( first) ?;
1773- Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1774- operator : UnaryArithmeticOp :: Post ( op) ,
1775- operand : Box :: new ( operand) ,
1776- } )
1775+ Rule :: post_arithmetic_op => {
1776+ let op = parse_post_arithmetic_op ( first) ?;
1777+ Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1778+ operator : UnaryArithmeticOp :: Post ( op) ,
1779+ operand : Box :: new ( operand) ,
1780+ } )
1781+ }
1782+ _ => Err ( miette ! (
1783+ "Unexpected rule in unary arithmetic operator: {:?}" ,
1784+ first. as_rule( )
1785+ ) ) ,
17771786 }
1778- _ => Err ( miette ! (
1779- "Unexpected rule in unary arithmetic operator: {:?}" ,
1780- first. as_rule( )
1781- ) ) ,
1782- }
17831787}
17841788
17851789fn unary_post_arithmetic_expr ( pair : Pair < Rule > ) -> Result < ArithmeticPart > {
1786- let mut inner = pair. into_inner ( ) ;
1787- let first = inner
1788- . next ( )
1789- . ok_or_else ( || miette ! ( "Expected unary post operator" ) ) ?;
1790- let second = inner. next ( ) . ok_or_else ( || miette ! ( "Expected operand" ) ) ?;
1791-
1792- let operand = match first. as_rule ( ) {
1793- Rule :: parentheses_expr => {
1794- let inner = first
1795- . into_inner ( )
1790+ let mut inner = pair. into_inner ( ) ;
1791+ let first = inner
17961792 . next ( )
1797- . ok_or_else ( || miette ! ( "Expected expression in parentheses" ) ) ?;
1798- let parts = parse_arithmetic_sequence ( inner) ?;
1799- Ok ( ArithmeticPart :: ParenthesesExpr ( Box :: new ( Arithmetic {
1800- parts,
1801- } ) ) )
1802- }
1803- Rule :: VARIABLE => Ok ( ArithmeticPart :: Variable ( first. as_str ( ) . to_string ( ) ) ) ,
1804- Rule :: NUMBER => Ok ( ArithmeticPart :: Number ( first. as_str ( ) . to_string ( ) ) ) ,
1805- _ => Err ( miette ! (
1806- "Unexpected rule in arithmetic expression: {:?}" ,
1807- first. as_rule( )
1808- ) ) ,
1809- } ?;
1810-
1811- // Validate that increment/decrement are only applied to variables or expressions
1812- if matches ! ( operand, ArithmeticPart :: Number ( _) ) {
1813- return Err ( miette ! (
1793+ . ok_or_else ( || miette ! ( "Expected unary post operator" ) ) ?;
1794+ let second = inner. next ( ) . ok_or_else ( || miette ! ( "Expected operand" ) ) ?;
1795+
1796+ let operand = match first. as_rule ( ) {
1797+ Rule :: parentheses_expr => {
1798+ let inner = first
1799+ . into_inner ( )
1800+ . next ( )
1801+ . ok_or_else ( || miette ! ( "Expected expression in parentheses" ) ) ?;
1802+ let parts = parse_arithmetic_sequence ( inner) ?;
1803+ Ok ( ArithmeticPart :: ParenthesesExpr ( Box :: new ( Arithmetic {
1804+ parts,
1805+ } ) ) )
1806+ }
1807+ Rule :: VARIABLE => {
1808+ Ok ( ArithmeticPart :: Variable ( first. as_str ( ) . to_string ( ) ) )
1809+ }
1810+ Rule :: NUMBER => Ok ( ArithmeticPart :: Number ( first. as_str ( ) . to_string ( ) ) ) ,
1811+ _ => Err ( miette ! (
1812+ "Unexpected rule in arithmetic expression: {:?}" ,
1813+ first. as_rule( )
1814+ ) ) ,
1815+ } ?;
1816+
1817+ // Validate that increment/decrement are only applied to variables or expressions
1818+ if matches ! ( operand, ArithmeticPart :: Number ( _) ) {
1819+ return Err ( miette ! (
18141820 "Increment/decrement operators cannot be applied to literal numbers"
18151821 ) ) ;
1816- }
1822+ }
18171823
1818- let op = parse_post_arithmetic_op ( second) ?;
1819- Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1820- operator : UnaryArithmeticOp :: Post ( op) ,
1821- operand : Box :: new ( operand) ,
1822- } )
1824+ let op = parse_post_arithmetic_op ( second) ?;
1825+ Ok ( ArithmeticPart :: UnaryArithmeticExpr {
1826+ operator : UnaryArithmeticOp :: Post ( op) ,
1827+ operand : Box :: new ( operand) ,
1828+ } )
18231829}
18241830
18251831fn parse_pre_arithmetic_op ( pair : Pair < Rule > ) -> Result < PreArithmeticOp > {
1826- let first = pair
1827- . into_inner ( )
1828- . next ( )
1829- . ok_or_else ( || miette ! ( "Expected increment or decrement operator" ) ) ?;
1830- match first. as_rule ( ) {
1831- Rule :: increment => Ok ( PreArithmeticOp :: Increment ) ,
1832- Rule :: decrement => Ok ( PreArithmeticOp :: Decrement ) ,
1833- Rule :: unary_plus => Ok ( PreArithmeticOp :: Plus ) ,
1834- Rule :: unary_minus => Ok ( PreArithmeticOp :: Minus ) ,
1835- Rule :: logical_not => Ok ( PreArithmeticOp :: LogicalNot ) ,
1836- Rule :: bitwise_not => Ok ( PreArithmeticOp :: BitwiseNot ) ,
1837- _ => Err ( miette ! (
1838- "Unexpected rule in pre arithmetic operator: {:?}" ,
1839- first. as_rule( )
1840- ) ) ,
1841- }
1832+ let first = pair
1833+ . into_inner ( )
1834+ . next ( )
1835+ . ok_or_else ( || miette ! ( "Expected increment or decrement operator" ) ) ?;
1836+ match first. as_rule ( ) {
1837+ Rule :: increment => Ok ( PreArithmeticOp :: Increment ) ,
1838+ Rule :: decrement => Ok ( PreArithmeticOp :: Decrement ) ,
1839+ Rule :: unary_plus => Ok ( PreArithmeticOp :: Plus ) ,
1840+ Rule :: unary_minus => Ok ( PreArithmeticOp :: Minus ) ,
1841+ Rule :: logical_not => Ok ( PreArithmeticOp :: LogicalNot ) ,
1842+ Rule :: bitwise_not => Ok ( PreArithmeticOp :: BitwiseNot ) ,
1843+ _ => Err ( miette ! (
1844+ "Unexpected rule in pre arithmetic operator: {:?}" ,
1845+ first. as_rule( )
1846+ ) ) ,
1847+ }
18421848}
18431849
18441850fn parse_post_arithmetic_op ( pair : Pair < Rule > ) -> Result < PostArithmeticOp > {
1845- let first = pair
1846- . into_inner ( )
1847- . next ( )
1848- . ok_or_else ( || miette ! ( "Expected increment or decrement operator" ) ) ?;
1849- match first. as_rule ( ) {
1850- Rule :: increment => Ok ( PostArithmeticOp :: Increment ) ,
1851- Rule :: decrement => Ok ( PostArithmeticOp :: Decrement ) ,
1852- _ => Err ( miette ! (
1853- "Unexpected rule in post arithmetic operator: {:?}" ,
1854- first. as_rule( )
1855- ) ) ,
1856- }
1851+ let first = pair
1852+ . into_inner ( )
1853+ . next ( )
1854+ . ok_or_else ( || miette ! ( "Expected increment or decrement operator" ) ) ?;
1855+ match first. as_rule ( ) {
1856+ Rule :: increment => Ok ( PostArithmeticOp :: Increment ) ,
1857+ Rule :: decrement => Ok ( PostArithmeticOp :: Decrement ) ,
1858+ _ => Err ( miette ! (
1859+ "Unexpected rule in post arithmetic operator: {:?}" ,
1860+ first. as_rule( )
1861+ ) ) ,
1862+ }
18571863}
18581864
18591865fn parse_variable_expansion ( part : Pair < Rule > ) -> Result < WordPart > {
0 commit comments