stub out parser for asm files.  Change invariant on lexer to always
print its error message when it returns an asmtok::Error token.
Compute a proper error code for llvm-mc in 'lex' mode.  Add new
-as-lex option to enable lexing mode (vs parsing mode).


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73859 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/tools/llvm-mc/llvm-mc.cpp b/tools/llvm-mc/llvm-mc.cpp
index 7a179e8..0acd78e 100644
--- a/tools/llvm-mc/llvm-mc.cpp
+++ b/tools/llvm-mc/llvm-mc.cpp
@@ -19,7 +19,7 @@
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/System/Signals.h"
-#include "AsmLexer.h"
+#include "AsmParser.h"
 using namespace llvm;
 
 static cl::opt<std::string>
@@ -34,16 +34,20 @@
             cl::value_desc("directory"), cl::Prefix);
 
 enum ActionType {
+  AC_AsLex,
   AC_Assemble
 };
 
 static cl::opt<ActionType>
 Action(cl::desc("Action to perform:"),
-       cl::values(clEnumValN(AC_Assemble, "assemble",
+       cl::init(AC_Assemble),
+       cl::values(clEnumValN(AC_AsLex, "as-lex",
+                             "Lex tokens from a .s file"),
+                  clEnumValN(AC_Assemble, "assemble",
                              "Assemble a .s file (default)"),
                   clEnumValEnd));
 
-static int AssembleInput(const char *ProgName) {
+static int AsLexInput(const char *ProgName) {
   std::string ErrorMessage;
   MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
                                                       &ErrorMessage);
@@ -65,16 +69,19 @@
   // it later.
   SrcMgr.setIncludeDirs(IncludeDirs);
 
-  
-  
   AsmLexer Lexer(SrcMgr);
   
+  bool Error = false;
+  
   asmtok::TokKind Tok = Lexer.Lex();
   while (Tok != asmtok::Eof) {
     switch (Tok) {
-    default: Lexer.PrintError(Lexer.getLoc(), "driver: unknown token"); break;
+    default:
+      Lexer.PrintError(Lexer.getLoc(), "driver: unknown token");
+      Error = true;
+      break;
     case asmtok::Error:
-      Lexer.PrintError(Lexer.getLoc(), "error, bad token");
+      Error = true; // error already printed.
       break;
     case asmtok::Identifier:
       outs() << "identifier: " << Lexer.getCurStrVal() << '\n';
@@ -103,9 +110,35 @@
     Tok = Lexer.Lex();
   }
   
-  return 1;
+  return Error;
 }
 
+static int AssembleInput(const char *ProgName) {
+  std::string ErrorMessage;
+  MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFilename,
+                                                      &ErrorMessage);
+  if (Buffer == 0) {
+    errs() << ProgName << ": ";
+    if (ErrorMessage.size())
+      errs() << ErrorMessage << "\n";
+    else
+      errs() << "input file didn't read correctly.\n";
+    return 1;
+  }
+  
+  SourceMgr SrcMgr;
+  
+  // Tell SrcMgr about this buffer, which is what TGParser will pick up.
+  SrcMgr.AddNewSourceBuffer(Buffer, SMLoc());
+  
+  // Record the location of the include directories so that the lexer can find
+  // it later.
+  SrcMgr.setIncludeDirs(IncludeDirs);
+  
+  AsmParser Parser(SrcMgr);
+  return Parser.Run();
+}  
+
 
 int main(int argc, char **argv) {
   // Print a stack trace if we signal out.
@@ -116,6 +149,8 @@
 
   switch (Action) {
   default:
+  case AC_AsLex:
+    return AsLexInput(argv[0]);
   case AC_Assemble:
     return AssembleInput(argv[0]);
   }