mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 22:34:45 +00:00
simplify expr in parens
This commit is contained in:
parent
e49860c1cd
commit
35e7f01362
1 changed files with 37 additions and 34 deletions
|
@ -93,41 +93,44 @@ fn loc_expr_in_parens_help_help<'a>(
|
||||||
fn loc_expr_in_parens_etc_help<'a>(
|
fn loc_expr_in_parens_etc_help<'a>(
|
||||||
min_indent: u16,
|
min_indent: u16,
|
||||||
) -> impl Parser<'a, Located<Expr<'a>>, EExpr<'a>> {
|
) -> impl Parser<'a, Located<Expr<'a>>, EExpr<'a>> {
|
||||||
then(
|
move |arena, state: State<'a>| {
|
||||||
loc!(and!(
|
let parser = loc!(and!(
|
||||||
specialize(EExpr::InParens, loc_expr_in_parens_help(min_indent)),
|
specialize(EExpr::InParens, loc_expr_in_parens_help(min_indent)),
|
||||||
and!(
|
one_of![record_field_access_chain(), |a, s| Ok((
|
||||||
one_of![record_field_access_chain(), |a, s| Ok((
|
NoProgress,
|
||||||
NoProgress,
|
Vec::new_in(a),
|
||||||
Vec::new_in(a),
|
s
|
||||||
s
|
))]
|
||||||
))],
|
));
|
||||||
// TODO remove the either
|
|
||||||
optional(
|
let (
|
||||||
// There may optionally be function args after the ')'
|
_,
|
||||||
// e.g. ((foo bar) baz)
|
Located {
|
||||||
// loc_function_args_help(min_indent),
|
mut region,
|
||||||
// If there aren't any args, there may be a '=' or ':' after it.
|
value: (loc_expr, field_accesses),
|
||||||
//
|
},
|
||||||
// (It's a syntax error to write e.g. `foo bar =` - so if there
|
state,
|
||||||
// were any args, there is definitely no need to parse '=' or ':'!)
|
) = parser.parse(arena, state)?;
|
||||||
//
|
|
||||||
// Also, there may be a '.' for field access (e.g. `(foo).bar`),
|
let mut value = loc_expr.value;
|
||||||
// but we only want to look for that if there weren't any args,
|
|
||||||
// as if there were any args they'd have consumed it anyway
|
// if there are field accesses, include the parentheses in the region
|
||||||
// e.g. in `((foo bar) baz.blah)` the `.blah` will be consumed by the `baz` parser
|
// otherwise, don't include the parentheses
|
||||||
map!(
|
if field_accesses.is_empty() {
|
||||||
and!(
|
region = loc_expr.region;
|
||||||
space0_e(min_indent, EExpr::Space, EExpr::IndentEquals),
|
} else {
|
||||||
equals_with_indent_help()
|
for field in field_accesses {
|
||||||
),
|
// Wrap the previous answer in the new one, so we end up
|
||||||
Either::Second
|
// with a nested Expr. That way, `foo.bar.baz` gets represented
|
||||||
)
|
// in the AST as if it had been written (foo.bar).baz all along.
|
||||||
)
|
value = Expr::Access(arena.alloc(value), field);
|
||||||
)
|
}
|
||||||
)),
|
}
|
||||||
move |arena, state, _progress, parsed| helper_help(arena, state, parsed, min_indent),
|
|
||||||
)
|
let loc_expr = Located::at(region, value);
|
||||||
|
|
||||||
|
Ok((MadeProgress, loc_expr, state))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn record_field_access_chain<'a>() -> impl Parser<'a, Vec<'a, &'a str>, EExpr<'a>> {
|
fn record_field_access_chain<'a>() -> impl Parser<'a, Vec<'a, &'a str>, EExpr<'a>> {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue