Doxygen
Loading...
Searching...
No Matches
declinfo.l
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2015 by Dimitri van Heesch.
4 *
5 * Permission to use, copy, modify, and distribute this software and its
6 * documentation under the terms of the GNU General Public License is hereby
7 * granted. No representations are made about the suitability of this software
8 * for any purpose. It is provided "as is" without express or implied warranty.
9 * See the GNU General Public License for more details.
10 *
11 * Documents produced by Doxygen are derivative works derived from the
12 * input used in their production; they are not affected by this license.
13 *
14 */
15%option never-interactive
16%option prefix="declinfoYY"
17%option nounput
18%option noyywrap
19%option reentrant
20%option extra-type="struct declinfoYY_state *"
21%top{
22#include <stdint.h>
23// forward declare yyscan_t to improve type safety
24#define YY_TYPEDEF_YY_SCANNER_T
25#define yyguts_t declinfoYYguts_t
26struct yyguts_t;
yyguts_t * yyscan_t
Definition code.l:25
#define yyguts_t
Definition code.l:23
28}
29
30%{
31
32// own header
33#include "declinfo.h"
34
35// other includes
36#include "debug.h"
37#include "message.h"
38#include "types.h"
39#include "util.h"
40
41#define YY_NO_INPUT 1
42#define YY_NO_UNISTD_H 1
43#define YY_NEVER_INTERACTIVE 1
44
45/* -----------------------------------------------------------------
46 *
47 * statics
48 */
69
70[[maybe_unused]] static const char *stateToString(int state);
71
72static void addType(yyscan_t yyscanner);
73static void addTypeName(yyscan_t yyscanner);
74static int yyread(char *buf,int max_size, yyscan_t yyscanner);
75
76/* -----------------------------------------------------------------
77 */
78#undef YY_INPUT
79#define YY_INPUT(buf,result,max_size) result=yyread(buf,max_size,yyscanner);
80
81// otherwise the filename would be the name of the converted file (*.cpp instead of *.l)
82static inline const char *getLexerFILE() {return __FILE__;}
83#define LEX_NO_INPUT_FILENAME
84#include "doxygen_lex.h"
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
static int yyread(char *buf, int max_size, yyscan_t yyscanner)
Definition declinfo.l:313
static void addTypeName(yyscan_t yyscanner)
Definition declinfo.l:298
static const char * stateToString(int state)
static const char * getLexerFILE()
Definition declinfo.l:82
static void addType(yyscan_t yyscanner)
Definition declinfo.l:282
DString className
Definition declinfo.l:54
const char * inputString
Definition declinfo.l:51
DString funcTempList
Definition declinfo.l:56
bool funcTempListFound
Definition declinfo.l:63
DString classTempList
Definition declinfo.l:55
bool classTempListFound
Definition declinfo.l:62
DString exceptionString
Definition declinfo.l:64
DString scope
Definition declinfo.l:53
This file contains a number of basic enums and types.
A bunch of utility functions.
85%}
86
87B [ \t]
88Bopt {B}*
89ID ([$a-z_A-Z\x80-\xFF][$a-z_A-Z0-9\x80-\xFF]*)|(@[0-9]+)
90
91%x Start
92%x Template
93%x ReadArgs
94%x Operator
95%x DeclType
96%x ReadExceptions
97
98%%
99
100<Start>"operator"/({B}*"["{B}*"]")* { // operator rule must be before {ID} rule
101 yyextra->name += yytext;
102 BEGIN(Operator);
103 }
104<Start>{ID}({B}*"*")?{B}*"("{B}*"^"{B}*")"{B}*"(" { // Objective-C 2.0 block return type
105 if (!yyextra->insideObjC)
106 {
107 REJECT;
108 }
109 else
110 {
111 addType(yyscanner);
112 yyextra->type += yytext;
113 }
114 }
115<Start>{ID}{B}*"("{B}*{ID}{B}*")" { // Objective-C class categories
116 if (!yyextra->insideObjC)
117 {
118 REJECT;
119 }
120 else
121 {
122 yyextra->name += yytext;
123 }
124 }
125<Start>([~!]{B}*)?{ID}{B}*"["{B}*"]" { // PHP
126 if (!yyextra->insidePHP)
127 {
128 REJECT;
129 }
130 addTypeName(yyscanner);
131 yyextra->name += removeRedundantWhiteSpace(yytext);
132 }
DString removeRedundantWhiteSpace(const DString &s)
Definition util.cpp:427
133<Start>"anonymous_namespace{"[^}]+"}" { // anonymous namespace
134 if (!yyextra->scope.empty())
135 {
136 yyextra->scope+=DString("::")+yytext;
137 }
138 else
139 {
140 yyextra->scope = yytext;
141 }
142 }
143<Start>([~!]{B}*)?{ID}/({B}*"["{B}*"]")* { // the []'s are for Java,
144 // the / was add to deal with multi-
145 // dimensional C++ arrays like A[][15]
146 // the leading ~ is for a destructor
147 // the leading ! is for a C++/CLI finalizer (see bug 456475 and 635198)
148 addTypeName(yyscanner);
149 yyextra->name += removeRedundantWhiteSpace(yytext);
150 }
151<Start>{B}*"::"{B}* { // found a yyextra->scope specifier
152 if (!yyextra->scope.empty() && !yyextra->scope.endsWith("::"))
153 {
154 if (!yyextra->name.empty()) yyextra->scope+="::"+yyextra->name; // add yyextra->name to yyextra->scope
155 }
156 else
157 {
158 yyextra->scope = yyextra->name; // yyextra->scope becomes yyextra->name
159 }
160 yyextra->name.clear();
161 }
162<Start>{B}*":" { // Objective-C argument separator
163 yyextra->name+=yytext;
164 }
165<Start>[*&]+ {
166 addType(yyscanner);
167 yyextra->type+=yytext;
168 }
169<Start>{B}+ {
170 addType(yyscanner);
171 }
172<Start>{B}*"("({ID}"::")*{B}*[&*]({B}*("const"|"volatile"){B}+)? {
173 if (yyextra->insidePHP) REJECT;
174 if (yyextra->insidePython) REJECT;
175 addType(yyscanner);
176 DString text(yytext);
177 yyextra->type+=text.stripWhiteSpace();
178 }
179<Start>{B}*")" {
180 yyextra->type+=")";
181 }
182<Start>{B}*"const"/{B}+ |
183<Start>{B}*"consteval"/{B}+ |
184<Start>{B}*"constinit"/{B}+ |
185<Start>{B}*"constexpr"/{B}+ {
186 if (!yyextra->type.empty()) yyextra->type+=" ";
187 yyextra->type+=yytext;
188 }
189<Start>{B}*"decltype"/{B}*"(" {
190 yyextra->roundCount=0;
191 if (!yyextra->type.empty()) yyextra->type+=" ";
192 yyextra->type+="decltype";
193 BEGIN(DeclType);
194 }
195<DeclType>{B}*"(" {
196 ++yyextra->roundCount;
197 yyextra->type+="(";
198 }
199<DeclType>{B}*")" {
200 yyextra->type+=")";
201 if (--yyextra->roundCount == 0) {
202 BEGIN(Start);
203 }
204 }
205<DeclType>. {
206 yyextra->type+=yytext;
207 }
208<Start>{B}*"(" { // TODO: function pointers
209 yyextra->args+="(";
210 BEGIN(ReadArgs);
211 }
212<Start>{B}*"[" {
213 yyextra->args+="[";
214 BEGIN(ReadArgs);
215 }
216<Start>{B}*"<" {
217 yyextra->name+="<";
218 yyextra->sharpCount=0;
219 yyextra->roundCount=0;
220 BEGIN(Template);
221 }
222<Template>"<<" { yyextra->name+="<<"; }
223<Template>">>" { yyextra->name+=">>"; }
224<Template>"(" { yyextra->name+="(";
225 yyextra->roundCount++;
226 }
227<Template>")" { yyextra->name+=")";
228 if (yyextra->roundCount>0)
229 {
230 yyextra->roundCount--;
231 }
232 }
233<Template>"<" {
234 yyextra->name+="<";
235 if (yyextra->roundCount==0)
236 {
237 yyextra->sharpCount++;
238 }
239 }
240<Template>">" {
241 yyextra->name+=">";
242 if (yyextra->roundCount==0)
243 {
244 if (yyextra->sharpCount)
245 --yyextra->sharpCount;
246 else
247 {
248 BEGIN(Start);
249 }
250 }
251 }
252<Template>. {
253 yyextra->name+=*yytext;
254 }
255<Operator>{B}*"("{B}*")"{B}*"<>"{Bopt}/"(" {
256 yyextra->name+="() <>";
257 BEGIN(ReadArgs);
258 }
259<Operator>{B}*"("{B}*")"{Bopt}/"(" {
260 yyextra->name+="()";
261 BEGIN(ReadArgs);
262 }
263<Operator>[^(]*{B}*("<>"{B}*)?/"(" {
264 yyextra->name+=yytext;
265 BEGIN(ReadArgs);
266 }
267<ReadArgs>"throw"{B}*"(" {
268 yyextra->exceptionString="throw(";
269 BEGIN(ReadExceptions);
270 }
271<ReadArgs>. {
272 yyextra->args+=*yytext;
273 }
274<ReadExceptions>. {
275 yyextra->exceptionString+=*yytext;
276 }
277<*>.
278<*>\n
279
280%%
281
282static void addType(yyscan_t yyscanner)
283{
284 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
285 //printf("addType() yyextra->type='%s' yyextra->scope='%s' yyextra->name='%s'\n",
286 // qPrint(yyextra->type),qPrint(yyextra->scope),qPrint(yyextra->name));
287 if (yyextra->name.empty() && yyextra->scope.empty())
288 {
289 return;
290 }
291 if (!yyextra->type.empty()) yyextra->type+=" ";
292 if (!yyextra->scope.empty()) yyextra->type+=yyextra->scope+"::";
293 yyextra->type+=yyextra->name;
294 yyextra->scope.clear();
295 yyextra->name.clear();
296}
297
298static void addTypeName(yyscan_t yyscanner)
299{
300 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
301 //printf("addTypeName() yyextra->type='%s' yyextra->scope='%s' yyextra->name='%s'\n",
302 // qPrint(yyextra->type),qPrint(yyextra->scope),qPrint(yyextra->name));
303 if (yyextra->name.empty() ||
304 yyextra->name.at(yyextra->name.length()-1)==':') // end of Objective-C keyword => append to yyextra->name not yyextra->type
305 {
306 return;
307 }
308 if (!yyextra->type.empty()) yyextra->type+=' ';
309 yyextra->type+=yyextra->name;
310 yyextra->name.clear();
311}
312
313static int yyread(char *buf,int max_size, yyscan_t yyscanner)
314{
315 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
316 int c=0;
317 while( c < max_size && yyextra->inputString[yyextra->inputPosition] )
318 {
319 *buf = yyextra->inputString[yyextra->inputPosition++] ;
320 c++; buf++;
321 }
322 return c;
323}
324
325/*@ public interface------------------------------------------------------------
326 */
329
330void parseFuncDecl(const DString &decl,const SrcLangExt lang,DString &cl,DString &t,
331 DString &n,DString &a,DString &ftl,DString &exc)
332{
333 if (decl.empty())
334 {
335 return;
336 }
337 declinfoYYlex_init_extra(&g_declinfo_extra, &g_yyscanner);
338 struct yyguts_t *yyg = (struct yyguts_t*)g_yyscanner;
339
340#ifdef FLEX_DEBUG
341 declinfoYYset_debug(Debug::isFlagSet(Debug::Lex_declinfo)?1:0,g_yyscanner);
342#endif
343
344 DebugLex debugLex(Debug::Lex_declinfo,__FILE__, NULL);
345 yyextra->inputString = decl.data();
346 //printf("Input='%s'\n",yyextra->inputString);
347 yyextra->inputPosition = 0;
348 yyextra->classTempListFound = false;
349 yyextra->funcTempListFound = false;
350 yyextra->insideObjC = lang==SrcLangExt::ObjC;
351 yyextra->insidePHP = lang==SrcLangExt::PHP;
352 yyextra->insidePython = lang==SrcLangExt::Python;
353 yyextra->scope.clear();
354 yyextra->className.clear();
355 yyextra->classTempList.clear();
356 yyextra->funcTempList.clear();
357 yyextra->name.clear();
358 yyextra->type.clear();
359 yyextra->args.clear();
360 yyextra->exceptionString.clear();
361 // first we try to find the yyextra->type, yyextra->scope, yyextra->name and arguments
362 declinfoYYrestart( yyin, g_yyscanner );
363 BEGIN( Start );
364 declinfoYYlex(g_yyscanner);
365
366 //printf("yyextra->type='%s' class='%s' yyextra->name='%s' yyextra->args='%s'\n",
367 // qPrint(yyextra->type),qPrint(yyextra->scope),qPrint(yyextra->name),qPrint(yyextra->args));
368
369 if (size_t nb = yyextra->name.rfind('[') ; nb!=DString::npos && yyextra->args.empty()) // correct for [] in yyextra->name ambiguity (due to Java return yyextra->type allowing [])
370 {
371 yyextra->args.prepend(yyextra->name.mid(nb));
372 yyextra->name=yyextra->name.left(nb);
373 }
374
375 cl=yyextra->scope;
376 n=removeRedundantWhiteSpace(yyextra->name);
377 size_t il=n.find('<'), ir=n.rfind('>');
378 if (il!=DString::npos && ir!=DString::npos && n.at(il+1)!='=')
379 // prevent <=>
380 // TODO: handle cases like where n="operator<< <T>"
381 {
383 n=n.left(il);
384 }
385
386 //ctl=yyextra->classTempList.copy();
387 //ftl=yyextra->funcTempList.copy();
388 t=removeRedundantWhiteSpace(yyextra->type);
389 a=removeRedundantWhiteSpace(yyextra->args);
390 exc=removeRedundantWhiteSpace(yyextra->exceptionString);
391
392 size_t len = t.length();
393 if (!t.empty() && t.at(len-1)==')') // for function pointers
394 {
395 // check if `)` is from decltype(`
396 size_t pos = t.find("decltype(");
397 if (pos != DString::npos)
398 {
399 size_t cnt = 1;
400 for (size_t i = pos + 9; i < len; i++)
401 {
402 if (t.at(i) == '(') cnt++;
403 else if (t.at(i) == ')')
404 {
405 cnt--;
406 if (cnt == 0)
407 {
408 if (i != len-1)
409 {
410 a.prepend(")");
411 t=t.left(len-1);
412 }
413 }
414 }
415 }
416 }
417 else
418 {
419 a.prepend(")");
420 t=t.left(len-1);
421 }
422 }
423 //printf("yyextra->type='%s' class='%s' yyextra->name='%s' yyextra->args='%s'\n",
424 // qPrint(t),qPrint(cl),qPrint(n),qPrint(a));
425
426 declinfoYYlex_destroy(g_yyscanner);
427 return;
428}
429
430#if 0
431void dumpDecl(const char *s)
432{
433 DString yyextra->className;
434 DString classTNames;
435 DString yyextra->type;
436 DString yyextra->name;
437 DString yyextra->args;
438 DString funcTNames;
439 msg("-----------------------------------------\n");
440 parseFuncDecl(s,yyextra->className,classTNames,yyextra->type,yyextra->name,yyextra->args,funcTNames);
441 msg("yyextra->type='{}' class='{}' classTempl='{}' yyextra->name='{}' "
442 "funcTemplateNames='{}' yyextra->args='{}'\n",
443 yyextra->type,yyextra->className,classTNames,
444 yyextra->name,funcTNames,yyextra->args
445 );
446}
447
448// some test code
449int main()
450{
451 dumpDecl("A < T > :: Value * A < T > :: getValue < S > ( const A < T > & a )");
452 dumpDecl("const A<T>::Value* A<T>::getValue<S>(const A<T>&a)");
453 dumpDecl("func()");
454 dumpDecl("friend void bla<>()");
455 dumpDecl("yyextra->name< T > :: operator () (int bla)");
456 dumpDecl("yyextra->name< T > :: operator << (int bla)");
457 dumpDecl("yyextra->name< T > :: operator << <> (int bla)");
458 dumpDecl("yyextra->className::func()");
459 dumpDecl("void ( * yyextra->Name < T > :: bla ) ( int, char * )");
460}
461#endif
462
463#include "declinfo.l.h"
size_t rfind(char c, size_t pos=npos) const
Definition dstring.h:244
DString mid(size_t index, size_t len=npos) const
Definition dstring.h:318
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
static constexpr size_t npos
value used to indicate 'not found' or 'to the end of the string', matching std::string::npos
Definition dstring.h:178
char & at(size_t i)
Returns a reference to the character at index i.
Definition dstring.h:686
DString & prepend(const char *s)
Definition dstring.h:515
size_t find(char c, size_t pos=0) const
Definition dstring.h:239
DString left(size_t len) const
Definition dstring.h:306
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition dstring.h:157
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:151
@ Lex_declinfo
Definition debug.h:59
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:132
void parseFuncDecl(const DString &decl, const SrcLangExt lang, DString &clName, DString &type, DString &name, DString &args, DString &funcTempList, DString &exceptions)
Definition declinfo.l:330
static yyscan_t g_yyscanner
Definition declinfo.l:327
static struct declinfoYY_state g_declinfo_extra
Definition declinfo.l:328
int main(int argc, char **argv)
Definition main.cpp:28
#define msg(fmt,...)
Definition message.h:94
SrcLangExt
Definition types.h:207