Add derive for leaf enum spec, fix compilation errors.

This commit is contained in:
Jesse Brault 2025-09-29 12:22:28 -05:00
parent 058b33ece5
commit eaebf8c926
9 changed files with 71 additions and 23 deletions

View File

@ -21,7 +21,7 @@ pub fn make_struct_ast_node_impl(spec: &StructSpec) -> TokenStream {
}
},
StructChild::MemberChild(member_child) => match member_child.build() {
MemberChildBuild::Node(node_child) => {
MemberChildBuild::Node(_) => {
let child_ident = format_ident!("{}", member_child.name());
if member_child.optional() {
Some(quote! {

View File

@ -23,5 +23,15 @@ pub fn deserialize_leaf_struct(name: &str, props: &Yaml) -> LeafStructBuildSpec
})
.map(Box::new)
.collect();
LeafStructBuildSpec::new(name, members)
let derive = props["derive"]
.as_vec()
.map(|derive_yaml| {
derive_yaml.iter()
.map(|trait_yaml| {
trait_yaml.as_str().unwrap().to_string()
}).collect::<Vec<_>>()
})
.unwrap_or_default();
LeafStructBuildSpec::new(name, members, derive)
}

View File

@ -1,13 +1,15 @@
pub struct LeafStructBuildSpec {
build: String,
members: Vec<Box<LeafStructMember>>,
derive: Vec<String>,
}
impl LeafStructBuildSpec {
pub fn new(build: &str, members: Vec<Box<LeafStructMember>>) -> Self {
pub fn new(build: &str, members: Vec<Box<LeafStructMember>>, derive: Vec<String>) -> Self {
Self {
build: build.to_string(),
members,
derive,
}
}
@ -18,6 +20,10 @@ impl LeafStructBuildSpec {
pub fn members(&self) -> impl Iterator<Item = &LeafStructMember> {
self.members.iter().map(Box::as_ref)
}
pub fn derive(&self) -> impl Iterator<Item = &str> {
self.derive.iter().map(String::as_str)
}
}
pub struct LeafStructMember {

View File

@ -83,10 +83,27 @@ pub fn make_leaf_struct_type(build_spec: &LeafStructBuildSpec) -> TokenStream {
})
.collect::<Vec<_>>();
quote! {
pub struct #type_ident {
#(#annotated_members),*
let struct_stream = if build_spec.derive().count() > 0 {
let derives = build_spec.derive().map(|derive| {
format_ident!("{}", derive)
}).collect::<Vec<_>>();
quote! {
#[derive(#(#derives),*)]
pub struct #type_ident {
#(#annotated_members),*
}
}
} else {
quote! {
pub struct #type_ident {
#(#annotated_members),*
}
}
};
quote! {
#struct_stream
impl #type_ident {
pub fn new(#(#member_args),*) -> Self {

View File

@ -32,7 +32,7 @@ pub fn name_analysis(paths: &Vec<PathBuf>) -> Result<(), Box<dyn std::error::Err
add_std_core_symbols(&mut symbol_table).expect("Failed to add std::core symbols.");
let diagnostics = analyze_names(
&mut compilation_units.iter().map(Box::as_mut),
compilation_units.as_mut_slice(),
&mut symbol_table
);
if diagnostics.is_empty() {

View File

@ -5,14 +5,14 @@ use crate::ast::walk::walk_depth_first;
use crate::diagnostic::DmDiagnostic;
use crate::name_analysis::symbol_table::SymbolTable;
fn gather_identifier(identifier: &Identifier, symbol_table: &SymbolTable, identifier_scope_ids: &mut HashMap<&Identifier, usize>) {
identifier_scope_ids[identifier] = symbol_table.current_scope_id();
fn gather_identifier(identifier: &Identifier, symbol_table: &SymbolTable, identifier_scope_ids: &mut HashMap<Identifier, usize>) {
identifier_scope_ids.insert(identifier.clone(), symbol_table.current_scope_id());
}
pub fn gather_compilation_unit(
compilation_unit: &mut CompilationUnit,
symbol_table: &mut SymbolTable,
identifier_scope_ids: &mut HashMap<&Identifier, usize>,
identifier_scope_ids: &mut HashMap<Identifier, usize>,
diagnostics: &mut Vec<DmDiagnostic>,
) {
walk_depth_first(compilation_unit, &mut |child| match child {

View File

@ -33,11 +33,11 @@ pub mod symbol;
pub mod symbol_table;
pub fn analyze_names(
compilation_units: &mut [CompilationUnit],
compilation_units: &mut [Box<CompilationUnit>],
symbol_table: &mut SymbolTable,
) -> Vec<DmDiagnostic> {
let mut diagnostics = vec![];
let mut identifier_scope_ids: HashMap<&Identifier, usize> = HashMap::new();
let mut identifier_scope_ids: HashMap<Identifier, usize> = HashMap::new();
// gather symbols
for compilation_unit in compilation_units.iter_mut() {

View File

@ -57,6 +57,11 @@ $defs:
description: Ordered child fields for this node.
items:
$ref: "#/$defs/StructChild"
derive:
type: array
description: Traits to derive.
items:
type: string
required:
- children
StructChild:
@ -201,6 +206,11 @@ $defs:
description: Ordered members for this node.
items:
$ref: "#/$defs/LeafStructMemberDefinition"
derive:
type: array
description: Traits to derive.
items:
type: string
required:
- members
required:

View File

@ -37,6 +37,11 @@ Identifier:
kind: file_id
- range:
kind: range
derive:
- Clone
- PartialEq
- Eq
- Hash
FullyQualifiedName:
struct:
children: