cmFortranParser.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #ifndef cmFortranParser_h
  4. #define cmFortranParser_h
  5. #if !defined(cmFortranLexer_cxx) && !defined(cmFortranParser_cxx)
  6. # include "cmConfigure.h" // IWYU pragma: keep
  7. # include <set>
  8. # include <string>
  9. # include <utility>
  10. # include <vector>
  11. #endif
  12. #include <stddef.h> /* size_t */
  13. /* Forward declare parser object type. */
  14. typedef struct cmFortranParser_s cmFortranParser;
  15. /* Functions to enter/exit #include'd files in order. */
  16. bool cmFortranParser_FilePush(cmFortranParser* parser, const char* fname);
  17. bool cmFortranParser_FilePop(cmFortranParser* parser);
  18. /* Callbacks for lexer. */
  19. int cmFortranParser_Input(cmFortranParser* parser, char* buffer,
  20. size_t bufferSize);
  21. void cmFortranParser_StringStart(cmFortranParser* parser);
  22. const char* cmFortranParser_StringEnd(cmFortranParser* parser);
  23. void cmFortranParser_StringAppend(cmFortranParser* parser, char c);
  24. void cmFortranParser_SetInInterface(cmFortranParser* parser, bool is_in);
  25. bool cmFortranParser_GetInInterface(cmFortranParser* parser);
  26. void cmFortranParser_SetInPPFalseBranch(cmFortranParser* parser, bool is_in);
  27. bool cmFortranParser_GetInPPFalseBranch(cmFortranParser* parser);
  28. void cmFortranParser_SetOldStartcond(cmFortranParser* parser, int arg);
  29. int cmFortranParser_GetOldStartcond(cmFortranParser* parser);
  30. /* Callbacks for parser. */
  31. void cmFortranParser_Error(cmFortranParser* parser, const char* message);
  32. void cmFortranParser_RuleUse(cmFortranParser* parser, const char* module_name);
  33. void cmFortranParser_RuleLineDirective(cmFortranParser* parser,
  34. const char* filename);
  35. void cmFortranParser_RuleInclude(cmFortranParser* parser, const char* name);
  36. void cmFortranParser_RuleModule(cmFortranParser* parser,
  37. const char* module_name);
  38. void cmFortranParser_RuleSubmodule(cmFortranParser* parser,
  39. const char* module_name,
  40. const char* submodule_name);
  41. void cmFortranParser_RuleSubmoduleNested(cmFortranParser* parser,
  42. const char* module_name,
  43. const char* submodule_name,
  44. const char* nested_submodule_name);
  45. void cmFortranParser_RuleDefine(cmFortranParser* parser, const char* name);
  46. void cmFortranParser_RuleUndef(cmFortranParser* parser, const char* name);
  47. void cmFortranParser_RuleIfdef(cmFortranParser* parser, const char* name);
  48. void cmFortranParser_RuleIfndef(cmFortranParser* parser, const char* name);
  49. void cmFortranParser_RuleIf(cmFortranParser* parser);
  50. void cmFortranParser_RuleElif(cmFortranParser* parser);
  51. void cmFortranParser_RuleElse(cmFortranParser* parser);
  52. void cmFortranParser_RuleEndif(cmFortranParser* parser);
  53. /* Define the parser stack element type. */
  54. struct cmFortran_yystype
  55. {
  56. char* string;
  57. };
  58. /* Setup the proper yylex interface. */
  59. #define YY_EXTRA_TYPE cmFortranParser*
  60. #define YY_DECL int cmFortran_yylex(YYSTYPE* yylvalp, yyscan_t yyscanner)
  61. #define YYSTYPE cmFortran_yystype
  62. #define YYSTYPE_IS_DECLARED 1
  63. #if !defined(cmFortranLexer_cxx)
  64. # define YY_NO_UNISTD_H
  65. # include "cmFortranLexer.h"
  66. #endif
  67. #if !defined(cmFortranLexer_cxx)
  68. # if !defined(cmFortranParser_cxx)
  69. # undef YY_EXTRA_TYPE
  70. # undef YY_DECL
  71. # undef YYSTYPE
  72. # undef YYSTYPE_IS_DECLARED
  73. # endif
  74. #endif
  75. #if !defined(cmFortranLexer_cxx) && !defined(cmFortranParser_cxx)
  76. # include <stack>
  77. // Information about a single source file.
  78. class cmFortranSourceInfo
  79. {
  80. public:
  81. // The name of the source file.
  82. std::string Source;
  83. // Set of provided and required modules.
  84. std::set<std::string> Provides;
  85. std::set<std::string> Requires;
  86. // Set of files included in the translation unit.
  87. std::set<std::string> Includes;
  88. };
  89. // Parser methods not included in generated interface.
  90. // Get the current buffer processed by the lexer.
  91. YY_BUFFER_STATE cmFortranLexer_GetCurrentBuffer(yyscan_t yyscanner);
  92. // The parser entry point.
  93. int cmFortran_yyparse(yyscan_t);
  94. // Define parser object internal structure.
  95. struct cmFortranFile
  96. {
  97. cmFortranFile(FILE* file, YY_BUFFER_STATE buffer, std::string dir)
  98. : File(file)
  99. , Buffer(buffer)
  100. , Directory(std::move(dir))
  101. , LastCharWasNewline(false)
  102. {
  103. }
  104. FILE* File;
  105. YY_BUFFER_STATE Buffer;
  106. std::string Directory;
  107. bool LastCharWasNewline;
  108. };
  109. struct cmFortranCompiler
  110. {
  111. std::string Id;
  112. std::string SModSep;
  113. std::string SModExt;
  114. };
  115. struct cmFortranParser_s
  116. {
  117. cmFortranParser_s(cmFortranCompiler fc, std::vector<std::string> includes,
  118. std::set<std::string> defines, cmFortranSourceInfo& info);
  119. ~cmFortranParser_s();
  120. cmFortranParser_s(const cmFortranParser_s&) = delete;
  121. cmFortranParser_s& operator=(const cmFortranParser_s&) = delete;
  122. bool FindIncludeFile(const char* dir, const char* includeName,
  123. std::string& fileName);
  124. std::string ModName(std::string const& mod_name) const;
  125. std::string SModName(std::string const& mod_name,
  126. std::string const& sub_name) const;
  127. // What compiler.
  128. cmFortranCompiler Compiler;
  129. // The include file search path.
  130. std::vector<std::string> IncludePath;
  131. // Lexical scanner instance.
  132. yyscan_t Scanner;
  133. // Stack of open files in the translation unit.
  134. std::stack<cmFortranFile> FileStack;
  135. // Buffer for string literals.
  136. std::string TokenString;
  137. // Error message text if a parser error occurs.
  138. std::string Error;
  139. // Flag for whether lexer is reading from inside an interface.
  140. bool InInterface;
  141. int OldStartcond;
  142. std::set<std::string> PPDefinitions;
  143. size_t InPPFalseBranch;
  144. std::stack<bool> SkipToEnd;
  145. // Information about the parsed source.
  146. cmFortranSourceInfo& Info;
  147. };
  148. #endif
  149. #endif