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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// grammar of the config file
//
// Notes: We parse exactly three modules ("input", "filter", "output")
// in our configuration. Each module consists of two name-object pairs
// that are nested. We don't want to allow limitless recursion here.
//
// config = mainmodule mainmodule mainmodule
// mainmodule = name '{' module+ '}'
// module = name object
// name = 'input' | 'filter' | 'output' | etc.
// object = '{' keyvalue* '}'
// keyvalue = statement '=>' value
// statement = name | if
// value = literal | '[' literal ']'
// literal = '"' name '"'
package conf
import (
"fmt"
"io"
"os"
"github.com/Shugyousha/stasher/registry"
)
// Having a Config to Manager function could be nice? Or we could just
// return a Manager from here which may be even better...
type Config struct {
}
type parser struct {
s scanner
last token
cur token
}
type moduledesc struct {
name string
keyvalues map[string]string
}
func newParser(s *scanner) *parser {
p := &parser{
s: *s,
}
return p
}
func NewConfig(r io.Reader) *Config {
p := newParser(newScanner(r))
p.startparsing()
return &Config{}
}
func (p *parser) startparsing() {
inputmdescs := p.mainmodule("input")
fmt.Fprintf(os.Stderr, "input moduledescs: %#v\n", inputmdescs)
for _, idesc := range inputmdescs {
inputnewfunc, ok := registry.Inputregistry[idesc.name]
if !ok {
fmt.Fprintf(os.Stderr, "input module is not known: %q\n", idesc.name)
}
inputnewfunc(nil)
}
filtermdescs := p.mainmodule("filter")
fmt.Fprintf(os.Stderr, "filter moduledescs: %#v\n", filtermdescs)
for _, fdesc := range filtermdescs {
filternewfunc, ok := registry.Filterregistry[fdesc.name]
if !ok {
fmt.Fprintf(os.Stderr, "filter module is not known: %q\n", fdesc.name)
}
filternewfunc(nil)
}
outputmdescs := p.mainmodule("output")
fmt.Fprintf(os.Stderr, "output moduledescs: %#v\n", outputmdescs)
for _, odesc := range outputmdescs {
filternewfunc, ok := registry.Outputregistry[odesc.name]
if !ok {
fmt.Fprintf(os.Stderr, "output module is not known: %q\n", odesc.name)
}
filternewfunc(nil)
}
}
func (p *parser) advanceOneToken(place string) {
var err error
p.last = p.cur
p.cur, err = p.s.Scan()
if err != nil {
fmt.Fprintf(os.Stderr, "Error: tokentype: %v, token: %q, err: %v\n", p.cur.Type, p.cur.Lit, err)
}
fmt.Fprintf(os.Stderr, "tokentype: %v, token: %q\n", p.cur.Type, p.cur.Lit)
}
func (p *parser) mainmodule(firstname string) []moduledesc {
var moduledescs []moduledesc
md := moduledesc{keyvalues: make(map[string]string)}
p.advanceOneToken("mainmodule")
if p.cur.Lit != firstname {
fmt.Fprintf(os.Stderr, "error when parsing module. We were expecting name %q but got %q at line %d offset %d.\n", firstname, p.cur.Lit, p.cur.LineNr, p.cur.Offset)
}
p.advanceOneToken("mainmodule")
if p.cur.Type != ObjectOpen {
fmt.Fprintf(os.Stderr, "error when parsing module. We were expecting an opening bracket but got %q at line %d offset %d.\n", p.cur.Lit, p.cur.LineNr, p.cur.Offset)
}
for {
p.advanceOneToken("more modules")
if p.cur.Type != Name {
if p.cur.Lit[0] == '}' {
break
}
fmt.Fprintf(os.Stderr, "error when parsing module. We were expecting either a name or the end of the module but got %q at line %d offset %d.\n", p.cur.Lit, p.cur.LineNr, p.cur.Offset)
}
md.name = p.cur.Lit
md.keyvalues = p.object(md.keyvalues)
moduledescs = append(moduledescs, md)
md = moduledesc{keyvalues: make(map[string]string)}
}
return moduledescs
}
func (p *parser) object(kv map[string]string) map[string]string {
var more bool
p.advanceOneToken("object")
if p.cur.Lit[0] != '{' {
fmt.Fprintf(os.Stderr, "error when parsing object. Was expecting an opening bracket but got %s at line %d offset %d.\n", p.cur.Lit, p.cur.LineNr, p.cur.Offset)
}
// hack to deal with case where there is no white space between
// the delimiters.
if p.cur.Lit == "{}" {
return kv
}
for {
more, kv = p.keyvalue(kv)
if !more {
break
}
}
return kv
}
func (p *parser) keyvalue(kv map[string]string) (bool, map[string]string) {
p.advanceOneToken("keyvalue")
fmt.Fprintf(os.Stderr, "keyvalue: tokentype: %v, token: %q\n", p.cur.Type, p.cur.Lit)
if p.cur.Type != Literal && p.cur.Type != Name {
fmt.Fprintf(os.Stderr, "error when parsing keyvalue. Was expecting a name or a literal but got %q at line %d offset %d.\n", p.cur.Lit, p.cur.LineNr, p.cur.Offset)
}
lhs := p.cur.Lit
p.advanceOneToken("keyvalue")
if p.cur.Lit != "=>" {
fmt.Fprintf(os.Stderr, "error when parsing keyvalue. Was expecting a '=>' but got %q at line %d offset %d.\n", p.cur.Lit, p.cur.LineNr, p.cur.Offset)
}
p.advanceOneToken("keyvalue")
if p.cur.Type != Literal && p.cur.Type != Name {
fmt.Fprintf(os.Stderr, "error when parsing keyvalue. Was expecting a literal or a name but got %q at line %d offset %d.\n", p.cur.Lit, p.cur.LineNr, p.cur.Offset)
}
kv[lhs] = p.cur.Lit
tok, err := p.s.Peek()
if err != nil {
fmt.Fprintf(os.Stderr, "error when parsing keyvalue. Got an error when checking if there are more keyvalues: %v.\n", err)
return false, kv
}
fmt.Fprintf(os.Stderr, "peeked %q (type %v) at line %d offset %d.\n", tok.Lit, tok.Type, tok.LineNr, tok.Offset)
if tok.Type == ObjectClose {
p.advanceOneToken("keyvaluelast")
return false, kv
}
return true, kv
}
|