From eaebf8c926df80f64f3930d69e1532c6a635addc Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Mon, 29 Sep 2025 12:22:28 -0500 Subject: [PATCH] Add derive for leaf enum spec, fix compilation errors. --- ast-generator/src/ast_node/struct_ast_node.rs | 2 +- .../src/deserialize/leaf_struct_spec.rs | 12 ++++++++- ast-generator/src/spec/leaf_struct_spec.rs | 8 +++++- .../src/type_gen/leaf_struct_type.rs | 23 +++++++++++++--- src/bin/dmc/name_analysis.rs | 2 +- src/name_analysis/gather.rs | 6 ++--- src/name_analysis/mod.rs | 4 +-- src/parser/ast.schema.yaml | 10 +++++++ src/parser/ast.yaml | 27 +++++++++++-------- 9 files changed, 71 insertions(+), 23 deletions(-) diff --git a/ast-generator/src/ast_node/struct_ast_node.rs b/ast-generator/src/ast_node/struct_ast_node.rs index ff32739..37409fb 100644 --- a/ast-generator/src/ast_node/struct_ast_node.rs +++ b/ast-generator/src/ast_node/struct_ast_node.rs @@ -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! { diff --git a/ast-generator/src/deserialize/leaf_struct_spec.rs b/ast-generator/src/deserialize/leaf_struct_spec.rs index 0b2c78c..73b1115 100644 --- a/ast-generator/src/deserialize/leaf_struct_spec.rs +++ b/ast-generator/src/deserialize/leaf_struct_spec.rs @@ -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::>() + }) + .unwrap_or_default(); + + LeafStructBuildSpec::new(name, members, derive) } diff --git a/ast-generator/src/spec/leaf_struct_spec.rs b/ast-generator/src/spec/leaf_struct_spec.rs index eeb6952..e148c31 100644 --- a/ast-generator/src/spec/leaf_struct_spec.rs +++ b/ast-generator/src/spec/leaf_struct_spec.rs @@ -1,13 +1,15 @@ pub struct LeafStructBuildSpec { build: String, members: Vec>, + derive: Vec, } impl LeafStructBuildSpec { - pub fn new(build: &str, members: Vec>) -> Self { + pub fn new(build: &str, members: Vec>, derive: Vec) -> Self { Self { build: build.to_string(), members, + derive, } } @@ -18,6 +20,10 @@ impl LeafStructBuildSpec { pub fn members(&self) -> impl Iterator { self.members.iter().map(Box::as_ref) } + + pub fn derive(&self) -> impl Iterator { + self.derive.iter().map(String::as_str) + } } pub struct LeafStructMember { diff --git a/ast-generator/src/type_gen/leaf_struct_type.rs b/ast-generator/src/type_gen/leaf_struct_type.rs index 1c7afb5..6bdce5f 100644 --- a/ast-generator/src/type_gen/leaf_struct_type.rs +++ b/ast-generator/src/type_gen/leaf_struct_type.rs @@ -83,10 +83,27 @@ pub fn make_leaf_struct_type(build_spec: &LeafStructBuildSpec) -> TokenStream { }) .collect::>(); - 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::>(); + + 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 { diff --git a/src/bin/dmc/name_analysis.rs b/src/bin/dmc/name_analysis.rs index 97ed110..3bbd993 100644 --- a/src/bin/dmc/name_analysis.rs +++ b/src/bin/dmc/name_analysis.rs @@ -32,7 +32,7 @@ pub fn name_analysis(paths: &Vec) -> Result<(), Box) { - identifier_scope_ids[identifier] = symbol_table.current_scope_id(); +fn gather_identifier(identifier: &Identifier, symbol_table: &SymbolTable, identifier_scope_ids: &mut HashMap) { + 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, diagnostics: &mut Vec, ) { walk_depth_first(compilation_unit, &mut |child| match child { diff --git a/src/name_analysis/mod.rs b/src/name_analysis/mod.rs index 6e8a645..3c89e3c 100644 --- a/src/name_analysis/mod.rs +++ b/src/name_analysis/mod.rs @@ -33,11 +33,11 @@ pub mod symbol; pub mod symbol_table; pub fn analyze_names( - compilation_units: &mut [CompilationUnit], + compilation_units: &mut [Box], symbol_table: &mut SymbolTable, ) -> Vec { let mut diagnostics = vec![]; - let mut identifier_scope_ids: HashMap<&Identifier, usize> = HashMap::new(); + let mut identifier_scope_ids: HashMap = HashMap::new(); // gather symbols for compilation_unit in compilation_units.iter_mut() { diff --git a/src/parser/ast.schema.yaml b/src/parser/ast.schema.yaml index a2f816a..028638b 100644 --- a/src/parser/ast.schema.yaml +++ b/src/parser/ast.schema.yaml @@ -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: diff --git a/src/parser/ast.yaml b/src/parser/ast.yaml index 15c17fc..4e33e2a 100644 --- a/src/parser/ast.yaml +++ b/src/parser/ast.yaml @@ -37,6 +37,11 @@ Identifier: kind: file_id - range: kind: range + derive: + - Clone + - PartialEq + - Eq + - Hash FullyQualifiedName: struct: children: @@ -867,8 +872,8 @@ ComparisonRhs: - expression: member: rule: ShiftExpression - build: - node: + build: + node: kind: Expression with: ShiftExpression ComparisonOperator: @@ -907,8 +912,8 @@ ShiftRhs: - expression: member: rule: AdditiveExpression - build: - node: + build: + node: kind: Expression with: AdditiveExpression ShiftOperator: @@ -943,8 +948,8 @@ AdditiveRhs: - expression: member: rule: MultiplicativeExpression - build: - node: + build: + node: kind: Expression with: MultiplicativeExpression AdditiveOperator: @@ -979,8 +984,8 @@ MultiplicativeRhs: - expression: member: rule: PrefixExpression - build: - node: + build: + node: kind: Expression with: PrefixExpression MultiplicativeOperator: @@ -999,10 +1004,10 @@ PrefixExpression: variant: Prefix children: - operator: - on_each: + on_each: rule: PrefixOperator - expression: - use_current: + use_current: kind: Expression - SuffixExpression: pass_through: @@ -1075,7 +1080,7 @@ ListExpression: children: - expression_list ParenthesizedExpression: - node_production: + node_production: kind: Expression with: Expression