2
* Copyright (c) 1998,1999,2000,2001,2002 Tal Davidson. All rights reserved.
5
* by Tal Davidson (davidsont@bigfoot.com)
6
* This file is a part of "Artistic Style" - an indentater and reformatter
7
* of C, C++, C# and Java source files.
9
* The "Artistic Style" project, including all files needed to compile it,
10
* is free software; you can redistribute it and/or use it and/or modify it
11
* under the terms of the GNU General Public License as published
12
* by the Free Software Foundation; either version 2 of the License,
13
* or (at your option) any later version.
15
* This program is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19
* You should have received a copy of the GNU General Public
20
* License along with this program.
25
#include "compiler_defines.h"
31
/* The enums below ave been moved up from inside the namespace astyle, since they
32
for some strange reason are not recognized by 'vectors' in Microsoft Visual C++ 5
33
when they are part of a namespace!!! There was no such problem with GNU's g++ compiler.
35
enum BracketMode { NONE_MODE, ATTACH_MODE, BREAK_MODE, BDAC_MODE };
36
enum BracketType { NULL_TYPE = 0,
40
SINGLE_LINE_TYPE = 8};
51
class ASSourceIterator
54
virtual bool hasMoreLines() const = 0;
55
virtual string nextLine() = 0;
63
static const string AS_IF, AS_ELSE;
64
static const string AS_DO, AS_WHILE;
65
static const string AS_FOR;
66
static const string AS_SWITCH, AS_CASE, AS_DEFAULT;
67
static const string AS_TRY, AS_CATCH, AS_THROWS, AS_FINALLY;
68
static const string AS_PUBLIC, AS_PROTECTED, AS_PRIVATE;
69
static const string AS_CLASS, AS_STRUCT, AS_UNION, AS_INTERFACE, AS_NAMESPACE, AS_EXTERN;
70
static const string AS_STATIC;
71
static const string AS_CONST;
72
static const string AS_SYNCHRONIZED;
73
static const string AS_OPERATOR, AS_TEMPLATE;
74
static const string AS_OPEN_BRACKET, AS_CLOSE_BRACKET;
75
static const string AS_OPEN_LINE_COMMENT, AS_OPEN_COMMENT, AS_CLOSE_COMMENT;
76
static const string AS_BAR_DEFINE, AS_BAR_INCLUDE, AS_BAR_IF, AS_BAR_EL, AS_BAR_ENDIF;
77
static const string AS_RETURN;
78
static const string AS_ASSIGN, AS_PLUS_ASSIGN, AS_MINUS_ASSIGN, AS_MULT_ASSIGN;
79
static const string AS_DIV_ASSIGN, AS_MOD_ASSIGN, AS_XOR_ASSIGN, AS_OR_ASSIGN, AS_AND_ASSIGN;
80
static const string AS_GR_GR_ASSIGN, AS_LS_LS_ASSIGN, AS_GR_GR_GR_ASSIGN, AS_LS_LS_LS_ASSIGN;
81
static const string AS_EQUAL, AS_PLUS_PLUS, AS_MINUS_MINUS, AS_NOT_EQUAL, AS_GR_EQUAL, AS_GR_GR_GR, AS_GR_GR;
82
static const string AS_LS_EQUAL, AS_LS_LS_LS, AS_LS_LS, AS_ARROW, AS_AND, AS_OR;
83
static const string AS_COLON_COLON, AS_PAREN_PAREN, AS_BLPAREN_BLPAREN;
84
static const string AS_PLUS, AS_MINUS, AS_MULT, AS_DIV, AS_MOD, AS_GR, AS_LS;
85
static const string AS_NOT, AS_BIT_XOR, AS_BIT_OR, AS_BIT_AND, AS_BIT_NOT;
86
static const string AS_QUESTION, AS_COLON, AS_SEMICOLON, AS_COMMA;
87
static const string AS_ASM;
88
static const string AS_FOREACH, AS_LOCK, AS_UNSAFE, AS_FIXED;
89
static const string AS_GET, AS_SET, AS_ADD, AS_REMOVE;
92
class ASBeautifier : protected ASResource
96
virtual ~ASBeautifier();
97
virtual void init(ASSourceIterator* iter); // pointer to dynamically created iterator.
99
virtual bool hasMoreLines() const;
100
virtual string nextLine();
101
virtual string beautify(const string &line);
102
void setTabIndentation(int length = 4, bool forceTabs = false);
103
void setSpaceIndentation(int length = 4);
104
void setMaxInStatementIndentLength(int max);
105
void setMinConditionalIndentLength(int min);
106
void setClassIndent(bool state);
107
void setSwitchIndent(bool state);
108
void setCaseIndent(bool state);
109
void setBracketIndent(bool state);
110
void setBlockIndent(bool state);
111
void setNamespaceIndent(bool state);
112
void setLabelIndent(bool state);
115
void setEmptyLineFill(bool state);
116
void setPreprocessorIndent(bool state);
120
int getNextProgramCharDistance(const string &line, int i);
121
bool isLegalNameChar(char ch) const;
122
bool isWhiteSpace(char ch) const;
123
const string *findHeader(const string &line, int i,
124
const vector<const string*> &possibleHeaders,
125
bool checkBoundry = true);
126
string trim(const string &str);
127
int indexOf(vector<const string*> &container, const string *element);
130
ASBeautifier(const ASBeautifier ©);
131
void operator=(ASBeautifier&); // not to be implemented
134
void registerInStatementIndent(const string &line, int i, int spaceTabCount,
135
int minIndent, bool updateParenStack);
136
string preLineWS(int spaceTabCount, int tabCount);
138
static vector<const string*> headers;
139
static vector<const string*> nonParenHeaders;
140
static vector<const string*> preprocessorHeaders;
141
static vector<const string*> preBlockStatements;
142
static vector<const string*> assignmentOperators;
143
static vector<const string*> nonAssignmentOperators;
145
static bool calledInitStatic;
147
ASSourceIterator *sourceIterator;
148
vector<ASBeautifier*> *waitingBeautifierStack;
149
vector<ASBeautifier*> *activeBeautifierStack;
150
vector<int> *waitingBeautifierStackLengthStack;
151
vector<int> *activeBeautifierStackLengthStack;
152
vector<const string*> *headerStack;
153
vector< vector<const string*>* > *tempStacks;
154
vector<int> *blockParenDepthStack;
155
vector<bool> *blockStatementStack;
156
vector<bool> *parenStatementStack;
157
vector<int> *inStatementIndentStack;
158
vector<int> *inStatementIndentStackSizeStack;
159
vector<int> *parenIndentStack;
160
vector<bool> *bracketBlockStateStack;
162
const string *currentHeader;
163
const string *previousLastLineHeader;
164
const string *immediatelyPreviousAssignmentOp;
165
const string *probationHeader;
177
bool isInDefineDefinition;
179
bool isInClassHeader;
180
bool isInClassHeaderTab;
183
bool namespaceIndent;
187
bool preprocessorIndent;
188
bool isInConditional;
189
bool isMinimalConditinalIndentSet;
190
bool shouldForceTabIndentation;
191
int minConditionalIndent;
195
int leadingWhiteSpaces;
196
int maxInStatementIndent;
200
char currentNonSpaceCh;
201
char currentNonLegalCh;
203
int prevFinalLineSpaceTabCount;
204
int prevFinalLineTabCount;
206
bool backslashEndsPrevLine;
211
class ASFormatter : public ASBeautifier
215
virtual ~ASFormatter();
216
virtual void init(ASSourceIterator* iter);
217
virtual bool hasMoreLines() const;
218
virtual string nextLine();
219
void setBracketFormatMode(BracketMode mode);
220
void setBreakClosingHeaderBracketsMode(bool state);
221
void setOperatorPaddingMode(bool mode);
222
void setParenthesisPaddingMode(bool mode);
223
void setBreakOneLineBlocksMode(bool state);
224
void setSingleStatementsMode(bool state);
225
void setTabSpaceConversionMode(bool state);
226
void setBreakBlocksMode(bool state);
227
void setBreakClosingHeaderBlocksMode(bool state);
228
void setBreakElseIfsMode(bool state);
231
void ASformatter(ASFormatter ©); // not to be imlpemented
232
void operator=(ASFormatter&); // not to be implemented
234
bool isFormattingEnabled() const;
235
void goForward(int i);
237
char peekNextChar() const;
238
bool isBeforeComment() const;
240
BracketType getBracketType() const;
241
bool isPointerOrReference() const;
242
bool isUnaryMinus() const;
243
bool isInExponent() const;
244
bool isOneLineBlockReached() const;
245
void appendChar(char ch, bool canBreakLine = true);
246
void appendCurrentChar(bool canBreakLine = true);
247
void appendSequence(const string &sequence, bool canBreakLine = true);
248
void appendSpacePad();
250
inline bool isSequenceReached(const string &sequence) const;
251
const string *findHeader(const vector<const string*> &headers, bool checkBoundry = true);
253
static vector<const string*> headers;
254
static vector<const string*> nonParenHeaders;
255
static vector<const string*> preprocessorHeaders;
256
static vector<const string*> preDefinitionHeaders;
257
static vector<const string*> preCommandHeaders;
258
static vector<const string*> operators;
259
static vector<const string*> assignmentOperators;
260
static bool calledInitStatic;
262
ASSourceIterator *sourceIterator;
263
vector<const string*> *preBracketHeaderStack;
264
vector<BracketType> *bracketTypeStack;
265
vector<int> *parenStack;
266
string readyFormattedLine;
268
string formattedLine;
269
const string *currentHeader;
270
const string *previousOperator;
273
char previousNonWSChar;
274
char previousCommandChar;
277
BracketMode bracketFormatMode;
279
bool shouldPadOperators;
280
bool shouldPadParenthesies;
281
bool shouldConvertTabs;
282
bool isInLineComment;
284
bool isInPreprocessor;
285
bool isInTemplate; // true both in template definitions (e.g. template<class A>) and template usage (e.g. F<int>).
286
bool doesLineStartComment;
289
bool isNonParenHeader;
290
bool foundQuestionMark;
291
bool foundPreDefinitionHeader;
292
bool foundPreCommandHeader;
294
bool isInClosingBracketLineBreak;
295
bool endOfCodeReached;
297
bool isPreviousBracketBlockRelated;
298
bool isInPotentialCalculation;
299
//bool foundOneLineBlock;
300
bool shouldBreakOneLineBlocks;
301
bool shouldReparseCurrentChar;
302
bool shouldBreakOneLineStatements;
303
bool shouldBreakLineAfterComments;
304
bool shouldBreakClosingHeaderBrackets;
305
bool shouldBreakElseIfs;
306
bool passedSemicolon;
308
bool isImmediatelyPostComment;
309
bool isImmediatelyPostLineComment;
310
bool isImmediatelyPostEmptyBlock;
312
bool shouldBreakBlocks;
313
bool shouldBreakClosingHeaderBlocks;
314
bool isPrependPostBlockEmptyLineRequested;
315
bool isAppendPostBlockEmptyLineRequested;
317
bool prependEmptyLine;
318
bool foundClosingHeader;
319
int previousReadyFormattedLineLength;
322
bool isImmediatelyPostHeader;
327
#ifdef USES_NAMESPACE
331
#endif // closes ASTYLE_H