Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ [+feature] Added parsing support for variants #14

Merged
merged 1 commit into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
# - <not jar>

# ----------------------------------------------------------------------
# |
Expand Down
20 changes: 20 additions & 0 deletions src/SimpleSchemaGenerator/SampleSchemas/Variants.SimpleSchema
Original file line number Diff line number Diff line change
@@ -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?
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 @@ -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):
Expand Down
9 changes: 9 additions & 0 deletions tests/Schema/Parse/ANTLR/Parse_UnitTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ def Execute(
"[",
"]",
"=",
"|",
"pass",
]:
scrubbed_content = scrubbed_content.replace(replace_token, "".ljust(len(replace_token)))
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)


# ----------------------------------------------------------------------
# |
Expand Down
267 changes: 267 additions & 0 deletions tests/Schema/Parse/ANTLR/__snapshots__/Parse_UnitTest.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -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

'''
# ---
Loading