Support lexing characters (#5893)

Adapts `StringLiteral` to lex characters. Adds a `CharLiteral` token,
which contains a `CharLiteralValue` which is a straight unicode code
point (suggested by zygoloid).

---------

Co-authored-by: Richard Smith <richard@metafoo.co.uk>
This commit is contained in:
Jon Ross-Perkins
2025-08-01 18:24:03 +00:00
committed by GitHub
co-authored by Richard Smith
parent c707a6deaa
commit cae8aa3adf
14 changed files with 314 additions and 79 deletions
+18 -16
View File
@@ -92,54 +92,56 @@ static void BM_SimpleStringValue(benchmark::State& state, int size,
std::string x(introducer);
x.append(size, 'a');
if (add_escape) {
// Adds a basic escape that forces ComputeValue to generate a new string.
// Adds a basic escape that forces ComputeStringValue to generate a new
// string.
x.append("\\\\");
}
x.append(terminator);
for (auto _ : state) {
StringLiteral::Lex(x)->ComputeValue(
StringLiteral::Lex(x)->ComputeStringValue(
allocator, Diagnostics::NullEmitter<const char*>());
}
}
static void BM_ComputeValue_NoGenerate_Short(benchmark::State& state) {
static void BM_ComputeStringValue_NoGenerate_Short(benchmark::State& state) {
BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/false, "\"");
}
static void BM_ComputeValue_NoGenerate_Long(benchmark::State& state) {
static void BM_ComputeStringValue_NoGenerate_Long(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/false, "\"");
}
static void BM_ComputeValue_WillGenerate_Short(benchmark::State& state) {
static void BM_ComputeStringValue_WillGenerate_Short(benchmark::State& state) {
BM_SimpleStringValue(state, 10, "\"", /*add_escape=*/true, "\"");
}
static void BM_ComputeValue_WillGenerate_Long(benchmark::State& state) {
static void BM_ComputeStringValue_WillGenerate_Long(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "\"", /*add_escape=*/true, "\"");
}
static void BM_ComputeValue_WillGenerate_Multiline(benchmark::State& state) {
static void BM_ComputeStringValue_WillGenerate_Multiline(
benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "'''\n", /*add_escape=*/true, "\n'''");
}
static void BM_ComputeValue_WillGenerate_MultilineDoubleQuote(
static void BM_ComputeStringValue_WillGenerate_MultilineDoubleQuote(
benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "\"\"\"\n", /*add_escape=*/true,
"\n\"\"\"");
}
static void BM_ComputeValue_WillGenerate_Raw(benchmark::State& state) {
static void BM_ComputeStringValue_WillGenerate_Raw(benchmark::State& state) {
BM_SimpleStringValue(state, 10000, "#\"", /*add_escape=*/true, "\"#");
}
BENCHMARK(BM_ComputeValue_NoGenerate_Short);
BENCHMARK(BM_ComputeValue_NoGenerate_Long);
BENCHMARK(BM_ComputeStringValue_NoGenerate_Short);
BENCHMARK(BM_ComputeStringValue_NoGenerate_Long);
BENCHMARK(BM_ComputeValue_WillGenerate_Short);
BENCHMARK(BM_ComputeValue_WillGenerate_Long);
BENCHMARK(BM_ComputeValue_WillGenerate_Multiline);
BENCHMARK(BM_ComputeValue_WillGenerate_MultilineDoubleQuote);
BENCHMARK(BM_ComputeValue_WillGenerate_Raw);
BENCHMARK(BM_ComputeStringValue_WillGenerate_Short);
BENCHMARK(BM_ComputeStringValue_WillGenerate_Long);
BENCHMARK(BM_ComputeStringValue_WillGenerate_Multiline);
BENCHMARK(BM_ComputeStringValue_WillGenerate_MultilineDoubleQuote);
BENCHMARK(BM_ComputeStringValue_WillGenerate_Raw);
} // namespace
} // namespace Carbon::Lex