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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
//! A module for syntax nodes that can appear in many different parts of a
//! program, like attributes and type annotations.

use std::sync::Arc;

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

use crate::{
	expr::{Expr, IdentExpr, IdentExprBuilder, NamespacedIdentBuilder},
	parser::ErrorRecoveringParseStream,
	token::{brace, operator, punct},
	ParseStream, Token, TokenKind,
};

#[derive(Clone, DebugLisp)]
pub struct AttributeList {
	pub attributes: Arc<[Attribute]>,
}

#[derive(Clone, DebugLisp)]
pub struct Attribute {
	pub at_sign: Token,
	pub name: Token,
	pub params: Option<ArgumentList>,
}

#[derive(Clone, DebugLisp)]
pub struct TypeDecl {
	pub annotator: Option<Token>,
	pub attributes: Option<AttributeList>,
	pub name: IdentExpr,
	pub child_ty: Option<Arc<TypeDecl>>,
	pub storage_class: Option<Token>,
	pub access_mode: Option<Token>,
	pub element_count: Option<Token>,
}

#[derive(Clone, DebugLisp)]
pub struct ArgumentList {
	pub brace_open: Token,
	pub arguments: Arc<[Expr]>,
	pub brace_close: Token,
}

impl Spanned for AttributeList {
	fn span(&self) -> Span {
		match self.attributes.len() {
			0 => Span::default(),
			1 => self.attributes.first().unwrap().span(),
			_ => self
				.attributes
				.first()
				.unwrap()
				.span()
				.through(self.attributes.last().unwrap().span()),
		}
	}
}

impl Parse for AttributeList {
	type Stream = ParseStream;

	fn parse(input: &mut Self::Stream) -> gramatika::Result<Self> {
		let attributes = input.parse_seq(|input| input.check(punct!["@"]));

		Ok(Self {
			attributes: attributes.into(),
		})
	}
}

impl Spanned for Attribute {
	fn span(&self) -> Span {
		if let Some(ref params) = self.params {
			self.at_sign.span().through(params.span())
		} else {
			self.at_sign.span().through(self.name.span())
		}
	}
}

impl Parse for Attribute {
	type Stream = ParseStream;

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

		Ok(Self {
			at_sign,
			name,
			params,
		})
	}
}

#[derive(Default)]
struct TypeDeclBuilder {
	attributes: Option<AttributeList>,
	annotator: Option<Token>,
	name: Option<IdentExpr>,
	child_ty: Option<Arc<TypeDecl>>,
	storage_class: Option<Token>,
	access_mode: Option<Token>,
	element_count: Option<Token>,
}

impl TypeDeclBuilder {
	fn new() -> Self {
		Self::default()
	}
	fn attributes(&mut self, attributes: AttributeList) -> &mut Self {
		self.attributes = Some(attributes);
		self
	}
	fn annotator(&mut self, colon: Token) -> &mut Self {
		self.annotator = Some(colon);
		self
	}
	fn name(&mut self, name: IdentExpr) -> &mut Self {
		self.name = Some(name);
		self
	}
	fn child_ty(&mut self, child_ty: TypeDecl) -> &mut Self {
		self.child_ty = Some(Arc::new(child_ty));
		self
	}
	fn storage_class(&mut self, storage_class: Token) -> &mut Self {
		self.storage_class = Some(storage_class);
		self
	}
	fn access_mode(&mut self, access_mode: Token) -> &mut Self {
		self.access_mode = Some(access_mode);
		self
	}
	fn element_count(&mut self, element_count: Token) -> &mut Self {
		self.element_count = Some(element_count);
		self
	}
	fn build(self) -> TypeDecl {
		TypeDecl {
			annotator: self.annotator,
			attributes: self.attributes,
			name: self.name.expect("`name` field is required!"),
			child_ty: self.child_ty,
			storage_class: self.storage_class,
			access_mode: self.access_mode,
			element_count: self.element_count,
		}
	}
}

