glsl_lang_pp/processor/
str.rs

1use thiserror::Error;
2
3use lang_util::{
4    located::{Located, LocatedBuilder},
5    FileId,
6};
7
8use crate::{last::LocatedIterator, parser, types::path::ParsedPath};
9
10use super::{
11    event::Event,
12    expand::{ExpandEvent, ExpandOne},
13    ProcessorState,
14};
15
16pub fn parse(input: &str) -> parser::Ast {
17    parser::Parser::new(input).parse()
18}
19
20#[derive(Debug, PartialEq, Eq, Error)]
21pub enum ProcessStrError {
22    #[error("an include was requested without a filesystem context")]
23    IncludeRequested(ParsedPath),
24}
25
26pub fn process(input: &str, state: ProcessorState) -> ExpandStr {
27    let file_id = FileId::new(0);
28    let ast = parser::Parser::new(input).parse();
29    ExpandStr {
30        inner: ExpandOne::new((file_id, ast), state),
31        final_state: None,
32    }
33}
34
35pub struct ExpandStr {
36    inner: ExpandOne,
37    final_state: Option<ProcessorState>,
38}
39
40impl ExpandStr {
41    pub fn tokenize(
42        self,
43        current_version: u16,
44        target_vulkan: bool,
45        registry: &crate::exts::Registry,
46    ) -> crate::last::Tokenizer<'_, Self> {
47        crate::last::Tokenizer::new(self, current_version, target_vulkan, registry)
48    }
49
50    pub fn into_state(mut self) -> Option<ProcessorState> {
51        self.final_state.take()
52    }
53}
54
55impl Iterator for ExpandStr {
56    type Item = Result<Event, Located<ProcessStrError>>;
57
58    fn next(&mut self) -> Option<Self::Item> {
59        let event = self.inner.next()?;
60        match event {
61            ExpandEvent::Event(event) => Some(Ok(event)),
62            ExpandEvent::EnterFile(node, path) => Some(Err(LocatedBuilder::new()
63                .pos(node.text_range())
64                .resolve_file(self.inner.location())
65                .finish(ProcessStrError::IncludeRequested(path)))),
66            ExpandEvent::Completed(state) => {
67                self.final_state = Some(state);
68                None
69            }
70        }
71    }
72}
73
74impl LocatedIterator for ExpandStr {
75    fn location(&self) -> &crate::processor::expand::ExpandLocation {
76        self.inner.location()
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    fn assert_send<T: Send>() {}
83
84    #[test]
85    fn test_error_send() {
86        assert_send::<super::ProcessStrError>();
87    }
88}