Moved EOF handling into parsekit and updates for code-compatiblity with latest parsekit version.
This commit is contained in:
parent
6a4a314fee
commit
3e0c9136c3
11
comment.go
11
comment.go
|
@ -6,14 +6,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.
|
||||||
// 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.
|
||||||
var comment = c.Sequence(
|
var comment = c.Seq(
|
||||||
c.Drop(c.OneOrMore(a.Hash)),
|
m.Drop(c.OneOrMore(a.Hash)),
|
||||||
c.Trim(c.ZeroOrMore(c.Not(a.EndOfLine)), " \t"),
|
m.Trim(c.ZeroOrMore(c.Not(a.EndOfLine)), " \t"),
|
||||||
c.Drop(a.EndOfLine))
|
m.Drop(a.EndOfLine))
|
||||||
|
|
||||||
func startComment(p *parsekit.P) {
|
func startComment(p *parsekit.P) {
|
||||||
p.Expects("comment")
|
p.Expects("comment")
|
||||||
if p.On(comment).Accept().RouteReturn().End() {
|
if p.On(comment).Accept().End() {
|
||||||
p.EmitLiteral(ItemComment)
|
p.EmitLiteral(ItemComment)
|
||||||
|
p.RouteReturn()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
eof.go
11
eof.go
|
@ -1,11 +0,0 @@
|
||||||
package parser
|
|
||||||
|
|
||||||
import "git.makaay.nl/mauricem/go-parsekit"
|
|
||||||
|
|
||||||
// TODO move into parsekit
|
|
||||||
func endOfFile(p *parsekit.P) {
|
|
||||||
p.Expects("end of file")
|
|
||||||
if p.On(a.EndOfFile).Stay().End() {
|
|
||||||
p.Emit(parsekit.ItemEOF, "EOF")
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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, a.Equal, optionalWhitespace)
|
keyAssignment = c.Seq(c.Opt(a.Whitespace), a.Equal, c.Opt(a.Whitespace))
|
||||||
|
|
||||||
// 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(a.ASCIILower, a.ASCIIUpper, a.Digit, a.Underscore, a.Minus)
|
bareKeyRune = c.Any(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
|
||||||
|
@ -23,22 +23,22 @@ var (
|
||||||
// Best practice is to use bare keys except when absolutely necessary.
|
// Best practice is to use bare keys except when absolutely necessary.
|
||||||
// A bare key must be non-empty, but an empty quoted key is allowed
|
// A bare key must be non-empty, but an empty quoted key is allowed
|
||||||
// (though discouraged).
|
// (though discouraged).
|
||||||
startOfKey = c.AnyOf(bareKeyRune, anyQuote)
|
startOfKey = c.Any(bareKeyRune, a.SingleQuote, a.DoubleQuote)
|
||||||
|
|
||||||
// Dotted keys are a sequence of bare or quoted keys joined with a dot.
|
// Dotted keys are a sequence of bare or quoted keys joined with a dot.
|
||||||
// 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, a.Dot, optionalWhitespace)
|
keySeparatorDot = c.Seq(c.Opt(a.Whitespace), a.Dot, c.Opt(a.Whitespace))
|
||||||
)
|
)
|
||||||
|
|
||||||
func startKeyValuePair(p *parsekit.P) {
|
func startKeyValuePair(p *parsekit.P) {
|
||||||
switch {
|
switch {
|
||||||
case p.On(a.WhitespaceAndNewlines).Skip().RouteRepeat().End():
|
case p.On(a.WhitespaceAndNewlines).Skip().RouteRep().End():
|
||||||
case p.On(a.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.ExpectEndOfFile()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,5 +76,5 @@ func startAssignment(p *parsekit.P) {
|
||||||
// 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.P) {
|
||||||
p.Expects("a value")
|
p.Expects("a value")
|
||||||
p.On(anyQuote).RouteTo(startString)
|
p.On(c.Any(a.SingleQuote, a.DoubleQuote)).RouteTo(startString)
|
||||||
}
|
}
|
||||||
|
|
4
toml.go
4
toml.go
|
@ -12,9 +12,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
c, a = parsekit.C, parsekit.A
|
c, a, m = parsekit.C, parsekit.A, parsekit.M
|
||||||
optionalWhitespace = c.Optional(a.Whitespace)
|
|
||||||
anyQuote = c.AnyOf(a.SingleQuote, a.DoubleQuote)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// NewParser creates a new parser, using the provided input string
|
// NewParser creates a new parser, using the provided input string
|
||||||
|
|
|
@ -8,12 +8,12 @@ var (
|
||||||
// UTF-8 characters. * Multi-line basic strings are surrounded by three
|
// UTF-8 characters. * Multi-line basic strings are surrounded by three
|
||||||
// quotation marks on each side. * Basic strings are surrounded by
|
// quotation marks on each side. * Basic strings are surrounded by
|
||||||
// quotation marks.
|
// quotation marks.
|
||||||
doubleQuote3 = c.String(`"""`)
|
doubleQuote3 = c.Str(`"""`)
|
||||||
|
|
||||||
// Any Unicode character may be used except those that must be escaped:
|
// Any Unicode character may be used except those that must be escaped:
|
||||||
// quotation mark, backslash, and the control characters (U+0000 to
|
// quotation mark, backslash, and the control characters (U+0000 to
|
||||||
// U+001F, U+007F).
|
// U+001F, U+007F).
|
||||||
charThatMustBeEscaped = c.AnyOf(c.RuneRange('\u0000', '\u001F'), c.Rune('\u007F'))
|
charThatMustBeEscaped = c.Any(c.RuneRange('\u0000', '\u001F'), c.Rune('\u007F'))
|
||||||
|
|
||||||
// For convenience, some popular characters have a compact escape sequence.
|
// For convenience, some popular characters have a compact escape sequence.
|
||||||
//
|
//
|
||||||
|
@ -26,11 +26,11 @@ 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'), a.DoubleQuote, a.Backslash)
|
validEscapeChar = c.Any(c.Runes('b', 't', 'n', 'f', 'r'), a.DoubleQuote, a.Backslash)
|
||||||
shortEscape = c.Sequence(a.Backslash, validEscapeChar)
|
shortEscape = c.Seq(a.Backslash, validEscapeChar)
|
||||||
shortUTF8Escape = c.Sequence(a.Backslash, c.Rune('u'), c.Repeat(4, a.HexDigit))
|
shortUTF8Escape = c.Seq(a.Backslash, c.Rune('u'), c.Rep(4, a.HexDigit))
|
||||||
longUTF8Escape = c.Sequence(a.Backslash, c.Rune('U'), c.Repeat(8, a.HexDigit))
|
longUTF8Escape = c.Seq(a.Backslash, c.Rune('U'), c.Rep(8, a.HexDigit))
|
||||||
validEscape = c.AnyOf(shortEscape, shortUTF8Escape, longUTF8Escape)
|
validEscape = c.Any(shortEscape, shortUTF8Escape, longUTF8Escape)
|
||||||
)
|
)
|
||||||
|
|
||||||
func startString(p *parsekit.P) {
|
func startString(p *parsekit.P) {
|
||||||
|
@ -51,10 +51,10 @@ func parseBasicString(p *parsekit.P) {
|
||||||
switch {
|
switch {
|
||||||
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().RouteRep().End():
|
||||||
case p.On(a.Backslash).RouteReturn().End():
|
case p.On(a.Backslash).RouteReturn().End():
|
||||||
case p.On(a.DoubleQuote).RouteReturn().End():
|
case p.On(a.DoubleQuote).RouteReturn().End():
|
||||||
case p.On(a.AnyRune).Accept().RouteRepeat().End():
|
case p.On(a.AnyRune).Accept().RouteRep().End():
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue