From 7819d0e80af36193384cb44ac99451f9939ad191 Mon Sep 17 00:00:00 2001 From: Rintaro Ishizaki Date: Thu, 1 Dec 2016 13:21:38 +0900 Subject: [PATCH] [Lexer] Disallow '$' as a start of identifier, special handle '`$`' Allowing 'let `$0` = 1', introduced in 6accc598 was not intentional. --- lib/Parse/Lexer.cpp | 10 +++++++++- test/Parse/dollar_identifier.swift | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/Parse/Lexer.cpp b/lib/Parse/Lexer.cpp index d0f77000461aa..eb8cd35e4f38b 100644 --- a/lib/Parse/Lexer.cpp +++ b/lib/Parse/Lexer.cpp @@ -472,7 +472,7 @@ static bool isValidIdentifierContinuationCodePoint(uint32_t c) { static bool isValidIdentifierStartCodePoint(uint32_t c) { if (!isValidIdentifierContinuationCodePoint(c)) return false; - if (c < 0x80 && isDigit(c)) + if (c < 0x80 && (isDigit(c) || c == '$')) return false; // N1518: Recommendations for extended identifier characters for C and C++ @@ -1385,6 +1385,14 @@ void Lexer::lexEscapedIdentifier() { } } + // Special case; allow '`$`'. + if (Quote[1] == '$' && Quote[2] == '`') { + CurPtr = Quote + 3; + formToken(tok::identifier, Quote); + NextToken.setEscapedIdentifier(true); + return; + } + // The backtick is punctuation. CurPtr = IdentifierStart; formToken(tok::backtick, Quote); diff --git a/test/Parse/dollar_identifier.swift b/test/Parse/dollar_identifier.swift index 73d2594402e3b..f72adb39b9b08 100644 --- a/test/Parse/dollar_identifier.swift +++ b/test/Parse/dollar_identifier.swift @@ -54,3 +54,10 @@ func escapedDollarFunc() { func `$`(`$`: Int) {} // no error `$`(`$`: 25) // no error } + +func escapedDollarAnd() { + // FIXME: Bad diagnostics. + `$0` = 1 // expected-error {{expected expression}} + `$$` = 2 // expected-error {{expected numeric value following '$'}} + `$abc` = 3 // expected-error {{expected numeric value following '$'}} +}