MIR Parser: Simplify the handling of quoted tokens. NFC.
[oota-llvm.git] / lib / CodeGen / MIRParser / MILexer.cpp
index adc72e2a5668511a17cf1a9d07ef01dca9b7ee84..624ef664620dddda164717f61aaae85b35e50a80 100644 (file)
@@ -69,13 +69,14 @@ static bool isIdentifierChar(char C) {
          C == '$';
 }
 
-void MIToken::unescapeQuotedStringValue(std::string &Str) const {
-  assert(isStringValueQuoted() && "String value isn't quoted");
-  StringRef Value = Range.drop_front(StringOffset);
+/// Unescapes the given string value.
+///
+/// Expects the string value to be quoted.
+static std::string unescapeQuotedString(StringRef Value) {
   assert(Value.front() == '"' && Value.back() == '"');
   Cursor C = Cursor(Value.substr(1, Value.size() - 2));
 
-  Str.clear();
+  std::string Str;
   Str.reserve(C.remaining().size());
   while (!C.isEOF()) {
     char Char = C.peek();
@@ -95,6 +96,7 @@ void MIToken::unescapeQuotedStringValue(std::string &Str) const {
     Str += Char;
     C.advance();
   }
+  return Str;
 }
 
 /// Lex a string constant using the following regular expression: \"[^\"]*\"
@@ -115,14 +117,16 @@ static Cursor lexStringConstant(
 }
 
 static Cursor lexName(
-    Cursor C, MIToken &Token, MIToken::TokenKind Type,
-    MIToken::TokenKind QuotedType, unsigned PrefixLength,
+    Cursor C, MIToken &Token, MIToken::TokenKind Type, unsigned PrefixLength,
     function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
   auto Range = C;
   C.advance(PrefixLength);
   if (C.peek() == '"') {
     if (Cursor R = lexStringConstant(C, ErrorCallback)) {
-      Token = MIToken(QuotedType, Range.upto(R), PrefixLength);
+      StringRef String = Range.upto(R);
+      Token = MIToken(Type, String,
+                      unescapeQuotedString(String.drop_front(PrefixLength)),
+                      PrefixLength);
       return R;
     }
     Token = MIToken(MIToken::Error, Range.remaining());
@@ -257,8 +261,7 @@ static Cursor maybeLexIRBlock(
     return None;
   if (isdigit(C.peek(Rule.size())))
     return maybeLexIndex(C, Token, Rule, MIToken::IRBlock);
-  return lexName(C, Token, MIToken::NamedIRBlock, MIToken::QuotedNamedIRBlock,
-                 Rule.size(), ErrorCallback);
+  return lexName(C, Token, MIToken::NamedIRBlock, Rule.size(), ErrorCallback);
 }
 
 static Cursor maybeLexIRValue(
@@ -267,8 +270,7 @@ static Cursor maybeLexIRValue(
   const StringRef Rule = "%ir.";
   if (!C.remaining().startswith(Rule))
     return None;
-  return lexName(C, Token, MIToken::NamedIRValue, MIToken::QuotedNamedIRValue,
-                 Rule.size(), ErrorCallback);
+  return lexName(C, Token, MIToken::NamedIRValue, Rule.size(), ErrorCallback);
 }
 
 static Cursor lexVirtualRegister(Cursor C, MIToken &Token) {
@@ -302,8 +304,7 @@ static Cursor maybeLexGlobalValue(
   if (C.peek() != '@')
     return None;
   if (!isdigit(C.peek(1)))
-    return lexName(C, Token, MIToken::NamedGlobalValue,
-                   MIToken::QuotedNamedGlobalValue, /*PrefixLength=*/1,
+    return lexName(C, Token, MIToken::NamedGlobalValue, /*PrefixLength=*/1,
                    ErrorCallback);
   auto Range = C;
   C.advance(1); // Skip the '@'
@@ -320,9 +321,8 @@ static Cursor maybeLexExternalSymbol(
     function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
   if (C.peek() != '$')
     return None;
-  return lexName(C, Token, MIToken::ExternalSymbol,
-                 MIToken::QuotedExternalSymbol,
-                 /*PrefixLength=*/1, ErrorCallback);
+  return lexName(C, Token, MIToken::ExternalSymbol, /*PrefixLength=*/1,
+                 ErrorCallback);
 }
 
 static bool isValidHexFloatingPointPrefix(char C) {