Skip to content

Commit

Permalink
Merge pull request hashicorp#240 from octo/scanner-next
Browse files Browse the repository at this point in the history
scanner: Update prevPos even when returning utf8.RuneError.
  • Loading branch information
mitchellh authored Mar 20, 2018
2 parents adef769 + fdaaf22 commit f40e974
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
13 changes: 5 additions & 8 deletions hcl/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,18 @@ func (s *Scanner) next() rune {
return eof
}

if ch == utf8.RuneError && size == 1 {
s.srcPos.Column++
s.srcPos.Offset += size
s.lastCharLen = size
s.err("illegal UTF-8 encoding")
return ch
}

// remember last position
s.prevPos = s.srcPos

s.srcPos.Column++
s.lastCharLen = size
s.srcPos.Offset += size

if ch == utf8.RuneError && size == 1 {
s.err("illegal UTF-8 encoding")
return ch
}

if ch == '\n' {
s.srcPos.Line++
s.lastLineLen = s.srcPos.Column
Expand Down
19 changes: 19 additions & 0 deletions hcl/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -592,3 +592,22 @@ func countNewlines(s string) int {
}
return n
}

func TestScanHeredocRegexpCompile(t *testing.T) {
cases := []string{
"0\xe1\n<<ȸ\nhello\nworld\nȸ",
}

for _, c := range cases {
s := New([]byte(c))
fmt.Printf("START %q\n", c)

for {
tok := s.Scan()
if tok.Type == token.EOF {
break
}
t.Logf("s.Scan() = %s", tok)
}
}
}

0 comments on commit f40e974

Please sign in to comment.