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
use derive_enum_from_into::{EnumFrom, EnumTryInto};
use tokenizer_lib::Token;
use visitable_derive::Visitable;

use crate::{
	errors::parse_lexing_error,
	extensions::decorators,
	extractor::{ExtractedFunction, GetFunction},
	Decorated, Keyword, ParseError, ParseErrors, StatementPosition, TSXKeyword, TSXToken,
	TypeDefinitionModuleDeclaration,
};

pub use self::{
	export::ExportDeclaration,
	variable::{VariableDeclaration, VariableDeclarationItem},
};

pub type StatementFunctionBase = crate::functions::GeneralFunctionBase<StatementPosition>;
pub type StatementFunction = crate::FunctionBase<StatementFunctionBase>;

pub mod classes;
pub mod export;
pub mod import;
pub mod variable;

pub use super::types::{
	declares::*,
	enum_declaration::{EnumDeclaration, EnumMember},
	interface::InterfaceDeclaration,
	type_alias::TypeAlias,
};
pub use classes::ClassDeclaration;
pub use import::{ImportDeclaration, ImportPart, ImportStatementId};

#[derive(Debug, Clone, Visitable, EnumFrom, EnumTryInto, PartialEq)]
#[try_into_references(&, &mut)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
pub enum Declaration {
	Variable(VariableDeclaration),
	Function(Decorated<StatementFunction>),
	ExtractedFunction(ExtractedFunction<StatementFunctionBase>),
	Class(Decorated<ClassDeclaration<StatementPosition>>),
	Enum(Decorated<EnumDeclaration>),
	Interface(Decorated<InterfaceDeclaration>),
	TypeAlias(TypeAlias),
	// Special TS only
	DeclareVariable(DeclareVariableDeclaration),
	DeclareFunction(DeclareFunctionDeclaration),
	#[from_ignore]
	DeclareInterface(InterfaceDeclaration),
	// Top level only
	Import(ImportDeclaration),
	Export(Decorated<ExportDeclaration>),
}

impl Declaration {
	// TODO strict mode, type etc can affect result
	pub(crate) fn is_declaration_start(
		reader: &mut impl tokenizer_lib::TokenReader<crate::TSXToken, source_map::Span>,
	) -> bool {
		matches!(
			reader.peek(),
			Some(Token(
				TSXToken::Keyword(
					TSXKeyword::Let
						| TSXKeyword::Const | TSXKeyword::Function
						| TSXKeyword::Class | TSXKeyword::Enum
						| TSXKeyword::Type | TSXKeyword::Declare
						| TSXKeyword::Import | TSXKeyword::Export
						| TSXKeyword::Async | TSXKeyword::Generator
				) | TSXToken::At,
				_
			))
		)
	}
}

impl crate::ASTNode for Declaration {
	fn get_position(&self) -> std::borrow::Cow<source_map::Span> {
		match self {
			Declaration::Variable(item) => item.get_position(),
			Declaration::Function(item) => item.get_position(),
			Declaration::ExtractedFunction(item) => item.get_position(),
			Declaration::Class(item) => item.get_position(),
			Declaration::Enum(item) => item.get_position(),
			Declaration::Interface(item) => item.get_position(),
			Declaration::TypeAlias(item) => item.get_position(),
			Declaration::DeclareVariable(item) => item.get_position(),
			Declaration::DeclareFunction(item) => item.get_position(),
			Declaration::DeclareInterface(item) => item.get_position(),
			Declaration::Import(item) => item.get_position(),
			Declaration::Export(item) => item.get_position(),
		}
	}

