Doxygen
Loading...
Searching...
No Matches
fortranscanner.l
Go to the documentation of this file.
1/* -*- mode: fundamental; indent-tabs-mode: 1; -*- */
2/*****************************************************************************
3 * Parser for Fortran90 F subset
4 *
5 * Copyright (C) by Anke Visser
6 * based on the work of Dimitri van Heesch.
7 *
8 * Permission to use, copy, modify, and distribute this software and its
9 * documentation under the terms of the GNU General Public License is hereby
10 * granted. No representations are made about the suitability of this software
11 * for any purpose. It is provided "as is" without express or implied warranty.
12 * See the GNU General Public License for more details.
13 *
14 * Documents produced by Doxygen are derivative works derived from the
15 * input used in their production; they are not affected by this license.
16 *
17 */
18
19/* Developer notes.
20 *
21 * - Consider using startScope(), endScope() functions with module, program,
22 * subroutine or any other scope in fortran program.
23 *
24 * - Symbol yyextra->modifiers (attributes) are collected using SymbolModifiers |= operator during
25 * substructure parsing. When substructure ends all yyextra->modifiers are applied to actual
26 * entries in applyModifiers() functions.
27 *
28 * - How case insensitiveness should be handled in code?
29 * On one side we have arg->name and entry->name, on another side modifierMap[name].
30 * In entries and arguments case is the same as in code, in modifier map case is lowered and
31 * then it is compared to lowered entry/argument names.
32 *
33 * - Do not like constructs like aa{BS} or {BS}bb. Should try to handle blank space
34 * with separate rule?: It seems it is often necessary, because we may parse something like
35 * "functionA" or "MyInterface". So constructs like '(^|[ \t])interface({BS_}{ID})?/[ \t\n]'
36 * are desired.
37 *
38 * - Must track yyextra->lineNr when using REJECT, unput() or similar commands.
39 */
40%option never-interactive
41%option case-insensitive
42%option prefix="fortranscannerYY"
43%option reentrant
44%option extra-type="struct fortranscannerYY_state *"
45%top{
46#include <stdint.h>
47// forward declare yyscan_t to improve type safety
48#define YY_TYPEDEF_YY_SCANNER_T
49#define yyguts_t fortranscannerYYguts_t
50struct yyguts_t;
yyguts_t * yyscan_t
Definition code.l:25
#define yyguts_t
Definition code.l:23
52}
53
54%{
55
56// own header
57#include "fortranscanner.h"
58
59// standard includes
60#include <algorithm>
61#include <map>
62#include <memory>
63#include <stack>
64#include <unordered_map>
65#include <unordered_set>
66#include <vector>
67
68// other includes
69#include "arguments.h"
70#include "commentscan.h"
71#include "config.h"
72#include "debug.h"
73#include "doxygen.h"
74#include "entry.h"
75#include "markdown.h"
76#include "message.h"
77#include "util.h"
78
79// Toggle for some debugging info
80//#define DBG_CTX(x) fprintf x
81#define DBG_CTX(x) do { } while(0)
82
83#define YY_NO_INPUT 1
84#define YY_NO_UNISTD_H 1
85
88
90{
91 BlockState(DString str, int lineNr) : blockString(str), blockLineNr(lineNr) {}
93 int blockLineNr = -1;
94};
95// {{{ ----- Helper structs -----
96//! Holds yyextra->modifiers (ie attributes) for one symbol (variable, function, etc)
98{
101
102 //! This is only used with function return value.
114 bool target;
115 bool save;
118 bool nopass;
119 bool pass;
121 bool volat; /* volatile is a reserved name */
122 bool value; /* volatile is a reserved name */
125
127 optional(false), protect(false), dimension(), allocatable(false),
128 external(false), intrinsic(false), parameter(false),
129 pointer(false), target(false), save(false), deferred(false), nonoverridable(false),
130 nopass(false), pass(false), contiguous(false), volat(false), value(false), passVar(),
131 bindVar() {}
132
135};
136
137//ostream& operator<<(ostream& out, const SymbolModifiers& mdfs);
138
139static const char *directionStrs[] =
140{
141 "", "intent(in)", "intent(out)", "intent(inout)"
142};
143static const char *directionParam[] =
144{
145 "", "[in]", "[out]", "[in,out]"
146};
147
148// }}}
149
151{
152 CommentInPrepass(int col, const DString &s) : column(col), str(s) {}
155};
156
157/* -----------------------------------------------------------------
158 *
159 * statics
160 */
161
163{
166 const char * inputString;
169 DString inputStringPrepass; ///< Input string for prepass of line cont. '&'
170 DString inputStringSemi; ///< Input string after command separator ';'
174 std::vector<CommentInPrepass> comments;
175 YY_BUFFER_STATE * includeStack = nullptr;
179 int lineNr = 1 ;
180 int colNr = 0 ;
181 Entry *current_root = nullptr;
182 Entry *global_scope = nullptr;
183 std::shared_ptr<Entry> global_root;
184 std::shared_ptr<Entry> file_root;
185 std::shared_ptr<Entry> last_entry;
186 std::shared_ptr<Entry> last_enum;
187 std::shared_ptr<Entry> current;
188 ScanVar vtype = V_IGNORE; // type of parsed variable
189 EntryList moduleProcedures; // list of all interfaces which contain unresolved module procedures
191 bool docBlockInBody = false;
196 std::stack<BlockState> blockStack;
198 size_t fencedSize = 0;
199// Argument *parameter; // element of parameter list
200 DString argType; // fortran type of an argument of a parameter list
201 DString argName; // last identifier name in variable list
202 DString initializer; // initial value of a variable
203 int initializerArrayScope; // number if nested array scopes in initializer
204 int initializerScope; // number if nested function calls in initializer
205 DString useModuleName; // name of module in the use statement
208 bool typeMode = false;
210 bool functionLine = false;
211 char stringStartSymbol; // single or double quote
212 bool parsingPrototype = false; // see parsePrototype()
213
214//! Accumulated modifiers of current statement, eg variable declaration.
216//! Holds program scope->symbol name->symbol modifiers.
217 std::map<Entry*,std::map<std::string,SymbolModifiers> > modifiers;
218 int anonCount = 0 ;
219
221 //! counter for the number of main programs in this file
223 int curIndent = 0;
224};
225
226//-----------------------------------------------------------------------------
227static int getAmpersandAtTheStart(const char *buf, int length);
228static int getAmpOrExclAtTheEnd(const char *buf, int length, char ch);
229static DString extractFromParens(const DString &name);
230static DString extractBind(const DString &name);
231
232
233static int yyread(yyscan_t yyscanner,char *buf,int max_size);
234static void startCommentBlock(yyscan_t yyscanner,bool);
235static void handleCommentBlock(yyscan_t yyscanner,const DString &doc,bool brief);
236static void subrHandleCommentBlock(yyscan_t yyscanner,const DString &doc,bool brief);
237static void subrHandleCommentBlockResult(yyscan_t yyscanner,const DString &doc,bool brief);
238static void addCurrentEntry(yyscan_t yyscanner,bool case_insens);
239static void addModule(yyscan_t yyscanner,const DString &name=DString(), bool isModule=false);
240static void addSubprogram(yyscan_t yyscanner,const DString &text);
241static void addInterface(yyscan_t yyscanner,DString name, InterfaceType type);
242static Argument *getParameter(yyscan_t yyscanner,const DString &name);
243static void scanner_abort(yyscan_t yyscanner);
244static void pop_state(yyscan_t yyscanner);
245static void pushBlockState(yyscan_t yyscanner,const DString &text);
246static void popBlockState(yyscan_t yyscanner);
247
248static void startScope(yyscan_t yyscanner,Entry *scope);
249static bool endScope(yyscan_t yyscanner,Entry *scope, bool isGlobalRoot=false);
250static void copyEntry(std::shared_ptr<Entry> dest, const std::shared_ptr<Entry> &src);
251static void resolveModuleProcedures(yyscan_t yyscanner,Entry *current_root);
252static void resolveTypeBoundProcedures(Entry *scope);
253static void truncatePrepass(yyscan_t yyscanner,int index);
254static void pushBuffer(yyscan_t yyscanner,const DString &buffer);
255static void popBuffer(yyscan_t yyscanner);
256static const CommentInPrepass* locatePrepassComment(yyscan_t yyscanner,int from, int to);
257static void updateVariablePrepassComment(yyscan_t yyscanner,int from, int to);
258static void newLine(yyscan_t yyscanner);
259static void initEntry(yyscan_t yyscanner);
260
261static const char *stateToString(int state);
262static inline int computeIndent(const char *s);
263
264
265//-----------------------------------------------------------------------------
266#undef YY_INPUT
267#define YY_INPUT(buf,result,max_size) result=yyread(yyscanner,buf,max_size);
268
269// otherwise the filename would be the name of the converted file (*.cpp instead of *.l)
270static inline const char *getLexerFILE() {return __FILE__;}
271#include "doxygen_lex.h"
272#define YY_USER_ACTION yyextra->colNr+=(int)yyleng;
273#define INVALID_ENTRY ((Entry*)0x8)
274
275
276//-----------------------------------------------------------------------------
277
This class contains the information about the argument of a function or template.
Definition arguments.h:27
A String class for use with Doxygen wrapping std::string and adding some additional functionality off...
Definition dstring.h:84
Represents an unstructured piece of information, about an entity found in the sources.
Definition entry.h:115
Abstract interface for outline parsers.
Definition parserintf.h:41
Interface for the comment block scanner.
std::vector< std::shared_ptr< Entry > > EntryList
Definition entry.h:270
static int getAmpersandAtTheStart(const char *buf, int length)
static const char * directionParam[]
static DString extractFromParens(const DString &name)
static void resolveModuleProcedures(yyscan_t yyscanner, Entry *current_root)
fill empty interface module procedures with info from corresponding module subprogs
static void newLine(yyscan_t yyscanner)
static void popBlockState(yyscan_t yyscanner)
static void addCurrentEntry(yyscan_t yyscanner, bool case_insens)
adds yyextra->current entry to yyextra->current_root and creates new yyextra->current
ScanVar
@ V_IGNORE
@ V_RESULT
@ V_VARIABLE
@ V_PARAMETER
static void pushBuffer(yyscan_t yyscanner, const DString &buffer)
static int computeIndent(const char *s)
static void addSubprogram(yyscan_t yyscanner, const DString &text)
static void startCommentBlock(yyscan_t yyscanner, bool)
static void addModule(yyscan_t yyscanner, const DString &name=DString(), bool isModule=false)
static void startScope(yyscan_t yyscanner, Entry *scope)
static int getAmpOrExclAtTheEnd(const char *buf, int length, char ch)
static int yyread(yyscan_t yyscanner, char *buf, int max_size)
static void subrHandleCommentBlockResult(yyscan_t yyscanner, const DString &doc, bool brief)
Handle result description as defined after the declaration of the parameter.
static const char * stateToString(int state)
static void popBuffer(yyscan_t yyscanner)
static void addInterface(yyscan_t yyscanner, DString name, InterfaceType type)
static void copyEntry(std::shared_ptr< Entry > dest, const std::shared_ptr< Entry > &src)
used to copy entry to an interface module procedure
static const char * directionStrs[]
static void scanner_abort(yyscan_t yyscanner)
static void pushBlockState(yyscan_t yyscanner, const DString &text)
static void pop_state(yyscan_t yyscanner)
static void resolveTypeBoundProcedures(Entry *scope)
static DString extractBind(const DString &name)
static void updateVariablePrepassComment(yyscan_t yyscanner, int from, int to)
static Argument * getParameter(yyscan_t yyscanner, const DString &name)
static void subrHandleCommentBlock(yyscan_t yyscanner, const DString &doc, bool brief)
Handle parameter description as defined after the declaration of the parameter.
static bool endScope(yyscan_t yyscanner, Entry *scope, bool isGlobalRoot=false)
static const char * getLexerFILE()
static const CommentInPrepass * locatePrepassComment(yyscan_t yyscanner, int from, int to)
InterfaceType
@ IF_NONE
@ IF_GENERIC
@ IF_SPECIFIC
@ IF_ABSTRACT
static void handleCommentBlock(yyscan_t yyscanner, const DString &doc, bool brief)
static void truncatePrepass(yyscan_t yyscanner, int index)
static void initEntry(yyscan_t yyscanner)
BlockState(DString str, int lineNr)
DString blockString
CommentInPrepass(int col, const DString &s)
Holds yyextra->modifiers (ie attributes) for one symbol (variable, function, etc).
SymbolModifiers & operator|=(const SymbolModifiers &mdfs)
DString type
This is only used with function return value.
Protection protection
DString inputStringPrepass
Input string for prepass of line cont. '&'.
std::map< Entry *, std::map< std::string, SymbolModifiers > > modifiers
Holds program scope->symbol name->symbol modifiers.
YY_BUFFER_STATE * includeStack
std::shared_ptr< Entry > last_enum
OutlineParserInterface * thisParser
SymbolModifiers currentModifiers
Accumulated modifiers of current statement, eg variable declaration.
unsigned int inputPositionPrepass
std::shared_ptr< Entry > global_root
std::vector< CommentInPrepass > comments
DString inputStringSemi
Input string after command separator ';'.
CommentScanner commentScanner
std::stack< BlockState > blockStack
std::shared_ptr< Entry > last_entry
int mainPrograms
counter for the number of main programs in this file
std::shared_ptr< Entry > current
std::shared_ptr< Entry > file_root
Protection
Definition types.h:32
A bunch of utility functions.
278%}
279
280 //-----------------------------------------------------------------------------
281 //-----------------------------------------------------------------------------
282CMD ("\\"|"@")
283IDSYM [a-z_A-Z0-9]
284SEPARATE [:, \t]
285ID [a-z_A-Z%]+{IDSYM}*
286ID_ [a-z_A-Z%]*{IDSYM}*
287OPERATOR_ID (operator{BS}"("{BS}(\.[a-z_A-Z]+\.|"="|"/="|"//"|"=="|"<"|"<="|">"|">="|"+"|"*"|"**"|"/"|"-"){BS}")")
288SUBPROG (subroutine|function)
289B [ \t]
290BS [ \t]*
291BS_ [ \t]+
292BT_ ([ \t]+|[ \t]*"(")
293COMMA {BS},{BS}
294ARGS_L0 ("("[^)]*")")
295ARGS_L1a [^()]*"("[^)]*")"[^)]*
296ARGS_L1 ("("{ARGS_L1a}*")")
297ARGS_L2 "("({ARGS_L0}|[^()]|{ARGS_L1a}|{ARGS_L1})*")"
298ARGS {BS}({ARGS_L0}|{ARGS_L1}|{ARGS_L2})
299NOARGS {BS}"\n"
300
301PRE [pP][rR][eE]
302CODE [cC][oO][dD][eE]
303
304COMM "!"[!<>]
305NUM_TYPE (complex|integer|logical|real)
306LOG_OPER (\.and\.|\.eq\.|\.eqv\.|\.ge\.|\.gt\.|\.le\.|\.lt\.|\.ne\.|\.neqv\.|\.or\.|\.not\.)
307KIND {ARGS}
308CHAR (CHARACTER{ARGS}?|CHARACTER{BS}"*"({BS}[0-9]+|{ARGS}))
309TYPE_SPEC (({NUM_TYPE}({BS}"*"{BS}[0-9]+)?)|({NUM_TYPE}{KIND})|DOUBLE{BS}COMPLEX|DOUBLE{BS}PRECISION|ENUMERATOR|{CHAR}|TYPE{ARGS}|CLASS{ARGS}|PROCEDURE{ARGS}?)
310
311INTENT_SPEC intent{BS}"("{BS}(in|out|in{BS}out){BS}")"
312ATTR_SPEC (EXTERNAL|ALLOCATABLE|DIMENSION{ARGS}|{INTENT_SPEC}|INTRINSIC|OPTIONAL|PARAMETER|POINTER|PROTECTED|PRIVATE|PUBLIC|SAVE|TARGET|NOPASS|PASS{ARGS}?|DEFERRED|NON_OVERRIDABLE|CONTIGUOUS|VOLATILE|VALUE)
313LANGUAGE_BIND_SPEC BIND{BS}"("{BS}C{BS}((,{BS}NAME{BS}"="{BS}"\""(.*)"\""{BS})|(,{BS}NAME{BS}"="{BS}"'"(.*)"'"{BS}))?")"
314/* Assume that attribute statements are almost the same as attributes. */
315ATTR_STMT {ATTR_SPEC}|DIMENSION
316EXTERNAL_STMT (EXTERNAL)
317
318CONTAINS CONTAINS
319PREFIX ((NON_)?RECURSIVE{BS_}|IMPURE{BS_}|PURE{BS_}|ELEMENTAL{BS_}){0,4}((NON_)?RECURSIVE|IMPURE|PURE|ELEMENTAL)?
320SCOPENAME ({ID}{BS}"::"{BS})*
321
322LINENR {B}*[1-9][0-9]*
323FILEICHAR [a-z_A-Z0-9\x80-\xFF\\:\\\/\-\+=&#@~]
324FILEECHAR [a-z_A-Z0-9\x80-\xFF\-\+=&#@~]
325FILECHARS {FILEICHAR}*{FILEECHAR}+
326HFILEMASK {FILEICHAR}*("."{FILEICHAR}+)+{FILECHARS}*
327VFILEMASK {FILECHARS}("."{FILECHARS})*
328FILEMASK {VFILEMASK}|{HFILEMASK}
329
330%option noyywrap
331%option stack
332%option caseless
333/*%option debug */
334
335 //---------------------------------------------------------------------------------
336
337 /** fortran parsing states */
338%x Subprog
339%x SubprogPrefix
340%x Parameterlist
341%x SubprogBody
342%x SubprogBodyContains
343%x Start
344%x Comment
345%x Module
346%x Program
347%x ModuleBody
348%x ModuleBodyContains
349%x AttributeList
350%x FVariable
351%x Initialization
352%x ArrayInitializer
353%x FEnum
354%x Typedef
355%x TypedefBody
356%x TypedefBodyContains
357%x InterfaceBody
358%x StrIgnore
359%x String
360%x Use
361%x UseOnly
362%x ModuleProcedure
363
364%x Prepass
365
366 /** comment parsing states */
367%x DocBlock
368%x DocBackLine
369%x DocCopyBlock
370
371%x BlockData
372
373/** prototype parsing */
374%x Prototype
375%x PrototypeSubprog
376%x PrototypeArgs
377
379
380 /*-----------------------------------------------------------------------------------*/
381
382<Prepass>^{BS}[&]*{BS}!.*\n { /* skip lines with just comment. Note code was in free format or has been converted to it */
383 yyextra->lineCountPrepass ++;
384 }
385<Prepass>^{BS}\n { /* skip empty lines */
386 yyextra->lineCountPrepass ++;
387 }
388<*>^.*\n { // prepass: look for line continuations
389 yyextra->functionLine = false;
390
391 DBG_CTX((stderr, "---%s", yytext));
392
393 int indexStart = getAmpersandAtTheStart(yytext, (int)yyleng);
394 int indexEnd = getAmpOrExclAtTheEnd(yytext, (int)yyleng, '\0');
395 if (indexEnd>=0 && yytext[indexEnd]!='&') //we are only interested in amp
396 {
397 indexEnd=-1;
398 }
399
400 if (indexEnd<0)
401 { // ----- no ampersand as line continuation
402 if (YY_START == Prepass)
403 { // last line in "continuation"
404
405 // Only take input after initial ampersand
406 yyextra->inputStringPrepass+=(const char*)(yytext+(indexStart+1));
407
408 //printf("BUFFER:%s\n", (const char*)yyextra->inputStringPrepass);
409 pushBuffer(yyscanner,yyextra->inputStringPrepass);
410 yyextra->colNr = 0;
411 pop_state(yyscanner);
412 }
413 else
414 { // simple line
415 yyextra->colNr = 0;
416 REJECT;
417 }
418 }
419 else
420 { // ----- line with continuation
421 if (YY_START != Prepass)
422 {
423 yyextra->comments.clear();
424 yyextra->inputStringPrepass=DString();
425 yy_push_state(Prepass,yyscanner);
426 }
427
428 size_t length = yyextra->inputStringPrepass.length();
429
430 // Only take input after initial ampersand
431 yyextra->inputStringPrepass+=(const char*)(yytext+(indexStart+1));
432 yyextra->lineCountPrepass ++;
433
434 // cut off & and remove following comment if present
435 truncatePrepass(yyscanner,static_cast<int>(length) + indexEnd - indexStart - 1);
436 }
437 }
#define DBG_CTX(x)
Definition code.l:71
438
439
440 /*------ ignore strings that are not initialization strings */
441<String>\"|\' { // string ends with next quote without previous backspace
442 if (yytext[0]!=yyextra->stringStartSymbol)
443 {
444 yyextra->colNr -= (int)yyleng;
445 REJECT;
446 } // single vs double quote
447 if (yy_top_state(yyscanner) == Initialization ||
448 yy_top_state(yyscanner) == ArrayInitializer)
449 {
450 yyextra->initializer+=yytext;
451 }
452 pop_state(yyscanner);
453 }
454<String>[\x80-\xFF]* |
455<String>. { if (yy_top_state(yyscanner) == Initialization ||
456 yy_top_state(yyscanner) == ArrayInitializer)
457 {
458 yyextra->initializer+=yytext;
459 }
460 }
461<*>\"|\' { /* string starts */
462 if (YY_START == StrIgnore)
463 { yyextra->colNr -= (int)yyleng;
464 REJECT;
465 }; // ignore in simple yyextra->comments
466 yy_push_state(YY_START,yyscanner);
467 if (yy_top_state(yyscanner) == Initialization ||
468 yy_top_state(yyscanner) == ArrayInitializer)
469 {
470 yyextra->initializer+=yytext;
471 }
472 yyextra->stringStartSymbol=yytext[0]; // single or double quote
473 BEGIN(String);
474 }
475
476 /*------ ignore simple comment (not documentation yyextra->comments) */
477
478<*>"!"/([^<>\n]|"<ff>"|"<FF>") { if (YY_START == String || YY_START == DocCopyBlock)
479 { yyextra->colNr -= (int)yyleng;
480 REJECT;
481 } // "!" is ignored in strings
482 // skip comment line (without docu yyextra->comments "!>" "!<" )
483 /* ignore further "!" and ignore yyextra->comments in Strings */
484 if ((YY_START != StrIgnore) && (YY_START != String))
485 {
486 yy_push_state(YY_START,yyscanner);
487 BEGIN(StrIgnore);
488 yyextra->debugStr="*!";
489 DBG_CTX((stderr,"start comment %d\n",yyextra->lineNr));
490 }
491 }
492<StrIgnore>.?/\n { pop_state(yyscanner); // comment ends with endline character
493 DBG_CTX((stderr,"end comment %d %s\n",yyextra->lineNr,qPrint(yyextra->debugStr)));
494 } // comment line ends
const char * qPrint(const char *s)
Definition dstring.h:783
495<StrIgnore>[\x80-\xFF]* |
496<StrIgnore>. { yyextra->debugStr+=yytext; }
497
498
499 /*------ use handling ------------------------------------------------------------*/
500
501<Start,ModuleBody,SubprogBody>"use"{BS_} {
502 if (YY_START == Start)
503 {
504 addModule(yyscanner);
505 pushBlockState(yyscanner,DString(yytext)+" (anonymous program)");
506 yy_push_state(ModuleBody,yyscanner); //anon program
507 }
508 yy_push_state(Use,yyscanner);
509 }
510<Use>{ID} {
511 DBG_CTX((stderr,"using dir %s\n",yytext));
512 yyextra->current->name=yytext;
513 yyextra->current->name=yyextra->current->name.lower();
514 yyextra->current->fileName = yyextra->fileName;
515 yyextra->current->section=EntryType::makeUsingDir();
516 yyextra->current_root->moveToSubEntryAndRefresh(yyextra->current);
517 yyextra->current->lang = SrcLangExt::Fortran;
518 pop_state(yyscanner);
519 }
520<Use>{ID}/, {
521 yyextra->useModuleName=yytext;
522 yyextra->useModuleName=yyextra->useModuleName.lower();
523 }
524<Use>,{BS}"ONLY" { BEGIN(UseOnly);
525 }
526<UseOnly>{BS},{BS} {}
527<UseOnly>{ID} {
528 yyextra->current->name= yyextra->useModuleName+"::"+yytext;
529 yyextra->current->name=yyextra->current->name.lower();
530 yyextra->current->fileName = yyextra->fileName;
531 yyextra->current->section=EntryType::makeUsingDecl();
532 yyextra->current_root->moveToSubEntryAndRefresh(yyextra->current);
533 yyextra->current->lang = SrcLangExt::Fortran;
534 }
535<Use,UseOnly>"\n" {
536 yyextra->colNr -= 1;
537 unput(*yytext);
538 pop_state(yyscanner);
539 }
540
541 /* INTERFACE definitions */
542<Start,ModuleBody,SubprogBody>{
543^{BS}interface{IDSYM}+ { /* variable with interface prefix */ }
544^{BS}interface { yyextra->ifType = IF_SPECIFIC;
545 yy_push_state(InterfaceBody,yyscanner);
546 // do not start a scope here, every
547 // interface body is a scope of its own
548 }
549
550^{BS}abstract{BS_}interface { yyextra->ifType = IF_ABSTRACT;
551 yy_push_state(InterfaceBody,yyscanner);
552 // do not start a scope here, every
553 // interface body is a scope of its own
554 }
555
556^{BS}interface{BS_}{ID}{ARGS}? { yyextra->ifType = IF_GENERIC;
557 yyextra->current->bodyLine = yyextra->lineNr + yyextra->lineCountPrepass + 1; // we have to be at the line after the definition and we have to take continuation lines into account.
558 yy_push_state(InterfaceBody,yyscanner);
559
560 // extract generic name
562 name = name.mid(9).stripWhiteSpace().lower();
563 addInterface(yyscanner,name, yyextra->ifType);
564 startScope(yyscanner,yyextra->last_entry.get());
565 }
DString stripWhiteSpace() const
returns a copy of this string with leading and trailing whitespace removed
Definition dstring.h:337
const char * name(int level)
Definition latexgen.h:368
566}
567
568<InterfaceBody>^{BS}end{BS}interface({BS_}{ID})? {
569 // end scope only if GENERIC interface
570 if (yyextra->ifType == IF_GENERIC)
571 {
572 yyextra->last_entry->parent()->endBodyLine = yyextra->lineNr - 1;
573 }
574 if (yyextra->ifType == IF_GENERIC && !endScope(yyscanner,yyextra->current_root))
575 {
576 yyterminate();
577 }
578 yyextra->ifType = IF_NONE;
579 pop_state(yyscanner);
580 }
#define yyterminate()
581<InterfaceBody>module{BS}procedure { yy_push_state(YY_START,yyscanner);
582 BEGIN(ModuleProcedure);
583 }
584<ModuleProcedure>{ID} { DString name = DString(yytext).lower();
585 if (yyextra->ifType == IF_ABSTRACT || yyextra->ifType == IF_SPECIFIC)
586 {
587 addInterface(yyscanner,name, yyextra->ifType);
588 startScope(yyscanner,yyextra->last_entry.get());
589 }
590
591 yyextra->current->section = EntryType::makeFunction();
592 yyextra->current->name = name;
593 yyextra->moduleProcedures.push_back(yyextra->current);
594 addCurrentEntry(yyscanner,true);
595 }
DString lower() const
Definition dstring.h:326
596<ModuleProcedure>"\n" { yyextra->colNr -= 1;
597 unput(*yytext);
598 pop_state(yyscanner);
599 }
600<InterfaceBody>. {}
601
602 /*-- Contains handling --*/
603<Start>^{BS}{CONTAINS}/({BS}|\n|!|;) {
604 if (YY_START == Start)
605 {
606 addModule(yyscanner);
607 yy_push_state(ModuleBodyContains,yyscanner); //anon program
608 }
609 }
610<ModuleBody>^{BS}{CONTAINS}/({BS}|\n|!|;) { BEGIN(ModuleBodyContains); }
611<SubprogBody>^{BS}{CONTAINS}/({BS}|\n|!|;) { BEGIN(SubprogBodyContains); }
612<TypedefBody>^{BS}{CONTAINS}/({BS}|\n|!|;) { BEGIN(TypedefBodyContains); }
613
614 /*------ module handling ------------------------------------------------------------*/
615<Start>block{BS}data{BS}{ID_} { //
616 yyextra->vtype = V_IGNORE;
617 yy_push_state(BlockData,yyscanner);
618 yyextra->defaultProtection = Protection::Public;
619 }
620<Start>module|program{BS_} { //
621 yyextra->vtype = V_IGNORE;
622 if (yytext[0]=='m' || yytext[0]=='M')
623 {
624 yy_push_state(Module,yyscanner);
625 }
626 else
627 {
628 yy_push_state(Program,yyscanner);
629 }
630 yyextra->defaultProtection = Protection::Public;
631 }
632<BlockData>^{BS}"end"({BS}(block{BS}data)({BS_}{ID})?)?{BS}/(\n|!|;) { // end block data
633 //if (!endScope(yyscanner,yyextra->current_root))
634 // yyterminate();
635 yyextra->defaultProtection = Protection::Public;
636 pop_state(yyscanner);
637 }
638<Start,ModuleBody,ModuleBodyContains>"end"({BS}(module|program)({BS_}{ID})?)?{BS}/(\n|!|;) { // end module
639 resolveModuleProcedures(yyscanner,yyextra->current_root);
640 if (!endScope(yyscanner,yyextra->current_root))
641 {
642 yyterminate();
643 }
644 yyextra->defaultProtection = Protection::Public;
645 if (yyextra->global_scope)
646 {
647 if (yyextra->global_scope != INVALID_ENTRY)
648 {
649 yy_push_state(Start,yyscanner);
650 }
651 else
652 {
653 pop_state(yyscanner); // cannot pop artrificial entry
654 }
655 }
656 else
657 {
658 yy_push_state(Start,yyscanner);
659 yyextra->global_scope = INVALID_ENTRY; // signal that the yyextra->global_scope has already been used.
660 }
661 popBlockState(yyscanner);
662 }
#define INVALID_ENTRY
663<Module>{ID} {
664 addModule(yyscanner, DString(yytext), true);
665 pushBlockState(yyscanner,DString("module ")+yytext);
666 BEGIN(ModuleBody);
667 }
668<Program>{ID} {
669 addModule(yyscanner, DString(yytext), false);
670 pushBlockState(yyscanner,DString("program ")+yytext);
671 BEGIN(ModuleBody);
672 }
673
674 /*------- access specification --------------------------------------------------------------------------*/
675
676<ModuleBody,TypedefBody,TypedefBodyContains>private/{BS}(\n|"!") {
677 yyextra->defaultProtection = Protection::Private;
678 yyextra->current->protection = yyextra->defaultProtection ;
679 }
680<ModuleBody,TypedefBody,TypedefBodyContains>public/{BS}(\n|"!") {
681 yyextra->defaultProtection = Protection::Public;
682 yyextra->current->protection = yyextra->defaultProtection ;
683 }
684
685 /*------- type definition -------------------------------------------------------------------------------*/
686
687<ModuleBody>^{BS}type{BS}"=" {}
688<Start,ModuleBody>^{BS}type/[^a-z0-9_] {
689 if (YY_START == Start)
690 {
691 addModule(yyscanner,DString());
692 pushBlockState(yyscanner,DString(yytext)+" (anonymous program)");
693 yy_push_state(ModuleBody,yyscanner); //anon program
694 }
695
696 yy_push_state(Typedef,yyscanner);
697 yyextra->current->protection = Protection::Package; // invalid in Fortran, replaced below
698 yyextra->typeProtection = Protection::Public;
699 yyextra->typeMode = true;
700 }
701<Typedef>{
702{COMMA} {}
703
704{BS}"::"{BS} {}
705
706abstract {
707 yyextra->current->spec.setAbstractClass(true);
708 }
709extends{ARGS} {
710 DString basename = extractFromParens(yytext).lower();
711 yyextra->current->extends.emplace_back(basename, Protection::Public, Specifier::Normal);
712 }
713public {
714 yyextra->current->protection = Protection::Public;
715 }
716private {
717 yyextra->current->protection = Protection::Private;
718 }
719{LANGUAGE_BIND_SPEC} {
720 /* ignored for now */
721 }
722{ID} { /* type name found */
723 yyextra->current->section = EntryType::makeClass();
724 yyextra->current->spec.setStruct(true);
725 yyextra->current->name = yytext;
726 yyextra->current->fileName = yyextra->fileName;
727 yyextra->current->bodyLine = yyextra->lineNr;
728 yyextra->current->startLine = yyextra->lineNr;
729
730 /* if type is part of a module, mod name is necessary for output */
731 if (yyextra->current_root &&
732 (yyextra->current_root->section.isClass() ||
733 yyextra->current_root->section.isNamespace()))
734 {
735 yyextra->current->name = yyextra->current_root->name + "::" + yyextra->current->name;
736 }
737
738 // set modifiers to allow adjusting public/private in surrounding module scope
739 if( yyextra->current->protection == Protection::Package )
740 {
741 yyextra->current->protection = yyextra->defaultProtection;
742 }
743 else if( yyextra->current->protection == Protection::Public )
744 {
745 yyextra->modifiers[yyextra->current_root][yyextra->current->name.lower().str()] |= DString("public");
746 }
747 else if( yyextra->current->protection == Protection::Private )
748 {
749 yyextra->modifiers[yyextra->current_root][yyextra->current->name.lower().str()] |= DString("private");
750 }
751
752 addCurrentEntry(yyscanner,true);
753 startScope(yyscanner,yyextra->last_entry.get());
754 BEGIN(TypedefBody);
755 }
756}
757
758<TypedefBodyContains>{ /* Type Bound Procedures */
759^{BS}PROCEDURE{ARGS}? {
760 yyextra->current->type = DString(yytext).simplifyWhiteSpace().lower();
761 }
DString simplifyWhiteSpace() const
return a copy of this string with leading and trailing whitespace removed and multiple internal white...
Definition dstring.cpp:127
762^{BS}final {
763 yyextra->current->spec.setFinal(true);
764 yyextra->current->type = DString(yytext).simplifyWhiteSpace();
765 }
766^{BS}generic {
767 yyextra->current->type = DString(yytext).simplifyWhiteSpace();
768 }
769{COMMA} {
770 }
771{ATTR_SPEC} {
772 yyextra->currentModifiers |= DString(yytext).stripWhiteSpace();
773 }
774{BS}"::"{BS} {
775 }
776{ID} {
777 DString name = yytext;
778 yyextra->modifiers[yyextra->current_root][name.lower().str()] |= yyextra->currentModifiers;
779 yyextra->current->section = EntryType::makeFunction();
780 yyextra->current->name = name;
781 // check for procedure(name)
782 if (yyextra->current->type.find('(')!=DString::npos)
783 {
784 yyextra->current->args = extractFromParens(yyextra->current->type).stripWhiteSpace();
785 }
786 else
787 {
788 yyextra->current->args = name.lower(); // target procedure name if no => is given
789 }
790 yyextra->current->fileName = yyextra->fileName;
791 yyextra->current->bodyLine = yyextra->lineNr;
792 yyextra->current->startLine = yyextra->lineNr;
793 addCurrentEntry(yyscanner,true);
794 }
static constexpr size_t npos
value used to indicate 'not found' or 'to the end of the string', matching std::string::npos
Definition dstring.h:178
795{BS}"=>"[^(\n|\!)]* { /* Specific bindings come after the ID. */
796 DString tmp = yytext;
797 size_t i = tmp.find("=>");
798 if (i!=DString::npos)
799 {
800 tmp.remove(0, i+2);
801 }
802 tmp = tmp.simplifyWhiteSpace().lower();
803 if (yyextra->last_entry->type == "generic")
804 {
805 // duplicate entries for each overloaded variant
806 // (parse through medhod1,method2, methodN, ...
807 //printf("Parsing through %s for generic method %s.\n", tmp.data(), last_entry->name.data());
808 i = tmp.find(',');
809 while (i!=DString::npos && i>0)
810 {
811 copyEntry(yyextra->current, yyextra->last_entry);
812 yyextra->current->name = yyextra->last_entry->name;
813 yyextra->current->section = EntryType::makeFunction();
814 yyextra->last_entry->args = tmp.left(i).stripWhiteSpace();
815 //printf("Found %s.\n", last_entry->args.data());
816 addCurrentEntry(yyscanner,true);
817 tmp = tmp.remove(0,i+1).stripWhiteSpace();
818 i = tmp.find(',');
819 }
820 }
821 //printf("Target function: %s\n", tmp.data());
822 yyextra->last_entry->args = tmp;
823 }
DString & remove(size_t index, size_t len)
Definition dstring.h:535
size_t find(char c, size_t pos=0) const
Definition dstring.h:239
DString left(size_t len) const
Definition dstring.h:306
824"\n" {
825 yyextra->currentModifiers = SymbolModifiers();
826 newLine(yyscanner);
827 yyextra->docBlock.clear();
828 }
829}
830
831
832<TypedefBody,TypedefBodyContains>{
833^{BS}"end"{BS}"type"({BS_}{ID})?{BS}/(\n|!|;) { /* end type definition */
834 yyextra->last_entry->parent()->endBodyLine = yyextra->lineNr;
835 if (!endScope(yyscanner,yyextra->current_root))
836 {
837 yyterminate();
838 }
839 yyextra->typeMode = false;
840 pop_state(yyscanner);
841 }
842^{BS}"end"{BS}/(\n|!|;) { /* incorrect end type definition */
843 warn(yyextra->fileName,yyextra->lineNr, "Found 'END' instead of 'END TYPE'");
844 yyextra->last_entry->parent()->endBodyLine = yyextra->lineNr;
845 if (!endScope(yyscanner,yyextra->current_root))
846 {
847 yyterminate();
848 }
849 yyextra->typeMode = false;
850 pop_state(yyscanner);
851 }
#define warn(file, line, fmt,...)
Definition message.h:97
852}
853
854 /*------- module/global/typedef variable ---------------------------------------------------*/
855
856<SubprogBody,SubprogBodyContains>^{BS}[0-9]*{BS}"end"({BS}{SUBPROG}({BS_}{ID})?)?{BS}/(\n|!|;) {
857 //
858 // ABSTRACT and specific interfaces are stored
859 // in a scope of their own, even if multiple
860 // are group in one INTERFACE/END INTERFACE block.
861 //
862 if (yyextra->ifType == IF_ABSTRACT || yyextra->ifType == IF_SPECIFIC)
863 {
864 endScope(yyscanner,yyextra->current_root);
865 yyextra->last_entry->endBodyLine = yyextra->lineNr - 1;
866 }
867 yyextra->current_root->endBodyLine = yyextra->lineNr - 1;
868
869 if (!endScope(yyscanner,yyextra->current_root))
870 {
871 yyterminate();
872 }
873 yyextra->subrCurrent.pop_back();
874 yyextra->vtype = V_IGNORE;
875 popBlockState(yyscanner);
876 pop_state(yyscanner) ;
877 }
878<BlockData>{
879{ID} {
880 }
881}
882<Start,ModuleBody,TypedefBody,SubprogBody,FEnum>{
883^{BS}{TYPE_SPEC}/{SEPARATE} {
884 yyextra->last_enum.reset();
885 if (YY_START == FEnum)
886 {
887 yyextra->argType = "@"; // enum marker
888 }
889 else
890 {
891 yyextra->argType = DString(yytext).simplifyWhiteSpace().lower();
892 }
893 yyextra->current->bodyLine = yyextra->lineNr + 1;
894 yyextra->current->endBodyLine = yyextra->lineNr + yyextra->lineCountPrepass;
895 /* variable declaration starts */
896 if (YY_START == Start)
897 {
898 addModule(yyscanner);
899 pushBlockState(yyscanner,DString(yytext)+" (anonymous program)");
900 yy_push_state(ModuleBody,yyscanner); //anon program
901 }
902 yy_push_state(AttributeList,yyscanner);
903 }
904{EXTERNAL_STMT}/({BS}"::"|{BS_}{ID}) {
905 /* external can be a "type" or an attribute */
906 if (YY_START == Start)
907 {
908 addModule(yyscanner);
909 pushBlockState(yyscanner,DString(yytext)+" (anonymous program)");
910 yy_push_state(ModuleBody,yyscanner); //anon program
911 }
912 DString tmp = yytext;
913 yyextra->currentModifiers |= tmp.stripWhiteSpace();
914 yyextra->argType = DString(yytext).simplifyWhiteSpace().lower();
915 yy_push_state(AttributeList,yyscanner);
916 }
917{ATTR_STMT}/{BS_}{ID} |
918{ATTR_STMT}/{BS}"::" {
919 /* attribute statement starts */
920 DBG_CTX((stderr,"5=========> Attribute statement: %s\n", yytext));
921 if (YY_START == Start)
922 {
923 addModule(yyscanner);
924 pushBlockState(yyscanner,DString(yytext)+" (anonymous program)");
925 yy_push_state(ModuleBody,yyscanner); //anon program
926 }
927 DString tmp = yytext;
928 yyextra->currentModifiers |= tmp.stripWhiteSpace();
929 yyextra->argType="";
930 yy_push_state(YY_START,yyscanner);
931 BEGIN( AttributeList ) ;
932 }
933"common" {
934 if (YY_START == Start)
935 {
936 addModule(yyscanner);
937 pushBlockState(yyscanner,DString(yytext)+" (anonymous program)");
938 yy_push_state(ModuleBody,yyscanner); //anon program
939 }
940 }
941{ID} {
942 if (YY_START == Start && DString(yytext).stripWhiteSpace().lower()!="include")
943 {
944 addModule(yyscanner);
945 pushBlockState(yyscanner,DString(yytext)+" (anonymous program)");
946 yy_push_state(ModuleBody,yyscanner); //anon program
947 }
948 }
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:75
949^{BS}"type"{BS_}"is"/{BT_} {}
950^{BS}"type"{BS}"=" {}
951^{BS}"class"{BS_}"is"/{BT_} {}
952^{BS}"class"{BS_}"default" {}
953}
954<AttributeList>{
955{COMMA} {}
956{BS} {}
957{LANGUAGE_BIND_SPEC} {
958 yyextra->currentModifiers |= yytext;
959 }
960{ATTR_SPEC}. { /* update yyextra->current yyextra->modifiers when it is an ATTR_SPEC and not a variable name */
961 /* buyyextra->625519 */
962 char chr = yytext[(int)yyleng-1];
963 if (isId(chr))
964 {
965 yyextra->colNr -= (int)yyleng;
966 REJECT;
967 }
968 else
969 {
970 DString tmp = yytext;
971 tmp = tmp.left(tmp.length() - 1);
972 yyextra->colNr -= 1;
973 unput(yytext[(int)yyleng-1]);
974 yyextra->currentModifiers |= (tmp);
975 }
976 }
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition dstring.h:151
bool isId(char c)
Returns true if c is a valid character for an identifier.
Definition dstring.h:895
977"::" { /* end attribute list */
978 BEGIN( FVariable );
979 }
980. { /* unknown attribute, consider variable name */
981 //cout<<"start variables, unput "<<*yytext<<endl;
982 yyextra->colNr -= 1;
983 unput(*yytext);
984 BEGIN( FVariable );
985 }
986}
987
988<FVariable>{BS} {}
989<FVariable>{OPERATOR_ID} { /* parse operator access statements "public :: operator(==)" */
991 /* if variable/type/etc is part of a module, mod name is necessary for output */
992 // get surrounding state
993 int currentState = YY_START;
994 pop_state(yyscanner);
995 int outerState = YY_START;
996 yy_push_state(currentState,yyscanner);
997 if( outerState == Start || outerState == ModuleBody )
998 {
999 if ((yyextra->current_root) &&
1000 (yyextra->current_root->section.isClass() ||
1001 yyextra->current_root->section.isNamespace()))
1002 {
1003 name = yyextra->current_root->name + "::" + name;
1004 }
1005 }
1006 /* remember attributes for the symbol */
1007 yyextra->modifiers[yyextra->current_root][name.str()] |= yyextra->currentModifiers;
1008 }
1009<FVariable>{ID} { /* parse variable declaration */
1010 //cout << "5=========> got variable: " << yyextra->argType << "::" << yytext << endl;
1011 /* work around for bug in DString.replace (DString works) */
1012 DString name=yytext;
1013 name = name.lower();
1014 /* if variable/type/etc is part of a module, mod name is necessary for output */
1015 if ((yyextra->current_root) && yyextra->current_root->section.isNamespace())
1016 {
1017 name = yyextra->current_root->name + "::" + name;
1018 }
1019 /* remember attributes for the symbol */
1020 yyextra->modifiers[yyextra->current_root][name.lower().str()] |= yyextra->currentModifiers;
1021 yyextra->argName= name;
1022
1023 yyextra->vtype= V_IGNORE;
1024 if (!yyextra->argType.empty() && !yyextra->current_root->section.isFunction())
1025 { // new variable entry
1026 yyextra->vtype = V_VARIABLE;
1027 yyextra->current->section = EntryType::makeVariable();
1028 yyextra->current->name = yyextra->argName;
1029 yyextra->current->type = yyextra->argType;
1030 yyextra->current->fileName = yyextra->fileName;
1031 yyextra->current->bodyLine = yyextra->lineNr; // used for source reference
1032 yyextra->current->startLine = yyextra->lineNr;
1033 if (yyextra->argType == "@")
1034 {
1035 yyextra->current_root->copyToSubEntry(yyextra->current);
1036 // add to the scope surrounding the enum (copy!)
1037 yyextra->last_enum = yyextra->current;
1038 yyextra->current_root->parent()->moveToSubEntryAndRefresh(yyextra->current);
1039 initEntry(yyscanner);
1040 }
1041 else
1042 {
1043 addCurrentEntry(yyscanner,true);
1044 }
1045 }
1046 else if (!yyextra->argType.empty())
1047 { // declaration of parameter list: add type for corr. parameter
1048 Argument *parameter = getParameter(yyscanner,yyextra->argName);
1049 if (parameter)
1050 {
1051 yyextra->vtype= V_PARAMETER;
1052 if (!yyextra->argType.empty()) parameter->type=yyextra->argType.stripWhiteSpace();
1053 if (!yyextra->docBlock.empty())
1054 {
1055 subrHandleCommentBlock(yyscanner,yyextra->docBlock,true);
1056 }
1057 }
1058 // save, it may be function return type
1059 if (parameter)
1060 {
1061 yyextra->modifiers[yyextra->current_root][name.lower().str()].type = yyextra->argType;
1062 }
1063 else
1064 {
1065 if ((yyextra->current_root->name.lower() == yyextra->argName.lower()) ||
1066 (yyextra->modifiers[yyextra->current_root->parent()][yyextra->current_root->name.lower().str()].returnName.lower() == yyextra->argName.lower()))
1067 {
1068 size_t strt = yyextra->current_root->type.find("function");
1069 DString lft;
1070 DString rght;
1071 if (strt != DString::npos)
1072 {
1073 yyextra->vtype = V_RESULT;
1074 lft = "";
1075 rght = "";
1076 if (strt != 0) lft = yyextra->current_root->type.left(strt).stripWhiteSpace();
1077 if ((yyextra->current_root->type.length() - strt - strlen("function"))!= 0)
1078 {
1079 rght = yyextra->current_root->type.right(yyextra->current_root->type.length() - strt - (int)strlen("function")).stripWhiteSpace();
1080 }
1081 yyextra->current_root->type = lft;
1082 if (rght.length() > 0)
1083 {
1084 if (yyextra->current_root->type.length() > 0) yyextra->current_root->type += " ";
1085 yyextra->current_root->type += rght;
1086 }
1087 if (yyextra->argType.stripWhiteSpace().length() > 0)
1088 {
1089 if (yyextra->current_root->type.length() > 0) yyextra->current_root->type += " ";
1090 yyextra->current_root->type += yyextra->argType.stripWhiteSpace();
1091 }
1092 if (yyextra->current_root->type.length() > 0) yyextra->current_root->type += " ";
1093 yyextra->current_root->type += "function";
1094 if (!yyextra->docBlock.empty())
1095 {
1096 subrHandleCommentBlockResult(yyscanner,yyextra->docBlock,true);
1097 }
1098 }
1099 else
1100 {
1101 yyextra->current_root->type += " " + yyextra->argType.stripWhiteSpace();
1102 }
1103 yyextra->current_root->type = yyextra->current_root->type.stripWhiteSpace();
1104 yyextra->modifiers[yyextra->current_root][name.lower().str()].type = yyextra->current_root->type;
1105 }
1106 else
1107 {
1108 yyextra->modifiers[yyextra->current_root][name.lower().str()].type = yyextra->argType;
1109 }
1110 }
1111 // any accumulated doc for argument should be emptied,
1112 // because it is handled other way and this doc can be
1113 // unexpectedly passed to the next member.
1114 yyextra->current->doc.clear();
1115 yyextra->current->brief.clear();
1116 }
1117 }
DString type
Definition arguments.h:43
DString right(size_t len) const
Definition dstring.h:311
1118<FVariable>{ARGS} { /* dimension of the previous entry. */
1119 DString name(yyextra->argName);
1120 DString attr("dimension");
1121 attr += yytext;
1122 yyextra->modifiers[yyextra->current_root][name.lower().str()] |= attr;
1123 }
1124<FVariable>{COMMA} { //printf("COMMA: %d<=..<=%d\n", yyextra->colNr-(int)yyleng, yyextra->colNr);
1125 // locate !< comment
1126 updateVariablePrepassComment(yyscanner,yyextra->colNr-(int)yyleng, yyextra->colNr);
1127 }
1128<FVariable>{BS}"=" {
1129 yy_push_state(YY_START,yyscanner);
1130 yyextra->initializer="=";
1131 yyextra->initializerScope = yyextra->initializerArrayScope = 0;
1132 BEGIN(Initialization);
1133 }
1134<FVariable>"\n" { yyextra->currentModifiers = SymbolModifiers();
1135 pop_state(yyscanner); // end variable declaration list
1136 newLine(yyscanner);
1137 yyextra->docBlock.clear();
1138 }
1139<FVariable>";".*"\n" { yyextra->currentModifiers = SymbolModifiers();
1140 pop_state(yyscanner); // end variable declaration list
1141 yyextra->docBlock.clear();
1142 yyextra->inputStringSemi = " \n"+DString(yytext+1);
1143 yyextra->lineNr--;
1144 pushBuffer(yyscanner,yyextra->inputStringSemi);
1145 }
1146<*>";".*"\n" {
1147 if (YY_START == FVariable) REJECT; // Just be on the safe side
1148 if (YY_START == String) REJECT; // ";" ignored in strings
1149 if (YY_START == StrIgnore) REJECT; // ";" ignored in regular yyextra->comments
1150 if (YY_START == DocBlock) REJECT; // ";" ignored in documentation blocks
1151 yyextra->inputStringSemi = " \n"+DString(yytext+1);
1152 yyextra->lineNr--;
1153 pushBuffer(yyscanner,yyextra->inputStringSemi);
1154 }
1155
1156<Initialization,ArrayInitializer>"[" |
1157<Initialization,ArrayInitializer>"(/" { yyextra->initializer+=yytext;
1158 yyextra->initializerArrayScope++;
1159 BEGIN(ArrayInitializer); // initializer may contain comma
1160 }
1161<ArrayInitializer>"]" |
1162<ArrayInitializer>"/)" { yyextra->initializer+=yytext;
1163 yyextra->initializerArrayScope--;
1164 if (yyextra->initializerArrayScope<=0)
1165 {
1166 yyextra->initializerArrayScope = 0; // just in case
1167 BEGIN(Initialization);
1168 }
1169 }
1170<ArrayInitializer>. { yyextra->initializer+=yytext; }
1171<Initialization>"(" { yyextra->initializerScope++;
1172 yyextra->initializer+=yytext;
1173 }
1174<Initialization>")" { yyextra->initializerScope--;
1175 yyextra->initializer+=yytext;
1176 }
1177<Initialization>{COMMA} { if (yyextra->initializerScope == 0)
1178 {
1179 updateVariablePrepassComment(yyscanner,yyextra->colNr-(int)yyleng, yyextra->colNr);
1180 pop_state(yyscanner); // end initialization
1181 if (yyextra->last_enum)
1182 {
1183 yyextra->last_enum->initializer.str(yyextra->initializer.str());
1184 }
1185 else
1186 {
1187 if (yyextra->vtype == V_VARIABLE) yyextra->last_entry->initializer.str(yyextra->initializer.str());
1188 }
1189 }
1190 else
1191 {
1192 yyextra->initializer+=", ";
1193 }
1194 }
1195<Initialization>"\n"|"!" { //|
1196 pop_state(yyscanner); // end initialization
1197 if (yyextra->last_enum)
1198 {
1199 yyextra->last_enum->initializer.str(yyextra->initializer.str());
1200 }
1201 else
1202 {
1203 if (yyextra->vtype == V_VARIABLE) yyextra->last_entry->initializer.str(yyextra->initializer.str());
1204 }
1205 yyextra->colNr -= 1;
1206 unput(*yytext);
1207 }
1208<Initialization>. { yyextra->initializer+=yytext; }
1209
1210<*>{BS}"enum"{BS}","{BS}"bind"{BS}"("{BS}"c"{BS}")"{BS} {
1211 if (YY_START == Start)
1212 {
1213 addModule(yyscanner);
1214 pushBlockState(yyscanner,DString(yytext)+" (anonymous program)");
1215 yy_push_state(ModuleBody,yyscanner); //anon program
1216 }
1217
1218 yy_push_state(FEnum,yyscanner);
1219 yyextra->current->protection = yyextra->defaultProtection;
1220 yyextra->typeProtection = yyextra->defaultProtection;
1221 yyextra->typeMode = true;
1222
1223 yyextra->current->spec.setStruct(true);
1224 yyextra->current->name.clear();
1225 yyextra->current->args.clear();
1226 yyextra->current->name.sprintf("@%d",yyextra->anonCount++);
1227
1228 yyextra->current->section = EntryType::makeEnum();
1229 yyextra->current->fileName = yyextra->fileName;
1230 yyextra->current->startLine = yyextra->lineNr;
1231 yyextra->current->bodyLine = yyextra->lineNr;
1232 if ((yyextra->current_root) &&
1233 (yyextra->current_root->section.isClass() ||
1234 yyextra->current_root->section.isNamespace()))
1235 {
1236 yyextra->current->name = yyextra->current_root->name + "::" + yyextra->current->name;
1237 }
1238
1239 addCurrentEntry(yyscanner,true);
1240 startScope(yyscanner,yyextra->last_entry.get());
1241 BEGIN( FEnum ) ;
1242 }
1243<FEnum>"end"{BS}"enum" {
1244 yyextra->last_entry->parent()->endBodyLine = yyextra->lineNr;
1245 if (!endScope(yyscanner,yyextra->current_root))
1246 {
1247 yyterminate();
1248 }
1249 yyextra->typeMode = false;
1250 pop_state(yyscanner);
1251 }
1252 /*------ fortran subroutine/function handling ------------------------------------------------------------*/
1253 /* Start is initial condition */
1254
1255<Start,ModuleBody,SubprogBody,InterfaceBody,ModuleBodyContains,SubprogBodyContains>^{BS}({PREFIX}{BS_})?{TYPE_SPEC}{BS}({PREFIX}{BS_})?/{SUBPROG}{BS_} {
1256 if (yyextra->ifType == IF_ABSTRACT || yyextra->ifType == IF_SPECIFIC)
1257 {
1258 addInterface(yyscanner,"$interface$", yyextra->ifType);
1259 startScope(yyscanner,yyextra->last_entry.get());
1260 }
1261
1262 // TYPE_SPEC is for old function style function result
1263 yyextra->current->type = DString(yytext).stripWhiteSpace().lower();
1264 yy_push_state(SubprogPrefix,yyscanner);
1265 }
1266
1267<SubprogPrefix>{BS}{SUBPROG}{BS_} {
1268 // Fortran subroutine or function found
1269 yyextra->vtype = V_IGNORE;
1270 DString result=yytext;
1271 result=result.stripWhiteSpace();
1272 addSubprogram(yyscanner,result);
1273 BEGIN(Subprog);
1274 yyextra->current->bodyLine = yyextra->lineNr + yyextra->lineCountPrepass + 1; // we have to be at the line after the definition and we have to take continuation lines into account.
1275 yyextra->current->startLine = yyextra->lineNr;
1276 }
1277
1278<Start,ModuleBody,SubprogBody,InterfaceBody,ModuleBodyContains,SubprogBodyContains>^{BS}({PREFIX}{BS_})?{SUBPROG}{BS_} {
1279 // Fortran subroutine or function found
1280 yyextra->vtype = V_IGNORE;
1281 if (yyextra->ifType == IF_ABSTRACT || yyextra->ifType == IF_SPECIFIC)
1282 {
1283 addInterface(yyscanner,"$interface$", yyextra->ifType);
1284 startScope(yyscanner,yyextra->last_entry.get());
1285 }
1286
1287 DString result = DString(yytext).stripWhiteSpace();
1288 addSubprogram(yyscanner,result);
1289 yy_push_state(Subprog,yyscanner);
1290 yyextra->current->bodyLine = yyextra->lineNr + yyextra->lineCountPrepass + 1; // we have to be at the line after the definition and we have to take continuation lines into account.
1291 yyextra->current->startLine = yyextra->lineNr;
1292 }
1293
1294<Subprog>{BS} { /* ignore white space */ }
1295<Subprog>{ID} { yyextra->current->name = yytext;
1296 //cout << "1a==========> got " << yyextra->current->type << " " << yytext << " " << yyextra->lineNr << endl;
1297 DString returnName = yyextra->current->name.lower();
1298 /* if type is part of a module, mod name is necessary for output */
1299 if ((yyextra->current_root) &&
1300 (yyextra->current_root->section.isClass() ||
1301 yyextra->current_root->section.isNamespace()))
1302 {
1303 yyextra->current->name= yyextra->current_root->name + "::" + yyextra->current->name;
1304 }
1305 yyextra->modifiers[yyextra->current_root][yyextra->current->name.lower().str()].returnName = std::move(returnName);
1306
1307 if (yyextra->ifType == IF_ABSTRACT || yyextra->ifType == IF_SPECIFIC)
1308 {
1309 yyextra->current_root->name = substitute(
1310 yyextra->current_root->name, "$interface$", DString(yytext).lower());
1311 }
1312
1313 BEGIN(Parameterlist);
1314 }
DString substitute(const DString &s, const DString &src, const DString &dst)
substitute all occurrences of src in s by dst
Definition dstring.cpp:485
1315<Parameterlist>"(" { yyextra->current->args = "("; }
1316<Parameterlist>")" {
1317 yyextra->current->args += ")";
1318 yyextra->current->args = removeRedundantWhiteSpace(yyextra->current->args);
1319 addCurrentEntry(yyscanner,true);
1320 startScope(yyscanner,yyextra->last_entry.get());
1321 BEGIN(SubprogBody);
1322 }
DString removeRedundantWhiteSpace(const DString &s)
Definition util.cpp:427
1323<Parameterlist>{COMMA}|{BS} { yyextra->current->args += yytext;
1324 const CommentInPrepass *c = locatePrepassComment(yyscanner,yyextra->colNr-(int)yyleng, yyextra->colNr);
1325 if (c)
1326 {
1327 if (!yyextra->current->argList.empty())
1328 {
1329 yyextra->current->argList.back().docs = c->str;
1330 }
1331 }
1332 }
1333<Parameterlist>{ID} {
1334 //yyextra->current->type not yet available
1335 DString param = DString(yytext).lower();
1336 // std::cout << "3=========> got parameter " << param << "\n";
1337 yyextra->current->args += param;
1338 Argument arg;
1339 arg.name = param;
1340 arg.type = "";
1341 yyextra->current->argList.push_back(arg);
1342 }
DString name
Definition arguments.h:45
void push_back(char c)
Definition dstring.h:202
1343<Parameterlist>{NOARGS} {
1344 newLine(yyscanner);
1345 //printf("3=========> without parameterlist \n");
1346 addCurrentEntry(yyscanner,true);
1347 startScope(yyscanner,yyextra->last_entry.get());
1348 BEGIN(SubprogBody);
1349 }
1350<SubprogBody>result{BS}\‍({BS}{ID} {
1351 if (yyextra->functionLine)
1352 {
1353 DString result= yytext;
1354 result= result.mid(result.find('(')+1);
1355 result= result.stripWhiteSpace();
1356 yyextra->modifiers[yyextra->current_root->parent()][yyextra->current_root->name.lower().str()].returnName = result;
1357 }
1358 //cout << "=====> got result " << result << endl;
1359 }
DString mid(size_t index, size_t len=npos) const
Definition dstring.h:318
1360
1361 /*---- documentation yyextra->comments --------------------------------------------------------------------*/
1362
1363<FVariable,SubprogBody,ModuleBody,TypedefBody,TypedefBodyContains>"!<" { /* backward docu comment */
1364 if (yyextra->vtype != V_IGNORE)
1365 {
1366 yyextra->current->docLine = yyextra->lineNr;
1367 yyextra->docBlockJavaStyle = false;
1368 yyextra->docBlock.clear();
1369 yyextra->docBlockJavaStyle = Config_getBool(JAVADOC_AUTOBRIEF);
1370 startCommentBlock(yyscanner,false);
1371 yy_push_state(DocBackLine,yyscanner);
1372 }
1373 else
1374 {
1375 /* handle out of place !< comment as a normal comment */
1376 if (YY_START == String)
1377 {
1378 yyextra->colNr -= (int)yyleng;
1379 REJECT;
1380 } // "!" is ignored in strings
1381 // skip comment line (without docu yyextra->comments "!>" "!<" )
1382 /* ignore further "!" and ignore yyextra->comments in Strings */
1383 if ((YY_START != StrIgnore) && (YY_START != String))
1384 {
1385 yy_push_state(YY_START,yyscanner);
1386 BEGIN(StrIgnore);
1387 yyextra->debugStr="*!";
1388 }
1389 }
1390 }
#define Config_getBool(name)
Definition config.h:33
1391<DocBackLine>.* { // contents of yyextra->current comment line
1392 yyextra->docBlock+=yytext;
1393 }
1394<DocBackLine>"\n"{BS}"!"("<"|"!"+) { // comment block (next line is also comment line)
1395 yyextra->docBlock+="\n"; // \n is necessary for lists
1396 newLine(yyscanner);
1397 }
1398<DocBackLine>"\n" { // comment block ends at the end of this line
1399 //cout <<"3=========> comment block : "<< yyextra->docBlock << endl;
1400 yyextra->colNr -= 1;
1401 unput(*yytext);
1402 if (yyextra->vtype == V_VARIABLE)
1403 {
1404 std::shared_ptr<Entry> tmp_entry = yyextra->current;
1405 // temporarily switch to the previous entry
1406 if (yyextra->last_enum)
1407 {
1408 yyextra->current = yyextra->last_enum;
1409 }
1410 else
1411 {
1412 yyextra->current = yyextra->last_entry;
1413 }
1414 handleCommentBlock(yyscanner,stripIndentation(yyextra->docBlock),true);
1415 // switch back
1416 yyextra->current = std::move(tmp_entry);
1417 }
1418 else if (yyextra->vtype == V_PARAMETER)
1419 {
1420 subrHandleCommentBlock(yyscanner,yyextra->docBlock,true);
1421 }
1422 else if (yyextra->vtype == V_RESULT)
1423 {
1424 subrHandleCommentBlockResult(yyscanner,yyextra->docBlock,true);
1425 }
1426 pop_state(yyscanner);
1427 yyextra->docBlock.clear();
1428 }
DString stripIndentation(const DString &s, bool skipFirstLine)
Definition util.cpp:4696
1429
1430<Start,SubprogBody,ModuleBody,TypedefBody,InterfaceBody,ModuleBodyContains,SubprogBodyContains,TypedefBodyContains,FEnum>^{BS} {
1431 yyextra->curIndent = computeIndent(yytext);
1432 }
1433<Start,SubprogBody,ModuleBody,TypedefBody,InterfaceBody,ModuleBodyContains,SubprogBodyContains,TypedefBodyContains,FEnum>"!>" {
1434 yy_push_state(YY_START,yyscanner);
1435 yyextra->current->docLine = yyextra->lineNr;
1436 yyextra->docBlockJavaStyle = false;
1437 if (YY_START==SubprogBody) yyextra->docBlockInBody = true;
1438 yyextra->docBlock.clear();
1439 yyextra->docBlockJavaStyle = Config_getBool(JAVADOC_AUTOBRIEF);
1440 startCommentBlock(yyscanner,false);
1441 BEGIN(DocBlock);
1442 //cout << "start DocBlock " << endl;
1443 }
1444
1445
1446<DocBlock>({CMD}{CMD}){ID}/[^a-z_A-Z0-9] { // escaped command
1447 yyextra->docBlock += yytext;
1448 }
1449<DocBlock>{CMD}("f$"|"f["|"f{"|"f(") {
1450 yyextra->docBlock += yytext;
1451 yyextra->docBlockName=&yytext[1];
1452 if (yyextra->docBlockName.at(1)=='[')
1453 {
1454 yyextra->docBlockName.at(1)=']';
1455 }
1456 if (yyextra->docBlockName.at(1)=='{')
1457 {
1458 yyextra->docBlockName.at(1)='}';
1459 }
1460 if (yyextra->docBlockName.at(1)=='(')
1461 {
1462 yyextra->docBlockName.at(1)=')';
1463 }
1464 yyextra->fencedSize=0;
1465 pushBlockState(yyscanner,yytext);
1466 BEGIN(DocCopyBlock);
1467 }
1468<DocBlock>{CMD}"ifile"{B}+"\""[^\n\"]+"\"" {
1469 yyextra->fileName = &yytext[6];
1470 yyextra->fileName = yyextra->fileName.stripWhiteSpace();
1471 yyextra->fileName = yyextra->fileName.mid(1,yyextra->fileName.length()-2);
1472 yyextra->docBlock += yytext;
1473 }
1474<DocBlock>{CMD}"ifile"{B}+{FILEMASK} {
1475 yyextra->fileName = &yytext[6];
1476 yyextra->fileName = yyextra->fileName.stripWhiteSpace();
1477 yyextra->docBlock += yytext;
1478 }
1479<DocBlock>{CMD}"iline"{LINENR}/[\n\.] |
1480<DocBlock>{CMD}"iline"{LINENR}{B} {
1481 bool ok = false;
1482 int nr = DString(&yytext[6]).toInt(&ok);
1483 if (!ok)
1484 {
1485 warn(yyextra->fileName,yyextra->lineNr,"Invalid line number '{}' for iline command",yytext);
1486 }
1487 else
1488 {
1489 yyextra->lineNr = nr;
1490 }
1491 yyextra->docBlock += yytext;
1492 }
int toInt(bool *ok=nullptr, int base=10) const
Definition dstring.cpp:191
1493<DocBlock>{B}*"<"{PRE}">" {
1494 yyextra->docBlock += yytext;
1495 yyextra->docBlockName="<pre>";
1496 yyextra->fencedSize=0;
1497 pushBlockState(yyscanner,yytext);
1498 BEGIN(DocCopyBlock);
1499 }
1500<DocBlock>{B}*"<"<CODE>">" {
1501 yyextra->docBlock += yytext;
1502 yyextra->docBlockName="<code>";
1503 yyextra->fencedSize=0;
1504 pushBlockState(yyscanner,yytext);
1505 BEGIN(DocCopyBlock);
1506 }
1507<DocBlock>{CMD}"startuml"/[^a-z_A-Z0-9\-] { // verbatim command
1508 yyextra->docBlock += yytext;
1509 yyextra->docBlockName="uml";
1510 yyextra->fencedSize=0;
1511 pushBlockState(yyscanner,yytext);
1512 BEGIN(DocCopyBlock);
1513 }
1514<DocBlock>{CMD}("verbatim"|"iliteral"|"latexonly"|"htmlonly"|"xmlonly"|"manonly"|"rtfonly"|"docbookonly"|"dot"|"msc"|"mermaid"|"code")/[^a-z_A-Z0-9\-] { // verbatim command
1515 yyextra->docBlock += yytext;
1516 yyextra->docBlockName=&yytext[1];
1517 yyextra->fencedSize=0;
1518 pushBlockState(yyscanner,yytext);
1519 BEGIN(DocCopyBlock);
1520 }
1521<DocBlock>"~~~"[~]* {
1522 DString pat = yytext;
1523 yyextra->docBlock += pat;
1524 yyextra->docBlockName="~~~";
1525 yyextra->fencedSize=pat.length();
1526 pushBlockState(yyscanner,yytext);
1527 BEGIN(DocCopyBlock);
1528 }
1529<DocBlock>"```"[`]*/(".")?[a-zA-Z0-9#_-]+ |
1530<DocBlock>"```"[`]*/"{"[^}]+"}" |
1531<DocBlock>"```"[`]* {
1532 DString pat = yytext;
1533 yyextra->docBlock += pat;
1534 yyextra->docBlockName="```";
1535 yyextra->fencedSize=pat.length();
1536 pushBlockState(yyscanner,yytext);
1537 BEGIN(DocCopyBlock);
1538 }
1539<DocBlock>"\\ilinebr "{BS} {
1540 DString indent;
1541 int extraSpaces = std::max(0,static_cast<int>(yyleng-9-yyextra->curIndent-2));
1542 indent.fill(' ',extraSpaces);
1543 //printf("extraSpaces=%d\n",extraSpaces);
1544 yyextra->docBlock += "\\ilinebr ";
1545 yyextra->docBlock += indent;
1546 }
DString fill(char c, size_t len)
Fills a string with a predefined character.
Definition dstring.h:278
1547
1548<DocBlock>[^@*`~\/\\\n]+ { // any character that isn't special
1549 yyextra->docBlock += yytext;
1550 }
1551<DocBlock>"\n"{BS}"!"(">"|"!"+) { // comment block (next line is also comment line)
1552 yyextra->docBlock+="\n"; // \n is necessary for lists
1553 newLine(yyscanner);
1554 }
1555<DocBlock>"\n" { // comment block ends at the end of this line
1556 //cout <<"3=========> comment block : "<< yyextra->docBlock << endl;
1557 yyextra->colNr -= 1;
1558 unput(*yytext);
1559 handleCommentBlock(yyscanner,stripIndentation(yyextra->docBlock),true);
1560 pop_state(yyscanner);
1561 }
1562<DocBlock>. { // command block
1563 yyextra->docBlock += *yytext;
1564 }
1565
1566 /* ---- Copy verbatim sections ------ */
1567
1568<DocCopyBlock>"</"{PRE}">" { // end of a <pre> block
1569 yyextra->docBlock += yytext;
1570 if (yyextra->docBlockName=="<pre>")
1571 {
1572 yyextra->docBlockName="";
1573 popBlockState(yyscanner);
1574 BEGIN(DocBlock);
1575 }
1576 }
1577<DocCopyBlock>"</"{CODE}">" { // end of a <code> block
1578 yyextra->docBlock += yytext;
1579 if (yyextra->docBlockName=="<code>")
1580 {
1581 yyextra->docBlockName="";
1582 popBlockState(yyscanner);
1583 BEGIN(DocBlock);
1584 }
1585 }
1586<DocCopyBlock>{CMD}("f$"|"f]"|"f}"|"f)") {
1587 yyextra->docBlock += yytext;
1588 if (yyextra->docBlockName==&yytext[1])
1589 {
1590 yyextra->docBlockName="";
1591 popBlockState(yyscanner);
1592 BEGIN(DocBlock);
1593 }
1594 }
1595<DocCopyBlock>{CMD}("endverbatim"|"endiliteral"|"endlatexonly"|"endhtmlonly"|"endxmlonly"|"enddocbookonly"|"endmanonly"|"endrtfonly"|"enddot"|"endmsc"|"endmermaid"|"enduml"|"endcode")/[^a-z_A-Z0-9] { // end of verbatim block
1596 yyextra->docBlock += yytext;
1597 if (&yytext[4]==yyextra->docBlockName)
1598 {
1599 yyextra->docBlockName="";
1600 popBlockState(yyscanner);
1601 BEGIN(DocBlock);
1602 }
1603 }
1604
1605<DocCopyBlock>^{B}*{COMM} { // start of a comment line, just ignore the comment indicator and leading whitespace
1606 }
1607<DocCopyBlock>"~~~"[~]* {
1608 DString pat = yytext;
1609 yyextra->docBlock += pat;
1610 if (yyextra->docBlockName == "~~~" && yyextra->fencedSize==pat.length())
1611 {
1612 popBlockState(yyscanner);
1613 BEGIN(DocBlock);
1614 }
1615 }
1616<DocCopyBlock>"```"[`]* {
1617 DString pat = yytext;
1618 yyextra->docBlock += pat;
1619 if (yyextra->docBlockName == "```" && yyextra->fencedSize==pat.length())
1620 {
1621 popBlockState(yyscanner);
1622 BEGIN(DocBlock);
1623 }
1624 }
1625
1626<DocCopyBlock>[^<@/\*\‍]!`~"\$\\\n]+ { // any character that is not special
1627 yyextra->docBlock += yytext;
1628 }
1629<DocCopyBlock>\n { // newline
1630 yyextra->docBlock += *yytext;
1631 newLine(yyscanner);
1632 }
1633<DocCopyBlock>. { // any other character
1634 yyextra->docBlock += *yytext;
1635 }
1636
1637 /*-----Prototype parsing -------------------------------------------------------------------------*/
1638<Prototype>{BS}{SUBPROG}{BS_} {
1639 BEGIN(PrototypeSubprog);
1640 }
1641<Prototype,PrototypeSubprog>{BS}{SCOPENAME}?{BS}{ID} {
1642 yyextra->current->name = DString(yytext).lower();
1643 yyextra->current->name.stripWhiteSpace();
1644 BEGIN(PrototypeArgs);
1645 }
1646<PrototypeArgs>{
1647"("|")"|","|{BS_} { yyextra->current->args += yytext; }
1648{ID} { yyextra->current->args += yytext;
1649 Argument a;
1650 a.name = DString(yytext).lower();
1651 yyextra->current->argList.push_back(a);
1652 }
1653}
1654
1655 /*------------------------------------------------------------------------------------------------*/
1656
1657<*>"\n" {
1658 newLine(yyscanner);
1659 //if (yyextra->debugStr.stripWhiteSpace().length() > 0) cout << "ignored text: " << yyextra->debugStr << " state: " <<YY_START << endl;
1660 yyextra->debugStr="";
1661 }
1662
1663
1664 /*---- error: EOF in wrong state --------------------------------------------------------------------*/
1665
1666<*><<EOF>> {
1667 if (yyextra->parsingPrototype)
1668 {
1669 yyterminate();
1670 }
1671 else if ( yyextra->includeStackPtr <= 0 )
1672 {
1673 if (YY_START!=INITIAL && YY_START!=Start)
1674 {
1675 DBG_CTX((stderr,"==== Error: EOF reached in wrong state (end missing)"));
1676 scanner_abort(yyscanner);
1677 }
1678 yyterminate();
1679 }
1680 else
1681 {
1682 popBuffer(yyscanner);
1683 }
1684 }
1685<*>{LOG_OPER} { // Fortran logical comparison keywords
1686 }
1687<*>. {
1688 //yyextra->debugStr+=yytext;
1689 //printf("I:%c\n", *yytext);
1690 } // ignore remaining text
1691
1692 /**********************************************************************************/
1693 /**********************************************************************************/
1694 /**********************************************************************************/
1695%%
1696//----------------------------------------------------------------------------
1697
1698static void newLine(yyscan_t yyscanner)
1699{
1700 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1701 yyextra->lineNr++;
1702 yyextra->lineNr+=yyextra->lineCountPrepass;
1703 yyextra->lineCountPrepass=0;
1704 yyextra->comments.clear();
1705}
1706
1707static inline int computeIndent(const char *s)
1708{
1709 int col=0;
1710 int tabSize=Config_getInt(TAB_SIZE);
1711 const char *p=s;
1712 char c = 0;
1713 while ((c=*p++))
1714 {
1715 if (c=='\t') col+=tabSize-(col%tabSize);
1716 else if (c=='\n') col=0;
1717 else col++;
1718 }
1719 return col;
1720}
1721
1722static const CommentInPrepass *locatePrepassComment(yyscan_t yyscanner,int from, int to)
1723{
1724 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1725 //printf("Locate %d-%d\n", from, to);
1726 for (const auto &cip : yyextra->comments)
1727 { // todo: optimize
1728 int c = cip.column;
1729 //printf("Candidate %d\n", c);
1730 if (c>=from && c<=to)
1731 {
1732 // comment for previous variable or parameter
1733 return &cip;
1734 }
1735 }
1736 return nullptr;
1737}
1738
1739static void updateVariablePrepassComment(yyscan_t yyscanner,int from, int to)
1740{
1741 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1742 const CommentInPrepass *c = locatePrepassComment(yyscanner,from, to);
1743 if (c && yyextra->vtype == V_VARIABLE)
1744 {
1745 yyextra->last_entry->brief = c->str;
1746 }
1747 else if (c && yyextra->vtype == V_PARAMETER)
1748 {
1749 Argument *parameter = getParameter(yyscanner,yyextra->argName);
1750 if (parameter) parameter->docs = c->str;
1751 }
1752}
1753
1754static int getAmpersandAtTheStart(const char *buf, int length)
1755{
1756 for(int i=0; i<length; i++)
1757 {
1758 switch(buf[i])
1759 {
1760 case ' ':
1761 case '\t':
1762 break;
1763 case '&':
1764 return i;
1765 default:
1766 return -1;
1767 }
1768 }
1769 return -1;
1770}
1771
1772/* Returns ampersand index, comment start index or -1 if neither exist.*/
1773static int getAmpOrExclAtTheEnd(const char *buf, int length, char ch)
1774{
1775 // Avoid ampersands in string and yyextra->comments
1776 int parseState = Start;
1777 char quoteSymbol = 0;
1778 int ampIndex = -1;
1779 int commentIndex = -1;
1780 quoteSymbol = ch;
1781 if (ch != '\0') parseState = String;
1782
1783 for(int i=0; i<length && parseState!=Comment; i++)
1784 {
1785 // When in string, skip backslashes
1786 // Legacy code, not sure whether this is correct?
1787 if (parseState==String)
1788 {
1789 if (buf[i]=='\\') i++;
1790 }
1791
1792 switch(buf[i])
1793 {
1794 case '\'':
1795 case '"':
1796 // Close string, if quote symbol matches.
1797 // Quote symbol is set iff parseState==String
1798 if (buf[i]==quoteSymbol)
1799 {
1800 parseState = Start;
1801 quoteSymbol = 0;
1802 }
1803 // Start new string, if not already in string or comment
1804 else if (parseState==Start)
1805 {
1806 parseState = String;
1807 quoteSymbol = buf[i];
1808 }
1809 ampIndex = -1; // invalidate prev ampersand
1810 break;
1811 case '!':
1812 // When in string or comment, ignore exclamation mark
1813 if (parseState==Start)
1814 {
1815 parseState = Comment;
1816 commentIndex = i;
1817 }
1818 break;
1819 case ' ': // ignore whitespace
1820 case '\t':
1821 case '\n': // this may be at the end of line
1822 break;
1823 case '&':
1824 ampIndex = i;
1825 break;
1826 default:
1827 ampIndex = -1; // invalidate prev ampersand
1828 }
1829 }
1830
1831 if (ampIndex>=0)
1832 return ampIndex;
1833 else
1834 return commentIndex;
1835}
1836
1837/* Although yyextra->comments at the end of continuation line are grabbed by this function,
1838* we still do not know how to use them later in parsing.
1839*/
1840void truncatePrepass(yyscan_t yyscanner,int index)
1841{
1842 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
1843 size_t length = yyextra->inputStringPrepass.length();
1844 for (size_t i=index+1; i<length; i++) {
1845 if (yyextra->inputStringPrepass[i]=='!' && i<length-1 && yyextra->inputStringPrepass[i+1]=='<') { // save comment
1846 yyextra->comments.emplace_back(index, yyextra->inputStringPrepass.right(length-i-2));
1847 }
1848 }
1849 yyextra->inputStringPrepass.resize(index);
1850}
1851
1852/* This function assumes that contents has at least size=length+1 */
1853static void insertCharacter(char *contents, int length, int pos, char c)
1854{
1855 // shift tail by one character
1856 for(int i=length; i>pos; i--)
1857 contents[i]=contents[i-1];
1858 // set the character
1859 contents[pos] = c;
1860}
1861
1862/* change yyextra->comments and bring line continuation character to previous line */
1863/* also used to set continuation marks in case of fortran code usage, done here as it is quite complicated code */
1864const char* prepassFixedForm(const char* contents, int *hasContLine,int fixedCommentAfter)
1865{
1866 int column=0;
1867 int prevLineLength=0;
1868 int prevLineAmpOrExclIndex=-1;
1869 int skipped = 0;
1870 char prevQuote = '\0';
1871 char thisQuote = '\0';
1872 bool emptyLabel=true;
1873 bool commented=false;
1874 bool inSingle=false;
1875 bool inDouble=false;
1876 bool inBackslash=false;
1877 bool fullCommentLine=true;
1878 bool artificialComment=false;
1879 bool spaces=true;
1880 int newContentsSize = (int)strlen(contents)+3; // \000, \n (when necessary) and one spare character (to avoid reallocation)
1881 char* newContents = (char*)malloc(newContentsSize);
1882 int curLine = 1;
1883 size_t sizCont;
1884
1885 int j = -1;
1886 sizCont = strlen(contents);
1887 for(size_t i=0;i<sizCont;i++) {
1888 column++;
1889 char c = contents[i];
1890 if (artificialComment && c != '\n')
1891 {
1892 if (c == '!' && spaces)
1893 {
1894 newContents[j++] = c;
1895 artificialComment = false;
1896 spaces = false;
1897 skipped = 0;
1898 continue;
1899 }
1900 else if (c == ' ' || c == '\t') continue;
1901 else
1902 {
1903 spaces = false;
1904 skipped++;
1905 continue;
1906 }
1907 }
1908
1909 j++;
1910 if (j>=newContentsSize-3) { // check for spare characters, which may be eventually used below (by & and '! ')
1911 newContents = (char*)realloc(newContents, newContentsSize+1000);
1912 newContentsSize = newContentsSize+1000;
1913 }
1914
1915 switch(c) {
1916 case '\n':
1917 if (!fullCommentLine)
1918 {
1919 prevLineLength=column;
1920 prevLineAmpOrExclIndex=getAmpOrExclAtTheEnd(&contents[i-prevLineLength+1], prevLineLength,prevQuote);
1921 if (prevLineAmpOrExclIndex == -1) prevLineAmpOrExclIndex = column - 1;
1922 if (skipped)
1923 {
1924 prevLineAmpOrExclIndex = -1;
1925 skipped = 0;
1926 }
1927 }
1928 else
1929 {
1930 prevLineLength+=column;
1931 /* Even though a full comment line is not really a comment line it can be seen as one. An empty line is also seen as a comment line (small bonus) */
1932 if (hasContLine)
1933 {
1934 hasContLine[curLine - 1] = 1;
1935 }
1936 }
1937 artificialComment=false;
1938 spaces=true;
1939 fullCommentLine=true;
1940 column=0;
1941 emptyLabel=true;
1942 commented=false;
1943 newContents[j]=c;
1944 prevQuote = thisQuote;
1945 curLine++;
1946 break;
1947 case ' ':
1948 case '\t':
1949 newContents[j]=c;
1950 break;
1951 case '\000':
1952 if (hasContLine)
1953 {
1954 free(newContents);
1955 return nullptr;
1956 }
1957 newContents[j]='\000';
1958 newContentsSize = (int)strlen(newContents);
1959 if (newContents[newContentsSize - 1] != '\n')
1960 {
1961 // to be on the safe side
1962 newContents = (char*)realloc(newContents, newContentsSize+2);
1963 newContents[newContentsSize] = '\n';
1964 newContents[newContentsSize + 1] = '\000';
1965 }
1966 return newContents;
1967 case '"':
1968 case '\'':
1969 case '\\':
1970 if ((column <= fixedCommentAfter) && (column!=6) && !commented)
1971 {
1972 // we have some special cases in respect to strings and escaped string characters
1973 fullCommentLine=false;
1974 newContents[j]=c;
1975 if (c == '\\')
1976 {
1977 inBackslash = !inBackslash;
1978 break;
1979 }
1980 else if (c == '\'')
1981 {
1982 if (!inDouble)
1983 {
1984 inSingle = !inSingle;
1985 if (inSingle) thisQuote = c;
1986 else thisQuote = '\0';
1987 }
1988 break;
1989 }
1990 else if (c == '"')
1991 {
1992 if (!inSingle)
1993 {
1994 inDouble = !inDouble;
1995 if (inDouble) thisQuote = c;
1996 else thisQuote = '\0';
1997 }
1998 break;
1999 }
2000 }
2001 inBackslash = false;
2002 // fallthrough
2003 case '#':
2004 case 'C':
2005 case 'c':
2006 case '*':
2007 case '!':
2008 if ((column <= fixedCommentAfter) && (column!=6))
2009 {
2010 emptyLabel=false;
2011 if (column==1)
2012 {
2013 newContents[j]='!';
2014 commented = true;
2015 }
2016 else if ((c == '!') && !inDouble && !inSingle)
2017 {
2018 newContents[j]=c;
2019 commented = true;
2020 }
2021 else
2022 {
2023 if (!commented) fullCommentLine=false;
2024 newContents[j]=c;
2025 }
2026 break;
2027 }
2028 // fallthrough
2029 default:
2030 if (!commented && (column < 6) && ((c - '0') >= 0) && ((c - '0') <= 9))
2031 { // remove numbers, i.e. labels from first 5 positions.
2032 newContents[j]=' ';
2033 }
2034 else if (column==6 && emptyLabel)
2035 { // continuation
2036 if (!commented) fullCommentLine=false;
2037 if (c != '0')
2038 { // 0 not allowed as continuation character, see f95 standard paragraph 3.3.2.3
2039 newContents[j]=' ';
2040
2041 if (prevLineAmpOrExclIndex==-1)
2042 { // add & just before end of previous line
2043 /* first line is not a continuation line in code, just in snippets etc. */
2044 if (curLine != 1) insertCharacter(newContents, j+1, (j+1)-6-1, '&');
2045 j++;
2046 }
2047 else
2048 { // add & just before end of previous line comment
2049 /* first line is not a continuation line in code, just in snippets etc. */
2050 if (curLine != 1) insertCharacter(newContents, j+1, (j+1)-6-prevLineLength+prevLineAmpOrExclIndex+skipped, '&');
2051 skipped = 0;
2052 j++;
2053 }
2054 if (hasContLine)
2055 {
2056 hasContLine[curLine - 1] = 1;
2057 }
2058 }
2059 else
2060 {
2061 newContents[j]=c; // , just handle like space
2062 }
2063 prevLineLength=0;
2064 }
2065 else if ((column > fixedCommentAfter) && !commented)
2066 {
2067 // first non commented non blank character after position fixedCommentAfter
2068 if (c == '&')
2069 {
2070 newContents[j]=' ';
2071 }
2072 else if (c != '!')
2073 {
2074 // I'm not a possible start of doxygen comment
2075 newContents[j]=' ';
2076 artificialComment = true;
2077 spaces=true;
2078 skipped = 0;
2079 }
2080 else
2081 {
2082 newContents[j]=c;
2083 commented = true;
2084 }
2085 }
2086 else
2087 {
2088 if (!commented) fullCommentLine=false;
2089 newContents[j]=c;
2090 emptyLabel=false;
2091 }
2092 break;
2093 }
2094 }
2095
2096 if (hasContLine)
2097 {
2098 free(newContents);
2099 return nullptr;
2100 }
2101
2102 if (j==-1) // contents was empty
2103 {
2104 newContents = (char*)realloc(newContents, 2);
2105 newContents[0] = '\n';
2106 newContents[1] = '\000';
2107 }
2108 else if (newContents[j] == '\n') // content ended with newline
2109 {
2110 newContents = (char*)realloc(newContents, j+2);
2111 newContents[j + 1] = '\000';
2112 }
2113 else // content did not end with a newline
2114 {
2115 newContents = (char*)realloc(newContents, j+3);
2116 newContents[j + 1] = '\n';
2117 newContents[j + 2] = '\000';
2118 }
2119 return newContents;
2120}
2121
2122
2123//------------------------------------------------------
2124static bool keyWordsFortranC(const char *contents)
2125{
2126 static const std::unordered_set<std::string> fortran_C_keywords = {
2127 "character", "call", "close", "common", "continue",
2128 "case", "contains", "cycle", "class", "codimension",
2129 "concurrent", "contiguous", "critical"
2130 };
2131
2132 if (*contents != 'c' && *contents != 'C') return false;
2133
2134 const char *c = contents;
2135 DString keyword;
2136 while (*c && *c != ' ') {keyword += *c; c++;}
2137 keyword = keyword.lower();
2138
2139 return (fortran_C_keywords.find(keyword.str()) != fortran_C_keywords.end());
2140}
2141
2142// simplified way to know if this is fixed form
2143bool recognizeFixedForm(const DString &contents, FortranFormat format)
2144{
2145 int column=0;
2146 bool skipLine=false;
2147
2148 if (format == FortranFormat::Fixed) return true;
2149 if (format == FortranFormat::Free) return false;
2150
2151 int tabSize=Config_getInt(TAB_SIZE);
2152 size_t sizCont = contents.length();
2153 for (size_t i=0;i<sizCont;i++)
2154 {
2155 column++;
2156
2157 switch(contents.at(i))
2158 {
2159 case '\n':
2160 column=0;
2161 skipLine=false;
2162 break;
2163 case '\t':
2164 column += tabSize-1;
2165 break;
2166 case ' ':
2167 break;
2168 case '\000':
2169 return false;
2170 case '#':
2171 skipLine=true;
2172 break;
2173 case 'C':
2174 case 'c':
2175 if (column==1)
2176 {
2177 return !keyWordsFortranC(contents.data()+i);
2178 }
2179 // fallthrough
2180 case '*':
2181 if (column==1) return true;
2182 if (skipLine) break;
2183 return false;
2184 case '!':
2185 if (column!=6) skipLine=true;
2186 break;
2187 default:
2188 if (skipLine) break;
2189 if (column>=7) return true;
2190 return false;
2191 }
2192 }
2193 return false;
2194}
2195
2197{
2198 DString ext = getFileNameExtension(fn);
2199 DString parserName = Doxygen::parserManager->getParserName(ext);
2200
2201 if (parserName == "fortranfixed") return FortranFormat::Fixed;
2202 else if (parserName == "fortranfree") return FortranFormat::Free;
2203
2205}
2206
2207//------------------------------------------------------------------------
2208
2209
2210static void pushBuffer(yyscan_t yyscanner,const DString &buffer)
2211{
2212 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2213 if (yyextra->includeStackCnt <= yyextra->includeStackPtr)
2214 {
2215 yyextra->includeStackCnt++;
2216 yyextra->includeStack = (YY_BUFFER_STATE *)realloc(yyextra->includeStack, yyextra->includeStackCnt * sizeof(YY_BUFFER_STATE));
2217 }
2218 yyextra->includeStack[yyextra->includeStackPtr++] = YY_CURRENT_BUFFER;
2219 yy_switch_to_buffer(yy_scan_string(buffer.data(),yyscanner),yyscanner);
2220
2221 DBG_CTX((stderr, "--PUSH--%s", qPrint(buffer)));
2222}
2223
2224static void popBuffer(yyscan_t yyscanner)
2225{
2226 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2227 DBG_CTX((stderr, "--POP--"));
2228 yyextra->includeStackPtr --;
2229 yy_delete_buffer( YY_CURRENT_BUFFER, yyscanner );
2230 yy_switch_to_buffer( yyextra->includeStack[yyextra->includeStackPtr], yyscanner );
2231}
2232
2233/** used to copy entry to an interface module procedure */
2234static void copyEntry(std::shared_ptr<Entry> dest, const std::shared_ptr<Entry> &src)
2235{
2236 dest->type = src->type;
2237 dest->fileName = src->fileName;
2238 dest->startLine = src->startLine;
2239 dest->bodyLine = src->bodyLine;
2240 dest->endBodyLine = src->endBodyLine;
2241 dest->args = src->args;
2242 dest->argList = src->argList;
2243 dest->doc = src->doc;
2244 dest->docLine = src->docLine;
2245 dest->docFile = src->docFile;
2246 dest->brief = src->brief;
2247 dest->briefLine= src->briefLine;
2248 dest->briefFile= src->briefFile;
2249}
2250
2251/** fill empty interface module procedures with info from
2252 corresponding module subprogs
2253
2254 TODO: handle procedures in used modules
2255*/
2256void resolveModuleProcedures(yyscan_t yyscanner,Entry *current_root)
2257{
2258 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2259 if (yyextra->moduleProcedures.empty()) return;
2260
2261 // build up map of available functions
2262 std::map<std::string,std::shared_ptr<Entry>> procMap;
2263 {
2264 for (const auto& cf: current_root->children())
2265 {
2266 if (!cf->section.isFunction())
2267 continue;
2268
2269 // remove scope from name
2270 DString name = cf->name;
2271 {
2272 if (size_t end = name.rfind(':') ; end!=DString::npos) name.remove(0, end+1);
2273 }
2274
2275 procMap.emplace(name.str(), cf);
2276 }
2277 }
2278
2279
2280 // for all module procedures
2281 for (const auto& ce1: yyextra->moduleProcedures)
2282 {
2283 if (procMap.find(ce1->name.str())!=procMap.end())
2284 {
2285 std::shared_ptr<Entry> proc = procMap[ce1->name.str()];
2286 copyEntry(ce1, proc);
2287 }
2288 } // for all interface module procedures
2289 yyextra->moduleProcedures.clear();
2290}
2291
2292/*! Extracts string which resides within parentheses of provided string. */
2294{
2295 DString extracted = name;
2296 if (size_t start = extracted.find('('); start!=DString::npos)
2297 {
2298 extracted.remove(0, start+1);
2299 }
2300 if (size_t end = extracted.rfind(')'); end!=DString::npos)
2301 {
2302 size_t length = extracted.length();
2303 extracted.remove(end, length);
2304 }
2305 extracted = extracted.stripWhiteSpace();
2306
2307 return extracted;
2308}
2309
2310/*! remove useless spaces from bind statement */
2311static DString extractBind(const DString &name)
2312{
2313 DString parensPart = extractFromParens(name);
2314 if (parensPart.length() == 1)
2315 {
2316 return "bind(C)";
2317 }
2318 else
2319 {
2320 //strip 'c'
2321 parensPart = parensPart.mid(1).stripWhiteSpace();
2322 // strip ','
2323 parensPart = parensPart.mid(1).stripWhiteSpace();
2324 // name part
2325 parensPart = parensPart.mid(4).stripWhiteSpace();
2326 // = part
2327 parensPart = parensPart.mid(1).stripWhiteSpace();
2328
2329 return "bind(C, name=" + parensPart + ")";
2330 }
2331}
2332
2333/*! Adds passed yyextra->modifiers to these yyextra->modifiers.*/
2335{
2336 if (mdfs.protection!=NONE_P) protection = mdfs.protection;
2337 if (mdfs.direction!=NONE_D) direction = mdfs.direction;
2338 optional |= mdfs.optional;
2339 if (!mdfs.dimension.empty()) dimension = mdfs.dimension;
2340 allocatable |= mdfs.allocatable;
2341 external |= mdfs.external;
2342 intrinsic |= mdfs.intrinsic;
2343 protect |= mdfs.protect;
2344 parameter |= mdfs.parameter;
2345 pointer |= mdfs.pointer;
2346 target |= mdfs.target;
2347 save |= mdfs.save;
2348 deferred |= mdfs.deferred;
2350 nopass |= mdfs.nopass;
2351 pass |= mdfs.pass;
2352 passVar = mdfs.passVar;
2353 bindVar = mdfs.bindVar;
2354 contiguous |= mdfs.contiguous;
2355 volat |= mdfs.volat;
2356 value |= mdfs.value;
2357 return *this;
2358}
2359
2360/*! Extracts and adds passed modifier to these yyextra->modifiers.*/
2362{
2363 DString mdfString = mdfStringArg.lower();
2364 SymbolModifiers newMdf;
2365
2366 if (mdfString.startsWith("dimension"))
2367 {
2368 newMdf.dimension=mdfString;
2369 }
2370 else if (mdfString.contains("intent"))
2371 {
2372 DString tmp = extractFromParens(mdfString);
2373 bool isin = tmp.find("in")!=DString::npos;
2374 bool isout = tmp.find("out")!=DString::npos;
2375 if (isin && isout) newMdf.direction = SymbolModifiers::INOUT;
2376 else if (isin) newMdf.direction = SymbolModifiers::IN;
2377 else if (isout) newMdf.direction = SymbolModifiers::OUT;
2378 }
2379 else if (mdfString=="public")
2380 {
2382 }
2383 else if (mdfString=="private")
2384 {
2386 }
2387 else if (mdfString=="protected")
2388 {
2389 newMdf.protect = true;
2390 }
2391 else if (mdfString=="optional")
2392 {
2393 newMdf.optional = true;
2394 }
2395 else if (mdfString=="allocatable")
2396 {
2397 newMdf.allocatable = true;
2398 }
2399 else if (mdfString=="external")
2400 {
2401 newMdf.external = true;
2402 }
2403 else if (mdfString=="intrinsic")
2404 {
2405 newMdf.intrinsic = true;
2406 }
2407 else if (mdfString=="parameter")
2408 {
2409 newMdf.parameter = true;
2410 }
2411 else if (mdfString=="pointer")
2412 {
2413 newMdf.pointer = true;
2414 }
2415 else if (mdfString=="target")
2416 {
2417 newMdf.target = true;
2418 }
2419 else if (mdfString=="save")
2420 {
2421 newMdf.save = true;
2422 }
2423 else if (mdfString=="nopass")
2424 {
2425 newMdf.nopass = true;
2426 }
2427 else if (mdfString=="deferred")
2428 {
2429 newMdf.deferred = true;
2430 }
2431 else if (mdfString=="non_overridable")
2432 {
2433 newMdf.nonoverridable = true;
2434 }
2435 else if (mdfString=="contiguous")
2436 {
2437 newMdf.contiguous = true;
2438 }
2439 else if (mdfString=="volatile")
2440 {
2441 newMdf.volat = true;
2442 }
2443 else if (mdfString=="value")
2444 {
2445 newMdf.value = true;
2446 }
2447 else if (mdfString.contains("pass"))
2448 {
2449 newMdf.pass = true;
2450 if (mdfString.contains("("))
2451 newMdf.passVar = extractFromParens(mdfString);
2452 else
2453 newMdf.passVar = "";
2454 }
2455 else if (mdfString.startsWith("bind"))
2456 {
2457 // we need here the original string as we want to don't want to have the lowercase name between the quotes of the name= part
2458 newMdf.bindVar = extractBind(mdfStringArg);
2459 }
2460
2461 (*this) |= newMdf;
2462 return *this;
2463}
2464
2465/*! For debugging purposes. */
2466//ostream& operator<<(ostream& out, const SymbolModifiers& mdfs)
2467//{
2468// out<<mdfs.protection<<", "<<mdfs.direction<<", "<<mdfs.optional<<
2469// ", "<<(mdfs.dimension.empty() ? "" : mdfs.dimension.latin1())<<
2470// ", "<<mdfs.allocatable<<", "<<mdfs.external<<", "<<mdfs.intrinsic;
2471//
2472// return out;
2473//}
2474
2475/*! Find argument with given name in \a subprog entry. */
2476static Argument *findArgument(Entry* subprog, DString name, bool byTypeName = false)
2477{
2478 DString cname(name.lower());
2479 for (Argument &arg : subprog->argList)
2480 {
2481 if ((!byTypeName && arg.name.lower() == cname) ||
2482 (byTypeName && arg.type.lower() == cname)
2483 )
2484 {
2485 return &arg;
2486 }
2487 }
2488 return nullptr;
2489}
2490
2491
2492/*! Apply yyextra->modifiers stored in \a mdfs to the \a typeName string. */
2493static DString applyModifiers(DString typeName, const SymbolModifiers& mdfs)
2494{
2495 if (!mdfs.dimension.empty())
2496 {
2497 if (!typeName.empty()) typeName += ", ";
2498 typeName += mdfs.dimension;
2499 }
2501 {
2502 if (!typeName.empty()) typeName += ", ";
2503 typeName += directionStrs[mdfs.direction];
2504 }
2505 if (mdfs.optional)
2506 {
2507 if (!typeName.empty()) typeName += ", ";
2508 typeName += "optional";
2509 }
2510 if (mdfs.allocatable)
2511 {
2512 if (!typeName.empty()) typeName += ", ";
2513 typeName += "allocatable";
2514 }
2515 if (mdfs.external)
2516 {
2517 if (!typeName.contains("external"))
2518 {
2519 if (!typeName.empty()) typeName += ", ";
2520 typeName += "external";
2521 }
2522 }
2523 if (mdfs.intrinsic)
2524 {
2525 if (!typeName.empty()) typeName += ", ";
2526 typeName += "intrinsic";
2527 }
2528 if (mdfs.parameter)
2529 {
2530 if (!typeName.empty()) typeName += ", ";
2531 typeName += "parameter";
2532 }
2533 if (mdfs.pointer)
2534 {
2535 if (!typeName.empty()) typeName += ", ";
2536 typeName += "pointer";
2537 }
2538 if (mdfs.target)
2539 {
2540 if (!typeName.empty()) typeName += ", ";
2541 typeName += "target";
2542 }
2543 if (mdfs.save)
2544 {
2545 if (!typeName.empty()) typeName += ", ";
2546 typeName += "save";
2547 }
2548 if (mdfs.deferred)
2549 {
2550 if (!typeName.empty()) typeName += ", ";
2551 typeName += "deferred";
2552 }
2553 if (mdfs.nonoverridable)
2554 {
2555 if (!typeName.empty()) typeName += ", ";
2556 typeName += "non_overridable";
2557 }
2558 if (mdfs.nopass)
2559 {
2560 if (!typeName.empty()) typeName += ", ";
2561 typeName += "nopass";
2562 }
2563 if (mdfs.pass)
2564 {
2565 if (!typeName.empty()) typeName += ", ";
2566 typeName += "pass";
2567 if (!mdfs.passVar.empty())
2568 typeName += "(" + mdfs.passVar + ")";
2569 }
2570 if (!mdfs.bindVar.empty())
2571 {
2572 if (!typeName.empty()) typeName += ", ";
2573 typeName += mdfs.bindVar;
2574 }
2576 {
2577 if (!typeName.empty()) typeName += ", ";
2578 typeName += "public";
2579 }
2580 else if (mdfs.protection == SymbolModifiers::PRIVATE)
2581 {
2582 if (!typeName.empty()) typeName += ", ";
2583 typeName += "private";
2584 }
2585 if (mdfs.protect)
2586 {
2587 if (!typeName.empty()) typeName += ", ";
2588 typeName += "protected";
2589 }
2590 if (mdfs.contiguous)
2591 {
2592 if (!typeName.empty()) typeName += ", ";
2593 typeName += "contiguous";
2594 }
2595 if (mdfs.volat)
2596 {
2597 if (!typeName.empty()) typeName += ", ";
2598 typeName += "volatile";
2599 }
2600 if (mdfs.value)
2601 {
2602 if (!typeName.empty()) typeName += ", ";
2603 typeName += "value";
2604 }
2605
2606 return typeName;
2607}
2608
2609/*! Apply yyextra->modifiers stored in \a mdfs to the \a arg argument. */
2610static void applyModifiers(Argument *arg, const SymbolModifiers& mdfs)
2611{
2612 arg->type = applyModifiers(arg->type, mdfs);
2613}
2614
2615/*! Apply yyextra->modifiers stored in \a mdfs to the \a ent entry. */
2616static void applyModifiers(Entry *ent, const SymbolModifiers& mdfs)
2617{
2618 ent->type = applyModifiers(ent->type, mdfs);
2619
2621 ent->protection = Protection::Public;
2622 else if (mdfs.protection == SymbolModifiers::PRIVATE)
2623 ent->protection = Protection::Private;
2624
2625 if (mdfs.nonoverridable)
2626 ent->spec.setFinal(true);
2627 if (mdfs.nopass)
2628 ent->isStatic = true;
2629 if (mdfs.deferred)
2630 ent->virt = Specifier::Pure;
2631}
2632
2633/*! Starts the new scope in fortran program. Consider using this function when
2634 * starting module, interface, function or other program block.
2635 * \see endScope()
2636 */
2637static void startScope(yyscan_t yyscanner,Entry *scope)
2638{
2639 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2640 //cout<<"start scope: "<<scope->name<<endl;
2641 yyextra->current_root= scope; /* start substructure */
2642
2643 yyextra->modifiers.emplace(scope, std::map<std::string,SymbolModifiers>());
2644
2645 // create new current with possibly different defaults...
2646 yyextra->current = std::make_shared<Entry>();
2647 initEntry(yyscanner);
2648}
2649
2650/*! Ends scope in fortran program: may update subprogram arguments or module variable attributes.
2651 * \see startScope()
2652 */
2653static bool endScope(yyscan_t yyscanner,Entry *scope, bool isGlobalRoot)
2654{
2655 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2656 if (yyextra->global_scope == scope)
2657 {
2658 yyextra->global_scope = nullptr;
2659 return true;
2660 }
2661 if (yyextra->global_scope == INVALID_ENTRY)
2662 {
2663 return true;
2664 }
2665 //cout<<"end scope: "<<scope->name<<endl;
2666 if (yyextra->current_root->parent() || isGlobalRoot)
2667 {
2668 yyextra->current_root= yyextra->current_root->parent(); /* end substructure */
2669 }
2670 else // if (yyextra->current_root != scope)
2671 {
2672 fprintf(stderr,"parse error in end <scopename>\n");
2673 scanner_abort(yyscanner);
2674 return false;
2675 }
2676
2677 // create new current with possibly different defaults...
2678 yyextra->current = std::make_shared<Entry>();
2679 initEntry(yyscanner);
2680
2681 // update variables or subprogram arguments with yyextra->modifiers
2682 std::map<std::string,SymbolModifiers>& mdfsMap = yyextra->modifiers[scope];
2683
2684 if (scope->section.isFunction())
2685 {
2686 // iterate all symbol yyextra->modifiers of the scope
2687 for (const auto &kv : mdfsMap)
2688 {
2689 //cout<<it.key()<<": "<<qPrint(it)<<endl;
2690 Argument *arg = findArgument(scope, kv.first);
2691
2692 if (arg)
2693 {
2694 applyModifiers(arg, kv.second);
2695 }
2696 }
2697
2698 // find return type for function
2699 //cout<<"RETURN NAME "<<yyextra->modifiers[yyextra->current_root][scope->name.lower()].returnName<<endl;
2700 DString returnName = yyextra->modifiers[yyextra->current_root][scope->name.lower().str()].returnName.lower();
2701 if (yyextra->modifiers[scope].find(returnName.str())!=yyextra->modifiers[scope].end())
2702 {
2703 scope->type = yyextra->modifiers[scope][returnName.str()].type; // returning type works
2704 applyModifiers(scope, yyextra->modifiers[scope][returnName.str()]); // returning array works
2705 }
2706
2707 }
2708 if (scope->section.isClass() && scope->spec.isInterface())
2709 { // was INTERFACE_SEC
2710 if (scope->parent() && scope->parent()->section.isFunction())
2711 { // interface within function
2712 // iterate functions of interface and
2713 // try to find types for dummy(ie. argument) procedures.
2714 //cout<<"Search in "<<scope->name<<endl;
2715 for (const auto &ce : scope->children())
2716 {
2717 if (!ce->section.isFunction())
2718 continue;
2719
2720 // remove prefix
2721 DString name = ce->name.lower();
2722 if (size_t ii = name.rfind(':'); ii!=DString::npos) name.remove(0, ii+1);
2723 Argument *arg = findArgument(scope->parent(), name);
2724 if (arg)
2725 {
2726 // set type of dummy procedure argument to interface
2727 arg->type = "external " + ce->type + "(";
2728 for (size_t i=0; i<ce->argList.size(); i++)
2729 {
2730 if (i > 0)
2731 {
2732 arg->type = arg->type + ", ";
2733 }
2734 const Argument &subarg = ce->argList.at(i);
2735 arg->type = arg->type + subarg.type + " " + subarg.name;
2736 }
2737 arg->type = arg->type + ")";
2738 arg->name = name;
2739 }
2740 }
2741 // clear all yyextra->modifiers of the scope
2742 yyextra->modifiers.erase(scope);
2743 scope->parent()->removeSubEntry(scope);
2744 scope = nullptr;
2745 return true;
2746 }
2747 }
2748 if (!scope->section.isFunction())
2749 { // not function section
2750 // iterate variables: get and apply yyextra->modifiers
2751 for (const auto &ce : scope->children())
2752 {
2753 if (!ce->section.isVariable() && !ce->section.isFunction() && !ce->section.isClass())
2754 continue;
2755
2756 //cout<<ce->name<<", "<<mdfsMap.contains(ce->name.lower())<<mdfsMap.count()<<endl;
2757 if (mdfsMap.find(ce->name.lower().str())!=mdfsMap.end())
2758 applyModifiers(ce.get(), mdfsMap[ce->name.lower().str()]);
2759
2760 // remove prefix for variable names
2761 if (ce->section.isVariable() || ce->section.isFunction())
2762 {
2763 if (size_t end = ce->name.rfind(':'); end!=DString::npos) ce->name.remove(0, end+1);
2764 }
2765 }
2766 }
2767
2768 // clear all yyextra->modifiers of the scope
2769 yyextra->modifiers.erase(scope);
2770
2771 // resolve procedures in types
2773
2774 return true;
2775}
2776
2777/*! search for types with type bound procedures (e.g. methods)
2778 * and try to resolve their arguments
2779 */
2781{
2782 // map of all subroutines/functions
2783 bool procMapCreated = false;
2784 std::unordered_map<std::string,std::shared_ptr<Entry>> procMap;
2785
2786 // map of all abstract interfaces
2787 bool interfMapCreated = false;
2788 std::unordered_map<std::string,std::shared_ptr<Entry>> interfMap;
2789
2790 // iterate over all types
2791 for (const auto &ce: scope->children())
2792 {
2793 if (!ce->section.isClass())
2794 continue;
2795
2796 // handle non-"generic" non-"deferred" methods, copying the arguments from the implementation
2797 std::unordered_map<std::string,std::shared_ptr<Entry>> methodMap;
2798 for (auto &ct: ce->children())
2799 {
2800 if (!ct->section.isFunction())
2801 continue;
2802
2803 if (ct->type=="generic")
2804 continue;
2805
2806 if (ct->virt==Specifier::Pure)
2807 continue;
2808
2809 // set up the procMap
2810 if (!procMapCreated)
2811 {
2812 for (const auto &cf: scope->children())
2813 {
2814 if (cf->section.isFunction())
2815 {
2816 procMap.emplace(cf->name.str(), cf);
2817 }
2818 }
2819 procMapCreated = true;
2820 }
2821
2822 // found a (non-generic) method
2823 DString implName = ct->args;
2824 if (procMap.find(implName.str())!=procMap.end())
2825 {
2826 std::shared_ptr<Entry> proc = procMap[implName.str()];
2827 ct->args = proc->args;
2828 ct->argList = ArgumentList(proc->argList);
2829 if (ct->brief.empty())
2830 {
2831 ct->brief = proc->brief;
2832 ct->briefLine = proc->briefLine;
2833 ct->briefFile = proc->briefFile;
2834 }
2835 if (ct->doc.empty())
2836 {
2837 ct->doc = proc->doc;
2838 ct->docLine = proc->docLine;
2839 ct->docFile = proc->docFile;
2840 }
2841 methodMap.emplace(ct->name.str(), ct);
2842 }
2843 }
2844
2845 // handle "deferred" methods (pure virtual functions), duplicating with arguments from the target abstract interface
2846 for (auto &ct: ce->children())
2847 {
2848 if (!ct->section.isFunction())
2849 continue;
2850
2851 if (ct->virt != Specifier::Pure)
2852 continue;
2853
2854 // set up the procMap
2855 if (!interfMapCreated)
2856 {
2857 for(const auto &cf: scope->children())
2858 {
2859 if (cf->section.isClass() && cf->spec.isInterface() && cf->type=="abstract")
2860 {
2861 std::shared_ptr<Entry> ci = cf->children().front();
2862 interfMap.emplace(ci->name.str(), ci);
2863 }
2864 }
2865 interfMapCreated = true;
2866 }
2867
2868 // found a (non-generic) method
2869 DString implName = ct->args;
2870 if (interfMap.find(implName.str())!= interfMap.end() )
2871 {
2872 std::shared_ptr<Entry> proc = interfMap[implName.str()];
2873 ct->args = proc->args;
2874 ct->argList = ArgumentList(proc->argList);
2875 if (ct->brief.empty())
2876 {
2877 ct->brief = proc->brief;
2878 ct->briefLine = proc->briefLine;
2879 ct->briefFile = proc->briefFile;
2880 }
2881 if (ct->doc.empty())
2882 {
2883 ct->doc = proc->doc;
2884 ct->docLine = proc->docLine;
2885 ct->docFile = proc->docFile;
2886 }
2887
2888 methodMap.emplace(ct->name.str(), ct);
2889 }
2890 }
2891
2892 // handle "generic" methods (that is function overloading!), duplicating with arguments from the target method of the type
2893 {
2894 for (auto &ct: ce->children())
2895 {
2896 if (!ct->section.isFunction())
2897 continue;
2898
2899 if (ct->type!="generic")
2900 continue;
2901
2902 // found a generic method (already duplicated for each entry by the parser)
2903 DString methodName = ct->args;
2904 if (methodMap.find(methodName.str()) != methodMap.end())
2905 {
2906 std::shared_ptr<Entry> method = methodMap[methodName.str()];
2907 ct->args = method->args;
2908 ct->argList = ArgumentList(method->argList);
2909 if (ct->brief.empty())
2910 {
2911 ct->brief = method->brief;
2912 ct->briefLine = method->briefLine;
2913 ct->briefFile = method->briefFile;
2914 }
2915 if (ct->doc.empty())
2916 {
2917 ct->doc = method->doc;
2918 ct->docLine = method->docLine;
2919 ct->docFile = method->docFile;
2920 }
2921 }
2922 }
2923 }
2924 }
2925}
2926
2927static int yyread(yyscan_t yyscanner,char *buf,int max_size)
2928{
2929 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2930 int c=0;
2931 while ( c < max_size && yyextra->inputString[yyextra->inputPosition] )
2932 {
2933 *buf = yyextra->inputString[yyextra->inputPosition++] ;
2934 c++; buf++;
2935 }
2936 return c;
2937}
2938
2939static void initParser(yyscan_t yyscanner)
2940{
2941 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2942 yyextra->last_entry.reset();
2943}
2944
2945static void initEntry(yyscan_t yyscanner)
2946{
2947 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2948 if (yyextra->typeMode)
2949 {
2950 yyextra->current->protection = yyextra->typeProtection;
2951 }
2952 else if (yyextra->current_root && yyextra->current_root->section.isClass() && yyextra->current_root->spec.isInterface())
2953 {
2954 yyextra->current->protection = Protection::Public;
2955 }
2956 else if (yyextra->current_root && yyextra->current_root->section.isFunction())
2957 {
2958 yyextra->current->protection = Protection::Private;
2959 }
2960 else
2961 {
2962 yyextra->current->protection = yyextra->defaultProtection;
2963 }
2964 yyextra->current->mtype = MethodTypes::Method;
2965 yyextra->current->virt = Specifier::Normal;
2966 yyextra->current->isStatic = false;
2967 yyextra->current->lang = SrcLangExt::Fortran;
2968 yyextra->commentScanner.initGroupInfo(yyextra->current.get());
2969}
2970
2971/**
2972 adds yyextra->current entry to yyextra->current_root and creates new yyextra->current
2973*/
2974static void addCurrentEntry(yyscan_t yyscanner,bool case_insens)
2975{
2976 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2977 if (case_insens) yyextra->current->name = yyextra->current->name.lower();
2978 //printf("===Adding entry %s to %s\n", qPrint(yyextra->current->name), qPrint(yyextra->current_root->name));
2979 yyextra->last_entry = yyextra->current;
2980 yyextra->current_root->moveToSubEntryAndRefresh(yyextra->current);
2981 initEntry(yyscanner);
2982}
2983
2984static void addModule(yyscan_t yyscanner,const DString &name, bool isModule)
2985{
2986 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
2987 DBG_CTX((stderr, "0=========> got module %s\n", qPrint(name)));
2988
2989 if (isModule)
2990 yyextra->current->section = EntryType::makeNamespace();
2991 else
2992 yyextra->current->section = EntryType::makeFunction();
2993
2994 if (!name.empty())
2995 {
2996 yyextra->current->name = name;
2997 }
2998 else
2999 {
3000 DString fname = yyextra->fileName;
3001 size_t index1 = fname.rfind('/');
3002 size_t index2 = fname.rfind('\\');
3003 size_t index = index1!=DString::npos && index2!=DString::npos ? std::max(index1,index2) :
3004 index1!=DString::npos ? index1 : index2;
3005 if (index!=DString::npos) fname = fname.mid(index+1);
3006 if (yyextra->mainPrograms) fname += "__" + DString().setNum(yyextra->mainPrograms);
3007 yyextra->mainPrograms++;
3008 fname = fname.prepend("__").append("__");
3009 yyextra->current->name = substitute(fname, ".", "_");
3010 }
3011 yyextra->current->type = "program";
3012 yyextra->current->fileName = yyextra->fileName;
3013 yyextra->current->bodyLine = yyextra->lineNr; // used for source reference
3014 yyextra->current->startLine = yyextra->lineNr;
3015 yyextra->current->protection = Protection::Public ;
3016 addCurrentEntry(yyscanner,true);
3017 startScope(yyscanner,yyextra->last_entry.get());
3018}
3019
3020
3021static void addSubprogram(yyscan_t yyscanner,const DString &text)
3022{
3023 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3024 DBG_CTX((stderr,"1=========> got subprog, type: %s\n",qPrint(text)));
3025 yyextra->subrCurrent.push_back(yyextra->current);
3026 yyextra->current->section = EntryType::makeFunction();
3027 DString subtype = text; subtype=subtype.lower().stripWhiteSpace();
3028 yyextra->functionLine = subtype.find("function")!=DString::npos;
3029 yyextra->current->type += " " + subtype;
3030 yyextra->current->type = yyextra->current->type.stripWhiteSpace();
3031 if (yyextra->ifType == IF_ABSTRACT)
3032 {
3033 yyextra->current->virt = Specifier::Virtual;
3034 }
3035 yyextra->current->fileName = yyextra->fileName;
3036 yyextra->current->bodyLine = yyextra->lineNr; // used for source reference start of body of routine
3037 yyextra->current->startLine = yyextra->lineNr; // used for source reference start of definition
3038 yyextra->current->args.clear();
3039 yyextra->current->argList.clear();
3040 pushBlockState(yyscanner,text);
3041 yyextra->docBlock.clear();
3042}
3043
3044/*! Adds interface to the root entry.
3045 * \note Code was brought to this procedure from the parser,
3046 * because there was/is idea to use it in several parts of the parser.
3047 */
3048static void addInterface(yyscan_t yyscanner,DString name, InterfaceType type)
3049{
3050 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3051 if (YY_START == Start)
3052 {
3053 addModule(yyscanner);
3054 pushBlockState(yyscanner,DString(yytext)+" (anonymous program)");
3055 yy_push_state(ModuleBody,yyscanner); //anon program
3056 }
3057
3058 yyextra->current->section = EntryType::makeClass(); // was EntryType::Interface;
3059 yyextra->current->spec = TypeSpecifier().setInterface(true);
3060 yyextra->current->name = name;
3061
3062 switch (type)
3063 {
3064 case IF_ABSTRACT:
3065 yyextra->current->type = "abstract";
3066 break;
3067
3068 case IF_GENERIC:
3069 yyextra->current->type = "generic";
3070 break;
3071
3072 case IF_SPECIFIC:
3073 case IF_NONE:
3074 default:
3075 yyextra->current->type = "";
3076 }
3077
3078 /* if type is part of a module, mod name is necessary for output */
3079 if ((yyextra->current_root) &&
3080 (yyextra->current_root->section.isClass() ||
3081 yyextra->current_root->section.isNamespace()))
3082 {
3083 yyextra->current->name= yyextra->current_root->name + "::" + yyextra->current->name;
3084 }
3085
3086 yyextra->current->fileName = yyextra->fileName;
3087 yyextra->current->bodyLine = yyextra->lineNr;
3088 yyextra->current->startLine = yyextra->lineNr;
3089 addCurrentEntry(yyscanner,true);
3090}
3091
3092
3093//-----------------------------------------------------------------------------
3094
3095/*! Get the argument \a name.
3096 */
3097static Argument *getParameter(yyscan_t yyscanner,const DString &name)
3098{
3099 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3100 // std::cout<<"addFortranParameter(): "<<name<<" DOCS:"<<(docs.empty()?DString("null"):docs)<<"\n";
3101 Argument *ret = nullptr;
3102 for (Argument &a:yyextra->current_root->argList)
3103 {
3104 if (a.name.lower()==name.lower())
3105 {
3106 ret=&a;
3107 //printf("parameter found: %s\n",(const char*)name);
3108 break;
3109 }
3110 } // for
3111 return ret;
3112}
3113
3114 //----------------------------------------------------------------------------
3115static void startCommentBlock(yyscan_t yyscanner,bool brief)
3116{
3117 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3118 if (brief)
3119 {
3120 yyextra->current->briefFile = yyextra->fileName;
3121 yyextra->current->briefLine = yyextra->lineNr;
3122 }
3123 else
3124 {
3125 yyextra->current->docFile = yyextra->fileName;
3126 yyextra->current->docLine = yyextra->lineNr;
3127 }
3128}
3129
3130//----------------------------------------------------------------------------
3131
3132static void handleCommentBlock(yyscan_t yyscanner,const DString &doc,bool brief)
3133{
3134 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3135 //printf("handleCommentBlock(doc=[%s] brief=%d yyextra->docBlockInBody=%d yyextra->docBlockJavaStyle=%d\n",
3136 // qPrint(doc),brief,yyextra->docBlockInBody,yyextra->docBlockJavaStyle);
3137
3138 bool hideInBodyDocs = Config_getBool(HIDE_IN_BODY_DOCS);
3139 if (yyextra->docBlockInBody && hideInBodyDocs)
3140 {
3141 yyextra->docBlockInBody = false;
3142 return;
3143 }
3144 DBG_CTX((stderr,"call parseCommentBlock [%s]\n",qPrint(doc)));
3145 int lineNr = brief ? yyextra->current->briefLine : yyextra->current->docLine;
3146 int position=0;
3147 bool needsEntry = false;
3148 Markdown markdown(yyextra->fileName,lineNr);
3149 GuardedSectionStack guards;
3150 DString strippedDoc = stripIndentation(doc);
3151 DString processedDoc = Config_getBool(MARKDOWN_SUPPORT) ? markdown.process(strippedDoc,lineNr) : strippedDoc;
3152 while (yyextra->commentScanner.parseCommentBlock(
3153 yyextra->thisParser,
3154 yyextra->docBlockInBody ? yyextra->subrCurrent.back().get() : yyextra->current.get(),
3155 processedDoc, // text
3156 yyextra->fileName, // file
3157 lineNr,
3158 yyextra->docBlockInBody ? false : brief,
3159 yyextra->docBlockInBody ? false : yyextra->docBlockJavaStyle,
3160 yyextra->docBlockInBody,
3161 yyextra->defaultProtection,
3162 position,
3163 needsEntry,
3164 Config_getBool(MARKDOWN_SUPPORT),
3165 &guards
3166 ))
3167 {
3168 DBG_CTX((stderr,"parseCommentBlock position=%d [%s] needsEntry=%d\n",position,doc.data()+position,needsEntry));
3169 if (needsEntry) addCurrentEntry(yyscanner,false);
3170 }
3171 DBG_CTX((stderr,"parseCommentBlock position=%d [%s] needsEntry=%d\n",position,doc.data()+position,needsEntry));
3172
3173 if (needsEntry) addCurrentEntry(yyscanner,false);
3174 yyextra->docBlockInBody = false;
3175}
3176
3177//----------------------------------------------------------------------------
3178/// Handle parameter description as defined after the declaration of the parameter
3179static void subrHandleCommentBlock(yyscan_t yyscanner,const DString &doc,bool brief)
3180{
3181 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3182 DString loc_doc;
3183 loc_doc = doc.stripWhiteSpace();
3184
3185 std::shared_ptr<Entry> tmp_entry = yyextra->current;
3186 yyextra->current = yyextra->subrCurrent.back(); // temporarily switch to the entry of the subroutine / function
3187
3188 // Still in the specification section so no inbodyDocs yet, but parameter documentation
3189 yyextra->current->inbodyDocs = "";
3190
3191 // strip \\param or @param, so we can do some extra checking. We will add it later on again.
3192 if (loc_doc.stripPrefix("\\param") ||
3193 loc_doc.stripPrefix("@param")
3194 ) loc_doc = loc_doc.stripWhiteSpace();
3195
3196 // direction as defined with the declaration of the parameter
3197 int dir1 = yyextra->modifiers[yyextra->current_root][yyextra->argName.lower().str()].direction;
3198 // in description [in] is specified
3199 if (loc_doc.lower().find(directionParam[SymbolModifiers::IN]) == 0)
3200 {
3201 // check if with the declaration intent(in) or nothing has been specified
3204 {
3205 // strip direction
3206 loc_doc = loc_doc.mid(strlen(directionParam[SymbolModifiers::IN]));
3207 loc_doc.stripWhiteSpace();
3208 // in case of empty documentation or (now) just name, consider it as no documentation
3209 if (!loc_doc.empty() && (loc_doc.lower() != yyextra->argName.lower()))
3210 {
3211 handleCommentBlock(yyscanner,DString("\n\n@param ") + directionParam[SymbolModifiers::IN] + " " +
3212 yyextra->argName + " " + loc_doc,brief);
3213 }
3214 }
3215 else
3216 {
3217 // something different specified, give warning and leave error.
3218 warn(yyextra->fileName,yyextra->lineNr, "Routine: {}{} inconsistency between intent attribute and documentation for parameter {}:",
3219 yyextra->current->name,yyextra->current->args,yyextra->argName);
3220 handleCommentBlock(yyscanner,DString("\n\n@param ") + directionParam[dir1] + " " +
3221 yyextra->argName + " " + loc_doc,brief);
3222 }
3223 }
3224 // analogous to the [in] case, here [out] direction specified
3225 else if (loc_doc.lower().find(directionParam[SymbolModifiers::OUT]) == 0)
3226 {
3229 {
3230 loc_doc = loc_doc.mid(strlen(directionParam[SymbolModifiers::OUT]));
3231 loc_doc.stripWhiteSpace();
3232 if (loc_doc.empty() || (loc_doc.lower() == yyextra->argName.lower()))
3233 {
3234 yyextra->current = tmp_entry;
3235 return;
3236 }
3237 handleCommentBlock(yyscanner,DString("\n\n@param ") + directionParam[SymbolModifiers::OUT] + " " +
3238 yyextra->argName + " " + loc_doc,brief);
3239 }
3240 else
3241 {
3242 warn(yyextra->fileName,yyextra->lineNr, "Routine: {}{} inconsistency between intent attribute and documentation for parameter {}:",
3243 yyextra->current->name,yyextra->current->args,yyextra->argName);
3244 handleCommentBlock(yyscanner,DString("\n\n@param ") + directionParam[dir1] + " " +
3245 yyextra->argName + " " + loc_doc,brief);
3246 }
3247 }
3248 // analogous to the [in] case, here [in,out] direction specified
3249 else if (loc_doc.lower().find(directionParam[SymbolModifiers::INOUT]) == 0)
3250 {
3253 {
3254 loc_doc = loc_doc.mid(strlen(directionParam[SymbolModifiers::INOUT]));
3255 loc_doc.stripWhiteSpace();
3256 if (!loc_doc.empty() && (loc_doc.lower() != yyextra->argName.lower()))
3257 {
3258 handleCommentBlock(yyscanner,DString("\n\n@param ") + directionParam[SymbolModifiers::INOUT] + " " +
3259 yyextra->argName + " " + loc_doc,brief);
3260 }
3261 }
3262 else
3263 {
3264 warn(yyextra->fileName,yyextra->lineNr, "Routine: {}{} inconsistency between intent attribute and documentation for parameter {}:",
3265 yyextra->current->name,yyextra->current->args,yyextra->argName);
3266 handleCommentBlock(yyscanner,DString("\n\n@param ") + directionParam[dir1] + " " +
3267 yyextra->argName + " " + loc_doc,brief);
3268 }
3269 }
3270 // analogous to the [in] case; here no direction specified
3271 else if (!loc_doc.empty() && (loc_doc.lower() != yyextra->argName.lower()))
3272 {
3273 handleCommentBlock(yyscanner,DString("\n\n@param ") + directionParam[dir1] + " " +
3274 yyextra->argName + " " + loc_doc,brief);
3275 }
3276
3277 // reset yyextra->current back to the part inside the routine
3278 yyextra->current = tmp_entry;
3279}
3280//----------------------------------------------------------------------------
3281/// Handle result description as defined after the declaration of the parameter
3282static void subrHandleCommentBlockResult(yyscan_t yyscanner,const DString &doc,bool brief)
3283{
3284 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3285 DString loc_doc;
3286 loc_doc = doc.stripWhiteSpace();
3287
3288 std::shared_ptr<Entry> tmp_entry = yyextra->current;
3289 yyextra->current = yyextra->subrCurrent.back(); // temporarily switch to the entry of the subroutine / function
3290
3291 // Still in the specification section so no inbodyDocs yet, but parameter documentation
3292 yyextra->current->inbodyDocs = "";
3293
3294 // strip \\returns or @returns. We will add it later on again.
3295 if (loc_doc.stripPrefix("\\returns") ||
3296 loc_doc.stripPrefix("\\return") ||
3297 loc_doc.stripPrefix("@returns") ||
3298 loc_doc.stripPrefix("@return")
3299 ) loc_doc = loc_doc.stripWhiteSpace();
3300
3301 if (!loc_doc.empty() && (loc_doc.lower() != yyextra->argName.lower()))
3302 {
3303 handleCommentBlock(yyscanner,DString("\n\n@returns ") + loc_doc,brief);
3304 }
3305
3306 // reset yyextra->current back to the part inside the routine
3307 yyextra->current = std::move(tmp_entry);
3308}
3309
3310//----------------------------------------------------------------------------
3311
3312static void parseMain(yyscan_t yyscanner, const DString &fileName,const char *fileBuf,
3313 const std::shared_ptr<Entry> &rt, FortranFormat format)
3314{
3315 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3316 char *tmpBuf = nullptr;
3317 initParser(yyscanner);
3318
3319 if (fileBuf==nullptr || fileBuf[0]=='\0') return;
3320
3321 yyextra->defaultProtection = Protection::Public;
3322 yyextra->inputString = fileBuf;
3323 yyextra->inputPosition = 0;
3324 yyextra->inputStringPrepass = nullptr;
3325 yyextra->inputPositionPrepass = 0;
3326
3327 //yyextra->anonCount = 0; // don't reset per file
3328 yyextra->current_root = rt.get();
3329 yyextra->global_root = rt;
3330
3331 yyextra->isFixedForm = recognizeFixedForm(fileBuf,format);
3332
3333 if (yyextra->isFixedForm)
3334 {
3335 yyextra->fixedCommentAfter = Config_getInt(FORTRAN_COMMENT_AFTER);
3336 msg("Prepassing fixed form of {}\n", fileName);
3337 //printf("---strlen=%d\n", strlen(fileBuf));
3338 //clock_t start=clock();
3339
3340 //printf("Input fixed form string:\n%s\n", fileBuf);
3341 //printf("===========================\n");
3342 yyextra->inputString = prepassFixedForm(fileBuf, nullptr,yyextra->fixedCommentAfter);
3343 Debug::print(Debug::FortranFixed2Free,0,"======== Fixed to Free format =========\n---- Input fixed form string ------- \n{}\n", fileBuf);
3344 Debug::print(Debug::FortranFixed2Free,0,"---- Resulting free form string ------- \n{}\n", yyextra->inputString);
3345 //printf("Resulting free form string:\n%s\n", yyextra->inputString);
3346 //printf("===========================\n");
3347
3348 //clock_t end=clock();
3349 //printf("CPU time used=%f\n", ((double) (end-start))/CLOCKS_PER_SEC);
3350 }
3351 else if (yyextra->inputString[strlen(fileBuf)-1] != '\n')
3352 {
3353 tmpBuf = (char *)malloc(strlen(fileBuf)+2);
3354 strcpy(tmpBuf,fileBuf);
3355 tmpBuf[strlen(fileBuf)]= '\n';
3356 tmpBuf[strlen(fileBuf)+1]= '\000';
3357 yyextra->inputString = tmpBuf;
3358 }
3359
3360 yyextra->lineNr= 1 ;
3361 yyextra->fileName = fileName;
3362 msg("Parsing file {}...\n",yyextra->fileName);
3363
3364 yyextra->global_scope = rt.get();
3365 startScope(yyscanner,rt.get()); // implies yyextra->current_root = rt
3366 initParser(yyscanner);
3367 yyextra->commentScanner.enterFile(yyextra->fileName,yyextra->lineNr);
3368
3369 // add entry for the file
3370 yyextra->current = std::make_shared<Entry>();
3371 yyextra->current->lang = SrcLangExt::Fortran;
3372 yyextra->current->name = yyextra->fileName;
3373 yyextra->current->section = EntryType::makeSource();
3374 yyextra->file_root = yyextra->current;
3375 yyextra->current_root->moveToSubEntryAndRefresh(yyextra->current);
3376 yyextra->current->lang = SrcLangExt::Fortran;
3377
3378 fortranscannerYYrestart( nullptr, yyscanner );
3379 {
3380 BEGIN( Start );
3381 }
3382
3383 fortranscannerYYlex(yyscanner);
3384 yyextra->commentScanner.leaveFile(yyextra->fileName,yyextra->lineNr);
3385
3386 if (yyextra->global_scope && yyextra->global_scope != INVALID_ENTRY)
3387 {
3388 endScope(yyscanner,yyextra->current_root, true); // true - global root
3389 }
3390
3391 //debugCompounds(rt); //debug
3392
3393 rt->program.str(std::string());
3394 //delete yyextra->current; yyextra->current=0;
3395 yyextra->moduleProcedures.clear();
3396 if (tmpBuf)
3397 {
3398 free((char*)tmpBuf);
3399 yyextra->inputString=nullptr;
3400 }
3401 if (yyextra->isFixedForm)
3402 {
3403 free((char*)yyextra->inputString);
3404 yyextra->inputString=nullptr;
3405 }
3406
3407}
3408
3409//----------------------------------------------------------------------------
3410
3412{
3417 {
3418 fortranscannerYYlex_init_extra(&extra,&yyscanner);
3419#ifdef FLEX_DEBUG
3420 fortranscannerYYset_debug(Debug::isFlagSet(Debug::Lex_fortranscanner) ? 1 : 0,yyscanner);
3421#endif
3422 }
3424 {
3425 fortranscannerYYlex_destroy(yyscanner);
3426 }
3427};
3428
3430 : p(std::make_unique<Private>(format))
3431{
3432}
3433
3435
3437 const char *fileBuf,
3438 const std::shared_ptr<Entry> &root,
3439 ClangTUParser * /*clangParser*/)
3440{
3441 struct yyguts_t *yyg = (struct yyguts_t*)p->yyscanner;
3442 yyextra->thisParser = this;
3443
3444 DebugLex debugLex(Debug::Lex_fortranscanner, __FILE__, qPrint(fileName));
3445
3446 ::parseMain(p->yyscanner,fileName,fileBuf,root,p->format);
3447}
3448
3450{
3451 return extension!=extension.lower(); // use preprocessor only for upper case extensions
3452}
3453
3455{
3456 struct yyguts_t *yyg = (struct yyguts_t*)p->yyscanner;
3457 pushBuffer(p->yyscanner,text);
3458 yyextra->parsingPrototype = true;
3459 BEGIN(Prototype);
3460 fortranscannerYYlex(p->yyscanner);
3461 yyextra->parsingPrototype = false;
3462 popBuffer(p->yyscanner);
3463}
3464
3465//----------------------------------------------------------------------------
3466
3467static void scanner_abort(yyscan_t yyscanner)
3468{
3469 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3470 fprintf(stderr,"********************************************************************\n");
3471 if (yyextra->blockLineNr == -1)
3472 {
3473 fprintf(stderr,"Error in file %s line: %d, state: %d(%s)\n",
3474 qPrint(yyextra->fileName),yyextra->lineNr,YY_START,stateToString(YY_START));
3475 }
3476 else
3477 {
3478 fprintf(stderr,"Error in file %s line: %d, state: %d(%s), starting command: '%s' probable line reference: %d\n",
3479 qPrint(yyextra->fileName),yyextra->lineNr,YY_START,stateToString(YY_START),qPrint(yyextra->blockString),yyextra->blockLineNr);
3480 }
3481 fprintf(stderr,"********************************************************************\n");
3482
3483 // empty the stack
3484 while (!yyextra->blockStack.empty()) yyextra->blockStack.pop();
3485
3486 bool start=false;
3487
3488 for (const auto &ce : yyextra->global_root->children())
3489 {
3490 if (ce == yyextra->file_root) start=true;
3491 if (start) ce->reset();
3492 }
3493
3494 // dummy call to avoid compiler warning
3495 (void)yy_top_state(yyscanner);
3496
3497 return;
3498 //exit(-1);
3499}
3500
3501static inline void pop_state(yyscan_t yyscanner)
3502{
3503 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3504 if ( yyg->yy_start_stack_ptr <= 0 )
3505 warn(yyextra->fileName,yyextra->lineNr,"Unexpected statement '{}'",yytext );
3506 else
3507 yy_pop_state(yyscanner);
3508}
3509
3510static void pushBlockState(yyscan_t yyscanner,const DString &text)
3511{
3512 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3513 yyextra->blockString = text;
3514 yyextra->blockLineNr = yyextra->lineNr;
3515 yyextra->blockStack.emplace(yyextra->blockString,yyextra->blockLineNr);
3516}
3517
3518static void popBlockState(yyscan_t yyscanner)
3519{
3520 struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
3521 if (yyextra->blockStack.empty())
3522 {
3523 warn(yyextra->fileName,yyextra->lineNr,"Internal inconsistency: empty stack while attempting popBlockState");
3524 }
3525 else
3526 {
3527 yyextra->blockStack.pop();
3528 }
3529 if (yyextra->blockStack.empty())
3530 {
3531 yyextra->blockString.clear();
3532 yyextra->blockLineNr=-1;
3533 }
3534 else
3535 {
3536 yyextra->blockString=yyextra->blockStack.top().blockString;
3537 yyextra->blockLineNr=yyextra->blockStack.top().blockLineNr;
3538 }
3539}
3540//----------------------------------------------------------------------------
3541
3542#include "fortranscanner.l.h"
DString docs
Definition arguments.h:48
This class represents an function or template argument list.
Definition arguments.h:66
Clang parser object for a single translation unit, which consists of a source file and the directly o...
Definition clangparser.h:25
void clear()
Definition dstring.h:214
DString & setNum(short n)
Definition dstring.h:552
size_t rfind(char c, size_t pos=npos) const
Definition dstring.h:244
bool empty() const
Returns true iff the string is empty (std::string compatible alias for isEmpty()).
Definition dstring.h:148
char & at(size_t i)
Returns a reference to the character at index i.
Definition dstring.h:686
DString & append(char c)
Definition dstring.h:489
DString & prepend(const char *s)
Definition dstring.h:515
int contains(char c, bool cs=true) const
Definition dstring.cpp:85
char & back()
Returns a reference to the last character.
Definition dstring.h:199
const std::string & str() const
Definition dstring.h:645
bool stripPrefix(const DString &prefix)
Definition dstring.h:290
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
bool startsWith(const char *s) const
Definition dstring.h:600
@ Lex_fortranscanner
Definition debug.h:63
@ FortranFixed2Free
Definition debug.h:41
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:78
static ParserManager * parserManager
Definition doxygen.h:122
const std::vector< std::shared_ptr< Entry > > & children() const
Definition entry.h:138
Entry * parent() const
Definition entry.h:133
ArgumentList argList
member arguments as a list
Definition entry.h:194
DString type
member type
Definition entry.h:172
EntryType section
entry type (see Sections);
Definition entry.h:171
Specifier virt
virtualness of the entry
Definition entry.h:190
DString name
member name
Definition entry.h:173
Protection protection
class protection
Definition entry.h:179
bool isStatic
static ?
Definition entry.h:184
TypeSpecifier spec
class/member specifiers
Definition entry.h:181
void removeSubEntry(const Entry *e)
Definition entry.cpp:172
bool needsPreprocessing(const DString &extension) const override
Returns true if the language identified by extension needs the C preprocessor to be run before feed t...
void parseInput(const DString &fileName, const char *fileBuf, const std::shared_ptr< Entry > &root, ClangTUParser *clangParser) override
Parses a single input file with the goal to build an Entry tree.
std::unique_ptr< Private > p
~FortranOutlineParser() override
FortranOutlineParser(FortranFormat format=FortranFormat::Unknown)
void parsePrototype(const DString &text) override
Callback function called by the comment block scanner.
Helper class to process markdown formatted text.
Definition markdown.h:33
DString process(const DString &input, int &startNewlines, bool fromParseInput=false)
DString getParserName(const DString &extension)
Gets the name of the parser associated with given extension.
Definition parserintf.h:268
Wrapper class for a number of boolean properties.
Definition types.h:696
std::stack< GuardedSection > GuardedSectionStack
Definition commentscan.h:48
#define Config_getInt(name)
Definition config.h:34
DirIterator end(const DirIterator &) noexcept
Definition dir.cpp:180
const char * prepassFixedForm(const char *contents, int *hasContLine, int fixedCommentAfter)
bool recognizeFixedForm(const DString &contents, FortranFormat format)
FortranFormat convertFileNameFortranParserCode(const DString &fn)
static DString applyModifiers(DString typeName, const SymbolModifiers &mdfs)
#define DBG_CTX(x)
const char * prepassFixedForm(const char *contents, int *hasContLine, int fixedCommentAfter)
static void initParser(yyscan_t yyscanner)
bool recognizeFixedForm(const DString &contents, FortranFormat format)
static void insertCharacter(char *contents, int length, int pos, char c)
static Argument * findArgument(Entry *subprog, DString name, bool byTypeName=false)
static bool keyWordsFortranC(const char *contents)
static void parseMain(yyscan_t yyscanner, const DString &fileName, const char *fileBuf, const std::shared_ptr< Entry > &rt, FortranFormat format)
#define msg(fmt,...)
Definition message.h:94
Definition message.h:146
Definition dstring.h:913
fortranscannerYY_state extra
FortranFormat
Definition types.h:614
DString getFileNameExtension(const DString &fn)
Definition util.cpp:4219