[DEV] better space for if, parse the namespace to squash it in single id, identify function

This commit is contained in:
Edouard DUPIN 2017-12-13 00:05:22 +01:00
parent 0f261a0c4c
commit 33fc52ee15
7 changed files with 79 additions and 25 deletions

View File

@ -131,7 +131,7 @@ etk::Vector<enum stack> m_urrentStack;
etk::String estyle::Generator::process(const etk::String& _code) {
m_lexer.lexify(_code);
process(0, m_lexer.size(), estyle::lexer::END_OF_FILE) {
process(0, m_lexer.size(), estyle::lexer::END_OF_FILE);
return m_output;
}
@ -289,7 +289,7 @@ int32_t estyle::Generator::process(int32_t _startId, int32_t _stopId, enum estyl
if (elem == estyle::lexer::BRACE_IN) {
m_urrentStack.pushBack(stack::BLOCK);
m_output += getEndOfLine();
//addSpace();
addSpace();
m_output += "{";
m_output += getEndOfLine();
m_indentation++;
@ -315,13 +315,16 @@ int32_t estyle::Generator::process(int32_t _startId, int32_t _stopId, enum estyl
addSpace();
int32_t spaceOffset = 4;
m_output += "if ";
if (previousIs(iii, estyle::lexer::RESERVED_ELSE) == true) {
spaceOffset += 5;
}
for (size_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, m_output, spaceOffset);
iii = generateCondition(jjj, spaceOffset);
break;
} else {
ESTYLE_ERROR("Get 'if' without '(' element");
@ -335,33 +338,44 @@ 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) {
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) {
continue;
}
return elem == _previousType;
}
return false;
}
size_t estyle::Generator::generateCondition(size_t _pos, etk::String& _data, int32_t _offset) {
size_t estyle::Generator::generateCondition(size_t _pos, int32_t _offset) {
int32_t sectionEnd = endOfSection(_pos);
int32_t nbCondition = countCurrentLevelCondition(_pos);
if (nbCondition == 0) {
for (size_t iii=_pos; iii<=sectionEnd; ++iii) {
addSpace(_data);
_data += m_lexer.getData(iii);
addSpace();
m_output += m_lexer.getData(iii);
}
} else {
_data += "( ";
m_output += "( ";
for (size_t iii=_pos+1; iii<=sectionEnd; ++iii) {
enum estyle::lexer::tocken elem = m_lexer.getTocken(iii);
if (elem == estyle::lexer::PARENTHESE_IN) {
iii = generateCondition(m_lexer, iii, _data, _offset + 5);
iii = generateCondition(iii, _offset + 5);
} else if ( elem == estyle::lexer::AND_AND
|| elem == estyle::lexer::OR_OR) {
_data += getEndOfLine();
addSpace(_data);
m_output += getEndOfLine();
addSpace();
for (int32_t ooo=0; ooo<_offset; ++ooo) {
_data += " ";
m_output += " ";
}
_data += " " + m_lexer.getData(iii) + " ";
m_output += " " + m_lexer.getData(iii) + " ";
} else {
addSpace(_data);
_data += m_lexer.getData(iii);
addSpace();
m_output += m_lexer.getData(iii);
}
}
@ -391,7 +405,7 @@ size_t estyle::Generator::endOfSection(size_t _pos) {
if ( elem == estyle::lexer::PARENTHESE_IN
|| elem == estyle::lexer::BRACKET_IN
|| elem == estyle::lexer::BRACE_IN) {
iii = endOfSection(m_lexer, iii);
iii = endOfSection(iii);
}
}
return m_lexer.size();
@ -399,7 +413,7 @@ size_t estyle::Generator::endOfSection(size_t _pos) {
int32_t estyle::Generator::countCurrentLevelCondition(size_t _pos) {
int32_t out;
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) {

View File

@ -31,6 +31,7 @@ namespace estyle {
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);
private:
void addSpace();
etk::String getEndOfLine();
@ -38,7 +39,9 @@ namespace estyle {
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, etk::String& _data, int32_t _offset);
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);
};
}

View File

@ -44,7 +44,7 @@ void estyle::Lexer::getChar(size_t _iii, char& _currentChar, char& _nextChar) {
} else {
_nextChar = 0;
}
ESTYLE_DEBUG(" parse '" << etk::String(_currentChar) << "'");
//ESTYLE_DEBUG(" parse '" << etk::String(_currentChar) << "'");
return;
}
_currentChar = 0;
@ -561,6 +561,8 @@ void estyle::Lexer::lexify(const etk::String& _input) {
m_stream = _input;
ESTYLE_DEBUG("Parse stream");
parse();
postAnnalyse_namespace();
postAnnalyse_function();
ESTYLE_DEBUG("find:");
for (auto &it: m_list) {
ESTYLE_DEBUG(" '" << it.getTocken() << "' ==> '" << m_stream.extract(it.getStart(),it.getStop()) << "'");
@ -624,22 +626,22 @@ void estyle::Lexer::postAnnalyse_namespace() {
{
auto itTmp = it;
++itTmp;
if (it != m_list.end() {
if (it != m_list.end()) {
if (itTmp->getTocken() == estyle::lexer::ID) {
it.setStop(itTmp->getStop());
it.setTocken(estyle::lexer::ID);
it->setStop(itTmp->getStop());
it->setTocken(estyle::lexer::ID);
// This work because I use etk::Vector ...
m_list.erase(itTmp);
}
}
}
if (it != m_list.begin() {
if (it != m_list.begin()) {
auto itTmp = it;
--itTmp;
if (itTmp->getTocken() == estyle::lexer::ID) {
itTmp.setStop(itTmp->getStop());
itTmp->setStop(it->getStop());
it = m_list.erase(it);
ESTYLE_WARNING("collapse '" << m_stream.extract(itTmp.getStart(), itTmp.getStop()) << "'");
ESTYLE_WARNING("collapse '" << m_stream.extract(itTmp->getStart(), itTmp->getStop()) << "'");
continue;
}
}
@ -647,4 +649,19 @@ void estyle::Lexer::postAnnalyse_namespace() {
}
++it;
}
}
void estyle::Lexer::postAnnalyse_function() {
auto it = m_list.begin();
while (it != m_list.end()) {
if (it->getTocken() == estyle::lexer::PARENTHESE_IN) {
if (it != m_list.begin()) {
auto itTmp = it;
--itTmp;
if (itTmp->getTocken() == estyle::lexer::ID) {
itTmp->setTocken(estyle::lexer::ELEMENT_FUNCTION);
}
}
}
++it;
}
}

View File

@ -50,6 +50,7 @@ namespace estyle {
public:
// squash element name space like "::lklkmlk" and "lmkmlk::lmkmlk::mlklk" in 1 element
void postAnnalyse_namespace();
void postAnnalyse_function();
public:
size_t size() const {
return m_list.size();

View File

@ -122,6 +122,8 @@ etk::String estyle::lexer::toString(estyle::lexer::tocken _token) {
case estyle::lexer::BASIC_TYPE_STD_STRING: return "std::string";
case estyle::lexer::BASIC_TYPE_STD_NULLPTR: return "std::nullptr";
case estyle::lexer::BASIC_TYPE_VOID: return "void";
case estyle::lexer::ELEMENT_FUNCTION: return "FUNCTION";
}
return etk::String("?[") + etk::toString(int32_t(_token)) + "]";
}

View File

@ -105,7 +105,7 @@ namespace estyle {
RESERVED_SHARP_IF_NOT_DEFINE, //!< element "#ifndef"
RESERVED_SHARP_END_IF, //!< element "#endif"
RESERVED_SHARP_ELSE, //!< element "#else"
RESERVED_NEW_LINE, //!< Neew line
RESERVED_NEW_LINE, //!< New line
RESERVED_LIST_END,
BASIC_TYPE_LIST_BEGIN,
@ -129,6 +129,10 @@ namespace estyle {
BASIC_TYPE_STD_NULLPTR, //!<
BASIC_TYPE_VOID, //!<
// post annalyse element:
ELEMENT_FUNCTION, //!< all composed with an ID previous a (
BASIC_TYPE_LIST_END,
};
etk::String toString(estyle::lexer::tocken _token);

View File

@ -56,4 +56,17 @@ TEST(testrestyle, test2) {
}
TEST(testrestyle, test3) {
estyle::Generator interface;
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);";
etk::String output = interface.process(source);
TEST_INFO("source:\n" << source);
TEST_INFO("output:\n" << output);
}