From 0fbd4bb066d66b411a28a8cb90527cd1f822279d Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Mon, 25 Apr 2022 12:42:37 +0200 Subject: [PATCH] Improve exception handling in lexer/parser templates --- src/main/java/org/sablecc/sablecc/lexer/Lexer.java | 13 ++++++++----- .../java/org/sablecc/sablecc/parser/Parser.java | 14 +++++++++----- src/main/resources/org/sablecc/sablecc/lexer.txt | 13 ++++++++----- src/main/resources/org/sablecc/sablecc/parser.txt | 14 +++++++++----- 4 files changed, 34 insertions(+), 20 deletions(-) diff --git a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java index 9e9639c..d053187 100644 --- a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java +++ b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java @@ -1014,9 +1014,12 @@ public class Lexer implements ITokenListContainer { try { - DataInputStream s = new DataInputStream( - new BufferedInputStream( - Lexer.class.getResourceAsStream("lexer.dat"))); + InputStream resStream = Lexer.class.getResourceAsStream("lexer.dat"); + if(resStream == null) + { + throw new RuntimeException("The file \"lexer.dat\" is missing."); + } + DataInputStream s = new DataInputStream(new BufferedInputStream(resStream)); // read gotoTable int length = s.readInt(); @@ -1054,9 +1057,9 @@ public class Lexer implements ITokenListContainer s.close(); } - catch(Exception e) + catch(IOException e) { - throw new RuntimeException("The file \"lexer.dat\" is either missing or corrupted."); + throw new RuntimeException("The file \"lexer.dat\" is either missing or corrupted.", e); } } } diff --git a/src/main/java/org/sablecc/sablecc/parser/Parser.java b/src/main/java/org/sablecc/sablecc/parser/Parser.java index 201e986..f76e51c 100644 --- a/src/main/java/org/sablecc/sablecc/parser/Parser.java +++ b/src/main/java/org/sablecc/sablecc/parser/Parser.java @@ -11,6 +11,7 @@ import de.hhu.stups.sablecc.patch.*; import java.io.DataInputStream; import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; @SuppressWarnings({"rawtypes","unchecked","unused"}) @@ -10642,9 +10643,12 @@ public class Parser implements IParser { try { - DataInputStream s = new DataInputStream( - new BufferedInputStream( - Parser.class.getResourceAsStream("parser.dat"))); + InputStream resStream = Parser.class.getResourceAsStream("parser.dat"); + if(resStream == null) + { + throw new RuntimeException("The file \"parser.dat\" is missing."); + } + DataInputStream s = new DataInputStream(new BufferedInputStream(resStream)); // read actionTable int length = s.readInt(); @@ -10703,9 +10707,9 @@ public class Parser implements IParser s.close(); } - catch(Exception e) + catch(IOException e) { - throw new RuntimeException("The file \"parser.dat\" is either missing or corrupted."); + throw new RuntimeException("The file \"parser.dat\" is either missing or corrupted.", e); } } } diff --git a/src/main/resources/org/sablecc/sablecc/lexer.txt b/src/main/resources/org/sablecc/sablecc/lexer.txt index 061cec6..2922cf1 100644 --- a/src/main/resources/org/sablecc/sablecc/lexer.txt +++ b/src/main/resources/org/sablecc/sablecc/lexer.txt @@ -412,9 +412,12 @@ Macro:LexerTail { try { - DataInputStream s = new DataInputStream( - new BufferedInputStream( - Lexer.class.getResourceAsStream("lexer.dat"))); + InputStream resStream = Lexer.class.getResourceAsStream("lexer.dat"); + if(resStream == null) + { + throw new RuntimeException("The file \"lexer.dat\" is missing."); + } + DataInputStream s = new DataInputStream(new BufferedInputStream(resStream)); // read gotoTable int length = s.readInt(); @@ -452,9 +455,9 @@ Macro:LexerTail s.close(); } - catch(Exception e) + catch(IOException e) { - throw new RuntimeException("The file \"lexer.dat\" is either missing or corrupted."); + throw new RuntimeException("The file \"lexer.dat\" is either missing or corrupted.", e); } } } diff --git a/src/main/resources/org/sablecc/sablecc/parser.txt b/src/main/resources/org/sablecc/sablecc/parser.txt index 9873f83..862ac91 100644 --- a/src/main/resources/org/sablecc/sablecc/parser.txt +++ b/src/main/resources/org/sablecc/sablecc/parser.txt @@ -19,6 +19,7 @@ import de.hhu.stups.sablecc.patch.*; import java.io.DataInputStream; import java.io.BufferedInputStream; +import java.io.InputStream; import java.io.IOException; @SuppressWarnings({"rawtypes","unchecked","unused"}) @@ -504,9 +505,12 @@ Macro:ParserTail { try { - DataInputStream s = new DataInputStream( - new BufferedInputStream( - Parser.class.getResourceAsStream("parser.dat"))); + InputStream resStream = Parser.class.getResourceAsStream("parser.dat"); + if(resStream == null) + { + throw new RuntimeException("The file \"parser.dat\" is missing."); + } + DataInputStream s = new DataInputStream(new BufferedInputStream(resStream)); // read actionTable int length = s.readInt(); @@ -565,9 +569,9 @@ Macro:ParserTail s.close(); } - catch(Exception e) + catch(IOException e) { - throw new RuntimeException("The file \"parser.dat\" is either missing or corrupted."); + throw new RuntimeException("The file \"parser.dat\" is either missing or corrupted.", e); } } } -- GitLab