pluto-src 0.1.1+0.10.4

Sources of Pluto (Lua 5.4 dialect) and logic to build it.
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#include "xml.hpp"

#include "Exception.hpp"
#include "string.hpp"
#include "StringBuilder.hpp"
#include "UniquePtr.hpp"
#include "utility.hpp" // SOUP_MOVE_RETURN

#define DEBUG_PARSE false

#if DEBUG_PARSE
#include <iostream>
#endif

NAMESPACE_SOUP
{
	XmlMode xml::MODE_XML{};
	XmlMode xml::MODE_LAX_XML{ {}, true, true };
	XmlMode xml::MODE_HTML{ { "area", "base", "br", "col", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr" }, true, true };

	UniquePtr<XmlTag> xml::parseAndDiscardMetadata(const std::string& xml, const XmlMode& mode)
	{
		return parseAndDiscardMetadata(xml.data(), &xml.data()[xml.size()], mode);
	}

	UniquePtr<XmlTag> xml::parseAndDiscardMetadata(const char* begin, const char* end, const XmlMode& mode)
	{
		auto nodes = parse(begin, end, mode);

		for (auto i = nodes.begin(); i != nodes.end(); )
		{
			if ((*i)->isTag())
			{
				XmlTag& tag = (*i)->asTag();
				if (tag.name.c_str()[0] == '?'
					|| tag.name.c_str()[0] == '!'
					)
				{
					i = nodes.erase(i);
					continue;
				}
			}
			++i;
		}

		if (nodes.size() == 1
			&& nodes.at(0)->isTag()
			)
		{
			SOUP_MOVE_RETURN(nodes.at(0));
		}

		auto body = soup::make_unique<XmlTag>();
		body->name = "body";
		body->children = std::move(nodes);
		return body;
	}

	std::vector<UniquePtr<XmlNode>> xml::parse(const std::string& xml, const XmlMode& mode)
	{
		return parse(xml.data(), &xml.data()[xml.size()], mode);
	}

	std::vector<UniquePtr<XmlNode>> xml::parse(const char* begin, const char* end, const XmlMode& mode)
	{
		std::vector<UniquePtr<XmlNode>> res{};

		auto i = begin;
		do
		{
			if (auto node = parseImpl(i, end, mode, 1000))
			{
				res.emplace_back(std::move(node));
			}
		} while (i != end);

		return res;
	}

	UniquePtr<XmlNode> xml::parseImpl(const char*& i, const char* end, const XmlMode& mode, int max_depth)
	{
		SOUP_ASSERT(max_depth != 0, "Depth limit exceeded");

		while (i != end && string::isSpace(*i))
		{
			++i;
		}
		if (i == end)
		{
			return {};
		}
		UniquePtr<XmlTag> tag;
		if (*i == '<')
		{
			++i;
			StringBuilder name_builder;
			name_builder.beginCopy(i);
			for (;; ++i)
			{
				if (i == end)
				{
					return tag;
				}
				if (*i == ' ' || *i == '/' || *i == '>')
				{
					break;
				}
			}
			name_builder.endCopy(i);
			tag = soup::make_unique<XmlTag>();
			tag->name = std::move(name_builder);
			if (*i != '>')
			{
				// Attributes
				StringBuilder name;
				name.beginCopy(i);
				for (;; ++i)
				{
					if (i == end)
					{
						return tag;
					}
					if (*i == '>')
					{
						name.endCopy(i);
						if (!name.empty())
						{
							if (mode.empty_attribute_syntax)
							{
								tag->attributes.emplace_back(std::move(name), std::string());
							}
							name.clear();
						}
						break;
					}
					if (*i == ' ')
					{
						name.endCopy(i);
						if (!name.empty())
						{
							if (mode.empty_attribute_syntax)
							{
								tag->attributes.emplace_back(std::move(name), std::string());
							}
							name.clear();
						}
						name.beginCopy(i + 1);
					}
					else if (*i == '/')
					{
						name.endCopy(i);
						if (!name.empty())
						{
							if (mode.empty_attribute_syntax)
							{
								tag->attributes.emplace_back(std::move(name), std::string());
							}
							name.clear();
						}
						if (mode.self_closing_tags.empty()
							&& (i + 1) != end
							&& *(i + 1) == '>'
							)
						{
#if DEBUG_PARSE
							std::cout << tag->name << " taking the easy way out" << std::endl;
#endif
							i += 2;
							return tag;
						}
						name.beginCopy(i + 1);
					}
					else if (*i == '=')
					{
						name.endCopy(i);
						StringBuilder value;
						++i;
						if (i != end && *i == '"')
						{
#if DEBUG_PARSE
							std::cout << "Collecting value for attribute " << name << ": ";
#endif
							++i;
							value.beginCopy(i);
							for (;; ++i)
							{
								if (i == end)
								{
									return tag;
								}
								if (*i == '"')
								{
									break;
								}
#if DEBUG_PARSE
								std::cout << *i;
#endif
							}
#if DEBUG_PARSE
							std::cout << std::endl;
#endif
							value.endCopy(i);
							tag->attributes.emplace_back(std::move(name), std::move(value));
						}
						else if (i != end && *i == '\'')
						{
#if DEBUG_PARSE
							std::cout << "Collecting value for attribute " << name << ": ";
#endif
							++i;
							value.beginCopy(i);
							for (;; ++i)
							{
								if (i == end)
								{
									return tag;
								}
								if (*i == '\'')
								{
									break;
								}
#if DEBUG_PARSE
								std::cout << *i;
#endif
							}
#if DEBUG_PARSE
							std::cout << std::endl;
#endif
							value.endCopy(i);
							tag->attributes.emplace_back(std::move(name), std::move(value));
						}
						else if (i != end && string::isAlphaNum(*i) && mode.unquoted_attributes)
						{
#if DEBUG_PARSE
							std::cout << "Collecting value for attribute " << name << ": ";
#endif
							value.beginCopy(i);
							for (;; ++i)
							{
								if (i == end)
								{
									return tag;
								}
								if (!string::isAlphaNum(*i))
								{
									break;
								}
#if DEBUG_PARSE
								std::cout << *i;
#endif
							}
#if DEBUG_PARSE
							std::cout << std::endl;
#endif
							value.endCopy(i);
							tag->attributes.emplace_back(std::move(name), std::move(value));

						}
						else
						{
#if DEBUG_PARSE
							std::cout << "Attribute " << name << " has no value";
#endif
							if (mode.empty_attribute_syntax)
							{
								tag->attributes.emplace_back(std::move(name), std::string());
							}
						}
						name.clear();
						name.beginCopy(i + 1);
					}
				}
			}
			++i;
			if (tag->name.c_str()[0] == '?' // <?xml ... ?>
				|| tag->name.c_str()[0] == '!' // <!DOCTYPE ...>
#if SOUP_CPP20
				|| mode.self_closing_tags.contains(tag->name)
#else
				|| mode.self_closing_tags.count(tag->name)
#endif
				)
			{
				return tag; // Won't have children
			}
		}
		StringBuilder text;
		while (i != end && string::isSpace(*i))
		{
			++i;
		}
		text.beginCopy(i);
		for (; i != end; ++i)
		{
			if (*i == '<')
			{
				text.endCopy(i);
#if DEBUG_PARSE
				if (!text.empty())
				{
					std::cout << "Copied text: " << text << std::endl;
				}
#endif
				if (!tag)
				{
					return soup::make_unique<XmlText>(std::move(text));
				}

				if ((i + 1) != end
					&& *(i + 1) == '/'
					)
				{
					if (!text.empty())
					{
#if DEBUG_PARSE
						std::cout << "Discharging XmlText for tag close: " << text << std::endl;
#endif
						tag->children.emplace_back(soup::make_unique<XmlText>(std::move(text)));
						text.clear();
					}

					i += 2;
					StringBuilder tbc_tag;
					tbc_tag.beginCopy(i);
					for (; i != end; ++i)
					{
						if (*i == '>')
						{
							break;
						}
					}
					tbc_tag.endCopy(i);
					++i;
#if DEBUG_PARSE
					std::cout << "tbc tag: " << tbc_tag << std::endl;
#endif
					if (tbc_tag != tag->name)
					{
#if DEBUG_PARSE
						std::cout << "Expected tbc tag to be " << tag->name << std::endl;
#endif
						i -= tbc_tag.length();
						i -= 3;
#if DEBUG_PARSE
						std::cout << "Cursor now at " << *i << ", unwinding" << std::endl;
#endif
						return tag;
					}
					text.beginCopy(i);
					break;
				}

				// Handle CDATA sections
				if ((i + 1) != end && *(i + 1) == '!'
					&& (i + 2) != end && *(i + 2) == '['
					&& (i + 3) != end && *(i + 3) == 'C'
					&& (i + 4) != end && *(i + 4) == 'D'
					&& (i + 5) != end && *(i + 5) == 'A'
					&& (i + 6) != end && *(i + 6) == 'T'
					&& (i + 7) != end && *(i + 7) == 'A'
					&& (i + 8) != end && *(i + 8) == '['
					)
				{
					i += 9;
					text.beginCopy(i);
					for (; i != end; ++i)
					{
						if (*i == ']'
							&& (i + 1) != end && *(i + 1) == ']'
							&& (i + 2) != end && *(i + 2) == '>'
							)
						{
							text.endCopy(i);
							i += 3;
							break;
						}
					}
#if DEBUG_PARSE
					if (!text.empty())
					{
						std::cout << "Copied text from CDATA section: " << text << std::endl;
					}
#endif
					text.beginCopy(i);
					--i; // Cursor is already in the right place but for loop will do `++i`
					continue;
				}

				if (!text.empty())
				{
#if DEBUG_PARSE
					std::cout << "Discharging XmlText for nested tag: " << text << std::endl;
#endif
					tag->children.emplace_back(soup::make_unique<XmlText>(std::move(text)));
					text.clear();
				}
#if DEBUG_PARSE
				auto child = parseImpl(i, end, mode, max_depth - 1);
				std::cout << "Recursed for " << child->encode() << std::endl;
				tag->children.emplace_back(std::move(child));
#else
				tag->children.emplace_back(parseImpl(i, end, mode, max_depth - 1));
#endif
				if (i == end)
				{
					text.beginCopy(i);
					break;
				}
				while (string::isSpace(*i) && ++i != end);
				text.beginCopy(i);
				--i; // Cursor is already in the right place but for loop will do `++i`
			}
		}
		text.endCopy(i);
		if (!tag)
		{
			return soup::make_unique<XmlText>(std::move(text));
		}
		if (!text.empty())
		{
#if DEBUG_PARSE
			std::cout << "Discharging XmlText for return: " << text << std::endl;
#endif
			tag->children.emplace_back(soup::make_unique<XmlText>(std::move(text)));
			text.clear();
		}
		return tag;
	}

	void XmlNode::encodeAndAppendTo(std::string& str, const XmlMode& mode) const SOUP_EXCAL
	{
		if (is_text)
		{
			static_cast<const XmlText*>(this)->encodeAndAppendTo(str);
		}
		else
		{
			static_cast<const XmlTag*>(this)->encodeAndAppendTo(str, mode);
		}
	}

	void XmlNode::encodePrettyAndAppendTo(std::string& str, const XmlMode& mode, unsigned depth) const SOUP_EXCAL
	{
		if (is_text)
		{
			static_cast<const XmlText*>(this)->encodeAndAppendTo(str);
		}
		else
		{
			static_cast<const XmlTag*>(this)->encodePrettyAndAppendTo(str, mode, depth);
		}
	}

	bool XmlNode::isTag() const noexcept
	{
		return !is_text;
	}

	bool XmlNode::isText() const noexcept
	{
		return is_text;
	}

	XmlTag& XmlNode::asTag()
	{
		if (!isTag())
		{
			SOUP_THROW(Exception("XmlNode has unexpected type"));
		}
		return *static_cast<XmlTag*>(this);
	}

	XmlText& XmlNode::asText()
	{
		if (!isText())
		{
			SOUP_THROW(Exception("XmlNode has unexpected type"));
		}
		return *static_cast<XmlText*>(this);
	}

	const XmlTag& XmlNode::asTag() const
	{
		if (!isTag())
		{
			SOUP_THROW(Exception("XmlNode has unexpected type"));
		}
		return *static_cast<const XmlTag*>(this);
	}

	const XmlText& XmlNode::asText() const
	{
		if (!isText())
		{
			SOUP_THROW(Exception("XmlNode has unexpected type"));
		}
		return *static_cast<const XmlText*>(this);
	}

	void XmlTag::encodeAndAppendTo(std::string& str, const XmlMode& mode) const SOUP_EXCAL
	{
#if SOUP_CPP20
		const bool is_self_closing = mode.self_closing_tags.contains(name);
#else
		const bool is_self_closing = mode.self_closing_tags.count(name);
#endif
		str.push_back('<');
		str.append(name);
		encodeAttributesAndAppendTo(str, mode);
		if (is_self_closing)
		{
			str.append(" /");
		}
		str.push_back('>');
		for (const auto& child : children)
		{
			child->encodeAndAppendTo(str, mode);
		}
		if (!is_self_closing)
		{
			str.append("</");
			str.append(name);
			str.push_back('>');
		}
	}

	void XmlTag::encodePrettyAndAppendTo(std::string& str, const XmlMode& mode, unsigned depth) const SOUP_EXCAL
	{
#if SOUP_CPP20
		const bool is_self_closing = mode.self_closing_tags.contains(name);
#else
		const bool is_self_closing = mode.self_closing_tags.count(name);
#endif
		str.push_back('<');
		str.append(name);
		encodeAttributesAndAppendTo(str, mode);
		if (is_self_closing)
		{
			str.append(" /");
		}
		str.push_back('>');
		const auto child_depth = (depth + 1);
		for (const auto& child : children)
		{
			str.push_back('\n');
			str.append(child_depth * 4, ' ');
			child->encodePrettyAndAppendTo(str, mode, child_depth);
		}
		if (!is_self_closing)
		{
			if (!children.empty())
			{
				str.push_back('\n');
				str.append(depth * 4, ' ');
			}
			str.append("</");
			str.append(name);
			str.push_back('>');
		}
	}

	void XmlTag::encodeAttributesAndAppendTo(std::string& str, const XmlMode& mode) const SOUP_EXCAL
	{
		for (const auto& e : attributes)
		{
			str.push_back(' ');
			//str.push_back('{');
			str.append(e.first);
			//str.push_back('}');
			if (!e.second.empty()
				|| !mode.empty_attribute_syntax
				)
			{
				str.push_back('=');
				if (e.second.find('"') != std::string::npos)
				{
					str.push_back('\'');
					str.append(e.second);
					str.push_back('\'');
				}
				else
				{
					str.push_back('"');
					str.append(e.second);
					str.push_back('"');
				}
			}
		}
	}

	bool XmlTag::hasAttribute(const std::string& name) const noexcept
	{
		for (const auto& a : attributes)
		{
			if (a.first == name)
			{
				return true;
			}
		}
		return false;
	}

	const std::string& XmlTag::getAttribute(const std::string& name) const
	{
		for (const auto& a : attributes)
		{
			if (a.first == name)
			{
				return a.second;
			}
		}
		SOUP_ASSERT_UNREACHABLE;
	}

	XmlTag* XmlTag::findTag(const std::string& name_target)
	{
		if (name == name_target)
		{
			return this;
		}
		XmlTag* res = nullptr;
		for (const auto& child : children)
		{
			if (!child->is_text)
			{
				res = static_cast<XmlTag*>(child.get())->findTag(name_target);
				if (res != nullptr)
				{
					break;
				}
			}
		}
		return res;
	}

	XmlText::XmlText(std::string&& contents) noexcept
		: XmlNode(true), contents(std::move(contents))
	{
		string::replaceAll(this->contents, "&amp;", "&");
		string::replaceAll(this->contents, "&lt;", "<");
		string::replaceAll(this->contents, "&gt;", ">");
	}

	void XmlText::encodeAndAppendTo(std::string& str) const SOUP_EXCAL
	{
		std::string contents = this->contents;
		string::replaceAll(contents, "&", "&amp;");
		string::replaceAll(contents, "<", "&lt;");
		string::replaceAll(contents, ">", "&gt;");
		str.append(contents);
	}
}