Skip to content

Commit e39fc04

Browse files
committed
Use new reified context entry + manage name of class/function/method definitions
1 parent 685d3b1 commit e39fc04

4 files changed

Lines changed: 66 additions & 20 deletions

File tree

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ FASTPythonMetamodelGenerator >> defineHierarchy [
296296

297297
entity --|> tEntity.
298298

299+
classDefinition --|> tNamedEntity.
300+
299301
comment --|> tComment.
300302

301303
functionDefinition --|> tNamedBehaviouralEntity.

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

Lines changed: 22 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+
| `invokedIn` | `FASTTNamedEntity` | `invoked` | `FASTTInvocation` | Optional invocation where this name is used|
9+
10+
11+
## Properties
12+
======================
13+
14+
| Name | Type | Default value | Comment |
15+
|---|
16+
| `endPos` | `Number` | nil | |
17+
| `name` | `String` | nil | |
18+
| `startPos` | `Number` | nil | |
19+
20+
"
121
Class {
222
#name : 'FASTPythonClassDefinition',
323
#superclass : 'FASTPythonEntity',
24+
#traits : 'FASTTNamedEntity',
25+
#classTraits : 'FASTTNamedEntity classTrait',
426
#category : 'FAST-Python-Model-Entities',
527
#package : 'FAST-Python-Model',
628
#tag : 'Entities'

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

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,62 @@ FASTPythonImporterTest >> setUp [
2929
{ #category : 'tests' }
3030
FASTPythonImporterTest >> testClassDefinition [
3131

32+
| class |
3233
self parse: 'class AClass():
3334
pass'.
3435

3536
self assert: (fast allWithType: FASTPythonModule) size equals: 1.
36-
self assert: (fast allWithType: FASTPythonClassDefinition) size equals: 1
37+
self assert: (fast allWithType: FASTPythonClassDefinition) size equals: 1.
38+
39+
class := (fast allWithType: FASTPythonClassDefinition) anyOne.
40+
self assert: class name equals: 'AClass'
3741
]
3842

3943
{ #category : 'tests' }
4044
FASTPythonImporterTest >> testFunctionDefinition [
4145

46+
| function |
4247
self parse: 'def test():
4348
return 1'.
4449

4550
self assert: (fast allWithType: FASTPythonModule) size equals: 1.
46-
self assert: (fast allWithType: FASTPythonFunctionDefinition) size equals: 1
51+
self assert: (fast allWithType: FASTPythonFunctionDefinition) size equals: 1.
52+
53+
function := (fast allWithType: FASTPythonFunctionDefinition) anyOne.
54+
self assert: function name equals: 'test'
55+
]
56+
57+
{ #category : 'tests - parameters' }
58+
FASTPythonImporterTest >> testFunctionParameters [
59+
60+
| function |
61+
self parse: 'def add(a, b):
62+
return a + b'.
63+
64+
self assert: (fast allWithType: FASTPythonModule) size equals: 1.
65+
self assert: (fast allWithType: FASTPythonFunctionDefinition) size equals: 1.
66+
function := (fast allWithType: FASTPythonFunctionDefinition) anyOne.
67+
68+
self assert: (fast allWithType: FASTPythonParameter) size equals: 2.
69+
self assertCollection: (fast allWithType: FASTPythonParameter) hasSameElements: function parameters.
70+
function parameters do: [ :parameter | self assert: (parameter isOfType: FASTPythonParameter) ].
71+
self assertCollection: (function parameters collect: #name) hasSameElements: #( 'a' 'b' )
4772
]
4873

4974
{ #category : 'tests' }
5075
FASTPythonImporterTest >> testMethodDefinition [
5176

77+
| method |
5278
self parse: 'class AClass():
5379
def method(self):
5480
pass'.
5581

5682
self assert: (fast allWithType: FASTPythonModule) size equals: 1.
5783
self assert: (fast allWithType: FASTPythonClassDefinition) size equals: 1.
5884
self assert: (fast allWithType: FASTPythonMethodDefinition) size equals: 1.
85+
86+
method := (fast allWithType: FASTPythonMethodDefinition) anyOne.
87+
self assert: method name equals: 'method'
5988
]
6089

6190
{ #category : 'tests' }
@@ -73,23 +102,6 @@ FASTPythonImporterTest >> testOneComment [
73102
self assert: entity sourceCode equals: '#There is only one comment'
74103
]
75104

76-
{ #category : 'tests - parameters' }
77-
FASTPythonImporterTest >> testParameters [
78-
79-
| function |
80-
self parse: 'def add(a, b):
81-
return a + b'.
82-
83-
self assert: (fast allWithType: FASTPythonModule) size equals: 1.
84-
self assert: (fast allWithType: FASTPythonFunctionDefinition) size equals: 1.
85-
function := (fast allWithType: FASTPythonFunctionDefinition) anyOne.
86-
87-
self assert: (fast allWithType: FASTPythonParameter) size equals: 2.
88-
self assertCollection: (fast allWithType: FASTPythonParameter) hasSameElements: function parameters.
89-
function parameters do: [ :parameter | self assert: (parameter isOfType: FASTPythonParameter) ].
90-
self assertCollection: (function parameters collect: #name) hasSameElements: #( 'a' 'b' )
91-
]
92-
93105
{ #category : 'tests' }
94106
FASTPythonImporterTest >> testTwoComments [
95107

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ TSFASTPythonImporter >> initialize [
1616
TSFASTPythonImporter >> visitFunctionDefinition: aNode [
1717
"TreeSitter has the same nodes for method and function definitions but in FAST we want to disambiguate."
1818

19-
^ (context anySatisfy: #isClassDefinition)
19+
| definition |
20+
definition := (context anySatisfy: [ :entry | entry fastEntity isClassDefinition ])
2021
ifTrue: [ self createEntityForNode: aNode kind: FASTPythonMethodDefinition ]
2122
ifFalse: [ self createEntityForNode: aNode kind: FASTPythonFunctionDefinition ]
2223
]
2324

25+
{ #category : 'visiting' }
26+
TSFASTPythonImporter >> visitIdentifier: aNode [
27+
28+
"If we are in a field `name` then we are probably in a definition of an entity and we set my content as a name."
29+
(context anySatisfy: [ :entry | entry field = #name ]) ifTrue: [ ^ context top fastEntity name: (self sourceCodeOf: aNode) ].
30+
31+
^ self createEntityForNode: aNode
32+
]
33+
2434
{ #category : 'visiting' }
2535
TSFASTPythonImporter >> visitParameters: aNode [
2636
"For parameters we want one by parameter. Not one node for multiple ones."

0 commit comments

Comments
 (0)