[DEV] first std::regex really work in c++

This commit is contained in:
Edouard DUPIN 2014-10-06 23:22:14 +02:00
parent 243d7e7494
commit d93844d6da
3 changed files with 83 additions and 37 deletions

View File

@ -50,71 +50,69 @@
<color>doubleQuoteText</color> <color>doubleQuoteText</color>
<regex>'((\\[\\'])|.){1,2}'</regex> <regex>'((\\[\\'])|.){1,2}'</regex>
</rule> </rule>
<rule name="simpleQuteTextError">
<color>SYNTAX_ERROR</color>
<regex>'</regex>
</rule>
</pass1> </pass1>
<pass2> <!-- Parse on display data ==> nor regenerate every display but every time modification apear --> <pass2> <!-- Parse on display data ==> nor regenerate every display but every time modification apear -->
<rule name="my keyword"> <rule name="my keyword">
<color>keyword</color> <color>keyword</color>
<regex>\@return|goto|if|else|case|default|switch|break|continue|while|do|for|sizeof\@</regex> <regex>\b(return|goto|if|else|case|default|switch|break|continue|while|do|for|sizeof)\b</regex>
</rule> </rule>
<rule name="my function keyword"> <rule name="my function keyword">
<color>systemFunction</color> <color>systemFunction</color>
<regex>\@new|delete|try|catch|memset|fopen|fread|fwrite|fgets|fclose|printf|(f|s|diag_)printf|calloc|malloc|realloc|(cyg|sup)_([a-z]|[A-Z]|[0-9]|_)+\@</regex> <regex>\b(new|delete|try|catch|memset|fopen|fread|fwrite|fgets|fclose|printf|(f|s|diag_)printf|calloc|malloc|realloc|(cyg|sup)_([a-z]|[A-Z]|[0-9]|_)+)\b</regex>
</rule> </rule>
<rule name="my type"> <rule name="my type">
<color>type</color> <color>type</color>
<regex>\@bool|BOOL|char(16_t|32_t)?|double|float|u?int(8|16|32|64|128)?(_t)?|long|short|signed|size_t|unsigned|void|(I|U)(8|16|32|64|128)\@</regex> <regex>\b(bool|BOOL|char(16_t|32_t)?|double|float|u?int(8|16|32|64|128)?(_t)?|long|short|signed|size_t|unsigned|void|(I|U)(8|16|32|64|128))\b</regex>
</rule> </rule>
<rule name="std type"> <rule name="std type">
<color>type</color> <color>type</color>
<regex>\@std::(vector|(u16|u32|w)?string|codecvt_utf(16|8_utf16|8)+|complex|iterator(_traits)?|tuple(_element|_size)?|pair)\@</regex> <regex>\b(std::(vector|(u16|u32|w)?string|codecvt_utf(16|8_utf16|8)+|complex|iterator(_traits)?|tuple(_element|_size)?|pair))\b</regex>
</rule> </rule>
<rule name="my storage keyword"> <rule name="my storage keyword">
<color>storageKeyword</color> <color>storageKeyword</color>
<regex>\@inline|const|class|virtual|private|public|protected|friend|const|extern|auto|register|static|unsigned|signed|volatile|char|double|float|int|long|short|void|typedef|struct|union|enum\@</regex> <regex>\b(inline|const|class|virtual|private|public|protected|friend|const|extern|auto|register|static|unsigned|signed|volatile|char|double|float|int|long|short|void|typedef|struct|union|enum)\b</regex>
</rule> </rule>
<rule name="my common Define"> <rule name="my common Define">
<color>commonDefine</color> <color>commonDefine</color>
<regex>\@NULL|MAX|MIN|__LINE__|__DATA__|__FILE__|__func__|__TIME__|__STDC__\@</regex> <regex>\b(NULL|MAX|MIN|__LINE__|__DATA__|__FILE__|__func__|__TIME__|__STDC__)\b</regex>
</rule> </rule>
<rule name="numeric constant"> <rule name="numeric constant">
<color>number</color> <color>number</color>
<regex>\@((0(x|X)[0-9a-fA-F]*)|(\d+\.?\d*|\.\d+)((e|E)(\+|\-)?\d+)?)(L|l|UL|ul|u|U|F|f)?\@</regex> <regex>\b(((0(x|X)[0-9a-fA-F]*)|(\d+\.?\d*|\.\d+)((e|E)(\+|\-)?\d+)?)(L|l|UL|ul|u|U|F|f)?)\b</regex>
</rule> </rule>
<rule name="my boolean"> <rule name="my boolean">
<color>boolean</color> <color>boolean</color>
<regex>\@true|TRUE|false|FALSE\@</regex> <regex>\b(true|TRUE|false|FALSE)\b</regex>
</rule> </rule>
<rule name="BIG LETTER"> <rule name="BIG LETTER">
<color>macro</color> <color>macro</color>
<regex>\@[A-Z_][A-Z_0-9]{3,500}\@</regex> <regex>\b([A-Z_][A-Z_0-9]{3,500})\b</regex>
</rule> </rule>
<rule name="CPP member"> <rule name="CPP member">
<color>memberClass</color> <color>memberClass</color>
<regex>\@m_[A-Za-z_0-9]*\@</regex> <regex>\b(m_[A-Za-z_0-9]*)\b</regex>
</rule> </rule>
<rule name="function input"> <rule name="function input">
<color>inputFunction</color> <color>inputFunction</color>
<regex>\@_[A-Za-z_0-9]*\@</regex> <regex>\b(_[A-Za-z_0-9]*)\b</regex>
<max>false</max> <max>false</max>
</rule> </rule>
<!--
<rule name="Function name"> <rule name="Function name">
<color>functionName</color> <color>functionName</color>
<regex>\@(\w|_)+[ \t]*\(</regex> <regex>\b((\w|_)+[ \t]*\()</regex>
</rule> </rule>
<rule name="condition"> <rule name="condition">
<color>boolean</color> <color>boolean</color>
<regex>==|&lt;=|&gt;=|!=|&lt;{1,2}|&gt;{1,2}|&amp;&amp;|\{|\}|</regex> <regex>==|&lt;=|&gt;=|!=|&lt;{1,2}|&gt;{1,2}|&amp;&amp;|\{|\}</regex>
</rule> </rule>
-->
<rule name="BIG LETTER"> <rule name="BIG LETTER">
<color>macro</color> <color>macro</color>
<regex>([A-Z]|_){4,500}</regex> <regex>([A-Z]|_){4,500}</regex>
<elemSubColor id="1">doxElem</elemSubColor> <elemSubColor id="1">doxElem</elemSubColor>
</rule> </rule>
<rule name="simpleQuteTextError">
<color>SYNTAX_ERROR</color>
<regex>'|"</regex>
</rule>
</pass2> </pass2>
</EdnLang> </EdnLang>

View File

@ -10,6 +10,7 @@
#include <appl/global.h> #include <appl/global.h>
#include <appl/Highlight.h> #include <appl/Highlight.h>
#include <exml/exml.h> #include <exml/exml.h>
#include <ewol/ewol.h>
#include <ewol/resource/Manager.h> #include <ewol/resource/Manager.h>
@ -176,19 +177,27 @@ void appl::Highlight::parse(int64_t _start,
int64_t elementStart = _start; int64_t elementStart = _start;
int64_t elementStop = _stop; int64_t elementStop = _stop;
appl::HighlightInfo resultat; appl::HighlightInfo resultat;
int64_t startTime = ewol::getTime();
while (elementStart <= elementStop) { while (elementStart <= elementStop) {
HL_DEBUG("Parse element in the buffer pos=" << elementStart); //APPL_DEBUG("Parse element in the buffer pos=" << elementStart);
appl::HighlightInfo resultatLast; appl::HighlightInfo resultatLast;
int64_t findAnOtherId = -1; int64_t findAnOtherId = -1;
int64_t currentTime = ewol::getTime();
//try to fond the HL in ALL of we have //try to fond the HL in ALL of we have
for (int64_t jjj=0; jjj<(int64_t)m_listHighlightPass1.size(); jjj++){ for (int64_t jjj=0; jjj<(int64_t)m_listHighlightPass1.size(); jjj++){
enum resultFind ret = HLP_FIND_OK; enum resultFind ret = HLP_FIND_OK;
HL_DEBUG("Parse HL id=" << jjj << " position search: (" << elementStart << "," << _stop << ") input start='" << _buffer[elementStart] << "' " << m_listHighlightPass1[jjj]->getPaternString()); /*
if (_buffer[elementStart] == '\n') {
APPL_DEBUG("Parse HL id=" << jjj << " position search: (" << elementStart << "," << _stop << ") input start='\\n' " << m_listHighlightPass1[jjj]->getPaternString());
} else {
APPL_DEBUG("Parse HL id=" << jjj << " position search: (" << elementStart << "," << _stop << ") input start='" << _buffer[elementStart] << "' " << m_listHighlightPass1[jjj]->getPaternString());
}
*/
// Stop the search to the end (to get the end of the pattern) // Stop the search to the end (to get the end of the pattern)
ret = m_listHighlightPass1[jjj]->find(elementStart, _buffer.size(), resultat, _buffer); ret = m_listHighlightPass1[jjj]->find(elementStart, _buffer.size(), resultat, _buffer);
if (HLP_FIND_ERROR != ret) { if (HLP_FIND_ERROR != ret) {
if (elementStart == resultat.start) { if (elementStart == resultat.start) {
APPL_DEBUG("Find Pattern in the Buffer : (" << resultat.start << "," << resultat.stop << ") startPos=" << elementStart ); //APPL_DEBUG(" native Find");
findAnOtherId = jjj; findAnOtherId = jjj;
resultatLast = resultat; resultatLast = resultat;
break; break;
@ -202,13 +211,16 @@ void appl::Highlight::parse(int64_t _start,
} }
} }
} }
int64_t currentTimeEnd = ewol::getTime();
int64_t deltaTime = currentTimeEnd - currentTime;
if (findAnOtherId != -1) { if (findAnOtherId != -1) {
//APPL_DEBUG("Find Pattern in the Buffer : time=" << (float)deltaTime/1000.0f << " ms (" << resultatLast.start << "," << resultatLast.stop << ") startPos=" << elementStart << " for=" << m_listHighlightPass1[findAnOtherId]->getPaternString());
// remove element in the current List where the current Element have a end inside the next... // remove element in the current List where the current Element have a end inside the next...
int64_t kkk=_addingPos; int64_t kkk=_addingPos;
while(kkk < (int64_t)_metaData.size() ) { while(kkk < (int64_t)_metaData.size() ) {
if (_metaData[kkk].start <= resultatLast.stop) { if (_metaData[kkk].start <= resultatLast.stop) {
// remove element // remove element
HL_DEBUG("Erase element=" << kkk); APPL_DEBUG("Erase element=" << kkk);
_metaData.erase(_metaData.begin()+kkk, _metaData.begin()+kkk+1); _metaData.erase(_metaData.begin()+kkk, _metaData.begin()+kkk+1);
// Increase the end of search // Increase the end of search
if (kkk < (int64_t)_metaData.size()) { if (kkk < (int64_t)_metaData.size()) {
@ -232,11 +244,17 @@ void appl::Highlight::parse(int64_t _start,
_addingPos++; _addingPos++;
// We find a pattern == > Stop search for the current element // We find a pattern == > Stop search for the current element
} else { } else {
break; //APPL_DEBUG("loose time : time=" << deltaTime << " us");
//break;
} }
// Go to the next element (and search again ...). // Go to the next element (and search again ...).
elementStart++; elementStart++;
} }
int64_t stopTime = ewol::getTime();
int64_t deltaTimeGlobal = stopTime - startTime;
APPL_DEBUG("parse in time=" << (float)deltaTimeGlobal/1000.0f << " ms ");
} }
@ -258,24 +276,42 @@ void appl::Highlight::parse2(int64_t _start,
if (elementStart == 306) { if (elementStart == 306) {
//etk::log::setLevel(etk::log::logLevelVerbose); //etk::log::setLevel(etk::log::logLevelVerbose);
} }
appl::HighlightInfo resultatLast;
int64_t findAnOtherId = -1;
//HL2_DEBUG("Parse element in the buffer pos=" << elementStart << "," << _buffer.size() << ")" ); //HL2_DEBUG("Parse element in the buffer pos=" << elementStart << "," << _buffer.size() << ")" );
//try to fond the HL in ALL of we have //try to fond the HL in ALL of we have
for (int64_t jjj=0; jjj<(int64_t)m_listHighlightPass2.size(); jjj++){ for (int64_t jjj=0; jjj<(int64_t)m_listHighlightPass2.size(); jjj++){
enum resultFind ret = HLP_FIND_OK; enum resultFind ret;
HL2_DEBUG("Parse HL id=" << jjj << " position search: (" << HL_DEBUG("Parse HL id=" << jjj << " position search: (" <<
elementStart << "," << elementStop << ") in='" elementStart << "," << elementStop << ") in='"
<< _buffer[elementStart] << "' " << m_listHighlightPass2[jjj]->getPaternString()); << _buffer[elementStart] << "' " << m_listHighlightPass2[jjj]->getPaternString());
// Stop the search to the end (to get the end of the pattern) // Stop the search to the end (to get the end of the pattern)
ret = m_listHighlightPass2[jjj]->find(elementStart, elementStop, resultat, _buffer); ret = m_listHighlightPass2[jjj]->find(elementStart, elementStop, resultat, _buffer);
if (ret != HLP_FIND_ERROR) { if (ret != HLP_FIND_ERROR) {
HL2_DEBUG("Find Pattern in the Buffer : (" << resultat.start << "," << resultat.stop << ")" ); if (elementStart == resultat.start) {
// add curent element in the list ... //APPL_DEBUG("Find Pattern in the Buffer : (" << resultat.start << "," << resultat.stop << ") startPos=" << elementStart );
_metaData.push_back(resultat); findAnOtherId = jjj;
elementStart = resultat.stop-1; resultatLast = resultat;
// Exit current cycle break;
break; } else {
// stack last find to prevent a unneded seach:
if ( findAnOtherId == -1
|| resultat.start < resultatLast.start) {
findAnOtherId = jjj;
resultatLast = resultat;
}
}
} }
} }
if (findAnOtherId != -1) {
//APPL_DEBUG("Find Pattern in the Buffer : (" << resultat.start << "," << resultat.stop << ") startPos=" << elementStart );
// add curent element in the list ...
_metaData.push_back(resultat);
elementStart = resultat.stop-1;
} else {
//break;
}
// Go to the next element (and search again ...). // Go to the next element (and search again ...).
elementStart++; elementStart++;
} }

View File

@ -31,7 +31,8 @@ appl::HighlightPattern::~HighlightPattern() {
void appl::HighlightPattern::setPatern(const std::string& _regExp, bool forceMaximize) { void appl::HighlightPattern::setPatern(const std::string& _regExp, bool forceMaximize) {
m_regexValue = _regExp; m_regexValue = _regExp;
try { try {
m_regExp.assign(_regExp); m_regExp.assign(_regExp, std::regex_constants::optimize | std::regex_constants::ECMAScript);
//m_regExp.assign(_regExp, std::regex_constants::optimize | std::regex_constants::extended);
m_hasParsingError = false; m_hasParsingError = false;
} catch (std::regex_error e) { } catch (std::regex_error e) {
m_hasParsingError = true; m_hasParsingError = true;
@ -126,12 +127,23 @@ enum resultFind appl::HighlightPattern::find(int32_t _start,
std::smatch resultMatch; std::smatch resultMatch;
//APPL_DEBUG("find data at : start=" << _start << " stop=" << _stop << " regex='" << m_regexValue << "'"); //APPL_DEBUG("find data at : start=" << _start << " stop=" << _stop << " regex='" << m_regexValue << "'");
std::regex_search(_buffer.begin() + _start, _buffer.begin() + _stop, resultMatch, m_regExp); std::regex_search(_buffer.begin() + _start, _buffer.begin() + _stop, resultMatch, m_regExp, std::regex_constants::match_continuous);
if (resultMatch.size() > 0) { if (resultMatch.size() > 0) {
_resultat.start = std::distance(_buffer.begin(), resultMatch[0].first);; _resultat.start = std::distance(_buffer.begin(), resultMatch[0].first);;
_resultat.stop = std::distance(_buffer.begin(), resultMatch[0].second); _resultat.stop = std::distance(_buffer.begin(), resultMatch[0].second);
//APPL_DEBUG("find data at : start=" << _resultat.start << " stop=" << _resultat.stop << " data='" <<std::string(_buffer, _resultat.start, _resultat.stop-_resultat.start) << "'" ); //APPL_DEBUG("find data at : start=" << _resultat.start << " stop=" << _resultat.stop << " data='" <<std::string(_buffer, _resultat.start, _resultat.stop-_resultat.start) << "'" );
/* /*
if (true){
//TK_DEBUG("in line : '" << etk::to_string(_buffer) << "'");
TK_DEBUG(" Find " << resultMatch.size() << " elements");
for (size_t iii=0; iii<resultMatch.size(); ++iii) {
int32_t posStart = std::distance(_buffer.begin(), resultMatch[iii].first);
int32_t posStop = std::distance(_buffer.begin(), resultMatch[iii].second);
TK_DEBUG(" [" << iii << "] " << posStart << " to " << posStop);
}
}
*/
/*
if (false){ if (false){
TK_DEBUG("in line : '" << etk::to_string(_buffer) << "'"); TK_DEBUG("in line : '" << etk::to_string(_buffer) << "'");
TK_DEBUG(" Find " << resultMatch.size() << " elements"); TK_DEBUG(" Find " << resultMatch.size() << " elements");
@ -140,7 +152,7 @@ enum resultFind appl::HighlightPattern::find(int32_t _start,
int32_t posStop = std::distance(_buffer.begin(), resultMatch[iii].second); int32_t posStop = std::distance(_buffer.begin(), resultMatch[iii].second);
TK_DEBUG(" [" << iii << "] " << *resultMatch[iii].first); TK_DEBUG(" [" << iii << "] " << *resultMatch[iii].first);
TK_DEBUG(" [" << iii << "] " << *resultMatch[iii].second); TK_DEBUG(" [" << iii << "] " << *resultMatch[iii].second);
TK_DEBUG(" [" << iii << "] " << etk::to_string(std::u32string(_buffer, posStart, posStop-posStart))); TK_DEBUG(" [" << iii << "] " << std::string(_buffer, posStart, posStop-posStart));
} }
} }
*/ */