mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-28 14:24:45 +00:00
parse underscores in expressions
This commit is contained in:
parent
e1a78645aa
commit
28ba645121
7 changed files with 87 additions and 8 deletions
|
@ -115,6 +115,8 @@ pub enum Expr<'a> {
|
|||
ident: &'a str,
|
||||
},
|
||||
|
||||
Underscore(&'a str),
|
||||
|
||||
// Tags
|
||||
GlobalTag(&'a str),
|
||||
PrivateTag(&'a str),
|
||||
|
|
|
@ -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, _) => {
|
||||
|
|
|
@ -411,6 +411,7 @@ pub enum EExpr<'a> {
|
|||
If(If<'a>, Row, Col),
|
||||
|
||||
Lambda(ELambda<'a>, Row, Col),
|
||||
Underscore(Row, Col),
|
||||
|
||||
InParens(EInParens<'a>, Row, Col),
|
||||
Record(ERecord<'a>, Row, Col),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue