diff --git a/estyle/Generator.cpp b/estyle/Generator.cpp index bf33559..0d6bf7a 100644 --- a/estyle/Generator.cpp +++ b/estyle/Generator.cpp @@ -20,6 +20,17 @@ void estyle::Generator::offsetPush(int32_t _count) { m_offsetStack.pushBack(_count); m_offset += _count; } +void estyle::Generator::offsetPushAuto() { + int32_t offset = 0; + for (int64_t iii=m_output.size()-1; iii>=0; --iii) { + if ( m_output[iii] == '\n' + || m_output[iii] == '\t') { + break; + } + offset++; + } + offsetPush(offset-m_offset); +} void estyle::Generator::offsetPop() { m_offset -= m_offsetStack.back(); @@ -309,6 +320,7 @@ int32_t estyle::Generator::process(int32_t _startId, } if (elem == estyle::lexer::EQUAL) { + addSpace(); m_output += "="; continue; } @@ -351,6 +363,13 @@ int32_t estyle::Generator::process(int32_t _startId, m_output += getEndOfLine(); continue; } + if (elem == estyle::lexer::ELEMENT_COMPLEX_TYPE) { + // complex type is special to generate... + etk::String type = generateType(iii); + addSpace(); + m_output += type; + continue; + } if (elem == estyle::lexer::RESERVED_IF) { addSpace(); m_output += "if "; @@ -372,13 +391,64 @@ int32_t estyle::Generator::process(int32_t _startId, } } offsetPop(); + // Check if next action is inside {} or not ... + if (nextIs(iii+1, estyle::lexer::BRACE_IN) == true) { + // no need to add one ... + int32_t countAction = countCurrent(iii+1, estyle::lexer::tocken::SEMICOLON); + // TODO: need to remove .. if needed... + continue; + } + m_output += getEndOfLine(); + addSpace(); + m_output += "{"; + indentationPush(); + m_output += getEndOfLine(); + + int32_t sectionEnd = endOfAction(iii+1); + iii = process(iii+1, sectionEnd+2); + + m_output += getEndOfLine(); + indentationPop(); + addSpace(); + m_output += "}"; + m_output += getEndOfLine(); continue; } - if (elem == estyle::lexer::ELEMENT_FUNCTION) { + if (elem == estyle::lexer::RESERVED_ELSE) { + if (nextIs(iii+1, estyle::lexer::BRACE_IN) == true) { + int32_t countAction = countCurrent(iii+1, estyle::lexer::tocken::SEMICOLON); + addSpace(); + m_output += "else"; + continue; + } + if (nextIs(iii+1, estyle::lexer::RESERVED_IF) == true) { + addSpace(); + m_output += "else"; + continue; + } + m_output += getEndOfLine(); + addSpace(); + m_output += "{"; + indentationPush(); + m_output += getEndOfLine(); + + int32_t sectionEnd = endOfAction(iii+1); + iii = process(iii+1, sectionEnd+1); + + m_output += getEndOfLine(); + indentationPop(); + addSpace(); + m_output += "}"; + m_output += getEndOfLine(); + continue; + } + if ( elem == estyle::lexer::ELEMENT_FUNCTION + || elem == estyle::lexer::ELEMENT_FUNCTION_DECLARATION) { addSpace(); m_output += m_lexer.getData(iii); m_output += " "; - offsetPush(m_lexer.getData(iii).size() + 2); + //offsetPush(m_lexer.getData(iii).size() + 2); + offsetPushAuto(); for (int64_t jjj=iii+1; jjj not comment they need to add it (wrong place but not may job ... bool estyle::Generator::previousIs(int64_t _pos, enum estyle::lexer::tocken _previousType) { @@ -414,6 +484,7 @@ bool estyle::Generator::previousIs(int64_t _pos, enum estyle::lexer::tocken _pre bool estyle::Generator::nextIs(int64_t _pos, enum estyle::lexer::tocken _nextType) { for (int32_t iii=_pos; iii listElement = m_lexer.getSubList(_pos); + for (size_t iii=0; iii m_offsetStack; void offsetPush(int32_t _count); + void offsetPushAuto(); void offsetPop(); @@ -51,6 +52,7 @@ namespace estyle { etk::String getDoxygenOneLine(); etk::String getDoxygenNLine(const etk::String& _data); int64_t endOfSection(int64_t _pos); + int64_t endOfAction(int64_t _pos); int32_t countCurrent(int64_t _pos, enum estyle::lexer::tocken _type1, enum estyle::lexer::tocken _type2 = estyle::lexer::END_OF_FILE, @@ -58,12 +60,14 @@ namespace estyle { enum estyle::lexer::tocken _type4 = estyle::lexer::END_OF_FILE); int32_t countCurrentLevelCondition(int64_t _pos); int32_t countCurrentParameters(int64_t _pos); + int32_t countCurrentAction(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); + etk::String generateType(int64_t _pos); }; } diff --git a/estyle/lexer/Lexer.cpp b/estyle/lexer/Lexer.cpp index 5d488b4..ffe4579 100644 --- a/estyle/lexer/Lexer.cpp +++ b/estyle/lexer/Lexer.cpp @@ -394,6 +394,9 @@ void estyle::Lexer::parse() { if (nextChar == '=') { m_list.pushBack(estyle::LexerElement(estyle::lexer::LESS_EQUAL, iii, iii+2)); iii++; + } else if (nextChar == '<') { + m_list.pushBack(estyle::LexerElement(estyle::lexer::LESS_LESS, iii, iii+2)); + iii++; } else { m_list.pushBack(estyle::LexerElement(estyle::lexer::LESS, iii, iii+1)); } @@ -403,6 +406,9 @@ void estyle::Lexer::parse() { if (nextChar == '=') { m_list.pushBack(estyle::LexerElement(estyle::lexer::GREATER_EQUAL, iii, iii+2)); iii++; + } else if (nextChar == '>') { + m_list.pushBack(estyle::LexerElement(estyle::lexer::GREATER_GREATER, iii, iii+2)); + iii++; } else { m_list.pushBack(estyle::LexerElement(estyle::lexer::GREATER, iii, iii+1)); } @@ -563,18 +569,32 @@ void estyle::Lexer::lexify(const etk::String& _input) { parse(); postAnnalyse_namespace(); postAnnalyse_function(); + postAnnalyse_function_typeExtern(); + postAnnalyse_function_typeInternal(); ESTYLE_DEBUG("find:"); for (auto &it: m_list) { - ESTYLE_DEBUG(" '" << it.getTocken() << "' ==> '" << m_stream.extract(it.getStart(),it.getStop()) << "'"); + if ( it.getTocken() == estyle::lexer::ELEMENT_COMPLEX + || it.getTocken() == estyle::lexer::ELEMENT_COMPLEX_TYPE) { + ESTYLE_DEBUG(" '" << it.getTocken() << "'"); + for (auto &it2: it.getList()) { + ESTYLE_DEBUG(" '" << it2.getTocken() << "' ==> '" << m_stream.extract(it2.getStart(),it2.getStop()) << "'"); + } + } else { + ESTYLE_DEBUG(" '" << it.getTocken() << "' ==> '" << m_stream.extract(it.getStart(),it.getStop()) << "'"); + } } } +etk::String estyle::Lexer::getDataSource(int32_t _start, int32_t _stop) const { + return m_stream.extract(_start, _stop); +} + etk::String estyle::Lexer::getData(int32_t _position) const { - return m_stream.extract(m_list[_position].getStart(), m_list[_position].getStop()); + return getDataSource(m_list[_position].getStart(), m_list[_position].getStop()); } etk::String estyle::Lexer::getValueString(int32_t _position) const { - return m_stream.extract(m_list[_position].getStart(), m_list[_position].getStop()); + return getDataSource(m_list[_position].getStart(), m_list[_position].getStop()); } ivec2 estyle::Lexer::getFilePosition(int32_t _position) const { @@ -600,7 +620,7 @@ etk::String estyle::Lexer::getFileLine(int32_t _position) const { if (positionStart < 0) { positionStart = 0; } - for (;positionStop< m_stream.size(); ++positionStop) { + for (;positionStop < int64_t(m_stream.size()); ++positionStop) { if (m_stream[positionStop] == '\n') { break; } @@ -665,4 +685,68 @@ void estyle::Lexer::postAnnalyse_function() { } ++it; } -} \ No newline at end of file +} + +void estyle::Lexer::postAnnalyse_function_typeExtern() { + for (int64_t iii=0; iii check if we have a type : '" << getData(iii) << "'"); + // search backward the first element availlable like const/ID/*/&/ + int64_t end = iii; + for (int64_t jjj=iii-1; jjj>=0; --jjj) { + auto elem = m_list[jjj].getTocken(); + if ( elem == estyle::lexer::BASIC_TYPE_INTEGER_SIZE_T + || elem == estyle::lexer::BASIC_TYPE_INTEGER_008 + || elem == estyle::lexer::BASIC_TYPE_INTEGER_008_UNSIGNED + || elem == estyle::lexer::BASIC_TYPE_INTEGER_016 + || elem == estyle::lexer::BASIC_TYPE_INTEGER_016_UNSIGNED + || elem == estyle::lexer::BASIC_TYPE_INTEGER_032 + || elem == estyle::lexer::BASIC_TYPE_INTEGER_032_UNSIGNED + || elem == estyle::lexer::BASIC_TYPE_INTEGER_064 + || elem == estyle::lexer::BASIC_TYPE_INTEGER_064_UNSIGNED + || elem == estyle::lexer::BASIC_TYPE_INTEGER_128 + || elem == estyle::lexer::BASIC_TYPE_INTEGER_128_UNSIGNED + || elem == estyle::lexer::BASIC_TYPE_FLOAT_32 + || elem == estyle::lexer::BASIC_TYPE_FLOAT_64 + || elem == estyle::lexer::BASIC_TYPE_FLOAT_96 + || elem == estyle::lexer::BASIC_TYPE_BOOLEAN + || elem == estyle::lexer::BASIC_TYPE_VOID + || elem == estyle::lexer::RESERVED_INT + || elem == estyle::lexer::RESERVED_LONG + || elem == estyle::lexer::RESERVED_SHORT + || elem == estyle::lexer::RESERVED_SIGNED + || elem == estyle::lexer::RESERVED_UNSIGNED + || elem == estyle::lexer::RESERVED_CONST + || elem == estyle::lexer::AND + || elem == estyle::lexer::MULTIPLY + || elem == estyle::lexer::GREATER + || elem == estyle::lexer::GREATER_GREATER + || elem == estyle::lexer::LESS + || elem == estyle::lexer::RESERVED_NEW_LINE) { + continue; + } + // find the end + end = jjj+1; + break; + } + if (end == iii) { + // Function call ... + } else { + estyle::LexerElement tmp = estyle::LexerElement(estyle::lexer::ELEMENT_COMPLEX_TYPE, m_list[end].getStart(), m_list[iii-1].getStop()); + for (int64_t jjj=end; jjj type: '" << getData(end) << "'"); + } + } + } +} + +void estyle::Lexer::postAnnalyse_function_typeInternal() { + +} + diff --git a/estyle/lexer/Lexer.hpp b/estyle/lexer/Lexer.hpp index bfe4af0..384927f 100644 --- a/estyle/lexer/Lexer.hpp +++ b/estyle/lexer/Lexer.hpp @@ -39,6 +39,16 @@ namespace estyle { void setStop(int32_t _pos) { m_stop = _pos; } + private: + //special case of complex element: + etk::Vector m_list; + public: + void pushElement(const estyle::LexerElement& _element) { + m_list.pushBack(_element); + } + const etk::Vector& getList() const { + return m_list; + } }; class Lexer { private: @@ -51,6 +61,8 @@ namespace estyle { // squash element name space like "::lklkmlk" and "lmkmlk::lmkmlk::mlklk" in 1 element void postAnnalyse_namespace(); void postAnnalyse_function(); + void postAnnalyse_function_typeExtern(); + void postAnnalyse_function_typeInternal(); public: int64_t size() const { return m_list.size(); @@ -58,7 +70,11 @@ namespace estyle { enum estyle::lexer::tocken getTocken(int32_t _position) const { return m_list[_position].getTocken(); } + const etk::Vector& getSubList(int32_t _position) const { + return m_list[_position].getList(); + } etk::String getData(int32_t _position) const; + etk::String getDataSource(int32_t _start, int32_t _stop) const; etk::String getValueString(int32_t _position) const; ivec2 getFilePosition(int32_t _position) const; etk::String getFileLine(int32_t _position) const; diff --git a/estyle/lexer/tocken.cpp b/estyle/lexer/tocken.cpp index 44a6bbb..6a7a309 100644 --- a/estyle/lexer/tocken.cpp +++ b/estyle/lexer/tocken.cpp @@ -25,8 +25,10 @@ etk::String estyle::lexer::toString(estyle::lexer::tocken _token) { case estyle::lexer::NOT: return "!"; case estyle::lexer::NOT_EQUAL: return "!="; case estyle::lexer::LESS: return "<"; + case estyle::lexer::LESS_LESS: return "<<"; case estyle::lexer::LESS_EQUAL: return "<="; case estyle::lexer::GREATER: return ">"; + case estyle::lexer::GREATER_GREATER: return ">>"; case estyle::lexer::GREATER_EQUAL: return ">="; case estyle::lexer::PLUS: return "+"; case estyle::lexer::MINUS: return "-"; @@ -124,6 +126,9 @@ etk::String estyle::lexer::toString(estyle::lexer::tocken _token) { case estyle::lexer::BASIC_TYPE_VOID: return "void"; case estyle::lexer::ELEMENT_FUNCTION: return "FUNCTION"; + case estyle::lexer::ELEMENT_FUNCTION_DECLARATION: return "FUNCTION_DECLARATION"; + case estyle::lexer::ELEMENT_COMPLEX: return "COMPLEX"; + case estyle::lexer::ELEMENT_COMPLEX_TYPE: return "COMPLEX_TYPE"; } return etk::String("?[") + etk::toString(int32_t(_token)) + "]"; } diff --git a/estyle/lexer/tocken.hpp b/estyle/lexer/tocken.hpp index 319ab43..26f2ad9 100644 --- a/estyle/lexer/tocken.hpp +++ b/estyle/lexer/tocken.hpp @@ -25,8 +25,10 @@ namespace estyle { NOT, //!< element "!" NOT_EQUAL, //!< element "!=" LESS, //!< element "<" + LESS_LESS, //!< element "<<" LESS_EQUAL, //!< element "<=" GREATER, //!< element ">" + GREATER_GREATER, //!< element ">>" GREATER_EQUAL, //!< element ">=" PLUS, //!< element "+" MINUS, //!< element "-" @@ -131,8 +133,12 @@ namespace estyle { // post annalyse element: ELEMENT_FUNCTION, //!< all composed with an ID previous a ( + ELEMENT_FUNCTION_DECLARATION, //!< all composed with an ID previous a ( ==> and have a type on the left + ELEMENT_COMPLEX, //!< complex type with multiple tocken inside + ELEMENT_COMPLEX_TYPE, //!< complex type with multiple tocken inside + BASIC_TYPE_LIST_END, }; etk::String toString(estyle::lexer::tocken _token); diff --git a/test/main.cpp b/test/main.cpp index 0c773cf..f15da59 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); if (ploppp == 54) { return -T;}"; + "namespace aaa { namespace BBB { const unsigned \n int myFunction(int32_t & arg1, float \t*\t\t* arg2, const\t \n double*\n& 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);