tidos_macro 0.6.7

Procedural macros for the Tidos component framework.
Documentation
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
use proc_macro2::Punct;
use proc_macro2::{Group, Ident, Literal, Spacing, TokenTree};
use quote::ToTokens;
use syn::buffer::Cursor;
use syn::parse::{Parse, ParseStream};
use syn::token::{Brace, Token};
use syn::Token;

use crate::parsing::commands::TypeOfCommandTag;
use crate::parsing::utils::{
	is_cursor_on_else_branch, is_cursor_on_else_if_branch, is_cursor_on_end_of_if_branch,
	is_cursor_on_new_if_branch, matches_case_statement, matches_corresponding_command_tag,
};
use crate::r#impl::{Attribute, Component, Content, ControlTag, HTMLTag};

impl Parse for Content {
	fn parse(input: ParseStream) -> syn::Result<Self> {
		if input.is_empty() {
			panic!("No tokens left to parse")
		}
		// <p></p>
		if input.peek(Token![<]) {
			let temp = input.parse::<HTMLTag>()?;

			return Ok(Content::Tag(temp));

		// @html{"<p>hello world</p>"}
		} else if matches! { input.cursor().punct(), Some((punct, _)) if punct.as_char() == '@'} {
			input.parse::<Punct>()?;
			let ident = input.parse::<Ident>()?;
			if ident.to_string() != "html" {
				panic!()
			}
			let group = input.parse::<Group>()?;

			return Ok(Content::RawHTMLExpression(group));
		// {#for ... in ... }, {#if}, {#match} or { name }
		} else if input.peek(Brace) {
			let group = input.parse::<Group>()?;

			let is_command = matches! { group.stream().into_iter().take(1).collect::<Vec<_>>()[0], TokenTree::Punct(ref punct) if punct.as_char() == '#'};
			if !is_command {
				// { name }
				return Ok(Content::Expression(group));
			}

			// {#for ... in ... } {#if} {#match}
			let type_of_command = syn::parse2::<TypeOfCommandTag>(group.stream())?;

			return match type_of_command {
				TypeOfCommandTag::For {
					left_side,
					right_side,
				} => {
					// ...
					let mut contents: Vec<Content> = Vec::new();
					while !input.is_empty()
						&& !matches_corresponding_command_tag(input.cursor(), "for")
					{
						let child = input.parse::<Content>()?;
						contents.push(child);
					}

					// {/for}
					input.parse::<Group>()?;

					Ok(Content::ControlTag(ControlTag::For {
						left_side,
						right_side,
						contents,
					}))
				}
				TypeOfCommandTag::If(if_statement) => {
					let mut if_content: Vec<Content> = Vec::new();
					while !is_cursor_on_new_if_branch(&input.cursor()) {
						let child = input.parse::<Content>()?;
						if_content.push(child);
					}

					let mut if_else_chain = Vec::new();
					while is_cursor_on_else_if_branch(&input.cursor()) {
						let else_if_statement = input.parse::<Group>()?;
						let else_if_statement = else_if_statement
							.stream()
							.into_iter()
							.skip(3)
							.collect::<Vec<_>>();
						let mut children_else_if_branch: Vec<Content> = Vec::new();
						while !is_cursor_on_new_if_branch(&input.cursor()) {
							let child = input.parse::<Content>()?;
							children_else_if_branch.push(child);
						}

						if_else_chain.push((else_if_statement, children_else_if_branch));
					}

					let else_content = if is_cursor_on_else_branch(&input.cursor()) {
						input.parse::<Group>()?;

						let mut children_if_branch: Vec<Content> = Vec::new();
						while !is_cursor_on_new_if_branch(&input.cursor()) {
							let child = input.parse::<Content>()?;
							children_if_branch.push(child);
						}

						Some(children_if_branch)
					} else {
						None
					};

					if !is_cursor_on_end_of_if_branch(&input.cursor()) {
						input.error("Expected {/if}");
					}
					input.parse::<Group>()?;

					Ok(Content::ControlTag(ControlTag::IfChain {
						if_statement,
						if_content,
						if_else_chain,
						else_content,
					}))
				}
				TypeOfCommandTag::Match(match_statement) => {
					// {:case ...}
					let mut cases = Vec::new();

					while !input.is_empty()
						&& !matches_corresponding_command_tag(input.cursor(), "match")
					{
						let case = input.parse::<Group>()?;
						let mut children: Vec<Content> = Vec::new();
						while !matches_case_statement(input.cursor())
							&& !matches_corresponding_command_tag(input.cursor(), "match")
						{
							let child = input.parse::<Content>()?;
							children.push(child);
						}

						cases.push((
							case.stream()
								.into_iter()
								.skip(2)
								.collect::<Vec<TokenTree>>(),
							children,
						));
					}

					// {/match}>
					input.parse::<Group>()?;

					Ok(Content::ControlTag(ControlTag::Match {
						match_statement,
						cases,
					}))
				}
			};

			panic!();

		// text between tags
		} else {
			let mut output = String::new();
			let mut last_was_punct = false;
			while !input.is_empty() && !(input.peek(Token![<]) || input.peek(Brace)) {
				let token = input.parse::<TokenTree>()?;
				match token {
					TokenTree::Group(_) => {
						panic!()
					}
					TokenTree::Ident(ident) => {
						if !output.is_empty() {
							output.push(' ');
						}
						last_was_punct = false;
						output.push_str(ident.to_string().as_str());
					}
					TokenTree::Punct(punct) => {
						output.push(punct.as_char());

						// Check if the next token is also a punctuation
						if !output.is_empty()
							&& proc_macro2::Punct::peek(input.cursor())
							&& punct.spacing() == Spacing::Alone
						{
							output.push(' ');
						}

						last_was_punct = true;
					}
					TokenTree::Literal(lit) => {
						if !output.is_empty() {
							output.push(' ');
						}
						last_was_punct = false;
						output.push_str(lit.to_string().as_str());
					}
				}
			}
			return Ok(Content::Literal(output));
		}
	}
}

