Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement the legalizer #438

Open
wants to merge 47 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
8b3b7d7
PR #437
mcy Jan 29, 2025
c6e445e
add Span.Len
mcy Jan 29, 2025
5a0be56
add new iterator helpers
mcy Jan 29, 2025
5c50385
add classifier methods for predeclared.Name
mcy Jan 29, 2025
c9309db
add an enum for syntax/edition values
mcy Jan 29, 2025
8dd79ae
add some new taxa.Nouns
mcy Jan 29, 2025
f40ec3c
ast additions
mcy Jan 29, 2025
453be0c
fix brackets not being recorded for compact options
mcy Jan 29, 2025
79bf0e0
update import parsing to catch more nested imports
mcy Jan 29, 2025
a469395
track if we're in edition mode
mcy Jan 29, 2025
2893908
add diagnostics for use in the legalizer
mcy Jan 29, 2025
e56474d
add new testdata files for use with the legalizer
mcy Jan 29, 2025
198a8bc
skeletonize the legalizer
mcy Jan 29, 2025
4c4129e
legalize syntax
mcy Jan 29, 2025
02ac466
legalize package
mcy Jan 29, 2025
96efead
legalize imports
mcy Jan 29, 2025
8603323
legalize against bare braces
mcy Jan 29, 2025
ce87eb6
legalize ranges
mcy Jan 29, 2025
e138256
legalize parent relationships for defs
mcy Jan 29, 2025
35f4923
legalize messages and friends
mcy Jan 29, 2025
f796024
legalize fields
mcy Jan 29, 2025
0eb82c2
legalize options
mcy Jan 29, 2025
c7f6c7e
legalize methods
mcy Jan 29, 2025
6575e52
legalize types
mcy Jan 29, 2025
6929176
fix zero_test
mcy Jan 30, 2025
fc14e67
lint
mcy Jan 30, 2025
8086d1e
add comments
mcy Jan 30, 2025
119c667
cr for predeclared
mcy Feb 7, 2025
d72c0ad
add tests for the syntax package
mcy Feb 7, 2025
b1be12f
fix group types not being printed
mcy Feb 7, 2025
d74a61b
string helpers
mcy Feb 7, 2025
f3de41d
add span helpers
mcy Feb 7, 2025
dd7fdfb
cr
mcy Feb 7, 2025
f7e68d3
cr
mcy Feb 7, 2025
d94e883
fix a renderer quirk
mcy Feb 7, 2025
ae8f170
Merge remote-tracking branch 'origin/main' into mcy/legalize
mcy Feb 7, 2025
cddd440
lint
mcy Feb 7, 2025
e321aaa
suggestions for reserved names
mcy Feb 10, 2025
e8e6a03
add more context to bad-nest errors
mcy Feb 10, 2025
21f75e5
cr
mcy Feb 10, 2025
ca53838
be more precise about classifying single-element paths
mcy Feb 10, 2025
58ffdca
legalize option values
mcy Feb 11, 2025
da83bd5
improve span for incomplete signatures
mcy Feb 11, 2025
5bdfe90
add more validation for types
mcy Feb 11, 2025
5d3ddb3
move a var
mcy Feb 11, 2025
e3318a4
legalize extn ranges
mcy Feb 11, 2025
2c9bfc9
lint
mcy Feb 11, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions experimental/parser/testdata/parser/def/bare_bodies.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto2";

package test;

message M {
int32 x = 1;
{
int32 y = 2;
}
}

{
message N {
int32 y = 2;
}
}
28 changes: 28 additions & 0 deletions experimental/parser/testdata/parser/def/bare_bodies.proto.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
decls:
- syntax: { kind: KIND_SYNTAX, value.literal.string_value: "proto2" }
- package.path.components: [{ ident: "test" }]
- def:
kind: KIND_MESSAGE
name.components: [{ ident: "M" }]
body.decls:
- def:
kind: KIND_FIELD
name.components: [{ ident: "x" }]
type.path.components: [{ ident: "int32" }]
value.literal.int_value: 1
- body.decls:
- def:
kind: KIND_FIELD
name.components: [{ ident: "y" }]
type.path.components: [{ ident: "int32" }]
value.literal.int_value: 2
- body.decls:
- def:
kind: KIND_MESSAGE
name.components: [{ ident: "N" }]
body.decls:
- def:
kind: KIND_FIELD
name.components: [{ ident: "y" }]
type.path.components: [{ ident: "int32" }]
value.literal.int_value: 2
25 changes: 25 additions & 0 deletions experimental/parser/testdata/parser/import/in_message.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto2";

package test;

message M {
import "foo.proto";
import public "foo.proto";
import weak "foo.proto";

import foo.proto;
}
15 changes: 15 additions & 0 deletions experimental/parser/testdata/parser/import/in_message.proto.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
decls:
- syntax: { kind: KIND_SYNTAX, value.literal.string_value: "proto2" }
- package.path.components: [{ ident: "test" }]
- def:
kind: KIND_MESSAGE
name.components: [{ ident: "M" }]
body.decls:
- import.import_path.literal.string_value: "foo.proto"
- import:
modifier: MODIFIER_PUBLIC
import_path.literal.string_value: "foo.proto"
- import:
modifier: MODIFIER_WEAK
import_path.literal.string_value: "foo.proto"
- import.import_path.path.components: [{ ident: "foo" }, { ident: "proto", separator: SEPARATOR_DOT }]
4 changes: 2 additions & 2 deletions experimental/parser/testdata/parser/import/repeated.proto
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ syntax = "proto2";
package test;

