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
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
#![doc = include_str!("../README.md")]
#![allow(clippy::new_without_default, clippy::too_many_lines)]

mod block;
mod comments;
pub mod cursor;
pub mod declarations;
mod errors;
pub mod expressions;
mod extensions;
pub mod functions;
pub mod generator_helpers;
mod lexer;
mod modules;
pub mod operators;
pub mod parameters;
pub mod property_key;
pub mod statements;
mod tokens;
pub mod types;
mod variable_fields;
pub mod visiting;

pub use block::{Block, BlockLike, BlockLikeMut, BlockOrSingleStatement, StatementOrDeclaration};
pub use comments::WithComment;
pub use cursor::{CursorId, EmptyCursorId};
pub use declarations::Declaration;

use errors::parse_lexing_error;
pub use errors::{ParseError, ParseErrors, ParseResult};
pub use expressions::{Expression, PropertyReference};
pub use extensions::{
	decorators::{Decorated, Decorator},
	is_expression,
	jsx::*,
};
pub use functions::{FunctionBase, FunctionBased, FunctionHeader};
pub use generator_helpers::IntoAST;
use iterator_endiate::EndiateIteratorExt;
pub use lexer::{lex_script, LexerOptions};
pub use modules::{FromFileError, Module, TypeDefinitionModule, TypeDefinitionModuleDeclaration};
pub use parameters::{FunctionParameters, Parameter, SpreadParameter};
pub use property_key::PropertyKey;
pub use source_map::{self, SourceId, Span};
pub use statements::Statement;
use temporary_annex::Annex;
pub use tokens::{tsx_keywords, TSXKeyword, TSXToken};
pub use types::{
	type_annotations::{self, TypeAnnotation},
	type_declarations::{self, GenericTypeConstraint, TypeDeclaration},
};
pub use variable_fields::*;
pub(crate) use visiting::{
	Chain, ChainVariable, VisitOptions, Visitable, VisitorMutReceiver, VisitorReceiver,
};

use tokenizer_lib::{sized_tokens::TokenEnd, Token, TokenReader};

pub(crate) use tokenizer_lib::sized_tokens::TokenStart;

use std::{borrow::Cow, str::FromStr};

/// The notation of a string
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
pub enum Quoted {
	Single,
	Double,
}

impl Quoted {
	fn as_char(self) -> char {
		match self {
			Quoted::Single => '\'',
			Quoted::Double => '"',
		}
	}
}

/// Options to customize parsing
#[allow(unused)]
#[derive(Copy, Clone)]
// TODO: Can be refactored with bit to reduce memory
#[allow(clippy::struct_excessive_bools)]
pub struct ParseOptions {
	/// Parsing of [JSX](https://facebook.github.io/jsx/) (includes some additions)
	pub jsx: bool,
	/// Allow custom characters in JSX attributes
	pub special_jsx_attributes: bool,
	/// Parses decorators on items
	pub decorators: bool,
	/// Skip **all** comments from the AST
	pub comments: Comments,
	/// See [crate::extensions::is_expression::IsExpression]
	pub is_expressions: bool,
	/// Allows functions to be prefixed with 'server'
	pub custom_function_headers: bool,
	/// For LSP allows incomplete AST for completions. TODO tidy up
	pub slots: bool,
	/// TODO temp for seeing how channel performs
	pub buffer_size: usize,
	/// TODO temp for seeing how channel performs
	///
	/// Has no effect on WASM
	pub stack_size: Option<usize>,
}

impl ParseOptions {
	fn get_lex_options(&self) -> LexerOptions {
		LexerOptions {
			comments: self.comments,
			lex_jsx: self.jsx,
			allow_unsupported_characters_in_jsx_attribute_keys: self.special_jsx_attributes,
		}
	}

	#[must_use]
	pub fn all_features() -> Self {
		Self {
			jsx: true,
			special_jsx_attributes: true,
			comments: Comments::All,
			decorators: true,
			slots: true,
			custom_function_headers: true,
			is_expressions: true,
			buffer_size: 100,
			stack_size: None,
		}
	}
}

// TODO not sure about some of these defaults, may change in future
impl Default for ParseOptions {
	fn default() -> Self {
		Self {
			jsx: true,
			special_jsx_attributes: false,
			comments: Comments::All,
			decorators: true,
			slots: false,
			custom_function_headers: false,
			is_expressions: false,
			buffer_size: 100,
			stack_size: None,
		}
	}
}

/// Settings for serializing `ASTNodes`
// TODO: Can be refactored with bit to reduce memory
#[allow(clippy::struct_excessive_bools)]
pub struct ToStringOptions {
	/// Does not include whitespace minification
	pub pretty: bool,
	/// Blocks have trailing semicolons. Has no effect if pretty == false
	pub trailing_semicolon: bool,
	/// Single statements get put on the same line as their parent statement
	pub single_statement_on_new_line: bool,
	/// Include type annotation syntax
	pub include_types: bool,
	/// TODO not sure about this
	pub include_decorators: bool,
	pub comments: Comments,
	pub indent_with: String,
	/// If false, panics if sees JSX
	pub expect_jsx: bool,
	pub expect_cursors: bool,
}

impl Default for ToStringOptions {
	fn default() -> Self {
		ToStringOptions {
			pretty: true,
			include_types: false,
			single_statement_on_new_line: true,
			include_decorators: false,
			comments: Comments::All,
			expect_jsx: false,
			trailing_semicolon: false,
			expect_cursors: false,
			indent_with: "\t".to_owned(),
		}
	}
}

impl ToStringOptions {
	#[must_use]
	pub fn minified() -> Self {
		ToStringOptions {
			pretty: false,
			comments: Comments::None,
			indent_with: String::new(),
			..Default::default()
		}
	}

	/// With typescript type syntax
	#[must_use]
	pub fn typescript() -> Self {
		ToStringOptions { include_types: true, ..Default::default() }
	}

	/// Whether to include comment in source
	pub(crate) fn should_add_comment(&self, document_comment: bool) -> bool {
		matches!(self.comments, Comments::All)
			|| (matches!(self.comments, Comments::JustDocumentation) && document_comment)
	}

	pub(crate) fn add_indent<T: source_map::ToString>(&self, indent: u8, buf: &mut T) {
		(0..indent).for_each(|_| buf.push_str(&self.indent_with));
	}

	/// Adds whitespace **conditionally** (based on pretty setting)
	pub(crate) fn add_gap<T: source_map::ToString>(&self, buf: &mut T) {
		if self.pretty {
			buf.push(' ');
		}
	}
}

#[derive(Debug, Clone, Copy)]
pub enum Comments {
	All,
	/// Only multiline comments starting with `/**`
	JustDocumentation,
	None,
}

/// Defines common methods that would exist on a AST part include position in source, creation from reader and
/// serializing to string from options.
///
/// TODO remove partial eq
pub trait ASTNode: Sized + Clone + PartialEq + std::fmt::Debug + Sync + Send + 'static {
	/// From string, with default impl to call abstract method `from_reader`
	fn from_string(
		script: String,
		options: ParseOptions,
		source: SourceId,
		offset: Option<u32>,
	) -> ParseResult<Self> {
		use source_map::LineStarts;

		// TODO take from argument
		let line_starts = LineStarts::new(script.as_str());

		lex_and_parse_script(line_starts, options, &script, source, offset, Default::default())
	}

	/// Returns position of node as span AS IT WAS PARSED. May be `Span::NULL` if AST was doesn't match anything in source
	fn get_position(&self) -> &Span;

	fn from_reader(
		reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
		state: &mut crate::ParsingState,
		options: &ParseOptions,
	) -> ParseResult<Self>;

	fn to_string_from_buffer<T: source_map::ToString>(
		&self,
		buf: &mut T,
		options: &crate::ToStringOptions,
		depth: u8,
	);

	/// Returns structure as valid string
	fn to_string(&self, options: &crate::ToStringOptions) -> String {
		let mut buf = String::new();
		self.to_string_from_buffer(&mut buf, options, 0);
		buf
	}
}

