From 7e1e5ead82d9f749d4e9fbccb9b1bee9110fbac3 Mon Sep 17 00:00:00 2001 From: dgelessus <dgelessus@users.noreply.github.com> Date: Tue, 4 Jul 2023 15:28:56 +0200 Subject: [PATCH] Make generated Lexer.State an enum Mostly so that the values have readable toString representations. --- .../java/org/sablecc/sablecc/GenLexer.java | 10 +++---- .../java/org/sablecc/sablecc/lexer/Lexer.java | 26 +++++++------------ .../resources/org/sablecc/sablecc/lexer.txt | 20 +++++--------- 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/src/main/java/org/sablecc/sablecc/GenLexer.java b/src/main/java/org/sablecc/sablecc/GenLexer.java index d4b8e53..03f2e07 100644 --- a/src/main/java/org/sablecc/sablecc/GenLexer.java +++ b/src/main/java/org/sablecc/sablecc/GenLexer.java @@ -286,18 +286,14 @@ public class GenLexer extends AnalysisAdapter if(ids.stateList.size() > 0) { - for(ListIterator<String> i = ids.stateList.listIterator(); i.hasNext();) + for (String s : ids.stateList) { - String s = i.next(); - - macros.apply(file, "LexerStateBody", - new String[] {s, "" + i.previousIndex()}); + macros.apply(file, "LexerStateBody", new String[] {s}); } } else { - macros.apply(file, "LexerStateBody", - new String[] {"INITIAL", "" + 0}); + macros.apply(file, "LexerStateBody", new String[] {"INITIAL"}); } macros.apply(file, "LexerStateTail"); diff --git a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java index 78892eb..9a4ac88 100644 --- a/src/main/java/org/sablecc/sablecc/lexer/Lexer.java +++ b/src/main/java/org/sablecc/sablecc/lexer/Lexer.java @@ -110,8 +110,8 @@ public class Lexer implements ITokenListContainer int accept_pos = -1; int accept_line = -1; - int[][][] gotoTable = Lexer.gotoTable[this.state.id()]; - int[] accept = Lexer.accept[this.state.id()]; + int[][][] gotoTable = Lexer.gotoTable[this.state.ordinal()]; + int[] accept = Lexer.accept[this.state.ordinal()]; this.text.setLength(0); while(true) @@ -212,7 +212,7 @@ public class Lexer implements ITokenListContainer pushBack(accept_length); this.pos = accept_pos; this.line = accept_line; - switch(state.id()) + switch(state.ordinal()) { case 1: state = State.PACKAGE; break; } @@ -226,7 +226,7 @@ public class Lexer implements ITokenListContainer pushBack(accept_length); this.pos = accept_pos; this.line = accept_line; - switch(state.id()) + switch(state.ordinal()) { case 0: state = State.PACKAGE; break; } @@ -380,7 +380,7 @@ public class Lexer implements ITokenListContainer pushBack(accept_length); this.pos = accept_pos; this.line = accept_line; - switch(state.id()) + switch(state.ordinal()) { case 0: state = State.NORMAL; break; case 1: state = State.NORMAL; break; @@ -995,21 +995,15 @@ public class Lexer implements ITokenListContainer };*/ - public static class State + public enum State { - public final static State NORMAL = new State(0); - public final static State PACKAGE = new State(1); - - private int id; - - private State(int id) - { - this.id = id; - } + NORMAL, + PACKAGE, + ; public int id() { - return this.id; + return this.ordinal(); } } diff --git a/src/main/resources/org/sablecc/sablecc/lexer.txt b/src/main/resources/org/sablecc/sablecc/lexer.txt index 2b41b6c..44e22ae 100644 --- a/src/main/resources/org/sablecc/sablecc/lexer.txt +++ b/src/main/resources/org/sablecc/sablecc/lexer.txt @@ -134,8 +134,8 @@ public class Lexer implements ITokenListContainer int accept_pos = -1; int accept_line = -1; - int[][][] gotoTable = Lexer.gotoTable[this.state.id()]; - int[] accept = Lexer.accept[this.state.id()]; + int[][][] gotoTable = Lexer.gotoTable[this.state.ordinal()]; + int[] accept = Lexer.accept[this.state.ordinal()]; this.text.setLength(0); while(true) @@ -256,7 +256,7 @@ Macro:LexerFixedToken $ Macro:TokenSwitchHeader - switch(state.id()) + switch(state.ordinal()) { $ @@ -382,28 +382,22 @@ Macro:LexerAcceptTail $ Macro:LexerStateHeader - public static class State + public enum State { $ Macro:LexerStateBody - public final static State $0$ = new State($1$); + $0$, $ Macro:LexerStateTail - - private int id; - - private State(int id) - { - this.id = id; - } + ; public int id() { - return this.id; + return this.ordinal(); } } -- GitLab