[DEV] start to have a good render on ifÃ

This commit is contained in:
Edouard DUPIN 2017-12-09 23:51:48 +01:00
parent 31cb2e90cc
commit ac92c5e10d
6 changed files with 328 additions and 12 deletions

View File

@ -12,6 +12,8 @@
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: /** */ ...") {
/*
@ -63,13 +65,13 @@ etk::String estyle::Generator::getEndOfLine() {
return "\r\n";
}
etk::String estyle::Generator::getDoxygenOneLine(int32_t _indentation) {
etk::String estyle::Generator::getDoxygenOneLine() {
if (propertyDoxygenOneLine.get() == true) {
return "//!";
}
return "///";
}
etk::String estyle::Generator::getDoxygenNLine(int32_t _indentation, const etk::String& _data) {
etk::String estyle::Generator::getDoxygenNLine(const etk::String& _data) {
if (propertyDoxygenMultipleLine.get() == 0) {
@ -82,10 +84,54 @@ etk::String estyle::Generator::getDoxygenNLine(int32_t _indentation, const etk::
}
void estyle::Generator::addSpace(etk::String& _data) {
if (_data.size() == 0) {
return;
}
if (_data.back() == '\0') {
ESTYLE_TODO("TODO : Do indentation... : '" << _data << "'");
}
if (_data.back() == '\n') {
if (propertyIndentWithTabulation.get() == true) {
for (int32_t iii=0; iii<m_indentation; ++iii) {
_data += "\t";
}
} else {
for (int32_t iii=0; iii<m_indentation; ++iii) {
for (int32_t jjj=0; jjj<propertyIndentSize.get(); ++jjj) {
_data += " ";
}
}
}
ESTYLE_TODO("TODO : Do indentation...");
return;
}
if ( _data.back() == ' '
|| _data.back() == '\t') {
return;
}
_data += " ";
}
enum class stack {
NAMESPACE,
CLASS,
STRUCT,
BLOCK,
DO,
WHILE,
FOR,
IF,
ELSE,
PTHESE_CONDITION,
PTHESE,
};
etk::Vector<enum stack> m_urrentStack;
etk::String estyle::Generator::process(const etk::String& _code) {
estyle::Lexer lexer(_code);
etk::String out;
int32_t indentation = 0;
for (size_t iii = 0; iii < lexer.size(); ++iii) {
enum estyle::lexer::tocken elem = lexer.getTocken(iii);
if (elem == estyle::lexer::RESERVED_COMMENT_1_LINE) {
@ -95,12 +141,13 @@ etk::String estyle::Generator::process(const etk::String& _code) {
continue;
}
if (elem == estyle::lexer::RESERVED_DOCUMENTATION_1_LINE) {
out += getDoxygenOneLine(indentation);
out += getDoxygenOneLine();
out += lexer.getData(iii);
out += getEndOfLine();
continue;
}
if (elem == estyle::lexer::RESERVED_COMMENT_N_LINE) {
addSpace(out);
out += "/*";
out += lexer.getData(iii);
out += "*/";
@ -113,7 +160,8 @@ etk::String estyle::Generator::process(const etk::String& _code) {
continue;
}
if (elem == estyle::lexer::RESERVED_DOCUMENTATION_1_LINE) {
out += getDoxygenNLine(indentation, lexer.getData(iii));
addSpace(out);
out += getDoxygenNLine(lexer.getData(iii));
out += getEndOfLine();
// TODO : Some mode can create error like /** */ becaming /// ...
if (iii+1 < lexer.size()) {
@ -124,8 +172,255 @@ etk::String estyle::Generator::process(const etk::String& _code) {
}
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_SIZE_T) {
addSpace(out);
out += "size_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_008) {
addSpace(out);
out += "int8_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_008_UNSIGNED) {
addSpace(out);
out += "uint8_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_016) {
addSpace(out);
out += "int16_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_016_UNSIGNED) {
addSpace(out);
out += "uint16_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_032) {
addSpace(out);
out += "int32_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_032_UNSIGNED) {
addSpace(out);
out += "uint32_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_064) {
addSpace(out);
out += "int64_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_064_UNSIGNED) {
addSpace(out);
out += "uint64_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_128) {
addSpace(out);
out += "int128_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_INTEGER_128_UNSIGNED) {
addSpace(out);
out += "uint128_t";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_FLOAT_32) {
addSpace(out);
out += "float";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_FLOAT_64) {
addSpace(out);
out += "double";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_FLOAT_96) {
addSpace(out);
out += "triple";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_BOOLEAN) {
addSpace(out);
out += "bool";
continue;
}
if (elem == estyle::lexer::BASIC_TYPE_VOID) {
addSpace(out);
out += "void";
continue;
}
if (elem == estyle::lexer::SEMICOLON) {
out += ";";
out += getEndOfLine();
continue;
}
if (elem == estyle::lexer::ID) {
addSpace(out);
out += lexer.getData(iii);
continue;
}
if (elem == estyle::lexer::EQUAL) {
out += "=";
continue;
}
if (elem == estyle::lexer::STRING) {
out += "\"";
out += lexer.getData(iii);
out += "\"";
continue;
}
if (elem == estyle::lexer::SIMPLE_QUOTE_STRING) {
out += "'";
out += lexer.getData(iii);
out += "'";
continue;
}
if (elem == estyle::lexer::BRACE_IN) {
m_urrentStack.pushBack(stack::BLOCK);
out += getEndOfLine();
//addSpace(out);
out += "{";
out += getEndOfLine();
m_indentation++;
continue;
}
if (elem == estyle::lexer::BRACE_OUT) {
m_indentation--;
if (m_urrentStack.size() == 0) {
ESTYLE_ERROR("Get '}' without a '{' element");
continue;
} else if (m_urrentStack.back() != stack::BLOCK) {
ESTYLE_ERROR("Get '}' without other than a '{' element in stack of type ...");
} else {
m_urrentStack.popBack();
}
out += getEndOfLine();
addSpace(out);
out += "}";
out += getEndOfLine();
continue;
}
if (elem == estyle::lexer::RESERVED_IF) {
addSpace(out);
int32_t spaceOffset = 4;
out += "if ";
for (size_t jjj=iii+1; jjj<lexer.size(); ++jjj) {
enum estyle::lexer::tocken elem = lexer.getTocken(jjj);
if (elem == estyle::lexer::RESERVED_NEW_LINE) {
// OK ==> normal case ...
} else if (elem == estyle::lexer::PARENTHESE_IN) {
// find condition section ...
iii = generateCondition(lexer, jjj, out, spaceOffset);
break;
} else {
ESTYLE_ERROR("Get 'if' without '(' element");
}
}
continue;
}
// default other case:
addSpace(out);
out += lexer.getData(iii);
}
return out;
};
size_t estyle::Generator::generateCondition(estyle::Lexer& _lexer, size_t _pos, etk::String& _data, int32_t _offset) {
int32_t sectionEnd = endOfSection(_lexer, _pos);
int32_t nbCondition = countCurrentLevelCondition(_lexer, _pos);
if (nbCondition == 0) {
for (size_t iii=_pos; iii<=sectionEnd; ++iii) {
addSpace(_data);
_data += _lexer.getData(iii);
}
} else {
_data += "( ";
for (size_t iii=_pos+1; iii<=sectionEnd; ++iii) {
enum estyle::lexer::tocken elem = _lexer.getTocken(iii);
if (elem == estyle::lexer::PARENTHESE_IN) {
iii = generateCondition(_lexer, iii, _data, _offset + 5);
} else if ( elem == estyle::lexer::AND_AND
|| elem == estyle::lexer::OR_OR) {
_data += getEndOfLine();
addSpace(_data);
for (int32_t ooo=0; ooo<_offset; ++ooo) {
_data += " ";
}
_data += " " + _lexer.getData(iii) + " ";
} else {
addSpace(_data);
_data += _lexer.getData(iii);
}
}
}
return sectionEnd;
}
size_t estyle::Generator::endOfSection(estyle::Lexer& _lexer, size_t _pos) {
ESTYLE_INFO(" " << _lexer.getTocken(_pos) << " " << _pos << " [BEGIN]");
enum estyle::lexer::tocken endTocken = estyle::lexer::END_OF_FILE;
enum estyle::lexer::tocken elem = _lexer.getTocken(_pos);
if (elem == estyle::lexer::PARENTHESE_IN) {
endTocken = estyle::lexer::PARENTHESE_OUT;
} else if (elem == estyle::lexer::BRACKET_IN) {
endTocken = estyle::lexer::BRACKET_OUT;
} else if (elem == estyle::lexer::BRACE_IN) {
endTocken = estyle::lexer::BRACE_OUT;
} else {
ESTYLE_ERROR("can not get end position of " << _lexer.getTocken(_pos));
return _pos;
}
for (size_t iii=_pos+1; iii < _lexer.size(); ++iii) {
elem = _lexer.getTocken(iii);
if (elem == endTocken) {
return iii;
}
if ( elem == estyle::lexer::PARENTHESE_IN
|| elem == estyle::lexer::BRACKET_IN
|| elem == estyle::lexer::BRACE_IN) {
iii = endOfSection(_lexer, iii);
}
}
return _lexer.size();
}
int32_t estyle::Generator::countCurrentLevelCondition(estyle::Lexer& _lexer, size_t _pos) {
int32_t out;
enum estyle::lexer::tocken endTocken = estyle::lexer::END_OF_FILE;
enum estyle::lexer::tocken elem = _lexer.getTocken(_pos);
if (elem == estyle::lexer::PARENTHESE_IN) {
endTocken = estyle::lexer::PARENTHESE_OUT;
} else if (elem == estyle::lexer::BRACKET_IN) {
endTocken = estyle::lexer::BRACKET_OUT;
} else if (elem == estyle::lexer::BRACE_IN) {
endTocken = estyle::lexer::BRACE_OUT;
} else {
ESTYLE_ERROR("can not get end position of " << _lexer.getTocken(_pos));
return _pos;
}
for (size_t iii=_pos+1; iii < _lexer.size(); ++iii) {
elem = _lexer.getTocken(iii);
if (elem == endTocken) {
return out;
}
if ( elem == estyle::lexer::PARENTHESE_IN
|| elem == estyle::lexer::BRACKET_IN
|| elem == estyle::lexer::BRACE_IN) {
iii = endOfSection(_lexer, iii);
}
if ( elem == estyle::lexer::AND_AND
|| elem == estyle::lexer::OR_OR) {
out++;
}
}
return out;
}

