From 2eb0ef0364591ff9ad30fcefa795bfd8eba17dea Mon Sep 17 00:00:00 2001 From: John Hodge Date: Thu, 19 Mar 2015 10:49:11 +0800 Subject: EVIL tagged union hackjob --- src/parse/expr.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'src/parse') diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index c588d3e8..e217974a 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -34,6 +34,7 @@ AST::Expr Parse_ExprBlock(TokenStream& lex) AST::Pattern Parse_PatternReal_Path(TokenStream& lex, AST::Path path); AST::Pattern Parse_PatternReal(TokenStream& lex); +AST::Pattern Parse_PatternStruct(TokenStream& lex, AST::Path path); /// Parse a pattern @@ -203,7 +204,7 @@ AST::Pattern Parse_PatternReal_Path(TokenStream& lex, AST::Path path) case TOK_PAREN_OPEN: return AST::Pattern(AST::Pattern::TagEnumVariant(), ::std::move(path), Parse_PatternList(lex)); case TOK_BRACE_OPEN: - throw ParseError::Todo(lex, "struct patterns"); + return Parse_PatternStruct(lex, ::std::move(path)); default: lex.putback(tok); return AST::Pattern(AST::Pattern::TagValue(), NEWNODE(AST::ExprNode_NamedValue, ::std::move(path))); @@ -231,6 +232,49 @@ AST::Pattern Parse_PatternReal_Path(TokenStream& lex, AST::Path path) return child_pats; } +AST::Pattern Parse_PatternStruct(TokenStream& lex, AST::Path path) +{ + TRACE_FUNCTION; + Token tok; + do { + GET_TOK(tok, lex); + DEBUG("tok = " << tok); + if( tok.type() == TOK_BRACE_CLOSE ) + break; + if( tok.type() == TOK_DOUBLE_DOT ) { + GET_TOK(tok, lex); + break; + } + + bool is_short_bind = false; + if( tok.type() == TOK_RWORD_REF ) { + is_short_bind = true; + GET_TOK(tok, lex); + } + if( tok.type() == TOK_RWORD_MUT ) { + is_short_bind = true; + GET_TOK(tok, lex); + } + + CHECK_TOK(tok, TOK_IDENT); + ::std::string field = tok.str(); + GET_TOK(tok, lex); + + AST::Pattern pat; + if( is_short_bind || tok.type() != TOK_COLON ) { + lex.putback(tok); + pat = AST::Pattern(AST::Pattern::TagBind(), field); + } + else { + CHECK_TOK(tok, TOK_COLON); + pat = Parse_Pattern(lex); + } + // TODO: Append + } while( GET_TOK(tok, lex) == TOK_COMMA ); + CHECK_TOK(tok, TOK_BRACE_CLOSE); + throw ParseError::Todo(lex, "struct patterns"); +} + ExprNodeP Parse_ExprBlockNode(TokenStream& lex); ExprNodeP Parse_ExprBlockLine(TokenStream& lex, bool *expect_end); void Parse_ExternBlock(TokenStream& lex, AST::MetaItems attrs, ::std::vector< AST::Item >& imports); -- cgit v1.2.3