mirror of
https://github.com/goccy/go-yaml.git
synced 2025-10-25 18:44:09 +00:00
194 lines
4.9 KiB
Go
194 lines
4.9 KiB
Go
|
|
package parser
|
||
|
|
|
||
|
|
import (
|
||
|
|
"github.com/goccy/go-yaml/ast"
|
||
|
|
"github.com/goccy/go-yaml/token"
|
||
|
|
)
|
||
|
|
|
||
|
|
func newMappingNode(ctx *context, tk *Token, isFlow bool, values ...*ast.MappingValueNode) (*ast.MappingNode, error) {
|
||
|
|
node := ast.Mapping(tk.RawToken(), isFlow, values...)
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newMappingValueNode(ctx *context, tk *Token, key ast.MapKeyNode, value ast.Node) (*ast.MappingValueNode, error) {
|
||
|
|
node := ast.MappingValue(tk.RawToken(), key, value)
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if key.GetToken().Position.Line == value.GetToken().Position.Line {
|
||
|
|
// originally key was commented, but now that null value has been added, value must be commented.
|
||
|
|
if err := setLineComment(ctx, value, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
if err := setLineComment(ctx, key, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newMappingKeyNode(ctx *context, tk *Token) (*ast.MappingKeyNode, error) {
|
||
|
|
node := ast.MappingKey(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newAnchorNode(ctx *context, tk *Token) (*ast.AnchorNode, error) {
|
||
|
|
node := ast.Anchor(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newAliasNode(ctx *context, tk *Token) (*ast.AliasNode, error) {
|
||
|
|
node := ast.Alias(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newDirectiveNode(ctx *context, tk *Token) (*ast.DirectiveNode, error) {
|
||
|
|
node := ast.Directive(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newMergeKeyNode(ctx *context, tk *Token) (*ast.MergeKeyNode, error) {
|
||
|
|
node := ast.MergeKey(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newNullNode(ctx *context, tk *Token) (*ast.NullNode, error) {
|
||
|
|
node := ast.Null(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newBoolNode(ctx *context, tk *Token) (*ast.BoolNode, error) {
|
||
|
|
node := ast.Bool(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newIntegerNode(ctx *context, tk *Token) (*ast.IntegerNode, error) {
|
||
|
|
node := ast.Integer(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newFloatNode(ctx *context, tk *Token) (*ast.FloatNode, error) {
|
||
|
|
node := ast.Float(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newInfinityNode(ctx *context, tk *Token) (*ast.InfinityNode, error) {
|
||
|
|
node := ast.Infinity(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newNanNode(ctx *context, tk *Token) (*ast.NanNode, error) {
|
||
|
|
node := ast.Nan(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newStringNode(ctx *context, tk *Token) (*ast.StringNode, error) {
|
||
|
|
node := ast.String(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newLiteralNode(ctx *context, tk *Token) (*ast.LiteralNode, error) {
|
||
|
|
node := ast.Literal(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newTagNode(ctx *context, tk *Token) (*ast.TagNode, error) {
|
||
|
|
node := ast.Tag(tk.RawToken())
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func newSequenceNode(ctx *context, tk *Token, isFlow bool) (*ast.SequenceNode, error) {
|
||
|
|
node := ast.Sequence(tk.RawToken(), isFlow)
|
||
|
|
node.SetPath(ctx.path)
|
||
|
|
if err := setLineComment(ctx, node, tk); err != nil {
|
||
|
|
return nil, err
|
||
|
|
}
|
||
|
|
return node, nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func setLineComment(ctx *context, node ast.Node, tk *Token) error {
|
||
|
|
if tk.LineComment == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
comment := ast.CommentGroup([]*token.Token{tk.LineComment})
|
||
|
|
comment.SetPath(ctx.path)
|
||
|
|
if err := node.SetComment(comment); err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
|
||
|
|
func setHeadComment(cm *ast.CommentGroupNode, value ast.Node) error {
|
||
|
|
if cm == nil {
|
||
|
|
return nil
|
||
|
|
}
|
||
|
|
switch n := value.(type) {
|
||
|
|
case *ast.MappingNode:
|
||
|
|
if len(n.Values) != 0 && value.GetComment() == nil {
|
||
|
|
cm.SetPath(n.Values[0].GetPath())
|
||
|
|
return n.Values[0].SetComment(cm)
|
||
|
|
}
|
||
|
|
case *ast.MappingValueNode:
|
||
|
|
cm.SetPath(n.GetPath())
|
||
|
|
return n.SetComment(cm)
|
||
|
|
}
|
||
|
|
cm.SetPath(value.GetPath())
|
||
|
|
return value.SetComment(cm)
|
||
|
|
}
|