diff options
author | John Hodge <tpg@mutabah.net> | 2016-08-17 22:14:25 +0800 |
---|---|---|
committer | John Hodge <tpg@mutabah.net> | 2016-08-17 22:14:25 +0800 |
commit | 1c8db13e4a9d1d3aabe1333197e4e07301fb8bf4 (patch) | |
tree | 2c0f52bc67f43b5f6baab4d5c3bd00f81f1fa484 /src/parse | |
parent | 83dbbbf519677dac24a6eac3c2ef2c95894db501 (diff) | |
download | mrust-1c8db13e4a9d1d3aabe1333197e4e07301fb8bf4.tar.gz |
Parse - Fix many parser bugs
Diffstat (limited to 'src/parse')
-rw-r--r-- | src/parse/common.hpp | 2 | ||||
-rw-r--r-- | src/parse/expr.cpp | 2 | ||||
-rw-r--r-- | src/parse/lex.cpp | 35 | ||||
-rw-r--r-- | src/parse/paths.cpp | 10 | ||||
-rw-r--r-- | src/parse/pattern.cpp | 18 | ||||
-rw-r--r-- | src/parse/types.cpp | 12 |
6 files changed, 52 insertions, 27 deletions
diff --git a/src/parse/common.hpp b/src/parse/common.hpp index bf1c7448..484b3a67 100644 --- a/src/parse/common.hpp +++ b/src/parse/common.hpp @@ -41,7 +41,7 @@ extern AST::PathParams Parse_Path_GenericList(TokenStream& lex); extern AST::MetaItem Parse_MetaItem(TokenStream& lex);
extern ::AST::MacroInvocation Parse_MacroInvocation(ProtoSpan ps, ::AST::MetaItems meta_items, ::std::string name, TokenStream& lex);
-extern TypeRef Parse_Type(TokenStream& lex, bool allow_trait_list = false);
+extern TypeRef Parse_Type(TokenStream& lex, bool allow_trait_list = true);
extern AST::Pattern Parse_Pattern(TokenStream& lex, bool is_refutable);
extern void Parse_Impl_Item(TokenStream& lex, AST::Impl& impl);
diff --git a/src/parse/expr.cpp b/src/parse/expr.cpp index fbc43dcf..44f0b35d 100644 --- a/src/parse/expr.cpp +++ b/src/parse/expr.cpp @@ -966,7 +966,7 @@ ExprNodeP Parse_ExprVal(TokenStream& lex) // UFCS
case TOK_DOUBLE_LT:
- PUTBACK(tok, lex);
+ PUTBACK(TOK_LT, lex);
case TOK_LT: {
TypeRef ty = Parse_Type(lex);
if( GET_TOK(tok, lex) == TOK_RWORD_AS ) {
diff --git a/src/parse/lex.cpp b/src/parse/lex.cpp index 2b84d0e4..c3bfa5e5 100644 --- a/src/parse/lex.cpp +++ b/src/parse/lex.cpp @@ -20,8 +20,8 @@ #include <typeinfo> #include <algorithm> // std::count -const bool DEBUG_PRINT_TOKENS = false; -//const bool DEBUG_PRINT_TOKENS = true; +//const bool DEBUG_PRINT_TOKENS = false; +const bool DEBUG_PRINT_TOKENS = true; Lexer::Lexer(const ::std::string& filename): m_path(filename.c_str()), @@ -207,7 +207,12 @@ signed int Lexer::getSymbol() while( chars[ofs] && chars[ofs] == ch ) { - ch = this->getc(); + try { + ch = this->getc(); + } + catch(Lexer::EndOfFile) { + ch = 0; + } ofs ++; } if( chars[ofs] == 0 ) @@ -322,7 +327,9 @@ Token Lexer::getTokenInt() DEC, HEX, } num_mode = DEC; - // TODO: handle integers/floats + + + // Handle integers/floats uint64_t val = 0; if( ch == '0' ) { // Octal/hex handling @@ -399,12 +406,24 @@ Token Lexer::getTokenInt() } return Token(val, CORETYPE_ANY); } - // Single dot + // Single dot - Still a float. (TODO) else if( !isdigit(ch) ) { this->ungetc(); - this->m_next_token = Token(TOK_DOT); - return Token(val, CORETYPE_ANY); + if( issym(ch) ) + { + this->m_next_token = Token(TOK_DOT); + return Token(val, CORETYPE_ANY); + } + else + { + double fval = static_cast<double>(val); + return Token(fval, CORETYPE_ANY); + } + } + else + { + // Digit, continue } } @@ -778,6 +797,8 @@ double Lexer::parseFloat(uint64_t whole) this->ungetc(); buf[ofs] = 0; + DEBUG("buf = " << buf << ", ch = '" << ch << "'"); + return ::std::strtod(buf, NULL); } diff --git a/src/parse/paths.cpp b/src/parse/paths.cpp index 6b84ec0b..ecfdf7dd 100644 --- a/src/parse/paths.cpp +++ b/src/parse/paths.cpp @@ -16,6 +16,8 @@ AST::PathParams Parse_Path_GenericList(TokenStream& lex); AST::Path Parse_Path(TokenStream& lex, eParsePathGenericMode generic_mode) { + TRACE_FUNCTION_F("generic_mode="<<generic_mode); + Token tok; switch( GET_TOK(tok, lex) ) { @@ -43,7 +45,7 @@ AST::Path Parse_Path(TokenStream& lex, eParsePathGenericMode generic_mode) case TOK_DOUBLE_LT: lex.putback( Token(TOK_LT) ); case TOK_LT: { - TypeRef ty = Parse_Type(lex); + TypeRef ty = Parse_Type(lex, false); // Don't allow un-parenthesied trait objects if( GET_TOK(tok, lex) == TOK_RWORD_AS ) { ::AST::Path trait; if( GET_TOK(tok, lex) == TOK_DOUBLE_COLON ) { @@ -100,6 +102,8 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi ::std::vector<AST::PathNode> Parse_PathNodes(TokenStream& lex, eParsePathGenericMode generic_mode) { + TRACE_FUNCTION_F("generic_mode="<<generic_mode); + Token tok; ::std::vector<AST::PathNode> ret; @@ -145,7 +149,7 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi TypeRef ret_type = TypeRef( TypeRef::TagUnit(), Span(tok.get_pos()) ); if( GET_TOK(tok, lex) == TOK_THINARROW ) { - ret_type = Parse_Type(lex); + ret_type = Parse_Type(lex, false); } else { PUTBACK(tok, lex); @@ -217,7 +221,7 @@ AST::Path Parse_Path(TokenStream& lex, bool is_abs, eParsePathGenericMode generi { ::std::string name = tok.str(); GET_CHECK_TOK(tok, lex, TOK_EQUAL); - assoc_bounds.push_back( ::std::make_pair( mv$(name), Parse_Type(lex) ) ); + assoc_bounds.push_back( ::std::make_pair( mv$(name), Parse_Type(lex,false) ) ); break; } default: diff --git a/src/parse/pattern.cpp b/src/parse/pattern.cpp index 424099ff..87f08d31 100644 --- a/src/parse/pattern.cpp +++ b/src/parse/pattern.cpp @@ -206,19 +206,19 @@ AST::Pattern Parse_PatternReal1(TokenStream& lex, bool is_refutable) dt = CORETYPE_I32; return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({dt, -tok.intval()}) ); } - //else if( tok.type() == TOK_FLOAT ) - //{ - // auto dt = tok.datatype(); - // if(dt == CORETYPE_ANY) - // dt = CORETYPE_F32; - // return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({dt, reinterpret_cast<uint64_t>(-tok.floatval()), dt}) ); - //} + else if( tok.type() == TOK_FLOAT ) + { + auto dt = tok.datatype(); + if(dt == CORETYPE_ANY) + dt = CORETYPE_F32; + return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Float({dt, -tok.floatval()}) ); + } else { throw ParseError::Unexpected(lex, tok, {TOK_INTEGER, TOK_FLOAT}); } - //case TOK_FLOAT: - // return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({tok.datatype(), reinterpret_cast<uint64_t>(tok.floatval())}) ); + case TOK_FLOAT: + return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Float({tok.datatype(), tok.floatval()}) ); case TOK_INTEGER: return AST::Pattern( AST::Pattern::TagValue(), AST::Pattern::Value::make_Integer({tok.datatype(), tok.intval()}) ); case TOK_RWORD_TRUE: diff --git a/src/parse/types.cpp b/src/parse/types.cpp index 6f3a3d58..34afc484 100644 --- a/src/parse/types.cpp +++ b/src/parse/types.cpp @@ -78,7 +78,7 @@ TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list) // TODO: Handle HRLS in fn types return Parse_Type_Fn(lex, hrls); default: - return Parse_Type_Path(lex, hrls, allow_trait_list); + return Parse_Type_Path(lex, hrls, true); } } // <ident> - Either a primitive, or a path @@ -114,12 +114,12 @@ TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list) } if( tok.type() == TOK_RWORD_MUT ) { // Mutable reference - return TypeRef(TypeRef::TagReference(), lex.end_span(ps), true, Parse_Type(lex)); + return TypeRef(TypeRef::TagReference(), lex.end_span(ps), true, Parse_Type(lex, false)); } else { PUTBACK(tok, lex); // Immutable reference - return TypeRef(TypeRef::TagReference(), lex.end_span(ps), false, Parse_Type(lex)); + return TypeRef(TypeRef::TagReference(), lex.end_span(ps), false, Parse_Type(lex, false)); } throw ParseError::BugCheck("Reached end of Parse_Type:AMP"); } @@ -130,10 +130,10 @@ TypeRef Parse_Type_Int(TokenStream& lex, bool allow_trait_list) { case TOK_RWORD_MUT: // Mutable pointer - return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), true, Parse_Type(lex)); + return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), true, Parse_Type(lex, false)); case TOK_RWORD_CONST: // Immutable pointer - return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), false, Parse_Type(lex)); + return TypeRef(TypeRef::TagPointer(), lex.end_span(ps), false, Parse_Type(lex, false)); default: throw ParseError::Unexpected(lex, tok, {TOK_RWORD_CONST, TOK_RWORD_MUT}); } @@ -247,7 +247,7 @@ TypeRef Parse_Type_Fn(TokenStream& lex, ::std::vector<::std::string> hrls) TypeRef ret_type = TypeRef(TypeRef::TagUnit(), Span(tok.get_pos())); if( GET_TOK(tok, lex) == TOK_THINARROW ) { - ret_type = Parse_Type(lex); + ret_type = Parse_Type(lex, false); } else { PUTBACK(tok, lex); |