#[cfg(not(target_arch = "wasm32"))]
#[doc(hidden)]
pub fn lex_and_parse_script<T: ASTNode>(
	line_starts: source_map::LineStarts,
	options: ParseOptions,
	script: &str,
	source: SourceId,
	offset: Option<u32>,
	cursors: Vec<(usize, CursorId<()>)>,
) -> Result<T, ParseError> {
	let (mut sender, mut reader) =
		tokenizer_lib::ParallelTokenQueue::new_with_buffer_size(options.buffer_size);
	let lex_options = options.get_lex_options();
	let length = script.len() as u32;
	let mut thread = std::thread::Builder::new().name("AST parsing".into());
	if let Some(stack_size) = options.stack_size {
		thread = thread.stack_size(stack_size);
	}

	let parsing_thread = thread
		.spawn(move || {
			let mut state = ParsingState {
				line_starts,
				source,
				length_of_source: length,
				constant_imports: Default::default(),
			};
			let res = T::from_reader(&mut reader, &mut state, &options);
			if res.is_ok() {
				reader.expect_next(TSXToken::EOS)?;
			}
			res
		})
		.unwrap();

	let lex_result = lexer::lex_script(script, &mut sender, &lex_options, offset, cursors);
	if let Err((reason, pos)) = lex_result {
		return Err(ParseError::new(reason, pos));
	}
	drop(sender);
	parsing_thread.join().expect("Parsing panicked")
}

#[cfg(target_arch = "wasm32")]
#[doc(hidden)]
pub fn lex_and_parse_script<T: ASTNode>(
	line_starts: source_map::LineStarts,
	options: ParseOptions,
	script: &str,
	source: SourceId,
	offset: Option<u32>,
	cursors: Vec<(usize, CursorId<()>)>,
) -> Result<T, ParseError> {
	let mut queue = tokenizer_lib::BufferedTokenQueue::new();
	let lex_result =
		lexer::lex_script(script, &mut queue, &options.get_lex_options(), offset, cursors);

	if let Err((reason, pos)) = lex_result {
		return Err(ParseError::new(reason, pos));
	}

	let mut state = ParsingState {
		line_starts,
		length_of_source: script.len() as u32,
		source,
		constant_imports: Default::default(),
	};
	let res = T::from_reader(&mut queue, &mut state, &options);
	if res.is_ok() {
		queue.expect_next(TSXToken::EOS)?;
	}
	res
}

pub(crate) fn throw_unexpected_token<T>(
	reader: &mut impl TokenReader<TSXToken, source_map::Start>,
	expected: &[TSXToken],
) -> Result<T, ParseError> {
	throw_unexpected_token_with_token(reader.next().unwrap(), expected)
}

pub(crate) fn throw_unexpected_token_with_token<T>(
	token: Token<TSXToken, source_map::Start>,
	expected: &[TSXToken],
) -> Result<T, ParseError> {
	let position = token.get_span();
	Err(ParseError::new(ParseErrors::UnexpectedToken { expected, found: token.0 }, position))
}

#[derive(Debug)]
pub struct ParsingState {
	pub(crate) line_starts: source_map::LineStarts,
	pub(crate) source: source_map::SourceId,
	pub(crate) length_of_source: u32,
	/// TODO as multithreaded channel + record is dynamic exists
	pub(crate) constant_imports: Vec<String>,
}

/// A keyword
#[derive(Debug, Eq, Clone)]
pub struct Keyword<T: tokens::TSXKeywordNode>(pub T, pub Span);

// TODO name
#[cfg(feature = "serde-serialize")]
impl<T: tokens::TSXKeywordNode> serde::Serialize for Keyword<T> {
	fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
	where
		S: serde::Serializer,
	{
		self.1.serialize(serializer)
	}
}

// Always true as T == T
impl<T: tokens::TSXKeywordNode + PartialEq> PartialEq for Keyword<T> {
	fn eq(&self, _: &Self) -> bool {
		true
	}
}

