Skip to content

Commit

Permalink
cue/scanner: drop support for //line comments
Browse files Browse the repository at this point in the history
This was inherited from Go, but it was never used nor wired up.
We leave all the machinery in cue/token to support adjusted positions,
as we may wish to later support adjusted positions via CUE attributes.

Signed-off-by: Daniel Martí <[email protected]>
Change-Id: I1485c07c287f8790d09df3e894aa8fe301e7dfd2
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1206373
Reviewed-by: Marcel van Lohuizen <[email protected]>
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
  • Loading branch information
mvdan committed Dec 27, 2024
1 parent ae8ee75 commit 901604a
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 102 deletions.
33 changes: 0 additions & 33 deletions cue/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@
package scanner

import (
"bytes"
"fmt"
"path/filepath"
"strconv"
"unicode"
"unicode/utf8"

Expand All @@ -48,7 +46,6 @@ type Scanner struct {
ch rune // current character
offset int // character offset
rdOffset int // reading offset (position after current character)
lineOffset int // current line offset
linesSinceLast int
spacesSinceLast int
insertEOL bool // insert a comma before next newline
Expand All @@ -73,7 +70,6 @@ func (s *Scanner) next() {
if s.rdOffset < len(s.src) {
s.offset = s.rdOffset
if s.ch == '\n' {
s.lineOffset = s.offset
s.file.AddLine(s.offset)
}
r, w := rune(s.src[s.rdOffset]), 1
Expand All @@ -94,7 +90,6 @@ func (s *Scanner) next() {
} else {
s.offset = len(s.src)
if s.ch == '\n' {
s.lineOffset = s.offset
s.file.AddLine(s.offset)
}
s.ch = -1 // eof
Expand Down Expand Up @@ -139,7 +134,6 @@ func (s *Scanner) Init(file *token.File, src []byte, eh ErrorHandler, mode Mode)
s.ch = ' '
s.offset = 0
s.rdOffset = 0
s.lineOffset = 0
s.insertEOL = false
s.ErrorCount = 0

Expand All @@ -156,29 +150,6 @@ func (s *Scanner) errf(offs int, msg string, args ...interface{}) {
s.ErrorCount++
}

var prefix = []byte("//line ")

func (s *Scanner) interpretLineComment(text []byte) {
if bytes.HasPrefix(text, prefix) {
// get filename and line number, if any
if i := bytes.LastIndex(text, []byte{':'}); i > 0 {
if line, err := strconv.Atoi(string(text[i+1:])); err == nil && line > 0 {
// valid //line filename:line comment
filename := string(bytes.TrimSpace(text[len(prefix):i]))
if filename != "" {
filename = filepath.Clean(filename)
if !filepath.IsAbs(filename) {
// make filename relative to current directory
filename = filepath.Join(s.dir, filename)
}
}
// update scanner position
s.file.AddLineInfo(s.lineOffset+len(text)+1, filename, line) // +len(text)+1 since comment applies to next line
}
}
}
}

func (s *Scanner) scanComment() string {
// initial '/' already consumed; s.ch == '/'
offs := s.offset - 1 // position of initial '/'
Expand All @@ -193,10 +164,6 @@ func (s *Scanner) scanComment() string {
}
s.next()
}
if offs == s.lineOffset {
// comment starts at the beginning of the current line
s.interpretLineComment(s.src[offs:s.offset])
}
goto exit
}

Expand Down
69 changes: 0 additions & 69 deletions cue/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ package scanner
import (
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"testing"

Expand Down Expand Up @@ -509,73 +507,6 @@ func TestRelative(t *testing.T) {
}
}

type segment struct {
srcline string // a line of source text
filename string // filename for current token
line int // line number for current token
}

var segments = []segment{
// exactly one token per line since the test consumes one token per segment
{" line1", filepath.Join("dir", "TestLineComments"), 1},
{"\nline2", filepath.Join("dir", "TestLineComments"), 2},
{"\nline3 //line File1.go:100", filepath.Join("dir", "TestLineComments"), 3}, // bad line comment, ignored
{"\nline4", filepath.Join("dir", "TestLineComments"), 4},
{"\n//line File1.go:100\n line100", filepath.Join("dir", "File1.go"), 100},
{"\n//line \t :42\n line1", "", 42},
{"\n//line File2.go:200\n line200", filepath.Join("dir", "File2.go"), 200},
{"\n//line foo\t:42\n line42", filepath.Join("dir", "foo"), 42},
{"\n //line foo:42\n line44", filepath.Join("dir", "foo"), 44}, // bad line comment, ignored
{"\n//line foo 42\n line46", filepath.Join("dir", "foo"), 46}, // bad line comment, ignored
{"\n//line foo:42 extra text\n line48", filepath.Join("dir", "foo"), 48}, // bad line comment, ignored
{"\n//line ./foo:42\n line42", filepath.Join("dir", "foo"), 42},
{"\n//line a/b/c/File1.go:100\n line100", filepath.Join("dir", "a", "b", "c", "File1.go"), 100},
}

var unixsegments = []segment{
{"\n//line /bar:42\n line42", "/bar", 42},
}

var winsegments = []segment{
{"\n//line c:\\bar:42\n line42", "c:\\bar", 42},
{"\n//line c:\\dir\\File1.go:100\n line100", "c:\\dir\\File1.go", 100},
}

// Verify that comments of the form "//line filename:line" are interpreted correctly.
func TestLineComments(t *testing.T) {
segs := segments
if runtime.GOOS == "windows" {
segs = append(segs, winsegments...)
} else {
segs = append(segs, unixsegments...)
}

// make source
var src string
for _, e := range segs {
src += e.srcline
}

// verify scan
var S Scanner
f := token.NewFile(filepath.Join("dir", "TestLineComments"), -1, len(src))
S.Init(f, []byte(src), nil, DontInsertCommas)
for _, s := range segs {
p, _, lit := S.Scan()
pos := f.Position(p)
checkPosScan(t, lit, p, token.Position{
Filename: s.filename,
Offset: pos.Offset,
Line: s.line,
Column: pos.Column,
})
}

if S.ErrorCount != 0 {
t.Errorf("found %d errors", S.ErrorCount)
}
}

// Verify that initializing the same scanner more than once works correctly.
func TestInit(t *testing.T) {
var s Scanner
Expand Down

0 comments on commit 901604a

Please sign in to comment.