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
//! A module for the syntax nodes representing top-level WGSL declarations.

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

mod alias;
mod func;
pub mod import;
pub mod legacy;
mod struc;
mod var;

use crate::{common::AttributeList, ParseStream, Token, TokenKind};

pub use self::{
	alias::TypeAliasDecl,
	func::{FunctionDecl, ParamDecl},
	import::{
		ImportDecl, ImportPath, ImportPathBlock, ImportPathDecl, ImportPathLeaf,
		NamespacedImportPath,
	},
	struc::{FieldDecl, StructBody, StructDecl},
	var::VarDecl,
};

#[derive(Clone, DebugLisp)]
pub enum Decl {
	Var(VarDecl),
	Const(VarDecl),
	TypeAlias(TypeAliasDecl),
	Struct(StructDecl),
	Field(FieldDecl),
	Function(FunctionDecl),
	Param(ParamDecl),
	Extension(legacy::ExtensionDecl), // TODO
	ImportPath(ImportPathDecl),
	Import(ImportDecl),
	Module(legacy::ModuleDecl), // TODO
}

lazy_static! {
	// FIXME: This is gross -- is there a better way to handle this?
	static ref ERR_TOKEN: Token = Token::Ident(literal_substr!("ERROR"), Span::default());
}

impl Decl {
	pub fn name(&self) -> &Token {
		match self {
			Decl::Var(decl) | Decl::Const(decl) => &decl.name,
			Decl::TypeAlias(decl) => &decl.name,
			Decl::Struct(decl) => &decl.name,
			Decl::Field(decl) => &decl.name,
			Decl::Function(decl) => &decl.name,
			Decl::Param(decl) => &decl.name,
			Decl::Extension(decl) => &decl.name,
			Decl::ImportPath(decl) => decl.name(),
			Decl::Import(_) => &ERR_TOKEN,
			Decl::Module(decl) => &decl.name,
		}
	}

	pub fn attributes(&self) -> Option<&AttributeList> {
		match self {
			Decl::Var(decl) => decl.attributes.as_ref(),
			Decl::Const(decl) => decl.attributes.as_ref(),
			Decl::TypeAlias(_) => None,
			Decl::Struct(decl) => decl.attributes.as_ref(),
			Decl::Field(decl) => decl.attributes.as_ref(),
			Decl::Function(decl) => decl.attributes.as_ref(),
			Decl::Param(decl) => decl.attributes.as_ref(),
			Decl::Extension(_) => None,
			Decl::ImportPath(_) => None,
			Decl::Import(_) => None,
			Decl::Module(_) => None,
		}
	}
}

impl Parse for Decl {
	type Stream = ParseStream;

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

		match input.peek() {
			Some(token) => match token.as_matchable() {
				(Punct, "@", _) => {
					let attributes = input.parse::<AttributeList>()?;

					match input.parse::<Decl>()? {
						Decl::Var(mut inner) => {
							inner.attributes = Some(attributes);
							Ok(Decl::Var(inner))
						}
						Decl::Const(mut inner) => {
							inner.attributes = Some(attributes);
							Ok(Decl::Const(inner))
						}
						Decl::Struct(mut inner) => {
							inner.attributes = Some(attributes);
							Ok(Decl::Struct(inner))
						}
						Decl::Function(mut inner) => {
							inner.attributes = Some(attributes);
							Ok(Decl::Function(inner))
						}
						_ => Err(SpannedError {
							message: "Attributes are not valid in this position".into(),
							span: Some(attributes.span()),
							source: input.source(),
						}),
					}
				}
				(Keyword, "var", _) => Ok(Decl::Var(input.parse()?)),
				(Keyword, "let" | "const" | "override", _) => Ok(Decl::Const(input.parse()?)),
				(Keyword, "alias", _) => Ok(Decl::TypeAlias(input.parse()?)),
				(Keyword, "struct", _) => Ok(Decl::Struct(input.parse()?)),
				(Keyword, "fn", _) => Ok(Decl::Function(input.parse()?)),
				(Keyword, "enable", _) => Ok(Decl::Extension(input.parse()?)),
				(Directive, "#define_import_path", _) => Ok(Decl::ImportPath(input.parse()?)),
				(Directive, "#import", _) => Ok(Decl::Import(input.parse()?)),
				(Keyword, "import", _) => Ok(Decl::Module(input.parse()?)),
				(_, _, span) => Err(SpannedError {
					message: "Expected `var`, `let`, `const`, `override`, `alias`, \
						`struct`, `fn`, `enable`, or `import`"
						.into(),
					span: Some(span),
					source: input.source(),
				}),
			},
			None => Err(SpannedError {
				message: "Unexpected end of input".into(),
				source: input.source(),
				span: input.prev().map(|token| token.span()),
			}),
		}
	}
}

impl Spanned for Decl {
	fn span(&self) -> Span {
		use Decl::*;

		match self {
			Var(inner) => inner.span(),
			Const(inner) => inner.span(),
			TypeAlias(inner) => inner.span(),
			Struct(inner) => inner.span(),
			Field(inner) => inner.span(),
			Function(inner) => inner.span(),
			Param(inner) => inner.span(),
			Extension(inner) => inner.span(),
			ImportPath(inner) => inner.span(),
			Import(inner) => inner.span(),
			Module(inner) => inner.span(),
		}
	}
}