| 1 | //Copyright 2013 Vastech SA (PTY) LTD |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package ast |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | ) |
| 20 | |
| 21 | func walkLexNode(node LexNode, visitor LexNodeVisitor) LexNodeVisitor { |
| 22 | switch n := node.(type) { |
| 23 | case *LexAlt: |
| 24 | return n.Walk(visitor) |
| 25 | case *LexCharLit: |
| 26 | return n.Walk(visitor) |
| 27 | case *LexCharRange: |
| 28 | return n.Walk(visitor) |
| 29 | case *LexDot: |
| 30 | return n.Walk(visitor) |
| 31 | case *LexGroupPattern: |
| 32 | return n.Walk(visitor) |
| 33 | case *LexIgnoredTokDef: |
| 34 | return n.Walk(visitor) |
| 35 | case *LexImports: |
| 36 | return n.Walk(visitor) |
| 37 | case *LexOptPattern: |
| 38 | return n.Walk(visitor) |
| 39 | case *LexPattern: |
| 40 | return n.Walk(visitor) |
| 41 | case *LexProductions: |
| 42 | return n.Walk(visitor) |
| 43 | case *LexRegDef: |
| 44 | return n.Walk(visitor) |
| 45 | case *LexRegDefId: |
| 46 | return n.Walk(visitor) |
| 47 | case *LexRepPattern: |
| 48 | return n.Walk(visitor) |
| 49 | case *LexTokDef: |
| 50 | return n.Walk(visitor) |
| 51 | } |
| 52 | panic(fmt.Sprintf("Unsupported LexNode type %T", node)) |
| 53 | } |
| 54 | |
| 55 | func (this *LexAlt) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 56 | for _, term := range this.Terms { |
| 57 | if v := walkLexNode(term, visitor); v == nil { |
| 58 | return nil |
| 59 | } |
| 60 | } |
| 61 | return visitor.Visit(this) |
| 62 | } |
| 63 | |
| 64 | func (this *LexCharLit) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 65 | return visitor.Visit(this) |
| 66 | } |
| 67 | |
| 68 | func (this *LexCharRange) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 69 | return visitor.Visit(this) |
| 70 | } |
| 71 | |
| 72 | func (this *LexDot) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 73 | return visitor.Visit(this) |
| 74 | } |
| 75 | |
| 76 | func (this *LexGroupPattern) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 77 | for _, term := range this.LexPattern.Alternatives { |
| 78 | if v := walkLexNode(term, visitor); v == nil { |
| 79 | return nil |
| 80 | } |
| 81 | } |
| 82 | return visitor.Visit(this) |
| 83 | } |
| 84 | |
| 85 | func (this *LexIgnoredTokDef) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 86 | return visitor.Visit(this) |
| 87 | } |
| 88 | |
| 89 | func (this *LexImports) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 90 | return visitor.Visit(this) |
| 91 | } |
| 92 | |
| 93 | func (this *LexOptPattern) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 94 | for _, term := range this.LexPattern.Alternatives { |
| 95 | if v := walkLexNode(term, visitor); v == nil { |
| 96 | return nil |
| 97 | } |
| 98 | } |
| 99 | return visitor.Visit(this) |
| 100 | } |
| 101 | |
| 102 | func (this *LexPattern) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 103 | for _, term := range this.Alternatives { |
| 104 | if v := walkLexNode(term, visitor); v == nil { |
| 105 | return nil |
| 106 | } |
| 107 | } |
| 108 | return visitor.Visit(this) |
| 109 | } |
| 110 | |
| 111 | func (this *LexProductions) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 112 | for _, term := range this.Productions { |
| 113 | if v := walkLexNode(term, visitor); v == nil { |
| 114 | return nil |
| 115 | } |
| 116 | } |
| 117 | return visitor.Visit(this) |
| 118 | } |
| 119 | |
| 120 | func (this *LexRegDef) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 121 | return visitor.Visit(this) |
| 122 | } |
| 123 | |
| 124 | func (this *LexRegDefId) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 125 | return visitor.Visit(this) |
| 126 | } |
| 127 | |
| 128 | func (this *LexRepPattern) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 129 | for _, term := range this.LexPattern.Alternatives { |
| 130 | if v := walkLexNode(term, visitor); v == nil { |
| 131 | return nil |
| 132 | } |
| 133 | } |
| 134 | return visitor.Visit(this) |
| 135 | } |
| 136 | |
| 137 | func (this *LexTokDef) Walk(visitor LexNodeVisitor) LexNodeVisitor { |
| 138 | return visitor.Visit(this) |
| 139 | } |