mylang_parser/parse/
program.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
use itertools::PutBack;
use mylang_ast::Stmt;
use mylang_token::{Locatable, Token};

use super::stmt::stmt;
use crate::result::{ParseErr, ParseResult};

pub fn program(tokens: &mut PutBack<impl Iterator<Item = Token>>) -> Vec<ParseResult<Stmt>> {
    let mut results = Vec::<Result<Stmt, ParseErr>>::new();

    while let Some(tok) = tokens.next() {
        match tok {
            Token::Newline(_) => (),

            _ => {
                let pos = tok.locate().end();
                tokens.put_back(tok);
                results.push(stmt(tokens, pos));
            }
        }
    }

    results
}