webidl.py 10 KB

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