Get rid of all (char) casts

This commit is contained in:
Ben Hamilton 2017-02-16 09:33:41 -08:00
parent 6c878b0709
commit 88ecb3d9fa
7 changed files with 53 additions and 21 deletions

View File

@ -34,6 +34,11 @@ public final class RangeTransition extends Transition {
@Override @Override
public String toString() { public String toString() {
return "'"+(char)from+"'..'"+(char)to+"'"; return new StringBuilder("'")
.appendCodePoint(from)
.append("'..'")
.appendCodePoint(to)
.append("'")
.toString();
} }
} }

View File

@ -16,6 +16,9 @@ public class LexerDFASerializer extends DFASerializer {
@Override @Override
protected String getEdgeLabel(int i) { protected String getEdgeLabel(int i) {
return "'"+(char)i+"'"; return new StringBuilder("'")
.appendCodePoint(i)
.append("'")
.toString();
} }
} }

View File

@ -505,11 +505,11 @@ public class IntervalSet implements IntSet {
int b = I.b; int b = I.b;
if ( a==b ) { if ( a==b ) {
if ( a==Token.EOF ) buf.append("<EOF>"); if ( a==Token.EOF ) buf.append("<EOF>");
else if ( elemAreChar ) buf.append("'").append((char)a).append("'"); else if ( elemAreChar ) buf.append("'").appendCodePoint(a).append("'");
else buf.append(a); else buf.append(a);
} }
else { else {
if ( elemAreChar ) buf.append("'").append((char)a).append("'..'").append((char)b).append("'"); if ( elemAreChar ) buf.append("'").appendCodePoint(a).append("'..'").appendCodePoint(b).append("'");
else buf.append(a).append("..").append(b); else buf.append(a).append("..").append(b);
} }
if ( iter.hasNext() ) { if ( iter.hasNext() ) {

View File

@ -18,6 +18,7 @@ import org.antlr.v4.runtime.atn.SetTransition;
import org.antlr.v4.runtime.atn.Transition; import org.antlr.v4.runtime.atn.Transition;
import org.antlr.v4.runtime.misc.Interval; import org.antlr.v4.runtime.misc.Interval;
import org.antlr.v4.runtime.misc.IntervalSet; import org.antlr.v4.runtime.misc.IntervalSet;
import org.antlr.v4.misc.CharSupport;
import org.antlr.v4.tool.ErrorType; import org.antlr.v4.tool.ErrorType;
import org.antlr.v4.tool.Grammar; import org.antlr.v4.tool.Grammar;
import org.antlr.v4.tool.Rule; import org.antlr.v4.tool.Rule;
@ -101,11 +102,11 @@ public class ATNOptimizer {
int maxElem = set.getMaxElement(); int maxElem = set.getMaxElement();
for (int k = minElem; k <= maxElem; k++) { for (int k = minElem; k <= maxElem; k++) {
if (matchSet.contains(k)) { if (matchSet.contains(k)) {
char setMin = (char) set.getMinElement();
char setMax = (char) set.getMaxElement();
// TODO: Token is missing (i.e. position in source will not be displayed). // TODO: Token is missing (i.e. position in source will not be displayed).
g.tool.errMgr.grammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName, g.tool.errMgr.grammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName,
null, (char) minElem + "-" + (char) maxElem, "[" + setMin + "-" + setMax + "]"); null,
CharSupport.toRange(minElem, maxElem, CharSupport.ToRangeMode.NOT_BRACKETED),
CharSupport.toRange(set.getMinElement(), set.getMaxElement(), CharSupport.ToRangeMode.BRACKETED));
break; break;
} }
} }

View File

@ -353,10 +353,12 @@ public class LexerATNFactory extends ParserATNFactory {
int n = chars.length(); int n = chars.length();
ATNState prev = left; ATNState prev = left;
right = null; right = null;
for (int i = 0; i < n; i++) { for (int i = 0; i < n; ) {
right = newState(stringLiteralAST); right = newState(stringLiteralAST);
prev.addTransition(new AtomTransition(right, chars.charAt(i))); int codePoint = chars.codePointAt(i);
prev.addTransition(new AtomTransition(right, codePoint));
prev = right; prev = right;
i += Character.charCount(codePoint);
} }
stringLiteralAST.atnState = left; stringLiteralAST.atnState = left;
return new Handle(left, right); return new Handle(left, right);
@ -394,30 +396,32 @@ public class LexerATNFactory extends ParserATNFactory {
} }
int n = chars.length(); int n = chars.length();
// now make x-y become set of char // now make x-y become set of char
for (int i = 0; i < n; i++) { for (int i = 0; i < n; ) {
int c = chars.charAt(i); int c = chars.codePointAt(i);
if (c == '\\' && i+1 < n && chars.charAt(i+1) == '-') { // \- int offset = Character.charCount(c);
if (c == '\\' && i+offset < n && chars.codePointAt(i+offset) == '-') { // \-
checkSetCollision(charSetAST, set, '-'); checkSetCollision(charSetAST, set, '-');
set.add('-'); set.add('-');
i++; offset++;
} }
else if (i+2 < n && chars.charAt(i+1) == '-') { // range x-y else if (i+offset+1 < n && chars.codePointAt(i+offset) == '-') { // range x-y
int x = c; int x = c;
int y = chars.charAt(i+2); int y = chars.codePointAt(i+offset+1);
if (x <= y) { if (x <= y) {
checkSetCollision(charSetAST, set, x, y); checkSetCollision(charSetAST, set, x, y);
set.add(x,y); set.add(x,y);
} }
else { else {
g.tool.errMgr.grammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED, g.tool.errMgr.grammarError(ErrorType.EMPTY_STRINGS_AND_SETS_NOT_ALLOWED,
g.fileName, charSetAST.getToken(), "[" + (char) x + "-" + (char) y + "]"); g.fileName, charSetAST.getToken(), CharSupport.toRange(x, y, CharSupport.ToRangeMode.BRACKETED));
} }
i += 2; offset += Character.charCount(y) + 1;
} }
else { else {
checkSetCollision(charSetAST, set, c); checkSetCollision(charSetAST, set, c);
set.add(c); set.add(c);
} }
i += offset;
} }
return set; return set;
} }
@ -425,7 +429,7 @@ public class LexerATNFactory extends ParserATNFactory {
protected void checkSetCollision(GrammarAST ast, IntervalSet set, int el) { protected void checkSetCollision(GrammarAST ast, IntervalSet set, int el) {
if (set.contains(el)) { if (set.contains(el)) {
g.tool.errMgr.grammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName, ast.getToken(), g.tool.errMgr.grammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName, ast.getToken(),
(char)el, ast.getText()); el, ast.getText());
} }
} }
@ -453,7 +457,7 @@ public class LexerATNFactory extends ParserATNFactory {
setText = sb.toString(); setText = sb.toString();
} }
g.tool.errMgr.grammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName, ast.getToken(), g.tool.errMgr.grammarError(ErrorType.CHARACTERS_COLLISION_IN_SET, g.fileName, ast.getToken(),
(char)a + "-" + (char)b, setText); CharSupport.toRange(a, b, CharSupport.ToRangeMode.NOT_BRACKETED), setText);
break; break;
} }
} }

