From a56707c85f46c5ef836901c979e776cc30999f84 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Sat, 6 Dec 2014 15:23:37 +0100 Subject: [PATCH] Fixes #28 : Support of literal strings --- lexer.go | 25 +++++++++++++++++++++++++ lexer_test.go | 27 +++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/lexer.go b/lexer.go index 7835116..cc4dc08 100644 --- a/lexer.go +++ b/lexer.go @@ -169,6 +169,8 @@ func (l *tomlLexer) lexRvalue() tomlLexStateFn { return l.lexComment case '"': return l.lexString + case '\'': + return l.lexLiteralString case ',': return l.lexComma case '\n': @@ -279,6 +281,29 @@ func (l *tomlLexer) lexLeftBracket() tomlLexStateFn { return l.lexRvalue } +func (l *tomlLexer) lexLiteralString() tomlLexStateFn { + l.pos++ + l.ignore() + growingString := "" + + for { + if l.peek() == '\'' { + l.emitWithValue(tokenString, growingString) + l.pos++ + l.ignore() + return l.lexRvalue + } + + growingString += string(l.peek()) + + if l.next() == eof { + break + } + } + + return l.errorf("unclosed string") +} + func (l *tomlLexer) lexString() tomlLexStateFn { l.pos++ l.ignore() diff --git a/lexer_test.go b/lexer_test.go index cc8fb47..2555a35 100644 --- a/lexer_test.go +++ b/lexer_test.go @@ -460,6 +460,33 @@ func TestKeyEqualStringUnicodeEscape(t *testing.T) { }) } +func TestLiteralString(t *testing.T) { + testFlow(t, `foo = 'C:\Users\nodejs\templates'`, []token{ + token{Position{1, 1}, tokenKey, "foo"}, + token{Position{1, 5}, tokenEqual, "="}, + token{Position{1, 8}, tokenString, `C:\Users\nodejs\templates`}, + token{Position{1, 34}, tokenEOF, ""}, + }) + testFlow(t, `foo = '\\ServerX\admin$\system32\'`, []token{ + token{Position{1, 1}, tokenKey, "foo"}, + token{Position{1, 5}, tokenEqual, "="}, + token{Position{1, 8}, tokenString, `\\ServerX\admin$\system32\`}, + token{Position{1, 35}, tokenEOF, ""}, + }) + testFlow(t, `foo = 'Tom "Dubs" Preston-Werner'`, []token{ + token{Position{1, 1}, tokenKey, "foo"}, + token{Position{1, 5}, tokenEqual, "="}, + token{Position{1, 8}, tokenString, `Tom "Dubs" Preston-Werner`}, + token{Position{1, 34}, tokenEOF, ""}, + }) + testFlow(t, `foo = '<\i\c*\s*>'`, []token{ + token{Position{1, 1}, tokenKey, "foo"}, + token{Position{1, 5}, tokenEqual, "="}, + token{Position{1, 8}, tokenString, `<\i\c*\s*>`}, + token{Position{1, 19}, tokenEOF, ""}, + }) +} + func TestUnicodeString(t *testing.T) { testFlow(t, `foo = "hello ♥ world"`, []token{ token{Position{1, 1}, tokenKey, "foo"},