From 8ad5bb4c0133d4077dbdcd8451c2cb6301e23bf5 Mon Sep 17 00:00:00 2001
From: dgelessus <dgelessus@users.noreply.github.com>
Date: Tue, 28 May 2024 14:18:09 +0200
Subject: [PATCH] Un-deprecate generated Lexer.unread and instead detect
 problematic case

Our parsers actually do use this method in a few cases that aren't easy
to replace.
---
 src/main/java/org/sablecc/sablecc/lexer/Lexer.java | 8 ++++----
 src/main/resources/org/sablecc/sablecc/lexer.txt   | 8 ++++----
 2 files changed, 8 insertions(+), 8 deletions(-)

diff --git a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java
index f8787a2..438b84d 100644
--- a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java
+++ b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java
@@ -390,17 +390,17 @@ public class Lexer
     }
 
     /**
-     * @deprecated This method doesn't restore line/column info correctly in some cases
-     *     (if a token begins in the middle of a CRLF newline pair).
-     *     There is no planned replacement.
      * @param tok the token to push back onto the input
      * @throws IOException when thrown by {@link PushbackReader#unread(int)}
      */
-    @Deprecated
     protected void unread(Token tok) throws IOException
     {
         String tokenText = tok.getText();
         int length = tokenText.length();
+        if(this.cr || (length > 0 && tokenText.charAt(0) == '\n'))
+        {
+            throw new IOException("Cannot unread a token containing a partial newline");
+        }
 
         for(int i = length - 1; i >= 0; i--)
         {
diff --git a/src/main/resources/org/sablecc/sablecc/lexer.txt b/src/main/resources/org/sablecc/sablecc/lexer.txt
index d070d6e..1577bea 100644
--- a/src/main/resources/org/sablecc/sablecc/lexer.txt
+++ b/src/main/resources/org/sablecc/sablecc/lexer.txt
@@ -284,17 +284,17 @@ Macro:LexerBody
     }
 
     /**
-     * @deprecated This method doesn't restore line/column info correctly in some cases
-     *     (if a token begins in the middle of a CRLF newline pair).
-     *     There is no planned replacement.
      * @param tok the token to push back onto the input
      * @throws IOException when thrown by {@link PushbackReader#unread(int)}
      */
-    @Deprecated
     protected void unread(Token tok) throws IOException
     {
         String tokenText = tok.getText();
         int length = tokenText.length();
+        if(this.cr || (length > 0 && tokenText.charAt(0) == '\n'))
+        {
+            throw new IOException("Cannot unread a token containing a partial newline");
+        }
 
         for(int i = length - 1; i >= 0; i--)
         {
-- 
GitLab