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