Skip to content

Commit

Permalink
spanner/spansql: fix parsing of adjacent inline and leading comments (#…
Browse files Browse the repository at this point in the history
…2851)

These should be in separate Comment entries, not combined:
	Some INT64,  -- comment 1
        -- comment 2
  • Loading branch information
dsymonds committed Sep 14, 2020
1 parent fc52577 commit cca8632
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
6 changes: 6 additions & 0 deletions spanner/spansql/parser.go
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions spanner/spansql/parser_test.go
Expand Up @@ -292,8 +292,12 @@ func TestParseDDL(t *testing.T) {
CREATE TABLE NonScalars (
Dummy INT64 NOT NULL, -- dummy comment
Ids ARRAY<INT64>, -- comment on ids
-- leading multi comment immediately after inline comment
BCol BOOL,
Names ARRAY<STRING(MAX)>,
) PRIMARY KEY (Dummy);
-- Trailing comment at end of file.
`, &DDL{Filename: "filename", List: []DDLStmt{
&CreateTable{
Name: "FooBar",
Expand Down Expand Up @@ -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),
Expand All @@ -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{
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit cca8632

Please sign in to comment.