Implement the try keyword with desugaring

This commit is contained in:
Sam Mohr 2024-11-01 17:34:11 -07:00
parent 69dd8d77f3
commit 308defac46
No known key found for this signature in database
GPG key ID: EA41D161A3C1BC99
9 changed files with 196 additions and 10 deletions

View file

@ -496,6 +496,9 @@ pub enum Expr<'a> {
// This form of debug is a desugared call to roc_dbg
LowLevelDbg(&'a (&'a str, &'a str), &'a Loc<Expr<'a>>, &'a Loc<Expr<'a>>),
/// The `try` keyword that performs early return on errors
Try,
// Application
/// To apply by name, do Apply(Var(...), ...)
/// To apply a tag by name, do Apply(Tag(...), ...)
@ -672,6 +675,7 @@ pub fn is_expr_suffixed(expr: &Expr) -> bool {
Expr::Dbg => false,
Expr::DbgStmt(a, b) => is_expr_suffixed(&a.value) || is_expr_suffixed(&b.value),
Expr::LowLevelDbg(_, a, b) => is_expr_suffixed(&a.value) || is_expr_suffixed(&b.value),
Expr::Try => false,
Expr::UnaryOp(a, _) => is_expr_suffixed(&a.value),
Expr::When(cond, branches) => {
is_expr_suffixed(&cond.value) || branches.iter().any(|x| is_when_branch_suffixed(x))
@ -1027,6 +1031,7 @@ impl<'a, 'b> RecursiveValueDefIter<'a, 'b> {
| Underscore(_)
| Crash
| Dbg
| Try
| Tag(_)
| OpaqueRef(_)
| MalformedIdent(_, _)
@ -2483,6 +2488,7 @@ impl<'a> Malformed for Expr<'a> {
Dbg => false,
DbgStmt(condition, continuation) => condition.is_malformed() || continuation.is_malformed(),
LowLevelDbg(_, condition, continuation) => condition.is_malformed() || continuation.is_malformed(),
Try => false,
Return(return_value, after_return) => return_value.is_malformed() || after_return.is_some_and(|ar| ar.is_malformed()),
Apply(func, args, _) => func.is_malformed() || args.iter().any(|arg| arg.is_malformed()),
BinOps(firsts, last) => firsts.iter().any(|(expr, _)| expr.is_malformed()) || last.is_malformed(),

View file

@ -195,6 +195,7 @@ fn loc_term_or_underscore_or_conditional<'a>(
loc(specialize_err(EExpr::Closure, closure_help(options))),
loc(crash_kw()),
loc(specialize_err(EExpr::Dbg, dbg_kw())),
loc(try_kw()),
loc(underscore_expression()),
loc(record_literal_help()),
loc(specialize_err(EExpr::List, list_literal_help())),
@ -217,6 +218,7 @@ fn loc_term_or_underscore<'a>(
)),
loc(specialize_err(EExpr::Closure, closure_help(options))),
loc(specialize_err(EExpr::Dbg, dbg_kw())),
loc(try_kw()),
loc(underscore_expression()),
loc(record_literal_help()),
loc(specialize_err(EExpr::List, list_literal_help())),
@ -235,6 +237,7 @@ fn loc_term<'a>(options: ExprParseOptions) -> impl Parser<'a, Loc<Expr<'a>>, EEx
)),
loc(specialize_err(EExpr::Closure, closure_help(options))),
loc(specialize_err(EExpr::Dbg, dbg_kw())),
loc(try_kw()),
loc(record_literal_help()),
loc(specialize_err(EExpr::List, list_literal_help())),
ident_seq(),
@ -2129,6 +2132,8 @@ fn expr_to_pattern_help<'a>(arena: &'a Bump, expr: &Expr<'a>) -> Result<Pattern<
pattern
}
Expr::Try => Pattern::Identifier { ident: "try" },
Expr::SpaceBefore(..) | Expr::SpaceAfter(..) | Expr::ParensAround(..) => unreachable!(),
Expr::Record(fields) => {
@ -2712,6 +2717,16 @@ fn dbg_kw<'a>() -> impl Parser<'a, Expr<'a>, EExpect<'a>> {
.trace("dbg_kw")
}
fn try_kw<'a>() -> impl Parser<'a, Expr<'a>, EExpr<'a>> {
(move |arena: &'a Bump, state: State<'a>, min_indent: u32| {
let (_, _, next_state) =
parser::keyword("try", EExpr::Try).parse(arena, state, min_indent)?;
Ok((MadeProgress, Expr::Try, next_state))
})
.trace("try_kw")
}
fn import<'a>() -> impl Parser<'a, ValueDef<'a>, EImport<'a>> {
skip_second(
skip_first(

View file

@ -3,7 +3,6 @@ use bumpalo::Bump;
use roc_module::called_via::{BinOp, UnaryOp};
use roc_region::all::{Loc, Position, Region};
use crate::parser::EReturn;
use crate::{
ast::{
AbilityImpls, AbilityMember, AssignedField, Collection, Defs, Expr, FullAst, Header,
@ -23,9 +22,9 @@ use crate::{
parser::{
EAbility, EClosure, EExpect, EExposes, EExpr, EHeader, EIf, EImport, EImportParams,
EImports, EInParens, EList, EPackageEntry, EPackageName, EPackages, EParams, EPattern,
EProvides, ERecord, ERequires, EString, EType, ETypeAbilityImpl, ETypeApply, ETypeInParens,
ETypeInlineAlias, ETypeRecord, ETypeTagUnion, ETypedIdent, EWhen, PInParens, PList,
PRecord, SyntaxError,
EProvides, ERecord, ERequires, EReturn, EString, EType, ETypeAbilityImpl, ETypeApply,
ETypeInParens, ETypeInlineAlias, ETypeRecord, ETypeTagUnion, ETypedIdent, EWhen, PInParens,
PList, PRecord, SyntaxError,
},
};
@ -757,6 +756,7 @@ impl<'a> Normalize<'a> for Expr<'a> {
arena.alloc(a.normalize(arena)),
arena.alloc(b.normalize(arena)),
),
Expr::Try => Expr::Try,
Expr::Return(a, b) => Expr::Return(
arena.alloc(a.normalize(arena)),
b.map(|loc_b| &*arena.alloc(loc_b.normalize(arena))),
@ -1055,6 +1055,7 @@ impl<'a> Normalize<'a> for EExpr<'a> {
}
EExpr::Underscore(_pos) => EExpr::Underscore(Position::zero()),
EExpr::Crash(_pos) => EExpr::Crash(Position::zero()),
EExpr::Try(_pos) => EExpr::Try(Position::zero()),
EExpr::InParens(inner_err, _pos) => {
EExpr::InParens(inner_err.normalize(arena), Position::zero())
}

View file

@ -343,6 +343,7 @@ pub enum EExpr<'a> {
Closure(EClosure<'a>, Position),
Underscore(Position),
Crash(Position),
Try(Position),
InParens(EInParens<'a>, Position),
Record(ERecord<'a>, Position),