mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-02 15:25:19 +00:00
parser: better error msg for unkn. number suffices
This commit is contained in:
parent
311235f7eb
commit
b593f1ffa7
@ -416,23 +416,36 @@ fn bool_lit(i: &str) -> ParseResult<'_> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn num_lit(i: &str) -> ParseResult<'_> {
|
fn num_lit(i: &str) -> ParseResult<'_> {
|
||||||
let integer_suffix = |i| {
|
const INTEGER_SUFFIX: &[&str] = &[
|
||||||
alt((
|
"i8", "i16", "i32", "i64", "i128", "isize", "u8", "u16", "u32", "u64", "u128", "usize",
|
||||||
tag("i8"),
|
];
|
||||||
tag("i16"),
|
const FLOAT_SUFFIX: &[&str] = &["f16", "f32", "f64", "f128"];
|
||||||
tag("i32"),
|
|
||||||
tag("i64"),
|
fn suffix<'a>(
|
||||||
tag("i128"),
|
start: &'a str,
|
||||||
tag("isize"),
|
kind: &'a str,
|
||||||
tag("u8"),
|
list: &'a [&[&str]],
|
||||||
tag("u16"),
|
ignore: &'a [&str],
|
||||||
tag("u32"),
|
) -> impl Fn(&'a str) -> ParseResult<'a> + Copy + 'a {
|
||||||
tag("u64"),
|
move |i| {
|
||||||
tag("u128"),
|
let (i, suffix) = identifier(i)?;
|
||||||
tag("usize"),
|
if list.iter().flat_map(|&i| i).any(|&item| item == suffix) {
|
||||||
))(i)
|
Ok((i, suffix))
|
||||||
};
|
} else if ignore.contains(&suffix) {
|
||||||
let float_suffix = |i| alt((tag("f16"), tag("f32"), tag("f64"), tag("f128")))(i);
|
// no need for a message, this case only occures in an `opt(…)`
|
||||||
|
Err(nom::Err::Error(ErrorContext::new("", i)))
|
||||||
|
} else {
|
||||||
|
Err(nom::Err::Failure(ErrorContext::new(
|
||||||
|
format!("unknown {kind} suffix `{suffix}`"),
|
||||||
|
start,
|
||||||
|
)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let integer_suffix = suffix(i, "integer", &[INTEGER_SUFFIX], &[]);
|
||||||
|
let float_suffix = suffix(i, "float", &[FLOAT_SUFFIX], &["e"]);
|
||||||
|
let either_suffix = suffix(i, "number", &[INTEGER_SUFFIX, FLOAT_SUFFIX], &["e"]);
|
||||||
|
|
||||||
recognize(tuple((
|
recognize(tuple((
|
||||||
opt(char('-')),
|
opt(char('-')),
|
||||||
@ -449,8 +462,7 @@ fn num_lit(i: &str) -> ParseResult<'_> {
|
|||||||
recognize(tuple((
|
recognize(tuple((
|
||||||
separated_digits(10, true),
|
separated_digits(10, true),
|
||||||
opt(alt((
|
opt(alt((
|
||||||
integer_suffix,
|
either_suffix,
|
||||||
float_suffix,
|
|
||||||
recognize(tuple((
|
recognize(tuple((
|
||||||
opt(tuple((char('.'), separated_digits(10, true)))),
|
opt(tuple((char('.'), separated_digits(10, true)))),
|
||||||
one_of("eE"),
|
one_of("eE"),
|
||||||
|
26
testing/tests/ui/num-suffix.rs
Normal file
26
testing/tests/ui/num-suffix.rs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
use rinja::Template;
|
||||||
|
|
||||||
|
#[allow(non_camel_case_types)]
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(
|
||||||
|
ext = "html",
|
||||||
|
source = "{{ 0x0x }}",
|
||||||
|
)]
|
||||||
|
struct IntSuffix;
|
||||||
|
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(
|
||||||
|
ext = "html",
|
||||||
|
source = "{{ 0.0_f127 }}",
|
||||||
|
)]
|
||||||
|
struct FloatSuffix;
|
||||||
|
|
||||||
|
#[derive(Template)]
|
||||||
|
#[template(
|
||||||
|
ext = "html",
|
||||||
|
source = "{{ 654u321 }}",
|
||||||
|
)]
|
||||||
|
struct EitherSuffix;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
}
|
29
testing/tests/ui/num-suffix.stderr
Normal file
29
testing/tests/ui/num-suffix.stderr
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
error: unknown integer suffix `x`
|
||||||
|
failed to parse template source at row 1, column 3 near:
|
||||||
|
"0x0x }}"
|
||||||
|
--> tests/ui/num-suffix.rs:4:10
|
||||||
|
|
|
||||||
|
4 | #[derive(Template)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: unknown float suffix `f127`
|
||||||
|
failed to parse template source at row 1, column 3 near:
|
||||||
|
"0.0_f127 }}"
|
||||||
|
--> tests/ui/num-suffix.rs:11:10
|
||||||
|
|
|
||||||
|
11 | #[derive(Template)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||||
|
|
||||||
|
error: unknown number suffix `u321`
|
||||||
|
failed to parse template source at row 1, column 3 near:
|
||||||
|
"654u321 }}"
|
||||||
|
--> tests/ui/num-suffix.rs:18:10
|
||||||
|
|
|
||||||
|
18 | #[derive(Template)]
|
||||||
|
| ^^^^^^^^
|
||||||
|
|
|
||||||
|
= note: this error originates in the derive macro `Template` (in Nightly builds, run with -Z macro-backtrace for more info)
|
Loading…
x
Reference in New Issue
Block a user