forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathASBeautifier.cpp
1964 lines (1703 loc) · 53.3 KB
/
ASBeautifier.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*
* ASBeautifier.cpp
*
* This file is a part of "Artistic Style" - an indentation and
* reformatting tool for C, C++, C# and Java source files.
* http://astyle.sourceforge.net
*
* The "Artistic Style" project, including all files needed to
* compile it, is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this project; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301, USA.
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
*/
#include "astyle.h"
#include <algorithm>
#include <iostream>
#define INIT_CONTAINER(container, value) {if ( (container) != NULL ) delete (container); (container) = (value); }
#define DELETE_CONTAINER(container) {if ( (container) != NULL ) delete (container); }
namespace astyle
{
vector<const string*> ASBeautifier::headers;
vector<const string*> ASBeautifier::nonParenHeaders;
vector<const string*> ASBeautifier::preBlockStatements;
vector<const string*> ASBeautifier::assignmentOperators;
vector<const string*> ASBeautifier::nonAssignmentOperators;
/*
* initialize the static vars
*/
void ASBeautifier::initStatic()
{
static int beautifierFileType = 9; // initialized with an invalid type
if (fileType == beautifierFileType) // don't build unless necessary
return;
beautifierFileType = fileType;
headers.clear();
nonParenHeaders.clear();
assignmentOperators.clear();
nonAssignmentOperators.clear();
preBlockStatements.clear();
ASResource::buildHeaders(headers, fileType, true);
ASResource::buildNonParenHeaders(nonParenHeaders, fileType, true);
ASResource::buildAssignmentOperators(assignmentOperators);
ASResource::buildNonAssignmentOperators(nonAssignmentOperators);
ASResource::buildPreBlockStatements(preBlockStatements);
// cout << "beaut" << endl;
}
/**
* ASBeautifier's constructor
*/
ASBeautifier::ASBeautifier()
{
waitingBeautifierStack = NULL;
activeBeautifierStack = NULL;
waitingBeautifierStackLengthStack = NULL;
activeBeautifierStackLengthStack = NULL;
headerStack = NULL;
tempStacks = NULL;
blockParenDepthStack = NULL;
blockStatementStack = NULL;
parenStatementStack = NULL;
bracketBlockStateStack = NULL;
inStatementIndentStack = NULL;
inStatementIndentStackSizeStack = NULL;
parenIndentStack = NULL;
sourceIterator = NULL;
isMinimalConditinalIndentSet = false;
shouldForceTabIndentation = false;
setSpaceIndentation(4);
setMaxInStatementIndentLength(40);
setClassIndent(false);
setSwitchIndent(false);
setCaseIndent(false);
setBlockIndent(false);
setBracketIndent(false);
setNamespaceIndent(false);
setLabelIndent(false);
setEmptyLineFill(false);
fileType = C_TYPE;
setCStyle();
setPreprocessorIndent(false);
}
/**
* ASBeautifier's copy constructor
*/
ASBeautifier::ASBeautifier(const ASBeautifier &other)
{
waitingBeautifierStack = NULL;
activeBeautifierStack = NULL;
waitingBeautifierStackLengthStack = NULL;
activeBeautifierStackLengthStack = NULL;
headerStack = new vector<const string*>;
*headerStack = *other.headerStack;
tempStacks = new vector<vector<const string*>*>;
vector<vector<const string*>*>::iterator iter;
for (iter = other.tempStacks->begin();
iter != other.tempStacks->end();
++iter)
{
vector<const string*> *newVec = new vector<const string*>;
*newVec = **iter;
tempStacks->push_back(newVec);
}
blockParenDepthStack = new vector<int>;
*blockParenDepthStack = *other.blockParenDepthStack;
blockStatementStack = new vector<bool>;
*blockStatementStack = *other.blockStatementStack;
parenStatementStack = new vector<bool>;
*parenStatementStack = *other.parenStatementStack;
bracketBlockStateStack = new vector<bool>;
*bracketBlockStateStack = *other.bracketBlockStateStack;
inStatementIndentStack = new vector<int>;
*inStatementIndentStack = *other.inStatementIndentStack;
inStatementIndentStackSizeStack = new vector<int>;
*inStatementIndentStackSizeStack = *other.inStatementIndentStackSizeStack;
parenIndentStack = new vector<int>;
*parenIndentStack = *other.parenIndentStack;
sourceIterator = other.sourceIterator;
// protected variables
fileType = other.fileType;
isCStyle = other.isCStyle;
isJavaStyle = other.isJavaStyle;
isSharpStyle = other.isSharpStyle;
// variables set by ASFormatter
// must also be updated in preprocessor
inLineNumber = other.inLineNumber;
outLineNumber = other.outLineNumber;
lineCommentNoBeautify = other.lineCommentNoBeautify;
isNonInStatementArray = other.isNonInStatementArray;
// private variables
indentString = other.indentString;
currentHeader = other.currentHeader;
previousLastLineHeader = other.previousLastLineHeader;
immediatelyPreviousAssignmentOp = other.immediatelyPreviousAssignmentOp;
probationHeader = other.probationHeader;
isInQuote = other.isInQuote;
isInComment = other.isInComment;
isInCase = other.isInCase;
isInQuestion = other.isInQuestion;
isInStatement = other.isInStatement;
isInHeader = other.isInHeader;
isInOperator = other.isInOperator;
isInTemplate = other.isInTemplate;
isInDefine = other.isInDefine;
isInDefineDefinition = other.isInDefineDefinition;
classIndent = other.classIndent;
isInClassHeader = other.isInClassHeader;
isInClassHeaderTab = other.isInClassHeaderTab;
switchIndent = other.switchIndent;
caseIndent = other.caseIndent;
namespaceIndent = other.namespaceIndent;
bracketIndent = other.bracketIndent;
blockIndent = other.blockIndent;
labelIndent = other.labelIndent;
preprocessorIndent = other.preprocessorIndent;
isInConditional = other.isInConditional;
isMinimalConditinalIndentSet = other.isMinimalConditinalIndentSet;
shouldForceTabIndentation = other.shouldForceTabIndentation;
emptyLineFill = other.emptyLineFill;
backslashEndsPrevLine = other.backslashEndsPrevLine;
blockCommentNoIndent = other.blockCommentNoIndent;
blockCommentNoBeautify = other.blockCommentNoBeautify;
previousLineProbationTab = other.previousLineProbationTab;
minConditionalIndent = other.minConditionalIndent;
parenDepth = other.parenDepth;
indentLength = other.indentLength;
blockTabCount = other.blockTabCount;
leadingWhiteSpaces = other.leadingWhiteSpaces;
maxInStatementIndent = other.maxInStatementIndent;
templateDepth = other.templateDepth;
prevFinalLineSpaceTabCount = other.prevFinalLineSpaceTabCount;
prevFinalLineTabCount = other.prevFinalLineTabCount;
defineTabCount = other.defineTabCount;
quoteChar = other.quoteChar;
prevNonSpaceCh = other.prevNonSpaceCh;
currentNonSpaceCh = other.currentNonSpaceCh;
currentNonLegalCh = other.currentNonLegalCh;
prevNonLegalCh = other.prevNonLegalCh;
}
/**
* ASBeautifier's destructor
*/
ASBeautifier::~ASBeautifier()
{
DELETE_CONTAINER(headerStack);
DELETE_CONTAINER(blockParenDepthStack);
DELETE_CONTAINER(blockStatementStack);
DELETE_CONTAINER(parenStatementStack);
DELETE_CONTAINER(bracketBlockStateStack);
DELETE_CONTAINER(inStatementIndentStack);
DELETE_CONTAINER(inStatementIndentStackSizeStack);
DELETE_CONTAINER(parenIndentStack);
DELETE_CONTAINER(waitingBeautifierStackLengthStack);
DELETE_CONTAINER(activeBeautifierStackLengthStack);
if(tempStacks){
vector< vector<const string*>* >::iterator iter = tempStacks->begin();
for(; iter != tempStacks->end(); iter++){
delete *iter;
}
tempStacks->clear();
delete tempStacks;
tempStacks = NULL;
}
if(waitingBeautifierStack){
vector<ASBeautifier*>::iterator iter = waitingBeautifierStack->begin();
for(; iter != waitingBeautifierStack->end(); iter++){
delete *iter;
}
waitingBeautifierStack->clear();
delete waitingBeautifierStack;
waitingBeautifierStack = NULL;
}
if(activeBeautifierStack){
vector<ASBeautifier*>::iterator iter = activeBeautifierStack->begin();
for(; iter != activeBeautifierStack->end(); iter++){
delete *iter;
}
activeBeautifierStack->clear();
delete activeBeautifierStack;
activeBeautifierStack = NULL;
}
}
/**
* initialize the ASBeautifier.
*
* init() should be called every time a ABeautifier object is to start
* beautifying a NEW source file.
* init() recieves a pointer to a DYNAMICALLY CREATED ASSourceIterator object
* that will be used to iterate through the source code. This object will be
* deleted during the ASBeautifier's destruction, and thus should not be
* deleted elsewhere.
*
* @param iter a pointer to the DYNAMICALLY CREATED ASSourceIterator object.
*/
void ASBeautifier::init(ASSourceIterator *iter)
{
sourceIterator = iter;
init();
}
/**
* initialize the ASBeautifier.
*/
void ASBeautifier::init()
{
initStatic();
INIT_CONTAINER(waitingBeautifierStack, new vector<ASBeautifier*>);
INIT_CONTAINER(activeBeautifierStack, new vector<ASBeautifier*>);
INIT_CONTAINER(waitingBeautifierStackLengthStack, new vector<int>);
INIT_CONTAINER(activeBeautifierStackLengthStack, new vector<int>);
INIT_CONTAINER(headerStack, new vector<const string*>);
INIT_CONTAINER(tempStacks, new vector<vector<const string*>*>);
tempStacks->push_back(new vector<const string*>);
INIT_CONTAINER(blockParenDepthStack, new vector<int>);
INIT_CONTAINER(blockStatementStack, new vector<bool>);
INIT_CONTAINER(parenStatementStack, new vector<bool>);
INIT_CONTAINER(bracketBlockStateStack, new vector<bool>);
bracketBlockStateStack->push_back(true);
INIT_CONTAINER(inStatementIndentStack, new vector<int>);
INIT_CONTAINER(inStatementIndentStackSizeStack, new vector<int>);
inStatementIndentStackSizeStack->push_back(0);
INIT_CONTAINER(parenIndentStack, new vector<int>);
immediatelyPreviousAssignmentOp = NULL;
previousLastLineHeader = NULL;
currentHeader = NULL;
isInQuote = false;
isInComment = false;
isInStatement = false;
isInCase = false;
isInQuestion = false;
isInClassHeader = false;
isInClassHeaderTab = false;
isInHeader = false;
isInOperator = false;
isInTemplate = false;
isInConditional = false;
templateDepth = 0;
parenDepth = 0;
blockTabCount = 0;
leadingWhiteSpaces = 0;
prevNonSpaceCh = '{';
currentNonSpaceCh = '{';
prevNonLegalCh = '{';
currentNonLegalCh = '{';
quoteChar = ' ';
prevFinalLineSpaceTabCount = 0;
prevFinalLineTabCount = 0;
probationHeader = NULL;
backslashEndsPrevLine = false;
isInDefine = false;
isInDefineDefinition = false;
defineTabCount = 0;
lineCommentNoBeautify = false;
blockCommentNoIndent = false;
blockCommentNoBeautify = false;
previousLineProbationTab = false;
isNonInStatementArray = false;
inLineNumber = -1; // for debugging
outLineNumber = 0; // for debugging
}
/**
* set indentation style to C/C++.
*/
void ASBeautifier::setCStyle()
{
fileType = C_TYPE;
isCStyle = true;
isJavaStyle = false;
isSharpStyle = false;
}
/**
* set indentation style to Java.
*/
void ASBeautifier::setJavaStyle()
{
fileType = JAVA_TYPE;
isJavaStyle = true;
isCStyle = false;
isSharpStyle = false;
}
/**
* set indentation style to C#.
*/
void ASBeautifier::setSharpStyle()
{
fileType = SHARP_TYPE;
isSharpStyle = true;
isCStyle = false;
isJavaStyle = false;
}
/**
* indent using one tab per indentation
*/
void ASBeautifier::setTabIndentation(int length, bool forceTabs)
{
indentString = "\t";
indentLength = length;
shouldForceTabIndentation = forceTabs;
if (!isMinimalConditinalIndentSet)
minConditionalIndent = indentLength * 2;
}
/**
* indent using a number of spaces per indentation.
*
* @param length number of spaces per indent.
*/
void ASBeautifier::setSpaceIndentation(int length)
{
indentString = string(length, ' ');
indentLength = length;
if (!isMinimalConditinalIndentSet)
minConditionalIndent = indentLength * 2;
}
/**
* set the maximum indentation between two lines in a multi-line statement.
*
* @param max maximum indentation length.
*/
void ASBeautifier::setMaxInStatementIndentLength(int max)
{
maxInStatementIndent = max;
}
/**
* set the minimum indentation between two lines in a multi-line condition.
*
* @param min minimal indentation length.
*/
void ASBeautifier::setMinConditionalIndentLength(int min)
{
minConditionalIndent = min;
isMinimalConditinalIndentSet = true;
}
/**
* set the state of the bracket indentation option. If true, brackets will
* be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setBracketIndent(bool state)
{
bracketIndent = state;
}
/**
* set the state of the block indentation option. If true, entire blocks
* will be indented one additional indent, similar to the GNU indent style.
*
* @param state state of option.
*/
void ASBeautifier::setBlockIndent(bool state)
{
if (state)
setBracketIndent(false); // so that we don't have both bracket and block indent
blockIndent = state;
}
/**
* set the state of the class indentation option. If true, C++ class
* definitions will be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setClassIndent(bool state)
{
classIndent = state;
}
/**
* set the state of the switch indentation option. If true, blocks of 'switch'
* statements will be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setSwitchIndent(bool state)
{
switchIndent = state;
}
/**
* set the state of the case indentation option. If true, lines of 'case'
* statements will be indented one additional indent.
*
* @param state state of option.
*/
void ASBeautifier::setCaseIndent(bool state)
{
caseIndent = state;
}
/**
* set the state of the namespace indentation option.
* If true, blocks of 'namespace' statements will be indented one
* additional indent. Otherwise, NO indentation will be added.
*
* @param state state of option.
*/
void ASBeautifier::setNamespaceIndent(bool state)
{
namespaceIndent = state;
}
/**
* set the state of the label indentation option.
* If true, labels will be indented one indent LESS than the
* current indentation level.
* If false, labels will be flushed to the left with NO
* indent at all.
*
* @param state state of option.
*/
void ASBeautifier::setLabelIndent(bool state)
{
labelIndent = state;
}
/**
* set the state of the preprocessor indentation option.
* If true, multiline #define statements will be indented.
*
* @param state state of option.
*/
void ASBeautifier::setPreprocessorIndent(bool state)
{
preprocessorIndent = state;
}
/**
* set the state of the empty line fill option.
* If true, empty lines will be filled with the whitespace.
* of their previous lines.
* If false, these lines will remain empty.
*
* @param state state of option.
*/
void ASBeautifier::setEmptyLineFill(bool state)
{
emptyLineFill = state;
}
/**
* get the number of spaces per indent
*
* @return value of indentLength option.
*/
int ASBeautifier::getIndentLength(void)
{
return indentLength;
}
/**
* get the char used for indentation, space or tab
*
* @return the char used for indentation.
*/
string ASBeautifier::getIndentString(void)
{
return indentString;
}
/**
* get the state of the case indentation option. If true, lines of 'case'
* statements will be indented one additional indent.
*
* @return state of caseIndent option.
*/
bool ASBeautifier::getCaseIndent(void)
{
return caseIndent;
}
/**
* get C style identifier.
* If true, a C source is being indented.
*
* @return state of isCStyle option.
*/
bool ASBeautifier::getCStyle(void)
{
return isCStyle;
}
/**
* get Java style identifier.
* If true, a Java source is being indented.
*
* @return state of isJavaStyle option.
*/
bool ASBeautifier::getJavaStyle(void)
{
return isJavaStyle;
}
/**
* get C# style identifier.
* If true, a C# source is being indented.
*
* @return state of isSharpStyle option.
*/
bool ASBeautifier::getSharpStyle(void)
{
return isSharpStyle;
}
/**
* get the state of the empty line fill option.
* If true, empty lines will be filled with the whitespace.
* of their previous lines.
* If false, these lines will remain empty.
*
* @return state of emptyLineFill option.
*/
bool ASBeautifier::getEmptyLineFill(void)
{
return emptyLineFill;
}
/**
* check if there are any indented lines ready to be read by nextLine()
*
* @return are there any indented lines ready?
*/
bool ASBeautifier::hasMoreLines() const
{
return sourceIterator->hasMoreLines();
}
/**
* get the next indented line.
*
* @return indented line.
*/
string ASBeautifier::nextLine()
{
return beautify(sourceIterator->nextLine());
}
/**
* beautify a line of source code.
* every line of source code in a source code file should be sent
* one after the other to the beautify method.
*
* @return the indented line.
* @param originalLine the original unindented line.
*/
string ASBeautifier::beautify(const string &originalLine)
{
string line;
bool isInLineComment = false;
bool lineStartsInComment = false;
bool isInClass = false;
bool isInSwitch = false;
bool isImmediatelyAfterConst = false;
bool isSpecialChar = false;
char ch = ' ';
char prevCh;
string outBuffer; // the newly idented line is bufferd here
int tabCount = 0;
const string *lastLineHeader = NULL;
bool closingBracketReached = false;
int spaceTabCount = 0;
char tempCh;
size_t headerStackSize = headerStack->size();
bool shouldIndentBrackettedLine = true;
int lineOpeningBlocksNum = 0;
int lineClosingBlocksNum = 0;
bool previousLineProbation = (probationHeader != NULL);
int i;
currentHeader = NULL;
lineStartsInComment = isInComment;
blockCommentNoBeautify = blockCommentNoIndent;
previousLineProbationTab = false;
outLineNumber++;
// handle and remove white spaces around the line:
// If not in comment, first find out size of white space before line,
// so that possible comments starting in the line continue in
// relation to the preliminary white-space.
if (!isInComment)
{
int strlen = originalLine.length();
leadingWhiteSpaces = 0;
for (int j = 0; j < strlen && isWhiteSpace(originalLine[j]); j++)
{
if (originalLine[j] == '\t')
leadingWhiteSpaces += indentLength;
else
leadingWhiteSpaces++;
}
line = trim(originalLine);
}
else
{
// convert leading tabs to spaces
string spaceTabs(indentLength, ' ');
string newLine = originalLine;
int strlen = newLine.length();
for (int j=0; j < leadingWhiteSpaces && j < strlen; j++)
{
if (newLine[j] == '\t')
{
newLine.replace(j, 1, spaceTabs);
strlen = newLine.length();
}
}
// trim the comment leaving the new leading whitespace
int trimSize = 0;
strlen = newLine.length();
while (trimSize < strlen
&& trimSize < leadingWhiteSpaces
&& isWhiteSpace(newLine[trimSize]))
trimSize++;
while (trimSize < strlen && isWhiteSpace(newLine[strlen-1]))
strlen--;
line = newLine.substr(trimSize, strlen);
size_t trimEnd = line.find_last_not_of(" \t");
if (trimEnd != string::npos)
{
int spacesToDelete = line.length() - 1 - trimEnd;
if (spacesToDelete > 0)
line.erase(trimEnd + 1, spacesToDelete);
}
}
if (line.length() == 0)
{
if (backslashEndsPrevLine) // must continue to clear variables
line = ' ';
else if (emptyLineFill)
return preLineWS(prevFinalLineSpaceTabCount, prevFinalLineTabCount);
else
return line;
}
// handle preprocessor commands
if (isCStyle && !isInComment && (line[0] == '#' || backslashEndsPrevLine))
{
if (line[0] == '#')
{
string preproc = trim(string(line.c_str() + 1));
// When finding a multi-lined #define statement, the original beautifier
// 1. sets its isInDefineDefinition flag
// 2. clones a new beautifier that will be used for the actual indentation
// of the #define. This clone is put into the activeBeautifierStack in order
// to be called for the actual indentation.
// The original beautifier will have isInDefineDefinition = true, isInDefine = false
// The cloned beautifier will have isInDefineDefinition = true, isInDefine = true
if (preprocessorIndent && preproc.compare(0, 6, "define") == 0 && line[line.length() - 1] == '\\')
{
if (!isInDefineDefinition)
{
ASBeautifier *defineBeautifier;
// this is the original beautifier
isInDefineDefinition = true;
// push a new beautifier into the active stack
// this beautifier will be used for the indentation of this define
defineBeautifier = new ASBeautifier(*this);
activeBeautifierStack->push_back(defineBeautifier);
}
else
{
// the is the cloned beautifier that is in charge of indenting the #define.
isInDefine = true;
}
}
else if (preproc.compare(0, 2, "if") == 0)
{
// push a new beautifier into the stack
waitingBeautifierStackLengthStack->push_back(waitingBeautifierStack->size());
activeBeautifierStackLengthStack->push_back(activeBeautifierStack->size());
waitingBeautifierStack->push_back(new ASBeautifier(*this));
}
else if (preproc.compare(0, 4/*2*/, "else") == 0)
{
if (waitingBeautifierStack && !waitingBeautifierStack->empty())
{
// MOVE current waiting beautifier to active stack.
activeBeautifierStack->push_back(waitingBeautifierStack->back());
waitingBeautifierStack->pop_back();
}
}
else if (preproc.compare(0, 4, "elif") == 0)
{
if (waitingBeautifierStack && !waitingBeautifierStack->empty())
{
// append a COPY current waiting beautifier to active stack, WITHOUT deleting the original.
activeBeautifierStack->push_back(new ASBeautifier(*(waitingBeautifierStack->back())));
}
}
else if (preproc.compare(0, 5, "endif") == 0)
{
int stackLength;
ASBeautifier *beautifier;
if (waitingBeautifierStackLengthStack && !waitingBeautifierStackLengthStack->empty())
{
stackLength = waitingBeautifierStackLengthStack->back();
waitingBeautifierStackLengthStack->pop_back();
while ((int) waitingBeautifierStack->size() > stackLength)
{
beautifier = waitingBeautifierStack->back();
waitingBeautifierStack->pop_back();
delete beautifier;
}
}
if (!activeBeautifierStackLengthStack->empty())
{
stackLength = activeBeautifierStackLengthStack->back();
activeBeautifierStackLengthStack->pop_back();
while ((int) activeBeautifierStack->size() > stackLength)
{
beautifier = activeBeautifierStack->back();
activeBeautifierStack->pop_back();
delete beautifier;
}
}
}
}
// check if the last char is a backslash
if (line.length() > 0)
backslashEndsPrevLine = (line[line.length() - 1] == '\\');
else
backslashEndsPrevLine = false;
// check if this line ends a multi-line #define
// if so, use the #define's cloned beautifier for the line's indentation
// and then remove it from the active beautifier stack and delete it.
if (!backslashEndsPrevLine && isInDefineDefinition && !isInDefine)
{
string beautifiedLine;
ASBeautifier *defineBeautifier;
isInDefineDefinition = false;
defineBeautifier = activeBeautifierStack->back();
activeBeautifierStack->pop_back();
beautifiedLine = defineBeautifier->beautify(line);
delete defineBeautifier;
return beautifiedLine;
}
// unless this is a multi-line #define, return this precompiler line as is.
if (!isInDefine && !isInDefineDefinition)
return originalLine;
}
// if there exists any worker beautifier in the activeBeautifierStack,
// then use it instead of me to indent the current line.
// variables set by ASFormatter must be updated.
if (!isInDefine && activeBeautifierStack != NULL && !activeBeautifierStack->empty())
{
activeBeautifierStack->back()->inLineNumber = inLineNumber;
activeBeautifierStack->back()->outLineNumber = outLineNumber;
activeBeautifierStack->back()->lineCommentNoBeautify = lineCommentNoBeautify;
activeBeautifierStack->back()->isNonInStatementArray = isNonInStatementArray;
return activeBeautifierStack->back()->beautify(line);
}
// calculate preliminary indentation based on data from past lines
if (!inStatementIndentStack->empty())
spaceTabCount = inStatementIndentStack->back();
for (i = 0; i < (int) headerStackSize; i++)
{
isInClass = false;
if (blockIndent || (!(i > 0 && (*headerStack)[i-1] != &AS_OPEN_BRACKET
&& (*headerStack)[i] == &AS_OPEN_BRACKET)))
++tabCount;
if (!isJavaStyle && !namespaceIndent && i >= 1
&& (*headerStack)[i-1] == &AS_NAMESPACE
&& (*headerStack)[i] == &AS_OPEN_BRACKET)
--tabCount;
if (isCStyle && i >= 1
&& (*headerStack)[i-1] == &AS_CLASS
&& (*headerStack)[i] == &AS_OPEN_BRACKET)
{
if (classIndent)
++tabCount;
isInClass = true;
}
// is the switchIndent option is on, indent switch statements an additional indent.
else if (switchIndent && i > 1 &&
(*headerStack)[i-1] == &AS_SWITCH &&
(*headerStack)[i] == &AS_OPEN_BRACKET
)
{
++tabCount;
isInSwitch = true;
}
}
if (!lineStartsInComment
&& isCStyle
&& isInClass
&& classIndent
&& headerStackSize >= 2
&& (*headerStack)[headerStackSize-2] == &AS_CLASS
&& (*headerStack)[headerStackSize-1] == &AS_OPEN_BRACKET
&& line[0] == '}')
--tabCount;
else if (!lineStartsInComment
&& isInSwitch
&& switchIndent
&& headerStackSize >= 2
&& (*headerStack)[headerStackSize-2] == &AS_SWITCH
&& (*headerStack)[headerStackSize-1] == &AS_OPEN_BRACKET
&& line[0] == '}')
--tabCount;
if (isInClassHeader)
{
isInClassHeaderTab = true;
tabCount += 2;
}
if (isInConditional)
{
--tabCount;
}
// parse characters in the current line.
for (i = 0; i < (int) line.length(); i++)
{
tempCh = line[i];
prevCh = ch;
ch = tempCh;
outBuffer.append(1, ch);
if (isWhiteSpace(ch))
continue;
// check for utf8 characters
// isalnum() will display an assert message in debug if not bypassed here
if (ch < 0)
continue;
// handle special characters (i.e. backslash+character such as \n, \t, ...)
if (isSpecialChar)
{
isSpecialChar = false;
continue;
}
if (!(isInComment || isInLineComment) && line.compare(i, 2, "\\\\") == 0)
{
outBuffer.append(1, '\\');
i++;
continue;
}
if (!(isInComment || isInLineComment) && ch == '\\')
{
isSpecialChar = true;
continue;
}
// handle quotes (such as 'x' and "Hello Dolly")
if (!(isInComment || isInLineComment) && (ch == '"' || ch == '\''))
if (!isInQuote)
{
quoteChar = ch;
isInQuote = true;
}
else if (quoteChar == ch)
{
isInQuote = false;
isInStatement = true;
continue;
}
if (isInQuote)
continue;