From 7ffd6b2bd728553c6dd23b7e624385e2ac8a8737 Mon Sep 17 00:00:00 2001 From: greg Date: Fri, 13 Mar 2020 23:04:16 -0700 Subject: [PATCH] Record pattern --- schala-lang/language/src/parser.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/schala-lang/language/src/parser.rs b/schala-lang/language/src/parser.rs index 5c5538e..e7db2fc 100644 --- a/schala-lang/language/src/parser.rs +++ b/schala-lang/language/src/parser.rs @@ -276,7 +276,23 @@ fn tuple_struct_pattern(text: &str) -> ParseResult { } fn record_pattern(text: &str) -> ParseResult { - unimplemented!() + let p = tuple(( + qualified_name, + delimited(ws(tag("{")), + separated_nonempty_list(ws(tag(",")), ws(record_pattern_entry)), //TODO support newlines? + ws(tag("}"))) + )); + map(p, |(name, members)| Pattern::Record(name, members))(text) +} + +fn record_pattern_entry(text: &str) -> ParseResult<(Rc, Pattern)> { + alt(( + map(tuple((ws(identifier), ws(tag(":")), ws(pattern))), + |(name, _, pattern)| (name, pattern)), + map(identifier, |name| + (name.clone(), Pattern::Literal(PatternLiteral::StringPattern(name.clone()))) + ) + ))(text) } fn pattern_literal(text: &str) -> ParseResult {