From 3dcff2e2445efbea369055a6085d92c663c5979b Mon Sep 17 00:00:00 2001 From: James Nugent Date: Tue, 1 Dec 2015 12:09:13 -0500 Subject: [PATCH] Fix scanner for Windows line endings and add tests --- hcl/scanner/scanner.go | 13 ++++++++++++- hcl/scanner/scanner_test.go | 39 +++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/hcl/scanner/scanner.go b/hcl/scanner/scanner.go index 4bc21bd5..488bfff6 100644 --- a/hcl/scanner/scanner.go +++ b/hcl/scanner/scanner.go @@ -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 { @@ -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") diff --git a/hcl/scanner/scanner_test.go b/hcl/scanner/scanner_test.go index f5ef8c0a..c6c29518 100644 --- a/hcl/scanner/scanner_test.go +++ b/hcl/scanner/scanner_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/hcl/hcl/token" + "strings" ) var f100 = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff" @@ -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=<