Skip to content

Commit

Permalink
A new version of the method to reconstruct skipped spaces based on a …
Browse files Browse the repository at this point in the history
…source map
  • Loading branch information
codeZeilen committed Jul 13, 2020
1 parent 31c8c08 commit 9b85725
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
private
calculateSourceMap

^ (self grammar semanticAction: OhmSourceMapping new) createSourceMapFor: self
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
treeNavigation
enclosingRule

| aParent |
aParent := self parent.
[aParent ruleName = OhmParsingExpression listRuleIdentifier] whileTrue: [
aParent := aParent parent].
^ aParent
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
testing
isRootNode

^ self parent isNil

This file was deleted.

This file was deleted.

6 changes: 6 additions & 0 deletions packages/Ohm-Core.package/OhmNode.class/instance/rootNode.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
treeNavigation
rootNode

^ self parent
ifNil: [self]
ifNotNil: [self parent rootNode]
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ skippedSpacesString
"This is calculated on demand as most semantics don't need it.
This is work in progress!"

| leftSibling spacesInterval |
leftSibling := self nextParentWithChildFurtherLeft children before: self.
spacesInterval := leftSibling interval end + 1 to: self interval start - 1.

| spacesInterval ownSourceMapDepth reversedMap enclosingRule reversePosition previousNodeIntervalEnd |
ownSourceMapDepth := (self sourceMap at: self interval start) size.
reversedMap := self sourceMap reversed.
enclosingRule := self enclosingRule.
reversePosition := [:originalPosition | (reversedMap size - originalPosition) + 1].

previousNodeIntervalEnd := reversedMap
findFirst: [:nodes | nodes last ~~ enclosingRule]
startingAt: (reversePosition value: self interval start) + 1.
previousNodeIntervalEnd := reversePosition value: previousNodeIntervalEnd.
spacesInterval := previousNodeIntervalEnd + 1 to: self interval start - 1.

"The following is a hack as we should not access the stream"
^ self interval stream contents
copyFrom: spacesInterval start
Expand Down
7 changes: 7 additions & 0 deletions packages/Ohm-Core.package/OhmNode.class/instance/sourceMap.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
accessing
sourceMap
"The sourceMap is only held by the root node"
^ self isRootNode
ifTrue: [sourceMap ifNil: [sourceMap := self calculateSourceMap]]
ifFalse: [self rootNode sourceMap]

9 changes: 6 additions & 3 deletions packages/Ohm-Core.package/OhmNode.class/methodProperties.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
"newNamed:forGrammar:andInput:startedAt:" : "pre 1/6/2015 17:49",
"newNamed:forGrammar:andInput:startedAt:withChildren:" : "pre 1/6/2015 17:17" },
"instance" : {
"calculateSourceMap" : "pre 7/13/2020 10:41",
"children" : "pre 1/5/2015 16:06",
"children:" : "pre 1/5/2015 16:06",
"enclosingRule" : "pre 7/13/2020 11:20",
"grammar" : "pre 1/5/2015 15:38",
"grammar:" : "pre 1/5/2015 15:38",
"initialize" : "pre 11/21/2017 15:29",
"interval" : "pre 1/5/2015 15:38",
"interval:" : "pre 1/5/2015 15:38",
"isListNode" : "pre 3/2/2018 16:08",
"isRootNode" : "pre 7/13/2020 10:31",
"isTerminalNode" : "pre 3/2/2018 16:17",
"nextParentWithChildFurtherLeft" : "pre 7/10/2020 10:50",
"nextParentWithChildFurtherLeftThan:" : "pre 7/10/2020 10:52",
"parent" : "pre 1/21/2015 15:03",
"parent:" : "pre 1/21/2015 15:03",
"printStringLimitedTo:" : "pre 6/9/2015 13:39",
"rootNode" : "pre 7/13/2020 10:28",
"ruleName" : "pre 1/6/2015 17:15",
"ruleName:" : "pre 1/6/2015 17:15",
"skippedSpacesNodes" : "pre 7/10/2020 11:17",
"skippedSpacesString" : "pre 7/10/2020 11:15" } }
"skippedSpacesString" : "pre 7/13/2020 11:23",
"sourceMap" : "pre 7/13/2020 10:32" } }
3 changes: 2 additions & 1 deletion packages/Ohm-Core.package/OhmNode.class/properties.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"grammar",
"interval",
"children",
"parent" ],
"parent",
"sourceMap" ],
"name" : "OhmNode",
"pools" : [
],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
running
testSkippedSpacesWithManyPExpr

| result |
grammar := OhmGrammar new: 'OhmNodeTestGrammar {
StartRule = (";" firstRule)+
firstRule = "a"
space += comment
comment = "\"" (~"\"" any)* "\""
}'.
result := (grammar match: ';a ; "comment" a' startingFrom: #StartRule) cst.

self assert: ' "comment" ' equals: result children last children last skippedSpacesString.
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
"instance" : {
"setUp" : "pre 7/10/2020 11:14",
"testSkippedSpacesNormalCase" : "pre 7/10/2020 11:15",
"testSkippedSpacesNormalCaseNodes" : "pre 7/10/2020 11:19" } }
"testSkippedSpacesNormalCaseNodes" : "pre 7/10/2020 11:19",
"testSkippedSpacesWithManyPExpr" : "pre 7/13/2020 11:27" } }
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
source map generating
createSourceMapFor: aNode

self sourceMap: ((1 to: aNode interval length) collect: [:i | OrderedCollection new]).

[self value: aNode.
^ self sourceMap]
ensure: [self sourceMap: nil]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
standard attributes
defaultExpression: aNode

(aNode interval start to: (aNode interval end min: self sourceMap size))
do: [:index | (self sourceMap at: index) add: aNode].

aNode children do: [:c | self value: c].
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
sourceMap: anObject

sourceMap := anObject.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
accessing
sourceMap

^ sourceMap
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
standard attributes
terminalExpression: aNode

aNode interval start to: aNode interval end do: [:i |
(self sourceMap at: i) add: aNode]
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"class" : {
},
"instance" : {
"createSourceMapFor:" : "pre 7/13/2020 10:39",
"defaultExpression:" : "pre 7/13/2020 11:02",
"sourceMap" : "pre 4/29/2020 10:20",
"sourceMap:" : "pre 4/29/2020 10:20",
"terminalExpression:" : "pre 7/13/2020 11:01" } }
14 changes: 14 additions & 0 deletions packages/Ohm-Core.package/OhmSourceMapping.class/properties.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"category" : "Ohm-Core",
"classinstvars" : [
],
"classvars" : [
],
"commentStamp" : "",
"instvars" : [
"sourceMap" ],
"name" : "OhmSourceMapping",
"pools" : [
],
"super" : "OhmSemanticAction",
"type" : "normal" }

0 comments on commit 9b85725

Please sign in to comment.