@mpeggroup/mpeg-sdl-parser 4.0.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 (375) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +337 -0
  3. package/bun.lock +65 -0
  4. package/dist/index.js +11829 -0
  5. package/grammar/sdl.ebnf.grammar +399 -0
  6. package/grammar/sdl.lezer.grammar +629 -0
  7. package/index.ts +83 -0
  8. package/package.json +61 -0
  9. package/src/analyser/checks/check-array-definition.ts +90 -0
  10. package/src/analyser/checks/check-array-element-access.ts +31 -0
  11. package/src/analyser/checks/check-class-declaration.ts +417 -0
  12. package/src/analyser/checks/check-computed-array-definition.ts +44 -0
  13. package/src/analyser/checks/check-computed-elementary-type-definition.ts +45 -0
  14. package/src/analyser/checks/check-elementary-type-definition.ts +167 -0
  15. package/src/analyser/checks/check-expression-binary.ts +242 -0
  16. package/src/analyser/checks/check-expression-lengthof.ts +34 -0
  17. package/src/analyser/checks/check-expression-unary.ts +166 -0
  18. package/src/analyser/checks/check-identifier.ts +71 -0
  19. package/src/analyser/checks/check-if-statement.ts +164 -0
  20. package/src/analyser/checks/check-map-declaration.ts +180 -0
  21. package/src/analyser/checks/check-map-definition.ts +118 -0
  22. package/src/analyser/checks/check-number-literal-float.ts +28 -0
  23. package/src/analyser/checks/check-parameter-list.ts +42 -0
  24. package/src/analyser/checks/check-specification.ts +53 -0
  25. package/src/analyser/checks/check-switch-statement.ts +206 -0
  26. package/src/analyser/checks/check.ts +20 -0
  27. package/src/analyser/create-sdl-analyser.ts +85 -0
  28. package/src/analyser/node-handler/abstract-analysis-node-handler.ts +626 -0
  29. package/src/analyser/node-handler/build-symbol-table-node-handler.ts +649 -0
  30. package/src/analyser/node-handler/specific-check-node-handler.ts +46 -0
  31. package/src/analyser/node-handler/validate-scope-node-handler.ts +290 -0
  32. package/src/analyser/node-handler/validate-type-node-handler.ts +497 -0
  33. package/src/analyser/sdl-analyser.ts +119 -0
  34. package/src/analyser/symbol-table.ts +388 -0
  35. package/src/analyser/util/symbol-table-utils.ts +458 -0
  36. package/src/analyser/util/type-utils.ts +238 -0
  37. package/src/ast/build/consume/consume-abstract-node.ts +199 -0
  38. package/src/ast/build/consume/consume-ast-node.ts +400 -0
  39. package/src/ast/build/consume/consume-primitive.ts +59 -0
  40. package/src/ast/build/consume/consume-trivia.ts +96 -0
  41. package/src/ast/build/consume/consume-white-space-and-missing-errors.ts +27 -0
  42. package/src/ast/build/node/build-aggregate-output-value.ts +57 -0
  43. package/src/ast/build/node/build-aligned-modifier.ts +99 -0
  44. package/src/ast/build/node/build-array-definition.ts +131 -0
  45. package/src/ast/build/node/build-array-element-access.ts +45 -0
  46. package/src/ast/build/node/build-base64-string-literal.ts +41 -0
  47. package/src/ast/build/node/build-binary-expression.ts +113 -0
  48. package/src/ast/build/node/build-binary-literal.ts +37 -0
  49. package/src/ast/build/node/build-bit-modifier.ts +86 -0
  50. package/src/ast/build/node/build-case-clause.ts +101 -0
  51. package/src/ast/build/node/build-class-declaration.ts +124 -0
  52. package/src/ast/build/node/build-class-definition.ts +56 -0
  53. package/src/ast/build/node/build-class-id-range.ts +32 -0
  54. package/src/ast/build/node/build-class-id.ts +19 -0
  55. package/src/ast/build/node/build-class-member-access.ts +27 -0
  56. package/src/ast/build/node/build-compound-statement.ts +33 -0
  57. package/src/ast/build/node/build-computed-array-definition.ts +53 -0
  58. package/src/ast/build/node/build-computed-elementary-type-definition.ts +100 -0
  59. package/src/ast/build/node/build-decimal-literal.ts +15 -0
  60. package/src/ast/build/node/build-default-clause.ts +76 -0
  61. package/src/ast/build/node/build-do-statement.ts +71 -0
  62. package/src/ast/build/node/build-elementary-type-definition.ts +164 -0
  63. package/src/ast/build/node/build-elementary-type-output-value.ts +25 -0
  64. package/src/ast/build/node/build-elementary-type.ts +36 -0
  65. package/src/ast/build/node/build-expandable-modifier.ts +68 -0
  66. package/src/ast/build/node/build-explicit-array-dimension.ts +37 -0
  67. package/src/ast/build/node/build-expression-statement.ts +31 -0
  68. package/src/ast/build/node/build-extended-class-id-range.ts +27 -0
  69. package/src/ast/build/node/build-extends-modifier.ts +41 -0
  70. package/src/ast/build/node/build-floating-point-literal.ts +15 -0
  71. package/src/ast/build/node/build-for-statement.ts +117 -0
  72. package/src/ast/build/node/build-hexadecimal-literal.ts +38 -0
  73. package/src/ast/build/node/build-identifier.ts +8 -0
  74. package/src/ast/build/node/build-if-statement.ts +82 -0
  75. package/src/ast/build/node/build-implicit-array-dimension.ts +78 -0
  76. package/src/ast/build/node/build-integer-literal.ts +15 -0
  77. package/src/ast/build/node/build-length-attribute.ts +48 -0
  78. package/src/ast/build/node/build-lengthof-expression.ts +45 -0
  79. package/src/ast/build/node/build-map-declaration.ts +110 -0
  80. package/src/ast/build/node/build-map-definition.ts +103 -0
  81. package/src/ast/build/node/build-map-entry.ts +33 -0
  82. package/src/ast/build/node/build-missing-error.ts +10 -0
  83. package/src/ast/build/node/build-multiple-character-literal.ts +65 -0
  84. package/src/ast/build/node/build-parameter-list.ts +50 -0
  85. package/src/ast/build/node/build-parameter-value-list.ts +54 -0
  86. package/src/ast/build/node/build-parameter.ts +47 -0
  87. package/src/ast/build/node/build-partial-array-dimension.ts +55 -0
  88. package/src/ast/build/node/build-specification.ts +24 -0
  89. package/src/ast/build/node/build-string-definition.ts +106 -0
  90. package/src/ast/build/node/build-switch-statement.ts +84 -0
  91. package/src/ast/build/node/build-token.ts +26 -0
  92. package/src/ast/build/node/build-unary-expression.ts +109 -0
  93. package/src/ast/build/node/build-unexpected-error.ts +14 -0
  94. package/src/ast/build/node/build-utf-string-literal.ts +110 -0
  95. package/src/ast/build/node/build-while-statement.ts +55 -0
  96. package/src/ast/build/util/array-dimension-kind-by-token-type-id-map.ts +167 -0
  97. package/src/ast/build/util/binary-operator-kind-by-token-type-id-map.ts +152 -0
  98. package/src/ast/build/util/build-context.ts +36 -0
  99. package/src/ast/build/util/class-id-kind-by-token-type-id-map.ts +160 -0
  100. package/src/ast/build/util/expression-kind-by-token-type-id-map.ts +168 -0
  101. package/src/ast/build/util/fetch-node.ts +433 -0
  102. package/src/ast/build/util/node-kind-by-token-type-id-map.ts +202 -0
  103. package/src/ast/build/util/statement-kind-by-token-type-id-map.ts +210 -0
  104. package/src/ast/build/util/string-literal-kind-by-token-type-id-map.ts +162 -0
  105. package/src/ast/build/util/token-kind-by-token-type-id-map.ts +337 -0
  106. package/src/ast/build-ast.ts +114 -0
  107. package/src/ast/node/abstract-array-definition.ts +17 -0
  108. package/src/ast/node/abstract-array-dimension.ts +20 -0
  109. package/src/ast/node/abstract-class-id.ts +13 -0
  110. package/src/ast/node/abstract-composite-node.ts +58 -0
  111. package/src/ast/node/abstract-elementary-type-definition.ts +29 -0
  112. package/src/ast/node/abstract-expression.ts +13 -0
  113. package/src/ast/node/abstract-leaf-node.ts +10 -0
  114. package/src/ast/node/abstract-node.ts +16 -0
  115. package/src/ast/node/abstract-statement.ts +13 -0
  116. package/src/ast/node/aggregate-output-value.ts +28 -0
  117. package/src/ast/node/aligned-modifier.ts +21 -0
  118. package/src/ast/node/array-definition.ts +44 -0
  119. package/src/ast/node/array-element-access.ts +24 -0
  120. package/src/ast/node/binary-expression.ts +28 -0
  121. package/src/ast/node/bit-modifier.ts +24 -0
  122. package/src/ast/node/case-clause.ts +30 -0
  123. package/src/ast/node/class-declaration.ts +39 -0
  124. package/src/ast/node/class-definition.ts +23 -0
  125. package/src/ast/node/class-id-range.ts +17 -0
  126. package/src/ast/node/class-id.ts +14 -0
  127. package/src/ast/node/class-member-access.ts +19 -0
  128. package/src/ast/node/compound-statement.ts +19 -0
  129. package/src/ast/node/computed-array-definition.ts +26 -0
  130. package/src/ast/node/computed-elementary-type-definition.ts +34 -0
  131. package/src/ast/node/default-clause.ts +26 -0
  132. package/src/ast/node/do-statement.ts +28 -0
  133. package/src/ast/node/elementary-type-definition.ts +49 -0
  134. package/src/ast/node/elementary-type-output-value.ts +19 -0
  135. package/src/ast/node/elementary-type.ts +18 -0
  136. package/src/ast/node/enum/array-dimension-kind.ts +11 -0
  137. package/src/ast/node/enum/binary-operator-kind.ts +41 -0
  138. package/src/ast/node/enum/class-id-kind.ts +12 -0
  139. package/src/ast/node/enum/elementary-type-kind.ts +13 -0
  140. package/src/ast/node/enum/expression-kind.ts +11 -0
  141. package/src/ast/node/enum/node-kind.ts +57 -0
  142. package/src/ast/node/enum/number-literal-kind.ts +17 -0
  143. package/src/ast/node/enum/statement-kind.ts +37 -0
  144. package/src/ast/node/enum/string-literal-kind.ts +13 -0
  145. package/src/ast/node/enum/string-variable-kind.ts +25 -0
  146. package/src/ast/node/enum/token-kind.ts +87 -0
  147. package/src/ast/node/expandable-modifier.ts +21 -0
  148. package/src/ast/node/explicit-array-dimension.ts +26 -0
  149. package/src/ast/node/expression-statement.ts +23 -0
  150. package/src/ast/node/extended-class-id-range.ts +20 -0
  151. package/src/ast/node/extends-modifier.ts +21 -0
  152. package/src/ast/node/for-statement.ts +29 -0
  153. package/src/ast/node/identifier.ts +12 -0
  154. package/src/ast/node/if-statement.ts +28 -0
  155. package/src/ast/node/implicit-array-dimension.ts +34 -0
  156. package/src/ast/node/length-attribute.ts +24 -0
  157. package/src/ast/node/length-of-expression.ts +21 -0
  158. package/src/ast/node/map-declaration.ts +34 -0
  159. package/src/ast/node/map-definition.ts +27 -0
  160. package/src/ast/node/map-entry.ts +18 -0
  161. package/src/ast/node/missing-error.ts +9 -0
  162. package/src/ast/node/number-literal.ts +18 -0
  163. package/src/ast/node/parameter-list.ts +25 -0
  164. package/src/ast/node/parameter-value-list.ts +29 -0
  165. package/src/ast/node/parameter.ts +20 -0
  166. package/src/ast/node/partial-array-dimension.ts +28 -0
  167. package/src/ast/node/specification.ts +21 -0
  168. package/src/ast/node/string-definition.ts +28 -0
  169. package/src/ast/node/string-literal.ts +21 -0
  170. package/src/ast/node/switch-statement.ts +32 -0
  171. package/src/ast/node/token.ts +20 -0
  172. package/src/ast/node/trivia.ts +13 -0
  173. package/src/ast/node/unary-expression.ts +31 -0
  174. package/src/ast/node/unexpected-error.ts +12 -0
  175. package/src/ast/node/while-statement.ts +26 -0
  176. package/src/ast/util/types.ts +138 -0
  177. package/src/ast/visitor/node-handler.ts +15 -0
  178. package/src/ast/visitor/node-visitor.ts +13 -0
  179. package/src/ast/visitor/traversing-visitor.ts +69 -0
  180. package/src/completion/completion-rules.ts +447 -0
  181. package/src/completion/get-potential-syntactic-tokens.ts +54 -0
  182. package/src/completion/get-potential-token-type-ids.ts +116 -0
  183. package/src/completion/hierarchical-map.ts +43 -0
  184. package/src/completion/rules/abstract-types.ts +174 -0
  185. package/src/completion/rules/aggregate-output-value-rules.ts +48 -0
  186. package/src/completion/rules/aligned-modifier-rules.ts +45 -0
  187. package/src/completion/rules/array-definition-rules.ts +87 -0
  188. package/src/completion/rules/array-element-access-rules.ts +22 -0
  189. package/src/completion/rules/assignment-expression-rules.ts +19 -0
  190. package/src/completion/rules/binary-expression-rules.ts +23 -0
  191. package/src/completion/rules/bit-modifier-rules.ts +25 -0
  192. package/src/completion/rules/case-clause-rules.ts +39 -0
  193. package/src/completion/rules/class-declaration-rules.ts +81 -0
  194. package/src/completion/rules/class-definition-rules.ts +28 -0
  195. package/src/completion/rules/class-id-range-rules.ts +17 -0
  196. package/src/completion/rules/class-id-rules.ts +9 -0
  197. package/src/completion/rules/class-member-access-rules.ts +7 -0
  198. package/src/completion/rules/compound-statement-rules.ts +25 -0
  199. package/src/completion/rules/computed-array-definition-rules.ts +25 -0
  200. package/src/completion/rules/computed-elementary-type-rules.ts +44 -0
  201. package/src/completion/rules/default-clause-rules.ts +23 -0
  202. package/src/completion/rules/do-statement-rules.ts +38 -0
  203. package/src/completion/rules/elementary-type-definition-rules.ts +62 -0
  204. package/src/completion/rules/elementary-type-output-value-rules.ts +13 -0
  205. package/src/completion/rules/elementary-type-rules.ts +18 -0
  206. package/src/completion/rules/expandable-modifier-rules.ts +21 -0
  207. package/src/completion/rules/explicit-array-dimension-rules.ts +22 -0
  208. package/src/completion/rules/expression-statement-rules.ts +22 -0
  209. package/src/completion/rules/extended-class-id-range-rules.ts +17 -0
  210. package/src/completion/rules/extends-modifier-rules.ts +17 -0
  211. package/src/completion/rules/for-statement-rules.ts +51 -0
  212. package/src/completion/rules/if-statement-rules.ts +42 -0
  213. package/src/completion/rules/implicit-array-dimension-rules.ts +21 -0
  214. package/src/completion/rules/length-attribute-rules.ts +19 -0
  215. package/src/completion/rules/lengthof-expression-rules.ts +23 -0
  216. package/src/completion/rules/map-declaration-rules.ts +45 -0
  217. package/src/completion/rules/map-definition-rules.ts +46 -0
  218. package/src/completion/rules/map-entry-rules.ts +17 -0
  219. package/src/completion/rules/parameter-list-rules.ts +21 -0
  220. package/src/completion/rules/parameter-rules.ts +17 -0
  221. package/src/completion/rules/parameter-value-list-rules.ts +27 -0
  222. package/src/completion/rules/partial-array-dimension-rules.ts +33 -0
  223. package/src/completion/rules/specification-rules.ts +37 -0
  224. package/src/completion/rules/string-definition-rules.ts +103 -0
  225. package/src/completion/rules/switch-statement-rules.ts +50 -0
  226. package/src/completion/rules/unary-expression-rules.ts +41 -0
  227. package/src/completion/rules/utf-string-literal-rules.ts +17 -0
  228. package/src/completion/rules/while-statement-rules.ts +38 -0
  229. package/src/lezer/context-tracker.ts +45 -0
  230. package/src/lezer/create-sdl-parser.ts +88 -0
  231. package/src/lezer/create-syntax-error-from-text-and-cursor.ts +110 -0
  232. package/src/lezer/create-syntax-error-from-text-and-position.ts +36 -0
  233. package/src/lezer/parser.terms.ts +129 -0
  234. package/src/lezer/parser.ts +63 -0
  235. package/src/lezer/props/fold-node-prop-source.ts +27 -0
  236. package/src/lezer/props/primitive-node-prop-source.ts +19 -0
  237. package/src/lezer/props/style-tags-node-prop-source.ts +188 -0
  238. package/src/lezer/props/syntactic-token-node-prop-source.ts +298 -0
  239. package/src/lezer/sdl-string-input.ts +46 -0
  240. package/src/location.ts +13 -0
  241. package/src/parse-helper.ts +93 -0
  242. package/src/prettier/prettier-plugin-sdl.ts +89 -0
  243. package/src/prettier/print-abstract-array-dimension.ts +84 -0
  244. package/src/prettier/print-abstract-class-id.ts +53 -0
  245. package/src/prettier/print-abstract-expression.ts +168 -0
  246. package/src/prettier/print-abstract-node.ts +224 -0
  247. package/src/prettier/print-abstract-statement.ts +124 -0
  248. package/src/prettier/print-aggregate-output-value.ts +35 -0
  249. package/src/prettier/print-aligned-modifier.ts +50 -0
  250. package/src/prettier/print-array-definition.ts +95 -0
  251. package/src/prettier/print-array-element-access.ts +14 -0
  252. package/src/prettier/print-bit-modifier.ts +48 -0
  253. package/src/prettier/print-case-clause.ts +58 -0
  254. package/src/prettier/print-class-declaration.ts +99 -0
  255. package/src/prettier/print-class-definition.ts +44 -0
  256. package/src/prettier/print-class-member-access.ts +13 -0
  257. package/src/prettier/print-computed-array-definition.ts +33 -0
  258. package/src/prettier/print-computed-elementary-type-definition.ts +60 -0
  259. package/src/prettier/print-default-clause.ts +39 -0
  260. package/src/prettier/print-do-statement.ts +31 -0
  261. package/src/prettier/print-elementary-type-definition.ts +108 -0
  262. package/src/prettier/print-elementary-type-output-value.ts +13 -0
  263. package/src/prettier/print-elementary-type.ts +28 -0
  264. package/src/prettier/print-expandable-modifier.ts +41 -0
  265. package/src/prettier/print-extends-modifier.ts +29 -0
  266. package/src/prettier/print-for-statement.ts +64 -0
  267. package/src/prettier/print-identifier.ts +10 -0
  268. package/src/prettier/print-if-statement.ts +76 -0
  269. package/src/prettier/print-length-attribute.ts +14 -0
  270. package/src/prettier/print-map-declaration.ts +75 -0
  271. package/src/prettier/print-map-definition.ts +67 -0
  272. package/src/prettier/print-map-entry.ts +22 -0
  273. package/src/prettier/print-number-literal.ts +84 -0
  274. package/src/prettier/print-parameter-list.ts +20 -0
  275. package/src/prettier/print-parameter-value-list.ts +20 -0
  276. package/src/prettier/print-parameter.ts +34 -0
  277. package/src/prettier/print-specification.ts +51 -0
  278. package/src/prettier/print-string-definition.ts +114 -0
  279. package/src/prettier/print-string-literal.ts +57 -0
  280. package/src/prettier/print-switch-statement.ts +46 -0
  281. package/src/prettier/print-token.ts +7 -0
  282. package/src/prettier/print-unexpected-error.ts +10 -0
  283. package/src/prettier/print-while-statement.ts +26 -0
  284. package/src/prettier/util/print-utils.ts +607 -0
  285. package/src/prettier/util/types.ts +40 -0
  286. package/src/scanner-error.ts +65 -0
  287. package/src/util/location-utils.ts +16 -0
  288. package/src/util/logger.ts +165 -0
  289. package/tests/analyser/create-sdl-analyser.test.ts +54 -0
  290. package/tests/analyser/get-sdl-analyser-test-scenarios.ts +80 -0
  291. package/tests/analyser/sdl-analyser-scenarios.test.ts +67 -0
  292. package/tests/analyser/semantic-error-scenarios.test.ts +40 -0
  293. package/tests/analyser/symbol-table.test.ts +188 -0
  294. package/tests/analyser/test-cases/array_definition.txt +50 -0
  295. package/tests/analyser/test-cases/class_declaration.txt +112 -0
  296. package/tests/analyser/test-cases/class_definition.txt +60 -0
  297. package/tests/analyser/test-cases/compound_statement.txt +63 -0
  298. package/tests/analyser/test-cases/computed_array_definition.txt +12 -0
  299. package/tests/analyser/test-cases/computed_elementary_type_definition.txt +39 -0
  300. package/tests/analyser/test-cases/do_statement.txt +16 -0
  301. package/tests/analyser/test-cases/elementary_type_definition.txt +58 -0
  302. package/tests/analyser/test-cases/expression.txt +159 -0
  303. package/tests/analyser/test-cases/for_statement.txt +55 -0
  304. package/tests/analyser/test-cases/if_statement.txt +57 -0
  305. package/tests/analyser/test-cases/map_declaration.txt +89 -0
  306. package/tests/analyser/test-cases/map_definition.txt +35 -0
  307. package/tests/analyser/test-cases/number_literal.txt +31 -0
  308. package/tests/analyser/test-cases/parameter_list.txt +16 -0
  309. package/tests/analyser/test-cases/scopes.txt +239 -0
  310. package/tests/analyser/test-cases/specification.txt +28 -0
  311. package/tests/analyser/test-cases/string_definition.txt +40 -0
  312. package/tests/analyser/test-cases/switch_statement.txt +96 -0
  313. package/tests/analyser/test-cases/while_statement.txt +16 -0
  314. package/tests/analyser/util/symbol-table-utils.test.ts +65 -0
  315. package/tests/ast/__snapshots__/build-ast.test.ts.snap +38897 -0
  316. package/tests/ast/build-ast.test.ts +463 -0
  317. package/tests/ast/visitor/traversing-visitor.test.ts +36 -0
  318. package/tests/completion/get-potential-syntactic-tokens.test.ts +89 -0
  319. package/tests/completion/get-potential-token-type-ids.test.ts +76 -0
  320. package/tests/completion/test-potential-syntactic-tokens-scenario.ts +24 -0
  321. package/tests/completion/test-potential-token-type-ids-scenario.ts +33 -0
  322. package/tests/fixtures/history-recording-node-handler.ts +232 -0
  323. package/tests/lezer/create-parse-error-from-text-and-position.test.ts +129 -0
  324. package/tests/lezer/create-sdl-parser.test.ts +15 -0
  325. package/tests/lezer/sdl-parser-scenarios.test.ts +24 -0
  326. package/tests/lezer/sdl-string-input.test.ts +12 -0
  327. package/tests/lezer/syntax-error-scenarios.test.ts +114 -0
  328. package/tests/lezer/test-cases/array_definition.txt +109 -0
  329. package/tests/lezer/test-cases/class_declaration.txt +141 -0
  330. package/tests/lezer/test-cases/class_definition.txt +89 -0
  331. package/tests/lezer/test-cases/comment.txt +335 -0
  332. package/tests/lezer/test-cases/compound_statement.txt +109 -0
  333. package/tests/lezer/test-cases/computed_array_definition.txt +33 -0
  334. package/tests/lezer/test-cases/computed_elementary_type_definition.txt +59 -0
  335. package/tests/lezer/test-cases/do_statement.txt +48 -0
  336. package/tests/lezer/test-cases/elementary_type_definition.txt +158 -0
  337. package/tests/lezer/test-cases/expression.txt +1203 -0
  338. package/tests/lezer/test-cases/for_statement.txt +183 -0
  339. package/tests/lezer/test-cases/if_statement.txt +183 -0
  340. package/tests/lezer/test-cases/invalid.txt +97 -0
  341. package/tests/lezer/test-cases/map_declaration.txt +152 -0
  342. package/tests/lezer/test-cases/map_definition.txt +59 -0
  343. package/tests/lezer/test-cases/number_literal.txt +273 -0
  344. package/tests/lezer/test-cases/specification.txt +38 -0
  345. package/tests/lezer/test-cases/string_definition.txt +263 -0
  346. package/tests/lezer/test-cases/string_literal.txt +303 -0
  347. package/tests/lezer/test-cases/switch_statement.txt +407 -0
  348. package/tests/lezer/test-cases/while_statement.txt +45 -0
  349. package/tests/parse-helper.test.ts +361 -0
  350. package/tests/prettier/__snapshots__/prettier-plugin-sdl.test.ts.snap +127 -0
  351. package/tests/prettier/prettier-plugin-sdl.test.ts +58 -0
  352. package/tests/prettier/print-array.test.ts +29 -0
  353. package/tests/prettier/print-class.test.ts +15 -0
  354. package/tests/prettier/print-do.test.ts +25 -0
  355. package/tests/prettier/print-elementary-type.test.ts +62 -0
  356. package/tests/prettier/print-error.test.ts +40 -0
  357. package/tests/prettier/print-expression.test.ts +17 -0
  358. package/tests/prettier/print-for.test.ts +23 -0
  359. package/tests/prettier/print-if.test.ts +71 -0
  360. package/tests/prettier/print-number-literal.test.ts +28 -0
  361. package/tests/prettier/print-specification.test.ts +54 -0
  362. package/tests/prettier/print-string.test.ts +36 -0
  363. package/tests/prettier/print-switch.test.ts +69 -0
  364. package/tests/prettier/print-while.test.ts +23 -0
  365. package/tests/prettier/test-prettier-scenario.ts +41 -0
  366. package/tests/prettier/util/print-utils.test.ts +665 -0
  367. package/tests/prettier/util/types.test.ts +38 -0
  368. package/tests/sample-specifications/invalid.sdl +24 -0
  369. package/tests/sample-specifications/prettified-invalid.sdl +28 -0
  370. package/tests/sample-specifications/prettified-various-elements-narrow.sdl +97 -0
  371. package/tests/sample-specifications/prettified-various-elements.sdl +90 -0
  372. package/tests/sample-specifications/sample.sdl +24 -0
  373. package/tests/sample-specifications/various-elements.sdl +88 -0
  374. package/tests/util/logger.test.ts +36 -0
  375. package/tsconfig.json +29 -0
