@@ -33,29 +33,6 @@ macro_rules! expr_prec_layer {
33
33
} ;
34
34
}
35
35
36
- fn alternative_binop < ' a > (
37
- rust : & ' static str ,
38
- dont_match : Option < & ' static str > ,
39
- rinja : & ' static str ,
40
- ) -> impl Fn ( & ' a str ) -> ParseResult < ' a , & ' static str > {
41
- move |i : & ' a str | -> ParseResult < ' a , & ' static str > {
42
- let ( _, fail) = opt ( tag ( rust) ) ( i) ?;
43
- if fail. is_some ( ) {
44
- let succeed = match dont_match {
45
- Some ( dont_match) => opt ( tag ( dont_match) ) ( i) ?. 1 . is_some ( ) ,
46
- None => false ,
47
- } ;
48
- if !succeed {
49
- return Err ( nom:: Err :: Failure ( ErrorContext :: new (
50
- format ! ( "the binary operator '{rust}' is called '{rinja}' in rinja" ) ,
51
- i,
52
- ) ) ) ;
53
- }
54
- }
55
- value ( rust, keyword ( rinja) ) ( i)
56
- }
57
- }
58
-
59
36
#[ derive( Clone , Debug , PartialEq ) ]
60
37
pub enum Expr < ' a > {
61
38
BoolLit ( & ' a str ) ,
@@ -207,8 +184,8 @@ impl<'a> Expr<'a> {
207
184
) )
208
185
) ;
209
186
expr_prec_layer ! ( bor, bxor, value( "|" , tag( "bitor" ) ) ) ;
210
- expr_prec_layer ! ( bxor, band, alternative_binop ( "^" , None , "xor" ) ) ;
211
- expr_prec_layer ! ( band, shifts, alternative_binop ( "&" , Some ( "&&" ) , "bitand" ) ) ;
187
+ expr_prec_layer ! ( bxor, band, token_xor ) ;
188
+ expr_prec_layer ! ( band, shifts, token_bitand ) ;
212
189
expr_prec_layer ! ( shifts, addsub, alt( ( tag( ">>" ) , tag( "<<" ) ) ) ) ;
213
190
expr_prec_layer ! ( addsub, muldivmod, alt( ( tag( "+" ) , tag( "-" ) ) ) ) ;
214
191
expr_prec_layer ! ( muldivmod, filtered, alt( ( tag( "*" ) , tag( "/" ) , tag( "%" ) ) ) ) ;
@@ -329,6 +306,33 @@ impl<'a> Expr<'a> {
329
306
}
330
307
}
331
308
309
+ fn token_xor ( i : & str ) -> ParseResult < ' _ > {
310
+ let ( i, good) = alt ( ( value ( true , keyword ( "xor" ) ) , value ( false , char ( '^' ) ) ) ) ( i) ?;
311
+ if good {
312
+ Ok ( ( i, "^" ) )
313
+ } else {
314
+ Err ( nom:: Err :: Failure ( ErrorContext :: new (
315
+ "the binary XOR operator is called `xor` in rinja" ,
316
+ i,
317
+ ) ) )
318
+ }
319
+ }
320
+
321
+ fn token_bitand ( i : & str ) -> ParseResult < ' _ > {
322
+ let ( i, good) = alt ( (
323
+ value ( true , keyword ( "bitand" ) ) ,
324
+ value ( false , pair ( char ( '&' ) , not ( char ( '&' ) ) ) ) ,
325
+ ) ) ( i) ?;
326
+ if good {
327
+ Ok ( ( i, "&" ) )
328
+ } else {
329
+ Err ( nom:: Err :: Failure ( ErrorContext :: new (
330
+ "the binary AND operator is called `bitand` in rinja" ,
331
+ i,
332
+ ) ) )
333
+ }
334
+ }
335
+
332
336
#[ derive( Clone , Debug , PartialEq ) ]
333
337
pub struct Filter < ' a > {
334
338
pub name : & ' a str ,
0 commit comments