ezno_parser/statements/
while_statement.rs1use source_map::Span;
2use visitable_derive::Visitable;
3
4use crate::{
5 ast::MultipleExpression, block::BlockOrSingleStatement, derive_ASTNode, ASTNode, TSXKeyword,
6 TSXToken,
7};
8
9#[apply(derive_ASTNode)]
10#[derive(Debug, PartialEq, Clone, Visitable, get_field_by_type::GetFieldByType)]
11#[get_field_by_type_target(Span)]
12pub struct WhileStatement {
13 pub condition: MultipleExpression,
14 pub inner: BlockOrSingleStatement,
15 pub position: Span,
16}
17
18impl ASTNode for WhileStatement {
19 fn get_position(&self) -> Span {
20 self.position
21 }
22
23 fn from_reader(
24 reader: &mut impl tokenizer_lib::TokenReader<TSXToken, crate::TokenStart>,
25 state: &mut crate::ParsingState,
26 options: &crate::ParseOptions,
27 ) -> Result<Self, crate::ParseError> {
28 let start = state.expect_keyword(reader, TSXKeyword::While)?;
29 reader.expect_next(TSXToken::OpenParentheses)?;
30 let condition = MultipleExpression::from_reader(reader, state, options)?;
31 reader.expect_next(TSXToken::CloseParentheses)?;
32 let inner = BlockOrSingleStatement::from_reader(reader, state, options)?;
33 Ok(Self { position: start.union(inner.get_position()), condition, inner })
34 }
35
36 fn to_string_from_buffer<T: source_map::ToString>(
37 &self,
38 buf: &mut T,
39 options: &crate::ToStringOptions,
40 local: crate::LocalToStringInformation,
41 ) {
42 buf.push_str("while");
43 options.push_gap_optionally(buf);
44 buf.push('(');
45 self.condition.to_string_from_buffer(buf, options, local);
46 buf.push(')');
47 options.push_gap_optionally(buf);
48 self.inner.to_string_from_buffer(buf, options, local.next_level());
49 }
50}
51
52#[apply(derive_ASTNode)]
53#[derive(Debug, PartialEq, Clone, Visitable, get_field_by_type::GetFieldByType)]
54#[get_field_by_type_target(Span)]
55pub struct DoWhileStatement {
56 pub condition: MultipleExpression,
57 pub inner: BlockOrSingleStatement,
59 pub position: Span,
60}
61
62impl ASTNode for DoWhileStatement {
63 fn get_position(&self) -> Span {
64 self.position
65 }
66
67 fn from_reader(
68 reader: &mut impl tokenizer_lib::TokenReader<crate::TSXToken, crate::TokenStart>,
69 state: &mut crate::ParsingState,
70 options: &crate::ParseOptions,
71 ) -> Result<Self, crate::ParseError> {
72 let start = state.expect_keyword(reader, TSXKeyword::Do)?;
73 let inner = BlockOrSingleStatement::from_reader(reader, state, options)?;
74 let _ = state.expect_keyword(reader, TSXKeyword::While)?;
75 reader.expect_next(TSXToken::OpenParentheses)?;
76 let condition = MultipleExpression::from_reader(reader, state, options)?;
77 let position =
78 start.union(reader.expect_next(TSXToken::CloseParentheses)?.get_end_after(1));
79 Ok(Self { condition, inner, position })
80 }
81
82 fn to_string_from_buffer<T: source_map::ToString>(
83 &self,
84 buf: &mut T,
85 options: &crate::ToStringOptions,
86 local: crate::LocalToStringInformation,
87 ) {
88 buf.push_str("do ");
89 self.inner.to_string_from_buffer(buf, options, local);
90 buf.push_str(" while");
91 options.push_gap_optionally(buf);
92 buf.push('(');
93 self.condition.to_string_from_buffer(buf, options, local);
94 buf.push(')');
95 }
96}