|
1 # -*- coding: utf-8 -*- |
|
2 """ |
|
3 pygments.lexers.webidl |
|
4 ~~~~~~~~~~~~~~~~~~~ |
|
5 |
|
6 Lexers for Web IDL, including some extensions. |
|
7 |
|
8 :copyright: Copyright 2006-2016 by the Pygments team, see AUTHORS. |
|
9 :license: BSD, see LICENSE for details. |
|
10 """ |
|
11 |
|
12 from pygments.lexer import RegexLexer, default, include, words |
|
13 from pygments.token import Comment, Keyword, Name, Number, Punctuation, \ |
|
14 String, Text |
|
15 |
|
16 __all__ = ['WebIDLLexer'] |
|
17 |
|
18 _builtin_types = ( |
|
19 # primitive types |
|
20 'byte', 'octet', 'boolean', |
|
21 r'(?:unsigned\s+)?(?:short|long(?:\s+long)?)', |
|
22 r'(?:unrestricted\s+)?(?:float|double)', |
|
23 # string types |
|
24 'DOMString', 'ByteString', 'USVString', |
|
25 # exception types |
|
26 'Error', 'DOMException', |
|
27 # typed array types |
|
28 'Uint8Array', 'Uint16Array', 'Uint32Array', 'Uint8ClampedArray', |
|
29 'Float32Array', 'Float64Array', |
|
30 # buffer source types |
|
31 'ArrayBuffer', 'DataView', 'Int8Array', 'Int16Array', 'Int32Array', |
|
32 # other |
|
33 'any', 'void', 'object', 'RegExp', |
|
34 ) |
|
35 _identifier = r'_?[A-Za-z][0-9A-Z_a-z-]*' |
|
36 _keyword_suffix = r'(?![\w-])' |
|
37 _string = r'"[^"]*"' |
|
38 |
|
39 class WebIDLLexer(RegexLexer): |
|
40 """ |
|
41 For Web IDL. |
|
42 |
|
43 .. versionadded:: 2.6 |
|
44 """ |
|
45 |
|
46 name = 'Web IDL' |
|
47 aliases = ['webidl'] |
|
48 filenames = ['*.webidl'] |
|
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, '#pop:2'), |
|
136 ], |
|
137 'default_value': [ |
|
138 include('common'), |
|
139 include('const_value'), |
|
140 (_string, String, '#pop'), |
|
141 (r'\[\s*\]', Punctuation, '#pop'), |
|
142 ], |
|
143 'const_value': [ |
|
144 include('common'), |
|
145 (words(('true', 'false', '-Infinity', 'Infinity', 'NaN', 'null'), |
|
146 suffix=_keyword_suffix), Keyword.Constant, '#pop'), |
|
147 (r'-?(?:(?:[0-9]+\.[0-9]*|[0-9]*\.[0-9]+)(?:[Ee][+-]?[0-9]+)?' + |
|
148 r'|[0-9]+[Ee][+-]?[0-9]+)', Number.Float, '#pop'), |
|
149 (r'-?[1-9][0-9]*', Number.Integer, '#pop'), |
|
150 (r'-?0[Xx][0-9A-Fa-f]+', Number.Hex, '#pop'), |
|
151 (r'-?0[0-7]*', Number.Oct, '#pop'), |
|
152 ], |
|
153 'typedef': [ |
|
154 include('common'), |
|
155 (_identifier, Name.Class), |
|
156 (r';', Punctuation, '#pop'), |
|
157 ], |
|
158 'namespace_rest': [ |
|
159 include('common'), |
|
160 (_identifier, Name.Namespace), |
|
161 (r'\{', Punctuation, 'namespace_body'), |
|
162 (r';', Punctuation, '#pop'), |
|
163 ], |
|
164 'namespace_body': [ |
|
165 include('common'), |
|
166 (r'\[', Punctuation, 'extended_attributes'), |
|
167 (r'readonly' + _keyword_suffix, Keyword), |
|
168 (r'attribute' + _keyword_suffix, |
|
169 Keyword, ('attribute_rest', 'type')), |
|
170 (r'const' + _keyword_suffix, Keyword, ('const_rest', 'type')), |
|
171 (r'\}', Punctuation, '#pop'), |
|
172 default(('operation_rest', 'type')), |
|
173 ], |
|
174 'interface_rest': [ |
|
175 include('common'), |
|
176 (_identifier, Name.Class), |
|
177 (r':', Punctuation), |
|
178 (r'\{', Punctuation, 'interface_body'), |
|
179 (r';', Punctuation, '#pop'), |
|
180 ], |
|
181 'interface_body': [ |
|
182 (words(('iterable', 'maplike', 'setlike'), suffix=_keyword_suffix), |
|
183 Keyword, 'iterable_maplike_setlike_rest'), |
|
184 (words(('setter', 'getter', 'creator', 'deleter', 'legacycaller', |
|
185 'inherit', 'static', 'stringifier', 'jsonifier'), |
|
186 suffix=_keyword_suffix), Keyword), |
|
187 (r'serializer' + _keyword_suffix, Keyword, 'serializer_rest'), |
|
188 (r';', Punctuation), |
|
189 include('namespace_body'), |
|
190 ], |
|
191 'attribute_rest': [ |
|
192 include('common'), |
|
193 (_identifier, Name.Variable), |
|
194 (r';', Punctuation, '#pop'), |
|
195 ], |
|
196 'const_rest': [ |
|
197 include('common'), |
|
198 (_identifier, Name.Constant), |
|
199 (r'=', Punctuation, 'const_value'), |
|
200 (r';', Punctuation, '#pop'), |
|
201 ], |
|
202 'operation_rest': [ |
|
203 include('common'), |
|
204 (r';', Punctuation, '#pop'), |
|
205 default('operation'), |
|
206 ], |
|
207 'operation': [ |
|
208 include('common'), |
|
209 (_identifier, Name.Function), |
|
210 (r'\(', Punctuation, 'argument_list'), |
|
211 (r';', Punctuation, '#pop:2'), |
|
212 ], |
|
213 'iterable_maplike_setlike_rest': [ |
|
214 include('common'), |
|
215 (r'<', Punctuation, 'type_list'), |
|
216 (r';', Punctuation, '#pop'), |
|
217 ], |
|
218 'type_list': [ |
|
219 include('common'), |
|
220 (r',', Punctuation), |
|
221 (r'>', Punctuation, '#pop'), |
|
222 default('type'), |
|
223 ], |
|
224 'serializer_rest': [ |
|
225 include('common'), |
|
226 (r'=', Punctuation, 'serialization_pattern'), |
|
227 (r';', Punctuation, '#pop'), |
|
228 default('operation'), |
|
229 ], |
|
230 'serialization_pattern': [ |
|
231 include('common'), |
|
232 (_identifier, Name.Variable, '#pop'), |
|
233 (r'\{', Punctuation, 'serialization_pattern_map'), |
|
234 (r'\[', Punctuation, 'serialization_pattern_list'), |
|
235 ], |
|
236 'serialization_pattern_map': [ |
|
237 include('common'), |
|
238 (words(('getter', 'inherit', 'attribute'), |
|
239 suffix=_keyword_suffix), Keyword), |
|
240 (r',', Punctuation), |
|
241 (_identifier, Name.Variable), |
|
242 (r'\}', Punctuation, '#pop:2'), |
|
243 ], |
|
244 'serialization_pattern_list': [ |
|
245 include('common'), |
|
246 (words(('getter', 'attribute'), suffix=_keyword_suffix), Keyword), |
|
247 (r',', Punctuation), |
|
248 (_identifier, Name.Variable), |
|
249 (r']', Punctuation, '#pop:2'), |
|
250 ], |
|
251 'enum_rest': [ |
|
252 include('common'), |
|
253 (_identifier, Name.Class), |
|
254 (r'\{', Punctuation, 'enum_body'), |
|
255 (r';', Punctuation, '#pop'), |
|
256 ], |
|
257 'enum_body': [ |
|
258 include('common'), |
|
259 (_string, String), |
|
260 (r',', Punctuation), |
|
261 (r'\}', Punctuation, '#pop'), |
|
262 ], |
|
263 'callback_rest': [ |
|
264 include('common'), |
|
265 (r'interface' + _keyword_suffix, |
|
266 Keyword, ('#pop', 'interface_rest')), |
|
267 (_identifier, Name.Class), |
|
268 (r'=', Punctuation, ('operation', 'type')), |
|
269 (r';', Punctuation, '#pop'), |
|
270 ], |
|
271 'dictionary_rest': [ |
|
272 include('common'), |
|
273 (_identifier, Name.Class), |
|
274 (r':', Punctuation), |
|
275 (r'\{', Punctuation, 'dictionary_body'), |
|
276 (r';', Punctuation, '#pop'), |
|
277 ], |
|
278 'dictionary_body': [ |
|
279 include('common'), |
|
280 (r'\[', Punctuation, 'extended_attributes'), |
|
281 (r'required' + _keyword_suffix, Keyword), |
|
282 (r'\}', Punctuation, '#pop'), |
|
283 default(('dictionary_item', 'type')), |
|
284 ], |
|
285 'dictionary_item': [ |
|
286 include('common'), |
|
287 (_identifier, Name.Variable), |
|
288 (r'=', Punctuation, 'default_value'), |
|
289 (r';', Punctuation, '#pop'), |
|
290 ], |
|
291 'implements_rest': [ |
|
292 include('common'), |
|
293 (r'implements' + _keyword_suffix, Keyword), |
|
294 (_identifier, Name.Class), |
|
295 (r';', Punctuation, '#pop'), |
|
296 ], |
|
297 } |