add suffixed:u8 to Expr::Var

This commit is contained in:
Luke Boswell 2024-03-26 11:38:51 +11:00
parent a418bf4fb8
commit 0a3b9c34b3
No known key found for this signature in database
GPG key ID: F6DB3C9DB47377B0
134 changed files with 466 additions and 107 deletions

View file

@ -289,6 +289,7 @@ pub enum Expr<'a> {
Var {
module_name: &'a str, // module_name will only be filled if the original Roc code stated something like `5 + SomeModule.myVar`, module_name will be blank if it was `5 + myVar`
ident: &'a str,
suffixed: u8, // how many `!` suffixes, for example `doTheThing!!` executes a Task that returns a Task
},
Underscore(&'a str),
@ -1395,11 +1396,13 @@ impl<'a> Expr<'a> {
pub const REPL_OPAQUE_FUNCTION: Self = Expr::Var {
module_name: "",
ident: "<function>",
suffixed: 0,
};
pub const REPL_RUNTIME_CRASH: Self = Expr::Var {
module_name: "",
ident: "*",
suffixed: 0,
};
pub fn loc_ref(&'a self, region: Region) -> Loc<&'a Self> {

View file

@ -42,7 +42,7 @@ pub enum Ident<'a> {
Access {
module_name: &'a str,
parts: &'a [Accessor<'a>],
suffixed: bool,
suffixed: u8,
},
/// `.foo { foo: 42 }` or `.1 (1, 2, 3)`
AccessorFunction(Accessor<'a>),
@ -192,7 +192,7 @@ pub fn parse_ident<'a>(
match chomp_identifier_chain(arena, state.bytes(), state.pos()) {
Ok((width, ident)) => {
let state = advance_state!(state, width as usize)?;
let mut state = advance_state!(state, width as usize)?;
if let Ident::Access {
module_name, parts, ..
} = ident
@ -206,21 +206,20 @@ pub fn parse_ident<'a>(
}
}
}
}
// Parse a suffixed `!` expression
if state.bytes().starts_with(b"!") {
if let Ident::Access {
module_name, parts, ..
} = ident
{
let new_ident = Ident::Access {
module_name,
parts,
suffixed: true,
};
return Ok((MadeProgress, new_ident, state.advance(1)));
// Parse any `!` suffixes
let mut suffixed = 0u8;
while state.bytes().starts_with(b"!") {
suffixed = suffixed.saturating_add(1);
state = state.advance(1);
}
let new_ident = Ident::Access {
module_name,
parts,
suffixed,
};
return Ok((MadeProgress, new_ident, state));
}
Ok((MadeProgress, ident, state))
@ -534,7 +533,7 @@ fn chomp_identifier_chain<'a>(
let ident = Ident::Access {
module_name,
parts: parts.into_bump_slice(),
suffixed: false,
suffixed: 0,
};
Ok((chomped as u32, ident))
@ -570,7 +569,7 @@ fn chomp_identifier_chain<'a>(
let ident = Ident::Access {
module_name: "",
parts: arena.alloc([Accessor::RecordField(value)]),
suffixed: false,
suffixed: 0,
};
Ok((chomped as u32, ident))
}

View file

@ -51,7 +51,7 @@ pub fn loc_pattern_help<'a>() -> impl Parser<'a, Loc<Pattern<'a>>, EPattern<'a>>
let pattern_state = state.clone();
// Return early with the suffixed statement
if let Pattern::Stmt(_) = pattern.value {
if let Pattern::BangSuffixed(_,_) = pattern.value {
return Ok((MadeProgress, pattern, pattern_state));
}
@ -397,14 +397,14 @@ fn loc_ident_pattern_help<'a>(
parts,
suffixed,
..
} if suffixed => {
} if suffixed > 0 => {
if module_name.is_empty() && parts.len() == 1 {
if let Accessor::RecordField(var) = &parts[0] {
return Ok((
MadeProgress,
Loc {
region: loc_ident.region,
value: Pattern::Stmt(var),
value: Pattern::BangSuffixed(var, suffixed),
},
state,
));
@ -416,7 +416,7 @@ fn loc_ident_pattern_help<'a>(
MadeProgress,
Loc {
region: loc_ident.region,
value: Pattern::Stmt(arena.alloc(format!("{}.{}", module_name, var))),
value: Pattern::BangSuffixed(arena.alloc(format!("{}.{}", module_name, var)), suffixed),
},
state,
));

View file

@ -175,6 +175,7 @@ mod test_parse {
let expr = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: false,
});
bumpalo::vec![in arena;
@ -191,6 +192,7 @@ mod test_parse {
let expr = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: false,
});
bumpalo::vec![in arena;
@ -236,6 +238,7 @@ mod test_parse {
let expr = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: false,
});
bumpalo::vec![in arena;
@ -251,11 +254,13 @@ mod test_parse {
let expr1 = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: false,
});
let expr2 = arena.alloc(Var {
module_name: "",
ident: "project",
suffixed: false,
});
bumpalo::vec![in arena;
@ -276,11 +281,13 @@ mod test_parse {
let expr1 = arena.alloc(Var {
module_name: "",
ident: "name",
suffixed: false,
});
let expr2 = arena.alloc(Var {
module_name: "",
ident: "project",
suffixed: false,
});
bumpalo::vec![in arena;