From 6a4a314fee01fcb912e0ae82c5901c59e17ef5ea Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Fri, 24 May 2019 12:41:53 +0000 Subject: [PATCH] Backup work. --- comment.go | 20 +++++++++----------- comment_test.go | 2 +- helpers_test.go | 2 +- toml_test.go | 7 +++---- value_string.go | 15 ++++++--------- 5 files changed, 20 insertions(+), 26 deletions(-) diff --git a/comment.go b/comment.go index f2529e8..3245cbb 100644 --- a/comment.go +++ b/comment.go @@ -5,17 +5,15 @@ import ( ) // A '#' hash symbol marks the rest of the line as a comment. -func startComment(p *parsekit.P) { - p.Expects("start of comment") - p.On(c.OneOrMore(a.Hash)).Skip().RouteTo(commentContents) -} - // All characters up to the end of the line are included in the comment. -func commentContents(p *parsekit.P) { - p.Expects("comment contents") - switch { - case p.On(a.EndOfLine).Skip().RouteReturn().End(): - p.EmitLiteralTrim(ItemComment) - case p.On(a.AnyRune).Accept().RouteRepeat().End(): +var comment = c.Sequence( + c.Drop(c.OneOrMore(a.Hash)), + c.Trim(c.ZeroOrMore(c.Not(a.EndOfLine)), " \t"), + c.Drop(a.EndOfLine)) + +func startComment(p *parsekit.P) { + p.Expects("comment") + if p.On(comment).Accept().RouteReturn().End() { + p.EmitLiteral(ItemComment) } } diff --git a/comment_test.go b/comment_test.go index 234a879..944c3df 100644 --- a/comment_test.go +++ b/comment_test.go @@ -17,6 +17,6 @@ func TestComments(t *testing.T) { {"comment with escape-y chars", `# \xxx/ \u can't escape/`, `#(\xxx/ \u can't escape/)`, ""}, {"comment with multiple hashes", `#### Just Jack!`, `#(Just Jack!)`, ""}, {"comment with hashes inside", `# Follow #me2`, `#(Follow #me2)`, ""}, - {"carriage returns in comment", "# \tlexe\r accepts embedded ca\r\riage \returns\r", "#(lexe\r accepts embedded ca\r\riage \returns)", ""}, + {"carriage returns in comment", "# \tlexe\r accepts embedded ca\r\riage \returns\r\n", "#(lexe\r accepts embedded ca\r\riage \returns)", ""}, }) } diff --git a/helpers_test.go b/helpers_test.go index 823ab9c..6fb124a 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -66,7 +66,7 @@ func runStatesT(t *testing.T, c statesT) { } actual := strings.Join(a, "") if actual != expected { - t.Errorf("[%s] Unexpected parser output:\nexpected: %s\nactual: %s\n", c.name, expected, actual) + t.Errorf("[%s] Unexpected parser output:\nexpected: %q\nactual: %q\n", c.name, expected, actual) } } } diff --git a/toml_test.go b/toml_test.go index 4dbdd87..ca827ef 100644 --- a/toml_test.go +++ b/toml_test.go @@ -20,9 +20,8 @@ func TestErrorFullIncludesLineAndRowPosition(t *testing.T) { } } -func TestInvalidUtf8Data(t *testing.T) { +func TestInvalidUTF8Data(t *testing.T) { runStatesTs(t, []statesT{ - {"inside comment", "# \xbc", "", "invalid UTF8 character in input (expected end of file)"}, {"bare key 1", "\xbc", "", "invalid UTF8 character in input (expected end of file)"}, {"bare key 2", "key\xbc", "[key]", "invalid UTF8 character in input (expected a value assignment)"}, {"start of value", "key=\xbc", "[key]=", "invalid UTF8 character in input (expected a value)"}, @@ -35,7 +34,7 @@ func TestWhiteSpaceAndNewlines(t *testing.T) { {"space", " ", "", ""}, {"tab", "\t", "", ""}, {"newline", "\n", "", ""}, - {"carriage return", "\r", "", ""}, - {"all whitespace and newlines", " \t \t \r\r\n\n \n \t", "", ""}, + {"all whitespace and newlines", " \t \t \r\n\n \n \t", "", ""}, + {"bare carriage return", "\r", "", "unexpected character '\\r' (expected end of file)"}, }) } diff --git a/value_string.go b/value_string.go index e68cb76..c066e4e 100644 --- a/value_string.go +++ b/value_string.go @@ -19,7 +19,7 @@ var ( // // \b - backspace (U+0008) // \t - tab (U+0009) - // \n - linefeed (U+000A) + // \n - LF (U+000A) // \f - form feed (U+000C) // \r - carriage return (U+000D) // \" - quote (U+0022) @@ -28,9 +28,9 @@ var ( // \UXXXXXXXX - unicode (U+XXXXXXXX) validEscapeChar = c.AnyOf(c.Runes('b', 't', 'n', 'f', 'r'), a.DoubleQuote, a.Backslash) shortEscape = c.Sequence(a.Backslash, validEscapeChar) - shortUtf8Escape = c.Sequence(a.Backslash, c.Rune('u'), c.Repeat(4, a.HexDigit)) - longUtf8Escape = c.Sequence(a.Backslash, c.Rune('U'), c.Repeat(8, a.HexDigit)) - validEscape = c.AnyOf(shortEscape, shortUtf8Escape, longUtf8Escape) + shortUTF8Escape = c.Sequence(a.Backslash, c.Rune('u'), c.Repeat(4, a.HexDigit)) + longUTF8Escape = c.Sequence(a.Backslash, c.Rune('U'), c.Repeat(8, a.HexDigit)) + validEscape = c.AnyOf(shortEscape, shortUTF8Escape, longUTF8Escape) ) func startString(p *parsekit.P) { @@ -67,11 +67,8 @@ func basicStringSpecifics(p *parsekit.P) { p.Expects("string contents") switch { case p.On(a.DoubleQuote).Skip().End(): - if err := p.EmitInterpreted(ItemString); err != nil { // TODO testcase? - p.EmitError("invalid data in string: %s", err) - } else { - p.RouteTo(startKeyValuePair) - } + p.EmitInterpreted(ItemString) + p.RouteTo(startKeyValuePair) case p.On(a.Backslash).End(): p.EmitError("invalid escape sequence") }