mirror of
https://github.com/roc-lang/roc.git
synced 2025-10-03 16:44:33 +00:00
feat(parse): support capturing a str in Pattern::Underscore
This commit is contained in:
parent
ce67f28c42
commit
8df5d5c13c
5 changed files with 15 additions and 12 deletions
|
@ -197,7 +197,7 @@ pub fn canonicalize_pattern<'a>(
|
||||||
ptype => unsupported_pattern(env, ptype, region),
|
ptype => unsupported_pattern(env, ptype, region),
|
||||||
},
|
},
|
||||||
|
|
||||||
Underscore => match pattern_type {
|
Underscore(_) => match pattern_type {
|
||||||
WhenBranch | FunctionArg => Pattern::Underscore,
|
WhenBranch | FunctionArg => Pattern::Underscore,
|
||||||
ptype => unsupported_pattern(env, ptype, region),
|
ptype => unsupported_pattern(env, ptype, region),
|
||||||
},
|
},
|
||||||
|
|
|
@ -37,7 +37,7 @@ impl<'a> Formattable<'a> for Pattern<'a> {
|
||||||
| Pattern::NonBase10Literal { .. }
|
| Pattern::NonBase10Literal { .. }
|
||||||
| Pattern::FloatLiteral(_)
|
| Pattern::FloatLiteral(_)
|
||||||
| Pattern::StrLiteral(_)
|
| Pattern::StrLiteral(_)
|
||||||
| Pattern::Underscore
|
| Pattern::Underscore(_)
|
||||||
| Pattern::Malformed(_)
|
| Pattern::Malformed(_)
|
||||||
| Pattern::QualifiedIdentifier { .. } => false,
|
| Pattern::QualifiedIdentifier { .. } => false,
|
||||||
}
|
}
|
||||||
|
@ -128,7 +128,7 @@ impl<'a> Formattable<'a> for Pattern<'a> {
|
||||||
StrLiteral(literal) => {
|
StrLiteral(literal) => {
|
||||||
todo!("Format string literal: {:?}", literal);
|
todo!("Format string literal: {:?}", literal);
|
||||||
}
|
}
|
||||||
Underscore => buf.push('_'),
|
Underscore(name) => buf.push_str(name),
|
||||||
|
|
||||||
// Space
|
// Space
|
||||||
SpaceBefore(sub_pattern, spaces) => {
|
SpaceBefore(sub_pattern, spaces) => {
|
||||||
|
|
|
@ -437,7 +437,7 @@ pub enum Pattern<'a> {
|
||||||
},
|
},
|
||||||
FloatLiteral(&'a str),
|
FloatLiteral(&'a str),
|
||||||
StrLiteral(StrLiteral<'a>),
|
StrLiteral(StrLiteral<'a>),
|
||||||
Underscore,
|
Underscore(&'a str),
|
||||||
|
|
||||||
// Space
|
// Space
|
||||||
SpaceBefore(&'a Pattern<'a>, &'a [CommentOrNewline<'a>]),
|
SpaceBefore(&'a Pattern<'a>, &'a [CommentOrNewline<'a>]),
|
||||||
|
@ -554,7 +554,8 @@ impl<'a> Pattern<'a> {
|
||||||
) => string_x == string_y && base_x == base_y && is_negative_x == is_negative_y,
|
) => string_x == string_y && base_x == base_y && is_negative_x == is_negative_y,
|
||||||
(FloatLiteral(x), FloatLiteral(y)) => x == y,
|
(FloatLiteral(x), FloatLiteral(y)) => x == y,
|
||||||
(StrLiteral(x), StrLiteral(y)) => x == y,
|
(StrLiteral(x), StrLiteral(y)) => x == y,
|
||||||
(Underscore, Underscore) => true,
|
// TODO do we want to compare anything here?
|
||||||
|
(Underscore(_), Underscore(_)) => true,
|
||||||
|
|
||||||
// Space
|
// Space
|
||||||
(SpaceBefore(x, _), SpaceBefore(y, _)) => x.equivalent(y),
|
(SpaceBefore(x, _), SpaceBefore(y, _)) => x.equivalent(y),
|
||||||
|
|
|
@ -666,7 +666,7 @@ fn annotation_or_alias<'a>(
|
||||||
NumLiteral(_) | NonBase10Literal { .. } | FloatLiteral(_) | StrLiteral(_) => {
|
NumLiteral(_) | NonBase10Literal { .. } | FloatLiteral(_) | StrLiteral(_) => {
|
||||||
Def::NotYetImplemented("TODO gracefully handle trying to annotate a litera")
|
Def::NotYetImplemented("TODO gracefully handle trying to annotate a litera")
|
||||||
}
|
}
|
||||||
Underscore => {
|
Underscore(_) => {
|
||||||
Def::NotYetImplemented("TODO gracefully handle trying to give a type annotation to an undrscore")
|
Def::NotYetImplemented("TODO gracefully handle trying to give a type annotation to an undrscore")
|
||||||
}
|
}
|
||||||
Malformed(_) => {
|
Malformed(_) => {
|
||||||
|
@ -1086,7 +1086,9 @@ fn string_pattern<'a>() -> impl Parser<'a, Pattern<'a>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn underscore_pattern<'a>() -> impl Parser<'a, Pattern<'a>> {
|
fn underscore_pattern<'a>() -> impl Parser<'a, Pattern<'a>> {
|
||||||
map!(ascii_char(b'_'), |_| Pattern::Underscore)
|
map_with_arena!(ascii_char(b'_'), |_arena, _thing| {
|
||||||
|
Pattern::Underscore(&"_")
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn record_destructure<'a>(min_indent: u16) -> impl Parser<'a, Pattern<'a>> {
|
fn record_destructure<'a>(min_indent: u16) -> impl Parser<'a, Pattern<'a>> {
|
||||||
|
|
|
@ -1414,7 +1414,7 @@ mod test_parse {
|
||||||
#[test]
|
#[test]
|
||||||
fn single_underscore_closure() {
|
fn single_underscore_closure() {
|
||||||
let arena = Bump::new();
|
let arena = Bump::new();
|
||||||
let pattern = Located::new(0, 0, 1, 2, Underscore);
|
let pattern = Located::new(0, 0, 1, 2, Underscore(&"_"));
|
||||||
let patterns = &[pattern];
|
let patterns = &[pattern];
|
||||||
let expected = Closure(patterns, arena.alloc(Located::new(0, 0, 6, 8, Num("42"))));
|
let expected = Closure(patterns, arena.alloc(Located::new(0, 0, 6, 8, Num("42"))));
|
||||||
let actual = parse_expr_with(&arena, "\\_ -> 42");
|
let actual = parse_expr_with(&arena, "\\_ -> 42");
|
||||||
|
@ -1464,8 +1464,8 @@ mod test_parse {
|
||||||
#[test]
|
#[test]
|
||||||
fn closure_with_underscores() {
|
fn closure_with_underscores() {
|
||||||
let arena = Bump::new();
|
let arena = Bump::new();
|
||||||
let underscore1 = Located::new(0, 0, 1, 2, Underscore);
|
let underscore1 = Located::new(0, 0, 1, 2, Underscore(&"_"));
|
||||||
let underscore2 = Located::new(0, 0, 4, 5, Underscore);
|
let underscore2 = Located::new(0, 0, 4, 5, Underscore(&"_"));
|
||||||
let patterns = bumpalo::vec![in &arena; underscore1, underscore2];
|
let patterns = bumpalo::vec![in &arena; underscore1, underscore2];
|
||||||
let expected = Closure(
|
let expected = Closure(
|
||||||
arena.alloc(patterns),
|
arena.alloc(patterns),
|
||||||
|
@ -2477,7 +2477,7 @@ mod test_parse {
|
||||||
guard: None,
|
guard: None,
|
||||||
});
|
});
|
||||||
let newlines = &[Newline];
|
let newlines = &[Newline];
|
||||||
let pattern2 = Pattern::SpaceBefore(arena.alloc(Underscore), newlines);
|
let pattern2 = Pattern::SpaceBefore(arena.alloc(Underscore(&"_")), newlines);
|
||||||
let loc_pattern2 = Located::new(2, 2, 4, 5, pattern2);
|
let loc_pattern2 = Located::new(2, 2, 4, 5, pattern2);
|
||||||
let expr2 = Num("4");
|
let expr2 = Num("4");
|
||||||
let loc_expr2 = Located::new(2, 2, 9, 10, expr2);
|
let loc_expr2 = Located::new(2, 2, 9, 10, expr2);
|
||||||
|
@ -2522,7 +2522,7 @@ mod test_parse {
|
||||||
guard: None,
|
guard: None,
|
||||||
});
|
});
|
||||||
let newlines = &[Newline];
|
let newlines = &[Newline];
|
||||||
let pattern2 = Pattern::SpaceBefore(arena.alloc(Underscore), newlines);
|
let pattern2 = Pattern::SpaceBefore(arena.alloc(Underscore(&"_")), newlines);
|
||||||
let loc_pattern2 = Located::new(2, 2, 4, 5, pattern2);
|
let loc_pattern2 = Located::new(2, 2, 4, 5, pattern2);
|
||||||
let expr2 = Num("4");
|
let expr2 = Num("4");
|
||||||
let loc_expr2 = Located::new(2, 2, 9, 10, expr2);
|
let loc_expr2 = Located::new(2, 2, 9, 10, expr2);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue