github.com/jhump/protocompile@v0.0.0-20221021153901-4f6f732835e8/ast/walk.go (about) 1 package ast 2 3 import "fmt" 4 5 // Walk conducts a walk of the AST rooted at the given root using the 6 // given visitor. It performs a "pre-order traversal", visiting a 7 // given AST node before it visits that node's descendants. 8 // 9 // If a visitor returns an error while walking the tree, the entire 10 // operation is aborted and that error is returned. 11 func Walk(root Node, v Visitor, opts ...WalkOption) error { 12 var wOpts walkOptions 13 for _, opt := range opts { 14 opt(&wOpts) 15 } 16 return walk(root, v, wOpts) 17 } 18 19 // WalkOption represents an option used with the Walk function. These 20 // allow optional before and after hooks to be invoked as each node in 21 // the tree is visited. 22 type WalkOption func(*walkOptions) 23 24 type walkOptions struct { 25 before, after func(Node) error 26 } 27 28 // WithBefore returns a WalkOption that will cause the given function to be 29 // invoked before a node is visited during a walk operation. If this hook 30 // returns an error, the node is not visited and the walk operation is aborted. 31 func WithBefore(fn func(Node) error) WalkOption { 32 return func(options *walkOptions) { 33 options.before = fn 34 } 35 } 36 37 // WithAfter returns a WalkOption that will cause the given function to be 38 // invoked after a node (as well as any descendants) is visited during a walk 39 // operation. If this hook returns an error, the node is not visited and the 40 // walk operation is aborted. 41 // 42 // If the walk is aborted due to some other visitor or before hook returning an 43 // error, the after hook is still called for all nodes that have been visited. 44 // However, the walk operation fails with the first error it encountered, so any 45 // error returned from an after hook is effectively ignored. 46 func WithAfter(fn func(Node) error) WalkOption { 47 return func(options *walkOptions) { 48 options.after = fn 49 } 50 } 51 52 func walk(root Node, v Visitor, opts walkOptions) (err error) { 53 if opts.before != nil { 54 if err := opts.before(root); err != nil { 55 return err 56 } 57 } 58 if opts.after != nil { 59 defer func() { 60 if afterErr := opts.after(root); afterErr != nil { 61 // if another call already returned an error then we 62 // have to ignore the error from the after hook 63 if err == nil { 64 err = afterErr 65 } 66 } 67 }() 68 } 69 70 if err := Visit(root, v); err != nil { 71 return err 72 } 73 74 if comp, ok := root.(CompositeNode); ok { 75 for _, child := range comp.Children() { 76 if err := walk(child, v, opts); err != nil { 77 return err 78 } 79 } 80 } 81 return nil 82 } 83 84 // Visit implements the double-dispatch idiom and visits the given node by 85 // calling the appropriate method of the given visitor. 86 func Visit(n Node, v Visitor) error { 87 switch n := n.(type) { 88 case *FileNode: 89 return v.VisitFileNode(n) 90 case *SyntaxNode: 91 return v.VisitSyntaxNode(n) 92 case *PackageNode: 93 return v.VisitPackageNode(n) 94 case *ImportNode: 95 return v.VisitImportNode(n) 96 case *OptionNode: 97 return v.VisitOptionNode(n) 98 case *OptionNameNode: 99 return v.VisitOptionNameNode(n) 100 case *FieldReferenceNode: 101 return v.VisitFieldReferenceNode(n) 102 case *CompactOptionsNode: 103 return v.VisitCompactOptionsNode(n) 104 case *MessageNode: 105 return v.VisitMessageNode(n) 106 case *ExtendNode: 107 return v.VisitExtendNode(n) 108 case *ExtensionRangeNode: 109 return v.VisitExtensionRangeNode(n) 110 case *ReservedNode: 111 return v.VisitReservedNode(n) 112 case *RangeNode: 113 return v.VisitRangeNode(n) 114 case *FieldNode: 115 return v.VisitFieldNode(n) 116 case *GroupNode: 117 return v.VisitGroupNode(n) 118 case *MapFieldNode: 119 return v.VisitMapFieldNode(n) 120 case *MapTypeNode: 121 return v.VisitMapTypeNode(n) 122 case *OneOfNode: 123 return v.VisitOneOfNode(n) 124 case *EnumNode: 125 return v.VisitEnumNode(n) 126 case *EnumValueNode: 127 return v.VisitEnumValueNode(n) 128 case *ServiceNode: 129 return v.VisitServiceNode(n) 130 case *RPCNode: 131 return v.VisitRPCNode(n) 132 case *RPCTypeNode: 133 return v.VisitRPCTypeNode(n) 134 case *IdentNode: 135 return v.VisitIdentNode(n) 136 case *CompoundIdentNode: 137 return v.VisitCompoundIdentNode(n) 138 case *StringLiteralNode: 139 return v.VisitStringLiteralNode(n) 140 case *CompoundStringLiteralNode: 141 return v.VisitCompoundStringLiteralNode(n) 142 case *UintLiteralNode: 143 return v.VisitUintLiteralNode(n) 144 case *PositiveUintLiteralNode: 145 return v.VisitPositiveUintLiteralNode(n) 146 case *NegativeIntLiteralNode: 147 return v.VisitNegativeIntLiteralNode(n) 148 case *FloatLiteralNode: 149 return v.VisitFloatLiteralNode(n) 150 case *SpecialFloatLiteralNode: 151 return v.VisitSpecialFloatLiteralNode(n) 152 case *SignedFloatLiteralNode: 153 return v.VisitSignedFloatLiteralNode(n) 154 case *BoolLiteralNode: 155 return v.VisitBoolLiteralNode(n) 156 case *ArrayLiteralNode: 157 return v.VisitArrayLiteralNode(n) 158 case *MessageLiteralNode: 159 return v.VisitMessageLiteralNode(n) 160 case *MessageFieldNode: 161 return v.VisitMessageFieldNode(n) 162 case *KeywordNode: 163 return v.VisitKeywordNode(n) 164 case *RuneNode: 165 return v.VisitRuneNode(n) 166 case *EmptyDeclNode: 167 return v.VisitEmptyDeclNode(n) 168 default: 169 panic(fmt.Sprintf("unexpected type of node: %T", n)) 170 } 171 } 172 173 // AncestorTracker is used to track the path of nodes during a walk operation. 174 // By passing AsWalkOptions to a call to Walk, a visitor can inspect the path to 175 // the node being visited using this tracker. 176 type AncestorTracker struct { 177 ancestors []Node 178 } 179 180 // AsWalkOptions returns WalkOption values that will cause this ancestor tracker 181 // to track the path through the AST during the walk operation. 182 func (t *AncestorTracker) AsWalkOptions() []WalkOption { 183 return []WalkOption{ 184 WithBefore(func(n Node) error { 185 t.ancestors = append(t.ancestors, n) 186 return nil 187 }), 188 WithAfter(func(n Node) error { 189 t.ancestors = t.ancestors[:len(t.ancestors)-1] 190 return nil 191 }), 192 } 193 } 194 195 // Path returns a slice of nodes that represents the path from the root of the 196 // walk operaiton to the currently visited node. The first element in the path 197 // is the root supplied to Walk. The last element in the path is the currently 198 // visited node. 199 // 200 // The returned slice is not a defensive copy; so callers should NOT mutate it. 201 func (t *AncestorTracker) Path() []Node { 202 return t.ancestors 203 } 204 205 // Parent returns the parent node of the currently visited node. If the node 206 // currently being visited is the root supplied to Walk then nil is returned. 207 func (t *AncestorTracker) Parent() Node { 208 if len(t.ancestors) <= 1 { 209 return nil 210 } 211 return t.ancestors[len(t.ancestors)-2] 212 } 213 214 // VisitChildren visits all direct children of the given node using the given 215 // visitor. If visiting a child returns an error, that error is immediately 216 // returned, and other children will not be visited. 217 func VisitChildren(n CompositeNode, v Visitor) error { 218 for _, ch := range n.Children() { 219 if err := Visit(ch, v); err != nil { 220 return err 221 } 222 } 223 return nil 224 } 225 226 // Visitor provides a technique for walking the AST that allows for 227 // dynamic dispatch, where a particular function is invoked based on 228 // the runtime type of the argument. 229 // 230 // It consists of a number of functions, each of which matches a 231 // concrete Node type. It also includes functions for sub-interfaces 232 // of Node and the Node interface itself, to be used as broader 233 // "catch all" functions. 234 // 235 // To use a visitor, provide a function for the node types of 236 // interest and pass visitor.Visit as the function to a Walk operation. 237 // When a node is traversed, the corresponding function field of 238 // the visitor is invoked, if not nil. If the function for a node's 239 // concrete type is nil/absent but the function for an interface it 240 // implements is present, that interface visit function will be used 241 // instead. If no matching function is present, the traversal will 242 // continue. If a matching function is present, it will be invoked 243 // and its response determines how the traversal proceeds. 244 // 245 // Every visit function returns (bool, *Visitor). If the bool returned 246 // is false, the visited node's descendants are skipped. Otherwise, 247 // traversal will continue into the node's children. If the returned 248 // visitor is nil, the current visitor will continue to be used. But 249 // if a non-nil visitor is returned, it will be used to visit the 250 // node's children. 251 type Visitor interface { 252 // VisitFileNode is invoked when visiting a *FileNode in the AST. 253 VisitFileNode(*FileNode) error 254 // VisitSyntaxNode is invoked when visiting a *SyntaxNode in the AST. 255 VisitSyntaxNode(*SyntaxNode) error 256 // VisitPackageNode is invoked when visiting a *PackageNode in the AST. 257 VisitPackageNode(*PackageNode) error 258 // VisitImportNode is invoked when visiting an *ImportNode in the AST. 259 VisitImportNode(*ImportNode) error 260 // VisitOptionNode is invoked when visiting an *OptionNode in the AST. 261 VisitOptionNode(*OptionNode) error 262 // VisitOptionNameNode is invoked when visiting an *OptionNameNode in the AST. 263 VisitOptionNameNode(*OptionNameNode) error 264 // VisitFieldReferenceNode is invoked when visiting a *FieldReferenceNode in the AST. 265 VisitFieldReferenceNode(*FieldReferenceNode) error 266 // VisitCompactOptionsNode is invoked when visiting a *CompactOptionsNode in the AST. 267 VisitCompactOptionsNode(*CompactOptionsNode) error 268 // VisitMessageNode is invoked when visiting a *MessageNode in the AST. 269 VisitMessageNode(*MessageNode) error 270 // VisitExtendNode is invoked when visiting an *ExtendNode in the AST. 271 VisitExtendNode(*ExtendNode) error 272 // VisitExtensionRangeNode is invoked when visiting an *ExtensionRangeNode in the AST. 273 VisitExtensionRangeNode(*ExtensionRangeNode) error 274 // VisitReservedNode is invoked when visiting a *ReservedNode in the AST. 275 VisitReservedNode(*ReservedNode) error 276 // VisitRangeNode is invoked when visiting a *RangeNode in the AST. 277 VisitRangeNode(*RangeNode) error 278 // VisitFieldNode is invoked when visiting a *FieldNode in the AST. 279 VisitFieldNode(*FieldNode) error 280 // VisitGroupNode is invoked when visiting a *GroupNode in the AST. 281 VisitGroupNode(*GroupNode) error 282 // VisitMapFieldNode is invoked when visiting a *MapFieldNode in the AST. 283 VisitMapFieldNode(*MapFieldNode) error 284 // VisitMapTypeNode is invoked when visiting a *MapTypeNode in the AST. 285 VisitMapTypeNode(*MapTypeNode) error 286 // VisitOneOfNode is invoked when visiting a *OneOfNode in the AST. 287 VisitOneOfNode(*OneOfNode) error 288 // VisitEnumNode is invoked when visiting an *EnumNode in the AST. 289 VisitEnumNode(*EnumNode) error 290 // VisitEnumValueNode is invoked when visiting an *EnumValueNode in the AST. 291 VisitEnumValueNode(*EnumValueNode) error 292 // VisitServiceNode is invoked when visiting a *ServiceNode in the AST. 293 VisitServiceNode(*ServiceNode) error 294 // VisitRPCNode is invoked when visiting an *RPCNode in the AST. 295 VisitRPCNode(*RPCNode) error 296 // VisitRPCTypeNode is invoked when visiting an *RPCTypeNode in the AST. 297 VisitRPCTypeNode(*RPCTypeNode) error 298 // VisitIdentNode is invoked when visiting an *IdentNode in the AST. 299 VisitIdentNode(*IdentNode) error 300 // VisitCompoundIdentNode is invoked when visiting a *CompoundIdentNode in the AST. 301 VisitCompoundIdentNode(*CompoundIdentNode) error 302 // VisitStringLiteralNode is invoked when visiting a *StringLiteralNode in the AST. 303 VisitStringLiteralNode(*StringLiteralNode) error 304 // VisitCompoundStringLiteralNode is invoked when visiting a *CompoundStringLiteralNode in the AST. 305 VisitCompoundStringLiteralNode(*CompoundStringLiteralNode) error 306 // VisitUintLiteralNode is invoked when visiting a *UintLiteralNode in the AST. 307 VisitUintLiteralNode(*UintLiteralNode) error 308 // VisitPositiveUintLiteralNode is invoked when visiting a *PositiveUintLiteralNode in the AST. 309 VisitPositiveUintLiteralNode(*PositiveUintLiteralNode) error 310 // VisitNegativeIntLiteralNode is invoked when visiting a *NegativeIntLiteralNode in the AST. 311 VisitNegativeIntLiteralNode(*NegativeIntLiteralNode) error 312 // VisitFloatLiteralNode is invoked when visiting a *FloatLiteralNode in the AST. 313 VisitFloatLiteralNode(*FloatLiteralNode) error 314 // VisitSpecialFloatLiteralNode is invoked when visiting a *SpecialFloatLiteralNode in the AST. 315 VisitSpecialFloatLiteralNode(*SpecialFloatLiteralNode) error 316 // VisitSignedFloatLiteralNode is invoked when visiting a *SignedFloatLiteralNode in the AST. 317 VisitSignedFloatLiteralNode(*SignedFloatLiteralNode) error 318 // VisitBoolLiteralNode is invoked when visiting a *BoolLiteralNode in the AST. 319 VisitBoolLiteralNode(*BoolLiteralNode) error 320 // VisitArrayLiteralNode is invoked when visiting an *ArrayLiteralNode in the AST. 321 VisitArrayLiteralNode(*ArrayLiteralNode) error 322 // VisitMessageLiteralNode is invoked when visiting a *MessageLiteralNode in the AST. 323 VisitMessageLiteralNode(*MessageLiteralNode) error 324 // VisitMessageFieldNode is invoked when visiting a *MessageFieldNode in the AST. 325 VisitMessageFieldNode(*MessageFieldNode) error 326 // VisitKeywordNode is invoked when visiting a *KeywordNode in the AST. 327 VisitKeywordNode(*KeywordNode) error 328 // VisitRuneNode is invoked when visiting a *RuneNode in the AST. 329 VisitRuneNode(*RuneNode) error 330 // VisitEmptyDeclNode is invoked when visiting a *EmptyDeclNode in the AST. 331 VisitEmptyDeclNode(*EmptyDeclNode) error 332 } 333 334 // NoOpVisitor is a visitor implementation that does nothing. All methods 335 // unconditionally return nil. This can be embedded into a struct to make that 336 // struct implement the Visitor interface, and only the relevant visit methods 337 // then need to be implemented on the struct. 338 type NoOpVisitor struct{} 339 340 var _ Visitor = NoOpVisitor{} 341 342 func (n NoOpVisitor) VisitFileNode(_ *FileNode) error { 343 return nil 344 } 345 346 func (n NoOpVisitor) VisitSyntaxNode(_ *SyntaxNode) error { 347 return nil 348 } 349 350 func (n NoOpVisitor) VisitPackageNode(_ *PackageNode) error { 351 return nil 352 } 353 354 func (n NoOpVisitor) VisitImportNode(_ *ImportNode) error { 355 return nil 356 } 357 358 func (n NoOpVisitor) VisitOptionNode(_ *OptionNode) error { 359 return nil 360 } 361 362 func (n NoOpVisitor) VisitOptionNameNode(_ *OptionNameNode) error { 363 return nil 364 } 365 366 func (n NoOpVisitor) VisitFieldReferenceNode(_ *FieldReferenceNode) error { 367 return nil 368 } 369 370 func (n NoOpVisitor) VisitCompactOptionsNode(_ *CompactOptionsNode) error { 371 return nil 372 } 373 374 func (n NoOpVisitor) VisitMessageNode(_ *MessageNode) error { 375 return nil 376 } 377 378 func (n NoOpVisitor) VisitExtendNode(_ *ExtendNode) error { 379 return nil 380 } 381 382 func (n NoOpVisitor) VisitExtensionRangeNode(_ *ExtensionRangeNode) error { 383 return nil 384 } 385 386 func (n NoOpVisitor) VisitReservedNode(_ *ReservedNode) error { 387 return nil 388 } 389 390 func (n NoOpVisitor) VisitRangeNode(_ *RangeNode) error { 391 return nil 392 } 393 394 func (n NoOpVisitor) VisitFieldNode(_ *FieldNode) error { 395 return nil 396 } 397 398 func (n NoOpVisitor) VisitGroupNode(_ *GroupNode) error { 399 return nil 400 } 401 402 func (n NoOpVisitor) VisitMapFieldNode(_ *MapFieldNode) error { 403 return nil 404 } 405 406 func (n NoOpVisitor) VisitMapTypeNode(_ *MapTypeNode) error { 407 return nil 408 } 409 410 func (n NoOpVisitor) VisitOneOfNode(_ *OneOfNode) error { 411 return nil 412 } 413 414 func (n NoOpVisitor) VisitEnumNode(_ *EnumNode) error { 415 return nil 416 } 417 418 func (n NoOpVisitor) VisitEnumValueNode(_ *EnumValueNode) error { 419 return nil 420 } 421 422 func (n NoOpVisitor) VisitServiceNode(_ *ServiceNode) error { 423 return nil 424 } 425 426 func (n NoOpVisitor) VisitRPCNode(_ *RPCNode) error { 427 return nil 428 } 429 430 func (n NoOpVisitor) VisitRPCTypeNode(_ *RPCTypeNode) error { 431 return nil 432 } 433 434 func (n NoOpVisitor) VisitIdentNode(_ *IdentNode) error { 435 return nil 436 } 437 438 func (n NoOpVisitor) VisitCompoundIdentNode(_ *CompoundIdentNode) error { 439 return nil 440 } 441 442 func (n NoOpVisitor) VisitStringLiteralNode(_ *StringLiteralNode) error { 443 return nil 444 } 445 446 func (n NoOpVisitor) VisitCompoundStringLiteralNode(_ *CompoundStringLiteralNode) error { 447 return nil 448 } 449 450 func (n NoOpVisitor) VisitUintLiteralNode(_ *UintLiteralNode) error { 451 return nil 452 } 453 454 func (n NoOpVisitor) VisitPositiveUintLiteralNode(_ *PositiveUintLiteralNode) error { 455 return nil 456 } 457 458 func (n NoOpVisitor) VisitNegativeIntLiteralNode(_ *NegativeIntLiteralNode) error { 459 return nil 460 } 461 462 func (n NoOpVisitor) VisitFloatLiteralNode(_ *FloatLiteralNode) error { 463 return nil 464 } 465 466 func (n NoOpVisitor) VisitSpecialFloatLiteralNode(_ *SpecialFloatLiteralNode) error { 467 return nil 468 } 469 470 func (n NoOpVisitor) VisitSignedFloatLiteralNode(_ *SignedFloatLiteralNode) error { 471 return nil 472 } 473 474 func (n NoOpVisitor) VisitBoolLiteralNode(_ *BoolLiteralNode) error { 475 return nil 476 } 477 478 func (n NoOpVisitor) VisitArrayLiteralNode(_ *ArrayLiteralNode) error { 479 return nil 480 } 481 482 func (n NoOpVisitor) VisitMessageLiteralNode(_ *MessageLiteralNode) error { 483 return nil 484 } 485 486 func (n NoOpVisitor) VisitMessageFieldNode(_ *MessageFieldNode) error { 487 return nil 488 } 489 490 func (n NoOpVisitor) VisitKeywordNode(_ *KeywordNode) error { 491 return nil 492 } 493 494 func (n NoOpVisitor) VisitRuneNode(_ *RuneNode) error { 495 return nil 496 } 497 498 func (n NoOpVisitor) VisitEmptyDeclNode(_ *EmptyDeclNode) error { 499 return nil 500 } 501 502 // SimpleVisitor is a visitor implementation that uses numerous function fields. 503 // If a relevant function field is not nil, then it will be invoked when a node 504 // is visited. 505 // 506 // In addition to a function for each concrete node type (and thus for each 507 // Visit* method of the Visitor interface), it also has function fields that 508 // accept interface types. So a visitor can, for example, easily treat all 509 // ValueNodes uniformly by providing a non-nil value for DoVisitValueNode 510 // instead of having to supply values for the various Do*Node methods 511 // corresponding to all types that implement ValueNode. 512 // 513 // The most specific function provided that matches a given node is the one that 514 // will be invoked. For example, DoVisitStringValueNode will be called if 515 // present and applicable before DoVisitValueNode. Similarly, DoVisitValueNode 516 // would be called before DoVisitTerminalNode or DoVisitCompositeNode. The 517 // DoVisitNode is the most generic function and is called only if no more 518 // specific function is present for a given node type. 519 // 520 // The *UintLiteralNode type implements both IntValueNode and FloatValueNode. 521 // In this case, the DoVisitIntValueNode function is considered more specific 522 // than DoVisitFloatValueNode, so will be preferred if present. 523 // 524 // Similarly, *MapFieldNode implements both FieldDeclNode and MessageDeclNode. 525 // In this case, the DoVisitFieldDeclNode function is considered more specific 526 // than DoVisitMessageDeclNode, so will be preferred if present. 527 type SimpleVisitor struct { 528 DoVisitFileNode func(*FileNode) error 529 DoVisitSyntaxNode func(*SyntaxNode) error 530 DoVisitPackageNode func(*PackageNode) error 531 DoVisitImportNode func(*ImportNode) error 532 DoVisitOptionNode func(*OptionNode) error 533 DoVisitOptionNameNode func(*OptionNameNode) error 534 DoVisitFieldReferenceNode func(*FieldReferenceNode) error 535 DoVisitCompactOptionsNode func(*CompactOptionsNode) error 536 DoVisitMessageNode func(*MessageNode) error 537 DoVisitExtendNode func(*ExtendNode) error 538 DoVisitExtensionRangeNode func(*ExtensionRangeNode) error 539 DoVisitReservedNode func(*ReservedNode) error 540 DoVisitRangeNode func(*RangeNode) error 541 DoVisitFieldNode func(*FieldNode) error 542 DoVisitGroupNode func(*GroupNode) error 543 DoVisitMapFieldNode func(*MapFieldNode) error 544 DoVisitMapTypeNode func(*MapTypeNode) error 545 DoVisitOneOfNode func(*OneOfNode) error 546 DoVisitEnumNode func(*EnumNode) error 547 DoVisitEnumValueNode func(*EnumValueNode) error 548 DoVisitServiceNode func(*ServiceNode) error 549 DoVisitRPCNode func(*RPCNode) error 550 DoVisitRPCTypeNode func(*RPCTypeNode) error 551 DoVisitIdentNode func(*IdentNode) error 552 DoVisitCompoundIdentNode func(*CompoundIdentNode) error 553 DoVisitStringLiteralNode func(*StringLiteralNode) error 554 DoVisitCompoundStringLiteralNode func(*CompoundStringLiteralNode) error 555 DoVisitUintLiteralNode func(*UintLiteralNode) error 556 DoVisitPositiveUintLiteralNode func(*PositiveUintLiteralNode) error 557 DoVisitNegativeIntLiteralNode func(*NegativeIntLiteralNode) error 558 DoVisitFloatLiteralNode func(*FloatLiteralNode) error 559 DoVisitSpecialFloatLiteralNode func(*SpecialFloatLiteralNode) error 560 DoVisitSignedFloatLiteralNode func(*SignedFloatLiteralNode) error 561 DoVisitBoolLiteralNode func(*BoolLiteralNode) error 562 DoVisitArrayLiteralNode func(*ArrayLiteralNode) error 563 DoVisitMessageLiteralNode func(*MessageLiteralNode) error 564 DoVisitMessageFieldNode func(*MessageFieldNode) error 565 DoVisitKeywordNode func(*KeywordNode) error 566 DoVisitRuneNode func(*RuneNode) error 567 DoVisitEmptyDeclNode func(*EmptyDeclNode) error 568 569 DoVisitFieldDeclNode func(FieldDeclNode) error 570 DoVisitMessageDeclNode func(MessageDeclNode) error 571 572 DoVisitIdentValueNode func(IdentValueNode) error 573 DoVisitStringValueNode func(StringValueNode) error 574 DoVisitIntValueNode func(IntValueNode) error 575 DoVisitFloatValueNode func(FloatValueNode) error 576 DoVisitValueNode func(ValueNode) error 577 578 DoVisitTerminalNode func(TerminalNode) error 579 DoVisitCompositeNode func(CompositeNode) error 580 DoVisitNode func(Node) error 581 } 582 583 var _ Visitor = (*SimpleVisitor)(nil) 584 585 func (b *SimpleVisitor) visitInterface(node Node) error { 586 switch n := node.(type) { 587 case FieldDeclNode: 588 if b.DoVisitFieldDeclNode != nil { 589 return b.DoVisitFieldDeclNode(n) 590 } 591 // *MapFieldNode implements both FieldDeclNode and MessageDeclNode, 592 // so handle other case here 593 if fn, ok := n.(MessageDeclNode); ok && b.DoVisitMessageDeclNode != nil { 594 return b.DoVisitMessageDeclNode(fn) 595 } 596 case MessageDeclNode: 597 if b.DoVisitMessageDeclNode != nil { 598 return b.DoVisitMessageDeclNode(n) 599 } 600 case IdentValueNode: 601 if b.DoVisitIdentValueNode != nil { 602 return b.DoVisitIdentValueNode(n) 603 } 604 case StringValueNode: 605 if b.DoVisitStringValueNode != nil { 606 return b.DoVisitStringValueNode(n) 607 } 608 case IntValueNode: 609 if b.DoVisitIntValueNode != nil { 610 return b.DoVisitIntValueNode(n) 611 } 612 // *UintLiteralNode implements both IntValueNode and FloatValueNode, 613 // so handle other case here 614 if fn, ok := n.(FloatValueNode); ok && b.DoVisitFloatValueNode != nil { 615 return b.DoVisitFloatValueNode(fn) 616 } 617 case FloatValueNode: 618 if b.DoVisitFloatValueNode != nil { 619 return b.DoVisitFloatValueNode(n) 620 } 621 } 622 623 if n, ok := node.(ValueNode); ok && b.DoVisitValueNode != nil { 624 return b.DoVisitValueNode(n) 625 } 626 627 switch n := node.(type) { 628 case TerminalNode: 629 if b.DoVisitTerminalNode != nil { 630 return b.DoVisitTerminalNode(n) 631 } 632 case CompositeNode: 633 if b.DoVisitCompositeNode != nil { 634 return b.DoVisitCompositeNode(n) 635 } 636 } 637 638 if b.DoVisitNode != nil { 639 return b.DoVisitNode(node) 640 } 641 642 return nil 643 } 644 645 func (b *SimpleVisitor) VisitFileNode(node *FileNode) error { 646 if b.DoVisitFileNode != nil { 647 return b.DoVisitFileNode(node) 648 } 649 return b.visitInterface(node) 650 } 651 652 func (b *SimpleVisitor) VisitSyntaxNode(node *SyntaxNode) error { 653 if b.DoVisitSyntaxNode != nil { 654 return b.DoVisitSyntaxNode(node) 655 } 656 return b.visitInterface(node) 657 } 658 659 func (b *SimpleVisitor) VisitPackageNode(node *PackageNode) error { 660 if b.DoVisitPackageNode != nil { 661 return b.DoVisitPackageNode(node) 662 } 663 return b.visitInterface(node) 664 } 665 666 func (b *SimpleVisitor) VisitImportNode(node *ImportNode) error { 667 if b.DoVisitImportNode != nil { 668 return b.DoVisitImportNode(node) 669 } 670 return b.visitInterface(node) 671 } 672 673 func (b *SimpleVisitor) VisitOptionNode(node *OptionNode) error { 674 if b.DoVisitOptionNode != nil { 675 return b.DoVisitOptionNode(node) 676 } 677 return b.visitInterface(node) 678 } 679 680 func (b *SimpleVisitor) VisitOptionNameNode(node *OptionNameNode) error { 681 if b.DoVisitOptionNameNode != nil { 682 return b.DoVisitOptionNameNode(node) 683 } 684 return b.visitInterface(node) 685 } 686 687 func (b *SimpleVisitor) VisitFieldReferenceNode(node *FieldReferenceNode) error { 688 if b.DoVisitFieldReferenceNode != nil { 689 return b.DoVisitFieldReferenceNode(node) 690 } 691 return b.visitInterface(node) 692 } 693 694 func (b *SimpleVisitor) VisitCompactOptionsNode(node *CompactOptionsNode) error { 695 if b.DoVisitCompactOptionsNode != nil { 696 return b.DoVisitCompactOptionsNode(node) 697 } 698 return b.visitInterface(node) 699 } 700 701 func (b *SimpleVisitor) VisitMessageNode(node *MessageNode) error { 702 if b.DoVisitMessageNode != nil { 703 return b.DoVisitMessageNode(node) 704 } 705 return b.visitInterface(node) 706 } 707 708 func (b *SimpleVisitor) VisitExtendNode(node *ExtendNode) error { 709 if b.DoVisitExtendNode != nil { 710 return b.DoVisitExtendNode(node) 711 } 712 return b.visitInterface(node) 713 } 714 715 func (b *SimpleVisitor) VisitExtensionRangeNode(node *ExtensionRangeNode) error { 716 if b.DoVisitExtensionRangeNode != nil { 717 return b.DoVisitExtensionRangeNode(node) 718 } 719 return b.visitInterface(node) 720 } 721 722 func (b *SimpleVisitor) VisitReservedNode(node *ReservedNode) error { 723 if b.DoVisitReservedNode != nil { 724 return b.DoVisitReservedNode(node) 725 } 726 return b.visitInterface(node) 727 } 728 729 func (b *SimpleVisitor) VisitRangeNode(node *RangeNode) error { 730 if b.DoVisitRangeNode != nil { 731 return b.DoVisitRangeNode(node) 732 } 733 return b.visitInterface(node) 734 } 735 736 func (b *SimpleVisitor) VisitFieldNode(node *FieldNode) error { 737 if b.DoVisitFieldNode != nil { 738 return b.DoVisitFieldNode(node) 739 } 740 return b.visitInterface(node) 741 } 742 743 func (b *SimpleVisitor) VisitGroupNode(node *GroupNode) error { 744 if b.DoVisitGroupNode != nil { 745 return b.DoVisitGroupNode(node) 746 } 747 return b.visitInterface(node) 748 } 749 750 func (b *SimpleVisitor) VisitMapFieldNode(node *MapFieldNode) error { 751 if b.DoVisitMapFieldNode != nil { 752 return b.DoVisitMapFieldNode(node) 753 } 754 return b.visitInterface(node) 755 } 756 757 func (b *SimpleVisitor) VisitMapTypeNode(node *MapTypeNode) error { 758 if b.DoVisitMapTypeNode != nil { 759 return b.DoVisitMapTypeNode(node) 760 } 761 return b.visitInterface(node) 762 } 763 764 func (b *SimpleVisitor) VisitOneOfNode(node *OneOfNode) error { 765 if b.DoVisitOneOfNode != nil { 766 return b.DoVisitOneOfNode(node) 767 } 768 return b.visitInterface(node) 769 } 770 771 func (b *SimpleVisitor) VisitEnumNode(node *EnumNode) error { 772 if b.DoVisitEnumNode != nil { 773 return b.DoVisitEnumNode(node) 774 } 775 return b.visitInterface(node) 776 } 777 778 func (b *SimpleVisitor) VisitEnumValueNode(node *EnumValueNode) error { 779 if b.DoVisitEnumValueNode != nil { 780 return b.DoVisitEnumValueNode(node) 781 } 782 return b.visitInterface(node) 783 } 784 785 func (b *SimpleVisitor) VisitServiceNode(node *ServiceNode) error { 786 if b.DoVisitServiceNode != nil { 787 return b.DoVisitServiceNode(node) 788 } 789 return b.visitInterface(node) 790 } 791 792 func (b *SimpleVisitor) VisitRPCNode(node *RPCNode) error { 793 if b.DoVisitRPCNode != nil { 794 return b.DoVisitRPCNode(node) 795 } 796 return b.visitInterface(node) 797 } 798 799 func (b *SimpleVisitor) VisitRPCTypeNode(node *RPCTypeNode) error { 800 if b.DoVisitRPCTypeNode != nil { 801 return b.DoVisitRPCTypeNode(node) 802 } 803 return b.visitInterface(node) 804 } 805 806 func (b *SimpleVisitor) VisitIdentNode(node *IdentNode) error { 807 if b.DoVisitIdentNode != nil { 808 return b.DoVisitIdentNode(node) 809 } 810 return b.visitInterface(node) 811 } 812 813 func (b *SimpleVisitor) VisitCompoundIdentNode(node *CompoundIdentNode) error { 814 if b.DoVisitCompoundIdentNode != nil { 815 return b.DoVisitCompoundIdentNode(node) 816 } 817 return b.visitInterface(node) 818 } 819 820 func (b *SimpleVisitor) VisitStringLiteralNode(node *StringLiteralNode) error { 821 if b.DoVisitStringLiteralNode != nil { 822 return b.DoVisitStringLiteralNode(node) 823 } 824 return b.visitInterface(node) 825 } 826 827 func (b *SimpleVisitor) VisitCompoundStringLiteralNode(node *CompoundStringLiteralNode) error { 828 if b.DoVisitCompoundStringLiteralNode != nil { 829 return b.DoVisitCompoundStringLiteralNode(node) 830 } 831 return b.visitInterface(node) 832 } 833 834 func (b *SimpleVisitor) VisitUintLiteralNode(node *UintLiteralNode) error { 835 if b.DoVisitUintLiteralNode != nil { 836 return b.DoVisitUintLiteralNode(node) 837 } 838 return b.visitInterface(node) 839 } 840 841 func (b *SimpleVisitor) VisitPositiveUintLiteralNode(node *PositiveUintLiteralNode) error { 842 if b.DoVisitPositiveUintLiteralNode != nil { 843 return b.DoVisitPositiveUintLiteralNode(node) 844 } 845 return b.visitInterface(node) 846 } 847 848 func (b *SimpleVisitor) VisitNegativeIntLiteralNode(node *NegativeIntLiteralNode) error { 849 if b.DoVisitNegativeIntLiteralNode != nil { 850 return b.DoVisitNegativeIntLiteralNode(node) 851 } 852 return b.visitInterface(node) 853 } 854 855 func (b *SimpleVisitor) VisitFloatLiteralNode(node *FloatLiteralNode) error { 856 if b.DoVisitFloatLiteralNode != nil { 857 return b.DoVisitFloatLiteralNode(node) 858 } 859 return b.visitInterface(node) 860 } 861 862 func (b *SimpleVisitor) VisitSpecialFloatLiteralNode(node *SpecialFloatLiteralNode) error { 863 if b.DoVisitSpecialFloatLiteralNode != nil { 864 return b.DoVisitSpecialFloatLiteralNode(node) 865 } 866 return b.visitInterface(node) 867 } 868 869 func (b *SimpleVisitor) VisitSignedFloatLiteralNode(node *SignedFloatLiteralNode) error { 870 if b.DoVisitSignedFloatLiteralNode != nil { 871 return b.DoVisitSignedFloatLiteralNode(node) 872 } 873 return b.visitInterface(node) 874 } 875 876 func (b *SimpleVisitor) VisitBoolLiteralNode(node *BoolLiteralNode) error { 877 if b.DoVisitBoolLiteralNode != nil { 878 return b.DoVisitBoolLiteralNode(node) 879 } 880 return b.visitInterface(node) 881 } 882 883 func (b *SimpleVisitor) VisitArrayLiteralNode(node *ArrayLiteralNode) error { 884 if b.DoVisitArrayLiteralNode != nil { 885 return b.DoVisitArrayLiteralNode(node) 886 } 887 return b.visitInterface(node) 888 } 889 890 func (b *SimpleVisitor) VisitMessageLiteralNode(node *MessageLiteralNode) error { 891 if b.DoVisitMessageLiteralNode != nil { 892 return b.DoVisitMessageLiteralNode(node) 893 } 894 return b.visitInterface(node) 895 } 896 897 func (b *SimpleVisitor) VisitMessageFieldNode(node *MessageFieldNode) error { 898 if b.DoVisitMessageFieldNode != nil { 899 return b.DoVisitMessageFieldNode(node) 900 } 901 return b.visitInterface(node) 902 } 903 904 func (b *SimpleVisitor) VisitKeywordNode(node *KeywordNode) error { 905 if b.DoVisitKeywordNode != nil { 906 return b.DoVisitKeywordNode(node) 907 } 908 return b.visitInterface(node) 909 } 910 911 func (b *SimpleVisitor) VisitRuneNode(node *RuneNode) error { 912 if b.DoVisitRuneNode != nil { 913 return b.DoVisitRuneNode(node) 914 } 915 return b.visitInterface(node) 916 } 917 918 func (b *SimpleVisitor) VisitEmptyDeclNode(node *EmptyDeclNode) error { 919 if b.DoVisitEmptyDeclNode != nil { 920 return b.DoVisitEmptyDeclNode(node) 921 } 922 return b.visitInterface(node) 923 }