Help: Fix lexing of multi-line strings

Teach our CMake lexer to recognize `[[...]]` multi-line strings, as used
in some 'check source compiles/runs' examples to pass literal C/C++ code
through a CMake function. Previously, this resulted in the lexer trying
to parse such code as CMake, which is obviously incorrect. Also, use the
new `[[` syntax in more places.

Note that, because `[[...]]` can also show up in some instances denoting
optional arguments, we only consider `[[` with no `=` as a string if the
`[[` is followed by a non-identifier character. This heuristic seems to
work for most existing usages.

Co-Authored-By: Georg Brandl <georg@python.org>
This commit is contained in:
Matthew Woehlke
2026-02-13 13:36:59 -05:00
co-authored by Georg Brandl
parent 961ed3f2e9
commit b88b7e7c18
7 changed files with 20 additions and 16 deletions
+1
View File
@@ -28,6 +28,7 @@ The following individuals and institutions are among the contributors:
* `Eran Ifrah <mailto:eran.ifrah@gmail.com>`_
* Esben Mose Hansen, Ange Optimization ApS
* `Geoffrey Viola <mailto:geoffrey.viola@asirobots.com>`_
* `Georg Brandl <mailto:georg@python.org>`_
* `Google Inc <https://www.google.com/>`_
* Gregor Jasny
* `Helio Chissini de Castro <mailto:helio@kde.org>`_
+2 -2
View File
@@ -77,7 +77,7 @@ and linked:
include(CheckCSourceCompiles)
check_c_source_compiles("
check_c_source_compiles([[
#include <emmintrin.h>
int main(void)
{
@@ -85,7 +85,7 @@ and linked:
(void)a;
return 0;
}
" PROJECT_HAVE_SSE2_INTRINSICS)
]] PROJECT_HAVE_SSE2_INTRINSICS)
See Also
^^^^^^^^
+2 -2
View File
@@ -65,12 +65,12 @@ the check is stored in the internal cache variable ``HAVE_NORETURN``.
include(CheckCSourceRuns)
check_c_source_runs("
check_c_source_runs([[
#include <stdlib.h>
#include <stdnoreturn.h>
noreturn void f(){ exit(0); }
int main(void) { f(); return 1; }
" HAVE_NORETURN)
]] HAVE_NORETURN)
See Also
^^^^^^^^
+2 -2
View File
@@ -79,13 +79,13 @@ in the internal cache variable ``HAVE_CXX11_LAMBDAS``:
include(CheckCXXSourceCompiles)
check_cxx_source_compiles("
check_cxx_source_compiles([[
int main()
{
auto lambda = []() { return 42; };
return lambda();
}
" HAVE_CXX11_LAMBDAS)
]] HAVE_CXX11_LAMBDAS)
See Also
^^^^^^^^
+4 -4
View File
@@ -111,13 +111,13 @@ result is stored in the internal cache variable ``HAVE_CXX11_LAMBDAS``:
include(CheckSourceCompiles)
check_source_compiles(CXX "
check_source_compiles(CXX [[
int main()
{
auto lambda = []() { return 42; };
return lambda();
}
" HAVE_CXX11_LAMBDAS)
]] HAVE_CXX11_LAMBDAS)
Example: Checking Code With Bracket Argument
""""""""""""""""""""""""""""""""""""""""""""
@@ -179,7 +179,7 @@ checking whether the PostgreSQL ``PGVerbosity`` enum contains
cmake_push_check_state(RESET)
set(CMAKE_REQUIRED_LIBRARIES PostgreSQL::PostgreSQL)
check_source_compiles(C "
check_source_compiles(C [[
#include <libpq-fe.h>
int main(void)
{
@@ -187,7 +187,7 @@ checking whether the PostgreSQL ``PGVerbosity`` enum contains
(void)e;
return 0;
}
" HAVE_PQERRORS_SQLSTATE)
]] HAVE_PQERRORS_SQLSTATE)
cmake_pop_check_state()
endif()
+6 -6
View File
@@ -96,12 +96,12 @@ the check is stored in the internal cache variable ``HAVE_NORETURN``.
include(CheckSourceRuns)
check_source_runs(C "
check_source_runs(C [[
#include <stdlib.h>
#include <stdnoreturn.h>
noreturn void f(){ exit(0); }
int main(void) { f(); return 1; }
" HAVE_NORETURN)
]] HAVE_NORETURN)
Example: Checking Fortran Code
""""""""""""""""""""""""""""""
@@ -112,12 +112,12 @@ Checking if Fortran source code runs successfully:
include(CheckSourceRuns)
check_source_runs(Fortran "
check_source_runs(Fortran [[
program test
real :: x[*]
call co_sum(x)
end program
" HAVE_COARRAY)
]] HAVE_COARRAY)
Example: Checking C++ Code With Bracket Argument
""""""""""""""""""""""""""""""""""""""""""""""""
@@ -171,7 +171,7 @@ successfully and stores a boolean result in the internal cache variable
set(CMAKE_REQUIRED_LIBRARIES gnu)
endif()
check_source_runs(C "
check_source_runs(C [[
#include <sched.h>
int main(void)
{
@@ -180,7 +180,7 @@ successfully and stores a boolean result in the internal cache variable
}
return 0;
}
" HAVE_SCHED_GETCPU)
]] HAVE_SCHED_GETCPU)
cmake_pop_check_state()
See Also
+3
View File
@@ -62,6 +62,9 @@ from pygments.token import (Comment, Name, Number, Operator, Punctuation,
# be present.
CMakeLexer.tokens["root"] = [
# [[string]]
(r'\[\[(?:\W[\w\W]*?)?\]\]', String.Multiline),
(r'\[(?P<level>=+)\[[\w\W]*?\](?P=level)\]', String.Multiline),
# fctn(
(r'\b(\w+)([ \t]*)(\()',
bygroups(Name.Function, Text, Name.Function), '#push'),