#[cfg(feature = "self-rust-tokenize")]
impl<T: tokens::TSXKeywordNode> self_rust_tokenize::SelfRustTokenize for Keyword<T> {
	fn append_to_token_stream(
		&self,
		token_stream: &mut self_rust_tokenize::proc_macro2::TokenStream,
	) {
		let span_tokens = self_rust_tokenize::SelfRustTokenize::to_tokens(&self.1);
		token_stream.extend(self_rust_tokenize::quote!(Keyword(Default::default(), #span_tokens)));
	}
}

impl<T: tokens::TSXKeywordNode> Keyword<T>
where
	tokens::TSXKeyword: std::convert::From<T>,
{
	#[must_use]
	pub fn new(span: Span) -> Self {
		Keyword(T::default(), span)
	}

	pub(crate) fn optionally_from_reader(
		reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
	) -> Option<Self> {
		reader
			// There has to be a better way. Probably using constants
			.conditional_next(|tok| *tok == TSXToken::Keyword(TSXKeyword::from(T::default())))
			.map(|token| Keyword::new(token.get_span()))
	}

	pub(crate) fn from_reader(
		reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
	) -> ParseResult<Self> {
		match reader.next() {
			Some(token) => {
				let keyword = TSXToken::Keyword(TSXKeyword::from(T::default()));
				let span = token.get_span();
				if token.0 == keyword {
					Ok(Self::new(span))
				} else {
					Err(ParseError::new(
						ParseErrors::UnexpectedToken { expected: &[keyword], found: token.0 },
						span,
					))
				}
			}
			None => Err(parse_lexing_error()),
		}
	}

	pub fn get_position(&self) -> &Span {
		&self.1
	}
}

impl<T: tokens::TSXKeywordNode> Visitable for Keyword<T> {
	fn visit<TData>(
		&self,
		visitors: &mut (impl VisitorReceiver<TData> + ?Sized),
		data: &mut TData,
		_options: &VisitOptions,
		chain: &mut Annex<Chain>,
	) {
		visitors.visit_keyword(&(self.0.into(), &self.1), data, chain);
	}

	fn visit_mut<TData>(
		&mut self,
		_visitors: &mut (impl VisitorMutReceiver<TData> + ?Sized),
		_data: &mut TData,
		_options: &VisitOptions,
		_chain: &mut Annex<Chain>,
	) {
		// TODO should this have a implementation?
	}
}

#[derive(Debug, PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
pub enum NumberSign {
	/// Also implies non negative/missing
	Positive,
	Negative,
}

impl NumberSign {
	pub fn apply<T: std::ops::Neg<Output = T>>(&self, x: T) -> T {
		match self {
			NumberSign::Positive => x,
			NumberSign::Negative => -x,
		}
	}
}

impl std::fmt::Display for NumberSign {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		if matches!(self, Self::Negative) {
			f.write_str("-")
		} else {
			Ok(())
		}
	}
}

/// TODO a mix between runtime numbers and source syntax based number
/// TODO hex cases lost in input :(
/// <https://tc39.es/ecma262/multipage/ecmascript-language-lexical-grammar.html#sec-literals-numeric-literals>
///
/// Some of these can't be parsed, but are there to make is so that a number can be generated from just a f64
#[derive(Debug, Clone)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
pub enum NumberRepresentation {
	Infinity,
	NegativeInfinity,
	NaN,
	Hex {
		sign: NumberSign,
		identifier_uppercase: bool,
		value: u64,
	},
	Bin {
		sign: NumberSign,
		identifier_uppercase: bool,
		value: u64,
	},
	Octal {
		sign: NumberSign,
		/// None = leading 0 (boo 👎)
		identifier_uppercase: Option<bool>,
		value: u64,
	},
	Number {
		/// TODO could do as something other than f64
		value: f64,
		/// To preserve formatting
		before_point: u8,
		/// To preserve formatting
		after_point: Option<u8>,
	},
	Exponential {
		sign: NumberSign,
		value: f64,
		exponent: i32,
		identifier_uppercase: bool,
	},
	BigInt(NumberSign, String),
}

impl std::hash::Hash for NumberRepresentation {
	fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
		core::mem::discriminant(self).hash(state);
	}
}

impl TryFrom<NumberRepresentation> for f64 {
	// BigInt!!
	type Error = ();

