Skip to content

Commit

Permalink
Represent func-type params stricter
Browse files Browse the repository at this point in the history
  • Loading branch information
werediver committed Aug 29, 2023
1 parent b0c9ab9 commit 0bf39f7
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
14 changes: 7 additions & 7 deletions dart-parser/src/dart/func_like.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ pub enum FuncModifier {
}

#[derive(PartialEq, Eq, Debug)]
pub struct FuncParams<Param> {
pub positional_req: Vec<Param>,
pub extra: Option<FuncParamsExtra<Param>>,
pub struct FuncParams<ParamPos, ParamNamed = ParamPos> {
pub positional_req: Vec<ParamPos>,
pub extra: Option<FuncParamsExtra<ParamPos, ParamNamed>>,
}

impl<T> Default for FuncParams<T> {
impl<T, U> Default for FuncParams<T, U> {
fn default() -> Self {
Self {
positional_req: Vec::new(),
Expand All @@ -60,9 +60,9 @@ impl<T> Default for FuncParams<T> {
}

#[derive(PartialEq, Eq, Debug)]
pub enum FuncParamsExtra<Param> {
PositionalOpt(Vec<Param>),
Named(Vec<MaybeRequired<Param>>),
pub enum FuncParamsExtra<ParamPos, ParamNamed = ParamPos> {
PositionalOpt(Vec<ParamPos>),
Named(Vec<MaybeRequired<ParamNamed>>),
}

#[derive(PartialEq, Eq, Debug)]
Expand Down
13 changes: 10 additions & 3 deletions dart-parser/src/dart/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,20 @@ impl<'s> NotFuncType<'s> {
pub struct FuncType<'s> {
pub return_type: Type<'s>,
pub type_params: Vec<TypeParam<'s>>,
pub params: FuncParams<FuncTypeParam<'s>>,
pub params: FuncParams<FuncTypeParamPos<'s>, FuncTypeParamNamed<'s>>,
pub is_nullable: bool,
}

/// A parameter in a function type.
/// A positional parameter in a function type.
#[derive(PartialEq, Eq, Debug)]
pub struct FuncTypeParam<'s> {
pub struct FuncTypeParamPos<'s> {
pub param_type: Type<'s>,
pub name: Option<&'s str>,
}

/// A named parameter in a function type.
#[derive(PartialEq, Eq, Debug)]
pub struct FuncTypeParamNamed<'s> {
pub param_type: Type<'s>,
pub name: &'s str,
}
48 changes: 24 additions & 24 deletions dart-parser/src/parser/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use nom::{

use crate::dart::{
func_like::{FuncParams, FuncParamsExtra},
ty::{FuncType, FuncTypeParam, Type},
ty::{FuncType, FuncTypeParamNamed, FuncTypeParamPos, Type},
MaybeRequired, NotFuncType, TypeParam,
};

Expand Down Expand Up @@ -144,7 +144,11 @@ where

fn build_func_type<'s>(
return_type: Option<NotFuncType<'s>>,
fn_chain: Vec<(Vec<TypeParam<'s>>, FuncParams<FuncTypeParam<'s>>, bool)>,
fn_chain: Vec<(
Vec<TypeParam<'s>>,
FuncParams<FuncTypeParamPos<'s>, FuncTypeParamNamed<'s>>,
bool,
)>,
) -> Option<Box<FuncType<'s>>> {
let ty = fn_chain.into_iter().fold(
Type::NotFunc(return_type.unwrap_or(NotFuncType::dynamic())),
Expand All @@ -165,7 +169,9 @@ fn build_func_type<'s>(
}
}

fn func_type_params<'s, E>(s: &'s str) -> PResult<FuncParams<FuncTypeParam>, E>
fn func_type_params<'s, E>(
s: &'s str,
) -> PResult<FuncParams<FuncTypeParamPos<'s>, FuncTypeParamNamed<'s>>, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
Expand All @@ -192,7 +198,7 @@ where
.parse(s)
}

fn func_type_params_pos_req<'s, E>(s: &'s str) -> PResult<Vec<FuncTypeParam>, E>
fn func_type_params_pos_req<'s, E>(s: &'s str) -> PResult<Vec<FuncTypeParamPos>, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
Expand All @@ -204,7 +210,7 @@ where
)(s)
}

