| 1 |
use std::collections::HashSet; |
| 2 |
use std::str; |
| 3 |
|
| 4 |
use winnow::Parser; |
| 5 |
use winnow::ascii::digit1; |
| 6 |
use winnow::combinator::{ |
| 7 |
alt, cut_err, fail, not, opt, peek, preceded, repeat, separated, terminated, |
| 8 |
}; |
| 9 |
use winnow::error::ParserError as _; |
| 10 |
use winnow::stream::Stream as _; |
| 11 |
|
| 12 |
use crate::node::CondTest; |
| 13 |
use crate::{ |
| 14 |
CharLit, ErrorContext, Level, Num, ParseErr, ParseResult, PathOrIdentifier, Span, StrLit, |
| 15 |
WithSpan, char_lit, filter, identifier, keyword, num_lit, path_or_identifier, skip_ws0, |
| 16 |
skip_ws1, str_lit, ws, |
| 17 |
}; |
| 18 |
|
| 19 |
macro_rules! expr_prec_layer { |
| 20 |
( $name:ident, $inner:ident, $op:expr ) => { |
| 21 |
fn $name(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 22 |
let mut level_guard = level.guard(); |
| 23 |
let start = *i; |
| 24 |
let mut expr = Self::$inner(i, level)?; |
| 25 |
let mut i_before = *i; |
| 26 |
let mut right = opt((ws($op), |i: &mut _| Self::$inner(i, level))); |
| 27 |
while let Some((op, right)) = right.parse_next(i)? { |
| 28 |
level_guard.nest(i_before)?; |
| 29 |
i_before = *i; |
| 30 |
expr = WithSpan::new(Self::BinOp(op, Box::new(expr), Box::new(right)), start); |
| 31 |
} |
| 32 |
Ok(expr) |
| 33 |
} |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
fn check_expr<'a>( |
| 38 |
expr: &WithSpan<'a, Expr<'a>>, |
| 39 |
allow_underscore: bool, |
| 40 |
) -> Result<(), ParseErr<'a>> { |
| 41 |
match &expr.inner { |
| 42 |
Expr::Var("_") if !allow_underscore => Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 43 |
"reserved keyword `_` cannot be used here", |
| 44 |
expr.span, |
| 45 |
))), |
| 46 |
Expr::IsDefined(var) | Expr::IsNotDefined(var) => { |
| 47 |
if *var == "_" { |
| 48 |
Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 49 |
"reserved keyword `_` cannot be used here", |
| 50 |
expr.span, |
| 51 |
))) |
| 52 |
} else { |
| 53 |
Ok(()) |
| 54 |
} |
| 55 |
} |
| 56 |
Expr::BoolLit(_) |
| 57 |
| Expr::NumLit(_, _) |
| 58 |
| Expr::StrLit(_) |
| 59 |
| Expr::CharLit(_) |
| 60 |
| Expr::Path(_) |
| 61 |
| Expr::Attr(_, _) |
| 62 |
| Expr::Filter(_) |
| 63 |
| Expr::NamedArgument(_, _) |
| 64 |
| Expr::Var(_) |
| 65 |
| Expr::RustMacro(_, _) |
| 66 |
| Expr::Try(_) |
| 67 |
| Expr::FilterSource |
| 68 |
| Expr::LetCond(_) => Ok(()), |
| 69 |
Expr::Array(elems) | Expr::Tuple(elems) | Expr::Concat(elems) => { |
| 70 |
for elem in elems { |
| 71 |
check_expr(elem, allow_underscore)?; |
| 72 |
} |
| 73 |
Ok(()) |
| 74 |
} |
| 75 |
Expr::Index(elem1, elem2) | Expr::BinOp(_, elem1, elem2) => { |
| 76 |
check_expr(elem1, false)?; |
| 77 |
check_expr(elem2, false) |
| 78 |
} |
| 79 |
Expr::Range(_, elem1, elem2) => { |
| 80 |
if let Some(elem1) = elem1 { |
| 81 |
check_expr(elem1, false)?; |
| 82 |
} |
| 83 |
if let Some(elem2) = elem2 { |
| 84 |
check_expr(elem2, false)?; |
| 85 |
} |
| 86 |
Ok(()) |
| 87 |
} |
| 88 |
Expr::As(elem, _) | Expr::Unary(_, elem) | Expr::Group(elem) => check_expr(elem, false), |
| 89 |
Expr::Call { path, args, .. } => { |
| 90 |
check_expr(path, false)?; |
| 91 |
for arg in args { |
| 92 |
check_expr(arg, false)?; |
| 93 |
} |
| 94 |
Ok(()) |
| 95 |
} |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
#[derive(Clone, Debug, PartialEq)] |
| 100 |
pub enum Expr<'a> { |
| 101 |
BoolLit(bool), |
| 102 |
NumLit(&'a str, Num<'a>), |
| 103 |
StrLit(StrLit<'a>), |
| 104 |
CharLit(CharLit<'a>), |
| 105 |
Var(&'a str), |
| 106 |
Path(Vec<&'a str>), |
| 107 |
Array(Vec<WithSpan<'a, Expr<'a>>>), |
| 108 |
Attr(Box<WithSpan<'a, Expr<'a>>>, Attr<'a>), |
| 109 |
Index(Box<WithSpan<'a, Expr<'a>>>, Box<WithSpan<'a, Expr<'a>>>), |
| 110 |
Filter(Filter<'a>), |
| 111 |
As(Box<WithSpan<'a, Expr<'a>>>, &'a str), |
| 112 |
NamedArgument(&'a str, Box<WithSpan<'a, Expr<'a>>>), |
| 113 |
Unary(&'a str, Box<WithSpan<'a, Expr<'a>>>), |
| 114 |
BinOp( |
| 115 |
&'a str, |
| 116 |
Box<WithSpan<'a, Expr<'a>>>, |
| 117 |
Box<WithSpan<'a, Expr<'a>>>, |
| 118 |
), |
| 119 |
Range( |
| 120 |
&'a str, |
| 121 |
Option<Box<WithSpan<'a, Expr<'a>>>>, |
| 122 |
Option<Box<WithSpan<'a, Expr<'a>>>>, |
| 123 |
), |
| 124 |
Group(Box<WithSpan<'a, Expr<'a>>>), |
| 125 |
Tuple(Vec<WithSpan<'a, Expr<'a>>>), |
| 126 |
Call { |
| 127 |
path: Box<WithSpan<'a, Expr<'a>>>, |
| 128 |
args: Vec<WithSpan<'a, Expr<'a>>>, |
| 129 |
generics: Vec<WithSpan<'a, TyGenerics<'a>>>, |
| 130 |
}, |
| 131 |
RustMacro(Vec<&'a str>, &'a str), |
| 132 |
Try(Box<WithSpan<'a, Expr<'a>>>), |
| 133 |
/// This variant should never be used directly. It is created when generating filter blocks. |
| 134 |
FilterSource, |
| 135 |
IsDefined(&'a str), |
| 136 |
IsNotDefined(&'a str), |
| 137 |
Concat(Vec<WithSpan<'a, Expr<'a>>>), |
| 138 |
/// If you have `&& let Some(y)`, this variant handles it. |
| 139 |
LetCond(Box<WithSpan<'a, CondTest<'a>>>), |
| 140 |
} |
| 141 |
|
| 142 |
impl<'a> Expr<'a> { |
| 143 |
pub(super) fn arguments( |
| 144 |
i: &mut &'a str, |
| 145 |
level: Level<'_>, |
| 146 |
is_template_macro: bool, |
| 147 |
) -> ParseResult<'a, Vec<WithSpan<'a, Self>>> { |
| 148 |
let _level_guard = level.nest(i)?; |
| 149 |
let mut named_arguments = HashSet::new(); |
| 150 |
let start = *i; |
| 151 |
|
| 152 |
preceded( |
| 153 |
ws('('), |
| 154 |
cut_err(terminated( |
| 155 |
separated( |
| 156 |
0.., |
| 157 |
ws(move |i: &mut _| { |
| 158 |
// Needed to prevent borrowing it twice between this closure and the one |
| 159 |
// calling `Self::named_arguments`. |
| 160 |
let named_arguments = &mut named_arguments; |
| 161 |
let has_named_arguments = !named_arguments.is_empty(); |
| 162 |
|
| 163 |
let expr = alt(( |
| 164 |
move |i: &mut _| { |
| 165 |
Self::named_argument( |
| 166 |
i, |
| 167 |
level, |
| 168 |
named_arguments, |
| 169 |
start, |
| 170 |
is_template_macro, |
| 171 |
) |
| 172 |
}, |
| 173 |
move |i: &mut _| Self::parse(i, level, false), |
| 174 |
)) |
| 175 |
.parse_next(i)?; |
| 176 |
if has_named_arguments && !matches!(*expr, Self::NamedArgument(_, _)) { |
| 177 |
Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 178 |
"named arguments must always be passed last", |
| 179 |
start, |
| 180 |
))) |
| 181 |
} else { |
| 182 |
Ok(expr) |
| 183 |
} |
| 184 |
}), |
| 185 |
',', |
| 186 |
), |
| 187 |
(opt(ws(',')), ')'), |
| 188 |
)), |
| 189 |
) |
| 190 |
.parse_next(i) |
| 191 |
} |
| 192 |
|
| 193 |
fn named_argument( |
| 194 |
i: &mut &'a str, |
| 195 |
level: Level<'_>, |
| 196 |
named_arguments: &mut HashSet<&'a str>, |
| 197 |
start: &'a str, |
| 198 |
is_template_macro: bool, |
| 199 |
) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 200 |
if !is_template_macro { |
| 201 |
// If this is not a template macro, we don't want to parse named arguments so |
| 202 |
// we instead return an error which will allow to continue the parsing. |
| 203 |
return fail.parse_next(i); |
| 204 |
} |
| 205 |
|
| 206 |
let (argument, _, value) = (identifier, ws('='), move |i: &mut _| { |
| 207 |
Self::parse(i, level, false) |
| 208 |
}) |
| 209 |
.parse_next(i)?; |
| 210 |
if named_arguments.insert(argument) { |
| 211 |
Ok(WithSpan::new( |
| 212 |
Self::NamedArgument(argument, Box::new(value)), |
| 213 |
start, |
| 214 |
)) |
| 215 |
} else { |
| 216 |
Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 217 |
format!("named argument `{argument}` was passed more than once"), |
| 218 |
start, |
| 219 |
))) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
pub(super) fn parse( |
| 224 |
i: &mut &'a str, |
| 225 |
level: Level<'_>, |
| 226 |
allow_underscore: bool, |
| 227 |
) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 228 |
let _level_guard = level.nest(i)?; |
| 229 |
let start = Span::from(*i); |
| 230 |
let range_right = move |i: &mut _| { |
| 231 |
( |
| 232 |
ws(alt(("..=", ".."))), |
| 233 |
opt(move |i: &mut _| Self::or(i, level)), |
| 234 |
) |
| 235 |
.parse_next(i) |
| 236 |
}; |
| 237 |
let expr = alt(( |
| 238 |
range_right.map(move |(op, right)| { |
| 239 |
WithSpan::new(Self::Range(op, None, right.map(Box::new)), start) |
| 240 |
}), |
| 241 |
(move |i: &mut _| Self::or(i, level), opt(range_right)).map(move |(left, right)| { |
| 242 |
match right { |
| 243 |
Some((op, right)) => WithSpan::new( |
| 244 |
Self::Range(op, Some(Box::new(left)), right.map(Box::new)), |
| 245 |
start, |
| 246 |
), |
| 247 |
None => left, |
| 248 |
} |
| 249 |
}), |
| 250 |
)) |
| 251 |
.parse_next(i)?; |
| 252 |
check_expr(&expr, allow_underscore)?; |
| 253 |
Ok(expr) |
| 254 |
} |
| 255 |
|
| 256 |
expr_prec_layer!(or, and, "||"); |
| 257 |
expr_prec_layer!(and, compare, "&&"); |
| 258 |
expr_prec_layer!(compare, bor, alt(("==", "!=", ">=", ">", "<=", "<",))); |
| 259 |
expr_prec_layer!(bor, bxor, "bitor".value("|")); |
| 260 |
expr_prec_layer!(bxor, band, token_xor); |
| 261 |
expr_prec_layer!(band, shifts, token_bitand); |
| 262 |
expr_prec_layer!(shifts, addsub, alt((">>", "<<"))); |
| 263 |
expr_prec_layer!(addsub, concat, alt(("+", "-"))); |
| 264 |
|
| 265 |
fn concat(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 266 |
fn concat_expr<'a>( |
| 267 |
i: &mut &'a str, |
| 268 |
level: Level<'_>, |
| 269 |
) -> ParseResult<'a, Option<WithSpan<'a, Expr<'a>>>> { |
| 270 |
let ws1 = |i: &mut _| opt(skip_ws1).parse_next(i); |
| 271 |
|
| 272 |
let start = *i; |
| 273 |
let data = opt((ws1, '~', ws1, |i: &mut _| Expr::muldivmod(i, level))).parse_next(i)?; |
| 274 |
if let Some((t1, _, t2, expr)) = data { |
| 275 |
if t1.is_none() || t2.is_none() { |
| 276 |
return Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 277 |
"the concat operator `~` must be surrounded by spaces", |
| 278 |
start, |
| 279 |
))); |
| 280 |
} |
| 281 |
Ok(Some(expr)) |
| 282 |
} else { |
| 283 |
Ok(None) |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
let start = *i; |
| 288 |
let expr = Self::muldivmod(i, level)?; |
| 289 |
let expr2 = concat_expr(i, level)?; |
| 290 |
if let Some(expr2) = expr2 { |
| 291 |
let mut exprs = vec![expr, expr2]; |
| 292 |
while let Some(expr) = concat_expr(i, level)? { |
| 293 |
exprs.push(expr); |
| 294 |
} |
| 295 |
Ok(WithSpan::new(Self::Concat(exprs), start)) |
| 296 |
} else { |
| 297 |
Ok(expr) |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
expr_prec_layer!(muldivmod, is_as, alt(("*", "/", "%"))); |
| 302 |
|
| 303 |
fn is_as(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 304 |
let start = *i; |
| 305 |
let lhs = Self::filtered(i, level)?; |
| 306 |
let before_keyword = *i; |
| 307 |
let rhs = opt(ws(identifier)).parse_next(i)?; |
| 308 |
match rhs { |
| 309 |
Some("is") => {} |
| 310 |
Some("as") => { |
| 311 |
let target = opt(identifier).parse_next(i)?; |
| 312 |
let target = target.unwrap_or_default(); |
| 313 |
if crate::PRIMITIVE_TYPES.contains(&target) { |
| 314 |
return Ok(WithSpan::new(Self::As(Box::new(lhs), target), start)); |
| 315 |
} else if target.is_empty() { |
| 316 |
return Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 317 |
"`as` operator expects the name of a primitive type on its right-hand side", |
| 318 |
before_keyword.trim_start(), |
| 319 |
))); |
| 320 |
} else { |
| 321 |
return Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 322 |
format!( |
| 323 |
"`as` operator expects the name of a primitive type on its right-hand \ |
| 324 |
side, found `{target}`" |
| 325 |
), |
| 326 |
before_keyword.trim_start(), |
| 327 |
))); |
| 328 |
} |
| 329 |
} |
| 330 |
_ => { |
| 331 |
*i = before_keyword; |
| 332 |
return Ok(lhs); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
let rhs = opt(terminated(opt(keyword("not")), ws(keyword("defined")))).parse_next(i)?; |
| 337 |
let ctor = match rhs { |
| 338 |
None => { |
| 339 |
return Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 340 |
"expected `defined` or `not defined` after `is`", |
| 341 |
// We use `start` to show the whole `var is` thing instead of the current token. |
| 342 |
start, |
| 343 |
))); |
| 344 |
} |
| 345 |
Some(None) => Self::IsDefined, |
| 346 |
Some(Some(_)) => Self::IsNotDefined, |
| 347 |
}; |
| 348 |
let var_name = match *lhs { |
| 349 |
Self::Var(var_name) => var_name, |
| 350 |
Self::Attr(_, _) => { |
| 351 |
return Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 352 |
"`is defined` operator can only be used on variables, not on their fields", |
| 353 |
start, |
| 354 |
))); |
| 355 |
} |
| 356 |
_ => { |
| 357 |
return Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 358 |
"`is defined` operator can only be used on variables", |
| 359 |
start, |
| 360 |
))); |
| 361 |
} |
| 362 |
}; |
| 363 |
Ok(WithSpan::new(ctor(var_name), start)) |
| 364 |
} |
| 365 |
|
| 366 |
fn filtered(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 367 |
let mut level_guard = level.guard(); |
| 368 |
let start = *i; |
| 369 |
let mut res = Self::prefix(i, level)?; |
| 370 |
while let Some((name, generics, args)) = opt(|i: &mut _| filter(i, level)).parse_next(i)? { |
| 371 |
level_guard.nest(i)?; |
| 372 |
|
| 373 |
let mut arguments = args.unwrap_or_else(|| Vec::with_capacity(1)); |
| 374 |
arguments.insert(0, res); |
| 375 |
|
| 376 |
res = WithSpan::new( |
| 377 |
Self::Filter(Filter { |
| 378 |
name, |
| 379 |
arguments, |
| 380 |
generics, |
| 381 |
}), |
| 382 |
start, |
| 383 |
); |
| 384 |
} |
| 385 |
Ok(res) |
| 386 |
} |
| 387 |
|
| 388 |
fn prefix(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 389 |
let start = *i; |
| 390 |
|
| 391 |
// This is a rare place where we create recursion in the parsed AST |
| 392 |
// without recursing the parser call stack. However, this can lead |
| 393 |
// to stack overflows in drop glue when the AST is very deep. |
| 394 |
let mut level_guard = level.guard(); |
| 395 |
let mut ops = vec![]; |
| 396 |
let mut i_before = *i; |
| 397 |
while let Some(op) = opt(ws(alt(("!", "-", "*", "&")))).parse_next(i)? { |
| 398 |
level_guard.nest(i_before)?; |
| 399 |
ops.push(op); |
| 400 |
i_before = *i; |
| 401 |
} |
| 402 |
|
| 403 |
let mut expr = Suffix::parse(i, level)?; |
| 404 |
for op in ops.iter().rev() { |
| 405 |
expr = WithSpan::new(Self::Unary(op, Box::new(expr)), start); |
| 406 |
} |
| 407 |
|
| 408 |
Ok(expr) |
| 409 |
} |
| 410 |
|
| 411 |
fn single(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 412 |
alt(( |
| 413 |
Self::num, |
| 414 |
Self::str, |
| 415 |
Self::char, |
| 416 |
Self::path_var_bool, |
| 417 |
move |i: &mut _| Self::array(i, level), |
| 418 |
move |i: &mut _| Self::group(i, level), |
| 419 |
)) |
| 420 |
.parse_next(i) |
| 421 |
} |
| 422 |
|
| 423 |
fn group(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 424 |
let start = *i; |
| 425 |
let expr = preceded(ws('('), opt(|i: &mut _| Self::parse(i, level, true))).parse_next(i)?; |
| 426 |
let Some(expr) = expr else { |
| 427 |
let _ = ')'.parse_next(i)?; |
| 428 |
return Ok(WithSpan::new(Self::Tuple(vec![]), start)); |
| 429 |
}; |
| 430 |
|
| 431 |
let comma = ws(opt(peek(','))).parse_next(i)?; |
| 432 |
if comma.is_none() { |
| 433 |
let _ = ')'.parse_next(i)?; |
| 434 |
return Ok(WithSpan::new(Self::Group(Box::new(expr)), start)); |
| 435 |
} |
| 436 |
|
| 437 |
let mut exprs = vec![expr]; |
| 438 |
repeat( |
| 439 |
0.., |
| 440 |
preceded(',', ws(|i: &mut _| Self::parse(i, level, true))), |
| 441 |
) |
| 442 |
.fold( |
| 443 |
|| (), |
| 444 |
|(), expr| { |
| 445 |
exprs.push(expr); |
| 446 |
}, |
| 447 |
) |
| 448 |
.parse_next(i)?; |
| 449 |
let _ = (ws(opt(',')), ')').parse_next(i)?; |
| 450 |
Ok(WithSpan::new(Self::Tuple(exprs), start)) |
| 451 |
} |
| 452 |
|
| 453 |
fn array(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 454 |
let start = *i; |
| 455 |
let array = preceded( |
| 456 |
ws('['), |
| 457 |
cut_err(terminated( |
| 458 |
opt(terminated( |
| 459 |
separated(1.., ws(move |i: &mut _| Self::parse(i, level, true)), ','), |
| 460 |
ws(opt(',')), |
| 461 |
)), |
| 462 |
']', |
| 463 |
)), |
| 464 |
) |
| 465 |
.parse_next(i)?; |
| 466 |
Ok(WithSpan::new(Self::Array(array.unwrap_or_default()), start)) |
| 467 |
} |
| 468 |
|
| 469 |
fn path_var_bool(i: &mut &'a str) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 470 |
let start = *i; |
| 471 |
path_or_identifier |
| 472 |
.map(|v| match v { |
| 473 |
PathOrIdentifier::Path(v) => Self::Path(v), |
| 474 |
PathOrIdentifier::Identifier("true") => Self::BoolLit(true), |
| 475 |
PathOrIdentifier::Identifier("false") => Self::BoolLit(false), |
| 476 |
PathOrIdentifier::Identifier(v) => Self::Var(v), |
| 477 |
}) |
| 478 |
.parse_next(i) |
| 479 |
.map(|expr| WithSpan::new(expr, start)) |
| 480 |
} |
| 481 |
|
| 482 |
fn str(i: &mut &'a str) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 483 |
let start = *i; |
| 484 |
str_lit |
| 485 |
.map(|i| WithSpan::new(Self::StrLit(i), start)) |
| 486 |
.parse_next(i) |
| 487 |
} |
| 488 |
|
| 489 |
fn num(i: &mut &'a str) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 490 |
let start = *i; |
| 491 |
let (num, full) = num_lit.with_taken().parse_next(i)?; |
| 492 |
Ok(WithSpan::new(Expr::NumLit(full, num), start)) |
| 493 |
} |
| 494 |
|
| 495 |
fn char(i: &mut &'a str) -> ParseResult<'a, WithSpan<'a, Self>> { |
| 496 |
let start = *i; |
| 497 |
char_lit |
| 498 |
.map(|i| WithSpan::new(Self::CharLit(i), start)) |
| 499 |
.parse_next(i) |
| 500 |
} |
| 501 |
|
| 502 |
#[must_use] |
| 503 |
pub fn contains_bool_lit_or_is_defined(&self) -> bool { |
| 504 |
match self { |
| 505 |
Self::BoolLit(_) | Self::IsDefined(_) | Self::IsNotDefined(_) => true, |
| 506 |
Self::Unary(_, expr) | Self::Group(expr) => expr.contains_bool_lit_or_is_defined(), |
| 507 |
Self::BinOp("&&" | "||", left, right) => { |
| 508 |
left.contains_bool_lit_or_is_defined() || right.contains_bool_lit_or_is_defined() |
| 509 |
} |
| 510 |
Self::NumLit(_, _) |
| 511 |
| Self::StrLit(_) |
| 512 |
| Self::CharLit(_) |
| 513 |
| Self::Var(_) |
| 514 |
| Self::FilterSource |
| 515 |
| Self::RustMacro(_, _) |
| 516 |
| Self::As(_, _) |
| 517 |
| Self::Call { .. } |
| 518 |
| Self::Range(_, _, _) |
| 519 |
| Self::Try(_) |
| 520 |
| Self::NamedArgument(_, _) |
| 521 |
| Self::Filter(_) |
| 522 |
| Self::Attr(_, _) |
| 523 |
| Self::Index(_, _) |
| 524 |
| Self::Tuple(_) |
| 525 |
| Self::Array(_) |
| 526 |
| Self::BinOp(_, _, _) |
| 527 |
| Self::Path(_) |
| 528 |
| Self::Concat(_) |
| 529 |
| Self::LetCond(_) => false, |
| 530 |
} |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
fn token_xor<'a>(i: &mut &'a str) -> ParseResult<'a> { |
| 535 |
let good = alt((keyword("xor").value(true), '^'.value(false))).parse_next(i)?; |
| 536 |
if good { |
| 537 |
Ok("^") |
| 538 |
} else { |
| 539 |
Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 540 |
"the binary XOR operator is called `xor` in askama", |
| 541 |
*i, |
| 542 |
))) |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
fn token_bitand<'a>(i: &mut &'a str) -> ParseResult<'a> { |
| 547 |
let good = alt((keyword("bitand").value(true), ('&', not('&')).value(false))).parse_next(i)?; |
| 548 |
if good { |
| 549 |
Ok("&") |
| 550 |
} else { |
| 551 |
Err(winnow::error::ErrMode::Cut(ErrorContext::new( |
| 552 |
"the binary AND operator is called `bitand` in askama", |
| 553 |
*i, |
| 554 |
))) |
| 555 |
} |
| 556 |
} |
| 557 |
|
| 558 |
#[derive(Clone, Debug, PartialEq)] |
| 559 |
pub struct Filter<'a> { |
| 560 |
pub name: &'a str, |
| 561 |
pub arguments: Vec<WithSpan<'a, Expr<'a>>>, |
| 562 |
pub generics: Vec<WithSpan<'a, TyGenerics<'a>>>, |
| 563 |
} |
| 564 |
|
| 565 |
#[derive(Clone, Debug, PartialEq)] |
| 566 |
pub struct Attr<'a> { |
| 567 |
pub name: &'a str, |
| 568 |
pub generics: Vec<WithSpan<'a, TyGenerics<'a>>>, |
| 569 |
} |
| 570 |
|
| 571 |
enum Suffix<'a> { |
| 572 |
Attr(Attr<'a>), |
| 573 |
Index(WithSpan<'a, Expr<'a>>), |
| 574 |
Call { |
| 575 |
args: Vec<WithSpan<'a, Expr<'a>>>, |
| 576 |
generics: Vec<WithSpan<'a, TyGenerics<'a>>>, |
| 577 |
}, |
| 578 |
// The value is the arguments of the macro call. |
| 579 |
MacroCall(&'a str), |
| 580 |
Try, |
| 581 |
} |
| 582 |
|
| 583 |
impl<'a> Suffix<'a> { |
| 584 |
fn parse(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, WithSpan<'a, Expr<'a>>> { |
| 585 |
let mut level_guard = level.guard(); |
| 586 |
let mut expr = Expr::single(i, level)?; |
| 587 |
let mut right = opt(alt(( |
| 588 |
|i: &mut _| Self::attr(i, level), |
| 589 |
|i: &mut _| Self::index(i, level), |
| 590 |
|i: &mut _| Self::call(i, level), |
| 591 |
Self::r#try, |
| 592 |
Self::r#macro, |
| 593 |
))); |
| 594 |
loop { |
| 595 |
let before_suffix = *i; |
| 596 |
let suffix = right.parse_next(i)?; |
| 597 |
let Some(suffix) = suffix else { |
| 598 |
break; |
| 599 |
}; |
| 600 |
level_guard.nest(before_suffix)?; |
| 601 |
|
| 602 |
match suffix { |
| 603 |
Self::Attr(attr) => { |
| 604 |
expr = WithSpan::new(Expr::Attr(expr.into(), attr), before_suffix) |
| 605 |
} |
| 606 |
Self::Index(index) => { |
| 607 |
expr = WithSpan::new(Expr::Index(expr.into(), index.into()), before_suffix); |
| 608 |
} |
| 609 |
Self::Call { args, generics } => { |
| 610 |
expr = WithSpan::new( |
| 611 |
Expr::Call { |
| 612 |
path: expr.into(), |
| 613 |
args, |
| 614 |
generics, |
| 615 |
}, |
| 616 |
before_suffix, |
| 617 |
) |
| 618 |
} |
| 619 |
Self::Try => expr = WithSpan::new(Expr::Try(expr.into()), before_suffix), |
| 620 |
Self::MacroCall(args) => match expr.inner { |
| 621 |
Expr::Path(path) => { |
| 622 |
expr = WithSpan::new(Expr::RustMacro(path, args), before_suffix) |
| 623 |
} |
| 624 |
Expr::Var(name) => { |
| 625 |
expr = WithSpan::new(Expr::RustMacro(vec![name], args), before_suffix) |
| 626 |
} |
| 627 |
_ => { |
| 628 |
return Err(winnow::error::ErrMode::from_input(&before_suffix).cut()); |
| 629 |
} |
| 630 |
}, |
| 631 |
} |
| 632 |
} |
| 633 |
Ok(expr) |
| 634 |
} |
| 635 |
|
| 636 |
fn r#macro(i: &mut &'a str) -> ParseResult<'a, Self> { |
| 637 |
fn nested_parenthesis<'a>(input: &mut &'a str) -> ParseResult<'a, ()> { |
| 638 |
let mut nested = 0; |
| 639 |
let mut last = 0; |
| 640 |
let mut in_str = false; |
| 641 |
let mut escaped = false; |
| 642 |
|
| 643 |
for (i, c) in input.char_indices() { |
| 644 |
if !(c == '(' || c == ')') || !in_str { |
| 645 |
match c { |
| 646 |
'(' => nested += 1, |
| 647 |
')' => { |
| 648 |
if nested == 0 { |
| 649 |
last = i; |
| 650 |
break; |
| 651 |
} |
| 652 |
nested -= 1; |
| 653 |
} |
| 654 |
'"' => { |
| 655 |
if in_str { |
| 656 |
if !escaped { |
| 657 |
in_str = false; |
| 658 |
} |
| 659 |
} else { |
| 660 |
in_str = true; |
| 661 |
} |
| 662 |
} |
| 663 |
'\\' => { |
| 664 |
escaped = !escaped; |
| 665 |
} |
| 666 |
_ => (), |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
if escaped && c != '\\' { |
| 671 |
escaped = false; |
| 672 |
} |
| 673 |
} |
| 674 |
|
| 675 |
if nested == 0 { |
| 676 |
let _ = input.next_slice(last); |
| 677 |
Ok(()) |
| 678 |
} else { |
| 679 |
fail.parse_next(input) |
| 680 |
} |
| 681 |
} |
| 682 |
|
| 683 |
preceded( |
| 684 |
(ws('!'), '('), |
| 685 |
cut_err(terminated( |
| 686 |
nested_parenthesis.take().map(Self::MacroCall), |
| 687 |
')', |
| 688 |
)), |
| 689 |
) |
| 690 |
.parse_next(i) |
| 691 |
} |
| 692 |
|
| 693 |
fn attr(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, Self> { |
| 694 |
preceded( |
| 695 |
ws(('.', not('.'))), |
| 696 |
cut_err(( |
| 697 |
alt((digit1, identifier)), |
| 698 |
opt(|i: &mut _| call_generics(i, level)), |
| 699 |
)), |
| 700 |
) |
| 701 |
.map(|(name, generics)| { |
| 702 |
Self::Attr(Attr { |
| 703 |
name, |
| 704 |
generics: generics.unwrap_or_default(), |
| 705 |
}) |
| 706 |
}) |
| 707 |
.parse_next(i) |
| 708 |
} |
| 709 |
|
| 710 |
fn index(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, Self> { |
| 711 |
preceded( |
| 712 |
ws('['), |
| 713 |
cut_err(terminated( |
| 714 |
ws(move |i: &mut _| Expr::parse(i, level, true)), |
| 715 |
']', |
| 716 |
)), |
| 717 |
) |
| 718 |
.map(Self::Index) |
| 719 |
.parse_next(i) |
| 720 |
} |
| 721 |
|
| 722 |
fn call(i: &mut &'a str, level: Level<'_>) -> ParseResult<'a, Self> { |
| 723 |
(opt(|i: &mut _| call_generics(i, level)), |i: &mut _| { |
| 724 |
Expr::arguments(i, level, false) |
| 725 |
}) |
| 726 |
.map(|(generics, args)| Self::Call { |
| 727 |
args, |
| 728 |
generics: generics.unwrap_or_default(), |
| 729 |
}) |
| 730 |
.parse_next(i) |
| 731 |
} |
| 732 |
|
| 733 |
fn r#try(i: &mut &'a str) -> ParseResult<'a, Self> { |
| 734 |
preceded(skip_ws0, '?').map(|_| Self::Try).parse_next(i) |
| 735 |
} |
| 736 |
} |
| 737 |
|
| 738 |
#[derive(Clone, Debug, PartialEq)] |
| 739 |
pub struct TyGenerics<'a> { |
| 740 |
pub refs: usize, |
| 741 |
pub path: Vec<&'a str>, |
| 742 |
pub args: Vec<WithSpan<'a, TyGenerics<'a>>>, |
| 743 |
} |
| 744 |
|
| 745 |
impl<'i> TyGenerics<'i> { |
| 746 |
fn parse(i: &mut &'i str, level: Level<'_>) -> ParseResult<'i, WithSpan<'i, Self>> { |
| 747 |
let start = *i; |
| 748 |
( |
| 749 |
repeat(0.., ws('&')), |
| 750 |
separated(1.., ws(identifier), "::"), |
| 751 |
opt(|i: &mut _| Self::args(i, level)).map(|generics| generics.unwrap_or_default()), |
| 752 |
) |
| 753 |
.map(|(refs, path, args)| WithSpan::new(TyGenerics { refs, path, args }, start)) |
| 754 |
.parse_next(i) |
| 755 |
} |
| 756 |
|
| 757 |
fn args( |
| 758 |
i: &mut &'i str, |
| 759 |
level: Level<'_>, |
| 760 |
) -> ParseResult<'i, Vec<WithSpan<'i, TyGenerics<'i>>>> { |
| 761 |
ws('<').parse_next(i)?; |
| 762 |
let _level_guard = level.nest(i)?; |
| 763 |
cut_err(terminated( |
| 764 |
terminated( |
| 765 |
separated(0.., |i: &mut _| TyGenerics::parse(i, level), ws(',')), |
| 766 |
ws(opt(',')), |
| 767 |
), |
| 768 |
'>', |
| 769 |
)) |
| 770 |
.parse_next(i) |
| 771 |
} |
| 772 |
} |
| 773 |
|
| 774 |
pub(crate) fn call_generics<'i>( |
| 775 |
i: &mut &'i str, |
| 776 |
level: Level<'_>, |
| 777 |
) -> ParseResult<'i, Vec<WithSpan<'i, TyGenerics<'i>>>> { |
| 778 |
preceded(ws("::"), cut_err(|i: &mut _| TyGenerics::args(i, level))).parse_next(i) |
| 779 |
} |