From 674d897352fe598514e16a0ebd526bd1f45017fb Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 2 Jun 2024 22:03:09 +0900 Subject: [PATCH] revset: inline trivial parsing functions to eliminate obvious assertions --- lib/src/revset_parser.rs | 40 +++++++++++++++++----------------------- 1 file changed, 17 insertions(+), 23 deletions(-) diff --git a/lib/src/revset_parser.rs b/lib/src/revset_parser.rs index 2a64dab44..0cc4164b9 100644 --- a/lib/src/revset_parser.rs +++ b/lib/src/revset_parser.rs @@ -581,10 +581,25 @@ fn parse_primary_node(pair: Pair) -> Result parse_string_pattern(first), + Rule::string_pattern => { + let (lhs, op, rhs) = first.into_inner().collect_tuple().unwrap(); + assert_eq!(lhs.as_rule(), Rule::identifier); + assert_eq!(op.as_rule(), Rule::pattern_kind_op); + assert_eq!(rhs.as_rule(), Rule::symbol); + let kind = lhs.as_str(); + let value = parse_as_string_literal(rhs.into_inner()); + ExpressionKind::StringPattern { kind, value } + } // Symbol without "@" may be substituted by aliases. Primary expression including "@" // is considered an indecomposable unit, and no alias substitution would be made. - Rule::symbol if pairs.peek().is_none() => parse_symbol(first.into_inner()), + Rule::symbol if pairs.peek().is_none() => { + let pairs = first.into_inner(); + let first = pairs.peek().unwrap(); + match first.as_rule() { + Rule::identifier => ExpressionKind::Identifier(first.as_str()), + _ => ExpressionKind::String(parse_as_string_literal(pairs)), + } + } Rule::symbol => { let name = parse_as_string_literal(first.into_inner()); assert_eq!(pairs.next().unwrap().as_rule(), Rule::at_op); @@ -605,27 +620,6 @@ fn parse_primary_node(pair: Pair) -> Result) -> ExpressionKind { - assert_eq!(pair.as_rule(), Rule::string_pattern); - let (lhs, op, rhs) = pair.into_inner().collect_tuple().unwrap(); - assert_eq!(lhs.as_rule(), Rule::identifier); - assert_eq!(op.as_rule(), Rule::pattern_kind_op); - assert_eq!(rhs.as_rule(), Rule::symbol); - let kind = lhs.as_str(); - let value = parse_as_string_literal(rhs.into_inner()); - ExpressionKind::StringPattern { kind, value } -} - -// TODO: inline -fn parse_symbol(pairs: Pairs) -> ExpressionKind { - let first = pairs.peek().unwrap(); - match first.as_rule() { - Rule::identifier => ExpressionKind::Identifier(first.as_str()), - _ => ExpressionKind::String(parse_as_string_literal(pairs)), - } -} - /// Parses part of compound symbol to string. fn parse_as_string_literal(mut pairs: Pairs) -> String { let first = pairs.next().unwrap();