| 1 | package parser |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | |
| 6 | "github.com/goccy/go-yaml/ast" |
| 7 | "github.com/goccy/go-yaml/internal/errors" |
| 8 | "github.com/goccy/go-yaml/token" |
| 9 | ) |
| 10 | |
| 11 | func newMappingNode(ctx *context, tk *Token, isFlow bool, values ...*ast.MappingValueNode) (*ast.MappingNode, error) { |
| 12 | node := ast.Mapping(tk.RawToken(), isFlow, values...) |
| 13 | node.SetPath(ctx.path) |
| 14 | return node, nil |
| 15 | } |
| 16 | |
| 17 | func newMappingValueNode(ctx *context, colonTk, entryTk *Token, key ast.MapKeyNode, value ast.Node) (*ast.MappingValueNode, error) { |
| 18 | node := ast.MappingValue(colonTk.RawToken(), key, value) |
| 19 | node.SetPath(ctx.path) |
| 20 | node.CollectEntry = entryTk.RawToken() |
| 21 | if key.GetToken().Position.Line == value.GetToken().Position.Line { |
| 22 | // originally key was commented, but now that null value has been added, value must be commented. |
| 23 | if err := setLineComment(ctx, value, colonTk); err != nil { |
| 24 | return nil, err |
| 25 | } |
| 26 | // set line comment by colonTk or entryTk. |
| 27 | if err := setLineComment(ctx, value, entryTk); err != nil { |
| 28 | return nil, err |
| 29 | } |
| 30 | } else { |
| 31 | if err := setLineComment(ctx, key, colonTk); err != nil { |
| 32 | return nil, err |
| 33 | } |
| 34 | // set line comment by colonTk or entryTk. |
| 35 | if err := setLineComment(ctx, key, entryTk); err != nil { |
| 36 | return nil, err |
| 37 | } |
| 38 | } |
| 39 | return node, nil |
| 40 | } |
| 41 | |
| 42 | func newMappingKeyNode(ctx *context, tk *Token) (*ast.MappingKeyNode, error) { |
| 43 | node := ast.MappingKey(tk.RawToken()) |
| 44 | node.SetPath(ctx.path) |
| 45 | if err := setLineComment(ctx, node, tk); err != nil { |
| 46 | return nil, err |
| 47 | } |
| 48 | return node, nil |
| 49 | } |
| 50 | |
| 51 | func newAnchorNode(ctx *context, tk *Token) (*ast.AnchorNode, error) { |
| 52 | node := ast.Anchor(tk.RawToken()) |
| 53 | node.SetPath(ctx.path) |
| 54 | if err := setLineComment(ctx, node, tk); err != nil { |
| 55 | return nil, err |
| 56 | } |
| 57 | return node, nil |
| 58 | } |
| 59 | |
| 60 | func newAliasNode(ctx *context, tk *Token) (*ast.AliasNode, error) { |
| 61 | node := ast.Alias(tk.RawToken()) |
| 62 | node.SetPath(ctx.path) |
| 63 | if err := setLineComment(ctx, node, tk); err != nil { |
| 64 | return nil, err |
| 65 | } |
| 66 | return node, nil |
| 67 | } |
| 68 | |
| 69 | func newDirectiveNode(ctx *context, tk *Token) (*ast.DirectiveNode, error) { |
| 70 | node := ast.Directive(tk.RawToken()) |
| 71 | node.SetPath(ctx.path) |
| 72 | if err := setLineComment(ctx, node, tk); err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | return node, nil |
| 76 | } |
| 77 | |
| 78 | func newMergeKeyNode(ctx *context, tk *Token) (*ast.MergeKeyNode, error) { |
| 79 | node := ast.MergeKey(tk.RawToken()) |
| 80 | node.SetPath(ctx.path) |
| 81 | if err := setLineComment(ctx, node, tk); err != nil { |
| 82 | return nil, err |
| 83 | } |
| 84 | return node, nil |
| 85 | } |
| 86 | |
| 87 | func newNullNode(ctx *context, tk *Token) (*ast.NullNode, error) { |
| 88 | node := ast.Null(tk.RawToken()) |
| 89 | node.SetPath(ctx.path) |
| 90 | if err := setLineComment(ctx, node, tk); err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | return node, nil |
| 94 | } |
| 95 | |
| 96 | func newBoolNode(ctx *context, tk *Token) (*ast.BoolNode, error) { |
| 97 | node := ast.Bool(tk.RawToken()) |
| 98 | node.SetPath(ctx.path) |
| 99 | if err := setLineComment(ctx, node, tk); err != nil { |
| 100 | return nil, err |
| 101 | } |
| 102 | return node, nil |
| 103 | } |
| 104 | |
| 105 | func newIntegerNode(ctx *context, tk *Token) (*ast.IntegerNode, error) { |
| 106 | node := ast.Integer(tk.RawToken()) |
| 107 | node.SetPath(ctx.path) |
| 108 | if err := setLineComment(ctx, node, tk); err != nil { |
| 109 | return nil, err |
| 110 | } |
| 111 | return node, nil |
| 112 | } |
| 113 | |
| 114 | func newFloatNode(ctx *context, tk *Token) (*ast.FloatNode, error) { |
| 115 | node := ast.Float(tk.RawToken()) |
| 116 | node.SetPath(ctx.path) |
| 117 | if err := setLineComment(ctx, node, tk); err != nil { |
| 118 | return nil, err |
| 119 | } |
| 120 | return node, nil |
| 121 | } |
| 122 | |
| 123 | func newInfinityNode(ctx *context, tk *Token) (*ast.InfinityNode, error) { |
| 124 | node := ast.Infinity(tk.RawToken()) |
| 125 | node.SetPath(ctx.path) |
| 126 | if err := setLineComment(ctx, node, tk); err != nil { |
| 127 | return nil, err |
| 128 | } |
| 129 | return node, nil |
| 130 | } |
| 131 | |
| 132 | func newNanNode(ctx *context, tk *Token) (*ast.NanNode, error) { |
| 133 | node := ast.Nan(tk.RawToken()) |
| 134 | node.SetPath(ctx.path) |
| 135 | if err := setLineComment(ctx, node, tk); err != nil { |
| 136 | return nil, err |
| 137 | } |
| 138 | return node, nil |
| 139 | } |
| 140 | |
| 141 | func newStringNode(ctx *context, tk *Token) (*ast.StringNode, error) { |
| 142 | node := ast.String(tk.RawToken()) |
| 143 | node.SetPath(ctx.path) |
| 144 | if err := setLineComment(ctx, node, tk); err != nil { |
| 145 | return nil, err |
| 146 | } |
| 147 | return node, nil |
| 148 | } |
| 149 | |
| 150 | func newLiteralNode(ctx *context, tk *Token) (*ast.LiteralNode, error) { |
| 151 | node := ast.Literal(tk.RawToken()) |
| 152 | node.SetPath(ctx.path) |
| 153 | if err := setLineComment(ctx, node, tk); err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | return node, nil |
| 157 | } |
| 158 | |
| 159 | func newTagNode(ctx *context, tk *Token) (*ast.TagNode, error) { |
| 160 | node := ast.Tag(tk.RawToken()) |
| 161 | node.SetPath(ctx.path) |
| 162 | if err := setLineComment(ctx, node, tk); err != nil { |
| 163 | return nil, err |
| 164 | } |
| 165 | return node, nil |
| 166 | } |
| 167 | |
| 168 | func newSequenceNode(ctx *context, tk *Token, isFlow bool) (*ast.SequenceNode, error) { |
| 169 | node := ast.Sequence(tk.RawToken(), isFlow) |
| 170 | node.SetPath(ctx.path) |
| 171 | if err := setLineComment(ctx, node, tk); err != nil { |
| 172 | return nil, err |
| 173 | } |
| 174 | return node, nil |
| 175 | } |
| 176 | |
| 177 | func newTagDefaultScalarValueNode(ctx *context, tag *token.Token) (ast.ScalarNode, error) { |
| 178 | pos := *(tag.Position) |
| 179 | pos.Column++ |
| 180 | |
| 181 | var ( |
| 182 | tk *Token |
| 183 | node ast.ScalarNode |
| 184 | ) |
| 185 | switch token.ReservedTagKeyword(tag.Value) { |
| 186 | case token.IntegerTag: |
| 187 | tk = &Token{Token: token.New("0", "0", &pos)} |
| 188 | n, err := newIntegerNode(ctx, tk) |
| 189 | if err != nil { |
| 190 | return nil, err |
| 191 | } |
| 192 | node = n |
| 193 | case token.FloatTag: |
| 194 | tk = &Token{Token: token.New("0", "0", &pos)} |
| 195 | n, err := newFloatNode(ctx, tk) |
| 196 | if err != nil { |
| 197 | return nil, err |
| 198 | } |
| 199 | node = n |
| 200 | case token.StringTag, token.BinaryTag, token.TimestampTag: |
| 201 | tk = &Token{Token: token.New("", "", &pos)} |
| 202 | n, err := newStringNode(ctx, tk) |
| 203 | if err != nil { |
| 204 | return nil, err |
| 205 | } |
| 206 | node = n |
| 207 | case token.BooleanTag: |
| 208 | tk = &Token{Token: token.New("false", "false", &pos)} |
| 209 | n, err := newBoolNode(ctx, tk) |
| 210 | if err != nil { |
| 211 | return nil, err |
| 212 | } |
| 213 | node = n |
| 214 | case token.NullTag: |
| 215 | tk = &Token{Token: token.New("null", "null", &pos)} |
| 216 | n, err := newNullNode(ctx, tk) |
| 217 | if err != nil { |
| 218 | return nil, err |
| 219 | } |
| 220 | node = n |
| 221 | default: |
| 222 | return nil, errors.ErrSyntax(fmt.Sprintf("cannot assign default value for %q tag", tag.Value), tag) |
| 223 | } |
| 224 | ctx.insertToken(tk) |
| 225 | ctx.goNext() |
| 226 | return node, nil |
| 227 | } |
| 228 | |
| 229 | func setLineComment(ctx *context, node ast.Node, tk *Token) error { |
| 230 | if tk == nil || tk.LineComment == nil { |
| 231 | return nil |
| 232 | } |
| 233 | comment := ast.CommentGroup([]*token.Token{tk.LineComment}) |
| 234 | comment.SetPath(ctx.path) |
| 235 | if err := node.SetComment(comment); err != nil { |
| 236 | return err |
| 237 | } |
| 238 | return nil |
| 239 | } |
| 240 | |
| 241 | func setHeadComment(cm *ast.CommentGroupNode, value ast.Node) error { |
| 242 | if cm == nil { |
| 243 | return nil |
| 244 | } |
| 245 | switch n := value.(type) { |
| 246 | case *ast.MappingNode: |
| 247 | if len(n.Values) != 0 && value.GetComment() == nil { |
| 248 | cm.SetPath(n.Values[0].GetPath()) |
| 249 | return n.Values[0].SetComment(cm) |
| 250 | } |
| 251 | case *ast.MappingValueNode: |
| 252 | cm.SetPath(n.GetPath()) |
| 253 | return n.SetComment(cm) |
| 254 | } |
| 255 | cm.SetPath(value.GetPath()) |
| 256 | return value.SetComment(cm) |
| 257 | } |