fn func_type_params_pos_opt<'s, E>(s: &'s str) -> PResult<Vec<FuncTypeParam>, E>
fn func_type_params_pos_opt<'s, E>(s: &'s str) -> PResult<Vec<FuncTypeParamPos>, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
Expand All @@ -222,7 +228,7 @@ where
)(s)
}

fn func_type_param_pos<'s, E>(s: &'s str) -> PResult<FuncTypeParam, E>
fn func_type_param_pos<'s, E>(s: &'s str) -> PResult<FuncTypeParamPos, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
Expand All @@ -237,11 +243,11 @@ where
// Just a type
terminated(ty, opt(spbr)).map(|ty| (ty, None)),
))
.map(|(param_type, name)| FuncTypeParam { param_type, name }),
.map(|(param_type, name)| FuncTypeParamPos { param_type, name }),
)(s)
}

fn func_type_params_named<'s, E>(s: &'s str) -> PResult<Vec<MaybeRequired<FuncTypeParam>>, E>
fn func_type_params_named<'s, E>(s: &'s str) -> PResult<Vec<MaybeRequired<FuncTypeParamNamed>>, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
Expand All @@ -262,7 +268,7 @@ where
)(s)
}

fn func_type_param_named<'s, E>(s: &'s str) -> PResult<MaybeRequired<FuncTypeParam>, E>
fn func_type_param_named<'s, E>(s: &'s str) -> PResult<MaybeRequired<FuncTypeParamNamed>, E>
where
E: ParseError<&'s str> + ContextError<&'s str>,
{
Expand All @@ -275,13 +281,7 @@ where
terminated(identifier, opt(spbr)),
))
.map(|(req, param_type, name)| {
MaybeRequired::new(
req.is_some(),
FuncTypeParam {
param_type,
name: Some(name),
},
)
MaybeRequired::new(req.is_some(), FuncTypeParamNamed { param_type, name })
}),
)
.parse(s)
Expand Down Expand Up @@ -314,7 +314,7 @@ mod tests {

use crate::dart::{
func_like::FuncParams,
ty::{FuncTypeParam, Type},
ty::{FuncTypeParamPos, Type},
};

use super::*;
Expand Down Expand Up @@ -401,7 +401,7 @@ mod tests {
return_type: Type::NotFunc(NotFuncType::name("void")),
type_params: Vec::new(),
params: FuncParams {
positional_req: vec![FuncTypeParam {
positional_req: vec![FuncTypeParamPos {
param_type: Type::NotFunc(NotFuncType::name("int")),
name: None
}],
Expand All @@ -426,7 +426,7 @@ mod tests {
extends: None
}],
params: FuncParams {
positional_req: vec![FuncTypeParam {
positional_req: vec![FuncTypeParamPos {
param_type: Type::NotFunc(NotFuncType::name("T")),
name: None
}],
Expand All @@ -448,7 +448,7 @@ mod tests {
return_type: Type::NotFunc(NotFuncType::name("void")),
type_params: Vec::new(),
params: FuncParams {
positional_req: vec![FuncTypeParam {
positional_req: vec![FuncTypeParamPos {
param_type: Type::NotFunc(NotFuncType::name("int")),
name: Some("x"),
}],
Expand All @@ -473,13 +473,13 @@ mod tests {
positional_req: Vec::new(),
extra: Some(FuncParamsExtra::Named(vec![MaybeRequired::new(
false,
FuncTypeParam {
FuncTypeParamNamed {
param_type: Type::NotFunc(NotFuncType {
name: "int",
type_args: Vec::new(),
is_nullable: true
}),
name: Some("x"),
name: "x",
}
)])),
},
Expand All @@ -502,7 +502,7 @@ mod tests {
positional_req: Vec::new(),
extra: Some(FuncParamsExtra::Named(vec![MaybeRequired::new(
false,
FuncTypeParam {
FuncTypeParamNamed {
param_type: Type::NotFunc(NotFuncType {
name: "List",
type_args: vec![Type::func(FuncType {
Expand All @@ -513,7 +513,7 @@ mod tests {
})],
is_nullable: true
}),
name: Some("funcs"),
name: "funcs",
}
)])),
},
Expand Down

0 comments on commit 0bf39f7

Please sign in to comment.