View File

@ -19,6 +19,11 @@ public class CharSupport {
*/ */
public static String ANTLRLiteralCharValueEscape[] = new String[255]; public static String ANTLRLiteralCharValueEscape[] = new String[255];
public enum ToRangeMode {
BRACKETED,
NOT_BRACKETED,
};
static { static {
ANTLRLiteralEscapedCharValue['n'] = '\n'; ANTLRLiteralEscapedCharValue['n'] = '\n';
ANTLRLiteralEscapedCharValue['r'] = '\r'; ANTLRLiteralEscapedCharValue['r'] = '\r';
@ -143,4 +148,18 @@ public class CharSupport {
public static String capitalize(String s) { public static String capitalize(String s) {
return Character.toUpperCase(s.charAt(0)) + s.substring(1); return Character.toUpperCase(s.charAt(0)) + s.substring(1);
} }
public static String toRange(int codePointStart, int codePointEnd, ToRangeMode mode) {
StringBuilder sb = new StringBuilder();
if (mode == ToRangeMode.BRACKETED) {
sb.append("[");
}
sb.appendCodePoint(codePointStart)
.append("-")
.appendCodePoint(codePointEnd);
if (mode == ToRangeMode.BRACKETED) {
sb.append("]");
}
return sb.toString();
}
} }

View File

@ -92,7 +92,7 @@ public class DOTGenerator {
if ( target.stateNumber == Integer.MAX_VALUE ) continue; if ( target.stateNumber == Integer.MAX_VALUE ) continue;
int ttype = i-1; // we shift up for EOF as -1 for parser int ttype = i-1; // we shift up for EOF as -1 for parser
String label = String.valueOf(ttype); String label = String.valueOf(ttype);
if ( isLexer ) label = "'"+getEdgeLabel(String.valueOf((char) i))+"'"; if ( isLexer ) label = "'"+getEdgeLabel(new StringBuilder().appendCodePoint(i).toString())+"'";
else if ( grammar!=null ) label = grammar.getTokenDisplayName(ttype); else if ( grammar!=null ) label = grammar.getTokenDisplayName(ttype);
ST st = stlib.getInstanceOf("edge"); ST st = stlib.getInstanceOf("edge");
st.add("label", label); st.add("label", label);
@ -259,7 +259,7 @@ public class DOTGenerator {
edgeST = stlib.getInstanceOf("edge"); edgeST = stlib.getInstanceOf("edge");
AtomTransition atom = (AtomTransition)edge; AtomTransition atom = (AtomTransition)edge;
String label = String.valueOf(atom.label); String label = String.valueOf(atom.label);
if ( isLexer ) label = "'"+getEdgeLabel(String.valueOf((char)atom.label))+"'"; if ( isLexer ) label = "'"+getEdgeLabel(new StringBuilder().appendCodePoint(atom.label).toString())+"'";
else if ( grammar!=null ) label = grammar.getTokenDisplayName(atom.label); else if ( grammar!=null ) label = grammar.getTokenDisplayName(atom.label);
edgeST.add("label", getEdgeLabel(label)); edgeST.add("label", getEdgeLabel(label));
} }