feat(CppParser): C++11 attributes support

This commit is contained in:
Günter Obiltschnig
2024-02-05 21:05:12 +01:00
parent ad07839db9
commit 568b0fca8e
12 changed files with 94 additions and 30 deletions

View File

@@ -49,9 +49,11 @@ void CppToken::syntaxError(const std::string& expected, const std::string& actua
OperatorToken::OperatorToken()
{
int i = 1;
int i = OP_OPENBRACKET;
_opMap["["] = i++;
_opMap["]"] = i++;
_opMap["[["] = i++;
_opMap["]]"] = i++;
_opMap["("] = i++;
_opMap[")"] = i++;
_opMap["{"] = i++;
@@ -159,13 +161,23 @@ void OperatorToken::finish(std::istream& istr)
case ')':
case '{':
case '}':
case '[':
case ']':
case ';':
case '?':
case '~':
case ',':
break;
case '[':
if (next == '[')
{
_value += (char) istr.get();
}
break;
case ']':
if (next == ']')
{
_value += (char) istr.get();
}
break;
case '.':
if (next == '.')
{
@@ -231,7 +243,7 @@ int OperatorToken::asInteger() const
IdentifierToken::IdentifierToken()
{
int i = 1;
int i = KW_ALIGNAS;
_kwMap["alignas"] = i++;
_kwMap["alignof"] = i++;
_kwMap["and"] = i++;

View File

@@ -39,6 +39,7 @@ using Poco::NumberFormatter;
using Poco::SyntaxException;
using Poco::icompare;
using Poco::trimInPlace;
using namespace std::string_literals;
namespace Poco {
@@ -117,6 +118,8 @@ inline void Parser::append(std::string& decl, const std::string& token)
token != "," &&
token != "[" &&
token != "]" &&
token != "[[" &&
token != "]]" &&
last != '~' &&
last != ':' &&
last != '(' &&
@@ -380,7 +383,7 @@ const Token* Parser::parseClassMembers(const Token* pNext, Struct* /*pClass*/)
poco_assert (isOperator(pNext, OperatorToken::OP_OPENBRACE));
pNext = next();
while (pNext->is(Token::IDENTIFIER_TOKEN) || pNext->is(Token::KEYWORD_TOKEN) || isOperator(pNext, OperatorToken::OP_COMPL) || isOperator(pNext, OperatorToken::OP_DBL_COLON))
while (pNext->is(Token::IDENTIFIER_TOKEN) || pNext->is(Token::KEYWORD_TOKEN) || isOperator(pNext, OperatorToken::OP_COMPL) || isOperator(pNext, OperatorToken::OP_DBL_COLON) || isOperator(pNext, OperatorToken::OP_DBL_OPENBRACKET))
{
switch (pNext->asInteger())
{
@@ -575,6 +578,11 @@ const Token* Parser::parseVarFunc(const Token* pNext)
const Token* Parser::parseVarFunc(const Token* pNext, std::string& decl)
{
_pCurrentSymbol = 0;
std::string attrs;
if (isOperator(pNext, OperatorToken::OP_DBL_OPENBRACKET))
{
pNext = parseAttributes(pNext, attrs);
}
if (isKeyword(pNext, IdentifierToken::KW_EXTERN))
{
pNext = parseExtern(pNext);
@@ -596,6 +604,7 @@ const Token* Parser::parseVarFunc(const Token* pNext, std::string& decl)
if (!currentNameSpace()->lookup(name))
{
Variable* pVar = new Variable(decl, currentNameSpace());
pVar->setAttributeList(attrs);
addSymbol(pVar, static_cast<int>(_istr.getCurrentLineNumber()));
}
pNext = next();
@@ -611,7 +620,7 @@ const Token* Parser::parseVarFunc(const Token* pNext, std::string& decl)
pNext = next();
expectOperator(pNext, OperatorToken::OP_OPENPARENT, "(");
}
pNext = parseFunc(pNext, decl);
pNext = parseFunc(pNext, attrs, decl);
}
}
_pCurrentSymbol = 0;
@@ -641,7 +650,7 @@ const Token* Parser::parseExtern(const Token* pNext)
}
const Token* Parser::parseFunc(const Token* pNext, std::string& decl)
const Token* Parser::parseFunc(const Token* pNext, const std::string& attrs, std::string& decl)
{
poco_assert (isOperator(pNext, OperatorToken::OP_OPENPARENT));
@@ -651,6 +660,7 @@ const Token* Parser::parseFunc(const Token* pNext, std::string& decl)
if (name.find(':') == std::string::npos)
{
pFunc = new Function(decl, currentNameSpace());
pFunc->setAttributeList(attrs);
addSymbol(pFunc, line);
}
pNext = parseParameters(pNext, pFunc);
@@ -909,6 +919,25 @@ const Token* Parser::parseIdentifier(const Token* pNext, std::string& id)
}
const Poco::Token* Parser::parseAttributes(const Poco::Token* pNext, std::string& attrs)
{
poco_assert (isOperator(pNext, OperatorToken::OP_DBL_OPENBRACKET));
append(attrs, pNext);
pNext = next();
while (pNext && !isOperator(pNext, OperatorToken::OP_DBL_CLOSBRACKET))
{
append(attrs, pNext);
pNext = next();
}
if (pNext)
{
append(attrs, pNext);
pNext = next();
}
return pNext;
}
void Parser::addSymbol(Symbol* pSymbol, int lineNumber, bool addGST)
{
pSymbol->setLineNumber(lineNumber);

View File

@@ -59,6 +59,12 @@ void Symbol::setAccess(Access access)
}
void Symbol::setAttributeList(const std::string& attrs)
{
_attributeList = attrs;
}
void Symbol::setDocumentation(const std::string& text)
{
_documentation = text;