1 //===-- include/flang/Parser/parsing.h --------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef FORTRAN_PARSER_PARSING_H_
10 #define FORTRAN_PARSER_PARSING_H_
11 
12 #include "characters.h"
13 #include "instrumented-parser.h"
14 #include "message.h"
15 #include "parse-tree.h"
16 #include "provenance.h"
17 #include "flang/Common/Fortran-features.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include <optional>
20 #include <string>
21 #include <utility>
22 #include <vector>
23 
24 namespace Fortran::parser {
25 
26 struct Options {
OptionsOptions27   Options() {}
28 
29   using Predefinition = std::pair<std::string, std::optional<std::string>>;
30 
31   bool isFixedForm{false};
32   int fixedFormColumns{72};
33   common::LanguageFeatureControl features;
34   std::vector<std::string> searchDirectories;
35   std::vector<Predefinition> predefinitions;
36   bool instrumentedParse{false};
37   bool isModuleFile{false};
38   bool needProvenanceRangeToCharBlockMappings{false};
39 };
40 
41 class Parsing {
42 public:
43   explicit Parsing(AllCookedSources &);
44   ~Parsing();
45 
consumedWholeFile()46   bool consumedWholeFile() const { return consumedWholeFile_; }
finalRestingPlace()47   const char *finalRestingPlace() const { return finalRestingPlace_; }
allCooked()48   AllCookedSources &allCooked() { return allCooked_; }
messages()49   Messages &messages() { return messages_; }
parseTree()50   std::optional<Program> &parseTree() { return parseTree_; }
51 
cooked()52   const CookedSource &cooked() const { return DEREF(currentCooked_); }
53 
54   const SourceFile *Prescan(const std::string &path, Options);
55   void DumpCookedChars(llvm::raw_ostream &) const;
56   void DumpProvenance(llvm::raw_ostream &) const;
57   void DumpParsingLog(llvm::raw_ostream &) const;
58   void Parse(llvm::raw_ostream &debugOutput);
59   void ClearLog();
60 
61   void EmitMessage(llvm::raw_ostream &o, const char *at,
62       const std::string &message, bool echoSourceLine = false) const {
63     allCooked_.allSources().EmitMessage(o,
64         allCooked_.GetProvenanceRange(CharBlock(at)), message, echoSourceLine);
65   }
66 
67 private:
68   Options options_;
69   AllCookedSources &allCooked_;
70   CookedSource *currentCooked_{nullptr};
71   Messages messages_;
72   bool consumedWholeFile_{false};
73   const char *finalRestingPlace_{nullptr};
74   std::optional<Program> parseTree_;
75   ParsingLog log_;
76 };
77 } // namespace Fortran::parser
78 #endif // FORTRAN_PARSER_PARSING_H_
79