Fix #1073. Better token output.

This commit is contained in:
parrt 2016-12-10 14:34:50 -08:00
parent 0a7e90fe8e
commit e65154c161
2 changed files with 19 additions and 3 deletions

View File

@ -258,6 +258,11 @@ public class CommonToken implements WritableToken, Serializable {
@Override
public String toString() {
return toString(null);
}
public String toString(Recognizer r) {
String channelStr = "";
if ( channel>0 ) {
channelStr=",channel="+channel;
@ -271,6 +276,10 @@ public class CommonToken implements WritableToken, Serializable {
else {
txt = "<no text>";
}
return "[@"+getTokenIndex()+","+start+":"+stop+"='"+txt+"',<"+type+">"+channelStr+","+line+":"+getCharPositionInLine()+"]";
String typeString = String.valueOf(type);
if ( r!=null ) {
typeString = r.getVocabulary().getDisplayName(type);
}
return "[@"+getTokenIndex()+","+start+":"+stop+"='"+txt+"',<"+typeString+">"+channelStr+","+line+":"+getCharPositionInLine()+"]";
}
}

View File

@ -8,11 +8,13 @@ package org.antlr.v4.gui;
import org.antlr.v4.runtime.ANTLRInputStream;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.DiagnosticErrorListener;
import org.antlr.v4.runtime.Lexer;
import org.antlr.v4.runtime.Parser;
import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.TokenStream;
import org.antlr.v4.runtime.atn.PredictionMode;
@ -194,8 +196,13 @@ public class TestRig {
tokens.fill();
if ( showTokens ) {
for (Object tok : tokens.getTokens()) {
System.out.println(tok);
for (Token tok : tokens.getTokens()) {
if ( tok instanceof CommonToken ) {
System.out.println(((CommonToken)tok).toString(lexer));
}
else {
System.out.println(tok.toString());
}
}
}