parse underscores in expressions

This commit is contained in:
Folkert 2021-04-11 21:02:31 +02:00
parent e1a78645aa
commit 28ba645121
7 changed files with 87 additions and 8 deletions

View file

@ -203,6 +203,7 @@ fn parse_loc_term<'a>(
loc!(specialize(EExpr::Str, string_literal_help())),
loc!(specialize(EExpr::Number, positive_number_literal_help())),
loc!(specialize(EExpr::Lambda, closure_help(min_indent, options))),
loc!(underscore_expression()),
loc!(record_literal_help(min_indent)),
loc!(specialize(EExpr::List, list_literal_help(min_indent))),
loc!(map_with_arena!(
@ -213,6 +214,26 @@ fn parse_loc_term<'a>(
.parse(arena, state)
}
fn underscore_expression<'a>() -> impl Parser<'a, Expr<'a>, EExpr<'a>> {
move |arena: &'a Bump, state: State<'a>| {
let (_, _, next_state) = word1(b'_', EExpr::Underscore).parse(arena, state)?;
let lowercase_ident_expr = {
let row = state.line;
let col = state.column;
specialize(move |_, _, _| EExpr::End(row, col), lowercase_ident())
};
let (_, output, final_state) = optional(lowercase_ident_expr).parse(arena, next_state)?;
match output {
Some(name) => Ok((MadeProgress, Expr::Underscore(name), final_state)),
None => Ok((MadeProgress, Expr::Underscore(&""), final_state)),
}
}
}
fn loc_possibly_negative_or_negated_term<'a>(
min_indent: u16,
options: ExprParseOptions,
@ -1316,6 +1337,7 @@ fn expr_to_pattern_help<'a>(arena: &'a Bump, expr: &Expr<'a>) -> Result<Pattern<
Ok(Pattern::QualifiedIdentifier { module_name, ident })
}
}
Expr::Underscore(opt_name) => Ok(Pattern::Underscore(opt_name)),
Expr::GlobalTag(value) => Ok(Pattern::GlobalTag(value)),
Expr::PrivateTag(value) => Ok(Pattern::PrivateTag(value)),
Expr::Apply(loc_val, loc_args, _) => {