y_lang/ast/
while_loop.rs

1use pest::iterators::Pair;
2
3use super::{Block, Expression, Position, Rule};
4
5#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
6pub struct WhileLoop<T> {
7    pub condition: Expression<T>,
8    pub block: Block<T>,
9    pub position: Position,
10    pub info: T,
11}
12
13impl WhileLoop<()> {
14    pub fn from_pair(pair: Pair<Rule>, file: &str) -> WhileLoop<()> {
15        let (line, col) = pair.line_col();
16
17        let mut inner = pair.into_inner();
18
19        let condition = Expression::from_pair(
20            inner.next().unwrap_or_else(|| {
21                panic!("Expected expression in while loop header at {line}:{col}")
22            }),
23            file,
24        );
25
26        let block = Block::from_pair(
27            inner.next().unwrap_or_else(|| {
28                panic!("Expected expression in while loop header at {line}:{col}")
29            }),
30            file,
31        );
32
33        WhileLoop {
34            condition,
35            block,
36            position: (file.to_owned(), line, col),
37            info: (),
38        }
39    }
40}