summaryrefslogtreecommitdiff
path: root/macros/optex/base/hisyntax-c.opm
blob: 07d6a6570ce16267f8b8c75487b086c88a36a637 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
%% This is part of the OpTeX project, see http://petr.olsak.net/optex

\_codedecl \_hisyntaxc {Syntax highlighting for C sources <2023-03-02>}

\_newtoks \_hisyntaxc  \_newtoks \_hicolorsc

\_global\_hicolorsc={%     colors for C language
   \_hicolor K \Red      % Keywords
   \_hicolor S \Magenta  % Strings
   \_hicolor C \Green    % Comments
   \_hicolor N \Cyan     % Numbers
   \_hicolor P \Blue     % Preprocessor
   \_hicolor O \Blue     % Non-letters
}
\_global\_hisyntaxc={%
   \_the\_hicolorsc
   \_let\c=\_relax \_let\e=\_relax \_let\o=\_relax
   \_replfromto {/*}{*/}         {\x C{/*#1*/}}%   /*...*/
   \_replfromto {//}{^^J}        {\z C{//#1}^^J}%  //...
   \_replfromto {\_string#}{^^J} {\z P{\##1}^^J}%  #include ...
   \_replthis   {\_string\"}     {{\_string\"}}%   \" protected inside strings
   \_replfromto {"}{"}           {\x S{"#1"}}%     "..."
   %
   \_edef\_tmpa {()\_string{\_string}+-*/=[]<>,:;\_pcent\_string&\_string^|!?}% non-letters
   \_ea \_foreach \_tmpa
      \_do {\_replthis{#1}{\n\o#1\n}}
   \_foreach                                                              % keywords
      {alignas}{alignof}{auto}{bool}{break}{case}{char}{const}%
      {constexpr}{continue}{default}{do}{double}{else}{enum}{extern}%
      {false}{float}{for}{goto}{if}{inline}{int}{long}{nullptr}%
      {register}{restrict}{return}{short}{signed}{sizeof}{static}%
      {static_assert}{struct}{switch}{thread_local}{true}{typedef}%
      {typeof}{typeof_unqual}{union}{unsigned}{void}{volatile}{while}%
      {_Alignas}{_Alignof}{_Atomic}{_BitInt}{_Bool}{_Complex}%
      {_Decimal128}{_Decimal32}{_Decimal64}{_Generic}{_Imaginary}%
      {_Noreturn}{_Static_assert}{_Thread_local}
      \_do {\_replthis{\n#1\n}{\z K{#1}}}
   \_replthis{.}{\n.\n}                                                   % numbers
   \_foreach 0123456789
      \_do {\_replfromto{\n#1}{\n}{\c#1##1\e}}
   \_replthis{\e.\c}{.}
   \_replthis{\e.\n}{.\e}
   \_replthis{\n.\c}{\c.}
   \_replthis{e\e\o+\c}{e+}\_replthis{e\e\o-\c}{e-}
   \_replthis{E\e\o+\c}{E+}\_replthis{E\e\o-\c}{E-}
   \_def\o#1{\z O{#1}}
   \_def\c#1\e{\z N{#1}}
}

\_endcode %------------------------------------------------


Each `hisyntax-<name>.opm` file must declare the token list
`\_hisyntax<name>` using `\newtoks` and must declare the syntax declaration
in it. The second token list `\_hicolors<name>` is optional but recommended.
User can re-declare his/her own colors by `\hicolors` which has precedence
before `\_hicolors<name>`.

The public variants can be declared if you want to give these token lists to
the public namespace. But it is not explicitly needed.

All settings must be global here because the file is typically read inside
a group and we need not read it repeatedly before each code chunk
again and again.

Note that `\foreach` is used for each non-letters (expanded via \string
because we needn't treat with active or special TeX characters). The
keyword is applied in the `\foreach` loop too.

See `hi-sytax.opm` for more information about `\hicolor`, `\replfromto` and
`\replthis` macros.

\endinput

2023-03-02: more C keywords added
2020-04-03: released