impl Parse for HTMLTag {
	fn parse(input: ParseStream) -> syn::Result<Self> {
		// <p>
		input.parse::<Token![<]>()?;
		let left_tag = input.step(|cursor| {
			let mut rest = *cursor;
			let mut output = String::new();
			let (ident, next) = rest
				.ident()
				.expect("Expected an html like <p> or <custom-element>");
			output.push_str(ident.to_string().as_str());

			rest = next;

			// parse custom element's tag name.
			// custom elements should contain '-', however it is not required.
			while matches!(rest.punct(), Some((p, _)) if p.as_char() == '-') {
				let next = rest.punct().unwrap().1;
				rest = next;

				output.push('-');
				let (ident, next) = rest
					.ident()
					.expect("Expected an html like <p> or <custom-element>");

				output.push_str(ident.to_string().as_str());
				rest = next;
			}

			return Ok((output, rest));
		})?;

		let mut attributes = Vec::new();
		while !((input.peek(Token![/]) && input.peek2(Token![>])) || input.peek(Token![>])) {
			let is_toggle_attribute = input.parse::<Token![:]>().is_ok();

			// todo also allow 'in names'
			// let name = input.parse::<Ident>()?.to_token_stream();
			let name = input.step(|cursor| {
				let mut rest = *cursor;
				let mut output = String::new();
				let (ident, next) = rest
					.ident()
					.expect("Expected an html like <p> or <custom-element>");
				output.push_str(ident.to_string().as_str());

				rest = next;

				// parse custom element's tag name.
				// custom elements should contain '-', however it is not required.
				while matches!(rest.punct(), Some((p, _)) if p.as_char() == '-') {
					let next = rest.punct().unwrap().1;
					rest = next;

					output.push('-');
					let (ident, next) = rest
						.ident()
						.expect("Expected an html like <p> or <custom-element>");

					output.push_str(ident.to_string().as_str());
					rest = next;
				}

				return Ok((output, rest));
			})?;

			if input.parse::<Token![=]>().is_ok() {
				if let Ok(literal) = input.parse::<Literal>() {
					if is_toggle_attribute {
						panic!("Unable to have a toggle attribute from a literal");
					}

					attributes.push(Attribute {
						is_toggle_attribute,
						name,
						value: Some(proc_macro2::TokenTree::Literal(literal)),
					});
				} else if let Ok(group) = input.parse::<Group>() {
					attributes.push(Attribute {
						is_toggle_attribute,
						name,
						value: Some(proc_macro2::TokenTree::Group(group)),
					});
				} else {
					panic!("Expected a literal \"\" or a group {{}}");
				}
			} else {
				attributes.push(Attribute {
					is_toggle_attribute,
					name,
					value: None,
				});
			}
		}

		// self closing tags, like <img />
		if input.peek(Token![/]) && input.peek2(Token![>]) {
			input.parse::<Token![/]>()?;
			input.parse::<Token![>]>()?;

			return Ok(HTMLTag {
				tag: left_tag.to_string(),
				attributes,
				children: vec![],
				is_self_closing: true,
			});
		}

		input.parse::<Token![>]>()?;

		// ...
		let mut children: Vec<Content> = Vec::new();
		while !input.is_empty() && !matches_tag(input.cursor(), left_tag.to_string()) {
			let child = input.parse::<Content>()?;
			children.push(child);
		}

		// </p>
		input.parse::<Token![<]>()?;
		input.parse::<Token![/]>()?;
		// right tag
		input.step(|cursor| {
			let mut rest = *cursor;
			let mut output = String::new();
			let (ident, next) = rest
				.ident()
				.expect("Expected an html like </p> or </custom-element>");
			output.push_str(ident.to_string().as_str());

			rest = next;

			while matches!(rest.punct(), Some((p, _)) if p.as_char() == '-') {
				let next = rest.punct().unwrap().1;
				rest = next;

				output.push('-');
				let (ident, next) = rest
					.ident()
					.expect("Expected an html like </p> or </custom-element>");

				output.push_str(ident.to_string().as_str());
				rest = next;
			}

			return Ok((output, rest));
		})?;
		input.parse::<Token![>]>()?;

		Ok(HTMLTag {
			tag: left_tag.to_string(),
			attributes,
			children,
			is_self_closing: false,
		})
	}
}

