PhoenixFileParser  1.5.1
Set of tools to ease file parsing
Loading...
Searching...
No Matches
PHighlightParser Class Reference

Parse full hightlighting of a string. More...

#include <PHighlightParser.h>

Public Member Functions

void addToken (const PString &beginPattern, const PString &endPattern, const PString &replaceBeforePattern, const PString &replaceAfterPattern)
 Add a begin end pattern.
 
void addToken (const PVecString &vecToken, const PString &replaceBeforePattern, const PString &replaceAfterPattern)
 Add token in the highlighter.
 
void clear ()
 Clear all PHighlightingAction.
 
PString makeHighlighting (const PString &content)
 Do the highlighting of the given PString.
 
 PHighlightParser ()
 Default constructor of PHighlightParser.
 
virtual ~PHighlightParser ()
 Destructor of PHighlightParser.
 

Private Member Functions

void doReplaceAction (PString &out, PFileParser &parser, PHighlightingAction *action, const PString &replacePattern, size_t indexLastMatchReplace)
 Do the replace action.
 
void initialisationPHighlightParser ()
 Initialisation function of the class PHighlightParser.
 

Private Attributes

PReplaceHightlightTree p_replaceTree
 Map of tree to be used to do the hightlighting.
 
std::vector< PHighlightingAction * > p_vecPtrAction
 Vector of pointer of PHighlightingAction.
 

Detailed Description

Parse full hightlighting of a string.

Definition at line 37 of file PHighlightParser.h.

Constructor & Destructor Documentation

◆ PHighlightParser()

PHighlightParser::PHighlightParser ( )

Default constructor of PHighlightParser.

Definition at line 10 of file PHighlightParser.cpp.

10 {
12}
void initialisationPHighlightParser()
Initialisation function of the class PHighlightParser.

References initialisationPHighlightParser().

+ Here is the call graph for this function:

◆ ~PHighlightParser()

PHighlightParser::~PHighlightParser ( )
virtual

Destructor of PHighlightParser.

Definition at line 15 of file PHighlightParser.cpp.

15 {
16 clear();
17}
void clear()
Clear all PHighlightingAction.

References clear().

+ Here is the call graph for this function:

Member Function Documentation

◆ addToken() [1/2]

void PHighlightParser::addToken ( const PString & beginPattern,
const PString & endPattern,
const PString & replaceBeforePattern,
const PString & replaceAfterPattern )

Add a begin end pattern.

Parameters
beginPattern: begin pattern to find
endPattern: end pattern of the sequence
replaceBeforePattern: PString to be placed before the begin pattern
replaceAfterPattern: PString to be placed after the end pattern

Definition at line 41 of file PHighlightParser.cpp.

41 {
42 PHighlightingAction* action = new PHighlightingAction;
44 action->replaceBeforePattern = replaceBeforePattern;
45 action->replaceAfterPattern = replaceAfterPattern;
46 action->endPattern = endPattern;
47 p_vecPtrAction.push_back(action);
48 p_replaceTree.addKeyValue(beginPattern.toVector(), action);
49}
PReplaceHightlightTree p_replaceTree
Map of tree to be used to do the hightlighting.
std::vector< PHighlightingAction * > p_vecPtrAction
Vector of pointer of PHighlightingAction.
PString endPattern
End pattern to be used with the GET_UNITL_WITHOUT_PATTERN.
PString replaceBeforePattern
String to be placed before the replaced pattern.
PString replaceAfterPattern
String to be placed after the replaced pattern.
PHighlightType::PHighlightType type
Type of the hightlighting.

References PHighlightingAction::endPattern, PHighlightType::GET_UNITL_WITHOUT_PATTERN, p_replaceTree, p_vecPtrAction, PHighlightingAction::replaceAfterPattern, PHighlightingAction::replaceBeforePattern, and PHighlightingAction::type.

◆ addToken() [2/2]

void PHighlightParser::addToken ( const PVecString & vecToken,
const PString & replaceBeforePattern,
const PString & replaceAfterPattern )

