wgsl.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. /*!-----------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Version: 0.38.0(0e330ae453813de4e6cf272460fb79c7117073d0)
  4. * Released under the MIT license
  5. * https://github.com/microsoft/monaco-editor/blob/main/LICENSE.txt
  6. *-----------------------------------------------------------------------------*/
  7. // src/basic-languages/wgsl/wgsl.ts
  8. var conf = {
  9. comments: {
  10. lineComment: "//",
  11. blockComment: ["/*", "*/"]
  12. },
  13. brackets: [
  14. ["{", "}"],
  15. ["[", "]"],
  16. ["(", ")"]
  17. ],
  18. autoClosingPairs: [
  19. { open: "[", close: "]" },
  20. { open: "{", close: "}" },
  21. { open: "(", close: ")" }
  22. ],
  23. surroundingPairs: [
  24. { open: "{", close: "}" },
  25. { open: "[", close: "]" },
  26. { open: "(", close: ")" }
  27. ]
  28. };
  29. function qw(str) {
  30. let result = [];
  31. const words = str.split(/\t+|\r+|\n+| +/);
  32. for (let i = 0; i < words.length; ++i) {
  33. if (words[i].length > 0) {
  34. result.push(words[i]);
  35. }
  36. }
  37. return result;
  38. }
  39. var atoms = qw("true false");
  40. var keywords = qw(`
  41. alias
  42. break
  43. case
  44. const
  45. const_assert
  46. continue
  47. continuing
  48. default
  49. diagnostic
  50. discard
  51. else
  52. enable
  53. fn
  54. for
  55. if
  56. let
  57. loop
  58. override
  59. requires
  60. return
  61. struct
  62. switch
  63. var
  64. while
  65. `);
  66. var reserved = qw(`
  67. NULL
  68. Self
  69. abstract
  70. active
  71. alignas
  72. alignof
  73. as
  74. asm
  75. asm_fragment
  76. async
  77. attribute
  78. auto
  79. await
  80. become
  81. binding_array
  82. cast
  83. catch
  84. class
  85. co_await
  86. co_return
  87. co_yield
  88. coherent
  89. column_major
  90. common
  91. compile
  92. compile_fragment
  93. concept
  94. const_cast
  95. consteval
  96. constexpr
  97. constinit
  98. crate
  99. debugger
  100. decltype
  101. delete
  102. demote
  103. demote_to_helper
  104. do
  105. dynamic_cast
  106. enum
  107. explicit
  108. export
  109. extends
  110. extern
  111. external
  112. fallthrough
  113. filter
  114. final
  115. finally
  116. friend
  117. from
  118. fxgroup
  119. get
  120. goto
  121. groupshared
  122. highp
  123. impl
  124. implements
  125. import
  126. inline
  127. instanceof
  128. interface
  129. layout
  130. lowp
  131. macro
  132. macro_rules
  133. match
  134. mediump
  135. meta
  136. mod
  137. module
  138. move
  139. mut
  140. mutable
  141. namespace
  142. new
  143. nil
  144. noexcept
  145. noinline
  146. nointerpolation
  147. noperspective
  148. null
  149. nullptr
  150. of
  151. operator
  152. package
  153. packoffset
  154. partition
  155. pass
  156. patch
  157. pixelfragment
  158. precise
  159. precision
  160. premerge
  161. priv
  162. protected
  163. pub
  164. public
  165. readonly
  166. ref
  167. regardless
  168. register
  169. reinterpret_cast
  170. require
  171. resource
  172. restrict
  173. self
  174. set
  175. shared
  176. sizeof
  177. smooth
  178. snorm
  179. static
  180. static_assert
  181. static_cast
  182. std
  183. subroutine
  184. super
  185. target
  186. template
  187. this
  188. thread_local
  189. throw
  190. trait
  191. try
  192. type
  193. typedef
  194. typeid
  195. typename
  196. typeof
  197. union
  198. unless
  199. unorm
  200. unsafe
  201. unsized
  202. use
  203. using
  204. varying
  205. virtual
  206. volatile
  207. wgsl
  208. where
  209. with
  210. writeonly
  211. yield
  212. `);
  213. var predeclared_enums = qw(`
  214. read write read_write
  215. function private workgroup uniform storage
  216. perspective linear flat
  217. center centroid sample
  218. vertex_index instance_index position front_facing frag_depth
  219. local_invocation_id local_invocation_index
  220. global_invocation_id workgroup_id num_workgroups
  221. sample_index sample_mask
  222. rgba8unorm
  223. rgba8snorm
  224. rgba8uint
  225. rgba8sint
  226. rgba16uint
  227. rgba16sint
  228. rgba16float
  229. r32uint
  230. r32sint
  231. r32float
  232. rg32uint
  233. rg32sint
  234. rg32float
  235. rgba32uint
  236. rgba32sint
  237. rgba32float
  238. bgra8unorm
  239. `);
  240. var predeclared_types = qw(`
  241. bool
  242. f16
  243. f32
  244. i32
  245. sampler sampler_comparison
  246. texture_depth_2d
  247. texture_depth_2d_array
  248. texture_depth_cube
  249. texture_depth_cube_array
  250. texture_depth_multisampled_2d
  251. texture_external
  252. texture_external
  253. u32
  254. `);
  255. var predeclared_type_generators = qw(`
  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. `);
  283. var predeclared_type_aliases = qw(`
  284. vec2i vec3i vec4i
  285. vec2u vec3u vec4u
  286. vec2f vec3f vec4f
  287. vec2h vec3h vec4h
  288. mat2x2f mat2x3f mat2x4f
  289. mat3x2f mat3x3f mat3x4f
  290. mat4x2f mat4x3f mat4x4f
  291. mat2x2h mat2x3h mat2x4h
  292. mat3x2h mat3x3h mat3x4h
  293. mat4x2h mat4x3h mat4x4h
  294. `);
  295. var predeclared_intrinsics = qw(`
  296. bitcast all any select arrayLength abs acos acosh asin asinh atan atanh atan2
  297. ceil clamp cos cosh countLeadingZeros countOneBits countTrailingZeros cross
  298. degrees determinant distance dot exp exp2 extractBits faceForward firstLeadingBit
  299. firstTrailingBit floor fma fract frexp inverseBits inverseSqrt ldexp length
  300. log log2 max min mix modf normalize pow quantizeToF16 radians reflect refract
  301. reverseBits round saturate sign sin sinh smoothstep sqrt step tan tanh transpose
  302. trunc dpdx dpdxCoarse dpdxFine dpdy dpdyCoarse dpdyFine fwidth fwidthCoarse fwidthFine
  303. textureDimensions textureGather textureGatherCompare textureLoad textureNumLayers
  304. textureNumLevels textureNumSamples textureSample textureSampleBias textureSampleCompare
  305. textureSampleCompareLevel textureSampleGrad textureSampleLevel textureSampleBaseClampToEdge
  306. textureStore atomicLoad atomicStore atomicAdd atomicSub atomicMax atomicMin
  307. atomicAnd atomicOr atomicXor atomicExchange atomicCompareExchangeWeak pack4x8snorm
  308. pack4x8unorm pack2x16snorm pack2x16unorm pack2x16float unpack4x8snorm unpack4x8unorm
  309. unpack2x16snorm unpack2x16unorm unpack2x16float storageBarrier workgroupBarrier
  310. workgroupUniformLoad
  311. `);
  312. var operators = qw(`
  313. &
  314. &&
  315. ->
  316. /
  317. =
  318. ==
  319. !=
  320. >
  321. >=
  322. <
  323. <=
  324. %
  325. -
  326. --
  327. +
  328. ++
  329. |
  330. ||
  331. *
  332. <<
  333. >>
  334. +=
  335. -=
  336. *=
  337. /=
  338. %=
  339. &=
  340. |=
  341. ^=
  342. >>=
  343. <<=
  344. `);
  345. var directive_re = /enable|requires|diagnostic/;
  346. var ident_re = /[_\p{XID_Start}]\p{XID_Continue}*/u;
  347. var predefined_token = "variable.predefined";
  348. var language = {
  349. tokenPostfix: ".wgsl",
  350. defaultToken: "invalid",
  351. unicode: true,
  352. atoms,
  353. keywords,
  354. reserved,
  355. predeclared_enums,
  356. predeclared_types,
  357. predeclared_type_generators,
  358. predeclared_type_aliases,
  359. predeclared_intrinsics,
  360. operators,
  361. symbols: /[!%&*+\-\.\/:;<=>^|_~]+/,
  362. tokenizer: {
  363. root: [
  364. [directive_re, "keyword", "@directive"],
  365. [
  366. ident_re,
  367. {
  368. cases: {
  369. "@atoms": predefined_token,
  370. "@keywords": "keyword",
  371. "@reserved": "invalid",
  372. "@predeclared_enums": predefined_token,
  373. "@predeclared_types": predefined_token,
  374. "@predeclared_type_generators": predefined_token,
  375. "@predeclared_type_aliases": predefined_token,
  376. "@predeclared_intrinsics": predefined_token,
  377. "@default": "identifier"
  378. }
  379. }
  380. ],
  381. { include: "@commentOrSpace" },
  382. { include: "@numbers" },
  383. [/;:\./, "delimiter"],
  384. [/,/, "delimiter"],
  385. [/[{}()\[\]]/, "@brackets"],
  386. ["@", "annotation", "@attribute"],
  387. [
  388. /@symbols/,
  389. {
  390. cases: {
  391. "@operators": "operator",
  392. "@default": "delimiter"
  393. }
  394. }
  395. ],
  396. [/./, "invalid"]
  397. ],
  398. commentOrSpace: [
  399. [/\s+/, "white"],
  400. [/\/\*/, "comment", "@blockComment"],
  401. [/\/\/.*$/, "comment"]
  402. ],
  403. blockComment: [
  404. [/[^\/*]+/, "comment"],
  405. [/\/\*/, "comment", "@push"],
  406. [/\*\//, "comment", "@pop"],
  407. [/[\/*]/, "comment"]
  408. ],
  409. attribute: [
  410. { include: "@commentOrSpace" },
  411. [/\w+/, "annotation", "@pop"]
  412. ],
  413. directive: [
  414. { include: "@commentOrSpace" },
  415. [/[()]/, "@brackets"],
  416. [/,/, "delimiter"],
  417. [ident_re, "meta.content"],
  418. [/;/, "delimiter", "@pop"]
  419. ],
  420. numbers: [
  421. [/0[fh]/, "number.float"],
  422. [/[1-9][0-9]*[fh]/, "number.float"],
  423. [/[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[fh]?/, "number.float"],
  424. [/[0-9]+\.[0-9]*([eE][+-]?[0-9]+)?[fh]?/, "number.float"],
  425. [/[0-9]+[eE][+-]?[0-9]+[fh]?/, "number.float"],
  426. [/0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+(?:[pP][+-]?[0-9]+[fh]?)?/, "number.hex"],
  427. [/0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*(?:[pP][+-]?[0-9]+[fh]?)?/, "number.hex"],
  428. [/0[xX][0-9a-fA-F]+[pP][+-]?[0-9]+[fh]?/, "number.hex"],
  429. [/0[xX][0-9a-fA-F]+[iu]?/, "number.hex"],
  430. [/[1-9][0-9]*[iu]?/, "number"],
  431. [/0[iu]?/, "number"]
  432. ]
  433. }
  434. };
  435. export {
  436. conf,
  437. language
  438. };