-
-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
1,710 additions
and
856 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package parser | ||
|
||
import "fmt" | ||
|
||
const ( | ||
colorFgHiBlack int = iota + 90 | ||
colorFgHiRed | ||
colorFgHiGreen | ||
colorFgHiYellow | ||
colorFgHiBlue | ||
colorFgHiMagenta | ||
colorFgHiCyan | ||
) | ||
|
||
var colorTable = []int{ | ||
colorFgHiRed, | ||
colorFgHiGreen, | ||
colorFgHiYellow, | ||
colorFgHiBlue, | ||
colorFgHiMagenta, | ||
colorFgHiCyan, | ||
} | ||
|
||
func colorize(idx int, content string) string { | ||
colorIdx := idx % len(colorTable) | ||
color := colorTable[colorIdx] | ||
return fmt.Sprintf("\x1b[1;%dm", color) + content + "\x1b[22;0m" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,193 @@ | ||
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) | ||
} |
Oops, something went wrong.