Parser fixes, and now using base filename

This commit is contained in:
Matthew Holt 2015-01-21 13:19:25 -07:00
parent decfda2705
commit eae024027f
4 changed files with 22 additions and 6 deletions

View file

@ -24,7 +24,12 @@ func Load(filename string) ([]Config, error) {
return nil, err
}
defer file.Close()
p := newParser(file)
p, err := newParser(file)
if err != nil {
return nil, err
}
return p.parse()
}

View file

@ -28,12 +28,16 @@ func init() {
return p.argErr()
}
file, err := os.Open(p.tkn())
filename := p.tkn()
file, err := os.Open(filename)
if err != nil {
return p.err("Parse", err.Error())
}
defer file.Close()
p2 := newParser(file)
p2, err := newParser(file)
if err != nil {
return p.err("Parse", "Could not import "+filename+"; "+err.Error())
}
p2.cfg = p.cfg
err = p2.directives()

View file

@ -20,10 +20,16 @@ type parser struct {
// newParser makes a new parser and prepares it for parsing, given
// the input to parse.
func newParser(file *os.File) *parser {
p := &parser{filename: file.Name()}
func newParser(file *os.File) (*parser, error) {
stat, err := file.Stat()
if err != nil {
return nil, err
}
p := &parser{filename: stat.Name()}
p.lexer.load(file)
return p
return p, nil
}
// Parse parses the configuration file. It produces a slice of Config

View file

@ -45,6 +45,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
return p.directives()
}