|
30 | 30 | // FIXME: Figure out if this can be migrated to LLVM. |
31 | 31 | #include "clang/Basic/CharInfo.h" |
32 | 32 |
|
| 33 | +// Regex parser delivered via libSwift |
| 34 | +#include "swift/Parse/ExperimentalRegexBridging.h" |
| 35 | +static ParseRegexStrawperson parseRegexStrawperson = nullptr; |
| 36 | +void Parser_registerParseRegexStrawperson(ParseRegexStrawperson fn) { |
| 37 | + parseRegexStrawperson = fn; |
| 38 | +} |
| 39 | + |
33 | 40 | #include <limits> |
34 | 41 |
|
35 | 42 | using namespace swift; |
@@ -1788,13 +1795,27 @@ static void validateMultilineIndents(const Token &Str, |
1788 | 1795 |
|
1789 | 1796 | /// Emit diagnostics for single-quote string and suggest replacement |
1790 | 1797 | /// with double-quoted equivalent. |
1791 | | -static void diagnoseSingleQuoteStringLiteral(const char *TokStart, |
1792 | | - const char *TokEnd, |
1793 | | - DiagnosticEngine *D) { |
| 1798 | +/// |
| 1799 | +/// Or, if we're in experimental regex mode, we will emit a custom |
| 1800 | +/// error message instead, determined by the Swift library. |
| 1801 | +void Lexer::diagnoseSingleQuoteStringLiteral(const char *TokStart, |
| 1802 | + const char *TokEnd) { |
1794 | 1803 | assert(*TokStart == '\'' && TokEnd[-1] == '\''); |
1795 | | - if (!D) |
| 1804 | + if (!Diags) // or assert? |
1796 | 1805 | return; |
1797 | 1806 |
|
| 1807 | + auto startLoc = Lexer::getSourceLoc(TokStart); |
| 1808 | + auto endLoc = Lexer::getSourceLoc(TokEnd); |
| 1809 | + |
| 1810 | + if (LangOpts.EnableExperimentalRegex) { |
| 1811 | + if (parseRegexStrawperson) { |
| 1812 | + auto copy = std::string(TokStart, TokEnd-TokStart); |
| 1813 | + auto msg = parseRegexStrawperson(copy.c_str()); |
| 1814 | + assert(msg != nullptr); |
| 1815 | + Diags->diagnose(startLoc, diag::lex_experimental_regex_strawperson, msg); |
| 1816 | + } |
| 1817 | + } |
| 1818 | + |
1798 | 1819 | SmallString<32> replacement; |
1799 | 1820 | replacement.push_back('"'); |
1800 | 1821 | const char *Ptr = TokStart + 1; |
@@ -1826,9 +1847,8 @@ static void diagnoseSingleQuoteStringLiteral(const char *TokStart, |
1826 | 1847 | replacement.append(OutputPtr, Ptr - 1); |
1827 | 1848 | replacement.push_back('"'); |
1828 | 1849 |
|
1829 | | - D->diagnose(Lexer::getSourceLoc(TokStart), diag::lex_single_quote_string) |
1830 | | - .fixItReplaceChars(Lexer::getSourceLoc(TokStart), |
1831 | | - Lexer::getSourceLoc(TokEnd), replacement); |
| 1850 | + Diags->diagnose(startLoc, diag::lex_single_quote_string) |
| 1851 | + .fixItReplaceChars(startLoc, endLoc, replacement); |
1832 | 1852 | } |
1833 | 1853 |
|
1834 | 1854 | /// lexStringLiteral: |
@@ -1895,7 +1915,7 @@ void Lexer::lexStringLiteral(unsigned CustomDelimiterLen) { |
1895 | 1915 | if (QuoteChar == '\'') { |
1896 | 1916 | assert(!IsMultilineString && CustomDelimiterLen == 0 && |
1897 | 1917 | "Single quoted string cannot have custom delimitor, nor multiline"); |
1898 | | - diagnoseSingleQuoteStringLiteral(TokStart, CurPtr, Diags); |
| 1918 | + diagnoseSingleQuoteStringLiteral(TokStart, CurPtr); |
1899 | 1919 | } |
1900 | 1920 |
|
1901 | 1921 | if (wasErroneous) |
|
0 commit comments