Doxygen
Loading...
Searching...
No Matches
sqlcode.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%option never-interactive
17%option prefix="sqlcodeYY"
18%option noyywrap
19%option nounput
20%option reentrant
21%option extra-type="struct sqlcodeYY_state *"
22%top{
23#include <stdint.h>
24// forward declare yyscan_t to improve type safety
25#define YY_TYPEDEF_YY_SCANNER_T
26#define yyguts_t sqlcodeYYguts_t
27struct yyguts_t;
yyguts_t * yyscan_t
Definition code.l:25
#define yyguts_t
Definition code.l:23
29}
30
31%{
32
33// own header
34#include "sqlcode.h"
35
36// stanrd includes
37#include <memory>
38
39// other includes
40#include "config.h"
41#include "debug.h"
42#include "doxygen.h"
43#include "filedef.h"
44#include "message.h"
45#include "outputlist.h"
46#include "searchindex.h"
47#include "util.h"
48
49#define YY_NEVER_INTERACTIVE 1
50#define YY_NO_INPUT 1
51#define YY_NO_UNISTD_H 1
52
54{
55 OutputCodeList * code = nullptr;
56 const char *inputString = nullptr; //!< the code fragment as text
57 int inputPosition = 0; //!< read offset during parsing
59 int inputLines = 0; //!< number of line in the code fragment
60 int yyLineNr = 1; //!< current line number
61 bool insideCodeLine = false;
62 const Definition *searchCtx = nullptr;
63
64 bool stripCodeComments = true;
65 bool exampleBlock = false;
68
69 std::unique_ptr<FileDef> exampleFileDef;
70 const FileDef *sourceFileDef = nullptr;
71 bool lineNumbers = false;
72 const Definition *currentDefinition = nullptr;
73 const MemberDef *currentMemberDef = nullptr;
74 bool includeCodeFragment = false;
75 const char *currentFontClass = nullptr;
76};
77
78[[maybe_unused]] static const char *stateToString(int state);
79
80static void setCurrentDoc(yyscan_t yyscanner,const DString &anchor);
81static void startCodeLine(yyscan_t yyscanner);
82static void endFontClass(yyscan_t yyscanner);
83static void endCodeLine(yyscan_t yyscanner);
84static void nextCodeLine(yyscan_t yyscanner);
85static void codifyLines(yyscan_t yyscanner,const char *text);
86static void startFontClass(yyscan_t yyscanner,const char *s);
87static int countLines(yyscan_t yyscanner);
88static int yyread(yyscan_t yyscanner,char *buf,int max_size);
89
90#undef YY_INPUT
91#define YY_INPUT(buf,result,max_size) result=yyread(yyscanner,buf,max_size);
92
93// otherwise the filename would be the name of the converted file (*.cpp instead of *.l)
94static inline const char *getLexerFILE() {return __FILE__;}
95#include "doxygen_lex.h"
96
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.
static void endCodeLine(yyscan_t yyscanner)
Definition sqlcode.l:289
static void nextCodeLine(yyscan_t yyscanner)
Definition sqlcode.l:297
static void startCodeLine(yyscan_t yyscanner)
Definition sqlcode.l:232
static void setCurrentDoc(yyscan_t yyscanner, const DString &anchor)
Definition sqlcode.l:212
static int yyread(yyscan_t yyscanner, char *buf, int max_size)
Definition sqlcode.l:365
static int countLines(yyscan_t yyscanner)
Definition sqlcode.l:346
static const char * stateToString(int state)
static void startFontClass(yyscan_t yyscanner, const char *s)
Definition sqlcode.l:337
static void codifyLines(yyscan_t yyscanner, const char *text)
Definition sqlcode.l:312
static const char * getLexerFILE()
Definition sqlcode.l:94
static void endFontClass(yyscan_t yyscanner)
Definition sqlcode.l:279
const char * currentFontClass
Definition sqlcode.l:75
const Definition * searchCtx
Definition sqlcode.l:62
bool includeCodeFragment
Definition sqlcode.l:74
bool exampleBlock
Definition sqlcode.l:65
const char * inputString
the code fragment as text
Definition sqlcode.l:56
DString classScope
Definition sqlcode.l:67
const MemberDef * currentMemberDef
Definition sqlcode.l:73
DString exampleName
Definition sqlcode.l:66
DString fileName
Definition sqlcode.l:58
bool lineNumbers
Definition sqlcode.l:71
bool insideCodeLine
Definition sqlcode.l:61
int yyLineNr
current line number
Definition sqlcode.l:60
OutputCodeList * code
Definition sqlcode.l:55
int inputLines
number of line in the code fragment
Definition sqlcode.l:59
const FileDef * sourceFileDef
Definition sqlcode.l:70
std::unique_ptr< FileDef > exampleFileDef
Definition sqlcode.l:69
int inputPosition
read offset during parsing
Definition sqlcode.l:57
bool stripCodeComments
Definition sqlcode.l:64
const Definition * currentDefinition
Definition sqlcode.l:72
A bunch of utility functions.
97%}
98
99nl (\r\n|\r|\n)
100idchar [A-Za-z0-9\-_]+
101keywords1 ("ADD"|"ALL"|"ALLOCATE"|"ALTER"|"AND"|"ANY"|"ARE"|"AS"|"ASENSITIVE"|"ASYMMETRIC"|"AT"|"ATOMIC"|"AUTHORIZATION"|"BETWEEN"|"BOTH"|"BY"|"CALL"|"CALLED"|"CASCADED"|"CAST")
102keywords2 ("CHECK"|"CLOSE"|"COLLATE"|"COLUMN"|"COMMIT"|"CONNECT"|"CONSTRAINT"|"CONTINUE"|"CORRESPONDING"|"CREATE"|"CROSS"|"CUBE"|"CURRENT"|"CURRENT_DATE"|"CURRENT_DEFAULT_TRANSFORM_GROUP")
103keywords3 ("CURRENT_PATH"|"CURRENT_ROLE"|"CURRENT_TIME"|"CURRENT_TIMESTAMP"|"CURRENT_TRANSFORM_GROUP_FOR_TYPE"|"CURRENT_USER")
104keywords4 ("CURSOR"|"CYCLE"|"DAY"|"DEALLOCATE"|"DECLARE"|"DEFAULT"|"DELETE"|"DEREF"|"DESCRIBE"|"DETERMINISTIC"|"DISCONNECT"|"DISTINCT"|"DROP"|"DYNAMIC")
105keywords5 ("EACH"|"ELEMENT"|"END-EXEC"|"ESCAPE"|"EXCEPT"|"EXEC"|"EXECUTE"|"EXISTS"|"EXTERNAL"|"FETCH"|"FILTER"|"FOR"|"FOREIGN"|"FREE"|"FROM"|"FULL"|"FUNCTION")
106keywords6 ("GET"|"GLOBAL"|"GRANT"|"GROUP"|"GROUPING"|"HAVING"|"HOLD"|"HOUR"|"IDENTITY"|"IMMEDIATE"|"IN"|"INDICATOR"|"INNER"|"INOUT"|"INPUT"|"INSENSITIVE"|"INSERT"|"INTERSECT")
107keywords7 ("INTERVAL"|"INTO"|"IS"|"ISOLATION"|"JOIN"|"LANGUAGE"|"LARGE"|"LATERAL"|"LEADING"|"LEFT"|"LIKE"|"LOCAL"|"LOCALTIME"|"LOCALTIMESTAMP"|"MATCH"|"MEMBER"|"MERGE"|"METHOD"|"MINUTE")
108keywords8 ("MODIFIES"|"MODULE"|"MONTH"|"MULTISET"|"NATIONAL"|"NATURAL"|"NEW"|"NO"|"NONE"|"NOT"|"OF"|"OLD"|"ON"|"ONLY"|"OPEN"|"OR"|"ORDER"|"OUT"|"OUTER"|"OUTPUT")
109keywords9 ("OVER"|"OVERLAPS"|"PARAMETER"|"PARTITION"|"PRECISION"|"PREPARE"|"PRIMARY"|"PROCEDURE"|"RANGE"|"READS"|"RECURSIVE"|"REF"|"REFERENCES"|"REFERENCING"|"REGR_AVGX"|"REGR_AVGY")
110keywords10 ("REGR_COUNT"|"REGR_INTERCEPT"|"REGR_R2"|"REGR_SLOPE"|"REGR_SXX"|"REGR_SXY"|"REGR_SYY"|"RELEASE"|"RESULT"|"RETURN"|"RETURNS"|"REVOKE"|"RIGHT"|"ROLLBACK"|"ROLLUP"|"ROW"|"ROWS"|"SAVEPOINT")
111keywords11 ("SCROLL"|"SEARCH"|"SECOND"|"SELECT"|"SENSITIVE"|"SESSION_USER"|"SET"|"SIMILAR"|"SOME"|"SPECIFIC"|"SPECIFICTYPE"|"SQL"|"SQLEXCEPTION"|"SQLSTATE"|"SQLWARNING"|"START"|"STATIC")
112keywords12 ("SUBMULTISET"|"SYMMETRIC"|"SYSTEM"|"SYSTEM_USER"|"TABLE"|"THEN"|"TIMEZONE_HOUR"|"TIMEZONE_MINUTE"|"TO"|"TRAILING"|"TRANSLATION"|"TREAT"|"TRIGGER"|"UESCAPE"|"UNION")
113keywords13 ("UNIQUE"|"UNNEST"|"UPDATE"|"UPPER"|"USER"|"USING"|"VALUE"|"VALUES"|"VAR_POP"|"VAR_SAMP"|"VARYING"|"WHEN"|"WHENEVER"|"WHERE"|"WIDTH_BUCKET"|"WINDOW"|"WITH"|"WITHIN"|"WITHOUT"|"YEAR")
114
115/* Need multiple keyword definitions due to max length */
116keyword (?i:{keywords1}|{keywords2}|{keywords3}|{keywords4}|{keywords5}|{keywords6}|{keywords7}|{keywords8}|{keywords9}|{keywords10}|{keywords11}|{keywords12}|{keywords13})
117
118typekeyword (?i:"ARRAY"|"BIGINT"|"BINARY"|"BLOB"|"BOOLEAN"|"CHAR"|"CHARACTER"|"CLOB"|"DATE"|"DEC"|"DECIMAL"|"DOUBLE"|"FLOAT"|"INT"|"INTEGER"|"NCHAR"|"NCLOB"|"NUMERIC"|"NVARCHAR"|"REAL"|"SMALLINT"|"TIME"|"TIMESTAMP"|"VARCHAR")
119
120flowkeyword (?i:"CASE"|"IF"|"ELSE"|"BEGIN"|"END"|"WHILE")
121
122literalkeyword (?i:"false"|"true"|"NULL"|"UNKNOWN")
123stringliteral (\"[^\"]*\")|('[^']*')
124number [0-9]+
125literals ({literalkeyword}|{stringliteral}|{number})
126
127variable @{idchar}+
128
129simplecomment --.*
130commentopen "/\*"
131commentclose "\*\/"
132
133%x COMMENT
134
136
137{literals} {
138 startFontClass(yyscanner,"stringliteral");
139 codifyLines(yyscanner,yytext);
140 endFontClass(yyscanner);
141 }
142
143
144{keyword} {
145 startFontClass(yyscanner,"keyword");
146 codifyLines(yyscanner,yytext);
147 endFontClass(yyscanner);
148 }
149
150{flowkeyword} {
151 startFontClass(yyscanner,"keywordflow");
152 codifyLines(yyscanner,yytext);
153 endFontClass(yyscanner);
154 }
155
156{typekeyword} {
157 startFontClass(yyscanner,"keywordtype");
158 codifyLines(yyscanner,yytext);
159 endFontClass(yyscanner);
160 }
161
162{variable} {
163 startFontClass(yyscanner,"preprocessor");
164 codifyLines(yyscanner,yytext);
165 endFontClass(yyscanner);
166 }
167
168{simplecomment} {
169 startFontClass(yyscanner,"comment");
170 codifyLines(yyscanner,yytext);
171 endFontClass(yyscanner);
172 }
173
174{commentopen} {
175 startFontClass(yyscanner,"comment");
176 codifyLines(yyscanner,yytext);
177 BEGIN(COMMENT);
178 }
179
180<COMMENT>. {
181 codifyLines(yyscanner,yytext);
182
183 }
184<COMMENT>{nl} {
185 codifyLines(yyscanner,yytext);
186 }
187
188<COMMENT>{commentclose} {
189 codifyLines(yyscanner,yytext);
190 endFontClass(yyscanner);
191 BEGIN(INITIAL);
192 }
193
194{idchar} {
195 codifyLines(yyscanner,yytext);
196 }
197
198{nl} {
199 codifyLines(yyscanner,yytext);
200 }
201
202[\x80-\xFF]* { // keep utf8 characters together...
203 codifyLines(yyscanner,yytext);
204 }
205. {
206 codifyLines(yyscanner,yytext);
207 }
208
209%%
210
211
212static void setCurrentDoc(yyscan_t yyscanner,const DString &anchor)
213{
214 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
215 if (Doxygen::searchIndex.enabled())
216 {
217 if (yyextra->searchCtx)
218 {
219 Doxygen::searchIndex.setCurrentDoc(yyextra->searchCtx,yyextra->searchCtx->anchor(),false);
220 }
221 else
222 {
223 Doxygen::searchIndex.setCurrentDoc(yyextra->sourceFileDef,anchor,true);
224 }
225 }
226}
227
228/*! start a new line of code, inserting a line number if yyextra->sourceFileDef
229 * is true. If a definition starts at the current line, then the line
230 * number is linked to the documentation of that definition.
231 */
232static void startCodeLine(yyscan_t yyscanner)
233{
234 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
235 if (yyextra->sourceFileDef && yyextra->lineNumbers)
236 {
237 const Definition *d = yyextra->sourceFileDef->getSourceDefinition(yyextra->yyLineNr);
238
239 if (!yyextra->includeCodeFragment && d && d->isLinkableInProject())
240 {
241 yyextra->currentDefinition = d;
242 yyextra->currentMemberDef = yyextra->sourceFileDef->getSourceMember(yyextra->yyLineNr);
243 yyextra->classScope = d->name();
244 DString lineAnchor;
245 lineAnchor.sprintf("l%05d",yyextra->yyLineNr);
246 if (yyextra->currentMemberDef)
247 {
248 yyextra->code->writeLineNumber(yyextra->currentMemberDef->getReference(),
249 yyextra->currentMemberDef->getOutputFileBase(),
250 yyextra->currentMemberDef->anchor(),yyextra->yyLineNr,
251 !yyextra->includeCodeFragment);
252 setCurrentDoc(yyscanner,lineAnchor);
253 }
254 else
255 {
256 yyextra->code->writeLineNumber(d->getReference(),
258 DString(),yyextra->yyLineNr,
259 !yyextra->includeCodeFragment);
260 setCurrentDoc(yyscanner,lineAnchor);
261 }
262 }
263 else
264 {
265 yyextra->code->writeLineNumber(DString(),DString(),DString(),yyextra->yyLineNr,
266 !yyextra->includeCodeFragment);
267 }
268 }
269
270 yyextra->code->startCodeLine(yyextra->yyLineNr);
271 yyextra->insideCodeLine=true;
272
273 if (yyextra->currentFontClass)
274 {
275 yyextra->code->startFontClass(yyextra->currentFontClass);
276 }
277}
278
279static void endFontClass(yyscan_t yyscanner)
280{
281 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
282 if (yyextra->currentFontClass)
283 {
284 yyextra->code->endFontClass();
285 yyextra->currentFontClass=0;
286 }
287}
288
289static void endCodeLine(yyscan_t yyscanner)
290{
291 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
292 endFontClass(yyscanner);
293 yyextra->code->endCodeLine();
294 yyextra->insideCodeLine=false;
295}
296
297static void nextCodeLine(yyscan_t yyscanner)
298{
299 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
300 const char *fc = yyextra->currentFontClass;
301 if (yyextra->insideCodeLine)
302 {
303 endCodeLine(yyscanner);
304 }
305 if (yyextra->yyLineNr<yyextra->inputLines)
306 {
307 yyextra->currentFontClass = fc;
308 startCodeLine(yyscanner);
309 }
310}
311
312static void codifyLines(yyscan_t yyscanner,const char *text)
313{
314 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
315 const char *p=text,*sp=p;
316 char c;
317 bool done=false;
318 while (!done)
319 {
320 sp=p;
321 while ((c=*p++) && c!='\n') { }
322 if (c=='\n')
323 {
324 yyextra->yyLineNr++;
325 size_t l = static_cast<size_t>(p-sp-1);
326 yyextra->code->codify(DString(sp,l));
327 nextCodeLine(yyscanner);
328 }
329 else
330 {
331 yyextra->code->codify(sp);
332 done=true;
333 }
334 }
335}
336
337static void startFontClass(yyscan_t yyscanner,const char *s)
338{
339 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
340 endFontClass(yyscanner);
341 yyextra->code->startFontClass(s);
342 yyextra->currentFontClass=s;
343}
344
345/*! counts the number of lines in the input */
346static int countLines(yyscan_t yyscanner)
347{
348 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
349 const char *p=yyextra->inputString;
350 char c;
351 int count=1;
352 while ((c=*p))
353 {
354 p++ ;
355 if (c=='\n') count++;
356 }
357 if (p>yyextra->inputString && *(p-1)!='\n')
358 { // last line does not end with a \n, so we add an extra
359 // line and explicitly terminate the line after parsing.
360 count++;
361 }
362 return count;
363}
364
365static int yyread(yyscan_t yyscanner,char *buf,int max_size)
366{
367 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
368 int inputPosition = yyextra->inputPosition;
369 const char *s = yyextra->inputString + inputPosition;
370 int c=0;
371 while( c < max_size && *s )
372 {
373 *buf++ = *s++;
374 c++;
375 }
376 yyextra->inputPosition += c;
377 return c;
378}
379
380
381// public interface -----------------------------------------------------------
382
388
390{
391 sqlcodeYYlex_init_extra(&p->state, &p->yyscanner);
392#ifdef FLEX_DEBUG
393 sqlcodeYYset_debug(Debug::isFlagSet(Debug::Lex_sqlcode)?1:0,p->yyscanner);
394#endif
396}
397
399{
400 sqlcodeYYlex_destroy(p->yyscanner);
401}
402
404{
405 struct yyguts_t *yyg = (struct yyguts_t*)p->yyscanner;
406 yyextra->currentDefinition = nullptr;
407 yyextra->currentMemberDef = nullptr;
408}
409
411 const DString &/* scopeName */,
412 const DString &input,
414 bool stripCodeComments,
415 const CodeParserOptions &options
416 )
417{
418 yyscan_t yyscanner = p->yyscanner;
419 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
420
421 if (input.empty()) return;
422
423 DebugLex debugLex(Debug::Lex_sqlcode, __FILE__, options.fileDef() ? qPrint(options.fileDef()->fileName()): nullptr);
424 yyextra->fileName = options.fileDef() ? options.fileDef()->fileName():"";
425 yyextra->code = &codeOutIntf;
426 yyextra->inputString = input.data();
427 yyextra->inputPosition = 0;
428 yyextra->currentFontClass = nullptr;
429 yyextra->insideCodeLine = false;
430 yyextra->searchCtx = options.searchCtx();
431 yyextra->yyLineNr = options.startLine()!=-1 ? options.startLine() : 1;
432 yyextra->inputLines = options.endLine()!=-1 ? options.endLine()+1 : yyextra->yyLineNr + countLines(yyscanner) - 1;
433 yyextra->stripCodeComments = stripCodeComments;
434 yyextra->exampleBlock = options.isExample();
435 yyextra->exampleName = options.exampleName();
436 yyextra->sourceFileDef = options.fileDef();
437 yyextra->lineNumbers = options.fileDef() && options.showLineNumbers();
438
439 if (options.isExample() && options.fileDef()==0)
440 {
441 // create a dummy filedef for the example
442 yyextra->exampleFileDef = createFileDef(DString(),!options.exampleName().empty() ? options.exampleName() : DString("generated"));
443 yyextra->sourceFileDef = yyextra->exampleFileDef.get();
444 }
445
446 if (yyextra->sourceFileDef)
447 {
448 setCurrentDoc(yyscanner,"l00001");
449 }
450
451 yyextra->includeCodeFragment = options.inlineFragment();
452 // Starts line 1 on the output
453 startCodeLine(yyscanner);
454
455 sqlcodeYYrestart( nullptr, yyscanner );
456
457 sqlcodeYYlex(yyscanner);
458
459 if (yyextra->insideCodeLine)
460 {
461 endCodeLine(yyscanner);
462 }
463 if (yyextra->exampleFileDef)
464 {
465 // delete the temporary file definition used for this example
466 yyextra->exampleFileDef.reset();
467 yyextra->sourceFileDef=nullptr;
468 }
469}
470
471//---------------------------------------------------------------------------------
472
473#include "sqlcode.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_sqlcode
Definition debug.h:70
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 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 sqlcode.l:410
std::unique_ptr< Private > p
Definition sqlcode.h:43
void resetCodeParserState() override
Resets the state of the code parser.
Definition sqlcode.l:403
~SQLCodeParser() override
Definition sqlcode.l:398
void setCurrentDoc(const Definition *ctx, const DString &anchor, bool isSourceFile)
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
sqlcodeYY_state state
Definition sqlcode.l:386
SrcLangExt
Definition types.h:207