Replace 'public' utils with a more generic 'optional_flag'
The 'public' util was arguably not really adding much except a layer of indirection. In the end, one useful parsing behavior to abstract is the idea of 'optional flag' that we use for both 'pub' and 'opaque' keywords.
This commit is contained in:
@@ -6,8 +6,7 @@ use crate::{
|
||||
};
|
||||
|
||||
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
|
||||
utils::public()
|
||||
.or_not()
|
||||
utils::optional_flag(Token::Pub)
|
||||
.then_ignore(just(Token::Const))
|
||||
.then(select! {Token::Name{name} => name})
|
||||
.then(
|
||||
@@ -21,7 +20,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
|
||||
ast::UntypedDefinition::ModuleConstant(ast::ModuleConstant {
|
||||
doc: None,
|
||||
location: span,
|
||||
public: public.is_some(),
|
||||
public,
|
||||
name,
|
||||
annotation,
|
||||
value: Box::new(value),
|
||||
|
||||
@@ -46,34 +46,33 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
|
||||
}]
|
||||
});
|
||||
|
||||
utils::public()
|
||||
.then(just(Token::Opaque).ignored().or_not())
|
||||
.or_not()
|
||||
utils::optional_flag(Token::Pub)
|
||||
.then(utils::optional_flag(Token::Opaque))
|
||||
.then(utils::type_name_with_args())
|
||||
.then(choice((constructors, record_sugar)))
|
||||
.map_with_span(|((pub_opaque, (name, parameters)), constructors), span| {
|
||||
ast::UntypedDefinition::DataType(ast::DataType {
|
||||
location: span,
|
||||
constructors: constructors
|
||||
.into_iter()
|
||||
.map(|mut constructor| {
|
||||
if constructor.sugar {
|
||||
constructor.name = name.clone();
|
||||
}
|
||||
.map_with_span(
|
||||
|(((public, opaque), (name, parameters)), constructors), span| {
|
||||
ast::UntypedDefinition::DataType(ast::DataType {
|
||||
location: span,
|
||||
constructors: constructors
|
||||
.into_iter()
|
||||
.map(|mut constructor| {
|
||||
if constructor.sugar {
|
||||
constructor.name = name.clone();
|
||||
}
|
||||
|
||||
constructor
|
||||
})
|
||||
.collect(),
|
||||
doc: None,
|
||||
name,
|
||||
opaque: pub_opaque
|
||||
.map(|(_, opt_opaque)| opt_opaque.is_some())
|
||||
.unwrap_or(false),
|
||||
parameters: parameters.unwrap_or_default(),
|
||||
public: pub_opaque.is_some(),
|
||||
typed_parameters: vec![],
|
||||
})
|
||||
})
|
||||
constructor
|
||||
})
|
||||
.collect(),
|
||||
doc: None,
|
||||
name,
|
||||
opaque,
|
||||
parameters: parameters.unwrap_or_default(),
|
||||
public,
|
||||
typed_parameters: vec![],
|
||||
})
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
fn labeled_constructor_type_args(
|
||||
|
||||
@@ -7,8 +7,7 @@ use crate::{
|
||||
};
|
||||
|
||||
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
|
||||
utils::public()
|
||||
.or_not()
|
||||
utils::optional_flag(Token::Pub)
|
||||
.then_ignore(just(Token::Fn))
|
||||
.then(select! {Token::Name {name} => name})
|
||||
.then(
|
||||
@@ -25,7 +24,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
|
||||
.delimited_by(just(Token::LeftBrace), just(Token::RightBrace)),
|
||||
)
|
||||
.map_with_span(
|
||||
|((((opt_pub, name), (arguments, args_span)), return_annotation), body), span| {
|
||||
|((((public, name), (arguments, args_span)), return_annotation), body), span| {
|
||||
ast::UntypedDefinition::Fn(ast::Function {
|
||||
arguments,
|
||||
body: body.unwrap_or_else(|| UntypedExpr::todo(span, None)),
|
||||
@@ -39,7 +38,7 @@ pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError
|
||||
},
|
||||
end_position: span.end - 1,
|
||||
name,
|
||||
public: opt_pub.is_some(),
|
||||
public,
|
||||
return_annotation,
|
||||
return_type: (),
|
||||
can_error: true,
|
||||
|
||||
@@ -6,19 +6,18 @@ use crate::{
|
||||
};
|
||||
|
||||
pub fn parser() -> impl Parser<Token, ast::UntypedDefinition, Error = ParseError> {
|
||||
utils::public()
|
||||
.or_not()
|
||||
utils::optional_flag(Token::Pub)
|
||||
.then(utils::type_name_with_args())
|
||||
.then_ignore(just(Token::Equal))
|
||||
.then(annotation())
|
||||
.map_with_span(|((opt_pub, (alias, parameters)), annotation), span| {
|
||||
.map_with_span(|((public, (alias, parameters)), annotation), span| {
|
||||
ast::UntypedDefinition::TypeAlias(ast::TypeAlias {
|
||||
alias,
|
||||
annotation,
|
||||
doc: None,
|
||||
location: span,
|
||||
parameters: parameters.unwrap_or_default(),
|
||||
public: opt_pub.is_some(),
|
||||
public,
|
||||
tipo: (),
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user