Skip to content
This repository has been archived by the owner on Jan 5, 2019. It is now read-only.

Commit

Permalink
fix issue emicklei#26 : do not associate comment when separated by 1 …
Browse files Browse the repository at this point in the history
…or more newlines
  • Loading branch information
Ernest Micklei committed Jan 4, 2018
1 parent b9faf51 commit c5e29eb
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 43 deletions.
6 changes: 3 additions & 3 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,12 @@ func maybeScanInlineComment(p *Parser, c elementContainer) {
}
}

// takeLastComment removes and returns the last element of the list if it is a Comment.
func takeLastComment(list []Visitee) (*Comment, []Visitee) {
// takeLastComment removes and returns the last element of the list if it is a Comment
func takeLastCommentIfOnLine(list []Visitee, line int) (*Comment, []Visitee) {
if len(list) == 0 {
return nil, list
}
if last, ok := list[len(list)-1].(*Comment); ok {
if last, ok := list[len(list)-1].(*Comment); ok && last.Position.Line == line {
return last, list[:len(list)-1]
}
return nil, list
Expand Down
24 changes: 23 additions & 1 deletion comment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ world`)
func TestTakeLastComment(t *testing.T) {
c0 := newComment(startPosition, "hi")
c1 := newComment(startPosition, "there")
_, l := takeLastComment([]Visitee{c0, c1})
_, l := takeLastCommentIfOnLine([]Visitee{c0, c1}, 1)
if got, want := len(l), 1; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
Expand Down Expand Up @@ -235,3 +235,25 @@ func TestParseCommentWithTripleSlash(t *testing.T) {
t.Fatalf("got [%d] want [%d]", got, want)
}
}

func TestCommentAssociation(t *testing.T) {
src := `
// foo1
// foo2
// bar
syntax = "proto3";
// baz
package bat;`
p := newParserOn(src)
def, err := p.Parse()
if err != nil {
t.Fatal(err)
}
if got, want := len(def.Elements), 5; got != want {
t.Fatalf("got [%v] want [%v]", got, want)
}
}
8 changes: 4 additions & 4 deletions enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (e *Enum) elements() []Visitee {

// takeLastComment is part of elementContainer
// removes and returns the last element of the list if it is a Comment.
func (e *Enum) takeLastComment() (last *Comment) {
last, e.Elements = takeLastComment(e.Elements)
func (e *Enum) takeLastComment(expectedOnLine int) (last *Comment) {
last, e.Elements = takeLastCommentIfOnLine(e.Elements, expectedOnLine)
return
}

Expand All @@ -84,7 +84,7 @@ func (e *Enum) parse(p *Parser) error {
case tOPTION:
v := new(Option)
v.Position = pos
v.Comment = e.takeLastComment()
v.Comment = e.takeLastComment(pos.Line)
err := v.parse(p)
if err != nil {
return err
Expand All @@ -98,7 +98,7 @@ func (e *Enum) parse(p *Parser) error {
p.nextPut(pos, tok, lit)
f := new(EnumField)
f.Position = pos
f.Comment = e.takeLastComment()
f.Comment = e.takeLastComment(pos.Line - 1)
err := f.parse(p)
if err != nil {
return err
Expand Down
4 changes: 2 additions & 2 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func (g *Group) Doc() *Comment {

// takeLastComment is part of elementContainer
// removes and returns the last element of the list if it is a Comment.
func (g *Group) takeLastComment() (last *Comment) {
last, g.Elements = takeLastComment(g.Elements)
func (g *Group) takeLastComment(expectedOnLine int) (last *Comment) {
last, g.Elements = takeLastCommentIfOnLine(g.Elements, expectedOnLine)
return
}

Expand Down
28 changes: 14 additions & 14 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,47 +76,47 @@ func parseMessageBody(p *Parser, c elementContainer) error {
case tENUM == tok:
e := new(Enum)
e.Position = pos
e.Comment = c.takeLastComment()
e.Comment = c.takeLastComment(pos.Line - 1)
if err := e.parse(p); err != nil {
return err
}
c.addElement(e)
case tMESSAGE == tok:
msg := new(Message)
msg.Position = pos
msg.Comment = c.takeLastComment()
msg.Comment = c.takeLastComment(pos.Line - 1)
if err := msg.parse(p); err != nil {
return err
}
c.addElement(msg)
case tOPTION == tok:
o := new(Option)
o.Position = pos
o.Comment = c.takeLastComment()
o.Comment = c.takeLastComment(pos.Line - 1)
if err := o.parse(p); err != nil {
return err
}
c.addElement(o)
case tONEOF == tok:
o := new(Oneof)
o.Position = pos
o.Comment = c.takeLastComment()
o.Comment = c.takeLastComment(pos.Line - 1)
if err := o.parse(p); err != nil {
return err
}
c.addElement(o)
case tMAP == tok:
f := newMapField()
f.Position = pos
f.Comment = c.takeLastComment()
f.Comment = c.takeLastComment(pos.Line - 1)
if err := f.parse(p); err != nil {
return err
}
c.addElement(f)
case tRESERVED == tok:
r := new(Reserved)
r.Position = pos
r.Comment = c.takeLastComment()
r.Comment = c.takeLastComment(pos.Line - 1)
if err := r.parse(p); err != nil {
return err
}
Expand All @@ -129,7 +129,7 @@ func parseMessageBody(p *Parser, c elementContainer) error {
if tGROUP == tok {
g := new(Group)
g.Position = pos
g.Comment = c.takeLastComment()
g.Comment = c.takeLastComment(pos.Line - 1)
g.Optional = prevTok == tOPTIONAL
g.Repeated = prevTok == tREPEATED
g.Required = prevTok == tREQUIRED
Expand All @@ -143,7 +143,7 @@ func parseMessageBody(p *Parser, c elementContainer) error {
f := newNormalField()
f.Type = lit
f.Position = pos
f.Comment = c.takeLastComment()
f.Comment = c.takeLastComment(pos.Line - 1)
f.Optional = prevTok == tOPTIONAL
f.Repeated = prevTok == tREPEATED
f.Required = prevTok == tREQUIRED
Expand All @@ -155,23 +155,23 @@ func parseMessageBody(p *Parser, c elementContainer) error {
case tGROUP == tok:
g := new(Group)
g.Position = pos
g.Comment = c.takeLastComment()
g.Comment = c.takeLastComment(pos.Line - 1)
if err := g.parse(p); err != nil {
return err
}
c.addElement(g)
case tEXTENSIONS == tok:
e := new(Extensions)
e.Position = pos
e.Comment = c.takeLastComment()
e.Comment = c.takeLastComment(pos.Line - 1)
if err := e.parse(p); err != nil {
return err
}
c.addElement(e)
case tEXTEND == tok:
e := new(Message)
e.Position = pos
e.Comment = c.takeLastComment()
e.Comment = c.takeLastComment(pos.Line - 1)
e.IsExtend = true
if err := e.parse(p); err != nil {
return err
Expand All @@ -188,7 +188,7 @@ func parseMessageBody(p *Parser, c elementContainer) error {
p.nextPut(pos, tok, lit)
f := newNormalField()
f.Position = pos
f.Comment = c.takeLastComment()
f.Comment = c.takeLastComment(pos.Line - 1)
if err := f.parse(p); err != nil {
return err
}
Expand Down Expand Up @@ -217,8 +217,8 @@ func (m *Message) elements() []Visitee {
return m.Elements
}

func (m *Message) takeLastComment() (last *Comment) {
last, m.Elements = takeLastComment(m.Elements)
func (m *Message) takeLastComment(expectedOnLine int) (last *Comment) {
last, m.Elements = takeLastCommentIfOnLine(m.Elements, expectedOnLine)
return
}

Expand Down
8 changes: 4 additions & 4 deletions oneof.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ func (o *Oneof) elements() []Visitee {

// takeLastComment is part of elementContainer
// removes and returns the last element of the list if it is a Comment.
func (o *Oneof) takeLastComment() (last *Comment) {
last, o.Elements = takeLastComment(o.Elements)
func (o *Oneof) takeLastComment(expectedOnLine int) (last *Comment) {
last, o.Elements = takeLastCommentIfOnLine(o.Elements, expectedOnLine)
return last
}

Expand Down Expand Up @@ -76,7 +76,7 @@ func (o *Oneof) parse(p *Parser) error {
case tIDENT:
f := newOneOfField()
f.Position = pos
f.Comment, o.Elements = takeLastComment(o.elements())
f.Comment, o.Elements = takeLastCommentIfOnLine(o.elements(), pos.Line-1) // TODO call takeLastComment instead?
f.Type = lit
if err := parseFieldAfterType(f.Field, p); err != nil {
return err
Expand All @@ -85,7 +85,7 @@ func (o *Oneof) parse(p *Parser) error {
case tGROUP:
g := new(Group)
g.Position = pos
g.Comment, o.Elements = takeLastComment(o.elements())
g.Comment, o.Elements = takeLastCommentIfOnLine(o.elements(), pos.Line-1)
if err := g.parse(p); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func TestParseComment(t *testing.T) {
t.Fatal(err)
}

if got, want := len(collect(pr).Comments()), 2; got != want {
if got, want := len(collect(pr).Comments()), 3; got != want {
t.Errorf("got [%v] want [%v]", got, want)
}
}
Expand Down
22 changes: 11 additions & 11 deletions proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ func (proto *Proto) elements() []Visitee {

// takeLastComment is part of elementContainer
// removes and returns the last element of the list if it is a Comment.
func (proto *Proto) takeLastComment() (last *Comment) {
last, proto.Elements = takeLastComment(proto.Elements)
func (proto *Proto) takeLastComment(expectedOnLine int) (last *Comment) {
last, proto.Elements = takeLastCommentIfOnLine(proto.Elements, expectedOnLine)
return
}

Expand All @@ -58,39 +58,39 @@ func (proto *Proto) parse(p *Parser) error {
case tOPTION == tok:
o := new(Option)
o.Position = pos
o.Comment, proto.Elements = takeLastComment(proto.Elements)
o.Comment, proto.Elements = takeLastCommentIfOnLine(proto.Elements, pos.Line-1)
if err := o.parse(p); err != nil {
return err
}
proto.Elements = append(proto.Elements, o)
case tSYNTAX == tok:
s := new(Syntax)
s.Position = pos
s.Comment, proto.Elements = takeLastComment(proto.Elements)
s.Comment, proto.Elements = takeLastCommentIfOnLine(proto.Elements, pos.Line-1)
if err := s.parse(p); err != nil {
return err
}
proto.Elements = append(proto.Elements, s)
case tIMPORT == tok:
im := new(Import)
im.Position = pos
im.Comment, proto.Elements = takeLastComment(proto.Elements)
im.Comment, proto.Elements = takeLastCommentIfOnLine(proto.Elements, pos.Line-1)
if err := im.parse(p); err != nil {
return err
}
proto.Elements = append(proto.Elements, im)
case tENUM == tok:
enum := new(Enum)
enum.Position = pos
enum.Comment, proto.Elements = takeLastComment(proto.Elements)
enum.Comment, proto.Elements = takeLastCommentIfOnLine(proto.Elements, pos.Line-1)
if err := enum.parse(p); err != nil {
return err
}
proto.Elements = append(proto.Elements, enum)
case tSERVICE == tok:
service := new(Service)
service.Position = pos
service.Comment, proto.Elements = takeLastComment(proto.Elements)
service.Comment, proto.Elements = takeLastCommentIfOnLine(proto.Elements, pos.Line-1)
err := service.parse(p)
if err != nil {
return err
Expand All @@ -99,15 +99,15 @@ func (proto *Proto) parse(p *Parser) error {
case tPACKAGE == tok:
pkg := new(Package)
pkg.Position = pos
pkg.Comment, proto.Elements = takeLastComment(proto.Elements)
pkg.Comment, proto.Elements = takeLastCommentIfOnLine(proto.Elements, pos.Line-1)
if err := pkg.parse(p); err != nil {
return err
}
proto.Elements = append(proto.Elements, pkg)
case tMESSAGE == tok:
msg := new(Message)
msg.Position = pos
msg.Comment, proto.Elements = takeLastComment(proto.Elements)
msg.Comment, proto.Elements = takeLastCommentIfOnLine(proto.Elements, pos.Line-1)
if err := msg.parse(p); err != nil {
return err
}
Expand All @@ -116,7 +116,7 @@ func (proto *Proto) parse(p *Parser) error {
case tEXTEND == tok:
msg := new(Message)
msg.Position = pos
msg.Comment, proto.Elements = takeLastComment(proto.Elements)
msg.Comment, proto.Elements = takeLastCommentIfOnLine(proto.Elements, pos.Line-1)
msg.IsExtend = true
if err := msg.parse(p); err != nil {
return err
Expand All @@ -140,5 +140,5 @@ done:
type elementContainer interface {
addElement(v Visitee)
elements() []Visitee
takeLastComment() *Comment
takeLastComment(expectedOnLine int) *Comment
}
6 changes: 3 additions & 3 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ func (s *Service) elements() []Visitee {

// takeLastComment is part of elementContainer
// removes and returns the last elements of the list if it is a Comment.
func (s *Service) takeLastComment() (last *Comment) {
last, s.Elements = takeLastComment(s.Elements)
func (s *Service) takeLastComment(expectedOnLine int) (last *Comment) {
last, s.Elements = takeLastCommentIfOnLine(s.Elements, expectedOnLine)
return
}

Expand All @@ -85,7 +85,7 @@ func (s *Service) parse(p *Parser) error {
case tRPC:
rpc := new(RPC)
rpc.Position = pos
rpc.Comment, s.Elements = takeLastComment(s.Elements)
rpc.Comment, s.Elements = takeLastCommentIfOnLine(s.Elements, pos.Line-1)
err := rpc.parse(p)
if err != nil {
return err
Expand Down

0 comments on commit c5e29eb

Please sign in to comment.