Skip to content

Commit

Permalink
Fix for case when comments precede a statement containing an issue
Browse files Browse the repository at this point in the history
e.g.

// foo
set foo.bar to 6

This would mark the error on the comment rather than the set statement.

Because participle includes comments in a node's Tokens, we need to strip them out before calculating where the issues are.
  • Loading branch information
asmaloney committed May 26, 2022
1 parent bda9d9b commit 6b8ef9b
Showing 1 changed file with 38 additions and 4 deletions.
42 changes: 38 additions & 4 deletions amod/issue_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ func (l *Log) ErrorTR(tokens []lexer.Token, start, end int, s string, a ...inter

// tokensToLocation takes the list of lexer tokens and converts it to our own
// issues.Location struct.
func tokensToLocation(tokens []lexer.Token) *issues.Location {
// If we have space tokens on either side, strip them out
func tokensToLocation(t []lexer.Token) *issues.Location {
tokens := trimCommentsFromRange(t)

// If we are in the middle of a pattern, we might have pattern spaces,
// so remove from beginning & end.
if tokens[0].Type == lexer.TokenType(lexemePatternSpace) {
tokens = tokens[1:]
}
Expand All @@ -52,12 +55,14 @@ func tokensToLocation(tokens []lexer.Token) *issues.Location {
}
}

func tokenRangeToLocation(tokens []lexer.Token, start, end int) *issues.Location {
func tokenRangeToLocation(t []lexer.Token, start, end int) *issues.Location {
if start < 0 || end < 1 || start == end || end < start {
fmt.Printf("Internal error (tokenRangeToLocation): start (%d) and/or end (%d) incorrect. Using full range.\n", start, end)
return tokensToLocation(tokens)
return tokensToLocation(t)
}

tokens := trimCommentsFromRange(t)

numTokens := len(tokens)
if end > numTokens-1 {
fmt.Printf("Internal error (tokenRangeToLocation): end (%d - 0-indexed) greater than tokens len (%d). Using full range.\n", end, numTokens)
Expand All @@ -68,3 +73,32 @@ func tokenRangeToLocation(tokens []lexer.Token, start, end int) *issues.Location

return tokensToLocation(restricted)
}

// trimCommentsFromRange will remove any comment tokens from the beginning and end of the range.
// This is necessary because participle will include them with the Tokens in a struct.
func trimCommentsFromRange(t []lexer.Token) (tokens []lexer.Token) {
begin := 0
for _, token := range t {
if token.Type == lexer.TokenType(lexemeComment) {
begin++
continue
}

break
}

end := len(t)
for i := end - 1; i >= 0; i-- {
token := t[i]

if token.Type == lexer.TokenType(lexemeComment) {
end--
continue
}

break
}

tokens = t[begin:end]
return
}

0 comments on commit 6b8ef9b

Please sign in to comment.