| 1 |
%% This is part of the OpTeX project, see http://petr.olsak.net/optex |
| 2 |
|
| 3 |
\_codedecl \_hisyntaxc {Syntax highlighting for C sources <2023-03-02>} |
| 4 |
|
| 5 |
\_newtoks \_hisyntaxc \_newtoks \_hicolorsc |
| 6 |
|
| 7 |
\_global\_hicolorsc={% colors for C language |
| 8 |
\_hicolor K \Red % Keywords |
| 9 |
\_hicolor S \Magenta % Strings |
| 10 |
\_hicolor C \Green % Comments |
| 11 |
\_hicolor N \Cyan % Numbers |
| 12 |
\_hicolor P \Blue % Preprocessor |
| 13 |
\_hicolor O \Blue % Non-letters |
| 14 |
} |
| 15 |
\_global\_hisyntaxc={% |
| 16 |
\_the\_hicolorsc |
| 17 |
\_let\c=\_relax \_let\e=\_relax \_let\o=\_relax |
| 18 |
\_replfromto {/*}{*/} {\x C{/*#1*/}}% /*...*/ |
| 19 |
\_replfromto {//}{^^J} {\z C{//#1}^^J}% //... |
| 20 |
\_replfromto {\_string#}{^^J} {\z P{\##1}^^J}% #include ... |
| 21 |
\_replthis {\_string\"} {{\_string\"}}% \" protected inside strings |
| 22 |
\_replfromto {"}{"} {\x S{"#1"}}% "..." |
| 23 |
% |
| 24 |
\_edef\_tmpa {()\_string{\_string}+-*/=[]<>,:;\_pcent\_string&\_string^|!?}% non-letters |
| 25 |
\_ea \_foreach \_tmpa |
| 26 |
\_do {\_replthis{#1}{\n\o#1\n}} |
| 27 |
\_foreach % keywords |
| 28 |
{alignas}{alignof}{auto}{bool}{break}{case}{char}{const}% |
| 29 |
{constexpr}{continue}{default}{do}{double}{else}{enum}{extern}% |
| 30 |
{false}{float}{for}{goto}{if}{inline}{int}{long}{nullptr}% |
| 31 |
{register}{restrict}{return}{short}{signed}{sizeof}{static}% |
| 32 |
{static_assert}{struct}{switch}{thread_local}{true}{typedef}% |
| 33 |
{typeof}{typeof_unqual}{union}{unsigned}{void}{volatile}{while}% |
| 34 |
{_Alignas}{_Alignof}{_Atomic}{_BitInt}{_Bool}{_Complex}% |
| 35 |
{_Decimal128}{_Decimal32}{_Decimal64}{_Generic}{_Imaginary}% |
| 36 |
{_Noreturn}{_Static_assert}{_Thread_local} |
| 37 |
\_do {\_replthis{\n#1\n}{\z K{#1}}} |
| 38 |
\_replthis{.}{\n.\n} % numbers |
| 39 |
\_foreach 0123456789 |
| 40 |
\_do {\_replfromto{\n#1}{\n}{\c#1##1\e}} |
| 41 |
\_replthis{\e.\c}{.} |
| 42 |
\_replthis{\e.\n}{.\e} |
| 43 |
\_replthis{\n.\c}{\c.} |
| 44 |
\_replthis{e\e\o+\c}{e+}\_replthis{e\e\o-\c}{e-} |
| 45 |
\_replthis{E\e\o+\c}{E+}\_replthis{E\e\o-\c}{E-} |
| 46 |
\_def\o#1{\z O{#1}} |
| 47 |
\_def\c#1\e{\z N{#1}} |
| 48 |
} |
| 49 |
|
| 50 |
\_endcode %------------------------------------------------ |
| 51 |
|
| 52 |
|
| 53 |
Each `hisyntax-<name>.opm` file must declare the token list |
| 54 |
`\_hisyntax<name>` using `\newtoks` and must declare the syntax declaration |
| 55 |
in it. The second token list `\_hicolors<name>` is optional but recommended. |
| 56 |
User can re-declare his/her own colors by `\hicolors` which has precedence |
| 57 |
before `\_hicolors<name>`. |
| 58 |
|
| 59 |
The public variants can be declared if you want to give these token lists to |
| 60 |
the public namespace. But it is not explicitly needed. |
| 61 |
|
| 62 |
All settings must be global here because the file is typically read inside |
| 63 |
a group and we need not read it repeatedly before each code chunk |
| 64 |
again and again. |
| 65 |
|
| 66 |
Note that `\foreach` is used for each non-letters (expanded via \string |
| 67 |
because we needn't treat with active or special TeX characters). The |
| 68 |
keyword is applied in the `\foreach` loop too. |
| 69 |
|
| 70 |
See `hi-sytax.opm` for more information about `\hicolor`, `\replfromto` and |
| 71 |
`\replthis` macros. |
| 72 |
|
| 73 |
\endinput |
| 74 |
|
| 75 |
2023-03-02: more C keywords added |
| 76 |
2020-04-03: released |