From 86df593d85576290a631907f19fcab2656467bdf Mon Sep 17 00:00:00 2001 From: "JesusFreke@JesusFreke.com" Date: Sun, 18 Apr 2010 19:33:12 +0000 Subject: [PATCH] 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 --- .../main/antlr3/org/jf/smali/smaliParser.g | 48 +++++++++++++++++-- smali/src/main/java/org/jf/smali/main.java | 14 +++++- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/smali/src/main/antlr3/org/jf/smali/smaliParser.g b/smali/src/main/antlr3/org/jf/smali/smaliParser.g index d909f50f..c523ba7e 100644 --- a/smali/src/main/antlr3/org/jf/smali/smaliParser.g +++ b/smali/src/main/antlr3/org/jf/smali/smaliParser.g @@ -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 = ""; + } + 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 = ""; + } + return "[@"+t.getTokenIndex()+","+ct.getStartIndex()+":"+ct.getStopIndex()+"='"+txt+"',<"+tokenNames[t.getType()]+">"+channelStr+","+t.getLine()+":"+t.getCharPositionInLine()+"]"; } public String getErrorHeader(RecognitionException e) { diff --git a/smali/src/main/java/org/jf/smali/main.java b/smali/src/main/java/org/jf/smali/main.java index 38f7f196..5a084dff 100644 --- a/smali/src/main/java/org/jf/smali/main.java +++ b/smali/src/main/java/org/jf/smali/main.java @@ -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);