Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better support for rust-like number literals #908

Merged
merged 6 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 84 additions & 5 deletions askama_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ use std::{fmt, str};

use nom::branch::alt;
use nom::bytes::complete::{escaped, is_not, tag, take_till};
use nom::character::complete::char;
use nom::character::complete::{anychar, digit1};
use nom::character::complete::{anychar, char, one_of, satisfy};
use nom::combinator::{cut, eof, map, opt, recognize};
use nom::error::{Error, ErrorKind, FromExternalError};
use nom::multi::many1;
use nom::multi::{many0_count, many1};
use nom::sequence::{delimited, pair, preceded, terminated, tuple};
use nom::{error_position, AsChar, InputTakeAtPosition};

Expand Down Expand Up @@ -252,8 +251,88 @@ fn bool_lit(i: &str) -> ParseResult<'_> {
fn num_lit(i: &str) -> ParseResult<'_> {
recognize(tuple((
opt(char('-')),
digit1,
opt(pair(char('.'), digit1)),
// digit1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like there's a bunch of duplication here, which I'd like to avoid. I'm guessing this will require some named parsers, which would probably aid in making this code easier to understand. Also please remove the commented out version of the old code.

// opt(pair(char('.'), digit1)),
alt((
recognize(tuple((
char('0'),
alt((
recognize(tuple((
char('b'),
many0_count(char('_')),
one_of("01"),
many0_count(one_of("01_")),
))),
recognize(tuple((
char('o'),
many0_count(char('_')),
satisfy(|ch| matches!(ch, '0'..='7')),
many0_count(satisfy(|ch| matches!(ch, '_' | '0'..='7'))),
))),
recognize(tuple((
char('x'),
many0_count(char('_')),
satisfy(|ch| ch.is_ascii_hexdigit()),
many0_count(satisfy(
|ch| matches!(ch, '_' | '0'..='9' | 'a'..='f' | 'A'..='F'),
)),
))),
)),
opt(alt((
tag("i8"),
tag("i16"),
tag("i32"),
tag("i64"),
tag("i128"),
tag("isize"),
tag("u8"),
tag("u16"),
tag("u32"),
tag("u64"),
tag("u128"),
tag("usize"),
))),
))),
recognize(tuple((
satisfy(|ch| ch.is_ascii_digit()),
many0_count(satisfy(|ch| matches!(ch, '_' | '0'..='9'))),
opt(alt((
tag("i8"),
tag("i16"),
tag("i32"),
tag("i64"),
tag("i128"),
tag("isize"),
tag("u8"),
tag("u16"),
tag("u32"),
tag("u64"),
tag("u128"),
tag("usize"),
tag("f32"),
tag("f64"),
recognize(tuple((
opt(tuple((
char('.'),
satisfy(|ch| ch.is_ascii_digit()),
many0_count(satisfy(|ch| matches!(ch, '_' | '0'..='9'))),
))),
one_of("eE"),
opt(one_of("+-")),
many0_count(char('_')),
satisfy(|ch| ch.is_ascii_digit()),
many0_count(satisfy(|ch| matches!(ch, '_' | '0'..='9'))),
opt(alt((tag("f32"), tag("f64")))),
))),
recognize(tuple((
char('.'),
satisfy(|ch| ch.is_ascii_digit()),
many0_count(satisfy(|ch| matches!(ch, '_' | '0'..='9'))),
opt(alt((tag("f32"), tag("f64")))),
))),
))),
))),
)),
)))(i)
}

Expand Down
11 changes: 11 additions & 0 deletions testing/templates/num-literals.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% let plain_int = 9__0__ -%}
{% let neg_int = -9__0__isize -%}
{% let suffix_int = 9__0__i32 -%}
{% let bin_int = 0b__1__0__ -%}
{% let oct_int = 0o__7__0__ -%}
{% let hex_int = 0x__f__0__ -%}
{% let plain_float = 1__0__.5__0__ -%}
{% let suffix_float = 1__0__.5__0__f32 -%}
{% let exp_float = 1__0__e+__1__0__f32 -%}
{% let dotexp_float = 1__0__.5__0__e+__1__0__f32 -%}
[{{ plain_int }}, {{ neg_int }}, {{ suffix_int }}, {{ bin_int }}, {{ oct_int }}, {{ hex_int }}, {{ plain_float }}, {{ suffix_float }}, {{ exp_float }}, {{ dotexp_float }}]
23 changes: 23 additions & 0 deletions testing/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,26 @@ fn test_define_string_var() {
let template = DefineStringVar;
assert_eq!(template.render().unwrap(), "");
}

#[derive(askama::Template)]
#[template(source = "{% let x = 4.5 %}{{ x }}", ext = "html")]
struct SimpleFloat;

#[test]
fn test_simple_float() {
let template = SimpleFloat;
assert_eq!(template.render().unwrap(), "4.5");
}

#[derive(askama::Template)]
#[template(path = "num-literals.html")]
struct NumLiterals;

#[test]
fn test_num_literals() {
let template = NumLiterals;
assert_eq!(
template.render().unwrap(),
"[90, -90, 90, 2, 56, 240, 10.5, 10.5, 100000000000, 105000000000]",
);
}