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