Escape quotes in double-quoted string literals

This commit is contained in:
Alexander Luzgarev 2018-04-07 11:29:42 +02:00
parent e54c01a243
commit 35f261e74f
3 changed files with 43 additions and 4 deletions

View File

@ -226,6 +226,16 @@ namespace Lexer.Tests
Assert.AreEqual("just a 'string'", tokens[0].PureToken.Value); Assert.AreEqual("just a 'string'", tokens[0].PureToken.Value);
} }
[Test]
public void ParseDoubleQuotedStringLiteralWithEscapedQuotes()
{
var sut = CreateLexer("\"just a \"\"string\"\"\"");
var tokens = sut.ParseAll();
Assert.AreEqual(2, tokens.Count);
Assert.AreEqual(TokenKind.DoubleQuotedStringLiteral, tokens[0].Kind);
Assert.AreEqual("just a \"string\"", tokens[0].PureToken.Value);
}
[Test] [Test]
public void ParseNumberStartingWithDot() public void ParseNumberStartingWithDot()
{ {

View File

@ -406,14 +406,38 @@ namespace Lexer
{ {
Window.ConsumeChar(); Window.ConsumeChar();
var n = 0; var n = 0;
while (Window.PeekChar(n) != '"') var pieces = new List<string>();
while (true)
{ {
if (Window.PeekChar(n) == '"')
{
if (Window.PeekChar(n + 1) == '"')
{
var piece = Window.GetAndConsumeChars(n);
pieces.Add(piece);
Window.ConsumeChar();
Window.ConsumeChar();
pieces.Add("\"");
n = -1;
}
else
{
break;
}
}
if (IsEolOrEof(Window.PeekChar(n)))
{
throw new ParsingException("Unfinished double-quoted string literal.");
}
n++; n++;
} }
var literal = Window.GetAndConsumeChars(n); var lastPiece = Window.GetAndConsumeChars(n);
pieces.Add(lastPiece);
var total = string.Join("", pieces);
Window.ConsumeChar(); Window.ConsumeChar();
return PureTokenFactory.CreateDoubleQuotedStringLiteral(literal); return PureTokenFactory.CreateDoubleQuotedStringLiteral(total);
} }
private PureToken ContinueParsingUnquotedStringLiteral() private PureToken ContinueParsingUnquotedStringLiteral()

View File

@ -88,9 +88,14 @@
return new PureToken(TokenKind.StringLiteral, "'" + EscapeStringLiteral(s) + "'", s, Window.Position); return new PureToken(TokenKind.StringLiteral, "'" + EscapeStringLiteral(s) + "'", s, Window.Position);
} }
private string EscapeDoubleQuotedStringLiteral(string s)
{
return s.Replace("\"", "\"\"");
}
public PureToken CreateDoubleQuotedStringLiteral(string s) public PureToken CreateDoubleQuotedStringLiteral(string s)
{ {
return new PureToken(TokenKind.DoubleQuotedStringLiteral, "\"" + s + "\"", s, Window.Position); return new PureToken(TokenKind.DoubleQuotedStringLiteral, "\"" + EscapeDoubleQuotedStringLiteral(s) + "\"", s, Window.Position);
} }
public PureToken CreateUnquotedStringLiteral(string s) public PureToken CreateUnquotedStringLiteral(string s)