cmFortranParserImpl.cxx 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*============================================================================
  2. CMake - Cross Platform Makefile Generator
  3. Copyright 2000-2015 Kitware, Inc., Insight Software Consortium
  4. Distributed under the OSI-approved BSD License (the "License");
  5. see accompanying file Copyright.txt for details.
  6. This software is distributed WITHOUT ANY WARRANTY; without even the
  7. implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the License for more information.
  9. ============================================================================*/
  10. #include "cmFortranParser.h"
  11. #include "cmSystemTools.h"
  12. #include <assert.h>
  13. //----------------------------------------------------------------------------
  14. bool cmFortranParser_s::FindIncludeFile(const char* dir,
  15. const char* includeName,
  16. std::string& fileName)
  17. {
  18. // If the file is a full path, include it directly.
  19. if(cmSystemTools::FileIsFullPath(includeName))
  20. {
  21. fileName = includeName;
  22. return cmSystemTools::FileExists(fileName.c_str(), true);
  23. }
  24. else
  25. {
  26. // Check for the file in the directory containing the including
  27. // file.
  28. std::string fullName = dir;
  29. fullName += "/";
  30. fullName += includeName;
  31. if(cmSystemTools::FileExists(fullName.c_str(), true))
  32. {
  33. fileName = fullName;
  34. return true;
  35. }
  36. // Search the include path for the file.
  37. for(std::vector<std::string>::const_iterator i =
  38. this->IncludePath.begin(); i != this->IncludePath.end(); ++i)
  39. {
  40. fullName = *i;
  41. fullName += "/";
  42. fullName += includeName;
  43. if(cmSystemTools::FileExists(fullName.c_str(), true))
  44. {
  45. fileName = fullName;
  46. return true;
  47. }
  48. }
  49. }
  50. return false;
  51. }
  52. //----------------------------------------------------------------------------
  53. cmFortranParser_s
  54. ::cmFortranParser_s(std::vector<std::string> const& includes,
  55. std::set<std::string> const& defines,
  56. cmFortranSourceInfo& info):
  57. IncludePath(includes), PPDefinitions(defines), Info(info)
  58. {
  59. this->InInterface = 0;
  60. this->InPPFalseBranch = 0;
  61. // Initialize the lexical scanner.
  62. cmFortran_yylex_init(&this->Scanner);
  63. cmFortran_yyset_extra(this, this->Scanner);
  64. // Create a dummy buffer that is never read but is the fallback
  65. // buffer when the last file is popped off the stack.
  66. YY_BUFFER_STATE buffer =
  67. cmFortran_yy_create_buffer(0, 4, this->Scanner);
  68. cmFortran_yy_switch_to_buffer(buffer, this->Scanner);
  69. }
  70. //----------------------------------------------------------------------------
  71. cmFortranParser_s::~cmFortranParser_s()
  72. {
  73. cmFortran_yylex_destroy(this->Scanner);
  74. }
  75. //----------------------------------------------------------------------------
  76. bool cmFortranParser_FilePush(cmFortranParser* parser,
  77. const char* fname)
  78. {
  79. // Open the new file and push it onto the stack. Save the old
  80. // buffer with it on the stack.
  81. if(FILE* file = cmsys::SystemTools::Fopen(fname, "rb"))
  82. {
  83. YY_BUFFER_STATE current =
  84. cmFortranLexer_GetCurrentBuffer(parser->Scanner);
  85. std::string dir = cmSystemTools::GetParentDirectory(fname);
  86. cmFortranFile f(file, current, dir);
  87. YY_BUFFER_STATE buffer =
  88. cmFortran_yy_create_buffer(0, 16384, parser->Scanner);
  89. cmFortran_yy_switch_to_buffer(buffer, parser->Scanner);
  90. parser->FileStack.push(f);
  91. return 1;
  92. }
  93. else
  94. {
  95. return 0;
  96. }
  97. }
  98. //----------------------------------------------------------------------------
  99. bool cmFortranParser_FilePop(cmFortranParser* parser)
  100. {
  101. // Pop one file off the stack and close it. Switch the lexer back
  102. // to the next one on the stack.
  103. if(parser->FileStack.empty())
  104. {
  105. return 0;
  106. }
  107. else
  108. {
  109. cmFortranFile f = parser->FileStack.top(); parser->FileStack.pop();
  110. fclose(f.File);
  111. YY_BUFFER_STATE current =
  112. cmFortranLexer_GetCurrentBuffer(parser->Scanner);
  113. cmFortran_yy_delete_buffer(current, parser->Scanner);
  114. cmFortran_yy_switch_to_buffer(f.Buffer, parser->Scanner);
  115. return 1;
  116. }
  117. }
  118. //----------------------------------------------------------------------------
  119. int cmFortranParser_Input(cmFortranParser* parser,
  120. char* buffer, size_t bufferSize)
  121. {
  122. // Read from the file on top of the stack. If the stack is empty,
  123. // the end of the translation unit has been reached.
  124. if(!parser->FileStack.empty())
  125. {
  126. FILE* file = parser->FileStack.top().File;
  127. return (int)fread(buffer, 1, bufferSize, file);
  128. }
  129. return 0;
  130. }
  131. //----------------------------------------------------------------------------
  132. void cmFortranParser_StringStart(cmFortranParser* parser)
  133. {
  134. parser->TokenString = "";
  135. }
  136. //----------------------------------------------------------------------------
  137. const char* cmFortranParser_StringEnd(cmFortranParser* parser)
  138. {
  139. return parser->TokenString.c_str();
  140. }
  141. //----------------------------------------------------------------------------
  142. void cmFortranParser_StringAppend(cmFortranParser* parser,
  143. char c)
  144. {
  145. parser->TokenString += c;
  146. }
  147. //----------------------------------------------------------------------------
  148. void cmFortranParser_SetInInterface(cmFortranParser* parser,
  149. bool in)
  150. {
  151. if(parser->InPPFalseBranch)
  152. {
  153. return;
  154. }
  155. parser->InInterface = in;
  156. }
  157. //----------------------------------------------------------------------------
  158. bool cmFortranParser_GetInInterface(cmFortranParser* parser)
  159. {
  160. return parser->InInterface;
  161. }
  162. //----------------------------------------------------------------------------
  163. void cmFortranParser_SetOldStartcond(cmFortranParser* parser,
  164. int arg)
  165. {
  166. parser->OldStartcond = arg;
  167. }
  168. //----------------------------------------------------------------------------
  169. int cmFortranParser_GetOldStartcond(cmFortranParser* parser)
  170. {
  171. return parser->OldStartcond;
  172. }
  173. //----------------------------------------------------------------------------
  174. void cmFortranParser_Error(cmFortranParser*, const char*)
  175. {
  176. // If there is a parser error just ignore it. The source will not
  177. // compile and the user will edit it. Then dependencies will have
  178. // to be regenerated anyway.
  179. }
  180. //----------------------------------------------------------------------------
  181. void cmFortranParser_RuleUse(cmFortranParser* parser,
  182. const char* name)
  183. {
  184. if(!parser->InPPFalseBranch)
  185. {
  186. parser->Info.Requires.insert(cmSystemTools::LowerCase(name) );
  187. }
  188. }
  189. //----------------------------------------------------------------------------
  190. void cmFortranParser_RuleInclude(cmFortranParser* parser,
  191. const char* name)
  192. {
  193. if(parser->InPPFalseBranch)
  194. {
  195. return;
  196. }
  197. // If processing an include statement there must be an open file.
  198. assert(!parser->FileStack.empty());
  199. // Get the directory containing the source in which the include
  200. // statement appears. This is always the first search location for
  201. // Fortran include files.
  202. std::string dir = parser->FileStack.top().Directory;
  203. // Find the included file. If it cannot be found just ignore the
  204. // problem because either the source will not compile or the user
  205. // does not care about depending on this included source.
  206. std::string fullName;
  207. if(parser->FindIncludeFile(dir.c_str(), name, fullName))
  208. {
  209. // Found the included file. Save it in the set of included files.
  210. parser->Info.Includes.insert(fullName);
  211. // Parse it immediately to translate the source inline.
  212. cmFortranParser_FilePush(parser, fullName.c_str());
  213. }
  214. }
  215. //----------------------------------------------------------------------------
  216. void cmFortranParser_RuleModule(cmFortranParser* parser,
  217. const char* name)
  218. {
  219. if(!parser->InPPFalseBranch && !parser->InInterface)
  220. {
  221. parser->Info.Provides.insert(cmSystemTools::LowerCase(name));
  222. }
  223. }
  224. //----------------------------------------------------------------------------
  225. void cmFortranParser_RuleDefine(cmFortranParser* parser,
  226. const char* macro)
  227. {
  228. if(!parser->InPPFalseBranch)
  229. {
  230. parser->PPDefinitions.insert(macro);
  231. }
  232. }
  233. //----------------------------------------------------------------------------
  234. void cmFortranParser_RuleUndef(cmFortranParser* parser,
  235. const char* macro)
  236. {
  237. if(!parser->InPPFalseBranch)
  238. {
  239. std::set<std::string>::iterator match;
  240. match = parser->PPDefinitions.find(macro);
  241. if(match != parser->PPDefinitions.end())
  242. {
  243. parser->PPDefinitions.erase(match);
  244. }
  245. }
  246. }
  247. //----------------------------------------------------------------------------
  248. void cmFortranParser_RuleIfdef(cmFortranParser* parser,
  249. const char* macro)
  250. {
  251. // A new PP branch has been opened
  252. parser->SkipToEnd.push(false);
  253. if (parser->InPPFalseBranch)
  254. {
  255. parser->InPPFalseBranch++;
  256. }
  257. else if(parser->PPDefinitions.find(macro) == parser->PPDefinitions.end())
  258. {
  259. parser->InPPFalseBranch=1;
  260. }
  261. else
  262. {
  263. parser->SkipToEnd.top() = true;
  264. }
  265. }
  266. //----------------------------------------------------------------------------
  267. void cmFortranParser_RuleIfndef(cmFortranParser* parser,
  268. const char* macro)
  269. {
  270. // A new PP branch has been opened
  271. parser->SkipToEnd.push(false);
  272. if (parser->InPPFalseBranch)
  273. {
  274. parser->InPPFalseBranch++;
  275. }
  276. else if(parser->PPDefinitions.find(macro) != parser->PPDefinitions.end())
  277. {
  278. parser->InPPFalseBranch = 1;
  279. }
  280. else
  281. {
  282. // ignore other branches
  283. parser->SkipToEnd.top() = true;
  284. }
  285. }
  286. //----------------------------------------------------------------------------
  287. void cmFortranParser_RuleIf(cmFortranParser* parser)
  288. {
  289. /* Note: The current parser is _not_ able to get statements like
  290. * #if 0
  291. * #if 1
  292. * #if MYSMBOL
  293. * #if defined(MYSYMBOL)
  294. * #if defined(MYSYMBOL) && ...
  295. * right. The same for #elif. Thus in
  296. * #if SYMBOL_1
  297. * ..
  298. * #elif SYMBOL_2
  299. * ...
  300. * ...
  301. * #elif SYMBOL_N
  302. * ..
  303. * #else
  304. * ..
  305. * #endif
  306. * _all_ N+1 branches are considered. If you got something like this
  307. * #if defined(MYSYMBOL)
  308. * #if !defined(MYSYMBOL)
  309. * use
  310. * #ifdef MYSYMBOL
  311. * #ifndef MYSYMBOL
  312. * instead.
  313. */
  314. // A new PP branch has been opened
  315. // Never skip! See note above.
  316. parser->SkipToEnd.push(false);
  317. }
  318. //----------------------------------------------------------------------------
  319. void cmFortranParser_RuleElif(cmFortranParser* parser)
  320. {
  321. /* Note: There are parser limitations. See the note at
  322. * cmFortranParser_RuleIf(..)
  323. */
  324. // Always taken unless an #ifdef or #ifndef-branch has been taken
  325. // already. If the second condition isn't meet already
  326. // (parser->InPPFalseBranch == 0) correct it.
  327. if(!parser->SkipToEnd.empty() &&
  328. parser->SkipToEnd.top() && !parser->InPPFalseBranch)
  329. {
  330. parser->InPPFalseBranch = 1;
  331. }
  332. }
  333. //----------------------------------------------------------------------------
  334. void cmFortranParser_RuleElse(cmFortranParser* parser)
  335. {
  336. // if the parent branch is false do nothing!
  337. if(parser->InPPFalseBranch > 1)
  338. {
  339. return;
  340. }
  341. // parser->InPPFalseBranch is either 0 or 1. We change it depending on
  342. // parser->SkipToEnd.top()
  343. if(!parser->SkipToEnd.empty() &&
  344. parser->SkipToEnd.top())
  345. {
  346. parser->InPPFalseBranch = 1;
  347. }
  348. else
  349. {
  350. parser->InPPFalseBranch = 0;
  351. }
  352. }
  353. //----------------------------------------------------------------------------
  354. void cmFortranParser_RuleEndif(cmFortranParser* parser)
  355. {
  356. if(!parser->SkipToEnd.empty())
  357. {
  358. parser->SkipToEnd.pop();
  359. }
  360. // #endif doesn't know if there was a "#else" in before, so it
  361. // always decreases InPPFalseBranch
  362. if(parser->InPPFalseBranch)
  363. {
  364. parser->InPPFalseBranch--;
  365. }
  366. }