123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359 |
- #include "cmFortranParser.h"
- #include "cmSystemTools.h"
- #include <assert.h>
- #include <set>
- #include <stack>
- #include <stdio.h>
- #include <string>
- #include <vector>
- bool cmFortranParser_s::FindIncludeFile(const char* dir,
- const char* includeName,
- std::string& fileName)
- {
-
- if (cmSystemTools::FileIsFullPath(includeName)) {
- fileName = includeName;
- return cmSystemTools::FileExists(fileName, true);
- }
-
-
- std::string fullName = dir;
- fullName += "/";
- fullName += includeName;
- if (cmSystemTools::FileExists(fullName, true)) {
- fileName = fullName;
- return true;
- }
-
- for (std::string const& i : this->IncludePath) {
- fullName = i;
- fullName += "/";
- fullName += includeName;
- if (cmSystemTools::FileExists(fullName, true)) {
- fileName = fullName;
- return true;
- }
- }
- return false;
- }
- cmFortranParser_s::cmFortranParser_s(std::vector<std::string> const& includes,
- std::set<std::string> const& defines,
- cmFortranSourceInfo& info)
- : IncludePath(includes)
- , PPDefinitions(defines)
- , Info(info)
- {
- this->InInterface = false;
- this->InPPFalseBranch = 0;
-
- cmFortran_yylex_init(&this->Scanner);
- cmFortran_yyset_extra(this, this->Scanner);
-
-
- YY_BUFFER_STATE buffer =
- cmFortran_yy_create_buffer(nullptr, 4, this->Scanner);
- cmFortran_yy_switch_to_buffer(buffer, this->Scanner);
- }
- cmFortranParser_s::~cmFortranParser_s()
- {
- cmFortran_yylex_destroy(this->Scanner);
- }
- bool cmFortranParser_FilePush(cmFortranParser* parser, const char* fname)
- {
-
-
- if (FILE* file = cmsys::SystemTools::Fopen(fname, "rb")) {
- YY_BUFFER_STATE current = cmFortranLexer_GetCurrentBuffer(parser->Scanner);
- std::string dir = cmSystemTools::GetParentDirectory(fname);
- cmFortranFile f(file, current, dir);
- YY_BUFFER_STATE buffer =
- cmFortran_yy_create_buffer(nullptr, 16384, parser->Scanner);
- cmFortran_yy_switch_to_buffer(buffer, parser->Scanner);
- parser->FileStack.push(f);
- return true;
- }
- return false;
- }
- bool cmFortranParser_FilePop(cmFortranParser* parser)
- {
-
-
- if (parser->FileStack.empty()) {
- return false;
- }
- cmFortranFile f = parser->FileStack.top();
- parser->FileStack.pop();
- fclose(f.File);
- YY_BUFFER_STATE current = cmFortranLexer_GetCurrentBuffer(parser->Scanner);
- cmFortran_yy_delete_buffer(current, parser->Scanner);
- cmFortran_yy_switch_to_buffer(f.Buffer, parser->Scanner);
- return true;
- }
- int cmFortranParser_Input(cmFortranParser* parser, char* buffer,
- size_t bufferSize)
- {
-
-
- if (!parser->FileStack.empty()) {
- cmFortranFile& ff = parser->FileStack.top();
- FILE* file = ff.File;
- size_t n = fread(buffer, 1, bufferSize, file);
- if (n > 0) {
- ff.LastCharWasNewline = buffer[n - 1] == '\n';
- } else if (!ff.LastCharWasNewline) {
-
-
- buffer[0] = '\n';
- n = 1;
- ff.LastCharWasNewline = true;
- }
- return static_cast<int>(n);
- }
- return 0;
- }
- void cmFortranParser_StringStart(cmFortranParser* parser)
- {
- parser->TokenString.clear();
- }
- const char* cmFortranParser_StringEnd(cmFortranParser* parser)
- {
- return parser->TokenString.c_str();
- }
- void cmFortranParser_StringAppend(cmFortranParser* parser, char c)
- {
- parser->TokenString += c;
- }
- void cmFortranParser_SetInInterface(cmFortranParser* parser, bool in)
- {
- if (parser->InPPFalseBranch) {
- return;
- }
- parser->InInterface = in;
- }
- bool cmFortranParser_GetInInterface(cmFortranParser* parser)
- {
- return parser->InInterface;
- }
- void cmFortranParser_SetOldStartcond(cmFortranParser* parser, int arg)
- {
- parser->OldStartcond = arg;
- }
- int cmFortranParser_GetOldStartcond(cmFortranParser* parser)
- {
- return parser->OldStartcond;
- }
- void cmFortranParser_Error(cmFortranParser* parser, const char* msg)
- {
- parser->Error = msg ? msg : "unknown error";
- }
- void cmFortranParser_RuleUse(cmFortranParser* parser, const char* name)
- {
- if (!parser->InPPFalseBranch) {
- parser->Info.Requires.insert(cmSystemTools::LowerCase(name));
- }
- }
- void cmFortranParser_RuleLineDirective(cmFortranParser* parser,
- const char* filename)
- {
-
- std::string included = filename;
-
-
- if (included.empty() || included[0] == '<') {
- return;
- }
-
-
- cmSystemTools::ReplaceString(included, "\\\\", "\\");
- cmSystemTools::ConvertToUnixSlashes(included);
-
- if (cmSystemTools::FileExists(included, true)) {
- parser->Info.Includes.insert(included);
- }
- }
- void cmFortranParser_RuleInclude(cmFortranParser* parser, const char* name)
- {
- if (parser->InPPFalseBranch) {
- return;
- }
-
- assert(!parser->FileStack.empty());
-
-
-
- std::string dir = parser->FileStack.top().Directory;
-
-
-
- std::string fullName;
- if (parser->FindIncludeFile(dir.c_str(), name, fullName)) {
-
- parser->Info.Includes.insert(fullName);
-
- cmFortranParser_FilePush(parser, fullName.c_str());
- }
- }
- void cmFortranParser_RuleModule(cmFortranParser* parser, const char* name)
- {
- if (!parser->InPPFalseBranch && !parser->InInterface) {
- parser->Info.Provides.insert(cmSystemTools::LowerCase(name));
- }
- }
- void cmFortranParser_RuleDefine(cmFortranParser* parser, const char* macro)
- {
- if (!parser->InPPFalseBranch) {
- parser->PPDefinitions.insert(macro);
- }
- }
- void cmFortranParser_RuleUndef(cmFortranParser* parser, const char* macro)
- {
- if (!parser->InPPFalseBranch) {
- std::set<std::string>::iterator match;
- match = parser->PPDefinitions.find(macro);
- if (match != parser->PPDefinitions.end()) {
- parser->PPDefinitions.erase(match);
- }
- }
- }
- void cmFortranParser_RuleIfdef(cmFortranParser* parser, const char* macro)
- {
-
- parser->SkipToEnd.push(false);
- if (parser->InPPFalseBranch) {
- parser->InPPFalseBranch++;
- } else if (parser->PPDefinitions.find(macro) ==
- parser->PPDefinitions.end()) {
- parser->InPPFalseBranch = 1;
- } else {
- parser->SkipToEnd.top() = true;
- }
- }
- void cmFortranParser_RuleIfndef(cmFortranParser* parser, const char* macro)
- {
-
- parser->SkipToEnd.push(false);
- if (parser->InPPFalseBranch) {
- parser->InPPFalseBranch++;
- } else if (parser->PPDefinitions.find(macro) !=
- parser->PPDefinitions.end()) {
- parser->InPPFalseBranch = 1;
- } else {
-
- parser->SkipToEnd.top() = true;
- }
- }
- void cmFortranParser_RuleIf(cmFortranParser* parser)
- {
-
-
-
- parser->SkipToEnd.push(false);
- }
- void cmFortranParser_RuleElif(cmFortranParser* parser)
- {
-
-
-
-
- if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top() &&
- !parser->InPPFalseBranch) {
- parser->InPPFalseBranch = 1;
- }
- }
- void cmFortranParser_RuleElse(cmFortranParser* parser)
- {
-
- if (parser->InPPFalseBranch > 1) {
- return;
- }
-
-
- if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top()) {
- parser->InPPFalseBranch = 1;
- } else {
- parser->InPPFalseBranch = 0;
- }
- }
- void cmFortranParser_RuleEndif(cmFortranParser* parser)
- {
- if (!parser->SkipToEnd.empty()) {
- parser->SkipToEnd.pop();
- }
-
-
- if (parser->InPPFalseBranch) {
- parser->InPPFalseBranch--;
- }
- }
|