Add generation for node ast file.

This commit is contained in:
Jesse Brault 2025-09-14 15:57:06 -05:00
parent b75e51ee41
commit 300e65a8d3
9 changed files with 47 additions and 22 deletions

View File

@ -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<ChildSpec> {
.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<ChildSpec> {
.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()))

View File

@ -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::<Vec<_>>();
let combined = quote! {
#(#types)*
};
AstGeneratedFile {
name: String::from("node.rs"),
contents: token_stream_to_string(combined)
}
}
pub fn generate_files(build_specs: &[BuildSpec]) -> Vec<AstGeneratedFile> {
vec![generate_build_file(build_specs)]
vec![generate_build_file(build_specs), generate_node_file(build_specs)]
}
pub fn test_dump() -> String {

View File

@ -353,6 +353,22 @@ impl SingleTypeChildToBuild {
}
}
pub fn new(
build: &str,
var_name: &str,
with: &str,
or_else: Option<String>,
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 {
&self.build

View File

@ -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),
}
}

View File

@ -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;