Lex string literals with escaped quotes

This commit is contained in:
Alexander Luzgarev 2018-03-31 22:31:29 +02:00
parent 8acea8e90e
commit 65f4ce2786
2 changed files with 28 additions and 3 deletions

View File

@ -215,5 +215,15 @@ namespace Parser.Tests
Assert.AreEqual(TokenKind.StringLiteral, tokens[0].Kind);
Assert.AreEqual("just a string", tokens[0].PureToken.Value);
}
[Test]
public void ParseStringLiteralWithEscapedQuotes()
{
var sut = CreateLexer("'just a ''string'''");
var tokens = sut.ParseAll();
Assert.AreEqual(2, tokens.Count);
Assert.AreEqual(TokenKind.StringLiteral, tokens[0].Kind);
Assert.AreEqual("just a 'string'", tokens[0].PureToken.Value);
}
}
}

View File

@ -306,11 +306,24 @@ namespace Lexer
private PureToken ContinueParsingStringLiteral()
{
Window.ConsumeChar();
var pieces = new List<string>();
var n = 0;
while (true) {
if (Window.PeekChar(n) == '\'')
{
break;
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)))
{
@ -319,9 +332,11 @@ namespace Lexer
n++;
}
var literal = Window.GetAndConsumeChars(n);
var lastPiece = Window.GetAndConsumeChars(n);
pieces.Add(lastPiece);
var total = string.Join("", pieces);
Window.ConsumeChar();
return PureTokenFactory.CreateStringLiteral(literal);
return PureTokenFactory.CreateStringLiteral(total);
}
private PureToken ContinueParsingDoubleQuotedStringLiteral()