aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax
diff options
context:
space:
mode:
authorFlorian Diebold <[email protected]>2018-12-25 12:01:47 +0000
committerFlorian Diebold <[email protected]>2018-12-25 14:16:42 +0000
commit4cb7b0f2af9787abe1c73fc3922e9b426a96e0ef (patch)
tree80e0ec0685f17b6470a7ff3b1f47abd1f9b6b9ab /crates/ra_syntax
parent6fcd38cc81bdcc9921da767872dfce65ee7d2d27 (diff)
Add AST definitions for struct/variant fields etc.
Fixes #117
Diffstat (limited to 'crates/ra_syntax')
-rw-r--r--crates/ra_syntax/src/ast.rs31
-rw-r--r--crates/ra_syntax/src/ast/generated.rs225
-rw-r--r--crates/ra_syntax/src/grammar.ron12
3 files changed, 256 insertions, 12 deletions
diff --git a/crates/ra_syntax/src/ast.rs b/crates/ra_syntax/src/ast.rs
index f12479fb4..5dbf9b221 100644
--- a/crates/ra_syntax/src/ast.rs
+++ b/crates/ra_syntax/src/ast.rs
@@ -363,3 +363,34 @@ impl<'a, N: AstNode<'a>> Iterator for AstChildren<'a, N> {
363 } 363 }
364 } 364 }
365} 365}
366
367#[derive(Debug, Clone, PartialEq, Eq)]
368pub enum StructFlavor<'a> {
369 Tuple(PosFieldList<'a>),
370 Named(NamedFieldDefList<'a>),
371 Unit,
372}
373
374impl<'a> StructFlavor<'a> {
375 fn from_node<N: AstNode<'a>>(node: N) -> StructFlavor<'a> {
376 if let Some(nfdl) = child_opt::<_, NamedFieldDefList>(node) {
377 StructFlavor::Named(nfdl)
378 } else if let Some(pfl) = child_opt::<_, PosFieldList>(node) {
379 StructFlavor::Tuple(pfl)
380 } else {
381 StructFlavor::Unit
382 }
383 }
384}
385
386impl<'a> StructDef<'a> {
387 pub fn flavor(self) -> StructFlavor<'a> {
388 StructFlavor::from_node(self)
389 }
390}
391
392impl<'a> EnumVariant<'a> {
393 pub fn flavor(self) -> StructFlavor<'a> {
394 StructFlavor::from_node(self)
395 }
396}
diff --git a/crates/ra_syntax/src/ast/generated.rs b/crates/ra_syntax/src/ast/generated.rs
index 334da67ef..35a9770a6 100644
--- a/crates/ra_syntax/src/ast/generated.rs
+++ b/crates/ra_syntax/src/ast/generated.rs
@@ -806,7 +806,94 @@ impl<'a> ast::NameOwner<'a> for EnumDef<'a> {}
806impl<'a> ast::TypeParamsOwner<'a> for EnumDef<'a> {} 806impl<'a> ast::TypeParamsOwner<'a> for EnumDef<'a> {}
807impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {} 807impl<'a> ast::AttrsOwner<'a> for EnumDef<'a> {}
808impl<'a> ast::DocCommentsOwner<'a> for EnumDef<'a> {} 808impl<'a> ast::DocCommentsOwner<'a> for EnumDef<'a> {}
809impl<'a> EnumDef<'a> {} 809impl<'a> EnumDef<'a> {
810 pub fn variant_list(self) -> Option<EnumVariantList<'a>> {
811 super::child_opt(self)
812 }
813}
814
815// EnumVariant
816#[derive(Debug, Clone, Copy,)]
817pub struct EnumVariantNode<R: TreeRoot<RaTypes> = OwnedRoot> {
818 pub(crate) syntax: SyntaxNode<R>,
819}
820pub type EnumVariant<'a> = EnumVariantNode<RefRoot<'a>>;
821
822impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<EnumVariantNode<R1>> for EnumVariantNode<R2> {
823 fn eq(&self, other: &EnumVariantNode<R1>) -> bool { self.syntax == other.syntax }
824}
825impl<R: TreeRoot<RaTypes>> Eq for EnumVariantNode<R> {}
826impl<R: TreeRoot<RaTypes>> Hash for EnumVariantNode<R> {
827 fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
828}
829
830impl<'a> AstNode<'a> for EnumVariant<'a> {
831 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
832 match syntax.kind() {
833 ENUM_VARIANT => Some(EnumVariant { syntax }),
834 _ => None,
835 }
836 }
837 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
838}
839
840impl<R: TreeRoot<RaTypes>> EnumVariantNode<R> {
841 pub fn borrowed(&self) -> EnumVariant {
842 EnumVariantNode { syntax: self.syntax.borrowed() }
843 }
844 pub fn owned(&self) -> EnumVariantNode {
845 EnumVariantNode { syntax: self.syntax.owned() }
846 }
847}
848
849
850impl<'a> ast::NameOwner<'a> for EnumVariant<'a> {}
851impl<'a> EnumVariant<'a> {
852 pub fn expr(self) -> Option<Expr<'a>> {
853 super::child_opt(self)
854 }
855}
856
857// EnumVariantList
858#[derive(Debug, Clone, Copy,)]
859pub struct EnumVariantListNode<R: TreeRoot<RaTypes> = OwnedRoot> {
860 pub(crate) syntax: SyntaxNode<R>,
861}
862pub type EnumVariantList<'a> = EnumVariantListNode<RefRoot<'a>>;
863
864impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<EnumVariantListNode<R1>> for EnumVariantListNode<R2> {
865 fn eq(&self, other: &EnumVariantListNode<R1>) -> bool { self.syntax == other.syntax }
866}
867impl<R: TreeRoot<RaTypes>> Eq for EnumVariantListNode<R> {}
868impl<R: TreeRoot<RaTypes>> Hash for EnumVariantListNode<R> {
869 fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
870}
871
872impl<'a> AstNode<'a> for EnumVariantList<'a> {
873 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
874 match syntax.kind() {
875 ENUM_VARIANT_LIST => Some(EnumVariantList { syntax }),
876 _ => None,
877 }
878 }
879 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
880}
881
882impl<R: TreeRoot<RaTypes>> EnumVariantListNode<R> {
883 pub fn borrowed(&self) -> EnumVariantList {
884 EnumVariantListNode { syntax: self.syntax.borrowed() }
885 }
886 pub fn owned(&self) -> EnumVariantListNode {
887 EnumVariantListNode { syntax: self.syntax.owned() }
888 }
889}
890
891
892impl<'a> EnumVariantList<'a> {
893 pub fn variants(self) -> impl Iterator<Item = EnumVariant<'a>> + 'a {
894 super::children(self)
895 }
896}
810 897
811// Expr 898// Expr
812#[derive(Debug, Clone, Copy, PartialEq, Eq)] 899#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -2189,7 +2276,52 @@ impl<R: TreeRoot<RaTypes>> NamedFieldDefNode<R> {
2189 2276
2190impl<'a> ast::NameOwner<'a> for NamedFieldDef<'a> {} 2277impl<'a> ast::NameOwner<'a> for NamedFieldDef<'a> {}
2191impl<'a> ast::AttrsOwner<'a> for NamedFieldDef<'a> {} 2278impl<'a> ast::AttrsOwner<'a> for NamedFieldDef<'a> {}
2192impl<'a> NamedFieldDef<'a> {} 2279impl<'a> NamedFieldDef<'a> {
2280 pub fn type_ref(self) -> Option<TypeRef<'a>> {
2281 super::child_opt(self)
2282 }
2283}
2284
2285// NamedFieldDefList
2286#[derive(Debug, Clone, Copy,)]
2287pub struct NamedFieldDefListNode<R: TreeRoot<RaTypes> = OwnedRoot> {
2288 pub(crate) syntax: SyntaxNode<R>,
2289}
2290pub type NamedFieldDefList<'a> = NamedFieldDefListNode<RefRoot<'a>>;
2291
2292impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<NamedFieldDefListNode<R1>> for NamedFieldDefListNode<R2> {
2293 fn eq(&self, other: &NamedFieldDefListNode<R1>) -> bool { self.syntax == other.syntax }
2294}
2295impl<R: TreeRoot<RaTypes>> Eq for NamedFieldDefListNode<R> {}
2296impl<R: TreeRoot<RaTypes>> Hash for NamedFieldDefListNode<R> {
2297 fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
2298}
2299
2300impl<'a> AstNode<'a> for NamedFieldDefList<'a> {
2301 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
2302 match syntax.kind() {
2303 NAMED_FIELD_DEF_LIST => Some(NamedFieldDefList { syntax }),
2304 _ => None,
2305 }
2306 }
2307 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
2308}
2309
2310impl<R: TreeRoot<RaTypes>> NamedFieldDefListNode<R> {
2311 pub fn borrowed(&self) -> NamedFieldDefList {
2312 NamedFieldDefListNode { syntax: self.syntax.borrowed() }
2313 }
2314 pub fn owned(&self) -> NamedFieldDefListNode {
2315 NamedFieldDefListNode { syntax: self.syntax.owned() }
2316 }
2317}
2318
2319
2320impl<'a> NamedFieldDefList<'a> {
2321 pub fn fields(self) -> impl Iterator<Item = NamedFieldDef<'a>> + 'a {
2322 super::children(self)
2323 }
2324}
2193 2325
2194// NamedFieldList 2326// NamedFieldList
2195#[derive(Debug, Clone, Copy,)] 2327#[derive(Debug, Clone, Copy,)]
@@ -2830,6 +2962,89 @@ impl<R: TreeRoot<RaTypes>> PointerTypeNode<R> {
2830 2962
2831impl<'a> PointerType<'a> {} 2963impl<'a> PointerType<'a> {}
2832 2964
2965// PosField
2966#[derive(Debug, Clone, Copy,)]
2967pub struct PosFieldNode<R: TreeRoot<RaTypes> = OwnedRoot> {
2968 pub(crate) syntax: SyntaxNode<R>,
2969}
2970pub type PosField<'a> = PosFieldNode<RefRoot<'a>>;
2971
2972impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<PosFieldNode<R1>> for PosFieldNode<R2> {
2973 fn eq(&self, other: &PosFieldNode<R1>) -> bool { self.syntax == other.syntax }
2974}
2975impl<R: TreeRoot<RaTypes>> Eq for PosFieldNode<R> {}
2976impl<R: TreeRoot<RaTypes>> Hash for PosFieldNode<R> {
2977 fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
2978}
2979
2980impl<'a> AstNode<'a> for PosField<'a> {
2981 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
2982 match syntax.kind() {
2983 POS_FIELD => Some(PosField { syntax }),
2984 _ => None,
2985 }
2986 }
2987 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
2988}
2989
2990impl<R: TreeRoot<RaTypes>> PosFieldNode<R> {
2991 pub fn borrowed(&self) -> PosField {
2992 PosFieldNode { syntax: self.syntax.borrowed() }
2993 }
2994 pub fn owned(&self) -> PosFieldNode {
2995 PosFieldNode { syntax: self.syntax.owned() }
2996 }
2997}
2998
2999
3000impl<'a> ast::AttrsOwner<'a> for PosField<'a> {}
3001impl<'a> PosField<'a> {
3002 pub fn type_ref(self) -> Option<TypeRef<'a>> {
3003 super::child_opt(self)
3004 }
3005}
3006
3007// PosFieldList
3008#[derive(Debug, Clone, Copy,)]
3009pub struct PosFieldListNode<R: TreeRoot<RaTypes> = OwnedRoot> {
3010 pub(crate) syntax: SyntaxNode<R>,
3011}
3012pub type PosFieldList<'a> = PosFieldListNode<RefRoot<'a>>;
3013
3014impl<R1: TreeRoot<RaTypes>, R2: TreeRoot<RaTypes>> PartialEq<PosFieldListNode<R1>> for PosFieldListNode<R2> {
3015 fn eq(&self, other: &PosFieldListNode<R1>) -> bool { self.syntax == other.syntax }
3016}
3017impl<R: TreeRoot<RaTypes>> Eq for PosFieldListNode<R> {}
3018impl<R: TreeRoot<RaTypes>> Hash for PosFieldListNode<R> {
3019 fn hash<H: Hasher>(&self, state: &mut H) { self.syntax.hash(state) }
3020}
3021
3022impl<'a> AstNode<'a> for PosFieldList<'a> {
3023 fn cast(syntax: SyntaxNodeRef<'a>) -> Option<Self> {
3024 match syntax.kind() {
3025 POS_FIELD_LIST => Some(PosFieldList { syntax }),
3026 _ => None,
3027 }
3028 }
3029 fn syntax(self) -> SyntaxNodeRef<'a> { self.syntax }
3030}
3031
3032impl<R: TreeRoot<RaTypes>> PosFieldListNode<R> {
3033 pub fn borrowed(&self) -> PosFieldList {
3034 PosFieldListNode { syntax: self.syntax.borrowed() }
3035 }
3036 pub fn owned(&self) -> PosFieldListNode {
3037 PosFieldListNode { syntax: self.syntax.owned() }
3038 }
3039}
3040
3041
3042impl<'a> PosFieldList<'a> {
3043 pub fn fields(self) -> impl Iterator<Item = PosField<'a>> + 'a {
3044 super::children(self)
3045 }
3046}
3047
2833// PrefixExpr 3048// PrefixExpr
2834#[derive(Debug, Clone, Copy,)] 3049#[derive(Debug, Clone, Copy,)]
2835pub struct PrefixExprNode<R: TreeRoot<RaTypes> = OwnedRoot> { 3050pub struct PrefixExprNode<R: TreeRoot<RaTypes> = OwnedRoot> {
@@ -3438,11 +3653,7 @@ impl<'a> ast::NameOwner<'a> for StructDef<'a> {}
3438impl<'a> ast::TypeParamsOwner<'a> for StructDef<'a> {} 3653impl<'a> ast::TypeParamsOwner<'a> for StructDef<'a> {}
3439impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {} 3654impl<'a> ast::AttrsOwner<'a> for StructDef<'a> {}
3440impl<'a> ast::DocCommentsOwner<'a> for StructDef<'a> {} 3655impl<'a> ast::DocCommentsOwner<'a> for StructDef<'a> {}
3441impl<'a> StructDef<'a> { 3656impl<'a> StructDef<'a> {}
3442 pub fn fields(self) -> impl Iterator<Item = NamedFieldDef<'a>> + 'a {
3443 super::children(self)
3444 }
3445}
3446 3657
3447// StructLit 3658// StructLit
3448#[derive(Debug, Clone, Copy,)] 3659#[derive(Debug, Clone, Copy,)]
diff --git a/crates/ra_syntax/src/grammar.ron b/crates/ra_syntax/src/grammar.ron
index 0da8b8183..e4e41d077 100644
--- a/crates/ra_syntax/src/grammar.ron
+++ b/crates/ra_syntax/src/grammar.ron
@@ -261,18 +261,20 @@ Grammar(
261 "TypeParamsOwner", 261 "TypeParamsOwner",
262 "AttrsOwner", 262 "AttrsOwner",
263 "DocCommentsOwner" 263 "DocCommentsOwner"
264 ],
265 collections: [
266 ["fields", "NamedFieldDef"]
267 ] 264 ]
268 ), 265 ),
269 "NamedFieldDef": ( traits: ["NameOwner", "AttrsOwner"] ), 266 "NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]),
267 "NamedFieldDef": ( traits: ["NameOwner", "AttrsOwner"], options: ["TypeRef"] ),
268 "PosFieldList": (collections: [["fields", "PosField"]]),
269 "PosField": ( traits: ["AttrsOwner"], options: ["TypeRef"]),
270 "EnumDef": ( traits: [ 270 "EnumDef": ( traits: [
271 "NameOwner", 271 "NameOwner",
272 "TypeParamsOwner", 272 "TypeParamsOwner",
273 "AttrsOwner", 273 "AttrsOwner",
274 "DocCommentsOwner" 274 "DocCommentsOwner"
275 ] ), 275 ], options: [["variant_list", "EnumVariantList"]] ),
276 "EnumVariantList": ( collections: [["variants", "EnumVariant"]] ),
277 "EnumVariant": ( traits: ["NameOwner"], options: ["Expr"] ),
276 "TraitDef": ( traits: ["NameOwner", "AttrsOwner", "DocCommentsOwner"] ), 278 "TraitDef": ( traits: ["NameOwner", "AttrsOwner", "DocCommentsOwner"] ),
277 "Module": ( 279 "Module": (
278 traits: ["NameOwner", "AttrsOwner", "DocCommentsOwner" ], 280 traits: ["NameOwner", "AttrsOwner", "DocCommentsOwner" ],