Make records grammar more orthogonal

We used

  name [: expr]

grammar before, now it is

  [name :] expr

which makes things simpler
This commit is contained in:
Aleksey Kladov 2020-04-11 16:42:24 +02:00
parent e7a68c8f55
commit 7a39bc3ba2
13 changed files with 142 additions and 68 deletions

View file

@ -187,6 +187,38 @@ impl ast::StructDef {
}
}
impl ast::RecordField {
pub fn for_field_name(field_name: &ast::NameRef) -> Option<ast::RecordField> {
eprintln!("field_name = {}", field_name);
dbg!(field_name.syntax().ancestors().nth(6));
let candidate =
field_name.syntax().parent().and_then(ast::RecordField::cast).or_else(|| {
field_name.syntax().ancestors().nth(4).and_then(ast::RecordField::cast)
})?;
if candidate.field_name().as_ref() == Some(field_name) {
Some(candidate)
} else {
None
}
}
/// Deals with field init shorthand
pub fn field_name(&self) -> Option<ast::NameRef> {
if let Some(name_ref) = self.name_ref() {
return Some(name_ref);
}
if let Some(ast::Expr::PathExpr(expr)) = self.expr() {
let path = expr.path()?;
let segment = path.segment()?;
let name_ref = segment.name_ref()?;
if path.qualifier().is_none() {
return Some(name_ref);
}
}
None
}
}
impl ast::EnumVariant {
pub fn parent_enum(&self) -> ast::EnumDef {
self.syntax()