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