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
use std::borrow::Cow;

use iterator_endiate::EndiateIteratorExt;
use source_map::Span;
use tokenizer_lib::{Token, TokenReader};
use visitable_derive::Visitable;

use crate::{
	extractor::ExtractedFunctions, tokens::token_as_identifier, ASTNode, Expression, ParseResult,
	ParseSettings, TSXToken, Visitable,
};

#[derive(Debug, PartialEq, Eq, Clone, Visitable)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
pub struct Decorator {
	pub name: String,
	pub arguments: Option<Vec<Expression>>,
	pub position: Span,
}

impl ASTNode for Decorator {
	fn get_position(&self) -> Cow<Span> {
		Cow::Borrowed(&self.position)
	}

	fn from_reader(
		reader: &mut impl TokenReader<TSXToken, Span>,
		state: &mut crate::ParsingState,
		settings: &ParseSettings,
	) -> ParseResult<Self> {
		let at_pos = reader.expect_next(TSXToken::At)?;
		Self::from_reader_sub_at_symbol(reader, state, settings, at_pos)
	}

	fn to_string_from_buffer<T: source_map::ToString>(
		&self,
		buf: &mut T,
		settings: &crate::ToStringSettingsAndData,
		depth: u8,
	) {
		if settings.0.include_decorators {
			buf.push('@');
			buf.push_str(self.name.as_str());
			if let Some(arguments) = &self.arguments {
				buf.push('(');
				for (at_end, argument) in arguments.iter().endiate() {
					argument.to_string_from_buffer(buf, settings, depth);
					if !at_end {
						buf.push(',');
						settings.0.add_gap(buf);
					}
				}
				buf.push(')');
			}
		}
	}
}

impl Decorator {
	pub(crate) fn from_reader_sub_at_symbol(
		reader: &mut impl TokenReader<TSXToken, Span>,
		state: &mut crate::ParsingState,
		settings: &ParseSettings,
		at_pos: Span,
	) -> ParseResult<Self> {
		let (name, name_position) = token_as_identifier(reader.next().unwrap(), "Decorator name")?;
		let (arguments, position) = if matches!(reader.peek().unwrap().0, TSXToken::OpenParentheses)
		{
			reader.next();
			let mut arguments = Vec::<_>::new();
			loop {
				if matches!(reader.peek().unwrap().0, TSXToken::CloseParentheses) {
					break;
				}
				arguments.push(Expression::from_reader(reader, state, settings)?);
				match reader.peek() {
					Some(Token(TSXToken::Comma, _)) => {
						reader.next();
					}
					_ => break,
				}
			}
			let end_position = reader.expect_next(TSXToken::CloseParentheses)?;
			(Some(arguments), at_pos.union(&end_position))
		} else {
			(None, at_pos.union(&name_position))
		};
		Ok(Self { name, arguments, position })
	}
}

/// TODO under cfg if don't want this could just be `type Decorated<T> = T;`
#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
pub struct Decorated<T> {
	pub decorators: Vec<Decorator>,
	pub on: T,
}

impl<N: ASTNode> ASTNode for Decorated<N> {
	fn get_position(&self) -> Cow<Span> {
		// TODO union with first decorated
		self.on.get_position()
	}

	fn from_reader(
		reader: &mut impl TokenReader<TSXToken, Span>,
		state: &mut crate::ParsingState,
		settings: &ParseSettings,
	) -> ParseResult<Self> {
		let decorators = decorators_from_reader(reader, state, settings)?;
		N::from_reader(reader, state, settings).map(|on| Self { on, decorators })
	}

	fn to_string_from_buffer<T: source_map::ToString>(
		&self,
		buf: &mut T,
		settings: &crate::ToStringSettingsAndData,
		depth: u8,
	) {
		self.to_string_from_buffer_just_decorators(buf, settings, depth);
		self.on.to_string_from_buffer(buf, settings, depth);
	}
}

impl<U> Decorated<U> {
	pub fn new(on: U) -> Self {
		Self { decorators: Default::default(), on }
	}

	pub(crate) fn to_string_from_buffer_just_decorators<T: source_map::ToString>(
		&self,
		buf: &mut T,
		settings: &crate::ToStringSettingsAndData,
		depth: u8,
	) {
		if settings.0.include_decorators {
			for decorator in self.decorators.iter() {
				decorator.to_string_from_buffer(buf, settings, depth);
				if settings.0.pretty {
					buf.push_new_line();
				} else {
					buf.push(' ');
				}
			}
		}
	}
}

pub(crate) fn decorators_from_reader(
	reader: &mut impl TokenReader<TSXToken, Span>,
	state: &mut crate::ParsingState,
	settings: &ParseSettings,
) -> ParseResult<Vec<Decorator>> {
	let mut decorators = Vec::new();
	while let Some(Token(TSXToken::At, _)) = reader.peek() {
		decorators.push(Decorator::from_reader(reader, state, settings)?);
	}
	Ok(decorators)
}

impl<T: Visitable> Visitable for Decorated<T> {
	fn visit<TData>(
		&self,
		visitors: &mut (impl crate::VisitorReceiver<TData> + ?Sized),
		data: &mut TData,
		settings: &crate::VisitSettings,
		functions: &mut ExtractedFunctions,
		chain: &mut temporary_annex::Annex<crate::Chain>,
	) {
		self.on.visit(visitors, data, settings, functions, chain);
	}

	fn visit_mut<TData>(
		&mut self,
		visitors: &mut (impl crate::VisitorMutReceiver<TData> + ?Sized),
		data: &mut TData,
		settings: &crate::VisitSettings,
		functions: &mut ExtractedFunctions,
		chain: &mut temporary_annex::Annex<crate::Chain>,
	) {
		self.on.visit_mut(visitors, data, settings, functions, chain);
	}
}