Skip to content

Commit

Permalink
Merge pull request #760 from nats-io/config-report-pos
Browse files Browse the repository at this point in the history
Report position of configuration errors
  • Loading branch information
derekcollison committed Oct 3, 2018
2 parents 11ef828 + 18cb7ed commit 86c6049
Show file tree
Hide file tree
Showing 8 changed files with 824 additions and 504 deletions.
30 changes: 27 additions & 3 deletions conf/lex.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,19 @@ type lexer struct {
// Used for processing escapable substrings in double-quoted and raw strings
stringParts []string
stringStateFn stateFn

// lstart is the start position of the current line.
lstart int

// ilstart is the start position of the line from the current item.
ilstart int
}

type item struct {
typ itemType
val string
line int
pos int
}

func (lx *lexer) nextItem() item {
Expand Down Expand Up @@ -147,8 +154,13 @@ func (lx *lexer) pop() stateFn {
}

func (lx *lexer) emit(typ itemType) {
lx.items <- item{typ, strings.Join(lx.stringParts, "") + lx.input[lx.start:lx.pos], lx.line}
val := strings.Join(lx.stringParts, "") + lx.input[lx.start:lx.pos]

// Position of item in line where it started.
pos := lx.pos - lx.ilstart - len(val)
lx.items <- item{typ, val, lx.line, pos}
lx.start = lx.pos
lx.ilstart = lx.lstart
}

func (lx *lexer) emitString() {
Expand All @@ -159,8 +171,11 @@ func (lx *lexer) emitString() {
} else {
finalString = lx.input[lx.start:lx.pos]
}
lx.items <- item{itemString, finalString, lx.line}
// Position of string in line where it started.
pos := lx.pos - lx.ilstart - len(finalString)
lx.items <- item{itemString, finalString, lx.line, pos}
lx.start = lx.pos
lx.ilstart = lx.lstart
}

func (lx *lexer) addCurrentStringPart(offset int) {
Expand All @@ -186,15 +201,20 @@ func (lx *lexer) next() (r rune) {

if lx.input[lx.pos] == '\n' {
lx.line++

// Mark start position of current line.
lx.lstart = lx.pos
}
r, lx.width = utf8.DecodeRuneInString(lx.input[lx.pos:])
lx.pos += lx.width

return r
}

// ignore skips over the pending input before this point.
func (lx *lexer) ignore() {
lx.start = lx.pos
lx.ilstart = lx.lstart
}

// backup steps back one rune. Can be called only once per call of next.
Expand All @@ -221,10 +241,14 @@ func (lx *lexer) errorf(format string, values ...interface{}) stateFn {
values[i] = escapeSpecial(v)
}
}

// Position of error in current line.
pos := lx.pos - lx.lstart
lx.items <- item{
itemError,
fmt.Sprintf(format, values...),
lx.line,
pos,
}
return nil
}
Expand Down Expand Up @@ -1131,7 +1155,7 @@ func (itype itemType) String() string {
}

func (item item) String() string {
return fmt.Sprintf("(%s, '%s', %d)", item.typ.String(), item.val, item.line)
return fmt.Sprintf("(%s, '%s', %d, %d)", item.typ.String(), item.val, item.line, item.pos)
}

func escapeSpecial(c rune) string {
Expand Down

0 comments on commit 86c6049

Please sign in to comment.