blob: da86465d7fe3be596ed3e4d26a0f50af95368ff3 [file] [log] [blame]
Chris Lattner22b67fb2009-06-21 07:19:10 +00001//===- AsmLexer.cpp - Lexer 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 lexer for assembly files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "AsmLexer.h"
15#include "llvm/Support/SourceMgr.h"
16#include "llvm/Support/MemoryBuffer.h"
17using namespace llvm;
18
19AsmLexer::AsmLexer(SourceMgr &SM) : SrcMgr(SM) {
20 CurBuffer = 0;
21 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
22 CurPtr = CurBuf->getBufferStart();
23 TokStart = 0;
24}
25
26void AsmLexer::PrintError(const char *Loc, const std::string &Msg) const {
27 SrcMgr.PrintError(SMLoc::getFromPointer(Loc), Msg);
28}
29
30void AsmLexer::PrintError(SMLoc Loc, const std::string &Msg) const {
31 SrcMgr.PrintError(Loc, Msg);
32}
33
34int AsmLexer::getNextChar() {
35 char CurChar = *CurPtr++;
36 switch (CurChar) {
37 default:
38 return (unsigned char)CurChar;
39 case 0: {
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.
44
45 // If this is the end of an included file, pop the parent file off the
46 // include stack.
47 SMLoc ParentIncludeLoc = SrcMgr.getParentIncludeLoc(CurBuffer);
48 if (ParentIncludeLoc != SMLoc()) {
49 CurBuffer = SrcMgr.FindBufferContainingLoc(ParentIncludeLoc);
50 CurBuf = SrcMgr.getMemoryBuffer(CurBuffer);
51 CurPtr = ParentIncludeLoc.getPointer();
52 return getNextChar();
53 }
54
55 // Otherwise, return end of file.
56 --CurPtr; // Another call to lex will return EOF again.
57 return EOF;
58 }
59 }
60}
61
62asmtok::TokKind AsmLexer::LexToken() {
63 TokStart = CurPtr;
64 // This always consumes at least one character.
65 int CurChar = getNextChar();
66
67 switch (CurChar) {
68 default:
69 // Handle letters: [a-zA-Z_]
70// if (isalpha(CurChar) || CurChar == '_' || CurChar == '#')
71// return LexIdentifier();
72
73 // Unknown character, emit an error.
74 return asmtok::Error;
75 case EOF: return asmtok::Eof;
76 case 0:
77 case ' ':
78 case '\t':
79 case '\n':
80 case '\r':
81 // Ignore whitespace.
82 return LexToken();
83 case ':': return asmtok::Colon;
84 case '+': return asmtok::Plus;
85 case '-': return asmtok::Minus;
86 }
87}