9#ifndef FORTRAN_PARSER_PRESCAN_H_
10#define FORTRAN_PARSER_PRESCAN_H_
19#include "flang/Common/Fortran-features.h"
20#include "flang/Parser/characters.h"
21#include "flang/Parser/message.h"
22#include "flang/Parser/provenance.h"
23#include "flang/Parser/token-sequence.h"
27#include <unordered_set>
43 const AllSources &allSources()
const {
return allSources_; }
44 AllSources &allSources() {
return allSources_; }
45 const Messages &messages()
const {
return messages_; }
46 Messages &messages() {
return messages_; }
47 const Preprocessor &preprocessor()
const {
return preprocessor_; }
52 preprocessingOnly_ = yes;
56 expandIncludeLines_ = yes;
67 Prescanner &set_fixedFormColumnLimit(
int limit) {
68 fixedFormColumnLimit_ = limit;
72 Prescanner &AddCompilerDirectiveSentinel(
const std::string &);
79 bool IsAtEnd()
const {
return nextLine_ >= limit_; }
80 bool IsNextLinePreprocessorDirective()
const;
82 Provenance GetCurrentProvenance()
const {
return GetProvenance(at_); }
84 const char *IsCompilerDirectiveSentinel(
const char *, std::size_t)
const;
85 const char *IsCompilerDirectiveSentinel(
CharBlock)
const;
87 std::optional<std::pair<const char *, const char *>>
88 IsCompilerDirectiveSentinel(
const char *p)
const;
90 template <
typename... A>
Message &Say(A &&...a) {
91 return messages_.Say(std::forward<A>(a)...);
95 struct LineClassification {
98 ConditionalCompilationDirective,
101 PreprocessorDirective,
106 LineClassification(Kind k, std::size_t po = 0,
const char *s =
nullptr)
107 : kind{k}, payloadOffset{po}, sentinel{s} {}
108 LineClassification(LineClassification &&) =
default;
109 LineClassification &operator=(LineClassification &&) =
default;
111 std::size_t payloadOffset;
112 const char *sentinel;
115 void BeginSourceLine(
const char *at) {
118 tabInCurrentLine_ =
false;
121 void BeginSourceLineAndAdvance() {
122 BeginSourceLine(nextLine_);
126 void BeginStatementAndAdvance() {
127 BeginSourceLineAndAdvance();
128 slashInCurrentStatement_ =
false;
129 preventHollerith_ =
false;
130 parenthesisNesting_ = 0;
131 continuationLines_ = 0;
132 isPossibleMacroCall_ =
false;
133 disableSourceContinuation_ =
false;
136 Provenance GetProvenance(
const char *sourceChar)
const {
137 return startProvenance_ + (sourceChar - start_);
141 const char *first,
const char *afterLast)
const {
142 std::size_t bytes = afterLast - first;
143 return {startProvenance_ + (first - start_), bytes};
147 tokens.PutNextTokenChar(ch, GetCurrentProvenance());
151 Provenance provenance{allSources_.CompilerInsertionProvenance(ch)};
152 tokens.PutNextTokenChar(ch, provenance);
156 EmitChar(tokens, ch);
161 bool InCompilerDirective()
const {
return directiveSentinel_ !=
nullptr; }
162 bool InFixedFormSource()
const {
163 return inFixedForm_ && !inPreprocessorDirective_ && !InCompilerDirective();
166 bool IsCComment(
const char *p)
const {
167 return p[0] ==
'/' && p[1] ==
'*' &&
168 (inPreprocessorDirective_ ||
171 common::LanguageFeature::ClassicCComments)));
177 void SkipToEndOfLine();
178 bool MustSkipToEndOfLine()
const;
181 bool SkipToNextSignificantCharacter();
182 void SkipCComments();
184 static const char *SkipWhiteSpace(
const char *);
185 const char *SkipWhiteSpaceAndCComments(
const char *)
const;
186 const char *SkipCComment(
const char *)
const;
189 void QuotedCharacterLiteral(
TokenSequence &,
const char *start);
190 void Hollerith(
TokenSequence &,
int count,
const char *start);
192 bool SkipCommentLine(
bool afterAmpersand);
193 bool IsFixedFormCommentLine(
const char *)
const;
194 const char *IsFreeFormComment(
const char *)
const;
195 std::optional<std::size_t> IsIncludeLine(
const char *)
const;
196 void FortranInclude(
const char *quote);
197 const char *IsPreprocessorDirectiveLine(
const char *)
const;
198 const char *FixedFormContinuationLine(
bool mightNeedSpace);
199 const char *FreeFormContinuationLine(
bool ampersand);
200 bool IsImplicitContinuation()
const;
201 bool FixedFormContinuation(
bool mightNeedSpace);
202 bool FreeFormContinuation();
203 bool Continuation(
bool mightNeedFixedFormSpace);
204 std::optional<LineClassification> IsFixedFormCompilerDirectiveLine(
206 std::optional<LineClassification> IsFreeFormCompilerDirectiveLine(
208 LineClassification ClassifyLine(
const char *)
const;
209 LineClassification ClassifyLine(
211 void SourceFormChange(std::string &&);
212 bool CompilerDirectiveContinuation(
TokenSequence &,
const char *sentinel);
220 bool preprocessingOnly_{
false};
221 bool expandIncludeLines_{
true};
222 bool isNestedInIncludeDirective_{
false};
223 bool backslashFreeFormContinuation_{
false};
224 bool inFixedForm_{
false};
225 int fixedFormColumnLimit_{72};
226 Encoding encoding_{Encoding::UTF_8};
227 int parenthesisNesting_{0};
228 int prescannerNesting_{0};
229 int continuationLines_{0};
230 bool isPossibleMacroCall_{
false};
231 bool afterPreprocessingDirective_{
false};
232 bool disableSourceContinuation_{
false};
235 const char *start_{
nullptr};
236 const char *limit_{
nullptr};
237 const char *nextLine_{
nullptr};
238 const char *directiveSentinel_{
nullptr};
242 const char *at_{
nullptr};
244 bool tabInCurrentLine_{
false};
245 bool slashInCurrentStatement_{
false};
246 bool preventHollerith_{
false};
247 bool inCharLiteral_{
false};
248 bool continuationInCharLiteral_{
false};
249 bool inPreprocessorDirective_{
false};
254 bool insertASpace_{
false};
261 bool omitNewline_{
false};
262 bool skipLeadingAmpersand_{
false};
264 const std::size_t firstCookedCharacterOffset_{cooked_.BufferedBytes()};
267 allSources_.CompilerInsertionProvenance(
' ')};
269 allSources_.CompilerInsertionProvenance(
'\\')};
273 static const int prime1{1019}, prime2{1021};
274 std::bitset<prime2> compilerDirectiveBloomFilter_;
275 std::unordered_set<std::string> compilerDirectiveSentinels_;
Definition: Fortran-features.h:84
Definition: provenance.h:139
Definition: char-block.h:28
Definition: provenance.h:233
Definition: message.h:188
Definition: message.h:319
Definition: preprocessor.h:73
Definition: provenance.h:52
Definition: token-sequence.h:35
Definition: check-expression.h:19
Definition: parse-tree.h:3353
Definition: parse-tree.h:355