summaryrefslogtreecommitdiff
path: root/vendor/github.com/jhump/protoreflect/desc/protoparse/ast.go
blob: 2b6b124423216bdb1a1a7937d10e3d74c8f175d4 (plain)
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
package protoparse

import (
	"fmt"

	"github.com/bufbuild/protocompile/ast"

	ast2 "github.com/jhump/protoreflect/desc/protoparse/ast"
)

func convertAST(file *ast.FileNode) *ast2.FileNode {
	elements := make([]ast2.FileElement, len(file.Decls))
	for i := range file.Decls {
		elements[i] = convertASTFileElement(file, file.Decls[i])
	}
	root := ast2.NewFileNode(convertASTSyntax(file, file.Syntax), elements)
	eofInfo := file.NodeInfo(file.EOF)
	root.FinalComments = convertASTComments(eofInfo.LeadingComments())
	root.FinalWhitespace = eofInfo.LeadingWhitespace()
	return root
}

func convertASTSyntax(f *ast.FileNode, s *ast.SyntaxNode) *ast2.SyntaxNode {
	return ast2.NewSyntaxNode(
		convertASTKeyword(f, s.Keyword),
		convertASTRune(f, s.Equals),
		convertASTString(f, s.Syntax),
		convertASTRune(f, s.Semicolon),
	)
}

func convertASTFileElement(f *ast.FileNode, el ast.FileElement) ast2.FileElement {
	switch el := el.(type) {
	case *ast.ImportNode:
		return convertASTImport(f, el)
	case *ast.PackageNode:
		return convertASTPackage(f, el)
	case *ast.OptionNode:
		return convertASTOption(f, el)
	case *ast.MessageNode:
		return convertASTMessage(f, el)
	case *ast.EnumNode:
		return convertASTEnum(f, el)
	case *ast.ExtendNode:
		return convertASTExtend(f, el)
	case *ast.ServiceNode:
		return convertASTService(f, el)
	case *ast.EmptyDeclNode:
		return convertASTEmpty(f, el)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.FileElement: %T", el))
	}
}

func convertASTImport(f *ast.FileNode, imp *ast.ImportNode) *ast2.ImportNode {
	var public, weak *ast2.KeywordNode
	if imp.Public != nil {
		public = convertASTKeyword(f, imp.Public)
	}
	if imp.Weak != nil {
		weak = convertASTKeyword(f, imp.Weak)
	}
	return ast2.NewImportNode(
		convertASTKeyword(f, imp.Keyword),
		public, weak,
		convertASTString(f, imp.Name),
		convertASTRune(f, imp.Semicolon),
	)
}

func convertASTPackage(f *ast.FileNode, p *ast.PackageNode) *ast2.PackageNode {
	return ast2.NewPackageNode(
		convertASTKeyword(f, p.Keyword),
		convertASTIdent(f, p.Name),
		convertASTRune(f, p.Semicolon),
	)
}

func convertASTOption(f *ast.FileNode, o *ast.OptionNode) *ast2.OptionNode {
	if o.Keyword == nil {
		return ast2.NewCompactOptionNode(
			convertASTOptionName(f, o.Name),
			convertASTRune(f, o.Equals),
			convertASTValue(f, o.Val),
		)
	}
	return ast2.NewOptionNode(
		convertASTKeyword(f, o.Keyword),
		convertASTOptionName(f, o.Name),
		convertASTRune(f, o.Equals),
		convertASTValue(f, o.Val),
		convertASTRune(f, o.Semicolon),
	)
}

func convertASTOptionName(f *ast.FileNode, n *ast.OptionNameNode) *ast2.OptionNameNode {
	parts := make([]*ast2.FieldReferenceNode, len(n.Parts))
	for i := range n.Parts {
		parts[i] = convertASTFieldReference(f, n.Parts[i])
	}
	dots := make([]*ast2.RuneNode, len(n.Dots))
	for i := range n.Dots {
		dots[i] = convertASTRune(f, n.Dots[i])
	}
	return ast2.NewOptionNameNode(parts, dots)
}

func convertASTFieldReference(f *ast.FileNode, n *ast.FieldReferenceNode) *ast2.FieldReferenceNode {
	switch {
	case n.IsExtension():
		return ast2.NewExtensionFieldReferenceNode(
			convertASTRune(f, n.Open),
			convertASTIdent(f, n.Name),
			convertASTRune(f, n.Close),
		)
	case n.IsAnyTypeReference():
		return ast2.NewAnyTypeReferenceNode(
			convertASTRune(f, n.Open),
			convertASTIdent(f, n.URLPrefix),
			convertASTRune(f, n.Slash),
			convertASTIdent(f, n.Name),
			convertASTRune(f, n.Close),
		)
	default:
		return ast2.NewFieldReferenceNode(convertASTIdent(f, n.Name).(*ast2.IdentNode))
	}
}

func convertASTMessage(f *ast.FileNode, m *ast.MessageNode) *ast2.MessageNode {
	decls := make([]ast2.MessageElement, len(m.Decls))
	for i := range m.Decls {
		decls[i] = convertASTMessageElement(f, m.Decls[i])
	}
	return ast2.NewMessageNode(
		convertASTKeyword(f, m.Keyword),
		convertASTIdentToken(f, m.Name),
		convertASTRune(f, m.OpenBrace),
		decls,
		convertASTRune(f, m.CloseBrace),
	)
}

func convertASTMessageElement(f *ast.FileNode, el ast.MessageElement) ast2.MessageElement {
	switch el := el.(type) {
	case *ast.OptionNode:
		return convertASTOption(f, el)
	case *ast.FieldNode:
		return convertASTField(f, el)
	case *ast.MapFieldNode:
		return convertASTMapField(f, el)
	case *ast.OneofNode:
		return convertASTOneOf(f, el)
	case *ast.GroupNode:
		return convertASTGroup(f, el)
	case *ast.MessageNode:
		return convertASTMessage(f, el)
	case *ast.EnumNode:
		return convertASTEnum(f, el)
	case *ast.ExtendNode:
		return convertASTExtend(f, el)
	case *ast.ExtensionRangeNode:
		return convertASTExtensions(f, el)
	case *ast.ReservedNode:
		return convertASTReserved(f, el)
	case *ast.EmptyDeclNode:
		return convertASTEmpty(f, el)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.MessageElement: %T", el))
	}
}

func convertASTField(f *ast.FileNode, fld *ast.FieldNode) *ast2.FieldNode {
	var lbl *ast2.KeywordNode
	if fld.Label.KeywordNode != nil {
		lbl = convertASTKeyword(f, fld.Label.KeywordNode)
	}
	var opts *ast2.CompactOptionsNode
	if fld.Options != nil {
		opts = convertASTCompactOptions(f, fld.Options)
	}
	return ast2.NewFieldNode(
		lbl,
		convertASTIdent(f, fld.FldType),
		convertASTIdentToken(f, fld.Name),
		convertASTRune(f, fld.Equals),
		convertASTUintLiteral(f, fld.Tag),
		opts,
		convertASTRune(f, fld.Semicolon),
	)
}

func convertASTMapField(f *ast.FileNode, fld *ast.MapFieldNode) *ast2.MapFieldNode {
	var opts *ast2.CompactOptionsNode
	if fld.Options != nil {
		opts = convertASTCompactOptions(f, fld.Options)
	}
	return ast2.NewMapFieldNode(
		convertASTMapFieldType(f, fld.MapType),
		convertASTIdentToken(f, fld.Name),
		convertASTRune(f, fld.Equals),
		convertASTUintLiteral(f, fld.Tag),
		opts,
		convertASTRune(f, fld.Semicolon),
	)
}

func convertASTMapFieldType(f *ast.FileNode, t *ast.MapTypeNode) *ast2.MapTypeNode {
	return ast2.NewMapTypeNode(
		convertASTKeyword(f, t.Keyword),
		convertASTRune(f, t.OpenAngle),
		convertASTIdentToken(f, t.KeyType),
		convertASTRune(f, t.Comma),
		convertASTIdent(f, t.ValueType),
		convertASTRune(f, t.CloseAngle),
	)
}