import "foo.proto";
import "foo.proto"; // Second
import "foo\x2eproto";

import "foo\x2eproto";
import "foo.proto"; // This should not trip the diagnostic again.
28 changes: 28 additions & 0 deletions experimental/parser/testdata/parser/method/bad_type.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto2";

package test;

service Foo {
rpc Bar1(optional foo.Bar) returns (foo.Bar);
rpc Bar2(foo.Bar) returns (repeated foo.Bar);
rpc Bar2(foo.Bar) returns repeated foo.Bar;
rpc Bar3(map<string, foo.Bar>) returns (foo.Bar);
rpc Bar4(string, foo.Bar) returns (foo.Bar);
rpc Bar5(foo.Bar) returns (foo.Bar, stream string);
rpc Bar6(stream repeated foo.Bar) returns (foo.Bar);
rpc Bar7(stream map<string, foo.Bar>) returns (foo.Bar);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
error: missing `(...)` around method return type
--> testdata/parser/method/bad_type.proto:22:31
|
22 | rpc Bar2(foo.Bar) returns repeated foo.Bar;
| ^^^^^^^^^^^^^^^^ help: replace this with `(repeated foo.Bar)`

encountered 1 error
101 changes: 101 additions & 0 deletions experimental/parser/testdata/parser/method/bad_type.proto.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
decls:
- syntax: { kind: KIND_SYNTAX, value.literal.string_value: "proto2" }
- package.path.components: [{ ident: "test" }]
- def:
kind: KIND_SERVICE
name.components: [{ ident: "Foo" }]
body.decls:
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar1" }]
signature:
inputs:
- prefixed:
prefix: PREFIX_OPTIONAL
type.path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
outputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar2" }]
signature:
inputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
outputs:
- prefixed:
prefix: PREFIX_REPEATED
type.path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar2" }]
signature:
inputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
outputs:
- prefixed:
prefix: PREFIX_REPEATED
type.path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar3" }]
signature:
inputs:
- generic:
path.components: [{ ident: "map" }]
args:
- path.components: [{ ident: "string" }]
- path.components:
- ident: "foo"
- { ident: "Bar", separator: SEPARATOR_DOT }
outputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar4" }]
signature:
inputs:
- path.components: [{ ident: "string" }]
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
outputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar5" }]
signature:
inputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
outputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- prefixed:
prefix: PREFIX_STREAM
type.path.components: [{ ident: "string" }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar6" }]
signature:
inputs:
- prefixed:
prefix: PREFIX_STREAM
type.prefixed:
prefix: PREFIX_REPEATED
type.path.components:
- ident: "foo"
- { ident: "Bar", separator: SEPARATOR_DOT }
outputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar7" }]
signature:
inputs:
- prefixed:
prefix: PREFIX_STREAM
type.generic:
path.components: [{ ident: "map" }]
args:
- path.components: [{ ident: "string" }]
- path.components:
- ident: "foo"
- { ident: "Bar", separator: SEPARATOR_DOT }
outputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
27 changes: 27 additions & 0 deletions experimental/parser/testdata/parser/method/incomplete.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto2";

package test;

service Foo {
rpc Bar1(foo.Bar) returns foo.Bar;
rpc Bar2(foo.Bar);
rpc Bar3 returns (foo.Bar);
rpc Bar4(foo.Bar) returns () {}
rpc Bar5() returns (stream foo.Bar);
rpc Bar6() returns;
rpc Bar7() returns stream foo.Bar;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
error: missing `(...)` around method return type
--> testdata/parser/method/incomplete.proto:20:31
|
20 | rpc Bar1(foo.Bar) returns foo.Bar;
| ^^^^^^^ help: replace this with `(foo.Bar)`

error: unexpected `;` after `returns`
--> testdata/parser/method/incomplete.proto:25:23
|
25 | rpc Bar6() returns;
| ^ expected `(`

error: missing `(...)` around method return type
--> testdata/parser/method/incomplete.proto:26:24
|
26 | rpc Bar7() returns stream foo.Bar;
| ^^^^^^^^^^^^^^ help: replace this with `(stream foo.Bar)`

encountered 3 errors
49 changes: 49 additions & 0 deletions experimental/parser/testdata/parser/method/incomplete.proto.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
decls:
- syntax: { kind: KIND_SYNTAX, value.literal.string_value: "proto2" }
- package.path.components: [{ ident: "test" }]
- def:
kind: KIND_SERVICE
name.components: [{ ident: "Foo" }]
body.decls:
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar1" }]
signature:
inputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
outputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar2" }]
signature.inputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar3" }]
signature.outputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar4" }]
signature.inputs:
- path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
body: {}
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar5" }]
signature.outputs:
- prefixed:
prefix: PREFIX_STREAM
type.path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar6" }]
signature: {}
- def:
kind: KIND_METHOD
name.components: [{ ident: "Bar7" }]
signature.outputs:
- prefixed:
prefix: PREFIX_STREAM
type.path.components: [{ ident: "foo" }, { ident: "Bar", separator: SEPARATOR_DOT }]
25 changes: 25 additions & 0 deletions experimental/parser/testdata/parser/method/ok.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2020-2024 Buf Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

syntax = "proto2";

package test;

service Foo {
rpc Bar1(foo.Bar) returns (foo.Bar);
rpc Bar2(foo.Bar) returns (foo.Bar) {}
rpc Bar3(stream foo.Bar) returns (foo.Bar);
rpc Bar4(foo.Bar) returns (stream foo.Bar) {}
rpc Bar5(stream foo.Bar) returns (stream foo.Bar);
}
Loading