-
Notifications
You must be signed in to change notification settings - Fork 16
/
file.go
358 lines (323 loc) · 10.1 KB
/
file.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
// 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.
package ast
import "fmt"
// FileDeclNode is a placeholder interface for AST nodes that represent files.
// This allows NoSourceNode to be used in place of *FileNode for some usages.
type FileDeclNode interface {
NodeWithOptions
Name() string
NodeInfo(n Node) NodeInfo
}
var _ FileDeclNode = (*FileNode)(nil)
var _ FileDeclNode = (*NoSourceNode)(nil)
// FileNode is the root of the AST hierarchy. It represents an entire
// protobuf source file.
type FileNode struct {
compositeNode
fileInfo *FileInfo
// A file has either a Syntax or Edition node, never both.
// If both are nil, neither declaration is present and the
// file is assumed to use "proto2" syntax.
Syntax *SyntaxNode
Edition *EditionNode
Decls []FileElement
// This synthetic node allows access to final comments and whitespace
EOF *RuneNode
}
// NewFileNode creates a new *FileNode. The syntax parameter is optional. If it
// is absent, it means the file had no syntax declaration.
//
// This function panics if the concrete type of any element of decls is not
// from this package.
func NewFileNode(info *FileInfo, syntax *SyntaxNode, decls []FileElement, eof Token) *FileNode {
return newFileNode(info, syntax, nil, decls, eof)
}
// NewFileNodeWithEdition creates a new *FileNode. The edition parameter is required. If a file
// has no edition declaration, use NewFileNode instead.
//
// This function panics if the concrete type of any element of decls is not
// from this package.
func NewFileNodeWithEdition(info *FileInfo, edition *EditionNode, decls []FileElement, eof Token) *FileNode {
if edition == nil {
panic("edition is nil")
}
return newFileNode(info, nil, edition, decls, eof)
}
func newFileNode(info *FileInfo, syntax *SyntaxNode, edition *EditionNode, decls []FileElement, eof Token) *FileNode {
numChildren := len(decls) + 1
if syntax != nil || edition != nil {
numChildren++
}
children := make([]Node, 0, numChildren)
if syntax != nil {
children = append(children, syntax)
} else if edition != nil {
children = append(children, edition)
}
for _, decl := range decls {
switch decl := decl.(type) {
case *PackageNode, *ImportNode, *OptionNode, *MessageNode,
*EnumNode, *ExtendNode, *ServiceNode, *EmptyDeclNode:
default:
panic(fmt.Sprintf("invalid FileElement type: %T", decl))
}
children = append(children, decl)
}
eofNode := NewRuneNode(0, eof)
children = append(children, eofNode)
return &FileNode{
compositeNode: compositeNode{
children: children,
},
fileInfo: info,
Syntax: syntax,
Edition: edition,
Decls: decls,
EOF: eofNode,
}
}
// NewEmptyFileNode returns an empty AST for a file with the given name.
func NewEmptyFileNode(filename string) *FileNode {
fileInfo := NewFileInfo(filename, []byte{})
return NewFileNode(fileInfo, nil, nil, fileInfo.AddToken(0, 0))
}
func (f *FileNode) Name() string {
return f.fileInfo.Name()
}
func (f *FileNode) NodeInfo(n Node) NodeInfo {
return f.fileInfo.NodeInfo(n)
}
func (f *FileNode) TokenInfo(t Token) NodeInfo {
return f.fileInfo.TokenInfo(t)
}
func (f *FileNode) ItemInfo(i Item) ItemInfo {
return f.fileInfo.ItemInfo(i)
}
func (f *FileNode) GetItem(i Item) (Token, Comment) {
return f.fileInfo.GetItem(i)
}
func (f *FileNode) Items() Sequence[Item] {
return f.fileInfo.Items()
}
func (f *FileNode) Tokens() Sequence[Token] {
return f.fileInfo.Tokens()
}
func (f *FileNode) RangeOptions(fn func(*OptionNode) bool) {
for _, decl := range f.Decls {
if opt, ok := decl.(*OptionNode); ok {
if !fn(opt) {
return
}
}
}
}
// FileElement is an interface implemented by all AST nodes that are
// allowed as top-level declarations in the file.
type FileElement interface {
Node
fileElement()
}
var _ FileElement = (*ImportNode)(nil)
var _ FileElement = (*PackageNode)(nil)
var _ FileElement = (*OptionNode)(nil)
var _ FileElement = (*MessageNode)(nil)
var _ FileElement = (*EnumNode)(nil)
var _ FileElement = (*ExtendNode)(nil)
var _ FileElement = (*ServiceNode)(nil)
var _ FileElement = (*EmptyDeclNode)(nil)
// SyntaxNode represents a syntax declaration, which if present must be
// the first non-comment content. Example:
//
// syntax = "proto2";
//
// Files that don't have a syntax node are assumed to use proto2 syntax.
type SyntaxNode struct {
compositeNode
Keyword *KeywordNode
Equals *RuneNode
Syntax StringValueNode
Semicolon *RuneNode
}
// NewSyntaxNode creates a new *SyntaxNode. All four arguments must be non-nil:
// - keyword: The token corresponding to the "syntax" keyword.
// - equals: The token corresponding to the "=" rune.
// - syntax: The actual syntax value, e.g. "proto2" or "proto3".
// - semicolon: The token corresponding to the ";" rune that ends the declaration.
func NewSyntaxNode(keyword *KeywordNode, equals *RuneNode, syntax StringValueNode, semicolon *RuneNode) *SyntaxNode {
if keyword == nil {
panic("keyword is nil")
}
if equals == nil {
panic("equals is nil")
}
if syntax == nil {
panic("syntax is nil")
}
var children []Node
if semicolon == nil {
children = []Node{keyword, equals, syntax}
} else {
children = []Node{keyword, equals, syntax, semicolon}
}
return &SyntaxNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Equals: equals,
Syntax: syntax,
Semicolon: semicolon,
}
}
// EditionNode represents an edition declaration, which if present must be
// the first non-comment content. Example:
//
// edition = "2023";
//
// Files may include either an edition node or a syntax node, but not both.
// If neither are present, the file is assumed to use proto2 syntax.
type EditionNode struct {
compositeNode
Keyword *KeywordNode
Equals *RuneNode
Edition StringValueNode
Semicolon *RuneNode
}
// NewEditionNode creates a new *EditionNode. All four arguments must be non-nil:
// - keyword: The token corresponding to the "edition" keyword.
// - equals: The token corresponding to the "=" rune.
// - edition: The actual edition value, e.g. "2023".
// - semicolon: The token corresponding to the ";" rune that ends the declaration.
func NewEditionNode(keyword *KeywordNode, equals *RuneNode, edition StringValueNode, semicolon *RuneNode) *EditionNode {
if keyword == nil {
panic("keyword is nil")
}
if equals == nil {
panic("equals is nil")
}
if edition == nil {
panic("edition is nil")
}
if semicolon == nil {
panic("semicolon is nil")
}
children := []Node{keyword, equals, edition, semicolon}
return &EditionNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Equals: equals,
Edition: edition,
Semicolon: semicolon,
}
}
// ImportNode represents an import statement. Example:
//
// import "google/protobuf/empty.proto";
type ImportNode struct {
compositeNode
Keyword *KeywordNode
// Optional; if present indicates this is a public import
Public *KeywordNode
// Optional; if present indicates this is a weak import
Weak *KeywordNode
Name StringValueNode
Semicolon *RuneNode
}
// NewImportNode creates a new *ImportNode. The public and weak arguments are optional
// and only one or the other (or neither) may be specified, not both. When public is
// non-nil, it indicates the "public" keyword in the import statement and means this is
// a public import. When weak is non-nil, it indicates the "weak" keyword in the import
// statement and means this is a weak import. When both are nil, this is a normal import.
// The other arguments must be non-nil:
// - keyword: The token corresponding to the "import" keyword.
// - public: The token corresponding to the optional "public" keyword.
// - weak: The token corresponding to the optional "weak" keyword.
// - name: The actual imported file name.
// - semicolon: The token corresponding to the ";" rune that ends the declaration.
func NewImportNode(keyword *KeywordNode, public *KeywordNode, weak *KeywordNode, name StringValueNode, semicolon *RuneNode) *ImportNode {
if keyword == nil {
panic("keyword is nil")
}
if name == nil {
panic("name is nil")
}
numChildren := 2
if semicolon == nil {
numChildren++
}
if public != nil || weak != nil {
numChildren++
}
children := make([]Node, 0, numChildren)
children = append(children, keyword)
if public != nil {
children = append(children, public)
} else if weak != nil {
children = append(children, weak)
}
children = append(children, name)
if semicolon != nil {
children = append(children, semicolon)
}
return &ImportNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Public: public,
Weak: weak,
Name: name,
Semicolon: semicolon,
}
}
func (*ImportNode) fileElement() {}
// PackageNode represents a package declaration. Example:
//
// package foobar.com;
type PackageNode struct {
compositeNode
Keyword *KeywordNode
Name IdentValueNode
Semicolon *RuneNode
}
func (*PackageNode) fileElement() {}
// NewPackageNode creates a new *PackageNode. All three arguments must be non-nil:
// - keyword: The token corresponding to the "package" keyword.
// - name: The package name declared for the file.
// - semicolon: The token corresponding to the ";" rune that ends the declaration.
func NewPackageNode(keyword *KeywordNode, name IdentValueNode, semicolon *RuneNode) *PackageNode {
if keyword == nil {
panic("keyword is nil")
}
if name == nil {
panic("name is nil")
}
var children []Node
if semicolon == nil {
children = []Node{keyword, name}
} else {
children = []Node{keyword, name, semicolon}
}
return &PackageNode{
compositeNode: compositeNode{
children: children,
},
Keyword: keyword,
Name: name,
Semicolon: semicolon,
}
}