Skip to content

Commit 4372ee7

Browse files
author
Anthony Romano
authored
Merge pull request #7 from heyitsanthony/braces
parse: rework curly brace handling
2 parents e901a6d + 69c5256 commit 4372ee7

File tree

3 files changed

+46
-59
lines changed

3 files changed

+46
-59
lines changed

parse/parse.go

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ func ReadFile(fpath string) (*Proto, error) {
126126
protoService = ProtoService{}
127127
)
128128

129-
skippingEnum := false
130-
skippingGatewayCnt := 0
129+
skipping := 0
130+
braceDepth := 0
131131
for _, line := range lines {
132132
if strings.HasPrefix(line, "//") {
133133
ls := strings.Replace(line, "//", "", 1)
@@ -137,76 +137,57 @@ func ReadFile(fpath string) (*Proto, error) {
137137
emitComments := comments
138138
comments = []string{}
139139

140-
// skippin enum
141-
if strings.HasPrefix(line, "enum ") {
142-
skippingEnum = true
143-
continue
144-
}
145-
if skippingEnum && strings.HasSuffix(line, "}") { // end of enum
146-
skippingEnum = false
147-
continue
148-
}
149-
if skippingEnum {
150-
continue
140+
if strings.Contains(line, "}") {
141+
braceDepth--
151142
}
152-
153-
// skip gengo/grpc-gateway
154-
if strings.HasPrefix(line, "option (google.api.http) = ") {
155-
skippingGatewayCnt++
156-
continue
143+
if strings.Contains(line, "{") {
144+
braceDepth++
157145
}
158-
if strings.HasPrefix(line, "post: ") {
159-
skippingGatewayCnt++
146+
line = strings.TrimSpace(line)
147+
switch {
148+
case skipping > 0 && braceDepth > skipping:
160149
continue
161-
}
162-
if strings.HasPrefix(line, "body: ") {
163-
skippingGatewayCnt++
164-
continue
165-
}
166-
if skippingGatewayCnt == 3 && line == "}" {
167-
skippingGatewayCnt++
150+
case strings.HasPrefix(line, "enum "):
151+
fallthrough
152+
case strings.HasPrefix(line, "option (google.api.http) = "):
153+
skipping = braceDepth - 1
168154
continue
169-
}
170-
if skippingGatewayCnt == 4 {
171-
skippingGatewayCnt = 0
172-
continue // end of grpc-gateway
155+
default:
156+
skipping = 0
173157
}
174158

175159
switch mode {
176160
case reading:
177-
for j, elem := range strings.Fields(line) {
178-
switch j {
179-
case 0:
180-
switch elem {
181-
case "message":
182-
mode = parsingMessage
183-
184-
case "service":
185-
mode = parsingService
186-
}
187-
188-
case 1: // proto message/service name
189-
switch mode {
190-
case parsingMessage: // message Name
191-
protoMessage.Name = strings.Replace(elem, "{", "", -1)
192-
protoMessage.Description = strings.Join(emitComments, " ")
193-
protoMessage.Fields = []ProtoField{} // reset
194-
195-
case parsingService: // service Name
196-
protoService.Name = strings.Replace(elem, "{", "", -1)
197-
protoService.Description = strings.Join(emitComments, " ")
198-
protoService.Methods = []ProtoMethod{} // reset
199-
}
200-
}
161+
fs := strings.Fields(line)
162+
if len(fs) < 2 {
163+
break
164+
}
165+
switch fs[0] {
166+
case "message":
167+
mode = parsingMessage
168+
case "service":
169+
mode = parsingService
170+
}
171+
switch fs[0] {
172+
// proto message/service name
173+
case "message":
174+
mode = parsingMessage
175+
protoMessage.Name = strings.Replace(fs[1], "{", "", -1)
176+
protoMessage.Description = strings.Join(emitComments, " ")
177+
protoMessage.Fields = []ProtoField{} // reset
178+
case "service":
179+
mode = parsingService
180+
protoService.Name = strings.Replace(fs[1], "{", "", -1)
181+
protoService.Description = strings.Join(emitComments, " ")
182+
protoService.Methods = []ProtoMethod{} // reset
201183
}
202-
203184
case parsingMessage:
204-
if strings.HasSuffix(line, "}") { // closing of message
185+
if braceDepth == 0 { // closing of message
205186
protoMessage.FilePath = fs
206187
rp.Messages = append(rp.Messages, protoMessage)
207188
protoMessage = ProtoMessage{}
208189
mode = reading
209-
continue
190+
break
210191
}
211192

212193
protoField := ProtoField{}
@@ -216,6 +197,9 @@ func ReadFile(fpath string) (*Proto, error) {
216197
tl = strings.Replace(tl, "repeated ", "", -1)
217198
}
218199
fds := strings.Fields(tl)
200+
if len(fds) < 2 {
201+
break
202+
}
219203
tp, err := ToProtoType(fds[0])
220204
if err != nil {
221205
protoField.ProtoType = 0
@@ -253,7 +237,7 @@ func ReadFile(fpath string) (*Proto, error) {
253237
protoMethod.ResponseType = f3
254238
protoMethod.Description = strings.Join(emitComments, " ")
255239
protoService.Methods = append(protoService.Methods, protoMethod)
256-
} else if !strings.HasSuffix(line, "{}") && strings.HasSuffix(line, "}") {
240+
} else if !strings.HasSuffix(line, "{}") && braceDepth == 0 {
257241
// end of service
258242
protoService.FilePath = fs
259243
rp.Services = append(rp.Services, protoService)

parse/testdata/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,7 @@ CompactionRequest compacts the key-value store up to a given revision. All super
365365
| create_revision | create_revision is the creation revision of the given key | int64 | int64 | long | int/long | int64 |
366366
| mod_revision | mod_revision is the last modified revision of the given key. | int64 | int64 | long | int/long | int64 |
367367
| value | value is the value of the given key, in bytes. | bytes | []byte | ByteString | str | string |
368+
| range_end | range_end is the range end. | bytes | []byte | ByteString | str | string |
368369

369370

370371

parse/testdata/rpc.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ message Compare {
433433
// value is the value of the given key, in bytes.
434434
bytes value = 7;
435435
}
436+
// range_end is the range end.
437+
bytes range_end = 8;
436438
}
437439

438440
// From google paxosdb paper:

0 commit comments

Comments
 (0)