Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(spanner/spansql): fix parsing of adjacent inline and leading comments #2851

Merged
merged 1 commit into from
Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions spanner/spansql/parser.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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