1 //===- AsmLexer.cpp - Lexer for Assembly Files ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This class implements the lexer for assembly files.
12 //===----------------------------------------------------------------------===//
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/MemoryBuffer.h"
19 AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
21 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
22 CurPtr = CurBuf->getBufferStart();
26 void AsmLexer::PrintError(const char *Loc, const std::string &Msg) const {
27 SrcMgr.PrintError(SMLoc::getFromPointer(Loc), Msg);
30 void AsmLexer::PrintError(SMLoc Loc, const std::string &Msg) const {
31 SrcMgr.PrintError(Loc, Msg);
34 int AsmLexer::getNextChar() {
35 char CurChar = *CurPtr++;
38 return (unsigned char)CurChar;
40 // A nul character in the stream is either the end of the current buffer or
41 // a random nul in the file. Disambiguate that here.
42 if (CurPtr-1 != CurBuf->getBufferEnd())
43 return 0; // Just whitespace.
45 // If this is the end of an included file, pop the parent file off the
47 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
48 if (ParentIncludeLoc != SMLoc()) {
49 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
50 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
51 CurPtr = ParentIncludeLoc.getPointer();
55 // Otherwise, return end of file.
56 --CurPtr; // Another call to lex will return EOF again.
62 asmtok::TokKind AsmLexer::LexToken() {
64 // This always consumes at least one character.
65 int CurChar = getNextChar();
69 // Handle letters: [a-zA-Z_]
70 // if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
71 // return LexIdentifier();
73 // Unknown character, emit an error.
75 case EOF: return asmtok::Eof;
83 case ':': return asmtok::Colon;
84 case '+': return asmtok::Plus;
85 case '-': return asmtok::Minus;