Add token in the highlighter.

Parameters
vecToken: vector of token to be treated with the same replace before and after
replaceBeforePattern: PString to be placed before the found pattern
replaceAfterPattern: PString to be placed after the found pattern

Definition at line 24 of file PHighlightParser.cpp.

24 {
25 PHighlightingAction* action = new PHighlightingAction;
27 action->replaceBeforePattern = replaceBeforePattern;
28 action->replaceAfterPattern = replaceAfterPattern;
29 p_vecPtrAction.push_back(action);
30 for(PVecString::const_iterator it(vecToken.begin()); it != vecToken.end(); ++it){
31 p_replaceTree.addKeyValue(it->toVector(), action);
32 }
33}

References p_replaceTree, p_vecPtrAction, PHighlightType::REPLACE, PHighlightingAction::replaceAfterPattern, PHighlightingAction::replaceBeforePattern, and PHighlightingAction::type.

◆ clear()

void PHighlightParser::clear ( )

Clear all PHighlightingAction.

Definition at line 116 of file PHighlightParser.cpp.

116 {
117 for(std::vector<PHighlightingAction*>::iterator it(p_vecPtrAction.begin()); it != p_vecPtrAction.end(); ++it){
118 delete *it;
119 }
120}

References p_vecPtrAction.

Referenced by ~PHighlightParser().

+ Here is the caller graph for this function:

◆ doReplaceAction()

void PHighlightParser::doReplaceAction ( PString & out,
PFileParser & parser,
PHighlightingAction * action,
const PString & replacePattern,
size_t indexLastMatchReplace )
private

Do the replace action.

Parameters
[out]out: PString to be modified
parser: PFileParser to be used to parse the current content
action: PHighlightingAction to be performed
replacePattern: current replace pattern (to put replace before and after PString around it)
indexLastMatchReplace: index of the last replace ends

Definition at line 134 of file PHighlightParser.cpp.

