spanner/spansql: fix DDL.LeadingComment for prior-line inline comments
Previously, LeadingComment would return an inline comment from the
previous line, which is not the intent of this helper. Now it will
ignore inline comments, which have an associated node, and only return a
comment that is the only thing on its line.
Change-Id: If5b9d580429dba1fd855ab2b79106e23cc5bbc45
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/52893
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Knut Olav Løite <koloite@gmail.com>
diff --git a/spanner/spansql/parser.go b/spanner/spansql/parser.go
index e59b360..143198c 100644
--- a/spanner/spansql/parser.go
+++ b/spanner/spansql/parser.go
@@ -100,10 +100,11 @@
// Handle comments.
for _, com := range p.comments {
c := &Comment{
- Marker: com.marker,
- Start: com.start,
- End: com.end,
- Text: com.text,
+ Marker: com.marker,
+ Isolated: com.isolated,
+ Start: com.start,
+ End: com.end,
+ Text: com.text,
}
// Strip common whitespace prefix and any whitespace suffix.
@@ -239,6 +240,7 @@
type comment struct {
marker string // "#" or "--" or "/*"
+ isolated bool // if it starts on its own line
start, end Position
text []string
}
@@ -670,6 +672,7 @@
// skipSpace skips past any space or comments.
func (p *parser) skipSpace() bool {
+ initLine := p.line
// If we start capturing a comment in this method,
// this is set to its comment value. Multi-line comments
// are only joined during a single skipSpace invocation.
@@ -710,7 +713,8 @@
if com == nil {
// New comment.
p.comments = append(p.comments, comment{
- marker: marker,
+ marker: marker,
+ isolated: (p.line != initLine) || p.line == 1,
start: Position{
Line: p.line,
Offset: p.offset + i,
diff --git a/spanner/spansql/parser_test.go b/spanner/spansql/parser_test.go
index 8c31515..cd079e3 100644
--- a/spanner/spansql/parser_test.go
+++ b/spanner/spansql/parser_test.go
@@ -352,7 +352,7 @@
Text: []string{"This is another comment."}},
{Marker: "/*", Start: line(4), End: line(5),
Text: []string{" This is a", "\t\t\t\t\t\t * multiline comment."}},
- {Marker: "--", Start: line(26), End: line(27),
+ {Marker: "--", Isolated: true, Start: line(26), End: line(27),
Text: []string{"This table has some commentary", "that spans multiple lines."}},
// These comments shouldn't get combined:
{Marker: "--", Start: line(29), End: line(29), Text: []string{"dummy comment"}},
@@ -430,13 +430,11 @@
}
// There are no leading comments on the columns of NonScalars,
// even though there's often a comment on the previous line.
- /* TODO: This is broken; LeadingComment needs fixing.
for _, cd := range tableByName(t, ddl, "NonScalars").Columns {
if com := ddl.LeadingComment(cd); com != nil {
t.Errorf("Leading comment found for NonScalars.%s: %v", cd.Name, com)
}
}
- */
}
func tableByName(t *testing.T, ddl *DDL, name string) *CreateTable {
diff --git a/spanner/spansql/types.go b/spanner/spansql/types.go
index 79fe888..c2397c1 100644
--- a/spanner/spansql/types.go
+++ b/spanner/spansql/types.go
@@ -475,7 +475,8 @@
// Comment represents a comment.
type Comment struct {
- Marker string // opening marker; one of "#", "--", "/*"
+ Marker string // Opening marker; one of "#", "--", "/*".
+ Isolated bool // Whether this comment is on its own line.
// Start and End are the position of the opening and terminating marker.
Start, End Position
Text []string
@@ -521,6 +522,10 @@
if ci >= len(ddl.Comments) || ddl.Comments[ci].End.Line != lineEnd {
return nil
}
+ if !ddl.Comments[ci].Isolated {
+ // This is an inline comment for a previous node.
+ return nil
+ }
return ddl.Comments[ci]
}