Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/webidl.py: 100%
13 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-07-01 06:54 +0000
1"""
2 pygments.lexers.webidl
3 ~~~~~~~~~~~~~~~~~~~~~~
5 Lexers for Web IDL, including some extensions.
7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11from pygments.lexer import RegexLexer, default, include, words
12from pygments.token import Comment, Keyword, Name, Number, Punctuation, \
13 String, Text
15__all__ = ['WebIDLLexer']
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'"[^"]*"'
39class WebIDLLexer(RegexLexer):
40 """
41 For Web IDL.
43 .. versionadded:: 2.6
44 """
46 name = 'Web IDL'
47 url = 'https://www.w3.org/wiki/Web_IDL'
48 aliases = ['webidl']
49 filenames = ['*.webidl']
51 tokens = {
52 'common': [
53 (r'\s+', Text),
54 (r'(?s)/\*.*?\*/', Comment.Multiline),
55 (r'//.*', Comment.Single),
56 (r'^#.*', Comment.Preproc),
57 ],
58 'root': [
59 include('common'),
60 (r'\[', Punctuation, 'extended_attributes'),
61 (r'partial' + _keyword_suffix, Keyword),
62 (r'typedef' + _keyword_suffix, Keyword, ('typedef', 'type')),
63 (r'interface' + _keyword_suffix, Keyword, 'interface_rest'),
64 (r'enum' + _keyword_suffix, Keyword, 'enum_rest'),
65 (r'callback' + _keyword_suffix, Keyword, 'callback_rest'),
66 (r'dictionary' + _keyword_suffix, Keyword, 'dictionary_rest'),
67 (r'namespace' + _keyword_suffix, Keyword, 'namespace_rest'),
68 (_identifier, Name.Class, 'implements_rest'),
69 ],
70 'extended_attributes': [
71 include('common'),
72 (r',', Punctuation),
73 (_identifier, Name.Decorator),
74 (r'=', Punctuation, 'extended_attribute_rest'),
75 (r'\(', Punctuation, 'argument_list'),
76 (r'\]', Punctuation, '#pop'),
77 ],
78 'extended_attribute_rest': [
79 include('common'),
80 (_identifier, Name, 'extended_attribute_named_rest'),
81 (_string, String),
82 (r'\(', Punctuation, 'identifier_list'),
83 default('#pop'),
84 ],
85 'extended_attribute_named_rest': [
86 include('common'),
87 (r'\(', Punctuation, 'argument_list'),
88 default('#pop'),
89 ],
90 'argument_list': [
91 include('common'),
92 (r'\)', Punctuation, '#pop'),
93 default('argument'),
94 ],
95 'argument': [
96 include('common'),
97 (r'optional' + _keyword_suffix, Keyword),
98 (r'\[', Punctuation, 'extended_attributes'),
99 (r',', Punctuation, '#pop'),
100 (r'\)', Punctuation, '#pop:2'),
101 default(('argument_rest', 'type'))
102 ],
103 'argument_rest': [
104 include('common'),
105 (_identifier, Name.Variable),
106 (r'\.\.\.', Punctuation),
107 (r'=', Punctuation, 'default_value'),
108 default('#pop'),
109 ],
110 'identifier_list': [
111 include('common'),
112 (_identifier, Name.Class),
113 (r',', Punctuation),
114 (r'\)', Punctuation, '#pop'),
115 ],
116 'type': [
117 include('common'),
118 (r'(?:' + r'|'.join(_builtin_types) + r')' + _keyword_suffix,
119 Keyword.Type, 'type_null'),
120 (words(('sequence', 'Promise', 'FrozenArray'),
121 suffix=_keyword_suffix), Keyword.Type, 'type_identifier'),
122 (_identifier, Name.Class, 'type_identifier'),
123 (r'\(', Punctuation, 'union_type'),
124 ],
125 'union_type': [
126 include('common'),
127 (r'or' + _keyword_suffix, Keyword),
128 (r'\)', Punctuation, ('#pop', 'type_null')),
129 default('type'),
130 ],
131 'type_identifier': [
132 (r'<', Punctuation, 'type_list'),
133 default(('#pop', 'type_null'))
134 ],
135 'type_null': [
136 (r'\?', Punctuation),
137 default('#pop:2'),
138 ],
139 'default_value': [
140 include('common'),
141 include('const_value'),
142 (_string, String, '#pop'),
143 (r'\[\s*\]', Punctuation, '#pop'),
144 ],
145 'const_value': [
146 include('common'),
147 (words(('true', 'false', '-Infinity', 'Infinity', 'NaN', 'null'),
148 suffix=_keyword_suffix), Keyword.Constant, '#pop'),
149 (r'-?(?:(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:[Ee][+-]?[0-9]+)?' +
150 r'|[0-9]+[Ee][+-]?[0-9]+)', Number.Float, '#pop'),
151 (r'-?[1-9][0-9]*', Number.Integer, '#pop'),
152 (r'-?0[Xx][0-9A-Fa-f]+', Number.Hex, '#pop'),
153 (r'-?0[0-7]*', Number.Oct, '#pop'),
154 ],
155 'typedef': [
156 include('common'),
157 (_identifier, Name.Class),
158 (r';', Punctuation, '#pop'),
159 ],
160 'namespace_rest': [
161 include('common'),
162 (_identifier, Name.Namespace),
163 (r'\{', Punctuation, 'namespace_body'),
164 (r';', Punctuation, '#pop'),
165 ],
166 'namespace_body': [
167 include('common'),
168 (r'\[', Punctuation, 'extended_attributes'),
169 (r'readonly' + _keyword_suffix, Keyword),
170 (r'attribute' + _keyword_suffix,
171 Keyword, ('attribute_rest', 'type')),
172 (r'const' + _keyword_suffix, Keyword, ('const_rest', 'type')),
173 (r'\}', Punctuation, '#pop'),
174 default(('operation_rest', 'type')),
175 ],
176 'interface_rest': [
177 include('common'),
178 (_identifier, Name.Class),
179 (r':', Punctuation),
180 (r'\{', Punctuation, 'interface_body'),
181 (r';', Punctuation, '#pop'),
182 ],
183 'interface_body': [
184 (words(('iterable', 'maplike', 'setlike'), suffix=_keyword_suffix),
185 Keyword, 'iterable_maplike_setlike_rest'),
186 (words(('setter', 'getter', 'creator', 'deleter', 'legacycaller',
187 'inherit', 'static', 'stringifier', 'jsonifier'),
188 suffix=_keyword_suffix), Keyword),
189 (r'serializer' + _keyword_suffix, Keyword, 'serializer_rest'),
190 (r';', Punctuation),
191 include('namespace_body'),
192 ],
193 'attribute_rest': [
194 include('common'),
195 (_identifier, Name.Variable),
196 (r';', Punctuation, '#pop'),
197 ],
198 'const_rest': [
199 include('common'),
200 (_identifier, Name.Constant),
201 (r'=', Punctuation, 'const_value'),
202 (r';', Punctuation, '#pop'),
203 ],
204 'operation_rest': [
205 include('common'),
206 (r';', Punctuation, '#pop'),
207 default('operation'),
208 ],
209 'operation': [
210 include('common'),
211 (_identifier, Name.Function),
212 (r'\(', Punctuation, 'argument_list'),
213 (r';', Punctuation, '#pop:2'),
214 ],
215 'iterable_maplike_setlike_rest': [
216 include('common'),
217 (r'<', Punctuation, 'type_list'),
218 (r';', Punctuation, '#pop'),
219 ],
220 'type_list': [
221 include('common'),
222 (r',', Punctuation),
223 (r'>', Punctuation, '#pop'),
224 default('type'),
225 ],
226 'serializer_rest': [
227 include('common'),
228 (r'=', Punctuation, 'serialization_pattern'),
229 (r';', Punctuation, '#pop'),
230 default('operation'),
231 ],
232 'serialization_pattern': [
233 include('common'),
234 (_identifier, Name.Variable, '#pop'),
235 (r'\{', Punctuation, 'serialization_pattern_map'),
236 (r'\[', Punctuation, 'serialization_pattern_list'),
237 ],
238 'serialization_pattern_map': [
239 include('common'),
240 (words(('getter', 'inherit', 'attribute'),
241 suffix=_keyword_suffix), Keyword),
242 (r',', Punctuation),
243 (_identifier, Name.Variable),
244 (r'\}', Punctuation, '#pop:2'),
245 ],
246 'serialization_pattern_list': [
247 include('common'),
248 (words(('getter', 'attribute'), suffix=_keyword_suffix), Keyword),
249 (r',', Punctuation),
250 (_identifier, Name.Variable),
251 (r']', Punctuation, '#pop:2'),
252 ],
253 'enum_rest': [
254 include('common'),
255 (_identifier, Name.Class),
256 (r'\{', Punctuation, 'enum_body'),
257 (r';', Punctuation, '#pop'),
258 ],
259 'enum_body': [
260 include('common'),
261 (_string, String),
262 (r',', Punctuation),
263 (r'\}', Punctuation, '#pop'),
264 ],
265 'callback_rest': [
266 include('common'),
267 (r'interface' + _keyword_suffix,
268 Keyword, ('#pop', 'interface_rest')),
269 (_identifier, Name.Class),
270 (r'=', Punctuation, ('operation', 'type')),
271 (r';', Punctuation, '#pop'),
272 ],
273 'dictionary_rest': [
274 include('common'),
275 (_identifier, Name.Class),
276 (r':', Punctuation),
277 (r'\{', Punctuation, 'dictionary_body'),
278 (r';', Punctuation, '#pop'),
279 ],
280 'dictionary_body': [
281 include('common'),
282 (r'\[', Punctuation, 'extended_attributes'),
283 (r'required' + _keyword_suffix, Keyword),
284 (r'\}', Punctuation, '#pop'),
285 default(('dictionary_item', 'type')),
286 ],
287 'dictionary_item': [
288 include('common'),
289 (_identifier, Name.Variable),
290 (r'=', Punctuation, 'default_value'),
291 (r';', Punctuation, '#pop'),
292 ],
293 'implements_rest': [
294 include('common'),
295 (r'implements' + _keyword_suffix, Keyword),
296 (_identifier, Name.Class),
297 (r';', Punctuation, '#pop'),
298 ],
299 }