Skip to content

Commit c45546a

Browse files
authored
Merge pull request #75 from yoheimuta/parse-rpc-trailing-comment
feat: parsing the rpc's trailing comment followed by the left curly brace
2 parents 5e6beb5 + 724bf0f commit c45546a

File tree

2 files changed

+113
-11
lines changed

2 files changed

+113
-11
lines changed

parser/service.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type RPC struct {
3434
Comments []*Comment
3535
// InlineComment is the optional one placed at the ending.
3636
InlineComment *Comment
37+
// InlineCommentBehindLeftCurly is the optional one placed behind a left curly.
38+
InlineCommentBehindLeftCurly *Comment
3739
// Meta is the meta information.
3840
Meta meta.Meta
3941
}
@@ -99,7 +101,8 @@ func (s *Service) Accept(v Visitor) {
99101
}
100102

101103
// ParseService parses the service.
102-
// service = "service" serviceName "{" { option | rpc | emptyStatement } "}"
104+
//
105+
// service = "service" serviceName "{" { option | rpc | emptyStatement } "}"
103106
//
104107
// See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#service_definition
105108
func (p *Parser) ParseService() (*Service, error) {
@@ -235,12 +238,13 @@ func (p *Parser) parseRPC() (*RPC, error) {
235238
}
236239

237240
var opts []*Option
241+
var inlineLeftCurly *Comment
238242
p.lex.Next()
239243
lastPos := p.lex.Pos
240244
switch p.lex.Token {
241245
case scanner.TLEFTCURLY:
242246
p.lex.UnNext()
243-
opts, err = p.parseRPCOptions()
247+
opts, inlineLeftCurly, err = p.parseRPCOptions()
244248
if err != nil {
245249
return nil, err
246250
}
@@ -259,10 +263,11 @@ func (p *Parser) parseRPC() (*RPC, error) {
259263
}
260264

261265
return &RPC{
262-
RPCName: rpcName,
263-
RPCRequest: rpcRequest,
264-
RPCResponse: rpcResponse,
265-
Options: opts,
266+
RPCName: rpcName,
267+
RPCRequest: rpcRequest,
268+
RPCResponse: rpcResponse,
269+
Options: opts,
270+
InlineCommentBehindLeftCurly: inlineLeftCurly,
266271
Meta: meta.Meta{
267272
Pos: startPos.Position,
268273
LastPos: lastPos.Position,
@@ -338,12 +343,14 @@ func (p *Parser) parseRPCResponse() (*RPCResponse, error) {
338343

339344
// rpcOptions = ( "{" {option | emptyStatement } "}" )
340345
// See https://developers.google.com/protocol-buffers/docs/reference/proto3-spec#service_definition
341-
func (p *Parser) parseRPCOptions() ([]*Option, error) {
346+
func (p *Parser) parseRPCOptions() ([]*Option, *Comment, error) {
342347
p.lex.Next()
343348
if p.lex.Token != scanner.TLEFTCURLY {
344-
return nil, p.unexpected("{")
349+
return nil, nil, p.unexpected("{")
345350
}
346351

352+
inlineLeftCurly := p.parseInlineComment()
353+
347354
var options []*Option
348355
for {
349356
p.lex.NextKeyword()
@@ -354,7 +361,7 @@ func (p *Parser) parseRPCOptions() ([]*Option, error) {
354361
case scanner.TOPTION:
355362
option, err := p.ParseOption()
356363
if err != nil {
357-
return nil, err
364+
return nil, nil, err
358365
}
359366
options = append(options, option)
360367
case scanner.TRIGHTCURLY:
@@ -363,13 +370,13 @@ func (p *Parser) parseRPCOptions() ([]*Option, error) {
363370
default:
364371
err := p.lex.ReadEmptyStatement()
365372
if err != nil {
366-
return nil, err
373+
return nil, nil, err
367374
}
368375
}
369376

370377
p.lex.Next()
371378
if p.lex.Token == scanner.TRIGHTCURLY {
372-
return options, nil
379+
return options, inlineLeftCurly, nil
373380
}
374381
p.lex.UnNext()
375382
}

parser/service_test.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,101 @@ service SearchService {
759759
},
760760
},
761761
},
762+
{
763+
name: "parsing the rpc with a trailing comment followd by the left curly",
764+
input: `
765+
service SearchService {
766+
rpc GetAll(GetRequest) returns(GetReply) { // get the global address table
767+
option(requestreply.Nats).Subject = "get.addrs";
768+
}
769+
}
770+
`,
771+
wantService: &parser.Service{
772+
ServiceName: "SearchService",
773+
ServiceBody: []parser.Visitee{
774+
&parser.RPC{
775+
RPCName: "GetAll",
776+
RPCRequest: &parser.RPCRequest{
777+
MessageType: "GetRequest",
778+
Meta: meta.Meta{
779+
Pos: meta.Position{
780+
Offset: 37,
781+
Line: 3,
782+
Column: 13,
783+
},
784+
},
785+
},
786+
RPCResponse: &parser.RPCResponse{
787+
MessageType: "GetReply",
788+
Meta: meta.Meta{
789+
Pos: meta.Position{
790+
Offset: 57,
791+
Line: 3,
792+
Column: 33,
793+
},
794+
},
795+
},
796+
Options: []*parser.Option{
797+
{
798+
OptionName: "(requestreply.Nats).Subject",
799+
Constant: `"get.addrs"`,
800+
Meta: meta.Meta{
801+
Pos: meta.Position{
802+
Offset: 106,
803+
Line: 4,
804+
Column: 5,
805+
},
806+
LastPos: meta.Position{
807+
Offset: 153,
808+
Line: 4,
809+
Column: 52,
810+
},
811+
},
812+
},
813+
},
814+
InlineCommentBehindLeftCurly: &parser.Comment{
815+
Raw: "// get the global address table",
816+
Meta: meta.Meta{
817+
Pos: meta.Position{
818+
Offset: 70,
819+
Line: 3,
820+
Column: 46,
821+
},
822+
LastPos: meta.Position{
823+
Offset: 100,
824+
Line: 3,
825+
Column: 76,
826+
},
827+
},
828+
},
829+
Meta: meta.Meta{
830+
Pos: meta.Position{
831+
Offset: 27,
832+
Line: 3,
833+
Column: 3,
834+
},
835+
LastPos: meta.Position{
836+
Offset: 157,
837+
Line: 5,
838+
Column: 3,
839+
},
840+
},
841+
},
842+
},
843+
Meta: meta.Meta{
844+
Pos: meta.Position{
845+
Offset: 1,
846+
Line: 2,
847+
Column: 1,
848+
},
849+
LastPos: meta.Position{
850+
Offset: 159,
851+
Line: 6,
852+
Column: 1,
853+
},
854+
},
855+
},
856+
},
762857
}
763858

764859
for _, test := range tests {

0 commit comments

Comments
 (0)