diff --git a/estyle/Generator.cpp b/estyle/Generator.cpp index 0d6bf7a..3c9ccf2 100644 --- a/estyle/Generator.cpp +++ b/estyle/Generator.cpp @@ -38,48 +38,143 @@ void estyle::Generator::offsetPop() { } +void estyle::Generator::typePush(etk::String _data) { + m_typeStack.pushBack(_data); + m_type = m_typeStack.back(); +} +void estyle::Generator::typePop() { + m_typeStack.popBack(); + if (m_typeStack.size() != 0) { + m_type = m_typeStack.back(); + } else { + m_type = "block"; + } +} + + +/* +/ ** + * + * / +*/ +#define DOXYGEN_MULTI_LINE__NORMAL (0) +/* +/ *! + * + * / +*/ +#define DOXYGEN_MULTI_LINE__NORMAL_EXCLAMATION (1) +/* +/ ** + + * / +*/ +#define DOXYGEN_MULTI_LINE__NORMAL_NO_STAR (2) +/* +/ *! + + * / +*/ +#define DOXYGEN_MULTI_LINE__NORMAL_NO_STAR_EXCLAMATION (3) +/* +/// +/// +/// +*/ +#define DOXYGEN_MULTI_LINE__NORMAL_SINGLE_LINE (4) +/* +//! +//! +//! +*/ +#define DOXYGEN_MULTI_LINE__NORMAL_SINGLE_LINE_EXCLAMATION (5) + +static void setPropertyDoxygenMultiLine(eproperty::List& _data) { + _data.add(DOXYGEN_MULTI_LINE__NORMAL, "normal"); + _data.add(DOXYGEN_MULTI_LINE__NORMAL_EXCLAMATION, "normal-exclamation"); + _data.add(DOXYGEN_MULTI_LINE__NORMAL_NO_STAR, "no-star"); + _data.add(DOXYGEN_MULTI_LINE__NORMAL_NO_STAR_EXCLAMATION, "no-star-exclamation"); + _data.add(DOXYGEN_MULTI_LINE__NORMAL_SINGLE_LINE, "single-line"); + _data.add(DOXYGEN_MULTI_LINE__NORMAL_SINGLE_LINE_EXCLAMATION, "single-line-exclamation"); +} + +estyle::BraceProperty::BraceProperty() : + inNewLineBefore(false), + inNewLineAfter(false), + inSpaceBefore(false), + inSpaceAfter(false), + outNewLineBefore(false), + outNewLineAfter(false), + outSpaceBefore(false), + outSpaceAfter(false), + single(false) { + +} +estyle::BraceProperty::BraceProperty(class estyle::Generator* _generator, const etk::String& _typeName): + inNewLineBefore(_generator, "brace-" + _typeName + "-in-new-line-before", false, "Set a new line before the input brace"), + inNewLineAfter(_generator, "brace-" + _typeName + "-in-new-line-after", false, "Set a new line after the input brace"), + inSpaceBefore(_generator, "brace-" + _typeName + "-in-space-before", false, "Set a space before the input brace (if not already present)"), + inSpaceAfter(_generator, "brace-" + _typeName + "-in-space-after", false, "Set a space after the input brace (if not already present)"), + outNewLineBefore(_generator, "brace-" + _typeName + "-out-new-line-before", false, "Set a new line before the input brace"), + outNewLineAfter(_generator, "brace-" + _typeName + "-out-new-line-after", false, "Set a new line after the input brace"), + outSpaceBefore(_generator, "brace-" + _typeName + "-out-space-before", false, "Set a space before the input brace (if not already present)"), + outSpaceAfter(_generator, "brace-" + _typeName + "-out-space-after", false, "Set a space after the input brace (if not already present)"), + single(_generator, "brace-" + _typeName + "-single", false, "remove or add brace if only 1 action is present.") { + +} +estyle::BraceProperty::BraceProperty(estyle::BraceProperty&& _obj) : + inNewLineBefore(etk::move(_obj.inNewLineBefore)), + inNewLineAfter(etk::move(_obj.inNewLineAfter)), + inSpaceBefore(etk::move(_obj.inSpaceBefore)), + inSpaceAfter(etk::move(_obj.inSpaceAfter)), + outNewLineBefore(etk::move(_obj.outNewLineBefore)), + outNewLineAfter(etk::move(_obj.outNewLineAfter)), + outSpaceBefore(etk::move(_obj.outSpaceBefore)), + outSpaceAfter(etk::move(_obj.outSpaceAfter)), + single(etk::move(_obj.single)) { + +} + +estyle::BraceProperty& estyle::BraceProperty::operator=(BraceProperty&& _obj) { + inNewLineBefore = etk::move(_obj.inNewLineBefore); + inNewLineAfter = etk::move(_obj.inNewLineAfter); + inSpaceBefore = etk::move(_obj.inSpaceBefore); + inSpaceAfter = etk::move(_obj.inSpaceAfter); + outNewLineBefore = etk::move(_obj.outNewLineBefore); + outNewLineAfter = etk::move(_obj.outNewLineAfter); + outSpaceBefore = etk::move(_obj.outSpaceBefore); + outSpaceAfter = etk::move(_obj.outSpaceAfter); + single = etk::move(_obj.single); + return *this; +} + +void estyle::Generator::clear() { + m_offset = 0; + m_offsetStack.clear(); + m_type = "block"; + m_typeStack.clear(); + //m_lexer; + m_indentation = 0; + m_output = ""; +} + estyle::Generator::Generator(): propertyEndOfLine(this, "end-of-line", true, "true: End with \\n, false \\r\\n"), propertyIndentWithTabulation(this, "indent-with-tabs", true, "true: indent with tabs: '\\t', false indent win space:' '"), propertyIndentSize(this, "indent-size", 4, "default 4 sapce in one tabulation indentation"), propertyDoxygenOneLine(this, "doxygen-1-line-type", true, "true: single line doxygen comment is done with '//!', false '///'"), - propertyDoxygenMultipleLine(this, "doxygen-N-line-type", 0, "0: /** */ ...") { - /* - / ** - * - * / - */ - propertyDoxygenMultipleLine.add(0, "normal"); - /* - / *! - * - * / - */ - propertyDoxygenMultipleLine.add(1, "normal-exclamation"); - /* - / ** - - * / - */ - propertyDoxygenMultipleLine.add(2, "no-star"); - /* - / *! - - * / - */ - propertyDoxygenMultipleLine.add(3, "no-star-exclamation"); - /* - /// - /// - /// - */ - propertyDoxygenMultipleLine.add(4, "single-line"); - /* - //! - //! - //! - */ - propertyDoxygenMultipleLine.add(5, "single-line-exclamation"); + propertyDoxygenMultipleLine(this, "doxygen-N-line-type", DOXYGEN_MULTI_LINE__NORMAL, "0: /** */ ...") { + setPropertyDoxygenMultiLine(propertyDoxygenMultipleLine); + propertyBrace.set("if", etk::move(estyle::BraceProperty(this, "if"))); + propertyBrace.set("else", etk::move(estyle::BraceProperty(this, "else"))); + propertyBrace.set("for", etk::move(estyle::BraceProperty(this, "for"))); + propertyBrace.set("while", etk::move(estyle::BraceProperty(this, "while"))); + propertyBrace.set("do", etk::move(estyle::BraceProperty(this, "do"))); + propertyBrace.set("switch", etk::move(estyle::BraceProperty(this, "switch"))); + propertyBrace.set("namespace", etk::move(estyle::BraceProperty(this, "namespace"))); + propertyBrace.set("class", etk::move(estyle::BraceProperty(this, "class"))); + propertyBrace.set("struct", etk::move(estyle::BraceProperty(this, "struct"))); + propertyBrace.set("block", etk::move(estyle::BraceProperty(this, "block"))); } estyle::Generator::~Generator() { @@ -112,8 +207,29 @@ etk::String estyle::Generator::getDoxygenNLine(const etk::String& _data) { } +void estyle::Generator::addNewLine() { + if (m_output.size() == 0) { + return; + } + if (m_output.back() == '\0') { + ESTYLE_TODO("TODO : Do indentation... : '" << m_output << "'"); + } + if (m_output.back() == '\n') { + return; + } + m_output += getEndOfLine(); +} -void estyle::Generator::addSpace() { +void estyle::Generator::addNewLineIfSemiColon() { + if (m_output.size() == 0) { + return; + } + if (m_output.back() == ';') { + m_output += getEndOfLine(); + } +} + +void estyle::Generator::addIndent() { if (m_output.size() == 0) { return; } @@ -137,6 +253,19 @@ void estyle::Generator::addSpace() { } return; } +} + +void estyle::Generator::addSpace() { + if (m_output.size() == 0) { + return; + } + if (m_output.back() == '\0') { + ESTYLE_TODO("TODO : Do indentation... : '" << m_output << "'"); + } + if (m_output.back() == '\n') { + addIndent(); + return; + } if ( m_output.back() == ' ' || m_output.back() == '\t') { return; @@ -170,7 +299,7 @@ enum class stack { etk::Vector m_urrentStack; etk::String estyle::Generator::process(const etk::String& _code) { - m_output = ""; + clear(); m_lexer.lexify(_code); process(0, m_lexer.size(), estyle::lexer::END_OF_FILE); return m_output; @@ -191,18 +320,21 @@ int32_t estyle::Generator::process(int32_t _startId, return iii; } if (elem == estyle::lexer::RESERVED_COMMENT_1_LINE) { + addNewLineIfSemiColon(); m_output += "//"; m_output += m_lexer.getData(iii); m_output += getEndOfLine(); continue; } if (elem == estyle::lexer::RESERVED_DOCUMENTATION_1_LINE) { + addNewLineIfSemiColon(); m_output += getDoxygenOneLine(); m_output += m_lexer.getData(iii); m_output += getEndOfLine(); continue; } if (elem == estyle::lexer::RESERVED_COMMENT_N_LINE) { + addNewLineIfSemiColon(); addSpace(); m_output += "/*"; m_output += m_lexer.getData(iii); @@ -216,6 +348,7 @@ int32_t estyle::Generator::process(int32_t _startId, continue; } if (elem == estyle::lexer::RESERVED_DOCUMENTATION_1_LINE) { + addNewLineIfSemiColon(); addSpace(); m_output += getDoxygenNLine(m_lexer.getData(iii)); m_output += getEndOfLine(); @@ -229,116 +362,138 @@ int32_t estyle::Generator::process(int32_t _startId, continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_SIZE_T) { + addNewLineIfSemiColon(); addSpace(); m_output += "size_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_008) { + addNewLineIfSemiColon(); addSpace(); m_output += "int8_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_008_UNSIGNED) { + addNewLineIfSemiColon(); addSpace(); m_output += "uint8_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_016) { + addNewLineIfSemiColon(); addSpace(); m_output += "int16_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_016_UNSIGNED) { + addNewLineIfSemiColon(); addSpace(); m_output += "uint16_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_032) { + addNewLineIfSemiColon(); addSpace(); m_output += "int32_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_032_UNSIGNED) { + addNewLineIfSemiColon(); addSpace(); m_output += "uint32_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_064) { + addNewLineIfSemiColon(); addSpace(); m_output += "int64_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_064_UNSIGNED) { + addNewLineIfSemiColon(); addSpace(); m_output += "uint64_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_128) { + addNewLineIfSemiColon(); addSpace(); m_output += "int128_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_INTEGER_128_UNSIGNED) { + addNewLineIfSemiColon(); addSpace(); m_output += "uint128_t"; continue; } if (elem == estyle::lexer::BASIC_TYPE_FLOAT_32) { + addNewLineIfSemiColon(); addSpace(); m_output += "float"; continue; } if (elem == estyle::lexer::BASIC_TYPE_FLOAT_64) { + addNewLineIfSemiColon(); addSpace(); m_output += "double"; continue; } if (elem == estyle::lexer::BASIC_TYPE_FLOAT_96) { + addNewLineIfSemiColon(); addSpace(); m_output += "triple"; continue; } if (elem == estyle::lexer::BASIC_TYPE_BOOLEAN) { + addNewLineIfSemiColon(); addSpace(); m_output += "bool"; continue; } if (elem == estyle::lexer::BASIC_TYPE_VOID) { + addNewLineIfSemiColon(); addSpace(); m_output += "void"; continue; } if (elem == estyle::lexer::SEMICOLON) { + addNewLineIfSemiColon(); m_output += ";"; - m_output += getEndOfLine(); continue; } if (elem == estyle::lexer::ID) { - addSpace(); + addNewLineIfSemiColon(); + //addSpace(); + addIndent(); m_output += m_lexer.getData(iii); continue; } if (elem == estyle::lexer::EQUAL) { + addNewLineIfSemiColon(); addSpace(); m_output += "="; continue; } if (elem == estyle::lexer::STRING) { + addNewLineIfSemiColon(); m_output += "\""; m_output += m_lexer.getData(iii); m_output += "\""; continue; } if (elem == estyle::lexer::SIMPLE_QUOTE_STRING) { + addNewLineIfSemiColon(); m_output += "'"; m_output += m_lexer.getData(iii); m_output += "'"; continue; } if (elem == estyle::lexer::BRACE_IN) { + addNewLineIfSemiColon(); m_urrentStack.pushBack(stack::BLOCK); - m_output += getEndOfLine(); + addNewLine(); addSpace(); m_output += "{"; m_output += getEndOfLine(); @@ -346,6 +501,7 @@ int32_t estyle::Generator::process(int32_t _startId, continue; } if (elem == estyle::lexer::BRACE_OUT) { + addNewLineIfSemiColon(); indentationPop(); if (m_urrentStack.size() == 0) { ESTYLE_ERROR("Get '}' without a '{' element"); @@ -355,15 +511,14 @@ int32_t estyle::Generator::process(int32_t _startId, } else { m_urrentStack.popBack(); } - if (onNewLine() == false) { - m_output += getEndOfLine(); - } + addNewLine(); addSpace(); m_output += "}"; m_output += getEndOfLine(); continue; } if (elem == estyle::lexer::ELEMENT_COMPLEX_TYPE) { + addNewLineIfSemiColon(); // complex type is special to generate... etk::String type = generateType(iii); addSpace(); @@ -371,8 +526,10 @@ int32_t estyle::Generator::process(int32_t _startId, continue; } if (elem == estyle::lexer::RESERVED_IF) { + addNewLineIfSemiColon(); addSpace(); m_output += "if "; + typePush("if"); if (previousIs(iii, estyle::lexer::RESERVED_ELSE) == true) { offsetPush(4 + 5); } else { @@ -391,32 +548,102 @@ int32_t estyle::Generator::process(int32_t _startId, } } offsetPop(); + bool needBrace = false; + bool haveBrace = false; + int32_t subGenerationStart = iii+1; + int32_t subGenerationStop = iii+1; // Check if next action is inside {} or not ... if (nextIs(iii+1, estyle::lexer::BRACE_IN) == true) { + haveBrace = true; // no need to add one ... int32_t countAction = countCurrent(iii+1, estyle::lexer::tocken::SEMICOLON); - // TODO: need to remove .. if needed... - continue; + if (countAction != 1) { + needBrace = true; + } + subGenerationStart = iii+2; + subGenerationStop = endOfSection(iii+1); + } else { + // only 1 instruction: + int32_t sectionEnd = endOfAction(iii+1); + subGenerationStop = sectionEnd+1; + } + /* + ESTYLE_ERROR(" single " << propertyBrace[m_type].single.get()); + ESTYLE_ERROR(" inNewLineBefore " << propertyBrace[m_type].inNewLineBefore.get()); + ESTYLE_ERROR(" inSpaceBefore " << propertyBrace[m_type].inSpaceBefore.get()); + ESTYLE_ERROR(" inSpaceAfter " << propertyBrace[m_type].inSpaceAfter.get()); + ESTYLE_ERROR(" inNewLineAfter " << propertyBrace[m_type].inNewLineAfter.get()); + ESTYLE_ERROR(" outNewLineBefore " << propertyBrace[m_type].outNewLineBefore.get()); + ESTYLE_ERROR(" outSpaceBefore " << propertyBrace[m_type].outSpaceBefore.get()); + ESTYLE_ERROR(" outSpaceAfter " << propertyBrace[m_type].outSpaceAfter.get()); + ESTYLE_ERROR(" outNewLineAfter " << propertyBrace[m_type].outNewLineAfter.get()); + */ + if (needBrace == false) { + if (propertyBrace[m_type].single.get() == false) { + needBrace = true; + } + } + if (needBrace == true) { + if (propertyBrace[m_type].inNewLineBefore.get() == true) { + addNewLine(); + // add indentation + addSpace(); + } + if (propertyBrace[m_type].inSpaceBefore.get() == true) { + // add 1 space if needed + addSpace(); + } + m_output += "{"; + if (propertyBrace[m_type].inSpaceAfter.get() == true) { + // add 1 space if needed + m_output += " "; + } + } + if (propertyBrace[m_type].inNewLineAfter.get() == true) { + addNewLine(); } - 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(); + iii = process(subGenerationStart, subGenerationStop); indentationPop(); - addSpace(); - m_output += "}"; - m_output += getEndOfLine(); + if (needBrace == true) { + if (propertyBrace[m_type].outNewLineBefore.get() == true) { + addNewLine(); + // add indentation + addSpace(); + } + } + if (needBrace == true) { + if (propertyBrace[m_type].outSpaceBefore.get() == true) { + // add 1 space if needed + addSpace(); + } + m_output += "}"; + if (propertyBrace[m_type].outSpaceAfter.get() == true) { + // add 1 space if needed + addSpace(); + } + } + if (haveBrace == true) { + iii--; + } else { + iii-=2; + } + // special case for a else after a condition ... + if (nextIs(iii+1, estyle::lexer::RESERVED_ELSE) == false) { + if (propertyBrace[m_type].outNewLineAfter.get() == true) { + addNewLine(); + } + } + typePop(); continue; } if (elem == estyle::lexer::RESERVED_ELSE) { + addNewLineIfSemiColon(); + typePush("else"); if (nextIs(iii+1, estyle::lexer::BRACE_IN) == true) { int32_t countAction = countCurrent(iii+1, estyle::lexer::tocken::SEMICOLON); + // TODO ... addSpace(); m_output += "else"; continue; @@ -426,25 +653,26 @@ int32_t estyle::Generator::process(int32_t _startId, m_output += "else"; continue; } - m_output += getEndOfLine(); + addNewLine(); addSpace(); m_output += "{"; indentationPush(); - m_output += getEndOfLine(); + addNewLine(); int32_t sectionEnd = endOfAction(iii+1); iii = process(iii+1, sectionEnd+1); - m_output += getEndOfLine(); + addNewLine(); indentationPop(); addSpace(); m_output += "}"; - m_output += getEndOfLine(); + addNewLine(); continue; } if ( elem == estyle::lexer::ELEMENT_FUNCTION || elem == estyle::lexer::ELEMENT_FUNCTION_DECLARATION) { - addSpace(); + addNewLineIfSemiColon(); + //addSpace(); m_output += m_lexer.getData(iii); m_output += " "; //offsetPush(m_lexer.getData(iii).size() + 2); @@ -465,7 +693,7 @@ int32_t estyle::Generator::process(int32_t _startId, continue; } // default other case: - addSpace(); + //addSpace(); m_output += m_lexer.getData(iii); } return _stopId+1; @@ -507,7 +735,7 @@ int64_t estyle::Generator::generateCondition(int64_t _pos) { iii = generateCondition(iii); } else if ( elem == estyle::lexer::AND_AND || elem == estyle::lexer::OR_OR) { - m_output += getEndOfLine(); + addNewLine(); offsetPop(); addSpace(); offsetPush(5); @@ -536,7 +764,7 @@ int64_t estyle::Generator::generateFunction(int64_t _pos) { iii = process(iii, sectionEnd, estyle::lexer::COMA, estyle::lexer::PARENTHESE_OUT); if (nextIs(iii-1, estyle::lexer::PARENTHESE_OUT) == false) { m_output += ","; - m_output += getEndOfLine(); + addNewLine(); addSpace(); } } @@ -548,7 +776,7 @@ int64_t estyle::Generator::generateFunction(int64_t _pos) { } int64_t estyle::Generator::endOfSection(int64_t _pos) { - ESTYLE_INFO(" " << m_lexer.getTocken(_pos) << " " << _pos << " [BEGIN]"); + ESTYLE_VERBOSE(" " << m_lexer.getTocken(_pos) << " " << _pos << " [BEGIN]"); enum estyle::lexer::tocken endTocken = estyle::lexer::END_OF_FILE; enum estyle::lexer::tocken elem = m_lexer.getTocken(_pos); if (elem == estyle::lexer::PARENTHESE_IN) { @@ -575,7 +803,7 @@ int64_t estyle::Generator::endOfSection(int64_t _pos) { return m_lexer.size(); } int64_t estyle::Generator::endOfAction(int64_t _pos) { - ESTYLE_INFO(" " << m_lexer.getTocken(_pos) << " " << _pos << " [BEGIN]"); + ESTYLE_VERBOSE(" " << m_lexer.getTocken(_pos) << " " << _pos << " [BEGIN]"); for (int64_t iii=_pos+1; iii < m_lexer.size(); ++iii) { enum estyle::lexer::tocken elem = m_lexer.getTocken(iii); if (elem == estyle::lexer::SEMICOLON) { diff --git a/estyle/Generator.hpp b/estyle/Generator.hpp index d5c4373..a18b8d8 100644 --- a/estyle/Generator.hpp +++ b/estyle/Generator.hpp @@ -13,6 +13,26 @@ #include namespace estyle { + class Generator; + class BraceProperty { + public: + BraceProperty(); + BraceProperty(estyle::Generator* _generator, const etk::String& _typeName); + eproperty::Value inNewLineBefore; + eproperty::Value inNewLineAfter; + eproperty::Value inSpaceBefore; + eproperty::Value inSpaceAfter; + eproperty::Value outNewLineBefore; + eproperty::Value outNewLineAfter; + eproperty::Value outSpaceBefore; + eproperty::Value outSpaceAfter; + eproperty::Value single; + BraceProperty(const BraceProperty& _obj) = delete; + BraceProperty(BraceProperty&& _obj); + ~BraceProperty() = default; + BraceProperty& operator=(BraceProperty&& _obj); + BraceProperty& operator=(const BraceProperty& _obj) = delete; + }; class Generator : public eproperty::Interface { public: Generator(); @@ -23,19 +43,28 @@ namespace estyle { eproperty::Value propertyIndentSize; eproperty::Value propertyDoxygenOneLine; eproperty::List propertyDoxygenMultipleLine; + // Brace section + // brace for "if" + etk::Map propertyBrace; + private: + void clear(); estyle::Lexer m_lexer; - int32_t m_indentation = 0; //!< number of indentation needed in the current line. + int32_t m_indentation = 12345; //!< 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 ....) + int32_t m_offset = 12345; //!< 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 offsetPushAuto(); void offsetPop(); + etk::String m_type = "ZZZZZZZZZZZZZZZZZZZZ"; // current stack type ... + etk::Vector m_typeStack; + void typePush(etk::String _count); + void typePop(); - etk::String m_output; + etk::String m_output = "ZZZZZZZZZZZZZZZZZZZZ"; public: etk::String process(const etk::String& _code); int32_t process(int32_t _startId, @@ -45,7 +74,19 @@ namespace estyle { enum estyle::lexer::tocken _endTocken3 = estyle::lexer::END_OF_FILE, enum estyle::lexer::tocken _endTocken4 = estyle::lexer::END_OF_FILE); private: + /** + * @brief Add space " " if no space or '\t' is set before and add the indentation if needed (last char is a "\n") + */ void addSpace(); + /** + * @brief Add Indentation "\t\t\t " if start of line + */ + void addIndent(); + /** + * @brief Add newline if no newLine is set before + */ + void addNewLine(); + void addNewLineIfSemiColon(); /// check if the previous cheracter is a newline or not... bool onNewLine(); etk::String getEndOfLine(); diff --git a/estyle/lexer/Lexer.cpp b/estyle/lexer/Lexer.cpp index ffe4579..eb2d461 100644 --- a/estyle/lexer/Lexer.cpp +++ b/estyle/lexer/Lexer.cpp @@ -565,6 +565,7 @@ estyle::Lexer::Lexer() { void estyle::Lexer::lexify(const etk::String& _input) { m_stream = _input; + m_list.clear(); ESTYLE_DEBUG("Parse stream"); parse(); postAnnalyse_namespace(); diff --git a/lutin_estyle-test.py b/lutin_estyle-test.py index 00662c2..8ee34d6 100644 --- a/lutin_estyle-test.py +++ b/lutin_estyle-test.py @@ -21,6 +21,7 @@ def configure(target, my_module): # add sources files my_module.add_src_file([ 'test/main.cpp', + 'test/testIf.cpp', ]) my_module.add_path("test") my_module.copy_path('data/*','test') diff --git a/test/main.cpp b/test/main.cpp index f15da59..91ca8e0 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -68,5 +68,3 @@ TEST(testrestyle, test3) { TEST_INFO("output:\n" << output); } - - diff --git a/test/testIf.cpp b/test/testIf.cpp index 250a73f..ffb89bc 100644 --- a/test/testIf.cpp +++ b/test/testIf.cpp @@ -3,105 +3,307 @@ * @copyright 2017, Edouard DUPIN, all right reserved * @license MPL-2 (see license file) */ +#include +#include +#include +#include #include #include -#include "testInterface.hpp" + +etk::String sourceIf1Action = "action_A;if(true){action_B;}action_D;"; +etk::String sourceIf1ActionNoBrace = "action_A;if(true)action_B;action_D;"; +etk::String sourceIf2Action = "action_A;if(true){action_B;action_C;}action_D;"; -TEST(testIf, direct_1) { - testInterface system; - bool ret = system.execute("variable result = false; if (true) { result = true;};"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isBoolean("result"), true); - EXPECT_EQ(system.getBoolean("result"), true); +TEST(testBraceIf, brace_if_0000) { + estyle::Generator interface; + //interface.properties.set("brace-if-in-new-line-before", "true"); + //interface.properties.set("brace-if-in-new-line-after", "true"); + //interface.properties.set("brace-if-in-space-before", "true"); + //interface.properties.set("brace-if-in-space-after", "true"); + //interface.properties.set("brace-if-out-new-line-before", "true"); + //interface.properties.set("brace-if-out-new-line-after", "true"); + //interface.properties.set("brace-if-out-space-before", "true"); + //interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true ){action_B;}action_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); } -TEST(testIf, direct_2) { - testInterface system; - bool ret = system.execute("variable result = false; if (false) { result = true;};"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isBoolean("result"), true); - EXPECT_EQ(system.getBoolean("result"), false); +TEST(testBraceIf, brace_if_1000) { + estyle::Generator interface; + interface.properties.set("brace-if-in-new-line-before", "true"); + //interface.properties.set("brace-if-in-new-line-after", "true"); + //interface.properties.set("brace-if-in-space-before", "true"); + //interface.properties.set("brace-if-in-space-after", "true"); + interface.properties.set("brace-if-out-new-line-before", "true"); + //interface.properties.set("brace-if-out-new-line-after", "true"); + //interface.properties.set("brace-if-out-space-before", "true"); + //interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true )\n{action_B;\n}action_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); } -TEST(testIf, direct_3) { - testInterface system; - bool ret = system.execute("variable result = false; if (true) result = true;"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isBoolean("result"), true); - EXPECT_EQ(system.getBoolean("result"), true); +TEST(testBraceIf, brace_if_1010) { + estyle::Generator interface; + interface.properties.set("brace-if-in-new-line-before", "true"); + //interface.properties.set("brace-if-in-new-line-after", "true"); + interface.properties.set("brace-if-in-space-before", "true"); + //interface.properties.set("brace-if-in-space-after", "true"); + interface.properties.set("brace-if-out-new-line-before", "true"); + //interface.properties.set("brace-if-out-new-line-after", "true"); + interface.properties.set("brace-if-out-space-before", "true"); + //interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true )\n{action_B;\n}action_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); } -TEST(testIf, direct_4) { - testInterface system; - bool ret = system.execute("variable result = false; if (false) result = true;"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isBoolean("result"), true); - EXPECT_EQ(system.getBoolean("result"), false); +TEST(testBraceIf, brace_if_1011) { + estyle::Generator interface; + interface.properties.set("brace-if-in-new-line-before", "true"); + //interface.properties.set("brace-if-in-new-line-after", "true"); + interface.properties.set("brace-if-in-space-before", "true"); + interface.properties.set("brace-if-in-space-after", "true"); + interface.properties.set("brace-if-out-new-line-before", "true"); + //interface.properties.set("brace-if-out-new-line-after", "true"); + interface.properties.set("brace-if-out-space-before", "true"); + interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true )\n{ action_B;\n} action_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); } -TEST(testIf, else_case_1) { - testInterface system; - bool ret = system.execute("variable result = 0; if (false) { result = 1;} else { result = 2;}"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isInteger32("result"), true); - EXPECT_EQ(system.getInteger32("result"), 2); -} -TEST(testIf, else_case_2) { - testInterface system; - bool ret = system.execute("variable result = 0; if (false) result = 1; else result = 2;"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isInteger32("result"), true); - EXPECT_EQ(system.getInteger32("result"), 2); +TEST(testBraceIf, brace_if_1100) { + estyle::Generator interface; + interface.properties.set("brace-if-in-new-line-before", "true"); + interface.properties.set("brace-if-in-new-line-after", "true"); + //interface.properties.set("brace-if-in-space-before", "true"); + //interface.properties.set("brace-if-in-space-after", "true"); + interface.properties.set("brace-if-out-new-line-before", "true"); + interface.properties.set("brace-if-out-new-line-after", "true"); + //interface.properties.set("brace-if-out-space-before", "true"); + //interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true )\n{\n\taction_B;\n}\naction_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); } -TEST(testIf, else_else_case) { - testInterface system; - bool ret = system.execute("variable result = 0; if (false) { result = 1;} else if (false) { result = 2;} else { result = 3;};"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isInteger32("result"), true); - EXPECT_EQ(system.getInteger32("result"), 3); +TEST(testBraceIf, brace_if_1101) { + estyle::Generator interface; + interface.properties.set("brace-if-in-new-line-before", "true"); + interface.properties.set("brace-if-in-new-line-after", "true"); + //interface.properties.set("brace-if-in-space-before", "true"); + interface.properties.set("brace-if-in-space-after", "true"); + interface.properties.set("brace-if-out-new-line-before", "true"); + interface.properties.set("brace-if-out-new-line-after", "true"); + //interface.properties.set("brace-if-out-space-before", "true"); + interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true )\n{ \n\taction_B;\n} \naction_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); } -TEST(testIf, variable_1) { - testInterface system; - bool ret = system.execute("variable a = true; variable result = false; if (a) { result = true;};"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isBoolean("result"), true); - EXPECT_EQ(system.getBoolean("result"), true); +TEST(testBraceIf, brace_if_1110) { + estyle::Generator interface; + interface.properties.set("brace-if-in-new-line-before", "true"); + interface.properties.set("brace-if-in-new-line-after", "true"); + interface.properties.set("brace-if-in-space-before", "true"); + //interface.properties.set("brace-if-in-space-after", "true"); + interface.properties.set("brace-if-out-new-line-before", "true"); + interface.properties.set("brace-if-out-new-line-after", "true"); + interface.properties.set("brace-if-out-space-before", "true"); + //interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true )\n{\n\taction_B;\n}\naction_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); } -TEST(testIf, variable_2) { - testInterface system; - bool ret = system.execute("variable a = false; variable result = false; if (a) { result = true;};"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isBoolean("result"), true); - EXPECT_EQ(system.getBoolean("result"), false); +TEST(testBraceIf, brace_if_1111) { + estyle::Generator interface; + interface.properties.set("brace-if-in-new-line-before", "true"); + interface.properties.set("brace-if-in-new-line-after", "true"); + interface.properties.set("brace-if-in-space-before", "true"); + interface.properties.set("brace-if-in-space-after", "true"); + interface.properties.set("brace-if-out-new-line-before", "true"); + interface.properties.set("brace-if-out-new-line-after", "true"); + interface.properties.set("brace-if-out-space-before", "true"); + interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true )\n{ \n\taction_B;\n} \naction_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); } -TEST(testIf, compare_1) { - testInterface system; - bool ret = system.execute("variable a = 111; variable b = 333; variable result = false; if (a != b) { result = true;};"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isBoolean("result"), true); - EXPECT_EQ(system.getBoolean("result"), true); +TEST(testBraceIf, brace_if_1001) { + estyle::Generator interface; + interface.properties.set("brace-if-in-new-line-before", "true"); + //interface.properties.set("brace-if-in-new-line-after", "true"); + //interface.properties.set("brace-if-in-space-before", "true"); + interface.properties.set("brace-if-in-space-after", "true"); + interface.properties.set("brace-if-out-new-line-before", "true"); + //interface.properties.set("brace-if-out-new-line-after", "true"); + //interface.properties.set("brace-if-out-space-before", "true"); + interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true )\n{ action_B;\n} action_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); } -TEST(testIf, compare_2) { - testInterface system; - bool ret = system.execute("variable a = 111; variable b = 333; variable result = false; if (a == b) { result = true;};"); - EXPECT_EQ(ret, true); - EXPECT_EQ(system.exist("result"), true); - EXPECT_EQ(system.isBoolean("result"), true); - EXPECT_EQ(system.getBoolean("result"), false); -} \ No newline at end of file + +TEST(testBraceIf, brace_if_0100) { + estyle::Generator interface; + //interface.properties.set("brace-if-in-new-line-before", "true"); + interface.properties.set("brace-if-in-new-line-after", "true"); + //interface.properties.set("brace-if-in-space-before", "true"); + //interface.properties.set("brace-if-in-space-after", "true"); + //interface.properties.set("brace-if-out-new-line-before", "true"); + interface.properties.set("brace-if-out-new-line-after", "true"); + //interface.properties.set("brace-if-out-space-before", "true"); + //interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true ){\n\taction_B;}\naction_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); +} + +TEST(testBraceIf, brace_if_0101) { + estyle::Generator interface; + //interface.properties.set("brace-if-in-new-line-before", "true"); + interface.properties.set("brace-if-in-new-line-after", "true"); + //interface.properties.set("brace-if-in-space-before", "true"); + interface.properties.set("brace-if-in-space-after", "true"); + //interface.properties.set("brace-if-out-new-line-before", "true"); + interface.properties.set("brace-if-out-new-line-after", "true"); + //interface.properties.set("brace-if-out-space-before", "true"); + interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true ){ \n\taction_B;} \naction_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); +} + +TEST(testBraceIf, brace_if_0110) { + estyle::Generator interface; + //interface.properties.set("brace-if-in-new-line-before", "true"); + interface.properties.set("brace-if-in-new-line-after", "true"); + interface.properties.set("brace-if-in-space-before", "true"); + //interface.properties.set("brace-if-in-space-after", "true"); + //interface.properties.set("brace-if-out-new-line-before", "true"); + interface.properties.set("brace-if-out-new-line-after", "true"); + interface.properties.set("brace-if-out-space-before", "true"); + //interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true ) {\n\taction_B; }\naction_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); +} + +TEST(testBraceIf, brace_if_0010) { + estyle::Generator interface; + //interface.properties.set("brace-if-in-new-line-before", "true"); + //interface.properties.set("brace-if-in-new-line-after", "true"); + interface.properties.set("brace-if-in-space-before", "true"); + //interface.properties.set("brace-if-in-space-after", "true"); + //interface.properties.set("brace-if-out-new-line-before", "true"); + //interface.properties.set("brace-if-out-new-line-after", "true"); + interface.properties.set("brace-if-out-space-before", "true"); + //interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true ) {action_B; }action_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); +} + +TEST(testBraceIf, brace_if_0011) { + estyle::Generator interface; + //interface.properties.set("brace-if-in-new-line-before", "true"); + //interface.properties.set("brace-if-in-new-line-after", "true"); + interface.properties.set("brace-if-in-space-before", "true"); + interface.properties.set("brace-if-in-space-after", "true"); + //interface.properties.set("brace-if-out-new-line-before", "true"); + //interface.properties.set("brace-if-out-new-line-after", "true"); + interface.properties.set("brace-if-out-space-before", "true"); + interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true ) { action_B; } action_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); +} + +TEST(testBraceIf, brace_if_0111) { + estyle::Generator interface; + //interface.properties.set("brace-if-in-new-line-before", "true"); + interface.properties.set("brace-if-in-new-line-after", "true"); + interface.properties.set("brace-if-in-space-before", "true"); + interface.properties.set("brace-if-in-space-after", "true"); + //interface.properties.set("brace-if-out-new-line-before", "true"); + interface.properties.set("brace-if-out-new-line-after", "true"); + interface.properties.set("brace-if-out-space-before", "true"); + interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true ) { \n\taction_B; } \naction_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); +} + +TEST(testBraceIf, brace_if_0001) { + estyle::Generator interface; + //interface.properties.set("brace-if-in-new-line-before", "true"); + //interface.properties.set("brace-if-in-new-line-after", "true"); + //interface.properties.set("brace-if-in-space-before", "true"); + interface.properties.set("brace-if-in-space-after", "true"); + //interface.properties.set("brace-if-out-new-line-before", "true"); + //interface.properties.set("brace-if-out-new-line-after", "true"); + //interface.properties.set("brace-if-out-space-before", "true"); + interface.properties.set("brace-if-out-space-after", "true"); + //interface.properties.set("brace-if-single", "true"); + etk::String outputRef = "action_A;\nif (true ){ action_B;} action_D;"; + etk::String output = interface.process(sourceIf1Action); + EXPECT_EQ(output, outputRef); + output = interface.process(sourceIf1ActionNoBrace); + EXPECT_EQ(output, outputRef); +} + + + +