fix: comments in record patterns closes #946

This commit is contained in:
rvcas
2024-05-22 12:26:32 -04:00
parent c16bd06e97
commit 7f38b55c1c
25 changed files with 172 additions and 100 deletions

View File

@@ -1,7 +1,7 @@
use chumsky::prelude::*;
use crate::{
ast::{CallArg, UntypedPattern},
ast::{CallArg, Span, UntypedPattern},
parser::{error::ParseError, token::Token},
};
@@ -10,23 +10,26 @@ pub fn parser(
) -> impl Parser<Token, UntypedPattern, Error = ParseError> + '_ {
select! {Token::UpName { name } => name}
.then(args(expression))
.map_with_span(|(name, (arguments, with_spread, is_record)), location| {
UntypedPattern::Constructor {
is_record,
location,
name,
arguments,
module: None,
constructor: (),
with_spread,
tipo: (),
}
})
.map_with_span(
|(name, (arguments, spread_location, is_record)), location| {
UntypedPattern::Constructor {
is_record,
location,
name,
arguments,
module: None,
constructor: (),
spread_location,
tipo: (),
}
},
)
}
pub(crate) fn args(
expression: Recursive<'_, Token, UntypedPattern, ParseError>,
) -> impl Parser<Token, (Vec<CallArg<UntypedPattern>>, bool, bool), Error = ParseError> + '_ {
) -> impl Parser<Token, (Vec<CallArg<UntypedPattern>>, Option<Span>, bool), Error = ParseError> + '_
{
let record_constructor_pattern_arg_parser = choice((
select! {Token::Name {name} => name}
.then_ignore(just(Token::Colon))
@@ -49,8 +52,9 @@ pub(crate) fn args(
.allow_trailing()
.then(
just(Token::DotDot)
.then_ignore(just(Token::Comma).or_not())
.ignored()
.map_with_span(|_spread, span| span)
.then_ignore(just(Token::Comma).or_not())
.or_not(),
)
.delimited_by(just(Token::LeftBrace), just(Token::RightBrace));
@@ -66,8 +70,9 @@ pub(crate) fn args(
.allow_trailing()
.then(
just(Token::DotDot)
.then_ignore(just(Token::Comma).or_not())
.ignored()
.map_with_span(|_spread, span| span)
.then_ignore(just(Token::Comma).or_not())
.or_not(),
)
.delimited_by(just(Token::LeftParen), just(Token::RightParen));
@@ -79,8 +84,8 @@ pub(crate) fn args(
.or_not()
.map(|opt_args| {
opt_args
.map(|((a, b), c)| (a, b.is_some(), c))
.unwrap_or_else(|| (vec![], false, false))
.map(|((a, b), c)| (a, b, c))
.unwrap_or_else(|| (vec![], None, false))
})
}

View File

@@ -9,6 +9,6 @@ Constructor {
arguments: [],
module: None,
constructor: (),
with_spread: false,
spread_location: None,
tipo: (),
}

View File

@@ -11,6 +11,6 @@ Constructor {
"module",
),
constructor: (),
with_spread: false,
spread_location: None,
tipo: (),
}

View File

@@ -30,6 +30,6 @@ Constructor {
],
module: None,
constructor: (),
with_spread: false,
spread_location: None,
tipo: (),
}

View File

@@ -26,6 +26,6 @@ Constructor {
],
module: None,
constructor: (),
with_spread: false,
spread_location: None,
tipo: (),
}

View File

@@ -39,6 +39,6 @@ Constructor {
],
module: None,
constructor: (),
with_spread: false,
spread_location: None,
tipo: (),
}

View File

@@ -20,6 +20,8 @@ Constructor {
],
module: None,
constructor: (),
with_spread: true,
spread_location: Some(
9..11,
),
tipo: (),
}

View File

@@ -18,7 +18,7 @@ Pair {
arguments: [],
module: None,
constructor: (),
with_spread: false,
spread_location: None,
tipo: (),
},
}

View File

@@ -18,7 +18,7 @@ pub fn parser(
.or_not(),
)
.map_with_span(|(name, opt_pattern), span| {
if let Some((c_name, (arguments, with_spread, is_record))) = opt_pattern {
if let Some((c_name, (arguments, spread_location, is_record))) = opt_pattern {
UntypedPattern::Constructor {
is_record,
location: span,
@@ -26,7 +26,7 @@ pub fn parser(
arguments,
module: Some(name),
constructor: (),
with_spread,
spread_location,
tipo: (),
}
} else {