github.com/vescale/zgraph@v0.0.0-20230410094002-959c02d50f95/parser/ast/ast.go (about)

     1  // Copyright 2022 zGraph Authors. All rights reserved.
     2  //
     3  // Copyright 2015 PingCAP, Inc.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  // Package ast is the abstract syntax tree parsed from a SQL statement by parser.
    17  // It can be analysed and transformed by optimizer.
    18  package ast
    19  
    20  import (
    21  	"github.com/vescale/zgraph/parser/format"
    22  )
    23  
    24  // Node is the basic element of the AST.
    25  // Interfaces embed Node should have 'Node' name suffix.
    26  type Node interface {
    27  	// Restore returns the sql text from ast tree
    28  	Restore(ctx *format.RestoreCtx) error
    29  	// Accept accepts Visitor to visit itself.
    30  	// The returned node should replace original node.
    31  	// ok returns false to stop visiting.
    32  	//
    33  	// Implementation of this method should first call visitor.Enter,
    34  	// assign the returned node to its method receiver, if skipChildren returns true,
    35  	// children should be skipped. Otherwise, call its children in particular order that
    36  	// later elements depends on former elements. Finally, return visitor.Leave.
    37  	Accept(v Visitor) (node Node, ok bool)
    38  	// Text returns the original text of the element.
    39  	Text() string
    40  	// SetText sets original text to the Node.
    41  	SetText(text string)
    42  	// SetOriginTextPosition set the start offset of this node in the origin text.
    43  	SetOriginTextPosition(offset int)
    44  	// OriginTextPosition get the start offset of this node in the origin text.
    45  	OriginTextPosition() int
    46  }
    47  
    48  // ExprNode is a node that can be evaluated.
    49  // Name of implementations should have 'Expr' suffix.
    50  type ExprNode interface {
    51  	Node
    52  	expression()
    53  }
    54  
    55  // StmtNode represents statement node.
    56  // Name of implementations should have 'Stmt' suffix.
    57  type StmtNode interface {
    58  	Node
    59  	statement()
    60  }
    61  
    62  // DDLNode represents DDL statement node.
    63  type DDLNode interface {
    64  	StmtNode
    65  	ddlStatement()
    66  }
    67  
    68  // DMLNode represents DML statement node.
    69  type DMLNode interface {
    70  	StmtNode
    71  	dmlStatement()
    72  }
    73  
    74  // Visitor visits a Node.
    75  type Visitor interface {
    76  	// Enter is called before children nodes are visited.
    77  	// The returned node must be the same type as the input node n.
    78  	// skipChildren returns true means children nodes should be skipped,
    79  	// this is useful when work is done in Enter and there is no need to visit children.
    80  	Enter(n Node) (node Node, skipChildren bool)
    81  	// Leave is called after children nodes have been visited.
    82  	// The returned node's type can be different from the input node if it is a ExprNode,
    83  	// Non-expression node must be the same type as the input node n.
    84  	// ok returns false to stop visiting.
    85  	Leave(n Node) (node Node, ok bool)
    86  }