~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to lib/astyle/astyle.h

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2006-05-23 18:39:42 UTC
  • Revision ID: james.westby@ubuntu.com-20060523183942-hucifbvh68k2bwz7
Tags: upstream-3.3.2
Import upstream version 3.3.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (c) 1998,1999,2000,2001,2002 Tal Davidson. All rights reserved.
 
3
 *
 
4
 * astyle.h
 
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.
 
8
 *
 
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.
 
14
 *
 
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.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public
 
20
 * License along with this program.
 
21
 */
 
22
#ifndef ASTYLE_H
 
23
#define ASTYLE_H
 
24
 
 
25
#include "compiler_defines.h"
 
26
 
 
27
#include <string>
 
28
#include <vector>
 
29
 
 
30
 
 
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.
 
34
*/
 
35
enum BracketMode   { NONE_MODE, ATTACH_MODE, BREAK_MODE, BDAC_MODE };
 
36
enum BracketType   { NULL_TYPE = 0,
 
37
                     DEFINITION_TYPE = 1,
 
38
                     COMMAND_TYPE = 2,
 
39
                     ARRAY_TYPE  = 4,
 
40
                     SINGLE_LINE_TYPE = 8};
 
41
 
 
42
 
 
43
#ifdef USES_NAMESPACE
 
44
using namespace std;
 
45
 
 
46
namespace astyle
 
47
{
 
48
#endif
 
49
 
 
50
 
 
51
class ASSourceIterator
 
52
{
 
53
    public:
 
54
        virtual bool hasMoreLines() const = 0;
 
55
        virtual string nextLine() = 0;
 
56
};
 
57
 
 
58
 
 
59
 
 
60
class ASResource
 
61
{
 
62
    public:
 
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;
 
90
};
 
91
 
 
92
class ASBeautifier : protected ASResource
 
93
{
 
94
    public:
 
95
        ASBeautifier();
 
96
        virtual ~ASBeautifier();
 
97
        virtual void init(ASSourceIterator* iter); // pointer to dynamically created iterator.
 
98
        virtual void init();
 
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);
 
113
        void setCStyle();
 
114
        void setJavaStyle();
 
115
        void setEmptyLineFill(bool state);
 
116
        void setPreprocessorIndent(bool state);
 
117
 
 
118
 
 
119
    protected:
 
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);
 
128
 
 
129
    private:
 
130
        ASBeautifier(const ASBeautifier &copy);
 
131
        void operator=(ASBeautifier&); // not to be implemented
 
132
 
 
133
        void initStatic();
 
134
        void registerInStatementIndent(const string &line, int i, int spaceTabCount,
 
135
                                       int minIndent, bool updateParenStack);
 
136
        string preLineWS(int spaceTabCount, int tabCount);
 
137
 
 
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;
 
144
 
 
145
        static bool calledInitStatic;
 
146
 
 
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;
 
161
        string indentString;
 
162
        const string *currentHeader;
 
163
        const string *previousLastLineHeader;
 
164
        const string *immediatelyPreviousAssignmentOp;
 
165
        const string *probationHeader;
 
166
        bool isInQuote;
 
167
        bool isInComment;
 
168
        bool isInCase;
 
169
        bool isInQuestion;
 
170
        bool isInStatement;
 
171
        bool isInHeader;
 
172
        bool isCStyle;
 
173
        bool isInOperator;
 
174
        bool isInTemplate;
 
175
        bool isInConst;
 
176
        bool isInDefine;
 
177
        bool isInDefineDefinition;
 
178
        bool classIndent;
 
179
        bool isInClassHeader;
 
180
        bool isInClassHeaderTab;
 
181
        bool switchIndent;
 
182
        bool caseIndent;
 
183
        bool namespaceIndent;
 
184
        bool bracketIndent;
 
185
        bool blockIndent;
 
186
        bool labelIndent;
 
187
        bool preprocessorIndent;
 
188
        bool isInConditional;
 
