Directory: | ./ |
---|---|
File: | src/FrontEnd/HeaderParser.cpp |
Date: | 2025-03-14 12:14:21 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 107 | 157 | 68.2% |
Branches: | 171 | 318 | 53.8% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include "HeaderParser.h" | ||
8 | |||
9 | ///Default constructor of HeaderParser | ||
10 |
5/5✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
✓ Branch 16 taken 3 times.
|
3 | HeaderParser::HeaderParser(){ |
11 |
1/1✓ Branch 1 taken 3 times.
|
3 | initialisationHeaderParser(); |
12 | 3 | } | |
13 | |||
14 | ///Copy constructor of HeaderParser | ||
15 | /** @param other : class to copy | ||
16 | */ | ||
17 | ✗ | HeaderParser::HeaderParser(const HeaderParser & other){ | |
18 | ✗ | copyHeaderParser(other); | |
19 | } | ||
20 | |||
21 | ///Destructor of HeaderParser | ||
22 | 6 | HeaderParser::~HeaderParser(){ | |
23 | |||
24 | } | ||
25 | |||
26 | ///Definition of equal operator of HeaderParser | ||
27 | /** @param other : class to copy | ||
28 | * @return copied class | ||
29 | */ | ||
30 | ✗ | HeaderParser & HeaderParser::operator = (const HeaderParser & other){ | |
31 | ✗ | copyHeaderParser(other); | |
32 | ✗ | return *this; | |
33 | } | ||
34 | |||
35 | ///Get the parsed vector of PSource | ||
36 | /** @return parsed vector of PSource | ||
37 | */ | ||
38 | 3 | const PVecSource & HeaderParser::getVecSource() const{ | |
39 | 3 | return p_vecSource; | |
40 | } | ||
41 | |||
42 | ///Copy function of HeaderParser | ||
43 | /** @param other : class to copy | ||
44 | */ | ||
45 | ✗ | void HeaderParser::copyHeaderParser(const HeaderParser & other){ | |
46 | ✗ | p_vecSource = other.p_vecSource; | |
47 | } | ||
48 | |||
49 | ///Parse the input file | ||
50 | /** @return true on success, false otherwise | ||
51 | */ | ||
52 | 3 | bool HeaderParser::parseFile(){ | |
53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if(!p_run) return false; |
54 | 3 | p_lastDocString = ""; | |
55 | 3 | p_otherCode = ""; | |
56 |
2/2✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
|
3 | p_currentSource.setName(p_parser->getFileName().getFileName()); |
57 | 3 | p_parser->skipWhiteSpace(); | |
58 | //To parse the file we need to read char by char until we get something we know | ||
59 |
5/6✓ Branch 1 taken 15 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 15 times.
✓ Branch 6 taken 3 times.
|
18 | while(!p_parser->isEndOfFile() && p_run){ //If we are not at the end of the file |
60 |
1/1✓ Branch 1 taken 15 times.
|
15 | PFunction function; |
61 |
2/3✓ Branch 1 taken 15 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
|
15 | if(parseDocString()){} |
62 |
3/8✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 15 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
15 | else if(p_parser->isMatch("//")){p_parser->getUntilKeyWithoutPatern("\n");} //Skip comment |
63 |
6/6✓ Branch 1 taken 15 times.
✓ Branch 4 taken 15 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 12 times.
✓ Branch 10 taken 3 times.
✓ Branch 13 taken 3 times.
|
15 | else if(p_parser->isMatch("/*")){p_parser->getUntilKeyWithoutPatern("*/");} //Skip comment |
64 |
3/3✓ Branch 1 taken 12 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 9 times.
|
12 | else if(parseMacro()){} |
65 |
2/3✓ Branch 1 taken 3 times.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
|
3 | else if(parseFunction(function)){ |
66 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | p_currentSource.getVecFunction().push_back(function); |
67 | }else{ | ||
68 | ✗ | incrementCurrentChar(); | |
69 | } | ||
70 |
1/1✓ Branch 1 taken 15 times.
|
15 | p_parser->skipWhiteSpace(); |
71 | 15 | } | |
72 | 3 | p_vecSource.push_back(p_currentSource); | |
73 | 3 | return true; | |
74 | } | ||
75 | |||
76 | ///Initialisation to be done just before loading a file | ||
77 | 3 | void HeaderParser::preLoadFile(){ | |
78 | //Save the current source | ||
79 |
1/1✓ Branch 2 taken 3 times.
|
3 | p_parser->setWhiteSpace(" \t\n"); |
80 |
1/1✓ Branch 2 taken 3 times.
|
3 | p_parser->setSeparator(",;{}()"); |
81 | 3 | } | |
82 | |||
83 | ///Initialisation to be done just after loading a file | ||
84 | 3 | void HeaderParser::postLoadFile(){ | |
85 | |||
86 | 3 | } | |
87 | |||
88 | ///Initialisation function of the class HeaderParser | ||
89 | 3 | void HeaderParser::initialisationHeaderParser(){ | |
90 | |||
91 | 3 | } | |
92 | |||
93 | ///Increment current char position | ||
94 | /** @param[out] textObj : obh to be used to store text | ||
95 | */ | ||
96 | ✗ | void HeaderParser::incrementCurrentChar(){ | |
97 | //If nothing is known I need to save the current char in the other code | ||
98 | ✗ | char ch = p_parser->getCurrentCh(); | |
99 | ✗ | if(ch == '~'){ch = ' ';} | |
100 | ✗ | p_otherCode += ch; | |
101 | ✗ | p_parser->getNextChar(); | |
102 | } | ||
103 | |||
104 | ///Add the other code parsed into the current PSource | ||
105 | 3 | void HeaderParser::playOtherCode(){ | |
106 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if(p_otherCode != ""){ |
107 | ✗ | PFunction other; | |
108 | ✗ | other.setOtherCode(p_otherCode); | |
109 | ✗ | p_currentSource.getVecFunction().push_back(other); | |
110 | ✗ | p_otherCode = ""; | |
111 | } | ||
112 | 3 | } | |
113 | |||
114 | ///Parse a macro | ||
115 | /** @return true on success, false otherwise | ||
116 | */ | ||
117 | 12 | bool HeaderParser::parseMacro(){ | |
118 |
4/4✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 9 times.
|
12 | if(!p_parser->isMatch("#")){return false;} |
119 |
5/5✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
✓ Branch 7 taken 9 times.
✓ Branch 10 taken 9 times.
✓ Branch 13 taken 9 times.
|
18 | PString macro("#" + p_parser->getUntilKeyWithoutPatern("\n") + "\n"); |
120 |
1/1✓ Branch 1 taken 9 times.
|
9 | PFunction func; |
121 |
1/1✓ Branch 1 taken 9 times.
|
9 | func.setOtherCode(macro); |
122 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
|
9 | p_currentSource.getVecFunction().push_back(func); |
123 | 9 | return true; | |
124 | 9 | } | |
125 | |||
126 | ///Parse a doc string | ||
127 | /** @return true on success, false otherwise | ||
128 | */ | ||
129 | 27 | bool HeaderParser::parseDocString(){ | |
130 |
2/3✓ Branch 2 taken 27 times.
✓ Branch 5 taken 27 times.
✗ Branch 6 not taken.
|
27 | if(!p_parser->isMatch("///")){return false;} |
131 | ✗ | p_lastDocString = p_parser->getUntilKeyWithoutPatern("\n"); | |
132 | |||
133 | ✗ | return true; | |
134 | } | ||
135 | |||
136 | ///Parse a PFunction | ||
137 | /** @param[out] function : PFunction to be parsed | ||
138 | * @return true on success, false otherwise | ||
139 | */ | ||
140 | 3 | bool HeaderParser::parseFunction(PFunction & function){ | |
141 |
1/1✓ Branch 1 taken 3 times.
|
3 | p_parser->pushPosition(); |
142 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
|
3 | PString templateDef(""), returnType(""); |
143 |
1/1✓ Branch 1 taken 3 times.
|
3 | parseTemplateDef(templateDef); |
144 |
1/1✓ Branch 1 taken 3 times.
|
3 | parseType(returnType); |
145 |
4/4✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 10 taken 3 times.
|
3 | std::cerr << "HeaderParser::parseFunction : returnType = '"<<returnType<<"'" << std::endl; |
146 |
1/1✓ Branch 1 taken 3 times.
|
3 | PString functionName(p_parser->getNextToken()); |
147 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if(functionName == ""){return false;} //This is not a function |
148 |
1/1✓ Branch 1 taken 3 times.
|
3 | function.setDocString(p_lastDocString); |
149 |
1/1✓ Branch 1 taken 3 times.
|
3 | p_lastDocString = ""; |
150 | //Let's get the arguments of the function | ||
151 |
3/4✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
|
3 | if(!p_parser->isMatch("(")){return false;} //This is not a function |
152 |
1/1✓ Branch 1 taken 3 times.
|
3 | function.setName(functionName); |
153 |
1/1✓ Branch 1 taken 3 times.
|
3 | function.setOutputType(returnType); |
154 |
10/15✓ Branch 1 taken 15 times.
✓ Branch 3 taken 15 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 15 times.
✓ Branch 9 taken 15 times.
✓ Branch 11 taken 12 times.
✓ Branch 12 taken 3 times.
✓ Branch 13 taken 12 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 15 times.
✗ Branch 16 not taken.
✓ Branch 18 taken 12 times.
✓ Branch 19 taken 3 times.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
|
15 | while(!p_parser->isEndOfFile() && !p_parser->isMatch(")") && p_run){ |
155 |
1/1✓ Branch 1 taken 12 times.
|
12 | PArgument arg; |
156 |
2/3✓ Branch 1 taken 12 times.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | if(parseDocString()){} |
157 |
3/8✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
12 | else if(p_parser->isMatch("//")){p_parser->getUntilKeyWithoutPatern("\n");} //Skip comment |
158 |
3/8✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
✗ Branch 10 not taken.
✗ Branch 11 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
|
12 | else if(p_parser->isMatch("/*")){p_parser->getUntilKeyWithoutPatern("*/");} //Skip comment |
159 |
2/3✓ Branch 1 taken 12 times.
✓ Branch 3 taken 12 times.
✗ Branch 4 not taken.
|
12 | else if(parseArgument(arg)){ |
160 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
|
12 | function.getVecArgument().push_back(arg); |
161 |
11/18✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 6 taken 3 times.
✓ Branch 7 taken 9 times.
✓ Branch 9 taken 3 times.
✓ Branch 12 taken 3 times.
✗ Branch 14 not taken.
✓ Branch 15 taken 3 times.
✓ Branch 16 taken 3 times.
✓ Branch 17 taken 9 times.
✓ Branch 19 taken 12 times.
✗ Branch 20 not taken.
✗ Branch 22 not taken.
✓ Branch 23 taken 12 times.
✗ Branch 24 not taken.
✗ Branch 25 not taken.
✗ Branch 27 not taken.
✗ Branch 28 not taken.
|
12 | if(!p_parser->isMatch(",") && !isMatchRewind(")")){ |
162 | ✗ | errorAt(); | |
163 | ✗ | std::cerr << "HeaderParser::parseFunction : expect ',' or ')' after function definition '"<<functionName<<"'" << std::endl; | |
164 | ✗ | return true; | |
165 | } | ||
166 | }else{ | ||
167 | ✗ | errorAt(); | |
168 | ✗ | std::cerr << "HeaderParser::parseFunction : cannot parse attribute" << std::endl; | |
169 | ✗ | return true; | |
170 | } | ||
171 |
1/1✓ Branch 1 taken 12 times.
|
12 | p_parser->skipWhiteSpace(); |
172 | |||
173 |
1/2✓ Branch 1 taken 12 times.
✗ Branch 2 not taken.
|
12 | } |
174 |
3/4✓ Branch 1 taken 3 times.
✓ Branch 4 taken 3 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 3 times.
|
3 | if(!p_parser->isMatch(";")){ |
175 | ✗ | errorAt(); | |
176 | ✗ | std::cerr << "HeaderParser::parseFunction : expect ';' after function definition '"<<functionName<<"'" << std::endl; | |
177 | ✗ | return true; | |
178 | } | ||
179 |
1/1✓ Branch 1 taken 3 times.
|
3 | playOtherCode(); |
180 | 3 | return true; | |
181 | |||
182 | 3 | } | |
183 | |||
184 | ///Parse a template definition | ||
185 | /** @param[out] templateDef : template definition | ||
186 | */ | ||
187 | 3 | void HeaderParser::parseTemplateDef(PString & templateDef){ | |
188 |
2/3✓ Branch 2 taken 3 times.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
|
3 | if(!p_parser->isMatchToken("template")){return;} |
189 | |||
190 | ✗ | if(!p_parser->isMatch("<")){ | |
191 | ✗ | errorAt(); | |
192 | ✗ | std::cerr << "HeaderParser::parseTemplateDef : expect '<' after 'template' token" << std::endl; | |
193 | ✗ | return; | |
194 | } | ||
195 | ✗ | templateDef += "template<"; | |
196 | ✗ | bool isTemplate(true); | |
197 | ✗ | while(!p_parser->isEndOfFile() && isTemplate && p_run){ | |
198 | ✗ | templateDef += p_parser->getNextToken(); //Some typename | |
199 | ✗ | templateDef += " "; | |
200 | ✗ | templateDef += p_parser->getNextToken(); //Some T | |
201 | ✗ | if(p_parser->isMatch(">")){isTemplate = false;} | |
202 | ✗ | else if(!p_parser->isMatch(",")){ | |
203 | ✗ | errorAt(); | |
204 | ✗ | std::cerr << "HeaderParser::parseTemplateDef : expect '>' or ',' after template definition '"<<templateDef<<"'" << std::endl; | |
205 | ✗ | return; | |
206 | } | ||
207 | } | ||
208 | ✗ | templateDef += ">"; | |
209 | } | ||
210 | |||
211 | ///Say if the given string is a pointer or a reference | ||
212 | /** @param str : string to be analysed | ||
213 | * @return true if the given string is a pointer or a reference, false otherwise | ||
214 | */ | ||
215 | 21 | bool isPtrRef(const PString & str){ | |
216 |
6/10✓ Branch 1 taken 21 times.
✓ Branch 4 taken 21 times.
✓ Branch 6 taken 21 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 9 times.
✓ Branch 10 taken 12 times.
✓ Branch 11 taken 21 times.
✗ Branch 12 not taken.
✗ Branch 14 not taken.
✗ Branch 15 not taken.
|
21 | return str.find("&*") || str == "__restrict__"; |
217 | } | ||
218 | |||
219 | ///Parse a PAttribute | ||
220 | /** @param[out] arg : PArgument to be parsed | ||
221 | * @return true on success, false otherwise | ||
222 | */ | ||
223 | 12 | bool HeaderParser::parseArgument(PArgument & arg){ | |
224 |
3/4✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
|
12 | if(p_parser->isMatchRewind(")")){return true;} |
225 |
1/1✓ Branch 1 taken 12 times.
|
12 | PString type(""); |
226 |
1/1✓ Branch 1 taken 12 times.
|
12 | parseType(type); |
227 | |||
228 |
3/3✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 7 taken 12 times.
|
12 | PString varName(""), ptrRef(""), tmpPtrRef(""); |
229 | |||
230 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
|
12 | tmpPtrRef = p_parser->getNextToken(); |
231 | |||
232 |
3/3✓ Branch 1 taken 12 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 3 times.
|
12 | if(isPtrRef(tmpPtrRef)){ |
233 | do{ | ||
234 |
1/1✓ Branch 1 taken 9 times.
|
9 | ptrRef += tmpPtrRef; |
235 |
2/2✓ Branch 1 taken 9 times.
✓ Branch 4 taken 9 times.
|
9 | tmpPtrRef = p_parser->getNextToken(); |
236 | // if(tmpPtrRef == "__restrict__"){ptrRef += " ";} | ||
237 | |||
238 |
2/3✓ Branch 1 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
|
9 | }while(isPtrRef(tmpPtrRef)); |
239 | |||
240 |
1/1✓ Branch 1 taken 9 times.
|
9 | varName = tmpPtrRef; |
241 | }else{ | ||
242 |
1/1✓ Branch 1 taken 3 times.
|
3 | ptrRef = tmpPtrRef; |
243 |
1/1✓ Branch 1 taken 3 times.
|
3 | varName = ptrRef; |
244 |
1/1✓ Branch 1 taken 3 times.
|
3 | ptrRef = ""; |
245 | } | ||
246 |
8/8✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 7 taken 12 times.
✓ Branch 10 taken 12 times.
✓ Branch 13 taken 12 times.
✓ Branch 16 taken 12 times.
✓ Branch 19 taken 12 times.
✓ Branch 22 taken 12 times.
|
12 | std::cerr << "HeaderParser::parseArgument : type = '"<<type<<", ptrRef = '"<<ptrRef<<"', varName = '"<<varName<<"'" << std::endl; |
247 |
1/1✓ Branch 1 taken 12 times.
|
12 | PString defaultValue(""); |
248 |
3/4✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 12 times.
|
12 | if(p_parser->isMatch("=")){ //Some default value |
249 | ✗ | defaultValue = p_parser->getNextToken(); | |
250 | //If double quote are not token, even default strings are single token | ||
251 | ✗ | p_parser->pushPosition(); | |
252 | ✗ | PString nexToken(p_parser->getNextToken()); | |
253 | ✗ | if(nexToken == "("){ //We call a function/constructor | |
254 | ✗ | defaultValue += p_parser->getUntilKeyWithoutPaternRecurse(")", "("); | |
255 | }else{ | ||
256 | ✗ | p_parser->popPosition(); //It was not a function/constructor call | |
257 | } | ||
258 | } | ||
259 |
1/1✓ Branch 1 taken 12 times.
|
12 | arg.setName(varName); |
260 |
1/1✓ Branch 1 taken 12 times.
|
12 | arg.setPtrRef(ptrRef); |
261 |
1/1✓ Branch 1 taken 12 times.
|
12 | arg.setType(type); |
262 |
1/1✓ Branch 1 taken 12 times.
|
12 | arg.setDefaultValue(defaultValue); |
263 | 12 | return true; | |
264 | 12 | } | |
265 | |||
266 | ///Parse a data type | ||
267 | /** @param[out] type : type to be parsed | ||
268 | */ | ||
269 | 15 | void HeaderParser::parseType(PString & type){ | |
270 |
1/1✓ Branch 1 taken 15 times.
|
15 | type = ""; |
271 |
1/1✓ Branch 1 taken 15 times.
|
15 | PString token(p_parser->getNextToken()); |
272 |
8/8✓ Branch 1 taken 21 times.
✓ Branch 2 taken 6 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 3 times.
✓ Branch 7 taken 3 times.
✓ Branch 8 taken 15 times.
✓ Branch 9 taken 12 times.
✓ Branch 10 taken 15 times.
|
27 | while(token == "const" || token == "unsigned" || token == "long"){ |
273 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
|
12 | type += token + " "; |
274 |
2/2✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
|
12 | token = p_parser->getNextToken(); |
275 | } | ||
276 |
1/1✓ Branch 1 taken 15 times.
|
15 | type += token; |
277 | 15 | } | |
278 | |||
279 | |||
280 | |||
281 |