Skip to content

parser: use built-in whitespace trimming #238

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

Merged
merged 2 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ jobs:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@master
with:
toolchain: "1.71.1"
toolchain: "1.80.0"
- run: cargo check --lib -p rinja --all-features

Audit:
Expand Down
2 changes: 1 addition & 1 deletion rinja/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "MIT OR Apache-2.0"
workspace = ".."
readme = "../README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"

[package.metadata.docs.rs]
features = ["full"]
Expand Down
2 changes: 1 addition & 1 deletion rinja_actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository = "https://github.com/rinja-rs/rinja"
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"

[package.metadata.docs.rs]
all-features = true
Expand Down
2 changes: 1 addition & 1 deletion rinja_axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = ["."]
name = "rinja_axum"
version = "0.3.5"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
description = "Axum integration for Rinja templates"
keywords = ["markup", "template", "jinja2", "html", "axum"]
categories = ["template-engine"]
Expand Down
2 changes: 1 addition & 1 deletion rinja_derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
workspace = ".."
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"

[package.metadata.docs.rs]
all-features = true
Expand Down
2 changes: 1 addition & 1 deletion rinja_derive_standalone/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repository = "https://github.com/rinja-rs/rinja"
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
publish = false

[package.metadata.docs.rs]
Expand Down
2 changes: 1 addition & 1 deletion rinja_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license = "MIT OR Apache-2.0"
workspace = ".."
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"

[package.metadata.docs.rs]
all-features = true
Expand Down
9 changes: 3 additions & 6 deletions rinja_parser/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ use winnow::combinator::{
terminated,
};
use winnow::error::{ErrorKind, ParserError as _};
use winnow::token::{take_till0, take_till1};

use crate::{
CharLit, ErrorContext, Level, Num, ParseResult, PathOrIdentifier, StrLit, WithSpan, char_lit,
filter, identifier, keyword, not_ws, num_lit, path_or_identifier, str_lit, ws,
filter, identifier, keyword, num_lit, path_or_identifier, skip_ws0, skip_ws1, str_lit, ws,
};

macro_rules! expr_prec_layer {
Expand Down Expand Up @@ -181,7 +180,7 @@ impl<'a> Expr<'a> {

fn concat(i: &'a str, level: Level) -> ParseResult<'a, WithSpan<'a, Self>> {
fn concat_expr(i: &str, level: Level) -> ParseResult<'_, Option<WithSpan<'_, Expr<'_>>>> {
let ws1 = |i| opt(take_till1(not_ws)).parse_next(i);
let ws1 = |i| opt(skip_ws1).parse_next(i);
let (j, data) = opt((ws1, '~', ws1, |i| Expr::muldivmod(i, level))).parse_next(i)?;
if let Some((t1, _, t2, expr)) = data {
if t1.is_none() || t2.is_none() {
Expand Down Expand Up @@ -591,8 +590,6 @@ impl<'a> Suffix<'a> {
}

fn r#try(i: &'a str) -> ParseResult<'a, Self> {
preceded(take_till0(not_ws), '?')
.map(|_| Self::Try)
.parse_next(i)
preceded(skip_ws0, '?').map(|_| Self::Try).parse_next(i)
}
}
21 changes: 14 additions & 7 deletions rinja_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use winnow::ascii::escaped;
use winnow::combinator::{alt, cut_err, delimited, fail, not, opt, peek, preceded, repeat};
use winnow::error::{ErrorKind, FromExternalError};
use winnow::stream::AsChar;
use winnow::token::{any, one_of, take_till0, take_till1, take_while};
use winnow::token::{any, one_of, take_till1, take_while};

pub mod expr;
pub use expr::{Expr, Filter};
Expand Down Expand Up @@ -274,18 +274,25 @@ impl<'a> From<ErrorContext<'a>> for winnow::error::ErrMode<ErrorContext<'a>> {
}
}

fn is_ws(c: char) -> bool {
matches!(c, ' ' | '\t' | '\r' | '\n')
#[inline]
fn skip_ws0(i: &str) -> ParseResult<'_, ()> {
Ok((i.trim_ascii_start(), ()))
}

fn not_ws(c: char) -> bool {
!is_ws(c)
#[inline]
fn skip_ws1(i: &str) -> ParseResult<'_, ()> {
let j = i.trim_ascii_start();
if i.len() != j.len() {
Ok((j, ()))
} else {
fail.parse_next(i)
}
}

fn ws<'a, O>(
inner: impl Parser<&'a str, O, ErrorContext<'a>>,
) -> impl Parser<&'a str, O, ErrorContext<'a>> {
delimited(take_till0(not_ws), inner, take_till0(not_ws))
delimited(skip_ws0, inner, skip_ws0)
}

