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
use crate::{tsx_keywords::Is, TSXToken};
use iterator_endiate::EndiateIteratorExt;
use source_map::Span;
use tokenizer_lib::TokenReader;
use visitable_derive::Visitable;

use crate::{
	expressions::{ExpressionOrBlock, MultipleExpression},
	ASTNode, Keyword, TypeAnnotation,
};

#[derive(Debug, PartialEq, Eq, Clone, Visitable, get_field_by_type::GetFieldByType)]
#[cfg_attr(feature = "self-rust-tokenize", derive(self_rust_tokenize::SelfRustTokenize))]
#[cfg_attr(feature = "serde-serialize", derive(serde::Serialize))]
#[get_field_by_type_target(Span)]
pub struct IsExpression {
	pub is: Keyword<Is>,
	pub matcher: Box<MultipleExpression>,
	pub branches: Vec<(TypeAnnotation, ExpressionOrBlock)>,
	pub position: Span,
}

impl ASTNode for IsExpression {
	fn from_reader(
		reader: &mut impl tokenizer_lib::TokenReader<crate::TSXToken, crate::TokenStart>,
		state: &mut crate::ParsingState,
		options: &crate::ParseOptions,
	) -> crate::ParseResult<Self> {
		let is = Keyword::from_reader(reader)?;
		is_expression_from_reader_sub_is_keyword(reader, state, options, is)
	}

	fn to_string_from_buffer<T: source_map::ToString>(
		&self,
		buf: &mut T,
		options: &crate::ToStringOptions,
		depth: u8,
	) {
		buf.push_str("is (");
		self.matcher.to_string_from_buffer(buf, options, depth);
		buf.push_str(") {");
		for (at_end, (l, r)) in self.branches.iter().endiate() {
			l.to_string_from_buffer(buf, options, depth);
			buf.push_str(" => ");
			r.to_string_from_buffer(buf, options, depth);
			if !at_end {
				buf.push_str(", ");
			}
		}
		buf.push('}');
	}

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

pub(crate) fn is_expression_from_reader_sub_is_keyword(
	reader: &mut impl TokenReader<crate::TSXToken, crate::TokenStart>,
	state: &mut crate::ParsingState,
	options: &crate::ParseOptions,
	is: Keyword<Is>,
) -> Result<IsExpression, crate::ParseError> {
	reader.expect_next(TSXToken::OpenParentheses)?;
	let matcher = MultipleExpression::from_reader(reader, state, options)?;
	reader.expect_next(TSXToken::CloseParentheses)?;
	reader.expect_next(TSXToken::OpenBrace)?;
	let mut branches = Vec::new();
	loop {
		let type_annotation =
			TypeAnnotation::from_reader_with_config(reader, state, options, false, true)?;
		reader.expect_next(TSXToken::Arrow)?;
		let body = ExpressionOrBlock::from_reader(reader, state, options)?;
		if let Some(token) = reader.conditional_next(|t| matches!(t, TSXToken::CloseBrace)) {
			branches.push((type_annotation, body));
			return Ok(IsExpression {
				position: is.get_position().union(token.get_end()),
				is,
				matcher: Box::new(matcher),
				branches,
			});
		}
		if matches!(body, ExpressionOrBlock::Expression(..)) {
			reader.expect_next(TSXToken::Comma)?;
		}
		branches.push((type_annotation, body));
	}
}