yyindent.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060 #include <qregexp.h>
00061 #include <qmap.h>
00062 #include <qvariant.h>
00063 #include <kdebug.h>
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073 static const int SmallRoof = 40;
00074 static const int BigRoof = 400;
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088 static int ppHardwareTabSize = 8;
00089 static int ppIndentSize = 4;
00090 static int ppContinuationIndentSize = 8;
00091 static int ppCommentOffset = 2;
00092
00093 static QRegExp *literal = 0;
00094 static QRegExp *label = 0;
00095 static QRegExp *inlineCComment = 0;
00096 static QRegExp *braceX = 0;
00097 static QRegExp *iflikeKeyword = 0;
00098
00099
00100 void configureCIndent( const QMap<QString, QVariant>& values )
00101 {
00102 if( values.contains("TabSize") )
00103 ppHardwareTabSize = values[ "TabSize" ].toInt();
00104
00105 if( values.contains("IndentSize") )
00106 ppIndentSize = values[ "IndentSize" ].toInt();
00107
00108 if( values.contains("ContinuationSize") )
00109 ppContinuationIndentSize = values[ "ContinuationSize" ].toInt();
00110
00111 if( values.contains("CommentOffset") )
00112 ppCommentOffset = values[ "CommentOffset" ].toInt();
00113 }
00114
00115
00116
00117
00118
00119 static QChar firstNonWhiteSpace( const QString& t )
00120 {
00121 int i = 0;
00122 while ( i < (int) t.length() ) {
00123 if ( !t[i].isSpace() )
00124 return t[i];
00125 i++;
00126 }
00127 return QChar::null;
00128 }
00129
00130
00131
00132
00133
00134 static bool isOnlyWhiteSpace( const QString& t )
00135 {
00136 return firstNonWhiteSpace( t ).isNull();
00137 }
00138
00139
00140
00141
00142
00143
00144 static int columnForIndex( const QString& t, int index )
00145 {
00146 int col = 0;
00147 if ( index > (int) t.length() )
00148 index = t.length();
00149
00150 for ( int i = 0; i < index; i++ ) {
00151 if ( t[i].latin1() == '\t' ) {
00152 col = ( (col / ppHardwareTabSize) + 1 ) * ppHardwareTabSize;
00153 } else {
00154 col++;
00155 }
00156 }
00157 return col;
00158 }
00159
00160
00161
00162
00163 static int indentOfLine( const QString& t )
00164 {
00165 return columnForIndex( t, t.find(firstNonWhiteSpace(t)) );
00166 }
00167
00168
00169
00170
00171
00172
00173
00174 static inline void eraseChar( QString& t, int k, QChar ch )
00175 {
00176 if ( t[k] != '\t' )
00177 t[k] = ch;
00178 }
00179
00180
00181
00182
00183
00184 static QString trimmedCodeLine( const QString& t )
00185 {
00186 QString trimmed = t;
00187 int k;
00188
00189
00190
00191
00192
00193
00194
00195
00196 k = 0;
00197 while ( (k = trimmed.find(*literal, k)) != -1 ) {
00198 for ( int i = 0; i < literal->matchedLength(); i++ )
00199 eraseChar( trimmed, k + i, QChar('X') );
00200 k += literal->matchedLength();
00201 }
00202
00203
00204
00205
00206
00207 k = 0;
00208 while ( (k = trimmed.find(*inlineCComment, k)) != -1 ) {
00209 for ( int i = 0; i < inlineCComment->matchedLength(); i++ )
00210 eraseChar( trimmed, k + i, QChar(' ') );
00211 k += inlineCComment->matchedLength();
00212 }
00213
00214
00215
00216
00217
00218
00219
00220
00221 while ( trimmed.findRev(QChar(':')) != -1 && trimmed.find(*label) != -1 ) {
00222 QString cap1 = label->cap( 1 );
00223 int pos1 = label->pos( 1 );
00224 for ( int i = 0; i < (int) cap1.length(); i++ )
00225 eraseChar( trimmed, pos1 + i, QChar(' ') );
00226 }
00227
00228
00229
00230
00231 k = trimmed.find( QString("//") );
00232 if ( k != -1 )
00233 trimmed.truncate( k );
00234
00235 return trimmed;
00236 }
00237
00238
00239
00240
00241
00242 static inline QChar lastParen( const QString& t )
00243 {
00244 int i = t.length();
00245 while ( i > 0 ) {
00246 i--;
00247 if ( t[i] == QChar('(') || t[i] == QChar(')') )
00248 return t[i];
00249 }
00250 return QChar::null;
00251 }
00252
00253
00254
00255
00256
00257 static inline bool okay( QChar typedIn, QChar okayCh )
00258 {
00259 return typedIn.isNull() || typedIn == okayCh;
00260 }
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 struct LinizerState
00271 {
00272 QString line;
00273 int braceDepth;
00274 bool leftBraceFollows;
00275
00276 QStringList::ConstIterator iter;
00277 bool inCComment;
00278 bool pendingRightBrace;
00279 };
00280
00281 static QStringList *yyProgram = 0;
00282 static LinizerState *yyLinizerState = 0;
00283
00284
00285 static const QString *yyLine = 0;
00286 static const int *yyBraceDepth = 0;
00287 static const bool *yyLeftBraceFollows = 0;
00288
00289
00290
00291
00292
00293 #define YY_SAVE() \
00294 LinizerState savedState = *yyLinizerState
00295 #define YY_RESTORE() \
00296 *yyLinizerState = savedState
00297
00298
00299
00300
00301
00302
00303 static bool readLine()
00304 {
00305 int k;
00306
00307 yyLinizerState->leftBraceFollows =
00308 ( firstNonWhiteSpace(yyLinizerState->line) == QChar('{') );
00309
00310 do {
00311 if ( yyLinizerState->iter == yyProgram->begin() ) {
00312 yyLinizerState->line = QString::null;
00313 return FALSE;
00314 }
00315
00316 --yyLinizerState->iter;
00317 yyLinizerState->line = *yyLinizerState->iter;
00318
00319 yyLinizerState->line = trimmedCodeLine( yyLinizerState->line );
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331 if ( yyLinizerState->inCComment ) {
00332 QString slashAster( "/*" );
00333
00334 k = yyLinizerState->line.find( slashAster );
00335 if ( k == -1 ) {
00336 yyLinizerState->line = QString::null;
00337 } else {
00338 yyLinizerState->line.truncate( k );
00339 yyLinizerState->inCComment = FALSE;
00340 }
00341 }
00342
00343 if ( !yyLinizerState->inCComment ) {
00344 QString asterSlash( "*/" );
00345
00346 k = yyLinizerState->line.find( asterSlash );
00347 if ( k != -1 ) {
00348 for ( int i = 0; i < k + 2; i++ )
00349 eraseChar( yyLinizerState->line, i, QChar(' ') );
00350 yyLinizerState->inCComment = TRUE;
00351 }
00352 }
00353
00354
00355
00356
00357 k = 0;
00358 while ( k < (int) yyLinizerState->line.length() ) {
00359 QChar ch = yyLinizerState->line[k];
00360 if ( ch == QChar('#') ) {
00361 yyLinizerState->line = QString::null;
00362 } else if ( !ch.isSpace() ) {
00363 break;
00364 }
00365 k++;
00366 }
00367
00368
00369
00370
00371 k = yyLinizerState->line.length();
00372 while ( k > 0 && yyLinizerState->line[k - 1].isSpace() )
00373 k--;
00374 yyLinizerState->line.truncate( k );
00375
00376
00377
00378
00379
00380 yyLinizerState->braceDepth +=
00381 yyLinizerState->line.contains( QChar('}') ) -
00382 yyLinizerState->line.contains( QChar('{') );
00383
00384
00385
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395 if ( yyLinizerState->pendingRightBrace )
00396 yyLinizerState->braceDepth++;
00397 yyLinizerState->pendingRightBrace =
00398 ( yyLinizerState->line.find(*braceX) == 0 );
00399 if ( yyLinizerState->pendingRightBrace )
00400 yyLinizerState->braceDepth--;
00401 } while ( yyLinizerState->line.isEmpty() );
00402
00403 return TRUE;
00404 }
00405
00406
00407
00408
00409
00410 static void startLinizer()
00411 {
00412 yyLinizerState->braceDepth = 0;
00413 yyLinizerState->inCComment = FALSE;
00414 yyLinizerState->pendingRightBrace = FALSE;
00415
00416 yyLine = &yyLinizerState->line;
00417 yyBraceDepth = &yyLinizerState->braceDepth;
00418 yyLeftBraceFollows = &yyLinizerState->leftBraceFollows;
00419
00420 yyLinizerState->iter = yyProgram->end();
00421 --yyLinizerState->iter;
00422 yyLinizerState->line = *yyLinizerState->iter;
00423 readLine();
00424 }
00425
00426
00427
00428
00429
00430
00431 static bool bottomLineStartsInCComment()
00432 {
00433 QString slashAster( "/*" );
00434 QString asterSlash( "*/" );
00435
00436
00437
00438
00439
00440 QStringList::ConstIterator p = yyProgram->end();
00441 --p;
00442
00443 for ( int i = 0; i < BigRoof; i++ ) {
00444 if ( p == yyProgram->begin() )
00445 return FALSE;
00446 --p;
00447
00448 if ( (*p).find(slashAster) != -1 ||
00449 (*p).find(asterSlash) != -1 ) {
00450 QString trimmed = trimmedCodeLine( *p );
00451
00452 if ( trimmed.find(slashAster) != -1 ) {
00453 return TRUE;
00454 } else if ( trimmed.find(asterSlash) != -1 ) {
00455 return FALSE;
00456 }
00457 }
00458 }
00459 return FALSE;
00460 }
00461
00462
00463
00464
00465
00466
00467
00468
00469
00470 static int indentWhenBottomLineStartsInCComment()
00471 {
00472 int k = yyLine->findRev( QString("/*") );
00473 if ( k == -1 ) {
00474
00475
00476
00477
00478 return indentOfLine( *yyLine );
00479 } else {
00480
00481
00482
00483
00484
00485 int indent = columnForIndex( *yyLine, k );
00486 k += 2;
00487 while ( k < (int) yyLine->length() ) {
00488 if ( !(*yyLine)[k].isSpace() )
00489 return columnForIndex( *yyLine, k );
00490 k++;
00491 }
00492 return indent + ppCommentOffset;
00493 }
00494 }
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514 static bool matchBracelessControlStatement()
00515 {
00516 int delimDepth = 0;
00517
00518 if ( yyLine->endsWith(QString("else")) )
00519 return TRUE;
00520
00521 if ( !yyLine->endsWith(QChar(')')) )
00522 return FALSE;
00523
00524 for ( int i = 0; i < SmallRoof; i++ ) {
00525 int j = yyLine->length();
00526 while ( j > 0 ) {
00527 j--;
00528 QChar ch = (*yyLine)[j];
00529
00530 switch ( ch.unicode() ) {
00531 case ')':
00532 delimDepth++;
00533 break;
00534 case '(':
00535 delimDepth--;
00536 if ( delimDepth == 0 ) {
00537 if ( yyLine->find(*iflikeKeyword) != -1 ) {
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547 return TRUE;
00548 }
00549 }
00550 if ( delimDepth == -1 ) {
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562 return FALSE;
00563 }
00564 break;
00565 case '{':
00566 case '}':
00567 case ';':
00568
00569
00570
00571
00572
00573
00574 if ( ch != QChar( ';' ) || delimDepth == 0 )
00575 return FALSE;
00576 }
00577 }
00578
00579 if ( !readLine() )
00580 break;
00581 }
00582 return FALSE;
00583 }
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599 static bool isUnfinishedLine()
00600 {
00601 bool unf = FALSE;
00602
00603 YY_SAVE();
00604
00605 if ( yyLine->isEmpty() )
00606 return FALSE;
00607
00608 QChar lastCh = (*yyLine)[(int) yyLine->length() - 1];
00609 if ( QString("{};").find(lastCh) == -1 ) {
00610
00611
00612
00613
00614
00615 unf = ( yyLine->contains(QString("Q_OBJECT")) == 0 &&
00616 !matchBracelessControlStatement() );
00617 } else if ( lastCh == QChar(';') ) {
00618 if ( lastParen(*yyLine) == QChar('(') ) {
00619
00620
00621
00622
00623
00624 unf = TRUE;
00625 } else if ( readLine() && yyLine->endsWith(QChar(';')) &&
00626 lastParen(*yyLine) == QChar('(') ) {
00627
00628
00629
00630
00631
00632
00633 unf = TRUE;
00634 }
00635 }
00636
00637 YY_RESTORE();
00638 return unf;
00639 }
00640
00641
00642
00643
00644
00645 static bool isContinuationLine()
00646 {
00647 bool cont = FALSE;
00648
00649 YY_SAVE();
00650 if ( readLine() )
00651 cont = isUnfinishedLine();
00652 YY_RESTORE();
00653 return cont;
00654 }
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664 static int indentForContinuationLine()
00665 {
00666 int braceDepth = 0;
00667 int delimDepth = 0;
00668
00669 bool leftBraceFollowed = *yyLeftBraceFollows;
00670
00671 for ( int i = 0; i < SmallRoof; i++ ) {
00672 int hook = -1;
00673
00674 int j = yyLine->length();
00675 while ( j > 0 && hook < 0 ) {
00676 j--;
00677 QChar ch = (*yyLine)[j];
00678
00679 switch ( ch.unicode() ) {
00680 case ')':
00681 case ']':
00682 delimDepth++;
00683 break;
00684 case '}':
00685 braceDepth++;
00686 break;
00687 case '(':
00688 case '[':
00689 delimDepth--;
00690
00691
00692
00693
00694 if ( delimDepth == -1 )
00695 hook = j;
00696 break;
00697 case '{':
00698 braceDepth--;
00699
00700
00701
00702
00703
00704
00705 if ( braceDepth == -1 ) {
00706 if ( j < (int) yyLine->length() - 1 ) {
00707 hook = j;
00708 } else {
00709 return 0;
00710 }
00711 }
00712 break;
00713 case '=':
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739 if ( j == 0 || QString("!=<>").find((*yyLine)[j - 1]) == -1 ) {
00740 if ( braceDepth == 0 && delimDepth == 0 &&
00741 j < (int) yyLine->length() - 1 &&
00742 !yyLine->endsWith(QChar(',')) &&
00743 (yyLine->contains(QChar('(')) ==
00744 yyLine->contains(QChar(')'))) )
00745 hook = j;
00746 }
00747 }
00748 }
00749
00750 if ( hook >= 0 ) {
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765 hook++;
00766 while ( hook < (int) yyLine->length() ) {
00767 if ( !(*yyLine)[hook].isSpace() )
00768 return columnForIndex( *yyLine, hook );
00769 hook++;
00770 }
00771 return indentOfLine( *yyLine ) + ppContinuationIndentSize;
00772 }
00773
00774 if ( braceDepth != 0 )
00775 break;
00776
00777
00778
00779
00780
00781 if ( delimDepth == 0 ) {
00782 if ( isContinuationLine() || leftBraceFollowed ) {
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798 return indentOfLine( *yyLine );
00799 } else {
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816 return indentOfLine( *yyLine ) + ppContinuationIndentSize;
00817 }
00818 }
00819
00820 if ( !readLine() )
00821 break;
00822 }
00823 return 0;
00824 }
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877 static int indentForStandaloneLine()
00878 {
00879 for ( int i = 0; i < SmallRoof; i++ ) {
00880 if ( !*yyLeftBraceFollows ) {
00881 YY_SAVE();
00882
00883 if ( matchBracelessControlStatement() ) {
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893 return indentOfLine( *yyLine ) + ppIndentSize;
00894 }
00895 YY_RESTORE();
00896 }
00897
00898 if ( yyLine->endsWith(QChar(';')) ||
00899 yyLine->contains(QChar('{')) > 0 ) {
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912 if ( *yyBraceDepth > 0 ) {
00913 do {
00914 if ( !readLine() )
00915 break;
00916 } while ( *yyBraceDepth > 0 );
00917 }
00918
00919 LinizerState hookState;
00920
00921 if ( *yyBraceDepth == 0 ) {
00922 while ( isContinuationLine() )
00923 readLine();
00924 hookState = *yyLinizerState;
00925
00926 readLine();
00927 if ( *yyBraceDepth == 0 ) {
00928 do {
00929 if ( !matchBracelessControlStatement() )
00930 break;
00931 hookState = *yyLinizerState;
00932 } while ( readLine() );
00933 }
00934 } else {
00935 hookState = *yyLinizerState;
00936 }
00937
00938 *yyLinizerState = hookState;
00939
00940 while ( isContinuationLine() )
00941 readLine();
00942
00943
00944
00945
00946
00947 if ( yyLine->stripWhiteSpace().length() > 1 )
00948 return indentOfLine( *yyLine ) - *yyBraceDepth * ppIndentSize;
00949 }
00950
00951 if ( !readLine() )
00952 break;
00953 }
00954 return 0;
00955 }
00956
00957
00958
00959
00960 static void initializeIndenter()
00961 {
00962 literal = new QRegExp( QString("([\"'])(?:\\\\.|[^\\\\])*\\1") );
00963 literal->setMinimal( TRUE );
00964 label = new QRegExp( QString(
00965 "^\\s*((?:case\\b[^:]+|[a-zA-Z_0-9]+):)(?!:)") );
00966 inlineCComment = new QRegExp( QString("/\\*.*\\*/") );
00967 inlineCComment->setMinimal( TRUE );
00968 braceX = new QRegExp( QString("^\\s*\\}\\s*(?:else|catch)\\b") );
00969 iflikeKeyword = new QRegExp( QString("\\b(?:catch|do|for|if|while)\\b") );
00970
00971 yyLinizerState = new LinizerState;
00972 }
00973
00974
00975
00976
00977 static void terminateIndenter()
00978 {
00979 delete literal;
00980 delete label;
00981 delete inlineCComment;
00982 delete braceX;
00983 delete iflikeKeyword;
00984 delete yyLinizerState;
00985 }
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997 int indentForBottomLine( const QStringList& program, QChar typedIn )
00998 {
00999 if ( program.isEmpty() )
01000 return 0;
01001
01002 initializeIndenter();
01003
01004 yyProgram = new QStringList( program );
01005 startLinizer();
01006
01007 const QString& bottomLine = program.last();
01008 QChar firstCh = firstNonWhiteSpace( bottomLine );
01009 int indent;
01010
01011 if ( bottomLineStartsInCComment() ) {
01012
01013
01014
01015
01016
01017 if ( isOnlyWhiteSpace(bottomLine) ) {
01018 indent = indentWhenBottomLineStartsInCComment();
01019 } else {
01020 indent = indentOfLine( bottomLine );
01021 }
01022 } else if ( okay(typedIn, QChar('#')) && firstCh == QChar('#') ) {
01023
01024
01025
01026 indent = 0;
01027 } else {
01028 if ( isUnfinishedLine() ) {
01029 indent = indentForContinuationLine();
01030 } else {
01031 indent = indentForStandaloneLine();
01032 }
01033
01034 if ( okay(typedIn, QChar('}')) && firstCh == QChar('}') ) {
01035
01036
01037
01038
01039 indent -= ppIndentSize;
01040 } else if ( okay(typedIn, QChar(':')) ) {
01041 QRegExp caseLabel( QString(
01042 "\\s*(?:case\\b[^:]+|default\\s+):\\s*") );
01043
01044 if ( caseLabel.exactMatch(bottomLine) ) {
01045
01046
01047
01048
01049
01050
01051
01052
01053
01054
01055 if ( indentOfLine(bottomLine) <= indent )
01056 indent -= ppIndentSize;
01057 else
01058 indent = indentOfLine( bottomLine );
01059 }
01060 }
01061 }
01062 delete yyProgram;
01063 terminateIndenter();
01064 return QMAX( 0, indent );
01065 }
01066
01067 #ifdef Q_TEST_YYINDENT
01068
01069
01070
01071
01072 #include <qfile.h>
01073 #include <qtextstream.h>
01074
01075 #include <errno.h>
01076
01077 static QString fileContents( const QString& fileName )
01078 {
01079 QFile f( fileName );
01080 if ( !f.open(IO_ReadOnly) ) {
01081 qWarning( "yyindent error: Cannot open file '%s' for reading: %s",
01082 fileName.latin1(), strerror(errno) );
01083 return QString::null;
01084 }
01085
01086 QTextStream t( &f );
01087 QString contents = t.read();
01088 f.close();
01089 if ( contents.isEmpty() )
01090 qWarning( "yyindent error: File '%s' is empty", fileName.latin1() );
01091 return contents;
01092 }
01093
01094 int main( int argc, char **argv )
01095 {
01096 if ( argc != 2 ) {
01097 qWarning( "usage: yyindent file.cpp" );
01098 return 1;
01099 }
01100
01101 QString code = fileContents( QString(argv[1]) );
01102 QStringList program = QStringList::split( QChar('\n'), code, TRUE );
01103 QStringList p;
01104 QString out;
01105
01106 while ( !program.isEmpty() && program.last().stripWhiteSpace().isEmpty() )
01107 program.remove( program.fromLast() );
01108
01109 QStringList::ConstIterator line = program.begin();
01110 while ( line != program.end() ) {
01111 p.push_back( *line );
01112 QChar typedIn = firstNonWhiteSpace( *line );
01113 if ( p.last().endsWith(QChar(':')) )
01114 typedIn = QChar( ':' );
01115 int indent = indentForBottomLine( p, typedIn );
01116
01117 if ( !(*line).stripWhiteSpace().isEmpty() ) {
01118 for ( int j = 0; j < indent; j++ )
01119 out += QChar( ' ' );
01120 out += (*line).stripWhiteSpace();
01121 }
01122 out += QChar( '\n' );
01123 line++;
01124 }
01125
01126 while ( out.endsWith(QChar('\n')) )
01127 out.truncate( out.length() - 1 );
01128
01129 printf( "%s\n", out.latin1() );
01130 return 0;
01131 }
01132 #endif
This file is part of the documentation for KDevelop Version 3.1.2.