View File

@ -19,14 +19,21 @@ namespace estyle {
~Generator();
protected:
eproperty::Value<bool> propertyEndOfLine;
eproperty::Value<bool> propertyIndentWithTabulation;
eproperty::Value<int8_t> propertyIndentSize;
eproperty::Value<bool> propertyDoxygenOneLine;
eproperty::List<int32_t> propertyDoxygenMultipleLine;
public:
etk::String process(const etk::String& _code);
private:
void addSpace(etk::String& _data);
etk::String getEndOfLine();
etk::String getDoxygenOneLine(int32_t _indentation);
etk::String getDoxygenNLine(int32_t _indentation, const etk::String& _data);
etk::String getDoxygenOneLine();
etk::String getDoxygenNLine(const etk::String& _data);
int32_t m_indentation = 0;
size_t endOfSection(estyle::Lexer& _lexer, size_t _pos);
int32_t countCurrentLevelCondition(estyle::Lexer& _lexer, size_t _pos);
size_t generateCondition(estyle::Lexer& _lexer, size_t _pos, etk::String& _data, int32_t _offset);
};
}

View File

@ -327,7 +327,6 @@ void estyle::Lexer::parse() {
ESTYLE_ERROR("Arrive at the end of file without '\"' element in string parsing");
}
m_list.pushBack(estyle::LexerElement(estyle::lexer::STRING, tokenStart, iii));
iii++;
getChar(iii, currentChar, nextChar);
continue;
}
@ -352,7 +351,6 @@ void estyle::Lexer::parse() {
ESTYLE_ERROR("Arrive at the end of file without '\'' element in string parsing");
}
m_list.pushBack(estyle::LexerElement(estyle::lexer::SIMPLE_QUOTE_STRING, tokenStart, iii));
iii++;
getChar(iii, currentChar, nextChar);
continue;
}

View File

@ -28,11 +28,27 @@ int main(int _argc, const char** _argv) {
return RUN_ALL_TESTS();
}
TEST(testParsingJS, test018) {
TEST(testrestyle, test1) {
estyle::Generator interface;
etk::String source = "/* simple comment */\nint32_t hello = \"plouf \\n\";\n";
etk::String source =
"/* simple comment */\n"
"int32_t hello = \"plouf \\n\";"
"\n";
etk::String output = interface.process(source);
TEST_INFO("source:\n" << source);
TEST_INFO("output:\n" << output);
}
TEST(testrestyle, test2) {
estyle::Generator interface;
etk::String source =
"if (plop == \"plop\" || (kikou != 363464564 && !tree)) { int32_t coucou; int64_t hello = 456; } else if (lol==345 && UNO == why) {/* nothing to do*/} else DEBUG_INFO(\"kjlkj\" << 456346.6 << \" \" << 0xabcdef123);";
etk::String output = interface.process(source);
TEST_INFO("source:\n" << source);