Skip to content

Commit

Permalink
inline: parse multiline attributes
Browse files Browse the repository at this point in the history
reimplement after broken by "take str per line instead of full inline
iter" commit

also resolves #18
  • Loading branch information
hellux committed Mar 20, 2023
1 parent bb65a06 commit 4f69614
Show file tree
Hide file tree
Showing 5 changed files with 309 additions and 114 deletions.
42 changes: 36 additions & 6 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@ impl<'s> AttributeValue<'s> {
pub fn parts(&'s self) -> AttributeValueParts<'s> {
AttributeValueParts { ahead: &self.raw }
}

fn extend(&mut self, s: &'s str) {
match &mut self.raw {
CowStr::Borrowed(prev) => {
self.raw = format!("{} {}", prev, s).into();
}
CowStr::Owned(ref mut prev) => {
prev.push(' ');
prev.push_str(s);
}
}
}
}

impl<'s> From<&'s str> for AttributeValue<'s> {
Expand Down Expand Up @@ -233,6 +245,18 @@ impl<'a, 's> Parser<'a, 's> {
}
}

pub fn restart(&mut self) {
self.state = State::Start;
}

pub fn set_input(&mut self, input: &'s str) {
debug_assert_eq!(self.chars.next(), None);
self.input = input;
self.chars = input.chars();
self.pos = 0;
self.pos_prev = 0;
}

pub fn step(&mut self) -> StepResult {
self.chars.next().map_or(StepResult::More, |c| {
use State::*;
Expand Down Expand Up @@ -272,12 +296,14 @@ impl<'a, 's> Parser<'a, 's> {
Some((Element::Key, sp)) => self.span_key = Some(sp),
Some((Element::Value { continuation }, sp)) => {
if continuation {
self.attrs.0.as_mut().unwrap().last_mut().unwrap().1 = format!(
"{} {}",
self.attrs.0.as_ref().unwrap().last().unwrap().1,
sp.of(self.input),
)
.into();
self.attrs
.0
.as_mut()
.unwrap()
.last_mut()
.unwrap()
.1
.extend(sp.of(self.input));
} else {
self.attrs.insert(
self.span_key.take().unwrap().of(self.input),
Expand All @@ -295,6 +321,10 @@ impl<'a, 's> Parser<'a, 's> {
}
})
}

pub fn len(&self) -> usize {
self.input.len() - self.chars.as_str().len()
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down
Loading

0 comments on commit 4f69614

Please sign in to comment.