Skip to content

Commit

Permalink
✨ [+feature] Added parsing support for tuples (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrownell authored May 14, 2024
2 parents 14b670b + 9713dc7 commit 79e0653
Show file tree
Hide file tree
Showing 4 changed files with 408 additions and 15 deletions.
20 changes: 20 additions & 0 deletions src/SimpleSchemaGenerator/SampleSchemas/Tuples.SimpleSchema
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Tuples ->
single: (String, )
multiple: (String, Integer, Number)

container: (String, Integer, Number, )*

with_metadata: (
String { min_length: 5 },
Integer { min: 10 },
Number { min: 20.0 },
)

nested: (
String,
(String*, Integer)+,
Number,
(String, (Number { max: 1.23 }, ), ),
)

tuples: Tuples?
30 changes: 15 additions & 15 deletions src/SimpleSchemaGenerator/Schema/Parse/ANTLR/Parse.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,18 +1130,18 @@ def visitParse_identifier_type_global(
# TODO: ),
# TODO: )

# TODO: # ----------------------------------------------------------------------
# TODO: def visitParse_tuple_type(self, ctx: SimpleSchemaParser.Parse_tuple_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: ParseTupleType(
# TODO: region,
# TODO: cardinality,
# TODO: metadata,
# TODO: cast(list[ParseType], children),
# TODO: ),
# TODO: )
# ----------------------------------------------------------------------
def visitParse_tuple_type(self, ctx: SimpleSchemaParser.Parse_tuple_typeContext):
children = self._GetChildren(ctx)

assert children
assert all(isinstance(child, ParseType) for child in children), children

self._stack.append(
lambda region, cardinality, metadata: ParseTupleType(
region,
cardinality,
metadata,
cast(list[ParseType], children),
),
)
8 changes: 8 additions & 0 deletions tests/Schema/Parse/ANTLR/Parse_UnitTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ def test_Extensions(self):
def test_Structures(self):
self.Execute(PathEx.EnsureFile(sample_schemas / "Structures.SimpleSchema"))

# ----------------------------------------------------------------------
def test_Tuples(self):
self.Execute(PathEx.EnsureFile(sample_schemas / "Tuples.SimpleSchema"))


# ----------------------------------------------------------------------
class TestParsing:
Expand Down Expand Up @@ -146,6 +150,10 @@ def test_Extensions(self, snapshot):
def test_Structures(self, snapshot):
self.Execute(PathEx.EnsureFile(sample_schemas / "Structures.SimpleSchema"), snapshot)

# ----------------------------------------------------------------------
def test_Tuples(self, snapshot):
self.Execute(PathEx.EnsureFile(sample_schemas / "Tuples.SimpleSchema"), snapshot)


# ----------------------------------------------------------------------
# |
Expand Down
Loading

0 comments on commit 79e0653

Please sign in to comment.