summaryrefslogtreecommitdiffstats
path: root/llvm/lib/CodeGen/MIRParser/MILexer.cpp
diff options
context:
space:
mode:
authorAlex Lorenz <arphaman@gmail.com>2015-06-22 20:37:46 +0000
committerAlex Lorenz <arphaman@gmail.com>2015-06-22 20:37:46 +0000
commit91370c5d62726374cf9ef95a49a0a287499c3cf8 (patch)
treef751334d15625bd6c2dfb5509a4113ec351b0bc9 /llvm/lib/CodeGen/MIRParser/MILexer.cpp
parentf22855079a30f981e87eb95599cbc628260d2f04 (diff)
downloadbcm5719-llvm-91370c5d62726374cf9ef95a49a0a287499c3cf8.tar.gz
bcm5719-llvm-91370c5d62726374cf9ef95a49a0a287499c3cf8.zip
MIR Serialization: Introduce a lexer for machine instructions.
This commit adds a function that tokenizes the string containing the machine instruction. This commit also adds a struct called 'MIToken' which is used to represent the lexer's tokens. Reviewers: Sean Silva Differential Revision: http://reviews.llvm.org/D10521 llvm-svn: 240323
Diffstat (limited to 'llvm/lib/CodeGen/MIRParser/MILexer.cpp')
-rw-r--r--llvm/lib/CodeGen/MIRParser/MILexer.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/llvm/lib/CodeGen/MIRParser/MILexer.cpp b/llvm/lib/CodeGen/MIRParser/MILexer.cpp
new file mode 100644
index 00000000000..69fbba60085
--- /dev/null
+++ b/llvm/lib/CodeGen/MIRParser/MILexer.cpp
@@ -0,0 +1,87 @@
+//===- MILexer.cpp - Machine instructions lexer implementation ----------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// This file implements the lexing of machine instructions.
+//
+//===----------------------------------------------------------------------===//
+
+#include "MILexer.h"
+#include "llvm/ADT/Twine.h"
+#include <cctype>
+
+using namespace llvm;
+
+namespace {
+
+/// This class provides a way to iterate and get characters from the source
+/// string.
+class Cursor {
+ const char *Ptr;
+ const char *End;
+
+public:
+ explicit Cursor(StringRef Str) {
+ Ptr = Str.data();
+ End = Ptr + Str.size();
+ }
+
+ bool isEOF() const { return Ptr == End; }
+
+ char peek() const { return isEOF() ? 0 : *Ptr; }
+
+ void advance() { ++Ptr; }
+
+ StringRef remaining() const { return StringRef(Ptr, End - Ptr); }
+
+ StringRef upto(Cursor C) const {
+ assert(C.Ptr >= Ptr && C.Ptr <= End);
+ return StringRef(Ptr, C.Ptr - Ptr);
+ }
+
+ StringRef::iterator location() const { return Ptr; }
+};
+
+} // end anonymous namespace
+
+/// Skip the leading whitespace characters and return the updated cursor.
+static Cursor skipWhitespace(Cursor C) {
+ while (isspace(C.peek()))
+ C.advance();
+ return C;
+}
+
+static bool isIdentifierChar(char C) {
+ return isalpha(C) || isdigit(C) || C == '_' || C == '-' || C == '.';
+}
+
+static Cursor lexIdentifier(Cursor C, MIToken &Token) {
+ auto Range = C;
+ while (isIdentifierChar(C.peek()))
+ C.advance();
+ Token = MIToken(MIToken::Identifier, Range.upto(C));
+ return C;
+}
+
+StringRef llvm::lexMIToken(
+ StringRef Source, MIToken &Token,
+ function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
+ auto C = skipWhitespace(Cursor(Source));
+ if (C.isEOF()) {
+ Token = MIToken(MIToken::Eof, C.remaining());
+ return C.remaining();
+ }
+
+ auto Char = C.peek();
+ if (isalpha(Char) || Char == '_')
+ return lexIdentifier(C, Token).remaining();
+ Token = MIToken(MIToken::Error, C.remaining());
+ ErrorCallback(C.location(),
+ Twine("unexpected character '") + Twine(Char) + "'");
+ return C.remaining();
+}
OpenPOWER on IntegriCloud