Skip to content

Commit c476083

Browse files
committed
Merge remote-tracking branch 'origin/main' into mcy/more-suggestions
2 parents f1bfdab + 3a19ea5 commit c476083

File tree

24 files changed

+731
-310
lines changed

24 files changed

+731
-310
lines changed

experimental/ast/decl_def.go

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package ast
1717
import (
1818
"github.com/bufbuild/protocompile/experimental/report"
1919
"github.com/bufbuild/protocompile/experimental/token"
20+
"github.com/bufbuild/protocompile/experimental/token/keyword"
2021
"github.com/bufbuild/protocompile/internal/arena"
2122
)
2223

@@ -87,7 +88,7 @@ type DeclDefArgs struct {
8788
// introduced by a special keyword, this will be a [TypePath] whose single
8889
// identifier is that keyword.
8990
//
90-
// See [DeclDef.Keyword].
91+
// See [DeclDef.KeywordToken].
9192
func (d DeclDef) Type() TypeAny {
9293
if d.IsZero() {
9394
return TypeAny{}
@@ -101,19 +102,28 @@ func (d DeclDef) SetType(ty TypeAny) {
101102
d.raw.ty = ty.raw
102103
}
103104

104-
// Keyword returns the introducing keyword for this definition, if
105+
// KeywordToken returns the introducing keyword for this definition, if
105106
// there is one.
106107
//
107108
// See [DeclDef.Type] for details on where this keyword comes from.
108-
func (d DeclDef) Keyword() token.Token {
109+
func (d DeclDef) Keyword() keyword.Keyword {
110+
return d.KeywordToken().Keyword()
111+
}
112+
113+
// KeywordToken returns the introducing keyword token for this definition, if
114+
// there is one.
115+
//
116+
// See [DeclDef.Type] for details on where this keyword comes from.
117+
func (d DeclDef) KeywordToken() token.Token {
109118
path := d.Type().AsPath()
110119
if path.IsZero() {
111120
return token.Zero
112121
}
113122

114123
ident := path.Path.AsIdent()
115-
switch ident.Text() {
116-
case "message", "enum", "service", "extend", "oneof", "group", "rpc", "option":
124+
switch ident.Keyword() {
125+
case keyword.Message, keyword.Enum, keyword.Service, keyword.Extend,
126+
keyword.Oneof, keyword.Group, keyword.RPC, keyword.Option:
117127
return ident
118128
default:
119129
return token.Zero
@@ -234,7 +244,7 @@ func (d DeclDef) Semicolon() token.Token {
234244
// See [DeclDef.Classify].
235245
func (d DeclDef) AsMessage() DefMessage {
236246
return DefMessage{
237-
Keyword: d.Keyword(),
247+
Keyword: d.KeywordToken(),
238248
Name: d.Name().AsIdent(),
239249
Body: d.Body(),
240250
Decl: d,
@@ -250,7 +260,7 @@ func (d DeclDef) AsMessage() DefMessage {
250260
// See [DeclDef.Classify].
251261
func (d DeclDef) AsEnum() DefEnum {
252262
return DefEnum{
253-
Keyword: d.Keyword(),
263+
Keyword: d.KeywordToken(),
254264
Name: d.Name().AsIdent(),
255265
Body: d.Body(),
256266
Decl: d,
@@ -266,7 +276,7 @@ func (d DeclDef) AsEnum() DefEnum {
266276
// See [DeclDef.Classify].
267277
func (d DeclDef) AsService() DefService {
268278
return DefService{
269-
Keyword: d.Keyword(),
279+
Keyword: d.KeywordToken(),
270280
Name: d.Name().AsIdent(),
271281
Body: d.Body(),
272282
Decl: d,
@@ -281,7 +291,7 @@ func (d DeclDef) AsService() DefService {
281291
// See [DeclDef.Classify].
282292
func (d DeclDef) AsExtend() DefExtend {
283293
return DefExtend{
284-
Keyword: d.Keyword(),
294+
Keyword: d.KeywordToken(),
285295
Extendee: d.Name(),
286296
Body: d.Body(),
287297
Decl: d,
@@ -316,7 +326,7 @@ func (d DeclDef) AsField() DefField {
316326
// See [DeclDef.Classify].
317327
func (d DeclDef) AsOneof() DefOneof {
318328
return DefOneof{
319-
Keyword: d.Keyword(),
329+
Keyword: d.KeywordToken(),
320330
Name: d.Name().AsIdent(),
321331
Body: d.Body(),
322332
Decl: d,
@@ -332,7 +342,7 @@ func (d DeclDef) AsOneof() DefOneof {
332342
// See [DeclDef.Classify].
333343
func (d DeclDef) AsGroup() DefGroup {
334344
return DefGroup{
335-
Keyword: d.Keyword(),
345+
Keyword: d.KeywordToken(),
336346
Name: d.Name().AsIdent(),
337347
Equals: d.Equals(),
338348
Tag: d.Value(),
@@ -368,7 +378,7 @@ func (d DeclDef) AsEnumValue() DefEnumValue {
368378
// See [DeclDef.Classify].
369379
func (d DeclDef) AsMethod() DefMethod {
370380
return DefMethod{
371-
Keyword: d.Keyword(),
381+
Keyword: d.KeywordToken(),
372382
Name: d.Name().AsIdent(),
373383
Signature: d.Signature(),
374384
Body: d.Body(),
@@ -384,7 +394,7 @@ func (d DeclDef) AsMethod() DefMethod {
384394
// See [DeclDef.Classify].
385395
func (d DeclDef) AsOption() DefOption {
386396
return DefOption{
387-
Keyword: d.Keyword(),
397+
Keyword: d.KeywordToken(),
388398
Option: Option{
389399
Path: d.Name(),
390400
Equals: d.Equals(),
@@ -399,7 +409,7 @@ func (d DeclDef) AsOption() DefOption {
399409
// definition it's supposed to represent.
400410
//
401411
// To select which definition this probably is, this function looks at
402-
// [DeclDef.Keyword]. If there is no keyword or it isn't something that it
412+
// [DeclDef.KeywordToken]. If there is no keyword or it isn't something that it
403413
// recognizes, it is classified as either an enum value or a field, depending on
404414
// whether this definition has a type.
405415
//
@@ -411,7 +421,7 @@ func (d DeclDef) Classify() DefKind {
411421
return DefKindInvalid
412422
}
413423

414-
switch d.Keyword().Text() {
424+
switch d.KeywordToken().Text() {
415425
case "message":
416426
if !d.Body().IsZero() {
417427
return DefKindMessage

experimental/ast/decl_file.go

Lines changed: 43 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/bufbuild/protocompile/experimental/report"
1919
"github.com/bufbuild/protocompile/experimental/seq"
2020
"github.com/bufbuild/protocompile/experimental/token"
21+
"github.com/bufbuild/protocompile/experimental/token/keyword"
2122
"github.com/bufbuild/protocompile/internal/arena"
2223
"github.com/bufbuild/protocompile/internal/iter"
2324
)
@@ -35,7 +36,7 @@ type File struct {
3536
DeclBody
3637
}
3738

38-
// Syntax returns this file's pragma, if it has one.
39+
// Syntax returns this file's declaration, if it has one.
3940
func (f File) Syntax() (syntax DeclSyntax) {
4041
seq.Values(f.Decls())(func(d DeclAny) bool {
4142
if s := d.AsSyntax(); !s.IsZero() {
@@ -76,7 +77,7 @@ func (f File) Imports() iter.Seq2[int, DeclImport] {
7677
}
7778
}
7879

79-
// DeclSyntax represents a language pragma, such as the syntax or edition
80+
// DeclSyntax represents a language declaration, such as the syntax or edition
8081
// keywords.
8182
//
8283
// # Grammar
@@ -103,23 +104,28 @@ type DeclSyntaxArgs struct {
103104
Semicolon token.Token
104105
}
105106

106-
// Keyword returns the keyword for this pragma.
107-
func (d DeclSyntax) Keyword() token.Token {
107+
// Keyword returns the keyword for this declaration.
108+
func (d DeclSyntax) Keyword() keyword.Keyword {
109+
return d.KeywordToken().Keyword()
110+
}
111+
112+
// KeywordToken returns the keyword token for this declaration.
113+
func (d DeclSyntax) KeywordToken() token.Token {
108114
if d.IsZero() {
109115
return token.Zero
110116
}
111117

112118
return d.raw.keyword.In(d.Context())
113119
}
114120

115-
// IsSyntax checks whether this is an OG syntax pragma.
121+
// IsSyntax checks whether this is an OG syntax declaration.
116122
func (d DeclSyntax) IsSyntax() bool {
117-
return d.Keyword().Text() == "syntax"
123+
return d.Keyword() == keyword.Syntax
118124
}
119125

120-
// IsEdition checks whether this is a new-style edition pragma.
126+
// IsEdition checks whether this is a new-style edition declaration.
121127
func (d DeclSyntax) IsEdition() bool {
122-
return d.Keyword().Text() == "edition"
128+
return d.Keyword() == keyword.Edition
123129
}
124130

125131
// Equals returns the equals sign after the keyword.
@@ -133,7 +139,7 @@ func (d DeclSyntax) Equals() token.Token {
133139
return d.raw.equals.In(d.Context())
134140
}
135141

136-
// Value returns the value expression of this pragma.
142+
// Value returns the value expression of this declaration.
137143
//
138144
// May be zero, if the user wrote something like syntax;. It can also be
139145
// a number or an identifier, for cases like edition = 2024; or syntax = proto2;.
@@ -145,7 +151,7 @@ func (d DeclSyntax) Value() ExprAny {
145151
return newExprAny(d.Context(), d.raw.value)
146152
}
147153

148-
// SetValue sets the expression for this pragma's value.
154+
// SetValue sets the expression for this declaration's value.
149155
//
150156
// If passed zero, this clears the value (e.g., for syntax = ;).
151157
func (d DeclSyntax) SetValue(expr ExprAny) {
@@ -170,7 +176,7 @@ func (d DeclSyntax) SetOptions(opts CompactOptions) {
170176
d.raw.options = d.Context().Nodes().options.Compress(opts.raw)
171177
}
172178

173-
// Semicolon returns this pragma's ending semicolon.
179+
// Semicolon returns this declaration's ending semicolon.
174180
//
175181
// May be zero, if the user forgot it.
176182
func (d DeclSyntax) Semicolon() token.Token {
@@ -187,7 +193,7 @@ func (d DeclSyntax) Span() report.Span {
187193
return report.Span{}
188194
}
189195

190-
return report.Join(d.Keyword(), d.Equals(), d.Value(), d.Semicolon())
196+
return report.Join(d.KeywordToken(), d.Equals(), d.Value(), d.Semicolon())
191197
}
192198

193199
func wrapDeclSyntax(c Context, ptr arena.Pointer[rawDeclSyntax]) DeclSyntax {
@@ -219,8 +225,13 @@ type DeclPackageArgs struct {
219225
Semicolon token.Token
220226
}
221227

222-
// Keyword returns the "package" keyword for this declaration.
223-
func (d DeclPackage) Keyword() token.Token {
228+
// Keyword returns the keyword for this declaration.
229+
func (d DeclPackage) Keyword() keyword.Keyword {
230+
return d.KeywordToken().Keyword()
231+
}
232+
233+
// KeywordToken returns the "package" token for this declaration.
234+
func (d DeclPackage) KeywordToken() token.Token {
224235
if d.IsZero() {
225236
return token.Zero
226237
}
@@ -274,7 +285,7 @@ func (d DeclPackage) Span() report.Span {
274285
return report.Span{}
275286
}
276287

277-
return report.Join(d.Keyword(), d.Path(), d.Semicolon())
288+
return report.Join(d.KeywordToken(), d.Path(), d.Semicolon())
278289
}
279290

280291
func wrapDeclPackage(c Context, ptr arena.Pointer[rawDeclPackage]) DeclPackage {
@@ -306,19 +317,29 @@ type DeclImportArgs struct {
306317
Semicolon token.Token
307318
}
308319

309-
// Keyword returns the "import" keyword for this pragma.
310-
func (d DeclImport) Keyword() token.Token {
320+
// Keyword returns the keyword for this declaration.
321+
func (d DeclImport) Keyword() keyword.Keyword {
322+
return d.KeywordToken().Keyword()
323+
}
324+
325+
// KeywordToken returns the "import" keyword for this declaration.
326+
func (d DeclImport) KeywordToken() token.Token {
311327
if d.IsZero() {
312328
return token.Zero
313329
}
314330

315331
return d.raw.keyword.In(d.Context())
316332
}
317333

318-
// Keyword returns the modifier keyword for this pragma.
334+
// Modifier returns the modifier keyword for this declaration.
335+
func (d DeclImport) Modifier() keyword.Keyword {
336+
return d.ModifierToken().Keyword()
337+
}
338+
339+
// ModifierToken returns the modifier token for this declaration.
319340
//
320341
// May be zero if there is no modifier.
321-
func (d DeclImport) Modifier() token.Token {
342+
func (d DeclImport) ModifierToken() token.Token {
322343
if d.IsZero() {
323344
return token.Zero
324345
}
@@ -328,12 +349,12 @@ func (d DeclImport) Modifier() token.Token {
328349

329350
// IsSyntax checks whether this is an "import public".
330351
func (d DeclImport) IsPublic() bool {
331-
return d.Modifier().Text() == "public"
352+
return d.Modifier() == keyword.Public
332353
}
333354

334355
// IsEdition checks whether this is an "import weak".
335356
func (d DeclImport) IsWeak() bool {
336-
return d.Modifier().Text() == "weak"
357+
return d.Modifier() == keyword.Weak
337358
}
338359

339360
// ImportPath returns the file path for this import as a string.
@@ -389,7 +410,7 @@ func (d DeclImport) Span() report.Span {
389410
return report.Span{}
390411
}
391412

392-
return report.Join(d.Keyword(), d.Modifier(), d.ImportPath(), d.Semicolon())
413+
return report.Join(d.KeywordToken(), d.ModifierToken(), d.ImportPath(), d.Semicolon())
393414
}
394415

395416
func wrapDeclImport(c Context, ptr arena.Pointer[rawDeclImport]) DeclImport {

experimental/ast/decl_range.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/bufbuild/protocompile/experimental/report"
1919
"github.com/bufbuild/protocompile/experimental/seq"
2020
"github.com/bufbuild/protocompile/experimental/token"
21+
"github.com/bufbuild/protocompile/experimental/token/keyword"
2122
"github.com/bufbuild/protocompile/internal/arena"
2223
)
2324

@@ -44,7 +45,12 @@ type DeclRangeArgs struct {
4445
}
4546

4647
// Keyword returns the keyword for this range.
47-
func (d DeclRange) Keyword() token.Token {
48+
func (d DeclRange) Keyword() keyword.Keyword {
49+
return d.KeywordToken().Keyword()
50+
}
51+
52+
// KeywordToken returns the keyword token for this range.
53+
func (d DeclRange) KeywordToken() token.Token {
4854
if d.IsZero() {
4955
return token.Zero
5056
}
@@ -54,12 +60,12 @@ func (d DeclRange) Keyword() token.Token {
5460

5561
// IsExtensions checks whether this is an extension range.
5662
func (d DeclRange) IsExtensions() bool {
57-
return d.Keyword().Text() == "extensions"
63+
return d.Keyword() == keyword.Extensions
5864
}
5965

6066
// IsReserved checks whether this is a reserved range.
6167
func (d DeclRange) IsReserved() bool {
62-
return d.Keyword().Text() == "reserved"
68+
return d.Keyword() == keyword.Reserved
6369
}
6470

6571
// Ranges returns the sequence of expressions denoting the ranges in this
@@ -118,10 +124,10 @@ func (d DeclRange) Span() report.Span {
118124
case d.IsZero():
119125
return report.Span{}
120126
case r.Len() == 0:
121-
return report.Join(d.Keyword(), d.Semicolon(), d.Options())
127+
return report.Join(d.KeywordToken(), d.Semicolon(), d.Options())
122128
default:
123129
return report.Join(
124-
d.Keyword(), d.Semicolon(), d.Options(),
130+
d.KeywordToken(), d.Semicolon(), d.Options(),
125131
r.At(0),
126132
r.At(r.Len()-1),
127133
)

0 commit comments

Comments
 (0)