1
/* This file is part of KDevelop
2
Copyright (C) 2002,2003 Roberto Raggi <roberto@kdevelop.org>
4
This library is free software; you can redistribute it and/or
5
modify it under the terms of the GNU Library General Public
6
License as published by the Free Software Foundation; either
7
version 2 of the License, or (at your option) any later version.
9
This library is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
Library General Public License for more details.
14
You should have received a copy of the GNU Library General Public License
15
along with this library; see the file COPYING.LIB. If not, write to
16
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17
Boston, MA 02111-1307, USA.
28
#include <qstringlist.h>
29
#include <qasciidict.h>
36
#define ADVANCE(tk, descr) \
38
const Token& token = lex->lookAhead( 0 ); \
40
reportError( i18n("'%1' expected found '%2'").arg(descr).arg(token.text()) ); \
46
#define ADVANCE_NR(tk, descr) \
48
const Token& token = lex->lookAhead( 0 ); \
50
reportError( i18n("'%1' expected found '%2'").arg(descr).arg(token.text()) ); \
56
#define CHECK(tk, descr) \
58
const Token& token = lex->lookAhead( 0 ); \
65
#define MATCH(tk, descr) \
67
const Token& token = lex->lookAhead( 0 ); \
69
reportError( Errors::SyntaxError ); \
74
#define UPDATE_POS(node, start, end) \
77
const Token &a = lex->tokenAt(start); \
78
const Token &b = lex->tokenAt( end!=start ? end-1 : end ); \
79
a.getStartPosition( &line, &col ); \
80
(node)->setStartPosition( line, col ); \
81
b.getEndPosition( &line, &col ); \
82
(node)->setEndPosition( line, col ); \
83
if( (node)->nodeType() == NodeType_Generic ) { \
84
if ((start) == (end) || (end) == (start)+1) \
85
(node)->setSlice(lex->source(), a.position(), a.length()); \
87
(node)->setText( toString((start),(end)) ); \
91
#define AST_FROM_TOKEN(node, tk) \
92
AST::Node node = CreateNode<AST>(); \
93
UPDATE_POS( node, (tk), (tk)+1 );
104
struct ParserPrivateData
110
Parser::Parser( Driver* driver, Lexer* lexer )
111
: m_driver( driver ),
114
d = new ParserPrivateData();
126
bool Parser::reportError( const Error& err )
128
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::reportError()" << endl;
129
if( m_problems < m_maxProblems ){
132
const Token& token = lex->lookAhead( 0 );
133
lex->getTokenPosition( token, &line, &col );
135
QString s = lex->lookAhead(0).text();
136
s = s.left( 30 ).stripWhiteSpace();
140
m_driver->addProblem( m_driver->currentFileName(), Problem(err.text.arg(s), line, col) );
146
bool Parser::reportError( const QString& msg )
148
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::reportError()" << endl;
149
if( m_problems < m_maxProblems ){
152
const Token& token = lex->lookAhead( 0 );
153
lex->getTokenPosition( token, &line, &col );
155
m_driver->addProblem( m_driver->currentFileName(), Problem(msg, line, col) );
161
void Parser::syntaxError()
163
(void) reportError( Errors::SyntaxError );
166
bool Parser::skipUntil( int token )
168
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::skipUntil()" << endl;
169
while( !lex->lookAhead(0).isNull() ){
170
if( lex->lookAhead(0) == token )
179
bool Parser::skipUntilDeclaration()
181
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::skipUntilDeclaration()" << endl;
183
while( !lex->lookAhead(0).isNull() ){
185
switch( lex->lookAhead(0) ){
189
case Token_identifier:
203
case Token_namespace:
210
case Token_const: // cv
211
case Token_volatile: // cv
214
case Token_protected:
216
case Token_signals: // Qt
217
case Token_slots: // Qt
228
bool Parser::skipUntilStatement()
230
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::skipUntilStatement() -- token = " << lex->lookAhead(0).text() << endl;
232
while( !lex->lookAhead(0).isNull() ){
233
switch( lex->lookAhead(0) ){
239
case Token_identifier:
282
bool Parser::skip( int l, int r )
285
while( !lex->lookAhead(0).isNull() ){
286
int tk = lex->lookAhead( 0 );
292
else if( l != '{' && (tk == '{' || tk == '}' || tk == ';') )
304
bool Parser::skipCommaExpression( AST::Node& node )
306
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::skipCommaExpression()" << endl;
308
int start = lex->index();
311
if( !skipExpression(expr) )
314
while( lex->lookAhead(0) == ',' ){
317
if( !skipExpression(expr) ){
318
reportError( i18n("expression expected") );
323
AST::Node ast = CreateNode<AST>();
324
UPDATE_POS( ast, start, lex->index() );
330
bool Parser::skipExpression( AST::Node& node )
332
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::skipExpression()" << endl;
334
int start = lex->index();
336
while( !lex->lookAhead(0).isNull() ){
337
int tk = lex->lookAhead( 0 );
351
case Token_identifier:
353
if( lex->lookAhead( 0 ) == Token_identifier )
375
AST::Node ast = CreateNode<AST>();
376
UPDATE_POS( ast, start, lex->index() );
389
bool Parser::parseName( NameAST::Node& node )
391
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseName()" << endl;
393
GroupAST::Node winDeclSpec;
394
parseWinDeclSpec( winDeclSpec );
396
int start = lex->index();
398
NameAST::Node ast = CreateNode<NameAST>();
400
if( lex->lookAhead(0) == Token_scope ){
401
ast->setGlobal( true );
405
int idx = lex->index();
408
ClassOrNamespaceNameAST::Node n;
409
if( !parseUnqualifiedName(n) ) {
413
if( lex->lookAhead(0) == Token_scope ){
415
ast->addClassOrNamespaceName( n );
416
if( lex->lookAhead(0) == Token_template )
417
lex->nextToken(); /// skip optional template #### @todo CHECK
419
ast->setUnqualifiedName( n );
424
if( idx == lex->index() )
427
UPDATE_POS( ast, start, lex->index() );
433
bool Parser::parseTranslationUnit( TranslationUnitAST::Node& node )
435
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTranslationUnit()" << endl;
437
int start = lex->index();
440
TranslationUnitAST::Node tun = CreateNode<TranslationUnitAST>();
442
while( !lex->lookAhead(0).isNull() ){
443
DeclarationAST::Node def;
444
int startDecl = lex->index();
445
if( !parseDeclaration(def) ){
447
if( startDecl == lex->index() )
448
lex->nextToken(); // skip at least one token
449
skipUntilDeclaration();
451
node->addDeclaration( def );
454
UPDATE_POS( node, start, lex->index() );
456
// force (0,0) as start position
457
node->setStartPosition( 0, 0 );
459
return m_problems == 0;
462
bool Parser::parseDeclaration( DeclarationAST::Node& node )
464
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseDeclaration()" << endl;
466
int start = lex->index();
468
switch( lex->lookAhead(0) ){
475
return parseLinkageSpecification( node );
477
case Token_namespace:
478
return parseNamespace( node );
481
return parseUsing( node );
484
return parseTypedef( node );
487
return parseAsmDefinition( node );
491
return parseTemplateDeclaration( node );
495
// lex->setIndex( start );
497
if( objcp && parseObjcDef(node) )
500
lex->setIndex( start );
502
GroupAST::Node storageSpec;
503
parseStorageClassSpecifier( storageSpec );
506
parseCvQualify( cv );
508
TypeSpecifierAST::Node spec;
509
AST::Node declarator;
510
if( parseEnumSpecifier(spec) || parseClassSpecifier(spec) ){
511
spec->setCvQualify( cv );
514
parseCvQualify( cv2 );
515
spec->setCv2Qualify( cv2 );
517
InitDeclaratorListAST::Node declarators;
518
parseInitDeclaratorList(declarators);
521
SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
522
ast->setStorageSpecifier( storageSpec );
523
ast->setTypeSpec( spec );
524
ast->setInitDeclaratorList( declarators );
525
UPDATE_POS( ast, start, lex->index() );
531
lex->setIndex( start );
532
return parseDeclarationInternal( node );
538
bool Parser::parseLinkageSpecification( DeclarationAST::Node& node )
540
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseLinkageSpecification()" << endl;
542
int start = lex->index();
544
if( lex->lookAhead(0) != Token_extern ){
549
LinkageSpecificationAST::Node ast = CreateNode<LinkageSpecificationAST>();
551
int startExternType = lex->index();
552
if( lex->lookAhead(0) == Token_string_literal ){
554
AST::Node externType = CreateNode<AST>();
555
UPDATE_POS( externType, startExternType, lex->index() );
557
ast->setExternType( externType );
560
if( lex->lookAhead(0) == '{' ){
561
LinkageBodyAST::Node linkageBody;
562
parseLinkageBody( linkageBody );
563
ast->setLinkageBody( linkageBody );
565
DeclarationAST::Node decl;
566
if( !parseDeclaration(decl) ){
567
reportError( i18n("Declaration syntax error") );
569
ast->setDeclaration( decl );
572
UPDATE_POS( ast, start, lex->index() );
579
bool Parser::parseLinkageBody( LinkageBodyAST::Node& node )
581
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseLinkageBody()" << endl;
583
int start = lex->index();
585
if( lex->lookAhead(0) != '{' ){
590
LinkageBodyAST::Node lba = CreateNode<LinkageBodyAST>();
593
while( !lex->lookAhead(0).isNull() ){
594
int tk = lex->lookAhead( 0 );
599
DeclarationAST::Node def;
600
int startDecl = lex->index();
601
if( parseDeclaration(def) ){
602
node->addDeclaration( def );
605
if( startDecl == lex->index() )
606
lex->nextToken(); // skip at least one token
607
skipUntilDeclaration();
611
if( lex->lookAhead(0) != '}' ){
612
reportError( i18n("} expected") );
616
UPDATE_POS( node, start, lex->index() );
620
bool Parser::parseNamespace( DeclarationAST::Node& node )
622
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseNamespace()" << endl;
624
int start = lex->index();
626
if( lex->lookAhead(0) != Token_namespace ){
631
int startNamespaceName = lex->index();
632
if( lex->lookAhead(0) == Token_identifier ){
635
AST::Node namespaceName = CreateNode<AST>();
636
UPDATE_POS( namespaceName, startNamespaceName, lex->index() );
638
if ( lex->lookAhead(0) == '=' ) {
643
if( parseName(name) ){
646
NamespaceAliasAST::Node ast = CreateNode<NamespaceAliasAST>();
647
ast->setNamespaceName( namespaceName );
648
ast->setAliasName( name );
649
UPDATE_POS( ast, start, lex->index() );
653
reportError( i18n("namespace expected") );
656
} else if( lex->lookAhead(0) != '{' ){
657
reportError( i18n("{ expected") );
661
NamespaceAST::Node ast = CreateNode<NamespaceAST>();
662
ast->setNamespaceName( namespaceName );
664
LinkageBodyAST::Node linkageBody;
665
parseLinkageBody( linkageBody );
667
ast->setLinkageBody( linkageBody );
668
UPDATE_POS( ast, start, lex->index() );
674
bool Parser::parseUsing( DeclarationAST::Node& node )
676
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseUsing()" << endl;
678
int start = lex->index();
680
if( lex->lookAhead(0) != Token_using ){
685
if( lex->lookAhead(0) == Token_namespace ){
686
if( !parseUsingDirective(node) ){
689
UPDATE_POS( node, start, lex->index() );
693
UsingAST::Node ast = CreateNode<UsingAST>();
695
int startTypeName = lex->index();
696
if( lex->lookAhead(0) == Token_typename ){
698
AST::Node tn = CreateNode<AST>();
699
UPDATE_POS( tn, startTypeName, lex->index() );
700
ast->setTypeName( tn );
704
if( !parseName(name) )
707
ast->setName( name );
711
UPDATE_POS( ast, start, lex->index() );
717
bool Parser::parseUsingDirective( DeclarationAST::Node& node )
719
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseUsingDirective()" << endl;
721
int start = lex->index();
723
if( lex->lookAhead(0) != Token_namespace ){
729
if( !parseName(name) ){
730
reportError( i18n("Namespace name expected") );
736
UsingDirectiveAST::Node ast = CreateNode<UsingDirectiveAST>();
737
ast->setName( name );
738
UPDATE_POS( ast, start, lex->index() );
745
bool Parser::parseOperatorFunctionId( AST::Node& node )
747
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseOperatorFunctionId()" << endl;
749
int start = lex->index();
751
if( lex->lookAhead(0) != Token_operator ){
757
if( parseOperator(op) ){
758
AST::Node asn = CreateNode<AST>();
760
UPDATE_POS( node, start, lex->index() );
763
// parse cast operator
767
TypeSpecifierAST::Node spec;
768
if( !parseSimpleTypeSpecifier(spec) ){
772
spec->setCvQualify( cv );
776
spec->setCv2Qualify( cv2 );
779
while( parsePtrOperator(ptrOp) )
782
AST::Node asn = CreateNode<AST>();
784
UPDATE_POS( node, start, lex->index() );
789
bool Parser::parseTemplateArgumentList( TemplateArgumentListAST::Node& node, bool reportError )
791
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTemplateArgumentList()" << endl;
793
int start = lex->index();
795
TemplateArgumentListAST::Node ast = CreateNode<TemplateArgumentListAST>();
798
if( !parseTemplateArgument(templArg) )
800
ast->addArgument( templArg );
802
while( lex->lookAhead(0) == ',' ){
805
if( !parseTemplateArgument(templArg) ){
812
ast->addArgument( templArg );
815
UPDATE_POS( ast, start, lex->index() );
821
bool Parser::parseTypedef( DeclarationAST::Node& node )
823
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTypedef()" << endl;
825
int start = lex->index();
827
if( lex->lookAhead(0) != Token_typedef ){
832
TypeSpecifierAST::Node spec;
833
if( !parseTypeSpecifierOrClassSpec(spec) ){
834
reportError( i18n("Need a type specifier to declare") );
838
InitDeclaratorListAST::Node declarators;
839
if( !parseInitDeclaratorList(declarators) ){
840
//reportError( i18n("Need an identifier to declare") );
846
TypedefAST::Node ast = CreateNode<TypedefAST>();
847
ast->setTypeSpec( spec );
848
ast->setInitDeclaratorList( declarators );
849
UPDATE_POS( ast, start, lex->index() );
855
bool Parser::parseAsmDefinition( DeclarationAST::Node& /*node*/ )
857
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseAsmDefinition()" << endl;
859
ADVANCE( Token_asm, "asm" );
862
parseCvQualify( cv );
871
bool Parser::parseTemplateDeclaration( DeclarationAST::Node& node )
873
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTemplateDeclaration()" << endl;
875
int start = lex->index();
879
int startExport = lex->index();
880
if( lex->lookAhead(0) == Token_export ){
882
AST::Node n = CreateNode<AST>();
883
UPDATE_POS( n, startExport, lex->index() );
887
if( lex->lookAhead(0) != Token_template ){
892
TemplateParameterListAST::Node params;
893
if( lex->lookAhead(0) == '<' ){
895
parseTemplateParameterList( params );
900
DeclarationAST::Node def;
901
if( !parseDeclaration(def) ){
902
reportError( i18n("expected a declaration") );
905
TemplateDeclarationAST::Node ast = CreateNode<TemplateDeclarationAST>();
906
ast->setExported( exp );
907
ast->setTemplateParameterList( params );
908
ast->setDeclaration( def );
909
UPDATE_POS( ast, start, lex->index() );
915
bool Parser::parseOperator( AST::Node& /*node*/ )
917
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseOperator()" << endl;
918
QString text = lex->lookAhead(0).text();
920
switch( lex->lookAhead(0) ){
924
if( lex->lookAhead(0) == '[' && lex->lookAhead(1) == ']' ){
961
if( lex->lookAhead(0) == '(' && lex->lookAhead(1) == ')' ){
965
} else if( lex->lookAhead(0) == '[' && lex->lookAhead(1) == ']' ){
975
bool Parser::parseCvQualify( GroupAST::Node& node )
977
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseCvQualify()" << endl;
979
int start = lex->index();
981
GroupAST::Node ast = CreateNode<GroupAST>();
984
while( !lex->lookAhead(0).isNull() ){
985
int tk = lex->lookAhead( 0 );
986
if( tk == Token_const || tk == Token_volatile ){
988
int startWord = lex->index();
990
AST::Node word = CreateNode<AST>();
991
UPDATE_POS( word, startWord, lex->index() );
992
ast->addNode( word );
1001
//kdDebug(9007)<< "-----------------> token = " << lex->lookAhead(0).text() << endl;
1002
UPDATE_POS( ast, start, lex->index() );
1008
bool Parser::parseSimpleTypeSpecifier( TypeSpecifierAST::Node& node )
1010
int start = lex->index();
1011
bool isIntegral = false;
1016
switch( lex->lookAhead(0) ){
1024
case Token_unsigned:
1037
TypeSpecifierAST::Node ast = CreateNode<TypeSpecifierAST>();
1039
ClassOrNamespaceNameAST::Node cl = CreateNode<ClassOrNamespaceNameAST>();
1041
AST::Node n = CreateNode<AST>();
1042
UPDATE_POS( n, start, lex->index() );
1044
UPDATE_POS( cl, start, lex->index() );
1046
NameAST::Node name = CreateNode<NameAST>();
1047
name->setUnqualifiedName( cl );
1048
UPDATE_POS( name, start, lex->index() );
1049
ast->setName( name );
1053
if( !parseName(name) ){
1054
lex->setIndex( start );
1057
ast->setName( name );
1060
UPDATE_POS( ast, start, lex->index() );
1065
bool Parser::parsePtrOperator( AST::Node& node )
1067
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parsePtrOperator()" << endl;
1069
int start = lex->index();
1071
if( lex->lookAhead(0) == '&' ){
1073
} else if( lex->lookAhead(0) == '*' ){
1076
int index = lex->index();
1078
if( !parsePtrToMember(memPtr) ){
1079
lex->setIndex( index );
1085
parseCvQualify( cv );
1087
AST::Node ast = CreateNode<AST>();
1088
UPDATE_POS( ast, start, lex->index() );
1095
bool Parser::parseTemplateArgument( AST::Node& node )
1097
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTemplateArgument()" << endl;
1099
int start = lex->index();
1100
if( parseTypeId(node) ){
1101
if( lex->lookAhead(0) == ',' || lex->lookAhead(0) == '>' )
1105
lex->setIndex( start );
1106
if( !parseLogicalOrExpression(node, true) ){
1113
bool Parser::parseTypeSpecifier( TypeSpecifierAST::Node& spec )
1115
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTypeSpecifier()" << endl;
1118
parseCvQualify( cv );
1120
if( parseElaboratedTypeSpecifier(spec) || parseSimpleTypeSpecifier(spec) ){
1121
spec->setCvQualify( cv );
1124
parseCvQualify( cv2 );
1125
spec->setCv2Qualify( cv2 );
1133
bool Parser::parseDeclarator( DeclaratorAST::Node& node )
1135
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseDeclarator()" << endl;
1137
int start = lex->index();
1139
DeclaratorAST::Node ast = CreateNode<DeclaratorAST>();
1141
DeclaratorAST::Node decl;
1142
NameAST::Node declId;
1145
while( parsePtrOperator(ptrOp) ){
1146
ast->addPtrOp( ptrOp );
1149
if( lex->lookAhead(0) == '(' ){
1152
if( !parseDeclarator(decl) ){
1155
ast->setSubDeclarator( decl );
1157
if( lex->lookAhead(0) != ')'){
1163
if( lex->lookAhead(0) == ':' ){
1165
} else if( parseDeclaratorId(declId) ){
1166
ast->setDeclaratorId( declId );
1168
lex->setIndex( start );
1172
if( lex->lookAhead(0) == ':' ){
1175
if( !parseConstantExpression(expr) ){
1176
reportError( i18n("Constant expression expected") );
1183
bool isVector = true;
1185
while( lex->lookAhead(0) == '[' ){
1186
int startArray = lex->index();
1189
parseCommaExpression( expr );
1191
ADVANCE( ']', "]" );
1192
AST::Node array = CreateNode<AST>();
1193
UPDATE_POS( array, startArray, lex->index() );
1194
ast->addArrayDimension( array );
1198
bool skipParen = false;
1199
if( lex->lookAhead(0) == Token_identifier && lex->lookAhead(1) == '(' && lex->lookAhead(2) == '(' ){
1205
if( ast->subDeclarator() && (!isVector || lex->lookAhead(0) != '(') ){
1206
lex->setIndex( start );
1210
int index = lex->index();
1211
if( lex->lookAhead(0) == '(' ){
1214
ParameterDeclarationClauseAST::Node params;
1215
if( !parseParameterDeclarationClause(params) ){
1216
//kdDebug(9007)<< "----------------------> not a parameter declaration, maybe an initializer!?" << endl;
1217
lex->setIndex( index );
1220
ast->setParameterDeclarationClause( params );
1222
if( lex->lookAhead(0) != ')' ){
1223
lex->setIndex( index );
1227
lex->nextToken(); // skip ')'
1229
int startConstant = lex->index();
1230
if( lex->lookAhead(0) == Token_const ){
1232
AST::Node constant = CreateNode<AST>();
1233
UPDATE_POS( constant, startConstant, lex->index() );
1234
ast->setConstant( constant );
1237
GroupAST::Node except;
1238
if( parseExceptionSpecification(except) ){
1239
ast->setExceptionSpecification( except );
1244
if( lex->lookAhead(0) != ')' ){
1245
reportError( i18n("')' expected") );
1253
UPDATE_POS( ast, start, lex->index() );
1259
bool Parser::parseAbstractDeclarator( DeclaratorAST::Node& node )
1261
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseDeclarator()" << endl;
1262
int start = lex->index();
1264
DeclaratorAST::Node ast = CreateNode<DeclaratorAST>();
1266
DeclaratorAST::Node decl;
1267
NameAST::Node declId;
1270
while( parsePtrOperator(ptrOp) ){
1271
ast->addPtrOp( ptrOp );
1274
if( lex->lookAhead(0) == '(' ){
1277
if( !parseAbstractDeclarator(decl) ){
1280
ast->setSubDeclarator( decl );
1282
if( lex->lookAhead(0) != ')'){
1290
while( lex->lookAhead(0) == '[' ){
1291
int startArray = lex->index();
1294
skipCommaExpression( expr );
1296
ADVANCE( ']', "]" );
1297
AST::Node array = CreateNode<AST>();
1298
UPDATE_POS( array, startArray, lex->index() );
1299
ast->addArrayDimension( array );
1302
bool skipParen = false;
1303
if( lex->lookAhead(0) == Token_identifier && lex->lookAhead(1) == '(' && lex->lookAhead(2) == '(' ){
1309
int index = lex->index();
1310
if( lex->lookAhead(0) == '(' ){
1313
ParameterDeclarationClauseAST::Node params;
1314
if( !parseParameterDeclarationClause(params) ){
1315
lex->setIndex( index );
1318
ast->setParameterDeclarationClause( params );
1320
if( lex->lookAhead(0) != ')' ){
1321
lex->setIndex( index );
1326
int startConstant = lex->index();
1327
if( lex->lookAhead(0) == Token_const ){
1329
AST::Node constant = CreateNode<AST>();
1330
UPDATE_POS( constant, startConstant, lex->index() );
1331
ast->setConstant( constant );
1334
GroupAST::Node except;
1335
if( parseExceptionSpecification(except) ){
1336
ast->setExceptionSpecification( except );
1341
if( lex->lookAhead(0) != ')' ){
1342
reportError( i18n("')' expected") );
1350
UPDATE_POS( ast, start, lex->index() );
1357
bool Parser::parseEnumSpecifier( TypeSpecifierAST::Node& node )
1359
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseEnumSpecifier()" << endl;
1361
int start = lex->index();
1363
if( lex->lookAhead(0) != Token_enum ){
1372
if( lex->lookAhead(0) != '{' ){
1373
lex->setIndex( start );
1378
EnumSpecifierAST::Node ast = CreateNode<EnumSpecifierAST>();
1379
ast->setName( name );
1381
EnumeratorAST::Node enumerator;
1382
if( parseEnumerator(enumerator) ){
1383
ast->addEnumerator( enumerator );
1385
while( lex->lookAhead(0) == ',' ){
1388
if( !parseEnumerator(enumerator) ){
1389
//reportError( i18n("Enumerator expected") );
1393
ast->addEnumerator( enumerator );
1397
if( lex->lookAhead(0) != '}' )
1398
reportError( i18n("} missing") );
1402
UPDATE_POS( ast, start, lex->index() );
1408
bool Parser::parseTemplateParameterList( TemplateParameterListAST::Node& node )
1410
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTemplateParameterList()" << endl;
1412
int start = lex->index();
1414
TemplateParameterListAST::Node ast = CreateNode<TemplateParameterListAST>();
1416
TemplateParameterAST::Node param;
1417
if( !parseTemplateParameter(param) ){
1420
ast->addTemplateParameter( param );
1422
while( lex->lookAhead(0) == ',' ){
1425
if( !parseTemplateParameter(param) ){
1429
ast->addTemplateParameter( param );
1433
UPDATE_POS( ast, start, lex->index() );
1439
bool Parser::parseTemplateParameter( TemplateParameterAST::Node& node )
1441
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTemplateParameter()" << endl;
1443
int start = lex->index();
1444
TemplateParameterAST::Node ast = CreateNode<TemplateParameterAST>();
1446
TypeParameterAST::Node typeParameter;
1447
ParameterDeclarationAST::Node param;
1449
int tk = lex->lookAhead( 0 );
1451
if( (tk == Token_class || tk == Token_typename || tk == Token_template) && parseTypeParameter(typeParameter) ){
1452
ast->setTypeParameter( typeParameter );
1456
if( !parseParameterDeclaration(param) )
1458
ast->setTypeValueParameter( param );
1461
UPDATE_POS( ast, start, lex->index() );
1467
bool Parser::parseTypeParameter( TypeParameterAST::Node& node )
1469
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTypeParameter()" << endl;
1471
int start = lex->index();
1472
TypeParameterAST::Node ast = CreateNode<TypeParameterAST>();
1474
AST_FROM_TOKEN( kind, lex->index() );
1475
ast->setKind( kind );
1477
switch( lex->lookAhead(0) ){
1480
case Token_typename:
1482
lex->nextToken(); // skip class
1484
// parse optional name
1486
if( parseName(name) ){
1487
ast->setName( name );
1488
if( lex->lookAhead(0) == '=' ){
1492
if( !parseTypeId(typeId) ){
1496
ast->setTypeId( typeId );
1502
case Token_template:
1504
lex->nextToken(); // skip template
1505
ADVANCE( '<', '<' );
1507
TemplateParameterListAST::Node params;
1508
if( !parseTemplateParameterList(params) ){
1511
ast->setTemplateParameterList( params );
1513
ADVANCE( '>', ">" );
1515
if( lex->lookAhead(0) == Token_class )
1518
// parse optional name
1520
if( parseName(name) ){
1521
ast->setName( name );
1522
if( lex->lookAhead(0) == '=' ){
1526
if( !parseTypeId(typeId) ){
1530
ast->setTypeId( typeId );
1534
if( lex->lookAhead(0) == '=' ){
1537
NameAST::Node templ_name;
1538
parseName( templ_name );
1549
UPDATE_POS( ast, start, lex->index() );
1554
bool Parser::parseStorageClassSpecifier( GroupAST::Node& node )
1556
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseStorageClassSpecifier()" << endl;
1558
int start = lex->index();
1559
GroupAST::Node ast = CreateNode<GroupAST>();
1561
while( !lex->lookAhead(0).isNull() ){
1562
int tk = lex->lookAhead( 0 );
1563
if( tk == Token_friend || tk == Token_auto || tk == Token_register || tk == Token_static ||
1564
tk == Token_extern || tk == Token_mutable ){
1565
int startNode = lex->index();
1568
AST::Node n = CreateNode<AST>();
1569
UPDATE_POS( n, startNode, lex->index() );
1575
if( ast->nodeList().count() == 0 )
1578
UPDATE_POS( ast, start, lex->index() );
1583
bool Parser::parseFunctionSpecifier( GroupAST::Node& node )
1585
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseFunctionSpecifier()" << endl;
1587
int start = lex->index();
1588
GroupAST::Node ast = CreateNode<GroupAST>();
1590
while( !lex->lookAhead(0).isNull() ){
1591
int tk = lex->lookAhead( 0 );
1592
if( tk == Token_inline || tk == Token_virtual || tk == Token_explicit ){
1593
int startNode = lex->index();
1596
AST::Node n = CreateNode<AST>();
1597
UPDATE_POS( n, startNode, lex->index() );
1604
if( ast->nodeList().count() == 0 )
1607
UPDATE_POS( ast, start, lex->index() );
1612
bool Parser::parseTypeId( AST::Node& node )
1614
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTypeId()" << endl;
1616
/// @todo implement the AST for typeId
1617
int start = lex->index();
1618
AST::Node ast = CreateNode<AST>();
1620
TypeSpecifierAST::Node spec;
1621
if( !parseTypeSpecifier(spec) ){
1625
DeclaratorAST::Node decl;
1626
parseAbstractDeclarator( decl );
1628
UPDATE_POS( ast, start, lex->index() );
1634
bool Parser::parseInitDeclaratorList( InitDeclaratorListAST::Node& node )
1636
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseInitDeclaratorList()" << endl;
1638
int start = lex->index();
1640
InitDeclaratorListAST::Node ast = CreateNode<InitDeclaratorListAST>();
1641
InitDeclaratorAST::Node decl;
1643
if( !parseInitDeclarator(decl) ){
1646
ast->addInitDeclarator( decl );
1648
while( lex->lookAhead(0) == ',' ){
1651
if( !parseInitDeclarator(decl) ){
1655
ast->addInitDeclarator( decl );
1657
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseInitDeclaratorList() -- end" << endl;
1659
UPDATE_POS( ast, start, lex->index() );
1665
bool Parser::parseParameterDeclarationClause( ParameterDeclarationClauseAST::Node& node )
1667
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseParameterDeclarationClause()" << endl;
1669
int start = lex->index();
1671
ParameterDeclarationClauseAST::Node ast = CreateNode<ParameterDeclarationClauseAST>();
1673
ParameterDeclarationListAST::Node params;
1674
if( !parseParameterDeclarationList(params) ){
1676
if ( lex->lookAhead(0) == ')' )
1679
if( lex->lookAhead(0) == Token_ellipsis && lex->lookAhead(1) == ')' ){
1680
AST_FROM_TOKEN( ellipsis, lex->index() );
1681
ast->setEllipsis( ellipsis );
1688
if( lex->lookAhead(0) == Token_ellipsis ){
1689
AST_FROM_TOKEN( ellipsis, lex->index() );
1690
ast->setEllipsis( ellipsis );
1695
ast->setParameterDeclarationList( params );
1697
/// @todo add ellipsis
1698
UPDATE_POS( ast, start, lex->index() );
1704
bool Parser::parseParameterDeclarationList( ParameterDeclarationListAST::Node& node )
1706
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseParameterDeclarationList()" << endl;
1708
int start = lex->index();
1710
ParameterDeclarationListAST::Node ast = CreateNode<ParameterDeclarationListAST>();
1712
ParameterDeclarationAST::Node param;
1713
if( !parseParameterDeclaration(param) ){
1714
lex->setIndex( start );
1717
ast->addParameter( param );
1719
while( lex->lookAhead(0) == ',' ){
1722
if( lex->lookAhead(0) == Token_ellipsis )
1725
if( !parseParameterDeclaration(param) ){
1726
lex->setIndex( start );
1729
ast->addParameter( param );
1732
UPDATE_POS( ast, start, lex->index() );
1738
bool Parser::parseParameterDeclaration( ParameterDeclarationAST::Node& node )
1740
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseParameterDeclaration()" << endl;
1742
int start = lex->index();
1745
TypeSpecifierAST::Node spec;
1746
if( !parseTypeSpecifier(spec) ){
1747
lex->setIndex( start );
1751
int index = lex->index();
1753
DeclaratorAST::Node decl;
1754
if( !parseDeclarator(decl) ){
1755
lex->setIndex( index );
1757
// try with abstract declarator
1758
if( !parseAbstractDeclarator(decl) )
1763
if( lex->lookAhead(0) == '=' ){
1765
if( !parseLogicalOrExpression(expr,true) ){
1766
//reportError( i18n("Expression expected") );
1770
ParameterDeclarationAST::Node ast = CreateNode<ParameterDeclarationAST>();
1771
ast->setTypeSpec( spec );
1772
ast->setDeclarator( decl );
1773
ast->setExpression( expr );
1775
UPDATE_POS( ast, start, lex->index() );
1781
bool Parser::parseClassSpecifier( TypeSpecifierAST::Node& node )
1783
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseClassSpecifier()" << endl;
1785
int start = lex->index();
1788
int classKeyStart = lex->index();
1790
int kind = lex->lookAhead( 0 );
1791
if( kind == Token_class || kind == Token_struct || kind == Token_union ){
1792
AST::Node asn = CreateNode<AST>();
1795
UPDATE_POS( classKey, classKeyStart, lex->index() );
1800
GroupAST::Node winDeclSpec;
1801
parseWinDeclSpec( winDeclSpec );
1803
while( lex->lookAhead(0) == Token_identifier && lex->lookAhead(1) == Token_identifier )
1809
BaseClauseAST::Node bases;
1810
if( lex->lookAhead(0) == ':' ){
1811
if( !parseBaseClause(bases) ){
1816
if( lex->lookAhead(0) != '{' ){
1817
lex->setIndex( start );
1821
ADVANCE( '{', '{' );
1823
ClassSpecifierAST::Node ast = CreateNode<ClassSpecifierAST>();
1824
ast->setWinDeclSpec( winDeclSpec );
1825
ast->setClassKey( classKey );
1826
ast->setName( name );
1827
ast->setBaseClause( bases );
1829
while( !lex->lookAhead(0).isNull() ){
1830
if( lex->lookAhead(0) == '}' )
1833
DeclarationAST::Node memSpec;
1834
int startDecl = lex->index();
1835
if( !parseMemberSpecification(memSpec) ){
1836
if( startDecl == lex->index() )
1837
lex->nextToken(); // skip at least one token
1838
skipUntilDeclaration();
1840
ast->addDeclaration( memSpec );
1843
if( lex->lookAhead(0) != '}' ){
1844
reportError( i18n("} missing") );
1848
UPDATE_POS( ast, start, lex->index() );
1854
bool Parser::parseAccessSpecifier( AST::Node& node )
1856
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseAccessSpecifier()" << endl;
1858
int start = lex->index();
1860
switch( lex->lookAhead(0) ){
1862
case Token_protected:
1863
case Token_private: {
1864
AST::Node asn = CreateNode<AST>();
1867
UPDATE_POS( node, start, lex->index() );
1875
bool Parser::parseMemberSpecification( DeclarationAST::Node& node )
1877
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseMemberSpecification()" << endl;
1879
int start = lex->index();
1883
if( lex->lookAhead(0) == ';' ){
1886
} else if( lex->lookAhead(0) == Token_Q_OBJECT || lex->lookAhead(0) == Token_K_DCOP ){
1889
} else if( lex->lookAhead(0) == Token_signals || lex->lookAhead(0) == Token_k_dcop || lex->lookAhead(0) == Token_k_dcop_signals ){
1890
AccessDeclarationAST::Node ast = CreateNode<AccessDeclarationAST>();
1892
AST::Node n = CreateNode<AST>();
1893
UPDATE_POS( n, start, lex->index() );
1894
ast->addAccess( n );
1895
ADVANCE( ':', ":" );
1896
UPDATE_POS( ast, start, lex->index() );
1899
} else if( parseTypedef(node) ){
1901
} else if( parseUsing(node) ){
1903
} else if( parseTemplateDeclaration(node) ){
1905
} else if( parseAccessSpecifier(access) ){
1906
AccessDeclarationAST::Node ast = CreateNode<AccessDeclarationAST>();
1907
ast->addAccess( access );
1909
int startSlot = lex->index();
1910
if( lex->lookAhead(0) == Token_slots ){
1912
AST::Node sl = CreateNode<AST>();
1913
UPDATE_POS( sl, startSlot, lex->index() );
1914
ast->addAccess( sl );
1916
ADVANCE( ':', ":" );
1917
UPDATE_POS( ast, start, lex->index() );
1922
lex->setIndex( start );
1924
GroupAST::Node storageSpec;
1925
parseStorageClassSpecifier( storageSpec );
1928
parseCvQualify( cv );
1930
TypeSpecifierAST::Node spec;
1931
if( parseEnumSpecifier(spec) || parseClassSpecifier(spec) ){
1932
spec->setCvQualify( cv );
1935
parseCvQualify( cv2 );
1936
spec->setCv2Qualify( cv2 );
1938
InitDeclaratorListAST::Node declarators;
1939
parseInitDeclaratorList( declarators );
1940
ADVANCE( ';', ";" );
1942
SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
1943
ast->setTypeSpec( spec );
1944
ast->setInitDeclaratorList( declarators );
1945
UPDATE_POS( ast, start, lex->index() );
1951
lex->setIndex( start );
1952
return parseDeclarationInternal( node );
1955
bool Parser::parseCtorInitializer( AST::Node& /*node*/ )
1957
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseCtorInitializer()" << endl;
1959
if( lex->lookAhead(0) != ':' ){
1965
if( !parseMemInitializerList(inits) ){
1966
reportError( i18n("Member initializers expected") );
1972
bool Parser::parseElaboratedTypeSpecifier( TypeSpecifierAST::Node& node )
1974
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseElaboratedTypeSpecifier()" << endl;
1976
int start = lex->index();
1978
int tk = lex->lookAhead( 0 );
1979
if( tk == Token_class ||
1980
tk == Token_struct ||
1981
tk == Token_union ||
1983
tk == Token_typename )
1985
AST::Node kind = CreateNode<AST>();
1987
UPDATE_POS( kind, start, lex->index() );
1991
if( parseName(name) ){
1992
ElaboratedTypeSpecifierAST::Node ast = CreateNode<ElaboratedTypeSpecifierAST>();
1993
ast->setKind( kind );
1994
ast->setName( name );
1995
UPDATE_POS( ast, start, lex->index() );
2002
lex->setIndex( start );
2006
bool Parser::parseDeclaratorId( NameAST::Node& node )
2008
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseDeclaratorId()" << endl;
2009
return parseName( node );
2012
bool Parser::parseExceptionSpecification( GroupAST::Node& node )
2014
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseExceptionSpecification()" << endl;
2016
if( lex->lookAhead(0) != Token_throw ){
2021
ADVANCE( '(', "(" );
2022
if( lex->lookAhead(0) == Token_ellipsis ){
2023
// extension found in MSVC++ 7.x headers
2024
int start = lex->index();
2025
GroupAST::Node ast = CreateNode<GroupAST>();
2026
AST_FROM_TOKEN( ellipsis, lex->index() );
2027
ast->addNode( ellipsis );
2029
UPDATE_POS( ast, start, lex->index() );
2032
parseTypeIdList( node );
2034
ADVANCE( ')', ")" );
2039
bool Parser::parseEnumerator( EnumeratorAST::Node& node )
2041
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseEnumerator()" << endl;
2043
int start = lex->index();
2045
if( lex->lookAhead(0) != Token_identifier ){
2050
EnumeratorAST::Node ena = CreateNode<EnumeratorAST>();
2053
AST::Node id = CreateNode<AST>();
2054
UPDATE_POS( id, start, lex->index() );
2057
if( lex->lookAhead(0) == '=' ){
2061
if( !parseConstantExpression(expr) ){
2062
reportError( i18n("Constant expression expected") );
2064
node->setExpr( expr );
2067
UPDATE_POS( node, start, lex->index() );
2072
bool Parser::parseInitDeclarator( InitDeclaratorAST::Node& node )
2074
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseInitDeclarator()" << endl;
2076
int start = lex->index();
2078
DeclaratorAST::Node decl;
2080
if( !parseDeclarator(decl) ){
2084
parseInitializer( init );
2086
InitDeclaratorAST::Node ast = CreateNode<InitDeclaratorAST>();
2087
ast->setDeclarator( decl );
2088
ast->setInitializer( init );
2089
UPDATE_POS( ast, start, lex->index() );
2097
bool Parser::parseBaseClause( BaseClauseAST::Node& node )
2099
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseBaseClause()" << endl;
2101
int start = lex->index();
2102
if( lex->lookAhead(0) != ':' ){
2107
BaseClauseAST::Node bca = CreateNode<BaseClauseAST>();
2109
BaseSpecifierAST::Node baseSpec;
2110
if( parseBaseSpecifier(baseSpec) ){
2111
bca->addBaseSpecifier( baseSpec );
2113
while( lex->lookAhead(0) == ',' ){
2116
if( !parseBaseSpecifier(baseSpec) ){
2117
reportError( i18n("Base class specifier expected") );
2120
bca->addBaseSpecifier( baseSpec );
2125
UPDATE_POS( bca, start, lex->index() );
2131
bool Parser::parseInitializer( AST::Node& node )
2133
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseInitializer()" << endl;
2135
if( lex->lookAhead(0) == '=' ){
2139
if( !parseInitializerClause(node) ){
2140
reportError( i18n("Initializer clause expected") );
2143
} else if( lex->lookAhead(0) == '(' ){
2146
skipCommaExpression( expr );
2148
ADVANCE( ')', ")" );
2154
bool Parser::parseMemInitializerList( AST::Node& /*node*/ )
2156
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseMemInitializerList()" << endl;
2159
if( !parseMemInitializer(init) ){
2163
while( lex->lookAhead(0) == ',' ){
2166
if( parseMemInitializer(init) ){
2175
bool Parser::parseMemInitializer( AST::Node& /*node*/ )
2177
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseMemInitializer()" << endl;
2179
NameAST::Node initId;
2180
if( !parseMemInitializerId(initId) ){
2181
reportError( i18n("Identifier expected") );
2184
ADVANCE( '(', '(' );
2186
skipCommaExpression( expr );
2187
ADVANCE( ')', ')' );
2192
bool Parser::parseTypeIdList( GroupAST::Node& node )
2194
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTypeIdList()" << endl;
2196
int start = lex->index();
2199
if( !parseTypeId(typeId) ){
2203
GroupAST::Node ast = CreateNode<GroupAST>();
2204
ast->addNode( typeId );
2206
while( lex->lookAhead(0) == ',' ){
2208
if( parseTypeId(typeId) ){
2209
ast->addNode( typeId );
2211
reportError( i18n("Type id expected") );
2216
UPDATE_POS( ast, start, lex->index() );
2221
bool Parser::parseBaseSpecifier( BaseSpecifierAST::Node& node )
2223
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseBaseSpecifier()" << endl;
2225
int start = lex->index();
2226
BaseSpecifierAST::Node ast = CreateNode<BaseSpecifierAST>();
2229
if( lex->lookAhead(0) == Token_virtual ){
2230
AST_FROM_TOKEN( virt, lex->index() );
2231
ast->setIsVirtual( virt );
2235
parseAccessSpecifier( access );
2237
parseAccessSpecifier( access );
2239
if( lex->lookAhead(0) == Token_virtual ){
2240
AST_FROM_TOKEN( virt, lex->index() );
2241
ast->setIsVirtual( virt );
2247
if( !parseName(name) ){
2248
reportError( i18n("Class name expected") );
2251
ast->setAccess( access );
2252
ast->setName( name );
2253
UPDATE_POS( ast, start, lex->index() );
2260
bool Parser::parseInitializerClause( AST::Node& node )
2262
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseInitializerClause()" << endl;
2264
if( lex->lookAhead(0) == '{' ){
2265
if( !skip('{','}') ){
2266
reportError( i18n("} missing") );
2270
if( !parseAssignmentExpression(node) ){
2271
//reportError( i18n("Expression expected") );
2278
bool Parser::parseMemInitializerId( NameAST::Node& node )
2280
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseMemInitializerId()" << endl;
2282
return parseName( node );
2285
bool Parser::parsePtrToMember( AST::Node& /*node*/ )
2287
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parsePtrToMember()" << endl;
2289
if( lex->lookAhead(0) == Token_scope ){
2293
while( lex->lookAhead(0) == Token_identifier ){
2296
if( lex->lookAhead(0) == Token_scope && lex->lookAhead(1) == '*' ){
2297
lex->nextToken(); // skip ::
2298
lex->nextToken(); // skip *
2307
bool Parser::parseUnqualifiedName( ClassOrNamespaceNameAST::Node& node )
2309
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseUnqualifiedName()" << endl;
2311
int start = lex->index();
2312
bool isDestructor = false;
2314
ClassOrNamespaceNameAST::Node ast = CreateNode<ClassOrNamespaceNameAST>();
2316
if( lex->lookAhead(0) == Token_identifier ){
2317
int startName = lex->index();
2318
AST::Node n = CreateNode<AST>();
2320
UPDATE_POS( n, startName, lex->index() );
2322
} else if( lex->lookAhead(0) == '~' && lex->lookAhead(1) == Token_identifier ){
2323
int startName = lex->index();
2324
AST::Node n = CreateNode<AST>();
2325
lex->nextToken(); // skip ~
2326
lex->nextToken(); // skip classname
2327
UPDATE_POS( n, startName, lex->index() );
2329
isDestructor = true;
2330
} else if( lex->lookAhead(0) == Token_operator ){
2332
if( !parseOperatorFunctionId(n) )
2339
if( !isDestructor ){
2341
int index = lex->index();
2343
if( lex->lookAhead(0) == '<' ){
2346
// optional template arguments
2347
TemplateArgumentListAST::Node args;
2348
parseTemplateArgumentList( args );
2350
if( lex->lookAhead(0) != '>' ){
2351
lex->setIndex( index );
2354
ast->setTemplateArgumentList( args );
2359
UPDATE_POS( ast, start, lex->index() );
2365
bool Parser::parseStringLiteral( AST::Node& /*node*/ )
2367
while( !lex->lookAhead(0).isNull() ) {
2368
if( lex->lookAhead(0) == Token_identifier &&
2369
lex->lookAhead(0).text() == "L" && lex->lookAhead(1) == Token_string_literal ) {
2373
} else if( lex->lookAhead(0) == Token_string_literal ) {
2381
bool Parser::skipExpressionStatement( StatementAST::Node& node )
2383
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::skipExpressionStatement()" << endl;
2385
int start = lex->index();
2388
skipCommaExpression( expr );
2390
ADVANCE( ';', ";" );
2392
ExpressionStatementAST::Node ast = CreateNode<ExpressionStatementAST>();
2393
ast->setExpression( expr );
2394
UPDATE_POS( ast, start, lex->index() );
2400
bool Parser::parseStatement( StatementAST::Node& node ) // thanks to fiore@8080.it ;)
2402
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseStatement()" << endl;
2403
switch( lex->lookAhead(0) ){
2406
return parseWhileStatement( node );
2409
return parseDoStatement( node );
2412
return parseForStatement( node );
2415
return parseForEachStatement( node );
2418
return parseIfStatement( node );
2421
return parseSwitchStatement( node );
2424
return parseTryBlockStatement( node );
2428
return parseLabeledStatement( node );
2431
case Token_continue:
2433
ADVANCE( ';', ";" );
2438
ADVANCE( Token_identifier, "identifier" );
2439
ADVANCE( ';', ";" );
2446
skipCommaExpression( expr );
2447
ADVANCE( ';', ";" );
2452
return parseCompoundStatement( node );
2454
case Token_identifier:
2455
if( parseLabeledStatement(node) )
2460
//kdDebug(9007)<< "------------> try with declaration statement" << endl;
2461
if ( parseDeclarationStatement(node) )
2464
return skipExpressionStatement( node );
2467
bool Parser::parseCondition( ConditionAST::Node& node )
2469
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseCondition()" << endl;
2471
int start = lex->index();
2473
ConditionAST::Node ast = CreateNode<ConditionAST>();
2475
TypeSpecifierAST::Node spec;
2476
if( parseTypeSpecifier(spec) ){
2477
DeclaratorAST::Node decl;
2478
if( parseDeclarator(decl) && lex->lookAhead(0) == '=' ) {
2482
if( skipExpression(expr) ){
2483
ast->setTypeSpec( spec );
2484
ast->setDeclarator( decl );
2485
ast->setExpression( expr );
2487
UPDATE_POS( ast, start, lex->index() );
2495
lex->setIndex( start );
2498
if( !skipCommaExpression(expr) )
2501
ast->setExpression( expr );
2502
UPDATE_POS( ast, start, lex->index() );
2508
bool Parser::parseWhileStatement( StatementAST::Node& node )
2510
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseWhileStatement()" << endl;
2511
int start = lex->index();
2513
ADVANCE( Token_while, "while" );
2514
ADVANCE( '(' , "(" );
2516
ConditionAST::Node cond;
2517
if( !parseCondition(cond) ){
2518
reportError( i18n("condition expected") );
2521
ADVANCE( ')', ")" );
2523
StatementAST::Node body;
2524
if( !parseStatement(body) ){
2525
reportError( i18n("statement expected") );
2528
WhileStatementAST::Node ast = CreateNode<WhileStatementAST>();
2529
ast->setCondition( cond );
2530
ast->setStatement( body );
2531
UPDATE_POS( ast, start, lex->index() );
2537
bool Parser::parseDoStatement( StatementAST::Node& node )
2539
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseDoStatement()" << endl;
2540
int start = lex->index();
2542
ADVANCE( Token_do, "do" );
2544
StatementAST::Node body;
2545
if( !parseStatement(body) ){
2546
reportError( i18n("statement expected") );
2550
ADVANCE_NR( Token_while, "while" );
2551
ADVANCE_NR( '(' , "(" );
2554
if( !skipCommaExpression(expr) ){
2555
reportError( i18n("expression expected") );
2559
ADVANCE_NR( ')', ")" );
2560
ADVANCE_NR( ';', ";" );
2562
DoStatementAST::Node ast = CreateNode<DoStatementAST>();
2563
ast->setStatement( body );
2564
//ast->setCondition( condition );
2565
UPDATE_POS( ast, start, lex->index() );
2571
bool Parser::parseForStatement( StatementAST::Node& node )
2573
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseForStatement()" << endl;
2574
int start = lex->index();
2576
ADVANCE( Token_for, "for" );
2577
ADVANCE( '(', "(" );
2579
StatementAST::Node init;
2580
if( !parseForInitStatement(init) ){
2581
reportError( i18n("for initialization expected") );
2585
ConditionAST::Node cond;
2586
parseCondition( cond );
2587
ADVANCE( ';', ";" );
2590
skipCommaExpression( expr );
2591
ADVANCE( ')', ")" );
2593
StatementAST::Node body;
2594
parseStatement(body);
2596
ForStatementAST::Node ast = CreateNode<ForStatementAST>();
2597
ast->setInitStatement( init );
2598
ast->setCondition( cond );
2599
// ast->setExpression( expression );
2600
ast->setStatement( body );
2601
UPDATE_POS( ast, start, lex->index() );
2608
///@todo add the right parsing for the foreach statement
2609
bool Parser::parseForEachStatement( StatementAST::Node& node )
2611
int start = lex->index();
2613
ADVANCE( Token_foreach, "foreach" );
2614
ADVANCE( '(', "(" );
2617
// replace with the right parsing
2618
skipCommaExpression( expr );
2619
ADVANCE( ')', ")" );
2621
StatementAST::Node body;
2622
parseStatement(body);
2624
ForEachStatementAST::Node ast = CreateNode<ForEachStatementAST>();
2625
// add here the parser results
2626
ast->setStatement( body );
2627
UPDATE_POS( ast, start, lex->index() );
2633
bool Parser::parseForInitStatement( StatementAST::Node& node )
2635
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseForInitStatement()" << endl;
2637
if ( parseDeclarationStatement(node) )
2640
return skipExpressionStatement( node );
2643
bool Parser::parseCompoundStatement( StatementAST::Node& node )
2645
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseCompoundStatement()" << endl;
2646
int start = lex->index();
2648
if( lex->lookAhead(0) != '{' ){
2653
StatementListAST::Node ast = CreateNode<StatementListAST>();
2655
while( !lex->lookAhead(0).isNull() ){
2656
if( lex->lookAhead(0) == '}' )
2659
StatementAST::Node stmt;
2660
int startStmt = lex->index();
2661
if( !parseStatement(stmt) ){
2662
if( startStmt == lex->index() )
2664
skipUntilStatement();
2666
ast->addStatement( stmt );
2670
if( lex->lookAhead(0) != '}' ){
2671
reportError( i18n("} expected") );
2676
UPDATE_POS( ast, start, lex->index() );
2682
bool Parser::parseIfStatement( StatementAST::Node& node )
2684
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseIfStatement()" << endl;
2686
int start = lex->index();
2688
ADVANCE( Token_if, "if" );
2690
ADVANCE( '(' , "(" );
2692
IfStatementAST::Node ast = CreateNode<IfStatementAST>();
2694
ConditionAST::Node cond;
2695
if( !parseCondition(cond) ){
2696
reportError( i18n("condition expected") );
2699
ADVANCE( ')', ")" );
2701
StatementAST::Node stmt;
2702
if( !parseStatement(stmt) ){
2703
reportError( i18n("statement expected") );
2706
ast->setCondition( cond );
2707
ast->setStatement( stmt );
2709
if( lex->lookAhead(0) == Token_else ){
2711
StatementAST::Node elseStmt;
2712
if( !parseStatement(elseStmt) ) {
2713
reportError( i18n("statement expected") );
2715
ast->setElseStatement( elseStmt );
2718
UPDATE_POS( ast, start, lex->index() );
2724
bool Parser::parseSwitchStatement( StatementAST::Node& node )
2726
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseSwitchStatement()" << endl;
2727
int start = lex->index();
2728
ADVANCE( Token_switch, "switch" );
2730
ADVANCE( '(' , "(" );
2732
ConditionAST::Node cond;
2733
if( !parseCondition(cond) ){
2734
reportError( i18n("condition expected") );
2737
ADVANCE( ')', ")" );
2739
StatementAST::Node stmt;
2740
if( !parseCompoundStatement(stmt) ){
2745
SwitchStatementAST::Node ast = CreateNode<SwitchStatementAST>();
2746
ast->setCondition( cond );
2747
ast->setStatement( stmt );
2748
UPDATE_POS( ast, start, lex->index() );
2754
bool Parser::parseLabeledStatement( StatementAST::Node& node )
2756
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseLabeledStatement()" << endl;
2757
switch( lex->lookAhead(0) ){
2758
case Token_identifier:
2760
if( lex->lookAhead(1) == ':' ){
2764
StatementAST::Node stmt;
2765
if( parseStatement(stmt) ){
2776
if( !parseConstantExpression(expr) ){
2777
reportError( i18n("expression expected") );
2778
} else if( lex->lookAhead(0) == Token_ellipsis ){
2782
if( !parseConstantExpression(expr2) ){
2783
reportError( i18n("expression expected") );
2786
ADVANCE( ':', ":" );
2788
StatementAST::Node stmt;
2789
if( parseStatement(stmt) ){
2801
bool Parser::parseBlockDeclaration( DeclarationAST::Node& node )
2803
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseBlockDeclaration()" << endl;
2804
switch( lex->lookAhead(0) ) {
2806
return parseTypedef( node );
2808
return parseUsing( node );
2810
return parseAsmDefinition( node );
2811
case Token_namespace:
2812
return parseNamespaceAliasDefinition( node );
2815
int start = lex->index();
2817
GroupAST::Node storageSpec;
2818
parseStorageClassSpecifier( storageSpec );
2821
parseCvQualify( cv );
2823
TypeSpecifierAST::Node spec;
2824
if ( !parseTypeSpecifierOrClassSpec(spec) ) { // replace with simpleTypeSpecifier?!?!
2825
lex->setIndex( start );
2828
spec->setCvQualify( cv );
2831
parseCvQualify( cv2 );
2832
spec->setCv2Qualify( cv2 );
2834
InitDeclaratorListAST::Node declarators;
2835
parseInitDeclaratorList( declarators );
2837
if( lex->lookAhead(0) != ';' ){
2838
lex->setIndex( start );
2843
SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
2844
ast->setTypeSpec( spec );
2845
ast->setInitDeclaratorList( declarators );
2846
UPDATE_POS( ast, start, lex->index() );
2852
bool Parser::parseNamespaceAliasDefinition( DeclarationAST::Node& /*node*/ )
2854
if ( lex->lookAhead(0) != Token_namespace ) {
2859
ADVANCE( Token_identifier, "identifier" );
2860
ADVANCE( '=', "=" );
2863
if( !parseName(name) ){
2864
reportError( i18n("Namespace name expected") );
2867
ADVANCE( ';', ";" );
2873
bool Parser::parseDeclarationStatement( StatementAST::Node& node )
2875
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseDeclarationStatement()" << endl;
2877
int start = lex->index();
2879
DeclarationAST::Node decl;
2880
if ( !parseBlockDeclaration(decl) ){
2884
DeclarationStatementAST::Node ast = CreateNode<DeclarationStatementAST>();
2885
ast->setDeclaration( decl );
2886
UPDATE_POS( ast, start, lex->index() );
2889
//kdDebug(9007)<< "---------------------> found a block declaration" << endl;
2893
bool Parser::parseDeclarationInternal( DeclarationAST::Node& node )
2895
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseDeclarationInternal()" << endl;
2897
int start = lex->index();
2899
// that is for the case '__declspec(dllexport) int ...' or
2900
// '__declspec(dllexport) inline int ...', etc.
2901
GroupAST::Node winDeclSpec;
2902
parseWinDeclSpec( winDeclSpec );
2904
GroupAST::Node funSpec;
2905
bool hasFunSpec = parseFunctionSpecifier( funSpec );
2907
GroupAST::Node storageSpec;
2908
bool hasStorageSpec = parseStorageClassSpecifier( storageSpec );
2910
if( hasStorageSpec && !hasFunSpec )
2911
hasFunSpec = parseFunctionSpecifier( funSpec );
2913
// that is for the case 'friend __declspec(dllexport) ....'
2914
GroupAST::Node winDeclSpec2;
2915
parseWinDeclSpec( winDeclSpec2 );
2918
parseCvQualify( cv );
2920
int index = lex->index();
2922
if( parseName(name) && lex->lookAhead(0) == '(' ){
2923
// no type specifier, maybe a constructor or a cast operator??
2925
lex->setIndex( index );
2927
InitDeclaratorAST::Node declarator;
2928
if( parseInitDeclarator(declarator) ){
2929
int endSignature = lex->index();
2931
switch( lex->lookAhead(0) ){
2936
InitDeclaratorListAST::Node declarators = CreateNode<InitDeclaratorListAST>();
2938
// update declarators position
2939
int startLine, startColumn, endLine, endColumn;
2940
if( declarator.get() ){
2941
declarator->getStartPosition( &startLine, &startColumn );
2942
declarator->getEndPosition( &endLine, &endColumn );
2943
declarators->setStartPosition( startLine, startColumn );
2944
declarators->setEndPosition( endLine, endColumn );
2946
declarators->addInitDeclarator( declarator );
2948
SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
2949
ast->setInitDeclaratorList( declarators );
2950
ast->setText( toString(start, endSignature) );
2952
UPDATE_POS( node, start, lex->index() );
2961
StatementListAST::Node funBody;
2962
if( parseCtorInitializer(ctorInit) && parseFunctionBody(funBody) ){
2963
FunctionDefinitionAST::Node ast = CreateNode<FunctionDefinitionAST>();
2964
ast->setStorageSpecifier( storageSpec );
2965
ast->setFunctionSpecifier( funSpec );
2966
ast->setInitDeclarator( declarator );
2967
ast->setFunctionBody( funBody );
2968
ast->setText( toString(start, endSignature) );
2970
UPDATE_POS( node, start, lex->index() );
2978
StatementListAST::Node funBody;
2979
if( parseFunctionBody(funBody) ){
2980
FunctionDefinitionAST::Node ast = CreateNode<FunctionDefinitionAST>();
2981
ast->setStorageSpecifier( storageSpec );
2982
ast->setFunctionSpecifier( funSpec );
2983
ast->setInitDeclarator( declarator );
2984
ast->setText( toString(start, endSignature) );
2985
ast->setFunctionBody( funBody );
2987
UPDATE_POS( node, start, lex->index() );
2995
// ops!! it seems a declarator
3007
lex->setIndex( index );
3009
if( lex->lookAhead(0) == Token_const && lex->lookAhead(1) == Token_identifier && lex->lookAhead(2) == '=' ){
3010
// constant definition
3012
InitDeclaratorListAST::Node declarators;
3013
if( parseInitDeclaratorList(declarators) ){
3014
ADVANCE( ';', ";" );
3015
DeclarationAST::Node ast = CreateNode<DeclarationAST>();
3017
UPDATE_POS( node, start, lex->index() );
3024
TypeSpecifierAST::Node spec;
3025
if( parseTypeSpecifier(spec) ){
3027
parseFunctionSpecifier( funSpec ); // e.g. "void inline"
3028
spec->setCvQualify( cv );
3030
InitDeclaratorListAST::Node declarators;
3032
InitDeclaratorAST::Node decl;
3033
int startDeclarator = lex->index();
3034
bool maybeFunctionDefinition = false;
3036
if( lex->lookAhead(0) != ';' ){
3037
if( parseInitDeclarator(decl) && lex->lookAhead(0) == '{' ){
3038
// function definition
3039
maybeFunctionDefinition = true;
3041
lex->setIndex( startDeclarator );
3042
if( !parseInitDeclaratorList(declarators) ){
3049
int endSignature = lex->index();
3050
switch( lex->lookAhead(0) ){
3054
SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
3055
ast->setStorageSpecifier( storageSpec );
3056
ast->setFunctionSpecifier( funSpec );
3057
ast->setText( toString(start, endSignature) );
3058
ast->setTypeSpec( spec );
3059
ast->setWinDeclSpec( winDeclSpec );
3060
ast->setInitDeclaratorList( declarators );
3062
UPDATE_POS( node, start, lex->index() );
3068
if( !maybeFunctionDefinition ){
3072
StatementListAST::Node funBody;
3073
if ( parseFunctionBody(funBody) ) {
3074
FunctionDefinitionAST::Node ast = CreateNode<FunctionDefinitionAST>();
3075
ast->setWinDeclSpec( winDeclSpec );
3076
ast->setStorageSpecifier( storageSpec );
3077
ast->setFunctionSpecifier( funSpec );
3078
ast->setText( toString(start, endSignature) );
3079
ast->setTypeSpec( spec );
3080
ast->setFunctionBody( funBody );
3081
ast->setInitDeclarator( decl );
3083
UPDATE_POS( node, start, lex->index() );
3096
bool Parser::parseFunctionBody( StatementListAST::Node& node )
3098
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseFunctionBody()" << endl;
3100
int start = lex->index();
3101
if( lex->lookAhead(0) != '{' ){
3106
StatementListAST::Node ast = CreateNode<StatementListAST>();
3108
while( !lex->lookAhead(0).isNull() ){
3109
if( lex->lookAhead(0) == '}' )
3112
StatementAST::Node stmt;
3113
int startStmt = lex->index();
3114
if( !parseStatement(stmt) ){
3115
if( startStmt == lex->index() )
3117
skipUntilStatement();
3119
ast->addStatement( stmt );
3122
if( lex->lookAhead(0) != '}' ){
3123
reportError( i18n("} expected") );
3127
UPDATE_POS( ast, start, lex->index() );
3133
QString Parser::toString( int start, int end, const QString& sep ) const
3137
for( int i=start; i<end; ++i ){
3138
l << lex->tokenAt(i).text();
3141
return l.join( sep ).stripWhiteSpace();
3144
bool Parser::parseTypeSpecifierOrClassSpec( TypeSpecifierAST::Node& node )
3146
if( parseClassSpecifier(node) )
3148
else if( parseEnumSpecifier(node) )
3150
else if( parseTypeSpecifier(node) )
3156
bool Parser::parseTryBlockStatement( StatementAST::Node& node )
3158
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseTryBlockStatement()" << endl;
3160
int start = lex->index();
3161
if( lex->lookAhead(0) != Token_try ){
3166
StatementAST::Node stmt;
3167
if( !parseCompoundStatement(stmt) ){
3171
if( lex->lookAhead(0) != Token_catch ){
3172
reportError( i18n("catch expected") );
3175
CatchStatementListAST::Node list = CreateNode<CatchStatementListAST>();
3177
while( lex->lookAhead(0) == Token_catch ){
3180
ADVANCE( '(', "(" );
3181
ConditionAST::Node cond;
3182
if( !parseCondition(cond) ){
3183
reportError( i18n("condition expected") );
3186
ADVANCE( ')', ")" );
3188
StatementAST::Node body;
3189
if( !parseCompoundStatement(body) ){
3193
CatchStatementAST::Node cstmt = CreateNode<CatchStatementAST>();
3194
cstmt->setCondition( cond );
3195
cstmt->setStatement( body );
3196
list->addStatement( cstmt );
3199
TryBlockStatementAST::Node ast = CreateNode<TryBlockStatementAST>();
3200
ast->setStatement( stmt );
3201
ast->setCatchStatementList( list );
3202
UPDATE_POS( ast, start, lex->index() );
3208
bool Parser::parsePrimaryExpression( AST::Node& /*node*/ )
3210
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parsePrimarExpression()" << endl;
3213
switch( lex->lookAhead(0) ){
3214
case Token_string_literal:
3217
parseStringLiteral( lit );
3221
case Token_number_literal:
3222
case Token_char_literal:
3232
case Token_dynamic_cast:
3233
case Token_static_cast:
3234
case Token_reinterpret_cast:
3235
case Token_const_cast:
3241
parseTypeId( typeId );
3246
parseCommaExpression( expr );
3256
parseCommaExpression( expr );
3264
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "token = " << lex->lookAhead(0).text() << endl;
3266
if( !parseExpression(expr) ){
3275
int start = lex->index();
3276
TypeSpecifierAST::Node typeSpec;
3277
if( parseSimpleTypeSpecifier(typeSpec) && lex->lookAhead(0) == '(' ){
3280
parseCommaExpression( expr );
3285
lex->setIndex( start );
3287
if( parseName(name) )
3295
bool Parser::parsePostfixExpression( AST::Node& /*node*/ )
3297
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parsePostfixExpression()" << endl;
3300
if( !parsePrimaryExpression(expr) )
3304
switch(lex->lookAhead(0))
3310
parseCommaExpression( e );
3319
parseCommaExpression( funArgs );
3333
if( lex->lookAhead(0) == Token_template )
3337
if( !parseName(name) ){
3343
case Token_typename:
3348
if( !parseName(name) ){
3354
parseCommaExpression(expr);
3369
bool Parser::parseUnaryExpression( AST::Node& node )
3371
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseUnaryExpression()" << endl;
3373
switch( lex->lookAhead(0) ){
3385
return parseCastExpression( expr );
3391
int index = lex->index();
3392
if( lex->lookAhead(0) == '(' ){
3395
if( parseTypeId(typeId) && lex->lookAhead(0) == ')' ){
3399
lex->setIndex( index );
3402
return parseUnaryExpression( expr );
3406
return parseNewExpression( node );
3409
return parseDeleteExpression( node );
3412
return parsePostfixExpression( node );
3415
bool Parser::parseNewExpression( AST::Node& /*node*/ )
3417
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseNewExpression()" << endl;
3418
if( lex->lookAhead(0) == Token_scope && lex->lookAhead(1) == Token_new )
3421
CHECK( Token_new, "new");
3423
if( lex->lookAhead(0) == '(' ){
3426
parseCommaExpression( expr );
3430
if( lex->lookAhead(0) == '(' ){
3433
parseTypeId( typeId );
3437
parseNewTypeId( typeId );
3441
parseNewInitializer( init );
3445
bool Parser::parseNewTypeId( AST::Node& /*node*/ )
3447
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseNewTypeId()" << endl;
3448
TypeSpecifierAST::Node typeSpec;
3449
if( parseTypeSpecifier(typeSpec) ){
3450
AST::Node declarator;
3451
parseNewDeclarator( declarator );
3458
bool Parser::parseNewDeclarator( AST::Node& /*node*/ )
3460
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseNewDeclarator()" << endl;
3462
if( parsePtrOperator(ptrOp) ){
3463
AST::Node declarator;
3464
parseNewDeclarator( declarator );
3468
if( lex->lookAhead(0) == '[' ){
3469
while( lex->lookAhead(0) == '[' ){
3472
parseExpression( expr );
3473
ADVANCE( ']', "]" );
3481
bool Parser::parseNewInitializer( AST::Node& /*node*/ )
3483
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseNewInitializer()" << endl;
3484
if( lex->lookAhead(0) != '(' )
3489
parseCommaExpression( expr );
3495
bool Parser::parseDeleteExpression( AST::Node& /*node*/ )
3497
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseDeleteExpression()" << endl;
3498
if( lex->lookAhead(0) == Token_scope && lex->lookAhead(1) == Token_delete )
3501
CHECK( Token_delete, "delete" );
3503
if( lex->lookAhead(0) == '[' ){
3509
return parseCastExpression( expr );
3512
bool Parser::parseCastExpression( AST::Node& /*node*/ )
3514
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseCastExpression()" << endl;
3516
int index = lex->index();
3518
if( lex->lookAhead(0) == '(' ){
3521
if ( parseTypeId(typeId) ) {
3522
if ( lex->lookAhead(0) == ')' ) {
3525
if( parseCastExpression(expr) )
3531
lex->setIndex( index );
3534
return parseUnaryExpression( expr );
3537
bool Parser::parsePmExpression( AST::Node& /*node*/ )
3539
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser:parsePmExpression()" << endl;
3541
if( !parseCastExpression(expr) )
3544
while( lex->lookAhead(0) == Token_ptrmem ){
3547
if( !parseCastExpression(expr) )
3554
bool Parser::parseMultiplicativeExpression( AST::Node& /*node*/ )
3556
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseMultiplicativeExpression()" << endl;
3558
if( !parsePmExpression(expr) )
3561
while( lex->lookAhead(0) == '*' || lex->lookAhead(0) == '/' || lex->lookAhead(0) == '%' ){
3564
if( !parsePmExpression(expr) )
3572
bool Parser::parseAdditiveExpression( AST::Node& /*node*/ )
3574
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseAdditiveExpression()" << endl;
3576
if( !parseMultiplicativeExpression(expr) )
3579
while( lex->lookAhead(0) == '+' || lex->lookAhead(0) == '-' ){
3582
if( !parseMultiplicativeExpression(expr) )
3589
bool Parser::parseShiftExpression( AST::Node& /*node*/ )
3591
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseShiftExpression()" << endl;
3593
if( !parseAdditiveExpression(expr) )
3596
while( lex->lookAhead(0) == Token_shift ){
3599
if( !parseAdditiveExpression(expr) )
3606
bool Parser::parseRelationalExpression( AST::Node& /*node*/, bool templArgs )
3608
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseRelationalExpression()" << endl;
3610
if( !parseShiftExpression(expr) )
3613
while( lex->lookAhead(0) == '<' || (lex->lookAhead(0) == '>' && !templArgs) ||
3614
lex->lookAhead(0) == Token_leq || lex->lookAhead(0) == Token_geq ){
3617
if( !parseShiftExpression(expr) )
3624
bool Parser::parseEqualityExpression( AST::Node& /*node*/, bool templArgs )
3626
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseEqualityExpression()" << endl;
3628
if( !parseRelationalExpression(expr, templArgs) )
3631
while( lex->lookAhead(0) == Token_eq || lex->lookAhead(0) == Token_not_eq ){
3634
if( !parseRelationalExpression(expr, templArgs) )
3641
bool Parser::parseAndExpression( AST::Node& /*node*/, bool templArgs )
3643
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseAndExpression()" << endl;
3645
if( !parseEqualityExpression(expr, templArgs) )
3648
while( lex->lookAhead(0) == '&' ){
3651
if( !parseEqualityExpression(expr, templArgs) )
3658
bool Parser::parseExclusiveOrExpression( AST::Node& /*node*/, bool templArgs )
3660
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseExclusiveOrExpression()" << endl;
3662
if( !parseAndExpression(expr, templArgs) )
3665
while( lex->lookAhead(0) == '^' ){
3668
if( !parseAndExpression(expr, templArgs) )
3675
bool Parser::parseInclusiveOrExpression( AST::Node& /*node*/, bool templArgs )
3677
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseInclusiveOrExpression()" << endl;
3679
if( !parseExclusiveOrExpression(expr, templArgs) )
3682
while( lex->lookAhead(0) == '|' ){
3685
if( !parseExclusiveOrExpression(expr, templArgs) )
3692
bool Parser::parseLogicalAndExpression( AST::Node& /*node*/, bool templArgs )
3694
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseLogicalAndExpression()" << endl;
3697
if( !parseInclusiveOrExpression(expr, templArgs) )
3700
while( lex->lookAhead(0) == Token_and ){
3703
if( !parseInclusiveOrExpression(expr, templArgs) )
3710
bool Parser::parseLogicalOrExpression( AST::Node& node, bool templArgs )
3712
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseLogicalOrExpression()" << endl;
3714
int start = lex->index();
3717
if( !parseLogicalAndExpression(expr, templArgs) )
3720
while( lex->lookAhead(0) == Token_or ){
3723
if( !parseLogicalAndExpression(expr, templArgs) )
3727
AST::Node ast = CreateNode<AST>();
3728
UPDATE_POS( ast, start, lex->index() );
3733
bool Parser::parseConditionalExpression( AST::Node& /*node*/ )
3735
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseConditionalExpression()" << endl;
3737
if( !parseLogicalOrExpression(expr) )
3740
if( lex->lookAhead(0) == '?' ){
3743
if( !parseExpression(expr) )
3748
if( !parseAssignmentExpression(expr) )
3755
bool Parser::parseAssignmentExpression( AST::Node& node )
3757
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseAssignmentExpression()" << endl;
3758
int start = lex->index();
3760
if( lex->lookAhead(0) == Token_throw && !parseThrowExpression(expr) )
3762
else if( !parseConditionalExpression(expr) )
3765
while( lex->lookAhead(0) == Token_assign || lex->lookAhead(0) == '=' ){
3768
if( !parseConditionalExpression(expr) )
3772
AST::Node ast = CreateNode<AST>();
3773
UPDATE_POS( ast, start, lex->index() );
3778
bool Parser::parseConstantExpression( AST::Node& node )
3780
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseConstantExpression()" << endl;
3781
int start = lex->index();
3782
if( parseConditionalExpression(node) ){
3783
AST::Node ast = CreateNode<AST>();
3784
UPDATE_POS( ast, start, lex->index() );
3791
bool Parser::parseExpression( AST::Node& node )
3793
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseExpression()" << endl;
3795
int start = lex->index();
3797
if( !parseCommaExpression(node) )
3800
AST::Node ast = CreateNode<AST>();
3801
UPDATE_POS( ast, start, lex->index() );
3806
bool Parser::parseCommaExpression( AST::Node& node )
3808
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseCommaExpression()" << endl;
3809
int start = lex->index();
3812
if( !parseAssignmentExpression(expr) )
3815
while( lex->lookAhead(0) == ',' ){
3818
if( !parseAssignmentExpression(expr) )
3822
AST::Node ast = CreateNode<AST>();
3823
UPDATE_POS( ast, start, lex->index() );
3828
bool Parser::parseThrowExpression( AST::Node& /*node*/ )
3830
//kdDebug(9007)<< "--- tok = " << lex->lookAhead(0).text() << " -- " << "Parser::parseThrowExpression()" << endl;
3831
if( lex->lookAhead(0) != Token_throw )
3834
CHECK( Token_throw, "throw" );
3836
if( !parseAssignmentExpression(expr) )
3842
bool Parser::parseIvarDeclList( AST::Node & node )
3848
bool Parser::parseIvarDecls( AST::Node & node )
3854
bool Parser::parseIvarDecl( AST::Node & node )
3860
bool Parser::parseIvars( AST::Node & node )
3866
bool Parser::parseIvarDeclarator( AST::Node & node )
3872
bool Parser::parseMethodDecl( AST::Node & node )
3878
bool Parser::parseUnarySelector( AST::Node & node )
3884
bool Parser::parseKeywordSelector( AST::Node & node )
3890
bool Parser::parseSelector( AST::Node & node )
3896
bool Parser::parseKeywordDecl( AST::Node & node )
3902
bool Parser::parseReceiver( AST::Node & node )
3908
bool Parser::parseObjcMessageExpr( AST::Node & node )
3914
bool Parser::parseMessageArgs( AST::Node & node )
3920
bool Parser::parseKeywordExpr( AST::Node & node )
3926
bool Parser::parseKeywordArgList( AST::Node & node )
3932
bool Parser::parseKeywordArg( AST::Node & node )
3938
bool Parser::parseReservedWord( AST::Node & node )
3944
bool Parser::parseMyParms( AST::Node & node )
3950
bool Parser::parseMyParm( AST::Node & node )
3956
bool Parser::parseOptParmList( AST::Node & node )
3962
bool Parser::parseObjcSelectorExpr( AST::Node & node )
3968
bool Parser::parseSelectorArg( AST::Node & node )
3974
bool Parser::parseKeywordNameList( AST::Node & node )
3980
bool Parser::parseKeywordName( AST::Node & node )
3986
bool Parser::parseObjcEncodeExpr( AST::Node & node )
3992
bool Parser::parseObjcString( AST::Node & node )
3998
bool Parser::parseProtocolRefs( AST::Node & node )
4004
bool Parser::parseIdentifierList( GroupAST::Node & node )
4006
int start = lex->index();
4008
if( lex->lookAhead(0) != Token_identifier )
4011
GroupAST::Node ast = CreateNode<GroupAST>();
4013
AST_FROM_TOKEN( tk, lex->index() );
4017
while( lex->lookAhead(0) == ',' ){
4019
if( lex->lookAhead(0) == Token_identifier ){
4020
AST_FROM_TOKEN( tk, lex->index() );
4024
ADVANCE( Token_identifier, "identifier" );
4028
UPDATE_POS( node, start, lex->index() );
4032
bool Parser::parseIdentifierColon( AST::Node & node )
4036
if( lex->lookAhead(0) == Token_identifier && lex->lookAhead(1) == ':' ){
4040
} // ### else if PTYPENAME -> return true ;
4045
bool Parser::parseObjcProtocolExpr( AST::Node & node )
4051
bool Parser::parseObjcOpenBracketExpr( AST::Node & node )
4057
bool Parser::parseObjcCloseBracket( AST::Node & node )
4063
bool Parser::parseObjcDef( DeclarationAST::Node & node )
4069
bool Parser::parseObjcClassDef( DeclarationAST::Node & node )
4075
bool Parser::parseObjcClassDecl( DeclarationAST::Node & node )
4079
ADVANCE( OBJC_CLASS, "@class" );
4081
GroupAST::Node idList;
4082
parseIdentifierList( idList );
4083
ADVANCE( ';', ";" );
4088
bool Parser::parseObjcProtocolDecl( DeclarationAST::Node & node )
4092
ADVANCE( OBJC_PROTOCOL, "@protocol" );
4094
GroupAST::Node idList;
4095
parseIdentifierList( idList );
4096
ADVANCE( ';', ";" );
4101
bool Parser::parseObjcAliasDecl( DeclarationAST::Node & node )
4105
ADVANCE( OBJC_ALIAS, "@alias" );
4107
GroupAST::Node idList;
4108
parseIdentifierList( idList );
4109
ADVANCE( ';', ";" );
4114
bool Parser::parseObjcProtocolDef( DeclarationAST::Node & node )
4120
bool Parser::parseObjcMethodDef( DeclarationAST::Node & node )
4126
bool Parser::parseWinDeclSpec( GroupAST::Node & node )
4128
if( lex->lookAhead(0) == Token_identifier && lex->lookAhead(0).text() == "__declspec" && lex->lookAhead(1) == '(' ){
4129
int start = lex->index();
4131
lex->nextToken(); // skip '('
4133
parseIdentifierList( node );
4134
ADVANCE( ')', ")" );
4136
UPDATE_POS( node, start, lex->index() );