Skip to content

Commit 45d004d

Browse files
authored
Merge pull request #175 from moosetechnology/syntax-json-importer
Syntax json importer
2 parents 22b5747 + 5f5f36a commit 45d004d

35 files changed

+3673
-88
lines changed

src/BaselineOfEsope/BaselineOfEsope.class.st

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
Class {
2-
#name : 'BaselineOfEsope',
3-
#superclass : 'BaselineOf',
4-
#category : 'BaselineOfEsope',
5-
#package : 'BaselineOfEsope'
2+
#name : #BaselineOfEsope,
3+
#superclass : #BaselineOf,
4+
#category : 'BaselineOfEsope'
65
}
76

8-
{ #category : 'baselines' }
7+
{ #category : #baselines }
98
BaselineOfEsope >> baseline: spec [
109
<baseline>
1110
spec
@@ -19,7 +18,7 @@ BaselineOfEsope >> baseline: spec [
1918

2019
]
2120

22-
{ #category : 'baselines' }
21+
{ #category : #baselines }
2322
BaselineOfEsope >> defineDependencies: spec [
2423
spec
2524
baseline: 'PetitParser2' with: [ spec repository: 'github://kursjan/petitparser2' ];
@@ -28,15 +27,15 @@ BaselineOfEsope >> defineDependencies: spec [
2827

2928
]
3029

31-
{ #category : 'baselines' }
30+
{ #category : #baselines }
3231
BaselineOfEsope >> defineGroups: spec [
3332

3433
spec
3534
group: 'famix'
3635
with: #( 'Famix-Esope-Entities' 'Famix-Esope-Generator' 'Famix-Esope-Tests' )
3736
]
3837

39-
{ #category : 'baselines' }
38+
{ #category : #baselines }
4039
BaselineOfEsope >> definePackages: spec [
4140
spec
4241
package: 'EsopeImporter' with: [ spec requires: #('Famix-Esope-Entities' PetitParser2 NeoJSON) ];

src/BaselineOfEsope/package.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Package { #name : 'BaselineOfEsope' }
1+
Package { #name : #BaselineOfEsope }

src/EsopeImporter-Tests/EsopeFullGrammarTest.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Class {
44
#instVars : [
55
'parser'
66
],
7-
#category : 'EsopeImporter-Tests-Rewriter'
7+
#category : #'EsopeImporter-Tests-Rewriter'
88
}
99

1010
{ #category : #tests }

src/EsopeImporter-Tests/EsopeGrammarTest.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Class {
44
#instVars : [
55
'sixSpaces'
66
],
7-
#category : 'EsopeImporter-Tests-Rewriter'
7+
#category : #'EsopeImporter-Tests-Rewriter'
88
}
99

1010
{ #category : #accessing }

src/EsopeImporter-Tests/IASTToFamixVisitorTest.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ IASTToFamixVisitorTest >> implicitRule: typeName range: ranges [
175175
IASTToFamixVisitorTest >> jsonToIAST: jsonCode [
176176
| jsonVisitor |
177177
jsonVisitor := JsonToIASTVisitor new.
178-
^jsonVisitor visitProgramFile: (NeoJSONReader fromString: jsonCode)
178+
^jsonVisitor onFile: (NeoJSONReader fromString: jsonCode)
179179
]
180180

181181
{ #category : #running }

src/EsopeImporter-Tests/JsonToIASTVisitorTest.class.st

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -423,10 +423,13 @@ JsonToIASTVisitorTest >> testEmptyExitStatement [
423423
"
424424

425425
| programFile |
426+
427+
self skip: 'Not a Fortran-77 instruction'.
428+
426429
programFile := self visitJsonCode: '{"meta":{"miVersion":"fortran77","miFilename":"fortran77.f"},"program_units":[{"anno":[],"arguments":null,"blocks":[{"anno":[],"label":null,"span":"(2:8)-(2:11)","statement":{"anno":[],"span":"(2:8)-(2:11)","tag":"exit","var":null},"tag":"statement"}],"name":"hello","options":[null,null],"span":"(1:7)-(3:9)","subprograms":null,"tag":"subroutine"}]}'.
427430

428431

429-
self assert: programFile body first body isEmpty
432+
self assert: programFile progUnits first body isEmpty
430433
]
431434

432435
{ #category : #'tests-progUnit' }
@@ -1328,11 +1331,14 @@ JsonToIASTVisitorTest >> testReturnConstantStatement [
13281331
end
13291332
"
13301333
| programFile fct |
1334+
1335+
self skip: 'Not a valid Fortran-77 statement'.
1336+
13311337
programFile := self visitJsonCode: '{"meta":{"miVersion":"fortran77","miFilename":"fortran77.f"},"program_units":[{"anno":[],"arguments":null,"blocks":[{"anno":[],"label":null,"span":"(2:7)-(2:15)","statement":{"anno":[],"span":"(2:7)-(2:15)","tag":"return","target":{"anno":[],"span":"(2:14)-(2:15)","tag":"value","value":{"contents":["42",null],"tag":"integer"}}},"tag":"statement"}],"name":"hello","result":null,"span":"(1:7)-(3:9)","subprograms":null,"tag":"function","type":{"anno":[],"base_type":"integer","selector":null,"span":"(1:7)-(1:13)"}}]}'.
13321338

1333-
self assert: programFile body size equals: 1.
1339+
self assert: programFile progUnits size equals: 1.
13341340

1335-
fct := programFile body first.
1341+
fct := programFile progUnits first.
13361342
self assert: fct body isEmpty
13371343
]
13381344

@@ -1746,5 +1752,5 @@ JsonToIASTVisitorTest >> visitF77Code: f77Code [
17461752
JsonToIASTVisitorTest >> visitJsonCode: jsonCode [
17471753
| visitor |
17481754
visitor := JsonToIASTVisitor new.
1749-
^visitor visitProgramFile: (NeoJSONReader fromString: jsonCode)
1755+
^visitor onFile: (NeoJSONReader fromString: jsonCode)
17501756
]

src/EsopeImporter-Tests/SyntaxJsonToIASTVisitorTest.class.st

Lines changed: 1731 additions & 0 deletions
Large diffs are not rendered by default.

src/EsopeImporter/FortranAbstractJsonVisitor.class.st

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"
22
An abstrat visitor for the JSON output of [camfort/fortran-src](https://github.com/camfort/fortran-src)
33
4-
Defines all the needed `#visitXYZ:` methods and go to the JSON tree
4+
Defines all the needed visiting methods to fully visit the JSON tree.
5+
6+
Typical used by calling `onFile: (NeoJSONReader fromString: <the-JSONCode>)`
57
"
68
Class {
79
#name : #FortranAbstractJsonVisitor,
@@ -99,6 +101,11 @@ FortranAbstractJsonVisitor >> isTaggedStatement: aStatementNode [
99101
includesKey: 'tag'
100102
]
101103

104+
{ #category : #'visiting prog-unit' }
105+
FortranAbstractJsonVisitor >> onFile: aProgramFileNode [
106+
^self visitJsonMap: aProgramFileNode keys: #(meta program_units)
107+
]
108+
102109
{ #category : #'visiting esope' }
103110
FortranAbstractJsonVisitor >> processEsopeComment: anEsopeCommentNode [
104111
"For Esope comments the treatment is similar for most of them
@@ -613,6 +620,8 @@ FortranAbstractJsonVisitor >> visitEsopeSegsupComment: anEsopeCommentNode [
613620

614621
{ #category : #'visiting statement' }
615622
FortranAbstractJsonVisitor >> visitExitStatement: anExitNode [
623+
self flag: 'Not a Fortran-77 instruction'.
624+
616625
^self visitJsonMap: anExitNode keys: #(span var)
617626
]
618627

@@ -974,10 +983,7 @@ FortranAbstractJsonVisitor >> visitJsonElement: aNode [
974983
{ #category : #'visiting reflectivity' }
975984
FortranAbstractJsonVisitor >> visitJsonMap: jsonDictionary [
976985
"If dicionary contains key 'tag', performs a visit of the whole node for this tag,
977-
otherwise, perform a visit of each value for their key
978-
'anno' key always ignored"
979-
980-
jsonDictionary removeKey: 'anno' ifAbsent: [ "nothing" ].
986+
otherwise, perform a visit of each value for their key"
981987

982988
^ (jsonDictionary includesKey: 'tag')
983989
ifTrue: [ self visitJsonTaggedElement: jsonDictionary ]
@@ -1231,11 +1237,6 @@ FortranAbstractJsonVisitor >> visitPrintStatement: aPrintStatementNode [
12311237
self visitArguments: (aPrintStatementNode at: 'arguments') }
12321238
]
12331239

1234-
{ #category : #'visiting prog-unit' }
1235-
FortranAbstractJsonVisitor >> visitProgramFile: aProgramFileNode [
1236-
^self visitJsonMap: aProgramFileNode keys: #(meta program_units)
1237-
]
1238-
12391240
{ #category : #'visiting prog-unit' }
12401241
FortranAbstractJsonVisitor >> visitProgram_units: aNode [
12411242
^self visitJsonArray: aNode

src/EsopeImporter/FortranProjectImporter.class.st

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -446,20 +446,20 @@ FortranProjectImporter >> isIncludeLine: aString [
446446
{ #category : #run }
447447
FortranProjectImporter >> jsonASTToIAST [
448448

449-
| visitor |
449+
| visitor iastEntity |
450450
visitor := JsonToIASTVisitor new.
451451

452-
iastmodel := (self tempJsonFolder allFiles)
453-
collect: [ :jsonFile | | iastEntity jsonDOM code |
454-
code := jsonFile contents.
455-
iastEntity := nil.
456-
jsonDOM := nil.
457-
errorHandler enterContext: jsonFile fullName.
458-
[ jsonDOM := NeoJSONReader fromString: code ]
459-
on: NeoJSONParseError
460-
do: [ JsonASTParseException signal: code ].
461-
jsonDOM ifNotNil: [ iastEntity := visitor visitProgramFile: jsonDOM ].
462-
errorHandler leaveContext.
452+
iastmodel := (self workingJsonName asFileReference allChildrenMatching: '*.json')
453+
collect: [ :jsonFile |
454+
errorHandler
455+
handleErrorsFor: 'file:' , jsonFile fullName
456+
running: [
457+
iastEntity := visitor onFile:
458+
(NeoJSONReader fromString: jsonFile contents) ]
459+
onErrorDo: [ :err |
460+
iastEntity := IASTErrorEntity new filename: jsonFile fullName ; yourself
461+
].
462+
463463
iastEntity
464464
]
465465

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Class {
2+
#name : #FortranProjectSyntaxImporter,
3+
#superclass : #FortranProjectImporter,
4+
#category : #'EsopeImporter-Importer'
5+
}
6+
7+
{ #category : #accessing }
8+
FortranProjectSyntaxImporter >> f77parser [
9+
10+
^ '/Users/lsafina/Projects/Syntax/trunk/examples/j77/bin/f77.out -json'
11+
]
12+
13+
{ #category : #accessing }
14+
FortranProjectSyntaxImporter >> getJsonFor: sourceCode [
15+
"very ugly, but intented for temporary use.
16+
TO BE REMOVED"
17+
| fastSrcFile json |
18+
fastSrcFile := self defaultFASTSource.
19+
fastSrcFile writeStreamDo: [:stream |
20+
stream truncate.
21+
self preprocess: sourceCode to: stream.
22+
stream lf
23+
].
24+
25+
self parseFortran77: fastSrcFile.
26+
27+
json := (((self fortranFolderToASTFolder: fastSrcFile parent) / fastSrcFile basename) withExtension: 'f.json') contents.
28+
^Character space join: (json lines collect: #trimBoth)
29+
]

src/EsopeImporter/IASTAbstractEntity.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Class {
44
#instVars : [
55
'sourceAnchor'
66
],
7-
#category : 'EsopeImporter-AST-IR'
7+
#category : #'EsopeImporter-AST-IR'
88
}
99

1010
{ #category : #visiting }

src/EsopeImporter/IASTAbstractFamixVisitor.class.st

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
1+
"
2+
An abstract visitor for the intermediate representation of a Fortran/Esope file
3+
4+
It keeps a `stack` of parsing contexts
5+
It can `spawn` another visitor on the current node for handling specialized visiting strategies
6+
"
17
Class {
28
#name : #IASTAbstractFamixVisitor,
39
#superclass : #Object,
410
#instVars : [
511
'model',
612
'stack'
713
],
8-
#category : 'EsopeImporter-Visitor'
14+
#category : #'EsopeImporter-Visitor'
915
}
1016

1117
{ #category : #'private-creation' }

src/EsopeImporter/IASTBlockData.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Class {
22
#name : #IASTBlockData,
33
#superclass : #IASTProgramUnit,
4-
#category : 'EsopeImporter-AST-IR'
4+
#category : #'EsopeImporter-AST-IR'
55
}
66

77
{ #category : #accessing }

src/EsopeImporter/IASTComment.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Class {
55
'text',
66
'isProgUnit'
77
],
8-
#category : 'EsopeImporter-AST-IR'
8+
#category : #'EsopeImporter-AST-IR'
99
}
1010

1111
{ #category : #accessing }

src/EsopeImporter/IASTEntityRef.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Class {
55
'entityName',
66
'annotations'
77
],
8-
#category : 'EsopeImporter-AST-IR'
8+
#category : #'EsopeImporter-AST-IR'
99
}
1010

1111
{ #category : #accessing }

src/EsopeImporter/IASTErrorEntity.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Class {
88
#instVars : [
99
'filename'
1010
],
11-
#category : 'EsopeImporter-AST-IR'
11+
#category : #'EsopeImporter-AST-IR'
1212
}
1313

1414
{ #category : #visiting }

src/EsopeImporter/IASTFunction.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Class {
44
#instVars : [
55
'returnType'
66
],
7-
#category : 'EsopeImporter-AST-IR'
7+
#category : #'EsopeImporter-AST-IR'
88
}
99

1010
{ #category : #accessing }

src/EsopeImporter/IASTImplicit.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Class {
44
#instVars : [
55
'rules'
66
],
7-
#category : 'EsopeImporter-AST-IR'
7+
#category : #'EsopeImporter-AST-IR'
88
}
99

1010
{ #category : #accessing }

src/EsopeImporter/IASTImplicitTypingRule.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Class {
55
'forType',
66
'ranges'
77
],
8-
#category : 'EsopeImporter-AST-IR'
8+
#category : #'EsopeImporter-AST-IR'
99
}
1010

1111
{ #category : #accessing }

src/EsopeImporter/IASTInclude.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Class {
1111
#instVars : [
1212
'includeCommand'
1313
],
14-
#category : 'EsopeImporter-AST-IR'
14+
#category : #'EsopeImporter-AST-IR'
1515
}
1616

1717
{ #category : #visiting }

src/EsopeImporter/IASTIncludedFile.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Class {
22
#name : #IASTIncludedFile,
33
#superclass : #IASTProgramUnit,
4-
#category : 'EsopeImporter-AST-IR'
4+
#category : #'EsopeImporter-AST-IR'
55
}
66

77
{ #category : #visiting }

src/EsopeImporter/IASTIndexedFileAnchor.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Class {
88
'endColumn',
99
'fileName'
1010
],
11-
#category : 'EsopeImporter-AST-IR'
11+
#category : #'EsopeImporter-AST-IR'
1212
}
1313

1414
{ #category : #accessing }

src/EsopeImporter/IASTInvocation.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Class {
66
'arguments',
77
'indices'
88
],
9-
#category : 'EsopeImporter-AST-IR'
9+
#category : #'EsopeImporter-AST-IR'
1010
}
1111

1212
{ #category : #accessing }

src/EsopeImporter/IASTMainProgram.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Class {
22
#name : #IASTMainProgram,
33
#superclass : #IASTProgramUnit,
4-
#category : 'EsopeImporter-AST-IR'
4+
#category : #'EsopeImporter-AST-IR'
55
}
66

77
{ #category : #accessing }

src/EsopeImporter/IASTParameter.class.st

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Class {
22
#name : #IASTParameter,
33
#superclass : #IASTEntityRef,
4-
#category : 'EsopeImporter-AST-IR'
4+
#category : #'EsopeImporter-AST-IR'
55
}
66

77
{ #category : #accessing }

src/EsopeImporter/IASTSubroutine.class.st

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ Class {
44
#instVars : [
55
'parameters'
66
],
7-
#category : 'EsopeImporter-AST-IR'
7+
#category : #'EsopeImporter-AST-IR'
88
}
99

1010
{ #category : #accessing }
@@ -20,12 +20,7 @@ IASTSubroutine >> parameters [
2020
]
2121

2222
{ #category : #accessing }
23-
IASTSubroutine >> parameters: aNodes [
23+
IASTSubroutine >> parameters: nodes [
2424

25-
parameters := aNodes ifNil: [ #( ) ] ifNotNil: [
26-
aNodes collect: [ :node |
27-
IASTParameter new
28-
sourceAnchor: node sourceAnchor ;
29-
entityName: node entityName ;
30-
yourself ] ]
25+
parameters := nodes
3126
]

0 commit comments

Comments
 (0)