From 269bd9ed36a6ad4e9a0910d108a251045701fba5 Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Sun, 26 May 2019 09:26:17 +0000 Subject: [PATCH] Compatbility with parsekit v0.0.2 --- comment.go | 2 +- helpers_test.go | 16 ++++++++-------- keyvaluepair.go | 12 ++++++------ toml.go | 9 +++++---- toml_test.go | 4 ++-- value_string.go | 10 +++++----- 6 files changed, 27 insertions(+), 26 deletions(-) diff --git a/comment.go b/comment.go index 71e4111..c573d48 100644 --- a/comment.go +++ b/comment.go @@ -11,7 +11,7 @@ var comment = c.Seq( m.Trim(c.ZeroOrMore(c.Not(a.EndOfLine)), " \t"), m.Drop(a.EndOfLine)) -func startComment(p *parsekit.P) { +func startComment(p *parsekit.ParseAPI) { p.Expects("comment") if p.On(comment).Accept() { p.EmitLiteral(ItemComment) diff --git a/helpers_test.go b/helpers_test.go index 6fb124a..8702099 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -6,7 +6,7 @@ import ( "testing" "git.makaay.nl/mauricem/go-parsekit" - parser "git.makaay.nl/mauricem/go-toml" + toml "git.makaay.nl/mauricem/go-toml" ) type statesT struct { @@ -25,7 +25,7 @@ func runStatesTs(t *testing.T, tests []statesT) { // ToArray returns Parser items as an array. // When an error occurs during scanning, a partial result will be // returned, accompanied by the error that occurred. -func parseItemsToArray(p *parsekit.Run) ([]parsekit.Item, *parsekit.Error) { +func parseItemsToArray(p *parsekit.ParseRun) ([]parsekit.Item, *parsekit.Error) { var items []parsekit.Item for { item, err, more := p.Next() @@ -37,7 +37,7 @@ func parseItemsToArray(p *parsekit.Run) ([]parsekit.Item, *parsekit.Error) { } func runStatesT(t *testing.T, c statesT) { - p := parser.NewParser(c.in) + p := toml.Parse(c.in) l, err := parseItemsToArray(p) if err == nil && c.err != "" { t.Errorf("[%s] Expected error '%s', but no error occurred", c.name, c.err) @@ -74,15 +74,15 @@ func runStatesT(t *testing.T, c statesT) { // parserItemToString returns a string representation of the parsekit.Item. func parserItemToString(i parsekit.Item) string { switch i.Type { - case parser.ItemComment: + case toml.ItemComment: return fmt.Sprintf("#(%s)", i.Value) - case parser.ItemKey: + case toml.ItemKey: return fmt.Sprintf("[%s]", i.Value) - case parser.ItemString: + case toml.ItemString: return fmt.Sprintf("STR(%s)", i.Value) - case parser.ItemKeyDot: + case toml.ItemKeyDot: return "." - case parser.ItemAssignment: + case toml.ItemAssignment: return "=" default: panic(fmt.Sprintf("parsekit bug: no string formatting exists for parsekit.Item id %d", i.Type)) diff --git a/keyvaluepair.go b/keyvaluepair.go index edd0eea..260ef7d 100644 --- a/keyvaluepair.go +++ b/keyvaluepair.go @@ -32,7 +32,7 @@ var ( keySeparatorDot = c.Seq(c.Opt(a.Whitespace), a.Dot, c.Opt(a.Whitespace)) ) -func startKeyValuePair(p *parsekit.P) { +func startKeyValuePair(p *parsekit.ParseAPI) { switch { case p.On(a.WhitespaceAndNewlines).Skip(): p.RouteRepeat() @@ -45,14 +45,14 @@ func startKeyValuePair(p *parsekit.P) { } } -func startKey(p *parsekit.P) { +func startKey(p *parsekit.ParseAPI) { p.Expects("a key name") if p.On(bareKeyRune).Stay() { p.RouteTo(startBareKey) } } -func startBareKey(p *parsekit.P) { +func startBareKey(p *parsekit.ParseAPI) { p.Expects("a bare key name") if p.On(bareKey).Accept() { p.EmitLiteral(ItemKey) @@ -60,7 +60,7 @@ func startBareKey(p *parsekit.P) { } } -func endOfKeyOrDot(p *parsekit.P) { +func endOfKeyOrDot(p *parsekit.ParseAPI) { if p.On(keySeparatorDot).Skip() { p.Emit(ItemKeyDot, ".") p.RouteTo(startKey) @@ -69,7 +69,7 @@ func endOfKeyOrDot(p *parsekit.P) { } } -func startAssignment(p *parsekit.P) { +func startAssignment(p *parsekit.ParseAPI) { p.Expects("a value assignment") if p.On(keyAssignment).Skip() { p.Emit(ItemAssignment, "=") @@ -79,7 +79,7 @@ func startAssignment(p *parsekit.P) { // Values must be of the following types: String, Integer, Float, Boolean, // Datetime, Array, or Inline Table. Unspecified values are invalid. -func startValue(p *parsekit.P) { +func startValue(p *parsekit.ParseAPI) { p.Expects("a value") if p.On(c.Any(a.SingleQuote, a.DoubleQuote)).Stay() { p.RouteTo(startString) diff --git a/toml.go b/toml.go index 6829ed4..0732c1a 100644 --- a/toml.go +++ b/toml.go @@ -15,8 +15,9 @@ var ( c, a, m = parsekit.C, parsekit.A, parsekit.M ) -// NewParser creates a new parser, using the provided input string -// as the data to parse. -func NewParser(input string) *parsekit.Run { - return parsekit.NewParser(startKeyValuePair).Parse(input) +var parser = parsekit.NewParser(startKeyValuePair) + +// Parse starts the parser for the provided input string. +func Parse(input string) *parsekit.ParseRun { + return parser.Parse(input) } diff --git a/toml_test.go b/toml_test.go index ca827ef..a1845f1 100644 --- a/toml_test.go +++ b/toml_test.go @@ -3,7 +3,7 @@ package parser_test import ( "testing" - parser "git.makaay.nl/mauricem/go-toml" + toml "git.makaay.nl/mauricem/go-toml" ) func TestEmptyInput(t *testing.T) { @@ -11,7 +11,7 @@ func TestEmptyInput(t *testing.T) { } func TestErrorFullIncludesLineAndRowPosition(t *testing.T) { - p := parser.NewParser("# 12345 abcde\t\n\n\n# 67890\r\n# 12345\n +") + p := toml.Parse("# 12345 abcde\t\n\n\n# 67890\r\n# 12345\n +") _, err := parseItemsToArray(p) actual := err.ErrorFull() expected := "unexpected character '+' (expected end of file) after line 6, column 3" diff --git a/value_string.go b/value_string.go index d3d04a4..172e8f1 100644 --- a/value_string.go +++ b/value_string.go @@ -33,7 +33,7 @@ var ( validEscape = c.Any(shortEscape, shortUTF8Escape, longUTF8Escape) ) -func startString(p *parsekit.P) { +func startString(p *parsekit.ParseAPI) { p.Expects("a string value") switch { case p.On(doubleQuote3).Stay(): @@ -43,14 +43,14 @@ func startString(p *parsekit.P) { } } -func startBasicString(p *parsekit.P) { +func startBasicString(p *parsekit.ParseAPI) { p.Expects("a basic string") if p.On(a.DoubleQuote).Skip() { p.RouteTo(parseBasicString).ThenTo(basicStringSpecifics) } } -func parseBasicString(p *parsekit.P) { +func parseBasicString(p *parsekit.ParseAPI) { p.Expects("string contents") switch { case p.On(charThatMustBeEscaped).Stay(): @@ -71,7 +71,7 @@ func parseBasicString(p *parsekit.P) { // * No additional \escape sequences are allowed. What the spec say about this: // "All other escape sequences [..] are reserved and, if used, TOML should // produce an error."" -func basicStringSpecifics(p *parsekit.P) { +func basicStringSpecifics(p *parsekit.ParseAPI) { p.Expects("string contents") switch { case p.On(a.DoubleQuote).Skip(): @@ -82,7 +82,7 @@ func basicStringSpecifics(p *parsekit.P) { } } -func startMultiLineBasicString(p *parsekit.P) { +func startMultiLineBasicString(p *parsekit.ParseAPI) { p.Expects("a multi-line basic string") if p.On(doubleQuote3).Skip() { p.EmitError("not yet implemented")