Skip to content

Commit 50ac555

Browse files
committed
Remove helper functions
1 parent fb4d8a6 commit 50ac555

File tree

2 files changed

+35
-31
lines changed

2 files changed

+35
-31
lines changed

rinja_parser/src/expr.rs

+29-25
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,6 @@ macro_rules! expr_prec_layer {
3333
};
3434
}
3535

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-
5936
#[derive(Clone, Debug, PartialEq)]
6037
pub enum Expr<'a> {
6138
BoolLit(&'a str),
@@ -207,8 +184,8 @@ impl<'a> Expr<'a> {
207184
))
208185
);
209186
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);
212189
expr_prec_layer!(shifts, addsub, alt((tag(">>"), tag("<<"))));
213190
expr_prec_layer!(addsub, muldivmod, alt((tag("+"), tag("-"))));
214191
expr_prec_layer!(muldivmod, filtered, alt((tag("*"), tag("/"), tag("%"))));
@@ -329,6 +306,33 @@ impl<'a> Expr<'a> {
329306
}
330307
}
331308

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+
332336
#[derive(Clone, Debug, PartialEq)]
333337
pub struct Filter<'a> {
334338
pub name: &'a str,

testing/tests/ui/iso646.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
error: the binary operator '&' is called 'bitand' in rinja
2-
failed to parse template source at row 1, column 5 near:
3-
"& b }}"
1+
error: the binary AND operator is called `bitand` in rinja
2+
failed to parse template source at row 1, column 6 near:
3+
" b }}"
44
--> tests/ui/iso646.rs:3:10
55
|
66
3 | #[derive(Template)]
@@ -19,9 +19,9 @@ error: the filter operator `|` must not be preceeded by any whitespace character
1919
|
2020
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)
2121

22-
error: the binary operator '^' is called 'xor' in rinja
23-
failed to parse template source at row 1, column 5 near:
24-
"^ b }}"
22+
error: the binary XOR operator is called `xor` in rinja
23+
failed to parse template source at row 1, column 6 near:
24+
" b }}"
2525
--> tests/ui/iso646.rs:31:10
2626
|
2727
31 | #[derive(Template)]

0 commit comments

Comments
 (0)