impl Spanned for TypeDecl {
	fn span(&self) -> Span {
		let first = self
			.annotator
			.as_ref()
			.map(|token| token.span())
			.or_else(|| self.attributes.as_ref().map(|attr| attr.span()))
			.unwrap_or_else(|| self.name.span());

		let last = self
			.access_mode
			.as_ref()
			.map(|token| token.span())
			.or_else(|| self.storage_class.as_ref().map(|token| token.span()))
			.or_else(|| {
				self.element_count.as_ref().map(|token| {
					let mut child_span = token.span();
					child_span.end.character += 1; // Account for the `>`
					child_span
				})
			})
			.or_else(|| {
				self.child_ty.as_ref().map(|token| {
					let mut child_span = token.span();
					child_span.end.character += 1; // Account for the `>`
					child_span
				})
			})
			.unwrap_or_else(|| self.name.span());

		first.through(last)
	}
}

impl Parse for TypeDecl {
	type Stream = ParseStream;

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

		if input.check(punct![:]) || input.check(operator![->]) {
			builder.annotator(input.next().unwrap());
		}
		if input.check(punct!["@"]) {
			builder.attributes(input.parse()?);
		}
		if input.check_kind(TokenKind::Type) {
			let name = input.next().unwrap();

			builder.name(IdentExpr::Leaf(name.clone()));

			if input.check(operator![<]) {
				input.consume(operator![<])?;

				while !input.check(operator![>]) && !input.check(operator![>>]) {
					match input.peek() {
						Some(token) => match token.as_matchable() {
							(Type | Ident, _, _) => {
								builder.child_ty(input.parse()?);
							}
							(Keyword, "function" | "private" | "workgroup" | "uniform" | "storage", _) => {
								builder.storage_class(input.next().unwrap());
							}
							(Keyword, "read" | "write" | "read_write", _) => {
								builder.access_mode(input.next().unwrap());
							}
							(Punct, ",", _) => {
								input.discard();
							},
							(IntLiteral, _, _)
								if matches!(name.lexeme().as_str(), "array" | "binding_array") =>
							{
								builder.element_count(input.next().unwrap());
							}
							(_, _, span) => {
								return Err(SpannedError {
									message: "Expected type, storage class, access mode, texel format, or element count"
										.into(),
									source: input.source(),
									span: Some(span),
								})
							}
						}
						None => {
							return Err(SpannedError {
								message: "Unexpected end of input".into(),
								source: input.source(),
								span: input.prev().map(|token| token.span()),
							})
						}
					}
				}

				if input.check(operator![>>]) {
					input.split_next(1, (Token::operator, Token::operator))?;
				} else {
					input.consume(operator![>])?;
				}
			}
		} else {
			let mut ident = input.parse::<IdentExprBuilder>()?;
			let mut expr = &mut ident;
			while let IdentExprBuilder::Namespaced(NamespacedIdentBuilder { ident, .. }) = expr {
				expr = ident.as_mut();
			}
			let IdentExprBuilder::Leaf(name) = expr else {
				unreachable!();
			};
			*name = input.upgrade_last(TokenKind::Ident, Token::struct_)?;

			builder.name(ident.build());
		}

		Ok(builder.build())
	}
}

impl Parse for ArgumentList {
	type Stream = ParseStream;

	fn parse(input: &mut Self::Stream) -> gramatika::Result<Self> {
		let brace_open = input.consume(brace!["("])?;
		let arguments = input.parse_seq_separated(punct![,], |input| !input.check(brace![")"]))?;
		let brace_close = input.consume(brace![")"])?;

		Ok(Self {
			brace_open,
			arguments: arguments.into(),
			brace_close,
		})
	}
}

impl Spanned for ArgumentList {
	fn span(&self) -> Span {
		self.brace_open.span().through(self.brace_close.span())
	}
}