1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::sync::Arc;

use gramatika::{Parse, ParseStreamer, Span, Spanned, SpannedError, Token as _};

use crate::{
	common::{AttributeList, TypeDecl},
	token::{brace, keyword, operator, punct, Token, TokenKind},
	ParseStream,
};

use super::Decl;

#[derive(Clone, DebugLisp)]
pub struct StructDecl {
	pub attributes: Option<AttributeList>,
	pub storage: Token,
	pub storage_modifier: Option<Token>,
	pub name: Token,
	pub body: StructBody,
}

#[derive(Clone, DebugLisp)]
pub struct StructBody {
	pub open_brace: Token,
	pub fields: Arc<[Decl]>,
	pub close_brace: Token,
}

#[derive(Clone, DebugLisp)]
pub struct FieldDecl {
	pub attributes: Option<AttributeList>,
	pub name: Token,
	pub ty: TypeDecl,
	pub separator: Option<Token>,
}

impl Parse for StructDecl {
	type Stream = ParseStream;

	fn parse(input: &mut Self::Stream) -> gramatika::Result<Self> {
		let storage = input.consume(keyword![struct])?;

		let storage_modifier = if input.check(operator![<]) {
			input.consume(operator![<])?;
			let modifier = input.consume_kind(TokenKind::Keyword)?;
			input.consume(operator![>])?;

			Some(modifier)
		} else {
			None
		};

		let name = input.consume_as(TokenKind::Ident, Token::struct_)?;
		let body = input.parse()?;

		Ok(Self {
			attributes: None,
			storage,
			storage_modifier,
			name,
			body,
		})
	}
}

impl Spanned for StructDecl {
	fn span(&self) -> Span {
		self.storage.span().through(self.body.span())
	}
}

impl Parse for StructBody {
	type Stream = ParseStream;

	fn parse(input: &mut Self::Stream) -> gramatika::Result<Self> {
		use TokenKind::*;

		let open_brace = input.consume(brace!["{"])?;

		let mut fields = vec![];
		while !input.check(brace!["}"]) {
			match input.peek() {
				Some(token) => match token.as_matchable() {
					(Punct, "@", _) | (Ident, _, _) => {
						let field = input.parse::<FieldDecl>()?;
						fields.push(Decl::Field(field));
					}
					(_, _, span) => {
						return Err(SpannedError {
							message: "Expected field declaration".into(),
							span: Some(span),
							source: input.source(),
						})
					}
				},
				None => {
					return Err(SpannedError {
						message: "Unexpected end of input".into(),
						source: input.source(),
						span: None,
					})
				}
			};
		}

		let close_brace = input.consume(brace!["}"])?;

		Ok(Self {
			open_brace,
			fields: fields.into(),
			close_brace,
		})
	}
}

impl Spanned for StructBody {
	fn span(&self) -> Span {
		self.open_brace.span().through(self.close_brace.span())
	}
}

impl Parse for FieldDecl {
	type Stream = ParseStream;

	fn parse(input: &mut Self::Stream) -> gramatika::Result<Self> {
		let attributes = if input.check(punct!["@"]) {
			Some(input.parse::<AttributeList>()?)
		} else {
			None
		};
		let name = input.consume_as(TokenKind::Ident, Token::field)?;
		let ty = input.parse()?;

		let separator = if !input.check(brace!("}")) {
			Some(input.consume(punct![,])?)
		} else {
			None
		};

		Ok(Self {
			attributes,
			name,
			ty,
			separator,
		})
	}
}

impl Spanned for FieldDecl {
	fn span(&self) -> Span {
		let span_start = self
			.attributes
			.as_ref()
			.and_then(|attrs| attrs.attributes.first().map(|attr| &attr.at_sign))
			.unwrap_or(&self.name)
			.span();

		let span_end = match &self.separator {
			Some(sep) => sep.span(),
			None => self.ty.span(),
		};

		span_start.through(span_end)
	}
}