func convertASTGroup(f *ast.FileNode, g *ast.GroupNode) *ast2.GroupNode {
	var lbl *ast2.KeywordNode
	if g.Label.KeywordNode != nil {
		lbl = convertASTKeyword(f, g.Label.KeywordNode)
	}
	var opts *ast2.CompactOptionsNode
	if g.Options != nil {
		opts = convertASTCompactOptions(f, g.Options)
	}
	decls := make([]ast2.MessageElement, len(g.Decls))
	for i := range g.Decls {
		decls[i] = convertASTMessageElement(f, g.Decls[i])
	}
	return ast2.NewGroupNode(
		lbl,
		convertASTKeyword(f, g.Keyword),
		convertASTIdentToken(f, g.Name),
		convertASTRune(f, g.Equals),
		convertASTUintLiteral(f, g.Tag),
		opts,
		convertASTRune(f, g.OpenBrace),
		decls,
		convertASTRune(f, g.CloseBrace),
	)
}

func convertASTOneOf(f *ast.FileNode, oo *ast.OneofNode) *ast2.OneOfNode {
	decls := make([]ast2.OneOfElement, len(oo.Decls))
	for i := range oo.Decls {
		decls[i] = convertASTOneOfElement(f, oo.Decls[i])
	}
	return ast2.NewOneOfNode(
		convertASTKeyword(f, oo.Keyword),
		convertASTIdentToken(f, oo.Name),
		convertASTRune(f, oo.OpenBrace),
		decls,
		convertASTRune(f, oo.CloseBrace),
	)
}

func convertASTOneOfElement(f *ast.FileNode, el ast.OneofElement) ast2.OneOfElement {
	switch el := el.(type) {
	case *ast.OptionNode:
		return convertASTOption(f, el)
	case *ast.FieldNode:
		return convertASTField(f, el)
	case *ast.GroupNode:
		return convertASTGroup(f, el)
	case *ast.EmptyDeclNode:
		return convertASTEmpty(f, el)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.OneOfElement: %T", el))
	}
}

func convertASTExtensions(f *ast.FileNode, e *ast.ExtensionRangeNode) *ast2.ExtensionRangeNode {
	var opts *ast2.CompactOptionsNode
	if e.Options != nil {
		opts = convertASTCompactOptions(f, e.Options)
	}
	ranges := make([]*ast2.RangeNode, len(e.Ranges))
	for i := range e.Ranges {
		ranges[i] = convertASTRange(f, e.Ranges[i])
	}
	commas := make([]*ast2.RuneNode, len(e.Commas))
	for i := range e.Commas {
		commas[i] = convertASTRune(f, e.Commas[i])
	}
	return ast2.NewExtensionRangeNode(
		convertASTKeyword(f, e.Keyword),
		ranges, commas, opts,
		convertASTRune(f, e.Semicolon),
	)
}

func convertASTReserved(f *ast.FileNode, r *ast.ReservedNode) *ast2.ReservedNode {
	ranges := make([]*ast2.RangeNode, len(r.Ranges))
	for i := range r.Ranges {
		ranges[i] = convertASTRange(f, r.Ranges[i])
	}
	commas := make([]*ast2.RuneNode, len(r.Commas))
	for i := range r.Commas {
		commas[i] = convertASTRune(f, r.Commas[i])
	}
	names := make([]ast2.StringValueNode, len(r.Names))
	for i := range r.Names {
		names[i] = convertASTString(f, r.Names[i])
	}
	if len(r.Ranges) > 0 {
		return ast2.NewReservedRangesNode(
			convertASTKeyword(f, r.Keyword),
			ranges, commas,
			convertASTRune(f, r.Semicolon),
		)
	}
	return ast2.NewReservedNamesNode(
		convertASTKeyword(f, r.Keyword),
		names, commas,
		convertASTRune(f, r.Semicolon),
	)
}

