Skip to content

Commit

Permalink
Fix scanner for Windows line endings and add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jen20 committed Dec 1, 2015
1 parent 8a65681 commit 3dcff2e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
13 changes: 12 additions & 1 deletion hcl/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,11 @@ func (s *Scanner) scanComment(ch rune) {
// single line comments
if ch == '#' || (ch == '/' && s.peek() != '*') {
ch = s.next()
for ch != '\n' && ch >= 0 && ch != eof {
for ch != '\n' && ch != '\r' && ch >= 0 && ch != eof {
if ch == '\r' && s.peek() == '\n' {
ch = s.next()
break
}
ch = s.next()
}
if ch != eof && ch >= 0 {
Expand Down Expand Up @@ -399,6 +403,13 @@ func (s *Scanner) scanHeredoc() {
return
}

// Ignore the '\r' in Windows line endings
if ch == '\r' {
if s.peek() == '\n' {
ch = s.next()
}
}

// If we didn't reach a newline then that is also not good
if ch != '\n' {
s.err("invalid characters in heredoc anchor")
Expand Down
39 changes: 39 additions & 0 deletions hcl/scanner/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/hashicorp/hcl/hcl/token"
"strings"
)

var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"
Expand Down Expand Up @@ -307,6 +308,44 @@ func TestFloat(t *testing.T) {
testTokenList(t, tokenLists["float"])
}

func TestWindowsLineEndings(t *testing.T) {
hcl := `// This should have Windows line endings
resource "aws_instance" "foo" {
user_data=<<HEREDOC
test script
HEREDOC
}`
hclWindowsEndings := strings.Replace(hcl, "\n", "\r\n", -1)

literals := []struct {
tokenType token.Type
literal string
}{
{token.COMMENT, `// This should have Windows line endings`},
{token.IDENT, `resource`},
{token.STRING, `"aws_instance"`},
{token.STRING, `"foo"`},
{token.LBRACE, `{`},
{token.IDENT, `user_data`},
{token.ASSIGN, `=`},
{token.HEREDOC, "<<HEREDOC\r\n test script\r\nHEREDOC\r\n"},
{token.RBRACE, `}`},
}

s := New([]byte(hclWindowsEndings))
for _, l := range literals {
tok := s.Scan()

if l.tokenType != tok.Type {
t.Errorf("got: %s want %s for %s\n", tok, l.tokenType, tok.String())
}

if l.literal != tok.Text {
t.Errorf("got: %s want %s\n", tok.Text, l.literal)
}
}
}

func TestRealExample(t *testing.T) {
complexHCL := `// This comes from Terraform, as a test
variable "foo" {
Expand Down

0 comments on commit 3dcff2e

Please sign in to comment.