Skip to main content

ezno_parser/types/
namespace.rs

1use get_field_by_type::GetFieldByType;
2use source_map::Span;
3use visitable_derive::Visitable;
4
5use crate::{derive_ASTNode, Block};
6
7#[apply(derive_ASTNode)]
8#[derive(Debug, Clone, PartialEq, get_field_by_type::GetFieldByType, Visitable)]
9#[get_field_by_type_target(Span)]
10pub struct Namespace {
11	pub is_declare: bool,
12	pub name: String,
13	pub inner: Block,
14	pub position: Span,
15}
16
17impl crate::ASTNode for Namespace {
18	fn from_reader(
19		reader: &mut impl tokenizer_lib::TokenReader<crate::TSXToken, crate::TokenStart>,
20		state: &mut crate::ParsingState,
21		options: &crate::ParseOptions,
22	) -> crate::ParseResult<Self> {
23		let start = reader.expect_next(crate::TSXToken::Keyword(crate::TSXKeyword::Namespace))?;
24		let (name, _) = crate::tokens::token_as_identifier(
25			reader.next().ok_or_else(crate::errors::parse_lexing_error)?,
26			"namespace name",
27		)?;
28		let inner = Block::from_reader(reader, state, options)?;
29		let position = start.union(inner.get_position());
30		Ok(Self { is_declare: false, name, inner, position })
31	}
32
33	fn to_string_from_buffer<T: source_map::ToString>(
34		&self,
35		buf: &mut T,
36		options: &crate::ToStringOptions,
37		local: crate::LocalToStringInformation,
38	) {
39		if options.include_type_annotations {
40			if self.is_declare {
41				buf.push_str("declare ");
42			}
43			buf.push_str("namespace ");
44			buf.push_str(&self.name);
45			buf.push(' ');
46			self.inner.to_string_from_buffer(buf, options, local);
47		}
48	}
49
50	fn get_position(&self) -> source_map::Span {
51		*self.get()
52	}
53}