From c394250a9d5ab618e7893d2959a526912c9aac6f Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Fri, 22 Apr 2022 15:25:03 +0200
Subject: [PATCH] Refactor checkResults internals to avoid unnecessary lookups

Now gets start/end positions directly from nodes where possible instead
of going through getTokenList() and getMapping(). This should also
reduce the amount of SourcePosition objects that need to be created, as
the existing ones from child nodes/tokens are reused.
---
 .../org/sablecc/sablecc/parser/Parser.java    | 84 +++++++++----------
 .../resources/org/sablecc/sablecc/parser.txt  | 84 +++++++++----------
 2 files changed, 80 insertions(+), 88 deletions(-)

diff --git a/src/main/java/org/sablecc/sablecc/parser/Parser.java b/src/main/java/org/sablecc/sablecc/parser/Parser.java
index d2ad45a..201e986 100644
--- a/src/main/java/org/sablecc/sablecc/parser/Parser.java
+++ b/src/main/java/org/sablecc/sablecc/parser/Parser.java
@@ -61,65 +61,74 @@ public class Parser implements IParser
         final PositionedNode node = (PositionedNode) elementToCheck;
 
         if (!this.getMapping().containsKey(node)) {
-            final int begin = findBeginPos(beginNodeList, node);
-            int end = findEndPos(endNodeList);
+            PositionedNode beginNode = findBeginNode(beginNodeList);
+            final int begin;
+            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 index is 0.
+                 */
+                beginNode = (PositionedNode)this.lexer.getTokenList().get(0);
+                begin = 0;
+            } else {
+                begin = findBeginPos(beginNode);
+            }
+
+            PositionedNode endNode = findEndNode(endNodeList);
+            int end = findEndPos(endNode);
             if (end == -1) end = begin;
             final SourcecodeRange range = new SourcecodeRange(begin, end);
 
             this.getMapping().put(node, range);
 
-            node.setStartPos(createBeginPos(begin));
-            node.setEndPos(createEndPos(end));
+            node.setStartPos(beginNode.getStartPos());
+            node.setEndPos(endNode.getEndPos());
         }
     }
 
