semantifold 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (303) hide show
  1. package/README.md +61 -15
  2. package/build/index.d.ts +52 -2
  3. package/build/index.d.ts.map +1 -1
  4. package/build/index.js +60 -8
  5. package/build/src/artifact-path.d.ts +23 -1
  6. package/build/src/artifact-path.d.ts.map +1 -1
  7. package/build/src/artifact-path.js +45 -3
  8. package/build/src/artifacts.d.ts.map +1 -1
  9. package/build/src/artifacts.js +66 -1
  10. package/build/src/backends/android.d.ts +49 -0
  11. package/build/src/backends/android.d.ts.map +1 -0
  12. package/build/src/backends/android.js +1274 -0
  13. package/build/src/backends/c-validation.d.ts.map +1 -1
  14. package/build/src/backends/c-validation.js +7 -3
  15. package/build/src/backends/cpp-validation.d.ts.map +1 -1
  16. package/build/src/backends/cpp-validation.js +7 -3
  17. package/build/src/backends/csharp.js +7 -1
  18. package/build/src/backends/dart-runtime.d.ts +2 -0
  19. package/build/src/backends/dart-runtime.d.ts.map +1 -0
  20. package/build/src/backends/dart-runtime.js +41 -0
  21. package/build/src/backends/dart.d.ts +20 -0
  22. package/build/src/backends/dart.d.ts.map +1 -0
  23. package/build/src/backends/dart.js +321 -0
  24. package/build/src/backends/effects.d.ts +60 -0
  25. package/build/src/backends/effects.d.ts.map +1 -0
  26. package/build/src/backends/effects.js +746 -0
  27. package/build/src/backends/go.js +18 -4
  28. package/build/src/backends/identifiers.d.ts +36 -4
  29. package/build/src/backends/identifiers.d.ts.map +1 -1
  30. package/build/src/backends/identifiers.js +160 -10
  31. package/build/src/backends/index.d.ts.map +1 -1
  32. package/build/src/backends/index.js +27 -4
  33. package/build/src/backends/ios.d.ts +68 -0
  34. package/build/src/backends/ios.d.ts.map +1 -0
  35. package/build/src/backends/ios.js +1503 -0
  36. package/build/src/backends/java.d.ts.map +1 -1
  37. package/build/src/backends/java.js +429 -36
  38. package/build/src/backends/javascript.d.ts +6 -0
  39. package/build/src/backends/javascript.d.ts.map +1 -1
  40. package/build/src/backends/javascript.js +380 -24
  41. package/build/src/backends/kotlin-runtime.d.ts +3 -0
  42. package/build/src/backends/kotlin-runtime.d.ts.map +1 -0
  43. package/build/src/backends/kotlin-runtime.js +29 -0
  44. package/build/src/backends/kotlin.d.ts +33 -0
  45. package/build/src/backends/kotlin.d.ts.map +1 -0
  46. package/build/src/backends/kotlin.js +138 -0
  47. package/build/src/backends/native-validation.d.ts +2 -2
  48. package/build/src/backends/native-validation.d.ts.map +1 -1
  49. package/build/src/backends/native-validation.js +2 -2
  50. package/build/src/backends/ordered-expressions.d.ts.map +1 -1
  51. package/build/src/backends/ordered-expressions.js +39 -7
  52. package/build/src/backends/php-stdlib-support.d.ts +19 -0
  53. package/build/src/backends/php-stdlib-support.d.ts.map +1 -0
  54. package/build/src/backends/php-stdlib-support.js +227 -0
  55. package/build/src/backends/php.d.ts.map +1 -1
  56. package/build/src/backends/php.js +448 -33
  57. package/build/src/backends/program.d.ts +83 -0
  58. package/build/src/backends/program.d.ts.map +1 -0
  59. package/build/src/backends/program.js +1309 -0
  60. package/build/src/backends/python.js +7 -1
  61. package/build/src/backends/ruby.d.ts.map +1 -1
  62. package/build/src/backends/ruby.js +310 -24
  63. package/build/src/backends/rust-runtime.d.ts +7 -0
  64. package/build/src/backends/rust-runtime.d.ts.map +1 -0
  65. package/build/src/backends/rust-runtime.js +32 -0
  66. package/build/src/backends/rust-validation.d.ts +9 -0
  67. package/build/src/backends/rust-validation.d.ts.map +1 -0
  68. package/build/src/backends/rust-validation.js +78 -0
  69. package/build/src/backends/rust.d.ts +24 -0
  70. package/build/src/backends/rust.d.ts.map +1 -0
  71. package/build/src/backends/rust.js +344 -0
  72. package/build/src/backends/scalars.d.ts +14 -6
  73. package/build/src/backends/scalars.d.ts.map +1 -1
  74. package/build/src/backends/scalars.js +167 -17
  75. package/build/src/backends/shared.d.ts +23 -4
  76. package/build/src/backends/shared.d.ts.map +1 -1
  77. package/build/src/backends/shared.js +2174 -62
  78. package/build/src/backends/stdlib-support.d.ts +14 -0
  79. package/build/src/backends/stdlib-support.d.ts.map +1 -0
  80. package/build/src/backends/stdlib-support.js +237 -0
  81. package/build/src/backends/swift-runtime.d.ts +3 -0
  82. package/build/src/backends/swift-runtime.d.ts.map +1 -0
  83. package/build/src/backends/swift-runtime.js +14 -0
  84. package/build/src/backends/swift.d.ts +8 -0
  85. package/build/src/backends/swift.d.ts.map +1 -0
  86. package/build/src/backends/swift.js +127 -0
  87. package/build/src/backends/typescript.d.ts.map +1 -1
  88. package/build/src/backends/typescript.js +344 -24
  89. package/build/src/backends/wasm.d.ts +43 -0
  90. package/build/src/backends/wasm.d.ts.map +1 -0
  91. package/build/src/backends/wasm.js +2164 -0
  92. package/build/src/backends/writer.d.ts +255 -4
  93. package/build/src/backends/writer.d.ts.map +1 -1
  94. package/build/src/backends/writer.js +491 -3
  95. package/build/src/diagnostic.d.ts +4 -3
  96. package/build/src/diagnostic.d.ts.map +1 -1
  97. package/build/src/diagnostic.js +5 -3
  98. package/build/src/frontends/c.d.ts.map +1 -1
  99. package/build/src/frontends/c.js +5 -3
  100. package/build/src/frontends/cpp.d.ts.map +1 -1
  101. package/build/src/frontends/cpp.js +5 -3
  102. package/build/src/frontends/csharp.d.ts.map +1 -1
  103. package/build/src/frontends/csharp.js +7 -3
  104. package/build/src/frontends/dart.d.ts +10 -0
  105. package/build/src/frontends/dart.d.ts.map +1 -0
  106. package/build/src/frontends/dart.js +913 -0
  107. package/build/src/frontends/index.d.ts +3 -1
  108. package/build/src/frontends/index.d.ts.map +1 -1
  109. package/build/src/frontends/index.js +15 -5
  110. package/build/src/frontends/java.d.ts +110 -1
  111. package/build/src/frontends/java.d.ts.map +1 -1
  112. package/build/src/frontends/java.js +1725 -70
  113. package/build/src/frontends/javascript-typescript.d.ts +69 -1
  114. package/build/src/frontends/javascript-typescript.d.ts.map +1 -1
  115. package/build/src/frontends/javascript-typescript.js +1825 -93
  116. package/build/src/frontends/kotlin.d.ts +10 -0
  117. package/build/src/frontends/kotlin.d.ts.map +1 -0
  118. package/build/src/frontends/kotlin.js +685 -0
  119. package/build/src/frontends/php.d.ts +118 -1
  120. package/build/src/frontends/php.d.ts.map +1 -1
  121. package/build/src/frontends/php.js +1583 -76
  122. package/build/src/frontends/program.d.ts +74 -0
  123. package/build/src/frontends/program.d.ts.map +1 -0
  124. package/build/src/frontends/program.js +989 -0
  125. package/build/src/frontends/ruby.d.ts +68 -1
  126. package/build/src/frontends/ruby.d.ts.map +1 -1
  127. package/build/src/frontends/ruby.js +1446 -101
  128. package/build/src/frontends/rust-parser.d.ts +11 -0
  129. package/build/src/frontends/rust-parser.d.ts.map +1 -0
  130. package/build/src/frontends/rust-parser.js +15 -0
  131. package/build/src/frontends/rust.d.ts +14 -0
  132. package/build/src/frontends/rust.d.ts.map +1 -0
  133. package/build/src/frontends/rust.js +715 -0
  134. package/build/src/frontends/scalars.d.ts +10 -0
  135. package/build/src/frontends/scalars.d.ts.map +1 -1
  136. package/build/src/frontends/scalars.js +28 -2
  137. package/build/src/frontends/swift.d.ts +10 -0
  138. package/build/src/frontends/swift.d.ts.map +1 -0
  139. package/build/src/frontends/swift.js +860 -0
  140. package/build/src/frontends/types.d.ts +186 -0
  141. package/build/src/frontends/types.d.ts.map +1 -0
  142. package/build/src/frontends/types.js +634 -0
  143. package/build/src/language-registry.d.ts +46 -4
  144. package/build/src/language-registry.d.ts.map +1 -1
  145. package/build/src/language-registry.js +177 -13
  146. package/build/src/mapping.d.ts +2 -2
  147. package/build/src/mapping.d.ts.map +1 -1
  148. package/build/src/mapping.js +31 -10
  149. package/build/src/semantic/capabilities.d.ts +66 -0
  150. package/build/src/semantic/capabilities.d.ts.map +1 -0
  151. package/build/src/semantic/capabilities.js +649 -0
  152. package/build/src/semantic/effects.d.ts +26 -0
  153. package/build/src/semantic/effects.d.ts.map +1 -0
  154. package/build/src/semantic/effects.js +764 -0
  155. package/build/src/semantic/generics.d.ts +23 -0
  156. package/build/src/semantic/generics.d.ts.map +1 -0
  157. package/build/src/semantic/generics.js +61 -0
  158. package/build/src/semantic/lifetime.d.ts +37 -0
  159. package/build/src/semantic/lifetime.d.ts.map +1 -0
  160. package/build/src/semantic/lifetime.js +527 -0
  161. package/build/src/semantic/provenance.d.ts +9 -2
  162. package/build/src/semantic/provenance.d.ts.map +1 -1
  163. package/build/src/semantic/provenance.js +462 -20
  164. package/build/src/semantic/scalars.d.ts +11 -0
  165. package/build/src/semantic/scalars.d.ts.map +1 -1
  166. package/build/src/semantic/scalars.js +16 -1
  167. package/build/src/semantic/stdlib.d.ts +254 -0
  168. package/build/src/semantic/stdlib.d.ts.map +1 -0
  169. package/build/src/semantic/stdlib.js +880 -0
  170. package/build/src/semantic/types.d.ts +3108 -178
  171. package/build/src/semantic/types.d.ts.map +1 -1
  172. package/build/src/semantic/types.js +730 -15
  173. package/build/src/semantic/validate.d.ts +85 -5
  174. package/build/src/semantic/validate.d.ts.map +1 -1
  175. package/build/src/semantic/validate.js +2576 -98
  176. package/build/src/stdlib-facades.d.ts +304 -0
  177. package/build/src/stdlib-facades.d.ts.map +1 -0
  178. package/build/src/stdlib-facades.js +1167 -0
  179. package/build/src/stdlib-negotiation.d.ts +69 -0
  180. package/build/src/stdlib-negotiation.d.ts.map +1 -0
  181. package/build/src/stdlib-negotiation.js +403 -0
  182. package/build/src/stdlib-providers.d.ts +72 -0
  183. package/build/src/stdlib-providers.d.ts.map +1 -0
  184. package/build/src/stdlib-providers.js +339 -0
  185. package/build/src/toolchains.d.ts +55 -1
  186. package/build/src/toolchains.d.ts.map +1 -1
  187. package/build/src/toolchains.js +15 -4
  188. package/docs/android.md +79 -0
  189. package/docs/architecture.md +47 -13
  190. package/docs/dart.md +60 -0
  191. package/docs/goals.md +3 -3
  192. package/docs/ios.md +121 -0
  193. package/docs/kotlin.md +42 -0
  194. package/docs/language-support.md +238 -26
  195. package/docs/multi-file-modules.md +52 -0
  196. package/docs/parser-qualification.md +121 -8
  197. package/docs/plans/2026-09-05-standard-library-portability.md +10 -10
  198. package/docs/rust.md +88 -0
  199. package/docs/source-maps.md +17 -7
  200. package/docs/specification.md +5 -0
  201. package/docs/standard-library-portability.md +39 -8
  202. package/docs/swift.md +44 -0
  203. package/docs/testing.md +381 -4
  204. package/docs/webassembly.md +52 -0
  205. package/index.js +66 -8
  206. package/node_modules/semantifold-tree-sitter-legacy-internal/README.md +2 -2
  207. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/LICENSE +21 -0
  208. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/README.md +45 -0
  209. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/binding.gyp +30 -0
  210. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/bindings/node/binding.cc +20 -0
  211. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/bindings/node/binding_test.js +9 -0
  212. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/bindings/node/index.d.ts +28 -0
  213. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/bindings/node/index.js +11 -0
  214. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/grammar.js +1630 -0
  215. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/package.json +64 -0
  216. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/darwin-arm64/tree-sitter-rust.node +0 -0
  217. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/darwin-x64/tree-sitter-rust.node +0 -0
  218. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/linux-arm64/tree-sitter-rust.node +0 -0
  219. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/linux-x64/tree-sitter-rust.node +0 -0
  220. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/win32-arm64/tree-sitter-rust.node +0 -0
  221. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/prebuilds/win32-x64/tree-sitter-rust.node +0 -0
  222. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/queries/highlights.scm +159 -0
  223. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/queries/injections.scm +9 -0
  224. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/queries/tags.scm +60 -0
  225. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/grammar.json +9405 -0
  226. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/node-types.json +5399 -0
  227. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/parser.c +192569 -0
  228. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/scanner.c +393 -0
  229. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/tree_sitter/alloc.h +54 -0
  230. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/tree_sitter/array.h +290 -0
  231. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/src/tree_sitter/parser.h +266 -0
  232. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/tree-sitter-rust.wasm +0 -0
  233. package/node_modules/semantifold-tree-sitter-legacy-internal/node_modules/tree-sitter-rust/tree-sitter.json +53 -0
  234. package/node_modules/semantifold-tree-sitter-legacy-internal/package.json +3 -2
  235. package/node_modules/semantifold-tree-sitter-legacy-internal/src/c.js +5 -4
  236. package/package.json +5 -2
  237. package/src/artifact-path.js +47 -2
  238. package/src/artifacts.js +67 -0
  239. package/src/backends/android.js +1370 -0
  240. package/src/backends/c-validation.js +6 -1
  241. package/src/backends/cpp-validation.js +6 -1
  242. package/src/backends/csharp.js +3 -0
  243. package/src/backends/dart-runtime.js +41 -0
  244. package/src/backends/dart.js +326 -0
  245. package/src/backends/effects.js +705 -0
  246. package/src/backends/go.js +14 -2
  247. package/src/backends/identifiers.js +166 -9
  248. package/src/backends/index.js +27 -2
  249. package/src/backends/ios.js +1612 -0
  250. package/src/backends/java.js +454 -36
  251. package/src/backends/javascript.js +389 -23
  252. package/src/backends/kotlin-runtime.js +29 -0
  253. package/src/backends/kotlin.js +144 -0
  254. package/src/backends/native-validation.js +1 -1
  255. package/src/backends/ordered-expressions.js +40 -6
  256. package/src/backends/php-stdlib-support.js +239 -0
  257. package/src/backends/php.js +469 -32
  258. package/src/backends/program.js +1408 -0
  259. package/src/backends/python.js +3 -0
  260. package/src/backends/ruby.js +326 -23
  261. package/src/backends/rust-runtime.js +34 -0
  262. package/src/backends/rust-validation.js +75 -0
  263. package/src/backends/rust.js +335 -0
  264. package/src/backends/scalars.js +141 -16
  265. package/src/backends/shared.js +2314 -115
  266. package/src/backends/stdlib-support.js +234 -0
  267. package/src/backends/swift-runtime.js +14 -0
  268. package/src/backends/swift.js +132 -0
  269. package/src/backends/typescript.js +349 -23
  270. package/src/backends/wasm.js +2208 -0
  271. package/src/backends/writer.js +545 -2
  272. package/src/diagnostic.js +4 -2
  273. package/src/frontends/c.js +4 -2
  274. package/src/frontends/cpp.js +4 -2
  275. package/src/frontends/csharp.js +4 -2
  276. package/src/frontends/dart.js +963 -0
  277. package/src/frontends/index.js +14 -4
  278. package/src/frontends/java.js +1977 -69
  279. package/src/frontends/javascript-typescript.js +2046 -102
  280. package/src/frontends/kotlin.js +713 -0
  281. package/src/frontends/php.js +1725 -79
  282. package/src/frontends/program.js +1062 -0
  283. package/src/frontends/ruby.js +1624 -98
  284. package/src/frontends/rust-parser.js +18 -0
  285. package/src/frontends/rust.js +714 -0
  286. package/src/frontends/scalars.js +31 -1
  287. package/src/frontends/swift.js +893 -0
  288. package/src/frontends/types.js +750 -0
  289. package/src/language-registry.js +182 -12
  290. package/src/mapping.js +33 -9
  291. package/src/semantic/capabilities.js +671 -0
  292. package/src/semantic/effects.js +737 -0
  293. package/src/semantic/generics.js +58 -0
  294. package/src/semantic/lifetime.js +517 -0
  295. package/src/semantic/provenance.js +412 -19
  296. package/src/semantic/scalars.js +17 -0
  297. package/src/semantic/stdlib.js +921 -0
  298. package/src/semantic/types.js +810 -14
  299. package/src/semantic/validate.js +2934 -209
  300. package/src/stdlib-facades.js +1211 -0
  301. package/src/stdlib-negotiation.js +446 -0
  302. package/src/stdlib-providers.js +377 -0
  303. package/src/toolchains.js +16 -3
