Tagged union to Rust enum (yaml_node_t)

This commit is contained in:
Simon Ask Ulsnes 2024-01-27 10:18:18 +01:00
parent 293870fedf
commit 82363fd251
6 changed files with 263 additions and 331 deletions

View file

@ -1,13 +1,12 @@
use crate::externs::{free, malloc, memcpy, memmove, memset, realloc, strdup, strlen}; use crate::externs::{free, malloc, memcpy, memmove, memset, realloc, strdup, strlen};
use crate::ops::{ForceAdd as _, ForceMul as _}; use crate::ops::{ForceAdd as _, ForceMul as _};
use crate::yaml::{size_t, yaml_char_t, YamlEventData, YamlTokenData}; use crate::yaml::{size_t, yaml_char_t, YamlEventData, YamlNodeData, YamlTokenData};
use crate::{ use crate::{
libc, yaml_break_t, yaml_document_t, yaml_emitter_state_t, yaml_emitter_t, yaml_encoding_t, libc, yaml_break_t, yaml_document_t, yaml_emitter_state_t, yaml_emitter_t, yaml_encoding_t,
yaml_event_t, yaml_mapping_style_t, yaml_mark_t, yaml_node_item_t, yaml_node_pair_t, yaml_event_t, yaml_mapping_style_t, yaml_mark_t, yaml_node_item_t, yaml_node_pair_t,
yaml_node_t, yaml_parser_state_t, yaml_parser_t, yaml_read_handler_t, yaml_scalar_style_t, yaml_node_t, yaml_parser_state_t, yaml_parser_t, yaml_read_handler_t, yaml_scalar_style_t,
yaml_sequence_style_t, yaml_simple_key_t, yaml_tag_directive_t, yaml_token_t, yaml_sequence_style_t, yaml_simple_key_t, yaml_stack_t, yaml_tag_directive_t, yaml_token_t,
yaml_version_directive_t, yaml_write_handler_t, PointerExt, YAML_ANY_ENCODING, yaml_version_directive_t, yaml_write_handler_t, PointerExt, YAML_ANY_ENCODING,
YAML_MAPPING_NODE, YAML_SCALAR_NODE, YAML_SEQUENCE_NODE,
}; };
use core::mem::{size_of, MaybeUninit}; use core::mem::{size_of, MaybeUninit};
use core::ptr::{self, addr_of_mut}; use core::ptr::{self, addr_of_mut};
@ -1090,18 +1089,18 @@ pub unsafe fn yaml_document_delete(document: &mut yaml_document_t) {
while !STACK_EMPTY!(document.nodes) { while !STACK_EMPTY!(document.nodes) {
let mut node = POP!(document.nodes); let mut node = POP!(document.nodes);
yaml_free(node.tag as *mut libc::c_void); yaml_free(node.tag as *mut libc::c_void);
match node.type_ { match node.data {
YAML_SCALAR_NODE => { YamlNodeData::NoNode => {
yaml_free(node.data.scalar.value as *mut libc::c_void); assert!(false);
} }
YAML_SEQUENCE_NODE => { YamlNodeData::Scalar { ref value, .. } => {
STACK_DEL!(node.data.sequence.items); yaml_free(*value as *mut libc::c_void);
} }
YAML_MAPPING_NODE => { YamlNodeData::Sequence { ref mut items, .. } => {
STACK_DEL!(node.data.mapping.pairs); STACK_DEL!(items);
} }
_ => { YamlNodeData::Mapping { ref mut pairs, .. } => {
__assert!(false); STACK_DEL!(pairs);
} }
} }
} }
@ -1174,8 +1173,6 @@ pub unsafe fn yaml_document_add_scalar(
}; };
let mut tag_copy: *mut yaml_char_t = ptr::null_mut::<yaml_char_t>(); let mut tag_copy: *mut yaml_char_t = ptr::null_mut::<yaml_char_t>();
let mut value_copy: *mut yaml_char_t = ptr::null_mut::<yaml_char_t>(); let mut value_copy: *mut yaml_char_t = ptr::null_mut::<yaml_char_t>();
let mut node = MaybeUninit::<yaml_node_t>::uninit();
let node = node.as_mut_ptr();
__assert!(!value.is_null()); __assert!(!value.is_null());
if tag.is_null() { if tag.is_null() {
tag = b"tag:yaml.org,2002:str\0" as *const u8 as *const libc::c_char as *mut yaml_char_t; tag = b"tag:yaml.org,2002:str\0" as *const u8 as *const libc::c_char as *mut yaml_char_t;
@ -1194,19 +1191,17 @@ pub unsafe fn yaml_document_add_scalar(
length as libc::c_ulong, length as libc::c_ulong,
); );
*value_copy.wrapping_offset(length as isize) = b'\0'; *value_copy.wrapping_offset(length as isize) = b'\0';
memset( let node = yaml_node_t {
node as *mut libc::c_void, data: YamlNodeData::Scalar {
0, value: value_copy,
size_of::<yaml_node_t>() as libc::c_ulong, length: length as size_t,
); style: style,
(*node).type_ = YAML_SCALAR_NODE; },
(*node).tag = tag_copy; tag: tag_copy,
(*node).start_mark = mark; start_mark: mark,
(*node).end_mark = mark; end_mark: mark,
(*node).data.scalar.value = value_copy; };
(*node).data.scalar.length = length as size_t; PUSH!(document.nodes, node);
(*node).data.scalar.style = style;
PUSH!(document.nodes, *node);
return document.nodes.top.c_offset_from(document.nodes.start) as libc::c_int; return document.nodes.top.c_offset_from(document.nodes.start) as libc::c_int;
} }
} }
@ -1243,8 +1238,6 @@ pub unsafe fn yaml_document_add_sequence(
end: ptr::null_mut::<yaml_node_item_t>(), end: ptr::null_mut::<yaml_node_item_t>(),
top: ptr::null_mut::<yaml_node_item_t>(), top: ptr::null_mut::<yaml_node_item_t>(),
}; };
let mut node = MaybeUninit::<yaml_node_t>::uninit();
let node = node.as_mut_ptr();
if tag.is_null() { if tag.is_null() {
tag = b"tag:yaml.org,2002:seq\0" as *const u8 as *const libc::c_char as *mut yaml_char_t; tag = b"tag:yaml.org,2002:seq\0" as *const u8 as *const libc::c_char as *mut yaml_char_t;
} }
@ -1252,20 +1245,20 @@ pub unsafe fn yaml_document_add_sequence(
tag_copy = yaml_strdup(tag); tag_copy = yaml_strdup(tag);
if !tag_copy.is_null() { if !tag_copy.is_null() {
STACK_INIT!(items, yaml_node_item_t); STACK_INIT!(items, yaml_node_item_t);
memset( let node = yaml_node_t {
node as *mut libc::c_void, data: YamlNodeData::Sequence {
0, items: yaml_stack_t {
size_of::<yaml_node_t>() as libc::c_ulong, start: items.start,
); end: items.end,
(*node).type_ = YAML_SEQUENCE_NODE; top: items.start,
(*node).tag = tag_copy; },
(*node).start_mark = mark; style,
(*node).end_mark = mark; },
(*node).data.sequence.items.start = items.start; tag: tag_copy,
(*node).data.sequence.items.end = items.end; start_mark: mark,
(*node).data.sequence.items.top = items.start; end_mark: mark,
(*node).data.sequence.style = style; };
PUSH!(document.nodes, *node); PUSH!(document.nodes, node);
return document.nodes.top.c_offset_from(document.nodes.start) as libc::c_int; return document.nodes.top.c_offset_from(document.nodes.start) as libc::c_int;
} }
} }
@ -1301,8 +1294,6 @@ pub unsafe fn yaml_document_add_mapping(
end: ptr::null_mut::<yaml_node_pair_t>(), end: ptr::null_mut::<yaml_node_pair_t>(),
top: ptr::null_mut::<yaml_node_pair_t>(), top: ptr::null_mut::<yaml_node_pair_t>(),
}; };
let mut node = MaybeUninit::<yaml_node_t>::uninit();
let node = node.as_mut_ptr();
if tag.is_null() { if tag.is_null() {
tag = b"tag:yaml.org,2002:map\0" as *const u8 as *const libc::c_char as *mut yaml_char_t; tag = b"tag:yaml.org,2002:map\0" as *const u8 as *const libc::c_char as *mut yaml_char_t;
} }
@ -1310,20 +1301,22 @@ pub unsafe fn yaml_document_add_mapping(
tag_copy = yaml_strdup(tag); tag_copy = yaml_strdup(tag);
if !tag_copy.is_null() { if !tag_copy.is_null() {
STACK_INIT!(pairs, yaml_node_pair_t); STACK_INIT!(pairs, yaml_node_pair_t);
memset(
node as *mut libc::c_void, let node = yaml_node_t {
0, data: YamlNodeData::Mapping {
size_of::<yaml_node_t>() as libc::c_ulong, pairs: yaml_stack_t {
); start: pairs.start,
(*node).type_ = YAML_MAPPING_NODE; end: pairs.end,
(*node).tag = tag_copy; top: pairs.start,
(*node).start_mark = mark; },
(*node).end_mark = mark; style,
(*node).data.mapping.pairs.start = pairs.start; },
(*node).data.mapping.pairs.end = pairs.end; tag: tag_copy,
(*node).data.mapping.pairs.top = pairs.start; start_mark: mark,
(*node).data.mapping.style = style; end_mark: mark,
PUSH!(document.nodes, *node); };
PUSH!(document.nodes, node);
return document.nodes.top.c_offset_from(document.nodes.start) as libc::c_int; return document.nodes.top.c_offset_from(document.nodes.start) as libc::c_int;
} }
} }
@ -1342,20 +1335,18 @@ pub unsafe fn yaml_document_append_sequence_item(
sequence > 0 sequence > 0
&& (document.nodes.start).wrapping_offset(sequence as isize) <= document.nodes.top && (document.nodes.start).wrapping_offset(sequence as isize) <= document.nodes.top
); );
__assert!( __assert!(matches!(
(*(document.nodes.start).wrapping_offset((sequence - 1) as isize)).type_ (*(document.nodes.start).wrapping_offset((sequence - 1) as isize)).data,
== YAML_SEQUENCE_NODE YamlNodeData::Sequence { .. }
); ));
__assert!( __assert!(
item > 0 && (document.nodes.start).wrapping_offset(item as isize) <= document.nodes.top item > 0 && (document.nodes.start).wrapping_offset(item as isize) <= document.nodes.top
); );
PUSH!( if let YamlNodeData::Sequence { ref mut items, .. } =
(*(document.nodes.start).wrapping_offset((sequence - 1) as isize)) (*(document.nodes.start).wrapping_offset((sequence - 1) as isize)).data
.data {
.sequence PUSH!(*items, item);
.items, }
item
);
Ok(()) Ok(())
} }
@ -1370,10 +1361,10 @@ pub unsafe fn yaml_document_append_mapping_pair(
mapping > 0 mapping > 0
&& (document.nodes.start).wrapping_offset(mapping as isize) <= document.nodes.top && (document.nodes.start).wrapping_offset(mapping as isize) <= document.nodes.top
); );
__assert!( __assert!(matches!(
(*(document.nodes.start).wrapping_offset((mapping - 1) as isize)).type_ (*(document.nodes.start).wrapping_offset((mapping - 1) as isize)).data,
== YAML_MAPPING_NODE YamlNodeData::Mapping { .. }
); ));
__assert!( __assert!(
key > 0 && (document.nodes.start).wrapping_offset(key as isize) <= document.nodes.top key > 0 && (document.nodes.start).wrapping_offset(key as isize) <= document.nodes.top
); );
@ -1381,12 +1372,10 @@ pub unsafe fn yaml_document_append_mapping_pair(
value > 0 && (document.nodes.start).wrapping_offset(value as isize) <= document.nodes.top value > 0 && (document.nodes.start).wrapping_offset(value as isize) <= document.nodes.top
); );
let pair = yaml_node_pair_t { key, value }; let pair = yaml_node_pair_t { key, value };
PUSH!( if let YamlNodeData::Mapping { ref mut pairs, .. } =
(*(document.nodes.start).wrapping_offset((mapping - 1) as isize)) (*(document.nodes.start).wrapping_offset((mapping - 1) as isize)).data
.data {
.mapping PUSH!(*pairs, pair);
.pairs, }
pair
);
Ok(()) Ok(())
} }

