2018-02-08 01:27:36 +01:00
|
|
|
#include "prescan.h"
|
2018-02-27 23:02:10 +01:00
|
|
|
#include "characters.h"
|
2018-01-30 20:54:47 +01:00
|
|
|
#include "idioms.h"
|
2018-02-13 21:50:47 +01:00
|
|
|
#include "message.h"
|
|
|
|
#include "preprocessor.h"
|
2018-01-30 20:54:47 +01:00
|
|
|
#include "source.h"
|
2018-02-13 21:50:47 +01:00
|
|
|
#include "token-sequence.h"
|
2018-03-20 18:59:07 +01:00
|
|
|
#include <cstddef>
|
2018-01-30 20:54:47 +01:00
|
|
|
#include <cstring>
|
2018-02-13 23:22:08 +01:00
|
|
|
#include <sstream>
|
2018-01-30 20:54:47 +01:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace Fortran {
|
2018-02-07 21:04:42 +01:00
|
|
|
namespace parser {
|
2018-01-30 20:54:47 +01:00
|
|
|
|
2018-02-13 21:24:54 +01:00
|
|
|
Prescanner::Prescanner(
|
2018-03-23 23:14:52 +01:00
|
|
|
Messages &messages, CookedSource &cooked, Preprocessor &preprocessor)
|
2018-02-13 21:24:54 +01:00
|
|
|
: messages_{messages}, cooked_{cooked}, preprocessor_{preprocessor} {}
|
2018-02-12 23:43:16 +01:00
|
|
|
|
2018-02-13 23:22:08 +01:00
|
|
|
Prescanner::Prescanner(const Prescanner &that)
|
|
|
|
: messages_{that.messages_}, cooked_{that.cooked_},
|
|
|
|
preprocessor_{that.preprocessor_}, inFixedForm_{that.inFixedForm_},
|
|
|
|
fixedFormColumnLimit_{that.fixedFormColumnLimit_},
|
|
|
|
enableOldDebugLines_{that.enableOldDebugLines_},
|
|
|
|
enableBackslashEscapesInCharLiterals_{
|
2018-03-20 18:59:07 +01:00
|
|
|
that.enableBackslashEscapesInCharLiterals_},
|
|
|
|
compilerDirectiveBloomFilter_{that.compilerDirectiveBloomFilter_},
|
|
|
|
compilerDirectiveSentinels_{that.compilerDirectiveSentinels_} {}
|
2018-02-13 23:22:08 +01:00
|
|
|
|
2018-03-23 23:14:52 +01:00
|
|
|
static void NormalizeCompilerDirectiveCommentMarker(TokenSequence *dir) {
|
|
|
|
char *p{dir->GetMutableCharData()};
|
|
|
|
char *limit{p + dir->SizeInChars()};
|
|
|
|
for (; p < limit; ++p) {
|
|
|
|
if (*p != ' ') {
|
|
|
|
CHECK(*p == '*' || *p == 'c' || *p == 'C' || *p == '!');
|
|
|
|
*p = '!';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CHECK(!"compiler directive all blank");
|
|
|
|
}
|
|
|
|
|
2018-02-13 21:24:54 +01:00
|
|
|
bool Prescanner::Prescan(ProvenanceRange range) {
|
2018-03-23 23:14:52 +01:00
|
|
|
AllSources &allSources{cooked_.allSources()};
|
|
|
|
ProvenanceRange around{allSources.GetContiguousRangeAround(range)};
|
2018-02-27 23:02:10 +01:00
|
|
|
startProvenance_ = range.start();
|
2018-03-20 18:59:07 +01:00
|
|
|
std::size_t offset{0};
|
2018-03-23 23:14:52 +01:00
|
|
|
const SourceFile *source{allSources.GetSourceFile(startProvenance_, &offset)};
|
2018-02-15 19:42:36 +01:00
|
|
|
CHECK(source != nullptr);
|
|
|
|
start_ = source->content() + offset;
|
|
|
|
limit_ = start_ + range.size();
|
|
|
|
lineStart_ = start_;
|
2018-03-23 23:14:52 +01:00
|
|
|
const bool beganInFixedForm{inFixedForm_};
|
2018-01-30 20:54:47 +01:00
|
|
|
while (lineStart_ < limit_) {
|
2018-03-23 23:14:52 +01:00
|
|
|
Statement();
|
|
|
|
}
|
|
|
|
if (inFixedForm_ != beganInFixedForm) {
|
|
|
|
std::string dir{"!dir$ "};
|
|
|
|
if (beganInFixedForm) {
|
|
|
|
dir += "fixed";
|
|
|
|
} else {
|
|
|
|
dir += "free";
|
2018-01-31 00:22:26 +01:00
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
dir += '\n';
|
|
|
|
TokenSequence tokens{dir, allSources.AddCompilerInsertion(dir).start()};
|
|
|
|
tokens.Emit(&cooked_);
|
|
|
|
}
|
|
|
|
return !anyFatalErrors_;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Prescanner::Statement() {
|
|
|
|
TokenSequence tokens;
|
|
|
|
LineClassification line{ClassifyLine(lineStart_)};
|
|
|
|
switch (line.kind) {
|
|
|
|
case LineClassification::Kind::Comment: NextLine(); return;
|
|
|
|
case LineClassification::Kind::Include:
|
|
|
|
FortranInclude(lineStart_ + line.payloadOffset);
|
|
|
|
NextLine();
|
|
|
|
return;
|
|
|
|
case LineClassification::Kind::PreprocessorDirective:
|
|
|
|
if (std::optional<TokenSequence> toks{TokenizePreprocessorDirective()}) {
|
|
|
|
preprocessor_.Directive(*toks, this);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
case LineClassification::Kind::CompilerDirective:
|
|
|
|
directiveSentinel_ = line.sentinel;
|
|
|
|
CHECK(directiveSentinel_ != nullptr);
|
|
|
|
BeginSourceLineAndAdvance();
|
|
|
|
if (inFixedForm_) {
|
|
|
|
CHECK(*at_ == '!' || *at_ == '*' || *at_ == 'c' || *at_ == 'C');
|
|
|
|
} else {
|
|
|
|
while (*at_ == ' ' || *at_ == '\t') {
|
|
|
|
++at_;
|
|
|
|
}
|
|
|
|
CHECK(*at_ == '!');
|
|
|
|
}
|
|
|
|
tokens.PutNextTokenChar('!', GetCurrentProvenance());
|
|
|
|
++at_, ++column_;
|
|
|
|
for (const char *sp{directiveSentinel_}; *sp != '\0';
|
|
|
|
++sp, ++at_, ++column_) {
|
|
|
|
tokens.PutNextTokenChar(*sp, GetCurrentProvenance());
|
|
|
|
}
|
|
|
|
tokens.CloseToken();
|
|
|
|
break;
|
|
|
|
case LineClassification::Kind::Source:
|
2018-01-30 20:54:47 +01:00
|
|
|
BeginSourceLineAndAdvance();
|
|
|
|
if (inFixedForm_) {
|
2018-01-31 00:22:26 +01:00
|
|
|
LabelField(&tokens);
|
2018-01-30 20:54:47 +01:00
|
|
|
} else {
|
|
|
|
SkipSpaces();
|
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (NextToken(&tokens)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
Provenance newlineProvenance{GetCurrentProvenance()};
|
|
|
|
if (std::optional<TokenSequence> preprocessed{
|
|
|
|
preprocessor_.MacroReplacement(tokens, *this)}) {
|
|
|
|
// Reprocess the preprocessed line.
|
|
|
|
preprocessed->PutNextTokenChar('\n', newlineProvenance);
|
|
|
|
preprocessed->CloseToken();
|
|
|
|
const char *ppd{preprocessed->ToCharBlock().begin()};
|
|
|
|
LineClassification ppl{ClassifyLine(ppd)};
|
|
|
|
switch (ppl.kind) {
|
|
|
|
case LineClassification::Kind::Comment: break;
|
|
|
|
case LineClassification::Kind::Include:
|
|
|
|
FortranInclude(ppd + ppl.payloadOffset);
|
|
|
|
break;
|
|
|
|
case LineClassification::Kind::PreprocessorDirective:
|
|
|
|
Complain("preprocessed line looks like a preprocessor directive"_en_US,
|
|
|
|
preprocessed->GetProvenanceRange().start());
|
|
|
|
preprocessed->ToLowerCase().Emit(&cooked_);
|
|
|
|
break;
|
|
|
|
case LineClassification::Kind::CompilerDirective:
|
|
|
|
NormalizeCompilerDirectiveCommentMarker(&*preprocessed);
|
|
|
|
preprocessed->ToLowerCase();
|
|
|
|
SourceFormChange(preprocessed->ToString());
|
|
|
|
preprocessed->Emit(&cooked_);
|
|
|
|
break;
|
|
|
|
case LineClassification::Kind::Source:
|
|
|
|
preprocessed->ToLowerCase().Emit(&cooked_);
|
|
|
|
break;
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
} else {
|
|
|
|
tokens.ToLowerCase();
|
|
|
|
if (line.kind == LineClassification::Kind::CompilerDirective) {
|
|
|
|
SourceFormChange(tokens.ToString());
|
|
|
|
}
|
|
|
|
tokens.Emit(&cooked_);
|
|
|
|
cooked_.Put('\n', newlineProvenance);
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
directiveSentinel_ = nullptr;
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
|
2018-03-17 00:58:35 +01:00
|
|
|
TokenSequence Prescanner::TokenizePreprocessorDirective() {
|
|
|
|
CHECK(lineStart_ < limit_ && !inPreprocessorDirective_);
|
2018-02-01 21:08:02 +01:00
|
|
|
auto saveAt = at_;
|
2018-03-17 00:58:35 +01:00
|
|
|
inPreprocessorDirective_ = true;
|
2018-01-30 20:54:47 +01:00
|
|
|
BeginSourceLineAndAdvance();
|
|
|
|
TokenSequence tokens;
|
|
|
|
while (NextToken(&tokens)) {
|
|
|
|
}
|
2018-03-17 00:13:49 +01:00
|
|
|
inPreprocessorDirective_ = false;
|
2018-02-01 21:08:02 +01:00
|
|
|
at_ = saveAt;
|
2018-01-30 20:54:47 +01:00
|
|
|
return {std::move(tokens)};
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:48:49 +01:00
|
|
|
Message &Prescanner::Error(Message &&message) {
|
2018-03-17 00:13:49 +01:00
|
|
|
anyFatalErrors_ = true;
|
2018-03-23 23:14:52 +01:00
|
|
|
return messages_.Put(std::move(message));
|
2018-03-17 00:13:49 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:48:49 +01:00
|
|
|
Message &Prescanner::Error(MessageFixedText text, Provenance p) {
|
2018-03-17 00:13:49 +01:00
|
|
|
anyFatalErrors_ = true;
|
2018-03-23 23:14:52 +01:00
|
|
|
return messages_.Put({p, text});
|
2018-03-17 00:13:49 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:48:49 +01:00
|
|
|
Message &Prescanner::Error(MessageFormattedText &&text, Provenance p) {
|
|
|
|
anyFatalErrors_ = true;
|
2018-03-23 23:14:52 +01:00
|
|
|
return messages_.Put({p, std::move(text)});
|
2018-03-19 19:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Message &Prescanner::Complain(Message &&message) {
|
2018-03-23 23:14:52 +01:00
|
|
|
return messages_.Put(std::move(message));
|
2018-02-12 23:43:16 +01:00
|
|
|
}
|
|
|
|
|
2018-03-19 19:48:49 +01:00
|
|
|
Message &Prescanner::Complain(MessageFixedText text, Provenance p) {
|
2018-03-23 23:14:52 +01:00
|
|
|
return messages_.Put({p, text});
|
2018-03-19 19:48:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Message &Prescanner::Complain(MessageFormattedText &&text, Provenance p) {
|
2018-03-23 23:14:52 +01:00
|
|
|
return messages_.Put({p, std::move(text)});
|
2018-02-21 21:12:52 +01:00
|
|
|
}
|
|
|
|
|
2018-01-30 20:54:47 +01:00
|
|
|
void Prescanner::NextLine() {
|
|
|
|
void *vstart{static_cast<void *>(const_cast<char *>(lineStart_))};
|
|
|
|
void *v{std::memchr(vstart, '\n', limit_ - lineStart_)};
|
|
|
|
if (v == nullptr) {
|
|
|
|
lineStart_ = limit_;
|
|
|
|
} else {
|
|
|
|
const char *nl{const_cast<const char *>(static_cast<char *>(v))};
|
|
|
|
lineStart_ = nl + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 00:22:26 +01:00
|
|
|
void Prescanner::LabelField(TokenSequence *token) {
|
2018-01-30 20:54:47 +01:00
|
|
|
int outCol{1};
|
2018-02-09 23:04:11 +01:00
|
|
|
for (; *at_ != '\n' && column_ <= 6; ++at_) {
|
2018-01-30 20:54:47 +01:00
|
|
|
if (*at_ == '\t') {
|
2018-01-31 00:22:26 +01:00
|
|
|
++at_;
|
2018-02-09 23:04:11 +01:00
|
|
|
column_ = 7;
|
2018-01-30 20:54:47 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*at_ != ' ' &&
|
2018-02-09 23:04:11 +01:00
|
|
|
(*at_ != '0' || column_ != 6)) { // '0' in column 6 becomes space
|
|
|
|
EmitChar(token, *at_);
|
2018-01-30 20:54:47 +01:00
|
|
|
++outCol;
|
|
|
|
}
|
2018-02-09 23:04:11 +01:00
|
|
|
++column_;
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
2018-01-31 00:22:26 +01:00
|
|
|
if (outCol > 1) {
|
2018-02-09 23:04:11 +01:00
|
|
|
token->CloseToken();
|
2018-01-31 00:22:26 +01:00
|
|
|
}
|
|
|
|
if (outCol < 7) {
|
2018-03-01 01:56:10 +01:00
|
|
|
if (outCol == 1) {
|
|
|
|
token->Put(" ", 6, sixSpaceProvenance_.start());
|
|
|
|
} else {
|
|
|
|
for (; outCol < 7; ++outCol) {
|
|
|
|
token->PutNextTokenChar(' ', spaceProvenance_);
|
|
|
|
}
|
|
|
|
token->CloseToken();
|
2018-01-31 00:22:26 +01:00
|
|
|
}
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Prescanner::NextChar() {
|
2018-02-13 21:24:54 +01:00
|
|
|
CHECK(*at_ != '\n');
|
2018-01-30 20:54:47 +01:00
|
|
|
++at_;
|
2018-02-09 23:04:11 +01:00
|
|
|
++column_;
|
2018-01-30 20:54:47 +01:00
|
|
|
if (inPreprocessorDirective_) {
|
|
|
|
while (*at_ == '/' && at_[1] == '*') {
|
|
|
|
char star{' '}, slash{' '};
|
2018-02-01 21:08:02 +01:00
|
|
|
at_ += 2;
|
2018-02-09 23:04:11 +01:00
|
|
|
column_ += 2;
|
2018-02-01 21:08:02 +01:00
|
|
|
while ((*at_ != '\n' || slash == '\\') && (star != '*' || slash != '/')) {
|
2018-01-30 20:54:47 +01:00
|
|
|
star = slash;
|
2018-02-01 21:08:02 +01:00
|
|
|
slash = *at_++;
|
2018-02-09 23:04:11 +01:00
|
|
|
++column_;
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*at_ == '\\' && at_ + 2 < limit_ && at_[1] == '\n') {
|
|
|
|
BeginSourceLineAndAdvance();
|
|
|
|
}
|
|
|
|
} else {
|
2018-02-09 23:04:11 +01:00
|
|
|
if ((inFixedForm_ && column_ > fixedFormColumnLimit_ &&
|
2018-02-05 23:29:26 +01:00
|
|
|
!tabInCurrentLine_) ||
|
2018-01-30 20:54:47 +01:00
|
|
|
(*at_ == '!' && !inCharLiteral_)) {
|
2018-03-23 23:14:52 +01:00
|
|
|
// Skip remainder of fixed form line due to '!' comment marker or
|
|
|
|
// hitting the right margin.
|
2018-01-30 20:54:47 +01:00
|
|
|
while (*at_ != '\n') {
|
|
|
|
++at_;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*at_ == '\n' || *at_ == '&') {
|
2018-02-16 20:14:11 +01:00
|
|
|
if (inFixedForm_) {
|
|
|
|
if (!FixedFormContinuation()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (!FreeFormContinuation()) {
|
2018-01-30 20:54:47 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*at_ == '\t') {
|
|
|
|
tabInCurrentLine_ = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Prescanner::SkipSpaces() {
|
2018-03-01 01:56:10 +01:00
|
|
|
bool wasInCharLiteral{inCharLiteral_};
|
|
|
|
inCharLiteral_ = false;
|
2018-01-30 20:54:47 +01:00
|
|
|
while (*at_ == ' ' || *at_ == '\t') {
|
|
|
|
NextChar();
|
|
|
|
}
|
2018-03-01 01:56:10 +01:00
|
|
|
inCharLiteral_ = wasInCharLiteral;
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Prescanner::NextToken(TokenSequence *tokens) {
|
2018-02-13 21:24:54 +01:00
|
|
|
CHECK(at_ >= start_ && at_ < limit_);
|
2018-01-30 20:54:47 +01:00
|
|
|
if (inFixedForm_) {
|
|
|
|
SkipSpaces();
|
|
|
|
} else if (*at_ == ' ' || *at_ == '\t') {
|
2018-03-20 18:59:07 +01:00
|
|
|
// Compress white space into a single space character.
|
|
|
|
// Discard white space at the end of a line.
|
2018-03-19 19:48:49 +01:00
|
|
|
const auto theSpace = at_;
|
2018-01-30 20:54:47 +01:00
|
|
|
NextChar();
|
|
|
|
SkipSpaces();
|
|
|
|
if (*at_ != '\n') {
|
2018-03-19 19:48:49 +01:00
|
|
|
tokens->PutNextTokenChar(' ', GetProvenance(theSpace));
|
2018-02-09 23:04:11 +01:00
|
|
|
tokens->CloseToken();
|
2018-01-30 20:54:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*at_ == '\n') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*at_ == '\'' || *at_ == '"') {
|
|
|
|
QuotedCharacterLiteral(tokens);
|
|
|
|
preventHollerith_ = false;
|
2018-02-27 23:02:10 +01:00
|
|
|
} else if (IsDecimalDigit(*at_)) {
|
2018-03-19 19:48:49 +01:00
|
|
|
int n{0}, digits{0};
|
2018-03-01 01:56:10 +01:00
|
|
|
static constexpr int maxHollerith{256 /*lines*/ * (132 - 6 /*columns*/)};
|
2018-01-30 20:54:47 +01:00
|
|
|
do {
|
|
|
|
if (n < maxHollerith) {
|
2018-02-27 23:02:10 +01:00
|
|
|
n = 10 * n + DecimalDigitValue(*at_);
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
2018-03-19 19:48:49 +01:00
|
|
|
++digits;
|
|
|
|
if (inFixedForm_ && !inPreprocessorDirective_) {
|
2018-01-30 20:54:47 +01:00
|
|
|
SkipSpaces();
|
|
|
|
}
|
2018-02-27 23:02:10 +01:00
|
|
|
} while (IsDecimalDigit(*at_));
|
2018-02-05 23:29:26 +01:00
|
|
|
if ((*at_ == 'h' || *at_ == 'H') && n > 0 && n < maxHollerith &&
|
2018-01-30 20:54:47 +01:00
|
|
|
!preventHollerith_) {
|
2018-03-01 01:56:10 +01:00
|
|
|
Hollerith(tokens, n);
|
2018-01-30 20:54:47 +01:00
|
|
|
} else if (*at_ == '.') {
|
2018-02-27 23:02:10 +01:00
|
|
|
while (IsDecimalDigit(EmitCharAndAdvance(tokens, *at_))) {
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
ExponentAndKind(tokens);
|
|
|
|
} else if (ExponentAndKind(tokens)) {
|
2018-03-19 19:48:49 +01:00
|
|
|
} else if (digits == 1 && n == 0 && (*at_ == 'x' || *at_ == 'X') &&
|
|
|
|
inPreprocessorDirective_) {
|
|
|
|
do {
|
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
|
|
|
} while (IsHexadecimalDigit(*at_));
|
2018-02-27 23:02:10 +01:00
|
|
|
} else if (IsLetter(*at_)) {
|
2018-01-30 20:54:47 +01:00
|
|
|
// Handles FORMAT(3I9HHOLLERITH) by skipping over the first I so that
|
|
|
|
// we don't misrecognize I9HOLLERITH as an identifier in the next case.
|
2018-03-17 00:13:49 +01:00
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
preventHollerith_ = false;
|
|
|
|
} else if (*at_ == '.') {
|
2018-02-05 21:48:09 +01:00
|
|
|
char nch{EmitCharAndAdvance(tokens, '.')};
|
2018-02-27 23:02:10 +01:00
|
|
|
if (IsDecimalDigit(nch)) {
|
|
|
|
while (IsDecimalDigit(EmitCharAndAdvance(tokens, *at_))) {
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
ExponentAndKind(tokens);
|
2018-02-05 21:48:09 +01:00
|
|
|
} else if (nch == '.' && EmitCharAndAdvance(tokens, '.') == '.') {
|
|
|
|
EmitCharAndAdvance(tokens, '.'); // variadic macro definition ellipsis
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
preventHollerith_ = false;
|
2018-02-27 23:02:10 +01:00
|
|
|
} else if (IsLegalInIdentifier(*at_)) {
|
2018-03-17 00:13:49 +01:00
|
|
|
while (IsLegalInIdentifier(EmitCharAndAdvance(tokens, *at_))) {
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
if (*at_ == '\'' || *at_ == '"') {
|
|
|
|
QuotedCharacterLiteral(tokens);
|
|
|
|
}
|
|
|
|
preventHollerith_ = false;
|
|
|
|
} else if (*at_ == '*') {
|
|
|
|
if (EmitCharAndAdvance(tokens, '*') == '*') {
|
|
|
|
EmitCharAndAdvance(tokens, '*');
|
|
|
|
} else {
|
|
|
|
preventHollerith_ = true; // ambiguity: CHARACTER*2H
|
|
|
|
}
|
|
|
|
} else {
|
2018-01-31 00:22:26 +01:00
|
|
|
char ch{*at_};
|
|
|
|
if (ch == '(' || ch == '[') {
|
|
|
|
++delimiterNesting_;
|
|
|
|
} else if ((ch == ')' || ch == ']') && delimiterNesting_ > 0) {
|
|
|
|
--delimiterNesting_;
|
|
|
|
}
|
|
|
|
char nch{EmitCharAndAdvance(tokens, ch)};
|
2018-01-30 20:54:47 +01:00
|
|
|
preventHollerith_ = false;
|
2018-02-01 21:08:02 +01:00
|
|
|
if ((nch == '=' &&
|
2018-02-05 23:29:26 +01:00
|
|
|
(ch == '<' || ch == '>' || ch == '/' || ch == '=' || ch == '!')) ||
|
2018-02-01 21:08:02 +01:00
|
|
|
(ch == nch &&
|
2018-02-05 23:29:26 +01:00
|
|
|
(ch == '/' || ch == ':' || ch == '*' || ch == '#' || ch == '&' ||
|
|
|
|
ch == '|' || ch == '<' || ch == '>')) ||
|
2018-01-30 20:54:47 +01:00
|
|
|
(ch == '=' && nch == '>')) {
|
2018-02-01 21:08:02 +01:00
|
|
|
// token comprises two characters
|
2018-01-30 20:54:47 +01:00
|
|
|
EmitCharAndAdvance(tokens, nch);
|
|
|
|
}
|
|
|
|
}
|
2018-02-09 23:04:11 +01:00
|
|
|
tokens->CloseToken();
|
2018-01-30 20:54:47 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Prescanner::ExponentAndKind(TokenSequence *tokens) {
|
2018-03-01 01:56:10 +01:00
|
|
|
char ed = ToLowerCaseLetter(*at_);
|
2018-01-30 20:54:47 +01:00
|
|
|
if (ed != 'e' && ed != 'd') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
EmitCharAndAdvance(tokens, ed);
|
|
|
|
if (*at_ == '+' || *at_ == '-') {
|
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
|
|
|
}
|
2018-02-27 23:02:10 +01:00
|
|
|
while (IsDecimalDigit(*at_)) {
|
2018-01-30 20:54:47 +01:00
|
|
|
EmitCharAndAdvance(tokens, *at_);
|
|
|
|
}
|
|
|
|
if (*at_ == '_') {
|
2018-03-17 00:13:49 +01:00
|
|
|
while (IsLegalInIdentifier(EmitCharAndAdvance(tokens, *at_))) {
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Prescanner::QuotedCharacterLiteral(TokenSequence *tokens) {
|
2018-03-01 01:56:10 +01:00
|
|
|
const char *start{at_}, quote{*start};
|
2018-01-30 20:54:47 +01:00
|
|
|
inCharLiteral_ = true;
|
2018-03-01 01:56:10 +01:00
|
|
|
const auto emit = [&](char ch) { EmitChar(tokens, ch); };
|
|
|
|
const auto insert = [&](char ch) { EmitInsertedChar(tokens, ch); };
|
|
|
|
bool escape{false};
|
|
|
|
while (true) {
|
|
|
|
char ch{*at_};
|
|
|
|
escape = !escape && ch == '\\' && enableBackslashEscapesInCharLiterals_;
|
|
|
|
EmitQuotedChar(
|
|
|
|
ch, emit, insert, false, !enableBackslashEscapesInCharLiterals_);
|
2018-02-16 20:14:11 +01:00
|
|
|
while (PadOutCharacterLiteral(tokens)) {
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
2018-03-01 01:56:10 +01:00
|
|
|
if (*at_ == '\n') {
|
2018-03-17 00:58:35 +01:00
|
|
|
if (!inPreprocessorDirective_) {
|
2018-03-19 19:48:49 +01:00
|
|
|
Error("incomplete character literal"_en_US, GetProvenance(start));
|
2018-03-17 00:58:35 +01:00
|
|
|
}
|
2018-03-01 01:56:10 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
NextChar();
|
|
|
|
if (*at_ == quote && !escape) {
|
2018-01-30 20:54:47 +01:00
|
|
|
// A doubled quote mark becomes a single instance of the quote character
|
2018-03-01 01:56:10 +01:00
|
|
|
// in the literal (later). There can be spaces between the quotes in
|
|
|
|
// fixed form source.
|
2018-01-30 20:54:47 +01:00
|
|
|
EmitCharAndAdvance(tokens, quote);
|
2018-03-19 19:48:49 +01:00
|
|
|
if (inFixedForm_ && !inPreprocessorDirective_) {
|
2018-01-30 20:54:47 +01:00
|
|
|
SkipSpaces();
|
|
|
|
}
|
|
|
|
if (*at_ != quote) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2018-03-01 01:56:10 +01:00
|
|
|
}
|
2018-01-30 20:54:47 +01:00
|
|
|
inCharLiteral_ = false;
|
|
|
|
}
|
|
|
|
|
2018-03-01 01:56:10 +01:00
|
|
|
void Prescanner::Hollerith(TokenSequence *tokens, int count) {
|
|
|
|
inCharLiteral_ = true;
|
|
|
|
EmitChar(tokens, 'H');
|
|
|
|
const char *start{at_};
|
|
|
|
while (count-- > 0) {
|
|
|
|
if (PadOutCharacterLiteral(tokens)) {
|
|
|
|
} else if (*at_ != '\n') {
|
|
|
|
NextChar();
|
|
|
|
EmitChar(tokens, *at_);
|
|
|
|
// Multi-byte character encodings should count as single characters.
|
|
|
|
int bytes{1};
|
|
|
|
if (encoding_ == Encoding::EUC_JP) {
|
|
|
|
if (std::optional<int> chBytes{EUC_JPCharacterBytes(at_)}) {
|
|
|
|
bytes = *chBytes;
|
|
|
|
}
|
|
|
|
} else if (encoding_ == Encoding::UTF8) {
|
|
|
|
if (std::optional<int> chBytes{UTF8CharacterBytes(at_)}) {
|
|
|
|
bytes = *chBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (bytes-- > 1) {
|
|
|
|
EmitChar(tokens, *++at_);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*at_ == '\n') {
|
2018-03-17 00:58:35 +01:00
|
|
|
if (!inPreprocessorDirective_) {
|
2018-03-19 19:48:49 +01:00
|
|
|
Error("incomplete Hollerith literal"_en_US, GetProvenance(start));
|
2018-03-17 00:58:35 +01:00
|
|
|
}
|
2018-03-01 01:56:10 +01:00
|
|
|
} else {
|
|
|
|
NextChar();
|
|
|
|
}
|
|
|
|
inCharLiteral_ = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// In fixed form, source card images must be processed as if they were at
|
|
|
|
// least 72 columns wide, at least in character literal contexts.
|
2018-02-16 20:14:11 +01:00
|
|
|
bool Prescanner::PadOutCharacterLiteral(TokenSequence *tokens) {
|
2018-03-01 01:56:10 +01:00
|
|
|
while (inFixedForm_ && !tabInCurrentLine_ && at_[1] == '\n') {
|
|
|
|
if (column_ < fixedFormColumnLimit_) {
|
|
|
|
tokens->PutNextTokenChar(' ', spaceProvenance_);
|
|
|
|
++column_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (!FixedFormContinuation() || tabInCurrentLine_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
CHECK(column_ == 7);
|
|
|
|
--at_; // point to column 6 of continuation line
|
|
|
|
column_ = 6;
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
bool Prescanner::IsFixedFormCommentLine(const char *start) const {
|
2018-01-30 20:54:47 +01:00
|
|
|
const char *p{start};
|
|
|
|
char ch{*p};
|
|
|
|
if (ch == '*' || ch == 'C' || ch == 'c' ||
|
|
|
|
ch == '%' || // VAX %list, %eject, &c.
|
|
|
|
((ch == 'D' || ch == 'd') && !enableOldDebugLines_)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
bool anyTabs{false};
|
|
|
|
while (true) {
|
|
|
|
ch = *p;
|
|
|
|
if (ch == ' ') {
|
|
|
|
++p;
|
|
|
|
} else if (ch == '\t') {
|
|
|
|
anyTabs = true;
|
|
|
|
++p;
|
|
|
|
} else if (ch == '0' && !anyTabs && p == start + 5) {
|
|
|
|
++p; // 0 in column 6 must treated as a space
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!anyTabs && p >= start + fixedFormColumnLimit_) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (*p == '!' && !inCharLiteral_ && (anyTabs || p != start + 5)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return *p == '\n';
|
|
|
|
}
|
|
|
|
|
2018-03-20 18:59:07 +01:00
|
|
|
bool Prescanner::IsFreeFormComment(const char *p) const {
|
2018-01-30 20:54:47 +01:00
|
|
|
while (*p == ' ' || *p == '\t') {
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
return *p == '!' || *p == '\n';
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:14:52 +01:00
|
|
|
std::optional<std::size_t> Prescanner::IsIncludeLine(const char *start) const {
|
|
|
|
const char *p{start};
|
2018-02-13 23:22:08 +01:00
|
|
|
while (*p == ' ' || *p == '\t') {
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
for (char ch : "include"s) {
|
2018-03-01 01:56:10 +01:00
|
|
|
if (ToLowerCaseLetter(*p++) != ch) {
|
2018-03-23 23:14:52 +01:00
|
|
|
return {};
|
2018-02-13 23:22:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
while (*p == ' ' || *p == '\t') {
|
|
|
|
++p;
|
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
if (*p == '"' || *p == '\'') {
|
|
|
|
return {p - start};
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Prescanner::FortranInclude(const char *firstQuote) {
|
|
|
|
const char *p{firstQuote};
|
|
|
|
while (*p != '"' && *p != '\'') {
|
|
|
|
++p;
|
2018-02-13 23:22:08 +01:00
|
|
|
}
|
|
|
|
char quote{*p};
|
|
|
|
std::string path;
|
|
|
|
for (++p; *p != '\n'; ++p) {
|
|
|
|
if (*p == quote) {
|
|
|
|
if (p[1] != quote) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
path += *p;
|
|
|
|
}
|
|
|
|
if (*p != quote) {
|
2018-03-19 19:48:49 +01:00
|
|
|
Error("malformed path name string"_en_US, GetProvenance(p));
|
2018-02-13 23:22:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (++p; *p == ' ' || *p == '\t'; ++p) {
|
|
|
|
}
|
|
|
|
if (*p != '\n' && *p != '!') {
|
2018-03-19 19:48:49 +01:00
|
|
|
Complain("excess characters after path name"_en_US, GetProvenance(p));
|
2018-02-13 23:22:08 +01:00
|
|
|
}
|
|
|
|
std::stringstream error;
|
2018-03-23 23:14:52 +01:00
|
|
|
Provenance provenance{GetProvenance(lineStart_)};
|
|
|
|
AllSources &allSources{cooked_.allSources()};
|
|
|
|
const SourceFile *currentFile{allSources.GetSourceFile(provenance)};
|
2018-02-14 00:24:43 +01:00
|
|
|
if (currentFile != nullptr) {
|
2018-03-23 23:14:52 +01:00
|
|
|
allSources.PushSearchPathDirectory(DirectoryName(currentFile->path()));
|
2018-02-14 00:24:43 +01:00
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
const SourceFile *included{allSources.Open(path, &error)};
|
2018-02-14 00:24:43 +01:00
|
|
|
if (currentFile != nullptr) {
|
2018-03-23 23:14:52 +01:00
|
|
|
allSources.PopSearchPathDirectory();
|
2018-02-14 00:24:43 +01:00
|
|
|
}
|
2018-02-13 23:22:08 +01:00
|
|
|
if (included == nullptr) {
|
2018-03-19 19:48:49 +01:00
|
|
|
Error(MessageFormattedText("INCLUDE: %s"_en_US, error.str().data()),
|
|
|
|
provenance);
|
2018-02-13 23:22:08 +01:00
|
|
|
return true;
|
|
|
|
}
|
2018-03-20 18:59:07 +01:00
|
|
|
if (included->bytes() == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
ProvenanceRange includeLineRange{
|
2018-03-23 23:14:52 +01:00
|
|
|
provenance, static_cast<std::size_t>(p - lineStart_)};
|
2018-02-13 23:22:08 +01:00
|
|
|
ProvenanceRange fileRange{
|
2018-03-23 23:14:52 +01:00
|
|
|
allSources.AddIncludedFile(*included, includeLineRange)};
|
2018-02-13 23:22:08 +01:00
|
|
|
anyFatalErrors_ |= !Prescanner{*this}.Prescan(fileRange);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-03-17 00:58:35 +01:00
|
|
|
bool Prescanner::IsPreprocessorDirectiveLine(const char *start) const {
|
2018-01-30 20:54:47 +01:00
|
|
|
const char *p{start};
|
|
|
|
for (; *p == ' '; ++p) {
|
|
|
|
}
|
|
|
|
if (*p == '#') {
|
|
|
|
return !inFixedForm_ || p != start + 5;
|
|
|
|
}
|
|
|
|
for (; *p == ' ' || *p == '\t'; ++p) {
|
|
|
|
}
|
|
|
|
return *p == '#';
|
|
|
|
}
|
|
|
|
|
2018-03-17 00:58:35 +01:00
|
|
|
bool Prescanner::IsNextLinePreprocessorDirective() const {
|
|
|
|
return IsPreprocessorDirectiveLine(lineStart_);
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:14:52 +01:00
|
|
|
void Prescanner::SkipCommentLines() {
|
2018-01-31 00:22:26 +01:00
|
|
|
while (lineStart_ < limit_) {
|
2018-03-23 23:14:52 +01:00
|
|
|
LineClassification line{ClassifyLine(lineStart_)};
|
|
|
|
if (line.kind != LineClassification::Kind::Comment) {
|
2018-01-31 00:22:26 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-03-20 18:59:07 +01:00
|
|
|
NextLine();
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *Prescanner::FixedFormContinuationLine() {
|
|
|
|
const char *p{lineStart_};
|
2018-02-28 01:33:10 +01:00
|
|
|
if (p >= limit_) {
|
2018-01-30 20:54:47 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
tabInCurrentLine_ = false;
|
2018-03-23 23:14:52 +01:00
|
|
|
char col1{*p};
|
|
|
|
if (directiveSentinel_ != nullptr) {
|
|
|
|
// Must be a continued compiler directive.
|
|
|
|
if (col1 != '!' && col1 != '*' && col1 != 'c' && col1 != 'C') {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
int j{1};
|
|
|
|
for (; j < 5; ++j) {
|
|
|
|
char ch{directiveSentinel_[j - 1]};
|
|
|
|
if (ch == '\0') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ch != ToLowerCaseLetter(p[j])) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; j < 5; ++j) {
|
|
|
|
if (p[j] != ' ') {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char col6{p[5]};
|
|
|
|
if (col6 != '\n' && col6 != '\t' && col6 != ' ' && col6 != '0') {
|
|
|
|
return p + 6;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Normal case: not in a compiler directive.
|
2018-01-30 20:54:47 +01:00
|
|
|
if (*p == '&') {
|
2018-03-01 01:56:10 +01:00
|
|
|
return p + 1; // extension; TODO: emit warning with -Mstandard
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
if (*p == '\t' && p[1] >= '1' && p[1] <= '9') {
|
|
|
|
tabInCurrentLine_ = true;
|
|
|
|
return p + 2; // VAX extension
|
|
|
|
}
|
2018-02-05 23:29:26 +01:00
|
|
|
if (p[0] == ' ' && p[1] == ' ' && p[2] == ' ' && p[3] == ' ' && p[4] == ' ') {
|
2018-01-30 20:54:47 +01:00
|
|
|
char col6{p[5]};
|
|
|
|
if (col6 != '\n' && col6 != '\t' && col6 != ' ' && col6 != '0') {
|
|
|
|
return p + 6;
|
|
|
|
}
|
|
|
|
}
|
2018-01-31 00:22:26 +01:00
|
|
|
if (delimiterNesting_ > 0) {
|
|
|
|
return p;
|
|
|
|
}
|
2018-01-30 20:54:47 +01:00
|
|
|
return nullptr; // not a continuation line
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Prescanner::FixedFormContinuation() {
|
2018-02-28 01:33:10 +01:00
|
|
|
// N.B. We accept '&' as a continuation indicator (even) in fixed form.
|
2018-03-23 23:14:52 +01:00
|
|
|
if (*at_ == '&' && inCharLiteral_) {
|
2018-02-28 01:33:10 +01:00
|
|
|
return false;
|
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
SkipCommentLines();
|
2018-01-30 20:54:47 +01:00
|
|
|
const char *cont{FixedFormContinuationLine()};
|
|
|
|
if (cont == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-02-01 21:08:02 +01:00
|
|
|
BeginSourceLine(cont);
|
2018-02-09 23:04:11 +01:00
|
|
|
column_ = 7;
|
2018-01-30 20:54:47 +01:00
|
|
|
NextLine();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Prescanner::FreeFormContinuation() {
|
|
|
|
const char *p{at_};
|
|
|
|
bool ampersand{*p == '&'};
|
|
|
|
if (ampersand) {
|
|
|
|
for (++p; *p == ' ' || *p == '\t'; ++p) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*p != '\n' && (inCharLiteral_ || *p != '!')) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
SkipCommentLines();
|
2018-01-30 20:54:47 +01:00
|
|
|
p = lineStart_;
|
|
|
|
if (p >= limit_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (; *p == ' ' || *p == '\t'; ++p) {
|
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
if (directiveSentinel_ != nullptr) {
|
|
|
|
// Look for a continued compiler directive.
|
|
|
|
if (*p++ != '!') {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (const char *s{directiveSentinel_}; *s != '\0'; ++p, ++s) {
|
|
|
|
if (*s != ToLowerCaseLetter(*p)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; *p == ' ' || *p == '\t'; ++p) {
|
|
|
|
}
|
|
|
|
if (*p == '&') {
|
|
|
|
++p;
|
|
|
|
} else if (!ampersand) {
|
|
|
|
return false;
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
|
|
|
} else {
|
2018-03-23 23:14:52 +01:00
|
|
|
// Normal case (not a compiler directive)
|
|
|
|
if (*p == '&') {
|
|
|
|
++p;
|
|
|
|
} else if (ampersand || delimiterNesting_ > 0) {
|
|
|
|
if (p > lineStart_) {
|
|
|
|
--p;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return false; // not a continuation
|
|
|
|
}
|
2018-01-30 20:54:47 +01:00
|
|
|
}
|
2018-01-31 00:22:26 +01:00
|
|
|
at_ = p;
|
|
|
|
tabInCurrentLine_ = false;
|
2018-01-30 20:54:47 +01:00
|
|
|
NextLine();
|
|
|
|
return true;
|
|
|
|
}
|
2018-03-20 18:59:07 +01:00
|
|
|
|
2018-03-23 23:14:52 +01:00
|
|
|
std::optional<Prescanner::LineClassification>
|
|
|
|
Prescanner::IsFixedFormCompilerDirectiveLine(const char *start) const {
|
2018-03-20 18:59:07 +01:00
|
|
|
const char *p{start};
|
2018-03-23 23:14:52 +01:00
|
|
|
char col1{*p};
|
|
|
|
if (col1 != '*' && col1 != 'C' && col1 != 'c' && col1 != '!') {
|
|
|
|
return {};
|
2018-03-20 18:59:07 +01:00
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
char sentinel[5], *sp{sentinel};
|
2018-03-20 18:59:07 +01:00
|
|
|
for (int col{2}; col < 6; ++col) {
|
|
|
|
char ch{*++p};
|
|
|
|
if (ch == '\n' || ch == '\t') {
|
2018-03-23 23:14:52 +01:00
|
|
|
return {};
|
2018-03-20 18:59:07 +01:00
|
|
|
}
|
|
|
|
if (ch != ' ') {
|
|
|
|
*sp++ = ToLowerCaseLetter(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*sp = '\0';
|
2018-03-23 23:14:52 +01:00
|
|
|
if (const char *sp{IsCompilerDirectiveSentinel(sentinel)}) {
|
|
|
|
return {
|
|
|
|
LineClassification{LineClassification::Kind::CompilerDirective, 6, sp}};
|
|
|
|
}
|
|
|
|
return {};
|
2018-03-20 18:59:07 +01:00
|
|
|
}
|
|
|
|
|
2018-03-23 23:14:52 +01:00
|
|
|
std::optional<Prescanner::LineClassification>
|
|
|
|
Prescanner::IsFreeFormCompilerDirectiveLine(const char *start) const {
|
|
|
|
char sentinel[8];
|
2018-03-20 18:59:07 +01:00
|
|
|
const char *p{start};
|
|
|
|
while (*p == ' ' || *p == '\t') {
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
if (*p++ != '!') {
|
2018-03-23 23:14:52 +01:00
|
|
|
return {};
|
2018-03-20 18:59:07 +01:00
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
for (std::size_t j{0}; j + 1 < sizeof sentinel; ++p, ++j) {
|
|
|
|
if (*p == '\n') {
|
2018-03-20 18:59:07 +01:00
|
|
|
break;
|
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
if (*p == ' ' || *p == '\t' || *p == '&') {
|
2018-03-20 18:59:07 +01:00
|
|
|
if (j == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sentinel[j] = '\0';
|
2018-03-23 23:14:52 +01:00
|
|
|
for (++p; *p == ' ' || *p == '\t'; ++p) {
|
|
|
|
}
|
|
|
|
if (*p == '!') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (const char *sp{IsCompilerDirectiveSentinel(sentinel)}) {
|
|
|
|
std::size_t offset = p - start;
|
|
|
|
return {LineClassification{
|
|
|
|
LineClassification::Kind::CompilerDirective, offset, sp}};
|
|
|
|
}
|
|
|
|
break;
|
2018-03-20 18:59:07 +01:00
|
|
|
}
|
|
|
|
sentinel[j] = ToLowerCaseLetter(*p);
|
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
return {};
|
2018-03-20 18:59:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Prescanner &Prescanner::AddCompilerDirectiveSentinel(const std::string &dir) {
|
|
|
|
std::uint64_t packed{0};
|
|
|
|
for (char ch : dir) {
|
|
|
|
packed = (packed << 8) | (ToLowerCaseLetter(ch) & 0xff);
|
|
|
|
}
|
|
|
|
compilerDirectiveBloomFilter_.set(packed % prime1);
|
|
|
|
compilerDirectiveBloomFilter_.set(packed % prime2);
|
|
|
|
compilerDirectiveSentinels_.insert(dir);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2018-03-23 23:14:52 +01:00
|
|
|
const char *Prescanner::IsCompilerDirectiveSentinel(const char *s) const {
|
2018-03-20 18:59:07 +01:00
|
|
|
std::uint64_t packed{0};
|
|
|
|
std::size_t n{0};
|
|
|
|
for (; s[n] != '\0'; ++n) {
|
|
|
|
packed = (packed << 8) | (s[n] & 0xff);
|
|
|
|
}
|
2018-03-23 23:14:52 +01:00
|
|
|
if (n == 0 || !compilerDirectiveBloomFilter_.test(packed % prime1) ||
|
|
|
|
!compilerDirectiveBloomFilter_.test(packed % prime2)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
const auto iter = compilerDirectiveSentinels_.find(std::string(s, n));
|
|
|
|
return iter == compilerDirectiveSentinels_.end() ? nullptr : iter->data();
|
|
|
|
}
|
|
|
|
|
|
|
|
Prescanner::LineClassification Prescanner::ClassifyLine(
|
|
|
|
const char *start) const {
|
|
|
|
if (inFixedForm_) {
|
|
|
|
if (std::optional<LineClassification> lc{
|
|
|
|
IsFixedFormCompilerDirectiveLine(start)}) {
|
|
|
|
return std::move(*lc);
|
|
|
|
}
|
|
|
|
if (IsFixedFormCommentLine(start)) {
|
|
|
|
return {LineClassification::Kind::Comment};
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (std::optional<LineClassification> lc{
|
|
|
|
IsFreeFormCompilerDirectiveLine(start)}) {
|
|
|
|
return std::move(*lc);
|
|
|
|
}
|
|
|
|
if (IsFreeFormComment(start)) {
|
|
|
|
return {LineClassification::Kind::Comment};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (std::optional<std::size_t> quoteOffset{IsIncludeLine(start)}) {
|
|
|
|
return {LineClassification::Kind::Include, *quoteOffset};
|
|
|
|
}
|
|
|
|
if (IsPreprocessorDirectiveLine(start)) {
|
|
|
|
return {LineClassification::Kind::PreprocessorDirective};
|
|
|
|
}
|
|
|
|
return {LineClassification::Kind::Source};
|
|
|
|
}
|
|
|
|
|
|
|
|
void Prescanner::SourceFormChange(std::string &&dir) {
|
|
|
|
if (dir == "!dir$ free") {
|
|
|
|
inFixedForm_ = false;
|
|
|
|
} else if (dir == "!dir$ fixed") {
|
|
|
|
inFixedForm_ = true;
|
|
|
|
}
|
2018-03-20 18:59:07 +01:00
|
|
|
}
|
2018-02-07 21:04:42 +01:00
|
|
|
} // namespace parser
|
2018-01-30 20:54:47 +01:00
|
|
|
} // namespace Fortran
|