From 7490e2c1c2686e5de165965d5d6bbaf61a1894c8 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Fri, 20 Jan 2017 11:44:41 +0100 Subject: Return module description For now it's mostly key-value pairs but it will most likely have to get more complex because of the if-statements. --- conf/parser.go | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'conf') diff --git a/conf/parser.go b/conf/parser.go index b22e6aa..dfaa8dc 100644 --- a/conf/parser.go +++ b/conf/parser.go @@ -32,6 +32,11 @@ type parser struct { cur token } +type moduledesc struct { + name string + keyvalues map[string]string +} + func newParser(s *scanner) *parser { p := &parser{ s: *s, @@ -48,7 +53,8 @@ func NewConfig(r io.Reader) *Config { } func (p *parser) startparsing() { - p.module("input") + mdesc := p.module("input") + fmt.Fprintf(os.Stderr, "moduledesc: %#v\n", mdesc) p.module("filter") @@ -65,7 +71,9 @@ func (p *parser) advanceOneToken(place string) { fmt.Fprintf(os.Stderr, "tokentype: %v, token: %q\n", p.cur.Type, p.cur.Lit) } -func (p *parser) module(firstname string) { +func (p *parser) module(firstname string) *moduledesc { + ret := moduledesc{keyvalues: make(map[string]string)} + p.advanceOneToken("module") if p.cur.Lit != firstname { @@ -81,16 +89,21 @@ func (p *parser) module(firstname string) { if p.cur.Type != Name { fmt.Fprintf(os.Stderr, "error when parsing module. We were expecting another name but got %q at line %d offset %d.\n", p.cur.Lit, p.cur.LineNr, p.cur.Offset) } - p.object() + ret.name = p.cur.Lit + + ret.keyvalues = p.object(ret.keyvalues) p.advanceOneToken("module") if p.cur.Lit[0] != '}' { fmt.Fprintf(os.Stderr, "error when parsing module. Was expecting a closing bracket but got %s at line %d offset %d.\n", p.cur.Lit, p.cur.LineNr, p.cur.Offset) } + return &ret } -func (p *parser) object() { +func (p *parser) object(kv map[string]string) map[string]string { + var more bool + p.advanceOneToken("object") if p.cur.Lit[0] != '{' { @@ -99,24 +112,27 @@ func (p *parser) object() { // hack to deal with case where there is no white space between // the delimiters. if p.cur.Lit == "{}" { - return + return kv } for { - more := p.keyvalue() + more, kv = p.keyvalue(kv) if !more { break } } + + return kv } -func (p *parser) keyvalue() bool { +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 { fmt.Fprintf(os.Stderr, "error when parsing keyvalue. Was expecting a name 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 != "=>" { @@ -127,18 +143,19 @@ func (p *parser) keyvalue() bool { 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 + 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 + return false, kv } - return true + return true, kv } -- cgit v1.2.3