From dbb4289b827a48571fa05d13ea3fab1365d2eb20 Mon Sep 17 00:00:00 2001 From: David Brownell Date: Tue, 14 May 2024 12:17:15 -0400 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20[+feature]=20Added=20parsing=20supp?= =?UTF-8?q?ort=20for=20variants?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pyproject.toml | 4 + .../SampleSchemas/Variants.SimpleSchema | 20 ++ .../Schema/Parse/ANTLR/Parse.py | 30 +- tests/Schema/Parse/ANTLR/Parse_UnitTest.py | 9 + .../ANTLR/__snapshots__/Parse_UnitTest.ambr | 267 ++++++++++++++++++ 5 files changed, 315 insertions(+), 15 deletions(-) create mode 100644 src/SimpleSchemaGenerator/SampleSchemas/Variants.SimpleSchema diff --git a/pyproject.toml b/pyproject.toml index e75ca5c..5d5a64c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -75,6 +75,10 @@ Repository = "https://github.com/davidbrownell/SimpleSchemaGenerator" [project.scripts] SimpleSchemaGenerator = "SimpleSchemaGenerator:EntryPoint.app" +# TODO: Ensure that other files included in package +# - *.SimpleSchema +# - *.g4 +# - # ---------------------------------------------------------------------- # | diff --git a/src/SimpleSchemaGenerator/SampleSchemas/Variants.SimpleSchema b/src/SimpleSchemaGenerator/SampleSchemas/Variants.SimpleSchema new file mode 100644 index 0000000..39fe28e --- /dev/null +++ b/src/SimpleSchemaGenerator/SampleSchemas/Variants.SimpleSchema @@ -0,0 +1,20 @@ +Variants -> + value: (String | Number) + + with_metadata: ( + String { min_length: 5 } + | Integer { min: 10 } + | Number { min: 20.0 } + ) + + nested_tuple: ( + String + | (Integer { min: 10 }, Number) + ) + + cardinality: ( + String[3] + | Integer[4] + )? + +variants: Variants? diff --git a/src/SimpleSchemaGenerator/Schema/Parse/ANTLR/Parse.py b/src/SimpleSchemaGenerator/Schema/Parse/ANTLR/Parse.py index b7ddbe1..321f144 100644 --- a/src/SimpleSchemaGenerator/Schema/Parse/ANTLR/Parse.py +++ b/src/SimpleSchemaGenerator/Schema/Parse/ANTLR/Parse.py @@ -1114,21 +1114,21 @@ def visitParse_identifier_type_global( # creating the type. self._stack.append(self.CreateRegion(ctx)) - # TODO: # ---------------------------------------------------------------------- - # TODO: def visitParse_variant_type(self, ctx: SimpleSchemaParser.Parse_variant_typeContext): - # TODO: children = self._GetChildren(ctx) - # TODO: - # TODO: assert children - # TODO: assert all(isinstance(child, ParseType) for child in children), children - # TODO: - # TODO: self._stack.append( - # TODO: lambda region, cardinality, metadata: ParseVariantType( - # TODO: region, - # TODO: cardinality, - # TODO: metadata, - # TODO: cast(list[ParseType], children), - # TODO: ), - # TODO: ) + # ---------------------------------------------------------------------- + def visitParse_variant_type(self, ctx: SimpleSchemaParser.Parse_variant_typeContext): + children = self._GetChildren(ctx) + + assert children + assert all(isinstance(child, ParseType) for child in children), children + + self._stack.append( + lambda region, cardinality, metadata: ParseVariantType( + region, + cardinality, + metadata, + cast(list[ParseType], children), + ), + ) # ---------------------------------------------------------------------- def visitParse_tuple_type(self, ctx: SimpleSchemaParser.Parse_tuple_typeContext): diff --git a/tests/Schema/Parse/ANTLR/Parse_UnitTest.py b/tests/Schema/Parse/ANTLR/Parse_UnitTest.py index b1ca7ce..8bb2989 100644 --- a/tests/Schema/Parse/ANTLR/Parse_UnitTest.py +++ b/tests/Schema/Parse/ANTLR/Parse_UnitTest.py @@ -84,6 +84,7 @@ def Execute( "[", "]", "=", + "|", "pass", ]: scrubbed_content = scrubbed_content.replace(replace_token, "".ljust(len(replace_token))) @@ -115,6 +116,10 @@ def test_Structures(self): def test_Tuples(self): self.Execute(PathEx.EnsureFile(sample_schemas / "Tuples.SimpleSchema")) + # ---------------------------------------------------------------------- + def test_Variants(self): + self.Execute(PathEx.EnsureFile(sample_schemas / "Variants.SimpleSchema")) + # ---------------------------------------------------------------------- class TestParsing: @@ -154,6 +159,10 @@ def test_Structures(self, snapshot): def test_Tuples(self, snapshot): self.Execute(PathEx.EnsureFile(sample_schemas / "Tuples.SimpleSchema"), snapshot) + # ---------------------------------------------------------------------- + def test_Variants(self, snapshot): + self.Execute(PathEx.EnsureFile(sample_schemas / "Variants.SimpleSchema"), snapshot) + # ---------------------------------------------------------------------- # | diff --git a/tests/Schema/Parse/ANTLR/__snapshots__/Parse_UnitTest.ambr b/tests/Schema/Parse/ANTLR/__snapshots__/Parse_UnitTest.ambr index 2c9e639..8c69980 100644 --- a/tests/Schema/Parse/ANTLR/__snapshots__/Parse_UnitTest.ambr +++ b/tests/Schema/Parse/ANTLR/__snapshots__/Parse_UnitTest.ambr @@ -1474,3 +1474,270 @@ ''' # --- +# name: TestParsing.test_Variants + ''' + - __type__: RootStatement + statements: + - __type__: ParseStructureStatement + name: + __type__: ParseIdentifier + value: Variants + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + children: + - __type__: ParseItemStatement + name: + __type__: ParseIdentifier + value: value + type: + __type__: ParseVariantType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + types: + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + identifiers: + - __type__: ParseIdentifier + value: String + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + identifiers: + - __type__: ParseIdentifier + value: Number + - __type__: ParseItemStatement + name: + __type__: ParseIdentifier + value: with_metadata + type: + __type__: ParseVariantType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + types: + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + unresolved_metadata: + __type__: Metadata + items: + - __type__: MetadataItem + name: + __type__: TerminalElement + value: min_length + expression: + __type__: IntegerExpression + value: 5 + identifiers: + - __type__: ParseIdentifier + value: String + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + unresolved_metadata: + __type__: Metadata + items: + - __type__: MetadataItem + name: + __type__: TerminalElement + value: min + expression: + __type__: IntegerExpression + value: 10 + identifiers: + - __type__: ParseIdentifier + value: Integer + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + unresolved_metadata: + __type__: Metadata + items: + - __type__: MetadataItem + name: + __type__: TerminalElement + value: min + expression: + __type__: NumberExpression + value: 20.0 + identifiers: + - __type__: ParseIdentifier + value: Number + - __type__: ParseItemStatement + name: + __type__: ParseIdentifier + value: nested_tuple + type: + __type__: ParseVariantType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + types: + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + identifiers: + - __type__: ParseIdentifier + value: String + - __type__: ParseTupleType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + types: + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + unresolved_metadata: + __type__: Metadata + items: + - __type__: MetadataItem + name: + __type__: TerminalElement + value: min + expression: + __type__: IntegerExpression + value: 10 + identifiers: + - __type__: ParseIdentifier + value: Integer + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 1 + max: + __type__: IntegerExpression + value: 1 + identifiers: + - __type__: ParseIdentifier + value: Number + - __type__: ParseItemStatement + name: + __type__: ParseIdentifier + value: cardinality + type: + __type__: ParseVariantType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 0 + max: + __type__: IntegerExpression + value: 1 + types: + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 3 + max: + __type__: IntegerExpression + value: 3 + identifiers: + - __type__: ParseIdentifier + value: String + - __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 4 + max: + __type__: IntegerExpression + value: 4 + identifiers: + - __type__: ParseIdentifier + value: Integer + - __type__: ParseItemStatement + name: + __type__: ParseIdentifier + value: variants + type: + __type__: ParseIdentifierType + cardinality: + __type__: Cardinality + min: + __type__: IntegerExpression + value: 0 + max: + __type__: IntegerExpression + value: 1 + identifiers: + - __type__: ParseIdentifier + value: Variants + + ''' +# ---