Doxygen
Loading...
Searching...
No Matches
commentcnv.l
Go to the documentation of this file.
-1/*****************************************************************************
2 *
3 * Copyright (C) 1997-2023 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 */
13
14%{
15 // redefine buffer size (defaults to 16Kb) to avoid a 'flex scanner push-back overflow' error
16 #undef YY_BUF_SIZE
17 #define YY_BUF_SIZE 10*1024*1024 // 10Mb should be sufficient for a source file
18%}
20%option never-interactive
21%option prefix="commentcnvYY"
22%option reentrant
23%option extra-type="struct commentcnvYY_state *"
24%top{
25#include <stdint.h>
26// forward declare yyscan_t to improve type safety
27#define YY_TYPEDEF_YY_SCANNER_T
28struct yyguts_t;
29typedef yyguts_t *yyscan_t;
yyguts_t * yyscan_t
Definition code.l:24
30}
32%{
33
34
35#include <stdio.h>
36#include <stdlib.h>
37#include <stack>
38#include <algorithm>
39#include <deque>
40#include <string_view>
41
42#include "debug.h"
43#include "message.h"
44#include "config.h"
45#include "doxygen.h"
46#include "util.h"
47#include "aliases.h"
48#include "condparser.h"
49#include "fileinfo.h"
50#include "stringutil.h"
51#include "regex.h"
52#include "section.h"
53
54#include <assert.h>
55
56#define YY_NO_INPUT 1
57#define YY_NO_UNISTD_H 1
60{
61 commentcnvYY_CondCtx(int line,const QCString &id,bool b)
62 : lineNr(line),sectionId(id), skip(b) {}
65 bool skip;
66};
68struct CommentCtx
69{
70 CommentCtx(int line)
71 : lineNr(line) {}
72 int lineNr;
73};
76{
93{
94 commentcnvYY_state(const std::string *i,std::string &o) : inBuf(i), outBuf(o) {}
95 const std::string *inBuf;
96 std::string &outBuf;
97 int inBufPos = 0;
98 int col = 0;
99 int blockHeadCol = 0; // column at which the start of a special comment block was found
100 int insertCommentCol = 0; // column at which an include or snippet command was found
102 int readLineCtx = 0;
103 int includeCtx = 0;
104 int raiseLevel = 0;
108 bool skip = FALSE;
110 int lineNr = 0;
111 int condCtx = 0;
112 std::stack<commentcnvYY_CondCtx> condStack;
113 std::stack<int> commentStack;
119 int charContext = 0;
120 int javaBlock = 0;
122 bool inVerbatim = false;
125 bool firstIncludeLine = false;
129 int blockCount = 0;
135 bool vhdl = FALSE; // for VHDL old style --! comment
136 SrcLangExt lang = SrcLangExt::Unknown;
137 bool isFixedForm = FALSE; // For Fortran
138 std::deque<std::unique_ptr<commentcnv_FileState>> includeStack;
139 std::vector<std::string> expandedAliases;
145[[maybe_unused]] static const char *stateToString(int state);
146static inline int computeIndent(const char *s);
148static void replaceCommentMarker(yyscan_t yyscanner,std::string_view s);
149static inline void copyToOutput(yyscan_t yyscanner,std::string_view s);
150static inline void copyToOutput(yyscan_t yyscanner,const char *s,int len);
151static void startCondSection(yyscan_t yyscanner,const QCString &sectId);
152static void endCondSection(yyscan_t yyscanner);
153static void handleCondSectionId(yyscan_t yyscanner,const char *expression);
154static void replaceAliases(yyscan_t yyscanner,std::string_view s,bool replaceCppComment=false,bool replaceCComment=false);
155static int yyread(yyscan_t yyscanner,char *buf,int max_size);
156static void replaceComment(yyscan_t yyscanner,int offset);
157static void clearCommentStack(yyscan_t yyscanner);
158static bool readIncludeFile(yyscan_t yyscanner,const QCString &inc,const QCString &blockId);
159static void insertCommentStart(yyscan_t yyscanner);
160static bool parseIncludeOptions(yyscan_t yyscanner,std::string_view s);
161
162#undef YY_INPUT
163#define YY_INPUT(buf,result,max_size) result=yyread(yyscanner,buf,max_size);
164
165// otherwise the filename would be the name of the converted file (*.cpp instead of *.l)
166static inline const char *getLexerFILE() {return __FILE__;}
167#include "doxygen_lex.h"
This is an alternative implementation of QCString.
Definition qcstring.h:101
static int yyread(yyscan_t yyscanner, char *buf, int max_size)
Definition code.l:3971
static const char * stateToString(int state)
static const char * getLexerFILE()
Definition code.l:263
static void startCondSection(yyscan_t yyscanner, const QCString &sectId)
static void copyToOutput(yyscan_t yyscanner, std::string_view s)
static int computeIndent(const char *s)
static void handleCondSectionId(yyscan_t yyscanner, const char *expression)
static void endCondSection(yyscan_t yyscanner)
static void replaceComment(yyscan_t yyscanner, int offset)
static void insertCommentStart(yyscan_t yyscanner)
static void replaceAliases(yyscan_t yyscanner, std::string_view s, bool replaceCppComment=false, bool replaceCComment=false)
copies string s with length len to the output, while replacing any alias commands found in the string...
static void replaceCommentMarker(yyscan_t yyscanner, std::string_view s)
static void clearCommentStack(yyscan_t yyscanner)
static bool readIncludeFile(yyscan_t yyscanner, const QCString &inc, const QCString &blockId)
static bool parseIncludeOptions(yyscan_t yyscanner, std::string_view s)
Token literal values and constants.
Definition CharStream.h:12
#define FALSE
Definition qcstring.h:34
Some helper functions for std::string.
CommentCtx(int line)
Definition commentcnv.l:72
std::string fileBuf
Definition commentcnv.l:86
YY_BUFFER_STATE bufState
Definition commentcnv.l:80
const std::string * oldFileBuf
Definition commentcnv.l:87
commentcnvYY_CondCtx(int line, const QCString &id, bool b)
Definition commentcnv.l:63
std::string & outBuf
Definition commentcnv.l:98
std::deque< std::unique_ptr< commentcnv_FileState > > includeStack
Definition commentcnv.l:140
QCString snippetFileName
Definition commentcnv.l:142
std::vector< std::string > expandedAliases
Definition commentcnv.l:141
std::stack< commentcnvYY_CondCtx > condStack
Definition commentcnv.l:114
const std::string * inBuf
Definition commentcnv.l:97
commentcnvYY_state(const std::string *i, std::string &o)
Definition commentcnv.l:96
std::stack< int > commentStack
Definition commentcnv.l:115
SrcLangExt
Definition types.h:207
A bunch of utility functions.
169%}
170
171MAILADDR ("mailto:")?[a-z_A-Z0-9\x80-\xff.+-]+"@"[a-z_A-Z0-9\x80-\xff-]+("."[a-z_A-Z0-9\x80-\xff\-]+)+[a-z_A-Z0-9\x80-\xff\-]+
172
173%option noyywrap
174
175%x Scan
176%x SkipString
177%x SkipVerbString
178%x SkipChar
179%x SkipLang
180%x SComment
181%x CComment
182%x CNComment
183%x Verbatim
184%x VerbatimCode
185%x ReadLine
186%x CopyLine
187%x CondLine
188%x ReadAliasArgs
189%x IncludeDoc
190%x SnippetDoc
191%x SnippetDocTag
192%x RawString
193%x IncludeFile
194
195CMD [\\@]
196 //- start: NUMBER -------------------------------------------------------------------------
197 // Note same defines in code.l: keep in sync
198DECIMAL_INTEGER [1-9][0-9']*[0-9]?[uU]?[lL]?[lL]?
199HEXADECIMAL_INTEGER "0"[xX][0-9a-zA-Z']+[0-9a-zA-Z]?
200OCTAL_INTEGER "0"[0-7][0-7']+[0-7]?
201BINARY_INTEGER "0"[bB][01][01']*[01]?
202INTEGER_NUMBER {DECIMAL_INTEGER}|{HEXADECIMAL_INTEGER}|{OCTAL_INTEGER}|{BINARY_INTEGER}
203
204FP_SUF [fFlL]
205
206DIGIT_SEQ [0-9][0-9']*[0-9]?
207FRAC_CONST {DIGIT_SEQ}"."|{DIGIT_SEQ}?"."{DIGIT_SEQ}
208FP_EXP [eE][+-]?{DIGIT_SEQ}
209DEC_FP1 {FRAC_CONST}{FP_EXP}?{FP_SUF}?
210DEC_FP2 {DIGIT_SEQ}{FP_EXP}{FP_SUF}
211
212HEX_DIGIT_SEQ [0-9a-fA-F][0-9a-fA-F']*[0-9a-fA-F]?
213HEX_FRAC_CONST {HEX_DIGIT_SEQ}"."|{HEX_DIGIT_SEQ}?"."{HEX_DIGIT_SEQ}
214BIN_EXP [pP][+-]?{DIGIT_SEQ}
215HEX_FP1 "0"[xX]{HEX_FRAC_CONST}{BIN_EXP}{FP_SUF}?
216HEX_FP2 "0"[xX]{HEX_DIGIT_SEQ}{BIN_EXP}{FP_SUF}?
217
218FLOAT_DECIMAL {DEC_FP1}|{DEC_FP2}
219FLOAT_HEXADECIMAL {HEX_FP1}|{HEX_FP2}
220FLOAT_NUMBER {FLOAT_DECIMAL}|{FLOAT_HEXADECIMAL}
221NUMBER {INTEGER_NUMBER}|{FLOAT_NUMBER}
222
223RAWBEGIN (u|U|L|u8)?R\"[^ \t\‍(\‍)\\‍]{0,16}"("
224RAWEND ")"[^ \t\‍(\‍)\\‍]{0,16}\"
225
226FILEICHAR [a-z_A-Z0-9\x80-\xFF\\:\\\/\-\+=&#@]
227FILEECHAR [a-z_A-Z0-9\x80-\xFF\-\+=&#@]
228FILECHARS {FILEICHAR}*{FILEECHAR}+
229HFILEMASK {FILEICHAR}*("."{FILEICHAR}+)+{FILECHARS}*
230VFILEMASK {FILECHARS}("."{FILECHARS})*
231FILEMASK {VFILEMASK}|{HFILEMASK}
232
233B [ \t]
234
235OPTS "{"[^}]*"}"{B}*
236
237 //- end: NUMBER ---------------------------------------------------------------------------
238
239 // C start comment
240CCS "/\*"
241 // C end comment
242CCE "*\/"
243 // Cpp comment
244CPPC "/\/"
245
246 // Optional any character
247ANYopt .*
248 // Optional white space
249WSopt [ \t\r]*
250 // readline non special
251RLopt [^\\@<\n\*\/`]*
252RL [^\\@<\n\*\/`]+
253 // Optional slash
254SLASHopt [/]*
255
256%%
257
258<Scan>{NUMBER} { //Note similar code in code.l
259 if (yyextra->lang!=SrcLangExt::Cpp) REJECT;
260 copyToOutput(yyscanner,yytext,yyleng);
261 }
262<Scan>{RAWBEGIN} {
263 if (yyextra->lang!=SrcLangExt::Cpp) REJECT;
264 copyToOutput(yyscanner,yytext,yyleng);
265 BEGIN(RawString);
266 }
267<Scan>[^"'!\/\n\\#,\-=; \t@$]* { /* eat anything that is not " / , or \n */
268 copyToOutput(yyscanner,yytext,yyleng);
269 }
270<Scan>[,= ;\t] { /* eat , so we have a nice separator in long initialization lines */
271 copyToOutput(yyscanner,yytext,yyleng);
272 }
273<Scan>"'''"! |
274<Scan>"\"\"\""! { /* start of python long comment */
275 if (yyextra->lang!=SrcLangExt::Python)
276 {
277 REJECT;
278 }
279 else
280 {
281 yyextra->pythonDocString = TRUE;
282 yyextra->pythonDocStringChar = yytext[0];
283 yyextra->nestingCount=1;
284 clearCommentStack(yyscanner); /* to be on the save side */
285 copyToOutput(yyscanner,yytext,yyleng);
286 BEGIN(CComment);
287 yyextra->commentStack.push(yyextra->lineNr);
288 }
289 }
#define TRUE
Definition qcstring.h:37
290<Scan>"'''" |
291<Scan>"\"\"\"" { /* start of python long comment */
292 if (yyextra->lang!=SrcLangExt::Python)
293 {
294 REJECT;
295 }
296 else if (Config_getBool(PYTHON_DOCSTRING))
297 {
298 REJECT;
299 }
300 else
301 { /* handle as if """! */
302 yyextra->pythonDocString = TRUE;
303 yyextra->pythonDocStringChar = yytext[0];
304 yyextra->nestingCount=1;
305 clearCommentStack(yyscanner); /* to be on the save side */
306 copyToOutput(yyscanner,yytext,yyleng);
307 BEGIN(CComment);
308 yyextra->commentStack.push(yyextra->lineNr);
309 }
310 }
#define Config_getBool(name)
Definition config.h:33
311<Scan>{B}*![><!]/.*\n {
312 if (yyextra->lang!=SrcLangExt::Fortran)
313 {
314 REJECT;
315 }
316 else
317 {
318 yyextra->nestingCount=0; // Fortran doesn't have an end comment
319 clearCommentStack(yyscanner); /* to be on the save side */
320 yyextra->specialComment=true;
321 copyToOutput(yyscanner,yytext,yyleng);
322 yyextra->blockHeadCol=yyextra->col-2;
323 BEGIN(CComment);
324 yyextra->commentStack.push(yyextra->lineNr);
325 }
326 }
327<Scan>[Cc\*][><!]/.*\n {
328 if (yyextra->lang!=SrcLangExt::Fortran)
329 {
330 REJECT;
331 }
332 else
333 {
334 /* check for fixed format; we might have some conditional as part of multiline if like C<5 .and. & */
335 if (yyextra->isFixedForm && (yyextra->col == 0))
336 {
337 yyextra->nestingCount=0; // Fortran doesn't have an end comment
338 clearCommentStack(yyscanner); /* to be on the save side */
339 yyextra->specialComment=true;
340 copyToOutput(yyscanner,yytext,yyleng);
341 yyextra->blockHeadCol=yyextra->col-1;
342 BEGIN(CComment);
343 yyextra->commentStack.push(yyextra->lineNr);
344 }
345 else
346 {
347 REJECT;
348 }
349 }
350 }
351<Scan>!.*\n {
352 if (yyextra->lang!=SrcLangExt::Fortran)
353 {
354 REJECT;
355 }
356 else
357 {
358 copyToOutput(yyscanner,yytext,yyleng);
359 }
360 }
361<Scan>[Cc\*].*\n {
362 if (yyextra->lang!=SrcLangExt::Fortran)
363 {
364 REJECT;
365 }
366 else
367 {
368 if (yyextra->col == 0)
369 {
370 copyToOutput(yyscanner,yytext,yyleng);
371 }
372 else
373 {
374 REJECT;
375 }
376 }
377 }
378<Scan>[$]?"@\"" { /* start of an interpolated verbatim C# string */
379 if (yyextra->lang!=SrcLangExt::CSharp) REJECT
380 copyToOutput(yyscanner,yytext,yyleng);
381 yyextra->stringContext = YY_START;
382 BEGIN(SkipVerbString);
383 }
384<Scan>"\"" { /* start of a string */
385 copyToOutput(yyscanner,yytext,yyleng);
386 yyextra->stringContext = YY_START;
387 BEGIN(SkipString);
388 }
389<Scan>' {
390 copyToOutput(yyscanner,yytext,yyleng);
391 yyextra->charContext = YY_START;
392 if (yyextra->lang!=SrcLangExt::VHDL)
393 {
394 BEGIN(SkipChar);
395 }
396 }
397<Scan>\n { /* new line */
398 copyToOutput(yyscanner,yytext,yyleng);
399 }
400<Scan>{CPPC}[!/]/.*\n[ \t]*{CPPC}[!/][ \t]*{CMD}"}" { // see bug #8731, don't treat multiline C++ comment as detailed description
401 // if the next line is an end of group marker.
402 yyextra->inSpecialComment=true;
403 yyextra->blockHeadCol=yyextra->col+1;
404 yyextra->insertCppCommentMarker=true;
405 copyToOutput(yyscanner,yytext,yyleng);
406 yyextra->readLineCtx=YY_START;
407 BEGIN(ReadLine);
408 }
409<Scan>{CPPC}"!"/.*\n[ \t]*{CPPC}[\/!][^\/] | /* start C++ style special comment block */
410<Scan>({CPPC}"/"[/]*)/[^/].*\n[ \t]*{CPPC}[\/!][^\/] { /* start C++ style special comment block */
411 if (yyextra->mlBrief)
412 {
413 REJECT; // bail out if we do not need to convert
414 }
415 else
416 {
417 int i=3;
418 if (yytext[2]=='/')
419 {
420 while (i<(int)yyleng && yytext[i]=='/') i++;
421 }
422 yyextra->blockHeadCol=yyextra->col+1;
423 if (yytext[2] == '!')
424 {
425 copyToOutput(yyscanner,"/*!");
426 }
427 else
428 {
429 copyToOutput(yyscanner,"/**");
430 }
431 if (i<yyleng) replaceAliases(yyscanner,yytext+i);
432 yyextra->inSpecialComment=TRUE;
433 //BEGIN(SComment);
434 yyextra->readLineCtx=SComment;
435 BEGIN(ReadLine);
436 }
437 }
438<Scan>{CPPC}"##Documentation"{ANYopt}/\n { /* Start of Rational Rose ANSI C++ comment block */
439 if (yyextra->mlBrief) REJECT;
440 int i=17; //=strlen("//##Documentation");
441 yyextra->blockHeadCol=yyextra->col+1;
442 copyToOutput(yyscanner,"/**");
443 if (i<yyleng) replaceAliases(yyscanner,yytext+i);
444 yyextra->inRoseComment=TRUE;
445 BEGIN(SComment);
446 }
447<Scan>{CPPC}[!\/]/.*\n[ \t]*{CPPC}[|\/][ \t]*{CMD}"}" { // next line contains an end marker, see bug 752712
448 yyextra->inSpecialComment=yytext[2]=='/' || yytext[2]=='!';
449 if (yyextra->inSpecialComment)
450 {
451 yyextra->blockHeadCol=yyextra->col+1;
452 }
453 copyToOutput(yyscanner,yytext,yyleng);
454 yyextra->readLineCtx=YY_START;
455 BEGIN(ReadLine);
456 }
457<Scan>{CPPC}[!/]/.*\n { /* one line special C++ comment */
458 yyextra->inSpecialComment=true;
459 yyextra->blockHeadCol=yyextra->col+1;
460 yyextra->insertCppCommentMarker=true;
461 copyToOutput(yyscanner,yytext,yyleng);
462 yyextra->readLineCtx=YY_START;
463 BEGIN(ReadLine);
464 }
465<Scan>{CPPC}/.*\n { /* one line normal C++ comment */
466 yyextra->inSpecialComment=false;
467 copyToOutput(yyscanner,yytext,yyleng);
468 yyextra->readLineCtx=YY_START;
469 BEGIN(CopyLine);
470 }
471<Scan>{CCS}{CCE} { /* avoid matching next rule for empty C comment, see bug 711723 */
472 copyToOutput(yyscanner,yytext,yyleng);
473 }
474<Scan>{CCS}[*!]? { /* start of a C comment */
475 if (yyextra->lang==SrcLangExt::Python)
476 {
477 REJECT;
478 }
479 yyextra->specialComment=(int)yyleng==3;
480 yyextra->nestingCount=1;
481 clearCommentStack(yyscanner); /* to be on the save side */
482 copyToOutput(yyscanner,yytext,yyleng);
483 if (yyextra->specialComment)
484 {
485 yyextra->blockHeadCol=0;
486 BEGIN(CComment);
487 }
488 else
489 {
490 BEGIN(CNComment);
491 }
492 yyextra->commentStack.push(yyextra->lineNr);
493 }
494<Scan>"#"[^\n]*\n {
495 if (yyextra->lang!=SrcLangExt::PHP)
496 {
497 REJECT;
498 }
499 copyToOutput(yyscanner,yytext,yyleng);
500 }
501<Scan>"#"("#")? {
502 if (yyextra->lang!=SrcLangExt::Python)
503 {
504 REJECT;
505 }
506 else
507 {
508 yyextra->nestingCount=0; // Python doesn't have an end comment for #
509 clearCommentStack(yyscanner); /* to be on the save side */
510 yyextra->specialComment=(int)yyleng==2;
511 if (yyextra->specialComment)
512 {
513 yyextra->blockHeadCol=yyextra->col;
514 }
515 yyextra->commentStack.push(yyextra->lineNr);
516 copyToOutput(yyscanner,yytext,yyleng);
517 BEGIN(CComment);
518 }
519 }
520<Scan>"--"[^!][^\n]* {
521 if (yyextra->lang!=SrcLangExt::VHDL)
522 {
523 REJECT;
524 }
525 else
526 {
527 copyToOutput(yyscanner,yytext,yyleng);
528 }
529 }
530<Scan>"--!" {
531 if (yyextra->lang!=SrcLangExt::VHDL)
532 {
533 REJECT;
534 }
535 else
536 {
537 yyextra->specialComment=true;
538 yyextra->blockHeadCol=yyextra->col;
539 yyextra->vhdl = TRUE;
540 yyextra->nestingCount=0; // VHDL doesn't have an end comment
541 clearCommentStack(yyscanner); /* to be on the save side */
542 yyextra->commentStack.push(yyextra->lineNr);
543 copyToOutput(yyscanner,yytext,yyleng);
544 BEGIN(CComment);
545 }
546 }
547<Scan>{B}*![><!] {
548 if (yyextra->lang!=SrcLangExt::Fortran)
549 {
550 REJECT;
551 }
552 else
553 {
554 yyextra->nestingCount=0; // Fortran doesn't have an end comment
555 clearCommentStack(yyscanner); /* to be on the save side */
556 yyextra->specialComment=true;
557 yyextra->blockHeadCol=yyextra->col;
558 yyextra->commentStack.push(yyextra->lineNr);
559 copyToOutput(yyscanner,yytext,yyleng);
560 BEGIN(CComment);
561 }
562 }
563<RawString>{RAWEND} {
564 copyToOutput(yyscanner,yytext,yyleng);
565 if (extractEndRawStringDelimiter(yytext)==yyextra->delimiter)
566 {
567 BEGIN(Scan);
568 }
569 }
QCString extractEndRawStringDelimiter(const char *rawEnd)
Definition util.cpp:6892
570<RawString>[^)\n]+ { copyToOutput(yyscanner,yytext,yyleng); }
571<RawString>\n { copyToOutput(yyscanner,yytext,yyleng); }
572<RawString>. { copyToOutput(yyscanner,yytext,yyleng); }
573
574<CComment,CNComment,ReadLine,IncludeFile>{MAILADDR} |
575<CComment,CNComment,ReadLine,IncludeFile>"<"{MAILADDR}">" { // Mail address, to prevent seeing e.g x@code-factory.org as start of a code block
576 copyToOutput(yyscanner,yytext,yyleng);
577 }
578<CComment,IncludeFile>"{"[ \t]*"@code"/[ \t\n] {
579 copyToOutput(yyscanner,"@iliteral{code}");
580 yyextra->lastCommentContext = YY_START;
581 yyextra->javaBlock=1;
582 yyextra->blockName=QCString("end")+&yytext[1];
583 yyextra->inVerbatim=true;
584 yyextra->verbatimLine=yyextra->lineNr;
585 BEGIN(VerbatimCode);
586 }
587<CComment,IncludeFile>"{"[ \t]*"@literal"/[ \t\n] {
588 copyToOutput(yyscanner,"@iliteral");
589 yyextra->lastCommentContext = YY_START;
590 yyextra->javaBlock=1;
591 yyextra->blockName=QCString("end")+&yytext[1];
592 yyextra->inVerbatim=true;
593 yyextra->verbatimLine=yyextra->lineNr;
594 BEGIN(VerbatimCode);
595 }
596<CComment,ReadLine,IncludeFile>{CMD}"ilinebr"[ \t]+("```"[`]*|"~~~"[~]*) { /* start of markdown code block */
597 if (!Config_getBool(MARKDOWN_SUPPORT))
598 {
599 REJECT;
600 }
601 copyToOutput(yyscanner,yytext,yyleng);
602 yyextra->lastCommentContext = YY_START;
603 yyextra->javaBlock=0;
604 yyextra->blockName=QCString(yytext).stripWhiteSpace().right(3); // take the ``` or ~~~ part
605 yyextra->inVerbatim=true;
606 yyextra->verbatimLine=yyextra->lineNr;
607 BEGIN(VerbatimCode);
608 }
QCString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition qcstring.h:260
QCString right(size_t len) const
Definition qcstring.h:234
609<CComment,ReadLine,IncludeFile>^[ \t]*("```"[`]*|"~~~"[~]*) { /* start of markdown code block */
610 if (!Config_getBool(MARKDOWN_SUPPORT))
611 {
612 REJECT;
613 }
614 copyToOutput(yyscanner,yytext,yyleng);
615 yyextra->lastCommentContext = YY_START;
616 yyextra->javaBlock=0;
617 yyextra->blockName=QCString(yytext).stripWhiteSpace().left(3); // take the ``` or ~~~ part
618 yyextra->inVerbatim=true;
619 yyextra->verbatimLine=yyextra->lineNr;
620 BEGIN(VerbatimCode);
621 }
QCString left(size_t len) const
Definition qcstring.h:229
622<CComment,ReadLine,IncludeFile>{CMD}("dot"|"code"|"msc"|"startuml")/[^a-z_A-Z0-9] { /* start of a verbatim block */
623 copyToOutput(yyscanner,yytext,yyleng);
624 yyextra->lastCommentContext = YY_START;
625 yyextra->javaBlock=0;
626 if (qstrcmp(&yytext[1],"startuml")==0)
627 {
628 yyextra->blockName="enduml";
629 }
630 else
631 {
632 yyextra->blockName=QCString("end")+&yytext[1];
633 }
634 yyextra->inVerbatim=true;
635 yyextra->verbatimLine=yyextra->lineNr;
636 BEGIN(VerbatimCode);
637 }
int qstrcmp(const char *str1, const char *str2)
Definition qcstring.h:69
638<CComment,ReadLine,IncludeFile>"\\`" {
639 copyToOutput(yyscanner,yytext,yyleng);
640 }
641<CComment,ReadLine,IncludeFile>"```" { // skip ``` if not at the start of the line
642 copyToOutput(yyscanner,yytext,yyleng);
643 }
644<CComment,ReadLine,IncludeFile>"`"{1,2} {
645 if (!Config_getBool(MARKDOWN_SUPPORT))
646 {
647 REJECT;
648 }
649 copyToOutput(yyscanner,yytext,yyleng);
650 yyextra->lastCommentContext = YY_START;
651 yyextra->javaBlock=0;
652 yyextra->blockName=yytext;
653 yyextra->inVerbatim=true;
654 yyextra->verbatimLine=yyextra->lineNr;
655 BEGIN(VerbatimCode);
656 }
657<CComment,ReadLine,IncludeFile>{CMD}("f$"|"f["|"f{"|"f(") {
658 copyToOutput(yyscanner,yytext,yyleng);
659 yyextra->blockName=&yytext[1];
660 if (yyextra->blockName.at(1)=='[')
661 {
662 yyextra->blockName.at(1)=']';
663 }
664 else if (yyextra->blockName.at(1)=='{')
665 {
666 yyextra->blockName.at(1)='}';
667 }
668 else if (yyextra->blockName.at(1)=='(')
669 {
670 yyextra->blockName.at(1)=')';
671 }
672 yyextra->lastCommentContext = YY_START;
673 yyextra->inVerbatim=true;
674 yyextra->verbatimLine=yyextra->lineNr;
675 BEGIN(Verbatim);
676 }
677<CComment,ReadLine,IncludeFile>"<!--!" { /* HTML comment doxygen command*/
678 if (yyextra->inVerbatim) REJECT;
679 //copyToOutput(yyscanner," ",5);
680 yyextra->inHtmlDoxygenCommand=true;
681 }
682<CComment,ReadLine,IncludeFile>"-->" { /* potential end HTML comment doxygen command*/
683 if (yyextra->inHtmlDoxygenCommand)
684 {
685 yyextra->inHtmlDoxygenCommand=false;
686 }
687 else
688 {
689 copyToOutput(yyscanner,yytext,yyleng);
690 }
691 }
692<CComment,ReadLine,IncludeFile>"<!--" { /* HTML comment */
693 copyToOutput(yyscanner,yytext,yyleng);
694 yyextra->blockName="-->";
695 yyextra->lastCommentContext = YY_START;
696 yyextra->inVerbatim=true;
697 yyextra->verbatimLine=yyextra->lineNr;
698 BEGIN(Verbatim);
699 }
700<CComment,ReadLine,IncludeFile>{CMD}("verbatim"|"iliteral"|"latexonly"|"htmlonly"|"xmlonly"|"docbookonly"|"rtfonly"|"manonly")/[^a-z_A-Z0-9] { /* start of a verbatim block */
701 copyToOutput(yyscanner,yytext,yyleng);
702 yyextra->blockName=QCString("end")+&yytext[1];
703 yyextra->lastCommentContext = YY_START;
704 yyextra->inVerbatim=true;
705 yyextra->verbatimLine=yyextra->lineNr;
706 BEGIN(Verbatim);
707 }
708<Scan>"\\\"" { /* escaped double quote */
709 copyToOutput(yyscanner,yytext,yyleng);
710 }
711<Scan>"\\\\" { /* escaped backslash */
712 copyToOutput(yyscanner,yytext,yyleng);
713 }
714<Scan>. { /* any other character */
715 copyToOutput(yyscanner,yytext,yyleng);
716 }
717<Verbatim>{CMD}("endverbatim"|"endiliteral"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"enddocbookonly"|"endrtfonly"|"endmanonly"|"f$"|"f]"|"f}"|"f)") { /* end of verbatim block */
718 copyToOutput(yyscanner,yytext,yyleng);
719 if (&yytext[1]==yyextra->blockName) // end of command or formula
720 {
721 yyextra->inVerbatim=false;
722 BEGIN(yyextra->lastCommentContext);
723 }
724 }
725<Verbatim>"-->" {
726 copyToOutput(yyscanner,yytext,yyleng);
727 if (yytext==yyextra->blockName)
728 {
729 yyextra->inVerbatim=false;
730 BEGIN(yyextra->lastCommentContext);
731 }
732 }
733<VerbatimCode>"{" {
734 if (yyextra->javaBlock==0)
735 {
736 REJECT;
737 }
738 else
739 {
740 yyextra->javaBlock++;
741 copyToOutput(yyscanner,yytext,yyleng);
742 }
743 }
744<VerbatimCode>"}" {
745 if (yyextra->javaBlock==0)
746 {
747 REJECT;
748 }
749 else
750 {
751 yyextra->javaBlock--;
752 if (yyextra->javaBlock==0)
753 {
754 copyToOutput(yyscanner," @endiliteral ");
755 yyextra->inVerbatim=false;
756 BEGIN(yyextra->lastCommentContext);
757 }
758 else
759 {
760 copyToOutput(yyscanner,yytext,yyleng);
761 }
762 }
763 }
764<VerbatimCode>("```"[`]*|"~~~"[~]*) { /* end of markdown code block */
765 if (yytext[0]=='`' && (yyextra->blockName=="`" || yyextra->blockName=="``")) // ``` type block inside single or double backtick "block"
766 {
767 // copy the end block marker
768 copyToOutput(yyscanner,yytext,yyleng);
769 }
770 else
771 {
772 copyToOutput(yyscanner,yytext,yyleng);
773 if (yytext[0]==yyextra->blockName[0])
774 {
775 yyextra->inVerbatim=false;
776 BEGIN(yyextra->lastCommentContext);
777 }
778 }
779 }
780<VerbatimCode>"''" {
781 copyToOutput(yyscanner,yytext,yyleng);
782 if (!Config_getBool(MARKDOWN_STRICT) && yyextra->blockName=="``") // support for ``text''
783 {
784 yyextra->inVerbatim=false;
785 BEGIN(yyextra->lastCommentContext);
786 }
787 }
788<VerbatimCode>"'" {
789 copyToOutput(yyscanner,yytext,yyleng);
790 if (!Config_getBool(MARKDOWN_STRICT) && yyextra->blockName=="`") // support for `text'
791 {
792 yyextra->inVerbatim=false;
793 BEGIN(yyextra->lastCommentContext);
794 }
795 }
796<VerbatimCode>"`"{1,2} {
797 copyToOutput(yyscanner,yytext,yyleng);
798 if (yytext==yyextra->blockName)
799 {
800 yyextra->inVerbatim=false;
801 BEGIN(yyextra->lastCommentContext);
802 }
803 }
804<VerbatimCode>{CMD}("enddot"|"endcode"|"endmsc"|"enduml")/("{")? { // end of verbatim block
805 copyToOutput(yyscanner,yytext,yyleng);
806 if (&yytext[1]==yyextra->blockName)
807 {
808 yyextra->inVerbatim=false;
809 BEGIN(yyextra->lastCommentContext);
810 }
811 }
812<VerbatimCode>^[ \t]*{CPPC}[\!\/]? { /* skip leading comments */
813 if (!yyextra->inSpecialComment || yyextra->mlBrief)
814 {
815 copyToOutput(yyscanner,yytext,yyleng);
816 }
817 else
818 {
819 int l=0;
820 while (yytext[l]==' ' || yytext[l]=='\t')
821 {
822 l++;
823 }
824 copyToOutput(yyscanner,yytext,l);
825 if (yyleng-l==3) // ends with //! or ///
826 {
827 copyToOutput(yyscanner," * ");
828 }
829 else // ends with //
830 {
831 copyToOutput(yyscanner,"//");
832 }
833 }
834 }
835<Verbatim,VerbatimCode>[^`'~@\/\-\\\n{}]* { /* any character not a backslash or new line or } */
836 copyToOutput(yyscanner,yytext,yyleng);
837 }
838<Verbatim,VerbatimCode>[^`'~@\/\-\\\n{}]+/\n{B}*"//" {
839 if (yyextra->lastCommentContext!=ReadLine)
840 {
841 REJECT;
842 }
843 else
844 {
845 copyToOutput(yyscanner,yytext,yyleng);
846 }
847 }
848<Verbatim,VerbatimCode>[^`'~@\/\-\\\n{}]+/\n { /* premature end of comment block */
849 if (yyextra->lastCommentContext==ReadLine)
850 {
851 yyextra->inVerbatim=false;
852 BEGIN(yyextra->lastCommentContext);
853 }
854 copyToOutput(yyscanner,yytext,yyleng);
855 }
856<Verbatim,VerbatimCode>\n { /* new line in verbatim block */
857 copyToOutput(yyscanner,yytext,yyleng);
858 if (yyextra->lastCommentContext == IncludeFile)
859 {
860 insertCommentStart(yyscanner);
861 }
862 }
863<Verbatim>^[ \t]*{CPPC}[/!] {
864 if (yyextra->blockName=="enddot" || yyextra->blockName=="endmsc" || yyextra->blockName=="enduml" || yyextra->blockName.at(0)=='f')
865 {
866 // see bug 487871, strip /// from dot images and formulas.
867 int l=0;
868 while (yytext[l]==' ' || yytext[l]=='\t')
869 {
870 l++;
871 }
872 copyToOutput(yyscanner,yytext,l);
873 copyToOutput(yyscanner," ");
874 }
875 else // even slashes are verbatim (e.g. \verbatim, \code)
876 {
877 REJECT;
878 }
879 }
880<Verbatim,VerbatimCode>. { /* any other character */
881 copyToOutput(yyscanner,yytext,yyleng);
882 }
883<SkipString>\\. { /* escaped character in string */
884 if (yyextra->lang==SrcLangExt::Fortran || yyextra->lang==SrcLangExt::VHDL)
885 {
886 unput(yytext[1]);
887 copyToOutput(yyscanner,yytext,1);
888 }
889 else
890 {
891 copyToOutput(yyscanner,yytext,yyleng);
892 }
893 }
894<SkipString>"\"" { /* end of string */
895 copyToOutput(yyscanner,yytext,yyleng);
896 BEGIN(yyextra->stringContext);
897 }
898<SkipString>. { /* any other string character */
899 copyToOutput(yyscanner,yytext,yyleng);
900 }
901<SkipString>\n { /* new line inside string (illegal for some compilers) */
902 copyToOutput(yyscanner,yytext,yyleng);
903 }
904<SkipVerbString>[^"\n]+ {
905 copyToOutput(yyscanner,yytext,yyleng);
906 }
907<SkipVerbString>\"\" { // escaped quote
908 copyToOutput(yyscanner,yytext,yyleng);
909 }
910<SkipVerbString>"\"" { /* end of string */
911 copyToOutput(yyscanner,yytext,yyleng);
912 BEGIN(yyextra->stringContext);
913 }
914<SkipVerbString>. {
915 copyToOutput(yyscanner,yytext,yyleng);
916 }
917<SkipVerbString>\n {
918 copyToOutput(yyscanner,yytext,yyleng);
919 }
920<SkipChar>\\. { /* escaped character */
921 if (yyextra->lang==SrcLangExt::Fortran || yyextra->lang==SrcLangExt::VHDL)
922 {
923 unput(yytext[1]);
924 copyToOutput(yyscanner,yytext,1);
925 }
926 else
927 {
928 copyToOutput(yyscanner,yytext,yyleng);
929 }
930 }
931<SkipChar>' { /* end of character literal */
932 copyToOutput(yyscanner,yytext,yyleng);
933 BEGIN(yyextra->charContext);
934 }
935<SkipChar>. { /* any other string character */
936 copyToOutput(yyscanner,yytext,yyleng);
937 }
938<SkipChar>\n { /* new line character */
939 copyToOutput(yyscanner,yytext,yyleng);
940 }
941
942<CComment,CNComment>[^ `~<\\!@*\n{\"'\/-`]* { /* anything that is not a '*' or command */
943 copyToOutput(yyscanner,yytext,yyleng);
944 }
945<CComment,CNComment>^{B}*"*"+[^*\/<\\@\n{\"`]* { /* stars without slashes */
946 if (yyextra->lang==SrcLangExt::Markdown) REJECT;
947 yyextra->col = computeIndent(yytext);
948 if (yyextra->col>yyextra->blockHeadCol)
949 {
950 //printf("new blockHeadCol=%d\n",yyextra->blockHeadCol);
951 yyextra->blockHeadCol=yyextra->col;
952 }
953 copyToOutput(yyscanner,yytext,yyleng);
954 }
955<CComment>"'''" |
956<CComment>"\"\"\"" { /* end of Python docstring */
957 if (yyextra->lang!=SrcLangExt::Python)
958 {
959 REJECT;
960 }
961 else if (yyextra->pythonDocStringChar != yytext[0])
962 {
963 copyToOutput(yyscanner,yytext,yyleng);
964 }
965 else
966 {
967 yyextra->nestingCount--;
968 yyextra->pythonDocString = FALSE;
969 yyextra->pythonDocStringChar = '\0';
970 copyToOutput(yyscanner,yytext,yyleng);
971 BEGIN(Scan);
972 }
973 }
974<CComment,CNComment>\n { /* new line in comment */
975 copyToOutput(yyscanner,yytext,yyleng);
976 /* in case of Fortran always end of comment */
977 if (yyextra->lang==SrcLangExt::Fortran)
978 {
979 BEGIN(Scan);
980 }
981 }
982<CComment,CNComment>"/""/"+/"*/" { /* we are already in C-comment so not a start of a nested comment but
983 * just the end of the comment (the end part is handled later). */
984 copyToOutput(yyscanner,yytext,yyleng);
985 }
986<CComment,CNComment>"/"+"*" { /* nested C comment */
987 if (yyextra->lang==SrcLangExt::Python ||
988 yyextra->lang==SrcLangExt::Markdown)
989 {
990 REJECT;
991 }
992 yyextra->nestingCount++;
993 yyextra->commentStack.push(yyextra->lineNr);
994 copyToOutput(yyscanner,yytext,yyleng);
995 }
996<CComment,CNComment>^{B}*"*"+"/" |
997<CComment,CNComment>"*"+"/" { /* end of C comment */
998 if (yyextra->lang==SrcLangExt::Python ||
999 yyextra->lang==SrcLangExt::Markdown)
1000 {
1001 REJECT;
1002 }
1003 else
1004 {
1005 copyToOutput(yyscanner,yytext,yyleng);
1006 yyextra->nestingCount--;
1007 if (yyextra->nestingCount<=0)
1008 {
1009 BEGIN(Scan);
1010 }
1011 else
1012 {
1013 //yyextra->nestingCount--;
1014 yyextra->commentStack.pop();
1015 }
1016 }
1017 }
1018 /* Python an VHDL share CComment,CNComment, so special attention for ending comments is required */
1019<CComment,CNComment>"\n"/[ \t]*"#" {
1020 if (yyextra->lang!=SrcLangExt::VHDL)
1021 {
1022 REJECT;
1023 }
1024 else
1025 {
1026 if (yyextra->vhdl) // inside --! comment
1027 {
1028 yyextra->vhdl = FALSE;
1029 copyToOutput(yyscanner,yytext,yyleng);
1030 BEGIN(Scan);
1031 }
1032 else // C-type comment
1033 {
1034 REJECT;
1035 }
1036 }
1037 }
1038<CComment,CNComment>"\n"/[ \t]*"-" {
1039 if (yyextra->lang!=SrcLangExt::Python || yyextra->pythonDocString)
1040 {
1041 REJECT;
1042 }
1043 else
1044 {
1045 copyToOutput(yyscanner,yytext,yyleng);
1046 BEGIN(Scan);
1047 }
1048 }
1049<CComment,CNComment>"\n"/[ \t]*[^ \t#\-] {
1050 if (yyextra->lang==SrcLangExt::Python)
1051 {
1052 if (yyextra->pythonDocString)
1053 {
1054 REJECT;
1055 }
1056 else
1057 {
1058 copyToOutput(yyscanner,yytext,yyleng);
1059 BEGIN(Scan);
1060 }
1061 }
1062 else if (yyextra->lang==SrcLangExt::VHDL)
1063 {
1064 if (yyextra->vhdl) // inside --! comment
1065 {
1066 yyextra->vhdl = FALSE;
1067 copyToOutput(yyscanner,yytext,yyleng);
1068 BEGIN(Scan);
1069 }
1070 else // C-type comment
1071 {
1072 REJECT;
1073 }
1074 }
1075 else
1076 {
1077 REJECT;
1078 }
1079 }
1080 /* removed for bug 674842 (bug was introduced in rev 768)
1081<CComment,CNComment>"'" {
1082 yyextra->charContext = YY_START;
1083 copyToOutput(yyscanner,yytext,yyleng);
1084 BEGIN(SkipChar);
1085 }
1086<CComment,CNComment>"\"" {
1087 yyextra->stringContext = YY_START;
1088 copyToOutput(yyscanner,yytext,yyleng);
1089 BEGIN(SkipString);
1090 }
1091 */
1092<CComment,CNComment>{CMD}"~"[a-z_A-Z-]* { // language switch command
1093 if (yyextra->lang!=SrcLangExt::Markdown) REJECT;
1094 QCString langId = QCString(yytext).mid(2);
1095 if (!langId.isEmpty() &&
1096 qstricmp(Config_getEnumAsString(OUTPUT_LANGUAGE),langId)!=0)
1097 { // enable language specific section
1098 if (!Config_isAvailableEnum(OUTPUT_LANGUAGE,langId))
1099 {
1100 warn(yyextra->fileName,yyextra->lineNr,
1101 "non supported language '{}' specified in '{}'",langId,QCString(yytext).stripWhiteSpace());
1102 }
1103 BEGIN(SkipLang);
1104 }
1105 }
QCString mid(size_t index, size_t len=static_cast< size_t >(-1)) const
Definition qcstring.h:241
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:163
#define Config_getEnumAsString(name)
Definition config.h:36
#define Config_isAvailableEnum(name, value)
Definition config.h:45
#define warn(file, line, fmt,...)
Definition message.h:97
int qstricmp(const char *s1, const char *s2)
Definition qcstring.cpp:530
std::string_view stripWhiteSpace(std::string_view s)
Given a string view s, returns a new, narrower view on that string, skipping over any leading or trai...
Definition stringutil.h:72
1106<CComment,CNComment>{CMD}{CMD} |
1107<CComment,CNComment>. {
1108 copyToOutput(yyscanner,yytext,yyleng);
1109 }
1110<SkipLang>{CMD}"~"[a-zA-Z-]* { /* language switch */
1111 QCString langId(&yytext[2]);
1112 if (!langId.isEmpty() && !Config_isAvailableEnum(OUTPUT_LANGUAGE,langId))
1113 {
1114 warn(yyextra->fileName,yyextra->lineNr,
1115 "non supported language '{}' specified in '{}'",langId,QCString(yytext).stripWhiteSpace());
1116 }
1117 else if (langId.isEmpty() ||
1118 qstricmp(Config_getEnumAsString(OUTPUT_LANGUAGE),langId)==0)
1119 { // enable language specific section
1120 BEGIN(CComment);
1121 }
1122 }
1123<SkipLang>[^*@\\\n]* { /* any character not a *, @, backslash or new line */
1124 }
1125<SkipLang>\n { /* new line in language block, needed for keeping track of line numbers */
1126 copyToOutput(yyscanner,yytext,yyleng);
1127 }
1128<SkipLang>. { /* any other character */
1129 }
1130<SComment>^[ \t]*{CPPC}"/"{SLASHopt}/\n {
1131 replaceComment(yyscanner,0);
1132 }
1133<SComment>\n[ \t]*{CPPC}"/"{SLASHopt}/\n {
1134 replaceComment(yyscanner,1);
1135 }
1136<SComment>^[ \t]*{CPPC}"/"[^\/\n]/.*\n {
1137 replaceComment(yyscanner,0);
1138 yyextra->readLineCtx=YY_START;
1139 YY_CURRENT_BUFFER->yy_at_bol=1;
1140 BEGIN(ReadLine);
1141 }
1142<SComment>\n[ \t]*{CPPC}[\/!]("<")?[ \t]*{CMD}"}".*\n {
1143 /* See Bug 752712: end the multiline comment when finding a @} or \} command */
1144 copyToOutput(yyscanner," */");
1145 copyToOutput(yyscanner,yytext,yyleng);
1146 yyextra->inSpecialComment=false;
1147 yyextra->inRoseComment=false;
1148 BEGIN(Scan);
1149 }
1150<SComment>\n[ \t]*{CPPC}"/"[^\\@\/\n]/.*\n {
1151 replaceComment(yyscanner,1);
1152 yyextra->readLineCtx=YY_START;
1153 YY_CURRENT_BUFFER->yy_at_bol=1;
1154 BEGIN(ReadLine);
1155 }
1156<SComment>^[ \t]*{CPPC}"!" | // just //!
1157<SComment>^[ \t]*{CPPC}"!<"/.*\n | // or //!< something
1158<SComment>^[ \t]*{CPPC}"!"[^<]/.*\n { // or //!something
1159 replaceComment(yyscanner,0);
1160 yyextra->readLineCtx=YY_START;
1161 YY_CURRENT_BUFFER->yy_at_bol=1;
1162 BEGIN(ReadLine);
1163 }
1164<SComment>\n[ \t]*{CPPC}"!" |
1165<SComment>\n[ \t]*{CPPC}"!<"/.*\n |
1166<SComment>\n[ \t]*{CPPC}"!"[^<\n]/.*\n {
1167 replaceComment(yyscanner,1);
1168 yyextra->readLineCtx=YY_START;
1169 YY_CURRENT_BUFFER->yy_at_bol=1;
1170 BEGIN(ReadLine);
1171 }
1172<SComment>^[ \t]*{CPPC}"##"/.*\n {
1173 if (!yyextra->inRoseComment)
1174 {
1175 REJECT;
1176 }
1177 else
1178 {
1179 replaceComment(yyscanner,0);
1180 yyextra->readLineCtx=YY_START;
1181 YY_CURRENT_BUFFER->yy_at_bol=1;
1182 BEGIN(ReadLine);
1183 }
1184 }
1185<SComment>\n[ \t]*{CPPC}"##"/.*\n {
1186 if (!yyextra->inRoseComment)
1187 {
1188 REJECT;
1189 }
1190 else
1191 {
1192 replaceComment(yyscanner,1);
1193 yyextra->readLineCtx=YY_START;
1194 YY_CURRENT_BUFFER->yy_at_bol=1;
1195 BEGIN(ReadLine);
1196 }
1197 }
1198<SComment>\n { /* end of special comment */
1199 copyToOutput(yyscanner," */");
1200 copyToOutput(yyscanner,yytext,yyleng);
1201 yyextra->inSpecialComment=FALSE;
1202 yyextra->inRoseComment=FALSE;
1203 yyextra->insertCppCommentMarker=false;
1204 yyextra->readLineCtx = Scan; // reset, otherwise there will be problems with:
1205 // static void handleCondSectionId
1206 BEGIN(Scan);
1207 }
1208<ReadLine>{CCS}"*" {
1209 copyToOutput(yyscanner,"/&zwj;**");
1210 }
1211<ReadLine>{CCE} {
1212 copyToOutput(yyscanner,"*&zwj;/");
1213 }
1214<ReadLine,CopyLine>"*" {
1215 copyToOutput(yyscanner,yytext,yyleng);
1216 }
1217<ReadLine,CopyLine>{RL} {
1218 copyToOutput(yyscanner,yytext,yyleng);
1219 }
1220<ReadLine,CopyLine>{RL}/{B}{CMD}"ilinebr"{B} {
1221 copyToOutput(yyscanner,yytext,yyleng);
1222 }
1223<ReadLine,CopyLine>{RLopt}/\n {
1224 copyToOutput(yyscanner,yytext,yyleng);
1225 yyextra->insertCppCommentMarker=false;
1226 BEGIN(yyextra->readLineCtx);
1227 }
1228<CComment,CNComment,ReadLine>"\<" { /* escaped html comment */
1229 copyToOutput(yyscanner,yytext,yyleng);
1230 }
1231<CComment,CNComment,ReadLine>{CMD}{CMD}[~a-z_A-Z][a-z_A-Z0-9]*[ \t]* { // escaped command
1232 copyToOutput(yyscanner,yytext,yyleng);
1233 }
1234
1235<CComment,ReadLine,IncludeFile>{CMD}("include"{OPTS}|"includedoc"{OPTS}*) {
1236 if (!parseIncludeOptions(yyscanner,std::string_view{yytext,static_cast<size_t>(yyleng)})) REJECT;
1237 yyextra->includeCtx = YY_START;
1238 yyextra->firstIncludeLine = true;
1239 yyextra->insertCommentCol = yyextra->col;
1240 if (!yyextra->insertCppCommentMarker && (yyextra->includeCtx==ReadLine || yyextra->includeCtx==IncludeFile))
1241 {
1242 yyextra->insertCppCommentMarker = yyextra->mlBrief;
1243 }
1244 //printf("blockHeadCol=%d insertCommentCol=%d\n",yyextra->blockHeadCol, yyextra->insertCommentCol);
1245 BEGIN(IncludeDoc);
1246 }
1247<CComment,ReadLine,IncludeFile>{CMD}("snippet"{OPTS}|"snippetdoc"{OPTS}*) {
1248 if (!parseIncludeOptions(yyscanner,std::string_view{yytext,static_cast<size_t>(yyleng)})) REJECT;
1249 yyextra->includeCtx = YY_START;
1250 yyextra->firstIncludeLine = true;
1251 yyextra->insertCommentCol = yyextra->col;
1252 if (!yyextra->insertCppCommentMarker && (yyextra->includeCtx==ReadLine || yyextra->includeCtx==IncludeFile))
1253 {
1254 yyextra->insertCppCommentMarker = yyextra->mlBrief;
1255 }
1256 //printf("blockHeadCol=%d insertCommentCol=%d\n",yyextra->blockHeadCol, yyextra->insertCommentCol);
1257 BEGIN(SnippetDoc);
1258 }
1259<IncludeDoc,SnippetDoc>{B}*
1260<IncludeDoc>{FILEMASK}|"\""[^\n\"]+"\"" {
1261 QCString fileName=yytext;
1262 if (yytext[0]=='"')
1263 {
1264 fileName=fileName.mid(1,fileName.length()-2); // strip quotes
1265 }
1266 if (readIncludeFile(yyscanner,fileName,""))
1267 {
1268 BEGIN(IncludeFile);
1269 }
1270 else
1271 {
1272 BEGIN(yyextra->includeCtx);
1273 }
1274 }
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:166
1275<SnippetDoc>({FILEMASK}|"\""[^\n\"]+"\""){B}+ {
1276 yyextra->snippetFileName=yytext;
1277 yyextra->snippetFileName=yyextra->snippetFileName.stripWhiteSpace();
1278 if (yyextra->snippetFileName == "this") yyextra->snippetFileName=yyextra->fileName;
1279 yyextra->snippetName = "";
1280 BEGIN(SnippetDocTag);
1281 }
1282<SnippetDocTag>[^\\@\n]+ {
1283 yyextra->snippetName += yytext;
1284 }
1285<SnippetDocTag>{CMD} {
1286 yyextra->snippetName += yytext;
1287 }
1288<SnippetDocTag>(\n|{CMD}"ilinebr") {
1289 for (int i=(int)yyleng-1;i>=0;i--) unput(yytext[i]);
1290 yyextra->snippetName = yyextra->snippetName.stripWhiteSpace();
1291 QCString blockId = "["+yyextra->snippetName+"]";
1292 if (readIncludeFile(yyscanner,yyextra->snippetFileName,blockId))
1293 {
1294 BEGIN(IncludeFile);
1295 }
1296 else
1297 {
1298 BEGIN(yyextra->includeCtx);
1299 }
1300 }
1301
1302<IncludeDoc,SnippetDoc>\n {
1303 copyToOutput(yyscanner,yytext,yyleng);
1304 insertCommentStart(yyscanner);
1305 // missing file name
1306 //warn(yyextra->fileName,yyextra->lineNr,"Found \\include{{doc}} command without valid file name argument");
1307 BEGIN(yyextra->includeCtx);
1308 }
1309<IncludeDoc,SnippetDoc>. { // invalid character
1310 copyToOutput(yyscanner,yytext,yyleng);
1311 BEGIN(yyextra->includeCtx);
1312 }
1313<CComment,ReadLine,IncludeFile>{CMD}"cond"/[^a-z_A-Z0-9] { // conditional section
1314 yyextra->condCtx = YY_START;
1315 BEGIN(CondLine);
1316 }
1317<CComment,ReadLine,IncludeFile>{CMD}"endcond"/[^a-z_A-Z0-9] { // end of conditional section
1318 bool oldSkip=yyextra->skip;
1319 endCondSection(yyscanner);
1320 if (YY_START==CComment && oldSkip && !yyextra->skip)
1321 {
1322 //printf("** Adding start of comment!\n");
1323 if (yyextra->lang!=SrcLangExt::Python &&
1324 yyextra->lang!=SrcLangExt::VHDL &&
1325 yyextra->lang!=SrcLangExt::Markdown &&
1326 yyextra->lang!=SrcLangExt::Fortran)
1327 {
1328 yyextra->outBuf+='/';
1329 yyextra->outBuf+='*';
1330 yyextra->col+=2;
1331 if (yyextra->specialComment)
1332 {
1333 yyextra->outBuf+='*';
1334 yyextra->col++;
1335 }
1336 }
1337 }
1338 }
1339<CondLine>[!()&| \ta-z_A-Z0-9.\-]+ {
1340 handleCondSectionId(yyscanner,yytext);
1341 }
1342<CComment,ReadLine,IncludeFile>{CMD}"cond"{WSopt}/\n {
1343 yyextra->condCtx=YY_START;
1344 handleCondSectionId(yyscanner," "); // fake section id causing the section to be hidden unconditionally
1345 }
1346<CondLine>\n |
1347<CondLine>. { // forgot section id?
1348 handleCondSectionId(yyscanner," "); // fake section id causing the section to be hidden unconditionally
1349 if (*yytext=='\n') { copyToOutput(yyscanner,"\n");}
1350 }
1351<CComment,ReadLine,IncludeFile,Verbatim,VerbatimCode>{CMD}[a-z_A-Z][a-z_A-Z0-9-]* { // expand alias without arguments
1352 bool inCppComment = YY_START==ReadLine && yyextra->readLineCtx==SComment;
1353 bool inCComment = YY_START==CComment && yyextra->blockHeadCol>0;
1354 replaceAliases(yyscanner,yytext,inCppComment,inCComment);
1355 }
1356<CComment,ReadLine,IncludeFile,Verbatim,VerbatimCode>{B}?{CMD}"ilinebr"{B}{CMD}"ialias{" { // expand alias with arguments
1357 yyextra->lastBlockContext=YY_START;
1358 yyextra->blockCount=1;
1359 int extraSpace = (yytext[0]==' '? 1:0);
1360 yyextra->aliasString=yytext+9+extraSpace;
1361 yyextra->aliasCmd=yytext+9+extraSpace;
1362 yyextra->lastEscaped=0;
1363 BEGIN( ReadAliasArgs );
1364 }
1365<CComment,ReadLine,IncludeFile,Verbatim,VerbatimCode>{CMD}[a-z_A-Z][a-z_A-Z0-9-]*"{" { // expand alias with arguments
1366 yyextra->lastBlockContext=YY_START;
1367 yyextra->blockCount=1;
1368 yyextra->aliasString=yytext;
1369 yyextra->aliasCmd=yytext;
1370 yyextra->lastEscaped=0;
1371 BEGIN( ReadAliasArgs );
1372 }
1373<ReadAliasArgs>^[ \t]*"*" {
1374 int indent=computeIndent(yytext)-1;
1375 if (indent>yyextra->blockHeadCol) // assume part of block marker
1376 {
1377 yyextra->aliasString+=yytext;
1378 }
1379 else
1380 {
1381 // skip leading *
1382 }
1383 }
1384<ReadAliasArgs>^[ \t]*{CPPC}[/!]/[^\n]* { // skip leading special comments (see bug 618079)
1385 }
1386<ReadAliasArgs>[^{}\n\\\*]+ {
1387 yyextra->aliasString+=yytext;
1388 yyextra->lastEscaped=FALSE;
1389 }
1390<ReadAliasArgs>"\\" {
1391 if (yyextra->lastEscaped) yyextra->lastEscaped=FALSE;
1392 else yyextra->lastEscaped=TRUE;
1393 yyextra->aliasString+=yytext;
1394 }
1395<ReadAliasArgs>{CMD}("endverbatim"|"endiliteral"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"enddocbookonly"|"endrtfonly"|"endmanonly"|"f$"|"f]"|"f}"|"f)") { /* end of verbatim block */
1396 yyextra->aliasString+=yytext;
1397 if (yyextra->inVerbatim && &yytext[1]==yyextra->blockName)
1398 // For verbatim sections we do not support matching end block markers inside
1399 // alias arguments. Instead this will end the verbatim block.
1400 // This is needed to support an alias definition
1401 // like startalign=" \latexonly\noalign{\endlatexonly" were otherwise
1402 // the scanner would try to find a matching closing brace for noalign and then
1403 // skip over the \endlatexonly command.
1404 {
1405 copyToOutput(yyscanner,yyextra->aliasString.view());
1406 yyextra->inVerbatim=false;
1407 BEGIN(yyextra->lastCommentContext);
1408 }
1409 }
1410<ReadAliasArgs>\n {
1411 yyextra->aliasString+=yytext;
1412 yyextra->lastEscaped=FALSE;
1413 if (yyextra->inVerbatim) // for verbatim sections we do not support multi-line
1414 // alias arguments.
1415 {
1416 copyToOutput(yyscanner,yyextra->aliasString.view());
1417 BEGIN( yyextra->lastBlockContext );
1418 }
1419 }
1420<ReadAliasArgs>"{" {
1421 yyextra->aliasString+=yytext;
1422 if (!yyextra->lastEscaped) yyextra->blockCount++;
1423 yyextra->lastEscaped=FALSE;
1424 }
1425<ReadAliasArgs>"}" {
1426 yyextra->aliasString+=yytext;
1427 if (!yyextra->lastEscaped) yyextra->blockCount--;
1428 if (yyextra->blockCount==0)
1429 {
1430 bool inCppComment = yyextra->lastBlockContext==ReadLine && yyextra->readLineCtx==SComment;
1431 bool inCComment = yyextra->lastBlockContext==CComment && yyextra->blockHeadCol>0;
1432 replaceAliases(yyscanner,yyextra->aliasString.view(),inCppComment,inCComment);
1433 BEGIN( yyextra->lastBlockContext );
1434 }
1435 yyextra->lastEscaped=FALSE;
1436 }
1437<ReadAliasArgs>. {
1438 yyextra->aliasString+=yytext;
1439 yyextra->lastEscaped=FALSE;
1440 }
1441<CopyLine>. {
1442 copyToOutput(yyscanner,yytext,yyleng);
1443 }
1444<CopyLine>\n {
1445 copyToOutput(yyscanner,yytext,yyleng);
1446 yyextra->insertCppCommentMarker=false;
1447 BEGIN(yyextra->readLineCtx);
1448 }
1449<ReadLine>{CMD}{CMD} |
1450<ReadLine>. {
1451 copyToOutput(yyscanner,yytext,yyleng);
1452 }
1453<IncludeFile>. {
1454 copyToOutput(yyscanner,yytext,yyleng);
1455 }
1456<IncludeFile>\n {
1457 copyToOutput(yyscanner,yytext,yyleng);
1458 insertCommentStart(yyscanner);
1459 }
1460<*>. {
1461 copyToOutput(yyscanner,yytext,yyleng);
1462 }
1463<<EOF>> {
1464 if (YY_START == ReadAliasArgs)
1465 {
1466 warn(yyextra->fileName,yyextra->lineNr,
1467 "Reached end of file while still searching closing '}}' of an alias argument (probable start: '{}')",
1468 yyextra->aliasCmd);
1469 }
1470 else if (YY_START==Verbatim || YY_START==VerbatimCode)
1471 {
1472 warn(yyextra->fileName,yyextra->lineNr,
1473 "Reached end of file while still searching closing '{}' of a verbatim block starting at line {}",
1474 yyextra->blockName,yyextra->verbatimLine);
1475 }
1476 if (yyextra->includeStack.empty())
1477 {
1478 yyextra->insertCppCommentMarker=false;
1479 yyterminate();
1480 }
1481 else // switch back to parent file
1482 {
1483 std::unique_ptr<commentcnv_FileState> &fs = yyextra->includeStack.back();
1484 YY_BUFFER_STATE oldBuf = YY_CURRENT_BUFFER;
1485 yy_switch_to_buffer(fs->bufState, yyscanner);
1486 yy_delete_buffer(oldBuf, yyscanner);
1487 BEGIN(fs->oldState);
1488 yyextra->fileName = fs->oldFileName;
1489 yyextra->lineNr = fs->oldLineNr;
1490 yyextra->inBuf = fs->oldFileBuf;
1491 yyextra->inBufPos = fs->oldFileBufPos;
1492 yyextra->includeCtx = fs->oldIncludeCtx;
1493 QCString lineStr= " \\ifile \""+yyextra->fileName+"\" \\iline "+QCString().setNum(yyextra->lineNr)+" ";
1494 if (fs->oldRaiseLvl!=yyextra->raiseLevel)
1495 {
1496 lineStr+="\\iraise " + std::to_string(fs->oldRaiseLvl)+ " ";
1497 }
1498 if (fs->oldRaiseLbl!=yyextra->raiseLabel)
1499 {
1500 lineStr+="\\iprefix \"" + fs->oldRaiseLbl + "\" ";
1501 }
1502 lineStr+="\\ilinebr ";
1503 yyextra->raiseLevel = fs->oldRaiseLvl;
1504 yyextra->raiseLabel = fs->oldRaiseLbl;
1505 copyToOutput(yyscanner,lineStr.view());
1506 yyextra->includeStack.pop_back();
1507 //printf("<<EOF>> switch back to %s line %d inbufPos=%d outbufPos=%d\n",
1508 // qPrint(yyextra->fileName),yyextra->lineNr,yyextra->inBufPos,yyextra->outBuf.curPos());
1509 }
1510 }
QCString & setNum(short n)
Definition qcstring.h:459
std::string_view view() const
Definition qcstring.h:174
#define yyterminate()
1511 /*
1512<*>\n { fprintf(stderr,"Lex scanner %s (%s) default rule newline for state %s.\n", __FILE__, qPrint(yyextra->fileName),stateToString(YY_START));}
1513 */
1514%%
1515
1516static bool parseIncludeOptions(yyscan_t yyscanner,std::string_view s)
1517{
1518 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1519
1520 //printf("parseIncludeOptions=%s\n",qPrint(s));
1521 size_t optIdxStart = s.find('{');
1522 size_t optIdxEnd = optIdxStart!=std::string::npos ? s.find("}",optIdxStart+1) : std::string::npos;
1523 std::string cmdName;
1524 StringVector optList;
1525 if (optIdxStart == std::string::npos) // no options
1526 {
1527 cmdName = stripWhiteSpace(s.substr(1)); // to remove {CMD}
1528 }
1529 else // options present
1530 {
1531 cmdName = stripWhiteSpace(s.substr(1,optIdxStart-1)); // to remove {CMD}
1532 optList = split(std::string{s.substr(optIdxStart+1,optIdxEnd-optIdxStart-1)},",");
1533 }
1534 bool isDoc = cmdName=="includedoc" || cmdName=="snippetdoc";
1535 for (const auto &opt : optList)
1536 {
1537 if (stripWhiteSpace(opt)==std::string_view{"doc"})
1538 {
1539 isDoc=true;
1540 break;
1541 }
1542 }
1543
1544 if (isDoc)
1545 {
1546 for (const auto &opt : optList)
1547 {
1548 std::string_view locOpt = stripWhiteSpace(opt);
1549 size_t posEqual = locOpt.find('=');
1550 std::string_view option = posEqual!=std::string::npos ? stripWhiteSpace(locOpt.substr(0,posEqual)) : locOpt;
1551 std::string_view value = posEqual!=std::string::npos ? stripWhiteSpace(locOpt.substr(posEqual+1)) : std::string_view();
1552
1553 if (option==std::string_view{"doc"} && value.empty())
1554 {
1555 }
1556 else if (option==std::string_view{"raise"} && !value.empty())
1557 {
1558 yyextra->raiseIncrement = atoi(value.data());
1559 if (yyextra->raiseLevel+yyextra->raiseIncrement>=SectionType::MaxLevel) // check range
1560 {
1561 warn(yyextra->fileName,yyextra->lineNr,"Raising section level from {} to {}, exceeds allowed range [0-{}], adjusting",
1562 yyextra->raiseLevel,yyextra->raiseLevel+yyextra->raiseIncrement,SectionType::MaxLevel-1);
1563 yyextra->raiseIncrement = std::max(0,SectionType::MaxLevel-1-yyextra->raiseLevel);
1564 }
1565 }
1566 else if (option==std::string_view{"prefix"} && !value.empty())
1567 {
1568 yyextra->raisePrefix = value;
1569 }
1570 else
1571 {
1572 warn(yyextra->fileName,yyextra->lineNr,"Unsupported option '{}' for {} command",option, cmdName);
1573 }
1574 }
1575 }
1576
1577 return isDoc;
1578}
1579
1580
1581static void replaceCommentMarker(yyscan_t yyscanner,std::string_view s)
1582{
1583 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1584 if (s.empty()) return;
1585 size_t p = 0;
1586 size_t len = s.length();
1587 char c = 0;
1588 // copy leading blanks
1589 while (p<len && (c=s[p]) && (c==' ' || c=='\t' || c=='\n'))
1590 {
1591 yyextra->outBuf+=c;
1592 if (c=='\n') { yyextra->lineNr++; yyextra->col=0; } else { yyextra->col++; }
1593 p++;
1594 }
1595 // replace start of comment marker by blanks and the last character by a *
1596 int blanks=0;
1597 while (p<len && (c=s[p]) && (c=='/' || c=='!' || c=='#'))
1598 {
1599 blanks++;
1600 p++;
1601 if (p<len && s[p]=='<') // comment-after-item marker
1602 {
1603 blanks++;
1604 p++;
1605 }
1606 if (c=='!') // end after first !
1607 {
1608 break;
1609 }
1610 }
1611 if (blanks>0)
1612 {
1613 while (blanks>2)
1614 {
1615 yyextra->outBuf+=' ';
1616 yyextra->col++;
1617 blanks--;
1618 }
1619 if (blanks>1) { yyextra->outBuf+='*'; yyextra->col++; }
1620 yyextra->outBuf+=' ';
1621 yyextra->col++;
1622 }
1623 // copy comment line to output
1624 yyextra->outBuf+=s.substr(p);
1625 yyextra->col+=static_cast<int>(s.substr(p).length());
1626}
1627
1628static inline int computeIndent(const char *s)
1629{
1630 int col=0;
1631 int tabSize=Config_getInt(TAB_SIZE);
1632 const char *p=s;
1633 char c = 0;
1634 while ((c=*p++))
1635 {
1636 if (c==' ') col++;
1637 else if (c=='\t') col+=tabSize-(col%tabSize);
1638 else break;
1639 }
1640 return col;
1641}
1642
1643static inline void copyToOutput(yyscan_t yyscanner,std::string_view s)
1644{
1645 int tabSize=Config_getInt(TAB_SIZE);
1646 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1647 size_t len = s.length();
1648 if (yyextra->skip) // only add newlines.
1649 {
1650 for (size_t i=0;i<len;i++)
1651 {
1652 switch(s[i])
1653 {
1654 case '\n':
1655 yyextra->outBuf+='\n';
1656 yyextra->lineNr++;
1657 yyextra->col=0;
1658 break;
1659 case '\t':
1660 yyextra->col+=tabSize-(yyextra->col%tabSize);
1661 break;
1662 default:
1663 yyextra->col++;
1664 break;
1665 }
1666 }
1667 }
1668 else if (len>0)
1669 {
1670 yyextra->outBuf+=s;
1671 for (size_t i=0;i<len;i++)
1672 {
1673 switch (s[i])
1674 {
1675 case '\n': yyextra->col=0;
1676 //fprintf(stderr,"---> copy %d\n",g_lineNr);
1677 yyextra->lineNr++; break;
1678 case '\t': yyextra->col+=tabSize-(yyextra->col%tabSize); break;
1679 default: yyextra->col++; break;
1680 }
1681 }
1682 }
1683}
1684
1685static inline void copyToOutput(yyscan_t yyscanner,const char *s,int len)
1686{
1687 copyToOutput(yyscanner,std::string_view{s,(size_t)len});
1688}
1689
1690static void clearCommentStack(yyscan_t yyscanner)
1691{
1692 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1693 while (!yyextra->commentStack.empty()) yyextra->commentStack.pop();
1694}
1695
1696static void startCondSection(yyscan_t yyscanner,const QCString &sectId)
1697{
1698 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1699 //printf("startCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
1700 CondParser prs;
1701 bool expResult = prs.parse(yyextra->fileName,yyextra->lineNr,sectId);
1702 yyextra->condStack.emplace(yyextra->lineNr,sectId,yyextra->skip);
1703 if (!expResult) // not enabled
1704 {
1705 yyextra->skip=TRUE;
1706 }
1707}
1708
1709static void endCondSection(yyscan_t yyscanner)
1710{
1711 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1712 if (yyextra->condStack.empty())
1713 {
1714 warn(yyextra->fileName,yyextra->lineNr,"Found \\endcond command without matching \\cond");
1715 yyextra->skip=FALSE;
1716 }
1717 else
1718 {
1719 const commentcnvYY_CondCtx &ctx = yyextra->condStack.top();
1720 yyextra->skip=ctx.skip;
1721 yyextra->condStack.pop();
1722 }
1723 //printf("endCondSection: skip=%d stack=%d\n",g_skip,g_condStack.count());
1724}
1725
1726static void handleCondSectionId(yyscan_t yyscanner,const char *expression)
1727{
1728 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1729 bool oldSkip=yyextra->skip;
1730 startCondSection(yyscanner,expression);
1731 if ((yyextra->condCtx==CComment || yyextra->readLineCtx==SComment) &&
1732 !oldSkip && yyextra->skip)
1733 {
1734 if (yyextra->lang!=SrcLangExt::Python &&
1735 yyextra->lang!=SrcLangExt::VHDL &&
1736 yyextra->lang!=SrcLangExt::Markdown &&
1737 yyextra->lang!=SrcLangExt::Fortran)
1738 {
1739 yyextra->outBuf+='*';
1740 yyextra->outBuf+='/';
1741 yyextra->col+=2;
1742 }
1743 }
1744 if (yyextra->readLineCtx==SComment)
1745 {
1746 BEGIN(SComment);
1747 }
1748 else
1749 {
1750 BEGIN(yyextra->condCtx);
1751 }
1752}
1753
1754/** Returns the section of text, in between a pair of markers.
1755 * Full lines are returned, excluding the lines on which the markers appear.
1756 * \sa routine lineBlock
1757 */
1758static QCString extractBlock(const QCString &text,const QCString &marker,int &blockPos)
1759{
1760 QCString result;
1761 int p=0,i=-1;
1762 bool found=FALSE;
1763
1764 // find the character positions of the markers
1765 int m1 = text.find(marker);
1766 if (m1==-1) return result;
1767 int m2 = text.find(marker,m1+static_cast<int>(marker.length()));
1768 if (m2==-1) return result;
1769
1770 // find start and end line positions for the markers
1771 int l1=-1,l2=-1;
1772 while (!found && (i=text.find('\n',p))!=-1)
1773 {
1774 found = (p<=m1 && m1<i); // found the line with the start marker
1775 p=i+1;
1776 }
1777 l1=p;
1778 blockPos=p;
1779 int lp=i;
1780 if (found)
1781 {
1782 while ((i=text.find('\n',p))!=-1)
1783 {
1784 if (p<=m2 && m2<i) // found the line with the end marker
1785 {
1786 l2=p;
1787 break;
1788 }
1789 p=i+1;
1790 lp=i;
1791 }
1792 }
1793 if (l2==-1) // marker at last line without newline (see bug706874)
1794 {
1795 l2=lp;
1796 }
1797 return l2>l1 ? text.mid(l1,l2-l1) : QCString();
1798}
1799
1800static void insertCommentStart(yyscan_t yyscanner)
1801{
1802 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1803 int startCol=yyextra->blockHeadCol;
1804 int contentCol=yyextra->insertCommentCol;
1805 int markerSpace=contentCol-startCol;
1806 //printf("insertCommentStart startCol=%d contentCol=%d mlBrief=%d insertCppCommentMarker=%d\n",
1807 // yyextra->blockHeadCol,yyextra->insertCommentCol,yyextra->mlBrief,yyextra->insertCppCommentMarker);
1808 std::string marker;
1809 if (yyextra->lang==SrcLangExt::Python) // need to insert # or space
1810 {
1811 if (yyextra->pythonDocString) // """! style comment
1812 {
1813 marker=" ";
1814 }
1815 else
1816 {
1817 marker="# ";
1818 }
1819 }
1820 else if (yyextra->lang==SrcLangExt::Fortran) // need to insert !!
1821 {
1822 marker="!! ";
1823 }
1824 else if (yyextra->lang==SrcLangExt::Markdown)
1825 {
1826 marker=" ";
1827 }
1828 else if (yyextra->insertCppCommentMarker) // need to insert ///
1829 {
1830 marker="/// ";
1831 if (startCol>0)
1832 {
1833 // insert `///` instead of '* '
1834 startCol--;
1835 markerSpace++;
1836 }
1837 }
1838 else // need to insert *
1839 {
1840 marker="* ";
1841 }
1842 int i=0;
1843 for (;i<startCol;i++)
1844 {
1845 copyToOutput(yyscanner," ");
1846 }
1847 if (static_cast<int>(marker.length())<=markerSpace && !yyextra->firstIncludeLine)
1848 {
1849 copyToOutput(yyscanner,marker);
1850 i+=static_cast<int>(marker.length());
1851 }
1852 for (;i<contentCol;i++)
1853 {
1854 copyToOutput(yyscanner," ");
1855 }
1856 yyextra->firstIncludeLine = false;
1857}
1858
1859static bool readIncludeFile(yyscan_t yyscanner,const QCString &inc,const QCString &blockId)
1860{
1861 //printf("readIncludeFile(inc=%s,blockId=%s)\n",qPrint(inc),qPrint(blockId));
1862 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1863 bool ambig = false;
1864 QCString absFileName = findFilePath(inc,ambig);
1865 FileInfo fi(absFileName.str());
1866 if (!absFileName.isEmpty() && fi.exists() && fi.isFile())
1867 {
1868 if (ambig)
1869 {
1870 warn_doc_error(yyextra->fileName,yyextra->lineNr,"included file name '{}' is ambiguous"
1871 "Possible candidates:\n{}",inc, showFileDefMatches(Doxygen::exampleNameLinkedMap,inc));
1872 }
1873 bool alreadyProcessed = std::any_of(
1874 yyextra->includeStack.begin(),
1875 yyextra->includeStack.end(),
1876 [&absFileName,&blockId](const auto &lfs)
1877 { return lfs->fileName==absFileName && lfs->blockId==blockId; }
1878 );
1879
1880 if (alreadyProcessed)
1881 {
1882 if (!blockId.isEmpty())
1883 {
1884 warn_doc_error(yyextra->fileName,yyextra->lineNr,"recursive usage of '\\snippet{{doc}}' block with name '{}' and file name '{}', skipping",
1885 blockId,absFileName);
1886 }
1887 else
1888 {
1889 warn_doc_error(yyextra->fileName,yyextra->lineNr,"recursive usage of '\\include{{doc}}' with file name '{}', skipping", absFileName);
1890 }
1891 return false;
1892 }
1893
1894 auto fs = std::make_unique<commentcnv_FileState>();
1895 if (!readInputFile(absFileName,fs->fileBuf,false))
1896 {
1897 warn_doc_error(yyextra->fileName,yyextra->lineNr,"\\{}{{doc}} file '{}' could not be read",blockId.isEmpty()?"include":"snippet",absFileName);
1898 fs.reset();
1899 return false;
1900 }
1901 int lineNr=1;
1902 if (!blockId.isEmpty())
1903 {
1904 QCString incText { fs->fileBuf };
1905 int count = incText.contains(blockId.data());
1906 if (count!=2)
1907 {
1908 warn_doc_error(yyextra->fileName,yyextra->lineNr,"block marked with {} for \\snippet{{doc}} should appear twice in file {}, found it {:d} times, skipping",
1909 blockId,absFileName,count);
1910 return false;
1911 }
1912 lineNr = lineBlock(incText, blockId);
1913 int blockPos = 0;
1914 incText = extractBlock(incText, blockId, blockPos);
1915 fs->fileBuf.clear();
1916 if (!incText.isEmpty())
1917 {
1918 fs->fileBuf.append(incText.str());
1919 }
1920 }
1921 int oldRaiseLevel = yyextra->raiseLevel;
1922 QCString oldRaiseLabel = yyextra->raiseLabel;
1923 yyextra->raiseLevel+=yyextra->raiseIncrement;
1924 yyextra->raiseLabel+=yyextra->raisePrefix;
1925 QCString lineStr=" \\ifile \""+absFileName+"\" \\iline " + std::to_string(lineNr)+" ";
1926 if (yyextra->raiseLevel>0)
1927 {
1928 lineStr+="\\iraise " + std::to_string(yyextra->raiseLevel)+" ";
1929 }
1930 if (!yyextra->raiseLabel.isEmpty())
1931 {
1932 lineStr+="\\iprefix \"" + yyextra->raiseLabel + "\" ";
1933 }
1934 lineStr+="\\ilinebr ";
1935 copyToOutput(yyscanner,lineStr.view());
1936
1937 fs->fileName = absFileName;
1938 fs->bufState = YY_CURRENT_BUFFER;
1939 fs->oldLineNr = yyextra->lineNr;
1940 fs->oldFileName = yyextra->fileName;
1941 fs->oldState = yyextra->includeCtx;
1942 fs->oldFileBuf = yyextra->inBuf;
1943 fs->oldFileBufPos = yyextra->inBufPos;
1944 fs->oldIncludeCtx = yyextra->includeCtx;
1945 fs->oldRaiseLvl = oldRaiseLevel;
1946 fs->oldRaiseLbl = oldRaiseLabel;
1947 fs->blockId = blockId;
1948 yy_switch_to_buffer(yy_create_buffer(nullptr, YY_BUF_SIZE, yyscanner),yyscanner);
1949 yyextra->fileName = absFileName;
1950 yyextra->lineNr = lineNr;
1951 yyextra->inBuf = &fs->fileBuf;
1952 yyextra->inBufPos = 0;
1953 yyextra->includeStack.push_back(std::move(fs));
1954 insertCommentStart(yyscanner);
1955 //printf("switched to %s\n",qPrint(fileName));
1956 }
1957 else
1958 {
1959 warn_doc_error(yyextra->fileName,yyextra->lineNr,"\\{}{{doc}} file '{}' not found",blockId.isEmpty()?"include":"snippet",inc);
1960 return false;
1961 }
1962 return true;
1963}
1964
1965/** copies string \a s with length \a len to the output, while
1966 * replacing any alias commands found in the string.
1967 */
1968static void replaceAliases(yyscan_t yyscanner,std::string_view s,bool replaceCppComment,bool replaceCComment)
1969{
1970 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1971 if (s.empty()) return;
1972 size_t pos = s.find('{');
1973 std::string cmd { s.substr(1, pos!=std::string::npos ? pos-1 : s.length()-1) };
1974 auto &expAlias = yyextra->expandedAliases;
1975 if (std::find(expAlias.begin(),expAlias.end(),cmd)!=std::end(expAlias))
1976 {
1977 copyToOutput(yyscanner,s);
1978 return; // prevent recursive expansion
1979 }
1980 else if (cmd=="ialias")
1981 {
1982 if (s.length()>cmd.length()+3) // \cmd{value}
1983 {
1984 std::string value { s.substr(cmd.length()+2,s.length()-cmd.length()-3) };
1985 //printf("removing value '%s'\n",qPrint(value));
1986 expAlias.erase(std::remove(expAlias.begin(),expAlias.end(),value),expAlias.end());
1987 }
1988 return;
1989 }
1990 std::string result = resolveAliasCmd(s);
1991 if (yyextra->inVerbatim) // inside verbatim blocks (like @code...@endcode) only expand aliases if
1992 // their expansion contains the matching end block marker.
1993 {
1994 std::string blk = yyextra->blockName.str();
1995 assert(!blk.empty());
1996 bool isNamedCommand=isId(blk[0]); // true for e.g. @endcode, false for e.g. ~~~
1997 size_t i=0,p=0;
1998 bool found=false;
1999 while ((i=result.find(blk,p))!=std::string::npos && !found) // for each match of blk in result
2000 {
2001 found = !isNamedCommand || // e.g. '~~~' or '-->'
2002 (i>0 && (result[i-1]=='\\' || result[i-1]=='@') && !isId(result[i+blk.length()])); // e.g. '@endcode' but not ~endcode or @endcodex
2003 p = i+yyextra->blockName.length();
2004 }
2005 //printf("blk=%s result=%s found=%d\n",qPrint(blk),qPrint(result),found);
2006 if (!found) // treat alias as part of the verbatim block
2007 {
2008 copyToOutput(yyscanner,s);
2009 return;
2010 }
2011 }
2012 //printf("replaceAliases(%s)->'%s' replaceCppComment=%d replaceCComment=%d\n",qPrint(s),qPrint(result),replaceCppComment,replaceCComment);
2013 if (result!=s)
2014 {
2015 size_t numSpaces = yyextra->blockHeadCol>1 ? yyextra->blockHeadCol-1 : 0;
2016 std::string spaces(numSpaces,' ');
2017 if (replaceCppComment) // In case we are replacing a multiline /// comment by a C style comment
2018 // and we have new lines in the alias argument, we need to place back a /// for each new line
2019 // to prevent breaking the multiline comment into multiple C style comments
2020 {
2021 std::string replacement = "\n"+spaces+"///";
2022 result = substituteStringView(result,"\n",replacement);
2023 }
2024 else if (replaceCComment)
2025 {
2026 std::string replacement = "\n"+spaces+" * ";
2027 result = substituteStringView(result,"\n",replacement);
2028 }
2029 expAlias.push_back(cmd);
2030 // add a ialias command to allow expansion of cmd again
2031 result += " \\ilinebr \\ialias{";
2032 result += cmd;
2033 result += "}";
2034 for (int i=(int)result.length()-1; i>=0; i--)
2035 {
2036 unput(result[i]);
2037 }
2038 }
2039 else
2040 {
2041 copyToOutput(yyscanner,result);
2042 }
2043}
2044
2045
2046static int yyread(yyscan_t yyscanner,char *buf,int max_size)
2047{
2048 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2049 int bytesInBuf = static_cast<int>(yyextra->inBuf->size())-yyextra->inBufPos;
2050 int bytesToCopy = std::min(max_size,bytesInBuf);
2051 memcpy(buf,yyextra->inBuf->data()+yyextra->inBufPos,bytesToCopy);
2052 yyextra->inBufPos+=bytesToCopy;
2053 return bytesToCopy;
2054}
2055
2056static void replaceComment(yyscan_t yyscanner,int offset)
2057{
2058 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2059 if (yyextra->mlBrief || yyextra->skip)
2060 {
2061 copyToOutput(yyscanner,yytext,yyleng);
2062 }
2063 else
2064 {
2065 int i=computeIndent(&yytext[offset]);
2066 //printf("i=%d blockHeadCol=%d\n",i,yyextra->blockHeadCol);
2067 if (i==yyextra->blockHeadCol || i+1==yyextra->blockHeadCol)
2068 {
2069 replaceCommentMarker(yyscanner,std::string_view(yytext,yyleng));
2070 }
2071 else
2072 {
2073 copyToOutput(yyscanner," */");
2074 for (i=(int)yyleng-1;i>=0;i--) unput(yytext[i]);
2075 yyextra->inSpecialComment=FALSE;
2076 BEGIN(Scan);
2077 }
2078 }
2079}
2080
2081/*! This function does three things:
2082 * -# It converts multi-line C++ style comment blocks (that are aligned)
2083 * to C style comment blocks (if MULTILINE_CPP_IS_BRIEF is set to NO).
2084 * -# It replaces aliases with their definition (see ALIASES)
2085 * -# It handles conditional sections (cond...endcond blocks)
2086 */
2087void convertCppComments(const std::string &inBuf,std::string &outBuf,const std::string &fn)
2088{
2089 QCString fileName { fn };
2090 yyscan_t yyscanner;
2091 commentcnvYY_state extra(&inBuf,outBuf);
2092 commentcnvYYlex_init_extra(&extra,&yyscanner);
2093#ifdef FLEX_DEBUG
2094 commentcnvYYset_debug(Debug::isFlagSet(Debug::Lex_commentcnv)?1:0,yyscanner);
2095#endif
2096 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2097 //printf("convertCppComments(%s)\n",qPrint(fileName));
2098 yyextra->inBufPos = 0;
2099 yyextra->col = 0;
2100 yyextra->mlBrief = Config_getBool(MULTILINE_CPP_IS_BRIEF);
2101 yyextra->skip = FALSE;
2102 yyextra->fileName = fileName;
2103 yyextra->lang = getLanguageFromFileName(fileName);
2104 yyextra->pythonDocString = FALSE;
2105 yyextra->lineNr = 1;
2106 yyextra->raiseLevel = 0;
2107 yyextra->raiseLabel = "";
2108 yyextra->raiseIncrement = 0;
2109 yyextra->raisePrefix = "";
2110 yyextra->insertCppCommentMarker=false;
2111 yyextra->expandedAliases.clear();
2112 while (!yyextra->condStack.empty()) yyextra->condStack.pop();
2113 clearCommentStack(yyscanner);
2114 yyextra->vhdl = FALSE;
2115
2116 DebugLex debugLex(Debug::Lex_commentcnv,__FILE__, qPrint(fileName));
2117 yyextra->isFixedForm = FALSE;
2118 if (yyextra->lang==SrcLangExt::Fortran)
2119 {
2121 yyextra->isFixedForm = recognizeFixedForm(inBuf,fmt);
2122 }
2123
2124 if (yyextra->lang==SrcLangExt::Markdown)
2125 {
2126 yyextra->nestingCount=0;
2127 BEGIN(CComment);
2128 yyextra->commentStack.push(yyextra->lineNr);
2129 }
2130 else
2131 {
2132 BEGIN(Scan);
2133 }
2134 yylex(yyscanner);
2135 while (!yyextra->condStack.empty())
2136 {
2137 const commentcnvYY_CondCtx &ctx = yyextra->condStack.top();
2138 QCString sectionInfo(" ");
2139 if (ctx.sectionId!=" ") sectionInfo.sprintf(" with label '%s' ",ctx.sectionId.stripWhiteSpace().data());
2140 warn(yyextra->fileName,ctx.lineNr,"Conditional section{}does not have "
2141 "a corresponding \\endcond command within this file.",sectionInfo.data());
2142 yyextra->condStack.pop();
2143 }
2144 if (yyextra->nestingCount>0 && yyextra->lang!=SrcLangExt::Markdown && yyextra->lang!=SrcLangExt::Fortran)
2145 {
2146 QCString lines;
2147 bool first = true;
2148 while (!yyextra->commentStack.empty())
2149 {
2150 int lineNr = yyextra->commentStack.top();
2151 if (!first) lines += ", ";
2152 lines += QCString().setNum(lineNr);
2153 first = false;
2154 yyextra->commentStack.pop();
2155 }
2156 warn(yyextra->fileName,yyextra->lineNr,"Reached end of file while still inside a (nested) comment. "
2157 "Nesting level {} (possible line reference(s): {})",yyextra->nestingCount,lines);
2158 }
2159 yyextra->nestingCount = 0;
2161 {
2162 Debug::print(Debug::CommentCnv,0,"-----------\nCommentCnv: {}\n"
2163 "output=[\n{}]\n-----------\n",fileName,yyextra->outBuf
2164 );
2165 }
2166 commentcnvYYlex_destroy(yyscanner);
2167}
2168
2169
2170//----------------------------------------------------------------------------
2171
2172#include "commentcnv.l.h"
std::string resolveAliasCmd(std::string_view aliasCmd)
Definition aliases.cpp:494
Copyright (C) 1997-2015 by Dimitri van Heesch.
Definition condparser.h:28
bool parse(const QCString &fileName, int lineNr, const QCString &expr)
Copyright (C) 1997-2015 by Dimitri van Heesch.
@ Lex_commentcnv
Definition debug.h:53
@ CommentCnv
Definition debug.h:31
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:132
static void print(DebugMask mask, int prio, fmt::format_string< Args... > fmt, Args &&... args)
Definition debug.h:76
static FileNameLinkedMap * exampleNameLinkedMap
Definition doxygen.h:102
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
int find(char c, int index=0, bool cs=TRUE) const
Definition qcstring.cpp:43
const std::string & str() const
Definition qcstring.h:552
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:172
int contains(char c, bool cs=TRUE) const
Definition qcstring.cpp:148
static constexpr int MaxLevel
Definition section.h:39
void convertCppComments(const std::string &inBuf, std::string &outBuf, const std::string &fn)
Converts the comments in a file.
static QCString extractBlock(const QCString &text, const QCString &marker, int &blockPos)
Returns the section of text, in between a pair of markers.
#define YY_BUF_SIZE
Definition commentcnv.l:19
#define Config_getInt(name)
Definition config.h:34
int yylex(void)
std::vector< std::string > StringVector
Definition containers.h:33
#define warn_doc_error(file, line, fmt,...)
Definition message.h:112
Definition message.h:144
const char * qPrint(const char *s)
Definition qcstring.h:687
std::string substituteStringView(std::string_view s, std::string_view toReplace, std::string_view replaceWith)
Returns a new string where occurrences of substring toReplace in string s are replaced by string repl...
Definition stringutil.h:50
FortranFormat
Definition types.h:572
QCString findFilePath(const QCString &file, bool &ambig)
Definition util.cpp:2923
SrcLangExt getLanguageFromFileName(const QCString &fileName, SrcLangExt defLang)
Definition util.cpp:5147
int lineBlock(const QCString &text, const QCString &marker)
Returns the line number of the line following the line with the marker.
Definition util.cpp:5822
bool readInputFile(const QCString &fileName, std::string &contents, bool filter, bool isSourceCode)
read a file name fileName and optionally filter and transcode it
Definition util.cpp:5486
bool recognizeFixedForm(const QCString &contents, FortranFormat format)
Definition util.cpp:6293
QCString showFileDefMatches(const FileNameLinkedMap *fnMap, const QCString &n)
Definition util.cpp:2966
StringVector split(const std::string &s, const std::string &delimiter)
split input string s by string delimiter delimiter.
Definition util.cpp:6568
FortranFormat convertFileNameFortranParserCode(QCString fn)
Definition util.cpp:6346
bool isId(int c)
Definition util.h:207