From 1717995c4f88c5af9945e71c71d1329dc704e040 Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Wed, 15 May 2024 19:32:43 +0200
Subject: [PATCH] Remove no longer needed checks from generated
 Parser.computePositions

Now that this method is only called for newly created AST nodes, there's
no need anymore to handle node lists, tokens, and nodes that already
have positions.
---
 .../org/sablecc/sablecc/parser/Parser.java    | 51 ++++++-------------
 .../resources/org/sablecc/sablecc/parser.txt  | 51 ++++++-------------
 2 files changed, 32 insertions(+), 70 deletions(-)

diff --git a/src/main/java/org/sablecc/sablecc/parser/Parser.java b/src/main/java/org/sablecc/sablecc/parser/Parser.java
index 25dfea1..1ab72f9 100644
--- a/src/main/java/org/sablecc/sablecc/parser/Parser.java
+++ b/src/main/java/org/sablecc/sablecc/parser/Parser.java
@@ -37,45 +37,26 @@ public class Parser implements IParser
         this.lexer = lexer;
     }
 
-    private void computePositions(Object resultNodeOrList, List<?> beginNodeList, List<?> endNodeList) {
-        if (resultNodeOrList instanceof List<?>) {
+    private void computePositions(PositionedNode node, List<?> beginNodeList, List<?> endNodeList) {
+        // This method should only ever be called for newly created AST nodes (that are not tokens).
+        assert !(node instanceof Token);
+        assert node.getStartPos() == null;
+        assert node.getEndPos() == null;
+
+        PositionedNode beginNode = findBeginNode(beginNodeList);
+        if (beginNode == null) {
             /*
-             * special case: this is a list of nodes, for example an identifier
-             * list, so we don't want to assign positions to the entire list,
-             * but the last element added to it
+             * Sometimes (haven't found out why) we get empty list here. In
+             * the only observed cases we were looking for the source range
+             * of the whole parse unit. Then the start pos is the first char.
              */
-            final List<?> nodeList = (List<?>) resultNodeOrList;
-            if (nodeList.isEmpty()) {
-                // no positions for empty lists...
-                return;
-            }
-            resultNodeOrList = nodeList.get(nodeList.size() - 1);
-        }
-
-        final PositionedNode node = (PositionedNode) resultNodeOrList;
-
-        // Add node positions if they haven't been calculated yet.
-        // Tokens are skipped because they always have positions -
-        // this avoids creating unnecessary SourcePosition objects.
-        if (!(node instanceof Token) && node.getStartPos() == null) {
-            assert node.getEndPos() == null;
-            PositionedNode beginNode = findBeginNode(beginNodeList);
-            if (beginNode == null) {
-                /*
-                 * Sometimes (haven't found out why) we get empty list here. In
-                 * the only observed cases we were looking for the source range
-                 * of the whole parse unit. Then the start pos is the first char.
-                 */
-                node.setStartPos(new SourcePosition(1, 1));
-            } else {
-                node.setStartPos(beginNode.getStartPos());
-            }
-
-            PositionedNode endNode = findEndNode(endNodeList);
-            node.setEndPos(endNode.getEndPos());
+            node.setStartPos(new SourcePosition(1, 1));
         } else {
-            assert node.getEndPos() != null;
+            node.setStartPos(beginNode.getStartPos());
         }
+
+        PositionedNode endNode = findEndNode(endNodeList);
+        node.setEndPos(endNode.getEndPos());
     }
 
     private PositionedNode findBeginNode(final List<?> list) {
diff --git a/src/main/resources/org/sablecc/sablecc/parser.txt b/src/main/resources/org/sablecc/sablecc/parser.txt
index 65591e4..7a52153 100644
--- a/src/main/resources/org/sablecc/sablecc/parser.txt
+++ b/src/main/resources/org/sablecc/sablecc/parser.txt
@@ -45,45 +45,26 @@ public class Parser implements IParser
         this.lexer = lexer;
     }
 
-    private void computePositions(Object resultNodeOrList, List<?> beginNodeList, List<?> endNodeList) {
-        if (resultNodeOrList instanceof List<?>) {
+    private void computePositions(PositionedNode node, List<?> beginNodeList, List<?> endNodeList) {
+        // This method should only ever be called for newly created AST nodes (that are not tokens).
+        assert !(node instanceof Token);
+        assert node.getStartPos() == null;
+        assert node.getEndPos() == null;
+
+        PositionedNode beginNode = findBeginNode(beginNodeList);
+        if (beginNode == null) {
             /*
-             * special case: this is a list of nodes, for example an identifier
-             * list, so we don't want to assign positions to the entire list,
-             * but the last element added to it
+             * Sometimes (haven't found out why) we get empty list here. In
+             * the only observed cases we were looking for the source range
+             * of the whole parse unit. Then the start pos is the first char.
              */
-            final List<?> nodeList = (List<?>) resultNodeOrList;
-            if (nodeList.isEmpty()) {
-                // no positions for empty lists...
-                return;
-            }
-            resultNodeOrList = nodeList.get(nodeList.size() - 1);
-        }
-
-        final PositionedNode node = (PositionedNode) resultNodeOrList;
-
-        // Add node positions if they haven't been calculated yet.
-        // Tokens are skipped because they always have positions -
-        // this avoids creating unnecessary SourcePosition objects.
-        if (!(node instanceof Token) && node.getStartPos() == null) {
-            assert node.getEndPos() == null;
-            PositionedNode beginNode = findBeginNode(beginNodeList);
-            if (beginNode == null) {
-                /*
-                 * Sometimes (haven't found out why) we get empty list here. In
-                 * the only observed cases we were looking for the source range
-                 * of the whole parse unit. Then the start pos is the first char.
-                 */
-                node.setStartPos(new SourcePosition(1, 1));
-            } else {
-                node.setStartPos(beginNode.getStartPos());
-            }
-
-            PositionedNode endNode = findEndNode(endNodeList);
-            node.setEndPos(endNode.getEndPos());
+            node.setStartPos(new SourcePosition(1, 1));
         } else {
-            assert node.getEndPos() != null;
+            node.setStartPos(beginNode.getStartPos());
         }
+
+        PositionedNode endNode = findEndNode(endNodeList);
+        node.setEndPos(endNode.getEndPos());
     }
 
     private PositionedNode findBeginNode(final List<?> list) {
-- 
GitLab