Skip to content

Commit 4c8bc82

Browse files
committed
Handle minimal example of try statement
Next step is to manage all cases of except
1 parent 60dd968 commit 4c8bc82

8 files changed

Lines changed: 189 additions & 5 deletions

File tree

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ FASTPythonMetamodelGenerator >> defineHierarchy [
394394
elifClause --|> tConditionalStatement.
395395

396396
ellipsis --|> literal.
397+
398+
exceptClause --|> tStatementBlock.
397399

398400
expression --|> statement. "In Python expressions can also be expression statements."
399401
expression --|> tExpression.
@@ -515,6 +517,9 @@ FASTPythonMetamodelGenerator >> defineHierarchy [
515517
tuple --|> tAsPatternTarget.
516518

517519
tuplePattern --|> expression. "TO REVIEW. This might be wrong."
520+
521+
tryStatement --|> statement.
522+
tryStatement --|> tStatementBlock.
518523

519524
unaryOperator --|> operator.
520525
unaryOperator --|> tUnaryOperator.
@@ -664,6 +669,8 @@ FASTPythonMetamodelGenerator >> defineRelations [
664669
(tComprehension property: #clauses) <>-* (expression property: #comprehensionClauseOwner).
665670

666671
(tDefinition property: #decorators) <>-* (decorator property: #definitionOwner).
672+
673+
(tryStatement property: #excepts) <>-* (exceptClause property: #parentTryStatement).
667674

668675
(tuplePattern property: #patterns) <>-* (expression property: #tuplePatternOwner).
669676

‎src/FAST-Python-Model/FASTPyExceptClause.class.st‎

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
1+
"
2+
## Relations
3+
======================
4+
5+
### Parents
6+
| Relation | Origin | Opposite | Type | Comment |
7+
|---|
8+
| `fastBehaviouralParent` | `FASTTStatementBlock` | `statementBlock` | `FASTTWithStatements` | Behavioural entity containing the statement block.|
9+
| `parentLoopStatement` | `FASTTStatement` | `body` | `FASTTLoopStatement` | Optional loop of which this statement is the body|
10+
| `parentTryStatement` | `FASTPyExceptClause` | `excepts` | `FASTPyTryStatement` | |
11+
| `statementContainer` | `FASTTStatement` | `statements` | `FASTTStatementBlock` | Block containing this statement.|
12+
13+
### Children
14+
| Relation | Origin | Opposite | Type | Comment |
15+
|---|
16+
| `statements` | `FASTTStatementBlock` | `statementContainer` | `FASTTStatement` | Statements enclosed in this block|
17+
18+
19+
## Properties
20+
======================
21+
22+
| Name | Type | Default value | Comment |
23+
|---|
24+
| `endPos` | `Number` | nil | |
25+
| `startPos` | `Number` | nil | |
26+
27+
"
128
Class {
229
#name : 'FASTPyExceptClause',
330
#superclass : 'FASTPyEntity',
31+
#traits : 'FASTTStatementBlock',
32+
#classTraits : 'FASTTStatementBlock classTrait',
33+
#instVars : [
34+
'#parentTryStatement => FMOne type: #FASTPyTryStatement opposite: #excepts'
35+
],
436
#category : 'FAST-Python-Model-Entities',
537
#package : 'FAST-Python-Model',
638
#tag : 'Entities'
@@ -14,3 +46,26 @@ FASTPyExceptClause class >> annotation [
1446
<generated>
1547
^ self
1648
]
49+
50+
{ #category : 'accessing' }
51+
FASTPyExceptClause >> parentTryStatement [
52+
"Relation named: #parentTryStatement type: #FASTPyTryStatement opposite: #excepts"
53+
54+
<generated>
55+
<container>
56+
^ parentTryStatement
57+
]
58+
59+
{ #category : 'accessing' }
60+
FASTPyExceptClause >> parentTryStatement: anObject [
61+
62+
<generated>
63+
parentTryStatement := anObject
64+
]
65+
66+
{ #category : 'navigation' }
67+
FASTPyExceptClause >> parentTryStatementGroup [
68+
<generated>
69+
<navigation: 'ParentTryStatement'>
70+
^ MooseSpecializedGroup with: self parentTryStatement
71+
]

‎src/FAST-Python-Model/FASTPyList.class.st‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Parents
66
| Relation | Origin | Opposite | Type | Comment |
77
|---|
8+
| `parentAsPatternSource` | `FASTPyTAsPatternSource` | `source` | `FASTPyAsPattern` | |
89
| `parentAsPatternTarget` | `FASTPyTAsPatternTarget` | `target` | `FASTPyAsPattern` | |
910
1011

‎src/FAST-Python-Model/FASTPyTAsPatternTarget.trait.st‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ I represent an entity that could be the alias/target of an as pattern.
77
### Parents
88
| Relation | Origin | Opposite | Type | Comment |
99
|---|
10+
| `parentAsPatternSource` | `FASTPyTAsPatternSource` | `source` | `FASTPyAsPattern` | |
1011
| `parentAsPatternTarget` | `FASTPyTAsPatternTarget` | `target` | `FASTPyAsPattern` | |
1112
1213
@@ -17,6 +18,8 @@ Trait {
1718
#instVars : [
1819
'#parentAsPatternTarget => FMOne type: #FASTPyAsPattern opposite: #target'
1920
],
21+
#traits : 'FASTPyTAsPatternSource',
22+
#classTraits : 'FASTPyTAsPatternSource classTrait',
2023
#category : 'FAST-Python-Model-Traits',
2124
#package : 'FAST-Python-Model',
2225
#tag : 'Traits'
Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
1+
"
2+
## Relations
3+
======================
4+
5+
### Parents
6+
| Relation | Origin | Opposite | Type | Comment |
7+
|---|
8+
| `fastBehaviouralParent` | `FASTTStatementBlock` | `statementBlock` | `FASTTWithStatements` | Behavioural entity containing the statement block.|
9+
| `parentLoopStatement` | `FASTTStatement` | `body` | `FASTTLoopStatement` | Optional loop of which this statement is the body|
10+
| `statementContainer` | `FASTTStatement` | `statements` | `FASTTStatementBlock` | Block containing this statement.|
11+
12+
### Children
13+
| Relation | Origin | Opposite | Type | Comment |
14+
|---|
15+
| `excepts` | `FASTPyTryStatement` | `parentTryStatement` | `FASTPyExceptClause` | |
16+
| `statements` | `FASTTStatementBlock` | `statementContainer` | `FASTTStatement` | Statements enclosed in this block|
17+
18+
19+
## Properties
20+
======================
21+
22+
| Name | Type | Default value | Comment |
23+
|---|
24+
| `endPos` | `Number` | nil | |
25+
| `startPos` | `Number` | nil | |
26+
27+
"
128
Class {
229
#name : 'FASTPyTryStatement',
3-
#superclass : 'FASTPyEntity',
30+
#superclass : 'FASTPyStatement',
31+
#traits : 'FASTTStatementBlock',
32+
#classTraits : 'FASTTStatementBlock classTrait',
33+
#instVars : [
34+
'#excepts => FMMany type: #FASTPyExceptClause opposite: #parentTryStatement'
35+
],
436
#category : 'FAST-Python-Model-Entities',
537
#package : 'FAST-Python-Model',
638
#tag : 'Entities'
@@ -9,8 +41,37 @@ Class {
941
{ #category : 'meta' }
1042
FASTPyTryStatement class >> annotation [
1143

12-
<FMClass: #TryStatement super: #FASTPyEntity>
44+
<FMClass: #TryStatement super: #FASTPyStatement>
1345
<package: #'FAST-Python-Model'>
1446
<generated>
1547
^ self
1648
]
49+
50+
{ #category : 'adding' }
51+
FASTPyTryStatement >> addExcept: anObject [
52+
<generated>
53+
^ self excepts add: anObject
54+
]
55+
56+
{ #category : 'accessing' }
57+
FASTPyTryStatement >> excepts [
58+
"Relation named: #excepts type: #FASTPyExceptClause opposite: #parentTryStatement"
59+
60+
<generated>
61+
<derived>
62+
^ excepts
63+
]
64+
65+
{ #category : 'accessing' }
66+
FASTPyTryStatement >> excepts: anObject [
67+
68+
<generated>
69+
excepts value: anObject
70+
]
71+
72+
{ #category : 'navigation' }
73+
FASTPyTryStatement >> exceptsGroup [
74+
<generated>
75+
<navigation: 'Excepts'>
76+
^ MooseSpecializedGroup withAll: self excepts asSet
77+
]

‎src/FAST-Python-Model/FASTPyTuple.class.st‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
### Parents
66
| Relation | Origin | Opposite | Type | Comment |
77
|---|
8+
| `parentAsPatternSource` | `FASTPyTAsPatternSource` | `source` | `FASTPyAsPattern` | |
89
| `parentAsPatternTarget` | `FASTPyTAsPatternTarget` | `target` | `FASTPyAsPattern` | |
910
1011

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10485,6 +10485,56 @@ FASTPythonImporterTest >> testTrue [
1048510485
self assert: (boolean isOfType: FASTTBooleanLiteral)
1048610486
]
1048710487

10488+
{ #category : 'tests - try statement' }
10489+
FASTPythonImporterTest >> testTryStatement [
10490+
<generated>
10491+
"Generated as regression test by FASTPyImporterTestGenerator>>#generateTestNamed:fromCode:protocol:
10492+
Regenerate executing: self regenerateTest: #testTryStatement "
10493+
10494+
self parse: 'try:
10495+
pass
10496+
except:
10497+
pass'.
10498+
10499+
self assert: (fast allWithType: FASTPyExceptClause) size equals: 1.
10500+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
10501+
self assert: (fast allWithType: FASTPyPassStatement) size equals: 2.
10502+
self assert: (fast allWithType: FASTPyTryStatement) size equals: 1.
10503+
10504+
stack push: fast rootEntities anyOne containedEntities first.
10505+
self assert: self topEntity sourceCode equals: 'try:
10506+
pass
10507+
except:
10508+
pass' withPlatformLineEndings.
10509+
self assert: (self topEntity isOfType: FASTPyTryStatement).
10510+
self assert: (self topEntity isOfType: FASTTStatementBlock).
10511+
10512+
"Testing value of multivalued relation excepts"
10513+
self assert: self topEntity excepts size equals: 1.
10514+
10515+
stack push: (stack top excepts at: 1).
10516+
self assert: self topEntity sourceCode equals: 'except:
10517+
pass' withPlatformLineEndings.
10518+
self assert: (self topEntity isOfType: FASTPyExceptClause).
10519+
self assert: (self topEntity isOfType: FASTTStatementBlock).
10520+
10521+
"Testing value of multivalued relation statements"
10522+
self assert: self topEntity statements size equals: 1.
10523+
10524+
stack push: (stack top statements at: 1).
10525+
self assert: self topEntity sourceCode equals: 'pass'.
10526+
self assert: (self topEntity isOfType: FASTPyPassStatement).
10527+
stack pop.
10528+
stack pop.
10529+
10530+
"Testing value of multivalued relation statements"
10531+
self assert: self topEntity statements size equals: 1.
10532+
10533+
stack push: (stack top statements at: 1).
10534+
self assert: self topEntity sourceCode equals: 'pass'.
10535+
self assert: (self topEntity isOfType: FASTPyPassStatement)
10536+
]
10537+
1048810538
{ #category : 'tests - collections' }
1048910539
FASTPythonImporterTest >> testTuple [
1049010540
<generated>

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,9 +119,9 @@ TSFASTPythonImporter >> visitBlock: aTSNode [
119119

120120
((context top isOfFASTType: FASTPyIfStatement) and: [ context top field = #consequence ]) ifTrue: [ ^ self handleNode: aTSNode kind: FASTPyThenClause ].
121121

122-
(context top isOfFASTType: FASTPyElifClause) ifTrue: [ self setTopFieldTo: #statements. "We manually update the field to bypass the block and" ^ self rawVisitChildren: aTSNode ].
123-
124-
(context top isOfFASTType: FASTPyWithStatement) ifTrue: [ self setTopFieldTo: #statements. "We manually update the field to bypass the block and" ^ self rawVisitChildren: aTSNode ].
122+
({ FASTPyElifClause. FASTPyWithStatement. FASTPyTryStatement. FASTPyExceptClause } anySatisfy: [ :type | context top isOfFASTType: type ]) ifTrue: [
123+
self setTopFieldTo: #statements. "We manually update the field to bypass the block and"
124+
^ self rawVisitChildren: aTSNode ].
125125

126126
^ self handleNode: aTSNode kind: FASTPyBlock
127127
]
@@ -324,6 +324,12 @@ TSFASTPythonImporter >> visitEscapedSequence: aTSNode [
324324
self error: 'We sould never finish here because we took the decision of not representing them and the node visit should be skipped in #visitStringContent:.'
325325
]
326326

327+
{ #category : 'visiting' }
328+
TSFASTPythonImporter >> visitExceptClause: aTSNode [
329+
330+
^ self handleNode: aTSNode kind: FASTPyExceptClause parentBlock: [ :entity | self topFastEntity addExcept: entity ]
331+
]
332+
327333
{ #category : 'visiting - alias' }
328334
TSFASTPythonImporter >> visitExpressionList: aTSNode [
329335

0 commit comments

Comments
 (0)