Lex string literals with escaped quotes
This commit is contained in:
parent
8acea8e90e
commit
65f4ce2786
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user