134 {
135 if(action == NULL){return;}
136 if(action->type == PHighlightType::REPLACE){
137 out += action->replaceBeforePattern + replacePattern + action->replaceAfterPattern; //Let's replace it
138 parser.setCurrentCharIdx(indexLastMatchReplace);
139 parser.incrementCurrentChar();
141 out += action->replaceBeforePattern + replacePattern + parser.getUntilKeyWithoutPatern(action->endPattern) + action->endPattern + action->replaceAfterPattern; //Let's replace it
142 }
143}
PString getUntilKeyWithoutPatern(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern exclu.
void incrementCurrentChar(size_t nbChar=1lu)
Increment the current caracter.
void setCurrentCharIdx(size_t index)
Set the index of the current char.

References PHighlightingAction::endPattern, PHighlightType::GET_UNITL_WITHOUT_PATTERN, PFileParser::getUntilKeyWithoutPatern(), PFileParser::incrementCurrentChar(), PHighlightType::REPLACE, PHighlightingAction::replaceAfterPattern, PHighlightingAction::replaceBeforePattern, PFileParser::setCurrentCharIdx(), and PHighlightingAction::type.

Referenced by makeHighlighting().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ initialisationPHighlightParser()

void PHighlightParser::initialisationPHighlightParser ( )
private

Initialisation function of the class PHighlightParser.

Definition at line 123 of file PHighlightParser.cpp.

123 {
124
125}

Referenced by PHighlightParser().

+ Here is the caller graph for this function:

◆ makeHighlighting()

PString PHighlightParser::makeHighlighting ( const PString & content)

Do the highlighting of the given PString.

Parameters
content: PString to be used
Returns
PString with corresponding highlighting

Definition at line 55 of file PHighlightParser.cpp.

55 {
56 if(content.size() == 0lu || p_replaceTree.getMapChild().size() == 0lu){return content;}
57 PFileParser parser;
58 parser.setFileContent(content);
59 PReplaceHightlightTree::iterator it(p_replaceTree.begin());
60 bool isPreviousMatchingString(false);
61 PString out(""), pattern(""), replacePattern("");
62 PHighlightingAction* action = NULL;
63 size_t beginTest(0lu), nbMatch(0lu), indexLastMatchReplace(0lu);
64 while(!parser.isEndOfFile()){
65 char currentChar = parser.getCurrentCh();
66// std::cerr << "PString::replace : content["<<i<<"] = '"<<currentChar<<"'" << std::endl;
67 if(it.next(currentChar)){ //We found a matching character
68 if(nbMatch == 0lu){ //This is the first matched character for this sequence
69 beginTest = parser.getCurrentCharIdx(); //So we remember the begining position
70 }
71 pattern += currentChar;
72 ++nbMatch; //Let's remember how much characters were OK
73 if(it->hasValue()){ //If the current node tree has a value
74 action = it->getValue();
75 replacePattern = pattern; //We have to remember the current pattern because it will change if we have other partial matches
76 isPreviousMatchingString = true; //And we say that we have a value
77 indexLastMatchReplace = parser.getCurrentCharIdx();
78 }
79 parser.incrementCurrentChar();
80 }else{
81 if(nbMatch == 0lu){
82 it = p_replaceTree.begin(); //Let's check again from the begining of the tree
83 isPreviousMatchingString = false;
84 pattern = "";
85 out += currentChar;
86 parser.incrementCurrentChar();
87 }else{
88 if(isPreviousMatchingString){ //If we had something to replace
89 doReplaceAction(out, parser, action, replacePattern, indexLastMatchReplace); //Let's replace it
90 }else{
91 out += parser.getChar(beginTest); //no sequence matches
92 parser.setCurrentCharIdx(beginTest); //so we start again one character later (we will have the ++i of the for loop for that)
93 parser.incrementCurrentChar();
94 }
95 isPreviousMatchingString = false;
96 pattern = "";
97 action = NULL;
98 nbMatch = 0lu;
99 it = p_replaceTree.begin(); //Let's check again from the begining of the tree
100 }
101 }
102 }
103 if(isPreviousMatchingString){ //If we had something to replace
104 doReplaceAction(out, parser, action, replacePattern, indexLastMatchReplace); //Let's replace it
105 }else if(nbMatch != 0lu){
106 for(size_t i(beginTest); i < parser.getNbTotalChar(); ++i){
107 out += parser.getChar(i);
108
109 }
110 }
111 //We are potentially at the end of the source, so no more test
112 return out;
113}
char getChar(size_t index) const
Get the char at the given index.
char getCurrentCh() const
Renvoie le caractère courant.
void setFileContent(const PString &fileContent)
Set the file content.
size_t getCurrentCharIdx() const
Return the index of the current character.
bool isEndOfFile() const
Dit si on est à la fin du fichier.
size_t getNbTotalChar() const
Return the number of characters in the current opened file.
void doReplaceAction(PString &out, PFileParser &parser, PHighlightingAction *action, const PString &replacePattern, size_t indexLastMatchReplace)
Do the replace action.

References doReplaceAction(), PFileParser::getChar(), PFileParser::getCurrentCh(), PFileParser::getCurrentCharIdx(), PFileParser::getNbTotalChar(), PFileParser::incrementCurrentChar(), PFileParser::isEndOfFile(), p_replaceTree, PFileParser::setCurrentCharIdx(), and PFileParser::setFileContent().

+ Here is the call graph for this function:

Member Data Documentation

◆ p_replaceTree

PReplaceHightlightTree PHighlightParser::p_replaceTree
private

Map of tree to be used to do the hightlighting.

Definition at line 54 of file PHighlightParser.h.

Referenced by addToken(), addToken(), and makeHighlighting().

◆ p_vecPtrAction

std::vector<PHighlightingAction*> PHighlightParser::p_vecPtrAction
private

Vector of pointer of PHighlightingAction.

Definition at line 56 of file PHighlightParser.h.

Referenced by addToken(), addToken(), and clear().


The documentation for this class was generated from the following files: