import (
"io"
"regexp"
- "strings"
)
// CommentsDo parses the given source and calls the provided handler for each
}
}
-// ERROR comments must start with text `ERROR "msg"` or `ERROR msg`.
-// Space around "msg" or msg is ignored.
-var errRx = regexp.MustCompile(`^ *ERROR *"?([^"]*)"?`)
-
-// ErrorMap collects all comments with comment text of the form
-// `ERROR "msg"` or `ERROR msg` from the given src and returns them
-// as []Error lists in a map indexed by line number. The position
-// for each Error is the position of the token immediately preceding
-// the comment, the Error message is the message msg extracted from
-// the comment, with all errors that are on the same line collected
-// in a slice, in source order. If there is no preceding token (the
-// `ERROR` comment appears in the beginning of the file), then the
-// recorded position is unknown (line, col = 0, 0). If there are no
-// ERROR comments, the result is nil.
-func ErrorMap(src io.Reader) (errmap map[uint][]Error) {
+// CommentMap collects all comments in the given src with comment text
+// that matches the supplied regular expression rx and returns them as
+// []Error lists in a map indexed by line number. The comment text is
+// the comment with any comment markers ("//", "/*", or "*/") stripped.
+// The position for each Error is the position of the token immediately
+// preceding the comment and the Error message is the comment text,
+// with all comments that are on the same line collected in a slice, in
+// source order. If there is no preceding token (the matching comment
+// appears at the beginning of the file), then the recorded position
+// is unknown (line, col = 0, 0). If there are no matching comments,
+// the result is nil.
+func CommentMap(src io.Reader, rx *regexp.Regexp) (res map[uint][]Error) {
// position of previous token
var base *PosBase
var prev struct{ line, col uint }
var s scanner
s.init(src, func(_, _ uint, text string) {
if text[0] != '/' {
- return // error, ignore
+ return // not a comment, ignore
}
if text[1] == '*' {
text = text[:len(text)-2] // strip trailing */
}
- if s := errRx.FindStringSubmatch(text[2:]); len(s) == 2 {
+ text = text[2:] // strip leading // or /*
+ if rx.MatchString(text) {
pos := MakePos(base, prev.line, prev.col)
- err := Error{pos, strings.TrimSpace(s[1])}
- if errmap == nil {
- errmap = make(map[uint][]Error)
+ err := Error{pos, text}
+ if res == nil {
+ res = make(map[uint][]Error)
}
- errmap[prev.line] = append(errmap[prev.line], err)
+ res[prev.line] = append(res[prev.line], err)
}
}, comments)
import (
"fmt"
+ "regexp"
"strings"
"testing"
)
-func TestErrorMap(t *testing.T) {
- const src = `/* ERROR 0:0 */ /* ERROR "0:0" */ // ERROR 0:0
+func TestCommentMap(t *testing.T) {
+ const src = `/* ERROR "0:0" */ /* ERROR "0:0" */ // ERROR "0:0"
// ERROR "0:0"
-x /* ERROR 3:1 */ // ignore automatically inserted semicolon here
-/* ERROR 3:1 */ // position of x on previous line
- x /* ERROR 5:4 */ ; // do not ignore this semicolon
-/* ERROR 5:22 */ // position of ; on previous line
- package /* ERROR 7:2 */ // indented with tab
- import /* ERROR 8:9 */ // indented with blanks
+x /* ERROR "3:1" */ // ignore automatically inserted semicolon here
+/* ERROR "3:1" */ // position of x on previous line
+ x /* ERROR "5:4" */ ; // do not ignore this semicolon
+/* ERROR "5:24" */ // position of ; on previous line
+ package /* ERROR "7:2" */ // indented with tab
+ import /* ERROR "8:9" */ // indented with blanks
`
- m := ErrorMap(strings.NewReader(src))
- got := 0 // number of errors found
+ m := CommentMap(strings.NewReader(src), regexp.MustCompile("^ ERROR "))
+ found := 0 // number of errors found
for line, errlist := range m {
for _, err := range errlist {
if err.Pos.Line() != line {
continue
}
// err.Pos.Line() == line
- msg := fmt.Sprintf("%d:%d", line, err.Pos.Col())
- if err.Msg != msg {
- t.Errorf("%v: got msg %q; want %q", err, err.Msg, msg)
+
+ got := strings.TrimSpace(err.Msg[len(" ERROR "):])
+ want := fmt.Sprintf(`"%d:%d"`, line, err.Pos.Col())
+ if got != want {
+ t.Errorf("%v: got msg %q; want %q", err, got, want)
continue
}
+ found++
}
- got += len(errlist)
}
- want := strings.Count(src, "ERROR")
- if got != want {
- t.Errorf("ErrorMap got %d errors; want %d", got, want)
+ want := strings.Count(src, " ERROR ")
+ if found != want {
+ t.Errorf("CommentMap got %d errors; want %d", found, want)
}
}
t.Error(err)
continue
}
- if m := syntax.ErrorMap(f); len(m) > 0 {
+ if m := syntax.CommentMap(f, regexp.MustCompile("^ ERROR ")); len(m) > 0 {
errmap[filename] = m
}
f.Close()
// one of errors in list should match the current error
index := -1 // list index of matching message, if any
for i, want := range list {
- rx, err := regexp.Compile(want.Msg)
+ pattern := strings.TrimSpace(want.Msg[len(" ERROR "):])
+ if n := len(pattern); n >= 2 && pattern[0] == '"' && pattern[n-1] == '"' {
+ pattern = pattern[1 : n-1]
+ }
+ rx, err := regexp.Compile(pattern)
if err != nil {
t.Errorf("%s:%d:%d: %v", filename, line, want.Pos.Col(), err)
continue
_ = unsafe.Alignof(new(struct{}))
_ = unsafe.Alignof(1<<10)
_ = unsafe.Alignof(1 << /* ERROR constant shift overflow */ 1000)
- _ = unsafe.Alignof(nil /* ERROR "untyped nil */ )
+ _ = unsafe.Alignof(nil /* ERROR untyped nil */ )
unsafe /* ERROR not used */ .Alignof(x)
var y S0
_ = S2{}.B
_ = S2{}.C
_ = S2{}.D /* ERROR "no field or method" */
- _ = S3{}.S1 /* ERROR "ambiguous selector S3{}.S1" */
+ _ = S3{}.S1 /* ERROR "ambiguous selector S3{}\.S1" */
_ = S3{}.A
- _ = S3{}.B /* ERROR "ambiguous selector" S3{}.B */
+ _ = S3{}.B /* ERROR "ambiguous selector S3{}\.B" */
_ = S3{}.D
_ = S3{}.E
_ = S4{}.A
_ = S4{}.B /* ERROR "no field or method" */
- _ = S5{}.X /* ERROR "ambiguous selector S5{}.X" */
+ _ = S5{}.X /* ERROR "ambiguous selector S5{}\.X" */
_ = S5{}.Y
- _ = S10{}.X /* ERROR "ambiguous selector S10{}.X" */
+ _ = S10{}.X /* ERROR "ambiguous selector S10{}\.X" */
_ = S10{}.Y
}
switch a {
case [3]int{1, 2, 3}:
case [3]int{1, 2, 3}: // no duplicate detection
- case [ /* ERROR "mismatched types */ 4]int{4, 5, 6}:
+ case [ /* ERROR "mismatched types" */ 4]int{4, 5, 6}:
}
// switch on channel
import "" // ERROR invalid import path \(empty string\)
import
var /* ERROR missing import path */ _ int
-import .; // ERROR missing import path
+import .; // ERROR missing import path
import 'x' // ERROR import path must be a string
var _ int
import /* ERROR imports must appear before other declarations */ _ "math"