github.com/go-maxhub/gremlins@v1.0.1-0.20231227222204-b03a6a1e3e09/core/engine/node.go (about)

     1  /*
     2   * Copyright 2022 The Gremlins 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 engine
    18  
    19  import (
    20  	"go/ast"
    21  	"go/token"
    22  )
    23  
    24  // NodeToken is the reference to the actualToken that will be mutated during
    25  // the mutation testing.
    26  type NodeToken struct {
    27  	tok    *token.Token
    28  	TokPos token.Pos
    29  }
    30  
    31  // NewTokenNode checks if the ast.Node implementation is supported by
    32  // Gremlins and gets its Tok/Op and relative position.
    33  // It returns false as second parameter if the implementation is not
    34  // supported.
    35  func NewTokenNode(n ast.Node) (*NodeToken, bool) {
    36  	var tok *token.Token
    37  	var pos token.Pos
    38  	switch n := n.(type) {
    39  	case *ast.AssignStmt:
    40  		tok = &n.Tok
    41  		pos = n.TokPos
    42  	case *ast.BinaryExpr:
    43  		tok = &n.Op
    44  		pos = n.OpPos
    45  	case *ast.BranchStmt:
    46  		tok = &n.Tok
    47  		pos = n.TokPos
    48  	case *ast.IncDecStmt:
    49  		tok = &n.Tok
    50  		pos = n.TokPos
    51  	case *ast.UnaryExpr:
    52  		tok = &n.Op
    53  		pos = n.OpPos
    54  	default:
    55  		return &NodeToken{}, false
    56  	}
    57  
    58  	return &NodeToken{
    59  		tok:    tok,
    60  		TokPos: pos,
    61  	}, true
    62  }
    63  
    64  // Tok returns the reference to the token.Token.
    65  func (n *NodeToken) Tok() token.Token {
    66  	return *n.tok
    67  }
    68  
    69  // SetTok sets the token.Token of the tokenNode.
    70  func (n *NodeToken) SetTok(t token.Token) {
    71  	*n.tok = t
    72  }