Starting code for iterating over the parsed ast.

This commit is contained in:
Tom Alexander 2023-04-24 22:10:24 -04:00
parent 3fc3ba58aa
commit c44e7d642f
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 42 additions and 0 deletions

View File

@ -18,6 +18,7 @@ use super::element::Element;
use super::object::Object;
use super::parser_with_context::parser_with_context;
use super::source::Source;
use super::token::Token;
use super::util::exit_matcher_parser;
use super::util::get_consumed;
use super::util::start_of_line;
@ -255,3 +256,31 @@ fn headline<'r, 's>(
fn headline_end<'r, 's>(_context: Context<'r, 's>, input: &'s str) -> Res<&'s str, &'s str> {
line_ending(input)
}
impl<'s> Document<'s> {
pub fn iter_tokens<'r>(&'r self) -> impl Iterator<Item = Token<'r, 's>> {
self.zeroth_section
.iter()
.map(Token::Section)
.chain(self.children.iter().map(Token::Heading))
}
}
impl<'s> Heading<'s> {
pub fn iter_tokens<'r>(&'r self) -> impl Iterator<Item = Token<'r, 's>> {
self.title.iter().map(Token::Object).chain(self.children.iter().map(
|de| {
match de {
DocumentElement::Heading(obj) => Token::Heading(obj),
DocumentElement::Section(obj) => Token::Section(obj),
}
}
))
}
}
impl<'s> Section<'s> {
pub fn iter_tokens<'r>(&'r self) -> impl Iterator<Item = Token<'r, 's>> {
self.children.iter().map(Token::Element)
}
}

View File

@ -35,6 +35,7 @@ pub mod sexp;
mod source;
mod table;
mod text_markup;
mod token;
mod util;
pub use document::document;
pub use document::Document;

12
src/parser/token.rs Normal file
View File

@ -0,0 +1,12 @@
use super::Document;
use super::Element;
use super::Heading;
use super::Object;
use super::Section;
pub enum Token<'r, 's> {
Heading(&'r Heading<'s>),
Section(&'r Section<'s>),
Object(&'r Object<'s>),
Element(&'r Element<'s>),
}