Merge pull request #146 from hashicorp/b-comment

hcl/scanner: single line '//' commments verify second '/'
This commit is contained in:
James Nugent 2016-09-02 11:27:27 -05:00 committed by GitHub
commit 05be7a778d
4 changed files with 11 additions and 3 deletions

View File

@ -214,4 +214,5 @@ func (c *CommentGroup) Pos() token.Pos {
// GoStringer
//-------------------------------------------------------------------
func (o *ObjectKey) GoString() string { return fmt.Sprintf("*%#v", *o) }
func (o *ObjectKey) GoString() string { return fmt.Sprintf("*%#v", *o) }
func (o *ObjectList) GoString() string { return fmt.Sprintf("*%#v", *o) }

View File

@ -398,13 +398,14 @@ func TestParse_inline(t *testing.T) {
{"v\nN{{}}", true},
{"v=/\n[,", true},
{"v=10kb", true},
{"v=/foo", true},
}
for _, tc := range cases {
t.Logf("Testing: %q", tc.Value)
_, err := Parse([]byte(tc.Value))
ast, err := Parse([]byte(tc.Value))
if (err != nil) != tc.Err {
t.Fatalf("Input: %q\n\nError: %s\n\nAST: %s", tc.Value, err)
t.Fatalf("Input: %q\n\nError: %s\n\nAST: %#v", tc.Value, err, ast)
}
}
}

View File

@ -224,6 +224,11 @@ func (s *Scanner) Scan() token.Token {
func (s *Scanner) scanComment(ch rune) {
// single line comments
if ch == '#' || (ch == '/' && s.peek() != '*') {
if ch == '/' && s.peek() != '/' {
s.err("expected '/' for comment")
return
}
ch = s.next()
for ch != '\n' && ch >= 0 && ch != eof {
ch = s.next()

View File

@ -496,6 +496,7 @@ func TestError(t *testing.T) {
testError(t, `"abc`, "1:5", "literal not terminated", token.STRING)
testError(t, `"abc`+"\n", "2:1", "literal not terminated", token.STRING)
testError(t, `/*/`, "1:4", "comment not terminated", token.COMMENT)
testError(t, `/foo`, "1:1", "expected '/' for comment", token.COMMENT)
}
func testError(t *testing.T, src, pos, msg string, tok token.Type) {