Skip to main content

vihaco_syntax/
header.rs

1// SPDX-FileCopyrightText: 2026 The vihaco Authors
2// SPDX-License-Identifier: MIT
3
4//! Section headers as collections of consumer-defined declarations.
5
6use chumsky::prelude::*;
7use vihaco_abi::traits::FromText;
8use vihaco_bytecode::SstHeader;
9use vihaco_parser::Parse;
10
11/// Semicolon-terminated declarations parsed from one section header.
12///
13/// Available with `program-image-execution`. `D` selects the declarations that
14/// the application accepts, such as device directives and program metadata.
15/// Whitespace and line comments may separate declarations; empty headers are
16/// valid. Consumers apply each declaration to their own preparation context
17/// and decide which declarations are required and when to resolve dependencies.
18/// This collection imposes no lowering or image-construction policy.
19#[derive(Debug, Clone, PartialEq)]
20pub struct HeaderDeclarations<D>(pub Vec<D>);
21
22impl<D> FromText for HeaderDeclarations<D>
23where
24    for<'src> D: Parse<'src>,
25{
26    fn from_text(text: &str) -> eyre::Result<Self> {
27        D::parser()
28            .padded_by(crate::skip())
29            .then_ignore(just(';').padded_by(crate::skip()))
30            .repeated()
31            .collect::<Vec<_>>()
32            .then_ignore(crate::skip())
33            .then_ignore(end())
34            .parse(text)
35            .into_result()
36            .map(Self)
37            .map_err(|errors| eyre::eyre!("invalid header declarations: {errors:?}"))
38    }
39}
40
41impl<D> SstHeader for HeaderDeclarations<D> where for<'src> D: Parse<'src> {}
42
43impl<D> IntoIterator for HeaderDeclarations<D> {
44    type Item = D;
45    type IntoIter = std::vec::IntoIter<D>;
46
47    fn into_iter(self) -> Self::IntoIter {
48        self.0.into_iter()
49    }
50}
51
52#[cfg(test)]
53mod tests;