this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

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>

+75 -78
+3 -2
cmd/cue/cmd/orphans.go
··· 28 28 "cuelang.org/go/cue/parser" 29 29 "cuelang.org/go/cue/token" 30 30 "cuelang.org/go/internal" 31 + "cuelang.org/go/internal/astinternal" 31 32 "cuelang.org/go/internal/encoding" 32 33 ) 33 34 ··· 220 221 } 221 222 return nil, fmt.Errorf( 222 223 `error evaluating label %v: %v`, 223 - internal.DebugStr(x), arg) 224 + astinternal.DebugStr(x), arg) 224 225 } 225 226 } 226 227 a = append(a, cue.Label(label)) ··· 339 340 } 340 341 341 342 func (x *listIndex) label(label ast.Label) *listIndex { 342 - key := internal.DebugStr(label) 343 + key := astinternal.DebugStr(label) 343 344 idx := x.index[key] 344 345 if idx == nil { 345 346 if x.field.Value == nil {
+3
cue/parser/parser.go
··· 25 25 "cuelang.org/go/cue/scanner" 26 26 "cuelang.org/go/cue/token" 27 27 "cuelang.org/go/internal" 28 + "cuelang.org/go/internal/astinternal" 28 29 ) 30 + 31 + var debugStr = astinternal.DebugStr 29 32 30 33 // The parser structure holds the parser's internal state. 31 34 type parser struct {
+48 -53
cue/parser/print.go internal/astinternal/debugstr.go
··· 1 - // Copyright 2018 The CUE Authors 1 + // Copyright 2021 CUE Authors 2 2 // 3 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 4 // you may not use this file except in compliance with the License. ··· 12 12 // See the License for the specific language governing permissions and 13 13 // limitations under the License. 14 14 15 - package parser 15 + package astinternal 16 16 17 17 import ( 18 18 "fmt" ··· 21 21 22 22 "cuelang.org/go/cue/ast" 23 23 "cuelang.org/go/cue/token" 24 - "cuelang.org/go/internal" 25 24 ) 26 25 27 - func init() { 28 - internal.DebugStr = debugStr 29 - } 30 - 31 - func debugStr(x interface{}) (out string) { 26 + func DebugStr(x interface{}) (out string) { 32 27 if n, ok := x.(ast.Node); ok { 33 28 comments := "" 34 29 for _, g := range n.Comments() { 35 - comments += debugStr(g) 30 + comments += DebugStr(g) 36 31 } 37 32 if comments != "" { 38 33 defer func() { out = "<" + comments + out + ">" }() ··· 41 36 switch v := x.(type) { 42 37 case *ast.File: 43 38 out := "" 44 - out += debugStr(v.Decls) 39 + out += DebugStr(v.Decls) 45 40 return out 46 41 47 42 case *ast.Package: 48 43 out := "package " 49 - out += debugStr(v.Name) 44 + out += DebugStr(v.Name) 50 45 return out 51 46 52 47 case *ast.LetClause: 53 48 out := "let " 54 - out += debugStr(v.Ident) 49 + out += DebugStr(v.Ident) 55 50 out += "=" 56 - out += debugStr(v.Expr) 51 + out += DebugStr(v.Expr) 57 52 return out 58 53 59 54 case *ast.Alias: 60 - out := debugStr(v.Ident) 55 + out := DebugStr(v.Ident) 61 56 out += "=" 62 - out += debugStr(v.Expr) 57 + out += DebugStr(v.Expr) 63 58 return out 64 59 65 60 case *ast.BottomLit: ··· 70 65 71 66 case *ast.Interpolation: 72 67 for _, e := range v.Elts { 73 - out += debugStr(e) 68 + out += DebugStr(e) 74 69 } 75 70 return out 76 71 77 72 case *ast.EmbedDecl: 78 - out += debugStr(v.Expr) 73 + out += DebugStr(v.Expr) 79 74 return out 80 75 81 76 case *ast.ImportDecl: 82 77 out := "import " 83 78 if v.Lparen != token.NoPos { 84 79 out += "( " 85 - out += debugStr(v.Specs) 80 + out += DebugStr(v.Specs) 86 81 out += " )" 87 82 } else { 88 - out += debugStr(v.Specs) 83 + out += DebugStr(v.Specs) 89 84 } 90 85 return out 91 86 92 87 case *ast.Comprehension: 93 - out := debugStr(v.Clauses) 94 - out += debugStr(v.Value) 88 + out := DebugStr(v.Clauses) 89 + out += DebugStr(v.Value) 95 90 return out 96 91 97 92 case *ast.StructLit: 98 93 out := "{" 99 - out += debugStr(v.Elts) 94 + out += DebugStr(v.Elts) 100 95 out += "}" 101 96 return out 102 97 103 98 case *ast.ListLit: 104 99 out := "[" 105 - out += debugStr(v.Elts) 100 + out += DebugStr(v.Elts) 106 101 out += "]" 107 102 return out 108 103 109 104 case *ast.Ellipsis: 110 105 out := "..." 111 106 if v.Type != nil { 112 - out += debugStr(v.Type) 107 + out += DebugStr(v.Type) 113 108 } 114 109 return out 115 110 116 111 case *ast.ListComprehension: 117 112 out := "[" 118 - out += debugStr(v.Expr) 113 + out += DebugStr(v.Expr) 119 114 out += " " 120 - out += debugStr(v.Clauses) 115 + out += DebugStr(v.Clauses) 121 116 out += "]" 122 117 return out 123 118 124 119 case *ast.ForClause: 125 120 out := "for " 126 121 if v.Key != nil { 127 - out += debugStr(v.Key) 122 + out += DebugStr(v.Key) 128 123 out += ": " 129 124 } 130 - out += debugStr(v.Value) 125 + out += DebugStr(v.Value) 131 126 out += " in " 132 - out += debugStr(v.Source) 127 + out += DebugStr(v.Source) 133 128 return out 134 129 135 130 case *ast.IfClause: 136 131 out := "if " 137 - out += debugStr(v.Condition) 132 + out += DebugStr(v.Condition) 138 133 return out 139 134 140 135 case *ast.Field: 141 - out := debugStr(v.Label) 136 + out := DebugStr(v.Label) 142 137 if v.Optional != token.NoPos { 143 138 out += "?" 144 139 } ··· 149 144 default: 150 145 out += fmt.Sprintf(" %s ", v.Token) 151 146 } 152 - out += debugStr(v.Value) 147 + out += DebugStr(v.Value) 153 148 for _, a := range v.Attrs { 154 149 out += " " 155 - out += debugStr(a) 150 + out += DebugStr(a) 156 151 } 157 152 } 158 153 return out ··· 165 160 166 161 case *ast.TemplateLabel: 167 162 out := "<" 168 - out += debugStr(v.Ident) 163 + out += DebugStr(v.Ident) 169 164 out += ">" 170 165 return out 171 166 172 167 case *ast.SelectorExpr: 173 - return debugStr(v.X) + "." + debugStr(v.Sel) 168 + return DebugStr(v.X) + "." + DebugStr(v.Sel) 174 169 175 170 case *ast.CallExpr: 176 - out := debugStr(v.Fun) 171 + out := DebugStr(v.Fun) 177 172 out += "(" 178 - out += debugStr(v.Args) 173 + out += DebugStr(v.Args) 179 174 out += ")" 180 175 return out 181 176 182 177 case *ast.ParenExpr: 183 178 out := "(" 184 - out += debugStr(v.X) 179 + out += DebugStr(v.X) 185 180 out += ")" 186 181 return out 187 182 188 183 case *ast.UnaryExpr: 189 - return v.Op.String() + debugStr(v.X) 184 + return v.Op.String() + DebugStr(v.X) 190 185 191 186 case *ast.BinaryExpr: 192 - out := debugStr(v.X) 187 + out := DebugStr(v.X) 193 188 op := v.Op.String() 194 189 if 'a' <= op[0] && op[0] <= 'z' { 195 190 op = fmt.Sprintf(" %s ", op) 196 191 } 197 192 out += op 198 - out += debugStr(v.Y) 193 + out += DebugStr(v.Y) 199 194 return out 200 195 201 196 case []*ast.CommentGroup: 202 197 var a []string 203 198 for _, c := range v { 204 - a = append(a, debugStr(c)) 199 + a = append(a, DebugStr(c)) 205 200 } 206 201 return strings.Join(a, "\n") 207 202 ··· 221 216 return str + strings.Join(a, " ") + "] " 222 217 223 218 case *ast.IndexExpr: 224 - out := debugStr(v.X) 219 + out := DebugStr(v.X) 225 220 out += "[" 226 - out += debugStr(v.Index) 221 + out += DebugStr(v.Index) 227 222 out += "]" 228 223 return out 229 224 230 225 case *ast.SliceExpr: 231 - out := debugStr(v.X) 226 + out := DebugStr(v.X) 232 227 out += "[" 233 - out += debugStr(v.Low) 228 + out += DebugStr(v.Low) 234 229 out += ":" 235 - out += debugStr(v.High) 230 + out += DebugStr(v.High) 236 231 out += "]" 237 232 return out 238 233 239 234 case *ast.ImportSpec: 240 235 out := "" 241 236 if v.Name != nil { 242 - out += debugStr(v.Name) 237 + out += DebugStr(v.Name) 243 238 out += " " 244 239 } 245 - out += debugStr(v.Path) 240 + out += DebugStr(v.Path) 246 241 return out 247 242 248 243 case []ast.Decl: ··· 251 246 } 252 247 out := "" 253 248 for _, d := range v { 254 - out += debugStr(d) 249 + out += DebugStr(d) 255 250 out += sep 256 251 } 257 252 return out[:len(out)-len(sep)] ··· 262 257 } 263 258 out := "" 264 259 for _, c := range v { 265 - out += debugStr(c) 260 + out += DebugStr(c) 266 261 out += " " 267 262 } 268 263 return out ··· 273 268 } 274 269 out := "" 275 270 for _, d := range v { 276 - out += debugStr(d) 271 + out += DebugStr(d) 277 272 out += sep 278 273 } 279 274 return out[:len(out)-len(sep)] ··· 284 279 } 285 280 out := "" 286 281 for _, d := range v { 287 - out += debugStr(d) 282 + out += DebugStr(d) 288 283 out += sep 289 284 } 290 285 return out[:len(out)-len(sep)]
+4 -4
cue/path.go
··· 24 24 "cuelang.org/go/cue/literal" 25 25 "cuelang.org/go/cue/parser" 26 26 "cuelang.org/go/cue/token" 27 - "cuelang.org/go/internal" 27 + "cuelang.org/go/internal/astinternal" 28 28 "cuelang.org/go/internal/core/adt" 29 29 "github.com/cockroachdb/apd/v2" 30 30 ) ··· 176 176 if b, ok := x.Index.(*ast.BasicLit); !ok { 177 177 sel = Selector{pathError{ 178 178 errors.Newf(token.NoPos, "non-constant expression %s", 179 - internal.DebugStr(x.Index))}} 179 + astinternal.DebugStr(x.Index))}} 180 180 } else { 181 181 sel = basicLitSelector(b) 182 182 } ··· 188 188 189 189 default: 190 190 return []Selector{{pathError{ 191 - errors.Newf(token.NoPos, "invalid label %s ", internal.DebugStr(x)), 191 + errors.Newf(token.NoPos, "invalid label %s ", astinternal.DebugStr(x)), 192 192 }}} 193 193 } 194 194 } ··· 267 267 268 268 default: 269 269 return Selector{pathError{ 270 - errors.Newf(token.NoPos, "invalid label %s ", internal.DebugStr(x)), 270 + errors.Newf(token.NoPos, "invalid label %s ", astinternal.DebugStr(x)), 271 271 }} 272 272 } 273 273 }
+3 -3
cue/types_test.go
··· 29 29 30 30 "cuelang.org/go/cue/ast" 31 31 "cuelang.org/go/cue/errors" 32 - "cuelang.org/go/internal" 32 + "cuelang.org/go/internal/astinternal" 33 33 "cuelang.org/go/internal/core/adt" 34 34 "cuelang.org/go/internal/core/debug" 35 35 ) ··· 823 823 t.Errorf("got %v; want %v", got, tc.raw) 824 824 } 825 825 826 - got := fmt.Sprint(internal.DebugStr(v.Eval().Syntax())) 826 + got := fmt.Sprint(astinternal.DebugStr(v.Eval().Syntax())) 827 827 if got != tc.eval { 828 828 t.Errorf("got %v; want %v", got, tc.eval) 829 829 } ··· 845 845 t.Errorf("got %v; want %v", got, tc.raw) 846 846 } 847 847 848 - got = fmt.Sprint(internal.DebugStr(v.Eval().Syntax())) 848 + got = fmt.Sprint(astinternal.DebugStr(v.Eval().Syntax())) 849 849 if got != tc.eval { 850 850 t.Errorf("got %v; want %v", got, tc.eval) 851 851 }
+2 -2
encoding/jsonschema/decode_test.go
··· 34 34 "cuelang.org/go/cue/token" 35 35 "cuelang.org/go/encoding/json" 36 36 "cuelang.org/go/encoding/yaml" 37 - "cuelang.org/go/internal" 37 + "cuelang.org/go/internal/astinternal" 38 38 "cuelang.org/go/internal/cuetest" 39 39 _ "cuelang.org/go/pkg" 40 40 ) ··· 182 182 t.Fatal(err) 183 183 } 184 184 185 - t.Fatal(internal.DebugStr(expr)) 185 + t.Fatal(astinternal.DebugStr(expr)) 186 186 }
+2 -1
internal/core/compile/compile.go
··· 22 22 "cuelang.org/go/cue/literal" 23 23 "cuelang.org/go/cue/token" 24 24 "cuelang.org/go/internal" 25 + "cuelang.org/go/internal/astinternal" 25 26 "cuelang.org/go/internal/core/adt" 26 27 ) 27 28 ··· 945 946 946 947 func (c *compiler) assertConcreteIsPossible(src ast.Node, op adt.Op, x adt.Expr) bool { 947 948 if !adt.AssertConcreteIsPossible(op, x) { 948 - str := internal.DebugStr(src) 949 + str := astinternal.DebugStr(src) 949 950 c.errf(src, "invalid operand %s ('%s' requires concrete value)", str, op) 950 951 } 951 952 return false
+2 -2
internal/core/export/export_test.go
··· 23 23 "cuelang.org/go/cue/format" 24 24 "cuelang.org/go/cue/parser" 25 25 "cuelang.org/go/encoding/gocode/gocodec" 26 - "cuelang.org/go/internal" 26 + "cuelang.org/go/internal/astinternal" 27 27 "cuelang.org/go/internal/core/adt" 28 28 "cuelang.org/go/internal/core/compile" 29 29 "cuelang.org/go/internal/core/convert" ··· 146 146 if err != nil { 147 147 t.Fatal("failed export: ", err) 148 148 } 149 - got := internal.DebugStr(expr) 149 + got := astinternal.DebugStr(expr) 150 150 if got != tc.out { 151 151 t.Errorf("got: %s\nwant: %s", got, tc.out) 152 152 }
+3 -3
internal/encoding/json/encode.go
··· 24 24 "cuelang.org/go/cue/errors" 25 25 "cuelang.org/go/cue/literal" 26 26 "cuelang.org/go/cue/token" 27 - "cuelang.org/go/internal" 27 + "cuelang.org/go/internal/astinternal" 28 28 ) 29 29 30 30 // Encode converts a CUE AST to JSON. ··· 158 158 return e.encodeScalar(l, false) 159 159 } 160 160 } 161 - return errors.Newf(n.Pos(), "json: unsupported node %s (%T)", internal.DebugStr(n), n) 161 + return errors.Newf(n.Pos(), "json: unsupported node %s (%T)", astinternal.DebugStr(n), n) 162 162 } 163 163 164 164 func (e *encoder) encodeScalar(l *ast.BasicLit, allowMinus bool) error { ··· 220 220 for _, d := range decls { 221 221 switch x := d.(type) { 222 222 default: 223 - return errors.Newf(x.Pos(), "json: unsupported node %s (%T)", internal.DebugStr(x), x) 223 + return errors.Newf(x.Pos(), "json: unsupported node %s (%T)", astinternal.DebugStr(x), x) 224 224 225 225 case *ast.Package: 226 226 if embed != nil || fields != nil {
+4 -4
internal/encoding/yaml/encode.go
··· 25 25 "cuelang.org/go/cue/errors" 26 26 "cuelang.org/go/cue/literal" 27 27 "cuelang.org/go/cue/token" 28 - "cuelang.org/go/internal" 28 + "cuelang.org/go/internal/astinternal" 29 29 ) 30 30 31 31 // Encode converts a CUE AST to YAML. ··· 87 87 break 88 88 } 89 89 } 90 - return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", internal.DebugStr(x), x) 90 + return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", astinternal.DebugStr(x), x) 91 91 default: 92 - return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", internal.DebugStr(x), x) 92 + return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", astinternal.DebugStr(x), x) 93 93 } 94 94 if err != nil { 95 95 return nil, err ··· 170 170 for _, d := range decls { 171 171 switch x := d.(type) { 172 172 default: 173 - return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", internal.DebugStr(x), x) 173 + return nil, errors.Newf(x.Pos(), "yaml: unsupported node %s (%T)", astinternal.DebugStr(x), x) 174 174 175 175 case *ast.Package: 176 176 if len(n.Content) > 0 {
-3
internal/internal.go
··· 39 39 // Right now Decimal is aliased to apd.Decimal. This may change in the future. 40 40 type Decimal = apd.Decimal 41 41 42 - // DebugStr prints a syntax node. 43 - var DebugStr func(x interface{}) string 44 - 45 42 // ErrIncomplete can be used by builtins to signal the evaluation was 46 43 // incomplete. 47 44 var ErrIncomplete = errors.New("incomplete value")
+1 -1
pkg/gen/gen.go
··· 176 176 177 177 v := instances[0].Value().Syntax(cue.Raw()) 178 178 // fmt.Printf("%T\n", v) 179 - // fmt.Println(internal.DebugStr(v)) 179 + // fmt.Println(astinternal.DebugStr(v)) 180 180 n := internal.ToExpr(v) 181 181 b, err := cueformat.Node(n) 182 182 if err != nil {