diff --git a/ast-generator/src/deserialize.rs b/ast-generator/src/deserialize.rs index 4c199cd..a4d95e7 100644 --- a/ast-generator/src/deserialize.rs +++ b/ast-generator/src/deserialize.rs @@ -70,11 +70,12 @@ fn get_single_child_to_build(name: &str, rule: &str, build: &Yaml) -> SingleChil } } -fn get_single_child(name: &str, rule: &str, build: &Yaml) -> ChildSpec { +fn get_single_child(name: &str, rule: &str, build: &Yaml, optional: bool) -> ChildSpec { ChildSpec::SingleChild(SingleChild::new( name, rule, get_single_child_to_build(name, rule, build), + optional )) } @@ -92,18 +93,25 @@ fn get_child_specs(children: &Yaml) -> Vec { .map(|(name, props)| (name.as_str().unwrap(), props)) .unwrap(); - let rule = props["rule"].as_str().unwrap(); + let rule = props["rule"] + .as_str() + .map(|s| s.to_string()) + .unwrap_or(name.to_case(Case::Pascal)); if get_skip(&props["skip"]) { - return ChildSpec::SkipChild(SkipChild::new(name, rule)); + return ChildSpec::SkipChild(SkipChild::new(name, &rule)); } let build = &props["build"]; if get_vec(&props["vec"]) { - get_vec_child(name, rule, build) + get_vec_child(name, &rule, build) } else { - get_single_child(name, rule, build) + let optional = props["optional"] + .as_bool() + .unwrap_or_else(|| false); + + get_single_child(name, &rule, build, optional) } } else { ChildSpec::SingleChild(SingleChild::from_name_snake(child_spec.as_str().unwrap())) diff --git a/ast-generator/src/spec.rs b/ast-generator/src/spec.rs index c6bddb4..5211799 100644 --- a/ast-generator/src/spec.rs +++ b/ast-generator/src/spec.rs @@ -224,6 +224,7 @@ pub struct SingleChild { name: String, rule: String, build: SingleChildToBuild, + optional: bool } impl SingleChild { @@ -234,14 +235,16 @@ impl SingleChild { build: SingleChildToBuild::Type(SingleTypeChildToBuild::from_build_or_rule( &name.to_case(Case::Pascal), )), + optional: false, } } - pub fn new(name: &str, rule: &str, build: SingleChildToBuild) -> Self { + pub fn new(name: &str, rule: &str, build: SingleChildToBuild, optional: bool) -> Self { Self { name: name.to_string(), rule: rule.to_string(), build, + optional } }