From 9d843097bc41f91727e92d08f00162b24fc04b67 Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Wed, 14 May 2025 10:04:31 -0500 Subject: [PATCH] Much work on build.rs and related. --- src/ast/build.rs | 513 ++++++++++++++++++++++++++++++++++++----- src/ast/mod.rs | 22 +- src/ast/unparse.rs | 147 ++---------- src/parser/deimos.pest | 24 +- 4 files changed, 503 insertions(+), 203 deletions(-) diff --git a/src/ast/build.rs b/src/ast/build.rs index 71cad7b..6884147 100644 --- a/src/ast/build.rs +++ b/src/ast/build.rs @@ -1,9 +1,4 @@ -use crate::ast::{ - CompilationUnit, DelegateOrIdentifier, FullyQualifiedName, FunctionModifier, FunctionTypeUse, - GenericArgument, GenericArguments, GenericParameter, GenericParameters, Identifier, - InputArgument, InputArguments, InterfaceOrClassTypeUse, Reference, References, ReturnType, - TupleTypeUse, TypeUse, VoidOrTypeUse, -}; +use crate::ast::{ClassDeclaration, CompilationUnit, DelegateOrIdentifier, FullyQualifiedName, FunctionDeclaration, FunctionModifier, FunctionTypeParameters, FunctionTypeUse, GenericArgument, GenericArguments, GenericParameter, GenericParameters, Identifier, InputArgument, InputArguments, InterfaceDeclaration, InterfaceFunctionDeclaration, InterfaceOperatorFunctionDeclaration, InterfaceOrClassTypeUse, ModuleDeclaration, ModuleLevelDeclaration, OperatorFunctionDeclaration, PlatformFunctionDeclaration, Reference, References, ReturnType, TupleTypeUse, TypeDeclaration, TypeFunctionArguments, TypeGenericArgument, TypeGenericArguments, TypeImplements, TypeImplementsArguments, TypeImplementsList, TypeTupleArgument, TypeTupleArguments, TypeUse, TypeWhereGuard, TypeWhereGuards, VoidOrTypeUse}; use crate::parser::Rule; use pest::iterators::Pair; @@ -15,7 +10,7 @@ fn expect_and_use(pair: Pair, rule: Rule, f: fn(Pair) -> T) -> T } pub fn build_ast(compilation_unit_pair: Pair) -> CompilationUnit { - todo!() + build_compilation_unit(compilation_unit_pair) } fn build_identifier(identifier_pair: Pair) -> Identifier { @@ -37,61 +32,109 @@ fn build_fqn(fqn_pair: Pair) -> FullyQualifiedName { } fn build_type_use(type_use_pair: Pair) -> TypeUse { - let inner_pair = type_use_pair.into_inner().next().unwrap(); - match inner_pair.as_rule() { - Rule::InterfaceOrClassTypeUse => { - TypeUse::InterfaceOrClass(build_interface_or_class_type_use(inner_pair)) + let mut borrow_count = 0; + let mut result = None; + for inner_pair in type_use_pair.into_inner() { + match inner_pair.as_rule() { + Rule::Borrow => { + borrow_count += 1; + } + Rule::InterfaceOrClassTypeUse => { + result = Some(TypeUse::InterfaceOrClass( + build_interface_or_class_type_use(borrow_count, inner_pair), + )); + } + Rule::TupleTypeUse => { + result = Some(TypeUse::Tuple(build_tuple_type_use( + borrow_count, + inner_pair, + ))); + } + Rule::FunctionTypeUse => { + result = Some(TypeUse::Function(build_function_type_use( + borrow_count, + inner_pair, + ))); + } + _ => unreachable!(), } - Rule::TupleTypeUse => TypeUse::Tuple(build_tuple_type_use(inner_pair)), - Rule::FunctionTypeUse => TypeUse::Function(build_function_type_use(inner_pair)), - _ => unreachable!(), + } + result.unwrap() +} + +fn build_interface_or_class_type_use( + borrow_count: usize, + pair: Pair, +) -> InterfaceOrClassTypeUse { + let mut is_mut = false; + let mut fqn = None; + let mut generic_arguments = GenericArguments(vec![]); + + for inner_pair in pair.into_inner() { + match inner_pair.as_rule() { + Rule::Mut => { + is_mut = true; + } + Rule::FullyQualifiedName => { + fqn = Some(build_fqn(inner_pair)); + } + Rule::GenericArguments => { + generic_arguments = build_generic_arguments(inner_pair); + } + _ => unreachable!(), + } + } + + InterfaceOrClassTypeUse { + borrow_count, + is_mut, + fqn: fqn.unwrap(), + generics: generic_arguments, } } -fn build_interface_or_class_type_use(pair: Pair) -> InterfaceOrClassTypeUse { - let mut inner = pair.into_inner(); - let fqn = expect_and_use(inner.next().unwrap(), Rule::FullyQualifiedName, build_fqn); - - let generics = inner - .next() - .map(|inner_pair| { - expect_and_use(inner_pair, Rule::GenericArguments, build_generic_arguments) - }) - .unwrap_or(GenericArguments(vec![])); - - InterfaceOrClassTypeUse { fqn, generics } +fn build_tuple_type_use(borrow_count: usize, tuple_type_use_pair: Pair) -> TupleTypeUse { + let mut is_mut = false; + let mut type_uses = vec![]; + for inner_pair in tuple_type_use_pair.into_inner() { + match inner_pair.as_rule() { + Rule::Mut => { + is_mut = true; + } + Rule::TypeUse => { + type_uses.push(build_type_use(inner_pair)); + } + _ => unreachable!(), + } + } + TupleTypeUse { + borrow_count, + is_mut, + type_uses, + } } -fn build_tuple_type_use(tuple_type_use_pair: Pair) -> TupleTypeUse { - TupleTypeUse( - tuple_type_use_pair - .into_inner() - .map(|type_use_pair| expect_and_use(type_use_pair, Rule::TypeUse, build_type_use)) - .collect(), - ) -} - -fn build_function_type_use(function_pair: Pair) -> FunctionTypeUse { +fn build_function_type_use(borrow_count: usize, function_pair: Pair) -> FunctionTypeUse { let mut function_modifier: Option = None; - let mut generics: Option = None; - let mut parameters: Option = None; - let mut inputs: Option = None; + let mut generics: GenericParameters = GenericParameters(vec![]); + let mut parameters: Option = None; + let mut inputs: InputArguments = InputArguments(vec![]); let mut return_type: Option = None; for inner_pair in function_pair.into_inner() { match inner_pair.as_rule() { Rule::FunctionTypeModifier => { - function_modifier = Some(build_function_modifier(inner_pair)); + function_modifier = Some(build_function_type_modifier(inner_pair)); } Rule::Fn => {} Rule::GenericParameters => { - generics = Some(build_generic_parameters(inner_pair)); + generics = build_generic_parameters(inner_pair); } - Rule::TupleTypeUse => { - parameters = Some(build_tuple_type_use(inner_pair)); + Rule::FunctionTypeParameters => { + parameters = Some(build_function_type_parameters(inner_pair)); } Rule::FunctionInputArguments => { - inputs = Some(build_function_input_arguments(inner_pair)); + inputs = build_function_input_arguments(inner_pair); } Rule::ReturnType => { return_type = Some(build_return_type(inner_pair)); @@ -101,10 +144,11 @@ fn build_function_type_use(function_pair: Pair) -> FunctionTypeUse { } FunctionTypeUse { + borrow_count, function_modifier, - generics: generics.unwrap_or(GenericParameters(vec![])), + generics, parameters: parameters.unwrap(), - inputs: inputs.unwrap_or(InputArguments(vec![])), + inputs, return_type: return_type.unwrap(), } } @@ -127,20 +171,6 @@ fn build_generic_argument(fqn_pair: Pair) -> GenericArgument { } } -fn build_function_modifier(function_modifier_pair: Pair) -> FunctionModifier { - let mut inner = function_modifier_pair.into_inner(); - if inner.len() == 2 { - FunctionModifier::MutRef - } else { - match inner.next().unwrap().as_rule() { - Rule::Cons => FunctionModifier::Cons, - Rule::Mut => FunctionModifier::Mut, - Rule::Ref => FunctionModifier::Ref, - _ => unreachable!(), - } - } -} - fn build_generic_parameters(generic_parameters_pair: Pair) -> GenericParameters { GenericParameters( generic_parameters_pair @@ -156,6 +186,17 @@ fn build_generic_parameters(generic_parameters_pair: Pair) -> GenericParam ) } +fn build_function_type_parameters( + function_type_parameters_pair: Pair, +) -> FunctionTypeParameters { + FunctionTypeParameters( + function_type_parameters_pair + .into_inner() + .map(|type_use_pair| expect_and_use(type_use_pair, Rule::TypeUse, build_type_use)) + .collect(), + ) +} + fn build_function_input_arguments(pair: Pair) -> InputArguments { InputArguments( pair.into_inner() @@ -176,6 +217,20 @@ fn build_function_input_arguments(pair: Pair) -> InputArguments { ) } +fn build_function_type_modifier(function_type_modifier_pair: Pair) -> FunctionModifier { + let mut inner = function_type_modifier_pair.into_inner(); + if inner.len() == 2 { + FunctionModifier::MutRef + } else { + match inner.next().unwrap().as_rule() { + Rule::Cons => FunctionModifier::Cons, + Rule::Mut => FunctionModifier::Mut, + Rule::Ref => FunctionModifier::Ref, + _ => unreachable!(), + } + } +} + fn build_return_type(return_type_pair: Pair) -> ReturnType { let mut inner = return_type_pair.into_inner(); @@ -215,3 +270,341 @@ fn build_references(ref_list_pair: Pair) -> References { .collect(), ) } + +fn build_compilation_unit(compilation_unit_pair: Pair) -> CompilationUnit { + let mut namespace = None; + let mut declarations = vec![]; + + for inner_pair in compilation_unit_pair.into_inner() { + match inner_pair.as_rule() { + Rule::Namespace => { + namespace = Some(build_namespace(inner_pair)); + } + Rule::ModuleLevelDeclaration => { + declarations.push(build_module_level_declaration(inner_pair)); + } + _ => unreachable!(), + } + } + + CompilationUnit { + namespace, + declarations, + } +} + +fn build_namespace(namespace_pair: Pair) -> FullyQualifiedName { + let mut inner = namespace_pair.into_inner(); + inner.next(); // ns + build_fqn(inner.next().unwrap()) +} + +fn build_module_level_declaration(pair: Pair) -> ModuleLevelDeclaration { + let inner_pair = pair.into_inner().next().unwrap(); + match inner_pair.as_rule() { + Rule::Type => ModuleLevelDeclaration::Type(build_type_declaration(inner_pair)), + Rule::Module => ModuleLevelDeclaration::Module(build_module_declaration(inner_pair)), + Rule::Interface => { + ModuleLevelDeclaration::Interface(build_interface_declaration(inner_pair)) + } + Rule::Class => ModuleLevelDeclaration::Class(build_class_declaration(inner_pair)), + Rule::FunctionDefinition => { + ModuleLevelDeclaration::Function(build_function_declaration(inner_pair)) + } + Rule::PlatformFunction => ModuleLevelDeclaration::PlatformFunction( + build_platform_function_declaration(inner_pair), + ), + _ => unreachable!(), + } +} + +fn build_module_declaration(module_pair: Pair) -> ModuleDeclaration { + let mut is_public = false; + let mut identifier = None; + let mut declarations = vec![]; + + for inner_pair in module_pair.into_inner() { + match inner_pair.as_rule() { + Rule::Pub => { + is_public = true; + } + Rule::Mod => {} + Rule::Identifier => { + identifier = Some(build_identifier(inner_pair)); + } + Rule::ModuleLevelDeclaration => { + declarations.push(build_module_level_declaration(inner_pair)); + } + _ => unreachable!(), + } + } + + ModuleDeclaration { + is_public, + identifier: identifier.unwrap(), + declarations, + } +} + +fn build_interface_declaration(interface_pair: Pair) -> InterfaceDeclaration { + todo!() +} + +fn build_class_declaration(class_pair: Pair) -> ClassDeclaration { + todo!() +} + +fn build_type_declaration(type_pair: Pair) -> TypeDeclaration { + let mut is_public = false; + let mut identifier = None; + let mut lhs = None; + let mut where_guards = TypeWhereGuards(vec![]); + let mut rhs = None; + + for inner_pair in type_pair.into_inner() { + match inner_pair.as_rule() { + Rule::Pub => is_public = true, + Rule::TypeKw => {} + Rule::Identifier => { + identifier = Some(build_identifier(inner_pair)); + } + Rule::TypeUse => { + if lhs.is_none() { + lhs = Some(build_type_use(inner_pair)); + } else { + rhs = Some(build_type_use(inner_pair)); + } + } + Rule::TypeWhereGuards => { + where_guards = build_type_where_guards(inner_pair); + } + _ => unreachable!(), + } + } + + TypeDeclaration { + is_public, + identifier: identifier.unwrap(), + lhs: lhs.unwrap(), + where_guards, + rhs: rhs.unwrap(), + } +} + +fn build_type_where_guards(type_where_guards_pair: Pair) -> TypeWhereGuards { + let mut inner = type_where_guards_pair.into_inner(); + inner.next(); // where + TypeWhereGuards( + inner + .map(|type_where_guard_pair| { + expect_and_use( + type_where_guard_pair, + Rule::TypeWhereGuard, + build_type_where_guard, + ) + }) + .collect(), + ) +} + +fn build_type_where_guard(type_where_guard_pair: Pair) -> TypeWhereGuard { + let mut inner = type_where_guard_pair.into_inner(); + let identifier = expect_and_use(inner.next().unwrap(), Rule::Identifier, build_identifier); + let implements = expect_and_use( + inner.next().unwrap(), + Rule::TypeImplementsList, + build_type_implements_list, + ); + TypeWhereGuard { + identifier, + implements, + } +} + +fn build_type_implements_list(type_implements_list_pair: Pair) -> TypeImplementsList { + TypeImplementsList( + type_implements_list_pair + .into_inner() + .map(|type_implements_pair| { + expect_and_use( + type_implements_pair, + Rule::TypeImplements, + build_type_implements, + ) + }) + .collect(), + ) +} + +fn build_type_implements(type_implements_pair: Pair) -> TypeImplements { + let mut inner = type_implements_pair.into_inner(); + let fqn = expect_and_use(inner.next().unwrap(), Rule::FullyQualifiedName, build_fqn); + let arguments = expect_and_use( + inner.next().unwrap(), + Rule::TypeImplementsArguments, + build_type_implements_arguments, + ); + TypeImplements { fqn, arguments } +} + +fn build_type_implements_arguments( + type_implements_arguments_pair: Pair, +) -> TypeImplementsArguments { + let inner_pair = type_implements_arguments_pair.into_inner().next().unwrap(); + match inner_pair.as_rule() { + Rule::TypeGenericArguments => { + TypeImplementsArguments::Generic(build_type_generic_arguments(inner_pair)) + } + Rule::TypeTupleArguments => { + TypeImplementsArguments::Tuple(build_type_tuple_arguments(inner_pair)) + } + Rule::TypeFunctionArguments => { + TypeImplementsArguments::Function(build_type_function_arguments(inner_pair)) + } + _ => unreachable!(), + } +} + +fn build_type_generic_arguments(type_generic_arguments_pair: Pair) -> TypeGenericArguments { + TypeGenericArguments( + type_generic_arguments_pair + .into_inner() + .map(|type_generic_argument_pair| { + expect_and_use( + type_generic_argument_pair, + Rule::TypeGenericArgument, + build_type_generic_argument, + ) + }) + .collect(), + ) +} + +fn build_type_generic_argument(type_generic_argument_pair: Pair) -> TypeGenericArgument { + let mut inner = type_generic_argument_pair.into_inner(); + if inner.len() == 2 { + inner.next(); // infer + TypeGenericArgument::Infer(expect_and_use( + inner.next().unwrap(), + Rule::Identifier, + build_identifier, + )) + } else { + let inner_pair = inner.next().unwrap(); + match inner_pair.as_rule() { + Rule::Underscore => TypeGenericArgument::Underscore, + Rule::FullyQualifiedName => { + TypeGenericArgument::FullyQualifiedName(build_fqn(inner_pair)) + } + _ => unreachable!(), + } + } +} + +fn build_type_tuple_arguments(type_tuple_arguments_pair: Pair) -> TypeTupleArguments { + TypeTupleArguments( + type_tuple_arguments_pair + .into_inner() + .map(|type_tuple_argument_pair| { + expect_and_use( + type_tuple_argument_pair, + Rule::TypeTupleArgument, + build_type_tuple_argument, + ) + }) + .collect(), + ) +} + +fn build_type_tuple_argument(type_tuple_argument_pair: Pair) -> TypeTupleArgument { + let mut inner = type_tuple_argument_pair.into_inner(); + let first = inner.next().unwrap(); + match first.as_rule() { + Rule::Underscore => TypeTupleArgument::Underscore, + Rule::FullyQualifiedName => TypeTupleArgument::FullyQualifiedName(build_fqn(first)), + Rule::Infer => { + let second = inner.next().unwrap(); + TypeTupleArgument::Infer(expect_and_use(second, Rule::Identifier, build_identifier)) + } + Rule::Ellipsis => { + let second = inner.next().unwrap(); + match second.as_rule() { + Rule::Underscore => TypeTupleArgument::EllipsisUnderscore, + Rule::Infer => TypeTupleArgument::EllipsisInfer(expect_and_use( + inner.next().unwrap(), + Rule::Identifier, + build_identifier, + )), + _ => unreachable!(), + } + } + _ => unreachable!(), + } +} + +fn build_type_function_arguments( + type_function_arguments_pair: Pair, +) -> TypeFunctionArguments { + let mut generics = TypeGenericArguments(vec![]); + let mut parameters = None; + let mut return_type = None; + + for inner_pair in type_function_arguments_pair.into_inner() { + match inner_pair.as_rule() { + Rule::TypeGenericArguments => { + generics = build_type_generic_arguments(inner_pair); + } + Rule::TypeTupleArguments => { + parameters = Some(build_type_tuple_arguments(inner_pair)); + } + Rule::ReturnType => { + return_type = Some(build_return_type(inner_pair)); + } + _ => unreachable!(), + } + } + + TypeFunctionArguments { + generics, + parameters: parameters.unwrap(), + return_type: return_type.unwrap(), + } +} + +fn build_function_declaration(function_definition_pair: Pair) -> FunctionDeclaration { + todo!() +} + +fn build_operator_function_declaration( + operator_function_pair: Pair, +) -> OperatorFunctionDeclaration { + todo!() +} + +fn build_platform_function_declaration(platform_function_pair: Pair) -> PlatformFunctionDeclaration { + todo!() +} + +fn build_interface_function_declaration( + interface_function_pair: Pair, +) -> InterfaceFunctionDeclaration { + todo!() +} + +fn build_interface_operator_function_declaration( + interface_operator_pair: Pair, +) -> InterfaceOperatorFunctionDeclaration { + todo!() +} + +fn build_default_interface_function_declaration( + default_interface_function_pair: Pair, +) -> InterfaceFunctionDeclaration { + todo!() +} + +fn build_default_interface_operator_function_declaration( + default_interface_operator_pair: Pair, +) -> InterfaceOperatorFunctionDeclaration { + todo!() +} diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 2dc7beb..a9c92e5 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -69,18 +69,25 @@ pub enum TypeUse { #[derive(Debug)] pub struct InterfaceOrClassTypeUse { + pub borrow_count: usize, + pub is_mut: bool, pub fqn: FullyQualifiedName, pub generics: GenericArguments, } #[derive(Debug)] -pub struct TupleTypeUse(pub Vec); +pub struct TupleTypeUse { + pub borrow_count: usize, + pub is_mut: bool, + pub type_uses: Vec, +} #[derive(Debug)] pub struct FunctionTypeUse { + pub borrow_count: usize, pub function_modifier: Option, pub generics: GenericParameters, - pub parameters: TupleTypeUse, + pub parameters: FunctionTypeParameters, pub inputs: InputArguments, pub return_type: ReturnType, } @@ -126,6 +133,11 @@ impl GenericParameters { #[derive(Debug)] pub struct GenericParameter(Identifier); +/* Function Type Parameters */ + +#[derive(Debug)] +pub struct FunctionTypeParameters(pub Vec); + /* Input Arguments */ #[derive(Debug)] @@ -341,7 +353,7 @@ pub enum TypeTupleArgument { #[derive(Debug)] pub struct TypeFunctionArguments { - pub generics: GenericArguments, + pub generics: TypeGenericArguments, pub parameters: TypeTupleArguments, pub return_type: ReturnType, } @@ -387,6 +399,7 @@ pub struct InterfaceFunctionDeclaration { pub identifier: Identifier, pub parameters: Parameters, pub return_type: ReturnType, + pub body: Option, } #[derive(Debug)] @@ -396,10 +409,9 @@ pub struct InterfaceOperatorFunctionDeclaration { pub operator: Operator, pub parameters: Parameters, pub return_type: ReturnType, + pub body: Option, } -// TODO: default interface functions - #[derive(Debug)] pub enum FunctionBody { Equals(Expression), diff --git a/src/ast/unparse.rs b/src/ast/unparse.rs index 36624d2..a591e2b 100644 --- a/src/ast/unparse.rs +++ b/src/ast/unparse.rs @@ -1,4 +1,3 @@ -use std::fmt::write; use crate::ast::*; macro_rules! to_unparse_vec { @@ -80,7 +79,7 @@ impl Unparse for Operator { use Operator::*; match self { Binary(op) => op.unparse(buf), - Unary(op) => op.unparse(buf), + PrefixUnary(op) => op.unparse(buf), } } } @@ -108,9 +107,9 @@ impl Unparse for BinaryOperator { } } -impl Unparse for UnaryOperator { +impl Unparse for PrefixUnaryOperator { fn unparse(&self, buf: &mut dyn std::fmt::Write) -> std::fmt::Result { - use UnaryOperator::*; + use PrefixUnaryOperator::*; match self { Not => write!(buf, "!"), Negative => write!(buf, "-"), @@ -130,7 +129,7 @@ impl Unparse for Identifier { } } -impl ListUnparse for Fqn { +impl ListUnparse for FullyQualifiedName { fn separator() -> &'static str { "::" } @@ -198,7 +197,9 @@ impl Unparse for TypeUse { fn unparse(&self, buf: &mut dyn std::fmt::Write) -> std::fmt::Result { use TypeUse::*; match self { - InterfaceOrClass(interface_or_class_type_use) => interface_or_class_type_use.unparse(buf), + InterfaceOrClass(interface_or_class_type_use) => { + interface_or_class_type_use.unparse(buf) + } Tuple(tuple_type_use) => tuple_type_use.unparse(buf), Function(function_type_use) => function_type_use.unparse(buf), } @@ -217,15 +218,15 @@ impl ListUnparse for TupleTypeUse { fn prefix() -> &'static str { "(" } - + fn separator() -> &'static str { ", " } - + fn suffix() -> &'static str { ")" } - + fn inner(&self) -> Vec<&dyn Unparse> { to_unparse_vec!(self.0) } @@ -252,44 +253,6 @@ impl Unparse for FunctionTypeUse { } } -impl Unparse for TypeArguments { - fn unparse(&self, buf: &mut dyn std::fmt::Write) -> std::fmt::Result { - use TypeArguments::*; - match self { - Generics(generic_arguments) => generic_arguments.unparse(buf), - Tuple(tuple_arguments) => tuple_arguments.unparse(buf), - Function(function_type_arguments) => function_type_arguments.unparse(buf), - } - } -} - -impl ListUnparse for TupleArguments { - fn prefix() -> &'static str { - "(" - } - - fn separator() -> &'static str { - ", " - } - - fn suffix() -> &'static str { - ")" - } - - fn inner(&self) -> Vec<&dyn Unparse> { - to_unparse_vec!(self.0) - } -} - -impl Unparse for FunctionTypeArguments { - fn unparse(&self, buf: &mut dyn std::fmt::Write) -> std::fmt::Result { - self.parameters.unparse(buf)?; - write!(buf, " ")?; - self.return_type.unparse(buf)?; - unparse_ok!() - } -} - /* Function components */ impl ListUnparse for Parameters { @@ -417,31 +380,6 @@ impl Unparse for InputArgument { } } -/* Where guards */ - -impl ListUnparse for WhereGuards { - fn prefix() -> &'static str { - "where " - } - - fn separator() -> &'static str { - ", " - } - - fn inner(&self) -> Vec<&dyn Unparse> { - to_unparse_vec!(self.0) - } -} - -impl Unparse for WhereGuard { - fn unparse(&self, buf: &mut dyn std::fmt::Write) -> std::fmt::Result { - self.identifier.unparse(buf)?; - write!(buf, " : ")?; - self.implements.unparse(buf)?; - unparse_ok!() - } -} - /* Implements */ impl ListUnparse for ImplementsList { @@ -540,7 +478,6 @@ impl Unparse for TypeDeclaration { type_parameters.unparse(buf)?; write!(buf, " ")?; } - self.where_guards.unparse(buf)?; write!(buf, "= ")?; self.rhs.unparse(buf)?; Ok(()) @@ -578,10 +515,6 @@ impl Unparse for InterfaceDeclaration { self.implements.unparse(buf)?; write!(buf, " ")?; } - if !self.where_guards.is_empty() { - self.where_guards.unparse(buf)?; - write!(buf, " ")?; - } unparse_contained_declarations!(self.declarations, buf); unparse_ok!() } @@ -605,14 +538,6 @@ impl Unparse for ClassDeclaration { self.implements.unparse(buf)?; write!(buf, " ")?; } - if !self.inputs.is_empty() { - self.inputs.unparse(buf)?; - write!(buf, " ")?; - } - if !self.where_guards.is_empty() { - self.where_guards.unparse(buf)?; - write!(buf, " ")?; - } unparse_contained_declarations!(self.declarations, buf); unparse_ok!() } @@ -637,12 +562,12 @@ impl Unparse for FunctionBody { fn unparse(&self, buf: &mut dyn std::fmt::Write) -> std::fmt::Result { use FunctionBody::*; match self { - EqualsBody(expression) => { + Equals(expression) => { write!(buf, "= ")?; expression.unparse(buf) } - BlockBody(body) => body.unparse(buf), - AliasBody(identifier) => { + Block(body) => body.unparse(buf), + Alias(identifier) => { write!(buf, "alias ")?; identifier.unparse(buf) } @@ -752,46 +677,6 @@ impl Unparse for InterfaceOperatorFunctionDeclaration { } } -/* Type components */ - -impl Unparse for TypeParameters { - fn unparse(&self, buf: &mut dyn std::fmt::Write) -> std::fmt::Result { - use TypeParameters::*; - match self { - Generic(generic_parameters) => generic_parameters.unparse(buf), - Tuple(tuple_parameters) => tuple_parameters.unparse(buf), - Function(function_type_parameters) => function_type_parameters.unparse(buf), - } - } -} - -impl ListUnparse for TupleParameters { - fn prefix() -> &'static str { - "(" - } - - fn separator() -> &'static str { - ", " - } - - fn suffix() -> &'static str { - ")" - } - - fn inner(&self) -> Vec<&dyn Unparse> { - to_unparse_vec!(self.0) - } -} - -impl Unparse for FunctionTypeParameters { - fn unparse(&self, buf: &mut dyn std::fmt::Write) -> std::fmt::Result { - self.parameters.unparse(buf)?; - write!(buf, " ")?; - self.return_type.unparse(buf)?; - unparse_ok!() - } -} - /* Class components */ impl ListUnparse for ClassConstructor { @@ -915,7 +800,7 @@ impl Unparse for IfStatement { write!(buf, "if ")?; self.condition.unparse(buf)?; write!(buf, " {{\n")?; - self.then_branch.unparse(buf)?; + self.then_block.unparse(buf)?; write!(buf, "\n}}")?; unparse_ok!() } @@ -926,11 +811,11 @@ impl Unparse for IfElseStatement { write!(buf, "if ")?; self.condition.unparse(buf)?; write!(buf, " ")?; - self.then_branch.unparse(buf)?; + self.then_block.unparse(buf)?; write!(buf, " ")?; self.else_ifs.unparse(buf)?; write!(buf, "else ")?; - self.else_branch.unparse(buf)?; + self.else_Block.unparse(buf)?; unparse_ok!() } } diff --git a/src/parser/deimos.pest b/src/parser/deimos.pest index 6f97efa..1c0fa0a 100644 --- a/src/parser/deimos.pest +++ b/src/parser/deimos.pest @@ -144,10 +144,7 @@ FullyQualifiedName = { // Parameters = declaration TypeUse = { - ( - Mut - | ( Borrow ~ Mut? )* - )? + Borrow* ~ ( InterfaceOrClassTypeUse | TupleTypeUse @@ -156,12 +153,14 @@ TypeUse = { } InterfaceOrClassTypeUse = { - FullyQualifiedName + Mut? + ~ FullyQualifiedName ~ GenericArguments? } TupleTypeUse = { - "(" + Mut? + ~ "(" ~ ( TypeUse ~ ( "," ~ TypeUse )* @@ -173,7 +172,7 @@ FunctionTypeUse = { FunctionTypeModifier? ~ Fn ~ GenericParameters? - ~ TupleTypeUse + ~ FunctionTypeParameters ~ FunctionInputArguments? ~ ReturnType } @@ -196,6 +195,17 @@ GenericParameters = { ~ ">" } +// Function Type Parameters + +FunctionTypeParameters = { + "(" + ~ ( + TypeUse + ~ ( "," ~ TypeUse )* + )? + ~ ")" +} + // Function Input Arguments FunctionInputArguments = {