lib/scanner: save keys across Scanner

and fix a few function / type defs
This commit is contained in:
Paul Stemmet 2021-07-31 20:00:41 +00:00 committed by Paul Stemmet
parent 01039b62e2
commit 0ee871dad5

View file

@ -97,8 +97,7 @@ impl Scanner
if base.is_empty() || self.state == StreamState::Done if base.is_empty() || self.state == StreamState::Done
{ {
self.stream_end(*base, tokens); return self.stream_end(*base, tokens);
return Ok(());
} }
if self.stats.column == 0 if self.stats.column == 0
@ -157,7 +156,7 @@ impl Scanner
} }
} }
fn stream_end(&mut self, buffer: &str, tokens: &mut Tokens) fn stream_end(&mut self, buffer: &str, tokens: &mut Tokens) -> Result<()>
{ {
match (self.state, buffer.is_empty()) match (self.state, buffer.is_empty())
{ {
@ -165,6 +164,7 @@ impl Scanner
{}, {},
(_, true) => (_, true) =>
{ {
self.remove_saved_key()?;
self.state = StreamState::Done; self.state = StreamState::Done;
let token = Token::StreamEnd; let token = Token::StreamEnd;
@ -174,6 +174,8 @@ impl Scanner
(_, false) => (_, false) =>
{}, {},
} }
Ok(())
} }
/// Chomp whitespace and optionally comments until we /// Chomp whitespace and optionally comments until we
@ -305,6 +307,8 @@ impl Scanner
return Ok(()); return Ok(());
} }
self.save_key(!REQUIRED)?;
let (token, amt) = scan_node_tag(buffer, &mut stats)?; let (token, amt) = scan_node_tag(buffer, &mut stats)?;
advance!(buffer, amt); advance!(buffer, amt);
@ -338,6 +342,8 @@ impl Scanner
_ => return Ok(()), _ => return Ok(()),
}; };
self.save_key(!REQUIRED)?;
advance!(buffer, :stats, 1); advance!(buffer, :stats, 1);
// *anchor 'rest of the line' // *anchor 'rest of the line'
@ -384,8 +390,7 @@ impl Scanner
Ok(()) Ok(())
} }
fn flow_scalar<'de>(&mut self, base: &mut &'de str, tokens: &mut Vec<Token<'de>>) fn flow_scalar<'de>(&mut self, base: &mut &'de str, tokens: &mut Tokens<'de>) -> Result<()>
-> Result<()>
{ {
let buffer = *base; let buffer = *base;
let mut stats = MStats::new(); let mut stats = MStats::new();
@ -396,6 +401,8 @@ impl Scanner
return Ok(()); return Ok(());
} }
self.save_key(!REQUIRED)?;
let (range, amt) = scan_flow_scalar(buffer, &mut stats, single)?; let (range, amt) = scan_flow_scalar(buffer, &mut stats, single)?;
let token = range.into_token(buffer)?; let token = range.into_token(buffer)?;
@ -454,19 +461,22 @@ impl Scanner
map: bool, map: bool,
) -> Result<()> ) -> Result<()>
{ {
self.key_possible(!REQUIRED);
self.context.flow_increment()?;
advance!(*base, :self.stats, 1);
let token = match map let token = match map
{ {
true => Token::FlowMappingStart, true => Token::FlowMappingStart,
false => Token::FlowSequenceStart, false => Token::FlowSequenceStart,
}; };
tokens.push(token); self.context.flow_increment()?;
advance!(*base, :self.stats, 1);
enqueue!(token, :self.stats => tokens);
self.save_key(!REQUIRED)?;
// A simple key may start after '[' or '{'
self.simple_key_allowed = true;
Ok(()) Ok(())
} }
@ -478,19 +488,24 @@ impl Scanner
map: bool, map: bool,
) -> Result<()> ) -> Result<()>
{ {
self.context.flow_decrement()?;
self.key_possible(!REQUIRED);
advance!(*base, :self.stats, 1);
let token = match map let token = match map
{ {
true => Token::FlowMappingEnd, true => Token::FlowMappingEnd,
false => Token::FlowSequenceEnd, false => Token::FlowSequenceEnd,
}; };
tokens.push(token); // Reset saved key
self.remove_saved_key()?;
// Decrease flow level by 1
self.context.flow_decrement()?;
// A simple key is not allowed after a ']' or '}'
self.simple_key_allowed = false;
advance!(*base, :self.stats, 1);
enqueue!(token, :self.stats => tokens);
Ok(()) Ok(())
} }
@ -501,7 +516,11 @@ impl Scanner
tokens: &mut Tokens<'de>, tokens: &mut Tokens<'de>,
) -> Result<()> ) -> Result<()>
{ {
self.key_possible(!REQUIRED); // Reset saved key
self.remove_saved_key()?;
// A simple key can start after a ','
self.simple_key_allowed = true;
advance!(*base, :self.stats, 1); advance!(*base, :self.stats, 1);
@ -524,7 +543,8 @@ impl Scanner
false => Err(ScanError::InvalidBlockEntry), false => Err(ScanError::InvalidBlockEntry),
}?; }?;
self.is_key_required()?; // Reset saved key
self.remove_saved_key()?;
// A key is possible after a '-' // A key is possible after a '-'
self.simple_key_allowed = true; self.simple_key_allowed = true;