webidl.py 10 KB

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