Add support for a new -T debugging option that prints out all the tokens

git-svn-id: https://smali.googlecode.com/svn/trunk@759 55b6fa8a-2a1e-11de-a435-ffa8d773f76a
diff --git a/smali/src/main/java/org/jf/smali/main.java b/smali/src/main/java/org/jf/smali/main.java
index 6422525..dd895a1 100644
--- a/smali/src/main/java/org/jf/smali/main.java
+++ b/smali/src/main/java/org/jf/smali/main.java
@@ -103,6 +103,7 @@
         boolean fixGoto = true;
         boolean verboseErrors = false;
         boolean oldLexer = false;
+        boolean printTokens = false;
 
         String outputDexFile = "out.dex";
         String dumpFileName = null;
@@ -149,6 +150,9 @@
                 case 'L':
                     oldLexer = true;
                     break;
+                case 'T':
+                    printTokens = true;
+                    break;
                 default:
                     assert false;
             }
@@ -181,7 +185,7 @@
             boolean errors = false;
 
             for (File file: filesToProcess) {
-                if (!assembleSmaliFile(file, dexFile, verboseErrors, oldLexer)) {
+                if (!assembleSmaliFile(file, dexFile, verboseErrors, oldLexer, printTokens)) {
                     errors = true;
                 }
             }
@@ -257,7 +261,8 @@
         }
     }
 
-    private static boolean assembleSmaliFile(File smaliFile, DexFile dexFile, boolean verboseErrors, boolean oldLexer)
+    private static boolean assembleSmaliFile(File smaliFile, DexFile dexFile, boolean verboseErrors, boolean oldLexer,
+                                             boolean printTokens)
             throws Exception {
         CommonTokenStream tokens;
 
@@ -280,6 +285,19 @@
             tokens = new CommonTokenStream((TokenSource)lexer);
         }
 
+        if (printTokens) {
+            tokens.getTokens();
+            
+            for (int i=0; i<tokens.size(); i++) {
+                Token token = tokens.get(i);
+                if (token.getChannel() == smaliLexer.HIDDEN) {
+                    continue;
+                }
+
+                System.out.println(smaliParser.tokenNames[token.getType()] + ": " + token.getText());
+            }
+        }
+
         smaliParser parser = new smaliParser(tokens);
         parser.setVerboseErrors(verboseErrors);
 
@@ -382,6 +400,9 @@
                 .withDescription("Use the old lexer")
                 .create("L");
 
+        Option printTokensOption = OptionBuilder.withLongOpt("print-tokens")
+                .withDescription("Print the name and text of each token")
+                .create("T");
 
         basicOptions.addOption(versionOption);
         basicOptions.addOption(helpOption);
@@ -393,6 +414,7 @@
         debugOptions.addOption(noFixGotoOption);
         debugOptions.addOption(verboseErrorsOption);
         debugOptions.addOption(oldLexerOption);
+        debugOptions.addOption(printTokensOption);
 
         for (Object option: basicOptions.getOptions()) {
             options.addOption((Option)option);