Skip to content

Commit

Permalink
Tolerate missing ';' in message body (#218)
Browse files Browse the repository at this point in the history
While still producing an error, see:
#200
  • Loading branch information
Alfus authored Dec 7, 2023
1 parent 340bf80 commit ebf3519
Show file tree
Hide file tree
Showing 7 changed files with 873 additions and 823 deletions.
12 changes: 7 additions & 5 deletions ast/field.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,19 +543,21 @@ func NewMapFieldNode(mapType *MapTypeNode, name *IdentNode, equals *RuneNode, ta
if tag == nil {
panic("tag is nil")
}
if semicolon == nil {
panic("semicolon is nil")
}
numChildren := 5
numChildren := 4
if opts != nil {
numChildren++
}
if semicolon != nil {
numChildren++
}
children := make([]Node, 0, numChildren)
children = append(children, mapType, name, equals, tag)
if opts != nil {
children = append(children, opts)
}
children = append(children, semicolon)
if semicolon != nil {
children = append(children, semicolon)
}

return &MapFieldNode{
compositeNode: compositeNode{
Expand Down
13 changes: 8 additions & 5 deletions ast/ranges.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,17 @@ func NewReservedIdentifiersNode(keyword *KeywordNode, names []*IdentNode, commas
if keyword == nil {
panic("keyword is nil")
}
if semicolon == nil {
panic("semicolon is nil")
}
if len(names) == 0 {
panic("must have at least one name")
}
if len(commas) != len(names)-1 {
panic(fmt.Sprintf("%d names requires %d commas, not %d", len(names), len(names)-1, len(commas)))
}
children := make([]Node, 0, len(names)*2+1)
numChildren := len(names) * 2
if semicolon != nil {
numChildren++
}
children := make([]Node, 0, numChildren)
children = append(children, keyword)
for i, name := range names {
if i > 0 {
Expand All @@ -362,7 +363,9 @@ func NewReservedIdentifiersNode(keyword *KeywordNode, names []*IdentNode, commas
}
children = append(children, name)
}
children = append(children, semicolon)
if semicolon != nil {
children = append(children, semicolon)
}
return &ReservedNode{
compositeNode: compositeNode{
children: children,
Expand Down
18 changes: 18 additions & 0 deletions parser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ func newEnumElements(semicolons []*ast.RuneNode, elements []ast.EnumElement) []a
return elems
}

func newMessageElements(semicolons []*ast.RuneNode, elements []ast.MessageElement) []ast.MessageElement {
elems := make([]ast.MessageElement, 0, len(semicolons)+len(elements))
for _, semicolon := range semicolons {
elems = append(elems, ast.NewEmptyDeclNode(semicolon))
}
elems = append(elems, elements...)
return elems
}

type nodeWithEmptyDecls[T ast.Node] struct {
Node T
EmptyDecls []*ast.EmptyDeclNode
Expand Down Expand Up @@ -182,3 +191,12 @@ func toEnumElements[T ast.EnumElement](nodes nodeWithEmptyDecls[T]) []ast.EnumEl
}
return elements
}

func toMessageElements[T ast.MessageElement](nodes nodeWithEmptyDecls[T]) []ast.MessageElement {
elements := make([]ast.MessageElement, 1+len(nodes.EmptyDecls))
elements[0] = nodes.Node
for i, emptyDecl := range nodes.EmptyDecls {
elements[i+1] = emptyDecl
}
return elements
}
86 changes: 86 additions & 0 deletions parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,92 @@ func TestLenientParse_SemicolonLess(t *testing.T) {
int32 bar = 1 [foo = 1];
}`,
},
"message-field": {
Error: `syntax = "proto3";
message Foo {
int32 bar = 1
}`,
NoError: `syntax = "proto3";
message Foo {
;
int32 bar = 1;;
}`,
},
"message-field-cardinality": {
Error: `syntax = "proto3";
message Foo {
repeated int32 bar = 1
}`,
NoError: `syntax = "proto3";
message Foo {
repeated int32 bar = 1;
}`,
},
"message-field-options": {
Error: `syntax = "proto3";
message Foo {
int32 bar = 1 [foo = 1]
}`,
NoError: `syntax = "proto3";
message Foo {
int32 bar = 1 [foo = 1];
}`,
},
"message-reserved": {
Error: `syntax = "proto3";
message Foo {
reserved "FOO"
}`,
NoError: `syntax = "proto3";
message Foo {
;
reserved "FOO";;
}`,
},
"message-options": {
Error: `syntax = "proto3";
message Foo {
option (foo) = 1
}`,
NoError: `syntax = "proto3";
message Foo {
;
option (foo) = 1;;
}`,
},
"message-map-field": {
Error: `syntax = "proto3";
message Foo {
map<string, int32> bar = 1
}`,
NoError: `syntax = "proto3";
message Foo {
;
map<string, int32> bar = 1;;
}`,
},
"message-map-field-options": {
Error: `syntax = "proto3";
message Foo {
map<string, int32> bar = 1 [foo = 1]
}`,
NoError: `syntax = "proto3";
message Foo {
;
map<string, int32> bar = 1 [foo = 1];;
}`,
},
"message-option": {
Error: `syntax = "proto3";
message Foo {
option (foo) = 1
}`,
NoError: `syntax = "proto3";
message Foo {
;
option (foo) = 1;;
}`,
},
}
for name, input := range inputs {
name, input := name, input
Expand Down
Loading

0 comments on commit ebf3519

Please sign in to comment.