Skip to content

Commit 4c8b39a

Browse files
committed
Regenerate the c++ keywords classifier
1 parent 8adc36e commit 4c8b39a

30 files changed

+3307
-807
lines changed

CMakeLists.txt

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,26 +143,6 @@ if (CXX_BUILD_TESTS)
143143
endif()
144144

145145

146-
if (NOT KWGEN_EXECUTABLE AND CMAKE_SYSTEM_NAME STREQUAL "WASI")
147-
148-
ExternalProject_Add(
149-
kwgen_host
150-
SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/tools/kwgen
151-
INSTALL_COMMAND ""
152-
)
153-
154-
ExternalProject_Get_property(kwgen_host BINARY_DIR)
155-
156-
add_executable(kwgen IMPORTED)
157-
set_target_properties(kwgen PROPERTIES IMPORTED_LOCATION "${BINARY_DIR}/kwgen")
158-
add_dependencies(kwgen kwgen_host)
159-
160-
else()
161-
162-
add_subdirectory(tools/kwgen)
163-
164-
endif()
165-
166146
add_subdirectory(src)
167147

168148
if (CXX_BUILD_TESTS)

Dockerfile.emsdk

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ RUN apt-get update && apt-get install -y \
55
wget \
66
unzip
77

8-
COPY tools/kwgen/kwgen.cc /tmp/kwgen.cc
9-
10-
RUN g++ -std=c++20 /tmp/kwgen.cc -o /usr/bin/kwgen
11-
128
RUN wget https://github.com/google/flatbuffers/releases/download/v25.1.24/Linux.flatc.binary.clang++-18.zip && \
139
unzip Linux.flatc.binary.clang++-18.zip && \
1410
mv flatc /usr/bin/flatc && \

Dockerfile.wasi

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ RUN apt-get update && apt-get install -y \
88
unzip \
99
ninja-build
1010

11-
COPY tools/kwgen/kwgen.cc /tmp/kwgen.cc
12-
13-
RUN g++ -std=c++20 /tmp/kwgen.cc -o /usr/bin/kwgen
14-
1511
RUN wget https://github.com/google/flatbuffers/releases/download/v25.1.24/Linux.flatc.binary.clang++-18.zip && \
1612
unzip Linux.flatc.binary.clang++-18.zip && \
1713
mv flatc /usr/bin/flatc && \

packages/cxx-frontend/.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

