diff dmd/Lexer.d @ 8:d42cd5917df4

wysiwyg strings, alias this, templates, TypeSlice implementation
author dkoroskin <>
date Mon, 14 Dec 2009 17:43:43 +0300
parents d706d958e4e8
children 544b922227c7
line wrap: on
line diff
--- a/dmd/Lexer.d	Mon Dec 14 14:50:03 2009 +0300
+++ b/dmd/Lexer.d	Mon Dec 14 17:43:43 2009 +0300
@@ -1561,6 +1561,64 @@
 
     TOK wysiwygStringConstant(Token* t, int tc)
 	{
+		uint c;
+		Loc start = loc;
+
+		p++;
+		stringbuffer.reset();
+		while (true)
+		{
+			c = *p++;
+			switch (c)
+			{
+				case '\n':
+					loc.linnum++;
+					break;
+
+				case '\r':
+					if (*p == '\n')
+						continue;	// ignore
+					c = '\n';	// treat EndOfLine as \n character
+					loc.linnum++;
+					break;
+
+				case 0:
+				case 0x1A:
+					error("unterminated string constant starting at %s", start.toChars());
+					t.ustring = "".ptr;
+					t.len = 0;
+					t.postfix = 0;
+					return TOKstring;
+
+				case '"':
+				case '`':
+					if (c == tc)
+					{
+						t.len = stringbuffer.offset;
+						stringbuffer.writeByte(0);
+						char* tmp = cast(char*)GC.malloc(stringbuffer.offset);
+						memcpy(tmp, stringbuffer.data, stringbuffer.offset);
+						t.ustring = tmp;
+						stringPostfix(t);
+						return TOKstring;
+					}
+					break;
+
+				default:
+					if (c & 0x80)
+					{   p--;
+						uint u = decodeUTF();
+						p++;
+						if (u == PS || u == LS)
+							loc.linnum++;
+						stringbuffer.writeUTF8(u);
+						continue;
+					}
+					break;
+			}
+			stringbuffer.writeByte(c);
+		}
+		
 		assert(false);
 	}
 
@@ -1575,9 +1633,59 @@
 		assert(false);
 	}
 
+	/**************************************
+	 * Lex delimited strings:
+	 *	q{ foo(xxx) } // " foo(xxx) "
+	 *	q{foo(}       // "foo("
+	 *	q{{foo}"}"}   // "{foo}"}""
+	 * Input:
+	 *	p is on the q
+	 */
     TOK tokenStringConstant(Token* t)
 	{
-		assert(false);
+		uint nest = 1;
+		Loc start = loc;
+		ubyte* pstart = ++p;
+
+		while (true)
+		{	
+			Token tok;
+
+			scan(&tok);
+			switch (tok.value)
+			{
+				case TOKlcurly:
+					nest++;
+					continue;
+
+				case TOKrcurly:
+					if (--nest == 0)
+						goto Ldone;
+					continue;
+
+				case TOKeof:
+					goto Lerror;
+
+				default:
+					continue;
+			}
+		}
+
+	Ldone:
+		t.len = p - 1 - pstart;
+		char* tmp = cast(char*)GC.malloc(t.len + 1);
+		memcpy(tmp, pstart, t.len);
+		tmp[t.len] = 0;
+		t.ustring = tmp;
+		stringPostfix(t);
+		return TOKstring;
+
+	Lerror:
+		error("unterminated token string constant starting at %s", start.toChars());
+		t.ustring = "".ptr;
+		t.len = 0;
+		t.postfix = 0;
+		return TOKstring;
 	}
 }
     TOK escapeStringConstant(Token* t, int wide)