	fn try_from(this: NumberRepresentation) -> Result<Self, Self::Error> {
		match this {
			NumberRepresentation::Infinity => Ok(f64::INFINITY),
			NumberRepresentation::NegativeInfinity => Ok(f64::NEG_INFINITY),
			NumberRepresentation::NaN => Ok(f64::NAN),
			NumberRepresentation::Number { value: internal, .. } => Ok(internal),
			NumberRepresentation::Hex { sign, value, .. }
			| NumberRepresentation::Bin { sign, value, .. }
			| NumberRepresentation::Octal { sign, value, .. } => Ok(sign.apply(value as f64)),
			NumberRepresentation::Exponential {
				sign,
				value,
				exponent,
				identifier_uppercase: _,
			} => Ok(sign.apply(value * 10f64.powi(exponent))),
			NumberRepresentation::BigInt(..) => Err(()),
		}
	}
}

// For code generation
impl From<f64> for NumberRepresentation {
	fn from(value: f64) -> Self {
		if value == f64::INFINITY {
			Self::Infinity
		} else if value == f64::NEG_INFINITY {
			Self::NegativeInfinity
		} else if value.is_nan() {
			Self::NaN
		} else {
			Self::Number {
				value,
				// These values should be fine
				before_point: 0,
				after_point: None,
			}
		}
	}
}

impl FromStr for NumberRepresentation {
	type Err = String;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		if s == "NaN" {
			return Ok(Self::NaN);
		}

		if s.contains('_') {
			return s.replace('_', "").parse();
		}

		let (sign, s) = if let Some(s) = s.strip_prefix('-') {
			(NumberSign::Negative, s)
		} else {
			(NumberSign::Positive, s)
		};

		let s = if s.contains('_') { Cow::Owned(s.replace('_', "")) } else { Cow::Borrowed(s) };

		if let Some(s) = s.strip_suffix('n') {
			Ok(NumberRepresentation::BigInt(sign, s.to_owned()))
		} else if let Some(s) = s.strip_prefix('0') {
			let next_char = s.chars().next();
			match next_char {
				Some('.') => {
					let after_point = Some(s.len() as u8 - 1);
					let before_point = 1;
					if s.len() == 1 {
						Ok(Self::Number { value: 0f64, before_point, after_point })
					} else {
						Ok(Self::Number {
							value: sign.apply(s.parse().map_err(|_| s.to_owned())?),
							before_point,
							after_point,
						})
					}
				}
				Some(c @ ('X' | 'x')) => {
					let identifier_uppercase = c.is_uppercase();
					let mut value = 0u64;
					for c in s[2..].as_bytes() {
						value <<= 4; // 16=2^4
						match c {
							b'0'..=b'9' => {
								value += u64::from(c - b'0');
							}
							b'a'..=b'f' => {
								value += u64::from(c - b'a') + 10;
							}
							b'A'..=b'F' => {
								value += u64::from(c - b'A') + 10;
							}
							_ => return Err(s.to_owned()),
						}
					}
					Ok(Self::Hex { sign, identifier_uppercase, value })
				}
				Some(c @ ('B' | 'b')) => {
					let identifier_uppercase = c.is_uppercase();
					let mut value = 0u64;
					for c in s[2..].as_bytes() {
						value <<= 1;
						match c {
							b'0' | b'1' => {
								value += u64::from(c - b'0');
							}
							_ => return Err(s.to_owned()),
						}
					}
					Ok(Self::Bin { identifier_uppercase, sign, value })
				}
				Some(c @ ('e' | 'E')) => {
					let exponent: i32 = s[1..].parse().map_err(|_| s.to_owned())?;
					Ok(Self::Exponential {
						sign,
						value: 0f64,
						exponent,
						identifier_uppercase: c.is_uppercase(),
					})
				}
				// 'o' | 'O' but can also be missed
				Some(c) => {
					let uses_character = matches!(c, 'o' | 'O');

					if !uses_character && s.contains(['8', '9', '.']) {
						let (before_point, after_point) =
							s.split_once('.').map_or((s.len() as u8 + 1, None), |(l, r)| {
								(l.len() as u8 + 1, Some(r.len() as u8))
							});

						return Ok(Self::Number {
							value: sign.apply(s.parse().map_err(|_| s.to_owned())?),
							before_point,
							after_point,
						});
					}

					// If it uses the the character then skip one, else skip zero
					let start: usize = uses_character.into();

					let mut value = 0u64;
					for c in s[start..].as_bytes() {
						value <<= 3; // 8=2^3
						if matches!(c, b'0'..=b'7') {
							value += u64::from(c - b'0');
						} else {
							return Err(s.to_owned());
						}
					}
					Ok(Self::Octal {
						sign,
						value,
						identifier_uppercase: uses_character.then_some(c.is_uppercase()),
					})
				}
				None => Ok(Self::Number { value: 0f64, before_point: 1, after_point: None }),
			}
		} else if s.starts_with('.') {
			let value: f64 = format!("0{s}").parse().map_err(|_| s.clone())?;
			Ok(Self::Number {
				value: sign.apply(value),
				before_point: 0,
				after_point: Some(s.len() as u8 - 1),
			})
		} else if let Some(s) = s.strip_suffix('.') {
			Ok(Self::Number {
				value: sign.apply(s.parse().map_err(|_| s)?),
				before_point: s.len() as u8,
				after_point: Some(0),
			})
		} else if let Some((left, right)) = s.split_once('e') {
			let value: f64 = left.parse().map_err(|_| s.clone())?;
			let exponent: i32 = right.parse().map_err(|_| s.clone())?;
			Ok(Self::Exponential { sign, value, exponent, identifier_uppercase: false })
		} else if let Some((left, right)) = s.split_once('E') {
			let value: f64 = left.parse().map_err(|_| s.clone())?;
			let exponent: i32 = right.parse().map_err(|_| s.clone())?;
			Ok(Self::Exponential { sign, value, exponent, identifier_uppercase: true })
		} else {
			let (before_point, after_point) = s
				.split_once('.')
				.map_or((s.len() as u8, None), |(l, r)| (l.len() as u8, Some(r.len() as u8)));

			Ok(Self::Number {
				value: sign.apply(s.parse().map_err(|_| s.clone())?),
				before_point,
				after_point,
			})
		}
	}
}

