From fe8c41bbd7943ebb5af0ed065f1e5fc03a041991 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sat, 4 Feb 2023 21:28:14 +0900 Subject: [PATCH] templater: extract primary rule, inline maybe_method instead --- src/template.pest | 14 ++++++++------ src/template_parser.rs | 11 ++++------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/src/template.pest b/src/template.pest index abd231e9f..65d0f3249 100644 --- a/src/template.pest +++ b/src/template.pest @@ -32,14 +32,16 @@ function_arguments = { | whitespace* } -maybe_method = { ("." ~ function)* } - // Note that "x(y)" is a function call but "x (y)" concatenates "x" and "y" +primary = _{ + ("(" ~ template ~ ")") + | function + | identifier + | literal +} + term = { - ("(" ~ template ~ ")") ~ maybe_method - | function ~ maybe_method - | identifier ~ maybe_method - | literal ~ maybe_method + primary ~ ("." ~ function)* } list = _{ diff --git a/src/template_parser.rs b/src/template_parser.rs index 755da8d2a..107d59200 100644 --- a/src/template_parser.rs +++ b/src/template_parser.rs @@ -342,13 +342,12 @@ fn expect_arguments( } fn parse_method_chain<'a, I: 'a>( - pair: Pair, input_property: PropertyAndLabels<'a, I>, + method_pairs: Pairs, parse_keyword: &impl Fn(Pair) -> TemplateParseResult>, ) -> TemplateParseResult> { let PropertyAndLabels(mut property, mut labels) = input_property; - assert_eq!(pair.as_rule(), Rule::maybe_method); - for chain in pair.into_inner() { + for chain in method_pairs { assert_eq!(chain.as_rule(), Rule::function); let (name, args_pair) = { let mut inner = chain.into_inner(); @@ -630,8 +629,6 @@ fn parse_term<'a, C: 'a>( assert_eq!(pair.as_rule(), Rule::term); let mut inner = pair.into_inner(); let expr = inner.next().unwrap(); - let maybe_method = inner.next().unwrap(); - assert!(inner.next().is_none()); let primary = match expr.as_rule() { Rule::literal => { let text = parse_string_literal(expr); @@ -652,10 +649,10 @@ fn parse_term<'a, C: 'a>( }; match primary { Expression::Property(property) => { - parse_method_chain(maybe_method, property, parse_keyword).map(Expression::Property) + parse_method_chain(property, inner, parse_keyword).map(Expression::Property) } Expression::Template(template) => { - if let Some(chain) = maybe_method.into_inner().next() { + if let Some(chain) = inner.next() { assert_eq!(chain.as_rule(), Rule::function); let name = chain.into_inner().next().unwrap(); Err(TemplateParseError::no_such_method("Template", &name))