2023-10-03 05:22:23 +00:00
|
|
|
//go:build !yq_nolua
|
|
|
|
|
2023-08-11 02:56:49 +00:00
|
|
|
package yqlib
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type luaEncoder struct {
|
|
|
|
docPrefix string
|
|
|
|
docSuffix string
|
|
|
|
indent int
|
|
|
|
indentStr string
|
|
|
|
unquoted bool
|
|
|
|
globals bool
|
|
|
|
escape *strings.Replacer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (le *luaEncoder) CanHandleAliases() bool {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewLuaEncoder(prefs LuaPreferences) Encoder {
|
|
|
|
escape := strings.NewReplacer(
|
|
|
|
"\000", "\\000",
|
|
|
|
"\001", "\\001",
|
|
|
|
"\002", "\\002",
|
|
|
|
"\003", "\\003",
|
|
|
|
"\004", "\\004",
|
|
|
|
"\005", "\\005",
|
|
|
|
"\006", "\\006",
|
|
|
|
"\007", "\\a",
|
|
|
|
"\010", "\\b",
|
|
|
|
"\011", "\\t",
|
|
|
|
"\012", "\\n",
|
|
|
|
"\013", "\\v",
|
|
|
|
"\014", "\\f",
|
|
|
|
"\015", "\\r",
|
|
|
|
"\016", "\\014",
|
|
|
|
"\017", "\\015",
|
|
|
|
"\020", "\\016",
|
|
|
|
"\021", "\\017",
|
|
|
|
"\022", "\\018",
|
|
|
|
"\023", "\\019",
|
|
|
|
"\024", "\\020",
|
|
|
|
"\025", "\\021",
|
|
|
|
"\026", "\\022",
|
|
|
|
"\027", "\\023",
|
|
|
|
"\030", "\\024",
|
|
|
|
"\031", "\\025",
|
|
|
|
"\032", "\\026",
|
|
|
|
"\033", "\\027",
|
|
|
|
"\034", "\\028",
|
|
|
|
"\035", "\\029",
|
|
|
|
"\036", "\\030",
|
|
|
|
"\037", "\\031",
|
|
|
|
"\"", "\\\"",
|
|
|
|
"'", "\\'",
|
|
|
|
"\\", "\\\\",
|
|
|
|
"\177", "\\127",
|
|
|
|
)
|
|
|
|
unescape := strings.NewReplacer(
|
|
|
|
"\\'", "'",
|
|
|
|
"\\\"", "\"",
|
|
|
|
"\\n", "\n",
|
|
|
|
"\\r", "\r",
|
|
|
|
"\\t", "\t",
|
|
|
|
"\\\\", "\\",
|
|
|
|
)
|
|
|
|
return &luaEncoder{unescape.Replace(prefs.DocPrefix), unescape.Replace(prefs.DocSuffix), 0, "\t", prefs.UnquotedKeys, prefs.Globals, escape}
|
|
|
|
}
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func (le *luaEncoder) PrintDocumentSeparator(_ io.Writer) error {
|
2023-08-11 02:56:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-11 02:17:34 +00:00
|
|
|
func (le *luaEncoder) PrintLeadingContent(_ io.Writer, _ string) error {
|
2023-08-11 02:56:49 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (le *luaEncoder) encodeString(writer io.Writer, node *CandidateNode) error {
|
2023-08-11 02:56:49 +00:00
|
|
|
quote := "\""
|
|
|
|
switch node.Style {
|
2023-10-18 01:11:53 +00:00
|
|
|
case LiteralStyle, FoldedStyle, FlowStyle:
|
2023-08-11 02:56:49 +00:00
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
if !strings.Contains(node.Value, "]"+strings.Repeat("=", i)+"]") {
|
2024-03-12 05:43:53 +00:00
|
|
|
err := WriteString(writer, "["+strings.Repeat("=", i)+"[\n")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
err = WriteString(writer, node.Value)
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, "]"+strings.Repeat("=", i)+"]")
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-18 01:11:53 +00:00
|
|
|
case SingleQuotedStyle:
|
2023-08-11 02:56:49 +00:00
|
|
|
quote = "'"
|
|
|
|
|
2023-09-18 23:52:36 +00:00
|
|
|
// fallthrough to regular ol' string
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, quote+le.escape.Replace(node.Value)+quote)
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (le *luaEncoder) writeIndent(writer io.Writer) error {
|
|
|
|
if le.indentStr == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
err := WriteString(writer, "\n")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, strings.Repeat(le.indentStr, le.indent))
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (le *luaEncoder) encodeArray(writer io.Writer, node *CandidateNode) error {
|
2024-03-12 05:43:53 +00:00
|
|
|
err := WriteString(writer, "{")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
le.indent++
|
|
|
|
for _, child := range node.Content {
|
|
|
|
err = le.writeIndent(writer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-10-12 00:25:26 +00:00
|
|
|
err := le.encodeAny(writer, child)
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
err = WriteString(writer, ",")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if child.LineComment != "" {
|
|
|
|
sansPrefix, _ := strings.CutPrefix(child.LineComment, "#")
|
2024-03-12 05:43:53 +00:00
|
|
|
err = WriteString(writer, " --"+sansPrefix)
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
le.indent--
|
|
|
|
if len(node.Content) != 0 {
|
|
|
|
err = le.writeIndent(writer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, "}")
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func needsQuoting(s string) bool {
|
|
|
|
// known keywords as of Lua 5.4
|
|
|
|
switch s {
|
|
|
|
case "do", "and", "else", "break",
|
|
|
|
"if", "end", "goto", "false",
|
|
|
|
"in", "for", "then", "local",
|
|
|
|
"or", "nil", "true", "until",
|
|
|
|
"elseif", "function", "not",
|
|
|
|
"repeat", "return", "while":
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
// [%a_][%w_]*
|
|
|
|
for i, c := range s {
|
|
|
|
if i == 0 {
|
|
|
|
if !((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || c == '_') {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (le *luaEncoder) encodeMap(writer io.Writer, node *CandidateNode, global bool) error {
|
2023-08-11 02:56:49 +00:00
|
|
|
if !global {
|
2024-03-12 05:43:53 +00:00
|
|
|
err := WriteString(writer, "{")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
le.indent++
|
|
|
|
}
|
|
|
|
for i, child := range node.Content {
|
|
|
|
if (i % 2) == 1 {
|
|
|
|
// value
|
2023-10-12 00:25:26 +00:00
|
|
|
err := le.encodeAny(writer, child)
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
err = WriteString(writer, ";")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// key
|
|
|
|
if !global || i > 0 {
|
|
|
|
err := le.writeIndent(writer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (le.unquoted || global) && child.Tag == "!!str" && !needsQuoting(child.Value) {
|
2024-03-12 05:43:53 +00:00
|
|
|
err := WriteString(writer, child.Value+" = ")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if global {
|
|
|
|
// This only works in Lua 5.2+
|
2024-03-12 05:43:53 +00:00
|
|
|
err := WriteString(writer, "_ENV")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
err := WriteString(writer, "[")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = le.encodeAny(writer, child)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
err = WriteString(writer, "] = ")
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if child.LineComment != "" {
|
|
|
|
sansPrefix, _ := strings.CutPrefix(child.LineComment, "#")
|
2024-03-12 05:43:53 +00:00
|
|
|
err := WriteString(writer, strings.Repeat(" ", i%2)+"--"+sansPrefix)
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if (i % 2) == 0 {
|
|
|
|
// newline and indent after comments on keys
|
|
|
|
err = le.writeIndent(writer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if global {
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, "\n")
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
|
|
|
le.indent--
|
|
|
|
if len(node.Content) != 0 {
|
|
|
|
err := le.writeIndent(writer)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, "}")
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (le *luaEncoder) encodeAny(writer io.Writer, node *CandidateNode) error {
|
2023-08-11 02:56:49 +00:00
|
|
|
switch node.Kind {
|
2023-10-18 01:11:53 +00:00
|
|
|
case SequenceNode:
|
2023-08-11 02:56:49 +00:00
|
|
|
return le.encodeArray(writer, node)
|
2023-10-18 01:11:53 +00:00
|
|
|
case MappingNode:
|
2023-08-11 02:56:49 +00:00
|
|
|
return le.encodeMap(writer, node, false)
|
2023-10-18 01:11:53 +00:00
|
|
|
case ScalarNode:
|
|
|
|
switch node.Tag {
|
2023-08-11 02:56:49 +00:00
|
|
|
case "!!str":
|
|
|
|
return le.encodeString(writer, node)
|
|
|
|
case "!!null":
|
|
|
|
// TODO reject invalid use as a table key
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, "nil")
|
2023-08-11 02:56:49 +00:00
|
|
|
case "!!bool":
|
|
|
|
// Yaml 1.2 has case variation e.g. True, FALSE etc but Lua only has
|
|
|
|
// lower case
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, strings.ToLower(node.Value))
|
2023-08-11 02:56:49 +00:00
|
|
|
case "!!int":
|
|
|
|
if strings.HasPrefix(node.Value, "0o") {
|
2023-10-18 01:11:53 +00:00
|
|
|
_, octalValue, err := parseInt64(node.Value)
|
2023-08-11 02:56:49 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, fmt.Sprintf("%d", octalValue))
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, strings.ToLower(node.Value))
|
2023-08-11 02:56:49 +00:00
|
|
|
case "!!float":
|
|
|
|
switch strings.ToLower(node.Value) {
|
2023-10-12 00:25:26 +00:00
|
|
|
case ".inf", "+.inf":
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, "(1/0)")
|
2023-08-11 02:56:49 +00:00
|
|
|
case "-.inf":
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, "(-1/0)")
|
2023-08-11 02:56:49 +00:00
|
|
|
case ".nan":
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, "(0/0)")
|
2023-08-11 02:56:49 +00:00
|
|
|
default:
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, node.Value)
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
|
|
|
default:
|
2023-10-18 01:11:53 +00:00
|
|
|
return fmt.Errorf("Lua encoder NYI -- %s", node.Tag)
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
|
|
|
default:
|
2023-10-18 01:11:53 +00:00
|
|
|
return fmt.Errorf("Lua encoder NYI -- %s", node.Tag)
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (le *luaEncoder) encodeTopLevel(writer io.Writer, node *CandidateNode) error {
|
2024-03-12 05:43:53 +00:00
|
|
|
err := WriteString(writer, le.docPrefix)
|
2023-10-12 00:25:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = le.encodeAny(writer, node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-03-12 05:43:53 +00:00
|
|
|
return WriteString(writer, le.docSuffix)
|
2023-10-12 00:25:26 +00:00
|
|
|
}
|
|
|
|
|
2023-10-18 01:11:53 +00:00
|
|
|
func (le *luaEncoder) Encode(writer io.Writer, node *CandidateNode) error {
|
|
|
|
|
2023-10-12 00:25:26 +00:00
|
|
|
if le.globals {
|
2023-10-18 01:11:53 +00:00
|
|
|
if node.Kind != MappingNode {
|
2023-10-12 00:25:26 +00:00
|
|
|
return fmt.Errorf("--lua-global requires a top level MappingNode")
|
|
|
|
}
|
|
|
|
return le.encodeMap(writer, node, true)
|
|
|
|
}
|
|
|
|
return le.encodeTopLevel(writer, node)
|
2023-08-11 02:56:49 +00:00
|
|
|
}
|