diff --git a/ast-generator/src/deserialize.rs b/ast-generator/src/deserialize.rs index 8b2e00f..fc35cba 100644 --- a/ast-generator/src/deserialize.rs +++ b/ast-generator/src/deserialize.rs @@ -104,8 +104,12 @@ fn get_single_child_to_build( Some(s) => SingleChildToBuild::Type(SingleTypeChildToBuild::from_build_or_rule( s, None, optional, )), - None => SingleChildToBuild::Type(SingleTypeChildToBuild::from_build_or_rule( - rule, None, optional, + None => SingleChildToBuild::Type(SingleTypeChildToBuild::new( + &rule, + &name, + &make_build_fn_name(&rule), + None, + optional, )), } } @@ -126,12 +130,7 @@ fn get_child_specs(children: &Yaml) -> Vec { .iter() .map(|child_spec| { if child_spec.is_hash() { - let as_hash = child_spec.as_hash().unwrap(); - let (name, props) = as_hash - .iter() - .next() - .map(|(name, props)| (name.as_str().unwrap(), props)) - .unwrap(); + let (name, props) = unwrap_single_member_hash(child_spec); let rule = props["rule"] .as_str() @@ -139,17 +138,16 @@ fn get_child_specs(children: &Yaml) -> Vec { .unwrap_or(name.to_case(Case::Pascal)); if get_as_bool(&props["skip"]) { - return ChildSpec::SkipChild(SkipChild::new(name, &rule)); + return ChildSpec::SkipChild(SkipChild::new(&name, &rule)); } let build = &props["build"]; if get_as_bool(&props["vec"]) { - get_vec_child(name, &rule, build) + get_vec_child(&name, &rule, build) } else { - let optional = props["optional"].as_bool().unwrap_or_else(|| false); - - get_single_child(name, &rule, optional, build) + let optional = get_as_bool(&props["optional"]); + get_single_child(&name, &rule, optional, build) } } else { ChildSpec::SingleChild(SingleChild::from_name_snake(child_spec.as_str().unwrap())) diff --git a/ast-generator/src/lib.rs b/ast-generator/src/lib.rs index e993ea4..863f9ac 100644 --- a/ast-generator/src/lib.rs +++ b/ast-generator/src/lib.rs @@ -61,8 +61,21 @@ fn generate_build_file(build_specs: &[BuildSpec]) -> AstGeneratedFile { } } +fn generate_node_file(build_specs: &[BuildSpec]) -> AstGeneratedFile { + let types = build_specs.iter() + .map(|build_spec| make_type(build_spec)) + .collect::>(); + let combined = quote! { + #(#types)* + }; + AstGeneratedFile { + name: String::from("node.rs"), + contents: token_stream_to_string(combined) + } +} + pub fn generate_files(build_specs: &[BuildSpec]) -> Vec { - vec![generate_build_file(build_specs)] + vec![generate_build_file(build_specs), generate_node_file(build_specs)] } pub fn test_dump() -> String { diff --git a/ast-generator/src/spec.rs b/ast-generator/src/spec.rs index 5830d3d..616d2e6 100644 --- a/ast-generator/src/spec.rs +++ b/ast-generator/src/spec.rs @@ -352,6 +352,22 @@ impl SingleTypeChildToBuild { optional, } } + + pub fn new( + build: &str, + var_name: &str, + with: &str, + or_else: Option, + optional: bool, + ) -> Self { + Self { + build: build.to_string(), + var_name: var_name.to_string(), + with: with.to_string(), + or_else, + optional, + } + } /// The type to build, in Pascal case. pub fn build(&self) -> &str { diff --git a/ast-generator/src/type_gen.rs b/ast-generator/src/type_gen.rs index 95e1ffa..db02b95 100644 --- a/ast-generator/src/type_gen.rs +++ b/ast-generator/src/type_gen.rs @@ -136,7 +136,7 @@ fn make_struct_type(build_spec: &StructBuildSpec) -> TokenStream { &mut accessors, ); } - _ => todo!() + _ => {} }; } } @@ -164,7 +164,7 @@ fn make_struct_type(build_spec: &StructBuildSpec) -> TokenStream { pub fn make_type(build_spec: &BuildSpec) -> TokenStream { match build_spec { BuildSpec::Enum(enum_build_spec) => make_enum_type(enum_build_spec), - BuildSpec::LeafEnum(leaf_enum_build_spec) => todo!(), + BuildSpec::LeafEnum(leaf_enum_build_spec) => quote! {}, BuildSpec::Struct(struct_build_spec) => make_struct_type(struct_build_spec), } } diff --git a/src/ast/children.rs b/src/ast/children.rs.bak similarity index 100% rename from src/ast/children.rs rename to src/ast/children.rs.bak diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 557ec4d..17abf56 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1,12 +1,10 @@ +pub mod node { + include!(concat!(env!("OUT_DIR"), "/src/ast/node.rs")); +} + pub mod build { //noinspection RsUnusedImport use crate::parser::Rule; include!(concat!(env!("OUT_DIR"), "/src/ast/build.rs")); } - -pub mod children; -pub mod node; -pub mod pretty_print; -pub mod unparse; -pub mod walk; diff --git a/src/ast/pretty_print.rs b/src/ast/pretty_print.rs.bak similarity index 100% rename from src/ast/pretty_print.rs rename to src/ast/pretty_print.rs.bak diff --git a/src/ast/unparse.rs b/src/ast/unparse.rs.bak similarity index 100% rename from src/ast/unparse.rs rename to src/ast/unparse.rs.bak diff --git a/src/ast/walk.rs b/src/ast/walk.rs.bak similarity index 100% rename from src/ast/walk.rs rename to src/ast/walk.rs.bak