revset: flatten symbol rule

This makes it clear that the identifier node is special. For the other
constructs, we don't distinguish between bare symbol and quoted string.
This commit is contained in:
Yuya Nishihara 2024-06-02 22:19:04 +09:00
parent 674d897352
commit 127e4d3455
2 changed files with 26 additions and 30 deletions

View file

@ -18,7 +18,7 @@ identifier_part = @{ (ASCII_ALPHANUMERIC | "_" | "/")+ }
identifier = @{ identifier = @{
identifier_part ~ (("." | "-" | "+") ~ identifier_part)* identifier_part ~ (("." | "-" | "+") ~ identifier_part)*
} }
symbol = { symbol = _{
identifier identifier
| string_literal | string_literal
| raw_string_literal | raw_string_literal

View file

@ -585,32 +585,29 @@ fn parse_primary_node(pair: Pair<Rule>) -> Result<ExpressionNode, RevsetParseErr
let (lhs, op, rhs) = first.into_inner().collect_tuple().unwrap(); let (lhs, op, rhs) = first.into_inner().collect_tuple().unwrap();
assert_eq!(lhs.as_rule(), Rule::identifier); assert_eq!(lhs.as_rule(), Rule::identifier);
assert_eq!(op.as_rule(), Rule::pattern_kind_op); assert_eq!(op.as_rule(), Rule::pattern_kind_op);
assert_eq!(rhs.as_rule(), Rule::symbol);
let kind = lhs.as_str(); let kind = lhs.as_str();
let value = parse_as_string_literal(rhs.into_inner()); let value = parse_as_string_literal(rhs);
ExpressionKind::StringPattern { kind, value } ExpressionKind::StringPattern { kind, value }
} }
// Symbol without "@" may be substituted by aliases. Primary expression including "@" // Identifier without "@" may be substituted by aliases. Primary expression including "@"
// is considered an indecomposable unit, and no alias substitution would be made. // is considered an indecomposable unit, and no alias substitution would be made.
Rule::symbol if pairs.peek().is_none() => { Rule::identifier if pairs.peek().is_none() => ExpressionKind::Identifier(first.as_str()),
let pairs = first.into_inner(); Rule::identifier | Rule::string_literal | Rule::raw_string_literal => {
let first = pairs.peek().unwrap(); let name = parse_as_string_literal(first);
match first.as_rule() { match pairs.next() {
Rule::identifier => ExpressionKind::Identifier(first.as_str()), None => ExpressionKind::String(name),
_ => ExpressionKind::String(parse_as_string_literal(pairs)), Some(op) => {
} assert_eq!(op.as_rule(), Rule::at_op);
} match pairs.next() {
Rule::symbol => { // postfix "<workspace_id>@"
let name = parse_as_string_literal(first.into_inner()); None => ExpressionKind::AtWorkspace(name),
assert_eq!(pairs.next().unwrap().as_rule(), Rule::at_op); // infix "<name>@<remote>"
if let Some(second) = pairs.next() { Some(second) => {
// infix "<name>@<remote>" let remote = parse_as_string_literal(second);
assert_eq!(second.as_rule(), Rule::symbol); ExpressionKind::RemoteSymbol { name, remote }
let remote = parse_as_string_literal(second.into_inner()); }
ExpressionKind::RemoteSymbol { name, remote } }
} else { }
// postfix "<workspace_id>@"
ExpressionKind::AtWorkspace(name)
} }
} }
// nullary "@" // nullary "@"
@ -621,18 +618,17 @@ fn parse_primary_node(pair: Pair<Rule>) -> Result<ExpressionNode, RevsetParseErr
} }
/// Parses part of compound symbol to string. /// Parses part of compound symbol to string.
fn parse_as_string_literal(mut pairs: Pairs<Rule>) -> String { fn parse_as_string_literal(pair: Pair<Rule>) -> String {
let first = pairs.next().unwrap(); match pair.as_rule() {
match first.as_rule() { Rule::identifier => pair.as_str().to_owned(),
Rule::identifier => first.as_str().to_owned(), Rule::string_literal => STRING_LITERAL_PARSER.parse(pair.into_inner()),
Rule::string_literal => STRING_LITERAL_PARSER.parse(first.into_inner()),
Rule::raw_string_literal => { Rule::raw_string_literal => {
let (content,) = first.into_inner().collect_tuple().unwrap(); let (content,) = pair.into_inner().collect_tuple().unwrap();
assert_eq!(content.as_rule(), Rule::raw_string_content); assert_eq!(content.as_rule(), Rule::raw_string_content);
content.as_str().to_owned() content.as_str().to_owned()
} }
_ => { _ => {
panic!("unexpected symbol parse rule: {:?}", first.as_str()); panic!("unexpected string literal rule: {:?}", pair.as_str());
} }
} }
} }