From 4fda9eb3584cfcfd1e35267526442c022693f7ed Mon Sep 17 00:00:00 2001 From: Ryo Nihei Date: Sat, 28 Aug 2021 01:55:06 +0900 Subject: Support the escape sequecens \' and \\ in a string literal --- spec/lexer_test.go | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) (limited to 'spec/lexer_test.go') diff --git a/spec/lexer_test.go b/spec/lexer_test.go index c3540f6..43b192f 100644 --- a/spec/lexer_test.go +++ b/spec/lexer_test.go @@ -40,11 +40,11 @@ func TestLexer_Run(t *testing.T) { }{ { caption: "the lexer can recognize all kinds of tokens", - src: `id"terminal"'.*+?|()[\':|;#()$1...#%`, + src: `id"terminal"'string':|;#()$1...#%`, tokens: []*token{ idTok("id"), termPatTok("terminal"), - strTok(`.*+?|()[\`), + strTok(`string`), symTok(tokenKindColon), symTok(tokenKindOr), symTok(tokenKindSemicolon), @@ -66,22 +66,30 @@ func TestLexer_Run(t *testing.T) { }, }, { - caption: "the lexer can recognize character sequences and escape sequences in terminal", + caption: "the lexer can recognize character sequences and escape sequences in a terminal", src: `"abc\"\\"`, tokens: []*token{ termPatTok(`abc"\\`), newEOFToken(), }, }, + { + caption: "the lexer can recognize character sequences and escape sequences in a string literal", + src: `'.*+?|()[\'\\'`, + tokens: []*token{ + strTok(`.*+?|()['\`), + newEOFToken(), + }, + }, { caption: "a pattern must include at least one character", src: `""`, err: synErrEmptyPattern, }, { - caption: "a literal pattern must include at least one character", + caption: "a string must include at least one character", src: `''`, - err: synErrEmptyPattern, + err: synErrEmptyString, }, { caption: "the lexer can recognize newlines and combine consecutive newlines into one", @@ -126,10 +134,20 @@ bar // This is the fourth comment. err: synErrUnclosedTerminal, }, { - caption: "an incompleted terminal is not a valid token", + caption: "an incompleted terminal in a pattern is not a valid token", src: `"\`, err: synErrIncompletedEscSeq, }, + { + caption: "an unclosed string is not a valid token", + src: `'abc`, + err: synErrUnclosedString, + }, + { + caption: "an incompleted terminal in a string is not a valid token", + src: `'\`, + err: synErrIncompletedEscSeq, + }, { caption: "a position must be greater than or equal to 1", src: `$0`, -- cgit v1.2.3