packages/cxx-frontend/scripts/build.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ async function dockerBuild() {
104104

105105
const cmakeOptions = [
106106
"-DCMAKE_INSTALL_PREFIX=build.em/install/usr",
107-
"-DKWGEN_EXECUTABLE=/usr/bin/kwgen",
108107
"-DFLATBUFFERS_FLATC_EXECUTABLE=/usr/bin/flatc",
109108
"-S .",
110109
"-B build.em",
@@ -136,13 +135,12 @@ async function dockerBuild() {
136135
await $`${docker} run -t --rm -u ${user} -v ${emscriptenCacheDir}:/emsdk/upstream/emscripten/cache/ -v ${projectRootSourcePath}:/code -w /code cxx-emsdk cmake --build build.em`;
137136
}
138137

139-
async function emsdkBuild({ cmake, emcmake, flatc, kwgen }) {
138+
async function emsdkBuild({ cmake, emcmake, flatc }) {
140139
const CMAKE_INTERPROCEDURAL_OPTIMIZATION =
141140
$.env.CMAKE_INTERPROCEDURAL_OPTIMIZATION ?? "ON";
142141

143142
const cmakeOptions = [
144143
`-DCMAKE_INSTALL_PREFIX=${projectRootSourcePath}/build.em/install/usr`,
145-
`-DKWGEN_EXECUTABLE=${kwgen}`,
146144
`-DFLATBUFFERS_FLATC_EXECUTABLE=${flatc}`,
147145
`-S ${projectRootSourcePath}`,
148146
`-B ${projectRootSourcePath}/build.em`,
@@ -193,16 +191,9 @@ async function detectEmsdk() {
193191
(await which("flatc", { nothrow: true }));
194192
if (!flatc) return null;
195193

196-
const kwgen =
197-
argv.kwgen ??
198-
$.env.KWGEN_EXECUTABLE ??
199-
(await which("kwgen", { nothrow: true }));
200-
if (!kwgen) return null;
201-
202194
return {
203195
cmake,
204196
emcmake,
205197
flatc,
206-
kwgen,
207198
};
208199
}

packages/cxx-frontend/src/TokenKind.ts

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,31 @@ export enum TokenKind {
8888
STAR_EQUAL,
8989
STAR,
9090
TILDE,
91+
_ATOMIC,
92+
_BITINT,
93+
_COMPLEX,
94+
_DECIMAL128,
95+
_DECIMAL32,
96+
_DECIMAL64,
97+
_GENERIC,
98+
_IMAGINARY,
99+
_NORETURN,
100+
__ATTRIBUTE__,
101+
__BUILTIN_BIT_CAST,
102+
__BUILTIN_OFFSETOF,
103+
__BUILTIN_VA_ARG,
104+
__BUILTIN_VA_LIST,
105+
__COMPLEX__,
106+
__EXTENSION__,
107+
__FLOAT128,
108+
__FLOAT80,
109+
__IMAG__,
110+
__INT128,
111+
__INT64,
112+
__REAL__,
113+
__RESTRICT__,
114+
__THREAD,
115+
__UNDERLYING_TYPE,
91116
ALIGNAS,
92117
ALIGNOF,
93118
ASM,
@@ -105,8 +130,8 @@ export enum TokenKind {
105130
CO_RETURN,
106131
CO_YIELD,
107132
CONCEPT,
108-
CONST_CAST,
109133
CONST,
134+
CONST_CAST,
110135
CONSTEVAL,
111136
CONSTEXPR,
112137
CONSTINIT,
@@ -142,15 +167,17 @@ export enum TokenKind {
142167
PRIVATE,
143168
PROTECTED,
144169
PUBLIC,
170+
REGISTER,
145171
REINTERPRET_CAST,
146172
REQUIRES,
173+
RESTRICT,
147174
RETURN,
148175
SHORT,
149176
SIGNED,
150177
SIZEOF,
178+
STATIC,
151179
STATIC_ASSERT,
152180
STATIC_CAST,
153-
STATIC,
154181
STRUCT,
155182
SWITCH,
156183
TEMPLATE,
@@ -162,6 +189,8 @@ export enum TokenKind {
162189
TYPEDEF,
163190
TYPEID,
164191
TYPENAME,
192+
TYPEOF,
193+
TYPEOF_UNQUAL,
165194
UNION,
166195
UNSIGNED,
167196
USING,
@@ -170,22 +199,4 @@ export enum TokenKind {
170199
VOLATILE,
171200
WCHAR_T,
172201
WHILE,
173-
__ATTRIBUTE__,
174-
__BUILTIN_BIT_CAST,
175-
__BUILTIN_OFFSETOF,
176-
__BUILTIN_VA_ARG,
177-
__BUILTIN_VA_LIST,
178-
__COMPLEX__,
179-
__EXTENSION__,
180-
__FLOAT128,
181-
__FLOAT80,
182-
__IMAG__,
183-
__INT128,
184-
__INT64,
185-
__REAL__,
186-
__RESTRICT__,
187-
__THREAD,
188-
__UNDERLYING_TYPE,
189-
_ATOMIC,
190-
_COMPLEX,
191202
}

packages/cxx-gen-ast/.prettierrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{}

packages/cxx-gen-ast/src/gen.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ import { gen_ast_slot_ts } from "./gen_ast_slot_ts.ts";
4444
import { gen_token_fwd_h } from "./gen_token_fwd_h.ts";
4545
import { gen_tokenkind_ts } from "./gen_tokenkind_ts.ts";
4646
import { gen_keywords_kwgen } from "./gen_keywords_kwgen.ts";
47+
import { gen_c_keywords_kwgen } from "./gen_c_keywords_kwgen.ts";
48+
import { gen_pp_keywords_kwgen } from "./gen_pp_keywords_kwgen.ts";
4749
import { gen_ast_pretty_printer_h } from "./gen_ast_pretty_printer_h.ts";
4850
import { gen_ast_pretty_printer_cc } from "./gen_ast_pretty_printer_cc.ts";
4951

@@ -150,7 +152,13 @@ gen_tokenkind_ts({
150152
output: path.join(outdir, "packages/cxx-frontend/src/TokenKind.ts"),
151153
});
152154
gen_keywords_kwgen({
153-
output: path.join(outdir, "src/parser/cxx/keywords.kwgen"),
155+
output: path.join(outdir, "src/parser/cxx/private/keywords-priv.h"),
156+
});
157+
gen_c_keywords_kwgen({
158+
output: path.join(outdir, "src/parser/cxx/private/c_keywords-priv.h"),
159+
});
160+
gen_pp_keywords_kwgen({
161+
output: path.join(outdir, "src/parser/cxx/private/pp_directives-priv.h"),
154162
});
155163

156164
child_process.execSync("clang-format -i *.h *.cc", {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2025 Roberto Raggi <[email protected]>
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
// SOFTWARE.
20+
21+
import * as tokens from "./tokens.ts";
22+
import kwgen from "./kwgen.ts";
23+
import { cpy_header } from "./cpy_header.ts";
24+
25+
export function gen_c_keywords_kwgen({ output }: { output: string }) {
26+
const keywords: string[] = [];
27+
28+
keywords.push(...tokens.C_KEYWORDS);
29+
30+
Object.entries(tokens.C_TOKEN_ALIASES).forEach(([tk]) => {
31+
keywords.push(tk);
32+
});
33+
34+
kwgen({
35+
copyright: cpy_header,
36+
output,
37+
keywords,
38+
tokenPrefix: "cxx::TokenKind::T_",
39+
tokenType: "cxx::TokenKind",
40+
toUpper: true,
41+
noEnums: true,
42+
defaultToken: "cxx::TokenKind::T_IDENTIFIER",
43+
classifier: "classifyC",
44+
});
45+
}

packages/cxx-gen-ast/src/gen_keywords_kwgen.ts

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,34 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
2020

21-
import * as fs from "fs";
2221
import * as tokens from "./tokens.ts";
22+
import kwgen from "./kwgen.ts";
23+
import { cpy_header } from "./cpy_header.ts";
2324

2425
export function gen_keywords_kwgen({ output }: { output: string }) {
25-
const code: string[] = [];
26-
const emit = (line = "") => code.push(line);
27-
2826
const isContextKeyword = (kw: string) => {
2927
return ["final", "override", "import", "module"].includes(kw);
3028
};
3129

32-
tokens.KEYWORDS.filter((kw) => !isContextKeyword(kw)).forEach((tk) =>
33-
emit(tk),
34-
);
35-
36-
emit();
37-
tokens.TOKEN_ALIASES.forEach(([tk]) => emit(tk));
30+
const keywords: string[] = [];
3831

39-
const out = `%no-enums
40-
%token-prefix=cxx::TokenKind::T_
41-
%token-type=cxx::TokenKind
42-
%toupper
43-
44-
%%
45-
${code.join("\n")}
46-
`;
32+
tokens.CXX_KEYWORDS.filter((kw) => !isContextKeyword(kw)).forEach((tk) =>
33+
keywords.push(tk),
34+
);
4735

48-
fs.writeFileSync(output, out);
36+
Object.entries(tokens.CXX_TOKEN_ALIASES).forEach(([tk]) => {
37+
keywords.push(tk);
38+
});
39+
40+
kwgen({
41+
copyright: cpy_header,
42+
output,
43+
keywords,
44+
tokenPrefix: "cxx::TokenKind::T_",
45+
tokenType: "cxx::TokenKind",
46+
toUpper: true,
47+
noEnums: true,
48+
defaultToken: "cxx::TokenKind::T_IDENTIFIER",
49+
classifier: "classify",
50+
});
4951
}

0 commit comments

Comments
 (0)