@@ -0,0 +1,1630 @@
1
+ /**
2
+ * @file Rust grammar for tree-sitter
3
+ * @author Maxim Sokolov <maxim0xff@gmail.com>
4
+ * @author Max Brunsfeld <maxbrunsfeld@gmail.com>
5
+ * @author Amaan Qureshi <amaanq12@gmail.com>
6
+ * @license MIT
7
+ */
8
+
9
+ /// <reference types="tree-sitter-cli/dsl" />
10
+ // @ts-check
11
+
12
+ // https://doc.rust-lang.org/reference/expressions.html#expression-precedence
13
+ const PREC = {
14
+ call: 15,
15
+ field: 14,
16
+ try: 13,
17
+ unary: 12,
18
+ cast: 11,
19
+ multiplicative: 10,
20
+ additive: 9,
21
+ shift: 8,
22
+ bitand: 7,
23
+ bitxor: 6,
24
+ bitor: 5,
25
+ comparative: 4,
26
+ and: 3,
27
+ or: 2,
28
+ range: 1,
29
+ assign: 0,
30
+ closure: -1,
31
+ };
32
+
33
+ const numericTypes = [
34
+ 'u8',
35
+ 'i8',
36
+ 'u16',
37
+ 'i16',
38
+ 'u32',
39
+ 'i32',
40
+ 'u64',
41
+ 'i64',
42
+ 'u128',
43
+ 'i128',
44
+ 'isize',
45
+ 'usize',
46
+ 'f32',
47
+ 'f64',
48
+ ];
49
+
50
+ // https://doc.rust-lang.org/reference/tokens.html#punctuation
51
+ const TOKEN_TREE_NON_SPECIAL_PUNCTUATION = [
52
+ '+', '-', '*', '/', '%', '^', '!', '&', '|', '&&', '||', '<<',
53
+ '>>', '+=', '-=', '*=', '/=', '%=', '^=', '&=', '|=', '<<=',
54
+ '>>=', '=', '==', '!=', '>', '<', '>=', '<=', '@', '_', '.',
55
+ '..', '...', '..=', ',', ';', ':', '::', '->', '=>', '#', '?',
56
+ ];
57
+
58
+ const primitiveTypes = numericTypes.concat(['bool', 'str', 'char']);
59
+
60
+ module.exports = grammar({
61
+ name: 'rust',
62
+
63
+ extras: $ => [
64
+ /\s/,
65
+ $.line_comment,
66
+ $.block_comment,
67
+ ],
68
+
69
+ externals: $ => [
70
+ $.string_content,
71
+ $._raw_string_literal_start,
72
+ $.raw_string_literal_content,
73
+ $._raw_string_literal_end,
74
+ $.float_literal,
75
+ $._outer_block_doc_comment_marker,
76
+ $._inner_block_doc_comment_marker,
77
+ $._block_comment_content,
78
+ $._line_doc_content,
79
+ $._error_sentinel,
80
+ ],
81
+
82
+ supertypes: $ => [
83
+ $._expression,
84
+ $._type,
85
+ $._literal,
86
+ $._literal_pattern,
87
+ $._declaration_statement,
88
+ $._pattern,
89
+ ],
90
+
91
+ inline: $ => [
92
+ $._path,
93
+ $._type_identifier,
94
+ $._tokens,
95
+ $._field_identifier,
96
+ $._non_special_token,
97
+ $._declaration_statement,
98
+ $._reserved_identifier,
99
+ $._expression_ending_with_block,
100
+ ],
101
+
102
+ conflicts: $ => [
103
+ // Local ambiguity due to anonymous types:
104
+ // See https://internals.rust-lang.org/t/pre-rfc-deprecating-anonymous-parameters/3710
105
+ [$._type, $._pattern],
106
+ [$.unit_type, $.tuple_pattern],
107
+ [$.scoped_identifier, $.scoped_type_identifier],
108
+ [$.parameters, $._pattern],
109
+ [$.parameters, $.tuple_struct_pattern],
110
+ [$.type_parameters, $.for_lifetimes],
111
+ [$.array_expression],
112
+ [$.visibility_modifier],
113
+ [$.visibility_modifier, $.scoped_identifier, $.scoped_type_identifier],
114
+ ],
115
+
116
+ word: $ => $.identifier,
117
+
118
+ rules: {
119
+ source_file: $ => seq(
120
+ optional($.shebang),
121
+ repeat($._statement),
122
+ ),
123
+
124
+ _statement: $ => choice(
125
+ $.expression_statement,
126
+ $._declaration_statement,
127
+ ),
128
+
129
+ empty_statement: _ => ';',
130
+
131
+ expression_statement: $ => choice(
132
+ seq($._expression, ';'),
133
+ prec(1, $._expression_ending_with_block),
134
+ ),
135
+
136
+ _declaration_statement: $ => choice(
137
+ $.const_item,
138
+ $.macro_invocation,
139
+ $.macro_definition,
140
+ $.empty_statement,
141
+ $.attribute_item,
142
+ $.inner_attribute_item,
143
+ $.mod_item,
144
+ $.foreign_mod_item,
145
+ $.struct_item,
146
+ $.union_item,
147
+ $.enum_item,
148
+ $.type_item,
149
+ $.function_item,
150
+ $.function_signature_item,
151
+ $.impl_item,
152
+ $.trait_item,
153
+ $.associated_type,
154
+ $.let_declaration,
155
+ $.use_declaration,
156
+ $.extern_crate_declaration,
157
+ $.static_item,
158
+ ),
159
+
160
+ // Section - Macro definitions
161
+
162
+ macro_definition: $ => {
163
+ const rules = seq(
164
+ repeat(seq($.macro_rule, ';')),
165
+ optional($.macro_rule),
166
+ );
167
+
168
+ return seq(
169
+ 'macro_rules!',
170
+ field('name', choice(
171
+ $.identifier,
172
+ $._reserved_identifier,
173
+ )),
174
+ choice(
175
+ seq('(', rules, ')', ';'),
176
+ seq('[', rules, ']', ';'),
177
+ seq('{', rules, '}'),
178
+ ),
179
+ );
180
+ },
181
+
182
+ macro_rule: $ => seq(
183
+ field('left', $.token_tree_pattern),
184
+ '=>',
185
+ field('right', $.token_tree),
186
+ ),
187
+
188
+ _token_pattern: $ => choice(
189
+ $.token_tree_pattern,
190
+ $.token_repetition_pattern,
191
+ $.token_binding_pattern,
192
+ $.metavariable,
193
+ $._non_special_token,
194
+ ),
195
+
196
+ token_tree_pattern: $ => choice(
197
+ seq('(', repeat($._token_pattern), ')'),
198
+ seq('[', repeat($._token_pattern), ']'),
199
+ seq('{', repeat($._token_pattern), '}'),
200
+ ),
201
+
202
+ token_binding_pattern: $ => prec(1, seq(
203
+ field('name', $.metavariable),
204
+ ':',
205
+ field('type', $.fragment_specifier),
206
+ )),
207
+
208
+ token_repetition_pattern: $ => seq(
209
+ '$', '(', repeat($._token_pattern), ')', optional(/[^+*?]+/), choice('+', '*', '?'),
210
+ ),
211
+
212
+ fragment_specifier: _ => choice(
213
+ 'block', 'expr', 'ident', 'item', 'lifetime', 'literal', 'meta', 'pat',
214
+ 'path', 'stmt', 'tt', 'ty', 'vis',
215
+ ),
216
+
217
+ _tokens: $ => choice(
218
+ $.token_tree,
219
+ $.token_repetition,
220
+ $.metavariable,
221
+ $._non_special_token,
222
+ ),
223
+
224
+ token_tree: $ => choice(
225
+ seq('(', repeat($._tokens), ')'),
226
+ seq('[', repeat($._tokens), ']'),
227
+ seq('{', repeat($._tokens), '}'),
228
+ ),
229
+
230
+ token_repetition: $ => seq(
231
+ '$', '(', repeat($._tokens), ')', optional(/[^+*?]+/), choice('+', '*', '?'),
232
+ ),
233
+
234
+ // Matches non-delimiter tokens common to both macro invocations and
235
+ // definitions. This is everything except $ and metavariables (which begin
236
+ // with $).
237
+ _non_special_token: $ => choice(
238
+ $._literal, $.identifier, $.mutable_specifier, $.self, $.super, $.crate,
239
+ alias(choice(...primitiveTypes), $.primitive_type),
240
+ prec.right(repeat1(choice(...TOKEN_TREE_NON_SPECIAL_PUNCTUATION))),
241
+ '\'',
242
+ 'as', 'async', 'await', 'break', 'const', 'continue', 'default', 'enum', 'fn', 'for', 'if', 'impl',
243
+ 'let', 'loop', 'match', 'mod', 'pub', 'return', 'static', 'struct', 'trait', 'type',
244
+ 'union', 'unsafe', 'use', 'where', 'while',
245
+ ),
246
+
247
+ // Section - Declarations
248
+
249
+ attribute_item: $ => seq(
250
+ '#',
251
+ '[',
252
+ $.attribute,
253
+ ']',
254
+ ),
255
+
256
+ inner_attribute_item: $ => seq(
257
+ '#',
258
+ '!',
259
+ '[',
260
+ $.attribute,
261
+ ']',
262
+ ),
263
+
264
+ attribute: $ => seq(
265
+ $._path,
266
+ optional(choice(
267
+ seq('=', field('value', $._expression)),
268
+ field('arguments', alias($.delim_token_tree, $.token_tree)),
269
+ )),
270
+ ),
271
+
272
+ mod_item: $ => seq(
273
+ optional($.visibility_modifier),
274
+ 'mod',
275
+ field('name', $.identifier),
276
+ choice(
277
+ ';',
278
+ field('body', $.declaration_list),
279
+ ),
280
+ ),
281
+
282
+ foreign_mod_item: $ => seq(
283
+ optional($.visibility_modifier),
284
+ $.extern_modifier,
285
+ choice(
286
+ ';',
287
+ field('body', $.declaration_list),
288
+ ),
289
+ ),
290
+
291
+ declaration_list: $ => seq(
292
+ '{',
293
+ repeat($._declaration_statement),
294
+ '}',
295
+ ),
296
+
297
+ struct_item: $ => seq(
298
+ optional($.visibility_modifier),
299
+ 'struct',
300
+ field('name', $._type_identifier),
301
+ field('type_parameters', optional($.type_parameters)),
302
+ choice(
303
+ seq(
304
+ optional($.where_clause),
305
+ field('body', $.field_declaration_list),
306
+ ),
307
+ seq(
308
+ field('body', $.ordered_field_declaration_list),
309
+ optional($.where_clause),
310
+ ';',
311
+ ),
312
+ ';',
313
+ ),
314
+ ),
315
+
316
+ union_item: $ => seq(
317
+ optional($.visibility_modifier),
318
+ 'union',
319
+ field('name', $._type_identifier),
320
+ field('type_parameters', optional($.type_parameters)),
321
+ optional($.where_clause),
322
+ field('body', $.field_declaration_list),
323
+ ),
324
+
325
+ enum_item: $ => seq(
326
+ optional($.visibility_modifier),
327
+ 'enum',
328
+ field('name', $._type_identifier),
329
+ field('type_parameters', optional($.type_parameters)),
330
+ optional($.where_clause),
331
+ field('body', $.enum_variant_list),
332
+ ),
333
+
334
+ enum_variant_list: $ => seq(
335
+ '{',
336
+ sepBy(',', seq(repeat($.attribute_item), $.enum_variant)),
337
+ optional(','),
338
+ '}',
339
+ ),
340
+
341
+ enum_variant: $ => seq(
342
+ optional($.visibility_modifier),
343
+ field('name', $.identifier),
344
+ field('body', optional(choice(
345
+ $.field_declaration_list,
346
+ $.ordered_field_declaration_list,
347
+ ))),
348
+ optional(seq(
349
+ '=',
350
+ field('value', $._expression),
351
+ )),
352
+ ),
353
+
354
+ field_declaration_list: $ => seq(
355
+ '{',
356
+ sepBy(',', seq(repeat($.attribute_item), $.field_declaration)),
357
+ optional(','),
358
+ '}',
359
+ ),
360
+
361
+ field_declaration: $ => seq(
362
+ optional($.visibility_modifier),
363
+ field('name', $._field_identifier),
364
+ ':',
365
+ field('type', $._type),
366
+ ),
367
+
368
+ ordered_field_declaration_list: $ => seq(
369
+ '(',
370
+ sepBy(',', seq(
371
+ repeat($.attribute_item),
372
+ optional($.visibility_modifier),
373
+ field('type', $._type),
374
+ )),
375
+ optional(','),
376
+ ')',
377
+ ),
378
+
379
+ extern_crate_declaration: $ => seq(
380
+ optional($.visibility_modifier),
381
+ 'extern',
382
+ $.crate,
383
+ field('name', $.identifier),
384
+ optional(seq(
385
+ 'as',
386
+ field('alias', $.identifier),
387
+ )),
388
+ ';',
389
+ ),
390
+
391
+ const_item: $ => seq(
392
+ optional($.visibility_modifier),
393
+ 'const',
394
+ field('name', $.identifier),
395
+ ':',
396
+ field('type', $._type),
397
+ optional(
398
+ seq(
399
+ '=',
400
+ field('value', $._expression),
401
+ ),
402
+ ),
403
+ ';',
404
+ ),
405
+
406
+ static_item: $ => seq(
407
+ optional($.visibility_modifier),
408
+ 'static',
409
+
410
+ // Not actual rust syntax, but made popular by the lazy_static crate.
411
+ optional('ref'),
412
+
413
+ optional($.mutable_specifier),
414
+ field('name', $.identifier),
415
+ ':',
416
+ field('type', $._type),
417
+ optional(seq(
418
+ '=',
419
+ field('value', $._expression),
420
+ )),
421
+ ';',
422
+ ),
423
+
424
+ type_item: $ => seq(
425
+ optional($.visibility_modifier),
426
+ 'type',
427
+ field('name', $._type_identifier),
428
+ field('type_parameters', optional($.type_parameters)),
429
+ '=',
430
+ field('type', $._type),
431
+ optional($.where_clause),
432
+ ';',
433
+ ),
434
+
435
+ function_item: $ => seq(
436
+ optional($.visibility_modifier),
437
+ optional($.function_modifiers),
438
+ 'fn',
439
+ field('name', choice($.identifier, $.metavariable)),
440
+ field('type_parameters', optional($.type_parameters)),
441
+ field('parameters', $.parameters),
442
+ optional(seq('->', field('return_type', $._type))),
443
+ optional($.where_clause),
444
+ field('body', $.block),
445
+ ),
446
+
447
+ function_signature_item: $ => seq(
448
+ optional($.visibility_modifier),
449
+ optional($.function_modifiers),
450
+ 'fn',
451
+ field('name', choice($.identifier, $.metavariable)),
452
+ field('type_parameters', optional($.type_parameters)),
453
+ field('parameters', $.parameters),
454
+ optional(seq('->', field('return_type', $._type))),
455
+ optional($.where_clause),
456
+ ';',
457
+ ),
458
+
459
+ function_modifiers: $ => repeat1(choice(
460
+ 'async',
461
+ 'default',
462
+ 'const',
463
+ 'unsafe',
464
+ $.extern_modifier,
465
+ )),
466
+
467
+ where_clause: $ => prec.right(seq(
468
+ 'where',
469
+ sepBy1(',', $.where_predicate),
470
+ optional(','),
471
+ )),
472
+
473
+ where_predicate: $ => seq(
474
+ field('left', choice(
475
+ $.lifetime,
476
+ $._type_identifier,
477
+ $.scoped_type_identifier,
478
+ $.generic_type,
479
+ $.reference_type,
480
+ $.pointer_type,
481
+ $.tuple_type,
482
+ $.array_type,
483
+ $.higher_ranked_trait_bound,
484
+ alias(choice(...primitiveTypes), $.primitive_type),
485
+ )),
486
+ field('bounds', $.trait_bounds),
487
+ ),
488
+
489
+ impl_item: $ => seq(
490
+ optional('unsafe'),
491
+ 'impl',
492
+ field('type_parameters', optional($.type_parameters)),
493
+ optional(seq(
494
+ optional('!'),
495
+ field('trait', choice(
496
+ $._type_identifier,
497
+ $.scoped_type_identifier,
498
+ $.generic_type,
499
+ )),
500
+ 'for',
501
+ )),
502
+ field('type', $._type),
503
+ optional($.where_clause),
504
+ choice(field('body', $.declaration_list), ';'),
505
+ ),
506
+
507
+ trait_item: $ => seq(
508
+ optional($.visibility_modifier),
509
+ optional('unsafe'),
510
+ 'trait',
511
+ field('name', $._type_identifier),
512
+ field('type_parameters', optional($.type_parameters)),
513
+ field('bounds', optional($.trait_bounds)),
514
+ optional($.where_clause),
515
+ field('body', $.declaration_list),
516
+ ),
517
+
518
+ associated_type: $ => seq(
519
+ 'type',
520
+ field('name', $._type_identifier),
521
+ field('type_parameters', optional($.type_parameters)),
522
+ field('bounds', optional($.trait_bounds)),
523
+ optional($.where_clause),
524
+ ';',
525
+ ),
526
+
527
+ trait_bounds: $ => seq(
528
+ ':',
529
+ sepBy1('+', choice(
530
+ $._type,
531
+ $.lifetime,
532
+ $.higher_ranked_trait_bound,
533
+ )),
534
+ ),
535
+
536
+ higher_ranked_trait_bound: $ => seq(
537
+ 'for',
538
+ field('type_parameters', $.type_parameters),
539
+ field('type', $._type),
540
+ ),
541
+
542
+ removed_trait_bound: $ => seq(
543
+ '?',
544
+ $._type,
545
+ ),
546
+
547
+ type_parameters: $ => prec(1, seq(
548
+ '<',
549
+ sepBy1(',', seq(
550
+ repeat($.attribute_item),
551
+ choice(
552
+ $.lifetime,
553
+ $.metavariable,
554
+ $._type_identifier,
555
+ $.constrained_type_parameter,
556
+ $.optional_type_parameter,
557
+ $.const_parameter,
558
+ ),
559
+ )),
560
+ optional(','),
561
+ '>',
562
+ )),
563
+
564
+ const_parameter: $ => seq(
565
+ 'const',
566
+ field('name', $.identifier),
567
+ ':',
568
+ field('type', $._type),
569
+ ),
570
+
571
+ constrained_type_parameter: $ => seq(
572
+ field('left', choice($.lifetime, $._type_identifier)),
573
+ field('bounds', $.trait_bounds),
574
+ ),
575
+
576
+ optional_type_parameter: $ => seq(
577
+ field('name', choice(
578
+ $._type_identifier,
579
+ $.constrained_type_parameter,
580
+ )),
581
+ '=',
582
+ field('default_type', $._type),
583
+ ),
584
+
585
+ let_declaration: $ => seq(
586
+ 'let',
587
+ optional($.mutable_specifier),
588
+ field('pattern', $._pattern),
589
+ optional(seq(
590
+ ':',
591
+ field('type', $._type),
592
+ )),
593
+ optional(seq(
594
+ '=',
595
+ field('value', $._expression),
596
+ )),
597
+ optional(seq(
598
+ 'else',
599
+ field('alternative', $.block),
600
+ )),
601
+ ';',
602
+ ),
603
+
604
+ use_declaration: $ => seq(
605
+ optional($.visibility_modifier),
606
+ 'use',
607
+ field('argument', $._use_clause),
608
+ ';',
609
+ ),
610
+
611
+ _use_clause: $ => choice(
612
+ $._path,
613
+ $.use_as_clause,
614
+ $.use_list,
615
+ $.scoped_use_list,
616
+ $.use_wildcard,
617
+ ),
618
+
619
+ scoped_use_list: $ => seq(
620
+ field('path', optional($._path)),
621
+ '::',
622
+ field('list', $.use_list),
623
+ ),
624
+
625
+ use_list: $ => seq(
626
+ '{',
627
+ sepBy(',', choice(
628
+ $._use_clause,
629
+ )),
630
+ optional(','),
631
+ '}',
632
+ ),
633
+
634
+ use_as_clause: $ => seq(
635
+ field('path', $._path),
636
+ 'as',
637
+ field('alias', $.identifier),
638
+ ),
639
+
640
+ use_wildcard: $ => seq(
641
+ optional(seq($._path, '::')),
642
+ '*',
643
+ ),
644
+
645
+ parameters: $ => seq(
646
+ '(',
647
+ sepBy(',', seq(
648
+ optional($.attribute_item),
649
+ choice(
650
+ $.parameter,
651
+ $.self_parameter,
652
+ $.variadic_parameter,
653
+ '_',
654
+ $._type,
655
+ ))),
656
+ optional(','),
657
+ ')',
658
+ ),
659
+
660
+ self_parameter: $ => seq(
661
+ optional('&'),
662
+ optional($.lifetime),
663
+ optional($.mutable_specifier),
664
+ $.self,
665
+ ),
666
+
667
+ variadic_parameter: $ => seq(
668
+ optional($.mutable_specifier),
669
+ optional(seq(
670
+ field('pattern', $._pattern),
671
+ ':',
672
+ )),
673
+ '...',
674
+ ),
675
+
676
+ parameter: $ => seq(
677
+ optional($.mutable_specifier),
678
+ field('pattern', choice(
679
+ $._pattern,
680
+ $.self,
681
+ )),
682
+ ':',
683
+ field('type', $._type),
684
+ ),
685
+
686
+ extern_modifier: $ => seq(
687
+ 'extern',
688
+ optional($.string_literal),
689
+ ),
690
+
691
+ visibility_modifier: $ => choice(
692
+ $.crate,
693
+ seq(
694
+ 'pub',
695
+ optional(seq(
696
+ '(',
697
+ choice(
698
+ $.self,
699
+ $.super,
700
+ $.crate,
701
+ seq('in', $._path),
702
+ ),
703
+ ')',
704
+ )),
705
+ ),
706
+ ),
707
+
708
+ // Section - Types
709
+
710
+ _type: $ => choice(
711
+ $.abstract_type,
712
+ $.reference_type,
713
+ $.metavariable,
714
+ $.pointer_type,
715
+ $.generic_type,
716
+ $.scoped_type_identifier,
717
+ $.tuple_type,
718
+ $.unit_type,
719
+ $.array_type,
720
+ $.function_type,
721
+ $._type_identifier,
722
+ $.macro_invocation,
723
+ $.never_type,
724
+ $.dynamic_type,
725
+ $.bounded_type,
726
+ $.removed_trait_bound,
727
+ alias(choice(...primitiveTypes), $.primitive_type),
728
+ ),
729
+
730
+ bracketed_type: $ => seq(
731
+ '<',
732
+ choice(
733
+ $._type,
734
+ $.qualified_type,
735
+ ),
736
+ '>',
737
+ ),
738
+
739
+ qualified_type: $ => seq(
740
+ field('type', $._type),
741
+ 'as',
742
+ field('alias', $._type),
743
+ ),
744
+
745
+ lifetime: $ => prec(1, seq('\'', $.identifier)),
746
+
747
+ array_type: $ => seq(
748
+ '[',
749
+ field('element', $._type),
750
+ optional(seq(
751
+ ';',
752
+ field('length', $._expression),
753
+ )),
754
+ ']',
755
+ ),
756
+
757
+ for_lifetimes: $ => seq(
758
+ 'for',
759
+ '<',
760
+ sepBy1(',', $.lifetime),
761
+ optional(','),
762
+ '>',
763
+ ),
764
+
765
+ function_type: $ => seq(
766
+ optional($.for_lifetimes),
767
+ prec(PREC.call, seq(
768
+ choice(
769
+ field('trait', choice(
770
+ $._type_identifier,
771
+ $.scoped_type_identifier,
772
+ )),
773
+ seq(
774
+ optional($.function_modifiers),
775
+ 'fn',
776
+ ),
777
+ ),
778
+ field('parameters', $.parameters),
779
+ )),
780
+ optional(seq('->', field('return_type', $._type))),
781
+ ),
782
+
783
+ tuple_type: $ => seq(
784
+ '(',
785
+ sepBy1(',', $._type),
786
+ optional(','),
787
+ ')',
788
+ ),
789
+
790
+ unit_type: _ => seq('(', ')'),
791
+
792
+ generic_function: $ => prec(1, seq(
793
+ field('function', choice(
794
+ $.identifier,
795
+ $.scoped_identifier,
796
+ $.field_expression,
797
+ )),
798
+ '::',
799
+ field('type_arguments', $.type_arguments),
800
+ )),
801
+
802
+ generic_type: $ => prec(1, seq(
803
+ field('type', choice(
804
+ $._type_identifier,
805
+ $._reserved_identifier,
806
+ $.scoped_type_identifier,
807
+ )),
808
+ field('type_arguments', $.type_arguments),
809
+ )),
810
+
811
+ generic_type_with_turbofish: $ => seq(
812
+ field('type', choice(
813
+ $._type_identifier,
814
+ $.scoped_identifier,
815
+ )),
816
+ '::',
817
+ field('type_arguments', $.type_arguments),
818
+ ),
819
+
820
+ bounded_type: $ => prec.left(-1, choice(
821
+ seq($.lifetime, '+', $._type),
822
+ seq($._type, '+', $._type),
823
+ seq($._type, '+', $.lifetime),
824
+ )),
825
+
826
+ type_arguments: $ => seq(
827
+ token(prec(1, '<')),
828
+ sepBy1(',', seq(
829
+ choice(
830
+ $._type,
831
+ $.type_binding,
832
+ $.lifetime,
833
+ $._literal,
834
+ $.block,
835
+ ),
836
+ optional($.trait_bounds),
837
+ )),
838
+ optional(','),
839
+ '>',
840
+ ),
841
+
842
+ type_binding: $ => seq(
843
+ field('name', $._type_identifier),
844
+ field('type_arguments', optional($.type_arguments)),
845
+ '=',
846
+ field('type', $._type),
847
+ ),
848
+
849
+ reference_type: $ => seq(
850
+ '&',
851
+ optional($.lifetime),
852
+ optional($.mutable_specifier),
853
+ field('type', $._type),
854
+ ),
855
+
856
+ pointer_type: $ => seq(
857
+ '*',
858
+ choice('const', $.mutable_specifier),
859
+ field('type', $._type),
860
+ ),
861
+
862
+ never_type: _ => '!',
863
+
864
+ abstract_type: $ => seq(
865
+ 'impl',
866
+ optional(seq('for', $.type_parameters)),
867
+ field('trait', choice(
868
+ $._type_identifier,
869
+ $.scoped_type_identifier,
870
+ $.removed_trait_bound,
871
+ $.generic_type,
872
+ $.function_type,
873
+ $.tuple_type,
874
+ )),
875
+ ),
876
+
877
+ dynamic_type: $ => seq(
878
+ 'dyn',
879
+ field('trait', choice(
880
+ $.higher_ranked_trait_bound,
881
+ $._type_identifier,
882
+ $.scoped_type_identifier,
883
+ $.generic_type,
884
+ $.function_type,
885
+ )),
886
+ ),
887
+
888
+ mutable_specifier: _ => 'mut',
889
+
890
+ // Section - Expressions
891
+
892
+ _expression_except_range: $ => choice(
893
+ $.unary_expression,
894
+ $.reference_expression,
895
+ $.try_expression,
896
+ $.binary_expression,
897
+ $.assignment_expression,
898
+ $.compound_assignment_expr,
899
+ $.type_cast_expression,
900
+ $.call_expression,
901
+ $.return_expression,
902
+ $.yield_expression,
903
+ $._literal,
904
+ prec.left($.identifier),
905
+ alias(choice(...primitiveTypes), $.identifier),
906
+ prec.left($._reserved_identifier),
907
+ $.self,
908
+ $.scoped_identifier,
909
+ $.generic_function,
910
+ $.await_expression,
911
+ $.field_expression,
912
+ $.array_expression,
913
+ $.tuple_expression,
914
+ prec(1, $.macro_invocation),
915
+ $.unit_expression,
916
+ $.break_expression,
917
+ $.continue_expression,
918
+ $.index_expression,
919
+ $.metavariable,
920
+ $.closure_expression,
921
+ $.parenthesized_expression,
922
+ $.struct_expression,
923
+ $._expression_ending_with_block,
924
+ ),
925
+
926
+ _expression: $ => choice(
927
+ $._expression_except_range,
928
+ $.range_expression,
929
+ ),
930
+
931
+ _expression_ending_with_block: $ => choice(
932
+ $.unsafe_block,
933
+ $.async_block,
934
+ $.try_block,
935
+ $.block,
936
+ $.if_expression,
937
+ $.match_expression,
938
+ $.while_expression,
939
+ $.loop_expression,
940
+ $.for_expression,
941
+ $.const_block,
942
+ ),
943
+
944
+ macro_invocation: $ => seq(
945
+ field('macro', choice(
946
+ $.scoped_identifier,
947
+ $.identifier,
948
+ $._reserved_identifier,
949
+ )),
950
+ '!',
951
+ alias($.delim_token_tree, $.token_tree),
952
+ ),
953
+
954
+ delim_token_tree: $ => choice(
955
+ seq('(', repeat($._delim_tokens), ')'),
956
+ seq('[', repeat($._delim_tokens), ']'),
957
+ seq('{', repeat($._delim_tokens), '}'),
958
+ ),
959
+
960
+ _delim_tokens: $ => choice(
961
+ $._non_delim_token,
962
+ alias($.delim_token_tree, $.token_tree),
963
+ ),
964
+
965
+ // Should match any token other than a delimiter.
966
+ _non_delim_token: $ => choice(
967
+ $._non_special_token,
968
+ '$',
969
+ ),
970
+
971
+ scoped_identifier: $ => seq(
972
+ field('path', optional(choice(
973
+ $._path,
974
+ $.bracketed_type,
975
+ alias($.generic_type_with_turbofish, $.generic_type),
976
+ ))),
977
+ '::',
978
+ field('name', choice($.identifier, $.super)),
979
+ ),
980
+
981
+ scoped_type_identifier_in_expression_position: $ => prec(-2, seq(
982
+ field('path', optional(choice(
983
+ $._path,
984
+ alias($.generic_type_with_turbofish, $.generic_type),
985
+ ))),
986
+ '::',
987
+ field('name', $._type_identifier),
988
+ )),
989
+
990
+ scoped_type_identifier: $ => seq(
991
+ field('path', optional(choice(
992
+ $._path,
993
+ alias($.generic_type_with_turbofish, $.generic_type),
994
+ $.bracketed_type,
995
+ $.generic_type,
996
+ ))),
997
+ '::',
998
+ field('name', $._type_identifier),
999
+ ),
1000
+
1001
+ range_expression: $ => prec.left(PREC.range, choice(
1002
+ seq($._expression, choice('..', '...', '..='), $._expression),
1003
+ seq($._expression, '..'),
1004
+ seq('..', $._expression),
1005
+ '..',
1006
+ )),
1007
+
1008
+ unary_expression: $ => prec(PREC.unary, seq(
1009
+ choice('-', '*', '!'),
1010
+ $._expression,
1011
+ )),
1012
+
1013
+ try_expression: $ => prec(PREC.try, seq(
1014
+ $._expression,
1015
+ '?',
1016
+ )),
1017
+
1018
+ reference_expression: $ => prec(PREC.unary, seq(
1019
+ '&',
1020
+ optional($.mutable_specifier),
1021
+ field('value', $._expression),
1022
+ )),
1023
+
1024
+ binary_expression: $ => {
1025
+ const table = [
1026
+ [PREC.and, '&&'],
1027
+ [PREC.or, '||'],
1028
+ [PREC.bitand, '&'],
1029
+ [PREC.bitor, '|'],
1030
+ [PREC.bitxor, '^'],
1031
+ [PREC.comparative, choice('==', '!=', '<', '<=', '>', '>=')],
1032
+ [PREC.shift, choice('<<', '>>')],
1033
+ [PREC.additive, choice('+', '-')],
1034
+ [PREC.multiplicative, choice('*', '/', '%')],
1035
+ ];
1036
+
1037
+ // @ts-ignore
1038
+ return choice(...table.map(([precedence, operator]) => prec.left(precedence, seq(
1039
+ field('left', $._expression),
1040
+ // @ts-ignore
1041
+ field('operator', operator),
1042
+ field('right', $._expression),
1043
+ ))));
1044
+ },
1045
+
1046
+ assignment_expression: $ => prec.left(PREC.assign, seq(
1047
+ field('left', $._expression),
1048
+ '=',
1049
+ field('right', $._expression),
1050
+ )),
1051
+
1052
+ compound_assignment_expr: $ => prec.left(PREC.assign, seq(
1053
+ field('left', $._expression),
1054
+ field('operator', choice('+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '<<=', '>>=')),
1055
+ field('right', $._expression),
1056
+ )),
1057
+
1058
+ type_cast_expression: $ => prec.left(PREC.cast, seq(
1059
+ field('value', $._expression),
1060
+ 'as',
1061
+ field('type', $._type),
1062
+ )),
1063
+
1064
+ return_expression: $ => choice(
1065
+ prec.left(seq('return', $._expression)),
1066
+ prec(-1, 'return'),
1067
+ ),
1068
+
1069
+ yield_expression: $ => choice(
1070
+ prec.left(seq('yield', $._expression)),
1071
+ prec(-1, 'yield'),
1072
+ ),
1073
+
1074
+ call_expression: $ => prec(PREC.call, seq(
1075
+ field('function', $._expression_except_range),
1076
+ field('arguments', $.arguments),
1077
+ )),
1078
+
1079
+ arguments: $ => seq(
1080
+ '(',
1081
+ sepBy(',', seq(repeat($.attribute_item), $._expression)),
1082
+ optional(','),
1083
+ ')',
1084
+ ),
1085
+
1086
+ array_expression: $ => seq(
1087
+ '[',
1088
+ repeat($.attribute_item),
1089
+ choice(
1090
+ seq(
1091
+ $._expression,
1092
+ ';',
1093
+ field('length', $._expression),
1094
+ ),
1095
+ seq(
1096
+ sepBy(',', seq(repeat($.attribute_item), $._expression)),
1097
+ optional(','),
1098
+ ),
1099
+ ),
1100
+ ']',
1101
+ ),
1102
+
1103
+ parenthesized_expression: $ => seq(
1104
+ '(',
1105
+ $._expression,
1106
+ ')',
1107
+ ),
1108
+
1109
+ tuple_expression: $ => seq(
1110
+ '(',
1111
+ repeat($.attribute_item),
1112
+ seq($._expression, ','),
1113
+ repeat(seq($._expression, ',')),
1114
+ optional($._expression),
1115
+ ')',
1116
+ ),
1117
+
1118
+ unit_expression: _ => seq('(', ')'),
1119
+
1120
+ struct_expression: $ => seq(
1121
+ field('name', choice(
1122
+ $._type_identifier,
1123
+ alias($.scoped_type_identifier_in_expression_position, $.scoped_type_identifier),
1124
+ $.generic_type_with_turbofish,
1125
+ )),
1126
+ field('body', $.field_initializer_list),
1127
+ ),
1128
+
1129
+ field_initializer_list: $ => seq(
1130
+ '{',
1131
+ sepBy(',', choice(
1132
+ $.shorthand_field_initializer,
1133
+ $.field_initializer,
1134
+ $.base_field_initializer,
1135
+ )),
1136
+ optional(','),
1137
+ '}',
1138
+ ),
1139
+
1140
+ shorthand_field_initializer: $ => seq(
1141
+ repeat($.attribute_item),
1142
+ $.identifier,
1143
+ ),
1144
+
1145
+ field_initializer: $ => seq(
1146
+ repeat($.attribute_item),
1147
+ field('field', choice($._field_identifier, $.integer_literal)),
1148
+ ':',
1149
+ field('value', $._expression),
1150
+ ),
1151
+
1152
+ base_field_initializer: $ => seq(
1153
+ '..',
1154
+ $._expression,
1155
+ ),
1156
+
1157
+ if_expression: $ => prec.right(seq(
1158
+ 'if',
1159
+ field('condition', $._condition),
1160
+ field('consequence', $.block),
1161
+ optional(field('alternative', $.else_clause)),
1162
+ )),
1163
+
1164
+ let_condition: $ => seq(
1165
+ 'let',
1166
+ field('pattern', $._pattern),
1167
+ '=',
1168
+ field('value', prec.left(PREC.and, $._expression)),
1169
+ ),
1170
+
1171
+ _let_chain: $ => prec.left(PREC.and, choice(
1172
+ seq($._let_chain, '&&', $.let_condition),
1173
+ seq($._let_chain, '&&', $._expression),
1174
+ seq($.let_condition, '&&', $._expression),
1175
+ seq($.let_condition, '&&', $.let_condition),
1176
+ seq($._expression, '&&', $.let_condition),
1177
+ )),
1178
+
1179
+ _condition: $ => choice(
1180
+ $._expression,
1181
+ $.let_condition,
1182
+ alias($._let_chain, $.let_chain),
1183
+ ),
1184
+
1185
+ else_clause: $ => seq(
1186
+ 'else',
1187
+ choice(
1188
+ $.block,
1189
+ $.if_expression,
1190
+ ),
1191
+ ),
1192
+
1193
+ match_expression: $ => seq(
1194
+ 'match',
1195
+ field('value', $._expression),
1196
+ field('body', $.match_block),
1197
+ ),
1198
+
1199
+ match_block: $ => seq(
1200
+ '{',
1201
+ optional(seq(
1202
+ repeat($.match_arm),
1203
+ alias($.last_match_arm, $.match_arm),
1204
+ )),
1205
+ '}',
1206
+ ),
1207
+
1208
+ match_arm: $ => prec.right(seq(
1209
+ repeat(choice($.attribute_item, $.inner_attribute_item)),
1210
+ field('pattern', $.match_pattern),
1211
+ '=>',
1212
+ choice(
1213
+ seq(field('value', $._expression), ','),
1214
+ field('value', prec(1, $._expression_ending_with_block)),
1215
+ ),
1216
+ )),
1217
+
1218
+ last_match_arm: $ => seq(
1219
+ repeat(choice($.attribute_item, $.inner_attribute_item)),
1220
+ field('pattern', $.match_pattern),
1221
+ '=>',
1222
+ field('value', $._expression),
1223
+ optional(','),
1224
+ ),
1225
+
1226
+ match_pattern: $ => seq(
1227
+ $._pattern,
1228
+ optional(seq('if', field('condition', $._condition))),
1229
+ ),
1230
+
1231
+ while_expression: $ => seq(
1232
+ optional(seq($.label, ':')),
1233
+ 'while',
1234
+ field('condition', $._condition),
1235
+ field('body', $.block),
1236
+ ),
1237
+
1238
+ loop_expression: $ => seq(
1239
+ optional(seq($.label, ':')),
1240
+ 'loop',
1241
+ field('body', $.block),
1242
+ ),
1243
+
1244
+ for_expression: $ => seq(
1245
+ optional(seq($.label, ':')),
1246
+ 'for',
1247
+ field('pattern', $._pattern),
1248
+ 'in',
1249
+ field('value', $._expression),
1250
+ field('body', $.block),
1251
+ ),
1252
+
1253
+ const_block: $ => seq(
1254
+ 'const',
1255
+ field('body', $.block),
1256
+ ),
1257
+
1258
+ closure_expression: $ => prec(PREC.closure, seq(
1259
+ optional('static'),
1260
+ optional('move'),
1261
+ field('parameters', $.closure_parameters),
1262
+ choice(
1263
+ seq(
1264
+ optional(seq('->', field('return_type', $._type))),
1265
+ field('body', $.block),
1266
+ ),
1267
+ field('body', choice($._expression, '_')),
1268
+ ),
1269
+ )),
1270
+
1271
+ closure_parameters: $ => seq(
1272
+ '|',
1273
+ sepBy(',', choice(
1274
+ $._pattern,
1275
+ $.parameter,
1276
+ )),
1277
+ '|',
1278
+ ),
1279
+
1280
+ label: $ => seq('\'', $.identifier),
1281
+
1282
+ break_expression: $ => prec.left(seq('break', optional($.label), optional($._expression))),
1283
+
1284
+ continue_expression: $ => prec.left(seq('continue', optional($.label))),
1285
+
1286
+ index_expression: $ => prec(PREC.call, seq($._expression, '[', $._expression, ']')),
1287
+
1288
+ await_expression: $ => prec(PREC.field, seq(
1289
+ $._expression,
1290
+ '.',
1291
+ 'await',
1292
+ )),
1293
+
1294
+ field_expression: $ => prec(PREC.field, seq(
1295
+ field('value', $._expression),
1296
+ '.',
1297
+ field('field', choice(
1298
+ $._field_identifier,
1299
+ $.integer_literal,
1300
+ )),
1301
+ )),
1302
+
1303
+ unsafe_block: $ => seq(
1304
+ 'unsafe',
1305
+ $.block,
1306
+ ),
1307
+
1308
+ async_block: $ => seq(
1309
+ 'async',
1310
+ optional('move'),
1311
+ $.block,
1312
+ ),
1313
+
1314
+ try_block: $ => seq(
1315
+ 'try',
1316
+ $.block,
1317
+ ),
1318
+
1319
+ block: $ => seq(
1320
+ optional(seq($.label, ':')),
1321
+ '{',
1322
+ repeat($._statement),
1323
+ optional($._expression),
1324
+ '}',
1325
+ ),
1326
+
1327
+ // Section - Patterns
1328
+
1329
+ _pattern: $ => choice(
1330
+ $._literal_pattern,
1331
+ alias(choice(...primitiveTypes), $.identifier),
1332
+ $.identifier,
1333
+ $.scoped_identifier,
1334
+ $.tuple_pattern,
1335
+ $.tuple_struct_pattern,
1336
+ $.struct_pattern,
1337
+ $._reserved_identifier,
1338
+ $.ref_pattern,
1339
+ $.slice_pattern,
1340
+ $.captured_pattern,
1341
+ $.reference_pattern,
1342
+ $.remaining_field_pattern,
1343
+ $.mut_pattern,
1344
+ $.range_pattern,
1345
+ $.or_pattern,
1346
+ $.const_block,
1347
+ $.macro_invocation,
1348
+ '_',
1349
+ ),
1350
+
1351
+ tuple_pattern: $ => seq(
1352
+ '(',
1353
+ sepBy(',', choice($._pattern, $.closure_expression)),
1354
+ optional(','),
1355
+ ')',
1356
+ ),
1357
+
1358
+ slice_pattern: $ => seq(
1359
+ '[',
1360
+ sepBy(',', $._pattern),
1361
+ optional(','),
1362
+ ']',
1363
+ ),
1364
+
1365
+ tuple_struct_pattern: $ => seq(
1366
+ field('type', choice(
1367
+ $.identifier,
1368
+ $.scoped_identifier,
1369
+ alias($.generic_type_with_turbofish, $.generic_type),
1370
+ )),
1371
+ '(',
1372
+ sepBy(',', $._pattern),
1373
+ optional(','),
1374
+ ')',
1375
+ ),
1376
+
1377
+ struct_pattern: $ => seq(
1378
+ field('type', choice(
1379
+ $._type_identifier,
1380
+ $.scoped_type_identifier,
1381
+ )),
1382
+ '{',
1383
+ sepBy(',', choice($.field_pattern, $.remaining_field_pattern)),
1384
+ optional(','),
1385
+ '}',
1386
+ ),
1387
+
1388
+ field_pattern: $ => seq(
1389
+ optional('ref'),
1390
+ optional($.mutable_specifier),
1391
+ choice(
1392
+ field('name', alias($.identifier, $.shorthand_field_identifier)),
1393
+ seq(
1394
+ field('name', $._field_identifier),
1395
+ ':',
1396
+ field('pattern', $._pattern),
1397
+ ),
1398
+ ),
1399
+ ),
1400
+
1401
+ remaining_field_pattern: _ => '..',
1402
+
1403
+ mut_pattern: $ => prec(-1, seq(
1404
+ $.mutable_specifier,
1405
+ $._pattern,
1406
+ )),
1407
+
1408
+ range_pattern: $ => seq(
1409
+ choice(
1410
+ $._literal_pattern,
1411
+ $._path,
1412
+ ),
1413
+ choice(
1414
+ seq(
1415
+ choice('...', '..=', '..'),
1416
+ choice(
1417
+ $._literal_pattern,
1418
+ $._path,
1419
+ ),
1420
+ ),
1421
+ '..',
1422
+ ),
1423
+ ),
1424
+
1425
+ ref_pattern: $ => seq(
1426
+ 'ref',
1427
+ $._pattern,
1428
+ ),
1429
+
1430
+ captured_pattern: $ => seq(
1431
+ $.identifier,
1432
+ '@',
1433
+ $._pattern,
1434
+ ),
1435
+
1436
+ reference_pattern: $ => seq(
1437
+ '&',
1438
+ optional($.mutable_specifier),
1439
+ $._pattern,
1440
+ ),
1441
+
1442
+ or_pattern: $ => prec.left(-2, choice(
1443
+ seq($._pattern, '|', $._pattern),
1444
+ seq('|', $._pattern),
1445
+ )),
1446
+
1447
+ // Section - Literals
1448
+
1449
+ _literal: $ => choice(
1450
+ $.string_literal,
1451
+ $.raw_string_literal,
1452
+ $.char_literal,
1453
+ $.boolean_literal,
1454
+ $.integer_literal,
1455
+ $.float_literal,
1456
+ ),
1457
+
1458
+ _literal_pattern: $ => choice(
1459
+ $.string_literal,
1460
+ $.raw_string_literal,
1461
+ $.char_literal,
1462
+ $.boolean_literal,
1463
+ $.integer_literal,
1464
+ $.float_literal,
1465
+ $.negative_literal,
1466
+ ),
1467
+
1468
+ negative_literal: $ => seq('-', choice($.integer_literal, $.float_literal)),
1469
+
1470
+ integer_literal: _ => token(seq(
1471
+ choice(
1472
+ /[0-9][0-9_]*/,
1473
+ /0x[0-9a-fA-F_]+/,
1474
+ /0b[01_]+/,
1475
+ /0o[0-7_]+/,
1476
+ ),
1477
+ optional(choice(...numericTypes)),
1478
+ )),
1479
+
1480
+ string_literal: $ => seq(
1481
+ alias(/[bc]?"/, '"'),
1482
+ repeat(choice(
1483
+ $.escape_sequence,
1484
+ $.string_content,
1485
+ )),
1486
+ token.immediate('"'),
1487
+ ),
1488
+
1489
+ raw_string_literal: $ => seq(
1490
+ $._raw_string_literal_start,
1491
+ alias($.raw_string_literal_content, $.string_content),
1492
+ $._raw_string_literal_end,
1493
+ ),
1494
+
1495
+ char_literal: _ => token(seq(
1496
+ optional('b'),
1497
+ '\'',
1498
+ optional(choice(
1499
+ seq('\\', choice(
1500
+ /[^xu]/,
1501
+ /u[0-9a-fA-F]{4}/,
1502
+ /u\{[0-9a-fA-F]+\}/,
1503
+ /x[0-9a-fA-F]{2}/,
1504
+ )),
1505
+ /[^\\']/,
1506
+ )),
1507
+ '\'',
1508
+ )),
1509
+
1510
+ escape_sequence: _ => token.immediate(
1511
+ seq('\\',
1512
+ choice(
1513
+ /[^xu]/,
1514
+ /u[0-9a-fA-F]{4}/,
1515
+ /u\{[0-9a-fA-F]+\}/,
1516
+ /x[0-9a-fA-F]{2}/,
1517
+ ),
1518
+ )),
1519
+
1520
+ boolean_literal: _ => choice('true', 'false'),
1521
+
1522
+ comment: $ => choice(
1523
+ $.line_comment,
1524
+ $.block_comment,
1525
+ ),
1526
+
1527
+ line_comment: $ => seq(
1528
+ // All line comments start with two //
1529
+ '//',
1530
+ // Then are followed by:
1531
+ // - 2 or more slashes making it a regular comment
1532
+ // - 1 slash or 1 or more bang operators making it a doc comment
1533
+ // - or just content for the comment
1534
+ choice(
1535
+ // A tricky edge case where what looks like a doc comment is not
1536
+ seq(token.immediate(prec(2, /\/\//)), /.*/),
1537
+ // A regular doc comment
1538
+ seq($._line_doc_comment_marker, field('doc', alias($._line_doc_content, $.doc_comment))),
1539
+ token.immediate(prec(1, /.*/)),
1540
+ ),
1541
+ ),
1542
+
1543
+ _line_doc_comment_marker: $ => choice(
1544
+ // An outer line doc comment applies to the element that it is outside of
1545
+ field('outer', alias($._outer_line_doc_comment_marker, $.outer_doc_comment_marker)),
1546
+ // An inner line doc comment applies to the element it is inside of
1547
+ field('inner', alias($._inner_line_doc_comment_marker, $.inner_doc_comment_marker)),
1548
+ ),
1549
+
1550
+ _inner_line_doc_comment_marker: _ => token.immediate(prec(2, '!')),
1551
+ _outer_line_doc_comment_marker: _ => token.immediate(prec(2, '/')),
1552
+
1553
+ block_comment: $ => seq(
1554
+ '/*',
1555
+ optional(
1556
+ choice(
1557
+ // Documentation block comments: /** docs */ or /*! docs */
1558
+ seq(
1559
+ $._block_doc_comment_marker,
1560
+ optional(field('doc', alias($._block_comment_content, $.doc_comment))),
1561
+ ),
1562
+ // Non-doc block comments
1563
+ $._block_comment_content,
1564
+ ),
1565
+ ),
1566
+ '*/',
1567
+ ),
1568
+
1569
+ _block_doc_comment_marker: $ => choice(
1570
+ field('outer', alias($._outer_block_doc_comment_marker, $.outer_doc_comment_marker)),
1571
+ field('inner', alias($._inner_block_doc_comment_marker, $.inner_doc_comment_marker)),
1572
+ ),
1573
+
1574
+ _path: $ => choice(
1575
+ $.self,
1576
+ alias(choice(...primitiveTypes), $.identifier),
1577
+ $.metavariable,
1578
+ $.super,
1579
+ $.crate,
1580
+ $.identifier,
1581
+ $.scoped_identifier,
1582
+ $._reserved_identifier,
1583
+ ),
1584
+
1585
+ identifier: _ => /(r#)?[_\p{XID_Start}][_\p{XID_Continue}]*/,
1586
+
1587
+ shebang: _ => /#![\s]*[^\[].+/,
1588
+
1589
+ _reserved_identifier: $ => alias(choice(
1590
+ 'default',
1591
+ 'union',
1592
+ ), $.identifier),
1593
+
1594
+ _type_identifier: $ => alias($.identifier, $.type_identifier),
1595
+ _field_identifier: $ => alias($.identifier, $.field_identifier),
1596
+
1597
+ self: _ => 'self',
1598
+ super: _ => 'super',
1599
+ crate: _ => 'crate',
1600
+
1601
+ metavariable: _ => /\$[a-zA-Z_]\w*/,
1602
+ },
1603
+ });
1604
+
1605
+ /**
1606
+ * Creates a rule to match one or more of the rules separated by the separator.
1607
+ *
1608
+ * @param {RuleOrLiteral} sep - The separator to use.
1609
+ * @param {RuleOrLiteral} rule
1610
+ *
1611
+ * @return {SeqRule}
1612
+ *
1613
+ */
1614
+ function sepBy1(sep, rule) {
1615
+ return seq(rule, repeat(seq(sep, rule)));
1616
+ }
1617
+
1618
+
1619
+ /**
1620
+ * Creates a rule to optionally match one or more of the rules separated by the separator.
1621
+ *
1622
+ * @param {RuleOrLiteral} sep - The separator to use.
1623
+ * @param {RuleOrLiteral} rule
1624
+ *
1625
+ * @return {ChoiceRule}
1626
+ *
1627
+ */
1628
+ function sepBy(sep, rule) {
1629
+ return optional(sepBy1(sep, rule));
1630
+ }