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,
}
pub struct Parser<I: Iterator<Item = char>> {
tokens: std::iter::Peekable<lex::Lexer<I>>,
pub struct Parser {
openers: Vec<Container>,
events: Vec<Event>,
//tree: tree::Builder<Container, Atom>,
}
impl<I: Iterator<Item = char>> Parser<I> {
pub fn new(chars: I) -> Self {
impl Parser {
pub fn new() -> Self {
Self {
tokens: lex::Lexer::new(chars).peekable(),
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 {
lex::Kind::Seq(lex::Sequence::Dollar) => {
@ -101,7 +121,7 @@ impl<I: Iterator<Item = char>> Parser<I> {
if let Some(lex::Token {
kind: lex::Kind::Seq(lex::Sequence::Backtick),
len,
}) = self.tokens.peek()
}) = self.chars.clone().next()
{
Some((DisplayMath, *len))
} else {
@ -110,7 +130,7 @@ impl<I: Iterator<Item = char>> Parser<I> {
})
.flatten();
if math_opt.is_some() {
self.tokens.next(); // backticks
chars.next(); // backticks
}
math_opt
}
@ -119,7 +139,7 @@ impl<I: Iterator<Item = char>> Parser<I> {
};
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 tok.len >= opener_len {
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 Delimiter::*;
use Kind::*;
use Sequence::*;
use Symbol::*;
use Kind::*;
#[derive(Debug)]
pub(crate) struct Token {
@ -78,35 +78,33 @@ impl Sequence {
}
}
pub(crate) struct Lexer<I: Iterator<Item = char>> {
chars: std::iter::Peekable<I>,
pub(crate) struct Lexer<'s> {
src: &'s str,
chars: std::str::Chars<'s>,
escape: bool,
next: Option<Token>,
len: usize,
}
impl<I: Iterator<Item = char>> Lexer<I> {
pub fn new(chars: I) -> Lexer<I> {
impl<'s> Lexer<'s> {
pub fn new(src: &'s str) -> Lexer<'s> {
Lexer {
chars: chars.peekable(),
src,
chars: src.chars(),
escape: false,
next: None,
len: 0,
}
}
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> {
let c = self.chars.next();
self.len += c.map_or(0, char::len_utf8);
c
self.chars.next()
}
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) {
@ -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;
fn next(&mut self) -> Option<Self::Item> {
@ -248,14 +246,14 @@ impl<I: Iterator<Item = char>> Iterator for Lexer<I> {
#[cfg(test)]
mod test {
use super::Delimiter::*;
use super::Kind::*;
use super::Sequence::*;
use super::Symbol::*;
use super::Kind::*;
macro_rules! test_lex {
($($st:ident,)? $src:expr $(,$($token:expr),* $(,)?)?) => {
#[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),*,)?];
assert_eq!(actual, expected, "{}", $src);
};

View file

@ -83,7 +83,7 @@ impl<'s> Iterator for Iter<'s> {
let chars = (&mut self.tree)
.take_while(|ev| matches!(ev, tree::Event::Element(..)))
.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 mut eat = false;