|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.zig |
|
4 ~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for Zig. |
|
7 |
|
8 :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 from pygments.lexer import RegexLexer, words |
|
13 from pygments.token import Comment, Operator, Keyword, Name, String, \ |
|
14 Number, Punctuation, Whitespace |
|
15 |
|
16 __all__ = ['ZigLexer'] |
|
17 |
|
18 |
|
19 class ZigLexer(RegexLexer): |
|
20 """ |
|
21 For `Zig <http://www.ziglang.org>`_ source code. |
|
22 |
|
23 grammar: https://ziglang.org/documentation/master/#Grammar |
|
24 """ |
|
25 name = 'Zig' |
|
26 aliases = ['zig'] |
|
27 filenames = ['*.zig'] |
|
28 mimetypes = ['text/zig'] |
|
29 |
|
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) |
|
38 |
|
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) |
|
44 |
|
45 structure_keywords = ( |
|
46 words(('struct', 'enum', 'union', 'error'), suffix=r'\b'), |
|
47 Keyword) |
|
48 |
|
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) |
|
54 |
|
55 conditional_keywords = ( |
|
56 words(('if', 'else', 'switch', 'and', 'or', 'orelse'), suffix=r'\b'), |
|
57 Keyword) |
|
58 |
|
59 repeat_keywords = ( |
|
60 words(('while', 'for'), suffix=r'\b'), |
|
61 Keyword) |
|
62 |
|
63 other_keywords = ( |
|
64 words(('fn', 'usingnamespace', 'test'), suffix=r'\b'), |
|
65 Keyword) |
|
66 |
|
67 constant_keywords = ( |
|
68 words(('true', 'false', 'null', 'undefined'), suffix=r'\b'), |
|
69 Keyword.Constant) |
|
70 |
|
71 tokens = { |
|
72 'root': [ |
|
73 (r'\n', Whitespace), |
|
74 (r'\s+', Whitespace), |
|
75 (r'//.*?\n', Comment.Single), |
|
76 |
|
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, |
|
86 |
|
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), |
|
92 |
|
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), |
|
98 |
|
99 # Identifier |
|
100 (r'@[a-zA-Z_]\w*', Name.Builtin), |
|
101 (r'[a-zA-Z_]\w*', Name), |
|
102 |
|
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), |
|
108 |
|
109 # Strings |
|
110 (r'\\\\[^\n]*', String.Heredoc), |
|
111 (r'c\\\\[^\n]*', String.Heredoc), |
|
112 (r'c?"', String, 'string'), |
|
113 |
|
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 } |
|
125 |
|
126 def get_tokens_unprocessed(self, text): |
|
127 for index, token, value in \ |
|
128 RegexLexer.get_tokens_unprocessed(self, text): |
|
129 yield index, token, value |