2019-10-16 18:21:01 +09:00
|
|
|
package ast
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"math"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"github.com/goccy/go-yaml/token"
|
|
|
|
|
)
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// NodeType type identifier of node
|
|
|
|
|
type NodeType int
|
|
|
|
|
|
|
|
|
|
const (
|
2019-10-21 03:49:01 +09:00
|
|
|
// UnknownNodeType type identifier for default
|
2019-10-21 03:33:41 +09:00
|
|
|
UnknownNodeType NodeType = iota
|
2019-10-21 03:49:01 +09:00
|
|
|
// DocumentType type identifier for document node
|
2019-10-21 03:33:41 +09:00
|
|
|
DocumentType
|
2019-10-21 03:49:01 +09:00
|
|
|
// NullType type identifier for null node
|
2019-10-21 03:33:41 +09:00
|
|
|
NullType
|
2019-10-21 03:49:01 +09:00
|
|
|
// BoolType type identifier for boolean node
|
2019-10-21 03:33:41 +09:00
|
|
|
BoolType
|
2019-10-21 03:49:01 +09:00
|
|
|
// IntegerType type identifier for integer node
|
2019-10-21 03:33:41 +09:00
|
|
|
IntegerType
|
2019-10-21 03:49:01 +09:00
|
|
|
// FloatType type identifier for float node
|
2019-10-21 03:33:41 +09:00
|
|
|
FloatType
|
2019-10-21 03:49:01 +09:00
|
|
|
// InfinityType type identifier for infinity node
|
2019-10-21 03:33:41 +09:00
|
|
|
InfinityType
|
2019-10-21 03:49:01 +09:00
|
|
|
// NanType type identifier for nan node
|
2019-10-21 03:33:41 +09:00
|
|
|
NanType
|
2019-10-21 03:49:01 +09:00
|
|
|
// StringType type identifier for string node
|
2019-10-21 03:33:41 +09:00
|
|
|
StringType
|
2019-10-21 03:49:01 +09:00
|
|
|
// MergeKeyType type identifier for merge key node
|
2019-10-21 03:33:41 +09:00
|
|
|
MergeKeyType
|
2019-10-21 03:49:01 +09:00
|
|
|
// LiteralType type identifier for literal node
|
2019-10-21 03:33:41 +09:00
|
|
|
LiteralType
|
2019-10-31 12:14:39 +09:00
|
|
|
// MappingType type identifier for mapping node
|
|
|
|
|
MappingType
|
2019-10-21 03:49:01 +09:00
|
|
|
// MappingValueType type identifier for mapping value node
|
2019-10-21 03:33:41 +09:00
|
|
|
MappingValueType
|
2019-10-21 03:49:01 +09:00
|
|
|
// SequenceType type identifier for sequence node
|
2019-10-21 03:33:41 +09:00
|
|
|
SequenceType
|
2019-10-21 03:49:01 +09:00
|
|
|
// AnchorType type identifier for anchor node
|
2019-10-21 03:33:41 +09:00
|
|
|
AnchorType
|
2019-10-21 03:49:01 +09:00
|
|
|
// AliasType type identifier for alias node
|
2019-10-21 03:33:41 +09:00
|
|
|
AliasType
|
2019-10-21 03:49:01 +09:00
|
|
|
// DirectiveType type identifier for directive node
|
2019-10-21 03:33:41 +09:00
|
|
|
DirectiveType
|
2019-10-21 03:49:01 +09:00
|
|
|
// TagType type identifier for tag node
|
2019-10-21 03:33:41 +09:00
|
|
|
TagType
|
|
|
|
|
)
|
|
|
|
|
|
2019-10-24 23:51:50 +09:00
|
|
|
// String node type identifier to text
|
|
|
|
|
func (t NodeType) String() string {
|
|
|
|
|
switch t {
|
|
|
|
|
case UnknownNodeType:
|
|
|
|
|
return "UnknownNode"
|
|
|
|
|
case DocumentType:
|
|
|
|
|
return "Document"
|
|
|
|
|
case NullType:
|
|
|
|
|
return "Null"
|
|
|
|
|
case BoolType:
|
|
|
|
|
return "Bool"
|
|
|
|
|
case IntegerType:
|
|
|
|
|
return "Integer"
|
|
|
|
|
case FloatType:
|
|
|
|
|
return "Float"
|
|
|
|
|
case InfinityType:
|
|
|
|
|
return "Infinity"
|
|
|
|
|
case NanType:
|
|
|
|
|
return "Nan"
|
|
|
|
|
case StringType:
|
|
|
|
|
return "String"
|
|
|
|
|
case MergeKeyType:
|
|
|
|
|
return "MergeKey"
|
|
|
|
|
case LiteralType:
|
|
|
|
|
return "Literal"
|
2019-10-31 12:14:39 +09:00
|
|
|
case MappingType:
|
|
|
|
|
return "Mapping"
|
2019-10-24 23:51:50 +09:00
|
|
|
case MappingValueType:
|
|
|
|
|
return "MappingValue"
|
|
|
|
|
case SequenceType:
|
|
|
|
|
return "Sequence"
|
|
|
|
|
case AnchorType:
|
|
|
|
|
return "Anchor"
|
|
|
|
|
case AliasType:
|
|
|
|
|
return "Alias"
|
|
|
|
|
case DirectiveType:
|
|
|
|
|
return "Directive"
|
|
|
|
|
case TagType:
|
|
|
|
|
return "Tag"
|
|
|
|
|
}
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Node type of node
|
|
|
|
|
type Node interface {
|
|
|
|
|
// String node to text
|
|
|
|
|
String() string
|
|
|
|
|
// GetToken returns token instance
|
|
|
|
|
GetToken() *token.Token
|
|
|
|
|
// Type returns type of node
|
|
|
|
|
Type() NodeType
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 17:07:00 +09:00
|
|
|
// File contains all documents in YAML file
|
2019-11-05 17:02:55 +09:00
|
|
|
type File struct {
|
|
|
|
|
Name string
|
|
|
|
|
Docs []*Document
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 17:07:00 +09:00
|
|
|
// String all documents to text
|
2019-11-05 17:02:55 +09:00
|
|
|
func (f *File) String() string {
|
|
|
|
|
docs := []string{}
|
|
|
|
|
for _, doc := range f.Docs {
|
|
|
|
|
docs = append(docs, doc.String())
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(docs, "\n")
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Document type of Document
|
2019-10-16 18:21:01 +09:00
|
|
|
type Document struct {
|
2019-11-05 17:02:55 +09:00
|
|
|
Start *token.Token // position of DocumentHeader ( `---` )
|
|
|
|
|
End *token.Token // position of DocumentEnd ( `...` )
|
|
|
|
|
Body Node
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-05 17:07:00 +09:00
|
|
|
// GetToken returns token instance
|
2019-11-05 17:02:55 +09:00
|
|
|
func (d *Document) GetToken() *token.Token {
|
|
|
|
|
return d.Body.GetToken()
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns DocumentType
|
|
|
|
|
func (d *Document) Type() NodeType { return DocumentType }
|
|
|
|
|
|
|
|
|
|
// String document to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (d *Document) String() string {
|
2019-11-05 17:02:55 +09:00
|
|
|
doc := []string{}
|
|
|
|
|
if d.Start != nil {
|
|
|
|
|
doc = append(doc, d.Start.Value)
|
|
|
|
|
}
|
|
|
|
|
doc = append(doc, d.Body.String())
|
|
|
|
|
if d.End != nil {
|
|
|
|
|
doc = append(doc, d.End.Value)
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
2019-11-05 17:02:55 +09:00
|
|
|
return strings.Join(doc, "\n")
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// ScalarNode type for scalar node
|
2019-10-16 18:21:01 +09:00
|
|
|
type ScalarNode interface {
|
|
|
|
|
Node
|
|
|
|
|
GetValue() interface{}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Null create node for null value
|
|
|
|
|
func Null(tk *token.Token) Node {
|
|
|
|
|
return &NullNode{
|
|
|
|
|
Token: tk,
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
2019-10-21 03:33:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Bool create node for boolean value
|
|
|
|
|
func Bool(tk *token.Token) Node {
|
|
|
|
|
b, _ := strconv.ParseBool(tk.Value)
|
|
|
|
|
return &BoolNode{
|
|
|
|
|
Token: tk,
|
|
|
|
|
Value: b,
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
2019-10-21 03:33:41 +09:00
|
|
|
}
|
|
|
|
|
|
2019-10-29 19:08:02 +09:00
|
|
|
func removeUnderScoreFromNumber(num string) string {
|
|
|
|
|
return strings.ReplaceAll(num, "_", "")
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Integer create node for integer value
|
|
|
|
|
func Integer(tk *token.Token) Node {
|
2019-10-29 19:08:02 +09:00
|
|
|
value := removeUnderScoreFromNumber(tk.Value)
|
|
|
|
|
switch tk.Type {
|
|
|
|
|
case token.BinaryIntegerType:
|
|
|
|
|
// skip two characters because binary token starts with '0b'
|
|
|
|
|
skipCharacterNum := 2
|
|
|
|
|
negativePrefix := ""
|
|
|
|
|
if value[0] == '-' {
|
|
|
|
|
skipCharacterNum++
|
|
|
|
|
negativePrefix = "-"
|
|
|
|
|
}
|
2019-10-29 20:15:27 +09:00
|
|
|
if len(negativePrefix) > 0 {
|
|
|
|
|
i, _ := strconv.ParseInt(negativePrefix+value[skipCharacterNum:], 2, 64)
|
|
|
|
|
return &IntegerNode{Token: tk, Value: i}
|
|
|
|
|
}
|
|
|
|
|
i, _ := strconv.ParseUint(negativePrefix+value[skipCharacterNum:], 2, 64)
|
2019-10-29 19:08:02 +09:00
|
|
|
return &IntegerNode{Token: tk, Value: i}
|
|
|
|
|
case token.OctetIntegerType:
|
|
|
|
|
// octet token starts with '0o' or '-0o' or '0' or '-0'
|
|
|
|
|
skipCharacterNum := 1
|
|
|
|
|
negativePrefix := ""
|
|
|
|
|
if value[0] == '-' {
|
|
|
|
|
skipCharacterNum++
|
|
|
|
|
if value[2] == 'o' {
|
|
|
|
|
skipCharacterNum++
|
|
|
|
|
}
|
|
|
|
|
negativePrefix = "-"
|
|
|
|
|
} else {
|
|
|
|
|
if value[1] == 'o' {
|
|
|
|
|
skipCharacterNum++
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-10-29 20:15:27 +09:00
|
|
|
if len(negativePrefix) > 0 {
|
|
|
|
|
i, _ := strconv.ParseInt(negativePrefix+value[skipCharacterNum:], 8, 64)
|
|
|
|
|
return &IntegerNode{Token: tk, Value: i}
|
|
|
|
|
}
|
|
|
|
|
i, _ := strconv.ParseUint(value[skipCharacterNum:], 8, 64)
|
2019-10-29 19:08:02 +09:00
|
|
|
return &IntegerNode{Token: tk, Value: i}
|
|
|
|
|
case token.HexIntegerType:
|
|
|
|
|
// hex token starts with '0x' or '-0x'
|
|
|
|
|
skipCharacterNum := 2
|
|
|
|
|
negativePrefix := ""
|
|
|
|
|
if value[0] == '-' {
|
|
|
|
|
skipCharacterNum++
|
|
|
|
|
negativePrefix = "-"
|
|
|
|
|
}
|
2019-10-29 20:15:27 +09:00
|
|
|
if len(negativePrefix) > 0 {
|
|
|
|
|
i, _ := strconv.ParseInt(negativePrefix+value[skipCharacterNum:], 16, 64)
|
|
|
|
|
return &IntegerNode{Token: tk, Value: i}
|
|
|
|
|
}
|
|
|
|
|
i, _ := strconv.ParseUint(value[skipCharacterNum:], 16, 64)
|
2019-10-29 19:08:02 +09:00
|
|
|
return &IntegerNode{Token: tk, Value: i}
|
|
|
|
|
}
|
2019-10-29 20:15:27 +09:00
|
|
|
if value[0] == '-' || value[0] == '+' {
|
|
|
|
|
i, _ := strconv.ParseInt(value, 10, 64)
|
|
|
|
|
return &IntegerNode{Token: tk, Value: i}
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
2019-10-29 20:15:27 +09:00
|
|
|
i, _ := strconv.ParseUint(value, 10, 64)
|
|
|
|
|
return &IntegerNode{Token: tk, Value: i}
|
2019-10-21 03:33:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Float create node for float value
|
|
|
|
|
func Float(tk *token.Token) Node {
|
2019-10-29 19:08:02 +09:00
|
|
|
f, _ := strconv.ParseFloat(removeUnderScoreFromNumber(tk.Value), 64)
|
2019-10-21 03:33:41 +09:00
|
|
|
return &FloatNode{
|
|
|
|
|
Token: tk,
|
|
|
|
|
Value: f,
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
2019-10-21 03:33:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Infinity create node for .inf or -.inf value
|
|
|
|
|
func Infinity(tk *token.Token) Node {
|
|
|
|
|
node := &InfinityNode{
|
|
|
|
|
Token: tk,
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
2019-10-21 03:33:41 +09:00
|
|
|
switch tk.Value {
|
2019-10-31 17:23:56 +09:00
|
|
|
case ".inf", ".Inf", ".INF":
|
2019-10-21 03:33:41 +09:00
|
|
|
node.Value = math.Inf(0)
|
2019-10-31 17:23:56 +09:00
|
|
|
case "-.inf", "-.Inf", "-.INF":
|
2019-10-21 03:33:41 +09:00
|
|
|
node.Value = math.Inf(-1)
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
2019-10-21 03:33:41 +09:00
|
|
|
return node
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Nan create node for .nan value
|
|
|
|
|
func Nan(tk *token.Token) Node {
|
|
|
|
|
return &NanNode{Token: tk}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// String create node for string value
|
|
|
|
|
func String(tk *token.Token) Node {
|
|
|
|
|
return &StringNode{
|
|
|
|
|
Token: tk,
|
|
|
|
|
Value: tk.Value,
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
2019-10-21 03:33:41 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MergeKey create node for merge key ( << )
|
|
|
|
|
func MergeKey(tk *token.Token) Node {
|
|
|
|
|
return &MergeKeyNode{
|
|
|
|
|
Token: tk,
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
2019-10-21 03:33:41 +09:00
|
|
|
}
|
2019-10-16 18:21:01 +09:00
|
|
|
|
2019-10-31 13:06:35 +09:00
|
|
|
// Mapping create node for map
|
2019-10-31 12:47:23 +09:00
|
|
|
func Mapping(tk *token.Token, isFlowStyle bool) *MappingNode {
|
|
|
|
|
return &MappingNode{
|
|
|
|
|
Start: tk,
|
|
|
|
|
IsFlowStyle: isFlowStyle,
|
|
|
|
|
Values: []*MappingValueNode{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 13:06:35 +09:00
|
|
|
// Sequence create node for sequence
|
|
|
|
|
func Sequence(tk *token.Token, isFlowStyle bool) *SequenceNode {
|
|
|
|
|
return &SequenceNode{
|
|
|
|
|
Start: tk,
|
|
|
|
|
IsFlowStyle: isFlowStyle,
|
|
|
|
|
Values: []Node{},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// NullNode type of null node
|
2019-10-16 18:21:01 +09:00
|
|
|
type NullNode struct {
|
|
|
|
|
ScalarNode
|
|
|
|
|
Token *token.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns NullType
|
|
|
|
|
func (n *NullNode) Type() NodeType { return NullType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *NullNode) GetToken() *token.Token {
|
|
|
|
|
return n.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// GetValue returns nil value
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *NullNode) GetValue() interface{} {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String returns `null` text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *NullNode) String() string {
|
|
|
|
|
return "null"
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// IntegerNode type of integer node
|
2019-10-16 18:21:01 +09:00
|
|
|
type IntegerNode struct {
|
|
|
|
|
ScalarNode
|
|
|
|
|
Token *token.Token
|
2019-10-29 20:15:27 +09:00
|
|
|
Value interface{} // int64 or uint64 value
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns IntegerType
|
|
|
|
|
func (n *IntegerNode) Type() NodeType { return IntegerType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *IntegerNode) GetToken() *token.Token {
|
|
|
|
|
return n.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// GetValue returns int64 value
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *IntegerNode) GetValue() interface{} {
|
|
|
|
|
return n.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String int64 to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *IntegerNode) String() string {
|
|
|
|
|
return n.Token.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// FloatNode type of float node
|
2019-10-16 18:21:01 +09:00
|
|
|
type FloatNode struct {
|
|
|
|
|
ScalarNode
|
|
|
|
|
Token *token.Token
|
|
|
|
|
Precision int
|
|
|
|
|
Value float64
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns FloatType
|
|
|
|
|
func (n *FloatNode) Type() NodeType { return FloatType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *FloatNode) GetToken() *token.Token {
|
|
|
|
|
return n.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// GetValue returns float64 value
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *FloatNode) GetValue() interface{} {
|
|
|
|
|
return n.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String float64 to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *FloatNode) String() string {
|
|
|
|
|
return n.Token.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// StringNode type of string node
|
2019-10-16 18:21:01 +09:00
|
|
|
type StringNode struct {
|
|
|
|
|
ScalarNode
|
|
|
|
|
Token *token.Token
|
|
|
|
|
Value string
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns StringType
|
|
|
|
|
func (n *StringNode) Type() NodeType { return StringType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *StringNode) GetToken() *token.Token {
|
|
|
|
|
return n.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// GetValue returns string value
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *StringNode) GetValue() interface{} {
|
|
|
|
|
return n.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String string value to text with quote if required
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *StringNode) String() string {
|
|
|
|
|
switch n.Token.Type {
|
|
|
|
|
case token.SingleQuoteType:
|
|
|
|
|
return fmt.Sprintf(`'%s'`, n.Value)
|
|
|
|
|
case token.DoubleQuoteType:
|
|
|
|
|
return fmt.Sprintf(`"%s"`, n.Value)
|
|
|
|
|
}
|
|
|
|
|
return n.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// LiteralNode type of literal node
|
2019-10-16 18:21:01 +09:00
|
|
|
type LiteralNode struct {
|
|
|
|
|
ScalarNode
|
|
|
|
|
Start *token.Token
|
|
|
|
|
Value *StringNode
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns LiteralType
|
|
|
|
|
func (n *LiteralNode) Type() NodeType { return LiteralType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *LiteralNode) GetToken() *token.Token {
|
|
|
|
|
return n.Start
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// GetValue returns string value
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *LiteralNode) GetValue() interface{} {
|
|
|
|
|
return n.Value.GetValue()
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String literal to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *LiteralNode) String() string {
|
|
|
|
|
return n.Value.String()
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// MergeKeyNode type of merge key node
|
2019-10-16 18:21:01 +09:00
|
|
|
type MergeKeyNode struct {
|
|
|
|
|
ScalarNode
|
|
|
|
|
Token *token.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns MergeKeyType
|
|
|
|
|
func (n *MergeKeyNode) Type() NodeType { return MergeKeyType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *MergeKeyNode) GetToken() *token.Token {
|
|
|
|
|
return n.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// GetValue returns '<<' value
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *MergeKeyNode) GetValue() interface{} {
|
|
|
|
|
return n.Token.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String returns '<<' value
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *MergeKeyNode) String() string {
|
|
|
|
|
return n.Token.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// BoolNode type of boolean node
|
2019-10-16 18:21:01 +09:00
|
|
|
type BoolNode struct {
|
|
|
|
|
ScalarNode
|
|
|
|
|
Token *token.Token
|
|
|
|
|
Value bool
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns BoolType
|
|
|
|
|
func (n *BoolNode) Type() NodeType { return BoolType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *BoolNode) GetToken() *token.Token {
|
|
|
|
|
return n.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// GetValue returns boolean value
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *BoolNode) GetValue() interface{} {
|
|
|
|
|
return n.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String boolean to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *BoolNode) String() string {
|
|
|
|
|
return n.Token.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// InfinityNode type of infinity node
|
2019-10-16 18:21:01 +09:00
|
|
|
type InfinityNode struct {
|
|
|
|
|
ScalarNode
|
|
|
|
|
Token *token.Token
|
|
|
|
|
Value float64
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns InfinityType
|
|
|
|
|
func (n *InfinityNode) Type() NodeType { return InfinityType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *InfinityNode) GetToken() *token.Token {
|
|
|
|
|
return n.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// GetValue returns math.Inf(0) or math.Inf(-1)
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *InfinityNode) GetValue() interface{} {
|
|
|
|
|
return n.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String infinity to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *InfinityNode) String() string {
|
|
|
|
|
return n.Token.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// NanNode type of nan node
|
2019-10-16 18:21:01 +09:00
|
|
|
type NanNode struct {
|
|
|
|
|
ScalarNode
|
|
|
|
|
Token *token.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns NanType
|
|
|
|
|
func (n *NanNode) Type() NodeType { return NanType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *NanNode) GetToken() *token.Token {
|
|
|
|
|
return n.Token
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// GetValue returns math.NaN()
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *NanNode) GetValue() interface{} {
|
|
|
|
|
return math.NaN()
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String returns .nan
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *NanNode) String() string {
|
|
|
|
|
return n.Token.Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 13:06:35 +09:00
|
|
|
// MapNode interface of MappingValueNode / MappingNode
|
2019-10-24 23:52:43 +09:00
|
|
|
type MapNode interface {
|
|
|
|
|
MapRange() *MapNodeIter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// MapNodeIter is an iterator for ranging over a MapNode
|
|
|
|
|
type MapNodeIter struct {
|
|
|
|
|
values []*MappingValueNode
|
|
|
|
|
idx int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const (
|
|
|
|
|
startRangeIndex = -1
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// Next advances the map iterator and reports whether there is another entry.
|
|
|
|
|
// It returns false when the iterator is exhausted.
|
|
|
|
|
func (m *MapNodeIter) Next() bool {
|
|
|
|
|
m.idx++
|
|
|
|
|
next := m.idx < len(m.values)
|
|
|
|
|
return next
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Key returns the key of the iterator's current map node entry.
|
|
|
|
|
func (m *MapNodeIter) Key() Node {
|
|
|
|
|
return m.values[m.idx].Key
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Value returns the value of the iterator's current map node entry.
|
|
|
|
|
func (m *MapNodeIter) Value() Node {
|
|
|
|
|
return m.values[m.idx].Value
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 12:25:33 +09:00
|
|
|
// MappingNode type of mapping node
|
|
|
|
|
type MappingNode struct {
|
|
|
|
|
Start *token.Token
|
|
|
|
|
End *token.Token
|
|
|
|
|
IsFlowStyle bool
|
2019-10-31 12:38:44 +09:00
|
|
|
Values []*MappingValueNode
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
|
|
|
|
|
2019-10-31 12:25:33 +09:00
|
|
|
// Type returns MappingType
|
|
|
|
|
func (n *MappingNode) Type() NodeType { return MappingType }
|
2019-10-21 03:33:41 +09:00
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-31 12:25:33 +09:00
|
|
|
func (n *MappingNode) GetToken() *token.Token {
|
2019-10-16 18:21:01 +09:00
|
|
|
return n.Start
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 12:25:33 +09:00
|
|
|
func (n *MappingNode) flowStyleString() string {
|
|
|
|
|
if len(n.Values) == 0 {
|
|
|
|
|
return "{}"
|
|
|
|
|
}
|
2019-10-16 18:21:01 +09:00
|
|
|
values := []string{}
|
|
|
|
|
for _, value := range n.Values {
|
|
|
|
|
values = append(values, strings.TrimLeft(value.String(), " "))
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("{%s}", strings.Join(values, ", "))
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 12:25:33 +09:00
|
|
|
func (n *MappingNode) blockStyleString() string {
|
2019-10-31 12:14:39 +09:00
|
|
|
if len(n.Values) == 0 {
|
|
|
|
|
return "{}"
|
|
|
|
|
}
|
2019-10-16 18:21:01 +09:00
|
|
|
values := []string{}
|
|
|
|
|
for _, value := range n.Values {
|
|
|
|
|
values = append(values, value.String())
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(values, "\n")
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 12:25:33 +09:00
|
|
|
// String mapping values to text
|
|
|
|
|
func (n *MappingNode) String() string {
|
|
|
|
|
if n.IsFlowStyle {
|
|
|
|
|
return n.flowStyleString()
|
|
|
|
|
}
|
|
|
|
|
return n.blockStyleString()
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 23:52:43 +09:00
|
|
|
// MapRange implements MapNode protocol
|
2019-10-31 12:14:39 +09:00
|
|
|
func (n *MappingNode) MapRange() *MapNodeIter {
|
2019-10-24 23:52:43 +09:00
|
|
|
return &MapNodeIter{
|
|
|
|
|
idx: startRangeIndex,
|
2019-10-31 12:38:44 +09:00
|
|
|
values: n.Values,
|
2019-10-24 23:52:43 +09:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// MappingValueNode type of mapping value
|
2019-10-16 18:21:01 +09:00
|
|
|
type MappingValueNode struct {
|
|
|
|
|
Start *token.Token
|
|
|
|
|
Key Node
|
|
|
|
|
Value Node
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns MappingValueType
|
|
|
|
|
func (n *MappingValueNode) Type() NodeType { return MappingValueType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *MappingValueNode) GetToken() *token.Token {
|
|
|
|
|
return n.Start
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String mapping value to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *MappingValueNode) String() string {
|
|
|
|
|
space := strings.Repeat(" ", n.Key.GetToken().Position.Column-1)
|
|
|
|
|
keyIndentLevel := n.Key.GetToken().Position.IndentLevel
|
|
|
|
|
valueIndentLevel := n.Value.GetToken().Position.IndentLevel
|
|
|
|
|
if _, ok := n.Value.(ScalarNode); ok {
|
|
|
|
|
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
|
|
|
|
|
} else if keyIndentLevel < valueIndentLevel {
|
|
|
|
|
return fmt.Sprintf("%s%s:\n%s", space, n.Key.String(), n.Value.String())
|
2019-10-31 13:06:35 +09:00
|
|
|
} else if m, ok := n.Value.(*MappingNode); ok && m.IsFlowStyle {
|
|
|
|
|
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
|
|
|
|
|
} else if s, ok := n.Value.(*SequenceNode); ok && s.IsFlowStyle {
|
2019-10-16 18:21:01 +09:00
|
|
|
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
|
|
|
|
|
} else if _, ok := n.Value.(*AnchorNode); ok {
|
|
|
|
|
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
|
|
|
|
|
} else if _, ok := n.Value.(*AliasNode); ok {
|
|
|
|
|
return fmt.Sprintf("%s%s: %s", space, n.Key.String(), n.Value.String())
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("%s%s:\n%s", space, n.Key.String(), n.Value.String())
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 23:52:43 +09:00
|
|
|
// MapRange implements MapNode protocol
|
|
|
|
|
func (n *MappingValueNode) MapRange() *MapNodeIter {
|
|
|
|
|
return &MapNodeIter{
|
|
|
|
|
idx: startRangeIndex,
|
|
|
|
|
values: []*MappingValueNode{n},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 13:06:35 +09:00
|
|
|
// ArrayNode interface of SequenceNode
|
2019-10-24 23:52:43 +09:00
|
|
|
type ArrayNode interface {
|
|
|
|
|
ArrayRange() *ArrayNodeIter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ArrayNodeIter is an iterator for ranging over a ArrayNode
|
|
|
|
|
type ArrayNodeIter struct {
|
|
|
|
|
values []Node
|
|
|
|
|
idx int
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Next advances the array iterator and reports whether there is another entry.
|
|
|
|
|
// It returns false when the iterator is exhausted.
|
|
|
|
|
func (m *ArrayNodeIter) Next() bool {
|
|
|
|
|
m.idx++
|
|
|
|
|
next := m.idx < len(m.values)
|
|
|
|
|
return next
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Value returns the value of the iterator's current array entry.
|
|
|
|
|
func (m *ArrayNodeIter) Value() Node {
|
|
|
|
|
return m.values[m.idx]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Len returns length of array
|
|
|
|
|
func (m *ArrayNodeIter) Len() int {
|
|
|
|
|
return len(m.values)
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 13:06:35 +09:00
|
|
|
// SequenceNode type of sequence node
|
|
|
|
|
type SequenceNode struct {
|
|
|
|
|
Start *token.Token
|
|
|
|
|
End *token.Token
|
|
|
|
|
IsFlowStyle bool
|
|
|
|
|
Values []Node
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
|
|
|
|
|
2019-10-31 13:06:35 +09:00
|
|
|
// Type returns SequenceType
|
|
|
|
|
func (n *SequenceNode) Type() NodeType { return SequenceType }
|
2019-10-21 03:33:41 +09:00
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-31 13:06:35 +09:00
|
|
|
func (n *SequenceNode) GetToken() *token.Token {
|
2019-10-16 18:21:01 +09:00
|
|
|
return n.Start
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 13:06:35 +09:00
|
|
|
func (n *SequenceNode) flowStyleString() string {
|
2019-10-16 18:21:01 +09:00
|
|
|
values := []string{}
|
|
|
|
|
for _, value := range n.Values {
|
|
|
|
|
values = append(values, value.String())
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("[%s]", strings.Join(values, ", "))
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 13:06:35 +09:00
|
|
|
func (n *SequenceNode) blockStyleString() string {
|
2019-10-16 18:21:01 +09:00
|
|
|
space := strings.Repeat(" ", n.Start.Position.Column-1)
|
|
|
|
|
values := []string{}
|
|
|
|
|
for _, value := range n.Values {
|
|
|
|
|
valueStr := value.String()
|
|
|
|
|
splittedValues := strings.Split(valueStr, "\n")
|
|
|
|
|
trimmedFirstValue := strings.TrimLeft(splittedValues[0], " ")
|
|
|
|
|
diffLength := len(splittedValues[0]) - len(trimmedFirstValue)
|
|
|
|
|
newValues := []string{trimmedFirstValue}
|
|
|
|
|
for i := 1; i < len(splittedValues); i++ {
|
|
|
|
|
trimmed := splittedValues[i][diffLength:]
|
|
|
|
|
newValues = append(newValues, fmt.Sprintf("%s %s", space, trimmed))
|
|
|
|
|
}
|
|
|
|
|
newValue := strings.Join(newValues, "\n")
|
|
|
|
|
values = append(values, fmt.Sprintf("%s- %s", space, newValue))
|
|
|
|
|
}
|
|
|
|
|
return strings.Join(values, "\n")
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-31 13:06:35 +09:00
|
|
|
// String sequence to text
|
|
|
|
|
func (n *SequenceNode) String() string {
|
|
|
|
|
if n.IsFlowStyle {
|
|
|
|
|
return n.flowStyleString()
|
|
|
|
|
}
|
|
|
|
|
return n.blockStyleString()
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-24 23:52:43 +09:00
|
|
|
// ArrayRange implements ArrayNode protocol
|
|
|
|
|
func (n *SequenceNode) ArrayRange() *ArrayNodeIter {
|
|
|
|
|
return &ArrayNodeIter{
|
|
|
|
|
idx: startRangeIndex,
|
|
|
|
|
values: n.Values,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// AnchorNode type of anchor node
|
2019-10-16 18:21:01 +09:00
|
|
|
type AnchorNode struct {
|
|
|
|
|
Start *token.Token
|
|
|
|
|
Name Node
|
|
|
|
|
Value Node
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns AnchorType
|
|
|
|
|
func (n *AnchorNode) Type() NodeType { return AnchorType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *AnchorNode) GetToken() *token.Token {
|
|
|
|
|
return n.Start
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String anchor to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *AnchorNode) String() string {
|
2019-10-19 22:01:36 +09:00
|
|
|
value := n.Value.String()
|
|
|
|
|
if len(strings.Split(value, "\n")) > 1 {
|
|
|
|
|
return fmt.Sprintf("&%s\n%s", n.Name.String(), value)
|
|
|
|
|
}
|
|
|
|
|
return fmt.Sprintf("&%s %s", n.Name.String(), value)
|
2019-10-16 18:21:01 +09:00
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// AliasNode type of alias node
|
2019-10-16 18:21:01 +09:00
|
|
|
type AliasNode struct {
|
|
|
|
|
Start *token.Token
|
|
|
|
|
Value Node
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns AliasType
|
|
|
|
|
func (n *AliasNode) Type() NodeType { return AliasType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *AliasNode) GetToken() *token.Token {
|
|
|
|
|
return n.Start
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String alias to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *AliasNode) String() string {
|
|
|
|
|
return fmt.Sprintf("*%s", n.Value.String())
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// DirectiveNode type of directive node
|
2019-10-16 18:21:01 +09:00
|
|
|
type DirectiveNode struct {
|
|
|
|
|
Start *token.Token
|
|
|
|
|
Value Node
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns DirectiveType
|
|
|
|
|
func (n *DirectiveNode) Type() NodeType { return DirectiveType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *DirectiveNode) GetToken() *token.Token {
|
|
|
|
|
return n.Start
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String directive to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *DirectiveNode) String() string {
|
|
|
|
|
return fmt.Sprintf("%s%s", n.Start.Value, n.Value.String())
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// TagNode type of tag node
|
2019-10-16 18:21:01 +09:00
|
|
|
type TagNode struct {
|
|
|
|
|
Start *token.Token
|
|
|
|
|
Value Node
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Type returns TagType
|
|
|
|
|
func (n *TagNode) Type() NodeType { return TagType }
|
|
|
|
|
|
|
|
|
|
// GetToken returns token instance
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *TagNode) GetToken() *token.Token {
|
|
|
|
|
return n.Start
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// String tag to text
|
2019-10-16 18:21:01 +09:00
|
|
|
func (n *TagNode) String() string {
|
|
|
|
|
return fmt.Sprintf("%s %s", n.Start.Value, n.Value.String())
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 12:53:30 +09:00
|
|
|
// Visitor has Visit method that is invokded for each node encountered by Walk.
|
2019-10-21 03:33:41 +09:00
|
|
|
// If the result visitor w is not nil, Walk visits each of the children of node with the visitor w,
|
|
|
|
|
// followed by a call of w.Visit(nil).
|
2019-10-16 18:21:01 +09:00
|
|
|
type Visitor interface {
|
|
|
|
|
Visit(Node) Visitor
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-21 03:33:41 +09:00
|
|
|
// Walk traverses an AST in depth-first order: It starts by calling v.Visit(node); node must not be nil.
|
|
|
|
|
// If the visitor w returned by v.Visit(node) is not nil,
|
|
|
|
|
// Walk is invoked recursively with visitor w for each of the non-nil children of node,
|
|
|
|
|
// followed by a call of w.Visit(nil).
|
2019-10-16 18:21:01 +09:00
|
|
|
func Walk(v Visitor, node Node) {
|
|
|
|
|
if v = v.Visit(node); v == nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch n := node.(type) {
|
|
|
|
|
case *NullNode:
|
|
|
|
|
case *IntegerNode:
|
|
|
|
|
case *FloatNode:
|
|
|
|
|
case *StringNode:
|
|
|
|
|
case *MergeKeyNode:
|
|
|
|
|
case *BoolNode:
|
|
|
|
|
case *InfinityNode:
|
|
|
|
|
case *NanNode:
|
2019-10-31 12:14:39 +09:00
|
|
|
case *MappingNode:
|
2019-10-16 18:21:01 +09:00
|
|
|
for _, value := range n.Values {
|
|
|
|
|
Walk(v, value)
|
|
|
|
|
}
|
|
|
|
|
case *MappingValueNode:
|
|
|
|
|
Walk(v, n.Key)
|
|
|
|
|
Walk(v, n.Value)
|
|
|
|
|
case *SequenceNode:
|
|
|
|
|
for _, value := range n.Values {
|
|
|
|
|
Walk(v, value)
|
|
|
|
|
}
|
|
|
|
|
case *AnchorNode:
|
|
|
|
|
Walk(v, n.Name)
|
|
|
|
|
Walk(v, n.Value)
|
|
|
|
|
case *AliasNode:
|
|
|
|
|
Walk(v, n.Value)
|
|
|
|
|
}
|
|
|
|
|
}
|