remove iter tree / parser
This commit is contained in:
parent
660e8041b0
commit
680e8ef65f
3 changed files with 35 additions and 63 deletions
|
@ -7,7 +7,6 @@ use Container::*;
|
|||
use Leaf::*;
|
||||
|
||||
pub type Tree = tree::Tree<Block, Atom>;
|
||||
pub type TreeIter<'t> = tree::Iter<'t, Block, Atom>;
|
||||
|
||||
pub fn parse(src: &str) -> Tree {
|
||||
Parser::new(src).parse()
|
||||
|
@ -287,7 +286,7 @@ mod test {
|
|||
macro_rules! test_parse {
|
||||
($src:expr $(,$($event:expr),* $(,)?)?) => {
|
||||
let t = super::Parser::new($src).parse();
|
||||
let actual = t.iter().map(|ev| (ev.kind, ev.span.of($src))).collect::<Vec<_>>();
|
||||
let actual = t.map(|ev| (ev.kind, ev.span.of($src))).collect::<Vec<_>>();
|
||||
let expected = &[$($($event),*,)?];
|
||||
assert_eq!(actual, expected, "\n\n{}\n\n", $src);
|
||||
};
|
||||
|
|
45
src/lib.rs
45
src/lib.rs
|
@ -11,31 +11,6 @@ pub struct Block;
|
|||
|
||||
const EOF: char = '\0';
|
||||
|
||||
pub struct Parser<'s> {
|
||||
src: &'s str,
|
||||
tree: block::Tree,
|
||||
}
|
||||
|
||||
impl<'s> Parser<'s> {
|
||||
#[must_use]
|
||||
pub fn new(src: &'s str) -> Self {
|
||||
Self {
|
||||
src,
|
||||
tree: block::parse(src),
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use]
|
||||
pub fn iter(&self) -> Iter {
|
||||
Iter {
|
||||
src: self.src,
|
||||
tree: self.tree.iter(),
|
||||
parser: None,
|
||||
inline_start: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
pub enum ListType {
|
||||
Unordered,
|
||||
|
@ -76,14 +51,26 @@ pub enum Event {
|
|||
Blankline,
|
||||
}
|
||||
|
||||
pub struct Iter<'s> {
|
||||
pub struct Parser<'s> {
|
||||
src: &'s str,
|
||||
tree: block::TreeIter<'s>,
|
||||
tree: block::Tree,
|
||||
parser: Option<inline::Parser<'s>>,
|
||||
inline_start: usize,
|
||||
}
|
||||
|
||||
impl<'s> Iterator for Iter<'s> {
|
||||
impl<'s> Parser<'s> {
|
||||
#[must_use]
|
||||
pub fn new(src: &'s str) -> Self {
|
||||
Self {
|
||||
src,
|
||||
tree: block::parse(src),
|
||||
parser: None,
|
||||
inline_start: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'s> Iterator for Parser<'s> {
|
||||
type Item = Event;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
@ -137,7 +124,7 @@ mod test {
|
|||
macro_rules! test_parse {
|
||||
($($st:ident,)? $src:expr $(,$($token:expr),* $(,)?)?) => {
|
||||
#[allow(unused)]
|
||||
let actual = super::Parser::new($src).iter().collect::<Vec<_>>();
|
||||
let actual = super::Parser::new($src).collect::<Vec<_>>();
|
||||
let expected = &[$($($token),*,)?];
|
||||
assert_eq!(actual, expected, "\n\n{}\n\n", $src);
|
||||
};
|
||||
|
|
38
src/tree.rs
38
src/tree.rs
|
@ -16,25 +16,21 @@ pub struct Event<C, A> {
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Tree<C, E> {
|
||||
nodes: Vec<Node<C, E>>,
|
||||
}
|
||||
|
||||
impl<C: Copy, E: Copy> Tree<C, E> {
|
||||
fn new(nodes: Vec<Node<C, E>>) -> Self {
|
||||
Self { nodes }
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> Iter<C, E> {
|
||||
self.into()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Iter<'a, C, E> {
|
||||
nodes: &'a [Node<C, E>],
|
||||
branch: Vec<NodeIndex>,
|
||||
head: Option<NodeIndex>,
|
||||
}
|
||||
|
||||
impl<'a, C: Copy, E: Copy> Iterator for Iter<'a, C, E> {
|
||||
impl<C, E> Tree<C, E> {
|
||||
fn new(nodes: Vec<Node<C, E>>) -> Self {
|
||||
Self {
|
||||
nodes,
|
||||
branch: Vec::new(),
|
||||
head: Some(NodeIndex::root()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<C: Copy, E: Copy> Iterator for Tree<C, E> {
|
||||
type Item = Event<C, E>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
|
@ -69,16 +65,6 @@ impl<'a, C: Copy, E: Copy> Iterator for Iter<'a, C, E> {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, C, E> From<&'a Tree<C, E>> for Iter<'a, C, E> {
|
||||
fn from(tree: &'a Tree<C, E>) -> Self {
|
||||
Self {
|
||||
nodes: &tree.nodes,
|
||||
branch: Vec::new(),
|
||||
head: Some(NodeIndex::root()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
struct NodeIndex(std::num::NonZeroUsize);
|
||||
|
||||
|
@ -199,7 +185,7 @@ impl<C: Copy + std::fmt::Display, E: Copy + std::fmt::Display> std::fmt::Display
|
|||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
const INDENT: &str = " ";
|
||||
let mut level = 0;
|
||||
for e in self.iter() {
|
||||
for e in self.clone() {
|
||||
let indent = INDENT.repeat(level);
|
||||
match e.kind {
|
||||
EventKind::Enter(container) => {
|
||||
|
|
Loading…
Reference in a new issue