impl std::fmt::Display for NumberRepresentation {
	fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
		write!(f, "{}", self.clone().as_js_string())
	}
}

impl PartialEq for NumberRepresentation {
	fn eq(&self, other: &Self) -> bool {
		if let (Ok(a), Ok(b)) = (f64::try_from(self.clone()), f64::try_from(other.clone())) {
			a == b
		} else {
			// TODO ...
			false
		}
	}
}

impl Eq for NumberRepresentation {}

impl NumberRepresentation {
	#[must_use]
	pub fn as_js_string(self) -> String {
		match self {
			NumberRepresentation::Infinity => "Infinity".to_owned(),
			NumberRepresentation::NegativeInfinity => "-Infinity".to_owned(),
			NumberRepresentation::NaN => "NaN".to_owned(),
			NumberRepresentation::Hex { sign, identifier_uppercase: true, value, .. } => {
				format!("{sign}0X{value:X}")
			}
			NumberRepresentation::Hex { sign, identifier_uppercase: false, value, .. } => {
				format!("{sign}0x{value:x}")
			}
			NumberRepresentation::Bin { sign, identifier_uppercase: true, value, .. } => {
				format!("{sign}0B{value}")
			}
			NumberRepresentation::Bin { sign, identifier_uppercase: false, value, .. } => {
				format!("{sign}0b{value}")
			}
			NumberRepresentation::Octal { identifier_uppercase: None, sign, value } => {
				format!("{sign}0{value:o}")
			}
			NumberRepresentation::Octal { identifier_uppercase: Some(true), sign, value } => {
				format!("{sign}0O{value:o}")
			}
			NumberRepresentation::Octal { identifier_uppercase: Some(false), sign, value } => {
				format!("{sign}0o{value:o}")
			}
			NumberRepresentation::Number { value, after_point, before_point } => {
				let is_negative = value.is_sign_negative();
				let mut buf = value.abs().to_string();

				// TODO only `options.preserve_formatting`
				let (bp, ap) = buf
					.split_once('.')
					.map_or((buf.len() as u8, None), |(l, r)| (l.len() as u8, Some(r.len() as u8)));

				// Remove leading zero
				if bp > before_point {
					let removed = buf.remove(0);
					debug_assert_eq!(removed, '0');
				}

				(bp..before_point).for_each(|_| buf.insert(0, '0'));
				if let Some(after_point) = after_point {
					if ap.is_none() {
						buf.push('.');
					}
					(ap.unwrap_or_default()..after_point).for_each(|_| buf.push('0'));
				}
				if is_negative {
					buf.insert(0, '-');
				}
				buf
			}
			NumberRepresentation::Exponential { sign, value, exponent, identifier_uppercase } => {
				if identifier_uppercase {
					format!("{sign}{value}E{exponent}")
				} else {
					format!("{sign}{value}e{exponent}")
				}
			}
			NumberRepresentation::BigInt(s, value) => format!("{s}{value}n"),
		}
	}
}

