Allow parsing a single newline/comment in empty list

This commit is contained in:
Joshua Warner 2021-11-02 21:56:12 -07:00
parent 35df58c18f
commit 6d4f3f4da7
5 changed files with 84 additions and 45 deletions

View file

@ -1261,53 +1261,46 @@ macro_rules! collection_trailing_sep {
#[macro_export]
macro_rules! collection_trailing_sep_e {
($opening_brace:expr, $elem:expr, $delimiter:expr, $closing_brace:expr, $min_indent:expr, $open_problem:expr, $space_problem:expr, $indent_problem:expr) => {
($opening_brace:expr, $elem:expr, $delimiter:expr, $closing_brace:expr, $min_indent:expr, $open_problem:expr, $space_problem:expr, $indent_problem:expr, $space_before:expr) => {
skip_first!(
$opening_brace,
skip_first!(
// We specifically allow space characters inside here, so that
// `[ ]` can be successfully parsed as an empty list, and then
// changed by the formatter back into `[]`.
//
// We don't allow newlines or comments in the middle of empty
// roc_collections because those are normally stored in an Expr,
// and there's no Expr in which to store them in an empty collection!
//
// We could change the AST to add extra storage specifically to
// support empty literals containing newlines or comments, but this
// does not seem worth even the tiniest regression in compiler performance.
zero_or_more!($crate::parser::word1(b' ', |row, col| $space_problem(
crate::parser::BadInputError::LineTooLong,
row,
col
))),
|arena, state| {
let (_, elements, state) =
and!(
$crate::parser::trailing_sep_by0(
$delimiter,
$crate::blankspace::space0_before_optional_after(
$elem,
$min_indent,
$space_problem,
$indent_problem,
$indent_problem
)
),
$crate::blankspace::space0_e($min_indent, $space_problem, $indent_problem)
).parse(arena, state)?;
|arena, state| {
let (_, spaces, state) = space0_e($min_indent, $space_problem, $indent_problem)
.parse(arena, state)?;
let (_,_, state) =
if elements.0.is_empty() {
one_of_with_error![$open_problem; $closing_brace].parse(arena, state)?
} else {
$closing_brace.parse(arena, state)?
};
let (_, (mut parsed_elems, mut final_comments), state) =
and!(
$crate::parser::trailing_sep_by0(
$delimiter,
$crate::blankspace::space0_before_optional_after(
$elem,
$min_indent,
$space_problem,
$indent_problem,
$indent_problem
)
),
$crate::blankspace::space0_e($min_indent, $space_problem, $indent_problem)
).parse(arena, state)?;
let (_,_, state) =
if parsed_elems.is_empty() {
one_of_with_error![$open_problem; $closing_brace].parse(arena, state)?
} else {
$closing_brace.parse(arena, state)?
};
Ok((MadeProgress, elements, state))
if !spaces.is_empty() {
if let Some(first) = parsed_elems.first_mut() {
first.value = $space_before(arena.alloc(first.value), spaces)
} else {
assert!(final_comments.is_empty());
final_comments = spaces;
}
}
)
Ok((MadeProgress, (parsed_elems, final_comments), state))
}
)
};
}