Skip to main content

sevenmark_parser/
core.rs

1use crate::ast::{Element, ErrorElement, Span};
2use crate::context::ParseContext;
3use crate::parser::document::document_parser;
4use crate::parser::{InputSource, ParserInput};
5use winnow::stream::Location as StreamLocation;
6
7pub fn parse_document(input: &str) -> Vec<Element> {
8    let context = ParseContext::new(input);
9
10    let mut stateful_input = ParserInput {
11        input: InputSource::new(input),
12        state: context,
13    };
14
15    match document_parser(&mut stateful_input) {
16        Ok(mut elements) => {
17            // Parse remaining content as Error element if any
18            if !stateful_input.input.is_empty() {
19                let start = stateful_input.current_token_start();
20                let remaining = stateful_input.input.to_string();
21                let end = start + remaining.len();
22
23                elements.push(Element::Error(ErrorElement {
24                    span: Span { start, end },
25                    value: remaining,
26                }));
27            }
28            elements
29        }
30        Err(_) => {
31            // If parser fails, treat entire input as single Error element
32            vec![Element::Error(ErrorElement {
33                span: Span {
34                    start: 0,
35                    end: input.len(),
36                },
37                value: input.to_string(),
38            })]
39        }
40    }
41}