From 9b85725d8017d2bdbb0ca4061092f6f2c55d43bd Mon Sep 17 00:00:00 2001 From: Patrick R Date: Mon, 13 Jul 2020 11:31:21 +0200 Subject: [PATCH] A new version of the method to reconstruct skipped spaces based on a source map --- .../OhmNode.class/instance/calculateSourceMap.st | 4 ++++ .../OhmNode.class/instance/enclosingRule.st | 8 ++++++++ .../OhmNode.class/instance/isRootNode.st | 4 ++++ .../instance/nextParentWithChildFurtherLeft.st | 4 ---- .../nextParentWithChildFurtherLeftThan..st | 7 ------- .../OhmNode.class/instance/rootNode.st | 6 ++++++ .../instance/skippedSpacesString.st | 16 ++++++++++++---- .../OhmNode.class/instance/sourceMap.st | 7 +++++++ .../OhmNode.class/methodProperties.json | 9 ++++++--- .../OhmNode.class/properties.json | 3 ++- .../instance/testSkippedSpacesWithManyPExpr.st | 13 +++++++++++++ .../OhmNodeTest.class/methodProperties.json | 3 ++- .../OhmSourceMapping.class/README.md | 0 .../instance/createSourceMapFor..st | 8 ++++++++ .../instance/defaultExpression..st | 7 +++++++ .../instance/sourceMap..st | 4 ++++ .../OhmSourceMapping.class/instance/sourceMap.st | 4 ++++ .../instance/terminalExpression..st | 5 +++++ .../OhmSourceMapping.class/methodProperties.json | 9 +++++++++ .../OhmSourceMapping.class/properties.json | 14 ++++++++++++++ 20 files changed, 115 insertions(+), 20 deletions(-) create mode 100644 packages/Ohm-Core.package/OhmNode.class/instance/calculateSourceMap.st create mode 100644 packages/Ohm-Core.package/OhmNode.class/instance/enclosingRule.st create mode 100644 packages/Ohm-Core.package/OhmNode.class/instance/isRootNode.st delete mode 100644 packages/Ohm-Core.package/OhmNode.class/instance/nextParentWithChildFurtherLeft.st delete mode 100644 packages/Ohm-Core.package/OhmNode.class/instance/nextParentWithChildFurtherLeftThan..st create mode 100644 packages/Ohm-Core.package/OhmNode.class/instance/rootNode.st create mode 100644 packages/Ohm-Core.package/OhmNode.class/instance/sourceMap.st create mode 100644 packages/Ohm-Core.package/OhmNodeTest.class/instance/testSkippedSpacesWithManyPExpr.st create mode 100644 packages/Ohm-Core.package/OhmSourceMapping.class/README.md create mode 100644 packages/Ohm-Core.package/OhmSourceMapping.class/instance/createSourceMapFor..st create mode 100644 packages/Ohm-Core.package/OhmSourceMapping.class/instance/defaultExpression..st create mode 100644 packages/Ohm-Core.package/OhmSourceMapping.class/instance/sourceMap..st create mode 100644 packages/Ohm-Core.package/OhmSourceMapping.class/instance/sourceMap.st create mode 100644 packages/Ohm-Core.package/OhmSourceMapping.class/instance/terminalExpression..st create mode 100644 packages/Ohm-Core.package/OhmSourceMapping.class/methodProperties.json create mode 100644 packages/Ohm-Core.package/OhmSourceMapping.class/properties.json diff --git a/packages/Ohm-Core.package/OhmNode.class/instance/calculateSourceMap.st b/packages/Ohm-Core.package/OhmNode.class/instance/calculateSourceMap.st new file mode 100644 index 0000000..ffc8b7f --- /dev/null +++ b/packages/Ohm-Core.package/OhmNode.class/instance/calculateSourceMap.st @@ -0,0 +1,4 @@ +private +calculateSourceMap + + ^ (self grammar semanticAction: OhmSourceMapping new) createSourceMapFor: self \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmNode.class/instance/enclosingRule.st b/packages/Ohm-Core.package/OhmNode.class/instance/enclosingRule.st new file mode 100644 index 0000000..bf62366 --- /dev/null +++ b/packages/Ohm-Core.package/OhmNode.class/instance/enclosingRule.st @@ -0,0 +1,8 @@ +treeNavigation +enclosingRule + + | aParent | + aParent := self parent. + [aParent ruleName = OhmParsingExpression listRuleIdentifier] whileTrue: [ + aParent := aParent parent]. + ^ aParent \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmNode.class/instance/isRootNode.st b/packages/Ohm-Core.package/OhmNode.class/instance/isRootNode.st new file mode 100644 index 0000000..dd0143c --- /dev/null +++ b/packages/Ohm-Core.package/OhmNode.class/instance/isRootNode.st @@ -0,0 +1,4 @@ +testing +isRootNode + + ^ self parent isNil \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmNode.class/instance/nextParentWithChildFurtherLeft.st b/packages/Ohm-Core.package/OhmNode.class/instance/nextParentWithChildFurtherLeft.st deleted file mode 100644 index 99a11cf..0000000 --- a/packages/Ohm-Core.package/OhmNode.class/instance/nextParentWithChildFurtherLeft.st +++ /dev/null @@ -1,4 +0,0 @@ -treeNavigation -nextParentWithChildFurtherLeft - - ^ self parent nextParentWithChildFurtherLeftThan: self \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmNode.class/instance/nextParentWithChildFurtherLeftThan..st b/packages/Ohm-Core.package/OhmNode.class/instance/nextParentWithChildFurtherLeftThan..st deleted file mode 100644 index b1a7f98..0000000 --- a/packages/Ohm-Core.package/OhmNode.class/instance/nextParentWithChildFurtherLeftThan..st +++ /dev/null @@ -1,7 +0,0 @@ -treeNavigation -nextParentWithChildFurtherLeftThan: aChildNode - - "TODO: Deal with root node" - ^ (self children indexOf: aChildNode) > 1 - ifTrue: [self] - ifFalse: [self parent nextParentWithChildFurtherLeftThan: self] \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmNode.class/instance/rootNode.st b/packages/Ohm-Core.package/OhmNode.class/instance/rootNode.st new file mode 100644 index 0000000..8fad6e5 --- /dev/null +++ b/packages/Ohm-Core.package/OhmNode.class/instance/rootNode.st @@ -0,0 +1,6 @@ +treeNavigation +rootNode + + ^ self parent + ifNil: [self] + ifNotNil: [self parent rootNode] \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmNode.class/instance/skippedSpacesString.st b/packages/Ohm-Core.package/OhmNode.class/instance/skippedSpacesString.st index fdd7e87..566d5b1 100644 --- a/packages/Ohm-Core.package/OhmNode.class/instance/skippedSpacesString.st +++ b/packages/Ohm-Core.package/OhmNode.class/instance/skippedSpacesString.st @@ -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 diff --git a/packages/Ohm-Core.package/OhmNode.class/instance/sourceMap.st b/packages/Ohm-Core.package/OhmNode.class/instance/sourceMap.st new file mode 100644 index 0000000..a46657e --- /dev/null +++ b/packages/Ohm-Core.package/OhmNode.class/instance/sourceMap.st @@ -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] + \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmNode.class/methodProperties.json b/packages/Ohm-Core.package/OhmNode.class/methodProperties.json index 52a443c..e07e36f 100644 --- a/packages/Ohm-Core.package/OhmNode.class/methodProperties.json +++ b/packages/Ohm-Core.package/OhmNode.class/methodProperties.json @@ -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" } } diff --git a/packages/Ohm-Core.package/OhmNode.class/properties.json b/packages/Ohm-Core.package/OhmNode.class/properties.json index 53910bd..a5d9d97 100644 --- a/packages/Ohm-Core.package/OhmNode.class/properties.json +++ b/packages/Ohm-Core.package/OhmNode.class/properties.json @@ -10,7 +10,8 @@ "grammar", "interval", "children", - "parent" ], + "parent", + "sourceMap" ], "name" : "OhmNode", "pools" : [ ], diff --git a/packages/Ohm-Core.package/OhmNodeTest.class/instance/testSkippedSpacesWithManyPExpr.st b/packages/Ohm-Core.package/OhmNodeTest.class/instance/testSkippedSpacesWithManyPExpr.st new file mode 100644 index 0000000..58e525a --- /dev/null +++ b/packages/Ohm-Core.package/OhmNodeTest.class/instance/testSkippedSpacesWithManyPExpr.st @@ -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. \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmNodeTest.class/methodProperties.json b/packages/Ohm-Core.package/OhmNodeTest.class/methodProperties.json index 6fc92aa..06db001 100644 --- a/packages/Ohm-Core.package/OhmNodeTest.class/methodProperties.json +++ b/packages/Ohm-Core.package/OhmNodeTest.class/methodProperties.json @@ -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" } } diff --git a/packages/Ohm-Core.package/OhmSourceMapping.class/README.md b/packages/Ohm-Core.package/OhmSourceMapping.class/README.md new file mode 100644 index 0000000..e69de29 diff --git a/packages/Ohm-Core.package/OhmSourceMapping.class/instance/createSourceMapFor..st b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/createSourceMapFor..st new file mode 100644 index 0000000..9d13aae --- /dev/null +++ b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/createSourceMapFor..st @@ -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] \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmSourceMapping.class/instance/defaultExpression..st b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/defaultExpression..st new file mode 100644 index 0000000..715af15 --- /dev/null +++ b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/defaultExpression..st @@ -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]. \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmSourceMapping.class/instance/sourceMap..st b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/sourceMap..st new file mode 100644 index 0000000..1f332f4 --- /dev/null +++ b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/sourceMap..st @@ -0,0 +1,4 @@ +accessing +sourceMap: anObject + + sourceMap := anObject. \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmSourceMapping.class/instance/sourceMap.st b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/sourceMap.st new file mode 100644 index 0000000..424dfb1 --- /dev/null +++ b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/sourceMap.st @@ -0,0 +1,4 @@ +accessing +sourceMap + + ^ sourceMap \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmSourceMapping.class/instance/terminalExpression..st b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/terminalExpression..st new file mode 100644 index 0000000..b5f178c --- /dev/null +++ b/packages/Ohm-Core.package/OhmSourceMapping.class/instance/terminalExpression..st @@ -0,0 +1,5 @@ +standard attributes +terminalExpression: aNode + + aNode interval start to: aNode interval end do: [:i | + (self sourceMap at: i) add: aNode] \ No newline at end of file diff --git a/packages/Ohm-Core.package/OhmSourceMapping.class/methodProperties.json b/packages/Ohm-Core.package/OhmSourceMapping.class/methodProperties.json new file mode 100644 index 0000000..36868fc --- /dev/null +++ b/packages/Ohm-Core.package/OhmSourceMapping.class/methodProperties.json @@ -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" } } diff --git a/packages/Ohm-Core.package/OhmSourceMapping.class/properties.json b/packages/Ohm-Core.package/OhmSourceMapping.class/properties.json new file mode 100644 index 0000000..c9a83f1 --- /dev/null +++ b/packages/Ohm-Core.package/OhmSourceMapping.class/properties.json @@ -0,0 +1,14 @@ +{ + "category" : "Ohm-Core", + "classinstvars" : [ + ], + "classvars" : [ + ], + "commentStamp" : "", + "instvars" : [ + "sourceMap" ], + "name" : "OhmSourceMapping", + "pools" : [ + ], + "super" : "OhmSemanticAction", + "type" : "normal" }