View file

@ -4,8 +4,7 @@ use crate::fmt::WriteToPtr;
use crate::ops::ForceMul as _; use crate::ops::ForceMul as _;
use crate::yaml::{ use crate::yaml::{
yaml_anchors_t, yaml_char_t, yaml_document_t, yaml_emitter_t, yaml_event_t, yaml_node_item_t, yaml_anchors_t, yaml_char_t, yaml_document_t, yaml_emitter_t, yaml_event_t, yaml_node_item_t,
yaml_node_pair_t, yaml_node_t, YamlEventData, YAML_ANY_ENCODING, YAML_MAPPING_NODE, yaml_node_pair_t, yaml_node_t, YamlEventData, YamlNodeData, YAML_ANY_ENCODING,
YAML_SCALAR_NODE, YAML_SEQUENCE_NODE,
}; };
use crate::{libc, yaml_document_delete, yaml_emitter_emit, PointerExt}; use crate::{libc, yaml_document_delete, yaml_emitter_emit, PointerExt};
use core::mem::size_of; use core::mem::size_of;
@ -133,21 +132,21 @@ unsafe fn yaml_emitter_delete_document_and_anchors(emitter: &mut yaml_emitter_t)
.wrapping_offset(index as isize) .wrapping_offset(index as isize)
< (*emitter.document).nodes.top < (*emitter.document).nodes.top
{ {
let mut node: yaml_node_t = *(*emitter.document) let node: *mut yaml_node_t = (*emitter.document)
.nodes .nodes
.start .start
.wrapping_offset(index as isize); .wrapping_offset(index as isize);
if !(*emitter.anchors.wrapping_offset(index as isize)).serialized { if !(*emitter.anchors.wrapping_offset(index as isize)).serialized {
yaml_free(node.tag as *mut libc::c_void); yaml_free((*node).tag as *mut libc::c_void);
if node.type_ == YAML_SCALAR_NODE { if let YamlNodeData::Scalar { value, .. } = &(*node).data {
yaml_free(node.data.scalar.value as *mut libc::c_void); yaml_free(*value as *mut libc::c_void);
} }
} }
if node.type_ == YAML_SEQUENCE_NODE { if let YamlNodeData::Sequence { ref mut items, .. } = (*node).data {
STACK_DEL!(node.data.sequence.items); STACK_DEL!(*items);
} }
if node.type_ == YAML_MAPPING_NODE { if let YamlNodeData::Mapping { ref mut pairs, .. } = (*node).data {
STACK_DEL!(node.data.mapping.pairs); STACK_DEL!(*pairs);
} }
index += 1; index += 1;
} }
@ -178,17 +177,17 @@ unsafe fn yaml_emitter_anchor_node(emitter: &mut yaml_emitter_t, index: libc::c_
addr_of_mut!((*(emitter.anchors).wrapping_offset((index - 1) as isize)).references); addr_of_mut!((*(emitter.anchors).wrapping_offset((index - 1) as isize)).references);
*fresh8 += 1; *fresh8 += 1;
if (*emitter.anchors.wrapping_offset((index - 1) as isize)).references == 1 { if (*emitter.anchors.wrapping_offset((index - 1) as isize)).references == 1 {
match (*node).type_ { match &(*node).data {
YAML_SEQUENCE_NODE => { YamlNodeData::Sequence { items, .. } => {
item = (*node).data.sequence.items.start; item = items.start;
while item < (*node).data.sequence.items.top { while item < items.top {
yaml_emitter_anchor_node_sub(emitter, *item); yaml_emitter_anchor_node_sub(emitter, *item);
item = item.wrapping_offset(1); item = item.wrapping_offset(1);
} }
} }
YAML_MAPPING_NODE => { YamlNodeData::Mapping { pairs, .. } => {
pair = (*node).data.mapping.pairs.start; pair = pairs.start;
while pair < (*node).data.mapping.pairs.top { while pair < pairs.top {
yaml_emitter_anchor_node_sub(emitter, (*pair).key); yaml_emitter_anchor_node_sub(emitter, (*pair).key);
yaml_emitter_anchor_node_sub(emitter, (*pair).value); yaml_emitter_anchor_node_sub(emitter, (*pair).value);
pair = pair.wrapping_offset(1); pair = pair.wrapping_offset(1);
@ -197,9 +196,8 @@ unsafe fn yaml_emitter_anchor_node(emitter: &mut yaml_emitter_t, index: libc::c_
_ => {} _ => {}
} }
} else if (*emitter.anchors.wrapping_offset((index - 1) as isize)).references == 2 { } else if (*emitter.anchors.wrapping_offset((index - 1) as isize)).references == 2 {
let fresh9 = &mut emitter.last_anchor_id; emitter.last_anchor_id += 1;
*fresh9 += 1; (*emitter.anchors.wrapping_offset((index - 1) as isize)).anchor = emitter.last_anchor_id;
(*emitter.anchors.wrapping_offset((index - 1) as isize)).anchor = *fresh9;
} }
} }
@ -230,10 +228,10 @@ unsafe fn yaml_emitter_dump_node(
return yaml_emitter_dump_alias(emitter, anchor); return yaml_emitter_dump_alias(emitter, anchor);
} }
(*emitter.anchors.wrapping_offset((index - 1) as isize)).serialized = true; (*emitter.anchors.wrapping_offset((index - 1) as isize)).serialized = true;
match (*node).type_ { match (*node).data {
YAML_SCALAR_NODE => yaml_emitter_dump_scalar(emitter, node, anchor), YamlNodeData::Scalar { .. } => yaml_emitter_dump_scalar(emitter, node, anchor),
YAML_SEQUENCE_NODE => yaml_emitter_dump_sequence(emitter, node, anchor), YamlNodeData::Sequence { .. } => yaml_emitter_dump_sequence(emitter, node, anchor),
YAML_MAPPING_NODE => yaml_emitter_dump_mapping(emitter, node, anchor), YamlNodeData::Mapping { .. } => yaml_emitter_dump_mapping(emitter, node, anchor),
_ => __assert!(false), _ => __assert!(false),
} }
} }
@ -251,7 +249,7 @@ unsafe fn yaml_emitter_dump_alias(
unsafe fn yaml_emitter_dump_scalar( unsafe fn yaml_emitter_dump_scalar(
emitter: &mut yaml_emitter_t, emitter: &mut yaml_emitter_t,
node: *mut yaml_node_t, node: *mut yaml_node_t, // TODO: take by value
anchor: *mut yaml_char_t, anchor: *mut yaml_char_t,
) -> Result<(), ()> { ) -> Result<(), ()> {
let plain_implicit = strcmp( let plain_implicit = strcmp(
@ -263,20 +261,28 @@ unsafe fn yaml_emitter_dump_scalar(
b"tag:yaml.org,2002:str\0" as *const u8 as *const libc::c_char, b"tag:yaml.org,2002:str\0" as *const u8 as *const libc::c_char,
) == 0; ) == 0;
let event = yaml_event_t { if let YamlNodeData::Scalar {
data: YamlEventData::Scalar { value,
anchor, length,
tag: (*node).tag, style,
value: (*node).data.scalar.value, } = &(*node).data
length: (*node).data.scalar.length, {
plain_implicit, let event = yaml_event_t {
quoted_implicit, data: YamlEventData::Scalar {
style: (*node).data.scalar.style, anchor,
}, tag: (*node).tag,
..Default::default() value: *value,
}; length: *length,
plain_implicit,
yaml_emitter_emit(emitter, &event) quoted_implicit,
style: *style,
},
..Default::default()
};
yaml_emitter_emit(emitter, &event)
} else {
unreachable!()
}
} }
unsafe fn yaml_emitter_dump_sequence( unsafe fn yaml_emitter_dump_sequence(
@ -290,27 +296,31 @@ unsafe fn yaml_emitter_dump_sequence(
) == 0; ) == 0;
let mut item: *mut yaml_node_item_t; let mut item: *mut yaml_node_item_t;
let event = yaml_event_t { if let YamlNodeData::Sequence { items, style } = &(*node).data {
data: YamlEventData::SequenceStart { let event = yaml_event_t {
anchor, data: YamlEventData::SequenceStart {
tag: (*node).tag, anchor,
implicit, tag: (*node).tag,
style: (*node).data.sequence.style, implicit,
}, style: *style,
..Default::default() },
}; ..Default::default()
};
yaml_emitter_emit(emitter, &event)?; yaml_emitter_emit(emitter, &event)?;
item = (*node).data.sequence.items.start; item = items.start;
while item < (*node).data.sequence.items.top { while item < items.top {
yaml_emitter_dump_node(emitter, *item)?; yaml_emitter_dump_node(emitter, *item)?;
item = item.wrapping_offset(1); item = item.wrapping_offset(1);
}
let event = yaml_event_t {
data: YamlEventData::SequenceEnd,
..Default::default()
};
yaml_emitter_emit(emitter, &event)
} else {
unreachable!()
} }
let event = yaml_event_t {
data: YamlEventData::SequenceEnd,
..Default::default()
};
yaml_emitter_emit(emitter, &event)
} }
unsafe fn yaml_emitter_dump_mapping( unsafe fn yaml_emitter_dump_mapping(
@ -324,26 +334,30 @@ unsafe fn yaml_emitter_dump_mapping(
) == 0; ) == 0;
let mut pair: *mut yaml_node_pair_t; let mut pair: *mut yaml_node_pair_t;
let event = yaml_event_t { if let YamlNodeData::Mapping { pairs, style } = &(*node).data {
data: YamlEventData::MappingStart { let event = yaml_event_t {
anchor, data: YamlEventData::MappingStart {
tag: (*node).tag, anchor,
implicit, tag: (*node).tag,
style: (*node).data.mapping.style, implicit,
}, style: *style,
..Default::default() },
}; ..Default::default()
};
yaml_emitter_emit(emitter, &event)?; yaml_emitter_emit(emitter, &event)?;
pair = (*node).data.mapping.pairs.start; pair = pairs.start;
while pair < (*node).data.mapping.pairs.top { while pair < pairs.top {
yaml_emitter_dump_node(emitter, (*pair).key)?; yaml_emitter_dump_node(emitter, (*pair).key)?;
yaml_emitter_dump_node(emitter, (*pair).value)?; yaml_emitter_dump_node(emitter, (*pair).value)?;
pair = pair.wrapping_offset(1); pair = pair.wrapping_offset(1);
}
let event = yaml_event_t {
data: YamlEventData::MappingEnd,
..Default::default()
};
yaml_emitter_emit(emitter, &event)
} else {
unreachable!()
} }
let event = yaml_event_t {
data: YamlEventData::MappingEnd,
..Default::default()
};
yaml_emitter_emit(emitter, &event)
} }

View file

@ -310,14 +310,14 @@ pub use crate::writer::yaml_emitter_flush;
pub use crate::yaml::{ pub use crate::yaml::{
yaml_alias_data_t, yaml_break_t, yaml_document_t, yaml_emitter_state_t, yaml_emitter_t, yaml_alias_data_t, yaml_break_t, yaml_document_t, yaml_emitter_state_t, yaml_emitter_t,
yaml_encoding_t, yaml_error_type_t, yaml_event_t, yaml_mapping_style_t, yaml_mark_t, yaml_encoding_t, yaml_error_type_t, yaml_event_t, yaml_mapping_style_t, yaml_mark_t,
yaml_node_item_t, yaml_node_pair_t, yaml_node_t, yaml_node_type_t, yaml_parser_state_t, yaml_node_item_t, yaml_node_pair_t, yaml_node_t, yaml_parser_state_t, yaml_parser_t,
yaml_parser_t, yaml_read_handler_t, yaml_scalar_style_t, yaml_sequence_style_t, yaml_read_handler_t, yaml_scalar_style_t, yaml_sequence_style_t, yaml_simple_key_t,
yaml_simple_key_t, yaml_stack_t, yaml_tag_directive_t, yaml_token_t, yaml_token_type_t, yaml_stack_t, yaml_tag_directive_t, yaml_token_t, yaml_token_type_t, yaml_version_directive_t,
yaml_version_directive_t, yaml_write_handler_t, YamlEventData, yaml_write_handler_t, YamlEventData,
}; };
#[doc(hidden)] #[doc(hidden)]
pub use crate::yaml::{ pub use crate::yaml::{
yaml_break_t::*, yaml_emitter_state_t::*, yaml_encoding_t::*, yaml_error_type_t::*, yaml_break_t::*, yaml_emitter_state_t::*, yaml_encoding_t::*, yaml_error_type_t::*,
yaml_mapping_style_t::*, yaml_node_type_t::*, yaml_parser_state_t::*, yaml_scalar_style_t::*, yaml_mapping_style_t::*, yaml_parser_state_t::*, yaml_scalar_style_t::*,
yaml_sequence_style_t::*, yaml_token_type_t::*, yaml_sequence_style_t::*, yaml_token_type_t::*,
}; };

View file

@ -1,11 +1,10 @@
use crate::api::{yaml_free, yaml_malloc, yaml_stack_extend, yaml_strdup}; use crate::api::{yaml_free, yaml_malloc, yaml_stack_extend, yaml_strdup};
use crate::externs::{memset, strcmp}; use crate::externs::strcmp;
use crate::yaml::{yaml_char_t, YamlEventData}; use crate::yaml::{yaml_char_t, YamlEventData, YamlNodeData};
use crate::{ use crate::{
libc, yaml_alias_data_t, yaml_document_delete, yaml_document_t, yaml_event_t, yaml_mark_t, libc, yaml_alias_data_t, yaml_document_delete, yaml_document_t, yaml_event_t, yaml_mark_t,
yaml_node_item_t, yaml_node_pair_t, yaml_node_t, yaml_parser_parse, yaml_parser_t, PointerExt, yaml_node_item_t, yaml_node_pair_t, yaml_node_t, yaml_parser_parse, yaml_parser_t, PointerExt,
YAML_COMPOSER_ERROR, YAML_MAPPING_NODE, YAML_MEMORY_ERROR, YAML_SCALAR_NODE, YAML_COMPOSER_ERROR, YAML_MEMORY_ERROR,
YAML_SEQUENCE_NODE,
}; };
use core::mem::{size_of, MaybeUninit}; use core::mem::{size_of, MaybeUninit};
use core::ptr::{self, addr_of_mut}; use core::ptr::{self, addr_of_mut};
@ -239,17 +238,16 @@ unsafe fn yaml_parser_load_node_add(
*((*parser.document).nodes.start).wrapping_offset((parent_index - 1) as isize) *((*parser.document).nodes.start).wrapping_offset((parent_index - 1) as isize)
); );
let current_block_17: u64; let current_block_17: u64;
match (*parent).type_ { match (*parent).data {
YAML_SEQUENCE_NODE => { YamlNodeData::Sequence { ref mut items, .. } => {
STACK_LIMIT!(parser, (*parent).data.sequence.items)?; STACK_LIMIT!(parser, items)?;
PUSH!((*parent).data.sequence.items, index); PUSH!(items, index);
} }
YAML_MAPPING_NODE => { YamlNodeData::Mapping { ref mut pairs, .. } => {
let mut pair = MaybeUninit::<yaml_node_pair_t>::uninit(); let mut pair = MaybeUninit::<yaml_node_pair_t>::uninit();
let pair = pair.as_mut_ptr(); let pair = pair.as_mut_ptr();
if !STACK_EMPTY!((*parent).data.mapping.pairs) { if !STACK_EMPTY!(pairs) {
let p: *mut yaml_node_pair_t = let p: *mut yaml_node_pair_t = pairs.top.wrapping_offset(-1_isize);
(*parent).data.mapping.pairs.top.wrapping_offset(-1_isize);
if (*p).key != 0 && (*p).value == 0 { if (*p).key != 0 && (*p).value == 0 {
(*p).value = index; (*p).value = index;
current_block_17 = 11307063007268554308; current_block_17 = 11307063007268554308;
@ -264,8 +262,8 @@ unsafe fn yaml_parser_load_node_add(
_ => { _ => {
(*pair).key = index; (*pair).key = index;
(*pair).value = 0; (*pair).value = 0;
STACK_LIMIT!(parser, (*parent).data.mapping.pairs)?; STACK_LIMIT!(parser, pairs)?;
PUSH!((*parent).data.mapping.pairs, *pair); PUSH!(pairs, *pair);
} }
} }
} }
@ -324,8 +322,6 @@ unsafe fn yaml_parser_load_scalar(
}; };
let current_block: u64; let current_block: u64;
let mut node = MaybeUninit::<yaml_node_t>::uninit();
let node = node.as_mut_ptr();
let index: libc::c_int; let index: libc::c_int;
if let Ok(()) = STACK_LIMIT!(parser, (*parser.document).nodes) { if let Ok(()) = STACK_LIMIT!(parser, (*parser.document).nodes) {
if tag.is_null() if tag.is_null()
@ -347,19 +343,17 @@ unsafe fn yaml_parser_load_scalar(
current_block = 11006700562992250127; current_block = 11006700562992250127;
} }
if current_block != 10579931339944277179 { if current_block != 10579931339944277179 {
memset( let node = yaml_node_t {
node as *mut libc::c_void, data: YamlNodeData::Scalar {
0, value,
size_of::<yaml_node_t>() as libc::c_ulong, length,
); style,
(*node).type_ = YAML_SCALAR_NODE; },
(*node).tag = tag; tag,
(*node).start_mark = (*event).start_mark; start_mark: (*event).start_mark,
(*node).end_mark = (*event).end_mark; end_mark: (*event).end_mark,
(*node).data.scalar.value = value; };
(*node).data.scalar.length = length; PUSH!((*parser.document).nodes, node);
(*node).data.scalar.style = style;
PUSH!((*parser.document).nodes, *node);
index = (*parser.document) index = (*parser.document)
.nodes .nodes
.top .top
@ -389,8 +383,6 @@ unsafe fn yaml_parser_load_sequence(
}; };
let current_block: u64; let current_block: u64;
let mut node = MaybeUninit::<yaml_node_t>::uninit();
let node = node.as_mut_ptr();
struct Items { struct Items {
start: *mut yaml_node_item_t, start: *mut yaml_node_item_t,
end: *mut yaml_node_item_t, end: *mut yaml_node_item_t,
@ -424,20 +416,22 @@ unsafe fn yaml_parser_load_sequence(
} }
if current_block != 13474536459355229096 { if current_block != 13474536459355229096 {
STACK_INIT!(items, yaml_node_item_t); STACK_INIT!(items, yaml_node_item_t);
memset(
node as *mut libc::c_void, let node = yaml_node_t {
0, data: YamlNodeData::Sequence {
size_of::<yaml_node_t>() as libc::c_ulong, items: crate::yaml_stack_t {
); start: items.start,
(*node).type_ = YAML_SEQUENCE_NODE; end: items.end,
(*node).tag = tag; top: items.start,
(*node).start_mark = (*event).start_mark; },
(*node).end_mark = (*event).end_mark; style,
(*node).data.sequence.items.start = items.start; },
(*node).data.sequence.items.end = items.end; tag,
(*node).data.sequence.items.top = items.start; start_mark: (*event).start_mark,
(*node).data.sequence.style = style; end_mark: (*event).end_mark,
PUSH!((*parser.document).nodes, *node); };
PUSH!((*parser.document).nodes, node);
index = (*parser.document) index = (*parser.document)
.nodes .nodes
.top .top
@ -461,10 +455,10 @@ unsafe fn yaml_parser_load_sequence_end(
) -> Result<(), ()> { ) -> Result<(), ()> {
__assert!(((*ctx).top).c_offset_from((*ctx).start) as libc::c_long > 0_i64); __assert!(((*ctx).top).c_offset_from((*ctx).start) as libc::c_long > 0_i64);
let index: libc::c_int = *(*ctx).top.wrapping_offset(-1_isize); let index: libc::c_int = *(*ctx).top.wrapping_offset(-1_isize);
__assert!( __assert!(matches!(
(*((*parser.document).nodes.start).wrapping_offset((index - 1) as isize)).type_ (*((*parser.document).nodes.start).wrapping_offset((index - 1) as isize)).data,
== YAML_SEQUENCE_NODE YamlNodeData::Sequence { .. }
); ));
(*(*parser.document) (*(*parser.document)
.nodes .nodes
.start .start
@ -489,8 +483,6 @@ unsafe fn yaml_parser_load_mapping(
}; };
let current_block: u64; let current_block: u64;
let mut node = MaybeUninit::<yaml_node_t>::uninit();
let node = node.as_mut_ptr();
struct Pairs { struct Pairs {
start: *mut yaml_node_pair_t, start: *mut yaml_node_pair_t,
end: *mut yaml_node_pair_t, end: *mut yaml_node_pair_t,
@ -524,20 +516,20 @@ unsafe fn yaml_parser_load_mapping(
} }
if current_block != 13635467803606088781 { if current_block != 13635467803606088781 {
STACK_INIT!(pairs, yaml_node_pair_t); STACK_INIT!(pairs, yaml_node_pair_t);
memset( let node = yaml_node_t {
node as *mut libc::c_void, data: YamlNodeData::Mapping {
0, pairs: crate::yaml_stack_t {
size_of::<yaml_node_t>() as libc::c_ulong, start: pairs.start,
); end: pairs.end,
(*node).type_ = YAML_MAPPING_NODE; top: pairs.start,
(*node).tag = tag; },
(*node).start_mark = (*event).start_mark; style,
(*node).end_mark = (*event).end_mark; },
(*node).data.mapping.pairs.start = pairs.start; tag,
(*node).data.mapping.pairs.end = pairs.end; start_mark: (*event).start_mark,
(*node).data.mapping.pairs.top = pairs.start; end_mark: (*event).end_mark,
(*node).data.mapping.style = style; };
PUSH!((*parser.document).nodes, *node); PUSH!((*parser.document).nodes, node);
index = (*parser.document) index = (*parser.document)
.nodes .nodes
.top .top
@ -561,10 +553,10 @@ unsafe fn yaml_parser_load_mapping_end(
) -> Result<(), ()> { ) -> Result<(), ()> {
__assert!(((*ctx).top).c_offset_from((*ctx).start) as libc::c_long > 0_i64); __assert!(((*ctx).top).c_offset_from((*ctx).start) as libc::c_long > 0_i64);
let index: libc::c_int = *(*ctx).top.wrapping_offset(-1_isize); let index: libc::c_int = *(*ctx).top.wrapping_offset(-1_isize);
__assert!( __assert!(matches!(
(*((*parser.document).nodes.start).wrapping_offset((index - 1) as isize)).type_ (*((*parser.document).nodes.start).wrapping_offset((index - 1) as isize)).data,
== YAML_MAPPING_NODE YamlNodeData::Mapping { .. }
); ));
(*(*parser.document) (*(*parser.document)
.nodes .nodes
.start .start

View file

@ -442,12 +442,10 @@ macro_rules! PUSH {
} }
macro_rules! POP { macro_rules! POP {
($stack:expr) => { ($stack:expr) => {{
*{ $stack.top = $stack.top.offset(-1);
$stack.top = $stack.top.offset(-1); core::ptr::read($stack.top)
$stack.top }};
}
};
} }
macro_rules! QUEUE_INIT { macro_rules! QUEUE_INIT {

View file

@ -2,7 +2,7 @@ use crate::libc;
use core::ops::Deref; use core::ops::Deref;
use core::ptr::{self, addr_of}; use core::ptr::{self, addr_of};
pub use self::{yaml_encoding_t::*, yaml_node_type_t::*}; pub use self::yaml_encoding_t::*;
pub use core::primitive::{i64 as ptrdiff_t, u64 as size_t, u8 as yaml_char_t}; pub use core::primitive::{i64 as ptrdiff_t, u64 as size_t, u8 as yaml_char_t};
/// The version directive data. /// The version directive data.
@ -474,114 +474,53 @@ pub enum YamlEventData {
MappingEnd, MappingEnd,
} }
/// Node types.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
#[repr(u32)]
#[non_exhaustive]
pub enum yaml_node_type_t {
/// An empty node.
YAML_NO_NODE = 0,
/// A scalar node.
YAML_SCALAR_NODE = 1,
/// A sequence node.
YAML_SEQUENCE_NODE = 2,
/// A mapping node.
YAML_MAPPING_NODE = 3,
}
/// The node structure. /// The node structure.
#[derive(Copy, Clone)]
#[repr(C)] #[repr(C)]
#[non_exhaustive] #[non_exhaustive]
pub struct yaml_node_t { pub struct yaml_node_t {
/// The node type. /// The node type.
pub type_: yaml_node_type_t, pub data: YamlNodeData,
/// The node tag. /// The node tag.
pub tag: *mut yaml_char_t, pub tag: *mut yaml_char_t,
/// The node data.
///
/// ```
/// # const _: &str = stringify! {
/// union {
/// /// The scalar parameters (for YAML_SCALAR_NODE).
/// scalar: struct {
/// /// The scalar value.
/// value: *mut u8,
/// /// The length of the scalar value.
/// length: u64,
/// /// The scalar style.
/// style: yaml_scalar_style_t,
/// },
/// /// The sequence parameters (for YAML_SEQUENCE_NODE).
/// sequence: struct {
/// /// The stack of sequence items.
/// items: yaml_stack_t<yaml_node_item_t>,
/// /// The sequence style.
/// style: yaml_sequence_style_t,
/// },
/// /// The mapping parameters (for YAML_MAPPING_NODE).
/// mapping: struct {
/// /// The stack of mapping pairs (key, value).
/// pairs: yaml_stack_t<yaml_node_pair_t>,
/// /// The mapping style.
/// style: yaml_mapping_style_t,
/// },
/// }
/// # };
/// ```
pub data: unnamed_yaml_node_t_data,
/// The beginning of the node. /// The beginning of the node.
pub start_mark: yaml_mark_t, pub start_mark: yaml_mark_t,
/// The end of the node. /// The end of the node.
pub end_mark: yaml_mark_t, pub end_mark: yaml_mark_t,
} }
#[derive(Copy, Clone)] /// Node types.
#[repr(C)] #[derive(Default)]
pub union unnamed_yaml_node_t_data { pub enum YamlNodeData {
/// The scalar parameters (for YAML_SCALAR_NODE). /// An empty node.
pub scalar: unnamed_yaml_node_t_data_scalar, #[default]
/// The sequence parameters (for YAML_SEQUENCE_NODE). NoNode,
pub sequence: unnamed_yaml_node_t_data_sequence, /// A scalar node.
/// The mapping parameters (for YAML_MAPPING_NODE). Scalar {
pub mapping: unnamed_yaml_node_t_data_mapping, /// The scalar value.
} value: *mut yaml_char_t,
/// The length of the scalar value.
#[derive(Copy, Clone)] length: size_t,
#[repr(C)] /// The scalar style.
#[non_exhaustive] style: yaml_scalar_style_t,
pub struct unnamed_yaml_node_t_data_scalar { },
/// The scalar value. /// A sequence node.
pub value: *mut yaml_char_t, Sequence {
/// The length of the scalar value. /// The stack of sequence items.
pub length: size_t, items: yaml_stack_t<yaml_node_item_t>,
/// The scalar style. /// The sequence style.
pub style: yaml_scalar_style_t, style: yaml_sequence_style_t,
},
/// A mapping node.
Mapping {
/// The stack of mapping pairs (key, value).
pairs: yaml_stack_t<yaml_node_pair_t>,
/// The mapping style.
style: yaml_mapping_style_t,
},
} }
/// An element of a sequence node. /// An element of a sequence node.
pub type yaml_node_item_t = libc::c_int; pub type yaml_node_item_t = libc::c_int;
#[derive(Copy, Clone)]
#[repr(C)]
#[non_exhaustive]
pub struct unnamed_yaml_node_t_data_sequence {
/// The stack of sequence items.
pub items: yaml_stack_t<yaml_node_item_t>,
/// The sequence style.
pub style: yaml_sequence_style_t,
}
#[derive(Copy, Clone)]
#[repr(C)]
#[non_exhaustive]
pub struct unnamed_yaml_node_t_data_mapping {
/// The stack of mapping pairs (key, value).
pub pairs: yaml_stack_t<yaml_node_pair_t>,
/// The mapping style.
pub style: yaml_mapping_style_t,
}
/// An element of a mapping node. /// An element of a mapping node.
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
#[repr(C)] #[repr(C)]