set up the top-level parsing loop.
authorChris Lattner <sabre@nondot.org>
Sun, 21 Jun 2009 20:54:55 +0000 (20:54 +0000)
committerChris Lattner <sabre@nondot.org>
Sun, 21 Jun 2009 20:54:55 +0000 (20:54 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@73860 91177308-0d34-0410-b5e6-96231b3b80d8

tools/llvm-mc/AsmLexer.h
tools/llvm-mc/AsmParser.cpp
tools/llvm-mc/AsmParser.h

index c5d1722e491db90e06932e76286f55791b8c1d6f..c668e8639cdc5b81a8e6fd8e8af69ae3e51e9040 100644 (file)
@@ -73,6 +73,8 @@ public:
   }
   
   asmtok::TokKind getKind() const { return CurKind; }
+  bool is(asmtok::TokKind K) const { return CurKind == K; }
+  bool isNot(asmtok::TokKind K) const { return CurKind != K; }
   
   const std::string &getCurStrVal() const {
     assert((CurKind == asmtok::Identifier || CurKind == asmtok::Register ||
index abb0d35b0865634c6aa62756ec680120d70ce541..9c0b4abb076b057ed1c03d3098871403d9463fbc 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "AsmParser.h"
+#include "llvm/Support/SourceMgr.h"
+#include "llvm/Support/raw_ostream.h"
 using namespace llvm;
 
 bool AsmParser::Run() {
+  // Prime the lexer.
+  Lexer.Lex();
+  
+  while (Lexer.isNot(asmtok::Eof))
+    if (ParseStatement())
+      return true;
+  
+  return false;
+}
+
+
+/// ParseStatement:
+///   ::= EndOfStatement
+///   ::= Label* Identifier Operands* EndOfStatement
+bool AsmParser::ParseStatement() {
+  switch (Lexer.getKind()) {
+  default:
+    Lexer.PrintError(Lexer.getLoc(), "unexpected token at start of statement");
+    return true;
+  case asmtok::EndOfStatement:
+    Lexer.Lex();
+    return false;
+  case asmtok::Identifier:
+    break;
+  // TODO: Recurse on local labels etc.
+  }
+  
+  // If we have an identifier, handle it as the key symbol.
+  //SMLoc IDLoc = Lexer.getLoc();
+  std::string IDVal = Lexer.getCurStrVal();
+  
+  // Consume the identifier, see what is after it.
+  if (Lexer.Lex() == asmtok::Colon) {
+    // identifier ':'   -> Label.
+    Lexer.Lex();
+    return ParseStatement();
+  }
+  
+  // Otherwise, we have a normal instruction or directive.  
+  if (IDVal[0] == '.')
+    outs() << "Found directive: " << IDVal << "\n";
+  else
+    outs() << "Found instruction: " << IDVal << "\n";
+
+  // Skip to end of line for now.
+  while (Lexer.isNot(asmtok::EndOfStatement) &&
+         Lexer.isNot(asmtok::Eof))
+    Lexer.Lex();
+  
+  // Eat EOL.
+  if (Lexer.is(asmtok::EndOfStatement))
+    Lexer.Lex();
   return false;
 }
index adcd74c7a51bb89cbb68f934cde7f3197274bf52..5e1f6c65627fdd9a6555c801de7d12b3e4cdf6e0 100644 (file)
@@ -27,6 +27,9 @@ public:
   
   bool Run();
   
+private:
+  bool ParseStatement();
+  
 };
 
 } // end namespace llvm