189
        bool isMinimalConditinalIndentSet;
 
190
                bool shouldForceTabIndentation;
 
191
        int minConditionalIndent;
 
192
        int parenDepth;
 
193
        int indentLength;
 
194
        int blockTabCount;
 
195
        int leadingWhiteSpaces;
 
196
        int maxInStatementIndent;
 
197
        int templateDepth;
 
198
        char quoteChar;
 
199
        char prevNonSpaceCh;
 
200
        char currentNonSpaceCh;
 
201
        char currentNonLegalCh;
 
202
        char prevNonLegalCh;
 
203
        int prevFinalLineSpaceTabCount;
 
204
        int prevFinalLineTabCount;
 
205
        bool emptyLineFill;
 
206
        bool backslashEndsPrevLine;
 
207
        int defineTabCount;
 
208
};
 
209
 
 
210
 
 
211
class ASFormatter : public ASBeautifier
 
212
{
 
213
    public:
 
214
        ASFormatter();
 
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);
 
229
 
 
230
    private:
 
231
        void ASformatter(ASFormatter &copy); // not to be imlpemented
 
232
        void operator=(ASFormatter&); // not to be implemented
 
233
        void staticInit();
 
234
        bool isFormattingEnabled() const;
 
235
        void goForward(int i);
 
236
        bool getNextChar();
 
237
        char peekNextChar() const;
 
238
        bool isBeforeComment() const;
 
239
        void trimNewLine();
 
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();
 
249
        void breakLine();
 
250
        inline bool isSequenceReached(const string &sequence) const;
 
251
        const string *findHeader(const vector<const string*> &headers, bool checkBoundry = true);
 
252
 
 
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;
 
261
 
 
262
        ASSourceIterator *sourceIterator;
 
263
        vector<const string*> *preBracketHeaderStack;
 
264
        vector<BracketType> *bracketTypeStack;
 
265
        vector<int> *parenStack;
 
266
        string readyFormattedLine;
 
267
        string currentLine;
 
268
        string formattedLine;
 
269
        const string *currentHeader;
 
270
        const string *previousOperator;
 
271
        char currentChar;
 
272
        char previousChar;
 
273
        char previousNonWSChar;
 
274
        char previousCommandChar;
 
275
        char quoteChar;
 
276
        int charNum;
 
277
        BracketMode bracketFormatMode;
 
278
        bool isVirgin;
 
279
        bool shouldPadOperators;
 
280
        bool shouldPadParenthesies;
 
281
        bool shouldConvertTabs;
 
282
        bool isInLineComment;
 
283
        bool isInComment;
 
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;
 
287
        bool isInQuote;
 
288
        bool isSpecialChar;
 
289
        bool isNonParenHeader;
 
290
        bool foundQuestionMark;
 
291
        bool foundPreDefinitionHeader;
 
292
        bool foundPreCommandHeader;
 
293
        bool isInLineBreak;
 
294
        bool isInClosingBracketLineBreak;
 
295
        bool endOfCodeReached;
 
296
        bool isLineReady;
 
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;
 
307
        bool passedColon;
 
308
        bool isImmediatelyPostComment;
 
309
        bool isImmediatelyPostLineComment;
 
310
                bool isImmediatelyPostEmptyBlock;
 
311
 
 
312
            bool shouldBreakBlocks;
 
313
            bool shouldBreakClosingHeaderBlocks;
 
314
        bool isPrependPostBlockEmptyLineRequested;
 
315
        bool isAppendPostBlockEmptyLineRequested;
 
316
 
 
317
            bool prependEmptyLine;
 
318
            bool foundClosingHeader;
 
319
            int previousReadyFormattedLineLength;
 
320
 
 
321
                bool isInHeader;
 
322
                bool isImmediatelyPostHeader;
 
323
 
 
324
};
 
325
 
 
326
 
 
327
#ifdef USES_NAMESPACE
 
328
}
 
329
#endif
 
330
 
 
331
#endif // closes ASTYLE_H
 
332