Skip to content

Rewriter #219

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions src/EsopeImporter-Tests/EsopeRewriterTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ EsopeRewriterTest >> testFortranLineStartWithTab [
self assert: parser generatedCode equals: ' call procedure', String lf.
]

{ #category : 'tests' }
EsopeRewriterTest >> testNoEndOfLine [

| sourcecode |
sourcecode := ' function toto
end'.
self parse: sourcecode.

self assert: parser generatedCode equals: sourcecode , OSPlatform current lineEnding
]

{ #category : 'tests' }
EsopeRewriterTest >> testNoTranslateFormat [

Expand Down
19 changes: 16 additions & 3 deletions src/EsopeImporter/PPEsopeGrammar.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ Class {
'segment',
'segprt',
'commentLine',
'anyLine'
'anyLine',
'lastLine'
],
#category : 'EsopeImporter-Importer',
#package : 'EsopeImporter',
Expand All @@ -43,6 +44,8 @@ PPEsopeGrammar class >> getSourceFromFile: filename [

{ #category : 'grammar' }
PPEsopeGrammar >> anyLine [
"The order between commentLine and fortranLine is important"

^ commentLine
/ fortranLine
/ emptyLine
Expand Down Expand Up @@ -118,9 +121,9 @@ PPEsopeGrammar >> file [
/ segsup
"No-Esope construct"
/ include
"The order between commentLine and fortranLine is important"
/ anyLine
) plus
) plus ,
lastLine
]

{ #category : 'grammar' }
Expand Down Expand Up @@ -162,6 +165,15 @@ PPEsopeGrammar >> includeName [
) flatten
]

{ #category : 'grammar' }
PPEsopeGrammar >> lastLine [
"Same as fortranLine but with option endOfLine
in case there is no return at the end of the file"

^ endOfLine negate star flatten
, endOfLine optional
]

{ #category : 'tokens' }
PPEsopeGrammar >> optionalBlanks [

Expand Down Expand Up @@ -215,6 +227,7 @@ PPEsopeGrammar >> segsup [

{ #category : 'grammar' }
PPEsopeGrammar >> start [

^file end
]

Expand Down
33 changes: 26 additions & 7 deletions src/EsopeImporter/PPEsopeRewriter.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ PPEsopeRewriter >> applySlashTransformation: inputSource [
regex := '\(\s?(\/)[^/]' asRegexIgnoringCase.
index := 1.
(regex matchingRangesIn: inputSource)
do: [ :interval | "1halt".
do: [ :interval |
oStream << (inputSource copyFrom: index to: interval first);
<< 'S__';
<< (inputSource copyFrom: interval first + 1 to: interval last).
Expand Down Expand Up @@ -177,8 +177,8 @@ PPEsopeRewriter >> fortranLine [
, endOfLine"

^ super fortranLine
==> [ :nodes | | line |
line := self sanitize: nodes first.
==> [ :nodes | | line |
line := self sanitizeLineWithTab: nodes first.
stream
<< ((self isCommentStatement: line)
ifTrue: [ line ]
Expand Down Expand Up @@ -270,6 +270,24 @@ PPEsopeRewriter >> isIfLogicalStatementFollowedBySegmentCommand: textLine [
ifFalse: [ false ]
]

{ #category : 'rules' }
PPEsopeRewriter >> lastLine [
" endOfLine negate star flatten
, endOfLine optional

if last line contains something it means it does not finish with return
in that case, add one
Note: last line should not need to be checked for any Esope stuff"

^ super lastLine
==> [ :nodes | | line |
line := self sanitizeLineWithTab: nodes first.
stream
<< line trimRight .
line ifNotEmpty: [ stream << OSPlatform current lineEnding ]
]
]

{ #category : 'utilities' }
PPEsopeRewriter >> logicalOperators [

Expand Down Expand Up @@ -326,7 +344,7 @@ PPEsopeRewriter >> processLinesInsideSegment: anOrderedCollection [
self addLineSeparator"""

anOrderedCollection do: [ :line | "option1"
line at: 1 put: (self sanitize: line first).
line at: 1 put: (self sanitizeLineWithTab: line first).
(self contains: line keyword: 'pointeur') ifTrue: [ line at: 1 put: self commentAnnotation , ' ' ].
line do: [ :part | part ifNotNil: [ stream << part value ] ] ]
]
Expand Down Expand Up @@ -370,13 +388,14 @@ PPEsopeRewriter >> rewriteFrom: esopeFile to: annotatedFile [
"Note: end-of-line character may be an issue on none Linux computers
Could use `OSPlatform current lineEnding` to get the 'correct line-ending'"

self parse: esopeFile contents.
self parse: esopeFile contents onError: [ Error signal: 'Rewriting error' ].

annotatedFile writeStreamDo: [ :fileStream | fileStream << self generatedCode ]
]

{ #category : 'utilities' }
PPEsopeRewriter >> sanitize: aString [
PPEsopeRewriter >> sanitizeLineWithTab: aString [
"replaces tab by spaces at the start of a line"

| firstSixChars index |
firstSixChars := aString size < 6
Expand Down