From 93424a0885e4a24772e45694c477e920079f6325 Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Wed, 18 Sep 2024 13:06:22 +0200 Subject: [PATCH] Allow getting structured position info and message from LexerException --- .../java/org/sablecc/sablecc/lexer/Lexer.java | 4 +-- .../sablecc/sablecc/lexer/LexerException.java | 29 +++++++++++++++- .../resources/org/sablecc/sablecc/lexer.txt | 33 +++++++++++++++++-- 3 files changed, 60 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java index c38142a..699b889 100644 --- a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java +++ b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java @@ -176,7 +176,7 @@ public class Lexer case -1: if(this.text.length() > 0) { - throw new LexerException("[" + (startLine + 1) + "," + (startPos + 1) + "] Unknown token: " + this.text); + throw new LexerException(startLine + 1, startPos + 1, "Unknown token: " + this.text); } return new EOF(startLine + 1, startPos + 1); @@ -350,7 +350,7 @@ public class Lexer break; default: - throw new LexerException("[" + (startLine + 1) + "," + (startPos + 1) + "] Internal lexer error: invalid accept table entry " + acceptToken + ", current text: " + this.text); + throw new LexerException(startLine + 1, startPos + 1, "Internal lexer error: invalid accept table entry " + acceptToken + ", current text: " + this.text); } pushBack(acceptLength); this.pos = acceptPos; diff --git a/src/main/java/org/sablecc/sablecc/lexer/LexerException.java b/src/main/java/org/sablecc/sablecc/lexer/LexerException.java index 9ae73de..ff4967e 100644 --- a/src/main/java/org/sablecc/sablecc/lexer/LexerException.java +++ b/src/main/java/org/sablecc/sablecc/lexer/LexerException.java @@ -5,8 +5,35 @@ package org.sablecc.sablecc.lexer; @SuppressWarnings({"serial"}) public class LexerException extends Exception { + private final int line; + private final int pos; + private final String realMsg; + + public LexerException(int line, int pos, String message) + { + super("[" + line + "," + pos + "] " + message); + this.line = line; + this.pos = pos; + this.realMsg = message; + } + public LexerException(String message) { - super(message); + this(0, 0, message); + } + + public int getLine() + { + return this.line; + } + + public int getPos() + { + return this.pos; + } + + public String getRealMsg() + { + return this.realMsg; } } diff --git a/src/main/resources/org/sablecc/sablecc/lexer.txt b/src/main/resources/org/sablecc/sablecc/lexer.txt index c877b18..aa9b584 100644 --- a/src/main/resources/org/sablecc/sablecc/lexer.txt +++ b/src/main/resources/org/sablecc/sablecc/lexer.txt @@ -13,9 +13,36 @@ package $0$lexer; @SuppressWarnings({"serial"}) public class LexerException extends Exception { + private final int line; + private final int pos; + private final String realMsg; + + public LexerException(int line, int pos, String message) + { + super("[" + line + "," + pos + "] " + message); + this.line = line; + this.pos = pos; + this.realMsg = message; + } + public LexerException(String message) { - super(message); + this(0, 0, message); + } + + public int getLine() + { + return this.line; + } + + public int getPos() + { + return this.pos; + } + + public String getRealMsg() + { + return this.realMsg; } } @@ -200,7 +227,7 @@ public class Lexer case -1: if(this.text.length() > 0) { - throw new LexerException("[" + (startLine + 1) + "," + (startPos + 1) + "] Unknown token: " + this.text); + throw new LexerException(startLine + 1, startPos + 1, "Unknown token: " + this.text); } return new EOF(startLine + 1, startPos + 1); @@ -244,7 +271,7 @@ $ Macro:LexerBody default: - throw new LexerException("[" + (startLine + 1) + "," + (startPos + 1) + "] Internal lexer error: invalid accept table entry " + acceptToken + ", current text: " + this.text); + throw new LexerException(startLine + 1, startPos + 1, "Internal lexer error: invalid accept table entry " + acceptToken + ", current text: " + this.text); } pushBack(acceptLength); this.pos = acceptPos; -- GitLab