Skip to content

Commit 54b28b6

Browse files
authored
Merge pull request #15152 from omochi/lex-remove-fields
[Parse] Remove unnecessary Lexer fields
2 parents a5268f8 + 3c8057e commit 54b28b6

File tree

5 files changed

+33
-31
lines changed

5 files changed

+33
-31
lines changed

include/swift/Parse/Lexer.h

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -92,22 +92,6 @@ class Lexer {
9292
/// Pointer to the next not consumed character.
9393
const char *CurPtr;
9494

95-
/// @{
96-
/// Members that are *not* permanent lexer state. The values only make sense
97-
/// during the lexImpl() invocation. These variables are declared as members
98-
/// rather than locals so that we don't have to thread them through to all
99-
/// lexing helpers.
100-
101-
/// Points to the point in the source buffer where we started scanning for
102-
/// the current token. Thus, the range [LastCommentBlockStart, CurPtr)
103-
/// covers all comments and whitespace that we skipped, and the token itself.
104-
const char *LastCommentBlockStart = nullptr;
105-
106-
/// True if we have seen a comment while scanning for the current token.
107-
bool SeenComment = false;
108-
109-
/// @}
110-
11195
Token NextToken;
11296

11397
/// \brief This is true if we're lexing a .sil file instead of a .swift

include/swift/Syntax/Trivia.h.gyb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ public:
170170
}
171171
}
172172

173+
bool isComment() const;
174+
173175
void accumulateAbsolutePosition(AbsolutePosition &Pos) const;
174176

175177
/// Try to compose this and Next to one TriviaPiece.

lib/Parse/Lexer.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,14 @@ void Lexer::formToken(tok Kind, const char *TokStart, bool MultilineString) {
278278
Kind = tok::eof;
279279
}
280280
unsigned CommentLength = 0;
281-
if (RetainComments == CommentRetentionMode::AttachToNextToken && SeenComment)
282-
CommentLength = TokStart - LastCommentBlockStart;
281+
if (RetainComments == CommentRetentionMode::AttachToNextToken) {
282+
auto Iter = llvm::find_if(LeadingTrivia, [](const TriviaPiece &Piece) {
283+
return Piece.isComment();
284+
});
285+
for (auto End = LeadingTrivia.end(); Iter != End; Iter++) {
286+
CommentLength += Iter->getTextLength();
287+
}
288+
}
283289

284290
StringRef TokenText { TokStart, static_cast<size_t>(CurPtr - TokStart) };
285291

@@ -2160,10 +2166,6 @@ void Lexer::lexImpl() {
21602166
NextToken.setAtStartOfLine(false);
21612167
}
21622168

2163-
// Remember where we started so that we can find the comment range.
2164-
LastCommentBlockStart = CurPtr;
2165-
SeenComment = false;
2166-
21672169
lexTrivia(LeadingTrivia, /* IsForTrailingTrivia */ false);
21682170

21692171
// Remember the start of the token so we can form the text range.
@@ -2253,14 +2255,12 @@ void Lexer::lexImpl() {
22532255
case '/':
22542256
if (CurPtr[0] == '/') { // "//"
22552257
skipSlashSlashComment(/*EatNewline=*/true);
2256-
SeenComment = true;
22572258
assert(isKeepingComments() &&
22582259
"Non token comment should be eaten by lexTrivia as LeadingTrivia");
22592260
return formToken(tok::comment, TokStart);
22602261
}
22612262
if (CurPtr[0] == '*') { // "/*"
22622263
skipSlashStarComment();
2263-
SeenComment = true;
22642264
assert(isKeepingComments() &&
22652265
"Non token comment should be eaten by lexTrivia as LeadingTrivia");
22662266
return formToken(tok::comment, TokStart);
@@ -2393,7 +2393,6 @@ void Lexer::lexTrivia(syntax::Trivia &Pieces, bool IsForTrailingTrivia) {
23932393
break;
23942394
} else if (*CurPtr == '/') {
23952395
// '// ...' comment.
2396-
SeenComment = true;
23972396
bool isDocComment = CurPtr[1] == '/';
23982397
skipSlashSlashComment(/*EatNewline=*/false);
23992398
size_t Length = CurPtr - TriviaStart;
@@ -2403,7 +2402,6 @@ void Lexer::lexTrivia(syntax::Trivia &Pieces, bool IsForTrailingTrivia) {
24032402
goto Restart;
24042403
} else if (*CurPtr == '*') {
24052404
// '/* ... */' comment.
2406-
SeenComment = true;
24072405
bool isDocComment = CurPtr[1] == '*';
24082406
skipSlashStarComment();
24092407
size_t Length = CurPtr - TriviaStart;

lib/Syntax/Trivia.cpp.gyb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ void TriviaPiece::dump(llvm::raw_ostream &OS, unsigned Indent) const {
4444
OS << ')';
4545
}
4646

47+
bool TriviaPiece::isComment() const {
48+
switch (Kind) {
49+
% for trivia in TRIVIAS:
50+
case TriviaKind::${trivia.name}:
51+
% if trivia.is_comment:
52+
return true;
53+
% else:
54+
return false;
55+
% end
56+
% end
57+
}
58+
}
59+
4760
void TriviaPiece::accumulateAbsolutePosition(AbsolutePosition &Pos) const {
4861
switch (Kind) {
4962
% for trivia in TRIVIAS:

utils/gyb_syntax_support/Trivia.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
class Trivia(object):
55
def __init__(self, name, comment, characters=[], swift_characters=[],
6-
is_new_line=False):
6+
is_new_line=False, is_comment=False):
77
self.name = name
88
self.comment = comment
99
self.characters = characters
1010
self.lower_name = lowercase_first_word(name)
1111
self.is_new_line = is_new_line
12+
self.is_comment = is_comment
1213

1314
# Swift sometimes doesn't support escaped characters like \f or \v;
1415
# we should allow specifying alternatives explicitly.
@@ -44,14 +45,18 @@ def is_collection(self):
4445
'A backtick \'`\' character, used to escape identifiers.',
4546
characters=['`']),
4647

47-
Trivia('LineComment', 'A developer line comment, starting with \'//\''),
48+
Trivia('LineComment', 'A developer line comment, starting with \'//\'',
49+
is_comment=True),
4850
Trivia('BlockComment',
4951
'A developer block comment, starting with \'/*\' and ending with'
50-
' \'*/\'.'),
52+
' \'*/\'.',
53+
is_comment=True),
5154
Trivia('DocLineComment',
52-
'A documentation line comment, starting with \'///\'.'),
55+
'A documentation line comment, starting with \'///\'.',
56+
is_comment=True),
5357
Trivia('DocBlockComment',
5458
'A documentation block comment, starting with \'/**\' and ending '
55-
'with \'*/\'.'),
59+
'with \'*/\'.',
60+
is_comment=True),
5661
Trivia('GarbageText', 'Any skipped garbage text.'),
5762
]

0 commit comments

Comments
 (0)