add support for a few simple escape characters in tblgen strings.
authorChris Lattner <sabre@nondot.org>
Fri, 13 Mar 2009 21:03:27 +0000 (21:03 +0000)
committerChris Lattner <sabre@nondot.org>
Fri, 13 Mar 2009 21:03:27 +0000 (21:03 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@66949 91177308-0d34-0410-b5e6-96231b3b80d8

test/TableGen/String.td [new file with mode: 0644]
utils/TableGen/TGLexer.cpp

diff --git a/test/TableGen/String.td b/test/TableGen/String.td
new file mode 100644 (file)
index 0000000..d2ae451
--- /dev/null
@@ -0,0 +1,5 @@
+// RUN: tblgen %s 
+class x {
+  string y = "missing terminating '\"' character";
+}
+
index e315db25451a9c829a5ad7fad74bfc233cc62d8f..8b810918dc676509ab2acd0180e045b1278f51f2 100644 (file)
@@ -151,6 +151,8 @@ tgtok::TokKind TGLexer::LexToken() {
 tgtok::TokKind TGLexer::LexString() {
   const char *StrStart = CurPtr;
   
+  CurStrVal = "";
+  
   while (*CurPtr != '"') {
     // If we hit the end of the buffer, report an error.
     if (*CurPtr == 0 && CurPtr == CurBuf->getBufferEnd())
@@ -159,10 +161,32 @@ tgtok::TokKind TGLexer::LexString() {
     if (*CurPtr == '\n' || *CurPtr == '\r')
       return ReturnError(StrStart, "End of line in string literal");
     
+    if (*CurPtr != '\\') {
+      CurStrVal += *CurPtr++;
+      continue;
+    }
+
     ++CurPtr;
+    
+    switch (*CurPtr) {
+    case '\\': case '\'': case '"':
+      // These turn into their literal character.
+      CurStrVal += *CurPtr++;
+      break;
+    case '\n':
+    case '\r':
+      return ReturnError(CurPtr, "escaped newlines not supported in tblgen");
+
+    // If we hit the end of the buffer, report an error.
+    case '\0':
+      if (CurPtr == CurBuf->getBufferEnd())
+        return ReturnError(StrStart, "End of file in string literal");
+      // FALL THROUGH
+    default:
+      return ReturnError(CurPtr, "invalid escape in string literal");
+    }
   }
   
-  CurStrVal.assign(StrStart, CurPtr);
   ++CurPtr;
   return tgtok::StrVal;
 }