Skip to content

Commit d60eb9d

Browse files
committed
Manage union type
1 parent 46da1d4 commit d60eb9d

5 files changed

Lines changed: 205 additions & 0 deletions

File tree

src/FAST-Python-Model-Generator/FASTPythonMetamodelGenerator.class.st

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -568,6 +568,8 @@ FASTPythonMetamodelGenerator >> defineHierarchy [
568568
unaryOperator --|> tUnaryOperator.
569569

570570
unionPattern --|> tPattern.
571+
572+
unionType --|> tTypeContent.
571573

572574
variable --|> expression.
573575
variable --|> tVariableEntity.
@@ -778,6 +780,8 @@ FASTPythonMetamodelGenerator >> defineRelations [
778780
(tWithTypeParameters property: #typeParameters) <>-* (type property: #parentWithTypeParameters).
779781

780782
(unionPattern property: #members) <>-* (tPattern property: #parentUnionPattern).
783+
784+
(unionType property: #types) <>-* (type property: #parentUnionType).
781785

782786
(withStatement property: #items) <>-* (tWithStatementItem property: #parentWithStatementItem)
783787
]

src/FAST-Python-Model/FASTPyType.class.st

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
| `parentFunctionDefinition` | `FASTPyType` | `returnType` | `FASTPyFunctionDefinition` | |
1212
| `parentGenericTypeArgument` | `FASTPyType` | `arguments` | `FASTPyGenericType` | |
1313
| `parentParameter` | `FASTPyType` | `type` | `FASTPyParameter` | |
14+
| `parentUnionType` | `FASTPyType` | `types` | `FASTPyUnionType` | |
1415
| `parentWithTypeParameters` | `FASTPyType` | `typeParameters` | `FASTPyTWithTypeParameters` | |
1516
| `parentyTypeAliasStatementAlias` | `FASTPyType` | `left` | `FASTPyTypeAliasStatement` | |
1617
| `parentyTypeAliasStatementType` | `FASTPyType` | `right` | `FASTPyTypeAliasStatement` | |
@@ -34,6 +35,7 @@ Class {
3435
'#parentFunctionDefinition => FMOne type: #FASTPyFunctionDefinition opposite: #returnType',
3536
'#parentGenericTypeArgument => FMOne type: #FASTPyGenericType opposite: #arguments',
3637
'#parentParameter => FMOne type: #FASTPyParameter opposite: #type',
38+
'#parentUnionType => FMOne type: #FASTPyUnionType opposite: #types',
3739
'#parentWithTypeParameters => FMOne type: #FASTPyTWithTypeParameters opposite: #typeParameters',
3840
'#parentyTypeAliasStatementAlias => FMOne type: #FASTPyTypeAliasStatement opposite: #left',
3941
'#parentyTypeAliasStatementType => FMOne type: #FASTPyTypeAliasStatement opposite: #right'
@@ -217,6 +219,29 @@ FASTPyType >> parentParameterGroup [
217219
^ MooseSpecializedGroup with: self parentParameter
218220
]
219221

222+
{ #category : 'accessing' }
223+
FASTPyType >> parentUnionType [
224+
"Relation named: #parentUnionType type: #FASTPyUnionType opposite: #types"
225+
226+
<generated>
227+
<container>
228+
^ parentUnionType
229+
]
230+
231+
{ #category : 'accessing' }
232+
FASTPyType >> parentUnionType: anObject [
233+
234+
<generated>
235+
parentUnionType := anObject
236+
]
237+
238+
{ #category : 'navigation' }
239+
FASTPyType >> parentUnionTypeGroup [
240+
<generated>
241+
<navigation: 'ParentUnionType'>
242+
^ MooseSpecializedGroup with: self parentUnionType
243+
]
244+
220245
{ #category : 'accessing' }
221246
FASTPyType >> parentWithTypeParameters [
222247
"Relation named: #parentWithTypeParameters type: #FASTPyTWithTypeParameters opposite: #typeParameters"

src/FAST-Python-Model/FASTPyUnionType.class.st

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1+
"
2+
## Relations
3+
======================
4+
5+
### Parents
6+
| Relation | Origin | Opposite | Type | Comment |
7+
|---|
8+
| `parentType` | `FASTPyTTypeContent` | `content` | `FASTPyType` | |
9+
10+
### Children
11+
| Relation | Origin | Opposite | Type | Comment |
12+
|---|
13+
| `types` | `FASTPyUnionType` | `parentUnionType` | `FASTPyType` | |
14+
15+
16+
17+
"
118
Class {
219
#name : 'FASTPyUnionType',
320
#superclass : 'FASTPyEntity',
21+
#traits : 'FASTPyTTypeContent',
22+
#classTraits : 'FASTPyTTypeContent classTrait',
23+
#instVars : [
24+
'#types => FMMany type: #FASTPyType opposite: #parentUnionType'
25+
],
426
#category : 'FAST-Python-Model-Entities',
527
#package : 'FAST-Python-Model',
628
#tag : 'Entities'
@@ -14,3 +36,32 @@ FASTPyUnionType class >> annotation [
1436
<generated>
1537
^ self
1638
]
39+
40+
{ #category : 'adding' }
41+
FASTPyUnionType >> addType: anObject [
42+
<generated>
43+
^ self types add: anObject
44+
]
45+
46+
{ #category : 'accessing' }
47+
FASTPyUnionType >> types [
48+
"Relation named: #types type: #FASTPyType opposite: #parentUnionType"
49+
50+
<generated>
51+
<derived>
52+
^ types
53+
]
54+
55+
{ #category : 'accessing' }
56+
FASTPyUnionType >> types: anObject [
57+
58+
<generated>
59+
types value: anObject
60+
]
61+
62+
{ #category : 'navigation' }
63+
FASTPyUnionType >> typesGroup [
64+
<generated>
65+
<navigation: 'Types'>
66+
^ MooseSpecializedGroup withAll: self types asSet
67+
]

src/FAST-Python-Tools-Tests/FASTPythonImporterTest.class.st

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24437,6 +24437,120 @@ FASTPythonImporterTest >> testUnaryOperator [
2443724437
self assert: operator operator equals: '-'
2443824438
]
2443924439

24440+
{ #category : 'tests - types' }
24441+
FASTPythonImporterTest >> testUnionType [
24442+
<generated>
24443+
"Generated as regression test by FASTPyImporterTestGenerator>>#generateTestNamed:fromCode:protocol:
24444+
Regenerate executing: self regenerateTest: #testUnionType "
24445+
24446+
self parse: 'def f( arg: IO[bytes] | int): pass'.
24447+
24448+
self assert: (fast allWithType: FASTPyBlock) size equals: 1.
24449+
self assert: (fast allWithType: FASTPyFunctionDefinition) size equals: 1.
24450+
self assert: (fast allWithType: FASTPyGenericType) size equals: 1.
24451+
self assert: (fast allWithType: FASTPyIdentifier) size equals: 2.
24452+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
24453+
self assert: (fast allWithType: FASTPyParameter) size equals: 1.
24454+
self assert: (fast allWithType: FASTPyPassStatement) size equals: 1.
24455+
self assert: (fast allWithType: FASTPyType) size equals: 4.
24456+
self assert: (fast allWithType: FASTPyUnionType) size equals: 1.
24457+
24458+
stack push: fast rootEntities anyOne containedEntities first.
24459+
self assert: self topEntity sourceCode equals: 'def f( arg: IO[bytes] | int): pass'.
24460+
self assert: (self topEntity isOfType: FASTPyFunctionDefinition).
24461+
self assert: (self topEntity isOfType: FASTPyTDefinition).
24462+
self assert: (self topEntity isOfType: FASTTWithParameters).
24463+
self assert: (self topEntity isOfType: FASTTWithStatements).
24464+
self assert: self topEntity name equals: 'f'.
24465+
24466+
"Testing value of multivalued relation parameters"
24467+
self assert: self topEntity parameters size equals: 1.
24468+
24469+
stack push: (stack top parameters at: 1).
24470+
self assert: self topEntity sourceCode equals: 'arg: IO[bytes] | int'.
24471+
self assert: (self topEntity isOfType: FASTPyParameter).
24472+
self assert: (self topEntity isOfType: FASTTVariableEntity).
24473+
self assert: self topEntity name equals: 'arg'.
24474+
24475+
"Testing value of monovalue relation type"
24476+
self assert: self topEntity type isNotNil.
24477+
24478+
stack push: self topEntity type.
24479+
self assert: self topEntity sourceCode equals: 'IO[bytes] | int'.
24480+
self assert: (self topEntity isOfType: FASTPyType).
24481+
24482+
"Testing value of monovalue relation content"
24483+
self assert: self topEntity content isNotNil.
24484+
24485+
stack push: self topEntity content.
24486+
self assert: self topEntity sourceCode equals: 'IO[bytes] | int'.
24487+
self assert: (self topEntity isOfType: FASTPyUnionType).
24488+
24489+
"Testing value of multivalued relation types"
24490+
self assert: self topEntity types size equals: 2.
24491+
24492+
stack push: (stack top types at: 1).
24493+
self assert: self topEntity sourceCode equals: 'IO[bytes]'.
24494+
self assert: (self topEntity isOfType: FASTPyType).
24495+
24496+
"Testing value of monovalue relation content"
24497+
self assert: self topEntity content isNotNil.
24498+
24499+
stack push: self topEntity content.
24500+
self assert: self topEntity sourceCode equals: 'IO[bytes]'.
24501+
self assert: (self topEntity isOfType: FASTPyGenericType).
24502+
self assert: self topEntity name equals: 'IO'.
24503+
24504+
"Testing value of multivalued relation typeParameters"
24505+
self assert: self topEntity typeParameters size equals: 1.
24506+
24507+
stack push: (stack top typeParameters at: 1).
24508+
self assert: self topEntity sourceCode equals: 'bytes'.
24509+
self assert: (self topEntity isOfType: FASTPyType).
24510+
24511+
"Testing value of monovalue relation content"
24512+
self assert: self topEntity content isNotNil.
24513+
24514+
stack push: self topEntity content.
24515+
self assert: self topEntity sourceCode equals: 'bytes'.
24516+
self assert: (self topEntity isOfType: FASTPyIdentifier).
24517+
stack pop.
24518+
stack pop.
24519+
stack pop.
24520+
stack pop.
24521+
24522+
stack push: (stack top types at: 2).
24523+
self assert: self topEntity sourceCode equals: 'int'.
24524+
self assert: (self topEntity isOfType: FASTPyType).
24525+
24526+
"Testing value of monovalue relation content"
24527+
self assert: self topEntity content isNotNil.
24528+
24529+
stack push: self topEntity content.
24530+
self assert: self topEntity sourceCode equals: 'int'.
24531+
self assert: (self topEntity isOfType: FASTPyIdentifier).
24532+
stack pop.
24533+
stack pop.
24534+
stack pop.
24535+
stack pop.
24536+
stack pop.
24537+
24538+
"Testing value of monovalue relation statementBlock"
24539+
self assert: self topEntity statementBlock isNotNil.
24540+
24541+
stack push: self topEntity statementBlock.
24542+
self assert: self topEntity sourceCode equals: 'pass'.
24543+
self assert: (self topEntity isOfType: FASTPyBlock).
24544+
self assert: (self topEntity isOfType: FASTTStatementBlock).
24545+
24546+
"Testing value of multivalued relation statements"
24547+
self assert: self topEntity statements size equals: 1.
24548+
24549+
stack push: (stack top statements at: 1).
24550+
self assert: self topEntity sourceCode equals: 'pass'.
24551+
self assert: (self topEntity isOfType: FASTPyPassStatement)
24552+
]
24553+
2444024554
{ #category : 'tests - advanced statements' }
2444124555
FASTPythonImporterTest >> testWhile [
2444224556
<generated>

src/FAST-Python-Tools/TSFASTPythonImporter.class.st

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,17 @@ TSFASTPythonImporter >> visitUnionPattern: aTSNode [
10131013
^ self handleNode: aTSNode kind: FASTPyUnionPattern
10141014
]
10151015

1016+
{ #category : 'visiting' }
1017+
TSFASTPythonImporter >> visitUnionType: aTSNode [
1018+
1019+
"If we have union types in union types, we want to flatten them in only one node. But the implementation might need to change depending on this issue: https://github.com/tree-sitter/tree-sitter-python/issues/334"
1020+
(self topFastEntity isOfType: FASTPyUnionType) ifTrue: [ ^ self rawVisitChildren: aTSNode ].
1021+
1022+
self onNextContextDo: [ :entry | entry aliasUnnamedFieldAs: #types ].
1023+
1024+
^ self handleNode: aTSNode kind: FASTPyUnionType
1025+
]
1026+
10161027
{ #category : 'visiting' }
10171028
TSFASTPythonImporter >> visitWildcardImport: aTSNode [
10181029

0 commit comments

Comments
 (0)