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 and #34
  • Loading branch information
hellux committed Mar 30, 2023
1 parent 5e48508 commit 1e912ec
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 117 deletions.
36 changes: 34 additions & 2 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,37 @@ impl<'s> std::fmt::Debug for Attributes<'s> {
}
}

pub struct Validator {
state: State,
}

impl Validator {
pub fn new() -> Self {
Self {
state: State::Start,
}
}

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

/// Returns number of valid bytes parsed (0 means invalid) if finished, otherwise more input is
/// needed.
pub fn parse(&mut self, input: &str) -> Option<usize> {
let mut chars = input.chars();
for c in &mut chars {
self.state = self.state.step(c);
match self.state {
State::Done => return Some(input.len() - chars.as_str().len()),
State::Invalid => return Some(0),
_ => {}
}
}
None
}
}

/// Attributes parser, take input of one or more consecutive attributes and create an `Attributes`
/// object.
///
Expand Down Expand Up @@ -266,8 +297,9 @@ impl<'s> Parser<'s> {

let mut pos = 0;
let mut pos_prev = 0;
let mut chars = input.chars();

for c in input.chars() {
for c in &mut chars {
let state_next = self.state.step(c);
let st = std::mem::replace(&mut self.state, state_next);

Expand Down Expand Up @@ -302,7 +334,7 @@ impl<'s> Parser<'s> {
}
}

fn finish(self) -> Attributes<'s> {
pub fn finish(self) -> Attributes<'s> {
self.attrs
}
}
Expand Down
Loading

0 comments on commit 1e912ec

Please sign in to comment.