internal: move DebugStr to new astinternal package
More robust.
Change-Id: I4ab0b510be244106cdcf75535ebe7a37e8e8c269
Reviewed-on: https://cue-review.googlesource.com/c/cue/+/9367
Reviewed-by: CUE cueckoo <cueckoo@gmail.com>
Reviewed-by: Paul Jolly <paul@myitcv.org.uk>
diff --git a/cmd/cue/cmd/orphans.go b/cmd/cue/cmd/orphans.go
index f1dcc10..e35dbb4 100644
--- a/cmd/cue/cmd/orphans.go
+++ b/cmd/cue/cmd/orphans.go
@@ -28,6 +28,7 @@
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
+ "cuelang.org/go/internal/astinternal"
"cuelang.org/go/internal/encoding"
)
@@ -220,7 +221,7 @@
}
return nil, fmt.Errorf(
`error evaluating label %v: %v`,
- internal.DebugStr(x), arg)
+ astinternal.DebugStr(x), arg)
}
}
a = append(a, cue.Label(label))
@@ -339,7 +340,7 @@
}
func (x *listIndex) label(label ast.Label) *listIndex {
- key := internal.DebugStr(label)
+ key := astinternal.DebugStr(label)
idx := x.index[key]
if idx == nil {
if x.field.Value == nil {
diff --git a/cue/parser/parser.go b/cue/parser/parser.go
index a5ded48..0a87b2b 100644
--- a/cue/parser/parser.go
+++ b/cue/parser/parser.go
@@ -25,8 +25,11 @@
"cuelang.org/go/cue/scanner"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
+ "cuelang.org/go/internal/astinternal"
)
+var debugStr = astinternal.DebugStr
+
// The parser structure holds the parser's internal state.
type parser struct {
file *token.File
diff --git a/cue/path.go b/cue/path.go
index 4f57056..6d7bc10 100644
--- a/cue/path.go
+++ b/cue/path.go
@@ -24,7 +24,7 @@
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/parser"
"cuelang.org/go/cue/token"
- "cuelang.org/go/internal"
+ "cuelang.org/go/internal/astinternal"
"cuelang.org/go/internal/core/adt"
"github.com/cockroachdb/apd/v2"
)
@@ -176,7 +176,7 @@
if b, ok := x.Index.(*ast.BasicLit); !ok {
sel = Selector{pathError{
errors.Newf(token.NoPos, "non-constant expression %s",
- internal.DebugStr(x.Index))}}
+ astinternal.DebugStr(x.Index))}}
} else {
sel = basicLitSelector(b)
}
@@ -188,7 +188,7 @@
default:
return []Selector{{pathError{
- errors.Newf(token.NoPos, "invalid label %s ", internal.DebugStr(x)),
+ errors.Newf(token.NoPos, "invalid label %s ", astinternal.DebugStr(x)),
}}}
}
}
@@ -267,7 +267,7 @@
default:
return Selector{pathError{
- errors.Newf(token.NoPos, "invalid label %s ", internal.DebugStr(x)),
+ errors.Newf(token.NoPos, "invalid label %s ", astinternal.DebugStr(x)),
}}
}
}
diff --git a/cue/types_test.go b/cue/types_test.go
index 12bb243..3315397 100644
--- a/cue/types_test.go
+++ b/cue/types_test.go
@@ -29,7 +29,7 @@
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/errors"
- "cuelang.org/go/internal"
+ "cuelang.org/go/internal/astinternal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/debug"
)
@@ -823,7 +823,7 @@
t.Errorf("got %v; want %v", got, tc.raw)
}
- got := fmt.Sprint(internal.DebugStr(v.Eval().Syntax()))
+ got := fmt.Sprint(astinternal.DebugStr(v.Eval().Syntax()))
if got != tc.eval {
t.Errorf("got %v; want %v", got, tc.eval)
}
@@ -845,7 +845,7 @@
t.Errorf("got %v; want %v", got, tc.raw)
}
- got = fmt.Sprint(internal.DebugStr(v.Eval().Syntax()))
+ got = fmt.Sprint(astinternal.DebugStr(v.Eval().Syntax()))
if got != tc.eval {
t.Errorf("got %v; want %v", got, tc.eval)
}
diff --git a/encoding/jsonschema/decode_test.go b/encoding/jsonschema/decode_test.go
index 252ae28..15b35d7 100644
--- a/encoding/jsonschema/decode_test.go
+++ b/encoding/jsonschema/decode_test.go
@@ -34,7 +34,7 @@
"cuelang.org/go/cue/token"
"cuelang.org/go/encoding/json"
"cuelang.org/go/encoding/yaml"
- "cuelang.org/go/internal"
+ "cuelang.org/go/internal/astinternal"
"cuelang.org/go/internal/cuetest"
_ "cuelang.org/go/pkg"
)
@@ -182,5 +182,5 @@
t.Fatal(err)
}
- t.Fatal(internal.DebugStr(expr))
+ t.Fatal(astinternal.DebugStr(expr))
}
diff --git a/cue/parser/print.go b/internal/astinternal/debugstr.go
similarity index 73%
rename from cue/parser/print.go
rename to internal/astinternal/debugstr.go
index 95f2d5c..c67b392 100644
--- a/cue/parser/print.go
+++ b/internal/astinternal/debugstr.go
@@ -1,4 +1,4 @@
-// Copyright 2018 The CUE Authors
+// Copyright 2021 CUE Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-package parser
+package astinternal
import (
"fmt"
@@ -21,18 +21,13 @@
"cuelang.org/go/cue/ast"
"cuelang.org/go/cue/token"
- "cuelang.org/go/internal"
)
-func init() {
- internal.DebugStr = debugStr
-}
-
-func debugStr(x interface{}) (out string) {
+func DebugStr(x interface{}) (out string) {
if n, ok := x.(ast.Node); ok {
comments := ""
for _, g := range n.Comments() {
- comments += debugStr(g)
+ comments += DebugStr(g)
}
if comments != "" {
defer func() { out = "<" + comments + out + ">" }()
@@ -41,25 +36,25 @@
switch v := x.(type) {
case *ast.File:
out := ""
- out += debugStr(v.Decls)
+ out += DebugStr(v.Decls)
return out
case *ast.Package:
out := "package "
- out += debugStr(v.Name)
+ out += DebugStr(v.Name)
return out
case *ast.LetClause:
out := "let "
- out += debugStr(v.Ident)
+ out += DebugStr(v.Ident)
out += "="
- out += debugStr(v.Expr)
+ out += DebugStr(v.Expr)
return out
case *ast.Alias:
- out := debugStr(v.Ident)
+ out := DebugStr(v.Ident)
out += "="
- out += debugStr(v.Expr)
+ out += DebugStr(v.Expr)
return out
case *ast.BottomLit:
@@ -70,75 +65,75 @@
case *ast.Interpolation:
for _, e := range v.Elts {
- out += debugStr(e)
+ out += DebugStr(e)
}
return out
case *ast.EmbedDecl:
- out += debugStr(v.Expr)
+ out += DebugStr(v.Expr)
return out
case *ast.ImportDecl:
out := "import "
if v.Lparen != token.NoPos {
out += "( "
- out += debugStr(v.Specs)
+ out += DebugStr(v.Specs)
out += " )"
} else {
- out += debugStr(v.Specs)
+ out += DebugStr(v.Specs)
}
return out
case *ast.Comprehension:
- out := debugStr(v.Clauses)
- out += debugStr(v.Value)
+ out := DebugStr(v.Clauses)
+ out += DebugStr(v.Value)
return out
case *ast.StructLit:
out := "{"
- out += debugStr(v.Elts)
+ out += DebugStr(v.Elts)
out += "}"
return out
case *ast.ListLit:
out := "["
- out += debugStr(v.Elts)
+ out += DebugStr(v.Elts)
out += "]"
return out
case *ast.Ellipsis:
out := "..."
if v.Type != nil {
- out += debugStr(v.Type)
+ out += DebugStr(v.Type)
}
return out
case *ast.ListComprehension:
out := "["
- out += debugStr(v.Expr)
+ out += DebugStr(v.Expr)
out += " "
- out += debugStr(v.Clauses)
+ out += DebugStr(v.Clauses)
out += "]"
return out
case *ast.ForClause:
out := "for "
if v.Key != nil {
- out += debugStr(v.Key)
+ out += DebugStr(v.Key)
out += ": "
}
- out += debugStr(v.Value)
+ out += DebugStr(v.Value)
out += " in "
- out += debugStr(v.Source)
+ out += DebugStr(v.Source)
return out
case *ast.IfClause:
out := "if "
- out += debugStr(v.Condition)
+ out += DebugStr(v.Condition)
return out
case *ast.Field:
- out := debugStr(v.Label)
+ out := DebugStr(v.Label)
if v.Optional != token.NoPos {
out += "?"
}
@@ -149,10 +144,10 @@
default:
out += fmt.Sprintf(" %s ", v.Token)
}
- out += debugStr(v.Value)
+ out += DebugStr(v.Value)
for _, a := range v.Attrs {
out += " "
- out += debugStr(a)
+ out += DebugStr(a)
}
}
return out
@@ -165,43 +160,43 @@
case *ast.TemplateLabel:
out := "<"
- out += debugStr(v.Ident)
+ out += DebugStr(v.Ident)
out += ">"
return out
case *ast.SelectorExpr:
- return debugStr(v.X) + "." + debugStr(v.Sel)
+ return DebugStr(v.X) + "." + DebugStr(v.Sel)
case *ast.CallExpr:
- out := debugStr(v.Fun)
+ out := DebugStr(v.Fun)
out += "("
- out += debugStr(v.Args)
+ out += DebugStr(v.Args)
out += ")"
return out
case *ast.ParenExpr:
out := "("
- out += debugStr(v.X)
+ out += DebugStr(v.X)
out += ")"
return out
case *ast.UnaryExpr:
- return v.Op.String() + debugStr(v.X)
+ return v.Op.String() + DebugStr(v.X)
case *ast.BinaryExpr:
- out := debugStr(v.X)
+ out := DebugStr(v.X)
op := v.Op.String()
if 'a' <= op[0] && op[0] <= 'z' {
op = fmt.Sprintf(" %s ", op)
}
out += op
- out += debugStr(v.Y)
+ out += DebugStr(v.Y)
return out
case []*ast.CommentGroup:
var a []string
for _, c := range v {
- a = append(a, debugStr(c))
+ a = append(a, DebugStr(c))
}
return strings.Join(a, "\n")
@@ -221,28 +216,28 @@
return str + strings.Join(a, " ") + "] "
case *ast.IndexExpr:
- out := debugStr(v.X)
+ out := DebugStr(v.X)
out += "["
- out += debugStr(v.Index)
+ out += DebugStr(v.Index)
out += "]"
return out
case *ast.SliceExpr:
- out := debugStr(v.X)
+ out := DebugStr(v.X)
out += "["
- out += debugStr(v.Low)
+ out += DebugStr(v.Low)
out += ":"
- out += debugStr(v.High)
+ out += DebugStr(v.High)
out += "]"
return out
case *ast.ImportSpec:
out := ""
if v.Name != nil {
- out += debugStr(v.Name)
+ out += DebugStr(v.Name)
out += " "
}
- out += debugStr(v.Path)
+ out += DebugStr(v.Path)
return out
case []ast.Decl:
@@ -251,7 +246,7 @@
}
out := ""
for _, d := range v {
- out += debugStr(d)
+ out += DebugStr(d)
out += sep
}
return out[:len(out)-len(sep)]
@@ -262,7 +257,7 @@
}
out := ""
for _, c := range v {
- out += debugStr(c)
+ out += DebugStr(c)
out += " "
}
return out
@@ -273,7 +268,7 @@
}
out := ""
for _, d := range v {
- out += debugStr(d)
+ out += DebugStr(d)
out += sep
}
return out[:len(out)-len(sep)]
@@ -284,7 +279,7 @@
}
out := ""
for _, d := range v {
- out += debugStr(d)
+ out += DebugStr(d)
out += sep
}
return out[:len(out)-len(sep)]
diff --git a/internal/core/compile/compile.go b/internal/core/compile/compile.go
index 16e2dbc..432ba2d 100644
--- a/internal/core/compile/compile.go
+++ b/internal/core/compile/compile.go
@@ -22,6 +22,7 @@
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/token"
"cuelang.org/go/internal"
+ "cuelang.org/go/internal/astinternal"
"cuelang.org/go/internal/core/adt"
)
@@ -945,7 +946,7 @@
func (c *compiler) assertConcreteIsPossible(src ast.Node, op adt.Op, x adt.Expr) bool {
if !adt.AssertConcreteIsPossible(op, x) {
- str := internal.DebugStr(src)
+ str := astinternal.DebugStr(src)
c.errf(src, "invalid operand %s ('%s' requires concrete value)", str, op)
}
return false
diff --git a/internal/core/export/export_test.go b/internal/core/export/export_test.go
index 73990b9..43bd421 100644
--- a/internal/core/export/export_test.go
+++ b/internal/core/export/export_test.go
@@ -23,7 +23,7 @@
"cuelang.org/go/cue/format"
"cuelang.org/go/cue/parser"
"cuelang.org/go/encoding/gocode/gocodec"
- "cuelang.org/go/internal"
+ "cuelang.org/go/internal/astinternal"
"cuelang.org/go/internal/core/adt"
"cuelang.org/go/internal/core/compile"
"cuelang.org/go/internal/core/convert"
@@ -146,7 +146,7 @@
if err != nil {
t.Fatal("failed export: ", err)
}
- got := internal.DebugStr(expr)
+ got := astinternal.DebugStr(expr)
if got != tc.out {
t.Errorf("got: %s\nwant: %s", got, tc.out)
}
diff --git a/internal/encoding/json/encode.go b/internal/encoding/json/encode.go
index 7a6b857..8f90035 100644
--- a/internal/encoding/json/encode.go
+++ b/internal/encoding/json/encode.go
@@ -24,7 +24,7 @@
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/token"
- "cuelang.org/go/internal"
+ "cuelang.org/go/internal/astinternal"
)
// Encode converts a CUE AST to JSON.
@@ -158,7 +158,7 @@
return e.encodeScalar(l, false)
}
}
- return errors.Newf(n.Pos(), "json: unsupported node %s (%T)", internal.DebugStr(n), n)
+ return errors.Newf(n.Pos(), "json: unsupported node %s (%T)", astinternal.DebugStr(n), n)
}
func (e *encoder) encodeScalar(l *ast.BasicLit, allowMinus bool) error {
@@ -220,7 +220,7 @@
for _, d := range decls {
switch x := d.(type) {
default:
- return errors.Newf(x.Pos(), "json: unsupported node %s (%T)", internal.DebugStr(x), x)
+ return errors.Newf(x.Pos(), "json: unsupported node %s (%T)", astinternal.DebugStr(x), x)
case *ast.Package:
if embed != nil || fields != nil {
diff --git a/internal/encoding/yaml/encode.go b/internal/encoding/yaml/encode.go
index 8fd0a61..5012842 100644
--- a/internal/encoding/yaml/encode.go
+++ b/internal/encoding/yaml/encode.go
@@ -25,7 +25,7 @@
"cuelang.org/go/cue/errors"
"cuelang.org/go/cue/literal"
"cuelang.org/go/cue/token"
- "cuelang.org/go/internal"
+ "cuelang.org/go/internal/astinternal"
)
// Encode converts a CUE AST to YAML.
@@ -87,9 +87,9 @@
break
}
}
- return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", internal.DebugStr(x), x)
+ return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", astinternal.DebugStr(x), x)
default:
- return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", internal.DebugStr(x), x)
+ return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", astinternal.DebugStr(x), x)
}
if err != nil {
return nil, err
@@ -170,7 +170,7 @@
for _, d := range decls {
switch x := d.(type) {
default:
- return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", internal.DebugStr(x), x)
+ return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", astinternal.DebugStr(x), x)
case *ast.Package:
if len(n.Content) > 0 {
diff --git a/internal/internal.go b/internal/internal.go
index b194b39..7544d95 100644
--- a/internal/internal.go
+++ b/internal/internal.go
@@ -39,9 +39,6 @@
// Right now Decimal is aliased to apd.Decimal. This may change in the future.
type Decimal = apd.Decimal
-// DebugStr prints a syntax node.
-var DebugStr func(x interface{}) string
-
// ErrIncomplete can be used by builtins to signal the evaluation was
// incomplete.
var ErrIncomplete = errors.New("incomplete value")
diff --git a/pkg/gen/gen.go b/pkg/gen/gen.go
index a3d395c..136b597 100644
--- a/pkg/gen/gen.go
+++ b/pkg/gen/gen.go
@@ -176,7 +176,7 @@
v := instances[0].Value().Syntax(cue.Raw())
// fmt.Printf("%T\n", v)
- // fmt.Println(internal.DebugStr(v))
+ // fmt.Println(astinternal.DebugStr(v))
n := internal.ToExpr(v)
b, err := cueformat.Node(n)
if err != nil {