@@ -0,0 +1,45 @@
1
+ # Simple
2
+
3
+ class A {while (i<9) {i++;}}
4
+
5
+ ==>
6
+
7
+ Specification(
8
+ ClassDeclaration(
9
+ class,
10
+ Whitespace
11
+ Identifier
12
+ Whitespace
13
+ OpenBrace
14
+ WhileStatement(
15
+ while
16
+ Whitespace
17
+ OpenParenthesis
18
+ BinaryExpression(
19
+ UnaryExpression(
20
+ Identifier
21
+ )
22
+ RelationalLessThan
23
+ UnaryExpression(
24
+ IntegerLiteral
25
+ )
26
+ )
27
+ CloseParenthesis
28
+ Whitespace
29
+ CompoundStatement(
30
+ OpenBrace
31
+ ExpressionStatement(
32
+ UnaryExpression(
33
+ UnaryExpression(
34
+ Identifier
35
+ )
36
+ PostfixIncrement
37
+ )
38
+ Semicolon
39
+ )
40
+ CloseBrace
41
+ )
42
+ )
43
+ CloseBrace
44
+ )
45
+ )
@@ -0,0 +1,361 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { type Buffer } from "node:buffer";
3
+ import path from "node:path";
4
+ import { promises as fs } from "node:fs";
5
+ import HistoryRecordingNodeHandler, {
6
+ expectedHistory,
7
+ } from "./fixtures/history-recording-node-handler.ts";
8
+ import { buildAst } from "../src/ast/build-ast.ts";
9
+ import {
10
+ createLenientSdlParser,
11
+ createStrictSdlParser,
12
+ } from "../src/lezer/create-sdl-parser.ts";
13
+ import {
14
+ collateSyntaxErrors,
15
+ dispatchNodeHandler,
16
+ prettyPrint,
17
+ } from "../src/parse-helper.ts";
18
+ import { SdlStringInput } from "../src/lezer/sdl-string-input.ts";
19
+ import type { Specification } from "../src/ast/node/specification.ts";
20
+
21
+ const strictSdlParser = createStrictSdlParser();
22
+ const lenientSdlParser = createLenientSdlParser();
23
+
24
+ describe("Parse Helper Tests", () => {
25
+ test("Test collateSyntaxErrors - invalid sample specification", async () => {
26
+ const sdlString = await fs.readFile(
27
+ path.join(__dirname, "./sample-specifications/invalid.sdl"),
28
+ ).then((buffer: Buffer) => buffer.toString());
29
+
30
+ const sdlStringInput = new SdlStringInput(sdlString);
31
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
32
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
33
+
34
+ expect(syntaxErrors).toHaveLength(5);
35
+ expect(syntaxErrors[0].errorLine).toEqual(
36
+ " bit transport_priority;",
37
+ );
38
+ expect(syntaxErrors[1].errorLine).toEqual(" unsigned int N = 184;");
39
+ expect(syntaxErrors[2].errorLine).toEqual(
40
+ " if (adaptation_ field_control == 0b01 || adaptation_field_control == 0b11) {",
41
+ );
42
+ expect(syntaxErrors[0].location!.column).toEqual(17);
43
+ expect(syntaxErrors[1].location!.column).toEqual(16);
44
+ expect(syntaxErrors[2].location!.column).toEqual(19);
45
+ });
46
+
47
+ test("Test collateSyntaxErrors - invalid elementary type definition", () => {
48
+ const sdlStringInput = new SdlStringInput(
49
+ "class A{bit transport_priority;}",
50
+ );
51
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
52
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
53
+
54
+ expect(syntaxErrors[0].message).toBe(
55
+ "SYNTAX ERROR: Expected: <LengthAttribute> => { row: 1, column: 13, position: 12 }",
56
+ );
57
+ });
58
+
59
+ test("Test collateSyntaxErrors - no class parameter values in parenthesis fails to parse", () => {
60
+ const sdlStringInput = new SdlStringInput("class A {ClassD d();}");
61
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
62
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
63
+
64
+ expect(syntaxErrors[0].message).toBe(
65
+ "SYNTAX ERROR: Expected one of: - ( + <BinaryLiteral> <DecimalLiteral> <FloatingPointLiteral> <HexadecimalLiteral> <Identifier> <IntegerLiteral> <LengthofExpression> <MultipleCharacterLiteral> => { row: 1, column: 19, position: 18 }",
66
+ );
67
+ });
68
+
69
+ test("Test collateSyntaxErrors - unexpected token fails to parse", () => {
70
+ const sdlStringInput = new SdlStringInput("class A {ClassD computed d;}");
71
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
72
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
73
+
74
+ expect(syntaxErrors[0].message).toBe(
75
+ "SYNTAX ERROR: Unexpected: computed => { row: 1, column: 17, position: 16 }",
76
+ );
77
+ });
78
+
79
+ test("Test collateSyntaxErrors - trailing comma in class parameter values fails to parse", () => {
80
+ const sdlStringInput = new SdlStringInput("class A {ClassD d(3,);}");
81
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
82
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
83
+
84
+ expect(syntaxErrors[0].message).toBe(
85
+ "SYNTAX ERROR: Expected one of: <BinaryExpression> <UnaryExpression> => { row: 1, column: 21, position: 20 }",
86
+ );
87
+ });
88
+
89
+ test("Test collateSyntaxErrors - unterminated class parameter values ending in expression fails to parse", () => {
90
+ const sdlStringInput = new SdlStringInput("class A {ClassD d(3;}");
91
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
92
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
93
+
94
+ expect(syntaxErrors[0].message).toBe(
95
+ "SYNTAX ERROR: Expected one of: - , != ) * / & && % + < << <= == > >= >> | || => { row: 1, column: 20, position: 19 }",
96
+ );
97
+ });
98
+
99
+ test("Test collateSyntaxErrors - unterminated class parameter values ending in comma fails to parse", () => {
100
+ const sdlStringInput = new SdlStringInput("class A {ClassD d(3,;}");
101
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
102
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
103
+
104
+ expect(syntaxErrors[0].message).toBe(
105
+ "SYNTAX ERROR: Expected one of: <BinaryExpression> <UnaryExpression> => { row: 1, column: 21, position: 20 }",
106
+ );
107
+ });
108
+
109
+ test("Test collateSyntaxErrors - unterminated string literal types fails to parse", () => {
110
+ const sdlStringInput = new SdlStringInput(
111
+ 'class A {utf8string d = u";}',
112
+ );
113
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
114
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
115
+
116
+ expect(syntaxErrors[0].message).toBe(
117
+ 'SYNTAX ERROR: Expected: " => { row: 1, column: 29, position: 28 }',
118
+ );
119
+ });
120
+
121
+ test("Test collateSyntaxErrors - missing utf string literal types fails to parse", () => {
122
+ const sdlStringInput = new SdlStringInput(
123
+ "class A {utf8string d = ;}",
124
+ );
125
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
126
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
127
+
128
+ expect(syntaxErrors[0].message).toBe(
129
+ "SYNTAX ERROR: Expected: <UtfStringLiteral> => { row: 1, column: 25, position: 24 }",
130
+ );
131
+ });
132
+
133
+ test("Test collateSyntaxErrors - missing string literal types fails to parse", () => {
134
+ const sdlStringInput = new SdlStringInput(
135
+ "class A {base64string d = ;}",
136
+ );
137
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
138
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
139
+
140
+ expect(syntaxErrors[0].message).toBe(
141
+ "SYNTAX ERROR: Expected: <Base64StringLiteral> => { row: 1, column: 27, position: 26 }",
142
+ );
143
+ });
144
+
145
+ test("Test collateSyntaxErrors - mix of concatenated string literal types fails to parse", () => {
146
+ const sdlStringInput = new SdlStringInput(
147
+ 'class A {utf8string d = u"hello" "world";}',
148
+ );
149
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
150
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
151
+
152
+ expect(syntaxErrors[0].message).toBe(
153
+ "SYNTAX ERROR: Expected: u => { row: 1, column: 34, position: 33 }",
154
+ );
155
+ });
156
+
157
+ test("Test collateSyntaxErrors - both legacy and reserved together fails to parse", () => {
158
+ const sdlStringInput = new SdlStringInput(
159
+ "class A {reserved legacy utfstring foo;}",
160
+ );
161
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
162
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
163
+
164
+ expect(syntaxErrors[0].message).toBe(
165
+ "SYNTAX ERROR: Expected one of: base64string const utf16string utf8string utfstring <AlignedModifier> => { row: 1, column: 19, position: 18 }",
166
+ );
167
+ });
168
+
169
+ test("Test collateSyntaxErrors - unexpected prefix for string literal", () => {
170
+ const sdlStringInput = new SdlStringInput(
171
+ 'class A {base64string foo = u"aGVsbG8K";}',
172
+ );
173
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
174
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
175
+
176
+ expect(syntaxErrors[0].message).toBe(
177
+ "SYNTAX ERROR: Unexpected: u => { row: 1, column: 29, position: 28 }",
178
+ );
179
+ });
180
+
181
+ test("Test collateSyntaxErrors - invalid basic string literal type fails to parse", () => {
182
+ const sdlStringInput = new SdlStringInput(
183
+ 'class A {utf8string foo = "hello";}',
184
+ );
185
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
186
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
187
+
188
+ expect(syntaxErrors[0].message).toBe(
189
+ "SYNTAX ERROR: Expected: u => { row: 1, column: 27, position: 26 }",
190
+ );
191
+ });
192
+
193
+ test("Test collateSyntaxErrors - invalid prefix for string literal", () => {
194
+ const sdlStringInput = new SdlStringInput(
195
+ 'class A {utf8string foo = u8"hello";}',
196
+ );
197
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
198
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
199
+
200
+ expect(syntaxErrors[0].message).toBe(
201
+ "SYNTAX ERROR: Unexpected: 8 => { row: 1, column: 28, position: 27 }",
202
+ );
203
+ });
204
+
205
+ test("Test collateSyntaxErrors - illegal alignment bit count value fails to parse", () => {
206
+ const sdlStringInput = new SdlStringInput(
207
+ "class A {aligned(17) utf8string foo;}",
208
+ );
209
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
210
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
211
+
212
+ expect(syntaxErrors[0].message).toBe(
213
+ "SYNTAX ERROR: Expected one of: 8 16 32 64 128 => { row: 1, column: 18, position: 17 }",
214
+ );
215
+ });
216
+
217
+ test("Test collateSyntaxErrors - unterminated alignment modifier fails to parse", () => {
218
+ const sdlStringInput = new SdlStringInput(
219
+ "class A {aligned(16 utf8string foo;}",
220
+ );
221
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
222
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
223
+
224
+ expect(syntaxErrors[0].message).toBe(
225
+ "SYNTAX ERROR: Expected: ) => { row: 1, column: 21, position: 20 }",
226
+ );
227
+ });
228
+
229
+ test("Test collateSyntaxErrors - while loop fails to parse", () => {
230
+ const sdlStringInput = new SdlStringInput("class A {while (1)}");
231
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
232
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
233
+
234
+ expect(syntaxErrors[0].message).toBe(
235
+ "SYNTAX ERROR: Expected one of: <ArrayDefinition> <ClassDefinition> <CompoundStatement> <ComputedArrayDefinition> <ComputedElementaryTypeDefinition> <DoStatement> <ElementaryTypeDefinition> <ExpressionStatement> <ForStatement> <IfStatement> <MapDefinition> <StringDefinition> <SwitchStatement> <WhileStatement> => { row: 1, column: 19, position: 18 }",
236
+ );
237
+ });
238
+
239
+ test("Test collateSyntaxErrors - empty specification produces no errors", () => {
240
+ const sdlStringInput = new SdlStringInput("");
241
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
242
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
243
+
244
+ expect(syntaxErrors.length).toBe(0);
245
+ });
246
+
247
+ test("Test collateSyntaxErrors - single invalid token produces error", () => {
248
+ const sdlStringInput = new SdlStringInput("§");
249
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
250
+ const syntaxErrors = collateSyntaxErrors(parseTree, sdlStringInput);
251
+
252
+ expect(syntaxErrors[0].message).toBe(
253
+ "SYNTAX ERROR: Unknown token: § => { row: 1, column: 1, position: 0 }",
254
+ );
255
+ });
256
+
257
+ test("Test prettyPrint", async () => {
258
+ const sdlString = await fs.readFile(
259
+ path.join(__dirname, "./sample-specifications/various-elements.sdl"),
260
+ ).then((buffer: Buffer) => buffer.toString());
261
+ let expectedSdlString = await fs.readFile(
262
+ path.join(
263
+ __dirname,
264
+ "./sample-specifications/prettified-various-elements.sdl",
265
+ ),
266
+ ).then((buffer: Buffer) => buffer.toString());
267
+ expectedSdlString = expectedSdlString.replace(/\r/g, "");
268
+
269
+ const sdlStringInput = new SdlStringInput(sdlString);
270
+ const parseTree = strictSdlParser.parse(sdlStringInput);
271
+ const specification = buildAst(parseTree, sdlStringInput);
272
+
273
+ const prettifiedSdlString = await prettyPrint(
274
+ specification as Specification,
275
+ sdlStringInput,
276
+ );
277
+ expect(
278
+ prettifiedSdlString,
279
+ ).toEqual(
280
+ expectedSdlString,
281
+ );
282
+ });
283
+
284
+ test("Test prettyPrint - narrower", async () => {
285
+ const sdlString = await fs.readFile(
286
+ path.join(__dirname, "./sample-specifications/various-elements.sdl"),
287
+ ).then((buffer: Buffer) => buffer.toString());
288
+ let expectedSdlString = await fs.readFile(
289
+ path.join(
290
+ __dirname,
291
+ "./sample-specifications/prettified-various-elements-narrow.sdl",
292
+ ),
293
+ ).then((buffer: Buffer) => buffer.toString());
294
+ expectedSdlString = expectedSdlString.replace(/\r/g, "");
295
+
296
+ const sdlStringInput = new SdlStringInput(sdlString);
297
+ const parseTree = strictSdlParser.parse(sdlStringInput);
298
+ const specification = buildAst(parseTree, sdlStringInput);
299
+
300
+ const narrowPrettifiedSdlString = await prettyPrint(
301
+ specification as Specification,
302
+ sdlStringInput,
303
+ 40,
304
+ );
305
+ expect(
306
+ narrowPrettifiedSdlString,
307
+ ).toEqual(
308
+ expectedSdlString,
309
+ );
310
+ });
311
+
312
+ test("Test prettyPrint - with syntax errors", async () => {
313
+ const sdlString = await fs.readFile(
314
+ path.join(__dirname, "./sample-specifications/invalid.sdl"),
315
+ ).then((buffer: Buffer) => buffer.toString());
316
+ let expectedSdlString = await fs.readFile(
317
+ path.join(
318
+ __dirname,
319
+ "./sample-specifications/prettified-invalid.sdl",
320
+ ),
321
+ ).then((buffer: Buffer) => buffer.toString());
322
+ expectedSdlString = expectedSdlString.replace(/\r/g, "");
323
+
324
+ const sdlStringInput = new SdlStringInput(sdlString);
325
+ const parseTree = lenientSdlParser.parse(sdlStringInput);
326
+ const specification = buildAst(parseTree, sdlStringInput, true);
327
+
328
+ const invalidPrettifiedSdlString = await prettyPrint(
329
+ specification as Specification,
330
+ sdlStringInput,
331
+ );
332
+ expect(
333
+ invalidPrettifiedSdlString,
334
+ ).toEqual(
335
+ expectedSdlString,
336
+ );
337
+ });
338
+
339
+ test("Test dispatchNodeHandler", async () => {
340
+ const sdlString = await fs.readFile(
341
+ path.join(__dirname, "./sample-specifications/sample.sdl"),
342
+ ).then((buffer: Buffer) => buffer.toString());
343
+
344
+ const sdlStringInput = new SdlStringInput(sdlString);
345
+ const parseTree = strictSdlParser.parse(sdlStringInput);
346
+ const specification = buildAst(parseTree, sdlStringInput);
347
+
348
+ const historyRecordingNodeHandler = new HistoryRecordingNodeHandler();
349
+
350
+ dispatchNodeHandler(
351
+ specification as Specification,
352
+ historyRecordingNodeHandler,
353
+ );
354
+
355
+ expect(
356
+ historyRecordingNodeHandler.nodeHistory,
357
+ ).toEqual(
358
+ expectedHistory,
359
+ );
360
+ });
361
+ });
@@ -0,0 +1,127 @@
1
+ // Bun Snapshot v1, https://bun.sh/docs/test/snapshots
2
+
3
+ exports[`Prettier Plugin SDL tests prettified output is as expected 1`] = `
4
+ "// repeated packets in bitstream
5
+ //
6
+ // should be declared in a wrapping Bitstream class
7
+ class transport_packet {
8
+ unsigned int(8) sync_byte = 0x47;
9
+ bit(1) transport_error_indicator;
10
+ bit(1) payload_unit_start_indicator;
11
+ bit(1) transport_priority;
12
+ unsigned int(13 // odd but valid comment position
13
+ ) PID;
14
+ bit(2)* transport_scrambling_control;
15
+ aligned(16) bit(2) adaptation_field_control = 0x0000.0000;
16
+
17
+ computed unsigned int N = 184;
18
+ computed float test = 1e1;
19
+ utf8string foo = u"bar" u"foo";
20
+ base64string foo2 = "BAR";
21
+ computed int dummy1 = 'ABCD' '01AB';
22
+ computed int dummy2 = lengthof(transport_scrambling_control) + N++;
23
+ if (adaptation_field_control == 0b10 || adaptation_field_control == 0b11) {
24
+ adaptation_field data; // previously declared class
25
+
26
+ // N is 184 minus the number of bytes in the adaptation_field
27
+ N = N - // and another
28
+ 1 // and another odd comment
29
+ - data.adaptation_field_length;
30
+ }
31
+ if (adaptation_field_control == 0b01 || adaptation_field_control == 0b11) {
32
+ bit(8) data_byte[N];
33
+ }
34
+ }
35
+
36
+ map Offsets (int) {
37
+ 0b00, { 1024 },
38
+ 0b01, { 2048 }
39
+ }
40
+
41
+ expandable(20) class odd(int a, B b) extends odder(a) : bit(2) id = 2 {
42
+ reserved unsigned int(Offsets) offset;
43
+ computed int myArray[2];
44
+ computed int i = 1;
45
+ Strange s(1, 2);
46
+ while (i < 2) {
47
+ i++;
48
+ }
49
+ i = 2;
50
+ do {
51
+ i--;
52
+ } while (i > 0);
53
+ if (1) {
54
+ i++;
55
+ }
56
+ else if (1) {
57
+ i--;
58
+ }
59
+ else {
60
+ i++;
61
+ }
62
+ if (1)
63
+ if (2)
64
+ i--;
65
+ else
66
+ i++;
67
+ switch (i) {
68
+ case 0:
69
+ case 1:
70
+ // nothing
71
+ break;
72
+ case 2: { // nothing
73
+ break;
74
+ }
75
+ default:
76
+ i++;
77
+ // foo
78
+ }
79
+ }
80
+
81
+ class odd2(int a) {
82
+ computed int x = 0;
83
+ for (computed int j = 0; j < 2; j++) {
84
+ x++;
85
+ }
86
+ for (; j > 0; j--) {
87
+ }
88
+ }
89
+
90
+ class A {
91
+ MyClass a[1..3];
92
+ reserved MyClass b[3][[2]];
93
+ } // end of file
94
+ "
95
+ `;
96
+
97
+ exports[`Prettier Plugin SDL tests can prettify with syntax errors 1`] = `
98
+ "// repeated packets in bitstream
99
+ class transport_packet {
100
+ unsigned int(8) sync_byte = 0x47;
101
+ bit(1) transport_error_indicator;
102
+ bit(1) payload_unit_start_indicator;
103
+ bit
104
+ transport_priority;
105
+ unsigned int(13) PID;
106
+ bit(2) transport_scrambling_control;
107
+ bit(2) adaptation_field_control;
108
+
109
+ unsigned int
110
+ N = 184;
111
+
112
+ if (adaptation_field_control == 0b10 || adaptation_field_control == 0b11) {
113
+ adaptation_field data; // previously declared class
114
+
115
+ // N is 184 minus the number of bytes in the adaptation_field
116
+ N = N - 1 - data.adaptation_field_length;
117
+ }
118
+
119
+ if (adaptation_
120
+
121
+ field_control == 0b01 || adaptation_field_control == 0b11)
122
+ {
123
+ bit(8) data_byte[N];
124
+ }
125
+ } // until EOF
126
+ "
127
+ `;
@@ -0,0 +1,58 @@
1
+ import * as prettier from "prettier";
2
+ import { promises as fs } from "node:fs";
3
+ import { type Buffer } from "node:buffer";
4
+ import path from "node:path";
5
+ import { describe, expect, test } from "bun:test";
6
+ import { prettierPluginSdl } from "../../src/prettier/prettier-plugin-sdl.ts";
7
+ import { createStrictSdlParser } from "../../src/lezer/create-sdl-parser.ts";
8
+ import { SdlStringInput } from "../../src/lezer/sdl-string-input.ts";
9
+
10
+ const strictSdlParser = createStrictSdlParser();
11
+
12
+ describe("Prettier Plugin SDL tests", () => {
13
+ test("prettified output is as expected", async () => {
14
+ const sampleSdlSpecification = await fs.readFile(
15
+ path.join(__dirname, "../sample-specifications/various-elements.sdl"),
16
+ ).then((buffer: Buffer) => buffer.toString());
17
+
18
+ const options: prettier.Options = {
19
+ parser: "sdl",
20
+ plugins: [prettierPluginSdl],
21
+ };
22
+
23
+ const prettified = await prettier.format(sampleSdlSpecification, options);
24
+
25
+ expect(prettified).toMatchSnapshot();
26
+ });
27
+
28
+ test("prettified output is valid", async () => {
29
+ const sampleSdlSpecification = await fs.readFile(
30
+ path.join(__dirname, "../sample-specifications/various-elements.sdl"),
31
+ ).then((buffer: Buffer) => buffer.toString());
32
+
33
+ const options: prettier.Options = {
34
+ parser: "sdl",
35
+ plugins: [prettierPluginSdl],
36
+ };
37
+
38
+ const prettified = await prettier.format(sampleSdlSpecification, options);
39
+ const sdlStringInput = new SdlStringInput(prettified);
40
+
41
+ strictSdlParser.parse(sdlStringInput);
42
+ });
43
+
44
+ test("can prettify with syntax errors", async () => {
45
+ const sampleSdlSpecification = await fs.readFile(
46
+ path.join(__dirname, "../sample-specifications/invalid.sdl"),
47
+ ).then((buffer: Buffer) => buffer.toString());
48
+
49
+ const options: prettier.Options = {
50
+ parser: "sdl",
51
+ plugins: [prettierPluginSdl],
52
+ };
53
+
54
+ const prettified = await prettier.format(sampleSdlSpecification, options);
55
+
56
+ expect(prettified).toMatchSnapshot();
57
+ });
58
+ });
@@ -0,0 +1,29 @@
1
+ import { describe, test } from "bun:test";
2
+ import { testPrettierScenario } from "./test-prettier-scenario.ts";
3
+
4
+ describe("Print Array tests", () => {
5
+ test("prettified array output is as expected", async () => {
6
+ await testPrettierScenario(
7
+ "class A{aligned(16) bit(2) adaptation_field_control[4][5];}",
8
+ "class A {\n" +
9
+ " aligned(16) bit(2) adaptation_field_control[4][5];\n" +
10
+ "}\n",
11
+ "class A {\n" +
12
+ " aligned(16) bit(2)\n" +
13
+ " adaptation_field_control[4][5];\n" +
14
+ "}\n",
15
+ );
16
+ });
17
+
18
+ test("prettified array output with implicit array is as expected", async () => {
19
+ await testPrettierScenario(
20
+ "class A{B b[2..4];}",
21
+ "class A {\n" +
22
+ " B b[2..4];\n" +
23
+ "}\n",
24
+ "class A {\n" +
25
+ " B b[2..4];\n" +
26
+ "}\n",
27
+ );
28
+ });
29
+ });
@@ -0,0 +1,15 @@
1
+ import { describe, test } from "bun:test";
2
+ import { testPrettierScenario } from "./test-prettier-scenario.ts";
3
+
4
+ describe("Print Class tests", () => {
5
+ test("prettified class output is as expected", async () => {
6
+ await testPrettierScenario(
7
+ "expandable(20) class odd(int a,B b) extends odder(a):bit(2) id= 2{}",
8
+ "expandable(20) class odd(int a, B b) extends odder(a) : bit(2) id = 2 {\n" +
9
+ "}\n",
10
+ "expandable(20) class odd(int a, B b)\n" +
11
+ " extends odder(a) : bit(2) id = 2 {\n" +
12
+ "}\n",
13
+ );
14
+ });
15
+ });
@@ -0,0 +1,25 @@
1
+ import { describe, test } from "bun:test";
2
+ import { testPrettierScenario } from "./test-prettier-scenario.ts";
3
+
4
+ describe("Print Do tests", () => {
5
+ test("prettified do output is as expected", async () => {
6
+ await testPrettierScenario(
7
+ "class A{do { a[long_variable_name] = long_variable_name + 1; } while (another_long_variable_name > another_long_variable_name_2 + 1);}",
8
+ "class A {\n" +
9
+ " do {\n" +
10
+ " a[long_variable_name] = long_variable_name + 1;\n" +
11
+ " } while (another_long_variable_name > another_long_variable_name_2 + 1);\n" +
12
+ "}\n",
13
+ "class A {\n" +
14
+ " do {\n" +
15
+ " a[long_variable_name] =\n" +
16
+ " long_variable_name +\n" +
17
+ " 1;\n" +
18
+ " } while\n" +
19
+ " (another_long_variable_name >\n" +
20
+ " another_long_variable_name_2 +\n" +
21
+ " 1);\n" +
22
+ "}\n",
23
+ );
24
+ });
25
+ });