From 04168ca61257c2d7df518d51aae0d79e6d3c0a78 Mon Sep 17 00:00:00 2001 From: Simon Ask Ulsnes Date: Fri, 2 Feb 2024 09:52:53 +0100 Subject: [PATCH] Parser: Safe token queue, Rustify errors --- Cargo.toml | 7 +- src/bin/run-parser-test-suite.rs | 29 +- src/error.rs | 40 +- src/lib.rs | 28 +- src/loader.rs | 230 ++++---- src/macros.rs | 11 - src/parser.rs | 891 +++++++++++++------------------ src/scanner.rs | 25 +- src/yaml.rs | 21 - 9 files changed, 583 insertions(+), 699 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ff5b6e2..e4b3400 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,9 +14,9 @@ description = "Safer libyaml port, based on unsafe-libyaml" documentation = "https://docs.rs/libyaml-safer" edition = "2021" keywords = ["yaml"] -license = "MIT" +license = "Apache 2.0 OR MIT" repository = "https://github.com/simonask/libyaml-safer" -rust-version = "1.56" +rust-version = "1.70" [workspace] [dev-dependencies] @@ -26,6 +26,9 @@ unsafe-libyaml-test-suite = { path = "tests/data" } [lib] doc-scrape-examples = false +[dependencies] +thiserror = "1.0" + [package.metadata.docs.rs] targets = ["x86_64-unknown-linux-gnu"] rustdoc-args = ["--generate-link-to-definition"] diff --git a/src/bin/run-parser-test-suite.rs b/src/bin/run-parser-test-suite.rs index 6dc2045..b92120b 100644 --- a/src/bin/run-parser-test-suite.rs +++ b/src/bin/run-parser-test-suite.rs @@ -12,14 +12,12 @@ )] use libyaml_safer::{ - yaml_event_delete, yaml_event_t, yaml_parser_delete, yaml_parser_initialize, yaml_parser_parse, - yaml_parser_set_input, yaml_parser_t, YamlEventData, YAML_DOUBLE_QUOTED_SCALAR_STYLE, - YAML_FOLDED_SCALAR_STYLE, YAML_LITERAL_SCALAR_STYLE, YAML_PLAIN_SCALAR_STYLE, - YAML_SINGLE_QUOTED_SCALAR_STYLE, + yaml_parser_delete, yaml_parser_initialize, yaml_parser_parse, yaml_parser_set_input, + yaml_parser_t, YamlEventData, YAML_DOUBLE_QUOTED_SCALAR_STYLE, YAML_FOLDED_SCALAR_STYLE, + YAML_LITERAL_SCALAR_STYLE, YAML_PLAIN_SCALAR_STYLE, YAML_SINGLE_QUOTED_SCALAR_STYLE, }; use std::env; use std::error::Error; -use std::fmt::Write as _; use std::fs::File; use std::io::{self, Read, Write}; use std::mem::MaybeUninit; @@ -38,21 +36,15 @@ pub(crate) unsafe fn unsafe_main( yaml_parser_set_input(&mut parser, stdin); - let mut event = yaml_event_t::default(); loop { - if yaml_parser_parse(&mut parser, &mut event).is_err() { - let mut error = format!("Parse error: {}", parser.problem.unwrap_or("")); - if parser.problem_mark.line != 0 || parser.problem_mark.column != 0 { - let _ = write!( - error, - "\nLine: {} Column: {}", - (parser.problem_mark.line).wrapping_add(1_u64), - (parser.problem_mark.column).wrapping_add(1_u64), - ); + let event = match yaml_parser_parse(&mut parser) { + Err(err) => { + let error = format!("Parse error: {}", err); + yaml_parser_delete(&mut parser); + return Err(error.into()); } - yaml_parser_delete(&mut parser); - return Err(error.into()); - } + Ok(event) => event, + }; let mut is_end = false; @@ -137,7 +129,6 @@ pub(crate) unsafe fn unsafe_main( } } - yaml_event_delete(&mut event); if is_end { break; } diff --git a/src/error.rs b/src/error.rs index 3f978a1..b5533b5 100644 --- a/src/error.rs +++ b/src/error.rs @@ -30,7 +30,7 @@ pub enum ReaderError { #[derive(Debug, thiserror::Error)] pub enum ScannerError { - #[error("{problem}")] + #[error("{}:{}: {} {} ({}:{})", problem_mark.line, problem_mark.column, problem, context, context_mark.line, context_mark.column)] Problem { context: &'static str, context_mark: yaml_mark_t, @@ -40,3 +40,41 @@ pub enum ScannerError { #[error(transparent)] Reader(#[from] ReaderError), } + +#[derive(Debug, thiserror::Error)] +pub enum ParserError { + #[error("no more tokens")] + UnexpectedEof, + #[error("{}:{}: {}", mark.line, mark.column, problem)] + Problem { + problem: &'static str, + mark: yaml_mark_t, + }, + #[error("{}:{}: {} {} ({}:{})", mark.line, mark.column, problem, context, context_mark.line, context_mark.column)] + ProblemWithContext { + context: &'static str, + context_mark: yaml_mark_t, + problem: &'static str, + mark: yaml_mark_t, + }, + #[error(transparent)] + Scanner(#[from] ScannerError), +} + +#[derive(Debug, thiserror::Error)] +pub enum ComposerError { + #[error("{}:{}: {}", mark.line, mark.column, problem)] + Problem { + problem: &'static str, + mark: yaml_mark_t, + }, + #[error("{}:{}: {} {} ({}:{})", mark.line, mark.column, problem, context, context_mark.line, context_mark.column)] + ProblemWithContext { + context: &'static str, + context_mark: yaml_mark_t, + problem: &'static str, + mark: yaml_mark_t, + }, + #[error(transparent)] + Parser(#[from] ParserError), +} diff --git a/src/lib.rs b/src/lib.rs index 75610a5..b7e1df8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -169,9 +169,7 @@ tie-fighter: '|\-*-/|' let mut read_in = SANITY_INPUT.as_bytes(); yaml_parser_set_input_string(&mut parser, &mut read_in); let mut doc = yaml_document_t::default(); - if yaml_parser_load(&mut parser, &mut doc).is_err() { - panic!("parser error: {:?} {:?}", parser.error, parser.problem); - } + yaml_parser_load(&mut parser, &mut doc).unwrap(); // let mut doc = doc.assume_init(); // let mut emitter = core::mem::MaybeUninit::uninit(); @@ -196,6 +194,22 @@ tie-fighter: '|\-*-/|' } } + const TEST_CASE_QF4Y: &str = r#"[ +foo: bar +] +"#; + + #[test] + fn test_case() { + let mut parser = parser_new(); + let mut input = TEST_CASE_QF4Y.as_bytes(); + yaml_parser_set_input_string(&mut parser, &mut input); + let mut doc = yaml_document_t::default(); + unsafe { + yaml_parser_load(&mut parser, &mut doc).unwrap(); + } + } + // #[test] // fn integration_s7bg() { // unsafe { @@ -288,4 +302,12 @@ tie-fighter: '|\-*-/|' emitter.assume_init() } } + + fn parser_new<'w>() -> yaml_parser_t<'w> { + unsafe { + let mut emitter = core::mem::MaybeUninit::uninit(); + yaml_parser_initialize(emitter.as_mut_ptr()).unwrap(); + emitter.assume_init() + } + } } diff --git a/src/loader.rs b/src/loader.rs index fea9bf7..4b97401 100644 --- a/src/loader.rs +++ b/src/loader.rs @@ -4,8 +4,7 @@ use alloc::{vec, vec::Vec}; use crate::yaml::{YamlEventData, YamlNodeData}; use crate::{ libc, yaml_alias_data_t, yaml_document_delete, yaml_document_t, yaml_event_t, yaml_mark_t, - yaml_node_pair_t, yaml_node_t, yaml_parser_parse, yaml_parser_t, YAML_COMPOSER_ERROR, - YAML_MEMORY_ERROR, + yaml_node_pair_t, yaml_node_t, yaml_parser_parse, yaml_parser_t, ComposerError, }; use core::mem::MaybeUninit; @@ -26,65 +25,71 @@ use core::mem::MaybeUninit; pub unsafe fn yaml_parser_load( parser: &mut yaml_parser_t, document: &mut yaml_document_t, -) -> Result<(), ()> { - let mut event = yaml_event_t::default(); +) -> Result<(), ComposerError> { *document = yaml_document_t::default(); document.nodes.reserve(16); if !parser.stream_start_produced { - if let Err(()) = yaml_parser_parse(parser, &mut event) { - yaml_parser_delete_aliases(parser); - yaml_document_delete(document); - return Err(()); - } else { - if let YamlEventData::StreamStart { .. } = &event.data { - } else { - panic!("expected stream start"); + match yaml_parser_parse(parser) { + Ok(yaml_event_t { + data: YamlEventData::StreamStart { .. }, + .. + }) => (), + Ok(_) => panic!("expected stream start"), + Err(err) => { + yaml_parser_delete_aliases(parser); + yaml_document_delete(document); + return Err(err.into()); } } } if parser.stream_end_produced { return Ok(()); } - if let Ok(()) = yaml_parser_parse(parser, &mut event) { - if let YamlEventData::StreamEnd = &event.data { - return Ok(()); - } - parser.aliases.reserve(16); - if let Ok(()) = yaml_parser_load_document(parser, &mut event, document) { - yaml_parser_delete_aliases(parser); - return Ok(()); + let err: ComposerError; + match yaml_parser_parse(parser) { + Ok(event) => { + if let YamlEventData::StreamEnd = &event.data { + return Ok(()); + } + parser.aliases.reserve(16); + match yaml_parser_load_document(parser, event, document) { + Ok(()) => { + yaml_parser_delete_aliases(parser); + return Ok(()); + } + Err(e) => err = e, + } } + Err(e) => err = e.into(), } yaml_parser_delete_aliases(parser); yaml_document_delete(document); - Err(()) + Err(err) } -fn yaml_parser_set_composer_error( - parser: &mut yaml_parser_t, +fn yaml_parser_set_composer_error( problem: &'static str, problem_mark: yaml_mark_t, -) -> Result<(), ()> { - parser.error = YAML_COMPOSER_ERROR; - parser.problem = Some(problem); - parser.problem_mark = problem_mark; - Err(()) +) -> Result { + Err(ComposerError::Problem { + problem, + mark: problem_mark, + }) } -fn yaml_parser_set_composer_error_context( - parser: &mut yaml_parser_t, +fn yaml_parser_set_composer_error_context( context: &'static str, context_mark: yaml_mark_t, problem: &'static str, problem_mark: yaml_mark_t, -) -> Result<(), ()> { - parser.error = YAML_COMPOSER_ERROR; - parser.context = Some(context); - parser.context_mark = context_mark; - parser.problem = Some(problem); - parser.problem_mark = problem_mark; - Err(()) +) -> Result { + Err(ComposerError::ProblemWithContext { + context, + context_mark, + problem, + mark: problem_mark, + }) } unsafe fn yaml_parser_delete_aliases(parser: &mut yaml_parser_t) { @@ -93,24 +98,24 @@ unsafe fn yaml_parser_delete_aliases(parser: &mut yaml_parser_t) { unsafe fn yaml_parser_load_document( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, + event: yaml_event_t, document: &mut yaml_document_t, -) -> Result<(), ()> { +) -> Result<(), ComposerError> { let mut ctx = vec![]; if let YamlEventData::DocumentStart { version_directive, tag_directives, implicit, - } = &mut event.data + } = event.data { - document.version_directive = *version_directive; - document.tag_directives = core::mem::take(tag_directives); - document.start_implicit = *implicit; + document.version_directive = version_directive; + document.tag_directives = tag_directives; + document.start_implicit = implicit; document.start_mark = event.start_mark; ctx.reserve(16); - if let Err(()) = yaml_parser_load_nodes(parser, document, &mut ctx) { + if let Err(err) = yaml_parser_load_nodes(parser, document, &mut ctx) { ctx.clear(); - return Err(()); + return Err(err); } ctx.clear(); Ok(()) @@ -123,40 +128,39 @@ unsafe fn yaml_parser_load_nodes( parser: &mut yaml_parser_t, document: &mut yaml_document_t, ctx: &mut Vec, -) -> Result<(), ()> { - let mut event = yaml_event_t::default(); +) -> Result<(), ComposerError> { let end_implicit; let end_mark; loop { - yaml_parser_parse(parser, &mut event)?; - match &event.data { + let event = yaml_parser_parse(parser)?; + match event.data { YamlEventData::NoEvent => panic!("empty event"), YamlEventData::StreamStart { .. } => panic!("unexpected stream start event"), YamlEventData::StreamEnd => panic!("unexpected stream end event"), YamlEventData::DocumentStart { .. } => panic!("unexpected document start event"), YamlEventData::DocumentEnd { implicit } => { - end_implicit = *implicit; + end_implicit = implicit; end_mark = event.end_mark; break; } YamlEventData::Alias { .. } => { - yaml_parser_load_alias(parser, &mut event, document, ctx)?; + yaml_parser_load_alias(parser, event, document, ctx)?; } YamlEventData::Scalar { .. } => { - yaml_parser_load_scalar(parser, &mut event, document, ctx)?; + yaml_parser_load_scalar(parser, event, document, ctx)?; } YamlEventData::SequenceStart { .. } => { - yaml_parser_load_sequence(parser, &mut event, document, ctx)?; + yaml_parser_load_sequence(parser, event, document, ctx)?; } YamlEventData::SequenceEnd => { - yaml_parser_load_sequence_end(parser, &mut event, document, ctx)?; + yaml_parser_load_sequence_end(parser, event, document, ctx)?; } YamlEventData::MappingStart { .. } => { - yaml_parser_load_mapping(parser, &mut event, document, ctx)?; + yaml_parser_load_mapping(parser, event, document, ctx)?; } YamlEventData::MappingEnd => { - yaml_parser_load_mapping_end(parser, &mut event, document, ctx)?; + yaml_parser_load_mapping_end(parser, event, document, ctx)?; } } } @@ -170,7 +174,7 @@ unsafe fn yaml_parser_register_anchor( document: &mut yaml_document_t, index: libc::c_int, anchor: Option, -) -> Result<(), ()> { +) -> Result<(), ComposerError> { let Some(anchor) = anchor else { return Ok(()); }; @@ -182,7 +186,6 @@ unsafe fn yaml_parser_register_anchor( for alias_data in parser.aliases.iter() { if alias_data.anchor == data.anchor { return yaml_parser_set_composer_error_context( - parser, "found duplicate anchor; first occurrence", alias_data.mark, "second occurrence", @@ -195,11 +198,10 @@ unsafe fn yaml_parser_register_anchor( } unsafe fn yaml_parser_load_node_add( - parser: &mut yaml_parser_t, document: &mut yaml_document_t, ctx: &mut Vec, index: libc::c_int, -) -> Result<(), ()> { +) -> Result<(), ComposerError> { if ctx.is_empty() { return Ok(()); } @@ -207,7 +209,6 @@ unsafe fn yaml_parser_load_node_add( let parent = &mut document.nodes[parent_index as usize - 1]; match parent.data { YamlNodeData::Sequence { ref mut items, .. } => { - STACK_LIMIT!(parser, items)?; items.push(index); } YamlNodeData::Mapping { ref mut pairs, .. } => { @@ -224,7 +225,6 @@ unsafe fn yaml_parser_load_node_add( if do_push { (*pair).key = index; (*pair).value = 0; - STACK_LIMIT!(parser, pairs)?; pairs.push(*pair); } } @@ -237,10 +237,10 @@ unsafe fn yaml_parser_load_node_add( unsafe fn yaml_parser_load_alias( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, // TODO: Take by value + event: yaml_event_t, document: &mut yaml_document_t, ctx: &mut Vec, -) -> Result<(), ()> { +) -> Result<(), ComposerError> { let anchor: &str = if let YamlEventData::Alias { anchor } = &event.data { &*anchor } else { @@ -249,72 +249,64 @@ unsafe fn yaml_parser_load_alias( for alias_data in parser.aliases.iter() { if alias_data.anchor == anchor { - return yaml_parser_load_node_add(parser, document, ctx, alias_data.index); + return yaml_parser_load_node_add(document, ctx, alias_data.index); } } - yaml_parser_set_composer_error(parser, "found undefined alias", event.start_mark) + yaml_parser_set_composer_error("found undefined alias", event.start_mark) } unsafe fn yaml_parser_load_scalar( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, // TODO: Take by value + event: yaml_event_t, document: &mut yaml_document_t, ctx: &mut Vec, -) -> Result<(), ()> { - let (mut tag, value, style, anchor) = if let YamlEventData::Scalar { - tag, +) -> Result<(), ComposerError> { + let YamlEventData::Scalar { + mut tag, value, style, anchor, .. - } = &event.data - { - (tag.clone(), value, *style, anchor.clone()) - } else { + } = event.data + else { unreachable!() }; let index: libc::c_int; - if let Ok(()) = STACK_LIMIT!(parser, document.nodes) { - if tag.is_none() || tag.as_deref() == Some("!") { - tag = Some(String::from("tag:yaml.org,2002:str")); - } - let node = yaml_node_t { - data: YamlNodeData::Scalar { - value: value.clone(), // TODO: move - style, - }, - tag, - start_mark: (*event).start_mark, - end_mark: (*event).end_mark, - }; - document.nodes.push(node); - index = document.nodes.len() as libc::c_int; - yaml_parser_register_anchor(parser, document, index, anchor)?; - return yaml_parser_load_node_add(parser, document, ctx, index); + if tag.is_none() || tag.as_deref() == Some("!") { + tag = Some(String::from("tag:yaml.org,2002:str")); } - Err(()) + let node = yaml_node_t { + data: YamlNodeData::Scalar { value, style }, + tag, + start_mark: event.start_mark, + end_mark: event.end_mark, + }; + document.nodes.push(node); + index = document.nodes.len() as libc::c_int; + yaml_parser_register_anchor(parser, document, index, anchor)?; + yaml_parser_load_node_add(document, ctx, index) } unsafe fn yaml_parser_load_sequence( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, // TODO: Take by value. + event: yaml_event_t, document: &mut yaml_document_t, ctx: &mut Vec, -) -> Result<(), ()> { - let (mut tag, style, anchor) = if let YamlEventData::SequenceStart { - anchor, tag, style, .. - } = &event.data - { - (tag.clone(), *style, anchor) - } else { +) -> Result<(), ComposerError> { + let YamlEventData::SequenceStart { + anchor, + mut tag, + style, + .. + } = event.data + else { unreachable!() }; let mut items = Vec::with_capacity(16); let index: libc::c_int; - STACK_LIMIT!(parser, document.nodes)?; if tag.is_none() || tag.as_deref() == Some("!") { tag = Some(String::from("tag:yaml.org,2002:seq")); } @@ -332,47 +324,46 @@ unsafe fn yaml_parser_load_sequence( document.nodes.push(node); index = document.nodes.len() as libc::c_int; yaml_parser_register_anchor(parser, document, index, anchor.clone())?; - yaml_parser_load_node_add(parser, document, ctx, index)?; - STACK_LIMIT!(parser, *ctx)?; + yaml_parser_load_node_add(document, ctx, index)?; ctx.push(index); Ok(()) } unsafe fn yaml_parser_load_sequence_end( _parser: &mut yaml_parser_t, - event: *mut yaml_event_t, + event: yaml_event_t, document: &mut yaml_document_t, ctx: &mut Vec, -) -> Result<(), ()> { +) -> Result<(), ComposerError> { __assert!(!ctx.is_empty()); let index: libc::c_int = *ctx.last().unwrap(); __assert!(matches!( document.nodes[index as usize - 1].data, YamlNodeData::Sequence { .. } )); - document.nodes[index as usize - 1].end_mark = (*event).end_mark; + document.nodes[index as usize - 1].end_mark = event.end_mark; _ = ctx.pop(); Ok(()) } unsafe fn yaml_parser_load_mapping( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, // TODO: take by value + event: yaml_event_t, document: &mut yaml_document_t, ctx: &mut Vec, -) -> Result<(), ()> { - let (mut tag, style, anchor) = if let YamlEventData::MappingStart { - anchor, tag, style, .. - } = &event.data - { - (tag.clone(), *style, anchor.clone()) - } else { +) -> Result<(), ComposerError> { + let YamlEventData::MappingStart { + anchor, + mut tag, + style, + .. + } = event.data + else { unreachable!() }; let mut pairs = Vec::with_capacity(16); let index: libc::c_int; - STACK_LIMIT!(parser, document.nodes)?; if tag.is_none() || tag.as_deref() == Some("!") { tag = Some(String::from("tag:yaml.org,2002:map")); } @@ -388,25 +379,24 @@ unsafe fn yaml_parser_load_mapping( document.nodes.push(node); index = document.nodes.len() as libc::c_int; yaml_parser_register_anchor(parser, document, index, anchor)?; - yaml_parser_load_node_add(parser, document, ctx, index)?; - STACK_LIMIT!(parser, *ctx)?; + yaml_parser_load_node_add(document, ctx, index)?; ctx.push(index); Ok(()) } unsafe fn yaml_parser_load_mapping_end( _parser: &mut yaml_parser_t, - event: *mut yaml_event_t, + event: yaml_event_t, document: &mut yaml_document_t, ctx: &mut Vec, -) -> Result<(), ()> { +) -> Result<(), ComposerError> { __assert!(!ctx.is_empty()); let index: libc::c_int = *ctx.last().unwrap(); __assert!(matches!( document.nodes[index as usize - 1].data, YamlNodeData::Mapping { .. } )); - document.nodes[index as usize - 1].end_mark = (*event).end_mark; + document.nodes[index as usize - 1].end_mark = event.end_mark; _ = ctx.pop(); Ok(()) } diff --git a/src/macros.rs b/src/macros.rs index ce244a3..bb3bf14 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -268,17 +268,6 @@ macro_rules! IS_BLANKZ { }; } -macro_rules! STACK_LIMIT { - ($context:expr, $stack:expr) => { - if $stack.len() < libc::c_int::MAX as usize - 1 { - Ok(()) - } else { - (*$context).error = YAML_MEMORY_ERROR; - Err(()) - } - }; -} - pub(crate) fn vecdeque_starts_with( vec: &alloc::collections::VecDeque, needle: &[T], diff --git a/src/parser.rs b/src/parser.rs index a2e005e..394863f 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -5,13 +5,13 @@ use crate::scanner::yaml_parser_fetch_more_tokens; use crate::yaml::{YamlEventData, YamlTokenData}; use crate::{ yaml_event_t, yaml_mark_t, yaml_parser_t, yaml_tag_directive_t, yaml_token_t, - yaml_version_directive_t, YAML_BLOCK_MAPPING_STYLE, YAML_BLOCK_SEQUENCE_STYLE, - YAML_FLOW_MAPPING_STYLE, YAML_FLOW_SEQUENCE_STYLE, YAML_NO_ERROR, YAML_PARSER_ERROR, - YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE, YAML_PARSE_BLOCK_MAPPING_KEY_STATE, - YAML_PARSE_BLOCK_MAPPING_VALUE_STATE, YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE, - YAML_PARSE_BLOCK_NODE_STATE, YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE, - YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE, YAML_PARSE_DOCUMENT_CONTENT_STATE, - YAML_PARSE_DOCUMENT_END_STATE, YAML_PARSE_DOCUMENT_START_STATE, YAML_PARSE_END_STATE, + yaml_version_directive_t, ParserError, YAML_BLOCK_MAPPING_STYLE, YAML_BLOCK_SEQUENCE_STYLE, + YAML_FLOW_MAPPING_STYLE, YAML_FLOW_SEQUENCE_STYLE, YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE, + YAML_PARSE_BLOCK_MAPPING_KEY_STATE, YAML_PARSE_BLOCK_MAPPING_VALUE_STATE, + YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE, YAML_PARSE_BLOCK_NODE_STATE, + YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE, YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE, + YAML_PARSE_DOCUMENT_CONTENT_STATE, YAML_PARSE_DOCUMENT_END_STATE, + YAML_PARSE_DOCUMENT_START_STATE, YAML_PARSE_END_STATE, YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE, YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE, YAML_PARSE_FLOW_MAPPING_KEY_STATE, YAML_PARSE_FLOW_MAPPING_VALUE_STATE, YAML_PARSE_FLOW_NODE_STATE, YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE, @@ -21,26 +21,53 @@ use crate::{ YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE, YAML_PARSE_STREAM_START_STATE, YAML_PLAIN_SCALAR_STYLE, }; -use core::ptr; -unsafe fn PEEK_TOKEN(parser: &mut yaml_parser_t) -> *mut yaml_token_t { - if parser.token_available || yaml_parser_fetch_more_tokens(parser).is_ok() { - parser.tokens.front_mut().unwrap() as *mut _ - } else { - ptr::null_mut::() +unsafe fn PEEK_TOKEN<'a>(parser: &'a mut yaml_parser_t) -> Result<&'a yaml_token_t, ParserError> { + if parser.token_available { + return Ok(parser + .tokens + .front() + .expect("token_available is true, but token queue is empty")); } + yaml_parser_fetch_more_tokens(parser)?; + if !parser.token_available { + return Err(ParserError::UnexpectedEof); + } + Ok(parser + .tokens + .front() + .expect("token_available is true, but token queue is empty")) +} + +unsafe fn PEEK_TOKEN_MUT<'a>( + parser: &'a mut yaml_parser_t, +) -> Result<&'a mut yaml_token_t, ParserError> { + if parser.token_available { + return Ok(parser + .tokens + .front_mut() + .expect("token_available is true, but token queue is empty")); + } + yaml_parser_fetch_more_tokens(parser)?; + if !parser.token_available { + return Err(ParserError::UnexpectedEof); + } + Ok(parser + .tokens + .front_mut() + .expect("token_available is true, but token queue is empty")) } unsafe fn SKIP_TOKEN(parser: &mut yaml_parser_t) { parser.token_available = false; parser.tokens_parsed = parser.tokens_parsed.wrapping_add(1); - let skipped = parser.tokens.pop_front(); + let skipped = parser.tokens.pop_front().expect("SKIP_TOKEN but EOF"); parser.stream_end_produced = matches!( skipped, - Some(yaml_token_t { + yaml_token_t { data: YamlTokenData::StreamEnd, .. - }) + } ); } @@ -56,283 +83,235 @@ unsafe fn SKIP_TOKEN(parser: &mut yaml_parser_t) { /// An application must not alternate the calls of yaml_parser_parse() with the /// calls of yaml_parser_scan() or yaml_parser_load(). Doing this will break the /// parser. -pub unsafe fn yaml_parser_parse( - parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { - *event = yaml_event_t::default(); - if parser.stream_end_produced - || parser.error != YAML_NO_ERROR - || parser.state == YAML_PARSE_END_STATE - { - return Ok(()); +pub unsafe fn yaml_parser_parse(parser: &mut yaml_parser_t) -> Result { + if parser.stream_end_produced || parser.state == YAML_PARSE_END_STATE { + return Ok(yaml_event_t { + data: YamlEventData::StreamEnd, + ..Default::default() + }); } - yaml_parser_state_machine(parser, event) + yaml_parser_state_machine(parser) } -fn yaml_parser_set_parser_error( - parser: &mut yaml_parser_t, +fn yaml_parser_set_parser_error( problem: &'static str, problem_mark: yaml_mark_t, -) { - parser.error = YAML_PARSER_ERROR; - parser.problem = Some(problem); - parser.problem_mark = problem_mark; +) -> Result { + Err(ParserError::Problem { + problem, + mark: problem_mark, + }) } -fn yaml_parser_set_parser_error_context( - parser: &mut yaml_parser_t, +fn yaml_parser_set_parser_error_context( context: &'static str, context_mark: yaml_mark_t, problem: &'static str, problem_mark: yaml_mark_t, -) { - parser.error = YAML_PARSER_ERROR; - parser.context = Some(context); - parser.context_mark = context_mark; - parser.problem = Some(problem); - parser.problem_mark = problem_mark; +) -> Result { + Err(ParserError::ProblemWithContext { + context, + context_mark, + problem, + mark: problem_mark, + }) } unsafe fn yaml_parser_state_machine( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { +) -> Result { match parser.state { - YAML_PARSE_STREAM_START_STATE => yaml_parser_parse_stream_start(parser, event), - YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE => { - yaml_parser_parse_document_start(parser, event, true) - } - YAML_PARSE_DOCUMENT_START_STATE => yaml_parser_parse_document_start(parser, event, false), - YAML_PARSE_DOCUMENT_CONTENT_STATE => yaml_parser_parse_document_content(parser, event), - YAML_PARSE_DOCUMENT_END_STATE => yaml_parser_parse_document_end(parser, event), - YAML_PARSE_BLOCK_NODE_STATE => yaml_parser_parse_node(parser, event, true, false), + YAML_PARSE_STREAM_START_STATE => yaml_parser_parse_stream_start(parser), + YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE => yaml_parser_parse_document_start(parser, true), + YAML_PARSE_DOCUMENT_START_STATE => yaml_parser_parse_document_start(parser, false), + YAML_PARSE_DOCUMENT_CONTENT_STATE => yaml_parser_parse_document_content(parser), + YAML_PARSE_DOCUMENT_END_STATE => yaml_parser_parse_document_end(parser), + YAML_PARSE_BLOCK_NODE_STATE => yaml_parser_parse_node(parser, true, false), YAML_PARSE_BLOCK_NODE_OR_INDENTLESS_SEQUENCE_STATE => { - yaml_parser_parse_node(parser, event, true, true) + yaml_parser_parse_node(parser, true, true) } - YAML_PARSE_FLOW_NODE_STATE => yaml_parser_parse_node(parser, event, false, false), + YAML_PARSE_FLOW_NODE_STATE => yaml_parser_parse_node(parser, false, false), YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE => { - yaml_parser_parse_block_sequence_entry(parser, event, true) + yaml_parser_parse_block_sequence_entry(parser, true) } YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE => { - yaml_parser_parse_block_sequence_entry(parser, event, false) + yaml_parser_parse_block_sequence_entry(parser, false) } YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE => { - yaml_parser_parse_indentless_sequence_entry(parser, event) + yaml_parser_parse_indentless_sequence_entry(parser) } YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE => { - yaml_parser_parse_block_mapping_key(parser, event, true) - } - YAML_PARSE_BLOCK_MAPPING_KEY_STATE => { - yaml_parser_parse_block_mapping_key(parser, event, false) - } - YAML_PARSE_BLOCK_MAPPING_VALUE_STATE => { - yaml_parser_parse_block_mapping_value(parser, event) + yaml_parser_parse_block_mapping_key(parser, true) } + YAML_PARSE_BLOCK_MAPPING_KEY_STATE => yaml_parser_parse_block_mapping_key(parser, false), + YAML_PARSE_BLOCK_MAPPING_VALUE_STATE => yaml_parser_parse_block_mapping_value(parser), YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE => { - yaml_parser_parse_flow_sequence_entry(parser, event, true) + yaml_parser_parse_flow_sequence_entry(parser, true) } YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE => { - yaml_parser_parse_flow_sequence_entry(parser, event, false) + yaml_parser_parse_flow_sequence_entry(parser, false) } YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE => { - yaml_parser_parse_flow_sequence_entry_mapping_key(parser, event) + yaml_parser_parse_flow_sequence_entry_mapping_key(parser) } YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE => { - yaml_parser_parse_flow_sequence_entry_mapping_value(parser, event) + yaml_parser_parse_flow_sequence_entry_mapping_value(parser) } YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE => { - yaml_parser_parse_flow_sequence_entry_mapping_end(parser, event) - } - YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE => { - yaml_parser_parse_flow_mapping_key(parser, event, true) - } - YAML_PARSE_FLOW_MAPPING_KEY_STATE => { - yaml_parser_parse_flow_mapping_key(parser, event, false) - } - YAML_PARSE_FLOW_MAPPING_VALUE_STATE => { - yaml_parser_parse_flow_mapping_value(parser, event, false) + yaml_parser_parse_flow_sequence_entry_mapping_end(parser) } + YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE => yaml_parser_parse_flow_mapping_key(parser, true), + YAML_PARSE_FLOW_MAPPING_KEY_STATE => yaml_parser_parse_flow_mapping_key(parser, false), + YAML_PARSE_FLOW_MAPPING_VALUE_STATE => yaml_parser_parse_flow_mapping_value(parser, false), YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE => { - yaml_parser_parse_flow_mapping_value(parser, event, true) + yaml_parser_parse_flow_mapping_value(parser, true) } - _ => Err(()), + YAML_PARSE_END_STATE => panic!("parser end state reached unexpectedly"), } } unsafe fn yaml_parser_parse_stream_start( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { - let token: *mut yaml_token_t = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - let token = &mut *token; +) -> Result { + let token = PEEK_TOKEN(parser)?; if let YamlTokenData::StreamStart { encoding } = &token.data { - parser.state = YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE; - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::StreamStart { encoding: *encoding, }, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, + start_mark: token.start_mark, + end_mark: token.end_mark, }; + parser.state = YAML_PARSE_IMPLICIT_DOCUMENT_START_STATE; SKIP_TOKEN(parser); - Ok(()) + Ok(event) } else { - yaml_parser_set_parser_error( - parser, - "did not find expected ", - (*token).start_mark, - ); - return Err(()); + let mark = token.start_mark; + yaml_parser_set_parser_error("did not find expected ", mark) } } unsafe fn yaml_parser_parse_document_start( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, implicit: bool, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; +) -> Result { let mut version_directive: Option = None; let mut tag_directives = vec![]; - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } + let mut token = PEEK_TOKEN(parser)?; if !implicit { - while let YamlTokenData::DocumentEnd = &(*token).data { + while let YamlTokenData::DocumentEnd = &token.data { SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } + token = PEEK_TOKEN(parser)?; } } if implicit - && !(*token).data.is_version_directive() - && !(*token).data.is_tag_directive() - && !(*token).data.is_document_start() - && !(*token).data.is_stream_end() + && !token.data.is_version_directive() + && !token.data.is_tag_directive() + && !token.data.is_document_start() + && !token.data.is_stream_end() { - yaml_parser_process_directives(parser, None, None)?; - parser.states.push(YAML_PARSE_DOCUMENT_END_STATE); - parser.state = YAML_PARSE_BLOCK_NODE_STATE; - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::DocumentStart { version_directive: None, tag_directives: vec![], implicit: true, }, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, + start_mark: token.start_mark, + end_mark: token.end_mark, }; - Ok(()) - } else if !(*token).data.is_stream_end() { + yaml_parser_process_directives(parser, None, None)?; + parser.states.push(YAML_PARSE_DOCUMENT_END_STATE); + parser.state = YAML_PARSE_BLOCK_NODE_STATE; + Ok(event) + } else if !token.data.is_stream_end() { let end_mark: yaml_mark_t; - let start_mark: yaml_mark_t = (*token).start_mark; + let start_mark: yaml_mark_t = token.start_mark; yaml_parser_process_directives( parser, Some(&mut version_directive), Some(&mut tag_directives), )?; - token = PEEK_TOKEN(parser); - if !token.is_null() { - if !(*token).data.is_document_start() { - yaml_parser_set_parser_error( - parser, - "did not find expected ", - (*token).start_mark, - ); - } else { - parser.states.push(YAML_PARSE_DOCUMENT_END_STATE); - parser.state = YAML_PARSE_DOCUMENT_CONTENT_STATE; - end_mark = (*token).end_mark; - *event = yaml_event_t { - data: YamlEventData::DocumentStart { - version_directive, - tag_directives: core::mem::take(&mut tag_directives), - implicit: false, - }, - start_mark, - end_mark, - }; - SKIP_TOKEN(parser); - return Ok(()); - } + token = PEEK_TOKEN(parser)?; + if !token.data.is_document_start() { + return yaml_parser_set_parser_error( + "did not find expected ", + token.start_mark, + ); + } else { + end_mark = token.end_mark; + let event = yaml_event_t { + data: YamlEventData::DocumentStart { + version_directive, + tag_directives: core::mem::take(&mut tag_directives), + implicit: false, + }, + start_mark, + end_mark, + }; + parser.states.push(YAML_PARSE_DOCUMENT_END_STATE); + parser.state = YAML_PARSE_DOCUMENT_CONTENT_STATE; + SKIP_TOKEN(parser); + return Ok(event); } - Err(()) } else { - parser.state = YAML_PARSE_END_STATE; - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::StreamEnd, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, + start_mark: token.start_mark, + end_mark: token.end_mark, }; + parser.state = YAML_PARSE_END_STATE; SKIP_TOKEN(parser); - Ok(()) + Ok(event) } } unsafe fn yaml_parser_parse_document_content( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { - let token: *mut yaml_token_t = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } +) -> Result { + let token = PEEK_TOKEN(parser)?; if let YamlTokenData::VersionDirective { .. } | YamlTokenData::TagDirective { .. } | YamlTokenData::DocumentStart | YamlTokenData::DocumentEnd - | YamlTokenData::StreamEnd = &(*token).data + | YamlTokenData::StreamEnd = &token.data { + let mark = token.start_mark; parser.state = parser.states.pop().unwrap(); - yaml_parser_process_empty_scalar(event, (*token).start_mark) + yaml_parser_process_empty_scalar(mark) } else { - yaml_parser_parse_node(parser, event, true, false) + yaml_parser_parse_node(parser, true, false) } } unsafe fn yaml_parser_parse_document_end( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { +) -> Result { let mut end_mark: yaml_mark_t; let mut implicit = true; - let token: *mut yaml_token_t = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - end_mark = (*token).start_mark; + let token = PEEK_TOKEN(parser)?; + end_mark = token.start_mark; let start_mark: yaml_mark_t = end_mark; - if let YamlTokenData::DocumentEnd = &(*token).data { - end_mark = (*token).end_mark; + if let YamlTokenData::DocumentEnd = &token.data { + end_mark = token.end_mark; SKIP_TOKEN(parser); implicit = false; } parser.tag_directives.clear(); parser.state = YAML_PARSE_DOCUMENT_START_STATE; - *event = yaml_event_t { + Ok(yaml_event_t { data: YamlEventData::DocumentEnd { implicit }, start_mark, end_mark, - }; - Ok(()) + }) } unsafe fn yaml_parser_parse_node( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, block: bool, indentless_sequence: bool, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; +) -> Result { let mut anchor: Option = None; let mut tag_handle: Option = None; let mut tag_suffix: Option = None; @@ -346,63 +325,48 @@ unsafe fn yaml_parser_parse_node( }; let implicit; - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } + let mut token = PEEK_TOKEN_MUT(parser)?; - if let YamlTokenData::Alias { value } = &mut (*token).data { - parser.state = parser.states.pop().unwrap(); - *event = yaml_event_t { + if let YamlTokenData::Alias { value } = &mut token.data { + let event = yaml_event_t { data: YamlEventData::Alias { anchor: core::mem::take(value), }, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, + start_mark: token.start_mark, + end_mark: token.end_mark, }; + parser.state = parser.states.pop().unwrap(); SKIP_TOKEN(parser); - return Ok(()); + return Ok(event); } - end_mark = (*token).start_mark; + end_mark = token.start_mark; start_mark = end_mark; - if let YamlTokenData::Anchor { value } = &mut (*token).data { + if let YamlTokenData::Anchor { value } = &mut token.data { anchor = Some(core::mem::take(value)); - start_mark = (*token).start_mark; - end_mark = (*token).end_mark; + start_mark = token.start_mark; + end_mark = token.end_mark; SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } else if let YamlTokenData::Tag { handle, suffix } = &mut (*token).data { + token = PEEK_TOKEN_MUT(parser)?; + if let YamlTokenData::Tag { handle, suffix } = &mut token.data { tag_handle = Some(core::mem::take(handle)); tag_suffix = Some(core::mem::take(suffix)); - tag_mark = (*token).start_mark; - end_mark = (*token).end_mark; + tag_mark = token.start_mark; + end_mark = token.end_mark; SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } } - } else if let YamlTokenData::Tag { handle, suffix } = &mut (*token).data { + } else if let YamlTokenData::Tag { handle, suffix } = &mut token.data { tag_handle = Some(core::mem::take(handle)); tag_suffix = Some(core::mem::take(suffix)); - tag_mark = (*token).start_mark; + tag_mark = token.start_mark; start_mark = tag_mark; - end_mark = (*token).end_mark; + end_mark = token.end_mark; SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } else if let YamlTokenData::Anchor { value } = &mut (*token).data { + token = PEEK_TOKEN_MUT(parser)?; + if let YamlTokenData::Anchor { value } = &mut token.data { anchor = Some(core::mem::take(value)); - end_mark = (*token).end_mark; + end_mark = token.end_mark; SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } } } @@ -418,24 +382,24 @@ unsafe fn yaml_parser_parse_node( } } if tag.is_none() { - yaml_parser_set_parser_error_context( - parser, + return yaml_parser_set_parser_error_context( "while parsing a node", start_mark, "found undefined tag handle", tag_mark, ); - return Err(()); } } } + let token = PEEK_TOKEN_MUT(parser)?; + implicit = tag.is_none() || tag.as_deref() == Some(""); - if indentless_sequence && (*token).data.is_block_entry() { - end_mark = (*token).end_mark; + if indentless_sequence && token.data.is_block_entry() { + end_mark = token.end_mark; parser.state = YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE; - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::SequenceStart { anchor, tag, @@ -445,18 +409,17 @@ unsafe fn yaml_parser_parse_node( start_mark, end_mark, }; - return Ok(()); - } else if let YamlTokenData::Scalar { value, style } = &mut (*token).data { + return Ok(event); + } else if let YamlTokenData::Scalar { value, style } = &mut token.data { let mut plain_implicit = false; let mut quoted_implicit = false; - end_mark = (*token).end_mark; + end_mark = token.end_mark; if *style == YAML_PLAIN_SCALAR_STYLE && tag.is_none() || tag.as_deref() == Some("!") { plain_implicit = true; } else if tag.is_none() { quoted_implicit = true; } - parser.state = parser.states.pop().unwrap(); - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::Scalar { anchor, tag, @@ -468,12 +431,13 @@ unsafe fn yaml_parser_parse_node( start_mark, end_mark, }; + parser.state = parser.states.pop().unwrap(); SKIP_TOKEN(parser); - return Ok(()); - } else if let YamlTokenData::FlowSequenceStart = &(*token).data { - end_mark = (*token).end_mark; + return Ok(event); + } else if let YamlTokenData::FlowSequenceStart = &token.data { + end_mark = token.end_mark; parser.state = YAML_PARSE_FLOW_SEQUENCE_FIRST_ENTRY_STATE; - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::SequenceStart { anchor, tag, @@ -483,11 +447,11 @@ unsafe fn yaml_parser_parse_node( start_mark, end_mark, }; - return Ok(()); - } else if let YamlTokenData::FlowMappingStart = &(*token).data { - end_mark = (*token).end_mark; + return Ok(event); + } else if let YamlTokenData::FlowMappingStart = &token.data { + end_mark = token.end_mark; parser.state = YAML_PARSE_FLOW_MAPPING_FIRST_KEY_STATE; - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::MappingStart { anchor, tag, @@ -497,11 +461,11 @@ unsafe fn yaml_parser_parse_node( start_mark, end_mark, }; - return Ok(()); - } else if block && (*token).data.is_block_sequence_start() { - end_mark = (*token).end_mark; + return Ok(event); + } else if block && token.data.is_block_sequence_start() { + end_mark = token.end_mark; parser.state = YAML_PARSE_BLOCK_SEQUENCE_FIRST_ENTRY_STATE; - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::SequenceStart { anchor, tag, @@ -511,11 +475,11 @@ unsafe fn yaml_parser_parse_node( start_mark, end_mark, }; - return Ok(()); - } else if block && (*token).data.is_block_mapping_start() { - end_mark = (*token).end_mark; + return Ok(event); + } else if block && token.data.is_block_mapping_start() { + end_mark = token.end_mark; parser.state = YAML_PARSE_BLOCK_MAPPING_FIRST_KEY_STATE; - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::MappingStart { anchor, tag, @@ -525,10 +489,10 @@ unsafe fn yaml_parser_parse_node( start_mark, end_mark, }; - return Ok(()); + return Ok(event); } else if anchor.is_some() || tag.is_some() { parser.state = parser.states.pop().unwrap(); - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::Scalar { anchor, tag, @@ -540,10 +504,9 @@ unsafe fn yaml_parser_parse_node( start_mark, end_mark, }; - return Ok(()); + return Ok(event); } else { - yaml_parser_set_parser_error_context( - parser, + return yaml_parser_set_parser_error_context( if block { "while parsing a block node" } else { @@ -551,421 +514,349 @@ unsafe fn yaml_parser_parse_node( }, start_mark, "did not find expected node content", - (*token).start_mark, + token.start_mark, ); - Err(()) } } unsafe fn yaml_parser_parse_block_sequence_entry( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, first: bool, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; +) -> Result { if first { - token = PEEK_TOKEN(parser); - parser.marks.push((*token).start_mark); + let token = PEEK_TOKEN(parser)?; + let mark = token.start_mark; + parser.marks.push(mark); SKIP_TOKEN(parser); } - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if let YamlTokenData::BlockEntry = &(*token).data { - let mark: yaml_mark_t = (*token).end_mark; + + let mut token = PEEK_TOKEN(parser)?; + + if let YamlTokenData::BlockEntry = &token.data { + let mark: yaml_mark_t = token.end_mark; SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_block_entry() && !(*token).data.is_block_end() { + token = PEEK_TOKEN(parser)?; + if !token.data.is_block_entry() && !token.data.is_block_end() { parser.states.push(YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE); - yaml_parser_parse_node(parser, event, true, false) + yaml_parser_parse_node(parser, true, false) } else { parser.state = YAML_PARSE_BLOCK_SEQUENCE_ENTRY_STATE; - yaml_parser_process_empty_scalar(event, mark) + yaml_parser_process_empty_scalar(mark) } - } else if (*token).data.is_block_end() { + } else if token.data.is_block_end() { + let event = yaml_event_t { + data: YamlEventData::SequenceEnd, + start_mark: token.start_mark, + end_mark: token.end_mark, + }; parser.state = parser.states.pop().unwrap(); let _ = parser.marks.pop(); - *event = yaml_event_t { - data: YamlEventData::SequenceEnd, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, - }; SKIP_TOKEN(parser); - Ok(()) + Ok(event) } else { + let token_mark = token.start_mark; let mark = parser.marks.pop().unwrap(); - yaml_parser_set_parser_error_context( - parser, + return yaml_parser_set_parser_error_context( "while parsing a block collection", mark, "did not find expected '-' indicator", - (*token).start_mark, + token_mark, ); - Err(()) } } unsafe fn yaml_parser_parse_indentless_sequence_entry( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if (*token).data.is_block_entry() { - let mark: yaml_mark_t = (*token).end_mark; +) -> Result { + let mut token = PEEK_TOKEN(parser)?; + if token.data.is_block_entry() { + let mark: yaml_mark_t = token.end_mark; SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_block_entry() - && !(*token).data.is_key() - && !(*token).data.is_value() - && !(*token).data.is_block_end() + token = PEEK_TOKEN(parser)?; + if !token.data.is_block_entry() + && !token.data.is_key() + && !token.data.is_value() + && !token.data.is_block_end() { parser .states .push(YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE); - yaml_parser_parse_node(parser, event, true, false) + yaml_parser_parse_node(parser, true, false) } else { parser.state = YAML_PARSE_INDENTLESS_SEQUENCE_ENTRY_STATE; - yaml_parser_process_empty_scalar(event, mark) + yaml_parser_process_empty_scalar(mark) } } else { - parser.state = parser.states.pop().unwrap(); - *event = yaml_event_t { + let event = yaml_event_t { data: YamlEventData::SequenceEnd, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, + start_mark: token.start_mark, + end_mark: token.end_mark, }; - Ok(()) + parser.state = parser.states.pop().unwrap(); + Ok(event) } } unsafe fn yaml_parser_parse_block_mapping_key( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, first: bool, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; +) -> Result { if first { - token = PEEK_TOKEN(parser); - parser.marks.push((*token).start_mark); + let token = PEEK_TOKEN(parser)?; + let mark = token.start_mark; + parser.marks.push(mark); SKIP_TOKEN(parser); } - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if (*token).data.is_key() { - let mark: yaml_mark_t = (*token).end_mark; + + let mut token = PEEK_TOKEN(parser)?; + if token.data.is_key() { + let mark: yaml_mark_t = token.end_mark; SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_key() && !(*token).data.is_value() && !(*token).data.is_block_end() { + token = PEEK_TOKEN(parser)?; + if !token.data.is_key() && !token.data.is_value() && !token.data.is_block_end() { parser.states.push(YAML_PARSE_BLOCK_MAPPING_VALUE_STATE); - yaml_parser_parse_node(parser, event, true, true) + yaml_parser_parse_node(parser, true, true) } else { parser.state = YAML_PARSE_BLOCK_MAPPING_VALUE_STATE; - yaml_parser_process_empty_scalar(event, mark) + yaml_parser_process_empty_scalar(mark) } - } else if (*token).data.is_block_end() { + } else if token.data.is_block_end() { + let event = yaml_event_t { + data: YamlEventData::MappingEnd, + start_mark: token.start_mark, + end_mark: token.end_mark, + }; parser.state = parser.states.pop().unwrap(); _ = parser.marks.pop(); - *event = yaml_event_t { - data: YamlEventData::MappingEnd, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, - }; SKIP_TOKEN(parser); - Ok(()) + Ok(event) } else { + let token_mark = token.start_mark; let mark = parser.marks.pop().unwrap(); yaml_parser_set_parser_error_context( - parser, "while parsing a block mapping", mark, "did not find expected key", - (*token).start_mark, - ); - Err(()) + token_mark, + ) } } unsafe fn yaml_parser_parse_block_mapping_value( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if (*token).data.is_value() { - let mark: yaml_mark_t = (*token).end_mark; +) -> Result { + let mut token = PEEK_TOKEN(parser)?; + if token.data.is_value() { + let mark: yaml_mark_t = token.end_mark; SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_key() && !(*token).data.is_value() && !(*token).data.is_block_end() { + token = PEEK_TOKEN(parser)?; + if !token.data.is_key() && !token.data.is_value() && !token.data.is_block_end() { parser.states.push(YAML_PARSE_BLOCK_MAPPING_KEY_STATE); - yaml_parser_parse_node(parser, event, true, true) + yaml_parser_parse_node(parser, true, true) } else { parser.state = YAML_PARSE_BLOCK_MAPPING_KEY_STATE; - yaml_parser_process_empty_scalar(event, mark) + yaml_parser_process_empty_scalar(mark) } } else { + let mark = token.start_mark; parser.state = YAML_PARSE_BLOCK_MAPPING_KEY_STATE; - yaml_parser_process_empty_scalar(event, (*token).start_mark) + yaml_parser_process_empty_scalar(mark) } } unsafe fn yaml_parser_parse_flow_sequence_entry( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, first: bool, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; +) -> Result { if first { - token = PEEK_TOKEN(parser); - parser.marks.push((*token).start_mark); + let token = PEEK_TOKEN(parser)?; + let mark = token.start_mark; + parser.marks.push(mark); SKIP_TOKEN(parser); } - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_flow_sequence_end() { + + let mut token = PEEK_TOKEN(parser)?; + if !token.data.is_flow_sequence_end() { if !first { - if (*token).data.is_flow_entry() { + if token.data.is_flow_entry() { SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } + token = PEEK_TOKEN(parser)?; } else { + let token_mark = token.start_mark; let mark = parser.marks.pop().unwrap(); - yaml_parser_set_parser_error_context( - parser, + return yaml_parser_set_parser_error_context( "while parsing a flow sequence", mark, "did not find expected ',' or ']'", - (*token).start_mark, + token_mark, ); - return Err(()); } } - if (*token).data.is_key() { - parser.state = YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE; - *event = yaml_event_t::default(); - *event = yaml_event_t { + if token.data.is_key() { + let event = yaml_event_t { data: YamlEventData::MappingStart { anchor: None, tag: None, implicit: true, style: YAML_FLOW_MAPPING_STYLE, }, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, + start_mark: token.start_mark, + end_mark: token.end_mark, }; + parser.state = YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_KEY_STATE; SKIP_TOKEN(parser); - return Ok(()); - } else if !(*token).data.is_flow_sequence_end() { + return Ok(event); + } else if !token.data.is_flow_sequence_end() { parser.states.push(YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE); - return yaml_parser_parse_node(parser, event, false, false); + return yaml_parser_parse_node(parser, false, false); } } + let event = yaml_event_t { + data: YamlEventData::SequenceEnd, + start_mark: token.start_mark, + end_mark: token.end_mark, + }; parser.state = parser.states.pop().unwrap(); _ = parser.marks.pop(); - *event = yaml_event_t { - data: YamlEventData::SequenceEnd, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, - }; SKIP_TOKEN(parser); - Ok(()) + Ok(event) } unsafe fn yaml_parser_parse_flow_sequence_entry_mapping_key( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { - let token: *mut yaml_token_t = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_value() - && !(*token).data.is_flow_entry() - && !(*token).data.is_flow_sequence_end() - { +) -> Result { + let token = PEEK_TOKEN(parser)?; + if !token.data.is_value() && !token.data.is_flow_entry() && !token.data.is_flow_sequence_end() { parser .states .push(YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE); - yaml_parser_parse_node(parser, event, false, false) + yaml_parser_parse_node(parser, false, false) } else { - let mark: yaml_mark_t = (*token).end_mark; + let mark: yaml_mark_t = token.end_mark; SKIP_TOKEN(parser); parser.state = YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_VALUE_STATE; - yaml_parser_process_empty_scalar(event, mark) + yaml_parser_process_empty_scalar(mark) } } unsafe fn yaml_parser_parse_flow_sequence_entry_mapping_value( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if (*token).data.is_value() { +) -> Result { + let mut token = PEEK_TOKEN(parser)?; + if token.data.is_value() { SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_flow_entry() && !(*token).data.is_flow_sequence_end() { + token = PEEK_TOKEN(parser)?; + if !token.data.is_flow_entry() && !token.data.is_flow_sequence_end() { parser .states .push(YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE); - return yaml_parser_parse_node(parser, event, false, false); + return yaml_parser_parse_node(parser, false, false); } } + let mark = token.start_mark; parser.state = YAML_PARSE_FLOW_SEQUENCE_ENTRY_MAPPING_END_STATE; - yaml_parser_process_empty_scalar(event, (*token).start_mark) + yaml_parser_process_empty_scalar(mark) } unsafe fn yaml_parser_parse_flow_sequence_entry_mapping_end( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, -) -> Result<(), ()> { - let token: *mut yaml_token_t = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } +) -> Result { + let token = PEEK_TOKEN(parser)?; + let start_mark = token.start_mark; + let end_mark = token.end_mark; parser.state = YAML_PARSE_FLOW_SEQUENCE_ENTRY_STATE; - *event = yaml_event_t { + Ok(yaml_event_t { data: YamlEventData::MappingEnd, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, - }; - Ok(()) + start_mark, + end_mark, + }) } unsafe fn yaml_parser_parse_flow_mapping_key( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, first: bool, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; +) -> Result { if first { - token = PEEK_TOKEN(parser); - parser.marks.push((*token).start_mark); + let token = PEEK_TOKEN(parser)?; + let mark = token.start_mark; + parser.marks.push(mark); SKIP_TOKEN(parser); } - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_flow_mapping_end() { + + let mut token = PEEK_TOKEN(parser)?; + if !token.data.is_flow_mapping_end() { if !first { - if (*token).data.is_flow_entry() { + if token.data.is_flow_entry() { SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } + token = PEEK_TOKEN(parser)?; } else { + let token_mark = token.start_mark; let mark = parser.marks.pop().unwrap(); - yaml_parser_set_parser_error_context( - parser, + return yaml_parser_set_parser_error_context( "while parsing a flow mapping", mark, "did not find expected ',' or '}'", - (*token).start_mark, + token_mark, ); - return Err(()); } } - if (*token).data.is_key() { + if token.data.is_key() { SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_value() - && !(*token).data.is_flow_entry() - && !(*token).data.is_flow_mapping_end() + token = PEEK_TOKEN(parser)?; + if !token.data.is_value() + && !token.data.is_flow_entry() + && !token.data.is_flow_mapping_end() { parser.states.push(YAML_PARSE_FLOW_MAPPING_VALUE_STATE); - return yaml_parser_parse_node(parser, event, false, false); + return yaml_parser_parse_node(parser, false, false); } else { + let mark = token.start_mark; parser.state = YAML_PARSE_FLOW_MAPPING_VALUE_STATE; - return yaml_parser_process_empty_scalar(event, (*token).start_mark); + return yaml_parser_process_empty_scalar(mark); } - } else if !(*token).data.is_flow_mapping_end() { + } else if !token.data.is_flow_mapping_end() { parser .states .push(YAML_PARSE_FLOW_MAPPING_EMPTY_VALUE_STATE); - return yaml_parser_parse_node(parser, event, false, false); + return yaml_parser_parse_node(parser, false, false); } } + let event = yaml_event_t { + data: YamlEventData::MappingEnd, + start_mark: token.start_mark, + end_mark: token.end_mark, + }; parser.state = parser.states.pop().unwrap(); _ = parser.marks.pop(); - *event = yaml_event_t { - data: YamlEventData::MappingEnd, - start_mark: (*token).start_mark, - end_mark: (*token).end_mark, - }; SKIP_TOKEN(parser); - Ok(()) + Ok(event) } unsafe fn yaml_parser_parse_flow_mapping_value( parser: &mut yaml_parser_t, - event: &mut yaml_event_t, empty: bool, -) -> Result<(), ()> { - let mut token: *mut yaml_token_t; - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } +) -> Result { + let mut token = PEEK_TOKEN(parser)?; if empty { + let mark = token.start_mark; parser.state = YAML_PARSE_FLOW_MAPPING_KEY_STATE; - return yaml_parser_process_empty_scalar(event, (*token).start_mark); + return yaml_parser_process_empty_scalar(mark); } - if (*token).data.is_value() { + if token.data.is_value() { SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } - if !(*token).data.is_flow_entry() && !(*token).data.is_flow_mapping_end() { + token = PEEK_TOKEN(parser)?; + if !token.data.is_flow_entry() && !token.data.is_flow_mapping_end() { parser.states.push(YAML_PARSE_FLOW_MAPPING_KEY_STATE); - return yaml_parser_parse_node(parser, event, false, false); + return yaml_parser_parse_node(parser, false, false); } } + let mark = token.start_mark; parser.state = YAML_PARSE_FLOW_MAPPING_KEY_STATE; - yaml_parser_process_empty_scalar(event, (*token).start_mark) + yaml_parser_process_empty_scalar(mark) } -unsafe fn yaml_parser_process_empty_scalar( - event: &mut yaml_event_t, - mark: yaml_mark_t, -) -> Result<(), ()> { - *event = yaml_event_t { +unsafe fn yaml_parser_process_empty_scalar(mark: yaml_mark_t) -> Result { + Ok(yaml_event_t { data: YamlEventData::Scalar { anchor: None, tag: None, @@ -976,15 +867,14 @@ unsafe fn yaml_parser_process_empty_scalar( }, start_mark: mark, end_mark: mark, - }; - Ok(()) + }) } unsafe fn yaml_parser_process_directives( parser: &mut yaml_parser_t, version_directive_ref: Option<&mut Option>, tag_directives_ref: Option<&mut Vec>, -) -> Result<(), ()> { +) -> Result<(), ParserError> { let default_tag_directives: [yaml_tag_directive_t; 2] = [ // TODO: Get rid of these heap allocations. yaml_tag_directive_t { @@ -1000,58 +890,44 @@ unsafe fn yaml_parser_process_directives( let mut tag_directives = Vec::with_capacity(16); - let mut token: *mut yaml_token_t; - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } + let mut token = PEEK_TOKEN(parser)?; loop { - if !((*token).data.is_version_directive() || (*token).data.is_tag_directive()) { + if !(token.data.is_version_directive() || token.data.is_tag_directive()) { break; } - if let YamlTokenData::VersionDirective { major, minor } = &(*token).data { + if let YamlTokenData::VersionDirective { major, minor } = &token.data { + let mark = token.start_mark; if version_directive.is_some() { - yaml_parser_set_parser_error( - parser, - "found duplicate %YAML directive", - (*token).start_mark, - ); - return Err(())?; + return yaml_parser_set_parser_error("found duplicate %YAML directive", mark); } else if *major != 1 || *minor != 1 && *minor != 2 { - yaml_parser_set_parser_error( - parser, - "found incompatible YAML document", - (*token).start_mark, - ); - return Err(()); + return yaml_parser_set_parser_error("found incompatible YAML document", mark); } else { version_directive = Some(yaml_version_directive_t { major: *major, minor: *minor, }); } - } else if let YamlTokenData::TagDirective { handle, prefix } = &(*token).data { + } else if let YamlTokenData::TagDirective { handle, prefix } = &token.data { let value = yaml_tag_directive_t { // TODO: Get rid of these clones by consuming tokens by value. handle: handle.clone(), prefix: prefix.clone(), }; - yaml_parser_append_tag_directive(parser, &value, false, (*token).start_mark)?; + let mark = token.start_mark; + yaml_parser_append_tag_directive(parser, &value, false, mark)?; tag_directives.push(value); } SKIP_TOKEN(parser); - token = PEEK_TOKEN(parser); - if token.is_null() { - return Err(()); - } + token = PEEK_TOKEN(parser)?; } + let start_mark = token.start_mark; for default_tag_directive in &default_tag_directives { - yaml_parser_append_tag_directive(parser, default_tag_directive, true, (*token).start_mark)? + yaml_parser_append_tag_directive(parser, default_tag_directive, true, start_mark)? } if let Some(version_directive_ref) = version_directive_ref { @@ -1076,14 +952,13 @@ unsafe fn yaml_parser_append_tag_directive( value: &yaml_tag_directive_t, allow_duplicates: bool, mark: yaml_mark_t, -) -> Result<(), ()> { +) -> Result<(), ParserError> { for tag_directive in parser.tag_directives.iter() { if value.handle == tag_directive.handle { if allow_duplicates { return Ok(()); } - yaml_parser_set_parser_error(parser, "found duplicate %TAG directive", mark); - return Err(()); + return yaml_parser_set_parser_error("found duplicate %TAG directive", mark); } } parser.tag_directives.push(value.clone()); diff --git a/src/scanner.rs b/src/scanner.rs index 67e3f87..c596001 100644 --- a/src/scanner.rs +++ b/src/scanner.rs @@ -7,7 +7,7 @@ use crate::yaml::{ptrdiff_t, size_t, YamlTokenData}; use crate::{ libc, yaml_mark_t, yaml_parser_t, yaml_simple_key_t, yaml_token_t, ReaderError, ScannerError, YAML_DOUBLE_QUOTED_SCALAR_STYLE, YAML_FOLDED_SCALAR_STYLE, YAML_LITERAL_SCALAR_STYLE, - YAML_NO_ERROR, YAML_PLAIN_SCALAR_STYLE, YAML_SINGLE_QUOTED_SCALAR_STYLE, + YAML_PLAIN_SCALAR_STYLE, YAML_SINGLE_QUOTED_SCALAR_STYLE, }; fn CACHE(parser: &mut yaml_parser_t, length: size_t) -> Result<(), ReaderError> { @@ -99,28 +99,25 @@ fn READ_LINE_STRING(parser: &mut yaml_parser_t, string: &mut String) { /// An application must not alternate the calls of yaml_parser_scan() with the /// calls of yaml_parser_parse() or yaml_parser_load(). Doing this will break /// the parser. -pub fn yaml_parser_scan( - parser: &mut yaml_parser_t, - token: &mut yaml_token_t, -) -> Result<(), ScannerError> { - *token = yaml_token_t::default(); - if parser.stream_end_produced || parser.error != YAML_NO_ERROR { - return Ok(()); +pub fn yaml_parser_scan(parser: &mut yaml_parser_t) -> Result { + if parser.stream_end_produced { + return Ok(yaml_token_t { + data: YamlTokenData::StreamEnd, + ..Default::default() + }); } if !parser.token_available { yaml_parser_fetch_more_tokens(parser)?; } - if let Some(popped) = parser.tokens.pop_front() { - *token = popped; + if let Some(token) = parser.tokens.pop_front() { parser.token_available = false; parser.tokens_parsed = parser.tokens_parsed.force_add(1); - if let YamlTokenData::StreamEnd = &(*token).data { + if let YamlTokenData::StreamEnd = &token.data { parser.stream_end_produced = true; } - Ok(()) + Ok(token) } else { - // token_available should have been false - unreachable!() + unreachable!("no more tokens, but stream-end was not produced") } } diff --git a/src/yaml.rs b/src/yaml.rs index e6f292a..e02eae2 100644 --- a/src/yaml.rs +++ b/src/yaml.rs @@ -670,20 +670,6 @@ pub struct yaml_alias_data_t { #[repr(C)] #[non_exhaustive] pub struct yaml_parser_t<'r> { - /// Error type. - pub error: yaml_error_type_t, - /// Error description. - pub problem: Option<&'static str>, - /// The byte about which the problem occured. - pub problem_offset: size_t, - /// The problematic value (-1 is none). - pub problem_value: libc::c_int, - /// The problem position. - pub problem_mark: yaml_mark_t, - /// The error context. - pub context: Option<&'static str>, - /// The context position. - pub context_mark: yaml_mark_t, /// Read handler. pub(crate) read_handler: Option<&'r mut dyn std::io::Read>, /// Standard (string or file) input data. @@ -743,13 +729,6 @@ pub struct yaml_parser_t<'r> { impl<'r> Default for yaml_parser_t<'r> { fn default() -> Self { Self { - error: Default::default(), - problem: Default::default(), - problem_offset: Default::default(), - problem_value: Default::default(), - problem_mark: Default::default(), - context: Default::default(), - context_mark: Default::default(), read_handler: None, input: Default::default(), eof: Default::default(),