Compatibility with new atoms in parsekit.
This commit is contained in:
parent
9a13e0dd7a
commit
234bbdf30f
|
@ -7,15 +7,15 @@ import (
|
||||||
// A '#' hash symbol marks the rest of the line as a comment.
|
// A '#' hash symbol marks the rest of the line as a comment.
|
||||||
func startComment(p *parsekit.P) {
|
func startComment(p *parsekit.P) {
|
||||||
p.Expects("start of comment")
|
p.Expects("start of comment")
|
||||||
p.On(c.OneOrMore(hash)).Skip().RouteTo(commentContents)
|
p.On(c.OneOrMore(a.Hash)).Skip().RouteTo(commentContents)
|
||||||
}
|
}
|
||||||
|
|
||||||
// All characters up to the end of the line are included in the comment.
|
// All characters up to the end of the line are included in the comment.
|
||||||
func commentContents(p *parsekit.P) {
|
func commentContents(p *parsekit.P) {
|
||||||
p.Expects("comment contents")
|
p.Expects("comment contents")
|
||||||
switch {
|
switch {
|
||||||
case p.On(endOfLine).Skip().RouteReturn().End():
|
case p.On(a.EndOfLine).Skip().RouteReturn().End():
|
||||||
p.EmitLiteralTrim(ItemComment)
|
p.EmitLiteralTrim(ItemComment)
|
||||||
case p.On(anyRune).Accept().RouteRepeat().End():
|
case p.On(a.AnyRune).Accept().RouteRepeat().End():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
2
eof.go
2
eof.go
|
@ -5,7 +5,7 @@ import "git.makaay.nl/mauricem/go-parsekit"
|
||||||
// TODO move into parsekit
|
// TODO move into parsekit
|
||||||
func endOfFile(p *parsekit.P) {
|
func endOfFile(p *parsekit.P) {
|
||||||
p.Expects("end of file")
|
p.Expects("end of file")
|
||||||
if p.On(c.EndOfFile()).Stay().End() {
|
if p.On(a.EndOfFile).Stay().End() {
|
||||||
p.Emit(parsekit.ItemEOF, "EOF")
|
p.Emit(parsekit.ItemEOF, "EOF")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.makaay.nl/mauricem/go-parsekit"
|
"git.makaay.nl/mauricem/go-parsekit"
|
||||||
"git.makaay.nl/mauricem/go-toml"
|
parser "git.makaay.nl/mauricem/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
type statesT struct {
|
type statesT struct {
|
||||||
|
|
|
@ -9,13 +9,13 @@ var (
|
||||||
// Whitespace is ignored around key names and values. The key, equals
|
// Whitespace is ignored around key names and values. The key, equals
|
||||||
// sign, and value must be on the same line (though some values can be
|
// sign, and value must be on the same line (though some values can be
|
||||||
// broken over multiple lines).
|
// broken over multiple lines).
|
||||||
keyAssignment = c.Sequence(optionalWhitespace, equal, optionalWhitespace)
|
keyAssignment = c.Sequence(optionalWhitespace, a.Equal, optionalWhitespace)
|
||||||
|
|
||||||
// A key may be either bare, quoted or dotted. Bare keys may only
|
// A key may be either bare, quoted or dotted. Bare keys may only
|
||||||
// contain ASCII letters, ASCII digits, underscores, and dashes
|
// contain ASCII letters, ASCII digits, underscores, and dashes
|
||||||
// (A-Za-z0-9_-). Note that bare keys are allowed to be composed of only
|
// (A-Za-z0-9_-). Note that bare keys are allowed to be composed of only
|
||||||
// ASCII digits, e.g. 1234, but are always interpreted as strings.
|
// ASCII digits, e.g. 1234, but are always interpreted as strings.
|
||||||
bareKeyRune = c.AnyOf(asciiLower, asciiUpper, digit, underscore, dash)
|
bareKeyRune = c.AnyOf(a.ASCIILower, a.ASCIIUpper, a.Digit, a.Underscore, a.Minus)
|
||||||
bareKey = c.OneOrMore(bareKeyRune)
|
bareKey = c.OneOrMore(bareKeyRune)
|
||||||
|
|
||||||
// Quoted keys follow the exact same rules as either basic strings or
|
// Quoted keys follow the exact same rules as either basic strings or
|
||||||
|
@ -29,13 +29,13 @@ var (
|
||||||
// This allows for grouping similar properties together. Whitespace
|
// This allows for grouping similar properties together. Whitespace
|
||||||
// around dot-separated parts is ignored, however, best practice is to
|
// around dot-separated parts is ignored, however, best practice is to
|
||||||
// not use any extraneous whitespace.
|
// not use any extraneous whitespace.
|
||||||
keySeparatorDot = c.Sequence(optionalWhitespace, dot, optionalWhitespace)
|
keySeparatorDot = c.Sequence(optionalWhitespace, a.Dot, optionalWhitespace)
|
||||||
)
|
)
|
||||||
|
|
||||||
func startKeyValuePair(p *parsekit.P) {
|
func startKeyValuePair(p *parsekit.P) {
|
||||||
switch {
|
switch {
|
||||||
case p.On(whitespaceOrNewlines).Skip().RouteRepeat().End():
|
case p.On(a.WhitespaceAndNewlines).Skip().RouteRepeat().End():
|
||||||
case p.On(hash).RouteTo(startComment).ThenReturnHere().End():
|
case p.On(a.Hash).RouteTo(startComment).ThenReturnHere().End():
|
||||||
case p.On(startOfKey).RouteTo(startKey).End():
|
case p.On(startOfKey).RouteTo(startKey).End():
|
||||||
default:
|
default:
|
||||||
p.RouteTo(endOfFile) // TODO Make end of file a Matcher, so this can be simpler.
|
p.RouteTo(endOfFile) // TODO Make end of file a Matcher, so this can be simpler.
|
||||||
|
|
25
toml.go
25
toml.go
|
@ -12,28 +12,9 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
c = parsekit.C
|
c, a = parsekit.C, parsekit.A
|
||||||
space = parsekit.MatchRune(' ')
|
optionalWhitespace = c.Optional(a.Whitespace)
|
||||||
tab = parsekit.MatchRune('\t')
|
anyQuote = c.AnyOf(a.SingleQuote, a.DoubleQuote)
|
||||||
carriageReturn = parsekit.MatchRune('\r')
|
|
||||||
lineFeed = parsekit.MatchRune('\n')
|
|
||||||
hash = parsekit.MatchRune('#')
|
|
||||||
underscore = parsekit.MatchRune('_')
|
|
||||||
dash = parsekit.MatchRune('-')
|
|
||||||
equal = parsekit.MatchRune('=')
|
|
||||||
dot = parsekit.MatchRune('.')
|
|
||||||
singleQuote = parsekit.MatchRune('\'')
|
|
||||||
doubleQuote = parsekit.MatchRune('"')
|
|
||||||
anyRune = c.AnyRune()
|
|
||||||
anyQuote = c.AnyOf(singleQuote, doubleQuote)
|
|
||||||
backslash = parsekit.MatchRune('\\')
|
|
||||||
asciiLower = c.RuneRange('a', 'z')
|
|
||||||
asciiUpper = c.RuneRange('A', 'Z')
|
|
||||||
digit = c.RuneRange('0', '9')
|
|
||||||
whitespace = c.OneOrMore(c.AnyOf(space, tab))
|
|
||||||
whitespaceOrNewlines = c.OneOrMore(c.AnyOf(space, tab, carriageReturn, lineFeed))
|
|
||||||
optionalWhitespace = c.Optional(whitespace)
|
|
||||||
endOfLine = c.AnyOf(lineFeed, c.EndOfFile())
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewParser creates a new parser, using the provided input string
|
// NewParser creates a new parser, using the provided input string
|
||||||
|
|
|
@ -3,7 +3,7 @@ package parser_test
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.makaay.nl/mauricem/go-toml"
|
parser "git.makaay.nl/mauricem/go-toml"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestEmptyInput(t *testing.T) {
|
func TestEmptyInput(t *testing.T) {
|
||||||
|
|
|
@ -26,11 +26,10 @@ var (
|
||||||
// \\ - backslash (U+005C)
|
// \\ - backslash (U+005C)
|
||||||
// \uXXXX - unicode (U+XXXX)
|
// \uXXXX - unicode (U+XXXX)
|
||||||
// \UXXXXXXXX - unicode (U+XXXXXXXX)
|
// \UXXXXXXXX - unicode (U+XXXXXXXX)
|
||||||
validEscapeChar = c.AnyOf(c.Runes('b', 't', 'n', 'f', 'r'), doubleQuote, backslash)
|
validEscapeChar = c.AnyOf(c.Runes('b', 't', 'n', 'f', 'r'), a.DoubleQuote, a.Backslash)
|
||||||
shortEscape = c.Sequence(backslash, validEscapeChar)
|
shortEscape = c.Sequence(a.Backslash, validEscapeChar)
|
||||||
hex = c.AnyOf(digit, c.RuneRange('a', 'f'), c.RuneRange('A', 'F'))
|
shortUtf8Escape = c.Sequence(a.Backslash, c.Rune('u'), c.Repeat(4, a.HexDigit))
|
||||||
shortUtf8Escape = c.Sequence(backslash, c.Rune('u'), c.Repeat(4, hex))
|
longUtf8Escape = c.Sequence(a.Backslash, c.Rune('U'), c.Repeat(8, a.HexDigit))
|
||||||
longUtf8Escape = c.Sequence(backslash, c.Rune('U'), c.Repeat(8, hex))
|
|
||||||
validEscape = c.AnyOf(shortEscape, shortUtf8Escape, longUtf8Escape)
|
validEscape = c.AnyOf(shortEscape, shortUtf8Escape, longUtf8Escape)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -38,13 +37,13 @@ func startString(p *parsekit.P) {
|
||||||
p.Expects("a string value")
|
p.Expects("a string value")
|
||||||
switch {
|
switch {
|
||||||
case p.On(doubleQuote3).RouteTo(startMultiLineBasicString).End():
|
case p.On(doubleQuote3).RouteTo(startMultiLineBasicString).End():
|
||||||
case p.On(doubleQuote).RouteTo(startBasicString).End():
|
case p.On(a.DoubleQuote).RouteTo(startBasicString).End():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func startBasicString(p *parsekit.P) {
|
func startBasicString(p *parsekit.P) {
|
||||||
p.Expects("a basic string")
|
p.Expects("a basic string")
|
||||||
p.On(doubleQuote).Skip().RouteTo(parseBasicString).ThenTo(basicStringSpecifics)
|
p.On(a.DoubleQuote).Skip().RouteTo(parseBasicString).ThenTo(basicStringSpecifics)
|
||||||
}
|
}
|
||||||
|
|
||||||
func parseBasicString(p *parsekit.P) {
|
func parseBasicString(p *parsekit.P) {
|
||||||
|
@ -53,9 +52,9 @@ func parseBasicString(p *parsekit.P) {
|
||||||
case p.On(charThatMustBeEscaped).End():
|
case p.On(charThatMustBeEscaped).End():
|
||||||
p.EmitError("invalid character in basic string: %q (must be escaped)", p.LastMatch)
|
p.EmitError("invalid character in basic string: %q (must be escaped)", p.LastMatch)
|
||||||
case p.On(validEscape).Accept().RouteRepeat().End():
|
case p.On(validEscape).Accept().RouteRepeat().End():
|
||||||
case p.On(backslash).RouteReturn().End():
|
case p.On(a.Backslash).RouteReturn().End():
|
||||||
case p.On(doubleQuote).RouteReturn().End():
|
case p.On(a.DoubleQuote).RouteReturn().End():
|
||||||
case p.On(anyRune).Accept().RouteRepeat().End():
|
case p.On(a.AnyRune).Accept().RouteRepeat().End():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,13 +66,13 @@ func parseBasicString(p *parsekit.P) {
|
||||||
func basicStringSpecifics(p *parsekit.P) {
|
func basicStringSpecifics(p *parsekit.P) {
|
||||||
p.Expects("string contents")
|
p.Expects("string contents")
|
||||||
switch {
|
switch {
|
||||||
case p.On(doubleQuote).Skip().End():
|
case p.On(a.DoubleQuote).Skip().End():
|
||||||
if err := p.EmitInterpreted(ItemString); err != nil { // TODO testcase?
|
if err := p.EmitInterpreted(ItemString); err != nil { // TODO testcase?
|
||||||
p.EmitError("invalid data in string: %s", err)
|
p.EmitError("invalid data in string: %s", err)
|
||||||
} else {
|
} else {
|
||||||
p.RouteTo(startKeyValuePair)
|
p.RouteTo(startKeyValuePair)
|
||||||
}
|
}
|
||||||
case p.On(backslash).End():
|
case p.On(a.Backslash).End():
|
||||||
p.EmitError("invalid escape sequence")
|
p.EmitError("invalid escape sequence")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue