Add AST definitions for struct/variant fields etc.

Fixes #117
This commit is contained in:
Florian Diebold 2018-12-25 13:01:47 +01:00
parent 6fcd38cc81
commit 4cb7b0f2af
3 changed files with 256 additions and 12 deletions

View file

@ -363,3 +363,34 @@ impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> {
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum StructFlavor<'a> {
Tuple(PosFieldList<'a>),
Named(NamedFieldDefList<'a>),
Unit,
}
impl<'a> StructFlavor<'a> {
fn from_node<N: AstNode<'a>>(node: N) -> StructFlavor<'a> {
if let Some(nfdl) = child_opt::<_, NamedFieldDefList>(node) {
StructFlavor::Named(nfdl)
} else if let Some(pfl) = child_opt::<_, PosFieldList>(node) {
StructFlavor::Tuple(pfl)
} else {
StructFlavor::Unit
}
}
}
impl<'a> StructDef<'a> {
pub fn flavor(self) -> StructFlavor<'a> {
StructFlavor::from_node(self)
}
}
impl<'a> EnumVariant<'a> {
pub fn flavor(self) -> StructFlavor<'a> {
StructFlavor::from_node(self)
}
}