forked from naoina/toml
-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.go
54 lines (45 loc) · 890 Bytes
/
parse.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package toml
import (
"fmt"
"github.com/naoina/toml/ast"
)
// Given TOML, return an AST representation. The toplevel is represented
// by a table.
func Parse(data []byte) (*ast.Table, error) {
d := &parseState{p: &tomlParser{Buffer: string(data)}}
d.init()
if err := d.parse(); err != nil {
return nil, err
}
return d.p.toml.table, nil
}
type parseState struct {
p *tomlParser
}
func (d *parseState) init() {
d.p.Init()
d.p.toml.init()
}
func (d *parseState) parse() error {
if err := d.p.Parse(); err != nil {
if err, ok := err.(*parseError); ok {
return fmt.Errorf("toml: line %d: parse error", err.Line())
}
return err
}
return d.execute()
}
func (d *parseState) execute() (err error) {
defer func() {
e := recover()
if e != nil {
cerr, ok := e.(convertError)
if !ok {
panic(e)
}
err = cerr.err
}
}()
d.p.Execute()
return nil
}