1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
|
//===-- FixedForm.cpp - Fortran Fixed-form Parsing Utilities ----*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "flang/Parse/FixedForm.h"
#include "flang/Parse/Parser.h"
namespace flang {
namespace fixedForm {
KeywordFilter::KeywordFilter(tok::TokenKind K1, tok::TokenKind K2,
tok::TokenKind K3) {
SmallArray[0] = K1; SmallArray[1] = K2;
SmallArray[2] = K3;
Keywords = ArrayRef<tok::TokenKind>(SmallArray, K3 == tok::unknown? 2 : 3);
}
KeywordMatcher::KeywordMatcher(ArrayRef<KeywordFilter> Filters) {
for(auto Filter : Filters) {
for(auto Keyword : Filter.getKeywords())
Register(Keyword);
}
}
void KeywordMatcher::operator=(ArrayRef<KeywordFilter> Filters) {
for(auto Filter : Filters) {
for(auto Keyword : Filter.getKeywords())
Register(Keyword);
}
}
void KeywordMatcher::Register(tok::TokenKind Keyword) {
auto Identifier = getTokenName(Keyword);
std::string Name(Identifier);
for (size_t I = 0, E = Name.size(); I != E; ++I)
Name[I] = ::tolower(Name[I]);
Keywords.insert(Name);
}
bool KeywordMatcher::Matches(StringRef Identifier) const {
std::string Name(Identifier);
for (size_t I = 0, E = Name.size(); I != E; ++I)
Name[I] = ::tolower(Name[I]);
return Keywords.find(Name) != Keywords.end();
}
static const tok::TokenKind AmbiguousExecKeywords[] = {
// ASSIGN10TOI
tok::kw_ASSIGN,
// DOI=1,10
tok::kw_DO,
tok::kw_DOWHILE,
// ENDDOconstructname
tok::kw_ENDDO,
// ELSEconstructname
tok::kw_ELSE,
// ELSEIF
tok::kw_ELSEIF,
// ENDIFconstructname
tok::kw_ENDIF,
// ENDSELECTconstructname
tok::kw_ENDSELECT,
// GOTOI
tok::kw_GOTO,
// CALLfoo
tok::kw_CALL,
// STOP1
tok::kw_STOP,
// ENTRYwhat
tok::kw_ENTRY,
// RETURN1
tok::kw_RETURN,
// CYCLE/EXITconstructname
tok::kw_CYCLE,
tok::kw_EXIT,
// PRINTfmt
tok::kw_PRINT,
// READfmt
tok::kw_READ,
tok::kw_DATA
};
AmbiguousExecutableStatements::AmbiguousExecutableStatements()
: KeywordFilter(AmbiguousExecKeywords) {}
static const tok::TokenKind AmbiguousEndKeywords[] = {
// END<...>name
tok::kw_END,
tok::kw_ENDPROGRAM,
tok::kw_ENDSUBROUTINE,
tok::kw_ENDFUNCTION
};
AmbiguousEndStatements::AmbiguousEndStatements()
: KeywordFilter(AmbiguousEndKeywords) {}
static const tok::TokenKind AmbiguousTypeKeywords[] = {
// INTEGERvar
tok::kw_INTEGER,
tok::kw_REAL,
tok::kw_COMPLEX,
tok::kw_DOUBLEPRECISION,
tok::kw_DOUBLECOMPLEX,
tok::kw_LOGICAL,
tok::kw_CHARACTER
};
AmbiguousTypeStatements::AmbiguousTypeStatements()
: KeywordFilter(AmbiguousTypeKeywords) {}
static const tok::TokenKind AmbiguousSpecKeywords[] = {
// IMPLICITREAL
tok::kw_IMPLICIT,
// DIMENSIONI(10)
tok::kw_DIMENSION,
// EXTERNALfoo
tok::kw_EXTERNAL,
// INTRINSICfoo
tok::kw_INTRINSIC,
// COMMONi
tok::kw_COMMON,
// DATAa/1/
tok::kw_DATA,
// SAVEi
tok::kw_SAVE
};
AmbiguousSpecificationStatements::AmbiguousSpecificationStatements()
: KeywordFilter(AmbiguousSpecKeywords) {}
static const tok::TokenKind AmbiguousTopLvlDeclKeywords[] = {
// PROGRAMname
tok::kw_PROGRAM,
tok::kw_SUBROUTINE,
tok::kw_FUNCTION,
// RECURSIVEfunctionFOO
tok::kw_RECURSIVE
};
AmbiguousTopLevelDeclarationStatements::AmbiguousTopLevelDeclarationStatements()
: KeywordFilter(AmbiguousTopLvlDeclKeywords){}
CommonAmbiguities::CommonAmbiguities() {
{
const KeywordFilter Filters[] = {
AmbiguousTypeStatements(),
KeywordFilter(tok::kw_FUNCTION, tok::kw_SUBROUTINE)
};
MatcherForKeywordsAfterRECURSIVE = llvm::makeArrayRef(Filters);
}
{
const KeywordFilter Filters[] = {
AmbiguousExecutableStatements(),
KeywordFilter(tok::kw_THEN)
};
MatcherForKeywordsAfterIF = llvm::makeArrayRef(Filters);
}
{
const KeywordFilter Filters[] = {
AmbiguousTopLevelDeclarationStatements(),
AmbiguousTypeStatements(),
AmbiguousSpecificationStatements(),
AmbiguousExecutableStatements(),
AmbiguousEndStatements()
};
MatcherForTopLevel = llvm::makeArrayRef(Filters);
}
{
const KeywordFilter Filters[] = {
AmbiguousTypeStatements(),
AmbiguousSpecificationStatements(),
AmbiguousExecutableStatements(),
AmbiguousEndStatements()
};
MatcherForSpecStmts = llvm::makeArrayRef(Filters);
}
{
const KeywordFilter Filters[] = {
AmbiguousExecutableStatements(),
AmbiguousEndStatements()
};
MatcherForExecStmts = llvm::makeArrayRef(Filters);
}
}
} // end namespace fixedForm
void Parser::ReLexAmbiguousIdentifier(const fixedForm::KeywordMatcher &Matcher) {
assert(Features.FixedForm);
if(Matcher.Matches(Tok.getIdentifierInfo()->getName()))
return;
NextTok.setKind(tok::unknown);
TheLexer.LexFixedFormIdentifierMatchLongestKeyword(Matcher, Tok);
ClassifyToken(Tok);
}
bool Parser::ExpectAndConsumeFixedFormAmbiguous(tok::TokenKind ExpectedTok, unsigned Diag,
const char *DiagMsg) {
if(Features.FixedForm) {
if(Tok.isAtStartOfStatement())
return ExpectAndConsume(ExpectedTok, Diag, DiagMsg);
ReLexAmbiguousIdentifier(fixedForm::KeywordMatcher(fixedForm::KeywordFilter(ExpectedTok)));
}
return ExpectAndConsume(ExpectedTok, Diag, DiagMsg);
}
void Parser::StartStatementReparse(SourceLocation Where) {
TheLexer.ReLexStatement(Where.isValid()? Where : LocFirstStmtToken);
Tok.startToken();
NextTok.setKind(tok::unknown);
Lex();
}
void Parser::LookForTopLevelStmtKeyword() {
bool AtStartOfStatement = StmtLabel? false : true;
if(Features.FixedForm) {
if(!AtStartOfStatement && Tok.isAtStartOfStatement()) return;
ReLexAmbiguousIdentifier(FixedFormAmbiguities.getMatcherForTopLevelKeywords());
}
}
void Parser::LookForSpecificationStmtKeyword() {
bool AtStartOfStatement = StmtLabel? false : true;
if(Features.FixedForm) {
if(!AtStartOfStatement && Tok.isAtStartOfStatement()) return;
ReLexAmbiguousIdentifier(FixedFormAmbiguities.getMatcherForSpecificationStmtKeywords());
}
}
void Parser::LookForExecutableStmtKeyword(bool AtStartOfStatement) {
if(Features.FixedForm) {
if(!AtStartOfStatement && Tok.isAtStartOfStatement()) return;
ReLexAmbiguousIdentifier(FixedFormAmbiguities.getMatcherForExecutableStmtKeywords());
}
}
} // end namespace flang
|