use ox_content_allocator::Allocator;
use ox_content_ast::{Document, Span};
use crate::error::ParseResult;
#[allow(unused_imports)]
use crate::profile_span;
mod block;
mod block_quote;
mod cursor;
mod fenced_code;
mod html;
mod inline;
mod inline_helpers;
mod inline_html;
mod leaf;
mod list;
mod list_item;
mod spans;
mod table;
#[cfg(test)]
mod tests;
#[derive(Debug, Clone, Default)]
pub struct ParserOptions {
pub gfm: bool,
pub footnotes: bool,
pub task_lists: bool,
pub tables: bool,
pub strikethrough: bool,
pub autolinks: bool,
pub max_nesting_depth: usize,
}
impl ParserOptions {
#[must_use]
pub fn gfm() -> Self {
Self {
gfm: true,
footnotes: true,
task_lists: true,
tables: true,
strikethrough: true,
autolinks: true,
max_nesting_depth: 100,
}
}
}
pub struct Parser<'a> {
allocator: &'a Allocator,
source: &'a str,
options: ParserOptions,
position: usize,
nesting_depth: usize,
}
impl<'a> Parser<'a> {
#[must_use]
pub fn new(allocator: &'a Allocator, source: &'a str) -> Self {
Self { allocator, source, options: ParserOptions::default(), position: 0, nesting_depth: 0 }
}
#[must_use]
pub fn with_options(allocator: &'a Allocator, source: &'a str, options: ParserOptions) -> Self {
Self { allocator, source, options, position: 0, nesting_depth: 0 }
}
pub fn parse(mut self) -> ParseResult<Document<'a>> {
profile_span!("parser::parse");
let mut children = self.allocator.new_vec();
while !self.is_at_end() {
if let Some(node) = self.parse_block()? {
children.push(node);
}
}
let span = Span::new(0, self.source.len() as u32);
Ok(Document { children, span })
}
}