From f618d67a5fda8e94917c0b0717e9f81f94c40b45 Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Fri, 10 May 2024 15:58:03 +0200
Subject: [PATCH] Pull common code out of generated Lexer accept switch cases

---
 .../java/org/sablecc/sablecc/lexer/Lexer.java | 164 +-----------------
 .../resources/org/sablecc/sablecc/lexer.txt   |  12 +-
 2 files changed, 8 insertions(+), 168 deletions(-)

diff --git a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java
index f397d6c..69e7501 100644
--- a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java
+++ b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java
@@ -179,10 +179,6 @@ public class Lexer
                     {
                         case 0:
                             tok = new TPkgId(getText(acceptLength), startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             switch(state.ordinal())
                             {
                                 case 1: state = State.PACKAGE; break;
@@ -191,10 +187,6 @@ public class Lexer
 
                         case 1:
                             tok = new TPackage(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             switch(state.ordinal())
                             {
                                 case 0: state = State.PACKAGE; break;
@@ -203,122 +195,62 @@ public class Lexer
 
                         case 2:
                             tok = new TStates(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 3:
                             tok = new THelpers(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 4:
                             tok = new TTokens(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 5:
                             tok = new TIgnored(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 6:
                             tok = new TProductions(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 7:
                             tok = new TAbstract(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 8:
                             tok = new TSyntax(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 9:
                             tok = new TTree(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 10:
                             tok = new TNew(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 11:
                             tok = new TNull(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 12:
                             tok = new TTokenSpecifier(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 13:
                             tok = new TProductionSpecifier(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 14:
                             tok = new TDot(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 15:
                             tok = new TDDot(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 16:
                             tok = new TSemicolon(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             switch(state.ordinal())
                             {
                                 case 0: state = State.NORMAL; break;
@@ -328,191 +260,103 @@ public class Lexer
 
                         case 17:
                             tok = new TEqual(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 18:
                             tok = new TLBkt(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 19:
                             tok = new TRBkt(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 20:
                             tok = new TLPar(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 21:
                             tok = new TRPar(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 22:
                             tok = new TLBrace(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 23:
                             tok = new TRBrace(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 24:
                             tok = new TPlus(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 25:
                             tok = new TMinus(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 26:
                             tok = new TQMark(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 27:
                             tok = new TStar(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 28:
                             tok = new TBar(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 29:
                             tok = new TComma(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 30:
                             tok = new TSlash(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 31:
                             tok = new TArrow(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 32:
                             tok = new TColon(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 33:
                             tok = new TId(getText(acceptLength), startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 34:
                             tok = new TChar(getText(acceptLength), startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 35:
                             tok = new TDecChar(getText(acceptLength), startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 36:
                             tok = new THexChar(getText(acceptLength), startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 37:
                             tok = new TString(getText(acceptLength), startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 38:
                             tok = new TBlank(getText(acceptLength), startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         case 39:
                             tok = new TComment(getText(acceptLength), startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
                             break;
 
                         default:
                             throw new LexerException("[" + (startLine + 1) + "," + (startPos + 1) + "] Internal lexer error: invalid accept table entry " + acceptToken + ", current text: " + this.text);
                     }
+                    pushBack(acceptLength);
+                    this.pos = acceptPos;
+                    this.line = acceptLine;
+                    this.cr = acceptCr;
                     return tok;
                 }
                 else
diff --git a/src/main/resources/org/sablecc/sablecc/lexer.txt b/src/main/resources/org/sablecc/sablecc/lexer.txt
index d9e3e82..7ca66ca 100644
--- a/src/main/resources/org/sablecc/sablecc/lexer.txt
+++ b/src/main/resources/org/sablecc/sablecc/lexer.txt
@@ -207,20 +207,12 @@ $
 Macro:LexerVariableToken
                         case $0$:
                             tok = new $1$(getText(acceptLength), startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
 
 $
 
 Macro:LexerFixedToken
                         case $0$:
                             tok = new $1$(startLine + 1, startPos + 1);
-                            pushBack(acceptLength);
-                            this.pos = acceptPos;
-                            this.line = acceptLine;
-                            this.cr = acceptCr;
 
 $
 
@@ -250,6 +242,10 @@ Macro:LexerBody
                         default:
                             throw new LexerException("[" + (startLine + 1) + "," + (startPos + 1) + "] Internal lexer error: invalid accept table entry " + acceptToken + ", current text: " + this.text);
                     }
+                    pushBack(acceptLength);
+                    this.pos = acceptPos;
+                    this.line = acceptLine;
+                    this.cr = acceptCr;
                     return tok;
                 }
                 else
-- 
GitLab