/// Skips input until `end` was found, but does not consume it.
Expand Down Expand Up @@ -892,7 +899,7 @@ fn filter<'a>(
i: &'a str,
level: &mut Level,
) -> ParseResult<'a, (&'a str, Option<Vec<WithSpan<'a, Expr<'a>>>>)> {
let (j, _) = take_till0(not_ws).parse_next(i)?;
let (j, _) = skip_ws0.parse_next(i)?;
let had_spaces = i.len() != j.len();
let (j, _) = ('|', not('|')).parse_next(j)?;

Expand Down
15 changes: 6 additions & 9 deletions rinja_parser/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use winnow::Parser;
use winnow::combinator::{
alt, cut_err, delimited, eof, fail, not, opt, peek, preceded, repeat, separated1, terminated,
};
use winnow::token::{any, tag, take_till0};
use winnow::token::{any, tag};

use crate::memchr_splitter::{Splitter1, Splitter2, Splitter3};
use crate::{
ErrorContext, Expr, Filter, ParseResult, State, Target, WithSpan, filter, identifier, is_ws,
keyword, not_ws, skip_till, str_lit_without_prefix, ws,
ErrorContext, Expr, Filter, ParseResult, State, Target, WithSpan, filter, identifier, keyword,
skip_till, skip_ws0, str_lit_without_prefix, ws,
};

#[derive(Debug, PartialEq)]
Expand Down Expand Up @@ -88,10 +88,7 @@ impl<'a> Node<'a> {
let start = i;
let (i, tag) = preceded(
|i| s.tag_block_start(i),
peek(preceded(
(opt(Whitespace::parse), take_till0(not_ws)),
identifier,
)),
peek(preceded((opt(Whitespace::parse), skip_ws0), identifier)),
)
.parse_next(i)?;

Expand Down Expand Up @@ -1076,9 +1073,9 @@ impl<'a> Lit<'a> {
}

pub(crate) fn split_ws_parts(s: &'a str) -> Self {
let trimmed_start = s.trim_start_matches(is_ws);
let trimmed_start = s.trim_ascii_start();
let len_start = s.len() - trimmed_start.len();
let trimmed = trimmed_start.trim_end_matches(is_ws);
let trimmed = trimmed_start.trim_ascii_end();
Self {
lws: &s[..len_start],
val: trimmed,
Expand Down
2 changes: 1 addition & 1 deletion rinja_rocket/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository = "https://github.com/rinja-rs/rinja"
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"

[package.metadata.docs.rs]
all-features = true
Expand Down
2 changes: 1 addition & 1 deletion rinja_warp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repository = "https://github.com/rinja-rs/rinja"
license = "MIT OR Apache-2.0"
readme = "README.md"
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"

[package.metadata.docs.rs]
all-features = true
Expand Down
2 changes: 1 addition & 1 deletion testing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.3.5"
authors = ["rinja-rs developers"]
workspace = ".."
edition = "2021"
rust-version = "1.71"
rust-version = "1.80"
publish = false

[features]
Expand Down
Loading