Skip to content

Commit

Permalink
fix pass macro hardbreak mixup
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredh159 committed Jan 5, 2025
1 parent 26ad54a commit d61e82d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
39 changes: 32 additions & 7 deletions parser/src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@ use crate::variants::token::*;

#[derive(Debug, Clone)]
pub struct Line<'arena> {
tokens: Deq<'arena, Token<'arena>>,
orig_len: u32,
pass_macro: bool,
pass_pluses: u8,
}

#[derive(Debug, Clone)]
pub struct Line1<'arena> {
tokens: Deq<'arena, Token<'arena>>,
orig_len: u32,
pass_tokens: bool,
Expand All @@ -13,34 +21,51 @@ impl<'arena> Line<'arena> {
Line {
orig_len: tokens.len() as u32,
tokens,
pass_tokens: false,
pass_macro: false,
pass_pluses: 0,
}
}

pub fn empty(bump: &'arena Bump) -> Self {
Line {
orig_len: 0,
tokens: Deq::new(bump),
pass_tokens: false,
pass_macro: false,
pass_pluses: 0,
}
}

pub fn with_capacity(capacity: usize, bump: &'arena Bump) -> Self {
Line {
orig_len: 0,
tokens: Deq::with_capacity(capacity, bump),
pass_tokens: false,
pass_macro: false,
pass_pluses: 0,
}
}

pub const fn may_contain_inline_pass(&self) -> bool {
self.pass_tokens
pub fn may_contain_inline_pass(&self) -> bool {
if !self.pass_macro && self.pass_pluses == 0 {
false
} else if self.pass_macro || self.pass_pluses > 1 {
true
} else {
let last = self.last().unwrap();
if last.is_kind_len(Plus, 1) && self.len() > 1 {
// NB: line ending ` +` is a hardbreak, cannot be pass
!self.nth_token(self.len() - 2).is_whitespaceish()
} else {
true
}
}
}

pub fn push(&mut self, token: Token<'arena>) {
match token.kind {
MacroName if token.lexeme == "pass:" => self.pass_tokens = true,
Plus if self.tokens.last().not_kind(Backtick) && token.len() < 4 => self.pass_tokens = true,
MacroName if token.lexeme == "pass:" => self.pass_macro = true,
Plus if self.tokens.last().not_kind(Backtick) && token.len() < 4 => {
self.pass_pluses = self.pass_pluses.saturating_add(1)
}
_ => {}
}
self.tokens.push(token);
Expand Down
5 changes: 1 addition & 4 deletions parser/src/tasks/parse_inlines/inline_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,7 @@ impl<'arena> Accum<'arena> {
next_token.drop_leading_bytes(1);
}
}
_ => {
dbg!(last_char, next_char);
self.push_text_token(&token);
}
_ => self.push_text_token(&token),
}
}

Expand Down
11 changes: 11 additions & 0 deletions parser/tests/all/parse_inlines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,17 @@ fn test_line_breaks() {
node!("bar"; 6..9),
],
),
(
// not confused as multi-line pass macro
"foo +\nbar +\nbaz",
nodes![
node!("foo"; 0..3),
node!(LineBreak, 3..6),
node!("bar"; 6..9),
node!(LineBreak, 9..12),
node!("baz"; 12..15),
],
),
(
"foo+\nbar", // not valid linebreak
nodes![
Expand Down
Binary file modified web-playground/public/wasm/dr_html_wasm_bg.wasm
Binary file not shown.

0 comments on commit d61e82d

Please sign in to comment.