Skip to content

Commit

Permalink
Remove helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Kijewski committed Jun 19, 2024
1 parent e78e2ec commit d19f304
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
54 changes: 29 additions & 25 deletions rinja_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,6 @@ macro_rules! expr_prec_layer {
};
}

fn alternative_binop<'a>(
rust: &'static str,
dont_match: Option<&'static str>,
rinja: &'static str,
) -> impl Fn(&'a str) -> ParseResult<'a, &'static str> {
move |i: &'a str| -> ParseResult<'a, &'static str> {
let (_, fail) = opt(tag(rust))(i)?;
if fail.is_some() {
let succeed = match dont_match {
Some(dont_match) => opt(tag(dont_match))(i)?.1.is_some(),
None => false,
};
if !succeed {
return Err(nom::Err::Failure(ErrorContext::new(
format!("the binary operator '{rust}' is called '{rinja}' in rinja"),
i,
)));
}
}
value(rust, keyword(rinja))(i)
}
}

#[derive(Clone, Debug, PartialEq)]
pub enum Expr<'a> {
BoolLit(&'a str),
Expand Down Expand Up @@ -207,8 +184,8 @@ impl<'a> Expr<'a> {
))
);
expr_prec_layer!(bor, bxor, value("|", tag("bitor")));
expr_prec_layer!(bxor, band, alternative_binop("^", None, "xor"));
expr_prec_layer!(band, shifts, alternative_binop("&", Some("&&"), "bitand"));
expr_prec_layer!(bxor, band, token_xor);
expr_prec_layer!(band, shifts, token_bitand);
expr_prec_layer!(shifts, addsub, alt((tag(">>"), tag("<<"))));
expr_prec_layer!(addsub, muldivmod, alt((tag("+"), tag("-"))));
expr_prec_layer!(muldivmod, filtered, alt((tag("*"), tag("/"), tag("%"))));
Expand Down Expand Up @@ -331,6 +308,33 @@ impl<'a> Expr<'a> {
}
}

fn token_xor(i: &str) -> ParseResult<'_> {
let (i, good) = alt((value(true, keyword("xor")), value(false, char('^'))))(i)?;
if good {
Ok((i, "^"))
} else {
Err(nom::Err::Failure(ErrorContext::new(
"the binary XOR operator is called `xor` in rinja",
i,
)))
}
}

fn token_bitand(i: &str) -> ParseResult<'_> {
let (i, good) = alt((
value(true, keyword("bitand")),
value(false, pair(char('&'), not(char('&')))),
))(i)?;
if good {
Ok((i, "&"))
} else {
Err(nom::Err::Failure(ErrorContext::new(
"the binary AND operator is called `bitand` in rinja",
i,
)))
}
}

#[derive(Clone, Debug, PartialEq)]
pub struct Filter<'a> {
pub name: &'a str,
Expand Down
12 changes: 6 additions & 6 deletions testing/tests/ui/iso646.stderr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
error: the binary operator '&' is called 'bitand' in rinja
failed to parse template source at row 1, column 5 near:
"& b }}"
error: the binary AND operator is called `bitand` in rinja
failed to parse template source at row 1, column 6 near:
" b }}"
--> tests/ui/iso646.rs:3:10
|
3 | #[derive(Template)]
Expand All @@ -19,9 +19,9 @@ error: The filter operator `|` must not be preceeded by any whitespace character
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)

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

0 comments on commit d19f304

Please sign in to comment.