From 2dc39feabd633d8abb6a9497ba3d5d7bfef22097 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Sat, 28 Mar 2015 16:24:00 -0600 Subject: [PATCH] Tweak to parser and main's error handling --- config/parser.go | 18 ++++++++++++------ config/parsing.go | 4 ++-- main.go | 2 +- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/config/parser.go b/config/parser.go index 087393e53..7379fd4b6 100644 --- a/config/parser.go +++ b/config/parser.go @@ -16,7 +16,7 @@ type ( cfg Config // each server gets one Config; this is the one we're currently building other []locationContext // tokens to be 'parsed' later by middleware generators scope *locationContext // the current location context (path scope) being populated - unused bool // sometimes a token will be read but not immediately consumed + unused *token // sometimes a token will be read but not immediately consumed } // locationContext represents a location context @@ -62,13 +62,13 @@ func (p *parser) parse() ([]Config, error) { // nextArg loads the next token if it is on the same line. // Returns true if a token was loaded; false otherwise. func (p *parser) nextArg() bool { - if p.unused { + if p.unused != nil { return false } - line := p.line() + line, tkn := p.line(), p.lexer.token if p.next() { if p.line() > line { - p.unused = true + p.unused = &tkn return false } return true @@ -79,8 +79,8 @@ func (p *parser) nextArg() bool { // next loads the next token and returns true if a token // was loaded; false otherwise. func (p *parser) next() bool { - if p.unused { - p.unused = false + if p.unused != nil { + p.unused = nil return true } else { return p.lexer.next() @@ -151,11 +151,17 @@ func (p *parser) unwrap() error { // tkn is shorthand to get the text/value of the current token. func (p *parser) tkn() string { + if p.unused != nil { + return p.unused.text + } return p.lexer.token.text } // line is shorthand to get the line number of the current token. func (p *parser) line() int { + if p.unused != nil { + return p.unused.line + } return p.lexer.token.line } diff --git a/config/parsing.go b/config/parsing.go index 074714536..4b2b541b7 100644 --- a/config/parsing.go +++ b/config/parsing.go @@ -48,7 +48,7 @@ func (p *parser) addressBlock() error { err := p.openCurlyBrace() if err != nil { // meh, single-server configs don't need curly braces - p.unused = true // we read the token but aren't consuming it + p.unused = &p.lexer.token // we read the token but aren't consuming it return p.directives() } @@ -224,7 +224,7 @@ func (p *parser) collectTokens() error { if p.tkn() == "{" { nesting++ } else if p.line() > line && nesting == 0 { - p.unused = true + p.unused = &p.lexer.token breakOk = true break } else if p.tkn() == "}" && nesting > 0 { diff --git a/main.go b/main.go index ff8c8d126..f2fd08aa3 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,7 @@ func main() { defer wg.Done() err := s.Serve() if err != nil { - s.Log(err) + log.Println(err) } }(s) }