From 8c820269e4420381e53350e61278b3093828f587 Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Mon, 30 Jul 2012 19:24:20 -0500 Subject: [PATCH 1/2] Fix IntegerList.binarySearch --- runtime/Java/src/org/antlr/v4/runtime/misc/IntegerList.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/runtime/Java/src/org/antlr/v4/runtime/misc/IntegerList.java b/runtime/Java/src/org/antlr/v4/runtime/misc/IntegerList.java index 84388291b..21ae22656 100644 --- a/runtime/Java/src/org/antlr/v4/runtime/misc/IntegerList.java +++ b/runtime/Java/src/org/antlr/v4/runtime/misc/IntegerList.java @@ -259,10 +259,14 @@ public class IntegerList { } public final int binarySearch(int key) { - return Arrays.binarySearch(_data, key); + return Arrays.binarySearch(_data, 0, _size, key); } public final int binarySearch(int fromIndex, int toIndex, int key) { + if (fromIndex < 0 || toIndex < 0 || fromIndex > _size || toIndex > _size) { + throw new IndexOutOfBoundsException(); + } + return Arrays.binarySearch(_data, fromIndex, toIndex, key); } From 170a8347bb597535f565e18d8275f9afc6b830fc Mon Sep 17 00:00:00 2001 From: Sam Harwell Date: Tue, 31 Jul 2012 09:06:18 -0500 Subject: [PATCH 2/2] Emit a RangeTransition instead of SetTransition if a set has only one interval --- .../antlr/v4/automata/LexerATNFactory.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/tool/src/org/antlr/v4/automata/LexerATNFactory.java b/tool/src/org/antlr/v4/automata/LexerATNFactory.java index a383ea9f2..d908f601a 100644 --- a/tool/src/org/antlr/v4/automata/LexerATNFactory.java +++ b/tool/src/org/antlr/v4/automata/LexerATNFactory.java @@ -34,7 +34,17 @@ import org.antlr.v4.codegen.CodeGenerator; import org.antlr.v4.misc.CharSupport; import org.antlr.v4.parse.ANTLRParser; import org.antlr.v4.runtime.CharStream; -import org.antlr.v4.runtime.atn.*; +import org.antlr.v4.runtime.atn.ATN; +import org.antlr.v4.runtime.atn.ATNState; +import org.antlr.v4.runtime.atn.ActionTransition; +import org.antlr.v4.runtime.atn.AtomTransition; +import org.antlr.v4.runtime.atn.NotSetTransition; +import org.antlr.v4.runtime.atn.RangeTransition; +import org.antlr.v4.runtime.atn.RuleStartState; +import org.antlr.v4.runtime.atn.SetTransition; +import org.antlr.v4.runtime.atn.TokensStartState; +import org.antlr.v4.runtime.atn.Transition; +import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.LexerGrammar; @@ -178,7 +188,15 @@ public class LexerATNFactory extends ParserATNFactory { left.addTransition(new NotSetTransition(right, set)); } else { - left.addTransition(new SetTransition(right, set)); + Transition transition; + if (set.getIntervals().size() == 1) { + Interval interval = set.getIntervals().get(0); + transition = new RangeTransition(right, interval.a, interval.b); + } else { + transition = new SetTransition(right, set); + } + + left.addTransition(transition); } associatedAST.atnState = left; return new Handle(left, right);