Don't generate verbose parser errors by default, but add a smali parameter to enable them

git-svn-id: https://smali.googlecode.com/svn/trunk@723 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
This commit is contained in:
JesusFreke@JesusFreke.com 2010-04-18 19:33:12 +00:00
parent 9f69ec12ea
commit 86df593d85
2 changed files with 56 additions and 6 deletions

View File

@ -135,14 +135,23 @@ import org.jf.dexlib.Code.Format.*;
@members {
private boolean verboseErrors = false;
public void setVerboseErrors(boolean verboseErrors) {
this.verboseErrors = verboseErrors;
}
public String getErrorMessage(RecognitionException e,
String[] tokenNames)
{
String[] tokenNames) {
if (!verboseErrors) {
return super.getErrorMessage(e, tokenNames);
}
List stack = getRuleInvocationStack(e, this.getClass().getName());
String msg = null;
if ( e instanceof NoViableAltException ) {
NoViableAltException nvae = (NoViableAltException)e;
msg = " no viable alt; token="+e.token+
msg = " no viable alt; token="+getTokenErrorDisplay(e.token)+
" (decision="+nvae.decisionNumber+
" state "+nvae.stateNumber+")"+
" decision=<<"+nvae.grammarDecisionDescription+">>";
@ -154,7 +163,38 @@ import org.jf.dexlib.Code.Format.*;
}
public String getTokenErrorDisplay(Token t) {
return t.toString();
if (!verboseErrors) {
String s = t.getText();
if ( s==null ) {
if ( t.getType()==Token.EOF ) {
s = "<EOF>";
}
else {
s = "<"+tokenNames[t.getType()]+">";
}
}
s = s.replaceAll("\n","\\\\n");
s = s.replaceAll("\r","\\\\r");
s = s.replaceAll("\t","\\\\t");
return "'"+s+"'";
}
CommonToken ct = (CommonToken)t;
String channelStr = "";
if (t.getChannel()>0) {
channelStr=",channel="+t.getChannel();
}
String txt = t.getText();
if ( txt!=null ) {
txt = txt.replaceAll("\n","\\\\n");
txt = txt.replaceAll("\r","\\\\r");
txt = txt.replaceAll("\t","\\\\t");
}
else {
txt = "<no text>";
}
return "[@"+t.getTokenIndex()+","+ct.getStartIndex()+":"+ct.getStopIndex()+"='"+txt+"',<"+tokenNames[t.getType()]+">"+channelStr+","+t.getLine()+":"+t.getCharPositionInLine()+"]";
}
public String getErrorHeader(RecognitionException e) {

View File

@ -97,6 +97,7 @@ public class main {
boolean sort = false;
boolean fixStringConst = true;
boolean fixGoto = true;
boolean verboseErrors = false;
String outputDexFile = "out.dex";
String dumpFileName = null;
@ -137,6 +138,9 @@ public class main {
case 'G':
fixGoto = false;
break;
case 'V':
verboseErrors = true;
break;
default:
assert false;
}
@ -169,7 +173,7 @@ public class main {
boolean errors = false;
for (File file: filesToProcess) {
if (!assembleSmaliFile(file, dexFile)) {
if (!assembleSmaliFile(file, dexFile, verboseErrors)) {
errors = true;
}
}
@ -245,7 +249,7 @@ public class main {
}
}
private static boolean assembleSmaliFile(File smaliFile, DexFile dexFile)
private static boolean assembleSmaliFile(File smaliFile, DexFile dexFile, boolean verboseErrors)
throws Exception {
ANTLRInputStream input = new ANTLRInputStream(new FileInputStream(smaliFile), "UTF8");
input.name = smaliFile.getAbsolutePath();
@ -254,6 +258,7 @@ public class main {
CommonTokenStream tokens = new CommonTokenStream(lexer);
smaliParser parser = new smaliParser(tokens);
parser.setVerboseErrors(verboseErrors);
smaliParser.smali_file_return result = parser.smali_file();
@ -346,6 +351,10 @@ public class main {
.withDescription("Don't replace goto type instructions with a larger version where appropriate")
.create("G");
Option verboseErrorsOption = OptionBuilder.withLongOpt("verbose-errors")
.withDescription("Generate verbose error messages")
.create("V");
basicOptions.addOption(versionOption);
basicOptions.addOption(helpOption);
basicOptions.addOption(outputOption);
@ -354,6 +363,7 @@ public class main {
debugOptions.addOption(sortOption);
debugOptions.addOption(noFixStringConstOption);
debugOptions.addOption(noFixGotoOption);
debugOptions.addOption(verboseErrorsOption);
for (Object option: basicOptions.getOptions()) {
options.addOption((Option)option);