Add closing function on content syntax parser

This provides saner, more testable, properly closed output even without
a trailing newline.
This commit is contained in:
Juno Takano 2025-12-26 01:18:38 -03:00
commit da0ed3f2b3

View file

@ -1,6 +1,6 @@
use std::collections::{HashMap};
use crate::{syntax::serial::populate_graph, types::Config};
use crate::{prelude::*, syntax::serial::populate_graph, types::Config};
use super::{Parseable as _, Token, LexMap};
use token::{
anchor::Anchor, linebreak::LineBreak, paragraph::Paragraph, header::Header,
@ -168,6 +168,7 @@ fn lex(text: &str, map: LexMap) -> Vec<Token> {
}
}
close(&state, &mut tokens);
tokens
}
@ -233,6 +234,18 @@ impl State {
}
}
fn close(state: &State, tokens: &mut Vec<Token>) {
match state.context.block {
BlockContext::Paragraph => {
tokens.push(Token::Paragraph(Paragraph::new(false)));
},
BlockContext::Header(_) => panic!("End of file with open header"),
BlockContext::PreFormat => panic!("End of file with open preformat"),
BlockContext::None => log!("End of file on None block context"),
}
}
fn parse(tokens: &[Token]) -> String {
tokens.iter().map(Token::render).collect::<String>()
}