[DEV] better output (function and if)

This commit is contained in:
Edouard DUPIN 2017-12-23 16:01:27 +01:00
parent 33fc52ee15
commit f634d4281f
5 changed files with 194 additions and 44 deletions

View File

@ -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_offset; ++ooo) {
m_output += " ";
}
return;
}
if ( m_output.back() == ' '
@ -114,6 +133,16 @@ void estyle::Generator::addSpace() {
m_output += " ";
}
bool estyle::Generator::onNewLine() {
if (m_output.size() == 0) {
return true;
}
if (m_output.back() == '\n') {
return true;
}
return false;
}
enum class stack {
NAMESPACE,
CLASS,
@ -130,15 +159,24 @@ enum class stack {
etk::Vector<enum stack> 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<m_lexer.size(); ++jjj) {
for (int64_t jjj=iii+1; jjj<m_lexer.size(); ++jjj) {
enum estyle::lexer::tocken elem = m_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(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<m_lexer.size(); ++jjj) {
enum estyle::lexer::tocken elem = m_lexer.getTocken(jjj);
if (elem == estyle::lexer::RESERVED_NEW_LINE) {
// OK ==> 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<m_lexer.size(); ++iii) {
enum estyle::lexer::tocken elem = m_lexer.getTocken(iii);
if (elem == estyle::lexer::RESERVED_NEW_LINE) {
continue;
}
return elem == _nextType;
}
return false;
}
size_t estyle::Generator::generateCondition(size_t _pos, int32_t _offset) {
int64_t estyle::Generator::generateCondition(int64_t _pos) {
int32_t sectionEnd = endOfSection(_pos);
int32_t nbCondition = countCurrentLevelCondition(_pos);
if (nbCondition == 0) {
for (size_t iii=_pos; iii<=sectionEnd; ++iii) {
addSpace();
m_output += m_lexer.getData(iii);
}
process(_pos, sectionEnd+1);
} else {
m_output += "( ";
for (size_t iii=_pos+1; iii<=sectionEnd; ++iii) {
offsetPush(5);
for (int64_t iii=_pos+1; iii<=sectionEnd; ++iii) {
enum estyle::lexer::tocken elem = m_lexer.getTocken(iii);
if (elem == estyle::lexer::PARENTHESE_IN) {
iii = generateCondition(iii, _offset + 5);
iii = generateCondition(iii);
} else if ( elem == estyle::lexer::AND_AND
|| elem == estyle::lexer::OR_OR) {
m_output += getEndOfLine();
offsetPop();
addSpace();
for (int32_t ooo=0; ooo<_offset; ++ooo) {
m_output += " ";
}
offsetPush(5);
m_output += " " + m_lexer.getData(iii) + " ";
} else {
addSpace();
m_output += m_lexer.getData(iii);
iii = process(iii, sectionEnd, estyle::lexer::AND_AND, estyle::lexer::OR_OR, estyle::lexer::PARENTHESE_IN);
iii--;
}
}
offsetPop();
}
m_output += ")";
return sectionEnd;
}
size_t estyle::Generator::endOfSection(size_t _pos) {
int64_t estyle::Generator::generateFunction(int64_t _pos) {
int32_t sectionEnd = endOfSection(_pos);
int32_t nbParameters = countCurrentParameters(_pos);
m_output += "(";
if (nbParameters == 0) {
process(_pos + 1, sectionEnd);
} else {
for (int64_t iii=_pos+1; iii<=sectionEnd; ++iii) {
iii = process(iii, sectionEnd, estyle::lexer::COMA);
if (nextIs(iii, estyle::lexer::PARENTHESE_OUT) == false) {
m_output += ",";
m_output += getEndOfLine();
addSpace();
}
}
}
m_output += ")";
return sectionEnd;
}
int64_t estyle::Generator::endOfSection(int64_t _pos) {
ESTYLE_INFO(" " << 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);
@ -397,7 +486,7 @@ size_t estyle::Generator::endOfSection(size_t _pos) {
ESTYLE_ERROR("can not get end position of " << m_lexer.getTocken(_pos));
return _pos;
}
for (size_t iii=_pos+1; iii < m_lexer.size(); ++iii) {
for (int64_t iii=_pos+1; iii < m_lexer.size(); ++iii) {
elem = m_lexer.getTocken(iii);
if (elem == endTocken) {
return iii;
@ -412,7 +501,11 @@ size_t estyle::Generator::endOfSection(size_t _pos) {
}
int32_t estyle::Generator::countCurrentLevelCondition(size_t _pos) {
int32_t estyle::Generator::countCurrent(int64_t _pos,
enum estyle::lexer::tocken _type1,
enum estyle::lexer::tocken _type2,
enum estyle::lexer::tocken _type3,
enum estyle::lexer::tocken _type4) {
int32_t out = 0;
enum estyle::lexer::tocken endTocken = estyle::lexer::END_OF_FILE;
enum estyle::lexer::tocken elem = m_lexer.getTocken(_pos);
@ -426,7 +519,7 @@ int32_t estyle::Generator::countCurrentLevelCondition(size_t _pos) {
ESTYLE_ERROR("can not get end position of " << m_lexer.getTocken(_pos));
return _pos;
}
for (size_t iii=_pos+1; iii < m_lexer.size(); ++iii) {
for (int64_t iii=_pos+1; iii < m_lexer.size(); ++iii) {
elem = m_lexer.getTocken(iii);
if (elem == endTocken) {
return out;
@ -436,17 +529,30 @@ int32_t estyle::Generator::countCurrentLevelCondition(size_t _pos) {
|| elem == estyle::lexer::BRACE_IN) {
iii = endOfSection(iii);
}
if ( elem == estyle::lexer::AND_AND
|| elem == estyle::lexer::OR_OR) {
if ( _type1 == elem
|| _type2 == elem
|| _type3 == elem
|| _type4 == elem) {
out++;
}
}
return out;
}
int32_t estyle::Generator::countCurrentLevelCondition(int64_t _pos) {
return countCurrent(_pos,
estyle::lexer::AND_AND,
estyle::lexer::OR_OR);
}
int32_t estyle::Generator::countCurrentLevelComa(size_t _pos) {
int32_t out;
int32_t estyle::Generator::countCurrentParameters(int64_t _pos) {
return countCurrent(_pos,
estyle::lexer::COMA);
}
int32_t estyle::Generator::countCurrentLevelComa(int64_t _pos) {
int32_t out = 0;
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) {
@ -459,7 +565,7 @@ int32_t estyle::Generator::countCurrentLevelComa(size_t _pos) {
ESTYLE_ERROR("can not get end position of " << m_lexer.getTocken(_pos));
return _pos;
}
for (size_t iii=_pos+1; iii < m_lexer.size(); ++iii) {
for (int64_t iii=_pos+1; iii < m_lexer.size(); ++iii) {
elem = m_lexer.getTocken(iii);
if (elem == endTocken) {
return out;
@ -476,3 +582,24 @@ int32_t estyle::Generator::countCurrentLevelComa(size_t _pos) {
return out;
}
int32_t estyle::Generator::getWhileCondition(int64_t _pos) {
enum estyle::lexer::tocken endTocken = estyle::lexer::PARENTHESE_OUT;
for (int64_t iii=_pos; iii < m_lexer.size(); ++iii) {
enum estyle::lexer::tocken elem = m_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(iii);
}
if ( elem == estyle::lexer::AND_AND
|| elem == estyle::lexer::OR_OR) {
return iii;
}
}
return m_lexer.size();
}

View File

@ -25,23 +25,45 @@ namespace estyle {
eproperty::List<int32_t> 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<int32_t> 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);
};
}

View File

@ -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()) {

View File

@ -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 {

View File

@ -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);