diff --git a/estyle/Generator.cpp b/estyle/Generator.cpp index 5e5d5eb..bf33559 100644 --- a/estyle/Generator.cpp +++ b/estyle/Generator.cpp @@ -8,6 +8,23 @@ +void estyle::Generator::indentationPush() { + m_indentation++; +} + +void estyle::Generator::indentationPop() { + m_indentation--; +} + +void estyle::Generator::offsetPush(int32_t _count) { + m_offsetStack.pushBack(_count); + m_offset += _count; +} + +void estyle::Generator::offsetPop() { + m_offset -= m_offsetStack.back(); + m_offsetStack.popBack(); +} estyle::Generator::Generator(): @@ -104,7 +121,9 @@ void estyle::Generator::addSpace() { } } } - ESTYLE_TODO("TODO : Do indentation..."); + for (int32_t ooo=0; ooo m_urrentStack; etk::String estyle::Generator::process(const etk::String& _code) { + m_output = ""; m_lexer.lexify(_code); process(0, m_lexer.size(), estyle::lexer::END_OF_FILE); return m_output; } -int32_t estyle::Generator::process(int32_t _startId, int32_t _stopId, enum estyle::lexer::tocken _endTocken) { - for (size_t iii = _startId; iii < _stopId; ++iii) { +int32_t estyle::Generator::process(int32_t _startId, + int32_t _stopId, + enum estyle::lexer::tocken _endTocken1, + enum estyle::lexer::tocken _endTocken2, + enum estyle::lexer::tocken _endTocken3, + enum estyle::lexer::tocken _endTocken4) { + for (int64_t iii = _startId; iii < _stopId; ++iii) { enum estyle::lexer::tocken elem = m_lexer.getTocken(iii); - if (elem == _endTocken) { + if ( elem == _endTocken1 + || elem == _endTocken2 + || elem == _endTocken3 + || elem == _endTocken4) { return iii; } if (elem == estyle::lexer::RESERVED_COMMENT_1_LINE) { @@ -292,11 +330,11 @@ int32_t estyle::Generator::process(int32_t _startId, int32_t _stopId, enum estyl addSpace(); m_output += "{"; m_output += getEndOfLine(); - m_indentation++; + indentationPush(); continue; } if (elem == estyle::lexer::BRACE_OUT) { - m_indentation--; + indentationPop(); if (m_urrentStack.size() == 0) { ESTYLE_ERROR("Get '}' without a '{' element"); continue; @@ -305,7 +343,9 @@ int32_t estyle::Generator::process(int32_t _startId, int32_t _stopId, enum estyl } else { m_urrentStack.popBack(); } - m_output += getEndOfLine(); + if (onNewLine() == false) { + m_output += getEndOfLine(); + } addSpace(); m_output += "}"; m_output += getEndOfLine(); @@ -313,23 +353,45 @@ int32_t estyle::Generator::process(int32_t _startId, int32_t _stopId, enum estyl } if (elem == estyle::lexer::RESERVED_IF) { addSpace(); - int32_t spaceOffset = 4; m_output += "if "; if (previousIs(iii, estyle::lexer::RESERVED_ELSE) == true) { - spaceOffset += 5; + offsetPush(4 + 5); + } else { + offsetPush(4); } - for (size_t jjj=iii+1; jjj normal case ... } else if (elem == estyle::lexer::PARENTHESE_IN) { // find condition section ... - iii = generateCondition(jjj, spaceOffset); + iii = generateCondition(jjj); break; } else { ESTYLE_ERROR("Get 'if' without '(' element"); } } + offsetPop(); + continue; + } + if (elem == estyle::lexer::ELEMENT_FUNCTION) { + addSpace(); + m_output += m_lexer.getData(iii); + m_output += " "; + offsetPush(m_lexer.getData(iii).size() + 2); + for (int64_t jjj=iii+1; jjj normal case ... + } else if (elem == estyle::lexer::PARENTHESE_IN) { + // find condition section ... + iii = generateFunction(jjj); + break; + } else { + ESTYLE_ERROR("Get '" << m_lexer.getData(iii) << "' (function) without '(' element"); + } + } + offsetPop(); continue; } // default other case: @@ -339,7 +401,7 @@ int32_t estyle::Generator::process(int32_t _startId, int32_t _stopId, enum estyl return m_lexer.size(); } // go ackward and find if previous is the corect type (remove) \n and empty space ==> not comment they need to add it (wrong place but not may job ... -bool estyle::Generator::previousIs(size_t _pos, enum estyle::lexer::tocken _previousType) { +bool estyle::Generator::previousIs(int64_t _pos, enum estyle::lexer::tocken _previousType) { for (int32_t iii=_pos-1; iii>0; --iii) { enum estyle::lexer::tocken elem = m_lexer.getTocken(iii); if (elem == estyle::lexer::RESERVED_NEW_LINE) { @@ -349,41 +411,68 @@ bool estyle::Generator::previousIs(size_t _pos, enum estyle::lexer::tocken _prev } return false; } +bool estyle::Generator::nextIs(int64_t _pos, enum estyle::lexer::tocken _nextType) { + for (int32_t iii=_pos; iii propertyDoxygenMultipleLine; private: estyle::Lexer m_lexer; - int32_t m_indentation = 0; //!< number of indentation needed in the current line + int32_t m_indentation = 0; //!< number of indentation needed in the current line. + void indentationPush(); + void indentationPop(); int32_t m_offset = 0; //!< number od space needed after the indentation (used for the if / else if / while / functions ....) + etk::Vector m_offsetStack; + void offsetPush(int32_t _count); + void offsetPop(); + etk::String m_output; public: etk::String process(const etk::String& _code); - int32_t process(int32_t _startId, int32_t _stopId, enum estyle::lexer::tocken _endTocken); + int32_t process(int32_t _startId, + int32_t _stopId, + enum estyle::lexer::tocken _endTocken1 = estyle::lexer::END_OF_FILE, + enum estyle::lexer::tocken _endTocken2 = estyle::lexer::END_OF_FILE, + enum estyle::lexer::tocken _endTocken3 = estyle::lexer::END_OF_FILE, + enum estyle::lexer::tocken _endTocken4 = estyle::lexer::END_OF_FILE); private: void addSpace(); + /// check if the previous cheracter is a newline or not... + bool onNewLine(); etk::String getEndOfLine(); etk::String getDoxygenOneLine(); etk::String getDoxygenNLine(const etk::String& _data); - size_t endOfSection(size_t _pos); - int32_t countCurrentLevelCondition(size_t _pos); - size_t generateCondition(size_t _pos, int32_t _offset); - bool previousIs(size_t _pos, enum estyle::lexer::tocken _previousType); - int32_t countCurrentLevelComa(size_t _pos); + int64_t endOfSection(int64_t _pos); + int32_t countCurrent(int64_t _pos, + enum estyle::lexer::tocken _type1, + enum estyle::lexer::tocken _type2 = estyle::lexer::END_OF_FILE, + enum estyle::lexer::tocken _type3 = estyle::lexer::END_OF_FILE, + enum estyle::lexer::tocken _type4 = estyle::lexer::END_OF_FILE); + int32_t countCurrentLevelCondition(int64_t _pos); + int32_t countCurrentParameters(int64_t _pos); + int64_t generateCondition(int64_t _pos); + int64_t generateFunction(int64_t _pos); + bool previousIs(int64_t _pos, enum estyle::lexer::tocken _previousType); + bool nextIs(int64_t _pos, enum estyle::lexer::tocken _nextType); + int32_t countCurrentLevelComa(int64_t _pos); + int32_t getWhileCondition(int64_t _pos); }; } diff --git a/estyle/lexer/Lexer.cpp b/estyle/lexer/Lexer.cpp index 669660f..5d488b4 100644 --- a/estyle/lexer/Lexer.cpp +++ b/estyle/lexer/Lexer.cpp @@ -650,6 +650,7 @@ void estyle::Lexer::postAnnalyse_namespace() { ++it; } } + void estyle::Lexer::postAnnalyse_function() { auto it = m_list.begin(); while (it != m_list.end()) { diff --git a/estyle/lexer/Lexer.hpp b/estyle/lexer/Lexer.hpp index da7cf47..bfe4af0 100644 --- a/estyle/lexer/Lexer.hpp +++ b/estyle/lexer/Lexer.hpp @@ -52,7 +52,7 @@ namespace estyle { void postAnnalyse_namespace(); void postAnnalyse_function(); public: - size_t size() const { + int64_t size() const { return m_list.size(); } enum estyle::lexer::tocken getTocken(int32_t _position) const { diff --git a/test/main.cpp b/test/main.cpp index 8aaf1b7..0c773cf 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -61,7 +61,7 @@ TEST(testrestyle, test3) { etk::String source = - "namespace aaa { namespace BBB { const unsigned int myFunction(int32_t& arg1, float** arg2, const double*& arg3=0x345aeF);const unsigned int myFunction(int32_t& arg1, float** arg2, const double*& arg3){ return 0;} } } if(aaa::BBB::myFunction(2452452345!=55 && 765432=5432,24523452354,\"43SFGDVEZT5R34EAQCWX\") == false && 235445 & 24545 == 'R') exit (-1);"; + "namespace aaa { namespace BBB { const unsigned int myFunction(int32_t& arg1, float** arg2, const double*& arg3=0x345aeF);const unsigned int myFunction(int32_t& arg1, float** arg2, const double*& arg3){ return 0;} } } if(aaa::BBB::myFunction(2452452345!=55 && 765432=5432,24523452354,\"43SFGDVEZT5R34EAQCWX\") == false && 235445 & 24545 == 'R') exit (-1); if (ploppp == 54) { return -T;}"; etk::String output = interface.process(source); TEST_INFO("source:\n" << source);