00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "parser.h"
00022 #include "driver.h"
00023 #include "lexer.h"
00024 #include "errors.h"
00025
00026
00027 #include <qstring.h>
00028 #include <qstringlist.h>
00029 #include <qasciidict.h>
00030
00031 #include <kdebug.h>
00032 #include <klocale.h>
00033
00034 using namespace std;
00035
00036 #define ADVANCE(tk, descr) \
00037 { \
00038 const Token& token = lex->lookAhead( 0 ); \
00039 if( token != tk ){ \
00040 reportError( i18n("'%1' expected found '%2'").arg(descr).arg(token.text()) ); \
00041 return false; \
00042 } \
00043 lex->nextToken(); \
00044 }
00045
00046 #define ADVANCE_NR(tk, descr) \
00047 { \
00048 const Token& token = lex->lookAhead( 0 ); \
00049 if( token != tk ){ \
00050 reportError( i18n("'%1' expected found '%2'").arg(descr).arg(token.text()) ); \
00051 } \
00052 else \
00053 lex->nextToken(); \
00054 }
00055
00056 #define CHECK(tk, descr) \
00057 { \
00058 const Token& token = lex->lookAhead( 0 ); \
00059 if( token != tk ){ \
00060 return false; \
00061 } \
00062 lex->nextToken(); \
00063 }
00064
00065 #define MATCH(tk, descr) \
00066 { \
00067 const Token& token = lex->lookAhead( 0 ); \
00068 if( token != tk ){ \
00069 reportError( Errors::SyntaxError ); \
00070 return false; \
00071 } \
00072 }
00073
00074 #define UPDATE_POS(node, start, end) \
00075 { \
00076 int line, col; \
00077 const Token &a = lex->tokenAt(start); \
00078 const Token &b = lex->tokenAt( end!=start ? end-1 : end ); \
00079 a.getStartPosition( &line, &col ); \
00080 (node)->setStartPosition( line, col ); \
00081 b.getEndPosition( &line, &col ); \
00082 (node)->setEndPosition( line, col ); \
00083 if( (node)->nodeType() == NodeType_Generic ) { \
00084 if ((start) == (end) || (end) == (start)+1) \
00085 (node)->setSlice(lex->source(), a.position(), a.length()); \
00086 else \
00087 (node)->setText( toString((start),(end)) ); \
00088 } \
00089 }
00090
00091 #define AST_FROM_TOKEN(node, tk) \
00092 AST::Node node = CreateNode<AST>(); \
00093 UPDATE_POS( node, (tk), (tk)+1 );
00094
00095
00096
00097 enum
00098 {
00099 OBJC_CLASS,
00100 OBJC_PROTOCOL,
00101 OBJC_ALIAS
00102 };
00103
00104 struct ParserPrivateData
00105 {
00106 ParserPrivateData()
00107 {}
00108 };
00109
00110 Parser::Parser( Driver* driver, Lexer* lexer )
00111 : m_driver( driver ),
00112 lex( lexer )
00113 {
00114 d = new ParserPrivateData();
00115
00116 m_maxProblems = 5;
00117 objcp = false;
00118 }
00119
00120 Parser::~Parser()
00121 {
00122 delete( d );
00123 d = 0;
00124 }
00125
00126 bool Parser::reportError( const Error& err )
00127 {
00128
00129 if( m_problems < m_maxProblems ){
00130 ++m_problems;
00131 int line=0, col=0;
00132 const Token& token = lex->lookAhead( 0 );
00133 lex->getTokenPosition( token, &line, &col );
00134
00135 QString s = lex->lookAhead(0).text();
00136 s = s.left( 30 ).stripWhiteSpace();
00137 if( s.isEmpty() )
00138 s = i18n( "<eof>" );
00139
00140 m_driver->addProblem( m_driver->currentFileName(), Problem(err.text.arg(s), line, col) );
00141 }
00142
00143 return true;
00144 }
00145
00146 bool Parser::reportError( const QString& msg )
00147 {
00148
00149 if( m_problems < m_maxProblems ){
00150 ++m_problems;
00151 int line=0, col=0;
00152 const Token& token = lex->lookAhead( 0 );
00153 lex->getTokenPosition( token, &line, &col );
00154
00155 m_driver->addProblem( m_driver->currentFileName(), Problem(msg, line, col) );
00156 }
00157
00158 return true;
00159 }
00160
00161 void Parser::syntaxError()
00162 {
00163 (void) reportError( Errors::SyntaxError );
00164 }
00165
00166 bool Parser::skipUntil( int token )
00167 {
00168
00169 while( !lex->lookAhead(0).isNull() ){
00170 if( lex->lookAhead(0) == token )
00171 return true;
00172
00173 lex->nextToken();
00174 }
00175
00176 return false;
00177 }
00178
00179 bool Parser::skipUntilDeclaration()
00180 {
00181
00182
00183 while( !lex->lookAhead(0).isNull() ){
00184
00185 switch( lex->lookAhead(0) ){
00186 case ';':
00187 case '~':
00188 case Token_scope:
00189 case Token_identifier:
00190 case Token_operator:
00191 case Token_char:
00192 case Token_wchar_t:
00193 case Token_bool:
00194 case Token_short:
00195 case Token_int:
00196 case Token_long:
00197 case Token_signed:
00198 case Token_unsigned:
00199 case Token_float:
00200 case Token_double:
00201 case Token_void:
00202 case Token_extern:
00203 case Token_namespace:
00204 case Token_using:
00205 case Token_typedef:
00206 case Token_asm:
00207 case Token_template:
00208 case Token_export:
00209
00210 case Token_const:
00211 case Token_volatile:
00212
00213 case Token_public:
00214 case Token_protected:
00215 case Token_private:
00216 case Token_signals:
00217 case Token_slots:
00218 return true;
00219
00220 default:
00221 lex->nextToken();
00222 }
00223 }
00224
00225 return false;
00226 }
00227
00228 bool Parser::skipUntilStatement()
00229 {
00230
00231
00232 while( !lex->lookAhead(0).isNull() ){
00233 switch( lex->lookAhead(0) ){
00234 case ';':
00235 case '{':
00236 case '}':
00237 case Token_const:
00238 case Token_volatile:
00239 case Token_identifier:
00240 case Token_case:
00241 case Token_default:
00242 case Token_if:
00243 case Token_switch:
00244 case Token_while:
00245 case Token_do:
00246 case Token_for:
00247 case Token_break:
00248 case Token_continue:
00249 case Token_return:
00250 case Token_goto:
00251 case Token_try:
00252 case Token_catch:
00253 case Token_throw:
00254 case Token_char:
00255 case Token_wchar_t:
00256 case Token_bool:
00257 case Token_short:
00258 case Token_int:
00259 case Token_long:
00260 case Token_signed:
00261 case Token_unsigned:
00262 case Token_float:
00263 case Token_double:
00264 case Token_void:
00265 case Token_class:
00266 case Token_struct:
00267 case Token_union:
00268 case Token_enum:
00269 case Token_scope:
00270 case Token_template:
00271 case Token_using:
00272 return true;
00273
00274 default:
00275 lex->nextToken();
00276 }
00277 }
00278
00279 return false;
00280 }
00281
00282 bool Parser::skip( int l, int r )
00283 {
00284 int count = 0;
00285 while( !lex->lookAhead(0).isNull() ){
00286 int tk = lex->lookAhead( 0 );
00287
00288 if( tk == l )
00289 ++count;
00290 else if( tk == r )
00291 --count;
00292 else if( l != '{' && (tk == '{' || tk == '}' || tk == ';') )
00293 return false;
00294
00295 if( count == 0 )
00296 return true;
00297
00298 lex->nextToken();
00299 }
00300
00301 return false;
00302 }
00303
00304 bool Parser::skipCommaExpression( AST::Node& node )
00305 {
00306
00307
00308 int start = lex->index();
00309
00310 AST::Node expr;
00311 if( !skipExpression(expr) )
00312 return false;
00313
00314 while( lex->lookAhead(0) == ',' ){
00315 lex->nextToken();
00316
00317 if( !skipExpression(expr) ){
00318 reportError( i18n("expression expected") );
00319 return false;
00320 }
00321 }
00322
00323 AST::Node ast = CreateNode<AST>();
00324 UPDATE_POS( ast, start, lex->index() );
00325 node = ast;
00326
00327 return true;
00328 }
00329
00330 bool Parser::skipExpression( AST::Node& node )
00331 {
00332
00333
00334 int start = lex->index();
00335
00336 while( !lex->lookAhead(0).isNull() ){
00337 int tk = lex->lookAhead( 0 );
00338
00339 switch( tk ){
00340 case '(':
00341 skip( '(', ')' );
00342 lex->nextToken();
00343 break;
00344
00345 case '[':
00346 skip( '[', ']' );
00347 lex->nextToken();
00348 break;
00349
00350 #if 0
00351 case Token_identifier:
00352 lex->nextToken();
00353 if( lex->lookAhead( 0 ) == Token_identifier )
00354 return true;
00355 break;
00356 #endif
00357
00358 case ';':
00359 case ',':
00360 case ']':
00361 case ')':
00362 case '{':
00363 case '}':
00364 case Token_case:
00365 case Token_default:
00366 case Token_if:
00367 case Token_while:
00368 case Token_do:
00369 case Token_for:
00370 case Token_break:
00371 case Token_continue:
00372 case Token_return:
00373 case Token_goto:
00374 {
00375 AST::Node ast = CreateNode<AST>();
00376 UPDATE_POS( ast, start, lex->index() );
00377 node = ast;
00378 }
00379 return true;
00380
00381 default:
00382 lex->nextToken();
00383 }
00384 }
00385
00386 return false;
00387 }
00388
00389 bool Parser::parseName( NameAST::Node& node )
00390 {
00391
00392
00393 GroupAST::Node winDeclSpec;
00394 parseWinDeclSpec( winDeclSpec );
00395
00396 int start = lex->index();
00397
00398 NameAST::Node ast = CreateNode<NameAST>();
00399
00400 if( lex->lookAhead(0) == Token_scope ){
00401 ast->setGlobal( true );
00402 lex->nextToken();
00403 }
00404
00405 int idx = lex->index();
00406
00407 while( true ){
00408 ClassOrNamespaceNameAST::Node n;
00409 if( !parseUnqualifiedName(n) ) {
00410 return false;
00411 }
00412
00413 if( lex->lookAhead(0) == Token_scope ){
00414 lex->nextToken();
00415 ast->addClassOrNamespaceName( n );
00416 if( lex->lookAhead(0) == Token_template )
00417 lex->nextToken();
00418 } else {
00419 ast->setUnqualifiedName( n );
00420 break;
00421 }
00422 }
00423
00424 if( idx == lex->index() )
00425 return false;
00426
00427 UPDATE_POS( ast, start, lex->index() );
00428 node = ast;
00429
00430 return true;
00431 }
00432
00433 bool Parser::parseTranslationUnit( TranslationUnitAST::Node& node )
00434 {
00435
00436
00437 int start = lex->index();
00438
00439 m_problems = 0;
00440 TranslationUnitAST::Node tun = CreateNode<TranslationUnitAST>();
00441 node = tun;
00442 while( !lex->lookAhead(0).isNull() ){
00443 DeclarationAST::Node def;
00444 int startDecl = lex->index();
00445 if( !parseDeclaration(def) ){
00446
00447 if( startDecl == lex->index() )
00448 lex->nextToken();
00449 skipUntilDeclaration();
00450 }
00451 node->addDeclaration( def );
00452 }
00453
00454 UPDATE_POS( node, start, lex->index() );
00455
00456
00457 node->setStartPosition( 0, 0 );
00458
00459 return m_problems == 0;
00460 }
00461
00462 bool Parser::parseDeclaration( DeclarationAST::Node& node )
00463 {
00464
00465
00466 int start = lex->index();
00467
00468 switch( lex->lookAhead(0) ){
00469
00470 case ';':
00471 lex->nextToken();
00472 return true;
00473
00474 case Token_extern:
00475 return parseLinkageSpecification( node );
00476
00477 case Token_namespace:
00478 return parseNamespace( node );
00479
00480 case Token_using:
00481 return parseUsing( node );
00482
00483 case Token_typedef:
00484 return parseTypedef( node );
00485
00486 case Token_asm:
00487 return parseAsmDefinition( node );
00488
00489 case Token_template:
00490 case Token_export:
00491 return parseTemplateDeclaration( node );
00492
00493 default:
00494 {
00495
00496
00497 if( objcp && parseObjcDef(node) )
00498 return true;
00499
00500 lex->setIndex( start );
00501
00502 GroupAST::Node storageSpec;
00503 parseStorageClassSpecifier( storageSpec );
00504
00505 GroupAST::Node cv;
00506 parseCvQualify( cv );
00507
00508 TypeSpecifierAST::Node spec;
00509 AST::Node declarator;
00510 if( parseEnumSpecifier(spec) || parseClassSpecifier(spec) ){
00511 spec->setCvQualify( cv );
00512
00513 GroupAST::Node cv2;
00514 parseCvQualify( cv2 );
00515 spec->setCv2Qualify( cv2 );
00516
00517 InitDeclaratorListAST::Node declarators;
00518 parseInitDeclaratorList(declarators);
00519 ADVANCE( ';', ";" );
00520
00521 SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
00522 ast->setStorageSpecifier( storageSpec );
00523 ast->setTypeSpec( spec );
00524 ast->setInitDeclaratorList( declarators );
00525 UPDATE_POS( ast, start, lex->index() );
00526 node = ast;
00527
00528 return true;
00529 }
00530
00531 lex->setIndex( start );
00532 return parseDeclarationInternal( node );
00533 }
00534
00535 }
00536 }
00537
00538 bool Parser::parseLinkageSpecification( DeclarationAST::Node& node )
00539 {
00540
00541
00542 int start = lex->index();
00543
00544 if( lex->lookAhead(0) != Token_extern ){
00545 return false;
00546 }
00547 lex->nextToken();
00548
00549 LinkageSpecificationAST::Node ast = CreateNode<LinkageSpecificationAST>();
00550
00551 int startExternType = lex->index();
00552 if( lex->lookAhead(0) == Token_string_literal ){
00553 lex->nextToken();
00554 AST::Node externType = CreateNode<AST>();
00555 UPDATE_POS( externType, startExternType, lex->index() );
00556
00557 ast->setExternType( externType );
00558 }
00559
00560 if( lex->lookAhead(0) == '{' ){
00561 LinkageBodyAST::Node linkageBody;
00562 parseLinkageBody( linkageBody );
00563 ast->setLinkageBody( linkageBody );
00564 } else {
00565 DeclarationAST::Node decl;
00566 if( !parseDeclaration(decl) ){
00567 reportError( i18n("Declaration syntax error") );
00568 }
00569 ast->setDeclaration( decl );
00570 }
00571
00572 UPDATE_POS( ast, start, lex->index() );
00573
00574 node = ast;
00575
00576 return true;
00577 }
00578
00579 bool Parser::parseLinkageBody( LinkageBodyAST::Node& node )
00580 {
00581
00582
00583 int start = lex->index();
00584
00585 if( lex->lookAhead(0) != '{' ){
00586 return false;
00587 }
00588 lex->nextToken();
00589
00590 LinkageBodyAST::Node lba = CreateNode<LinkageBodyAST>();
00591 node = lba;
00592
00593 while( !lex->lookAhead(0).isNull() ){
00594 int tk = lex->lookAhead( 0 );
00595
00596 if( tk == '}' )
00597 break;
00598
00599 DeclarationAST::Node def;
00600 int startDecl = lex->index();
00601 if( parseDeclaration(def) ){
00602 node->addDeclaration( def );
00603 } else {
00604
00605 if( startDecl == lex->index() )
00606 lex->nextToken();
00607 skipUntilDeclaration();
00608 }
00609 }
00610
00611 if( lex->lookAhead(0) != '}' ){
00612 reportError( i18n("} expected") );
00613 } else
00614 lex->nextToken();
00615
00616 UPDATE_POS( node, start, lex->index() );
00617 return true;
00618 }
00619
00620 bool Parser::parseNamespace( DeclarationAST::Node& node )
00621 {
00622
00623
00624 int start = lex->index();
00625
00626 if( lex->lookAhead(0) != Token_namespace ){
00627 return false;
00628 }
00629 lex->nextToken();
00630
00631 int startNamespaceName = lex->index();
00632 if( lex->lookAhead(0) == Token_identifier ){
00633 lex->nextToken();
00634 }
00635 AST::Node namespaceName = CreateNode<AST>();
00636 UPDATE_POS( namespaceName, startNamespaceName, lex->index() );
00637
00638 if ( lex->lookAhead(0) == '=' ) {
00639
00640 lex->nextToken();
00641
00642 NameAST::Node name;
00643 if( parseName(name) ){
00644 ADVANCE( ';', ";" );
00645
00646 NamespaceAliasAST::Node ast = CreateNode<NamespaceAliasAST>();
00647 ast->setNamespaceName( namespaceName );
00648 ast->setAliasName( name );
00649 UPDATE_POS( ast, start, lex->index() );
00650 node = ast;
00651 return true;
00652 } else {
00653 reportError( i18n("namespace expected") );
00654 return false;
00655 }
00656 } else if( lex->lookAhead(0) != '{' ){
00657 reportError( i18n("{ expected") );
00658 return false;
00659 }
00660
00661 NamespaceAST::Node ast = CreateNode<NamespaceAST>();
00662 ast->setNamespaceName( namespaceName );
00663
00664 LinkageBodyAST::Node linkageBody;
00665 parseLinkageBody( linkageBody );
00666
00667 ast->setLinkageBody( linkageBody );
00668 UPDATE_POS( ast, start, lex->index() );
00669 node = ast;
00670
00671 return true;
00672 }
00673
00674 bool Parser::parseUsing( DeclarationAST::Node& node )
00675 {
00676
00677
00678 int start = lex->index();
00679
00680 if( lex->lookAhead(0) != Token_using ){
00681 return false;
00682 }
00683 lex->nextToken();
00684
00685 if( lex->lookAhead(0) == Token_namespace ){
00686 if( !parseUsingDirective(node) ){
00687 return false;
00688 }
00689 UPDATE_POS( node, start, lex->index() );
00690 return true;
00691 }
00692
00693 UsingAST::Node ast = CreateNode<UsingAST>();
00694
00695 int startTypeName = lex->index();
00696 if( lex->lookAhead(0) == Token_typename ){
00697 lex->nextToken();
00698 AST::Node tn = CreateNode<AST>();
00699 UPDATE_POS( tn, startTypeName, lex->index() );
00700 ast->setTypeName( tn );
00701 }
00702
00703 NameAST::Node name;
00704 if( !parseName(name) )
00705 return false;
00706
00707 ast->setName( name );
00708
00709 ADVANCE( ';', ";" );
00710
00711 UPDATE_POS( ast, start, lex->index() );
00712 node = ast;
00713
00714 return true;
00715 }
00716
00717 bool Parser::parseUsingDirective( DeclarationAST::Node& node )
00718 {
00719
00720
00721 int start = lex->index();
00722
00723 if( lex->lookAhead(0) != Token_namespace ){
00724 return false;
00725 }
00726 lex->nextToken();
00727
00728 NameAST::Node name;
00729 if( !parseName(name) ){
00730 reportError( i18n("Namespace name expected") );
00731 return false;
00732 }
00733
00734 ADVANCE( ';', ";" );
00735
00736 UsingDirectiveAST::Node ast = CreateNode<UsingDirectiveAST>();
00737 ast->setName( name );
00738 UPDATE_POS( ast, start, lex->index() );
00739 node = ast;
00740
00741 return true;
00742 }
00743
00744
00745 bool Parser::parseOperatorFunctionId( AST::Node& node )
00746 {
00747
00748
00749 int start = lex->index();
00750
00751 if( lex->lookAhead(0) != Token_operator ){
00752 return false;
00753 }
00754 lex->nextToken();
00755
00756 AST::Node op;
00757 if( parseOperator(op) ){
00758 AST::Node asn = CreateNode<AST>();
00759 node = asn;
00760 UPDATE_POS( node, start, lex->index() );
00761 return true;
00762 } else {
00763
00764 GroupAST::Node cv;
00765 parseCvQualify(cv);
00766
00767 TypeSpecifierAST::Node spec;
00768 if( !parseSimpleTypeSpecifier(spec) ){
00769 syntaxError();
00770 return false;
00771 }
00772 spec->setCvQualify( cv );
00773
00774 GroupAST::Node cv2;
00775 parseCvQualify(cv2);
00776 spec->setCv2Qualify( cv2 );
00777
00778 AST::Node ptrOp;
00779 while( parsePtrOperator(ptrOp) )
00780 ;
00781
00782 AST::Node asn = CreateNode<AST>();
00783 node = asn;
00784 UPDATE_POS( node, start, lex->index() );
00785 return true;
00786 }
00787 }
00788
00789 bool Parser::parseTemplateArgumentList( TemplateArgumentListAST::Node& node, bool reportError )
00790 {
00791
00792
00793 int start = lex->index();
00794
00795 TemplateArgumentListAST::Node ast = CreateNode<TemplateArgumentListAST>();
00796
00797 AST::Node templArg;
00798 if( !parseTemplateArgument(templArg) )
00799 return false;
00800 ast->addArgument( templArg );
00801
00802 while( lex->lookAhead(0) == ',' ){
00803 lex->nextToken();
00804
00805 if( !parseTemplateArgument(templArg) ){
00806 if( reportError ){
00807 syntaxError();
00808 break;
00809 } else
00810 return false;
00811 }
00812 ast->addArgument( templArg );
00813 }
00814
00815 UPDATE_POS( ast, start, lex->index() );
00816 node = ast;
00817
00818 return true;
00819 }
00820
00821 bool Parser::parseTypedef( DeclarationAST::Node& node )
00822 {
00823
00824
00825 int start = lex->index();
00826
00827 if( lex->lookAhead(0) != Token_typedef ){
00828 return false;
00829 }
00830 lex->nextToken();
00831
00832 TypeSpecifierAST::Node spec;
00833 if( !parseTypeSpecifierOrClassSpec(spec) ){
00834 reportError( i18n("Need a type specifier to declare") );
00835 return false;
00836 }
00837
00838 InitDeclaratorListAST::Node declarators;
00839 if( !parseInitDeclaratorList(declarators) ){
00840
00841
00842 }
00843
00844 ADVANCE( ';', ";" );
00845
00846 TypedefAST::Node ast = CreateNode<TypedefAST>();
00847 ast->setTypeSpec( spec );
00848 ast->setInitDeclaratorList( declarators );
00849 UPDATE_POS( ast, start, lex->index() );
00850 node = ast;
00851
00852 return true;
00853 }
00854
00855 bool Parser::parseAsmDefinition( DeclarationAST::Node& )
00856 {
00857
00858
00859 ADVANCE( Token_asm, "asm" );
00860
00861 GroupAST::Node cv;
00862 parseCvQualify( cv );
00863
00864 skip( '(', ')' );
00865 ADVANCE( ')', ")" );
00866 ADVANCE( ';', ';' );
00867
00868 return true;
00869 }
00870
00871 bool Parser::parseTemplateDeclaration( DeclarationAST::Node& node )
00872 {
00873
00874
00875 int start = lex->index();
00876
00877 AST::Node exp;
00878
00879 int startExport = lex->index();
00880 if( lex->lookAhead(0) == Token_export ){
00881 lex->nextToken();
00882 AST::Node n = CreateNode<AST>();
00883 UPDATE_POS( n, startExport, lex->index() );
00884 exp = n;
00885 }
00886
00887 if( lex->lookAhead(0) != Token_template ){
00888 return false;
00889 }
00890 lex->nextToken();
00891
00892 TemplateParameterListAST::Node params;
00893 if( lex->lookAhead(0) == '<' ){
00894 lex->nextToken();
00895 parseTemplateParameterList( params );
00896
00897 ADVANCE( '>', ">" );
00898 }
00899
00900 DeclarationAST::Node def;
00901 if( !parseDeclaration(def) ){
00902 reportError( i18n("expected a declaration") );
00903 }
00904
00905 TemplateDeclarationAST::Node ast = CreateNode<TemplateDeclarationAST>();
00906 ast->setExported( exp );
00907 ast->setTemplateParameterList( params );
00908 ast->setDeclaration( def );
00909 UPDATE_POS( ast, start, lex->index() );
00910 node = ast;
00911
00912 return true;
00913 }
00914
00915 bool Parser::parseOperator( AST::Node& )
00916 {
00917
00918 QString text = lex->lookAhead(0).text();
00919
00920 switch( lex->lookAhead(0) ){
00921 case Token_new:
00922 case Token_delete:
00923 lex->nextToken();
00924 if( lex->lookAhead(0) == '[' && lex->lookAhead(1) == ']' ){
00925 lex->nextToken();
00926 lex->nextToken();
00927 text += "[]";
00928 }
00929 return true;
00930
00931 case '+':
00932 case '-':
00933 case '*':
00934 case '/':
00935 case '%':
00936 case '^':
00937 case '&':
00938 case '|':
00939 case '~':
00940 case '!':
00941 case '=':
00942 case '<':
00943 case '>':
00944 case ',':
00945 case Token_assign:
00946 case Token_shift:
00947 case Token_eq:
00948 case Token_not_eq:
00949 case Token_leq:
00950 case Token_geq:
00951 case Token_and:
00952 case Token_or:
00953 case Token_incr:
00954 case Token_decr:
00955 case Token_ptrmem:
00956 case Token_arrow:
00957 lex->nextToken();
00958 return true;
00959
00960 default:
00961 if( lex->lookAhead(0) == '(' && lex->lookAhead(1) == ')' ){
00962 lex->nextToken();
00963 lex->nextToken();
00964 return true;
00965 } else if( lex->lookAhead(0) == '[' && lex->lookAhead(1) == ']' ){
00966 lex->nextToken();
00967 lex->nextToken();
00968 return true;
00969 }
00970 }
00971
00972 return false;
00973 }
00974
00975 bool Parser::parseCvQualify( GroupAST::Node& node )
00976 {
00977
00978
00979 int start = lex->index();
00980
00981 GroupAST::Node ast = CreateNode<GroupAST>();
00982
00983 int n = 0;
00984 while( !lex->lookAhead(0).isNull() ){
00985 int tk = lex->lookAhead( 0 );
00986 if( tk == Token_const || tk == Token_volatile ){
00987 ++n;
00988 int startWord = lex->index();
00989 lex->nextToken();
00990 AST::Node word = CreateNode<AST>();
00991 UPDATE_POS( word, startWord, lex->index() );
00992 ast->addNode( word );
00993 } else
00994 break;
00995 }
00996
00997 if( n == 0 )
00998 return false;
00999
01000
01001
01002 UPDATE_POS( ast, start, lex->index() );
01003
01004 node = ast;
01005 return true;
01006 }
01007
01008 bool Parser::parseSimpleTypeSpecifier( TypeSpecifierAST::Node& node )
01009 {
01010 int start = lex->index();
01011 bool isIntegral = false;
01012 bool done = false;
01013
01014 while( !done ){
01015
01016 switch( lex->lookAhead(0) ){
01017 case Token_char:
01018 case Token_wchar_t:
01019 case Token_bool:
01020 case Token_short:
01021 case Token_int:
01022 case Token_long:
01023 case Token_signed:
01024 case Token_unsigned:
01025 case Token_float:
01026 case Token_double:
01027 case Token_void:
01028 isIntegral = true;
01029 lex->nextToken();
01030 break;
01031
01032 default:
01033 done = true;
01034 }
01035 }
01036
01037 TypeSpecifierAST::Node ast = CreateNode<TypeSpecifierAST>();
01038 if( isIntegral ){
01039 ClassOrNamespaceNameAST::Node cl = CreateNode<ClassOrNamespaceNameAST>();
01040
01041 AST::Node n = CreateNode<AST>();
01042 UPDATE_POS( n, start, lex->index() );
01043 cl->setName( n );
01044 UPDATE_POS( cl, start, lex->index() );
01045
01046 NameAST::Node name = CreateNode<NameAST>();
01047 name->setUnqualifiedName( cl );
01048 UPDATE_POS( name, start, lex->index() );
01049 ast->setName( name );
01050
01051 } else {
01052 NameAST::Node name;
01053 if( !parseName(name) ){
01054 lex->setIndex( start );
01055 return false;
01056 }
01057 ast->setName( name );
01058 }
01059
01060 UPDATE_POS( ast, start, lex->index() );
01061 node = ast;
01062 return true;
01063 }
01064
01065 bool Parser::parsePtrOperator( AST::Node& node )
01066 {
01067
01068
01069 int start = lex->index();
01070
01071 if( lex->lookAhead(0) == '&' ){
01072 lex->nextToken();
01073 } else if( lex->lookAhead(0) == '*' ){
01074 lex->nextToken();
01075 } else {
01076 int index = lex->index();
01077 AST::Node memPtr;
01078 if( !parsePtrToMember(memPtr) ){
01079 lex->setIndex( index );
01080 return false;
01081 }
01082 }
01083
01084 GroupAST::Node cv;
01085 parseCvQualify( cv );
01086
01087 AST::Node ast = CreateNode<AST>();
01088 UPDATE_POS( ast, start, lex->index() );
01089 node = ast;
01090
01091 return true;
01092 }
01093
01094
01095 bool Parser::parseTemplateArgument( AST::Node& node )
01096 {
01097
01098
01099 int start = lex->index();
01100 if( parseTypeId(node) ){
01101 if( lex->lookAhead(0) == ',' || lex->lookAhead(0) == '>' )
01102 return true;
01103 }
01104
01105 lex->setIndex( start );
01106 if( !parseLogicalOrExpression(node, true) ){
01107 return false;
01108 }
01109
01110 return true;
01111 }
01112
01113 bool Parser::parseTypeSpecifier( TypeSpecifierAST::Node& spec )
01114 {
01115
01116
01117 GroupAST::Node cv;
01118 parseCvQualify( cv );
01119
01120 if( parseElaboratedTypeSpecifier(spec) || parseSimpleTypeSpecifier(spec) ){
01121 spec->setCvQualify( cv );
01122
01123 GroupAST::Node cv2;
01124 parseCvQualify( cv2 );
01125 spec->setCv2Qualify( cv2 );
01126
01127 return true;
01128 }
01129
01130 return false;
01131 }
01132
01133 bool Parser::parseDeclarator( DeclaratorAST::Node& node )
01134 {
01135
01136
01137 int start = lex->index();
01138
01139 DeclaratorAST::Node ast = CreateNode<DeclaratorAST>();
01140
01141 DeclaratorAST::Node decl;
01142 NameAST::Node declId;
01143
01144 AST::Node ptrOp;
01145 while( parsePtrOperator(ptrOp) ){
01146 ast->addPtrOp( ptrOp );
01147 }
01148
01149 if( lex->lookAhead(0) == '(' ){
01150 lex->nextToken();
01151
01152 if( !parseDeclarator(decl) ){
01153 return false;
01154 }
01155 ast->setSubDeclarator( decl );
01156
01157 if( lex->lookAhead(0) != ')'){
01158 return false;
01159 }
01160 lex->nextToken();
01161 } else {
01162
01163 if( lex->lookAhead(0) == ':' ){
01164
01165 } else if( parseDeclaratorId(declId) ){
01166 ast->setDeclaratorId( declId );
01167 } else {
01168 lex->setIndex( start );
01169 return false;
01170 }
01171
01172 if( lex->lookAhead(0) == ':' ){
01173 lex->nextToken();
01174 AST::Node expr;
01175 if( !parseConstantExpression(expr) ){
01176 reportError( i18n("Constant expression expected") );
01177 }
01178 goto update_pos;
01179 }
01180 }
01181
01182 {
01183 bool isVector = true;
01184
01185 while( lex->lookAhead(0) == '[' ){
01186 int startArray = lex->index();
01187 lex->nextToken();
01188 AST::Node expr;
01189 parseCommaExpression( expr );
01190
01191 ADVANCE( ']', "]" );
01192 AST::Node array = CreateNode<AST>();
01193 UPDATE_POS( array, startArray, lex->index() );
01194 ast->addArrayDimension( array );
01195 isVector = true;
01196 }
01197
01198 bool skipParen = false;
01199 if( lex->lookAhead(0) == Token_identifier && lex->lookAhead(1) == '(' && lex->lookAhead(2) == '(' ){
01200 lex->nextToken();
01201 lex->nextToken();
01202 skipParen = true;
01203 }
01204
01205 if( ast->subDeclarator() && (!isVector || lex->lookAhead(0) != '(') ){
01206 lex->setIndex( start );
01207 return false;
01208 }
01209
01210 int index = lex->index();
01211 if( lex->lookAhead(0) == '(' ){
01212 lex->nextToken();
01213
01214 ParameterDeclarationClauseAST::Node params;
01215 if( !parseParameterDeclarationClause(params) ){
01216
01217 lex->setIndex( index );
01218 goto update_pos;
01219 }
01220 ast->setParameterDeclarationClause( params );
01221
01222 if( lex->lookAhead(0) != ')' ){
01223 lex->setIndex( index );
01224 goto update_pos;
01225 }
01226
01227 lex->nextToken();
01228
01229 int startConstant = lex->index();
01230 if( lex->lookAhead(0) == Token_const ){
01231 lex->nextToken();
01232 AST::Node constant = CreateNode<AST>();
01233 UPDATE_POS( constant, startConstant, lex->index() );
01234 ast->setConstant( constant );
01235 }
01236
01237 GroupAST::Node except;
01238 if( parseExceptionSpecification(except) ){
01239 ast->setExceptionSpecification( except );
01240 }
01241 }
01242
01243 if( skipParen ){
01244 if( lex->lookAhead(0) != ')' ){
01245 reportError( i18n("')' expected") );
01246 } else
01247 lex->nextToken();
01248 }
01249
01250 }
01251
01252 update_pos:
01253 UPDATE_POS( ast, start, lex->index() );
01254 node = ast;
01255
01256 return true;
01257 }
01258
01259 bool Parser::parseAbstractDeclarator( DeclaratorAST::Node& node )
01260 {
01261
01262 int start = lex->index();
01263
01264 DeclaratorAST::Node ast = CreateNode<DeclaratorAST>();
01265
01266 DeclaratorAST::Node decl;
01267 NameAST::Node declId;
01268
01269 AST::Node ptrOp;
01270 while( parsePtrOperator(ptrOp) ){
01271 ast->addPtrOp( ptrOp );
01272 }
01273
01274 if( lex->lookAhead(0) == '(' ){
01275 lex->nextToken();
01276
01277 if( !parseAbstractDeclarator(decl) ){
01278 return false;
01279 }
01280 ast->setSubDeclarator( decl );
01281
01282 if( lex->lookAhead(0) != ')'){
01283 return false;
01284 }
01285 lex->nextToken();
01286 }
01287
01288 {
01289
01290 while( lex->lookAhead(0) == '[' ){
01291 int startArray = lex->index();
01292 lex->nextToken();
01293 AST::Node expr;
01294 skipCommaExpression( expr );
01295
01296 ADVANCE( ']', "]" );
01297 AST::Node array = CreateNode<AST>();
01298 UPDATE_POS( array, startArray, lex->index() );
01299 ast->addArrayDimension( array );
01300 }
01301
01302 bool skipParen = false;
01303 if( lex->lookAhead(0) == Token_identifier && lex->lookAhead(1) == '(' && lex->lookAhead(2) == '(' ){
01304 lex->nextToken();
01305 lex->nextToken();
01306 skipParen = true;
01307 }
01308
01309 int index = lex->index();
01310 if( lex->lookAhead(0) == '(' ){
01311 lex->nextToken();
01312
01313 ParameterDeclarationClauseAST::Node params;
01314 if( !parseParameterDeclarationClause(params) ){
01315 lex->setIndex( index );
01316 goto UPDATE_POS;
01317 }
01318 ast->setParameterDeclarationClause( params );
01319
01320 if( lex->lookAhead(0) != ')' ){
01321 lex->setIndex( index );
01322 goto UPDATE_POS;
01323 } else
01324 lex->nextToken();
01325
01326 int startConstant = lex->index();
01327 if( lex->lookAhead(0) == Token_const ){
01328 lex->nextToken();
01329 AST::Node constant = CreateNode<AST>();
01330 UPDATE_POS( constant, startConstant, lex->index() );
01331 ast->setConstant( constant );
01332 }
01333
01334 GroupAST::Node except;
01335 if( parseExceptionSpecification(except) ){
01336 ast->setExceptionSpecification( except );
01337 }
01338 }
01339
01340 if( skipParen ){
01341 if( lex->lookAhead(0) != ')' ){
01342 reportError( i18n("')' expected") );
01343 } else
01344 lex->nextToken();
01345 }
01346
01347 }
01348
01349 UPDATE_POS:
01350 UPDATE_POS( ast, start, lex->index() );
01351 node = ast;
01352
01353 return true;
01354 }
01355
01356
01357 bool Parser::parseEnumSpecifier( TypeSpecifierAST::Node& node )
01358 {
01359
01360
01361 int start = lex->index();
01362
01363 if( lex->lookAhead(0) != Token_enum ){
01364 return false;
01365 }
01366
01367 lex->nextToken();
01368
01369 NameAST::Node name;
01370 parseName( name );
01371
01372 if( lex->lookAhead(0) != '{' ){
01373 lex->setIndex( start );
01374 return false;
01375 }
01376 lex->nextToken();
01377
01378 EnumSpecifierAST::Node ast = CreateNode<EnumSpecifierAST>();
01379 ast->setName( name );
01380
01381 EnumeratorAST::Node enumerator;
01382 if( parseEnumerator(enumerator) ){
01383 ast->addEnumerator( enumerator );
01384
01385 while( lex->lookAhead(0) == ',' ){
01386 lex->nextToken();
01387
01388 if( !parseEnumerator(enumerator) ){
01389
01390 break;
01391 }
01392
01393 ast->addEnumerator( enumerator );
01394 }
01395 }
01396
01397 if( lex->lookAhead(0) != '}' )
01398 reportError( i18n("} missing") );
01399 else
01400 lex->nextToken();
01401
01402 UPDATE_POS( ast, start, lex->index() );
01403 node = ast;
01404
01405 return true;
01406 }
01407
01408 bool Parser::parseTemplateParameterList( TemplateParameterListAST::Node& node )
01409 {
01410
01411
01412 int start = lex->index();
01413
01414 TemplateParameterListAST::Node ast = CreateNode<TemplateParameterListAST>();
01415
01416 TemplateParameterAST::Node param;
01417 if( !parseTemplateParameter(param) ){
01418 return false;
01419 }
01420 ast->addTemplateParameter( param );
01421
01422 while( lex->lookAhead(0) == ',' ){
01423 lex->nextToken();
01424
01425 if( !parseTemplateParameter(param) ){
01426 syntaxError();
01427 break;
01428 } else {
01429 ast->addTemplateParameter( param );
01430 }
01431 }
01432
01433 UPDATE_POS( ast, start, lex->index() );
01434 node = ast;
01435
01436 return true;
01437 }
01438
01439 bool Parser::parseTemplateParameter( TemplateParameterAST::Node& node )
01440 {
01441
01442
01443 int start = lex->index();
01444 TemplateParameterAST::Node ast = CreateNode<TemplateParameterAST>();
01445
01446 TypeParameterAST::Node typeParameter;
01447 ParameterDeclarationAST::Node param;
01448
01449 int tk = lex->lookAhead( 0 );
01450
01451 if( (tk == Token_class || tk == Token_typename || tk == Token_template) && parseTypeParameter(typeParameter) ){
01452 ast->setTypeParameter( typeParameter );
01453 goto ok;
01454 }
01455
01456 if( !parseParameterDeclaration(param) )
01457 return false;
01458 ast->setTypeValueParameter( param );
01459
01460 ok:
01461 UPDATE_POS( ast, start, lex->index() );
01462 node = ast;
01463
01464 return true;
01465 }
01466
01467 bool Parser::parseTypeParameter( TypeParameterAST::Node& node )
01468 {
01469
01470
01471 int start = lex->index();
01472 TypeParameterAST::Node ast = CreateNode<TypeParameterAST>();
01473
01474 AST_FROM_TOKEN( kind, lex->index() );
01475 ast->setKind( kind );
01476
01477 switch( lex->lookAhead(0) ){
01478
01479 case Token_class:
01480 case Token_typename:
01481 {
01482 lex->nextToken();
01483
01484
01485 NameAST::Node name;
01486 if( parseName(name) ){
01487 ast->setName( name );
01488 if( lex->lookAhead(0) == '=' ){
01489 lex->nextToken();
01490
01491 AST::Node typeId;
01492 if( !parseTypeId(typeId) ){
01493 syntaxError();
01494 return false;
01495 }
01496 ast->setTypeId( typeId );
01497 }
01498 }
01499 }
01500 break;
01501
01502 case Token_template:
01503 {
01504 lex->nextToken();
01505 ADVANCE( '<', '<' );
01506
01507 TemplateParameterListAST::Node params;
01508 if( !parseTemplateParameterList(params) ){
01509 return false;
01510 }
01511 ast->setTemplateParameterList( params );
01512
01513 ADVANCE( '>', ">" );
01514
01515 if( lex->lookAhead(0) == Token_class )
01516 lex->nextToken();
01517
01518
01519 NameAST::Node name;
01520 if( parseName(name) ){
01521 ast->setName( name );
01522 if( lex->lookAhead(0) == '=' ){
01523 lex->nextToken();
01524
01525 AST::Node typeId;
01526 if( !parseTypeId(typeId) ){
01527 syntaxError();
01528 return false;
01529 }
01530 ast->setTypeId( typeId );
01531 }
01532 }
01533
01534 if( lex->lookAhead(0) == '=' ){
01535 lex->nextToken();
01536
01537 NameAST::Node templ_name;
01538 parseName( templ_name );
01539 }
01540 }
01541 break;
01542
01543 default:
01544 return false;
01545
01546 }
01547
01548
01549 UPDATE_POS( ast, start, lex->index() );
01550 node = ast;
01551 return true;
01552 }
01553
01554 bool Parser::parseStorageClassSpecifier( GroupAST::Node& node )
01555 {
01556
01557
01558 int start = lex->index();
01559 GroupAST::Node ast = CreateNode<GroupAST>();
01560
01561 while( !lex->lookAhead(0).isNull() ){
01562 int tk = lex->lookAhead( 0 );
01563 if( tk == Token_friend || tk == Token_auto || tk == Token_register || tk == Token_static ||
01564 tk == Token_extern || tk == Token_mutable ){
01565 int startNode = lex->index();
01566 lex->nextToken();
01567
01568 AST::Node n = CreateNode<AST>();
01569 UPDATE_POS( n, startNode, lex->index() );
01570 ast->addNode( n );
01571 } else
01572 break;
01573 }
01574
01575 if( ast->nodeList().count() == 0 )
01576 return false;
01577
01578 UPDATE_POS( ast, start, lex->index() );
01579 node = ast;
01580 return true;
01581 }
01582
01583 bool Parser::parseFunctionSpecifier( GroupAST::Node& node )
01584 {
01585
01586
01587 int start = lex->index();
01588 GroupAST::Node ast = CreateNode<GroupAST>();
01589
01590 while( !lex->lookAhead(0).isNull() ){
01591 int tk = lex->lookAhead( 0 );
01592 if( tk == Token_inline || tk == Token_virtual || tk == Token_explicit ){
01593 int startNode = lex->index();
01594 lex->nextToken();
01595
01596 AST::Node n = CreateNode<AST>();
01597 UPDATE_POS( n, startNode, lex->index() );
01598 ast->addNode( n );
01599 } else {
01600 break;
01601 }
01602 }
01603
01604 if( ast->nodeList().count() == 0 )
01605 return false;
01606
01607 UPDATE_POS( ast, start, lex->index() );
01608 node = ast;
01609 return true;
01610 }
01611
01612 bool Parser::parseTypeId( AST::Node& node )
01613 {
01614
01615
01617 int start = lex->index();
01618 AST::Node ast = CreateNode<AST>();
01619
01620 TypeSpecifierAST::Node spec;
01621 if( !parseTypeSpecifier(spec) ){
01622 return false;
01623 }
01624
01625 DeclaratorAST::Node decl;
01626 parseAbstractDeclarator( decl );
01627
01628 UPDATE_POS( ast, start, lex->index() );
01629 node = ast;
01630
01631 return true;
01632 }
01633
01634 bool Parser::parseInitDeclaratorList( InitDeclaratorListAST::Node& node )
01635 {
01636
01637
01638 int start = lex->index();
01639
01640 InitDeclaratorListAST::Node ast = CreateNode<InitDeclaratorListAST>();
01641 InitDeclaratorAST::Node decl;
01642
01643 if( !parseInitDeclarator(decl) ){
01644 return false;
01645 }
01646 ast->addInitDeclarator( decl );
01647
01648 while( lex->lookAhead(0) == ',' ){
01649 lex->nextToken();
01650
01651 if( !parseInitDeclarator(decl) ){
01652 syntaxError();
01653 break;
01654 }
01655 ast->addInitDeclarator( decl );
01656 }
01657
01658
01659 UPDATE_POS( ast, start, lex->index() );
01660 node = ast;
01661
01662 return true;
01663 }
01664
01665 bool Parser::parseParameterDeclarationClause( ParameterDeclarationClauseAST::Node& node )
01666 {
01667
01668
01669 int start = lex->index();
01670
01671 ParameterDeclarationClauseAST::Node ast = CreateNode<ParameterDeclarationClauseAST>();
01672
01673 ParameterDeclarationListAST::Node params;
01674 if( !parseParameterDeclarationList(params) ){
01675
01676 if ( lex->lookAhead(0) == ')' )
01677 goto good;
01678
01679 if( lex->lookAhead(0) == Token_ellipsis && lex->lookAhead(1) == ')' ){
01680 AST_FROM_TOKEN( ellipsis, lex->index() );
01681 ast->setEllipsis( ellipsis );
01682 lex->nextToken();
01683 goto good;
01684 }
01685 return false;
01686 }
01687
01688 if( lex->lookAhead(0) == Token_ellipsis ){
01689 AST_FROM_TOKEN( ellipsis, lex->index() );
01690 ast->setEllipsis( ellipsis );
01691 lex->nextToken();
01692 }
01693
01694 good:
01695 ast->setParameterDeclarationList( params );
01696
01698 UPDATE_POS( ast, start, lex->index() );
01699 node = ast;
01700
01701 return true;
01702 }
01703
01704 bool Parser::parseParameterDeclarationList( ParameterDeclarationListAST::Node& node )
01705 {
01706
01707
01708 int start = lex->index();
01709
01710 ParameterDeclarationListAST::Node ast = CreateNode<ParameterDeclarationListAST>();
01711
01712 ParameterDeclarationAST::Node param;
01713 if( !parseParameterDeclaration(param) ){
01714 lex->setIndex( start );
01715 return false;
01716 }
01717 ast->addParameter( param );
01718
01719 while( lex->lookAhead(0) == ',' ){
01720 lex->nextToken();
01721
01722 if( lex->lookAhead(0) == Token_ellipsis )
01723 break;
01724
01725 if( !parseParameterDeclaration(param) ){
01726 lex->setIndex( start );
01727 return false;
01728 }
01729 ast->addParameter( param );
01730 }
01731
01732 UPDATE_POS( ast, start, lex->index() );
01733 node = ast;
01734
01735 return true;
01736 }
01737
01738 bool Parser::parseParameterDeclaration( ParameterDeclarationAST::Node& node )
01739 {
01740
01741
01742 int start = lex->index();
01743
01744
01745 TypeSpecifierAST::Node spec;
01746 if( !parseTypeSpecifier(spec) ){
01747 lex->setIndex( start );
01748 return false;
01749 }
01750
01751 int index = lex->index();
01752
01753 DeclaratorAST::Node decl;
01754 if( !parseDeclarator(decl) ){
01755 lex->setIndex( index );
01756
01757
01758 if( !parseAbstractDeclarator(decl) )
01759 return false;
01760 }
01761
01762 AST::Node expr;
01763 if( lex->lookAhead(0) == '=' ){
01764 lex->nextToken();
01765 if( !parseLogicalOrExpression(expr,true) ){
01766
01767 }
01768 }
01769
01770 ParameterDeclarationAST::Node ast = CreateNode<ParameterDeclarationAST>();
01771 ast->setTypeSpec( spec );
01772 ast->setDeclarator( decl );
01773 ast->setExpression( expr );
01774
01775 UPDATE_POS( ast, start, lex->index() );
01776 node = ast;
01777
01778 return true;
01779 }
01780
01781 bool Parser::parseClassSpecifier( TypeSpecifierAST::Node& node )
01782 {
01783
01784
01785 int start = lex->index();
01786
01787 AST::Node classKey;
01788 int classKeyStart = lex->index();
01789
01790 int kind = lex->lookAhead( 0 );
01791 if( kind == Token_class || kind == Token_struct || kind == Token_union ){
01792 AST::Node asn = CreateNode<AST>();
01793 classKey = asn;
01794 lex->nextToken();
01795 UPDATE_POS( classKey, classKeyStart, lex->index() );
01796 } else {
01797 return false;
01798 }
01799
01800 GroupAST::Node winDeclSpec;
01801 parseWinDeclSpec( winDeclSpec );
01802
01803 while( lex->lookAhead(0) == Token_identifier && lex->lookAhead(1) == Token_identifier )
01804 lex->nextToken();
01805
01806 NameAST::Node name;
01807 parseName( name );
01808
01809 BaseClauseAST::Node bases;
01810 if( lex->lookAhead(0) == ':' ){
01811 if( !parseBaseClause(bases) ){
01812 skipUntil( '{' );
01813 }
01814 }
01815
01816 if( lex->lookAhead(0) != '{' ){
01817 lex->setIndex( start );
01818 return false;
01819 }
01820
01821 ADVANCE( '{', '{' );
01822
01823 ClassSpecifierAST::Node ast = CreateNode<ClassSpecifierAST>();
01824 ast->setWinDeclSpec( winDeclSpec );
01825 ast->setClassKey( classKey );
01826 ast->setName( name );
01827 ast->setBaseClause( bases );
01828
01829 while( !lex->lookAhead(0).isNull() ){
01830 if( lex->lookAhead(0) == '}' )
01831 break;
01832
01833 DeclarationAST::Node memSpec;
01834 int startDecl = lex->index();
01835 if( !parseMemberSpecification(memSpec) ){
01836 if( startDecl == lex->index() )
01837 lex->nextToken();
01838 skipUntilDeclaration();
01839 } else
01840 ast->addDeclaration( memSpec );
01841 }
01842
01843 if( lex->lookAhead(0) != '}' ){
01844 reportError( i18n("} missing") );
01845 } else
01846 lex->nextToken();
01847
01848 UPDATE_POS( ast, start, lex->index() );
01849 node = ast;
01850
01851 return true;
01852 }
01853
01854 bool Parser::parseAccessSpecifier( AST::Node& node )
01855 {
01856
01857
01858 int start = lex->index();
01859
01860 switch( lex->lookAhead(0) ){
01861 case Token_public:
01862 case Token_protected:
01863 case Token_private: {
01864 AST::Node asn = CreateNode<AST>();
01865 node = asn;
01866 lex->nextToken();
01867 UPDATE_POS( node, start, lex->index() );
01868 return true;
01869 }
01870 }
01871
01872 return false;
01873 }
01874
01875 bool Parser::parseMemberSpecification( DeclarationAST::Node& node )
01876 {
01877
01878
01879 int start = lex->index();
01880
01881 AST::Node access;
01882
01883 if( lex->lookAhead(0) == ';' ){
01884 lex->nextToken();
01885 return true;
01886 } else if( lex->lookAhead(0) == Token_Q_OBJECT || lex->lookAhead(0) == Token_K_DCOP ){
01887 lex->nextToken();
01888 return true;
01889 } else if( lex->lookAhead(0) == Token_signals || lex->lookAhead(0) == Token_k_dcop || lex->lookAhead(0) == Token_k_dcop_signals ){
01890 AccessDeclarationAST::Node ast = CreateNode<AccessDeclarationAST>();
01891 lex->nextToken();
01892 AST::Node n = CreateNode<AST>();
01893 UPDATE_POS( n, start, lex->index() );
01894 ast->addAccess( n );
01895 ADVANCE( ':', ":" );
01896 UPDATE_POS( ast, start, lex->index() );
01897 node = ast;
01898 return true;
01899 } else if( parseTypedef(node) ){
01900 return true;
01901 } else if( parseUsing(node) ){
01902 return true;
01903 } else if( parseTemplateDeclaration(node) ){
01904 return true;
01905 } else if( parseAccessSpecifier(access) ){
01906 AccessDeclarationAST::Node ast = CreateNode<AccessDeclarationAST>();
01907 ast->addAccess( access );
01908
01909 int startSlot = lex->index();
01910 if( lex->lookAhead(0) == Token_slots ){
01911 lex->nextToken();
01912 AST::Node sl = CreateNode<AST>();
01913 UPDATE_POS( sl, startSlot, lex->index() );
01914 ast->addAccess( sl );
01915 }
01916 ADVANCE( ':', ":" );
01917 UPDATE_POS( ast, start, lex->index() );
01918 node = ast;
01919 return true;
01920 }
01921
01922 lex->setIndex( start );
01923
01924 GroupAST::Node storageSpec;
01925 parseStorageClassSpecifier( storageSpec );
01926
01927 GroupAST::Node cv;
01928 parseCvQualify( cv );
01929
01930 TypeSpecifierAST::Node spec;
01931 if( parseEnumSpecifier(spec) || parseClassSpecifier(spec) ){
01932 spec->setCvQualify( cv );
01933
01934 GroupAST::Node cv2;
01935 parseCvQualify( cv2 );
01936 spec->setCv2Qualify( cv2 );
01937
01938 InitDeclaratorListAST::Node declarators;
01939 parseInitDeclaratorList( declarators );
01940 ADVANCE( ';', ";" );
01941
01942 SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
01943 ast->setTypeSpec( spec );
01944 ast->setInitDeclaratorList( declarators );
01945 UPDATE_POS( ast, start, lex->index() );
01946 node = ast;
01947
01948 return true;
01949 }
01950
01951 lex->setIndex( start );
01952 return parseDeclarationInternal( node );
01953 }
01954
01955 bool Parser::parseCtorInitializer( AST::Node& )
01956 {
01957
01958
01959 if( lex->lookAhead(0) != ':' ){
01960 return false;
01961 }
01962 lex->nextToken();
01963
01964 AST::Node inits;
01965 if( !parseMemInitializerList(inits) ){
01966 reportError( i18n("Member initializers expected") );
01967 }
01968
01969 return true;
01970 }
01971
01972 bool Parser::parseElaboratedTypeSpecifier( TypeSpecifierAST::Node& node )
01973 {
01974
01975
01976 int start = lex->index();
01977
01978 int tk = lex->lookAhead( 0 );
01979 if( tk == Token_class ||
01980 tk == Token_struct ||
01981 tk == Token_union ||
01982 tk == Token_enum ||
01983 tk == Token_typename )
01984 {
01985 AST::Node kind = CreateNode<AST>();
01986 lex->nextToken();
01987 UPDATE_POS( kind, start, lex->index() );
01988
01989 NameAST::Node name;
01990
01991 if( parseName(name) ){
01992 ElaboratedTypeSpecifierAST::Node ast = CreateNode<ElaboratedTypeSpecifierAST>();
01993 ast->setKind( kind );
01994 ast->setName( name );
01995 UPDATE_POS( ast, start, lex->index() );
01996 node = ast;
01997
01998 return true;
01999 }
02000 }
02001
02002 lex->setIndex( start );
02003 return false;
02004 }
02005
02006 bool Parser::parseDeclaratorId( NameAST::Node& node )
02007 {
02008
02009 return parseName( node );
02010 }
02011
02012 bool Parser::parseExceptionSpecification( GroupAST::Node& node )
02013 {
02014
02015
02016 if( lex->lookAhead(0) != Token_throw ){
02017 return false;
02018 }
02019 lex->nextToken();
02020
02021 ADVANCE( '(', "(" );
02022 if( lex->lookAhead(0) == Token_ellipsis ){
02023
02024 int start = lex->index();
02025 GroupAST::Node ast = CreateNode<GroupAST>();
02026 AST_FROM_TOKEN( ellipsis, lex->index() );
02027 ast->addNode( ellipsis );
02028 lex->nextToken();
02029 UPDATE_POS( ast, start, lex->index() );
02030 node = ast;
02031 } else {
02032 parseTypeIdList( node );
02033 }
02034 ADVANCE( ')', ")" );
02035
02036 return true;
02037 }
02038
02039 bool Parser::parseEnumerator( EnumeratorAST::Node& node )
02040 {
02041
02042
02043 int start = lex->index();
02044
02045 if( lex->lookAhead(0) != Token_identifier ){
02046 return false;
02047 }
02048 lex->nextToken();
02049
02050 EnumeratorAST::Node ena = CreateNode<EnumeratorAST>();
02051 node = ena;
02052
02053 AST::Node id = CreateNode<AST>();
02054 UPDATE_POS( id, start, lex->index() );
02055 node->setId( id );
02056
02057 if( lex->lookAhead(0) == '=' ){
02058 lex->nextToken();
02059
02060 AST::Node expr;
02061 if( !parseConstantExpression(expr) ){
02062 reportError( i18n("Constant expression expected") );
02063 }
02064 node->setExpr( expr );
02065 }
02066
02067 UPDATE_POS( node, start, lex->index() );
02068
02069 return true;
02070 }
02071
02072 bool Parser::parseInitDeclarator( InitDeclaratorAST::Node& node )
02073 {
02074
02075
02076 int start = lex->index();
02077
02078 DeclaratorAST::Node decl;
02079 AST::Node init;
02080 if( !parseDeclarator(decl) ){
02081 return false;
02082 }
02083
02084 parseInitializer( init );
02085
02086 InitDeclaratorAST::Node ast = CreateNode<InitDeclaratorAST>();
02087 ast->setDeclarator( decl );
02088 ast->setInitializer( init );
02089 UPDATE_POS( ast, start, lex->index() );
02090 node = ast;
02091
02092 return true;
02093 }
02094
02095
02096
02097 bool Parser::parseBaseClause( BaseClauseAST::Node& node )
02098 {
02099
02100
02101 int start = lex->index();
02102 if( lex->lookAhead(0) != ':' ){
02103 return false;
02104 }
02105 lex->nextToken();
02106
02107 BaseClauseAST::Node bca = CreateNode<BaseClauseAST>();
02108
02109 BaseSpecifierAST::Node baseSpec;
02110 if( parseBaseSpecifier(baseSpec) ){
02111 bca->addBaseSpecifier( baseSpec );
02112
02113 while( lex->lookAhead(0) == ',' ){
02114 lex->nextToken();
02115
02116 if( !parseBaseSpecifier(baseSpec) ){
02117 reportError( i18n("Base class specifier expected") );
02118 return false;
02119 }
02120 bca->addBaseSpecifier( baseSpec );
02121 }
02122 } else
02123 return false;
02124
02125 UPDATE_POS( bca, start, lex->index() );
02126 node = bca;
02127
02128 return true;
02129 }
02130
02131 bool Parser::parseInitializer( AST::Node& node )
02132 {
02133
02134
02135 if( lex->lookAhead(0) == '=' ){
02136 lex->nextToken();
02137
02138 AST::Node init;
02139 if( !parseInitializerClause(node) ){
02140 reportError( i18n("Initializer clause expected") );
02141 return false;
02142 }
02143 } else if( lex->lookAhead(0) == '(' ){
02144 lex->nextToken();
02145 AST::Node expr;
02146 skipCommaExpression( expr );
02147
02148 ADVANCE( ')', ")" );
02149 }
02150
02151 return false;
02152 }
02153
02154 bool Parser::parseMemInitializerList( AST::Node& )
02155 {
02156
02157
02158 AST::Node init;
02159 if( !parseMemInitializer(init) ){
02160 return false;
02161 }
02162
02163 while( lex->lookAhead(0) == ',' ){
02164 lex->nextToken();
02165
02166 if( parseMemInitializer(init) ){
02167 } else {
02168 break;
02169 }
02170 }
02171
02172 return true;
02173 }
02174
02175 bool Parser::parseMemInitializer( AST::Node& )
02176 {
02177
02178
02179 NameAST::Node initId;
02180 if( !parseMemInitializerId(initId) ){
02181 reportError( i18n("Identifier expected") );
02182 return false;
02183 }
02184 ADVANCE( '(', '(' );
02185 AST::Node expr;
02186 skipCommaExpression( expr );
02187 ADVANCE( ')', ')' );
02188
02189 return true;
02190 }
02191
02192 bool Parser::parseTypeIdList( GroupAST::Node& node )
02193 {
02194
02195
02196 int start = lex->index();
02197
02198 AST::Node typeId;
02199 if( !parseTypeId(typeId) ){
02200 return false;
02201 }
02202
02203 GroupAST::Node ast = CreateNode<GroupAST>();
02204 ast->addNode( typeId );
02205
02206 while( lex->lookAhead(0) == ',' ){
02207 lex->nextToken();
02208 if( parseTypeId(typeId) ){
02209 ast->addNode( typeId );
02210 } else {
02211 reportError( i18n("Type id expected") );
02212 break;
02213 }
02214 }
02215
02216 UPDATE_POS( ast, start, lex->index() );
02217 node = ast;
02218 return true;
02219 }
02220
02221 bool Parser::parseBaseSpecifier( BaseSpecifierAST::Node& node )
02222 {
02223
02224
02225 int start = lex->index();
02226 BaseSpecifierAST::Node ast = CreateNode<BaseSpecifierAST>();
02227
02228 AST::Node access;
02229 if( lex->lookAhead(0) == Token_virtual ){
02230 AST_FROM_TOKEN( virt, lex->index() );
02231 ast->setIsVirtual( virt );
02232
02233 lex->nextToken();
02234
02235 parseAccessSpecifier( access );
02236 } else {
02237 parseAccessSpecifier( access );
02238
02239 if( lex->lookAhead(0) == Token_virtual ){
02240 AST_FROM_TOKEN( virt, lex->index() );
02241 ast->setIsVirtual( virt );
02242 lex->nextToken();
02243 }
02244 }
02245
02246 NameAST::Node name;
02247 if( !parseName(name) ){
02248 reportError( i18n("Class name expected") );
02249 }
02250
02251 ast->setAccess( access );
02252 ast->setName( name );
02253 UPDATE_POS( ast, start, lex->index() );
02254 node = ast;
02255
02256 return true;
02257 }
02258
02259
02260 bool Parser::parseInitializerClause( AST::Node& node )
02261 {
02262
02263
02264 if( lex->lookAhead(0) == '{' ){
02265 if( !skip('{','}') ){
02266 reportError( i18n("} missing") );
02267 } else
02268 lex->nextToken();
02269 } else {
02270 if( !parseAssignmentExpression(node) ){
02271
02272 }
02273 }
02274
02275 return true;
02276 }
02277
02278 bool Parser::parseMemInitializerId( NameAST::Node& node )
02279 {
02280
02281
02282 return parseName( node );
02283 }
02284
02285 bool Parser::parsePtrToMember( AST::Node& )
02286 {
02287
02288
02289 if( lex->lookAhead(0) == Token_scope ){
02290 lex->nextToken();
02291 }
02292
02293 while( lex->lookAhead(0) == Token_identifier ){
02294 lex->nextToken();
02295
02296 if( lex->lookAhead(0) == Token_scope && lex->lookAhead(1) == '*' ){
02297 lex->nextToken();
02298 lex->nextToken();
02299 return true;
02300 } else
02301 break;
02302 }
02303
02304 return false;
02305 }
02306
02307 bool Parser::parseUnqualifiedName( ClassOrNamespaceNameAST::Node& node )
02308 {
02309
02310
02311 int start = lex->index();
02312 bool isDestructor = false;
02313
02314 ClassOrNamespaceNameAST::Node ast = CreateNode<ClassOrNamespaceNameAST>();
02315
02316 if( lex->lookAhead(0) == Token_identifier ){
02317 int startName = lex->index();
02318 AST::Node n = CreateNode<AST>();
02319 lex->nextToken();
02320 UPDATE_POS( n, startName, lex->index() );
02321 ast->setName( n );
02322 } else if( lex->lookAhead(0) == '~' && lex->lookAhead(1) == Token_identifier ){
02323 int startName = lex->index();
02324 AST::Node n = CreateNode<AST>();
02325 lex->nextToken();
02326 lex->nextToken();
02327 UPDATE_POS( n, startName, lex->index() );
02328 ast->setName( n );
02329 isDestructor = true;
02330 } else if( lex->lookAhead(0) == Token_operator ){
02331 AST::Node n;
02332 if( !parseOperatorFunctionId(n) )
02333 return false;
02334 ast->setName( n );
02335 } else {
02336 return false;
02337 }
02338
02339 if( !isDestructor ){
02340
02341 int index = lex->index();
02342
02343 if( lex->lookAhead(0) == '<' ){
02344 lex->nextToken();
02345
02346
02347 TemplateArgumentListAST::Node args;
02348 parseTemplateArgumentList( args );
02349
02350 if( lex->lookAhead(0) != '>' ){
02351 lex->setIndex( index );
02352 } else {
02353 lex->nextToken();
02354 ast->setTemplateArgumentList( args );
02355 }
02356 }
02357 }
02358
02359 UPDATE_POS( ast, start, lex->index() );
02360 node = ast;
02361
02362 return true;
02363 }
02364
02365 bool Parser::parseStringLiteral( AST::Node& )
02366 {
02367 while( !lex->lookAhead(0).isNull() ) {
02368 if( lex->lookAhead(0) == Token_identifier &&
02369 lex->lookAhead(0).text() == "L" && lex->lookAhead(1) == Token_string_literal ) {
02370
02371 lex->nextToken();
02372 lex->nextToken();
02373 } else if( lex->lookAhead(0) == Token_string_literal ) {
02374 lex->nextToken();
02375 } else
02376 return false;
02377 }
02378 return true;
02379 }
02380
02381 bool Parser::skipExpressionStatement( StatementAST::Node& node )
02382 {
02383
02384
02385 int start = lex->index();
02386
02387 AST::Node expr;
02388 skipCommaExpression( expr );
02389
02390 ADVANCE( ';', ";" );
02391
02392 ExpressionStatementAST::Node ast = CreateNode<ExpressionStatementAST>();
02393 ast->setExpression( expr );
02394 UPDATE_POS( ast, start, lex->index() );
02395 node = ast;
02396
02397 return true;
02398 }
02399
02400 bool Parser::parseStatement( StatementAST::Node& node )
02401 {
02402
02403 switch( lex->lookAhead(0) ){
02404
02405 case Token_while:
02406 return parseWhileStatement( node );
02407
02408 case Token_do:
02409 return parseDoStatement( node );
02410
02411 case Token_for:
02412 return parseForStatement( node );
02413
02414 case Token_if:
02415 return parseIfStatement( node );
02416
02417 case Token_switch:
02418 return parseSwitchStatement( node );
02419
02420 case Token_try:
02421 return parseTryBlockStatement( node );
02422
02423 case Token_case:
02424 case Token_default:
02425 return parseLabeledStatement( node );
02426
02427 case Token_break:
02428 case Token_continue:
02429 lex->nextToken();
02430 ADVANCE( ';', ";" );
02431 return true;
02432
02433 case Token_goto:
02434 lex->nextToken();
02435 ADVANCE( Token_identifier, "identifier" );
02436 ADVANCE( ';', ";" );
02437 return true;
02438
02439 case Token_return:
02440 {
02441 lex->nextToken();
02442 AST::Node expr;
02443 skipCommaExpression( expr );
02444 ADVANCE( ';', ";" );
02445 }
02446 return true;
02447
02448 case '{':
02449 return parseCompoundStatement( node );
02450
02451 case Token_identifier:
02452 if( parseLabeledStatement(node) )
02453 return true;
02454 break;
02455 }
02456
02457
02458 if ( parseDeclarationStatement(node) )
02459 return true;
02460
02461 return skipExpressionStatement( node );
02462 }
02463
02464 bool Parser::parseCondition( ConditionAST::Node& node )
02465 {
02466
02467
02468 int start = lex->index();
02469
02470 ConditionAST::Node ast = CreateNode<ConditionAST>();
02471
02472 TypeSpecifierAST::Node spec;
02473 if( parseTypeSpecifier(spec) ){
02474 DeclaratorAST::Node decl;
02475 if( parseDeclarator(decl) && lex->lookAhead(0) == '=' ) {
02476 lex->nextToken();
02477
02478 AST::Node expr;
02479 if( skipExpression(expr) ){
02480 ast->setTypeSpec( spec );
02481 ast->setDeclarator( decl );
02482 ast->setExpression( expr );
02483
02484 UPDATE_POS( ast, start, lex->index() );
02485 node = ast;
02486
02487 return true;
02488 }
02489 }
02490 }
02491
02492 lex->setIndex( start );
02493
02494 AST::Node expr;
02495 if( !skipCommaExpression(expr) )
02496 return false;
02497
02498 ast->setExpression( expr );
02499 UPDATE_POS( ast, start, lex->index() );
02500 node = ast;
02501 return true;
02502 }
02503
02504
02505 bool Parser::parseWhileStatement( StatementAST::Node& node )
02506 {
02507
02508 int start = lex->index();
02509
02510 ADVANCE( Token_while, "while" );
02511 ADVANCE( '(' , "(" );
02512
02513 ConditionAST::Node cond;
02514 if( !parseCondition(cond) ){
02515 reportError( i18n("condition expected") );
02516 return false;
02517 }
02518 ADVANCE( ')', ")" );
02519
02520 StatementAST::Node body;
02521 if( !parseStatement(body) ){
02522 reportError( i18n("statement expected") );
02523 return false;
02524 }
02525
02526 WhileStatementAST::Node ast = CreateNode<WhileStatementAST>();
02527 ast->setCondition( cond );
02528 ast->setStatement( body );
02529 UPDATE_POS( ast, start, lex->index() );
02530 node = ast;
02531
02532 return true;
02533 }
02534
02535 bool Parser::parseDoStatement( StatementAST::Node& node )
02536 {
02537
02538 int start = lex->index();
02539
02540 ADVANCE( Token_do, "do" );
02541
02542 StatementAST::Node body;
02543 if( !parseStatement(body) ){
02544 reportError( i18n("statement expected") );
02545
02546 }
02547
02548 ADVANCE_NR( Token_while, "while" );
02549 ADVANCE_NR( '(' , "(" );
02550
02551 AST::Node expr;
02552 if( !skipCommaExpression(expr) ){
02553 reportError( i18n("expression expected") );
02554
02555 }
02556
02557 ADVANCE_NR( ')', ")" );
02558 ADVANCE_NR( ';', ";" );
02559
02560 DoStatementAST::Node ast = CreateNode<DoStatementAST>();
02561 ast->setStatement( body );
02562
02563 UPDATE_POS( ast, start, lex->index() );
02564 node = ast;
02565
02566 return true;
02567 }
02568
02569 bool Parser::parseForStatement( StatementAST::Node& node )
02570 {
02571
02572 int start = lex->index();
02573
02574 ADVANCE( Token_for, "for" );
02575 ADVANCE( '(', "(" );
02576
02577 StatementAST::Node init;
02578 if( !parseForInitStatement(init) ){
02579 reportError( i18n("for initialization expected") );
02580 return false;
02581 }
02582
02583 ConditionAST::Node cond;
02584 parseCondition( cond );
02585 ADVANCE( ';', ";" );
02586
02587 AST::Node expr;
02588 skipCommaExpression( expr );
02589 ADVANCE( ')', ")" );
02590
02591 StatementAST::Node body;
02592 if( !parseStatement(body) )
02593 return false;
02594
02595 ForStatementAST::Node ast = CreateNode<ForStatementAST>();
02596 ast->setInitStatement( init );
02597 ast->setCondition( cond );
02598
02599 ast->setStatement( body );
02600 UPDATE_POS( ast, start, lex->index() );
02601 node = ast;
02602
02603 return true;
02604 }
02605
02606 bool Parser::parseForInitStatement( StatementAST::Node& node )
02607 {
02608
02609
02610 if ( parseDeclarationStatement(node) )
02611 return true;
02612
02613 return skipExpressionStatement( node );
02614 }
02615
02616 bool Parser::parseCompoundStatement( StatementAST::Node& node )
02617 {
02618
02619 int start = lex->index();
02620
02621 if( lex->lookAhead(0) != '{' ){
02622 return false;
02623 }
02624 lex->nextToken();
02625
02626 StatementListAST::Node ast = CreateNode<StatementListAST>();
02627
02628 while( !lex->lookAhead(0).isNull() ){
02629 if( lex->lookAhead(0) == '}' )
02630 break;
02631
02632 StatementAST::Node stmt;
02633 int startStmt = lex->index();
02634 if( !parseStatement(stmt) ){
02635 if( startStmt == lex->index() )
02636 lex->nextToken();
02637 skipUntilStatement();
02638 } else {
02639 ast->addStatement( stmt );
02640 }
02641 }
02642
02643 if( lex->lookAhead(0) != '}' ){
02644 reportError( i18n("} expected") );
02645 } else {
02646 lex->nextToken();
02647 }
02648
02649 UPDATE_POS( ast, start, lex->index() );
02650 node = ast;
02651
02652 return true;
02653 }
02654
02655 bool Parser::parseIfStatement( StatementAST::Node& node )
02656 {
02657
02658
02659 int start = lex->index();
02660
02661 ADVANCE( Token_if, "if" );
02662
02663 ADVANCE( '(' , "(" );
02664
02665 IfStatementAST::Node ast = CreateNode<IfStatementAST>();
02666
02667 ConditionAST::Node cond;
02668 if( !parseCondition(cond) ){
02669 reportError( i18n("condition expected") );
02670 return false;
02671 }
02672 ADVANCE( ')', ")" );
02673
02674 StatementAST::Node stmt;
02675 if( !parseStatement(stmt) ){
02676 reportError( i18n("statement expected") );
02677 return false;
02678 }
02679
02680 ast->setCondition( cond );
02681 ast->setStatement( stmt );
02682
02683 if( lex->lookAhead(0) == Token_else ){
02684 lex->nextToken();
02685 StatementAST::Node elseStmt;
02686 if( !parseStatement(elseStmt) ) {
02687 reportError( i18n("statement expected") );
02688 return false;
02689 }
02690 ast->setElseStatement( elseStmt );
02691 }
02692
02693 UPDATE_POS( ast, start, lex->index() );
02694 node = ast;
02695
02696 return true;
02697 }
02698
02699 bool Parser::parseSwitchStatement( StatementAST::Node& node )
02700 {
02701
02702 int start = lex->index();
02703 ADVANCE( Token_switch, "switch" );
02704
02705 ADVANCE( '(' , "(" );
02706
02707 ConditionAST::Node cond;
02708 if( !parseCondition(cond) ){
02709 reportError( i18n("condition expected") );
02710 return false;
02711 }
02712 ADVANCE( ')', ")" );
02713
02714 StatementAST::Node stmt;
02715 if( !parseCompoundStatement(stmt) ){
02716 syntaxError();
02717 return false;
02718 }
02719
02720 SwitchStatementAST::Node ast = CreateNode<SwitchStatementAST>();
02721 ast->setCondition( cond );
02722 ast->setStatement( stmt );
02723 UPDATE_POS( ast, start, lex->index() );
02724 node = ast;
02725
02726 return true;
02727 }
02728
02729 bool Parser::parseLabeledStatement( StatementAST::Node& node )
02730 {
02731
02732 switch( lex->lookAhead(0) ){
02733 case Token_identifier:
02734 case Token_default:
02735 if( lex->lookAhead(1) == ':' ){
02736 lex->nextToken();
02737 lex->nextToken();
02738
02739 StatementAST::Node stmt;
02740 if( parseStatement(stmt) ){
02741 node = stmt;
02742 return true;
02743 }
02744 }
02745 break;
02746
02747 case Token_case:
02748 {
02749 lex->nextToken();
02750 AST::Node expr;
02751 if( !parseConstantExpression(expr) ){
02752 reportError( i18n("expression expected") );
02753 } else if( lex->lookAhead(0) == Token_ellipsis ){
02754 lex->nextToken();
02755
02756 AST::Node expr2;
02757 if( !parseConstantExpression(expr2) ){
02758 reportError( i18n("expression expected") );
02759 }
02760 }
02761 ADVANCE( ':', ":" );
02762
02763 StatementAST::Node stmt;
02764 if( parseStatement(stmt) ){
02765 node = stmt;
02766 return true;
02767 }
02768 }
02769 break;
02770
02771 }
02772
02773 return false;
02774 }
02775
02776 bool Parser::parseBlockDeclaration( DeclarationAST::Node& node )
02777 {
02778
02779 switch( lex->lookAhead(0) ) {
02780 case Token_typedef:
02781 return parseTypedef( node );
02782 case Token_using:
02783 return parseUsing( node );
02784 case Token_asm:
02785 return parseAsmDefinition( node );
02786 case Token_namespace:
02787 return parseNamespaceAliasDefinition( node );
02788 }
02789
02790 int start = lex->index();
02791
02792 GroupAST::Node storageSpec;
02793 parseStorageClassSpecifier( storageSpec );
02794
02795 GroupAST::Node cv;
02796 parseCvQualify( cv );
02797
02798 TypeSpecifierAST::Node spec;
02799 if ( !parseTypeSpecifierOrClassSpec(spec) ) {
02800 lex->setIndex( start );
02801 return false;
02802 }
02803 spec->setCvQualify( cv );
02804
02805 GroupAST::Node cv2;
02806 parseCvQualify( cv2 );
02807 spec->setCv2Qualify( cv2 );
02808
02809 InitDeclaratorListAST::Node declarators;
02810 parseInitDeclaratorList( declarators );
02811
02812 if( lex->lookAhead(0) != ';' ){
02813 lex->setIndex( start );
02814 return false;
02815 }
02816 lex->nextToken();
02817
02818 SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
02819 ast->setTypeSpec( spec );
02820 ast->setInitDeclaratorList( declarators );
02821 UPDATE_POS( ast, start, lex->index() );
02822 node = ast;
02823
02824 return true;
02825 }
02826
02827 bool Parser::parseNamespaceAliasDefinition( DeclarationAST::Node& )
02828 {
02829 if ( lex->lookAhead(0) != Token_namespace ) {
02830 return false;
02831 }
02832 lex->nextToken();
02833
02834 ADVANCE( Token_identifier, "identifier" );
02835 ADVANCE( '=', "=" );
02836
02837 NameAST::Node name;
02838 if( !parseName(name) ){
02839 reportError( i18n("Namespace name expected") );
02840 }
02841
02842 ADVANCE( ';', ";" );
02843
02844 return true;
02845
02846 }
02847
02848 bool Parser::parseDeclarationStatement( StatementAST::Node& node )
02849 {
02850
02851
02852 int start = lex->index();
02853
02854 DeclarationAST::Node decl;
02855 if ( !parseBlockDeclaration(decl) ){
02856 return false;
02857 }
02858
02859 DeclarationStatementAST::Node ast = CreateNode<DeclarationStatementAST>();
02860 ast->setDeclaration( decl );
02861 UPDATE_POS( ast, start, lex->index() );
02862 node = ast;
02863
02864
02865 return true;
02866 }
02867
02868 bool Parser::parseDeclarationInternal( DeclarationAST::Node& node )
02869 {
02870
02871
02872 int start = lex->index();
02873
02874
02875
02876 GroupAST::Node winDeclSpec;
02877 parseWinDeclSpec( winDeclSpec );
02878
02879 GroupAST::Node funSpec;
02880 bool hasFunSpec = parseFunctionSpecifier( funSpec );
02881
02882 GroupAST::Node storageSpec;
02883 bool hasStorageSpec = parseStorageClassSpecifier( storageSpec );
02884
02885 if( hasStorageSpec && !hasFunSpec )
02886 hasFunSpec = parseFunctionSpecifier( funSpec );
02887
02888
02889 GroupAST::Node winDeclSpec2;
02890 parseWinDeclSpec( winDeclSpec2 );
02891
02892 GroupAST::Node cv;
02893 parseCvQualify( cv );
02894
02895 int index = lex->index();
02896 NameAST::Node name;
02897 if( parseName(name) && lex->lookAhead(0) == '(' ){
02898
02899
02900 lex->setIndex( index );
02901
02902 InitDeclaratorAST::Node declarator;
02903 if( parseInitDeclarator(declarator) ){
02904 int endSignature = lex->index();
02905
02906 switch( lex->lookAhead(0) ){
02907 case ';':
02908 {
02909 lex->nextToken();
02910
02911 InitDeclaratorListAST::Node declarators = CreateNode<InitDeclaratorListAST>();
02912
02913
02914 int startLine, startColumn, endLine, endColumn;
02915 if( declarator.get() ){
02916 declarator->getStartPosition( &startLine, &startColumn );
02917 declarator->getEndPosition( &endLine, &endColumn );
02918 declarators->setStartPosition( startLine, startColumn );
02919 declarators->setEndPosition( endLine, endColumn );
02920 }
02921 declarators->addInitDeclarator( declarator );
02922
02923 SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
02924 ast->setInitDeclaratorList( declarators );
02925 ast->setText( toString(start, endSignature) );
02926 node = ast;
02927 UPDATE_POS( node, start, lex->index() );
02928 return true;
02929
02930 }
02931 break;
02932
02933 case ':':
02934 {
02935 AST::Node ctorInit;
02936 StatementListAST::Node funBody;
02937 if( parseCtorInitializer(ctorInit) && parseFunctionBody(funBody) ){
02938 FunctionDefinitionAST::Node ast = CreateNode<FunctionDefinitionAST>();
02939 ast->setStorageSpecifier( storageSpec );
02940 ast->setFunctionSpecifier( funSpec );
02941 ast->setInitDeclarator( declarator );
02942 ast->setFunctionBody( funBody );
02943 ast->setText( toString(start, endSignature) );
02944 node = ast;
02945 UPDATE_POS( node, start, lex->index() );
02946 return true;
02947 }
02948 }
02949 break;
02950
02951 case '{':
02952 {
02953 StatementListAST::Node funBody;
02954 if( parseFunctionBody(funBody) ){
02955 FunctionDefinitionAST::Node ast = CreateNode<FunctionDefinitionAST>();
02956 ast->setStorageSpecifier( storageSpec );
02957 ast->setFunctionSpecifier( funSpec );
02958 ast->setInitDeclarator( declarator );
02959 ast->setText( toString(start, endSignature) );
02960 ast->setFunctionBody( funBody );
02961 node = ast;
02962 UPDATE_POS( node, start, lex->index() );
02963 return true;
02964 }
02965 }
02966 break;
02967
02968 case '(':
02969 case '[':
02970
02971 goto start_decl;
02972 break;
02973 }
02974
02975 }
02976
02977 syntaxError();
02978 return false;
02979 }
02980
02981 start_decl:
02982 lex->setIndex( index );
02983
02984 if( lex->lookAhead(0) == Token_const && lex->lookAhead(1) == Token_identifier && lex->lookAhead(2) == '=' ){
02985
02986 lex->nextToken();
02987 InitDeclaratorListAST::Node declarators;
02988 if( parseInitDeclaratorList(declarators) ){
02989 ADVANCE( ';', ";" );
02990 DeclarationAST::Node ast = CreateNode<DeclarationAST>();
02991 node = ast;
02992 UPDATE_POS( node, start, lex->index() );
02993 return true;
02994 }
02995 syntaxError();
02996 return false;
02997 }
02998
02999 TypeSpecifierAST::Node spec;
03000 if( parseTypeSpecifier(spec) ){
03001 if ( !hasFunSpec )
03002 parseFunctionSpecifier( funSpec );
03003 spec->setCvQualify( cv );
03004
03005 InitDeclaratorListAST::Node declarators;
03006
03007 InitDeclaratorAST::Node decl;
03008 int startDeclarator = lex->index();
03009 bool maybeFunctionDefinition = false;
03010
03011 if( lex->lookAhead(0) != ';' ){
03012 if( parseInitDeclarator(decl) && lex->lookAhead(0) == '{' ){
03013
03014 maybeFunctionDefinition = true;
03015 } else {
03016 lex->setIndex( startDeclarator );
03017 if( !parseInitDeclaratorList(declarators) ){
03018 syntaxError();
03019 return false;
03020 }
03021 }
03022 }
03023
03024 int endSignature = lex->index();
03025 switch( lex->lookAhead(0) ){
03026 case ';':
03027 {
03028 lex->nextToken();
03029 SimpleDeclarationAST::Node ast = CreateNode<SimpleDeclarationAST>();
03030 ast->setStorageSpecifier( storageSpec );
03031 ast->setFunctionSpecifier( funSpec );
03032 ast->setText( toString(start, endSignature) );
03033 ast->setTypeSpec( spec );
03034 ast->setWinDeclSpec( winDeclSpec );
03035 ast->setInitDeclaratorList( declarators );
03036 node = ast;
03037 UPDATE_POS( node, start, lex->index() );
03038 }
03039 return true;
03040
03041 case '{':
03042 {
03043 if( !maybeFunctionDefinition ){
03044 syntaxError();
03045 return false;
03046 }
03047 StatementListAST::Node funBody;
03048 if ( parseFunctionBody(funBody) ) {
03049 FunctionDefinitionAST::Node ast = CreateNode<FunctionDefinitionAST>();
03050 ast->setWinDeclSpec( winDeclSpec );
03051 ast->setStorageSpecifier( storageSpec );
03052 ast->setFunctionSpecifier( funSpec );
03053 ast->setText( toString(start, endSignature) );
03054 ast->setTypeSpec( spec );
03055 ast->setFunctionBody( funBody );
03056 ast->setInitDeclarator( decl );
03057 node = ast;
03058 UPDATE_POS( node, start, lex->index() );
03059 return true;
03060 }
03061 }
03062 break;
03063
03064 }
03065 }
03066
03067 syntaxError();
03068 return false;
03069 }
03070
03071 bool Parser::parseFunctionBody( StatementListAST::Node& node )
03072 {
03073
03074
03075 int start = lex->index();
03076 if( lex->lookAhead(0) != '{' ){
03077 return false;
03078 }
03079 lex->nextToken();
03080
03081 StatementListAST::Node ast = CreateNode<StatementListAST>();
03082
03083 while( !lex->lookAhead(0).isNull() ){
03084 if( lex->lookAhead(0) == '}' )
03085 break;
03086
03087 StatementAST::Node stmt;
03088 int startStmt = lex->index();
03089 if( !parseStatement(stmt) ){
03090 if( startStmt == lex->index() )
03091 lex->nextToken();
03092 skipUntilStatement();
03093 } else
03094 ast->addStatement( stmt );
03095 }
03096
03097 if( lex->lookAhead(0) != '}' ){
03098 reportError( i18n("} expected") );
03099 } else
03100 lex->nextToken();
03101
03102 UPDATE_POS( ast, start, lex->index() );
03103 node = ast;
03104
03105 return true;
03106 }
03107
03108 QString Parser::toString( int start, int end, const QString& sep ) const
03109 {
03110 QStringList l;
03111
03112 for( int i=start; i<end; ++i ){
03113 l << lex->tokenAt(i).text();
03114 }
03115
03116 return l.join( sep ).stripWhiteSpace();
03117 }
03118
03119 bool Parser::parseTypeSpecifierOrClassSpec( TypeSpecifierAST::Node& node )
03120 {
03121 if( parseClassSpecifier(node) )
03122 return true;
03123 else if( parseEnumSpecifier(node) )
03124 return true;
03125 else if( parseTypeSpecifier(node) )
03126 return true;
03127
03128 return false;
03129 }
03130
03131 bool Parser::parseTryBlockStatement( StatementAST::Node& node )
03132 {
03133
03134
03135 if( lex->lookAhead(0) != Token_try ){
03136 return false;
03137 }
03138 lex->nextToken();
03139
03140 StatementAST::Node stmt;
03141 if( !parseCompoundStatement(stmt) ){
03142 syntaxError();
03143 return false;
03144 }
03145
03146 if( lex->lookAhead(0) != Token_catch ){
03147 reportError( i18n("catch expected") );
03148 return false;
03149 }
03150
03151 while( lex->lookAhead(0) == Token_catch ){
03152 lex->nextToken();
03153 ADVANCE( '(', "(" );
03154 ConditionAST::Node cond;
03155 if( !parseCondition(cond) ){
03156 reportError( i18n("condition expected") );
03157 return false;
03158 }
03159 ADVANCE( ')', ")" );
03160
03161 StatementAST::Node body;
03162 if( !parseCompoundStatement(body) ){
03163 syntaxError();
03164 return false;
03165 }
03166 }
03167
03168 node = stmt;
03169 return true;
03170 }
03171
03172 bool Parser::parsePrimaryExpression( AST::Node& )
03173 {
03174
03175
03176
03177 switch( lex->lookAhead(0) ){
03178 case Token_string_literal:
03179 {
03180 AST::Node lit;
03181 parseStringLiteral( lit );
03182 }
03183 return true;
03184
03185 case Token_number_literal:
03186 case Token_char_literal:
03187 case Token_true:
03188 case Token_false:
03189 lex->nextToken();
03190 return true;
03191
03192 case Token_this:
03193 lex->nextToken();
03194 return true;
03195
03196 case Token_dynamic_cast:
03197 case Token_static_cast:
03198 case Token_reinterpret_cast:
03199 case Token_const_cast:
03200 {
03201 lex->nextToken();
03202
03203 CHECK( '<', "<" );
03204 AST::Node typeId;
03205 parseTypeId( typeId );
03206 CHECK( '>', ">" );
03207
03208 CHECK( '(', "(" );
03209 AST::Node expr;
03210 parseCommaExpression( expr );
03211 CHECK( ')', ")" );
03212 }
03213 return true;
03214
03215 case Token_typeid:
03216 {
03217 lex->nextToken();
03218 CHECK( '(', "(" );
03219 AST::Node expr;
03220 parseCommaExpression( expr );
03221 CHECK( ')', ")" );
03222 }
03223 return true;
03224
03225 case '(':
03226 {
03227 lex->nextToken();
03228
03229 AST::Node expr;
03230 if( !parseExpression(expr) ){
03231 return false;
03232 }
03233 CHECK( ')', ")" );
03234 }
03235 return true;
03236
03237 default:
03238 {
03239 int start = lex->index();
03240 TypeSpecifierAST::Node typeSpec;
03241 if( parseSimpleTypeSpecifier(typeSpec) && lex->lookAhead(0) == '(' ){
03242 lex->nextToken();
03243 AST::Node expr;
03244 parseCommaExpression( expr );
03245 CHECK( ')', ")" );
03246 return true;
03247 }
03248
03249 lex->setIndex( start );
03250 NameAST::Node name;
03251 if( parseName(name) )
03252 return true;
03253 }
03254 }
03255
03256 return false;
03257 }
03258
03259 bool Parser::parsePostfixExpression( AST::Node& )
03260 {
03261
03262
03263 AST::Node expr;
03264 if( !parsePrimaryExpression(expr) )
03265 return false;
03266
03267 while( true ){
03268 switch(lex->lookAhead(0))
03269 {
03270 case '[':
03271 {
03272 lex->nextToken();
03273 AST::Node e;
03274 parseCommaExpression( e );
03275 CHECK( ']', "]" );
03276 }
03277 break;
03278
03279 case '(':
03280 {
03281 lex->nextToken();
03282 AST::Node funArgs;
03283 parseCommaExpression( funArgs );
03284 CHECK( ')', ")" );
03285 }
03286 break;
03287
03288 case Token_incr:
03289 case Token_decr:
03290 lex->nextToken();
03291 break;
03292
03293 case '.':
03294 case Token_arrow:
03295 {
03296 lex->nextToken();
03297 if( lex->lookAhead(0) == Token_template )
03298 lex->nextToken();
03299
03300 NameAST::Node name;
03301 if( !parseName(name) ){
03302 return false;
03303 }
03304 }
03305 break;
03306
03307 case Token_typename:
03308 {
03309 lex->nextToken();
03310
03311 NameAST::Node name;
03312 if( !parseName(name) ){
03313 return false;
03314 }
03315
03316 CHECK( '(', "(" );
03317 AST::Node expr;
03318 parseCommaExpression(expr);
03319 CHECK( ')', ")" );
03320 }
03321 return true;
03322
03323 default:
03324 return true;
03325
03326 }
03327
03328 }
03329
03330 return true;
03331 }
03332
03333 bool Parser::parseUnaryExpression( AST::Node& node )
03334 {
03335
03336
03337 switch( lex->lookAhead(0) ){
03338 case Token_incr:
03339 case Token_decr:
03340 case '*':
03341 case '&':
03342 case '+':
03343 case '-':
03344 case '!':
03345 case '~':
03346 {
03347 lex->nextToken();
03348 AST::Node expr;
03349 return parseCastExpression( expr );
03350 }
03351
03352 case Token_sizeof:
03353 {
03354 lex->nextToken();
03355 int index = lex->index();
03356 if( lex->lookAhead(0) == '(' ){
03357 lex->nextToken();
03358 AST::Node typeId;
03359 if( parseTypeId(typeId) && lex->lookAhead(0) == ')' ){
03360 lex->nextToken();
03361 return true;
03362 }
03363 lex->setIndex( index );
03364 }
03365 AST::Node expr;
03366 return parseUnaryExpression( expr );
03367 }
03368
03369 case Token_new:
03370 return parseNewExpression( node );
03371
03372 case Token_delete:
03373 return parseDeleteExpression( node );
03374 }
03375
03376 return parsePostfixExpression( node );
03377 }
03378
03379 bool Parser::parseNewExpression( AST::Node& )
03380 {
03381
03382 if( lex->lookAhead(0) == Token_scope && lex->lookAhead(1) == Token_new )
03383 lex->nextToken();
03384
03385 CHECK( Token_new, "new");
03386
03387 if( lex->lookAhead(0) == '(' ){
03388 lex->nextToken();
03389 AST::Node expr;
03390 parseCommaExpression( expr );
03391 CHECK( ')', ")" );
03392 }
03393
03394 if( lex->lookAhead(0) == '(' ){
03395 lex->nextToken();
03396 AST::Node typeId;
03397 parseTypeId( typeId );
03398 CHECK( ')', ")" );
03399 } else {
03400 AST::Node typeId;
03401 parseNewTypeId( typeId );
03402 }
03403
03404 AST::Node init;
03405 parseNewInitializer( init );
03406 return true;
03407 }
03408
03409 bool Parser::parseNewTypeId( AST::Node& )
03410 {
03411
03412 TypeSpecifierAST::Node typeSpec;
03413 if( parseTypeSpecifier(typeSpec) ){
03414 AST::Node declarator;
03415 parseNewDeclarator( declarator );
03416 return true;
03417 }
03418
03419 return false;
03420 }
03421
03422 bool Parser::parseNewDeclarator( AST::Node& )
03423 {
03424
03425 AST::Node ptrOp;
03426 if( parsePtrOperator(ptrOp) ){
03427 AST::Node declarator;
03428 parseNewDeclarator( declarator );
03429 return true;
03430 }
03431
03432 if( lex->lookAhead(0) == '[' ){
03433 while( lex->lookAhead(0) == '[' ){
03434 lex->nextToken();
03435 AST::Node expr;
03436 parseExpression( expr );
03437 ADVANCE( ']', "]" );
03438 }
03439 return true;
03440 }
03441
03442 return false;
03443 }
03444
03445 bool Parser::parseNewInitializer( AST::Node& )
03446 {
03447
03448 if( lex->lookAhead(0) != '(' )
03449 return false;
03450
03451 lex->nextToken();
03452 AST::Node expr;
03453 parseCommaExpression( expr );
03454 CHECK( ')', ")" );
03455
03456 return true;
03457 }
03458
03459 bool Parser::parseDeleteExpression( AST::Node& )
03460 {
03461
03462 if( lex->lookAhead(0) == Token_scope && lex->lookAhead(1) == Token_delete )
03463 lex->nextToken();
03464
03465 CHECK( Token_delete, "delete" );
03466
03467 if( lex->lookAhead(0) == '[' ){
03468 lex->nextToken();
03469 CHECK( ']', "]" );
03470 }
03471
03472 AST::Node expr;
03473 return parseCastExpression( expr );
03474 }
03475
03476 bool Parser::parseCastExpression( AST::Node& )
03477 {
03478
03479
03480 int index = lex->index();
03481
03482 if( lex->lookAhead(0) == '(' ){
03483 lex->nextToken();
03484 AST::Node typeId;
03485 if ( parseTypeId(typeId) ) {
03486 if ( lex->lookAhead(0) == ')' ) {
03487 lex->nextToken();
03488 AST::Node expr;
03489 if( parseCastExpression(expr) )
03490 return true;
03491 }
03492 }
03493 }
03494
03495 lex->setIndex( index );
03496
03497 AST::Node expr;
03498 return parseUnaryExpression( expr );
03499 }
03500
03501 bool Parser::parsePmExpression( AST::Node& )
03502 {
03503
03504 AST::Node expr;
03505 if( !parseCastExpression(expr) )
03506 return false;
03507
03508 while( lex->lookAhead(0) == Token_ptrmem ){
03509 lex->nextToken();
03510
03511 if( !parseCastExpression(expr) )
03512 return false;
03513 }
03514
03515 return true;
03516 }
03517
03518 bool Parser::parseMultiplicativeExpression( AST::Node& )
03519 {
03520
03521 AST::Node expr;
03522 if( !parsePmExpression(expr) )
03523 return false;
03524
03525 while( lex->lookAhead(0) == '*' || lex->lookAhead(0) == '/' || lex->lookAhead(0) == '%' ){
03526 lex->nextToken();
03527
03528 if( !parsePmExpression(expr) )
03529 return false;
03530 }
03531
03532 return true;
03533 }
03534
03535
03536 bool Parser::parseAdditiveExpression( AST::Node& )
03537 {
03538
03539 AST::Node expr;
03540 if( !parseMultiplicativeExpression(expr) )
03541 return false;
03542
03543 while( lex->lookAhead(0) == '+' || lex->lookAhead(0) == '-' ){
03544 lex->nextToken();
03545
03546 if( !parseMultiplicativeExpression(expr) )
03547 return false;
03548 }
03549
03550 return true;
03551 }
03552
03553 bool Parser::parseShiftExpression( AST::Node& )
03554 {
03555
03556 AST::Node expr;
03557 if( !parseAdditiveExpression(expr) )
03558 return false;
03559
03560 while( lex->lookAhead(0) == Token_shift ){
03561 lex->nextToken();
03562
03563 if( !parseAdditiveExpression(expr) )
03564 return false;
03565 }
03566
03567 return true;
03568 }
03569
03570 bool Parser::parseRelationalExpression( AST::Node& , bool templArgs )
03571 {
03572
03573 AST::Node expr;
03574 if( !parseShiftExpression(expr) )
03575 return false;
03576
03577 while( lex->lookAhead(0) == '<' || (lex->lookAhead(0) == '>' && !templArgs) ||
03578 lex->lookAhead(0) == Token_leq || lex->lookAhead(0) == Token_geq ){
03579 lex->nextToken();
03580
03581 if( !parseShiftExpression(expr) )
03582 return false;
03583 }
03584
03585 return true;
03586 }
03587
03588 bool Parser::parseEqualityExpression( AST::Node& , bool templArgs )
03589 {
03590
03591 AST::Node expr;
03592 if( !parseRelationalExpression(expr, templArgs) )
03593 return false;
03594
03595 while( lex->lookAhead(0) == Token_eq || lex->lookAhead(0) == Token_not_eq ){
03596 lex->nextToken();
03597
03598 if( !parseRelationalExpression(expr, templArgs) )
03599 return false;
03600 }
03601
03602 return true;
03603 }
03604
03605 bool Parser::parseAndExpression( AST::Node& , bool templArgs )
03606 {
03607
03608 AST::Node expr;
03609 if( !parseEqualityExpression(expr, templArgs) )
03610 return false;
03611
03612 while( lex->lookAhead(0) == '&' ){
03613 lex->nextToken();
03614
03615 if( !parseEqualityExpression(expr, templArgs) )
03616 return false;
03617 }
03618
03619 return true;
03620 }
03621
03622 bool Parser::parseExclusiveOrExpression( AST::Node& , bool templArgs )
03623 {
03624
03625 AST::Node expr;
03626 if( !parseAndExpression(expr, templArgs) )
03627 return false;
03628
03629 while( lex->lookAhead(0) == '^' ){
03630 lex->nextToken();
03631
03632 if( !parseAndExpression(expr, templArgs) )
03633 return false;
03634 }
03635
03636 return true;
03637 }
03638
03639 bool Parser::parseInclusiveOrExpression( AST::Node& , bool templArgs )
03640 {
03641
03642 AST::Node expr;
03643 if( !parseExclusiveOrExpression(expr, templArgs) )
03644 return false;
03645
03646 while( lex->lookAhead(0) == '|' ){
03647 lex->nextToken();
03648
03649 if( !parseExclusiveOrExpression(expr, templArgs) )
03650 return false;
03651 }
03652
03653 return true;
03654 }
03655
03656 bool Parser::parseLogicalAndExpression( AST::Node& , bool templArgs )
03657 {
03658
03659
03660 AST::Node expr;
03661 if( !parseInclusiveOrExpression(expr, templArgs) )
03662 return false;
03663
03664 while( lex->lookAhead(0) == Token_and ){
03665 lex->nextToken();
03666
03667 if( !parseInclusiveOrExpression(expr, templArgs) )
03668 return false;
03669 }
03670
03671 return true;
03672 }
03673
03674 bool Parser::parseLogicalOrExpression( AST::Node& node, bool templArgs )
03675 {
03676
03677
03678 int start = lex->index();
03679
03680 AST::Node expr;
03681 if( !parseLogicalAndExpression(expr, templArgs) )
03682 return false;
03683
03684 while( lex->lookAhead(0) == Token_or ){
03685 lex->nextToken();
03686
03687 if( !parseLogicalAndExpression(expr, templArgs) )
03688 return false;
03689 }
03690
03691 AST::Node ast = CreateNode<AST>();
03692 UPDATE_POS( ast, start, lex->index() );
03693 node = ast;
03694 return true;
03695 }
03696
03697 bool Parser::parseConditionalExpression( AST::Node& )
03698 {
03699
03700 AST::Node expr;
03701 if( !parseLogicalOrExpression(expr) )
03702 return false;
03703
03704 if( lex->lookAhead(0) == '?' ){
03705 lex->nextToken();
03706
03707 if( !parseExpression(expr) )
03708 return false;
03709
03710 CHECK( ':', ":" );
03711
03712 if( !parseAssignmentExpression(expr) )
03713 return false;
03714 }
03715
03716 return true;
03717 }
03718
03719 bool Parser::parseAssignmentExpression( AST::Node& node )
03720 {
03721
03722 int start = lex->index();
03723 AST::Node expr;
03724 if( lex->lookAhead(0) == Token_throw && !parseThrowExpression(expr) )
03725 return false;
03726 else if( !parseConditionalExpression(expr) )
03727 return false;
03728
03729 while( lex->lookAhead(0) == Token_assign || lex->lookAhead(0) == '=' ){
03730 lex->nextToken();
03731
03732 if( !parseConditionalExpression(expr) )
03733 return false;
03734 }
03735
03736 AST::Node ast = CreateNode<AST>();
03737 UPDATE_POS( ast, start, lex->index() );
03738 node = ast;
03739 return true;
03740 }
03741
03742 bool Parser::parseConstantExpression( AST::Node& node )
03743 {
03744
03745 int start = lex->index();
03746 if( parseConditionalExpression(node) ){
03747 AST::Node ast = CreateNode<AST>();
03748 UPDATE_POS( ast, start, lex->index() );
03749 node = ast;
03750 return true;
03751 }
03752 return false;
03753 }
03754
03755 bool Parser::parseExpression( AST::Node& node )
03756 {
03757
03758
03759 int start = lex->index();
03760
03761 if( !parseCommaExpression(node) )
03762 return false;
03763
03764 AST::Node ast = CreateNode<AST>();
03765 UPDATE_POS( ast, start, lex->index() );
03766 node = ast;
03767 return true;
03768 }
03769
03770 bool Parser::parseCommaExpression( AST::Node& node )
03771 {
03772
03773 int start = lex->index();
03774
03775 AST::Node expr;
03776 if( !parseAssignmentExpression(expr) )
03777 return false;
03778
03779 while( lex->lookAhead(0) == ',' ){
03780 lex->nextToken();
03781
03782 if( !parseAssignmentExpression(expr) )
03783 return false;
03784 }
03785
03786 AST::Node ast = CreateNode<AST>();
03787 UPDATE_POS( ast, start, lex->index() );
03788 node = ast;
03789 return true;
03790 }
03791
03792 bool Parser::parseThrowExpression( AST::Node& )
03793 {
03794
03795 if( lex->lookAhead(0) != Token_throw )
03796 return false;
03797
03798 CHECK( Token_throw, "throw" );
03799 AST::Node expr;
03800 if( !parseAssignmentExpression(expr) )
03801 return false;
03802
03803 return true;
03804 }
03805
03806 bool Parser::parseIvarDeclList( AST::Node & node )
03807 {
03808 Q_UNUSED( node );
03809 return false;
03810 }
03811
03812 bool Parser::parseIvarDecls( AST::Node & node )
03813 {
03814 Q_UNUSED( node );
03815 return false;
03816 }
03817
03818 bool Parser::parseIvarDecl( AST::Node & node )
03819 {
03820 Q_UNUSED( node );
03821 return false;
03822 }
03823
03824 bool Parser::parseIvars( AST::Node & node )
03825 {
03826 Q_UNUSED( node );
03827 return false;
03828 }
03829
03830 bool Parser::parseIvarDeclarator( AST::Node & node )
03831 {
03832 Q_UNUSED( node );
03833 return false;
03834 }
03835
03836 bool Parser::parseMethodDecl( AST::Node & node )
03837 {
03838 Q_UNUSED( node );
03839 return false;
03840 }
03841
03842 bool Parser::parseUnarySelector( AST::Node & node )
03843 {
03844 Q_UNUSED( node );
03845 return false;
03846 }
03847
03848 bool Parser::parseKeywordSelector( AST::Node & node )
03849 {
03850 Q_UNUSED( node );
03851 return false;
03852 }
03853
03854 bool Parser::parseSelector( AST::Node & node )
03855 {
03856 Q_UNUSED( node );
03857 return false;
03858 }
03859
03860 bool Parser::parseKeywordDecl( AST::Node & node )
03861 {
03862 Q_UNUSED( node );
03863 return false;
03864 }
03865
03866 bool Parser::parseReceiver( AST::Node & node )
03867 {
03868 Q_UNUSED( node );
03869 return false;
03870 }
03871
03872 bool Parser::parseObjcMessageExpr( AST::Node & node )
03873 {
03874 Q_UNUSED( node );
03875 return false;
03876 }
03877
03878 bool Parser::parseMessageArgs( AST::Node & node )
03879 {
03880 Q_UNUSED( node );
03881 return false;
03882 }
03883
03884 bool Parser::parseKeywordExpr( AST::Node & node )
03885 {
03886 Q_UNUSED( node );
03887 return false;
03888 }
03889
03890 bool Parser::parseKeywordArgList( AST::Node & node )
03891 {
03892 Q_UNUSED( node );
03893 return false;
03894 }
03895
03896 bool Parser::parseKeywordArg( AST::Node & node )
03897 {
03898 Q_UNUSED( node );
03899 return false;
03900 }
03901
03902 bool Parser::parseReservedWord( AST::Node & node )
03903 {
03904 Q_UNUSED( node );
03905 return false;
03906 }
03907
03908 bool Parser::parseMyParms( AST::Node & node )
03909 {
03910 Q_UNUSED( node );
03911 return false;
03912 }
03913
03914 bool Parser::parseMyParm( AST::Node & node )
03915 {
03916 Q_UNUSED( node );
03917 return false;
03918 }
03919
03920 bool Parser::parseOptParmList( AST::Node & node )
03921 {
03922 Q_UNUSED( node );
03923 return false;
03924 }
03925
03926 bool Parser::parseObjcSelectorExpr( AST::Node & node )
03927 {
03928 Q_UNUSED( node );
03929 return false;
03930 }
03931
03932 bool Parser::parseSelectorArg( AST::Node & node )
03933 {
03934 Q_UNUSED( node );
03935 return false;
03936 }
03937
03938 bool Parser::parseKeywordNameList( AST::Node & node )
03939 {
03940 Q_UNUSED( node );
03941 return false;
03942 }
03943
03944 bool Parser::parseKeywordName( AST::Node & node )
03945 {
03946 Q_UNUSED( node );
03947 return false;
03948 }
03949
03950 bool Parser::parseObjcEncodeExpr( AST::Node & node )
03951 {
03952 Q_UNUSED( node );
03953 return false;
03954 }
03955
03956 bool Parser::parseObjcString( AST::Node & node )
03957 {
03958 Q_UNUSED( node );
03959 return false;
03960 }
03961
03962 bool Parser::parseProtocolRefs( AST::Node & node )
03963 {
03964 Q_UNUSED( node );
03965 return false;
03966 }
03967
03968 bool Parser::parseIdentifierList( GroupAST::Node & node )
03969 {
03970 int start = lex->index();
03971
03972 if( lex->lookAhead(0) != Token_identifier )
03973 return false;
03974
03975 GroupAST::Node ast = CreateNode<GroupAST>();
03976
03977 AST_FROM_TOKEN( tk, lex->index() );
03978 ast->addNode( tk );
03979 lex->nextToken();
03980
03981 while( lex->lookAhead(0) == ',' ){
03982 lex->nextToken();
03983 if( lex->lookAhead(0) == Token_identifier ){
03984 AST_FROM_TOKEN( tk, lex->index() );
03985 ast->addNode( tk );
03986 lex->nextToken();
03987 }
03988 ADVANCE( Token_identifier, "identifier" );
03989 }
03990
03991 node = ast;
03992 UPDATE_POS( node, start, lex->index() );
03993 return true;
03994 }
03995
03996 bool Parser::parseIdentifierColon( AST::Node & node )
03997 {
03998 Q_UNUSED( node );
03999
04000 if( lex->lookAhead(0) == Token_identifier && lex->lookAhead(1) == ':' ){
04001 lex->nextToken();
04002 lex->nextToken();
04003 return true;
04004 }
04005
04006 return false;
04007 }
04008
04009 bool Parser::parseObjcProtocolExpr( AST::Node & node )
04010 {
04011 Q_UNUSED( node );
04012 return false;
04013 }
04014
04015 bool Parser::parseObjcOpenBracketExpr( AST::Node & node )
04016 {
04017 Q_UNUSED( node );
04018 return false;
04019 }
04020
04021 bool Parser::parseObjcCloseBracket( AST::Node & node )
04022 {
04023 Q_UNUSED( node );
04024 return false;
04025 }
04026
04027 bool Parser::parseObjcDef( DeclarationAST::Node & node )
04028 {
04029 Q_UNUSED( node );
04030 return false;
04031 }
04032
04033 bool Parser::parseObjcClassDef( DeclarationAST::Node & node )
04034 {
04035 Q_UNUSED( node );
04036 return false;
04037 }
04038
04039 bool Parser::parseObjcClassDecl( DeclarationAST::Node & node )
04040 {
04041 Q_UNUSED( node );
04042
04043 ADVANCE( OBJC_CLASS, "@class" );
04044
04045 GroupAST::Node idList;
04046 parseIdentifierList( idList );
04047 ADVANCE( ';', ";" );
04048
04049 return true;
04050 }
04051
04052 bool Parser::parseObjcProtocolDecl( DeclarationAST::Node & node )
04053 {
04054 Q_UNUSED( node );
04055
04056 ADVANCE( OBJC_PROTOCOL, "@protocol" );
04057
04058 GroupAST::Node idList;
04059 parseIdentifierList( idList );
04060 ADVANCE( ';', ";" );
04061
04062 return true;
04063 }
04064
04065 bool Parser::parseObjcAliasDecl( DeclarationAST::Node & node )
04066 {
04067 Q_UNUSED( node );
04068
04069 ADVANCE( OBJC_ALIAS, "@alias" );
04070
04071 GroupAST::Node idList;
04072 parseIdentifierList( idList );
04073 ADVANCE( ';', ";" );
04074
04075 return true;
04076 }
04077
04078 bool Parser::parseObjcProtocolDef( DeclarationAST::Node & node )
04079 {
04080 Q_UNUSED( node );
04081 return false;
04082 }
04083
04084 bool Parser::parseObjcMethodDef( DeclarationAST::Node & node )
04085 {
04086 Q_UNUSED( node );
04087 return false;
04088 }
04089
04090 bool Parser::parseWinDeclSpec( GroupAST::Node & node )
04091 {
04092 if( lex->lookAhead(0) == Token_identifier && lex->lookAhead(0).text() == "__declspec" && lex->lookAhead(1) == '(' ){
04093 int start = lex->index();
04094 lex->nextToken();
04095 lex->nextToken();
04096
04097 parseIdentifierList( node );
04098 ADVANCE( ')', ")" );
04099
04100 UPDATE_POS( node, start, lex->index() );
04101 return true;
04102 }
04103
04104 return false;
04105 }
04106