MIR Serialization: Introduce a lexer for machine instructions.
[oota-llvm.git] / lib / CodeGen / MIRParser / MILexer.h
1 //===- MILexer.h - Lexer for machine instructions -------------------------===//
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 file declares the function that lexes the machine instruction source
11 // string.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
16 #define LLVM_LIB_CODEGEN_MIRPARSER_MILEXER_H
17
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/ADT/STLExtras.h"
20 #include <functional>
21
22 namespace llvm {
23
24 class Twine;
25
26 /// A token produced by the machine instruction lexer.
27 struct MIToken {
28   enum TokenKind {
29     // Markers
30     Eof,
31     Error,
32
33     // Identifier tokens
34     Identifier
35   };
36
37 private:
38   TokenKind Kind;
39   StringRef Range;
40
41 public:
42   MIToken(TokenKind Kind, StringRef Range) : Kind(Kind), Range(Range) {}
43
44   TokenKind kind() const { return Kind; }
45
46   bool isError() const { return Kind == Error; }
47
48   bool is(TokenKind K) const { return Kind == K; }
49
50   bool isNot(TokenKind K) const { return Kind != K; }
51
52   StringRef::iterator location() const { return Range.begin(); }
53
54   StringRef stringValue() const { return Range; }
55 };
56
57 /// Consume a single machine instruction token in the given source and return
58 /// the remaining source string.
59 StringRef lexMIToken(
60     StringRef Source, MIToken &Token,
61     function_ref<void(StringRef::iterator, const Twine &)> ErrorCallback);
62
63 } // end namespace llvm
64
65 #endif