set up the top-level parsing loop.
[oota-llvm.git] / tools / llvm-mc / AsmParser.cpp
1 //===- AsmParser.cpp - Parser for Assembly Files --------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This class implements the parser for assembly files.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "AsmParser.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/raw_ostream.h"
17 using namespace llvm;
18
19 bool AsmParser::Run() {
20   // Prime the lexer.
21   Lexer.Lex();
22   
23   while (Lexer.isNot(asmtok::Eof))
24     if (ParseStatement())
25       return true;
26   
27   return false;
28 }
29
30
31 /// ParseStatement:
32 ///   ::= EndOfStatement
33 ///   ::= Label* Identifier Operands* EndOfStatement
34 bool AsmParser::ParseStatement() {
35   switch (Lexer.getKind()) {
36   default:
37     Lexer.PrintError(Lexer.getLoc(), "unexpected token at start of statement");
38     return true;
39   case asmtok::EndOfStatement:
40     Lexer.Lex();
41     return false;
42   case asmtok::Identifier:
43     break;
44   // TODO: Recurse on local labels etc.
45   }
46   
47   // If we have an identifier, handle it as the key symbol.
48   //SMLoc IDLoc = Lexer.getLoc();
49   std::string IDVal = Lexer.getCurStrVal();
50   
51   // Consume the identifier, see what is after it.
52   if (Lexer.Lex() == asmtok::Colon) {
53     // identifier ':'   -> Label.
54     Lexer.Lex();
55     return ParseStatement();
56   }
57   
58   // Otherwise, we have a normal instruction or directive.  
59   if (IDVal[0] == '.')
60     outs() << "Found directive: " << IDVal << "\n";
61   else
62     outs() << "Found instruction: " << IDVal << "\n";
63
64   // Skip to end of line for now.
65   while (Lexer.isNot(asmtok::EndOfStatement) &&
66          Lexer.isNot(asmtok::Eof))
67     Lexer.Lex();
68   
69   // Eat EOL.
70   if (Lexer.is(asmtok::EndOfStatement))
71     Lexer.Lex();
72   return false;
73 }