1"""
2 pygments.lexers.zig
3 ~~~~~~~~~~~~~~~~~~~
4
5 Lexers for Zig.
6
7 :copyright: Copyright 2006-2025 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
10
11from pygments.lexer import RegexLexer, words
12from pygments.token import Comment, Operator, Keyword, Name, String, \
13 Number, Punctuation, Whitespace
14
15__all__ = ['ZigLexer']
16
17
18class ZigLexer(RegexLexer):
19 """
20 Lexer for the Zig language.
21
22 grammar: https://ziglang.org/documentation/master/#Grammar
23 """
24 name = 'Zig'
25 url = 'http://www.ziglang.org'
26 aliases = ['zig']
27 filenames = ['*.zig']
28 mimetypes = ['text/zig']
29 version_added = ''
30
31 type_keywords = (
32 words(('bool', 'f16', 'f32', 'f64', 'f128', 'void', 'noreturn', 'type',
33 'anyerror', 'promise', 'i0', 'u0', 'isize', 'usize', 'comptime_int',
34 'comptime_float', 'c_short', 'c_ushort', 'c_int', 'c_uint', 'c_long',
35 'c_ulong', 'c_longlong', 'c_ulonglong', 'c_longdouble', 'c_void'
36 'i8', 'u8', 'i16', 'u16', 'i32', 'u32', 'i64', 'u64', 'i128',
37 'u128'), suffix=r'\b'),
38 Keyword.Type)
39
40 storage_keywords = (
41 words(('const', 'var', 'extern', 'packed', 'export', 'pub', 'noalias',
42 'inline', 'comptime', 'nakedcc', 'stdcallcc', 'volatile', 'allowzero',
43 'align', 'linksection', 'threadlocal'), suffix=r'\b'),
44 Keyword.Reserved)
45
46 structure_keywords = (
47 words(('struct', 'enum', 'union', 'error'), suffix=r'\b'),
48 Keyword)
49
50 statement_keywords = (
51 words(('break', 'return', 'continue', 'asm', 'defer', 'errdefer',
52 'unreachable', 'try', 'catch', 'async', 'await', 'suspend',
53 'resume', 'cancel'), suffix=r'\b'),
54 Keyword)
55
56 conditional_keywords = (
57 words(('if', 'else', 'switch', 'and', 'or', 'orelse'), suffix=r'\b'),
58 Keyword)
59
60 repeat_keywords = (
61 words(('while', 'for'), suffix=r'\b'),
62 Keyword)
63
64 other_keywords = (
65 words(('fn', 'usingnamespace', 'test'), suffix=r'\b'),
66 Keyword)
67
68 constant_keywords = (
69 words(('true', 'false', 'null', 'undefined'), suffix=r'\b'),
70 Keyword.Constant)
71
72 tokens = {
73 'root': [
74 (r'\n', Whitespace),
75 (r'\s+', Whitespace),
76 (r'//.*?\n', Comment.Single),
77
78 # Keywords
79 statement_keywords,
80 storage_keywords,
81 structure_keywords,
82 repeat_keywords,
83 type_keywords,
84 constant_keywords,
85 conditional_keywords,
86 other_keywords,
87
88 # Floats
89 (r'0x[0-9a-fA-F]+\.[0-9a-fA-F]+([pP][\-+]?[0-9a-fA-F]+)?', Number.Float),
90 (r'0x[0-9a-fA-F]+\.?[pP][\-+]?[0-9a-fA-F]+', Number.Float),
91 (r'[0-9]+\.[0-9]+([eE][-+]?[0-9]+)?', Number.Float),
92 (r'[0-9]+\.?[eE][-+]?[0-9]+', Number.Float),
93
94 # Integers
95 (r'0b[01]+', Number.Bin),
96 (r'0o[0-7]+', Number.Oct),
97 (r'0x[0-9a-fA-F]+', Number.Hex),
98 (r'[0-9]+', Number.Integer),
99
100 # Identifier
101 (r'@[a-zA-Z_]\w*', Name.Builtin),
102 (r'[a-zA-Z_]\w*', Name),
103
104 # Characters
105 (r'\'\\\'\'', String.Escape),
106 (r'\'\\(x[a-fA-F0-9]{2}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{6}|[nr\\t\'"])\'',
107 String.Escape),
108 (r'\'[^\\\']\'', String),
109
110 # Strings
111 (r'\\\\[^\n]*', String.Heredoc),
112 (r'c\\\\[^\n]*', String.Heredoc),
113 (r'c?"', String, 'string'),
114
115 # Operators, Punctuation
116 (r'[+%=><|^!?/\-*&~:]', Operator),
117 (r'[{}()\[\],.;]', Punctuation)
118 ],
119 'string': [
120 (r'\\(x[a-fA-F0-9]{2}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{6}|[nr\\t\'"])',
121 String.Escape),
122 (r'[^\\"\n]+', String),
123 (r'"', String, '#pop')
124 ]
125 }