@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.
- package/LICENSE +21 -0
- package/README.md +337 -0
- package/bun.lock +65 -0
- package/dist/index.js +11829 -0
- package/grammar/sdl.ebnf.grammar +399 -0
- package/grammar/sdl.lezer.grammar +629 -0
- package/index.ts +83 -0
- package/package.json +61 -0
- package/src/analyser/checks/check-array-definition.ts +90 -0
- package/src/analyser/checks/check-array-element-access.ts +31 -0
- package/src/analyser/checks/check-class-declaration.ts +417 -0
- package/src/analyser/checks/check-computed-array-definition.ts +44 -0
- package/src/analyser/checks/check-computed-elementary-type-definition.ts +45 -0
- package/src/analyser/checks/check-elementary-type-definition.ts +167 -0
- package/src/analyser/checks/check-expression-binary.ts +242 -0
- package/src/analyser/checks/check-expression-lengthof.ts +34 -0
- package/src/analyser/checks/check-expression-unary.ts +166 -0
- package/src/analyser/checks/check-identifier.ts +71 -0
- package/src/analyser/checks/check-if-statement.ts +164 -0
- package/src/analyser/checks/check-map-declaration.ts +180 -0
- package/src/analyser/checks/check-map-definition.ts +118 -0
- package/src/analyser/checks/check-number-literal-float.ts +28 -0
- package/src/analyser/checks/check-parameter-list.ts +42 -0
- package/src/analyser/checks/check-specification.ts +53 -0
- package/src/analyser/checks/check-switch-statement.ts +206 -0
- package/src/analyser/checks/check.ts +20 -0
- package/src/analyser/create-sdl-analyser.ts +85 -0
- package/src/analyser/node-handler/abstract-analysis-node-handler.ts +626 -0
- package/src/analyser/node-handler/build-symbol-table-node-handler.ts +649 -0
- package/src/analyser/node-handler/specific-check-node-handler.ts +46 -0
- package/src/analyser/node-handler/validate-scope-node-handler.ts +290 -0
- package/src/analyser/node-handler/validate-type-node-handler.ts +497 -0
- package/src/analyser/sdl-analyser.ts +119 -0
- package/src/analyser/symbol-table.ts +388 -0
- package/src/analyser/util/symbol-table-utils.ts +458 -0
- package/src/analyser/util/type-utils.ts +238 -0
- package/src/ast/build/consume/consume-abstract-node.ts +199 -0
- package/src/ast/build/consume/consume-ast-node.ts +400 -0
- package/src/ast/build/consume/consume-primitive.ts +59 -0
- package/src/ast/build/consume/consume-trivia.ts +96 -0
- package/src/ast/build/consume/consume-white-space-and-missing-errors.ts +27 -0
- package/src/ast/build/node/build-aggregate-output-value.ts +57 -0
- package/src/ast/build/node/build-aligned-modifier.ts +99 -0
- package/src/ast/build/node/build-array-definition.ts +131 -0
- package/src/ast/build/node/build-array-element-access.ts +45 -0
- package/src/ast/build/node/build-base64-string-literal.ts +41 -0
- package/src/ast/build/node/build-binary-expression.ts +113 -0
- package/src/ast/build/node/build-binary-literal.ts +37 -0
- package/src/ast/build/node/build-bit-modifier.ts +86 -0
- package/src/ast/build/node/build-case-clause.ts +101 -0
- package/src/ast/build/node/build-class-declaration.ts +124 -0
- package/src/ast/build/node/build-class-definition.ts +56 -0
- package/src/ast/build/node/build-class-id-range.ts +32 -0
- package/src/ast/build/node/build-class-id.ts +19 -0
- package/src/ast/build/node/build-class-member-access.ts +27 -0
- package/src/ast/build/node/build-compound-statement.ts +33 -0
- package/src/ast/build/node/build-computed-array-definition.ts +53 -0
- package/src/ast/build/node/build-computed-elementary-type-definition.ts +100 -0
- package/src/ast/build/node/build-decimal-literal.ts +15 -0
- package/src/ast/build/node/build-default-clause.ts +76 -0
- package/src/ast/build/node/build-do-statement.ts +71 -0
- package/src/ast/build/node/build-elementary-type-definition.ts +164 -0
- package/src/ast/build/node/build-elementary-type-output-value.ts +25 -0
- package/src/ast/build/node/build-elementary-type.ts +36 -0
- package/src/ast/build/node/build-expandable-modifier.ts +68 -0
- package/src/ast/build/node/build-explicit-array-dimension.ts +37 -0
- package/src/ast/build/node/build-expression-statement.ts +31 -0
- package/src/ast/build/node/build-extended-class-id-range.ts +27 -0
- package/src/ast/build/node/build-extends-modifier.ts +41 -0
- package/src/ast/build/node/build-floating-point-literal.ts +15 -0
- package/src/ast/build/node/build-for-statement.ts +117 -0
- package/src/ast/build/node/build-hexadecimal-literal.ts +38 -0
- package/src/ast/build/node/build-identifier.ts +8 -0
- package/src/ast/build/node/build-if-statement.ts +82 -0
- package/src/ast/build/node/build-implicit-array-dimension.ts +78 -0
- package/src/ast/build/node/build-integer-literal.ts +15 -0
- package/src/ast/build/node/build-length-attribute.ts +48 -0
- package/src/ast/build/node/build-lengthof-expression.ts +45 -0
- package/src/ast/build/node/build-map-declaration.ts +110 -0
- package/src/ast/build/node/build-map-definition.ts +103 -0
- package/src/ast/build/node/build-map-entry.ts +33 -0
- package/src/ast/build/node/build-missing-error.ts +10 -0
- package/src/ast/build/node/build-multiple-character-literal.ts +65 -0
- package/src/ast/build/node/build-parameter-list.ts +50 -0
- package/src/ast/build/node/build-parameter-value-list.ts +54 -0
- package/src/ast/build/node/build-parameter.ts +47 -0
- package/src/ast/build/node/build-partial-array-dimension.ts +55 -0
- package/src/ast/build/node/build-specification.ts +24 -0
- package/src/ast/build/node/build-string-definition.ts +106 -0
- package/src/ast/build/node/build-switch-statement.ts +84 -0
- package/src/ast/build/node/build-token.ts +26 -0
- package/src/ast/build/node/build-unary-expression.ts +109 -0
- package/src/ast/build/node/build-unexpected-error.ts +14 -0
- package/src/ast/build/node/build-utf-string-literal.ts +110 -0
- package/src/ast/build/node/build-while-statement.ts +55 -0
- package/src/ast/build/util/array-dimension-kind-by-token-type-id-map.ts +167 -0
- package/src/ast/build/util/binary-operator-kind-by-token-type-id-map.ts +152 -0
- package/src/ast/build/util/build-context.ts +36 -0
- package/src/ast/build/util/class-id-kind-by-token-type-id-map.ts +160 -0
- package/src/ast/build/util/expression-kind-by-token-type-id-map.ts +168 -0
- package/src/ast/build/util/fetch-node.ts +433 -0
- package/src/ast/build/util/node-kind-by-token-type-id-map.ts +202 -0
- package/src/ast/build/util/statement-kind-by-token-type-id-map.ts +210 -0
- package/src/ast/build/util/string-literal-kind-by-token-type-id-map.ts +162 -0
- package/src/ast/build/util/token-kind-by-token-type-id-map.ts +337 -0
- package/src/ast/build-ast.ts +114 -0
- package/src/ast/node/abstract-array-definition.ts +17 -0
- package/src/ast/node/abstract-array-dimension.ts +20 -0
- package/src/ast/node/abstract-class-id.ts +13 -0
- package/src/ast/node/abstract-composite-node.ts +58 -0
- package/src/ast/node/abstract-elementary-type-definition.ts +29 -0
- package/src/ast/node/abstract-expression.ts +13 -0
- package/src/ast/node/abstract-leaf-node.ts +10 -0
- package/src/ast/node/abstract-node.ts +16 -0
- package/src/ast/node/abstract-statement.ts +13 -0
- package/src/ast/node/aggregate-output-value.ts +28 -0
- package/src/ast/node/aligned-modifier.ts +21 -0
- package/src/ast/node/array-definition.ts +44 -0
- package/src/ast/node/array-element-access.ts +24 -0
- package/src/ast/node/binary-expression.ts +28 -0
- package/src/ast/node/bit-modifier.ts +24 -0
- package/src/ast/node/case-clause.ts +30 -0
- package/src/ast/node/class-declaration.ts +39 -0
- package/src/ast/node/class-definition.ts +23 -0
- package/src/ast/node/class-id-range.ts +17 -0
- package/src/ast/node/class-id.ts +14 -0
- package/src/ast/node/class-member-access.ts +19 -0
- package/src/ast/node/compound-statement.ts +19 -0
- package/src/ast/node/computed-array-definition.ts +26 -0
- package/src/ast/node/computed-elementary-type-definition.ts +34 -0
- package/src/ast/node/default-clause.ts +26 -0
- package/src/ast/node/do-statement.ts +28 -0
- package/src/ast/node/elementary-type-definition.ts +49 -0
- package/src/ast/node/elementary-type-output-value.ts +19 -0
- package/src/ast/node/elementary-type.ts +18 -0
- package/src/ast/node/enum/array-dimension-kind.ts +11 -0
- package/src/ast/node/enum/binary-operator-kind.ts +41 -0
- package/src/ast/node/enum/class-id-kind.ts +12 -0
- package/src/ast/node/enum/elementary-type-kind.ts +13 -0
- package/src/ast/node/enum/expression-kind.ts +11 -0
- package/src/ast/node/enum/node-kind.ts +57 -0
- package/src/ast/node/enum/number-literal-kind.ts +17 -0
- package/src/ast/node/enum/statement-kind.ts +37 -0
- package/src/ast/node/enum/string-literal-kind.ts +13 -0
- package/src/ast/node/enum/string-variable-kind.ts +25 -0
- package/src/ast/node/enum/token-kind.ts +87 -0
- package/src/ast/node/expandable-modifier.ts +21 -0
- package/src/ast/node/explicit-array-dimension.ts +26 -0
- package/src/ast/node/expression-statement.ts +23 -0
- package/src/ast/node/extended-class-id-range.ts +20 -0
- package/src/ast/node/extends-modifier.ts +21 -0
- package/src/ast/node/for-statement.ts +29 -0
- package/src/ast/node/identifier.ts +12 -0
- package/src/ast/node/if-statement.ts +28 -0
- package/src/ast/node/implicit-array-dimension.ts +34 -0
- package/src/ast/node/length-attribute.ts +24 -0
- package/src/ast/node/length-of-expression.ts +21 -0
- package/src/ast/node/map-declaration.ts +34 -0
- package/src/ast/node/map-definition.ts +27 -0
- package/src/ast/node/map-entry.ts +18 -0
- package/src/ast/node/missing-error.ts +9 -0
- package/src/ast/node/number-literal.ts +18 -0
- package/src/ast/node/parameter-list.ts +25 -0
- package/src/ast/node/parameter-value-list.ts +29 -0
- package/src/ast/node/parameter.ts +20 -0
- package/src/ast/node/partial-array-dimension.ts +28 -0
- package/src/ast/node/specification.ts +21 -0
- package/src/ast/node/string-definition.ts +28 -0
- package/src/ast/node/string-literal.ts +21 -0
- package/src/ast/node/switch-statement.ts +32 -0
- package/src/ast/node/token.ts +20 -0
- package/src/ast/node/trivia.ts +13 -0
- package/src/ast/node/unary-expression.ts +31 -0
- package/src/ast/node/unexpected-error.ts +12 -0
- package/src/ast/node/while-statement.ts +26 -0
- package/src/ast/util/types.ts +138 -0
- package/src/ast/visitor/node-handler.ts +15 -0
- package/src/ast/visitor/node-visitor.ts +13 -0
- package/src/ast/visitor/traversing-visitor.ts +69 -0
- package/src/completion/completion-rules.ts +447 -0
- package/src/completion/get-potential-syntactic-tokens.ts +54 -0
- package/src/completion/get-potential-token-type-ids.ts +116 -0
- package/src/completion/hierarchical-map.ts +43 -0
- package/src/completion/rules/abstract-types.ts +174 -0
- package/src/completion/rules/aggregate-output-value-rules.ts +48 -0
- package/src/completion/rules/aligned-modifier-rules.ts +45 -0
- package/src/completion/rules/array-definition-rules.ts +87 -0
- package/src/completion/rules/array-element-access-rules.ts +22 -0
- package/src/completion/rules/assignment-expression-rules.ts +19 -0
- package/src/completion/rules/binary-expression-rules.ts +23 -0
- package/src/completion/rules/bit-modifier-rules.ts +25 -0
- package/src/completion/rules/case-clause-rules.ts +39 -0
- package/src/completion/rules/class-declaration-rules.ts +81 -0
- package/src/completion/rules/class-definition-rules.ts +28 -0
- package/src/completion/rules/class-id-range-rules.ts +17 -0
- package/src/completion/rules/class-id-rules.ts +9 -0
- package/src/completion/rules/class-member-access-rules.ts +7 -0
- package/src/completion/rules/compound-statement-rules.ts +25 -0
- package/src/completion/rules/computed-array-definition-rules.ts +25 -0
- package/src/completion/rules/computed-elementary-type-rules.ts +44 -0
- package/src/completion/rules/default-clause-rules.ts +23 -0
- package/src/completion/rules/do-statement-rules.ts +38 -0
- package/src/completion/rules/elementary-type-definition-rules.ts +62 -0
- package/src/completion/rules/elementary-type-output-value-rules.ts +13 -0
- package/src/completion/rules/elementary-type-rules.ts +18 -0
- package/src/completion/rules/expandable-modifier-rules.ts +21 -0
- package/src/completion/rules/explicit-array-dimension-rules.ts +22 -0
- package/src/completion/rules/expression-statement-rules.ts +22 -0
- package/src/completion/rules/extended-class-id-range-rules.ts +17 -0
- package/src/completion/rules/extends-modifier-rules.ts +17 -0
- package/src/completion/rules/for-statement-rules.ts +51 -0
- package/src/completion/rules/if-statement-rules.ts +42 -0
- package/src/completion/rules/implicit-array-dimension-rules.ts +21 -0
- package/src/completion/rules/length-attribute-rules.ts +19 -0
- package/src/completion/rules/lengthof-expression-rules.ts +23 -0
- package/src/completion/rules/map-declaration-rules.ts +45 -0
- package/src/completion/rules/map-definition-rules.ts +46 -0
- package/src/completion/rules/map-entry-rules.ts +17 -0
- package/src/completion/rules/parameter-list-rules.ts +21 -0
- package/src/completion/rules/parameter-rules.ts +17 -0
- package/src/completion/rules/parameter-value-list-rules.ts +27 -0
- package/src/completion/rules/partial-array-dimension-rules.ts +33 -0
- package/src/completion/rules/specification-rules.ts +37 -0
- package/src/completion/rules/string-definition-rules.ts +103 -0
- package/src/completion/rules/switch-statement-rules.ts +50 -0
- package/src/completion/rules/unary-expression-rules.ts +41 -0
- package/src/completion/rules/utf-string-literal-rules.ts +17 -0
- package/src/completion/rules/while-statement-rules.ts +38 -0
- package/src/lezer/context-tracker.ts +45 -0
- package/src/lezer/create-sdl-parser.ts +88 -0
- package/src/lezer/create-syntax-error-from-text-and-cursor.ts +110 -0
- package/src/lezer/create-syntax-error-from-text-and-position.ts +36 -0
- package/src/lezer/parser.terms.ts +129 -0
- package/src/lezer/parser.ts +63 -0
- package/src/lezer/props/fold-node-prop-source.ts +27 -0
- package/src/lezer/props/primitive-node-prop-source.ts +19 -0
- package/src/lezer/props/style-tags-node-prop-source.ts +188 -0
- package/src/lezer/props/syntactic-token-node-prop-source.ts +298 -0
- package/src/lezer/sdl-string-input.ts +46 -0
- package/src/location.ts +13 -0
- package/src/parse-helper.ts +93 -0
- package/src/prettier/prettier-plugin-sdl.ts +89 -0
- package/src/prettier/print-abstract-array-dimension.ts +84 -0
- package/src/prettier/print-abstract-class-id.ts +53 -0
- package/src/prettier/print-abstract-expression.ts +168 -0
- package/src/prettier/print-abstract-node.ts +224 -0
- package/src/prettier/print-abstract-statement.ts +124 -0
- package/src/prettier/print-aggregate-output-value.ts +35 -0
- package/src/prettier/print-aligned-modifier.ts +50 -0
- package/src/prettier/print-array-definition.ts +95 -0
- package/src/prettier/print-array-element-access.ts +14 -0
- package/src/prettier/print-bit-modifier.ts +48 -0
- package/src/prettier/print-case-clause.ts +58 -0
- package/src/prettier/print-class-declaration.ts +99 -0
- package/src/prettier/print-class-definition.ts +44 -0
- package/src/prettier/print-class-member-access.ts +13 -0
- package/src/prettier/print-computed-array-definition.ts +33 -0
- package/src/prettier/print-computed-elementary-type-definition.ts +60 -0
- package/src/prettier/print-default-clause.ts +39 -0
- package/src/prettier/print-do-statement.ts +31 -0
- package/src/prettier/print-elementary-type-definition.ts +108 -0
- package/src/prettier/print-elementary-type-output-value.ts +13 -0
- package/src/prettier/print-elementary-type.ts +28 -0
- package/src/prettier/print-expandable-modifier.ts +41 -0
- package/src/prettier/print-extends-modifier.ts +29 -0
- package/src/prettier/print-for-statement.ts +64 -0
- package/src/prettier/print-identifier.ts +10 -0
- package/src/prettier/print-if-statement.ts +76 -0
- package/src/prettier/print-length-attribute.ts +14 -0
- package/src/prettier/print-map-declaration.ts +75 -0
- package/src/prettier/print-map-definition.ts +67 -0
- package/src/prettier/print-map-entry.ts +22 -0
- package/src/prettier/print-number-literal.ts +84 -0
- package/src/prettier/print-parameter-list.ts +20 -0
- package/src/prettier/print-parameter-value-list.ts +20 -0
- package/src/prettier/print-parameter.ts +34 -0
- package/src/prettier/print-specification.ts +51 -0
- package/src/prettier/print-string-definition.ts +114 -0
- package/src/prettier/print-string-literal.ts +57 -0
- package/src/prettier/print-switch-statement.ts +46 -0
- package/src/prettier/print-token.ts +7 -0
- package/src/prettier/print-unexpected-error.ts +10 -0
- package/src/prettier/print-while-statement.ts +26 -0
- package/src/prettier/util/print-utils.ts +607 -0
- package/src/prettier/util/types.ts +40 -0
- package/src/scanner-error.ts +65 -0
- package/src/util/location-utils.ts +16 -0
- package/src/util/logger.ts +165 -0
- package/tests/analyser/create-sdl-analyser.test.ts +54 -0
- package/tests/analyser/get-sdl-analyser-test-scenarios.ts +80 -0
- package/tests/analyser/sdl-analyser-scenarios.test.ts +67 -0
- package/tests/analyser/semantic-error-scenarios.test.ts +40 -0
- package/tests/analyser/symbol-table.test.ts +188 -0
- package/tests/analyser/test-cases/array_definition.txt +50 -0
- package/tests/analyser/test-cases/class_declaration.txt +112 -0
- package/tests/analyser/test-cases/class_definition.txt +60 -0
- package/tests/analyser/test-cases/compound_statement.txt +63 -0
- package/tests/analyser/test-cases/computed_array_definition.txt +12 -0
- package/tests/analyser/test-cases/computed_elementary_type_definition.txt +39 -0
- package/tests/analyser/test-cases/do_statement.txt +16 -0
- package/tests/analyser/test-cases/elementary_type_definition.txt +58 -0
- package/tests/analyser/test-cases/expression.txt +159 -0
- package/tests/analyser/test-cases/for_statement.txt +55 -0
- package/tests/analyser/test-cases/if_statement.txt +57 -0
- package/tests/analyser/test-cases/map_declaration.txt +89 -0
- package/tests/analyser/test-cases/map_definition.txt +35 -0
- package/tests/analyser/test-cases/number_literal.txt +31 -0
- package/tests/analyser/test-cases/parameter_list.txt +16 -0
- package/tests/analyser/test-cases/scopes.txt +239 -0
- package/tests/analyser/test-cases/specification.txt +28 -0
- package/tests/analyser/test-cases/string_definition.txt +40 -0
- package/tests/analyser/test-cases/switch_statement.txt +96 -0
- package/tests/analyser/test-cases/while_statement.txt +16 -0
- package/tests/analyser/util/symbol-table-utils.test.ts +65 -0
- package/tests/ast/__snapshots__/build-ast.test.ts.snap +38897 -0
- package/tests/ast/build-ast.test.ts +463 -0
- package/tests/ast/visitor/traversing-visitor.test.ts +36 -0
- package/tests/completion/get-potential-syntactic-tokens.test.ts +89 -0
- package/tests/completion/get-potential-token-type-ids.test.ts +76 -0
- package/tests/completion/test-potential-syntactic-tokens-scenario.ts +24 -0
- package/tests/completion/test-potential-token-type-ids-scenario.ts +33 -0
- package/tests/fixtures/history-recording-node-handler.ts +232 -0
- package/tests/lezer/create-parse-error-from-text-and-position.test.ts +129 -0
- package/tests/lezer/create-sdl-parser.test.ts +15 -0
- package/tests/lezer/sdl-parser-scenarios.test.ts +24 -0
- package/tests/lezer/sdl-string-input.test.ts +12 -0
- package/tests/lezer/syntax-error-scenarios.test.ts +114 -0
- package/tests/lezer/test-cases/array_definition.txt +109 -0
- package/tests/lezer/test-cases/class_declaration.txt +141 -0
- package/tests/lezer/test-cases/class_definition.txt +89 -0
- package/tests/lezer/test-cases/comment.txt +335 -0
- package/tests/lezer/test-cases/compound_statement.txt +109 -0
- package/tests/lezer/test-cases/computed_array_definition.txt +33 -0
- package/tests/lezer/test-cases/computed_elementary_type_definition.txt +59 -0
- package/tests/lezer/test-cases/do_statement.txt +48 -0
- package/tests/lezer/test-cases/elementary_type_definition.txt +158 -0
- package/tests/lezer/test-cases/expression.txt +1203 -0
- package/tests/lezer/test-cases/for_statement.txt +183 -0
- package/tests/lezer/test-cases/if_statement.txt +183 -0
- package/tests/lezer/test-cases/invalid.txt +97 -0
- package/tests/lezer/test-cases/map_declaration.txt +152 -0
- package/tests/lezer/test-cases/map_definition.txt +59 -0
- package/tests/lezer/test-cases/number_literal.txt +273 -0
- package/tests/lezer/test-cases/specification.txt +38 -0
- package/tests/lezer/test-cases/string_definition.txt +263 -0
- package/tests/lezer/test-cases/string_literal.txt +303 -0
- package/tests/lezer/test-cases/switch_statement.txt +407 -0
- package/tests/lezer/test-cases/while_statement.txt +45 -0
- package/tests/parse-helper.test.ts +361 -0
- package/tests/prettier/__snapshots__/prettier-plugin-sdl.test.ts.snap +127 -0
- package/tests/prettier/prettier-plugin-sdl.test.ts +58 -0
- package/tests/prettier/print-array.test.ts +29 -0
- package/tests/prettier/print-class.test.ts +15 -0
- package/tests/prettier/print-do.test.ts +25 -0
- package/tests/prettier/print-elementary-type.test.ts +62 -0
- package/tests/prettier/print-error.test.ts +40 -0
- package/tests/prettier/print-expression.test.ts +17 -0
- package/tests/prettier/print-for.test.ts +23 -0
- package/tests/prettier/print-if.test.ts +71 -0
- package/tests/prettier/print-number-literal.test.ts +28 -0
- package/tests/prettier/print-specification.test.ts +54 -0
- package/tests/prettier/print-string.test.ts +36 -0
- package/tests/prettier/print-switch.test.ts +69 -0
- package/tests/prettier/print-while.test.ts +23 -0
- package/tests/prettier/test-prettier-scenario.ts +41 -0
- package/tests/prettier/util/print-utils.test.ts +665 -0
- package/tests/prettier/util/types.test.ts +38 -0
- package/tests/sample-specifications/invalid.sdl +24 -0
- package/tests/sample-specifications/prettified-invalid.sdl +28 -0
- package/tests/sample-specifications/prettified-various-elements-narrow.sdl +97 -0
- package/tests/sample-specifications/prettified-various-elements.sdl +90 -0
- package/tests/sample-specifications/sample.sdl +24 -0
- package/tests/sample-specifications/various-elements.sdl +88 -0
- package/tests/util/logger.test.ts +36 -0
- package/tsconfig.json +29 -0
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { InternalScannerError } from "../scanner-error.ts";
|
|
2
|
+
import type { Location } from "../location.ts";
|
|
3
|
+
import getLogger from "../util/logger.ts";
|
|
4
|
+
import type { AbstractCompositeNode } from "../ast/node/abstract-composite-node.ts";
|
|
5
|
+
|
|
6
|
+
const logger = getLogger("SymbolTable");
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Enum representing different numeric variable types.
|
|
10
|
+
*/
|
|
11
|
+
export enum NumericType {
|
|
12
|
+
INTEGER,
|
|
13
|
+
DECIMAL,
|
|
14
|
+
FLOATING_POINT,
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Enum representing different string variable types.
|
|
19
|
+
*/
|
|
20
|
+
export enum StringType {
|
|
21
|
+
BASIC,
|
|
22
|
+
UCS,
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Enum representing different kinds of semantic symbols.
|
|
27
|
+
*/
|
|
28
|
+
export enum SymbolKind {
|
|
29
|
+
CLASS,
|
|
30
|
+
MAP,
|
|
31
|
+
VARIABLE,
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export enum AddSymbolResult {
|
|
35
|
+
SUCCESS,
|
|
36
|
+
DUPLICATE,
|
|
37
|
+
MEMBER_CONFLICT,
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Enum representing different kinds of scopes.
|
|
42
|
+
*/
|
|
43
|
+
export enum ScopeKind {
|
|
44
|
+
GLOBAL,
|
|
45
|
+
BLOCK,
|
|
46
|
+
CLASS,
|
|
47
|
+
MAP,
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export interface SymbolAttributes {
|
|
51
|
+
isComputed?: boolean;
|
|
52
|
+
isConst?: boolean;
|
|
53
|
+
isString?: boolean;
|
|
54
|
+
isArray?: boolean;
|
|
55
|
+
|
|
56
|
+
// Used for numeric variables
|
|
57
|
+
numericType?: NumericType;
|
|
58
|
+
|
|
59
|
+
// Used for string variables
|
|
60
|
+
stringType?: StringType;
|
|
61
|
+
|
|
62
|
+
// Used for array of class types, Map class output type or Class definition
|
|
63
|
+
classType?: string;
|
|
64
|
+
|
|
65
|
+
// Used for Map definition
|
|
66
|
+
mapType?: string;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface Symbol {
|
|
70
|
+
name: string;
|
|
71
|
+
kind: SymbolKind;
|
|
72
|
+
attributes: SymbolAttributes;
|
|
73
|
+
location: Location;
|
|
74
|
+
node: AbstractCompositeNode;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Used to differentiate between class members defined in different branches of conditional statements within the same class scope.
|
|
79
|
+
* SDL allows such members to have the same name, but they are considered distinct members that may
|
|
80
|
+
* or may not be present at runtime depending on the conditions.
|
|
81
|
+
* The branchId is a string that represents the path of scopes from the class scope to the nested current scope.
|
|
82
|
+
*/
|
|
83
|
+
export interface ClassMemberSymbol {
|
|
84
|
+
symbol: Symbol;
|
|
85
|
+
branchId?: string;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface Scope {
|
|
89
|
+
name: string;
|
|
90
|
+
parent?: Scope;
|
|
91
|
+
children: Scope[];
|
|
92
|
+
symbols: Map<string, Symbol>;
|
|
93
|
+
classMemberSymbols?: Map<string, ClassMemberSymbol[]>;
|
|
94
|
+
kind: ScopeKind;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export enum Mode {
|
|
98
|
+
READ,
|
|
99
|
+
WRITE,
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export class SymbolTable {
|
|
103
|
+
private globalScope: Scope;
|
|
104
|
+
private currentScope: Scope;
|
|
105
|
+
private scopeNameCounters: Map<Scope, Map<string, number>> = new Map();
|
|
106
|
+
private mode: Mode = Mode.WRITE;
|
|
107
|
+
|
|
108
|
+
constructor() {
|
|
109
|
+
this.globalScope = {
|
|
110
|
+
symbols: new Map(),
|
|
111
|
+
children: [],
|
|
112
|
+
name: "GLOBAL",
|
|
113
|
+
kind: ScopeKind.GLOBAL,
|
|
114
|
+
};
|
|
115
|
+
this.currentScope = this.globalScope;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private nextScopeName(baseName: string): string {
|
|
119
|
+
if (!this.scopeNameCounters.has(this.currentScope)) {
|
|
120
|
+
this.scopeNameCounters.set(this.currentScope, new Map());
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const counters = this.scopeNameCounters.get(this.currentScope)!;
|
|
124
|
+
const count = counters.get(baseName) ?? 0;
|
|
125
|
+
|
|
126
|
+
counters.set(baseName, count + 1);
|
|
127
|
+
|
|
128
|
+
return (count === 0) ? baseName : `${baseName}#${count}`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private getCurrentBranchId(): string | undefined {
|
|
132
|
+
const classScope = this.getEnclosingClassScope();
|
|
133
|
+
|
|
134
|
+
if (!classScope || (this.currentScope === classScope)) {
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const parts: string[] = [];
|
|
139
|
+
let scope: Scope | undefined = this.currentScope;
|
|
140
|
+
|
|
141
|
+
while (scope && (scope !== classScope)) {
|
|
142
|
+
if (scope.name) {
|
|
143
|
+
parts.unshift(scope.name);
|
|
144
|
+
}
|
|
145
|
+
scope = scope.parent;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return (parts.length > 0) ? parts.join("/") : undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private defineClassMember(symbol: Symbol): AddSymbolResult {
|
|
152
|
+
const classScope = this.getEnclosingClassScope();
|
|
153
|
+
|
|
154
|
+
if (!classScope) {
|
|
155
|
+
throw new InternalScannerError(
|
|
156
|
+
`Attempt to define class member '${symbol.name}' outside of class scope`,
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (!classScope.classMemberSymbols) {
|
|
161
|
+
classScope.classMemberSymbols = new Map();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
const members = classScope.classMemberSymbols;
|
|
165
|
+
|
|
166
|
+
if (!members.has(symbol.name)) {
|
|
167
|
+
members.set(symbol.name, []);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// check for existing member with same name but different elementary type or string variable kind defined in different branch of conditional statement
|
|
171
|
+
const existingMembers = members.get(symbol.name)!;
|
|
172
|
+
|
|
173
|
+
for (const memberEntry of existingMembers) {
|
|
174
|
+
const member = memberEntry.symbol;
|
|
175
|
+
|
|
176
|
+
if (member.attributes.numericType !== symbol.attributes.numericType) {
|
|
177
|
+
logger.debug(
|
|
178
|
+
`Defined class member: ${symbol.name} in class scope: ${
|
|
179
|
+
classScope.name ?? "anonymous"
|
|
180
|
+
} conflicts with existing member type: ${symbol.attributes.numericType} != ${symbol.attributes.numericType}`,
|
|
181
|
+
);
|
|
182
|
+
return AddSymbolResult.MEMBER_CONFLICT;
|
|
183
|
+
} else if (
|
|
184
|
+
member.attributes.stringType !== symbol.attributes.stringType
|
|
185
|
+
) {
|
|
186
|
+
logger.debug(
|
|
187
|
+
`Defined class member: ${symbol.name} in class scope: ${
|
|
188
|
+
classScope.name ?? "anonymous"
|
|
189
|
+
} conflicts with existing member type: ${symbol.attributes.stringType} != ${symbol.attributes.stringType}`,
|
|
190
|
+
);
|
|
191
|
+
return AddSymbolResult.MEMBER_CONFLICT;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const branchId = this.getCurrentBranchId();
|
|
196
|
+
|
|
197
|
+
existingMembers.push({ symbol, branchId });
|
|
198
|
+
|
|
199
|
+
logger.debug(
|
|
200
|
+
`Defined class member: ${symbol.name} in class scope: ${
|
|
201
|
+
classScope.name ?? "anonymous"
|
|
202
|
+
}${branchId ? ` (branch: ${branchId})` : ""}`,
|
|
203
|
+
);
|
|
204
|
+
|
|
205
|
+
return AddSymbolResult.SUCCESS;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
private enterScope(baseName: string, scopeKind: ScopeKind): void {
|
|
209
|
+
const name = this.nextScopeName(baseName);
|
|
210
|
+
|
|
211
|
+
let child = this.currentScope.children.find((c) => c.name === name);
|
|
212
|
+
|
|
213
|
+
if (child && (this.mode === Mode.WRITE)) {
|
|
214
|
+
throw new InternalScannerError(
|
|
215
|
+
`Scope with name '${name}' already exists in current scope '${this.currentScope.name}'`,
|
|
216
|
+
);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
if (!child && (this.mode === Mode.READ)) {
|
|
220
|
+
throw new InternalScannerError(
|
|
221
|
+
`Scope with name '${name}' does not exist in current scope '${this.currentScope.name}'`,
|
|
222
|
+
);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
// Add the scope if if doesn't already exist
|
|
226
|
+
if (!child) {
|
|
227
|
+
child = {
|
|
228
|
+
symbols: new Map(),
|
|
229
|
+
parent: this.currentScope,
|
|
230
|
+
children: [],
|
|
231
|
+
name,
|
|
232
|
+
kind: scopeKind,
|
|
233
|
+
};
|
|
234
|
+
this.currentScope.children.push(child);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
this.currentScope = child;
|
|
238
|
+
|
|
239
|
+
logger.debug(`Entered ${ScopeKind[scopeKind]} scope: ${name}`);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
getEnclosingClassScope(): Scope | undefined {
|
|
243
|
+
let scope: Scope | undefined = this.currentScope;
|
|
244
|
+
|
|
245
|
+
while (scope) {
|
|
246
|
+
if (scope.kind === ScopeKind.CLASS) {
|
|
247
|
+
return scope;
|
|
248
|
+
}
|
|
249
|
+
scope = scope.parent;
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return undefined;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
enterBlockScope(baseName: string): void {
|
|
256
|
+
this.enterScope(baseName, ScopeKind.BLOCK);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
enterClassScope(baseName: string): void {
|
|
260
|
+
this.enterScope(baseName, ScopeKind.CLASS);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
enterMapScope(baseName: string): void {
|
|
264
|
+
this.enterScope(baseName, ScopeKind.MAP);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
exitScope(): void {
|
|
268
|
+
if (this.currentScope.parent) {
|
|
269
|
+
logger.debug(
|
|
270
|
+
`Exiting ${
|
|
271
|
+
ScopeKind[this.currentScope.kind]
|
|
272
|
+
} scope: ${this.currentScope.name}`,
|
|
273
|
+
);
|
|
274
|
+
|
|
275
|
+
this.currentScope = this.currentScope.parent;
|
|
276
|
+
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
throw new InternalScannerError("Cannot exit global scope");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
getCurrentScope(): Scope {
|
|
284
|
+
return this.currentScope;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
getGlobalScope(): Scope {
|
|
288
|
+
return this.globalScope;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
resetScope(): void {
|
|
292
|
+
this.currentScope = this.globalScope;
|
|
293
|
+
this.scopeNameCounters.clear();
|
|
294
|
+
this.mode = Mode.READ;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
addSymbol(symbol: Symbol): AddSymbolResult {
|
|
298
|
+
if (this.mode === Mode.READ) {
|
|
299
|
+
throw new InternalScannerError(
|
|
300
|
+
`Attempt to add symbol '${symbol.name}' in read-only mode`,
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
if (this.currentScope.symbols.has(symbol.name)) {
|
|
305
|
+
return AddSymbolResult.DUPLICATE;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (this.currentScope !== this.globalScope) {
|
|
309
|
+
if (symbol.kind === SymbolKind.CLASS) {
|
|
310
|
+
throw new InternalScannerError(
|
|
311
|
+
`Class symbol '${symbol.name}' must be defined in global scope`,
|
|
312
|
+
);
|
|
313
|
+
}
|
|
314
|
+
if (symbol.kind === SymbolKind.MAP) {
|
|
315
|
+
throw new InternalScannerError(
|
|
316
|
+
`Map symbol '${symbol.name}' must be defined in global scope`,
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
this.currentScope.symbols.set(symbol.name, symbol);
|
|
322
|
+
|
|
323
|
+
logger.debug(
|
|
324
|
+
`Defined symbol: ${symbol.name} in scope: ${
|
|
325
|
+
this.currentScope.name ?? "anonymous"
|
|
326
|
+
}`,
|
|
327
|
+
);
|
|
328
|
+
|
|
329
|
+
// if in a class scope and if variable is parsed (regardless of nested level) or if
|
|
330
|
+
// variable is computed (and in in top level of class scope) then define variable as a class member
|
|
331
|
+
if (
|
|
332
|
+
this.getEnclosingClassScope() &&
|
|
333
|
+
((symbol.attributes.isComputed !== true) ||
|
|
334
|
+
(this.currentScope.kind === ScopeKind.CLASS))
|
|
335
|
+
) {
|
|
336
|
+
if (this.defineClassMember(symbol) !== AddSymbolResult.SUCCESS) {
|
|
337
|
+
return AddSymbolResult.MEMBER_CONFLICT;
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return AddSymbolResult.SUCCESS;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
lookupClassMember(name: string): ClassMemberSymbol[] | undefined {
|
|
345
|
+
const classScope = this.getEnclosingClassScope();
|
|
346
|
+
|
|
347
|
+
if (!classScope?.classMemberSymbols) {
|
|
348
|
+
return undefined;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
return classScope.classMemberSymbols.get(name);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
lookupVariable(name: string): Symbol | undefined {
|
|
355
|
+
let scope: Scope | undefined = this.currentScope;
|
|
356
|
+
|
|
357
|
+
while (scope) {
|
|
358
|
+
const symbol = scope.symbols.get(name);
|
|
359
|
+
|
|
360
|
+
if (symbol && (symbol.kind === SymbolKind.VARIABLE)) {
|
|
361
|
+
return symbol;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
scope = scope.parent;
|
|
365
|
+
}
|
|
366
|
+
return undefined;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
lookupClass(name: string): Symbol | undefined {
|
|
370
|
+
const symbol = this.globalScope.symbols.get(name);
|
|
371
|
+
|
|
372
|
+
if (symbol && (symbol.kind === SymbolKind.CLASS)) {
|
|
373
|
+
return symbol;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return undefined;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
lookupMap(name: string): Symbol | undefined {
|
|
380
|
+
const symbol = this.globalScope.symbols.get(name);
|
|
381
|
+
|
|
382
|
+
if (symbol && (symbol.kind === SymbolKind.MAP)) {
|
|
383
|
+
return symbol;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
388
|
+
}
|