Skip to content

Commit

Permalink
Report error when loading the lexer
Browse files Browse the repository at this point in the history
  • Loading branch information
mholt committed Aug 17, 2016
1 parent 8b8afd7 commit e8e5595
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 7 deletions.
3 changes: 2 additions & 1 deletion caddyfile/dispenser.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ type Dispenser struct {

// NewDispenser returns a Dispenser, ready to use for parsing the given input.
func NewDispenser(filename string, input io.Reader) Dispenser {
tokens, _ := allTokens(input) // ignoring error because nothing to do with it
return Dispenser{
filename: filename,
tokens: allTokens(input),
tokens: tokens,
cursor: -1,
}
}
Expand Down
5 changes: 4 additions & 1 deletion caddyfile/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func (l *lexer) load(input io.Reader) error {

// discard byte order mark, if present
firstCh, _, err := l.reader.ReadRune()
if err == nil && firstCh != 0xFEFF {
if err != nil {
return err
}
if firstCh != 0xFEFF {
err := l.reader.UnreadRune()
if err != nil {
return err
Expand Down
15 changes: 11 additions & 4 deletions caddyfile/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ func Parse(filename string, input io.Reader, validDirectives []string) ([]Server
// allTokens lexes the entire input, but does not parse it.
// It returns all the tokens from the input, unstructured
// and in order.
func allTokens(input io.Reader) (tokens []Token) {
func allTokens(input io.Reader) ([]Token, error) {
l := new(lexer)
l.load(input)
err := l.load(input)
if err != nil {
return nil, err
}
var tokens []Token
for l.next() {
tokens = append(tokens, l.token)
}
return
return tokens, nil
}

type parser struct {
Expand Down Expand Up @@ -294,7 +298,10 @@ func (p *parser) doSingleImport(importFile string) ([]Token, error) {
return nil, p.Errf("Could not import %s: is a directory", importFile)
}

importedTokens := allTokens(file)
importedTokens, err := allTokens(file)
if err != nil {
return nil, p.Errf("Could not read tokens while importing %s: %v", importFile, err)
}

// Tack the filename onto these tokens so errors show the imported file's name
filename := filepath.Base(importFile)
Expand Down
5 changes: 4 additions & 1 deletion caddyfile/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ import (
func TestAllTokens(t *testing.T) {
input := strings.NewReader("a b c\nd e")
expected := []string{"a", "b", "c", "d", "e"}
tokens := allTokens(input)
tokens, err := allTokens(input)

if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if len(tokens) != len(expected) {
t.Fatalf("Expected %d tokens, got %d", len(expected), len(tokens))
}
Expand Down

0 comments on commit e8e5595

Please sign in to comment.