@@ -19,6 +19,7 @@ use nom::combinator::consumed;
1919use nom:: combinator:: map;
2020use nom:: combinator:: value;
2121use nom:: error:: context;
22+ use nom:: Slice ;
2223use nom_rule:: rule;
2324use pratt:: Affix ;
2425use pratt:: Associativity ;
@@ -126,7 +127,7 @@ pub fn subexpr(min_precedence: u32) -> impl FnMut(Input) -> IResult<Expr> {
126127 }
127128 }
128129
129- run_pratt_parser ( ExprParser , & expr_elements. into_iter ( ) , rest, i)
130+ run_pratt_parser ( ExprParser , expr_elements, rest, i)
130131 }
131132}
132133
@@ -1567,76 +1568,127 @@ pub fn expr_element(i: Input) -> IResult<WithSpan<ExprElement>> {
15671568 ) ( i)
15681569}
15691570
1571+ #[ inline]
1572+ fn return_op < T > ( i : Input , start : usize , op : T ) -> IResult < T > {
1573+ Ok ( ( i. slice ( start..) , op) )
1574+ }
1575+
1576+ macro_rules! op_branch {
1577+ ( $i: ident, $token_0: ident, $( $kind: ident => $op: expr) ,+ $( , ) ?) => {
1578+ match $token_0. kind {
1579+ $(
1580+ TokenKind :: $kind => return return_op( $i, 1 , $op) ,
1581+ ) +
1582+ _ => ( ) ,
1583+ }
1584+ } ;
1585+ }
1586+
15701587pub fn unary_op ( i : Input ) -> IResult < UnaryOperator > {
15711588 // Plus and Minus are parsed as binary op at first.
1572- alt ( (
1573- value ( UnaryOperator :: Not , rule ! { NOT } ) ,
1574- value ( UnaryOperator :: Factorial , rule ! { Factorial } ) ,
1575- value ( UnaryOperator :: SquareRoot , rule ! { SquareRoot } ) ,
1576- value ( UnaryOperator :: BitwiseNot , rule ! { BitWiseNot } ) ,
1577- value ( UnaryOperator :: CubeRoot , rule ! { CubeRoot } ) ,
1578- value ( UnaryOperator :: Abs , rule ! { Abs } ) ,
1579- ) ) ( i)
1589+ if let Some ( token_0) = i. tokens . first ( ) {
1590+ op_branch ! (
1591+ i, token_0,
1592+ NOT => UnaryOperator :: Not ,
1593+ Factorial => UnaryOperator :: Factorial ,
1594+ SquareRoot => UnaryOperator :: SquareRoot ,
1595+ BitWiseNot => UnaryOperator :: BitwiseNot ,
1596+ CubeRoot => UnaryOperator :: CubeRoot ,
1597+ Abs => UnaryOperator :: Abs ,
1598+ ) ;
1599+ }
1600+ Err ( nom:: Err :: Error ( Error :: from_error_kind (
1601+ i,
1602+ ErrorKind :: Other ( "expecting `NOT`, '!', '|/', '~', '||/', '@', or more ..." ) ,
1603+ ) ) )
15801604}
15811605
15821606pub fn binary_op ( i : Input ) -> IResult < BinaryOperator > {
1583- alt ( (
1584- alt ( (
1585- value ( BinaryOperator :: Plus , rule ! { "+" } ) ,
1586- value ( BinaryOperator :: Minus , rule ! { "-" } ) ,
1587- value ( BinaryOperator :: Multiply , rule ! { "*" } ) ,
1588- value ( BinaryOperator :: Divide , rule ! { "/" } ) ,
1589- value ( BinaryOperator :: IntDiv , rule ! { "//" } ) ,
1590- value ( BinaryOperator :: Div , rule ! { DIV } ) ,
1591- value ( BinaryOperator :: Modulo , rule ! { "%" } ) ,
1592- value ( BinaryOperator :: StringConcat , rule ! { "||" } ) ,
1593- value ( BinaryOperator :: CosineDistance , rule ! { "<=>" } ) ,
1594- value ( BinaryOperator :: L1Distance , rule ! { "<+>" } ) ,
1595- value ( BinaryOperator :: L2Distance , rule ! { "<->" } ) ,
1596- value ( BinaryOperator :: Gt , rule ! { ">" } ) ,
1597- value ( BinaryOperator :: Lt , rule ! { "<" } ) ,
1598- value ( BinaryOperator :: Gte , rule ! { ">=" } ) ,
1599- value ( BinaryOperator :: Lte , rule ! { "<=" } ) ,
1600- value ( BinaryOperator :: Eq , rule ! { "=" } ) ,
1601- value ( BinaryOperator :: NotEq , rule ! { "<>" | "!=" } ) ,
1602- value ( BinaryOperator :: Caret , rule ! { "^" } ) ,
1603- ) ) ,
1604- alt ( (
1605- value ( BinaryOperator :: And , rule ! { AND } ) ,
1606- value ( BinaryOperator :: Or , rule ! { OR } ) ,
1607- value ( BinaryOperator :: Xor , rule ! { XOR } ) ,
1608- value ( BinaryOperator :: LikeAny ( None ) , rule ! { LIKE ~ ANY } ) ,
1609- value ( BinaryOperator :: Like ( None ) , rule ! { LIKE } ) ,
1610- value ( BinaryOperator :: NotLike ( None ) , rule ! { NOT ~ LIKE } ) ,
1611- value ( BinaryOperator :: Regexp , rule ! { REGEXP } ) ,
1612- value ( BinaryOperator :: NotRegexp , rule ! { NOT ~ REGEXP } ) ,
1613- value ( BinaryOperator :: RLike , rule ! { RLIKE } ) ,
1614- value ( BinaryOperator :: NotRLike , rule ! { NOT ~ RLIKE } ) ,
1615- value ( BinaryOperator :: SoundsLike , rule ! { SOUNDS ~ LIKE } ) ,
1616- value ( BinaryOperator :: BitwiseOr , rule ! { BitWiseOr } ) ,
1617- value ( BinaryOperator :: BitwiseAnd , rule ! { BitWiseAnd } ) ,
1618- value ( BinaryOperator :: BitwiseXor , rule ! { BitWiseXor } ) ,
1619- value ( BinaryOperator :: BitwiseShiftLeft , rule ! { ShiftLeft } ) ,
1620- value ( BinaryOperator :: BitwiseShiftRight , rule ! { ShiftRight } ) ,
1621- ) ) ,
1622- ) ) ( i)
1607+ if let Some ( token_0) = i. tokens . first ( ) {
1608+ op_branch ! (
1609+ i, token_0,
1610+ Plus => BinaryOperator :: Plus ,
1611+ Minus => BinaryOperator :: Minus ,
1612+ Multiply => BinaryOperator :: Multiply ,
1613+ Divide => BinaryOperator :: Divide ,
1614+ IntDiv => BinaryOperator :: IntDiv ,
1615+ DIV => BinaryOperator :: Div ,
1616+ Modulo => BinaryOperator :: Modulo ,
1617+ StringConcat => BinaryOperator :: StringConcat ,
1618+ Spaceship => BinaryOperator :: CosineDistance ,
1619+ L1DISTANCE => BinaryOperator :: L1Distance ,
1620+ L2DISTANCE => BinaryOperator :: L2Distance ,
1621+ Gt => BinaryOperator :: Gt ,
1622+ Lt => BinaryOperator :: Lt ,
1623+ Gte => BinaryOperator :: Gte ,
1624+ Lte => BinaryOperator :: Lte ,
1625+ Eq => BinaryOperator :: Eq ,
1626+ NotEq => BinaryOperator :: NotEq ,
1627+ Caret => BinaryOperator :: Caret ,
1628+ AND => BinaryOperator :: And ,
1629+ OR => BinaryOperator :: Or ,
1630+ XOR => BinaryOperator :: Xor ,
1631+ REGEXP => BinaryOperator :: Regexp ,
1632+ RLIKE => BinaryOperator :: RLike ,
1633+ BitWiseOr => BinaryOperator :: BitwiseOr ,
1634+ BitWiseAnd => BinaryOperator :: BitwiseAnd ,
1635+ BitWiseXor => BinaryOperator :: BitwiseXor ,
1636+ ShiftLeft => BinaryOperator :: BitwiseShiftLeft ,
1637+ ShiftRight => BinaryOperator :: BitwiseShiftRight ,
1638+ ) ;
1639+ match token_0. kind {
1640+ TokenKind :: LIKE => {
1641+ return if matches ! (
1642+ i. tokens. get( 1 ) . map( |first| first. kind == TokenKind :: ANY ) ,
1643+ Some ( true )
1644+ ) {
1645+ return_op ( i, 2 , BinaryOperator :: LikeAny ( None ) )
1646+ } else {
1647+ return_op ( i, 1 , BinaryOperator :: Like ( None ) )
1648+ }
1649+ }
1650+ TokenKind :: NOT => match i. tokens . get ( 1 ) . map ( |first| first. kind ) {
1651+ Some ( TokenKind :: LIKE ) => {
1652+ return return_op ( i, 2 , BinaryOperator :: NotLike ( None ) ) ;
1653+ }
1654+ Some ( TokenKind :: REGEXP ) => {
1655+ return return_op ( i, 2 , BinaryOperator :: NotRegexp ) ;
1656+ }
1657+ Some ( TokenKind :: RLIKE ) => {
1658+ return return_op ( i, 2 , BinaryOperator :: NotRLike ) ;
1659+ }
1660+ _ => ( ) ,
1661+ } ,
1662+ TokenKind :: SOUNDS => {
1663+ if let Some ( TokenKind :: LIKE ) = i. tokens . get ( 1 ) . map ( |first| first. kind ) {
1664+ return return_op ( i, 2 , BinaryOperator :: SoundsLike ) ;
1665+ }
1666+ }
1667+ _ => ( ) ,
1668+ }
1669+ }
1670+ Err ( nom:: Err :: Error ( Error :: from_error_kind ( i, ErrorKind :: Other ( "expecting `IS`, `IN`, `LIKE`, `EXISTS`, `BETWEEN`, `+`, `-`, `*`, `/`, `//`, `DIV`, `%`, `||`, `<=>`, `<+>`, `<->`, `>`, `<`, `>=`, `<=`, `=`, `<>`, `!=`, `^`, `AND`, `OR`, `XOR`, `NOT`, `REGEXP`, `RLIKE`, `SOUNDS`, or more ..." ) ) ) )
16231671}
16241672
1625- pub fn json_op ( i : Input ) -> IResult < JsonOperator > {
1626- alt ( (
1627- value ( JsonOperator :: Arrow , rule ! { "->" } ) ,
1628- value ( JsonOperator :: LongArrow , rule ! { "->>" } ) ,
1629- value ( JsonOperator :: HashArrow , rule ! { "#>" } ) ,
1630- value ( JsonOperator :: HashLongArrow , rule ! { "#>>" } ) ,
1631- value ( JsonOperator :: Question , rule ! { "?" } ) ,
1632- value ( JsonOperator :: QuestionOr , rule ! { "?|" } ) ,
1633- value ( JsonOperator :: QuestionAnd , rule ! { "?&" } ) ,
1634- value ( JsonOperator :: AtArrow , rule ! { "@>" } ) ,
1635- value ( JsonOperator :: ArrowAt , rule ! { "<@" } ) ,
1636- value ( JsonOperator :: AtQuestion , rule ! { "@?" } ) ,
1637- value ( JsonOperator :: AtAt , rule ! { "@@" } ) ,
1638- value ( JsonOperator :: HashMinus , rule ! { "#-" } ) ,
1639- ) ) ( i)
1673+ pub ( crate ) fn json_op ( i : Input ) -> IResult < JsonOperator > {
1674+ if let Some ( token_0) = i. tokens . first ( ) {
1675+ op_branch ! (
1676+ i, token_0,
1677+ RArrow => JsonOperator :: Arrow ,
1678+ LongRArrow => JsonOperator :: LongArrow ,
1679+ HashRArrow => JsonOperator :: HashArrow ,
1680+ HashLongRArrow => JsonOperator :: HashLongArrow ,
1681+ Placeholder => JsonOperator :: Question ,
1682+ QuestionOr => JsonOperator :: QuestionOr ,
1683+ QuestionAnd => JsonOperator :: QuestionAnd ,
1684+ AtArrow => JsonOperator :: AtArrow ,
1685+ ArrowAt => JsonOperator :: ArrowAt ,
1686+ AtQuestion => JsonOperator :: AtQuestion ,
1687+ AtAt => JsonOperator :: AtAt ,
1688+ HashMinus => JsonOperator :: HashMinus ,
1689+ ) ;
1690+ }
1691+ Err ( nom:: Err :: Error ( Error :: from_error_kind ( i, ErrorKind :: Other ( "expecting `->`, '->>', '#>', '#>>', '?', '?|', '?&', '@>', '<@', '@?', '@@', '#-', or more ..." ) ) ) )
16401692}
16411693
16421694pub fn literal ( i : Input ) -> IResult < Literal > {
0 commit comments