Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AST parent pointer #134

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
372 changes: 335 additions & 37 deletions src/ast.rs

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1716,11 +1716,11 @@ impl<'a> Parser<'a> {
),
}),
#[cfg(feature = "ast-span")]
_ => Ok(Type2::Any((
_ => Ok(Type2::Any { span: (
begin_type2_range,
self.lexer_position.range.1,
begin_type2_line,
))),
)}),
#[cfg(not(feature = "ast-span"))]
_ => Ok(Type2::Any),
}
Expand Down Expand Up @@ -3160,11 +3160,11 @@ impl<'a> Parser<'a> {

Ok(Some(Occurrence {
#[cfg(feature = "ast-span")]
occur: Occur::Optional((
occur: Occur::Optional { span: (
self.parser_position.range.0,
self.parser_position.range.1,
self.parser_position.line,
)),
)},
#[cfg(not(feature = "ast-span"))]
occur: Occur::Optional,
#[cfg(feature = "ast-comments")]
Expand All @@ -3187,11 +3187,11 @@ impl<'a> Parser<'a> {

Ok(Some(Occurrence {
#[cfg(feature = "ast-span")]
occur: Occur::OneOrMore((
occur: Occur::OneOrMore { span: (
self.parser_position.range.0,
self.parser_position.range.1,
self.parser_position.line,
)),
)},
#[cfg(not(feature = "ast-span"))]
occur: Occur::OneOrMore,
#[cfg(feature = "ast-comments")]
Expand Down Expand Up @@ -3221,11 +3221,11 @@ impl<'a> Parser<'a> {
#[cfg(feature = "ast-span")]
{
self.parser_position.range = self.lexer_position.range;
Occur::ZeroOrMore((
Occur::ZeroOrMore { span: (
self.parser_position.range.0,
self.parser_position.range.1,
self.parser_position.line,
))
)}
}

#[cfg(not(feature = "ast-span"))]
Expand Down
22 changes: 11 additions & 11 deletions src/parser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ mod tests {
value: 9.9,
span: (0, 3, 1),
},
Type2::Any((0, 1, 1)),
Type2::Any { span: (0, 1, 1) },
Type2::Array {
group: Group {
group_choices: vec![GroupChoice {
Expand Down Expand Up @@ -720,7 +720,7 @@ mod tests {
GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur: Some(Occurrence {
occur: Occur::OneOrMore((1, 2, 1)),
occur: Occur::OneOrMore { span: (1, 2, 1) },
comments: None,
_a: PhantomData::default(),
}),
Expand Down Expand Up @@ -801,7 +801,7 @@ mod tests {
GroupEntry::ValueMemberKey {
ge: Box::from(ValueMemberKeyEntry {
occur: Some(Occurrence {
occur: Occur::Optional((2, 3, 1)),
occur: Occur::Optional { span: (2, 3, 1) },
comments: None,
_a: PhantomData::default(),
}),
Expand Down Expand Up @@ -1029,7 +1029,7 @@ mod tests {
GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur: Some(Occurrence {
occur: Occur::ZeroOrMore((3, 4, 1)),
occur: Occur::ZeroOrMore { span: (3, 4, 1) },
comments: None,
_a: PhantomData::default(),
}),
Expand Down Expand Up @@ -1094,7 +1094,7 @@ mod tests {
GroupEntry::TypeGroupname {
ge: TypeGroupnameEntry {
occur: Some(Occurrence {
occur: Occur::ZeroOrMore((19, 20, 1)),
occur: Occur::ZeroOrMore { span: (19, 20, 1) },
comments: None,
_a: PhantomData::default(),
}),
Expand Down Expand Up @@ -1386,7 +1386,7 @@ mod tests {
GroupEntry::ValueMemberKey {
ge: Box::from(ValueMemberKeyEntry {
occur: Some(Occurrence {
occur: Occur::ZeroOrMore((0, 1, 1)),
occur: Occur::ZeroOrMore { span: (0, 1, 1) },
comments: None,
_a: PhantomData::default(),
}),
Expand Down Expand Up @@ -1488,7 +1488,7 @@ mod tests {
GroupEntry::ValueMemberKey {
ge: Box::from(ValueMemberKeyEntry {
occur: Some(Occurrence {
occur: Occur::Optional((0, 1, 1)),
occur: Occur::Optional { span: (0, 1, 1) },
comments: None,
_a: PhantomData::default(),
}),
Expand Down Expand Up @@ -1583,7 +1583,7 @@ mod tests {
GroupEntry::ValueMemberKey {
ge: Box::from(ValueMemberKeyEntry {
occur: Some(Occurrence {
occur: Occur::ZeroOrMore((0, 1, 1)),
occur: Occur::ZeroOrMore { span: (0, 1, 1) },
comments: None,
_a: PhantomData::default(),
}),
Expand Down Expand Up @@ -1819,12 +1819,12 @@ mod tests {
_a: PhantomData::default(),
},
Occurrence {
occur: Occur::ZeroOrMore((0, 1, 1)),
occur: Occur::ZeroOrMore { span: (0, 1, 1) },
comments: None,
_a: PhantomData::default(),
},
Occurrence {
occur: Occur::OneOrMore((0, 1, 1)),
occur: Occur::OneOrMore { span: (0, 1, 1) },
comments: None,
_a: PhantomData::default(),
},
Expand All @@ -1847,7 +1847,7 @@ mod tests {
_a: PhantomData::default(),
},
Occurrence {
occur: Occur::Optional((0, 1, 1)),
occur: Occur::Optional { span: (0, 1, 1) },
comments: None,
_a: PhantomData::default(),
},
Expand Down
12 changes: 6 additions & 6 deletions src/validator/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<T: std::fmt::Debug> fmt::Display for Error<T> {
Error::CBORParsing(error) => write!(f, "error parsing cbor: {}", error),
Error::JSONParsing(error) => write!(f, "error parsing json string: {}", error),
Error::CDDLParsing(error) => write!(f, "error parsing CDDL: {}", error),
Error::UTF8Parsing(error) => write!(f, "error pasing utf8: {}", error),
Error::UTF8Parsing(error) => write!(f, "error parsing utf8: {}", error),
}
}
}
Expand Down Expand Up @@ -1178,7 +1178,7 @@ where
self.visit_type2(target)?;
if self.errors.len() != error_count {
#[cfg(feature = "ast-span")]
if let Some(Occur::Optional(_)) = self.occurrence.take() {
if let Some(Occur::Optional { .. }) = self.occurrence.take() {
self.add_error(format!(
"expected default value {}, got {:?}",
controller, self.cbor
Expand Down Expand Up @@ -2164,7 +2164,7 @@ where
}
},
#[cfg(feature = "ast-span")]
Type2::Any(_) => Ok(()),
Type2::Any { .. } => Ok(()),
#[cfg(not(feature = "ast-span"))]
Type2::Any => Ok(()),
_ => {
Expand Down Expand Up @@ -2294,8 +2294,8 @@ where
Value::Map(m) => {
if let Some(occur) = &self.occurrence {
#[cfg(feature = "ast-span")]
if let Occur::ZeroOrMore(_) | Occur::OneOrMore(_) = occur {
if let Occur::OneOrMore(_) = occur {
if let Occur::ZeroOrMore { .. } | Occur::OneOrMore { .. } = occur {
if let Occur::OneOrMore { .. } = occur {
if m.is_empty() {
self.add_error(format!(
"map cannot be empty, one or more entries with key type {} required",
Expand Down Expand Up @@ -3326,7 +3326,7 @@ where
self.cbor_location.push_str(&format!("/{}", value));

None
} else if let Some(Occur::Optional(_)) | Some(Occur::ZeroOrMore(_)) =
} else if let Some(Occur::Optional { .. }) | Some(Occur::ZeroOrMore { .. }) =
&self.occurrence.take()
{
self.advance_to_next_entry = true;
Expand Down
2 changes: 1 addition & 1 deletion src/validator/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ fn dedent_bytes(source: &[u8], is_utf8_byte_string: bool) -> Result<Vec<u8>, Str
}

/// Numeric addition of target and controller. The Vec return type is to
/// accomodate more than one type choice in the controller
/// accommodate more than one type choice in the controller
pub fn plus_operation<'a>(
cddl: &CDDL,
target: &Type2,
Expand Down
10 changes: 5 additions & 5 deletions src/validator/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ impl<'a> JSONValidator<'a> {
self.json_location.push_str(&format!("/{}", t));

return Ok(());
} else if let Some(Occur::Optional(_)) | Some(Occur::ZeroOrMore(_)) =
} else if let Some(Occur::Optional { .. }) | Some(Occur::ZeroOrMore { .. }) =
&self.occurrence.take()
{
self.advance_to_next_entry = true;
Expand Down Expand Up @@ -1266,7 +1266,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> {
self.visit_type2(target)?;
if self.errors.len() != error_count {
#[cfg(feature = "ast-span")]
if let Some(Occur::Optional(_)) = self.occurrence.take() {
if let Some(Occur::Optional { .. }) = self.occurrence.take() {
self.add_error(format!(
"expected default value {}, got {}",
controller, self.json
Expand Down Expand Up @@ -1775,7 +1775,7 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> {
Ok(())
}
#[cfg(feature = "ast-span")]
Type2::Any(_) => Ok(()),
Type2::Any { .. } => Ok(()),
#[cfg(not(feature = "ast-span"))]
Type2::Any => Ok(()),
_ => {
Expand Down Expand Up @@ -1897,8 +1897,8 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> {
Value::Object(o) => {
if let Some(occur) = &self.occurrence {
#[cfg(feature = "ast-span")]
if let Occur::ZeroOrMore(_) | Occur::OneOrMore(_) = occur {
if let Occur::OneOrMore(_) = occur {
if let Occur::ZeroOrMore { .. } | Occur::OneOrMore { .. } = occur {
if let Occur::OneOrMore { .. } = occur {
if o.is_empty() {
self.add_error(format!(
"object cannot be empty, one or more entries with key type {} required",
Expand Down
15 changes: 9 additions & 6 deletions src/validator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ pub mod cbor;
/// JSON validation implementation
pub mod json;

/// parent visitor implementation
mod parent_visitor;

mod control;

use crate::{
Expand Down Expand Up @@ -831,19 +834,19 @@ pub fn validate_array_occurrence<'de, T: Deserialize<'de>>(
) -> std::result::Result<(bool, bool), Vec<String>> {
let mut iter_items = false;
#[cfg(feature = "ast-span")]
let allow_empty_array = matches!(occurrence, Some(Occur::Optional(_)));
let allow_empty_array = matches!(occurrence, Some(Occur::Optional { .. }));
#[cfg(not(feature = "ast-span"))]
let allow_empty_array = matches!(occurrence, Some(Occur::Optional));

let mut errors = Vec::new();

match occurrence {
#[cfg(feature = "ast-span")]
Some(Occur::ZeroOrMore(_)) => iter_items = true,
Some(Occur::ZeroOrMore { .. }) => iter_items = true,
#[cfg(not(feature = "ast-span"))]
Some(Occur::ZeroOrMore) => iter_items = true,
#[cfg(feature = "ast-span")]
Some(Occur::OneOrMore(_)) => {
Some(Occur::OneOrMore { .. }) => {
if values.is_empty() {
errors.push("array must have at least one item".to_string());
} else {
Expand Down Expand Up @@ -882,7 +885,7 @@ pub fn validate_array_occurrence<'de, T: Deserialize<'de>>(
iter_items = true;
}
#[cfg(feature = "ast-span")]
Some(Occur::Optional(_)) => {
Some(Occur::Optional { .. }) => {
if values.len() > 1 {
errors.push("array must have 0 or 1 items".to_string());
}
Expand Down Expand Up @@ -1014,11 +1017,11 @@ pub fn validate_entry_count(valid_entry_counts: &[EntryCount], num_entries: usiz
num_entries == ec.count as usize
|| match ec.entry_occurrence {
#[cfg(feature = "ast-span")]
Some(Occur::ZeroOrMore(_)) | Some(Occur::Optional(_)) => true,
Some(Occur::ZeroOrMore { .. }) | Some(Occur::Optional { .. }) => true,
#[cfg(not(feature = "ast-span"))]
Some(Occur::ZeroOrMore) | Some(Occur::Optional) => true,
#[cfg(feature = "ast-span")]
Some(Occur::OneOrMore(_)) if num_entries > 0 => true,
Some(Occur::OneOrMore { .. } ) if num_entries > 0 => true,
#[cfg(not(feature = "ast-span"))]
Some(Occur::OneOrMore) if num_entries > 0 => true,
Some(Occur::Exact { lower, upper, .. }) => {
Expand Down
Loading