Skip to content

Commit f12c151

Browse files
committed
[Lexer] Improve lexing of BOM trivia
Simplify lexing of BOM trivia, eliminating the need to manually construct the trivia StringRef.
1 parent a2996b7 commit f12c151

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

Diff for: include/swift/Parse/Lexer.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,13 @@ class Lexer {
553553
void lexOperatorIdentifier();
554554
void lexHexNumber();
555555
void lexNumber();
556-
StringRef lexTrivia(bool IsForTrailingTrivia);
556+
557+
/// Skip over trivia and return characters that were skipped over in a \c
558+
/// StringRef. \p AllTriviaStart determines the start of the trivia. In nearly
559+
/// all cases, this should be \c CurPtr. If other trivia has already been
560+
/// skipped over (like a BOM), this can be used to point to the start of the
561+
/// BOM. The returned \c StringRef will always start at \p AllTriviaStart.
562+
StringRef lexTrivia(bool IsForTrailingTrivia, const char *AllTriviaStart);
557563
static unsigned lexUnicodeEscape(const char *&CurPtr, Lexer *Diags);
558564

559565
unsigned lexCharacter(const char *&CurPtr, char StopQuote,

Diff for: lib/Parse/Lexer.cpp

+5-8
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ void Lexer::formToken(tok Kind, const char *TokStart) {
291291
StringRef TokenText { TokStart, static_cast<size_t>(CurPtr - TokStart) };
292292

293293
if (TriviaRetention == TriviaRetentionMode::WithTrivia && Kind != tok::eof) {
294-
TrailingTrivia = lexTrivia(/*IsForTrailingTrivia=*/true);
294+
TrailingTrivia = lexTrivia(/*IsForTrailingTrivia=*/true, CurPtr);
295295
} else {
296296
TrailingTrivia = StringRef();
297297
}
@@ -2344,11 +2344,8 @@ void Lexer::lexImpl() {
23442344
NextToken.setAtStartOfLine(false);
23452345
}
23462346

2347-
// Advance CurPtr to the end of the first trivia in the source file and form
2348-
// the leading trivia including the BOM
2349-
lexTrivia(/*IsForTrailingTrivia=*/false);
2350-
LeadingTrivia = StringRef(LeadingTriviaStart, CurPtr - LeadingTriviaStart);
2351-
2347+
LeadingTrivia = lexTrivia(/*IsForTrailingTrivia=*/false, LeadingTriviaStart);
2348+
23522349
// Remember the start of the token so we can form the text range.
23532350
const char *TokStart = CurPtr;
23542351

@@ -2525,8 +2522,8 @@ Token Lexer::getTokenAtLocation(const SourceManager &SM, SourceLoc Loc,
25252522
return L.peekNextToken();
25262523
}
25272524

2528-
StringRef Lexer::lexTrivia(bool IsForTrailingTrivia) {
2529-
const char *AllTriviaStart = CurPtr;
2525+
StringRef Lexer::lexTrivia(bool IsForTrailingTrivia,
2526+
const char *AllTriviaStart) {
25302527
CommentStart = nullptr;
25312528

25322529
Restart:

0 commit comments

Comments
 (0)