Skip to content

Commit d1cadb8

Browse files
committed
Remove Explicit Trivia from Lexer
1 parent 4c162b2 commit d1cadb8

File tree

6 files changed

+24
-520
lines changed

6 files changed

+24
-520
lines changed

include/swift/Parse/Lexer.h

Lines changed: 7 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "swift/Basic/SourceManager.h"
2323
#include "swift/Parse/LexerState.h"
2424
#include "swift/Parse/Token.h"
25-
#include "swift/Parse/ParsedTrivia.h"
2625
#include "llvm/ADT/SmallVector.h"
2726
#include "llvm/Support/SaveAndRestore.h"
2827

@@ -45,11 +44,6 @@ enum class CommentRetentionMode {
4544
ReturnAsTokens,
4645
};
4746

48-
enum class TriviaRetentionMode {
49-
WithoutTrivia,
50-
WithTrivia,
51-
};
52-
5347
enum class HashbangMode : bool {
5448
Disallowed,
5549
Allowed,
@@ -131,21 +125,10 @@ class Lexer {
131125

132126
const CommentRetentionMode RetainComments;
133127

134-
const TriviaRetentionMode TriviaRetention;
135-
136128
/// InSILBody - This is true when we're lexing the body of a SIL declaration
137129
/// in a SIL file. This enables some context-sensitive lexing.
138130
bool InSILBody = false;
139131

140-
/// The current leading trivia for the next token.
141-
///
142-
/// The StringRef points into the source buffer that is currently being lexed.
143-
StringRef LeadingTrivia;
144-
145-
/// The current trailing trivia for the next token.
146-
/// The StringRef points into the source buffer that is currently being lexed.
147-
StringRef TrailingTrivia;
148-
149132
/// The location at which the comment of the next token starts. \c nullptr if
150133
/// the next token doesn't have a comment.
151134
const char *CommentStart;
@@ -166,8 +149,8 @@ class Lexer {
166149
Lexer(const PrincipalTag &, const LangOptions &LangOpts,
167150
const SourceManager &SourceMgr, unsigned BufferID,
168151
DiagnosticEngine *Diags, LexerMode LexMode,
169-
HashbangMode HashbangAllowed, CommentRetentionMode RetainComments,
170-
TriviaRetentionMode TriviaRetention);
152+
HashbangMode HashbangAllowed,
153+
CommentRetentionMode RetainComments);
171154

172155
void initialize(unsigned Offset, unsigned EndOffset);
173156

@@ -202,14 +185,13 @@ class Lexer {
202185
const LangOptions &Options, const SourceManager &SourceMgr,
203186
unsigned BufferID, DiagnosticEngine *Diags, LexerMode LexMode,
204187
HashbangMode HashbangAllowed = HashbangMode::Disallowed,
205-
CommentRetentionMode RetainComments = CommentRetentionMode::None,
206-
TriviaRetentionMode TriviaRetention = TriviaRetentionMode::WithoutTrivia);
188+
CommentRetentionMode RetainComments = CommentRetentionMode::None);
207189

208190
/// Create a lexer that scans a subrange of the source buffer.
209191
Lexer(const LangOptions &Options, const SourceManager &SourceMgr,
210192
unsigned BufferID, DiagnosticEngine *Diags, LexerMode LexMode,
211193
HashbangMode HashbangAllowed, CommentRetentionMode RetainComments,
212-
TriviaRetentionMode TriviaRetention, unsigned Offset,
194+
unsigned Offset,
213195
unsigned EndOffset);
214196

215197
/// Create a sub-lexer that lexes from the same buffer, but scans
@@ -233,15 +215,9 @@ class Lexer {
233215
return LexMode == LexerMode::SwiftInterface;
234216
}
235217

236-
/// Lex a token. If \c TriviaRetentionMode is \c WithTrivia, passed pointers
237-
/// to trivia are populated.
238-
void lex(Token &Result, StringRef &LeadingTriviaResult,
239-
StringRef &TrailingTriviaResult) {
218+
/// Lex a token.
219+
void lex(Token &Result) {
240220
Result = NextToken;
241-
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
242-
LeadingTriviaResult = LeadingTrivia;
243-
TrailingTriviaResult = TrailingTrivia;
244-
}
245221
// Emit any diagnostics recorded for this token.
246222
if (DiagQueue)
247223
DiagQueue->emit();
@@ -250,11 +226,6 @@ class Lexer {
250226
lexImpl();
251227
}
252228

253-
void lex(Token &Result) {
254-
StringRef LeadingTrivia, TrailingTrivia;
255-
lex(Result, LeadingTrivia, TrailingTrivia);
256-
}
257-
258229
/// Reset the lexer's buffer pointer to \p Offset bytes after the buffer
259230
/// start.
260231
void resetToOffset(size_t Offset) {
@@ -304,8 +275,7 @@ class Lexer {
304275
/// Returns the lexer state for the beginning of the given token.
305276
/// After restoring the state, lexer will return this token and continue from
306277
/// there.
307-
State getStateForBeginningOfToken(const Token &Tok,
308-
const StringRef &LeadingTrivia = {}) const {
278+
State getStateForBeginningOfToken(const Token &Tok) const {
309279

310280
// If the token has a comment attached to it, rewind to before the comment,
311281
// not just the start of the token. This ensures that we will re-lex and
@@ -314,11 +284,6 @@ class Lexer {
314284
if (TokStart.isInvalid())
315285
TokStart = Tok.getLoc();
316286
auto S = getStateForBeginningOfTokenLoc(TokStart);
317-
if (TriviaRetention == TriviaRetentionMode::WithTrivia) {
318-
S.LeadingTrivia = LeadingTrivia;
319-
} else {
320-
S.LeadingTrivia = StringRef();
321-
}
322287
return S;
323288
}
324289

@@ -340,10 +305,6 @@ class Lexer {
340305
// Don't re-emit diagnostics from readvancing the lexer.
341306
if (DiagQueue && !enableDiagnostics)
342307
DiagQueue->clear();
343-
344-
// Restore Trivia.
345-
if (TriviaRetention == TriviaRetentionMode::WithTrivia)
346-
LeadingTrivia = S.LeadingTrivia;
347308
}
348309

349310
/// Restore the lexer state to a given state that is located before
@@ -681,13 +642,6 @@ class Lexer {
681642

682643
};
683644

684-
/// A lexer that can lex trivia into its pieces
685-
class TriviaLexer {
686-
public:
687-
/// Decompose the trivia in \p TriviaStr into their pieces.
688-
static ParsedTrivia lexTrivia(StringRef TriviaStr);
689-
};
690-
691645
/// Given an ordered token \param Array , get the iterator pointing to the first
692646
/// token that is not before \param Loc .
693647
template<typename ArrayTy, typename Iterator = typename ArrayTy::iterator>

include/swift/Parse/LexerState.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
#include "llvm/ADT/Optional.h"
2121
#include "swift/Basic/SourceLoc.h"
22-
#include "swift/Parse/ParsedTrivia.h"
2322

2423
namespace swift {
2524
class Lexer;
@@ -39,7 +38,6 @@ class LexerState {
3938
private:
4039
explicit LexerState(SourceLoc Loc) : Loc(Loc) {}
4140
SourceLoc Loc;
42-
StringRef LeadingTrivia;
4341
friend class Lexer;
4442
};
4543

lib/Sema/TypeCheckStmt.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
#include "swift/Parse/LocalContext.h"
4444
#include "swift/Parse/Parser.h"
4545
#include "swift/Sema/IDETypeChecking.h"
46-
#include "swift/Syntax/TokenKinds.h"
4746
#include "llvm/ADT/DenseMap.h"
4847
#include "llvm/ADT/PointerUnion.h"
4948
#include "llvm/ADT/SmallString.h"

unittests/Parse/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
add_swift_unittest(SwiftParseTests
22
BuildConfigTests.cpp
33
LexerTests.cpp
4-
LexerTriviaTests.cpp
54
TokenizerTests.cpp
65
)
76

0 commit comments

Comments
 (0)