github.com/team-ide/go-dialect@v1.9.20/vitess/sqlparser/rewriter_api.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package sqlparser
    18  
    19  // The rewriter was heavily inspired by https://github.com/golang/tools/blob/master/go/ast/astutil/rewrite.go
    20  
    21  // Rewrite traverses a syntax tree recursively, starting with root,
    22  // and calling pre and post for each node as described below.
    23  // Rewrite returns the syntax tree, possibly modified.
    24  //
    25  // If pre is not nil, it is called for each node before the node's
    26  // children are traversed (pre-order). If pre returns false, no
    27  // children are traversed, and post is not called for that node.
    28  //
    29  // If post is not nil, and a prior call of pre didn't return false,
    30  // post is called for each node after its children are traversed
    31  // (post-order). If post returns false, traversal is terminated and
    32  // Apply returns immediately.
    33  //
    34  // Only fields that refer to AST nodes are considered children;
    35  // i.e., fields of basic types (strings, []byte, etc.) are ignored.
    36  //
    37  func Rewrite(node SQLNode, pre, post ApplyFunc) (result SQLNode) {
    38  	parent := &RootNode{node}
    39  
    40  	// this is the root-replacer, used when the user replaces the root of the ast
    41  	replacer := func(newNode SQLNode, _ SQLNode) {
    42  		parent.SQLNode = newNode
    43  	}
    44  
    45  	a := &application{
    46  		pre:  pre,
    47  		post: post,
    48  	}
    49  
    50  	a.rewriteSQLNode(parent, node, replacer)
    51  
    52  	return parent.SQLNode
    53  }
    54  
    55  // RootNode is the root node of the AST when rewriting. It is the first element of the tree.
    56  type RootNode struct {
    57  	SQLNode
    58  }
    59  
    60  // An ApplyFunc is invoked by Rewrite for each node n, even if n is nil,
    61  // before and/or after the node's children, using a Cursor describing
    62  // the current node and providing operations on it.
    63  //
    64  // The return value of ApplyFunc controls the syntax tree traversal.
    65  // See Rewrite for details.
    66  type ApplyFunc func(*Cursor) bool
    67  
    68  // A Cursor describes a node encountered during Apply.
    69  // Information about the node and its parent is available
    70  // from the Node and Parent methods.
    71  type Cursor struct {
    72  	parent   SQLNode
    73  	replacer replacerFunc
    74  	node     SQLNode
    75  
    76  	// marks that the node has been replaced, and the new node should be visited
    77  	revisit bool
    78  }
    79  
    80  // Node returns the current Node.
    81  func (c *Cursor) Node() SQLNode { return c.node }
    82  
    83  // Parent returns the parent of the current Node.
    84  func (c *Cursor) Parent() SQLNode { return c.parent }
    85  
    86  // Replace replaces the current node in the parent field with this new object. The use needs to make sure to not
    87  // replace the object with something of the wrong type, or the visitor will panic.
    88  func (c *Cursor) Replace(newNode SQLNode) {
    89  	c.replacer(newNode, c.parent)
    90  	c.node = newNode
    91  }
    92  
    93  // ReplacerF returns a replace func that will work even when the cursor has moved to a different node.
    94  func (c *Cursor) ReplacerF() func(newNode SQLNode) {
    95  	replacer := c.replacer
    96  	parent := c.parent
    97  	return func(newNode SQLNode) {
    98  		replacer(newNode, parent)
    99  	}
   100  }
   101  
   102  // ReplaceAndRevisit replaces the current node in the parent field with this new object.
   103  // When used, this will abort the visitation of the current node - no post or children visited,
   104  // and the new node visited.
   105  func (c *Cursor) ReplaceAndRevisit(newNode SQLNode) {
   106  	switch newNode.(type) {
   107  	case SelectExprs:
   108  	default:
   109  		// We need to add support to the generated code for when to look at the revisit flag. At the moment it is only
   110  		// there for slices of SQLNode implementations
   111  		panic("no support added for this type yet")
   112  	}
   113  
   114  	c.replacer(newNode, c.parent)
   115  	c.node = newNode
   116  	c.revisit = true
   117  }
   118  
   119  type replacerFunc func(newNode, parent SQLNode)
   120  
   121  // application carries all the shared data so we can pass it around cheaply.
   122  type application struct {
   123  	pre, post ApplyFunc
   124  	cur       Cursor
   125  }