From 0adb4bbe0ea8e99c0b385a702a7c5447f6168f47 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Thu, 4 Sep 2025 10:21:30 -0500 Subject: [PATCH] Add or else (default) to yaml spec. --- ast-generator/src/deserialize.rs | 18 ++++++++++++++++-- ast-generator/src/spec.rs | 5 ++++- src/parser/ast.schema.yaml | 6 ++++++ src/parser/ast.yaml | 24 ++++++++++++------------ 4 files changed, 38 insertions(+), 15 deletions(-) diff --git a/ast-generator/src/deserialize.rs b/ast-generator/src/deserialize.rs index 74a49dc..f5bbffa 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, optional: bool, build: &Yam } } -fn get_single_child(name: &str, rule: &str, optional: bool, build: &Yaml) -> ChildSpec { +fn get_single_child(name: &str, rule: &str, optional: bool, or_else: Option, build: &Yaml) -> ChildSpec { ChildSpec::SingleChild(SingleChild::new( name, rule, get_single_child_to_build(name, rule, optional, build), + or_else )) } @@ -109,8 +110,21 @@ fn get_child_specs(children: &Yaml) -> Vec { let optional = props["optional"] .as_bool() .unwrap_or_else(|| false); + let or_else = props["or_else"] + .as_str() + .map(|s| s.to_string()) + .or_else(|| { + let or_else_default = props["or_else_default"] + .as_bool() + .unwrap_or_else(|| false); + if or_else_default { + Some(String::from("default")) + } else { + None + } + }); - get_single_child(name, &rule, optional, build) + get_single_child(name, &rule, optional, or_else, build) } } 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 e21cbee..f42b85c 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, + or_else: Option } impl SingleChild { @@ -235,14 +236,16 @@ impl SingleChild { &name.to_case(Case::Pascal), false, )), + or_else: None, } } - pub fn new(name: &str, rule: &str, build: SingleChildToBuild) -> Self { + pub fn new(name: &str, rule: &str, build: SingleChildToBuild, or_else: Option) -> Self { Self { name: name.to_string(), rule: rule.to_string(), build, + or_else, } } diff --git a/src/parser/ast.schema.yaml b/src/parser/ast.schema.yaml index 695f091..a62f4ca 100644 --- a/src/parser/ast.schema.yaml +++ b/src/parser/ast.schema.yaml @@ -82,6 +82,12 @@ $defs: optional: type: boolean description: If true, this child will be stored as an Option. + or_else: + type: string + description: The method name to call upon the built-type if the rule is not found. Takes precedence over "or_else_default". + or_else_default: + type: boolean + description: Whether to call the default method on the built-type if the rule is not found. build: oneOf: - type: string diff --git a/src/parser/ast.yaml b/src/parser/ast.yaml index 938d738..41b1894 100644 --- a/src/parser/ast.yaml +++ b/src/parser/ast.yaml @@ -112,9 +112,9 @@ Interface: skip: true - identifier - generic_parameters: - optional: true + or_else_default: true - implements_list: - optional: true + or_else_default: true - declarations: rule: InterfaceLevelDeclaration vec: true @@ -133,11 +133,11 @@ Class: skip: true - identifier - generic_parameters: - optional: true + or_else_default: true - class_constructor: - optional: true + or_else_default: true - implements_list: - optional: true + or_else_default: true - class_level_declarations: rule: ClassLevelDeclaration vec: true @@ -155,17 +155,17 @@ Function: skip: true - generics: rule: GenericParameters - optional: true + or_else_default: true - identifier - parameters - return_type: - optional: true + or_else: void - function_body OperatorFunction: children: - is_public: rule: Pub - build: + build: type: boolean on: rule_present - op_kw: @@ -173,17 +173,17 @@ OperatorFunction: skip: true - generics: rule: GenericParameters - optional: true + or_else_default: true - operator - parameters - return_type: - optional: true + or_else: void - function_body PlatformFunction: children: - is_public: rule: Pub - build: + build: type: boolean on: rule_present - platform_kw: @@ -194,7 +194,7 @@ PlatformFunction: skip: true - generics: rule: GenericParameters - optional: true + or_else_default: true - identifier - parameters - return_type