2018-01-30 20:54:47 +01:00
|
|
|
#ifndef FORTRAN_PRESCAN_H_
|
|
|
|
#define FORTRAN_PRESCAN_H_
|
|
|
|
|
|
|
|
// Defines a fast Fortran source prescanning phase that implements some
|
|
|
|
// character-level features of the language that can be inefficient to
|
|
|
|
// support directly in a backtracking parser. This phase handles Fortran
|
|
|
|
// line continuation, comment removal, card image margins, padding out
|
|
|
|
// fixed form character literals on truncated card images, and drives the
|
|
|
|
// Fortran source preprocessor.
|
|
|
|
//
|
|
|
|
// It is possible to run the Fortran parser without running this prescan
|
|
|
|
// phase, using only the parsers defined in cooked-chars.h, so long as
|
|
|
|
// preprocessing and INCLUDE lines need not be handled.
|
|
|
|
|
|
|
|
#include "char-buffer.h"
|
2018-02-03 00:52:43 +01:00
|
|
|
#include "message.h"
|
2018-02-01 21:08:02 +01:00
|
|
|
#include "position.h"
|
2018-01-30 20:54:47 +01:00
|
|
|
#include "preprocessor.h"
|
|
|
|
#include "source.h"
|
|
|
|
#include <optional>
|
|
|
|
|
|
|
|
namespace Fortran {
|
2018-02-07 21:04:42 +01:00
|
|
|
namespace parser {
|
2018-01-30 20:54:47 +01:00
|
|
|
|
|
|
|
class Prescanner {
|
2018-02-05 21:54:36 +01:00
|
|
|
public:
|
2018-02-03 00:52:43 +01:00
|
|
|
explicit Prescanner(Messages &messages)
|
|
|
|
: messages_{messages}, preprocessor_{*this} {}
|
2018-02-02 00:01:23 +01:00
|
|
|
|
2018-02-03 00:52:43 +01:00
|
|
|
Messages &messages() const { return messages_; }
|
2018-02-02 00:01:23 +01:00
|
|
|
const SourceFile &sourceFile() const { return *sourceFile_; }
|
|
|
|
Position position() const { return atPosition_; }
|
2018-02-03 00:52:43 +01:00
|
|
|
bool anyFatalErrors() const { return anyFatalErrors_; }
|
2018-01-30 20:54:47 +01:00
|
|
|
|
|
|
|
Prescanner &set_fixedForm(bool yes) {
|
|
|
|
inFixedForm_ = yes;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Prescanner &set_enableOldDebugLines(bool yes) {
|
|
|
|
enableOldDebugLines_ = yes;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Prescanner &set_enableBackslashEscapesInCharLiterals(bool yes) {
|
|
|
|
enableBackslashEscapesInCharLiterals_ = yes;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
Prescanner &set_fixedFormColumnLimit(int limit) {
|
|
|
|
fixedFormColumnLimit_ = limit;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
CharBuffer Prescan(const SourceFile &source);
|
|
|
|
std::optional<TokenSequence> NextTokenizedLine();
|
|
|
|
|
2018-02-05 21:54:36 +01:00
|
|
|
private:
|
2018-02-01 21:08:02 +01:00
|
|
|
void BeginSourceLine(const char *at) {
|
2018-01-30 20:54:47 +01:00
|
|
|
at_ = at;
|
2018-02-01 21:08:02 +01:00
|
|
|
atPosition_ = lineStartPosition_;
|
2018-01-30 20:54:47 +01:00
|
|
|
tabInCurrentLine_ = false;
|
|
|
|
preventHollerith_ = false;
|
2018-01-31 00:22:26 +01:00
|
|
|
delimiterNesting_ = 0;
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void BeginSourceLineAndAdvance() {
|
|
|
|
BeginSourceLine(lineStart_);
|
|
|
|
NextLine();
|
|
|
|
}
|
|
|
|
|
|
|
|
char EmitCharAndAdvance(TokenSequence *tokens, char ch) {
|
|
|
|
tokens->AddChar(ch);
|
|
|
|
NextChar();
|
|
|
|
return *at_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NextLine();
|
2018-01-31 00:22:26 +01:00
|
|
|
void LabelField(TokenSequence *);
|
2018-01-30 20:54:47 +01:00
|
|
|
void NextChar();
|
|
|
|
void SkipSpaces();
|
|
|
|
bool NextToken(TokenSequence *);
|
|
|
|
bool ExponentAndKind(TokenSequence *);
|
|
|
|
void QuotedCharacterLiteral(TokenSequence *);
|
|
|
|
bool PadOutCharacterLiteral();
|
2018-01-31 00:22:26 +01:00
|
|
|
bool CommentLines();
|
|
|
|
bool CommentLinesAndPreprocessorDirectives();
|
2018-01-30 20:54:47 +01:00
|
|
|
bool IsFixedFormCommentLine(const char *);
|
|
|
|
bool IsFreeFormComment(const char *);
|
|
|
|
bool IsPreprocessorDirectiveLine(const char *);
|
|
|
|
const char *FixedFormContinuationLine();
|
|
|
|
bool FixedFormContinuation();
|
|
|
|
bool FreeFormContinuation();
|
2018-01-31 00:22:26 +01:00
|
|
|
void PayNewlineDebt(CharBuffer *);
|
2018-01-30 20:54:47 +01:00
|
|
|
|
2018-02-03 00:52:43 +01:00
|
|
|
Messages &messages_;
|
|
|
|
bool anyFatalErrors_{false};
|
2018-01-30 20:54:47 +01:00
|
|
|
const char *lineStart_{nullptr}; // next line to process; <= limit_
|
|
|
|
const char *at_{nullptr}; // next character to process; < lineStart_
|
|
|
|
int column_{1}; // card image column position of next character
|
|
|
|
const char *limit_{nullptr}; // first address after end of source
|
|
|
|
int newlineDebt_{0}; // newline characters consumed but not yet emitted
|
2018-02-02 00:01:23 +01:00
|
|
|
const SourceFile *sourceFile_{nullptr};
|
2018-02-01 21:08:02 +01:00
|
|
|
Position atPosition_, lineStartPosition_;
|
2018-01-30 20:54:47 +01:00
|
|
|
bool inCharLiteral_{false};
|
|
|
|
bool inPreprocessorDirective_{false};
|
|
|
|
bool inFixedForm_{true};
|
|
|
|
int fixedFormColumnLimit_{72};
|
|
|
|
bool tabInCurrentLine_{false};
|
|
|
|
bool preventHollerith_{false};
|
|
|
|
bool enableOldDebugLines_{false};
|
|
|
|
bool enableBackslashEscapesInCharLiterals_{true};
|
2018-01-31 00:22:26 +01:00
|
|
|
int delimiterNesting_{0};
|
2018-01-30 20:54:47 +01:00
|
|
|
Preprocessor preprocessor_;
|
|
|
|
};
|
2018-02-07 21:04:42 +01:00
|
|
|
} // namespace parser
|
2018-01-30 20:54:47 +01:00
|
|
|
} // namespace Fortran
|
|
|
|
#endif // FORTRAN_PRESCAN_H_
|