diff --git a/comment.go b/comment.go index 5986ef9..f2529e8 100644 --- a/comment.go +++ b/comment.go @@ -7,15 +7,15 @@ import ( // A '#' hash symbol marks the rest of the line as a comment. func startComment(p *parsekit.P) { 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. func commentContents(p *parsekit.P) { p.Expects("comment contents") switch { - case p.On(endOfLine).Skip().RouteReturn().End(): + case p.On(a.EndOfLine).Skip().RouteReturn().End(): p.EmitLiteralTrim(ItemComment) - case p.On(anyRune).Accept().RouteRepeat().End(): + case p.On(a.AnyRune).Accept().RouteRepeat().End(): } } diff --git a/eof.go b/eof.go index 36f6934..08ffdb3 100644 --- a/eof.go +++ b/eof.go @@ -5,7 +5,7 @@ import "git.makaay.nl/mauricem/go-parsekit" // TODO move into parsekit func endOfFile(p *parsekit.P) { p.Expects("end of file") - if p.On(c.EndOfFile()).Stay().End() { + if p.On(a.EndOfFile).Stay().End() { p.Emit(parsekit.ItemEOF, "EOF") } } diff --git a/helpers_test.go b/helpers_test.go index 9b7065e..21527ae 100644 --- a/helpers_test.go +++ b/helpers_test.go @@ -6,7 +6,7 @@ import ( "testing" "git.makaay.nl/mauricem/go-parsekit" - "git.makaay.nl/mauricem/go-toml" + parser "git.makaay.nl/mauricem/go-toml" ) type statesT struct { diff --git a/keyvaluepair.go b/keyvaluepair.go index d55c95c..3d27e21 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, equal, optionalWhitespace) + keyAssignment = c.Sequence(optionalWhitespace, a.Equal, optionalWhitespace) // 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(asciiLower, asciiUpper, digit, underscore, dash) + bareKeyRune = c.AnyOf(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 @@ -29,13 +29,13 @@ var ( // 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, dot, optionalWhitespace) + keySeparatorDot = c.Sequence(optionalWhitespace, a.Dot, optionalWhitespace) ) func startKeyValuePair(p *parsekit.P) { switch { - case p.On(whitespaceOrNewlines).Skip().RouteRepeat().End(): - case p.On(hash).RouteTo(startComment).ThenReturnHere().End(): + case p.On(a.WhitespaceAndNewlines).Skip().RouteRepeat().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. diff --git a/toml.go b/toml.go index a5f95c5..8c37be6 100644 --- a/toml.go +++ b/toml.go @@ -12,28 +12,9 @@ const ( ) var ( - c = parsekit.C - space = parsekit.MatchRune(' ') - tab = parsekit.MatchRune('\t') - 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()) + c, a = parsekit.C, parsekit.A + optionalWhitespace = c.Optional(a.Whitespace) + anyQuote = c.AnyOf(a.SingleQuote, a.DoubleQuote) ) // NewParser creates a new parser, using the provided input string diff --git a/toml_test.go b/toml_test.go index 3799c91..4dbdd87 100644 --- a/toml_test.go +++ b/toml_test.go @@ -3,7 +3,7 @@ package parser_test import ( "testing" - "git.makaay.nl/mauricem/go-toml" + parser "git.makaay.nl/mauricem/go-toml" ) func TestEmptyInput(t *testing.T) { diff --git a/value_string.go b/value_string.go index 6f063cc..e68cb76 100644 --- a/value_string.go +++ b/value_string.go @@ -26,11 +26,10 @@ var ( // \\ - backslash (U+005C) // \uXXXX - unicode (U+XXXX) // \UXXXXXXXX - unicode (U+XXXXXXXX) - validEscapeChar = c.AnyOf(c.Runes('b', 't', 'n', 'f', 'r'), doubleQuote, backslash) - shortEscape = c.Sequence(backslash, validEscapeChar) - hex = c.AnyOf(digit, c.RuneRange('a', 'f'), c.RuneRange('A', 'F')) - shortUtf8Escape = c.Sequence(backslash, c.Rune('u'), c.Repeat(4, hex)) - longUtf8Escape = c.Sequence(backslash, c.Rune('U'), c.Repeat(8, hex)) + 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) ) @@ -38,13 +37,13 @@ func startString(p *parsekit.P) { p.Expects("a string value") switch { 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) { 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) { @@ -53,9 +52,9 @@ func parseBasicString(p *parsekit.P) { 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(backslash).RouteReturn().End(): - case p.On(doubleQuote).RouteReturn().End(): - case p.On(anyRune).Accept().RouteRepeat().End(): + case p.On(a.Backslash).RouteReturn().End(): + case p.On(a.DoubleQuote).RouteReturn().End(): + case p.On(a.AnyRune).Accept().RouteRepeat().End(): } } @@ -67,13 +66,13 @@ func parseBasicString(p *parsekit.P) { func basicStringSpecifics(p *parsekit.P) { p.Expects("string contents") switch { - case p.On(doubleQuote).Skip().End(): + case p.On(a.DoubleQuote).Skip().End(): if err := p.EmitInterpreted(ItemString); err != nil { // TODO testcase? p.EmitError("invalid data in string: %s", err) } else { p.RouteTo(startKeyValuePair) } - case p.On(backslash).End(): + case p.On(a.Backslash).End(): p.EmitError("invalid escape sequence") } }