From aae4656c6462e877e940561e9f5704d6b97bf7d5 Mon Sep 17 00:00:00 2001 From: Thomas Pelletier Date: Mon, 1 Feb 2021 22:03:53 -0500 Subject: [PATCH] Standard Table --- toml.go | 89 ++++++++++++++++++++++++++++++++++++++++++++++++---- toml_test.go | 7 +++++ 2 files changed, 90 insertions(+), 6 deletions(-) diff --git a/toml.go b/toml.go index ea6db52..07b89f5 100644 --- a/toml.go +++ b/toml.go @@ -33,6 +33,8 @@ type builder interface { InlineTableBegin() InlineTableEnd() InlineTableSeparator() + StandardTableBegin() + StandardTableEnd() } type position struct { @@ -44,6 +46,14 @@ type documentBuilder struct { document Document } +func (d *documentBuilder) StandardTableBegin() { + fmt.Println("STD-TABLE[") +} + +func (d *documentBuilder) StandardTableEnd() { + fmt.Println("STD-TABLE]") +} + func (d *documentBuilder) InlineTableSeparator() { fmt.Println(", InlineTable SEPARATOR") } @@ -290,6 +300,10 @@ func (p *parser) parseRequiredNewline() error { } func (p *parser) parseExpression() error { + //expression = ws [ comment ] + //expression =/ ws keyval ws [ comment ] + //expression =/ ws table ws [ comment ] + err := p.parseWhitespace() if err != nil { return err @@ -305,12 +319,11 @@ func (p *parser) parseExpression() error { // or line with something? if r == '[' { // parse table. could be either a standard table or an array table - // TODO - } - - // it has to be a keyval - - if isUnquotedKeyRune(r) || r == '\'' || r == '"' { + err := p.parseTable() + if err != nil { + return err + } + } else if isUnquotedKeyRune(r) || r == '\'' || r == '"' { err := p.parseKeyval() if err != nil { return err @@ -844,3 +857,67 @@ func (p *parser) parseBasicString() error { } } } + +func (p *parser) parseTable() error { + //;; Table + // + //table = std-table / array-table + // + //;; Standard Table + // + //std-table = std-table-open key std-table-close + // + //std-table-open = %x5B ws ; [ Left square bracket + //std-table-close = ws %x5D ; ] Right square bracket + // + //;; Array Table + // + //array-table = array-table-open key array-table-close + // + //array-table-open = %x5B.5B ws ; [[ Double left square bracket + //array-table-close = ws %x5D.5D ; ]] Double right square bracket + + if p.follows("[[") { + panic("TODO") // TODO: array-table + } + + return p.parseStandardTable() +} + +func (p *parser) parseStandardTable() error { + //;; Standard Table + // + //std-table = std-table-open key std-table-close + // + //std-table-open = %x5B ws ; [ Left square bracket + //std-table-close = ws %x5D ; ] Right square bracket + + err := p.expect('[') + if err != nil { + panic("std-table should start with [") + } + p.ignore() + p.builder.StandardTableBegin() + + err = p.parseWhitespace() + if err != nil { + return err + } + + err = p.parseKey() + if err != nil { + return err + } + + err = p.parseWhitespace() + if err != nil { + return err + } + err = p.expect(']') + if err != nil { + return err + } + p.ignore() + p.builder.StandardTableEnd() + return nil +} diff --git a/toml_test.go b/toml_test.go index 3bddd1e..168573b 100644 --- a/toml_test.go +++ b/toml_test.go @@ -31,6 +31,11 @@ var inputs = []string{ `d = ["foo", "test"]`, `d = {}`, `e = {f = "bar"}`, + `[foo]`, + `[ test ]`, + `[ "hello".world ]`, + `[test] +a = false`, } func TestParse(t *testing.T) { @@ -47,6 +52,8 @@ func TestParse(t *testing.T) { type noopBuilder struct { } +func (n noopBuilder) StandardTableBegin() {} +func (n noopBuilder) StandardTableEnd() {} func (n noopBuilder) InlineTableSeparator() {} func (n noopBuilder) InlineTableBegin() {} func (n noopBuilder) InlineTableEnd() {}