From 65f4ce27861a919002f2446ee43a6bf111760aef Mon Sep 17 00:00:00 2001 From: Alexander Luzgarev Date: Sat, 31 Mar 2018 22:31:29 +0200 Subject: [PATCH] Lex string literals with escaped quotes --- Lexer.Tests/MLexerShould.cs | 10 ++++++++++ Lexer/MLexer.cs | 21 ++++++++++++++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/Lexer.Tests/MLexerShould.cs b/Lexer.Tests/MLexerShould.cs index 28fd34e..a75bbe3 100644 --- a/Lexer.Tests/MLexerShould.cs +++ b/Lexer.Tests/MLexerShould.cs @@ -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); + } } } \ No newline at end of file diff --git a/Lexer/MLexer.cs b/Lexer/MLexer.cs index 93af021..d9d4d80 100644 --- a/Lexer/MLexer.cs +++ b/Lexer/MLexer.cs @@ -306,11 +306,24 @@ namespace Lexer private PureToken ContinueParsingStringLiteral() { Window.ConsumeChar(); + var pieces = new List(); 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()