File: c_highlighter.py

package info (click to toggle)
gnat-gps 18-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,716 kB
  • sloc: ada: 362,679; python: 31,031; xml: 9,597; makefile: 1,030; ansic: 917; sh: 264; java: 17
file content (106 lines) | stat: -rw-r--r-- 3,600 bytes parent folder | download
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from highlighter.common import *

hl_cont_line = simple(r"\\\n", tag=tag_type)

############
# Literals #
############

string_literal = region(
    r'"', r'"|[^\\]$', matchall=False, tag=tag_string,
    highlighter=(hl_cont_line, hl_inside_strings)
)

character_literal = simple(r"'(?:\\.|.)?'", tag=tag_string)
number_literal = simple(r"\b[0-9]*\.?[0-9]+\b", tag=tag_number)

############
# Comments #
############

comments_subhl = (hl_cont_line, hl_comment_notes)

c99_comment = region(r"//", r"$", tag=tag_comment, name="comment",
                     highlighter=comments_subhl)

multiline_comment = region(r"/\*", r"\*/", tag=tag_comment, name="ml_comment",
                           highlighter=(hl_comment_notes,))

preprocessor_comment = region(
    r"^{0}#{1}if{2}0".format(ws, ws, ws), r"^{0}#{1}endif".format(ws, ws),
    name="preprocessor_comment",
    tag=tag_comment,
    highlighter=(region_ref("preprocessor_comment"),))

pp_words = "|".join(["define", "if", "elif", "else", "endif", "ifndef",
                     "error", "import", "undef", "include", "using", "ifdef",
                     "line", "pragma"])

################
# Preprocessor #
################

preprocessor_directive = region(
    r"^{0}#{1}(?:{2})".format(ws, ws, pp_words), r'$',
    tag=new_style(lang="General",
                  name="preprocessor",
                  label="Preprocessor",
                  doc="Style used for preprocessor directives",
                  foreground_colors=("#606090", "#A0A0F0"),
                  prio=0),
    highlighter=(
        hl_cont_line,
        string_literal,
        simple(r'\<.*?\>', tag=tag_string),
        character_literal,
        number_literal,
        c99_comment,
        multiline_comment
    ),
    matchall=False
)

#######################
# General highlighter #
#######################

register_highlighter(
    language="c",
    spec=(
        string_literal, character_literal, number_literal,
        c99_comment, multiline_comment, preprocessor_comment,
        preprocessor_directive,
        # Match keywords
        words("auto|break|case|const|continue|default|do|else|enum|extern|for"
              "|goto|if|register|return|sizeof|static|struct|switch|typedef"
              "|union|void|volatile|while", tag=tag_keyword),

        words("int|long|char|float|short|unsigned|double|signed",
              tag=tag_type)
    )
)

register_highlighter(
    language="c++",
    spec=(
        string_literal, character_literal, number_literal,
        c99_comment, multiline_comment, preprocessor_comment,
        preprocessor_directive,
        # Match keywords
        words("alignas|alignof|and|and_eq|asm|atomic_cancel|atomic_commit"
              "|atomic_noexcept|auto|bitand|bitor|break|case|catch|class"
              "|compl|concept|const|constexpr|const_cast|continue|decltype"
              "|default|delete|do|dynamic_cast|else|enum|explicit|export"
              "|extern|false|for|friend|goto|if|inline|mutable|namespace|new"
              "|noexcept|not|not_eq|nullptr|operator|or|or_eq|private"
              "|protected|public|register|reinterpret_cast|requires|return"
              "|sizeof|static|static_assert|static_cast|struct|switch"
              "|synchronized|template|this|thread_local|throw|true|try|typedef"
              "|typeid|typename|union|using|virtual|void|volatile|while"
              "|xor|xor_eq", tag=tag_keyword),

        words("bool|char|char16_t|char32_t|double|float|int|long|short"
              "|signed|uint16_t|uint32_t|uint64_t|unsigned|wchar_t",
              tag=tag_type)
    )
)