@@ -45,10 +45,11 @@ func (s *scanner) init(src io.Reader, errh, pragh func(line, col uint, msg strin
45
45
// calls the error handler installed with init. The handler
46
46
// must exist.
47
47
//
48
- // If a //line or //go: directive is encountered, next
49
- // calls the pragma handler installed with init, if not nil.
48
+ // If a //line or //go: directive is encountered at the start
49
+ // of a line, next calls the directive handler pragh installed
50
+ // with init, if not nil.
50
51
//
51
- // The (line, col) position passed to the error and pragma
52
+ // The (line, col) position passed to the error and directive
52
53
// handler is always at or after the current source reading
53
54
// position.
54
55
func (s * scanner ) next () {
@@ -561,13 +562,14 @@ func (s *scanner) skipLine(r rune) {
561
562
562
563
func (s * scanner ) lineComment () {
563
564
r := s .getr ()
564
- if s .pragh == nil || (r != 'g' && r != 'l' ) {
565
+ // directives must start at the beginning of the line (s.col == 0)
566
+ if s .col != 0 || s .pragh == nil || (r != 'g' && r != 'l' ) {
565
567
s .skipLine (r )
566
568
return
567
569
}
568
- // s.pragh != nil && (r == 'g' || r == 'l')
570
+ // s.col == 0 && s. pragh != nil && (r == 'g' || r == 'l')
569
571
570
- // recognize pragmas
572
+ // recognize directives
571
573
prefix := "go:"
572
574
if r == 'l' {
573
575
prefix = "line "
@@ -580,15 +582,15 @@ func (s *scanner) lineComment() {
580
582
r = s .getr ()
581
583
}
582
584
583
- // pragma text without line ending (which may be "\r\n" if Windows),
585
+ // directive text without line ending (which may be "\r\n" if Windows),
584
586
s .startLit ()
585
587
s .skipLine (r )
586
588
text := s .stopLit ()
587
589
if i := len (text ) - 1 ; i >= 0 && text [i ] == '\r' {
588
590
text = text [:i ]
589
591
}
590
592
591
- s .pragh (s .line , s .col + 2 , prefix + string (text )) // +2 since pragma text starts after //
593
+ s .pragh (s .line , s .col + 2 , prefix + string (text )) // +2 since directive text starts after //
592
594
}
593
595
594
596
func (s * scanner ) fullComment () {
0 commit comments