diff options
Diffstat (limited to 'libsolidity/parsing')
-rw-r--r-- | libsolidity/parsing/Token.cpp | 30 | ||||
-rw-r--r-- | libsolidity/parsing/Token.h | 2 |
2 files changed, 18 insertions, 14 deletions
diff --git a/libsolidity/parsing/Token.cpp b/libsolidity/parsing/Token.cpp index 693ed420..78a90559 100644 --- a/libsolidity/parsing/Token.cpp +++ b/libsolidity/parsing/Token.cpp @@ -53,7 +53,6 @@ namespace solidity void ElementaryTypeNameToken::assertDetails(Token::Value _baseType, unsigned const& _first, unsigned const& _second) { solAssert(Token::isElementaryTypeName(_baseType), ""); - string tokenString = Token::toString(_baseType); if (_baseType == Token::BytesM) { solAssert(_second == 0, "There should not be a second size argument to type bytesM."); @@ -61,10 +60,10 @@ void ElementaryTypeNameToken::assertDetails(Token::Value _baseType, unsigned con } else if (_baseType == Token::UIntM || _baseType == Token::IntM) { - solAssert(_second == 0, "There should not be a second size argument to type " + tokenString + "."); + solAssert(_second == 0, "There should not be a second size argument to type " + string(Token::toString(_baseType)) + "."); solAssert( _first <= 256 && _first % 8 == 0, - "No elementary type " + tokenString + to_string(_first) + "." + "No elementary type " + string(Token::toString(_baseType)) + to_string(_first) + "." ); } m_token = _baseType; @@ -120,26 +119,30 @@ tuple<Token::Value, unsigned short, unsigned short> Token::fromIdentifierOrKeywo if (positionM != _literal.end()) { string baseType(_literal.begin(), positionM); - auto positionX = find(positionM, _literal.end(), 'x'); + auto positionX = find_if_not(positionM, _literal.end(), ::isdigit); unsigned short m = extractM(string(positionM, positionX)); - if (baseType == toString(Token::Bytes)) + Token::Value keyword = keywordByName(baseType); + if (keyword == Token::Bytes) { - if (0 < m && m <= 32) + if (0 < m && m <= 32 && positionX == _literal.end()) return make_tuple(Token::BytesM, m, 0); - return make_tuple(Token::Identifier, 0, 0); } - else if (baseType == toString(Token::UInt) || baseType == toString(Token::Int)) + else if (keyword == Token::UInt || keyword == Token::Int) { - if (0 < m && m <= 256 && m % 8 == 0) + if (0 < m && m <= 256 && m % 8 == 0 && positionX == _literal.end()) { - if (baseType == toString(Token::UInt)) + if (keyword == Token::UInt) return make_tuple(Token::UIntM, m, 0); else return make_tuple(Token::IntM, m, 0); } - return make_tuple(Token::Identifier, 0, 0); } + return make_tuple(Token::Identifier, 0, 0); } + return make_tuple(keywordByName(_literal), 0, 0); +} +Token::Value Token::keywordByName(string const& _name) +{ // The following macros are used inside TOKEN_LIST and cause non-keyword tokens to be ignored // and keywords to be put inside the keywords variable. #define KEYWORD(name, string, precedence) {string, Token::name}, @@ -147,12 +150,11 @@ tuple<Token::Value, unsigned short, unsigned short> Token::fromIdentifierOrKeywo static const map<string, Token::Value> keywords({TOKEN_LIST(TOKEN, KEYWORD)}); #undef KEYWORD #undef TOKEN - auto it = keywords.find(_literal); - return it == keywords.end() ? make_tuple(Token::Identifier, 0, 0) : make_tuple(it->second, 0, 0); + auto it = keywords.find(_name); + return it == keywords.end() ? Token::Identifier : it->second; } #undef KT #undef KK - } } diff --git a/libsolidity/parsing/Token.h b/libsolidity/parsing/Token.h index 3de204e0..a64eded5 100644 --- a/libsolidity/parsing/Token.h +++ b/libsolidity/parsing/Token.h @@ -307,6 +307,8 @@ private: // if out_of_range error is thrown, they returns 0s, therefore securing // the variable's identity as an identifier. static unsigned extractM(std::string const& _literal); + // @returns the keyword with name @a _name or Token::Identifier of no such keyword exists. + static Token::Value keywordByName(std::string const& _name); static char const* const m_name[NUM_TOKENS]; static char const* const m_string[NUM_TOKENS]; static int8_t const m_precedence[NUM_TOKENS]; |