From c04e4356a1767fc97131a7ffe3c2c61eac7e3c3e Mon Sep 17 00:00:00 2001 From: greg Date: Wed, 4 Sep 2019 10:53:52 -0700 Subject: [PATCH] Changing how patterns work to support qualified names in patterns --- schala-lang/language/src/ast.rs | 7 +++--- schala-lang/language/src/parsing.rs | 33 ++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/schala-lang/language/src/ast.rs b/schala-lang/language/src/ast.rs index 290b513..bc300ff 100644 --- a/schala-lang/language/src/ast.rs +++ b/schala-lang/language/src/ast.rs @@ -230,8 +230,9 @@ pub enum Pattern { Ignored, TuplePattern(Vec), Literal(PatternLiteral), - TupleStruct(Rc, Vec), - Record(Rc, Vec<(Rc, Pattern)>), + TupleStruct(QualifiedName, Vec), + Record(QualifiedName, Vec<(Rc, Pattern)>), + VarOrName(QualifiedName), } #[derive(Debug, PartialEq, Clone)] @@ -242,8 +243,6 @@ pub enum PatternLiteral { }, StringPattern(Rc), BoolPattern(bool), - //TODO I think VarPattern also needs to know about FQSNs - VarPattern(QualifiedName) } #[derive(Debug, PartialEq, Clone)] diff --git a/schala-lang/language/src/parsing.rs b/schala-lang/language/src/parsing.rs index 3301462..c56f736 100644 --- a/schala-lang/language/src/parsing.rs +++ b/schala-lang/language/src/parsing.rs @@ -936,13 +936,15 @@ impl Parser { let qualified_name = self.qualified_name()?; match self.token_handler.peek_kind() { LCurlyBrace => { - + let members = delimited!(self, LCurlyBrace, record_pattern_entry, Comma, RCurlyBrace); + Pattern::Record(qualified_name, members) }, LParen => { - + let members = delimited!(self, LParen, pattern, Comma, RParen); + Pattern::TupleStruct(qualified_name, members) }, _ => { - + Pattern::VarPattern(qualified_name) }, } }, @@ -950,6 +952,31 @@ impl Parser { } } + #[recursive_descent_method] + fn pattern_literal(&mut self) -> ParseResult { + match self.token_handler.peek_kind() { + Keyword(Kw::True) => { + self.token_handler.next(); + Pattern::Literal(PatternLiteral::BoolPattern(true)) + }, + Keyword(Kw::False) => { + self.token_handler.next(); + Pattern::Literal(PatternLiteral::BoolPattern(false)) + }, + StrLiteral(s) => { + self.token_handler.next(); + Pattern::Literal(PatternLiteral::StringPattern(s)) + }, + DigitGroup(_) | HexLiteral(_) | BinNumberSigil | Period => self.signed_number_literal()?, + Operator(ref op) if **op == "-" => self.signed_number_literal()?, + Underscore => { + self.token_handler.next(); + Pattern::Ignored + }, + other => return ParseError::new_with_token(format!("{:?} is not a valid Pattern", other), tok) + } + } + /* #[recursive_descent_method] fn simple_pattern(&mut self) -> ParseResult {