inline: allow reading src

This commit is contained in:
Noah Hellman 2023-02-25 15:37:23 +01:00
parent 8382fe122f
commit 1b7bb25519
2 changed files with 19 additions and 20 deletions

View file

@ -74,8 +74,9 @@ pub struct Event {
} }
#[derive(Clone)] #[derive(Clone)]
pub struct Input<'s> { struct Input<'s> {
/// Lexer, hosting source. src: &'s str,
/// Lexer.
lexer: lex::Lexer<'s>, lexer: lex::Lexer<'s>,
/// The block is complete, the final line has been provided. /// The block is complete, the final line has been provided.
complete: bool, complete: bool,
@ -84,23 +85,24 @@ pub struct Input<'s> {
} }
impl<'s> Input<'s> { impl<'s> Input<'s> {
fn new() -> Self { fn new(src: &'s str) -> Self {
Self { Self {
src,
lexer: lex::Lexer::new(""), lexer: lex::Lexer::new(""),
complete: false, complete: false,
span: Span::empty_at(0), span: Span::empty_at(0),
} }
} }
fn feed_line(&mut self, line: &'s str, offset: usize, last: bool) { fn feed_line(&mut self, line: Span, last: bool) {
debug_assert!(!self.complete); debug_assert!(!self.complete);
self.lexer = lex::Lexer::new(line); self.lexer = lex::Lexer::new(line.of(self.src));
self.complete = last; self.complete = last;
self.span = Span::empty_at(offset); self.span = line.empty_before();
} }
fn reset(&mut self) { fn reset(&mut self) {
*self = Self::new(); *self = Self::new(self.src);
} }
fn eat(&mut self) -> Option<lex::Token> { fn eat(&mut self) -> Option<lex::Token> {
@ -197,17 +199,17 @@ pub struct Parser<'s> {
} }
impl<'s> Parser<'s> { impl<'s> Parser<'s> {
pub fn new() -> Self { pub fn new(src: &'s str) -> Self {
Self { Self {
input: Input::new(), input: Input::new(src),
openers: Vec::new(), openers: Vec::new(),
events: std::collections::VecDeque::new(), events: std::collections::VecDeque::new(),
verbatim: None, verbatim: None,
} }
} }
pub fn feed_line(&mut self, line: &'s str, offset: usize, last: bool) { pub fn feed_line(&mut self, line: Span, last: bool) {
self.input.feed_line(line, offset, last); self.input.feed_line(line, last);
} }
pub fn reset(&mut self) { pub fn reset(&mut self) {
@ -899,8 +901,8 @@ 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 mut p = super::Parser::new(); let mut p = super::Parser::new($src);
p.feed_line($src, 0, true); p.feed_line(super::Span::by_len(0, $src.len()), true);
let actual = p.map(|ev| (ev.kind, ev.span.of($src))).collect::<Vec<_>>(); let actual = p.map(|ev| (ev.kind, ev.span.of($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);

View file

@ -672,7 +672,7 @@ impl<'s> PrePass<'s> {
let inlines = tree.take_inlines().collect::<Vec<_>>(); let inlines = tree.take_inlines().collect::<Vec<_>>();
inline_parser.reset(); inline_parser.reset();
inlines.iter().enumerate().for_each(|(i, sp)| { inlines.iter().enumerate().for_each(|(i, sp)| {
inline_parser.feed_line(sp.of(src), sp.start(), i == inlines.len() - 1); inline_parser.feed_line(*sp, i == inlines.len() - 1);
inline_parser.for_each(|ev| match ev.kind { inline_parser.for_each(|ev| match ev.kind {
inline::EventKind::Str => { inline::EventKind::Str => {
let mut chars = ev.span.of(src).chars().peekable(); let mut chars = ev.span.of(src).chars().peekable();
@ -774,7 +774,7 @@ impl<'s> Parser<'s> {
#[must_use] #[must_use]
pub fn new(src: &'s str) -> Self { pub fn new(src: &'s str) -> Self {
let tree = block::parse(src); let tree = block::parse(src);
let mut inline_parser = inline::Parser::new(); let mut inline_parser = inline::Parser::new(src);
let pre_pass = PrePass::new(src, tree.clone(), &mut inline_parser); let pre_pass = PrePass::new(src, tree.clone(), &mut inline_parser);
Self { Self {
@ -1044,11 +1044,8 @@ impl<'s> Parser<'s> {
if self.verbatim { if self.verbatim {
Event::Str(content.into()) Event::Str(content.into())
} else { } else {
self.inline_parser.feed_line( self.inline_parser
content, .feed_line(ev.span, self.tree.branch_is_empty());
ev.span.start(),
self.tree.branch_is_empty(),
);
return self.next(); return self.next();
} }
} }