diff --git a/spanner/spansql/parser.go b/spanner/spansql/parser.go index 43b2fcebdfa..3faae445f8f 100644 --- a/spanner/spansql/parser.go +++ b/spanner/spansql/parser.go @@ -735,6 +735,12 @@ func (p *parser) skipSpace() bool { p.line++ } i += ti + len(term) + + // A non-isolated comment is always complete and doesn't get + // combined with any future comment. + if !com.isolated { + com = nil + } } p.s = p.s[i:] p.offset += i diff --git a/spanner/spansql/parser_test.go b/spanner/spansql/parser_test.go index 3c9960689ef..59c53ee31bd 100644 --- a/spanner/spansql/parser_test.go +++ b/spanner/spansql/parser_test.go @@ -292,8 +292,12 @@ func TestParseDDL(t *testing.T) { CREATE TABLE NonScalars ( Dummy INT64 NOT NULL, -- dummy comment Ids ARRAY, -- comment on ids + -- leading multi comment immediately after inline comment + BCol BOOL, Names ARRAY, ) PRIMARY KEY (Dummy); + + -- Trailing comment at end of file. `, &DDL{Filename: "filename", List: []DDLStmt{ &CreateTable{ Name: "FooBar", @@ -410,7 +414,8 @@ func TestParseDDL(t *testing.T) { Columns: []ColumnDef{ {Name: "Dummy", Type: Type{Base: Int64}, NotNull: true, Position: line(34)}, {Name: "Ids", Type: Type{Array: true, Base: Int64}, Position: line(35)}, - {Name: "Names", Type: Type{Array: true, Base: String, Len: MaxLen}, Position: line(36)}, + {Name: "BCol", Type: Type{Base: Bool}, Position: line(37)}, + {Name: "Names", Type: Type{Array: true, Base: String, Len: MaxLen}, Position: line(38)}, }, PrimaryKey: []KeyPart{{Column: "Dummy"}}, Position: line(33), @@ -431,6 +436,10 @@ func TestParseDDL(t *testing.T) { // These comments shouldn't get combined: {Marker: "--", Start: line(34), End: line(34), Text: []string{"dummy comment"}}, {Marker: "--", Start: line(35), End: line(35), Text: []string{"comment on ids"}}, + {Marker: "--", Isolated: true, Start: line(36), End: line(36), Text: []string{"leading multi comment immediately after inline comment"}}, + + // Comment after everything else. + {Marker: "--", Isolated: true, Start: line(41), End: line(41), Text: []string{"Trailing comment at end of file."}}, }}}, // No trailing comma: {`ALTER TABLE T ADD COLUMN C2 INT64`, &DDL{Filename: "filename", List: []DDLStmt{ @@ -502,9 +511,12 @@ func TestParseDDL(t *testing.T) { } else if com.Text[0] != "This is another comment." { t.Errorf("InlineComment returned the wrong comment (%q) for FooBar.RepoPath", com.Text[0]) } - // There are no leading comments on the columns of NonScalars, + // There are no leading comments on the columns of NonScalars (except for BCol), // even though there's often a comment on the previous line. for _, cd := range tableByName(t, ddl, "NonScalars").Columns { + if cd.Name == "BCol" { + continue + } if com := ddl.LeadingComment(cd); com != nil { t.Errorf("Leading comment found for NonScalars.%s: %v", cd.Name, com) }