diff options
Diffstat (limited to 'src/parse')
-rw-r--r-- | src/parse/eTokenType.enum.h | 1 | ||||
-rw-r--r-- | src/parse/expr.cpp | 9 | ||||
-rw-r--r-- | src/parse/lex.cpp | 8 | ||||
-rw-r--r-- | src/parse/pattern.cpp | 5 | ||||
-rw-r--r-- | src/parse/root.cpp | 73 | ||||
-rw-r--r-- | src/parse/token.cpp | 11 | ||||
-rw-r--r-- | src/parse/tokenstream.hpp | 2 |
7 files changed, 92 insertions, 17 deletions
diff --git a/src/parse/eTokenType.enum.h b/src/parse/eTokenType.enum.h index 5104142b..4408c45a 100644 --- a/src/parse/eTokenType.enum.h +++ b/src/parse/eTokenType.enum.h @@ -58,6 +58,7 @@ _(TOK_SLASH) _(TOK_DOT) _(TOK_DOUBLE_DOT) +_(TOK_DOUBLE_DOT_EQUAL) _(TOK_TRIPLE_DOT) _(TOK_EQUAL) diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index 5194e1d8..e5d7981d 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -706,7 +706,7 @@ ExprNodeP Parse_Expr1_1(TokenStream& lex) ExprNodeP left, right; // Inclusive range to a value - if( GET_TOK(tok, lex) == TOK_TRIPLE_DOT ) { + if( GET_TOK(tok, lex) == TOK_TRIPLE_DOT || (TARGETVER_1_29 && tok.type() == TOK_DOUBLE_DOT_EQUAL) ) { right = next(lex); return NEWNODE( AST::ExprNode_BinOp, AST::ExprNode_BinOp::RANGE_INC, nullptr, mv$(right) ); } @@ -746,6 +746,13 @@ LEFTASSOC(Parse_Expr1_2, Parse_Expr1_5, case TOK_TRIPLE_DOT: rv = NEWNODE( AST::ExprNode_BinOp, AST::ExprNode_BinOp::RANGE_INC, mv$(rv), next(lex) ); break; + case TOK_DOUBLE_DOT_EQUAL: + if( TARGETVER_1_29 ) + { + rv = NEWNODE( AST::ExprNode_BinOp, AST::ExprNode_BinOp::RANGE_INC, mv$(rv), next(lex) ); + break; + } + // Fall through ) // 1: Bool OR LEFTASSOC(Parse_Expr1_5, Parse_Expr2, diff --git a/src/parse/lex.cpp b/src/parse/lex.cpp index 8376eeee..0b145379 100644 --- a/src/parse/lex.cpp +++ b/src/parse/lex.cpp @@ -83,8 +83,10 @@ static const struct { TOKENT("-=", TOK_DASH_EQUAL), TOKENT("->", TOK_THINARROW), TOKENT(".", TOK_DOT), + // NOTE: These have special handling when following numbers TOKENT("..", TOK_DOUBLE_DOT), TOKENT("...",TOK_TRIPLE_DOT), + TOKENT("..=",TOK_DOUBLE_DOT_EQUAL), TOKENT("/" , TOK_SLASH), TOKENT("/*", BLOCKCOMMENT), TOKENT("//", LINECOMMENT), @@ -402,9 +404,13 @@ Token Lexer::getTokenInt() // Double/Triple Dot if( ch == '.' ) { - if( this->getc() == '.') { + ch = this->getc(); + if( ch == '.') { this->m_next_tokens.push_back(TOK_TRIPLE_DOT); } + else if( ch == '=') { + this->m_next_tokens.push_back(TOK_DOUBLE_DOT_EQUAL); + } else { this->ungetc(); this->m_next_tokens.push_back(TOK_DOUBLE_DOT); diff --git a/src/parse/pattern.cpp b/src/parse/pattern.cpp index f6d61728..239bac80 100644 --- a/src/parse/pattern.cpp +++ b/src/parse/pattern.cpp @@ -113,6 +113,7 @@ AST::Pattern Parse_Pattern(TokenStream& lex, bool is_refutable) break; // Known value `IDENT ...` case TOK_TRIPLE_DOT: + case TOK_DOUBLE_DOT_EQUAL: break; // Known binding `ident @` case TOK_AT: @@ -156,7 +157,9 @@ AST::Pattern Parse_PatternReal(TokenStream& lex, bool is_refutable) } auto ps = lex.start_span(); AST::Pattern ret = Parse_PatternReal1(lex, is_refutable); - if( GET_TOK(tok, lex) == TOK_TRIPLE_DOT ) + if( (GET_TOK(tok, lex) == TOK_TRIPLE_DOT) + || (TARGETVER_1_29 && tok.type() == TOK_DOUBLE_DOT_EQUAL) + ) { if( !ret.data().is_Value() ) throw ParseError::Generic(lex, "Using '...' with a non-value on left"); diff --git a/src/parse/root.cpp b/src/parse/root.cpp index 68ad570b..ef1fb439 100644 --- a/src/parse/root.cpp +++ b/src/parse/root.cpp @@ -18,6 +18,7 @@ #include <expand/cfg.hpp> // check_cfg - for `mod nonexistant;` #include <fstream> // Used by directory path #include "lex.hpp" // New file lexer +#include <parse/interpolated_fragment.hpp> #include <ast/expr.hpp> template<typename T> @@ -649,9 +650,7 @@ AST::Trait Parse_TraitDef(TokenStream& lex, const AST::AttributeList& meta_items // TODO: Just add these as `where Self: <foo>` (would that break typecheck?) do { if( GET_TOK(tok, lex) == TOK_LIFETIME ) { - // TODO: Need a better way of indiciating 'static than just an invalid path - ASSERT_BUG(lex.point_span(), tok.str() == "static", "TODO: Support lifetimes other than 'static in trait bounds"); - supertraits.push_back( make_spanned( Span(tok.get_pos()), Type_TraitPath{ {}, AST::Path() } ) ); + params.add_bound(::AST::GenericBound::make_TypeLifetime({ TypeRef(lex.point_span(), "Self"), ::AST::LifetimeRef(lex.get_ident(tok)) })); } else if( tok.type() == TOK_BRACE_OPEN ) { break; @@ -988,14 +987,26 @@ AST::Attribute Parse_MetaItem(TokenStream& lex) { TRACE_FUNCTION; Token tok; - GET_TOK(tok, lex); - if( tok.type() == TOK_INTERPOLATED_META ) { + if( lex.lookahead(0) == TOK_INTERPOLATED_META ) { + GET_TOK(tok, lex); return mv$(tok.frag_meta()); } auto ps = lex.start_span(); - CHECK_TOK(tok, TOK_IDENT); + GET_TOK(tok, lex); + + switch(tok.type()) + { + case TOK_IDENT: + break; + case TOK_INTEGER: + if( TARGETVER_1_29 ) + return AST::Attribute(lex.end_span(ps), "", tok.to_str()); + default: + throw ParseError::Unexpected(lex, tok, {TOK_IDENT, TOK_INTEGER}); + } + ::std::string name = mv$(tok.str()); switch(GET_TOK(tok, lex)) { @@ -1006,6 +1017,10 @@ AST::Attribute Parse_MetaItem(TokenStream& lex) return AST::Attribute(lex.end_span(ps), name, tok.str()); case TOK_INTERPOLATED_EXPR: { auto n = tok.take_frag_node(); + void Expand_BareExpr(const AST::Crate& , const AST::Module&, ::std::unique_ptr<AST::ExprNode>& n); + assert( lex.parse_state().crate ); + assert( lex.parse_state().module ); + Expand_BareExpr(*lex.parse_state().crate, *lex.parse_state().module, n); if( auto* v = dynamic_cast<::AST::ExprNode_String*>(&*n) ) { return AST::Attribute(lex.end_span(ps), name, mv$(v->m_value)); @@ -1013,9 +1028,15 @@ AST::Attribute Parse_MetaItem(TokenStream& lex) else { // - Force an error. - CHECK_TOK(tok, TOK_STRING); + throw ParseError::Unexpected(lex, Token(InterpolatedFragment(InterpolatedFragment::EXPR, n.release())), TOK_STRING); } break; } + case TOK_INTEGER: + if( TARGETVER_1_29 ) + return AST::Attribute(lex.end_span(ps), name, tok.to_str()); + case TOK_IDENT: + if( TARGETVER_1_29 ) + return AST::Attribute(lex.end_span(ps), name, tok.to_str()); default: // - Force an error. CHECK_TOK(tok, TOK_STRING); @@ -1311,8 +1332,19 @@ void Parse_Use_Set(TokenStream& lex, const ProtoSpan& ps, const AST::Path& base_ break ; } else { - CHECK_TOK(tok, TOK_IDENT); - path = base_path + AST::PathNode(tok.str(), {}); + path = ::AST::Path(base_path); + + while(1) + { + CHECK_TOK(tok, TOK_IDENT); + path += AST::PathNode(tok.str(), {}); + if( !TARGETVER_1_29 ) + break; + if( lex.lookahead(0) != TOK_DOUBLE_COLON ) + break; + GET_CHECK_TOK(tok, lex, TOK_DOUBLE_COLON); + GET_TOK(tok, lex); + } name = mv$(tok.str()); } @@ -1706,6 +1738,19 @@ bool Parse_MacroInvocation_Opt(TokenStream& lex, AST::MacroInvocation& out_inv) } return ::AST::Named< ::AST::Item> { "", mv$(impl), false }; } + // `unsafe auto trait` + case TOK_IDENT: + if( TARGETVER_1_29 && tok.str() == "auto" ) { + GET_CHECK_TOK(tok, lex, TOK_RWORD_TRAIT); + GET_CHECK_TOK(tok, lex, TOK_IDENT); + item_name = mv$(tok.str()); + auto tr = Parse_TraitDef(lex, meta_items); + tr.set_is_unsafe(); + tr.set_is_marker(); + item_data = ::AST::Item( ::std::move(tr) ); + break; + } + //goto default; default: throw ParseError::Unexpected(lex, tok, {TOK_RWORD_FN, TOK_RWORD_TRAIT, TOK_RWORD_IMPL}); } @@ -1742,6 +1787,15 @@ bool Parse_MacroInvocation_Opt(TokenStream& lex, AST::MacroInvocation& out_inv) item_name = mv$(tok.str()); item_data = ::AST::Item( Parse_Union(lex, meta_items) ); } + // `auto trait` + else if( TARGETVER_1_29 && tok.str() == "auto" ) { + GET_CHECK_TOK(tok, lex, TOK_RWORD_TRAIT); + GET_CHECK_TOK(tok, lex, TOK_IDENT); + item_name = mv$(tok.str()); + auto tr = Parse_TraitDef(lex, meta_items); + tr.set_is_marker(); + item_data = ::AST::Item( ::std::move(tr) ); + } else { throw ParseError::Unexpected(lex, tok); } @@ -1959,6 +2013,7 @@ AST::Crate Parse_Crate(::std::string mainfile) crate.root_module().m_file_info.path = mainpath; crate.root_module().m_file_info.controls_dir = true; + lex.parse_state().crate = &crate; Parse_ModRoot(lex, crate.root_module(), crate.m_attrs); return crate; diff --git a/src/parse/token.cpp b/src/parse/token.cpp index 115df135..f1471453 100644 --- a/src/parse/token.cpp +++ b/src/parse/token.cpp @@ -359,8 +359,9 @@ struct EscapedString { case TOK_SLASH: return "/"; case TOK_DOT: return "."; - case TOK_DOUBLE_DOT: return "..."; - case TOK_TRIPLE_DOT: return ".."; + case TOK_DOUBLE_DOT: return ".."; + case TOK_DOUBLE_DOT_EQUAL: return "..="; + case TOK_TRIPLE_DOT: return "..."; case TOK_EQUAL: return "="; case TOK_PLUS_EQUAL: return "+="; @@ -484,13 +485,13 @@ struct EscapedString { os << ":" << *reinterpret_cast<AST::Path*>(tok.m_data.as_Fragment()); break; case TOK_INTERPOLATED_EXPR: - os << ":" << *reinterpret_cast<AST::ExprNode*>(tok.m_data.as_Fragment()); + os << ":" << *reinterpret_cast<const AST::ExprNode*>(tok.m_data.as_Fragment()); break; case TOK_INTERPOLATED_STMT: - os << ":" << *reinterpret_cast<AST::ExprNode*>(tok.m_data.as_Fragment()); + os << ":" << *reinterpret_cast<const AST::ExprNode*>(tok.m_data.as_Fragment()); break; case TOK_INTERPOLATED_BLOCK: - os << ":" << *reinterpret_cast<AST::ExprNode*>(tok.m_data.as_Fragment()); + os << ":" << *reinterpret_cast<const AST::ExprNode*>(tok.m_data.as_Fragment()); break; case TOK_INTERPOLATED_META: os << ":" << *reinterpret_cast<AST::Attribute*>(tok.m_data.as_Fragment()); diff --git a/src/parse/tokenstream.hpp b/src/parse/tokenstream.hpp index a9d325c2..25b6a3c1 100644 --- a/src/parse/tokenstream.hpp +++ b/src/parse/tokenstream.hpp @@ -16,6 +16,7 @@ namespace AST { class Module; + class Crate; class AttributeList; } @@ -27,6 +28,7 @@ struct ParseState // A debugging hook that disables expansion of macros bool no_expand_macros = false; + const ::AST::Crate* crate = nullptr; ::AST::Module* module = nullptr; ::AST::AttributeList* parent_attrs = nullptr; |