Lines
96.61 %
Functions
96.15 %
Branches
100 %
//! Resumable scan for the end of one form: each byte is visited once however
//! the input is split into chunks.
pub(crate) enum Scan {
Complete(usize),
Incomplete,
Invalid(String),
}
/// Where a string literal stands. `""` waits for a third quote to tell an empty
/// string from a triple-quoted one.
#[derive(Debug, Clone, Copy)]
enum Lit {
Open1,
Open2,
Plain { escaped: bool },
Triple { run: u8 },
Hash,
enum Step {
More,
End,
EndBefore,
impl Lit {
fn step(&mut self, byte: u8) -> Step {
match *self {
Self::Open1 if byte == b'"' => *self = Self::Open2,
Self::Open1 => {
*self = Self::Plain {
escaped: byte == b'\\',
};
Self::Open2 if byte == b'"' => *self = Self::Triple { run: 0 },
Self::Open2 => return Step::EndBefore,
Self::Plain { escaped: true } => *self = Self::Plain { escaped: false },
Self::Plain { escaped: false } => match byte {
b'\\' => *self = Self::Plain { escaped: true },
b'"' => return Step::End,
_ => {}
},
Self::Triple { run } if byte == b'"' => {
if run == 2 {
return Step::End;
*self = Self::Triple { run: run + 1 };
Self::Triple { .. } => *self = Self::Triple { run: 0 },
Self::Hash if byte == b'"' => return Step::End,
Self::Hash => {}
Step::More
enum Code {
Plain,
Comment,
HashSeen,
Lit(Lit),
enum State {
Prefix,
PrefixComma,
Head,
List { depth: usize, code: Code },
enum Flow {
Next,
Stall,
Done(Scan),
#[derive(Debug)]
pub(crate) struct Scanner {
pos: usize,
head: usize,
state: State,
impl Scanner {
pub(crate) fn new() -> Self {
Self {
pos: 0,
head: 0,
state: State::Prefix,
/// Scans `bytes`, the form's whole text so far, from where the last call stopped.
pub(crate) fn advance(&mut self, bytes: &[u8]) -> Scan {
while self.pos < bytes.len() {
match self.step(bytes) {
Flow::Next => {}
Flow::Stall => break,
Flow::Done(scan) => return scan,
Scan::Incomplete
/// Scans `bytes` as the whole input: an undecided head becomes an atom and
/// a trailing `""` an empty string.
pub(crate) fn finish(&mut self, bytes: &[u8]) -> Scan {
match self.advance(bytes) {
Scan::Incomplete => match self.state {
State::Head if self.head < bytes.len() => Scan::Complete(bytes.len()),
State::Lit(Lit::Open2) => Scan::Complete(bytes.len()),
_ => Scan::Incomplete,
scan => scan,
fn step(&mut self, bytes: &[u8]) -> Flow {
let byte = bytes[self.pos];
match self.state {
State::Prefix => self.prefix(byte),
State::PrefixComma if byte == b'@' => self.go(State::Prefix, 1),
State::PrefixComma => self.go(State::Prefix, 0),
State::Head => self.head(bytes),
State::Lit(lit) => self.lit(lit, byte),
State::List { depth, code } => self.list(depth, code, byte),
fn go(&mut self, state: State, consumed: usize) -> Flow {
self.state = state;
self.pos += consumed;
Flow::Next
fn prefix(&mut self, byte: u8) -> Flow {
match byte {
b'\'' | b'`' => self.go(State::Prefix, 1),
b',' => self.go(State::PrefixComma, 1),
_ => {
self.head = self.pos;
self.go(State::Head, 0)
fn head(&mut self, bytes: &[u8]) -> Flow {
let open = State::List {
depth: 1,
code: Code::Plain,
let from = self.head;
match &bytes[from..] {
[b'(', ..] => self.goto(open, from + 1),
[b'"', ..] => self.goto(State::Lit(Lit::Open1), from + 1),
[b')', ..] => Flow::Done(Scan::Invalid("unexpected ')'".into())),
[b'#', b'"', ..] => self.goto(State::Lit(Lit::Hash), from + 2),
[b'#', b'u', b'8', b'(', ..] => self.goto(open, from + 4),
[b'#'] | [b'#', b'u'] | [b'#', b'u', b'8'] => Flow::Stall,
rest => Flow::Done(Scan::Complete(from + atom_len(rest))),
fn goto(&mut self, state: State, pos: usize) -> Flow {
self.pos = pos;
fn lit(&mut self, mut lit: Lit, byte: u8) -> Flow {
match lit.step(byte) {
Step::More => self.go(State::Lit(lit), 1),
Step::End => Flow::Done(Scan::Complete(self.pos + 1)),
Step::EndBefore => Flow::Done(Scan::Complete(self.pos)),
fn list(&mut self, depth: usize, code: Code, byte: u8) -> Flow {
let (depth, code, consumed) = match (code, byte) {
(Code::Plain, b'(') => (depth + 1, Code::Plain, 1),
(Code::Plain, b')') if depth == 1 => return Flow::Done(Scan::Complete(self.pos + 1)),
(Code::Plain, b')') => (depth - 1, Code::Plain, 1),
(Code::Plain, b'"') => (depth, Code::Lit(Lit::Open1), 1),
(Code::Plain, b'#') => (depth, Code::HashSeen, 1),
(Code::Plain, b';') => (depth, Code::Comment, 1),
(Code::Plain, _) => (depth, Code::Plain, 1),
(Code::HashSeen, b'"') => (depth, Code::Lit(Lit::Hash), 1),
(Code::HashSeen, _) => (depth, Code::Plain, 0),
(Code::Comment, b'\n') => (depth, Code::Plain, 1),
(Code::Comment, _) => (depth, Code::Comment, 1),
(Code::Lit(mut lit), byte) => match lit.step(byte) {
Step::More => (depth, Code::Lit(lit), 1),
Step::End => (depth, Code::Plain, 1),
Step::EndBefore => (depth, Code::Plain, 0),
self.go(State::List { depth, code }, consumed)
fn atom_len(rest: &[u8]) -> usize {
rest.iter()
.position(|b| matches!(b, b' ' | b'\t' | b'\n' | b'\r' | b'(' | b')' | b';'))
.unwrap_or(rest.len())
#[cfg(test)]
mod tests;