-
Notifications
You must be signed in to change notification settings - Fork 0
/
toml_parser.go
82 lines (72 loc) · 1.93 KB
/
toml_parser.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package toml
import (
"github.com/alecthomas/kong"
"github.com/alecthomas/participle"
"github.com/alecthomas/participle/lexer"
"github.com/alecthomas/participle/lexer/ebnf"
"github.com/alecthomas/repr"
"os"
)
type TOML struct {
Pos lexer.Position
Entries []*Entry `{ @@ }`
}
type Entry struct {
Field *Field ` @@`
Section *Section `| @@`
}
type Field struct {
Key string `@Ident "="`
Value *Value `@@`
}
type Value struct {
String *string ` @String`
DateTime *string `| @DateTime`
Date *string `| @Date`
Time *string `| @Time`
Bool *bool `| {@"true" | "false"}`
Integer *int64 `| @Int`
Float *float64 `| @Float`
List []*Value `| "[" [ @@ { "," @@ } ] "]"`
}
type Section struct {
Name string `"[" @(Ident { "." Ident }) "]"`
Fields []*Field `{ @@ }`
}
var (
tomLexer = lexer.Must(ebnf.New(`
Comment = "#" { "\u0000"…"\uffff"-"\n" } .
DateTime = date "T" time [ "-" digit digit ":" digit digit ] .
Date = date .
Time = time .
Ident = (alpha | "_") { "_" | alpha | digit } .
String = "\"" { "\u0000"…"\uffff"-"\""-"\\" | "\\" any } "\"" .
Int = [ "-" | "+" ] digit { digit } .
Float = ("." | digit) {"." | digit }
Punct = "!"…"/" | ":"…"@" | "["…` + "\"`\"" + ` | "{"…"~" .
Whitespace = " " | "\t" | "\n" | "\r" .
alpha = "a"…"z" | "A"…"Z" .
digit = "0"…"9" .
any = "\u0000"…"\uffff" .
date = digit digit digit digit "-" digit digit "-" digit digit .
time = digit digit ":" digit digit ":" digit digit [ "." { digit } ] .
`))
tomlParser = participle.MustBuild(&TOML{},
participle.Lexer(tomLexer),
participle.Unquote("String"),
participle.Elide("Whitespace", "Comment"),
)
cli struct {
File string `help:"TOML file to parse." arg:""`
}
)
func main() {
ctx := kong.Parse(&cli)
toml := &TOML{}
r, err := os.Open(cli.File)
ctx.FatalIfErrorf(err)
defer r.Close()
err = tomlParser.Parse(r, toml)
ctx.FatalIfErrorf(err)
repr.Println(toml)
}