2015-01-19 01:11:21 -05:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
)
|
|
|
|
|
|
|
|
// dispenser is a type that gets exposed to middleware
|
|
|
|
// generators so that they can parse tokens to configure
|
|
|
|
// their instance.
|
2015-01-30 01:09:14 -05:00
|
|
|
// TODO: Rename this; it does more than dispense tokens.
|
|
|
|
// It also provides access to the server to touch its
|
|
|
|
// configuration.
|
2015-01-19 01:11:21 -05:00
|
|
|
type dispenser struct {
|
2015-01-21 14:09:49 -05:00
|
|
|
parser *parser
|
|
|
|
cursor int
|
|
|
|
nesting int
|
|
|
|
tokens []token
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// newDispenser returns a new dispenser.
|
|
|
|
func newDispenser(p *parser) *dispenser {
|
|
|
|
d := new(dispenser)
|
2015-01-21 14:09:49 -05:00
|
|
|
d.cursor = -1
|
2015-01-19 01:11:21 -05:00
|
|
|
d.parser = p
|
|
|
|
return d
|
|
|
|
}
|
|
|
|
|
|
|
|
// Next loads the next token. Returns true if a token
|
|
|
|
// was loaded; false otherwise. If false, all tokens
|
|
|
|
// have been consumed.
|
|
|
|
// TODO: Have the other Next functions call this one...?
|
|
|
|
func (d *dispenser) Next() bool {
|
2015-01-21 14:09:49 -05:00
|
|
|
if d.cursor >= len(d.tokens)-1 {
|
2015-01-19 01:11:21 -05:00
|
|
|
return false
|
|
|
|
} else {
|
2015-01-21 14:09:49 -05:00
|
|
|
d.cursor++
|
2015-01-19 01:11:21 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NextArg loads the next token if it is on the same
|
|
|
|
// line. Returns true if a token was loaded; false
|
|
|
|
// otherwise. If false, all tokens on the line have
|
|
|
|
// been consumed.
|
|
|
|
func (d *dispenser) NextArg() bool {
|
2015-01-21 14:09:49 -05:00
|
|
|
if d.cursor < 0 {
|
|
|
|
d.cursor++
|
2015-01-19 01:11:21 -05:00
|
|
|
return true
|
|
|
|
}
|
2015-01-21 14:09:49 -05:00
|
|
|
if d.cursor >= len(d.tokens) {
|
2015-01-19 01:11:21 -05:00
|
|
|
return false
|
|
|
|
}
|
2015-01-21 14:09:49 -05:00
|
|
|
if d.cursor < len(d.tokens)-1 &&
|
|
|
|
d.tokens[d.cursor].line == d.tokens[d.cursor+1].line {
|
|
|
|
d.cursor++
|
2015-01-19 01:11:21 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-01-21 14:09:49 -05:00
|
|
|
// TODO: Assert that there's a line break and only advance
|
|
|
|
// the token if that's the case? (store an error otherwise)
|
2015-01-19 01:11:21 -05:00
|
|
|
func (d *dispenser) NextLine() bool {
|
2015-01-21 14:09:49 -05:00
|
|
|
if d.cursor < 0 {
|
|
|
|
d.cursor++
|
2015-01-19 01:11:21 -05:00
|
|
|
return true
|
|
|
|
}
|
2015-01-21 14:09:49 -05:00
|
|
|
if d.cursor >= len(d.tokens) {
|
2015-01-19 01:11:21 -05:00
|
|
|
return false
|
|
|
|
}
|
2015-01-21 14:09:49 -05:00
|
|
|
if d.cursor < len(d.tokens)-1 &&
|
|
|
|
d.tokens[d.cursor].line < d.tokens[d.cursor+1].line {
|
|
|
|
d.cursor++
|
2015-01-19 01:11:21 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2015-01-21 14:09:49 -05:00
|
|
|
// NextBlock advances the cursor to the next token only
|
|
|
|
// if the current token is an open curly brace on the
|
|
|
|
// same line. If so, that token is consumed and this
|
|
|
|
// function will return true until the closing curly
|
|
|
|
// brace is consumed by this method.
|
|
|
|
func (d *dispenser) NextBlock() bool {
|
|
|
|
if d.nesting > 0 {
|
|
|
|
d.Next()
|
|
|
|
if d.Val() == "}" {
|
|
|
|
d.nesting--
|
|
|
|
d.Next() // consume closing brace
|
|
|
|
return false
|
|
|
|
}
|
2015-01-19 01:11:21 -05:00
|
|
|
return true
|
2015-01-21 14:09:49 -05:00
|
|
|
}
|
|
|
|
if !d.NextArg() {
|
2015-01-19 01:11:21 -05:00
|
|
|
return false
|
|
|
|
}
|
2015-01-21 14:09:49 -05:00
|
|
|
if d.Val() != "{" {
|
|
|
|
d.cursor-- // roll back if not opening brace
|
2015-01-19 01:11:21 -05:00
|
|
|
return false
|
|
|
|
}
|
2015-01-21 14:09:49 -05:00
|
|
|
d.Next()
|
|
|
|
d.nesting++
|
|
|
|
return true
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Val gets the text of the current token.
|
|
|
|
func (d *dispenser) Val() string {
|
2015-01-21 14:09:49 -05:00
|
|
|
if d.cursor < 0 || d.cursor >= len(d.tokens) {
|
2015-01-19 01:11:21 -05:00
|
|
|
return ""
|
|
|
|
} else {
|
2015-01-21 14:09:49 -05:00
|
|
|
return d.tokens[d.cursor].text
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArgErr generates an argument error, meaning that another
|
|
|
|
// argument was expected but not found. The error is saved
|
|
|
|
// within the dispenser, but this function returns nil for
|
2015-01-21 14:09:49 -05:00
|
|
|
// convenience in practice.
|
2015-01-30 00:02:17 -05:00
|
|
|
func (d *dispenser) ArgErr() error {
|
2015-01-19 01:11:21 -05:00
|
|
|
if d.Val() == "{" {
|
2015-01-30 00:02:17 -05:00
|
|
|
return d.Err("Unexpected token '{', expecting argument")
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
2015-01-30 00:02:17 -05:00
|
|
|
return d.Err("Unexpected line break after '" + d.Val() + "' (missing arguments?)")
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
|
|
|
|
2015-01-21 14:09:49 -05:00
|
|
|
// Err generates a custom parse error with a message of msg.
|
2015-01-30 00:02:17 -05:00
|
|
|
// TODO: Update the docs of all these Err methods!
|
2015-01-21 14:09:49 -05:00
|
|
|
// This function returns nil for convenience, but loads the
|
|
|
|
// error into the dispenser so it can be reported. The caller
|
|
|
|
// of the middleware preparator is responsible for checking
|
|
|
|
// the error in the dispenser after the middleware preparator
|
|
|
|
// is finished.
|
2015-01-30 00:02:17 -05:00
|
|
|
func (d *dispenser) Err(msg string) error {
|
2015-01-21 14:09:49 -05:00
|
|
|
msg = fmt.Sprintf("%s:%d - Parse error: %s", d.parser.filename, d.tokens[d.cursor].line, msg)
|
2015-01-30 00:02:17 -05:00
|
|
|
return errors.New(msg)
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Args is a convenience function that loads the next arguments
|
|
|
|
// (tokens on the same line) into an arbitrary number of strings
|
|
|
|
// pointed to in targets. If there are fewer tokens available
|
2015-01-21 19:51:47 -05:00
|
|
|
// than string pointers, the remaining strings will not be changed
|
|
|
|
// and false will be returned. If there were enough tokens available
|
|
|
|
// to fill the arguments, then true will be returned.
|
|
|
|
func (d *dispenser) Args(targets ...*string) bool {
|
|
|
|
enough := true
|
|
|
|
for i := 0; i < len(targets); i++ {
|
|
|
|
if !d.NextArg() {
|
|
|
|
enough = false
|
|
|
|
break
|
|
|
|
}
|
2015-01-19 01:11:21 -05:00
|
|
|
*targets[i] = d.Val()
|
|
|
|
}
|
2015-01-21 19:51:47 -05:00
|
|
|
return enough
|
2015-01-19 01:11:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Startup registers a function to execute when the server starts.
|
|
|
|
func (d *dispenser) Startup(fn func() error) {
|
|
|
|
d.parser.cfg.Startup = append(d.parser.cfg.Startup, fn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root returns the server root file path.
|
|
|
|
func (d *dispenser) Root() string {
|
|
|
|
if d.parser.cfg.Root == "" {
|
|
|
|
return "."
|
|
|
|
} else {
|
|
|
|
return d.parser.cfg.Root
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Host returns the hostname the server is bound to.
|
|
|
|
func (d *dispenser) Host() string {
|
|
|
|
return d.parser.cfg.Host
|
|
|
|
}
|
|
|
|
|
|
|
|
// Port returns the port that the server is listening on.
|
|
|
|
func (d *dispenser) Port() string {
|
|
|
|
return d.parser.cfg.Port
|
|
|
|
}
|