use super::{
Anchor, Call, CollectionStyle, Cont, Cow, DUPLICATE_PROPERTIES, Error, ErrorKind, Event,
FlowLack, FlowState, LineIndex, Parser, Properties, Run, ScalarStyle, Span, Step, Tag, Token,
TokenKind, implicit_key_fits,
};
impl<'a> Parser<'a> {
pub(super) fn flow_sequence(
&mut self,
tokens: Run,
start: usize,
depth: usize,
tag: Option<Tag>,
anchor: Option<Anchor<'a>>,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
let start_span = self.token(tokens, start).span;
self.limits
.check_depth(depth + 1)
.map_err(|error| self.limit_at(start_span, error))?;
self.emit(Event::SequenceStart {
tag,
anchor,
style: CollectionStyle::Flow,
span: start_span,
})?;
let state = FlowState {
tokens,
start_span,
index: start + 1,
entries: 0,
depth,
};
self.flow_sequence_loop(state, stack)
}
pub(super) fn flow_sequence_loop(
&mut self,
mut state: FlowState,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
let tokens = state.tokens;
let index = state.index;
if index >= tokens.len() {
return Err(self.flow_error(tokens, index, FlowLack::Separator));
}
let token = self.token(tokens, index);
if matches!(token.kind, TokenKind::FlowSequenceEnd) {
let end_span = token.span;
self.emit(Event::SequenceEnd { span: end_span })?;
return Ok(Step::Return(index + 1));
}
if matches!(token.kind, TokenKind::FlowEntry) {
return Err(self.flow_error(
tokens,
index,
FlowLack::Node("unexpected empty flow sequence entry".to_owned()),
));
}
state.entries += 1;
self.limits
.check_collection_entries(state.entries)
.map_err(|error| self.limit_at(token.span, error))?;
let depth = state.depth + 1;
if matches!(self.token(tokens, index).kind, TokenKind::Key) {
stack.push(Cont::FlowSequenceItem {
state,
inline: false,
});
return Ok(Step::Call(Call::FlowExplicitMapping {
tokens,
start: index,
depth,
}));
}
if let Some(colon) = self.implicit_flow_colon(self.tokens(tokens), index) {
stack.push(Cont::FlowSequenceItem {
state,
inline: false,
});
return Ok(Step::Call(Call::ImplicitFlowMapping {
tokens,
start: index,
colon,
depth,
}));
}
stack.push(Cont::FlowSequenceItem {
state,
inline: true,
});
Ok(Step::Call(Call::Inline {
tokens: tokens.from(index),
depth,
tag: None,
anchor: None,
}))
}
pub(super) fn flow_explicit_mapping(
&mut self,
tokens: Run,
start: usize,
depth: usize,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
let depth = depth + 1;
let start_span = self.token(tokens, start).span;
self.limits
.check_depth(depth)
.map_err(|error| self.limit_at(start_span, error))?;
self.emit(Event::MappingStart {
tag: None,
anchor: None,
style: CollectionStyle::Flow,
span: start_span,
})?;
let key_start = start + 1;
let key_end = self.find_flow_value(self.tokens(tokens), key_start);
let key = tokens.slice(key_start, key_end);
if key.is_empty() {
self.emit(self.null_scalar(Span::empty(start_span.end())))?;
return self.flow_explicit_value(tokens, start_span, key_end, depth, stack);
}
stack.push(Cont::FlowExplicitKey {
tokens,
start_span,
key,
key_end,
depth,
});
Ok(Step::Call(Call::Inline {
tokens: key,
depth,
tag: None,
anchor: None,
}))
}
pub(super) fn flow_explicit_value(
&mut self,
tokens: Run,
start_span: Span,
key_end: usize,
depth: usize,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
let mut index = key_end;
if index < tokens.len() && matches!(self.token(tokens, index).kind, TokenKind::Value) {
index += 1;
let value_end = self.find_flow_item_end(self.tokens(tokens), index);
if index == value_end {
let span = Span::empty(self.token(tokens, index - 1).span.end());
self.emit(self.null_scalar(span))?;
return self.flow_mapping_end(tokens, start_span, value_end);
}
let value = tokens.slice(index, value_end);
stack.push(Cont::FlowExplicitValue {
tokens,
start_span,
value,
value_end,
});
return Ok(Step::Call(Call::Inline {
tokens: value,
depth,
tag: None,
anchor: None,
}));
}
let span = self.empty_before(tokens, index, start_span);
self.emit(self.null_scalar(span))?;
self.flow_mapping_end(tokens, start_span, index)
}
pub(super) fn flow_mapping_end(
&mut self,
tokens: Run,
start_span: Span,
index: usize,
) -> Result<Step<'a>, Error> {
let span = self.empty_before(tokens, index, start_span);
self.emit(Event::MappingEnd { span })?;
Ok(Step::Return(index))
}
pub(super) fn implicit_flow_mapping(
&mut self,
tokens: Run,
start: usize,
colon: usize,
depth: usize,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
let depth = depth + 1;
let start_span = self.token(tokens, start).span;
self.limits
.check_depth(depth)
.map_err(|error| self.limit_at(start_span, error))?;
self.emit(Event::MappingStart {
tag: None,
anchor: None,
style: CollectionStyle::Flow,
span: start_span,
})?;
let key_start = start
+ usize::from(matches!(
self.token_at(tokens, start).map(|token| &token.kind),
Some(TokenKind::Key)
));
let key = tokens.slice(key_start, colon);
if key.is_empty() {
let span = Span::empty(self.token(tokens, colon).span.start());
self.emit(self.null_scalar(span))?;
return self.implicit_flow_value(tokens, start_span, colon, depth, stack);
}
stack.push(Cont::ImplicitFlowKey {
tokens,
start_span,
key,
colon,
depth,
});
Ok(Step::Call(Call::Inline {
tokens: key,
depth,
tag: None,
anchor: None,
}))
}
pub(super) fn implicit_flow_value(
&mut self,
tokens: Run,
start_span: Span,
colon: usize,
depth: usize,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
let value_start = colon + 1;
let value_end = self.find_flow_item_end(self.tokens(tokens), value_start);
if value_start >= tokens.len() || value_start == value_end {
let span = Span::empty(self.token(tokens, colon).span.end());
self.emit(self.null_scalar(span))?;
return self.flow_mapping_end(tokens, start_span, value_end);
}
let value = tokens.slice(value_start, value_end);
stack.push(Cont::ImplicitFlowValue {
tokens,
start_span,
value,
value_end,
});
Ok(Step::Call(Call::Inline {
tokens: value,
depth,
tag: None,
anchor: None,
}))
}
pub(super) fn flow_mapping(
&mut self,
tokens: Run,
start: usize,
depth: usize,
tag: Option<Tag>,
anchor: Option<Anchor<'a>>,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
let start_span = self.token(tokens, start).span;
self.limits
.check_depth(depth + 1)
.map_err(|error| self.limit_at(start_span, error))?;
self.emit(Event::MappingStart {
tag,
anchor,
style: CollectionStyle::Flow,
span: start_span,
})?;
let state = FlowState {
tokens,
start_span,
index: start + 1,
entries: 0,
depth,
};
self.flow_mapping_loop(state, stack)
}
pub(super) fn flow_mapping_loop(
&mut self,
mut state: FlowState,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
let tokens = state.tokens;
let mut index = state.index;
if index >= tokens.len() {
return Err(self.flow_error(tokens, index, FlowLack::Separator));
}
let token = self.token(tokens, index);
if matches!(token.kind, TokenKind::FlowMappingEnd) {
let end_span = token.span;
self.emit(Event::MappingEnd { span: end_span })?;
return Ok(Step::Return(index + 1));
}
if matches!(token.kind, TokenKind::FlowEntry) {
return Err(self.flow_error(
tokens,
index,
FlowLack::Node("unexpected empty flow mapping entry".to_owned()),
));
}
if matches!(token.kind, TokenKind::Key) {
index += 1;
}
let key_end = self.find_flow_value(self.tokens(tokens), index);
let key = tokens.slice(index, key_end);
state.index = key_end;
if key.is_empty() {
if index > 0
&& matches!(
self.token(tokens, index - 1).kind,
TokenKind::FlowMappingStart | TokenKind::FlowEntry | TokenKind::Key
)
&& self
.token_at(tokens, key_end)
.is_some_and(|token| matches!(token.kind, TokenKind::FlowSequenceEnd))
{
return Err(self.flow_error(
tokens,
key_end,
FlowLack::Node("unexpected ] in node position".to_owned()),
));
}
let span = Span::empty(self.token(tokens, index.saturating_sub(1)).span.end());
self.emit(self.null_scalar(span))?;
return self.flow_mapping_after_key(state, stack);
}
let depth = state.depth + 1;
stack.push(Cont::FlowMappingKey { state, key });
Ok(Step::Call(Call::Inline {
tokens: key,
depth,
tag: None,
anchor: None,
}))
}
pub(super) fn flow_mapping_after_key(
&mut self,
mut state: FlowState,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
let tokens = state.tokens;
let mut index = state.index;
let Some(token) = self
.token_at(tokens, index)
.filter(|token| !matches!(token.kind, TokenKind::FlowSequenceEnd))
else {
return Err(self.flow_error(tokens, index, FlowLack::Separator));
};
state.entries += 1;
let span = token.span;
self.limits
.check_collection_entries(state.entries)
.map_err(|error| self.limit_at(span, error))?;
if matches!(self.token(tokens, index).kind, TokenKind::Value) {
index += 1;
} else {
let span = self.empty_before(tokens, index, state.start_span);
self.emit(self.null_scalar(span))?;
}
if index >= tokens.len()
|| matches!(
self.token(tokens, index).kind,
TokenKind::FlowEntry | TokenKind::FlowMappingEnd
)
{
if matches!(
self.token(tokens, index.saturating_sub(1)).kind,
TokenKind::Value
) {
let span = self.empty_before(tokens, index, state.start_span);
self.emit(self.null_scalar(span))?;
}
state.index = index;
stack.push(Cont::FlowMappingNext(state));
return Ok(Step::Return(0));
}
let value_end = self.find_flow_item_end(self.tokens(tokens), index);
if value_end == index {
return Err(self.flow_error(
tokens,
index,
FlowLack::Node("unexpected ] in node position".to_owned()),
));
}
let value = tokens.slice(index, value_end);
state.index = value_end;
let depth = state.depth + 1;
stack.push(Cont::FlowMappingValue { state, value });
Ok(Step::Call(Call::Inline {
tokens: value,
depth,
tag: None,
anchor: None,
}))
}
pub(super) fn flow_mapping_next(
&mut self,
mut state: FlowState,
stack: &mut Vec<Cont>,
) -> Result<Step<'a>, Error> {
if state.index < state.tokens.len()
&& matches!(
self.token(state.tokens, state.index).kind,
TokenKind::FlowEntry
)
{
state.index += 1;
}
self.flow_mapping_loop(state, stack)
}
#[allow(
clippy::unused_self,
reason = "the method is part of a uniform grammar or diagnostic surface"
)]
pub(super) fn implicit_flow_colon(&self, tokens: &[Token<'a>], start: usize) -> Option<usize> {
let mut depth = 0usize;
for (index, token) in tokens.iter().enumerate().skip(start) {
match token.kind {
TokenKind::FlowSequenceStart | TokenKind::FlowMappingStart => depth += 1,
TokenKind::FlowSequenceEnd | TokenKind::FlowMappingEnd => {
if depth == 0 {
return None;
}
depth -= 1;
}
TokenKind::Value if depth == 0 => {
let key_start = tokens[start].span.start();
let implicit_key = key_start.line() == token.span.start().line()
&& implicit_key_fits(key_start, token.span.start());
if implicit_key || matches!(tokens[start].kind, TokenKind::Key) {
return Some(index);
}
return None;
}
TokenKind::FlowEntry if depth == 0 => return None,
_ => {}
}
}
None
}
#[allow(
clippy::match_same_arms,
reason = "the arms stay separate to document each representation or state"
)]
#[allow(
clippy::unused_self,
reason = "the method is part of a uniform grammar or diagnostic surface"
)]
pub(super) fn find_flow_value(&self, tokens: &[Token<'a>], start: usize) -> usize {
let mut depth = 0usize;
for (index, token) in tokens.iter().enumerate().skip(start) {
match token.kind {
TokenKind::FlowSequenceStart | TokenKind::FlowMappingStart => depth += 1,
TokenKind::FlowSequenceEnd | TokenKind::FlowMappingEnd => {
if depth == 0 {
return index;
}
depth -= 1;
}
TokenKind::Value if depth == 0 => return index,
TokenKind::FlowEntry if depth == 0 => return index,
_ => {}
}
}
tokens.len()
}
#[allow(
clippy::unused_self,
reason = "the method is part of a uniform grammar or diagnostic surface"
)]
pub(super) fn find_flow_item_end(&self, tokens: &[Token<'a>], start: usize) -> usize {
let mut depth = 0usize;
for (index, token) in tokens.iter().enumerate().skip(start) {
match token.kind {
TokenKind::FlowSequenceStart | TokenKind::FlowMappingStart => depth += 1,
TokenKind::FlowSequenceEnd | TokenKind::FlowMappingEnd => {
if depth == 0 {
return index;
}
depth -= 1;
}
TokenKind::FlowEntry if depth == 0 => return index,
_ => {}
}
}
tokens.len()
}
#[cold]
pub(super) fn flow_error(&self, tokens: Run, index: usize, lack: FlowLack) -> Error {
let failure = self.token_at(tokens, index).map(|token| token.span);
let span = failure.unwrap_or_else(|| self.end_of_input());
let Some((opening, sequence)) = self.open_flow(tokens, index) else {
return self.error(
span,
match lack {
FlowLack::Separator => "expected ',' or a closing bracket".to_owned(),
FlowLack::Node(problem) => problem,
},
);
};
let (name, closing) = if sequence {
("sequence", ']')
} else {
("mapping", '}')
};
let from = opening.start();
let there = format!("at line {} column {}", from.line(), from.column());
let lack = if self.compat {
self.libyaml_flow_lack(tokens, index, sequence, lack)
} else {
lack
};
let (problem, context) = match lack {
FlowLack::Separator if self.compat => (
format!("did not find expected ',' or '{closing}'"),
format!(", while parsing a flow {name} {there}"),
),
FlowLack::Node(_) if self.compat => (
"did not find expected node content".to_owned(),
format!(", while parsing a flow {name} {there}"),
),
FlowLack::Separator if failure.is_none() => (
format!("unterminated flow {name}"),
format!(", opened {there}"),
),
FlowLack::Separator => (
format!("expected ',' or '{closing}'"),
format!(", in the flow {name} opened {there}"),
),
FlowLack::Node(problem) => (problem, format!(", in the flow {name} opened {there}")),
};
self.error(span, problem).with_context(&context)
}
pub(super) fn libyaml_flow_lack(
&self,
tokens: Run,
index: usize,
sequence: bool,
lack: FlowLack,
) -> FlowLack {
let line = &self.lines[tokens.line];
let at = line.start + tokens.start + index.min(tokens.len());
let before = |back: usize| {
at.checked_sub(back)
.filter(|&token| token >= line.start)
.map(|token| &self.token_arena[token].kind)
};
let failing = self.token_at(tokens, index).map(|token| &token.kind);
if sequence
&& matches!(before(2), Some(TokenKind::Key))
&& matches!(before(1), Some(TokenKind::Value | TokenKind::FlowEntry))
&& !matches!(failing, Some(TokenKind::Value | TokenKind::FlowEntry))
{
return FlowLack::Separator;
}
match lack {
FlowLack::Separator
if (failing.is_none()
|| sequence && matches!(failing, Some(TokenKind::FlowMappingEnd)))
&& matches!(
before(1),
Some(
TokenKind::FlowSequenceStart
| TokenKind::FlowMappingStart
| TokenKind::FlowEntry
| TokenKind::Key
| TokenKind::Value
)
) =>
{
FlowLack::Node(String::new())
}
lack => lack,
}
}
pub(super) fn open_flow(&self, tokens: Run, index: usize) -> Option<(Span, bool)> {
let line = &self.lines[tokens.line];
let before = (line.start + tokens.start + index).min(line.end);
let mut closed = 0_usize;
for token in self.token_arena[line.start..before].iter().rev() {
match token.kind {
TokenKind::FlowSequenceEnd | TokenKind::FlowMappingEnd => closed += 1,
TokenKind::FlowSequenceStart | TokenKind::FlowMappingStart if closed > 0 => {
closed -= 1;
}
TokenKind::FlowSequenceStart => return Some((token.span, true)),
TokenKind::FlowMappingStart => return Some((token.span, false)),
_ => {}
}
}
None
}
pub(super) fn end_of_input(&self) -> Span {
Span::empty(LineIndex::end_marker(self.input, self.compat))
}
#[inline]
pub(super) fn properties(&self, run: Run) -> Result<Properties<'a>, Error> {
if !self.starts_with_property(run) {
return Ok((None, None, 0));
}
self.read_properties(run)
}
#[inline]
pub(super) fn starts_with_property(&self, run: Run) -> bool {
matches!(
self.tokens(run).first().map(|token| &token.kind),
Some(TokenKind::Tag(_) | TokenKind::Anchor(_))
)
}
pub(super) fn read_properties(&self, run: Run) -> Result<Properties<'a>, Error> {
let mut tag = None;
let mut anchor = None;
let mut index = 0;
for token in self.tokens(run) {
let duplicate = match &token.kind {
TokenKind::Tag(value) => tag.replace(value.clone()).is_some(),
TokenKind::Anchor(value) => anchor.replace(Anchor::new(value.clone())).is_some(),
_ => break,
};
if duplicate {
return Err(self.error(token.span, DUPLICATE_PROPERTIES));
}
index += 1;
}
Ok((tag, anchor, index))
}
pub(super) fn line_first(&self, index: usize) -> Option<&Token<'a>> {
let line = self.lines.get(index)?;
if line.is_empty() {
return None;
}
self.token_arena.get(line.start)
}
pub(super) fn line_last(&self, index: usize) -> Option<&Token<'a>> {
let line = self.lines.get(index)?;
if line.is_empty() {
return None;
}
line.end
.checked_sub(1)
.and_then(|end| self.token_arena.get(end))
}
pub(super) fn line_tokens(&self, index: usize) -> &[Token<'a>] {
let line = &self.lines[index];
&self.token_arena[line.start..line.end]
}
pub(super) fn take_line_first_span(&mut self, index: usize) -> Option<Span> {
let line = self.lines.get_mut(index)?;
if line.is_empty() {
return None;
}
let span = self.token_arena[line.start].span;
line.advance(1);
Some(span)
}
pub(super) fn line_run(&self, index: usize) -> Run {
Run {
line: index,
start: 0,
end: self.lines[index].len(),
}
}
pub(super) fn tokens(&self, run: Run) -> &[Token<'a>] {
let line = &self.lines[run.line];
&self.token_arena[line.start + run.start..line.start + run.end]
}
pub(super) fn token(&self, run: Run, index: usize) -> &Token<'a> {
&self.tokens(run)[index]
}
pub(super) fn token_at(&self, run: Run, index: usize) -> Option<&Token<'a>> {
self.tokens(run).get(index)
}
pub(super) fn empty_before(&self, run: Run, index: usize, start: Span) -> Span {
self.token_at(run, index).map_or_else(
|| Span::empty(start.end()),
|token| Span::empty(token.span.start()),
)
}
pub(super) fn null_scalar(&self, span: Span) -> Event<'a> {
self.null_scalar_with(span, None, None)
}
#[allow(
clippy::unused_self,
reason = "the method is part of a uniform grammar or diagnostic surface"
)]
pub(super) fn null_scalar_with(
&self,
span: Span,
tag: Option<Tag>,
anchor: Option<Anchor<'a>>,
) -> Event<'a> {
Event::Scalar {
value: Cow::Borrowed(""),
raw: Cow::Borrowed(""),
style: ScalarStyle::Plain,
tag,
anchor,
span,
}
}
pub(super) fn emit(&mut self, mut event: Event<'a>) -> Result<(), Error> {
match &mut event {
Event::Scalar { tag, .. } => {
if tag.as_ref().is_some_and(Tag::is_core_collection) {
*tag = None;
}
}
Event::SequenceStart { tag, .. } => {
if tag.as_ref().is_some_and(|tag| {
tag.is_core_scalar()
|| (tag.is_core_collection()
&& !matches!(tag.suffix(), "seq" | "omap" | "pairs"))
}) {
*tag = None;
}
}
Event::MappingStart { tag, .. } => {
let drop = tag.as_ref().is_some_and(|tag| {
tag.is_core_scalar()
|| (tag.is_core_collection() && !matches!(tag.suffix(), "map" | "set"))
});
if drop {
*tag = None;
}
}
_ => {}
}
self.emitted = self.emitted.saturating_add(1);
let span = event.span();
self.limits
.check_events(self.emitted)
.map_err(|error| self.limit_at(span, error))?;
self.sink.event(event)
}
pub(super) fn line_starts_with(
&self,
index: usize,
predicate: impl Fn(&TokenKind<'a>) -> bool,
) -> bool {
self.line_first(index)
.is_some_and(|token| predicate(&token.kind))
}
pub(super) fn empty_span(&self, index: usize) -> Span {
if self.lines.get(index).is_some()
&& let Some(token) = self.line_first(index)
{
let marker = token.span.start();
return Span::empty(marker);
}
Span::empty(self.end)
}
pub(super) fn last_span(&self, index: usize) -> Span {
if index > 0
&& self.lines.get(index - 1).is_some()
&& let Some(token) = self.line_last(index - 1)
{
return Span::empty(token.span.end());
}
self.empty_span(index)
}
#[allow(
clippy::unused_self,
reason = "the method is part of a uniform grammar or diagnostic surface"
)]
pub(super) fn error(&self, span: Span, message: impl Into<String>) -> Error {
Error::new(ErrorKind::Parser, message).with_span(span)
}
#[allow(
clippy::unused_self,
reason = "the method is part of a uniform grammar or diagnostic surface"
)]
pub(super) fn limit_at(&self, span: Span, limit: crate::limits::LimitExceeded) -> Error {
Error::from(limit).with_span(span)
}
}