Skip to content

Commit c9fa1b2

Browse files
authored
Merge pull request #5410 from rintaro/lex-hoist-se0037
[Lexer] Trim comments off operator-identifier before tokenize
2 parents 0b258e2 + 1df518c commit c9fa1b2

File tree

2 files changed

+18
-19
lines changed

2 files changed

+18
-19
lines changed

lib/Parse/Lexer.cpp

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,17 @@ void Lexer::lexOperatorIdentifier() {
697697
break;
698698
} while (advanceIfValidContinuationOfOperator(CurPtr, BufferEnd));
699699

700+
if (CurPtr-TokStart > 2) {
701+
// If there is a "//" or "/*" in the middle of an identifier token,
702+
// it starts a comment.
703+
for (auto Ptr = TokStart+1; Ptr != CurPtr-1; ++Ptr) {
704+
if (Ptr[0] == '/' && (Ptr[1] == '/' || Ptr[1] == '*')) {
705+
CurPtr = Ptr;
706+
break;
707+
}
708+
}
709+
}
710+
700711
// Decide between the binary, prefix, and postfix cases.
701712
// It's binary if either both sides are bound or both sides are not bound.
702713
// Otherwise, it's postfix if left-bound and prefix if right-bound.
@@ -771,27 +782,9 @@ void Lexer::lexOperatorIdentifier() {
771782
return formToken(tok::unknown, TokStart);
772783
}
773784
} else {
774-
// If there is a "//" in the middle of an identifier token, it starts
775-
// a single-line comment.
776-
auto Pos = StringRef(TokStart, CurPtr-TokStart).find("//");
777-
if (Pos != StringRef::npos) {
778-
CurPtr = TokStart+Pos;
779-
// Next token is a comment, which counts as whitespace.
780-
rightBound = false;
781-
}
782-
783-
// If there is a "/*" in the middle of an identifier token, it starts
784-
// a multi-line comment.
785-
Pos = StringRef(TokStart, CurPtr-TokStart).find("/*");
786-
if (Pos != StringRef::npos) {
787-
CurPtr = TokStart+Pos;
788-
// Next token is a comment, which counts as whitespace.
789-
rightBound = false;
790-
}
791-
792785
// Verify there is no "*/" in the middle of the identifier token, we reject
793786
// it as potentially ending a block comment.
794-
Pos = StringRef(TokStart, CurPtr-TokStart).find("*/");
787+
auto Pos = StringRef(TokStart, CurPtr-TokStart).find("*/");
795788
if (Pos != StringRef::npos) {
796789
diagnose(TokStart+Pos, diag::lex_unexpected_block_comment_end);
797790
return formToken(tok::unknown, TokStart);

test/Parse/comment_operator.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,9 @@ func test6() { _ = 1+/* */2 } // expected-error {{'+' is not a p
2727
_ = foo!// this is dangerous
2828
_ = 1 +/**/ 2
2929
_ = 1 +/* hi */2
30+
31+
// Ensure built-in operators are properly tokenized.
32+
_ =/**/2
33+
_/**/= 2
34+
typealias A = () ->/* */()
35+
func test7(x: Int) { _ = x./* desc */ } // expected-error {{expected member name following '.'}}

0 commit comments

Comments
 (0)