wip parse inner

This commit is contained in:
Noah Hellman 2022-11-21 19:56:11 +01:00
parent cc59484086
commit 6c5fbc2af1
3 changed files with 48 additions and 27 deletions

View file

@ -77,22 +77,42 @@ pub enum Dir {
Both, Both,
} }
pub struct Parser<I: Iterator<Item = char>> { pub struct Parser {
tokens: std::iter::Peekable<lex::Lexer<I>>,
openers: Vec<Container>, openers: Vec<Container>,
events: Vec<Event>,
//tree: tree::Builder<Container, Atom>, //tree: tree::Builder<Container, Atom>,
} }
impl<I: Iterator<Item = char>> Parser<I> { impl Parser {
pub fn new(chars: I) -> Self { pub fn new() -> Self {
Self { Self {
tokens: lex::Lexer::new(chars).peekable(),
openers: Vec::new(), openers: Vec::new(),
events: Vec::new(),
} }
} }
pub fn parse(mut self, evs: &mut Vec<Event>) { /*
while let Some(t) = self.tokens.next() { pub fn parse(mut self, src: &str) -> impl Iterator<Event> {
todo!()
}
*/
}
struct Parse<'s> {
src: &'s str,
lexer: lex::Lexer<'s>,
events: &'s mut Vec<Event>,
}
impl<'s> Parse<'s> {
fn new(src: &'s str, events: &'s mut Vec<Event>) -> Self {
todo!()
}
/*
fn parse(mut self, src: &str, evs: &mut Vec<Event>) {
let mut chars = src.chars();
while let Some(t) = chars.next() {
{ {
let verbatim_opt = match t.kind { let verbatim_opt = match t.kind {
lex::Kind::Seq(lex::Sequence::Dollar) => { lex::Kind::Seq(lex::Sequence::Dollar) => {
@ -101,7 +121,7 @@ impl<I: Iterator<Item = char>> Parser<I> {
if let Some(lex::Token { if let Some(lex::Token {
kind: lex::Kind::Seq(lex::Sequence::Backtick), kind: lex::Kind::Seq(lex::Sequence::Backtick),
len, len,
}) = self.tokens.peek() }) = self.chars.clone().next()
{ {
Some((DisplayMath, *len)) Some((DisplayMath, *len))
} else { } else {
@ -110,7 +130,7 @@ impl<I: Iterator<Item = char>> Parser<I> {
}) })
.flatten(); .flatten();
if math_opt.is_some() { if math_opt.is_some() {
self.tokens.next(); // backticks chars.next(); // backticks
} }
math_opt math_opt
} }
@ -119,7 +139,7 @@ impl<I: Iterator<Item = char>> Parser<I> {
}; };
if let Some((atom, opener_len)) = verbatim_opt { if let Some((atom, opener_len)) = verbatim_opt {
for tok in self.tokens { for tok in chars {
if let lex::Kind::Seq(lex::Sequence::Backtick) = tok.kind { if let lex::Kind::Seq(lex::Sequence::Backtick) = tok.kind {
if tok.len >= opener_len { if tok.len >= opener_len {
break; break;
@ -188,6 +208,7 @@ impl<I: Iterator<Item = char>> Parser<I> {
} }
} }
} }
*/
} }
/* /*
@ -213,3 +234,5 @@ impl<'s> Iterator for Parser<'s> {
} }
} }
*/ */
mod test {}

View file

@ -1,9 +1,9 @@
use crate::EOF; use crate::EOF;
use Delimiter::*; use Delimiter::*;
use Kind::*;
use Sequence::*; use Sequence::*;
use Symbol::*; use Symbol::*;
use Kind::*;
#[derive(Debug)] #[derive(Debug)]
pub(crate) struct Token { pub(crate) struct Token {
@ -78,35 +78,33 @@ impl Sequence {
} }
} }
pub(crate) struct Lexer<I: Iterator<Item = char>> { pub(crate) struct Lexer<'s> {
chars: std::iter::Peekable<I>, src: &'s str,
chars: std::str::Chars<'s>,
escape: bool, escape: bool,
next: Option<Token>, next: Option<Token>,
len: usize,
} }
impl<I: Iterator<Item = char>> Lexer<I> { impl<'s> Lexer<'s> {
pub fn new(chars: I) -> Lexer<I> { pub fn new(src: &'s str) -> Lexer<'s> {
Lexer { Lexer {
chars: chars.peekable(), src,
chars: src.chars(),
escape: false, escape: false,
next: None, next: None,
len: 0,
} }
} }
fn peek(&mut self) -> char { fn peek(&mut self) -> char {
self.chars.peek().copied().unwrap_or(EOF) self.chars.clone().next().unwrap_or(EOF)
} }
fn eat(&mut self) -> Option<char> { fn eat(&mut self) -> Option<char> {
let c = self.chars.next(); self.chars.next()
self.len += c.map_or(0, char::len_utf8);
c
} }
fn len(&self) -> usize { fn len(&self) -> usize {
self.len self.src.len() - self.chars.as_str().len()
} }
fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) { fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
@ -222,7 +220,7 @@ impl<I: Iterator<Item = char>> Lexer<I> {
} }
} }
impl<I: Iterator<Item = char>> Iterator for Lexer<I> { impl<'s> Iterator for Lexer<'s> {
type Item = Token; type Item = Token;
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
@ -248,14 +246,14 @@ impl<I: Iterator<Item = char>> Iterator for Lexer<I> {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::Delimiter::*; use super::Delimiter::*;
use super::Kind::*;
use super::Sequence::*; use super::Sequence::*;
use super::Symbol::*; use super::Symbol::*;
use super::Kind::*;
macro_rules! test_lex { macro_rules! test_lex {
($($st:ident,)? $src:expr $(,$($token:expr),* $(,)?)?) => { ($($st:ident,)? $src:expr $(,$($token:expr),* $(,)?)?) => {
#[allow(unused)] #[allow(unused)]
let actual = super::Lexer::new($src.chars()).map(|t| t.kind).collect::<Vec<_>>(); let actual = super::Lexer::new($src).map(|t| t.kind).collect::<Vec<_>>();
let expected = vec![$($($token),*,)?]; let expected = vec![$($($token),*,)?];
assert_eq!(actual, expected, "{}", $src); assert_eq!(actual, expected, "{}", $src);
}; };

View file

@ -83,7 +83,7 @@ impl<'s> Iterator for Iter<'s> {
let chars = (&mut self.tree) let chars = (&mut self.tree)
.take_while(|ev| matches!(ev, tree::Event::Element(..))) .take_while(|ev| matches!(ev, tree::Event::Element(..)))
.flat_map(|ev| ev.span().of(self.src).chars()); .flat_map(|ev| ev.span().of(self.src).chars());
inline::Parser::new(chars).parse(&mut self.events); //inline::Parser::new(chars).parse(&mut self.events);
/* /*
let chars = std::iter::from_fn(|| { let chars = std::iter::from_fn(|| {
let mut eat = false; let mut eat = false;