Skip to content

Commit

Permalink
fix(oca-file): parsing attribute types
Browse files Browse the repository at this point in the history
- Improve logic and naming in pest
- Improve naming of the functions
- Fix support for refn and refs
  • Loading branch information
mitfik committed Jan 2, 2024
1 parent a700d5f commit 063b8e2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 28 deletions.
11 changes: 5 additions & 6 deletions oca-file/src/ocafile.pest
Original file line number Diff line number Diff line change
Expand Up @@ -212,16 +212,15 @@ base_attr_type = @{ ("Text" |
object_attr_type = @{( "Object("~ arg_ws? ~ "{" ~ arg_ws? ~ (attr_key ~ "=" ~ _attr_type ~ ("," ~ arg_ws_maybe)?)+ ~ arg_ws_maybe ~ "})")}

// TODO find out if we could have just array and then types
array_attr_type = ${( "Array["~ (base_attr_type | object_attr_type) ~"]" )}
ref_array = ${ "Array[" ~ reference_type ~ "]"}
reference_type = _{ ref_said | ref_name }
reference = @{ char+ }
array_attr_type = ${( "Array["~ (base_attr_type | object_attr_type | reference_type ) ~"]" )}
reference_type = _{ ref_said | ref_alias }
alias = @{ char+ }
said = @{ char+ }
refs = _{^"refs:"}
refn = _{^"refn:"}
ref_said = _{ refs ~ said}
ref_name = _{ refn ~ reference }
_attr_type = ${ base_attr_type | array_attr_type | ref_said | ref_name | ref_array | object_attr_type }
ref_alias = _{ refn ~ alias }
_attr_type = ${ base_attr_type | array_attr_type | ref_said | ref_alias | object_attr_type }
attr_pair = @{attr_key ~ "=" ~ _attr_type}
attr_pairs = ${ (arg_ws ~ attr_pair)+}

Expand Down
2 changes: 1 addition & 1 deletion oca-file/src/ocafile/instructions/add.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl AddInstruction {
for attr in attr_pairs.into_inner() {
debug!("Parsing attribute pair {:?}", attr);
if let Some((key, value)) =
helpers::extract_attribute_type(attr)
helpers::extract_attribute(attr)
{
info!("Parsed attribute: {:?} = {:?}", key, value);

Expand Down
48 changes: 27 additions & 21 deletions oca-file/src/ocafile/instructions/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,27 @@ use recursion::ExpandableExt;
use said::SelfAddressingIdentifier;
use crate::ocafile::{Pair, Rule};

fn extract(input: Pair) -> NestedAttrType {
fn extract_attr_type(input: Pair) -> NestedAttrType {
NestedAttrType::expand_frames(input, |seed| {
match seed.as_rule() {
Rule::array_attr_type => {
NestedAttrTypeFrame::Array(seed.into_inner().next().unwrap())
},
Rule::alias => {
NestedAttrTypeFrame::Reference(oca_ast::ast::RefValue::Name(seed.as_str().to_string()))
},
Rule::said => {
let said = SelfAddressingIdentifier::from_str(seed.as_str()).unwrap();
NestedAttrTypeFrame::Reference(RefValue::Said(said))
},

Rule::base_attr_type => {
let attr_type = AttributeType::from_str(seed.as_span().as_str()).unwrap();
NestedAttrTypeFrame::Value(attr_type)
},
Rule::object_attr_type => {
NestedAttrTypeFrame::Object(extract_object(seed))
},
Rule::_attr_type => {
let mut inner = seed.into_inner();
let inner_pair = inner.next().unwrap();
Expand All @@ -28,23 +46,6 @@ fn extract(input: Pair) -> NestedAttrType {

}
},
Rule::base_attr_type => {
let attr_type = AttributeType::from_str(seed.as_span().as_str()).unwrap();
NestedAttrTypeFrame::Value(attr_type)
},
Rule::reference => {
NestedAttrTypeFrame::Reference(oca_ast::ast::RefValue::Name(seed.as_str().to_string()))
},
Rule::said => {
let said = SelfAddressingIdentifier::from_str(seed.as_str()).unwrap();
NestedAttrTypeFrame::Reference(RefValue::Said(said))
},
Rule::object_attr_type => {
NestedAttrTypeFrame::Object(extract_object(seed))
},
Rule::array_attr_type => {
NestedAttrTypeFrame::Array(seed)
},
r => {
panic!("Matching attr type didn't work. Unhandled Rule type: {:?}", r);
}
Expand All @@ -63,7 +64,7 @@ fn extract_object(input_pair: Pair) -> IndexMap<String, Pair> {
idmap
}

pub fn extract_attribute_type(attr_pair: Pair) -> Option<(String, NestedAttrType)> {
pub fn extract_attribute(attr_pair: Pair) -> Option<(String, NestedAttrType)> {
let mut attr_name = String::new();
let mut attr_type = NestedAttrType::Value(AttributeType::Text);

Expand All @@ -74,9 +75,14 @@ pub fn extract_attribute_type(attr_pair: Pair) -> Option<(String, NestedAttrType
debug!("Extracting attribute key {:?}", attr_name);
attr_name = item.as_str().to_string();
},
_ => {
Rule::_attr_type => {
debug!("Attribute type to parse: {:?}", item);
attr_type = extract(item);
let mut inner = item.into_inner();
let inner_pair = inner.next().unwrap();
attr_type = extract_attr_type(inner_pair);
}
_ => {
panic!("Invalid attribute in {:?}", item.as_rule());
}
}
}
Expand Down

0 comments on commit 063b8e2

Please sign in to comment.