/// Classes and `function` functions have two variants depending whether in statement position
/// or expression position
pub trait ExpressionOrStatementPosition:
	Clone + std::fmt::Debug + Sync + Send + PartialEq + Eq + 'static
{
	fn from_reader(
		reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
		state: &mut crate::ParsingState,
		options: &ParseOptions,
	) -> ParseResult<Self>;

	fn as_option_variable_identifier(&self) -> Option<&VariableIdentifier>;

	fn as_option_variable_identifier_mut(&mut self) -> Option<&mut VariableIdentifier>;

	fn as_option_str(&self) -> Option<&str> {
		if let Some(VariableIdentifier::Standard(name, _)) = self.as_option_variable_identifier() {
			Some(name)
		} else {
			None
		}
	}
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
pub struct StatementPosition(VariableIdentifier);

impl ExpressionOrStatementPosition for StatementPosition {
	fn from_reader(
		reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
		state: &mut crate::ParsingState,
		options: &ParseOptions,
	) -> ParseResult<Self> {
		VariableIdentifier::from_reader(reader, state, options).map(Self)
	}

	fn as_option_variable_identifier(&self) -> Option<&VariableIdentifier> {
		Some(&self.0)
	}

	fn as_option_variable_identifier_mut(&mut self) -> Option<&mut VariableIdentifier> {
		Some(&mut self.0)
	}
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
pub struct ExpressionPosition(Option<VariableIdentifier>);

impl ExpressionOrStatementPosition for ExpressionPosition {
	fn from_reader(
		reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
		state: &mut crate::ParsingState,
		options: &ParseOptions,
	) -> ParseResult<Self> {
		if let Some(Token(
			TSXToken::OpenBrace
			| TSXToken::OpenParentheses
			| TSXToken::Keyword(TSXKeyword::Extends),
			_,
		))
		| None = reader.peek()
		{
			Ok(Self(None))
		} else {
			Ok(Self(Some(VariableIdentifier::from_reader(reader, state, options)?)))
		}
	}

	fn as_option_variable_identifier(&self) -> Option<&VariableIdentifier> {
		self.0.as_ref()
	}

	fn as_option_variable_identifier_mut(&mut self) -> Option<&mut VariableIdentifier> {
		self.0.as_mut()
	}
}

/// Parses items surrounded in `{`, `[`, `(`, etc.
///
/// Supports trailing commas. But **does not create** *empty* like items afterwards
pub(crate) fn parse_bracketed<T: ASTNode>(
	reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
	state: &mut crate::ParsingState,
	options: &ParseOptions,
	start: Option<TSXToken>,
	end: TSXToken,
) -> ParseResult<(Vec<T>, TokenEnd)> {
	if let Some(start) = start {
		let _ = reader.expect_next(start)?;
	}
	let mut nodes: Vec<T> = Vec::new();
	loop {
		if let Some(token) = reader.conditional_next(|token| *token == end) {
			return Ok((nodes, token.get_end()));
		}
		nodes.push(T::from_reader(reader, state, options)?);
		match reader.next().ok_or_else(errors::parse_lexing_error)? {
			Token(TSXToken::Comma, _) => {}
			token => {
				if token.0 == end {
					let get_end = token.get_end();
					return Ok((nodes, get_end));
				}
				let position = token.get_span();
				return Err(ParseError::new(
					crate::ParseErrors::UnexpectedToken {
						expected: &[end, TSXToken::Comma],
						found: token.0,
					},
					position,
				));
			}
		}
	}
}

#[cfg(not(target_arch = "wasm32"))]
/// For demos and testing
pub fn script_to_tokens(source: String) -> impl Iterator<Item = (String, bool)> + 'static {
	let (mut sender, reader) = tokenizer_lib::ParallelTokenQueue::new();
	// TODO clone ...
	let input = source.clone();
	let _lexing_thread = std::thread::spawn(move || {
		let _lex_script =
			lexer::lex_script(&input, &mut sender, &Default::default(), None, Default::default());
		drop(sender);
	});

	receiver_to_tokens(reader, source)
}

#[cfg(target_arch = "wasm32")]
/// For demos and testing
pub fn script_to_tokens(source: String) -> impl Iterator<Item = (String, bool)> + 'static {
	let mut queue = tokenizer_lib::BufferedTokenQueue::new();

	let _lex_script =
		lexer::lex_script(&source, &mut queue, &Default::default(), None, Default::default());

	receiver_to_tokens(queue, source)
}

