From a2e63b46161780e939911d0e64d73b5ae7a3244d Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Fri, 10 Nov 2023 15:28:50 +0100
Subject: [PATCH] Restore Lexer line counting state correctly in middle of a
 CRLF pair

---
 .../java/org/sablecc/sablecc/lexer/Lexer.java | 42 +++++++++++++++++++
 .../resources/org/sablecc/sablecc/lexer.txt   |  4 ++
 2 files changed, 46 insertions(+)

diff --git a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java
index 47769e0..f2f11b9 100644
--- a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java
+++ b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java
@@ -66,6 +66,7 @@ public class Lexer
         int acceptLength = -1;
         int acceptPos = -1;
         int acceptLine = -1;
+        boolean acceptCr = false;
 
         int[][][] gotoTable = Lexer.gotoTable[this.state.ordinal()];
         int[] accept = Lexer.accept[this.state.ordinal()];
@@ -166,6 +167,7 @@ public class Lexer
                     acceptLength = this.text.length();
                     acceptPos = this.pos;
                     acceptLine = this.line;
+                    acceptCr = this.cr;
                 }
             }
             else
@@ -180,6 +182,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             switch(state.ordinal())
                             {
                                 case 1: state = State.PACKAGE; break;
@@ -192,6 +195,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             switch(state.ordinal())
                             {
                                 case 0: state = State.PACKAGE; break;
@@ -204,6 +208,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 3:
@@ -212,6 +217,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 4:
@@ -220,6 +226,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 5:
@@ -228,6 +235,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 6:
@@ -236,6 +244,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 7:
@@ -244,6 +253,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 8:
@@ -252,6 +262,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 9:
@@ -260,6 +271,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 10:
@@ -268,6 +280,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 11:
@@ -276,6 +289,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 12:
@@ -284,6 +298,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 13:
@@ -292,6 +307,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 14:
@@ -300,6 +316,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 15:
@@ -308,6 +325,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 16:
@@ -316,6 +334,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             switch(state.ordinal())
                             {
                                 case 0: state = State.NORMAL; break;
@@ -329,6 +348,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 18:
@@ -337,6 +357,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 19:
@@ -345,6 +366,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 20:
@@ -353,6 +375,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 21:
@@ -361,6 +384,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 22:
@@ -369,6 +393,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 23:
@@ -377,6 +402,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 24:
@@ -385,6 +411,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 25:
@@ -393,6 +420,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 26:
@@ -401,6 +429,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 27:
@@ -409,6 +438,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 28:
@@ -417,6 +447,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 29:
@@ -425,6 +456,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 30:
@@ -433,6 +465,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 31:
@@ -441,6 +474,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 32:
@@ -449,6 +483,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 33:
@@ -457,6 +492,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 34:
@@ -465,6 +501,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 35:
@@ -473,6 +510,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 36:
@@ -481,6 +519,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 37:
@@ -489,6 +528,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 38:
@@ -497,6 +537,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                         case 39:
@@ -505,6 +546,7 @@ public class Lexer
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
                             return token;
                         }
                     }
diff --git a/src/main/resources/org/sablecc/sablecc/lexer.txt b/src/main/resources/org/sablecc/sablecc/lexer.txt
index c082f57..95e85f0 100644
--- a/src/main/resources/org/sablecc/sablecc/lexer.txt
+++ b/src/main/resources/org/sablecc/sablecc/lexer.txt
@@ -90,6 +90,7 @@ public class Lexer
         int acceptLength = -1;
         int acceptPos = -1;
         int acceptLine = -1;
+        boolean acceptCr = false;
 
         int[][][] gotoTable = Lexer.gotoTable[this.state.ordinal()];
         int[] accept = Lexer.accept[this.state.ordinal()];
@@ -190,6 +191,7 @@ public class Lexer
                     acceptLength = this.text.length();
                     acceptPos = this.pos;
                     acceptLine = this.line;
+                    acceptCr = this.cr;
                 }
             }
             else
@@ -208,6 +210,7 @@ Macro:LexerVariableToken
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
 
 $
 
@@ -218,6 +221,7 @@ Macro:LexerFixedToken
                             pushBack(acceptLength);
                             this.pos = acceptPos;
                             this.line = acceptLine;
+                            this.cr = acceptCr;
 
 $
 
-- 
GitLab