Memoize token nature computation

This commit is contained in:
Florin Iucha 2017-10-14 19:27:47 -04:00 committed by PKEuS
parent 4700b75ded
commit cccf035535
2 changed files with 44 additions and 34 deletions

View File

@ -69,32 +69,32 @@ void Token::update_property_info()
{ {
if (!_str.empty()) { if (!_str.empty()) {
if (_str == "true" || _str == "false") if (_str == "true" || _str == "false")
_tokType = eBoolean; tokType(eBoolean);
else if (std::isalpha((unsigned char)_str[0]) || _str[0] == '_' || _str[0] == '$') { // Name else if (std::isalpha((unsigned char)_str[0]) || _str[0] == '_' || _str[0] == '$') { // Name
if (_varId) if (_varId)
_tokType = eVariable; tokType(eVariable);
else if (_tokType != eVariable && _tokType != eFunction && _tokType != eType && _tokType != eKeyword) else if (_tokType != eVariable && _tokType != eFunction && _tokType != eType && _tokType != eKeyword)
_tokType = eName; tokType(eName);
} else if (std::isdigit((unsigned char)_str[0]) || (_str.length() > 1 && _str[0] == '-' && std::isdigit((unsigned char)_str[1]))) } else if (std::isdigit((unsigned char)_str[0]) || (_str.length() > 1 && _str[0] == '-' && std::isdigit((unsigned char)_str[1])))
_tokType = eNumber; tokType(eNumber);
else if (_str.length() > 1 && _str[0] == '"' && endsWith(_str,'"')) else if (_str.length() > 1 && _str[0] == '"' && endsWith(_str,'"'))
_tokType = eString; tokType(eString);
else if (_str.length() > 1 && _str[0] == '\'' && endsWith(_str,'\'')) else if (_str.length() > 1 && _str[0] == '\'' && endsWith(_str,'\''))
_tokType = eChar; tokType(eChar);
else if (_str == "=" || _str == "<<=" || _str == ">>=" || else if (_str == "=" || _str == "<<=" || _str == ">>=" ||
(_str.size() == 2U && _str[1] == '=' && std::strchr("+-*/%&^|", _str[0]))) (_str.size() == 2U && _str[1] == '=' && std::strchr("+-*/%&^|", _str[0])))
_tokType = eAssignmentOp; tokType(eAssignmentOp);
else if (_str.size() == 1 && _str.find_first_of(",[]()?:") != std::string::npos) else if (_str.size() == 1 && _str.find_first_of(",[]()?:") != std::string::npos)
_tokType = eExtendedOp; tokType(eExtendedOp);
else if (_str=="<<" || _str==">>" || (_str.size()==1 && _str.find_first_of("+-*/%") != std::string::npos)) else if (_str=="<<" || _str==">>" || (_str.size()==1 && _str.find_first_of("+-*/%") != std::string::npos))
_tokType = eArithmeticalOp; tokType(eArithmeticalOp);
else if (_str.size() == 1 && _str.find_first_of("&|^~") != std::string::npos) else if (_str.size() == 1 && _str.find_first_of("&|^~") != std::string::npos)
_tokType = eBitOp; tokType(eBitOp);
else if (_str.size() <= 2 && else if (_str.size() <= 2 &&
(_str == "&&" || (_str == "&&" ||
_str == "||" || _str == "||" ||
_str == "!")) _str == "!"))
_tokType = eLogicalOp; tokType(eLogicalOp);
else if (_str.size() <= 2 && !_link && else if (_str.size() <= 2 && !_link &&
(_str == "==" || (_str == "==" ||
_str == "!=" || _str == "!=" ||
@ -102,17 +102,17 @@ void Token::update_property_info()
_str == "<=" || _str == "<=" ||
_str == ">" || _str == ">" ||
_str == ">=")) _str == ">="))
_tokType = eComparisonOp; tokType(eComparisonOp);
else if (_str.size() == 2 && else if (_str.size() == 2 &&
(_str == "++" || (_str == "++" ||
_str == "--")) _str == "--"))
_tokType = eIncDecOp; tokType(eIncDecOp);
else if (_str.size() == 1 && (_str.find_first_of("{}") != std::string::npos || (_link && _str.find_first_of("<>") != std::string::npos))) else if (_str.size() == 1 && (_str.find_first_of("{}") != std::string::npos || (_link && _str.find_first_of("<>") != std::string::npos)))
_tokType = eBracket; tokType(eBracket);
else else
_tokType = eOther; tokType(eOther);
} else { } else {
_tokType = eNone; tokType(eNone);
} }
update_property_isStandardType(); update_property_isStandardType();
@ -140,7 +140,7 @@ void Token::update_property_isStandardType()
if (stdTypes.find(_str)!=stdTypes.end()) { if (stdTypes.find(_str)!=stdTypes.end()) {
isStandardType(true); isStandardType(true);
_tokType = eType; tokType(eType);
} }
} }
@ -232,7 +232,7 @@ void Token::swapWithNext()
void Token::takeData(Token *fromToken) void Token::takeData(Token *fromToken)
{ {
_str = fromToken->_str; _str = fromToken->_str;
_tokType = fromToken->_tokType; tokType(fromToken->_tokType);
_flags = fromToken->_flags; _flags = fromToken->_flags;
_varId = fromToken->_varId; _varId = fromToken->_varId;
_fileIndex = fromToken->_fileIndex; _fileIndex = fromToken->_fileIndex;
@ -1627,9 +1627,9 @@ void Token::type(const ::Type *t)
{ {
_type = t; _type = t;
if (t) { if (t) {
_tokType = eType; tokType(eType);
isEnumType(_type->isEnumType()); isEnumType(_type->isEnumType());
} else if (_tokType == eType) } else if (_tokType == eType)
_tokType = eName; tokType(eName);
} }

View File

@ -238,24 +238,31 @@ public:
} }
void tokType(Token::Type t) { void tokType(Token::Type t) {
_tokType = t; _tokType = t;
bool memoizedIsName = (_tokType == eName || _tokType == eType || _tokType == eVariable ||
_tokType == eFunction || _tokType == eKeyword || _tokType == eBoolean ||
_tokType == eEnumerator); // TODO: "true"/"false" aren't really a name...
setFlag(fIsName, memoizedIsName);
bool memoizedIsLiteral = (_tokType == eNumber || _tokType == eString || _tokType == eChar ||
_tokType == eBoolean || _tokType == eLiteral || _tokType == eEnumerator);
setFlag(fIsLiteral, memoizedIsLiteral);
} }
void isKeyword(bool kwd) { void isKeyword(bool kwd) {
if (kwd) if (kwd)
_tokType = eKeyword; tokType(eKeyword);
else if (_tokType == eKeyword) else if (_tokType == eKeyword)
_tokType = eName; tokType(eName);
} }
bool isKeyword() const { bool isKeyword() const {
return _tokType == eKeyword; return _tokType == eKeyword;
} }
bool isName() const { bool isName() const {
return _tokType == eName || _tokType == eType || _tokType == eVariable || _tokType == eFunction || _tokType == eKeyword || return getFlag(fIsName);
_tokType == eBoolean || _tokType == eEnumerator; // TODO: "true"/"false" aren't really a name...
} }
bool isUpperCaseName() const; bool isUpperCaseName() const;
bool isLiteral() const { bool isLiteral() const {
return _tokType == eNumber || _tokType == eString || _tokType == eChar || return getFlag(fIsLiteral);
_tokType == eBoolean || _tokType == eLiteral || _tokType == eEnumerator;
} }
bool isNumber() const { bool isNumber() const {
return _tokType == eNumber; return _tokType == eNumber;
@ -502,7 +509,7 @@ public:
void varId(unsigned int id) { void varId(unsigned int id) {
_varId = id; _varId = id;
if (id != 0) { if (id != 0) {
_tokType = eVariable; tokType(eVariable);
isStandardType(false); isStandardType(false);
} else { } else {
update_property_info(); update_property_info();
@ -616,9 +623,9 @@ public:
void function(const Function *f) { void function(const Function *f) {
_function = f; _function = f;
if (f) if (f)
_tokType = eFunction; tokType(eFunction);
else if (_tokType == eFunction) else if (_tokType == eFunction)
_tokType = eName; tokType(eName);
} }
/** /**
@ -635,9 +642,9 @@ public:
void variable(const Variable *v) { void variable(const Variable *v) {
_variable = v; _variable = v;
if (v || _varId) if (v || _varId)
_tokType = eVariable; tokType(eVariable);
else if (_tokType == eVariable) else if (_tokType == eVariable)
_tokType = eName; tokType(eName);
} }
/** /**
@ -674,9 +681,9 @@ public:
void enumerator(const Enumerator *e) { void enumerator(const Enumerator *e) {
_enumerator = e; _enumerator = e;
if (e) if (e)
_tokType = eEnumerator; tokType(eEnumerator);
else if (_tokType == eEnumerator) else if (_tokType == eEnumerator)
_tokType = eName; tokType(eName);
} }
/** /**
@ -884,7 +891,10 @@ private:
fIsAttributePacked = (1 << 15), // __attribute__((packed)) fIsAttributePacked = (1 << 15), // __attribute__((packed))
fIsOperatorKeyword = (1 << 16), // operator=, etc fIsOperatorKeyword = (1 << 16), // operator=, etc
fIsComplex = (1 << 17), // complex/_Complex type fIsComplex = (1 << 17), // complex/_Complex type
fIsEnumType = (1 << 18) // enumeration type fIsEnumType = (1 << 18), // enumeration type
fIsName = (1 << 19),
fIsLiteral = (1 << 20),
}; };
unsigned int _flags; unsigned int _flags;