-
-    private int findBeginPos(final List<Object> list,
-            PositionedNode n) {
+    private PositionedNode findBeginNode(final List<Object> list) {
         Object first = list.get(0);
-        if (!(first instanceof PositionedNode) && !(first instanceof IToken)) {
+        if (first instanceof List<?>) {
             List<?> list2 = (List<?>) first;
 
             if (list2.size() > 0) {
-                first = list2.get(0);
+                return (PositionedNode)list2.get(0);
             } else {
-                /*
-                 * 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 index is 0.
-                 */
-                return 0;
+                return null;
             }
         }
 
-        if (first instanceof IToken) {
-            return findIndex((IToken) first);
-        }
+        return (PositionedNode)first;
+    }
 
-        final PositionedNode node = (PositionedNode) first;
-        final SourcecodeRange item = this.getMapping().get(node);
-        if (item == null){
-            System.err.println(n.getClass().getSimpleName() + " / " + node.getClass().getSimpleName() + ": " + node);
+    private int findBeginPos(final PositionedNode node) {
+        if (node instanceof IToken) {
+            return findIndex((IToken) node);
         }
-        return item.getBeginIndex();
-    }
 
+        return this.getMapping().get(node).getBeginIndex();
+    }
 
-    private int findEndPos(final List<Object> list) {
+    private PositionedNode findEndNode(final List<Object> list) {
         Object last = list.get(list.size() - 1);
-        if (!(last instanceof PositionedNode) && !(last instanceof IToken)) {
+        if (last instanceof List<?>) {
             final List<?> list2 = (List<?>) last;
-            last = list2.get(list2.size() - 1);
+            return (PositionedNode)list2.get(list2.size() - 1);
         }
 
-        if (last instanceof IToken) {
-            return findIndex((IToken) last);
+        return (PositionedNode)last;
+    }
+
+    private int findEndPos(final PositionedNode node) {
+        if (node instanceof IToken) {
+            return findIndex((IToken) node);
         }
 
-        final PositionedNode node = (PositionedNode) last;
         final SourcecodeRange item = this.getMapping().get(node);
-        if (item == null)
+        if (item == null) {
             return -1;
+        }
         return item.getEndIndex();
     }
 
@@ -135,19 +144,6 @@ public class Parser implements IParser
         return -1;
     }
 
-    private SourcePosition createBeginPos(final int index) {
-        final List<IToken> list = this.lexer.getTokenList();
-        final IToken token = list.get(index);
-        return new SourcePosition(token.getLine(), token.getPos());
-    }
-
-    private SourcePosition createEndPos(final int index) {
-        final List<IToken> list = this.lexer.getTokenList();
-        final IToken token = list.get(index);
-        return new SourcePosition(token.getLine(), token.getPos()
-            + token.getText().length());
-    }
-
     /**
      * @deprecated Overriding this method no longer has any effect. This optimization is now applied automatically iff it is safe.
      */
diff --git a/src/main/resources/org/sablecc/sablecc/parser.txt b/src/main/resources/org/sablecc/sablecc/parser.txt
index a843d32..9873f83 100644
--- a/src/main/resources/org/sablecc/sablecc/parser.txt
+++ b/src/main/resources/org/sablecc/sablecc/parser.txt
@@ -69,65 +69,74 @@ public class Parser implements IParser
         final PositionedNode node = (PositionedNode) elementToCheck;
 
         if (!this.getMapping().containsKey(node)) {
-            final int begin = findBeginPos(beginNodeList, node);
-            int end = findEndPos(endNodeList);
+            PositionedNode beginNode = findBeginNode(beginNodeList);
+            final int begin;
+            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 index is 0.
+                 */
+                beginNode = (PositionedNode)this.lexer.getTokenList().get(0);
+                begin = 0;
+            } else {
+                begin = findBeginPos(beginNode);
+            }
+
+            PositionedNode endNode = findEndNode(endNodeList);
+            int end = findEndPos(endNode);
             if (end == -1) end = begin;
             final SourcecodeRange range = new SourcecodeRange(begin, end);
 
             this.getMapping().put(node, range);
 
-            node.setStartPos(createBeginPos(begin));
-            node.setEndPos(createEndPos(end));
+            node.setStartPos(beginNode.getStartPos());
+            node.setEndPos(endNode.getEndPos());
         }
     }
 
-
-    private int findBeginPos(final List<Object> list,
-            PositionedNode n) {
+    private PositionedNode findBeginNode(final List<Object> list) {
         Object first = list.get(0);
-        if (!(first instanceof PositionedNode) && !(first instanceof IToken)) {
+        if (first instanceof List<?>) {
             List<?> list2 = (List<?>) first;
 
             if (list2.size() > 0) {
-                first = list2.get(0);
+                return (PositionedNode)list2.get(0);
             } else {
-                /*
-                 * 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 index is 0.
-                 */
-                return 0;
+                return null;
             }
         }
 
-        if (first instanceof IToken) {
-            return findIndex((IToken) first);
-        }
+        return (PositionedNode)first;
+    }
 
-        final PositionedNode node = (PositionedNode) first;
-        final SourcecodeRange item = this.getMapping().get(node);
-        if (item == null){
-            System.err.println(n.getClass().getSimpleName() + " / " + node.getClass().getSimpleName() + ": " + node);
+    private int findBeginPos(final PositionedNode node) {
+        if (node instanceof IToken) {
+            return findIndex((IToken) node);
         }
-        return item.getBeginIndex();
-    }
 
+        return this.getMapping().get(node).getBeginIndex();
+    }
 
-    private int findEndPos(final List<Object> list) {
+    private PositionedNode findEndNode(final List<Object> list) {
         Object last = list.get(list.size() - 1);
-        if (!(last instanceof PositionedNode) && !(last instanceof IToken)) {
+        if (last instanceof List<?>) {
             final List<?> list2 = (List<?>) last;
-            last = list2.get(list2.size() - 1);
+            return (PositionedNode)list2.get(list2.size() - 1);
         }
 
-        if (last instanceof IToken) {
-            return findIndex((IToken) last);
+        return (PositionedNode)last;
+    }
+
+    private int findEndPos(final PositionedNode node) {
+        if (node instanceof IToken) {
+            return findIndex((IToken) node);
         }
 
-        final PositionedNode node = (PositionedNode) last;
         final SourcecodeRange item = this.getMapping().get(node);
-        if (item == null)
+        if (item == null) {
             return -1;
+        }
         return item.getEndIndex();
     }
 
@@ -143,19 +152,6 @@ public class Parser implements IParser
         return -1;
     }
 
-    private SourcePosition createBeginPos(final int index) {
-        final List<IToken> list = this.lexer.getTokenList();
-        final IToken token = list.get(index);
-        return new SourcePosition(token.getLine(), token.getPos());
-    }
-
-    private SourcePosition createEndPos(final int index) {
-        final List<IToken> list = this.lexer.getTokenList();
-        final IToken token = list.get(index);
-        return new SourcePosition(token.getLine(), token.getPos()
-            + token.getText().length());
-    }
-
     /**
      * @deprecated Overriding this method no longer has any effect. This optimization is now applied automatically iff it is safe.
      */
-- 
GitLab