File indexing completed on 2024-04-21 04:36:12

0001 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
0002  *
0003  *   Copyright (C) 2006-2010 by Jim Pattee <jimp03@email.com>
0004  *   Copyright (C) 1998-2002 by Tal Davidson
0005  *   <http://www.gnu.org/licenses/lgpl-3.0.html>
0006  *
0007  *   This file is a part of Artistic Style - an indentation and
0008  *   reformatting tool for C, C++, C# and Java source files.
0009  *   <http://astyle.sourceforge.net>
0010  *
0011  *   Artistic Style is free software: you can redistribute it and/or modify
0012  *   it under the terms of the GNU Lesser General Public License as published
0013  *   by the Free Software Foundation, either version 3 of the License, or
0014  *   (at your option) any later version.
0015  *
0016  *   Artistic Style is distributed in the hope that it will be useful,
0017  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0018  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0019  *   GNU Lesser General Public License for more details.
0020  *
0021  *   You should have received a copy of the GNU Lesser General Public License
0022  *   along with Artistic Style.  If not, see <http://www.gnu.org/licenses/>.
0023  *
0024  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
0025  */
0026 
0027 #ifndef ASTYLE_H
0028 #define ASTYLE_H
0029 
0030 #ifdef __VMS
0031 #define __USE_STD_IOSTREAM 1
0032 #include <assert>
0033 #else
0034 #include <cassert>
0035 #endif
0036 
0037 #include <string.h>         // need both string and string.h for GCC
0038 #include <string>
0039 #include <vector>
0040 #include <cctype>
0041 
0042 #ifdef _WIN32
0043 #define STDCALL __stdcall
0044 #define EXPORT  __declspec(dllexport)
0045 #else
0046 #define STDCALL
0047 #define EXPORT
0048 #endif
0049 
0050 #ifdef _MSC_VER
0051 #pragma warning(disable: 4996)  // secure version deprecation warnings
0052 #pragma warning(disable: 4267)  // 64 bit signed/unsigned loss of data
0053 #endif
0054 
0055 #ifdef __BORLANDC__
0056 #pragma warn -aus   // variable is assigned a value that is never used in function.
0057 #endif
0058 
0059 #ifdef __INTEL_COMPILER
0060 #pragma warning(disable:  383)  // value copied to temporary, reference to temporary used
0061 #pragma warning(disable:  444)  // destructor for base class is not virtual
0062 #pragma warning(disable:  981)  // operands are evaluated in unspecified order
0063 #endif
0064 
0065 using namespace std;
0066 
0067 namespace astyle
0068 {
0069 
0070 enum FileType      { C_TYPE=0, JAVA_TYPE=1, SHARP_TYPE=2 };
0071 
0072 /* The enums below are not recognized by 'vectors' in Microsoft Visual C++
0073    V5 when they are part of a namespace!!!  Use Visual C++ V6 or higher.
0074 */
0075 enum FormatStyle   { STYLE_NONE,
0076                      STYLE_ALLMAN,
0077                      STYLE_JAVA,
0078                      STYLE_KandR,
0079                      STYLE_STROUSTRUP,
0080                      STYLE_WHITESMITH,
0081                      STYLE_BANNER,
0082                      STYLE_GNU,
0083                      STYLE_LINUX,
0084                      STYLE_HORSTMANN,
0085                      STYLE_1TBS
0086                    };
0087 
0088 enum BracketMode   { NONE_MODE,
0089                      ATTACH_MODE,
0090                      BREAK_MODE,
0091                      LINUX_MODE,
0092                      STROUSTRUP_MODE,
0093                      HORSTMANN_MODE,
0094                      BDAC_MODE = LINUX_MODE
0095                    };
0096 
0097 enum BracketType   { NULL_TYPE = 0,
0098                      NAMESPACE_TYPE = 1,        // also a DEFINITION_TYPE
0099                      CLASS_TYPE = 2,            // also a DEFINITION_TYPE
0100                      STRUCT_TYPE = 4,           // also a DEFINITION_TYPE
0101                      INTERFACE_TYPE = 8,        // also a DEFINITION_TYPE
0102                      DEFINITION_TYPE = 16,
0103                      COMMAND_TYPE = 32,
0104                      ARRAY_NIS_TYPE = 64,       // also an ARRAY_TYPE
0105                      ARRAY_TYPE = 128,          // arrays and enums
0106                      EXTERN_TYPE = 256,         // extern "C". not a command type extern
0107                      SINGLE_LINE_TYPE = 512
0108                    };
0109 
0110 enum PointerAlign { ALIGN_NONE,
0111                     ALIGN_TYPE,
0112                     ALIGN_MIDDLE,
0113                     ALIGN_NAME
0114                   };
0115 
0116 enum FileEncoding { ENCODING_OK,
0117                     UTF_16BE,
0118                     UTF_16LE,
0119                     UTF_32BE,
0120                     UTF_32LE
0121                   };
0122 
0123 enum LineEndFormat { LINEEND_DEFAULT,   // Use line break that matches most of the file
0124                      LINEEND_WINDOWS,
0125                      LINEEND_LINUX,
0126                      LINEEND_MACOLD,
0127                      LINEEND_CRLF = LINEEND_WINDOWS,
0128                      LINEEND_LF   = LINEEND_LINUX,
0129                      LINEEND_CR   = LINEEND_MACOLD
0130                    };
0131 
0132 
0133 //----------------------------------------------------------------------------
0134 // Class ASSourceIterator
0135 // A pure virtual class is used by ASFormatter and ASBeautifier instead of
0136 // ASStreamIterator. This allows programs using AStyle as a plugin to define
0137 // their own ASStreamIterator. The ASStreamIterator class must inherit
0138 // this class.
0139 //----------------------------------------------------------------------------
0140 
0141 class ASSourceIterator
0142 {
0143     public:
0144         ASSourceIterator() {}
0145         virtual ~ASSourceIterator() {}
0146         virtual bool hasMoreLines() const = 0;
0147         virtual string nextLine(bool emptyLineWasDeleted = false) = 0;
0148         virtual string peekNextLine() = 0;
0149         virtual void peekReset() = 0;
0150 };
0151 
0152 //----------------------------------------------------------------------------
0153 // Class ASResource
0154 //----------------------------------------------------------------------------
0155 
0156 class ASResource
0157 {
0158     public:
0159         void buildAssignmentOperators(vector<const string*>* assignmentOperators);
0160         void buildCastOperators(vector<const string*>* castOperators);
0161         void buildHeaders(vector<const string*>* headers, int fileType, bool beautifier=false);
0162         void buildIndentableHeaders(vector<const string*>* indentableHeaders);
0163         void buildNonAssignmentOperators(vector<const string*>* nonAssignmentOperators);
0164         void buildNonParenHeaders(vector<const string*>* nonParenHeaders, int fileType, bool beautifier=false);
0165         void buildOperators(vector<const string*>* operators);
0166         void buildPreBlockStatements(vector<const string*>* preBlockStatements, int fileType);
0167         void buildPreCommandHeaders(vector<const string*>* preCommandHeaders, int fileType);
0168         void buildPreDefinitionHeaders(vector<const string*>* preDefinitionHeaders, int fileType);
0169 
0170     public:
0171         static const string AS_IF, AS_ELSE;
0172         static const string AS_DO, AS_WHILE;
0173         static const string AS_FOR;
0174         static const string AS_SWITCH, AS_CASE, AS_DEFAULT;
0175         static const string AS_TRY, AS_CATCH, AS_THROWS, AS_FINALLY;
0176         static const string AS_PUBLIC, AS_PROTECTED, AS_PRIVATE;
0177         static const string AS_CLASS, AS_STRUCT, AS_UNION, AS_INTERFACE, AS_NAMESPACE;
0178         static const string AS_EXTERN, AS_ENUM;
0179         static const string AS_STATIC, AS_CONST, AS_WHERE, AS_NEW;
0180         static const string AS_SYNCHRONIZED;
0181         static const string AS_OPERATOR, AS_TEMPLATE;
0182         static const string AS_OPEN_BRACKET, AS_CLOSE_BRACKET;
0183         static const string AS_OPEN_LINE_COMMENT, AS_OPEN_COMMENT, AS_CLOSE_COMMENT;
0184         static const string AS_BAR_DEFINE, AS_BAR_INCLUDE, AS_BAR_IF, AS_BAR_EL, AS_BAR_ENDIF;
0185         static const string AS_RETURN;
0186         static const string AS_CIN, AS_COUT, AS_CERR;
0187         static const string AS_ASSIGN, AS_PLUS_ASSIGN, AS_MINUS_ASSIGN, AS_MULT_ASSIGN;
0188         static const string AS_DIV_ASSIGN, AS_MOD_ASSIGN, AS_XOR_ASSIGN, AS_OR_ASSIGN, AS_AND_ASSIGN;
0189         static const string AS_GR_GR_ASSIGN, AS_LS_LS_ASSIGN, AS_GR_GR_GR_ASSIGN, AS_LS_LS_LS_ASSIGN;
0190         static const string AS_GCC_MIN_ASSIGN, AS_GCC_MAX_ASSIGN;
0191         static const string AS_EQUAL, AS_PLUS_PLUS, AS_MINUS_MINUS, AS_NOT_EQUAL, AS_GR_EQUAL, AS_GR_GR_GR, AS_GR_GR;
0192         static const string AS_LS_EQUAL, AS_LS_LS_LS, AS_LS_LS;
0193         static const string AS_QUESTION_QUESTION, AS_EQUAL_GR;
0194         static const string AS_ARROW, AS_AND, AS_OR;
0195         static const string AS_COLON_COLON, AS_PAREN_PAREN, AS_BLPAREN_BLPAREN;
0196         static const string AS_PLUS, AS_MINUS, AS_MULT, AS_DIV, AS_MOD, AS_GR, AS_LS;
0197         static const string AS_NOT, AS_BIT_XOR, AS_BIT_OR, AS_BIT_AND, AS_BIT_NOT;
0198         static const string AS_QUESTION, AS_COLON, AS_SEMICOLON, AS_COMMA;
0199         static const string AS_ASM, AS__ASM__, AS_MS_ASM, AS_MS__ASM;
0200         static const string AS_FOREACH, AS_LOCK, AS_UNSAFE, AS_FIXED;
0201         static const string AS_GET, AS_SET, AS_ADD, AS_REMOVE;
0202         static const string AS_DELEGATE, AS_UNCHECKED;
0203         static const string AS_CONST_CAST, AS_DYNAMIC_CAST, AS_REINTERPRET_CAST, AS_STATIC_CAST;
0204 };  // Class ASResource
0205 
0206 //----------------------------------------------------------------------------
0207 // Class ASBase
0208 //----------------------------------------------------------------------------
0209 
0210 class ASBase
0211 {
0212     private:
0213         // all variables should be set by the "init" function
0214         int baseFileType;      // a value from enum FileType
0215 
0216     protected:
0217         ASBase() { baseFileType = C_TYPE; }
0218         ~ASBase() {}
0219 
0220         // functions definitions are at the end of ASResource.cpp
0221         bool findKeyword(const string &line, int i, const string &keyword) const;
0222         string getCurrentWord(const string& line, size_t index) const;
0223 
0224     protected:
0225 
0226         void init(int fileTypeArg) { baseFileType = fileTypeArg; }
0227         bool isCStyle() const { return (baseFileType == C_TYPE); }
0228         bool isJavaStyle() const { return (baseFileType == JAVA_TYPE); }
0229         bool isSharpStyle() const { return (baseFileType == SHARP_TYPE); }
0230 
0231         // check if a specific character can be used in a legal variable/method/class name
0232         bool isLegalNameChar(char ch) const {
0233             if (isWhiteSpace(ch)) return false;
0234             if ((unsigned) ch > 127) return false;
0235             return (isalnum(ch)
0236                     || ch == '.' || ch == '_'
0237                     || (isJavaStyle() && ch == '$')
0238                     || (isSharpStyle() && ch == '@'));  // may be used as a prefix
0239         }
0240 
0241         // check if a specific character can be part of a header
0242         bool isCharPotentialHeader(const string &line, size_t i) const {
0243             assert(!isWhiteSpace(line[i]));
0244             char prevCh = ' ';
0245             if (i > 0) prevCh = line[i-1];
0246             if (!isLegalNameChar(prevCh) && isLegalNameChar(line[i]))
0247                 return true;
0248             return false;
0249         }
0250 
0251         // check if a specific character can be part of an operator
0252         bool isCharPotentialOperator(char ch) const {
0253             assert(!isWhiteSpace(ch));
0254             if ((unsigned) ch > 127) return false;
0255             return (ispunct(ch)
0256                     && ch != '{' && ch != '}'
0257                     && ch != '(' && ch != ')'
0258                     && ch != '[' && ch != ']'
0259                     && ch != ';' && ch != ','
0260                     && ch != '#' && ch != '\\'
0261                     && ch != '\'' && ch != '\"');
0262         }
0263 
0264         // check if a specific character is a whitespace character
0265         bool isWhiteSpace(char ch) const { return (ch == ' ' || ch == '\t'); }
0266 
0267         // peek at the next unread character.
0268         char peekNextChar(const string &line, int i) const {
0269             char ch = ' ';
0270             size_t peekNum = line.find_first_not_of(" \t", i + 1);
0271             if (peekNum == string::npos)
0272                 return ch;
0273             ch = line[peekNum];
0274             return ch;
0275         }
0276 };  // Class ASBase
0277 
0278 //----------------------------------------------------------------------------
0279 // Class ASBeautifier
0280 //----------------------------------------------------------------------------
0281 
0282 class ASBeautifier : protected ASResource, protected ASBase
0283 {
0284     public:
0285         ASBeautifier();
0286         virtual ~ASBeautifier();
0287         virtual void init(ASSourceIterator* iter);
0288         void init();
0289         virtual bool hasMoreLines() const;
0290         virtual string nextLine();
0291         virtual string beautify(const string &line);
0292         void deleteVector(vector<const string*>*& container);
0293         void initVector(vector<const string*>*& container);
0294         void setTabIndentation(int length = 4, bool forceTabs = false);
0295         void setSpaceIndentation(int length = 4);
0296         void setMaxInStatementIndentLength(int max);
0297         void setMinConditionalIndentLength(int min);
0298         void setIndentManuallySet(bool state);
0299         void setMinConditionalManuallySet(bool state);
0300         void setModeManuallySet(bool state);
0301         void setClassIndent(bool state);
0302         void setSwitchIndent(bool state);
0303         void setCaseIndent(bool state);
0304         void setBracketIndent(bool state);
0305         void setBlockIndent(bool state);
0306         void setNamespaceIndent(bool state);
0307         void setLabelIndent(bool state);
0308         void setCStyle();
0309         void setJavaStyle();
0310         void setSharpStyle();
0311         void setEmptyLineFill(bool state);
0312         void setPreprocessorIndent(bool state);
0313         int  getFileType();
0314         int  getIndentLength(void);
0315         string getIndentString(void);
0316         bool getBracketIndent(void);
0317         bool getBlockIndent(void);
0318         bool getCaseIndent(void);
0319         bool getClassIndent(void);
0320         bool getEmptyLineFill(void);
0321         bool getForceTabIndentation(void);
0322         bool getIndentManuallySet(void);
0323         bool getMinConditionalManuallySet(void);
0324         bool getModeManuallySet(void);
0325         bool getSwitchIndent(void);
0326 
0327     protected:
0328         void deleteStaticVectors();
0329         const string* findHeader(const string &line, int i,
0330                                  const vector<const string*>* possibleHeaders) const;
0331         const string* findOperator(const string &line, int i,
0332                                    const vector<const string*>* possibleOperators) const;
0333         int getNextProgramCharDistance(const string &line, int i) const;
0334         int  indexOf(vector<const string*> &container, const string *element);
0335         string trim(const string &str);
0336 
0337         // variables set by ASFormatter - must be updated in activeBeautifierStack
0338         int  inLineNumber;
0339         int  horstmannIndentInStatement;
0340         int  nonInStatementBracket;
0341         bool lineCommentNoBeautify;
0342         bool isNonInStatementArray;
0343         bool isSharpAccessor;
0344         bool isSharpDelegate;
0345         bool isInExtern;
0346         bool isInBeautifySQL;
0347         bool isInIndentableStruct;
0348 
0349     private:
0350         ASBeautifier(const ASBeautifier &copy);
0351         ASBeautifier& operator=(ASBeautifier&);        // not to be implemented
0352 
0353         void initStatic();
0354         void registerInStatementIndent(const string &line, int i, int spaceTabCount,
0355                                        int tabIncrementIn, int minIndent, bool updateParenStack);
0356         string preLineWS(int spaceTabCount, int tabCount);
0357 
0358         static int beautifierFileType;
0359         static vector<const string*>* headers;
0360         static vector<const string*>* nonParenHeaders;
0361         static vector<const string*>* preBlockStatements;
0362         static vector<const string*>* assignmentOperators;
0363         static vector<const string*>* nonAssignmentOperators;
0364         static vector<const string*>* indentableHeaders;
0365 
0366         ASSourceIterator *sourceIterator;
0367         vector<ASBeautifier*> *waitingBeautifierStack;
0368         vector<ASBeautifier*> *activeBeautifierStack;
0369         vector<int> *waitingBeautifierStackLengthStack;
0370         vector<int> *activeBeautifierStackLengthStack;
0371         vector<const string*> *headerStack;
0372         vector< vector<const string*>* > *tempStacks;
0373         vector<int> *blockParenDepthStack;
0374         vector<bool> *blockStatementStack;
0375         vector<bool> *parenStatementStack;
0376         vector<bool> *bracketBlockStateStack;
0377         vector<int> *inStatementIndentStack;
0378         vector<int> *inStatementIndentStackSizeStack;
0379         vector<int> *parenIndentStack;
0380         int  convertTabToSpaces(int i, int tabIncrementIn) const;
0381         int  getInStatementIndentAssign(const string& line, size_t currPos) const;
0382         int  getInStatementIndentComma(const string& line, size_t currPos) const;
0383         bool isClassAccessModifier(string& line) const;
0384         bool isLineEndComment(string& line, int startPos) const;
0385         bool statementEndsWithComma(string &line, int index);
0386         vector<vector<const string*>*>* copyTempStacks(const ASBeautifier &other) const;
0387         template<typename T> void deleteContainer(T &container);
0388         void deleteContainer(vector<vector<const string*>*>* &container);
0389         template<typename T> void initContainer(T &container, T value);
0390 
0391     private:  // variables
0392         string indentString;
0393         const string *currentHeader;
0394         const string *previousLastLineHeader;
0395         const string *probationHeader;
0396         bool isInQuote;
0397         bool isInVerbatimQuote;
0398         bool haveLineContinuationChar;
0399         bool isInAsm;
0400         bool isInAsmOneLine;
0401         bool isInAsmBlock;
0402         bool isInComment;
0403         bool isInHorstmannComment;
0404         bool isInCase;
0405         bool isInQuestion;
0406         bool isInStatement;
0407         bool isInHeader;
0408         bool isInTemplate;
0409         bool isInDefine;
0410         bool isInDefineDefinition;
0411         bool classIndent;
0412         bool isInClassInitializer;
0413         bool isInClassHeaderTab;
0414         bool isInEnum;
0415         bool switchIndent;
0416         bool caseIndent;
0417         bool namespaceIndent;
0418         bool bracketIndent;
0419         bool blockIndent;
0420         bool labelIndent;
0421         bool preprocessorIndent;
0422         bool isInConditional;
0423         bool isIndentManuallySet;
0424         bool isMinConditionalManuallySet;
0425         bool isModeManuallySet;
0426         bool shouldForceTabIndentation;
0427         bool emptyLineFill;
0428         bool backslashEndsPrevLine;
0429         bool lineOpensComment;
0430         bool blockCommentNoIndent;
0431         bool blockCommentNoBeautify;
0432         bool previousLineProbationTab;
0433         int  fileType;
0434         int  minConditionalIndent;
0435         int  parenDepth;
0436         int  indentLength;
0437         int  blockTabCount;
0438         int  maxInStatementIndent;
0439         int  classInitializerTabs;
0440         int  templateDepth;
0441         int  prevFinalLineSpaceTabCount;
0442         int  prevFinalLineTabCount;
0443         int  defineTabCount;
0444         char quoteChar;
0445         char prevNonSpaceCh;
0446         char currentNonSpaceCh;
0447         char currentNonLegalCh;
0448         char prevNonLegalCh;
0449 };  // Class ASBeautifier
0450 
0451 //----------------------------------------------------------------------------
0452 // Class ASEnhancer
0453 //----------------------------------------------------------------------------
0454 
0455 class ASEnhancer : protected ASBase
0456 {
0457     public:  // functions
0458         ASEnhancer();
0459         ~ASEnhancer();
0460         void init(int, int, string, bool, bool);
0461         void enhance(string &line, bool isInSQL);
0462 
0463     private:
0464         // options from command line or options file
0465         int  indentLength;
0466         bool useTabs;
0467         bool caseIndent;
0468         bool emptyLineFill;
0469 
0470         // parsing variables
0471         int  lineNumber;
0472         bool isInQuote;
0473         bool isInComment;
0474         char quoteChar;
0475 
0476         // unindent variables
0477         int  bracketCount;
0478         int  switchDepth;
0479         bool lookingForCaseBracket;
0480         bool unindentNextLine;
0481 
0482         // struct used by ParseFormattedLine function
0483         // contains variables used to unindent the case blocks
0484         struct switchVariables {
0485             int  switchBracketCount;
0486             int  unindentDepth;
0487             bool unindentCase;
0488         };
0489 
0490         switchVariables sw;                      // switch variables struct
0491         vector<switchVariables>  swVector;       // stack vector of switch variables
0492 
0493         // event table variables
0494         bool nextLineIsEventIndent;             // begin event table indent is reached
0495         bool isInEventTable;                    // need to indent an event table
0496 
0497         // SQL variables
0498         bool nextLineIsDeclareIndent;           // begin declare section indent is reached
0499         bool isInDeclareSection;                // need to indent a declare section
0500 
0501 
0502     private:  // functions
0503         size_t  findCaseColon(string  &line, size_t caseIndex) const;
0504         int     indentLine(string  &line, int indent) const;
0505         bool    isBeginDeclareSectionSQL(string  &line, size_t index) const;
0506         bool    isEndDeclareSectionSQL(string  &line, size_t index) const;
0507         size_t  processSwitchBlock(string  &line, size_t index);
0508         int     unindentLine(string  &line, int unindent) const;
0509 };  // Class ASEnhancer
0510 
0511 //----------------------------------------------------------------------------
0512 // Class ASFormatter
0513 //----------------------------------------------------------------------------
0514 
0515 class ASFormatter : public ASBeautifier
0516 {
0517     public: // functions
0518         ASFormatter();
0519         virtual ~ASFormatter();
0520         virtual void init(ASSourceIterator* iter);
0521         virtual bool hasMoreLines() const;
0522         virtual string nextLine();
0523         LineEndFormat getLineEndFormat() const;
0524         void setFormattingStyle(FormatStyle style);
0525         void setAddBracketsMode(bool state);
0526         void setAddOneLineBracketsMode(bool state);
0527         void setBracketFormatMode(BracketMode mode);
0528         void setBreakClosingHeaderBracketsMode(bool state);
0529         void setBreakBlocksMode(bool state);
0530         void setBreakClosingHeaderBlocksMode(bool state);
0531         void setBreakElseIfsMode(bool state);
0532         void setBreakOneLineBlocksMode(bool state);
0533         void setDeleteEmptyLinesMode(bool state);
0534         void setIndentCol1CommentsMode(bool state);
0535         void setLineEndFormat(LineEndFormat fmt);
0536         void setOperatorPaddingMode(bool mode);
0537         void setParensOutsidePaddingMode(bool mode);
0538         void setParensInsidePaddingMode(bool mode);
0539         void setParensHeaderPaddingMode(bool mode);
0540         void setParensUnPaddingMode(bool state);
0541         void setPointerAlignment(PointerAlign alignment);
0542         void setSingleStatementsMode(bool state);
0543         void setTabSpaceConversionMode(bool state);
0544 
0545     private:  // functions
0546         void ASformatter(ASFormatter &copy);           // not to be imlpemented
0547         ASFormatter& operator=(ASFormatter&);          // not to be implemented
0548         template<typename T> void deleteContainer(T &container);
0549         template<typename T> void initContainer(T &container, T value);
0550         char peekNextChar() const;
0551         BracketType getBracketType();
0552         bool addBracketsToStatement();
0553         bool commentAndHeaderFollows() const;
0554         bool getNextChar();
0555         bool getNextLine(bool emptyLineWasDeleted = false);
0556         bool isBeforeComment() const;
0557         bool isBeforeAnyComment() const;
0558         bool isBeforeAnyLineEndComment(int startPos) const;
0559         bool isBeforeMultipleLineEndComments(int startPos) const;
0560         bool isBracketType(BracketType a, BracketType b) const;
0561         bool isCurrentBracketBroken() const;
0562         bool isDereferenceOrAddressOf() const;
0563         bool isExecSQL(string  &line, size_t index) const;
0564         bool isEmptyLine(const string &line) const;
0565         bool isNextWordSharpNonParenHeader(int startChar) const;
0566         bool isNonInStatementArrayBracket() const;
0567         bool isPointerOrReference() const;
0568         bool isPointerOrReferenceCentered() const;
0569         bool isSharpStyleWithParen(const string* header) const;
0570         bool isStructAccessModified(string  &firstLine, size_t index) const;
0571         bool isUnaryOperator() const;
0572         bool isInExponent() const;
0573         bool isOneLineBlockReached(string& line, int startChar) const;
0574         bool isNextCharOpeningBracket(int startChar) const;
0575         bool isOkToBreakBlock(BracketType bracketType) const;
0576         int  getCurrentLineCommentAdjustment();
0577         int  getNextLineCommentAdjustment();
0578         void appendCharInsideComments();
0579         void appendSequence(const string &sequence, bool canBreakLine = true);
0580         void appendSpacePad();
0581         void appendSpaceAfter();
0582         void breakLine();
0583         void buildLanguageVectors();
0584         void checkForFollowingHeader(const string& firstLine);
0585         void convertTabToSpaces();
0586         void deleteContainer(vector<BracketType>* &container);
0587         void formatArrayRunIn();
0588         void formatRunIn();
0589         void goForward(int i);
0590         void initContainer(vector<BracketType>* &container, vector<BracketType>* value);
0591         void initNewLine();
0592         void padOperators(const string *newOperator);
0593         void padParens();
0594         void formatArrayBrackets(BracketType bracketType, bool isOpeningArrayBracket);
0595         void formatClosingBracket(BracketType bracketType);
0596         void formatCommentBody();
0597         void formatCommentOpener();
0598         void formatLineCommentBody();
0599         void formatLineCommentOpener();
0600         void formatOpeningBracket(BracketType bracketType);
0601         void formatQuoteBody();
0602         void formatQuoteOpener();
0603         void formatPointerOrReference();
0604         void formatPointerOrReferenceCast();
0605         void adjustComments();
0606         void isLineBreakBeforeClosingHeader();
0607         void setBreakBlocksVariables();
0608         void fixOptionVariableConflicts();
0609         void processPreprocessor();
0610         void trimContinuationLine();
0611         size_t findNextChar(string& line, char searchChar, int searchStart = 0);
0612         string getPreviousWord(const string& line, int currPos) const;
0613         string peekNextText(const string& firstLine, bool endOnEmptyLine=false) const;
0614 
0615     private:  // variables
0616         static int formatterFileType;
0617         static vector<const string*>* headers;
0618         static vector<const string*>* nonParenHeaders;
0619         static vector<const string*>* preDefinitionHeaders;
0620         static vector<const string*>* preCommandHeaders;
0621         static vector<const string*>* operators;
0622         static vector<const string*>* assignmentOperators;
0623         static vector<const string*>* castOperators;
0624 
0625         ASSourceIterator *sourceIterator;
0626         ASEnhancer *enhancer;
0627 
0628         vector<const string*> *preBracketHeaderStack;
0629         vector<BracketType> *bracketTypeStack;
0630         vector<int> *parenStack;
0631         vector<bool> *structStack;
0632         string readyFormattedLine;
0633         string currentLine;
0634         string formattedLine;
0635         const string *currentHeader;
0636         const string *previousOperator;    // used ONLY by pad-oper
0637         char currentChar;
0638         char previousChar;
0639         char previousNonWSChar;
0640         char previousCommandChar;
0641         char quoteChar;
0642         int  charNum;
0643         int  preprocBracketTypeStackSize;
0644         int  tabIncrementIn;
0645         int  spacePadNum;
0646         int  nextLineSpacePadNum;
0647         int  templateDepth;
0648         int  traceLineNumber;
0649         int  horstmannIndentChars;
0650         size_t leadingSpaces;
0651         size_t formattedLineCommentNum;     // comment location on formattedLine
0652         size_t currentLineFirstBracketNum;  // first bracket location on currentLine
0653         size_t previousReadyFormattedLineLength;
0654         FormatStyle formattingStyle;
0655         BracketMode bracketFormatMode;
0656         BracketType previousBracketType;
0657         PointerAlign pointerAlignment;
0658         LineEndFormat lineEnd;
0659         bool isVirgin;
0660         bool shouldPadOperators;
0661         bool shouldPadParensOutside;
0662         bool shouldPadParensInside;
0663         bool shouldPadHeader;
0664         bool shouldUnPadParens;
0665         bool shouldConvertTabs;
0666         bool shouldIndentCol1Comments;
0667         bool isInLineComment;
0668         bool isInComment;
0669         bool noTrimCommentContinuation;
0670         bool isInPreprocessor;
0671         bool isInTemplate;
0672         bool doesLineStartComment;
0673         bool lineEndsInCommentOnly;
0674         bool lineIsLineCommentOnly;
0675         bool lineIsEmpty;
0676         bool isImmediatelyPostCommentOnly;
0677         bool isImmediatelyPostEmptyLine;
0678         bool isInQuote;
0679         bool isInVerbatimQuote;
0680         bool haveLineContinuationChar;
0681         bool isInQuoteContinuation;
0682         bool isInBlParen;
0683         bool isSpecialChar;
0684         bool isNonParenHeader;
0685         bool foundQuestionMark;
0686         bool foundPreDefinitionHeader;
0687         bool foundNamespaceHeader;
0688         bool foundClassHeader;
0689         bool foundStructHeader;
0690         bool foundInterfaceHeader;
0691         bool foundPreCommandHeader;
0692         bool foundCastOperator;
0693         bool isInLineBreak;
0694         bool endOfCodeReached;
0695         bool lineCommentNoIndent;
0696         bool isInExecSQL;
0697         bool isInAsm;
0698         bool isInAsmOneLine;
0699         bool isInAsmBlock;
0700         bool isLineReady;
0701         bool isPreviousBracketBlockRelated;
0702         bool isInPotentialCalculation;
0703         bool isCharImmediatelyPostComment;
0704         bool isPreviousCharPostComment;
0705         bool isCharImmediatelyPostLineComment;
0706         bool isCharImmediatelyPostOpenBlock;
0707         bool isCharImmediatelyPostCloseBlock;
0708         bool isCharImmediatelyPostTemplate;
0709         bool isCharImmediatelyPostReturn;
0710         bool isCharImmediatelyPostOperator;
0711         bool breakCurrentOneLineBlock;
0712         bool isInHorstmannRunIn;
0713         bool currentLineBeginsWithBracket;
0714         bool shouldBreakOneLineBlocks;
0715         bool shouldReparseCurrentChar;
0716         bool shouldBreakOneLineStatements;
0717         bool shouldBreakClosingHeaderBrackets;
0718         bool shouldBreakElseIfs;
0719         bool shouldAddBrackets;
0720         bool shouldAddOneLineBrackets;
0721         bool shouldDeleteEmptyLines;
0722         bool needHeaderOpeningBracket;
0723         bool shouldBreakLineAtNextChar;
0724         bool passedSemicolon;
0725         bool passedColon;
0726         bool clearNonInStatement;
0727         bool isImmediatelyPostComment;
0728         bool isImmediatelyPostLineComment;
0729         bool isImmediatelyPostEmptyBlock;
0730         bool isImmediatelyPostPreprocessor;
0731         bool isImmediatelyPostReturn;
0732         bool isImmediatelyPostOperator;
0733 
0734         bool shouldBreakBlocks;
0735         bool shouldBreakClosingHeaderBlocks;
0736         bool isPrependPostBlockEmptyLineRequested;
0737         bool isAppendPostBlockEmptyLineRequested;
0738 
0739         bool prependEmptyLine;
0740         bool appendOpeningBracket;
0741         bool foundClosingHeader;
0742 
0743         bool isInHeader;
0744         bool isImmediatelyPostHeader;
0745         bool isInCase;
0746         bool isJavaStaticConstructor;
0747 
0748     private:  // inline functions
0749         // append a character to the current formatted line.
0750         void appendChar(char ch, bool canBreakLine) {
0751             if (canBreakLine && isInLineBreak)
0752                 breakLine();
0753             formattedLine.append(1, ch);
0754             isImmediatelyPostCommentOnly = false;
0755         }
0756 
0757         // append the CURRENT character (curentChar) to the current formatted line.
0758         void appendCurrentChar(bool canBreakLine = true) {
0759             appendChar(currentChar, canBreakLine);
0760         }
0761 
0762         // check if a specific sequence exists in the current placement of the current line
0763         bool isSequenceReached(const char *sequence) const {
0764             return currentLine.compare(charNum, strlen(sequence), sequence) == 0;
0765         }
0766 
0767         // call ASBase::findHeader for the current character
0768         const string *findHeader(const vector<const string*>* headers) {
0769             return ASBeautifier::findHeader(currentLine, charNum, headers);
0770         }
0771 
0772         // call ASBase::findOperator for the current character
0773         const string *findOperator(const vector<const string*>* headers) {
0774             return ASBeautifier::findOperator(currentLine, charNum, headers);
0775         }
0776 };  // Class ASFormatter
0777 
0778 
0779 //----------------------------------------------------------------------------
0780 // astyle namespace global declarations
0781 //----------------------------------------------------------------------------
0782 // sort comparison functions for ASResource
0783 bool sortOnLength(const string *a, const string *b);
0784 bool sortOnName(const string *a, const string *b);
0785 
0786 }   // end of astyle namespace
0787 
0788 // end of astyle namespace  --------------------------------------------------
0789 
0790 
0791 //----------------------------------------------------------------------------
0792 // declarations for library build
0793 // global because they are called externally and are NOT part of the namespace
0794 //----------------------------------------------------------------------------
0795 
0796 typedef void (STDCALL *fpError)(int, char*);       // pointer to callback error handler
0797 typedef char* (STDCALL *fpAlloc)(unsigned long);   // pointer to callback memory allocation
0798 extern "C" EXPORT char* STDCALL AStyleMain(const char*, const char*, fpError, fpAlloc);
0799 extern "C" EXPORT const char* STDCALL AStyleGetVersion (void);
0800 
0801 
0802 #endif // closes ASTYLE_H