00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef ASTYLE_H
00023 #define ASTYLE_H
00024
00025 #include "compiler_defines.h"
00026
00027 #include <string>
00028 #include <vector>
00029
00030
00031
00032
00033
00034
00035 enum BracketMode { NONE_MODE, ATTACH_MODE, BREAK_MODE, BDAC_MODE };
00036 enum BracketType { NULL_TYPE = 0,
00037 DEFINITION_TYPE = 1,
00038 COMMAND_TYPE = 2,
00039 ARRAY_TYPE = 4,
00040 SINGLE_LINE_TYPE = 8};
00041
00042
00043 #ifdef USES_NAMESPACE
00044 using namespace std;
00045
00046 namespace astyle
00047 {
00048 #endif
00049
00050
00051 class ASSourceIterator
00052 {
00053 public:
00054 virtual bool hasMoreLines() const = 0;
00055 virtual string nextLine() = 0;
00056 };
00057
00058
00059
00060 class ASResource
00061 {
00062 public:
00063 static const string AS_IF, AS_ELSE;
00064 static const string AS_DO, AS_WHILE;
00065 static const string AS_FOR;
00066 static const string AS_SWITCH, AS_CASE, AS_DEFAULT;
00067 static const string AS_TRY, AS_CATCH, AS_THROWS, AS_FINALLY;
00068 static const string AS_PUBLIC, AS_PROTECTED, AS_PRIVATE;
00069 static const string AS_CLASS, AS_STRUCT, AS_UNION, AS_INTERFACE, AS_NAMESPACE, AS_EXTERN;
00070 static const string AS_STATIC;
00071 static const string AS_CONST;
00072 static const string AS_SYNCHRONIZED;
00073 static const string AS_OPERATOR, AS_TEMPLATE;
00074 static const string AS_OPEN_BRACKET, AS_CLOSE_BRACKET;
00075 static const string AS_OPEN_LINE_COMMENT, AS_OPEN_COMMENT, AS_CLOSE_COMMENT;
00076 static const string AS_BAR_DEFINE, AS_BAR_INCLUDE, AS_BAR_IF, AS_BAR_EL, AS_BAR_ENDIF;
00077 static const string AS_RETURN;
00078 static const string AS_ASSIGN, AS_PLUS_ASSIGN, AS_MINUS_ASSIGN, AS_MULT_ASSIGN;
00079 static const string AS_DIV_ASSIGN, AS_MOD_ASSIGN, AS_XOR_ASSIGN, AS_OR_ASSIGN, AS_AND_ASSIGN;
00080 static const string AS_GR_GR_ASSIGN, AS_LS_LS_ASSIGN, AS_GR_GR_GR_ASSIGN, AS_LS_LS_LS_ASSIGN;
00081 static const string AS_EQUAL, AS_PLUS_PLUS, AS_MINUS_MINUS, AS_NOT_EQUAL, AS_GR_EQUAL, AS_GR_GR_GR, AS_GR_GR;
00082 static const string AS_LS_EQUAL, AS_LS_LS_LS, AS_LS_LS, AS_ARROW, AS_AND, AS_OR;
00083 static const string AS_COLON_COLON, AS_PAREN_PAREN, AS_BLPAREN_BLPAREN;
00084 static const string AS_PLUS, AS_MINUS, AS_MULT, AS_DIV, AS_MOD, AS_GR, AS_LS;
00085 static const string AS_NOT, AS_BIT_XOR, AS_BIT_OR, AS_BIT_AND, AS_BIT_NOT;
00086 static const string AS_QUESTION, AS_COLON, AS_SEMICOLON, AS_COMMA;
00087 static const string AS_ASM;
00088 static const string AS_FOREACH, AS_LOCK, AS_UNSAFE, AS_FIXED;
00089 static const string AS_GET, AS_SET, AS_ADD, AS_REMOVE;
00090 };
00091
00092 class ASBeautifier : protected ASResource
00093 {
00094 public:
00095 ASBeautifier();
00096 virtual ~ASBeautifier();
00097 virtual void init(ASSourceIterator* iter);
00098 virtual void init();
00099 virtual bool hasMoreLines() const;
00100 virtual string nextLine();
00101 virtual string beautify(const string &line);
00102 void setTabIndentation(int length = 4, bool forceTabs = false);
00103 void setSpaceIndentation(int length = 4);
00104 void setMaxInStatementIndentLength(int max);
00105 void setMinConditionalIndentLength(int min);
00106 void setClassIndent(bool state);
00107 void setSwitchIndent(bool state);
00108 void setCaseIndent(bool state);
00109 void setBracketIndent(bool state);
00110 void setBlockIndent(bool state);
00111 void setNamespaceIndent(bool state);
00112 void setLabelIndent(bool state);
00113 void setCStyle();
00114 void setJavaStyle();
00115 void setEmptyLineFill(bool state);
00116 void setPreprocessorIndent(bool state);
00117
00118
00119 protected:
00120 int getNextProgramCharDistance(const string &line, int i);
00121 bool isLegalNameChar(char ch) const;
00122 bool isWhiteSpace(char ch) const;
00123 const string *findHeader(const string &line, int i,
00124 const vector<const string*> &possibleHeaders,
00125 bool checkBoundry = true);
00126 string trim(const string &str);
00127 int indexOf(vector<const string*> &container, const string *element);
00128
00129 private:
00130 ASBeautifier(const ASBeautifier ©);
00131 void operator=(ASBeautifier&);
00132
00133 void initStatic();
00134 void registerInStatementIndent(const string &line, int i, int spaceTabCount,
00135 int minIndent, bool updateParenStack);
00136 string preLineWS(int spaceTabCount, int tabCount);
00137
00138 static vector<const string*> headers;
00139 static vector<const string*> nonParenHeaders;
00140 static vector<const string*> preprocessorHeaders;
00141 static vector<const string*> preBlockStatements;
00142 static vector<const string*> assignmentOperators;
00143 static vector<const string*> nonAssignmentOperators;
00144
00145 static bool calledInitStatic;
00146
00147 ASSourceIterator *sourceIterator;
00148 vector<ASBeautifier*> *waitingBeautifierStack;
00149 vector<ASBeautifier*> *activeBeautifierStack;
00150 vector<int> *waitingBeautifierStackLengthStack;
00151 vector<int> *activeBeautifierStackLengthStack;
00152 vector<const string*> *headerStack;
00153 vector< vector<const string*>* > *tempStacks;
00154 vector<int> *blockParenDepthStack;
00155 vector<bool> *blockStatementStack;
00156 vector<bool> *parenStatementStack;
00157 vector<int> *inStatementIndentStack;
00158 vector<int> *inStatementIndentStackSizeStack;
00159 vector<int> *parenIndentStack;
00160 vector<bool> *bracketBlockStateStack;
00161 string indentString;
00162 const string *currentHeader;
00163 const string *previousLastLineHeader;
00164 const string *immediatelyPreviousAssignmentOp;
00165 const string *probationHeader;
00166 bool isInQuote;
00167 bool isInComment;
00168 bool isInCase;
00169 bool isInQuestion;
00170 bool isInStatement;
00171 bool isInHeader;
00172 bool isCStyle;
00173 bool isInOperator;
00174 bool isInTemplate;
00175 bool isInConst;
00176 bool isInDefine;
00177 bool isInDefineDefinition;
00178 bool classIndent;
00179 bool isInClassHeader;
00180 bool isInClassHeaderTab;
00181 bool switchIndent;
00182 bool caseIndent;
00183 bool namespaceIndent;
00184 bool bracketIndent;
00185 bool blockIndent;
00186 bool labelIndent;
00187 bool preprocessorIndent;
00188 bool isInConditional;
00189 bool isMinimalConditinalIndentSet;
00190 bool shouldForceTabIndentation;
00191 int minConditionalIndent;
00192 int parenDepth;
00193 int indentLength;
00194 int blockTabCount;
00195 int leadingWhiteSpaces;
00196 int maxInStatementIndent;
00197 int templateDepth;
00198 char quoteChar;
00199 char prevNonSpaceCh;
00200 char currentNonSpaceCh;
00201 char currentNonLegalCh;
00202 char prevNonLegalCh;
00203 int prevFinalLineSpaceTabCount;
00204 int prevFinalLineTabCount;
00205 bool emptyLineFill;
00206 bool backslashEndsPrevLine;
00207 int defineTabCount;
00208 };
00209
00210
00211 class ASFormatter : public ASBeautifier
00212 {
00213 public:
00214 ASFormatter();
00215 virtual ~ASFormatter();
00216 virtual void init(ASSourceIterator* iter);
00217 virtual bool hasMoreLines() const;
00218 virtual string nextLine();
00219 void setBracketFormatMode(BracketMode mode);
00220 void setBreakClosingHeaderBracketsMode(bool state);
00221 void setOperatorPaddingMode(bool mode);
00222 void setParenthesisPaddingMode(bool mode);
00223 void setBreakOneLineBlocksMode(bool state);
00224 void setSingleStatementsMode(bool state);
00225 void setTabSpaceConversionMode(bool state);
00226 void setBreakBlocksMode(bool state);
00227 void setBreakClosingHeaderBlocksMode(bool state);
00228 void setBreakElseIfsMode(bool state);
00229
00230 private:
00231 void ASformatter(ASFormatter ©);
00232 void operator=(ASFormatter&);
00233 void staticInit();
00234 bool isFormattingEnabled() const;
00235 void goForward(int i);
00236 bool getNextChar();
00237 char peekNextChar() const;
00238 bool isBeforeComment() const;
00239 void trimNewLine();
00240 BracketType getBracketType() const;
00241 bool isPointerOrReference() const;
00242 bool isUnaryMinus() const;
00243 bool isInExponent() const;
00244 bool isOneLineBlockReached() const;
00245 void appendChar(char ch, bool canBreakLine = true);
00246 void appendCurrentChar(bool canBreakLine = true);
00247 void appendSequence(const string &sequence, bool canBreakLine = true);
00248 void appendSpacePad();
00249 void breakLine();
00250 inline bool isSequenceReached(const string &sequence) const;
00251 const string *findHeader(const vector<const string*> &headers, bool checkBoundry = true);
00252
00253 static vector<const string*> headers;
00254 static vector<const string*> nonParenHeaders;
00255 static vector<const string*> preprocessorHeaders;
00256 static vector<const string*> preDefinitionHeaders;
00257 static vector<const string*> preCommandHeaders;
00258 static vector<const string*> operators;
00259 static vector<const string*> assignmentOperators;
00260 static bool calledInitStatic;
00261
00262 ASSourceIterator *sourceIterator;
00263 vector<const string*> *preBracketHeaderStack;
00264 vector<BracketType> *bracketTypeStack;
00265 vector<int> *parenStack;
00266 string readyFormattedLine;
00267 string currentLine;
00268 string formattedLine;
00269 const string *currentHeader;
00270 const string *previousOperator;
00271 char currentChar;
00272 char previousChar;
00273 char previousNonWSChar;
00274 char previousCommandChar;
00275 char quoteChar;
00276 int charNum;
00277 BracketMode bracketFormatMode;
00278 bool isVirgin;
00279 bool shouldPadOperators;
00280 bool shouldPadParenthesies;
00281 bool shouldConvertTabs;
00282 bool isInLineComment;
00283 bool isInComment;
00284 bool isInPreprocessor;
00285 bool isInTemplate;
00286 bool doesLineStartComment;
00287 bool isInQuote;
00288 bool isSpecialChar;
00289 bool isNonParenHeader;
00290 bool foundQuestionMark;
00291 bool foundPreDefinitionHeader;
00292 bool foundPreCommandHeader;
00293 bool isInLineBreak;
00294 bool isInClosingBracketLineBreak;
00295 bool endOfCodeReached;
00296 bool isLineReady;
00297 bool isPreviousBracketBlockRelated;
00298 bool isInPotentialCalculation;
00299
00300 bool shouldBreakOneLineBlocks;
00301 bool shouldReparseCurrentChar;
00302 bool shouldBreakOneLineStatements;
00303 bool shouldBreakLineAfterComments;
00304 bool shouldBreakClosingHeaderBrackets;
00305 bool shouldBreakElseIfs;
00306 bool passedSemicolon;
00307 bool passedColon;
00308 bool isImmediatelyPostComment;
00309 bool isImmediatelyPostLineComment;
00310 bool isImmediatelyPostEmptyBlock;
00311
00312 bool shouldBreakBlocks;
00313 bool shouldBreakClosingHeaderBlocks;
00314 bool isPrependPostBlockEmptyLineRequested;
00315 bool isAppendPostBlockEmptyLineRequested;
00316
00317 bool prependEmptyLine;
00318 bool foundClosingHeader;
00319 int previousReadyFormattedLineLength;
00320
00321 bool isInHeader;
00322 bool isImmediatelyPostHeader;
00323
00324 };
00325
00326
00327 #ifdef USES_NAMESPACE
00328 }
00329 #endif
00330
00331 #endif // closes ASTYLE_H
00332