wgsl.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. """
  2. pygments.lexers.wgsl
  3. ~~~~~~~~~~~~~~~~~~~~
  4. Lexer for the WebGPU Shading Language.
  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, include, bygroups, words, default
  9. from pygments.token import Comment, Operator, Keyword, Name, \
  10. Number, Punctuation, Whitespace
  11. from pygments import unistring as uni
  12. __all__ = ['WgslLexer']
  13. LF = '\\u000a'
  14. VT = '\\u000b'
  15. FF = '\\u000c'
  16. CR = '\\u000d'
  17. NextLine = '\\u0085'
  18. LineSep = '\\u2028'
  19. ParaSep = '\\u2029'
  20. LineEndCodePoints = [LF,VT,FF,CR,NextLine,LineSep,ParaSep]
  21. NotLineEndRE = '[^' + "".join(LineEndCodePoints) + ']'
  22. LineEndRE = '[' + "".join(LineEndCodePoints) + ']'
  23. # https://www.w3.org/TR/WGSL/#syntax-ident_pattern_token
  24. ident_pattern_token = '([{}][{}]+)|[{}]'.format(uni.xid_start,uni.xid_continue,uni.xid_start)
  25. class WgslLexer(RegexLexer):
  26. """
  27. Lexer for the WebGPU Shading Language.
  28. .. versionadded:: 2.15
  29. """
  30. name = 'WebGPU Shading Language'
  31. url = 'https://www.w3.org/TR/WGSL/'
  32. aliases = ['wgsl']
  33. filenames = ['*.wgsl']
  34. mimetypes = ['text/wgsl']
  35. # https://www.w3.org/TR/WGSL/#var-and-value
  36. keyword_decl = (words('var let const override'.split(),suffix=r'\b'), Keyword.Declaration)
  37. # https://www.w3.org/TR/WGSL/#keyword-summary
  38. keywords = (words("""
  39. alias
  40. break
  41. case
  42. const_assert
  43. continue
  44. continuing
  45. default
  46. diagnostic
  47. discard
  48. else
  49. enable
  50. false
  51. fn
  52. for
  53. if
  54. loop
  55. requires
  56. return
  57. struct
  58. switch
  59. true
  60. while
  61. """.split(), suffix=r'\b'), Keyword)
  62. # https://www.w3.org/TR/WGSL/#reserved-words
  63. keyword_reserved = (words("""
  64. NULL
  65. Self
  66. abstract
  67. active
  68. alignas
  69. alignof
  70. as
  71. asm
  72. asm_fragment
  73. async
  74. attribute
  75. auto
  76. await
  77. become
  78. binding_array
  79. cast
  80. catch
  81. class
  82. co_await
  83. co_return
  84. co_yield
  85. coherent
  86. column_major
  87. common
  88. compile
  89. compile_fragment
  90. concept
  91. const_cast
  92. consteval
  93. constexpr
  94. constinit
  95. crate
  96. debugger
  97. decltype
  98. delete
  99. demote
  100. demote_to_helper
  101. do
  102. dynamic_cast
  103. enum
  104. explicit
  105. export
  106. extends
  107. extern
  108. external
  109. fallthrough
  110. filter
  111. final
  112. finally
  113. friend
  114. from
  115. fxgroup
  116. get
  117. goto
  118. groupshared
  119. highp
  120. impl
  121. implements
  122. import
  123. inline
  124. instanceof
  125. interface
  126. layout
  127. lowp
  128. macro
  129. macro_rules
  130. match
  131. mediump
  132. meta
  133. mod
  134. module
  135. move
  136. mut
  137. mutable
  138. namespace
  139. new
  140. nil
  141. noexcept
  142. noinline
  143. nointerpolation
  144. noperspective
  145. null
  146. nullptr
  147. of
  148. operator
  149. package
  150. packoffset
  151. partition
  152. pass
  153. patch
  154. pixelfragment
  155. precise
  156. precision
  157. premerge
  158. priv
  159. protected
  160. pub
  161. public
  162. readonly
  163. ref
  164. regardless
  165. register
  166. reinterpret_cast
  167. require
  168. resource
  169. restrict
  170. self
  171. set
  172. shared
  173. sizeof
  174. smooth
  175. snorm
  176. static
  177. static_assert
  178. static_cast
  179. std
  180. subroutine
  181. super
  182. target
  183. template
  184. this
  185. thread_local
  186. throw
  187. trait
  188. try
  189. type
  190. typedef
  191. typeid
  192. typename
  193. typeof
  194. union
  195. unless
  196. unorm
  197. unsafe
  198. unsized
  199. use
  200. using
  201. varying
  202. virtual
  203. volatile
  204. wgsl
  205. where
  206. with
  207. writeonly
  208. yield
  209. """.split(), suffix=r'\b'), Keyword.Reserved)
  210. # https://www.w3.org/TR/WGSL/#predeclared-enumerants
  211. predeclared_enums = (words("""
  212. read write read_write
  213. function private workgroup uniform storage
  214. perspective linear flat
  215. center centroid sample
  216. vertex_index instance_index position front_facing frag_depth
  217. local_invocation_id local_invocation_index
  218. global_invocation_id workgroup_id num_workgroups
  219. sample_index sample_mask
  220. rgba8unorm
  221. rgba8snorm
  222. rgba8uint
  223. rgba8sint
  224. rgba16uint
  225. rgba16sint
  226. rgba16float
  227. r32uint
  228. r32sint
  229. r32float
  230. rg32uint
  231. rg32sint
  232. rg32float
  233. rgba32uint
  234. rgba32sint
  235. rgba32float
  236. bgra8unorm
  237. """.split(), suffix=r'\b'), Name.Builtin)
  238. # https://www.w3.org/TR/WGSL/#predeclared-types
  239. predeclared_types = (words("""
  240. bool
  241. f16
  242. f32
  243. i32
  244. sampler sampler_comparison
  245. texture_depth_2d
  246. texture_depth_2d_array
  247. texture_depth_cube
  248. texture_depth_cube_array
  249. texture_depth_multisampled_2d
  250. texture_external
  251. texture_external
  252. u32
  253. """.split(), suffix=r'\b'), Name.Builtin)
  254. # https://www.w3.org/TR/WGSL/#predeclared-types
  255. predeclared_type_generators = (words("""
  256. array
  257. atomic
  258. mat2x2
  259. mat2x3
  260. mat2x4
  261. mat3x2
  262. mat3x3
  263. mat3x4
  264. mat4x2
  265. mat4x3
  266. mat4x4
  267. ptr
  268. texture_1d
  269. texture_2d
  270. texture_2d_array
  271. texture_3d
  272. texture_cube
  273. texture_cube_array
  274. texture_multisampled_2d
  275. texture_storage_1d
  276. texture_storage_2d
  277. texture_storage_2d_array
  278. texture_storage_3d
  279. vec2
  280. vec3
  281. vec4
  282. """.split(), suffix=r'\b'), Name.Builtin)
  283. # Predeclared type aliases for vectors
  284. # https://www.w3.org/TR/WGSL/#vector-types
  285. predeclared_type_alias_vectors = (words("""
  286. vec2i vec3i vec4i
  287. vec2u vec3u vec4u
  288. vec2f vec3f vec4f
  289. vec2h vec3h vec4h
  290. """.split(), suffix=r'\b'), Name.Builtin)
  291. # Predeclared type aliases for matrices
  292. # https://www.w3.org/TR/WGSL/#matrix-types
  293. predeclared_type_alias_matrices = (words("""
  294. mat2x2f mat2x3f mat2x4f
  295. mat3x2f mat3x3f mat3x4f
  296. mat4x2f mat4x3f mat4x4f
  297. mat2x2h mat2x3h mat2x4h
  298. mat3x2h mat3x3h mat3x4h
  299. mat4x2h mat4x3h mat4x4h
  300. """.split(), suffix=r'\b'), Name.Builtin)
  301. tokens = {
  302. 'blankspace': [
  303. # https://www.w3.org/TR/WGSL/#blankspace
  304. (r'[\u0020\u0009\u000a\u000b\u000c\u000d\u0085\u200e\u200f\u2028\u2029]+', Whitespace),
  305. ],
  306. 'comments': [
  307. # Line ending comments
  308. # Match up CR/LF pair first.
  309. (r'//{}*{}{}'.format(NotLineEndRE,CR,LF), Comment.Single),
  310. (r'//{}*{}'.format(NotLineEndRE,LineEndRE), Comment.Single),
  311. (r'/\*', Comment.Multiline, 'block_comment'),
  312. ],
  313. 'attribute': [
  314. include('blankspace'),
  315. include('comments'),
  316. (ident_pattern_token, Name.Decorator,'#pop'),
  317. default('#pop'),
  318. ],
  319. 'root': [
  320. include('blankspace'),
  321. include('comments'),
  322. # Attributes.
  323. # https://www.w3.org/TR/WGSL/#attributes
  324. # Mark the '@' and the attribute name as a decorator.
  325. (r'@', Name.Decorator, 'attribute'),
  326. # Keywords
  327. (r'(true|false)\b', Keyword.Constant),
  328. keyword_decl,
  329. keywords,
  330. keyword_reserved,
  331. # Predeclared
  332. predeclared_enums,
  333. predeclared_types,
  334. predeclared_type_generators,
  335. predeclared_type_alias_vectors,
  336. predeclared_type_alias_matrices,
  337. # Decimal float literals
  338. # https://www.w3.org/TR/WGSL/#syntax-decimal_float_literal
  339. # 0, with type-specifying suffix.
  340. (r'0[fh]', Number.Float),
  341. # Other decimal integer, with type-specifying suffix.
  342. (r'[1-9][0-9]*[fh]', Number.Float),
  343. # Has decimal point, at least one digit after decimal.
  344. (r'[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[fh]?', Number.Float),
  345. # Has decimal point, at least one digit before decimal.
  346. (r'[0-9]+\.[0-9]*([eE][+-]?[0-9]+)?[fh]?', Number.Float),
  347. # Has at least one digit, and has an exponent.
  348. (r'[0-9]+[eE][+-]?[0-9]+[fh]?', Number.Float),
  349. # Hex float literals
  350. # https://www.w3.org/TR/WGSL/#syntax-hex_float_literal
  351. (r'0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+([pP][+-]?[0-9]+[fh]?)?', Number.Float),
  352. (r'0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*([pP][+-]?[0-9]+[fh]?)?', Number.Float),
  353. (r'0[xX][0-9a-fA-F]+[pP][+-]?[0-9]+[fh]?', Number.Float),
  354. # Hexadecimal integer literals
  355. # https://www.w3.org/TR/WGSL/#syntax-hex_int_literal
  356. (r'0[xX][0-9a-fA-F]+[iu]?', Number.Hex),
  357. # Decimal integer literals
  358. # https://www.w3.org/TR/WGSL/#syntax-decimal_int_literal
  359. # We need two rules here because 01 is not valid.
  360. (r'[1-9][0-9]*[iu]?', Number.Integer),
  361. (r'0[iu]?', Number.Integer), # Must match last.
  362. # Operators and Punctuation
  363. (r'[{}()\[\],\.;:]', Punctuation),
  364. (r'->', Punctuation), # Return-type arrow
  365. (r'[+\-*/%&|<>^!~=]', Operator),
  366. # TODO: Treat context-depedendent names specially
  367. # https://www.w3.org/TR/WGSL/#context-dependent-name
  368. # Identifiers
  369. (ident_pattern_token, Name),
  370. # TODO: templates start and end tokens.
  371. # https://www.w3.org/TR/WGSL/#template-lists-sec
  372. ],
  373. 'block_comment': [
  374. # https://www.w3.org/TR/WGSL/#block-comment
  375. (r'[^*/]+', Comment.Multiline),
  376. (r'/\*', Comment.Multiline, '#push'),
  377. (r'\*/', Comment.Multiline, '#pop'),
  378. (r'[*/]', Comment.Multiline),
  379. ],
  380. }