Compatbility with parsekit v0.0.2

This commit is contained in:
Maurice Makaay 2019-05-26 09:26:17 +00:00
parent 9ab468a3ba
commit 269bd9ed36
6 changed files with 27 additions and 26 deletions

View File

@ -11,7 +11,7 @@ var comment = c.Seq(
m.Trim(c.ZeroOrMore(c.Not(a.EndOfLine)), " \t"), m.Trim(c.ZeroOrMore(c.Not(a.EndOfLine)), " \t"),
m.Drop(a.EndOfLine)) m.Drop(a.EndOfLine))
func startComment(p *parsekit.P) { func startComment(p *parsekit.ParseAPI) {
p.Expects("comment") p.Expects("comment")
if p.On(comment).Accept() { if p.On(comment).Accept() {
p.EmitLiteral(ItemComment) p.EmitLiteral(ItemComment)

View File

@ -6,7 +6,7 @@ import (
"testing" "testing"
"git.makaay.nl/mauricem/go-parsekit" "git.makaay.nl/mauricem/go-parsekit"
parser "git.makaay.nl/mauricem/go-toml" toml "git.makaay.nl/mauricem/go-toml"
) )
type statesT struct { type statesT struct {
@ -25,7 +25,7 @@ func runStatesTs(t *testing.T, tests []statesT) {
// ToArray returns Parser items as an array. // ToArray returns Parser items as an array.
// When an error occurs during scanning, a partial result will be // When an error occurs during scanning, a partial result will be
// returned, accompanied by the error that occurred. // 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 var items []parsekit.Item
for { for {
item, err, more := p.Next() 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) { func runStatesT(t *testing.T, c statesT) {
p := parser.NewParser(c.in) p := toml.Parse(c.in)
l, err := parseItemsToArray(p) l, err := parseItemsToArray(p)
if err == nil && c.err != "" { if err == nil && c.err != "" {
t.Errorf("[%s] Expected error '%s', but no error occurred", c.name, 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. // parserItemToString returns a string representation of the parsekit.Item.
func parserItemToString(i parsekit.Item) string { func parserItemToString(i parsekit.Item) string {
switch i.Type { switch i.Type {
case parser.ItemComment: case toml.ItemComment:
return fmt.Sprintf("#(%s)", i.Value) return fmt.Sprintf("#(%s)", i.Value)
case parser.ItemKey: case toml.ItemKey:
return fmt.Sprintf("[%s]", i.Value) return fmt.Sprintf("[%s]", i.Value)
case parser.ItemString: case toml.ItemString:
return fmt.Sprintf("STR(%s)", i.Value) return fmt.Sprintf("STR(%s)", i.Value)
case parser.ItemKeyDot: case toml.ItemKeyDot:
return "." return "."
case parser.ItemAssignment: case toml.ItemAssignment:
return "=" return "="
default: default:
panic(fmt.Sprintf("parsekit bug: no string formatting exists for parsekit.Item id %d", i.Type)) panic(fmt.Sprintf("parsekit bug: no string formatting exists for parsekit.Item id %d", i.Type))

View File

@ -32,7 +32,7 @@ var (
keySeparatorDot = c.Seq(c.Opt(a.Whitespace), a.Dot, c.Opt(a.Whitespace)) keySeparatorDot = c.Seq(c.Opt(a.Whitespace), a.Dot, c.Opt(a.Whitespace))
) )
func startKeyValuePair(p *parsekit.P) { func startKeyValuePair(p *parsekit.ParseAPI) {
switch { switch {
case p.On(a.WhitespaceAndNewlines).Skip(): case p.On(a.WhitespaceAndNewlines).Skip():
p.RouteRepeat() 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") p.Expects("a key name")
if p.On(bareKeyRune).Stay() { if p.On(bareKeyRune).Stay() {
p.RouteTo(startBareKey) p.RouteTo(startBareKey)
} }
} }
func startBareKey(p *parsekit.P) { func startBareKey(p *parsekit.ParseAPI) {
p.Expects("a bare key name") p.Expects("a bare key name")
if p.On(bareKey).Accept() { if p.On(bareKey).Accept() {
p.EmitLiteral(ItemKey) 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() { if p.On(keySeparatorDot).Skip() {
p.Emit(ItemKeyDot, ".") p.Emit(ItemKeyDot, ".")
p.RouteTo(startKey) 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") p.Expects("a value assignment")
if p.On(keyAssignment).Skip() { if p.On(keyAssignment).Skip() {
p.Emit(ItemAssignment, "=") p.Emit(ItemAssignment, "=")
@ -79,7 +79,7 @@ func startAssignment(p *parsekit.P) {
// Values must be of the following types: String, Integer, Float, Boolean, // Values must be of the following types: String, Integer, Float, Boolean,
// Datetime, Array, or Inline Table. Unspecified values are invalid. // Datetime, Array, or Inline Table. Unspecified values are invalid.
func startValue(p *parsekit.P) { func startValue(p *parsekit.ParseAPI) {
p.Expects("a value") p.Expects("a value")
if p.On(c.Any(a.SingleQuote, a.DoubleQuote)).Stay() { if p.On(c.Any(a.SingleQuote, a.DoubleQuote)).Stay() {
p.RouteTo(startString) p.RouteTo(startString)

View File

@ -15,8 +15,9 @@ var (
c, a, m = parsekit.C, parsekit.A, parsekit.M c, a, m = parsekit.C, parsekit.A, parsekit.M
) )
// NewParser creates a new parser, using the provided input string var parser = parsekit.NewParser(startKeyValuePair)
// as the data to parse.
func NewParser(input string) *parsekit.Run { // Parse starts the parser for the provided input string.
return parsekit.NewParser(startKeyValuePair).Parse(input) func Parse(input string) *parsekit.ParseRun {
return parser.Parse(input)
} }

View File

@ -3,7 +3,7 @@ package parser_test
import ( import (
"testing" "testing"
parser "git.makaay.nl/mauricem/go-toml" toml "git.makaay.nl/mauricem/go-toml"
) )
func TestEmptyInput(t *testing.T) { func TestEmptyInput(t *testing.T) {
@ -11,7 +11,7 @@ func TestEmptyInput(t *testing.T) {
} }
func TestErrorFullIncludesLineAndRowPosition(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) _, err := parseItemsToArray(p)
actual := err.ErrorFull() actual := err.ErrorFull()
expected := "unexpected character '+' (expected end of file) after line 6, column 3" expected := "unexpected character '+' (expected end of file) after line 6, column 3"

View File

@ -33,7 +33,7 @@ var (
validEscape = c.Any(shortEscape, shortUTF8Escape, longUTF8Escape) validEscape = c.Any(shortEscape, shortUTF8Escape, longUTF8Escape)
) )
func startString(p *parsekit.P) { func startString(p *parsekit.ParseAPI) {
p.Expects("a string value") p.Expects("a string value")
switch { switch {
case p.On(doubleQuote3).Stay(): 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") p.Expects("a basic string")
if p.On(a.DoubleQuote).Skip() { if p.On(a.DoubleQuote).Skip() {
p.RouteTo(parseBasicString).ThenTo(basicStringSpecifics) p.RouteTo(parseBasicString).ThenTo(basicStringSpecifics)
} }
} }
func parseBasicString(p *parsekit.P) { func parseBasicString(p *parsekit.ParseAPI) {
p.Expects("string contents") p.Expects("string contents")
switch { switch {
case p.On(charThatMustBeEscaped).Stay(): 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: // * No additional \escape sequences are allowed. What the spec say about this:
// "All other escape sequences [..] are reserved and, if used, TOML should // "All other escape sequences [..] are reserved and, if used, TOML should
// produce an error."" // produce an error.""
func basicStringSpecifics(p *parsekit.P) { func basicStringSpecifics(p *parsekit.ParseAPI) {
p.Expects("string contents") p.Expects("string contents")
switch { switch {
case p.On(a.DoubleQuote).Skip(): 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") p.Expects("a multi-line basic string")
if p.On(doubleQuote3).Skip() { if p.On(doubleQuote3).Skip() {
p.EmitError("not yet implemented") p.EmitError("not yet implemented")