package toml import ( "git.makaay.nl/mauricem/go-parsekit/parse" ) // Arrays are square brackets with values inside. Whitespace is ignored. // Elements are separated by commas. Data types may not be mixed (different // ways to define strings should be considered the same type, and so should // arrays with different element types). // // arr1 = [ 1, 2, 3 ] // arr2 = [ "red", "yellow", "green" ] // arr3 = [ [ 1, 2 ], [3, 4, 5] ] // arr4 = [ "all", 'strings', """are the same""", '''type'''] // arr5 = [ [ 1, 2 ], ["a", "b", "c"] ] // // arr6 = [ 1, 2.0 ] # INVALID // // Arrays can also be multiline. A terminating comma (also called trailing // comma) is ok after the last value of the array. There can be an arbitrary // number of newlines and comments before a value and before the closing bracket. // // arr7 = [ // 1, 2, 3 // ] // // arr8 = [ // 1, // 2, # this is ok // ] var ( arraySpace = c.ZeroOrMore(c.Any(blank, newline, comment)) arrayOpen = a.SquareOpen.Then(arraySpace) arraySeparator = c.Seq(arraySpace, a.Comma, arraySpace) arrayClose = c.Seq(c.Optional(arraySpace.Then(a.Comma)), arraySpace, a.SquareClose) ) func (t *parser) startArray(p *parse.API) { // Check for the start of the array. if !p.Accept(arrayOpen) { p.Expected("an array") return } items := []item{} // Check for an empty array. if p.Accept(arrayClose) { t.addParsedItem(pStaticArray, items) return } // Not an empty array, parse the items. for { // Check for a valid item. if !p.Handle(t.startValue) { return } // Pop the item from the value parsing and append it to the array items. parseItems, item := t.Items[0:len(t.Items)-1], t.Items[len(t.Items)-1] t.Items = parseItems // Data types may not be mixed (different ways to define strings should be // considered the same type, and so should arrays with different element types). if len(items) > 0 && item.Type != items[0].Type { p.Error("type mismatch in array of %ss: found an item of type %s", items[0].Type, item.Type) return } items = append(items, item) // Check for the end of the array. if p.Accept(arrayClose) { t.addParsedItem(pStaticArray, items) return } // Not the end of the array? Then we should find an array separator. if !p.Accept(arraySeparator) { p.Expected("an array separator") return } } }