Skip to content

Commit b42a2c8

Browse files
committed
Simplify aliases management + start import
1 parent e308d57 commit b42a2c8

File tree

7 files changed

+61
-30
lines changed

7 files changed

+61
-30
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,8 @@ Class {
140140
'tUnaryOperator',
141141
'tAssignment',
142142
'variable',
143-
'structuralEntity'
143+
'structuralEntity',
144+
'import'
144145
],
145146
#category : 'FAST-Python-Model-Generator',
146147
#package : 'FAST-Python-Model-Generator'
@@ -230,6 +231,7 @@ FASTPythonMetamodelGenerator >> defineClasses [
230231
identifier := builder ensureClassNamed: #Identifier."Todo"
231232
ifClause := builder ensureClassNamed: #IfClause."Todo"
232233
ifStatement := builder ensureClassNamed: #IfStatement."Todo"
234+
import := builder ensureClassNamed: #Import. "To review"
233235
importFromStatement := builder ensureClassNamed: #ImportFromStatement."Todo"
234236
importPrefix := builder ensureClassNamed: #ImportPrefix."Todo"
235237
importStatement := builder ensureClassNamed: #ImportStatement."Todo"
@@ -331,6 +333,10 @@ FASTPythonMetamodelGenerator >> defineHierarchy [
331333
self flag: #todo. "I am not sure this should be the case but the importer is too young and we need this to not have everything breaking."
332334
identifier --|> expression.
333335

336+
importStatement --|> import.
337+
338+
importFromStatement --|> import.
339+
334340
float --|> literal.
335341

336342
functionDefinition --|> tNamedBehaviouralEntity.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
Class {
2+
#name : 'FASTPyImport',
3+
#superclass : 'FASTPyEntity',
4+
#category : 'FAST-Python-Model-Entities',
5+
#package : 'FAST-Python-Model',
6+
#tag : 'Entities'
7+
}
8+
9+
{ #category : 'meta' }
10+
FASTPyImport class >> annotation [
11+
12+
<FMClass: #Import super: #FASTPyEntity>
13+
<package: #'FAST-Python-Model'>
14+
<generated>
15+
^ self
16+
]

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Class {
22
#name : 'FASTPyImportFromStatement',
3-
#superclass : 'FASTPyEntity',
3+
#superclass : 'FASTPyImport',
44
#category : 'FAST-Python-Model-Entities',
55
#package : 'FAST-Python-Model',
66
#tag : 'Entities'
@@ -9,7 +9,7 @@ Class {
99
{ #category : 'meta' }
1010
FASTPyImportFromStatement class >> annotation [
1111

12-
<FMClass: #ImportFromStatement super: #FASTPyEntity>
12+
<FMClass: #ImportFromStatement super: #FASTPyImport>
1313
<package: #'FAST-Python-Model'>
1414
<generated>
1515
^ self

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Class {
22
#name : 'FASTPyImportStatement',
3-
#superclass : 'FASTPyEntity',
3+
#superclass : 'FASTPyImport',
44
#category : 'FAST-Python-Model-Entities',
55
#package : 'FAST-Python-Model',
66
#tag : 'Entities'
@@ -9,7 +9,7 @@ Class {
99
{ #category : 'meta' }
1010
FASTPyImportStatement class >> annotation [
1111

12-
<FMClass: #ImportStatement super: #FASTPyEntity>
12+
<FMClass: #ImportStatement super: #FASTPyImport>
1313
<package: #'FAST-Python-Model'>
1414
<generated>
1515
^ self

src/FAST-Python-Model/FASTPyTEntityCreator.trait.st

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ FASTPyTEntityCreator >> newIfStatement [
447447
^ self add: FASTPyIfStatement new
448448
]
449449

450+
{ #category : 'entity creation' }
451+
FASTPyTEntityCreator >> newImport [
452+
453+
<generated>
454+
^ self add: FASTPyImport new
455+
]
456+
450457
{ #category : 'entity creation' }
451458
FASTPyTEntityCreator >> newImportFromStatement [
452459

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,19 @@ FASTPythonImporterTest >> testFunctionParameters [
281281
self assertCollection: (function parameters collect: #name) hasSameElements: #( 'a' 'b' )
282282
]
283283

284+
{ #category : 'tests' }
285+
FASTPythonImporterTest >> testImport [
286+
287+
| import |
288+
self parse: 'import random'.
289+
290+
self assert: (fast allWithType: FASTPyModule) size equals: 1.
291+
self assert: (fast allWithType: FASTPyImportStatement) size equals: 1.
292+
293+
import := (fast allWithType: FASTPyImportStatement) anyOne.
294+
self assert: import sourceCode equals: 'import random'.
295+
]
296+
284297
{ #category : 'tests - literals' }
285298
FASTPythonImporterTest >> testInteger [
286299

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

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ TSFASTPythonImporter >> initialize [
1616
TSFASTPythonImporter >> manageBinaryOperator: aTSNode ofKind: aClass [
1717

1818
| fastEntity |
19-
fastEntity := self instantiateFastEntity: aClass from: aTSNode.
19+
self onNextContextDo: [ :contextEntry |
20+
contextEntry
21+
add: 'leftOperand' asAliasOfField: 'left';
22+
add: 'rightOperand' asAliasOfField: 'right' ].
2023

21-
self setParentOf: fastEntity.
22-
23-
self pushContext: fastEntity node: aTSNode during: [
24-
context top add: 'leftOperand' asAliasOfField: 'left'.
25-
context top add: 'rightOperand' asAliasOfField: 'right'.
26-
self visitChildren: aTSNode in: fastEntity ].
24+
fastEntity := self createEntityForNode: aTSNode.
2725

2826
"TreeSitter does not seems to manage operator. See: https://github.com/tree-sitter/tree-sitter-python/issues/328
2927
So I try to het this property manually."
@@ -35,17 +33,12 @@ TSFASTPythonImporter >> manageBinaryOperator: aTSNode ofKind: aClass [
3533
{ #category : 'visiting' }
3634
TSFASTPythonImporter >> visitAssignment: aTSNode [
3735

38-
| fastEntity |
39-
fastEntity := self instantiateFastEntity: FASTPyAssignment from: aTSNode.
40-
41-
self setParentOf: fastEntity.
42-
43-
self pushContext: fastEntity node: aTSNode during: [
44-
context top add: 'variable' asAliasOfField: 'left'.
45-
context top add: 'expression' asAliasOfField: 'right'.
46-
self visitChildren: aTSNode in: fastEntity ].
36+
self onNextContextDo: [ :contextEntry |
37+
contextEntry
38+
add: 'variable' asAliasOfField: 'left';
39+
add: 'expression' asAliasOfField: 'right' ].
4740

48-
^ fastEntity
41+
^ self createEntityForNode: aTSNode
4942
]
5043

5144
{ #category : 'visiting' }
@@ -64,14 +57,10 @@ TSFASTPythonImporter >> visitBooleanOperator: aTSNode [
6457
TSFASTPythonImporter >> visitComparisonOperator: aTSNode [
6558

6659
| fastEntity |
67-
fastEntity := self instantiateFastEntity: FASTPyComparisonOperator from: aTSNode.
68-
69-
self setParentOf: fastEntity.
70-
71-
self pushContext: fastEntity node: aTSNode during: [
72-
context top add: 'operands' asAliasOfField: '<unnamedChild>'.
73-
self visitChildren: aTSNode in: fastEntity ].
74-
60+
self onNextContextDo: [ :contextEntry | contextEntry add: 'operands' asAliasOfField: '<unnamedChild>' ].
61+
62+
fastEntity := self createEntityForNode: aTSNode.
63+
7564
"The node does not gives us the operators so we try to get them ourselves."
7665
fastEntity operators:
7766
(fastEntity operands overlappingPairsCollect: [ :operand1 :operand2 | (fastEntity sourceText copyFrom: operand1 endPos + 1 to: operand2 startPos - 1) trim ]).

0 commit comments

Comments
 (0)