Add '%char%' as pattern to match in Token::Match.

All and only those single characters enclosed in "'" are accepted.
This commit is contained in:
Edoardo Prezioso 2012-11-04 16:34:32 +01:00
parent 7c370ec873
commit 87c931b74b
3 changed files with 22 additions and 0 deletions

View File

@ -621,6 +621,15 @@ bool Token::Match(const Token *tok, const char pattern[], unsigned int varid)
patternUnderstood = true;
}
break;
case 'c':
// Character (%char%)
{
if (tok->_type != eChar)
return false;
p += 6;
patternUnderstood = true;
}
break;
case 's':
// String (%str%)
{

View File

@ -123,6 +123,7 @@ public:
* - "%type%" Anything that can be a variable type, e.g. "int", but not "delete".
* - "%num%" Any numeric token, e.g. "23"
* - "%bool%" true or false
* - "%char%" Any token enclosed in '-character.
* - "%str%" Any token starting with "-character (C-string).
* - "%varid%" Match with parameter varid
* - "%or%" A bitwise-or operator '|'

View File

@ -56,6 +56,7 @@ private:
TEST_CASE(matchSingleChar);
TEST_CASE(matchNothingOrAnyNotElse);
TEST_CASE(matchType);
TEST_CASE(matchChar);
TEST_CASE(matchStr);
TEST_CASE(matchVarid);
TEST_CASE(matchNumeric);
@ -276,6 +277,17 @@ private:
ASSERT_EQUALS(false, Token::Match(noType.tokens(), "%type%"));
}
void matchChar() {
givenACodeSampleToTokenize chr1("'a'", true);
ASSERT_EQUALS(true, Token::Match(chr1.tokens(), "%char%"));
givenACodeSampleToTokenize chr2("'1'", true);
ASSERT_EQUALS(true, Token::Match(chr2.tokens(), "%char%"));
givenACodeSampleToTokenize noChr("\"10\"", true);
ASSERT_EQUALS(false, Token::Match(noChr.tokens(), "%char%"));
}
void matchStr() {
givenACodeSampleToTokenize noStr1("abc", true);
ASSERT_EQUALS(false, Token::Match(noStr1.tokens(), "%str%"));