/// For testing and other features
fn receiver_to_tokens(
	mut receiver: impl TokenReader<TSXToken, TokenStart> + 'static,
	input: String,
) -> impl Iterator<Item = (String, bool)> + 'static {
	let mut last = 0u32;
	let mut last_section = None;
	std::iter::from_fn(move || {
		if last_section.is_some() {
			return last_section.take();
		}

		let token = receiver.next()?;
		if matches!(token.0, TSXToken::EOS) {
			return None;
		}
		let span = token.get_span();
		let start = span.start;
		let section = (input.get(std::ops::Range::from(span)).unwrap_or("?").to_owned(), true);
		if last == start {
			last = span.end;
			Some(section)
		} else {
			last_section = Some(section);
			let token = input.get((last as usize)..(start as usize)).unwrap_or("?").to_owned();
			last = span.end;
			Some((token, false))
		}
	})
}

/// *`to_strings`* items surrounded in `{`, `[`, `(`, etc
///
/// TODO delimiter
pub(crate) fn to_string_bracketed<T: source_map::ToString, U: ASTNode>(
	nodes: &[U],
	brackets: (char, char),
	buf: &mut T,
	options: &crate::ToStringOptions,
	depth: u8,
) {
	buf.push(brackets.0);
	for (at_end, node) in nodes.iter().endiate() {
		node.to_string_from_buffer(buf, options, depth);
		if !at_end {
			buf.push(',');
			options.add_gap(buf);
		}
	}
	buf.push(brackets.1);
}

/// Part of [ASI](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Lexical_grammar#automatic_semicolon_insertion)
pub(crate) fn expect_semi_colon(
	reader: &mut impl TokenReader<TSXToken, crate::TokenStart>,
	line_starts: &source_map::LineStarts,
	prev: u32,
) -> ParseResult<()> {
	if let Some(token) = reader.peek() {
		let Token(kind, start) = token;
		// eprintln!("{:?} {:?} {:?}", prev, next, line_starts);
		if let TSXToken::CloseBrace | TSXToken::EOS = kind {
			Ok(())
		} else if !matches!(kind, TSXToken::SemiColon)
			&& line_starts.byte_indexes_on_different_lines(prev as usize, start.0 as usize)
		{
			Ok(())
		} else {
			reader.expect_next(TSXToken::SemiColon).map(|_| ()).map_err(Into::into)
		}
	} else {
		Ok(())
	}
}

/// Re-exports or generator and general use
pub mod ast {
	pub use crate::{
		declarations::classes::*,
		declarations::*,
		expressions::*,
		extensions::jsx::*,
		functions::{
			FunctionBase, FunctionHeader, FunctionParameters, MethodHeader, Parameter,
			ParameterData, SpreadParameter,
		},
		statements::*,
		Block, Decorated, Keyword, NumberRepresentation, PropertyKey, StatementOrDeclaration,
		VariableField, VariableIdentifier, WithComment,
	};

	pub use source_map::{BaseSpan, SourceId};

	pub use self::assignments::{LHSOfAssignment, VariableOrPropertyAccess};
}

#[cfg(test)]
#[doc(hidden)]
pub(crate) mod test_utils {
	#[macro_export]
	#[allow(clippy::crate_in_macro_def)]
	macro_rules! assert_matches_ast {
		($source:literal, $ast_pattern:pat) => {{
			let node = crate::ASTNode::from_string(
				$source.to_owned(),
				Default::default(),
				crate::SourceId::NULL,
				None,
			)
			.unwrap();
			// AST matchers are partial expressions
			let matches = ::match_deref::match_deref! {
				match &node {
					$ast_pattern => true,
					_ => false,
				}
			};

			if !matches {
				panic!("{:#?} did not match {}", node, stringify!($ast_pattern));
			}
		}};
	}

	#[macro_export]
	#[allow(clippy::crate_in_macro_def)]
	macro_rules! span {
		($start:pat, $end:pat) => {
			crate::Span { start: $start, end: $end, .. }
		};
	}
}