	fn from_reader(
		reader: &mut impl tokenizer_lib::TokenReader<crate::TSXToken, source_map::Span>,
		state: &mut crate::ParsingState,
		settings: &crate::ParseSettings,
	) -> crate::ParseResult<Self> {
		// TODO assert decorators are used. If they exist but item is not `Decorated`
		// then need to throw a parse error
		let decorators = decorators::decorators_from_reader(reader, state, settings)?;

		match reader.peek().ok_or_else(parse_lexing_error)?.0 {
			// Const can be either variable declaration or const enum
			TSXToken::Keyword(TSXKeyword::Const) => {
				let after_const = reader.peek_n(2);
				if let Some(Token(TSXToken::Keyword(TSXKeyword::Enum), _)) = after_const {
					EnumDeclaration::from_reader(reader, state, settings)
						.map(|on| Declaration::Enum(Decorated { decorators, on }))
				} else {
					let declaration = VariableDeclaration::from_reader(reader, state, settings)?;
					Ok(Declaration::Variable(declaration))
				}
			}
			TSXToken::Keyword(TSXKeyword::Let) => {
				let declaration = VariableDeclaration::from_reader(reader, state, settings)?;
				Ok(Declaration::Variable(declaration))
			}
			TSXToken::Keyword(TSXKeyword::Enum) => {
				EnumDeclaration::from_reader(reader, state, settings)
					.map(|on| Declaration::Enum(Decorated { decorators, on }))
			}
			TSXToken::Keyword(TSXKeyword::Generator) if settings.generator_keyword => {
				let func = StatementFunction::from_reader(reader, state, settings)?;
				if !decorators.is_empty() {
					todo!();
				}
				let extracted = state.function_extractor.new_extracted_function(func);
				Ok(Declaration::ExtractedFunction(extracted))
			}
			TSXToken::Keyword(TSXKeyword::Function | TSXKeyword::Async) => {
				let func = StatementFunction::from_reader(reader, state, settings)?;
				if !decorators.is_empty() {
					todo!();
				}
				let extracted = state.function_extractor.new_extracted_function(func);
				Ok(Declaration::ExtractedFunction(extracted))
			}

			TSXToken::Keyword(TSXKeyword::Class) => {
				let Token(_, class_token_pos) = reader.next().unwrap();
				let class_keyword = Keyword::new(class_token_pos);
				ClassDeclaration::from_reader_sub_class_keyword(
					reader,
					state,
					settings,
					class_keyword,
				)
				.map(|on| Declaration::Class(Decorated { decorators, on }))
			}
			TSXToken::Keyword(TSXKeyword::Export) => {
				ExportDeclaration::from_reader(reader, state, settings)
					.map(|on| Declaration::Export(Decorated { decorators, on }))
			}
			TSXToken::Keyword(TSXKeyword::Import) => {
				ImportDeclaration::from_reader(reader, state, settings).map(Into::into)
			}
			TSXToken::Keyword(TSXKeyword::Interface) => {
				InterfaceDeclaration::from_reader(reader, state, settings)
					.map(|on| Declaration::Interface(Decorated { decorators, on }))
			}
			TSXToken::Keyword(TSXKeyword::Type) => {
				TypeAlias::from_reader(reader, state, settings).map(Into::into)
			}
			TSXToken::Keyword(TSXKeyword::Declare) => {
				let declare_span = reader.next().unwrap().1;
				crate::modules::parse_declare_item(
					reader,
					state,
					settings,
					decorators,
					declare_span,
				)
				.map(|ty_def_mod_stmt| match ty_def_mod_stmt {
					TypeDefinitionModuleDeclaration::Variable(declare_var) => {
						Declaration::DeclareVariable(declare_var)
					}
					TypeDefinitionModuleDeclaration::Function(declare_func) => {
						Declaration::DeclareFunction(declare_func)
					}
					TypeDefinitionModuleDeclaration::Class(_) => todo!("error"),
					TypeDefinitionModuleDeclaration::Interface(_) => {
						todo!("error")
					}
					TypeDefinitionModuleDeclaration::TypeAlias(_) => todo!("error"),
					TypeDefinitionModuleDeclaration::Namespace(_) => todo!(),
					TypeDefinitionModuleDeclaration::Comment(_) => unreachable!(),
					TypeDefinitionModuleDeclaration::LocalTypeAlias(_) => todo!(),
					TypeDefinitionModuleDeclaration::LocalVariableDeclaration(_) => {
						todo!()
					}
				})
			}
			_ => {
				let Token(token, position) = reader.next().unwrap();
				return Err(ParseError::new(
					ParseErrors::UnexpectedToken {
						expected: &[
							TSXToken::Keyword(TSXKeyword::Let),
							TSXToken::Keyword(TSXKeyword::Const),
							TSXToken::Keyword(TSXKeyword::Function),
							TSXToken::Keyword(TSXKeyword::Class),
							TSXToken::Keyword(TSXKeyword::Enum),
							TSXToken::Keyword(TSXKeyword::Type),
							TSXToken::Keyword(TSXKeyword::Declare),
							TSXToken::Keyword(TSXKeyword::Import),
							TSXToken::Keyword(TSXKeyword::Export),
							TSXToken::Keyword(TSXKeyword::Async),
							TSXToken::Keyword(TSXKeyword::Generator),
						],
						found: token,
					},
					position,
				));
			}
		}
	}

	fn to_string_from_buffer<T: source_map::ToString>(
		&self,
		buf: &mut T,
		settings: &crate::ToStringSettingsAndData,
		depth: u8,
	) {
		match self {
			Declaration::Function(f) => f.to_string_from_buffer(buf, settings, depth),
			Declaration::ExtractedFunction(func) => {
				if let Some(func) =
					GetFunction::<StatementFunctionBase>::get_function_ref(&settings.1, func.0)
				{
					func.to_string_from_buffer(buf, settings, depth)
				}
			}
			Declaration::Variable(var) => var.to_string_from_buffer(buf, settings, depth),
			Declaration::Class(cls) => cls.to_string_from_buffer(buf, settings, depth),
			Declaration::Import(is) => is.to_string_from_buffer(buf, settings, depth),
			Declaration::Export(es) => es.to_string_from_buffer(buf, settings, depth),
			Declaration::Interface(id) => id.to_string_from_buffer(buf, settings, depth),
			Declaration::TypeAlias(ta) => ta.to_string_from_buffer(buf, settings, depth),
			Declaration::Enum(r#enum) => r#enum.to_string_from_buffer(buf, settings, depth),
			// TODO should skip these under no types
			Declaration::DeclareFunction(dfd) => dfd.to_string_from_buffer(buf, settings, depth),
			Declaration::DeclareVariable(dvd) => dvd.to_string_from_buffer(buf, settings, depth),
			Declaration::DeclareInterface(did) => {
				buf.push_str("declare ");
				did.to_string_from_buffer(buf, settings, depth)
			}
		}
	}
}