-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
188 lines (153 loc) · 4.44 KB
/
Copy pathnode.go
File metadata and controls
188 lines (153 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package yaml
import "github.com/ercross/yaml/token"
type (
NodeType int8
)
const (
NodeTypeUnknown NodeType = iota // Node type is unknown and used as a placeholder for uninitialized or invalid nodes.
// NodeTypeScalar represents a single value node, such as a string, integer, or boolean.
// Example:
// key: "Hello, World"
NodeTypeScalar
// NodeTypeDocument is the root of a YAML document. It represents the whole document structure and may contain sequences or mappings.
// Example:
// ---
// title: "YAML Example"
// ---
NodeTypeDocument
// NodeTypeMultilineString represents a multi-line string, often using the `|` symbol to preserve line breaks.
// Example:
// description: |
// This is a multi-line
// string in YAML.
NodeTypeMultilineString
// NodeTypeFoldedString is a multi-line text node that uses the `>` symbol to fold lines. Line breaks within folded text are converted to spaces.
// Example:
// note: >
// This text will be folded
// into a single line when parsed.
NodeTypeFoldedString
// NodeTypeAnchor represents an anchor node, allowing values to be reused or referenced elsewhere in the document.
// Example:
// base: &baseAnchor "Base Value"
NodeTypeAnchor
// NodeTypeSequenceFlowStyle represents a sequence in flow style, denoted by square brackets `[]`. This style is non-nestable.
// Example:
// items: [1, 2, 3]
NodeTypeSequenceFlowStyle
// NodeTypeSequenceBlockStyle represents a sequence in block style, using `-` to denote each item. This style can contain nested elements.
// Example:
// items:
// - name: "Item 1"
// - name: "Item 2"
NodeTypeSequenceBlockStyle
// NodeTypeMappingFlowStyle represents a mapping in flow style, using curly braces `{}`. This style is non-nestable.
// Example:
// info: { key1: "value1", key2: "value2" }
NodeTypeMappingFlowStyle
// NodeTypeMappingBlockStyle represents a mapping in block style, where each key-value pair is on a new line.
// This style can contain nested mappings, sequences, or scalars.
// Example:
// user:
// name: "Alice"
// age: 30
NodeTypeMappingBlockStyle
// NodeTypeAlias represents a reference to an anchor node, allowing the reuse of values defined by an anchor.
// Example:
// base: &baseAnchor "Base Value"
// alias: *baseAnchor
NodeTypeAlias
)
type (
Node interface {
Key() string
Type() NodeType
Value() interface{}
Children() any
}
NodeBuilder interface {
AddChild(Node)
SetValue(any)
ToNode() Node
SetKey(key string)
CurrentPosition() token.Location
SetCurrentPosition(position token.Location)
}
)
type (
// DocumentNode is usually the root of an AbstractSyntaxTree
DocumentNode struct {
children []Node
}
ScalarNode struct {
value any
key string
currentPosition token.Location
}
)
// IsNestable checks if NodeType can serve as a parent node to other child nodes.
// In the context of YAML, only certain node types can nest other nodes.
// Specifically, NodeTypeSequenceBlockStyle and NodeTypeMappingBlockStyle are nestable,
// while other types such as scalars, flow styles, and aliases are not.
func (nt NodeType) IsNestable() bool {
switch nt {
case NodeTypeSequenceBlockStyle, NodeTypeMappingBlockStyle:
return true
default:
return false
}
}
func NewDocumentNode() *DocumentNode {
return &DocumentNode{}
}
func (n *DocumentNode) Key() string {
return ""
}
func (n *DocumentNode) Type() NodeType {
return NodeTypeDocument
}
func (n *DocumentNode) Value() any {
return nil
}
func (n *DocumentNode) Children() []Node {
return n.children
}
func (n *DocumentNode) AddChild(child Node) {
n.children = append(n.children, child)
}
func (n *DocumentNode) SetValue(_ interface{}) {
return
}
func NewScalarNodeBuilder() *ScalarNode {
return &ScalarNode{}
}
func (n *ScalarNode) Key() string {
return n.key
}
func (n *ScalarNode) Type() NodeType {
return NodeTypeScalar
}
func (n *ScalarNode) Value() any {
return n.value
}
func (n *ScalarNode) Children() any {
panic("can not get children of scalar node")
}
func (n *ScalarNode) AddChild(_ Node) {
panic("can not add child to scalar node")
}
func (n *ScalarNode) SetValue(v any) {
n.value = v
}
func (n *ScalarNode) SetKey(key string) {
n.key = key
}
func (n *ScalarNode) ToNode() Node {
return n
}
func (n *ScalarNode) SetCurrentPosition(position token.Location) {
n.currentPosition = position
}
func (n *ScalarNode) CurrentPosition() token.Location {
return n.currentPosition
}