Align grammar for record patterns and literals

The grammar now looks like this

   [name_ref :] pat
This commit is contained in:
Aleksey Kladov 2020-04-11 23:33:17 +02:00
parent 6b49e774e2
commit 5e5eb6a108
18 changed files with 145 additions and 90 deletions

View file

@ -1,6 +1,8 @@
//! Various extension methods to ast Nodes, which are hard to code-generate.
//! Extensions for various expressions live in a sibling `expr_extensions` module.
use std::fmt;
use itertools::Itertools;
use ra_parser::SyntaxKind;
@ -217,6 +219,34 @@ impl ast::RecordField {
}
}
pub enum NameOrNameRef {
Name(ast::Name),
NameRef(ast::NameRef),
}
impl fmt::Display for NameOrNameRef {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
NameOrNameRef::Name(it) => fmt::Display::fmt(it, f),
NameOrNameRef::NameRef(it) => fmt::Display::fmt(it, f),
}
}
}
impl ast::RecordFieldPat {
/// Deals with field init shorthand
pub fn field_name(&self) -> Option<NameOrNameRef> {
if let Some(name_ref) = self.name_ref() {
return Some(NameOrNameRef::NameRef(name_ref));
}
if let Some(ast::Pat::BindPat(pat)) = self.pat() {
let name = pat.name()?;
return Some(NameOrNameRef::Name(name));
}
None
}
}
impl ast::EnumVariant {
pub fn parent_enum(&self) -> ast::EnumDef {
self.syntax()