From 104362255134a29a6370a3f77d4433ec1ded526a Mon Sep 17 00:00:00 2001 From: Herb Sutter Date: Wed, 13 Sep 2023 12:09:22 -0700 Subject: [PATCH] Allow `__identifier__keyword` to be the identifier "keyword", closes #642 See --- regression-tests/test-results/version | 2 +- source/build.info | 2 +- source/common.h | 1 + source/lex.h | 13 +++++++++++++ source/parse.h | 2 +- 5 files changed, 17 insertions(+), 3 deletions(-) diff --git a/regression-tests/test-results/version b/regression-tests/test-results/version index f9603c154..3ec7b4dea 100644 --- a/regression-tests/test-results/version +++ b/regression-tests/test-results/version @@ -1,5 +1,5 @@ -cppfront compiler v0.2.1 Build 8912:1051 +cppfront compiler v0.2.1 Build 8913:1037 Copyright(c) Herb Sutter All rights reserved SPDX-License-Identifier: CC-BY-NC-ND-4.0 diff --git a/source/build.info b/source/build.info index cbce88813..495b5d43f 100644 --- a/source/build.info +++ b/source/build.info @@ -1 +1 @@ -"8912:1051" \ No newline at end of file +"8913:1037" \ No newline at end of file diff --git a/source/common.h b/source/common.h index a156da34b..72e455876 100644 --- a/source/common.h +++ b/source/common.h @@ -359,6 +359,7 @@ auto is_identifier_continue(char c) } //G identifier: +//G '__identifier__' keyword [Note: without whitespace before the keyword] //G identifier-start //G identifier identifier-continue //G 'operator' operator diff --git a/source/lex.h b/source/lex.h index a458ad721..9902f346f 100644 --- a/source/lex.h +++ b/source/lex.h @@ -306,6 +306,16 @@ class token v.start(*this, depth); } + auto remove_prefix_if(std::string_view prefix) { + if ( + sv.size() > prefix.size() + && sv.starts_with(prefix) + ) + { + sv.remove_prefix(prefix.size()); + } + } + private: std::string_view sv; source_position pos; @@ -1682,6 +1692,9 @@ auto lex_line( else { store(j, lexeme::Identifier); + + tokens.back().remove_prefix_if("__identifier__"); + if (tokens.back() == "NULL") { errors.emplace_back( source_position(lineno, i), diff --git a/source/parse.h b/source/parse.h index 914a7a9cd..fd7ddceb7 100644 --- a/source/parse.h +++ b/source/parse.h @@ -7106,7 +7106,7 @@ class parser //G declaration: - //G access-specifier? identifier unnamed-declaration + //G access-specifier? identifier '...'? unnamed-declaration //G access-specifier? identifier alias //G //G access-specifier: