rename SourceMgr::PrintError to PrintMessage.
[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::Error(SMLoc L, const char *Msg) {
20   Lexer.PrintMessage(L, Msg);
21   return true;
22 }
23
24 bool AsmParser::TokError(const char *Msg) {
25   Lexer.PrintMessage(Lexer.getLoc(), Msg);
26   return true;
27 }
28
29
30 bool AsmParser::Run() {
31   // Prime the lexer.
32   Lexer.Lex();
33   
34   while (Lexer.isNot(asmtok::Eof))
35     if (ParseStatement())
36       return true;
37   
38   return false;
39 }
40
41
42 /// ParseStatement:
43 ///   ::= EndOfStatement
44 ///   ::= Label* Identifier Operands* EndOfStatement
45 bool AsmParser::ParseStatement() {
46   switch (Lexer.getKind()) {
47   default:
48     return TokError("unexpected token at start of statement");
49   case asmtok::EndOfStatement:
50     Lexer.Lex();
51     return false;
52   case asmtok::Identifier:
53     break;
54   // TODO: Recurse on local labels etc.
55   }
56   
57   // If we have an identifier, handle it as the key symbol.
58   //SMLoc IDLoc = Lexer.getLoc();
59   std::string IDVal = Lexer.getCurStrVal();
60   
61   // Consume the identifier, see what is after it.
62   if (Lexer.Lex() == asmtok::Colon) {
63     // identifier ':'   -> Label.
64     Lexer.Lex();
65     return ParseStatement();
66   }
67   
68   // Otherwise, we have a normal instruction or directive.  
69   if (IDVal[0] == '.')
70     outs() << "Found directive: " << IDVal << "\n";
71   else
72     outs() << "Found instruction: " << IDVal << "\n";
73
74   // Skip to end of line for now.
75   while (Lexer.isNot(asmtok::EndOfStatement) &&
76          Lexer.isNot(asmtok::Eof))
77     Lexer.Lex();
78   
79   // Eat EOL.
80   if (Lexer.is(asmtok::EndOfStatement))
81     Lexer.Lex();
82   return false;
83 }