Doxygen
Loading...
Searching...
No Matches
xmlcode.l
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2020 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/******************************************************************************
16 * Parser for syntax highlighting and references for XML
17 * written by Weston Thayer
18 ******************************************************************************/
19
20%option never-interactive
21%option prefix="xmlcodeYY"
22%option reentrant
23%option extra-type="struct xmlcodeYY_state *"
24%option noyy_top_state
25%option nounput
26%option noyywrap
27%top{
28#include <stdint.h>
29// forward declare yyscan_t to improve type safety
30#define YY_TYPEDEF_YY_SCANNER_T
31#define yyguts_t xmlcodeYYguts_t
32struct yyguts_t;
yyguts_t * yyscan_t
Definition code.l:25
#define yyguts_t
Definition code.l:23
34}
35
36%{
37
38// own header
39#include "xmlcode.h"
40
41// standard includes
42#include <memory>
43
44// other includes
45#include "config.h"
46#include "debug.h"
47#include "doxygen.h"
48#include "filedef.h"
49#include "message.h"
50#include "outputlist.h"
51#include "searchindex.h"
52#include "util.h"
53
54#define YY_NEVER_INTERACTIVE 1
55#define YY_NO_INPUT 1
56#define YY_NO_UNISTD_H 1
57
59{
64 const char * inputString = nullptr; //!< the code fragment as text
65 int inputPosition = 0; //!< read offset during parsing
67 int inputLines = 0; //!< number of line in the code fragment
68 int yyLineNr = 0; //!< current line number
69 bool insideCodeLine = false;
70 const Definition *searchCtx = nullptr;
71
72 bool stripCodeComments = true;
73 bool exampleBlock = false;
76
81
83
84 std::unique_ptr<FileDef> exampleFileDef;
85 const FileDef * sourceFileDef = nullptr;
86 bool lineNumbers = false;
87 const Definition * currentDefinition = nullptr;
88 const MemberDef * currentMemberDef = nullptr;
89 bool includeCodeFragment = false;
90 const char * currentFontClass = nullptr;
91};
92
93[[maybe_unused]] static const char *stateToString(int state);
94
95static void codify(yyscan_t yyscanner,const char* text);
96static void setCurrentDoc(yyscan_t yyscanner,const DString &anchor);
97static void startCodeLine(yyscan_t yyscanner);
98static void endFontClass(yyscan_t yyscanner);
99static void endCodeLine(yyscan_t yyscanner);
100static void nextCodeLine(yyscan_t yyscanner);
101static void codifyLines(yyscan_t yyscanner,const char *text);
102static void startFontClass(yyscan_t yyscanner,const char *s);
103static int countLines(yyscan_t yyscanner);
104static int yyread(yyscan_t yyscanner,char *buf,int max_size);
105
106#undef YY_INPUT
107#define YY_INPUT(buf,result,max_size) result=yyread(yyscanner,buf,max_size);
108
109// otherwise the filename would be the name of the converted file (*.cpp instead of *.l)
110static inline const char *getLexerFILE() {return __FILE__;}
111#include "doxygen_lex.h"
112
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
The common base class of all entity definitions found in the sources.
Definition definition.h:77
A model of a file symbol.
Definition filedef.h:97
A model of a class/file/namespace member symbol.
Definition memberdef.h:45
Class representing a list of different code generators.
Definition outputlist.h:162
Web server based search engine.
bool lineNumbers
Definition xmlcode.l:86
DString curClassName
Definition xmlcode.l:61
DString CurrScope
Definition xmlcode.l:82
DString args
Definition xmlcode.l:79
const char * currentFontClass
Definition xmlcode.l:90
OutputCodeList * code
Definition xmlcode.l:60
const Definition * currentDefinition
Definition xmlcode.l:87
const FileDef * sourceFileDef
Definition xmlcode.l:85
DString classScope
Definition xmlcode.l:80
const MemberDef * currentMemberDef
Definition xmlcode.l:88
int yyLineNr
current line number
Definition xmlcode.l:68
int inputPosition
read offset during parsing
Definition xmlcode.l:65
std::unique_ptr< FileDef > exampleFileDef
Definition xmlcode.l:84
DString type
Definition xmlcode.l:77
DString exampleFile
Definition xmlcode.l:75
DString exampleName
Definition xmlcode.l:74
const char * inputString
the code fragment as text
Definition xmlcode.l:64
DString parmName
Definition xmlcode.l:63
int inputLines
number of line in the code fragment
Definition xmlcode.l:67
bool includeCodeFragment
Definition xmlcode.l:89
DString name
Definition xmlcode.l:78
DString parmType
Definition xmlcode.l:62
bool stripCodeComments
Definition xmlcode.l:72
const Definition * searchCtx
Definition xmlcode.l:70
bool exampleBlock
Definition xmlcode.l:73
bool insideCodeLine
Definition xmlcode.l:69
DString fileName
Definition xmlcode.l:66
A bunch of utility functions.
static void endCodeLine(yyscan_t yyscanner)
Definition xmlcode.l:312
static void nextCodeLine(yyscan_t yyscanner)
Definition xmlcode.l:320
static void startCodeLine(yyscan_t yyscanner)
Definition xmlcode.l:254
static void setCurrentDoc(yyscan_t yyscanner, const DString &anchor)
Definition xmlcode.l:234
static int yyread(yyscan_t yyscanner, char *buf, int max_size)
Definition xmlcode.l:213
static int countLines(yyscan_t yyscanner)
Definition xmlcode.l:369
static const char * stateToString(int state)
static void startFontClass(yyscan_t yyscanner, const char *s)
Definition xmlcode.l:360
static void codifyLines(yyscan_t yyscanner, const char *text)
Definition xmlcode.l:335
static const char * getLexerFILE()
Definition xmlcode.l:110
static void codify(yyscan_t yyscanner, const char *text)
Definition xmlcode.l:228
static void endFontClass(yyscan_t yyscanner)
Definition xmlcode.l:302
113%}
114
115nl (\r\n|\r|\n)
116ws [ \t]+
117open "<"
118close ">"
119namestart [A-Za-z\200-\377_]
120namechar [:A-Za-z\200-\377_0-9.-]
121esc "&#"[0-9]+";"|"&#x"[0-9a-fA-F]+";"
122name {namestart}{namechar}*
123comment {open}"!--"([^-]|"-"[^-])*"--"{close}
124cdata {open}"![CDATA["([^\‍]]|"\‍]"[^\‍]])*"]]"{close}
125string \"([^\"&]|{esc})*\"|\'([^'&]|{esc})*\'
126
127%option noyywrap
128%option nounput
129
131
132<INITIAL>{ws} {
133 codifyLines(yyscanner,yytext);
134 }
135<INITIAL>"/" {
136 endFontClass(yyscanner);
137 codify(yyscanner,yytext);
138 }
139<INITIAL>"=" {
140 endFontClass(yyscanner);
141 codify(yyscanner,yytext);
142 }
143<INITIAL>{close} {
144 endFontClass(yyscanner);
145 codify(yyscanner,yytext);
146 }
147<INITIAL>{name} {
148 startFontClass(yyscanner,"keyword");
149 codify(yyscanner,yytext);
150 endFontClass(yyscanner);
151 }
152<INITIAL>{string} {
153 startFontClass(yyscanner,"stringliteral");
154 codifyLines(yyscanner,yytext);
155 endFontClass(yyscanner);
156 }
157{cdata} {
158 startFontClass(yyscanner,"xmlcdata");
159 codifyLines(yyscanner,yytext);
160 endFontClass(yyscanner);
161 }
162
163{open}{ws}?{name} {
164 // Write the < in a different color
165 char openBracket[] = { yytext[0], '\0' };
166 codify(yyscanner,openBracket);
167
168 // Then write the rest
169 yytext++;
170 startFontClass(yyscanner,"keywordtype");
171 codify(yyscanner,yytext);
172 endFontClass(yyscanner);
173
174 BEGIN(INITIAL);
175 }
176{open}{ws}?"/"{name} {
177 // Write the "</" in a different color
178 char closeBracket[] = { yytext[0], yytext[1], '\0' };
179 endFontClass(yyscanner);
180 codify(yyscanner,closeBracket);
181
182 // Then write the rest
183 yytext++; // skip the '<'
184 yytext++; // skip the '/'
185 startFontClass(yyscanner,"keywordtype");
186 codify(yyscanner,yytext);
187 endFontClass(yyscanner);
188
189 BEGIN(INITIAL);
190 }
191{comment} {
192 // Strip off the extra '!'
193 // yytext++; // <
194 // *yytext = '<'; // replace '!' with '<'
195
196 startFontClass(yyscanner,"comment");
197 codifyLines(yyscanner,yytext);
198 endFontClass(yyscanner);
199 }
200{nl} {
201 codifyLines(yyscanner,yytext);
202 }
203
204. {
205 //printf("!ERROR(%c)\n", *yytext);
206 codifyLines(yyscanner,yytext);
207 }
208
209%%
210
211//----------------------------------------------------------------------------------------
212
213static int yyread(yyscan_t yyscanner,char *buf,int max_size)
214{
215 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
216 int inputPosition = yyextra->inputPosition;
217 const char *s = yyextra->inputString + inputPosition;
218 int c=0;
219 while( c < max_size && *s)
220 {
221 *buf++ = *s++;
222 c++;
223 }
224 yyextra->inputPosition += c;
225 return c;
226}
227
228static void codify(yyscan_t yyscanner,const char* text)
229{
230 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
231 yyextra->code->codify(text);
232}
233
234static void setCurrentDoc(yyscan_t yyscanner,const DString &anchor)
235{
236 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
237 if (Doxygen::searchIndex.enabled())
238 {
239 if (yyextra->searchCtx)
240 {
241 Doxygen::searchIndex.setCurrentDoc(yyextra->searchCtx,yyextra->searchCtx->anchor(),false);
242 }
243 else
244 {
245 Doxygen::searchIndex.setCurrentDoc(yyextra->sourceFileDef,anchor,true);
246 }
247 }
248}
249
250/*! start a new line of code, inserting a line number if yyextra->sourceFileDef
251 * is true. If a definition starts at the current line, then the line
252 * number is linked to the documentation of that definition.
253 */
254static void startCodeLine(yyscan_t yyscanner)
255{
256 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
257 if (yyextra->sourceFileDef && yyextra->lineNumbers)
258 {
259 const Definition *d = yyextra->sourceFileDef->getSourceDefinition(yyextra->yyLineNr);
260
261 if (!yyextra->includeCodeFragment && d && d->isLinkableInProject())
262 {
263 yyextra->currentDefinition = d;
264 yyextra->currentMemberDef = yyextra->sourceFileDef->getSourceMember(yyextra->yyLineNr);
265 //yyextra->insideBody = false;
266 yyextra->classScope = d->name();
267 DString lineAnchor;
268 lineAnchor.sprintf("l%05d",yyextra->yyLineNr);
269 if (yyextra->currentMemberDef)
270 {
271 yyextra->code->writeLineNumber(yyextra->currentMemberDef->getReference(),
272 yyextra->currentMemberDef->getOutputFileBase(),
273 yyextra->currentMemberDef->anchor(),yyextra->yyLineNr,
274 !yyextra->includeCodeFragment);
275 setCurrentDoc(yyscanner,lineAnchor);
276 }
277 else
278 {
279 yyextra->code->writeLineNumber(d->getReference(),
281 DString(),yyextra->yyLineNr,
282 !yyextra->includeCodeFragment);
283 setCurrentDoc(yyscanner,lineAnchor);
284 }
285 }
286 else
287 {
288 yyextra->code->writeLineNumber(DString(),DString(),DString(),yyextra->yyLineNr,
289 !yyextra->includeCodeFragment);
290 }
291 }
292
293 yyextra->code->startCodeLine(yyextra->yyLineNr);
294 yyextra->insideCodeLine = true;
295
296 if (yyextra->currentFontClass)
297 {
298 yyextra->code->startFontClass(yyextra->currentFontClass);
299 }
300}
301
302static void endFontClass(yyscan_t yyscanner)
303{
304 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
305 if (yyextra->currentFontClass)
306 {
307 yyextra->code->endFontClass();
308 yyextra->currentFontClass=0;
309 }
310}
311
312static void endCodeLine(yyscan_t yyscanner)
313{
314 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
315 endFontClass(yyscanner);
316 yyextra->code->endCodeLine();
317 yyextra->insideCodeLine = false;
318}
319
320static void nextCodeLine(yyscan_t yyscanner)
321{
322 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
323 const char *fc = yyextra->currentFontClass;
324 if (yyextra->insideCodeLine)
325 {
326 endCodeLine(yyscanner);
327 }
328 if (yyextra->yyLineNr<yyextra->inputLines)
329 {
330 yyextra->currentFontClass = fc;
331 startCodeLine(yyscanner);
332 }
333}
334
335static void codifyLines(yyscan_t yyscanner,const char *text)
336{
337 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
338 const char *p=text,*sp=p;
339 char c;
340 bool done=false;
341 while (!done)
342 {
343 sp=p;
344 while ((c=*p++) && c!='\n') { }
345 if (c=='\n')
346 {
347 yyextra->yyLineNr++;
348 size_t l = static_cast<size_t>(p-sp-1);
349 yyextra->code->codify(DString(sp,l));
350 nextCodeLine(yyscanner);
351 }
352 else
353 {
354 yyextra->code->codify(sp);
355 done=true;
356 }
357 }
358}
359
360static void startFontClass(yyscan_t yyscanner,const char *s)
361{
362 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
363 endFontClass(yyscanner);
364 yyextra->code->startFontClass(s);
365 yyextra->currentFontClass=s;
366}
367
368/*! counts the number of lines in the input */
369static int countLines(yyscan_t yyscanner)
370{
371 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
372 const char *p=yyextra->inputString;
373 char c;
374 int count=1;
375 while ((c=*p))
376 {
377 p++ ;
378 if (c=='\n') count++;
379 }
380 if (p>yyextra->inputString && *(p-1)!='\n')
381 { // last line does not end with a \n, so we add an extra
382 // line and explicitly terminate the line after parsing.
383 count++;
384 }
385 return count;
386}
387
388//----------------------------------------------------------------------------------------
389
395
397{
398 xmlcodeYYlex_init_extra(&p->state,&p->yyscanner);
399#ifdef FLEX_DEBUG
400 xmlcodeYYset_debug(Debug::isFlagSet(Debug::Lex_xmlcode)?1:0,p->yyscanner);
401#endif
403}
404
406{
407 xmlcodeYYlex_destroy(p->yyscanner);
408}
409
411{
412 struct yyguts_t *yyg = (struct yyguts_t*)p->yyscanner;
413 yyextra->currentDefinition = nullptr;
414 yyextra->currentMemberDef = nullptr;
415}
416
418 const DString &/* scopeName */,
419 const DString &input,
421 bool stripCodeComments,
422 const CodeParserOptions &options
423 )
424{
425 yyscan_t yyscanner = p->yyscanner;
426 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
427
428 if (input.empty()) return;
429
430 DebugLex debugLex(Debug::Lex_xmlcode, __FILE__, options.fileDef() ? qPrint(options.fileDef()->fileName()): nullptr);
431 yyextra->fileName = options.fileDef() ? options.fileDef()->fileName():"";
432 yyextra->code = &codeOutIntf;
433 yyextra->inputString = input.data();
434 yyextra->inputPosition = 0;
435 yyextra->currentFontClass = nullptr;
436 yyextra->insideCodeLine = false;
437 yyextra->searchCtx = options.searchCtx();
438 yyextra->yyLineNr = options.startLine()!=-1 ? options.startLine() : 1;
439 yyextra->inputLines = options.endLine()!=-1 ? options.endLine()+1 : yyextra->yyLineNr + countLines(yyscanner) - 1;
440 yyextra->stripCodeComments = stripCodeComments;
441 yyextra->exampleBlock = options.isExample();
442 yyextra->exampleName = options.exampleName();
443 yyextra->sourceFileDef = options.fileDef();
444 yyextra->lineNumbers = options.fileDef() && options.showLineNumbers();
445
446 if (options.isExample() && options.fileDef()==nullptr)
447 {
448 // create a dummy filedef for the example
449 yyextra->exampleFileDef = createFileDef("",(!options.exampleName().empty()?options.exampleName():DString("generated")));
450 yyextra->sourceFileDef = yyextra->exampleFileDef.get();
451 }
452
453 if (yyextra->sourceFileDef)
454 {
455 setCurrentDoc(yyscanner,"l00001");
456 }
457
458 yyextra->includeCodeFragment = options.inlineFragment();
459 // Starts line 1 on the output
460 startCodeLine(yyscanner);
461
462 xmlcodeYYrestart( nullptr, yyscanner );
463
464 xmlcodeYYlex(yyscanner);
465
466 if (yyextra->insideCodeLine)
467 {
468 endCodeLine(yyscanner);
469 }
470 if (yyextra->exampleFileDef)
471 {
472 // delete the temporary file definition used for this example
473 yyextra->exampleFileDef.reset();
474 yyextra->sourceFileDef=nullptr;
475 }
476}
477
478
479#include "xmlcode.l.h"
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
DString & sprintf(const char *format,...)
Definition dstring.cpp:34
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
@ Lex_xmlcode
Definition debug.h:73
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:132
virtual const DString & name() const =0
virtual bool isLinkableInProject() const =0
virtual DString getReference() const =0
virtual DString getOutputFileBase() const =0
static SearchIndexIntf searchIndex
Definition doxygen.h:117
virtual DString fileName() const =0
void setCurrentDoc(const Definition *ctx, const DString &anchor, bool isSourceFile)
std::unique_ptr< Private > p
Definition xmlcode.h:42
~XMLCodeParser() override
Definition xmlcode.l:405
void resetCodeParserState() override
Resets the state of the code parser.
Definition xmlcode.l:410
void parseCode(OutputCodeList &codeOutIntf, const DString &scopeName, const DString &input, SrcLangExt, bool stripCodeComments, const CodeParserOptions &options) override
Parses a source file or fragment with the goal to produce highlighted and cross-referenced output.
Definition xmlcode.l:417
const char * qPrint(const char *s)
Definition dstring.h:783
std::unique_ptr< FileDef > createFileDef(const DString &p, const DString &n, const DString &ref, const DString &dn)
Definition filedef.cpp:270
Definition dstring.h:913
Options to configure the code parser.
Definition parserintf.h:77
bool isExample() const
Definition parserintf.h:80
const Definition * searchCtx() const
Definition parserintf.h:88
DString exampleName() const
Definition parserintf.h:81
const FileDef * fileDef() const
Definition parserintf.h:82
int endLine() const
Definition parserintf.h:84
bool showLineNumbers() const
Definition parserintf.h:87
bool inlineFragment() const
Definition parserintf.h:85
int startLine() const
Definition parserintf.h:83
xmlcodeYY_state state
Definition xmlcode.l:393
SrcLangExt
Definition types.h:207