From 993f20b4cd3cc4692817ded4f632efcff6d7b72f Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 17 Mar 2015 22:12:27 +0800 Subject: Block fn-s, parse (but no AST) for array literals --- src/parse/common.hpp | 1 + src/parse/expr.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/parse/root.cpp | 3 +++ 3 files changed, 48 insertions(+) (limited to 'src') diff --git a/src/parse/common.hpp b/src/parse/common.hpp index 9659500c..a5938591 100644 --- a/src/parse/common.hpp +++ b/src/parse/common.hpp @@ -35,6 +35,7 @@ extern void Parse_Struct(AST::Module& mod, TokenStream& lex, bool is_public, con extern AST::Impl Parse_Impl(TokenStream& lex, bool is_unsafe=false); extern AST::Function Parse_FunctionDef(TokenStream& lex, ::std::string abi, AST::MetaItems attrs, bool allow_self, bool can_be_prototype); +extern AST::Function Parse_FunctionDefWithCode(TokenStream& lex, ::std::string abi, AST::MetaItems attrs, bool allow_self); extern AST::Expr Parse_Expr(TokenStream& lex, bool const_only); extern AST::Expr Parse_ExprBlock(TokenStream& lex); diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index f2927eaf..01ecc2cb 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -163,6 +163,10 @@ AST::Pattern Parse_PatternReal1(TokenStream& lex) return AST::Pattern( ); case TOK_AMP: DEBUG("Ref"); + if( GET_TOK(tok, lex) == TOK_RWORD_MUT ) + // TODO: Actually use mutability + return AST::Pattern( AST::Pattern::TagReference(), Parse_PatternReal(lex) ); + lex.putback(tok); return AST::Pattern( AST::Pattern::TagReference(), Parse_PatternReal(lex) ); case TOK_IDENT: lex.putback(tok); @@ -287,6 +291,15 @@ ExprNodeP Parse_ExprBlockNode(TokenStream& lex) if( !local_mod.get() ) local_mod.reset( new AST::Module("") ); local_mod->add_impl(Parse_Impl(lex, false)); break; + // - 'fn' + case TOK_RWORD_FN: + if( !local_mod.get() ) local_mod.reset( new AST::Module("") ); + GET_CHECK_TOK(tok, lex, TOK_IDENT); + // - self not allowed, not prototype + local_mod->add_function( + false, tok.str(), Parse_FunctionDefWithCode(lex, "rust", ::std::move(item_attrs), false) + ); + break; default: { lex.putback(tok); bool expect_end = false; @@ -1133,6 +1146,37 @@ ExprNodeP Parse_ExprVal(TokenStream& lex) CHECK_TOK(tok, TOK_PAREN_CLOSE); return rv; } + case TOK_SQUARE_OPEN: + if( GET_TOK(tok, lex) == TOK_SQUARE_CLOSE ) + { + // Empty literal + //return NEWNODE( AST::ExprNode_Array, ::std::vector() ); + } + else + { + lex.putback(tok); + auto first = Parse_Expr0(lex); + if( GET_TOK(tok, lex) == TOK_SEMICOLON ) + { + // Repetiion + auto count = Parse_Expr0(lex); + GET_CHECK_TOK(tok, lex, TOK_SQUARE_CLOSE); + //return NEWNODE( AST::ExprNode_Array, ::std::move(first), ::std::move(count); ); + } + else + { + ::std::vector items; + items.push_back( ::std::move(first) ); + while( tok.type() == TOK_COMMA ) + { + items.push_back( Parse_Expr0(lex) ); + GET_TOK(tok, lex); + } + CHECK_TOK(tok, TOK_SQUARE_CLOSE); + //return NEWNODE( AST::ExprNode_Array, ::std::move(items) ); + } + } + throw ParseError::Todo(lex, "Array literals"); case TOK_MACRO: { TokenTree tt = Parse_TT(lex, true); diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 270b38e0..cd197292 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -1490,6 +1490,9 @@ void Parse_ModRoot_Items(TokenStream& lex, AST::Crate& crate, AST::Module& mod, // Pass "!" as 'path' to allow termination on EOF Parse_ModRoot_Items(*expanded_macro, crate, mod, modstack, "!"); } + // - Silently consume ';' after the macro + if( GET_TOK(tok, lex) != TOK_SEMICOLON ) + lex.putback(tok); break; case TOK_RWORD_USE: -- cgit v1.2.3