cmFortranParser.h 5.69 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing for details.  */
3 4 5
#ifndef cmFortranParser_h
#define cmFortranParser_h

6
#if !defined(cmFortranLexer_cxx) && !defined(cmFortranParser_cxx)
7
#  include "cmConfigure.h" // IWYU pragma: keep
8

9 10
#  include <set>
#  include <string>
wahikihiki's avatar
wahikihiki committed
11
#  include <utility>
12
#  include <vector>
13 14
#endif

15 16 17 18 19 20
#include <stddef.h> /* size_t */

/* Forward declare parser object type.  */
typedef struct cmFortranParser_s cmFortranParser;

/* Functions to enter/exit #include'd files in order.  */
21
bool cmFortranParser_FilePush(cmFortranParser* parser, const char* fname);
22 23 24
bool cmFortranParser_FilePop(cmFortranParser* parser);

/* Callbacks for lexer.  */
25 26
int cmFortranParser_Input(cmFortranParser* parser, char* buffer,
                          size_t bufferSize);
27 28 29

void cmFortranParser_StringStart(cmFortranParser* parser);
const char* cmFortranParser_StringEnd(cmFortranParser* parser);
30
void cmFortranParser_StringAppend(cmFortranParser* parser, char c);
31

32
void cmFortranParser_SetInInterface(cmFortranParser* parser, bool is_in);
33 34
bool cmFortranParser_GetInInterface(cmFortranParser* parser);

35
void cmFortranParser_SetInPPFalseBranch(cmFortranParser* parser, bool is_in);
36 37
bool cmFortranParser_GetInPPFalseBranch(cmFortranParser* parser);

38
void cmFortranParser_SetOldStartcond(cmFortranParser* parser, int arg);
39 40 41
int cmFortranParser_GetOldStartcond(cmFortranParser* parser);

/* Callbacks for parser.  */
42
void cmFortranParser_Error(cmFortranParser* parser, const char* message);
43
void cmFortranParser_RuleUse(cmFortranParser* parser, const char* module_name);
44 45
void cmFortranParser_RuleLineDirective(cmFortranParser* parser,
                                       const char* filename);
46
void cmFortranParser_RuleInclude(cmFortranParser* parser, const char* name);
47 48
void cmFortranParser_RuleModule(cmFortranParser* parser,
                                const char* module_name);
49 50 51 52 53 54 55
void cmFortranParser_RuleSubmodule(cmFortranParser* parser,
                                   const char* module_name,
                                   const char* submodule_name);
void cmFortranParser_RuleSubmoduleNested(cmFortranParser* parser,
                                         const char* module_name,
                                         const char* submodule_name,
                                         const char* nested_submodule_name);
56 57 58 59
void cmFortranParser_RuleDefine(cmFortranParser* parser, const char* name);
void cmFortranParser_RuleUndef(cmFortranParser* parser, const char* name);
void cmFortranParser_RuleIfdef(cmFortranParser* parser, const char* name);
void cmFortranParser_RuleIfndef(cmFortranParser* parser, const char* name);
60 61 62 63 64 65
void cmFortranParser_RuleIf(cmFortranParser* parser);
void cmFortranParser_RuleElif(cmFortranParser* parser);
void cmFortranParser_RuleElse(cmFortranParser* parser);
void cmFortranParser_RuleEndif(cmFortranParser* parser);

/* Define the parser stack element type.  */
66
struct cmFortran_yystype
67 68 69 70 71 72
{
  char* string;
};

/* Setup the proper yylex interface.  */
#define YY_EXTRA_TYPE cmFortranParser*
73
#define YY_DECL int cmFortran_yylex(YYSTYPE* yylvalp, yyscan_t yyscanner)
74 75 76
#define YYSTYPE cmFortran_yystype
#define YYSTYPE_IS_DECLARED 1
#if !defined(cmFortranLexer_cxx)
77 78
#  define YY_NO_UNISTD_H
#  include "cmFortranLexer.h"
79 80
#endif
#if !defined(cmFortranLexer_cxx)
81 82 83 84 85 86
#  if !defined(cmFortranParser_cxx)
#    undef YY_EXTRA_TYPE
#    undef YY_DECL
#    undef YYSTYPE
#    undef YYSTYPE_IS_DECLARED
#  endif
87 88
#endif

89
#if !defined(cmFortranLexer_cxx) && !defined(cmFortranParser_cxx)
90
#  include <stack>
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

// Information about a single source file.
class cmFortranSourceInfo
{
public:
  // The name of the source file.
  std::string Source;

  // Set of provided and required modules.
  std::set<std::string> Provides;
  std::set<std::string> Requires;

  // Set of files included in the translation unit.
  std::set<std::string> Includes;
};

// Parser methods not included in generated interface.

// Get the current buffer processed by the lexer.
YY_BUFFER_STATE cmFortranLexer_GetCurrentBuffer(yyscan_t yyscanner);

// The parser entry point.
int cmFortran_yyparse(yyscan_t);

// Define parser object internal structure.
struct cmFortranFile
{
wahikihiki's avatar
wahikihiki committed
118
  cmFortranFile(FILE* file, YY_BUFFER_STATE buffer, std::string dir)
119 120
    : File(file)
    , Buffer(buffer)
wahikihiki's avatar
wahikihiki committed
121
    , Directory(std::move(dir))
122
    , LastCharWasNewline(false)
123 124
  {
  }
125 126 127
  FILE* File;
  YY_BUFFER_STATE Buffer;
  std::string Directory;
128
  bool LastCharWasNewline;
129 130
};

131 132 133
struct cmFortranCompiler
{
  std::string Id;
134 135
  std::string SModSep;
  std::string SModExt;
136 137
};

138 139
struct cmFortranParser_s
{
140
  cmFortranParser_s(cmFortranCompiler fc, std::vector<std::string> includes,
wahikihiki's avatar
wahikihiki committed
141
                    std::set<std::string> defines, cmFortranSourceInfo& info);
142 143 144 145 146
  ~cmFortranParser_s();

  bool FindIncludeFile(const char* dir, const char* includeName,
                       std::string& fileName);

147 148 149 150
  std::string ModName(std::string const& mod_name) const;
  std::string SModName(std::string const& mod_name,
                       std::string const& sub_name) const;

151 152 153
  // What compiler.
  cmFortranCompiler Compiler;

154 155 156 157 158 159 160 161 162 163 164 165
  // The include file search path.
  std::vector<std::string> IncludePath;

  // Lexical scanner instance.
  yyscan_t Scanner;

  // Stack of open files in the translation unit.
  std::stack<cmFortranFile> FileStack;

  // Buffer for string literals.
  std::string TokenString;

166 167 168
  // Error message text if a parser error occurs.
  std::string Error;

169 170 171 172 173 174 175 176 177 178 179 180 181
  // Flag for whether lexer is reading from inside an interface.
  bool InInterface;

  int OldStartcond;
  std::set<std::string> PPDefinitions;
  size_t InPPFalseBranch;
  std::stack<bool> SkipToEnd;

  // Information about the parsed source.
  cmFortranSourceInfo& Info;
};
#endif

182
#endif