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