From 44f7a7aead83e3d5e0701c77d13432e2be9b0e7a Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Mon, 1 Feb 2021 21:41:34 -0500 Subject: [PATCH] Inline tables --- toml.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++ toml_test.go | 5 ++++ 2 files changed, 90 insertions(+) diff --git a/toml.go b/toml.go index 1e9aec2..ea6db52 100644 --- a/toml.go +++ b/toml.go @@ -30,6 +30,9 @@ type builder interface { ArrayBegin() ArrayEnd() ArraySeparator() + InlineTableBegin() + InlineTableEnd() + InlineTableSeparator() } type position struct { @@ -41,6 +44,18 @@ type documentBuilder struct { document Document } +func (d *documentBuilder) InlineTableSeparator() { + fmt.Println(", InlineTable SEPARATOR") +} + +func (d *documentBuilder) InlineTableBegin() { + fmt.Println("{ InlineTable BEGIN") +} + +func (d *documentBuilder) InlineTableEnd() { + fmt.Println("} InlineTable END") +} + func (d *documentBuilder) ArraySeparator() { fmt.Println(", ARRAY SEPARATOR") } @@ -358,12 +373,81 @@ func (p *parser) parseVal() error { return p.parseString() case '[': return p.parseArray() + case '{': + return p.parseInlineTable() // TODO default: return &InvalidCharacter{r: r} } } +func (p *parser) parseInlineTable() error { + //inline-table = inline-table-open [ inline-table-keyvals ] inline-table-close + // + //inline-table-open = %x7B ws ; { + // inline-table-close = ws %x7D ; } + //inline-table-sep = ws %x2C ws ; , Comma + // + //inline-table-keyvals = keyval [ inline-table-sep inline-table-keyvals ] + + err := p.expect('{') + if err != nil { + panic("inline tables should start with {") + } + p.ignore() + p.builder.InlineTableBegin() + + err = p.parseWhitespace() + if err != nil { + return err + } + + r := p.peek() + if r == '}' { + p.next() + p.ignore() + p.builder.InlineTableEnd() + return nil + } + + err = p.parseKeyval() + if err != nil { + return err + } + + for { + err = p.parseWhitespace() + if err != nil { + return err + } + + r := p.peek() + if r == '}' { + p.next() + p.ignore() + p.builder.InlineTableEnd() + return nil + } + + err := p.expect(',') + if err != nil { + return err + } + p.builder.InlineTableSeparator() + p.ignore() + + err = p.parseWhitespace() + if err != nil { + return err + } + + err = p.parseKeyval() + if err != nil { + return err + } + } +} + func (p *parser) parseArray() error { //array = array-open [ array-values ] ws-comment-newline array-close @@ -371,6 +455,7 @@ func (p *parser) parseArray() error { if err != nil { panic("arrays should start with [") } + p.ignore() p.builder.ArrayBegin() diff --git a/toml_test.go b/toml_test.go index 1563fce..3bddd1e 100644 --- a/toml_test.go +++ b/toml_test.go @@ -29,6 +29,8 @@ var inputs = []string{ `c = [[[]]]`, `d = ["foo","bar"]`, `d = ["foo", "test"]`, + `d = {}`, + `e = {f = "bar"}`, } func TestParse(t *testing.T) { @@ -45,6 +47,9 @@ func TestParse(t *testing.T) { type noopBuilder struct { } +func (n noopBuilder) InlineTableSeparator() {} +func (n noopBuilder) InlineTableBegin() {} +func (n noopBuilder) InlineTableEnd() {} func (n noopBuilder) ArraySeparator() {} func (n noopBuilder) ArrayBegin() {} func (n noopBuilder) ArrayEnd() {}