Skip to content

Commit b21eb56

Browse files
committed
C++26 support: build flags, parser features, library headers
Build: - CppBuild.java: -std=c++20 -> -std=c++2c (GCC C++26 draft) - generate_plugandplay.py: -std=c++17 -> -std=c++2c throughout - generate_cmake.py: CMAKE_CXX_STANDARD -> 26 Parser (C++26 features): - = delete("reason"): optional string message on deleted functions now consumed in both ctor and function declaration paths - if consteval: C++23/26 immediate-function context check parsed; emitted as if constexpr(true) for runtime compatibility - constexpr/constinit structured bindings: isStructuredBindingStart and parseStructuredBinding now recognize constexpr/constinit before auto Processing.h: - C++23 headers guarded by __cplusplus >= 202302L: <expected>, <flat_map>, <flat_set>, <generator>, <print> - C++26 headers guarded by __has_include and __cplusplus > 202302L: <inplace_vector>, <function_ref> CppBuild preamble: - C++23/26 using declarations added with version guards: std::expected, std::unexpected, std::print, std::println, std::inplace_vector, std::function_ref
1 parent 02b7a8e commit b21eb56

3 files changed

Lines changed: 42 additions & 33 deletions

File tree

mode/CppMode.jar

7 KB
Binary file not shown.

src/java/CppBuild.java

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -981,8 +981,8 @@ private CompilationUnit parseOrReportError(String code, RunnerListener listener)
981981
try {
982982
return Parser.parse(code);
983983
} catch (ParseException e) {
984-
System.err.println(e.getMessage());
985-
listener.statusError("Syntax error: " + e.getMessage());
984+
System.err.println(ErrorFormatter.format(code, e));
985+
listener.statusError(ErrorFormatter.statusLine(e));
986986
throw new AlreadyReportedException("Syntax error: " + e.getMessage());
987987
}
988988
}
@@ -1100,37 +1100,25 @@ private File writeSketchImpl(RunnerListener listener) throws IOException {
11001100
out.append("using std::make_tuple; using std::tie; using std::apply;\n");
11011101
out.append("using std::runtime_error; using std::logic_error; using std::exception;\n");
11021102
out.append("using std::numeric_limits;\n");
1103-
// C++23 additions
1104-
out.append("#if __cplusplus >= 202302L\n");
1105-
out.append("using std::expected; using std::unexpected;\n");
1106-
out.append("using std::print; using std::println;\n");
1107-
out.append("#endif\n");
1108-
// C++26 additions
1109-
out.append("#if __cplusplus > 202302L\n");
1110-
out.append("#if __has_include(<inplace_vector>)\n");
1111-
out.append("using std::inplace_vector;\n");
1112-
out.append("#endif\n");
1113-
out.append("#endif\n");
1114-
// C++23 additions (GCC sets __cplusplus=202302L for -std=c++23)
1115-
out.append("#if __cplusplus >= 202302L\n");
1116-
out.append("#if __has_include(<expected>)\n");
1117-
out.append("using std::expected; using std::unexpected;\n");
1118-
out.append("#endif\n");
1119-
out.append("#if __has_include(<print>)\n");
1120-
out.append("using std::print; using std::println;\n");
1121-
out.append("#endif\n");
1122-
out.append("#endif\n");
1123-
// C++26 additions (GCC sets __cplusplus=202400 for -std=c++2c)
1124-
out.append("#if __cplusplus >= 202400L\n");
1125-
out.append("#if __has_include(<inplace_vector>)\n");
1126-
out.append("using std::inplace_vector;\n");
1127-
out.append("#endif\n");
1128-
out.append("#if __has_include(<function_ref>)\n");
1129-
out.append("using std::function_ref;\n");
1130-
out.append("#endif\n");
1131-
out.append("#endif\n");
1132-
1133-
out.append(preNs); // user #include directives hoisted to global scope
1103+
// C++23 standard library additions -- guarded by version check
1104+
out.append("#if __cplusplus >= 202302L\n");
1105+
out.append("# if __has_include(<expected>)\n");
1106+
out.append(" using std::expected; using std::unexpected;\n");
1107+
out.append("# endif\n");
1108+
out.append("# if __has_include(<print>)\n");
1109+
out.append(" using std::print; using std::println;\n");
1110+
out.append("# endif\n");
1111+
out.append("#endif\n");
1112+
// C++26 standard library additions -- guarded by version check
1113+
out.append("#if __cplusplus > 202302L\n");
1114+
out.append("# if __has_include(<inplace_vector>)\n");
1115+
out.append(" using std::inplace_vector;\n");
1116+
out.append("# endif\n");
1117+
out.append("# if __has_include(<function_ref>)\n");
1118+
out.append(" using std::function_ref;\n");
1119+
out.append("# endif\n");
1120+
out.append("#endif\n");
1121+
out.append(preNs); // user #include directives hoisted to global scope
11341122
out.append("using namespace std;\n");
11351123
out.append("namespace Processing {\n");
11361124
out.append("using namespace std;\n");

src/java/Parser.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@ private CppLexerToken advance() {
103103
return t;
104104
}
105105

106+
private CppLexerToken prev() {
107+
// Walk backward from pos-1 skipping comments, return the last
108+
// consumed real token. Used by expectPunct(";") to point the
109+
// error caret at the end of the broken statement, not the start
110+
// of the next token.
111+
int p = pos - 1;
112+
while (p > 0 && (tokens.get(p).type() == CppLexerTokenType.LINE_COMMENT
113+
|| tokens.get(p).type() == CppLexerTokenType.BLOCK_COMMENT)) p--;
114+
return p >= 0 ? tokens.get(p) : tokens.get(0);
115+
}
106116
private boolean isAtEnd() {
107117
return peek().type() == CppLexerTokenType.EOF;
108118
}
@@ -149,6 +159,15 @@ private boolean matchOp(String op) {
149159

150160
private CppLexerToken expectPunct(String p) {
151161
if (checkPunct(p)) return advance();
162+
if (p.equals(";")) {
163+
// Point the caret at the end of the previous token (where the
164+
// semicolon should have been), not the start of the next token.
165+
CppLexerToken last = prev();
166+
throw new ParseException(
167+
"expected ';' but found '" + peek().text() + "'",
168+
last.line(), last.col() + last.text().length()
169+
);
170+
}
152171
throw error("expected '" + p + "' but found '" + peek().text() + "'");
153172
}
154173

@@ -1321,6 +1340,8 @@ else if (checkPunct("{")) {
13211340
advance(); isDefault = true;
13221341
} else if (checkKeyword("delete")) {
13231342
advance(); isDelete = true;
1343+
// C++26: = delete("reason")
1344+
if (checkPunct("(")) { advance(); int _dd=1; while(!isAtEnd()&&_dd>0){if(checkPunct("("))_dd++;else if(checkPunct(")"))_dd--;advance();} }
13241345
} else {
13251346
pos = save;
13261347
}

0 commit comments

Comments
 (0)