1"""
2 pygments.lexers.webidl
3 ~~~~~~~~~~~~~~~~~~~~~~
4
5 Lexers for Web IDL, including some extensions.
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, default, include, words
12from pygments.token import Comment, Keyword, Name, Number, Punctuation, \
13 String, Text
14
15__all__ = ['WebIDLLexer']
16
17_builtin_types = (
18 # primitive types
19 'byte', 'octet', 'boolean',
20 r'(?:unsigned\s+)?(?:short|long(?:\s+long)?)',
21 r'(?:unrestricted\s+)?(?:float|double)',
22 # string types
23 'DOMString', 'ByteString', 'USVString',
24 # exception types
25 'Error', 'DOMException',
26 # typed array types
27 'Uint8Array', 'Uint16Array', 'Uint32Array', 'Uint8ClampedArray',
28 'Float32Array', 'Float64Array',
29 # buffer source types
30 'ArrayBuffer', 'DataView', 'Int8Array', 'Int16Array', 'Int32Array',
31 # other
32 'any', 'void', 'object', 'RegExp',
33)
34_identifier = r'_?[A-Za-z][a-zA-Z0-9_-]*'
35_keyword_suffix = r'(?![\w-])'
36_string = r'"[^"]*"'
37
38
39class WebIDLLexer(RegexLexer):
40 """
41 For Web IDL.
42 """
43
44 name = 'Web IDL'
45 url = 'https://www.w3.org/wiki/Web_IDL'
46 aliases = ['webidl']
47 filenames = ['*.webidl']
48 version_added = '2.6'
49
50 tokens = {
51 'common': [
52 (r'\s+', Text),
53 (r'(?s)/\*.*?\*/', Comment.Multiline),
54 (r'//.*', Comment.Single),
55 (r'^#.*', Comment.Preproc),
56 ],
57 'root': [
58 include('common'),
59 (r'\[', Punctuation, 'extended_attributes'),
60 (r'partial' + _keyword_suffix, Keyword),
61 (r'typedef' + _keyword_suffix, Keyword, ('typedef', 'type')),
62 (r'interface' + _keyword_suffix, Keyword, 'interface_rest'),
63 (r'enum' + _keyword_suffix, Keyword, 'enum_rest'),
64 (r'callback' + _keyword_suffix, Keyword, 'callback_rest'),
65 (r'dictionary' + _keyword_suffix, Keyword, 'dictionary_rest'),
66 (r'namespace' + _keyword_suffix, Keyword, 'namespace_rest'),
67 (_identifier, Name.Class, 'implements_rest'),
68 ],
69 'extended_attributes': [
70 include('common'),
71 (r',', Punctuation),
72 (_identifier, Name.Decorator),
73 (r'=', Punctuation, 'extended_attribute_rest'),
74 (r'\(', Punctuation, 'argument_list'),
75 (r'\]', Punctuation, '#pop'),
76 ],
77 'extended_attribute_rest': [
78 include('common'),
79 (_identifier, Name, 'extended_attribute_named_rest'),
80 (_string, String),
81 (r'\(', Punctuation, 'identifier_list'),
82 default('#pop'),
83 ],
84 'extended_attribute_named_rest': [
85 include('common'),
86 (r'\(', Punctuation, 'argument_list'),
87 default('#pop'),
88 ],
89 'argument_list': [
90 include('common'),
91 (r'\)', Punctuation, '#pop'),
92 default('argument'),
93 ],
94 'argument': [
95 include('common'),
96 (r'optional' + _keyword_suffix, Keyword),
97 (r'\[', Punctuation, 'extended_attributes'),
98 (r',', Punctuation, '#pop'),
99 (r'\)', Punctuation, '#pop:2'),
100 default(('argument_rest', 'type'))
101 ],
102 'argument_rest': [
103 include('common'),
104 (_identifier, Name.Variable),
105 (r'\.\.\.', Punctuation),
106 (r'=', Punctuation, 'default_value'),
107 default('#pop'),
108 ],
109 'identifier_list': [
110 include('common'),
111 (_identifier, Name.Class),
112 (r',', Punctuation),
113 (r'\)', Punctuation, '#pop'),
114 ],
115 'type': [
116 include('common'),
117 (r'(?:' + r'|'.join(_builtin_types) + r')' + _keyword_suffix,
118 Keyword.Type, 'type_null'),
119 (words(('sequence', 'Promise', 'FrozenArray'),
120 suffix=_keyword_suffix), Keyword.Type, 'type_identifier'),
121 (_identifier, Name.Class, 'type_identifier'),
122 (r'\(', Punctuation, 'union_type'),
123 ],
124 'union_type': [
125 include('common'),
126 (r'or' + _keyword_suffix, Keyword),
127 (r'\)', Punctuation, ('#pop', 'type_null')),
128 default('type'),
129 ],
130 'type_identifier': [
131 (r'<', Punctuation, 'type_list'),
132 default(('#pop', 'type_null'))
133 ],
134 'type_null': [
135 (r'\?', Punctuation),
136 default('#pop:2'),
137 ],
138 'default_value': [
139 include('common'),
140 include('const_value'),
141 (_string, String, '#pop'),
142 (r'\[\s*\]', Punctuation, '#pop'),
143 ],
144 'const_value': [
145 include('common'),
146 (words(('true', 'false', '-Infinity', 'Infinity', 'NaN', 'null'),
147 suffix=_keyword_suffix), Keyword.Constant, '#pop'),
148 (r'-?(?:(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:[Ee][+-]?[0-9]+)?' +
149 r'|[0-9]+[Ee][+-]?[0-9]+)', Number.Float, '#pop'),
150 (r'-?[1-9][0-9]*', Number.Integer, '#pop'),
151 (r'-?0[Xx][0-9A-Fa-f]+', Number.Hex, '#pop'),
152 (r'-?0[0-7]*', Number.Oct, '#pop'),
153 ],
154 'typedef': [
155 include('common'),
156 (_identifier, Name.Class),
157 (r';', Punctuation, '#pop'),
158 ],
159 'namespace_rest': [
160 include('common'),
161 (_identifier, Name.Namespace),
162 (r'\{', Punctuation, 'namespace_body'),
163 (r';', Punctuation, '#pop'),
164 ],
165 'namespace_body': [
166 include('common'),
167 (r'\[', Punctuation, 'extended_attributes'),
168 (r'readonly' + _keyword_suffix, Keyword),
169 (r'attribute' + _keyword_suffix,
170 Keyword, ('attribute_rest', 'type')),
171 (r'const' + _keyword_suffix, Keyword, ('const_rest', 'type')),
172 (r'\}', Punctuation, '#pop'),
173 default(('operation_rest', 'type')),
174 ],
175 'interface_rest': [
176 include('common'),
177 (_identifier, Name.Class),
178 (r':', Punctuation),
179 (r'\{', Punctuation, 'interface_body'),
180 (r';', Punctuation, '#pop'),
181 ],
182 'interface_body': [
183 (words(('iterable', 'maplike', 'setlike'), suffix=_keyword_suffix),
184 Keyword, 'iterable_maplike_setlike_rest'),
185 (words(('setter', 'getter', 'creator', 'deleter', 'legacycaller',
186 'inherit', 'static', 'stringifier', 'jsonifier'),
187 suffix=_keyword_suffix), Keyword),
188 (r'serializer' + _keyword_suffix, Keyword, 'serializer_rest'),
189 (r';', Punctuation),
190 include('namespace_body'),
191 ],
192 'attribute_rest': [
193 include('common'),
194 (_identifier, Name.Variable),
195 (r';', Punctuation, '#pop'),
196 ],
197 'const_rest': [
198 include('common'),
199 (_identifier, Name.Constant),
200 (r'=', Punctuation, 'const_value'),
201 (r';', Punctuation, '#pop'),
202 ],
203 'operation_rest': [
204 include('common'),
205 (r';', Punctuation, '#pop'),
206 default('operation'),
207 ],
208 'operation': [
209 include('common'),
210 (_identifier, Name.Function),
211 (r'\(', Punctuation, 'argument_list'),
212 (r';', Punctuation, '#pop:2'),
213 ],
214 'iterable_maplike_setlike_rest': [
215 include('common'),
216 (r'<', Punctuation, 'type_list'),
217 (r';', Punctuation, '#pop'),
218 ],
219 'type_list': [
220 include('common'),
221 (r',', Punctuation),
222 (r'>', Punctuation, '#pop'),
223 default('type'),
224 ],
225 'serializer_rest': [
226 include('common'),
227 (r'=', Punctuation, 'serialization_pattern'),
228 (r';', Punctuation, '#pop'),
229 default('operation'),
230 ],
231 'serialization_pattern': [
232 include('common'),
233 (_identifier, Name.Variable, '#pop'),
234 (r'\{', Punctuation, 'serialization_pattern_map'),
235 (r'\[', Punctuation, 'serialization_pattern_list'),
236 ],
237 'serialization_pattern_map': [
238 include('common'),
239 (words(('getter', 'inherit', 'attribute'),
240 suffix=_keyword_suffix), Keyword),
241 (r',', Punctuation),
242 (_identifier, Name.Variable),
243 (r'\}', Punctuation, '#pop:2'),
244 ],
245 'serialization_pattern_list': [
246 include('common'),
247 (words(('getter', 'attribute'), suffix=_keyword_suffix), Keyword),
248 (r',', Punctuation),
249 (_identifier, Name.Variable),
250 (r']', Punctuation, '#pop:2'),
251 ],
252 'enum_rest': [
253 include('common'),
254 (_identifier, Name.Class),
255 (r'\{', Punctuation, 'enum_body'),
256 (r';', Punctuation, '#pop'),
257 ],
258 'enum_body': [
259 include('common'),
260 (_string, String),
261 (r',', Punctuation),
262 (r'\}', Punctuation, '#pop'),
263 ],
264 'callback_rest': [
265 include('common'),
266 (r'interface' + _keyword_suffix,
267 Keyword, ('#pop', 'interface_rest')),
268 (_identifier, Name.Class),
269 (r'=', Punctuation, ('operation', 'type')),
270 (r';', Punctuation, '#pop'),
271 ],
272 'dictionary_rest': [
273 include('common'),
274 (_identifier, Name.Class),
275 (r':', Punctuation),
276 (r'\{', Punctuation, 'dictionary_body'),
277 (r';', Punctuation, '#pop'),
278 ],
279 'dictionary_body': [
280 include('common'),
281 (r'\[', Punctuation, 'extended_attributes'),
282 (r'required' + _keyword_suffix, Keyword),
283 (r'\}', Punctuation, '#pop'),
284 default(('dictionary_item', 'type')),
285 ],
286 'dictionary_item': [
287 include('common'),
288 (_identifier, Name.Variable),
289 (r'=', Punctuation, 'default_value'),
290 (r';', Punctuation, '#pop'),
291 ],
292 'implements_rest': [
293 include('common'),
294 (r'implements' + _keyword_suffix, Keyword),
295 (_identifier, Name.Class),
296 (r';', Punctuation, '#pop'),
297 ],
298 }