@@ -18,6 +18,7 @@ import (
18
18
"github.com/bufbuild/protocompile/experimental/report"
19
19
"github.com/bufbuild/protocompile/experimental/seq"
20
20
"github.com/bufbuild/protocompile/experimental/token"
21
+ "github.com/bufbuild/protocompile/experimental/token/keyword"
21
22
"github.com/bufbuild/protocompile/internal/arena"
22
23
"github.com/bufbuild/protocompile/internal/iter"
23
24
)
@@ -35,7 +36,7 @@ type File struct {
35
36
DeclBody
36
37
}
37
38
38
- // Syntax returns this file's pragma , if it has one.
39
+ // Syntax returns this file's declaration , if it has one.
39
40
func (f File ) Syntax () (syntax DeclSyntax ) {
40
41
seq .Values (f .Decls ())(func (d DeclAny ) bool {
41
42
if s := d .AsSyntax (); ! s .IsZero () {
@@ -76,7 +77,7 @@ func (f File) Imports() iter.Seq2[int, DeclImport] {
76
77
}
77
78
}
78
79
79
- // DeclSyntax represents a language pragma , such as the syntax or edition
80
+ // DeclSyntax represents a language declaration , such as the syntax or edition
80
81
// keywords.
81
82
//
82
83
// # Grammar
@@ -103,23 +104,28 @@ type DeclSyntaxArgs struct {
103
104
Semicolon token.Token
104
105
}
105
106
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 {
108
114
if d .IsZero () {
109
115
return token .Zero
110
116
}
111
117
112
118
return d .raw .keyword .In (d .Context ())
113
119
}
114
120
115
- // IsSyntax checks whether this is an OG syntax pragma .
121
+ // IsSyntax checks whether this is an OG syntax declaration .
116
122
func (d DeclSyntax ) IsSyntax () bool {
117
- return d .Keyword (). Text () == "syntax"
123
+ return d .Keyword () == keyword . Syntax
118
124
}
119
125
120
- // IsEdition checks whether this is a new-style edition pragma .
126
+ // IsEdition checks whether this is a new-style edition declaration .
121
127
func (d DeclSyntax ) IsEdition () bool {
122
- return d .Keyword (). Text () == "edition"
128
+ return d .Keyword () == keyword . Edition
123
129
}
124
130
125
131
// Equals returns the equals sign after the keyword.
@@ -133,7 +139,7 @@ func (d DeclSyntax) Equals() token.Token {
133
139
return d .raw .equals .In (d .Context ())
134
140
}
135
141
136
- // Value returns the value expression of this pragma .
142
+ // Value returns the value expression of this declaration .
137
143
//
138
144
// May be zero, if the user wrote something like syntax;. It can also be
139
145
// a number or an identifier, for cases like edition = 2024; or syntax = proto2;.
@@ -145,7 +151,7 @@ func (d DeclSyntax) Value() ExprAny {
145
151
return newExprAny (d .Context (), d .raw .value )
146
152
}
147
153
148
- // SetValue sets the expression for this pragma 's value.
154
+ // SetValue sets the expression for this declaration 's value.
149
155
//
150
156
// If passed zero, this clears the value (e.g., for syntax = ;).
151
157
func (d DeclSyntax ) SetValue (expr ExprAny ) {
@@ -170,7 +176,7 @@ func (d DeclSyntax) SetOptions(opts CompactOptions) {
170
176
d .raw .options = d .Context ().Nodes ().options .Compress (opts .raw )
171
177
}
172
178
173
- // Semicolon returns this pragma 's ending semicolon.
179
+ // Semicolon returns this declaration 's ending semicolon.
174
180
//
175
181
// May be zero, if the user forgot it.
176
182
func (d DeclSyntax ) Semicolon () token.Token {
@@ -187,7 +193,7 @@ func (d DeclSyntax) Span() report.Span {
187
193
return report.Span {}
188
194
}
189
195
190
- return report .Join (d .Keyword (), d .Equals (), d .Value (), d .Semicolon ())
196
+ return report .Join (d .KeywordToken (), d .Equals (), d .Value (), d .Semicolon ())
191
197
}
192
198
193
199
func wrapDeclSyntax (c Context , ptr arena.Pointer [rawDeclSyntax ]) DeclSyntax {
@@ -219,8 +225,13 @@ type DeclPackageArgs struct {
219
225
Semicolon token.Token
220
226
}
221
227
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 {
224
235
if d .IsZero () {
225
236
return token .Zero
226
237
}
@@ -274,7 +285,7 @@ func (d DeclPackage) Span() report.Span {
274
285
return report.Span {}
275
286
}
276
287
277
- return report .Join (d .Keyword (), d .Path (), d .Semicolon ())
288
+ return report .Join (d .KeywordToken (), d .Path (), d .Semicolon ())
278
289
}
279
290
280
291
func wrapDeclPackage (c Context , ptr arena.Pointer [rawDeclPackage ]) DeclPackage {
@@ -306,19 +317,29 @@ type DeclImportArgs struct {
306
317
Semicolon token.Token
307
318
}
308
319
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 {
311
327
if d .IsZero () {
312
328
return token .Zero
313
329
}
314
330
315
331
return d .raw .keyword .In (d .Context ())
316
332
}
317
333
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.
319
340
//
320
341
// May be zero if there is no modifier.
321
- func (d DeclImport ) Modifier () token.Token {
342
+ func (d DeclImport ) ModifierToken () token.Token {
322
343
if d .IsZero () {
323
344
return token .Zero
324
345
}
@@ -328,12 +349,12 @@ func (d DeclImport) Modifier() token.Token {
328
349
329
350
// IsSyntax checks whether this is an "import public".
330
351
func (d DeclImport ) IsPublic () bool {
331
- return d .Modifier (). Text () == "public"
352
+ return d .Modifier () == keyword . Public
332
353
}
333
354
334
355
// IsEdition checks whether this is an "import weak".
335
356
func (d DeclImport ) IsWeak () bool {
336
- return d .Modifier (). Text () == "weak"
357
+ return d .Modifier () == keyword . Weak
337
358
}
338
359
339
360
// ImportPath returns the file path for this import as a string.
@@ -389,7 +410,7 @@ func (d DeclImport) Span() report.Span {
389
410
return report.Span {}
390
411
}
391
412
392
- return report .Join (d .Keyword (), d .Modifier (), d .ImportPath (), d .Semicolon ())
413
+ return report .Join (d .KeywordToken (), d .ModifierToken (), d .ImportPath (), d .Semicolon ())
393
414
}
394
415
395
416
func wrapDeclImport (c Context , ptr arena.Pointer [rawDeclImport ]) DeclImport {
0 commit comments