Skip to content

Commit

Permalink
Basic parser for inline tables
Browse files Browse the repository at this point in the history
  • Loading branch information
pelletier committed Sep 9, 2015
1 parent 71813ed commit 7df5a95
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 0 deletions.
52 changes: 52 additions & 0 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,8 @@ func (p *tomlParser) parseRvalue() interface{} {
return val
case tokenLeftBracket:
return p.parseArray()
case tokenLeftCurlyBrace:
return p.parseInlineTable()
case tokenError:
p.raiseError(tok, "%s", tok)
}
Expand All @@ -253,6 +255,56 @@ func (p *tomlParser) parseRvalue() interface{} {
return nil
}

func tokenIsComma(t *token) bool {
return t != nil && t.typ == tokenComma
}

func (p *tomlParser) parseInlineTable() *TomlTree {
println("parsing inline table")
tree := newTomlTree()
var previous *token
Loop:
for {
follow := p.peek()
if follow == nil || follow.typ == tokenEOF {
p.raiseError(follow, "unterminated inline table")
}
println("token type:", follow.typ.String())
switch follow.typ {
case tokenRightCurlyBrace:
p.getToken()
break Loop
case tokenKey:
if !tokenIsComma(previous) && previous != nil {
p.raiseError(follow, "comma expected between fields in inline table")
}
key := p.getToken()
p.assume(tokenEqual)
value := p.parseRvalue()
println("got key:", key.val)
println("got rvalue:", value)
println("got rvalue type:", reflect.TypeOf(value))
tree.Set(key.val, value)
case tokenComma:
if previous == nil {
p.raiseError(follow, "inline table cannot start with a comma")
}
if tokenIsComma(previous) {
p.raiseError(follow, "need field between two commas in inline table")
}
p.getToken()
default:
p.raiseError(follow, "unexpected token type in inline table: %s", follow.typ.String())
}
println("assigning previous to follow")
previous = follow
}
if tokenIsComma(previous) {
p.raiseError(previous, "trailing comma at the end of inline table")
}
return tree
}

func (p *tomlParser) parseArray() []interface{} {
var array []interface{}
arrayType := reflect.TypeOf(nil)
Expand Down
34 changes: 34 additions & 0 deletions parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,40 @@ func TestArrayWithExtraCommaComment(t *testing.T) {
})
}

func TestSimpleInlineGroup(t *testing.T) {
tree, err := Load("key = {a = 42}")
assertTree(t, tree, err, map[string]interface{}{
"key": map[string]interface{}{
"a": int64(42),
},
})
}

func TestDoubleInlineGroup(t *testing.T) {
tree, err := Load("key = {a = 42, b = \"foo\"}")
assertTree(t, tree, err, map[string]interface{}{
"key": map[string]interface{}{
"a": int64(42),
"b": "foo",
},
})
}

func TestExampleInlineGroup(t *testing.T) {
tree, err := Load(`name = { first = "Tom", last = "Preston-Werner" }
point = { x = 1, y = 2 }`)
assertTree(t, tree, err, map[string]interface{}{
"name": map[string]interface{}{
"first": "Tom",
"last": "Preston-Werner",
},
"point": map[string]interface{}{
"x": int64(1),
"y": int64(2),
},
})
}

func TestDuplicateGroups(t *testing.T) {
_, err := Load("[foo]\na=2\n[foo]b=3")
if err.Error() != "(3, 2): duplicated tables" {
Expand Down

0 comments on commit 7df5a95

Please sign in to comment.