1 // Copyright (c) 2018-2019, NVIDIA CORPORATION.  All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef FORTRAN_PARSER_PARSING_H_
16 #define FORTRAN_PARSER_PARSING_H_
17 
18 #include "characters.h"
19 #include "features.h"
20 #include "instrumented-parser.h"
21 #include "message.h"
22 #include "parse-tree.h"
23 #include "provenance.h"
24 #include <optional>
25 #include <ostream>
26 #include <string>
27 #include <utility>
28 #include <vector>
29 
30 namespace Fortran::parser {
31 
32 struct Options {
OptionsOptions33   Options() {}
34 
35   using Predefinition = std::pair<std::string, std::optional<std::string>>;
36 
37   bool isFixedForm{false};
38   int fixedFormColumns{72};
39   LanguageFeatureControl features;
40   std::vector<std::string> searchDirectories;
41   std::vector<Predefinition> predefinitions;
42   bool instrumentedParse{false};
43   bool isModuleFile{false};
44   bool needProvenanceRangeToCharBlockMappings{false};
45 };
46 
47 class Parsing {
48 public:
49   explicit Parsing(AllSources &);
50   ~Parsing();
51 
consumedWholeFile()52   bool consumedWholeFile() const { return consumedWholeFile_; }
finalRestingPlace()53   const char *finalRestingPlace() const { return finalRestingPlace_; }
cooked()54   CookedSource &cooked() { return cooked_; }
messages()55   Messages &messages() { return messages_; }
parseTree()56   std::optional<Program> &parseTree() { return parseTree_; }
57 
58   const SourceFile *Prescan(const std::string &path, Options);
59   void DumpCookedChars(std::ostream &) const;
60   void DumpProvenance(std::ostream &) const;
61   void DumpParsingLog(std::ostream &) const;
62   void Parse(std::ostream *debugOutput = nullptr);
63   void ClearLog();
64 
65   void EmitMessage(std::ostream &o, const char *at, const std::string &message,
66       bool echoSourceLine = false) const {
67     cooked_.allSources().EmitMessage(
68         o, cooked_.GetProvenanceRange(CharBlock(at)), message, echoSourceLine);
69   }
70 
71   bool ForTesting(std::string path, std::ostream &);
72 
73 private:
74   Options options_;
75   CookedSource cooked_;
76   Messages messages_;
77   bool consumedWholeFile_{false};
78   const char *finalRestingPlace_{nullptr};
79   std::optional<Program> parseTree_;
80   ParsingLog log_;
81 };
82 }
83 #endif  // FORTRAN_PARSER_PARSING_H_
84