fn matches_tag(cursor: Cursor, target_tag: String) -> bool {
	let mut rest = cursor;
	if !matches!(rest.punct(), Some((punct, _)) if punct.as_char() == '<') {
		return false;
	}
	let (_, next) = rest.punct().unwrap();
	rest = next;

	if !matches!(rest.punct(), Some((punct, _)) if punct.as_char() == '/') {
		return false;
	}
	let (_, next) = rest.punct().unwrap();
	rest = next;

	let mut right_hand_side = String::new();
	let (ident, next) = rest
		.ident()
		.expect("Expected an html like <p> or <custom-element>");
	right_hand_side.push_str(ident.to_string().as_str());

	rest = next;

	while matches!(rest.punct(), Some((p, _)) if p.as_char() == '-') {
		let next = rest.punct().unwrap().1;
		rest = next;

		right_hand_side.push('-');
		let (ident, next) = rest
			.ident()
			.expect("Expected an html like <p> or <custom-element>");

		right_hand_side.push_str(ident.to_string().as_str());
		rest = next;
	}

	if right_hand_side != target_tag {
		return false;
	}

	if !matches!(rest.punct(), Some((punct, _)) if punct.as_char() == '>') {
		return false;
	}

	true
}

// fn is_next_a_open_bracket(cursor: Cursor) -> bool {
//     matches!(cursor.group(), TokenTree::Group(ref group) if group.delim_span() == Delimiter::Brace)
// }

impl Parse for Component {
	fn parse(input: ParseStream) -> syn::Result<Self> {
		if input.is_empty() {
			return Ok(Component {
				children: Vec::new(),
			});
		}

		let mut children: Vec<Content> = Vec::new();

		while !input.is_empty() {
			let child = input.parse::<Content>()?;
			children.push(child);
		}

		Ok(Component { children })
	}
}

impl Parse for ControlTag {
	fn parse(input: ParseStream) -> syn::Result<Self> {
		// {#

		if input.peek(Token![for]) {
			let gr = input.parse::<Group>()?;
			// input.parse::<Token![for]>()?;
			// ...
			let mut children: Vec<Content> = Vec::new();
			while !input.is_empty() && !matches_corresponding_command_tag(input.cursor(), "for") {
				let child = input.parse::<Content>()?;
				children.push(child);
			}

			return Ok(ControlTag::Match {
				match_statement: vec![],
				cases: vec![],
			});
		} else if input.peek(Token![if]) {
		} else if input.peek(Token![match]) {
		} else {
			panic!("invalid logic block");
		}

		input.parse::<Punct>()?;
		input.parse::<Token![#]>()?;
		input.parse::<Token![<]>()?;
		let left_tag: Ident = input.parse()?;
		input.parse::<Token![>]>()?;

		// {/for}
		input.parse::<Token![<]>()?;
		input.parse::<Token![/]>()?;
		input.parse::<Ident>()?;
		input.parse::<Token![>]>()?;

		return Ok(ControlTag::Match {
			match_statement: vec![],
			cases: vec![],
		});
	}
}