let header = on_table(event, input, source, errors);
state.start_table(header, errors);
}
EventKind::SimpleKey => { let (path, key) = on_key(event, input, source, errors); let Some(key) = key else { break;
}; let Some(next_event) = input.next_token() else { break;
}; let keyval_event = if next_event.kind() == EventKind::Whitespace { let Some(next_event) = input.next_token() else { break;
};
next_event
} else {
next_event
}; if keyval_event.kind() != EventKind::KeyValSep { break;
}
if input
.first()
.map(|e| e.kind() == EventKind::Whitespace)
.unwrap_or(false)
{ let _ = input.next_token();
} let value = value(input, source, errors);
let dotted = true; let Some(parent_table) = descend_path(&mutself.current_table, &path, dotted, errors) else { return;
}; // "Likewise, using dotted keys to redefine tables already defined in [table] form is not allowed" let mixed_table_types = parent_table.is_dotted() == path.is_empty(); if mixed_table_types { let key_span = get_key_span(&key);
errors.report_error(ParseError::new("duplicate key").with_unexpected(key_span)); return;
} let key_span = get_key_span(&key); match parent_table.entry(key) {
Entry::Vacant(o) => {
o.insert(value);
}
Entry::Occupied(existing) => { // "Since tables cannot be defined more than once, redefining such tables using a [table] header is not allowed" let old_span = get_key_span(existing.key());
errors.report_error(
ParseError::new("duplicate key")
.with_unexpected(key_span)
.with_context(old_span),
);
}
}
}
fn finish_table(&mutself, errors: &mutdyn ErrorSink) { #[cfg(feature = "debug")] let _scope = TraceScope::new("document::finish_table"); let prev_table = core::mem::take(&mutself.current_table); iflet Some(header) = self.current_header.take() { let Some(key) = &header.key else { return;
}; let header_span = header.span.start()..header.span.end(); let prev_table = Spanned::new(header_span.clone(), DeValue::Table(prev_table));
let parent_key = &header.path; let dotted = false; let Some(parent_table) = descend_path(&mutself.root, parent_key, dotted, errors) else { return;
}; #[cfg(feature = "debug")]
trace(
&format!("key={key}",),
anstyle::AnsiColor::Blue.on_default(),
); if header.is_array { let entry = parent_table.entry(key.clone()).or_insert_with(|| { letmut array = DeArray::new();
array.set_array_of_tables(true);
Spanned::new(header_span, DeValue::Array(array))
}); let Some(array) = entry
.as_mut()
.as_array_mut()
.filter(|a| a.is_array_of_tables()) else { let key_span = get_key_span(key); let old_span = entry.span(); let old_span = toml_parser::Span::new_unchecked(old_span.start, old_span.end);
errors.report_error(
ParseError::new("duplicate key")
.with_unexpected(key_span)
.with_context(old_span),
); return;
};
array.push(prev_table);
} else { let existing = parent_table.insert(key.clone(), prev_table);
debug_assert!(existing.is_none());
}
} else { self.root = prev_table;
}
}
fn start_table(&mutself, header: TableHeader<'i>, errors: &mut dyn ErrorSink) { if !header.is_array { // 1. Look up the table on start to ensure the duplicate_key error points to the right line // 2. Ensure any child tables from an implicit table are preserved let root = &mutself.root; iflet (Some(parent_table), Some(key)) =
(descend_path(root, &header.path, false, errors), &header.key)
{ iflet Some((old_key, old_value)) = parent_table.remove_entry(key) { match old_value.into_inner() {
DeValue::Table(t) if t.is_implicit() && !t.is_dotted() => { self.current_table = t;
} // Since tables cannot be defined more than once, redefining such tables using a [table] header is not allowed. Likewise, using dotted keys to redefine tables already defined in [table] form is not allowed.
old_value => { let old_span = get_key_span(&old_key); let key_span = get_key_span(key);
errors.report_error(
ParseError::new("duplicate key")
.with_unexpected(key_span)
.with_context(old_span),
);
let value = DeValue::Table(new_table); let value = Spanned::new(key.span(), value); let value = entry.insert(value);
value.as_mut().as_table_mut().unwrap()
}
Entry::Occupied(entry) => { let spanned = entry.into_mut(); let old_span = spanned.span(); match spanned.as_mut() {
DeValue::Array(refmut array) => { if !array.is_array_of_tables() { let old_span =
toml_parser::Span::new_unchecked(old_span.start, old_span.end); let key_span = get_key_span(key);
errors.report_error(
ParseError::new( "cannot extend value of type array with a dotted key",
)
.with_unexpected(key_span)
.with_context(old_span),
); return None;
}
debug_assert!(!array.is_empty());
let index = array.len() - 1; let last_child = array.get_mut(index).unwrap();
match last_child.as_mut() {
DeValue::Table(table) => table,
existing => { let old_span =
toml_parser::Span::new_unchecked(old_span.start, old_span.end); let key_span = get_key_span(key);
errors.report_error(
ParseError::new(format!( "cannot extend value of type {} with a dotted key",
existing.type_str()
))
.with_unexpected(key_span)
.with_context(old_span),
); return None;
}
}
}
DeValue::Table(refmut sweet_child_of_mine) => { if sweet_child_of_mine.is_inline() { let key_span = get_key_span(key);
errors.report_error(
ParseError::new( "cannot extend value of type inline table with a dotted key",
)
.with_unexpected(key_span),
); return None;
} // Since tables cannot be defined more than once, redefining such tables using a // [table] header is not allowed. Likewise, using dotted keys to redefine tables // already defined in [table] form is not allowed. if dotted && !sweet_child_of_mine.is_implicit() { let key_span = get_key_span(key);
errors.report_error(
ParseError::new("duplicate key").with_unexpected(key_span),
); return None;
}
sweet_child_of_mine
}
existing => { let old_span =
toml_parser::Span::new_unchecked(old_span.start, old_span.end); let key_span = get_key_span(key);
errors.report_error(
ParseError::new(format!( "cannot extend value of type {} with a dotted key",
existing.type_str()
))
.with_unexpected(key_span)
.with_context(old_span),
); return None;
}
}
}
};
}
Some(table)
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.