C++: Add parsing of Q_PRIVATE_PROPERTY.

Reviewed-by: Roberto Raggi
This commit is contained in:
Christian Kamm
2010-12-08 15:08:03 +01:00
parent 40725cfd6d
commit 16542241c9
11 changed files with 90 additions and 1 deletions

View File

@@ -1432,6 +1432,8 @@ bool Preprocessor::isQtReservedWord(const QByteArray &macroId) const
return true; return true;
else if (size == 10 && macroId.at(0) == 'Q' && macroId == "Q_PROPERTY") else if (size == 10 && macroId.at(0) == 'Q' && macroId == "Q_PROPERTY")
return true; return true;
else if (size == 18 && macroId.at(0) == 'Q' && macroId == "Q_PRIVATE_PROPERTY")
return true;
else if (size == 7 && macroId.at(0) == 'Q' && macroId == "Q_ENUMS") else if (size == 7 && macroId.at(0) == 'Q' && macroId == "Q_ENUMS")
return true; return true;
else if (size == 7 && macroId.at(0) == 'Q' && macroId == "Q_FLAGS") else if (size == 7 && macroId.at(0) == 'Q' && macroId == "Q_FLAGS")

View File

@@ -702,6 +702,7 @@ bool CodeFormatter::tryDeclaration()
switch (kind) { switch (kind) {
case T_Q_ENUMS: case T_Q_ENUMS:
case T_Q_PROPERTY: case T_Q_PROPERTY:
case T_Q_PRIVATE_PROPERTY:
case T_Q_FLAGS: case T_Q_FLAGS:
case T_Q_GADGET: case T_Q_GADGET:
case T_Q_OBJECT: case T_Q_OBJECT:

View File

@@ -3387,6 +3387,11 @@ unsigned QtPropertyDeclarationAST::firstToken() const
return property_specifier_token; return property_specifier_token;
if (lparen_token) if (lparen_token)
return lparen_token; return lparen_token;
if (expression)
if (unsigned candidate = expression->firstToken())
return candidate;
if (comma_token)
return comma_token;
if (type_id) if (type_id)
if (unsigned candidate = type_id->firstToken()) if (unsigned candidate = type_id->firstToken())
return candidate; return candidate;
@@ -3415,6 +3420,11 @@ unsigned QtPropertyDeclarationAST::lastToken() const
if (type_id) if (type_id)
if (unsigned candidate = type_id->lastToken()) if (unsigned candidate = type_id->lastToken())
return candidate; return candidate;
if (comma_token)
return comma_token + 1;
if (expression)
if (unsigned candidate = expression->lastToken())
return candidate;
if (lparen_token) if (lparen_token)
return lparen_token + 1; return lparen_token + 1;
if (property_specifier_token) if (property_specifier_token)

View File

@@ -773,6 +773,8 @@ class CPLUSPLUS_EXPORT QtPropertyDeclarationAST: public DeclarationAST
public: public:
unsigned property_specifier_token; unsigned property_specifier_token;
unsigned lparen_token; unsigned lparen_token;
ExpressionAST *expression; // for Q_PRIVATE_PROPERTY(expression, ...)
unsigned comma_token;
ExpressionAST *type_id; ExpressionAST *type_id;
NameAST *property_name; NameAST *property_name;
QtPropertyDeclarationItemListAST *property_declaration_item_list; QtPropertyDeclarationItemListAST *property_declaration_item_list;
@@ -782,6 +784,8 @@ public:
QtPropertyDeclarationAST() QtPropertyDeclarationAST()
: property_specifier_token(0) : property_specifier_token(0)
, lparen_token(0) , lparen_token(0)
, expression(0)
, comma_token(0)
, type_id(0) , type_id(0)
, property_name(0) , property_name(0)
, property_declaration_item_list(0) , property_declaration_item_list(0)

View File

@@ -198,6 +198,9 @@ QtPropertyDeclarationAST *QtPropertyDeclarationAST::clone(MemoryPool *pool) cons
QtPropertyDeclarationAST *ast = new (pool) QtPropertyDeclarationAST; QtPropertyDeclarationAST *ast = new (pool) QtPropertyDeclarationAST;
ast->property_specifier_token = property_specifier_token; ast->property_specifier_token = property_specifier_token;
ast->lparen_token = lparen_token; ast->lparen_token = lparen_token;
if (expression)
ast->expression = expression->clone(pool);
ast->comma_token = comma_token;
if (type_id) if (type_id)
ast->type_id = type_id->clone(pool); ast->type_id = type_id->clone(pool);
if (property_name) if (property_name)

View File

@@ -301,6 +301,13 @@ bool ASTMatcher::match(QtPropertyDeclarationAST *node, QtPropertyDeclarationAST
pattern->lparen_token = node->lparen_token; pattern->lparen_token = node->lparen_token;
if (! pattern->expression)
pattern->expression = node->expression;
else if (! AST::match(node->expression, pattern->expression, this))
return false;
pattern->comma_token = node->comma_token;
if (! pattern->type_id) if (! pattern->type_id)
pattern->type_id = node->type_id; pattern->type_id = node->type_id;
else if (! AST::match(node->type_id, pattern->type_id, this)) else if (! AST::match(node->type_id, pattern->type_id, this))

View File

@@ -154,6 +154,7 @@ void QtPropertyDeclarationItemAST::accept0(ASTVisitor *visitor)
void QtPropertyDeclarationAST::accept0(ASTVisitor *visitor) void QtPropertyDeclarationAST::accept0(ASTVisitor *visitor)
{ {
if (visitor->visit(this)) { if (visitor->visit(this)) {
accept(expression, visitor);
accept(type_id, visitor); accept(type_id, visitor);
accept(property_name, visitor); accept(property_name, visitor);
accept(property_declaration_item_list, visitor); accept(property_declaration_item_list, visitor);

View File

@@ -1442,6 +1442,47 @@ static inline int classify14(const char *s, bool q, bool) {
return T_IDENTIFIER; return T_IDENTIFIER;
} }
static inline int classify18(const char *s, bool q, bool) {
if (q && s[0] == 'Q') {
if (s[1] == '_') {
if (s[2] == 'P') {
if (s[3] == 'R') {
if (s[4] == 'I') {
if (s[5] == 'V') {
if (s[6] == 'A') {
if (s[7] == 'T') {
if (s[8] == 'E') {
if (s[9] == '_') {
if (s[10] == 'P') {
if (s[11] == 'R') {
if (s[12] == 'O') {
if (s[13] == 'P') {
if (s[14] == 'E') {
if (s[15] == 'R') {
if (s[16] == 'T') {
if (s[17] == 'Y') {
return T_Q_PRIVATE_PROPERTY;
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
return T_IDENTIFIER;
}
static inline int classify19(const char *s, bool q, bool) { static inline int classify19(const char *s, bool q, bool) {
if (q && s[0] == 'Q') { if (q && s[0] == 'Q') {
if (s[1] == '_') { if (s[1] == '_') {
@@ -1502,6 +1543,7 @@ int Lexer::classify(const char *s, int n, bool q, bool x) {
case 13: return classify13(s, q, x); case 13: return classify13(s, q, x);
case 14: return classify14(s, q, x); case 14: return classify14(s, q, x);
case 16: return classify16(s, q, x); case 16: return classify16(s, q, x);
case 18: return classify18(s, q, x);
case 19: return classify19(s, q, x); case 19: return classify19(s, q, x);
default: return T_IDENTIFIER; default: return T_IDENTIFIER;
} // switch } // switch

View File

@@ -286,6 +286,7 @@ void Parser::skipUntilDeclaration()
case T_Q_SIGNALS: case T_Q_SIGNALS:
case T_Q_SLOTS: case T_Q_SLOTS:
case T_Q_PROPERTY: case T_Q_PROPERTY:
case T_Q_PRIVATE_PROPERTY:
case T_Q_ENUMS: case T_Q_ENUMS:
case T_Q_FLAGS: case T_Q_FLAGS:
case T_Q_INTERFACES: case T_Q_INTERFACES:
@@ -1972,13 +1973,26 @@ bool Parser::parseAccessDeclaration(DeclarationAST *&node)
bool Parser::parseQtPropertyDeclaration(DeclarationAST *&node) bool Parser::parseQtPropertyDeclaration(DeclarationAST *&node)
{ {
DEBUG_THIS_RULE(); DEBUG_THIS_RULE();
if (LA() != T_Q_PROPERTY) const bool privateProperty = (LA() == T_Q_PRIVATE_PROPERTY);
if (LA() != T_Q_PROPERTY && !privateProperty)
return false; return false;
QtPropertyDeclarationAST *ast = new (_pool)QtPropertyDeclarationAST; QtPropertyDeclarationAST *ast = new (_pool)QtPropertyDeclarationAST;
ast->property_specifier_token = consumeToken(); ast->property_specifier_token = consumeToken();
if (LA() == T_LPAREN) { if (LA() == T_LPAREN) {
ast->lparen_token = consumeToken(); ast->lparen_token = consumeToken();
if (privateProperty) {
if (parsePostfixExpression(ast->expression)) {
match(T_COMMA, &ast->comma_token);
} else {
error(cursor(),
"expected expression before `%s'",
tok().spell());
return true;
}
}
parseTypeId(ast->type_id); parseTypeId(ast->type_id);
SimpleNameAST *property_name = new (_pool) SimpleNameAST; SimpleNameAST *property_name = new (_pool) SimpleNameAST;
@@ -2238,6 +2252,7 @@ bool Parser::parseMemberSpecification(DeclarationAST *&node, ClassSpecifierAST *
return parseAccessDeclaration(node); return parseAccessDeclaration(node);
case T_Q_PROPERTY: case T_Q_PROPERTY:
case T_Q_PRIVATE_PROPERTY:
return parseQtPropertyDeclaration(node); return parseQtPropertyDeclaration(node);
case T_Q_ENUMS: case T_Q_ENUMS:

View File

@@ -244,6 +244,7 @@ enum Kind {
T_Q_Q, T_Q_Q,
T_Q_INVOKABLE, T_Q_INVOKABLE,
T_Q_PROPERTY, T_Q_PROPERTY,
T_Q_PRIVATE_PROPERTY,
T_Q_INTERFACES, T_Q_INTERFACES,
T_Q_ENUMS, T_Q_ENUMS,
T_Q_FLAGS, T_Q_FLAGS,

View File

@@ -198,6 +198,9 @@ virtual bool visit(QtPropertyDeclarationAST *ast)
terminal(ast->property_specifier_token, ast); terminal(ast->property_specifier_token, ast);
if (ast->lparen_token) if (ast->lparen_token)
terminal(ast->lparen_token, ast); terminal(ast->lparen_token, ast);
nonterminal(ast->expression);
if (ast->comma_token)
terminal(ast->comma_token, ast);
nonterminal(ast->type_id); nonterminal(ast->type_id);
nonterminal(ast->property_name); nonterminal(ast->property_name);
for (QtPropertyDeclarationItemListAST *iter = ast->property_declaration_item_list; iter; iter = iter->next) for (QtPropertyDeclarationItemListAST *iter = ast->property_declaration_item_list; iter; iter = iter->next)