go-toml/parser/parser.go

44 lines
1.6 KiB
Go

package parser
import "github.com/mmakaay/toml/parsekit"
// Item types that are produced by this parser.
const (
ItemComment parsekit.ItemType = iota // Comment string
ItemKey // Key of a key/value pair
ItemKeyDot // Dot for a dotted key
ItemAssignment // Value assignment coming up (=)
ItemString // A value of type string
)
var (
c = parsekit.C
space = c.Rune(' ')
tab = c.Rune('\t')
carriageReturn = c.Rune('\r')
lineFeed = c.Rune('\n')
hash = c.Rune('#')
underscore = c.Rune('_')
dash = c.Rune('-')
equal = c.Rune('=')
dot = c.Rune('.')
singleQuote = c.Rune('\'')
doubleQuote = c.Rune('"')
any = c.Any()
anyQuote = c.AnyOf(singleQuote, doubleQuote)
backslash = c.Rune('\\')
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.Rune(parsekit.EOF))
)
// NewParser creates a new parser, using the provided input string
// as the data to parse.
func NewParser(input string) *parsekit.P {
return parsekit.New(input, startKeyValuePair)
}