From 3e0c9136c36aede156646252e07e99b6bf5ab0fc Mon Sep 17 00:00:00 2001 From: Maurice Makaay Date: Fri, 24 May 2019 13:58:00 +0000 Subject: [PATCH] Moved EOF handling into parsekit and updates for code-compatiblity with latest parsekit version. --- comment.go | 11 ++++++----- eof.go | 11 ----------- keyvaluepair.go | 14 +++++++------- toml.go | 4 +--- value_string.go | 18 +++++++++--------- 5 files changed, 23 insertions(+), 35 deletions(-) delete mode 100644 eof.go diff --git a/comment.go b/comment.go index 3245cbb..020c3f0 100644 --- a/comment.go +++ b/comment.go @@ -6,14 +6,15 @@ import ( // 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. -var comment = c.Sequence( - c.Drop(c.OneOrMore(a.Hash)), - c.Trim(c.ZeroOrMore(c.Not(a.EndOfLine)), " \t"), - c.Drop(a.EndOfLine)) +var comment = c.Seq( + m.Drop(c.OneOrMore(a.Hash)), + m.Trim(c.ZeroOrMore(c.Not(a.EndOfLine)), " \t"), + m.Drop(a.EndOfLine)) func startComment(p *parsekit.P) { p.Expects("comment") - if p.On(comment).Accept().RouteReturn().End() { + if p.On(comment).Accept().End() { p.EmitLiteral(ItemComment) + p.RouteReturn() } } diff --git a/eof.go b/eof.go deleted file mode 100644 index 08ffdb3..0000000 --- a/eof.go +++ /dev/null @@ -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") - } -} diff --git a/keyvaluepair.go b/keyvaluepair.go index 3d27e21..7e70318 100644 --- a/keyvaluepair.go +++ b/keyvaluepair.go @@ -9,13 +9,13 @@ var ( // 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 // 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 // contain ASCII letters, ASCII digits, underscores, and dashes // (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. - 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) // 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. // A bare key must be non-empty, but an empty quoted key is allowed // (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. // This allows for grouping similar properties together. Whitespace // around dot-separated parts is ignored, however, best practice is to // 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) { 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(startOfKey).RouteTo(startKey).End(): 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. func startValue(p *parsekit.P) { p.Expects("a value") - p.On(anyQuote).RouteTo(startString) + p.On(c.Any(a.SingleQuote, a.DoubleQuote)).RouteTo(startString) } diff --git a/toml.go b/toml.go index 564d0b8..5b16206 100644 --- a/toml.go +++ b/toml.go @@ -12,9 +12,7 @@ const ( ) var ( - c, a = parsekit.C, parsekit.A - optionalWhitespace = c.Optional(a.Whitespace) - anyQuote = c.AnyOf(a.SingleQuote, a.DoubleQuote) + c, a, m = parsekit.C, parsekit.A, parsekit.M ) // NewParser creates a new parser, using the provided input string diff --git a/value_string.go b/value_string.go index c066e4e..80862e8 100644 --- a/value_string.go +++ b/value_string.go @@ -8,12 +8,12 @@ var ( // UTF-8 characters. * Multi-line basic strings are surrounded by three // quotation marks on each side. * Basic strings are surrounded by // quotation marks. - doubleQuote3 = c.String(`"""`) + doubleQuote3 = c.Str(`"""`) // Any Unicode character may be used except those that must be escaped: // quotation mark, backslash, and the control characters (U+0000 to // 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. // @@ -26,11 +26,11 @@ var ( // \\ - backslash (U+005C) // \uXXXX - unicode (U+XXXX) // \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) + validEscapeChar = c.Any(c.Runes('b', 't', 'n', 'f', 'r'), a.DoubleQuote, a.Backslash) + shortEscape = c.Seq(a.Backslash, validEscapeChar) + shortUTF8Escape = c.Seq(a.Backslash, c.Rune('u'), c.Rep(4, a.HexDigit)) + longUTF8Escape = c.Seq(a.Backslash, c.Rune('U'), c.Rep(8, a.HexDigit)) + validEscape = c.Any(shortEscape, shortUTF8Escape, longUTF8Escape) ) func startString(p *parsekit.P) { @@ -51,10 +51,10 @@ func parseBasicString(p *parsekit.P) { switch { case p.On(charThatMustBeEscaped).End(): 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.DoubleQuote).RouteReturn().End(): - case p.On(a.AnyRune).Accept().RouteRepeat().End(): + case p.On(a.AnyRune).Accept().RouteRep().End(): } }