func convertASTRange(f *ast.FileNode, r *ast.RangeNode) *ast2.RangeNode {
	var to, max *ast2.KeywordNode
	var end ast2.IntValueNode
	if r.To != nil {
		to = convertASTKeyword(f, r.To)
	}
	if r.Max != nil {
		max = convertASTKeyword(f, r.Max)
	}
	if r.EndVal != nil {
		end = convertASTInt(f, r.EndVal)
	}
	return ast2.NewRangeNode(
		convertASTInt(f, r.StartVal),
		to, end, max,
	)
}

func convertASTEnum(f *ast.FileNode, e *ast.EnumNode) *ast2.EnumNode {
	decls := make([]ast2.EnumElement, len(e.Decls))
	for i := range e.Decls {
		decls[i] = convertASTEnumElement(f, e.Decls[i])
	}
	return ast2.NewEnumNode(
		convertASTKeyword(f, e.Keyword),
		convertASTIdentToken(f, e.Name),
		convertASTRune(f, e.OpenBrace),
		decls,
		convertASTRune(f, e.CloseBrace),
	)
}

func convertASTEnumElement(f *ast.FileNode, el ast.EnumElement) ast2.EnumElement {
	switch el := el.(type) {
	case *ast.OptionNode:
		return convertASTOption(f, el)
	case *ast.EnumValueNode:
		return convertASTEnumValue(f, el)
	case *ast.ReservedNode:
		return convertASTReserved(f, el)
	case *ast.EmptyDeclNode:
		return convertASTEmpty(f, el)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.EnumElement: %T", el))
	}
}

func convertASTEnumValue(f *ast.FileNode, e *ast.EnumValueNode) *ast2.EnumValueNode {
	var opts *ast2.CompactOptionsNode
	if e.Options != nil {
		opts = convertASTCompactOptions(f, e.Options)
	}
	return ast2.NewEnumValueNode(
		convertASTIdentToken(f, e.Name),
		convertASTRune(f, e.Equals),
		convertASTInt(f, e.Number),
		opts,
		convertASTRune(f, e.Semicolon),
	)
}

func convertASTExtend(f *ast.FileNode, e *ast.ExtendNode) *ast2.ExtendNode {
	decls := make([]ast2.ExtendElement, len(e.Decls))
	for i := range e.Decls {
		decls[i] = convertASTExtendElement(f, e.Decls[i])
	}
	return ast2.NewExtendNode(
		convertASTKeyword(f, e.Keyword),
		convertASTIdent(f, e.Extendee),
		convertASTRune(f, e.OpenBrace),
		decls,
		convertASTRune(f, e.CloseBrace),
	)
}

func convertASTExtendElement(f *ast.FileNode, el ast.ExtendElement) ast2.ExtendElement {
	switch el := el.(type) {
	case *ast.FieldNode:
		return convertASTField(f, el)
	case *ast.GroupNode:
		return convertASTGroup(f, el)
	case *ast.EmptyDeclNode:
		return convertASTEmpty(f, el)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.ExtendElement: %T", el))
	}
}

func convertASTService(f *ast.FileNode, s *ast.ServiceNode) *ast2.ServiceNode {
	decls := make([]ast2.ServiceElement, len(s.Decls))
	for i := range s.Decls {
		decls[i] = convertASTServiceElement(f, s.Decls[i])
	}
	return ast2.NewServiceNode(
		convertASTKeyword(f, s.Keyword),
		convertASTIdentToken(f, s.Name),
		convertASTRune(f, s.OpenBrace),
		decls,
		convertASTRune(f, s.CloseBrace),
	)
}

func convertASTServiceElement(f *ast.FileNode, el ast.ServiceElement) ast2.ServiceElement {
	switch el := el.(type) {
	case *ast.OptionNode:
		return convertASTOption(f, el)
	case *ast.RPCNode:
		return convertASTMethod(f, el)
	case *ast.EmptyDeclNode:
		return convertASTEmpty(f, el)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.ServiceElement: %T", el))
	}
}

func convertASTMethod(f *ast.FileNode, m *ast.RPCNode) *ast2.RPCNode {
	if m.OpenBrace == nil {
		return ast2.NewRPCNode(
			convertASTKeyword(f, m.Keyword),
			convertASTIdentToken(f, m.Name),
			convertASTMethodType(f, m.Input),
			convertASTKeyword(f, m.Returns),
			convertASTMethodType(f, m.Output),
			convertASTRune(f, m.Semicolon),
		)
	}
	decls := make([]ast2.RPCElement, len(m.Decls))
	for i := range m.Decls {
		decls[i] = convertASTMethodElement(f, m.Decls[i])
	}
	return ast2.NewRPCNodeWithBody(
		convertASTKeyword(f, m.Keyword),
		convertASTIdentToken(f, m.Name),
		convertASTMethodType(f, m.Input),
		convertASTKeyword(f, m.Returns),
		convertASTMethodType(f, m.Output),
		convertASTRune(f, m.OpenBrace),
		decls,
		convertASTRune(f, m.CloseBrace),
	)
}

func convertASTMethodElement(f *ast.FileNode, el ast.RPCElement) ast2.RPCElement {
	switch el := el.(type) {
	case *ast.OptionNode:
		return convertASTOption(f, el)
	case *ast.EmptyDeclNode:
		return convertASTEmpty(f, el)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.RPCElement: %T", el))
	}
}

func convertASTMethodType(f *ast.FileNode, t *ast.RPCTypeNode) *ast2.RPCTypeNode {
	var stream *ast2.KeywordNode
	if t.Stream != nil {
		stream = convertASTKeyword(f, t.Stream)
	}
	return ast2.NewRPCTypeNode(
		convertASTRune(f, t.OpenParen),
		stream,
		convertASTIdent(f, t.MessageType),
		convertASTRune(f, t.CloseParen),
	)
}

func convertASTCompactOptions(f *ast.FileNode, opts *ast.CompactOptionsNode) *ast2.CompactOptionsNode {
	elems := make([]*ast2.OptionNode, len(opts.Options))
	for i := range opts.Options {
		elems[i] = convertASTOption(f, opts.Options[i])
	}
	commas := make([]*ast2.RuneNode, len(opts.Commas))
	for i := range opts.Commas {
		commas[i] = convertASTRune(f, opts.Commas[i])
	}
	return ast2.NewCompactOptionsNode(
		convertASTRune(f, opts.OpenBracket),
		elems, commas,
		convertASTRune(f, opts.CloseBracket),
	)
}

func convertASTEmpty(f *ast.FileNode, e *ast.EmptyDeclNode) *ast2.EmptyDeclNode {
	return ast2.NewEmptyDeclNode(convertASTRune(f, e.Semicolon))
}

func convertASTValue(f *ast.FileNode, v ast.ValueNode) ast2.ValueNode {
	switch v := v.(type) {
	case *ast.IdentNode:
		return convertASTIdentToken(f, v)
	case *ast.CompoundIdentNode:
		return convertASTCompoundIdent(f, v)
	case *ast.StringLiteralNode:
		return convertASTStringLiteral(f, v)
	case *ast.CompoundStringLiteralNode:
		return convertASTCompoundStringLiteral(f, v)
	case *ast.UintLiteralNode:
		return convertASTUintLiteral(f, v)
	case *ast.NegativeIntLiteralNode:
		return convertASTNegativeIntLiteral(f, v)
	case *ast.FloatLiteralNode:
		return convertASTFloatLiteral(f, v)
	case *ast.SpecialFloatLiteralNode:
		return convertASTSpecialFloatLiteral(f, v)
	case *ast.SignedFloatLiteralNode:
		return convertASTSignedFloatLiteral(f, v)
	case *ast.ArrayLiteralNode:
		return convertASTArrayLiteral(f, v)
	case *ast.MessageLiteralNode:
		return convertASTMessageLiteral(f, v)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.ValueNode: %T", v))
	}
}

func convertASTIdent(f *ast.FileNode, ident ast.IdentValueNode) ast2.IdentValueNode {
	switch ident := ident.(type) {
	case *ast.IdentNode:
		return convertASTIdentToken(f, ident)
	case *ast.CompoundIdentNode:
		return convertASTCompoundIdent(f, ident)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.IdentValueNode: %T", ident))
	}
}

func convertASTIdentToken(f *ast.FileNode, ident *ast.IdentNode) *ast2.IdentNode {
	return ast2.NewIdentNode(ident.Val, convertASTTokenInfo(f, ident.Token()))
}

func convertASTCompoundIdent(f *ast.FileNode, ident *ast.CompoundIdentNode) *ast2.CompoundIdentNode {
	var leadingDot *ast2.RuneNode
	if ident.LeadingDot != nil {
		leadingDot = convertASTRune(f, ident.LeadingDot)
	}
	components := make([]*ast2.IdentNode, len(ident.Components))
	for i := range ident.Components {
		components[i] = convertASTIdentToken(f, ident.Components[i])
	}
	dots := make([]*ast2.RuneNode, len(ident.Dots))
	for i := range ident.Dots {
		dots[i] = convertASTRune(f, ident.Dots[i])
	}
	return ast2.NewCompoundIdentNode(leadingDot, components, dots)
}

func convertASTString(f *ast.FileNode, str ast.StringValueNode) ast2.StringValueNode {
	switch str := str.(type) {
	case *ast.StringLiteralNode:
		return convertASTStringLiteral(f, str)
	case *ast.CompoundStringLiteralNode:
		return convertASTCompoundStringLiteral(f, str)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.StringValueNode: %T", str))
	}
}

func convertASTStringLiteral(f *ast.FileNode, str *ast.StringLiteralNode) *ast2.StringLiteralNode {
	return ast2.NewStringLiteralNode(str.Val, convertASTTokenInfo(f, str.Token()))
}

func convertASTCompoundStringLiteral(f *ast.FileNode, str *ast.CompoundStringLiteralNode) *ast2.CompoundStringLiteralNode {
	children := str.Children()
	components := make([]*ast2.StringLiteralNode, len(children))
	for i := range children {
		components[i] = convertASTStringLiteral(f, children[i].(*ast.StringLiteralNode))
	}
	return ast2.NewCompoundLiteralStringNode(components...)
}

func convertASTInt(f *ast.FileNode, n ast.IntValueNode) ast2.IntValueNode {
	switch n := n.(type) {
	case *ast.UintLiteralNode:
		return convertASTUintLiteral(f, n)
	case *ast.NegativeIntLiteralNode:
		return convertASTNegativeIntLiteral(f, n)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.IntValueNode: %T", n))
	}
}

func convertASTUintLiteral(f *ast.FileNode, n *ast.UintLiteralNode) *ast2.UintLiteralNode {
	return ast2.NewUintLiteralNode(n.Val, convertASTTokenInfo(f, n.Token()))
}

func convertASTNegativeIntLiteral(f *ast.FileNode, n *ast.NegativeIntLiteralNode) *ast2.NegativeIntLiteralNode {
	return ast2.NewNegativeIntLiteralNode(convertASTRune(f, n.Minus), convertASTUintLiteral(f, n.Uint))
}

func convertASTFloat(f *ast.FileNode, n ast.FloatValueNode) ast2.FloatValueNode {
	switch n := n.(type) {
	case *ast.FloatLiteralNode:
		return convertASTFloatLiteral(f, n)
	case *ast.SpecialFloatLiteralNode:
		return convertASTSpecialFloatLiteral(f, n)
	case *ast.UintLiteralNode:
		return convertASTUintLiteral(f, n)
	default:
		panic(fmt.Sprintf("unrecognized type of ast.FloatValueNode: %T", n))
	}
}

func convertASTFloatLiteral(f *ast.FileNode, n *ast.FloatLiteralNode) *ast2.FloatLiteralNode {
	return ast2.NewFloatLiteralNode(n.Val, convertASTTokenInfo(f, n.Token()))
}

func convertASTSpecialFloatLiteral(f *ast.FileNode, n *ast.SpecialFloatLiteralNode) *ast2.SpecialFloatLiteralNode {
	return ast2.NewSpecialFloatLiteralNode(convertASTKeyword(f, n.KeywordNode))
}

func convertASTSignedFloatLiteral(f *ast.FileNode, n *ast.SignedFloatLiteralNode) *ast2.SignedFloatLiteralNode {
	return ast2.NewSignedFloatLiteralNode(convertASTRune(f, n.Sign), convertASTFloat(f, n.Float))
}

func convertASTArrayLiteral(f *ast.FileNode, ar *ast.ArrayLiteralNode) *ast2.ArrayLiteralNode {
	vals := make([]ast2.ValueNode, len(ar.Elements))
	for i := range ar.Elements {
		vals[i] = convertASTValue(f, ar.Elements[i])
	}
	commas := make([]*ast2.RuneNode, len(ar.Commas))
	for i := range ar.Commas {
		commas[i] = convertASTRune(f, ar.Commas[i])
	}
	return ast2.NewArrayLiteralNode(
		convertASTRune(f, ar.OpenBracket),
		vals, commas,
		convertASTRune(f, ar.CloseBracket),
	)
}

func convertASTMessageLiteral(f *ast.FileNode, m *ast.MessageLiteralNode) *ast2.MessageLiteralNode {
	fields := make([]*ast2.MessageFieldNode, len(m.Elements))
	for i := range m.Elements {
		fields[i] = convertASTMessageLiteralField(f, m.Elements[i])
	}
	seps := make([]*ast2.RuneNode, len(m.Seps))
	for i := range m.Seps {
		if m.Seps[i] != nil {
			seps[i] = convertASTRune(f, m.Seps[i])
		}
	}
	return ast2.NewMessageLiteralNode(
		convertASTRune(f, m.Open),
		fields, seps,
		convertASTRune(f, m.Close),
	)
}

func convertASTMessageLiteralField(f *ast.FileNode, fld *ast.MessageFieldNode) *ast2.MessageFieldNode {
	var sep *ast2.RuneNode
	if fld.Sep != nil {
		sep = convertASTRune(f, fld.Sep)
	}
	return ast2.NewMessageFieldNode(
		convertASTFieldReference(f, fld.Name),
		sep,
		convertASTValue(f, fld.Val),
	)
}

func convertASTKeyword(f *ast.FileNode, k *ast.KeywordNode) *ast2.KeywordNode {
	return ast2.NewKeywordNode(k.Val, convertASTTokenInfo(f, k.Token()))
}

func convertASTRune(f *ast.FileNode, r *ast.RuneNode) *ast2.RuneNode {
	return ast2.NewRuneNode(r.Rune, convertASTTokenInfo(f, r.Token()))
}

func convertASTTokenInfo(f *ast.FileNode, tok ast.Token) ast2.TokenInfo {
	info := f.TokenInfo(tok)
	return ast2.TokenInfo{
		PosRange: ast2.PosRange{
			Start: info.Start(),
			End:   info.End(),
		},
		RawText:           info.RawText(),
		LeadingWhitespace: info.LeadingWhitespace(),
		LeadingComments:   convertASTComments(info.LeadingComments()),
		TrailingComments:  convertASTComments(info.TrailingComments()),
	}
}

func convertASTComments(comments ast.Comments) []ast2.Comment {
	results := make([]ast2.Comment, comments.Len())
	for i := 0; i < comments.Len(); i++ {
		cmt := comments.Index(i)
		results[i] = ast2.Comment{
			PosRange: ast2.PosRange{
				Start: cmt.Start(),
				End:   cmt.End(),
			},
			LeadingWhitespace: cmt.LeadingWhitespace(),
			Text:              cmt.RawText(),
		}
	}
	return results
}