Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/lexers/wgsl.py: 100%

30 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:16 +0000

1""" 

2 pygments.lexers.wgsl 

3 ~~~~~~~~~~~~~~~~~~~~ 

4 

5 Lexer for the WebGPU Shading Language. 

6 

7 :copyright: Copyright 2006-2023 by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11from pygments.lexer import RegexLexer, include, bygroups, words, default 

12from pygments.token import Comment, Operator, Keyword, Name, \ 

13 Number, Punctuation, Whitespace 

14from pygments import unistring as uni 

15 

16__all__ = ['WgslLexer'] 

17 

18LF = '\\u000a' 

19VT = '\\u000b' 

20FF = '\\u000c' 

21CR = '\\u000d' 

22NextLine = '\\u0085' 

23LineSep = '\\u2028' 

24ParaSep = '\\u2029' 

25LineEndCodePoints = [LF,VT,FF,CR,NextLine,LineSep,ParaSep] 

26NotLineEndRE = '[^' + "".join(LineEndCodePoints) + ']' 

27LineEndRE = '[' + "".join(LineEndCodePoints) + ']' 

28 

29# https://www.w3.org/TR/WGSL/#syntax-ident_pattern_token 

30ident_pattern_token = '([{}][{}]+)|[{}]'.format(uni.xid_start,uni.xid_continue,uni.xid_start) 

31 

32 

33class WgslLexer(RegexLexer): 

34 """ 

35 Lexer for the WebGPU Shading Language. 

36 

37 .. versionadded:: 2.15 

38 """ 

39 name = 'WebGPU Shading Language' 

40 url = 'https://www.w3.org/TR/WGSL/' 

41 aliases = ['wgsl'] 

42 filenames = ['*.wgsl'] 

43 mimetypes = ['text/wgsl'] 

44 

45 # https://www.w3.org/TR/WGSL/#var-and-value 

46 keyword_decl = (words('var let const override'.split(),suffix=r'\b'), Keyword.Declaration) 

47 # https://www.w3.org/TR/WGSL/#keyword-summary 

48 keywords = (words(""" 

49 alias 

50 break 

51 case 

52 const_assert 

53 continue 

54 continuing 

55 default 

56 diagnostic 

57 discard 

58 else 

59 enable 

60 false 

61 fn 

62 for 

63 if 

64 loop 

65 requires 

66 return 

67 struct 

68 switch 

69 true 

70 while 

71 """.split(), suffix=r'\b'), Keyword) 

72 

73 # https://www.w3.org/TR/WGSL/#reserved-words 

74 keyword_reserved = (words(""" 

75 NULL 

76 Self 

77 abstract 

78 active 

79 alignas 

80 alignof 

81 as 

82 asm 

83 asm_fragment 

84 async 

85 attribute 

86 auto 

87 await 

88 become 

89 binding_array 

90 cast 

91 catch 

92 class 

93 co_await 

94 co_return 

95 co_yield 

96 coherent 

97 column_major 

98 common 

99 compile 

100 compile_fragment 

101 concept 

102 const_cast 

103 consteval 

104 constexpr 

105 constinit 

106 crate 

107 debugger 

108 decltype 

109 delete 

110 demote 

111 demote_to_helper 

112 do 

113 dynamic_cast 

114 enum 

115 explicit 

116 export 

117 extends 

118 extern 

119 external 

120 fallthrough 

121 filter 

122 final 

123 finally 

124 friend 

125 from 

126 fxgroup 

127 get 

128 goto 

129 groupshared 

130 highp 

131 impl 

132 implements 

133 import 

134 inline 

135 instanceof 

136 interface 

137 layout 

138 lowp 

139 macro 

140 macro_rules 

141 match 

142 mediump 

143 meta 

144 mod 

145 module 

146 move 

147 mut 

148 mutable 

149 namespace 

150 new 

151 nil 

152 noexcept 

153 noinline 

154 nointerpolation 

155 noperspective 

156 null 

157 nullptr 

158 of 

159 operator 

160 package 

161 packoffset 

162 partition 

163 pass 

164 patch 

165 pixelfragment 

166 precise 

167 precision 

168 premerge 

169 priv 

170 protected 

171 pub 

172 public 

173 readonly 

174 ref 

175 regardless 

176 register 

177 reinterpret_cast 

178 require 

179 resource 

180 restrict 

181 self 

182 set 

183 shared 

184 sizeof 

185 smooth 

186 snorm 

187 static 

188 static_assert 

189 static_cast 

190 std 

191 subroutine 

192 super 

193 target 

194 template 

195 this 

196 thread_local 

197 throw 

198 trait 

199 try 

200 type 

201 typedef 

202 typeid 

203 typename 

204 typeof 

205 union 

206 unless 

207 unorm 

208 unsafe 

209 unsized 

210 use 

211 using 

212 varying 

213 virtual 

214 volatile 

215 wgsl 

216 where 

217 with 

218 writeonly 

219 yield 

220 """.split(), suffix=r'\b'), Keyword.Reserved) 

221 

222 # https://www.w3.org/TR/WGSL/#predeclared-enumerants 

223 predeclared_enums = (words(""" 

224 read write read_write 

225 function private workgroup uniform storage 

226 perspective linear flat 

227 center centroid sample 

228 vertex_index instance_index position front_facing frag_depth 

229 local_invocation_id local_invocation_index 

230 global_invocation_id workgroup_id num_workgroups 

231 sample_index sample_mask 

232 rgba8unorm 

233 rgba8snorm 

234 rgba8uint 

235 rgba8sint 

236 rgba16uint 

237 rgba16sint 

238 rgba16float 

239 r32uint 

240 r32sint 

241 r32float 

242 rg32uint 

243 rg32sint 

244 rg32float 

245 rgba32uint 

246 rgba32sint 

247 rgba32float 

248 bgra8unorm 

249 """.split(), suffix=r'\b'), Name.Builtin) 

250 

251 # https://www.w3.org/TR/WGSL/#predeclared-types 

252 predeclared_types = (words(""" 

253 bool 

254 f16 

255 f32 

256 i32 

257 sampler sampler_comparison 

258 texture_depth_2d 

259 texture_depth_2d_array 

260 texture_depth_cube 

261 texture_depth_cube_array 

262 texture_depth_multisampled_2d 

263 texture_external 

264 texture_external 

265 u32 

266 """.split(), suffix=r'\b'), Name.Builtin) 

267 

268 # https://www.w3.org/TR/WGSL/#predeclared-types 

269 predeclared_type_generators = (words(""" 

270 array 

271 atomic 

272 mat2x2 

273 mat2x3 

274 mat2x4 

275 mat3x2 

276 mat3x3 

277 mat3x4 

278 mat4x2 

279 mat4x3 

280 mat4x4 

281 ptr 

282 texture_1d 

283 texture_2d 

284 texture_2d_array 

285 texture_3d 

286 texture_cube 

287 texture_cube_array 

288 texture_multisampled_2d 

289 texture_storage_1d 

290 texture_storage_2d 

291 texture_storage_2d_array 

292 texture_storage_3d 

293 vec2 

294 vec3 

295 vec4 

296 """.split(), suffix=r'\b'), Name.Builtin) 

297 

298 # Predeclared type aliases for vectors 

299 # https://www.w3.org/TR/WGSL/#vector-types 

300 predeclared_type_alias_vectors = (words(""" 

301 vec2i vec3i vec4i 

302 vec2u vec3u vec4u 

303 vec2f vec3f vec4f 

304 vec2h vec3h vec4h 

305 """.split(), suffix=r'\b'), Name.Builtin) 

306 

307 # Predeclared type aliases for matrices 

308 # https://www.w3.org/TR/WGSL/#matrix-types 

309 predeclared_type_alias_matrices = (words(""" 

310 mat2x2f mat2x3f mat2x4f 

311 mat3x2f mat3x3f mat3x4f 

312 mat4x2f mat4x3f mat4x4f 

313 mat2x2h mat2x3h mat2x4h 

314 mat3x2h mat3x3h mat3x4h 

315 mat4x2h mat4x3h mat4x4h 

316 """.split(), suffix=r'\b'), Name.Builtin) 

317 

318 tokens = { 

319 'blankspace': [ 

320 # https://www.w3.org/TR/WGSL/#blankspace 

321 (r'[\u0020\u0009\u000a\u000b\u000c\u000d\u0085\u200e\u200f\u2028\u2029]+', Whitespace), 

322 ], 

323 'comments': [ 

324 # Line ending comments 

325 # Match up CR/LF pair first. 

326 (r'//{}*{}{}'.format(NotLineEndRE,CR,LF), Comment.Single), 

327 (r'//{}*{}'.format(NotLineEndRE,LineEndRE), Comment.Single), 

328 (r'/\*', Comment.Multiline, 'block_comment'), 

329 ], 

330 'attribute': [ 

331 include('blankspace'), 

332 include('comments'), 

333 (ident_pattern_token, Name.Decorator,'#pop'), 

334 default('#pop'), 

335 ], 

336 'root': [ 

337 include('blankspace'), 

338 include('comments'), 

339 

340 # Attributes. 

341 # https://www.w3.org/TR/WGSL/#attributes 

342 # Mark the '@' and the attribute name as a decorator. 

343 (r'@', Name.Decorator, 'attribute'), 

344 

345 # Keywords 

346 (r'(true|false)\b', Keyword.Constant), 

347 keyword_decl, 

348 keywords, 

349 keyword_reserved, 

350 

351 # Predeclared 

352 predeclared_enums, 

353 predeclared_types, 

354 predeclared_type_generators, 

355 predeclared_type_alias_vectors, 

356 predeclared_type_alias_matrices, 

357 

358 # Decimal float literals 

359 # https://www.w3.org/TR/WGSL/#syntax-decimal_float_literal 

360 # 0, with type-specifying suffix. 

361 (r'0[fh]', Number.Float), 

362 # Other decimal integer, with type-specifying suffix. 

363 (r'[1-9][0-9]*[fh]', Number.Float), 

364 # Has decimal point, at least one digit after decimal. 

365 (r'[0-9]*\.[0-9]+([eE][+-]?[0-9]+)?[fh]?', Number.Float), 

366 # Has decimal point, at least one digit before decimal. 

367 (r'[0-9]+\.[0-9]*([eE][+-]?[0-9]+)?[fh]?', Number.Float), 

368 # Has at least one digit, and has an exponent. 

369 (r'[0-9]+[eE][+-]?[0-9]+[fh]?', Number.Float), 

370 

371 # Hex float literals 

372 # https://www.w3.org/TR/WGSL/#syntax-hex_float_literal 

373 (r'0[xX][0-9a-fA-F]*\.[0-9a-fA-F]+([pP][+-]?[0-9]+[fh]?)?', Number.Float), 

374 (r'0[xX][0-9a-fA-F]+\.[0-9a-fA-F]*([pP][+-]?[0-9]+[fh]?)?', Number.Float), 

375 (r'0[xX][0-9a-fA-F]+[pP][+-]?[0-9]+[fh]?', Number.Float), 

376 

377 # Hexadecimal integer literals 

378 # https://www.w3.org/TR/WGSL/#syntax-hex_int_literal 

379 (r'0[xX][0-9a-fA-F]+[iu]?', Number.Hex), 

380 # Decimal integer literals 

381 # https://www.w3.org/TR/WGSL/#syntax-decimal_int_literal 

382 # We need two rules here because 01 is not valid. 

383 (r'[1-9][0-9]*[iu]?', Number.Integer), 

384 (r'0[iu]?', Number.Integer), # Must match last. 

385 

386 # Operators and Punctuation 

387 (r'[{}()\[\],\.;:]', Punctuation), 

388 (r'->', Punctuation), # Return-type arrow 

389 (r'[+\-*/%&|<>^!~=]', Operator), 

390 

391 # TODO: Treat context-depedendent names specially 

392 # https://www.w3.org/TR/WGSL/#context-dependent-name 

393 

394 # Identifiers 

395 (ident_pattern_token, Name), 

396 

397 # TODO: templates start and end tokens. 

398 # https://www.w3.org/TR/WGSL/#template-lists-sec 

399 ], 

400 'block_comment': [ 

401 # https://www.w3.org/TR/WGSL/#block-comment 

402 (r'[^*/]+', Comment.Multiline), 

403 (r'/\*', Comment.Multiline, '#push'), 

404 (r'\*/', Comment.Multiline, '#pop'), 

405 (r'[*/]', Comment.Multiline), 

406 ], 

407 }